blob: cd24763d5fde3f13477c75f5c32a776797f0067f [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 */
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700100 u8 is_ht40; /* 1 = 40 MHz channel width */
Ben Cahill77626352007-11-29 11:09:44 +0800101 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
Reinette Chatree6a6cf42009-08-13 13:30:50 -0700335 if (unlikely(tid >= TID_MAX_LOAD_COUNT))
336 return MAX_TID_COUNT;
337
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200338 tl = &lq_data->load[tid];
339
340 curr_time -= curr_time % TID_ROUND_VALUE;
341
342 /* Happens only for the first packet. Initialize the data */
343 if (!(tl->queue_count)) {
344 tl->total = 1;
345 tl->time_stamp = curr_time;
346 tl->queue_count = 1;
347 tl->head = 0;
348 tl->packet_count[0] = 1;
Ron Rindjunskyfaa29712008-06-12 09:46:58 +0800349 return MAX_TID_COUNT;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200350 }
351
352 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
353 index = time_diff / TID_QUEUE_CELL_SPACING;
354
355 /* The history is too long: remove data that is older than */
356 /* TID_MAX_TIME_DIFF */
357 if (index >= TID_QUEUE_MAX_SIZE)
358 rs_tl_rm_old_stats(tl, curr_time);
359
360 index = (tl->head + index) % TID_QUEUE_MAX_SIZE;
361 tl->packet_count[index] = tl->packet_count[index] + 1;
362 tl->total = tl->total + 1;
363
364 if ((index + 1) > tl->queue_count)
365 tl->queue_count = index + 1;
Ron Rindjunskyfaa29712008-06-12 09:46:58 +0800366
367 return tid;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200368}
369
370/*
371 get the traffic load value for tid
372*/
Tomas Winklere227cea2008-07-18 13:53:05 +0800373static u32 rs_tl_get_load(struct iwl_lq_sta *lq_data, u8 tid)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200374{
375 u32 curr_time = jiffies_to_msecs(jiffies);
376 u32 time_diff;
377 s32 index;
Tomas Winklere227cea2008-07-18 13:53:05 +0800378 struct iwl_traffic_load *tl = NULL;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200379
380 if (tid >= TID_MAX_LOAD_COUNT)
381 return 0;
382
383 tl = &(lq_data->load[tid]);
384
385 curr_time -= curr_time % TID_ROUND_VALUE;
386
387 if (!(tl->queue_count))
388 return 0;
389
390 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
391 index = time_diff / TID_QUEUE_CELL_SPACING;
392
393 /* The history is too long: remove data that is older than */
394 /* TID_MAX_TIME_DIFF */
395 if (index >= TID_QUEUE_MAX_SIZE)
396 rs_tl_rm_old_stats(tl, curr_time);
397
398 return tl->total;
399}
400
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700401static void rs_tl_turn_on_agg_for_tid(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +0800402 struct iwl_lq_sta *lq_data, u8 tid,
Johannes Berg4b7679a2008-09-18 18:14:18 +0200403 struct ieee80211_sta *sta)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200404{
Johannes Bergff550cb2008-09-11 03:17:05 +0200405 if (rs_tl_get_load(lq_data, tid) > IWL_AGG_LOAD_THRESHOLD) {
Tomas Winklere1623442009-01-27 14:27:56 -0800406 IWL_DEBUG_HT(priv, "Starting Tx agg: STA: %pM tid: %d\n",
Johannes Berge1749612008-10-27 15:59:26 -0700407 sta->addr, tid);
Johannes Berg4b7679a2008-09-18 18:14:18 +0200408 ieee80211_start_tx_ba_session(priv->hw, sta->addr, tid);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200409 }
410}
411
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700412static void rs_tl_turn_on_agg(struct iwl_priv *priv, u8 tid,
Tomas Winklere227cea2008-07-18 13:53:05 +0800413 struct iwl_lq_sta *lq_data,
Johannes Berg4b7679a2008-09-18 18:14:18 +0200414 struct ieee80211_sta *sta)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200415{
416 if ((tid < TID_MAX_LOAD_COUNT))
417 rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
418 else if (tid == IWL_AGG_ALL_TID)
419 for (tid = 0; tid < TID_MAX_LOAD_COUNT; tid++)
420 rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
Wey-Yi Guy47eef9b2009-09-17 10:43:44 -0700421 if (priv->cfg->use_rts_for_ht) {
422 /*
423 * switch to RTS/CTS if it is the prefer protection method
424 * for HT traffic
425 */
426 IWL_DEBUG_HT(priv, "use RTS/CTS protection for HT\n");
427 priv->staging_rxon.flags &= ~RXON_FLG_SELF_CTS_EN;
428 iwlcore_commit_rxon(priv);
429 }
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200430}
431
Guy Cohen39e88502008-04-23 17:14:57 -0700432static inline int get_num_of_ant_from_rate(u32 rate_n_flags)
Guy Cohenfde0db32008-04-21 15:42:01 -0700433{
Tomas Winkler3ac7f142008-07-21 02:40:14 +0300434 return !!(rate_n_flags & RATE_MCS_ANT_A_MSK) +
435 !!(rate_n_flags & RATE_MCS_ANT_B_MSK) +
436 !!(rate_n_flags & RATE_MCS_ANT_C_MSK);
Guy Cohenfde0db32008-04-21 15:42:01 -0700437}
438
Ben Cahill77626352007-11-29 11:09:44 +0800439/**
440 * rs_collect_tx_data - Update the success/failure sliding window
441 *
442 * We keep a sliding window of the last 62 packets transmitted
443 * at this rate. window->data contains the bitmask of successful
444 * packets.
445 */
Tomas Winklere227cea2008-07-18 13:53:05 +0800446static int rs_collect_tx_data(struct iwl_rate_scale_data *windows,
Ron Rindjunsky99556432008-01-28 14:07:25 +0200447 int scale_index, s32 tpt, int retries,
448 int successes)
Zhu Yib481de92007-09-25 17:54:57 -0700449{
Tomas Winklere227cea2008-07-18 13:53:05 +0800450 struct iwl_rate_scale_data *window = NULL;
Guy Cohen39e88502008-04-23 17:14:57 -0700451 static const u64 mask = (((u64)1) << (IWL_RATE_MAX_WINDOW - 1));
Zhu Yib481de92007-09-25 17:54:57 -0700452 s32 fail_count;
453
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800454 if (scale_index < 0 || scale_index >= IWL_RATE_COUNT)
455 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -0700456
Ben Cahill77626352007-11-29 11:09:44 +0800457 /* Select data for current tx bit rate */
Zhu Yib481de92007-09-25 17:54:57 -0700458 window = &(windows[scale_index]);
459
Ben Cahill77626352007-11-29 11:09:44 +0800460 /*
461 * Keep track of only the latest 62 tx frame attempts in this rate's
462 * history window; anything older isn't really relevant any more.
463 * If we have filled up the sliding window, drop the oldest attempt;
464 * if the oldest attempt (highest bit in bitmap) shows "success",
465 * subtract "1" from the success counter (this is the main reason
466 * we keep these bitmaps!).
467 */
Ron Rindjunsky99556432008-01-28 14:07:25 +0200468 while (retries > 0) {
Guy Cohen39e88502008-04-23 17:14:57 -0700469 if (window->counter >= IWL_RATE_MAX_WINDOW) {
470
471 /* remove earliest */
472 window->counter = IWL_RATE_MAX_WINDOW - 1;
473
Ron Rindjunsky99556432008-01-28 14:07:25 +0200474 if (window->data & mask) {
475 window->data &= ~mask;
Guy Cohen39e88502008-04-23 17:14:57 -0700476 window->success_counter--;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200477 }
Zhu Yib481de92007-09-25 17:54:57 -0700478 }
Zhu Yib481de92007-09-25 17:54:57 -0700479
Ron Rindjunsky99556432008-01-28 14:07:25 +0200480 /* Increment frames-attempted counter */
481 window->counter++;
Ben Cahill77626352007-11-29 11:09:44 +0800482
Ron Rindjunsky99556432008-01-28 14:07:25 +0200483 /* Shift bitmap by one frame (throw away oldest history),
484 * OR in "1", and increment "success" if this
485 * frame was successful. */
Guy Cohen90d77952008-09-09 10:54:53 +0800486 window->data <<= 1;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200487 if (successes > 0) {
Guy Cohen39e88502008-04-23 17:14:57 -0700488 window->success_counter++;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200489 window->data |= 0x1;
490 successes--;
491 }
492
493 retries--;
Zhu Yib481de92007-09-25 17:54:57 -0700494 }
495
Ben Cahill77626352007-11-29 11:09:44 +0800496 /* Calculate current success ratio, avoid divide-by-0! */
Zhu Yib481de92007-09-25 17:54:57 -0700497 if (window->counter > 0)
498 window->success_ratio = 128 * (100 * window->success_counter)
499 / window->counter;
500 else
501 window->success_ratio = IWL_INVALID_VALUE;
502
503 fail_count = window->counter - window->success_counter;
504
Ben Cahill77626352007-11-29 11:09:44 +0800505 /* Calculate average throughput, if we have enough history. */
Zhu Yib481de92007-09-25 17:54:57 -0700506 if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
507 (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
508 window->average_tpt = (window->success_ratio * tpt + 64) / 128;
509 else
510 window->average_tpt = IWL_INVALID_VALUE;
511
Ben Cahill77626352007-11-29 11:09:44 +0800512 /* Tag this window as having been updated */
Zhu Yib481de92007-09-25 17:54:57 -0700513 window->stamp = jiffies;
514
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800515 return 0;
Zhu Yib481de92007-09-25 17:54:57 -0700516}
517
Ben Cahill77626352007-11-29 11:09:44 +0800518/*
519 * Fill uCode API rate_n_flags field, based on "search" or "active" table.
520 */
Guy Cohen39e88502008-04-23 17:14:57 -0700521/* FIXME:RS:remove this function and put the flags statically in the table */
Tomas Winkler978785a2008-12-19 10:37:31 +0800522static u32 rate_n_flags_from_tbl(struct iwl_priv *priv,
523 struct iwl_scale_tbl_info *tbl,
524 int index, u8 use_green)
Zhu Yib481de92007-09-25 17:54:57 -0700525{
Guy Cohen39e88502008-04-23 17:14:57 -0700526 u32 rate_n_flags = 0;
527
Zhu Yib481de92007-09-25 17:54:57 -0700528 if (is_legacy(tbl->lq_type)) {
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800529 rate_n_flags = iwl_rates[index].plcp;
Zhu Yib481de92007-09-25 17:54:57 -0700530 if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
Guy Cohen39e88502008-04-23 17:14:57 -0700531 rate_n_flags |= RATE_MCS_CCK_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700532
Guy Cohenfde0db32008-04-21 15:42:01 -0700533 } else if (is_Ht(tbl->lq_type)) {
534 if (index > IWL_LAST_OFDM_RATE) {
Tomas Winkler978785a2008-12-19 10:37:31 +0800535 IWL_ERR(priv, "Invalid HT rate index %d\n", index);
Zhu Yib481de92007-09-25 17:54:57 -0700536 index = IWL_LAST_OFDM_RATE;
Guy Cohenfde0db32008-04-21 15:42:01 -0700537 }
Guy Cohen39e88502008-04-23 17:14:57 -0700538 rate_n_flags = RATE_MCS_HT_MSK;
Guy Cohenfde0db32008-04-21 15:42:01 -0700539
540 if (is_siso(tbl->lq_type))
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800541 rate_n_flags |= iwl_rates[index].plcp_siso;
Guy Cohenfde0db32008-04-21 15:42:01 -0700542 else if (is_mimo2(tbl->lq_type))
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800543 rate_n_flags |= iwl_rates[index].plcp_mimo2;
Guy Cohenfde0db32008-04-21 15:42:01 -0700544 else
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800545 rate_n_flags |= iwl_rates[index].plcp_mimo3;
Zhu Yib481de92007-09-25 17:54:57 -0700546 } else {
Tomas Winkler978785a2008-12-19 10:37:31 +0800547 IWL_ERR(priv, "Invalid tbl->lq_type %d\n", tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -0700548 }
549
Guy Cohen39e88502008-04-23 17:14:57 -0700550 rate_n_flags |= ((tbl->ant_type << RATE_MCS_ANT_POS) &
Guy Cohenfde0db32008-04-21 15:42:01 -0700551 RATE_MCS_ANT_ABC_MSK);
Zhu Yib481de92007-09-25 17:54:57 -0700552
Guy Cohen39e88502008-04-23 17:14:57 -0700553 if (is_Ht(tbl->lq_type)) {
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700554 if (tbl->is_ht40) {
Guy Cohen39e88502008-04-23 17:14:57 -0700555 if (tbl->is_dup)
556 rate_n_flags |= RATE_MCS_DUP_MSK;
557 else
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700558 rate_n_flags |= RATE_MCS_HT40_MSK;
Guy Cohen39e88502008-04-23 17:14:57 -0700559 }
560 if (tbl->is_SGI)
561 rate_n_flags |= RATE_MCS_SGI_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700562
Guy Cohen39e88502008-04-23 17:14:57 -0700563 if (use_green) {
564 rate_n_flags |= RATE_MCS_GF_MSK;
565 if (is_siso(tbl->lq_type) && tbl->is_SGI) {
566 rate_n_flags &= ~RATE_MCS_SGI_MSK;
Tomas Winkler978785a2008-12-19 10:37:31 +0800567 IWL_ERR(priv, "GF was set with SGI:SISO\n");
Guy Cohen39e88502008-04-23 17:14:57 -0700568 }
569 }
Zhu Yib481de92007-09-25 17:54:57 -0700570 }
Guy Cohen39e88502008-04-23 17:14:57 -0700571 return rate_n_flags;
Zhu Yib481de92007-09-25 17:54:57 -0700572}
573
Ben Cahill77626352007-11-29 11:09:44 +0800574/*
575 * Interpret uCode API's rate_n_flags format,
576 * fill "search" or "active" tx mode table.
577 */
Guy Cohen39e88502008-04-23 17:14:57 -0700578static int rs_get_tbl_info_from_mcs(const u32 rate_n_flags,
Johannes Berg8318d782008-01-24 19:38:38 +0100579 enum ieee80211_band band,
Tomas Winklere227cea2008-07-18 13:53:05 +0800580 struct iwl_scale_tbl_info *tbl,
Zhu Yib481de92007-09-25 17:54:57 -0700581 int *rate_idx)
582{
Guy Cohen39e88502008-04-23 17:14:57 -0700583 u32 ant_msk = (rate_n_flags & RATE_MCS_ANT_ABC_MSK);
584 u8 num_of_ant = get_num_of_ant_from_rate(rate_n_flags);
585 u8 mcs;
Zhu Yib481de92007-09-25 17:54:57 -0700586
Tomas Winklere7d326ac2008-06-12 09:47:11 +0800587 *rate_idx = iwl_hwrate_to_plcp_idx(rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -0700588
Guy Cohenfde0db32008-04-21 15:42:01 -0700589 if (*rate_idx == IWL_RATE_INVALID) {
Zhu Yib481de92007-09-25 17:54:57 -0700590 *rate_idx = -1;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800591 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -0700592 }
Ben Cahill77626352007-11-29 11:09:44 +0800593 tbl->is_SGI = 0; /* default legacy setup */
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700594 tbl->is_ht40 = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700595 tbl->is_dup = 0;
Guy Cohenfde0db32008-04-21 15:42:01 -0700596 tbl->ant_type = (ant_msk >> RATE_MCS_ANT_POS);
597 tbl->lq_type = LQ_NONE;
Mohamed Abbasd6e93392009-05-08 13:44:39 -0700598 tbl->max_search = IWL_MAX_SEARCH;
Zhu Yib481de92007-09-25 17:54:57 -0700599
Ben Cahill77626352007-11-29 11:09:44 +0800600 /* legacy rate format */
Guy Cohen39e88502008-04-23 17:14:57 -0700601 if (!(rate_n_flags & RATE_MCS_HT_MSK)) {
Guy Cohenfde0db32008-04-21 15:42:01 -0700602 if (num_of_ant == 1) {
Johannes Berg8318d782008-01-24 19:38:38 +0100603 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700604 tbl->lq_type = LQ_A;
605 else
606 tbl->lq_type = LQ_G;
Zhu Yib481de92007-09-25 17:54:57 -0700607 }
Guy Cohenfde0db32008-04-21 15:42:01 -0700608 /* HT rate format */
Zhu Yib481de92007-09-25 17:54:57 -0700609 } else {
Guy Cohen39e88502008-04-23 17:14:57 -0700610 if (rate_n_flags & RATE_MCS_SGI_MSK)
Zhu Yib481de92007-09-25 17:54:57 -0700611 tbl->is_SGI = 1;
612
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700613 if ((rate_n_flags & RATE_MCS_HT40_MSK) ||
Guy Cohen39e88502008-04-23 17:14:57 -0700614 (rate_n_flags & RATE_MCS_DUP_MSK))
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700615 tbl->is_ht40 = 1;
Zhu Yib481de92007-09-25 17:54:57 -0700616
Guy Cohen39e88502008-04-23 17:14:57 -0700617 if (rate_n_flags & RATE_MCS_DUP_MSK)
Zhu Yib481de92007-09-25 17:54:57 -0700618 tbl->is_dup = 1;
Guy Cohenfde0db32008-04-21 15:42:01 -0700619
Guy Cohen39e88502008-04-23 17:14:57 -0700620 mcs = rs_extract_rate(rate_n_flags);
Guy Cohenfde0db32008-04-21 15:42:01 -0700621
Guy Cohen39e88502008-04-23 17:14:57 -0700622 /* SISO */
623 if (mcs <= IWL_RATE_SISO_60M_PLCP) {
Guy Cohenfde0db32008-04-21 15:42:01 -0700624 if (num_of_ant == 1)
625 tbl->lq_type = LQ_SISO; /*else NONE*/
626 /* MIMO2 */
Guy Cohen39e88502008-04-23 17:14:57 -0700627 } else if (mcs <= IWL_RATE_MIMO2_60M_PLCP) {
Guy Cohenfde0db32008-04-21 15:42:01 -0700628 if (num_of_ant == 2)
629 tbl->lq_type = LQ_MIMO2;
630 /* MIMO3 */
631 } else {
Mohamed Abbasd6e93392009-05-08 13:44:39 -0700632 if (num_of_ant == 3) {
633 tbl->max_search = IWL_MAX_11N_MIMO3_SEARCH;
Guy Cohenfde0db32008-04-21 15:42:01 -0700634 tbl->lq_type = LQ_MIMO3;
Mohamed Abbasd6e93392009-05-08 13:44:39 -0700635 }
Guy Cohenfde0db32008-04-21 15:42:01 -0700636 }
Zhu Yib481de92007-09-25 17:54:57 -0700637 }
638 return 0;
639}
Guy Cohenaade00c2008-04-23 17:14:59 -0700640
641/* switch to another antenna/antennas and return 1 */
642/* if no other valid antenna found, return 0 */
643static int rs_toggle_antenna(u32 valid_ant, u32 *rate_n_flags,
Tomas Winklere227cea2008-07-18 13:53:05 +0800644 struct iwl_scale_tbl_info *tbl)
Zhu Yib481de92007-09-25 17:54:57 -0700645{
Guy Cohenaade00c2008-04-23 17:14:59 -0700646 u8 new_ant_type;
647
648 if (!tbl->ant_type || tbl->ant_type > ANT_ABC)
649 return 0;
650
651 if (!rs_is_valid_ant(valid_ant, tbl->ant_type))
652 return 0;
653
654 new_ant_type = ant_toggle_lookup[tbl->ant_type];
655
656 while ((new_ant_type != tbl->ant_type) &&
657 !rs_is_valid_ant(valid_ant, new_ant_type))
658 new_ant_type = ant_toggle_lookup[new_ant_type];
659
660 if (new_ant_type == tbl->ant_type)
661 return 0;
662
663 tbl->ant_type = new_ant_type;
664 *rate_n_flags &= ~RATE_MCS_ANT_ABC_MSK;
665 *rate_n_flags |= new_ant_type << RATE_MCS_ANT_POS;
666 return 1;
Zhu Yib481de92007-09-25 17:54:57 -0700667}
668
Daniel C Halperinb2617932009-08-13 13:30:59 -0700669/**
670 * Green-field mode is valid if the station supports it and
671 * there are no non-GF stations present in the BSS.
672 */
673static inline u8 rs_use_green(struct ieee80211_sta *sta,
Johannes Bergfad95bf2009-09-11 10:38:15 -0700674 struct iwl_ht_config *ht_conf)
Zhu Yib481de92007-09-25 17:54:57 -0700675{
Daniel C Halperinb2617932009-08-13 13:30:59 -0700676 return (sta->ht_cap.cap & IEEE80211_HT_CAP_GRN_FLD) &&
677 !(ht_conf->non_GF_STA_present);
Guy Cohenf935a6d2008-04-23 17:15:02 -0700678}
Zhu Yib481de92007-09-25 17:54:57 -0700679
680/**
681 * rs_get_supported_rates - get the available rates
682 *
683 * if management frame or broadcast frame only return
684 * basic available rates.
685 *
686 */
Tomas Winklere227cea2008-07-18 13:53:05 +0800687static u16 rs_get_supported_rates(struct iwl_lq_sta *lq_sta,
688 struct ieee80211_hdr *hdr,
689 enum iwl_table_type rate_type)
Zhu Yib481de92007-09-25 17:54:57 -0700690{
Zhu Yib481de92007-09-25 17:54:57 -0700691 if (hdr && is_multicast_ether_addr(hdr->addr1) &&
Guy Coheneecd6e52008-04-23 17:14:58 -0700692 lq_sta->active_rate_basic)
693 return lq_sta->active_rate_basic;
694
695 if (is_legacy(rate_type)) {
696 return lq_sta->active_legacy_rate;
697 } else {
698 if (is_siso(rate_type))
699 return lq_sta->active_siso_rate;
700 else if (is_mimo2(rate_type))
701 return lq_sta->active_mimo2_rate;
702 else
703 return lq_sta->active_mimo3_rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -0800704 }
Zhu Yib481de92007-09-25 17:54:57 -0700705}
706
Ester Kummerbf403db2008-05-05 10:22:40 +0800707static u16 rs_get_adjacent_rate(struct iwl_priv *priv, u8 index, u16 rate_mask,
708 int rate_type)
Zhu Yib481de92007-09-25 17:54:57 -0700709{
710 u8 high = IWL_RATE_INVALID;
711 u8 low = IWL_RATE_INVALID;
712
Ian Schram01ebd062007-10-25 17:15:22 +0800713 /* 802.11A or ht walks to the next literal adjacent rate in
Zhu Yib481de92007-09-25 17:54:57 -0700714 * the rate table */
715 if (is_a_band(rate_type) || !is_legacy(rate_type)) {
716 int i;
717 u32 mask;
718
719 /* Find the previous rate that is in the rate mask */
720 i = index - 1;
721 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
722 if (rate_mask & mask) {
723 low = i;
724 break;
725 }
726 }
727
728 /* Find the next rate that is in the rate mask */
729 i = index + 1;
730 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
731 if (rate_mask & mask) {
732 high = i;
733 break;
734 }
735 }
736
737 return (high << 8) | low;
738 }
739
740 low = index;
741 while (low != IWL_RATE_INVALID) {
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800742 low = iwl_rates[low].prev_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700743 if (low == IWL_RATE_INVALID)
744 break;
745 if (rate_mask & (1 << low))
746 break;
Tomas Winklere1623442009-01-27 14:27:56 -0800747 IWL_DEBUG_RATE(priv, "Skipping masked lower rate: %d\n", low);
Zhu Yib481de92007-09-25 17:54:57 -0700748 }
749
750 high = index;
751 while (high != IWL_RATE_INVALID) {
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800752 high = iwl_rates[high].next_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700753 if (high == IWL_RATE_INVALID)
754 break;
755 if (rate_mask & (1 << high))
756 break;
Tomas Winklere1623442009-01-27 14:27:56 -0800757 IWL_DEBUG_RATE(priv, "Skipping masked higher rate: %d\n", high);
Zhu Yib481de92007-09-25 17:54:57 -0700758 }
759
760 return (high << 8) | low;
761}
762
Tomas Winklere227cea2008-07-18 13:53:05 +0800763static u32 rs_get_lower_rate(struct iwl_lq_sta *lq_sta,
764 struct iwl_scale_tbl_info *tbl,
765 u8 scale_index, u8 ht_possible)
Zhu Yib481de92007-09-25 17:54:57 -0700766{
Zhu Yib481de92007-09-25 17:54:57 -0700767 s32 low;
768 u16 rate_mask;
769 u16 high_low;
770 u8 switch_to_legacy = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -0800771 u8 is_green = lq_sta->is_green;
Wey-Yi Guy2ff65782009-09-11 10:38:18 -0700772 struct iwl_priv *priv = lq_sta->drv;
Zhu Yib481de92007-09-25 17:54:57 -0700773
774 /* check if we need to switch from HT to legacy rates.
775 * assumption is that mandatory rates (1Mbps or 6Mbps)
776 * are always supported (spec demand) */
777 if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_index)) {
778 switch_to_legacy = 1;
779 scale_index = rs_ht_to_legacy[scale_index];
Johannes Berg8318d782008-01-24 19:38:38 +0100780 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700781 tbl->lq_type = LQ_A;
782 else
783 tbl->lq_type = LQ_G;
784
Guy Cohen69fdb302008-04-23 17:15:01 -0700785 if (num_of_ant(tbl->ant_type) > 1)
Wey-Yi Guy2ff65782009-09-11 10:38:18 -0700786 tbl->ant_type =
787 first_antenna(priv->hw_params.valid_tx_ant);
Zhu Yib481de92007-09-25 17:54:57 -0700788
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700789 tbl->is_ht40 = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700790 tbl->is_SGI = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -0700791 tbl->max_search = IWL_MAX_SEARCH;
Zhu Yib481de92007-09-25 17:54:57 -0700792 }
793
Guy Coheneecd6e52008-04-23 17:14:58 -0700794 rate_mask = rs_get_supported_rates(lq_sta, NULL, tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -0700795
Ben Cahill77626352007-11-29 11:09:44 +0800796 /* Mask with station rate restriction */
Zhu Yib481de92007-09-25 17:54:57 -0700797 if (is_legacy(tbl->lq_type)) {
Ben Cahill77626352007-11-29 11:09:44 +0800798 /* supp_rates has no CCK bits in A mode */
Johannes Berg8318d782008-01-24 19:38:38 +0100799 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700800 rate_mask = (u16)(rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -0800801 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
Zhu Yib481de92007-09-25 17:54:57 -0700802 else
Tomas Winklerc33104f2008-01-14 17:46:21 -0800803 rate_mask = (u16)(rate_mask & lq_sta->supp_rates);
Zhu Yib481de92007-09-25 17:54:57 -0700804 }
805
Ben Cahill77626352007-11-29 11:09:44 +0800806 /* If we switched from HT to legacy, check current rate */
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800807 if (switch_to_legacy && (rate_mask & (1 << scale_index))) {
Guy Cohen39e88502008-04-23 17:14:57 -0700808 low = scale_index;
809 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700810 }
811
Ester Kummerbf403db2008-05-05 10:22:40 +0800812 high_low = rs_get_adjacent_rate(lq_sta->drv, scale_index, rate_mask,
813 tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -0700814 low = high_low & 0xff;
815
Guy Cohen39e88502008-04-23 17:14:57 -0700816 if (low == IWL_RATE_INVALID)
817 low = scale_index;
818
819out:
Tomas Winkler978785a2008-12-19 10:37:31 +0800820 return rate_n_flags_from_tbl(lq_sta->drv, tbl, low, is_green);
Zhu Yib481de92007-09-25 17:54:57 -0700821}
822
Ben Cahill77626352007-11-29 11:09:44 +0800823/*
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700824 * Simple function to compare two rate scale table types
825 */
826static bool table_type_matches(struct iwl_scale_tbl_info *a,
827 struct iwl_scale_tbl_info *b)
828{
829 return (a->lq_type == b->lq_type) && (a->ant_type == b->ant_type) &&
830 (a->is_SGI == b->is_SGI);
831}
832/*
833 * Static function to get the expected throughput from an iwl_scale_tbl_info
834 * that wraps a NULL pointer check
835 */
836static s32 get_expected_tpt(struct iwl_scale_tbl_info *tbl, int rs_index)
837{
838 if (tbl->expected_tpt)
839 return tbl->expected_tpt[rs_index];
840 return 0;
841}
842
843/*
Ben Cahill77626352007-11-29 11:09:44 +0800844 * mac80211 sends us Tx status
845 */
Johannes Berg4b7679a2008-09-18 18:14:18 +0200846static void rs_tx_status(void *priv_r, struct ieee80211_supported_band *sband,
847 struct ieee80211_sta *sta, void *priv_sta,
Johannes Berge039fa42008-05-15 12:55:29 +0200848 struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -0700849{
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700850 int legacy_success;
851 int retries;
852 int rs_index, mac_index, i;
Tomas Winkler417f1142008-11-12 13:14:06 -0800853 struct iwl_lq_sta *lq_sta = priv_sta;
Tomas Winkler66c73db2008-04-15 16:01:40 -0700854 struct iwl_link_quality_cmd *table;
Zhu Yib481de92007-09-25 17:54:57 -0700855 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berg4b7679a2008-09-18 18:14:18 +0200856 struct iwl_priv *priv = (struct iwl_priv *)priv_r;
Johannes Berge039fa42008-05-15 12:55:29 +0200857 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Tomas Winklere227cea2008-07-18 13:53:05 +0800858 struct iwl_rate_scale_data *window = NULL;
Daniel C Halperin50273092009-08-28 09:44:45 -0700859 enum mac80211_rate_control_flags mac_flags;
Guy Cohen39e88502008-04-23 17:14:57 -0700860 u32 tx_rate;
Tomas Winklere227cea2008-07-18 13:53:05 +0800861 struct iwl_scale_tbl_info tbl_type;
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700862 struct iwl_scale_tbl_info *curr_tbl, *other_tbl;
Zhu Yib481de92007-09-25 17:54:57 -0700863 s32 tpt = 0;
864
Tomas Winklere1623442009-01-27 14:27:56 -0800865 IWL_DEBUG_RATE_LIMIT(priv, "get frame ack response, update rate scale window\n");
Zhu Yib481de92007-09-25 17:54:57 -0700866
Tomas Winkler417f1142008-11-12 13:14:06 -0800867 if (!ieee80211_is_data(hdr->frame_control) ||
Gábor Stefanik514d65c2009-04-23 19:36:08 +0200868 info->flags & IEEE80211_TX_CTL_NO_ACK)
Zhu Yib481de92007-09-25 17:54:57 -0700869 return;
870
Ron Rindjunsky99556432008-01-28 14:07:25 +0200871 /* This packet was aggregated but doesn't carry rate scale info */
Johannes Berge039fa42008-05-15 12:55:29 +0200872 if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
873 !(info->flags & IEEE80211_TX_STAT_AMPDU))
Ron Rindjunsky99556432008-01-28 14:07:25 +0200874 return;
875
Zhu Yib481de92007-09-25 17:54:57 -0700876
Johannes Berg05c914f2008-09-11 00:01:58 +0200877 if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) &&
Tomas Winklerc33104f2008-01-14 17:46:21 -0800878 !lq_sta->ibss_sta_added)
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700879 return;
Zhu Yib481de92007-09-25 17:54:57 -0700880
Ben Cahill77626352007-11-29 11:09:44 +0800881 /*
882 * Ignore this Tx frame response if its initial rate doesn't match
883 * that of latest Link Quality command. There may be stragglers
884 * from a previous Link Quality command, but we're no longer interested
885 * in those; they're either from the "active" mode while we're trying
886 * to check "search" mode, or a prior "search" mode after we've moved
887 * to a new "search" mode (which might become the new "active" mode).
888 */
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700889 table = &lq_sta->lq;
Guy Cohen39e88502008-04-23 17:14:57 -0700890 tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
891 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800892 if (priv->band == IEEE80211_BAND_5GHZ)
893 rs_index -= IWL_FIRST_OFDM_RATE;
Daniel C Halperin50273092009-08-28 09:44:45 -0700894 mac_flags = info->status.rates[0].flags;
895 mac_index = info->status.rates[0].idx;
Daniel C Halperin31513be2009-08-28 09:44:47 -0700896 /* For HT packets, map MCS to PLCP */
897 if (mac_flags & IEEE80211_TX_RC_MCS) {
898 mac_index &= RATE_MCS_CODE_MSK; /* Remove # of streams */
899 if (mac_index >= (IWL_RATE_9M_INDEX - IWL_FIRST_OFDM_RATE))
900 mac_index++;
Daniel C Halperin392a0ba2009-09-11 10:38:08 -0700901 /*
902 * mac80211 HT index is always zero-indexed; we need to move
903 * HT OFDM rates after CCK rates in 2.4 GHz band
904 */
905 if (priv->band == IEEE80211_BAND_2GHZ)
906 mac_index += IWL_FIRST_OFDM_RATE;
Daniel C Halperin31513be2009-08-28 09:44:47 -0700907 }
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700908 /* Here we actually compare this rate to the latest LQ command */
Daniel C Halperin50273092009-08-28 09:44:45 -0700909 if ((mac_index < 0) ||
910 (tbl_type.is_SGI != !!(mac_flags & IEEE80211_TX_RC_SHORT_GI)) ||
911 (tbl_type.is_ht40 != !!(mac_flags & IEEE80211_TX_RC_40_MHZ_WIDTH)) ||
912 (tbl_type.is_dup != !!(mac_flags & IEEE80211_TX_RC_DUP_DATA)) ||
Johannes Berge6a98542008-10-21 12:40:02 +0200913 (tbl_type.ant_type != info->antenna_sel_tx) ||
Daniel C Halperin50273092009-08-28 09:44:45 -0700914 (!!(tx_rate & RATE_MCS_HT_MSK) != !!(mac_flags & IEEE80211_TX_RC_MCS)) ||
915 (!!(tx_rate & RATE_MCS_GF_MSK) != !!(mac_flags & IEEE80211_TX_RC_GREEN_FIELD)) ||
Daniel C Halperin31513be2009-08-28 09:44:47 -0700916 (rs_index != mac_index)) {
917 IWL_DEBUG_RATE(priv, "initial rate %d does not match %d (0x%x)\n", mac_index, rs_index, tx_rate);
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700918 /*
919 * Since rates mis-match, the last LQ command may have failed.
920 * After IWL_MISSED_RATE_MAX mis-matches, resync the uCode with
921 * ... driver.
Mohamed Abbasd5e49032008-12-12 08:22:15 -0800922 */
Abbas, Mohamed7d049e52009-01-20 21:33:53 -0800923 lq_sta->missed_rate_counter++;
924 if (lq_sta->missed_rate_counter > IWL_MISSED_RATE_MAX) {
925 lq_sta->missed_rate_counter = 0;
926 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
927 }
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700928 /* Regardless, ignore this status info for outdated rate */
929 return;
930 } else
931 /* Rate did match, so reset the missed_rate_counter */
932 lq_sta->missed_rate_counter = 0;
933
934 /* Figure out if rate scale algorithm is in active or search table */
935 if (table_type_matches(&tbl_type,
936 &(lq_sta->lq_info[lq_sta->active_tbl]))) {
937 curr_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
938 other_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
939 } else if (table_type_matches(&tbl_type,
940 &lq_sta->lq_info[1 - lq_sta->active_tbl])) {
941 curr_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
942 other_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
943 } else {
944 IWL_DEBUG_RATE(priv, "Neither active nor search matches tx rate\n");
945 return;
Zhu Yib481de92007-09-25 17:54:57 -0700946 }
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700947 window = (struct iwl_rate_scale_data *)&(curr_tbl->win[0]);
Zhu Yib481de92007-09-25 17:54:57 -0700948
Ben Cahill77626352007-11-29 11:09:44 +0800949 /*
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700950 * Updating the frame history depends on whether packets were
951 * aggregated.
952 *
953 * For aggregation, all packets were transmitted at the same rate, the
954 * first index into rate scale table.
Ben Cahill77626352007-11-29 11:09:44 +0800955 */
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700956 if (info->flags & IEEE80211_TX_STAT_AMPDU) {
957 tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
958 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type,
959 &rs_index);
960 tpt = get_expected_tpt(curr_tbl, rs_index);
961 rs_collect_tx_data(window, rs_index, tpt,
962 info->status.ampdu_ack_len,
963 info->status.ampdu_ack_map);
Zhu Yib481de92007-09-25 17:54:57 -0700964
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700965 /* Update success/fail counts if not searching for new mode */
966 if (lq_sta->stay_in_tbl) {
Johannes Berge039fa42008-05-15 12:55:29 +0200967 lq_sta->total_success += info->status.ampdu_ack_map;
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700968 lq_sta->total_failed += (info->status.ampdu_ack_len -
969 info->status.ampdu_ack_map);
970 }
971 } else {
972 /*
973 * For legacy, update frame history with for each Tx retry.
974 */
975 retries = info->status.rates[0].count - 1;
976 /* HW doesn't send more than 15 retries */
977 retries = min(retries, 15);
978
979 /* The last transmission may have been successful */
980 legacy_success = !!(info->flags & IEEE80211_TX_STAT_ACK);
981 /* Collect data for each rate used during failed TX attempts */
982 for (i = 0; i <= retries; ++i) {
983 tx_rate = le32_to_cpu(table->rs_table[i].rate_n_flags);
984 rs_get_tbl_info_from_mcs(tx_rate, priv->band,
985 &tbl_type, &rs_index);
986 /*
987 * Only collect stats if retried rate is in the same RS
988 * table as active/search.
989 */
990 if (table_type_matches(&tbl_type, curr_tbl))
991 tpt = get_expected_tpt(curr_tbl, rs_index);
992 else if (table_type_matches(&tbl_type, other_tbl))
993 tpt = get_expected_tpt(other_tbl, rs_index);
Ron Rindjunsky99556432008-01-28 14:07:25 +0200994 else
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700995 continue;
996
997 /* Constants mean 1 transmission, 0 successes */
998 if (i < retries)
999 rs_collect_tx_data(window, rs_index, tpt, 1,
1000 0);
1001 else
1002 rs_collect_tx_data(window, rs_index, tpt, 1,
1003 legacy_success);
1004 }
1005
1006 /* Update success/fail counts if not searching for new mode */
1007 if (lq_sta->stay_in_tbl) {
1008 lq_sta->total_success += legacy_success;
1009 lq_sta->total_failed += retries + (1 - legacy_success);
Ron Rindjunsky99556432008-01-28 14:07:25 +02001010 }
Zhu Yib481de92007-09-25 17:54:57 -07001011 }
Daniel C Halperin95407aa2009-09-17 10:43:48 -07001012 /* The last TX rate is cached in lq_sta; it's set in if/else above */
1013 lq_sta->last_rate_n_flags = tx_rate;
Zhu Yib481de92007-09-25 17:54:57 -07001014
Ben Cahill77626352007-11-29 11:09:44 +08001015 /* See if there's a better rate or modulation mode to try. */
Abbas, Mohamedc338ba32009-01-21 10:58:02 -08001016 if (sta && sta->supp_rates[sband->band])
Gábor Stefanik514d65c2009-04-23 19:36:08 +02001017 rs_rate_scale_perform(priv, skb, sta, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07001018}
1019
Ben Cahill77626352007-11-29 11:09:44 +08001020/*
1021 * Begin a period of staying with a selected modulation mode.
1022 * Set "stay_in_tbl" flag to prevent any mode switches.
1023 * Set frame tx success limits according to legacy vs. high-throughput,
1024 * and reset overall (spanning all rates) tx success history statistics.
1025 * These control how long we stay using same modulation mode before
1026 * searching for a new mode.
1027 */
Ester Kummerbf403db2008-05-05 10:22:40 +08001028static void rs_set_stay_in_table(struct iwl_priv *priv, u8 is_legacy,
Tomas Winklere227cea2008-07-18 13:53:05 +08001029 struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07001030{
Tomas Winklere1623442009-01-27 14:27:56 -08001031 IWL_DEBUG_RATE(priv, "we are staying in the same table\n");
Tomas Winklerc33104f2008-01-14 17:46:21 -08001032 lq_sta->stay_in_tbl = 1; /* only place this gets set */
Zhu Yib481de92007-09-25 17:54:57 -07001033 if (is_legacy) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001034 lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT;
1035 lq_sta->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
1036 lq_sta->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
Zhu Yib481de92007-09-25 17:54:57 -07001037 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001038 lq_sta->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
1039 lq_sta->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
1040 lq_sta->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
Zhu Yib481de92007-09-25 17:54:57 -07001041 }
Tomas Winklerc33104f2008-01-14 17:46:21 -08001042 lq_sta->table_count = 0;
1043 lq_sta->total_failed = 0;
1044 lq_sta->total_success = 0;
Mohamed Abbas447fee72009-04-20 14:37:02 -07001045 lq_sta->flush_timer = jiffies;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001046 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001047}
1048
Ben Cahill77626352007-11-29 11:09:44 +08001049/*
1050 * Find correct throughput table for given mode of modulation
1051 */
Tomas Winklere227cea2008-07-18 13:53:05 +08001052static void rs_set_expected_tpt_table(struct iwl_lq_sta *lq_sta,
1053 struct iwl_scale_tbl_info *tbl)
Zhu Yib481de92007-09-25 17:54:57 -07001054{
1055 if (is_legacy(tbl->lq_type)) {
1056 if (!is_a_band(tbl->lq_type))
1057 tbl->expected_tpt = expected_tpt_G;
1058 else
1059 tbl->expected_tpt = expected_tpt_A;
1060 } else if (is_siso(tbl->lq_type)) {
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001061 if (tbl->is_ht40 && !lq_sta->is_dup)
Zhu Yib481de92007-09-25 17:54:57 -07001062 if (tbl->is_SGI)
1063 tbl->expected_tpt = expected_tpt_siso40MHzSGI;
1064 else
1065 tbl->expected_tpt = expected_tpt_siso40MHz;
1066 else if (tbl->is_SGI)
1067 tbl->expected_tpt = expected_tpt_siso20MHzSGI;
1068 else
1069 tbl->expected_tpt = expected_tpt_siso20MHz;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001070 } else if (is_mimo2(tbl->lq_type)) {
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001071 if (tbl->is_ht40 && !lq_sta->is_dup)
Zhu Yib481de92007-09-25 17:54:57 -07001072 if (tbl->is_SGI)
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001073 tbl->expected_tpt = expected_tpt_mimo2_40MHzSGI;
Zhu Yib481de92007-09-25 17:54:57 -07001074 else
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001075 tbl->expected_tpt = expected_tpt_mimo2_40MHz;
Zhu Yib481de92007-09-25 17:54:57 -07001076 else if (tbl->is_SGI)
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001077 tbl->expected_tpt = expected_tpt_mimo2_20MHzSGI;
Zhu Yib481de92007-09-25 17:54:57 -07001078 else
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001079 tbl->expected_tpt = expected_tpt_mimo2_20MHz;
1080 } else if (is_mimo3(tbl->lq_type)) {
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001081 if (tbl->is_ht40 && !lq_sta->is_dup)
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001082 if (tbl->is_SGI)
1083 tbl->expected_tpt = expected_tpt_mimo3_40MHzSGI;
1084 else
1085 tbl->expected_tpt = expected_tpt_mimo3_40MHz;
1086 else if (tbl->is_SGI)
1087 tbl->expected_tpt = expected_tpt_mimo3_20MHzSGI;
1088 else
1089 tbl->expected_tpt = expected_tpt_mimo3_20MHz;
Zhu Yib481de92007-09-25 17:54:57 -07001090 } else
1091 tbl->expected_tpt = expected_tpt_G;
1092}
1093
Ben Cahill77626352007-11-29 11:09:44 +08001094/*
1095 * Find starting rate for new "search" high-throughput mode of modulation.
1096 * Goal is to find lowest expected rate (under perfect conditions) that is
1097 * above the current measured throughput of "active" mode, to give new mode
1098 * a fair chance to prove itself without too many challenges.
1099 *
1100 * This gets called when transitioning to more aggressive modulation
1101 * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
1102 * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
1103 * to decrease to match "active" throughput. When moving from MIMO to SISO,
1104 * bit rate will typically need to increase, but not if performance was bad.
1105 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001106static s32 rs_get_best_rate(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001107 struct iwl_lq_sta *lq_sta,
1108 struct iwl_scale_tbl_info *tbl, /* "search" */
Guy Cohenf935a6d2008-04-23 17:15:02 -07001109 u16 rate_mask, s8 index)
Zhu Yib481de92007-09-25 17:54:57 -07001110{
Ben Cahill77626352007-11-29 11:09:44 +08001111 /* "active" values */
Tomas Winklere227cea2008-07-18 13:53:05 +08001112 struct iwl_scale_tbl_info *active_tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001113 &(lq_sta->lq_info[lq_sta->active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07001114 s32 active_sr = active_tbl->win[index].success_ratio;
Zhu Yib481de92007-09-25 17:54:57 -07001115 s32 active_tpt = active_tbl->expected_tpt[index];
Ben Cahill77626352007-11-29 11:09:44 +08001116
1117 /* expected "search" throughput */
1118 s32 *tpt_tbl = tbl->expected_tpt;
1119
1120 s32 new_rate, high, low, start_hi;
Zhu Yib481de92007-09-25 17:54:57 -07001121 u16 high_low;
Guy Cohenf935a6d2008-04-23 17:15:02 -07001122 s8 rate = index;
Zhu Yib481de92007-09-25 17:54:57 -07001123
1124 new_rate = high = low = start_hi = IWL_RATE_INVALID;
1125
1126 for (; ;) {
Ester Kummerbf403db2008-05-05 10:22:40 +08001127 high_low = rs_get_adjacent_rate(priv, rate, rate_mask,
1128 tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -07001129
1130 low = high_low & 0xff;
1131 high = (high_low >> 8) & 0xff;
1132
Ben Cahill77626352007-11-29 11:09:44 +08001133 /*
1134 * Lower the "search" bit rate, to give new "search" mode
1135 * approximately the same throughput as "active" if:
1136 *
1137 * 1) "Active" mode has been working modestly well (but not
1138 * great), and expected "search" throughput (under perfect
1139 * conditions) at candidate rate is above the actual
1140 * measured "active" throughput (but less than expected
1141 * "active" throughput under perfect conditions).
1142 * OR
1143 * 2) "Active" mode has been working perfectly or very well
1144 * and expected "search" throughput (under perfect
1145 * conditions) at candidate rate is above expected
1146 * "active" throughput (under perfect conditions).
1147 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001148 if ((((100 * tpt_tbl[rate]) > lq_sta->last_tpt) &&
Zhu Yib481de92007-09-25 17:54:57 -07001149 ((active_sr > IWL_RATE_DECREASE_TH) &&
1150 (active_sr <= IWL_RATE_HIGH_TH) &&
1151 (tpt_tbl[rate] <= active_tpt))) ||
1152 ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
1153 (tpt_tbl[rate] > active_tpt))) {
1154
Ben Cahill77626352007-11-29 11:09:44 +08001155 /* (2nd or later pass)
1156 * If we've already tried to raise the rate, and are
1157 * now trying to lower it, use the higher rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001158 if (start_hi != IWL_RATE_INVALID) {
1159 new_rate = start_hi;
1160 break;
1161 }
Ben Cahill77626352007-11-29 11:09:44 +08001162
Zhu Yib481de92007-09-25 17:54:57 -07001163 new_rate = rate;
Ben Cahill77626352007-11-29 11:09:44 +08001164
1165 /* Loop again with lower rate */
Zhu Yib481de92007-09-25 17:54:57 -07001166 if (low != IWL_RATE_INVALID)
1167 rate = low;
Ben Cahill77626352007-11-29 11:09:44 +08001168
1169 /* Lower rate not available, use the original */
Zhu Yib481de92007-09-25 17:54:57 -07001170 else
1171 break;
Ben Cahill77626352007-11-29 11:09:44 +08001172
1173 /* Else try to raise the "search" rate to match "active" */
Zhu Yib481de92007-09-25 17:54:57 -07001174 } else {
Ben Cahill77626352007-11-29 11:09:44 +08001175 /* (2nd or later pass)
1176 * If we've already tried to lower the rate, and are
1177 * now trying to raise it, use the lower rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001178 if (new_rate != IWL_RATE_INVALID)
1179 break;
Ben Cahill77626352007-11-29 11:09:44 +08001180
1181 /* Loop again with higher rate */
Zhu Yib481de92007-09-25 17:54:57 -07001182 else if (high != IWL_RATE_INVALID) {
1183 start_hi = high;
1184 rate = high;
Ben Cahill77626352007-11-29 11:09:44 +08001185
1186 /* Higher rate not available, use the original */
Zhu Yib481de92007-09-25 17:54:57 -07001187 } else {
Guy Cohen90d77952008-09-09 10:54:53 +08001188 new_rate = rate;
Zhu Yib481de92007-09-25 17:54:57 -07001189 break;
1190 }
1191 }
1192 }
1193
1194 return new_rate;
1195}
Zhu Yib481de92007-09-25 17:54:57 -07001196
Ben Cahill77626352007-11-29 11:09:44 +08001197/*
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001198 * Set up search table for MIMO2
Ben Cahill77626352007-11-29 11:09:44 +08001199 */
Guy Cohenfde0db32008-04-21 15:42:01 -07001200static int rs_switch_to_mimo2(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001201 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001202 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001203 struct ieee80211_sta *sta,
Tomas Winklere227cea2008-07-18 13:53:05 +08001204 struct iwl_scale_tbl_info *tbl, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001205{
Zhu Yib481de92007-09-25 17:54:57 -07001206 u16 rate_mask;
1207 s32 rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001208 s8 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07001209
Luis R. Rodriguezde27e642008-12-23 15:58:44 -08001210 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
Zhu Yib481de92007-09-25 17:54:57 -07001211 return -1;
1212
Johannes Bergd9fe60d2008-10-09 12:13:49 +02001213 if (((sta->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 2)
Tomas Winkler00c5ae22008-09-03 11:26:42 +08001214 == WLAN_HT_CAP_SM_PS_STATIC)
Zhu Yib481de92007-09-25 17:54:57 -07001215 return -1;
1216
Ben Cahill77626352007-11-29 11:09:44 +08001217 /* Need both Tx chains/antennas to support MIMO */
Guy Cohenaade00c2008-04-23 17:14:59 -07001218 if (priv->hw_params.tx_chains_num < 2)
Zhu Yib481de92007-09-25 17:54:57 -07001219 return -1;
1220
Tomas Winklere1623442009-01-27 14:27:56 -08001221 IWL_DEBUG_RATE(priv, "LQ: try to switch to MIMO2\n");
Guy Cohen39e88502008-04-23 17:14:57 -07001222
1223 tbl->lq_type = LQ_MIMO2;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001224 tbl->is_dup = lq_sta->is_dup;
Zhu Yib481de92007-09-25 17:54:57 -07001225 tbl->action = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001226 tbl->max_search = IWL_MAX_SEARCH;
Guy Cohen39e88502008-04-23 17:14:57 -07001227 rate_mask = lq_sta->active_mimo2_rate;
1228
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001229 if (iwl_is_ht40_tx_allowed(priv, &sta->ht_cap))
1230 tbl->is_ht40 = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001231 else
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001232 tbl->is_ht40 = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001233
Guy Coheneecd6e52008-04-23 17:14:58 -07001234 rs_set_expected_tpt_table(lq_sta, tbl);
Zhu Yib481de92007-09-25 17:54:57 -07001235
Guy Cohenf935a6d2008-04-23 17:15:02 -07001236 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
Zhu Yib481de92007-09-25 17:54:57 -07001237
Tomas Winklere1623442009-01-27 14:27:56 -08001238 IWL_DEBUG_RATE(priv, "LQ: MIMO2 best rate %d mask %X\n", rate, rate_mask);
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001239 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1240 IWL_DEBUG_RATE(priv, "Can't switch with index %d rate mask %x\n",
1241 rate, rate_mask);
1242 return -1;
1243 }
1244 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
Guy Cohen39e88502008-04-23 17:14:57 -07001245
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001246 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
1247 tbl->current_rate, is_green);
1248 return 0;
1249}
1250
1251/*
1252 * Set up search table for MIMO3
1253 */
1254static int rs_switch_to_mimo3(struct iwl_priv *priv,
1255 struct iwl_lq_sta *lq_sta,
1256 struct ieee80211_conf *conf,
1257 struct ieee80211_sta *sta,
1258 struct iwl_scale_tbl_info *tbl, int index)
1259{
1260 u16 rate_mask;
1261 s32 rate;
1262 s8 is_green = lq_sta->is_green;
1263
1264 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
1265 return -1;
1266
1267 if (((sta->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 2)
1268 == WLAN_HT_CAP_SM_PS_STATIC)
1269 return -1;
1270
1271 /* Need both Tx chains/antennas to support MIMO */
1272 if (priv->hw_params.tx_chains_num < 3)
1273 return -1;
1274
1275 IWL_DEBUG_RATE(priv, "LQ: try to switch to MIMO3\n");
1276
1277 tbl->lq_type = LQ_MIMO3;
1278 tbl->is_dup = lq_sta->is_dup;
1279 tbl->action = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001280 tbl->max_search = IWL_MAX_11N_MIMO3_SEARCH;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001281 rate_mask = lq_sta->active_mimo3_rate;
1282
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001283 if (iwl_is_ht40_tx_allowed(priv, &sta->ht_cap))
1284 tbl->is_ht40 = 1;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001285 else
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001286 tbl->is_ht40 = 0;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001287
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001288 rs_set_expected_tpt_table(lq_sta, tbl);
1289
1290 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
1291
1292 IWL_DEBUG_RATE(priv, "LQ: MIMO3 best rate %d mask %X\n",
1293 rate, rate_mask);
Guy Cohen39e88502008-04-23 17:14:57 -07001294 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
Tomas Winklere1623442009-01-27 14:27:56 -08001295 IWL_DEBUG_RATE(priv, "Can't switch with index %d rate mask %x\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001296 rate, rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07001297 return -1;
Guy Cohen39e88502008-04-23 17:14:57 -07001298 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001299 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
Zhu Yib481de92007-09-25 17:54:57 -07001300
Tomas Winklere1623442009-01-27 14:27:56 -08001301 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001302 tbl->current_rate, is_green);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001303 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07001304}
1305
Ben Cahill77626352007-11-29 11:09:44 +08001306/*
1307 * Set up search table for SISO
1308 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001309static int rs_switch_to_siso(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001310 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001311 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001312 struct ieee80211_sta *sta,
Tomas Winklere227cea2008-07-18 13:53:05 +08001313 struct iwl_scale_tbl_info *tbl, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001314{
Zhu Yib481de92007-09-25 17:54:57 -07001315 u16 rate_mask;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001316 u8 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07001317 s32 rate;
1318
Luis R. Rodriguezde27e642008-12-23 15:58:44 -08001319 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
Zhu Yib481de92007-09-25 17:54:57 -07001320 return -1;
1321
Tomas Winklere1623442009-01-27 14:27:56 -08001322 IWL_DEBUG_RATE(priv, "LQ: try to switch to SISO\n");
Guy Cohen39e88502008-04-23 17:14:57 -07001323
Tomas Winklerc33104f2008-01-14 17:46:21 -08001324 tbl->is_dup = lq_sta->is_dup;
Zhu Yib481de92007-09-25 17:54:57 -07001325 tbl->lq_type = LQ_SISO;
1326 tbl->action = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001327 tbl->max_search = IWL_MAX_SEARCH;
Guy Cohen39e88502008-04-23 17:14:57 -07001328 rate_mask = lq_sta->active_siso_rate;
Zhu Yib481de92007-09-25 17:54:57 -07001329
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001330 if (iwl_is_ht40_tx_allowed(priv, &sta->ht_cap))
1331 tbl->is_ht40 = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001332 else
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001333 tbl->is_ht40 = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001334
Zhu Yib481de92007-09-25 17:54:57 -07001335 if (is_green)
Guy Cohen39e88502008-04-23 17:14:57 -07001336 tbl->is_SGI = 0; /*11n spec: no SGI in SISO+Greenfield*/
Zhu Yib481de92007-09-25 17:54:57 -07001337
Guy Coheneecd6e52008-04-23 17:14:58 -07001338 rs_set_expected_tpt_table(lq_sta, tbl);
Guy Cohenf935a6d2008-04-23 17:15:02 -07001339 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
Zhu Yib481de92007-09-25 17:54:57 -07001340
Tomas Winklere1623442009-01-27 14:27:56 -08001341 IWL_DEBUG_RATE(priv, "LQ: get best rate %d mask %X\n", rate, rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07001342 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
Tomas Winklere1623442009-01-27 14:27:56 -08001343 IWL_DEBUG_RATE(priv, "can not switch with index %d rate mask %x\n",
Zhu Yib481de92007-09-25 17:54:57 -07001344 rate, rate_mask);
1345 return -1;
1346 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001347 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
Tomas Winklere1623442009-01-27 14:27:56 -08001348 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001349 tbl->current_rate, is_green);
Mohamed Abbas403ab562007-11-06 22:06:25 -08001350 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07001351}
1352
Ben Cahill77626352007-11-29 11:09:44 +08001353/*
1354 * Try to switch to new modulation mode from legacy
1355 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001356static int rs_move_legacy_other(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001357 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001358 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001359 struct ieee80211_sta *sta,
Zhu Yib481de92007-09-25 17:54:57 -07001360 int index)
1361{
Tomas Winklere227cea2008-07-18 13:53:05 +08001362 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1363 struct iwl_scale_tbl_info *search_tbl =
1364 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1365 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1366 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1367 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001368 u8 start_action = tbl->action;
Guy Cohenfde0db32008-04-21 15:42:01 -07001369 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
Guy Cohen3110bef2008-09-09 10:54:54 +08001370 u8 tx_chains_num = priv->hw_params.tx_chains_num;
Guy Cohenfde0db32008-04-21 15:42:01 -07001371 int ret = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001372 u8 update_search_tbl_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001373
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001374 if (!iwl_ht_enabled(priv))
1375 /* stay in Legacy */
1376 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
Johannes Berg3ad3b922009-08-07 15:41:48 -07001377 else if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001378 tbl->action > IWL_LEGACY_SWITCH_SISO)
1379 tbl->action = IWL_LEGACY_SWITCH_SISO;
Zhu Yib481de92007-09-25 17:54:57 -07001380 for (; ;) {
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001381 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001382 switch (tbl->action) {
Guy Cohen3110bef2008-09-09 10:54:54 +08001383 case IWL_LEGACY_SWITCH_ANTENNA1:
1384 case IWL_LEGACY_SWITCH_ANTENNA2:
Tomas Winklere1623442009-01-27 14:27:56 -08001385 IWL_DEBUG_RATE(priv, "LQ: Legacy toggle Antenna\n");
Zhu Yib481de92007-09-25 17:54:57 -07001386
Guy Cohen3110bef2008-09-09 10:54:54 +08001387 if ((tbl->action == IWL_LEGACY_SWITCH_ANTENNA1 &&
1388 tx_chains_num <= 1) ||
1389 (tbl->action == IWL_LEGACY_SWITCH_ANTENNA2 &&
1390 tx_chains_num <= 2))
1391 break;
1392
Ben Cahill77626352007-11-29 11:09:44 +08001393 /* Don't change antenna if success has been great */
Zhu Yib481de92007-09-25 17:54:57 -07001394 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1395 break;
Ben Cahill77626352007-11-29 11:09:44 +08001396
Ben Cahill77626352007-11-29 11:09:44 +08001397 /* Set up search table to try other antenna */
Zhu Yib481de92007-09-25 17:54:57 -07001398 memcpy(search_tbl, tbl, sz);
1399
Guy Cohenaade00c2008-04-23 17:14:59 -07001400 if (rs_toggle_antenna(valid_tx_ant,
1401 &search_tbl->current_rate, search_tbl)) {
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001402 update_search_tbl_counter = 1;
Guy Cohen3110bef2008-09-09 10:54:54 +08001403 rs_set_expected_tpt_table(lq_sta, search_tbl);
Guy Cohenaade00c2008-04-23 17:14:59 -07001404 goto out;
1405 }
Guy Cohena5e8b502008-05-29 16:35:11 +08001406 break;
Zhu Yib481de92007-09-25 17:54:57 -07001407 case IWL_LEGACY_SWITCH_SISO:
Tomas Winklere1623442009-01-27 14:27:56 -08001408 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to SISO\n");
Ben Cahill77626352007-11-29 11:09:44 +08001409
1410 /* Set up search table to try SISO */
Zhu Yib481de92007-09-25 17:54:57 -07001411 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001412 search_tbl->is_SGI = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001413 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001414 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001415 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001416 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001417 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001418 }
Zhu Yib481de92007-09-25 17:54:57 -07001419
1420 break;
Guy Cohen3110bef2008-09-09 10:54:54 +08001421 case IWL_LEGACY_SWITCH_MIMO2_AB:
1422 case IWL_LEGACY_SWITCH_MIMO2_AC:
1423 case IWL_LEGACY_SWITCH_MIMO2_BC:
Tomas Winklere1623442009-01-27 14:27:56 -08001424 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to MIMO2\n");
Ben Cahill77626352007-11-29 11:09:44 +08001425
1426 /* Set up search table to try MIMO */
Zhu Yib481de92007-09-25 17:54:57 -07001427 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001428 search_tbl->is_SGI = 0;
Guy Cohen3110bef2008-09-09 10:54:54 +08001429
1430 if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AB)
1431 search_tbl->ant_type = ANT_AB;
1432 else if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AC)
1433 search_tbl->ant_type = ANT_AC;
1434 else
1435 search_tbl->ant_type = ANT_BC;
1436
1437 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1438 break;
1439
Guy Cohenfde0db32008-04-21 15:42:01 -07001440 ret = rs_switch_to_mimo2(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 break;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001447
1448 case IWL_LEGACY_SWITCH_MIMO3_ABC:
1449 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to MIMO3\n");
1450
1451 /* Set up search table to try MIMO3 */
1452 memcpy(search_tbl, tbl, sz);
1453 search_tbl->is_SGI = 0;
1454
1455 search_tbl->ant_type = ANT_ABC;
1456
1457 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1458 break;
1459
1460 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1461 search_tbl, index);
1462 if (!ret) {
1463 lq_sta->action_counter = 0;
1464 goto out;
1465 }
1466 break;
Zhu Yib481de92007-09-25 17:54:57 -07001467 }
1468 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001469 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001470 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
Zhu Yib481de92007-09-25 17:54:57 -07001471
1472 if (tbl->action == start_action)
1473 break;
1474
1475 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001476 search_tbl->lq_type = LQ_NONE;
Zhu Yib481de92007-09-25 17:54:57 -07001477 return 0;
1478
Guy Cohen3110bef2008-09-09 10:54:54 +08001479out:
1480 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001481 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001482 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001483 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001484 if (update_search_tbl_counter)
1485 search_tbl->action = tbl->action;
Zhu Yib481de92007-09-25 17:54:57 -07001486 return 0;
1487
1488}
1489
Ben Cahill77626352007-11-29 11:09:44 +08001490/*
1491 * Try to switch to new modulation mode from SISO
1492 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001493static int rs_move_siso_to_other(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001494 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001495 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001496 struct ieee80211_sta *sta, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001497{
Tomas Winklerc33104f2008-01-14 17:46:21 -08001498 u8 is_green = lq_sta->is_green;
Tomas Winklere227cea2008-07-18 13:53:05 +08001499 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1500 struct iwl_scale_tbl_info *search_tbl =
1501 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1502 struct iwl_rate_scale_data *window = &(tbl->win[index]);
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001503 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Tomas Winklere227cea2008-07-18 13:53:05 +08001504 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1505 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001506 u8 start_action = tbl->action;
Guy Cohenfde0db32008-04-21 15:42:01 -07001507 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
Guy Cohen3110bef2008-09-09 10:54:54 +08001508 u8 tx_chains_num = priv->hw_params.tx_chains_num;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001509 u8 update_search_tbl_counter = 0;
Guy Cohenfde0db32008-04-21 15:42:01 -07001510 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001511
Johannes Berg3ad3b922009-08-07 15:41:48 -07001512 if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001513 tbl->action > IWL_SISO_SWITCH_ANTENNA2) {
1514 /* stay in SISO */
1515 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1516 }
Zhu Yib481de92007-09-25 17:54:57 -07001517 for (;;) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001518 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001519 switch (tbl->action) {
Guy Cohen3110bef2008-09-09 10:54:54 +08001520 case IWL_SISO_SWITCH_ANTENNA1:
1521 case IWL_SISO_SWITCH_ANTENNA2:
Tomas Winklere1623442009-01-27 14:27:56 -08001522 IWL_DEBUG_RATE(priv, "LQ: SISO toggle Antenna\n");
Guy Cohen3110bef2008-09-09 10:54:54 +08001523
1524 if ((tbl->action == IWL_SISO_SWITCH_ANTENNA1 &&
1525 tx_chains_num <= 1) ||
1526 (tbl->action == IWL_SISO_SWITCH_ANTENNA2 &&
1527 tx_chains_num <= 2))
1528 break;
1529
Zhu Yib481de92007-09-25 17:54:57 -07001530 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1531 break;
Zhu Yib481de92007-09-25 17:54:57 -07001532
1533 memcpy(search_tbl, tbl, sz);
Guy Cohenaade00c2008-04-23 17:14:59 -07001534 if (rs_toggle_antenna(valid_tx_ant,
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001535 &search_tbl->current_rate, search_tbl)) {
1536 update_search_tbl_counter = 1;
Guy Cohenaade00c2008-04-23 17:14:59 -07001537 goto out;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001538 }
Guy Cohena5e8b502008-05-29 16:35:11 +08001539 break;
Guy Cohen3110bef2008-09-09 10:54:54 +08001540 case IWL_SISO_SWITCH_MIMO2_AB:
1541 case IWL_SISO_SWITCH_MIMO2_AC:
1542 case IWL_SISO_SWITCH_MIMO2_BC:
Tomas Winklere1623442009-01-27 14:27:56 -08001543 IWL_DEBUG_RATE(priv, "LQ: SISO switch to MIMO2\n");
Zhu Yib481de92007-09-25 17:54:57 -07001544 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001545 search_tbl->is_SGI = 0;
Guy Cohen3110bef2008-09-09 10:54:54 +08001546
1547 if (tbl->action == IWL_SISO_SWITCH_MIMO2_AB)
1548 search_tbl->ant_type = ANT_AB;
1549 else if (tbl->action == IWL_SISO_SWITCH_MIMO2_AC)
1550 search_tbl->ant_type = ANT_AC;
1551 else
1552 search_tbl->ant_type = ANT_BC;
1553
1554 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1555 break;
1556
Guy Cohenfde0db32008-04-21 15:42:01 -07001557 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001558 search_tbl, index);
Guy Cohen3110bef2008-09-09 10:54:54 +08001559 if (!ret)
Zhu Yib481de92007-09-25 17:54:57 -07001560 goto out;
1561 break;
1562 case IWL_SISO_SWITCH_GI:
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001563 if (!tbl->is_ht40 && !(ht_cap->cap &
1564 IEEE80211_HT_CAP_SGI_20))
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001565 break;
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001566 if (tbl->is_ht40 && !(ht_cap->cap &
1567 IEEE80211_HT_CAP_SGI_40))
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001568 break;
1569
Tomas Winklere1623442009-01-27 14:27:56 -08001570 IWL_DEBUG_RATE(priv, "LQ: SISO toggle SGI/NGI\n");
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001571
Zhu Yib481de92007-09-25 17:54:57 -07001572 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001573 if (is_green) {
1574 if (!tbl->is_SGI)
1575 break;
1576 else
Winkler, Tomas15b16872008-12-19 10:37:33 +08001577 IWL_ERR(priv,
1578 "SGI was set in GF+SISO\n");
Zhu Yib481de92007-09-25 17:54:57 -07001579 }
Guy Cohen39e88502008-04-23 17:14:57 -07001580 search_tbl->is_SGI = !tbl->is_SGI;
Guy Coheneecd6e52008-04-23 17:14:58 -07001581 rs_set_expected_tpt_table(lq_sta, search_tbl);
Guy Cohen39e88502008-04-23 17:14:57 -07001582 if (tbl->is_SGI) {
1583 s32 tpt = lq_sta->last_tpt / 100;
1584 if (tpt >= search_tbl->expected_tpt[index])
1585 break;
1586 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001587 search_tbl->current_rate =
1588 rate_n_flags_from_tbl(priv, search_tbl,
1589 index, is_green);
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001590 update_search_tbl_counter = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001591 goto out;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001592 case IWL_SISO_SWITCH_MIMO3_ABC:
1593 IWL_DEBUG_RATE(priv, "LQ: SISO switch to MIMO3\n");
1594 memcpy(search_tbl, tbl, sz);
1595 search_tbl->is_SGI = 0;
1596 search_tbl->ant_type = ANT_ABC;
1597
1598 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1599 break;
1600
1601 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1602 search_tbl, index);
1603 if (!ret)
1604 goto out;
1605 break;
Zhu Yib481de92007-09-25 17:54:57 -07001606 }
1607 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001608 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001609 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
Zhu Yib481de92007-09-25 17:54:57 -07001610
1611 if (tbl->action == start_action)
1612 break;
1613 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001614 search_tbl->lq_type = LQ_NONE;
Zhu Yib481de92007-09-25 17:54:57 -07001615 return 0;
1616
1617 out:
Guy Cohen3110bef2008-09-09 10:54:54 +08001618 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001619 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001620 if (tbl->action > IWL_SISO_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001621 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001622 if (update_search_tbl_counter)
1623 search_tbl->action = tbl->action;
1624
Zhu Yib481de92007-09-25 17:54:57 -07001625 return 0;
1626}
1627
Ben Cahill77626352007-11-29 11:09:44 +08001628/*
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001629 * Try to switch to new modulation mode from MIMO2
Ben Cahill77626352007-11-29 11:09:44 +08001630 */
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001631static int rs_move_mimo2_to_other(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001632 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001633 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001634 struct ieee80211_sta *sta, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001635{
Tomas Winklerc33104f2008-01-14 17:46:21 -08001636 s8 is_green = lq_sta->is_green;
Tomas Winklere227cea2008-07-18 13:53:05 +08001637 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1638 struct iwl_scale_tbl_info *search_tbl =
1639 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Guy Cohen3110bef2008-09-09 10:54:54 +08001640 struct iwl_rate_scale_data *window = &(tbl->win[index]);
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001641 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Tomas Winklere227cea2008-07-18 13:53:05 +08001642 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1643 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001644 u8 start_action = tbl->action;
Guy Cohen3110bef2008-09-09 10:54:54 +08001645 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1646 u8 tx_chains_num = priv->hw_params.tx_chains_num;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001647 u8 update_search_tbl_counter = 0;
Guy Cohenaade00c2008-04-23 17:14:59 -07001648 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001649
Johannes Berg3ad3b922009-08-07 15:41:48 -07001650 if ((iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE) &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001651 (tbl->action < IWL_MIMO2_SWITCH_SISO_A ||
1652 tbl->action > IWL_MIMO2_SWITCH_SISO_C)) {
1653 /* switch in SISO */
1654 tbl->action = IWL_MIMO2_SWITCH_SISO_A;
1655 }
Zhu Yib481de92007-09-25 17:54:57 -07001656 for (;;) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001657 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001658 switch (tbl->action) {
Guy Cohen3110bef2008-09-09 10:54:54 +08001659 case IWL_MIMO2_SWITCH_ANTENNA1:
1660 case IWL_MIMO2_SWITCH_ANTENNA2:
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001661 IWL_DEBUG_RATE(priv, "LQ: MIMO2 toggle Antennas\n");
Guy Cohen3110bef2008-09-09 10:54:54 +08001662
1663 if (tx_chains_num <= 2)
1664 break;
1665
1666 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1667 break;
1668
1669 memcpy(search_tbl, tbl, sz);
1670 if (rs_toggle_antenna(valid_tx_ant,
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001671 &search_tbl->current_rate, search_tbl)) {
1672 update_search_tbl_counter = 1;
Guy Cohen3110bef2008-09-09 10:54:54 +08001673 goto out;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001674 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001675 break;
1676 case IWL_MIMO2_SWITCH_SISO_A:
1677 case IWL_MIMO2_SWITCH_SISO_B:
1678 case IWL_MIMO2_SWITCH_SISO_C:
Tomas Winklere1623442009-01-27 14:27:56 -08001679 IWL_DEBUG_RATE(priv, "LQ: MIMO2 switch to SISO\n");
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001680
Ben Cahill77626352007-11-29 11:09:44 +08001681 /* Set up new search table for SISO */
Zhu Yib481de92007-09-25 17:54:57 -07001682 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001683
Guy Cohen3110bef2008-09-09 10:54:54 +08001684 if (tbl->action == IWL_MIMO2_SWITCH_SISO_A)
Guy Cohenfde0db32008-04-21 15:42:01 -07001685 search_tbl->ant_type = ANT_A;
Guy Cohen3110bef2008-09-09 10:54:54 +08001686 else if (tbl->action == IWL_MIMO2_SWITCH_SISO_B)
Guy Cohenfde0db32008-04-21 15:42:01 -07001687 search_tbl->ant_type = ANT_B;
Guy Cohen3110bef2008-09-09 10:54:54 +08001688 else
1689 search_tbl->ant_type = ANT_C;
1690
1691 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1692 break;
Zhu Yib481de92007-09-25 17:54:57 -07001693
Tomas Winklerc33104f2008-01-14 17:46:21 -08001694 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001695 search_tbl, index);
Guy Cohen3110bef2008-09-09 10:54:54 +08001696 if (!ret)
Zhu Yib481de92007-09-25 17:54:57 -07001697 goto out;
Guy Cohen3110bef2008-09-09 10:54:54 +08001698
Zhu Yib481de92007-09-25 17:54:57 -07001699 break;
1700
Guy Cohen3110bef2008-09-09 10:54:54 +08001701 case IWL_MIMO2_SWITCH_GI:
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001702 if (!tbl->is_ht40 && !(ht_cap->cap &
1703 IEEE80211_HT_CAP_SGI_20))
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001704 break;
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001705 if (tbl->is_ht40 && !(ht_cap->cap &
1706 IEEE80211_HT_CAP_SGI_40))
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001707 break;
1708
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001709 IWL_DEBUG_RATE(priv, "LQ: MIMO2 toggle SGI/NGI\n");
1710
1711 /* Set up new search table for MIMO2 */
1712 memcpy(search_tbl, tbl, sz);
1713 search_tbl->is_SGI = !tbl->is_SGI;
1714 rs_set_expected_tpt_table(lq_sta, search_tbl);
1715 /*
1716 * If active table already uses the fastest possible
1717 * modulation (dual stream with short guard interval),
1718 * and it's working well, there's no need to look
1719 * for a better type of modulation!
1720 */
1721 if (tbl->is_SGI) {
1722 s32 tpt = lq_sta->last_tpt / 100;
1723 if (tpt >= search_tbl->expected_tpt[index])
1724 break;
1725 }
1726 search_tbl->current_rate =
1727 rate_n_flags_from_tbl(priv, search_tbl,
1728 index, is_green);
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001729 update_search_tbl_counter = 1;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001730 goto out;
1731
1732 case IWL_MIMO2_SWITCH_MIMO3_ABC:
1733 IWL_DEBUG_RATE(priv, "LQ: MIMO2 switch to MIMO3\n");
1734 memcpy(search_tbl, tbl, sz);
1735 search_tbl->is_SGI = 0;
1736 search_tbl->ant_type = ANT_ABC;
1737
1738 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1739 break;
1740
1741 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1742 search_tbl, index);
1743 if (!ret)
1744 goto out;
1745
1746 break;
1747 }
1748 tbl->action++;
1749 if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
1750 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
1751
1752 if (tbl->action == start_action)
1753 break;
1754 }
1755 search_tbl->lq_type = LQ_NONE;
1756 return 0;
1757 out:
1758 lq_sta->search_better_tbl = 1;
1759 tbl->action++;
1760 if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
1761 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001762 if (update_search_tbl_counter)
1763 search_tbl->action = tbl->action;
1764
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001765 return 0;
1766
1767}
1768
1769/*
1770 * Try to switch to new modulation mode from MIMO3
1771 */
1772static int rs_move_mimo3_to_other(struct iwl_priv *priv,
1773 struct iwl_lq_sta *lq_sta,
1774 struct ieee80211_conf *conf,
1775 struct ieee80211_sta *sta, int index)
1776{
1777 s8 is_green = lq_sta->is_green;
1778 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1779 struct iwl_scale_tbl_info *search_tbl =
1780 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1781 struct iwl_rate_scale_data *window = &(tbl->win[index]);
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001782 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001783 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1784 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1785 u8 start_action = tbl->action;
1786 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1787 u8 tx_chains_num = priv->hw_params.tx_chains_num;
1788 int ret;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001789 u8 update_search_tbl_counter = 0;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001790
Johannes Berg3ad3b922009-08-07 15:41:48 -07001791 if ((iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE) &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001792 (tbl->action < IWL_MIMO3_SWITCH_SISO_A ||
1793 tbl->action > IWL_MIMO3_SWITCH_SISO_C)) {
1794 /* switch in SISO */
1795 tbl->action = IWL_MIMO3_SWITCH_SISO_A;
1796 }
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001797 for (;;) {
1798 lq_sta->action_counter++;
1799 switch (tbl->action) {
1800 case IWL_MIMO3_SWITCH_ANTENNA1:
1801 case IWL_MIMO3_SWITCH_ANTENNA2:
1802 IWL_DEBUG_RATE(priv, "LQ: MIMO3 toggle Antennas\n");
1803
1804 if (tx_chains_num <= 3)
1805 break;
1806
1807 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1808 break;
1809
1810 memcpy(search_tbl, tbl, sz);
1811 if (rs_toggle_antenna(valid_tx_ant,
1812 &search_tbl->current_rate, search_tbl))
1813 goto out;
1814 break;
1815 case IWL_MIMO3_SWITCH_SISO_A:
1816 case IWL_MIMO3_SWITCH_SISO_B:
1817 case IWL_MIMO3_SWITCH_SISO_C:
1818 IWL_DEBUG_RATE(priv, "LQ: MIMO3 switch to SISO\n");
1819
1820 /* Set up new search table for SISO */
1821 memcpy(search_tbl, tbl, sz);
1822
1823 if (tbl->action == IWL_MIMO3_SWITCH_SISO_A)
1824 search_tbl->ant_type = ANT_A;
1825 else if (tbl->action == IWL_MIMO3_SWITCH_SISO_B)
1826 search_tbl->ant_type = ANT_B;
1827 else
1828 search_tbl->ant_type = ANT_C;
1829
1830 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1831 break;
1832
1833 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
1834 search_tbl, index);
1835 if (!ret)
1836 goto out;
1837
1838 break;
1839
1840 case IWL_MIMO3_SWITCH_MIMO2_AB:
1841 case IWL_MIMO3_SWITCH_MIMO2_AC:
1842 case IWL_MIMO3_SWITCH_MIMO2_BC:
1843 IWL_DEBUG_RATE(priv, "LQ: MIMO3 switch to MIMO2\n");
1844
1845 memcpy(search_tbl, tbl, sz);
1846 search_tbl->is_SGI = 0;
1847 if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AB)
1848 search_tbl->ant_type = ANT_AB;
1849 else if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AC)
1850 search_tbl->ant_type = ANT_AC;
1851 else
1852 search_tbl->ant_type = ANT_BC;
1853
1854 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1855 break;
1856
1857 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
1858 search_tbl, index);
1859 if (!ret)
1860 goto out;
1861
1862 break;
1863
1864 case IWL_MIMO3_SWITCH_GI:
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001865 if (!tbl->is_ht40 && !(ht_cap->cap &
1866 IEEE80211_HT_CAP_SGI_20))
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001867 break;
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001868 if (tbl->is_ht40 && !(ht_cap->cap &
1869 IEEE80211_HT_CAP_SGI_40))
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001870 break;
1871
1872 IWL_DEBUG_RATE(priv, "LQ: MIMO3 toggle SGI/NGI\n");
Ben Cahill77626352007-11-29 11:09:44 +08001873
1874 /* Set up new search table for MIMO */
Zhu Yib481de92007-09-25 17:54:57 -07001875 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001876 search_tbl->is_SGI = !tbl->is_SGI;
Guy Coheneecd6e52008-04-23 17:14:58 -07001877 rs_set_expected_tpt_table(lq_sta, search_tbl);
Ben Cahill77626352007-11-29 11:09:44 +08001878 /*
1879 * If active table already uses the fastest possible
1880 * modulation (dual stream with short guard interval),
1881 * and it's working well, there's no need to look
1882 * for a better type of modulation!
1883 */
Guy Cohen39e88502008-04-23 17:14:57 -07001884 if (tbl->is_SGI) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001885 s32 tpt = lq_sta->last_tpt / 100;
Guy Cohen39e88502008-04-23 17:14:57 -07001886 if (tpt >= search_tbl->expected_tpt[index])
1887 break;
Zhu Yib481de92007-09-25 17:54:57 -07001888 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001889 search_tbl->current_rate =
1890 rate_n_flags_from_tbl(priv, search_tbl,
1891 index, is_green);
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001892 update_search_tbl_counter = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001893 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07001894 }
1895 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001896 if (tbl->action > IWL_MIMO3_SWITCH_GI)
1897 tbl->action = IWL_MIMO3_SWITCH_ANTENNA1;
Zhu Yib481de92007-09-25 17:54:57 -07001898
1899 if (tbl->action == start_action)
1900 break;
1901 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001902 search_tbl->lq_type = LQ_NONE;
Zhu Yib481de92007-09-25 17:54:57 -07001903 return 0;
1904 out:
Guy Cohen3110bef2008-09-09 10:54:54 +08001905 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001906 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001907 if (tbl->action > IWL_MIMO3_SWITCH_GI)
1908 tbl->action = IWL_MIMO3_SWITCH_ANTENNA1;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001909 if (update_search_tbl_counter)
1910 search_tbl->action = tbl->action;
1911
Zhu Yib481de92007-09-25 17:54:57 -07001912 return 0;
1913
1914}
1915
Ben Cahill77626352007-11-29 11:09:44 +08001916/*
1917 * Check whether we should continue using same modulation mode, or
1918 * begin search for a new mode, based on:
1919 * 1) # tx successes or failures while using this mode
1920 * 2) # times calling this function
1921 * 3) elapsed time in this mode (not used, for now)
1922 */
Tomas Winklere227cea2008-07-18 13:53:05 +08001923static void rs_stay_in_table(struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07001924{
Tomas Winklere227cea2008-07-18 13:53:05 +08001925 struct iwl_scale_tbl_info *tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001926 int i;
1927 int active_tbl;
1928 int flush_interval_passed = 0;
Ester Kummerbf403db2008-05-05 10:22:40 +08001929 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07001930
Ester Kummerbf403db2008-05-05 10:22:40 +08001931 priv = lq_sta->drv;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001932 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001933
Tomas Winklerc33104f2008-01-14 17:46:21 -08001934 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07001935
Ben Cahill77626352007-11-29 11:09:44 +08001936 /* If we've been disallowing search, see if we should now allow it */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001937 if (lq_sta->stay_in_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07001938
Ben Cahill77626352007-11-29 11:09:44 +08001939 /* Elapsed time using current modulation mode */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001940 if (lq_sta->flush_timer)
Zhu Yib481de92007-09-25 17:54:57 -07001941 flush_interval_passed =
Mohamed Abbas447fee72009-04-20 14:37:02 -07001942 time_after(jiffies,
1943 (unsigned long)(lq_sta->flush_timer +
Zhu Yib481de92007-09-25 17:54:57 -07001944 IWL_RATE_SCALE_FLUSH_INTVL));
1945
Ben Cahill77626352007-11-29 11:09:44 +08001946 /*
1947 * Check if we should allow search for new modulation mode.
1948 * If many frames have failed or succeeded, or we've used
1949 * this same modulation for a long time, allow search, and
1950 * reset history stats that keep track of whether we should
1951 * allow a new search. Also (below) reset all bitmaps and
1952 * stats in active history.
1953 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001954 if ((lq_sta->total_failed > lq_sta->max_failure_limit) ||
1955 (lq_sta->total_success > lq_sta->max_success_limit) ||
1956 ((!lq_sta->search_better_tbl) && (lq_sta->flush_timer)
Zhu Yib481de92007-09-25 17:54:57 -07001957 && (flush_interval_passed))) {
Tomas Winklere1623442009-01-27 14:27:56 -08001958 IWL_DEBUG_RATE(priv, "LQ: stay is expired %d %d %d\n:",
Tomas Winklerc33104f2008-01-14 17:46:21 -08001959 lq_sta->total_failed,
1960 lq_sta->total_success,
Zhu Yib481de92007-09-25 17:54:57 -07001961 flush_interval_passed);
Ben Cahill77626352007-11-29 11:09:44 +08001962
1963 /* Allow search for new mode */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001964 lq_sta->stay_in_tbl = 0; /* only place reset */
1965 lq_sta->total_failed = 0;
1966 lq_sta->total_success = 0;
1967 lq_sta->flush_timer = 0;
Ben Cahill77626352007-11-29 11:09:44 +08001968
1969 /*
1970 * Else if we've used this modulation mode enough repetitions
1971 * (regardless of elapsed time or success/failure), reset
1972 * history bitmaps and rate-specific stats for all rates in
1973 * active table.
1974 */
Mohamed Abbas403ab562007-11-06 22:06:25 -08001975 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001976 lq_sta->table_count++;
1977 if (lq_sta->table_count >=
1978 lq_sta->table_count_limit) {
1979 lq_sta->table_count = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001980
Tomas Winklere1623442009-01-27 14:27:56 -08001981 IWL_DEBUG_RATE(priv, "LQ: stay in table clear win\n");
Zhu Yib481de92007-09-25 17:54:57 -07001982 for (i = 0; i < IWL_RATE_COUNT; i++)
1983 rs_rate_scale_clear_window(
1984 &(tbl->win[i]));
1985 }
1986 }
1987
Ben Cahill77626352007-11-29 11:09:44 +08001988 /* If transitioning to allow "search", reset all history
1989 * bitmaps and stats in active table (this will become the new
1990 * "search" table). */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001991 if (!lq_sta->stay_in_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07001992 for (i = 0; i < IWL_RATE_COUNT; i++)
1993 rs_rate_scale_clear_window(&(tbl->win[i]));
1994 }
1995 }
1996}
1997
Ben Cahill77626352007-11-29 11:09:44 +08001998/*
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07001999 * setup rate table in uCode
2000 * return rate_n_flags as used in the table
2001 */
2002static u32 rs_update_rate_tbl(struct iwl_priv *priv,
2003 struct iwl_lq_sta *lq_sta,
2004 struct iwl_scale_tbl_info *tbl,
2005 int index, u8 is_green)
2006{
2007 u32 rate;
2008
2009 /* Update uCode's rate table. */
2010 rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
2011 rs_fill_link_cmd(priv, lq_sta, rate);
2012 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
2013
2014 return rate;
2015}
2016
2017/*
Ben Cahill77626352007-11-29 11:09:44 +08002018 * Do rate scaling and search for new modulation mode.
2019 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002020static void rs_rate_scale_perform(struct iwl_priv *priv,
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002021 struct sk_buff *skb,
Johannes Berg4b7679a2008-09-18 18:14:18 +02002022 struct ieee80211_sta *sta,
2023 struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002024{
Johannes Berg4b7679a2008-09-18 18:14:18 +02002025 struct ieee80211_hw *hw = priv->hw;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002026 struct ieee80211_conf *conf = &hw->conf;
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002027 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2028 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002029 int low = IWL_RATE_INVALID;
2030 int high = IWL_RATE_INVALID;
2031 int index;
2032 int i;
Tomas Winklere227cea2008-07-18 13:53:05 +08002033 struct iwl_rate_scale_data *window = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002034 int current_tpt = IWL_INVALID_VALUE;
2035 int low_tpt = IWL_INVALID_VALUE;
2036 int high_tpt = IWL_INVALID_VALUE;
2037 u32 fail_count;
2038 s8 scale_action = 0;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002039 u16 rate_mask;
Zhu Yib481de92007-09-25 17:54:57 -07002040 u8 update_lq = 0;
Tomas Winklere227cea2008-07-18 13:53:05 +08002041 struct iwl_scale_tbl_info *tbl, *tbl1;
Zhu Yib481de92007-09-25 17:54:57 -07002042 u16 rate_scale_index_msk = 0;
Guy Cohen39e88502008-04-23 17:14:57 -07002043 u32 rate;
Zhu Yib481de92007-09-25 17:54:57 -07002044 u8 is_green = 0;
2045 u8 active_tbl = 0;
2046 u8 done_search = 0;
2047 u16 high_low;
Guy Cohena5e8b502008-05-29 16:35:11 +08002048 s32 sr;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002049 u8 tid = MAX_TID_COUNT;
Wey-Yi Guy86b47662009-03-10 14:35:09 -07002050 struct iwl_tid_data *tid_data;
Zhu Yib481de92007-09-25 17:54:57 -07002051
Tomas Winklere1623442009-01-27 14:27:56 -08002052 IWL_DEBUG_RATE(priv, "rate scale calculate new rate for skb\n");
Zhu Yib481de92007-09-25 17:54:57 -07002053
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002054 /* Send management frames and NO_ACK data using lowest rate. */
Tomas Winkler417f1142008-11-12 13:14:06 -08002055 /* TODO: this could probably be improved.. */
2056 if (!ieee80211_is_data(hdr->frame_control) ||
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002057 info->flags & IEEE80211_TX_CTL_NO_ACK)
Zhu Yib481de92007-09-25 17:54:57 -07002058 return;
Zhu Yib481de92007-09-25 17:54:57 -07002059
Johannes Berg4b7679a2008-09-18 18:14:18 +02002060 if (!sta || !lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002061 return;
2062
Johannes Berg4b7679a2008-09-18 18:14:18 +02002063 lq_sta->supp_rates = sta->supp_rates[lq_sta->band];
Zhu Yib481de92007-09-25 17:54:57 -07002064
Ron Rindjunskyfaa29712008-06-12 09:46:58 +08002065 tid = rs_tl_add_packet(lq_sta, hdr);
2066
Ben Cahill77626352007-11-29 11:09:44 +08002067 /*
2068 * Select rate-scale / modulation-mode table to work with in
2069 * the rest of this function: "search" if searching for better
2070 * modulation mode, or "active" if doing rate scaling within a mode.
2071 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002072 if (!lq_sta->search_better_tbl)
2073 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002074 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002075 active_tbl = 1 - lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002076
Tomas Winklerc33104f2008-01-14 17:46:21 -08002077 tbl = &(lq_sta->lq_info[active_tbl]);
Wey-Yi Guy2681b202009-05-21 13:44:22 -07002078 if (is_legacy(tbl->lq_type))
2079 lq_sta->is_green = 0;
2080 else
Daniel C Halperinb2617932009-08-13 13:30:59 -07002081 lq_sta->is_green = rs_use_green(sta, &priv->current_ht_config);
Tomas Winklerc33104f2008-01-14 17:46:21 -08002082 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07002083
Ben Cahill77626352007-11-29 11:09:44 +08002084 /* current tx rate */
Johannes Bergb7e35002008-09-11 02:22:58 +02002085 index = lq_sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002086
Tomas Winklere1623442009-01-27 14:27:56 -08002087 IWL_DEBUG_RATE(priv, "Rate scale index %d for type %d\n", index,
Zhu Yib481de92007-09-25 17:54:57 -07002088 tbl->lq_type);
2089
Ben Cahill77626352007-11-29 11:09:44 +08002090 /* rates available for this association, and for modulation mode */
Guy Coheneecd6e52008-04-23 17:14:58 -07002091 rate_mask = rs_get_supported_rates(lq_sta, hdr, tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -07002092
Tomas Winklere1623442009-01-27 14:27:56 -08002093 IWL_DEBUG_RATE(priv, "mask 0x%04X \n", rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07002094
2095 /* mask with station rate restriction */
2096 if (is_legacy(tbl->lq_type)) {
Johannes Berg8318d782008-01-24 19:38:38 +01002097 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Ben Cahill77626352007-11-29 11:09:44 +08002098 /* supp_rates has no CCK bits in A mode */
Zhu Yib481de92007-09-25 17:54:57 -07002099 rate_scale_index_msk = (u16) (rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -08002100 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
Zhu Yib481de92007-09-25 17:54:57 -07002101 else
2102 rate_scale_index_msk = (u16) (rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -08002103 lq_sta->supp_rates);
Zhu Yib481de92007-09-25 17:54:57 -07002104
2105 } else
2106 rate_scale_index_msk = rate_mask;
2107
2108 if (!rate_scale_index_msk)
2109 rate_scale_index_msk = rate_mask;
2110
Guy Cohen07bc28e2008-04-23 17:15:03 -07002111 if (!((1 << index) & rate_scale_index_msk)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002112 IWL_ERR(priv, "Current Rate is not valid\n");
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07002113 if (lq_sta->search_better_tbl) {
2114 /* revert to active table if search table is not valid*/
2115 tbl->lq_type = LQ_NONE;
2116 lq_sta->search_better_tbl = 0;
2117 tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
2118 /* get "active" rate info */
2119 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
2120 rate = rs_update_rate_tbl(priv, lq_sta,
2121 tbl, index, is_green);
2122 }
Guy Cohen07bc28e2008-04-23 17:15:03 -07002123 return;
Zhu Yib481de92007-09-25 17:54:57 -07002124 }
2125
Ben Cahill77626352007-11-29 11:09:44 +08002126 /* Get expected throughput table and history window for current rate */
Guy Cohen07bc28e2008-04-23 17:15:03 -07002127 if (!tbl->expected_tpt) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002128 IWL_ERR(priv, "tbl->expected_tpt is NULL\n");
Guy Cohen07bc28e2008-04-23 17:15:03 -07002129 return;
2130 }
Zhu Yib481de92007-09-25 17:54:57 -07002131
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002132 /* force user max rate if set by user */
2133 if ((lq_sta->max_rate_idx != -1) &&
2134 (lq_sta->max_rate_idx < index)) {
2135 index = lq_sta->max_rate_idx;
2136 update_lq = 1;
2137 window = &(tbl->win[index]);
2138 goto lq_update;
2139 }
2140
Zhu Yib481de92007-09-25 17:54:57 -07002141 window = &(tbl->win[index]);
2142
Ben Cahill77626352007-11-29 11:09:44 +08002143 /*
2144 * If there is not enough history to calculate actual average
2145 * throughput, keep analyzing results of more tx frames, without
2146 * changing rate or mode (bypass most of the rest of this function).
2147 * Set up new rate table in uCode only if old rate is not supported
2148 * in current association (use new rate found above).
2149 */
Zhu Yib481de92007-09-25 17:54:57 -07002150 fail_count = window->counter - window->success_counter;
Guy Cohen07bc28e2008-04-23 17:15:03 -07002151 if ((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
2152 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002153 IWL_DEBUG_RATE(priv, "LQ: still below TH. succ=%d total=%d "
Zhu Yib481de92007-09-25 17:54:57 -07002154 "for index %d\n",
2155 window->success_counter, window->counter, index);
Ben Cahill77626352007-11-29 11:09:44 +08002156
2157 /* Can't calculate this yet; not enough history */
Zhu Yib481de92007-09-25 17:54:57 -07002158 window->average_tpt = IWL_INVALID_VALUE;
Ben Cahill77626352007-11-29 11:09:44 +08002159
2160 /* Should we stay with this modulation mode,
2161 * or search for a new one? */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002162 rs_stay_in_table(lq_sta);
Ben Cahill77626352007-11-29 11:09:44 +08002163
Zhu Yib481de92007-09-25 17:54:57 -07002164 goto out;
Guy Cohen3110bef2008-09-09 10:54:54 +08002165 }
Zhu Yib481de92007-09-25 17:54:57 -07002166
Ben Cahill77626352007-11-29 11:09:44 +08002167 /* Else we have enough samples; calculate estimate of
2168 * actual average throughput */
Guy Cohen3110bef2008-09-09 10:54:54 +08002169
2170 BUG_ON(window->average_tpt != ((window->success_ratio *
2171 tbl->expected_tpt[index] + 64) / 128));
Zhu Yib481de92007-09-25 17:54:57 -07002172
Ben Cahill77626352007-11-29 11:09:44 +08002173 /* If we are searching for better modulation mode, check success. */
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002174 if (lq_sta->search_better_tbl &&
Johannes Berg3ad3b922009-08-07 15:41:48 -07002175 (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_MULTI)) {
Ben Cahill77626352007-11-29 11:09:44 +08002176 /* If good success, continue using the "search" mode;
2177 * no need to send new link quality command, since we're
2178 * continuing to use the setup that we've been trying. */
Guy Cohen07bc28e2008-04-23 17:15:03 -07002179 if (window->average_tpt > lq_sta->last_tpt) {
2180
Tomas Winklere1623442009-01-27 14:27:56 -08002181 IWL_DEBUG_RATE(priv, "LQ: SWITCHING TO NEW TABLE "
Guy Cohen07bc28e2008-04-23 17:15:03 -07002182 "suc=%d cur-tpt=%d old-tpt=%d\n",
2183 window->success_ratio,
2184 window->average_tpt,
2185 lq_sta->last_tpt);
2186
2187 if (!is_legacy(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08002188 lq_sta->enable_counter = 1;
Guy Cohen07bc28e2008-04-23 17:15:03 -07002189
Ben Cahill77626352007-11-29 11:09:44 +08002190 /* Swap tables; "search" becomes "active" */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002191 lq_sta->active_tbl = active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002192 current_tpt = window->average_tpt;
Ben Cahill77626352007-11-29 11:09:44 +08002193
2194 /* Else poor success; go back to mode in "active" table */
Zhu Yib481de92007-09-25 17:54:57 -07002195 } else {
Guy Cohen07bc28e2008-04-23 17:15:03 -07002196
Tomas Winklere1623442009-01-27 14:27:56 -08002197 IWL_DEBUG_RATE(priv, "LQ: GOING BACK TO THE OLD TABLE "
Guy Cohen07bc28e2008-04-23 17:15:03 -07002198 "suc=%d cur-tpt=%d old-tpt=%d\n",
2199 window->success_ratio,
2200 window->average_tpt,
2201 lq_sta->last_tpt);
2202
Ben Cahill77626352007-11-29 11:09:44 +08002203 /* Nullify "search" table */
Zhu Yib481de92007-09-25 17:54:57 -07002204 tbl->lq_type = LQ_NONE;
Ben Cahill77626352007-11-29 11:09:44 +08002205
2206 /* Revert to "active" table */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002207 active_tbl = lq_sta->active_tbl;
2208 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07002209
Ben Cahill77626352007-11-29 11:09:44 +08002210 /* Revert to "active" rate and throughput info */
Tomas Winklere7d326ac2008-06-12 09:47:11 +08002211 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
Tomas Winklerc33104f2008-01-14 17:46:21 -08002212 current_tpt = lq_sta->last_tpt;
Ben Cahill77626352007-11-29 11:09:44 +08002213
2214 /* Need to set up a new rate table in uCode */
2215 update_lq = 1;
Zhu Yib481de92007-09-25 17:54:57 -07002216 }
Ben Cahill77626352007-11-29 11:09:44 +08002217
2218 /* Either way, we've made a decision; modulation mode
2219 * search is done, allow rate adjustment next time. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002220 lq_sta->search_better_tbl = 0;
Ben Cahill77626352007-11-29 11:09:44 +08002221 done_search = 1; /* Don't switch modes below! */
Zhu Yib481de92007-09-25 17:54:57 -07002222 goto lq_update;
2223 }
2224
Ben Cahill77626352007-11-29 11:09:44 +08002225 /* (Else) not in search of better modulation mode, try for better
2226 * starting rate, while staying in this mode. */
Ester Kummerbf403db2008-05-05 10:22:40 +08002227 high_low = rs_get_adjacent_rate(priv, index, rate_scale_index_msk,
Zhu Yib481de92007-09-25 17:54:57 -07002228 tbl->lq_type);
2229 low = high_low & 0xff;
2230 high = (high_low >> 8) & 0xff;
2231
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002232 /* If user set max rate, dont allow higher than user constrain */
2233 if ((lq_sta->max_rate_idx != -1) &&
2234 (lq_sta->max_rate_idx < high))
2235 high = IWL_RATE_INVALID;
2236
Guy Cohena5e8b502008-05-29 16:35:11 +08002237 sr = window->success_ratio;
2238
Ben Cahill77626352007-11-29 11:09:44 +08002239 /* Collect measured throughputs for current and adjacent rates */
Zhu Yib481de92007-09-25 17:54:57 -07002240 current_tpt = window->average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07002241 if (low != IWL_RATE_INVALID)
2242 low_tpt = tbl->win[low].average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07002243 if (high != IWL_RATE_INVALID)
2244 high_tpt = tbl->win[high].average_tpt;
2245
Guy Cohena5e8b502008-05-29 16:35:11 +08002246 scale_action = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002247
Ben Cahill77626352007-11-29 11:09:44 +08002248 /* Too many failures, decrease rate */
Guy Cohena5e8b502008-05-29 16:35:11 +08002249 if ((sr <= IWL_RATE_DECREASE_TH) || (current_tpt == 0)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002250 IWL_DEBUG_RATE(priv, "decrease rate because of low success_ratio\n");
Zhu Yib481de92007-09-25 17:54:57 -07002251 scale_action = -1;
Ben Cahill77626352007-11-29 11:09:44 +08002252
2253 /* No throughput measured yet for adjacent rates; try increase. */
Zhu Yib481de92007-09-25 17:54:57 -07002254 } else if ((low_tpt == IWL_INVALID_VALUE) &&
Guy Cohena5e8b502008-05-29 16:35:11 +08002255 (high_tpt == IWL_INVALID_VALUE)) {
2256
2257 if (high != IWL_RATE_INVALID && sr >= IWL_RATE_INCREASE_TH)
2258 scale_action = 1;
2259 else if (low != IWL_RATE_INVALID)
Wey-Yi Guy8fe72312009-03-10 14:35:10 -07002260 scale_action = 0;
Guy Cohena5e8b502008-05-29 16:35:11 +08002261 }
Ben Cahill77626352007-11-29 11:09:44 +08002262
2263 /* Both adjacent throughputs are measured, but neither one has better
2264 * throughput; we're using the best rate, don't change it! */
Zhu Yib481de92007-09-25 17:54:57 -07002265 else if ((low_tpt != IWL_INVALID_VALUE) &&
2266 (high_tpt != IWL_INVALID_VALUE) &&
2267 (low_tpt < current_tpt) &&
2268 (high_tpt < current_tpt))
2269 scale_action = 0;
Ben Cahill77626352007-11-29 11:09:44 +08002270
2271 /* At least one adjacent rate's throughput is measured,
2272 * and may have better performance. */
Zhu Yib481de92007-09-25 17:54:57 -07002273 else {
Ben Cahill77626352007-11-29 11:09:44 +08002274 /* Higher adjacent rate's throughput is measured */
Zhu Yib481de92007-09-25 17:54:57 -07002275 if (high_tpt != IWL_INVALID_VALUE) {
Ben Cahill77626352007-11-29 11:09:44 +08002276 /* Higher rate has better throughput */
Guy Cohena5e8b502008-05-29 16:35:11 +08002277 if (high_tpt > current_tpt &&
2278 sr >= IWL_RATE_INCREASE_TH) {
Zhu Yib481de92007-09-25 17:54:57 -07002279 scale_action = 1;
Guy Cohena5e8b502008-05-29 16:35:11 +08002280 } else {
Wey-Yi Guy8fe72312009-03-10 14:35:10 -07002281 scale_action = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002282 }
Ben Cahill77626352007-11-29 11:09:44 +08002283
2284 /* Lower adjacent rate's throughput is measured */
Zhu Yib481de92007-09-25 17:54:57 -07002285 } else if (low_tpt != IWL_INVALID_VALUE) {
Ben Cahill77626352007-11-29 11:09:44 +08002286 /* Lower rate has better throughput */
Zhu Yib481de92007-09-25 17:54:57 -07002287 if (low_tpt > current_tpt) {
Tomas Winklere1623442009-01-27 14:27:56 -08002288 IWL_DEBUG_RATE(priv,
2289 "decrease rate because of low tpt\n");
Zhu Yib481de92007-09-25 17:54:57 -07002290 scale_action = -1;
Guy Cohena5e8b502008-05-29 16:35:11 +08002291 } else if (sr >= IWL_RATE_INCREASE_TH) {
Zhu Yib481de92007-09-25 17:54:57 -07002292 scale_action = 1;
Guy Cohena5e8b502008-05-29 16:35:11 +08002293 }
Zhu Yib481de92007-09-25 17:54:57 -07002294 }
2295 }
2296
Ben Cahill77626352007-11-29 11:09:44 +08002297 /* Sanity check; asked for decrease, but success rate or throughput
2298 * has been good at old rate. Don't change it. */
Guy Cohena5e8b502008-05-29 16:35:11 +08002299 if ((scale_action == -1) && (low != IWL_RATE_INVALID) &&
2300 ((sr > IWL_RATE_HIGH_TH) ||
Zhu Yib481de92007-09-25 17:54:57 -07002301 (current_tpt > (100 * tbl->expected_tpt[low]))))
Zhu Yib481de92007-09-25 17:54:57 -07002302 scale_action = 0;
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002303 if (!iwl_ht_enabled(priv) && !is_legacy(tbl->lq_type))
2304 scale_action = -1;
Johannes Berg3ad3b922009-08-07 15:41:48 -07002305 if (iwl_tx_ant_restriction(priv) != IWL_ANT_OK_MULTI &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002306 (is_mimo2(tbl->lq_type) || is_mimo3(tbl->lq_type)))
2307 scale_action = -1;
Zhu Yib481de92007-09-25 17:54:57 -07002308 switch (scale_action) {
2309 case -1:
Ben Cahill77626352007-11-29 11:09:44 +08002310 /* Decrease starting rate, update uCode's rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002311 if (low != IWL_RATE_INVALID) {
2312 update_lq = 1;
2313 index = low;
2314 }
Mohamed Abbas447fee72009-04-20 14:37:02 -07002315
Zhu Yib481de92007-09-25 17:54:57 -07002316 break;
2317 case 1:
Ben Cahill77626352007-11-29 11:09:44 +08002318 /* Increase starting rate, update uCode's rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002319 if (high != IWL_RATE_INVALID) {
2320 update_lq = 1;
2321 index = high;
2322 }
2323
2324 break;
2325 case 0:
Ben Cahill77626352007-11-29 11:09:44 +08002326 /* No change */
Zhu Yib481de92007-09-25 17:54:57 -07002327 default:
2328 break;
2329 }
2330
Tomas Winklere1623442009-01-27 14:27:56 -08002331 IWL_DEBUG_RATE(priv, "choose rate scale index %d action %d low %d "
Zhu Yib481de92007-09-25 17:54:57 -07002332 "high %d type %d\n",
2333 index, scale_action, low, high, tbl->lq_type);
2334
Guy Cohena5e8b502008-05-29 16:35:11 +08002335lq_update:
Ben Cahill77626352007-11-29 11:09:44 +08002336 /* Replace uCode's rate table for the destination station. */
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07002337 if (update_lq)
2338 rate = rs_update_rate_tbl(priv, lq_sta,
2339 tbl, index, is_green);
Ben Cahill77626352007-11-29 11:09:44 +08002340
Johannes Berg3ad3b922009-08-07 15:41:48 -07002341 if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_MULTI) {
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002342 /* Should we stay with this modulation mode,
2343 * or search for a new one? */
2344 rs_stay_in_table(lq_sta);
2345 }
Ben Cahill77626352007-11-29 11:09:44 +08002346 /*
2347 * Search for new modulation mode if we're:
2348 * 1) Not changing rates right now
2349 * 2) Not just finishing up a search
2350 * 3) Allowing a new search
2351 */
Guy Cohen47cfd462008-05-27 11:29:34 +08002352 if (!update_lq && !done_search && !lq_sta->stay_in_tbl && window->counter) {
Ben Cahill77626352007-11-29 11:09:44 +08002353 /* Save current throughput to compare with "search" throughput*/
Tomas Winklerc33104f2008-01-14 17:46:21 -08002354 lq_sta->last_tpt = current_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07002355
Ben Cahill77626352007-11-29 11:09:44 +08002356 /* Select a new "search" modulation mode to try.
2357 * If one is found, set up the new "search" table. */
Zhu Yib481de92007-09-25 17:54:57 -07002358 if (is_legacy(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08002359 rs_move_legacy_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07002360 else if (is_siso(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08002361 rs_move_siso_to_other(priv, lq_sta, conf, sta, index);
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07002362 else if (is_mimo2(tbl->lq_type))
2363 rs_move_mimo2_to_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07002364 else
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07002365 rs_move_mimo3_to_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07002366
Ben Cahill77626352007-11-29 11:09:44 +08002367 /* If new "search" mode was selected, set up in uCode table */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002368 if (lq_sta->search_better_tbl) {
Ben Cahill77626352007-11-29 11:09:44 +08002369 /* Access the "search" table, clear its history. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002370 tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Zhu Yib481de92007-09-25 17:54:57 -07002371 for (i = 0; i < IWL_RATE_COUNT; i++)
2372 rs_rate_scale_clear_window(&(tbl->win[i]));
2373
Ben Cahill77626352007-11-29 11:09:44 +08002374 /* Use new "search" start rate */
Tomas Winklere7d326ac2008-06-12 09:47:11 +08002375 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002376
Tomas Winklere1623442009-01-27 14:27:56 -08002377 IWL_DEBUG_RATE(priv, "Switch current mcs: %X index: %d\n",
Guy Cohen39e88502008-04-23 17:14:57 -07002378 tbl->current_rate, index);
Guy Cohen07bc28e2008-04-23 17:15:03 -07002379 rs_fill_link_cmd(priv, lq_sta, tbl->current_rate);
Tomas Winkler66c73db2008-04-15 16:01:40 -07002380 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
Mohamed Abbas447fee72009-04-20 14:37:02 -07002381 } else
2382 done_search = 1;
2383 }
Zhu Yib481de92007-09-25 17:54:57 -07002384
Mohamed Abbas447fee72009-04-20 14:37:02 -07002385 if (done_search && !lq_sta->stay_in_tbl) {
Ben Cahill77626352007-11-29 11:09:44 +08002386 /* If the "active" (non-search) mode was legacy,
2387 * and we've tried switching antennas,
2388 * but we haven't been able to try HT modes (not available),
2389 * stay with best antenna legacy modulation for a while
2390 * before next round of mode comparisons. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002391 tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
Luis R. Rodriguezde27e642008-12-23 15:58:44 -08002392 if (is_legacy(tbl1->lq_type) && !conf_is_ht(conf) &&
Mohamed Abbasd6e93392009-05-08 13:44:39 -07002393 lq_sta->action_counter > tbl1->max_search) {
Tomas Winklere1623442009-01-27 14:27:56 -08002394 IWL_DEBUG_RATE(priv, "LQ: STAY in legacy table\n");
Ester Kummerbf403db2008-05-05 10:22:40 +08002395 rs_set_stay_in_table(priv, 1, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002396 }
2397
Ben Cahill77626352007-11-29 11:09:44 +08002398 /* If we're in an HT mode, and all 3 mode switch actions
2399 * have been tried and compared, stay in this best modulation
2400 * mode for a while before next round of mode comparisons. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002401 if (lq_sta->enable_counter &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002402 (lq_sta->action_counter >= tbl1->max_search) &&
2403 iwl_ht_enabled(priv)) {
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002404 if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
2405 (lq_sta->tx_agg_tid_en & (1 << tid)) &&
2406 (tid != MAX_TID_COUNT)) {
Wey-Yi Guy86b47662009-03-10 14:35:09 -07002407 tid_data =
2408 &priv->stations[lq_sta->lq.sta_id].tid[tid];
2409 if (tid_data->agg.state == IWL_AGG_OFF) {
2410 IWL_DEBUG_RATE(priv,
2411 "try to aggregate tid %d\n",
2412 tid);
2413 rs_tl_turn_on_agg(priv, tid,
2414 lq_sta, sta);
2415 }
Zhu Yib481de92007-09-25 17:54:57 -07002416 }
Ester Kummerbf403db2008-05-05 10:22:40 +08002417 rs_set_stay_in_table(priv, 0, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002418 }
Zhu Yib481de92007-09-25 17:54:57 -07002419 }
2420
2421out:
Tomas Winkler978785a2008-12-19 10:37:31 +08002422 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
Zhu Yib481de92007-09-25 17:54:57 -07002423 i = index;
Johannes Bergb7e35002008-09-11 02:22:58 +02002424 lq_sta->last_txrate_idx = i;
Zhu Yib481de92007-09-25 17:54:57 -07002425
Zhu Yib481de92007-09-25 17:54:57 -07002426 return;
2427}
2428
2429
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002430static void rs_initialize_lq(struct iwl_priv *priv,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002431 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02002432 struct ieee80211_sta *sta,
2433 struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002434{
Tomas Winklere227cea2008-07-18 13:53:05 +08002435 struct iwl_scale_tbl_info *tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002436 int rate_idx;
Guy Cohenaade00c2008-04-23 17:14:59 -07002437 int i;
Guy Cohen39e88502008-04-23 17:14:57 -07002438 u32 rate;
Daniel C Halperinb2617932009-08-13 13:30:59 -07002439 u8 use_green = rs_use_green(sta, &priv->current_ht_config);
Guy Cohenaade00c2008-04-23 17:14:59 -07002440 u8 active_tbl = 0;
2441 u8 valid_tx_ant;
Zhu Yib481de92007-09-25 17:54:57 -07002442
Johannes Berg4b7679a2008-09-18 18:14:18 +02002443 if (!sta || !lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002444 goto out;
2445
Johannes Bergb7e35002008-09-11 02:22:58 +02002446 i = lq_sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002447
Tomas Winklerc33104f2008-01-14 17:46:21 -08002448 if ((lq_sta->lq.sta_id == 0xff) &&
Johannes Berg05c914f2008-09-11 00:01:58 +02002449 (priv->iw_mode == NL80211_IFTYPE_ADHOC))
Zhu Yib481de92007-09-25 17:54:57 -07002450 goto out;
2451
Guy Cohenaade00c2008-04-23 17:14:59 -07002452 valid_tx_ant = priv->hw_params.valid_tx_ant;
2453
Tomas Winklerc33104f2008-01-14 17:46:21 -08002454 if (!lq_sta->search_better_tbl)
2455 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002456 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002457 active_tbl = 1 - lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002458
Tomas Winklerc33104f2008-01-14 17:46:21 -08002459 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07002460
2461 if ((i < 0) || (i >= IWL_RATE_COUNT))
2462 i = 0;
2463
Tomas Winkler1826dcc2008-05-15 13:54:02 +08002464 rate = iwl_rates[i].plcp;
Winkler, Tomaseb48dca2008-10-29 14:05:48 -07002465 tbl->ant_type = first_antenna(valid_tx_ant);
2466 rate |= tbl->ant_type << RATE_MCS_ANT_POS;
Zhu Yib481de92007-09-25 17:54:57 -07002467
2468 if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
Guy Cohen39e88502008-04-23 17:14:57 -07002469 rate |= RATE_MCS_CCK_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002470
Guy Cohen39e88502008-04-23 17:14:57 -07002471 rs_get_tbl_info_from_mcs(rate, priv->band, tbl, &rate_idx);
Guy Cohenaade00c2008-04-23 17:14:59 -07002472 if (!rs_is_valid_ant(valid_tx_ant, tbl->ant_type))
2473 rs_toggle_antenna(valid_tx_ant, &rate, tbl);
Zhu Yib481de92007-09-25 17:54:57 -07002474
Tomas Winkler978785a2008-12-19 10:37:31 +08002475 rate = rate_n_flags_from_tbl(priv, tbl, rate_idx, use_green);
Guy Cohen39e88502008-04-23 17:14:57 -07002476 tbl->current_rate = rate;
Guy Coheneecd6e52008-04-23 17:14:58 -07002477 rs_set_expected_tpt_table(lq_sta, tbl);
Guy Cohen07bc28e2008-04-23 17:15:03 -07002478 rs_fill_link_cmd(NULL, lq_sta, rate);
Tomas Winkler66c73db2008-04-15 16:01:40 -07002479 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07002480 out:
2481 return;
2482}
2483
Johannes Berge6a98542008-10-21 12:40:02 +02002484static void rs_get_rate(void *priv_r, struct ieee80211_sta *sta, void *priv_sta,
2485 struct ieee80211_tx_rate_control *txrc)
Zhu Yib481de92007-09-25 17:54:57 -07002486{
2487
Johannes Berge6a98542008-10-21 12:40:02 +02002488 struct sk_buff *skb = txrc->skb;
2489 struct ieee80211_supported_band *sband = txrc->sband;
Johannes Berg4b7679a2008-09-18 18:14:18 +02002490 struct iwl_priv *priv = (struct iwl_priv *)priv_r;
2491 struct ieee80211_conf *conf = &priv->hw->conf;
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002492 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Zhu Yib481de92007-09-25 17:54:57 -07002493 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berge6a98542008-10-21 12:40:02 +02002494 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Tomas Winkler417f1142008-11-12 13:14:06 -08002495 struct iwl_lq_sta *lq_sta = priv_sta;
2496 int rate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002497
Tomas Winklere1623442009-01-27 14:27:56 -08002498 IWL_DEBUG_RATE_LIMIT(priv, "rate scale calculate new rate for skb\n");
Zhu Yib481de92007-09-25 17:54:57 -07002499
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002500 /* Get max rate if user set max rate */
2501 if (lq_sta) {
2502 lq_sta->max_rate_idx = txrc->max_rate_idx;
2503 if ((sband->band == IEEE80211_BAND_5GHZ) &&
2504 (lq_sta->max_rate_idx != -1))
2505 lq_sta->max_rate_idx += IWL_FIRST_OFDM_RATE;
2506 if ((lq_sta->max_rate_idx < 0) ||
2507 (lq_sta->max_rate_idx >= IWL_RATE_COUNT))
2508 lq_sta->max_rate_idx = -1;
2509 }
2510
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002511 /* Send management frames and NO_ACK data using lowest rate. */
Luis R. Rodriguez4c6d4f52009-07-16 10:05:41 -07002512 if (rate_control_send_low(sta, priv_sta, txrc))
Johannes Berg4b7679a2008-09-18 18:14:18 +02002513 return;
Zhu Yib481de92007-09-25 17:54:57 -07002514
Tomas Winkler417f1142008-11-12 13:14:06 -08002515 rate_idx = lq_sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002516
Johannes Berg05c914f2008-09-11 00:01:58 +02002517 if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) &&
Tomas Winklerc33104f2008-01-14 17:46:21 -08002518 !lq_sta->ibss_sta_added) {
Tomas Winklerc587de02009-06-03 11:44:07 -07002519 u8 sta_id = iwl_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002520
2521 if (sta_id == IWL_INVALID_STATION) {
Tomas Winklere1623442009-01-27 14:27:56 -08002522 IWL_DEBUG_RATE(priv, "LQ: ADD station %pM\n",
Johannes Berge1749612008-10-27 15:59:26 -07002523 hdr->addr1);
Tomas Winklerc587de02009-06-03 11:44:07 -07002524 sta_id = iwl_add_station(priv, hdr->addr1,
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002525 false, CMD_ASYNC, ht_cap);
Zhu Yib481de92007-09-25 17:54:57 -07002526 }
2527 if ((sta_id != IWL_INVALID_STATION)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002528 lq_sta->lq.sta_id = sta_id;
2529 lq_sta->lq.rs_table[0].rate_n_flags = 0;
2530 lq_sta->ibss_sta_added = 1;
Johannes Berg4b7679a2008-09-18 18:14:18 +02002531 rs_initialize_lq(priv, conf, sta, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002532 }
Zhu Yib481de92007-09-25 17:54:57 -07002533 }
2534
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002535 if (lq_sta->last_rate_n_flags & RATE_MCS_HT_MSK) {
Tomas Winkler417f1142008-11-12 13:14:06 -08002536 rate_idx -= IWL_FIRST_OFDM_RATE;
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002537 /* 6M and 9M shared same MCS index */
2538 rate_idx = (rate_idx > 0) ? (rate_idx - 1) : 0;
2539 if (rs_extract_rate(lq_sta->last_rate_n_flags) >=
2540 IWL_RATE_MIMO3_6M_PLCP)
2541 rate_idx = rate_idx + (2 * MCS_INDEX_PER_STREAM);
2542 else if (rs_extract_rate(lq_sta->last_rate_n_flags) >=
2543 IWL_RATE_MIMO2_6M_PLCP)
2544 rate_idx = rate_idx + MCS_INDEX_PER_STREAM;
2545 info->control.rates[0].flags = IEEE80211_TX_RC_MCS;
2546 if (lq_sta->last_rate_n_flags & RATE_MCS_SGI_MSK)
2547 info->control.rates[0].flags |= IEEE80211_TX_RC_SHORT_GI;
2548 if (lq_sta->last_rate_n_flags & RATE_MCS_DUP_MSK)
2549 info->control.rates[0].flags |= IEEE80211_TX_RC_DUP_DATA;
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07002550 if (lq_sta->last_rate_n_flags & RATE_MCS_HT40_MSK)
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002551 info->control.rates[0].flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
2552 if (lq_sta->last_rate_n_flags & RATE_MCS_GF_MSK)
2553 info->control.rates[0].flags |= IEEE80211_TX_RC_GREEN_FIELD;
2554 } else {
Daniel C Halperin50273092009-08-28 09:44:45 -07002555 /* Check for invalid rates */
2556 if ((rate_idx < 0) || (rate_idx >= IWL_RATE_COUNT_LEGACY) ||
2557 ((sband->band == IEEE80211_BAND_5GHZ) &&
2558 (rate_idx < IWL_FIRST_OFDM_RATE)))
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002559 rate_idx = rate_lowest_index(sband, sta);
Daniel C Halperin50273092009-08-28 09:44:45 -07002560 /* On valid 5 GHz rate, adjust index */
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002561 else if (sband->band == IEEE80211_BAND_5GHZ)
2562 rate_idx -= IWL_FIRST_OFDM_RATE;
Daniel C Halperin7ebaeff2009-08-21 13:34:20 -07002563 info->control.rates[0].flags = 0;
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002564 }
Tomas Winkler417f1142008-11-12 13:14:06 -08002565 info->control.rates[0].idx = rate_idx;
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002566
Zhu Yib481de92007-09-25 17:54:57 -07002567}
2568
Johannes Berg4b7679a2008-09-18 18:14:18 +02002569static void *rs_alloc_sta(void *priv_rate, struct ieee80211_sta *sta,
2570 gfp_t gfp)
Zhu Yib481de92007-09-25 17:54:57 -07002571{
Tomas Winklere227cea2008-07-18 13:53:05 +08002572 struct iwl_lq_sta *lq_sta;
Ester Kummerbf403db2008-05-05 10:22:40 +08002573 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07002574 int i, j;
2575
Ester Kummerbf403db2008-05-05 10:22:40 +08002576 priv = (struct iwl_priv *)priv_rate;
Tomas Winklere1623442009-01-27 14:27:56 -08002577 IWL_DEBUG_RATE(priv, "create station rate scale window\n");
Zhu Yib481de92007-09-25 17:54:57 -07002578
Tomas Winklere227cea2008-07-18 13:53:05 +08002579 lq_sta = kzalloc(sizeof(struct iwl_lq_sta), gfp);
Zhu Yib481de92007-09-25 17:54:57 -07002580
Tomas Winklerc33104f2008-01-14 17:46:21 -08002581 if (lq_sta == NULL)
Zhu Yib481de92007-09-25 17:54:57 -07002582 return NULL;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002583 lq_sta->lq.sta_id = 0xff;
Zhu Yib481de92007-09-25 17:54:57 -07002584
Zhu Yi63fddb92007-09-27 11:27:36 +08002585
Zhu Yib481de92007-09-25 17:54:57 -07002586 for (j = 0; j < LQ_SIZE; j++)
2587 for (i = 0; i < IWL_RATE_COUNT; i++)
Tomas Winkler3ac7f142008-07-21 02:40:14 +03002588 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
Zhu Yib481de92007-09-25 17:54:57 -07002589
Tomas Winklerc33104f2008-01-14 17:46:21 -08002590 return lq_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002591}
2592
Johannes Berg4b7679a2008-09-18 18:14:18 +02002593static void rs_rate_init(void *priv_r, struct ieee80211_supported_band *sband,
2594 struct ieee80211_sta *sta, void *priv_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002595{
2596 int i, j;
Johannes Berg4b7679a2008-09-18 18:14:18 +02002597 struct iwl_priv *priv = (struct iwl_priv *)priv_r;
2598 struct ieee80211_conf *conf = &priv->hw->conf;
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002599 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Tomas Winklere227cea2008-07-18 13:53:05 +08002600 struct iwl_lq_sta *lq_sta = priv_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002601
Tomas Winklerc33104f2008-01-14 17:46:21 -08002602 lq_sta->flush_timer = 0;
Johannes Berg4b7679a2008-09-18 18:14:18 +02002603 lq_sta->supp_rates = sta->supp_rates[sband->band];
Zhu Yib481de92007-09-25 17:54:57 -07002604 for (j = 0; j < LQ_SIZE; j++)
2605 for (i = 0; i < IWL_RATE_COUNT; i++)
Tomas Winkler3ac7f142008-07-21 02:40:14 +03002606 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
Zhu Yib481de92007-09-25 17:54:57 -07002607
Tomas Winklere1623442009-01-27 14:27:56 -08002608 IWL_DEBUG_RATE(priv, "LQ: *** rate scale station global init ***\n");
Zhu Yib481de92007-09-25 17:54:57 -07002609 /* TODO: what is a good starting rate for STA? About middle? Maybe not
2610 * the lowest or the highest rate.. Could consider using RSSI from
2611 * previous packets? Need to have IEEE 802.1X auth succeed immediately
2612 * after assoc.. */
2613
Tomas Winklerc33104f2008-01-14 17:46:21 -08002614 lq_sta->ibss_sta_added = 0;
Johannes Berg05c914f2008-09-11 00:01:58 +02002615 if (priv->iw_mode == NL80211_IFTYPE_AP) {
Tomas Winklerc587de02009-06-03 11:44:07 -07002616 u8 sta_id = iwl_find_station(priv,
Abhijeet Kolekare11bc022009-04-08 11:26:41 -07002617 sta->addr);
Joe Perches0795af52007-10-03 17:59:30 -07002618
Zhu Yib481de92007-09-25 17:54:57 -07002619 /* for IBSS the call are from tasklet */
Tomas Winklere1623442009-01-27 14:27:56 -08002620 IWL_DEBUG_RATE(priv, "LQ: ADD station %pM\n", sta->addr);
Zhu Yib481de92007-09-25 17:54:57 -07002621
2622 if (sta_id == IWL_INVALID_STATION) {
Tomas Winklere1623442009-01-27 14:27:56 -08002623 IWL_DEBUG_RATE(priv, "LQ: ADD station %pM\n", sta->addr);
Tomas Winklerc587de02009-06-03 11:44:07 -07002624 sta_id = iwl_add_station(priv, sta->addr, false,
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002625 CMD_ASYNC, ht_cap);
Zhu Yib481de92007-09-25 17:54:57 -07002626 }
2627 if ((sta_id != IWL_INVALID_STATION)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002628 lq_sta->lq.sta_id = sta_id;
2629 lq_sta->lq.rs_table[0].rate_n_flags = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002630 }
2631 /* FIXME: this is w/a remove it later */
2632 priv->assoc_station_added = 1;
2633 }
2634
Tomas Winklerc33104f2008-01-14 17:46:21 -08002635 lq_sta->is_dup = 0;
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002636 lq_sta->max_rate_idx = -1;
Abbas, Mohamed7d049e52009-01-20 21:33:53 -08002637 lq_sta->missed_rate_counter = IWL_MISSED_RATE_MAX;
Daniel C Halperinb2617932009-08-13 13:30:59 -07002638 lq_sta->is_green = rs_use_green(sta, &priv->current_ht_config);
Guy Cohen39e88502008-04-23 17:14:57 -07002639 lq_sta->active_legacy_rate = priv->active_rate & ~(0x1000);
Tomas Winklerc33104f2008-01-14 17:46:21 -08002640 lq_sta->active_rate_basic = priv->active_rate_basic;
Johannes Berg8318d782008-01-24 19:38:38 +01002641 lq_sta->band = priv->band;
Ben Cahill77626352007-11-29 11:09:44 +08002642 /*
2643 * active_siso_rate mask includes 9 MBits (bit 5), and CCK (bits 0-3),
2644 * supp_rates[] does not; shift to convert format, force 9 MBits off.
2645 */
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002646 lq_sta->active_siso_rate = ht_cap->mcs.rx_mask[0] << 1;
2647 lq_sta->active_siso_rate |= ht_cap->mcs.rx_mask[0] & 0x1;
Guy Cohenfde0db32008-04-21 15:42:01 -07002648 lq_sta->active_siso_rate &= ~((u16)0x2);
2649 lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -07002650
Ben Cahill77626352007-11-29 11:09:44 +08002651 /* Same here */
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002652 lq_sta->active_mimo2_rate = ht_cap->mcs.rx_mask[1] << 1;
2653 lq_sta->active_mimo2_rate |= ht_cap->mcs.rx_mask[1] & 0x1;
Guy Cohenfde0db32008-04-21 15:42:01 -07002654 lq_sta->active_mimo2_rate &= ~((u16)0x2);
2655 lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE;
2656
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002657 lq_sta->active_mimo3_rate = ht_cap->mcs.rx_mask[2] << 1;
2658 lq_sta->active_mimo3_rate |= ht_cap->mcs.rx_mask[2] & 0x1;
Guy Cohenfde0db32008-04-21 15:42:01 -07002659 lq_sta->active_mimo3_rate &= ~((u16)0x2);
2660 lq_sta->active_mimo3_rate <<= IWL_FIRST_OFDM_RATE;
2661
Tomas Winklere1623442009-01-27 14:27:56 -08002662 IWL_DEBUG_RATE(priv, "SISO-RATE=%X MIMO2-RATE=%X MIMO3-RATE=%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002663 lq_sta->active_siso_rate,
Guy Cohenfde0db32008-04-21 15:42:01 -07002664 lq_sta->active_mimo2_rate,
2665 lq_sta->active_mimo3_rate);
2666
Tomas Winklera96a27f2008-10-23 23:48:56 -07002667 /* These values will be overridden later */
Guy Cohen07bc28e2008-04-23 17:15:03 -07002668 lq_sta->lq.general_params.single_stream_ant_msk = ANT_A;
2669 lq_sta->lq.general_params.dual_stream_ant_msk = ANT_AB;
2670
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002671 /* as default allow aggregation for all tids */
2672 lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002673 lq_sta->drv = priv;
Zhu Yib481de92007-09-25 17:54:57 -07002674
Daniel C Halperin50273092009-08-28 09:44:45 -07002675 /* Set last_txrate_idx to lowest rate */
2676 lq_sta->last_txrate_idx = rate_lowest_index(sband, sta);
2677 if (sband->band == IEEE80211_BAND_5GHZ)
2678 lq_sta->last_txrate_idx += IWL_FIRST_OFDM_RATE;
Mohamed Abbasd5e49032008-12-12 08:22:15 -08002679
Johannes Berg4b7679a2008-09-18 18:14:18 +02002680 rs_initialize_lq(priv, conf, sta, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002681}
2682
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002683static void rs_fill_link_cmd(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08002684 struct iwl_lq_sta *lq_sta, u32 new_rate)
Zhu Yib481de92007-09-25 17:54:57 -07002685{
Tomas Winklere227cea2008-07-18 13:53:05 +08002686 struct iwl_scale_tbl_info tbl_type;
Zhu Yib481de92007-09-25 17:54:57 -07002687 int index = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002688 int rate_idx;
Zhu Yi1b696de2007-09-27 11:27:41 +08002689 int repeat_rate = 0;
Guy Cohenaade00c2008-04-23 17:14:59 -07002690 u8 ant_toggle_cnt = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002691 u8 use_ht_possible = 1;
Guy Cohenaade00c2008-04-23 17:14:59 -07002692 u8 valid_tx_ant = 0;
Guy Cohen07bc28e2008-04-23 17:15:03 -07002693 struct iwl_link_quality_cmd *lq_cmd = &lq_sta->lq;
Zhu Yib481de92007-09-25 17:54:57 -07002694
Ben Cahill77626352007-11-29 11:09:44 +08002695 /* Override starting rate (index 0) if needed for debug purposes */
Guy Cohen39e88502008-04-23 17:14:57 -07002696 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Zhu Yi98d7e092007-09-27 11:27:42 +08002697
Guy Cohen39e88502008-04-23 17:14:57 -07002698 /* Interpret new_rate (rate_n_flags) */
Guy Cohenaade00c2008-04-23 17:14:59 -07002699 memset(&tbl_type, 0, sizeof(tbl_type));
Guy Cohen39e88502008-04-23 17:14:57 -07002700 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band,
Zhu Yib481de92007-09-25 17:54:57 -07002701 &tbl_type, &rate_idx);
2702
Ben Cahill77626352007-11-29 11:09:44 +08002703 /* How many times should we repeat the initial rate? */
Zhu Yib481de92007-09-25 17:54:57 -07002704 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07002705 ant_toggle_cnt = 1;
Zhu Yi1b696de2007-09-27 11:27:41 +08002706 repeat_rate = IWL_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002707 } else {
Zhu Yi1b696de2007-09-27 11:27:41 +08002708 repeat_rate = IWL_HT_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002709 }
Zhu Yib481de92007-09-25 17:54:57 -07002710
2711 lq_cmd->general_params.mimo_delimiter =
2712 is_mimo(tbl_type.lq_type) ? 1 : 0;
Ben Cahill77626352007-11-29 11:09:44 +08002713
2714 /* Fill 1st table entry (index 0) */
Guy Cohen39e88502008-04-23 17:14:57 -07002715 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002716
Guy Cohen07bc28e2008-04-23 17:15:03 -07002717 if (num_of_ant(tbl_type.ant_type) == 1) {
2718 lq_cmd->general_params.single_stream_ant_msk =
2719 tbl_type.ant_type;
2720 } else if (num_of_ant(tbl_type.ant_type) == 2) {
2721 lq_cmd->general_params.dual_stream_ant_msk =
2722 tbl_type.ant_type;
2723 } /* otherwise we don't modify the existing value */
Zhu Yib481de92007-09-25 17:54:57 -07002724
2725 index++;
Zhu Yi1b696de2007-09-27 11:27:41 +08002726 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002727
Guy Cohenaade00c2008-04-23 17:14:59 -07002728 if (priv)
2729 valid_tx_ant = priv->hw_params.valid_tx_ant;
2730
Ben Cahill77626352007-11-29 11:09:44 +08002731 /* Fill rest of rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002732 while (index < LINK_QUAL_MAX_RETRY_NUM) {
Ben Cahill77626352007-11-29 11:09:44 +08002733 /* Repeat initial/next rate.
2734 * For legacy IWL_NUMBER_TRY == 1, this loop will not execute.
2735 * For HT IWL_HT_NUMBER_TRY == 3, this executes twice. */
Zhu Yi1b696de2007-09-27 11:27:41 +08002736 while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
Zhu Yib481de92007-09-25 17:54:57 -07002737 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07002738 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2739 ant_toggle_cnt++;
2740 else if (priv &&
2741 rs_toggle_antenna(valid_tx_ant,
2742 &new_rate, &tbl_type))
2743 ant_toggle_cnt = 1;
2744}
Zhu Yi98d7e092007-09-27 11:27:42 +08002745
Ben Cahill77626352007-11-29 11:09:44 +08002746 /* Override next rate if needed for debug purposes */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002747 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Ben Cahill77626352007-11-29 11:09:44 +08002748
2749 /* Fill next table entry */
Zhu Yib481de92007-09-25 17:54:57 -07002750 lq_cmd->rs_table[index].rate_n_flags =
Guy Cohen39e88502008-04-23 17:14:57 -07002751 cpu_to_le32(new_rate);
Zhu Yi1b696de2007-09-27 11:27:41 +08002752 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002753 index++;
2754 }
2755
Guy Cohen39e88502008-04-23 17:14:57 -07002756 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band, &tbl_type,
Zhu Yib481de92007-09-25 17:54:57 -07002757 &rate_idx);
2758
Ben Cahill77626352007-11-29 11:09:44 +08002759 /* Indicate to uCode which entries might be MIMO.
2760 * If initial rate was MIMO, this will finally end up
2761 * as (IWL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
Zhu Yib481de92007-09-25 17:54:57 -07002762 if (is_mimo(tbl_type.lq_type))
2763 lq_cmd->general_params.mimo_delimiter = index;
2764
Ben Cahill77626352007-11-29 11:09:44 +08002765 /* Get next rate */
Guy Cohen39e88502008-04-23 17:14:57 -07002766 new_rate = rs_get_lower_rate(lq_sta, &tbl_type, rate_idx,
2767 use_ht_possible);
Zhu Yib481de92007-09-25 17:54:57 -07002768
Ben Cahill77626352007-11-29 11:09:44 +08002769 /* How many times should we repeat the next rate? */
Zhu Yib481de92007-09-25 17:54:57 -07002770 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07002771 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2772 ant_toggle_cnt++;
2773 else if (priv &&
2774 rs_toggle_antenna(valid_tx_ant,
2775 &new_rate, &tbl_type))
2776 ant_toggle_cnt = 1;
2777
Zhu Yi1b696de2007-09-27 11:27:41 +08002778 repeat_rate = IWL_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002779 } else {
Zhu Yi1b696de2007-09-27 11:27:41 +08002780 repeat_rate = IWL_HT_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002781 }
Zhu Yib481de92007-09-25 17:54:57 -07002782
Ben Cahill77626352007-11-29 11:09:44 +08002783 /* Don't allow HT rates after next pass.
2784 * rs_get_lower_rate() will change type to LQ_A or LQ_G. */
Zhu Yib481de92007-09-25 17:54:57 -07002785 use_ht_possible = 0;
2786
Ben Cahill77626352007-11-29 11:09:44 +08002787 /* Override next rate if needed for debug purposes */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002788 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Ben Cahill77626352007-11-29 11:09:44 +08002789
2790 /* Fill next table entry */
Guy Cohen39e88502008-04-23 17:14:57 -07002791 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002792
2793 index++;
Zhu Yi1b696de2007-09-27 11:27:41 +08002794 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002795 }
2796
Wey-Yi Guy13c33a02009-06-03 11:44:11 -07002797 lq_cmd->agg_params.agg_frame_cnt_limit = LINK_QUAL_AGG_FRAME_LIMIT_MAX;
2798 lq_cmd->agg_params.agg_dis_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
2799 lq_cmd->agg_params.agg_time_limit =
2800 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
Zhu Yib481de92007-09-25 17:54:57 -07002801}
2802
Johannes Berg4b7679a2008-09-18 18:14:18 +02002803static void *rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
Zhu Yib481de92007-09-25 17:54:57 -07002804{
Johannes Berg4b7679a2008-09-18 18:14:18 +02002805 return hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07002806}
2807/* rate scale requires free function to be implemented */
2808static void rs_free(void *priv_rate)
2809{
2810 return;
2811}
2812
Johannes Berg4b7679a2008-09-18 18:14:18 +02002813static void rs_free_sta(void *priv_r, struct ieee80211_sta *sta,
2814 void *priv_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002815{
Tomas Winklere227cea2008-07-18 13:53:05 +08002816 struct iwl_lq_sta *lq_sta = priv_sta;
Rami Rosen45527c22008-10-07 09:50:01 +02002817 struct iwl_priv *priv __maybe_unused = priv_r;
Zhu Yib481de92007-09-25 17:54:57 -07002818
Tomas Winklere1623442009-01-27 14:27:56 -08002819 IWL_DEBUG_RATE(priv, "enter\n");
Tomas Winklerc33104f2008-01-14 17:46:21 -08002820 kfree(lq_sta);
Tomas Winklere1623442009-01-27 14:27:56 -08002821 IWL_DEBUG_RATE(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07002822}
2823
2824
Zhu Yi93dc6462007-09-27 11:27:37 +08002825#ifdef CONFIG_MAC80211_DEBUGFS
Zhu Yi5ae212c2007-09-27 11:27:38 +08002826static int open_file_generic(struct inode *inode, struct file *file)
2827{
2828 file->private_data = inode->i_private;
2829 return 0;
2830}
Tomas Winklere227cea2008-07-18 13:53:05 +08002831static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
2832 u32 *rate_n_flags, int index)
Zhu Yi98d7e092007-09-27 11:27:42 +08002833{
Ester Kummerbf403db2008-05-05 10:22:40 +08002834 struct iwl_priv *priv;
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07002835 u8 valid_tx_ant;
2836 u8 ant_sel_tx;
Ester Kummerbf403db2008-05-05 10:22:40 +08002837
2838 priv = lq_sta->drv;
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07002839 valid_tx_ant = priv->hw_params.valid_tx_ant;
Guy Cohen39e88502008-04-23 17:14:57 -07002840 if (lq_sta->dbg_fixed_rate) {
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07002841 ant_sel_tx =
2842 ((lq_sta->dbg_fixed_rate & RATE_MCS_ANT_ABC_MSK)
2843 >> RATE_MCS_ANT_POS);
2844 if ((valid_tx_ant & ant_sel_tx) == ant_sel_tx) {
Guy Cohen39e88502008-04-23 17:14:57 -07002845 *rate_n_flags = lq_sta->dbg_fixed_rate;
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07002846 IWL_DEBUG_RATE(priv, "Fixed rate ON\n");
Guy Cohen39e88502008-04-23 17:14:57 -07002847 } else {
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07002848 lq_sta->dbg_fixed_rate = 0;
2849 IWL_ERR(priv,
2850 "Invalid antenna selection 0x%X, Valid is 0x%X\n",
2851 ant_sel_tx, valid_tx_ant);
2852 IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
Guy Cohen39e88502008-04-23 17:14:57 -07002853 }
Guy Cohen39e88502008-04-23 17:14:57 -07002854 } else {
Tomas Winklere1623442009-01-27 14:27:56 -08002855 IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
Zhu Yi98d7e092007-09-27 11:27:42 +08002856 }
Zhu Yi98d7e092007-09-27 11:27:42 +08002857}
2858
2859static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
2860 const char __user *user_buf, size_t count, loff_t *ppos)
2861{
Tomas Winklere227cea2008-07-18 13:53:05 +08002862 struct iwl_lq_sta *lq_sta = file->private_data;
Ester Kummerbf403db2008-05-05 10:22:40 +08002863 struct iwl_priv *priv;
Zhu Yi98d7e092007-09-27 11:27:42 +08002864 char buf[64];
2865 int buf_size;
2866 u32 parsed_rate;
2867
Ester Kummerbf403db2008-05-05 10:22:40 +08002868 priv = lq_sta->drv;
Zhu Yi98d7e092007-09-27 11:27:42 +08002869 memset(buf, 0, sizeof(buf));
2870 buf_size = min(count, sizeof(buf) - 1);
2871 if (copy_from_user(buf, user_buf, buf_size))
2872 return -EFAULT;
2873
2874 if (sscanf(buf, "%x", &parsed_rate) == 1)
Guy Cohen39e88502008-04-23 17:14:57 -07002875 lq_sta->dbg_fixed_rate = parsed_rate;
Zhu Yi98d7e092007-09-27 11:27:42 +08002876 else
Guy Cohen39e88502008-04-23 17:14:57 -07002877 lq_sta->dbg_fixed_rate = 0;
Zhu Yi98d7e092007-09-27 11:27:42 +08002878
Guy Cohen39e88502008-04-23 17:14:57 -07002879 lq_sta->active_legacy_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
2880 lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2881 lq_sta->active_mimo2_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2882 lq_sta->active_mimo3_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
Zhu Yi98d7e092007-09-27 11:27:42 +08002883
Tomas Winklere1623442009-01-27 14:27:56 -08002884 IWL_DEBUG_RATE(priv, "sta_id %d rate 0x%X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07002885 lq_sta->lq.sta_id, lq_sta->dbg_fixed_rate);
Zhu Yi98d7e092007-09-27 11:27:42 +08002886
Guy Cohen39e88502008-04-23 17:14:57 -07002887 if (lq_sta->dbg_fixed_rate) {
Guy Cohen07bc28e2008-04-23 17:15:03 -07002888 rs_fill_link_cmd(NULL, lq_sta, lq_sta->dbg_fixed_rate);
Tomas Winkler66c73db2008-04-15 16:01:40 -07002889 iwl_send_lq_cmd(lq_sta->drv, &lq_sta->lq, CMD_ASYNC);
Zhu Yi98d7e092007-09-27 11:27:42 +08002890 }
2891
2892 return count;
2893}
Zhu Yi0209dc12007-09-27 11:27:43 +08002894
Zhu Yi5ae212c2007-09-27 11:27:38 +08002895static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
2896 char __user *user_buf, size_t count, loff_t *ppos)
2897{
Frank Seidela412c802009-03-01 20:25:38 +01002898 char *buff;
Zhu Yi5ae212c2007-09-27 11:27:38 +08002899 int desc = 0;
2900 int i = 0;
Wey-Yi Guy12b96812009-04-08 11:39:27 -07002901 int index = 0;
Frank Seidela412c802009-03-01 20:25:38 +01002902 ssize_t ret;
Zhu Yi5ae212c2007-09-27 11:27:38 +08002903
Tomas Winklere227cea2008-07-18 13:53:05 +08002904 struct iwl_lq_sta *lq_sta = file->private_data;
Wey-Yi Guyd8ae4f52009-03-10 14:35:07 -07002905 struct iwl_priv *priv;
Wey-Yi Guyadb7b5e2009-03-10 14:35:08 -07002906 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002907
Wey-Yi Guyd8ae4f52009-03-10 14:35:07 -07002908 priv = lq_sta->drv;
Frank Seidela412c802009-03-01 20:25:38 +01002909 buff = kmalloc(1024, GFP_KERNEL);
2910 if (!buff)
2911 return -ENOMEM;
2912
Tomas Winklerc33104f2008-01-14 17:46:21 -08002913 desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
Zhu Yi98d7e092007-09-27 11:27:42 +08002914 desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002915 lq_sta->total_failed, lq_sta->total_success,
Guy Cohen39e88502008-04-23 17:14:57 -07002916 lq_sta->active_legacy_rate);
Zhu Yi98d7e092007-09-27 11:27:42 +08002917 desc += sprintf(buff+desc, "fixed rate 0x%X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07002918 lq_sta->dbg_fixed_rate);
Wey-Yi Guyd8ae4f52009-03-10 14:35:07 -07002919 desc += sprintf(buff+desc, "valid_tx_ant %s%s%s\n",
2920 (priv->hw_params.valid_tx_ant & ANT_A) ? "ANT_A," : "",
2921 (priv->hw_params.valid_tx_ant & ANT_B) ? "ANT_B," : "",
2922 (priv->hw_params.valid_tx_ant & ANT_C) ? "ANT_C" : "");
Wey-Yi Guyadb7b5e2009-03-10 14:35:08 -07002923 desc += sprintf(buff+desc, "lq type %s\n",
2924 (is_legacy(tbl->lq_type)) ? "legacy" : "HT");
2925 if (is_Ht(tbl->lq_type)) {
2926 desc += sprintf(buff+desc, " %s",
2927 (is_siso(tbl->lq_type)) ? "SISO" :
2928 ((is_mimo2(tbl->lq_type)) ? "MIMO2" : "MIMO3"));
2929 desc += sprintf(buff+desc, " %s",
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07002930 (tbl->is_ht40) ? "40MHz" : "20MHz");
Wey-Yi Guy2681b202009-05-21 13:44:22 -07002931 desc += sprintf(buff+desc, " %s %s\n", (tbl->is_SGI) ? "SGI" : "",
2932 (lq_sta->is_green) ? "GF enabled" : "");
Wey-Yi Guyadb7b5e2009-03-10 14:35:08 -07002933 }
Wey-Yi Guy12b96812009-04-08 11:39:27 -07002934 desc += sprintf(buff+desc, "last tx rate=0x%X\n",
2935 lq_sta->last_rate_n_flags);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002936 desc += sprintf(buff+desc, "general:"
2937 "flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002938 lq_sta->lq.general_params.flags,
2939 lq_sta->lq.general_params.mimo_delimiter,
2940 lq_sta->lq.general_params.single_stream_ant_msk,
2941 lq_sta->lq.general_params.dual_stream_ant_msk);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002942
2943 desc += sprintf(buff+desc, "agg:"
2944 "time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002945 le16_to_cpu(lq_sta->lq.agg_params.agg_time_limit),
2946 lq_sta->lq.agg_params.agg_dis_start_th,
2947 lq_sta->lq.agg_params.agg_frame_cnt_limit);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002948
2949 desc += sprintf(buff+desc,
2950 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002951 lq_sta->lq.general_params.start_rate_index[0],
2952 lq_sta->lq.general_params.start_rate_index[1],
2953 lq_sta->lq.general_params.start_rate_index[2],
2954 lq_sta->lq.general_params.start_rate_index[3]);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002955
Wey-Yi Guy12b96812009-04-08 11:39:27 -07002956 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
2957 index = iwl_hwrate_to_plcp_idx(
2958 le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags));
2959 if (is_legacy(tbl->lq_type)) {
2960 desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps\n",
2961 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags),
2962 iwl_rate_mcs[index].mbps);
2963 } else {
2964 desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps (%s)\n",
2965 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags),
2966 iwl_rate_mcs[index].mbps, iwl_rate_mcs[index].mcs);
2967 }
2968 }
Zhu Yi5ae212c2007-09-27 11:27:38 +08002969
Frank Seidela412c802009-03-01 20:25:38 +01002970 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2971 kfree(buff);
2972 return ret;
Zhu Yi5ae212c2007-09-27 11:27:38 +08002973}
2974
2975static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
Zhu Yi98d7e092007-09-27 11:27:42 +08002976 .write = rs_sta_dbgfs_scale_table_write,
Zhu Yi5ae212c2007-09-27 11:27:38 +08002977 .read = rs_sta_dbgfs_scale_table_read,
2978 .open = open_file_generic,
2979};
Zhu Yi0209dc12007-09-27 11:27:43 +08002980static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
2981 char __user *user_buf, size_t count, loff_t *ppos)
2982{
Frank Seidela412c802009-03-01 20:25:38 +01002983 char *buff;
Zhu Yi0209dc12007-09-27 11:27:43 +08002984 int desc = 0;
2985 int i, j;
Frank Seidela412c802009-03-01 20:25:38 +01002986 ssize_t ret;
Zhu Yi0209dc12007-09-27 11:27:43 +08002987
Tomas Winklere227cea2008-07-18 13:53:05 +08002988 struct iwl_lq_sta *lq_sta = file->private_data;
Frank Seidela412c802009-03-01 20:25:38 +01002989
2990 buff = kmalloc(1024, GFP_KERNEL);
2991 if (!buff)
2992 return -ENOMEM;
2993
Zhu Yi0209dc12007-09-27 11:27:43 +08002994 for (i = 0; i < LQ_SIZE; i++) {
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07002995 desc += sprintf(buff+desc,
2996 "%s type=%d SGI=%d HT40=%d DUP=%d GF=%d\n"
Zhu Yi0209dc12007-09-27 11:27:43 +08002997 "rate=0x%X\n",
Abhijeet Kolekarc3056062008-11-12 13:14:08 -08002998 lq_sta->active_tbl == i ? "*" : "x",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002999 lq_sta->lq_info[i].lq_type,
3000 lq_sta->lq_info[i].is_SGI,
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07003001 lq_sta->lq_info[i].is_ht40,
Tomas Winklerc33104f2008-01-14 17:46:21 -08003002 lq_sta->lq_info[i].is_dup,
Wey-Yi Guy2681b202009-05-21 13:44:22 -07003003 lq_sta->is_green,
Guy Cohen39e88502008-04-23 17:14:57 -07003004 lq_sta->lq_info[i].current_rate);
Zhu Yi0209dc12007-09-27 11:27:43 +08003005 for (j = 0; j < IWL_RATE_COUNT; j++) {
3006 desc += sprintf(buff+desc,
Tomas Winklerc33104f2008-01-14 17:46:21 -08003007 "counter=%d success=%d %%=%d\n",
3008 lq_sta->lq_info[i].win[j].counter,
3009 lq_sta->lq_info[i].win[j].success_counter,
3010 lq_sta->lq_info[i].win[j].success_ratio);
Zhu Yi0209dc12007-09-27 11:27:43 +08003011 }
3012 }
Frank Seidela412c802009-03-01 20:25:38 +01003013 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
3014 kfree(buff);
3015 return ret;
Zhu Yi0209dc12007-09-27 11:27:43 +08003016}
3017
3018static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
3019 .read = rs_sta_dbgfs_stats_table_read,
3020 .open = open_file_generic,
3021};
Zhu Yi5ae212c2007-09-27 11:27:38 +08003022
Wey-Yi Guy38167452009-05-08 13:44:43 -07003023static ssize_t rs_sta_dbgfs_rate_scale_data_read(struct file *file,
3024 char __user *user_buf, size_t count, loff_t *ppos)
3025{
3026 char buff[120];
3027 int desc = 0;
3028 ssize_t ret;
3029
3030 struct iwl_lq_sta *lq_sta = file->private_data;
3031 struct iwl_priv *priv;
3032 struct iwl_scale_tbl_info *tbl = &lq_sta->lq_info[lq_sta->active_tbl];
3033
3034 priv = lq_sta->drv;
3035
3036 if (is_Ht(tbl->lq_type))
3037 desc += sprintf(buff+desc,
3038 "Bit Rate= %d Mb/s\n",
3039 tbl->expected_tpt[lq_sta->last_txrate_idx]);
3040 else
3041 desc += sprintf(buff+desc,
3042 "Bit Rate= %d Mb/s\n",
3043 iwl_rates[lq_sta->last_txrate_idx].ieee >> 1);
3044 desc += sprintf(buff+desc,
3045 "Signal Level= %d dBm\tNoise Level= %d dBm\n",
3046 priv->last_rx_rssi, priv->last_rx_noise);
3047 desc += sprintf(buff+desc,
3048 "Tsf= 0x%llx\tBeacon time= 0x%08X\n",
3049 priv->last_tsf, priv->last_beacon_time);
3050
3051 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
3052 return ret;
3053}
3054
3055static const struct file_operations rs_sta_dbgfs_rate_scale_data_ops = {
3056 .read = rs_sta_dbgfs_rate_scale_data_read,
3057 .open = open_file_generic,
3058};
3059
Zhu Yi93dc6462007-09-27 11:27:37 +08003060static void rs_add_debugfs(void *priv, void *priv_sta,
3061 struct dentry *dir)
3062{
Tomas Winklere227cea2008-07-18 13:53:05 +08003063 struct iwl_lq_sta *lq_sta = priv_sta;
Tomas Winklerc33104f2008-01-14 17:46:21 -08003064 lq_sta->rs_sta_dbgfs_scale_table_file =
Zhu Yi0209dc12007-09-27 11:27:43 +08003065 debugfs_create_file("rate_scale_table", 0600, dir,
Tomas Winklerc33104f2008-01-14 17:46:21 -08003066 lq_sta, &rs_sta_dbgfs_scale_table_ops);
3067 lq_sta->rs_sta_dbgfs_stats_table_file =
Zhu Yi0209dc12007-09-27 11:27:43 +08003068 debugfs_create_file("rate_stats_table", 0600, dir,
Tomas Winklerc33104f2008-01-14 17:46:21 -08003069 lq_sta, &rs_sta_dbgfs_stats_table_ops);
Wey-Yi Guy38167452009-05-08 13:44:43 -07003070 lq_sta->rs_sta_dbgfs_rate_scale_data_file =
3071 debugfs_create_file("rate_scale_data", 0600, dir,
3072 lq_sta, &rs_sta_dbgfs_rate_scale_data_ops);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02003073 lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file =
3074 debugfs_create_u8("tx_agg_tid_enable", 0600, dir,
3075 &lq_sta->tx_agg_tid_en);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02003076
Zhu Yi93dc6462007-09-27 11:27:37 +08003077}
3078
3079static void rs_remove_debugfs(void *priv, void *priv_sta)
3080{
Tomas Winklere227cea2008-07-18 13:53:05 +08003081 struct iwl_lq_sta *lq_sta = priv_sta;
Tomas Winklerc33104f2008-01-14 17:46:21 -08003082 debugfs_remove(lq_sta->rs_sta_dbgfs_scale_table_file);
3083 debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
Wey-Yi Guy38167452009-05-08 13:44:43 -07003084 debugfs_remove(lq_sta->rs_sta_dbgfs_rate_scale_data_file);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02003085 debugfs_remove(lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file);
Zhu Yi93dc6462007-09-27 11:27:37 +08003086}
3087#endif
3088
Zhu Yib481de92007-09-25 17:54:57 -07003089static struct rate_control_ops rs_ops = {
3090 .module = NULL,
3091 .name = RS_NAME,
3092 .tx_status = rs_tx_status,
3093 .get_rate = rs_get_rate,
3094 .rate_init = rs_rate_init,
Zhu Yib481de92007-09-25 17:54:57 -07003095 .alloc = rs_alloc,
3096 .free = rs_free,
3097 .alloc_sta = rs_alloc_sta,
3098 .free_sta = rs_free_sta,
Zhu Yi93dc6462007-09-27 11:27:37 +08003099#ifdef CONFIG_MAC80211_DEBUGFS
3100 .add_sta_debugfs = rs_add_debugfs,
3101 .remove_sta_debugfs = rs_remove_debugfs,
3102#endif
Zhu Yib481de92007-09-25 17:54:57 -07003103};
3104
Tomas Winklere227cea2008-07-18 13:53:05 +08003105int iwlagn_rate_control_register(void)
Zhu Yib481de92007-09-25 17:54:57 -07003106{
Reinette Chatre897e1cf2008-03-28 16:21:09 -07003107 return ieee80211_rate_control_register(&rs_ops);
Zhu Yib481de92007-09-25 17:54:57 -07003108}
3109
Tomas Winklere227cea2008-07-18 13:53:05 +08003110void iwlagn_rate_control_unregister(void)
Zhu Yib481de92007-09-25 17:54:57 -07003111{
3112 ieee80211_rate_control_unregister(&rs_ops);
3113}
3114