blob: 7cdbcfe483f3244892a0cee0d962700fff6b43c3 [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 */
55#define IWL_RATE_SCALE_FLUSH_INTVL (2*HZ)
Zhu Yib481de92007-09-25 17:54:57 -070056
57static u8 rs_ht_to_legacy[] = {
58 IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
59 IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
60 IWL_RATE_6M_INDEX,
61 IWL_RATE_6M_INDEX, IWL_RATE_9M_INDEX,
62 IWL_RATE_12M_INDEX, IWL_RATE_18M_INDEX,
63 IWL_RATE_24M_INDEX, IWL_RATE_36M_INDEX,
64 IWL_RATE_48M_INDEX, IWL_RATE_54M_INDEX
65};
66
Guy Cohenaade00c2008-04-23 17:14:59 -070067static const u8 ant_toggle_lookup[] = {
68 /*ANT_NONE -> */ ANT_NONE,
69 /*ANT_A -> */ ANT_B,
70 /*ANT_B -> */ ANT_C,
71 /*ANT_AB -> */ ANT_BC,
72 /*ANT_C -> */ ANT_A,
73 /*ANT_AC -> */ ANT_AB,
74 /*ANT_BC -> */ ANT_AC,
75 /*ANT_ABC -> */ ANT_ABC,
76};
77
Ben Cahill77626352007-11-29 11:09:44 +080078/**
Tomas Winklere227cea2008-07-18 13:53:05 +080079 * struct iwl_rate_scale_data -- tx success history for one rate
Ben Cahill77626352007-11-29 11:09:44 +080080 */
Tomas Winklere227cea2008-07-18 13:53:05 +080081struct iwl_rate_scale_data {
Ben Cahill77626352007-11-29 11:09:44 +080082 u64 data; /* bitmap of successful frames */
83 s32 success_counter; /* number of frames successful */
84 s32 success_ratio; /* per-cent * 128 */
85 s32 counter; /* number of frames attempted */
86 s32 average_tpt; /* success ratio * expected throughput */
Zhu Yib481de92007-09-25 17:54:57 -070087 unsigned long stamp;
88};
89
Ben Cahill77626352007-11-29 11:09:44 +080090/**
Tomas Winklere227cea2008-07-18 13:53:05 +080091 * struct iwl_scale_tbl_info -- tx params and success history for all rates
Ben Cahill77626352007-11-29 11:09:44 +080092 *
Tomas Winklere227cea2008-07-18 13:53:05 +080093 * There are two of these in struct iwl_lq_sta,
Ben Cahill77626352007-11-29 11:09:44 +080094 * one for "active", and one for "search".
95 */
Tomas Winklere227cea2008-07-18 13:53:05 +080096struct iwl_scale_tbl_info {
Guy Cohenfde0db32008-04-21 15:42:01 -070097 enum iwl_table_type lq_type;
98 u8 ant_type;
Ben Cahill77626352007-11-29 11:09:44 +080099 u8 is_SGI; /* 1 = short guard interval */
100 u8 is_fat; /* 1 = 40 MHz channel width */
101 u8 is_dup; /* 1 = duplicated data streams */
102 u8 action; /* change modulation; IWL_[LEGACY/SISO/MIMO]_SWITCH_* */
103 s32 *expected_tpt; /* throughput metrics; expected_tpt_G, etc. */
Guy Cohen39e88502008-04-23 17:14:57 -0700104 u32 current_rate; /* rate_n_flags, uCode API format */
Tomas Winklere227cea2008-07-18 13:53:05 +0800105 struct iwl_rate_scale_data win[IWL_RATE_COUNT]; /* rate histories */
Zhu Yib481de92007-09-25 17:54:57 -0700106};
107
Tomas Winklere227cea2008-07-18 13:53:05 +0800108struct iwl_traffic_load {
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200109 unsigned long time_stamp; /* age of the oldest statistics */
110 u32 packet_count[TID_QUEUE_MAX_SIZE]; /* packet count in this time
111 * slice */
112 u32 total; /* total num of packets during the
113 * last TID_MAX_TIME_DIFF */
114 u8 queue_count; /* number of queues that has
115 * been used since the last cleanup */
116 u8 head; /* start of the circular buffer */
117};
118
Ben Cahill77626352007-11-29 11:09:44 +0800119/**
Tomas Winklere227cea2008-07-18 13:53:05 +0800120 * struct iwl_lq_sta -- driver's rate scaling private structure
Ben Cahill77626352007-11-29 11:09:44 +0800121 *
122 * Pointer to this gets passed back and forth between driver and mac80211.
123 */
Tomas Winklere227cea2008-07-18 13:53:05 +0800124struct iwl_lq_sta {
Ben Cahill77626352007-11-29 11:09:44 +0800125 u8 active_tbl; /* index of active table, range 0-1 */
126 u8 enable_counter; /* indicates HT mode */
127 u8 stay_in_tbl; /* 1: disallow, 0: allow search for new mode */
128 u8 search_better_tbl; /* 1: currently trying alternate mode */
Zhu Yib481de92007-09-25 17:54:57 -0700129 s32 last_tpt;
Ben Cahill77626352007-11-29 11:09:44 +0800130
131 /* The following determine when to search for a new mode */
Zhu Yib481de92007-09-25 17:54:57 -0700132 u32 table_count_limit;
Ben Cahill77626352007-11-29 11:09:44 +0800133 u32 max_failure_limit; /* # failed frames before new search */
134 u32 max_success_limit; /* # successful frames before new search */
Zhu Yib481de92007-09-25 17:54:57 -0700135 u32 table_count;
Ben Cahill77626352007-11-29 11:09:44 +0800136 u32 total_failed; /* total failed frames, any/all rates */
137 u32 total_success; /* total successful frames, any/all rates */
138 u32 flush_timer; /* time staying in mode before new search */
139
140 u8 action_counter; /* # mode-switch actions tried */
Zhu Yib481de92007-09-25 17:54:57 -0700141 u8 is_green;
142 u8 is_dup;
Johannes Berg8318d782008-01-24 19:38:38 +0100143 enum ieee80211_band band;
Zhu Yib481de92007-09-25 17:54:57 -0700144 u8 ibss_sta_added;
Ben Cahill77626352007-11-29 11:09:44 +0800145
146 /* The following are bitmaps of rates; IWL_RATE_6M_MASK, etc. */
Zhu Yi02dede02007-09-27 11:27:40 +0800147 u32 supp_rates;
Guy Cohen39e88502008-04-23 17:14:57 -0700148 u16 active_legacy_rate;
Zhu Yib481de92007-09-25 17:54:57 -0700149 u16 active_siso_rate;
Guy Cohenfde0db32008-04-21 15:42:01 -0700150 u16 active_mimo2_rate;
151 u16 active_mimo3_rate;
Zhu Yib481de92007-09-25 17:54:57 -0700152 u16 active_rate_basic;
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -0800153 s8 max_rate_idx; /* Max rate set by user */
Abbas, Mohamed7d049e52009-01-20 21:33:53 -0800154 u8 missed_rate_counter;
Ben Cahill77626352007-11-29 11:09:44 +0800155
Tomas Winkler66c73db2008-04-15 16:01:40 -0700156 struct iwl_link_quality_cmd lq;
Tomas Winklere227cea2008-07-18 13:53:05 +0800157 struct iwl_scale_tbl_info lq_info[LQ_SIZE]; /* "active", "search" */
158 struct iwl_traffic_load load[TID_MAX_LOAD_COUNT];
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200159 u8 tx_agg_tid_en;
Zhu Yi5ae212c2007-09-27 11:27:38 +0800160#ifdef CONFIG_MAC80211_DEBUGFS
Zhu Yi98d7e092007-09-27 11:27:42 +0800161 struct dentry *rs_sta_dbgfs_scale_table_file;
Zhu Yi0209dc12007-09-27 11:27:43 +0800162 struct dentry *rs_sta_dbgfs_stats_table_file;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200163 struct dentry *rs_sta_dbgfs_tx_agg_tid_en_file;
Guy Cohen39e88502008-04-23 17:14:57 -0700164 u32 dbg_fixed_rate;
Zhu Yi5ae212c2007-09-27 11:27:38 +0800165#endif
Zhu Yi62430652008-05-05 10:22:46 +0800166 struct iwl_priv *drv;
Johannes Bergb7e35002008-09-11 02:22:58 +0200167
168 /* used to be in sta_info */
169 int last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -0700170};
171
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700172static void rs_rate_scale_perform(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700173 struct ieee80211_hdr *hdr,
Johannes Berg4b7679a2008-09-18 18:14:18 +0200174 struct ieee80211_sta *sta,
175 struct iwl_lq_sta *lq_sta);
Guy Cohenfde0db32008-04-21 15:42:01 -0700176static void rs_fill_link_cmd(const struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +0800177 struct iwl_lq_sta *lq_sta, u32 rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -0700178
179
Zhu Yi98d7e092007-09-27 11:27:42 +0800180#ifdef CONFIG_MAC80211_DEBUGFS
Tomas Winklere227cea2008-07-18 13:53:05 +0800181static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
182 u32 *rate_n_flags, int index);
Zhu Yi98d7e092007-09-27 11:27:42 +0800183#else
Tomas Winklere227cea2008-07-18 13:53:05 +0800184static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
185 u32 *rate_n_flags, int index)
Zhu Yi98d7e092007-09-27 11:27:42 +0800186{}
187#endif
Ben Cahill77626352007-11-29 11:09:44 +0800188
189/*
190 * Expected throughput metrics for following rates:
191 * 1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
192 * "G" is the only table that supports CCK (the first 4 rates).
193 */
Wey-Yi Guy584a0f02009-04-01 14:33:24 -0700194
Zhu Yib481de92007-09-25 17:54:57 -0700195static s32 expected_tpt_A[IWL_RATE_COUNT] = {
196 0, 0, 0, 0, 40, 57, 72, 98, 121, 154, 177, 186, 186
197};
198
199static s32 expected_tpt_G[IWL_RATE_COUNT] = {
200 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 186
201};
202
203static s32 expected_tpt_siso20MHz[IWL_RATE_COUNT] = {
204 0, 0, 0, 0, 42, 42, 76, 102, 124, 159, 183, 193, 202
205};
206
207static s32 expected_tpt_siso20MHzSGI[IWL_RATE_COUNT] = {
208 0, 0, 0, 0, 46, 46, 82, 110, 132, 168, 192, 202, 211
209};
210
Wey-Yi Guy584a0f02009-04-01 14:33:24 -0700211static s32 expected_tpt_mimo2_20MHz[IWL_RATE_COUNT] = {
Zhu Yib481de92007-09-25 17:54:57 -0700212 0, 0, 0, 0, 74, 74, 123, 155, 179, 214, 236, 244, 251
213};
214
Wey-Yi Guy584a0f02009-04-01 14:33:24 -0700215static s32 expected_tpt_mimo2_20MHzSGI[IWL_RATE_COUNT] = {
Zhu Yib481de92007-09-25 17:54:57 -0700216 0, 0, 0, 0, 81, 81, 131, 164, 188, 222, 243, 251, 257
217};
218
219static s32 expected_tpt_siso40MHz[IWL_RATE_COUNT] = {
220 0, 0, 0, 0, 77, 77, 127, 160, 184, 220, 242, 250, 257
221};
222
223static s32 expected_tpt_siso40MHzSGI[IWL_RATE_COUNT] = {
224 0, 0, 0, 0, 83, 83, 135, 169, 193, 229, 250, 257, 264
225};
226
Wey-Yi Guy584a0f02009-04-01 14:33:24 -0700227static s32 expected_tpt_mimo2_40MHz[IWL_RATE_COUNT] = {
Zhu Yib481de92007-09-25 17:54:57 -0700228 0, 0, 0, 0, 123, 123, 182, 214, 235, 264, 279, 285, 289
229};
230
Wey-Yi Guy584a0f02009-04-01 14:33:24 -0700231static s32 expected_tpt_mimo2_40MHzSGI[IWL_RATE_COUNT] = {
Zhu Yib481de92007-09-25 17:54:57 -0700232 0, 0, 0, 0, 131, 131, 191, 222, 242, 270, 284, 289, 293
233};
234
Wey-Yi Guy584a0f02009-04-01 14:33:24 -0700235/* Expected throughput metric MIMO3 */
236static s32 expected_tpt_mimo3_20MHz[IWL_RATE_COUNT] = {
237 0, 0, 0, 0, 99, 99, 153, 186, 208, 239, 256, 263, 268
238};
239
240static s32 expected_tpt_mimo3_20MHzSGI[IWL_RATE_COUNT] = {
241 0, 0, 0, 0, 106, 106, 162, 194, 215, 246, 262, 268, 273
242};
243
244static s32 expected_tpt_mimo3_40MHz[IWL_RATE_COUNT] = {
245 0, 0, 0, 0, 152, 152, 211, 239, 255, 279, 290, 294, 297
246};
247
248static s32 expected_tpt_mimo3_40MHzSGI[IWL_RATE_COUNT] = {
249 0, 0, 0, 0, 160, 160, 219, 245, 261, 284, 294, 297, 300
250};
251
Guy Cohen39e88502008-04-23 17:14:57 -0700252static inline u8 rs_extract_rate(u32 rate_n_flags)
Zhu Yib481de92007-09-25 17:54:57 -0700253{
254 return (u8)(rate_n_flags & 0xFF);
255}
256
Tomas Winklere227cea2008-07-18 13:53:05 +0800257static void rs_rate_scale_clear_window(struct iwl_rate_scale_data *window)
Zhu Yib481de92007-09-25 17:54:57 -0700258{
259 window->data = 0;
260 window->success_counter = 0;
261 window->success_ratio = IWL_INVALID_VALUE;
262 window->counter = 0;
263 window->average_tpt = IWL_INVALID_VALUE;
264 window->stamp = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700265}
266
Guy Cohenaade00c2008-04-23 17:14:59 -0700267static inline u8 rs_is_valid_ant(u8 valid_antenna, u8 ant_type)
268{
Tomas Winkler3ac7f142008-07-21 02:40:14 +0300269 return (ant_type & valid_antenna) == ant_type;
Guy Cohenaade00c2008-04-23 17:14:59 -0700270}
271
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200272/*
273 * removes the old data from the statistics. All data that is older than
274 * TID_MAX_TIME_DIFF, will be deleted.
275 */
Tomas Winklere227cea2008-07-18 13:53:05 +0800276static void rs_tl_rm_old_stats(struct iwl_traffic_load *tl, u32 curr_time)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200277{
278 /* The oldest age we want to keep */
279 u32 oldest_time = curr_time - TID_MAX_TIME_DIFF;
280
281 while (tl->queue_count &&
282 (tl->time_stamp < oldest_time)) {
283 tl->total -= tl->packet_count[tl->head];
284 tl->packet_count[tl->head] = 0;
285 tl->time_stamp += TID_QUEUE_CELL_SPACING;
286 tl->queue_count--;
287 tl->head++;
288 if (tl->head >= TID_QUEUE_MAX_SIZE)
289 tl->head = 0;
290 }
291}
292
293/*
294 * increment traffic load value for tid and also remove
295 * any old values if passed the certain time period
296 */
Tomas Winklere227cea2008-07-18 13:53:05 +0800297static u8 rs_tl_add_packet(struct iwl_lq_sta *lq_data,
Ron Rindjunskyfaa29712008-06-12 09:46:58 +0800298 struct ieee80211_hdr *hdr)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200299{
300 u32 curr_time = jiffies_to_msecs(jiffies);
301 u32 time_diff;
302 s32 index;
Tomas Winklere227cea2008-07-18 13:53:05 +0800303 struct iwl_traffic_load *tl = NULL;
Tomas Winkler54dbb522008-05-15 13:54:06 +0800304 u8 tid;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200305
Tomas Winkler417f1142008-11-12 13:14:06 -0800306 if (ieee80211_is_data_qos(hdr->frame_control)) {
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700307 u8 *qc = ieee80211_get_qos_ctl(hdr);
Tomas Winkler54dbb522008-05-15 13:54:06 +0800308 tid = qc[0] & 0xf;
309 } else
Ron Rindjunskyfaa29712008-06-12 09:46:58 +0800310 return MAX_TID_COUNT;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200311
312 tl = &lq_data->load[tid];
313
314 curr_time -= curr_time % TID_ROUND_VALUE;
315
316 /* Happens only for the first packet. Initialize the data */
317 if (!(tl->queue_count)) {
318 tl->total = 1;
319 tl->time_stamp = curr_time;
320 tl->queue_count = 1;
321 tl->head = 0;
322 tl->packet_count[0] = 1;
Ron Rindjunskyfaa29712008-06-12 09:46:58 +0800323 return MAX_TID_COUNT;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200324 }
325
326 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
327 index = time_diff / TID_QUEUE_CELL_SPACING;
328
329 /* The history is too long: remove data that is older than */
330 /* TID_MAX_TIME_DIFF */
331 if (index >= TID_QUEUE_MAX_SIZE)
332 rs_tl_rm_old_stats(tl, curr_time);
333
334 index = (tl->head + index) % TID_QUEUE_MAX_SIZE;
335 tl->packet_count[index] = tl->packet_count[index] + 1;
336 tl->total = tl->total + 1;
337
338 if ((index + 1) > tl->queue_count)
339 tl->queue_count = index + 1;
Ron Rindjunskyfaa29712008-06-12 09:46:58 +0800340
341 return tid;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200342}
343
344/*
345 get the traffic load value for tid
346*/
Tomas Winklere227cea2008-07-18 13:53:05 +0800347static u32 rs_tl_get_load(struct iwl_lq_sta *lq_data, u8 tid)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200348{
349 u32 curr_time = jiffies_to_msecs(jiffies);
350 u32 time_diff;
351 s32 index;
Tomas Winklere227cea2008-07-18 13:53:05 +0800352 struct iwl_traffic_load *tl = NULL;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200353
354 if (tid >= TID_MAX_LOAD_COUNT)
355 return 0;
356
357 tl = &(lq_data->load[tid]);
358
359 curr_time -= curr_time % TID_ROUND_VALUE;
360
361 if (!(tl->queue_count))
362 return 0;
363
364 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
365 index = time_diff / TID_QUEUE_CELL_SPACING;
366
367 /* The history is too long: remove data that is older than */
368 /* TID_MAX_TIME_DIFF */
369 if (index >= TID_QUEUE_MAX_SIZE)
370 rs_tl_rm_old_stats(tl, curr_time);
371
372 return tl->total;
373}
374
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700375static void rs_tl_turn_on_agg_for_tid(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +0800376 struct iwl_lq_sta *lq_data, u8 tid,
Johannes Berg4b7679a2008-09-18 18:14:18 +0200377 struct ieee80211_sta *sta)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200378{
Johannes Bergff550cb2008-09-11 03:17:05 +0200379 if (rs_tl_get_load(lq_data, tid) > IWL_AGG_LOAD_THRESHOLD) {
Tomas Winklere1623442009-01-27 14:27:56 -0800380 IWL_DEBUG_HT(priv, "Starting Tx agg: STA: %pM tid: %d\n",
Johannes Berge1749612008-10-27 15:59:26 -0700381 sta->addr, tid);
Johannes Berg4b7679a2008-09-18 18:14:18 +0200382 ieee80211_start_tx_ba_session(priv->hw, sta->addr, tid);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200383 }
384}
385
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700386static void rs_tl_turn_on_agg(struct iwl_priv *priv, u8 tid,
Tomas Winklere227cea2008-07-18 13:53:05 +0800387 struct iwl_lq_sta *lq_data,
Johannes Berg4b7679a2008-09-18 18:14:18 +0200388 struct ieee80211_sta *sta)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200389{
390 if ((tid < TID_MAX_LOAD_COUNT))
391 rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
392 else if (tid == IWL_AGG_ALL_TID)
393 for (tid = 0; tid < TID_MAX_LOAD_COUNT; tid++)
394 rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
395}
396
Guy Cohen39e88502008-04-23 17:14:57 -0700397static inline int get_num_of_ant_from_rate(u32 rate_n_flags)
Guy Cohenfde0db32008-04-21 15:42:01 -0700398{
Tomas Winkler3ac7f142008-07-21 02:40:14 +0300399 return !!(rate_n_flags & RATE_MCS_ANT_A_MSK) +
400 !!(rate_n_flags & RATE_MCS_ANT_B_MSK) +
401 !!(rate_n_flags & RATE_MCS_ANT_C_MSK);
Guy Cohenfde0db32008-04-21 15:42:01 -0700402}
403
Ben Cahill77626352007-11-29 11:09:44 +0800404/**
405 * rs_collect_tx_data - Update the success/failure sliding window
406 *
407 * We keep a sliding window of the last 62 packets transmitted
408 * at this rate. window->data contains the bitmask of successful
409 * packets.
410 */
Tomas Winklere227cea2008-07-18 13:53:05 +0800411static int rs_collect_tx_data(struct iwl_rate_scale_data *windows,
Ron Rindjunsky99556432008-01-28 14:07:25 +0200412 int scale_index, s32 tpt, int retries,
413 int successes)
Zhu Yib481de92007-09-25 17:54:57 -0700414{
Tomas Winklere227cea2008-07-18 13:53:05 +0800415 struct iwl_rate_scale_data *window = NULL;
Guy Cohen39e88502008-04-23 17:14:57 -0700416 static const u64 mask = (((u64)1) << (IWL_RATE_MAX_WINDOW - 1));
Zhu Yib481de92007-09-25 17:54:57 -0700417 s32 fail_count;
418
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800419 if (scale_index < 0 || scale_index >= IWL_RATE_COUNT)
420 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -0700421
Ben Cahill77626352007-11-29 11:09:44 +0800422 /* Select data for current tx bit rate */
Zhu Yib481de92007-09-25 17:54:57 -0700423 window = &(windows[scale_index]);
424
Ben Cahill77626352007-11-29 11:09:44 +0800425 /*
426 * Keep track of only the latest 62 tx frame attempts in this rate's
427 * history window; anything older isn't really relevant any more.
428 * If we have filled up the sliding window, drop the oldest attempt;
429 * if the oldest attempt (highest bit in bitmap) shows "success",
430 * subtract "1" from the success counter (this is the main reason
431 * we keep these bitmaps!).
432 */
Ron Rindjunsky99556432008-01-28 14:07:25 +0200433 while (retries > 0) {
Guy Cohen39e88502008-04-23 17:14:57 -0700434 if (window->counter >= IWL_RATE_MAX_WINDOW) {
435
436 /* remove earliest */
437 window->counter = IWL_RATE_MAX_WINDOW - 1;
438
Ron Rindjunsky99556432008-01-28 14:07:25 +0200439 if (window->data & mask) {
440 window->data &= ~mask;
Guy Cohen39e88502008-04-23 17:14:57 -0700441 window->success_counter--;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200442 }
Zhu Yib481de92007-09-25 17:54:57 -0700443 }
Zhu Yib481de92007-09-25 17:54:57 -0700444
Ron Rindjunsky99556432008-01-28 14:07:25 +0200445 /* Increment frames-attempted counter */
446 window->counter++;
Ben Cahill77626352007-11-29 11:09:44 +0800447
Ron Rindjunsky99556432008-01-28 14:07:25 +0200448 /* Shift bitmap by one frame (throw away oldest history),
449 * OR in "1", and increment "success" if this
450 * frame was successful. */
Guy Cohen90d77952008-09-09 10:54:53 +0800451 window->data <<= 1;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200452 if (successes > 0) {
Guy Cohen39e88502008-04-23 17:14:57 -0700453 window->success_counter++;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200454 window->data |= 0x1;
455 successes--;
456 }
457
458 retries--;
Zhu Yib481de92007-09-25 17:54:57 -0700459 }
460
Ben Cahill77626352007-11-29 11:09:44 +0800461 /* Calculate current success ratio, avoid divide-by-0! */
Zhu Yib481de92007-09-25 17:54:57 -0700462 if (window->counter > 0)
463 window->success_ratio = 128 * (100 * window->success_counter)
464 / window->counter;
465 else
466 window->success_ratio = IWL_INVALID_VALUE;
467
468 fail_count = window->counter - window->success_counter;
469
Ben Cahill77626352007-11-29 11:09:44 +0800470 /* Calculate average throughput, if we have enough history. */
Zhu Yib481de92007-09-25 17:54:57 -0700471 if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
472 (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
473 window->average_tpt = (window->success_ratio * tpt + 64) / 128;
474 else
475 window->average_tpt = IWL_INVALID_VALUE;
476
Ben Cahill77626352007-11-29 11:09:44 +0800477 /* Tag this window as having been updated */
Zhu Yib481de92007-09-25 17:54:57 -0700478 window->stamp = jiffies;
479
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800480 return 0;
Zhu Yib481de92007-09-25 17:54:57 -0700481}
482
Ben Cahill77626352007-11-29 11:09:44 +0800483/*
484 * Fill uCode API rate_n_flags field, based on "search" or "active" table.
485 */
Guy Cohen39e88502008-04-23 17:14:57 -0700486/* FIXME:RS:remove this function and put the flags statically in the table */
Tomas Winkler978785a2008-12-19 10:37:31 +0800487static u32 rate_n_flags_from_tbl(struct iwl_priv *priv,
488 struct iwl_scale_tbl_info *tbl,
489 int index, u8 use_green)
Zhu Yib481de92007-09-25 17:54:57 -0700490{
Guy Cohen39e88502008-04-23 17:14:57 -0700491 u32 rate_n_flags = 0;
492
Zhu Yib481de92007-09-25 17:54:57 -0700493 if (is_legacy(tbl->lq_type)) {
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800494 rate_n_flags = iwl_rates[index].plcp;
Zhu Yib481de92007-09-25 17:54:57 -0700495 if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
Guy Cohen39e88502008-04-23 17:14:57 -0700496 rate_n_flags |= RATE_MCS_CCK_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700497
Guy Cohenfde0db32008-04-21 15:42:01 -0700498 } else if (is_Ht(tbl->lq_type)) {
499 if (index > IWL_LAST_OFDM_RATE) {
Tomas Winkler978785a2008-12-19 10:37:31 +0800500 IWL_ERR(priv, "Invalid HT rate index %d\n", index);
Zhu Yib481de92007-09-25 17:54:57 -0700501 index = IWL_LAST_OFDM_RATE;
Guy Cohenfde0db32008-04-21 15:42:01 -0700502 }
Guy Cohen39e88502008-04-23 17:14:57 -0700503 rate_n_flags = RATE_MCS_HT_MSK;
Guy Cohenfde0db32008-04-21 15:42:01 -0700504
505 if (is_siso(tbl->lq_type))
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800506 rate_n_flags |= iwl_rates[index].plcp_siso;
Guy Cohenfde0db32008-04-21 15:42:01 -0700507 else if (is_mimo2(tbl->lq_type))
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800508 rate_n_flags |= iwl_rates[index].plcp_mimo2;
Guy Cohenfde0db32008-04-21 15:42:01 -0700509 else
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800510 rate_n_flags |= iwl_rates[index].plcp_mimo3;
Zhu Yib481de92007-09-25 17:54:57 -0700511 } else {
Tomas Winkler978785a2008-12-19 10:37:31 +0800512 IWL_ERR(priv, "Invalid tbl->lq_type %d\n", tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -0700513 }
514
Guy Cohen39e88502008-04-23 17:14:57 -0700515 rate_n_flags |= ((tbl->ant_type << RATE_MCS_ANT_POS) &
Guy Cohenfde0db32008-04-21 15:42:01 -0700516 RATE_MCS_ANT_ABC_MSK);
Zhu Yib481de92007-09-25 17:54:57 -0700517
Guy Cohen39e88502008-04-23 17:14:57 -0700518 if (is_Ht(tbl->lq_type)) {
519 if (tbl->is_fat) {
520 if (tbl->is_dup)
521 rate_n_flags |= RATE_MCS_DUP_MSK;
522 else
523 rate_n_flags |= RATE_MCS_FAT_MSK;
524 }
525 if (tbl->is_SGI)
526 rate_n_flags |= RATE_MCS_SGI_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700527
Guy Cohen39e88502008-04-23 17:14:57 -0700528 if (use_green) {
529 rate_n_flags |= RATE_MCS_GF_MSK;
530 if (is_siso(tbl->lq_type) && tbl->is_SGI) {
531 rate_n_flags &= ~RATE_MCS_SGI_MSK;
Tomas Winkler978785a2008-12-19 10:37:31 +0800532 IWL_ERR(priv, "GF was set with SGI:SISO\n");
Guy Cohen39e88502008-04-23 17:14:57 -0700533 }
534 }
Zhu Yib481de92007-09-25 17:54:57 -0700535 }
Guy Cohen39e88502008-04-23 17:14:57 -0700536 return rate_n_flags;
Zhu Yib481de92007-09-25 17:54:57 -0700537}
538
Ben Cahill77626352007-11-29 11:09:44 +0800539/*
540 * Interpret uCode API's rate_n_flags format,
541 * fill "search" or "active" tx mode table.
542 */
Guy Cohen39e88502008-04-23 17:14:57 -0700543static int rs_get_tbl_info_from_mcs(const u32 rate_n_flags,
Johannes Berg8318d782008-01-24 19:38:38 +0100544 enum ieee80211_band band,
Tomas Winklere227cea2008-07-18 13:53:05 +0800545 struct iwl_scale_tbl_info *tbl,
Zhu Yib481de92007-09-25 17:54:57 -0700546 int *rate_idx)
547{
Guy Cohen39e88502008-04-23 17:14:57 -0700548 u32 ant_msk = (rate_n_flags & RATE_MCS_ANT_ABC_MSK);
549 u8 num_of_ant = get_num_of_ant_from_rate(rate_n_flags);
550 u8 mcs;
Zhu Yib481de92007-09-25 17:54:57 -0700551
Tomas Winklere7d326ac2008-06-12 09:47:11 +0800552 *rate_idx = iwl_hwrate_to_plcp_idx(rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -0700553
Guy Cohenfde0db32008-04-21 15:42:01 -0700554 if (*rate_idx == IWL_RATE_INVALID) {
Zhu Yib481de92007-09-25 17:54:57 -0700555 *rate_idx = -1;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800556 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -0700557 }
Ben Cahill77626352007-11-29 11:09:44 +0800558 tbl->is_SGI = 0; /* default legacy setup */
Zhu Yib481de92007-09-25 17:54:57 -0700559 tbl->is_fat = 0;
560 tbl->is_dup = 0;
Guy Cohenfde0db32008-04-21 15:42:01 -0700561 tbl->ant_type = (ant_msk >> RATE_MCS_ANT_POS);
562 tbl->lq_type = LQ_NONE;
Zhu Yib481de92007-09-25 17:54:57 -0700563
Ben Cahill77626352007-11-29 11:09:44 +0800564 /* legacy rate format */
Guy Cohen39e88502008-04-23 17:14:57 -0700565 if (!(rate_n_flags & RATE_MCS_HT_MSK)) {
Guy Cohenfde0db32008-04-21 15:42:01 -0700566 if (num_of_ant == 1) {
Johannes Berg8318d782008-01-24 19:38:38 +0100567 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700568 tbl->lq_type = LQ_A;
569 else
570 tbl->lq_type = LQ_G;
Zhu Yib481de92007-09-25 17:54:57 -0700571 }
Guy Cohenfde0db32008-04-21 15:42:01 -0700572 /* HT rate format */
Zhu Yib481de92007-09-25 17:54:57 -0700573 } else {
Guy Cohen39e88502008-04-23 17:14:57 -0700574 if (rate_n_flags & RATE_MCS_SGI_MSK)
Zhu Yib481de92007-09-25 17:54:57 -0700575 tbl->is_SGI = 1;
576
Guy Cohen39e88502008-04-23 17:14:57 -0700577 if ((rate_n_flags & RATE_MCS_FAT_MSK) ||
578 (rate_n_flags & RATE_MCS_DUP_MSK))
Zhu Yib481de92007-09-25 17:54:57 -0700579 tbl->is_fat = 1;
580
Guy Cohen39e88502008-04-23 17:14:57 -0700581 if (rate_n_flags & RATE_MCS_DUP_MSK)
Zhu Yib481de92007-09-25 17:54:57 -0700582 tbl->is_dup = 1;
Guy Cohenfde0db32008-04-21 15:42:01 -0700583
Guy Cohen39e88502008-04-23 17:14:57 -0700584 mcs = rs_extract_rate(rate_n_flags);
Guy Cohenfde0db32008-04-21 15:42:01 -0700585
Guy Cohen39e88502008-04-23 17:14:57 -0700586 /* SISO */
587 if (mcs <= IWL_RATE_SISO_60M_PLCP) {
Guy Cohenfde0db32008-04-21 15:42:01 -0700588 if (num_of_ant == 1)
589 tbl->lq_type = LQ_SISO; /*else NONE*/
590 /* MIMO2 */
Guy Cohen39e88502008-04-23 17:14:57 -0700591 } else if (mcs <= IWL_RATE_MIMO2_60M_PLCP) {
Guy Cohenfde0db32008-04-21 15:42:01 -0700592 if (num_of_ant == 2)
593 tbl->lq_type = LQ_MIMO2;
594 /* MIMO3 */
595 } else {
596 if (num_of_ant == 3)
597 tbl->lq_type = LQ_MIMO3;
598 }
Zhu Yib481de92007-09-25 17:54:57 -0700599 }
600 return 0;
601}
Guy Cohenaade00c2008-04-23 17:14:59 -0700602
603/* switch to another antenna/antennas and return 1 */
604/* if no other valid antenna found, return 0 */
605static int rs_toggle_antenna(u32 valid_ant, u32 *rate_n_flags,
Tomas Winklere227cea2008-07-18 13:53:05 +0800606 struct iwl_scale_tbl_info *tbl)
Zhu Yib481de92007-09-25 17:54:57 -0700607{
Guy Cohenaade00c2008-04-23 17:14:59 -0700608 u8 new_ant_type;
609
610 if (!tbl->ant_type || tbl->ant_type > ANT_ABC)
611 return 0;
612
613 if (!rs_is_valid_ant(valid_ant, tbl->ant_type))
614 return 0;
615
616 new_ant_type = ant_toggle_lookup[tbl->ant_type];
617
618 while ((new_ant_type != tbl->ant_type) &&
619 !rs_is_valid_ant(valid_ant, new_ant_type))
620 new_ant_type = ant_toggle_lookup[new_ant_type];
621
622 if (new_ant_type == tbl->ant_type)
623 return 0;
624
625 tbl->ant_type = new_ant_type;
626 *rate_n_flags &= ~RATE_MCS_ANT_ABC_MSK;
627 *rate_n_flags |= new_ant_type << RATE_MCS_ANT_POS;
628 return 1;
Zhu Yib481de92007-09-25 17:54:57 -0700629}
630
Guy Cohen39e88502008-04-23 17:14:57 -0700631/* FIXME:RS: in 4965 we don't use greenfield at all */
Guy Cohenf935a6d2008-04-23 17:15:02 -0700632/* FIXME:RS: don't use greenfield for now in TX */
Guy Cohenf935a6d2008-04-23 17:15:02 -0700633#if 0
634static inline u8 rs_use_green(struct iwl_priv *priv, struct ieee80211_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -0700635{
Tomas Winkler3ac7f142008-07-21 02:40:14 +0300636 return (conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) &&
Ron Rindjunsky270243a2007-11-26 16:14:41 +0200637 priv->current_ht_config.is_green_field &&
Tomas Winkler3ac7f142008-07-21 02:40:14 +0300638 !priv->current_ht_config.non_GF_STA_present;
Zhu Yib481de92007-09-25 17:54:57 -0700639}
Ron Rindjunsky4f85f5b2008-06-09 22:54:35 +0300640#endif
Guy Cohenf935a6d2008-04-23 17:15:02 -0700641static inline u8 rs_use_green(struct iwl_priv *priv, struct ieee80211_conf *conf)
642{
643 return 0;
644}
Zhu Yib481de92007-09-25 17:54:57 -0700645
646/**
647 * rs_get_supported_rates - get the available rates
648 *
649 * if management frame or broadcast frame only return
650 * basic available rates.
651 *
652 */
Tomas Winklere227cea2008-07-18 13:53:05 +0800653static u16 rs_get_supported_rates(struct iwl_lq_sta *lq_sta,
654 struct ieee80211_hdr *hdr,
655 enum iwl_table_type rate_type)
Zhu Yib481de92007-09-25 17:54:57 -0700656{
Zhu Yib481de92007-09-25 17:54:57 -0700657 if (hdr && is_multicast_ether_addr(hdr->addr1) &&
Guy Coheneecd6e52008-04-23 17:14:58 -0700658 lq_sta->active_rate_basic)
659 return lq_sta->active_rate_basic;
660
661 if (is_legacy(rate_type)) {
662 return lq_sta->active_legacy_rate;
663 } else {
664 if (is_siso(rate_type))
665 return lq_sta->active_siso_rate;
666 else if (is_mimo2(rate_type))
667 return lq_sta->active_mimo2_rate;
668 else
669 return lq_sta->active_mimo3_rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -0800670 }
Zhu Yib481de92007-09-25 17:54:57 -0700671}
672
Ester Kummerbf403db2008-05-05 10:22:40 +0800673static u16 rs_get_adjacent_rate(struct iwl_priv *priv, u8 index, u16 rate_mask,
674 int rate_type)
Zhu Yib481de92007-09-25 17:54:57 -0700675{
676 u8 high = IWL_RATE_INVALID;
677 u8 low = IWL_RATE_INVALID;
678
Ian Schram01ebd062007-10-25 17:15:22 +0800679 /* 802.11A or ht walks to the next literal adjacent rate in
Zhu Yib481de92007-09-25 17:54:57 -0700680 * the rate table */
681 if (is_a_band(rate_type) || !is_legacy(rate_type)) {
682 int i;
683 u32 mask;
684
685 /* Find the previous rate that is in the rate mask */
686 i = index - 1;
687 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
688 if (rate_mask & mask) {
689 low = i;
690 break;
691 }
692 }
693
694 /* Find the next rate that is in the rate mask */
695 i = index + 1;
696 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
697 if (rate_mask & mask) {
698 high = i;
699 break;
700 }
701 }
702
703 return (high << 8) | low;
704 }
705
706 low = index;
707 while (low != IWL_RATE_INVALID) {
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800708 low = iwl_rates[low].prev_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700709 if (low == IWL_RATE_INVALID)
710 break;
711 if (rate_mask & (1 << low))
712 break;
Tomas Winklere1623442009-01-27 14:27:56 -0800713 IWL_DEBUG_RATE(priv, "Skipping masked lower rate: %d\n", low);
Zhu Yib481de92007-09-25 17:54:57 -0700714 }
715
716 high = index;
717 while (high != IWL_RATE_INVALID) {
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800718 high = iwl_rates[high].next_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700719 if (high == IWL_RATE_INVALID)
720 break;
721 if (rate_mask & (1 << high))
722 break;
Tomas Winklere1623442009-01-27 14:27:56 -0800723 IWL_DEBUG_RATE(priv, "Skipping masked higher rate: %d\n", high);
Zhu Yib481de92007-09-25 17:54:57 -0700724 }
725
726 return (high << 8) | low;
727}
728
Tomas Winklere227cea2008-07-18 13:53:05 +0800729static u32 rs_get_lower_rate(struct iwl_lq_sta *lq_sta,
730 struct iwl_scale_tbl_info *tbl,
731 u8 scale_index, u8 ht_possible)
Zhu Yib481de92007-09-25 17:54:57 -0700732{
Zhu Yib481de92007-09-25 17:54:57 -0700733 s32 low;
734 u16 rate_mask;
735 u16 high_low;
736 u8 switch_to_legacy = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -0800737 u8 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -0700738
739 /* check if we need to switch from HT to legacy rates.
740 * assumption is that mandatory rates (1Mbps or 6Mbps)
741 * are always supported (spec demand) */
742 if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_index)) {
743 switch_to_legacy = 1;
744 scale_index = rs_ht_to_legacy[scale_index];
Johannes Berg8318d782008-01-24 19:38:38 +0100745 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700746 tbl->lq_type = LQ_A;
747 else
748 tbl->lq_type = LQ_G;
749
Guy Cohen69fdb302008-04-23 17:15:01 -0700750 if (num_of_ant(tbl->ant_type) > 1)
Guy Cohenfde0db32008-04-21 15:42:01 -0700751 tbl->ant_type = ANT_A;/*FIXME:RS*/
Zhu Yib481de92007-09-25 17:54:57 -0700752
753 tbl->is_fat = 0;
754 tbl->is_SGI = 0;
755 }
756
Guy Coheneecd6e52008-04-23 17:14:58 -0700757 rate_mask = rs_get_supported_rates(lq_sta, NULL, tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -0700758
Ben Cahill77626352007-11-29 11:09:44 +0800759 /* Mask with station rate restriction */
Zhu Yib481de92007-09-25 17:54:57 -0700760 if (is_legacy(tbl->lq_type)) {
Ben Cahill77626352007-11-29 11:09:44 +0800761 /* supp_rates has no CCK bits in A mode */
Johannes Berg8318d782008-01-24 19:38:38 +0100762 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700763 rate_mask = (u16)(rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -0800764 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
Zhu Yib481de92007-09-25 17:54:57 -0700765 else
Tomas Winklerc33104f2008-01-14 17:46:21 -0800766 rate_mask = (u16)(rate_mask & lq_sta->supp_rates);
Zhu Yib481de92007-09-25 17:54:57 -0700767 }
768
Ben Cahill77626352007-11-29 11:09:44 +0800769 /* If we switched from HT to legacy, check current rate */
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800770 if (switch_to_legacy && (rate_mask & (1 << scale_index))) {
Guy Cohen39e88502008-04-23 17:14:57 -0700771 low = scale_index;
772 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700773 }
774
Ester Kummerbf403db2008-05-05 10:22:40 +0800775 high_low = rs_get_adjacent_rate(lq_sta->drv, scale_index, rate_mask,
776 tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -0700777 low = high_low & 0xff;
778
Guy Cohen39e88502008-04-23 17:14:57 -0700779 if (low == IWL_RATE_INVALID)
780 low = scale_index;
781
782out:
Tomas Winkler978785a2008-12-19 10:37:31 +0800783 return rate_n_flags_from_tbl(lq_sta->drv, tbl, low, is_green);
Zhu Yib481de92007-09-25 17:54:57 -0700784}
785
Ben Cahill77626352007-11-29 11:09:44 +0800786/*
787 * mac80211 sends us Tx status
788 */
Johannes Berg4b7679a2008-09-18 18:14:18 +0200789static void rs_tx_status(void *priv_r, struct ieee80211_supported_band *sband,
790 struct ieee80211_sta *sta, void *priv_sta,
Johannes Berge039fa42008-05-15 12:55:29 +0200791 struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -0700792{
793 int status;
794 u8 retries;
795 int rs_index, index = 0;
Tomas Winkler417f1142008-11-12 13:14:06 -0800796 struct iwl_lq_sta *lq_sta = priv_sta;
Tomas Winkler66c73db2008-04-15 16:01:40 -0700797 struct iwl_link_quality_cmd *table;
Zhu Yib481de92007-09-25 17:54:57 -0700798 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berg4b7679a2008-09-18 18:14:18 +0200799 struct iwl_priv *priv = (struct iwl_priv *)priv_r;
800 struct ieee80211_hw *hw = priv->hw;
Johannes Berge039fa42008-05-15 12:55:29 +0200801 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Tomas Winklere227cea2008-07-18 13:53:05 +0800802 struct iwl_rate_scale_data *window = NULL;
803 struct iwl_rate_scale_data *search_win = NULL;
Guy Cohen39e88502008-04-23 17:14:57 -0700804 u32 tx_rate;
Tomas Winklere227cea2008-07-18 13:53:05 +0800805 struct iwl_scale_tbl_info tbl_type;
806 struct iwl_scale_tbl_info *curr_tbl, *search_tbl;
Zhu Yib481de92007-09-25 17:54:57 -0700807 u8 active_index = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700808 s32 tpt = 0;
809
Tomas Winklere1623442009-01-27 14:27:56 -0800810 IWL_DEBUG_RATE_LIMIT(priv, "get frame ack response, update rate scale window\n");
Zhu Yib481de92007-09-25 17:54:57 -0700811
Tomas Winkler417f1142008-11-12 13:14:06 -0800812 if (!ieee80211_is_data(hdr->frame_control) ||
813 is_multicast_ether_addr(hdr->addr1))
Zhu Yib481de92007-09-25 17:54:57 -0700814 return;
815
Ron Rindjunsky99556432008-01-28 14:07:25 +0200816 /* This packet was aggregated but doesn't carry rate scale info */
Johannes Berge039fa42008-05-15 12:55:29 +0200817 if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
818 !(info->flags & IEEE80211_TX_STAT_AMPDU))
Ron Rindjunsky99556432008-01-28 14:07:25 +0200819 return;
820
Wey-Yi Guy8fe72312009-03-10 14:35:10 -0700821 if (info->flags & IEEE80211_TX_STAT_AMPDU)
822 retries = 0;
823 else
824 retries = info->status.rates[0].count - 1;
Zhu Yib481de92007-09-25 17:54:57 -0700825
826 if (retries > 15)
827 retries = 15;
828
Johannes Berg05c914f2008-09-11 00:01:58 +0200829 if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) &&
Tomas Winklerc33104f2008-01-14 17:46:21 -0800830 !lq_sta->ibss_sta_added)
Tomas Winklerf4d60822008-03-05 11:31:00 -0800831 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700832
Tomas Winklerc33104f2008-01-14 17:46:21 -0800833 table = &lq_sta->lq;
834 active_index = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -0700835
Tomas Winklerc33104f2008-01-14 17:46:21 -0800836 curr_tbl = &(lq_sta->lq_info[active_index]);
837 search_tbl = &(lq_sta->lq_info[(1 - active_index)]);
Tomas Winklere227cea2008-07-18 13:53:05 +0800838 window = (struct iwl_rate_scale_data *)&(curr_tbl->win[0]);
839 search_win = (struct iwl_rate_scale_data *)&(search_tbl->win[0]);
Zhu Yib481de92007-09-25 17:54:57 -0700840
Ben Cahill77626352007-11-29 11:09:44 +0800841 /*
842 * Ignore this Tx frame response if its initial rate doesn't match
843 * that of latest Link Quality command. There may be stragglers
844 * from a previous Link Quality command, but we're no longer interested
845 * in those; they're either from the "active" mode while we're trying
846 * to check "search" mode, or a prior "search" mode after we've moved
847 * to a new "search" mode (which might become the new "active" mode).
848 */
Guy Cohen39e88502008-04-23 17:14:57 -0700849 tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
850 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800851 if (priv->band == IEEE80211_BAND_5GHZ)
852 rs_index -= IWL_FIRST_OFDM_RATE;
853
Johannes Berge6a98542008-10-21 12:40:02 +0200854 if ((info->status.rates[0].idx < 0) ||
855 (tbl_type.is_SGI != !!(info->status.rates[0].flags & IEEE80211_TX_RC_SHORT_GI)) ||
856 (tbl_type.is_fat != !!(info->status.rates[0].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)) ||
857 (tbl_type.is_dup != !!(info->status.rates[0].flags & IEEE80211_TX_RC_DUP_DATA)) ||
858 (tbl_type.ant_type != info->antenna_sel_tx) ||
859 (!!(tx_rate & RATE_MCS_HT_MSK) != !!(info->status.rates[0].flags & IEEE80211_TX_RC_MCS)) ||
860 (!!(tx_rate & RATE_MCS_GF_MSK) != !!(info->status.rates[0].flags & IEEE80211_TX_RC_GREEN_FIELD)) ||
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800861 (hw->wiphy->bands[priv->band]->bitrates[rs_index].bitrate !=
Johannes Berge6a98542008-10-21 12:40:02 +0200862 hw->wiphy->bands[info->band]->bitrates[info->status.rates[0].idx].bitrate)) {
Tomas Winklere1623442009-01-27 14:27:56 -0800863 IWL_DEBUG_RATE(priv, "initial rate does not match 0x%x\n", tx_rate);
Mohamed Abbasd5e49032008-12-12 08:22:15 -0800864 /* the last LQ command could failed so the LQ in ucode not
865 * the same in driver sync up
866 */
Abbas, Mohamed7d049e52009-01-20 21:33:53 -0800867 lq_sta->missed_rate_counter++;
868 if (lq_sta->missed_rate_counter > IWL_MISSED_RATE_MAX) {
869 lq_sta->missed_rate_counter = 0;
870 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
871 }
Tomas Winklerf4d60822008-03-05 11:31:00 -0800872 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700873 }
874
Abbas, Mohamed7d049e52009-01-20 21:33:53 -0800875 lq_sta->missed_rate_counter = 0;
Ben Cahill77626352007-11-29 11:09:44 +0800876 /* Update frame history window with "failure" for each Tx retry. */
Zhu Yib481de92007-09-25 17:54:57 -0700877 while (retries) {
Ben Cahill77626352007-11-29 11:09:44 +0800878 /* Look up the rate and other info used for each tx attempt.
879 * Each tx attempt steps one entry deeper in the rate table. */
Guy Cohen39e88502008-04-23 17:14:57 -0700880 tx_rate = le32_to_cpu(table->rs_table[index].rate_n_flags);
881 rs_get_tbl_info_from_mcs(tx_rate, priv->band,
Zhu Yib481de92007-09-25 17:54:57 -0700882 &tbl_type, &rs_index);
883
Ben Cahill77626352007-11-29 11:09:44 +0800884 /* If type matches "search" table,
885 * add failure to "search" history */
Zhu Yib481de92007-09-25 17:54:57 -0700886 if ((tbl_type.lq_type == search_tbl->lq_type) &&
Guy Cohenfde0db32008-04-21 15:42:01 -0700887 (tbl_type.ant_type == search_tbl->ant_type) &&
Zhu Yib481de92007-09-25 17:54:57 -0700888 (tbl_type.is_SGI == search_tbl->is_SGI)) {
889 if (search_tbl->expected_tpt)
890 tpt = search_tbl->expected_tpt[rs_index];
891 else
892 tpt = 0;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200893 rs_collect_tx_data(search_win, rs_index, tpt, 1, 0);
Ben Cahill77626352007-11-29 11:09:44 +0800894
895 /* Else if type matches "current/active" table,
896 * add failure to "current/active" history */
Zhu Yib481de92007-09-25 17:54:57 -0700897 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
Guy Cohenfde0db32008-04-21 15:42:01 -0700898 (tbl_type.ant_type == curr_tbl->ant_type) &&
Zhu Yib481de92007-09-25 17:54:57 -0700899 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
900 if (curr_tbl->expected_tpt)
901 tpt = curr_tbl->expected_tpt[rs_index];
902 else
903 tpt = 0;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200904 rs_collect_tx_data(window, rs_index, tpt, 1, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700905 }
Ben Cahill77626352007-11-29 11:09:44 +0800906
907 /* If not searching for a new mode, increment failed counter
908 * ... this helps determine when to start searching again */
Tomas Winklerc33104f2008-01-14 17:46:21 -0800909 if (lq_sta->stay_in_tbl)
910 lq_sta->total_failed++;
Zhu Yib481de92007-09-25 17:54:57 -0700911 --retries;
912 index++;
913
914 }
915
Ben Cahill77626352007-11-29 11:09:44 +0800916 /*
917 * Find (by rate) the history window to update with final Tx attempt;
918 * if Tx was successful first try, use original rate,
919 * else look up the rate that was, finally, successful.
920 */
Guy Cohen39e88502008-04-23 17:14:57 -0700921 tx_rate = le32_to_cpu(table->rs_table[index].rate_n_flags);
922 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
Zhu Yib481de92007-09-25 17:54:57 -0700923
Ben Cahill77626352007-11-29 11:09:44 +0800924 /* Update frame history window with "success" if Tx got ACKed ... */
Johannes Berge039fa42008-05-15 12:55:29 +0200925 status = !!(info->flags & IEEE80211_TX_STAT_ACK);
Zhu Yib481de92007-09-25 17:54:57 -0700926
Ben Cahill77626352007-11-29 11:09:44 +0800927 /* If type matches "search" table,
928 * add final tx status to "search" history */
Zhu Yib481de92007-09-25 17:54:57 -0700929 if ((tbl_type.lq_type == search_tbl->lq_type) &&
Guy Cohenfde0db32008-04-21 15:42:01 -0700930 (tbl_type.ant_type == search_tbl->ant_type) &&
Zhu Yib481de92007-09-25 17:54:57 -0700931 (tbl_type.is_SGI == search_tbl->is_SGI)) {
932 if (search_tbl->expected_tpt)
933 tpt = search_tbl->expected_tpt[rs_index];
934 else
935 tpt = 0;
Wey-Yi Guydf36c042009-03-10 14:35:11 -0700936 if (info->flags & IEEE80211_TX_STAT_AMPDU)
Ron Rindjunsky99556432008-01-28 14:07:25 +0200937 rs_collect_tx_data(search_win, rs_index, tpt,
Johannes Berge039fa42008-05-15 12:55:29 +0200938 info->status.ampdu_ack_len,
939 info->status.ampdu_ack_map);
Ron Rindjunsky99556432008-01-28 14:07:25 +0200940 else
941 rs_collect_tx_data(search_win, rs_index, tpt,
942 1, status);
Ben Cahill77626352007-11-29 11:09:44 +0800943 /* Else if type matches "current/active" table,
944 * add final tx status to "current/active" history */
Zhu Yib481de92007-09-25 17:54:57 -0700945 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
Guy Cohenfde0db32008-04-21 15:42:01 -0700946 (tbl_type.ant_type == curr_tbl->ant_type) &&
Zhu Yib481de92007-09-25 17:54:57 -0700947 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
948 if (curr_tbl->expected_tpt)
949 tpt = curr_tbl->expected_tpt[rs_index];
950 else
951 tpt = 0;
Wey-Yi Guydf36c042009-03-10 14:35:11 -0700952 if (info->flags & IEEE80211_TX_STAT_AMPDU)
Ron Rindjunsky99556432008-01-28 14:07:25 +0200953 rs_collect_tx_data(window, rs_index, tpt,
Johannes Berge039fa42008-05-15 12:55:29 +0200954 info->status.ampdu_ack_len,
955 info->status.ampdu_ack_map);
Ron Rindjunsky99556432008-01-28 14:07:25 +0200956 else
957 rs_collect_tx_data(window, rs_index, tpt,
958 1, status);
Zhu Yib481de92007-09-25 17:54:57 -0700959 }
960
Ben Cahill77626352007-11-29 11:09:44 +0800961 /* If not searching for new mode, increment success/failed counter
962 * ... these help determine when to start searching again */
Tomas Winklerc33104f2008-01-14 17:46:21 -0800963 if (lq_sta->stay_in_tbl) {
Wey-Yi Guydf36c042009-03-10 14:35:11 -0700964 if (info->flags & IEEE80211_TX_STAT_AMPDU) {
Johannes Berge039fa42008-05-15 12:55:29 +0200965 lq_sta->total_success += info->status.ampdu_ack_map;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200966 lq_sta->total_failed +=
Johannes Berge039fa42008-05-15 12:55:29 +0200967 (info->status.ampdu_ack_len - info->status.ampdu_ack_map);
Ron Rindjunsky99556432008-01-28 14:07:25 +0200968 } else {
969 if (status)
970 lq_sta->total_success++;
971 else
972 lq_sta->total_failed++;
973 }
Zhu Yib481de92007-09-25 17:54:57 -0700974 }
975
Ben Cahill77626352007-11-29 11:09:44 +0800976 /* See if there's a better rate or modulation mode to try. */
Abbas, Mohamedc338ba32009-01-21 10:58:02 -0800977 if (sta && sta->supp_rates[sband->band])
978 rs_rate_scale_perform(priv, hdr, sta, lq_sta);
Tomas Winklerf4d60822008-03-05 11:31:00 -0800979out:
Zhu Yib481de92007-09-25 17:54:57 -0700980 return;
981}
982
Ben Cahill77626352007-11-29 11:09:44 +0800983/*
984 * Begin a period of staying with a selected modulation mode.
985 * Set "stay_in_tbl" flag to prevent any mode switches.
986 * Set frame tx success limits according to legacy vs. high-throughput,
987 * and reset overall (spanning all rates) tx success history statistics.
988 * These control how long we stay using same modulation mode before
989 * searching for a new mode.
990 */
Ester Kummerbf403db2008-05-05 10:22:40 +0800991static void rs_set_stay_in_table(struct iwl_priv *priv, u8 is_legacy,
Tomas Winklere227cea2008-07-18 13:53:05 +0800992 struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -0700993{
Tomas Winklere1623442009-01-27 14:27:56 -0800994 IWL_DEBUG_RATE(priv, "we are staying in the same table\n");
Tomas Winklerc33104f2008-01-14 17:46:21 -0800995 lq_sta->stay_in_tbl = 1; /* only place this gets set */
Zhu Yib481de92007-09-25 17:54:57 -0700996 if (is_legacy) {
Tomas Winklerc33104f2008-01-14 17:46:21 -0800997 lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT;
998 lq_sta->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
999 lq_sta->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
Zhu Yib481de92007-09-25 17:54:57 -07001000 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001001 lq_sta->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
1002 lq_sta->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
1003 lq_sta->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
Zhu Yib481de92007-09-25 17:54:57 -07001004 }
Tomas Winklerc33104f2008-01-14 17:46:21 -08001005 lq_sta->table_count = 0;
1006 lq_sta->total_failed = 0;
1007 lq_sta->total_success = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001008}
1009
Ben Cahill77626352007-11-29 11:09:44 +08001010/*
1011 * Find correct throughput table for given mode of modulation
1012 */
Tomas Winklere227cea2008-07-18 13:53:05 +08001013static void rs_set_expected_tpt_table(struct iwl_lq_sta *lq_sta,
1014 struct iwl_scale_tbl_info *tbl)
Zhu Yib481de92007-09-25 17:54:57 -07001015{
1016 if (is_legacy(tbl->lq_type)) {
1017 if (!is_a_band(tbl->lq_type))
1018 tbl->expected_tpt = expected_tpt_G;
1019 else
1020 tbl->expected_tpt = expected_tpt_A;
1021 } else if (is_siso(tbl->lq_type)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001022 if (tbl->is_fat && !lq_sta->is_dup)
Zhu Yib481de92007-09-25 17:54:57 -07001023 if (tbl->is_SGI)
1024 tbl->expected_tpt = expected_tpt_siso40MHzSGI;
1025 else
1026 tbl->expected_tpt = expected_tpt_siso40MHz;
1027 else if (tbl->is_SGI)
1028 tbl->expected_tpt = expected_tpt_siso20MHzSGI;
1029 else
1030 tbl->expected_tpt = expected_tpt_siso20MHz;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001031 } else if (is_mimo2(tbl->lq_type)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001032 if (tbl->is_fat && !lq_sta->is_dup)
Zhu Yib481de92007-09-25 17:54:57 -07001033 if (tbl->is_SGI)
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001034 tbl->expected_tpt = expected_tpt_mimo2_40MHzSGI;
Zhu Yib481de92007-09-25 17:54:57 -07001035 else
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001036 tbl->expected_tpt = expected_tpt_mimo2_40MHz;
Zhu Yib481de92007-09-25 17:54:57 -07001037 else if (tbl->is_SGI)
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001038 tbl->expected_tpt = expected_tpt_mimo2_20MHzSGI;
Zhu Yib481de92007-09-25 17:54:57 -07001039 else
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001040 tbl->expected_tpt = expected_tpt_mimo2_20MHz;
1041 } else if (is_mimo3(tbl->lq_type)) {
1042 if (tbl->is_fat && !lq_sta->is_dup)
1043 if (tbl->is_SGI)
1044 tbl->expected_tpt = expected_tpt_mimo3_40MHzSGI;
1045 else
1046 tbl->expected_tpt = expected_tpt_mimo3_40MHz;
1047 else if (tbl->is_SGI)
1048 tbl->expected_tpt = expected_tpt_mimo3_20MHzSGI;
1049 else
1050 tbl->expected_tpt = expected_tpt_mimo3_20MHz;
Zhu Yib481de92007-09-25 17:54:57 -07001051 } else
1052 tbl->expected_tpt = expected_tpt_G;
1053}
1054
Ben Cahill77626352007-11-29 11:09:44 +08001055/*
1056 * Find starting rate for new "search" high-throughput mode of modulation.
1057 * Goal is to find lowest expected rate (under perfect conditions) that is
1058 * above the current measured throughput of "active" mode, to give new mode
1059 * a fair chance to prove itself without too many challenges.
1060 *
1061 * This gets called when transitioning to more aggressive modulation
1062 * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
1063 * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
1064 * to decrease to match "active" throughput. When moving from MIMO to SISO,
1065 * bit rate will typically need to increase, but not if performance was bad.
1066 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001067static s32 rs_get_best_rate(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001068 struct iwl_lq_sta *lq_sta,
1069 struct iwl_scale_tbl_info *tbl, /* "search" */
Guy Cohenf935a6d2008-04-23 17:15:02 -07001070 u16 rate_mask, s8 index)
Zhu Yib481de92007-09-25 17:54:57 -07001071{
Ben Cahill77626352007-11-29 11:09:44 +08001072 /* "active" values */
Tomas Winklere227cea2008-07-18 13:53:05 +08001073 struct iwl_scale_tbl_info *active_tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001074 &(lq_sta->lq_info[lq_sta->active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07001075 s32 active_sr = active_tbl->win[index].success_ratio;
Zhu Yib481de92007-09-25 17:54:57 -07001076 s32 active_tpt = active_tbl->expected_tpt[index];
Ben Cahill77626352007-11-29 11:09:44 +08001077
1078 /* expected "search" throughput */
1079 s32 *tpt_tbl = tbl->expected_tpt;
1080
1081 s32 new_rate, high, low, start_hi;
Zhu Yib481de92007-09-25 17:54:57 -07001082 u16 high_low;
Guy Cohenf935a6d2008-04-23 17:15:02 -07001083 s8 rate = index;
Zhu Yib481de92007-09-25 17:54:57 -07001084
1085 new_rate = high = low = start_hi = IWL_RATE_INVALID;
1086
1087 for (; ;) {
Ester Kummerbf403db2008-05-05 10:22:40 +08001088 high_low = rs_get_adjacent_rate(priv, rate, rate_mask,
1089 tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -07001090
1091 low = high_low & 0xff;
1092 high = (high_low >> 8) & 0xff;
1093
Ben Cahill77626352007-11-29 11:09:44 +08001094 /*
1095 * Lower the "search" bit rate, to give new "search" mode
1096 * approximately the same throughput as "active" if:
1097 *
1098 * 1) "Active" mode has been working modestly well (but not
1099 * great), and expected "search" throughput (under perfect
1100 * conditions) at candidate rate is above the actual
1101 * measured "active" throughput (but less than expected
1102 * "active" throughput under perfect conditions).
1103 * OR
1104 * 2) "Active" mode has been working perfectly or very well
1105 * and expected "search" throughput (under perfect
1106 * conditions) at candidate rate is above expected
1107 * "active" throughput (under perfect conditions).
1108 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001109 if ((((100 * tpt_tbl[rate]) > lq_sta->last_tpt) &&
Zhu Yib481de92007-09-25 17:54:57 -07001110 ((active_sr > IWL_RATE_DECREASE_TH) &&
1111 (active_sr <= IWL_RATE_HIGH_TH) &&
1112 (tpt_tbl[rate] <= active_tpt))) ||
1113 ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
1114 (tpt_tbl[rate] > active_tpt))) {
1115
Ben Cahill77626352007-11-29 11:09:44 +08001116 /* (2nd or later pass)
1117 * If we've already tried to raise the rate, and are
1118 * now trying to lower it, use the higher rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001119 if (start_hi != IWL_RATE_INVALID) {
1120 new_rate = start_hi;
1121 break;
1122 }
Ben Cahill77626352007-11-29 11:09:44 +08001123
Zhu Yib481de92007-09-25 17:54:57 -07001124 new_rate = rate;
Ben Cahill77626352007-11-29 11:09:44 +08001125
1126 /* Loop again with lower rate */
Zhu Yib481de92007-09-25 17:54:57 -07001127 if (low != IWL_RATE_INVALID)
1128 rate = low;
Ben Cahill77626352007-11-29 11:09:44 +08001129
1130 /* Lower rate not available, use the original */
Zhu Yib481de92007-09-25 17:54:57 -07001131 else
1132 break;
Ben Cahill77626352007-11-29 11:09:44 +08001133
1134 /* Else try to raise the "search" rate to match "active" */
Zhu Yib481de92007-09-25 17:54:57 -07001135 } else {
Ben Cahill77626352007-11-29 11:09:44 +08001136 /* (2nd or later pass)
1137 * If we've already tried to lower the rate, and are
1138 * now trying to raise it, use the lower rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001139 if (new_rate != IWL_RATE_INVALID)
1140 break;
Ben Cahill77626352007-11-29 11:09:44 +08001141
1142 /* Loop again with higher rate */
Zhu Yib481de92007-09-25 17:54:57 -07001143 else if (high != IWL_RATE_INVALID) {
1144 start_hi = high;
1145 rate = high;
Ben Cahill77626352007-11-29 11:09:44 +08001146
1147 /* Higher rate not available, use the original */
Zhu Yib481de92007-09-25 17:54:57 -07001148 } else {
Guy Cohen90d77952008-09-09 10:54:53 +08001149 new_rate = rate;
Zhu Yib481de92007-09-25 17:54:57 -07001150 break;
1151 }
1152 }
1153 }
1154
1155 return new_rate;
1156}
Zhu Yib481de92007-09-25 17:54:57 -07001157
Ben Cahill77626352007-11-29 11:09:44 +08001158/*
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001159 * Set up search table for MIMO2
Ben Cahill77626352007-11-29 11:09:44 +08001160 */
Guy Cohenfde0db32008-04-21 15:42:01 -07001161static int rs_switch_to_mimo2(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001162 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001163 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001164 struct ieee80211_sta *sta,
Tomas Winklere227cea2008-07-18 13:53:05 +08001165 struct iwl_scale_tbl_info *tbl, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001166{
Zhu Yib481de92007-09-25 17:54:57 -07001167 u16 rate_mask;
1168 s32 rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001169 s8 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07001170
Luis R. Rodriguezde27e642008-12-23 15:58:44 -08001171 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
Zhu Yib481de92007-09-25 17:54:57 -07001172 return -1;
1173
Johannes Bergd9fe60d2008-10-09 12:13:49 +02001174 if (((sta->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 2)
Tomas Winkler00c5ae22008-09-03 11:26:42 +08001175 == WLAN_HT_CAP_SM_PS_STATIC)
Zhu Yib481de92007-09-25 17:54:57 -07001176 return -1;
1177
Ben Cahill77626352007-11-29 11:09:44 +08001178 /* Need both Tx chains/antennas to support MIMO */
Guy Cohenaade00c2008-04-23 17:14:59 -07001179 if (priv->hw_params.tx_chains_num < 2)
Zhu Yib481de92007-09-25 17:54:57 -07001180 return -1;
1181
Tomas Winklere1623442009-01-27 14:27:56 -08001182 IWL_DEBUG_RATE(priv, "LQ: try to switch to MIMO2\n");
Guy Cohen39e88502008-04-23 17:14:57 -07001183
1184 tbl->lq_type = LQ_MIMO2;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001185 tbl->is_dup = lq_sta->is_dup;
Zhu Yib481de92007-09-25 17:54:57 -07001186 tbl->action = 0;
Guy Cohen39e88502008-04-23 17:14:57 -07001187 rate_mask = lq_sta->active_mimo2_rate;
1188
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001189 if (priv->current_ht_config.supported_chan_width
Guy Cohen39e88502008-04-23 17:14:57 -07001190 == IWL_CHANNEL_WIDTH_40MHZ)
Zhu Yib481de92007-09-25 17:54:57 -07001191 tbl->is_fat = 1;
1192 else
1193 tbl->is_fat = 0;
1194
Guy Cohen39e88502008-04-23 17:14:57 -07001195 /* FIXME: - don't toggle SGI here
Zhu Yib481de92007-09-25 17:54:57 -07001196 if (tbl->is_fat) {
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001197 if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
Zhu Yib481de92007-09-25 17:54:57 -07001198 tbl->is_SGI = 1;
1199 else
1200 tbl->is_SGI = 0;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001201 } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
Zhu Yib481de92007-09-25 17:54:57 -07001202 tbl->is_SGI = 1;
1203 else
1204 tbl->is_SGI = 0;
Guy Cohen39e88502008-04-23 17:14:57 -07001205 */
Zhu Yib481de92007-09-25 17:54:57 -07001206
Guy Coheneecd6e52008-04-23 17:14:58 -07001207 rs_set_expected_tpt_table(lq_sta, tbl);
Zhu Yib481de92007-09-25 17:54:57 -07001208
Guy Cohenf935a6d2008-04-23 17:15:02 -07001209 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
Zhu Yib481de92007-09-25 17:54:57 -07001210
Tomas Winklere1623442009-01-27 14:27:56 -08001211 IWL_DEBUG_RATE(priv, "LQ: MIMO2 best rate %d mask %X\n", rate, rate_mask);
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001212 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1213 IWL_DEBUG_RATE(priv, "Can't switch with index %d rate mask %x\n",
1214 rate, rate_mask);
1215 return -1;
1216 }
1217 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
Guy Cohen39e88502008-04-23 17:14:57 -07001218
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001219 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
1220 tbl->current_rate, is_green);
1221 return 0;
1222}
1223
1224/*
1225 * Set up search table for MIMO3
1226 */
1227static int rs_switch_to_mimo3(struct iwl_priv *priv,
1228 struct iwl_lq_sta *lq_sta,
1229 struct ieee80211_conf *conf,
1230 struct ieee80211_sta *sta,
1231 struct iwl_scale_tbl_info *tbl, int index)
1232{
1233 u16 rate_mask;
1234 s32 rate;
1235 s8 is_green = lq_sta->is_green;
1236
1237 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
1238 return -1;
1239
1240 if (((sta->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 2)
1241 == WLAN_HT_CAP_SM_PS_STATIC)
1242 return -1;
1243
1244 /* Need both Tx chains/antennas to support MIMO */
1245 if (priv->hw_params.tx_chains_num < 3)
1246 return -1;
1247
1248 IWL_DEBUG_RATE(priv, "LQ: try to switch to MIMO3\n");
1249
1250 tbl->lq_type = LQ_MIMO3;
1251 tbl->is_dup = lq_sta->is_dup;
1252 tbl->action = 0;
1253 rate_mask = lq_sta->active_mimo3_rate;
1254
1255 if (priv->current_ht_config.supported_chan_width
1256 == IWL_CHANNEL_WIDTH_40MHZ)
1257 tbl->is_fat = 1;
1258 else
1259 tbl->is_fat = 0;
1260
1261 /* FIXME: - don't toggle SGI here
1262 if (tbl->is_fat) {
1263 if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
1264 tbl->is_SGI = 1;
1265 else
1266 tbl->is_SGI = 0;
1267 } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
1268 tbl->is_SGI = 1;
1269 else
1270 tbl->is_SGI = 0;
1271 */
1272
1273 rs_set_expected_tpt_table(lq_sta, tbl);
1274
1275 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
1276
1277 IWL_DEBUG_RATE(priv, "LQ: MIMO3 best rate %d mask %X\n",
1278 rate, rate_mask);
Guy Cohen39e88502008-04-23 17:14:57 -07001279 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
Tomas Winklere1623442009-01-27 14:27:56 -08001280 IWL_DEBUG_RATE(priv, "Can't switch with index %d rate mask %x\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001281 rate, rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07001282 return -1;
Guy Cohen39e88502008-04-23 17:14:57 -07001283 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001284 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
Zhu Yib481de92007-09-25 17:54:57 -07001285
Tomas Winklere1623442009-01-27 14:27:56 -08001286 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001287 tbl->current_rate, is_green);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001288 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07001289}
1290
Ben Cahill77626352007-11-29 11:09:44 +08001291/*
1292 * Set up search table for SISO
1293 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001294static int rs_switch_to_siso(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001295 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001296 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001297 struct ieee80211_sta *sta,
Tomas Winklere227cea2008-07-18 13:53:05 +08001298 struct iwl_scale_tbl_info *tbl, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001299{
Zhu Yib481de92007-09-25 17:54:57 -07001300 u16 rate_mask;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001301 u8 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07001302 s32 rate;
1303
Luis R. Rodriguezde27e642008-12-23 15:58:44 -08001304 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
Zhu Yib481de92007-09-25 17:54:57 -07001305 return -1;
1306
Tomas Winklere1623442009-01-27 14:27:56 -08001307 IWL_DEBUG_RATE(priv, "LQ: try to switch to SISO\n");
Guy Cohen39e88502008-04-23 17:14:57 -07001308
Tomas Winklerc33104f2008-01-14 17:46:21 -08001309 tbl->is_dup = lq_sta->is_dup;
Zhu Yib481de92007-09-25 17:54:57 -07001310 tbl->lq_type = LQ_SISO;
1311 tbl->action = 0;
Guy Cohen39e88502008-04-23 17:14:57 -07001312 rate_mask = lq_sta->active_siso_rate;
Zhu Yib481de92007-09-25 17:54:57 -07001313
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001314 if (priv->current_ht_config.supported_chan_width
1315 == IWL_CHANNEL_WIDTH_40MHZ)
Zhu Yib481de92007-09-25 17:54:57 -07001316 tbl->is_fat = 1;
1317 else
1318 tbl->is_fat = 0;
1319
Guy Cohen39e88502008-04-23 17:14:57 -07001320 /* FIXME: - don't toggle SGI here
Zhu Yib481de92007-09-25 17:54:57 -07001321 if (tbl->is_fat) {
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001322 if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
Zhu Yib481de92007-09-25 17:54:57 -07001323 tbl->is_SGI = 1;
1324 else
1325 tbl->is_SGI = 0;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001326 } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
Zhu Yib481de92007-09-25 17:54:57 -07001327 tbl->is_SGI = 1;
1328 else
1329 tbl->is_SGI = 0;
Guy Cohen39e88502008-04-23 17:14:57 -07001330 */
Zhu Yib481de92007-09-25 17:54:57 -07001331
1332 if (is_green)
Guy Cohen39e88502008-04-23 17:14:57 -07001333 tbl->is_SGI = 0; /*11n spec: no SGI in SISO+Greenfield*/
Zhu Yib481de92007-09-25 17:54:57 -07001334
Guy Coheneecd6e52008-04-23 17:14:58 -07001335 rs_set_expected_tpt_table(lq_sta, tbl);
Guy Cohenf935a6d2008-04-23 17:15:02 -07001336 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
Zhu Yib481de92007-09-25 17:54:57 -07001337
Tomas Winklere1623442009-01-27 14:27:56 -08001338 IWL_DEBUG_RATE(priv, "LQ: get best rate %d mask %X\n", rate, rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07001339 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
Tomas Winklere1623442009-01-27 14:27:56 -08001340 IWL_DEBUG_RATE(priv, "can not switch with index %d rate mask %x\n",
Zhu Yib481de92007-09-25 17:54:57 -07001341 rate, rate_mask);
1342 return -1;
1343 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001344 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
Tomas Winklere1623442009-01-27 14:27:56 -08001345 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001346 tbl->current_rate, is_green);
Mohamed Abbas403ab562007-11-06 22:06:25 -08001347 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07001348}
1349
Ben Cahill77626352007-11-29 11:09:44 +08001350/*
1351 * Try to switch to new modulation mode from legacy
1352 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001353static int rs_move_legacy_other(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001354 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001355 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001356 struct ieee80211_sta *sta,
Zhu Yib481de92007-09-25 17:54:57 -07001357 int index)
1358{
Tomas Winklere227cea2008-07-18 13:53:05 +08001359 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1360 struct iwl_scale_tbl_info *search_tbl =
1361 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1362 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1363 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1364 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001365 u8 start_action = tbl->action;
Guy Cohenfde0db32008-04-21 15:42:01 -07001366 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
Guy Cohen3110bef2008-09-09 10:54:54 +08001367 u8 tx_chains_num = priv->hw_params.tx_chains_num;
Guy Cohenfde0db32008-04-21 15:42:01 -07001368 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001369
1370 for (; ;) {
1371 switch (tbl->action) {
Guy Cohen3110bef2008-09-09 10:54:54 +08001372 case IWL_LEGACY_SWITCH_ANTENNA1:
1373 case IWL_LEGACY_SWITCH_ANTENNA2:
Tomas Winklere1623442009-01-27 14:27:56 -08001374 IWL_DEBUG_RATE(priv, "LQ: Legacy toggle Antenna\n");
Zhu Yib481de92007-09-25 17:54:57 -07001375
Tomas Winklerc33104f2008-01-14 17:46:21 -08001376 lq_sta->action_counter++;
Ben Cahill77626352007-11-29 11:09:44 +08001377
Guy Cohen3110bef2008-09-09 10:54:54 +08001378 if ((tbl->action == IWL_LEGACY_SWITCH_ANTENNA1 &&
1379 tx_chains_num <= 1) ||
1380 (tbl->action == IWL_LEGACY_SWITCH_ANTENNA2 &&
1381 tx_chains_num <= 2))
1382 break;
1383
Ben Cahill77626352007-11-29 11:09:44 +08001384 /* Don't change antenna if success has been great */
Zhu Yib481de92007-09-25 17:54:57 -07001385 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1386 break;
Ben Cahill77626352007-11-29 11:09:44 +08001387
Ben Cahill77626352007-11-29 11:09:44 +08001388 /* Set up search table to try other antenna */
Zhu Yib481de92007-09-25 17:54:57 -07001389 memcpy(search_tbl, tbl, sz);
1390
Guy Cohenaade00c2008-04-23 17:14:59 -07001391 if (rs_toggle_antenna(valid_tx_ant,
1392 &search_tbl->current_rate, search_tbl)) {
Guy Cohen3110bef2008-09-09 10:54:54 +08001393 rs_set_expected_tpt_table(lq_sta, search_tbl);
Guy Cohenaade00c2008-04-23 17:14:59 -07001394 goto out;
1395 }
Guy Cohena5e8b502008-05-29 16:35:11 +08001396 break;
Zhu Yib481de92007-09-25 17:54:57 -07001397 case IWL_LEGACY_SWITCH_SISO:
Tomas Winklere1623442009-01-27 14:27:56 -08001398 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to SISO\n");
Ben Cahill77626352007-11-29 11:09:44 +08001399
1400 /* Set up search table to try SISO */
Zhu Yib481de92007-09-25 17:54:57 -07001401 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001402 search_tbl->is_SGI = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001403 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001404 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001405 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001406 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001407 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001408 }
Zhu Yib481de92007-09-25 17:54:57 -07001409
1410 break;
Guy Cohen3110bef2008-09-09 10:54:54 +08001411 case IWL_LEGACY_SWITCH_MIMO2_AB:
1412 case IWL_LEGACY_SWITCH_MIMO2_AC:
1413 case IWL_LEGACY_SWITCH_MIMO2_BC:
Tomas Winklere1623442009-01-27 14:27:56 -08001414 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to MIMO2\n");
Ben Cahill77626352007-11-29 11:09:44 +08001415
1416 /* Set up search table to try MIMO */
Zhu Yib481de92007-09-25 17:54:57 -07001417 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001418 search_tbl->is_SGI = 0;
Guy Cohen3110bef2008-09-09 10:54:54 +08001419
1420 if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AB)
1421 search_tbl->ant_type = ANT_AB;
1422 else if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AC)
1423 search_tbl->ant_type = ANT_AC;
1424 else
1425 search_tbl->ant_type = ANT_BC;
1426
1427 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1428 break;
1429
Guy Cohenfde0db32008-04-21 15:42:01 -07001430 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001431 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001432 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001433 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001434 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001435 }
Zhu Yib481de92007-09-25 17:54:57 -07001436 break;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001437
1438 case IWL_LEGACY_SWITCH_MIMO3_ABC:
1439 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to MIMO3\n");
1440
1441 /* Set up search table to try MIMO3 */
1442 memcpy(search_tbl, tbl, sz);
1443 search_tbl->is_SGI = 0;
1444
1445 search_tbl->ant_type = ANT_ABC;
1446
1447 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1448 break;
1449
1450 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1451 search_tbl, index);
1452 if (!ret) {
1453 lq_sta->action_counter = 0;
1454 goto out;
1455 }
1456 break;
Zhu Yib481de92007-09-25 17:54:57 -07001457 }
1458 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001459 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001460 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
Zhu Yib481de92007-09-25 17:54:57 -07001461
1462 if (tbl->action == start_action)
1463 break;
1464
1465 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001466 search_tbl->lq_type = LQ_NONE;
Zhu Yib481de92007-09-25 17:54:57 -07001467 return 0;
1468
Guy Cohen3110bef2008-09-09 10:54:54 +08001469out:
1470 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001471 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001472 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001473 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
Zhu Yib481de92007-09-25 17:54:57 -07001474 return 0;
1475
1476}
1477
Ben Cahill77626352007-11-29 11:09:44 +08001478/*
1479 * Try to switch to new modulation mode from SISO
1480 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001481static int rs_move_siso_to_other(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001482 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001483 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001484 struct ieee80211_sta *sta, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001485{
Tomas Winklerc33104f2008-01-14 17:46:21 -08001486 u8 is_green = lq_sta->is_green;
Tomas Winklere227cea2008-07-18 13:53:05 +08001487 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1488 struct iwl_scale_tbl_info *search_tbl =
1489 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1490 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1491 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1492 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001493 u8 start_action = tbl->action;
Guy Cohenfde0db32008-04-21 15:42:01 -07001494 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
Guy Cohen3110bef2008-09-09 10:54:54 +08001495 u8 tx_chains_num = priv->hw_params.tx_chains_num;
Guy Cohenfde0db32008-04-21 15:42:01 -07001496 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001497
1498 for (;;) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001499 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001500 switch (tbl->action) {
Guy Cohen3110bef2008-09-09 10:54:54 +08001501 case IWL_SISO_SWITCH_ANTENNA1:
1502 case IWL_SISO_SWITCH_ANTENNA2:
Tomas Winklere1623442009-01-27 14:27:56 -08001503 IWL_DEBUG_RATE(priv, "LQ: SISO toggle Antenna\n");
Guy Cohen3110bef2008-09-09 10:54:54 +08001504
1505 if ((tbl->action == IWL_SISO_SWITCH_ANTENNA1 &&
1506 tx_chains_num <= 1) ||
1507 (tbl->action == IWL_SISO_SWITCH_ANTENNA2 &&
1508 tx_chains_num <= 2))
1509 break;
1510
Zhu Yib481de92007-09-25 17:54:57 -07001511 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1512 break;
Zhu Yib481de92007-09-25 17:54:57 -07001513
1514 memcpy(search_tbl, tbl, sz);
Guy Cohenaade00c2008-04-23 17:14:59 -07001515 if (rs_toggle_antenna(valid_tx_ant,
Guy Cohen3110bef2008-09-09 10:54:54 +08001516 &search_tbl->current_rate, search_tbl))
Guy Cohenaade00c2008-04-23 17:14:59 -07001517 goto out;
Guy Cohena5e8b502008-05-29 16:35:11 +08001518 break;
Guy Cohen3110bef2008-09-09 10:54:54 +08001519 case IWL_SISO_SWITCH_MIMO2_AB:
1520 case IWL_SISO_SWITCH_MIMO2_AC:
1521 case IWL_SISO_SWITCH_MIMO2_BC:
Tomas Winklere1623442009-01-27 14:27:56 -08001522 IWL_DEBUG_RATE(priv, "LQ: SISO switch to MIMO2\n");
Zhu Yib481de92007-09-25 17:54:57 -07001523 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001524 search_tbl->is_SGI = 0;
Guy Cohen3110bef2008-09-09 10:54:54 +08001525
1526 if (tbl->action == IWL_SISO_SWITCH_MIMO2_AB)
1527 search_tbl->ant_type = ANT_AB;
1528 else if (tbl->action == IWL_SISO_SWITCH_MIMO2_AC)
1529 search_tbl->ant_type = ANT_AC;
1530 else
1531 search_tbl->ant_type = ANT_BC;
1532
1533 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1534 break;
1535
Guy Cohenfde0db32008-04-21 15:42:01 -07001536 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001537 search_tbl, index);
Guy Cohen3110bef2008-09-09 10:54:54 +08001538 if (!ret)
Zhu Yib481de92007-09-25 17:54:57 -07001539 goto out;
1540 break;
1541 case IWL_SISO_SWITCH_GI:
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001542 if (!tbl->is_fat &&
1543 !(priv->current_ht_config.sgf &
1544 HT_SHORT_GI_20MHZ))
1545 break;
1546 if (tbl->is_fat &&
1547 !(priv->current_ht_config.sgf &
1548 HT_SHORT_GI_40MHZ))
1549 break;
1550
Tomas Winklere1623442009-01-27 14:27:56 -08001551 IWL_DEBUG_RATE(priv, "LQ: SISO toggle SGI/NGI\n");
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001552
Zhu Yib481de92007-09-25 17:54:57 -07001553 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001554 if (is_green) {
1555 if (!tbl->is_SGI)
1556 break;
1557 else
Winkler, Tomas15b16872008-12-19 10:37:33 +08001558 IWL_ERR(priv,
1559 "SGI was set in GF+SISO\n");
Zhu Yib481de92007-09-25 17:54:57 -07001560 }
Guy Cohen39e88502008-04-23 17:14:57 -07001561 search_tbl->is_SGI = !tbl->is_SGI;
Guy Coheneecd6e52008-04-23 17:14:58 -07001562 rs_set_expected_tpt_table(lq_sta, search_tbl);
Guy Cohen39e88502008-04-23 17:14:57 -07001563 if (tbl->is_SGI) {
1564 s32 tpt = lq_sta->last_tpt / 100;
1565 if (tpt >= search_tbl->expected_tpt[index])
1566 break;
1567 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001568 search_tbl->current_rate =
1569 rate_n_flags_from_tbl(priv, search_tbl,
1570 index, is_green);
Zhu Yib481de92007-09-25 17:54:57 -07001571 goto out;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001572 case IWL_SISO_SWITCH_MIMO3_ABC:
1573 IWL_DEBUG_RATE(priv, "LQ: SISO switch to MIMO3\n");
1574 memcpy(search_tbl, tbl, sz);
1575 search_tbl->is_SGI = 0;
1576 search_tbl->ant_type = ANT_ABC;
1577
1578 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1579 break;
1580
1581 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1582 search_tbl, index);
1583 if (!ret)
1584 goto out;
1585 break;
Zhu Yib481de92007-09-25 17:54:57 -07001586 }
1587 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001588 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001589 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
Zhu Yib481de92007-09-25 17:54:57 -07001590
1591 if (tbl->action == start_action)
1592 break;
1593 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001594 search_tbl->lq_type = LQ_NONE;
Zhu Yib481de92007-09-25 17:54:57 -07001595 return 0;
1596
1597 out:
Guy Cohen3110bef2008-09-09 10:54:54 +08001598 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001599 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001600 if (tbl->action > IWL_SISO_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001601 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
Zhu Yib481de92007-09-25 17:54:57 -07001602 return 0;
1603}
1604
Ben Cahill77626352007-11-29 11:09:44 +08001605/*
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001606 * Try to switch to new modulation mode from MIMO2
Ben Cahill77626352007-11-29 11:09:44 +08001607 */
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001608static int rs_move_mimo2_to_other(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001609 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001610 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001611 struct ieee80211_sta *sta, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001612{
Tomas Winklerc33104f2008-01-14 17:46:21 -08001613 s8 is_green = lq_sta->is_green;
Tomas Winklere227cea2008-07-18 13:53:05 +08001614 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1615 struct iwl_scale_tbl_info *search_tbl =
1616 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Guy Cohen3110bef2008-09-09 10:54:54 +08001617 struct iwl_rate_scale_data *window = &(tbl->win[index]);
Tomas Winklere227cea2008-07-18 13:53:05 +08001618 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1619 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001620 u8 start_action = tbl->action;
Guy Cohen3110bef2008-09-09 10:54:54 +08001621 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1622 u8 tx_chains_num = priv->hw_params.tx_chains_num;
Guy Cohenaade00c2008-04-23 17:14:59 -07001623 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001624
1625 for (;;) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001626 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001627 switch (tbl->action) {
Guy Cohen3110bef2008-09-09 10:54:54 +08001628 case IWL_MIMO2_SWITCH_ANTENNA1:
1629 case IWL_MIMO2_SWITCH_ANTENNA2:
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001630 IWL_DEBUG_RATE(priv, "LQ: MIMO2 toggle Antennas\n");
Guy Cohen3110bef2008-09-09 10:54:54 +08001631
1632 if (tx_chains_num <= 2)
1633 break;
1634
1635 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1636 break;
1637
1638 memcpy(search_tbl, tbl, sz);
1639 if (rs_toggle_antenna(valid_tx_ant,
1640 &search_tbl->current_rate, search_tbl))
1641 goto out;
1642 break;
1643 case IWL_MIMO2_SWITCH_SISO_A:
1644 case IWL_MIMO2_SWITCH_SISO_B:
1645 case IWL_MIMO2_SWITCH_SISO_C:
Tomas Winklere1623442009-01-27 14:27:56 -08001646 IWL_DEBUG_RATE(priv, "LQ: MIMO2 switch to SISO\n");
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001647
Ben Cahill77626352007-11-29 11:09:44 +08001648 /* Set up new search table for SISO */
Zhu Yib481de92007-09-25 17:54:57 -07001649 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001650
Guy Cohen3110bef2008-09-09 10:54:54 +08001651 if (tbl->action == IWL_MIMO2_SWITCH_SISO_A)
Guy Cohenfde0db32008-04-21 15:42:01 -07001652 search_tbl->ant_type = ANT_A;
Guy Cohen3110bef2008-09-09 10:54:54 +08001653 else if (tbl->action == IWL_MIMO2_SWITCH_SISO_B)
Guy Cohenfde0db32008-04-21 15:42:01 -07001654 search_tbl->ant_type = ANT_B;
Guy Cohen3110bef2008-09-09 10:54:54 +08001655 else
1656 search_tbl->ant_type = ANT_C;
1657
1658 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1659 break;
Zhu Yib481de92007-09-25 17:54:57 -07001660
Tomas Winklerc33104f2008-01-14 17:46:21 -08001661 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001662 search_tbl, index);
Guy Cohen3110bef2008-09-09 10:54:54 +08001663 if (!ret)
Zhu Yib481de92007-09-25 17:54:57 -07001664 goto out;
Guy Cohen3110bef2008-09-09 10:54:54 +08001665
Zhu Yib481de92007-09-25 17:54:57 -07001666 break;
1667
Guy Cohen3110bef2008-09-09 10:54:54 +08001668 case IWL_MIMO2_SWITCH_GI:
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001669 if (!tbl->is_fat &&
1670 !(priv->current_ht_config.sgf &
1671 HT_SHORT_GI_20MHZ))
1672 break;
1673 if (tbl->is_fat &&
1674 !(priv->current_ht_config.sgf &
1675 HT_SHORT_GI_40MHZ))
1676 break;
1677
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001678 IWL_DEBUG_RATE(priv, "LQ: MIMO2 toggle SGI/NGI\n");
1679
1680 /* Set up new search table for MIMO2 */
1681 memcpy(search_tbl, tbl, sz);
1682 search_tbl->is_SGI = !tbl->is_SGI;
1683 rs_set_expected_tpt_table(lq_sta, search_tbl);
1684 /*
1685 * If active table already uses the fastest possible
1686 * modulation (dual stream with short guard interval),
1687 * and it's working well, there's no need to look
1688 * for a better type of modulation!
1689 */
1690 if (tbl->is_SGI) {
1691 s32 tpt = lq_sta->last_tpt / 100;
1692 if (tpt >= search_tbl->expected_tpt[index])
1693 break;
1694 }
1695 search_tbl->current_rate =
1696 rate_n_flags_from_tbl(priv, search_tbl,
1697 index, is_green);
1698 goto out;
1699
1700 case IWL_MIMO2_SWITCH_MIMO3_ABC:
1701 IWL_DEBUG_RATE(priv, "LQ: MIMO2 switch to MIMO3\n");
1702 memcpy(search_tbl, tbl, sz);
1703 search_tbl->is_SGI = 0;
1704 search_tbl->ant_type = ANT_ABC;
1705
1706 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1707 break;
1708
1709 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1710 search_tbl, index);
1711 if (!ret)
1712 goto out;
1713
1714 break;
1715 }
1716 tbl->action++;
1717 if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
1718 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
1719
1720 if (tbl->action == start_action)
1721 break;
1722 }
1723 search_tbl->lq_type = LQ_NONE;
1724 return 0;
1725 out:
1726 lq_sta->search_better_tbl = 1;
1727 tbl->action++;
1728 if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
1729 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
1730 return 0;
1731
1732}
1733
1734/*
1735 * Try to switch to new modulation mode from MIMO3
1736 */
1737static int rs_move_mimo3_to_other(struct iwl_priv *priv,
1738 struct iwl_lq_sta *lq_sta,
1739 struct ieee80211_conf *conf,
1740 struct ieee80211_sta *sta, int index)
1741{
1742 s8 is_green = lq_sta->is_green;
1743 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1744 struct iwl_scale_tbl_info *search_tbl =
1745 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1746 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1747 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1748 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1749 u8 start_action = tbl->action;
1750 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1751 u8 tx_chains_num = priv->hw_params.tx_chains_num;
1752 int ret;
1753
1754 for (;;) {
1755 lq_sta->action_counter++;
1756 switch (tbl->action) {
1757 case IWL_MIMO3_SWITCH_ANTENNA1:
1758 case IWL_MIMO3_SWITCH_ANTENNA2:
1759 IWL_DEBUG_RATE(priv, "LQ: MIMO3 toggle Antennas\n");
1760
1761 if (tx_chains_num <= 3)
1762 break;
1763
1764 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1765 break;
1766
1767 memcpy(search_tbl, tbl, sz);
1768 if (rs_toggle_antenna(valid_tx_ant,
1769 &search_tbl->current_rate, search_tbl))
1770 goto out;
1771 break;
1772 case IWL_MIMO3_SWITCH_SISO_A:
1773 case IWL_MIMO3_SWITCH_SISO_B:
1774 case IWL_MIMO3_SWITCH_SISO_C:
1775 IWL_DEBUG_RATE(priv, "LQ: MIMO3 switch to SISO\n");
1776
1777 /* Set up new search table for SISO */
1778 memcpy(search_tbl, tbl, sz);
1779
1780 if (tbl->action == IWL_MIMO3_SWITCH_SISO_A)
1781 search_tbl->ant_type = ANT_A;
1782 else if (tbl->action == IWL_MIMO3_SWITCH_SISO_B)
1783 search_tbl->ant_type = ANT_B;
1784 else
1785 search_tbl->ant_type = ANT_C;
1786
1787 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1788 break;
1789
1790 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
1791 search_tbl, index);
1792 if (!ret)
1793 goto out;
1794
1795 break;
1796
1797 case IWL_MIMO3_SWITCH_MIMO2_AB:
1798 case IWL_MIMO3_SWITCH_MIMO2_AC:
1799 case IWL_MIMO3_SWITCH_MIMO2_BC:
1800 IWL_DEBUG_RATE(priv, "LQ: MIMO3 switch to MIMO2\n");
1801
1802 memcpy(search_tbl, tbl, sz);
1803 search_tbl->is_SGI = 0;
1804 if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AB)
1805 search_tbl->ant_type = ANT_AB;
1806 else if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AC)
1807 search_tbl->ant_type = ANT_AC;
1808 else
1809 search_tbl->ant_type = ANT_BC;
1810
1811 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1812 break;
1813
1814 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
1815 search_tbl, index);
1816 if (!ret)
1817 goto out;
1818
1819 break;
1820
1821 case IWL_MIMO3_SWITCH_GI:
1822 if (!tbl->is_fat &&
1823 !(priv->current_ht_config.sgf &
1824 HT_SHORT_GI_20MHZ))
1825 break;
1826 if (tbl->is_fat &&
1827 !(priv->current_ht_config.sgf &
1828 HT_SHORT_GI_40MHZ))
1829 break;
1830
1831 IWL_DEBUG_RATE(priv, "LQ: MIMO3 toggle SGI/NGI\n");
Ben Cahill77626352007-11-29 11:09:44 +08001832
1833 /* Set up new search table for MIMO */
Zhu Yib481de92007-09-25 17:54:57 -07001834 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001835 search_tbl->is_SGI = !tbl->is_SGI;
Guy Coheneecd6e52008-04-23 17:14:58 -07001836 rs_set_expected_tpt_table(lq_sta, search_tbl);
Ben Cahill77626352007-11-29 11:09:44 +08001837 /*
1838 * If active table already uses the fastest possible
1839 * modulation (dual stream with short guard interval),
1840 * and it's working well, there's no need to look
1841 * for a better type of modulation!
1842 */
Guy Cohen39e88502008-04-23 17:14:57 -07001843 if (tbl->is_SGI) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001844 s32 tpt = lq_sta->last_tpt / 100;
Guy Cohen39e88502008-04-23 17:14:57 -07001845 if (tpt >= search_tbl->expected_tpt[index])
1846 break;
Zhu Yib481de92007-09-25 17:54:57 -07001847 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001848 search_tbl->current_rate =
1849 rate_n_flags_from_tbl(priv, search_tbl,
1850 index, is_green);
Zhu Yib481de92007-09-25 17:54:57 -07001851 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07001852 }
1853 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001854 if (tbl->action > IWL_MIMO3_SWITCH_GI)
1855 tbl->action = IWL_MIMO3_SWITCH_ANTENNA1;
Zhu Yib481de92007-09-25 17:54:57 -07001856
1857 if (tbl->action == start_action)
1858 break;
1859 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001860 search_tbl->lq_type = LQ_NONE;
Zhu Yib481de92007-09-25 17:54:57 -07001861 return 0;
1862 out:
Guy Cohen3110bef2008-09-09 10:54:54 +08001863 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001864 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001865 if (tbl->action > IWL_MIMO3_SWITCH_GI)
1866 tbl->action = IWL_MIMO3_SWITCH_ANTENNA1;
Zhu Yib481de92007-09-25 17:54:57 -07001867 return 0;
1868
1869}
1870
Ben Cahill77626352007-11-29 11:09:44 +08001871/*
1872 * Check whether we should continue using same modulation mode, or
1873 * begin search for a new mode, based on:
1874 * 1) # tx successes or failures while using this mode
1875 * 2) # times calling this function
1876 * 3) elapsed time in this mode (not used, for now)
1877 */
Tomas Winklere227cea2008-07-18 13:53:05 +08001878static void rs_stay_in_table(struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07001879{
Tomas Winklere227cea2008-07-18 13:53:05 +08001880 struct iwl_scale_tbl_info *tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001881 int i;
1882 int active_tbl;
1883 int flush_interval_passed = 0;
Ester Kummerbf403db2008-05-05 10:22:40 +08001884 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07001885
Ester Kummerbf403db2008-05-05 10:22:40 +08001886 priv = lq_sta->drv;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001887 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001888
Tomas Winklerc33104f2008-01-14 17:46:21 -08001889 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07001890
Ben Cahill77626352007-11-29 11:09:44 +08001891 /* If we've been disallowing search, see if we should now allow it */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001892 if (lq_sta->stay_in_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07001893
Ben Cahill77626352007-11-29 11:09:44 +08001894 /* Elapsed time using current modulation mode */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001895 if (lq_sta->flush_timer)
Zhu Yib481de92007-09-25 17:54:57 -07001896 flush_interval_passed =
1897 time_after(jiffies,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001898 (unsigned long)(lq_sta->flush_timer +
Zhu Yib481de92007-09-25 17:54:57 -07001899 IWL_RATE_SCALE_FLUSH_INTVL));
1900
Ben Cahill77626352007-11-29 11:09:44 +08001901 /*
1902 * Check if we should allow search for new modulation mode.
1903 * If many frames have failed or succeeded, or we've used
1904 * this same modulation for a long time, allow search, and
1905 * reset history stats that keep track of whether we should
1906 * allow a new search. Also (below) reset all bitmaps and
1907 * stats in active history.
1908 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001909 if ((lq_sta->total_failed > lq_sta->max_failure_limit) ||
1910 (lq_sta->total_success > lq_sta->max_success_limit) ||
1911 ((!lq_sta->search_better_tbl) && (lq_sta->flush_timer)
Zhu Yib481de92007-09-25 17:54:57 -07001912 && (flush_interval_passed))) {
Tomas Winklere1623442009-01-27 14:27:56 -08001913 IWL_DEBUG_RATE(priv, "LQ: stay is expired %d %d %d\n:",
Tomas Winklerc33104f2008-01-14 17:46:21 -08001914 lq_sta->total_failed,
1915 lq_sta->total_success,
Zhu Yib481de92007-09-25 17:54:57 -07001916 flush_interval_passed);
Ben Cahill77626352007-11-29 11:09:44 +08001917
1918 /* Allow search for new mode */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001919 lq_sta->stay_in_tbl = 0; /* only place reset */
1920 lq_sta->total_failed = 0;
1921 lq_sta->total_success = 0;
1922 lq_sta->flush_timer = 0;
Ben Cahill77626352007-11-29 11:09:44 +08001923
1924 /*
1925 * Else if we've used this modulation mode enough repetitions
1926 * (regardless of elapsed time or success/failure), reset
1927 * history bitmaps and rate-specific stats for all rates in
1928 * active table.
1929 */
Mohamed Abbas403ab562007-11-06 22:06:25 -08001930 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001931 lq_sta->table_count++;
1932 if (lq_sta->table_count >=
1933 lq_sta->table_count_limit) {
1934 lq_sta->table_count = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001935
Tomas Winklere1623442009-01-27 14:27:56 -08001936 IWL_DEBUG_RATE(priv, "LQ: stay in table clear win\n");
Zhu Yib481de92007-09-25 17:54:57 -07001937 for (i = 0; i < IWL_RATE_COUNT; i++)
1938 rs_rate_scale_clear_window(
1939 &(tbl->win[i]));
1940 }
1941 }
1942
Ben Cahill77626352007-11-29 11:09:44 +08001943 /* If transitioning to allow "search", reset all history
1944 * bitmaps and stats in active table (this will become the new
1945 * "search" table). */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001946 if (!lq_sta->stay_in_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07001947 for (i = 0; i < IWL_RATE_COUNT; i++)
1948 rs_rate_scale_clear_window(&(tbl->win[i]));
1949 }
1950 }
1951}
1952
Ben Cahill77626352007-11-29 11:09:44 +08001953/*
1954 * Do rate scaling and search for new modulation mode.
1955 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001956static void rs_rate_scale_perform(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001957 struct ieee80211_hdr *hdr,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001958 struct ieee80211_sta *sta,
1959 struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07001960{
Johannes Berg4b7679a2008-09-18 18:14:18 +02001961 struct ieee80211_hw *hw = priv->hw;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001962 struct ieee80211_conf *conf = &hw->conf;
Zhu Yib481de92007-09-25 17:54:57 -07001963 int low = IWL_RATE_INVALID;
1964 int high = IWL_RATE_INVALID;
1965 int index;
1966 int i;
Tomas Winklere227cea2008-07-18 13:53:05 +08001967 struct iwl_rate_scale_data *window = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001968 int current_tpt = IWL_INVALID_VALUE;
1969 int low_tpt = IWL_INVALID_VALUE;
1970 int high_tpt = IWL_INVALID_VALUE;
1971 u32 fail_count;
1972 s8 scale_action = 0;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07001973 u16 rate_mask;
Zhu Yib481de92007-09-25 17:54:57 -07001974 u8 update_lq = 0;
Tomas Winklere227cea2008-07-18 13:53:05 +08001975 struct iwl_scale_tbl_info *tbl, *tbl1;
Zhu Yib481de92007-09-25 17:54:57 -07001976 u16 rate_scale_index_msk = 0;
Guy Cohen39e88502008-04-23 17:14:57 -07001977 u32 rate;
Zhu Yib481de92007-09-25 17:54:57 -07001978 u8 is_green = 0;
1979 u8 active_tbl = 0;
1980 u8 done_search = 0;
1981 u16 high_low;
Guy Cohena5e8b502008-05-29 16:35:11 +08001982 s32 sr;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001983 u8 tid = MAX_TID_COUNT;
Wey-Yi Guy86b47662009-03-10 14:35:09 -07001984 struct iwl_tid_data *tid_data;
Zhu Yib481de92007-09-25 17:54:57 -07001985
Tomas Winklere1623442009-01-27 14:27:56 -08001986 IWL_DEBUG_RATE(priv, "rate scale calculate new rate for skb\n");
Zhu Yib481de92007-09-25 17:54:57 -07001987
Tomas Winkler417f1142008-11-12 13:14:06 -08001988 /* Send management frames and broadcast/multicast data using
1989 * lowest rate. */
1990 /* TODO: this could probably be improved.. */
1991 if (!ieee80211_is_data(hdr->frame_control) ||
1992 is_multicast_ether_addr(hdr->addr1))
Zhu Yib481de92007-09-25 17:54:57 -07001993 return;
Zhu Yib481de92007-09-25 17:54:57 -07001994
Johannes Berg4b7679a2008-09-18 18:14:18 +02001995 if (!sta || !lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07001996 return;
1997
Johannes Berg4b7679a2008-09-18 18:14:18 +02001998 lq_sta->supp_rates = sta->supp_rates[lq_sta->band];
Zhu Yib481de92007-09-25 17:54:57 -07001999
Ron Rindjunskyfaa29712008-06-12 09:46:58 +08002000 tid = rs_tl_add_packet(lq_sta, hdr);
2001
Ben Cahill77626352007-11-29 11:09:44 +08002002 /*
2003 * Select rate-scale / modulation-mode table to work with in
2004 * the rest of this function: "search" if searching for better
2005 * modulation mode, or "active" if doing rate scaling within a mode.
2006 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002007 if (!lq_sta->search_better_tbl)
2008 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002009 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002010 active_tbl = 1 - lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002011
Tomas Winklerc33104f2008-01-14 17:46:21 -08002012 tbl = &(lq_sta->lq_info[active_tbl]);
2013 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07002014
Ben Cahill77626352007-11-29 11:09:44 +08002015 /* current tx rate */
Johannes Bergb7e35002008-09-11 02:22:58 +02002016 index = lq_sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002017
Tomas Winklere1623442009-01-27 14:27:56 -08002018 IWL_DEBUG_RATE(priv, "Rate scale index %d for type %d\n", index,
Zhu Yib481de92007-09-25 17:54:57 -07002019 tbl->lq_type);
2020
Ben Cahill77626352007-11-29 11:09:44 +08002021 /* rates available for this association, and for modulation mode */
Guy Coheneecd6e52008-04-23 17:14:58 -07002022 rate_mask = rs_get_supported_rates(lq_sta, hdr, tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -07002023
Tomas Winklere1623442009-01-27 14:27:56 -08002024 IWL_DEBUG_RATE(priv, "mask 0x%04X \n", rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07002025
2026 /* mask with station rate restriction */
2027 if (is_legacy(tbl->lq_type)) {
Johannes Berg8318d782008-01-24 19:38:38 +01002028 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Ben Cahill77626352007-11-29 11:09:44 +08002029 /* supp_rates has no CCK bits in A mode */
Zhu Yib481de92007-09-25 17:54:57 -07002030 rate_scale_index_msk = (u16) (rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -08002031 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
Zhu Yib481de92007-09-25 17:54:57 -07002032 else
2033 rate_scale_index_msk = (u16) (rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -08002034 lq_sta->supp_rates);
Zhu Yib481de92007-09-25 17:54:57 -07002035
2036 } else
2037 rate_scale_index_msk = rate_mask;
2038
2039 if (!rate_scale_index_msk)
2040 rate_scale_index_msk = rate_mask;
2041
Guy Cohen07bc28e2008-04-23 17:15:03 -07002042 if (!((1 << index) & rate_scale_index_msk)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002043 IWL_ERR(priv, "Current Rate is not valid\n");
Guy Cohen07bc28e2008-04-23 17:15:03 -07002044 return;
Zhu Yib481de92007-09-25 17:54:57 -07002045 }
2046
Ben Cahill77626352007-11-29 11:09:44 +08002047 /* Get expected throughput table and history window for current rate */
Guy Cohen07bc28e2008-04-23 17:15:03 -07002048 if (!tbl->expected_tpt) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002049 IWL_ERR(priv, "tbl->expected_tpt is NULL\n");
Guy Cohen07bc28e2008-04-23 17:15:03 -07002050 return;
2051 }
Zhu Yib481de92007-09-25 17:54:57 -07002052
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002053 /* force user max rate if set by user */
2054 if ((lq_sta->max_rate_idx != -1) &&
2055 (lq_sta->max_rate_idx < index)) {
2056 index = lq_sta->max_rate_idx;
2057 update_lq = 1;
2058 window = &(tbl->win[index]);
2059 goto lq_update;
2060 }
2061
Zhu Yib481de92007-09-25 17:54:57 -07002062 window = &(tbl->win[index]);
2063
Ben Cahill77626352007-11-29 11:09:44 +08002064 /*
2065 * If there is not enough history to calculate actual average
2066 * throughput, keep analyzing results of more tx frames, without
2067 * changing rate or mode (bypass most of the rest of this function).
2068 * Set up new rate table in uCode only if old rate is not supported
2069 * in current association (use new rate found above).
2070 */
Zhu Yib481de92007-09-25 17:54:57 -07002071 fail_count = window->counter - window->success_counter;
Guy Cohen07bc28e2008-04-23 17:15:03 -07002072 if ((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
2073 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002074 IWL_DEBUG_RATE(priv, "LQ: still below TH. succ=%d total=%d "
Zhu Yib481de92007-09-25 17:54:57 -07002075 "for index %d\n",
2076 window->success_counter, window->counter, index);
Ben Cahill77626352007-11-29 11:09:44 +08002077
2078 /* Can't calculate this yet; not enough history */
Zhu Yib481de92007-09-25 17:54:57 -07002079 window->average_tpt = IWL_INVALID_VALUE;
Ben Cahill77626352007-11-29 11:09:44 +08002080
2081 /* Should we stay with this modulation mode,
2082 * or search for a new one? */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002083 rs_stay_in_table(lq_sta);
Ben Cahill77626352007-11-29 11:09:44 +08002084
Zhu Yib481de92007-09-25 17:54:57 -07002085 goto out;
Guy Cohen3110bef2008-09-09 10:54:54 +08002086 }
Zhu Yib481de92007-09-25 17:54:57 -07002087
Ben Cahill77626352007-11-29 11:09:44 +08002088 /* Else we have enough samples; calculate estimate of
2089 * actual average throughput */
Guy Cohen3110bef2008-09-09 10:54:54 +08002090
2091 BUG_ON(window->average_tpt != ((window->success_ratio *
2092 tbl->expected_tpt[index] + 64) / 128));
Zhu Yib481de92007-09-25 17:54:57 -07002093
Ben Cahill77626352007-11-29 11:09:44 +08002094 /* If we are searching for better modulation mode, check success. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002095 if (lq_sta->search_better_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07002096
Ben Cahill77626352007-11-29 11:09:44 +08002097 /* If good success, continue using the "search" mode;
2098 * no need to send new link quality command, since we're
2099 * continuing to use the setup that we've been trying. */
Guy Cohen07bc28e2008-04-23 17:15:03 -07002100 if (window->average_tpt > lq_sta->last_tpt) {
2101
Tomas Winklere1623442009-01-27 14:27:56 -08002102 IWL_DEBUG_RATE(priv, "LQ: SWITCHING TO NEW TABLE "
Guy Cohen07bc28e2008-04-23 17:15:03 -07002103 "suc=%d cur-tpt=%d old-tpt=%d\n",
2104 window->success_ratio,
2105 window->average_tpt,
2106 lq_sta->last_tpt);
2107
2108 if (!is_legacy(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08002109 lq_sta->enable_counter = 1;
Guy Cohen07bc28e2008-04-23 17:15:03 -07002110
Ben Cahill77626352007-11-29 11:09:44 +08002111 /* Swap tables; "search" becomes "active" */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002112 lq_sta->active_tbl = active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002113 current_tpt = window->average_tpt;
Ben Cahill77626352007-11-29 11:09:44 +08002114
2115 /* Else poor success; go back to mode in "active" table */
Zhu Yib481de92007-09-25 17:54:57 -07002116 } else {
Guy Cohen07bc28e2008-04-23 17:15:03 -07002117
Tomas Winklere1623442009-01-27 14:27:56 -08002118 IWL_DEBUG_RATE(priv, "LQ: GOING BACK TO THE OLD TABLE "
Guy Cohen07bc28e2008-04-23 17:15:03 -07002119 "suc=%d cur-tpt=%d old-tpt=%d\n",
2120 window->success_ratio,
2121 window->average_tpt,
2122 lq_sta->last_tpt);
2123
Ben Cahill77626352007-11-29 11:09:44 +08002124 /* Nullify "search" table */
Zhu Yib481de92007-09-25 17:54:57 -07002125 tbl->lq_type = LQ_NONE;
Ben Cahill77626352007-11-29 11:09:44 +08002126
2127 /* Revert to "active" table */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002128 active_tbl = lq_sta->active_tbl;
2129 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07002130
Ben Cahill77626352007-11-29 11:09:44 +08002131 /* Revert to "active" rate and throughput info */
Tomas Winklere7d326ac2008-06-12 09:47:11 +08002132 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
Tomas Winklerc33104f2008-01-14 17:46:21 -08002133 current_tpt = lq_sta->last_tpt;
Ben Cahill77626352007-11-29 11:09:44 +08002134
2135 /* Need to set up a new rate table in uCode */
2136 update_lq = 1;
Zhu Yib481de92007-09-25 17:54:57 -07002137 }
Ben Cahill77626352007-11-29 11:09:44 +08002138
2139 /* Either way, we've made a decision; modulation mode
2140 * search is done, allow rate adjustment next time. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002141 lq_sta->search_better_tbl = 0;
Ben Cahill77626352007-11-29 11:09:44 +08002142 done_search = 1; /* Don't switch modes below! */
Zhu Yib481de92007-09-25 17:54:57 -07002143 goto lq_update;
2144 }
2145
Ben Cahill77626352007-11-29 11:09:44 +08002146 /* (Else) not in search of better modulation mode, try for better
2147 * starting rate, while staying in this mode. */
Ester Kummerbf403db2008-05-05 10:22:40 +08002148 high_low = rs_get_adjacent_rate(priv, index, rate_scale_index_msk,
Zhu Yib481de92007-09-25 17:54:57 -07002149 tbl->lq_type);
2150 low = high_low & 0xff;
2151 high = (high_low >> 8) & 0xff;
2152
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002153 /* If user set max rate, dont allow higher than user constrain */
2154 if ((lq_sta->max_rate_idx != -1) &&
2155 (lq_sta->max_rate_idx < high))
2156 high = IWL_RATE_INVALID;
2157
Guy Cohena5e8b502008-05-29 16:35:11 +08002158 sr = window->success_ratio;
2159
Ben Cahill77626352007-11-29 11:09:44 +08002160 /* Collect measured throughputs for current and adjacent rates */
Zhu Yib481de92007-09-25 17:54:57 -07002161 current_tpt = window->average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07002162 if (low != IWL_RATE_INVALID)
2163 low_tpt = tbl->win[low].average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07002164 if (high != IWL_RATE_INVALID)
2165 high_tpt = tbl->win[high].average_tpt;
2166
Guy Cohena5e8b502008-05-29 16:35:11 +08002167 scale_action = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002168
Ben Cahill77626352007-11-29 11:09:44 +08002169 /* Too many failures, decrease rate */
Guy Cohena5e8b502008-05-29 16:35:11 +08002170 if ((sr <= IWL_RATE_DECREASE_TH) || (current_tpt == 0)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002171 IWL_DEBUG_RATE(priv, "decrease rate because of low success_ratio\n");
Zhu Yib481de92007-09-25 17:54:57 -07002172 scale_action = -1;
Ben Cahill77626352007-11-29 11:09:44 +08002173
2174 /* No throughput measured yet for adjacent rates; try increase. */
Zhu Yib481de92007-09-25 17:54:57 -07002175 } else if ((low_tpt == IWL_INVALID_VALUE) &&
Guy Cohena5e8b502008-05-29 16:35:11 +08002176 (high_tpt == IWL_INVALID_VALUE)) {
2177
2178 if (high != IWL_RATE_INVALID && sr >= IWL_RATE_INCREASE_TH)
2179 scale_action = 1;
2180 else if (low != IWL_RATE_INVALID)
Wey-Yi Guy8fe72312009-03-10 14:35:10 -07002181 scale_action = 0;
Guy Cohena5e8b502008-05-29 16:35:11 +08002182 }
Ben Cahill77626352007-11-29 11:09:44 +08002183
2184 /* Both adjacent throughputs are measured, but neither one has better
2185 * throughput; we're using the best rate, don't change it! */
Zhu Yib481de92007-09-25 17:54:57 -07002186 else if ((low_tpt != IWL_INVALID_VALUE) &&
2187 (high_tpt != IWL_INVALID_VALUE) &&
2188 (low_tpt < current_tpt) &&
2189 (high_tpt < current_tpt))
2190 scale_action = 0;
Ben Cahill77626352007-11-29 11:09:44 +08002191
2192 /* At least one adjacent rate's throughput is measured,
2193 * and may have better performance. */
Zhu Yib481de92007-09-25 17:54:57 -07002194 else {
Ben Cahill77626352007-11-29 11:09:44 +08002195 /* Higher adjacent rate's throughput is measured */
Zhu Yib481de92007-09-25 17:54:57 -07002196 if (high_tpt != IWL_INVALID_VALUE) {
Ben Cahill77626352007-11-29 11:09:44 +08002197 /* Higher rate has better throughput */
Guy Cohena5e8b502008-05-29 16:35:11 +08002198 if (high_tpt > current_tpt &&
2199 sr >= IWL_RATE_INCREASE_TH) {
Zhu Yib481de92007-09-25 17:54:57 -07002200 scale_action = 1;
Guy Cohena5e8b502008-05-29 16:35:11 +08002201 } else {
Wey-Yi Guy8fe72312009-03-10 14:35:10 -07002202 scale_action = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002203 }
Ben Cahill77626352007-11-29 11:09:44 +08002204
2205 /* Lower adjacent rate's throughput is measured */
Zhu Yib481de92007-09-25 17:54:57 -07002206 } else if (low_tpt != IWL_INVALID_VALUE) {
Ben Cahill77626352007-11-29 11:09:44 +08002207 /* Lower rate has better throughput */
Zhu Yib481de92007-09-25 17:54:57 -07002208 if (low_tpt > current_tpt) {
Tomas Winklere1623442009-01-27 14:27:56 -08002209 IWL_DEBUG_RATE(priv,
2210 "decrease rate because of low tpt\n");
Zhu Yib481de92007-09-25 17:54:57 -07002211 scale_action = -1;
Guy Cohena5e8b502008-05-29 16:35:11 +08002212 } else if (sr >= IWL_RATE_INCREASE_TH) {
Zhu Yib481de92007-09-25 17:54:57 -07002213 scale_action = 1;
Guy Cohena5e8b502008-05-29 16:35:11 +08002214 }
Zhu Yib481de92007-09-25 17:54:57 -07002215 }
2216 }
2217
Ben Cahill77626352007-11-29 11:09:44 +08002218 /* Sanity check; asked for decrease, but success rate or throughput
2219 * has been good at old rate. Don't change it. */
Guy Cohena5e8b502008-05-29 16:35:11 +08002220 if ((scale_action == -1) && (low != IWL_RATE_INVALID) &&
2221 ((sr > IWL_RATE_HIGH_TH) ||
Zhu Yib481de92007-09-25 17:54:57 -07002222 (current_tpt > (100 * tbl->expected_tpt[low]))))
Zhu Yib481de92007-09-25 17:54:57 -07002223 scale_action = 0;
2224
2225 switch (scale_action) {
2226 case -1:
Ben Cahill77626352007-11-29 11:09:44 +08002227 /* Decrease starting rate, update uCode's rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002228 if (low != IWL_RATE_INVALID) {
2229 update_lq = 1;
2230 index = low;
2231 }
2232 break;
2233 case 1:
Ben Cahill77626352007-11-29 11:09:44 +08002234 /* Increase starting rate, update uCode's rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002235 if (high != IWL_RATE_INVALID) {
2236 update_lq = 1;
2237 index = high;
2238 }
2239
2240 break;
2241 case 0:
Ben Cahill77626352007-11-29 11:09:44 +08002242 /* No change */
Zhu Yib481de92007-09-25 17:54:57 -07002243 default:
2244 break;
2245 }
2246
Tomas Winklere1623442009-01-27 14:27:56 -08002247 IWL_DEBUG_RATE(priv, "choose rate scale index %d action %d low %d "
Zhu Yib481de92007-09-25 17:54:57 -07002248 "high %d type %d\n",
2249 index, scale_action, low, high, tbl->lq_type);
2250
Guy Cohena5e8b502008-05-29 16:35:11 +08002251lq_update:
Ben Cahill77626352007-11-29 11:09:44 +08002252 /* Replace uCode's rate table for the destination station. */
Zhu Yib481de92007-09-25 17:54:57 -07002253 if (update_lq) {
Tomas Winkler978785a2008-12-19 10:37:31 +08002254 rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
Guy Cohen07bc28e2008-04-23 17:15:03 -07002255 rs_fill_link_cmd(priv, lq_sta, rate);
Tomas Winkler66c73db2008-04-15 16:01:40 -07002256 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07002257 }
Ben Cahill77626352007-11-29 11:09:44 +08002258
2259 /* Should we stay with this modulation mode, or search for a new one? */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002260 rs_stay_in_table(lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002261
Ben Cahill77626352007-11-29 11:09:44 +08002262 /*
2263 * Search for new modulation mode if we're:
2264 * 1) Not changing rates right now
2265 * 2) Not just finishing up a search
2266 * 3) Allowing a new search
2267 */
Guy Cohen47cfd462008-05-27 11:29:34 +08002268 if (!update_lq && !done_search && !lq_sta->stay_in_tbl && window->counter) {
Ben Cahill77626352007-11-29 11:09:44 +08002269 /* Save current throughput to compare with "search" throughput*/
Tomas Winklerc33104f2008-01-14 17:46:21 -08002270 lq_sta->last_tpt = current_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07002271
Ben Cahill77626352007-11-29 11:09:44 +08002272 /* Select a new "search" modulation mode to try.
2273 * If one is found, set up the new "search" table. */
Zhu Yib481de92007-09-25 17:54:57 -07002274 if (is_legacy(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08002275 rs_move_legacy_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07002276 else if (is_siso(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08002277 rs_move_siso_to_other(priv, lq_sta, conf, sta, index);
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07002278 else if (is_mimo2(tbl->lq_type))
2279 rs_move_mimo2_to_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07002280 else
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07002281 rs_move_mimo3_to_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07002282
Ben Cahill77626352007-11-29 11:09:44 +08002283 /* If new "search" mode was selected, set up in uCode table */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002284 if (lq_sta->search_better_tbl) {
Ben Cahill77626352007-11-29 11:09:44 +08002285 /* Access the "search" table, clear its history. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002286 tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Zhu Yib481de92007-09-25 17:54:57 -07002287 for (i = 0; i < IWL_RATE_COUNT; i++)
2288 rs_rate_scale_clear_window(&(tbl->win[i]));
2289
Ben Cahill77626352007-11-29 11:09:44 +08002290 /* Use new "search" start rate */
Tomas Winklere7d326ac2008-06-12 09:47:11 +08002291 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002292
Tomas Winklere1623442009-01-27 14:27:56 -08002293 IWL_DEBUG_RATE(priv, "Switch current mcs: %X index: %d\n",
Guy Cohen39e88502008-04-23 17:14:57 -07002294 tbl->current_rate, index);
Guy Cohen07bc28e2008-04-23 17:15:03 -07002295 rs_fill_link_cmd(priv, lq_sta, tbl->current_rate);
Tomas Winkler66c73db2008-04-15 16:01:40 -07002296 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07002297 }
Zhu Yib481de92007-09-25 17:54:57 -07002298
Ben Cahill77626352007-11-29 11:09:44 +08002299 /* If the "active" (non-search) mode was legacy,
2300 * and we've tried switching antennas,
2301 * but we haven't been able to try HT modes (not available),
2302 * stay with best antenna legacy modulation for a while
2303 * before next round of mode comparisons. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002304 tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
Luis R. Rodriguezde27e642008-12-23 15:58:44 -08002305 if (is_legacy(tbl1->lq_type) && !conf_is_ht(conf) &&
Johannes Bergae5eb022008-10-14 16:58:37 +02002306 lq_sta->action_counter >= 1) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002307 lq_sta->action_counter = 0;
Tomas Winklere1623442009-01-27 14:27:56 -08002308 IWL_DEBUG_RATE(priv, "LQ: STAY in legacy table\n");
Ester Kummerbf403db2008-05-05 10:22:40 +08002309 rs_set_stay_in_table(priv, 1, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002310 }
2311
Ben Cahill77626352007-11-29 11:09:44 +08002312 /* If we're in an HT mode, and all 3 mode switch actions
2313 * have been tried and compared, stay in this best modulation
2314 * mode for a while before next round of mode comparisons. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002315 if (lq_sta->enable_counter &&
2316 (lq_sta->action_counter >= IWL_ACTION_LIMIT)) {
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002317 if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
2318 (lq_sta->tx_agg_tid_en & (1 << tid)) &&
2319 (tid != MAX_TID_COUNT)) {
Wey-Yi Guy86b47662009-03-10 14:35:09 -07002320 tid_data =
2321 &priv->stations[lq_sta->lq.sta_id].tid[tid];
2322 if (tid_data->agg.state == IWL_AGG_OFF) {
2323 IWL_DEBUG_RATE(priv,
2324 "try to aggregate tid %d\n",
2325 tid);
2326 rs_tl_turn_on_agg(priv, tid,
2327 lq_sta, sta);
2328 }
Zhu Yib481de92007-09-25 17:54:57 -07002329 }
Tomas Winklerc33104f2008-01-14 17:46:21 -08002330 lq_sta->action_counter = 0;
Ester Kummerbf403db2008-05-05 10:22:40 +08002331 rs_set_stay_in_table(priv, 0, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002332 }
Ben Cahill77626352007-11-29 11:09:44 +08002333
2334 /*
2335 * Else, don't search for a new modulation mode.
2336 * Put new timestamp in stay-in-modulation-mode flush timer if:
2337 * 1) Not changing rates right now
2338 * 2) Not just finishing up a search
2339 * 3) flush timer is empty
2340 */
Zhu Yib481de92007-09-25 17:54:57 -07002341 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002342 if ((!update_lq) && (!done_search) && (!lq_sta->flush_timer))
2343 lq_sta->flush_timer = jiffies;
Zhu Yib481de92007-09-25 17:54:57 -07002344 }
2345
2346out:
Tomas Winkler978785a2008-12-19 10:37:31 +08002347 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
Zhu Yib481de92007-09-25 17:54:57 -07002348 i = index;
Johannes Bergb7e35002008-09-11 02:22:58 +02002349 lq_sta->last_txrate_idx = i;
Zhu Yib481de92007-09-25 17:54:57 -07002350
Zhu Yib481de92007-09-25 17:54:57 -07002351 return;
2352}
2353
2354
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002355static void rs_initialize_lq(struct iwl_priv *priv,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002356 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02002357 struct ieee80211_sta *sta,
2358 struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002359{
Tomas Winklere227cea2008-07-18 13:53:05 +08002360 struct iwl_scale_tbl_info *tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002361 int rate_idx;
Guy Cohenaade00c2008-04-23 17:14:59 -07002362 int i;
Guy Cohen39e88502008-04-23 17:14:57 -07002363 u32 rate;
Guy Cohenaade00c2008-04-23 17:14:59 -07002364 u8 use_green = rs_use_green(priv, conf);
2365 u8 active_tbl = 0;
2366 u8 valid_tx_ant;
Zhu Yib481de92007-09-25 17:54:57 -07002367
Johannes Berg4b7679a2008-09-18 18:14:18 +02002368 if (!sta || !lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002369 goto out;
2370
Johannes Bergb7e35002008-09-11 02:22:58 +02002371 i = lq_sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002372
Tomas Winklerc33104f2008-01-14 17:46:21 -08002373 if ((lq_sta->lq.sta_id == 0xff) &&
Johannes Berg05c914f2008-09-11 00:01:58 +02002374 (priv->iw_mode == NL80211_IFTYPE_ADHOC))
Zhu Yib481de92007-09-25 17:54:57 -07002375 goto out;
2376
Guy Cohenaade00c2008-04-23 17:14:59 -07002377 valid_tx_ant = priv->hw_params.valid_tx_ant;
2378
Tomas Winklerc33104f2008-01-14 17:46:21 -08002379 if (!lq_sta->search_better_tbl)
2380 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002381 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002382 active_tbl = 1 - lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002383
Tomas Winklerc33104f2008-01-14 17:46:21 -08002384 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07002385
2386 if ((i < 0) || (i >= IWL_RATE_COUNT))
2387 i = 0;
2388
Tomas Winkler1826dcc2008-05-15 13:54:02 +08002389 rate = iwl_rates[i].plcp;
Winkler, Tomaseb48dca2008-10-29 14:05:48 -07002390 tbl->ant_type = first_antenna(valid_tx_ant);
2391 rate |= tbl->ant_type << RATE_MCS_ANT_POS;
Zhu Yib481de92007-09-25 17:54:57 -07002392
2393 if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
Guy Cohen39e88502008-04-23 17:14:57 -07002394 rate |= RATE_MCS_CCK_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002395
Guy Cohen39e88502008-04-23 17:14:57 -07002396 rs_get_tbl_info_from_mcs(rate, priv->band, tbl, &rate_idx);
Guy Cohenaade00c2008-04-23 17:14:59 -07002397 if (!rs_is_valid_ant(valid_tx_ant, tbl->ant_type))
2398 rs_toggle_antenna(valid_tx_ant, &rate, tbl);
Zhu Yib481de92007-09-25 17:54:57 -07002399
Tomas Winkler978785a2008-12-19 10:37:31 +08002400 rate = rate_n_flags_from_tbl(priv, tbl, rate_idx, use_green);
Guy Cohen39e88502008-04-23 17:14:57 -07002401 tbl->current_rate = rate;
Guy Coheneecd6e52008-04-23 17:14:58 -07002402 rs_set_expected_tpt_table(lq_sta, tbl);
Guy Cohen07bc28e2008-04-23 17:15:03 -07002403 rs_fill_link_cmd(NULL, lq_sta, rate);
Tomas Winkler66c73db2008-04-15 16:01:40 -07002404 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07002405 out:
2406 return;
2407}
2408
Johannes Berge6a98542008-10-21 12:40:02 +02002409static void rs_get_rate(void *priv_r, struct ieee80211_sta *sta, void *priv_sta,
2410 struct ieee80211_tx_rate_control *txrc)
Zhu Yib481de92007-09-25 17:54:57 -07002411{
2412
Johannes Berge6a98542008-10-21 12:40:02 +02002413 struct sk_buff *skb = txrc->skb;
2414 struct ieee80211_supported_band *sband = txrc->sband;
Johannes Berg4b7679a2008-09-18 18:14:18 +02002415 struct iwl_priv *priv = (struct iwl_priv *)priv_r;
2416 struct ieee80211_conf *conf = &priv->hw->conf;
Zhu Yib481de92007-09-25 17:54:57 -07002417 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berge6a98542008-10-21 12:40:02 +02002418 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Tomas Winkler417f1142008-11-12 13:14:06 -08002419 struct iwl_lq_sta *lq_sta = priv_sta;
2420 int rate_idx;
Abbas, Mohamedc338ba32009-01-21 10:58:02 -08002421 u64 mask_bit = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002422
Tomas Winklere1623442009-01-27 14:27:56 -08002423 IWL_DEBUG_RATE_LIMIT(priv, "rate scale calculate new rate for skb\n");
Zhu Yib481de92007-09-25 17:54:57 -07002424
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002425 /* Get max rate if user set max rate */
2426 if (lq_sta) {
2427 lq_sta->max_rate_idx = txrc->max_rate_idx;
2428 if ((sband->band == IEEE80211_BAND_5GHZ) &&
2429 (lq_sta->max_rate_idx != -1))
2430 lq_sta->max_rate_idx += IWL_FIRST_OFDM_RATE;
2431 if ((lq_sta->max_rate_idx < 0) ||
2432 (lq_sta->max_rate_idx >= IWL_RATE_COUNT))
2433 lq_sta->max_rate_idx = -1;
2434 }
2435
Abbas, Mohamedc338ba32009-01-21 10:58:02 -08002436 if (sta)
2437 mask_bit = sta->supp_rates[sband->band];
2438
Michael Wu2bc454b2007-12-25 19:33:16 -05002439 /* Send management frames and broadcast/multicast data using lowest
2440 * rate. */
Tomas Winkler417f1142008-11-12 13:14:06 -08002441 if (!ieee80211_is_data(hdr->frame_control) ||
2442 is_multicast_ether_addr(hdr->addr1) || !sta || !lq_sta) {
Abbas, Mohamedc338ba32009-01-21 10:58:02 -08002443 if (!mask_bit)
2444 info->control.rates[0].idx =
2445 rate_lowest_index(sband, NULL);
2446 else
2447 info->control.rates[0].idx =
2448 rate_lowest_index(sband, sta);
Johannes Berg4b7679a2008-09-18 18:14:18 +02002449 return;
Zhu Yib481de92007-09-25 17:54:57 -07002450 }
2451
Tomas Winkler417f1142008-11-12 13:14:06 -08002452 rate_idx = lq_sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002453
Johannes Berg05c914f2008-09-11 00:01:58 +02002454 if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) &&
Tomas Winklerc33104f2008-01-14 17:46:21 -08002455 !lq_sta->ibss_sta_added) {
Tomas Winkler947b13a2008-04-16 16:34:48 -07002456 u8 sta_id = iwl_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002457
2458 if (sta_id == IWL_INVALID_STATION) {
Tomas Winklere1623442009-01-27 14:27:56 -08002459 IWL_DEBUG_RATE(priv, "LQ: ADD station %pM\n",
Johannes Berge1749612008-10-27 15:59:26 -07002460 hdr->addr1);
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08002461 sta_id = iwl_add_station_flags(priv, hdr->addr1,
Ron Rindjunsky67d62032007-11-26 16:14:40 +02002462 0, CMD_ASYNC, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07002463 }
2464 if ((sta_id != IWL_INVALID_STATION)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002465 lq_sta->lq.sta_id = sta_id;
2466 lq_sta->lq.rs_table[0].rate_n_flags = 0;
2467 lq_sta->ibss_sta_added = 1;
Johannes Berg4b7679a2008-09-18 18:14:18 +02002468 rs_initialize_lq(priv, conf, sta, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002469 }
Zhu Yib481de92007-09-25 17:54:57 -07002470 }
2471
Tomas Winkler417f1142008-11-12 13:14:06 -08002472 if (rate_idx < 0 || rate_idx > IWL_RATE_COUNT)
2473 rate_idx = rate_lowest_index(sband, sta);
2474 else if (sband->band == IEEE80211_BAND_5GHZ)
2475 rate_idx -= IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -07002476
Tomas Winkler417f1142008-11-12 13:14:06 -08002477 info->control.rates[0].idx = rate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002478}
2479
Johannes Berg4b7679a2008-09-18 18:14:18 +02002480static void *rs_alloc_sta(void *priv_rate, struct ieee80211_sta *sta,
2481 gfp_t gfp)
Zhu Yib481de92007-09-25 17:54:57 -07002482{
Tomas Winklere227cea2008-07-18 13:53:05 +08002483 struct iwl_lq_sta *lq_sta;
Ester Kummerbf403db2008-05-05 10:22:40 +08002484 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07002485 int i, j;
2486
Ester Kummerbf403db2008-05-05 10:22:40 +08002487 priv = (struct iwl_priv *)priv_rate;
Tomas Winklere1623442009-01-27 14:27:56 -08002488 IWL_DEBUG_RATE(priv, "create station rate scale window\n");
Zhu Yib481de92007-09-25 17:54:57 -07002489
Tomas Winklere227cea2008-07-18 13:53:05 +08002490 lq_sta = kzalloc(sizeof(struct iwl_lq_sta), gfp);
Zhu Yib481de92007-09-25 17:54:57 -07002491
Tomas Winklerc33104f2008-01-14 17:46:21 -08002492 if (lq_sta == NULL)
Zhu Yib481de92007-09-25 17:54:57 -07002493 return NULL;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002494 lq_sta->lq.sta_id = 0xff;
Zhu Yib481de92007-09-25 17:54:57 -07002495
Zhu Yi63fddb92007-09-27 11:27:36 +08002496
Zhu Yib481de92007-09-25 17:54:57 -07002497 for (j = 0; j < LQ_SIZE; j++)
2498 for (i = 0; i < IWL_RATE_COUNT; i++)
Tomas Winkler3ac7f142008-07-21 02:40:14 +03002499 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
Zhu Yib481de92007-09-25 17:54:57 -07002500
Tomas Winklerc33104f2008-01-14 17:46:21 -08002501 return lq_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002502}
2503
Johannes Berg4b7679a2008-09-18 18:14:18 +02002504static void rs_rate_init(void *priv_r, struct ieee80211_supported_band *sband,
2505 struct ieee80211_sta *sta, void *priv_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002506{
2507 int i, j;
Johannes Berg4b7679a2008-09-18 18:14:18 +02002508 struct iwl_priv *priv = (struct iwl_priv *)priv_r;
2509 struct ieee80211_conf *conf = &priv->hw->conf;
Tomas Winklere227cea2008-07-18 13:53:05 +08002510 struct iwl_lq_sta *lq_sta = priv_sta;
Mohamed Abbasd5e49032008-12-12 08:22:15 -08002511 u16 mask_bit = 0;
Abbas, Mohamed7d049e52009-01-20 21:33:53 -08002512 int count;
2513 int start_rate = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002514
Tomas Winklerc33104f2008-01-14 17:46:21 -08002515 lq_sta->flush_timer = 0;
Johannes Berg4b7679a2008-09-18 18:14:18 +02002516 lq_sta->supp_rates = sta->supp_rates[sband->band];
Zhu Yib481de92007-09-25 17:54:57 -07002517 for (j = 0; j < LQ_SIZE; j++)
2518 for (i = 0; i < IWL_RATE_COUNT; i++)
Tomas Winkler3ac7f142008-07-21 02:40:14 +03002519 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
Zhu Yib481de92007-09-25 17:54:57 -07002520
Tomas Winklere1623442009-01-27 14:27:56 -08002521 IWL_DEBUG_RATE(priv, "LQ: *** rate scale station global init ***\n");
Zhu Yib481de92007-09-25 17:54:57 -07002522 /* TODO: what is a good starting rate for STA? About middle? Maybe not
2523 * the lowest or the highest rate.. Could consider using RSSI from
2524 * previous packets? Need to have IEEE 802.1X auth succeed immediately
2525 * after assoc.. */
2526
Tomas Winklerc33104f2008-01-14 17:46:21 -08002527 lq_sta->ibss_sta_added = 0;
Johannes Berg05c914f2008-09-11 00:01:58 +02002528 if (priv->iw_mode == NL80211_IFTYPE_AP) {
Johannes Berg4b7679a2008-09-18 18:14:18 +02002529 u8 sta_id = iwl_find_station(priv, sta->addr);
Joe Perches0795af52007-10-03 17:59:30 -07002530
Zhu Yib481de92007-09-25 17:54:57 -07002531 /* for IBSS the call are from tasklet */
Tomas Winklere1623442009-01-27 14:27:56 -08002532 IWL_DEBUG_RATE(priv, "LQ: ADD station %pM\n", sta->addr);
Zhu Yib481de92007-09-25 17:54:57 -07002533
2534 if (sta_id == IWL_INVALID_STATION) {
Tomas Winklere1623442009-01-27 14:27:56 -08002535 IWL_DEBUG_RATE(priv, "LQ: ADD station %pM\n", sta->addr);
Johannes Berg4b7679a2008-09-18 18:14:18 +02002536 sta_id = iwl_add_station_flags(priv, sta->addr,
Ron Rindjunsky67d62032007-11-26 16:14:40 +02002537 0, CMD_ASYNC, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07002538 }
2539 if ((sta_id != IWL_INVALID_STATION)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002540 lq_sta->lq.sta_id = sta_id;
2541 lq_sta->lq.rs_table[0].rate_n_flags = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002542 }
2543 /* FIXME: this is w/a remove it later */
2544 priv->assoc_station_added = 1;
2545 }
2546
Tomas Winklerc33104f2008-01-14 17:46:21 -08002547 lq_sta->is_dup = 0;
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002548 lq_sta->max_rate_idx = -1;
Abbas, Mohamed7d049e52009-01-20 21:33:53 -08002549 lq_sta->missed_rate_counter = IWL_MISSED_RATE_MAX;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002550 lq_sta->is_green = rs_use_green(priv, conf);
Guy Cohen39e88502008-04-23 17:14:57 -07002551 lq_sta->active_legacy_rate = priv->active_rate & ~(0x1000);
Tomas Winklerc33104f2008-01-14 17:46:21 -08002552 lq_sta->active_rate_basic = priv->active_rate_basic;
Johannes Berg8318d782008-01-24 19:38:38 +01002553 lq_sta->band = priv->band;
Ben Cahill77626352007-11-29 11:09:44 +08002554 /*
2555 * active_siso_rate mask includes 9 MBits (bit 5), and CCK (bits 0-3),
2556 * supp_rates[] does not; shift to convert format, force 9 MBits off.
2557 */
Johannes Bergae5eb022008-10-14 16:58:37 +02002558 lq_sta->active_siso_rate = sta->ht_cap.mcs.rx_mask[0] << 1;
2559 lq_sta->active_siso_rate |= sta->ht_cap.mcs.rx_mask[0] & 0x1;
Guy Cohenfde0db32008-04-21 15:42:01 -07002560 lq_sta->active_siso_rate &= ~((u16)0x2);
2561 lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -07002562
Ben Cahill77626352007-11-29 11:09:44 +08002563 /* Same here */
Johannes Bergae5eb022008-10-14 16:58:37 +02002564 lq_sta->active_mimo2_rate = sta->ht_cap.mcs.rx_mask[1] << 1;
2565 lq_sta->active_mimo2_rate |= sta->ht_cap.mcs.rx_mask[1] & 0x1;
Guy Cohenfde0db32008-04-21 15:42:01 -07002566 lq_sta->active_mimo2_rate &= ~((u16)0x2);
2567 lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE;
2568
Johannes Bergae5eb022008-10-14 16:58:37 +02002569 lq_sta->active_mimo3_rate = sta->ht_cap.mcs.rx_mask[2] << 1;
2570 lq_sta->active_mimo3_rate |= sta->ht_cap.mcs.rx_mask[2] & 0x1;
Guy Cohenfde0db32008-04-21 15:42:01 -07002571 lq_sta->active_mimo3_rate &= ~((u16)0x2);
2572 lq_sta->active_mimo3_rate <<= IWL_FIRST_OFDM_RATE;
2573
Tomas Winklere1623442009-01-27 14:27:56 -08002574 IWL_DEBUG_RATE(priv, "SISO-RATE=%X MIMO2-RATE=%X MIMO3-RATE=%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002575 lq_sta->active_siso_rate,
Guy Cohenfde0db32008-04-21 15:42:01 -07002576 lq_sta->active_mimo2_rate,
2577 lq_sta->active_mimo3_rate);
2578
Tomas Winklera96a27f2008-10-23 23:48:56 -07002579 /* These values will be overridden later */
Guy Cohen07bc28e2008-04-23 17:15:03 -07002580 lq_sta->lq.general_params.single_stream_ant_msk = ANT_A;
2581 lq_sta->lq.general_params.dual_stream_ant_msk = ANT_AB;
2582
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002583 /* as default allow aggregation for all tids */
2584 lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002585 lq_sta->drv = priv;
Zhu Yib481de92007-09-25 17:54:57 -07002586
Mohamed Abbasd5e49032008-12-12 08:22:15 -08002587 /* Find highest tx rate supported by hardware and destination station */
Abbas, Mohamed7d049e52009-01-20 21:33:53 -08002588 mask_bit = sta->supp_rates[sband->band];
2589 count = sband->n_bitrates;
2590 if (sband->band == IEEE80211_BAND_5GHZ) {
2591 count += IWL_FIRST_OFDM_RATE;
2592 start_rate = IWL_FIRST_OFDM_RATE;
2593 mask_bit <<= IWL_FIRST_OFDM_RATE;
2594 }
2595
2596 mask_bit = mask_bit & lq_sta->active_legacy_rate;
2597 lq_sta->last_txrate_idx = 4;
2598 for (i = start_rate; i < count; i++)
Mohamed Abbasd5e49032008-12-12 08:22:15 -08002599 if (mask_bit & BIT(i))
2600 lq_sta->last_txrate_idx = i;
2601
Johannes Berg4b7679a2008-09-18 18:14:18 +02002602 rs_initialize_lq(priv, conf, sta, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002603}
2604
Guy Cohenfde0db32008-04-21 15:42:01 -07002605static void rs_fill_link_cmd(const struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08002606 struct iwl_lq_sta *lq_sta, u32 new_rate)
Zhu Yib481de92007-09-25 17:54:57 -07002607{
Tomas Winklere227cea2008-07-18 13:53:05 +08002608 struct iwl_scale_tbl_info tbl_type;
Zhu Yib481de92007-09-25 17:54:57 -07002609 int index = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002610 int rate_idx;
Zhu Yi1b696de2007-09-27 11:27:41 +08002611 int repeat_rate = 0;
Guy Cohenaade00c2008-04-23 17:14:59 -07002612 u8 ant_toggle_cnt = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002613 u8 use_ht_possible = 1;
Guy Cohenaade00c2008-04-23 17:14:59 -07002614 u8 valid_tx_ant = 0;
Guy Cohen07bc28e2008-04-23 17:15:03 -07002615 struct iwl_link_quality_cmd *lq_cmd = &lq_sta->lq;
Zhu Yib481de92007-09-25 17:54:57 -07002616
Ben Cahill77626352007-11-29 11:09:44 +08002617 /* Override starting rate (index 0) if needed for debug purposes */
Guy Cohen39e88502008-04-23 17:14:57 -07002618 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Zhu Yi98d7e092007-09-27 11:27:42 +08002619
Guy Cohen39e88502008-04-23 17:14:57 -07002620 /* Interpret new_rate (rate_n_flags) */
Guy Cohenaade00c2008-04-23 17:14:59 -07002621 memset(&tbl_type, 0, sizeof(tbl_type));
Guy Cohen39e88502008-04-23 17:14:57 -07002622 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band,
Zhu Yib481de92007-09-25 17:54:57 -07002623 &tbl_type, &rate_idx);
2624
Ben Cahill77626352007-11-29 11:09:44 +08002625 /* How many times should we repeat the initial rate? */
Zhu Yib481de92007-09-25 17:54:57 -07002626 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07002627 ant_toggle_cnt = 1;
Zhu Yi1b696de2007-09-27 11:27:41 +08002628 repeat_rate = IWL_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002629 } else {
Zhu Yi1b696de2007-09-27 11:27:41 +08002630 repeat_rate = IWL_HT_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002631 }
Zhu Yib481de92007-09-25 17:54:57 -07002632
2633 lq_cmd->general_params.mimo_delimiter =
2634 is_mimo(tbl_type.lq_type) ? 1 : 0;
Ben Cahill77626352007-11-29 11:09:44 +08002635
2636 /* Fill 1st table entry (index 0) */
Guy Cohen39e88502008-04-23 17:14:57 -07002637 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002638
Guy Cohen07bc28e2008-04-23 17:15:03 -07002639 if (num_of_ant(tbl_type.ant_type) == 1) {
2640 lq_cmd->general_params.single_stream_ant_msk =
2641 tbl_type.ant_type;
2642 } else if (num_of_ant(tbl_type.ant_type) == 2) {
2643 lq_cmd->general_params.dual_stream_ant_msk =
2644 tbl_type.ant_type;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07002645 } else if (num_of_ant(tbl_type.ant_type) == 3) {
2646 lq_cmd->general_params.dual_stream_ant_msk =
2647 tbl_type.ant_type;
Guy Cohen07bc28e2008-04-23 17:15:03 -07002648 } /* otherwise we don't modify the existing value */
Zhu Yib481de92007-09-25 17:54:57 -07002649
2650 index++;
Zhu Yi1b696de2007-09-27 11:27:41 +08002651 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002652
Guy Cohenaade00c2008-04-23 17:14:59 -07002653 if (priv)
2654 valid_tx_ant = priv->hw_params.valid_tx_ant;
2655
Ben Cahill77626352007-11-29 11:09:44 +08002656 /* Fill rest of rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002657 while (index < LINK_QUAL_MAX_RETRY_NUM) {
Ben Cahill77626352007-11-29 11:09:44 +08002658 /* Repeat initial/next rate.
2659 * For legacy IWL_NUMBER_TRY == 1, this loop will not execute.
2660 * For HT IWL_HT_NUMBER_TRY == 3, this executes twice. */
Zhu Yi1b696de2007-09-27 11:27:41 +08002661 while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
Zhu Yib481de92007-09-25 17:54:57 -07002662 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07002663 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2664 ant_toggle_cnt++;
2665 else if (priv &&
2666 rs_toggle_antenna(valid_tx_ant,
2667 &new_rate, &tbl_type))
2668 ant_toggle_cnt = 1;
2669}
Zhu Yi98d7e092007-09-27 11:27:42 +08002670
Ben Cahill77626352007-11-29 11:09:44 +08002671 /* Override next rate if needed for debug purposes */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002672 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Ben Cahill77626352007-11-29 11:09:44 +08002673
2674 /* Fill next table entry */
Zhu Yib481de92007-09-25 17:54:57 -07002675 lq_cmd->rs_table[index].rate_n_flags =
Guy Cohen39e88502008-04-23 17:14:57 -07002676 cpu_to_le32(new_rate);
Zhu Yi1b696de2007-09-27 11:27:41 +08002677 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002678 index++;
2679 }
2680
Guy Cohen39e88502008-04-23 17:14:57 -07002681 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band, &tbl_type,
Zhu Yib481de92007-09-25 17:54:57 -07002682 &rate_idx);
2683
Ben Cahill77626352007-11-29 11:09:44 +08002684 /* Indicate to uCode which entries might be MIMO.
2685 * If initial rate was MIMO, this will finally end up
2686 * as (IWL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
Zhu Yib481de92007-09-25 17:54:57 -07002687 if (is_mimo(tbl_type.lq_type))
2688 lq_cmd->general_params.mimo_delimiter = index;
2689
Ben Cahill77626352007-11-29 11:09:44 +08002690 /* Get next rate */
Guy Cohen39e88502008-04-23 17:14:57 -07002691 new_rate = rs_get_lower_rate(lq_sta, &tbl_type, rate_idx,
2692 use_ht_possible);
Zhu Yib481de92007-09-25 17:54:57 -07002693
Ben Cahill77626352007-11-29 11:09:44 +08002694 /* How many times should we repeat the next rate? */
Zhu Yib481de92007-09-25 17:54:57 -07002695 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07002696 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2697 ant_toggle_cnt++;
2698 else if (priv &&
2699 rs_toggle_antenna(valid_tx_ant,
2700 &new_rate, &tbl_type))
2701 ant_toggle_cnt = 1;
2702
Zhu Yi1b696de2007-09-27 11:27:41 +08002703 repeat_rate = IWL_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002704 } else {
Zhu Yi1b696de2007-09-27 11:27:41 +08002705 repeat_rate = IWL_HT_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002706 }
Zhu Yib481de92007-09-25 17:54:57 -07002707
Ben Cahill77626352007-11-29 11:09:44 +08002708 /* Don't allow HT rates after next pass.
2709 * rs_get_lower_rate() will change type to LQ_A or LQ_G. */
Zhu Yib481de92007-09-25 17:54:57 -07002710 use_ht_possible = 0;
2711
Ben Cahill77626352007-11-29 11:09:44 +08002712 /* Override next rate if needed for debug purposes */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002713 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Ben Cahill77626352007-11-29 11:09:44 +08002714
2715 /* Fill next table entry */
Guy Cohen39e88502008-04-23 17:14:57 -07002716 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002717
2718 index++;
Zhu Yi1b696de2007-09-27 11:27:41 +08002719 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002720 }
2721
Tomas Winkler5e1dd8d2008-05-29 16:35:17 +08002722 lq_cmd->agg_params.agg_frame_cnt_limit = 64;
Zhu Yib481de92007-09-25 17:54:57 -07002723 lq_cmd->agg_params.agg_dis_start_th = 3;
2724 lq_cmd->agg_params.agg_time_limit = cpu_to_le16(4000);
Zhu Yib481de92007-09-25 17:54:57 -07002725}
2726
Johannes Berg4b7679a2008-09-18 18:14:18 +02002727static void *rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
Zhu Yib481de92007-09-25 17:54:57 -07002728{
Johannes Berg4b7679a2008-09-18 18:14:18 +02002729 return hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07002730}
2731/* rate scale requires free function to be implemented */
2732static void rs_free(void *priv_rate)
2733{
2734 return;
2735}
2736
Johannes Berg4b7679a2008-09-18 18:14:18 +02002737static void rs_free_sta(void *priv_r, struct ieee80211_sta *sta,
2738 void *priv_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002739{
Tomas Winklere227cea2008-07-18 13:53:05 +08002740 struct iwl_lq_sta *lq_sta = priv_sta;
Rami Rosen45527c22008-10-07 09:50:01 +02002741 struct iwl_priv *priv __maybe_unused = priv_r;
Zhu Yib481de92007-09-25 17:54:57 -07002742
Tomas Winklere1623442009-01-27 14:27:56 -08002743 IWL_DEBUG_RATE(priv, "enter\n");
Tomas Winklerc33104f2008-01-14 17:46:21 -08002744 kfree(lq_sta);
Tomas Winklere1623442009-01-27 14:27:56 -08002745 IWL_DEBUG_RATE(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07002746}
2747
2748
Zhu Yi93dc6462007-09-27 11:27:37 +08002749#ifdef CONFIG_MAC80211_DEBUGFS
Zhu Yi5ae212c2007-09-27 11:27:38 +08002750static int open_file_generic(struct inode *inode, struct file *file)
2751{
2752 file->private_data = inode->i_private;
2753 return 0;
2754}
Tomas Winklere227cea2008-07-18 13:53:05 +08002755static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
2756 u32 *rate_n_flags, int index)
Zhu Yi98d7e092007-09-27 11:27:42 +08002757{
Ester Kummerbf403db2008-05-05 10:22:40 +08002758 struct iwl_priv *priv;
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07002759 u8 valid_tx_ant;
2760 u8 ant_sel_tx;
Ester Kummerbf403db2008-05-05 10:22:40 +08002761
2762 priv = lq_sta->drv;
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07002763 valid_tx_ant = priv->hw_params.valid_tx_ant;
Guy Cohen39e88502008-04-23 17:14:57 -07002764 if (lq_sta->dbg_fixed_rate) {
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07002765 ant_sel_tx =
2766 ((lq_sta->dbg_fixed_rate & RATE_MCS_ANT_ABC_MSK)
2767 >> RATE_MCS_ANT_POS);
2768 if ((valid_tx_ant & ant_sel_tx) == ant_sel_tx) {
Guy Cohen39e88502008-04-23 17:14:57 -07002769 *rate_n_flags = lq_sta->dbg_fixed_rate;
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07002770 IWL_DEBUG_RATE(priv, "Fixed rate ON\n");
Guy Cohen39e88502008-04-23 17:14:57 -07002771 } else {
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07002772 lq_sta->dbg_fixed_rate = 0;
2773 IWL_ERR(priv,
2774 "Invalid antenna selection 0x%X, Valid is 0x%X\n",
2775 ant_sel_tx, valid_tx_ant);
2776 IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
Guy Cohen39e88502008-04-23 17:14:57 -07002777 }
Guy Cohen39e88502008-04-23 17:14:57 -07002778 } else {
Tomas Winklere1623442009-01-27 14:27:56 -08002779 IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
Zhu Yi98d7e092007-09-27 11:27:42 +08002780 }
Zhu Yi98d7e092007-09-27 11:27:42 +08002781}
2782
2783static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
2784 const char __user *user_buf, size_t count, loff_t *ppos)
2785{
Tomas Winklere227cea2008-07-18 13:53:05 +08002786 struct iwl_lq_sta *lq_sta = file->private_data;
Ester Kummerbf403db2008-05-05 10:22:40 +08002787 struct iwl_priv *priv;
Zhu Yi98d7e092007-09-27 11:27:42 +08002788 char buf[64];
2789 int buf_size;
2790 u32 parsed_rate;
2791
Ester Kummerbf403db2008-05-05 10:22:40 +08002792 priv = lq_sta->drv;
Zhu Yi98d7e092007-09-27 11:27:42 +08002793 memset(buf, 0, sizeof(buf));
2794 buf_size = min(count, sizeof(buf) - 1);
2795 if (copy_from_user(buf, user_buf, buf_size))
2796 return -EFAULT;
2797
2798 if (sscanf(buf, "%x", &parsed_rate) == 1)
Guy Cohen39e88502008-04-23 17:14:57 -07002799 lq_sta->dbg_fixed_rate = parsed_rate;
Zhu Yi98d7e092007-09-27 11:27:42 +08002800 else
Guy Cohen39e88502008-04-23 17:14:57 -07002801 lq_sta->dbg_fixed_rate = 0;
Zhu Yi98d7e092007-09-27 11:27:42 +08002802
Guy Cohen39e88502008-04-23 17:14:57 -07002803 lq_sta->active_legacy_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
2804 lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2805 lq_sta->active_mimo2_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2806 lq_sta->active_mimo3_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
Zhu Yi98d7e092007-09-27 11:27:42 +08002807
Tomas Winklere1623442009-01-27 14:27:56 -08002808 IWL_DEBUG_RATE(priv, "sta_id %d rate 0x%X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07002809 lq_sta->lq.sta_id, lq_sta->dbg_fixed_rate);
Zhu Yi98d7e092007-09-27 11:27:42 +08002810
Guy Cohen39e88502008-04-23 17:14:57 -07002811 if (lq_sta->dbg_fixed_rate) {
Guy Cohen07bc28e2008-04-23 17:15:03 -07002812 rs_fill_link_cmd(NULL, lq_sta, lq_sta->dbg_fixed_rate);
Tomas Winkler66c73db2008-04-15 16:01:40 -07002813 iwl_send_lq_cmd(lq_sta->drv, &lq_sta->lq, CMD_ASYNC);
Zhu Yi98d7e092007-09-27 11:27:42 +08002814 }
2815
2816 return count;
2817}
Zhu Yi0209dc12007-09-27 11:27:43 +08002818
Zhu Yi5ae212c2007-09-27 11:27:38 +08002819static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
2820 char __user *user_buf, size_t count, loff_t *ppos)
2821{
Frank Seidela412c802009-03-01 20:25:38 +01002822 char *buff;
Zhu Yi5ae212c2007-09-27 11:27:38 +08002823 int desc = 0;
2824 int i = 0;
Frank Seidela412c802009-03-01 20:25:38 +01002825 ssize_t ret;
Zhu Yi5ae212c2007-09-27 11:27:38 +08002826
Tomas Winklere227cea2008-07-18 13:53:05 +08002827 struct iwl_lq_sta *lq_sta = file->private_data;
Wey-Yi Guyd8ae4f52009-03-10 14:35:07 -07002828 struct iwl_priv *priv;
Wey-Yi Guyadb7b5e2009-03-10 14:35:08 -07002829 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002830
Wey-Yi Guyd8ae4f52009-03-10 14:35:07 -07002831 priv = lq_sta->drv;
Frank Seidela412c802009-03-01 20:25:38 +01002832 buff = kmalloc(1024, GFP_KERNEL);
2833 if (!buff)
2834 return -ENOMEM;
2835
Tomas Winklerc33104f2008-01-14 17:46:21 -08002836 desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
Zhu Yi98d7e092007-09-27 11:27:42 +08002837 desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002838 lq_sta->total_failed, lq_sta->total_success,
Guy Cohen39e88502008-04-23 17:14:57 -07002839 lq_sta->active_legacy_rate);
Zhu Yi98d7e092007-09-27 11:27:42 +08002840 desc += sprintf(buff+desc, "fixed rate 0x%X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07002841 lq_sta->dbg_fixed_rate);
Wey-Yi Guyd8ae4f52009-03-10 14:35:07 -07002842 desc += sprintf(buff+desc, "valid_tx_ant %s%s%s\n",
2843 (priv->hw_params.valid_tx_ant & ANT_A) ? "ANT_A," : "",
2844 (priv->hw_params.valid_tx_ant & ANT_B) ? "ANT_B," : "",
2845 (priv->hw_params.valid_tx_ant & ANT_C) ? "ANT_C" : "");
Wey-Yi Guyadb7b5e2009-03-10 14:35:08 -07002846 desc += sprintf(buff+desc, "lq type %s\n",
2847 (is_legacy(tbl->lq_type)) ? "legacy" : "HT");
2848 if (is_Ht(tbl->lq_type)) {
2849 desc += sprintf(buff+desc, " %s",
2850 (is_siso(tbl->lq_type)) ? "SISO" :
2851 ((is_mimo2(tbl->lq_type)) ? "MIMO2" : "MIMO3"));
2852 desc += sprintf(buff+desc, " %s",
2853 (tbl->is_fat) ? "40MHz" : "20MHz");
2854 desc += sprintf(buff+desc, " %s\n", (tbl->is_SGI) ? "SGI" : "");
2855 }
Zhu Yi5ae212c2007-09-27 11:27:38 +08002856 desc += sprintf(buff+desc, "general:"
2857 "flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002858 lq_sta->lq.general_params.flags,
2859 lq_sta->lq.general_params.mimo_delimiter,
2860 lq_sta->lq.general_params.single_stream_ant_msk,
2861 lq_sta->lq.general_params.dual_stream_ant_msk);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002862
2863 desc += sprintf(buff+desc, "agg:"
2864 "time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002865 le16_to_cpu(lq_sta->lq.agg_params.agg_time_limit),
2866 lq_sta->lq.agg_params.agg_dis_start_th,
2867 lq_sta->lq.agg_params.agg_frame_cnt_limit);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002868
2869 desc += sprintf(buff+desc,
2870 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002871 lq_sta->lq.general_params.start_rate_index[0],
2872 lq_sta->lq.general_params.start_rate_index[1],
2873 lq_sta->lq.general_params.start_rate_index[2],
2874 lq_sta->lq.general_params.start_rate_index[3]);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002875
2876
2877 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
2878 desc += sprintf(buff+desc, " rate[%d] 0x%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002879 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags));
Zhu Yi5ae212c2007-09-27 11:27:38 +08002880
Frank Seidela412c802009-03-01 20:25:38 +01002881 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2882 kfree(buff);
2883 return ret;
Zhu Yi5ae212c2007-09-27 11:27:38 +08002884}
2885
2886static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
Zhu Yi98d7e092007-09-27 11:27:42 +08002887 .write = rs_sta_dbgfs_scale_table_write,
Zhu Yi5ae212c2007-09-27 11:27:38 +08002888 .read = rs_sta_dbgfs_scale_table_read,
2889 .open = open_file_generic,
2890};
Zhu Yi0209dc12007-09-27 11:27:43 +08002891static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
2892 char __user *user_buf, size_t count, loff_t *ppos)
2893{
Frank Seidela412c802009-03-01 20:25:38 +01002894 char *buff;
Zhu Yi0209dc12007-09-27 11:27:43 +08002895 int desc = 0;
2896 int i, j;
Frank Seidela412c802009-03-01 20:25:38 +01002897 ssize_t ret;
Zhu Yi0209dc12007-09-27 11:27:43 +08002898
Tomas Winklere227cea2008-07-18 13:53:05 +08002899 struct iwl_lq_sta *lq_sta = file->private_data;
Frank Seidela412c802009-03-01 20:25:38 +01002900
2901 buff = kmalloc(1024, GFP_KERNEL);
2902 if (!buff)
2903 return -ENOMEM;
2904
Zhu Yi0209dc12007-09-27 11:27:43 +08002905 for (i = 0; i < LQ_SIZE; i++) {
2906 desc += sprintf(buff+desc, "%s type=%d SGI=%d FAT=%d DUP=%d\n"
2907 "rate=0x%X\n",
Abhijeet Kolekarc3056062008-11-12 13:14:08 -08002908 lq_sta->active_tbl == i ? "*" : "x",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002909 lq_sta->lq_info[i].lq_type,
2910 lq_sta->lq_info[i].is_SGI,
2911 lq_sta->lq_info[i].is_fat,
2912 lq_sta->lq_info[i].is_dup,
Guy Cohen39e88502008-04-23 17:14:57 -07002913 lq_sta->lq_info[i].current_rate);
Zhu Yi0209dc12007-09-27 11:27:43 +08002914 for (j = 0; j < IWL_RATE_COUNT; j++) {
2915 desc += sprintf(buff+desc,
Tomas Winklerc33104f2008-01-14 17:46:21 -08002916 "counter=%d success=%d %%=%d\n",
2917 lq_sta->lq_info[i].win[j].counter,
2918 lq_sta->lq_info[i].win[j].success_counter,
2919 lq_sta->lq_info[i].win[j].success_ratio);
Zhu Yi0209dc12007-09-27 11:27:43 +08002920 }
2921 }
Frank Seidela412c802009-03-01 20:25:38 +01002922 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2923 kfree(buff);
2924 return ret;
Zhu Yi0209dc12007-09-27 11:27:43 +08002925}
2926
2927static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
2928 .read = rs_sta_dbgfs_stats_table_read,
2929 .open = open_file_generic,
2930};
Zhu Yi5ae212c2007-09-27 11:27:38 +08002931
Zhu Yi93dc6462007-09-27 11:27:37 +08002932static void rs_add_debugfs(void *priv, void *priv_sta,
2933 struct dentry *dir)
2934{
Tomas Winklere227cea2008-07-18 13:53:05 +08002935 struct iwl_lq_sta *lq_sta = priv_sta;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002936 lq_sta->rs_sta_dbgfs_scale_table_file =
Zhu Yi0209dc12007-09-27 11:27:43 +08002937 debugfs_create_file("rate_scale_table", 0600, dir,
Tomas Winklerc33104f2008-01-14 17:46:21 -08002938 lq_sta, &rs_sta_dbgfs_scale_table_ops);
2939 lq_sta->rs_sta_dbgfs_stats_table_file =
Zhu Yi0209dc12007-09-27 11:27:43 +08002940 debugfs_create_file("rate_stats_table", 0600, dir,
Tomas Winklerc33104f2008-01-14 17:46:21 -08002941 lq_sta, &rs_sta_dbgfs_stats_table_ops);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002942 lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file =
2943 debugfs_create_u8("tx_agg_tid_enable", 0600, dir,
2944 &lq_sta->tx_agg_tid_en);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002945
Zhu Yi93dc6462007-09-27 11:27:37 +08002946}
2947
2948static void rs_remove_debugfs(void *priv, void *priv_sta)
2949{
Tomas Winklere227cea2008-07-18 13:53:05 +08002950 struct iwl_lq_sta *lq_sta = priv_sta;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002951 debugfs_remove(lq_sta->rs_sta_dbgfs_scale_table_file);
2952 debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002953 debugfs_remove(lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file);
Zhu Yi93dc6462007-09-27 11:27:37 +08002954}
2955#endif
2956
Zhu Yib481de92007-09-25 17:54:57 -07002957static struct rate_control_ops rs_ops = {
2958 .module = NULL,
2959 .name = RS_NAME,
2960 .tx_status = rs_tx_status,
2961 .get_rate = rs_get_rate,
2962 .rate_init = rs_rate_init,
Zhu Yib481de92007-09-25 17:54:57 -07002963 .alloc = rs_alloc,
2964 .free = rs_free,
2965 .alloc_sta = rs_alloc_sta,
2966 .free_sta = rs_free_sta,
Zhu Yi93dc6462007-09-27 11:27:37 +08002967#ifdef CONFIG_MAC80211_DEBUGFS
2968 .add_sta_debugfs = rs_add_debugfs,
2969 .remove_sta_debugfs = rs_remove_debugfs,
2970#endif
Zhu Yib481de92007-09-25 17:54:57 -07002971};
2972
Tomas Winklere227cea2008-07-18 13:53:05 +08002973int iwlagn_rate_control_register(void)
Zhu Yib481de92007-09-25 17:54:57 -07002974{
Reinette Chatre897e1cf2008-03-28 16:21:09 -07002975 return ieee80211_rate_control_register(&rs_ops);
Zhu Yib481de92007-09-25 17:54:57 -07002976}
2977
Tomas Winklere227cea2008-07-18 13:53:05 +08002978void iwlagn_rate_control_unregister(void)
Zhu Yib481de92007-09-25 17:54:57 -07002979{
2980 ieee80211_rate_control_unregister(&rs_ops);
2981}
2982