blob: 3b1bbc394a4904c985db8cc70e3b671f985c735d [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 Winklere7d326a2008-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
Wey-Yi Guy2681b202009-05-21 13:44:22 -0700660/* in 4965 we don't use greenfield at all */
661static inline u8 rs_use_green(struct iwl_priv *priv,
662 struct ieee80211_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -0700663{
Wey-Yi Guy2681b202009-05-21 13:44:22 -0700664 u8 is_green;
665
666 if ((priv->hw_rev & CSR_HW_REV_TYPE_MSK) == CSR_HW_REV_TYPE_4965)
667 is_green = 0;
668 else
669 is_green = (conf_is_ht(conf) &&
670 priv->current_ht_config.is_green_field &&
671 !priv->current_ht_config.non_GF_STA_present);
672 return is_green;
Guy Cohenf935a6d2008-04-23 17:15:02 -0700673}
Zhu Yib481de92007-09-25 17:54:57 -0700674
675/**
676 * rs_get_supported_rates - get the available rates
677 *
678 * if management frame or broadcast frame only return
679 * basic available rates.
680 *
681 */
Tomas Winklere227cea2008-07-18 13:53:05 +0800682static u16 rs_get_supported_rates(struct iwl_lq_sta *lq_sta,
683 struct ieee80211_hdr *hdr,
684 enum iwl_table_type rate_type)
Zhu Yib481de92007-09-25 17:54:57 -0700685{
Zhu Yib481de92007-09-25 17:54:57 -0700686 if (hdr && is_multicast_ether_addr(hdr->addr1) &&
Guy Coheneecd6e52008-04-23 17:14:58 -0700687 lq_sta->active_rate_basic)
688 return lq_sta->active_rate_basic;
689
690 if (is_legacy(rate_type)) {
691 return lq_sta->active_legacy_rate;
692 } else {
693 if (is_siso(rate_type))
694 return lq_sta->active_siso_rate;
695 else if (is_mimo2(rate_type))
696 return lq_sta->active_mimo2_rate;
697 else
698 return lq_sta->active_mimo3_rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -0800699 }
Zhu Yib481de92007-09-25 17:54:57 -0700700}
701
Ester Kummerbf403db2008-05-05 10:22:40 +0800702static u16 rs_get_adjacent_rate(struct iwl_priv *priv, u8 index, u16 rate_mask,
703 int rate_type)
Zhu Yib481de92007-09-25 17:54:57 -0700704{
705 u8 high = IWL_RATE_INVALID;
706 u8 low = IWL_RATE_INVALID;
707
Ian Schram01ebd062007-10-25 17:15:22 +0800708 /* 802.11A or ht walks to the next literal adjacent rate in
Zhu Yib481de92007-09-25 17:54:57 -0700709 * the rate table */
710 if (is_a_band(rate_type) || !is_legacy(rate_type)) {
711 int i;
712 u32 mask;
713
714 /* Find the previous rate that is in the rate mask */
715 i = index - 1;
716 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
717 if (rate_mask & mask) {
718 low = i;
719 break;
720 }
721 }
722
723 /* Find the next rate that is in the rate mask */
724 i = index + 1;
725 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
726 if (rate_mask & mask) {
727 high = i;
728 break;
729 }
730 }
731
732 return (high << 8) | low;
733 }
734
735 low = index;
736 while (low != IWL_RATE_INVALID) {
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800737 low = iwl_rates[low].prev_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700738 if (low == IWL_RATE_INVALID)
739 break;
740 if (rate_mask & (1 << low))
741 break;
Tomas Winklere1623442009-01-27 14:27:56 -0800742 IWL_DEBUG_RATE(priv, "Skipping masked lower rate: %d\n", low);
Zhu Yib481de92007-09-25 17:54:57 -0700743 }
744
745 high = index;
746 while (high != IWL_RATE_INVALID) {
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800747 high = iwl_rates[high].next_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700748 if (high == IWL_RATE_INVALID)
749 break;
750 if (rate_mask & (1 << high))
751 break;
Tomas Winklere1623442009-01-27 14:27:56 -0800752 IWL_DEBUG_RATE(priv, "Skipping masked higher rate: %d\n", high);
Zhu Yib481de92007-09-25 17:54:57 -0700753 }
754
755 return (high << 8) | low;
756}
757
Tomas Winklere227cea2008-07-18 13:53:05 +0800758static u32 rs_get_lower_rate(struct iwl_lq_sta *lq_sta,
759 struct iwl_scale_tbl_info *tbl,
760 u8 scale_index, u8 ht_possible)
Zhu Yib481de92007-09-25 17:54:57 -0700761{
Zhu Yib481de92007-09-25 17:54:57 -0700762 s32 low;
763 u16 rate_mask;
764 u16 high_low;
765 u8 switch_to_legacy = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -0800766 u8 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -0700767
768 /* check if we need to switch from HT to legacy rates.
769 * assumption is that mandatory rates (1Mbps or 6Mbps)
770 * are always supported (spec demand) */
771 if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_index)) {
772 switch_to_legacy = 1;
773 scale_index = rs_ht_to_legacy[scale_index];
Johannes Berg8318d782008-01-24 19:38:38 +0100774 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700775 tbl->lq_type = LQ_A;
776 else
777 tbl->lq_type = LQ_G;
778
Guy Cohen69fdb302008-04-23 17:15:01 -0700779 if (num_of_ant(tbl->ant_type) > 1)
Guy Cohenfde0db32008-04-21 15:42:01 -0700780 tbl->ant_type = ANT_A;/*FIXME:RS*/
Zhu Yib481de92007-09-25 17:54:57 -0700781
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700782 tbl->is_ht40 = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700783 tbl->is_SGI = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -0700784 tbl->max_search = IWL_MAX_SEARCH;
Zhu Yib481de92007-09-25 17:54:57 -0700785 }
786
Guy Coheneecd6e52008-04-23 17:14:58 -0700787 rate_mask = rs_get_supported_rates(lq_sta, NULL, tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -0700788
Ben Cahill77626352007-11-29 11:09:44 +0800789 /* Mask with station rate restriction */
Zhu Yib481de92007-09-25 17:54:57 -0700790 if (is_legacy(tbl->lq_type)) {
Ben Cahill77626352007-11-29 11:09:44 +0800791 /* supp_rates has no CCK bits in A mode */
Johannes Berg8318d782008-01-24 19:38:38 +0100792 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700793 rate_mask = (u16)(rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -0800794 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
Zhu Yib481de92007-09-25 17:54:57 -0700795 else
Tomas Winklerc33104f2008-01-14 17:46:21 -0800796 rate_mask = (u16)(rate_mask & lq_sta->supp_rates);
Zhu Yib481de92007-09-25 17:54:57 -0700797 }
798
Ben Cahill77626352007-11-29 11:09:44 +0800799 /* If we switched from HT to legacy, check current rate */
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800800 if (switch_to_legacy && (rate_mask & (1 << scale_index))) {
Guy Cohen39e88502008-04-23 17:14:57 -0700801 low = scale_index;
802 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700803 }
804
Ester Kummerbf403db2008-05-05 10:22:40 +0800805 high_low = rs_get_adjacent_rate(lq_sta->drv, scale_index, rate_mask,
806 tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -0700807 low = high_low & 0xff;
808
Guy Cohen39e88502008-04-23 17:14:57 -0700809 if (low == IWL_RATE_INVALID)
810 low = scale_index;
811
812out:
Tomas Winkler978785a2008-12-19 10:37:31 +0800813 return rate_n_flags_from_tbl(lq_sta->drv, tbl, low, is_green);
Zhu Yib481de92007-09-25 17:54:57 -0700814}
815
Ben Cahill77626352007-11-29 11:09:44 +0800816/*
817 * mac80211 sends us Tx status
818 */
Johannes Berg4b7679a2008-09-18 18:14:18 +0200819static void rs_tx_status(void *priv_r, struct ieee80211_supported_band *sband,
820 struct ieee80211_sta *sta, void *priv_sta,
Johannes Berge039fa42008-05-15 12:55:29 +0200821 struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -0700822{
823 int status;
824 u8 retries;
825 int rs_index, index = 0;
Tomas Winkler417f1142008-11-12 13:14:06 -0800826 struct iwl_lq_sta *lq_sta = priv_sta;
Tomas Winkler66c73db2008-04-15 16:01:40 -0700827 struct iwl_link_quality_cmd *table;
Zhu Yib481de92007-09-25 17:54:57 -0700828 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berg4b7679a2008-09-18 18:14:18 +0200829 struct iwl_priv *priv = (struct iwl_priv *)priv_r;
830 struct ieee80211_hw *hw = priv->hw;
Johannes Berge039fa42008-05-15 12:55:29 +0200831 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Tomas Winklere227cea2008-07-18 13:53:05 +0800832 struct iwl_rate_scale_data *window = NULL;
833 struct iwl_rate_scale_data *search_win = NULL;
Guy Cohen39e88502008-04-23 17:14:57 -0700834 u32 tx_rate;
Tomas Winklere227cea2008-07-18 13:53:05 +0800835 struct iwl_scale_tbl_info tbl_type;
836 struct iwl_scale_tbl_info *curr_tbl, *search_tbl;
Zhu Yib481de92007-09-25 17:54:57 -0700837 u8 active_index = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700838 s32 tpt = 0;
839
Tomas Winklere1623442009-01-27 14:27:56 -0800840 IWL_DEBUG_RATE_LIMIT(priv, "get frame ack response, update rate scale window\n");
Zhu Yib481de92007-09-25 17:54:57 -0700841
Tomas Winkler417f1142008-11-12 13:14:06 -0800842 if (!ieee80211_is_data(hdr->frame_control) ||
Gábor Stefanik514d65c2009-04-23 19:36:08 +0200843 info->flags & IEEE80211_TX_CTL_NO_ACK)
Zhu Yib481de92007-09-25 17:54:57 -0700844 return;
845
Ron Rindjunsky99556432008-01-28 14:07:25 +0200846 /* This packet was aggregated but doesn't carry rate scale info */
Johannes Berge039fa42008-05-15 12:55:29 +0200847 if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
848 !(info->flags & IEEE80211_TX_STAT_AMPDU))
Ron Rindjunsky99556432008-01-28 14:07:25 +0200849 return;
850
Wey-Yi Guy8fe72312009-03-10 14:35:10 -0700851 if (info->flags & IEEE80211_TX_STAT_AMPDU)
852 retries = 0;
853 else
854 retries = info->status.rates[0].count - 1;
Zhu Yib481de92007-09-25 17:54:57 -0700855
856 if (retries > 15)
857 retries = 15;
858
Johannes Berg05c914f2008-09-11 00:01:58 +0200859 if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) &&
Tomas Winklerc33104f2008-01-14 17:46:21 -0800860 !lq_sta->ibss_sta_added)
Tomas Winklerf4d60822008-03-05 11:31:00 -0800861 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700862
Tomas Winklerc33104f2008-01-14 17:46:21 -0800863 table = &lq_sta->lq;
864 active_index = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -0700865
Tomas Winklerc33104f2008-01-14 17:46:21 -0800866 curr_tbl = &(lq_sta->lq_info[active_index]);
867 search_tbl = &(lq_sta->lq_info[(1 - active_index)]);
Tomas Winklere227cea2008-07-18 13:53:05 +0800868 window = (struct iwl_rate_scale_data *)&(curr_tbl->win[0]);
869 search_win = (struct iwl_rate_scale_data *)&(search_tbl->win[0]);
Zhu Yib481de92007-09-25 17:54:57 -0700870
Ben Cahill77626352007-11-29 11:09:44 +0800871 /*
872 * Ignore this Tx frame response if its initial rate doesn't match
873 * that of latest Link Quality command. There may be stragglers
874 * from a previous Link Quality command, but we're no longer interested
875 * in those; they're either from the "active" mode while we're trying
876 * to check "search" mode, or a prior "search" mode after we've moved
877 * to a new "search" mode (which might become the new "active" mode).
878 */
Guy Cohen39e88502008-04-23 17:14:57 -0700879 tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
880 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800881 if (priv->band == IEEE80211_BAND_5GHZ)
882 rs_index -= IWL_FIRST_OFDM_RATE;
883
Johannes Berge6a98542008-10-21 12:40:02 +0200884 if ((info->status.rates[0].idx < 0) ||
885 (tbl_type.is_SGI != !!(info->status.rates[0].flags & IEEE80211_TX_RC_SHORT_GI)) ||
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700886 (tbl_type.is_ht40 != !!(info->status.rates[0].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)) ||
Johannes Berge6a98542008-10-21 12:40:02 +0200887 (tbl_type.is_dup != !!(info->status.rates[0].flags & IEEE80211_TX_RC_DUP_DATA)) ||
888 (tbl_type.ant_type != info->antenna_sel_tx) ||
889 (!!(tx_rate & RATE_MCS_HT_MSK) != !!(info->status.rates[0].flags & IEEE80211_TX_RC_MCS)) ||
890 (!!(tx_rate & RATE_MCS_GF_MSK) != !!(info->status.rates[0].flags & IEEE80211_TX_RC_GREEN_FIELD)) ||
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800891 (hw->wiphy->bands[priv->band]->bitrates[rs_index].bitrate !=
Johannes Berge6a98542008-10-21 12:40:02 +0200892 hw->wiphy->bands[info->band]->bitrates[info->status.rates[0].idx].bitrate)) {
Tomas Winklere1623442009-01-27 14:27:56 -0800893 IWL_DEBUG_RATE(priv, "initial rate does not match 0x%x\n", tx_rate);
Mohamed Abbasd5e49032008-12-12 08:22:15 -0800894 /* the last LQ command could failed so the LQ in ucode not
895 * the same in driver sync up
896 */
Abbas, Mohamed7d049e52009-01-20 21:33:53 -0800897 lq_sta->missed_rate_counter++;
898 if (lq_sta->missed_rate_counter > IWL_MISSED_RATE_MAX) {
899 lq_sta->missed_rate_counter = 0;
900 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
901 }
Tomas Winklerf4d60822008-03-05 11:31:00 -0800902 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700903 }
904
Abbas, Mohamed7d049e52009-01-20 21:33:53 -0800905 lq_sta->missed_rate_counter = 0;
Ben Cahill77626352007-11-29 11:09:44 +0800906 /* Update frame history window with "failure" for each Tx retry. */
Zhu Yib481de92007-09-25 17:54:57 -0700907 while (retries) {
Ben Cahill77626352007-11-29 11:09:44 +0800908 /* Look up the rate and other info used for each tx attempt.
909 * Each tx attempt steps one entry deeper in the rate table. */
Guy Cohen39e88502008-04-23 17:14:57 -0700910 tx_rate = le32_to_cpu(table->rs_table[index].rate_n_flags);
911 rs_get_tbl_info_from_mcs(tx_rate, priv->band,
Zhu Yib481de92007-09-25 17:54:57 -0700912 &tbl_type, &rs_index);
913
Ben Cahill77626352007-11-29 11:09:44 +0800914 /* If type matches "search" table,
915 * add failure to "search" history */
Zhu Yib481de92007-09-25 17:54:57 -0700916 if ((tbl_type.lq_type == search_tbl->lq_type) &&
Guy Cohenfde0db32008-04-21 15:42:01 -0700917 (tbl_type.ant_type == search_tbl->ant_type) &&
Zhu Yib481de92007-09-25 17:54:57 -0700918 (tbl_type.is_SGI == search_tbl->is_SGI)) {
919 if (search_tbl->expected_tpt)
920 tpt = search_tbl->expected_tpt[rs_index];
921 else
922 tpt = 0;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200923 rs_collect_tx_data(search_win, rs_index, tpt, 1, 0);
Ben Cahill77626352007-11-29 11:09:44 +0800924
925 /* Else if type matches "current/active" table,
926 * add failure to "current/active" history */
Zhu Yib481de92007-09-25 17:54:57 -0700927 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
Guy Cohenfde0db32008-04-21 15:42:01 -0700928 (tbl_type.ant_type == curr_tbl->ant_type) &&
Zhu Yib481de92007-09-25 17:54:57 -0700929 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
930 if (curr_tbl->expected_tpt)
931 tpt = curr_tbl->expected_tpt[rs_index];
932 else
933 tpt = 0;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200934 rs_collect_tx_data(window, rs_index, tpt, 1, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700935 }
Ben Cahill77626352007-11-29 11:09:44 +0800936
937 /* If not searching for a new mode, increment failed counter
938 * ... this helps determine when to start searching again */
Tomas Winklerc33104f2008-01-14 17:46:21 -0800939 if (lq_sta->stay_in_tbl)
940 lq_sta->total_failed++;
Zhu Yib481de92007-09-25 17:54:57 -0700941 --retries;
942 index++;
943
944 }
945
Ben Cahill77626352007-11-29 11:09:44 +0800946 /*
947 * Find (by rate) the history window to update with final Tx attempt;
948 * if Tx was successful first try, use original rate,
949 * else look up the rate that was, finally, successful.
950 */
Guy Cohen39e88502008-04-23 17:14:57 -0700951 tx_rate = le32_to_cpu(table->rs_table[index].rate_n_flags);
Wey-Yi Guy12b96812009-04-08 11:39:27 -0700952 lq_sta->last_rate_n_flags = tx_rate;
Guy Cohen39e88502008-04-23 17:14:57 -0700953 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
Zhu Yib481de92007-09-25 17:54:57 -0700954
Ben Cahill77626352007-11-29 11:09:44 +0800955 /* Update frame history window with "success" if Tx got ACKed ... */
Johannes Berge039fa42008-05-15 12:55:29 +0200956 status = !!(info->flags & IEEE80211_TX_STAT_ACK);
Zhu Yib481de92007-09-25 17:54:57 -0700957
Ben Cahill77626352007-11-29 11:09:44 +0800958 /* If type matches "search" table,
959 * add final tx status to "search" history */
Zhu Yib481de92007-09-25 17:54:57 -0700960 if ((tbl_type.lq_type == search_tbl->lq_type) &&
Guy Cohenfde0db32008-04-21 15:42:01 -0700961 (tbl_type.ant_type == search_tbl->ant_type) &&
Zhu Yib481de92007-09-25 17:54:57 -0700962 (tbl_type.is_SGI == search_tbl->is_SGI)) {
963 if (search_tbl->expected_tpt)
964 tpt = search_tbl->expected_tpt[rs_index];
965 else
966 tpt = 0;
Wey-Yi Guydf36c042009-03-10 14:35:11 -0700967 if (info->flags & IEEE80211_TX_STAT_AMPDU)
Ron Rindjunsky99556432008-01-28 14:07:25 +0200968 rs_collect_tx_data(search_win, rs_index, tpt,
Johannes Berge039fa42008-05-15 12:55:29 +0200969 info->status.ampdu_ack_len,
970 info->status.ampdu_ack_map);
Ron Rindjunsky99556432008-01-28 14:07:25 +0200971 else
972 rs_collect_tx_data(search_win, rs_index, tpt,
973 1, status);
Ben Cahill77626352007-11-29 11:09:44 +0800974 /* Else if type matches "current/active" table,
975 * add final tx status to "current/active" history */
Zhu Yib481de92007-09-25 17:54:57 -0700976 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
Guy Cohenfde0db32008-04-21 15:42:01 -0700977 (tbl_type.ant_type == curr_tbl->ant_type) &&
Zhu Yib481de92007-09-25 17:54:57 -0700978 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
979 if (curr_tbl->expected_tpt)
980 tpt = curr_tbl->expected_tpt[rs_index];
981 else
982 tpt = 0;
Wey-Yi Guydf36c042009-03-10 14:35:11 -0700983 if (info->flags & IEEE80211_TX_STAT_AMPDU)
Ron Rindjunsky99556432008-01-28 14:07:25 +0200984 rs_collect_tx_data(window, rs_index, tpt,
Johannes Berge039fa42008-05-15 12:55:29 +0200985 info->status.ampdu_ack_len,
986 info->status.ampdu_ack_map);
Ron Rindjunsky99556432008-01-28 14:07:25 +0200987 else
988 rs_collect_tx_data(window, rs_index, tpt,
989 1, status);
Zhu Yib481de92007-09-25 17:54:57 -0700990 }
991
Ben Cahill77626352007-11-29 11:09:44 +0800992 /* If not searching for new mode, increment success/failed counter
993 * ... these help determine when to start searching again */
Tomas Winklerc33104f2008-01-14 17:46:21 -0800994 if (lq_sta->stay_in_tbl) {
Wey-Yi Guydf36c042009-03-10 14:35:11 -0700995 if (info->flags & IEEE80211_TX_STAT_AMPDU) {
Johannes Berge039fa42008-05-15 12:55:29 +0200996 lq_sta->total_success += info->status.ampdu_ack_map;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200997 lq_sta->total_failed +=
Johannes Berge039fa42008-05-15 12:55:29 +0200998 (info->status.ampdu_ack_len - info->status.ampdu_ack_map);
Ron Rindjunsky99556432008-01-28 14:07:25 +0200999 } else {
1000 if (status)
1001 lq_sta->total_success++;
1002 else
1003 lq_sta->total_failed++;
1004 }
Zhu Yib481de92007-09-25 17:54:57 -07001005 }
1006
Ben Cahill77626352007-11-29 11:09:44 +08001007 /* See if there's a better rate or modulation mode to try. */
Abbas, Mohamedc338ba32009-01-21 10:58:02 -08001008 if (sta && sta->supp_rates[sband->band])
Gábor Stefanik514d65c2009-04-23 19:36:08 +02001009 rs_rate_scale_perform(priv, skb, sta, lq_sta);
Tomas Winklerf4d60822008-03-05 11:31:00 -08001010out:
Zhu Yib481de92007-09-25 17:54:57 -07001011 return;
1012}
1013
Ben Cahill77626352007-11-29 11:09:44 +08001014/*
1015 * Begin a period of staying with a selected modulation mode.
1016 * Set "stay_in_tbl" flag to prevent any mode switches.
1017 * Set frame tx success limits according to legacy vs. high-throughput,
1018 * and reset overall (spanning all rates) tx success history statistics.
1019 * These control how long we stay using same modulation mode before
1020 * searching for a new mode.
1021 */
Ester Kummerbf403db2008-05-05 10:22:40 +08001022static void rs_set_stay_in_table(struct iwl_priv *priv, u8 is_legacy,
Tomas Winklere227cea2008-07-18 13:53:05 +08001023 struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07001024{
Tomas Winklere1623442009-01-27 14:27:56 -08001025 IWL_DEBUG_RATE(priv, "we are staying in the same table\n");
Tomas Winklerc33104f2008-01-14 17:46:21 -08001026 lq_sta->stay_in_tbl = 1; /* only place this gets set */
Zhu Yib481de92007-09-25 17:54:57 -07001027 if (is_legacy) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001028 lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT;
1029 lq_sta->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
1030 lq_sta->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
Zhu Yib481de92007-09-25 17:54:57 -07001031 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001032 lq_sta->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
1033 lq_sta->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
1034 lq_sta->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
Zhu Yib481de92007-09-25 17:54:57 -07001035 }
Tomas Winklerc33104f2008-01-14 17:46:21 -08001036 lq_sta->table_count = 0;
1037 lq_sta->total_failed = 0;
1038 lq_sta->total_success = 0;
Mohamed Abbas447fee72009-04-20 14:37:02 -07001039 lq_sta->flush_timer = jiffies;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001040 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001041}
1042
Ben Cahill77626352007-11-29 11:09:44 +08001043/*
1044 * Find correct throughput table for given mode of modulation
1045 */
Tomas Winklere227cea2008-07-18 13:53:05 +08001046static void rs_set_expected_tpt_table(struct iwl_lq_sta *lq_sta,
1047 struct iwl_scale_tbl_info *tbl)
Zhu Yib481de92007-09-25 17:54:57 -07001048{
1049 if (is_legacy(tbl->lq_type)) {
1050 if (!is_a_band(tbl->lq_type))
1051 tbl->expected_tpt = expected_tpt_G;
1052 else
1053 tbl->expected_tpt = expected_tpt_A;
1054 } else if (is_siso(tbl->lq_type)) {
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001055 if (tbl->is_ht40 && !lq_sta->is_dup)
Zhu Yib481de92007-09-25 17:54:57 -07001056 if (tbl->is_SGI)
1057 tbl->expected_tpt = expected_tpt_siso40MHzSGI;
1058 else
1059 tbl->expected_tpt = expected_tpt_siso40MHz;
1060 else if (tbl->is_SGI)
1061 tbl->expected_tpt = expected_tpt_siso20MHzSGI;
1062 else
1063 tbl->expected_tpt = expected_tpt_siso20MHz;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001064 } else if (is_mimo2(tbl->lq_type)) {
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001065 if (tbl->is_ht40 && !lq_sta->is_dup)
Zhu Yib481de92007-09-25 17:54:57 -07001066 if (tbl->is_SGI)
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001067 tbl->expected_tpt = expected_tpt_mimo2_40MHzSGI;
Zhu Yib481de92007-09-25 17:54:57 -07001068 else
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001069 tbl->expected_tpt = expected_tpt_mimo2_40MHz;
Zhu Yib481de92007-09-25 17:54:57 -07001070 else if (tbl->is_SGI)
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001071 tbl->expected_tpt = expected_tpt_mimo2_20MHzSGI;
Zhu Yib481de92007-09-25 17:54:57 -07001072 else
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001073 tbl->expected_tpt = expected_tpt_mimo2_20MHz;
1074 } else if (is_mimo3(tbl->lq_type)) {
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001075 if (tbl->is_ht40 && !lq_sta->is_dup)
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001076 if (tbl->is_SGI)
1077 tbl->expected_tpt = expected_tpt_mimo3_40MHzSGI;
1078 else
1079 tbl->expected_tpt = expected_tpt_mimo3_40MHz;
1080 else if (tbl->is_SGI)
1081 tbl->expected_tpt = expected_tpt_mimo3_20MHzSGI;
1082 else
1083 tbl->expected_tpt = expected_tpt_mimo3_20MHz;
Zhu Yib481de92007-09-25 17:54:57 -07001084 } else
1085 tbl->expected_tpt = expected_tpt_G;
1086}
1087
Ben Cahill77626352007-11-29 11:09:44 +08001088/*
1089 * Find starting rate for new "search" high-throughput mode of modulation.
1090 * Goal is to find lowest expected rate (under perfect conditions) that is
1091 * above the current measured throughput of "active" mode, to give new mode
1092 * a fair chance to prove itself without too many challenges.
1093 *
1094 * This gets called when transitioning to more aggressive modulation
1095 * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
1096 * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
1097 * to decrease to match "active" throughput. When moving from MIMO to SISO,
1098 * bit rate will typically need to increase, but not if performance was bad.
1099 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001100static s32 rs_get_best_rate(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001101 struct iwl_lq_sta *lq_sta,
1102 struct iwl_scale_tbl_info *tbl, /* "search" */
Guy Cohenf935a6d2008-04-23 17:15:02 -07001103 u16 rate_mask, s8 index)
Zhu Yib481de92007-09-25 17:54:57 -07001104{
Ben Cahill77626352007-11-29 11:09:44 +08001105 /* "active" values */
Tomas Winklere227cea2008-07-18 13:53:05 +08001106 struct iwl_scale_tbl_info *active_tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001107 &(lq_sta->lq_info[lq_sta->active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07001108 s32 active_sr = active_tbl->win[index].success_ratio;
Zhu Yib481de92007-09-25 17:54:57 -07001109 s32 active_tpt = active_tbl->expected_tpt[index];
Ben Cahill77626352007-11-29 11:09:44 +08001110
1111 /* expected "search" throughput */
1112 s32 *tpt_tbl = tbl->expected_tpt;
1113
1114 s32 new_rate, high, low, start_hi;
Zhu Yib481de92007-09-25 17:54:57 -07001115 u16 high_low;
Guy Cohenf935a6d2008-04-23 17:15:02 -07001116 s8 rate = index;
Zhu Yib481de92007-09-25 17:54:57 -07001117
1118 new_rate = high = low = start_hi = IWL_RATE_INVALID;
1119
1120 for (; ;) {
Ester Kummerbf403db2008-05-05 10:22:40 +08001121 high_low = rs_get_adjacent_rate(priv, rate, rate_mask,
1122 tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -07001123
1124 low = high_low & 0xff;
1125 high = (high_low >> 8) & 0xff;
1126
Ben Cahill77626352007-11-29 11:09:44 +08001127 /*
1128 * Lower the "search" bit rate, to give new "search" mode
1129 * approximately the same throughput as "active" if:
1130 *
1131 * 1) "Active" mode has been working modestly well (but not
1132 * great), and expected "search" throughput (under perfect
1133 * conditions) at candidate rate is above the actual
1134 * measured "active" throughput (but less than expected
1135 * "active" throughput under perfect conditions).
1136 * OR
1137 * 2) "Active" mode has been working perfectly or very well
1138 * and expected "search" throughput (under perfect
1139 * conditions) at candidate rate is above expected
1140 * "active" throughput (under perfect conditions).
1141 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001142 if ((((100 * tpt_tbl[rate]) > lq_sta->last_tpt) &&
Zhu Yib481de92007-09-25 17:54:57 -07001143 ((active_sr > IWL_RATE_DECREASE_TH) &&
1144 (active_sr <= IWL_RATE_HIGH_TH) &&
1145 (tpt_tbl[rate] <= active_tpt))) ||
1146 ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
1147 (tpt_tbl[rate] > active_tpt))) {
1148
Ben Cahill77626352007-11-29 11:09:44 +08001149 /* (2nd or later pass)
1150 * If we've already tried to raise the rate, and are
1151 * now trying to lower it, use the higher rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001152 if (start_hi != IWL_RATE_INVALID) {
1153 new_rate = start_hi;
1154 break;
1155 }
Ben Cahill77626352007-11-29 11:09:44 +08001156
Zhu Yib481de92007-09-25 17:54:57 -07001157 new_rate = rate;
Ben Cahill77626352007-11-29 11:09:44 +08001158
1159 /* Loop again with lower rate */
Zhu Yib481de92007-09-25 17:54:57 -07001160 if (low != IWL_RATE_INVALID)
1161 rate = low;
Ben Cahill77626352007-11-29 11:09:44 +08001162
1163 /* Lower rate not available, use the original */
Zhu Yib481de92007-09-25 17:54:57 -07001164 else
1165 break;
Ben Cahill77626352007-11-29 11:09:44 +08001166
1167 /* Else try to raise the "search" rate to match "active" */
Zhu Yib481de92007-09-25 17:54:57 -07001168 } else {
Ben Cahill77626352007-11-29 11:09:44 +08001169 /* (2nd or later pass)
1170 * If we've already tried to lower the rate, and are
1171 * now trying to raise it, use the lower rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001172 if (new_rate != IWL_RATE_INVALID)
1173 break;
Ben Cahill77626352007-11-29 11:09:44 +08001174
1175 /* Loop again with higher rate */
Zhu Yib481de92007-09-25 17:54:57 -07001176 else if (high != IWL_RATE_INVALID) {
1177 start_hi = high;
1178 rate = high;
Ben Cahill77626352007-11-29 11:09:44 +08001179
1180 /* Higher rate not available, use the original */
Zhu Yib481de92007-09-25 17:54:57 -07001181 } else {
Guy Cohen90d77952008-09-09 10:54:53 +08001182 new_rate = rate;
Zhu Yib481de92007-09-25 17:54:57 -07001183 break;
1184 }
1185 }
1186 }
1187
1188 return new_rate;
1189}
Zhu Yib481de92007-09-25 17:54:57 -07001190
Ben Cahill77626352007-11-29 11:09:44 +08001191/*
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001192 * Set up search table for MIMO2
Ben Cahill77626352007-11-29 11:09:44 +08001193 */
Guy Cohenfde0db32008-04-21 15:42:01 -07001194static int rs_switch_to_mimo2(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001195 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001196 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001197 struct ieee80211_sta *sta,
Tomas Winklere227cea2008-07-18 13:53:05 +08001198 struct iwl_scale_tbl_info *tbl, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001199{
Zhu Yib481de92007-09-25 17:54:57 -07001200 u16 rate_mask;
1201 s32 rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001202 s8 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07001203
Luis R. Rodriguezde27e642008-12-23 15:58:44 -08001204 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
Zhu Yib481de92007-09-25 17:54:57 -07001205 return -1;
1206
Johannes Bergd9fe60d2008-10-09 12:13:49 +02001207 if (((sta->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 2)
Tomas Winkler00c5ae22008-09-03 11:26:42 +08001208 == WLAN_HT_CAP_SM_PS_STATIC)
Zhu Yib481de92007-09-25 17:54:57 -07001209 return -1;
1210
Ben Cahill77626352007-11-29 11:09:44 +08001211 /* Need both Tx chains/antennas to support MIMO */
Guy Cohenaade00c2008-04-23 17:14:59 -07001212 if (priv->hw_params.tx_chains_num < 2)
Zhu Yib481de92007-09-25 17:54:57 -07001213 return -1;
1214
Tomas Winklere1623442009-01-27 14:27:56 -08001215 IWL_DEBUG_RATE(priv, "LQ: try to switch to MIMO2\n");
Guy Cohen39e88502008-04-23 17:14:57 -07001216
1217 tbl->lq_type = LQ_MIMO2;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001218 tbl->is_dup = lq_sta->is_dup;
Zhu Yib481de92007-09-25 17:54:57 -07001219 tbl->action = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001220 tbl->max_search = IWL_MAX_SEARCH;
Guy Cohen39e88502008-04-23 17:14:57 -07001221 rate_mask = lq_sta->active_mimo2_rate;
1222
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001223 if (iwl_is_ht40_tx_allowed(priv, &sta->ht_cap))
1224 tbl->is_ht40 = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001225 else
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001226 tbl->is_ht40 = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001227
Guy Coheneecd6e52008-04-23 17:14:58 -07001228 rs_set_expected_tpt_table(lq_sta, tbl);
Zhu Yib481de92007-09-25 17:54:57 -07001229
Guy Cohenf935a6d2008-04-23 17:15:02 -07001230 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
Zhu Yib481de92007-09-25 17:54:57 -07001231
Tomas Winklere1623442009-01-27 14:27:56 -08001232 IWL_DEBUG_RATE(priv, "LQ: MIMO2 best rate %d mask %X\n", rate, rate_mask);
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001233 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1234 IWL_DEBUG_RATE(priv, "Can't switch with index %d rate mask %x\n",
1235 rate, rate_mask);
1236 return -1;
1237 }
1238 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
Guy Cohen39e88502008-04-23 17:14:57 -07001239
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001240 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
1241 tbl->current_rate, is_green);
1242 return 0;
1243}
1244
1245/*
1246 * Set up search table for MIMO3
1247 */
1248static int rs_switch_to_mimo3(struct iwl_priv *priv,
1249 struct iwl_lq_sta *lq_sta,
1250 struct ieee80211_conf *conf,
1251 struct ieee80211_sta *sta,
1252 struct iwl_scale_tbl_info *tbl, int index)
1253{
1254 u16 rate_mask;
1255 s32 rate;
1256 s8 is_green = lq_sta->is_green;
1257
1258 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
1259 return -1;
1260
1261 if (((sta->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 2)
1262 == WLAN_HT_CAP_SM_PS_STATIC)
1263 return -1;
1264
1265 /* Need both Tx chains/antennas to support MIMO */
1266 if (priv->hw_params.tx_chains_num < 3)
1267 return -1;
1268
1269 IWL_DEBUG_RATE(priv, "LQ: try to switch to MIMO3\n");
1270
1271 tbl->lq_type = LQ_MIMO3;
1272 tbl->is_dup = lq_sta->is_dup;
1273 tbl->action = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001274 tbl->max_search = IWL_MAX_11N_MIMO3_SEARCH;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001275 rate_mask = lq_sta->active_mimo3_rate;
1276
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001277 if (iwl_is_ht40_tx_allowed(priv, &sta->ht_cap))
1278 tbl->is_ht40 = 1;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001279 else
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001280 tbl->is_ht40 = 0;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001281
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001282 rs_set_expected_tpt_table(lq_sta, tbl);
1283
1284 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
1285
1286 IWL_DEBUG_RATE(priv, "LQ: MIMO3 best rate %d mask %X\n",
1287 rate, rate_mask);
Guy Cohen39e88502008-04-23 17:14:57 -07001288 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
Tomas Winklere1623442009-01-27 14:27:56 -08001289 IWL_DEBUG_RATE(priv, "Can't switch with index %d rate mask %x\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001290 rate, rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07001291 return -1;
Guy Cohen39e88502008-04-23 17:14:57 -07001292 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001293 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
Zhu Yib481de92007-09-25 17:54:57 -07001294
Tomas Winklere1623442009-01-27 14:27:56 -08001295 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001296 tbl->current_rate, is_green);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001297 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07001298}
1299
Ben Cahill77626352007-11-29 11:09:44 +08001300/*
1301 * Set up search table for SISO
1302 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001303static int rs_switch_to_siso(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001304 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001305 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001306 struct ieee80211_sta *sta,
Tomas Winklere227cea2008-07-18 13:53:05 +08001307 struct iwl_scale_tbl_info *tbl, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001308{
Zhu Yib481de92007-09-25 17:54:57 -07001309 u16 rate_mask;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001310 u8 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07001311 s32 rate;
1312
Luis R. Rodriguezde27e642008-12-23 15:58:44 -08001313 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
Zhu Yib481de92007-09-25 17:54:57 -07001314 return -1;
1315
Tomas Winklere1623442009-01-27 14:27:56 -08001316 IWL_DEBUG_RATE(priv, "LQ: try to switch to SISO\n");
Guy Cohen39e88502008-04-23 17:14:57 -07001317
Tomas Winklerc33104f2008-01-14 17:46:21 -08001318 tbl->is_dup = lq_sta->is_dup;
Zhu Yib481de92007-09-25 17:54:57 -07001319 tbl->lq_type = LQ_SISO;
1320 tbl->action = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001321 tbl->max_search = IWL_MAX_SEARCH;
Guy Cohen39e88502008-04-23 17:14:57 -07001322 rate_mask = lq_sta->active_siso_rate;
Zhu Yib481de92007-09-25 17:54:57 -07001323
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001324 if (iwl_is_ht40_tx_allowed(priv, &sta->ht_cap))
1325 tbl->is_ht40 = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001326 else
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001327 tbl->is_ht40 = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001328
Zhu Yib481de92007-09-25 17:54:57 -07001329 if (is_green)
Guy Cohen39e88502008-04-23 17:14:57 -07001330 tbl->is_SGI = 0; /*11n spec: no SGI in SISO+Greenfield*/
Zhu Yib481de92007-09-25 17:54:57 -07001331
Guy Coheneecd6e52008-04-23 17:14:58 -07001332 rs_set_expected_tpt_table(lq_sta, tbl);
Guy Cohenf935a6d2008-04-23 17:15:02 -07001333 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
Zhu Yib481de92007-09-25 17:54:57 -07001334
Tomas Winklere1623442009-01-27 14:27:56 -08001335 IWL_DEBUG_RATE(priv, "LQ: get best rate %d mask %X\n", rate, rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07001336 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
Tomas Winklere1623442009-01-27 14:27:56 -08001337 IWL_DEBUG_RATE(priv, "can not switch with index %d rate mask %x\n",
Zhu Yib481de92007-09-25 17:54:57 -07001338 rate, rate_mask);
1339 return -1;
1340 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001341 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
Tomas Winklere1623442009-01-27 14:27:56 -08001342 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001343 tbl->current_rate, is_green);
Mohamed Abbas403ab562007-11-06 22:06:25 -08001344 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07001345}
1346
Ben Cahill77626352007-11-29 11:09:44 +08001347/*
1348 * Try to switch to new modulation mode from legacy
1349 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001350static int rs_move_legacy_other(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001351 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001352 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001353 struct ieee80211_sta *sta,
Zhu Yib481de92007-09-25 17:54:57 -07001354 int index)
1355{
Tomas Winklere227cea2008-07-18 13:53:05 +08001356 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1357 struct iwl_scale_tbl_info *search_tbl =
1358 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1359 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1360 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1361 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001362 u8 start_action = tbl->action;
Guy Cohenfde0db32008-04-21 15:42:01 -07001363 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
Guy Cohen3110bef2008-09-09 10:54:54 +08001364 u8 tx_chains_num = priv->hw_params.tx_chains_num;
Guy Cohenfde0db32008-04-21 15:42:01 -07001365 int ret = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001366 u8 update_search_tbl_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001367
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001368 if (!iwl_ht_enabled(priv))
1369 /* stay in Legacy */
1370 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
Johannes Berg3ad3b922009-08-07 15:41:48 -07001371 else if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001372 tbl->action > IWL_LEGACY_SWITCH_SISO)
1373 tbl->action = IWL_LEGACY_SWITCH_SISO;
Zhu Yib481de92007-09-25 17:54:57 -07001374 for (; ;) {
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001375 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001376 switch (tbl->action) {
Guy Cohen3110bef2008-09-09 10:54:54 +08001377 case IWL_LEGACY_SWITCH_ANTENNA1:
1378 case IWL_LEGACY_SWITCH_ANTENNA2:
Tomas Winklere1623442009-01-27 14:27:56 -08001379 IWL_DEBUG_RATE(priv, "LQ: Legacy toggle Antenna\n");
Zhu Yib481de92007-09-25 17:54:57 -07001380
Guy Cohen3110bef2008-09-09 10:54:54 +08001381 if ((tbl->action == IWL_LEGACY_SWITCH_ANTENNA1 &&
1382 tx_chains_num <= 1) ||
1383 (tbl->action == IWL_LEGACY_SWITCH_ANTENNA2 &&
1384 tx_chains_num <= 2))
1385 break;
1386
Ben Cahill77626352007-11-29 11:09:44 +08001387 /* Don't change antenna if success has been great */
Zhu Yib481de92007-09-25 17:54:57 -07001388 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1389 break;
Ben Cahill77626352007-11-29 11:09:44 +08001390
Ben Cahill77626352007-11-29 11:09:44 +08001391 /* Set up search table to try other antenna */
Zhu Yib481de92007-09-25 17:54:57 -07001392 memcpy(search_tbl, tbl, sz);
1393
Guy Cohenaade00c2008-04-23 17:14:59 -07001394 if (rs_toggle_antenna(valid_tx_ant,
1395 &search_tbl->current_rate, search_tbl)) {
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001396 update_search_tbl_counter = 1;
Guy Cohen3110bef2008-09-09 10:54:54 +08001397 rs_set_expected_tpt_table(lq_sta, search_tbl);
Guy Cohenaade00c2008-04-23 17:14:59 -07001398 goto out;
1399 }
Guy Cohena5e8b502008-05-29 16:35:11 +08001400 break;
Zhu Yib481de92007-09-25 17:54:57 -07001401 case IWL_LEGACY_SWITCH_SISO:
Tomas Winklere1623442009-01-27 14:27:56 -08001402 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to SISO\n");
Ben Cahill77626352007-11-29 11:09:44 +08001403
1404 /* Set up search table to try SISO */
Zhu Yib481de92007-09-25 17:54:57 -07001405 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001406 search_tbl->is_SGI = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001407 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001408 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001409 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001410 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001411 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001412 }
Zhu Yib481de92007-09-25 17:54:57 -07001413
1414 break;
Guy Cohen3110bef2008-09-09 10:54:54 +08001415 case IWL_LEGACY_SWITCH_MIMO2_AB:
1416 case IWL_LEGACY_SWITCH_MIMO2_AC:
1417 case IWL_LEGACY_SWITCH_MIMO2_BC:
Tomas Winklere1623442009-01-27 14:27:56 -08001418 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to MIMO2\n");
Ben Cahill77626352007-11-29 11:09:44 +08001419
1420 /* Set up search table to try MIMO */
Zhu Yib481de92007-09-25 17:54:57 -07001421 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001422 search_tbl->is_SGI = 0;
Guy Cohen3110bef2008-09-09 10:54:54 +08001423
1424 if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AB)
1425 search_tbl->ant_type = ANT_AB;
1426 else if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AC)
1427 search_tbl->ant_type = ANT_AC;
1428 else
1429 search_tbl->ant_type = ANT_BC;
1430
1431 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1432 break;
1433
Guy Cohenfde0db32008-04-21 15:42:01 -07001434 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001435 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001436 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001437 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001438 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001439 }
Zhu Yib481de92007-09-25 17:54:57 -07001440 break;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001441
1442 case IWL_LEGACY_SWITCH_MIMO3_ABC:
1443 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to MIMO3\n");
1444
1445 /* Set up search table to try MIMO3 */
1446 memcpy(search_tbl, tbl, sz);
1447 search_tbl->is_SGI = 0;
1448
1449 search_tbl->ant_type = ANT_ABC;
1450
1451 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1452 break;
1453
1454 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1455 search_tbl, index);
1456 if (!ret) {
1457 lq_sta->action_counter = 0;
1458 goto out;
1459 }
1460 break;
Zhu Yib481de92007-09-25 17:54:57 -07001461 }
1462 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001463 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001464 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
Zhu Yib481de92007-09-25 17:54:57 -07001465
1466 if (tbl->action == start_action)
1467 break;
1468
1469 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001470 search_tbl->lq_type = LQ_NONE;
Zhu Yib481de92007-09-25 17:54:57 -07001471 return 0;
1472
Guy Cohen3110bef2008-09-09 10:54:54 +08001473out:
1474 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001475 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001476 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001477 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001478 if (update_search_tbl_counter)
1479 search_tbl->action = tbl->action;
Zhu Yib481de92007-09-25 17:54:57 -07001480 return 0;
1481
1482}
1483
Ben Cahill77626352007-11-29 11:09:44 +08001484/*
1485 * Try to switch to new modulation mode from SISO
1486 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001487static int rs_move_siso_to_other(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001488 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001489 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001490 struct ieee80211_sta *sta, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001491{
Tomas Winklerc33104f2008-01-14 17:46:21 -08001492 u8 is_green = lq_sta->is_green;
Tomas Winklere227cea2008-07-18 13:53:05 +08001493 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1494 struct iwl_scale_tbl_info *search_tbl =
1495 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1496 struct iwl_rate_scale_data *window = &(tbl->win[index]);
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001497 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Tomas Winklere227cea2008-07-18 13:53:05 +08001498 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1499 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001500 u8 start_action = tbl->action;
Guy Cohenfde0db32008-04-21 15:42:01 -07001501 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
Guy Cohen3110bef2008-09-09 10:54:54 +08001502 u8 tx_chains_num = priv->hw_params.tx_chains_num;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001503 u8 update_search_tbl_counter = 0;
Guy Cohenfde0db32008-04-21 15:42:01 -07001504 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001505
Johannes Berg3ad3b922009-08-07 15:41:48 -07001506 if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001507 tbl->action > IWL_SISO_SWITCH_ANTENNA2) {
1508 /* stay in SISO */
1509 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1510 }
Zhu Yib481de92007-09-25 17:54:57 -07001511 for (;;) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001512 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001513 switch (tbl->action) {
Guy Cohen3110bef2008-09-09 10:54:54 +08001514 case IWL_SISO_SWITCH_ANTENNA1:
1515 case IWL_SISO_SWITCH_ANTENNA2:
Tomas Winklere1623442009-01-27 14:27:56 -08001516 IWL_DEBUG_RATE(priv, "LQ: SISO toggle Antenna\n");
Guy Cohen3110bef2008-09-09 10:54:54 +08001517
1518 if ((tbl->action == IWL_SISO_SWITCH_ANTENNA1 &&
1519 tx_chains_num <= 1) ||
1520 (tbl->action == IWL_SISO_SWITCH_ANTENNA2 &&
1521 tx_chains_num <= 2))
1522 break;
1523
Zhu Yib481de92007-09-25 17:54:57 -07001524 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1525 break;
Zhu Yib481de92007-09-25 17:54:57 -07001526
1527 memcpy(search_tbl, tbl, sz);
Guy Cohenaade00c2008-04-23 17:14:59 -07001528 if (rs_toggle_antenna(valid_tx_ant,
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001529 &search_tbl->current_rate, search_tbl)) {
1530 update_search_tbl_counter = 1;
Guy Cohenaade00c2008-04-23 17:14:59 -07001531 goto out;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001532 }
Guy Cohena5e8b502008-05-29 16:35:11 +08001533 break;
Guy Cohen3110bef2008-09-09 10:54:54 +08001534 case IWL_SISO_SWITCH_MIMO2_AB:
1535 case IWL_SISO_SWITCH_MIMO2_AC:
1536 case IWL_SISO_SWITCH_MIMO2_BC:
Tomas Winklere1623442009-01-27 14:27:56 -08001537 IWL_DEBUG_RATE(priv, "LQ: SISO switch to MIMO2\n");
Zhu Yib481de92007-09-25 17:54:57 -07001538 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001539 search_tbl->is_SGI = 0;
Guy Cohen3110bef2008-09-09 10:54:54 +08001540
1541 if (tbl->action == IWL_SISO_SWITCH_MIMO2_AB)
1542 search_tbl->ant_type = ANT_AB;
1543 else if (tbl->action == IWL_SISO_SWITCH_MIMO2_AC)
1544 search_tbl->ant_type = ANT_AC;
1545 else
1546 search_tbl->ant_type = ANT_BC;
1547
1548 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1549 break;
1550
Guy Cohenfde0db32008-04-21 15:42:01 -07001551 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001552 search_tbl, index);
Guy Cohen3110bef2008-09-09 10:54:54 +08001553 if (!ret)
Zhu Yib481de92007-09-25 17:54:57 -07001554 goto out;
1555 break;
1556 case IWL_SISO_SWITCH_GI:
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001557 if (!tbl->is_ht40 && !(ht_cap->cap &
1558 IEEE80211_HT_CAP_SGI_20))
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001559 break;
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001560 if (tbl->is_ht40 && !(ht_cap->cap &
1561 IEEE80211_HT_CAP_SGI_40))
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001562 break;
1563
Tomas Winklere1623442009-01-27 14:27:56 -08001564 IWL_DEBUG_RATE(priv, "LQ: SISO toggle SGI/NGI\n");
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001565
Zhu Yib481de92007-09-25 17:54:57 -07001566 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001567 if (is_green) {
1568 if (!tbl->is_SGI)
1569 break;
1570 else
Winkler, Tomas15b16872008-12-19 10:37:33 +08001571 IWL_ERR(priv,
1572 "SGI was set in GF+SISO\n");
Zhu Yib481de92007-09-25 17:54:57 -07001573 }
Guy Cohen39e88502008-04-23 17:14:57 -07001574 search_tbl->is_SGI = !tbl->is_SGI;
Guy Coheneecd6e52008-04-23 17:14:58 -07001575 rs_set_expected_tpt_table(lq_sta, search_tbl);
Guy Cohen39e88502008-04-23 17:14:57 -07001576 if (tbl->is_SGI) {
1577 s32 tpt = lq_sta->last_tpt / 100;
1578 if (tpt >= search_tbl->expected_tpt[index])
1579 break;
1580 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001581 search_tbl->current_rate =
1582 rate_n_flags_from_tbl(priv, search_tbl,
1583 index, is_green);
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001584 update_search_tbl_counter = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001585 goto out;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001586 case IWL_SISO_SWITCH_MIMO3_ABC:
1587 IWL_DEBUG_RATE(priv, "LQ: SISO switch to MIMO3\n");
1588 memcpy(search_tbl, tbl, sz);
1589 search_tbl->is_SGI = 0;
1590 search_tbl->ant_type = ANT_ABC;
1591
1592 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1593 break;
1594
1595 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1596 search_tbl, index);
1597 if (!ret)
1598 goto out;
1599 break;
Zhu Yib481de92007-09-25 17:54:57 -07001600 }
1601 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001602 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001603 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
Zhu Yib481de92007-09-25 17:54:57 -07001604
1605 if (tbl->action == start_action)
1606 break;
1607 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001608 search_tbl->lq_type = LQ_NONE;
Zhu Yib481de92007-09-25 17:54:57 -07001609 return 0;
1610
1611 out:
Guy Cohen3110bef2008-09-09 10:54:54 +08001612 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001613 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001614 if (tbl->action > IWL_SISO_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001615 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001616 if (update_search_tbl_counter)
1617 search_tbl->action = tbl->action;
1618
Zhu Yib481de92007-09-25 17:54:57 -07001619 return 0;
1620}
1621
Ben Cahill77626352007-11-29 11:09:44 +08001622/*
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001623 * Try to switch to new modulation mode from MIMO2
Ben Cahill77626352007-11-29 11:09:44 +08001624 */
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001625static int rs_move_mimo2_to_other(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001626 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001627 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001628 struct ieee80211_sta *sta, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001629{
Tomas Winklerc33104f2008-01-14 17:46:21 -08001630 s8 is_green = lq_sta->is_green;
Tomas Winklere227cea2008-07-18 13:53:05 +08001631 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1632 struct iwl_scale_tbl_info *search_tbl =
1633 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Guy Cohen3110bef2008-09-09 10:54:54 +08001634 struct iwl_rate_scale_data *window = &(tbl->win[index]);
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001635 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Tomas Winklere227cea2008-07-18 13:53:05 +08001636 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1637 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001638 u8 start_action = tbl->action;
Guy Cohen3110bef2008-09-09 10:54:54 +08001639 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1640 u8 tx_chains_num = priv->hw_params.tx_chains_num;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001641 u8 update_search_tbl_counter = 0;
Guy Cohenaade00c2008-04-23 17:14:59 -07001642 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001643
Johannes Berg3ad3b922009-08-07 15:41:48 -07001644 if ((iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE) &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001645 (tbl->action < IWL_MIMO2_SWITCH_SISO_A ||
1646 tbl->action > IWL_MIMO2_SWITCH_SISO_C)) {
1647 /* switch in SISO */
1648 tbl->action = IWL_MIMO2_SWITCH_SISO_A;
1649 }
Zhu Yib481de92007-09-25 17:54:57 -07001650 for (;;) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001651 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001652 switch (tbl->action) {
Guy Cohen3110bef2008-09-09 10:54:54 +08001653 case IWL_MIMO2_SWITCH_ANTENNA1:
1654 case IWL_MIMO2_SWITCH_ANTENNA2:
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001655 IWL_DEBUG_RATE(priv, "LQ: MIMO2 toggle Antennas\n");
Guy Cohen3110bef2008-09-09 10:54:54 +08001656
1657 if (tx_chains_num <= 2)
1658 break;
1659
1660 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1661 break;
1662
1663 memcpy(search_tbl, tbl, sz);
1664 if (rs_toggle_antenna(valid_tx_ant,
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001665 &search_tbl->current_rate, search_tbl)) {
1666 update_search_tbl_counter = 1;
Guy Cohen3110bef2008-09-09 10:54:54 +08001667 goto out;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001668 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001669 break;
1670 case IWL_MIMO2_SWITCH_SISO_A:
1671 case IWL_MIMO2_SWITCH_SISO_B:
1672 case IWL_MIMO2_SWITCH_SISO_C:
Tomas Winklere1623442009-01-27 14:27:56 -08001673 IWL_DEBUG_RATE(priv, "LQ: MIMO2 switch to SISO\n");
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001674
Ben Cahill77626352007-11-29 11:09:44 +08001675 /* Set up new search table for SISO */
Zhu Yib481de92007-09-25 17:54:57 -07001676 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001677
Guy Cohen3110bef2008-09-09 10:54:54 +08001678 if (tbl->action == IWL_MIMO2_SWITCH_SISO_A)
Guy Cohenfde0db32008-04-21 15:42:01 -07001679 search_tbl->ant_type = ANT_A;
Guy Cohen3110bef2008-09-09 10:54:54 +08001680 else if (tbl->action == IWL_MIMO2_SWITCH_SISO_B)
Guy Cohenfde0db32008-04-21 15:42:01 -07001681 search_tbl->ant_type = ANT_B;
Guy Cohen3110bef2008-09-09 10:54:54 +08001682 else
1683 search_tbl->ant_type = ANT_C;
1684
1685 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1686 break;
Zhu Yib481de92007-09-25 17:54:57 -07001687
Tomas Winklerc33104f2008-01-14 17:46:21 -08001688 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001689 search_tbl, index);
Guy Cohen3110bef2008-09-09 10:54:54 +08001690 if (!ret)
Zhu Yib481de92007-09-25 17:54:57 -07001691 goto out;
Guy Cohen3110bef2008-09-09 10:54:54 +08001692
Zhu Yib481de92007-09-25 17:54:57 -07001693 break;
1694
Guy Cohen3110bef2008-09-09 10:54:54 +08001695 case IWL_MIMO2_SWITCH_GI:
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001696 if (!tbl->is_ht40 && !(ht_cap->cap &
1697 IEEE80211_HT_CAP_SGI_20))
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001698 break;
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001699 if (tbl->is_ht40 && !(ht_cap->cap &
1700 IEEE80211_HT_CAP_SGI_40))
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001701 break;
1702
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001703 IWL_DEBUG_RATE(priv, "LQ: MIMO2 toggle SGI/NGI\n");
1704
1705 /* Set up new search table for MIMO2 */
1706 memcpy(search_tbl, tbl, sz);
1707 search_tbl->is_SGI = !tbl->is_SGI;
1708 rs_set_expected_tpt_table(lq_sta, search_tbl);
1709 /*
1710 * If active table already uses the fastest possible
1711 * modulation (dual stream with short guard interval),
1712 * and it's working well, there's no need to look
1713 * for a better type of modulation!
1714 */
1715 if (tbl->is_SGI) {
1716 s32 tpt = lq_sta->last_tpt / 100;
1717 if (tpt >= search_tbl->expected_tpt[index])
1718 break;
1719 }
1720 search_tbl->current_rate =
1721 rate_n_flags_from_tbl(priv, search_tbl,
1722 index, is_green);
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001723 update_search_tbl_counter = 1;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001724 goto out;
1725
1726 case IWL_MIMO2_SWITCH_MIMO3_ABC:
1727 IWL_DEBUG_RATE(priv, "LQ: MIMO2 switch to MIMO3\n");
1728 memcpy(search_tbl, tbl, sz);
1729 search_tbl->is_SGI = 0;
1730 search_tbl->ant_type = ANT_ABC;
1731
1732 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1733 break;
1734
1735 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1736 search_tbl, index);
1737 if (!ret)
1738 goto out;
1739
1740 break;
1741 }
1742 tbl->action++;
1743 if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
1744 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
1745
1746 if (tbl->action == start_action)
1747 break;
1748 }
1749 search_tbl->lq_type = LQ_NONE;
1750 return 0;
1751 out:
1752 lq_sta->search_better_tbl = 1;
1753 tbl->action++;
1754 if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
1755 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001756 if (update_search_tbl_counter)
1757 search_tbl->action = tbl->action;
1758
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001759 return 0;
1760
1761}
1762
1763/*
1764 * Try to switch to new modulation mode from MIMO3
1765 */
1766static int rs_move_mimo3_to_other(struct iwl_priv *priv,
1767 struct iwl_lq_sta *lq_sta,
1768 struct ieee80211_conf *conf,
1769 struct ieee80211_sta *sta, int index)
1770{
1771 s8 is_green = lq_sta->is_green;
1772 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1773 struct iwl_scale_tbl_info *search_tbl =
1774 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1775 struct iwl_rate_scale_data *window = &(tbl->win[index]);
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001776 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001777 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1778 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1779 u8 start_action = tbl->action;
1780 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1781 u8 tx_chains_num = priv->hw_params.tx_chains_num;
1782 int ret;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001783 u8 update_search_tbl_counter = 0;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001784
Johannes Berg3ad3b922009-08-07 15:41:48 -07001785 if ((iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE) &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001786 (tbl->action < IWL_MIMO3_SWITCH_SISO_A ||
1787 tbl->action > IWL_MIMO3_SWITCH_SISO_C)) {
1788 /* switch in SISO */
1789 tbl->action = IWL_MIMO3_SWITCH_SISO_A;
1790 }
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001791 for (;;) {
1792 lq_sta->action_counter++;
1793 switch (tbl->action) {
1794 case IWL_MIMO3_SWITCH_ANTENNA1:
1795 case IWL_MIMO3_SWITCH_ANTENNA2:
1796 IWL_DEBUG_RATE(priv, "LQ: MIMO3 toggle Antennas\n");
1797
1798 if (tx_chains_num <= 3)
1799 break;
1800
1801 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1802 break;
1803
1804 memcpy(search_tbl, tbl, sz);
1805 if (rs_toggle_antenna(valid_tx_ant,
1806 &search_tbl->current_rate, search_tbl))
1807 goto out;
1808 break;
1809 case IWL_MIMO3_SWITCH_SISO_A:
1810 case IWL_MIMO3_SWITCH_SISO_B:
1811 case IWL_MIMO3_SWITCH_SISO_C:
1812 IWL_DEBUG_RATE(priv, "LQ: MIMO3 switch to SISO\n");
1813
1814 /* Set up new search table for SISO */
1815 memcpy(search_tbl, tbl, sz);
1816
1817 if (tbl->action == IWL_MIMO3_SWITCH_SISO_A)
1818 search_tbl->ant_type = ANT_A;
1819 else if (tbl->action == IWL_MIMO3_SWITCH_SISO_B)
1820 search_tbl->ant_type = ANT_B;
1821 else
1822 search_tbl->ant_type = ANT_C;
1823
1824 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1825 break;
1826
1827 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
1828 search_tbl, index);
1829 if (!ret)
1830 goto out;
1831
1832 break;
1833
1834 case IWL_MIMO3_SWITCH_MIMO2_AB:
1835 case IWL_MIMO3_SWITCH_MIMO2_AC:
1836 case IWL_MIMO3_SWITCH_MIMO2_BC:
1837 IWL_DEBUG_RATE(priv, "LQ: MIMO3 switch to MIMO2\n");
1838
1839 memcpy(search_tbl, tbl, sz);
1840 search_tbl->is_SGI = 0;
1841 if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AB)
1842 search_tbl->ant_type = ANT_AB;
1843 else if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AC)
1844 search_tbl->ant_type = ANT_AC;
1845 else
1846 search_tbl->ant_type = ANT_BC;
1847
1848 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1849 break;
1850
1851 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
1852 search_tbl, index);
1853 if (!ret)
1854 goto out;
1855
1856 break;
1857
1858 case IWL_MIMO3_SWITCH_GI:
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001859 if (!tbl->is_ht40 && !(ht_cap->cap &
1860 IEEE80211_HT_CAP_SGI_20))
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001861 break;
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001862 if (tbl->is_ht40 && !(ht_cap->cap &
1863 IEEE80211_HT_CAP_SGI_40))
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001864 break;
1865
1866 IWL_DEBUG_RATE(priv, "LQ: MIMO3 toggle SGI/NGI\n");
Ben Cahill77626352007-11-29 11:09:44 +08001867
1868 /* Set up new search table for MIMO */
Zhu Yib481de92007-09-25 17:54:57 -07001869 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001870 search_tbl->is_SGI = !tbl->is_SGI;
Guy Coheneecd6e52008-04-23 17:14:58 -07001871 rs_set_expected_tpt_table(lq_sta, search_tbl);
Ben Cahill77626352007-11-29 11:09:44 +08001872 /*
1873 * If active table already uses the fastest possible
1874 * modulation (dual stream with short guard interval),
1875 * and it's working well, there's no need to look
1876 * for a better type of modulation!
1877 */
Guy Cohen39e88502008-04-23 17:14:57 -07001878 if (tbl->is_SGI) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001879 s32 tpt = lq_sta->last_tpt / 100;
Guy Cohen39e88502008-04-23 17:14:57 -07001880 if (tpt >= search_tbl->expected_tpt[index])
1881 break;
Zhu Yib481de92007-09-25 17:54:57 -07001882 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001883 search_tbl->current_rate =
1884 rate_n_flags_from_tbl(priv, search_tbl,
1885 index, is_green);
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001886 update_search_tbl_counter = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001887 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07001888 }
1889 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001890 if (tbl->action > IWL_MIMO3_SWITCH_GI)
1891 tbl->action = IWL_MIMO3_SWITCH_ANTENNA1;
Zhu Yib481de92007-09-25 17:54:57 -07001892
1893 if (tbl->action == start_action)
1894 break;
1895 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001896 search_tbl->lq_type = LQ_NONE;
Zhu Yib481de92007-09-25 17:54:57 -07001897 return 0;
1898 out:
Guy Cohen3110bef2008-09-09 10:54:54 +08001899 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001900 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001901 if (tbl->action > IWL_MIMO3_SWITCH_GI)
1902 tbl->action = IWL_MIMO3_SWITCH_ANTENNA1;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001903 if (update_search_tbl_counter)
1904 search_tbl->action = tbl->action;
1905
Zhu Yib481de92007-09-25 17:54:57 -07001906 return 0;
1907
1908}
1909
Ben Cahill77626352007-11-29 11:09:44 +08001910/*
1911 * Check whether we should continue using same modulation mode, or
1912 * begin search for a new mode, based on:
1913 * 1) # tx successes or failures while using this mode
1914 * 2) # times calling this function
1915 * 3) elapsed time in this mode (not used, for now)
1916 */
Tomas Winklere227cea2008-07-18 13:53:05 +08001917static void rs_stay_in_table(struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07001918{
Tomas Winklere227cea2008-07-18 13:53:05 +08001919 struct iwl_scale_tbl_info *tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001920 int i;
1921 int active_tbl;
1922 int flush_interval_passed = 0;
Ester Kummerbf403db2008-05-05 10:22:40 +08001923 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07001924
Ester Kummerbf403db2008-05-05 10:22:40 +08001925 priv = lq_sta->drv;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001926 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001927
Tomas Winklerc33104f2008-01-14 17:46:21 -08001928 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07001929
Ben Cahill77626352007-11-29 11:09:44 +08001930 /* If we've been disallowing search, see if we should now allow it */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001931 if (lq_sta->stay_in_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07001932
Ben Cahill77626352007-11-29 11:09:44 +08001933 /* Elapsed time using current modulation mode */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001934 if (lq_sta->flush_timer)
Zhu Yib481de92007-09-25 17:54:57 -07001935 flush_interval_passed =
Mohamed Abbas447fee72009-04-20 14:37:02 -07001936 time_after(jiffies,
1937 (unsigned long)(lq_sta->flush_timer +
Zhu Yib481de92007-09-25 17:54:57 -07001938 IWL_RATE_SCALE_FLUSH_INTVL));
1939
Ben Cahill77626352007-11-29 11:09:44 +08001940 /*
1941 * Check if we should allow search for new modulation mode.
1942 * If many frames have failed or succeeded, or we've used
1943 * this same modulation for a long time, allow search, and
1944 * reset history stats that keep track of whether we should
1945 * allow a new search. Also (below) reset all bitmaps and
1946 * stats in active history.
1947 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001948 if ((lq_sta->total_failed > lq_sta->max_failure_limit) ||
1949 (lq_sta->total_success > lq_sta->max_success_limit) ||
1950 ((!lq_sta->search_better_tbl) && (lq_sta->flush_timer)
Zhu Yib481de92007-09-25 17:54:57 -07001951 && (flush_interval_passed))) {
Tomas Winklere1623442009-01-27 14:27:56 -08001952 IWL_DEBUG_RATE(priv, "LQ: stay is expired %d %d %d\n:",
Tomas Winklerc33104f2008-01-14 17:46:21 -08001953 lq_sta->total_failed,
1954 lq_sta->total_success,
Zhu Yib481de92007-09-25 17:54:57 -07001955 flush_interval_passed);
Ben Cahill77626352007-11-29 11:09:44 +08001956
1957 /* Allow search for new mode */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001958 lq_sta->stay_in_tbl = 0; /* only place reset */
1959 lq_sta->total_failed = 0;
1960 lq_sta->total_success = 0;
1961 lq_sta->flush_timer = 0;
Ben Cahill77626352007-11-29 11:09:44 +08001962
1963 /*
1964 * Else if we've used this modulation mode enough repetitions
1965 * (regardless of elapsed time or success/failure), reset
1966 * history bitmaps and rate-specific stats for all rates in
1967 * active table.
1968 */
Mohamed Abbas403ab562007-11-06 22:06:25 -08001969 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001970 lq_sta->table_count++;
1971 if (lq_sta->table_count >=
1972 lq_sta->table_count_limit) {
1973 lq_sta->table_count = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001974
Tomas Winklere1623442009-01-27 14:27:56 -08001975 IWL_DEBUG_RATE(priv, "LQ: stay in table clear win\n");
Zhu Yib481de92007-09-25 17:54:57 -07001976 for (i = 0; i < IWL_RATE_COUNT; i++)
1977 rs_rate_scale_clear_window(
1978 &(tbl->win[i]));
1979 }
1980 }
1981
Ben Cahill77626352007-11-29 11:09:44 +08001982 /* If transitioning to allow "search", reset all history
1983 * bitmaps and stats in active table (this will become the new
1984 * "search" table). */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001985 if (!lq_sta->stay_in_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07001986 for (i = 0; i < IWL_RATE_COUNT; i++)
1987 rs_rate_scale_clear_window(&(tbl->win[i]));
1988 }
1989 }
1990}
1991
Ben Cahill77626352007-11-29 11:09:44 +08001992/*
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07001993 * setup rate table in uCode
1994 * return rate_n_flags as used in the table
1995 */
1996static u32 rs_update_rate_tbl(struct iwl_priv *priv,
1997 struct iwl_lq_sta *lq_sta,
1998 struct iwl_scale_tbl_info *tbl,
1999 int index, u8 is_green)
2000{
2001 u32 rate;
2002
2003 /* Update uCode's rate table. */
2004 rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
2005 rs_fill_link_cmd(priv, lq_sta, rate);
2006 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
2007
2008 return rate;
2009}
2010
2011/*
Ben Cahill77626352007-11-29 11:09:44 +08002012 * Do rate scaling and search for new modulation mode.
2013 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002014static void rs_rate_scale_perform(struct iwl_priv *priv,
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002015 struct sk_buff *skb,
Johannes Berg4b7679a2008-09-18 18:14:18 +02002016 struct ieee80211_sta *sta,
2017 struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002018{
Johannes Berg4b7679a2008-09-18 18:14:18 +02002019 struct ieee80211_hw *hw = priv->hw;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002020 struct ieee80211_conf *conf = &hw->conf;
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002021 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2022 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002023 int low = IWL_RATE_INVALID;
2024 int high = IWL_RATE_INVALID;
2025 int index;
2026 int i;
Tomas Winklere227cea2008-07-18 13:53:05 +08002027 struct iwl_rate_scale_data *window = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002028 int current_tpt = IWL_INVALID_VALUE;
2029 int low_tpt = IWL_INVALID_VALUE;
2030 int high_tpt = IWL_INVALID_VALUE;
2031 u32 fail_count;
2032 s8 scale_action = 0;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002033 u16 rate_mask;
Zhu Yib481de92007-09-25 17:54:57 -07002034 u8 update_lq = 0;
Tomas Winklere227cea2008-07-18 13:53:05 +08002035 struct iwl_scale_tbl_info *tbl, *tbl1;
Zhu Yib481de92007-09-25 17:54:57 -07002036 u16 rate_scale_index_msk = 0;
Guy Cohen39e88502008-04-23 17:14:57 -07002037 u32 rate;
Zhu Yib481de92007-09-25 17:54:57 -07002038 u8 is_green = 0;
2039 u8 active_tbl = 0;
2040 u8 done_search = 0;
2041 u16 high_low;
Guy Cohena5e8b502008-05-29 16:35:11 +08002042 s32 sr;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002043 u8 tid = MAX_TID_COUNT;
Wey-Yi Guy86b47662009-03-10 14:35:09 -07002044 struct iwl_tid_data *tid_data;
Zhu Yib481de92007-09-25 17:54:57 -07002045
Tomas Winklere1623442009-01-27 14:27:56 -08002046 IWL_DEBUG_RATE(priv, "rate scale calculate new rate for skb\n");
Zhu Yib481de92007-09-25 17:54:57 -07002047
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002048 /* Send management frames and NO_ACK data using lowest rate. */
Tomas Winkler417f1142008-11-12 13:14:06 -08002049 /* TODO: this could probably be improved.. */
2050 if (!ieee80211_is_data(hdr->frame_control) ||
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002051 info->flags & IEEE80211_TX_CTL_NO_ACK)
Zhu Yib481de92007-09-25 17:54:57 -07002052 return;
Zhu Yib481de92007-09-25 17:54:57 -07002053
Johannes Berg4b7679a2008-09-18 18:14:18 +02002054 if (!sta || !lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002055 return;
2056
Johannes Berg4b7679a2008-09-18 18:14:18 +02002057 lq_sta->supp_rates = sta->supp_rates[lq_sta->band];
Zhu Yib481de92007-09-25 17:54:57 -07002058
Ron Rindjunskyfaa29712008-06-12 09:46:58 +08002059 tid = rs_tl_add_packet(lq_sta, hdr);
2060
Ben Cahill77626352007-11-29 11:09:44 +08002061 /*
2062 * Select rate-scale / modulation-mode table to work with in
2063 * the rest of this function: "search" if searching for better
2064 * modulation mode, or "active" if doing rate scaling within a mode.
2065 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002066 if (!lq_sta->search_better_tbl)
2067 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002068 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002069 active_tbl = 1 - lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002070
Tomas Winklerc33104f2008-01-14 17:46:21 -08002071 tbl = &(lq_sta->lq_info[active_tbl]);
Wey-Yi Guy2681b202009-05-21 13:44:22 -07002072 if (is_legacy(tbl->lq_type))
2073 lq_sta->is_green = 0;
2074 else
2075 lq_sta->is_green = rs_use_green(priv, conf);
Tomas Winklerc33104f2008-01-14 17:46:21 -08002076 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07002077
Ben Cahill77626352007-11-29 11:09:44 +08002078 /* current tx rate */
Johannes Bergb7e35002008-09-11 02:22:58 +02002079 index = lq_sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002080
Tomas Winklere1623442009-01-27 14:27:56 -08002081 IWL_DEBUG_RATE(priv, "Rate scale index %d for type %d\n", index,
Zhu Yib481de92007-09-25 17:54:57 -07002082 tbl->lq_type);
2083
Ben Cahill77626352007-11-29 11:09:44 +08002084 /* rates available for this association, and for modulation mode */
Guy Coheneecd6e52008-04-23 17:14:58 -07002085 rate_mask = rs_get_supported_rates(lq_sta, hdr, tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -07002086
Tomas Winklere1623442009-01-27 14:27:56 -08002087 IWL_DEBUG_RATE(priv, "mask 0x%04X \n", rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07002088
2089 /* mask with station rate restriction */
2090 if (is_legacy(tbl->lq_type)) {
Johannes Berg8318d782008-01-24 19:38:38 +01002091 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Ben Cahill77626352007-11-29 11:09:44 +08002092 /* supp_rates has no CCK bits in A mode */
Zhu Yib481de92007-09-25 17:54:57 -07002093 rate_scale_index_msk = (u16) (rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -08002094 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
Zhu Yib481de92007-09-25 17:54:57 -07002095 else
2096 rate_scale_index_msk = (u16) (rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -08002097 lq_sta->supp_rates);
Zhu Yib481de92007-09-25 17:54:57 -07002098
2099 } else
2100 rate_scale_index_msk = rate_mask;
2101
2102 if (!rate_scale_index_msk)
2103 rate_scale_index_msk = rate_mask;
2104
Guy Cohen07bc28e2008-04-23 17:15:03 -07002105 if (!((1 << index) & rate_scale_index_msk)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002106 IWL_ERR(priv, "Current Rate is not valid\n");
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07002107 if (lq_sta->search_better_tbl) {
2108 /* revert to active table if search table is not valid*/
2109 tbl->lq_type = LQ_NONE;
2110 lq_sta->search_better_tbl = 0;
2111 tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
2112 /* get "active" rate info */
2113 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
2114 rate = rs_update_rate_tbl(priv, lq_sta,
2115 tbl, index, is_green);
2116 }
Guy Cohen07bc28e2008-04-23 17:15:03 -07002117 return;
Zhu Yib481de92007-09-25 17:54:57 -07002118 }
2119
Ben Cahill77626352007-11-29 11:09:44 +08002120 /* Get expected throughput table and history window for current rate */
Guy Cohen07bc28e2008-04-23 17:15:03 -07002121 if (!tbl->expected_tpt) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002122 IWL_ERR(priv, "tbl->expected_tpt is NULL\n");
Guy Cohen07bc28e2008-04-23 17:15:03 -07002123 return;
2124 }
Zhu Yib481de92007-09-25 17:54:57 -07002125
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002126 /* force user max rate if set by user */
2127 if ((lq_sta->max_rate_idx != -1) &&
2128 (lq_sta->max_rate_idx < index)) {
2129 index = lq_sta->max_rate_idx;
2130 update_lq = 1;
2131 window = &(tbl->win[index]);
2132 goto lq_update;
2133 }
2134
Zhu Yib481de92007-09-25 17:54:57 -07002135 window = &(tbl->win[index]);
2136
Ben Cahill77626352007-11-29 11:09:44 +08002137 /*
2138 * If there is not enough history to calculate actual average
2139 * throughput, keep analyzing results of more tx frames, without
2140 * changing rate or mode (bypass most of the rest of this function).
2141 * Set up new rate table in uCode only if old rate is not supported
2142 * in current association (use new rate found above).
2143 */
Zhu Yib481de92007-09-25 17:54:57 -07002144 fail_count = window->counter - window->success_counter;
Guy Cohen07bc28e2008-04-23 17:15:03 -07002145 if ((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
2146 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002147 IWL_DEBUG_RATE(priv, "LQ: still below TH. succ=%d total=%d "
Zhu Yib481de92007-09-25 17:54:57 -07002148 "for index %d\n",
2149 window->success_counter, window->counter, index);
Ben Cahill77626352007-11-29 11:09:44 +08002150
2151 /* Can't calculate this yet; not enough history */
Zhu Yib481de92007-09-25 17:54:57 -07002152 window->average_tpt = IWL_INVALID_VALUE;
Ben Cahill77626352007-11-29 11:09:44 +08002153
2154 /* Should we stay with this modulation mode,
2155 * or search for a new one? */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002156 rs_stay_in_table(lq_sta);
Ben Cahill77626352007-11-29 11:09:44 +08002157
Zhu Yib481de92007-09-25 17:54:57 -07002158 goto out;
Guy Cohen3110bef2008-09-09 10:54:54 +08002159 }
Zhu Yib481de92007-09-25 17:54:57 -07002160
Ben Cahill77626352007-11-29 11:09:44 +08002161 /* Else we have enough samples; calculate estimate of
2162 * actual average throughput */
Guy Cohen3110bef2008-09-09 10:54:54 +08002163
2164 BUG_ON(window->average_tpt != ((window->success_ratio *
2165 tbl->expected_tpt[index] + 64) / 128));
Zhu Yib481de92007-09-25 17:54:57 -07002166
Ben Cahill77626352007-11-29 11:09:44 +08002167 /* If we are searching for better modulation mode, check success. */
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002168 if (lq_sta->search_better_tbl &&
Johannes Berg3ad3b922009-08-07 15:41:48 -07002169 (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_MULTI)) {
Ben Cahill77626352007-11-29 11:09:44 +08002170 /* If good success, continue using the "search" mode;
2171 * no need to send new link quality command, since we're
2172 * continuing to use the setup that we've been trying. */
Guy Cohen07bc28e2008-04-23 17:15:03 -07002173 if (window->average_tpt > lq_sta->last_tpt) {
2174
Tomas Winklere1623442009-01-27 14:27:56 -08002175 IWL_DEBUG_RATE(priv, "LQ: SWITCHING TO NEW TABLE "
Guy Cohen07bc28e2008-04-23 17:15:03 -07002176 "suc=%d cur-tpt=%d old-tpt=%d\n",
2177 window->success_ratio,
2178 window->average_tpt,
2179 lq_sta->last_tpt);
2180
2181 if (!is_legacy(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08002182 lq_sta->enable_counter = 1;
Guy Cohen07bc28e2008-04-23 17:15:03 -07002183
Ben Cahill77626352007-11-29 11:09:44 +08002184 /* Swap tables; "search" becomes "active" */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002185 lq_sta->active_tbl = active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002186 current_tpt = window->average_tpt;
Ben Cahill77626352007-11-29 11:09:44 +08002187
2188 /* Else poor success; go back to mode in "active" table */
Zhu Yib481de92007-09-25 17:54:57 -07002189 } else {
Guy Cohen07bc28e2008-04-23 17:15:03 -07002190
Tomas Winklere1623442009-01-27 14:27:56 -08002191 IWL_DEBUG_RATE(priv, "LQ: GOING BACK TO THE OLD TABLE "
Guy Cohen07bc28e2008-04-23 17:15:03 -07002192 "suc=%d cur-tpt=%d old-tpt=%d\n",
2193 window->success_ratio,
2194 window->average_tpt,
2195 lq_sta->last_tpt);
2196
Ben Cahill77626352007-11-29 11:09:44 +08002197 /* Nullify "search" table */
Zhu Yib481de92007-09-25 17:54:57 -07002198 tbl->lq_type = LQ_NONE;
Ben Cahill77626352007-11-29 11:09:44 +08002199
2200 /* Revert to "active" table */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002201 active_tbl = lq_sta->active_tbl;
2202 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07002203
Ben Cahill77626352007-11-29 11:09:44 +08002204 /* Revert to "active" rate and throughput info */
Tomas Winklere7d326a2008-06-12 09:47:11 +08002205 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
Tomas Winklerc33104f2008-01-14 17:46:21 -08002206 current_tpt = lq_sta->last_tpt;
Ben Cahill77626352007-11-29 11:09:44 +08002207
2208 /* Need to set up a new rate table in uCode */
2209 update_lq = 1;
Zhu Yib481de92007-09-25 17:54:57 -07002210 }
Ben Cahill77626352007-11-29 11:09:44 +08002211
2212 /* Either way, we've made a decision; modulation mode
2213 * search is done, allow rate adjustment next time. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002214 lq_sta->search_better_tbl = 0;
Ben Cahill77626352007-11-29 11:09:44 +08002215 done_search = 1; /* Don't switch modes below! */
Zhu Yib481de92007-09-25 17:54:57 -07002216 goto lq_update;
2217 }
2218
Ben Cahill77626352007-11-29 11:09:44 +08002219 /* (Else) not in search of better modulation mode, try for better
2220 * starting rate, while staying in this mode. */
Ester Kummerbf403db2008-05-05 10:22:40 +08002221 high_low = rs_get_adjacent_rate(priv, index, rate_scale_index_msk,
Zhu Yib481de92007-09-25 17:54:57 -07002222 tbl->lq_type);
2223 low = high_low & 0xff;
2224 high = (high_low >> 8) & 0xff;
2225
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002226 /* If user set max rate, dont allow higher than user constrain */
2227 if ((lq_sta->max_rate_idx != -1) &&
2228 (lq_sta->max_rate_idx < high))
2229 high = IWL_RATE_INVALID;
2230
Guy Cohena5e8b502008-05-29 16:35:11 +08002231 sr = window->success_ratio;
2232
Ben Cahill77626352007-11-29 11:09:44 +08002233 /* Collect measured throughputs for current and adjacent rates */
Zhu Yib481de92007-09-25 17:54:57 -07002234 current_tpt = window->average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07002235 if (low != IWL_RATE_INVALID)
2236 low_tpt = tbl->win[low].average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07002237 if (high != IWL_RATE_INVALID)
2238 high_tpt = tbl->win[high].average_tpt;
2239
Guy Cohena5e8b502008-05-29 16:35:11 +08002240 scale_action = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002241
Ben Cahill77626352007-11-29 11:09:44 +08002242 /* Too many failures, decrease rate */
Guy Cohena5e8b502008-05-29 16:35:11 +08002243 if ((sr <= IWL_RATE_DECREASE_TH) || (current_tpt == 0)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002244 IWL_DEBUG_RATE(priv, "decrease rate because of low success_ratio\n");
Zhu Yib481de92007-09-25 17:54:57 -07002245 scale_action = -1;
Ben Cahill77626352007-11-29 11:09:44 +08002246
2247 /* No throughput measured yet for adjacent rates; try increase. */
Zhu Yib481de92007-09-25 17:54:57 -07002248 } else if ((low_tpt == IWL_INVALID_VALUE) &&
Guy Cohena5e8b502008-05-29 16:35:11 +08002249 (high_tpt == IWL_INVALID_VALUE)) {
2250
2251 if (high != IWL_RATE_INVALID && sr >= IWL_RATE_INCREASE_TH)
2252 scale_action = 1;
2253 else if (low != IWL_RATE_INVALID)
Wey-Yi Guy8fe72312009-03-10 14:35:10 -07002254 scale_action = 0;
Guy Cohena5e8b502008-05-29 16:35:11 +08002255 }
Ben Cahill77626352007-11-29 11:09:44 +08002256
2257 /* Both adjacent throughputs are measured, but neither one has better
2258 * throughput; we're using the best rate, don't change it! */
Zhu Yib481de92007-09-25 17:54:57 -07002259 else if ((low_tpt != IWL_INVALID_VALUE) &&
2260 (high_tpt != IWL_INVALID_VALUE) &&
2261 (low_tpt < current_tpt) &&
2262 (high_tpt < current_tpt))
2263 scale_action = 0;
Ben Cahill77626352007-11-29 11:09:44 +08002264
2265 /* At least one adjacent rate's throughput is measured,
2266 * and may have better performance. */
Zhu Yib481de92007-09-25 17:54:57 -07002267 else {
Ben Cahill77626352007-11-29 11:09:44 +08002268 /* Higher adjacent rate's throughput is measured */
Zhu Yib481de92007-09-25 17:54:57 -07002269 if (high_tpt != IWL_INVALID_VALUE) {
Ben Cahill77626352007-11-29 11:09:44 +08002270 /* Higher rate has better throughput */
Guy Cohena5e8b502008-05-29 16:35:11 +08002271 if (high_tpt > current_tpt &&
2272 sr >= IWL_RATE_INCREASE_TH) {
Zhu Yib481de92007-09-25 17:54:57 -07002273 scale_action = 1;
Guy Cohena5e8b502008-05-29 16:35:11 +08002274 } else {
Wey-Yi Guy8fe72312009-03-10 14:35:10 -07002275 scale_action = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002276 }
Ben Cahill77626352007-11-29 11:09:44 +08002277
2278 /* Lower adjacent rate's throughput is measured */
Zhu Yib481de92007-09-25 17:54:57 -07002279 } else if (low_tpt != IWL_INVALID_VALUE) {
Ben Cahill77626352007-11-29 11:09:44 +08002280 /* Lower rate has better throughput */
Zhu Yib481de92007-09-25 17:54:57 -07002281 if (low_tpt > current_tpt) {
Tomas Winklere1623442009-01-27 14:27:56 -08002282 IWL_DEBUG_RATE(priv,
2283 "decrease rate because of low tpt\n");
Zhu Yib481de92007-09-25 17:54:57 -07002284 scale_action = -1;
Guy Cohena5e8b502008-05-29 16:35:11 +08002285 } else if (sr >= IWL_RATE_INCREASE_TH) {
Zhu Yib481de92007-09-25 17:54:57 -07002286 scale_action = 1;
Guy Cohena5e8b502008-05-29 16:35:11 +08002287 }
Zhu Yib481de92007-09-25 17:54:57 -07002288 }
2289 }
2290
Ben Cahill77626352007-11-29 11:09:44 +08002291 /* Sanity check; asked for decrease, but success rate or throughput
2292 * has been good at old rate. Don't change it. */
Guy Cohena5e8b502008-05-29 16:35:11 +08002293 if ((scale_action == -1) && (low != IWL_RATE_INVALID) &&
2294 ((sr > IWL_RATE_HIGH_TH) ||
Zhu Yib481de92007-09-25 17:54:57 -07002295 (current_tpt > (100 * tbl->expected_tpt[low]))))
Zhu Yib481de92007-09-25 17:54:57 -07002296 scale_action = 0;
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002297 if (!iwl_ht_enabled(priv) && !is_legacy(tbl->lq_type))
2298 scale_action = -1;
Johannes Berg3ad3b922009-08-07 15:41:48 -07002299 if (iwl_tx_ant_restriction(priv) != IWL_ANT_OK_MULTI &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002300 (is_mimo2(tbl->lq_type) || is_mimo3(tbl->lq_type)))
2301 scale_action = -1;
Zhu Yib481de92007-09-25 17:54:57 -07002302 switch (scale_action) {
2303 case -1:
Ben Cahill77626352007-11-29 11:09:44 +08002304 /* Decrease starting rate, update uCode's rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002305 if (low != IWL_RATE_INVALID) {
2306 update_lq = 1;
2307 index = low;
2308 }
Mohamed Abbas447fee72009-04-20 14:37:02 -07002309
Zhu Yib481de92007-09-25 17:54:57 -07002310 break;
2311 case 1:
Ben Cahill77626352007-11-29 11:09:44 +08002312 /* Increase starting rate, update uCode's rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002313 if (high != IWL_RATE_INVALID) {
2314 update_lq = 1;
2315 index = high;
2316 }
2317
2318 break;
2319 case 0:
Ben Cahill77626352007-11-29 11:09:44 +08002320 /* No change */
Zhu Yib481de92007-09-25 17:54:57 -07002321 default:
2322 break;
2323 }
2324
Tomas Winklere1623442009-01-27 14:27:56 -08002325 IWL_DEBUG_RATE(priv, "choose rate scale index %d action %d low %d "
Zhu Yib481de92007-09-25 17:54:57 -07002326 "high %d type %d\n",
2327 index, scale_action, low, high, tbl->lq_type);
2328
Guy Cohena5e8b502008-05-29 16:35:11 +08002329lq_update:
Ben Cahill77626352007-11-29 11:09:44 +08002330 /* Replace uCode's rate table for the destination station. */
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07002331 if (update_lq)
2332 rate = rs_update_rate_tbl(priv, lq_sta,
2333 tbl, index, is_green);
Ben Cahill77626352007-11-29 11:09:44 +08002334
Johannes Berg3ad3b922009-08-07 15:41:48 -07002335 if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_MULTI) {
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002336 /* Should we stay with this modulation mode,
2337 * or search for a new one? */
2338 rs_stay_in_table(lq_sta);
2339 }
Ben Cahill77626352007-11-29 11:09:44 +08002340 /*
2341 * Search for new modulation mode if we're:
2342 * 1) Not changing rates right now
2343 * 2) Not just finishing up a search
2344 * 3) Allowing a new search
2345 */
Guy Cohen47cfd462008-05-27 11:29:34 +08002346 if (!update_lq && !done_search && !lq_sta->stay_in_tbl && window->counter) {
Ben Cahill77626352007-11-29 11:09:44 +08002347 /* Save current throughput to compare with "search" throughput*/
Tomas Winklerc33104f2008-01-14 17:46:21 -08002348 lq_sta->last_tpt = current_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07002349
Ben Cahill77626352007-11-29 11:09:44 +08002350 /* Select a new "search" modulation mode to try.
2351 * If one is found, set up the new "search" table. */
Zhu Yib481de92007-09-25 17:54:57 -07002352 if (is_legacy(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08002353 rs_move_legacy_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07002354 else if (is_siso(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08002355 rs_move_siso_to_other(priv, lq_sta, conf, sta, index);
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07002356 else if (is_mimo2(tbl->lq_type))
2357 rs_move_mimo2_to_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07002358 else
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07002359 rs_move_mimo3_to_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07002360
Ben Cahill77626352007-11-29 11:09:44 +08002361 /* If new "search" mode was selected, set up in uCode table */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002362 if (lq_sta->search_better_tbl) {
Ben Cahill77626352007-11-29 11:09:44 +08002363 /* Access the "search" table, clear its history. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002364 tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Zhu Yib481de92007-09-25 17:54:57 -07002365 for (i = 0; i < IWL_RATE_COUNT; i++)
2366 rs_rate_scale_clear_window(&(tbl->win[i]));
2367
Ben Cahill77626352007-11-29 11:09:44 +08002368 /* Use new "search" start rate */
Tomas Winklere7d326a2008-06-12 09:47:11 +08002369 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002370
Tomas Winklere1623442009-01-27 14:27:56 -08002371 IWL_DEBUG_RATE(priv, "Switch current mcs: %X index: %d\n",
Guy Cohen39e88502008-04-23 17:14:57 -07002372 tbl->current_rate, index);
Guy Cohen07bc28e2008-04-23 17:15:03 -07002373 rs_fill_link_cmd(priv, lq_sta, tbl->current_rate);
Tomas Winkler66c73db2008-04-15 16:01:40 -07002374 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
Mohamed Abbas447fee72009-04-20 14:37:02 -07002375 } else
2376 done_search = 1;
2377 }
Zhu Yib481de92007-09-25 17:54:57 -07002378
Mohamed Abbas447fee72009-04-20 14:37:02 -07002379 if (done_search && !lq_sta->stay_in_tbl) {
Ben Cahill77626352007-11-29 11:09:44 +08002380 /* If the "active" (non-search) mode was legacy,
2381 * and we've tried switching antennas,
2382 * but we haven't been able to try HT modes (not available),
2383 * stay with best antenna legacy modulation for a while
2384 * before next round of mode comparisons. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002385 tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
Luis R. Rodriguezde27e642008-12-23 15:58:44 -08002386 if (is_legacy(tbl1->lq_type) && !conf_is_ht(conf) &&
Mohamed Abbasd6e93392009-05-08 13:44:39 -07002387 lq_sta->action_counter > tbl1->max_search) {
Tomas Winklere1623442009-01-27 14:27:56 -08002388 IWL_DEBUG_RATE(priv, "LQ: STAY in legacy table\n");
Ester Kummerbf403db2008-05-05 10:22:40 +08002389 rs_set_stay_in_table(priv, 1, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002390 }
2391
Ben Cahill77626352007-11-29 11:09:44 +08002392 /* If we're in an HT mode, and all 3 mode switch actions
2393 * have been tried and compared, stay in this best modulation
2394 * mode for a while before next round of mode comparisons. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002395 if (lq_sta->enable_counter &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002396 (lq_sta->action_counter >= tbl1->max_search) &&
2397 iwl_ht_enabled(priv)) {
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002398 if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
2399 (lq_sta->tx_agg_tid_en & (1 << tid)) &&
2400 (tid != MAX_TID_COUNT)) {
Wey-Yi Guy86b47662009-03-10 14:35:09 -07002401 tid_data =
2402 &priv->stations[lq_sta->lq.sta_id].tid[tid];
2403 if (tid_data->agg.state == IWL_AGG_OFF) {
2404 IWL_DEBUG_RATE(priv,
2405 "try to aggregate tid %d\n",
2406 tid);
2407 rs_tl_turn_on_agg(priv, tid,
2408 lq_sta, sta);
2409 }
Zhu Yib481de92007-09-25 17:54:57 -07002410 }
Ester Kummerbf403db2008-05-05 10:22:40 +08002411 rs_set_stay_in_table(priv, 0, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002412 }
Zhu Yib481de92007-09-25 17:54:57 -07002413 }
2414
2415out:
Tomas Winkler978785a2008-12-19 10:37:31 +08002416 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
Zhu Yib481de92007-09-25 17:54:57 -07002417 i = index;
Johannes Bergb7e35002008-09-11 02:22:58 +02002418 lq_sta->last_txrate_idx = i;
Zhu Yib481de92007-09-25 17:54:57 -07002419
Zhu Yib481de92007-09-25 17:54:57 -07002420 return;
2421}
2422
2423
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002424static void rs_initialize_lq(struct iwl_priv *priv,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002425 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02002426 struct ieee80211_sta *sta,
2427 struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002428{
Tomas Winklere227cea2008-07-18 13:53:05 +08002429 struct iwl_scale_tbl_info *tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002430 int rate_idx;
Guy Cohenaade00c2008-04-23 17:14:59 -07002431 int i;
Guy Cohen39e88502008-04-23 17:14:57 -07002432 u32 rate;
Guy Cohenaade00c2008-04-23 17:14:59 -07002433 u8 use_green = rs_use_green(priv, conf);
2434 u8 active_tbl = 0;
2435 u8 valid_tx_ant;
Zhu Yib481de92007-09-25 17:54:57 -07002436
Johannes Berg4b7679a2008-09-18 18:14:18 +02002437 if (!sta || !lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002438 goto out;
2439
Johannes Bergb7e35002008-09-11 02:22:58 +02002440 i = lq_sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002441
Tomas Winklerc33104f2008-01-14 17:46:21 -08002442 if ((lq_sta->lq.sta_id == 0xff) &&
Johannes Berg05c914f2008-09-11 00:01:58 +02002443 (priv->iw_mode == NL80211_IFTYPE_ADHOC))
Zhu Yib481de92007-09-25 17:54:57 -07002444 goto out;
2445
Guy Cohenaade00c2008-04-23 17:14:59 -07002446 valid_tx_ant = priv->hw_params.valid_tx_ant;
2447
Tomas Winklerc33104f2008-01-14 17:46:21 -08002448 if (!lq_sta->search_better_tbl)
2449 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002450 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002451 active_tbl = 1 - lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002452
Tomas Winklerc33104f2008-01-14 17:46:21 -08002453 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07002454
2455 if ((i < 0) || (i >= IWL_RATE_COUNT))
2456 i = 0;
2457
Tomas Winkler1826dcc2008-05-15 13:54:02 +08002458 rate = iwl_rates[i].plcp;
Winkler, Tomaseb48dca2008-10-29 14:05:48 -07002459 tbl->ant_type = first_antenna(valid_tx_ant);
2460 rate |= tbl->ant_type << RATE_MCS_ANT_POS;
Zhu Yib481de92007-09-25 17:54:57 -07002461
2462 if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
Guy Cohen39e88502008-04-23 17:14:57 -07002463 rate |= RATE_MCS_CCK_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002464
Guy Cohen39e88502008-04-23 17:14:57 -07002465 rs_get_tbl_info_from_mcs(rate, priv->band, tbl, &rate_idx);
Guy Cohenaade00c2008-04-23 17:14:59 -07002466 if (!rs_is_valid_ant(valid_tx_ant, tbl->ant_type))
2467 rs_toggle_antenna(valid_tx_ant, &rate, tbl);
Zhu Yib481de92007-09-25 17:54:57 -07002468
Tomas Winkler978785a2008-12-19 10:37:31 +08002469 rate = rate_n_flags_from_tbl(priv, tbl, rate_idx, use_green);
Guy Cohen39e88502008-04-23 17:14:57 -07002470 tbl->current_rate = rate;
Guy Coheneecd6e52008-04-23 17:14:58 -07002471 rs_set_expected_tpt_table(lq_sta, tbl);
Guy Cohen07bc28e2008-04-23 17:15:03 -07002472 rs_fill_link_cmd(NULL, lq_sta, rate);
Tomas Winkler66c73db2008-04-15 16:01:40 -07002473 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07002474 out:
2475 return;
2476}
2477
Johannes Berge6a98542008-10-21 12:40:02 +02002478static void rs_get_rate(void *priv_r, struct ieee80211_sta *sta, void *priv_sta,
2479 struct ieee80211_tx_rate_control *txrc)
Zhu Yib481de92007-09-25 17:54:57 -07002480{
2481
Johannes Berge6a98542008-10-21 12:40:02 +02002482 struct sk_buff *skb = txrc->skb;
2483 struct ieee80211_supported_band *sband = txrc->sband;
Johannes Berg4b7679a2008-09-18 18:14:18 +02002484 struct iwl_priv *priv = (struct iwl_priv *)priv_r;
2485 struct ieee80211_conf *conf = &priv->hw->conf;
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002486 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Zhu Yib481de92007-09-25 17:54:57 -07002487 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berge6a98542008-10-21 12:40:02 +02002488 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Tomas Winkler417f1142008-11-12 13:14:06 -08002489 struct iwl_lq_sta *lq_sta = priv_sta;
2490 int rate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002491
Tomas Winklere1623442009-01-27 14:27:56 -08002492 IWL_DEBUG_RATE_LIMIT(priv, "rate scale calculate new rate for skb\n");
Zhu Yib481de92007-09-25 17:54:57 -07002493
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002494 /* Get max rate if user set max rate */
2495 if (lq_sta) {
2496 lq_sta->max_rate_idx = txrc->max_rate_idx;
2497 if ((sband->band == IEEE80211_BAND_5GHZ) &&
2498 (lq_sta->max_rate_idx != -1))
2499 lq_sta->max_rate_idx += IWL_FIRST_OFDM_RATE;
2500 if ((lq_sta->max_rate_idx < 0) ||
2501 (lq_sta->max_rate_idx >= IWL_RATE_COUNT))
2502 lq_sta->max_rate_idx = -1;
2503 }
2504
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002505 /* Send management frames and NO_ACK data using lowest rate. */
Luis R. Rodriguez4c6d4f52009-07-16 10:05:41 -07002506 if (rate_control_send_low(sta, priv_sta, txrc))
Johannes Berg4b7679a2008-09-18 18:14:18 +02002507 return;
Zhu Yib481de92007-09-25 17:54:57 -07002508
Tomas Winkler417f1142008-11-12 13:14:06 -08002509 rate_idx = lq_sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002510
Johannes Berg05c914f2008-09-11 00:01:58 +02002511 if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) &&
Tomas Winklerc33104f2008-01-14 17:46:21 -08002512 !lq_sta->ibss_sta_added) {
Tomas Winklerc587de02009-06-03 11:44:07 -07002513 u8 sta_id = iwl_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002514
2515 if (sta_id == IWL_INVALID_STATION) {
Tomas Winklere1623442009-01-27 14:27:56 -08002516 IWL_DEBUG_RATE(priv, "LQ: ADD station %pM\n",
Johannes Berge1749612008-10-27 15:59:26 -07002517 hdr->addr1);
Tomas Winklerc587de02009-06-03 11:44:07 -07002518 sta_id = iwl_add_station(priv, hdr->addr1,
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002519 false, CMD_ASYNC, ht_cap);
Zhu Yib481de92007-09-25 17:54:57 -07002520 }
2521 if ((sta_id != IWL_INVALID_STATION)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002522 lq_sta->lq.sta_id = sta_id;
2523 lq_sta->lq.rs_table[0].rate_n_flags = 0;
2524 lq_sta->ibss_sta_added = 1;
Johannes Berg4b7679a2008-09-18 18:14:18 +02002525 rs_initialize_lq(priv, conf, sta, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002526 }
Zhu Yib481de92007-09-25 17:54:57 -07002527 }
2528
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002529 if (lq_sta->last_rate_n_flags & RATE_MCS_HT_MSK) {
Tomas Winkler417f1142008-11-12 13:14:06 -08002530 rate_idx -= IWL_FIRST_OFDM_RATE;
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002531 /* 6M and 9M shared same MCS index */
2532 rate_idx = (rate_idx > 0) ? (rate_idx - 1) : 0;
2533 if (rs_extract_rate(lq_sta->last_rate_n_flags) >=
2534 IWL_RATE_MIMO3_6M_PLCP)
2535 rate_idx = rate_idx + (2 * MCS_INDEX_PER_STREAM);
2536 else if (rs_extract_rate(lq_sta->last_rate_n_flags) >=
2537 IWL_RATE_MIMO2_6M_PLCP)
2538 rate_idx = rate_idx + MCS_INDEX_PER_STREAM;
2539 info->control.rates[0].flags = IEEE80211_TX_RC_MCS;
2540 if (lq_sta->last_rate_n_flags & RATE_MCS_SGI_MSK)
2541 info->control.rates[0].flags |= IEEE80211_TX_RC_SHORT_GI;
2542 if (lq_sta->last_rate_n_flags & RATE_MCS_DUP_MSK)
2543 info->control.rates[0].flags |= IEEE80211_TX_RC_DUP_DATA;
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07002544 if (lq_sta->last_rate_n_flags & RATE_MCS_HT40_MSK)
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002545 info->control.rates[0].flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
2546 if (lq_sta->last_rate_n_flags & RATE_MCS_GF_MSK)
2547 info->control.rates[0].flags |= IEEE80211_TX_RC_GREEN_FIELD;
2548 } else {
2549 if (rate_idx < 0 || rate_idx > IWL_RATE_COUNT)
2550 rate_idx = rate_lowest_index(sband, sta);
2551 else if (sband->band == IEEE80211_BAND_5GHZ)
2552 rate_idx -= IWL_FIRST_OFDM_RATE;
2553 }
Tomas Winkler417f1142008-11-12 13:14:06 -08002554 info->control.rates[0].idx = rate_idx;
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002555
Zhu Yib481de92007-09-25 17:54:57 -07002556}
2557
Johannes Berg4b7679a2008-09-18 18:14:18 +02002558static void *rs_alloc_sta(void *priv_rate, struct ieee80211_sta *sta,
2559 gfp_t gfp)
Zhu Yib481de92007-09-25 17:54:57 -07002560{
Tomas Winklere227cea2008-07-18 13:53:05 +08002561 struct iwl_lq_sta *lq_sta;
Ester Kummerbf403db2008-05-05 10:22:40 +08002562 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07002563 int i, j;
2564
Ester Kummerbf403db2008-05-05 10:22:40 +08002565 priv = (struct iwl_priv *)priv_rate;
Tomas Winklere1623442009-01-27 14:27:56 -08002566 IWL_DEBUG_RATE(priv, "create station rate scale window\n");
Zhu Yib481de92007-09-25 17:54:57 -07002567
Tomas Winklere227cea2008-07-18 13:53:05 +08002568 lq_sta = kzalloc(sizeof(struct iwl_lq_sta), gfp);
Zhu Yib481de92007-09-25 17:54:57 -07002569
Tomas Winklerc33104f2008-01-14 17:46:21 -08002570 if (lq_sta == NULL)
Zhu Yib481de92007-09-25 17:54:57 -07002571 return NULL;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002572 lq_sta->lq.sta_id = 0xff;
Zhu Yib481de92007-09-25 17:54:57 -07002573
Zhu Yi63fddb92007-09-27 11:27:36 +08002574
Zhu Yib481de92007-09-25 17:54:57 -07002575 for (j = 0; j < LQ_SIZE; j++)
2576 for (i = 0; i < IWL_RATE_COUNT; i++)
Tomas Winkler3ac7f142008-07-21 02:40:14 +03002577 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
Zhu Yib481de92007-09-25 17:54:57 -07002578
Tomas Winklerc33104f2008-01-14 17:46:21 -08002579 return lq_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002580}
2581
Johannes Berg4b7679a2008-09-18 18:14:18 +02002582static void rs_rate_init(void *priv_r, struct ieee80211_supported_band *sband,
2583 struct ieee80211_sta *sta, void *priv_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002584{
2585 int i, j;
Johannes Berg4b7679a2008-09-18 18:14:18 +02002586 struct iwl_priv *priv = (struct iwl_priv *)priv_r;
2587 struct ieee80211_conf *conf = &priv->hw->conf;
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002588 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Tomas Winklere227cea2008-07-18 13:53:05 +08002589 struct iwl_lq_sta *lq_sta = priv_sta;
Mohamed Abbasd5e49032008-12-12 08:22:15 -08002590 u16 mask_bit = 0;
Abbas, Mohamed7d049e52009-01-20 21:33:53 -08002591 int count;
2592 int start_rate = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002593
Tomas Winklerc33104f2008-01-14 17:46:21 -08002594 lq_sta->flush_timer = 0;
Johannes Berg4b7679a2008-09-18 18:14:18 +02002595 lq_sta->supp_rates = sta->supp_rates[sband->band];
Zhu Yib481de92007-09-25 17:54:57 -07002596 for (j = 0; j < LQ_SIZE; j++)
2597 for (i = 0; i < IWL_RATE_COUNT; i++)
Tomas Winkler3ac7f142008-07-21 02:40:14 +03002598 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
Zhu Yib481de92007-09-25 17:54:57 -07002599
Tomas Winklere1623442009-01-27 14:27:56 -08002600 IWL_DEBUG_RATE(priv, "LQ: *** rate scale station global init ***\n");
Zhu Yib481de92007-09-25 17:54:57 -07002601 /* TODO: what is a good starting rate for STA? About middle? Maybe not
2602 * the lowest or the highest rate.. Could consider using RSSI from
2603 * previous packets? Need to have IEEE 802.1X auth succeed immediately
2604 * after assoc.. */
2605
Tomas Winklerc33104f2008-01-14 17:46:21 -08002606 lq_sta->ibss_sta_added = 0;
Johannes Berg05c914f2008-09-11 00:01:58 +02002607 if (priv->iw_mode == NL80211_IFTYPE_AP) {
Tomas Winklerc587de02009-06-03 11:44:07 -07002608 u8 sta_id = iwl_find_station(priv,
Abhijeet Kolekare11bc022009-04-08 11:26:41 -07002609 sta->addr);
Joe Perches0795af52007-10-03 17:59:30 -07002610
Zhu Yib481de92007-09-25 17:54:57 -07002611 /* for IBSS the call are from tasklet */
Tomas Winklere1623442009-01-27 14:27:56 -08002612 IWL_DEBUG_RATE(priv, "LQ: ADD station %pM\n", sta->addr);
Zhu Yib481de92007-09-25 17:54:57 -07002613
2614 if (sta_id == IWL_INVALID_STATION) {
Tomas Winklere1623442009-01-27 14:27:56 -08002615 IWL_DEBUG_RATE(priv, "LQ: ADD station %pM\n", sta->addr);
Tomas Winklerc587de02009-06-03 11:44:07 -07002616 sta_id = iwl_add_station(priv, sta->addr, false,
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002617 CMD_ASYNC, ht_cap);
Zhu Yib481de92007-09-25 17:54:57 -07002618 }
2619 if ((sta_id != IWL_INVALID_STATION)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002620 lq_sta->lq.sta_id = sta_id;
2621 lq_sta->lq.rs_table[0].rate_n_flags = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002622 }
2623 /* FIXME: this is w/a remove it later */
2624 priv->assoc_station_added = 1;
2625 }
2626
Tomas Winklerc33104f2008-01-14 17:46:21 -08002627 lq_sta->is_dup = 0;
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002628 lq_sta->max_rate_idx = -1;
Abbas, Mohamed7d049e52009-01-20 21:33:53 -08002629 lq_sta->missed_rate_counter = IWL_MISSED_RATE_MAX;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002630 lq_sta->is_green = rs_use_green(priv, conf);
Guy Cohen39e88502008-04-23 17:14:57 -07002631 lq_sta->active_legacy_rate = priv->active_rate & ~(0x1000);
Tomas Winklerc33104f2008-01-14 17:46:21 -08002632 lq_sta->active_rate_basic = priv->active_rate_basic;
Johannes Berg8318d782008-01-24 19:38:38 +01002633 lq_sta->band = priv->band;
Ben Cahill77626352007-11-29 11:09:44 +08002634 /*
2635 * active_siso_rate mask includes 9 MBits (bit 5), and CCK (bits 0-3),
2636 * supp_rates[] does not; shift to convert format, force 9 MBits off.
2637 */
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002638 lq_sta->active_siso_rate = ht_cap->mcs.rx_mask[0] << 1;
2639 lq_sta->active_siso_rate |= ht_cap->mcs.rx_mask[0] & 0x1;
Guy Cohenfde0db32008-04-21 15:42:01 -07002640 lq_sta->active_siso_rate &= ~((u16)0x2);
2641 lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -07002642
Ben Cahill77626352007-11-29 11:09:44 +08002643 /* Same here */
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002644 lq_sta->active_mimo2_rate = ht_cap->mcs.rx_mask[1] << 1;
2645 lq_sta->active_mimo2_rate |= ht_cap->mcs.rx_mask[1] & 0x1;
Guy Cohenfde0db32008-04-21 15:42:01 -07002646 lq_sta->active_mimo2_rate &= ~((u16)0x2);
2647 lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE;
2648
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002649 lq_sta->active_mimo3_rate = ht_cap->mcs.rx_mask[2] << 1;
2650 lq_sta->active_mimo3_rate |= ht_cap->mcs.rx_mask[2] & 0x1;
Guy Cohenfde0db32008-04-21 15:42:01 -07002651 lq_sta->active_mimo3_rate &= ~((u16)0x2);
2652 lq_sta->active_mimo3_rate <<= IWL_FIRST_OFDM_RATE;
2653
Tomas Winklere1623442009-01-27 14:27:56 -08002654 IWL_DEBUG_RATE(priv, "SISO-RATE=%X MIMO2-RATE=%X MIMO3-RATE=%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002655 lq_sta->active_siso_rate,
Guy Cohenfde0db32008-04-21 15:42:01 -07002656 lq_sta->active_mimo2_rate,
2657 lq_sta->active_mimo3_rate);
2658
Tomas Winklera96a27f2008-10-23 23:48:56 -07002659 /* These values will be overridden later */
Guy Cohen07bc28e2008-04-23 17:15:03 -07002660 lq_sta->lq.general_params.single_stream_ant_msk = ANT_A;
2661 lq_sta->lq.general_params.dual_stream_ant_msk = ANT_AB;
2662
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002663 /* as default allow aggregation for all tids */
2664 lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002665 lq_sta->drv = priv;
Zhu Yib481de92007-09-25 17:54:57 -07002666
Mohamed Abbasd5e49032008-12-12 08:22:15 -08002667 /* Find highest tx rate supported by hardware and destination station */
Abbas, Mohamed7d049e52009-01-20 21:33:53 -08002668 mask_bit = sta->supp_rates[sband->band];
2669 count = sband->n_bitrates;
2670 if (sband->band == IEEE80211_BAND_5GHZ) {
2671 count += IWL_FIRST_OFDM_RATE;
2672 start_rate = IWL_FIRST_OFDM_RATE;
2673 mask_bit <<= IWL_FIRST_OFDM_RATE;
2674 }
2675
2676 mask_bit = mask_bit & lq_sta->active_legacy_rate;
2677 lq_sta->last_txrate_idx = 4;
2678 for (i = start_rate; i < count; i++)
Mohamed Abbasd5e49032008-12-12 08:22:15 -08002679 if (mask_bit & BIT(i))
2680 lq_sta->last_txrate_idx = i;
2681
Johannes Berg4b7679a2008-09-18 18:14:18 +02002682 rs_initialize_lq(priv, conf, sta, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002683}
2684
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002685static void rs_fill_link_cmd(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08002686 struct iwl_lq_sta *lq_sta, u32 new_rate)
Zhu Yib481de92007-09-25 17:54:57 -07002687{
Tomas Winklere227cea2008-07-18 13:53:05 +08002688 struct iwl_scale_tbl_info tbl_type;
Zhu Yib481de92007-09-25 17:54:57 -07002689 int index = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002690 int rate_idx;
Zhu Yi1b696de2007-09-27 11:27:41 +08002691 int repeat_rate = 0;
Guy Cohenaade00c2008-04-23 17:14:59 -07002692 u8 ant_toggle_cnt = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002693 u8 use_ht_possible = 1;
Guy Cohenaade00c2008-04-23 17:14:59 -07002694 u8 valid_tx_ant = 0;
Guy Cohen07bc28e2008-04-23 17:15:03 -07002695 struct iwl_link_quality_cmd *lq_cmd = &lq_sta->lq;
Zhu Yib481de92007-09-25 17:54:57 -07002696
Ben Cahill77626352007-11-29 11:09:44 +08002697 /* Override starting rate (index 0) if needed for debug purposes */
Guy Cohen39e88502008-04-23 17:14:57 -07002698 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Zhu Yi98d7e092007-09-27 11:27:42 +08002699
Guy Cohen39e88502008-04-23 17:14:57 -07002700 /* Interpret new_rate (rate_n_flags) */
Guy Cohenaade00c2008-04-23 17:14:59 -07002701 memset(&tbl_type, 0, sizeof(tbl_type));
Guy Cohen39e88502008-04-23 17:14:57 -07002702 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band,
Zhu Yib481de92007-09-25 17:54:57 -07002703 &tbl_type, &rate_idx);
2704
Ben Cahill77626352007-11-29 11:09:44 +08002705 /* How many times should we repeat the initial rate? */
Zhu Yib481de92007-09-25 17:54:57 -07002706 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07002707 ant_toggle_cnt = 1;
Zhu Yi1b696de2007-09-27 11:27:41 +08002708 repeat_rate = IWL_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002709 } else {
Zhu Yi1b696de2007-09-27 11:27:41 +08002710 repeat_rate = IWL_HT_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002711 }
Zhu Yib481de92007-09-25 17:54:57 -07002712
2713 lq_cmd->general_params.mimo_delimiter =
2714 is_mimo(tbl_type.lq_type) ? 1 : 0;
Ben Cahill77626352007-11-29 11:09:44 +08002715
2716 /* Fill 1st table entry (index 0) */
Guy Cohen39e88502008-04-23 17:14:57 -07002717 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002718
Guy Cohen07bc28e2008-04-23 17:15:03 -07002719 if (num_of_ant(tbl_type.ant_type) == 1) {
2720 lq_cmd->general_params.single_stream_ant_msk =
2721 tbl_type.ant_type;
2722 } else if (num_of_ant(tbl_type.ant_type) == 2) {
2723 lq_cmd->general_params.dual_stream_ant_msk =
2724 tbl_type.ant_type;
2725 } /* otherwise we don't modify the existing value */
Zhu Yib481de92007-09-25 17:54:57 -07002726
2727 index++;
Zhu Yi1b696de2007-09-27 11:27:41 +08002728 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002729
Guy Cohenaade00c2008-04-23 17:14:59 -07002730 if (priv)
2731 valid_tx_ant = priv->hw_params.valid_tx_ant;
2732
Ben Cahill77626352007-11-29 11:09:44 +08002733 /* Fill rest of rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002734 while (index < LINK_QUAL_MAX_RETRY_NUM) {
Ben Cahill77626352007-11-29 11:09:44 +08002735 /* Repeat initial/next rate.
2736 * For legacy IWL_NUMBER_TRY == 1, this loop will not execute.
2737 * For HT IWL_HT_NUMBER_TRY == 3, this executes twice. */
Zhu Yi1b696de2007-09-27 11:27:41 +08002738 while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
Zhu Yib481de92007-09-25 17:54:57 -07002739 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07002740 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2741 ant_toggle_cnt++;
2742 else if (priv &&
2743 rs_toggle_antenna(valid_tx_ant,
2744 &new_rate, &tbl_type))
2745 ant_toggle_cnt = 1;
2746}
Zhu Yi98d7e092007-09-27 11:27:42 +08002747
Ben Cahill77626352007-11-29 11:09:44 +08002748 /* Override next rate if needed for debug purposes */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002749 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Ben Cahill77626352007-11-29 11:09:44 +08002750
2751 /* Fill next table entry */
Zhu Yib481de92007-09-25 17:54:57 -07002752 lq_cmd->rs_table[index].rate_n_flags =
Guy Cohen39e88502008-04-23 17:14:57 -07002753 cpu_to_le32(new_rate);
Zhu Yi1b696de2007-09-27 11:27:41 +08002754 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002755 index++;
2756 }
2757
Guy Cohen39e88502008-04-23 17:14:57 -07002758 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band, &tbl_type,
Zhu Yib481de92007-09-25 17:54:57 -07002759 &rate_idx);
2760
Ben Cahill77626352007-11-29 11:09:44 +08002761 /* Indicate to uCode which entries might be MIMO.
2762 * If initial rate was MIMO, this will finally end up
2763 * as (IWL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
Zhu Yib481de92007-09-25 17:54:57 -07002764 if (is_mimo(tbl_type.lq_type))
2765 lq_cmd->general_params.mimo_delimiter = index;
2766
Ben Cahill77626352007-11-29 11:09:44 +08002767 /* Get next rate */
Guy Cohen39e88502008-04-23 17:14:57 -07002768 new_rate = rs_get_lower_rate(lq_sta, &tbl_type, rate_idx,
2769 use_ht_possible);
Zhu Yib481de92007-09-25 17:54:57 -07002770
Ben Cahill77626352007-11-29 11:09:44 +08002771 /* How many times should we repeat the next rate? */
Zhu Yib481de92007-09-25 17:54:57 -07002772 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07002773 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2774 ant_toggle_cnt++;
2775 else if (priv &&
2776 rs_toggle_antenna(valid_tx_ant,
2777 &new_rate, &tbl_type))
2778 ant_toggle_cnt = 1;
2779
Zhu Yi1b696de2007-09-27 11:27:41 +08002780 repeat_rate = IWL_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002781 } else {
Zhu Yi1b696de2007-09-27 11:27:41 +08002782 repeat_rate = IWL_HT_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002783 }
Zhu Yib481de92007-09-25 17:54:57 -07002784
Ben Cahill77626352007-11-29 11:09:44 +08002785 /* Don't allow HT rates after next pass.
2786 * rs_get_lower_rate() will change type to LQ_A or LQ_G. */
Zhu Yib481de92007-09-25 17:54:57 -07002787 use_ht_possible = 0;
2788
Ben Cahill77626352007-11-29 11:09:44 +08002789 /* Override next rate if needed for debug purposes */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002790 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Ben Cahill77626352007-11-29 11:09:44 +08002791
2792 /* Fill next table entry */
Guy Cohen39e88502008-04-23 17:14:57 -07002793 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002794
2795 index++;
Zhu Yi1b696de2007-09-27 11:27:41 +08002796 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002797 }
2798
Wey-Yi Guy13c33a02009-06-03 11:44:11 -07002799 lq_cmd->agg_params.agg_frame_cnt_limit = LINK_QUAL_AGG_FRAME_LIMIT_MAX;
2800 lq_cmd->agg_params.agg_dis_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
2801 lq_cmd->agg_params.agg_time_limit =
2802 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
Zhu Yib481de92007-09-25 17:54:57 -07002803}
2804
Johannes Berg4b7679a2008-09-18 18:14:18 +02002805static void *rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
Zhu Yib481de92007-09-25 17:54:57 -07002806{
Johannes Berg4b7679a2008-09-18 18:14:18 +02002807 return hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07002808}
2809/* rate scale requires free function to be implemented */
2810static void rs_free(void *priv_rate)
2811{
2812 return;
2813}
2814
Johannes Berg4b7679a2008-09-18 18:14:18 +02002815static void rs_free_sta(void *priv_r, struct ieee80211_sta *sta,
2816 void *priv_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002817{
Tomas Winklere227cea2008-07-18 13:53:05 +08002818 struct iwl_lq_sta *lq_sta = priv_sta;
Rami Rosen45527c22008-10-07 09:50:01 +02002819 struct iwl_priv *priv __maybe_unused = priv_r;
Zhu Yib481de92007-09-25 17:54:57 -07002820
Tomas Winklere1623442009-01-27 14:27:56 -08002821 IWL_DEBUG_RATE(priv, "enter\n");
Tomas Winklerc33104f2008-01-14 17:46:21 -08002822 kfree(lq_sta);
Tomas Winklere1623442009-01-27 14:27:56 -08002823 IWL_DEBUG_RATE(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07002824}
2825
2826
Zhu Yi93dc6462007-09-27 11:27:37 +08002827#ifdef CONFIG_MAC80211_DEBUGFS
Zhu Yi5ae212c2007-09-27 11:27:38 +08002828static int open_file_generic(struct inode *inode, struct file *file)
2829{
2830 file->private_data = inode->i_private;
2831 return 0;
2832}
Tomas Winklere227cea2008-07-18 13:53:05 +08002833static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
2834 u32 *rate_n_flags, int index)
Zhu Yi98d7e092007-09-27 11:27:42 +08002835{
Ester Kummerbf403db2008-05-05 10:22:40 +08002836 struct iwl_priv *priv;
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07002837 u8 valid_tx_ant;
2838 u8 ant_sel_tx;
Ester Kummerbf403db2008-05-05 10:22:40 +08002839
2840 priv = lq_sta->drv;
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07002841 valid_tx_ant = priv->hw_params.valid_tx_ant;
Guy Cohen39e88502008-04-23 17:14:57 -07002842 if (lq_sta->dbg_fixed_rate) {
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07002843 ant_sel_tx =
2844 ((lq_sta->dbg_fixed_rate & RATE_MCS_ANT_ABC_MSK)
2845 >> RATE_MCS_ANT_POS);
2846 if ((valid_tx_ant & ant_sel_tx) == ant_sel_tx) {
Guy Cohen39e88502008-04-23 17:14:57 -07002847 *rate_n_flags = lq_sta->dbg_fixed_rate;
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07002848 IWL_DEBUG_RATE(priv, "Fixed rate ON\n");
Guy Cohen39e88502008-04-23 17:14:57 -07002849 } else {
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07002850 lq_sta->dbg_fixed_rate = 0;
2851 IWL_ERR(priv,
2852 "Invalid antenna selection 0x%X, Valid is 0x%X\n",
2853 ant_sel_tx, valid_tx_ant);
2854 IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
Guy Cohen39e88502008-04-23 17:14:57 -07002855 }
Guy Cohen39e88502008-04-23 17:14:57 -07002856 } else {
Tomas Winklere1623442009-01-27 14:27:56 -08002857 IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
Zhu Yi98d7e092007-09-27 11:27:42 +08002858 }
Zhu Yi98d7e092007-09-27 11:27:42 +08002859}
2860
2861static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
2862 const char __user *user_buf, size_t count, loff_t *ppos)
2863{
Tomas Winklere227cea2008-07-18 13:53:05 +08002864 struct iwl_lq_sta *lq_sta = file->private_data;
Ester Kummerbf403db2008-05-05 10:22:40 +08002865 struct iwl_priv *priv;
Zhu Yi98d7e092007-09-27 11:27:42 +08002866 char buf[64];
2867 int buf_size;
2868 u32 parsed_rate;
2869
Ester Kummerbf403db2008-05-05 10:22:40 +08002870 priv = lq_sta->drv;
Zhu Yi98d7e092007-09-27 11:27:42 +08002871 memset(buf, 0, sizeof(buf));
2872 buf_size = min(count, sizeof(buf) - 1);
2873 if (copy_from_user(buf, user_buf, buf_size))
2874 return -EFAULT;
2875
2876 if (sscanf(buf, "%x", &parsed_rate) == 1)
Guy Cohen39e88502008-04-23 17:14:57 -07002877 lq_sta->dbg_fixed_rate = parsed_rate;
Zhu Yi98d7e092007-09-27 11:27:42 +08002878 else
Guy Cohen39e88502008-04-23 17:14:57 -07002879 lq_sta->dbg_fixed_rate = 0;
Zhu Yi98d7e092007-09-27 11:27:42 +08002880
Guy Cohen39e88502008-04-23 17:14:57 -07002881 lq_sta->active_legacy_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
2882 lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2883 lq_sta->active_mimo2_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2884 lq_sta->active_mimo3_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
Zhu Yi98d7e092007-09-27 11:27:42 +08002885
Tomas Winklere1623442009-01-27 14:27:56 -08002886 IWL_DEBUG_RATE(priv, "sta_id %d rate 0x%X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07002887 lq_sta->lq.sta_id, lq_sta->dbg_fixed_rate);
Zhu Yi98d7e092007-09-27 11:27:42 +08002888
Guy Cohen39e88502008-04-23 17:14:57 -07002889 if (lq_sta->dbg_fixed_rate) {
Guy Cohen07bc28e2008-04-23 17:15:03 -07002890 rs_fill_link_cmd(NULL, lq_sta, lq_sta->dbg_fixed_rate);
Tomas Winkler66c73db2008-04-15 16:01:40 -07002891 iwl_send_lq_cmd(lq_sta->drv, &lq_sta->lq, CMD_ASYNC);
Zhu Yi98d7e092007-09-27 11:27:42 +08002892 }
2893
2894 return count;
2895}
Zhu Yi0209dc12007-09-27 11:27:43 +08002896
Zhu Yi5ae212c2007-09-27 11:27:38 +08002897static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
2898 char __user *user_buf, size_t count, loff_t *ppos)
2899{
Frank Seidela412c802009-03-01 20:25:38 +01002900 char *buff;
Zhu Yi5ae212c2007-09-27 11:27:38 +08002901 int desc = 0;
2902 int i = 0;
Wey-Yi Guy12b96812009-04-08 11:39:27 -07002903 int index = 0;
Frank Seidela412c802009-03-01 20:25:38 +01002904 ssize_t ret;
Zhu Yi5ae212c2007-09-27 11:27:38 +08002905
Tomas Winklere227cea2008-07-18 13:53:05 +08002906 struct iwl_lq_sta *lq_sta = file->private_data;
Wey-Yi Guyd8ae4f52009-03-10 14:35:07 -07002907 struct iwl_priv *priv;
Wey-Yi Guyadb7b5e2009-03-10 14:35:08 -07002908 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002909
Wey-Yi Guyd8ae4f52009-03-10 14:35:07 -07002910 priv = lq_sta->drv;
Frank Seidela412c802009-03-01 20:25:38 +01002911 buff = kmalloc(1024, GFP_KERNEL);
2912 if (!buff)
2913 return -ENOMEM;
2914
Tomas Winklerc33104f2008-01-14 17:46:21 -08002915 desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
Zhu Yi98d7e092007-09-27 11:27:42 +08002916 desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002917 lq_sta->total_failed, lq_sta->total_success,
Guy Cohen39e88502008-04-23 17:14:57 -07002918 lq_sta->active_legacy_rate);
Zhu Yi98d7e092007-09-27 11:27:42 +08002919 desc += sprintf(buff+desc, "fixed rate 0x%X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07002920 lq_sta->dbg_fixed_rate);
Wey-Yi Guyd8ae4f52009-03-10 14:35:07 -07002921 desc += sprintf(buff+desc, "valid_tx_ant %s%s%s\n",
2922 (priv->hw_params.valid_tx_ant & ANT_A) ? "ANT_A," : "",
2923 (priv->hw_params.valid_tx_ant & ANT_B) ? "ANT_B," : "",
2924 (priv->hw_params.valid_tx_ant & ANT_C) ? "ANT_C" : "");
Wey-Yi Guyadb7b5e2009-03-10 14:35:08 -07002925 desc += sprintf(buff+desc, "lq type %s\n",
2926 (is_legacy(tbl->lq_type)) ? "legacy" : "HT");
2927 if (is_Ht(tbl->lq_type)) {
2928 desc += sprintf(buff+desc, " %s",
2929 (is_siso(tbl->lq_type)) ? "SISO" :
2930 ((is_mimo2(tbl->lq_type)) ? "MIMO2" : "MIMO3"));
2931 desc += sprintf(buff+desc, " %s",
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07002932 (tbl->is_ht40) ? "40MHz" : "20MHz");
Wey-Yi Guy2681b202009-05-21 13:44:22 -07002933 desc += sprintf(buff+desc, " %s %s\n", (tbl->is_SGI) ? "SGI" : "",
2934 (lq_sta->is_green) ? "GF enabled" : "");
Wey-Yi Guyadb7b5e2009-03-10 14:35:08 -07002935 }
Wey-Yi Guy12b96812009-04-08 11:39:27 -07002936 desc += sprintf(buff+desc, "last tx rate=0x%X\n",
2937 lq_sta->last_rate_n_flags);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002938 desc += sprintf(buff+desc, "general:"
2939 "flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002940 lq_sta->lq.general_params.flags,
2941 lq_sta->lq.general_params.mimo_delimiter,
2942 lq_sta->lq.general_params.single_stream_ant_msk,
2943 lq_sta->lq.general_params.dual_stream_ant_msk);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002944
2945 desc += sprintf(buff+desc, "agg:"
2946 "time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002947 le16_to_cpu(lq_sta->lq.agg_params.agg_time_limit),
2948 lq_sta->lq.agg_params.agg_dis_start_th,
2949 lq_sta->lq.agg_params.agg_frame_cnt_limit);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002950
2951 desc += sprintf(buff+desc,
2952 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002953 lq_sta->lq.general_params.start_rate_index[0],
2954 lq_sta->lq.general_params.start_rate_index[1],
2955 lq_sta->lq.general_params.start_rate_index[2],
2956 lq_sta->lq.general_params.start_rate_index[3]);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002957
Wey-Yi Guy12b96812009-04-08 11:39:27 -07002958 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
2959 index = iwl_hwrate_to_plcp_idx(
2960 le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags));
2961 if (is_legacy(tbl->lq_type)) {
2962 desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps\n",
2963 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags),
2964 iwl_rate_mcs[index].mbps);
2965 } else {
2966 desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps (%s)\n",
2967 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags),
2968 iwl_rate_mcs[index].mbps, iwl_rate_mcs[index].mcs);
2969 }
2970 }
Zhu Yi5ae212c2007-09-27 11:27:38 +08002971
Frank Seidela412c802009-03-01 20:25:38 +01002972 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2973 kfree(buff);
2974 return ret;
Zhu Yi5ae212c2007-09-27 11:27:38 +08002975}
2976
2977static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
Zhu Yi98d7e092007-09-27 11:27:42 +08002978 .write = rs_sta_dbgfs_scale_table_write,
Zhu Yi5ae212c2007-09-27 11:27:38 +08002979 .read = rs_sta_dbgfs_scale_table_read,
2980 .open = open_file_generic,
2981};
Zhu Yi0209dc12007-09-27 11:27:43 +08002982static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
2983 char __user *user_buf, size_t count, loff_t *ppos)
2984{
Frank Seidela412c802009-03-01 20:25:38 +01002985 char *buff;
Zhu Yi0209dc12007-09-27 11:27:43 +08002986 int desc = 0;
2987 int i, j;
Frank Seidela412c802009-03-01 20:25:38 +01002988 ssize_t ret;
Zhu Yi0209dc12007-09-27 11:27:43 +08002989
Tomas Winklere227cea2008-07-18 13:53:05 +08002990 struct iwl_lq_sta *lq_sta = file->private_data;
Frank Seidela412c802009-03-01 20:25:38 +01002991
2992 buff = kmalloc(1024, GFP_KERNEL);
2993 if (!buff)
2994 return -ENOMEM;
2995
Zhu Yi0209dc12007-09-27 11:27:43 +08002996 for (i = 0; i < LQ_SIZE; i++) {
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07002997 desc += sprintf(buff+desc,
2998 "%s type=%d SGI=%d HT40=%d DUP=%d GF=%d\n"
Zhu Yi0209dc12007-09-27 11:27:43 +08002999 "rate=0x%X\n",
Abhijeet Kolekarc3056062008-11-12 13:14:08 -08003000 lq_sta->active_tbl == i ? "*" : "x",
Tomas Winklerc33104f2008-01-14 17:46:21 -08003001 lq_sta->lq_info[i].lq_type,
3002 lq_sta->lq_info[i].is_SGI,
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07003003 lq_sta->lq_info[i].is_ht40,
Tomas Winklerc33104f2008-01-14 17:46:21 -08003004 lq_sta->lq_info[i].is_dup,
Wey-Yi Guy2681b202009-05-21 13:44:22 -07003005 lq_sta->is_green,
Guy Cohen39e88502008-04-23 17:14:57 -07003006 lq_sta->lq_info[i].current_rate);
Zhu Yi0209dc12007-09-27 11:27:43 +08003007 for (j = 0; j < IWL_RATE_COUNT; j++) {
3008 desc += sprintf(buff+desc,
Tomas Winklerc33104f2008-01-14 17:46:21 -08003009 "counter=%d success=%d %%=%d\n",
3010 lq_sta->lq_info[i].win[j].counter,
3011 lq_sta->lq_info[i].win[j].success_counter,
3012 lq_sta->lq_info[i].win[j].success_ratio);
Zhu Yi0209dc12007-09-27 11:27:43 +08003013 }
3014 }
Frank Seidela412c802009-03-01 20:25:38 +01003015 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
3016 kfree(buff);
3017 return ret;
Zhu Yi0209dc12007-09-27 11:27:43 +08003018}
3019
3020static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
3021 .read = rs_sta_dbgfs_stats_table_read,
3022 .open = open_file_generic,
3023};
Zhu Yi5ae212c2007-09-27 11:27:38 +08003024
Wey-Yi Guy38167452009-05-08 13:44:43 -07003025static ssize_t rs_sta_dbgfs_rate_scale_data_read(struct file *file,
3026 char __user *user_buf, size_t count, loff_t *ppos)
3027{
3028 char buff[120];
3029 int desc = 0;
3030 ssize_t ret;
3031
3032 struct iwl_lq_sta *lq_sta = file->private_data;
3033 struct iwl_priv *priv;
3034 struct iwl_scale_tbl_info *tbl = &lq_sta->lq_info[lq_sta->active_tbl];
3035
3036 priv = lq_sta->drv;
3037
3038 if (is_Ht(tbl->lq_type))
3039 desc += sprintf(buff+desc,
3040 "Bit Rate= %d Mb/s\n",
3041 tbl->expected_tpt[lq_sta->last_txrate_idx]);
3042 else
3043 desc += sprintf(buff+desc,
3044 "Bit Rate= %d Mb/s\n",
3045 iwl_rates[lq_sta->last_txrate_idx].ieee >> 1);
3046 desc += sprintf(buff+desc,
3047 "Signal Level= %d dBm\tNoise Level= %d dBm\n",
3048 priv->last_rx_rssi, priv->last_rx_noise);
3049 desc += sprintf(buff+desc,
3050 "Tsf= 0x%llx\tBeacon time= 0x%08X\n",
3051 priv->last_tsf, priv->last_beacon_time);
3052
3053 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
3054 return ret;
3055}
3056
3057static const struct file_operations rs_sta_dbgfs_rate_scale_data_ops = {
3058 .read = rs_sta_dbgfs_rate_scale_data_read,
3059 .open = open_file_generic,
3060};
3061
Zhu Yi93dc6462007-09-27 11:27:37 +08003062static void rs_add_debugfs(void *priv, void *priv_sta,
3063 struct dentry *dir)
3064{
Tomas Winklere227cea2008-07-18 13:53:05 +08003065 struct iwl_lq_sta *lq_sta = priv_sta;
Tomas Winklerc33104f2008-01-14 17:46:21 -08003066 lq_sta->rs_sta_dbgfs_scale_table_file =
Zhu Yi0209dc12007-09-27 11:27:43 +08003067 debugfs_create_file("rate_scale_table", 0600, dir,
Tomas Winklerc33104f2008-01-14 17:46:21 -08003068 lq_sta, &rs_sta_dbgfs_scale_table_ops);
3069 lq_sta->rs_sta_dbgfs_stats_table_file =
Zhu Yi0209dc12007-09-27 11:27:43 +08003070 debugfs_create_file("rate_stats_table", 0600, dir,
Tomas Winklerc33104f2008-01-14 17:46:21 -08003071 lq_sta, &rs_sta_dbgfs_stats_table_ops);
Wey-Yi Guy38167452009-05-08 13:44:43 -07003072 lq_sta->rs_sta_dbgfs_rate_scale_data_file =
3073 debugfs_create_file("rate_scale_data", 0600, dir,
3074 lq_sta, &rs_sta_dbgfs_rate_scale_data_ops);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02003075 lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file =
3076 debugfs_create_u8("tx_agg_tid_enable", 0600, dir,
3077 &lq_sta->tx_agg_tid_en);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02003078
Zhu Yi93dc6462007-09-27 11:27:37 +08003079}
3080
3081static void rs_remove_debugfs(void *priv, void *priv_sta)
3082{
Tomas Winklere227cea2008-07-18 13:53:05 +08003083 struct iwl_lq_sta *lq_sta = priv_sta;
Tomas Winklerc33104f2008-01-14 17:46:21 -08003084 debugfs_remove(lq_sta->rs_sta_dbgfs_scale_table_file);
3085 debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
Wey-Yi Guy38167452009-05-08 13:44:43 -07003086 debugfs_remove(lq_sta->rs_sta_dbgfs_rate_scale_data_file);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02003087 debugfs_remove(lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file);
Zhu Yi93dc6462007-09-27 11:27:37 +08003088}
3089#endif
3090
Zhu Yib481de92007-09-25 17:54:57 -07003091static struct rate_control_ops rs_ops = {
3092 .module = NULL,
3093 .name = RS_NAME,
3094 .tx_status = rs_tx_status,
3095 .get_rate = rs_get_rate,
3096 .rate_init = rs_rate_init,
Zhu Yib481de92007-09-25 17:54:57 -07003097 .alloc = rs_alloc,
3098 .free = rs_free,
3099 .alloc_sta = rs_alloc_sta,
3100 .free_sta = rs_free_sta,
Zhu Yi93dc6462007-09-27 11:27:37 +08003101#ifdef CONFIG_MAC80211_DEBUGFS
3102 .add_sta_debugfs = rs_add_debugfs,
3103 .remove_sta_debugfs = rs_remove_debugfs,
3104#endif
Zhu Yib481de92007-09-25 17:54:57 -07003105};
3106
Tomas Winklere227cea2008-07-18 13:53:05 +08003107int iwlagn_rate_control_register(void)
Zhu Yib481de92007-09-25 17:54:57 -07003108{
Reinette Chatre897e1cf2008-03-28 16:21:09 -07003109 return ieee80211_rate_control_register(&rs_ops);
Zhu Yib481de92007-09-25 17:54:57 -07003110}
3111
Tomas Winklere227cea2008-07-18 13:53:05 +08003112void iwlagn_rate_control_unregister(void)
Zhu Yib481de92007-09-25 17:54:57 -07003113{
3114 ieee80211_rate_control_unregister(&rs_ops);
3115}
3116