blob: 52a4810274e91fe45fc98048794e12b62f4a0774 [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);
Wey-Yi Guy46f93812009-07-24 11:13:03 -0700180static void rs_fill_link_cmd(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
Wey-Yi Guya9c146b2009-05-22 11:01:48 -0700273#define MCS_INDEX_PER_STREAM (8)
274
Guy Cohen39e88502008-04-23 17:14:57 -0700275static inline u8 rs_extract_rate(u32 rate_n_flags)
Zhu Yib481de92007-09-25 17:54:57 -0700276{
277 return (u8)(rate_n_flags & 0xFF);
278}
279
Tomas Winklere227cea2008-07-18 13:53:05 +0800280static void rs_rate_scale_clear_window(struct iwl_rate_scale_data *window)
Zhu Yib481de92007-09-25 17:54:57 -0700281{
282 window->data = 0;
283 window->success_counter = 0;
284 window->success_ratio = IWL_INVALID_VALUE;
285 window->counter = 0;
286 window->average_tpt = IWL_INVALID_VALUE;
287 window->stamp = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700288}
289
Guy Cohenaade00c2008-04-23 17:14:59 -0700290static inline u8 rs_is_valid_ant(u8 valid_antenna, u8 ant_type)
291{
Tomas Winkler3ac7f142008-07-21 02:40:14 +0300292 return (ant_type & valid_antenna) == ant_type;
Guy Cohenaade00c2008-04-23 17:14:59 -0700293}
294
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200295/*
296 * removes the old data from the statistics. All data that is older than
297 * TID_MAX_TIME_DIFF, will be deleted.
298 */
Tomas Winklere227cea2008-07-18 13:53:05 +0800299static void rs_tl_rm_old_stats(struct iwl_traffic_load *tl, u32 curr_time)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200300{
301 /* The oldest age we want to keep */
302 u32 oldest_time = curr_time - TID_MAX_TIME_DIFF;
303
304 while (tl->queue_count &&
305 (tl->time_stamp < oldest_time)) {
306 tl->total -= tl->packet_count[tl->head];
307 tl->packet_count[tl->head] = 0;
308 tl->time_stamp += TID_QUEUE_CELL_SPACING;
309 tl->queue_count--;
310 tl->head++;
311 if (tl->head >= TID_QUEUE_MAX_SIZE)
312 tl->head = 0;
313 }
314}
315
316/*
317 * increment traffic load value for tid and also remove
318 * any old values if passed the certain time period
319 */
Tomas Winklere227cea2008-07-18 13:53:05 +0800320static u8 rs_tl_add_packet(struct iwl_lq_sta *lq_data,
Ron Rindjunskyfaa29712008-06-12 09:46:58 +0800321 struct ieee80211_hdr *hdr)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200322{
323 u32 curr_time = jiffies_to_msecs(jiffies);
324 u32 time_diff;
325 s32 index;
Tomas Winklere227cea2008-07-18 13:53:05 +0800326 struct iwl_traffic_load *tl = NULL;
Tomas Winkler54dbb522008-05-15 13:54:06 +0800327 u8 tid;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200328
Tomas Winkler417f1142008-11-12 13:14:06 -0800329 if (ieee80211_is_data_qos(hdr->frame_control)) {
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700330 u8 *qc = ieee80211_get_qos_ctl(hdr);
Tomas Winkler54dbb522008-05-15 13:54:06 +0800331 tid = qc[0] & 0xf;
332 } else
Ron Rindjunskyfaa29712008-06-12 09:46:58 +0800333 return MAX_TID_COUNT;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200334
335 tl = &lq_data->load[tid];
336
337 curr_time -= curr_time % TID_ROUND_VALUE;
338
339 /* Happens only for the first packet. Initialize the data */
340 if (!(tl->queue_count)) {
341 tl->total = 1;
342 tl->time_stamp = curr_time;
343 tl->queue_count = 1;
344 tl->head = 0;
345 tl->packet_count[0] = 1;
Ron Rindjunskyfaa29712008-06-12 09:46:58 +0800346 return MAX_TID_COUNT;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200347 }
348
349 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
350 index = time_diff / TID_QUEUE_CELL_SPACING;
351
352 /* The history is too long: remove data that is older than */
353 /* TID_MAX_TIME_DIFF */
354 if (index >= TID_QUEUE_MAX_SIZE)
355 rs_tl_rm_old_stats(tl, curr_time);
356
357 index = (tl->head + index) % TID_QUEUE_MAX_SIZE;
358 tl->packet_count[index] = tl->packet_count[index] + 1;
359 tl->total = tl->total + 1;
360
361 if ((index + 1) > tl->queue_count)
362 tl->queue_count = index + 1;
Ron Rindjunskyfaa29712008-06-12 09:46:58 +0800363
364 return tid;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200365}
366
367/*
368 get the traffic load value for tid
369*/
Tomas Winklere227cea2008-07-18 13:53:05 +0800370static u32 rs_tl_get_load(struct iwl_lq_sta *lq_data, u8 tid)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200371{
372 u32 curr_time = jiffies_to_msecs(jiffies);
373 u32 time_diff;
374 s32 index;
Tomas Winklere227cea2008-07-18 13:53:05 +0800375 struct iwl_traffic_load *tl = NULL;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200376
377 if (tid >= TID_MAX_LOAD_COUNT)
378 return 0;
379
380 tl = &(lq_data->load[tid]);
381
382 curr_time -= curr_time % TID_ROUND_VALUE;
383
384 if (!(tl->queue_count))
385 return 0;
386
387 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
388 index = time_diff / TID_QUEUE_CELL_SPACING;
389
390 /* The history is too long: remove data that is older than */
391 /* TID_MAX_TIME_DIFF */
392 if (index >= TID_QUEUE_MAX_SIZE)
393 rs_tl_rm_old_stats(tl, curr_time);
394
395 return tl->total;
396}
397
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700398static void rs_tl_turn_on_agg_for_tid(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +0800399 struct iwl_lq_sta *lq_data, u8 tid,
Johannes Berg4b7679a2008-09-18 18:14:18 +0200400 struct ieee80211_sta *sta)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200401{
Johannes Bergff550cb2008-09-11 03:17:05 +0200402 if (rs_tl_get_load(lq_data, tid) > IWL_AGG_LOAD_THRESHOLD) {
Tomas Winklere1623442009-01-27 14:27:56 -0800403 IWL_DEBUG_HT(priv, "Starting Tx agg: STA: %pM tid: %d\n",
Johannes Berge1749612008-10-27 15:59:26 -0700404 sta->addr, tid);
Johannes Berg4b7679a2008-09-18 18:14:18 +0200405 ieee80211_start_tx_ba_session(priv->hw, sta->addr, tid);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200406 }
407}
408
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700409static void rs_tl_turn_on_agg(struct iwl_priv *priv, u8 tid,
Tomas Winklere227cea2008-07-18 13:53:05 +0800410 struct iwl_lq_sta *lq_data,
Johannes Berg4b7679a2008-09-18 18:14:18 +0200411 struct ieee80211_sta *sta)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200412{
413 if ((tid < TID_MAX_LOAD_COUNT))
414 rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
415 else if (tid == IWL_AGG_ALL_TID)
416 for (tid = 0; tid < TID_MAX_LOAD_COUNT; tid++)
417 rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
418}
419
Guy Cohen39e88502008-04-23 17:14:57 -0700420static inline int get_num_of_ant_from_rate(u32 rate_n_flags)
Guy Cohenfde0db32008-04-21 15:42:01 -0700421{
Tomas Winkler3ac7f142008-07-21 02:40:14 +0300422 return !!(rate_n_flags & RATE_MCS_ANT_A_MSK) +
423 !!(rate_n_flags & RATE_MCS_ANT_B_MSK) +
424 !!(rate_n_flags & RATE_MCS_ANT_C_MSK);
Guy Cohenfde0db32008-04-21 15:42:01 -0700425}
426
Ben Cahill77626352007-11-29 11:09:44 +0800427/**
428 * rs_collect_tx_data - Update the success/failure sliding window
429 *
430 * We keep a sliding window of the last 62 packets transmitted
431 * at this rate. window->data contains the bitmask of successful
432 * packets.
433 */
Tomas Winklere227cea2008-07-18 13:53:05 +0800434static int rs_collect_tx_data(struct iwl_rate_scale_data *windows,
Ron Rindjunsky99556432008-01-28 14:07:25 +0200435 int scale_index, s32 tpt, int retries,
436 int successes)
Zhu Yib481de92007-09-25 17:54:57 -0700437{
Tomas Winklere227cea2008-07-18 13:53:05 +0800438 struct iwl_rate_scale_data *window = NULL;
Guy Cohen39e88502008-04-23 17:14:57 -0700439 static const u64 mask = (((u64)1) << (IWL_RATE_MAX_WINDOW - 1));
Zhu Yib481de92007-09-25 17:54:57 -0700440 s32 fail_count;
441
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800442 if (scale_index < 0 || scale_index >= IWL_RATE_COUNT)
443 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -0700444
Ben Cahill77626352007-11-29 11:09:44 +0800445 /* Select data for current tx bit rate */
Zhu Yib481de92007-09-25 17:54:57 -0700446 window = &(windows[scale_index]);
447
Ben Cahill77626352007-11-29 11:09:44 +0800448 /*
449 * Keep track of only the latest 62 tx frame attempts in this rate's
450 * history window; anything older isn't really relevant any more.
451 * If we have filled up the sliding window, drop the oldest attempt;
452 * if the oldest attempt (highest bit in bitmap) shows "success",
453 * subtract "1" from the success counter (this is the main reason
454 * we keep these bitmaps!).
455 */
Ron Rindjunsky99556432008-01-28 14:07:25 +0200456 while (retries > 0) {
Guy Cohen39e88502008-04-23 17:14:57 -0700457 if (window->counter >= IWL_RATE_MAX_WINDOW) {
458
459 /* remove earliest */
460 window->counter = IWL_RATE_MAX_WINDOW - 1;
461
Ron Rindjunsky99556432008-01-28 14:07:25 +0200462 if (window->data & mask) {
463 window->data &= ~mask;
Guy Cohen39e88502008-04-23 17:14:57 -0700464 window->success_counter--;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200465 }
Zhu Yib481de92007-09-25 17:54:57 -0700466 }
Zhu Yib481de92007-09-25 17:54:57 -0700467
Ron Rindjunsky99556432008-01-28 14:07:25 +0200468 /* Increment frames-attempted counter */
469 window->counter++;
Ben Cahill77626352007-11-29 11:09:44 +0800470
Ron Rindjunsky99556432008-01-28 14:07:25 +0200471 /* Shift bitmap by one frame (throw away oldest history),
472 * OR in "1", and increment "success" if this
473 * frame was successful. */
Guy Cohen90d77952008-09-09 10:54:53 +0800474 window->data <<= 1;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200475 if (successes > 0) {
Guy Cohen39e88502008-04-23 17:14:57 -0700476 window->success_counter++;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200477 window->data |= 0x1;
478 successes--;
479 }
480
481 retries--;
Zhu Yib481de92007-09-25 17:54:57 -0700482 }
483
Ben Cahill77626352007-11-29 11:09:44 +0800484 /* Calculate current success ratio, avoid divide-by-0! */
Zhu Yib481de92007-09-25 17:54:57 -0700485 if (window->counter > 0)
486 window->success_ratio = 128 * (100 * window->success_counter)
487 / window->counter;
488 else
489 window->success_ratio = IWL_INVALID_VALUE;
490
491 fail_count = window->counter - window->success_counter;
492
Ben Cahill77626352007-11-29 11:09:44 +0800493 /* Calculate average throughput, if we have enough history. */
Zhu Yib481de92007-09-25 17:54:57 -0700494 if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
495 (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
496 window->average_tpt = (window->success_ratio * tpt + 64) / 128;
497 else
498 window->average_tpt = IWL_INVALID_VALUE;
499
Ben Cahill77626352007-11-29 11:09:44 +0800500 /* Tag this window as having been updated */
Zhu Yib481de92007-09-25 17:54:57 -0700501 window->stamp = jiffies;
502
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800503 return 0;
Zhu Yib481de92007-09-25 17:54:57 -0700504}
505
Ben Cahill77626352007-11-29 11:09:44 +0800506/*
507 * Fill uCode API rate_n_flags field, based on "search" or "active" table.
508 */
Guy Cohen39e88502008-04-23 17:14:57 -0700509/* FIXME:RS:remove this function and put the flags statically in the table */
Tomas Winkler978785a2008-12-19 10:37:31 +0800510static u32 rate_n_flags_from_tbl(struct iwl_priv *priv,
511 struct iwl_scale_tbl_info *tbl,
512 int index, u8 use_green)
Zhu Yib481de92007-09-25 17:54:57 -0700513{
Guy Cohen39e88502008-04-23 17:14:57 -0700514 u32 rate_n_flags = 0;
515
Zhu Yib481de92007-09-25 17:54:57 -0700516 if (is_legacy(tbl->lq_type)) {
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800517 rate_n_flags = iwl_rates[index].plcp;
Zhu Yib481de92007-09-25 17:54:57 -0700518 if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
Guy Cohen39e88502008-04-23 17:14:57 -0700519 rate_n_flags |= RATE_MCS_CCK_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700520
Guy Cohenfde0db32008-04-21 15:42:01 -0700521 } else if (is_Ht(tbl->lq_type)) {
522 if (index > IWL_LAST_OFDM_RATE) {
Tomas Winkler978785a2008-12-19 10:37:31 +0800523 IWL_ERR(priv, "Invalid HT rate index %d\n", index);
Zhu Yib481de92007-09-25 17:54:57 -0700524 index = IWL_LAST_OFDM_RATE;
Guy Cohenfde0db32008-04-21 15:42:01 -0700525 }
Guy Cohen39e88502008-04-23 17:14:57 -0700526 rate_n_flags = RATE_MCS_HT_MSK;
Guy Cohenfde0db32008-04-21 15:42:01 -0700527
528 if (is_siso(tbl->lq_type))
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800529 rate_n_flags |= iwl_rates[index].plcp_siso;
Guy Cohenfde0db32008-04-21 15:42:01 -0700530 else if (is_mimo2(tbl->lq_type))
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800531 rate_n_flags |= iwl_rates[index].plcp_mimo2;
Guy Cohenfde0db32008-04-21 15:42:01 -0700532 else
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800533 rate_n_flags |= iwl_rates[index].plcp_mimo3;
Zhu Yib481de92007-09-25 17:54:57 -0700534 } else {
Tomas Winkler978785a2008-12-19 10:37:31 +0800535 IWL_ERR(priv, "Invalid tbl->lq_type %d\n", tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -0700536 }
537
Guy Cohen39e88502008-04-23 17:14:57 -0700538 rate_n_flags |= ((tbl->ant_type << RATE_MCS_ANT_POS) &
Guy Cohenfde0db32008-04-21 15:42:01 -0700539 RATE_MCS_ANT_ABC_MSK);
Zhu Yib481de92007-09-25 17:54:57 -0700540
Guy Cohen39e88502008-04-23 17:14:57 -0700541 if (is_Ht(tbl->lq_type)) {
542 if (tbl->is_fat) {
543 if (tbl->is_dup)
544 rate_n_flags |= RATE_MCS_DUP_MSK;
545 else
546 rate_n_flags |= RATE_MCS_FAT_MSK;
547 }
548 if (tbl->is_SGI)
549 rate_n_flags |= RATE_MCS_SGI_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700550
Guy Cohen39e88502008-04-23 17:14:57 -0700551 if (use_green) {
552 rate_n_flags |= RATE_MCS_GF_MSK;
553 if (is_siso(tbl->lq_type) && tbl->is_SGI) {
554 rate_n_flags &= ~RATE_MCS_SGI_MSK;
Tomas Winkler978785a2008-12-19 10:37:31 +0800555 IWL_ERR(priv, "GF was set with SGI:SISO\n");
Guy Cohen39e88502008-04-23 17:14:57 -0700556 }
557 }
Zhu Yib481de92007-09-25 17:54:57 -0700558 }
Guy Cohen39e88502008-04-23 17:14:57 -0700559 return rate_n_flags;
Zhu Yib481de92007-09-25 17:54:57 -0700560}
561
Ben Cahill77626352007-11-29 11:09:44 +0800562/*
563 * Interpret uCode API's rate_n_flags format,
564 * fill "search" or "active" tx mode table.
565 */
Guy Cohen39e88502008-04-23 17:14:57 -0700566static int rs_get_tbl_info_from_mcs(const u32 rate_n_flags,
Johannes Berg8318d782008-01-24 19:38:38 +0100567 enum ieee80211_band band,
Tomas Winklere227cea2008-07-18 13:53:05 +0800568 struct iwl_scale_tbl_info *tbl,
Zhu Yib481de92007-09-25 17:54:57 -0700569 int *rate_idx)
570{
Guy Cohen39e88502008-04-23 17:14:57 -0700571 u32 ant_msk = (rate_n_flags & RATE_MCS_ANT_ABC_MSK);
572 u8 num_of_ant = get_num_of_ant_from_rate(rate_n_flags);
573 u8 mcs;
Zhu Yib481de92007-09-25 17:54:57 -0700574
Tomas Winklere7d326a2008-06-12 09:47:11 +0800575 *rate_idx = iwl_hwrate_to_plcp_idx(rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -0700576
Guy Cohenfde0db32008-04-21 15:42:01 -0700577 if (*rate_idx == IWL_RATE_INVALID) {
Zhu Yib481de92007-09-25 17:54:57 -0700578 *rate_idx = -1;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800579 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -0700580 }
Ben Cahill77626352007-11-29 11:09:44 +0800581 tbl->is_SGI = 0; /* default legacy setup */
Zhu Yib481de92007-09-25 17:54:57 -0700582 tbl->is_fat = 0;
583 tbl->is_dup = 0;
Guy Cohenfde0db32008-04-21 15:42:01 -0700584 tbl->ant_type = (ant_msk >> RATE_MCS_ANT_POS);
585 tbl->lq_type = LQ_NONE;
Mohamed Abbasd6e93392009-05-08 13:44:39 -0700586 tbl->max_search = IWL_MAX_SEARCH;
Zhu Yib481de92007-09-25 17:54:57 -0700587
Ben Cahill77626352007-11-29 11:09:44 +0800588 /* legacy rate format */
Guy Cohen39e88502008-04-23 17:14:57 -0700589 if (!(rate_n_flags & RATE_MCS_HT_MSK)) {
Guy Cohenfde0db32008-04-21 15:42:01 -0700590 if (num_of_ant == 1) {
Johannes Berg8318d782008-01-24 19:38:38 +0100591 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700592 tbl->lq_type = LQ_A;
593 else
594 tbl->lq_type = LQ_G;
Zhu Yib481de92007-09-25 17:54:57 -0700595 }
Guy Cohenfde0db32008-04-21 15:42:01 -0700596 /* HT rate format */
Zhu Yib481de92007-09-25 17:54:57 -0700597 } else {
Guy Cohen39e88502008-04-23 17:14:57 -0700598 if (rate_n_flags & RATE_MCS_SGI_MSK)
Zhu Yib481de92007-09-25 17:54:57 -0700599 tbl->is_SGI = 1;
600
Guy Cohen39e88502008-04-23 17:14:57 -0700601 if ((rate_n_flags & RATE_MCS_FAT_MSK) ||
602 (rate_n_flags & RATE_MCS_DUP_MSK))
Zhu Yib481de92007-09-25 17:54:57 -0700603 tbl->is_fat = 1;
604
Guy Cohen39e88502008-04-23 17:14:57 -0700605 if (rate_n_flags & RATE_MCS_DUP_MSK)
Zhu Yib481de92007-09-25 17:54:57 -0700606 tbl->is_dup = 1;
Guy Cohenfde0db32008-04-21 15:42:01 -0700607
Guy Cohen39e88502008-04-23 17:14:57 -0700608 mcs = rs_extract_rate(rate_n_flags);
Guy Cohenfde0db32008-04-21 15:42:01 -0700609
Guy Cohen39e88502008-04-23 17:14:57 -0700610 /* SISO */
611 if (mcs <= IWL_RATE_SISO_60M_PLCP) {
Guy Cohenfde0db32008-04-21 15:42:01 -0700612 if (num_of_ant == 1)
613 tbl->lq_type = LQ_SISO; /*else NONE*/
614 /* MIMO2 */
Guy Cohen39e88502008-04-23 17:14:57 -0700615 } else if (mcs <= IWL_RATE_MIMO2_60M_PLCP) {
Guy Cohenfde0db32008-04-21 15:42:01 -0700616 if (num_of_ant == 2)
617 tbl->lq_type = LQ_MIMO2;
618 /* MIMO3 */
619 } else {
Mohamed Abbasd6e93392009-05-08 13:44:39 -0700620 if (num_of_ant == 3) {
621 tbl->max_search = IWL_MAX_11N_MIMO3_SEARCH;
Guy Cohenfde0db32008-04-21 15:42:01 -0700622 tbl->lq_type = LQ_MIMO3;
Mohamed Abbasd6e93392009-05-08 13:44:39 -0700623 }
Guy Cohenfde0db32008-04-21 15:42:01 -0700624 }
Zhu Yib481de92007-09-25 17:54:57 -0700625 }
626 return 0;
627}
Guy Cohenaade00c2008-04-23 17:14:59 -0700628
629/* switch to another antenna/antennas and return 1 */
630/* if no other valid antenna found, return 0 */
631static int rs_toggle_antenna(u32 valid_ant, u32 *rate_n_flags,
Tomas Winklere227cea2008-07-18 13:53:05 +0800632 struct iwl_scale_tbl_info *tbl)
Zhu Yib481de92007-09-25 17:54:57 -0700633{
Guy Cohenaade00c2008-04-23 17:14:59 -0700634 u8 new_ant_type;
635
636 if (!tbl->ant_type || tbl->ant_type > ANT_ABC)
637 return 0;
638
639 if (!rs_is_valid_ant(valid_ant, tbl->ant_type))
640 return 0;
641
642 new_ant_type = ant_toggle_lookup[tbl->ant_type];
643
644 while ((new_ant_type != tbl->ant_type) &&
645 !rs_is_valid_ant(valid_ant, new_ant_type))
646 new_ant_type = ant_toggle_lookup[new_ant_type];
647
648 if (new_ant_type == tbl->ant_type)
649 return 0;
650
651 tbl->ant_type = new_ant_type;
652 *rate_n_flags &= ~RATE_MCS_ANT_ABC_MSK;
653 *rate_n_flags |= new_ant_type << RATE_MCS_ANT_POS;
654 return 1;
Zhu Yib481de92007-09-25 17:54:57 -0700655}
656
Wey-Yi Guy2681b202009-05-21 13:44:22 -0700657/* in 4965 we don't use greenfield at all */
658static inline u8 rs_use_green(struct iwl_priv *priv,
659 struct ieee80211_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -0700660{
Wey-Yi Guy2681b202009-05-21 13:44:22 -0700661 u8 is_green;
662
663 if ((priv->hw_rev & CSR_HW_REV_TYPE_MSK) == CSR_HW_REV_TYPE_4965)
664 is_green = 0;
665 else
666 is_green = (conf_is_ht(conf) &&
667 priv->current_ht_config.is_green_field &&
668 !priv->current_ht_config.non_GF_STA_present);
669 return is_green;
Guy Cohenf935a6d2008-04-23 17:15:02 -0700670}
Zhu Yib481de92007-09-25 17:54:57 -0700671
672/**
673 * rs_get_supported_rates - get the available rates
674 *
675 * if management frame or broadcast frame only return
676 * basic available rates.
677 *
678 */
Tomas Winklere227cea2008-07-18 13:53:05 +0800679static u16 rs_get_supported_rates(struct iwl_lq_sta *lq_sta,
680 struct ieee80211_hdr *hdr,
681 enum iwl_table_type rate_type)
Zhu Yib481de92007-09-25 17:54:57 -0700682{
Zhu Yib481de92007-09-25 17:54:57 -0700683 if (hdr && is_multicast_ether_addr(hdr->addr1) &&
Guy Coheneecd6e52008-04-23 17:14:58 -0700684 lq_sta->active_rate_basic)
685 return lq_sta->active_rate_basic;
686
687 if (is_legacy(rate_type)) {
688 return lq_sta->active_legacy_rate;
689 } else {
690 if (is_siso(rate_type))
691 return lq_sta->active_siso_rate;
692 else if (is_mimo2(rate_type))
693 return lq_sta->active_mimo2_rate;
694 else
695 return lq_sta->active_mimo3_rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -0800696 }
Zhu Yib481de92007-09-25 17:54:57 -0700697}
698
Ester Kummerbf403db2008-05-05 10:22:40 +0800699static u16 rs_get_adjacent_rate(struct iwl_priv *priv, u8 index, u16 rate_mask,
700 int rate_type)
Zhu Yib481de92007-09-25 17:54:57 -0700701{
702 u8 high = IWL_RATE_INVALID;
703 u8 low = IWL_RATE_INVALID;
704
Ian Schram01ebd062007-10-25 17:15:22 +0800705 /* 802.11A or ht walks to the next literal adjacent rate in
Zhu Yib481de92007-09-25 17:54:57 -0700706 * the rate table */
707 if (is_a_band(rate_type) || !is_legacy(rate_type)) {
708 int i;
709 u32 mask;
710
711 /* Find the previous rate that is in the rate mask */
712 i = index - 1;
713 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
714 if (rate_mask & mask) {
715 low = i;
716 break;
717 }
718 }
719
720 /* Find the next rate that is in the rate mask */
721 i = index + 1;
722 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
723 if (rate_mask & mask) {
724 high = i;
725 break;
726 }
727 }
728
729 return (high << 8) | low;
730 }
731
732 low = index;
733 while (low != IWL_RATE_INVALID) {
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800734 low = iwl_rates[low].prev_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700735 if (low == IWL_RATE_INVALID)
736 break;
737 if (rate_mask & (1 << low))
738 break;
Tomas Winklere1623442009-01-27 14:27:56 -0800739 IWL_DEBUG_RATE(priv, "Skipping masked lower rate: %d\n", low);
Zhu Yib481de92007-09-25 17:54:57 -0700740 }
741
742 high = index;
743 while (high != IWL_RATE_INVALID) {
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800744 high = iwl_rates[high].next_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700745 if (high == IWL_RATE_INVALID)
746 break;
747 if (rate_mask & (1 << high))
748 break;
Tomas Winklere1623442009-01-27 14:27:56 -0800749 IWL_DEBUG_RATE(priv, "Skipping masked higher rate: %d\n", high);
Zhu Yib481de92007-09-25 17:54:57 -0700750 }
751
752 return (high << 8) | low;
753}
754
Tomas Winklere227cea2008-07-18 13:53:05 +0800755static u32 rs_get_lower_rate(struct iwl_lq_sta *lq_sta,
756 struct iwl_scale_tbl_info *tbl,
757 u8 scale_index, u8 ht_possible)
Zhu Yib481de92007-09-25 17:54:57 -0700758{
Zhu Yib481de92007-09-25 17:54:57 -0700759 s32 low;
760 u16 rate_mask;
761 u16 high_low;
762 u8 switch_to_legacy = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -0800763 u8 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -0700764
765 /* check if we need to switch from HT to legacy rates.
766 * assumption is that mandatory rates (1Mbps or 6Mbps)
767 * are always supported (spec demand) */
768 if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_index)) {
769 switch_to_legacy = 1;
770 scale_index = rs_ht_to_legacy[scale_index];
Johannes Berg8318d782008-01-24 19:38:38 +0100771 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700772 tbl->lq_type = LQ_A;
773 else
774 tbl->lq_type = LQ_G;
775
Guy Cohen69fdb302008-04-23 17:15:01 -0700776 if (num_of_ant(tbl->ant_type) > 1)
Guy Cohenfde0db32008-04-21 15:42:01 -0700777 tbl->ant_type = ANT_A;/*FIXME:RS*/
Zhu Yib481de92007-09-25 17:54:57 -0700778
779 tbl->is_fat = 0;
780 tbl->is_SGI = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -0700781 tbl->max_search = IWL_MAX_SEARCH;
Zhu Yib481de92007-09-25 17:54:57 -0700782 }
783
Guy Coheneecd6e52008-04-23 17:14:58 -0700784 rate_mask = rs_get_supported_rates(lq_sta, NULL, tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -0700785
Ben Cahill77626352007-11-29 11:09:44 +0800786 /* Mask with station rate restriction */
Zhu Yib481de92007-09-25 17:54:57 -0700787 if (is_legacy(tbl->lq_type)) {
Ben Cahill77626352007-11-29 11:09:44 +0800788 /* supp_rates has no CCK bits in A mode */
Johannes Berg8318d782008-01-24 19:38:38 +0100789 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700790 rate_mask = (u16)(rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -0800791 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
Zhu Yib481de92007-09-25 17:54:57 -0700792 else
Tomas Winklerc33104f2008-01-14 17:46:21 -0800793 rate_mask = (u16)(rate_mask & lq_sta->supp_rates);
Zhu Yib481de92007-09-25 17:54:57 -0700794 }
795
Ben Cahill77626352007-11-29 11:09:44 +0800796 /* If we switched from HT to legacy, check current rate */
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800797 if (switch_to_legacy && (rate_mask & (1 << scale_index))) {
Guy Cohen39e88502008-04-23 17:14:57 -0700798 low = scale_index;
799 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700800 }
801
Ester Kummerbf403db2008-05-05 10:22:40 +0800802 high_low = rs_get_adjacent_rate(lq_sta->drv, scale_index, rate_mask,
803 tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -0700804 low = high_low & 0xff;
805
Guy Cohen39e88502008-04-23 17:14:57 -0700806 if (low == IWL_RATE_INVALID)
807 low = scale_index;
808
809out:
Tomas Winkler978785a2008-12-19 10:37:31 +0800810 return rate_n_flags_from_tbl(lq_sta->drv, tbl, low, is_green);
Zhu Yib481de92007-09-25 17:54:57 -0700811}
812
Ben Cahill77626352007-11-29 11:09:44 +0800813/*
814 * mac80211 sends us Tx status
815 */
Johannes Berg4b7679a2008-09-18 18:14:18 +0200816static void rs_tx_status(void *priv_r, struct ieee80211_supported_band *sband,
817 struct ieee80211_sta *sta, void *priv_sta,
Johannes Berge039fa42008-05-15 12:55:29 +0200818 struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -0700819{
820 int status;
821 u8 retries;
822 int rs_index, index = 0;
Tomas Winkler417f1142008-11-12 13:14:06 -0800823 struct iwl_lq_sta *lq_sta = priv_sta;
Tomas Winkler66c73db2008-04-15 16:01:40 -0700824 struct iwl_link_quality_cmd *table;
Zhu Yib481de92007-09-25 17:54:57 -0700825 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berg4b7679a2008-09-18 18:14:18 +0200826 struct iwl_priv *priv = (struct iwl_priv *)priv_r;
827 struct ieee80211_hw *hw = priv->hw;
Johannes Berge039fa42008-05-15 12:55:29 +0200828 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Tomas Winklere227cea2008-07-18 13:53:05 +0800829 struct iwl_rate_scale_data *window = NULL;
830 struct iwl_rate_scale_data *search_win = NULL;
Guy Cohen39e88502008-04-23 17:14:57 -0700831 u32 tx_rate;
Tomas Winklere227cea2008-07-18 13:53:05 +0800832 struct iwl_scale_tbl_info tbl_type;
833 struct iwl_scale_tbl_info *curr_tbl, *search_tbl;
Zhu Yib481de92007-09-25 17:54:57 -0700834 u8 active_index = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700835 s32 tpt = 0;
836
Tomas Winklere1623442009-01-27 14:27:56 -0800837 IWL_DEBUG_RATE_LIMIT(priv, "get frame ack response, update rate scale window\n");
Zhu Yib481de92007-09-25 17:54:57 -0700838
Tomas Winkler417f1142008-11-12 13:14:06 -0800839 if (!ieee80211_is_data(hdr->frame_control) ||
Gábor Stefanik514d65c2009-04-23 19:36:08 +0200840 info->flags & IEEE80211_TX_CTL_NO_ACK)
Zhu Yib481de92007-09-25 17:54:57 -0700841 return;
842
Ron Rindjunsky99556432008-01-28 14:07:25 +0200843 /* This packet was aggregated but doesn't carry rate scale info */
Johannes Berge039fa42008-05-15 12:55:29 +0200844 if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
845 !(info->flags & IEEE80211_TX_STAT_AMPDU))
Ron Rindjunsky99556432008-01-28 14:07:25 +0200846 return;
847
Wey-Yi Guy8fe72312009-03-10 14:35:10 -0700848 if (info->flags & IEEE80211_TX_STAT_AMPDU)
849 retries = 0;
850 else
851 retries = info->status.rates[0].count - 1;
Zhu Yib481de92007-09-25 17:54:57 -0700852
853 if (retries > 15)
854 retries = 15;
855
Johannes Berg05c914f2008-09-11 00:01:58 +0200856 if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) &&
Tomas Winklerc33104f2008-01-14 17:46:21 -0800857 !lq_sta->ibss_sta_added)
Tomas Winklerf4d60822008-03-05 11:31:00 -0800858 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700859
Tomas Winklerc33104f2008-01-14 17:46:21 -0800860 table = &lq_sta->lq;
861 active_index = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -0700862
Tomas Winklerc33104f2008-01-14 17:46:21 -0800863 curr_tbl = &(lq_sta->lq_info[active_index]);
864 search_tbl = &(lq_sta->lq_info[(1 - active_index)]);
Tomas Winklere227cea2008-07-18 13:53:05 +0800865 window = (struct iwl_rate_scale_data *)&(curr_tbl->win[0]);
866 search_win = (struct iwl_rate_scale_data *)&(search_tbl->win[0]);
Zhu Yib481de92007-09-25 17:54:57 -0700867
Ben Cahill77626352007-11-29 11:09:44 +0800868 /*
869 * Ignore this Tx frame response if its initial rate doesn't match
870 * that of latest Link Quality command. There may be stragglers
871 * from a previous Link Quality command, but we're no longer interested
872 * in those; they're either from the "active" mode while we're trying
873 * to check "search" mode, or a prior "search" mode after we've moved
874 * to a new "search" mode (which might become the new "active" mode).
875 */
Guy Cohen39e88502008-04-23 17:14:57 -0700876 tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
877 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800878 if (priv->band == IEEE80211_BAND_5GHZ)
879 rs_index -= IWL_FIRST_OFDM_RATE;
880
Johannes Berge6a98542008-10-21 12:40:02 +0200881 if ((info->status.rates[0].idx < 0) ||
882 (tbl_type.is_SGI != !!(info->status.rates[0].flags & IEEE80211_TX_RC_SHORT_GI)) ||
883 (tbl_type.is_fat != !!(info->status.rates[0].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)) ||
884 (tbl_type.is_dup != !!(info->status.rates[0].flags & IEEE80211_TX_RC_DUP_DATA)) ||
885 (tbl_type.ant_type != info->antenna_sel_tx) ||
886 (!!(tx_rate & RATE_MCS_HT_MSK) != !!(info->status.rates[0].flags & IEEE80211_TX_RC_MCS)) ||
887 (!!(tx_rate & RATE_MCS_GF_MSK) != !!(info->status.rates[0].flags & IEEE80211_TX_RC_GREEN_FIELD)) ||
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800888 (hw->wiphy->bands[priv->band]->bitrates[rs_index].bitrate !=
Johannes Berge6a98542008-10-21 12:40:02 +0200889 hw->wiphy->bands[info->band]->bitrates[info->status.rates[0].idx].bitrate)) {
Tomas Winklere1623442009-01-27 14:27:56 -0800890 IWL_DEBUG_RATE(priv, "initial rate does not match 0x%x\n", tx_rate);
Mohamed Abbasd5e49032008-12-12 08:22:15 -0800891 /* the last LQ command could failed so the LQ in ucode not
892 * the same in driver sync up
893 */
Abbas, Mohamed7d049e52009-01-20 21:33:53 -0800894 lq_sta->missed_rate_counter++;
895 if (lq_sta->missed_rate_counter > IWL_MISSED_RATE_MAX) {
896 lq_sta->missed_rate_counter = 0;
897 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
898 }
Tomas Winklerf4d60822008-03-05 11:31:00 -0800899 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700900 }
901
Abbas, Mohamed7d049e52009-01-20 21:33:53 -0800902 lq_sta->missed_rate_counter = 0;
Ben Cahill77626352007-11-29 11:09:44 +0800903 /* Update frame history window with "failure" for each Tx retry. */
Zhu Yib481de92007-09-25 17:54:57 -0700904 while (retries) {
Ben Cahill77626352007-11-29 11:09:44 +0800905 /* Look up the rate and other info used for each tx attempt.
906 * Each tx attempt steps one entry deeper in the rate table. */
Guy Cohen39e88502008-04-23 17:14:57 -0700907 tx_rate = le32_to_cpu(table->rs_table[index].rate_n_flags);
908 rs_get_tbl_info_from_mcs(tx_rate, priv->band,
Zhu Yib481de92007-09-25 17:54:57 -0700909 &tbl_type, &rs_index);
910
Ben Cahill77626352007-11-29 11:09:44 +0800911 /* If type matches "search" table,
912 * add failure to "search" history */
Zhu Yib481de92007-09-25 17:54:57 -0700913 if ((tbl_type.lq_type == search_tbl->lq_type) &&
Guy Cohenfde0db32008-04-21 15:42:01 -0700914 (tbl_type.ant_type == search_tbl->ant_type) &&
Zhu Yib481de92007-09-25 17:54:57 -0700915 (tbl_type.is_SGI == search_tbl->is_SGI)) {
916 if (search_tbl->expected_tpt)
917 tpt = search_tbl->expected_tpt[rs_index];
918 else
919 tpt = 0;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200920 rs_collect_tx_data(search_win, rs_index, tpt, 1, 0);
Ben Cahill77626352007-11-29 11:09:44 +0800921
922 /* Else if type matches "current/active" table,
923 * add failure to "current/active" history */
Zhu Yib481de92007-09-25 17:54:57 -0700924 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
Guy Cohenfde0db32008-04-21 15:42:01 -0700925 (tbl_type.ant_type == curr_tbl->ant_type) &&
Zhu Yib481de92007-09-25 17:54:57 -0700926 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
927 if (curr_tbl->expected_tpt)
928 tpt = curr_tbl->expected_tpt[rs_index];
929 else
930 tpt = 0;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200931 rs_collect_tx_data(window, rs_index, tpt, 1, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700932 }
Ben Cahill77626352007-11-29 11:09:44 +0800933
934 /* If not searching for a new mode, increment failed counter
935 * ... this helps determine when to start searching again */
Tomas Winklerc33104f2008-01-14 17:46:21 -0800936 if (lq_sta->stay_in_tbl)
937 lq_sta->total_failed++;
Zhu Yib481de92007-09-25 17:54:57 -0700938 --retries;
939 index++;
940
941 }
942
Ben Cahill77626352007-11-29 11:09:44 +0800943 /*
944 * Find (by rate) the history window to update with final Tx attempt;
945 * if Tx was successful first try, use original rate,
946 * else look up the rate that was, finally, successful.
947 */
Guy Cohen39e88502008-04-23 17:14:57 -0700948 tx_rate = le32_to_cpu(table->rs_table[index].rate_n_flags);
Wey-Yi Guy12b96812009-04-08 11:39:27 -0700949 lq_sta->last_rate_n_flags = tx_rate;
Guy Cohen39e88502008-04-23 17:14:57 -0700950 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
Zhu Yib481de92007-09-25 17:54:57 -0700951
Ben Cahill77626352007-11-29 11:09:44 +0800952 /* Update frame history window with "success" if Tx got ACKed ... */
Johannes Berge039fa42008-05-15 12:55:29 +0200953 status = !!(info->flags & IEEE80211_TX_STAT_ACK);
Zhu Yib481de92007-09-25 17:54:57 -0700954
Ben Cahill77626352007-11-29 11:09:44 +0800955 /* If type matches "search" table,
956 * add final tx status to "search" history */
Zhu Yib481de92007-09-25 17:54:57 -0700957 if ((tbl_type.lq_type == search_tbl->lq_type) &&
Guy Cohenfde0db32008-04-21 15:42:01 -0700958 (tbl_type.ant_type == search_tbl->ant_type) &&
Zhu Yib481de92007-09-25 17:54:57 -0700959 (tbl_type.is_SGI == search_tbl->is_SGI)) {
960 if (search_tbl->expected_tpt)
961 tpt = search_tbl->expected_tpt[rs_index];
962 else
963 tpt = 0;
Wey-Yi Guydf36c042009-03-10 14:35:11 -0700964 if (info->flags & IEEE80211_TX_STAT_AMPDU)
Ron Rindjunsky99556432008-01-28 14:07:25 +0200965 rs_collect_tx_data(search_win, rs_index, tpt,
Johannes Berge039fa42008-05-15 12:55:29 +0200966 info->status.ampdu_ack_len,
967 info->status.ampdu_ack_map);
Ron Rindjunsky99556432008-01-28 14:07:25 +0200968 else
969 rs_collect_tx_data(search_win, rs_index, tpt,
970 1, status);
Ben Cahill77626352007-11-29 11:09:44 +0800971 /* Else if type matches "current/active" table,
972 * add final tx status to "current/active" history */
Zhu Yib481de92007-09-25 17:54:57 -0700973 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
Guy Cohenfde0db32008-04-21 15:42:01 -0700974 (tbl_type.ant_type == curr_tbl->ant_type) &&
Zhu Yib481de92007-09-25 17:54:57 -0700975 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
976 if (curr_tbl->expected_tpt)
977 tpt = curr_tbl->expected_tpt[rs_index];
978 else
979 tpt = 0;
Wey-Yi Guydf36c042009-03-10 14:35:11 -0700980 if (info->flags & IEEE80211_TX_STAT_AMPDU)
Ron Rindjunsky99556432008-01-28 14:07:25 +0200981 rs_collect_tx_data(window, rs_index, tpt,
Johannes Berge039fa42008-05-15 12:55:29 +0200982 info->status.ampdu_ack_len,
983 info->status.ampdu_ack_map);
Ron Rindjunsky99556432008-01-28 14:07:25 +0200984 else
985 rs_collect_tx_data(window, rs_index, tpt,
986 1, status);
Zhu Yib481de92007-09-25 17:54:57 -0700987 }
988
Ben Cahill77626352007-11-29 11:09:44 +0800989 /* If not searching for new mode, increment success/failed counter
990 * ... these help determine when to start searching again */
Tomas Winklerc33104f2008-01-14 17:46:21 -0800991 if (lq_sta->stay_in_tbl) {
Wey-Yi Guydf36c042009-03-10 14:35:11 -0700992 if (info->flags & IEEE80211_TX_STAT_AMPDU) {
Johannes Berge039fa42008-05-15 12:55:29 +0200993 lq_sta->total_success += info->status.ampdu_ack_map;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200994 lq_sta->total_failed +=
Johannes Berge039fa42008-05-15 12:55:29 +0200995 (info->status.ampdu_ack_len - info->status.ampdu_ack_map);
Ron Rindjunsky99556432008-01-28 14:07:25 +0200996 } else {
997 if (status)
998 lq_sta->total_success++;
999 else
1000 lq_sta->total_failed++;
1001 }
Zhu Yib481de92007-09-25 17:54:57 -07001002 }
1003
Ben Cahill77626352007-11-29 11:09:44 +08001004 /* See if there's a better rate or modulation mode to try. */
Abbas, Mohamedc338ba32009-01-21 10:58:02 -08001005 if (sta && sta->supp_rates[sband->band])
Gábor Stefanik514d65c2009-04-23 19:36:08 +02001006 rs_rate_scale_perform(priv, skb, sta, lq_sta);
Tomas Winklerf4d60822008-03-05 11:31:00 -08001007out:
Zhu Yib481de92007-09-25 17:54:57 -07001008 return;
1009}
1010
Ben Cahill77626352007-11-29 11:09:44 +08001011/*
1012 * Begin a period of staying with a selected modulation mode.
1013 * Set "stay_in_tbl" flag to prevent any mode switches.
1014 * Set frame tx success limits according to legacy vs. high-throughput,
1015 * and reset overall (spanning all rates) tx success history statistics.
1016 * These control how long we stay using same modulation mode before
1017 * searching for a new mode.
1018 */
Ester Kummerbf403db2008-05-05 10:22:40 +08001019static void rs_set_stay_in_table(struct iwl_priv *priv, u8 is_legacy,
Tomas Winklere227cea2008-07-18 13:53:05 +08001020 struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07001021{
Tomas Winklere1623442009-01-27 14:27:56 -08001022 IWL_DEBUG_RATE(priv, "we are staying in the same table\n");
Tomas Winklerc33104f2008-01-14 17:46:21 -08001023 lq_sta->stay_in_tbl = 1; /* only place this gets set */
Zhu Yib481de92007-09-25 17:54:57 -07001024 if (is_legacy) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001025 lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT;
1026 lq_sta->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
1027 lq_sta->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
Zhu Yib481de92007-09-25 17:54:57 -07001028 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001029 lq_sta->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
1030 lq_sta->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
1031 lq_sta->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
Zhu Yib481de92007-09-25 17:54:57 -07001032 }
Tomas Winklerc33104f2008-01-14 17:46:21 -08001033 lq_sta->table_count = 0;
1034 lq_sta->total_failed = 0;
1035 lq_sta->total_success = 0;
Mohamed Abbas447fee72009-04-20 14:37:02 -07001036 lq_sta->flush_timer = jiffies;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001037 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001038}
1039
Ben Cahill77626352007-11-29 11:09:44 +08001040/*
1041 * Find correct throughput table for given mode of modulation
1042 */
Tomas Winklere227cea2008-07-18 13:53:05 +08001043static void rs_set_expected_tpt_table(struct iwl_lq_sta *lq_sta,
1044 struct iwl_scale_tbl_info *tbl)
Zhu Yib481de92007-09-25 17:54:57 -07001045{
1046 if (is_legacy(tbl->lq_type)) {
1047 if (!is_a_band(tbl->lq_type))
1048 tbl->expected_tpt = expected_tpt_G;
1049 else
1050 tbl->expected_tpt = expected_tpt_A;
1051 } else if (is_siso(tbl->lq_type)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001052 if (tbl->is_fat && !lq_sta->is_dup)
Zhu Yib481de92007-09-25 17:54:57 -07001053 if (tbl->is_SGI)
1054 tbl->expected_tpt = expected_tpt_siso40MHzSGI;
1055 else
1056 tbl->expected_tpt = expected_tpt_siso40MHz;
1057 else if (tbl->is_SGI)
1058 tbl->expected_tpt = expected_tpt_siso20MHzSGI;
1059 else
1060 tbl->expected_tpt = expected_tpt_siso20MHz;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001061 } else if (is_mimo2(tbl->lq_type)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001062 if (tbl->is_fat && !lq_sta->is_dup)
Zhu Yib481de92007-09-25 17:54:57 -07001063 if (tbl->is_SGI)
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001064 tbl->expected_tpt = expected_tpt_mimo2_40MHzSGI;
Zhu Yib481de92007-09-25 17:54:57 -07001065 else
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001066 tbl->expected_tpt = expected_tpt_mimo2_40MHz;
Zhu Yib481de92007-09-25 17:54:57 -07001067 else if (tbl->is_SGI)
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001068 tbl->expected_tpt = expected_tpt_mimo2_20MHzSGI;
Zhu Yib481de92007-09-25 17:54:57 -07001069 else
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001070 tbl->expected_tpt = expected_tpt_mimo2_20MHz;
1071 } else if (is_mimo3(tbl->lq_type)) {
1072 if (tbl->is_fat && !lq_sta->is_dup)
1073 if (tbl->is_SGI)
1074 tbl->expected_tpt = expected_tpt_mimo3_40MHzSGI;
1075 else
1076 tbl->expected_tpt = expected_tpt_mimo3_40MHz;
1077 else if (tbl->is_SGI)
1078 tbl->expected_tpt = expected_tpt_mimo3_20MHzSGI;
1079 else
1080 tbl->expected_tpt = expected_tpt_mimo3_20MHz;
Zhu Yib481de92007-09-25 17:54:57 -07001081 } else
1082 tbl->expected_tpt = expected_tpt_G;
1083}
1084
Ben Cahill77626352007-11-29 11:09:44 +08001085/*
1086 * Find starting rate for new "search" high-throughput mode of modulation.
1087 * Goal is to find lowest expected rate (under perfect conditions) that is
1088 * above the current measured throughput of "active" mode, to give new mode
1089 * a fair chance to prove itself without too many challenges.
1090 *
1091 * This gets called when transitioning to more aggressive modulation
1092 * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
1093 * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
1094 * to decrease to match "active" throughput. When moving from MIMO to SISO,
1095 * bit rate will typically need to increase, but not if performance was bad.
1096 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001097static s32 rs_get_best_rate(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001098 struct iwl_lq_sta *lq_sta,
1099 struct iwl_scale_tbl_info *tbl, /* "search" */
Guy Cohenf935a6d2008-04-23 17:15:02 -07001100 u16 rate_mask, s8 index)
Zhu Yib481de92007-09-25 17:54:57 -07001101{
Ben Cahill77626352007-11-29 11:09:44 +08001102 /* "active" values */
Tomas Winklere227cea2008-07-18 13:53:05 +08001103 struct iwl_scale_tbl_info *active_tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001104 &(lq_sta->lq_info[lq_sta->active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07001105 s32 active_sr = active_tbl->win[index].success_ratio;
Zhu Yib481de92007-09-25 17:54:57 -07001106 s32 active_tpt = active_tbl->expected_tpt[index];
Ben Cahill77626352007-11-29 11:09:44 +08001107
1108 /* expected "search" throughput */
1109 s32 *tpt_tbl = tbl->expected_tpt;
1110
1111 s32 new_rate, high, low, start_hi;
Zhu Yib481de92007-09-25 17:54:57 -07001112 u16 high_low;
Guy Cohenf935a6d2008-04-23 17:15:02 -07001113 s8 rate = index;
Zhu Yib481de92007-09-25 17:54:57 -07001114
1115 new_rate = high = low = start_hi = IWL_RATE_INVALID;
1116
1117 for (; ;) {
Ester Kummerbf403db2008-05-05 10:22:40 +08001118 high_low = rs_get_adjacent_rate(priv, rate, rate_mask,
1119 tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -07001120
1121 low = high_low & 0xff;
1122 high = (high_low >> 8) & 0xff;
1123
Ben Cahill77626352007-11-29 11:09:44 +08001124 /*
1125 * Lower the "search" bit rate, to give new "search" mode
1126 * approximately the same throughput as "active" if:
1127 *
1128 * 1) "Active" mode has been working modestly well (but not
1129 * great), and expected "search" throughput (under perfect
1130 * conditions) at candidate rate is above the actual
1131 * measured "active" throughput (but less than expected
1132 * "active" throughput under perfect conditions).
1133 * OR
1134 * 2) "Active" mode has been working perfectly or very well
1135 * and expected "search" throughput (under perfect
1136 * conditions) at candidate rate is above expected
1137 * "active" throughput (under perfect conditions).
1138 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001139 if ((((100 * tpt_tbl[rate]) > lq_sta->last_tpt) &&
Zhu Yib481de92007-09-25 17:54:57 -07001140 ((active_sr > IWL_RATE_DECREASE_TH) &&
1141 (active_sr <= IWL_RATE_HIGH_TH) &&
1142 (tpt_tbl[rate] <= active_tpt))) ||
1143 ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
1144 (tpt_tbl[rate] > active_tpt))) {
1145
Ben Cahill77626352007-11-29 11:09:44 +08001146 /* (2nd or later pass)
1147 * If we've already tried to raise the rate, and are
1148 * now trying to lower it, use the higher rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001149 if (start_hi != IWL_RATE_INVALID) {
1150 new_rate = start_hi;
1151 break;
1152 }
Ben Cahill77626352007-11-29 11:09:44 +08001153
Zhu Yib481de92007-09-25 17:54:57 -07001154 new_rate = rate;
Ben Cahill77626352007-11-29 11:09:44 +08001155
1156 /* Loop again with lower rate */
Zhu Yib481de92007-09-25 17:54:57 -07001157 if (low != IWL_RATE_INVALID)
1158 rate = low;
Ben Cahill77626352007-11-29 11:09:44 +08001159
1160 /* Lower rate not available, use the original */
Zhu Yib481de92007-09-25 17:54:57 -07001161 else
1162 break;
Ben Cahill77626352007-11-29 11:09:44 +08001163
1164 /* Else try to raise the "search" rate to match "active" */
Zhu Yib481de92007-09-25 17:54:57 -07001165 } else {
Ben Cahill77626352007-11-29 11:09:44 +08001166 /* (2nd or later pass)
1167 * If we've already tried to lower the rate, and are
1168 * now trying to raise it, use the lower rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001169 if (new_rate != IWL_RATE_INVALID)
1170 break;
Ben Cahill77626352007-11-29 11:09:44 +08001171
1172 /* Loop again with higher rate */
Zhu Yib481de92007-09-25 17:54:57 -07001173 else if (high != IWL_RATE_INVALID) {
1174 start_hi = high;
1175 rate = high;
Ben Cahill77626352007-11-29 11:09:44 +08001176
1177 /* Higher rate not available, use the original */
Zhu Yib481de92007-09-25 17:54:57 -07001178 } else {
Guy Cohen90d77952008-09-09 10:54:53 +08001179 new_rate = rate;
Zhu Yib481de92007-09-25 17:54:57 -07001180 break;
1181 }
1182 }
1183 }
1184
1185 return new_rate;
1186}
Zhu Yib481de92007-09-25 17:54:57 -07001187
Ben Cahill77626352007-11-29 11:09:44 +08001188/*
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001189 * Set up search table for MIMO2
Ben Cahill77626352007-11-29 11:09:44 +08001190 */
Guy Cohenfde0db32008-04-21 15:42:01 -07001191static int rs_switch_to_mimo2(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001192 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001193 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001194 struct ieee80211_sta *sta,
Tomas Winklere227cea2008-07-18 13:53:05 +08001195 struct iwl_scale_tbl_info *tbl, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001196{
Zhu Yib481de92007-09-25 17:54:57 -07001197 u16 rate_mask;
1198 s32 rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001199 s8 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07001200
Luis R. Rodriguezde27e642008-12-23 15:58:44 -08001201 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
Zhu Yib481de92007-09-25 17:54:57 -07001202 return -1;
1203
Johannes Bergd9fe60d2008-10-09 12:13:49 +02001204 if (((sta->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 2)
Tomas Winkler00c5ae22008-09-03 11:26:42 +08001205 == WLAN_HT_CAP_SM_PS_STATIC)
Zhu Yib481de92007-09-25 17:54:57 -07001206 return -1;
1207
Ben Cahill77626352007-11-29 11:09:44 +08001208 /* Need both Tx chains/antennas to support MIMO */
Guy Cohenaade00c2008-04-23 17:14:59 -07001209 if (priv->hw_params.tx_chains_num < 2)
Zhu Yib481de92007-09-25 17:54:57 -07001210 return -1;
1211
Tomas Winklere1623442009-01-27 14:27:56 -08001212 IWL_DEBUG_RATE(priv, "LQ: try to switch to MIMO2\n");
Guy Cohen39e88502008-04-23 17:14:57 -07001213
1214 tbl->lq_type = LQ_MIMO2;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001215 tbl->is_dup = lq_sta->is_dup;
Zhu Yib481de92007-09-25 17:54:57 -07001216 tbl->action = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001217 tbl->max_search = IWL_MAX_SEARCH;
Guy Cohen39e88502008-04-23 17:14:57 -07001218 rate_mask = lq_sta->active_mimo2_rate;
1219
Wey-Yi Guya30199f2009-04-30 13:56:28 -07001220 if (iwl_is_fat_tx_allowed(priv, &sta->ht_cap))
Zhu Yib481de92007-09-25 17:54:57 -07001221 tbl->is_fat = 1;
1222 else
1223 tbl->is_fat = 0;
1224
Guy Cohen39e88502008-04-23 17:14:57 -07001225 /* FIXME: - don't toggle SGI here
Zhu Yib481de92007-09-25 17:54:57 -07001226 if (tbl->is_fat) {
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001227 if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
Zhu Yib481de92007-09-25 17:54:57 -07001228 tbl->is_SGI = 1;
1229 else
1230 tbl->is_SGI = 0;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001231 } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
Zhu Yib481de92007-09-25 17:54:57 -07001232 tbl->is_SGI = 1;
1233 else
1234 tbl->is_SGI = 0;
Guy Cohen39e88502008-04-23 17:14:57 -07001235 */
Zhu Yib481de92007-09-25 17:54:57 -07001236
Guy Coheneecd6e52008-04-23 17:14:58 -07001237 rs_set_expected_tpt_table(lq_sta, tbl);
Zhu Yib481de92007-09-25 17:54:57 -07001238
Guy Cohenf935a6d2008-04-23 17:15:02 -07001239 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
Zhu Yib481de92007-09-25 17:54:57 -07001240
Tomas Winklere1623442009-01-27 14:27:56 -08001241 IWL_DEBUG_RATE(priv, "LQ: MIMO2 best rate %d mask %X\n", rate, rate_mask);
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001242 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1243 IWL_DEBUG_RATE(priv, "Can't switch with index %d rate mask %x\n",
1244 rate, rate_mask);
1245 return -1;
1246 }
1247 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
Guy Cohen39e88502008-04-23 17:14:57 -07001248
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001249 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
1250 tbl->current_rate, is_green);
1251 return 0;
1252}
1253
1254/*
1255 * Set up search table for MIMO3
1256 */
1257static int rs_switch_to_mimo3(struct iwl_priv *priv,
1258 struct iwl_lq_sta *lq_sta,
1259 struct ieee80211_conf *conf,
1260 struct ieee80211_sta *sta,
1261 struct iwl_scale_tbl_info *tbl, int index)
1262{
1263 u16 rate_mask;
1264 s32 rate;
1265 s8 is_green = lq_sta->is_green;
1266
1267 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
1268 return -1;
1269
1270 if (((sta->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 2)
1271 == WLAN_HT_CAP_SM_PS_STATIC)
1272 return -1;
1273
1274 /* Need both Tx chains/antennas to support MIMO */
1275 if (priv->hw_params.tx_chains_num < 3)
1276 return -1;
1277
1278 IWL_DEBUG_RATE(priv, "LQ: try to switch to MIMO3\n");
1279
1280 tbl->lq_type = LQ_MIMO3;
1281 tbl->is_dup = lq_sta->is_dup;
1282 tbl->action = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001283 tbl->max_search = IWL_MAX_11N_MIMO3_SEARCH;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001284 rate_mask = lq_sta->active_mimo3_rate;
1285
Wey-Yi Guya30199f2009-04-30 13:56:28 -07001286 if (iwl_is_fat_tx_allowed(priv, &sta->ht_cap))
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001287 tbl->is_fat = 1;
1288 else
1289 tbl->is_fat = 0;
1290
1291 /* FIXME: - don't toggle SGI here
1292 if (tbl->is_fat) {
1293 if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
1294 tbl->is_SGI = 1;
1295 else
1296 tbl->is_SGI = 0;
1297 } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
1298 tbl->is_SGI = 1;
1299 else
1300 tbl->is_SGI = 0;
1301 */
1302
1303 rs_set_expected_tpt_table(lq_sta, tbl);
1304
1305 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
1306
1307 IWL_DEBUG_RATE(priv, "LQ: MIMO3 best rate %d mask %X\n",
1308 rate, rate_mask);
Guy Cohen39e88502008-04-23 17:14:57 -07001309 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
Tomas Winklere1623442009-01-27 14:27:56 -08001310 IWL_DEBUG_RATE(priv, "Can't switch with index %d rate mask %x\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001311 rate, rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07001312 return -1;
Guy Cohen39e88502008-04-23 17:14:57 -07001313 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001314 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
Zhu Yib481de92007-09-25 17:54:57 -07001315
Tomas Winklere1623442009-01-27 14:27:56 -08001316 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001317 tbl->current_rate, is_green);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001318 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07001319}
1320
Ben Cahill77626352007-11-29 11:09:44 +08001321/*
1322 * Set up search table for SISO
1323 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001324static int rs_switch_to_siso(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001325 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001326 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001327 struct ieee80211_sta *sta,
Tomas Winklere227cea2008-07-18 13:53:05 +08001328 struct iwl_scale_tbl_info *tbl, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001329{
Zhu Yib481de92007-09-25 17:54:57 -07001330 u16 rate_mask;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001331 u8 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07001332 s32 rate;
1333
Luis R. Rodriguezde27e642008-12-23 15:58:44 -08001334 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
Zhu Yib481de92007-09-25 17:54:57 -07001335 return -1;
1336
Tomas Winklere1623442009-01-27 14:27:56 -08001337 IWL_DEBUG_RATE(priv, "LQ: try to switch to SISO\n");
Guy Cohen39e88502008-04-23 17:14:57 -07001338
Tomas Winklerc33104f2008-01-14 17:46:21 -08001339 tbl->is_dup = lq_sta->is_dup;
Zhu Yib481de92007-09-25 17:54:57 -07001340 tbl->lq_type = LQ_SISO;
1341 tbl->action = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001342 tbl->max_search = IWL_MAX_SEARCH;
Guy Cohen39e88502008-04-23 17:14:57 -07001343 rate_mask = lq_sta->active_siso_rate;
Zhu Yib481de92007-09-25 17:54:57 -07001344
Wey-Yi Guya30199f2009-04-30 13:56:28 -07001345 if (iwl_is_fat_tx_allowed(priv, &sta->ht_cap))
Zhu Yib481de92007-09-25 17:54:57 -07001346 tbl->is_fat = 1;
1347 else
1348 tbl->is_fat = 0;
1349
Guy Cohen39e88502008-04-23 17:14:57 -07001350 /* FIXME: - don't toggle SGI here
Zhu Yib481de92007-09-25 17:54:57 -07001351 if (tbl->is_fat) {
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001352 if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
Zhu Yib481de92007-09-25 17:54:57 -07001353 tbl->is_SGI = 1;
1354 else
1355 tbl->is_SGI = 0;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001356 } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
Zhu Yib481de92007-09-25 17:54:57 -07001357 tbl->is_SGI = 1;
1358 else
1359 tbl->is_SGI = 0;
Guy Cohen39e88502008-04-23 17:14:57 -07001360 */
Zhu Yib481de92007-09-25 17:54:57 -07001361
1362 if (is_green)
Guy Cohen39e88502008-04-23 17:14:57 -07001363 tbl->is_SGI = 0; /*11n spec: no SGI in SISO+Greenfield*/
Zhu Yib481de92007-09-25 17:54:57 -07001364
Guy Coheneecd6e52008-04-23 17:14:58 -07001365 rs_set_expected_tpt_table(lq_sta, tbl);
Guy Cohenf935a6d2008-04-23 17:15:02 -07001366 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
Zhu Yib481de92007-09-25 17:54:57 -07001367
Tomas Winklere1623442009-01-27 14:27:56 -08001368 IWL_DEBUG_RATE(priv, "LQ: get best rate %d mask %X\n", rate, rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07001369 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
Tomas Winklere1623442009-01-27 14:27:56 -08001370 IWL_DEBUG_RATE(priv, "can not switch with index %d rate mask %x\n",
Zhu Yib481de92007-09-25 17:54:57 -07001371 rate, rate_mask);
1372 return -1;
1373 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001374 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
Tomas Winklere1623442009-01-27 14:27:56 -08001375 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001376 tbl->current_rate, is_green);
Mohamed Abbas403ab562007-11-06 22:06:25 -08001377 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07001378}
1379
Ben Cahill77626352007-11-29 11:09:44 +08001380/*
1381 * Try to switch to new modulation mode from legacy
1382 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001383static int rs_move_legacy_other(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001384 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001385 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001386 struct ieee80211_sta *sta,
Zhu Yib481de92007-09-25 17:54:57 -07001387 int index)
1388{
Tomas Winklere227cea2008-07-18 13:53:05 +08001389 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1390 struct iwl_scale_tbl_info *search_tbl =
1391 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1392 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1393 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1394 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001395 u8 start_action = tbl->action;
Guy Cohenfde0db32008-04-21 15:42:01 -07001396 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
Guy Cohen3110bef2008-09-09 10:54:54 +08001397 u8 tx_chains_num = priv->hw_params.tx_chains_num;
Guy Cohenfde0db32008-04-21 15:42:01 -07001398 int ret = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001399 u8 update_search_tbl_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001400
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001401 if (!iwl_ht_enabled(priv))
1402 /* stay in Legacy */
1403 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
1404 else if (iwl_tx_ant_restriction(priv) == IWL_TX_SINGLE &&
1405 tbl->action > IWL_LEGACY_SWITCH_SISO)
1406 tbl->action = IWL_LEGACY_SWITCH_SISO;
Zhu Yib481de92007-09-25 17:54:57 -07001407 for (; ;) {
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001408 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001409 switch (tbl->action) {
Guy Cohen3110bef2008-09-09 10:54:54 +08001410 case IWL_LEGACY_SWITCH_ANTENNA1:
1411 case IWL_LEGACY_SWITCH_ANTENNA2:
Tomas Winklere1623442009-01-27 14:27:56 -08001412 IWL_DEBUG_RATE(priv, "LQ: Legacy toggle Antenna\n");
Zhu Yib481de92007-09-25 17:54:57 -07001413
Guy Cohen3110bef2008-09-09 10:54:54 +08001414 if ((tbl->action == IWL_LEGACY_SWITCH_ANTENNA1 &&
1415 tx_chains_num <= 1) ||
1416 (tbl->action == IWL_LEGACY_SWITCH_ANTENNA2 &&
1417 tx_chains_num <= 2))
1418 break;
1419
Ben Cahill77626352007-11-29 11:09:44 +08001420 /* Don't change antenna if success has been great */
Zhu Yib481de92007-09-25 17:54:57 -07001421 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1422 break;
Ben Cahill77626352007-11-29 11:09:44 +08001423
Ben Cahill77626352007-11-29 11:09:44 +08001424 /* Set up search table to try other antenna */
Zhu Yib481de92007-09-25 17:54:57 -07001425 memcpy(search_tbl, tbl, sz);
1426
Guy Cohenaade00c2008-04-23 17:14:59 -07001427 if (rs_toggle_antenna(valid_tx_ant,
1428 &search_tbl->current_rate, search_tbl)) {
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001429 update_search_tbl_counter = 1;
Guy Cohen3110bef2008-09-09 10:54:54 +08001430 rs_set_expected_tpt_table(lq_sta, search_tbl);
Guy Cohenaade00c2008-04-23 17:14:59 -07001431 goto out;
1432 }
Guy Cohena5e8b502008-05-29 16:35:11 +08001433 break;
Zhu Yib481de92007-09-25 17:54:57 -07001434 case IWL_LEGACY_SWITCH_SISO:
Tomas Winklere1623442009-01-27 14:27:56 -08001435 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to SISO\n");
Ben Cahill77626352007-11-29 11:09:44 +08001436
1437 /* Set up search table to try SISO */
Zhu Yib481de92007-09-25 17:54:57 -07001438 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001439 search_tbl->is_SGI = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001440 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001441 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001442 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001443 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001444 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001445 }
Zhu Yib481de92007-09-25 17:54:57 -07001446
1447 break;
Guy Cohen3110bef2008-09-09 10:54:54 +08001448 case IWL_LEGACY_SWITCH_MIMO2_AB:
1449 case IWL_LEGACY_SWITCH_MIMO2_AC:
1450 case IWL_LEGACY_SWITCH_MIMO2_BC:
Tomas Winklere1623442009-01-27 14:27:56 -08001451 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to MIMO2\n");
Ben Cahill77626352007-11-29 11:09:44 +08001452
1453 /* Set up search table to try MIMO */
Zhu Yib481de92007-09-25 17:54:57 -07001454 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001455 search_tbl->is_SGI = 0;
Guy Cohen3110bef2008-09-09 10:54:54 +08001456
1457 if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AB)
1458 search_tbl->ant_type = ANT_AB;
1459 else if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AC)
1460 search_tbl->ant_type = ANT_AC;
1461 else
1462 search_tbl->ant_type = ANT_BC;
1463
1464 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1465 break;
1466
Guy Cohenfde0db32008-04-21 15:42:01 -07001467 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001468 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001469 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001470 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001471 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001472 }
Zhu Yib481de92007-09-25 17:54:57 -07001473 break;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001474
1475 case IWL_LEGACY_SWITCH_MIMO3_ABC:
1476 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to MIMO3\n");
1477
1478 /* Set up search table to try MIMO3 */
1479 memcpy(search_tbl, tbl, sz);
1480 search_tbl->is_SGI = 0;
1481
1482 search_tbl->ant_type = ANT_ABC;
1483
1484 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1485 break;
1486
1487 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1488 search_tbl, index);
1489 if (!ret) {
1490 lq_sta->action_counter = 0;
1491 goto out;
1492 }
1493 break;
Zhu Yib481de92007-09-25 17:54:57 -07001494 }
1495 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001496 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001497 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
Zhu Yib481de92007-09-25 17:54:57 -07001498
1499 if (tbl->action == start_action)
1500 break;
1501
1502 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001503 search_tbl->lq_type = LQ_NONE;
Zhu Yib481de92007-09-25 17:54:57 -07001504 return 0;
1505
Guy Cohen3110bef2008-09-09 10:54:54 +08001506out:
1507 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001508 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001509 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001510 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001511 if (update_search_tbl_counter)
1512 search_tbl->action = tbl->action;
Zhu Yib481de92007-09-25 17:54:57 -07001513 return 0;
1514
1515}
1516
Ben Cahill77626352007-11-29 11:09:44 +08001517/*
1518 * Try to switch to new modulation mode from SISO
1519 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001520static int rs_move_siso_to_other(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001521 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001522 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001523 struct ieee80211_sta *sta, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001524{
Tomas Winklerc33104f2008-01-14 17:46:21 -08001525 u8 is_green = lq_sta->is_green;
Tomas Winklere227cea2008-07-18 13:53:05 +08001526 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1527 struct iwl_scale_tbl_info *search_tbl =
1528 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1529 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1530 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1531 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001532 u8 start_action = tbl->action;
Guy Cohenfde0db32008-04-21 15:42:01 -07001533 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
Guy Cohen3110bef2008-09-09 10:54:54 +08001534 u8 tx_chains_num = priv->hw_params.tx_chains_num;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001535 u8 update_search_tbl_counter = 0;
Guy Cohenfde0db32008-04-21 15:42:01 -07001536 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001537
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001538 if (iwl_tx_ant_restriction(priv) == IWL_TX_SINGLE &&
1539 tbl->action > IWL_SISO_SWITCH_ANTENNA2) {
1540 /* stay in SISO */
1541 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1542 }
Zhu Yib481de92007-09-25 17:54:57 -07001543 for (;;) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001544 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001545 switch (tbl->action) {
Guy Cohen3110bef2008-09-09 10:54:54 +08001546 case IWL_SISO_SWITCH_ANTENNA1:
1547 case IWL_SISO_SWITCH_ANTENNA2:
Tomas Winklere1623442009-01-27 14:27:56 -08001548 IWL_DEBUG_RATE(priv, "LQ: SISO toggle Antenna\n");
Guy Cohen3110bef2008-09-09 10:54:54 +08001549
1550 if ((tbl->action == IWL_SISO_SWITCH_ANTENNA1 &&
1551 tx_chains_num <= 1) ||
1552 (tbl->action == IWL_SISO_SWITCH_ANTENNA2 &&
1553 tx_chains_num <= 2))
1554 break;
1555
Zhu Yib481de92007-09-25 17:54:57 -07001556 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1557 break;
Zhu Yib481de92007-09-25 17:54:57 -07001558
1559 memcpy(search_tbl, tbl, sz);
Guy Cohenaade00c2008-04-23 17:14:59 -07001560 if (rs_toggle_antenna(valid_tx_ant,
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001561 &search_tbl->current_rate, search_tbl)) {
1562 update_search_tbl_counter = 1;
Guy Cohenaade00c2008-04-23 17:14:59 -07001563 goto out;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001564 }
Guy Cohena5e8b502008-05-29 16:35:11 +08001565 break;
Guy Cohen3110bef2008-09-09 10:54:54 +08001566 case IWL_SISO_SWITCH_MIMO2_AB:
1567 case IWL_SISO_SWITCH_MIMO2_AC:
1568 case IWL_SISO_SWITCH_MIMO2_BC:
Tomas Winklere1623442009-01-27 14:27:56 -08001569 IWL_DEBUG_RATE(priv, "LQ: SISO switch to MIMO2\n");
Zhu Yib481de92007-09-25 17:54:57 -07001570 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001571 search_tbl->is_SGI = 0;
Guy Cohen3110bef2008-09-09 10:54:54 +08001572
1573 if (tbl->action == IWL_SISO_SWITCH_MIMO2_AB)
1574 search_tbl->ant_type = ANT_AB;
1575 else if (tbl->action == IWL_SISO_SWITCH_MIMO2_AC)
1576 search_tbl->ant_type = ANT_AC;
1577 else
1578 search_tbl->ant_type = ANT_BC;
1579
1580 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1581 break;
1582
Guy Cohenfde0db32008-04-21 15:42:01 -07001583 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001584 search_tbl, index);
Guy Cohen3110bef2008-09-09 10:54:54 +08001585 if (!ret)
Zhu Yib481de92007-09-25 17:54:57 -07001586 goto out;
1587 break;
1588 case IWL_SISO_SWITCH_GI:
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001589 if (!tbl->is_fat &&
1590 !(priv->current_ht_config.sgf &
1591 HT_SHORT_GI_20MHZ))
1592 break;
1593 if (tbl->is_fat &&
1594 !(priv->current_ht_config.sgf &
1595 HT_SHORT_GI_40MHZ))
1596 break;
1597
Tomas Winklere1623442009-01-27 14:27:56 -08001598 IWL_DEBUG_RATE(priv, "LQ: SISO toggle SGI/NGI\n");
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001599
Zhu Yib481de92007-09-25 17:54:57 -07001600 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001601 if (is_green) {
1602 if (!tbl->is_SGI)
1603 break;
1604 else
Winkler, Tomas15b16872008-12-19 10:37:33 +08001605 IWL_ERR(priv,
1606 "SGI was set in GF+SISO\n");
Zhu Yib481de92007-09-25 17:54:57 -07001607 }
Guy Cohen39e88502008-04-23 17:14:57 -07001608 search_tbl->is_SGI = !tbl->is_SGI;
Guy Coheneecd6e52008-04-23 17:14:58 -07001609 rs_set_expected_tpt_table(lq_sta, search_tbl);
Guy Cohen39e88502008-04-23 17:14:57 -07001610 if (tbl->is_SGI) {
1611 s32 tpt = lq_sta->last_tpt / 100;
1612 if (tpt >= search_tbl->expected_tpt[index])
1613 break;
1614 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001615 search_tbl->current_rate =
1616 rate_n_flags_from_tbl(priv, search_tbl,
1617 index, is_green);
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001618 update_search_tbl_counter = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001619 goto out;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001620 case IWL_SISO_SWITCH_MIMO3_ABC:
1621 IWL_DEBUG_RATE(priv, "LQ: SISO switch to MIMO3\n");
1622 memcpy(search_tbl, tbl, sz);
1623 search_tbl->is_SGI = 0;
1624 search_tbl->ant_type = ANT_ABC;
1625
1626 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1627 break;
1628
1629 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1630 search_tbl, index);
1631 if (!ret)
1632 goto out;
1633 break;
Zhu Yib481de92007-09-25 17:54:57 -07001634 }
1635 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001636 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001637 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
Zhu Yib481de92007-09-25 17:54:57 -07001638
1639 if (tbl->action == start_action)
1640 break;
1641 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001642 search_tbl->lq_type = LQ_NONE;
Zhu Yib481de92007-09-25 17:54:57 -07001643 return 0;
1644
1645 out:
Guy Cohen3110bef2008-09-09 10:54:54 +08001646 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001647 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001648 if (tbl->action > IWL_SISO_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001649 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001650 if (update_search_tbl_counter)
1651 search_tbl->action = tbl->action;
1652
Zhu Yib481de92007-09-25 17:54:57 -07001653 return 0;
1654}
1655
Ben Cahill77626352007-11-29 11:09:44 +08001656/*
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001657 * Try to switch to new modulation mode from MIMO2
Ben Cahill77626352007-11-29 11:09:44 +08001658 */
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001659static int rs_move_mimo2_to_other(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001660 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001661 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001662 struct ieee80211_sta *sta, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001663{
Tomas Winklerc33104f2008-01-14 17:46:21 -08001664 s8 is_green = lq_sta->is_green;
Tomas Winklere227cea2008-07-18 13:53:05 +08001665 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1666 struct iwl_scale_tbl_info *search_tbl =
1667 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Guy Cohen3110bef2008-09-09 10:54:54 +08001668 struct iwl_rate_scale_data *window = &(tbl->win[index]);
Tomas Winklere227cea2008-07-18 13:53:05 +08001669 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1670 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001671 u8 start_action = tbl->action;
Guy Cohen3110bef2008-09-09 10:54:54 +08001672 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1673 u8 tx_chains_num = priv->hw_params.tx_chains_num;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001674 u8 update_search_tbl_counter = 0;
Guy Cohenaade00c2008-04-23 17:14:59 -07001675 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001676
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001677 if ((iwl_tx_ant_restriction(priv) == IWL_TX_SINGLE) &&
1678 (tbl->action < IWL_MIMO2_SWITCH_SISO_A ||
1679 tbl->action > IWL_MIMO2_SWITCH_SISO_C)) {
1680 /* switch in SISO */
1681 tbl->action = IWL_MIMO2_SWITCH_SISO_A;
1682 }
Zhu Yib481de92007-09-25 17:54:57 -07001683 for (;;) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001684 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001685 switch (tbl->action) {
Guy Cohen3110bef2008-09-09 10:54:54 +08001686 case IWL_MIMO2_SWITCH_ANTENNA1:
1687 case IWL_MIMO2_SWITCH_ANTENNA2:
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001688 IWL_DEBUG_RATE(priv, "LQ: MIMO2 toggle Antennas\n");
Guy Cohen3110bef2008-09-09 10:54:54 +08001689
1690 if (tx_chains_num <= 2)
1691 break;
1692
1693 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1694 break;
1695
1696 memcpy(search_tbl, tbl, sz);
1697 if (rs_toggle_antenna(valid_tx_ant,
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001698 &search_tbl->current_rate, search_tbl)) {
1699 update_search_tbl_counter = 1;
Guy Cohen3110bef2008-09-09 10:54:54 +08001700 goto out;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001701 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001702 break;
1703 case IWL_MIMO2_SWITCH_SISO_A:
1704 case IWL_MIMO2_SWITCH_SISO_B:
1705 case IWL_MIMO2_SWITCH_SISO_C:
Tomas Winklere1623442009-01-27 14:27:56 -08001706 IWL_DEBUG_RATE(priv, "LQ: MIMO2 switch to SISO\n");
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001707
Ben Cahill77626352007-11-29 11:09:44 +08001708 /* Set up new search table for SISO */
Zhu Yib481de92007-09-25 17:54:57 -07001709 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001710
Guy Cohen3110bef2008-09-09 10:54:54 +08001711 if (tbl->action == IWL_MIMO2_SWITCH_SISO_A)
Guy Cohenfde0db32008-04-21 15:42:01 -07001712 search_tbl->ant_type = ANT_A;
Guy Cohen3110bef2008-09-09 10:54:54 +08001713 else if (tbl->action == IWL_MIMO2_SWITCH_SISO_B)
Guy Cohenfde0db32008-04-21 15:42:01 -07001714 search_tbl->ant_type = ANT_B;
Guy Cohen3110bef2008-09-09 10:54:54 +08001715 else
1716 search_tbl->ant_type = ANT_C;
1717
1718 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1719 break;
Zhu Yib481de92007-09-25 17:54:57 -07001720
Tomas Winklerc33104f2008-01-14 17:46:21 -08001721 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001722 search_tbl, index);
Guy Cohen3110bef2008-09-09 10:54:54 +08001723 if (!ret)
Zhu Yib481de92007-09-25 17:54:57 -07001724 goto out;
Guy Cohen3110bef2008-09-09 10:54:54 +08001725
Zhu Yib481de92007-09-25 17:54:57 -07001726 break;
1727
Guy Cohen3110bef2008-09-09 10:54:54 +08001728 case IWL_MIMO2_SWITCH_GI:
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001729 if (!tbl->is_fat &&
1730 !(priv->current_ht_config.sgf &
1731 HT_SHORT_GI_20MHZ))
1732 break;
1733 if (tbl->is_fat &&
1734 !(priv->current_ht_config.sgf &
1735 HT_SHORT_GI_40MHZ))
1736 break;
1737
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001738 IWL_DEBUG_RATE(priv, "LQ: MIMO2 toggle SGI/NGI\n");
1739
1740 /* Set up new search table for MIMO2 */
1741 memcpy(search_tbl, tbl, sz);
1742 search_tbl->is_SGI = !tbl->is_SGI;
1743 rs_set_expected_tpt_table(lq_sta, search_tbl);
1744 /*
1745 * If active table already uses the fastest possible
1746 * modulation (dual stream with short guard interval),
1747 * and it's working well, there's no need to look
1748 * for a better type of modulation!
1749 */
1750 if (tbl->is_SGI) {
1751 s32 tpt = lq_sta->last_tpt / 100;
1752 if (tpt >= search_tbl->expected_tpt[index])
1753 break;
1754 }
1755 search_tbl->current_rate =
1756 rate_n_flags_from_tbl(priv, search_tbl,
1757 index, is_green);
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001758 update_search_tbl_counter = 1;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001759 goto out;
1760
1761 case IWL_MIMO2_SWITCH_MIMO3_ABC:
1762 IWL_DEBUG_RATE(priv, "LQ: MIMO2 switch to MIMO3\n");
1763 memcpy(search_tbl, tbl, sz);
1764 search_tbl->is_SGI = 0;
1765 search_tbl->ant_type = ANT_ABC;
1766
1767 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1768 break;
1769
1770 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1771 search_tbl, index);
1772 if (!ret)
1773 goto out;
1774
1775 break;
1776 }
1777 tbl->action++;
1778 if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
1779 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
1780
1781 if (tbl->action == start_action)
1782 break;
1783 }
1784 search_tbl->lq_type = LQ_NONE;
1785 return 0;
1786 out:
1787 lq_sta->search_better_tbl = 1;
1788 tbl->action++;
1789 if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
1790 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001791 if (update_search_tbl_counter)
1792 search_tbl->action = tbl->action;
1793
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001794 return 0;
1795
1796}
1797
1798/*
1799 * Try to switch to new modulation mode from MIMO3
1800 */
1801static int rs_move_mimo3_to_other(struct iwl_priv *priv,
1802 struct iwl_lq_sta *lq_sta,
1803 struct ieee80211_conf *conf,
1804 struct ieee80211_sta *sta, int index)
1805{
1806 s8 is_green = lq_sta->is_green;
1807 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1808 struct iwl_scale_tbl_info *search_tbl =
1809 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1810 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1811 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1812 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1813 u8 start_action = tbl->action;
1814 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1815 u8 tx_chains_num = priv->hw_params.tx_chains_num;
1816 int ret;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001817 u8 update_search_tbl_counter = 0;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001818
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001819 if ((iwl_tx_ant_restriction(priv) == IWL_TX_SINGLE) &&
1820 (tbl->action < IWL_MIMO3_SWITCH_SISO_A ||
1821 tbl->action > IWL_MIMO3_SWITCH_SISO_C)) {
1822 /* switch in SISO */
1823 tbl->action = IWL_MIMO3_SWITCH_SISO_A;
1824 }
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001825 for (;;) {
1826 lq_sta->action_counter++;
1827 switch (tbl->action) {
1828 case IWL_MIMO3_SWITCH_ANTENNA1:
1829 case IWL_MIMO3_SWITCH_ANTENNA2:
1830 IWL_DEBUG_RATE(priv, "LQ: MIMO3 toggle Antennas\n");
1831
1832 if (tx_chains_num <= 3)
1833 break;
1834
1835 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1836 break;
1837
1838 memcpy(search_tbl, tbl, sz);
1839 if (rs_toggle_antenna(valid_tx_ant,
1840 &search_tbl->current_rate, search_tbl))
1841 goto out;
1842 break;
1843 case IWL_MIMO3_SWITCH_SISO_A:
1844 case IWL_MIMO3_SWITCH_SISO_B:
1845 case IWL_MIMO3_SWITCH_SISO_C:
1846 IWL_DEBUG_RATE(priv, "LQ: MIMO3 switch to SISO\n");
1847
1848 /* Set up new search table for SISO */
1849 memcpy(search_tbl, tbl, sz);
1850
1851 if (tbl->action == IWL_MIMO3_SWITCH_SISO_A)
1852 search_tbl->ant_type = ANT_A;
1853 else if (tbl->action == IWL_MIMO3_SWITCH_SISO_B)
1854 search_tbl->ant_type = ANT_B;
1855 else
1856 search_tbl->ant_type = ANT_C;
1857
1858 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1859 break;
1860
1861 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
1862 search_tbl, index);
1863 if (!ret)
1864 goto out;
1865
1866 break;
1867
1868 case IWL_MIMO3_SWITCH_MIMO2_AB:
1869 case IWL_MIMO3_SWITCH_MIMO2_AC:
1870 case IWL_MIMO3_SWITCH_MIMO2_BC:
1871 IWL_DEBUG_RATE(priv, "LQ: MIMO3 switch to MIMO2\n");
1872
1873 memcpy(search_tbl, tbl, sz);
1874 search_tbl->is_SGI = 0;
1875 if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AB)
1876 search_tbl->ant_type = ANT_AB;
1877 else if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AC)
1878 search_tbl->ant_type = ANT_AC;
1879 else
1880 search_tbl->ant_type = ANT_BC;
1881
1882 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1883 break;
1884
1885 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
1886 search_tbl, index);
1887 if (!ret)
1888 goto out;
1889
1890 break;
1891
1892 case IWL_MIMO3_SWITCH_GI:
1893 if (!tbl->is_fat &&
1894 !(priv->current_ht_config.sgf &
1895 HT_SHORT_GI_20MHZ))
1896 break;
1897 if (tbl->is_fat &&
1898 !(priv->current_ht_config.sgf &
1899 HT_SHORT_GI_40MHZ))
1900 break;
1901
1902 IWL_DEBUG_RATE(priv, "LQ: MIMO3 toggle SGI/NGI\n");
Ben Cahill77626352007-11-29 11:09:44 +08001903
1904 /* Set up new search table for MIMO */
Zhu Yib481de92007-09-25 17:54:57 -07001905 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001906 search_tbl->is_SGI = !tbl->is_SGI;
Guy Coheneecd6e52008-04-23 17:14:58 -07001907 rs_set_expected_tpt_table(lq_sta, search_tbl);
Ben Cahill77626352007-11-29 11:09:44 +08001908 /*
1909 * If active table already uses the fastest possible
1910 * modulation (dual stream with short guard interval),
1911 * and it's working well, there's no need to look
1912 * for a better type of modulation!
1913 */
Guy Cohen39e88502008-04-23 17:14:57 -07001914 if (tbl->is_SGI) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001915 s32 tpt = lq_sta->last_tpt / 100;
Guy Cohen39e88502008-04-23 17:14:57 -07001916 if (tpt >= search_tbl->expected_tpt[index])
1917 break;
Zhu Yib481de92007-09-25 17:54:57 -07001918 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001919 search_tbl->current_rate =
1920 rate_n_flags_from_tbl(priv, search_tbl,
1921 index, is_green);
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001922 update_search_tbl_counter = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001923 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07001924 }
1925 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001926 if (tbl->action > IWL_MIMO3_SWITCH_GI)
1927 tbl->action = IWL_MIMO3_SWITCH_ANTENNA1;
Zhu Yib481de92007-09-25 17:54:57 -07001928
1929 if (tbl->action == start_action)
1930 break;
1931 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001932 search_tbl->lq_type = LQ_NONE;
Zhu Yib481de92007-09-25 17:54:57 -07001933 return 0;
1934 out:
Guy Cohen3110bef2008-09-09 10:54:54 +08001935 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001936 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001937 if (tbl->action > IWL_MIMO3_SWITCH_GI)
1938 tbl->action = IWL_MIMO3_SWITCH_ANTENNA1;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001939 if (update_search_tbl_counter)
1940 search_tbl->action = tbl->action;
1941
Zhu Yib481de92007-09-25 17:54:57 -07001942 return 0;
1943
1944}
1945
Ben Cahill77626352007-11-29 11:09:44 +08001946/*
1947 * Check whether we should continue using same modulation mode, or
1948 * begin search for a new mode, based on:
1949 * 1) # tx successes or failures while using this mode
1950 * 2) # times calling this function
1951 * 3) elapsed time in this mode (not used, for now)
1952 */
Tomas Winklere227cea2008-07-18 13:53:05 +08001953static void rs_stay_in_table(struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07001954{
Tomas Winklere227cea2008-07-18 13:53:05 +08001955 struct iwl_scale_tbl_info *tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001956 int i;
1957 int active_tbl;
1958 int flush_interval_passed = 0;
Ester Kummerbf403db2008-05-05 10:22:40 +08001959 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07001960
Ester Kummerbf403db2008-05-05 10:22:40 +08001961 priv = lq_sta->drv;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001962 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001963
Tomas Winklerc33104f2008-01-14 17:46:21 -08001964 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07001965
Ben Cahill77626352007-11-29 11:09:44 +08001966 /* If we've been disallowing search, see if we should now allow it */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001967 if (lq_sta->stay_in_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07001968
Ben Cahill77626352007-11-29 11:09:44 +08001969 /* Elapsed time using current modulation mode */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001970 if (lq_sta->flush_timer)
Zhu Yib481de92007-09-25 17:54:57 -07001971 flush_interval_passed =
Mohamed Abbas447fee72009-04-20 14:37:02 -07001972 time_after(jiffies,
1973 (unsigned long)(lq_sta->flush_timer +
Zhu Yib481de92007-09-25 17:54:57 -07001974 IWL_RATE_SCALE_FLUSH_INTVL));
1975
Ben Cahill77626352007-11-29 11:09:44 +08001976 /*
1977 * Check if we should allow search for new modulation mode.
1978 * If many frames have failed or succeeded, or we've used
1979 * this same modulation for a long time, allow search, and
1980 * reset history stats that keep track of whether we should
1981 * allow a new search. Also (below) reset all bitmaps and
1982 * stats in active history.
1983 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001984 if ((lq_sta->total_failed > lq_sta->max_failure_limit) ||
1985 (lq_sta->total_success > lq_sta->max_success_limit) ||
1986 ((!lq_sta->search_better_tbl) && (lq_sta->flush_timer)
Zhu Yib481de92007-09-25 17:54:57 -07001987 && (flush_interval_passed))) {
Tomas Winklere1623442009-01-27 14:27:56 -08001988 IWL_DEBUG_RATE(priv, "LQ: stay is expired %d %d %d\n:",
Tomas Winklerc33104f2008-01-14 17:46:21 -08001989 lq_sta->total_failed,
1990 lq_sta->total_success,
Zhu Yib481de92007-09-25 17:54:57 -07001991 flush_interval_passed);
Ben Cahill77626352007-11-29 11:09:44 +08001992
1993 /* Allow search for new mode */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001994 lq_sta->stay_in_tbl = 0; /* only place reset */
1995 lq_sta->total_failed = 0;
1996 lq_sta->total_success = 0;
1997 lq_sta->flush_timer = 0;
Ben Cahill77626352007-11-29 11:09:44 +08001998
1999 /*
2000 * Else if we've used this modulation mode enough repetitions
2001 * (regardless of elapsed time or success/failure), reset
2002 * history bitmaps and rate-specific stats for all rates in
2003 * active table.
2004 */
Mohamed Abbas403ab562007-11-06 22:06:25 -08002005 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002006 lq_sta->table_count++;
2007 if (lq_sta->table_count >=
2008 lq_sta->table_count_limit) {
2009 lq_sta->table_count = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002010
Tomas Winklere1623442009-01-27 14:27:56 -08002011 IWL_DEBUG_RATE(priv, "LQ: stay in table clear win\n");
Zhu Yib481de92007-09-25 17:54:57 -07002012 for (i = 0; i < IWL_RATE_COUNT; i++)
2013 rs_rate_scale_clear_window(
2014 &(tbl->win[i]));
2015 }
2016 }
2017
Ben Cahill77626352007-11-29 11:09:44 +08002018 /* If transitioning to allow "search", reset all history
2019 * bitmaps and stats in active table (this will become the new
2020 * "search" table). */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002021 if (!lq_sta->stay_in_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07002022 for (i = 0; i < IWL_RATE_COUNT; i++)
2023 rs_rate_scale_clear_window(&(tbl->win[i]));
2024 }
2025 }
2026}
2027
Ben Cahill77626352007-11-29 11:09:44 +08002028/*
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07002029 * setup rate table in uCode
2030 * return rate_n_flags as used in the table
2031 */
2032static u32 rs_update_rate_tbl(struct iwl_priv *priv,
2033 struct iwl_lq_sta *lq_sta,
2034 struct iwl_scale_tbl_info *tbl,
2035 int index, u8 is_green)
2036{
2037 u32 rate;
2038
2039 /* Update uCode's rate table. */
2040 rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
2041 rs_fill_link_cmd(priv, lq_sta, rate);
2042 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
2043
2044 return rate;
2045}
2046
2047/*
Ben Cahill77626352007-11-29 11:09:44 +08002048 * Do rate scaling and search for new modulation mode.
2049 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002050static void rs_rate_scale_perform(struct iwl_priv *priv,
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002051 struct sk_buff *skb,
Johannes Berg4b7679a2008-09-18 18:14:18 +02002052 struct ieee80211_sta *sta,
2053 struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002054{
Johannes Berg4b7679a2008-09-18 18:14:18 +02002055 struct ieee80211_hw *hw = priv->hw;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002056 struct ieee80211_conf *conf = &hw->conf;
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002057 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2058 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002059 int low = IWL_RATE_INVALID;
2060 int high = IWL_RATE_INVALID;
2061 int index;
2062 int i;
Tomas Winklere227cea2008-07-18 13:53:05 +08002063 struct iwl_rate_scale_data *window = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002064 int current_tpt = IWL_INVALID_VALUE;
2065 int low_tpt = IWL_INVALID_VALUE;
2066 int high_tpt = IWL_INVALID_VALUE;
2067 u32 fail_count;
2068 s8 scale_action = 0;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002069 u16 rate_mask;
Zhu Yib481de92007-09-25 17:54:57 -07002070 u8 update_lq = 0;
Tomas Winklere227cea2008-07-18 13:53:05 +08002071 struct iwl_scale_tbl_info *tbl, *tbl1;
Zhu Yib481de92007-09-25 17:54:57 -07002072 u16 rate_scale_index_msk = 0;
Guy Cohen39e88502008-04-23 17:14:57 -07002073 u32 rate;
Zhu Yib481de92007-09-25 17:54:57 -07002074 u8 is_green = 0;
2075 u8 active_tbl = 0;
2076 u8 done_search = 0;
2077 u16 high_low;
Guy Cohena5e8b502008-05-29 16:35:11 +08002078 s32 sr;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002079 u8 tid = MAX_TID_COUNT;
Wey-Yi Guy86b47662009-03-10 14:35:09 -07002080 struct iwl_tid_data *tid_data;
Zhu Yib481de92007-09-25 17:54:57 -07002081
Tomas Winklere1623442009-01-27 14:27:56 -08002082 IWL_DEBUG_RATE(priv, "rate scale calculate new rate for skb\n");
Zhu Yib481de92007-09-25 17:54:57 -07002083
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002084 /* Send management frames and NO_ACK data using lowest rate. */
Tomas Winkler417f1142008-11-12 13:14:06 -08002085 /* TODO: this could probably be improved.. */
2086 if (!ieee80211_is_data(hdr->frame_control) ||
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002087 info->flags & IEEE80211_TX_CTL_NO_ACK)
Zhu Yib481de92007-09-25 17:54:57 -07002088 return;
Zhu Yib481de92007-09-25 17:54:57 -07002089
Johannes Berg4b7679a2008-09-18 18:14:18 +02002090 if (!sta || !lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002091 return;
2092
Johannes Berg4b7679a2008-09-18 18:14:18 +02002093 lq_sta->supp_rates = sta->supp_rates[lq_sta->band];
Zhu Yib481de92007-09-25 17:54:57 -07002094
Ron Rindjunskyfaa29712008-06-12 09:46:58 +08002095 tid = rs_tl_add_packet(lq_sta, hdr);
2096
Ben Cahill77626352007-11-29 11:09:44 +08002097 /*
2098 * Select rate-scale / modulation-mode table to work with in
2099 * the rest of this function: "search" if searching for better
2100 * modulation mode, or "active" if doing rate scaling within a mode.
2101 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002102 if (!lq_sta->search_better_tbl)
2103 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002104 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002105 active_tbl = 1 - lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002106
Tomas Winklerc33104f2008-01-14 17:46:21 -08002107 tbl = &(lq_sta->lq_info[active_tbl]);
Wey-Yi Guy2681b202009-05-21 13:44:22 -07002108 if (is_legacy(tbl->lq_type))
2109 lq_sta->is_green = 0;
2110 else
2111 lq_sta->is_green = rs_use_green(priv, conf);
Tomas Winklerc33104f2008-01-14 17:46:21 -08002112 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07002113
Ben Cahill77626352007-11-29 11:09:44 +08002114 /* current tx rate */
Johannes Bergb7e35002008-09-11 02:22:58 +02002115 index = lq_sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002116
Tomas Winklere1623442009-01-27 14:27:56 -08002117 IWL_DEBUG_RATE(priv, "Rate scale index %d for type %d\n", index,
Zhu Yib481de92007-09-25 17:54:57 -07002118 tbl->lq_type);
2119
Ben Cahill77626352007-11-29 11:09:44 +08002120 /* rates available for this association, and for modulation mode */
Guy Coheneecd6e52008-04-23 17:14:58 -07002121 rate_mask = rs_get_supported_rates(lq_sta, hdr, tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -07002122
Tomas Winklere1623442009-01-27 14:27:56 -08002123 IWL_DEBUG_RATE(priv, "mask 0x%04X \n", rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07002124
2125 /* mask with station rate restriction */
2126 if (is_legacy(tbl->lq_type)) {
Johannes Berg8318d782008-01-24 19:38:38 +01002127 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Ben Cahill77626352007-11-29 11:09:44 +08002128 /* supp_rates has no CCK bits in A mode */
Zhu Yib481de92007-09-25 17:54:57 -07002129 rate_scale_index_msk = (u16) (rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -08002130 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
Zhu Yib481de92007-09-25 17:54:57 -07002131 else
2132 rate_scale_index_msk = (u16) (rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -08002133 lq_sta->supp_rates);
Zhu Yib481de92007-09-25 17:54:57 -07002134
2135 } else
2136 rate_scale_index_msk = rate_mask;
2137
2138 if (!rate_scale_index_msk)
2139 rate_scale_index_msk = rate_mask;
2140
Guy Cohen07bc28e2008-04-23 17:15:03 -07002141 if (!((1 << index) & rate_scale_index_msk)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002142 IWL_ERR(priv, "Current Rate is not valid\n");
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07002143 if (lq_sta->search_better_tbl) {
2144 /* revert to active table if search table is not valid*/
2145 tbl->lq_type = LQ_NONE;
2146 lq_sta->search_better_tbl = 0;
2147 tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
2148 /* get "active" rate info */
2149 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
2150 rate = rs_update_rate_tbl(priv, lq_sta,
2151 tbl, index, is_green);
2152 }
Guy Cohen07bc28e2008-04-23 17:15:03 -07002153 return;
Zhu Yib481de92007-09-25 17:54:57 -07002154 }
2155
Ben Cahill77626352007-11-29 11:09:44 +08002156 /* Get expected throughput table and history window for current rate */
Guy Cohen07bc28e2008-04-23 17:15:03 -07002157 if (!tbl->expected_tpt) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002158 IWL_ERR(priv, "tbl->expected_tpt is NULL\n");
Guy Cohen07bc28e2008-04-23 17:15:03 -07002159 return;
2160 }
Zhu Yib481de92007-09-25 17:54:57 -07002161
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002162 /* force user max rate if set by user */
2163 if ((lq_sta->max_rate_idx != -1) &&
2164 (lq_sta->max_rate_idx < index)) {
2165 index = lq_sta->max_rate_idx;
2166 update_lq = 1;
2167 window = &(tbl->win[index]);
2168 goto lq_update;
2169 }
2170
Zhu Yib481de92007-09-25 17:54:57 -07002171 window = &(tbl->win[index]);
2172
Ben Cahill77626352007-11-29 11:09:44 +08002173 /*
2174 * If there is not enough history to calculate actual average
2175 * throughput, keep analyzing results of more tx frames, without
2176 * changing rate or mode (bypass most of the rest of this function).
2177 * Set up new rate table in uCode only if old rate is not supported
2178 * in current association (use new rate found above).
2179 */
Zhu Yib481de92007-09-25 17:54:57 -07002180 fail_count = window->counter - window->success_counter;
Guy Cohen07bc28e2008-04-23 17:15:03 -07002181 if ((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
2182 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002183 IWL_DEBUG_RATE(priv, "LQ: still below TH. succ=%d total=%d "
Zhu Yib481de92007-09-25 17:54:57 -07002184 "for index %d\n",
2185 window->success_counter, window->counter, index);
Ben Cahill77626352007-11-29 11:09:44 +08002186
2187 /* Can't calculate this yet; not enough history */
Zhu Yib481de92007-09-25 17:54:57 -07002188 window->average_tpt = IWL_INVALID_VALUE;
Ben Cahill77626352007-11-29 11:09:44 +08002189
2190 /* Should we stay with this modulation mode,
2191 * or search for a new one? */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002192 rs_stay_in_table(lq_sta);
Ben Cahill77626352007-11-29 11:09:44 +08002193
Zhu Yib481de92007-09-25 17:54:57 -07002194 goto out;
Guy Cohen3110bef2008-09-09 10:54:54 +08002195 }
Zhu Yib481de92007-09-25 17:54:57 -07002196
Ben Cahill77626352007-11-29 11:09:44 +08002197 /* Else we have enough samples; calculate estimate of
2198 * actual average throughput */
Guy Cohen3110bef2008-09-09 10:54:54 +08002199
2200 BUG_ON(window->average_tpt != ((window->success_ratio *
2201 tbl->expected_tpt[index] + 64) / 128));
Zhu Yib481de92007-09-25 17:54:57 -07002202
Ben Cahill77626352007-11-29 11:09:44 +08002203 /* If we are searching for better modulation mode, check success. */
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002204 if (lq_sta->search_better_tbl &&
2205 (iwl_tx_ant_restriction(priv) == IWL_TX_MULTI)) {
Ben Cahill77626352007-11-29 11:09:44 +08002206 /* If good success, continue using the "search" mode;
2207 * no need to send new link quality command, since we're
2208 * continuing to use the setup that we've been trying. */
Guy Cohen07bc28e2008-04-23 17:15:03 -07002209 if (window->average_tpt > lq_sta->last_tpt) {
2210
Tomas Winklere1623442009-01-27 14:27:56 -08002211 IWL_DEBUG_RATE(priv, "LQ: SWITCHING TO NEW TABLE "
Guy Cohen07bc28e2008-04-23 17:15:03 -07002212 "suc=%d cur-tpt=%d old-tpt=%d\n",
2213 window->success_ratio,
2214 window->average_tpt,
2215 lq_sta->last_tpt);
2216
2217 if (!is_legacy(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08002218 lq_sta->enable_counter = 1;
Guy Cohen07bc28e2008-04-23 17:15:03 -07002219
Ben Cahill77626352007-11-29 11:09:44 +08002220 /* Swap tables; "search" becomes "active" */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002221 lq_sta->active_tbl = active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002222 current_tpt = window->average_tpt;
Ben Cahill77626352007-11-29 11:09:44 +08002223
2224 /* Else poor success; go back to mode in "active" table */
Zhu Yib481de92007-09-25 17:54:57 -07002225 } else {
Guy Cohen07bc28e2008-04-23 17:15:03 -07002226
Tomas Winklere1623442009-01-27 14:27:56 -08002227 IWL_DEBUG_RATE(priv, "LQ: GOING BACK TO THE OLD TABLE "
Guy Cohen07bc28e2008-04-23 17:15:03 -07002228 "suc=%d cur-tpt=%d old-tpt=%d\n",
2229 window->success_ratio,
2230 window->average_tpt,
2231 lq_sta->last_tpt);
2232
Ben Cahill77626352007-11-29 11:09:44 +08002233 /* Nullify "search" table */
Zhu Yib481de92007-09-25 17:54:57 -07002234 tbl->lq_type = LQ_NONE;
Ben Cahill77626352007-11-29 11:09:44 +08002235
2236 /* Revert to "active" table */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002237 active_tbl = lq_sta->active_tbl;
2238 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07002239
Ben Cahill77626352007-11-29 11:09:44 +08002240 /* Revert to "active" rate and throughput info */
Tomas Winklere7d326a2008-06-12 09:47:11 +08002241 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
Tomas Winklerc33104f2008-01-14 17:46:21 -08002242 current_tpt = lq_sta->last_tpt;
Ben Cahill77626352007-11-29 11:09:44 +08002243
2244 /* Need to set up a new rate table in uCode */
2245 update_lq = 1;
Zhu Yib481de92007-09-25 17:54:57 -07002246 }
Ben Cahill77626352007-11-29 11:09:44 +08002247
2248 /* Either way, we've made a decision; modulation mode
2249 * search is done, allow rate adjustment next time. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002250 lq_sta->search_better_tbl = 0;
Ben Cahill77626352007-11-29 11:09:44 +08002251 done_search = 1; /* Don't switch modes below! */
Zhu Yib481de92007-09-25 17:54:57 -07002252 goto lq_update;
2253 }
2254
Ben Cahill77626352007-11-29 11:09:44 +08002255 /* (Else) not in search of better modulation mode, try for better
2256 * starting rate, while staying in this mode. */
Ester Kummerbf403db2008-05-05 10:22:40 +08002257 high_low = rs_get_adjacent_rate(priv, index, rate_scale_index_msk,
Zhu Yib481de92007-09-25 17:54:57 -07002258 tbl->lq_type);
2259 low = high_low & 0xff;
2260 high = (high_low >> 8) & 0xff;
2261
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002262 /* If user set max rate, dont allow higher than user constrain */
2263 if ((lq_sta->max_rate_idx != -1) &&
2264 (lq_sta->max_rate_idx < high))
2265 high = IWL_RATE_INVALID;
2266
Guy Cohena5e8b502008-05-29 16:35:11 +08002267 sr = window->success_ratio;
2268
Ben Cahill77626352007-11-29 11:09:44 +08002269 /* Collect measured throughputs for current and adjacent rates */
Zhu Yib481de92007-09-25 17:54:57 -07002270 current_tpt = window->average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07002271 if (low != IWL_RATE_INVALID)
2272 low_tpt = tbl->win[low].average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07002273 if (high != IWL_RATE_INVALID)
2274 high_tpt = tbl->win[high].average_tpt;
2275
Guy Cohena5e8b502008-05-29 16:35:11 +08002276 scale_action = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002277
Ben Cahill77626352007-11-29 11:09:44 +08002278 /* Too many failures, decrease rate */
Guy Cohena5e8b502008-05-29 16:35:11 +08002279 if ((sr <= IWL_RATE_DECREASE_TH) || (current_tpt == 0)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002280 IWL_DEBUG_RATE(priv, "decrease rate because of low success_ratio\n");
Zhu Yib481de92007-09-25 17:54:57 -07002281 scale_action = -1;
Ben Cahill77626352007-11-29 11:09:44 +08002282
2283 /* No throughput measured yet for adjacent rates; try increase. */
Zhu Yib481de92007-09-25 17:54:57 -07002284 } else if ((low_tpt == IWL_INVALID_VALUE) &&
Guy Cohena5e8b502008-05-29 16:35:11 +08002285 (high_tpt == IWL_INVALID_VALUE)) {
2286
2287 if (high != IWL_RATE_INVALID && sr >= IWL_RATE_INCREASE_TH)
2288 scale_action = 1;
2289 else if (low != IWL_RATE_INVALID)
Wey-Yi Guy8fe72312009-03-10 14:35:10 -07002290 scale_action = 0;
Guy Cohena5e8b502008-05-29 16:35:11 +08002291 }
Ben Cahill77626352007-11-29 11:09:44 +08002292
2293 /* Both adjacent throughputs are measured, but neither one has better
2294 * throughput; we're using the best rate, don't change it! */
Zhu Yib481de92007-09-25 17:54:57 -07002295 else if ((low_tpt != IWL_INVALID_VALUE) &&
2296 (high_tpt != IWL_INVALID_VALUE) &&
2297 (low_tpt < current_tpt) &&
2298 (high_tpt < current_tpt))
2299 scale_action = 0;
Ben Cahill77626352007-11-29 11:09:44 +08002300
2301 /* At least one adjacent rate's throughput is measured,
2302 * and may have better performance. */
Zhu Yib481de92007-09-25 17:54:57 -07002303 else {
Ben Cahill77626352007-11-29 11:09:44 +08002304 /* Higher adjacent rate's throughput is measured */
Zhu Yib481de92007-09-25 17:54:57 -07002305 if (high_tpt != IWL_INVALID_VALUE) {
Ben Cahill77626352007-11-29 11:09:44 +08002306 /* Higher rate has better throughput */
Guy Cohena5e8b502008-05-29 16:35:11 +08002307 if (high_tpt > current_tpt &&
2308 sr >= IWL_RATE_INCREASE_TH) {
Zhu Yib481de92007-09-25 17:54:57 -07002309 scale_action = 1;
Guy Cohena5e8b502008-05-29 16:35:11 +08002310 } else {
Wey-Yi Guy8fe72312009-03-10 14:35:10 -07002311 scale_action = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002312 }
Ben Cahill77626352007-11-29 11:09:44 +08002313
2314 /* Lower adjacent rate's throughput is measured */
Zhu Yib481de92007-09-25 17:54:57 -07002315 } else if (low_tpt != IWL_INVALID_VALUE) {
Ben Cahill77626352007-11-29 11:09:44 +08002316 /* Lower rate has better throughput */
Zhu Yib481de92007-09-25 17:54:57 -07002317 if (low_tpt > current_tpt) {
Tomas Winklere1623442009-01-27 14:27:56 -08002318 IWL_DEBUG_RATE(priv,
2319 "decrease rate because of low tpt\n");
Zhu Yib481de92007-09-25 17:54:57 -07002320 scale_action = -1;
Guy Cohena5e8b502008-05-29 16:35:11 +08002321 } else if (sr >= IWL_RATE_INCREASE_TH) {
Zhu Yib481de92007-09-25 17:54:57 -07002322 scale_action = 1;
Guy Cohena5e8b502008-05-29 16:35:11 +08002323 }
Zhu Yib481de92007-09-25 17:54:57 -07002324 }
2325 }
2326
Ben Cahill77626352007-11-29 11:09:44 +08002327 /* Sanity check; asked for decrease, but success rate or throughput
2328 * has been good at old rate. Don't change it. */
Guy Cohena5e8b502008-05-29 16:35:11 +08002329 if ((scale_action == -1) && (low != IWL_RATE_INVALID) &&
2330 ((sr > IWL_RATE_HIGH_TH) ||
Zhu Yib481de92007-09-25 17:54:57 -07002331 (current_tpt > (100 * tbl->expected_tpt[low]))))
Zhu Yib481de92007-09-25 17:54:57 -07002332 scale_action = 0;
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002333 if (!iwl_ht_enabled(priv) && !is_legacy(tbl->lq_type))
2334 scale_action = -1;
2335 if (iwl_tx_ant_restriction(priv) != IWL_TX_MULTI &&
2336 (is_mimo2(tbl->lq_type) || is_mimo3(tbl->lq_type)))
2337 scale_action = -1;
Zhu Yib481de92007-09-25 17:54:57 -07002338 switch (scale_action) {
2339 case -1:
Ben Cahill77626352007-11-29 11:09:44 +08002340 /* Decrease starting rate, update uCode's rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002341 if (low != IWL_RATE_INVALID) {
2342 update_lq = 1;
2343 index = low;
2344 }
Mohamed Abbas447fee72009-04-20 14:37:02 -07002345
Zhu Yib481de92007-09-25 17:54:57 -07002346 break;
2347 case 1:
Ben Cahill77626352007-11-29 11:09:44 +08002348 /* Increase starting rate, update uCode's rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002349 if (high != IWL_RATE_INVALID) {
2350 update_lq = 1;
2351 index = high;
2352 }
2353
2354 break;
2355 case 0:
Ben Cahill77626352007-11-29 11:09:44 +08002356 /* No change */
Zhu Yib481de92007-09-25 17:54:57 -07002357 default:
2358 break;
2359 }
2360
Tomas Winklere1623442009-01-27 14:27:56 -08002361 IWL_DEBUG_RATE(priv, "choose rate scale index %d action %d low %d "
Zhu Yib481de92007-09-25 17:54:57 -07002362 "high %d type %d\n",
2363 index, scale_action, low, high, tbl->lq_type);
2364
Guy Cohena5e8b502008-05-29 16:35:11 +08002365lq_update:
Ben Cahill77626352007-11-29 11:09:44 +08002366 /* Replace uCode's rate table for the destination station. */
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07002367 if (update_lq)
2368 rate = rs_update_rate_tbl(priv, lq_sta,
2369 tbl, index, is_green);
Ben Cahill77626352007-11-29 11:09:44 +08002370
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002371 if (iwl_tx_ant_restriction(priv) == IWL_TX_MULTI) {
2372 /* Should we stay with this modulation mode,
2373 * or search for a new one? */
2374 rs_stay_in_table(lq_sta);
2375 }
Ben Cahill77626352007-11-29 11:09:44 +08002376 /*
2377 * Search for new modulation mode if we're:
2378 * 1) Not changing rates right now
2379 * 2) Not just finishing up a search
2380 * 3) Allowing a new search
2381 */
Guy Cohen47cfd462008-05-27 11:29:34 +08002382 if (!update_lq && !done_search && !lq_sta->stay_in_tbl && window->counter) {
Ben Cahill77626352007-11-29 11:09:44 +08002383 /* Save current throughput to compare with "search" throughput*/
Tomas Winklerc33104f2008-01-14 17:46:21 -08002384 lq_sta->last_tpt = current_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07002385
Ben Cahill77626352007-11-29 11:09:44 +08002386 /* Select a new "search" modulation mode to try.
2387 * If one is found, set up the new "search" table. */
Zhu Yib481de92007-09-25 17:54:57 -07002388 if (is_legacy(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08002389 rs_move_legacy_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07002390 else if (is_siso(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08002391 rs_move_siso_to_other(priv, lq_sta, conf, sta, index);
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07002392 else if (is_mimo2(tbl->lq_type))
2393 rs_move_mimo2_to_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07002394 else
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07002395 rs_move_mimo3_to_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07002396
Ben Cahill77626352007-11-29 11:09:44 +08002397 /* If new "search" mode was selected, set up in uCode table */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002398 if (lq_sta->search_better_tbl) {
Ben Cahill77626352007-11-29 11:09:44 +08002399 /* Access the "search" table, clear its history. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002400 tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Zhu Yib481de92007-09-25 17:54:57 -07002401 for (i = 0; i < IWL_RATE_COUNT; i++)
2402 rs_rate_scale_clear_window(&(tbl->win[i]));
2403
Ben Cahill77626352007-11-29 11:09:44 +08002404 /* Use new "search" start rate */
Tomas Winklere7d326a2008-06-12 09:47:11 +08002405 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002406
Tomas Winklere1623442009-01-27 14:27:56 -08002407 IWL_DEBUG_RATE(priv, "Switch current mcs: %X index: %d\n",
Guy Cohen39e88502008-04-23 17:14:57 -07002408 tbl->current_rate, index);
Guy Cohen07bc28e2008-04-23 17:15:03 -07002409 rs_fill_link_cmd(priv, lq_sta, tbl->current_rate);
Tomas Winkler66c73db2008-04-15 16:01:40 -07002410 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
Mohamed Abbas447fee72009-04-20 14:37:02 -07002411 } else
2412 done_search = 1;
2413 }
Zhu Yib481de92007-09-25 17:54:57 -07002414
Mohamed Abbas447fee72009-04-20 14:37:02 -07002415 if (done_search && !lq_sta->stay_in_tbl) {
Ben Cahill77626352007-11-29 11:09:44 +08002416 /* If the "active" (non-search) mode was legacy,
2417 * and we've tried switching antennas,
2418 * but we haven't been able to try HT modes (not available),
2419 * stay with best antenna legacy modulation for a while
2420 * before next round of mode comparisons. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002421 tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
Luis R. Rodriguezde27e642008-12-23 15:58:44 -08002422 if (is_legacy(tbl1->lq_type) && !conf_is_ht(conf) &&
Mohamed Abbasd6e93392009-05-08 13:44:39 -07002423 lq_sta->action_counter > tbl1->max_search) {
Tomas Winklere1623442009-01-27 14:27:56 -08002424 IWL_DEBUG_RATE(priv, "LQ: STAY in legacy table\n");
Ester Kummerbf403db2008-05-05 10:22:40 +08002425 rs_set_stay_in_table(priv, 1, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002426 }
2427
Ben Cahill77626352007-11-29 11:09:44 +08002428 /* If we're in an HT mode, and all 3 mode switch actions
2429 * have been tried and compared, stay in this best modulation
2430 * mode for a while before next round of mode comparisons. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002431 if (lq_sta->enable_counter &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002432 (lq_sta->action_counter >= tbl1->max_search) &&
2433 iwl_ht_enabled(priv)) {
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002434 if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
2435 (lq_sta->tx_agg_tid_en & (1 << tid)) &&
2436 (tid != MAX_TID_COUNT)) {
Wey-Yi Guy86b47662009-03-10 14:35:09 -07002437 tid_data =
2438 &priv->stations[lq_sta->lq.sta_id].tid[tid];
2439 if (tid_data->agg.state == IWL_AGG_OFF) {
2440 IWL_DEBUG_RATE(priv,
2441 "try to aggregate tid %d\n",
2442 tid);
2443 rs_tl_turn_on_agg(priv, tid,
2444 lq_sta, sta);
2445 }
Zhu Yib481de92007-09-25 17:54:57 -07002446 }
Ester Kummerbf403db2008-05-05 10:22:40 +08002447 rs_set_stay_in_table(priv, 0, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002448 }
Zhu Yib481de92007-09-25 17:54:57 -07002449 }
2450
2451out:
Tomas Winkler978785a2008-12-19 10:37:31 +08002452 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
Zhu Yib481de92007-09-25 17:54:57 -07002453 i = index;
Johannes Bergb7e35002008-09-11 02:22:58 +02002454 lq_sta->last_txrate_idx = i;
Zhu Yib481de92007-09-25 17:54:57 -07002455
Zhu Yib481de92007-09-25 17:54:57 -07002456 return;
2457}
2458
2459
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002460static void rs_initialize_lq(struct iwl_priv *priv,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002461 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02002462 struct ieee80211_sta *sta,
2463 struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002464{
Tomas Winklere227cea2008-07-18 13:53:05 +08002465 struct iwl_scale_tbl_info *tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002466 int rate_idx;
Guy Cohenaade00c2008-04-23 17:14:59 -07002467 int i;
Guy Cohen39e88502008-04-23 17:14:57 -07002468 u32 rate;
Guy Cohenaade00c2008-04-23 17:14:59 -07002469 u8 use_green = rs_use_green(priv, conf);
2470 u8 active_tbl = 0;
2471 u8 valid_tx_ant;
Zhu Yib481de92007-09-25 17:54:57 -07002472
Johannes Berg4b7679a2008-09-18 18:14:18 +02002473 if (!sta || !lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002474 goto out;
2475
Johannes Bergb7e35002008-09-11 02:22:58 +02002476 i = lq_sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002477
Tomas Winklerc33104f2008-01-14 17:46:21 -08002478 if ((lq_sta->lq.sta_id == 0xff) &&
Johannes Berg05c914f2008-09-11 00:01:58 +02002479 (priv->iw_mode == NL80211_IFTYPE_ADHOC))
Zhu Yib481de92007-09-25 17:54:57 -07002480 goto out;
2481
Guy Cohenaade00c2008-04-23 17:14:59 -07002482 valid_tx_ant = priv->hw_params.valid_tx_ant;
2483
Tomas Winklerc33104f2008-01-14 17:46:21 -08002484 if (!lq_sta->search_better_tbl)
2485 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002486 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002487 active_tbl = 1 - lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002488
Tomas Winklerc33104f2008-01-14 17:46:21 -08002489 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07002490
2491 if ((i < 0) || (i >= IWL_RATE_COUNT))
2492 i = 0;
2493
Tomas Winkler1826dcc2008-05-15 13:54:02 +08002494 rate = iwl_rates[i].plcp;
Winkler, Tomaseb48dca2008-10-29 14:05:48 -07002495 tbl->ant_type = first_antenna(valid_tx_ant);
2496 rate |= tbl->ant_type << RATE_MCS_ANT_POS;
Zhu Yib481de92007-09-25 17:54:57 -07002497
2498 if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
Guy Cohen39e88502008-04-23 17:14:57 -07002499 rate |= RATE_MCS_CCK_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002500
Guy Cohen39e88502008-04-23 17:14:57 -07002501 rs_get_tbl_info_from_mcs(rate, priv->band, tbl, &rate_idx);
Guy Cohenaade00c2008-04-23 17:14:59 -07002502 if (!rs_is_valid_ant(valid_tx_ant, tbl->ant_type))
2503 rs_toggle_antenna(valid_tx_ant, &rate, tbl);
Zhu Yib481de92007-09-25 17:54:57 -07002504
Tomas Winkler978785a2008-12-19 10:37:31 +08002505 rate = rate_n_flags_from_tbl(priv, tbl, rate_idx, use_green);
Guy Cohen39e88502008-04-23 17:14:57 -07002506 tbl->current_rate = rate;
Guy Coheneecd6e52008-04-23 17:14:58 -07002507 rs_set_expected_tpt_table(lq_sta, tbl);
Guy Cohen07bc28e2008-04-23 17:15:03 -07002508 rs_fill_link_cmd(NULL, lq_sta, rate);
Tomas Winkler66c73db2008-04-15 16:01:40 -07002509 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07002510 out:
2511 return;
2512}
2513
Johannes Berge6a98542008-10-21 12:40:02 +02002514static void rs_get_rate(void *priv_r, struct ieee80211_sta *sta, void *priv_sta,
2515 struct ieee80211_tx_rate_control *txrc)
Zhu Yib481de92007-09-25 17:54:57 -07002516{
2517
Johannes Berge6a98542008-10-21 12:40:02 +02002518 struct sk_buff *skb = txrc->skb;
2519 struct ieee80211_supported_band *sband = txrc->sband;
Johannes Berg4b7679a2008-09-18 18:14:18 +02002520 struct iwl_priv *priv = (struct iwl_priv *)priv_r;
2521 struct ieee80211_conf *conf = &priv->hw->conf;
Zhu Yib481de92007-09-25 17:54:57 -07002522 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berge6a98542008-10-21 12:40:02 +02002523 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Tomas Winkler417f1142008-11-12 13:14:06 -08002524 struct iwl_lq_sta *lq_sta = priv_sta;
2525 int rate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002526
Tomas Winklere1623442009-01-27 14:27:56 -08002527 IWL_DEBUG_RATE_LIMIT(priv, "rate scale calculate new rate for skb\n");
Zhu Yib481de92007-09-25 17:54:57 -07002528
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002529 /* Get max rate if user set max rate */
2530 if (lq_sta) {
2531 lq_sta->max_rate_idx = txrc->max_rate_idx;
2532 if ((sband->band == IEEE80211_BAND_5GHZ) &&
2533 (lq_sta->max_rate_idx != -1))
2534 lq_sta->max_rate_idx += IWL_FIRST_OFDM_RATE;
2535 if ((lq_sta->max_rate_idx < 0) ||
2536 (lq_sta->max_rate_idx >= IWL_RATE_COUNT))
2537 lq_sta->max_rate_idx = -1;
2538 }
2539
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002540 /* Send management frames and NO_ACK data using lowest rate. */
Luis R. Rodriguez4c6d4f52009-07-16 10:05:41 -07002541 if (rate_control_send_low(sta, priv_sta, txrc))
Johannes Berg4b7679a2008-09-18 18:14:18 +02002542 return;
Zhu Yib481de92007-09-25 17:54:57 -07002543
Tomas Winkler417f1142008-11-12 13:14:06 -08002544 rate_idx = lq_sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002545
Johannes Berg05c914f2008-09-11 00:01:58 +02002546 if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) &&
Tomas Winklerc33104f2008-01-14 17:46:21 -08002547 !lq_sta->ibss_sta_added) {
Tomas Winklerc587de02009-06-03 11:44:07 -07002548 u8 sta_id = iwl_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002549
2550 if (sta_id == IWL_INVALID_STATION) {
Tomas Winklere1623442009-01-27 14:27:56 -08002551 IWL_DEBUG_RATE(priv, "LQ: ADD station %pM\n",
Johannes Berge1749612008-10-27 15:59:26 -07002552 hdr->addr1);
Tomas Winklerc587de02009-06-03 11:44:07 -07002553 sta_id = iwl_add_station(priv, hdr->addr1,
2554 false, CMD_ASYNC, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07002555 }
2556 if ((sta_id != IWL_INVALID_STATION)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002557 lq_sta->lq.sta_id = sta_id;
2558 lq_sta->lq.rs_table[0].rate_n_flags = 0;
2559 lq_sta->ibss_sta_added = 1;
Johannes Berg4b7679a2008-09-18 18:14:18 +02002560 rs_initialize_lq(priv, conf, sta, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002561 }
Zhu Yib481de92007-09-25 17:54:57 -07002562 }
2563
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002564 if (lq_sta->last_rate_n_flags & RATE_MCS_HT_MSK) {
Tomas Winkler417f1142008-11-12 13:14:06 -08002565 rate_idx -= IWL_FIRST_OFDM_RATE;
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002566 /* 6M and 9M shared same MCS index */
2567 rate_idx = (rate_idx > 0) ? (rate_idx - 1) : 0;
2568 if (rs_extract_rate(lq_sta->last_rate_n_flags) >=
2569 IWL_RATE_MIMO3_6M_PLCP)
2570 rate_idx = rate_idx + (2 * MCS_INDEX_PER_STREAM);
2571 else if (rs_extract_rate(lq_sta->last_rate_n_flags) >=
2572 IWL_RATE_MIMO2_6M_PLCP)
2573 rate_idx = rate_idx + MCS_INDEX_PER_STREAM;
2574 info->control.rates[0].flags = IEEE80211_TX_RC_MCS;
2575 if (lq_sta->last_rate_n_flags & RATE_MCS_SGI_MSK)
2576 info->control.rates[0].flags |= IEEE80211_TX_RC_SHORT_GI;
2577 if (lq_sta->last_rate_n_flags & RATE_MCS_DUP_MSK)
2578 info->control.rates[0].flags |= IEEE80211_TX_RC_DUP_DATA;
2579 if (lq_sta->last_rate_n_flags & RATE_MCS_FAT_MSK)
2580 info->control.rates[0].flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
2581 if (lq_sta->last_rate_n_flags & RATE_MCS_GF_MSK)
2582 info->control.rates[0].flags |= IEEE80211_TX_RC_GREEN_FIELD;
2583 } else {
2584 if (rate_idx < 0 || rate_idx > IWL_RATE_COUNT)
2585 rate_idx = rate_lowest_index(sband, sta);
2586 else if (sband->band == IEEE80211_BAND_5GHZ)
2587 rate_idx -= IWL_FIRST_OFDM_RATE;
2588 }
Tomas Winkler417f1142008-11-12 13:14:06 -08002589 info->control.rates[0].idx = rate_idx;
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002590
Zhu Yib481de92007-09-25 17:54:57 -07002591}
2592
Johannes Berg4b7679a2008-09-18 18:14:18 +02002593static void *rs_alloc_sta(void *priv_rate, struct ieee80211_sta *sta,
2594 gfp_t gfp)
Zhu Yib481de92007-09-25 17:54:57 -07002595{
Tomas Winklere227cea2008-07-18 13:53:05 +08002596 struct iwl_lq_sta *lq_sta;
Ester Kummerbf403db2008-05-05 10:22:40 +08002597 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07002598 int i, j;
2599
Ester Kummerbf403db2008-05-05 10:22:40 +08002600 priv = (struct iwl_priv *)priv_rate;
Tomas Winklere1623442009-01-27 14:27:56 -08002601 IWL_DEBUG_RATE(priv, "create station rate scale window\n");
Zhu Yib481de92007-09-25 17:54:57 -07002602
Tomas Winklere227cea2008-07-18 13:53:05 +08002603 lq_sta = kzalloc(sizeof(struct iwl_lq_sta), gfp);
Zhu Yib481de92007-09-25 17:54:57 -07002604
Tomas Winklerc33104f2008-01-14 17:46:21 -08002605 if (lq_sta == NULL)
Zhu Yib481de92007-09-25 17:54:57 -07002606 return NULL;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002607 lq_sta->lq.sta_id = 0xff;
Zhu Yib481de92007-09-25 17:54:57 -07002608
Zhu Yi63fddb92007-09-27 11:27:36 +08002609
Zhu Yib481de92007-09-25 17:54:57 -07002610 for (j = 0; j < LQ_SIZE; j++)
2611 for (i = 0; i < IWL_RATE_COUNT; i++)
Tomas Winkler3ac7f142008-07-21 02:40:14 +03002612 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
Zhu Yib481de92007-09-25 17:54:57 -07002613
Tomas Winklerc33104f2008-01-14 17:46:21 -08002614 return lq_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002615}
2616
Johannes Berg4b7679a2008-09-18 18:14:18 +02002617static void rs_rate_init(void *priv_r, struct ieee80211_supported_band *sband,
2618 struct ieee80211_sta *sta, void *priv_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002619{
2620 int i, j;
Johannes Berg4b7679a2008-09-18 18:14:18 +02002621 struct iwl_priv *priv = (struct iwl_priv *)priv_r;
2622 struct ieee80211_conf *conf = &priv->hw->conf;
Tomas Winklere227cea2008-07-18 13:53:05 +08002623 struct iwl_lq_sta *lq_sta = priv_sta;
Mohamed Abbasd5e49032008-12-12 08:22:15 -08002624 u16 mask_bit = 0;
Abbas, Mohamed7d049e52009-01-20 21:33:53 -08002625 int count;
2626 int start_rate = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002627
Tomas Winklerc33104f2008-01-14 17:46:21 -08002628 lq_sta->flush_timer = 0;
Johannes Berg4b7679a2008-09-18 18:14:18 +02002629 lq_sta->supp_rates = sta->supp_rates[sband->band];
Zhu Yib481de92007-09-25 17:54:57 -07002630 for (j = 0; j < LQ_SIZE; j++)
2631 for (i = 0; i < IWL_RATE_COUNT; i++)
Tomas Winkler3ac7f142008-07-21 02:40:14 +03002632 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
Zhu Yib481de92007-09-25 17:54:57 -07002633
Tomas Winklere1623442009-01-27 14:27:56 -08002634 IWL_DEBUG_RATE(priv, "LQ: *** rate scale station global init ***\n");
Zhu Yib481de92007-09-25 17:54:57 -07002635 /* TODO: what is a good starting rate for STA? About middle? Maybe not
2636 * the lowest or the highest rate.. Could consider using RSSI from
2637 * previous packets? Need to have IEEE 802.1X auth succeed immediately
2638 * after assoc.. */
2639
Tomas Winklerc33104f2008-01-14 17:46:21 -08002640 lq_sta->ibss_sta_added = 0;
Johannes Berg05c914f2008-09-11 00:01:58 +02002641 if (priv->iw_mode == NL80211_IFTYPE_AP) {
Tomas Winklerc587de02009-06-03 11:44:07 -07002642 u8 sta_id = iwl_find_station(priv,
Abhijeet Kolekare11bc022009-04-08 11:26:41 -07002643 sta->addr);
Joe Perches0795af52007-10-03 17:59:30 -07002644
Zhu Yib481de92007-09-25 17:54:57 -07002645 /* for IBSS the call are from tasklet */
Tomas Winklere1623442009-01-27 14:27:56 -08002646 IWL_DEBUG_RATE(priv, "LQ: ADD station %pM\n", sta->addr);
Zhu Yib481de92007-09-25 17:54:57 -07002647
2648 if (sta_id == IWL_INVALID_STATION) {
Tomas Winklere1623442009-01-27 14:27:56 -08002649 IWL_DEBUG_RATE(priv, "LQ: ADD station %pM\n", sta->addr);
Tomas Winklerc587de02009-06-03 11:44:07 -07002650 sta_id = iwl_add_station(priv, sta->addr, false,
2651 CMD_ASYNC, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07002652 }
2653 if ((sta_id != IWL_INVALID_STATION)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002654 lq_sta->lq.sta_id = sta_id;
2655 lq_sta->lq.rs_table[0].rate_n_flags = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002656 }
2657 /* FIXME: this is w/a remove it later */
2658 priv->assoc_station_added = 1;
2659 }
2660
Tomas Winklerc33104f2008-01-14 17:46:21 -08002661 lq_sta->is_dup = 0;
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002662 lq_sta->max_rate_idx = -1;
Abbas, Mohamed7d049e52009-01-20 21:33:53 -08002663 lq_sta->missed_rate_counter = IWL_MISSED_RATE_MAX;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002664 lq_sta->is_green = rs_use_green(priv, conf);
Guy Cohen39e88502008-04-23 17:14:57 -07002665 lq_sta->active_legacy_rate = priv->active_rate & ~(0x1000);
Tomas Winklerc33104f2008-01-14 17:46:21 -08002666 lq_sta->active_rate_basic = priv->active_rate_basic;
Johannes Berg8318d782008-01-24 19:38:38 +01002667 lq_sta->band = priv->band;
Ben Cahill77626352007-11-29 11:09:44 +08002668 /*
2669 * active_siso_rate mask includes 9 MBits (bit 5), and CCK (bits 0-3),
2670 * supp_rates[] does not; shift to convert format, force 9 MBits off.
2671 */
Johannes Bergae5eb022008-10-14 16:58:37 +02002672 lq_sta->active_siso_rate = sta->ht_cap.mcs.rx_mask[0] << 1;
2673 lq_sta->active_siso_rate |= sta->ht_cap.mcs.rx_mask[0] & 0x1;
Guy Cohenfde0db32008-04-21 15:42:01 -07002674 lq_sta->active_siso_rate &= ~((u16)0x2);
2675 lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -07002676
Ben Cahill77626352007-11-29 11:09:44 +08002677 /* Same here */
Johannes Bergae5eb022008-10-14 16:58:37 +02002678 lq_sta->active_mimo2_rate = sta->ht_cap.mcs.rx_mask[1] << 1;
2679 lq_sta->active_mimo2_rate |= sta->ht_cap.mcs.rx_mask[1] & 0x1;
Guy Cohenfde0db32008-04-21 15:42:01 -07002680 lq_sta->active_mimo2_rate &= ~((u16)0x2);
2681 lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE;
2682
Johannes Bergae5eb022008-10-14 16:58:37 +02002683 lq_sta->active_mimo3_rate = sta->ht_cap.mcs.rx_mask[2] << 1;
2684 lq_sta->active_mimo3_rate |= sta->ht_cap.mcs.rx_mask[2] & 0x1;
Guy Cohenfde0db32008-04-21 15:42:01 -07002685 lq_sta->active_mimo3_rate &= ~((u16)0x2);
2686 lq_sta->active_mimo3_rate <<= IWL_FIRST_OFDM_RATE;
2687
Tomas Winklere1623442009-01-27 14:27:56 -08002688 IWL_DEBUG_RATE(priv, "SISO-RATE=%X MIMO2-RATE=%X MIMO3-RATE=%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002689 lq_sta->active_siso_rate,
Guy Cohenfde0db32008-04-21 15:42:01 -07002690 lq_sta->active_mimo2_rate,
2691 lq_sta->active_mimo3_rate);
2692
Tomas Winklera96a27f2008-10-23 23:48:56 -07002693 /* These values will be overridden later */
Guy Cohen07bc28e2008-04-23 17:15:03 -07002694 lq_sta->lq.general_params.single_stream_ant_msk = ANT_A;
2695 lq_sta->lq.general_params.dual_stream_ant_msk = ANT_AB;
2696
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002697 /* as default allow aggregation for all tids */
2698 lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002699 lq_sta->drv = priv;
Zhu Yib481de92007-09-25 17:54:57 -07002700
Mohamed Abbasd5e49032008-12-12 08:22:15 -08002701 /* Find highest tx rate supported by hardware and destination station */
Abbas, Mohamed7d049e52009-01-20 21:33:53 -08002702 mask_bit = sta->supp_rates[sband->band];
2703 count = sband->n_bitrates;
2704 if (sband->band == IEEE80211_BAND_5GHZ) {
2705 count += IWL_FIRST_OFDM_RATE;
2706 start_rate = IWL_FIRST_OFDM_RATE;
2707 mask_bit <<= IWL_FIRST_OFDM_RATE;
2708 }
2709
2710 mask_bit = mask_bit & lq_sta->active_legacy_rate;
2711 lq_sta->last_txrate_idx = 4;
2712 for (i = start_rate; i < count; i++)
Mohamed Abbasd5e49032008-12-12 08:22:15 -08002713 if (mask_bit & BIT(i))
2714 lq_sta->last_txrate_idx = i;
2715
Johannes Berg4b7679a2008-09-18 18:14:18 +02002716 rs_initialize_lq(priv, conf, sta, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002717}
2718
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002719static void rs_fill_link_cmd(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08002720 struct iwl_lq_sta *lq_sta, u32 new_rate)
Zhu Yib481de92007-09-25 17:54:57 -07002721{
Tomas Winklere227cea2008-07-18 13:53:05 +08002722 struct iwl_scale_tbl_info tbl_type;
Zhu Yib481de92007-09-25 17:54:57 -07002723 int index = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002724 int rate_idx;
Zhu Yi1b696de2007-09-27 11:27:41 +08002725 int repeat_rate = 0;
Guy Cohenaade00c2008-04-23 17:14:59 -07002726 u8 ant_toggle_cnt = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002727 u8 use_ht_possible = 1;
Guy Cohenaade00c2008-04-23 17:14:59 -07002728 u8 valid_tx_ant = 0;
Guy Cohen07bc28e2008-04-23 17:15:03 -07002729 struct iwl_link_quality_cmd *lq_cmd = &lq_sta->lq;
Zhu Yib481de92007-09-25 17:54:57 -07002730
Ben Cahill77626352007-11-29 11:09:44 +08002731 /* Override starting rate (index 0) if needed for debug purposes */
Guy Cohen39e88502008-04-23 17:14:57 -07002732 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Zhu Yi98d7e092007-09-27 11:27:42 +08002733
Guy Cohen39e88502008-04-23 17:14:57 -07002734 /* Interpret new_rate (rate_n_flags) */
Guy Cohenaade00c2008-04-23 17:14:59 -07002735 memset(&tbl_type, 0, sizeof(tbl_type));
Guy Cohen39e88502008-04-23 17:14:57 -07002736 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band,
Zhu Yib481de92007-09-25 17:54:57 -07002737 &tbl_type, &rate_idx);
2738
Ben Cahill77626352007-11-29 11:09:44 +08002739 /* How many times should we repeat the initial rate? */
Zhu Yib481de92007-09-25 17:54:57 -07002740 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07002741 ant_toggle_cnt = 1;
Zhu Yi1b696de2007-09-27 11:27:41 +08002742 repeat_rate = IWL_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002743 } else {
Zhu Yi1b696de2007-09-27 11:27:41 +08002744 repeat_rate = IWL_HT_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002745 }
Zhu Yib481de92007-09-25 17:54:57 -07002746
2747 lq_cmd->general_params.mimo_delimiter =
2748 is_mimo(tbl_type.lq_type) ? 1 : 0;
Ben Cahill77626352007-11-29 11:09:44 +08002749
2750 /* Fill 1st table entry (index 0) */
Guy Cohen39e88502008-04-23 17:14:57 -07002751 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002752
Guy Cohen07bc28e2008-04-23 17:15:03 -07002753 if (num_of_ant(tbl_type.ant_type) == 1) {
2754 lq_cmd->general_params.single_stream_ant_msk =
2755 tbl_type.ant_type;
2756 } else if (num_of_ant(tbl_type.ant_type) == 2) {
2757 lq_cmd->general_params.dual_stream_ant_msk =
2758 tbl_type.ant_type;
2759 } /* otherwise we don't modify the existing value */
Zhu Yib481de92007-09-25 17:54:57 -07002760
2761 index++;
Zhu Yi1b696de2007-09-27 11:27:41 +08002762 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002763
Guy Cohenaade00c2008-04-23 17:14:59 -07002764 if (priv)
2765 valid_tx_ant = priv->hw_params.valid_tx_ant;
2766
Ben Cahill77626352007-11-29 11:09:44 +08002767 /* Fill rest of rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002768 while (index < LINK_QUAL_MAX_RETRY_NUM) {
Ben Cahill77626352007-11-29 11:09:44 +08002769 /* Repeat initial/next rate.
2770 * For legacy IWL_NUMBER_TRY == 1, this loop will not execute.
2771 * For HT IWL_HT_NUMBER_TRY == 3, this executes twice. */
Zhu Yi1b696de2007-09-27 11:27:41 +08002772 while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
Zhu Yib481de92007-09-25 17:54:57 -07002773 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07002774 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2775 ant_toggle_cnt++;
2776 else if (priv &&
2777 rs_toggle_antenna(valid_tx_ant,
2778 &new_rate, &tbl_type))
2779 ant_toggle_cnt = 1;
2780}
Zhu Yi98d7e092007-09-27 11:27:42 +08002781
Ben Cahill77626352007-11-29 11:09:44 +08002782 /* Override next rate if needed for debug purposes */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002783 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Ben Cahill77626352007-11-29 11:09:44 +08002784
2785 /* Fill next table entry */
Zhu Yib481de92007-09-25 17:54:57 -07002786 lq_cmd->rs_table[index].rate_n_flags =
Guy Cohen39e88502008-04-23 17:14:57 -07002787 cpu_to_le32(new_rate);
Zhu Yi1b696de2007-09-27 11:27:41 +08002788 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002789 index++;
2790 }
2791
Guy Cohen39e88502008-04-23 17:14:57 -07002792 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band, &tbl_type,
Zhu Yib481de92007-09-25 17:54:57 -07002793 &rate_idx);
2794
Ben Cahill77626352007-11-29 11:09:44 +08002795 /* Indicate to uCode which entries might be MIMO.
2796 * If initial rate was MIMO, this will finally end up
2797 * as (IWL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
Zhu Yib481de92007-09-25 17:54:57 -07002798 if (is_mimo(tbl_type.lq_type))
2799 lq_cmd->general_params.mimo_delimiter = index;
2800
Ben Cahill77626352007-11-29 11:09:44 +08002801 /* Get next rate */
Guy Cohen39e88502008-04-23 17:14:57 -07002802 new_rate = rs_get_lower_rate(lq_sta, &tbl_type, rate_idx,
2803 use_ht_possible);
Zhu Yib481de92007-09-25 17:54:57 -07002804
Ben Cahill77626352007-11-29 11:09:44 +08002805 /* How many times should we repeat the next rate? */
Zhu Yib481de92007-09-25 17:54:57 -07002806 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07002807 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2808 ant_toggle_cnt++;
2809 else if (priv &&
2810 rs_toggle_antenna(valid_tx_ant,
2811 &new_rate, &tbl_type))
2812 ant_toggle_cnt = 1;
2813
Zhu Yi1b696de2007-09-27 11:27:41 +08002814 repeat_rate = IWL_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002815 } else {
Zhu Yi1b696de2007-09-27 11:27:41 +08002816 repeat_rate = IWL_HT_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002817 }
Zhu Yib481de92007-09-25 17:54:57 -07002818
Ben Cahill77626352007-11-29 11:09:44 +08002819 /* Don't allow HT rates after next pass.
2820 * rs_get_lower_rate() will change type to LQ_A or LQ_G. */
Zhu Yib481de92007-09-25 17:54:57 -07002821 use_ht_possible = 0;
2822
Ben Cahill77626352007-11-29 11:09:44 +08002823 /* Override next rate if needed for debug purposes */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002824 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Ben Cahill77626352007-11-29 11:09:44 +08002825
2826 /* Fill next table entry */
Guy Cohen39e88502008-04-23 17:14:57 -07002827 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002828
2829 index++;
Zhu Yi1b696de2007-09-27 11:27:41 +08002830 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002831 }
2832
Wey-Yi Guy13c33a02009-06-03 11:44:11 -07002833 lq_cmd->agg_params.agg_frame_cnt_limit = LINK_QUAL_AGG_FRAME_LIMIT_MAX;
2834 lq_cmd->agg_params.agg_dis_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
2835 lq_cmd->agg_params.agg_time_limit =
2836 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
Zhu Yib481de92007-09-25 17:54:57 -07002837}
2838
Johannes Berg4b7679a2008-09-18 18:14:18 +02002839static void *rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
Zhu Yib481de92007-09-25 17:54:57 -07002840{
Johannes Berg4b7679a2008-09-18 18:14:18 +02002841 return hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07002842}
2843/* rate scale requires free function to be implemented */
2844static void rs_free(void *priv_rate)
2845{
2846 return;
2847}
2848
Johannes Berg4b7679a2008-09-18 18:14:18 +02002849static void rs_free_sta(void *priv_r, struct ieee80211_sta *sta,
2850 void *priv_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002851{
Tomas Winklere227cea2008-07-18 13:53:05 +08002852 struct iwl_lq_sta *lq_sta = priv_sta;
Rami Rosen45527c22008-10-07 09:50:01 +02002853 struct iwl_priv *priv __maybe_unused = priv_r;
Zhu Yib481de92007-09-25 17:54:57 -07002854
Tomas Winklere1623442009-01-27 14:27:56 -08002855 IWL_DEBUG_RATE(priv, "enter\n");
Tomas Winklerc33104f2008-01-14 17:46:21 -08002856 kfree(lq_sta);
Tomas Winklere1623442009-01-27 14:27:56 -08002857 IWL_DEBUG_RATE(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07002858}
2859
2860
Zhu Yi93dc6462007-09-27 11:27:37 +08002861#ifdef CONFIG_MAC80211_DEBUGFS
Zhu Yi5ae212c2007-09-27 11:27:38 +08002862static int open_file_generic(struct inode *inode, struct file *file)
2863{
2864 file->private_data = inode->i_private;
2865 return 0;
2866}
Tomas Winklere227cea2008-07-18 13:53:05 +08002867static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
2868 u32 *rate_n_flags, int index)
Zhu Yi98d7e092007-09-27 11:27:42 +08002869{
Ester Kummerbf403db2008-05-05 10:22:40 +08002870 struct iwl_priv *priv;
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07002871 u8 valid_tx_ant;
2872 u8 ant_sel_tx;
Ester Kummerbf403db2008-05-05 10:22:40 +08002873
2874 priv = lq_sta->drv;
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07002875 valid_tx_ant = priv->hw_params.valid_tx_ant;
Guy Cohen39e88502008-04-23 17:14:57 -07002876 if (lq_sta->dbg_fixed_rate) {
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07002877 ant_sel_tx =
2878 ((lq_sta->dbg_fixed_rate & RATE_MCS_ANT_ABC_MSK)
2879 >> RATE_MCS_ANT_POS);
2880 if ((valid_tx_ant & ant_sel_tx) == ant_sel_tx) {
Guy Cohen39e88502008-04-23 17:14:57 -07002881 *rate_n_flags = lq_sta->dbg_fixed_rate;
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07002882 IWL_DEBUG_RATE(priv, "Fixed rate ON\n");
Guy Cohen39e88502008-04-23 17:14:57 -07002883 } else {
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07002884 lq_sta->dbg_fixed_rate = 0;
2885 IWL_ERR(priv,
2886 "Invalid antenna selection 0x%X, Valid is 0x%X\n",
2887 ant_sel_tx, valid_tx_ant);
2888 IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
Guy Cohen39e88502008-04-23 17:14:57 -07002889 }
Guy Cohen39e88502008-04-23 17:14:57 -07002890 } else {
Tomas Winklere1623442009-01-27 14:27:56 -08002891 IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
Zhu Yi98d7e092007-09-27 11:27:42 +08002892 }
Zhu Yi98d7e092007-09-27 11:27:42 +08002893}
2894
2895static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
2896 const char __user *user_buf, size_t count, loff_t *ppos)
2897{
Tomas Winklere227cea2008-07-18 13:53:05 +08002898 struct iwl_lq_sta *lq_sta = file->private_data;
Ester Kummerbf403db2008-05-05 10:22:40 +08002899 struct iwl_priv *priv;
Zhu Yi98d7e092007-09-27 11:27:42 +08002900 char buf[64];
2901 int buf_size;
2902 u32 parsed_rate;
2903
Ester Kummerbf403db2008-05-05 10:22:40 +08002904 priv = lq_sta->drv;
Zhu Yi98d7e092007-09-27 11:27:42 +08002905 memset(buf, 0, sizeof(buf));
2906 buf_size = min(count, sizeof(buf) - 1);
2907 if (copy_from_user(buf, user_buf, buf_size))
2908 return -EFAULT;
2909
2910 if (sscanf(buf, "%x", &parsed_rate) == 1)
Guy Cohen39e88502008-04-23 17:14:57 -07002911 lq_sta->dbg_fixed_rate = parsed_rate;
Zhu Yi98d7e092007-09-27 11:27:42 +08002912 else
Guy Cohen39e88502008-04-23 17:14:57 -07002913 lq_sta->dbg_fixed_rate = 0;
Zhu Yi98d7e092007-09-27 11:27:42 +08002914
Guy Cohen39e88502008-04-23 17:14:57 -07002915 lq_sta->active_legacy_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
2916 lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2917 lq_sta->active_mimo2_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2918 lq_sta->active_mimo3_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
Zhu Yi98d7e092007-09-27 11:27:42 +08002919
Tomas Winklere1623442009-01-27 14:27:56 -08002920 IWL_DEBUG_RATE(priv, "sta_id %d rate 0x%X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07002921 lq_sta->lq.sta_id, lq_sta->dbg_fixed_rate);
Zhu Yi98d7e092007-09-27 11:27:42 +08002922
Guy Cohen39e88502008-04-23 17:14:57 -07002923 if (lq_sta->dbg_fixed_rate) {
Guy Cohen07bc28e2008-04-23 17:15:03 -07002924 rs_fill_link_cmd(NULL, lq_sta, lq_sta->dbg_fixed_rate);
Tomas Winkler66c73db2008-04-15 16:01:40 -07002925 iwl_send_lq_cmd(lq_sta->drv, &lq_sta->lq, CMD_ASYNC);
Zhu Yi98d7e092007-09-27 11:27:42 +08002926 }
2927
2928 return count;
2929}
Zhu Yi0209dc12007-09-27 11:27:43 +08002930
Zhu Yi5ae212c2007-09-27 11:27:38 +08002931static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
2932 char __user *user_buf, size_t count, loff_t *ppos)
2933{
Frank Seidela412c802009-03-01 20:25:38 +01002934 char *buff;
Zhu Yi5ae212c2007-09-27 11:27:38 +08002935 int desc = 0;
2936 int i = 0;
Wey-Yi Guy12b96812009-04-08 11:39:27 -07002937 int index = 0;
Frank Seidela412c802009-03-01 20:25:38 +01002938 ssize_t ret;
Zhu Yi5ae212c2007-09-27 11:27:38 +08002939
Tomas Winklere227cea2008-07-18 13:53:05 +08002940 struct iwl_lq_sta *lq_sta = file->private_data;
Wey-Yi Guyd8ae4f52009-03-10 14:35:07 -07002941 struct iwl_priv *priv;
Wey-Yi Guyadb7b5e2009-03-10 14:35:08 -07002942 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002943
Wey-Yi Guyd8ae4f52009-03-10 14:35:07 -07002944 priv = lq_sta->drv;
Frank Seidela412c802009-03-01 20:25:38 +01002945 buff = kmalloc(1024, GFP_KERNEL);
2946 if (!buff)
2947 return -ENOMEM;
2948
Tomas Winklerc33104f2008-01-14 17:46:21 -08002949 desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
Zhu Yi98d7e092007-09-27 11:27:42 +08002950 desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002951 lq_sta->total_failed, lq_sta->total_success,
Guy Cohen39e88502008-04-23 17:14:57 -07002952 lq_sta->active_legacy_rate);
Zhu Yi98d7e092007-09-27 11:27:42 +08002953 desc += sprintf(buff+desc, "fixed rate 0x%X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07002954 lq_sta->dbg_fixed_rate);
Wey-Yi Guyd8ae4f52009-03-10 14:35:07 -07002955 desc += sprintf(buff+desc, "valid_tx_ant %s%s%s\n",
2956 (priv->hw_params.valid_tx_ant & ANT_A) ? "ANT_A," : "",
2957 (priv->hw_params.valid_tx_ant & ANT_B) ? "ANT_B," : "",
2958 (priv->hw_params.valid_tx_ant & ANT_C) ? "ANT_C" : "");
Wey-Yi Guyadb7b5e2009-03-10 14:35:08 -07002959 desc += sprintf(buff+desc, "lq type %s\n",
2960 (is_legacy(tbl->lq_type)) ? "legacy" : "HT");
2961 if (is_Ht(tbl->lq_type)) {
2962 desc += sprintf(buff+desc, " %s",
2963 (is_siso(tbl->lq_type)) ? "SISO" :
2964 ((is_mimo2(tbl->lq_type)) ? "MIMO2" : "MIMO3"));
2965 desc += sprintf(buff+desc, " %s",
2966 (tbl->is_fat) ? "40MHz" : "20MHz");
Wey-Yi Guy2681b202009-05-21 13:44:22 -07002967 desc += sprintf(buff+desc, " %s %s\n", (tbl->is_SGI) ? "SGI" : "",
2968 (lq_sta->is_green) ? "GF enabled" : "");
Wey-Yi Guyadb7b5e2009-03-10 14:35:08 -07002969 }
Wey-Yi Guy12b96812009-04-08 11:39:27 -07002970 desc += sprintf(buff+desc, "last tx rate=0x%X\n",
2971 lq_sta->last_rate_n_flags);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002972 desc += sprintf(buff+desc, "general:"
2973 "flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002974 lq_sta->lq.general_params.flags,
2975 lq_sta->lq.general_params.mimo_delimiter,
2976 lq_sta->lq.general_params.single_stream_ant_msk,
2977 lq_sta->lq.general_params.dual_stream_ant_msk);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002978
2979 desc += sprintf(buff+desc, "agg:"
2980 "time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002981 le16_to_cpu(lq_sta->lq.agg_params.agg_time_limit),
2982 lq_sta->lq.agg_params.agg_dis_start_th,
2983 lq_sta->lq.agg_params.agg_frame_cnt_limit);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002984
2985 desc += sprintf(buff+desc,
2986 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002987 lq_sta->lq.general_params.start_rate_index[0],
2988 lq_sta->lq.general_params.start_rate_index[1],
2989 lq_sta->lq.general_params.start_rate_index[2],
2990 lq_sta->lq.general_params.start_rate_index[3]);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002991
Wey-Yi Guy12b96812009-04-08 11:39:27 -07002992 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
2993 index = iwl_hwrate_to_plcp_idx(
2994 le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags));
2995 if (is_legacy(tbl->lq_type)) {
2996 desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps\n",
2997 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags),
2998 iwl_rate_mcs[index].mbps);
2999 } else {
3000 desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps (%s)\n",
3001 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags),
3002 iwl_rate_mcs[index].mbps, iwl_rate_mcs[index].mcs);
3003 }
3004 }
Zhu Yi5ae212c2007-09-27 11:27:38 +08003005
Frank Seidela412c802009-03-01 20:25:38 +01003006 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
3007 kfree(buff);
3008 return ret;
Zhu Yi5ae212c2007-09-27 11:27:38 +08003009}
3010
3011static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
Zhu Yi98d7e092007-09-27 11:27:42 +08003012 .write = rs_sta_dbgfs_scale_table_write,
Zhu Yi5ae212c2007-09-27 11:27:38 +08003013 .read = rs_sta_dbgfs_scale_table_read,
3014 .open = open_file_generic,
3015};
Zhu Yi0209dc12007-09-27 11:27:43 +08003016static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
3017 char __user *user_buf, size_t count, loff_t *ppos)
3018{
Frank Seidela412c802009-03-01 20:25:38 +01003019 char *buff;
Zhu Yi0209dc12007-09-27 11:27:43 +08003020 int desc = 0;
3021 int i, j;
Frank Seidela412c802009-03-01 20:25:38 +01003022 ssize_t ret;
Zhu Yi0209dc12007-09-27 11:27:43 +08003023
Tomas Winklere227cea2008-07-18 13:53:05 +08003024 struct iwl_lq_sta *lq_sta = file->private_data;
Frank Seidela412c802009-03-01 20:25:38 +01003025
3026 buff = kmalloc(1024, GFP_KERNEL);
3027 if (!buff)
3028 return -ENOMEM;
3029
Zhu Yi0209dc12007-09-27 11:27:43 +08003030 for (i = 0; i < LQ_SIZE; i++) {
Wey-Yi Guy2681b202009-05-21 13:44:22 -07003031 desc += sprintf(buff+desc, "%s type=%d SGI=%d FAT=%d DUP=%d GF=%d\n"
Zhu Yi0209dc12007-09-27 11:27:43 +08003032 "rate=0x%X\n",
Abhijeet Kolekarc3056062008-11-12 13:14:08 -08003033 lq_sta->active_tbl == i ? "*" : "x",
Tomas Winklerc33104f2008-01-14 17:46:21 -08003034 lq_sta->lq_info[i].lq_type,
3035 lq_sta->lq_info[i].is_SGI,
3036 lq_sta->lq_info[i].is_fat,
3037 lq_sta->lq_info[i].is_dup,
Wey-Yi Guy2681b202009-05-21 13:44:22 -07003038 lq_sta->is_green,
Guy Cohen39e88502008-04-23 17:14:57 -07003039 lq_sta->lq_info[i].current_rate);
Zhu Yi0209dc12007-09-27 11:27:43 +08003040 for (j = 0; j < IWL_RATE_COUNT; j++) {
3041 desc += sprintf(buff+desc,
Tomas Winklerc33104f2008-01-14 17:46:21 -08003042 "counter=%d success=%d %%=%d\n",
3043 lq_sta->lq_info[i].win[j].counter,
3044 lq_sta->lq_info[i].win[j].success_counter,
3045 lq_sta->lq_info[i].win[j].success_ratio);
Zhu Yi0209dc12007-09-27 11:27:43 +08003046 }
3047 }
Frank Seidela412c802009-03-01 20:25:38 +01003048 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
3049 kfree(buff);
3050 return ret;
Zhu Yi0209dc12007-09-27 11:27:43 +08003051}
3052
3053static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
3054 .read = rs_sta_dbgfs_stats_table_read,
3055 .open = open_file_generic,
3056};
Zhu Yi5ae212c2007-09-27 11:27:38 +08003057
Wey-Yi Guy38167452009-05-08 13:44:43 -07003058static ssize_t rs_sta_dbgfs_rate_scale_data_read(struct file *file,
3059 char __user *user_buf, size_t count, loff_t *ppos)
3060{
3061 char buff[120];
3062 int desc = 0;
3063 ssize_t ret;
3064
3065 struct iwl_lq_sta *lq_sta = file->private_data;
3066 struct iwl_priv *priv;
3067 struct iwl_scale_tbl_info *tbl = &lq_sta->lq_info[lq_sta->active_tbl];
3068
3069 priv = lq_sta->drv;
3070
3071 if (is_Ht(tbl->lq_type))
3072 desc += sprintf(buff+desc,
3073 "Bit Rate= %d Mb/s\n",
3074 tbl->expected_tpt[lq_sta->last_txrate_idx]);
3075 else
3076 desc += sprintf(buff+desc,
3077 "Bit Rate= %d Mb/s\n",
3078 iwl_rates[lq_sta->last_txrate_idx].ieee >> 1);
3079 desc += sprintf(buff+desc,
3080 "Signal Level= %d dBm\tNoise Level= %d dBm\n",
3081 priv->last_rx_rssi, priv->last_rx_noise);
3082 desc += sprintf(buff+desc,
3083 "Tsf= 0x%llx\tBeacon time= 0x%08X\n",
3084 priv->last_tsf, priv->last_beacon_time);
3085
3086 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
3087 return ret;
3088}
3089
3090static const struct file_operations rs_sta_dbgfs_rate_scale_data_ops = {
3091 .read = rs_sta_dbgfs_rate_scale_data_read,
3092 .open = open_file_generic,
3093};
3094
Zhu Yi93dc6462007-09-27 11:27:37 +08003095static void rs_add_debugfs(void *priv, void *priv_sta,
3096 struct dentry *dir)
3097{
Tomas Winklere227cea2008-07-18 13:53:05 +08003098 struct iwl_lq_sta *lq_sta = priv_sta;
Tomas Winklerc33104f2008-01-14 17:46:21 -08003099 lq_sta->rs_sta_dbgfs_scale_table_file =
Zhu Yi0209dc12007-09-27 11:27:43 +08003100 debugfs_create_file("rate_scale_table", 0600, dir,
Tomas Winklerc33104f2008-01-14 17:46:21 -08003101 lq_sta, &rs_sta_dbgfs_scale_table_ops);
3102 lq_sta->rs_sta_dbgfs_stats_table_file =
Zhu Yi0209dc12007-09-27 11:27:43 +08003103 debugfs_create_file("rate_stats_table", 0600, dir,
Tomas Winklerc33104f2008-01-14 17:46:21 -08003104 lq_sta, &rs_sta_dbgfs_stats_table_ops);
Wey-Yi Guy38167452009-05-08 13:44:43 -07003105 lq_sta->rs_sta_dbgfs_rate_scale_data_file =
3106 debugfs_create_file("rate_scale_data", 0600, dir,
3107 lq_sta, &rs_sta_dbgfs_rate_scale_data_ops);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02003108 lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file =
3109 debugfs_create_u8("tx_agg_tid_enable", 0600, dir,
3110 &lq_sta->tx_agg_tid_en);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02003111
Zhu Yi93dc6462007-09-27 11:27:37 +08003112}
3113
3114static void rs_remove_debugfs(void *priv, void *priv_sta)
3115{
Tomas Winklere227cea2008-07-18 13:53:05 +08003116 struct iwl_lq_sta *lq_sta = priv_sta;
Tomas Winklerc33104f2008-01-14 17:46:21 -08003117 debugfs_remove(lq_sta->rs_sta_dbgfs_scale_table_file);
3118 debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
Wey-Yi Guy38167452009-05-08 13:44:43 -07003119 debugfs_remove(lq_sta->rs_sta_dbgfs_rate_scale_data_file);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02003120 debugfs_remove(lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file);
Zhu Yi93dc6462007-09-27 11:27:37 +08003121}
3122#endif
3123
Zhu Yib481de92007-09-25 17:54:57 -07003124static struct rate_control_ops rs_ops = {
3125 .module = NULL,
3126 .name = RS_NAME,
3127 .tx_status = rs_tx_status,
3128 .get_rate = rs_get_rate,
3129 .rate_init = rs_rate_init,
Zhu Yib481de92007-09-25 17:54:57 -07003130 .alloc = rs_alloc,
3131 .free = rs_free,
3132 .alloc_sta = rs_alloc_sta,
3133 .free_sta = rs_free_sta,
Zhu Yi93dc6462007-09-27 11:27:37 +08003134#ifdef CONFIG_MAC80211_DEBUGFS
3135 .add_sta_debugfs = rs_add_debugfs,
3136 .remove_sta_debugfs = rs_remove_debugfs,
3137#endif
Zhu Yib481de92007-09-25 17:54:57 -07003138};
3139
Tomas Winklere227cea2008-07-18 13:53:05 +08003140int iwlagn_rate_control_register(void)
Zhu Yib481de92007-09-25 17:54:57 -07003141{
Reinette Chatre897e1cf2008-03-28 16:21:09 -07003142 return ieee80211_rate_control_register(&rs_ops);
Zhu Yib481de92007-09-25 17:54:57 -07003143}
3144
Tomas Winklere227cea2008-07-18 13:53:05 +08003145void iwlagn_rate_control_unregister(void)
Zhu Yib481de92007-09-25 17:54:57 -07003146{
3147 ieee80211_rate_control_unregister(&rs_ops);
3148}
3149