blob: fd731534df1c3a89fd32ea4ea469d33439db5e86 [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);
421}
422
Guy Cohen39e88502008-04-23 17:14:57 -0700423static inline int get_num_of_ant_from_rate(u32 rate_n_flags)
Guy Cohenfde0db32008-04-21 15:42:01 -0700424{
Tomas Winkler3ac7f142008-07-21 02:40:14 +0300425 return !!(rate_n_flags & RATE_MCS_ANT_A_MSK) +
426 !!(rate_n_flags & RATE_MCS_ANT_B_MSK) +
427 !!(rate_n_flags & RATE_MCS_ANT_C_MSK);
Guy Cohenfde0db32008-04-21 15:42:01 -0700428}
429
Ben Cahill77626352007-11-29 11:09:44 +0800430/**
431 * rs_collect_tx_data - Update the success/failure sliding window
432 *
433 * We keep a sliding window of the last 62 packets transmitted
434 * at this rate. window->data contains the bitmask of successful
435 * packets.
436 */
Tomas Winklere227cea2008-07-18 13:53:05 +0800437static int rs_collect_tx_data(struct iwl_rate_scale_data *windows,
Ron Rindjunsky99556432008-01-28 14:07:25 +0200438 int scale_index, s32 tpt, int retries,
439 int successes)
Zhu Yib481de92007-09-25 17:54:57 -0700440{
Tomas Winklere227cea2008-07-18 13:53:05 +0800441 struct iwl_rate_scale_data *window = NULL;
Guy Cohen39e88502008-04-23 17:14:57 -0700442 static const u64 mask = (((u64)1) << (IWL_RATE_MAX_WINDOW - 1));
Zhu Yib481de92007-09-25 17:54:57 -0700443 s32 fail_count;
444
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800445 if (scale_index < 0 || scale_index >= IWL_RATE_COUNT)
446 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -0700447
Ben Cahill77626352007-11-29 11:09:44 +0800448 /* Select data for current tx bit rate */
Zhu Yib481de92007-09-25 17:54:57 -0700449 window = &(windows[scale_index]);
450
Ben Cahill77626352007-11-29 11:09:44 +0800451 /*
452 * Keep track of only the latest 62 tx frame attempts in this rate's
453 * history window; anything older isn't really relevant any more.
454 * If we have filled up the sliding window, drop the oldest attempt;
455 * if the oldest attempt (highest bit in bitmap) shows "success",
456 * subtract "1" from the success counter (this is the main reason
457 * we keep these bitmaps!).
458 */
Ron Rindjunsky99556432008-01-28 14:07:25 +0200459 while (retries > 0) {
Guy Cohen39e88502008-04-23 17:14:57 -0700460 if (window->counter >= IWL_RATE_MAX_WINDOW) {
461
462 /* remove earliest */
463 window->counter = IWL_RATE_MAX_WINDOW - 1;
464
Ron Rindjunsky99556432008-01-28 14:07:25 +0200465 if (window->data & mask) {
466 window->data &= ~mask;
Guy Cohen39e88502008-04-23 17:14:57 -0700467 window->success_counter--;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200468 }
Zhu Yib481de92007-09-25 17:54:57 -0700469 }
Zhu Yib481de92007-09-25 17:54:57 -0700470
Ron Rindjunsky99556432008-01-28 14:07:25 +0200471 /* Increment frames-attempted counter */
472 window->counter++;
Ben Cahill77626352007-11-29 11:09:44 +0800473
Ron Rindjunsky99556432008-01-28 14:07:25 +0200474 /* Shift bitmap by one frame (throw away oldest history),
475 * OR in "1", and increment "success" if this
476 * frame was successful. */
Guy Cohen90d77952008-09-09 10:54:53 +0800477 window->data <<= 1;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200478 if (successes > 0) {
Guy Cohen39e88502008-04-23 17:14:57 -0700479 window->success_counter++;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200480 window->data |= 0x1;
481 successes--;
482 }
483
484 retries--;
Zhu Yib481de92007-09-25 17:54:57 -0700485 }
486
Ben Cahill77626352007-11-29 11:09:44 +0800487 /* Calculate current success ratio, avoid divide-by-0! */
Zhu Yib481de92007-09-25 17:54:57 -0700488 if (window->counter > 0)
489 window->success_ratio = 128 * (100 * window->success_counter)
490 / window->counter;
491 else
492 window->success_ratio = IWL_INVALID_VALUE;
493
494 fail_count = window->counter - window->success_counter;
495
Ben Cahill77626352007-11-29 11:09:44 +0800496 /* Calculate average throughput, if we have enough history. */
Zhu Yib481de92007-09-25 17:54:57 -0700497 if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
498 (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
499 window->average_tpt = (window->success_ratio * tpt + 64) / 128;
500 else
501 window->average_tpt = IWL_INVALID_VALUE;
502
Ben Cahill77626352007-11-29 11:09:44 +0800503 /* Tag this window as having been updated */
Zhu Yib481de92007-09-25 17:54:57 -0700504 window->stamp = jiffies;
505
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800506 return 0;
Zhu Yib481de92007-09-25 17:54:57 -0700507}
508
Ben Cahill77626352007-11-29 11:09:44 +0800509/*
510 * Fill uCode API rate_n_flags field, based on "search" or "active" table.
511 */
Guy Cohen39e88502008-04-23 17:14:57 -0700512/* FIXME:RS:remove this function and put the flags statically in the table */
Tomas Winkler978785a2008-12-19 10:37:31 +0800513static u32 rate_n_flags_from_tbl(struct iwl_priv *priv,
514 struct iwl_scale_tbl_info *tbl,
515 int index, u8 use_green)
Zhu Yib481de92007-09-25 17:54:57 -0700516{
Guy Cohen39e88502008-04-23 17:14:57 -0700517 u32 rate_n_flags = 0;
518
Zhu Yib481de92007-09-25 17:54:57 -0700519 if (is_legacy(tbl->lq_type)) {
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800520 rate_n_flags = iwl_rates[index].plcp;
Zhu Yib481de92007-09-25 17:54:57 -0700521 if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
Guy Cohen39e88502008-04-23 17:14:57 -0700522 rate_n_flags |= RATE_MCS_CCK_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700523
Guy Cohenfde0db32008-04-21 15:42:01 -0700524 } else if (is_Ht(tbl->lq_type)) {
525 if (index > IWL_LAST_OFDM_RATE) {
Tomas Winkler978785a2008-12-19 10:37:31 +0800526 IWL_ERR(priv, "Invalid HT rate index %d\n", index);
Zhu Yib481de92007-09-25 17:54:57 -0700527 index = IWL_LAST_OFDM_RATE;
Guy Cohenfde0db32008-04-21 15:42:01 -0700528 }
Guy Cohen39e88502008-04-23 17:14:57 -0700529 rate_n_flags = RATE_MCS_HT_MSK;
Guy Cohenfde0db32008-04-21 15:42:01 -0700530
531 if (is_siso(tbl->lq_type))
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800532 rate_n_flags |= iwl_rates[index].plcp_siso;
Guy Cohenfde0db32008-04-21 15:42:01 -0700533 else if (is_mimo2(tbl->lq_type))
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800534 rate_n_flags |= iwl_rates[index].plcp_mimo2;
Guy Cohenfde0db32008-04-21 15:42:01 -0700535 else
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800536 rate_n_flags |= iwl_rates[index].plcp_mimo3;
Zhu Yib481de92007-09-25 17:54:57 -0700537 } else {
Tomas Winkler978785a2008-12-19 10:37:31 +0800538 IWL_ERR(priv, "Invalid tbl->lq_type %d\n", tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -0700539 }
540
Guy Cohen39e88502008-04-23 17:14:57 -0700541 rate_n_flags |= ((tbl->ant_type << RATE_MCS_ANT_POS) &
Guy Cohenfde0db32008-04-21 15:42:01 -0700542 RATE_MCS_ANT_ABC_MSK);
Zhu Yib481de92007-09-25 17:54:57 -0700543
Guy Cohen39e88502008-04-23 17:14:57 -0700544 if (is_Ht(tbl->lq_type)) {
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700545 if (tbl->is_ht40) {
Guy Cohen39e88502008-04-23 17:14:57 -0700546 if (tbl->is_dup)
547 rate_n_flags |= RATE_MCS_DUP_MSK;
548 else
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700549 rate_n_flags |= RATE_MCS_HT40_MSK;
Guy Cohen39e88502008-04-23 17:14:57 -0700550 }
551 if (tbl->is_SGI)
552 rate_n_flags |= RATE_MCS_SGI_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700553
Guy Cohen39e88502008-04-23 17:14:57 -0700554 if (use_green) {
555 rate_n_flags |= RATE_MCS_GF_MSK;
556 if (is_siso(tbl->lq_type) && tbl->is_SGI) {
557 rate_n_flags &= ~RATE_MCS_SGI_MSK;
Tomas Winkler978785a2008-12-19 10:37:31 +0800558 IWL_ERR(priv, "GF was set with SGI:SISO\n");
Guy Cohen39e88502008-04-23 17:14:57 -0700559 }
560 }
Zhu Yib481de92007-09-25 17:54:57 -0700561 }
Guy Cohen39e88502008-04-23 17:14:57 -0700562 return rate_n_flags;
Zhu Yib481de92007-09-25 17:54:57 -0700563}
564
Ben Cahill77626352007-11-29 11:09:44 +0800565/*
566 * Interpret uCode API's rate_n_flags format,
567 * fill "search" or "active" tx mode table.
568 */
Guy Cohen39e88502008-04-23 17:14:57 -0700569static int rs_get_tbl_info_from_mcs(const u32 rate_n_flags,
Johannes Berg8318d782008-01-24 19:38:38 +0100570 enum ieee80211_band band,
Tomas Winklere227cea2008-07-18 13:53:05 +0800571 struct iwl_scale_tbl_info *tbl,
Zhu Yib481de92007-09-25 17:54:57 -0700572 int *rate_idx)
573{
Guy Cohen39e88502008-04-23 17:14:57 -0700574 u32 ant_msk = (rate_n_flags & RATE_MCS_ANT_ABC_MSK);
575 u8 num_of_ant = get_num_of_ant_from_rate(rate_n_flags);
576 u8 mcs;
Zhu Yib481de92007-09-25 17:54:57 -0700577
Tomas Winklere7d326ac2008-06-12 09:47:11 +0800578 *rate_idx = iwl_hwrate_to_plcp_idx(rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -0700579
Guy Cohenfde0db32008-04-21 15:42:01 -0700580 if (*rate_idx == IWL_RATE_INVALID) {
Zhu Yib481de92007-09-25 17:54:57 -0700581 *rate_idx = -1;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800582 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -0700583 }
Ben Cahill77626352007-11-29 11:09:44 +0800584 tbl->is_SGI = 0; /* default legacy setup */
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700585 tbl->is_ht40 = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700586 tbl->is_dup = 0;
Guy Cohenfde0db32008-04-21 15:42:01 -0700587 tbl->ant_type = (ant_msk >> RATE_MCS_ANT_POS);
588 tbl->lq_type = LQ_NONE;
Mohamed Abbasd6e93392009-05-08 13:44:39 -0700589 tbl->max_search = IWL_MAX_SEARCH;
Zhu Yib481de92007-09-25 17:54:57 -0700590
Ben Cahill77626352007-11-29 11:09:44 +0800591 /* legacy rate format */
Guy Cohen39e88502008-04-23 17:14:57 -0700592 if (!(rate_n_flags & RATE_MCS_HT_MSK)) {
Guy Cohenfde0db32008-04-21 15:42:01 -0700593 if (num_of_ant == 1) {
Johannes Berg8318d782008-01-24 19:38:38 +0100594 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700595 tbl->lq_type = LQ_A;
596 else
597 tbl->lq_type = LQ_G;
Zhu Yib481de92007-09-25 17:54:57 -0700598 }
Guy Cohenfde0db32008-04-21 15:42:01 -0700599 /* HT rate format */
Zhu Yib481de92007-09-25 17:54:57 -0700600 } else {
Guy Cohen39e88502008-04-23 17:14:57 -0700601 if (rate_n_flags & RATE_MCS_SGI_MSK)
Zhu Yib481de92007-09-25 17:54:57 -0700602 tbl->is_SGI = 1;
603
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700604 if ((rate_n_flags & RATE_MCS_HT40_MSK) ||
Guy Cohen39e88502008-04-23 17:14:57 -0700605 (rate_n_flags & RATE_MCS_DUP_MSK))
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700606 tbl->is_ht40 = 1;
Zhu Yib481de92007-09-25 17:54:57 -0700607
Guy Cohen39e88502008-04-23 17:14:57 -0700608 if (rate_n_flags & RATE_MCS_DUP_MSK)
Zhu Yib481de92007-09-25 17:54:57 -0700609 tbl->is_dup = 1;
Guy Cohenfde0db32008-04-21 15:42:01 -0700610
Guy Cohen39e88502008-04-23 17:14:57 -0700611 mcs = rs_extract_rate(rate_n_flags);
Guy Cohenfde0db32008-04-21 15:42:01 -0700612
Guy Cohen39e88502008-04-23 17:14:57 -0700613 /* SISO */
614 if (mcs <= IWL_RATE_SISO_60M_PLCP) {
Guy Cohenfde0db32008-04-21 15:42:01 -0700615 if (num_of_ant == 1)
616 tbl->lq_type = LQ_SISO; /*else NONE*/
617 /* MIMO2 */
Guy Cohen39e88502008-04-23 17:14:57 -0700618 } else if (mcs <= IWL_RATE_MIMO2_60M_PLCP) {
Guy Cohenfde0db32008-04-21 15:42:01 -0700619 if (num_of_ant == 2)
620 tbl->lq_type = LQ_MIMO2;
621 /* MIMO3 */
622 } else {
Mohamed Abbasd6e93392009-05-08 13:44:39 -0700623 if (num_of_ant == 3) {
624 tbl->max_search = IWL_MAX_11N_MIMO3_SEARCH;
Guy Cohenfde0db32008-04-21 15:42:01 -0700625 tbl->lq_type = LQ_MIMO3;
Mohamed Abbasd6e93392009-05-08 13:44:39 -0700626 }
Guy Cohenfde0db32008-04-21 15:42:01 -0700627 }
Zhu Yib481de92007-09-25 17:54:57 -0700628 }
629 return 0;
630}
Guy Cohenaade00c2008-04-23 17:14:59 -0700631
632/* switch to another antenna/antennas and return 1 */
633/* if no other valid antenna found, return 0 */
634static int rs_toggle_antenna(u32 valid_ant, u32 *rate_n_flags,
Tomas Winklere227cea2008-07-18 13:53:05 +0800635 struct iwl_scale_tbl_info *tbl)
Zhu Yib481de92007-09-25 17:54:57 -0700636{
Guy Cohenaade00c2008-04-23 17:14:59 -0700637 u8 new_ant_type;
638
639 if (!tbl->ant_type || tbl->ant_type > ANT_ABC)
640 return 0;
641
642 if (!rs_is_valid_ant(valid_ant, tbl->ant_type))
643 return 0;
644
645 new_ant_type = ant_toggle_lookup[tbl->ant_type];
646
647 while ((new_ant_type != tbl->ant_type) &&
648 !rs_is_valid_ant(valid_ant, new_ant_type))
649 new_ant_type = ant_toggle_lookup[new_ant_type];
650
651 if (new_ant_type == tbl->ant_type)
652 return 0;
653
654 tbl->ant_type = new_ant_type;
655 *rate_n_flags &= ~RATE_MCS_ANT_ABC_MSK;
656 *rate_n_flags |= new_ant_type << RATE_MCS_ANT_POS;
657 return 1;
Zhu Yib481de92007-09-25 17:54:57 -0700658}
659
Daniel C Halperinb2617932009-08-13 13:30:59 -0700660/**
661 * Green-field mode is valid if the station supports it and
662 * there are no non-GF stations present in the BSS.
663 */
664static inline u8 rs_use_green(struct ieee80211_sta *sta,
665 struct iwl_ht_info *ht_conf)
Zhu Yib481de92007-09-25 17:54:57 -0700666{
Daniel C Halperinb2617932009-08-13 13:30:59 -0700667 return (sta->ht_cap.cap & IEEE80211_HT_CAP_GRN_FLD) &&
668 !(ht_conf->non_GF_STA_present);
Guy Cohenf935a6d2008-04-23 17:15:02 -0700669}
Zhu Yib481de92007-09-25 17:54:57 -0700670
671/**
672 * rs_get_supported_rates - get the available rates
673 *
674 * if management frame or broadcast frame only return
675 * basic available rates.
676 *
677 */
Tomas Winklere227cea2008-07-18 13:53:05 +0800678static u16 rs_get_supported_rates(struct iwl_lq_sta *lq_sta,
679 struct ieee80211_hdr *hdr,
680 enum iwl_table_type rate_type)
Zhu Yib481de92007-09-25 17:54:57 -0700681{
Zhu Yib481de92007-09-25 17:54:57 -0700682 if (hdr && is_multicast_ether_addr(hdr->addr1) &&
Guy Coheneecd6e52008-04-23 17:14:58 -0700683 lq_sta->active_rate_basic)
684 return lq_sta->active_rate_basic;
685
686 if (is_legacy(rate_type)) {
687 return lq_sta->active_legacy_rate;
688 } else {
689 if (is_siso(rate_type))
690 return lq_sta->active_siso_rate;
691 else if (is_mimo2(rate_type))
692 return lq_sta->active_mimo2_rate;
693 else
694 return lq_sta->active_mimo3_rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -0800695 }
Zhu Yib481de92007-09-25 17:54:57 -0700696}
697
Ester Kummerbf403db2008-05-05 10:22:40 +0800698static u16 rs_get_adjacent_rate(struct iwl_priv *priv, u8 index, u16 rate_mask,
699 int rate_type)
Zhu Yib481de92007-09-25 17:54:57 -0700700{
701 u8 high = IWL_RATE_INVALID;
702 u8 low = IWL_RATE_INVALID;
703
Ian Schram01ebd062007-10-25 17:15:22 +0800704 /* 802.11A or ht walks to the next literal adjacent rate in
Zhu Yib481de92007-09-25 17:54:57 -0700705 * the rate table */
706 if (is_a_band(rate_type) || !is_legacy(rate_type)) {
707 int i;
708 u32 mask;
709
710 /* Find the previous rate that is in the rate mask */
711 i = index - 1;
712 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
713 if (rate_mask & mask) {
714 low = i;
715 break;
716 }
717 }
718
719 /* Find the next rate that is in the rate mask */
720 i = index + 1;
721 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
722 if (rate_mask & mask) {
723 high = i;
724 break;
725 }
726 }
727
728 return (high << 8) | low;
729 }
730
731 low = index;
732 while (low != IWL_RATE_INVALID) {
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800733 low = iwl_rates[low].prev_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700734 if (low == IWL_RATE_INVALID)
735 break;
736 if (rate_mask & (1 << low))
737 break;
Tomas Winklere1623442009-01-27 14:27:56 -0800738 IWL_DEBUG_RATE(priv, "Skipping masked lower rate: %d\n", low);
Zhu Yib481de92007-09-25 17:54:57 -0700739 }
740
741 high = index;
742 while (high != IWL_RATE_INVALID) {
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800743 high = iwl_rates[high].next_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700744 if (high == IWL_RATE_INVALID)
745 break;
746 if (rate_mask & (1 << high))
747 break;
Tomas Winklere1623442009-01-27 14:27:56 -0800748 IWL_DEBUG_RATE(priv, "Skipping masked higher rate: %d\n", high);
Zhu Yib481de92007-09-25 17:54:57 -0700749 }
750
751 return (high << 8) | low;
752}
753
Tomas Winklere227cea2008-07-18 13:53:05 +0800754static u32 rs_get_lower_rate(struct iwl_lq_sta *lq_sta,
755 struct iwl_scale_tbl_info *tbl,
756 u8 scale_index, u8 ht_possible)
Zhu Yib481de92007-09-25 17:54:57 -0700757{
Zhu Yib481de92007-09-25 17:54:57 -0700758 s32 low;
759 u16 rate_mask;
760 u16 high_low;
761 u8 switch_to_legacy = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -0800762 u8 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -0700763
764 /* check if we need to switch from HT to legacy rates.
765 * assumption is that mandatory rates (1Mbps or 6Mbps)
766 * are always supported (spec demand) */
767 if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_index)) {
768 switch_to_legacy = 1;
769 scale_index = rs_ht_to_legacy[scale_index];
Johannes Berg8318d782008-01-24 19:38:38 +0100770 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700771 tbl->lq_type = LQ_A;
772 else
773 tbl->lq_type = LQ_G;
774
Guy Cohen69fdb302008-04-23 17:15:01 -0700775 if (num_of_ant(tbl->ant_type) > 1)
Guy Cohenfde0db32008-04-21 15:42:01 -0700776 tbl->ant_type = ANT_A;/*FIXME:RS*/
Zhu Yib481de92007-09-25 17:54:57 -0700777
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700778 tbl->is_ht40 = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700779 tbl->is_SGI = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -0700780 tbl->max_search = IWL_MAX_SEARCH;
Zhu Yib481de92007-09-25 17:54:57 -0700781 }
782
Guy Coheneecd6e52008-04-23 17:14:58 -0700783 rate_mask = rs_get_supported_rates(lq_sta, NULL, tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -0700784
Ben Cahill77626352007-11-29 11:09:44 +0800785 /* Mask with station rate restriction */
Zhu Yib481de92007-09-25 17:54:57 -0700786 if (is_legacy(tbl->lq_type)) {
Ben Cahill77626352007-11-29 11:09:44 +0800787 /* supp_rates has no CCK bits in A mode */
Johannes Berg8318d782008-01-24 19:38:38 +0100788 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700789 rate_mask = (u16)(rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -0800790 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
Zhu Yib481de92007-09-25 17:54:57 -0700791 else
Tomas Winklerc33104f2008-01-14 17:46:21 -0800792 rate_mask = (u16)(rate_mask & lq_sta->supp_rates);
Zhu Yib481de92007-09-25 17:54:57 -0700793 }
794
Ben Cahill77626352007-11-29 11:09:44 +0800795 /* If we switched from HT to legacy, check current rate */
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800796 if (switch_to_legacy && (rate_mask & (1 << scale_index))) {
Guy Cohen39e88502008-04-23 17:14:57 -0700797 low = scale_index;
798 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700799 }
800
Ester Kummerbf403db2008-05-05 10:22:40 +0800801 high_low = rs_get_adjacent_rate(lq_sta->drv, scale_index, rate_mask,
802 tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -0700803 low = high_low & 0xff;
804
Guy Cohen39e88502008-04-23 17:14:57 -0700805 if (low == IWL_RATE_INVALID)
806 low = scale_index;
807
808out:
Tomas Winkler978785a2008-12-19 10:37:31 +0800809 return rate_n_flags_from_tbl(lq_sta->drv, tbl, low, is_green);
Zhu Yib481de92007-09-25 17:54:57 -0700810}
811
Ben Cahill77626352007-11-29 11:09:44 +0800812/*
813 * mac80211 sends us Tx status
814 */
Johannes Berg4b7679a2008-09-18 18:14:18 +0200815static void rs_tx_status(void *priv_r, struct ieee80211_supported_band *sband,
816 struct ieee80211_sta *sta, void *priv_sta,
Johannes Berge039fa42008-05-15 12:55:29 +0200817 struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -0700818{
819 int status;
820 u8 retries;
Daniel C Halperin50273092009-08-28 09:44:45 -0700821 int rs_index, mac_index, index = 0;
Tomas Winkler417f1142008-11-12 13:14:06 -0800822 struct iwl_lq_sta *lq_sta = priv_sta;
Tomas Winkler66c73db2008-04-15 16:01:40 -0700823 struct iwl_link_quality_cmd *table;
Zhu Yib481de92007-09-25 17:54:57 -0700824 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berg4b7679a2008-09-18 18:14:18 +0200825 struct iwl_priv *priv = (struct iwl_priv *)priv_r;
Johannes Berge039fa42008-05-15 12:55:29 +0200826 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Tomas Winklere227cea2008-07-18 13:53:05 +0800827 struct iwl_rate_scale_data *window = NULL;
828 struct iwl_rate_scale_data *search_win = NULL;
Daniel C Halperin50273092009-08-28 09:44:45 -0700829 enum mac80211_rate_control_flags mac_flags;
Guy Cohen39e88502008-04-23 17:14:57 -0700830 u32 tx_rate;
Tomas Winklere227cea2008-07-18 13:53:05 +0800831 struct iwl_scale_tbl_info tbl_type;
832 struct iwl_scale_tbl_info *curr_tbl, *search_tbl;
Zhu Yib481de92007-09-25 17:54:57 -0700833 u8 active_index = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700834 s32 tpt = 0;
835
Tomas Winklere1623442009-01-27 14:27:56 -0800836 IWL_DEBUG_RATE_LIMIT(priv, "get frame ack response, update rate scale window\n");
Zhu Yib481de92007-09-25 17:54:57 -0700837
Tomas Winkler417f1142008-11-12 13:14:06 -0800838 if (!ieee80211_is_data(hdr->frame_control) ||
Gábor Stefanik514d65c2009-04-23 19:36:08 +0200839 info->flags & IEEE80211_TX_CTL_NO_ACK)
Zhu Yib481de92007-09-25 17:54:57 -0700840 return;
841
Ron Rindjunsky99556432008-01-28 14:07:25 +0200842 /* This packet was aggregated but doesn't carry rate scale info */
Johannes Berge039fa42008-05-15 12:55:29 +0200843 if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
844 !(info->flags & IEEE80211_TX_STAT_AMPDU))
Ron Rindjunsky99556432008-01-28 14:07:25 +0200845 return;
846
Wey-Yi Guy8fe72312009-03-10 14:35:10 -0700847 if (info->flags & IEEE80211_TX_STAT_AMPDU)
848 retries = 0;
849 else
850 retries = info->status.rates[0].count - 1;
Zhu Yib481de92007-09-25 17:54:57 -0700851
852 if (retries > 15)
853 retries = 15;
854
Johannes Berg05c914f2008-09-11 00:01:58 +0200855 if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) &&
Tomas Winklerc33104f2008-01-14 17:46:21 -0800856 !lq_sta->ibss_sta_added)
Tomas Winklerf4d60822008-03-05 11:31:00 -0800857 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700858
Tomas Winklerc33104f2008-01-14 17:46:21 -0800859 table = &lq_sta->lq;
860 active_index = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -0700861
Tomas Winklerc33104f2008-01-14 17:46:21 -0800862 curr_tbl = &(lq_sta->lq_info[active_index]);
863 search_tbl = &(lq_sta->lq_info[(1 - active_index)]);
Tomas Winklere227cea2008-07-18 13:53:05 +0800864 window = (struct iwl_rate_scale_data *)&(curr_tbl->win[0]);
865 search_win = (struct iwl_rate_scale_data *)&(search_tbl->win[0]);
Zhu Yib481de92007-09-25 17:54:57 -0700866
Ben Cahill77626352007-11-29 11:09:44 +0800867 /*
868 * Ignore this Tx frame response if its initial rate doesn't match
869 * that of latest Link Quality command. There may be stragglers
870 * from a previous Link Quality command, but we're no longer interested
871 * in those; they're either from the "active" mode while we're trying
872 * to check "search" mode, or a prior "search" mode after we've moved
873 * to a new "search" mode (which might become the new "active" mode).
874 */
Guy Cohen39e88502008-04-23 17:14:57 -0700875 tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
876 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800877 if (priv->band == IEEE80211_BAND_5GHZ)
878 rs_index -= IWL_FIRST_OFDM_RATE;
Daniel C Halperin50273092009-08-28 09:44:45 -0700879 mac_flags = info->status.rates[0].flags;
880 mac_index = info->status.rates[0].idx;
Daniel C Halperin31513be2009-08-28 09:44:47 -0700881 /* For HT packets, map MCS to PLCP */
882 if (mac_flags & IEEE80211_TX_RC_MCS) {
883 mac_index &= RATE_MCS_CODE_MSK; /* Remove # of streams */
884 if (mac_index >= (IWL_RATE_9M_INDEX - IWL_FIRST_OFDM_RATE))
885 mac_index++;
Daniel C Halperin392a0ba2009-09-11 10:38:08 -0700886 /*
887 * mac80211 HT index is always zero-indexed; we need to move
888 * HT OFDM rates after CCK rates in 2.4 GHz band
889 */
890 if (priv->band == IEEE80211_BAND_2GHZ)
891 mac_index += IWL_FIRST_OFDM_RATE;
Daniel C Halperin31513be2009-08-28 09:44:47 -0700892 }
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800893
Daniel C Halperin50273092009-08-28 09:44:45 -0700894 if ((mac_index < 0) ||
895 (tbl_type.is_SGI != !!(mac_flags & IEEE80211_TX_RC_SHORT_GI)) ||
896 (tbl_type.is_ht40 != !!(mac_flags & IEEE80211_TX_RC_40_MHZ_WIDTH)) ||
897 (tbl_type.is_dup != !!(mac_flags & IEEE80211_TX_RC_DUP_DATA)) ||
Johannes Berge6a98542008-10-21 12:40:02 +0200898 (tbl_type.ant_type != info->antenna_sel_tx) ||
Daniel C Halperin50273092009-08-28 09:44:45 -0700899 (!!(tx_rate & RATE_MCS_HT_MSK) != !!(mac_flags & IEEE80211_TX_RC_MCS)) ||
900 (!!(tx_rate & RATE_MCS_GF_MSK) != !!(mac_flags & IEEE80211_TX_RC_GREEN_FIELD)) ||
Daniel C Halperin31513be2009-08-28 09:44:47 -0700901 (rs_index != mac_index)) {
902 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 -0800903 /* the last LQ command could failed so the LQ in ucode not
904 * the same in driver sync up
905 */
Abbas, Mohamed7d049e52009-01-20 21:33:53 -0800906 lq_sta->missed_rate_counter++;
907 if (lq_sta->missed_rate_counter > IWL_MISSED_RATE_MAX) {
908 lq_sta->missed_rate_counter = 0;
909 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
910 }
Tomas Winklerf4d60822008-03-05 11:31:00 -0800911 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700912 }
913
Abbas, Mohamed7d049e52009-01-20 21:33:53 -0800914 lq_sta->missed_rate_counter = 0;
Ben Cahill77626352007-11-29 11:09:44 +0800915 /* Update frame history window with "failure" for each Tx retry. */
Zhu Yib481de92007-09-25 17:54:57 -0700916 while (retries) {
Ben Cahill77626352007-11-29 11:09:44 +0800917 /* Look up the rate and other info used for each tx attempt.
918 * Each tx attempt steps one entry deeper in the rate table. */
Guy Cohen39e88502008-04-23 17:14:57 -0700919 tx_rate = le32_to_cpu(table->rs_table[index].rate_n_flags);
920 rs_get_tbl_info_from_mcs(tx_rate, priv->band,
Zhu Yib481de92007-09-25 17:54:57 -0700921 &tbl_type, &rs_index);
922
Ben Cahill77626352007-11-29 11:09:44 +0800923 /* If type matches "search" table,
924 * add failure to "search" history */
Zhu Yib481de92007-09-25 17:54:57 -0700925 if ((tbl_type.lq_type == search_tbl->lq_type) &&
Guy Cohenfde0db32008-04-21 15:42:01 -0700926 (tbl_type.ant_type == search_tbl->ant_type) &&
Zhu Yib481de92007-09-25 17:54:57 -0700927 (tbl_type.is_SGI == search_tbl->is_SGI)) {
928 if (search_tbl->expected_tpt)
929 tpt = search_tbl->expected_tpt[rs_index];
930 else
931 tpt = 0;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200932 rs_collect_tx_data(search_win, rs_index, tpt, 1, 0);
Ben Cahill77626352007-11-29 11:09:44 +0800933
934 /* Else if type matches "current/active" table,
935 * add failure to "current/active" history */
Zhu Yib481de92007-09-25 17:54:57 -0700936 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
Guy Cohenfde0db32008-04-21 15:42:01 -0700937 (tbl_type.ant_type == curr_tbl->ant_type) &&
Zhu Yib481de92007-09-25 17:54:57 -0700938 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
939 if (curr_tbl->expected_tpt)
940 tpt = curr_tbl->expected_tpt[rs_index];
941 else
942 tpt = 0;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200943 rs_collect_tx_data(window, rs_index, tpt, 1, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700944 }
Ben Cahill77626352007-11-29 11:09:44 +0800945
946 /* If not searching for a new mode, increment failed counter
947 * ... this helps determine when to start searching again */
Tomas Winklerc33104f2008-01-14 17:46:21 -0800948 if (lq_sta->stay_in_tbl)
949 lq_sta->total_failed++;
Zhu Yib481de92007-09-25 17:54:57 -0700950 --retries;
951 index++;
952
953 }
954
Ben Cahill77626352007-11-29 11:09:44 +0800955 /*
956 * Find (by rate) the history window to update with final Tx attempt;
957 * if Tx was successful first try, use original rate,
958 * else look up the rate that was, finally, successful.
959 */
Guy Cohen39e88502008-04-23 17:14:57 -0700960 tx_rate = le32_to_cpu(table->rs_table[index].rate_n_flags);
Wey-Yi Guy12b96812009-04-08 11:39:27 -0700961 lq_sta->last_rate_n_flags = tx_rate;
Guy Cohen39e88502008-04-23 17:14:57 -0700962 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
Zhu Yib481de92007-09-25 17:54:57 -0700963
Ben Cahill77626352007-11-29 11:09:44 +0800964 /* Update frame history window with "success" if Tx got ACKed ... */
Johannes Berge039fa42008-05-15 12:55:29 +0200965 status = !!(info->flags & IEEE80211_TX_STAT_ACK);
Zhu Yib481de92007-09-25 17:54:57 -0700966
Ben Cahill77626352007-11-29 11:09:44 +0800967 /* If type matches "search" table,
968 * add final tx status to "search" history */
Zhu Yib481de92007-09-25 17:54:57 -0700969 if ((tbl_type.lq_type == search_tbl->lq_type) &&
Guy Cohenfde0db32008-04-21 15:42:01 -0700970 (tbl_type.ant_type == search_tbl->ant_type) &&
Zhu Yib481de92007-09-25 17:54:57 -0700971 (tbl_type.is_SGI == search_tbl->is_SGI)) {
972 if (search_tbl->expected_tpt)
973 tpt = search_tbl->expected_tpt[rs_index];
974 else
975 tpt = 0;
Wey-Yi Guydf36c042009-03-10 14:35:11 -0700976 if (info->flags & IEEE80211_TX_STAT_AMPDU)
Ron Rindjunsky99556432008-01-28 14:07:25 +0200977 rs_collect_tx_data(search_win, rs_index, tpt,
Johannes Berge039fa42008-05-15 12:55:29 +0200978 info->status.ampdu_ack_len,
979 info->status.ampdu_ack_map);
Ron Rindjunsky99556432008-01-28 14:07:25 +0200980 else
981 rs_collect_tx_data(search_win, rs_index, tpt,
982 1, status);
Ben Cahill77626352007-11-29 11:09:44 +0800983 /* Else if type matches "current/active" table,
984 * add final tx status to "current/active" history */
Zhu Yib481de92007-09-25 17:54:57 -0700985 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
Guy Cohenfde0db32008-04-21 15:42:01 -0700986 (tbl_type.ant_type == curr_tbl->ant_type) &&
Zhu Yib481de92007-09-25 17:54:57 -0700987 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
988 if (curr_tbl->expected_tpt)
989 tpt = curr_tbl->expected_tpt[rs_index];
990 else
991 tpt = 0;
Wey-Yi Guydf36c042009-03-10 14:35:11 -0700992 if (info->flags & IEEE80211_TX_STAT_AMPDU)
Ron Rindjunsky99556432008-01-28 14:07:25 +0200993 rs_collect_tx_data(window, rs_index, tpt,
Johannes Berge039fa42008-05-15 12:55:29 +0200994 info->status.ampdu_ack_len,
995 info->status.ampdu_ack_map);
Ron Rindjunsky99556432008-01-28 14:07:25 +0200996 else
997 rs_collect_tx_data(window, rs_index, tpt,
998 1, status);
Zhu Yib481de92007-09-25 17:54:57 -0700999 }
1000
Ben Cahill77626352007-11-29 11:09:44 +08001001 /* If not searching for new mode, increment success/failed counter
1002 * ... these help determine when to start searching again */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001003 if (lq_sta->stay_in_tbl) {
Wey-Yi Guydf36c042009-03-10 14:35:11 -07001004 if (info->flags & IEEE80211_TX_STAT_AMPDU) {
Johannes Berge039fa42008-05-15 12:55:29 +02001005 lq_sta->total_success += info->status.ampdu_ack_map;
Ron Rindjunsky99556432008-01-28 14:07:25 +02001006 lq_sta->total_failed +=
Johannes Berge039fa42008-05-15 12:55:29 +02001007 (info->status.ampdu_ack_len - info->status.ampdu_ack_map);
Ron Rindjunsky99556432008-01-28 14:07:25 +02001008 } else {
1009 if (status)
1010 lq_sta->total_success++;
1011 else
1012 lq_sta->total_failed++;
1013 }
Zhu Yib481de92007-09-25 17:54:57 -07001014 }
1015
Ben Cahill77626352007-11-29 11:09:44 +08001016 /* See if there's a better rate or modulation mode to try. */
Abbas, Mohamedc338ba32009-01-21 10:58:02 -08001017 if (sta && sta->supp_rates[sband->band])
Gábor Stefanik514d65c2009-04-23 19:36:08 +02001018 rs_rate_scale_perform(priv, skb, sta, lq_sta);
Tomas Winklerf4d60822008-03-05 11:31:00 -08001019out:
Zhu Yib481de92007-09-25 17:54:57 -07001020 return;
1021}
1022
Ben Cahill77626352007-11-29 11:09:44 +08001023/*
1024 * Begin a period of staying with a selected modulation mode.
1025 * Set "stay_in_tbl" flag to prevent any mode switches.
1026 * Set frame tx success limits according to legacy vs. high-throughput,
1027 * and reset overall (spanning all rates) tx success history statistics.
1028 * These control how long we stay using same modulation mode before
1029 * searching for a new mode.
1030 */
Ester Kummerbf403db2008-05-05 10:22:40 +08001031static void rs_set_stay_in_table(struct iwl_priv *priv, u8 is_legacy,
Tomas Winklere227cea2008-07-18 13:53:05 +08001032 struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07001033{
Tomas Winklere1623442009-01-27 14:27:56 -08001034 IWL_DEBUG_RATE(priv, "we are staying in the same table\n");
Tomas Winklerc33104f2008-01-14 17:46:21 -08001035 lq_sta->stay_in_tbl = 1; /* only place this gets set */
Zhu Yib481de92007-09-25 17:54:57 -07001036 if (is_legacy) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001037 lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT;
1038 lq_sta->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
1039 lq_sta->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
Zhu Yib481de92007-09-25 17:54:57 -07001040 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001041 lq_sta->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
1042 lq_sta->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
1043 lq_sta->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
Zhu Yib481de92007-09-25 17:54:57 -07001044 }
Tomas Winklerc33104f2008-01-14 17:46:21 -08001045 lq_sta->table_count = 0;
1046 lq_sta->total_failed = 0;
1047 lq_sta->total_success = 0;
Mohamed Abbas447fee72009-04-20 14:37:02 -07001048 lq_sta->flush_timer = jiffies;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001049 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001050}
1051
Ben Cahill77626352007-11-29 11:09:44 +08001052/*
1053 * Find correct throughput table for given mode of modulation
1054 */
Tomas Winklere227cea2008-07-18 13:53:05 +08001055static void rs_set_expected_tpt_table(struct iwl_lq_sta *lq_sta,
1056 struct iwl_scale_tbl_info *tbl)
Zhu Yib481de92007-09-25 17:54:57 -07001057{
1058 if (is_legacy(tbl->lq_type)) {
1059 if (!is_a_band(tbl->lq_type))
1060 tbl->expected_tpt = expected_tpt_G;
1061 else
1062 tbl->expected_tpt = expected_tpt_A;
1063 } else if (is_siso(tbl->lq_type)) {
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001064 if (tbl->is_ht40 && !lq_sta->is_dup)
Zhu Yib481de92007-09-25 17:54:57 -07001065 if (tbl->is_SGI)
1066 tbl->expected_tpt = expected_tpt_siso40MHzSGI;
1067 else
1068 tbl->expected_tpt = expected_tpt_siso40MHz;
1069 else if (tbl->is_SGI)
1070 tbl->expected_tpt = expected_tpt_siso20MHzSGI;
1071 else
1072 tbl->expected_tpt = expected_tpt_siso20MHz;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001073 } else if (is_mimo2(tbl->lq_type)) {
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001074 if (tbl->is_ht40 && !lq_sta->is_dup)
Zhu Yib481de92007-09-25 17:54:57 -07001075 if (tbl->is_SGI)
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001076 tbl->expected_tpt = expected_tpt_mimo2_40MHzSGI;
Zhu Yib481de92007-09-25 17:54:57 -07001077 else
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001078 tbl->expected_tpt = expected_tpt_mimo2_40MHz;
Zhu Yib481de92007-09-25 17:54:57 -07001079 else if (tbl->is_SGI)
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001080 tbl->expected_tpt = expected_tpt_mimo2_20MHzSGI;
Zhu Yib481de92007-09-25 17:54:57 -07001081 else
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001082 tbl->expected_tpt = expected_tpt_mimo2_20MHz;
1083 } else if (is_mimo3(tbl->lq_type)) {
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001084 if (tbl->is_ht40 && !lq_sta->is_dup)
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001085 if (tbl->is_SGI)
1086 tbl->expected_tpt = expected_tpt_mimo3_40MHzSGI;
1087 else
1088 tbl->expected_tpt = expected_tpt_mimo3_40MHz;
1089 else if (tbl->is_SGI)
1090 tbl->expected_tpt = expected_tpt_mimo3_20MHzSGI;
1091 else
1092 tbl->expected_tpt = expected_tpt_mimo3_20MHz;
Zhu Yib481de92007-09-25 17:54:57 -07001093 } else
1094 tbl->expected_tpt = expected_tpt_G;
1095}
1096
Ben Cahill77626352007-11-29 11:09:44 +08001097/*
1098 * Find starting rate for new "search" high-throughput mode of modulation.
1099 * Goal is to find lowest expected rate (under perfect conditions) that is
1100 * above the current measured throughput of "active" mode, to give new mode
1101 * a fair chance to prove itself without too many challenges.
1102 *
1103 * This gets called when transitioning to more aggressive modulation
1104 * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
1105 * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
1106 * to decrease to match "active" throughput. When moving from MIMO to SISO,
1107 * bit rate will typically need to increase, but not if performance was bad.
1108 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001109static s32 rs_get_best_rate(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001110 struct iwl_lq_sta *lq_sta,
1111 struct iwl_scale_tbl_info *tbl, /* "search" */
Guy Cohenf935a6d2008-04-23 17:15:02 -07001112 u16 rate_mask, s8 index)
Zhu Yib481de92007-09-25 17:54:57 -07001113{
Ben Cahill77626352007-11-29 11:09:44 +08001114 /* "active" values */
Tomas Winklere227cea2008-07-18 13:53:05 +08001115 struct iwl_scale_tbl_info *active_tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001116 &(lq_sta->lq_info[lq_sta->active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07001117 s32 active_sr = active_tbl->win[index].success_ratio;
Zhu Yib481de92007-09-25 17:54:57 -07001118 s32 active_tpt = active_tbl->expected_tpt[index];
Ben Cahill77626352007-11-29 11:09:44 +08001119
1120 /* expected "search" throughput */
1121 s32 *tpt_tbl = tbl->expected_tpt;
1122
1123 s32 new_rate, high, low, start_hi;
Zhu Yib481de92007-09-25 17:54:57 -07001124 u16 high_low;
Guy Cohenf935a6d2008-04-23 17:15:02 -07001125 s8 rate = index;
Zhu Yib481de92007-09-25 17:54:57 -07001126
1127 new_rate = high = low = start_hi = IWL_RATE_INVALID;
1128
1129 for (; ;) {
Ester Kummerbf403db2008-05-05 10:22:40 +08001130 high_low = rs_get_adjacent_rate(priv, rate, rate_mask,
1131 tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -07001132
1133 low = high_low & 0xff;
1134 high = (high_low >> 8) & 0xff;
1135
Ben Cahill77626352007-11-29 11:09:44 +08001136 /*
1137 * Lower the "search" bit rate, to give new "search" mode
1138 * approximately the same throughput as "active" if:
1139 *
1140 * 1) "Active" mode has been working modestly well (but not
1141 * great), and expected "search" throughput (under perfect
1142 * conditions) at candidate rate is above the actual
1143 * measured "active" throughput (but less than expected
1144 * "active" throughput under perfect conditions).
1145 * OR
1146 * 2) "Active" mode has been working perfectly or very well
1147 * and expected "search" throughput (under perfect
1148 * conditions) at candidate rate is above expected
1149 * "active" throughput (under perfect conditions).
1150 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001151 if ((((100 * tpt_tbl[rate]) > lq_sta->last_tpt) &&
Zhu Yib481de92007-09-25 17:54:57 -07001152 ((active_sr > IWL_RATE_DECREASE_TH) &&
1153 (active_sr <= IWL_RATE_HIGH_TH) &&
1154 (tpt_tbl[rate] <= active_tpt))) ||
1155 ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
1156 (tpt_tbl[rate] > active_tpt))) {
1157
Ben Cahill77626352007-11-29 11:09:44 +08001158 /* (2nd or later pass)
1159 * If we've already tried to raise the rate, and are
1160 * now trying to lower it, use the higher rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001161 if (start_hi != IWL_RATE_INVALID) {
1162 new_rate = start_hi;
1163 break;
1164 }
Ben Cahill77626352007-11-29 11:09:44 +08001165
Zhu Yib481de92007-09-25 17:54:57 -07001166 new_rate = rate;
Ben Cahill77626352007-11-29 11:09:44 +08001167
1168 /* Loop again with lower rate */
Zhu Yib481de92007-09-25 17:54:57 -07001169 if (low != IWL_RATE_INVALID)
1170 rate = low;
Ben Cahill77626352007-11-29 11:09:44 +08001171
1172 /* Lower rate not available, use the original */
Zhu Yib481de92007-09-25 17:54:57 -07001173 else
1174 break;
Ben Cahill77626352007-11-29 11:09:44 +08001175
1176 /* Else try to raise the "search" rate to match "active" */
Zhu Yib481de92007-09-25 17:54:57 -07001177 } else {
Ben Cahill77626352007-11-29 11:09:44 +08001178 /* (2nd or later pass)
1179 * If we've already tried to lower the rate, and are
1180 * now trying to raise it, use the lower rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001181 if (new_rate != IWL_RATE_INVALID)
1182 break;
Ben Cahill77626352007-11-29 11:09:44 +08001183
1184 /* Loop again with higher rate */
Zhu Yib481de92007-09-25 17:54:57 -07001185 else if (high != IWL_RATE_INVALID) {
1186 start_hi = high;
1187 rate = high;
Ben Cahill77626352007-11-29 11:09:44 +08001188
1189 /* Higher rate not available, use the original */
Zhu Yib481de92007-09-25 17:54:57 -07001190 } else {
Guy Cohen90d77952008-09-09 10:54:53 +08001191 new_rate = rate;
Zhu Yib481de92007-09-25 17:54:57 -07001192 break;
1193 }
1194 }
1195 }
1196
1197 return new_rate;
1198}
Zhu Yib481de92007-09-25 17:54:57 -07001199
Ben Cahill77626352007-11-29 11:09:44 +08001200/*
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001201 * Set up search table for MIMO2
Ben Cahill77626352007-11-29 11:09:44 +08001202 */
Guy Cohenfde0db32008-04-21 15:42:01 -07001203static int rs_switch_to_mimo2(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001204 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001205 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001206 struct ieee80211_sta *sta,
Tomas Winklere227cea2008-07-18 13:53:05 +08001207 struct iwl_scale_tbl_info *tbl, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001208{
Zhu Yib481de92007-09-25 17:54:57 -07001209 u16 rate_mask;
1210 s32 rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001211 s8 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07001212
Luis R. Rodriguezde27e642008-12-23 15:58:44 -08001213 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
Zhu Yib481de92007-09-25 17:54:57 -07001214 return -1;
1215
Johannes Bergd9fe60d2008-10-09 12:13:49 +02001216 if (((sta->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 2)
Tomas Winkler00c5ae22008-09-03 11:26:42 +08001217 == WLAN_HT_CAP_SM_PS_STATIC)
Zhu Yib481de92007-09-25 17:54:57 -07001218 return -1;
1219
Ben Cahill77626352007-11-29 11:09:44 +08001220 /* Need both Tx chains/antennas to support MIMO */
Guy Cohenaade00c2008-04-23 17:14:59 -07001221 if (priv->hw_params.tx_chains_num < 2)
Zhu Yib481de92007-09-25 17:54:57 -07001222 return -1;
1223
Tomas Winklere1623442009-01-27 14:27:56 -08001224 IWL_DEBUG_RATE(priv, "LQ: try to switch to MIMO2\n");
Guy Cohen39e88502008-04-23 17:14:57 -07001225
1226 tbl->lq_type = LQ_MIMO2;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001227 tbl->is_dup = lq_sta->is_dup;
Zhu Yib481de92007-09-25 17:54:57 -07001228 tbl->action = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001229 tbl->max_search = IWL_MAX_SEARCH;
Guy Cohen39e88502008-04-23 17:14:57 -07001230 rate_mask = lq_sta->active_mimo2_rate;
1231
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001232 if (iwl_is_ht40_tx_allowed(priv, &sta->ht_cap))
1233 tbl->is_ht40 = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001234 else
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001235 tbl->is_ht40 = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001236
Guy Coheneecd6e52008-04-23 17:14:58 -07001237 rs_set_expected_tpt_table(lq_sta, tbl);
Zhu Yib481de92007-09-25 17:54:57 -07001238
Guy Cohenf935a6d2008-04-23 17:15:02 -07001239 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
Zhu Yib481de92007-09-25 17:54:57 -07001240
Tomas Winklere1623442009-01-27 14:27:56 -08001241 IWL_DEBUG_RATE(priv, "LQ: MIMO2 best rate %d mask %X\n", rate, rate_mask);
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001242 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1243 IWL_DEBUG_RATE(priv, "Can't switch with index %d rate mask %x\n",
1244 rate, rate_mask);
1245 return -1;
1246 }
1247 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
Guy Cohen39e88502008-04-23 17:14:57 -07001248
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001249 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
1250 tbl->current_rate, is_green);
1251 return 0;
1252}
1253
1254/*
1255 * Set up search table for MIMO3
1256 */
1257static int rs_switch_to_mimo3(struct iwl_priv *priv,
1258 struct iwl_lq_sta *lq_sta,
1259 struct ieee80211_conf *conf,
1260 struct ieee80211_sta *sta,
1261 struct iwl_scale_tbl_info *tbl, int index)
1262{
1263 u16 rate_mask;
1264 s32 rate;
1265 s8 is_green = lq_sta->is_green;
1266
1267 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
1268 return -1;
1269
1270 if (((sta->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 2)
1271 == WLAN_HT_CAP_SM_PS_STATIC)
1272 return -1;
1273
1274 /* Need both Tx chains/antennas to support MIMO */
1275 if (priv->hw_params.tx_chains_num < 3)
1276 return -1;
1277
1278 IWL_DEBUG_RATE(priv, "LQ: try to switch to MIMO3\n");
1279
1280 tbl->lq_type = LQ_MIMO3;
1281 tbl->is_dup = lq_sta->is_dup;
1282 tbl->action = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001283 tbl->max_search = IWL_MAX_11N_MIMO3_SEARCH;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001284 rate_mask = lq_sta->active_mimo3_rate;
1285
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001286 if (iwl_is_ht40_tx_allowed(priv, &sta->ht_cap))
1287 tbl->is_ht40 = 1;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001288 else
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001289 tbl->is_ht40 = 0;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001290
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001291 rs_set_expected_tpt_table(lq_sta, tbl);
1292
1293 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
1294
1295 IWL_DEBUG_RATE(priv, "LQ: MIMO3 best rate %d mask %X\n",
1296 rate, rate_mask);
Guy Cohen39e88502008-04-23 17:14:57 -07001297 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
Tomas Winklere1623442009-01-27 14:27:56 -08001298 IWL_DEBUG_RATE(priv, "Can't switch with index %d rate mask %x\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001299 rate, rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07001300 return -1;
Guy Cohen39e88502008-04-23 17:14:57 -07001301 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001302 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
Zhu Yib481de92007-09-25 17:54:57 -07001303
Tomas Winklere1623442009-01-27 14:27:56 -08001304 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001305 tbl->current_rate, is_green);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001306 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07001307}
1308
Ben Cahill77626352007-11-29 11:09:44 +08001309/*
1310 * Set up search table for SISO
1311 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001312static int rs_switch_to_siso(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001313 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001314 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001315 struct ieee80211_sta *sta,
Tomas Winklere227cea2008-07-18 13:53:05 +08001316 struct iwl_scale_tbl_info *tbl, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001317{
Zhu Yib481de92007-09-25 17:54:57 -07001318 u16 rate_mask;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001319 u8 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07001320 s32 rate;
1321
Luis R. Rodriguezde27e642008-12-23 15:58:44 -08001322 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
Zhu Yib481de92007-09-25 17:54:57 -07001323 return -1;
1324
Tomas Winklere1623442009-01-27 14:27:56 -08001325 IWL_DEBUG_RATE(priv, "LQ: try to switch to SISO\n");
Guy Cohen39e88502008-04-23 17:14:57 -07001326
Tomas Winklerc33104f2008-01-14 17:46:21 -08001327 tbl->is_dup = lq_sta->is_dup;
Zhu Yib481de92007-09-25 17:54:57 -07001328 tbl->lq_type = LQ_SISO;
1329 tbl->action = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001330 tbl->max_search = IWL_MAX_SEARCH;
Guy Cohen39e88502008-04-23 17:14:57 -07001331 rate_mask = lq_sta->active_siso_rate;
Zhu Yib481de92007-09-25 17:54:57 -07001332
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001333 if (iwl_is_ht40_tx_allowed(priv, &sta->ht_cap))
1334 tbl->is_ht40 = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001335 else
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001336 tbl->is_ht40 = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001337
Zhu Yib481de92007-09-25 17:54:57 -07001338 if (is_green)
Guy Cohen39e88502008-04-23 17:14:57 -07001339 tbl->is_SGI = 0; /*11n spec: no SGI in SISO+Greenfield*/
Zhu Yib481de92007-09-25 17:54:57 -07001340
Guy Coheneecd6e52008-04-23 17:14:58 -07001341 rs_set_expected_tpt_table(lq_sta, tbl);
Guy Cohenf935a6d2008-04-23 17:15:02 -07001342 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
Zhu Yib481de92007-09-25 17:54:57 -07001343
Tomas Winklere1623442009-01-27 14:27:56 -08001344 IWL_DEBUG_RATE(priv, "LQ: get best rate %d mask %X\n", rate, rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07001345 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
Tomas Winklere1623442009-01-27 14:27:56 -08001346 IWL_DEBUG_RATE(priv, "can not switch with index %d rate mask %x\n",
Zhu Yib481de92007-09-25 17:54:57 -07001347 rate, rate_mask);
1348 return -1;
1349 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001350 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
Tomas Winklere1623442009-01-27 14:27:56 -08001351 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001352 tbl->current_rate, is_green);
Mohamed Abbas403ab562007-11-06 22:06:25 -08001353 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07001354}
1355
Ben Cahill77626352007-11-29 11:09:44 +08001356/*
1357 * Try to switch to new modulation mode from legacy
1358 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001359static int rs_move_legacy_other(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001360 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001361 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001362 struct ieee80211_sta *sta,
Zhu Yib481de92007-09-25 17:54:57 -07001363 int index)
1364{
Tomas Winklere227cea2008-07-18 13:53:05 +08001365 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1366 struct iwl_scale_tbl_info *search_tbl =
1367 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1368 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1369 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1370 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001371 u8 start_action = tbl->action;
Guy Cohenfde0db32008-04-21 15:42:01 -07001372 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
Guy Cohen3110bef2008-09-09 10:54:54 +08001373 u8 tx_chains_num = priv->hw_params.tx_chains_num;
Guy Cohenfde0db32008-04-21 15:42:01 -07001374 int ret = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001375 u8 update_search_tbl_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001376
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001377 if (!iwl_ht_enabled(priv))
1378 /* stay in Legacy */
1379 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
Johannes Berg3ad3b922009-08-07 15:41:48 -07001380 else if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001381 tbl->action > IWL_LEGACY_SWITCH_SISO)
1382 tbl->action = IWL_LEGACY_SWITCH_SISO;
Zhu Yib481de92007-09-25 17:54:57 -07001383 for (; ;) {
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001384 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001385 switch (tbl->action) {
Guy Cohen3110bef2008-09-09 10:54:54 +08001386 case IWL_LEGACY_SWITCH_ANTENNA1:
1387 case IWL_LEGACY_SWITCH_ANTENNA2:
Tomas Winklere1623442009-01-27 14:27:56 -08001388 IWL_DEBUG_RATE(priv, "LQ: Legacy toggle Antenna\n");
Zhu Yib481de92007-09-25 17:54:57 -07001389
Guy Cohen3110bef2008-09-09 10:54:54 +08001390 if ((tbl->action == IWL_LEGACY_SWITCH_ANTENNA1 &&
1391 tx_chains_num <= 1) ||
1392 (tbl->action == IWL_LEGACY_SWITCH_ANTENNA2 &&
1393 tx_chains_num <= 2))
1394 break;
1395
Ben Cahill77626352007-11-29 11:09:44 +08001396 /* Don't change antenna if success has been great */
Zhu Yib481de92007-09-25 17:54:57 -07001397 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1398 break;
Ben Cahill77626352007-11-29 11:09:44 +08001399
Ben Cahill77626352007-11-29 11:09:44 +08001400 /* Set up search table to try other antenna */
Zhu Yib481de92007-09-25 17:54:57 -07001401 memcpy(search_tbl, tbl, sz);
1402
Guy Cohenaade00c2008-04-23 17:14:59 -07001403 if (rs_toggle_antenna(valid_tx_ant,
1404 &search_tbl->current_rate, search_tbl)) {
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001405 update_search_tbl_counter = 1;
Guy Cohen3110bef2008-09-09 10:54:54 +08001406 rs_set_expected_tpt_table(lq_sta, search_tbl);
Guy Cohenaade00c2008-04-23 17:14:59 -07001407 goto out;
1408 }
Guy Cohena5e8b502008-05-29 16:35:11 +08001409 break;
Zhu Yib481de92007-09-25 17:54:57 -07001410 case IWL_LEGACY_SWITCH_SISO:
Tomas Winklere1623442009-01-27 14:27:56 -08001411 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to SISO\n");
Ben Cahill77626352007-11-29 11:09:44 +08001412
1413 /* Set up search table to try SISO */
Zhu Yib481de92007-09-25 17:54:57 -07001414 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001415 search_tbl->is_SGI = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001416 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001417 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001418 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001419 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001420 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001421 }
Zhu Yib481de92007-09-25 17:54:57 -07001422
1423 break;
Guy Cohen3110bef2008-09-09 10:54:54 +08001424 case IWL_LEGACY_SWITCH_MIMO2_AB:
1425 case IWL_LEGACY_SWITCH_MIMO2_AC:
1426 case IWL_LEGACY_SWITCH_MIMO2_BC:
Tomas Winklere1623442009-01-27 14:27:56 -08001427 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to MIMO2\n");
Ben Cahill77626352007-11-29 11:09:44 +08001428
1429 /* Set up search table to try MIMO */
Zhu Yib481de92007-09-25 17:54:57 -07001430 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001431 search_tbl->is_SGI = 0;
Guy Cohen3110bef2008-09-09 10:54:54 +08001432
1433 if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AB)
1434 search_tbl->ant_type = ANT_AB;
1435 else if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AC)
1436 search_tbl->ant_type = ANT_AC;
1437 else
1438 search_tbl->ant_type = ANT_BC;
1439
1440 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1441 break;
1442
Guy Cohenfde0db32008-04-21 15:42:01 -07001443 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001444 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001445 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001446 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001447 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001448 }
Zhu Yib481de92007-09-25 17:54:57 -07001449 break;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001450
1451 case IWL_LEGACY_SWITCH_MIMO3_ABC:
1452 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to MIMO3\n");
1453
1454 /* Set up search table to try MIMO3 */
1455 memcpy(search_tbl, tbl, sz);
1456 search_tbl->is_SGI = 0;
1457
1458 search_tbl->ant_type = ANT_ABC;
1459
1460 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1461 break;
1462
1463 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1464 search_tbl, index);
1465 if (!ret) {
1466 lq_sta->action_counter = 0;
1467 goto out;
1468 }
1469 break;
Zhu Yib481de92007-09-25 17:54:57 -07001470 }
1471 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001472 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001473 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
Zhu Yib481de92007-09-25 17:54:57 -07001474
1475 if (tbl->action == start_action)
1476 break;
1477
1478 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001479 search_tbl->lq_type = LQ_NONE;
Zhu Yib481de92007-09-25 17:54:57 -07001480 return 0;
1481
Guy Cohen3110bef2008-09-09 10:54:54 +08001482out:
1483 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001484 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001485 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001486 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001487 if (update_search_tbl_counter)
1488 search_tbl->action = tbl->action;
Zhu Yib481de92007-09-25 17:54:57 -07001489 return 0;
1490
1491}
1492
Ben Cahill77626352007-11-29 11:09:44 +08001493/*
1494 * Try to switch to new modulation mode from SISO
1495 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001496static int rs_move_siso_to_other(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001497 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001498 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001499 struct ieee80211_sta *sta, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001500{
Tomas Winklerc33104f2008-01-14 17:46:21 -08001501 u8 is_green = lq_sta->is_green;
Tomas Winklere227cea2008-07-18 13:53:05 +08001502 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1503 struct iwl_scale_tbl_info *search_tbl =
1504 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1505 struct iwl_rate_scale_data *window = &(tbl->win[index]);
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001506 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Tomas Winklere227cea2008-07-18 13:53:05 +08001507 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1508 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001509 u8 start_action = tbl->action;
Guy Cohenfde0db32008-04-21 15:42:01 -07001510 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
Guy Cohen3110bef2008-09-09 10:54:54 +08001511 u8 tx_chains_num = priv->hw_params.tx_chains_num;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001512 u8 update_search_tbl_counter = 0;
Guy Cohenfde0db32008-04-21 15:42:01 -07001513 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001514
Johannes Berg3ad3b922009-08-07 15:41:48 -07001515 if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001516 tbl->action > IWL_SISO_SWITCH_ANTENNA2) {
1517 /* stay in SISO */
1518 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1519 }
Zhu Yib481de92007-09-25 17:54:57 -07001520 for (;;) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001521 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001522 switch (tbl->action) {
Guy Cohen3110bef2008-09-09 10:54:54 +08001523 case IWL_SISO_SWITCH_ANTENNA1:
1524 case IWL_SISO_SWITCH_ANTENNA2:
Tomas Winklere1623442009-01-27 14:27:56 -08001525 IWL_DEBUG_RATE(priv, "LQ: SISO toggle Antenna\n");
Guy Cohen3110bef2008-09-09 10:54:54 +08001526
1527 if ((tbl->action == IWL_SISO_SWITCH_ANTENNA1 &&
1528 tx_chains_num <= 1) ||
1529 (tbl->action == IWL_SISO_SWITCH_ANTENNA2 &&
1530 tx_chains_num <= 2))
1531 break;
1532
Zhu Yib481de92007-09-25 17:54:57 -07001533 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1534 break;
Zhu Yib481de92007-09-25 17:54:57 -07001535
1536 memcpy(search_tbl, tbl, sz);
Guy Cohenaade00c2008-04-23 17:14:59 -07001537 if (rs_toggle_antenna(valid_tx_ant,
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001538 &search_tbl->current_rate, search_tbl)) {
1539 update_search_tbl_counter = 1;
Guy Cohenaade00c2008-04-23 17:14:59 -07001540 goto out;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001541 }
Guy Cohena5e8b502008-05-29 16:35:11 +08001542 break;
Guy Cohen3110bef2008-09-09 10:54:54 +08001543 case IWL_SISO_SWITCH_MIMO2_AB:
1544 case IWL_SISO_SWITCH_MIMO2_AC:
1545 case IWL_SISO_SWITCH_MIMO2_BC:
Tomas Winklere1623442009-01-27 14:27:56 -08001546 IWL_DEBUG_RATE(priv, "LQ: SISO switch to MIMO2\n");
Zhu Yib481de92007-09-25 17:54:57 -07001547 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001548 search_tbl->is_SGI = 0;
Guy Cohen3110bef2008-09-09 10:54:54 +08001549
1550 if (tbl->action == IWL_SISO_SWITCH_MIMO2_AB)
1551 search_tbl->ant_type = ANT_AB;
1552 else if (tbl->action == IWL_SISO_SWITCH_MIMO2_AC)
1553 search_tbl->ant_type = ANT_AC;
1554 else
1555 search_tbl->ant_type = ANT_BC;
1556
1557 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1558 break;
1559
Guy Cohenfde0db32008-04-21 15:42:01 -07001560 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001561 search_tbl, index);
Guy Cohen3110bef2008-09-09 10:54:54 +08001562 if (!ret)
Zhu Yib481de92007-09-25 17:54:57 -07001563 goto out;
1564 break;
1565 case IWL_SISO_SWITCH_GI:
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001566 if (!tbl->is_ht40 && !(ht_cap->cap &
1567 IEEE80211_HT_CAP_SGI_20))
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001568 break;
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001569 if (tbl->is_ht40 && !(ht_cap->cap &
1570 IEEE80211_HT_CAP_SGI_40))
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001571 break;
1572
Tomas Winklere1623442009-01-27 14:27:56 -08001573 IWL_DEBUG_RATE(priv, "LQ: SISO toggle SGI/NGI\n");
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001574
Zhu Yib481de92007-09-25 17:54:57 -07001575 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001576 if (is_green) {
1577 if (!tbl->is_SGI)
1578 break;
1579 else
Winkler, Tomas15b16872008-12-19 10:37:33 +08001580 IWL_ERR(priv,
1581 "SGI was set in GF+SISO\n");
Zhu Yib481de92007-09-25 17:54:57 -07001582 }
Guy Cohen39e88502008-04-23 17:14:57 -07001583 search_tbl->is_SGI = !tbl->is_SGI;
Guy Coheneecd6e52008-04-23 17:14:58 -07001584 rs_set_expected_tpt_table(lq_sta, search_tbl);
Guy Cohen39e88502008-04-23 17:14:57 -07001585 if (tbl->is_SGI) {
1586 s32 tpt = lq_sta->last_tpt / 100;
1587 if (tpt >= search_tbl->expected_tpt[index])
1588 break;
1589 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001590 search_tbl->current_rate =
1591 rate_n_flags_from_tbl(priv, search_tbl,
1592 index, is_green);
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001593 update_search_tbl_counter = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001594 goto out;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001595 case IWL_SISO_SWITCH_MIMO3_ABC:
1596 IWL_DEBUG_RATE(priv, "LQ: SISO switch to MIMO3\n");
1597 memcpy(search_tbl, tbl, sz);
1598 search_tbl->is_SGI = 0;
1599 search_tbl->ant_type = ANT_ABC;
1600
1601 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1602 break;
1603
1604 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1605 search_tbl, index);
1606 if (!ret)
1607 goto out;
1608 break;
Zhu Yib481de92007-09-25 17:54:57 -07001609 }
1610 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001611 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001612 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
Zhu Yib481de92007-09-25 17:54:57 -07001613
1614 if (tbl->action == start_action)
1615 break;
1616 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001617 search_tbl->lq_type = LQ_NONE;
Zhu Yib481de92007-09-25 17:54:57 -07001618 return 0;
1619
1620 out:
Guy Cohen3110bef2008-09-09 10:54:54 +08001621 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001622 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001623 if (tbl->action > IWL_SISO_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001624 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001625 if (update_search_tbl_counter)
1626 search_tbl->action = tbl->action;
1627
Zhu Yib481de92007-09-25 17:54:57 -07001628 return 0;
1629}
1630
Ben Cahill77626352007-11-29 11:09:44 +08001631/*
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001632 * Try to switch to new modulation mode from MIMO2
Ben Cahill77626352007-11-29 11:09:44 +08001633 */
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001634static int rs_move_mimo2_to_other(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001635 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001636 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001637 struct ieee80211_sta *sta, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001638{
Tomas Winklerc33104f2008-01-14 17:46:21 -08001639 s8 is_green = lq_sta->is_green;
Tomas Winklere227cea2008-07-18 13:53:05 +08001640 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1641 struct iwl_scale_tbl_info *search_tbl =
1642 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Guy Cohen3110bef2008-09-09 10:54:54 +08001643 struct iwl_rate_scale_data *window = &(tbl->win[index]);
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001644 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Tomas Winklere227cea2008-07-18 13:53:05 +08001645 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1646 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001647 u8 start_action = tbl->action;
Guy Cohen3110bef2008-09-09 10:54:54 +08001648 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1649 u8 tx_chains_num = priv->hw_params.tx_chains_num;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001650 u8 update_search_tbl_counter = 0;
Guy Cohenaade00c2008-04-23 17:14:59 -07001651 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001652
Johannes Berg3ad3b922009-08-07 15:41:48 -07001653 if ((iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE) &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001654 (tbl->action < IWL_MIMO2_SWITCH_SISO_A ||
1655 tbl->action > IWL_MIMO2_SWITCH_SISO_C)) {
1656 /* switch in SISO */
1657 tbl->action = IWL_MIMO2_SWITCH_SISO_A;
1658 }
Zhu Yib481de92007-09-25 17:54:57 -07001659 for (;;) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001660 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001661 switch (tbl->action) {
Guy Cohen3110bef2008-09-09 10:54:54 +08001662 case IWL_MIMO2_SWITCH_ANTENNA1:
1663 case IWL_MIMO2_SWITCH_ANTENNA2:
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001664 IWL_DEBUG_RATE(priv, "LQ: MIMO2 toggle Antennas\n");
Guy Cohen3110bef2008-09-09 10:54:54 +08001665
1666 if (tx_chains_num <= 2)
1667 break;
1668
1669 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1670 break;
1671
1672 memcpy(search_tbl, tbl, sz);
1673 if (rs_toggle_antenna(valid_tx_ant,
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001674 &search_tbl->current_rate, search_tbl)) {
1675 update_search_tbl_counter = 1;
Guy Cohen3110bef2008-09-09 10:54:54 +08001676 goto out;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001677 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001678 break;
1679 case IWL_MIMO2_SWITCH_SISO_A:
1680 case IWL_MIMO2_SWITCH_SISO_B:
1681 case IWL_MIMO2_SWITCH_SISO_C:
Tomas Winklere1623442009-01-27 14:27:56 -08001682 IWL_DEBUG_RATE(priv, "LQ: MIMO2 switch to SISO\n");
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001683
Ben Cahill77626352007-11-29 11:09:44 +08001684 /* Set up new search table for SISO */
Zhu Yib481de92007-09-25 17:54:57 -07001685 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001686
Guy Cohen3110bef2008-09-09 10:54:54 +08001687 if (tbl->action == IWL_MIMO2_SWITCH_SISO_A)
Guy Cohenfde0db32008-04-21 15:42:01 -07001688 search_tbl->ant_type = ANT_A;
Guy Cohen3110bef2008-09-09 10:54:54 +08001689 else if (tbl->action == IWL_MIMO2_SWITCH_SISO_B)
Guy Cohenfde0db32008-04-21 15:42:01 -07001690 search_tbl->ant_type = ANT_B;
Guy Cohen3110bef2008-09-09 10:54:54 +08001691 else
1692 search_tbl->ant_type = ANT_C;
1693
1694 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1695 break;
Zhu Yib481de92007-09-25 17:54:57 -07001696
Tomas Winklerc33104f2008-01-14 17:46:21 -08001697 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001698 search_tbl, index);
Guy Cohen3110bef2008-09-09 10:54:54 +08001699 if (!ret)
Zhu Yib481de92007-09-25 17:54:57 -07001700 goto out;
Guy Cohen3110bef2008-09-09 10:54:54 +08001701
Zhu Yib481de92007-09-25 17:54:57 -07001702 break;
1703
Guy Cohen3110bef2008-09-09 10:54:54 +08001704 case IWL_MIMO2_SWITCH_GI:
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001705 if (!tbl->is_ht40 && !(ht_cap->cap &
1706 IEEE80211_HT_CAP_SGI_20))
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001707 break;
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001708 if (tbl->is_ht40 && !(ht_cap->cap &
1709 IEEE80211_HT_CAP_SGI_40))
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001710 break;
1711
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001712 IWL_DEBUG_RATE(priv, "LQ: MIMO2 toggle SGI/NGI\n");
1713
1714 /* Set up new search table for MIMO2 */
1715 memcpy(search_tbl, tbl, sz);
1716 search_tbl->is_SGI = !tbl->is_SGI;
1717 rs_set_expected_tpt_table(lq_sta, search_tbl);
1718 /*
1719 * If active table already uses the fastest possible
1720 * modulation (dual stream with short guard interval),
1721 * and it's working well, there's no need to look
1722 * for a better type of modulation!
1723 */
1724 if (tbl->is_SGI) {
1725 s32 tpt = lq_sta->last_tpt / 100;
1726 if (tpt >= search_tbl->expected_tpt[index])
1727 break;
1728 }
1729 search_tbl->current_rate =
1730 rate_n_flags_from_tbl(priv, search_tbl,
1731 index, is_green);
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001732 update_search_tbl_counter = 1;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001733 goto out;
1734
1735 case IWL_MIMO2_SWITCH_MIMO3_ABC:
1736 IWL_DEBUG_RATE(priv, "LQ: MIMO2 switch to MIMO3\n");
1737 memcpy(search_tbl, tbl, sz);
1738 search_tbl->is_SGI = 0;
1739 search_tbl->ant_type = ANT_ABC;
1740
1741 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1742 break;
1743
1744 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1745 search_tbl, index);
1746 if (!ret)
1747 goto out;
1748
1749 break;
1750 }
1751 tbl->action++;
1752 if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
1753 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
1754
1755 if (tbl->action == start_action)
1756 break;
1757 }
1758 search_tbl->lq_type = LQ_NONE;
1759 return 0;
1760 out:
1761 lq_sta->search_better_tbl = 1;
1762 tbl->action++;
1763 if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
1764 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001765 if (update_search_tbl_counter)
1766 search_tbl->action = tbl->action;
1767
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001768 return 0;
1769
1770}
1771
1772/*
1773 * Try to switch to new modulation mode from MIMO3
1774 */
1775static int rs_move_mimo3_to_other(struct iwl_priv *priv,
1776 struct iwl_lq_sta *lq_sta,
1777 struct ieee80211_conf *conf,
1778 struct ieee80211_sta *sta, int index)
1779{
1780 s8 is_green = lq_sta->is_green;
1781 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1782 struct iwl_scale_tbl_info *search_tbl =
1783 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1784 struct iwl_rate_scale_data *window = &(tbl->win[index]);
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001785 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001786 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1787 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1788 u8 start_action = tbl->action;
1789 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1790 u8 tx_chains_num = priv->hw_params.tx_chains_num;
1791 int ret;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001792 u8 update_search_tbl_counter = 0;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001793
Johannes Berg3ad3b922009-08-07 15:41:48 -07001794 if ((iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE) &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001795 (tbl->action < IWL_MIMO3_SWITCH_SISO_A ||
1796 tbl->action > IWL_MIMO3_SWITCH_SISO_C)) {
1797 /* switch in SISO */
1798 tbl->action = IWL_MIMO3_SWITCH_SISO_A;
1799 }
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001800 for (;;) {
1801 lq_sta->action_counter++;
1802 switch (tbl->action) {
1803 case IWL_MIMO3_SWITCH_ANTENNA1:
1804 case IWL_MIMO3_SWITCH_ANTENNA2:
1805 IWL_DEBUG_RATE(priv, "LQ: MIMO3 toggle Antennas\n");
1806
1807 if (tx_chains_num <= 3)
1808 break;
1809
1810 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1811 break;
1812
1813 memcpy(search_tbl, tbl, sz);
1814 if (rs_toggle_antenna(valid_tx_ant,
1815 &search_tbl->current_rate, search_tbl))
1816 goto out;
1817 break;
1818 case IWL_MIMO3_SWITCH_SISO_A:
1819 case IWL_MIMO3_SWITCH_SISO_B:
1820 case IWL_MIMO3_SWITCH_SISO_C:
1821 IWL_DEBUG_RATE(priv, "LQ: MIMO3 switch to SISO\n");
1822
1823 /* Set up new search table for SISO */
1824 memcpy(search_tbl, tbl, sz);
1825
1826 if (tbl->action == IWL_MIMO3_SWITCH_SISO_A)
1827 search_tbl->ant_type = ANT_A;
1828 else if (tbl->action == IWL_MIMO3_SWITCH_SISO_B)
1829 search_tbl->ant_type = ANT_B;
1830 else
1831 search_tbl->ant_type = ANT_C;
1832
1833 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1834 break;
1835
1836 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
1837 search_tbl, index);
1838 if (!ret)
1839 goto out;
1840
1841 break;
1842
1843 case IWL_MIMO3_SWITCH_MIMO2_AB:
1844 case IWL_MIMO3_SWITCH_MIMO2_AC:
1845 case IWL_MIMO3_SWITCH_MIMO2_BC:
1846 IWL_DEBUG_RATE(priv, "LQ: MIMO3 switch to MIMO2\n");
1847
1848 memcpy(search_tbl, tbl, sz);
1849 search_tbl->is_SGI = 0;
1850 if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AB)
1851 search_tbl->ant_type = ANT_AB;
1852 else if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AC)
1853 search_tbl->ant_type = ANT_AC;
1854 else
1855 search_tbl->ant_type = ANT_BC;
1856
1857 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1858 break;
1859
1860 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
1861 search_tbl, index);
1862 if (!ret)
1863 goto out;
1864
1865 break;
1866
1867 case IWL_MIMO3_SWITCH_GI:
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001868 if (!tbl->is_ht40 && !(ht_cap->cap &
1869 IEEE80211_HT_CAP_SGI_20))
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001870 break;
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001871 if (tbl->is_ht40 && !(ht_cap->cap &
1872 IEEE80211_HT_CAP_SGI_40))
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001873 break;
1874
1875 IWL_DEBUG_RATE(priv, "LQ: MIMO3 toggle SGI/NGI\n");
Ben Cahill77626352007-11-29 11:09:44 +08001876
1877 /* Set up new search table for MIMO */
Zhu Yib481de92007-09-25 17:54:57 -07001878 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001879 search_tbl->is_SGI = !tbl->is_SGI;
Guy Coheneecd6e52008-04-23 17:14:58 -07001880 rs_set_expected_tpt_table(lq_sta, search_tbl);
Ben Cahill77626352007-11-29 11:09:44 +08001881 /*
1882 * If active table already uses the fastest possible
1883 * modulation (dual stream with short guard interval),
1884 * and it's working well, there's no need to look
1885 * for a better type of modulation!
1886 */
Guy Cohen39e88502008-04-23 17:14:57 -07001887 if (tbl->is_SGI) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001888 s32 tpt = lq_sta->last_tpt / 100;
Guy Cohen39e88502008-04-23 17:14:57 -07001889 if (tpt >= search_tbl->expected_tpt[index])
1890 break;
Zhu Yib481de92007-09-25 17:54:57 -07001891 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001892 search_tbl->current_rate =
1893 rate_n_flags_from_tbl(priv, search_tbl,
1894 index, is_green);
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001895 update_search_tbl_counter = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001896 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07001897 }
1898 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001899 if (tbl->action > IWL_MIMO3_SWITCH_GI)
1900 tbl->action = IWL_MIMO3_SWITCH_ANTENNA1;
Zhu Yib481de92007-09-25 17:54:57 -07001901
1902 if (tbl->action == start_action)
1903 break;
1904 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001905 search_tbl->lq_type = LQ_NONE;
Zhu Yib481de92007-09-25 17:54:57 -07001906 return 0;
1907 out:
Guy Cohen3110bef2008-09-09 10:54:54 +08001908 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001909 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;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001912 if (update_search_tbl_counter)
1913 search_tbl->action = tbl->action;
1914
Zhu Yib481de92007-09-25 17:54:57 -07001915 return 0;
1916
1917}
1918
Ben Cahill77626352007-11-29 11:09:44 +08001919/*
1920 * Check whether we should continue using same modulation mode, or
1921 * begin search for a new mode, based on:
1922 * 1) # tx successes or failures while using this mode
1923 * 2) # times calling this function
1924 * 3) elapsed time in this mode (not used, for now)
1925 */
Tomas Winklere227cea2008-07-18 13:53:05 +08001926static void rs_stay_in_table(struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07001927{
Tomas Winklere227cea2008-07-18 13:53:05 +08001928 struct iwl_scale_tbl_info *tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001929 int i;
1930 int active_tbl;
1931 int flush_interval_passed = 0;
Ester Kummerbf403db2008-05-05 10:22:40 +08001932 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07001933
Ester Kummerbf403db2008-05-05 10:22:40 +08001934 priv = lq_sta->drv;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001935 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001936
Tomas Winklerc33104f2008-01-14 17:46:21 -08001937 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07001938
Ben Cahill77626352007-11-29 11:09:44 +08001939 /* If we've been disallowing search, see if we should now allow it */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001940 if (lq_sta->stay_in_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07001941
Ben Cahill77626352007-11-29 11:09:44 +08001942 /* Elapsed time using current modulation mode */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001943 if (lq_sta->flush_timer)
Zhu Yib481de92007-09-25 17:54:57 -07001944 flush_interval_passed =
Mohamed Abbas447fee72009-04-20 14:37:02 -07001945 time_after(jiffies,
1946 (unsigned long)(lq_sta->flush_timer +
Zhu Yib481de92007-09-25 17:54:57 -07001947 IWL_RATE_SCALE_FLUSH_INTVL));
1948
Ben Cahill77626352007-11-29 11:09:44 +08001949 /*
1950 * Check if we should allow search for new modulation mode.
1951 * If many frames have failed or succeeded, or we've used
1952 * this same modulation for a long time, allow search, and
1953 * reset history stats that keep track of whether we should
1954 * allow a new search. Also (below) reset all bitmaps and
1955 * stats in active history.
1956 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001957 if ((lq_sta->total_failed > lq_sta->max_failure_limit) ||
1958 (lq_sta->total_success > lq_sta->max_success_limit) ||
1959 ((!lq_sta->search_better_tbl) && (lq_sta->flush_timer)
Zhu Yib481de92007-09-25 17:54:57 -07001960 && (flush_interval_passed))) {
Tomas Winklere1623442009-01-27 14:27:56 -08001961 IWL_DEBUG_RATE(priv, "LQ: stay is expired %d %d %d\n:",
Tomas Winklerc33104f2008-01-14 17:46:21 -08001962 lq_sta->total_failed,
1963 lq_sta->total_success,
Zhu Yib481de92007-09-25 17:54:57 -07001964 flush_interval_passed);
Ben Cahill77626352007-11-29 11:09:44 +08001965
1966 /* Allow search for new mode */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001967 lq_sta->stay_in_tbl = 0; /* only place reset */
1968 lq_sta->total_failed = 0;
1969 lq_sta->total_success = 0;
1970 lq_sta->flush_timer = 0;
Ben Cahill77626352007-11-29 11:09:44 +08001971
1972 /*
1973 * Else if we've used this modulation mode enough repetitions
1974 * (regardless of elapsed time or success/failure), reset
1975 * history bitmaps and rate-specific stats for all rates in
1976 * active table.
1977 */
Mohamed Abbas403ab562007-11-06 22:06:25 -08001978 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001979 lq_sta->table_count++;
1980 if (lq_sta->table_count >=
1981 lq_sta->table_count_limit) {
1982 lq_sta->table_count = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001983
Tomas Winklere1623442009-01-27 14:27:56 -08001984 IWL_DEBUG_RATE(priv, "LQ: stay in table clear win\n");
Zhu Yib481de92007-09-25 17:54:57 -07001985 for (i = 0; i < IWL_RATE_COUNT; i++)
1986 rs_rate_scale_clear_window(
1987 &(tbl->win[i]));
1988 }
1989 }
1990
Ben Cahill77626352007-11-29 11:09:44 +08001991 /* If transitioning to allow "search", reset all history
1992 * bitmaps and stats in active table (this will become the new
1993 * "search" table). */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001994 if (!lq_sta->stay_in_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07001995 for (i = 0; i < IWL_RATE_COUNT; i++)
1996 rs_rate_scale_clear_window(&(tbl->win[i]));
1997 }
1998 }
1999}
2000
Ben Cahill77626352007-11-29 11:09:44 +08002001/*
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07002002 * setup rate table in uCode
2003 * return rate_n_flags as used in the table
2004 */
2005static u32 rs_update_rate_tbl(struct iwl_priv *priv,
2006 struct iwl_lq_sta *lq_sta,
2007 struct iwl_scale_tbl_info *tbl,
2008 int index, u8 is_green)
2009{
2010 u32 rate;
2011
2012 /* Update uCode's rate table. */
2013 rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
2014 rs_fill_link_cmd(priv, lq_sta, rate);
2015 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
2016
2017 return rate;
2018}
2019
2020/*
Ben Cahill77626352007-11-29 11:09:44 +08002021 * Do rate scaling and search for new modulation mode.
2022 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002023static void rs_rate_scale_perform(struct iwl_priv *priv,
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002024 struct sk_buff *skb,
Johannes Berg4b7679a2008-09-18 18:14:18 +02002025 struct ieee80211_sta *sta,
2026 struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002027{
Johannes Berg4b7679a2008-09-18 18:14:18 +02002028 struct ieee80211_hw *hw = priv->hw;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002029 struct ieee80211_conf *conf = &hw->conf;
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002030 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2031 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002032 int low = IWL_RATE_INVALID;
2033 int high = IWL_RATE_INVALID;
2034 int index;
2035 int i;
Tomas Winklere227cea2008-07-18 13:53:05 +08002036 struct iwl_rate_scale_data *window = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002037 int current_tpt = IWL_INVALID_VALUE;
2038 int low_tpt = IWL_INVALID_VALUE;
2039 int high_tpt = IWL_INVALID_VALUE;
2040 u32 fail_count;
2041 s8 scale_action = 0;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002042 u16 rate_mask;
Zhu Yib481de92007-09-25 17:54:57 -07002043 u8 update_lq = 0;
Tomas Winklere227cea2008-07-18 13:53:05 +08002044 struct iwl_scale_tbl_info *tbl, *tbl1;
Zhu Yib481de92007-09-25 17:54:57 -07002045 u16 rate_scale_index_msk = 0;
Guy Cohen39e88502008-04-23 17:14:57 -07002046 u32 rate;
Zhu Yib481de92007-09-25 17:54:57 -07002047 u8 is_green = 0;
2048 u8 active_tbl = 0;
2049 u8 done_search = 0;
2050 u16 high_low;
Guy Cohena5e8b502008-05-29 16:35:11 +08002051 s32 sr;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002052 u8 tid = MAX_TID_COUNT;
Wey-Yi Guy86b47662009-03-10 14:35:09 -07002053 struct iwl_tid_data *tid_data;
Zhu Yib481de92007-09-25 17:54:57 -07002054
Tomas Winklere1623442009-01-27 14:27:56 -08002055 IWL_DEBUG_RATE(priv, "rate scale calculate new rate for skb\n");
Zhu Yib481de92007-09-25 17:54:57 -07002056
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002057 /* Send management frames and NO_ACK data using lowest rate. */
Tomas Winkler417f1142008-11-12 13:14:06 -08002058 /* TODO: this could probably be improved.. */
2059 if (!ieee80211_is_data(hdr->frame_control) ||
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002060 info->flags & IEEE80211_TX_CTL_NO_ACK)
Zhu Yib481de92007-09-25 17:54:57 -07002061 return;
Zhu Yib481de92007-09-25 17:54:57 -07002062
Johannes Berg4b7679a2008-09-18 18:14:18 +02002063 if (!sta || !lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002064 return;
2065
Johannes Berg4b7679a2008-09-18 18:14:18 +02002066 lq_sta->supp_rates = sta->supp_rates[lq_sta->band];
Zhu Yib481de92007-09-25 17:54:57 -07002067
Ron Rindjunskyfaa29712008-06-12 09:46:58 +08002068 tid = rs_tl_add_packet(lq_sta, hdr);
2069
Ben Cahill77626352007-11-29 11:09:44 +08002070 /*
2071 * Select rate-scale / modulation-mode table to work with in
2072 * the rest of this function: "search" if searching for better
2073 * modulation mode, or "active" if doing rate scaling within a mode.
2074 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002075 if (!lq_sta->search_better_tbl)
2076 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002077 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002078 active_tbl = 1 - lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002079
Tomas Winklerc33104f2008-01-14 17:46:21 -08002080 tbl = &(lq_sta->lq_info[active_tbl]);
Wey-Yi Guy2681b202009-05-21 13:44:22 -07002081 if (is_legacy(tbl->lq_type))
2082 lq_sta->is_green = 0;
2083 else
Daniel C Halperinb2617932009-08-13 13:30:59 -07002084 lq_sta->is_green = rs_use_green(sta, &priv->current_ht_config);
Tomas Winklerc33104f2008-01-14 17:46:21 -08002085 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07002086
Ben Cahill77626352007-11-29 11:09:44 +08002087 /* current tx rate */
Johannes Bergb7e35002008-09-11 02:22:58 +02002088 index = lq_sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002089
Tomas Winklere1623442009-01-27 14:27:56 -08002090 IWL_DEBUG_RATE(priv, "Rate scale index %d for type %d\n", index,
Zhu Yib481de92007-09-25 17:54:57 -07002091 tbl->lq_type);
2092
Ben Cahill77626352007-11-29 11:09:44 +08002093 /* rates available for this association, and for modulation mode */
Guy Coheneecd6e52008-04-23 17:14:58 -07002094 rate_mask = rs_get_supported_rates(lq_sta, hdr, tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -07002095
Tomas Winklere1623442009-01-27 14:27:56 -08002096 IWL_DEBUG_RATE(priv, "mask 0x%04X \n", rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07002097
2098 /* mask with station rate restriction */
2099 if (is_legacy(tbl->lq_type)) {
Johannes Berg8318d782008-01-24 19:38:38 +01002100 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Ben Cahill77626352007-11-29 11:09:44 +08002101 /* supp_rates has no CCK bits in A mode */
Zhu Yib481de92007-09-25 17:54:57 -07002102 rate_scale_index_msk = (u16) (rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -08002103 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
Zhu Yib481de92007-09-25 17:54:57 -07002104 else
2105 rate_scale_index_msk = (u16) (rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -08002106 lq_sta->supp_rates);
Zhu Yib481de92007-09-25 17:54:57 -07002107
2108 } else
2109 rate_scale_index_msk = rate_mask;
2110
2111 if (!rate_scale_index_msk)
2112 rate_scale_index_msk = rate_mask;
2113
Guy Cohen07bc28e2008-04-23 17:15:03 -07002114 if (!((1 << index) & rate_scale_index_msk)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002115 IWL_ERR(priv, "Current Rate is not valid\n");
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07002116 if (lq_sta->search_better_tbl) {
2117 /* revert to active table if search table is not valid*/
2118 tbl->lq_type = LQ_NONE;
2119 lq_sta->search_better_tbl = 0;
2120 tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
2121 /* get "active" rate info */
2122 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
2123 rate = rs_update_rate_tbl(priv, lq_sta,
2124 tbl, index, is_green);
2125 }
Guy Cohen07bc28e2008-04-23 17:15:03 -07002126 return;
Zhu Yib481de92007-09-25 17:54:57 -07002127 }
2128
Ben Cahill77626352007-11-29 11:09:44 +08002129 /* Get expected throughput table and history window for current rate */
Guy Cohen07bc28e2008-04-23 17:15:03 -07002130 if (!tbl->expected_tpt) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002131 IWL_ERR(priv, "tbl->expected_tpt is NULL\n");
Guy Cohen07bc28e2008-04-23 17:15:03 -07002132 return;
2133 }
Zhu Yib481de92007-09-25 17:54:57 -07002134
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002135 /* force user max rate if set by user */
2136 if ((lq_sta->max_rate_idx != -1) &&
2137 (lq_sta->max_rate_idx < index)) {
2138 index = lq_sta->max_rate_idx;
2139 update_lq = 1;
2140 window = &(tbl->win[index]);
2141 goto lq_update;
2142 }
2143
Zhu Yib481de92007-09-25 17:54:57 -07002144 window = &(tbl->win[index]);
2145
Ben Cahill77626352007-11-29 11:09:44 +08002146 /*
2147 * If there is not enough history to calculate actual average
2148 * throughput, keep analyzing results of more tx frames, without
2149 * changing rate or mode (bypass most of the rest of this function).
2150 * Set up new rate table in uCode only if old rate is not supported
2151 * in current association (use new rate found above).
2152 */
Zhu Yib481de92007-09-25 17:54:57 -07002153 fail_count = window->counter - window->success_counter;
Guy Cohen07bc28e2008-04-23 17:15:03 -07002154 if ((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
2155 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002156 IWL_DEBUG_RATE(priv, "LQ: still below TH. succ=%d total=%d "
Zhu Yib481de92007-09-25 17:54:57 -07002157 "for index %d\n",
2158 window->success_counter, window->counter, index);
Ben Cahill77626352007-11-29 11:09:44 +08002159
2160 /* Can't calculate this yet; not enough history */
Zhu Yib481de92007-09-25 17:54:57 -07002161 window->average_tpt = IWL_INVALID_VALUE;
Ben Cahill77626352007-11-29 11:09:44 +08002162
2163 /* Should we stay with this modulation mode,
2164 * or search for a new one? */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002165 rs_stay_in_table(lq_sta);
Ben Cahill77626352007-11-29 11:09:44 +08002166
Zhu Yib481de92007-09-25 17:54:57 -07002167 goto out;
Guy Cohen3110bef2008-09-09 10:54:54 +08002168 }
Zhu Yib481de92007-09-25 17:54:57 -07002169
Ben Cahill77626352007-11-29 11:09:44 +08002170 /* Else we have enough samples; calculate estimate of
2171 * actual average throughput */
Guy Cohen3110bef2008-09-09 10:54:54 +08002172
2173 BUG_ON(window->average_tpt != ((window->success_ratio *
2174 tbl->expected_tpt[index] + 64) / 128));
Zhu Yib481de92007-09-25 17:54:57 -07002175
Ben Cahill77626352007-11-29 11:09:44 +08002176 /* If we are searching for better modulation mode, check success. */
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002177 if (lq_sta->search_better_tbl &&
Johannes Berg3ad3b922009-08-07 15:41:48 -07002178 (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_MULTI)) {
Ben Cahill77626352007-11-29 11:09:44 +08002179 /* If good success, continue using the "search" mode;
2180 * no need to send new link quality command, since we're
2181 * continuing to use the setup that we've been trying. */
Guy Cohen07bc28e2008-04-23 17:15:03 -07002182 if (window->average_tpt > lq_sta->last_tpt) {
2183
Tomas Winklere1623442009-01-27 14:27:56 -08002184 IWL_DEBUG_RATE(priv, "LQ: SWITCHING TO NEW TABLE "
Guy Cohen07bc28e2008-04-23 17:15:03 -07002185 "suc=%d cur-tpt=%d old-tpt=%d\n",
2186 window->success_ratio,
2187 window->average_tpt,
2188 lq_sta->last_tpt);
2189
2190 if (!is_legacy(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08002191 lq_sta->enable_counter = 1;
Guy Cohen07bc28e2008-04-23 17:15:03 -07002192
Ben Cahill77626352007-11-29 11:09:44 +08002193 /* Swap tables; "search" becomes "active" */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002194 lq_sta->active_tbl = active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002195 current_tpt = window->average_tpt;
Ben Cahill77626352007-11-29 11:09:44 +08002196
2197 /* Else poor success; go back to mode in "active" table */
Zhu Yib481de92007-09-25 17:54:57 -07002198 } else {
Guy Cohen07bc28e2008-04-23 17:15:03 -07002199
Tomas Winklere1623442009-01-27 14:27:56 -08002200 IWL_DEBUG_RATE(priv, "LQ: GOING BACK TO THE OLD TABLE "
Guy Cohen07bc28e2008-04-23 17:15:03 -07002201 "suc=%d cur-tpt=%d old-tpt=%d\n",
2202 window->success_ratio,
2203 window->average_tpt,
2204 lq_sta->last_tpt);
2205
Ben Cahill77626352007-11-29 11:09:44 +08002206 /* Nullify "search" table */
Zhu Yib481de92007-09-25 17:54:57 -07002207 tbl->lq_type = LQ_NONE;
Ben Cahill77626352007-11-29 11:09:44 +08002208
2209 /* Revert to "active" table */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002210 active_tbl = lq_sta->active_tbl;
2211 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07002212
Ben Cahill77626352007-11-29 11:09:44 +08002213 /* Revert to "active" rate and throughput info */
Tomas Winklere7d326ac2008-06-12 09:47:11 +08002214 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
Tomas Winklerc33104f2008-01-14 17:46:21 -08002215 current_tpt = lq_sta->last_tpt;
Ben Cahill77626352007-11-29 11:09:44 +08002216
2217 /* Need to set up a new rate table in uCode */
2218 update_lq = 1;
Zhu Yib481de92007-09-25 17:54:57 -07002219 }
Ben Cahill77626352007-11-29 11:09:44 +08002220
2221 /* Either way, we've made a decision; modulation mode
2222 * search is done, allow rate adjustment next time. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002223 lq_sta->search_better_tbl = 0;
Ben Cahill77626352007-11-29 11:09:44 +08002224 done_search = 1; /* Don't switch modes below! */
Zhu Yib481de92007-09-25 17:54:57 -07002225 goto lq_update;
2226 }
2227
Ben Cahill77626352007-11-29 11:09:44 +08002228 /* (Else) not in search of better modulation mode, try for better
2229 * starting rate, while staying in this mode. */
Ester Kummerbf403db2008-05-05 10:22:40 +08002230 high_low = rs_get_adjacent_rate(priv, index, rate_scale_index_msk,
Zhu Yib481de92007-09-25 17:54:57 -07002231 tbl->lq_type);
2232 low = high_low & 0xff;
2233 high = (high_low >> 8) & 0xff;
2234
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002235 /* If user set max rate, dont allow higher than user constrain */
2236 if ((lq_sta->max_rate_idx != -1) &&
2237 (lq_sta->max_rate_idx < high))
2238 high = IWL_RATE_INVALID;
2239
Guy Cohena5e8b502008-05-29 16:35:11 +08002240 sr = window->success_ratio;
2241
Ben Cahill77626352007-11-29 11:09:44 +08002242 /* Collect measured throughputs for current and adjacent rates */
Zhu Yib481de92007-09-25 17:54:57 -07002243 current_tpt = window->average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07002244 if (low != IWL_RATE_INVALID)
2245 low_tpt = tbl->win[low].average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07002246 if (high != IWL_RATE_INVALID)
2247 high_tpt = tbl->win[high].average_tpt;
2248
Guy Cohena5e8b502008-05-29 16:35:11 +08002249 scale_action = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002250
Ben Cahill77626352007-11-29 11:09:44 +08002251 /* Too many failures, decrease rate */
Guy Cohena5e8b502008-05-29 16:35:11 +08002252 if ((sr <= IWL_RATE_DECREASE_TH) || (current_tpt == 0)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002253 IWL_DEBUG_RATE(priv, "decrease rate because of low success_ratio\n");
Zhu Yib481de92007-09-25 17:54:57 -07002254 scale_action = -1;
Ben Cahill77626352007-11-29 11:09:44 +08002255
2256 /* No throughput measured yet for adjacent rates; try increase. */
Zhu Yib481de92007-09-25 17:54:57 -07002257 } else if ((low_tpt == IWL_INVALID_VALUE) &&
Guy Cohena5e8b502008-05-29 16:35:11 +08002258 (high_tpt == IWL_INVALID_VALUE)) {
2259
2260 if (high != IWL_RATE_INVALID && sr >= IWL_RATE_INCREASE_TH)
2261 scale_action = 1;
2262 else if (low != IWL_RATE_INVALID)
Wey-Yi Guy8fe72312009-03-10 14:35:10 -07002263 scale_action = 0;
Guy Cohena5e8b502008-05-29 16:35:11 +08002264 }
Ben Cahill77626352007-11-29 11:09:44 +08002265
2266 /* Both adjacent throughputs are measured, but neither one has better
2267 * throughput; we're using the best rate, don't change it! */
Zhu Yib481de92007-09-25 17:54:57 -07002268 else if ((low_tpt != IWL_INVALID_VALUE) &&
2269 (high_tpt != IWL_INVALID_VALUE) &&
2270 (low_tpt < current_tpt) &&
2271 (high_tpt < current_tpt))
2272 scale_action = 0;
Ben Cahill77626352007-11-29 11:09:44 +08002273
2274 /* At least one adjacent rate's throughput is measured,
2275 * and may have better performance. */
Zhu Yib481de92007-09-25 17:54:57 -07002276 else {
Ben Cahill77626352007-11-29 11:09:44 +08002277 /* Higher adjacent rate's throughput is measured */
Zhu Yib481de92007-09-25 17:54:57 -07002278 if (high_tpt != IWL_INVALID_VALUE) {
Ben Cahill77626352007-11-29 11:09:44 +08002279 /* Higher rate has better throughput */
Guy Cohena5e8b502008-05-29 16:35:11 +08002280 if (high_tpt > current_tpt &&
2281 sr >= IWL_RATE_INCREASE_TH) {
Zhu Yib481de92007-09-25 17:54:57 -07002282 scale_action = 1;
Guy Cohena5e8b502008-05-29 16:35:11 +08002283 } else {
Wey-Yi Guy8fe72312009-03-10 14:35:10 -07002284 scale_action = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002285 }
Ben Cahill77626352007-11-29 11:09:44 +08002286
2287 /* Lower adjacent rate's throughput is measured */
Zhu Yib481de92007-09-25 17:54:57 -07002288 } else if (low_tpt != IWL_INVALID_VALUE) {
Ben Cahill77626352007-11-29 11:09:44 +08002289 /* Lower rate has better throughput */
Zhu Yib481de92007-09-25 17:54:57 -07002290 if (low_tpt > current_tpt) {
Tomas Winklere1623442009-01-27 14:27:56 -08002291 IWL_DEBUG_RATE(priv,
2292 "decrease rate because of low tpt\n");
Zhu Yib481de92007-09-25 17:54:57 -07002293 scale_action = -1;
Guy Cohena5e8b502008-05-29 16:35:11 +08002294 } else if (sr >= IWL_RATE_INCREASE_TH) {
Zhu Yib481de92007-09-25 17:54:57 -07002295 scale_action = 1;
Guy Cohena5e8b502008-05-29 16:35:11 +08002296 }
Zhu Yib481de92007-09-25 17:54:57 -07002297 }
2298 }
2299
Ben Cahill77626352007-11-29 11:09:44 +08002300 /* Sanity check; asked for decrease, but success rate or throughput
2301 * has been good at old rate. Don't change it. */
Guy Cohena5e8b502008-05-29 16:35:11 +08002302 if ((scale_action == -1) && (low != IWL_RATE_INVALID) &&
2303 ((sr > IWL_RATE_HIGH_TH) ||
Zhu Yib481de92007-09-25 17:54:57 -07002304 (current_tpt > (100 * tbl->expected_tpt[low]))))
Zhu Yib481de92007-09-25 17:54:57 -07002305 scale_action = 0;
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002306 if (!iwl_ht_enabled(priv) && !is_legacy(tbl->lq_type))
2307 scale_action = -1;
Johannes Berg3ad3b922009-08-07 15:41:48 -07002308 if (iwl_tx_ant_restriction(priv) != IWL_ANT_OK_MULTI &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002309 (is_mimo2(tbl->lq_type) || is_mimo3(tbl->lq_type)))
2310 scale_action = -1;
Zhu Yib481de92007-09-25 17:54:57 -07002311 switch (scale_action) {
2312 case -1:
Ben Cahill77626352007-11-29 11:09:44 +08002313 /* Decrease starting rate, update uCode's rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002314 if (low != IWL_RATE_INVALID) {
2315 update_lq = 1;
2316 index = low;
2317 }
Mohamed Abbas447fee72009-04-20 14:37:02 -07002318
Zhu Yib481de92007-09-25 17:54:57 -07002319 break;
2320 case 1:
Ben Cahill77626352007-11-29 11:09:44 +08002321 /* Increase starting rate, update uCode's rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002322 if (high != IWL_RATE_INVALID) {
2323 update_lq = 1;
2324 index = high;
2325 }
2326
2327 break;
2328 case 0:
Ben Cahill77626352007-11-29 11:09:44 +08002329 /* No change */
Zhu Yib481de92007-09-25 17:54:57 -07002330 default:
2331 break;
2332 }
2333
Tomas Winklere1623442009-01-27 14:27:56 -08002334 IWL_DEBUG_RATE(priv, "choose rate scale index %d action %d low %d "
Zhu Yib481de92007-09-25 17:54:57 -07002335 "high %d type %d\n",
2336 index, scale_action, low, high, tbl->lq_type);
2337
Guy Cohena5e8b502008-05-29 16:35:11 +08002338lq_update:
Ben Cahill77626352007-11-29 11:09:44 +08002339 /* Replace uCode's rate table for the destination station. */
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07002340 if (update_lq)
2341 rate = rs_update_rate_tbl(priv, lq_sta,
2342 tbl, index, is_green);
Ben Cahill77626352007-11-29 11:09:44 +08002343
Johannes Berg3ad3b922009-08-07 15:41:48 -07002344 if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_MULTI) {
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002345 /* Should we stay with this modulation mode,
2346 * or search for a new one? */
2347 rs_stay_in_table(lq_sta);
2348 }
Ben Cahill77626352007-11-29 11:09:44 +08002349 /*
2350 * Search for new modulation mode if we're:
2351 * 1) Not changing rates right now
2352 * 2) Not just finishing up a search
2353 * 3) Allowing a new search
2354 */
Guy Cohen47cfd462008-05-27 11:29:34 +08002355 if (!update_lq && !done_search && !lq_sta->stay_in_tbl && window->counter) {
Ben Cahill77626352007-11-29 11:09:44 +08002356 /* Save current throughput to compare with "search" throughput*/
Tomas Winklerc33104f2008-01-14 17:46:21 -08002357 lq_sta->last_tpt = current_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07002358
Ben Cahill77626352007-11-29 11:09:44 +08002359 /* Select a new "search" modulation mode to try.
2360 * If one is found, set up the new "search" table. */
Zhu Yib481de92007-09-25 17:54:57 -07002361 if (is_legacy(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08002362 rs_move_legacy_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07002363 else if (is_siso(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08002364 rs_move_siso_to_other(priv, lq_sta, conf, sta, index);
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07002365 else if (is_mimo2(tbl->lq_type))
2366 rs_move_mimo2_to_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07002367 else
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07002368 rs_move_mimo3_to_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07002369
Ben Cahill77626352007-11-29 11:09:44 +08002370 /* If new "search" mode was selected, set up in uCode table */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002371 if (lq_sta->search_better_tbl) {
Ben Cahill77626352007-11-29 11:09:44 +08002372 /* Access the "search" table, clear its history. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002373 tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Zhu Yib481de92007-09-25 17:54:57 -07002374 for (i = 0; i < IWL_RATE_COUNT; i++)
2375 rs_rate_scale_clear_window(&(tbl->win[i]));
2376
Ben Cahill77626352007-11-29 11:09:44 +08002377 /* Use new "search" start rate */
Tomas Winklere7d326ac2008-06-12 09:47:11 +08002378 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002379
Tomas Winklere1623442009-01-27 14:27:56 -08002380 IWL_DEBUG_RATE(priv, "Switch current mcs: %X index: %d\n",
Guy Cohen39e88502008-04-23 17:14:57 -07002381 tbl->current_rate, index);
Guy Cohen07bc28e2008-04-23 17:15:03 -07002382 rs_fill_link_cmd(priv, lq_sta, tbl->current_rate);
Tomas Winkler66c73db2008-04-15 16:01:40 -07002383 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
Mohamed Abbas447fee72009-04-20 14:37:02 -07002384 } else
2385 done_search = 1;
2386 }
Zhu Yib481de92007-09-25 17:54:57 -07002387
Mohamed Abbas447fee72009-04-20 14:37:02 -07002388 if (done_search && !lq_sta->stay_in_tbl) {
Ben Cahill77626352007-11-29 11:09:44 +08002389 /* If the "active" (non-search) mode was legacy,
2390 * and we've tried switching antennas,
2391 * but we haven't been able to try HT modes (not available),
2392 * stay with best antenna legacy modulation for a while
2393 * before next round of mode comparisons. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002394 tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
Luis R. Rodriguezde27e642008-12-23 15:58:44 -08002395 if (is_legacy(tbl1->lq_type) && !conf_is_ht(conf) &&
Mohamed Abbasd6e93392009-05-08 13:44:39 -07002396 lq_sta->action_counter > tbl1->max_search) {
Tomas Winklere1623442009-01-27 14:27:56 -08002397 IWL_DEBUG_RATE(priv, "LQ: STAY in legacy table\n");
Ester Kummerbf403db2008-05-05 10:22:40 +08002398 rs_set_stay_in_table(priv, 1, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002399 }
2400
Ben Cahill77626352007-11-29 11:09:44 +08002401 /* If we're in an HT mode, and all 3 mode switch actions
2402 * have been tried and compared, stay in this best modulation
2403 * mode for a while before next round of mode comparisons. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002404 if (lq_sta->enable_counter &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002405 (lq_sta->action_counter >= tbl1->max_search) &&
2406 iwl_ht_enabled(priv)) {
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002407 if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
2408 (lq_sta->tx_agg_tid_en & (1 << tid)) &&
2409 (tid != MAX_TID_COUNT)) {
Wey-Yi Guy86b47662009-03-10 14:35:09 -07002410 tid_data =
2411 &priv->stations[lq_sta->lq.sta_id].tid[tid];
2412 if (tid_data->agg.state == IWL_AGG_OFF) {
2413 IWL_DEBUG_RATE(priv,
2414 "try to aggregate tid %d\n",
2415 tid);
2416 rs_tl_turn_on_agg(priv, tid,
2417 lq_sta, sta);
2418 }
Zhu Yib481de92007-09-25 17:54:57 -07002419 }
Ester Kummerbf403db2008-05-05 10:22:40 +08002420 rs_set_stay_in_table(priv, 0, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002421 }
Zhu Yib481de92007-09-25 17:54:57 -07002422 }
2423
2424out:
Tomas Winkler978785a2008-12-19 10:37:31 +08002425 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
Zhu Yib481de92007-09-25 17:54:57 -07002426 i = index;
Johannes Bergb7e35002008-09-11 02:22:58 +02002427 lq_sta->last_txrate_idx = i;
Zhu Yib481de92007-09-25 17:54:57 -07002428
Zhu Yib481de92007-09-25 17:54:57 -07002429 return;
2430}
2431
2432
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002433static void rs_initialize_lq(struct iwl_priv *priv,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002434 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02002435 struct ieee80211_sta *sta,
2436 struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002437{
Tomas Winklere227cea2008-07-18 13:53:05 +08002438 struct iwl_scale_tbl_info *tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002439 int rate_idx;
Guy Cohenaade00c2008-04-23 17:14:59 -07002440 int i;
Guy Cohen39e88502008-04-23 17:14:57 -07002441 u32 rate;
Daniel C Halperinb2617932009-08-13 13:30:59 -07002442 u8 use_green = rs_use_green(sta, &priv->current_ht_config);
Guy Cohenaade00c2008-04-23 17:14:59 -07002443 u8 active_tbl = 0;
2444 u8 valid_tx_ant;
Zhu Yib481de92007-09-25 17:54:57 -07002445
Johannes Berg4b7679a2008-09-18 18:14:18 +02002446 if (!sta || !lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002447 goto out;
2448
Johannes Bergb7e35002008-09-11 02:22:58 +02002449 i = lq_sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002450
Tomas Winklerc33104f2008-01-14 17:46:21 -08002451 if ((lq_sta->lq.sta_id == 0xff) &&
Johannes Berg05c914f2008-09-11 00:01:58 +02002452 (priv->iw_mode == NL80211_IFTYPE_ADHOC))
Zhu Yib481de92007-09-25 17:54:57 -07002453 goto out;
2454
Guy Cohenaade00c2008-04-23 17:14:59 -07002455 valid_tx_ant = priv->hw_params.valid_tx_ant;
2456
Tomas Winklerc33104f2008-01-14 17:46:21 -08002457 if (!lq_sta->search_better_tbl)
2458 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002459 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002460 active_tbl = 1 - lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002461
Tomas Winklerc33104f2008-01-14 17:46:21 -08002462 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07002463
2464 if ((i < 0) || (i >= IWL_RATE_COUNT))
2465 i = 0;
2466
Tomas Winkler1826dcc2008-05-15 13:54:02 +08002467 rate = iwl_rates[i].plcp;
Winkler, Tomaseb48dca2008-10-29 14:05:48 -07002468 tbl->ant_type = first_antenna(valid_tx_ant);
2469 rate |= tbl->ant_type << RATE_MCS_ANT_POS;
Zhu Yib481de92007-09-25 17:54:57 -07002470
2471 if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
Guy Cohen39e88502008-04-23 17:14:57 -07002472 rate |= RATE_MCS_CCK_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002473
Guy Cohen39e88502008-04-23 17:14:57 -07002474 rs_get_tbl_info_from_mcs(rate, priv->band, tbl, &rate_idx);
Guy Cohenaade00c2008-04-23 17:14:59 -07002475 if (!rs_is_valid_ant(valid_tx_ant, tbl->ant_type))
2476 rs_toggle_antenna(valid_tx_ant, &rate, tbl);
Zhu Yib481de92007-09-25 17:54:57 -07002477
Tomas Winkler978785a2008-12-19 10:37:31 +08002478 rate = rate_n_flags_from_tbl(priv, tbl, rate_idx, use_green);
Guy Cohen39e88502008-04-23 17:14:57 -07002479 tbl->current_rate = rate;
Guy Coheneecd6e52008-04-23 17:14:58 -07002480 rs_set_expected_tpt_table(lq_sta, tbl);
Guy Cohen07bc28e2008-04-23 17:15:03 -07002481 rs_fill_link_cmd(NULL, lq_sta, rate);
Tomas Winkler66c73db2008-04-15 16:01:40 -07002482 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07002483 out:
2484 return;
2485}
2486
Johannes Berge6a98542008-10-21 12:40:02 +02002487static void rs_get_rate(void *priv_r, struct ieee80211_sta *sta, void *priv_sta,
2488 struct ieee80211_tx_rate_control *txrc)
Zhu Yib481de92007-09-25 17:54:57 -07002489{
2490
Johannes Berge6a98542008-10-21 12:40:02 +02002491 struct sk_buff *skb = txrc->skb;
2492 struct ieee80211_supported_band *sband = txrc->sband;
Johannes Berg4b7679a2008-09-18 18:14:18 +02002493 struct iwl_priv *priv = (struct iwl_priv *)priv_r;
2494 struct ieee80211_conf *conf = &priv->hw->conf;
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002495 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Zhu Yib481de92007-09-25 17:54:57 -07002496 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berge6a98542008-10-21 12:40:02 +02002497 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Tomas Winkler417f1142008-11-12 13:14:06 -08002498 struct iwl_lq_sta *lq_sta = priv_sta;
2499 int rate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002500
Tomas Winklere1623442009-01-27 14:27:56 -08002501 IWL_DEBUG_RATE_LIMIT(priv, "rate scale calculate new rate for skb\n");
Zhu Yib481de92007-09-25 17:54:57 -07002502
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002503 /* Get max rate if user set max rate */
2504 if (lq_sta) {
2505 lq_sta->max_rate_idx = txrc->max_rate_idx;
2506 if ((sband->band == IEEE80211_BAND_5GHZ) &&
2507 (lq_sta->max_rate_idx != -1))
2508 lq_sta->max_rate_idx += IWL_FIRST_OFDM_RATE;
2509 if ((lq_sta->max_rate_idx < 0) ||
2510 (lq_sta->max_rate_idx >= IWL_RATE_COUNT))
2511 lq_sta->max_rate_idx = -1;
2512 }
2513
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002514 /* Send management frames and NO_ACK data using lowest rate. */
Luis R. Rodriguez4c6d4f52009-07-16 10:05:41 -07002515 if (rate_control_send_low(sta, priv_sta, txrc))
Johannes Berg4b7679a2008-09-18 18:14:18 +02002516 return;
Zhu Yib481de92007-09-25 17:54:57 -07002517
Tomas Winkler417f1142008-11-12 13:14:06 -08002518 rate_idx = lq_sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002519
Johannes Berg05c914f2008-09-11 00:01:58 +02002520 if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) &&
Tomas Winklerc33104f2008-01-14 17:46:21 -08002521 !lq_sta->ibss_sta_added) {
Tomas Winklerc587de02009-06-03 11:44:07 -07002522 u8 sta_id = iwl_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002523
2524 if (sta_id == IWL_INVALID_STATION) {
Tomas Winklere1623442009-01-27 14:27:56 -08002525 IWL_DEBUG_RATE(priv, "LQ: ADD station %pM\n",
Johannes Berge1749612008-10-27 15:59:26 -07002526 hdr->addr1);
Tomas Winklerc587de02009-06-03 11:44:07 -07002527 sta_id = iwl_add_station(priv, hdr->addr1,
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002528 false, CMD_ASYNC, ht_cap);
Zhu Yib481de92007-09-25 17:54:57 -07002529 }
2530 if ((sta_id != IWL_INVALID_STATION)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002531 lq_sta->lq.sta_id = sta_id;
2532 lq_sta->lq.rs_table[0].rate_n_flags = 0;
2533 lq_sta->ibss_sta_added = 1;
Johannes Berg4b7679a2008-09-18 18:14:18 +02002534 rs_initialize_lq(priv, conf, sta, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002535 }
Zhu Yib481de92007-09-25 17:54:57 -07002536 }
2537
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002538 if (lq_sta->last_rate_n_flags & RATE_MCS_HT_MSK) {
Tomas Winkler417f1142008-11-12 13:14:06 -08002539 rate_idx -= IWL_FIRST_OFDM_RATE;
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002540 /* 6M and 9M shared same MCS index */
2541 rate_idx = (rate_idx > 0) ? (rate_idx - 1) : 0;
2542 if (rs_extract_rate(lq_sta->last_rate_n_flags) >=
2543 IWL_RATE_MIMO3_6M_PLCP)
2544 rate_idx = rate_idx + (2 * MCS_INDEX_PER_STREAM);
2545 else if (rs_extract_rate(lq_sta->last_rate_n_flags) >=
2546 IWL_RATE_MIMO2_6M_PLCP)
2547 rate_idx = rate_idx + MCS_INDEX_PER_STREAM;
2548 info->control.rates[0].flags = IEEE80211_TX_RC_MCS;
2549 if (lq_sta->last_rate_n_flags & RATE_MCS_SGI_MSK)
2550 info->control.rates[0].flags |= IEEE80211_TX_RC_SHORT_GI;
2551 if (lq_sta->last_rate_n_flags & RATE_MCS_DUP_MSK)
2552 info->control.rates[0].flags |= IEEE80211_TX_RC_DUP_DATA;
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07002553 if (lq_sta->last_rate_n_flags & RATE_MCS_HT40_MSK)
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002554 info->control.rates[0].flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
2555 if (lq_sta->last_rate_n_flags & RATE_MCS_GF_MSK)
2556 info->control.rates[0].flags |= IEEE80211_TX_RC_GREEN_FIELD;
2557 } else {
Daniel C Halperin50273092009-08-28 09:44:45 -07002558 /* Check for invalid rates */
2559 if ((rate_idx < 0) || (rate_idx >= IWL_RATE_COUNT_LEGACY) ||
2560 ((sband->band == IEEE80211_BAND_5GHZ) &&
2561 (rate_idx < IWL_FIRST_OFDM_RATE)))
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002562 rate_idx = rate_lowest_index(sband, sta);
Daniel C Halperin50273092009-08-28 09:44:45 -07002563 /* On valid 5 GHz rate, adjust index */
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002564 else if (sband->band == IEEE80211_BAND_5GHZ)
2565 rate_idx -= IWL_FIRST_OFDM_RATE;
Daniel C Halperin7ebaeff2009-08-21 13:34:20 -07002566 info->control.rates[0].flags = 0;
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002567 }
Tomas Winkler417f1142008-11-12 13:14:06 -08002568 info->control.rates[0].idx = rate_idx;
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002569
Zhu Yib481de92007-09-25 17:54:57 -07002570}
2571
Johannes Berg4b7679a2008-09-18 18:14:18 +02002572static void *rs_alloc_sta(void *priv_rate, struct ieee80211_sta *sta,
2573 gfp_t gfp)
Zhu Yib481de92007-09-25 17:54:57 -07002574{
Tomas Winklere227cea2008-07-18 13:53:05 +08002575 struct iwl_lq_sta *lq_sta;
Ester Kummerbf403db2008-05-05 10:22:40 +08002576 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07002577 int i, j;
2578
Ester Kummerbf403db2008-05-05 10:22:40 +08002579 priv = (struct iwl_priv *)priv_rate;
Tomas Winklere1623442009-01-27 14:27:56 -08002580 IWL_DEBUG_RATE(priv, "create station rate scale window\n");
Zhu Yib481de92007-09-25 17:54:57 -07002581
Tomas Winklere227cea2008-07-18 13:53:05 +08002582 lq_sta = kzalloc(sizeof(struct iwl_lq_sta), gfp);
Zhu Yib481de92007-09-25 17:54:57 -07002583
Tomas Winklerc33104f2008-01-14 17:46:21 -08002584 if (lq_sta == NULL)
Zhu Yib481de92007-09-25 17:54:57 -07002585 return NULL;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002586 lq_sta->lq.sta_id = 0xff;
Zhu Yib481de92007-09-25 17:54:57 -07002587
Zhu Yi63fddb92007-09-27 11:27:36 +08002588
Zhu Yib481de92007-09-25 17:54:57 -07002589 for (j = 0; j < LQ_SIZE; j++)
2590 for (i = 0; i < IWL_RATE_COUNT; i++)
Tomas Winkler3ac7f142008-07-21 02:40:14 +03002591 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
Zhu Yib481de92007-09-25 17:54:57 -07002592
Tomas Winklerc33104f2008-01-14 17:46:21 -08002593 return lq_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002594}
2595
Johannes Berg4b7679a2008-09-18 18:14:18 +02002596static void rs_rate_init(void *priv_r, struct ieee80211_supported_band *sband,
2597 struct ieee80211_sta *sta, void *priv_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002598{
2599 int i, j;
Johannes Berg4b7679a2008-09-18 18:14:18 +02002600 struct iwl_priv *priv = (struct iwl_priv *)priv_r;
2601 struct ieee80211_conf *conf = &priv->hw->conf;
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002602 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Tomas Winklere227cea2008-07-18 13:53:05 +08002603 struct iwl_lq_sta *lq_sta = priv_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002604
Tomas Winklerc33104f2008-01-14 17:46:21 -08002605 lq_sta->flush_timer = 0;
Johannes Berg4b7679a2008-09-18 18:14:18 +02002606 lq_sta->supp_rates = sta->supp_rates[sband->band];
Zhu Yib481de92007-09-25 17:54:57 -07002607 for (j = 0; j < LQ_SIZE; j++)
2608 for (i = 0; i < IWL_RATE_COUNT; i++)
Tomas Winkler3ac7f142008-07-21 02:40:14 +03002609 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
Zhu Yib481de92007-09-25 17:54:57 -07002610
Tomas Winklere1623442009-01-27 14:27:56 -08002611 IWL_DEBUG_RATE(priv, "LQ: *** rate scale station global init ***\n");
Zhu Yib481de92007-09-25 17:54:57 -07002612 /* TODO: what is a good starting rate for STA? About middle? Maybe not
2613 * the lowest or the highest rate.. Could consider using RSSI from
2614 * previous packets? Need to have IEEE 802.1X auth succeed immediately
2615 * after assoc.. */
2616
Tomas Winklerc33104f2008-01-14 17:46:21 -08002617 lq_sta->ibss_sta_added = 0;
Johannes Berg05c914f2008-09-11 00:01:58 +02002618 if (priv->iw_mode == NL80211_IFTYPE_AP) {
Tomas Winklerc587de02009-06-03 11:44:07 -07002619 u8 sta_id = iwl_find_station(priv,
Abhijeet Kolekare11bc022009-04-08 11:26:41 -07002620 sta->addr);
Joe Perches0795af52007-10-03 17:59:30 -07002621
Zhu Yib481de92007-09-25 17:54:57 -07002622 /* for IBSS the call are from tasklet */
Tomas Winklere1623442009-01-27 14:27:56 -08002623 IWL_DEBUG_RATE(priv, "LQ: ADD station %pM\n", sta->addr);
Zhu Yib481de92007-09-25 17:54:57 -07002624
2625 if (sta_id == IWL_INVALID_STATION) {
Tomas Winklere1623442009-01-27 14:27:56 -08002626 IWL_DEBUG_RATE(priv, "LQ: ADD station %pM\n", sta->addr);
Tomas Winklerc587de02009-06-03 11:44:07 -07002627 sta_id = iwl_add_station(priv, sta->addr, false,
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002628 CMD_ASYNC, ht_cap);
Zhu Yib481de92007-09-25 17:54:57 -07002629 }
2630 if ((sta_id != IWL_INVALID_STATION)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002631 lq_sta->lq.sta_id = sta_id;
2632 lq_sta->lq.rs_table[0].rate_n_flags = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002633 }
2634 /* FIXME: this is w/a remove it later */
2635 priv->assoc_station_added = 1;
2636 }
2637
Tomas Winklerc33104f2008-01-14 17:46:21 -08002638 lq_sta->is_dup = 0;
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002639 lq_sta->max_rate_idx = -1;
Abbas, Mohamed7d049e52009-01-20 21:33:53 -08002640 lq_sta->missed_rate_counter = IWL_MISSED_RATE_MAX;
Daniel C Halperinb2617932009-08-13 13:30:59 -07002641 lq_sta->is_green = rs_use_green(sta, &priv->current_ht_config);
Guy Cohen39e88502008-04-23 17:14:57 -07002642 lq_sta->active_legacy_rate = priv->active_rate & ~(0x1000);
Tomas Winklerc33104f2008-01-14 17:46:21 -08002643 lq_sta->active_rate_basic = priv->active_rate_basic;
Johannes Berg8318d782008-01-24 19:38:38 +01002644 lq_sta->band = priv->band;
Ben Cahill77626352007-11-29 11:09:44 +08002645 /*
2646 * active_siso_rate mask includes 9 MBits (bit 5), and CCK (bits 0-3),
2647 * supp_rates[] does not; shift to convert format, force 9 MBits off.
2648 */
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002649 lq_sta->active_siso_rate = ht_cap->mcs.rx_mask[0] << 1;
2650 lq_sta->active_siso_rate |= ht_cap->mcs.rx_mask[0] & 0x1;
Guy Cohenfde0db32008-04-21 15:42:01 -07002651 lq_sta->active_siso_rate &= ~((u16)0x2);
2652 lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -07002653
Ben Cahill77626352007-11-29 11:09:44 +08002654 /* Same here */
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002655 lq_sta->active_mimo2_rate = ht_cap->mcs.rx_mask[1] << 1;
2656 lq_sta->active_mimo2_rate |= ht_cap->mcs.rx_mask[1] & 0x1;
Guy Cohenfde0db32008-04-21 15:42:01 -07002657 lq_sta->active_mimo2_rate &= ~((u16)0x2);
2658 lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE;
2659
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002660 lq_sta->active_mimo3_rate = ht_cap->mcs.rx_mask[2] << 1;
2661 lq_sta->active_mimo3_rate |= ht_cap->mcs.rx_mask[2] & 0x1;
Guy Cohenfde0db32008-04-21 15:42:01 -07002662 lq_sta->active_mimo3_rate &= ~((u16)0x2);
2663 lq_sta->active_mimo3_rate <<= IWL_FIRST_OFDM_RATE;
2664
Tomas Winklere1623442009-01-27 14:27:56 -08002665 IWL_DEBUG_RATE(priv, "SISO-RATE=%X MIMO2-RATE=%X MIMO3-RATE=%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002666 lq_sta->active_siso_rate,
Guy Cohenfde0db32008-04-21 15:42:01 -07002667 lq_sta->active_mimo2_rate,
2668 lq_sta->active_mimo3_rate);
2669
Tomas Winklera96a27f2008-10-23 23:48:56 -07002670 /* These values will be overridden later */
Guy Cohen07bc28e2008-04-23 17:15:03 -07002671 lq_sta->lq.general_params.single_stream_ant_msk = ANT_A;
2672 lq_sta->lq.general_params.dual_stream_ant_msk = ANT_AB;
2673
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002674 /* as default allow aggregation for all tids */
2675 lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002676 lq_sta->drv = priv;
Zhu Yib481de92007-09-25 17:54:57 -07002677
Daniel C Halperin50273092009-08-28 09:44:45 -07002678 /* Set last_txrate_idx to lowest rate */
2679 lq_sta->last_txrate_idx = rate_lowest_index(sband, sta);
2680 if (sband->band == IEEE80211_BAND_5GHZ)
2681 lq_sta->last_txrate_idx += IWL_FIRST_OFDM_RATE;
Mohamed Abbasd5e49032008-12-12 08:22:15 -08002682
Johannes Berg4b7679a2008-09-18 18:14:18 +02002683 rs_initialize_lq(priv, conf, sta, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002684}
2685
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002686static void rs_fill_link_cmd(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08002687 struct iwl_lq_sta *lq_sta, u32 new_rate)
Zhu Yib481de92007-09-25 17:54:57 -07002688{
Tomas Winklere227cea2008-07-18 13:53:05 +08002689 struct iwl_scale_tbl_info tbl_type;
Zhu Yib481de92007-09-25 17:54:57 -07002690 int index = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002691 int rate_idx;
Zhu Yi1b696de2007-09-27 11:27:41 +08002692 int repeat_rate = 0;
Guy Cohenaade00c2008-04-23 17:14:59 -07002693 u8 ant_toggle_cnt = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002694 u8 use_ht_possible = 1;
Guy Cohenaade00c2008-04-23 17:14:59 -07002695 u8 valid_tx_ant = 0;
Guy Cohen07bc28e2008-04-23 17:15:03 -07002696 struct iwl_link_quality_cmd *lq_cmd = &lq_sta->lq;
Zhu Yib481de92007-09-25 17:54:57 -07002697
Ben Cahill77626352007-11-29 11:09:44 +08002698 /* Override starting rate (index 0) if needed for debug purposes */
Guy Cohen39e88502008-04-23 17:14:57 -07002699 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Zhu Yi98d7e092007-09-27 11:27:42 +08002700
Guy Cohen39e88502008-04-23 17:14:57 -07002701 /* Interpret new_rate (rate_n_flags) */
Guy Cohenaade00c2008-04-23 17:14:59 -07002702 memset(&tbl_type, 0, sizeof(tbl_type));
Guy Cohen39e88502008-04-23 17:14:57 -07002703 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band,
Zhu Yib481de92007-09-25 17:54:57 -07002704 &tbl_type, &rate_idx);
2705
Ben Cahill77626352007-11-29 11:09:44 +08002706 /* How many times should we repeat the initial rate? */
Zhu Yib481de92007-09-25 17:54:57 -07002707 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07002708 ant_toggle_cnt = 1;
Zhu Yi1b696de2007-09-27 11:27:41 +08002709 repeat_rate = IWL_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002710 } else {
Zhu Yi1b696de2007-09-27 11:27:41 +08002711 repeat_rate = IWL_HT_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002712 }
Zhu Yib481de92007-09-25 17:54:57 -07002713
2714 lq_cmd->general_params.mimo_delimiter =
2715 is_mimo(tbl_type.lq_type) ? 1 : 0;
Ben Cahill77626352007-11-29 11:09:44 +08002716
2717 /* Fill 1st table entry (index 0) */
Guy Cohen39e88502008-04-23 17:14:57 -07002718 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002719
Guy Cohen07bc28e2008-04-23 17:15:03 -07002720 if (num_of_ant(tbl_type.ant_type) == 1) {
2721 lq_cmd->general_params.single_stream_ant_msk =
2722 tbl_type.ant_type;
2723 } else if (num_of_ant(tbl_type.ant_type) == 2) {
2724 lq_cmd->general_params.dual_stream_ant_msk =
2725 tbl_type.ant_type;
2726 } /* otherwise we don't modify the existing value */
Zhu Yib481de92007-09-25 17:54:57 -07002727
2728 index++;
Zhu Yi1b696de2007-09-27 11:27:41 +08002729 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002730
Guy Cohenaade00c2008-04-23 17:14:59 -07002731 if (priv)
2732 valid_tx_ant = priv->hw_params.valid_tx_ant;
2733
Ben Cahill77626352007-11-29 11:09:44 +08002734 /* Fill rest of rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002735 while (index < LINK_QUAL_MAX_RETRY_NUM) {
Ben Cahill77626352007-11-29 11:09:44 +08002736 /* Repeat initial/next rate.
2737 * For legacy IWL_NUMBER_TRY == 1, this loop will not execute.
2738 * For HT IWL_HT_NUMBER_TRY == 3, this executes twice. */
Zhu Yi1b696de2007-09-27 11:27:41 +08002739 while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
Zhu Yib481de92007-09-25 17:54:57 -07002740 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07002741 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2742 ant_toggle_cnt++;
2743 else if (priv &&
2744 rs_toggle_antenna(valid_tx_ant,
2745 &new_rate, &tbl_type))
2746 ant_toggle_cnt = 1;
2747}
Zhu Yi98d7e092007-09-27 11:27:42 +08002748
Ben Cahill77626352007-11-29 11:09:44 +08002749 /* Override next rate if needed for debug purposes */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002750 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Ben Cahill77626352007-11-29 11:09:44 +08002751
2752 /* Fill next table entry */
Zhu Yib481de92007-09-25 17:54:57 -07002753 lq_cmd->rs_table[index].rate_n_flags =
Guy Cohen39e88502008-04-23 17:14:57 -07002754 cpu_to_le32(new_rate);
Zhu Yi1b696de2007-09-27 11:27:41 +08002755 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002756 index++;
2757 }
2758
Guy Cohen39e88502008-04-23 17:14:57 -07002759 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band, &tbl_type,
Zhu Yib481de92007-09-25 17:54:57 -07002760 &rate_idx);
2761
Ben Cahill77626352007-11-29 11:09:44 +08002762 /* Indicate to uCode which entries might be MIMO.
2763 * If initial rate was MIMO, this will finally end up
2764 * as (IWL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
Zhu Yib481de92007-09-25 17:54:57 -07002765 if (is_mimo(tbl_type.lq_type))
2766 lq_cmd->general_params.mimo_delimiter = index;
2767
Ben Cahill77626352007-11-29 11:09:44 +08002768 /* Get next rate */
Guy Cohen39e88502008-04-23 17:14:57 -07002769 new_rate = rs_get_lower_rate(lq_sta, &tbl_type, rate_idx,
2770 use_ht_possible);
Zhu Yib481de92007-09-25 17:54:57 -07002771
Ben Cahill77626352007-11-29 11:09:44 +08002772 /* How many times should we repeat the next rate? */
Zhu Yib481de92007-09-25 17:54:57 -07002773 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07002774 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2775 ant_toggle_cnt++;
2776 else if (priv &&
2777 rs_toggle_antenna(valid_tx_ant,
2778 &new_rate, &tbl_type))
2779 ant_toggle_cnt = 1;
2780
Zhu Yi1b696de2007-09-27 11:27:41 +08002781 repeat_rate = IWL_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002782 } else {
Zhu Yi1b696de2007-09-27 11:27:41 +08002783 repeat_rate = IWL_HT_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002784 }
Zhu Yib481de92007-09-25 17:54:57 -07002785
Ben Cahill77626352007-11-29 11:09:44 +08002786 /* Don't allow HT rates after next pass.
2787 * rs_get_lower_rate() will change type to LQ_A or LQ_G. */
Zhu Yib481de92007-09-25 17:54:57 -07002788 use_ht_possible = 0;
2789
Ben Cahill77626352007-11-29 11:09:44 +08002790 /* Override next rate if needed for debug purposes */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002791 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Ben Cahill77626352007-11-29 11:09:44 +08002792
2793 /* Fill next table entry */
Guy Cohen39e88502008-04-23 17:14:57 -07002794 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002795
2796 index++;
Zhu Yi1b696de2007-09-27 11:27:41 +08002797 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002798 }
2799
Wey-Yi Guy13c33a02009-06-03 11:44:11 -07002800 lq_cmd->agg_params.agg_frame_cnt_limit = LINK_QUAL_AGG_FRAME_LIMIT_MAX;
2801 lq_cmd->agg_params.agg_dis_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
2802 lq_cmd->agg_params.agg_time_limit =
2803 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
Zhu Yib481de92007-09-25 17:54:57 -07002804}
2805
Johannes Berg4b7679a2008-09-18 18:14:18 +02002806static void *rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
Zhu Yib481de92007-09-25 17:54:57 -07002807{
Johannes Berg4b7679a2008-09-18 18:14:18 +02002808 return hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07002809}
2810/* rate scale requires free function to be implemented */
2811static void rs_free(void *priv_rate)
2812{
2813 return;
2814}
2815
Johannes Berg4b7679a2008-09-18 18:14:18 +02002816static void rs_free_sta(void *priv_r, struct ieee80211_sta *sta,
2817 void *priv_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002818{
Tomas Winklere227cea2008-07-18 13:53:05 +08002819 struct iwl_lq_sta *lq_sta = priv_sta;
Rami Rosen45527c22008-10-07 09:50:01 +02002820 struct iwl_priv *priv __maybe_unused = priv_r;
Zhu Yib481de92007-09-25 17:54:57 -07002821
Tomas Winklere1623442009-01-27 14:27:56 -08002822 IWL_DEBUG_RATE(priv, "enter\n");
Tomas Winklerc33104f2008-01-14 17:46:21 -08002823 kfree(lq_sta);
Tomas Winklere1623442009-01-27 14:27:56 -08002824 IWL_DEBUG_RATE(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07002825}
2826
2827
Zhu Yi93dc6462007-09-27 11:27:37 +08002828#ifdef CONFIG_MAC80211_DEBUGFS
Zhu Yi5ae212c2007-09-27 11:27:38 +08002829static int open_file_generic(struct inode *inode, struct file *file)
2830{
2831 file->private_data = inode->i_private;
2832 return 0;
2833}
Tomas Winklere227cea2008-07-18 13:53:05 +08002834static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
2835 u32 *rate_n_flags, int index)
Zhu Yi98d7e092007-09-27 11:27:42 +08002836{
Ester Kummerbf403db2008-05-05 10:22:40 +08002837 struct iwl_priv *priv;
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07002838 u8 valid_tx_ant;
2839 u8 ant_sel_tx;
Ester Kummerbf403db2008-05-05 10:22:40 +08002840
2841 priv = lq_sta->drv;
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07002842 valid_tx_ant = priv->hw_params.valid_tx_ant;
Guy Cohen39e88502008-04-23 17:14:57 -07002843 if (lq_sta->dbg_fixed_rate) {
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07002844 ant_sel_tx =
2845 ((lq_sta->dbg_fixed_rate & RATE_MCS_ANT_ABC_MSK)
2846 >> RATE_MCS_ANT_POS);
2847 if ((valid_tx_ant & ant_sel_tx) == ant_sel_tx) {
Guy Cohen39e88502008-04-23 17:14:57 -07002848 *rate_n_flags = lq_sta->dbg_fixed_rate;
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07002849 IWL_DEBUG_RATE(priv, "Fixed rate ON\n");
Guy Cohen39e88502008-04-23 17:14:57 -07002850 } else {
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07002851 lq_sta->dbg_fixed_rate = 0;
2852 IWL_ERR(priv,
2853 "Invalid antenna selection 0x%X, Valid is 0x%X\n",
2854 ant_sel_tx, valid_tx_ant);
2855 IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
Guy Cohen39e88502008-04-23 17:14:57 -07002856 }
Guy Cohen39e88502008-04-23 17:14:57 -07002857 } else {
Tomas Winklere1623442009-01-27 14:27:56 -08002858 IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
Zhu Yi98d7e092007-09-27 11:27:42 +08002859 }
Zhu Yi98d7e092007-09-27 11:27:42 +08002860}
2861
2862static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
2863 const char __user *user_buf, size_t count, loff_t *ppos)
2864{
Tomas Winklere227cea2008-07-18 13:53:05 +08002865 struct iwl_lq_sta *lq_sta = file->private_data;
Ester Kummerbf403db2008-05-05 10:22:40 +08002866 struct iwl_priv *priv;
Zhu Yi98d7e092007-09-27 11:27:42 +08002867 char buf[64];
2868 int buf_size;
2869 u32 parsed_rate;
2870
Ester Kummerbf403db2008-05-05 10:22:40 +08002871 priv = lq_sta->drv;
Zhu Yi98d7e092007-09-27 11:27:42 +08002872 memset(buf, 0, sizeof(buf));
2873 buf_size = min(count, sizeof(buf) - 1);
2874 if (copy_from_user(buf, user_buf, buf_size))
2875 return -EFAULT;
2876
2877 if (sscanf(buf, "%x", &parsed_rate) == 1)
Guy Cohen39e88502008-04-23 17:14:57 -07002878 lq_sta->dbg_fixed_rate = parsed_rate;
Zhu Yi98d7e092007-09-27 11:27:42 +08002879 else
Guy Cohen39e88502008-04-23 17:14:57 -07002880 lq_sta->dbg_fixed_rate = 0;
Zhu Yi98d7e092007-09-27 11:27:42 +08002881
Guy Cohen39e88502008-04-23 17:14:57 -07002882 lq_sta->active_legacy_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
2883 lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2884 lq_sta->active_mimo2_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2885 lq_sta->active_mimo3_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
Zhu Yi98d7e092007-09-27 11:27:42 +08002886
Tomas Winklere1623442009-01-27 14:27:56 -08002887 IWL_DEBUG_RATE(priv, "sta_id %d rate 0x%X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07002888 lq_sta->lq.sta_id, lq_sta->dbg_fixed_rate);
Zhu Yi98d7e092007-09-27 11:27:42 +08002889
Guy Cohen39e88502008-04-23 17:14:57 -07002890 if (lq_sta->dbg_fixed_rate) {
Guy Cohen07bc28e2008-04-23 17:15:03 -07002891 rs_fill_link_cmd(NULL, lq_sta, lq_sta->dbg_fixed_rate);
Tomas Winkler66c73db2008-04-15 16:01:40 -07002892 iwl_send_lq_cmd(lq_sta->drv, &lq_sta->lq, CMD_ASYNC);
Zhu Yi98d7e092007-09-27 11:27:42 +08002893 }
2894
2895 return count;
2896}
Zhu Yi0209dc12007-09-27 11:27:43 +08002897
Zhu Yi5ae212c2007-09-27 11:27:38 +08002898static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
2899 char __user *user_buf, size_t count, loff_t *ppos)
2900{
Frank Seidela412c802009-03-01 20:25:38 +01002901 char *buff;
Zhu Yi5ae212c2007-09-27 11:27:38 +08002902 int desc = 0;
2903 int i = 0;
Wey-Yi Guy12b96812009-04-08 11:39:27 -07002904 int index = 0;
Frank Seidela412c802009-03-01 20:25:38 +01002905 ssize_t ret;
Zhu Yi5ae212c2007-09-27 11:27:38 +08002906
Tomas Winklere227cea2008-07-18 13:53:05 +08002907 struct iwl_lq_sta *lq_sta = file->private_data;
Wey-Yi Guyd8ae4f52009-03-10 14:35:07 -07002908 struct iwl_priv *priv;
Wey-Yi Guyadb7b5e2009-03-10 14:35:08 -07002909 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002910
Wey-Yi Guyd8ae4f52009-03-10 14:35:07 -07002911 priv = lq_sta->drv;
Frank Seidela412c802009-03-01 20:25:38 +01002912 buff = kmalloc(1024, GFP_KERNEL);
2913 if (!buff)
2914 return -ENOMEM;
2915
Tomas Winklerc33104f2008-01-14 17:46:21 -08002916 desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
Zhu Yi98d7e092007-09-27 11:27:42 +08002917 desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002918 lq_sta->total_failed, lq_sta->total_success,
Guy Cohen39e88502008-04-23 17:14:57 -07002919 lq_sta->active_legacy_rate);
Zhu Yi98d7e092007-09-27 11:27:42 +08002920 desc += sprintf(buff+desc, "fixed rate 0x%X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07002921 lq_sta->dbg_fixed_rate);
Wey-Yi Guyd8ae4f52009-03-10 14:35:07 -07002922 desc += sprintf(buff+desc, "valid_tx_ant %s%s%s\n",
2923 (priv->hw_params.valid_tx_ant & ANT_A) ? "ANT_A," : "",
2924 (priv->hw_params.valid_tx_ant & ANT_B) ? "ANT_B," : "",
2925 (priv->hw_params.valid_tx_ant & ANT_C) ? "ANT_C" : "");
Wey-Yi Guyadb7b5e2009-03-10 14:35:08 -07002926 desc += sprintf(buff+desc, "lq type %s\n",
2927 (is_legacy(tbl->lq_type)) ? "legacy" : "HT");
2928 if (is_Ht(tbl->lq_type)) {
2929 desc += sprintf(buff+desc, " %s",
2930 (is_siso(tbl->lq_type)) ? "SISO" :
2931 ((is_mimo2(tbl->lq_type)) ? "MIMO2" : "MIMO3"));
2932 desc += sprintf(buff+desc, " %s",
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07002933 (tbl->is_ht40) ? "40MHz" : "20MHz");
Wey-Yi Guy2681b202009-05-21 13:44:22 -07002934 desc += sprintf(buff+desc, " %s %s\n", (tbl->is_SGI) ? "SGI" : "",
2935 (lq_sta->is_green) ? "GF enabled" : "");
Wey-Yi Guyadb7b5e2009-03-10 14:35:08 -07002936 }
Wey-Yi Guy12b96812009-04-08 11:39:27 -07002937 desc += sprintf(buff+desc, "last tx rate=0x%X\n",
2938 lq_sta->last_rate_n_flags);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002939 desc += sprintf(buff+desc, "general:"
2940 "flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002941 lq_sta->lq.general_params.flags,
2942 lq_sta->lq.general_params.mimo_delimiter,
2943 lq_sta->lq.general_params.single_stream_ant_msk,
2944 lq_sta->lq.general_params.dual_stream_ant_msk);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002945
2946 desc += sprintf(buff+desc, "agg:"
2947 "time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002948 le16_to_cpu(lq_sta->lq.agg_params.agg_time_limit),
2949 lq_sta->lq.agg_params.agg_dis_start_th,
2950 lq_sta->lq.agg_params.agg_frame_cnt_limit);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002951
2952 desc += sprintf(buff+desc,
2953 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002954 lq_sta->lq.general_params.start_rate_index[0],
2955 lq_sta->lq.general_params.start_rate_index[1],
2956 lq_sta->lq.general_params.start_rate_index[2],
2957 lq_sta->lq.general_params.start_rate_index[3]);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002958
Wey-Yi Guy12b96812009-04-08 11:39:27 -07002959 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
2960 index = iwl_hwrate_to_plcp_idx(
2961 le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags));
2962 if (is_legacy(tbl->lq_type)) {
2963 desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps\n",
2964 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags),
2965 iwl_rate_mcs[index].mbps);
2966 } else {
2967 desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps (%s)\n",
2968 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags),
2969 iwl_rate_mcs[index].mbps, iwl_rate_mcs[index].mcs);
2970 }
2971 }
Zhu Yi5ae212c2007-09-27 11:27:38 +08002972
Frank Seidela412c802009-03-01 20:25:38 +01002973 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2974 kfree(buff);
2975 return ret;
Zhu Yi5ae212c2007-09-27 11:27:38 +08002976}
2977
2978static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
Zhu Yi98d7e092007-09-27 11:27:42 +08002979 .write = rs_sta_dbgfs_scale_table_write,
Zhu Yi5ae212c2007-09-27 11:27:38 +08002980 .read = rs_sta_dbgfs_scale_table_read,
2981 .open = open_file_generic,
2982};
Zhu Yi0209dc12007-09-27 11:27:43 +08002983static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
2984 char __user *user_buf, size_t count, loff_t *ppos)
2985{
Frank Seidela412c802009-03-01 20:25:38 +01002986 char *buff;
Zhu Yi0209dc12007-09-27 11:27:43 +08002987 int desc = 0;
2988 int i, j;
Frank Seidela412c802009-03-01 20:25:38 +01002989 ssize_t ret;
Zhu Yi0209dc12007-09-27 11:27:43 +08002990
Tomas Winklere227cea2008-07-18 13:53:05 +08002991 struct iwl_lq_sta *lq_sta = file->private_data;
Frank Seidela412c802009-03-01 20:25:38 +01002992
2993 buff = kmalloc(1024, GFP_KERNEL);
2994 if (!buff)
2995 return -ENOMEM;
2996
Zhu Yi0209dc12007-09-27 11:27:43 +08002997 for (i = 0; i < LQ_SIZE; i++) {
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07002998 desc += sprintf(buff+desc,
2999 "%s type=%d SGI=%d HT40=%d DUP=%d GF=%d\n"
Zhu Yi0209dc12007-09-27 11:27:43 +08003000 "rate=0x%X\n",
Abhijeet Kolekarc3056062008-11-12 13:14:08 -08003001 lq_sta->active_tbl == i ? "*" : "x",
Tomas Winklerc33104f2008-01-14 17:46:21 -08003002 lq_sta->lq_info[i].lq_type,
3003 lq_sta->lq_info[i].is_SGI,
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07003004 lq_sta->lq_info[i].is_ht40,
Tomas Winklerc33104f2008-01-14 17:46:21 -08003005 lq_sta->lq_info[i].is_dup,
Wey-Yi Guy2681b202009-05-21 13:44:22 -07003006 lq_sta->is_green,
Guy Cohen39e88502008-04-23 17:14:57 -07003007 lq_sta->lq_info[i].current_rate);
Zhu Yi0209dc12007-09-27 11:27:43 +08003008 for (j = 0; j < IWL_RATE_COUNT; j++) {
3009 desc += sprintf(buff+desc,
Tomas Winklerc33104f2008-01-14 17:46:21 -08003010 "counter=%d success=%d %%=%d\n",
3011 lq_sta->lq_info[i].win[j].counter,
3012 lq_sta->lq_info[i].win[j].success_counter,
3013 lq_sta->lq_info[i].win[j].success_ratio);
Zhu Yi0209dc12007-09-27 11:27:43 +08003014 }
3015 }
Frank Seidela412c802009-03-01 20:25:38 +01003016 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
3017 kfree(buff);
3018 return ret;
Zhu Yi0209dc12007-09-27 11:27:43 +08003019}
3020
3021static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
3022 .read = rs_sta_dbgfs_stats_table_read,
3023 .open = open_file_generic,
3024};
Zhu Yi5ae212c2007-09-27 11:27:38 +08003025
Wey-Yi Guy38167452009-05-08 13:44:43 -07003026static ssize_t rs_sta_dbgfs_rate_scale_data_read(struct file *file,
3027 char __user *user_buf, size_t count, loff_t *ppos)
3028{
3029 char buff[120];
3030 int desc = 0;
3031 ssize_t ret;
3032
3033 struct iwl_lq_sta *lq_sta = file->private_data;
3034 struct iwl_priv *priv;
3035 struct iwl_scale_tbl_info *tbl = &lq_sta->lq_info[lq_sta->active_tbl];
3036
3037 priv = lq_sta->drv;
3038
3039 if (is_Ht(tbl->lq_type))
3040 desc += sprintf(buff+desc,
3041 "Bit Rate= %d Mb/s\n",
3042 tbl->expected_tpt[lq_sta->last_txrate_idx]);
3043 else
3044 desc += sprintf(buff+desc,
3045 "Bit Rate= %d Mb/s\n",
3046 iwl_rates[lq_sta->last_txrate_idx].ieee >> 1);
3047 desc += sprintf(buff+desc,
3048 "Signal Level= %d dBm\tNoise Level= %d dBm\n",
3049 priv->last_rx_rssi, priv->last_rx_noise);
3050 desc += sprintf(buff+desc,
3051 "Tsf= 0x%llx\tBeacon time= 0x%08X\n",
3052 priv->last_tsf, priv->last_beacon_time);
3053
3054 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
3055 return ret;
3056}
3057
3058static const struct file_operations rs_sta_dbgfs_rate_scale_data_ops = {
3059 .read = rs_sta_dbgfs_rate_scale_data_read,
3060 .open = open_file_generic,
3061};
3062
Zhu Yi93dc6462007-09-27 11:27:37 +08003063static void rs_add_debugfs(void *priv, void *priv_sta,
3064 struct dentry *dir)
3065{
Tomas Winklere227cea2008-07-18 13:53:05 +08003066 struct iwl_lq_sta *lq_sta = priv_sta;
Tomas Winklerc33104f2008-01-14 17:46:21 -08003067 lq_sta->rs_sta_dbgfs_scale_table_file =
Zhu Yi0209dc12007-09-27 11:27:43 +08003068 debugfs_create_file("rate_scale_table", 0600, dir,
Tomas Winklerc33104f2008-01-14 17:46:21 -08003069 lq_sta, &rs_sta_dbgfs_scale_table_ops);
3070 lq_sta->rs_sta_dbgfs_stats_table_file =
Zhu Yi0209dc12007-09-27 11:27:43 +08003071 debugfs_create_file("rate_stats_table", 0600, dir,
Tomas Winklerc33104f2008-01-14 17:46:21 -08003072 lq_sta, &rs_sta_dbgfs_stats_table_ops);
Wey-Yi Guy38167452009-05-08 13:44:43 -07003073 lq_sta->rs_sta_dbgfs_rate_scale_data_file =
3074 debugfs_create_file("rate_scale_data", 0600, dir,
3075 lq_sta, &rs_sta_dbgfs_rate_scale_data_ops);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02003076 lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file =
3077 debugfs_create_u8("tx_agg_tid_enable", 0600, dir,
3078 &lq_sta->tx_agg_tid_en);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02003079
Zhu Yi93dc6462007-09-27 11:27:37 +08003080}
3081
3082static void rs_remove_debugfs(void *priv, void *priv_sta)
3083{
Tomas Winklere227cea2008-07-18 13:53:05 +08003084 struct iwl_lq_sta *lq_sta = priv_sta;
Tomas Winklerc33104f2008-01-14 17:46:21 -08003085 debugfs_remove(lq_sta->rs_sta_dbgfs_scale_table_file);
3086 debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
Wey-Yi Guy38167452009-05-08 13:44:43 -07003087 debugfs_remove(lq_sta->rs_sta_dbgfs_rate_scale_data_file);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02003088 debugfs_remove(lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file);
Zhu Yi93dc6462007-09-27 11:27:37 +08003089}
3090#endif
3091
Zhu Yib481de92007-09-25 17:54:57 -07003092static struct rate_control_ops rs_ops = {
3093 .module = NULL,
3094 .name = RS_NAME,
3095 .tx_status = rs_tx_status,
3096 .get_rate = rs_get_rate,
3097 .rate_init = rs_rate_init,
Zhu Yib481de92007-09-25 17:54:57 -07003098 .alloc = rs_alloc,
3099 .free = rs_free,
3100 .alloc_sta = rs_alloc_sta,
3101 .free_sta = rs_free_sta,
Zhu Yi93dc6462007-09-27 11:27:37 +08003102#ifdef CONFIG_MAC80211_DEBUGFS
3103 .add_sta_debugfs = rs_add_debugfs,
3104 .remove_sta_debugfs = rs_remove_debugfs,
3105#endif
Zhu Yib481de92007-09-25 17:54:57 -07003106};
3107
Tomas Winklere227cea2008-07-18 13:53:05 +08003108int iwlagn_rate_control_register(void)
Zhu Yib481de92007-09-25 17:54:57 -07003109{
Reinette Chatre897e1cf2008-03-28 16:21:09 -07003110 return ieee80211_rate_control_register(&rs_ops);
Zhu Yib481de92007-09-25 17:54:57 -07003111}
3112
Tomas Winklere227cea2008-07-18 13:53:05 +08003113void iwlagn_rate_control_unregister(void)
Zhu Yib481de92007-09-25 17:54:57 -07003114{
3115 ieee80211_rate_control_unregister(&rs_ops);
3116}
3117