blob: 469d56321d88bd5697647523947a632d40d0aaee [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 Winklere7d326a2008-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/*
824 * mac80211 sends us Tx status
825 */
Johannes Berg4b7679a2008-09-18 18:14:18 +0200826static void rs_tx_status(void *priv_r, struct ieee80211_supported_band *sband,
827 struct ieee80211_sta *sta, void *priv_sta,
Johannes Berge039fa42008-05-15 12:55:29 +0200828 struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -0700829{
830 int status;
831 u8 retries;
Daniel C Halperin50273092009-08-28 09:44:45 -0700832 int rs_index, mac_index, index = 0;
Tomas Winkler417f1142008-11-12 13:14:06 -0800833 struct iwl_lq_sta *lq_sta = priv_sta;
Tomas Winkler66c73db2008-04-15 16:01:40 -0700834 struct iwl_link_quality_cmd *table;
Zhu Yib481de92007-09-25 17:54:57 -0700835 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berg4b7679a2008-09-18 18:14:18 +0200836 struct iwl_priv *priv = (struct iwl_priv *)priv_r;
Johannes Berge039fa42008-05-15 12:55:29 +0200837 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Tomas Winklere227cea2008-07-18 13:53:05 +0800838 struct iwl_rate_scale_data *window = NULL;
839 struct iwl_rate_scale_data *search_win = NULL;
Daniel C Halperin50273092009-08-28 09:44:45 -0700840 enum mac80211_rate_control_flags mac_flags;
Guy Cohen39e88502008-04-23 17:14:57 -0700841 u32 tx_rate;
Tomas Winklere227cea2008-07-18 13:53:05 +0800842 struct iwl_scale_tbl_info tbl_type;
843 struct iwl_scale_tbl_info *curr_tbl, *search_tbl;
Zhu Yib481de92007-09-25 17:54:57 -0700844 u8 active_index = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700845 s32 tpt = 0;
846
Tomas Winklere1623442009-01-27 14:27:56 -0800847 IWL_DEBUG_RATE_LIMIT(priv, "get frame ack response, update rate scale window\n");
Zhu Yib481de92007-09-25 17:54:57 -0700848
Tomas Winkler417f1142008-11-12 13:14:06 -0800849 if (!ieee80211_is_data(hdr->frame_control) ||
Gábor Stefanik514d65c2009-04-23 19:36:08 +0200850 info->flags & IEEE80211_TX_CTL_NO_ACK)
Zhu Yib481de92007-09-25 17:54:57 -0700851 return;
852
Ron Rindjunsky99556432008-01-28 14:07:25 +0200853 /* This packet was aggregated but doesn't carry rate scale info */
Johannes Berge039fa42008-05-15 12:55:29 +0200854 if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
855 !(info->flags & IEEE80211_TX_STAT_AMPDU))
Ron Rindjunsky99556432008-01-28 14:07:25 +0200856 return;
857
Wey-Yi Guy8fe72312009-03-10 14:35:10 -0700858 if (info->flags & IEEE80211_TX_STAT_AMPDU)
859 retries = 0;
860 else
861 retries = info->status.rates[0].count - 1;
Zhu Yib481de92007-09-25 17:54:57 -0700862
863 if (retries > 15)
864 retries = 15;
865
Johannes Berg05c914f2008-09-11 00:01:58 +0200866 if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) &&
Tomas Winklerc33104f2008-01-14 17:46:21 -0800867 !lq_sta->ibss_sta_added)
Tomas Winklerf4d60822008-03-05 11:31:00 -0800868 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700869
Tomas Winklerc33104f2008-01-14 17:46:21 -0800870 table = &lq_sta->lq;
871 active_index = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -0700872
Tomas Winklerc33104f2008-01-14 17:46:21 -0800873 curr_tbl = &(lq_sta->lq_info[active_index]);
874 search_tbl = &(lq_sta->lq_info[(1 - active_index)]);
Tomas Winklere227cea2008-07-18 13:53:05 +0800875 window = (struct iwl_rate_scale_data *)&(curr_tbl->win[0]);
876 search_win = (struct iwl_rate_scale_data *)&(search_tbl->win[0]);
Zhu Yib481de92007-09-25 17:54:57 -0700877
Ben Cahill77626352007-11-29 11:09:44 +0800878 /*
879 * Ignore this Tx frame response if its initial rate doesn't match
880 * that of latest Link Quality command. There may be stragglers
881 * from a previous Link Quality command, but we're no longer interested
882 * in those; they're either from the "active" mode while we're trying
883 * to check "search" mode, or a prior "search" mode after we've moved
884 * to a new "search" mode (which might become the new "active" mode).
885 */
Guy Cohen39e88502008-04-23 17:14:57 -0700886 tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
887 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800888 if (priv->band == IEEE80211_BAND_5GHZ)
889 rs_index -= IWL_FIRST_OFDM_RATE;
Daniel C Halperin50273092009-08-28 09:44:45 -0700890 mac_flags = info->status.rates[0].flags;
891 mac_index = info->status.rates[0].idx;
Daniel C Halperin31513be2009-08-28 09:44:47 -0700892 /* For HT packets, map MCS to PLCP */
893 if (mac_flags & IEEE80211_TX_RC_MCS) {
894 mac_index &= RATE_MCS_CODE_MSK; /* Remove # of streams */
895 if (mac_index >= (IWL_RATE_9M_INDEX - IWL_FIRST_OFDM_RATE))
896 mac_index++;
Daniel C Halperin392a0ba2009-09-11 10:38:08 -0700897 /*
898 * mac80211 HT index is always zero-indexed; we need to move
899 * HT OFDM rates after CCK rates in 2.4 GHz band
900 */
901 if (priv->band == IEEE80211_BAND_2GHZ)
902 mac_index += IWL_FIRST_OFDM_RATE;
Daniel C Halperin31513be2009-08-28 09:44:47 -0700903 }
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800904
Daniel C Halperin50273092009-08-28 09:44:45 -0700905 if ((mac_index < 0) ||
906 (tbl_type.is_SGI != !!(mac_flags & IEEE80211_TX_RC_SHORT_GI)) ||
907 (tbl_type.is_ht40 != !!(mac_flags & IEEE80211_TX_RC_40_MHZ_WIDTH)) ||
908 (tbl_type.is_dup != !!(mac_flags & IEEE80211_TX_RC_DUP_DATA)) ||
Johannes Berge6a98542008-10-21 12:40:02 +0200909 (tbl_type.ant_type != info->antenna_sel_tx) ||
Daniel C Halperin50273092009-08-28 09:44:45 -0700910 (!!(tx_rate & RATE_MCS_HT_MSK) != !!(mac_flags & IEEE80211_TX_RC_MCS)) ||
911 (!!(tx_rate & RATE_MCS_GF_MSK) != !!(mac_flags & IEEE80211_TX_RC_GREEN_FIELD)) ||
Daniel C Halperin31513be2009-08-28 09:44:47 -0700912 (rs_index != mac_index)) {
913 IWL_DEBUG_RATE(priv, "initial rate %d does not match %d (0x%x)\n", mac_index, rs_index, tx_rate);
Mohamed Abbasd5e49032008-12-12 08:22:15 -0800914 /* the last LQ command could failed so the LQ in ucode not
915 * the same in driver sync up
916 */
Abbas, Mohamed7d049e52009-01-20 21:33:53 -0800917 lq_sta->missed_rate_counter++;
918 if (lq_sta->missed_rate_counter > IWL_MISSED_RATE_MAX) {
919 lq_sta->missed_rate_counter = 0;
920 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
921 }
Tomas Winklerf4d60822008-03-05 11:31:00 -0800922 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700923 }
924
Abbas, Mohamed7d049e52009-01-20 21:33:53 -0800925 lq_sta->missed_rate_counter = 0;
Ben Cahill77626352007-11-29 11:09:44 +0800926 /* Update frame history window with "failure" for each Tx retry. */
Zhu Yib481de92007-09-25 17:54:57 -0700927 while (retries) {
Ben Cahill77626352007-11-29 11:09:44 +0800928 /* Look up the rate and other info used for each tx attempt.
929 * Each tx attempt steps one entry deeper in the rate table. */
Guy Cohen39e88502008-04-23 17:14:57 -0700930 tx_rate = le32_to_cpu(table->rs_table[index].rate_n_flags);
931 rs_get_tbl_info_from_mcs(tx_rate, priv->band,
Zhu Yib481de92007-09-25 17:54:57 -0700932 &tbl_type, &rs_index);
933
Ben Cahill77626352007-11-29 11:09:44 +0800934 /* If type matches "search" table,
935 * add failure to "search" history */
Zhu Yib481de92007-09-25 17:54:57 -0700936 if ((tbl_type.lq_type == search_tbl->lq_type) &&
Guy Cohenfde0db32008-04-21 15:42:01 -0700937 (tbl_type.ant_type == search_tbl->ant_type) &&
Zhu Yib481de92007-09-25 17:54:57 -0700938 (tbl_type.is_SGI == search_tbl->is_SGI)) {
939 if (search_tbl->expected_tpt)
940 tpt = search_tbl->expected_tpt[rs_index];
941 else
942 tpt = 0;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200943 rs_collect_tx_data(search_win, rs_index, tpt, 1, 0);
Ben Cahill77626352007-11-29 11:09:44 +0800944
945 /* Else if type matches "current/active" table,
946 * add failure to "current/active" history */
Zhu Yib481de92007-09-25 17:54:57 -0700947 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
Guy Cohenfde0db32008-04-21 15:42:01 -0700948 (tbl_type.ant_type == curr_tbl->ant_type) &&
Zhu Yib481de92007-09-25 17:54:57 -0700949 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
950 if (curr_tbl->expected_tpt)
951 tpt = curr_tbl->expected_tpt[rs_index];
952 else
953 tpt = 0;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200954 rs_collect_tx_data(window, rs_index, tpt, 1, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700955 }
Ben Cahill77626352007-11-29 11:09:44 +0800956
957 /* If not searching for a new mode, increment failed counter
958 * ... this helps determine when to start searching again */
Tomas Winklerc33104f2008-01-14 17:46:21 -0800959 if (lq_sta->stay_in_tbl)
960 lq_sta->total_failed++;
Zhu Yib481de92007-09-25 17:54:57 -0700961 --retries;
962 index++;
963
964 }
965
Ben Cahill77626352007-11-29 11:09:44 +0800966 /*
967 * Find (by rate) the history window to update with final Tx attempt;
968 * if Tx was successful first try, use original rate,
969 * else look up the rate that was, finally, successful.
970 */
Guy Cohen39e88502008-04-23 17:14:57 -0700971 tx_rate = le32_to_cpu(table->rs_table[index].rate_n_flags);
Wey-Yi Guy12b96812009-04-08 11:39:27 -0700972 lq_sta->last_rate_n_flags = tx_rate;
Guy Cohen39e88502008-04-23 17:14:57 -0700973 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
Zhu Yib481de92007-09-25 17:54:57 -0700974
Ben Cahill77626352007-11-29 11:09:44 +0800975 /* Update frame history window with "success" if Tx got ACKed ... */
Johannes Berge039fa42008-05-15 12:55:29 +0200976 status = !!(info->flags & IEEE80211_TX_STAT_ACK);
Zhu Yib481de92007-09-25 17:54:57 -0700977
Ben Cahill77626352007-11-29 11:09:44 +0800978 /* If type matches "search" table,
979 * add final tx status to "search" history */
Zhu Yib481de92007-09-25 17:54:57 -0700980 if ((tbl_type.lq_type == search_tbl->lq_type) &&
Guy Cohenfde0db32008-04-21 15:42:01 -0700981 (tbl_type.ant_type == search_tbl->ant_type) &&
Zhu Yib481de92007-09-25 17:54:57 -0700982 (tbl_type.is_SGI == search_tbl->is_SGI)) {
983 if (search_tbl->expected_tpt)
984 tpt = search_tbl->expected_tpt[rs_index];
985 else
986 tpt = 0;
Wey-Yi Guydf36c042009-03-10 14:35:11 -0700987 if (info->flags & IEEE80211_TX_STAT_AMPDU)
Ron Rindjunsky99556432008-01-28 14:07:25 +0200988 rs_collect_tx_data(search_win, rs_index, tpt,
Johannes Berge039fa42008-05-15 12:55:29 +0200989 info->status.ampdu_ack_len,
990 info->status.ampdu_ack_map);
Ron Rindjunsky99556432008-01-28 14:07:25 +0200991 else
992 rs_collect_tx_data(search_win, rs_index, tpt,
993 1, status);
Ben Cahill77626352007-11-29 11:09:44 +0800994 /* Else if type matches "current/active" table,
995 * add final tx status to "current/active" history */
Zhu Yib481de92007-09-25 17:54:57 -0700996 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
Guy Cohenfde0db32008-04-21 15:42:01 -0700997 (tbl_type.ant_type == curr_tbl->ant_type) &&
Zhu Yib481de92007-09-25 17:54:57 -0700998 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
999 if (curr_tbl->expected_tpt)
1000 tpt = curr_tbl->expected_tpt[rs_index];
1001 else
1002 tpt = 0;
Wey-Yi Guydf36c042009-03-10 14:35:11 -07001003 if (info->flags & IEEE80211_TX_STAT_AMPDU)
Ron Rindjunsky99556432008-01-28 14:07:25 +02001004 rs_collect_tx_data(window, rs_index, tpt,
Johannes Berge039fa42008-05-15 12:55:29 +02001005 info->status.ampdu_ack_len,
1006 info->status.ampdu_ack_map);
Ron Rindjunsky99556432008-01-28 14:07:25 +02001007 else
1008 rs_collect_tx_data(window, rs_index, tpt,
1009 1, status);
Zhu Yib481de92007-09-25 17:54:57 -07001010 }
1011
Ben Cahill77626352007-11-29 11:09:44 +08001012 /* If not searching for new mode, increment success/failed counter
1013 * ... these help determine when to start searching again */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001014 if (lq_sta->stay_in_tbl) {
Wey-Yi Guydf36c042009-03-10 14:35:11 -07001015 if (info->flags & IEEE80211_TX_STAT_AMPDU) {
Johannes Berge039fa42008-05-15 12:55:29 +02001016 lq_sta->total_success += info->status.ampdu_ack_map;
Ron Rindjunsky99556432008-01-28 14:07:25 +02001017 lq_sta->total_failed +=
Johannes Berge039fa42008-05-15 12:55:29 +02001018 (info->status.ampdu_ack_len - info->status.ampdu_ack_map);
Ron Rindjunsky99556432008-01-28 14:07:25 +02001019 } else {
1020 if (status)
1021 lq_sta->total_success++;
1022 else
1023 lq_sta->total_failed++;
1024 }
Zhu Yib481de92007-09-25 17:54:57 -07001025 }
1026
Ben Cahill77626352007-11-29 11:09:44 +08001027 /* See if there's a better rate or modulation mode to try. */
Abbas, Mohamedc338ba32009-01-21 10:58:02 -08001028 if (sta && sta->supp_rates[sband->band])
Gábor Stefanik514d65c2009-04-23 19:36:08 +02001029 rs_rate_scale_perform(priv, skb, sta, lq_sta);
Tomas Winklerf4d60822008-03-05 11:31:00 -08001030out:
Zhu Yib481de92007-09-25 17:54:57 -07001031 return;
1032}
1033
Ben Cahill77626352007-11-29 11:09:44 +08001034/*
1035 * Begin a period of staying with a selected modulation mode.
1036 * Set "stay_in_tbl" flag to prevent any mode switches.
1037 * Set frame tx success limits according to legacy vs. high-throughput,
1038 * and reset overall (spanning all rates) tx success history statistics.
1039 * These control how long we stay using same modulation mode before
1040 * searching for a new mode.
1041 */
Ester Kummerbf403db2008-05-05 10:22:40 +08001042static void rs_set_stay_in_table(struct iwl_priv *priv, u8 is_legacy,
Tomas Winklere227cea2008-07-18 13:53:05 +08001043 struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07001044{
Tomas Winklere1623442009-01-27 14:27:56 -08001045 IWL_DEBUG_RATE(priv, "we are staying in the same table\n");
Tomas Winklerc33104f2008-01-14 17:46:21 -08001046 lq_sta->stay_in_tbl = 1; /* only place this gets set */
Zhu Yib481de92007-09-25 17:54:57 -07001047 if (is_legacy) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001048 lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT;
1049 lq_sta->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
1050 lq_sta->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
Zhu Yib481de92007-09-25 17:54:57 -07001051 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001052 lq_sta->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
1053 lq_sta->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
1054 lq_sta->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
Zhu Yib481de92007-09-25 17:54:57 -07001055 }
Tomas Winklerc33104f2008-01-14 17:46:21 -08001056 lq_sta->table_count = 0;
1057 lq_sta->total_failed = 0;
1058 lq_sta->total_success = 0;
Mohamed Abbas447fee72009-04-20 14:37:02 -07001059 lq_sta->flush_timer = jiffies;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001060 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001061}
1062
Ben Cahill77626352007-11-29 11:09:44 +08001063/*
1064 * Find correct throughput table for given mode of modulation
1065 */
Tomas Winklere227cea2008-07-18 13:53:05 +08001066static void rs_set_expected_tpt_table(struct iwl_lq_sta *lq_sta,
1067 struct iwl_scale_tbl_info *tbl)
Zhu Yib481de92007-09-25 17:54:57 -07001068{
1069 if (is_legacy(tbl->lq_type)) {
1070 if (!is_a_band(tbl->lq_type))
1071 tbl->expected_tpt = expected_tpt_G;
1072 else
1073 tbl->expected_tpt = expected_tpt_A;
1074 } else if (is_siso(tbl->lq_type)) {
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001075 if (tbl->is_ht40 && !lq_sta->is_dup)
Zhu Yib481de92007-09-25 17:54:57 -07001076 if (tbl->is_SGI)
1077 tbl->expected_tpt = expected_tpt_siso40MHzSGI;
1078 else
1079 tbl->expected_tpt = expected_tpt_siso40MHz;
1080 else if (tbl->is_SGI)
1081 tbl->expected_tpt = expected_tpt_siso20MHzSGI;
1082 else
1083 tbl->expected_tpt = expected_tpt_siso20MHz;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001084 } else if (is_mimo2(tbl->lq_type)) {
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001085 if (tbl->is_ht40 && !lq_sta->is_dup)
Zhu Yib481de92007-09-25 17:54:57 -07001086 if (tbl->is_SGI)
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001087 tbl->expected_tpt = expected_tpt_mimo2_40MHzSGI;
Zhu Yib481de92007-09-25 17:54:57 -07001088 else
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001089 tbl->expected_tpt = expected_tpt_mimo2_40MHz;
Zhu Yib481de92007-09-25 17:54:57 -07001090 else if (tbl->is_SGI)
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001091 tbl->expected_tpt = expected_tpt_mimo2_20MHzSGI;
Zhu Yib481de92007-09-25 17:54:57 -07001092 else
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001093 tbl->expected_tpt = expected_tpt_mimo2_20MHz;
1094 } else if (is_mimo3(tbl->lq_type)) {
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001095 if (tbl->is_ht40 && !lq_sta->is_dup)
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001096 if (tbl->is_SGI)
1097 tbl->expected_tpt = expected_tpt_mimo3_40MHzSGI;
1098 else
1099 tbl->expected_tpt = expected_tpt_mimo3_40MHz;
1100 else if (tbl->is_SGI)
1101 tbl->expected_tpt = expected_tpt_mimo3_20MHzSGI;
1102 else
1103 tbl->expected_tpt = expected_tpt_mimo3_20MHz;
Zhu Yib481de92007-09-25 17:54:57 -07001104 } else
1105 tbl->expected_tpt = expected_tpt_G;
1106}
1107
Ben Cahill77626352007-11-29 11:09:44 +08001108/*
1109 * Find starting rate for new "search" high-throughput mode of modulation.
1110 * Goal is to find lowest expected rate (under perfect conditions) that is
1111 * above the current measured throughput of "active" mode, to give new mode
1112 * a fair chance to prove itself without too many challenges.
1113 *
1114 * This gets called when transitioning to more aggressive modulation
1115 * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
1116 * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
1117 * to decrease to match "active" throughput. When moving from MIMO to SISO,
1118 * bit rate will typically need to increase, but not if performance was bad.
1119 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001120static s32 rs_get_best_rate(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001121 struct iwl_lq_sta *lq_sta,
1122 struct iwl_scale_tbl_info *tbl, /* "search" */
Guy Cohenf935a6d2008-04-23 17:15:02 -07001123 u16 rate_mask, s8 index)
Zhu Yib481de92007-09-25 17:54:57 -07001124{
Ben Cahill77626352007-11-29 11:09:44 +08001125 /* "active" values */
Tomas Winklere227cea2008-07-18 13:53:05 +08001126 struct iwl_scale_tbl_info *active_tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001127 &(lq_sta->lq_info[lq_sta->active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07001128 s32 active_sr = active_tbl->win[index].success_ratio;
Zhu Yib481de92007-09-25 17:54:57 -07001129 s32 active_tpt = active_tbl->expected_tpt[index];
Ben Cahill77626352007-11-29 11:09:44 +08001130
1131 /* expected "search" throughput */
1132 s32 *tpt_tbl = tbl->expected_tpt;
1133
1134 s32 new_rate, high, low, start_hi;
Zhu Yib481de92007-09-25 17:54:57 -07001135 u16 high_low;
Guy Cohenf935a6d2008-04-23 17:15:02 -07001136 s8 rate = index;
Zhu Yib481de92007-09-25 17:54:57 -07001137
1138 new_rate = high = low = start_hi = IWL_RATE_INVALID;
1139
1140 for (; ;) {
Ester Kummerbf403db2008-05-05 10:22:40 +08001141 high_low = rs_get_adjacent_rate(priv, rate, rate_mask,
1142 tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -07001143
1144 low = high_low & 0xff;
1145 high = (high_low >> 8) & 0xff;
1146
Ben Cahill77626352007-11-29 11:09:44 +08001147 /*
1148 * Lower the "search" bit rate, to give new "search" mode
1149 * approximately the same throughput as "active" if:
1150 *
1151 * 1) "Active" mode has been working modestly well (but not
1152 * great), and expected "search" throughput (under perfect
1153 * conditions) at candidate rate is above the actual
1154 * measured "active" throughput (but less than expected
1155 * "active" throughput under perfect conditions).
1156 * OR
1157 * 2) "Active" mode has been working perfectly or very well
1158 * and expected "search" throughput (under perfect
1159 * conditions) at candidate rate is above expected
1160 * "active" throughput (under perfect conditions).
1161 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001162 if ((((100 * tpt_tbl[rate]) > lq_sta->last_tpt) &&
Zhu Yib481de92007-09-25 17:54:57 -07001163 ((active_sr > IWL_RATE_DECREASE_TH) &&
1164 (active_sr <= IWL_RATE_HIGH_TH) &&
1165 (tpt_tbl[rate] <= active_tpt))) ||
1166 ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
1167 (tpt_tbl[rate] > active_tpt))) {
1168
Ben Cahill77626352007-11-29 11:09:44 +08001169 /* (2nd or later pass)
1170 * If we've already tried to raise the rate, and are
1171 * now trying to lower it, use the higher rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001172 if (start_hi != IWL_RATE_INVALID) {
1173 new_rate = start_hi;
1174 break;
1175 }
Ben Cahill77626352007-11-29 11:09:44 +08001176
Zhu Yib481de92007-09-25 17:54:57 -07001177 new_rate = rate;
Ben Cahill77626352007-11-29 11:09:44 +08001178
1179 /* Loop again with lower rate */
Zhu Yib481de92007-09-25 17:54:57 -07001180 if (low != IWL_RATE_INVALID)
1181 rate = low;
Ben Cahill77626352007-11-29 11:09:44 +08001182
1183 /* Lower rate not available, use the original */
Zhu Yib481de92007-09-25 17:54:57 -07001184 else
1185 break;
Ben Cahill77626352007-11-29 11:09:44 +08001186
1187 /* Else try to raise the "search" rate to match "active" */
Zhu Yib481de92007-09-25 17:54:57 -07001188 } else {
Ben Cahill77626352007-11-29 11:09:44 +08001189 /* (2nd or later pass)
1190 * If we've already tried to lower the rate, and are
1191 * now trying to raise it, use the lower rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001192 if (new_rate != IWL_RATE_INVALID)
1193 break;
Ben Cahill77626352007-11-29 11:09:44 +08001194
1195 /* Loop again with higher rate */
Zhu Yib481de92007-09-25 17:54:57 -07001196 else if (high != IWL_RATE_INVALID) {
1197 start_hi = high;
1198 rate = high;
Ben Cahill77626352007-11-29 11:09:44 +08001199
1200 /* Higher rate not available, use the original */
Zhu Yib481de92007-09-25 17:54:57 -07001201 } else {
Guy Cohen90d77952008-09-09 10:54:53 +08001202 new_rate = rate;
Zhu Yib481de92007-09-25 17:54:57 -07001203 break;
1204 }
1205 }
1206 }
1207
1208 return new_rate;
1209}
Zhu Yib481de92007-09-25 17:54:57 -07001210
Ben Cahill77626352007-11-29 11:09:44 +08001211/*
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001212 * Set up search table for MIMO2
Ben Cahill77626352007-11-29 11:09:44 +08001213 */
Guy Cohenfde0db32008-04-21 15:42:01 -07001214static int rs_switch_to_mimo2(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001215 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001216 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001217 struct ieee80211_sta *sta,
Tomas Winklere227cea2008-07-18 13:53:05 +08001218 struct iwl_scale_tbl_info *tbl, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001219{
Zhu Yib481de92007-09-25 17:54:57 -07001220 u16 rate_mask;
1221 s32 rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001222 s8 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07001223
Luis R. Rodriguezde27e642008-12-23 15:58:44 -08001224 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
Zhu Yib481de92007-09-25 17:54:57 -07001225 return -1;
1226
Johannes Bergd9fe60d2008-10-09 12:13:49 +02001227 if (((sta->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 2)
Tomas Winkler00c5ae22008-09-03 11:26:42 +08001228 == WLAN_HT_CAP_SM_PS_STATIC)
Zhu Yib481de92007-09-25 17:54:57 -07001229 return -1;
1230
Ben Cahill77626352007-11-29 11:09:44 +08001231 /* Need both Tx chains/antennas to support MIMO */
Guy Cohenaade00c2008-04-23 17:14:59 -07001232 if (priv->hw_params.tx_chains_num < 2)
Zhu Yib481de92007-09-25 17:54:57 -07001233 return -1;
1234
Tomas Winklere1623442009-01-27 14:27:56 -08001235 IWL_DEBUG_RATE(priv, "LQ: try to switch to MIMO2\n");
Guy Cohen39e88502008-04-23 17:14:57 -07001236
1237 tbl->lq_type = LQ_MIMO2;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001238 tbl->is_dup = lq_sta->is_dup;
Zhu Yib481de92007-09-25 17:54:57 -07001239 tbl->action = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001240 tbl->max_search = IWL_MAX_SEARCH;
Guy Cohen39e88502008-04-23 17:14:57 -07001241 rate_mask = lq_sta->active_mimo2_rate;
1242
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001243 if (iwl_is_ht40_tx_allowed(priv, &sta->ht_cap))
1244 tbl->is_ht40 = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001245 else
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001246 tbl->is_ht40 = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001247
Guy Coheneecd6e52008-04-23 17:14:58 -07001248 rs_set_expected_tpt_table(lq_sta, tbl);
Zhu Yib481de92007-09-25 17:54:57 -07001249
Guy Cohenf935a6d2008-04-23 17:15:02 -07001250 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
Zhu Yib481de92007-09-25 17:54:57 -07001251
Tomas Winklere1623442009-01-27 14:27:56 -08001252 IWL_DEBUG_RATE(priv, "LQ: MIMO2 best rate %d mask %X\n", rate, rate_mask);
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001253 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1254 IWL_DEBUG_RATE(priv, "Can't switch with index %d rate mask %x\n",
1255 rate, rate_mask);
1256 return -1;
1257 }
1258 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
Guy Cohen39e88502008-04-23 17:14:57 -07001259
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001260 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
1261 tbl->current_rate, is_green);
1262 return 0;
1263}
1264
1265/*
1266 * Set up search table for MIMO3
1267 */
1268static int rs_switch_to_mimo3(struct iwl_priv *priv,
1269 struct iwl_lq_sta *lq_sta,
1270 struct ieee80211_conf *conf,
1271 struct ieee80211_sta *sta,
1272 struct iwl_scale_tbl_info *tbl, int index)
1273{
1274 u16 rate_mask;
1275 s32 rate;
1276 s8 is_green = lq_sta->is_green;
1277
1278 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
1279 return -1;
1280
1281 if (((sta->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 2)
1282 == WLAN_HT_CAP_SM_PS_STATIC)
1283 return -1;
1284
1285 /* Need both Tx chains/antennas to support MIMO */
1286 if (priv->hw_params.tx_chains_num < 3)
1287 return -1;
1288
1289 IWL_DEBUG_RATE(priv, "LQ: try to switch to MIMO3\n");
1290
1291 tbl->lq_type = LQ_MIMO3;
1292 tbl->is_dup = lq_sta->is_dup;
1293 tbl->action = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001294 tbl->max_search = IWL_MAX_11N_MIMO3_SEARCH;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001295 rate_mask = lq_sta->active_mimo3_rate;
1296
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001297 if (iwl_is_ht40_tx_allowed(priv, &sta->ht_cap))
1298 tbl->is_ht40 = 1;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001299 else
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001300 tbl->is_ht40 = 0;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001301
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001302 rs_set_expected_tpt_table(lq_sta, tbl);
1303
1304 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
1305
1306 IWL_DEBUG_RATE(priv, "LQ: MIMO3 best rate %d mask %X\n",
1307 rate, rate_mask);
Guy Cohen39e88502008-04-23 17:14:57 -07001308 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
Tomas Winklere1623442009-01-27 14:27:56 -08001309 IWL_DEBUG_RATE(priv, "Can't switch with index %d rate mask %x\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001310 rate, rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07001311 return -1;
Guy Cohen39e88502008-04-23 17:14:57 -07001312 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001313 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
Zhu Yib481de92007-09-25 17:54:57 -07001314
Tomas Winklere1623442009-01-27 14:27:56 -08001315 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001316 tbl->current_rate, is_green);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001317 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07001318}
1319
Ben Cahill77626352007-11-29 11:09:44 +08001320/*
1321 * Set up search table for SISO
1322 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001323static int rs_switch_to_siso(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001324 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001325 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001326 struct ieee80211_sta *sta,
Tomas Winklere227cea2008-07-18 13:53:05 +08001327 struct iwl_scale_tbl_info *tbl, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001328{
Zhu Yib481de92007-09-25 17:54:57 -07001329 u16 rate_mask;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001330 u8 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07001331 s32 rate;
1332
Luis R. Rodriguezde27e642008-12-23 15:58:44 -08001333 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
Zhu Yib481de92007-09-25 17:54:57 -07001334 return -1;
1335
Tomas Winklere1623442009-01-27 14:27:56 -08001336 IWL_DEBUG_RATE(priv, "LQ: try to switch to SISO\n");
Guy Cohen39e88502008-04-23 17:14:57 -07001337
Tomas Winklerc33104f2008-01-14 17:46:21 -08001338 tbl->is_dup = lq_sta->is_dup;
Zhu Yib481de92007-09-25 17:54:57 -07001339 tbl->lq_type = LQ_SISO;
1340 tbl->action = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001341 tbl->max_search = IWL_MAX_SEARCH;
Guy Cohen39e88502008-04-23 17:14:57 -07001342 rate_mask = lq_sta->active_siso_rate;
Zhu Yib481de92007-09-25 17:54:57 -07001343
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001344 if (iwl_is_ht40_tx_allowed(priv, &sta->ht_cap))
1345 tbl->is_ht40 = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001346 else
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001347 tbl->is_ht40 = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001348
Zhu Yib481de92007-09-25 17:54:57 -07001349 if (is_green)
Guy Cohen39e88502008-04-23 17:14:57 -07001350 tbl->is_SGI = 0; /*11n spec: no SGI in SISO+Greenfield*/
Zhu Yib481de92007-09-25 17:54:57 -07001351
Guy Coheneecd6e52008-04-23 17:14:58 -07001352 rs_set_expected_tpt_table(lq_sta, tbl);
Guy Cohenf935a6d2008-04-23 17:15:02 -07001353 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
Zhu Yib481de92007-09-25 17:54:57 -07001354
Tomas Winklere1623442009-01-27 14:27:56 -08001355 IWL_DEBUG_RATE(priv, "LQ: get best rate %d mask %X\n", rate, rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07001356 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
Tomas Winklere1623442009-01-27 14:27:56 -08001357 IWL_DEBUG_RATE(priv, "can not switch with index %d rate mask %x\n",
Zhu Yib481de92007-09-25 17:54:57 -07001358 rate, rate_mask);
1359 return -1;
1360 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001361 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
Tomas Winklere1623442009-01-27 14:27:56 -08001362 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001363 tbl->current_rate, is_green);
Mohamed Abbas403ab562007-11-06 22:06:25 -08001364 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07001365}
1366
Ben Cahill77626352007-11-29 11:09:44 +08001367/*
1368 * Try to switch to new modulation mode from legacy
1369 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001370static int rs_move_legacy_other(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001371 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001372 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001373 struct ieee80211_sta *sta,
Zhu Yib481de92007-09-25 17:54:57 -07001374 int index)
1375{
Tomas Winklere227cea2008-07-18 13:53:05 +08001376 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1377 struct iwl_scale_tbl_info *search_tbl =
1378 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1379 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1380 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1381 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001382 u8 start_action = tbl->action;
Guy Cohenfde0db32008-04-21 15:42:01 -07001383 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
Guy Cohen3110bef2008-09-09 10:54:54 +08001384 u8 tx_chains_num = priv->hw_params.tx_chains_num;
Guy Cohenfde0db32008-04-21 15:42:01 -07001385 int ret = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001386 u8 update_search_tbl_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001387
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001388 if (!iwl_ht_enabled(priv))
1389 /* stay in Legacy */
1390 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
Johannes Berg3ad3b922009-08-07 15:41:48 -07001391 else if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001392 tbl->action > IWL_LEGACY_SWITCH_SISO)
1393 tbl->action = IWL_LEGACY_SWITCH_SISO;
Zhu Yib481de92007-09-25 17:54:57 -07001394 for (; ;) {
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001395 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001396 switch (tbl->action) {
Guy Cohen3110bef2008-09-09 10:54:54 +08001397 case IWL_LEGACY_SWITCH_ANTENNA1:
1398 case IWL_LEGACY_SWITCH_ANTENNA2:
Tomas Winklere1623442009-01-27 14:27:56 -08001399 IWL_DEBUG_RATE(priv, "LQ: Legacy toggle Antenna\n");
Zhu Yib481de92007-09-25 17:54:57 -07001400
Guy Cohen3110bef2008-09-09 10:54:54 +08001401 if ((tbl->action == IWL_LEGACY_SWITCH_ANTENNA1 &&
1402 tx_chains_num <= 1) ||
1403 (tbl->action == IWL_LEGACY_SWITCH_ANTENNA2 &&
1404 tx_chains_num <= 2))
1405 break;
1406
Ben Cahill77626352007-11-29 11:09:44 +08001407 /* Don't change antenna if success has been great */
Zhu Yib481de92007-09-25 17:54:57 -07001408 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1409 break;
Ben Cahill77626352007-11-29 11:09:44 +08001410
Ben Cahill77626352007-11-29 11:09:44 +08001411 /* Set up search table to try other antenna */
Zhu Yib481de92007-09-25 17:54:57 -07001412 memcpy(search_tbl, tbl, sz);
1413
Guy Cohenaade00c2008-04-23 17:14:59 -07001414 if (rs_toggle_antenna(valid_tx_ant,
1415 &search_tbl->current_rate, search_tbl)) {
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001416 update_search_tbl_counter = 1;
Guy Cohen3110bef2008-09-09 10:54:54 +08001417 rs_set_expected_tpt_table(lq_sta, search_tbl);
Guy Cohenaade00c2008-04-23 17:14:59 -07001418 goto out;
1419 }
Guy Cohena5e8b502008-05-29 16:35:11 +08001420 break;
Zhu Yib481de92007-09-25 17:54:57 -07001421 case IWL_LEGACY_SWITCH_SISO:
Tomas Winklere1623442009-01-27 14:27:56 -08001422 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to SISO\n");
Ben Cahill77626352007-11-29 11:09:44 +08001423
1424 /* Set up search table to try SISO */
Zhu Yib481de92007-09-25 17:54:57 -07001425 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001426 search_tbl->is_SGI = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001427 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001428 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001429 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001430 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001431 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001432 }
Zhu Yib481de92007-09-25 17:54:57 -07001433
1434 break;
Guy Cohen3110bef2008-09-09 10:54:54 +08001435 case IWL_LEGACY_SWITCH_MIMO2_AB:
1436 case IWL_LEGACY_SWITCH_MIMO2_AC:
1437 case IWL_LEGACY_SWITCH_MIMO2_BC:
Tomas Winklere1623442009-01-27 14:27:56 -08001438 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to MIMO2\n");
Ben Cahill77626352007-11-29 11:09:44 +08001439
1440 /* Set up search table to try MIMO */
Zhu Yib481de92007-09-25 17:54:57 -07001441 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001442 search_tbl->is_SGI = 0;
Guy Cohen3110bef2008-09-09 10:54:54 +08001443
1444 if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AB)
1445 search_tbl->ant_type = ANT_AB;
1446 else if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AC)
1447 search_tbl->ant_type = ANT_AC;
1448 else
1449 search_tbl->ant_type = ANT_BC;
1450
1451 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1452 break;
1453
Guy Cohenfde0db32008-04-21 15:42:01 -07001454 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001455 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001456 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001457 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001458 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001459 }
Zhu Yib481de92007-09-25 17:54:57 -07001460 break;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001461
1462 case IWL_LEGACY_SWITCH_MIMO3_ABC:
1463 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to MIMO3\n");
1464
1465 /* Set up search table to try MIMO3 */
1466 memcpy(search_tbl, tbl, sz);
1467 search_tbl->is_SGI = 0;
1468
1469 search_tbl->ant_type = ANT_ABC;
1470
1471 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1472 break;
1473
1474 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1475 search_tbl, index);
1476 if (!ret) {
1477 lq_sta->action_counter = 0;
1478 goto out;
1479 }
1480 break;
Zhu Yib481de92007-09-25 17:54:57 -07001481 }
1482 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001483 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001484 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
Zhu Yib481de92007-09-25 17:54:57 -07001485
1486 if (tbl->action == start_action)
1487 break;
1488
1489 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001490 search_tbl->lq_type = LQ_NONE;
Zhu Yib481de92007-09-25 17:54:57 -07001491 return 0;
1492
Guy Cohen3110bef2008-09-09 10:54:54 +08001493out:
1494 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001495 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001496 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001497 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001498 if (update_search_tbl_counter)
1499 search_tbl->action = tbl->action;
Zhu Yib481de92007-09-25 17:54:57 -07001500 return 0;
1501
1502}
1503
Ben Cahill77626352007-11-29 11:09:44 +08001504/*
1505 * Try to switch to new modulation mode from SISO
1506 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001507static int rs_move_siso_to_other(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001508 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001509 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001510 struct ieee80211_sta *sta, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001511{
Tomas Winklerc33104f2008-01-14 17:46:21 -08001512 u8 is_green = lq_sta->is_green;
Tomas Winklere227cea2008-07-18 13:53:05 +08001513 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1514 struct iwl_scale_tbl_info *search_tbl =
1515 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1516 struct iwl_rate_scale_data *window = &(tbl->win[index]);
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001517 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Tomas Winklere227cea2008-07-18 13:53:05 +08001518 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1519 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001520 u8 start_action = tbl->action;
Guy Cohenfde0db32008-04-21 15:42:01 -07001521 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
Guy Cohen3110bef2008-09-09 10:54:54 +08001522 u8 tx_chains_num = priv->hw_params.tx_chains_num;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001523 u8 update_search_tbl_counter = 0;
Guy Cohenfde0db32008-04-21 15:42:01 -07001524 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001525
Johannes Berg3ad3b922009-08-07 15:41:48 -07001526 if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001527 tbl->action > IWL_SISO_SWITCH_ANTENNA2) {
1528 /* stay in SISO */
1529 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1530 }
Zhu Yib481de92007-09-25 17:54:57 -07001531 for (;;) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001532 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001533 switch (tbl->action) {
Guy Cohen3110bef2008-09-09 10:54:54 +08001534 case IWL_SISO_SWITCH_ANTENNA1:
1535 case IWL_SISO_SWITCH_ANTENNA2:
Tomas Winklere1623442009-01-27 14:27:56 -08001536 IWL_DEBUG_RATE(priv, "LQ: SISO toggle Antenna\n");
Guy Cohen3110bef2008-09-09 10:54:54 +08001537
1538 if ((tbl->action == IWL_SISO_SWITCH_ANTENNA1 &&
1539 tx_chains_num <= 1) ||
1540 (tbl->action == IWL_SISO_SWITCH_ANTENNA2 &&
1541 tx_chains_num <= 2))
1542 break;
1543
Zhu Yib481de92007-09-25 17:54:57 -07001544 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1545 break;
Zhu Yib481de92007-09-25 17:54:57 -07001546
1547 memcpy(search_tbl, tbl, sz);
Guy Cohenaade00c2008-04-23 17:14:59 -07001548 if (rs_toggle_antenna(valid_tx_ant,
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001549 &search_tbl->current_rate, search_tbl)) {
1550 update_search_tbl_counter = 1;
Guy Cohenaade00c2008-04-23 17:14:59 -07001551 goto out;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001552 }
Guy Cohena5e8b502008-05-29 16:35:11 +08001553 break;
Guy Cohen3110bef2008-09-09 10:54:54 +08001554 case IWL_SISO_SWITCH_MIMO2_AB:
1555 case IWL_SISO_SWITCH_MIMO2_AC:
1556 case IWL_SISO_SWITCH_MIMO2_BC:
Tomas Winklere1623442009-01-27 14:27:56 -08001557 IWL_DEBUG_RATE(priv, "LQ: SISO switch to MIMO2\n");
Zhu Yib481de92007-09-25 17:54:57 -07001558 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001559 search_tbl->is_SGI = 0;
Guy Cohen3110bef2008-09-09 10:54:54 +08001560
1561 if (tbl->action == IWL_SISO_SWITCH_MIMO2_AB)
1562 search_tbl->ant_type = ANT_AB;
1563 else if (tbl->action == IWL_SISO_SWITCH_MIMO2_AC)
1564 search_tbl->ant_type = ANT_AC;
1565 else
1566 search_tbl->ant_type = ANT_BC;
1567
1568 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1569 break;
1570
Guy Cohenfde0db32008-04-21 15:42:01 -07001571 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001572 search_tbl, index);
Guy Cohen3110bef2008-09-09 10:54:54 +08001573 if (!ret)
Zhu Yib481de92007-09-25 17:54:57 -07001574 goto out;
1575 break;
1576 case IWL_SISO_SWITCH_GI:
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001577 if (!tbl->is_ht40 && !(ht_cap->cap &
1578 IEEE80211_HT_CAP_SGI_20))
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001579 break;
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001580 if (tbl->is_ht40 && !(ht_cap->cap &
1581 IEEE80211_HT_CAP_SGI_40))
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001582 break;
1583
Tomas Winklere1623442009-01-27 14:27:56 -08001584 IWL_DEBUG_RATE(priv, "LQ: SISO toggle SGI/NGI\n");
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001585
Zhu Yib481de92007-09-25 17:54:57 -07001586 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001587 if (is_green) {
1588 if (!tbl->is_SGI)
1589 break;
1590 else
Winkler, Tomas15b16872008-12-19 10:37:33 +08001591 IWL_ERR(priv,
1592 "SGI was set in GF+SISO\n");
Zhu Yib481de92007-09-25 17:54:57 -07001593 }
Guy Cohen39e88502008-04-23 17:14:57 -07001594 search_tbl->is_SGI = !tbl->is_SGI;
Guy Coheneecd6e52008-04-23 17:14:58 -07001595 rs_set_expected_tpt_table(lq_sta, search_tbl);
Guy Cohen39e88502008-04-23 17:14:57 -07001596 if (tbl->is_SGI) {
1597 s32 tpt = lq_sta->last_tpt / 100;
1598 if (tpt >= search_tbl->expected_tpt[index])
1599 break;
1600 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001601 search_tbl->current_rate =
1602 rate_n_flags_from_tbl(priv, search_tbl,
1603 index, is_green);
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001604 update_search_tbl_counter = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001605 goto out;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001606 case IWL_SISO_SWITCH_MIMO3_ABC:
1607 IWL_DEBUG_RATE(priv, "LQ: SISO switch to MIMO3\n");
1608 memcpy(search_tbl, tbl, sz);
1609 search_tbl->is_SGI = 0;
1610 search_tbl->ant_type = ANT_ABC;
1611
1612 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1613 break;
1614
1615 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1616 search_tbl, index);
1617 if (!ret)
1618 goto out;
1619 break;
Zhu Yib481de92007-09-25 17:54:57 -07001620 }
1621 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001622 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001623 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
Zhu Yib481de92007-09-25 17:54:57 -07001624
1625 if (tbl->action == start_action)
1626 break;
1627 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001628 search_tbl->lq_type = LQ_NONE;
Zhu Yib481de92007-09-25 17:54:57 -07001629 return 0;
1630
1631 out:
Guy Cohen3110bef2008-09-09 10:54:54 +08001632 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001633 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001634 if (tbl->action > IWL_SISO_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001635 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001636 if (update_search_tbl_counter)
1637 search_tbl->action = tbl->action;
1638
Zhu Yib481de92007-09-25 17:54:57 -07001639 return 0;
1640}
1641
Ben Cahill77626352007-11-29 11:09:44 +08001642/*
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001643 * Try to switch to new modulation mode from MIMO2
Ben Cahill77626352007-11-29 11:09:44 +08001644 */
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001645static int rs_move_mimo2_to_other(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001646 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001647 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001648 struct ieee80211_sta *sta, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001649{
Tomas Winklerc33104f2008-01-14 17:46:21 -08001650 s8 is_green = lq_sta->is_green;
Tomas Winklere227cea2008-07-18 13:53:05 +08001651 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1652 struct iwl_scale_tbl_info *search_tbl =
1653 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Guy Cohen3110bef2008-09-09 10:54:54 +08001654 struct iwl_rate_scale_data *window = &(tbl->win[index]);
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001655 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Tomas Winklere227cea2008-07-18 13:53:05 +08001656 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1657 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001658 u8 start_action = tbl->action;
Guy Cohen3110bef2008-09-09 10:54:54 +08001659 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1660 u8 tx_chains_num = priv->hw_params.tx_chains_num;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001661 u8 update_search_tbl_counter = 0;
Guy Cohenaade00c2008-04-23 17:14:59 -07001662 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001663
Johannes Berg3ad3b922009-08-07 15:41:48 -07001664 if ((iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE) &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001665 (tbl->action < IWL_MIMO2_SWITCH_SISO_A ||
1666 tbl->action > IWL_MIMO2_SWITCH_SISO_C)) {
1667 /* switch in SISO */
1668 tbl->action = IWL_MIMO2_SWITCH_SISO_A;
1669 }
Zhu Yib481de92007-09-25 17:54:57 -07001670 for (;;) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001671 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001672 switch (tbl->action) {
Guy Cohen3110bef2008-09-09 10:54:54 +08001673 case IWL_MIMO2_SWITCH_ANTENNA1:
1674 case IWL_MIMO2_SWITCH_ANTENNA2:
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001675 IWL_DEBUG_RATE(priv, "LQ: MIMO2 toggle Antennas\n");
Guy Cohen3110bef2008-09-09 10:54:54 +08001676
1677 if (tx_chains_num <= 2)
1678 break;
1679
1680 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1681 break;
1682
1683 memcpy(search_tbl, tbl, sz);
1684 if (rs_toggle_antenna(valid_tx_ant,
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001685 &search_tbl->current_rate, search_tbl)) {
1686 update_search_tbl_counter = 1;
Guy Cohen3110bef2008-09-09 10:54:54 +08001687 goto out;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001688 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001689 break;
1690 case IWL_MIMO2_SWITCH_SISO_A:
1691 case IWL_MIMO2_SWITCH_SISO_B:
1692 case IWL_MIMO2_SWITCH_SISO_C:
Tomas Winklere1623442009-01-27 14:27:56 -08001693 IWL_DEBUG_RATE(priv, "LQ: MIMO2 switch to SISO\n");
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001694
Ben Cahill77626352007-11-29 11:09:44 +08001695 /* Set up new search table for SISO */
Zhu Yib481de92007-09-25 17:54:57 -07001696 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001697
Guy Cohen3110bef2008-09-09 10:54:54 +08001698 if (tbl->action == IWL_MIMO2_SWITCH_SISO_A)
Guy Cohenfde0db32008-04-21 15:42:01 -07001699 search_tbl->ant_type = ANT_A;
Guy Cohen3110bef2008-09-09 10:54:54 +08001700 else if (tbl->action == IWL_MIMO2_SWITCH_SISO_B)
Guy Cohenfde0db32008-04-21 15:42:01 -07001701 search_tbl->ant_type = ANT_B;
Guy Cohen3110bef2008-09-09 10:54:54 +08001702 else
1703 search_tbl->ant_type = ANT_C;
1704
1705 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1706 break;
Zhu Yib481de92007-09-25 17:54:57 -07001707
Tomas Winklerc33104f2008-01-14 17:46:21 -08001708 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001709 search_tbl, index);
Guy Cohen3110bef2008-09-09 10:54:54 +08001710 if (!ret)
Zhu Yib481de92007-09-25 17:54:57 -07001711 goto out;
Guy Cohen3110bef2008-09-09 10:54:54 +08001712
Zhu Yib481de92007-09-25 17:54:57 -07001713 break;
1714
Guy Cohen3110bef2008-09-09 10:54:54 +08001715 case IWL_MIMO2_SWITCH_GI:
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001716 if (!tbl->is_ht40 && !(ht_cap->cap &
1717 IEEE80211_HT_CAP_SGI_20))
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001718 break;
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001719 if (tbl->is_ht40 && !(ht_cap->cap &
1720 IEEE80211_HT_CAP_SGI_40))
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001721 break;
1722
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001723 IWL_DEBUG_RATE(priv, "LQ: MIMO2 toggle SGI/NGI\n");
1724
1725 /* Set up new search table for MIMO2 */
1726 memcpy(search_tbl, tbl, sz);
1727 search_tbl->is_SGI = !tbl->is_SGI;
1728 rs_set_expected_tpt_table(lq_sta, search_tbl);
1729 /*
1730 * If active table already uses the fastest possible
1731 * modulation (dual stream with short guard interval),
1732 * and it's working well, there's no need to look
1733 * for a better type of modulation!
1734 */
1735 if (tbl->is_SGI) {
1736 s32 tpt = lq_sta->last_tpt / 100;
1737 if (tpt >= search_tbl->expected_tpt[index])
1738 break;
1739 }
1740 search_tbl->current_rate =
1741 rate_n_flags_from_tbl(priv, search_tbl,
1742 index, is_green);
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001743 update_search_tbl_counter = 1;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001744 goto out;
1745
1746 case IWL_MIMO2_SWITCH_MIMO3_ABC:
1747 IWL_DEBUG_RATE(priv, "LQ: MIMO2 switch to MIMO3\n");
1748 memcpy(search_tbl, tbl, sz);
1749 search_tbl->is_SGI = 0;
1750 search_tbl->ant_type = ANT_ABC;
1751
1752 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1753 break;
1754
1755 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1756 search_tbl, index);
1757 if (!ret)
1758 goto out;
1759
1760 break;
1761 }
1762 tbl->action++;
1763 if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
1764 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
1765
1766 if (tbl->action == start_action)
1767 break;
1768 }
1769 search_tbl->lq_type = LQ_NONE;
1770 return 0;
1771 out:
1772 lq_sta->search_better_tbl = 1;
1773 tbl->action++;
1774 if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
1775 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001776 if (update_search_tbl_counter)
1777 search_tbl->action = tbl->action;
1778
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001779 return 0;
1780
1781}
1782
1783/*
1784 * Try to switch to new modulation mode from MIMO3
1785 */
1786static int rs_move_mimo3_to_other(struct iwl_priv *priv,
1787 struct iwl_lq_sta *lq_sta,
1788 struct ieee80211_conf *conf,
1789 struct ieee80211_sta *sta, int index)
1790{
1791 s8 is_green = lq_sta->is_green;
1792 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1793 struct iwl_scale_tbl_info *search_tbl =
1794 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1795 struct iwl_rate_scale_data *window = &(tbl->win[index]);
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001796 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001797 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1798 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1799 u8 start_action = tbl->action;
1800 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1801 u8 tx_chains_num = priv->hw_params.tx_chains_num;
1802 int ret;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001803 u8 update_search_tbl_counter = 0;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001804
Johannes Berg3ad3b922009-08-07 15:41:48 -07001805 if ((iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE) &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001806 (tbl->action < IWL_MIMO3_SWITCH_SISO_A ||
1807 tbl->action > IWL_MIMO3_SWITCH_SISO_C)) {
1808 /* switch in SISO */
1809 tbl->action = IWL_MIMO3_SWITCH_SISO_A;
1810 }
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001811 for (;;) {
1812 lq_sta->action_counter++;
1813 switch (tbl->action) {
1814 case IWL_MIMO3_SWITCH_ANTENNA1:
1815 case IWL_MIMO3_SWITCH_ANTENNA2:
1816 IWL_DEBUG_RATE(priv, "LQ: MIMO3 toggle Antennas\n");
1817
1818 if (tx_chains_num <= 3)
1819 break;
1820
1821 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1822 break;
1823
1824 memcpy(search_tbl, tbl, sz);
1825 if (rs_toggle_antenna(valid_tx_ant,
1826 &search_tbl->current_rate, search_tbl))
1827 goto out;
1828 break;
1829 case IWL_MIMO3_SWITCH_SISO_A:
1830 case IWL_MIMO3_SWITCH_SISO_B:
1831 case IWL_MIMO3_SWITCH_SISO_C:
1832 IWL_DEBUG_RATE(priv, "LQ: MIMO3 switch to SISO\n");
1833
1834 /* Set up new search table for SISO */
1835 memcpy(search_tbl, tbl, sz);
1836
1837 if (tbl->action == IWL_MIMO3_SWITCH_SISO_A)
1838 search_tbl->ant_type = ANT_A;
1839 else if (tbl->action == IWL_MIMO3_SWITCH_SISO_B)
1840 search_tbl->ant_type = ANT_B;
1841 else
1842 search_tbl->ant_type = ANT_C;
1843
1844 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1845 break;
1846
1847 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
1848 search_tbl, index);
1849 if (!ret)
1850 goto out;
1851
1852 break;
1853
1854 case IWL_MIMO3_SWITCH_MIMO2_AB:
1855 case IWL_MIMO3_SWITCH_MIMO2_AC:
1856 case IWL_MIMO3_SWITCH_MIMO2_BC:
1857 IWL_DEBUG_RATE(priv, "LQ: MIMO3 switch to MIMO2\n");
1858
1859 memcpy(search_tbl, tbl, sz);
1860 search_tbl->is_SGI = 0;
1861 if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AB)
1862 search_tbl->ant_type = ANT_AB;
1863 else if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AC)
1864 search_tbl->ant_type = ANT_AC;
1865 else
1866 search_tbl->ant_type = ANT_BC;
1867
1868 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1869 break;
1870
1871 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
1872 search_tbl, index);
1873 if (!ret)
1874 goto out;
1875
1876 break;
1877
1878 case IWL_MIMO3_SWITCH_GI:
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001879 if (!tbl->is_ht40 && !(ht_cap->cap &
1880 IEEE80211_HT_CAP_SGI_20))
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001881 break;
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001882 if (tbl->is_ht40 && !(ht_cap->cap &
1883 IEEE80211_HT_CAP_SGI_40))
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001884 break;
1885
1886 IWL_DEBUG_RATE(priv, "LQ: MIMO3 toggle SGI/NGI\n");
Ben Cahill77626352007-11-29 11:09:44 +08001887
1888 /* Set up new search table for MIMO */
Zhu Yib481de92007-09-25 17:54:57 -07001889 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001890 search_tbl->is_SGI = !tbl->is_SGI;
Guy Coheneecd6e52008-04-23 17:14:58 -07001891 rs_set_expected_tpt_table(lq_sta, search_tbl);
Ben Cahill77626352007-11-29 11:09:44 +08001892 /*
1893 * If active table already uses the fastest possible
1894 * modulation (dual stream with short guard interval),
1895 * and it's working well, there's no need to look
1896 * for a better type of modulation!
1897 */
Guy Cohen39e88502008-04-23 17:14:57 -07001898 if (tbl->is_SGI) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001899 s32 tpt = lq_sta->last_tpt / 100;
Guy Cohen39e88502008-04-23 17:14:57 -07001900 if (tpt >= search_tbl->expected_tpt[index])
1901 break;
Zhu Yib481de92007-09-25 17:54:57 -07001902 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001903 search_tbl->current_rate =
1904 rate_n_flags_from_tbl(priv, search_tbl,
1905 index, is_green);
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001906 update_search_tbl_counter = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001907 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07001908 }
1909 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001910 if (tbl->action > IWL_MIMO3_SWITCH_GI)
1911 tbl->action = IWL_MIMO3_SWITCH_ANTENNA1;
Zhu Yib481de92007-09-25 17:54:57 -07001912
1913 if (tbl->action == start_action)
1914 break;
1915 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001916 search_tbl->lq_type = LQ_NONE;
Zhu Yib481de92007-09-25 17:54:57 -07001917 return 0;
1918 out:
Guy Cohen3110bef2008-09-09 10:54:54 +08001919 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001920 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001921 if (tbl->action > IWL_MIMO3_SWITCH_GI)
1922 tbl->action = IWL_MIMO3_SWITCH_ANTENNA1;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001923 if (update_search_tbl_counter)
1924 search_tbl->action = tbl->action;
1925
Zhu Yib481de92007-09-25 17:54:57 -07001926 return 0;
1927
1928}
1929
Ben Cahill77626352007-11-29 11:09:44 +08001930/*
1931 * Check whether we should continue using same modulation mode, or
1932 * begin search for a new mode, based on:
1933 * 1) # tx successes or failures while using this mode
1934 * 2) # times calling this function
1935 * 3) elapsed time in this mode (not used, for now)
1936 */
Tomas Winklere227cea2008-07-18 13:53:05 +08001937static void rs_stay_in_table(struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07001938{
Tomas Winklere227cea2008-07-18 13:53:05 +08001939 struct iwl_scale_tbl_info *tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001940 int i;
1941 int active_tbl;
1942 int flush_interval_passed = 0;
Ester Kummerbf403db2008-05-05 10:22:40 +08001943 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07001944
Ester Kummerbf403db2008-05-05 10:22:40 +08001945 priv = lq_sta->drv;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001946 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001947
Tomas Winklerc33104f2008-01-14 17:46:21 -08001948 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07001949
Ben Cahill77626352007-11-29 11:09:44 +08001950 /* If we've been disallowing search, see if we should now allow it */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001951 if (lq_sta->stay_in_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07001952
Ben Cahill77626352007-11-29 11:09:44 +08001953 /* Elapsed time using current modulation mode */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001954 if (lq_sta->flush_timer)
Zhu Yib481de92007-09-25 17:54:57 -07001955 flush_interval_passed =
Mohamed Abbas447fee72009-04-20 14:37:02 -07001956 time_after(jiffies,
1957 (unsigned long)(lq_sta->flush_timer +
Zhu Yib481de92007-09-25 17:54:57 -07001958 IWL_RATE_SCALE_FLUSH_INTVL));
1959
Ben Cahill77626352007-11-29 11:09:44 +08001960 /*
1961 * Check if we should allow search for new modulation mode.
1962 * If many frames have failed or succeeded, or we've used
1963 * this same modulation for a long time, allow search, and
1964 * reset history stats that keep track of whether we should
1965 * allow a new search. Also (below) reset all bitmaps and
1966 * stats in active history.
1967 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001968 if ((lq_sta->total_failed > lq_sta->max_failure_limit) ||
1969 (lq_sta->total_success > lq_sta->max_success_limit) ||
1970 ((!lq_sta->search_better_tbl) && (lq_sta->flush_timer)
Zhu Yib481de92007-09-25 17:54:57 -07001971 && (flush_interval_passed))) {
Tomas Winklere1623442009-01-27 14:27:56 -08001972 IWL_DEBUG_RATE(priv, "LQ: stay is expired %d %d %d\n:",
Tomas Winklerc33104f2008-01-14 17:46:21 -08001973 lq_sta->total_failed,
1974 lq_sta->total_success,
Zhu Yib481de92007-09-25 17:54:57 -07001975 flush_interval_passed);
Ben Cahill77626352007-11-29 11:09:44 +08001976
1977 /* Allow search for new mode */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001978 lq_sta->stay_in_tbl = 0; /* only place reset */
1979 lq_sta->total_failed = 0;
1980 lq_sta->total_success = 0;
1981 lq_sta->flush_timer = 0;
Ben Cahill77626352007-11-29 11:09:44 +08001982
1983 /*
1984 * Else if we've used this modulation mode enough repetitions
1985 * (regardless of elapsed time or success/failure), reset
1986 * history bitmaps and rate-specific stats for all rates in
1987 * active table.
1988 */
Mohamed Abbas403ab562007-11-06 22:06:25 -08001989 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001990 lq_sta->table_count++;
1991 if (lq_sta->table_count >=
1992 lq_sta->table_count_limit) {
1993 lq_sta->table_count = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001994
Tomas Winklere1623442009-01-27 14:27:56 -08001995 IWL_DEBUG_RATE(priv, "LQ: stay in table clear win\n");
Zhu Yib481de92007-09-25 17:54:57 -07001996 for (i = 0; i < IWL_RATE_COUNT; i++)
1997 rs_rate_scale_clear_window(
1998 &(tbl->win[i]));
1999 }
2000 }
2001
Ben Cahill77626352007-11-29 11:09:44 +08002002 /* If transitioning to allow "search", reset all history
2003 * bitmaps and stats in active table (this will become the new
2004 * "search" table). */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002005 if (!lq_sta->stay_in_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07002006 for (i = 0; i < IWL_RATE_COUNT; i++)
2007 rs_rate_scale_clear_window(&(tbl->win[i]));
2008 }
2009 }
2010}
2011
Ben Cahill77626352007-11-29 11:09:44 +08002012/*
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07002013 * setup rate table in uCode
2014 * return rate_n_flags as used in the table
2015 */
2016static u32 rs_update_rate_tbl(struct iwl_priv *priv,
2017 struct iwl_lq_sta *lq_sta,
2018 struct iwl_scale_tbl_info *tbl,
2019 int index, u8 is_green)
2020{
2021 u32 rate;
2022
2023 /* Update uCode's rate table. */
2024 rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
2025 rs_fill_link_cmd(priv, lq_sta, rate);
2026 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
2027
2028 return rate;
2029}
2030
2031/*
Ben Cahill77626352007-11-29 11:09:44 +08002032 * Do rate scaling and search for new modulation mode.
2033 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002034static void rs_rate_scale_perform(struct iwl_priv *priv,
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002035 struct sk_buff *skb,
Johannes Berg4b7679a2008-09-18 18:14:18 +02002036 struct ieee80211_sta *sta,
2037 struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002038{
Johannes Berg4b7679a2008-09-18 18:14:18 +02002039 struct ieee80211_hw *hw = priv->hw;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002040 struct ieee80211_conf *conf = &hw->conf;
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002041 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2042 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002043 int low = IWL_RATE_INVALID;
2044 int high = IWL_RATE_INVALID;
2045 int index;
2046 int i;
Tomas Winklere227cea2008-07-18 13:53:05 +08002047 struct iwl_rate_scale_data *window = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002048 int current_tpt = IWL_INVALID_VALUE;
2049 int low_tpt = IWL_INVALID_VALUE;
2050 int high_tpt = IWL_INVALID_VALUE;
2051 u32 fail_count;
2052 s8 scale_action = 0;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002053 u16 rate_mask;
Zhu Yib481de92007-09-25 17:54:57 -07002054 u8 update_lq = 0;
Tomas Winklere227cea2008-07-18 13:53:05 +08002055 struct iwl_scale_tbl_info *tbl, *tbl1;
Zhu Yib481de92007-09-25 17:54:57 -07002056 u16 rate_scale_index_msk = 0;
Guy Cohen39e88502008-04-23 17:14:57 -07002057 u32 rate;
Zhu Yib481de92007-09-25 17:54:57 -07002058 u8 is_green = 0;
2059 u8 active_tbl = 0;
2060 u8 done_search = 0;
2061 u16 high_low;
Guy Cohena5e8b502008-05-29 16:35:11 +08002062 s32 sr;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002063 u8 tid = MAX_TID_COUNT;
Wey-Yi Guy86b47662009-03-10 14:35:09 -07002064 struct iwl_tid_data *tid_data;
Zhu Yib481de92007-09-25 17:54:57 -07002065
Tomas Winklere1623442009-01-27 14:27:56 -08002066 IWL_DEBUG_RATE(priv, "rate scale calculate new rate for skb\n");
Zhu Yib481de92007-09-25 17:54:57 -07002067
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002068 /* Send management frames and NO_ACK data using lowest rate. */
Tomas Winkler417f1142008-11-12 13:14:06 -08002069 /* TODO: this could probably be improved.. */
2070 if (!ieee80211_is_data(hdr->frame_control) ||
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002071 info->flags & IEEE80211_TX_CTL_NO_ACK)
Zhu Yib481de92007-09-25 17:54:57 -07002072 return;
Zhu Yib481de92007-09-25 17:54:57 -07002073
Johannes Berg4b7679a2008-09-18 18:14:18 +02002074 if (!sta || !lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002075 return;
2076
Johannes Berg4b7679a2008-09-18 18:14:18 +02002077 lq_sta->supp_rates = sta->supp_rates[lq_sta->band];
Zhu Yib481de92007-09-25 17:54:57 -07002078
Ron Rindjunskyfaa29712008-06-12 09:46:58 +08002079 tid = rs_tl_add_packet(lq_sta, hdr);
2080
Ben Cahill77626352007-11-29 11:09:44 +08002081 /*
2082 * Select rate-scale / modulation-mode table to work with in
2083 * the rest of this function: "search" if searching for better
2084 * modulation mode, or "active" if doing rate scaling within a mode.
2085 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002086 if (!lq_sta->search_better_tbl)
2087 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002088 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002089 active_tbl = 1 - lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002090
Tomas Winklerc33104f2008-01-14 17:46:21 -08002091 tbl = &(lq_sta->lq_info[active_tbl]);
Wey-Yi Guy2681b202009-05-21 13:44:22 -07002092 if (is_legacy(tbl->lq_type))
2093 lq_sta->is_green = 0;
2094 else
Daniel C Halperinb2617932009-08-13 13:30:59 -07002095 lq_sta->is_green = rs_use_green(sta, &priv->current_ht_config);
Tomas Winklerc33104f2008-01-14 17:46:21 -08002096 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07002097
Ben Cahill77626352007-11-29 11:09:44 +08002098 /* current tx rate */
Johannes Bergb7e35002008-09-11 02:22:58 +02002099 index = lq_sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002100
Tomas Winklere1623442009-01-27 14:27:56 -08002101 IWL_DEBUG_RATE(priv, "Rate scale index %d for type %d\n", index,
Zhu Yib481de92007-09-25 17:54:57 -07002102 tbl->lq_type);
2103
Ben Cahill77626352007-11-29 11:09:44 +08002104 /* rates available for this association, and for modulation mode */
Guy Coheneecd6e52008-04-23 17:14:58 -07002105 rate_mask = rs_get_supported_rates(lq_sta, hdr, tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -07002106
Tomas Winklere1623442009-01-27 14:27:56 -08002107 IWL_DEBUG_RATE(priv, "mask 0x%04X \n", rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07002108
2109 /* mask with station rate restriction */
2110 if (is_legacy(tbl->lq_type)) {
Johannes Berg8318d782008-01-24 19:38:38 +01002111 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Ben Cahill77626352007-11-29 11:09:44 +08002112 /* supp_rates has no CCK bits in A mode */
Zhu Yib481de92007-09-25 17:54:57 -07002113 rate_scale_index_msk = (u16) (rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -08002114 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
Zhu Yib481de92007-09-25 17:54:57 -07002115 else
2116 rate_scale_index_msk = (u16) (rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -08002117 lq_sta->supp_rates);
Zhu Yib481de92007-09-25 17:54:57 -07002118
2119 } else
2120 rate_scale_index_msk = rate_mask;
2121
2122 if (!rate_scale_index_msk)
2123 rate_scale_index_msk = rate_mask;
2124
Guy Cohen07bc28e2008-04-23 17:15:03 -07002125 if (!((1 << index) & rate_scale_index_msk)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002126 IWL_ERR(priv, "Current Rate is not valid\n");
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07002127 if (lq_sta->search_better_tbl) {
2128 /* revert to active table if search table is not valid*/
2129 tbl->lq_type = LQ_NONE;
2130 lq_sta->search_better_tbl = 0;
2131 tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
2132 /* get "active" rate info */
2133 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
2134 rate = rs_update_rate_tbl(priv, lq_sta,
2135 tbl, index, is_green);
2136 }
Guy Cohen07bc28e2008-04-23 17:15:03 -07002137 return;
Zhu Yib481de92007-09-25 17:54:57 -07002138 }
2139
Ben Cahill77626352007-11-29 11:09:44 +08002140 /* Get expected throughput table and history window for current rate */
Guy Cohen07bc28e2008-04-23 17:15:03 -07002141 if (!tbl->expected_tpt) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002142 IWL_ERR(priv, "tbl->expected_tpt is NULL\n");
Guy Cohen07bc28e2008-04-23 17:15:03 -07002143 return;
2144 }
Zhu Yib481de92007-09-25 17:54:57 -07002145
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002146 /* force user max rate if set by user */
2147 if ((lq_sta->max_rate_idx != -1) &&
2148 (lq_sta->max_rate_idx < index)) {
2149 index = lq_sta->max_rate_idx;
2150 update_lq = 1;
2151 window = &(tbl->win[index]);
2152 goto lq_update;
2153 }
2154
Zhu Yib481de92007-09-25 17:54:57 -07002155 window = &(tbl->win[index]);
2156
Ben Cahill77626352007-11-29 11:09:44 +08002157 /*
2158 * If there is not enough history to calculate actual average
2159 * throughput, keep analyzing results of more tx frames, without
2160 * changing rate or mode (bypass most of the rest of this function).
2161 * Set up new rate table in uCode only if old rate is not supported
2162 * in current association (use new rate found above).
2163 */
Zhu Yib481de92007-09-25 17:54:57 -07002164 fail_count = window->counter - window->success_counter;
Guy Cohen07bc28e2008-04-23 17:15:03 -07002165 if ((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
2166 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002167 IWL_DEBUG_RATE(priv, "LQ: still below TH. succ=%d total=%d "
Zhu Yib481de92007-09-25 17:54:57 -07002168 "for index %d\n",
2169 window->success_counter, window->counter, index);
Ben Cahill77626352007-11-29 11:09:44 +08002170
2171 /* Can't calculate this yet; not enough history */
Zhu Yib481de92007-09-25 17:54:57 -07002172 window->average_tpt = IWL_INVALID_VALUE;
Ben Cahill77626352007-11-29 11:09:44 +08002173
2174 /* Should we stay with this modulation mode,
2175 * or search for a new one? */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002176 rs_stay_in_table(lq_sta);
Ben Cahill77626352007-11-29 11:09:44 +08002177
Zhu Yib481de92007-09-25 17:54:57 -07002178 goto out;
Guy Cohen3110bef2008-09-09 10:54:54 +08002179 }
Zhu Yib481de92007-09-25 17:54:57 -07002180
Ben Cahill77626352007-11-29 11:09:44 +08002181 /* Else we have enough samples; calculate estimate of
2182 * actual average throughput */
Guy Cohen3110bef2008-09-09 10:54:54 +08002183
2184 BUG_ON(window->average_tpt != ((window->success_ratio *
2185 tbl->expected_tpt[index] + 64) / 128));
Zhu Yib481de92007-09-25 17:54:57 -07002186
Ben Cahill77626352007-11-29 11:09:44 +08002187 /* If we are searching for better modulation mode, check success. */
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002188 if (lq_sta->search_better_tbl &&
Johannes Berg3ad3b922009-08-07 15:41:48 -07002189 (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_MULTI)) {
Ben Cahill77626352007-11-29 11:09:44 +08002190 /* If good success, continue using the "search" mode;
2191 * no need to send new link quality command, since we're
2192 * continuing to use the setup that we've been trying. */
Guy Cohen07bc28e2008-04-23 17:15:03 -07002193 if (window->average_tpt > lq_sta->last_tpt) {
2194
Tomas Winklere1623442009-01-27 14:27:56 -08002195 IWL_DEBUG_RATE(priv, "LQ: SWITCHING TO NEW TABLE "
Guy Cohen07bc28e2008-04-23 17:15:03 -07002196 "suc=%d cur-tpt=%d old-tpt=%d\n",
2197 window->success_ratio,
2198 window->average_tpt,
2199 lq_sta->last_tpt);
2200
2201 if (!is_legacy(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08002202 lq_sta->enable_counter = 1;
Guy Cohen07bc28e2008-04-23 17:15:03 -07002203
Ben Cahill77626352007-11-29 11:09:44 +08002204 /* Swap tables; "search" becomes "active" */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002205 lq_sta->active_tbl = active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002206 current_tpt = window->average_tpt;
Ben Cahill77626352007-11-29 11:09:44 +08002207
2208 /* Else poor success; go back to mode in "active" table */
Zhu Yib481de92007-09-25 17:54:57 -07002209 } else {
Guy Cohen07bc28e2008-04-23 17:15:03 -07002210
Tomas Winklere1623442009-01-27 14:27:56 -08002211 IWL_DEBUG_RATE(priv, "LQ: GOING BACK TO THE OLD TABLE "
Guy Cohen07bc28e2008-04-23 17:15:03 -07002212 "suc=%d cur-tpt=%d old-tpt=%d\n",
2213 window->success_ratio,
2214 window->average_tpt,
2215 lq_sta->last_tpt);
2216
Ben Cahill77626352007-11-29 11:09:44 +08002217 /* Nullify "search" table */
Zhu Yib481de92007-09-25 17:54:57 -07002218 tbl->lq_type = LQ_NONE;
Ben Cahill77626352007-11-29 11:09:44 +08002219
2220 /* Revert to "active" table */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002221 active_tbl = lq_sta->active_tbl;
2222 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07002223
Ben Cahill77626352007-11-29 11:09:44 +08002224 /* Revert to "active" rate and throughput info */
Tomas Winklere7d326a2008-06-12 09:47:11 +08002225 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
Tomas Winklerc33104f2008-01-14 17:46:21 -08002226 current_tpt = lq_sta->last_tpt;
Ben Cahill77626352007-11-29 11:09:44 +08002227
2228 /* Need to set up a new rate table in uCode */
2229 update_lq = 1;
Zhu Yib481de92007-09-25 17:54:57 -07002230 }
Ben Cahill77626352007-11-29 11:09:44 +08002231
2232 /* Either way, we've made a decision; modulation mode
2233 * search is done, allow rate adjustment next time. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002234 lq_sta->search_better_tbl = 0;
Ben Cahill77626352007-11-29 11:09:44 +08002235 done_search = 1; /* Don't switch modes below! */
Zhu Yib481de92007-09-25 17:54:57 -07002236 goto lq_update;
2237 }
2238
Ben Cahill77626352007-11-29 11:09:44 +08002239 /* (Else) not in search of better modulation mode, try for better
2240 * starting rate, while staying in this mode. */
Ester Kummerbf403db2008-05-05 10:22:40 +08002241 high_low = rs_get_adjacent_rate(priv, index, rate_scale_index_msk,
Zhu Yib481de92007-09-25 17:54:57 -07002242 tbl->lq_type);
2243 low = high_low & 0xff;
2244 high = (high_low >> 8) & 0xff;
2245
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002246 /* If user set max rate, dont allow higher than user constrain */
2247 if ((lq_sta->max_rate_idx != -1) &&
2248 (lq_sta->max_rate_idx < high))
2249 high = IWL_RATE_INVALID;
2250
Guy Cohena5e8b502008-05-29 16:35:11 +08002251 sr = window->success_ratio;
2252
Ben Cahill77626352007-11-29 11:09:44 +08002253 /* Collect measured throughputs for current and adjacent rates */
Zhu Yib481de92007-09-25 17:54:57 -07002254 current_tpt = window->average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07002255 if (low != IWL_RATE_INVALID)
2256 low_tpt = tbl->win[low].average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07002257 if (high != IWL_RATE_INVALID)
2258 high_tpt = tbl->win[high].average_tpt;
2259
Guy Cohena5e8b502008-05-29 16:35:11 +08002260 scale_action = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002261
Ben Cahill77626352007-11-29 11:09:44 +08002262 /* Too many failures, decrease rate */
Guy Cohena5e8b502008-05-29 16:35:11 +08002263 if ((sr <= IWL_RATE_DECREASE_TH) || (current_tpt == 0)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002264 IWL_DEBUG_RATE(priv, "decrease rate because of low success_ratio\n");
Zhu Yib481de92007-09-25 17:54:57 -07002265 scale_action = -1;
Ben Cahill77626352007-11-29 11:09:44 +08002266
2267 /* No throughput measured yet for adjacent rates; try increase. */
Zhu Yib481de92007-09-25 17:54:57 -07002268 } else if ((low_tpt == IWL_INVALID_VALUE) &&
Guy Cohena5e8b502008-05-29 16:35:11 +08002269 (high_tpt == IWL_INVALID_VALUE)) {
2270
2271 if (high != IWL_RATE_INVALID && sr >= IWL_RATE_INCREASE_TH)
2272 scale_action = 1;
2273 else if (low != IWL_RATE_INVALID)
Wey-Yi Guy8fe72312009-03-10 14:35:10 -07002274 scale_action = 0;
Guy Cohena5e8b502008-05-29 16:35:11 +08002275 }
Ben Cahill77626352007-11-29 11:09:44 +08002276
2277 /* Both adjacent throughputs are measured, but neither one has better
2278 * throughput; we're using the best rate, don't change it! */
Zhu Yib481de92007-09-25 17:54:57 -07002279 else if ((low_tpt != IWL_INVALID_VALUE) &&
2280 (high_tpt != IWL_INVALID_VALUE) &&
2281 (low_tpt < current_tpt) &&
2282 (high_tpt < current_tpt))
2283 scale_action = 0;
Ben Cahill77626352007-11-29 11:09:44 +08002284
2285 /* At least one adjacent rate's throughput is measured,
2286 * and may have better performance. */
Zhu Yib481de92007-09-25 17:54:57 -07002287 else {
Ben Cahill77626352007-11-29 11:09:44 +08002288 /* Higher adjacent rate's throughput is measured */
Zhu Yib481de92007-09-25 17:54:57 -07002289 if (high_tpt != IWL_INVALID_VALUE) {
Ben Cahill77626352007-11-29 11:09:44 +08002290 /* Higher rate has better throughput */
Guy Cohena5e8b502008-05-29 16:35:11 +08002291 if (high_tpt > current_tpt &&
2292 sr >= IWL_RATE_INCREASE_TH) {
Zhu Yib481de92007-09-25 17:54:57 -07002293 scale_action = 1;
Guy Cohena5e8b502008-05-29 16:35:11 +08002294 } else {
Wey-Yi Guy8fe72312009-03-10 14:35:10 -07002295 scale_action = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002296 }
Ben Cahill77626352007-11-29 11:09:44 +08002297
2298 /* Lower adjacent rate's throughput is measured */
Zhu Yib481de92007-09-25 17:54:57 -07002299 } else if (low_tpt != IWL_INVALID_VALUE) {
Ben Cahill77626352007-11-29 11:09:44 +08002300 /* Lower rate has better throughput */
Zhu Yib481de92007-09-25 17:54:57 -07002301 if (low_tpt > current_tpt) {
Tomas Winklere1623442009-01-27 14:27:56 -08002302 IWL_DEBUG_RATE(priv,
2303 "decrease rate because of low tpt\n");
Zhu Yib481de92007-09-25 17:54:57 -07002304 scale_action = -1;
Guy Cohena5e8b502008-05-29 16:35:11 +08002305 } else if (sr >= IWL_RATE_INCREASE_TH) {
Zhu Yib481de92007-09-25 17:54:57 -07002306 scale_action = 1;
Guy Cohena5e8b502008-05-29 16:35:11 +08002307 }
Zhu Yib481de92007-09-25 17:54:57 -07002308 }
2309 }
2310
Ben Cahill77626352007-11-29 11:09:44 +08002311 /* Sanity check; asked for decrease, but success rate or throughput
2312 * has been good at old rate. Don't change it. */
Guy Cohena5e8b502008-05-29 16:35:11 +08002313 if ((scale_action == -1) && (low != IWL_RATE_INVALID) &&
2314 ((sr > IWL_RATE_HIGH_TH) ||
Zhu Yib481de92007-09-25 17:54:57 -07002315 (current_tpt > (100 * tbl->expected_tpt[low]))))
Zhu Yib481de92007-09-25 17:54:57 -07002316 scale_action = 0;
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002317 if (!iwl_ht_enabled(priv) && !is_legacy(tbl->lq_type))
2318 scale_action = -1;
Johannes Berg3ad3b922009-08-07 15:41:48 -07002319 if (iwl_tx_ant_restriction(priv) != IWL_ANT_OK_MULTI &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002320 (is_mimo2(tbl->lq_type) || is_mimo3(tbl->lq_type)))
2321 scale_action = -1;
Zhu Yib481de92007-09-25 17:54:57 -07002322 switch (scale_action) {
2323 case -1:
Ben Cahill77626352007-11-29 11:09:44 +08002324 /* Decrease starting rate, update uCode's rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002325 if (low != IWL_RATE_INVALID) {
2326 update_lq = 1;
2327 index = low;
2328 }
Mohamed Abbas447fee72009-04-20 14:37:02 -07002329
Zhu Yib481de92007-09-25 17:54:57 -07002330 break;
2331 case 1:
Ben Cahill77626352007-11-29 11:09:44 +08002332 /* Increase starting rate, update uCode's rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002333 if (high != IWL_RATE_INVALID) {
2334 update_lq = 1;
2335 index = high;
2336 }
2337
2338 break;
2339 case 0:
Ben Cahill77626352007-11-29 11:09:44 +08002340 /* No change */
Zhu Yib481de92007-09-25 17:54:57 -07002341 default:
2342 break;
2343 }
2344
Tomas Winklere1623442009-01-27 14:27:56 -08002345 IWL_DEBUG_RATE(priv, "choose rate scale index %d action %d low %d "
Zhu Yib481de92007-09-25 17:54:57 -07002346 "high %d type %d\n",
2347 index, scale_action, low, high, tbl->lq_type);
2348
Guy Cohena5e8b502008-05-29 16:35:11 +08002349lq_update:
Ben Cahill77626352007-11-29 11:09:44 +08002350 /* Replace uCode's rate table for the destination station. */
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07002351 if (update_lq)
2352 rate = rs_update_rate_tbl(priv, lq_sta,
2353 tbl, index, is_green);
Ben Cahill77626352007-11-29 11:09:44 +08002354
Johannes Berg3ad3b922009-08-07 15:41:48 -07002355 if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_MULTI) {
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002356 /* Should we stay with this modulation mode,
2357 * or search for a new one? */
2358 rs_stay_in_table(lq_sta);
2359 }
Ben Cahill77626352007-11-29 11:09:44 +08002360 /*
2361 * Search for new modulation mode if we're:
2362 * 1) Not changing rates right now
2363 * 2) Not just finishing up a search
2364 * 3) Allowing a new search
2365 */
Guy Cohen47cfd462008-05-27 11:29:34 +08002366 if (!update_lq && !done_search && !lq_sta->stay_in_tbl && window->counter) {
Ben Cahill77626352007-11-29 11:09:44 +08002367 /* Save current throughput to compare with "search" throughput*/
Tomas Winklerc33104f2008-01-14 17:46:21 -08002368 lq_sta->last_tpt = current_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07002369
Ben Cahill77626352007-11-29 11:09:44 +08002370 /* Select a new "search" modulation mode to try.
2371 * If one is found, set up the new "search" table. */
Zhu Yib481de92007-09-25 17:54:57 -07002372 if (is_legacy(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08002373 rs_move_legacy_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07002374 else if (is_siso(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08002375 rs_move_siso_to_other(priv, lq_sta, conf, sta, index);
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07002376 else if (is_mimo2(tbl->lq_type))
2377 rs_move_mimo2_to_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07002378 else
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07002379 rs_move_mimo3_to_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07002380
Ben Cahill77626352007-11-29 11:09:44 +08002381 /* If new "search" mode was selected, set up in uCode table */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002382 if (lq_sta->search_better_tbl) {
Ben Cahill77626352007-11-29 11:09:44 +08002383 /* Access the "search" table, clear its history. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002384 tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Zhu Yib481de92007-09-25 17:54:57 -07002385 for (i = 0; i < IWL_RATE_COUNT; i++)
2386 rs_rate_scale_clear_window(&(tbl->win[i]));
2387
Ben Cahill77626352007-11-29 11:09:44 +08002388 /* Use new "search" start rate */
Tomas Winklere7d326a2008-06-12 09:47:11 +08002389 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002390
Tomas Winklere1623442009-01-27 14:27:56 -08002391 IWL_DEBUG_RATE(priv, "Switch current mcs: %X index: %d\n",
Guy Cohen39e88502008-04-23 17:14:57 -07002392 tbl->current_rate, index);
Guy Cohen07bc28e2008-04-23 17:15:03 -07002393 rs_fill_link_cmd(priv, lq_sta, tbl->current_rate);
Tomas Winkler66c73db2008-04-15 16:01:40 -07002394 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
Mohamed Abbas447fee72009-04-20 14:37:02 -07002395 } else
2396 done_search = 1;
2397 }
Zhu Yib481de92007-09-25 17:54:57 -07002398
Mohamed Abbas447fee72009-04-20 14:37:02 -07002399 if (done_search && !lq_sta->stay_in_tbl) {
Ben Cahill77626352007-11-29 11:09:44 +08002400 /* If the "active" (non-search) mode was legacy,
2401 * and we've tried switching antennas,
2402 * but we haven't been able to try HT modes (not available),
2403 * stay with best antenna legacy modulation for a while
2404 * before next round of mode comparisons. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002405 tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
Luis R. Rodriguezde27e642008-12-23 15:58:44 -08002406 if (is_legacy(tbl1->lq_type) && !conf_is_ht(conf) &&
Mohamed Abbasd6e93392009-05-08 13:44:39 -07002407 lq_sta->action_counter > tbl1->max_search) {
Tomas Winklere1623442009-01-27 14:27:56 -08002408 IWL_DEBUG_RATE(priv, "LQ: STAY in legacy table\n");
Ester Kummerbf403db2008-05-05 10:22:40 +08002409 rs_set_stay_in_table(priv, 1, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002410 }
2411
Ben Cahill77626352007-11-29 11:09:44 +08002412 /* If we're in an HT mode, and all 3 mode switch actions
2413 * have been tried and compared, stay in this best modulation
2414 * mode for a while before next round of mode comparisons. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002415 if (lq_sta->enable_counter &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002416 (lq_sta->action_counter >= tbl1->max_search) &&
2417 iwl_ht_enabled(priv)) {
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002418 if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
2419 (lq_sta->tx_agg_tid_en & (1 << tid)) &&
2420 (tid != MAX_TID_COUNT)) {
Wey-Yi Guy86b47662009-03-10 14:35:09 -07002421 tid_data =
2422 &priv->stations[lq_sta->lq.sta_id].tid[tid];
2423 if (tid_data->agg.state == IWL_AGG_OFF) {
2424 IWL_DEBUG_RATE(priv,
2425 "try to aggregate tid %d\n",
2426 tid);
2427 rs_tl_turn_on_agg(priv, tid,
2428 lq_sta, sta);
2429 }
Zhu Yib481de92007-09-25 17:54:57 -07002430 }
Ester Kummerbf403db2008-05-05 10:22:40 +08002431 rs_set_stay_in_table(priv, 0, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002432 }
Zhu Yib481de92007-09-25 17:54:57 -07002433 }
2434
2435out:
Tomas Winkler978785a2008-12-19 10:37:31 +08002436 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
Zhu Yib481de92007-09-25 17:54:57 -07002437 i = index;
Johannes Bergb7e35002008-09-11 02:22:58 +02002438 lq_sta->last_txrate_idx = i;
Zhu Yib481de92007-09-25 17:54:57 -07002439
Zhu Yib481de92007-09-25 17:54:57 -07002440 return;
2441}
2442
2443
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002444static void rs_initialize_lq(struct iwl_priv *priv,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002445 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02002446 struct ieee80211_sta *sta,
2447 struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002448{
Tomas Winklere227cea2008-07-18 13:53:05 +08002449 struct iwl_scale_tbl_info *tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002450 int rate_idx;
Guy Cohenaade00c2008-04-23 17:14:59 -07002451 int i;
Guy Cohen39e88502008-04-23 17:14:57 -07002452 u32 rate;
Daniel C Halperinb2617932009-08-13 13:30:59 -07002453 u8 use_green = rs_use_green(sta, &priv->current_ht_config);
Guy Cohenaade00c2008-04-23 17:14:59 -07002454 u8 active_tbl = 0;
2455 u8 valid_tx_ant;
Zhu Yib481de92007-09-25 17:54:57 -07002456
Johannes Berg4b7679a2008-09-18 18:14:18 +02002457 if (!sta || !lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002458 goto out;
2459
Johannes Bergb7e35002008-09-11 02:22:58 +02002460 i = lq_sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002461
Tomas Winklerc33104f2008-01-14 17:46:21 -08002462 if ((lq_sta->lq.sta_id == 0xff) &&
Johannes Berg05c914f2008-09-11 00:01:58 +02002463 (priv->iw_mode == NL80211_IFTYPE_ADHOC))
Zhu Yib481de92007-09-25 17:54:57 -07002464 goto out;
2465
Guy Cohenaade00c2008-04-23 17:14:59 -07002466 valid_tx_ant = priv->hw_params.valid_tx_ant;
2467
Tomas Winklerc33104f2008-01-14 17:46:21 -08002468 if (!lq_sta->search_better_tbl)
2469 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002470 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002471 active_tbl = 1 - lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002472
Tomas Winklerc33104f2008-01-14 17:46:21 -08002473 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07002474
2475 if ((i < 0) || (i >= IWL_RATE_COUNT))
2476 i = 0;
2477
Tomas Winkler1826dcc2008-05-15 13:54:02 +08002478 rate = iwl_rates[i].plcp;
Winkler, Tomaseb48dca2008-10-29 14:05:48 -07002479 tbl->ant_type = first_antenna(valid_tx_ant);
2480 rate |= tbl->ant_type << RATE_MCS_ANT_POS;
Zhu Yib481de92007-09-25 17:54:57 -07002481
2482 if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
Guy Cohen39e88502008-04-23 17:14:57 -07002483 rate |= RATE_MCS_CCK_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002484
Guy Cohen39e88502008-04-23 17:14:57 -07002485 rs_get_tbl_info_from_mcs(rate, priv->band, tbl, &rate_idx);
Guy Cohenaade00c2008-04-23 17:14:59 -07002486 if (!rs_is_valid_ant(valid_tx_ant, tbl->ant_type))
2487 rs_toggle_antenna(valid_tx_ant, &rate, tbl);
Zhu Yib481de92007-09-25 17:54:57 -07002488
Tomas Winkler978785a2008-12-19 10:37:31 +08002489 rate = rate_n_flags_from_tbl(priv, tbl, rate_idx, use_green);
Guy Cohen39e88502008-04-23 17:14:57 -07002490 tbl->current_rate = rate;
Guy Coheneecd6e52008-04-23 17:14:58 -07002491 rs_set_expected_tpt_table(lq_sta, tbl);
Guy Cohen07bc28e2008-04-23 17:15:03 -07002492 rs_fill_link_cmd(NULL, lq_sta, rate);
Tomas Winkler66c73db2008-04-15 16:01:40 -07002493 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07002494 out:
2495 return;
2496}
2497
Johannes Berge6a98542008-10-21 12:40:02 +02002498static void rs_get_rate(void *priv_r, struct ieee80211_sta *sta, void *priv_sta,
2499 struct ieee80211_tx_rate_control *txrc)
Zhu Yib481de92007-09-25 17:54:57 -07002500{
2501
Johannes Berge6a98542008-10-21 12:40:02 +02002502 struct sk_buff *skb = txrc->skb;
2503 struct ieee80211_supported_band *sband = txrc->sband;
Johannes Berg4b7679a2008-09-18 18:14:18 +02002504 struct iwl_priv *priv = (struct iwl_priv *)priv_r;
2505 struct ieee80211_conf *conf = &priv->hw->conf;
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002506 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Zhu Yib481de92007-09-25 17:54:57 -07002507 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berge6a98542008-10-21 12:40:02 +02002508 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Tomas Winkler417f1142008-11-12 13:14:06 -08002509 struct iwl_lq_sta *lq_sta = priv_sta;
2510 int rate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002511
Tomas Winklere1623442009-01-27 14:27:56 -08002512 IWL_DEBUG_RATE_LIMIT(priv, "rate scale calculate new rate for skb\n");
Zhu Yib481de92007-09-25 17:54:57 -07002513
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002514 /* Get max rate if user set max rate */
2515 if (lq_sta) {
2516 lq_sta->max_rate_idx = txrc->max_rate_idx;
2517 if ((sband->band == IEEE80211_BAND_5GHZ) &&
2518 (lq_sta->max_rate_idx != -1))
2519 lq_sta->max_rate_idx += IWL_FIRST_OFDM_RATE;
2520 if ((lq_sta->max_rate_idx < 0) ||
2521 (lq_sta->max_rate_idx >= IWL_RATE_COUNT))
2522 lq_sta->max_rate_idx = -1;
2523 }
2524
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002525 /* Send management frames and NO_ACK data using lowest rate. */
Luis R. Rodriguez4c6d4f52009-07-16 10:05:41 -07002526 if (rate_control_send_low(sta, priv_sta, txrc))
Johannes Berg4b7679a2008-09-18 18:14:18 +02002527 return;
Zhu Yib481de92007-09-25 17:54:57 -07002528
Tomas Winkler417f1142008-11-12 13:14:06 -08002529 rate_idx = lq_sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002530
Johannes Berg05c914f2008-09-11 00:01:58 +02002531 if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) &&
Tomas Winklerc33104f2008-01-14 17:46:21 -08002532 !lq_sta->ibss_sta_added) {
Tomas Winklerc587de02009-06-03 11:44:07 -07002533 u8 sta_id = iwl_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002534
2535 if (sta_id == IWL_INVALID_STATION) {
Tomas Winklere1623442009-01-27 14:27:56 -08002536 IWL_DEBUG_RATE(priv, "LQ: ADD station %pM\n",
Johannes Berge1749612008-10-27 15:59:26 -07002537 hdr->addr1);
Tomas Winklerc587de02009-06-03 11:44:07 -07002538 sta_id = iwl_add_station(priv, hdr->addr1,
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002539 false, CMD_ASYNC, ht_cap);
Zhu Yib481de92007-09-25 17:54:57 -07002540 }
2541 if ((sta_id != IWL_INVALID_STATION)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002542 lq_sta->lq.sta_id = sta_id;
2543 lq_sta->lq.rs_table[0].rate_n_flags = 0;
2544 lq_sta->ibss_sta_added = 1;
Johannes Berg4b7679a2008-09-18 18:14:18 +02002545 rs_initialize_lq(priv, conf, sta, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002546 }
Zhu Yib481de92007-09-25 17:54:57 -07002547 }
2548
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002549 if (lq_sta->last_rate_n_flags & RATE_MCS_HT_MSK) {
Tomas Winkler417f1142008-11-12 13:14:06 -08002550 rate_idx -= IWL_FIRST_OFDM_RATE;
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002551 /* 6M and 9M shared same MCS index */
2552 rate_idx = (rate_idx > 0) ? (rate_idx - 1) : 0;
2553 if (rs_extract_rate(lq_sta->last_rate_n_flags) >=
2554 IWL_RATE_MIMO3_6M_PLCP)
2555 rate_idx = rate_idx + (2 * MCS_INDEX_PER_STREAM);
2556 else if (rs_extract_rate(lq_sta->last_rate_n_flags) >=
2557 IWL_RATE_MIMO2_6M_PLCP)
2558 rate_idx = rate_idx + MCS_INDEX_PER_STREAM;
2559 info->control.rates[0].flags = IEEE80211_TX_RC_MCS;
2560 if (lq_sta->last_rate_n_flags & RATE_MCS_SGI_MSK)
2561 info->control.rates[0].flags |= IEEE80211_TX_RC_SHORT_GI;
2562 if (lq_sta->last_rate_n_flags & RATE_MCS_DUP_MSK)
2563 info->control.rates[0].flags |= IEEE80211_TX_RC_DUP_DATA;
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07002564 if (lq_sta->last_rate_n_flags & RATE_MCS_HT40_MSK)
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002565 info->control.rates[0].flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
2566 if (lq_sta->last_rate_n_flags & RATE_MCS_GF_MSK)
2567 info->control.rates[0].flags |= IEEE80211_TX_RC_GREEN_FIELD;
2568 } else {
Daniel C Halperin50273092009-08-28 09:44:45 -07002569 /* Check for invalid rates */
2570 if ((rate_idx < 0) || (rate_idx >= IWL_RATE_COUNT_LEGACY) ||
2571 ((sband->band == IEEE80211_BAND_5GHZ) &&
2572 (rate_idx < IWL_FIRST_OFDM_RATE)))
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002573 rate_idx = rate_lowest_index(sband, sta);
Daniel C Halperin50273092009-08-28 09:44:45 -07002574 /* On valid 5 GHz rate, adjust index */
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002575 else if (sband->band == IEEE80211_BAND_5GHZ)
2576 rate_idx -= IWL_FIRST_OFDM_RATE;
Daniel C Halperin7ebaeff2009-08-21 13:34:20 -07002577 info->control.rates[0].flags = 0;
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002578 }
Tomas Winkler417f1142008-11-12 13:14:06 -08002579 info->control.rates[0].idx = rate_idx;
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002580
Zhu Yib481de92007-09-25 17:54:57 -07002581}
2582
Johannes Berg4b7679a2008-09-18 18:14:18 +02002583static void *rs_alloc_sta(void *priv_rate, struct ieee80211_sta *sta,
2584 gfp_t gfp)
Zhu Yib481de92007-09-25 17:54:57 -07002585{
Tomas Winklere227cea2008-07-18 13:53:05 +08002586 struct iwl_lq_sta *lq_sta;
Ester Kummerbf403db2008-05-05 10:22:40 +08002587 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07002588 int i, j;
2589
Ester Kummerbf403db2008-05-05 10:22:40 +08002590 priv = (struct iwl_priv *)priv_rate;
Tomas Winklere1623442009-01-27 14:27:56 -08002591 IWL_DEBUG_RATE(priv, "create station rate scale window\n");
Zhu Yib481de92007-09-25 17:54:57 -07002592
Tomas Winklere227cea2008-07-18 13:53:05 +08002593 lq_sta = kzalloc(sizeof(struct iwl_lq_sta), gfp);
Zhu Yib481de92007-09-25 17:54:57 -07002594
Tomas Winklerc33104f2008-01-14 17:46:21 -08002595 if (lq_sta == NULL)
Zhu Yib481de92007-09-25 17:54:57 -07002596 return NULL;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002597 lq_sta->lq.sta_id = 0xff;
Zhu Yib481de92007-09-25 17:54:57 -07002598
Zhu Yi63fddb92007-09-27 11:27:36 +08002599
Zhu Yib481de92007-09-25 17:54:57 -07002600 for (j = 0; j < LQ_SIZE; j++)
2601 for (i = 0; i < IWL_RATE_COUNT; i++)
Tomas Winkler3ac7f142008-07-21 02:40:14 +03002602 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
Zhu Yib481de92007-09-25 17:54:57 -07002603
Tomas Winklerc33104f2008-01-14 17:46:21 -08002604 return lq_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002605}
2606
Johannes Berg4b7679a2008-09-18 18:14:18 +02002607static void rs_rate_init(void *priv_r, struct ieee80211_supported_band *sband,
2608 struct ieee80211_sta *sta, void *priv_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002609{
2610 int i, j;
Johannes Berg4b7679a2008-09-18 18:14:18 +02002611 struct iwl_priv *priv = (struct iwl_priv *)priv_r;
2612 struct ieee80211_conf *conf = &priv->hw->conf;
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002613 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Tomas Winklere227cea2008-07-18 13:53:05 +08002614 struct iwl_lq_sta *lq_sta = priv_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002615
Tomas Winklerc33104f2008-01-14 17:46:21 -08002616 lq_sta->flush_timer = 0;
Johannes Berg4b7679a2008-09-18 18:14:18 +02002617 lq_sta->supp_rates = sta->supp_rates[sband->band];
Zhu Yib481de92007-09-25 17:54:57 -07002618 for (j = 0; j < LQ_SIZE; j++)
2619 for (i = 0; i < IWL_RATE_COUNT; i++)
Tomas Winkler3ac7f142008-07-21 02:40:14 +03002620 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
Zhu Yib481de92007-09-25 17:54:57 -07002621
Tomas Winklere1623442009-01-27 14:27:56 -08002622 IWL_DEBUG_RATE(priv, "LQ: *** rate scale station global init ***\n");
Zhu Yib481de92007-09-25 17:54:57 -07002623 /* TODO: what is a good starting rate for STA? About middle? Maybe not
2624 * the lowest or the highest rate.. Could consider using RSSI from
2625 * previous packets? Need to have IEEE 802.1X auth succeed immediately
2626 * after assoc.. */
2627
Tomas Winklerc33104f2008-01-14 17:46:21 -08002628 lq_sta->ibss_sta_added = 0;
Johannes Berg05c914f2008-09-11 00:01:58 +02002629 if (priv->iw_mode == NL80211_IFTYPE_AP) {
Tomas Winklerc587de02009-06-03 11:44:07 -07002630 u8 sta_id = iwl_find_station(priv,
Abhijeet Kolekare11bc022009-04-08 11:26:41 -07002631 sta->addr);
Joe Perches0795af52007-10-03 17:59:30 -07002632
Zhu Yib481de92007-09-25 17:54:57 -07002633 /* for IBSS the call are from tasklet */
Tomas Winklere1623442009-01-27 14:27:56 -08002634 IWL_DEBUG_RATE(priv, "LQ: ADD station %pM\n", sta->addr);
Zhu Yib481de92007-09-25 17:54:57 -07002635
2636 if (sta_id == IWL_INVALID_STATION) {
Tomas Winklere1623442009-01-27 14:27:56 -08002637 IWL_DEBUG_RATE(priv, "LQ: ADD station %pM\n", sta->addr);
Tomas Winklerc587de02009-06-03 11:44:07 -07002638 sta_id = iwl_add_station(priv, sta->addr, false,
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002639 CMD_ASYNC, ht_cap);
Zhu Yib481de92007-09-25 17:54:57 -07002640 }
2641 if ((sta_id != IWL_INVALID_STATION)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002642 lq_sta->lq.sta_id = sta_id;
2643 lq_sta->lq.rs_table[0].rate_n_flags = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002644 }
2645 /* FIXME: this is w/a remove it later */
2646 priv->assoc_station_added = 1;
2647 }
2648
Tomas Winklerc33104f2008-01-14 17:46:21 -08002649 lq_sta->is_dup = 0;
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002650 lq_sta->max_rate_idx = -1;
Abbas, Mohamed7d049e52009-01-20 21:33:53 -08002651 lq_sta->missed_rate_counter = IWL_MISSED_RATE_MAX;
Daniel C Halperinb2617932009-08-13 13:30:59 -07002652 lq_sta->is_green = rs_use_green(sta, &priv->current_ht_config);
Guy Cohen39e88502008-04-23 17:14:57 -07002653 lq_sta->active_legacy_rate = priv->active_rate & ~(0x1000);
Tomas Winklerc33104f2008-01-14 17:46:21 -08002654 lq_sta->active_rate_basic = priv->active_rate_basic;
Johannes Berg8318d782008-01-24 19:38:38 +01002655 lq_sta->band = priv->band;
Ben Cahill77626352007-11-29 11:09:44 +08002656 /*
2657 * active_siso_rate mask includes 9 MBits (bit 5), and CCK (bits 0-3),
2658 * supp_rates[] does not; shift to convert format, force 9 MBits off.
2659 */
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002660 lq_sta->active_siso_rate = ht_cap->mcs.rx_mask[0] << 1;
2661 lq_sta->active_siso_rate |= ht_cap->mcs.rx_mask[0] & 0x1;
Guy Cohenfde0db32008-04-21 15:42:01 -07002662 lq_sta->active_siso_rate &= ~((u16)0x2);
2663 lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -07002664
Ben Cahill77626352007-11-29 11:09:44 +08002665 /* Same here */
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002666 lq_sta->active_mimo2_rate = ht_cap->mcs.rx_mask[1] << 1;
2667 lq_sta->active_mimo2_rate |= ht_cap->mcs.rx_mask[1] & 0x1;
Guy Cohenfde0db32008-04-21 15:42:01 -07002668 lq_sta->active_mimo2_rate &= ~((u16)0x2);
2669 lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE;
2670
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002671 lq_sta->active_mimo3_rate = ht_cap->mcs.rx_mask[2] << 1;
2672 lq_sta->active_mimo3_rate |= ht_cap->mcs.rx_mask[2] & 0x1;
Guy Cohenfde0db32008-04-21 15:42:01 -07002673 lq_sta->active_mimo3_rate &= ~((u16)0x2);
2674 lq_sta->active_mimo3_rate <<= IWL_FIRST_OFDM_RATE;
2675
Tomas Winklere1623442009-01-27 14:27:56 -08002676 IWL_DEBUG_RATE(priv, "SISO-RATE=%X MIMO2-RATE=%X MIMO3-RATE=%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002677 lq_sta->active_siso_rate,
Guy Cohenfde0db32008-04-21 15:42:01 -07002678 lq_sta->active_mimo2_rate,
2679 lq_sta->active_mimo3_rate);
2680
Tomas Winklera96a27f2008-10-23 23:48:56 -07002681 /* These values will be overridden later */
Guy Cohen07bc28e2008-04-23 17:15:03 -07002682 lq_sta->lq.general_params.single_stream_ant_msk = ANT_A;
2683 lq_sta->lq.general_params.dual_stream_ant_msk = ANT_AB;
2684
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002685 /* as default allow aggregation for all tids */
2686 lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002687 lq_sta->drv = priv;
Zhu Yib481de92007-09-25 17:54:57 -07002688
Daniel C Halperin50273092009-08-28 09:44:45 -07002689 /* Set last_txrate_idx to lowest rate */
2690 lq_sta->last_txrate_idx = rate_lowest_index(sband, sta);
2691 if (sband->band == IEEE80211_BAND_5GHZ)
2692 lq_sta->last_txrate_idx += IWL_FIRST_OFDM_RATE;
Mohamed Abbasd5e49032008-12-12 08:22:15 -08002693
Johannes Berg4b7679a2008-09-18 18:14:18 +02002694 rs_initialize_lq(priv, conf, sta, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002695}
2696
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002697static void rs_fill_link_cmd(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08002698 struct iwl_lq_sta *lq_sta, u32 new_rate)
Zhu Yib481de92007-09-25 17:54:57 -07002699{
Tomas Winklere227cea2008-07-18 13:53:05 +08002700 struct iwl_scale_tbl_info tbl_type;
Zhu Yib481de92007-09-25 17:54:57 -07002701 int index = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002702 int rate_idx;
Zhu Yi1b696de2007-09-27 11:27:41 +08002703 int repeat_rate = 0;
Guy Cohenaade00c2008-04-23 17:14:59 -07002704 u8 ant_toggle_cnt = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002705 u8 use_ht_possible = 1;
Guy Cohenaade00c2008-04-23 17:14:59 -07002706 u8 valid_tx_ant = 0;
Guy Cohen07bc28e2008-04-23 17:15:03 -07002707 struct iwl_link_quality_cmd *lq_cmd = &lq_sta->lq;
Zhu Yib481de92007-09-25 17:54:57 -07002708
Ben Cahill77626352007-11-29 11:09:44 +08002709 /* Override starting rate (index 0) if needed for debug purposes */
Guy Cohen39e88502008-04-23 17:14:57 -07002710 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Zhu Yi98d7e092007-09-27 11:27:42 +08002711
Guy Cohen39e88502008-04-23 17:14:57 -07002712 /* Interpret new_rate (rate_n_flags) */
Guy Cohenaade00c2008-04-23 17:14:59 -07002713 memset(&tbl_type, 0, sizeof(tbl_type));
Guy Cohen39e88502008-04-23 17:14:57 -07002714 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band,
Zhu Yib481de92007-09-25 17:54:57 -07002715 &tbl_type, &rate_idx);
2716
Ben Cahill77626352007-11-29 11:09:44 +08002717 /* How many times should we repeat the initial rate? */
Zhu Yib481de92007-09-25 17:54:57 -07002718 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07002719 ant_toggle_cnt = 1;
Zhu Yi1b696de2007-09-27 11:27:41 +08002720 repeat_rate = IWL_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002721 } else {
Zhu Yi1b696de2007-09-27 11:27:41 +08002722 repeat_rate = IWL_HT_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002723 }
Zhu Yib481de92007-09-25 17:54:57 -07002724
2725 lq_cmd->general_params.mimo_delimiter =
2726 is_mimo(tbl_type.lq_type) ? 1 : 0;
Ben Cahill77626352007-11-29 11:09:44 +08002727
2728 /* Fill 1st table entry (index 0) */
Guy Cohen39e88502008-04-23 17:14:57 -07002729 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002730
Guy Cohen07bc28e2008-04-23 17:15:03 -07002731 if (num_of_ant(tbl_type.ant_type) == 1) {
2732 lq_cmd->general_params.single_stream_ant_msk =
2733 tbl_type.ant_type;
2734 } else if (num_of_ant(tbl_type.ant_type) == 2) {
2735 lq_cmd->general_params.dual_stream_ant_msk =
2736 tbl_type.ant_type;
2737 } /* otherwise we don't modify the existing value */
Zhu Yib481de92007-09-25 17:54:57 -07002738
2739 index++;
Zhu Yi1b696de2007-09-27 11:27:41 +08002740 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002741
Guy Cohenaade00c2008-04-23 17:14:59 -07002742 if (priv)
2743 valid_tx_ant = priv->hw_params.valid_tx_ant;
2744
Ben Cahill77626352007-11-29 11:09:44 +08002745 /* Fill rest of rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002746 while (index < LINK_QUAL_MAX_RETRY_NUM) {
Ben Cahill77626352007-11-29 11:09:44 +08002747 /* Repeat initial/next rate.
2748 * For legacy IWL_NUMBER_TRY == 1, this loop will not execute.
2749 * For HT IWL_HT_NUMBER_TRY == 3, this executes twice. */
Zhu Yi1b696de2007-09-27 11:27:41 +08002750 while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
Zhu Yib481de92007-09-25 17:54:57 -07002751 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07002752 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2753 ant_toggle_cnt++;
2754 else if (priv &&
2755 rs_toggle_antenna(valid_tx_ant,
2756 &new_rate, &tbl_type))
2757 ant_toggle_cnt = 1;
2758}
Zhu Yi98d7e092007-09-27 11:27:42 +08002759
Ben Cahill77626352007-11-29 11:09:44 +08002760 /* Override next rate if needed for debug purposes */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002761 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Ben Cahill77626352007-11-29 11:09:44 +08002762
2763 /* Fill next table entry */
Zhu Yib481de92007-09-25 17:54:57 -07002764 lq_cmd->rs_table[index].rate_n_flags =
Guy Cohen39e88502008-04-23 17:14:57 -07002765 cpu_to_le32(new_rate);
Zhu Yi1b696de2007-09-27 11:27:41 +08002766 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002767 index++;
2768 }
2769
Guy Cohen39e88502008-04-23 17:14:57 -07002770 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band, &tbl_type,
Zhu Yib481de92007-09-25 17:54:57 -07002771 &rate_idx);
2772
Ben Cahill77626352007-11-29 11:09:44 +08002773 /* Indicate to uCode which entries might be MIMO.
2774 * If initial rate was MIMO, this will finally end up
2775 * as (IWL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
Zhu Yib481de92007-09-25 17:54:57 -07002776 if (is_mimo(tbl_type.lq_type))
2777 lq_cmd->general_params.mimo_delimiter = index;
2778
Ben Cahill77626352007-11-29 11:09:44 +08002779 /* Get next rate */
Guy Cohen39e88502008-04-23 17:14:57 -07002780 new_rate = rs_get_lower_rate(lq_sta, &tbl_type, rate_idx,
2781 use_ht_possible);
Zhu Yib481de92007-09-25 17:54:57 -07002782
Ben Cahill77626352007-11-29 11:09:44 +08002783 /* How many times should we repeat the next rate? */
Zhu Yib481de92007-09-25 17:54:57 -07002784 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07002785 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2786 ant_toggle_cnt++;
2787 else if (priv &&
2788 rs_toggle_antenna(valid_tx_ant,
2789 &new_rate, &tbl_type))
2790 ant_toggle_cnt = 1;
2791
Zhu Yi1b696de2007-09-27 11:27:41 +08002792 repeat_rate = IWL_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002793 } else {
Zhu Yi1b696de2007-09-27 11:27:41 +08002794 repeat_rate = IWL_HT_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002795 }
Zhu Yib481de92007-09-25 17:54:57 -07002796
Ben Cahill77626352007-11-29 11:09:44 +08002797 /* Don't allow HT rates after next pass.
2798 * rs_get_lower_rate() will change type to LQ_A or LQ_G. */
Zhu Yib481de92007-09-25 17:54:57 -07002799 use_ht_possible = 0;
2800
Ben Cahill77626352007-11-29 11:09:44 +08002801 /* Override next rate if needed for debug purposes */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002802 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Ben Cahill77626352007-11-29 11:09:44 +08002803
2804 /* Fill next table entry */
Guy Cohen39e88502008-04-23 17:14:57 -07002805 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002806
2807 index++;
Zhu Yi1b696de2007-09-27 11:27:41 +08002808 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002809 }
2810
Wey-Yi Guy13c33a02009-06-03 11:44:11 -07002811 lq_cmd->agg_params.agg_frame_cnt_limit = LINK_QUAL_AGG_FRAME_LIMIT_MAX;
2812 lq_cmd->agg_params.agg_dis_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
2813 lq_cmd->agg_params.agg_time_limit =
2814 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
Zhu Yib481de92007-09-25 17:54:57 -07002815}
2816
Johannes Berg4b7679a2008-09-18 18:14:18 +02002817static void *rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
Zhu Yib481de92007-09-25 17:54:57 -07002818{
Johannes Berg4b7679a2008-09-18 18:14:18 +02002819 return hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07002820}
2821/* rate scale requires free function to be implemented */
2822static void rs_free(void *priv_rate)
2823{
2824 return;
2825}
2826
Johannes Berg4b7679a2008-09-18 18:14:18 +02002827static void rs_free_sta(void *priv_r, struct ieee80211_sta *sta,
2828 void *priv_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002829{
Tomas Winklere227cea2008-07-18 13:53:05 +08002830 struct iwl_lq_sta *lq_sta = priv_sta;
Rami Rosen45527c22008-10-07 09:50:01 +02002831 struct iwl_priv *priv __maybe_unused = priv_r;
Zhu Yib481de92007-09-25 17:54:57 -07002832
Tomas Winklere1623442009-01-27 14:27:56 -08002833 IWL_DEBUG_RATE(priv, "enter\n");
Tomas Winklerc33104f2008-01-14 17:46:21 -08002834 kfree(lq_sta);
Tomas Winklere1623442009-01-27 14:27:56 -08002835 IWL_DEBUG_RATE(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07002836}
2837
2838
Zhu Yi93dc6462007-09-27 11:27:37 +08002839#ifdef CONFIG_MAC80211_DEBUGFS
Zhu Yi5ae212c2007-09-27 11:27:38 +08002840static int open_file_generic(struct inode *inode, struct file *file)
2841{
2842 file->private_data = inode->i_private;
2843 return 0;
2844}
Tomas Winklere227cea2008-07-18 13:53:05 +08002845static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
2846 u32 *rate_n_flags, int index)
Zhu Yi98d7e092007-09-27 11:27:42 +08002847{
Ester Kummerbf403db2008-05-05 10:22:40 +08002848 struct iwl_priv *priv;
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07002849 u8 valid_tx_ant;
2850 u8 ant_sel_tx;
Ester Kummerbf403db2008-05-05 10:22:40 +08002851
2852 priv = lq_sta->drv;
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07002853 valid_tx_ant = priv->hw_params.valid_tx_ant;
Guy Cohen39e88502008-04-23 17:14:57 -07002854 if (lq_sta->dbg_fixed_rate) {
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07002855 ant_sel_tx =
2856 ((lq_sta->dbg_fixed_rate & RATE_MCS_ANT_ABC_MSK)
2857 >> RATE_MCS_ANT_POS);
2858 if ((valid_tx_ant & ant_sel_tx) == ant_sel_tx) {
Guy Cohen39e88502008-04-23 17:14:57 -07002859 *rate_n_flags = lq_sta->dbg_fixed_rate;
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07002860 IWL_DEBUG_RATE(priv, "Fixed rate ON\n");
Guy Cohen39e88502008-04-23 17:14:57 -07002861 } else {
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07002862 lq_sta->dbg_fixed_rate = 0;
2863 IWL_ERR(priv,
2864 "Invalid antenna selection 0x%X, Valid is 0x%X\n",
2865 ant_sel_tx, valid_tx_ant);
2866 IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
Guy Cohen39e88502008-04-23 17:14:57 -07002867 }
Guy Cohen39e88502008-04-23 17:14:57 -07002868 } else {
Tomas Winklere1623442009-01-27 14:27:56 -08002869 IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
Zhu Yi98d7e092007-09-27 11:27:42 +08002870 }
Zhu Yi98d7e092007-09-27 11:27:42 +08002871}
2872
2873static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
2874 const char __user *user_buf, size_t count, loff_t *ppos)
2875{
Tomas Winklere227cea2008-07-18 13:53:05 +08002876 struct iwl_lq_sta *lq_sta = file->private_data;
Ester Kummerbf403db2008-05-05 10:22:40 +08002877 struct iwl_priv *priv;
Zhu Yi98d7e092007-09-27 11:27:42 +08002878 char buf[64];
2879 int buf_size;
2880 u32 parsed_rate;
2881
Ester Kummerbf403db2008-05-05 10:22:40 +08002882 priv = lq_sta->drv;
Zhu Yi98d7e092007-09-27 11:27:42 +08002883 memset(buf, 0, sizeof(buf));
2884 buf_size = min(count, sizeof(buf) - 1);
2885 if (copy_from_user(buf, user_buf, buf_size))
2886 return -EFAULT;
2887
2888 if (sscanf(buf, "%x", &parsed_rate) == 1)
Guy Cohen39e88502008-04-23 17:14:57 -07002889 lq_sta->dbg_fixed_rate = parsed_rate;
Zhu Yi98d7e092007-09-27 11:27:42 +08002890 else
Guy Cohen39e88502008-04-23 17:14:57 -07002891 lq_sta->dbg_fixed_rate = 0;
Zhu Yi98d7e092007-09-27 11:27:42 +08002892
Guy Cohen39e88502008-04-23 17:14:57 -07002893 lq_sta->active_legacy_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
2894 lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2895 lq_sta->active_mimo2_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2896 lq_sta->active_mimo3_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
Zhu Yi98d7e092007-09-27 11:27:42 +08002897
Tomas Winklere1623442009-01-27 14:27:56 -08002898 IWL_DEBUG_RATE(priv, "sta_id %d rate 0x%X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07002899 lq_sta->lq.sta_id, lq_sta->dbg_fixed_rate);
Zhu Yi98d7e092007-09-27 11:27:42 +08002900
Guy Cohen39e88502008-04-23 17:14:57 -07002901 if (lq_sta->dbg_fixed_rate) {
Guy Cohen07bc28e2008-04-23 17:15:03 -07002902 rs_fill_link_cmd(NULL, lq_sta, lq_sta->dbg_fixed_rate);
Tomas Winkler66c73db2008-04-15 16:01:40 -07002903 iwl_send_lq_cmd(lq_sta->drv, &lq_sta->lq, CMD_ASYNC);
Zhu Yi98d7e092007-09-27 11:27:42 +08002904 }
2905
2906 return count;
2907}
Zhu Yi0209dc12007-09-27 11:27:43 +08002908
Zhu Yi5ae212c2007-09-27 11:27:38 +08002909static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
2910 char __user *user_buf, size_t count, loff_t *ppos)
2911{
Frank Seidela412c802009-03-01 20:25:38 +01002912 char *buff;
Zhu Yi5ae212c2007-09-27 11:27:38 +08002913 int desc = 0;
2914 int i = 0;
Wey-Yi Guy12b96812009-04-08 11:39:27 -07002915 int index = 0;
Frank Seidela412c802009-03-01 20:25:38 +01002916 ssize_t ret;
Zhu Yi5ae212c2007-09-27 11:27:38 +08002917
Tomas Winklere227cea2008-07-18 13:53:05 +08002918 struct iwl_lq_sta *lq_sta = file->private_data;
Wey-Yi Guyd8ae4f52009-03-10 14:35:07 -07002919 struct iwl_priv *priv;
Wey-Yi Guyadb7b5e2009-03-10 14:35:08 -07002920 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002921
Wey-Yi Guyd8ae4f52009-03-10 14:35:07 -07002922 priv = lq_sta->drv;
Frank Seidela412c802009-03-01 20:25:38 +01002923 buff = kmalloc(1024, GFP_KERNEL);
2924 if (!buff)
2925 return -ENOMEM;
2926
Tomas Winklerc33104f2008-01-14 17:46:21 -08002927 desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
Zhu Yi98d7e092007-09-27 11:27:42 +08002928 desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002929 lq_sta->total_failed, lq_sta->total_success,
Guy Cohen39e88502008-04-23 17:14:57 -07002930 lq_sta->active_legacy_rate);
Zhu Yi98d7e092007-09-27 11:27:42 +08002931 desc += sprintf(buff+desc, "fixed rate 0x%X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07002932 lq_sta->dbg_fixed_rate);
Wey-Yi Guyd8ae4f52009-03-10 14:35:07 -07002933 desc += sprintf(buff+desc, "valid_tx_ant %s%s%s\n",
2934 (priv->hw_params.valid_tx_ant & ANT_A) ? "ANT_A," : "",
2935 (priv->hw_params.valid_tx_ant & ANT_B) ? "ANT_B," : "",
2936 (priv->hw_params.valid_tx_ant & ANT_C) ? "ANT_C" : "");
Wey-Yi Guyadb7b5e2009-03-10 14:35:08 -07002937 desc += sprintf(buff+desc, "lq type %s\n",
2938 (is_legacy(tbl->lq_type)) ? "legacy" : "HT");
2939 if (is_Ht(tbl->lq_type)) {
2940 desc += sprintf(buff+desc, " %s",
2941 (is_siso(tbl->lq_type)) ? "SISO" :
2942 ((is_mimo2(tbl->lq_type)) ? "MIMO2" : "MIMO3"));
2943 desc += sprintf(buff+desc, " %s",
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07002944 (tbl->is_ht40) ? "40MHz" : "20MHz");
Wey-Yi Guy2681b202009-05-21 13:44:22 -07002945 desc += sprintf(buff+desc, " %s %s\n", (tbl->is_SGI) ? "SGI" : "",
2946 (lq_sta->is_green) ? "GF enabled" : "");
Wey-Yi Guyadb7b5e2009-03-10 14:35:08 -07002947 }
Wey-Yi Guy12b96812009-04-08 11:39:27 -07002948 desc += sprintf(buff+desc, "last tx rate=0x%X\n",
2949 lq_sta->last_rate_n_flags);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002950 desc += sprintf(buff+desc, "general:"
2951 "flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002952 lq_sta->lq.general_params.flags,
2953 lq_sta->lq.general_params.mimo_delimiter,
2954 lq_sta->lq.general_params.single_stream_ant_msk,
2955 lq_sta->lq.general_params.dual_stream_ant_msk);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002956
2957 desc += sprintf(buff+desc, "agg:"
2958 "time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002959 le16_to_cpu(lq_sta->lq.agg_params.agg_time_limit),
2960 lq_sta->lq.agg_params.agg_dis_start_th,
2961 lq_sta->lq.agg_params.agg_frame_cnt_limit);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002962
2963 desc += sprintf(buff+desc,
2964 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002965 lq_sta->lq.general_params.start_rate_index[0],
2966 lq_sta->lq.general_params.start_rate_index[1],
2967 lq_sta->lq.general_params.start_rate_index[2],
2968 lq_sta->lq.general_params.start_rate_index[3]);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002969
Wey-Yi Guy12b96812009-04-08 11:39:27 -07002970 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
2971 index = iwl_hwrate_to_plcp_idx(
2972 le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags));
2973 if (is_legacy(tbl->lq_type)) {
2974 desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps\n",
2975 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags),
2976 iwl_rate_mcs[index].mbps);
2977 } else {
2978 desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps (%s)\n",
2979 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags),
2980 iwl_rate_mcs[index].mbps, iwl_rate_mcs[index].mcs);
2981 }
2982 }
Zhu Yi5ae212c2007-09-27 11:27:38 +08002983
Frank Seidela412c802009-03-01 20:25:38 +01002984 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2985 kfree(buff);
2986 return ret;
Zhu Yi5ae212c2007-09-27 11:27:38 +08002987}
2988
2989static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
Zhu Yi98d7e092007-09-27 11:27:42 +08002990 .write = rs_sta_dbgfs_scale_table_write,
Zhu Yi5ae212c2007-09-27 11:27:38 +08002991 .read = rs_sta_dbgfs_scale_table_read,
2992 .open = open_file_generic,
2993};
Zhu Yi0209dc12007-09-27 11:27:43 +08002994static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
2995 char __user *user_buf, size_t count, loff_t *ppos)
2996{
Frank Seidela412c802009-03-01 20:25:38 +01002997 char *buff;
Zhu Yi0209dc12007-09-27 11:27:43 +08002998 int desc = 0;
2999 int i, j;
Frank Seidela412c802009-03-01 20:25:38 +01003000 ssize_t ret;
Zhu Yi0209dc12007-09-27 11:27:43 +08003001
Tomas Winklere227cea2008-07-18 13:53:05 +08003002 struct iwl_lq_sta *lq_sta = file->private_data;
Frank Seidela412c802009-03-01 20:25:38 +01003003
3004 buff = kmalloc(1024, GFP_KERNEL);
3005 if (!buff)
3006 return -ENOMEM;
3007
Zhu Yi0209dc12007-09-27 11:27:43 +08003008 for (i = 0; i < LQ_SIZE; i++) {
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07003009 desc += sprintf(buff+desc,
3010 "%s type=%d SGI=%d HT40=%d DUP=%d GF=%d\n"
Zhu Yi0209dc12007-09-27 11:27:43 +08003011 "rate=0x%X\n",
Abhijeet Kolekarc3056062008-11-12 13:14:08 -08003012 lq_sta->active_tbl == i ? "*" : "x",
Tomas Winklerc33104f2008-01-14 17:46:21 -08003013 lq_sta->lq_info[i].lq_type,
3014 lq_sta->lq_info[i].is_SGI,
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07003015 lq_sta->lq_info[i].is_ht40,
Tomas Winklerc33104f2008-01-14 17:46:21 -08003016 lq_sta->lq_info[i].is_dup,
Wey-Yi Guy2681b202009-05-21 13:44:22 -07003017 lq_sta->is_green,
Guy Cohen39e88502008-04-23 17:14:57 -07003018 lq_sta->lq_info[i].current_rate);
Zhu Yi0209dc12007-09-27 11:27:43 +08003019 for (j = 0; j < IWL_RATE_COUNT; j++) {
3020 desc += sprintf(buff+desc,
Tomas Winklerc33104f2008-01-14 17:46:21 -08003021 "counter=%d success=%d %%=%d\n",
3022 lq_sta->lq_info[i].win[j].counter,
3023 lq_sta->lq_info[i].win[j].success_counter,
3024 lq_sta->lq_info[i].win[j].success_ratio);
Zhu Yi0209dc12007-09-27 11:27:43 +08003025 }
3026 }
Frank Seidela412c802009-03-01 20:25:38 +01003027 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
3028 kfree(buff);
3029 return ret;
Zhu Yi0209dc12007-09-27 11:27:43 +08003030}
3031
3032static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
3033 .read = rs_sta_dbgfs_stats_table_read,
3034 .open = open_file_generic,
3035};
Zhu Yi5ae212c2007-09-27 11:27:38 +08003036
Wey-Yi Guy38167452009-05-08 13:44:43 -07003037static ssize_t rs_sta_dbgfs_rate_scale_data_read(struct file *file,
3038 char __user *user_buf, size_t count, loff_t *ppos)
3039{
3040 char buff[120];
3041 int desc = 0;
3042 ssize_t ret;
3043
3044 struct iwl_lq_sta *lq_sta = file->private_data;
3045 struct iwl_priv *priv;
3046 struct iwl_scale_tbl_info *tbl = &lq_sta->lq_info[lq_sta->active_tbl];
3047
3048 priv = lq_sta->drv;
3049
3050 if (is_Ht(tbl->lq_type))
3051 desc += sprintf(buff+desc,
3052 "Bit Rate= %d Mb/s\n",
3053 tbl->expected_tpt[lq_sta->last_txrate_idx]);
3054 else
3055 desc += sprintf(buff+desc,
3056 "Bit Rate= %d Mb/s\n",
3057 iwl_rates[lq_sta->last_txrate_idx].ieee >> 1);
3058 desc += sprintf(buff+desc,
3059 "Signal Level= %d dBm\tNoise Level= %d dBm\n",
3060 priv->last_rx_rssi, priv->last_rx_noise);
3061 desc += sprintf(buff+desc,
3062 "Tsf= 0x%llx\tBeacon time= 0x%08X\n",
3063 priv->last_tsf, priv->last_beacon_time);
3064
3065 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
3066 return ret;
3067}
3068
3069static const struct file_operations rs_sta_dbgfs_rate_scale_data_ops = {
3070 .read = rs_sta_dbgfs_rate_scale_data_read,
3071 .open = open_file_generic,
3072};
3073
Zhu Yi93dc6462007-09-27 11:27:37 +08003074static void rs_add_debugfs(void *priv, void *priv_sta,
3075 struct dentry *dir)
3076{
Tomas Winklere227cea2008-07-18 13:53:05 +08003077 struct iwl_lq_sta *lq_sta = priv_sta;
Tomas Winklerc33104f2008-01-14 17:46:21 -08003078 lq_sta->rs_sta_dbgfs_scale_table_file =
Zhu Yi0209dc12007-09-27 11:27:43 +08003079 debugfs_create_file("rate_scale_table", 0600, dir,
Tomas Winklerc33104f2008-01-14 17:46:21 -08003080 lq_sta, &rs_sta_dbgfs_scale_table_ops);
3081 lq_sta->rs_sta_dbgfs_stats_table_file =
Zhu Yi0209dc12007-09-27 11:27:43 +08003082 debugfs_create_file("rate_stats_table", 0600, dir,
Tomas Winklerc33104f2008-01-14 17:46:21 -08003083 lq_sta, &rs_sta_dbgfs_stats_table_ops);
Wey-Yi Guy38167452009-05-08 13:44:43 -07003084 lq_sta->rs_sta_dbgfs_rate_scale_data_file =
3085 debugfs_create_file("rate_scale_data", 0600, dir,
3086 lq_sta, &rs_sta_dbgfs_rate_scale_data_ops);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02003087 lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file =
3088 debugfs_create_u8("tx_agg_tid_enable", 0600, dir,
3089 &lq_sta->tx_agg_tid_en);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02003090
Zhu Yi93dc6462007-09-27 11:27:37 +08003091}
3092
3093static void rs_remove_debugfs(void *priv, void *priv_sta)
3094{
Tomas Winklere227cea2008-07-18 13:53:05 +08003095 struct iwl_lq_sta *lq_sta = priv_sta;
Tomas Winklerc33104f2008-01-14 17:46:21 -08003096 debugfs_remove(lq_sta->rs_sta_dbgfs_scale_table_file);
3097 debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
Wey-Yi Guy38167452009-05-08 13:44:43 -07003098 debugfs_remove(lq_sta->rs_sta_dbgfs_rate_scale_data_file);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02003099 debugfs_remove(lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file);
Zhu Yi93dc6462007-09-27 11:27:37 +08003100}
3101#endif
3102
Zhu Yib481de92007-09-25 17:54:57 -07003103static struct rate_control_ops rs_ops = {
3104 .module = NULL,
3105 .name = RS_NAME,
3106 .tx_status = rs_tx_status,
3107 .get_rate = rs_get_rate,
3108 .rate_init = rs_rate_init,
Zhu Yib481de92007-09-25 17:54:57 -07003109 .alloc = rs_alloc,
3110 .free = rs_free,
3111 .alloc_sta = rs_alloc_sta,
3112 .free_sta = rs_free_sta,
Zhu Yi93dc6462007-09-27 11:27:37 +08003113#ifdef CONFIG_MAC80211_DEBUGFS
3114 .add_sta_debugfs = rs_add_debugfs,
3115 .remove_sta_debugfs = rs_remove_debugfs,
3116#endif
Zhu Yib481de92007-09-25 17:54:57 -07003117};
3118
Tomas Winklere227cea2008-07-18 13:53:05 +08003119int iwlagn_rate_control_register(void)
Zhu Yib481de92007-09-25 17:54:57 -07003120{
Reinette Chatre897e1cf2008-03-28 16:21:09 -07003121 return ieee80211_rate_control_register(&rs_ops);
Zhu Yib481de92007-09-25 17:54:57 -07003122}
3123
Tomas Winklere227cea2008-07-18 13:53:05 +08003124void iwlagn_rate_control_unregister(void)
Zhu Yib481de92007-09-25 17:54:57 -07003125{
3126 ieee80211_rate_control_unregister(&rs_ops);
3127}
3128