blob: a1b0ff56bc353fffb9752516663af2e8b45fc7bb [file] [log] [blame]
Zhu Yib481de92007-09-25 17:54:57 -07001/******************************************************************************
2 *
Reinette Chatre01f81622009-01-08 10:20:02 -08003 * Copyright(c) 2005 - 2009 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:
Winkler, Tomas759ef892008-12-09 11:28:58 -080022 * Intel Linux Wireless <ilw@linux.intel.com>
Zhu Yib481de92007-09-25 17:54:57 -070023 * 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
Tomas Winkler3e0d4cb2008-04-24 11:55:38 -070038#include "iwl-dev.h"
Emmanuel Grumbachbe1f3ab62008-06-12 09:47:18 +080039#include "iwl-sta.h"
Tomas Winkler857485c2008-03-21 13:53:44 -070040#include "iwl-core.h"
Zhu Yib481de92007-09-25 17:54:57 -070041
Tomas Winklere227cea2008-07-18 13:53:05 +080042#define RS_NAME "iwl-agn-rs"
Zhu Yib481de92007-09-25 17:54:57 -070043
Guy Cohenaade00c2008-04-23 17:14:59 -070044#define NUM_TRY_BEFORE_ANT_TOGGLE 1
Zhu Yib481de92007-09-25 17:54:57 -070045#define IWL_NUMBER_TRY 1
46#define IWL_HT_NUMBER_TRY 3
47
Ben Cahill77626352007-11-29 11:09:44 +080048#define IWL_RATE_MAX_WINDOW 62 /* # tx in history window */
49#define IWL_RATE_MIN_FAILURE_TH 6 /* min failures to calc tpt */
50#define IWL_RATE_MIN_SUCCESS_TH 8 /* min successes to calc tpt */
51
Abbas, Mohamed7d049e52009-01-20 21:33:53 -080052/* max allowed rate miss before sync LQ cmd */
53#define IWL_MISSED_RATE_MAX 15
Ben Cahill77626352007-11-29 11:09:44 +080054/* max time to accum history 2 seconds */
Mohamed Abbas447fee72009-04-20 14:37:02 -070055#define IWL_RATE_SCALE_FLUSH_INTVL (3*HZ)
Zhu Yib481de92007-09-25 17:54:57 -070056
57static u8 rs_ht_to_legacy[] = {
58 IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
59 IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
60 IWL_RATE_6M_INDEX,
61 IWL_RATE_6M_INDEX, IWL_RATE_9M_INDEX,
62 IWL_RATE_12M_INDEX, IWL_RATE_18M_INDEX,
63 IWL_RATE_24M_INDEX, IWL_RATE_36M_INDEX,
64 IWL_RATE_48M_INDEX, IWL_RATE_54M_INDEX
65};
66
Guy Cohenaade00c2008-04-23 17:14:59 -070067static const u8 ant_toggle_lookup[] = {
68 /*ANT_NONE -> */ ANT_NONE,
69 /*ANT_A -> */ ANT_B,
70 /*ANT_B -> */ ANT_C,
71 /*ANT_AB -> */ ANT_BC,
72 /*ANT_C -> */ ANT_A,
73 /*ANT_AC -> */ ANT_AB,
74 /*ANT_BC -> */ ANT_AC,
75 /*ANT_ABC -> */ ANT_ABC,
76};
77
Ben Cahill77626352007-11-29 11:09:44 +080078/**
Tomas Winklere227cea2008-07-18 13:53:05 +080079 * struct iwl_rate_scale_data -- tx success history for one rate
Ben Cahill77626352007-11-29 11:09:44 +080080 */
Tomas Winklere227cea2008-07-18 13:53:05 +080081struct iwl_rate_scale_data {
Ben Cahill77626352007-11-29 11:09:44 +080082 u64 data; /* bitmap of successful frames */
83 s32 success_counter; /* number of frames successful */
84 s32 success_ratio; /* per-cent * 128 */
85 s32 counter; /* number of frames attempted */
86 s32 average_tpt; /* success ratio * expected throughput */
Zhu Yib481de92007-09-25 17:54:57 -070087 unsigned long stamp;
88};
89
Ben Cahill77626352007-11-29 11:09:44 +080090/**
Tomas Winklere227cea2008-07-18 13:53:05 +080091 * struct iwl_scale_tbl_info -- tx params and success history for all rates
Ben Cahill77626352007-11-29 11:09:44 +080092 *
Tomas Winklere227cea2008-07-18 13:53:05 +080093 * There are two of these in struct iwl_lq_sta,
Ben Cahill77626352007-11-29 11:09:44 +080094 * one for "active", and one for "search".
95 */
Tomas Winklere227cea2008-07-18 13:53:05 +080096struct iwl_scale_tbl_info {
Guy Cohenfde0db32008-04-21 15:42:01 -070097 enum iwl_table_type lq_type;
98 u8 ant_type;
Ben Cahill77626352007-11-29 11:09:44 +080099 u8 is_SGI; /* 1 = short guard interval */
100 u8 is_fat; /* 1 = 40 MHz channel width */
101 u8 is_dup; /* 1 = duplicated data streams */
102 u8 action; /* change modulation; IWL_[LEGACY/SISO/MIMO]_SWITCH_* */
Mohamed Abbasd6e93392009-05-08 13:44:39 -0700103 u8 max_search; /* maximun number of tables we can search */
Ben Cahill77626352007-11-29 11:09:44 +0800104 s32 *expected_tpt; /* throughput metrics; expected_tpt_G, etc. */
Guy Cohen39e88502008-04-23 17:14:57 -0700105 u32 current_rate; /* rate_n_flags, uCode API format */
Tomas Winklere227cea2008-07-18 13:53:05 +0800106 struct iwl_rate_scale_data win[IWL_RATE_COUNT]; /* rate histories */
Zhu Yib481de92007-09-25 17:54:57 -0700107};
108
Tomas Winklere227cea2008-07-18 13:53:05 +0800109struct iwl_traffic_load {
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200110 unsigned long time_stamp; /* age of the oldest statistics */
111 u32 packet_count[TID_QUEUE_MAX_SIZE]; /* packet count in this time
112 * slice */
113 u32 total; /* total num of packets during the
114 * last TID_MAX_TIME_DIFF */
115 u8 queue_count; /* number of queues that has
116 * been used since the last cleanup */
117 u8 head; /* start of the circular buffer */
118};
119
Ben Cahill77626352007-11-29 11:09:44 +0800120/**
Tomas Winklere227cea2008-07-18 13:53:05 +0800121 * struct iwl_lq_sta -- driver's rate scaling private structure
Ben Cahill77626352007-11-29 11:09:44 +0800122 *
123 * Pointer to this gets passed back and forth between driver and mac80211.
124 */
Tomas Winklere227cea2008-07-18 13:53:05 +0800125struct iwl_lq_sta {
Ben Cahill77626352007-11-29 11:09:44 +0800126 u8 active_tbl; /* index of active table, range 0-1 */
127 u8 enable_counter; /* indicates HT mode */
128 u8 stay_in_tbl; /* 1: disallow, 0: allow search for new mode */
129 u8 search_better_tbl; /* 1: currently trying alternate mode */
Zhu Yib481de92007-09-25 17:54:57 -0700130 s32 last_tpt;
Ben Cahill77626352007-11-29 11:09:44 +0800131
132 /* The following determine when to search for a new mode */
Zhu Yib481de92007-09-25 17:54:57 -0700133 u32 table_count_limit;
Ben Cahill77626352007-11-29 11:09:44 +0800134 u32 max_failure_limit; /* # failed frames before new search */
135 u32 max_success_limit; /* # successful frames before new search */
Zhu Yib481de92007-09-25 17:54:57 -0700136 u32 table_count;
Ben Cahill77626352007-11-29 11:09:44 +0800137 u32 total_failed; /* total failed frames, any/all rates */
138 u32 total_success; /* total successful frames, any/all rates */
Mohamed Abbas447fee72009-04-20 14:37:02 -0700139 u64 flush_timer; /* time staying in mode before new search */
Ben Cahill77626352007-11-29 11:09:44 +0800140
141 u8 action_counter; /* # mode-switch actions tried */
Zhu Yib481de92007-09-25 17:54:57 -0700142 u8 is_green;
143 u8 is_dup;
Johannes Berg8318d782008-01-24 19:38:38 +0100144 enum ieee80211_band band;
Zhu Yib481de92007-09-25 17:54:57 -0700145 u8 ibss_sta_added;
Ben Cahill77626352007-11-29 11:09:44 +0800146
147 /* The following are bitmaps of rates; IWL_RATE_6M_MASK, etc. */
Zhu Yi02dede02007-09-27 11:27:40 +0800148 u32 supp_rates;
Guy Cohen39e88502008-04-23 17:14:57 -0700149 u16 active_legacy_rate;
Zhu Yib481de92007-09-25 17:54:57 -0700150 u16 active_siso_rate;
Guy Cohenfde0db32008-04-21 15:42:01 -0700151 u16 active_mimo2_rate;
152 u16 active_mimo3_rate;
Zhu Yib481de92007-09-25 17:54:57 -0700153 u16 active_rate_basic;
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -0800154 s8 max_rate_idx; /* Max rate set by user */
Abbas, Mohamed7d049e52009-01-20 21:33:53 -0800155 u8 missed_rate_counter;
Ben Cahill77626352007-11-29 11:09:44 +0800156
Tomas Winkler66c73db2008-04-15 16:01:40 -0700157 struct iwl_link_quality_cmd lq;
Tomas Winklere227cea2008-07-18 13:53:05 +0800158 struct iwl_scale_tbl_info lq_info[LQ_SIZE]; /* "active", "search" */
159 struct iwl_traffic_load load[TID_MAX_LOAD_COUNT];
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200160 u8 tx_agg_tid_en;
Zhu Yi5ae212c2007-09-27 11:27:38 +0800161#ifdef CONFIG_MAC80211_DEBUGFS
Zhu Yi98d7e092007-09-27 11:27:42 +0800162 struct dentry *rs_sta_dbgfs_scale_table_file;
Zhu Yi0209dc12007-09-27 11:27:43 +0800163 struct dentry *rs_sta_dbgfs_stats_table_file;
Wey-Yi Guy38167452009-05-08 13:44:43 -0700164 struct dentry *rs_sta_dbgfs_rate_scale_data_file;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200165 struct dentry *rs_sta_dbgfs_tx_agg_tid_en_file;
Guy Cohen39e88502008-04-23 17:14:57 -0700166 u32 dbg_fixed_rate;
Zhu Yi5ae212c2007-09-27 11:27:38 +0800167#endif
Zhu Yi62430652008-05-05 10:22:46 +0800168 struct iwl_priv *drv;
Johannes Bergb7e35002008-09-11 02:22:58 +0200169
170 /* used to be in sta_info */
171 int last_txrate_idx;
Wey-Yi Guy12b96812009-04-08 11:39:27 -0700172 /* last tx rate_n_flags */
173 u32 last_rate_n_flags;
Zhu Yib481de92007-09-25 17:54:57 -0700174};
175
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700176static void rs_rate_scale_perform(struct iwl_priv *priv,
Gábor Stefanik514d65c2009-04-23 19:36:08 +0200177 struct sk_buff *skb,
Johannes Berg4b7679a2008-09-18 18:14:18 +0200178 struct ieee80211_sta *sta,
179 struct iwl_lq_sta *lq_sta);
Guy Cohenfde0db32008-04-21 15:42:01 -0700180static void rs_fill_link_cmd(const struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +0800181 struct iwl_lq_sta *lq_sta, u32 rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -0700182
183
Zhu Yi98d7e092007-09-27 11:27:42 +0800184#ifdef CONFIG_MAC80211_DEBUGFS
Tomas Winklere227cea2008-07-18 13:53:05 +0800185static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
186 u32 *rate_n_flags, int index);
Zhu Yi98d7e092007-09-27 11:27:42 +0800187#else
Tomas Winklere227cea2008-07-18 13:53:05 +0800188static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
189 u32 *rate_n_flags, int index)
Zhu Yi98d7e092007-09-27 11:27:42 +0800190{}
191#endif
Ben Cahill77626352007-11-29 11:09:44 +0800192
193/*
194 * Expected throughput metrics for following rates:
195 * 1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
196 * "G" is the only table that supports CCK (the first 4 rates).
197 */
Wey-Yi Guy584a0f02009-04-01 14:33:24 -0700198
Zhu Yib481de92007-09-25 17:54:57 -0700199static s32 expected_tpt_A[IWL_RATE_COUNT] = {
200 0, 0, 0, 0, 40, 57, 72, 98, 121, 154, 177, 186, 186
201};
202
203static s32 expected_tpt_G[IWL_RATE_COUNT] = {
204 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 186
205};
206
207static s32 expected_tpt_siso20MHz[IWL_RATE_COUNT] = {
208 0, 0, 0, 0, 42, 42, 76, 102, 124, 159, 183, 193, 202
209};
210
211static s32 expected_tpt_siso20MHzSGI[IWL_RATE_COUNT] = {
212 0, 0, 0, 0, 46, 46, 82, 110, 132, 168, 192, 202, 211
213};
214
Wey-Yi Guy584a0f02009-04-01 14:33:24 -0700215static s32 expected_tpt_mimo2_20MHz[IWL_RATE_COUNT] = {
Zhu Yib481de92007-09-25 17:54:57 -0700216 0, 0, 0, 0, 74, 74, 123, 155, 179, 214, 236, 244, 251
217};
218
Wey-Yi Guy584a0f02009-04-01 14:33:24 -0700219static s32 expected_tpt_mimo2_20MHzSGI[IWL_RATE_COUNT] = {
Zhu Yib481de92007-09-25 17:54:57 -0700220 0, 0, 0, 0, 81, 81, 131, 164, 188, 222, 243, 251, 257
221};
222
223static s32 expected_tpt_siso40MHz[IWL_RATE_COUNT] = {
224 0, 0, 0, 0, 77, 77, 127, 160, 184, 220, 242, 250, 257
225};
226
227static s32 expected_tpt_siso40MHzSGI[IWL_RATE_COUNT] = {
228 0, 0, 0, 0, 83, 83, 135, 169, 193, 229, 250, 257, 264
229};
230
Wey-Yi Guy584a0f02009-04-01 14:33:24 -0700231static s32 expected_tpt_mimo2_40MHz[IWL_RATE_COUNT] = {
Zhu Yib481de92007-09-25 17:54:57 -0700232 0, 0, 0, 0, 123, 123, 182, 214, 235, 264, 279, 285, 289
233};
234
Wey-Yi Guy584a0f02009-04-01 14:33:24 -0700235static s32 expected_tpt_mimo2_40MHzSGI[IWL_RATE_COUNT] = {
Zhu Yib481de92007-09-25 17:54:57 -0700236 0, 0, 0, 0, 131, 131, 191, 222, 242, 270, 284, 289, 293
237};
238
Wey-Yi Guy584a0f02009-04-01 14:33:24 -0700239/* Expected throughput metric MIMO3 */
240static s32 expected_tpt_mimo3_20MHz[IWL_RATE_COUNT] = {
241 0, 0, 0, 0, 99, 99, 153, 186, 208, 239, 256, 263, 268
242};
243
244static s32 expected_tpt_mimo3_20MHzSGI[IWL_RATE_COUNT] = {
245 0, 0, 0, 0, 106, 106, 162, 194, 215, 246, 262, 268, 273
246};
247
248static s32 expected_tpt_mimo3_40MHz[IWL_RATE_COUNT] = {
249 0, 0, 0, 0, 152, 152, 211, 239, 255, 279, 290, 294, 297
250};
251
252static s32 expected_tpt_mimo3_40MHzSGI[IWL_RATE_COUNT] = {
253 0, 0, 0, 0, 160, 160, 219, 245, 261, 284, 294, 297, 300
254};
255
Wey-Yi Guy12b96812009-04-08 11:39:27 -0700256/* mbps, mcs */
257const static struct iwl_rate_mcs_info iwl_rate_mcs[IWL_RATE_COUNT] = {
258 {"1", ""},
259 {"2", ""},
260 {"5.5", ""},
261 {"11", ""},
262 {"6", "BPSK 1/2"},
263 {"9", "BPSK 1/2"},
264 {"12", "QPSK 1/2"},
265 {"18", "QPSK 3/4"},
266 {"24", "16QAM 1/2"},
267 {"36", "16QAM 3/4"},
268 {"48", "64QAM 2/3"},
269 {"54", "64QAM 3/4"},
270 {"60", "64QAM 5/6"}
271};
272
Guy Cohen39e88502008-04-23 17:14:57 -0700273static inline u8 rs_extract_rate(u32 rate_n_flags)
Zhu Yib481de92007-09-25 17:54:57 -0700274{
275 return (u8)(rate_n_flags & 0xFF);
276}
277
Tomas Winklere227cea2008-07-18 13:53:05 +0800278static void rs_rate_scale_clear_window(struct iwl_rate_scale_data *window)
Zhu Yib481de92007-09-25 17:54:57 -0700279{
280 window->data = 0;
281 window->success_counter = 0;
282 window->success_ratio = IWL_INVALID_VALUE;
283 window->counter = 0;
284 window->average_tpt = IWL_INVALID_VALUE;
285 window->stamp = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700286}
287
Guy Cohenaade00c2008-04-23 17:14:59 -0700288static inline u8 rs_is_valid_ant(u8 valid_antenna, u8 ant_type)
289{
Tomas Winkler3ac7f142008-07-21 02:40:14 +0300290 return (ant_type & valid_antenna) == ant_type;
Guy Cohenaade00c2008-04-23 17:14:59 -0700291}
292
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200293/*
294 * removes the old data from the statistics. All data that is older than
295 * TID_MAX_TIME_DIFF, will be deleted.
296 */
Tomas Winklere227cea2008-07-18 13:53:05 +0800297static void rs_tl_rm_old_stats(struct iwl_traffic_load *tl, u32 curr_time)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200298{
299 /* The oldest age we want to keep */
300 u32 oldest_time = curr_time - TID_MAX_TIME_DIFF;
301
302 while (tl->queue_count &&
303 (tl->time_stamp < oldest_time)) {
304 tl->total -= tl->packet_count[tl->head];
305 tl->packet_count[tl->head] = 0;
306 tl->time_stamp += TID_QUEUE_CELL_SPACING;
307 tl->queue_count--;
308 tl->head++;
309 if (tl->head >= TID_QUEUE_MAX_SIZE)
310 tl->head = 0;
311 }
312}
313
314/*
315 * increment traffic load value for tid and also remove
316 * any old values if passed the certain time period
317 */
Tomas Winklere227cea2008-07-18 13:53:05 +0800318static u8 rs_tl_add_packet(struct iwl_lq_sta *lq_data,
Ron Rindjunskyfaa29712008-06-12 09:46:58 +0800319 struct ieee80211_hdr *hdr)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200320{
321 u32 curr_time = jiffies_to_msecs(jiffies);
322 u32 time_diff;
323 s32 index;
Tomas Winklere227cea2008-07-18 13:53:05 +0800324 struct iwl_traffic_load *tl = NULL;
Tomas Winkler54dbb522008-05-15 13:54:06 +0800325 u8 tid;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200326
Tomas Winkler417f1142008-11-12 13:14:06 -0800327 if (ieee80211_is_data_qos(hdr->frame_control)) {
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700328 u8 *qc = ieee80211_get_qos_ctl(hdr);
Tomas Winkler54dbb522008-05-15 13:54:06 +0800329 tid = qc[0] & 0xf;
330 } else
Ron Rindjunskyfaa29712008-06-12 09:46:58 +0800331 return MAX_TID_COUNT;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200332
333 tl = &lq_data->load[tid];
334
335 curr_time -= curr_time % TID_ROUND_VALUE;
336
337 /* Happens only for the first packet. Initialize the data */
338 if (!(tl->queue_count)) {
339 tl->total = 1;
340 tl->time_stamp = curr_time;
341 tl->queue_count = 1;
342 tl->head = 0;
343 tl->packet_count[0] = 1;
Ron Rindjunskyfaa29712008-06-12 09:46:58 +0800344 return MAX_TID_COUNT;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200345 }
346
347 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
348 index = time_diff / TID_QUEUE_CELL_SPACING;
349
350 /* The history is too long: remove data that is older than */
351 /* TID_MAX_TIME_DIFF */
352 if (index >= TID_QUEUE_MAX_SIZE)
353 rs_tl_rm_old_stats(tl, curr_time);
354
355 index = (tl->head + index) % TID_QUEUE_MAX_SIZE;
356 tl->packet_count[index] = tl->packet_count[index] + 1;
357 tl->total = tl->total + 1;
358
359 if ((index + 1) > tl->queue_count)
360 tl->queue_count = index + 1;
Ron Rindjunskyfaa29712008-06-12 09:46:58 +0800361
362 return tid;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200363}
364
365/*
366 get the traffic load value for tid
367*/
Tomas Winklere227cea2008-07-18 13:53:05 +0800368static u32 rs_tl_get_load(struct iwl_lq_sta *lq_data, u8 tid)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200369{
370 u32 curr_time = jiffies_to_msecs(jiffies);
371 u32 time_diff;
372 s32 index;
Tomas Winklere227cea2008-07-18 13:53:05 +0800373 struct iwl_traffic_load *tl = NULL;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200374
375 if (tid >= TID_MAX_LOAD_COUNT)
376 return 0;
377
378 tl = &(lq_data->load[tid]);
379
380 curr_time -= curr_time % TID_ROUND_VALUE;
381
382 if (!(tl->queue_count))
383 return 0;
384
385 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
386 index = time_diff / TID_QUEUE_CELL_SPACING;
387
388 /* The history is too long: remove data that is older than */
389 /* TID_MAX_TIME_DIFF */
390 if (index >= TID_QUEUE_MAX_SIZE)
391 rs_tl_rm_old_stats(tl, curr_time);
392
393 return tl->total;
394}
395
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700396static void rs_tl_turn_on_agg_for_tid(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +0800397 struct iwl_lq_sta *lq_data, u8 tid,
Johannes Berg4b7679a2008-09-18 18:14:18 +0200398 struct ieee80211_sta *sta)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200399{
Johannes Bergff550cb2008-09-11 03:17:05 +0200400 if (rs_tl_get_load(lq_data, tid) > IWL_AGG_LOAD_THRESHOLD) {
Tomas Winklere1623442009-01-27 14:27:56 -0800401 IWL_DEBUG_HT(priv, "Starting Tx agg: STA: %pM tid: %d\n",
Johannes Berge1749612008-10-27 15:59:26 -0700402 sta->addr, tid);
Johannes Berg4b7679a2008-09-18 18:14:18 +0200403 ieee80211_start_tx_ba_session(priv->hw, sta->addr, tid);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200404 }
405}
406
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700407static void rs_tl_turn_on_agg(struct iwl_priv *priv, u8 tid,
Tomas Winklere227cea2008-07-18 13:53:05 +0800408 struct iwl_lq_sta *lq_data,
Johannes Berg4b7679a2008-09-18 18:14:18 +0200409 struct ieee80211_sta *sta)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200410{
411 if ((tid < TID_MAX_LOAD_COUNT))
412 rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
413 else if (tid == IWL_AGG_ALL_TID)
414 for (tid = 0; tid < TID_MAX_LOAD_COUNT; tid++)
415 rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
416}
417
Guy Cohen39e88502008-04-23 17:14:57 -0700418static inline int get_num_of_ant_from_rate(u32 rate_n_flags)
Guy Cohenfde0db32008-04-21 15:42:01 -0700419{
Tomas Winkler3ac7f142008-07-21 02:40:14 +0300420 return !!(rate_n_flags & RATE_MCS_ANT_A_MSK) +
421 !!(rate_n_flags & RATE_MCS_ANT_B_MSK) +
422 !!(rate_n_flags & RATE_MCS_ANT_C_MSK);
Guy Cohenfde0db32008-04-21 15:42:01 -0700423}
424
Ben Cahill77626352007-11-29 11:09:44 +0800425/**
426 * rs_collect_tx_data - Update the success/failure sliding window
427 *
428 * We keep a sliding window of the last 62 packets transmitted
429 * at this rate. window->data contains the bitmask of successful
430 * packets.
431 */
Tomas Winklere227cea2008-07-18 13:53:05 +0800432static int rs_collect_tx_data(struct iwl_rate_scale_data *windows,
Ron Rindjunsky99556432008-01-28 14:07:25 +0200433 int scale_index, s32 tpt, int retries,
434 int successes)
Zhu Yib481de92007-09-25 17:54:57 -0700435{
Tomas Winklere227cea2008-07-18 13:53:05 +0800436 struct iwl_rate_scale_data *window = NULL;
Guy Cohen39e88502008-04-23 17:14:57 -0700437 static const u64 mask = (((u64)1) << (IWL_RATE_MAX_WINDOW - 1));
Zhu Yib481de92007-09-25 17:54:57 -0700438 s32 fail_count;
439
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800440 if (scale_index < 0 || scale_index >= IWL_RATE_COUNT)
441 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -0700442
Ben Cahill77626352007-11-29 11:09:44 +0800443 /* Select data for current tx bit rate */
Zhu Yib481de92007-09-25 17:54:57 -0700444 window = &(windows[scale_index]);
445
Ben Cahill77626352007-11-29 11:09:44 +0800446 /*
447 * Keep track of only the latest 62 tx frame attempts in this rate's
448 * history window; anything older isn't really relevant any more.
449 * If we have filled up the sliding window, drop the oldest attempt;
450 * if the oldest attempt (highest bit in bitmap) shows "success",
451 * subtract "1" from the success counter (this is the main reason
452 * we keep these bitmaps!).
453 */
Ron Rindjunsky99556432008-01-28 14:07:25 +0200454 while (retries > 0) {
Guy Cohen39e88502008-04-23 17:14:57 -0700455 if (window->counter >= IWL_RATE_MAX_WINDOW) {
456
457 /* remove earliest */
458 window->counter = IWL_RATE_MAX_WINDOW - 1;
459
Ron Rindjunsky99556432008-01-28 14:07:25 +0200460 if (window->data & mask) {
461 window->data &= ~mask;
Guy Cohen39e88502008-04-23 17:14:57 -0700462 window->success_counter--;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200463 }
Zhu Yib481de92007-09-25 17:54:57 -0700464 }
Zhu Yib481de92007-09-25 17:54:57 -0700465
Ron Rindjunsky99556432008-01-28 14:07:25 +0200466 /* Increment frames-attempted counter */
467 window->counter++;
Ben Cahill77626352007-11-29 11:09:44 +0800468
Ron Rindjunsky99556432008-01-28 14:07:25 +0200469 /* Shift bitmap by one frame (throw away oldest history),
470 * OR in "1", and increment "success" if this
471 * frame was successful. */
Guy Cohen90d77952008-09-09 10:54:53 +0800472 window->data <<= 1;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200473 if (successes > 0) {
Guy Cohen39e88502008-04-23 17:14:57 -0700474 window->success_counter++;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200475 window->data |= 0x1;
476 successes--;
477 }
478
479 retries--;
Zhu Yib481de92007-09-25 17:54:57 -0700480 }
481
Ben Cahill77626352007-11-29 11:09:44 +0800482 /* Calculate current success ratio, avoid divide-by-0! */
Zhu Yib481de92007-09-25 17:54:57 -0700483 if (window->counter > 0)
484 window->success_ratio = 128 * (100 * window->success_counter)
485 / window->counter;
486 else
487 window->success_ratio = IWL_INVALID_VALUE;
488
489 fail_count = window->counter - window->success_counter;
490
Ben Cahill77626352007-11-29 11:09:44 +0800491 /* Calculate average throughput, if we have enough history. */
Zhu Yib481de92007-09-25 17:54:57 -0700492 if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
493 (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
494 window->average_tpt = (window->success_ratio * tpt + 64) / 128;
495 else
496 window->average_tpt = IWL_INVALID_VALUE;
497
Ben Cahill77626352007-11-29 11:09:44 +0800498 /* Tag this window as having been updated */
Zhu Yib481de92007-09-25 17:54:57 -0700499 window->stamp = jiffies;
500
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800501 return 0;
Zhu Yib481de92007-09-25 17:54:57 -0700502}
503
Ben Cahill77626352007-11-29 11:09:44 +0800504/*
505 * Fill uCode API rate_n_flags field, based on "search" or "active" table.
506 */
Guy Cohen39e88502008-04-23 17:14:57 -0700507/* FIXME:RS:remove this function and put the flags statically in the table */
Tomas Winkler978785a2008-12-19 10:37:31 +0800508static u32 rate_n_flags_from_tbl(struct iwl_priv *priv,
509 struct iwl_scale_tbl_info *tbl,
510 int index, u8 use_green)
Zhu Yib481de92007-09-25 17:54:57 -0700511{
Guy Cohen39e88502008-04-23 17:14:57 -0700512 u32 rate_n_flags = 0;
513
Zhu Yib481de92007-09-25 17:54:57 -0700514 if (is_legacy(tbl->lq_type)) {
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800515 rate_n_flags = iwl_rates[index].plcp;
Zhu Yib481de92007-09-25 17:54:57 -0700516 if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
Guy Cohen39e88502008-04-23 17:14:57 -0700517 rate_n_flags |= RATE_MCS_CCK_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700518
Guy Cohenfde0db32008-04-21 15:42:01 -0700519 } else if (is_Ht(tbl->lq_type)) {
520 if (index > IWL_LAST_OFDM_RATE) {
Tomas Winkler978785a2008-12-19 10:37:31 +0800521 IWL_ERR(priv, "Invalid HT rate index %d\n", index);
Zhu Yib481de92007-09-25 17:54:57 -0700522 index = IWL_LAST_OFDM_RATE;
Guy Cohenfde0db32008-04-21 15:42:01 -0700523 }
Guy Cohen39e88502008-04-23 17:14:57 -0700524 rate_n_flags = RATE_MCS_HT_MSK;
Guy Cohenfde0db32008-04-21 15:42:01 -0700525
526 if (is_siso(tbl->lq_type))
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800527 rate_n_flags |= iwl_rates[index].plcp_siso;
Guy Cohenfde0db32008-04-21 15:42:01 -0700528 else if (is_mimo2(tbl->lq_type))
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800529 rate_n_flags |= iwl_rates[index].plcp_mimo2;
Guy Cohenfde0db32008-04-21 15:42:01 -0700530 else
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800531 rate_n_flags |= iwl_rates[index].plcp_mimo3;
Zhu Yib481de92007-09-25 17:54:57 -0700532 } else {
Tomas Winkler978785a2008-12-19 10:37:31 +0800533 IWL_ERR(priv, "Invalid tbl->lq_type %d\n", tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -0700534 }
535
Guy Cohen39e88502008-04-23 17:14:57 -0700536 rate_n_flags |= ((tbl->ant_type << RATE_MCS_ANT_POS) &
Guy Cohenfde0db32008-04-21 15:42:01 -0700537 RATE_MCS_ANT_ABC_MSK);
Zhu Yib481de92007-09-25 17:54:57 -0700538
Guy Cohen39e88502008-04-23 17:14:57 -0700539 if (is_Ht(tbl->lq_type)) {
540 if (tbl->is_fat) {
541 if (tbl->is_dup)
542 rate_n_flags |= RATE_MCS_DUP_MSK;
543 else
544 rate_n_flags |= RATE_MCS_FAT_MSK;
545 }
546 if (tbl->is_SGI)
547 rate_n_flags |= RATE_MCS_SGI_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700548
Guy Cohen39e88502008-04-23 17:14:57 -0700549 if (use_green) {
550 rate_n_flags |= RATE_MCS_GF_MSK;
551 if (is_siso(tbl->lq_type) && tbl->is_SGI) {
552 rate_n_flags &= ~RATE_MCS_SGI_MSK;
Tomas Winkler978785a2008-12-19 10:37:31 +0800553 IWL_ERR(priv, "GF was set with SGI:SISO\n");
Guy Cohen39e88502008-04-23 17:14:57 -0700554 }
555 }
Zhu Yib481de92007-09-25 17:54:57 -0700556 }
Guy Cohen39e88502008-04-23 17:14:57 -0700557 return rate_n_flags;
Zhu Yib481de92007-09-25 17:54:57 -0700558}
559
Ben Cahill77626352007-11-29 11:09:44 +0800560/*
561 * Interpret uCode API's rate_n_flags format,
562 * fill "search" or "active" tx mode table.
563 */
Guy Cohen39e88502008-04-23 17:14:57 -0700564static int rs_get_tbl_info_from_mcs(const u32 rate_n_flags,
Johannes Berg8318d782008-01-24 19:38:38 +0100565 enum ieee80211_band band,
Tomas Winklere227cea2008-07-18 13:53:05 +0800566 struct iwl_scale_tbl_info *tbl,
Zhu Yib481de92007-09-25 17:54:57 -0700567 int *rate_idx)
568{
Guy Cohen39e88502008-04-23 17:14:57 -0700569 u32 ant_msk = (rate_n_flags & RATE_MCS_ANT_ABC_MSK);
570 u8 num_of_ant = get_num_of_ant_from_rate(rate_n_flags);
571 u8 mcs;
Zhu Yib481de92007-09-25 17:54:57 -0700572
Tomas Winklere7d326a2008-06-12 09:47:11 +0800573 *rate_idx = iwl_hwrate_to_plcp_idx(rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -0700574
Guy Cohenfde0db32008-04-21 15:42:01 -0700575 if (*rate_idx == IWL_RATE_INVALID) {
Zhu Yib481de92007-09-25 17:54:57 -0700576 *rate_idx = -1;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800577 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -0700578 }
Ben Cahill77626352007-11-29 11:09:44 +0800579 tbl->is_SGI = 0; /* default legacy setup */
Zhu Yib481de92007-09-25 17:54:57 -0700580 tbl->is_fat = 0;
581 tbl->is_dup = 0;
Guy Cohenfde0db32008-04-21 15:42:01 -0700582 tbl->ant_type = (ant_msk >> RATE_MCS_ANT_POS);
583 tbl->lq_type = LQ_NONE;
Mohamed Abbasd6e93392009-05-08 13:44:39 -0700584 tbl->max_search = IWL_MAX_SEARCH;
Zhu Yib481de92007-09-25 17:54:57 -0700585
Ben Cahill77626352007-11-29 11:09:44 +0800586 /* legacy rate format */
Guy Cohen39e88502008-04-23 17:14:57 -0700587 if (!(rate_n_flags & RATE_MCS_HT_MSK)) {
Guy Cohenfde0db32008-04-21 15:42:01 -0700588 if (num_of_ant == 1) {
Johannes Berg8318d782008-01-24 19:38:38 +0100589 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700590 tbl->lq_type = LQ_A;
591 else
592 tbl->lq_type = LQ_G;
Zhu Yib481de92007-09-25 17:54:57 -0700593 }
Guy Cohenfde0db32008-04-21 15:42:01 -0700594 /* HT rate format */
Zhu Yib481de92007-09-25 17:54:57 -0700595 } else {
Guy Cohen39e88502008-04-23 17:14:57 -0700596 if (rate_n_flags & RATE_MCS_SGI_MSK)
Zhu Yib481de92007-09-25 17:54:57 -0700597 tbl->is_SGI = 1;
598
Guy Cohen39e88502008-04-23 17:14:57 -0700599 if ((rate_n_flags & RATE_MCS_FAT_MSK) ||
600 (rate_n_flags & RATE_MCS_DUP_MSK))
Zhu Yib481de92007-09-25 17:54:57 -0700601 tbl->is_fat = 1;
602
Guy Cohen39e88502008-04-23 17:14:57 -0700603 if (rate_n_flags & RATE_MCS_DUP_MSK)
Zhu Yib481de92007-09-25 17:54:57 -0700604 tbl->is_dup = 1;
Guy Cohenfde0db32008-04-21 15:42:01 -0700605
Guy Cohen39e88502008-04-23 17:14:57 -0700606 mcs = rs_extract_rate(rate_n_flags);
Guy Cohenfde0db32008-04-21 15:42:01 -0700607
Guy Cohen39e88502008-04-23 17:14:57 -0700608 /* SISO */
609 if (mcs <= IWL_RATE_SISO_60M_PLCP) {
Guy Cohenfde0db32008-04-21 15:42:01 -0700610 if (num_of_ant == 1)
611 tbl->lq_type = LQ_SISO; /*else NONE*/
612 /* MIMO2 */
Guy Cohen39e88502008-04-23 17:14:57 -0700613 } else if (mcs <= IWL_RATE_MIMO2_60M_PLCP) {
Guy Cohenfde0db32008-04-21 15:42:01 -0700614 if (num_of_ant == 2)
615 tbl->lq_type = LQ_MIMO2;
616 /* MIMO3 */
617 } else {
Mohamed Abbasd6e93392009-05-08 13:44:39 -0700618 if (num_of_ant == 3) {
619 tbl->max_search = IWL_MAX_11N_MIMO3_SEARCH;
Guy Cohenfde0db32008-04-21 15:42:01 -0700620 tbl->lq_type = LQ_MIMO3;
Mohamed Abbasd6e93392009-05-08 13:44:39 -0700621 }
Guy Cohenfde0db32008-04-21 15:42:01 -0700622 }
Zhu Yib481de92007-09-25 17:54:57 -0700623 }
624 return 0;
625}
Guy Cohenaade00c2008-04-23 17:14:59 -0700626
627/* switch to another antenna/antennas and return 1 */
628/* if no other valid antenna found, return 0 */
629static int rs_toggle_antenna(u32 valid_ant, u32 *rate_n_flags,
Tomas Winklere227cea2008-07-18 13:53:05 +0800630 struct iwl_scale_tbl_info *tbl)
Zhu Yib481de92007-09-25 17:54:57 -0700631{
Guy Cohenaade00c2008-04-23 17:14:59 -0700632 u8 new_ant_type;
633
634 if (!tbl->ant_type || tbl->ant_type > ANT_ABC)
635 return 0;
636
637 if (!rs_is_valid_ant(valid_ant, tbl->ant_type))
638 return 0;
639
640 new_ant_type = ant_toggle_lookup[tbl->ant_type];
641
642 while ((new_ant_type != tbl->ant_type) &&
643 !rs_is_valid_ant(valid_ant, new_ant_type))
644 new_ant_type = ant_toggle_lookup[new_ant_type];
645
646 if (new_ant_type == tbl->ant_type)
647 return 0;
648
649 tbl->ant_type = new_ant_type;
650 *rate_n_flags &= ~RATE_MCS_ANT_ABC_MSK;
651 *rate_n_flags |= new_ant_type << RATE_MCS_ANT_POS;
652 return 1;
Zhu Yib481de92007-09-25 17:54:57 -0700653}
654
Wey-Yi Guy2681b202009-05-21 13:44:22 -0700655/* in 4965 we don't use greenfield at all */
656static inline u8 rs_use_green(struct iwl_priv *priv,
657 struct ieee80211_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -0700658{
Wey-Yi Guy2681b202009-05-21 13:44:22 -0700659 u8 is_green;
660
661 if ((priv->hw_rev & CSR_HW_REV_TYPE_MSK) == CSR_HW_REV_TYPE_4965)
662 is_green = 0;
663 else
664 is_green = (conf_is_ht(conf) &&
665 priv->current_ht_config.is_green_field &&
666 !priv->current_ht_config.non_GF_STA_present);
667 return is_green;
Guy Cohenf935a6d2008-04-23 17:15:02 -0700668}
Zhu Yib481de92007-09-25 17:54:57 -0700669
670/**
671 * rs_get_supported_rates - get the available rates
672 *
673 * if management frame or broadcast frame only return
674 * basic available rates.
675 *
676 */
Tomas Winklere227cea2008-07-18 13:53:05 +0800677static u16 rs_get_supported_rates(struct iwl_lq_sta *lq_sta,
678 struct ieee80211_hdr *hdr,
679 enum iwl_table_type rate_type)
Zhu Yib481de92007-09-25 17:54:57 -0700680{
Zhu Yib481de92007-09-25 17:54:57 -0700681 if (hdr && is_multicast_ether_addr(hdr->addr1) &&
Guy Coheneecd6e52008-04-23 17:14:58 -0700682 lq_sta->active_rate_basic)
683 return lq_sta->active_rate_basic;
684
685 if (is_legacy(rate_type)) {
686 return lq_sta->active_legacy_rate;
687 } else {
688 if (is_siso(rate_type))
689 return lq_sta->active_siso_rate;
690 else if (is_mimo2(rate_type))
691 return lq_sta->active_mimo2_rate;
692 else
693 return lq_sta->active_mimo3_rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -0800694 }
Zhu Yib481de92007-09-25 17:54:57 -0700695}
696
Ester Kummerbf403db2008-05-05 10:22:40 +0800697static u16 rs_get_adjacent_rate(struct iwl_priv *priv, u8 index, u16 rate_mask,
698 int rate_type)
Zhu Yib481de92007-09-25 17:54:57 -0700699{
700 u8 high = IWL_RATE_INVALID;
701 u8 low = IWL_RATE_INVALID;
702
Ian Schram01ebd062007-10-25 17:15:22 +0800703 /* 802.11A or ht walks to the next literal adjacent rate in
Zhu Yib481de92007-09-25 17:54:57 -0700704 * the rate table */
705 if (is_a_band(rate_type) || !is_legacy(rate_type)) {
706 int i;
707 u32 mask;
708
709 /* Find the previous rate that is in the rate mask */
710 i = index - 1;
711 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
712 if (rate_mask & mask) {
713 low = i;
714 break;
715 }
716 }
717
718 /* Find the next rate that is in the rate mask */
719 i = index + 1;
720 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
721 if (rate_mask & mask) {
722 high = i;
723 break;
724 }
725 }
726
727 return (high << 8) | low;
728 }
729
730 low = index;
731 while (low != IWL_RATE_INVALID) {
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800732 low = iwl_rates[low].prev_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700733 if (low == IWL_RATE_INVALID)
734 break;
735 if (rate_mask & (1 << low))
736 break;
Tomas Winklere1623442009-01-27 14:27:56 -0800737 IWL_DEBUG_RATE(priv, "Skipping masked lower rate: %d\n", low);
Zhu Yib481de92007-09-25 17:54:57 -0700738 }
739
740 high = index;
741 while (high != IWL_RATE_INVALID) {
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800742 high = iwl_rates[high].next_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700743 if (high == IWL_RATE_INVALID)
744 break;
745 if (rate_mask & (1 << high))
746 break;
Tomas Winklere1623442009-01-27 14:27:56 -0800747 IWL_DEBUG_RATE(priv, "Skipping masked higher rate: %d\n", high);
Zhu Yib481de92007-09-25 17:54:57 -0700748 }
749
750 return (high << 8) | low;
751}
752
Tomas Winklere227cea2008-07-18 13:53:05 +0800753static u32 rs_get_lower_rate(struct iwl_lq_sta *lq_sta,
754 struct iwl_scale_tbl_info *tbl,
755 u8 scale_index, u8 ht_possible)
Zhu Yib481de92007-09-25 17:54:57 -0700756{
Zhu Yib481de92007-09-25 17:54:57 -0700757 s32 low;
758 u16 rate_mask;
759 u16 high_low;
760 u8 switch_to_legacy = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -0800761 u8 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -0700762
763 /* check if we need to switch from HT to legacy rates.
764 * assumption is that mandatory rates (1Mbps or 6Mbps)
765 * are always supported (spec demand) */
766 if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_index)) {
767 switch_to_legacy = 1;
768 scale_index = rs_ht_to_legacy[scale_index];
Johannes Berg8318d782008-01-24 19:38:38 +0100769 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700770 tbl->lq_type = LQ_A;
771 else
772 tbl->lq_type = LQ_G;
773
Guy Cohen69fdb302008-04-23 17:15:01 -0700774 if (num_of_ant(tbl->ant_type) > 1)
Guy Cohenfde0db32008-04-21 15:42:01 -0700775 tbl->ant_type = ANT_A;/*FIXME:RS*/
Zhu Yib481de92007-09-25 17:54:57 -0700776
777 tbl->is_fat = 0;
778 tbl->is_SGI = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -0700779 tbl->max_search = IWL_MAX_SEARCH;
Zhu Yib481de92007-09-25 17:54:57 -0700780 }
781
Guy Coheneecd6e52008-04-23 17:14:58 -0700782 rate_mask = rs_get_supported_rates(lq_sta, NULL, tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -0700783
Ben Cahill77626352007-11-29 11:09:44 +0800784 /* Mask with station rate restriction */
Zhu Yib481de92007-09-25 17:54:57 -0700785 if (is_legacy(tbl->lq_type)) {
Ben Cahill77626352007-11-29 11:09:44 +0800786 /* supp_rates has no CCK bits in A mode */
Johannes Berg8318d782008-01-24 19:38:38 +0100787 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700788 rate_mask = (u16)(rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -0800789 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
Zhu Yib481de92007-09-25 17:54:57 -0700790 else
Tomas Winklerc33104f2008-01-14 17:46:21 -0800791 rate_mask = (u16)(rate_mask & lq_sta->supp_rates);
Zhu Yib481de92007-09-25 17:54:57 -0700792 }
793
Ben Cahill77626352007-11-29 11:09:44 +0800794 /* If we switched from HT to legacy, check current rate */
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800795 if (switch_to_legacy && (rate_mask & (1 << scale_index))) {
Guy Cohen39e88502008-04-23 17:14:57 -0700796 low = scale_index;
797 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700798 }
799
Ester Kummerbf403db2008-05-05 10:22:40 +0800800 high_low = rs_get_adjacent_rate(lq_sta->drv, scale_index, rate_mask,
801 tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -0700802 low = high_low & 0xff;
803
Guy Cohen39e88502008-04-23 17:14:57 -0700804 if (low == IWL_RATE_INVALID)
805 low = scale_index;
806
807out:
Tomas Winkler978785a2008-12-19 10:37:31 +0800808 return rate_n_flags_from_tbl(lq_sta->drv, tbl, low, is_green);
Zhu Yib481de92007-09-25 17:54:57 -0700809}
810
Ben Cahill77626352007-11-29 11:09:44 +0800811/*
812 * mac80211 sends us Tx status
813 */
Johannes Berg4b7679a2008-09-18 18:14:18 +0200814static void rs_tx_status(void *priv_r, struct ieee80211_supported_band *sband,
815 struct ieee80211_sta *sta, void *priv_sta,
Johannes Berge039fa42008-05-15 12:55:29 +0200816 struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -0700817{
818 int status;
819 u8 retries;
820 int rs_index, index = 0;
Tomas Winkler417f1142008-11-12 13:14:06 -0800821 struct iwl_lq_sta *lq_sta = priv_sta;
Tomas Winkler66c73db2008-04-15 16:01:40 -0700822 struct iwl_link_quality_cmd *table;
Zhu Yib481de92007-09-25 17:54:57 -0700823 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berg4b7679a2008-09-18 18:14:18 +0200824 struct iwl_priv *priv = (struct iwl_priv *)priv_r;
825 struct ieee80211_hw *hw = priv->hw;
Johannes Berge039fa42008-05-15 12:55:29 +0200826 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Tomas Winklere227cea2008-07-18 13:53:05 +0800827 struct iwl_rate_scale_data *window = NULL;
828 struct iwl_rate_scale_data *search_win = NULL;
Guy Cohen39e88502008-04-23 17:14:57 -0700829 u32 tx_rate;
Tomas Winklere227cea2008-07-18 13:53:05 +0800830 struct iwl_scale_tbl_info tbl_type;
831 struct iwl_scale_tbl_info *curr_tbl, *search_tbl;
Zhu Yib481de92007-09-25 17:54:57 -0700832 u8 active_index = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700833 s32 tpt = 0;
834
Tomas Winklere1623442009-01-27 14:27:56 -0800835 IWL_DEBUG_RATE_LIMIT(priv, "get frame ack response, update rate scale window\n");
Zhu Yib481de92007-09-25 17:54:57 -0700836
Tomas Winkler417f1142008-11-12 13:14:06 -0800837 if (!ieee80211_is_data(hdr->frame_control) ||
Gábor Stefanik514d65c2009-04-23 19:36:08 +0200838 info->flags & IEEE80211_TX_CTL_NO_ACK)
Zhu Yib481de92007-09-25 17:54:57 -0700839 return;
840
Ron Rindjunsky99556432008-01-28 14:07:25 +0200841 /* This packet was aggregated but doesn't carry rate scale info */
Johannes Berge039fa42008-05-15 12:55:29 +0200842 if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
843 !(info->flags & IEEE80211_TX_STAT_AMPDU))
Ron Rindjunsky99556432008-01-28 14:07:25 +0200844 return;
845
Wey-Yi Guy8fe72312009-03-10 14:35:10 -0700846 if (info->flags & IEEE80211_TX_STAT_AMPDU)
847 retries = 0;
848 else
849 retries = info->status.rates[0].count - 1;
Zhu Yib481de92007-09-25 17:54:57 -0700850
851 if (retries > 15)
852 retries = 15;
853
Johannes Berg05c914f2008-09-11 00:01:58 +0200854 if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) &&
Tomas Winklerc33104f2008-01-14 17:46:21 -0800855 !lq_sta->ibss_sta_added)
Tomas Winklerf4d60822008-03-05 11:31:00 -0800856 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700857
Tomas Winklerc33104f2008-01-14 17:46:21 -0800858 table = &lq_sta->lq;
859 active_index = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -0700860
Tomas Winklerc33104f2008-01-14 17:46:21 -0800861 curr_tbl = &(lq_sta->lq_info[active_index]);
862 search_tbl = &(lq_sta->lq_info[(1 - active_index)]);
Tomas Winklere227cea2008-07-18 13:53:05 +0800863 window = (struct iwl_rate_scale_data *)&(curr_tbl->win[0]);
864 search_win = (struct iwl_rate_scale_data *)&(search_tbl->win[0]);
Zhu Yib481de92007-09-25 17:54:57 -0700865
Ben Cahill77626352007-11-29 11:09:44 +0800866 /*
867 * Ignore this Tx frame response if its initial rate doesn't match
868 * that of latest Link Quality command. There may be stragglers
869 * from a previous Link Quality command, but we're no longer interested
870 * in those; they're either from the "active" mode while we're trying
871 * to check "search" mode, or a prior "search" mode after we've moved
872 * to a new "search" mode (which might become the new "active" mode).
873 */
Guy Cohen39e88502008-04-23 17:14:57 -0700874 tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
875 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800876 if (priv->band == IEEE80211_BAND_5GHZ)
877 rs_index -= IWL_FIRST_OFDM_RATE;
878
Johannes Berge6a98542008-10-21 12:40:02 +0200879 if ((info->status.rates[0].idx < 0) ||
880 (tbl_type.is_SGI != !!(info->status.rates[0].flags & IEEE80211_TX_RC_SHORT_GI)) ||
881 (tbl_type.is_fat != !!(info->status.rates[0].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)) ||
882 (tbl_type.is_dup != !!(info->status.rates[0].flags & IEEE80211_TX_RC_DUP_DATA)) ||
883 (tbl_type.ant_type != info->antenna_sel_tx) ||
884 (!!(tx_rate & RATE_MCS_HT_MSK) != !!(info->status.rates[0].flags & IEEE80211_TX_RC_MCS)) ||
885 (!!(tx_rate & RATE_MCS_GF_MSK) != !!(info->status.rates[0].flags & IEEE80211_TX_RC_GREEN_FIELD)) ||
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800886 (hw->wiphy->bands[priv->band]->bitrates[rs_index].bitrate !=
Johannes Berge6a98542008-10-21 12:40:02 +0200887 hw->wiphy->bands[info->band]->bitrates[info->status.rates[0].idx].bitrate)) {
Tomas Winklere1623442009-01-27 14:27:56 -0800888 IWL_DEBUG_RATE(priv, "initial rate does not match 0x%x\n", tx_rate);
Mohamed Abbasd5e49032008-12-12 08:22:15 -0800889 /* the last LQ command could failed so the LQ in ucode not
890 * the same in driver sync up
891 */
Abbas, Mohamed7d049e52009-01-20 21:33:53 -0800892 lq_sta->missed_rate_counter++;
893 if (lq_sta->missed_rate_counter > IWL_MISSED_RATE_MAX) {
894 lq_sta->missed_rate_counter = 0;
895 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
896 }
Tomas Winklerf4d60822008-03-05 11:31:00 -0800897 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700898 }
899
Abbas, Mohamed7d049e52009-01-20 21:33:53 -0800900 lq_sta->missed_rate_counter = 0;
Ben Cahill77626352007-11-29 11:09:44 +0800901 /* Update frame history window with "failure" for each Tx retry. */
Zhu Yib481de92007-09-25 17:54:57 -0700902 while (retries) {
Ben Cahill77626352007-11-29 11:09:44 +0800903 /* Look up the rate and other info used for each tx attempt.
904 * Each tx attempt steps one entry deeper in the rate table. */
Guy Cohen39e88502008-04-23 17:14:57 -0700905 tx_rate = le32_to_cpu(table->rs_table[index].rate_n_flags);
906 rs_get_tbl_info_from_mcs(tx_rate, priv->band,
Zhu Yib481de92007-09-25 17:54:57 -0700907 &tbl_type, &rs_index);
908
Ben Cahill77626352007-11-29 11:09:44 +0800909 /* If type matches "search" table,
910 * add failure to "search" history */
Zhu Yib481de92007-09-25 17:54:57 -0700911 if ((tbl_type.lq_type == search_tbl->lq_type) &&
Guy Cohenfde0db32008-04-21 15:42:01 -0700912 (tbl_type.ant_type == search_tbl->ant_type) &&
Zhu Yib481de92007-09-25 17:54:57 -0700913 (tbl_type.is_SGI == search_tbl->is_SGI)) {
914 if (search_tbl->expected_tpt)
915 tpt = search_tbl->expected_tpt[rs_index];
916 else
917 tpt = 0;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200918 rs_collect_tx_data(search_win, rs_index, tpt, 1, 0);
Ben Cahill77626352007-11-29 11:09:44 +0800919
920 /* Else if type matches "current/active" table,
921 * add failure to "current/active" history */
Zhu Yib481de92007-09-25 17:54:57 -0700922 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
Guy Cohenfde0db32008-04-21 15:42:01 -0700923 (tbl_type.ant_type == curr_tbl->ant_type) &&
Zhu Yib481de92007-09-25 17:54:57 -0700924 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
925 if (curr_tbl->expected_tpt)
926 tpt = curr_tbl->expected_tpt[rs_index];
927 else
928 tpt = 0;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200929 rs_collect_tx_data(window, rs_index, tpt, 1, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700930 }
Ben Cahill77626352007-11-29 11:09:44 +0800931
932 /* If not searching for a new mode, increment failed counter
933 * ... this helps determine when to start searching again */
Tomas Winklerc33104f2008-01-14 17:46:21 -0800934 if (lq_sta->stay_in_tbl)
935 lq_sta->total_failed++;
Zhu Yib481de92007-09-25 17:54:57 -0700936 --retries;
937 index++;
938
939 }
940
Ben Cahill77626352007-11-29 11:09:44 +0800941 /*
942 * Find (by rate) the history window to update with final Tx attempt;
943 * if Tx was successful first try, use original rate,
944 * else look up the rate that was, finally, successful.
945 */
Guy Cohen39e88502008-04-23 17:14:57 -0700946 tx_rate = le32_to_cpu(table->rs_table[index].rate_n_flags);
Wey-Yi Guy12b96812009-04-08 11:39:27 -0700947 lq_sta->last_rate_n_flags = tx_rate;
Guy Cohen39e88502008-04-23 17:14:57 -0700948 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
Zhu Yib481de92007-09-25 17:54:57 -0700949
Ben Cahill77626352007-11-29 11:09:44 +0800950 /* Update frame history window with "success" if Tx got ACKed ... */
Johannes Berge039fa42008-05-15 12:55:29 +0200951 status = !!(info->flags & IEEE80211_TX_STAT_ACK);
Zhu Yib481de92007-09-25 17:54:57 -0700952
Ben Cahill77626352007-11-29 11:09:44 +0800953 /* If type matches "search" table,
954 * add final tx status to "search" history */
Zhu Yib481de92007-09-25 17:54:57 -0700955 if ((tbl_type.lq_type == search_tbl->lq_type) &&
Guy Cohenfde0db32008-04-21 15:42:01 -0700956 (tbl_type.ant_type == search_tbl->ant_type) &&
Zhu Yib481de92007-09-25 17:54:57 -0700957 (tbl_type.is_SGI == search_tbl->is_SGI)) {
958 if (search_tbl->expected_tpt)
959 tpt = search_tbl->expected_tpt[rs_index];
960 else
961 tpt = 0;
Wey-Yi Guydf36c042009-03-10 14:35:11 -0700962 if (info->flags & IEEE80211_TX_STAT_AMPDU)
Ron Rindjunsky99556432008-01-28 14:07:25 +0200963 rs_collect_tx_data(search_win, rs_index, tpt,
Johannes Berge039fa42008-05-15 12:55:29 +0200964 info->status.ampdu_ack_len,
965 info->status.ampdu_ack_map);
Ron Rindjunsky99556432008-01-28 14:07:25 +0200966 else
967 rs_collect_tx_data(search_win, rs_index, tpt,
968 1, status);
Ben Cahill77626352007-11-29 11:09:44 +0800969 /* Else if type matches "current/active" table,
970 * add final tx status to "current/active" history */
Zhu Yib481de92007-09-25 17:54:57 -0700971 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
Guy Cohenfde0db32008-04-21 15:42:01 -0700972 (tbl_type.ant_type == curr_tbl->ant_type) &&
Zhu Yib481de92007-09-25 17:54:57 -0700973 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
974 if (curr_tbl->expected_tpt)
975 tpt = curr_tbl->expected_tpt[rs_index];
976 else
977 tpt = 0;
Wey-Yi Guydf36c042009-03-10 14:35:11 -0700978 if (info->flags & IEEE80211_TX_STAT_AMPDU)
Ron Rindjunsky99556432008-01-28 14:07:25 +0200979 rs_collect_tx_data(window, rs_index, tpt,
Johannes Berge039fa42008-05-15 12:55:29 +0200980 info->status.ampdu_ack_len,
981 info->status.ampdu_ack_map);
Ron Rindjunsky99556432008-01-28 14:07:25 +0200982 else
983 rs_collect_tx_data(window, rs_index, tpt,
984 1, status);
Zhu Yib481de92007-09-25 17:54:57 -0700985 }
986
Ben Cahill77626352007-11-29 11:09:44 +0800987 /* If not searching for new mode, increment success/failed counter
988 * ... these help determine when to start searching again */
Tomas Winklerc33104f2008-01-14 17:46:21 -0800989 if (lq_sta->stay_in_tbl) {
Wey-Yi Guydf36c042009-03-10 14:35:11 -0700990 if (info->flags & IEEE80211_TX_STAT_AMPDU) {
Johannes Berge039fa42008-05-15 12:55:29 +0200991 lq_sta->total_success += info->status.ampdu_ack_map;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200992 lq_sta->total_failed +=
Johannes Berge039fa42008-05-15 12:55:29 +0200993 (info->status.ampdu_ack_len - info->status.ampdu_ack_map);
Ron Rindjunsky99556432008-01-28 14:07:25 +0200994 } else {
995 if (status)
996 lq_sta->total_success++;
997 else
998 lq_sta->total_failed++;
999 }
Zhu Yib481de92007-09-25 17:54:57 -07001000 }
1001
Ben Cahill77626352007-11-29 11:09:44 +08001002 /* See if there's a better rate or modulation mode to try. */
Abbas, Mohamedc338ba32009-01-21 10:58:02 -08001003 if (sta && sta->supp_rates[sband->band])
Gábor Stefanik514d65c2009-04-23 19:36:08 +02001004 rs_rate_scale_perform(priv, skb, sta, lq_sta);
Tomas Winklerf4d60822008-03-05 11:31:00 -08001005out:
Zhu Yib481de92007-09-25 17:54:57 -07001006 return;
1007}
1008
Ben Cahill77626352007-11-29 11:09:44 +08001009/*
1010 * Begin a period of staying with a selected modulation mode.
1011 * Set "stay_in_tbl" flag to prevent any mode switches.
1012 * Set frame tx success limits according to legacy vs. high-throughput,
1013 * and reset overall (spanning all rates) tx success history statistics.
1014 * These control how long we stay using same modulation mode before
1015 * searching for a new mode.
1016 */
Ester Kummerbf403db2008-05-05 10:22:40 +08001017static void rs_set_stay_in_table(struct iwl_priv *priv, u8 is_legacy,
Tomas Winklere227cea2008-07-18 13:53:05 +08001018 struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07001019{
Tomas Winklere1623442009-01-27 14:27:56 -08001020 IWL_DEBUG_RATE(priv, "we are staying in the same table\n");
Tomas Winklerc33104f2008-01-14 17:46:21 -08001021 lq_sta->stay_in_tbl = 1; /* only place this gets set */
Zhu Yib481de92007-09-25 17:54:57 -07001022 if (is_legacy) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001023 lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT;
1024 lq_sta->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
1025 lq_sta->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
Zhu Yib481de92007-09-25 17:54:57 -07001026 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001027 lq_sta->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
1028 lq_sta->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
1029 lq_sta->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
Zhu Yib481de92007-09-25 17:54:57 -07001030 }
Tomas Winklerc33104f2008-01-14 17:46:21 -08001031 lq_sta->table_count = 0;
1032 lq_sta->total_failed = 0;
1033 lq_sta->total_success = 0;
Mohamed Abbas447fee72009-04-20 14:37:02 -07001034 lq_sta->flush_timer = jiffies;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001035 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001036}
1037
Ben Cahill77626352007-11-29 11:09:44 +08001038/*
1039 * Find correct throughput table for given mode of modulation
1040 */
Tomas Winklere227cea2008-07-18 13:53:05 +08001041static void rs_set_expected_tpt_table(struct iwl_lq_sta *lq_sta,
1042 struct iwl_scale_tbl_info *tbl)
Zhu Yib481de92007-09-25 17:54:57 -07001043{
1044 if (is_legacy(tbl->lq_type)) {
1045 if (!is_a_band(tbl->lq_type))
1046 tbl->expected_tpt = expected_tpt_G;
1047 else
1048 tbl->expected_tpt = expected_tpt_A;
1049 } else if (is_siso(tbl->lq_type)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001050 if (tbl->is_fat && !lq_sta->is_dup)
Zhu Yib481de92007-09-25 17:54:57 -07001051 if (tbl->is_SGI)
1052 tbl->expected_tpt = expected_tpt_siso40MHzSGI;
1053 else
1054 tbl->expected_tpt = expected_tpt_siso40MHz;
1055 else if (tbl->is_SGI)
1056 tbl->expected_tpt = expected_tpt_siso20MHzSGI;
1057 else
1058 tbl->expected_tpt = expected_tpt_siso20MHz;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001059 } else if (is_mimo2(tbl->lq_type)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001060 if (tbl->is_fat && !lq_sta->is_dup)
Zhu Yib481de92007-09-25 17:54:57 -07001061 if (tbl->is_SGI)
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001062 tbl->expected_tpt = expected_tpt_mimo2_40MHzSGI;
Zhu Yib481de92007-09-25 17:54:57 -07001063 else
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001064 tbl->expected_tpt = expected_tpt_mimo2_40MHz;
Zhu Yib481de92007-09-25 17:54:57 -07001065 else if (tbl->is_SGI)
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001066 tbl->expected_tpt = expected_tpt_mimo2_20MHzSGI;
Zhu Yib481de92007-09-25 17:54:57 -07001067 else
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001068 tbl->expected_tpt = expected_tpt_mimo2_20MHz;
1069 } else if (is_mimo3(tbl->lq_type)) {
1070 if (tbl->is_fat && !lq_sta->is_dup)
1071 if (tbl->is_SGI)
1072 tbl->expected_tpt = expected_tpt_mimo3_40MHzSGI;
1073 else
1074 tbl->expected_tpt = expected_tpt_mimo3_40MHz;
1075 else if (tbl->is_SGI)
1076 tbl->expected_tpt = expected_tpt_mimo3_20MHzSGI;
1077 else
1078 tbl->expected_tpt = expected_tpt_mimo3_20MHz;
Zhu Yib481de92007-09-25 17:54:57 -07001079 } else
1080 tbl->expected_tpt = expected_tpt_G;
1081}
1082
Ben Cahill77626352007-11-29 11:09:44 +08001083/*
1084 * Find starting rate for new "search" high-throughput mode of modulation.
1085 * Goal is to find lowest expected rate (under perfect conditions) that is
1086 * above the current measured throughput of "active" mode, to give new mode
1087 * a fair chance to prove itself without too many challenges.
1088 *
1089 * This gets called when transitioning to more aggressive modulation
1090 * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
1091 * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
1092 * to decrease to match "active" throughput. When moving from MIMO to SISO,
1093 * bit rate will typically need to increase, but not if performance was bad.
1094 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001095static s32 rs_get_best_rate(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001096 struct iwl_lq_sta *lq_sta,
1097 struct iwl_scale_tbl_info *tbl, /* "search" */
Guy Cohenf935a6d2008-04-23 17:15:02 -07001098 u16 rate_mask, s8 index)
Zhu Yib481de92007-09-25 17:54:57 -07001099{
Ben Cahill77626352007-11-29 11:09:44 +08001100 /* "active" values */
Tomas Winklere227cea2008-07-18 13:53:05 +08001101 struct iwl_scale_tbl_info *active_tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001102 &(lq_sta->lq_info[lq_sta->active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07001103 s32 active_sr = active_tbl->win[index].success_ratio;
Zhu Yib481de92007-09-25 17:54:57 -07001104 s32 active_tpt = active_tbl->expected_tpt[index];
Ben Cahill77626352007-11-29 11:09:44 +08001105
1106 /* expected "search" throughput */
1107 s32 *tpt_tbl = tbl->expected_tpt;
1108
1109 s32 new_rate, high, low, start_hi;
Zhu Yib481de92007-09-25 17:54:57 -07001110 u16 high_low;
Guy Cohenf935a6d2008-04-23 17:15:02 -07001111 s8 rate = index;
Zhu Yib481de92007-09-25 17:54:57 -07001112
1113 new_rate = high = low = start_hi = IWL_RATE_INVALID;
1114
1115 for (; ;) {
Ester Kummerbf403db2008-05-05 10:22:40 +08001116 high_low = rs_get_adjacent_rate(priv, rate, rate_mask,
1117 tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -07001118
1119 low = high_low & 0xff;
1120 high = (high_low >> 8) & 0xff;
1121
Ben Cahill77626352007-11-29 11:09:44 +08001122 /*
1123 * Lower the "search" bit rate, to give new "search" mode
1124 * approximately the same throughput as "active" if:
1125 *
1126 * 1) "Active" mode has been working modestly well (but not
1127 * great), and expected "search" throughput (under perfect
1128 * conditions) at candidate rate is above the actual
1129 * measured "active" throughput (but less than expected
1130 * "active" throughput under perfect conditions).
1131 * OR
1132 * 2) "Active" mode has been working perfectly or very well
1133 * and expected "search" throughput (under perfect
1134 * conditions) at candidate rate is above expected
1135 * "active" throughput (under perfect conditions).
1136 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001137 if ((((100 * tpt_tbl[rate]) > lq_sta->last_tpt) &&
Zhu Yib481de92007-09-25 17:54:57 -07001138 ((active_sr > IWL_RATE_DECREASE_TH) &&
1139 (active_sr <= IWL_RATE_HIGH_TH) &&
1140 (tpt_tbl[rate] <= active_tpt))) ||
1141 ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
1142 (tpt_tbl[rate] > active_tpt))) {
1143
Ben Cahill77626352007-11-29 11:09:44 +08001144 /* (2nd or later pass)
1145 * If we've already tried to raise the rate, and are
1146 * now trying to lower it, use the higher rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001147 if (start_hi != IWL_RATE_INVALID) {
1148 new_rate = start_hi;
1149 break;
1150 }
Ben Cahill77626352007-11-29 11:09:44 +08001151
Zhu Yib481de92007-09-25 17:54:57 -07001152 new_rate = rate;
Ben Cahill77626352007-11-29 11:09:44 +08001153
1154 /* Loop again with lower rate */
Zhu Yib481de92007-09-25 17:54:57 -07001155 if (low != IWL_RATE_INVALID)
1156 rate = low;
Ben Cahill77626352007-11-29 11:09:44 +08001157
1158 /* Lower rate not available, use the original */
Zhu Yib481de92007-09-25 17:54:57 -07001159 else
1160 break;
Ben Cahill77626352007-11-29 11:09:44 +08001161
1162 /* Else try to raise the "search" rate to match "active" */
Zhu Yib481de92007-09-25 17:54:57 -07001163 } else {
Ben Cahill77626352007-11-29 11:09:44 +08001164 /* (2nd or later pass)
1165 * If we've already tried to lower the rate, and are
1166 * now trying to raise it, use the lower rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001167 if (new_rate != IWL_RATE_INVALID)
1168 break;
Ben Cahill77626352007-11-29 11:09:44 +08001169
1170 /* Loop again with higher rate */
Zhu Yib481de92007-09-25 17:54:57 -07001171 else if (high != IWL_RATE_INVALID) {
1172 start_hi = high;
1173 rate = high;
Ben Cahill77626352007-11-29 11:09:44 +08001174
1175 /* Higher rate not available, use the original */
Zhu Yib481de92007-09-25 17:54:57 -07001176 } else {
Guy Cohen90d77952008-09-09 10:54:53 +08001177 new_rate = rate;
Zhu Yib481de92007-09-25 17:54:57 -07001178 break;
1179 }
1180 }
1181 }
1182
1183 return new_rate;
1184}
Zhu Yib481de92007-09-25 17:54:57 -07001185
Ben Cahill77626352007-11-29 11:09:44 +08001186/*
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001187 * Set up search table for MIMO2
Ben Cahill77626352007-11-29 11:09:44 +08001188 */
Guy Cohenfde0db32008-04-21 15:42:01 -07001189static int rs_switch_to_mimo2(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001190 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001191 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001192 struct ieee80211_sta *sta,
Tomas Winklere227cea2008-07-18 13:53:05 +08001193 struct iwl_scale_tbl_info *tbl, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001194{
Zhu Yib481de92007-09-25 17:54:57 -07001195 u16 rate_mask;
1196 s32 rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001197 s8 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07001198
Luis R. Rodriguezde27e642008-12-23 15:58:44 -08001199 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
Zhu Yib481de92007-09-25 17:54:57 -07001200 return -1;
1201
Johannes Bergd9fe60d2008-10-09 12:13:49 +02001202 if (((sta->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 2)
Tomas Winkler00c5ae22008-09-03 11:26:42 +08001203 == WLAN_HT_CAP_SM_PS_STATIC)
Zhu Yib481de92007-09-25 17:54:57 -07001204 return -1;
1205
Ben Cahill77626352007-11-29 11:09:44 +08001206 /* Need both Tx chains/antennas to support MIMO */
Guy Cohenaade00c2008-04-23 17:14:59 -07001207 if (priv->hw_params.tx_chains_num < 2)
Zhu Yib481de92007-09-25 17:54:57 -07001208 return -1;
1209
Tomas Winklere1623442009-01-27 14:27:56 -08001210 IWL_DEBUG_RATE(priv, "LQ: try to switch to MIMO2\n");
Guy Cohen39e88502008-04-23 17:14:57 -07001211
1212 tbl->lq_type = LQ_MIMO2;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001213 tbl->is_dup = lq_sta->is_dup;
Zhu Yib481de92007-09-25 17:54:57 -07001214 tbl->action = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001215 tbl->max_search = IWL_MAX_SEARCH;
Guy Cohen39e88502008-04-23 17:14:57 -07001216 rate_mask = lq_sta->active_mimo2_rate;
1217
Wey-Yi Guya30199f2009-04-30 13:56:28 -07001218 if (iwl_is_fat_tx_allowed(priv, &sta->ht_cap))
Zhu Yib481de92007-09-25 17:54:57 -07001219 tbl->is_fat = 1;
1220 else
1221 tbl->is_fat = 0;
1222
Guy Cohen39e88502008-04-23 17:14:57 -07001223 /* FIXME: - don't toggle SGI here
Zhu Yib481de92007-09-25 17:54:57 -07001224 if (tbl->is_fat) {
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001225 if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
Zhu Yib481de92007-09-25 17:54:57 -07001226 tbl->is_SGI = 1;
1227 else
1228 tbl->is_SGI = 0;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001229 } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
Zhu Yib481de92007-09-25 17:54:57 -07001230 tbl->is_SGI = 1;
1231 else
1232 tbl->is_SGI = 0;
Guy Cohen39e88502008-04-23 17:14:57 -07001233 */
Zhu Yib481de92007-09-25 17:54:57 -07001234
Guy Coheneecd6e52008-04-23 17:14:58 -07001235 rs_set_expected_tpt_table(lq_sta, tbl);
Zhu Yib481de92007-09-25 17:54:57 -07001236
Guy Cohenf935a6d2008-04-23 17:15:02 -07001237 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
Zhu Yib481de92007-09-25 17:54:57 -07001238
Tomas Winklere1623442009-01-27 14:27:56 -08001239 IWL_DEBUG_RATE(priv, "LQ: MIMO2 best rate %d mask %X\n", rate, rate_mask);
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001240 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1241 IWL_DEBUG_RATE(priv, "Can't switch with index %d rate mask %x\n",
1242 rate, rate_mask);
1243 return -1;
1244 }
1245 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
Guy Cohen39e88502008-04-23 17:14:57 -07001246
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001247 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
1248 tbl->current_rate, is_green);
1249 return 0;
1250}
1251
1252/*
1253 * Set up search table for MIMO3
1254 */
1255static int rs_switch_to_mimo3(struct iwl_priv *priv,
1256 struct iwl_lq_sta *lq_sta,
1257 struct ieee80211_conf *conf,
1258 struct ieee80211_sta *sta,
1259 struct iwl_scale_tbl_info *tbl, int index)
1260{
1261 u16 rate_mask;
1262 s32 rate;
1263 s8 is_green = lq_sta->is_green;
1264
1265 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
1266 return -1;
1267
1268 if (((sta->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 2)
1269 == WLAN_HT_CAP_SM_PS_STATIC)
1270 return -1;
1271
1272 /* Need both Tx chains/antennas to support MIMO */
1273 if (priv->hw_params.tx_chains_num < 3)
1274 return -1;
1275
1276 IWL_DEBUG_RATE(priv, "LQ: try to switch to MIMO3\n");
1277
1278 tbl->lq_type = LQ_MIMO3;
1279 tbl->is_dup = lq_sta->is_dup;
1280 tbl->action = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001281 tbl->max_search = IWL_MAX_11N_MIMO3_SEARCH;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001282 rate_mask = lq_sta->active_mimo3_rate;
1283
Wey-Yi Guya30199f2009-04-30 13:56:28 -07001284 if (iwl_is_fat_tx_allowed(priv, &sta->ht_cap))
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001285 tbl->is_fat = 1;
1286 else
1287 tbl->is_fat = 0;
1288
1289 /* FIXME: - don't toggle SGI here
1290 if (tbl->is_fat) {
1291 if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
1292 tbl->is_SGI = 1;
1293 else
1294 tbl->is_SGI = 0;
1295 } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
1296 tbl->is_SGI = 1;
1297 else
1298 tbl->is_SGI = 0;
1299 */
1300
1301 rs_set_expected_tpt_table(lq_sta, tbl);
1302
1303 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
1304
1305 IWL_DEBUG_RATE(priv, "LQ: MIMO3 best rate %d mask %X\n",
1306 rate, rate_mask);
Guy Cohen39e88502008-04-23 17:14:57 -07001307 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
Tomas Winklere1623442009-01-27 14:27:56 -08001308 IWL_DEBUG_RATE(priv, "Can't switch with index %d rate mask %x\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001309 rate, rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07001310 return -1;
Guy Cohen39e88502008-04-23 17:14:57 -07001311 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001312 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
Zhu Yib481de92007-09-25 17:54:57 -07001313
Tomas Winklere1623442009-01-27 14:27:56 -08001314 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001315 tbl->current_rate, is_green);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001316 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07001317}
1318
Ben Cahill77626352007-11-29 11:09:44 +08001319/*
1320 * Set up search table for SISO
1321 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001322static int rs_switch_to_siso(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001323 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001324 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001325 struct ieee80211_sta *sta,
Tomas Winklere227cea2008-07-18 13:53:05 +08001326 struct iwl_scale_tbl_info *tbl, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001327{
Zhu Yib481de92007-09-25 17:54:57 -07001328 u16 rate_mask;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001329 u8 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07001330 s32 rate;
1331
Luis R. Rodriguezde27e642008-12-23 15:58:44 -08001332 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
Zhu Yib481de92007-09-25 17:54:57 -07001333 return -1;
1334
Tomas Winklere1623442009-01-27 14:27:56 -08001335 IWL_DEBUG_RATE(priv, "LQ: try to switch to SISO\n");
Guy Cohen39e88502008-04-23 17:14:57 -07001336
Tomas Winklerc33104f2008-01-14 17:46:21 -08001337 tbl->is_dup = lq_sta->is_dup;
Zhu Yib481de92007-09-25 17:54:57 -07001338 tbl->lq_type = LQ_SISO;
1339 tbl->action = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001340 tbl->max_search = IWL_MAX_SEARCH;
Guy Cohen39e88502008-04-23 17:14:57 -07001341 rate_mask = lq_sta->active_siso_rate;
Zhu Yib481de92007-09-25 17:54:57 -07001342
Wey-Yi Guya30199f2009-04-30 13:56:28 -07001343 if (iwl_is_fat_tx_allowed(priv, &sta->ht_cap))
Zhu Yib481de92007-09-25 17:54:57 -07001344 tbl->is_fat = 1;
1345 else
1346 tbl->is_fat = 0;
1347
Guy Cohen39e88502008-04-23 17:14:57 -07001348 /* FIXME: - don't toggle SGI here
Zhu Yib481de92007-09-25 17:54:57 -07001349 if (tbl->is_fat) {
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001350 if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
Zhu Yib481de92007-09-25 17:54:57 -07001351 tbl->is_SGI = 1;
1352 else
1353 tbl->is_SGI = 0;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001354 } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
Zhu Yib481de92007-09-25 17:54:57 -07001355 tbl->is_SGI = 1;
1356 else
1357 tbl->is_SGI = 0;
Guy Cohen39e88502008-04-23 17:14:57 -07001358 */
Zhu Yib481de92007-09-25 17:54:57 -07001359
1360 if (is_green)
Guy Cohen39e88502008-04-23 17:14:57 -07001361 tbl->is_SGI = 0; /*11n spec: no SGI in SISO+Greenfield*/
Zhu Yib481de92007-09-25 17:54:57 -07001362
Guy Coheneecd6e52008-04-23 17:14:58 -07001363 rs_set_expected_tpt_table(lq_sta, tbl);
Guy Cohenf935a6d2008-04-23 17:15:02 -07001364 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
Zhu Yib481de92007-09-25 17:54:57 -07001365
Tomas Winklere1623442009-01-27 14:27:56 -08001366 IWL_DEBUG_RATE(priv, "LQ: get best rate %d mask %X\n", rate, rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07001367 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
Tomas Winklere1623442009-01-27 14:27:56 -08001368 IWL_DEBUG_RATE(priv, "can not switch with index %d rate mask %x\n",
Zhu Yib481de92007-09-25 17:54:57 -07001369 rate, rate_mask);
1370 return -1;
1371 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001372 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
Tomas Winklere1623442009-01-27 14:27:56 -08001373 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001374 tbl->current_rate, is_green);
Mohamed Abbas403ab562007-11-06 22:06:25 -08001375 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07001376}
1377
Ben Cahill77626352007-11-29 11:09:44 +08001378/*
1379 * Try to switch to new modulation mode from legacy
1380 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001381static int rs_move_legacy_other(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001382 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001383 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001384 struct ieee80211_sta *sta,
Zhu Yib481de92007-09-25 17:54:57 -07001385 int index)
1386{
Tomas Winklere227cea2008-07-18 13:53:05 +08001387 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1388 struct iwl_scale_tbl_info *search_tbl =
1389 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1390 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1391 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1392 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001393 u8 start_action = tbl->action;
Guy Cohenfde0db32008-04-21 15:42:01 -07001394 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
Guy Cohen3110bef2008-09-09 10:54:54 +08001395 u8 tx_chains_num = priv->hw_params.tx_chains_num;
Guy Cohenfde0db32008-04-21 15:42:01 -07001396 int ret = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001397 u8 update_search_tbl_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001398
1399 for (; ;) {
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001400 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001401 switch (tbl->action) {
Guy Cohen3110bef2008-09-09 10:54:54 +08001402 case IWL_LEGACY_SWITCH_ANTENNA1:
1403 case IWL_LEGACY_SWITCH_ANTENNA2:
Tomas Winklere1623442009-01-27 14:27:56 -08001404 IWL_DEBUG_RATE(priv, "LQ: Legacy toggle Antenna\n");
Zhu Yib481de92007-09-25 17:54:57 -07001405
Guy Cohen3110bef2008-09-09 10:54:54 +08001406 if ((tbl->action == IWL_LEGACY_SWITCH_ANTENNA1 &&
1407 tx_chains_num <= 1) ||
1408 (tbl->action == IWL_LEGACY_SWITCH_ANTENNA2 &&
1409 tx_chains_num <= 2))
1410 break;
1411
Ben Cahill77626352007-11-29 11:09:44 +08001412 /* Don't change antenna if success has been great */
Zhu Yib481de92007-09-25 17:54:57 -07001413 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1414 break;
Ben Cahill77626352007-11-29 11:09:44 +08001415
Ben Cahill77626352007-11-29 11:09:44 +08001416 /* Set up search table to try other antenna */
Zhu Yib481de92007-09-25 17:54:57 -07001417 memcpy(search_tbl, tbl, sz);
1418
Guy Cohenaade00c2008-04-23 17:14:59 -07001419 if (rs_toggle_antenna(valid_tx_ant,
1420 &search_tbl->current_rate, search_tbl)) {
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001421 update_search_tbl_counter = 1;
Guy Cohen3110bef2008-09-09 10:54:54 +08001422 rs_set_expected_tpt_table(lq_sta, search_tbl);
Guy Cohenaade00c2008-04-23 17:14:59 -07001423 goto out;
1424 }
Guy Cohena5e8b502008-05-29 16:35:11 +08001425 break;
Zhu Yib481de92007-09-25 17:54:57 -07001426 case IWL_LEGACY_SWITCH_SISO:
Tomas Winklere1623442009-01-27 14:27:56 -08001427 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to SISO\n");
Ben Cahill77626352007-11-29 11:09:44 +08001428
1429 /* Set up search table to try SISO */
Zhu Yib481de92007-09-25 17:54:57 -07001430 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001431 search_tbl->is_SGI = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001432 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001433 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001434 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001435 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001436 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001437 }
Zhu Yib481de92007-09-25 17:54:57 -07001438
1439 break;
Guy Cohen3110bef2008-09-09 10:54:54 +08001440 case IWL_LEGACY_SWITCH_MIMO2_AB:
1441 case IWL_LEGACY_SWITCH_MIMO2_AC:
1442 case IWL_LEGACY_SWITCH_MIMO2_BC:
Tomas Winklere1623442009-01-27 14:27:56 -08001443 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to MIMO2\n");
Ben Cahill77626352007-11-29 11:09:44 +08001444
1445 /* Set up search table to try MIMO */
Zhu Yib481de92007-09-25 17:54:57 -07001446 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001447 search_tbl->is_SGI = 0;
Guy Cohen3110bef2008-09-09 10:54:54 +08001448
1449 if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AB)
1450 search_tbl->ant_type = ANT_AB;
1451 else if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AC)
1452 search_tbl->ant_type = ANT_AC;
1453 else
1454 search_tbl->ant_type = ANT_BC;
1455
1456 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1457 break;
1458
Guy Cohenfde0db32008-04-21 15:42:01 -07001459 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001460 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001461 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001462 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001463 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001464 }
Zhu Yib481de92007-09-25 17:54:57 -07001465 break;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001466
1467 case IWL_LEGACY_SWITCH_MIMO3_ABC:
1468 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to MIMO3\n");
1469
1470 /* Set up search table to try MIMO3 */
1471 memcpy(search_tbl, tbl, sz);
1472 search_tbl->is_SGI = 0;
1473
1474 search_tbl->ant_type = ANT_ABC;
1475
1476 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1477 break;
1478
1479 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1480 search_tbl, index);
1481 if (!ret) {
1482 lq_sta->action_counter = 0;
1483 goto out;
1484 }
1485 break;
Zhu Yib481de92007-09-25 17:54:57 -07001486 }
1487 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001488 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001489 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
Zhu Yib481de92007-09-25 17:54:57 -07001490
1491 if (tbl->action == start_action)
1492 break;
1493
1494 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001495 search_tbl->lq_type = LQ_NONE;
Zhu Yib481de92007-09-25 17:54:57 -07001496 return 0;
1497
Guy Cohen3110bef2008-09-09 10:54:54 +08001498out:
1499 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001500 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001501 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001502 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001503 if (update_search_tbl_counter)
1504 search_tbl->action = tbl->action;
Zhu Yib481de92007-09-25 17:54:57 -07001505 return 0;
1506
1507}
1508
Ben Cahill77626352007-11-29 11:09:44 +08001509/*
1510 * Try to switch to new modulation mode from SISO
1511 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001512static int rs_move_siso_to_other(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001513 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001514 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001515 struct ieee80211_sta *sta, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001516{
Tomas Winklerc33104f2008-01-14 17:46:21 -08001517 u8 is_green = lq_sta->is_green;
Tomas Winklere227cea2008-07-18 13:53:05 +08001518 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1519 struct iwl_scale_tbl_info *search_tbl =
1520 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1521 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1522 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1523 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001524 u8 start_action = tbl->action;
Guy Cohenfde0db32008-04-21 15:42:01 -07001525 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
Guy Cohen3110bef2008-09-09 10:54:54 +08001526 u8 tx_chains_num = priv->hw_params.tx_chains_num;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001527 u8 update_search_tbl_counter = 0;
Guy Cohenfde0db32008-04-21 15:42:01 -07001528 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001529
1530 for (;;) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001531 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001532 switch (tbl->action) {
Guy Cohen3110bef2008-09-09 10:54:54 +08001533 case IWL_SISO_SWITCH_ANTENNA1:
1534 case IWL_SISO_SWITCH_ANTENNA2:
Tomas Winklere1623442009-01-27 14:27:56 -08001535 IWL_DEBUG_RATE(priv, "LQ: SISO toggle Antenna\n");
Guy Cohen3110bef2008-09-09 10:54:54 +08001536
1537 if ((tbl->action == IWL_SISO_SWITCH_ANTENNA1 &&
1538 tx_chains_num <= 1) ||
1539 (tbl->action == IWL_SISO_SWITCH_ANTENNA2 &&
1540 tx_chains_num <= 2))
1541 break;
1542
Zhu Yib481de92007-09-25 17:54:57 -07001543 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1544 break;
Zhu Yib481de92007-09-25 17:54:57 -07001545
1546 memcpy(search_tbl, tbl, sz);
Guy Cohenaade00c2008-04-23 17:14:59 -07001547 if (rs_toggle_antenna(valid_tx_ant,
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001548 &search_tbl->current_rate, search_tbl)) {
1549 update_search_tbl_counter = 1;
Guy Cohenaade00c2008-04-23 17:14:59 -07001550 goto out;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001551 }
Guy Cohena5e8b502008-05-29 16:35:11 +08001552 break;
Guy Cohen3110bef2008-09-09 10:54:54 +08001553 case IWL_SISO_SWITCH_MIMO2_AB:
1554 case IWL_SISO_SWITCH_MIMO2_AC:
1555 case IWL_SISO_SWITCH_MIMO2_BC:
Tomas Winklere1623442009-01-27 14:27:56 -08001556 IWL_DEBUG_RATE(priv, "LQ: SISO switch to MIMO2\n");
Zhu Yib481de92007-09-25 17:54:57 -07001557 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001558 search_tbl->is_SGI = 0;
Guy Cohen3110bef2008-09-09 10:54:54 +08001559
1560 if (tbl->action == IWL_SISO_SWITCH_MIMO2_AB)
1561 search_tbl->ant_type = ANT_AB;
1562 else if (tbl->action == IWL_SISO_SWITCH_MIMO2_AC)
1563 search_tbl->ant_type = ANT_AC;
1564 else
1565 search_tbl->ant_type = ANT_BC;
1566
1567 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1568 break;
1569
Guy Cohenfde0db32008-04-21 15:42:01 -07001570 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001571 search_tbl, index);
Guy Cohen3110bef2008-09-09 10:54:54 +08001572 if (!ret)
Zhu Yib481de92007-09-25 17:54:57 -07001573 goto out;
1574 break;
1575 case IWL_SISO_SWITCH_GI:
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001576 if (!tbl->is_fat &&
1577 !(priv->current_ht_config.sgf &
1578 HT_SHORT_GI_20MHZ))
1579 break;
1580 if (tbl->is_fat &&
1581 !(priv->current_ht_config.sgf &
1582 HT_SHORT_GI_40MHZ))
1583 break;
1584
Tomas Winklere1623442009-01-27 14:27:56 -08001585 IWL_DEBUG_RATE(priv, "LQ: SISO toggle SGI/NGI\n");
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001586
Zhu Yib481de92007-09-25 17:54:57 -07001587 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001588 if (is_green) {
1589 if (!tbl->is_SGI)
1590 break;
1591 else
Winkler, Tomas15b16872008-12-19 10:37:33 +08001592 IWL_ERR(priv,
1593 "SGI was set in GF+SISO\n");
Zhu Yib481de92007-09-25 17:54:57 -07001594 }
Guy Cohen39e88502008-04-23 17:14:57 -07001595 search_tbl->is_SGI = !tbl->is_SGI;
Guy Coheneecd6e52008-04-23 17:14:58 -07001596 rs_set_expected_tpt_table(lq_sta, search_tbl);
Guy Cohen39e88502008-04-23 17:14:57 -07001597 if (tbl->is_SGI) {
1598 s32 tpt = lq_sta->last_tpt / 100;
1599 if (tpt >= search_tbl->expected_tpt[index])
1600 break;
1601 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001602 search_tbl->current_rate =
1603 rate_n_flags_from_tbl(priv, search_tbl,
1604 index, is_green);
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001605 update_search_tbl_counter = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001606 goto out;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001607 case IWL_SISO_SWITCH_MIMO3_ABC:
1608 IWL_DEBUG_RATE(priv, "LQ: SISO switch to MIMO3\n");
1609 memcpy(search_tbl, tbl, sz);
1610 search_tbl->is_SGI = 0;
1611 search_tbl->ant_type = ANT_ABC;
1612
1613 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1614 break;
1615
1616 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1617 search_tbl, index);
1618 if (!ret)
1619 goto out;
1620 break;
Zhu Yib481de92007-09-25 17:54:57 -07001621 }
1622 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001623 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001624 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
Zhu Yib481de92007-09-25 17:54:57 -07001625
1626 if (tbl->action == start_action)
1627 break;
1628 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001629 search_tbl->lq_type = LQ_NONE;
Zhu Yib481de92007-09-25 17:54:57 -07001630 return 0;
1631
1632 out:
Guy Cohen3110bef2008-09-09 10:54:54 +08001633 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001634 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001635 if (tbl->action > IWL_SISO_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001636 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001637 if (update_search_tbl_counter)
1638 search_tbl->action = tbl->action;
1639
Zhu Yib481de92007-09-25 17:54:57 -07001640 return 0;
1641}
1642
Ben Cahill77626352007-11-29 11:09:44 +08001643/*
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001644 * Try to switch to new modulation mode from MIMO2
Ben Cahill77626352007-11-29 11:09:44 +08001645 */
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001646static int rs_move_mimo2_to_other(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001647 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001648 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001649 struct ieee80211_sta *sta, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001650{
Tomas Winklerc33104f2008-01-14 17:46:21 -08001651 s8 is_green = lq_sta->is_green;
Tomas Winklere227cea2008-07-18 13:53:05 +08001652 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1653 struct iwl_scale_tbl_info *search_tbl =
1654 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Guy Cohen3110bef2008-09-09 10:54:54 +08001655 struct iwl_rate_scale_data *window = &(tbl->win[index]);
Tomas Winklere227cea2008-07-18 13:53:05 +08001656 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1657 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001658 u8 start_action = tbl->action;
Guy Cohen3110bef2008-09-09 10:54:54 +08001659 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1660 u8 tx_chains_num = priv->hw_params.tx_chains_num;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001661 u8 update_search_tbl_counter = 0;
Guy Cohenaade00c2008-04-23 17:14:59 -07001662 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001663
1664 for (;;) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001665 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001666 switch (tbl->action) {
Guy Cohen3110bef2008-09-09 10:54:54 +08001667 case IWL_MIMO2_SWITCH_ANTENNA1:
1668 case IWL_MIMO2_SWITCH_ANTENNA2:
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001669 IWL_DEBUG_RATE(priv, "LQ: MIMO2 toggle Antennas\n");
Guy Cohen3110bef2008-09-09 10:54:54 +08001670
1671 if (tx_chains_num <= 2)
1672 break;
1673
1674 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1675 break;
1676
1677 memcpy(search_tbl, tbl, sz);
1678 if (rs_toggle_antenna(valid_tx_ant,
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001679 &search_tbl->current_rate, search_tbl)) {
1680 update_search_tbl_counter = 1;
Guy Cohen3110bef2008-09-09 10:54:54 +08001681 goto out;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001682 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001683 break;
1684 case IWL_MIMO2_SWITCH_SISO_A:
1685 case IWL_MIMO2_SWITCH_SISO_B:
1686 case IWL_MIMO2_SWITCH_SISO_C:
Tomas Winklere1623442009-01-27 14:27:56 -08001687 IWL_DEBUG_RATE(priv, "LQ: MIMO2 switch to SISO\n");
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001688
Ben Cahill77626352007-11-29 11:09:44 +08001689 /* Set up new search table for SISO */
Zhu Yib481de92007-09-25 17:54:57 -07001690 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001691
Guy Cohen3110bef2008-09-09 10:54:54 +08001692 if (tbl->action == IWL_MIMO2_SWITCH_SISO_A)
Guy Cohenfde0db32008-04-21 15:42:01 -07001693 search_tbl->ant_type = ANT_A;
Guy Cohen3110bef2008-09-09 10:54:54 +08001694 else if (tbl->action == IWL_MIMO2_SWITCH_SISO_B)
Guy Cohenfde0db32008-04-21 15:42:01 -07001695 search_tbl->ant_type = ANT_B;
Guy Cohen3110bef2008-09-09 10:54:54 +08001696 else
1697 search_tbl->ant_type = ANT_C;
1698
1699 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1700 break;
Zhu Yib481de92007-09-25 17:54:57 -07001701
Tomas Winklerc33104f2008-01-14 17:46:21 -08001702 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001703 search_tbl, index);
Guy Cohen3110bef2008-09-09 10:54:54 +08001704 if (!ret)
Zhu Yib481de92007-09-25 17:54:57 -07001705 goto out;
Guy Cohen3110bef2008-09-09 10:54:54 +08001706
Zhu Yib481de92007-09-25 17:54:57 -07001707 break;
1708
Guy Cohen3110bef2008-09-09 10:54:54 +08001709 case IWL_MIMO2_SWITCH_GI:
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001710 if (!tbl->is_fat &&
1711 !(priv->current_ht_config.sgf &
1712 HT_SHORT_GI_20MHZ))
1713 break;
1714 if (tbl->is_fat &&
1715 !(priv->current_ht_config.sgf &
1716 HT_SHORT_GI_40MHZ))
1717 break;
1718
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001719 IWL_DEBUG_RATE(priv, "LQ: MIMO2 toggle SGI/NGI\n");
1720
1721 /* Set up new search table for MIMO2 */
1722 memcpy(search_tbl, tbl, sz);
1723 search_tbl->is_SGI = !tbl->is_SGI;
1724 rs_set_expected_tpt_table(lq_sta, search_tbl);
1725 /*
1726 * If active table already uses the fastest possible
1727 * modulation (dual stream with short guard interval),
1728 * and it's working well, there's no need to look
1729 * for a better type of modulation!
1730 */
1731 if (tbl->is_SGI) {
1732 s32 tpt = lq_sta->last_tpt / 100;
1733 if (tpt >= search_tbl->expected_tpt[index])
1734 break;
1735 }
1736 search_tbl->current_rate =
1737 rate_n_flags_from_tbl(priv, search_tbl,
1738 index, is_green);
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001739 update_search_tbl_counter = 1;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001740 goto out;
1741
1742 case IWL_MIMO2_SWITCH_MIMO3_ABC:
1743 IWL_DEBUG_RATE(priv, "LQ: MIMO2 switch to MIMO3\n");
1744 memcpy(search_tbl, tbl, sz);
1745 search_tbl->is_SGI = 0;
1746 search_tbl->ant_type = ANT_ABC;
1747
1748 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1749 break;
1750
1751 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1752 search_tbl, index);
1753 if (!ret)
1754 goto out;
1755
1756 break;
1757 }
1758 tbl->action++;
1759 if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
1760 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
1761
1762 if (tbl->action == start_action)
1763 break;
1764 }
1765 search_tbl->lq_type = LQ_NONE;
1766 return 0;
1767 out:
1768 lq_sta->search_better_tbl = 1;
1769 tbl->action++;
1770 if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
1771 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001772 if (update_search_tbl_counter)
1773 search_tbl->action = tbl->action;
1774
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001775 return 0;
1776
1777}
1778
1779/*
1780 * Try to switch to new modulation mode from MIMO3
1781 */
1782static int rs_move_mimo3_to_other(struct iwl_priv *priv,
1783 struct iwl_lq_sta *lq_sta,
1784 struct ieee80211_conf *conf,
1785 struct ieee80211_sta *sta, int index)
1786{
1787 s8 is_green = lq_sta->is_green;
1788 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1789 struct iwl_scale_tbl_info *search_tbl =
1790 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1791 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1792 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1793 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1794 u8 start_action = tbl->action;
1795 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1796 u8 tx_chains_num = priv->hw_params.tx_chains_num;
1797 int ret;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001798 u8 update_search_tbl_counter = 0;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001799
1800 for (;;) {
1801 lq_sta->action_counter++;
1802 switch (tbl->action) {
1803 case IWL_MIMO3_SWITCH_ANTENNA1:
1804 case IWL_MIMO3_SWITCH_ANTENNA2:
1805 IWL_DEBUG_RATE(priv, "LQ: MIMO3 toggle Antennas\n");
1806
1807 if (tx_chains_num <= 3)
1808 break;
1809
1810 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1811 break;
1812
1813 memcpy(search_tbl, tbl, sz);
1814 if (rs_toggle_antenna(valid_tx_ant,
1815 &search_tbl->current_rate, search_tbl))
1816 goto out;
1817 break;
1818 case IWL_MIMO3_SWITCH_SISO_A:
1819 case IWL_MIMO3_SWITCH_SISO_B:
1820 case IWL_MIMO3_SWITCH_SISO_C:
1821 IWL_DEBUG_RATE(priv, "LQ: MIMO3 switch to SISO\n");
1822
1823 /* Set up new search table for SISO */
1824 memcpy(search_tbl, tbl, sz);
1825
1826 if (tbl->action == IWL_MIMO3_SWITCH_SISO_A)
1827 search_tbl->ant_type = ANT_A;
1828 else if (tbl->action == IWL_MIMO3_SWITCH_SISO_B)
1829 search_tbl->ant_type = ANT_B;
1830 else
1831 search_tbl->ant_type = ANT_C;
1832
1833 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1834 break;
1835
1836 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
1837 search_tbl, index);
1838 if (!ret)
1839 goto out;
1840
1841 break;
1842
1843 case IWL_MIMO3_SWITCH_MIMO2_AB:
1844 case IWL_MIMO3_SWITCH_MIMO2_AC:
1845 case IWL_MIMO3_SWITCH_MIMO2_BC:
1846 IWL_DEBUG_RATE(priv, "LQ: MIMO3 switch to MIMO2\n");
1847
1848 memcpy(search_tbl, tbl, sz);
1849 search_tbl->is_SGI = 0;
1850 if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AB)
1851 search_tbl->ant_type = ANT_AB;
1852 else if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AC)
1853 search_tbl->ant_type = ANT_AC;
1854 else
1855 search_tbl->ant_type = ANT_BC;
1856
1857 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1858 break;
1859
1860 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
1861 search_tbl, index);
1862 if (!ret)
1863 goto out;
1864
1865 break;
1866
1867 case IWL_MIMO3_SWITCH_GI:
1868 if (!tbl->is_fat &&
1869 !(priv->current_ht_config.sgf &
1870 HT_SHORT_GI_20MHZ))
1871 break;
1872 if (tbl->is_fat &&
1873 !(priv->current_ht_config.sgf &
1874 HT_SHORT_GI_40MHZ))
1875 break;
1876
1877 IWL_DEBUG_RATE(priv, "LQ: MIMO3 toggle SGI/NGI\n");
Ben Cahill77626352007-11-29 11:09:44 +08001878
1879 /* Set up new search table for MIMO */
Zhu Yib481de92007-09-25 17:54:57 -07001880 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001881 search_tbl->is_SGI = !tbl->is_SGI;
Guy Coheneecd6e52008-04-23 17:14:58 -07001882 rs_set_expected_tpt_table(lq_sta, search_tbl);
Ben Cahill77626352007-11-29 11:09:44 +08001883 /*
1884 * If active table already uses the fastest possible
1885 * modulation (dual stream with short guard interval),
1886 * and it's working well, there's no need to look
1887 * for a better type of modulation!
1888 */
Guy Cohen39e88502008-04-23 17:14:57 -07001889 if (tbl->is_SGI) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001890 s32 tpt = lq_sta->last_tpt / 100;
Guy Cohen39e88502008-04-23 17:14:57 -07001891 if (tpt >= search_tbl->expected_tpt[index])
1892 break;
Zhu Yib481de92007-09-25 17:54:57 -07001893 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001894 search_tbl->current_rate =
1895 rate_n_flags_from_tbl(priv, search_tbl,
1896 index, is_green);
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001897 update_search_tbl_counter = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001898 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07001899 }
1900 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001901 if (tbl->action > IWL_MIMO3_SWITCH_GI)
1902 tbl->action = IWL_MIMO3_SWITCH_ANTENNA1;
Zhu Yib481de92007-09-25 17:54:57 -07001903
1904 if (tbl->action == start_action)
1905 break;
1906 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001907 search_tbl->lq_type = LQ_NONE;
Zhu Yib481de92007-09-25 17:54:57 -07001908 return 0;
1909 out:
Guy Cohen3110bef2008-09-09 10:54:54 +08001910 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001911 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001912 if (tbl->action > IWL_MIMO3_SWITCH_GI)
1913 tbl->action = IWL_MIMO3_SWITCH_ANTENNA1;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001914 if (update_search_tbl_counter)
1915 search_tbl->action = tbl->action;
1916
Zhu Yib481de92007-09-25 17:54:57 -07001917 return 0;
1918
1919}
1920
Ben Cahill77626352007-11-29 11:09:44 +08001921/*
1922 * Check whether we should continue using same modulation mode, or
1923 * begin search for a new mode, based on:
1924 * 1) # tx successes or failures while using this mode
1925 * 2) # times calling this function
1926 * 3) elapsed time in this mode (not used, for now)
1927 */
Tomas Winklere227cea2008-07-18 13:53:05 +08001928static void rs_stay_in_table(struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07001929{
Tomas Winklere227cea2008-07-18 13:53:05 +08001930 struct iwl_scale_tbl_info *tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001931 int i;
1932 int active_tbl;
1933 int flush_interval_passed = 0;
Ester Kummerbf403db2008-05-05 10:22:40 +08001934 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07001935
Ester Kummerbf403db2008-05-05 10:22:40 +08001936 priv = lq_sta->drv;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001937 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001938
Tomas Winklerc33104f2008-01-14 17:46:21 -08001939 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07001940
Ben Cahill77626352007-11-29 11:09:44 +08001941 /* If we've been disallowing search, see if we should now allow it */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001942 if (lq_sta->stay_in_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07001943
Ben Cahill77626352007-11-29 11:09:44 +08001944 /* Elapsed time using current modulation mode */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001945 if (lq_sta->flush_timer)
Zhu Yib481de92007-09-25 17:54:57 -07001946 flush_interval_passed =
Mohamed Abbas447fee72009-04-20 14:37:02 -07001947 time_after(jiffies,
1948 (unsigned long)(lq_sta->flush_timer +
Zhu Yib481de92007-09-25 17:54:57 -07001949 IWL_RATE_SCALE_FLUSH_INTVL));
1950
Ben Cahill77626352007-11-29 11:09:44 +08001951 /*
1952 * Check if we should allow search for new modulation mode.
1953 * If many frames have failed or succeeded, or we've used
1954 * this same modulation for a long time, allow search, and
1955 * reset history stats that keep track of whether we should
1956 * allow a new search. Also (below) reset all bitmaps and
1957 * stats in active history.
1958 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001959 if ((lq_sta->total_failed > lq_sta->max_failure_limit) ||
1960 (lq_sta->total_success > lq_sta->max_success_limit) ||
1961 ((!lq_sta->search_better_tbl) && (lq_sta->flush_timer)
Zhu Yib481de92007-09-25 17:54:57 -07001962 && (flush_interval_passed))) {
Tomas Winklere1623442009-01-27 14:27:56 -08001963 IWL_DEBUG_RATE(priv, "LQ: stay is expired %d %d %d\n:",
Tomas Winklerc33104f2008-01-14 17:46:21 -08001964 lq_sta->total_failed,
1965 lq_sta->total_success,
Zhu Yib481de92007-09-25 17:54:57 -07001966 flush_interval_passed);
Ben Cahill77626352007-11-29 11:09:44 +08001967
1968 /* Allow search for new mode */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001969 lq_sta->stay_in_tbl = 0; /* only place reset */
1970 lq_sta->total_failed = 0;
1971 lq_sta->total_success = 0;
1972 lq_sta->flush_timer = 0;
Ben Cahill77626352007-11-29 11:09:44 +08001973
1974 /*
1975 * Else if we've used this modulation mode enough repetitions
1976 * (regardless of elapsed time or success/failure), reset
1977 * history bitmaps and rate-specific stats for all rates in
1978 * active table.
1979 */
Mohamed Abbas403ab562007-11-06 22:06:25 -08001980 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001981 lq_sta->table_count++;
1982 if (lq_sta->table_count >=
1983 lq_sta->table_count_limit) {
1984 lq_sta->table_count = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001985
Tomas Winklere1623442009-01-27 14:27:56 -08001986 IWL_DEBUG_RATE(priv, "LQ: stay in table clear win\n");
Zhu Yib481de92007-09-25 17:54:57 -07001987 for (i = 0; i < IWL_RATE_COUNT; i++)
1988 rs_rate_scale_clear_window(
1989 &(tbl->win[i]));
1990 }
1991 }
1992
Ben Cahill77626352007-11-29 11:09:44 +08001993 /* If transitioning to allow "search", reset all history
1994 * bitmaps and stats in active table (this will become the new
1995 * "search" table). */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001996 if (!lq_sta->stay_in_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07001997 for (i = 0; i < IWL_RATE_COUNT; i++)
1998 rs_rate_scale_clear_window(&(tbl->win[i]));
1999 }
2000 }
2001}
2002
Ben Cahill77626352007-11-29 11:09:44 +08002003/*
2004 * Do rate scaling and search for new modulation mode.
2005 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002006static void rs_rate_scale_perform(struct iwl_priv *priv,
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002007 struct sk_buff *skb,
Johannes Berg4b7679a2008-09-18 18:14:18 +02002008 struct ieee80211_sta *sta,
2009 struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002010{
Johannes Berg4b7679a2008-09-18 18:14:18 +02002011 struct ieee80211_hw *hw = priv->hw;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002012 struct ieee80211_conf *conf = &hw->conf;
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002013 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2014 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002015 int low = IWL_RATE_INVALID;
2016 int high = IWL_RATE_INVALID;
2017 int index;
2018 int i;
Tomas Winklere227cea2008-07-18 13:53:05 +08002019 struct iwl_rate_scale_data *window = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002020 int current_tpt = IWL_INVALID_VALUE;
2021 int low_tpt = IWL_INVALID_VALUE;
2022 int high_tpt = IWL_INVALID_VALUE;
2023 u32 fail_count;
2024 s8 scale_action = 0;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002025 u16 rate_mask;
Zhu Yib481de92007-09-25 17:54:57 -07002026 u8 update_lq = 0;
Tomas Winklere227cea2008-07-18 13:53:05 +08002027 struct iwl_scale_tbl_info *tbl, *tbl1;
Zhu Yib481de92007-09-25 17:54:57 -07002028 u16 rate_scale_index_msk = 0;
Guy Cohen39e88502008-04-23 17:14:57 -07002029 u32 rate;
Zhu Yib481de92007-09-25 17:54:57 -07002030 u8 is_green = 0;
2031 u8 active_tbl = 0;
2032 u8 done_search = 0;
2033 u16 high_low;
Guy Cohena5e8b502008-05-29 16:35:11 +08002034 s32 sr;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002035 u8 tid = MAX_TID_COUNT;
Wey-Yi Guy86b47662009-03-10 14:35:09 -07002036 struct iwl_tid_data *tid_data;
Zhu Yib481de92007-09-25 17:54:57 -07002037
Tomas Winklere1623442009-01-27 14:27:56 -08002038 IWL_DEBUG_RATE(priv, "rate scale calculate new rate for skb\n");
Zhu Yib481de92007-09-25 17:54:57 -07002039
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002040 /* Send management frames and NO_ACK data using lowest rate. */
Tomas Winkler417f1142008-11-12 13:14:06 -08002041 /* TODO: this could probably be improved.. */
2042 if (!ieee80211_is_data(hdr->frame_control) ||
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002043 info->flags & IEEE80211_TX_CTL_NO_ACK)
Zhu Yib481de92007-09-25 17:54:57 -07002044 return;
Zhu Yib481de92007-09-25 17:54:57 -07002045
Johannes Berg4b7679a2008-09-18 18:14:18 +02002046 if (!sta || !lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002047 return;
2048
Johannes Berg4b7679a2008-09-18 18:14:18 +02002049 lq_sta->supp_rates = sta->supp_rates[lq_sta->band];
Zhu Yib481de92007-09-25 17:54:57 -07002050
Ron Rindjunskyfaa29712008-06-12 09:46:58 +08002051 tid = rs_tl_add_packet(lq_sta, hdr);
2052
Ben Cahill77626352007-11-29 11:09:44 +08002053 /*
2054 * Select rate-scale / modulation-mode table to work with in
2055 * the rest of this function: "search" if searching for better
2056 * modulation mode, or "active" if doing rate scaling within a mode.
2057 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002058 if (!lq_sta->search_better_tbl)
2059 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002060 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002061 active_tbl = 1 - lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002062
Tomas Winklerc33104f2008-01-14 17:46:21 -08002063 tbl = &(lq_sta->lq_info[active_tbl]);
Wey-Yi Guy2681b202009-05-21 13:44:22 -07002064 if (is_legacy(tbl->lq_type))
2065 lq_sta->is_green = 0;
2066 else
2067 lq_sta->is_green = rs_use_green(priv, conf);
Tomas Winklerc33104f2008-01-14 17:46:21 -08002068 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07002069
Ben Cahill77626352007-11-29 11:09:44 +08002070 /* current tx rate */
Johannes Bergb7e35002008-09-11 02:22:58 +02002071 index = lq_sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002072
Tomas Winklere1623442009-01-27 14:27:56 -08002073 IWL_DEBUG_RATE(priv, "Rate scale index %d for type %d\n", index,
Zhu Yib481de92007-09-25 17:54:57 -07002074 tbl->lq_type);
2075
Ben Cahill77626352007-11-29 11:09:44 +08002076 /* rates available for this association, and for modulation mode */
Guy Coheneecd6e52008-04-23 17:14:58 -07002077 rate_mask = rs_get_supported_rates(lq_sta, hdr, tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -07002078
Tomas Winklere1623442009-01-27 14:27:56 -08002079 IWL_DEBUG_RATE(priv, "mask 0x%04X \n", rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07002080
2081 /* mask with station rate restriction */
2082 if (is_legacy(tbl->lq_type)) {
Johannes Berg8318d782008-01-24 19:38:38 +01002083 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Ben Cahill77626352007-11-29 11:09:44 +08002084 /* supp_rates has no CCK bits in A mode */
Zhu Yib481de92007-09-25 17:54:57 -07002085 rate_scale_index_msk = (u16) (rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -08002086 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
Zhu Yib481de92007-09-25 17:54:57 -07002087 else
2088 rate_scale_index_msk = (u16) (rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -08002089 lq_sta->supp_rates);
Zhu Yib481de92007-09-25 17:54:57 -07002090
2091 } else
2092 rate_scale_index_msk = rate_mask;
2093
2094 if (!rate_scale_index_msk)
2095 rate_scale_index_msk = rate_mask;
2096
Guy Cohen07bc28e2008-04-23 17:15:03 -07002097 if (!((1 << index) & rate_scale_index_msk)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002098 IWL_ERR(priv, "Current Rate is not valid\n");
Guy Cohen07bc28e2008-04-23 17:15:03 -07002099 return;
Zhu Yib481de92007-09-25 17:54:57 -07002100 }
2101
Ben Cahill77626352007-11-29 11:09:44 +08002102 /* Get expected throughput table and history window for current rate */
Guy Cohen07bc28e2008-04-23 17:15:03 -07002103 if (!tbl->expected_tpt) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002104 IWL_ERR(priv, "tbl->expected_tpt is NULL\n");
Guy Cohen07bc28e2008-04-23 17:15:03 -07002105 return;
2106 }
Zhu Yib481de92007-09-25 17:54:57 -07002107
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002108 /* force user max rate if set by user */
2109 if ((lq_sta->max_rate_idx != -1) &&
2110 (lq_sta->max_rate_idx < index)) {
2111 index = lq_sta->max_rate_idx;
2112 update_lq = 1;
2113 window = &(tbl->win[index]);
2114 goto lq_update;
2115 }
2116
Zhu Yib481de92007-09-25 17:54:57 -07002117 window = &(tbl->win[index]);
2118
Ben Cahill77626352007-11-29 11:09:44 +08002119 /*
2120 * If there is not enough history to calculate actual average
2121 * throughput, keep analyzing results of more tx frames, without
2122 * changing rate or mode (bypass most of the rest of this function).
2123 * Set up new rate table in uCode only if old rate is not supported
2124 * in current association (use new rate found above).
2125 */
Zhu Yib481de92007-09-25 17:54:57 -07002126 fail_count = window->counter - window->success_counter;
Guy Cohen07bc28e2008-04-23 17:15:03 -07002127 if ((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
2128 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002129 IWL_DEBUG_RATE(priv, "LQ: still below TH. succ=%d total=%d "
Zhu Yib481de92007-09-25 17:54:57 -07002130 "for index %d\n",
2131 window->success_counter, window->counter, index);
Ben Cahill77626352007-11-29 11:09:44 +08002132
2133 /* Can't calculate this yet; not enough history */
Zhu Yib481de92007-09-25 17:54:57 -07002134 window->average_tpt = IWL_INVALID_VALUE;
Ben Cahill77626352007-11-29 11:09:44 +08002135
2136 /* Should we stay with this modulation mode,
2137 * or search for a new one? */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002138 rs_stay_in_table(lq_sta);
Ben Cahill77626352007-11-29 11:09:44 +08002139
Zhu Yib481de92007-09-25 17:54:57 -07002140 goto out;
Guy Cohen3110bef2008-09-09 10:54:54 +08002141 }
Zhu Yib481de92007-09-25 17:54:57 -07002142
Ben Cahill77626352007-11-29 11:09:44 +08002143 /* Else we have enough samples; calculate estimate of
2144 * actual average throughput */
Guy Cohen3110bef2008-09-09 10:54:54 +08002145
2146 BUG_ON(window->average_tpt != ((window->success_ratio *
2147 tbl->expected_tpt[index] + 64) / 128));
Zhu Yib481de92007-09-25 17:54:57 -07002148
Ben Cahill77626352007-11-29 11:09:44 +08002149 /* If we are searching for better modulation mode, check success. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002150 if (lq_sta->search_better_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07002151
Ben Cahill77626352007-11-29 11:09:44 +08002152 /* If good success, continue using the "search" mode;
2153 * no need to send new link quality command, since we're
2154 * continuing to use the setup that we've been trying. */
Guy Cohen07bc28e2008-04-23 17:15:03 -07002155 if (window->average_tpt > lq_sta->last_tpt) {
2156
Tomas Winklere1623442009-01-27 14:27:56 -08002157 IWL_DEBUG_RATE(priv, "LQ: SWITCHING TO NEW TABLE "
Guy Cohen07bc28e2008-04-23 17:15:03 -07002158 "suc=%d cur-tpt=%d old-tpt=%d\n",
2159 window->success_ratio,
2160 window->average_tpt,
2161 lq_sta->last_tpt);
2162
2163 if (!is_legacy(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08002164 lq_sta->enable_counter = 1;
Guy Cohen07bc28e2008-04-23 17:15:03 -07002165
Ben Cahill77626352007-11-29 11:09:44 +08002166 /* Swap tables; "search" becomes "active" */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002167 lq_sta->active_tbl = active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002168 current_tpt = window->average_tpt;
Ben Cahill77626352007-11-29 11:09:44 +08002169
2170 /* Else poor success; go back to mode in "active" table */
Zhu Yib481de92007-09-25 17:54:57 -07002171 } else {
Guy Cohen07bc28e2008-04-23 17:15:03 -07002172
Tomas Winklere1623442009-01-27 14:27:56 -08002173 IWL_DEBUG_RATE(priv, "LQ: GOING BACK TO THE OLD TABLE "
Guy Cohen07bc28e2008-04-23 17:15:03 -07002174 "suc=%d cur-tpt=%d old-tpt=%d\n",
2175 window->success_ratio,
2176 window->average_tpt,
2177 lq_sta->last_tpt);
2178
Ben Cahill77626352007-11-29 11:09:44 +08002179 /* Nullify "search" table */
Zhu Yib481de92007-09-25 17:54:57 -07002180 tbl->lq_type = LQ_NONE;
Ben Cahill77626352007-11-29 11:09:44 +08002181
2182 /* Revert to "active" table */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002183 active_tbl = lq_sta->active_tbl;
2184 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07002185
Ben Cahill77626352007-11-29 11:09:44 +08002186 /* Revert to "active" rate and throughput info */
Tomas Winklere7d326a2008-06-12 09:47:11 +08002187 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
Tomas Winklerc33104f2008-01-14 17:46:21 -08002188 current_tpt = lq_sta->last_tpt;
Ben Cahill77626352007-11-29 11:09:44 +08002189
2190 /* Need to set up a new rate table in uCode */
2191 update_lq = 1;
Zhu Yib481de92007-09-25 17:54:57 -07002192 }
Ben Cahill77626352007-11-29 11:09:44 +08002193
2194 /* Either way, we've made a decision; modulation mode
2195 * search is done, allow rate adjustment next time. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002196 lq_sta->search_better_tbl = 0;
Ben Cahill77626352007-11-29 11:09:44 +08002197 done_search = 1; /* Don't switch modes below! */
Zhu Yib481de92007-09-25 17:54:57 -07002198 goto lq_update;
2199 }
2200
Ben Cahill77626352007-11-29 11:09:44 +08002201 /* (Else) not in search of better modulation mode, try for better
2202 * starting rate, while staying in this mode. */
Ester Kummerbf403db2008-05-05 10:22:40 +08002203 high_low = rs_get_adjacent_rate(priv, index, rate_scale_index_msk,
Zhu Yib481de92007-09-25 17:54:57 -07002204 tbl->lq_type);
2205 low = high_low & 0xff;
2206 high = (high_low >> 8) & 0xff;
2207
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002208 /* If user set max rate, dont allow higher than user constrain */
2209 if ((lq_sta->max_rate_idx != -1) &&
2210 (lq_sta->max_rate_idx < high))
2211 high = IWL_RATE_INVALID;
2212
Guy Cohena5e8b502008-05-29 16:35:11 +08002213 sr = window->success_ratio;
2214
Ben Cahill77626352007-11-29 11:09:44 +08002215 /* Collect measured throughputs for current and adjacent rates */
Zhu Yib481de92007-09-25 17:54:57 -07002216 current_tpt = window->average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07002217 if (low != IWL_RATE_INVALID)
2218 low_tpt = tbl->win[low].average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07002219 if (high != IWL_RATE_INVALID)
2220 high_tpt = tbl->win[high].average_tpt;
2221
Guy Cohena5e8b502008-05-29 16:35:11 +08002222 scale_action = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002223
Ben Cahill77626352007-11-29 11:09:44 +08002224 /* Too many failures, decrease rate */
Guy Cohena5e8b502008-05-29 16:35:11 +08002225 if ((sr <= IWL_RATE_DECREASE_TH) || (current_tpt == 0)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002226 IWL_DEBUG_RATE(priv, "decrease rate because of low success_ratio\n");
Zhu Yib481de92007-09-25 17:54:57 -07002227 scale_action = -1;
Ben Cahill77626352007-11-29 11:09:44 +08002228
2229 /* No throughput measured yet for adjacent rates; try increase. */
Zhu Yib481de92007-09-25 17:54:57 -07002230 } else if ((low_tpt == IWL_INVALID_VALUE) &&
Guy Cohena5e8b502008-05-29 16:35:11 +08002231 (high_tpt == IWL_INVALID_VALUE)) {
2232
2233 if (high != IWL_RATE_INVALID && sr >= IWL_RATE_INCREASE_TH)
2234 scale_action = 1;
2235 else if (low != IWL_RATE_INVALID)
Wey-Yi Guy8fe72312009-03-10 14:35:10 -07002236 scale_action = 0;
Guy Cohena5e8b502008-05-29 16:35:11 +08002237 }
Ben Cahill77626352007-11-29 11:09:44 +08002238
2239 /* Both adjacent throughputs are measured, but neither one has better
2240 * throughput; we're using the best rate, don't change it! */
Zhu Yib481de92007-09-25 17:54:57 -07002241 else if ((low_tpt != IWL_INVALID_VALUE) &&
2242 (high_tpt != IWL_INVALID_VALUE) &&
2243 (low_tpt < current_tpt) &&
2244 (high_tpt < current_tpt))
2245 scale_action = 0;
Ben Cahill77626352007-11-29 11:09:44 +08002246
2247 /* At least one adjacent rate's throughput is measured,
2248 * and may have better performance. */
Zhu Yib481de92007-09-25 17:54:57 -07002249 else {
Ben Cahill77626352007-11-29 11:09:44 +08002250 /* Higher adjacent rate's throughput is measured */
Zhu Yib481de92007-09-25 17:54:57 -07002251 if (high_tpt != IWL_INVALID_VALUE) {
Ben Cahill77626352007-11-29 11:09:44 +08002252 /* Higher rate has better throughput */
Guy Cohena5e8b502008-05-29 16:35:11 +08002253 if (high_tpt > current_tpt &&
2254 sr >= IWL_RATE_INCREASE_TH) {
Zhu Yib481de92007-09-25 17:54:57 -07002255 scale_action = 1;
Guy Cohena5e8b502008-05-29 16:35:11 +08002256 } else {
Wey-Yi Guy8fe72312009-03-10 14:35:10 -07002257 scale_action = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002258 }
Ben Cahill77626352007-11-29 11:09:44 +08002259
2260 /* Lower adjacent rate's throughput is measured */
Zhu Yib481de92007-09-25 17:54:57 -07002261 } else if (low_tpt != IWL_INVALID_VALUE) {
Ben Cahill77626352007-11-29 11:09:44 +08002262 /* Lower rate has better throughput */
Zhu Yib481de92007-09-25 17:54:57 -07002263 if (low_tpt > current_tpt) {
Tomas Winklere1623442009-01-27 14:27:56 -08002264 IWL_DEBUG_RATE(priv,
2265 "decrease rate because of low tpt\n");
Zhu Yib481de92007-09-25 17:54:57 -07002266 scale_action = -1;
Guy Cohena5e8b502008-05-29 16:35:11 +08002267 } else if (sr >= IWL_RATE_INCREASE_TH) {
Zhu Yib481de92007-09-25 17:54:57 -07002268 scale_action = 1;
Guy Cohena5e8b502008-05-29 16:35:11 +08002269 }
Zhu Yib481de92007-09-25 17:54:57 -07002270 }
2271 }
2272
Ben Cahill77626352007-11-29 11:09:44 +08002273 /* Sanity check; asked for decrease, but success rate or throughput
2274 * has been good at old rate. Don't change it. */
Guy Cohena5e8b502008-05-29 16:35:11 +08002275 if ((scale_action == -1) && (low != IWL_RATE_INVALID) &&
2276 ((sr > IWL_RATE_HIGH_TH) ||
Zhu Yib481de92007-09-25 17:54:57 -07002277 (current_tpt > (100 * tbl->expected_tpt[low]))))
Zhu Yib481de92007-09-25 17:54:57 -07002278 scale_action = 0;
2279
2280 switch (scale_action) {
2281 case -1:
Ben Cahill77626352007-11-29 11:09:44 +08002282 /* Decrease starting rate, update uCode's rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002283 if (low != IWL_RATE_INVALID) {
2284 update_lq = 1;
2285 index = low;
2286 }
Mohamed Abbas447fee72009-04-20 14:37:02 -07002287
Zhu Yib481de92007-09-25 17:54:57 -07002288 break;
2289 case 1:
Ben Cahill77626352007-11-29 11:09:44 +08002290 /* Increase starting rate, update uCode's rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002291 if (high != IWL_RATE_INVALID) {
2292 update_lq = 1;
2293 index = high;
2294 }
2295
2296 break;
2297 case 0:
Ben Cahill77626352007-11-29 11:09:44 +08002298 /* No change */
Zhu Yib481de92007-09-25 17:54:57 -07002299 default:
2300 break;
2301 }
2302
Tomas Winklere1623442009-01-27 14:27:56 -08002303 IWL_DEBUG_RATE(priv, "choose rate scale index %d action %d low %d "
Zhu Yib481de92007-09-25 17:54:57 -07002304 "high %d type %d\n",
2305 index, scale_action, low, high, tbl->lq_type);
2306
Guy Cohena5e8b502008-05-29 16:35:11 +08002307lq_update:
Ben Cahill77626352007-11-29 11:09:44 +08002308 /* Replace uCode's rate table for the destination station. */
Zhu Yib481de92007-09-25 17:54:57 -07002309 if (update_lq) {
Tomas Winkler978785a2008-12-19 10:37:31 +08002310 rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
Guy Cohen07bc28e2008-04-23 17:15:03 -07002311 rs_fill_link_cmd(priv, lq_sta, rate);
Tomas Winkler66c73db2008-04-15 16:01:40 -07002312 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07002313 }
Ben Cahill77626352007-11-29 11:09:44 +08002314
2315 /* Should we stay with this modulation mode, or search for a new one? */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002316 rs_stay_in_table(lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002317
Ben Cahill77626352007-11-29 11:09:44 +08002318 /*
2319 * Search for new modulation mode if we're:
2320 * 1) Not changing rates right now
2321 * 2) Not just finishing up a search
2322 * 3) Allowing a new search
2323 */
Guy Cohen47cfd462008-05-27 11:29:34 +08002324 if (!update_lq && !done_search && !lq_sta->stay_in_tbl && window->counter) {
Ben Cahill77626352007-11-29 11:09:44 +08002325 /* Save current throughput to compare with "search" throughput*/
Tomas Winklerc33104f2008-01-14 17:46:21 -08002326 lq_sta->last_tpt = current_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07002327
Ben Cahill77626352007-11-29 11:09:44 +08002328 /* Select a new "search" modulation mode to try.
2329 * If one is found, set up the new "search" table. */
Zhu Yib481de92007-09-25 17:54:57 -07002330 if (is_legacy(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08002331 rs_move_legacy_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07002332 else if (is_siso(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08002333 rs_move_siso_to_other(priv, lq_sta, conf, sta, index);
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07002334 else if (is_mimo2(tbl->lq_type))
2335 rs_move_mimo2_to_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07002336 else
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07002337 rs_move_mimo3_to_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07002338
Ben Cahill77626352007-11-29 11:09:44 +08002339 /* If new "search" mode was selected, set up in uCode table */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002340 if (lq_sta->search_better_tbl) {
Ben Cahill77626352007-11-29 11:09:44 +08002341 /* Access the "search" table, clear its history. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002342 tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Zhu Yib481de92007-09-25 17:54:57 -07002343 for (i = 0; i < IWL_RATE_COUNT; i++)
2344 rs_rate_scale_clear_window(&(tbl->win[i]));
2345
Ben Cahill77626352007-11-29 11:09:44 +08002346 /* Use new "search" start rate */
Tomas Winklere7d326a2008-06-12 09:47:11 +08002347 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002348
Tomas Winklere1623442009-01-27 14:27:56 -08002349 IWL_DEBUG_RATE(priv, "Switch current mcs: %X index: %d\n",
Guy Cohen39e88502008-04-23 17:14:57 -07002350 tbl->current_rate, index);
Guy Cohen07bc28e2008-04-23 17:15:03 -07002351 rs_fill_link_cmd(priv, lq_sta, tbl->current_rate);
Tomas Winkler66c73db2008-04-15 16:01:40 -07002352 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
Mohamed Abbas447fee72009-04-20 14:37:02 -07002353 } else
2354 done_search = 1;
2355 }
Zhu Yib481de92007-09-25 17:54:57 -07002356
Mohamed Abbas447fee72009-04-20 14:37:02 -07002357 if (done_search && !lq_sta->stay_in_tbl) {
Ben Cahill77626352007-11-29 11:09:44 +08002358 /* If the "active" (non-search) mode was legacy,
2359 * and we've tried switching antennas,
2360 * but we haven't been able to try HT modes (not available),
2361 * stay with best antenna legacy modulation for a while
2362 * before next round of mode comparisons. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002363 tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
Luis R. Rodriguezde27e642008-12-23 15:58:44 -08002364 if (is_legacy(tbl1->lq_type) && !conf_is_ht(conf) &&
Mohamed Abbasd6e93392009-05-08 13:44:39 -07002365 lq_sta->action_counter > tbl1->max_search) {
Tomas Winklere1623442009-01-27 14:27:56 -08002366 IWL_DEBUG_RATE(priv, "LQ: STAY in legacy table\n");
Ester Kummerbf403db2008-05-05 10:22:40 +08002367 rs_set_stay_in_table(priv, 1, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002368 }
2369
Ben Cahill77626352007-11-29 11:09:44 +08002370 /* If we're in an HT mode, and all 3 mode switch actions
2371 * have been tried and compared, stay in this best modulation
2372 * mode for a while before next round of mode comparisons. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002373 if (lq_sta->enable_counter &&
Mohamed Abbasd6e93392009-05-08 13:44:39 -07002374 (lq_sta->action_counter >= tbl1->max_search)) {
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002375 if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
2376 (lq_sta->tx_agg_tid_en & (1 << tid)) &&
2377 (tid != MAX_TID_COUNT)) {
Wey-Yi Guy86b47662009-03-10 14:35:09 -07002378 tid_data =
2379 &priv->stations[lq_sta->lq.sta_id].tid[tid];
2380 if (tid_data->agg.state == IWL_AGG_OFF) {
2381 IWL_DEBUG_RATE(priv,
2382 "try to aggregate tid %d\n",
2383 tid);
2384 rs_tl_turn_on_agg(priv, tid,
2385 lq_sta, sta);
2386 }
Zhu Yib481de92007-09-25 17:54:57 -07002387 }
Ester Kummerbf403db2008-05-05 10:22:40 +08002388 rs_set_stay_in_table(priv, 0, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002389 }
Zhu Yib481de92007-09-25 17:54:57 -07002390 }
2391
2392out:
Tomas Winkler978785a2008-12-19 10:37:31 +08002393 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
Zhu Yib481de92007-09-25 17:54:57 -07002394 i = index;
Johannes Bergb7e35002008-09-11 02:22:58 +02002395 lq_sta->last_txrate_idx = i;
Zhu Yib481de92007-09-25 17:54:57 -07002396
Zhu Yib481de92007-09-25 17:54:57 -07002397 return;
2398}
2399
2400
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002401static void rs_initialize_lq(struct iwl_priv *priv,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002402 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02002403 struct ieee80211_sta *sta,
2404 struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002405{
Tomas Winklere227cea2008-07-18 13:53:05 +08002406 struct iwl_scale_tbl_info *tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002407 int rate_idx;
Guy Cohenaade00c2008-04-23 17:14:59 -07002408 int i;
Guy Cohen39e88502008-04-23 17:14:57 -07002409 u32 rate;
Guy Cohenaade00c2008-04-23 17:14:59 -07002410 u8 use_green = rs_use_green(priv, conf);
2411 u8 active_tbl = 0;
2412 u8 valid_tx_ant;
Zhu Yib481de92007-09-25 17:54:57 -07002413
Johannes Berg4b7679a2008-09-18 18:14:18 +02002414 if (!sta || !lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002415 goto out;
2416
Johannes Bergb7e35002008-09-11 02:22:58 +02002417 i = lq_sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002418
Tomas Winklerc33104f2008-01-14 17:46:21 -08002419 if ((lq_sta->lq.sta_id == 0xff) &&
Johannes Berg05c914f2008-09-11 00:01:58 +02002420 (priv->iw_mode == NL80211_IFTYPE_ADHOC))
Zhu Yib481de92007-09-25 17:54:57 -07002421 goto out;
2422
Guy Cohenaade00c2008-04-23 17:14:59 -07002423 valid_tx_ant = priv->hw_params.valid_tx_ant;
2424
Tomas Winklerc33104f2008-01-14 17:46:21 -08002425 if (!lq_sta->search_better_tbl)
2426 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002427 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002428 active_tbl = 1 - lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002429
Tomas Winklerc33104f2008-01-14 17:46:21 -08002430 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07002431
2432 if ((i < 0) || (i >= IWL_RATE_COUNT))
2433 i = 0;
2434
Tomas Winkler1826dcc2008-05-15 13:54:02 +08002435 rate = iwl_rates[i].plcp;
Winkler, Tomaseb48dca2008-10-29 14:05:48 -07002436 tbl->ant_type = first_antenna(valid_tx_ant);
2437 rate |= tbl->ant_type << RATE_MCS_ANT_POS;
Zhu Yib481de92007-09-25 17:54:57 -07002438
2439 if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
Guy Cohen39e88502008-04-23 17:14:57 -07002440 rate |= RATE_MCS_CCK_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002441
Guy Cohen39e88502008-04-23 17:14:57 -07002442 rs_get_tbl_info_from_mcs(rate, priv->band, tbl, &rate_idx);
Guy Cohenaade00c2008-04-23 17:14:59 -07002443 if (!rs_is_valid_ant(valid_tx_ant, tbl->ant_type))
2444 rs_toggle_antenna(valid_tx_ant, &rate, tbl);
Zhu Yib481de92007-09-25 17:54:57 -07002445
Tomas Winkler978785a2008-12-19 10:37:31 +08002446 rate = rate_n_flags_from_tbl(priv, tbl, rate_idx, use_green);
Guy Cohen39e88502008-04-23 17:14:57 -07002447 tbl->current_rate = rate;
Guy Coheneecd6e52008-04-23 17:14:58 -07002448 rs_set_expected_tpt_table(lq_sta, tbl);
Guy Cohen07bc28e2008-04-23 17:15:03 -07002449 rs_fill_link_cmd(NULL, lq_sta, rate);
Tomas Winkler66c73db2008-04-15 16:01:40 -07002450 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07002451 out:
2452 return;
2453}
2454
Johannes Berge6a98542008-10-21 12:40:02 +02002455static void rs_get_rate(void *priv_r, struct ieee80211_sta *sta, void *priv_sta,
2456 struct ieee80211_tx_rate_control *txrc)
Zhu Yib481de92007-09-25 17:54:57 -07002457{
2458
Johannes Berge6a98542008-10-21 12:40:02 +02002459 struct sk_buff *skb = txrc->skb;
2460 struct ieee80211_supported_band *sband = txrc->sband;
Johannes Berg4b7679a2008-09-18 18:14:18 +02002461 struct iwl_priv *priv = (struct iwl_priv *)priv_r;
2462 struct ieee80211_conf *conf = &priv->hw->conf;
Zhu Yib481de92007-09-25 17:54:57 -07002463 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berge6a98542008-10-21 12:40:02 +02002464 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Tomas Winkler417f1142008-11-12 13:14:06 -08002465 struct iwl_lq_sta *lq_sta = priv_sta;
2466 int rate_idx;
Abbas, Mohamedc338ba32009-01-21 10:58:02 -08002467 u64 mask_bit = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002468
Tomas Winklere1623442009-01-27 14:27:56 -08002469 IWL_DEBUG_RATE_LIMIT(priv, "rate scale calculate new rate for skb\n");
Zhu Yib481de92007-09-25 17:54:57 -07002470
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002471 /* Get max rate if user set max rate */
2472 if (lq_sta) {
2473 lq_sta->max_rate_idx = txrc->max_rate_idx;
2474 if ((sband->band == IEEE80211_BAND_5GHZ) &&
2475 (lq_sta->max_rate_idx != -1))
2476 lq_sta->max_rate_idx += IWL_FIRST_OFDM_RATE;
2477 if ((lq_sta->max_rate_idx < 0) ||
2478 (lq_sta->max_rate_idx >= IWL_RATE_COUNT))
2479 lq_sta->max_rate_idx = -1;
2480 }
2481
Abbas, Mohamedc338ba32009-01-21 10:58:02 -08002482 if (sta)
2483 mask_bit = sta->supp_rates[sband->band];
2484
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002485 /* Send management frames and NO_ACK data using lowest rate. */
Tomas Winkler417f1142008-11-12 13:14:06 -08002486 if (!ieee80211_is_data(hdr->frame_control) ||
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002487 info->flags & IEEE80211_TX_CTL_NO_ACK || !sta || !lq_sta) {
Abbas, Mohamedc338ba32009-01-21 10:58:02 -08002488 if (!mask_bit)
2489 info->control.rates[0].idx =
2490 rate_lowest_index(sband, NULL);
2491 else
2492 info->control.rates[0].idx =
2493 rate_lowest_index(sband, sta);
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002494 if (info->flags & IEEE80211_TX_CTL_NO_ACK)
2495 info->control.rates[0].count = 1;
Johannes Berg4b7679a2008-09-18 18:14:18 +02002496 return;
Zhu Yib481de92007-09-25 17:54:57 -07002497 }
2498
Tomas Winkler417f1142008-11-12 13:14:06 -08002499 rate_idx = lq_sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002500
Johannes Berg05c914f2008-09-11 00:01:58 +02002501 if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) &&
Tomas Winklerc33104f2008-01-14 17:46:21 -08002502 !lq_sta->ibss_sta_added) {
Abhijeet Kolekare11bc022009-04-08 11:26:41 -07002503 u8 sta_id = priv->cfg->ops->smgmt->find_station(priv,
2504 hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002505
2506 if (sta_id == IWL_INVALID_STATION) {
Tomas Winklere1623442009-01-27 14:27:56 -08002507 IWL_DEBUG_RATE(priv, "LQ: ADD station %pM\n",
Johannes Berge1749612008-10-27 15:59:26 -07002508 hdr->addr1);
Abhijeet Kolekar06fd3d82009-04-08 11:26:42 -07002509 sta_id = priv->cfg->ops->smgmt->add_station(priv,
Abhijeet Kolekare11bc022009-04-08 11:26:41 -07002510 hdr->addr1, 0,
2511 CMD_ASYNC, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07002512 }
2513 if ((sta_id != IWL_INVALID_STATION)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002514 lq_sta->lq.sta_id = sta_id;
2515 lq_sta->lq.rs_table[0].rate_n_flags = 0;
2516 lq_sta->ibss_sta_added = 1;
Johannes Berg4b7679a2008-09-18 18:14:18 +02002517 rs_initialize_lq(priv, conf, sta, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002518 }
Zhu Yib481de92007-09-25 17:54:57 -07002519 }
2520
Tomas Winkler417f1142008-11-12 13:14:06 -08002521 if (rate_idx < 0 || rate_idx > IWL_RATE_COUNT)
2522 rate_idx = rate_lowest_index(sband, sta);
2523 else if (sband->band == IEEE80211_BAND_5GHZ)
2524 rate_idx -= IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -07002525
Tomas Winkler417f1142008-11-12 13:14:06 -08002526 info->control.rates[0].idx = rate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002527}
2528
Johannes Berg4b7679a2008-09-18 18:14:18 +02002529static void *rs_alloc_sta(void *priv_rate, struct ieee80211_sta *sta,
2530 gfp_t gfp)
Zhu Yib481de92007-09-25 17:54:57 -07002531{
Tomas Winklere227cea2008-07-18 13:53:05 +08002532 struct iwl_lq_sta *lq_sta;
Ester Kummerbf403db2008-05-05 10:22:40 +08002533 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07002534 int i, j;
2535
Ester Kummerbf403db2008-05-05 10:22:40 +08002536 priv = (struct iwl_priv *)priv_rate;
Tomas Winklere1623442009-01-27 14:27:56 -08002537 IWL_DEBUG_RATE(priv, "create station rate scale window\n");
Zhu Yib481de92007-09-25 17:54:57 -07002538
Tomas Winklere227cea2008-07-18 13:53:05 +08002539 lq_sta = kzalloc(sizeof(struct iwl_lq_sta), gfp);
Zhu Yib481de92007-09-25 17:54:57 -07002540
Tomas Winklerc33104f2008-01-14 17:46:21 -08002541 if (lq_sta == NULL)
Zhu Yib481de92007-09-25 17:54:57 -07002542 return NULL;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002543 lq_sta->lq.sta_id = 0xff;
Zhu Yib481de92007-09-25 17:54:57 -07002544
Zhu Yi63fddb92007-09-27 11:27:36 +08002545
Zhu Yib481de92007-09-25 17:54:57 -07002546 for (j = 0; j < LQ_SIZE; j++)
2547 for (i = 0; i < IWL_RATE_COUNT; i++)
Tomas Winkler3ac7f142008-07-21 02:40:14 +03002548 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
Zhu Yib481de92007-09-25 17:54:57 -07002549
Tomas Winklerc33104f2008-01-14 17:46:21 -08002550 return lq_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002551}
2552
Johannes Berg4b7679a2008-09-18 18:14:18 +02002553static void rs_rate_init(void *priv_r, struct ieee80211_supported_band *sband,
2554 struct ieee80211_sta *sta, void *priv_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002555{
2556 int i, j;
Johannes Berg4b7679a2008-09-18 18:14:18 +02002557 struct iwl_priv *priv = (struct iwl_priv *)priv_r;
2558 struct ieee80211_conf *conf = &priv->hw->conf;
Tomas Winklere227cea2008-07-18 13:53:05 +08002559 struct iwl_lq_sta *lq_sta = priv_sta;
Mohamed Abbasd5e49032008-12-12 08:22:15 -08002560 u16 mask_bit = 0;
Abbas, Mohamed7d049e52009-01-20 21:33:53 -08002561 int count;
2562 int start_rate = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002563
Tomas Winklerc33104f2008-01-14 17:46:21 -08002564 lq_sta->flush_timer = 0;
Johannes Berg4b7679a2008-09-18 18:14:18 +02002565 lq_sta->supp_rates = sta->supp_rates[sband->band];
Zhu Yib481de92007-09-25 17:54:57 -07002566 for (j = 0; j < LQ_SIZE; j++)
2567 for (i = 0; i < IWL_RATE_COUNT; i++)
Tomas Winkler3ac7f142008-07-21 02:40:14 +03002568 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
Zhu Yib481de92007-09-25 17:54:57 -07002569
Tomas Winklere1623442009-01-27 14:27:56 -08002570 IWL_DEBUG_RATE(priv, "LQ: *** rate scale station global init ***\n");
Zhu Yib481de92007-09-25 17:54:57 -07002571 /* TODO: what is a good starting rate for STA? About middle? Maybe not
2572 * the lowest or the highest rate.. Could consider using RSSI from
2573 * previous packets? Need to have IEEE 802.1X auth succeed immediately
2574 * after assoc.. */
2575
Tomas Winklerc33104f2008-01-14 17:46:21 -08002576 lq_sta->ibss_sta_added = 0;
Johannes Berg05c914f2008-09-11 00:01:58 +02002577 if (priv->iw_mode == NL80211_IFTYPE_AP) {
Abhijeet Kolekare11bc022009-04-08 11:26:41 -07002578 u8 sta_id = priv->cfg->ops->smgmt->find_station(priv,
2579 sta->addr);
Joe Perches0795af52007-10-03 17:59:30 -07002580
Zhu Yib481de92007-09-25 17:54:57 -07002581 /* for IBSS the call are from tasklet */
Tomas Winklere1623442009-01-27 14:27:56 -08002582 IWL_DEBUG_RATE(priv, "LQ: ADD station %pM\n", sta->addr);
Zhu Yib481de92007-09-25 17:54:57 -07002583
2584 if (sta_id == IWL_INVALID_STATION) {
Tomas Winklere1623442009-01-27 14:27:56 -08002585 IWL_DEBUG_RATE(priv, "LQ: ADD station %pM\n", sta->addr);
Abhijeet Kolekar06fd3d82009-04-08 11:26:42 -07002586 sta_id = priv->cfg->ops->smgmt->add_station(priv,
Abhijeet Kolekare11bc022009-04-08 11:26:41 -07002587 sta->addr, 0,
2588 CMD_ASYNC, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07002589 }
2590 if ((sta_id != IWL_INVALID_STATION)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002591 lq_sta->lq.sta_id = sta_id;
2592 lq_sta->lq.rs_table[0].rate_n_flags = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002593 }
2594 /* FIXME: this is w/a remove it later */
2595 priv->assoc_station_added = 1;
2596 }
2597
Tomas Winklerc33104f2008-01-14 17:46:21 -08002598 lq_sta->is_dup = 0;
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002599 lq_sta->max_rate_idx = -1;
Abbas, Mohamed7d049e52009-01-20 21:33:53 -08002600 lq_sta->missed_rate_counter = IWL_MISSED_RATE_MAX;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002601 lq_sta->is_green = rs_use_green(priv, conf);
Guy Cohen39e88502008-04-23 17:14:57 -07002602 lq_sta->active_legacy_rate = priv->active_rate & ~(0x1000);
Tomas Winklerc33104f2008-01-14 17:46:21 -08002603 lq_sta->active_rate_basic = priv->active_rate_basic;
Johannes Berg8318d782008-01-24 19:38:38 +01002604 lq_sta->band = priv->band;
Ben Cahill77626352007-11-29 11:09:44 +08002605 /*
2606 * active_siso_rate mask includes 9 MBits (bit 5), and CCK (bits 0-3),
2607 * supp_rates[] does not; shift to convert format, force 9 MBits off.
2608 */
Johannes Bergae5eb022008-10-14 16:58:37 +02002609 lq_sta->active_siso_rate = sta->ht_cap.mcs.rx_mask[0] << 1;
2610 lq_sta->active_siso_rate |= sta->ht_cap.mcs.rx_mask[0] & 0x1;
Guy Cohenfde0db32008-04-21 15:42:01 -07002611 lq_sta->active_siso_rate &= ~((u16)0x2);
2612 lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -07002613
Ben Cahill77626352007-11-29 11:09:44 +08002614 /* Same here */
Johannes Bergae5eb022008-10-14 16:58:37 +02002615 lq_sta->active_mimo2_rate = sta->ht_cap.mcs.rx_mask[1] << 1;
2616 lq_sta->active_mimo2_rate |= sta->ht_cap.mcs.rx_mask[1] & 0x1;
Guy Cohenfde0db32008-04-21 15:42:01 -07002617 lq_sta->active_mimo2_rate &= ~((u16)0x2);
2618 lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE;
2619
Johannes Bergae5eb022008-10-14 16:58:37 +02002620 lq_sta->active_mimo3_rate = sta->ht_cap.mcs.rx_mask[2] << 1;
2621 lq_sta->active_mimo3_rate |= sta->ht_cap.mcs.rx_mask[2] & 0x1;
Guy Cohenfde0db32008-04-21 15:42:01 -07002622 lq_sta->active_mimo3_rate &= ~((u16)0x2);
2623 lq_sta->active_mimo3_rate <<= IWL_FIRST_OFDM_RATE;
2624
Tomas Winklere1623442009-01-27 14:27:56 -08002625 IWL_DEBUG_RATE(priv, "SISO-RATE=%X MIMO2-RATE=%X MIMO3-RATE=%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002626 lq_sta->active_siso_rate,
Guy Cohenfde0db32008-04-21 15:42:01 -07002627 lq_sta->active_mimo2_rate,
2628 lq_sta->active_mimo3_rate);
2629
Tomas Winklera96a27f2008-10-23 23:48:56 -07002630 /* These values will be overridden later */
Guy Cohen07bc28e2008-04-23 17:15:03 -07002631 lq_sta->lq.general_params.single_stream_ant_msk = ANT_A;
2632 lq_sta->lq.general_params.dual_stream_ant_msk = ANT_AB;
2633
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002634 /* as default allow aggregation for all tids */
2635 lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002636 lq_sta->drv = priv;
Zhu Yib481de92007-09-25 17:54:57 -07002637
Mohamed Abbasd5e49032008-12-12 08:22:15 -08002638 /* Find highest tx rate supported by hardware and destination station */
Abbas, Mohamed7d049e52009-01-20 21:33:53 -08002639 mask_bit = sta->supp_rates[sband->band];
2640 count = sband->n_bitrates;
2641 if (sband->band == IEEE80211_BAND_5GHZ) {
2642 count += IWL_FIRST_OFDM_RATE;
2643 start_rate = IWL_FIRST_OFDM_RATE;
2644 mask_bit <<= IWL_FIRST_OFDM_RATE;
2645 }
2646
2647 mask_bit = mask_bit & lq_sta->active_legacy_rate;
2648 lq_sta->last_txrate_idx = 4;
2649 for (i = start_rate; i < count; i++)
Mohamed Abbasd5e49032008-12-12 08:22:15 -08002650 if (mask_bit & BIT(i))
2651 lq_sta->last_txrate_idx = i;
2652
Johannes Berg4b7679a2008-09-18 18:14:18 +02002653 rs_initialize_lq(priv, conf, sta, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002654}
2655
Guy Cohenfde0db32008-04-21 15:42:01 -07002656static void rs_fill_link_cmd(const struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08002657 struct iwl_lq_sta *lq_sta, u32 new_rate)
Zhu Yib481de92007-09-25 17:54:57 -07002658{
Tomas Winklere227cea2008-07-18 13:53:05 +08002659 struct iwl_scale_tbl_info tbl_type;
Zhu Yib481de92007-09-25 17:54:57 -07002660 int index = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002661 int rate_idx;
Zhu Yi1b696de2007-09-27 11:27:41 +08002662 int repeat_rate = 0;
Guy Cohenaade00c2008-04-23 17:14:59 -07002663 u8 ant_toggle_cnt = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002664 u8 use_ht_possible = 1;
Guy Cohenaade00c2008-04-23 17:14:59 -07002665 u8 valid_tx_ant = 0;
Guy Cohen07bc28e2008-04-23 17:15:03 -07002666 struct iwl_link_quality_cmd *lq_cmd = &lq_sta->lq;
Zhu Yib481de92007-09-25 17:54:57 -07002667
Ben Cahill77626352007-11-29 11:09:44 +08002668 /* Override starting rate (index 0) if needed for debug purposes */
Guy Cohen39e88502008-04-23 17:14:57 -07002669 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Zhu Yi98d7e092007-09-27 11:27:42 +08002670
Guy Cohen39e88502008-04-23 17:14:57 -07002671 /* Interpret new_rate (rate_n_flags) */
Guy Cohenaade00c2008-04-23 17:14:59 -07002672 memset(&tbl_type, 0, sizeof(tbl_type));
Guy Cohen39e88502008-04-23 17:14:57 -07002673 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band,
Zhu Yib481de92007-09-25 17:54:57 -07002674 &tbl_type, &rate_idx);
2675
Ben Cahill77626352007-11-29 11:09:44 +08002676 /* How many times should we repeat the initial rate? */
Zhu Yib481de92007-09-25 17:54:57 -07002677 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07002678 ant_toggle_cnt = 1;
Zhu Yi1b696de2007-09-27 11:27:41 +08002679 repeat_rate = IWL_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002680 } else {
Zhu Yi1b696de2007-09-27 11:27:41 +08002681 repeat_rate = IWL_HT_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002682 }
Zhu Yib481de92007-09-25 17:54:57 -07002683
2684 lq_cmd->general_params.mimo_delimiter =
2685 is_mimo(tbl_type.lq_type) ? 1 : 0;
Ben Cahill77626352007-11-29 11:09:44 +08002686
2687 /* Fill 1st table entry (index 0) */
Guy Cohen39e88502008-04-23 17:14:57 -07002688 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002689
Guy Cohen07bc28e2008-04-23 17:15:03 -07002690 if (num_of_ant(tbl_type.ant_type) == 1) {
2691 lq_cmd->general_params.single_stream_ant_msk =
2692 tbl_type.ant_type;
2693 } else if (num_of_ant(tbl_type.ant_type) == 2) {
2694 lq_cmd->general_params.dual_stream_ant_msk =
2695 tbl_type.ant_type;
2696 } /* otherwise we don't modify the existing value */
Zhu Yib481de92007-09-25 17:54:57 -07002697
2698 index++;
Zhu Yi1b696de2007-09-27 11:27:41 +08002699 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002700
Guy Cohenaade00c2008-04-23 17:14:59 -07002701 if (priv)
2702 valid_tx_ant = priv->hw_params.valid_tx_ant;
2703
Ben Cahill77626352007-11-29 11:09:44 +08002704 /* Fill rest of rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002705 while (index < LINK_QUAL_MAX_RETRY_NUM) {
Ben Cahill77626352007-11-29 11:09:44 +08002706 /* Repeat initial/next rate.
2707 * For legacy IWL_NUMBER_TRY == 1, this loop will not execute.
2708 * For HT IWL_HT_NUMBER_TRY == 3, this executes twice. */
Zhu Yi1b696de2007-09-27 11:27:41 +08002709 while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
Zhu Yib481de92007-09-25 17:54:57 -07002710 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07002711 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2712 ant_toggle_cnt++;
2713 else if (priv &&
2714 rs_toggle_antenna(valid_tx_ant,
2715 &new_rate, &tbl_type))
2716 ant_toggle_cnt = 1;
2717}
Zhu Yi98d7e092007-09-27 11:27:42 +08002718
Ben Cahill77626352007-11-29 11:09:44 +08002719 /* Override next rate if needed for debug purposes */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002720 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Ben Cahill77626352007-11-29 11:09:44 +08002721
2722 /* Fill next table entry */
Zhu Yib481de92007-09-25 17:54:57 -07002723 lq_cmd->rs_table[index].rate_n_flags =
Guy Cohen39e88502008-04-23 17:14:57 -07002724 cpu_to_le32(new_rate);
Zhu Yi1b696de2007-09-27 11:27:41 +08002725 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002726 index++;
2727 }
2728
Guy Cohen39e88502008-04-23 17:14:57 -07002729 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band, &tbl_type,
Zhu Yib481de92007-09-25 17:54:57 -07002730 &rate_idx);
2731
Ben Cahill77626352007-11-29 11:09:44 +08002732 /* Indicate to uCode which entries might be MIMO.
2733 * If initial rate was MIMO, this will finally end up
2734 * as (IWL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
Zhu Yib481de92007-09-25 17:54:57 -07002735 if (is_mimo(tbl_type.lq_type))
2736 lq_cmd->general_params.mimo_delimiter = index;
2737
Ben Cahill77626352007-11-29 11:09:44 +08002738 /* Get next rate */
Guy Cohen39e88502008-04-23 17:14:57 -07002739 new_rate = rs_get_lower_rate(lq_sta, &tbl_type, rate_idx,
2740 use_ht_possible);
Zhu Yib481de92007-09-25 17:54:57 -07002741
Ben Cahill77626352007-11-29 11:09:44 +08002742 /* How many times should we repeat the next rate? */
Zhu Yib481de92007-09-25 17:54:57 -07002743 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07002744 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2745 ant_toggle_cnt++;
2746 else if (priv &&
2747 rs_toggle_antenna(valid_tx_ant,
2748 &new_rate, &tbl_type))
2749 ant_toggle_cnt = 1;
2750
Zhu Yi1b696de2007-09-27 11:27:41 +08002751 repeat_rate = IWL_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002752 } else {
Zhu Yi1b696de2007-09-27 11:27:41 +08002753 repeat_rate = IWL_HT_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002754 }
Zhu Yib481de92007-09-25 17:54:57 -07002755
Ben Cahill77626352007-11-29 11:09:44 +08002756 /* Don't allow HT rates after next pass.
2757 * rs_get_lower_rate() will change type to LQ_A or LQ_G. */
Zhu Yib481de92007-09-25 17:54:57 -07002758 use_ht_possible = 0;
2759
Ben Cahill77626352007-11-29 11:09:44 +08002760 /* Override next rate if needed for debug purposes */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002761 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Ben Cahill77626352007-11-29 11:09:44 +08002762
2763 /* Fill next table entry */
Guy Cohen39e88502008-04-23 17:14:57 -07002764 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002765
2766 index++;
Zhu Yi1b696de2007-09-27 11:27:41 +08002767 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002768 }
2769
Tomas Winkler5e1dd8d2008-05-29 16:35:17 +08002770 lq_cmd->agg_params.agg_frame_cnt_limit = 64;
Zhu Yib481de92007-09-25 17:54:57 -07002771 lq_cmd->agg_params.agg_dis_start_th = 3;
2772 lq_cmd->agg_params.agg_time_limit = cpu_to_le16(4000);
Zhu Yib481de92007-09-25 17:54:57 -07002773}
2774
Johannes Berg4b7679a2008-09-18 18:14:18 +02002775static void *rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
Zhu Yib481de92007-09-25 17:54:57 -07002776{
Johannes Berg4b7679a2008-09-18 18:14:18 +02002777 return hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07002778}
2779/* rate scale requires free function to be implemented */
2780static void rs_free(void *priv_rate)
2781{
2782 return;
2783}
2784
Johannes Berg4b7679a2008-09-18 18:14:18 +02002785static void rs_free_sta(void *priv_r, struct ieee80211_sta *sta,
2786 void *priv_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002787{
Tomas Winklere227cea2008-07-18 13:53:05 +08002788 struct iwl_lq_sta *lq_sta = priv_sta;
Rami Rosen45527c22008-10-07 09:50:01 +02002789 struct iwl_priv *priv __maybe_unused = priv_r;
Zhu Yib481de92007-09-25 17:54:57 -07002790
Tomas Winklere1623442009-01-27 14:27:56 -08002791 IWL_DEBUG_RATE(priv, "enter\n");
Tomas Winklerc33104f2008-01-14 17:46:21 -08002792 kfree(lq_sta);
Tomas Winklere1623442009-01-27 14:27:56 -08002793 IWL_DEBUG_RATE(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07002794}
2795
2796
Zhu Yi93dc6462007-09-27 11:27:37 +08002797#ifdef CONFIG_MAC80211_DEBUGFS
Zhu Yi5ae212c2007-09-27 11:27:38 +08002798static int open_file_generic(struct inode *inode, struct file *file)
2799{
2800 file->private_data = inode->i_private;
2801 return 0;
2802}
Tomas Winklere227cea2008-07-18 13:53:05 +08002803static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
2804 u32 *rate_n_flags, int index)
Zhu Yi98d7e092007-09-27 11:27:42 +08002805{
Ester Kummerbf403db2008-05-05 10:22:40 +08002806 struct iwl_priv *priv;
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07002807 u8 valid_tx_ant;
2808 u8 ant_sel_tx;
Ester Kummerbf403db2008-05-05 10:22:40 +08002809
2810 priv = lq_sta->drv;
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07002811 valid_tx_ant = priv->hw_params.valid_tx_ant;
Guy Cohen39e88502008-04-23 17:14:57 -07002812 if (lq_sta->dbg_fixed_rate) {
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07002813 ant_sel_tx =
2814 ((lq_sta->dbg_fixed_rate & RATE_MCS_ANT_ABC_MSK)
2815 >> RATE_MCS_ANT_POS);
2816 if ((valid_tx_ant & ant_sel_tx) == ant_sel_tx) {
Guy Cohen39e88502008-04-23 17:14:57 -07002817 *rate_n_flags = lq_sta->dbg_fixed_rate;
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07002818 IWL_DEBUG_RATE(priv, "Fixed rate ON\n");
Guy Cohen39e88502008-04-23 17:14:57 -07002819 } else {
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07002820 lq_sta->dbg_fixed_rate = 0;
2821 IWL_ERR(priv,
2822 "Invalid antenna selection 0x%X, Valid is 0x%X\n",
2823 ant_sel_tx, valid_tx_ant);
2824 IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
Guy Cohen39e88502008-04-23 17:14:57 -07002825 }
Guy Cohen39e88502008-04-23 17:14:57 -07002826 } else {
Tomas Winklere1623442009-01-27 14:27:56 -08002827 IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
Zhu Yi98d7e092007-09-27 11:27:42 +08002828 }
Zhu Yi98d7e092007-09-27 11:27:42 +08002829}
2830
2831static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
2832 const char __user *user_buf, size_t count, loff_t *ppos)
2833{
Tomas Winklere227cea2008-07-18 13:53:05 +08002834 struct iwl_lq_sta *lq_sta = file->private_data;
Ester Kummerbf403db2008-05-05 10:22:40 +08002835 struct iwl_priv *priv;
Zhu Yi98d7e092007-09-27 11:27:42 +08002836 char buf[64];
2837 int buf_size;
2838 u32 parsed_rate;
2839
Ester Kummerbf403db2008-05-05 10:22:40 +08002840 priv = lq_sta->drv;
Zhu Yi98d7e092007-09-27 11:27:42 +08002841 memset(buf, 0, sizeof(buf));
2842 buf_size = min(count, sizeof(buf) - 1);
2843 if (copy_from_user(buf, user_buf, buf_size))
2844 return -EFAULT;
2845
2846 if (sscanf(buf, "%x", &parsed_rate) == 1)
Guy Cohen39e88502008-04-23 17:14:57 -07002847 lq_sta->dbg_fixed_rate = parsed_rate;
Zhu Yi98d7e092007-09-27 11:27:42 +08002848 else
Guy Cohen39e88502008-04-23 17:14:57 -07002849 lq_sta->dbg_fixed_rate = 0;
Zhu Yi98d7e092007-09-27 11:27:42 +08002850
Guy Cohen39e88502008-04-23 17:14:57 -07002851 lq_sta->active_legacy_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
2852 lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2853 lq_sta->active_mimo2_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2854 lq_sta->active_mimo3_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
Zhu Yi98d7e092007-09-27 11:27:42 +08002855
Tomas Winklere1623442009-01-27 14:27:56 -08002856 IWL_DEBUG_RATE(priv, "sta_id %d rate 0x%X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07002857 lq_sta->lq.sta_id, lq_sta->dbg_fixed_rate);
Zhu Yi98d7e092007-09-27 11:27:42 +08002858
Guy Cohen39e88502008-04-23 17:14:57 -07002859 if (lq_sta->dbg_fixed_rate) {
Guy Cohen07bc28e2008-04-23 17:15:03 -07002860 rs_fill_link_cmd(NULL, lq_sta, lq_sta->dbg_fixed_rate);
Tomas Winkler66c73db2008-04-15 16:01:40 -07002861 iwl_send_lq_cmd(lq_sta->drv, &lq_sta->lq, CMD_ASYNC);
Zhu Yi98d7e092007-09-27 11:27:42 +08002862 }
2863
2864 return count;
2865}
Zhu Yi0209dc12007-09-27 11:27:43 +08002866
Zhu Yi5ae212c2007-09-27 11:27:38 +08002867static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
2868 char __user *user_buf, size_t count, loff_t *ppos)
2869{
Frank Seidela412c802009-03-01 20:25:38 +01002870 char *buff;
Zhu Yi5ae212c2007-09-27 11:27:38 +08002871 int desc = 0;
2872 int i = 0;
Wey-Yi Guy12b96812009-04-08 11:39:27 -07002873 int index = 0;
Frank Seidela412c802009-03-01 20:25:38 +01002874 ssize_t ret;
Zhu Yi5ae212c2007-09-27 11:27:38 +08002875
Tomas Winklere227cea2008-07-18 13:53:05 +08002876 struct iwl_lq_sta *lq_sta = file->private_data;
Wey-Yi Guyd8ae4f52009-03-10 14:35:07 -07002877 struct iwl_priv *priv;
Wey-Yi Guyadb7b5e2009-03-10 14:35:08 -07002878 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002879
Wey-Yi Guyd8ae4f52009-03-10 14:35:07 -07002880 priv = lq_sta->drv;
Frank Seidela412c802009-03-01 20:25:38 +01002881 buff = kmalloc(1024, GFP_KERNEL);
2882 if (!buff)
2883 return -ENOMEM;
2884
Tomas Winklerc33104f2008-01-14 17:46:21 -08002885 desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
Zhu Yi98d7e092007-09-27 11:27:42 +08002886 desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002887 lq_sta->total_failed, lq_sta->total_success,
Guy Cohen39e88502008-04-23 17:14:57 -07002888 lq_sta->active_legacy_rate);
Zhu Yi98d7e092007-09-27 11:27:42 +08002889 desc += sprintf(buff+desc, "fixed rate 0x%X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07002890 lq_sta->dbg_fixed_rate);
Wey-Yi Guyd8ae4f52009-03-10 14:35:07 -07002891 desc += sprintf(buff+desc, "valid_tx_ant %s%s%s\n",
2892 (priv->hw_params.valid_tx_ant & ANT_A) ? "ANT_A," : "",
2893 (priv->hw_params.valid_tx_ant & ANT_B) ? "ANT_B," : "",
2894 (priv->hw_params.valid_tx_ant & ANT_C) ? "ANT_C" : "");
Wey-Yi Guyadb7b5e2009-03-10 14:35:08 -07002895 desc += sprintf(buff+desc, "lq type %s\n",
2896 (is_legacy(tbl->lq_type)) ? "legacy" : "HT");
2897 if (is_Ht(tbl->lq_type)) {
2898 desc += sprintf(buff+desc, " %s",
2899 (is_siso(tbl->lq_type)) ? "SISO" :
2900 ((is_mimo2(tbl->lq_type)) ? "MIMO2" : "MIMO3"));
2901 desc += sprintf(buff+desc, " %s",
2902 (tbl->is_fat) ? "40MHz" : "20MHz");
Wey-Yi Guy2681b202009-05-21 13:44:22 -07002903 desc += sprintf(buff+desc, " %s %s\n", (tbl->is_SGI) ? "SGI" : "",
2904 (lq_sta->is_green) ? "GF enabled" : "");
Wey-Yi Guyadb7b5e2009-03-10 14:35:08 -07002905 }
Wey-Yi Guy12b96812009-04-08 11:39:27 -07002906 desc += sprintf(buff+desc, "last tx rate=0x%X\n",
2907 lq_sta->last_rate_n_flags);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002908 desc += sprintf(buff+desc, "general:"
2909 "flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002910 lq_sta->lq.general_params.flags,
2911 lq_sta->lq.general_params.mimo_delimiter,
2912 lq_sta->lq.general_params.single_stream_ant_msk,
2913 lq_sta->lq.general_params.dual_stream_ant_msk);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002914
2915 desc += sprintf(buff+desc, "agg:"
2916 "time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002917 le16_to_cpu(lq_sta->lq.agg_params.agg_time_limit),
2918 lq_sta->lq.agg_params.agg_dis_start_th,
2919 lq_sta->lq.agg_params.agg_frame_cnt_limit);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002920
2921 desc += sprintf(buff+desc,
2922 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002923 lq_sta->lq.general_params.start_rate_index[0],
2924 lq_sta->lq.general_params.start_rate_index[1],
2925 lq_sta->lq.general_params.start_rate_index[2],
2926 lq_sta->lq.general_params.start_rate_index[3]);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002927
Wey-Yi Guy12b96812009-04-08 11:39:27 -07002928 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
2929 index = iwl_hwrate_to_plcp_idx(
2930 le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags));
2931 if (is_legacy(tbl->lq_type)) {
2932 desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps\n",
2933 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags),
2934 iwl_rate_mcs[index].mbps);
2935 } else {
2936 desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps (%s)\n",
2937 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags),
2938 iwl_rate_mcs[index].mbps, iwl_rate_mcs[index].mcs);
2939 }
2940 }
Zhu Yi5ae212c2007-09-27 11:27:38 +08002941
Frank Seidela412c802009-03-01 20:25:38 +01002942 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2943 kfree(buff);
2944 return ret;
Zhu Yi5ae212c2007-09-27 11:27:38 +08002945}
2946
2947static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
Zhu Yi98d7e092007-09-27 11:27:42 +08002948 .write = rs_sta_dbgfs_scale_table_write,
Zhu Yi5ae212c2007-09-27 11:27:38 +08002949 .read = rs_sta_dbgfs_scale_table_read,
2950 .open = open_file_generic,
2951};
Zhu Yi0209dc12007-09-27 11:27:43 +08002952static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
2953 char __user *user_buf, size_t count, loff_t *ppos)
2954{
Frank Seidela412c802009-03-01 20:25:38 +01002955 char *buff;
Zhu Yi0209dc12007-09-27 11:27:43 +08002956 int desc = 0;
2957 int i, j;
Frank Seidela412c802009-03-01 20:25:38 +01002958 ssize_t ret;
Zhu Yi0209dc12007-09-27 11:27:43 +08002959
Tomas Winklere227cea2008-07-18 13:53:05 +08002960 struct iwl_lq_sta *lq_sta = file->private_data;
Frank Seidela412c802009-03-01 20:25:38 +01002961
2962 buff = kmalloc(1024, GFP_KERNEL);
2963 if (!buff)
2964 return -ENOMEM;
2965
Zhu Yi0209dc12007-09-27 11:27:43 +08002966 for (i = 0; i < LQ_SIZE; i++) {
Wey-Yi Guy2681b202009-05-21 13:44:22 -07002967 desc += sprintf(buff+desc, "%s type=%d SGI=%d FAT=%d DUP=%d GF=%d\n"
Zhu Yi0209dc12007-09-27 11:27:43 +08002968 "rate=0x%X\n",
Abhijeet Kolekarc3056062008-11-12 13:14:08 -08002969 lq_sta->active_tbl == i ? "*" : "x",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002970 lq_sta->lq_info[i].lq_type,
2971 lq_sta->lq_info[i].is_SGI,
2972 lq_sta->lq_info[i].is_fat,
2973 lq_sta->lq_info[i].is_dup,
Wey-Yi Guy2681b202009-05-21 13:44:22 -07002974 lq_sta->is_green,
Guy Cohen39e88502008-04-23 17:14:57 -07002975 lq_sta->lq_info[i].current_rate);
Zhu Yi0209dc12007-09-27 11:27:43 +08002976 for (j = 0; j < IWL_RATE_COUNT; j++) {
2977 desc += sprintf(buff+desc,
Tomas Winklerc33104f2008-01-14 17:46:21 -08002978 "counter=%d success=%d %%=%d\n",
2979 lq_sta->lq_info[i].win[j].counter,
2980 lq_sta->lq_info[i].win[j].success_counter,
2981 lq_sta->lq_info[i].win[j].success_ratio);
Zhu Yi0209dc12007-09-27 11:27:43 +08002982 }
2983 }
Frank Seidela412c802009-03-01 20:25:38 +01002984 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2985 kfree(buff);
2986 return ret;
Zhu Yi0209dc12007-09-27 11:27:43 +08002987}
2988
2989static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
2990 .read = rs_sta_dbgfs_stats_table_read,
2991 .open = open_file_generic,
2992};
Zhu Yi5ae212c2007-09-27 11:27:38 +08002993
Wey-Yi Guy38167452009-05-08 13:44:43 -07002994static ssize_t rs_sta_dbgfs_rate_scale_data_read(struct file *file,
2995 char __user *user_buf, size_t count, loff_t *ppos)
2996{
2997 char buff[120];
2998 int desc = 0;
2999 ssize_t ret;
3000
3001 struct iwl_lq_sta *lq_sta = file->private_data;
3002 struct iwl_priv *priv;
3003 struct iwl_scale_tbl_info *tbl = &lq_sta->lq_info[lq_sta->active_tbl];
3004
3005 priv = lq_sta->drv;
3006
3007 if (is_Ht(tbl->lq_type))
3008 desc += sprintf(buff+desc,
3009 "Bit Rate= %d Mb/s\n",
3010 tbl->expected_tpt[lq_sta->last_txrate_idx]);
3011 else
3012 desc += sprintf(buff+desc,
3013 "Bit Rate= %d Mb/s\n",
3014 iwl_rates[lq_sta->last_txrate_idx].ieee >> 1);
3015 desc += sprintf(buff+desc,
3016 "Signal Level= %d dBm\tNoise Level= %d dBm\n",
3017 priv->last_rx_rssi, priv->last_rx_noise);
3018 desc += sprintf(buff+desc,
3019 "Tsf= 0x%llx\tBeacon time= 0x%08X\n",
3020 priv->last_tsf, priv->last_beacon_time);
3021
3022 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
3023 return ret;
3024}
3025
3026static const struct file_operations rs_sta_dbgfs_rate_scale_data_ops = {
3027 .read = rs_sta_dbgfs_rate_scale_data_read,
3028 .open = open_file_generic,
3029};
3030
Zhu Yi93dc6462007-09-27 11:27:37 +08003031static void rs_add_debugfs(void *priv, void *priv_sta,
3032 struct dentry *dir)
3033{
Tomas Winklere227cea2008-07-18 13:53:05 +08003034 struct iwl_lq_sta *lq_sta = priv_sta;
Tomas Winklerc33104f2008-01-14 17:46:21 -08003035 lq_sta->rs_sta_dbgfs_scale_table_file =
Zhu Yi0209dc12007-09-27 11:27:43 +08003036 debugfs_create_file("rate_scale_table", 0600, dir,
Tomas Winklerc33104f2008-01-14 17:46:21 -08003037 lq_sta, &rs_sta_dbgfs_scale_table_ops);
3038 lq_sta->rs_sta_dbgfs_stats_table_file =
Zhu Yi0209dc12007-09-27 11:27:43 +08003039 debugfs_create_file("rate_stats_table", 0600, dir,
Tomas Winklerc33104f2008-01-14 17:46:21 -08003040 lq_sta, &rs_sta_dbgfs_stats_table_ops);
Wey-Yi Guy38167452009-05-08 13:44:43 -07003041 lq_sta->rs_sta_dbgfs_rate_scale_data_file =
3042 debugfs_create_file("rate_scale_data", 0600, dir,
3043 lq_sta, &rs_sta_dbgfs_rate_scale_data_ops);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02003044 lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file =
3045 debugfs_create_u8("tx_agg_tid_enable", 0600, dir,
3046 &lq_sta->tx_agg_tid_en);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02003047
Zhu Yi93dc6462007-09-27 11:27:37 +08003048}
3049
3050static void rs_remove_debugfs(void *priv, void *priv_sta)
3051{
Tomas Winklere227cea2008-07-18 13:53:05 +08003052 struct iwl_lq_sta *lq_sta = priv_sta;
Tomas Winklerc33104f2008-01-14 17:46:21 -08003053 debugfs_remove(lq_sta->rs_sta_dbgfs_scale_table_file);
3054 debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
Wey-Yi Guy38167452009-05-08 13:44:43 -07003055 debugfs_remove(lq_sta->rs_sta_dbgfs_rate_scale_data_file);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02003056 debugfs_remove(lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file);
Zhu Yi93dc6462007-09-27 11:27:37 +08003057}
3058#endif
3059
Zhu Yib481de92007-09-25 17:54:57 -07003060static struct rate_control_ops rs_ops = {
3061 .module = NULL,
3062 .name = RS_NAME,
3063 .tx_status = rs_tx_status,
3064 .get_rate = rs_get_rate,
3065 .rate_init = rs_rate_init,
Zhu Yib481de92007-09-25 17:54:57 -07003066 .alloc = rs_alloc,
3067 .free = rs_free,
3068 .alloc_sta = rs_alloc_sta,
3069 .free_sta = rs_free_sta,
Zhu Yi93dc6462007-09-27 11:27:37 +08003070#ifdef CONFIG_MAC80211_DEBUGFS
3071 .add_sta_debugfs = rs_add_debugfs,
3072 .remove_sta_debugfs = rs_remove_debugfs,
3073#endif
Zhu Yib481de92007-09-25 17:54:57 -07003074};
3075
Tomas Winklere227cea2008-07-18 13:53:05 +08003076int iwlagn_rate_control_register(void)
Zhu Yib481de92007-09-25 17:54:57 -07003077{
Reinette Chatre897e1cf2008-03-28 16:21:09 -07003078 return ieee80211_rate_control_register(&rs_ops);
Zhu Yib481de92007-09-25 17:54:57 -07003079}
3080
Tomas Winklere227cea2008-07-18 13:53:05 +08003081void iwlagn_rate_control_unregister(void)
Zhu Yib481de92007-09-25 17:54:57 -07003082{
3083 ieee80211_rate_control_unregister(&rs_ops);
3084}
3085