blob: 0a71bb55d0ee083207cfc1e49b97b0df042858f4 [file] [log] [blame]
Zhu Yib481de92007-09-25 17:54:57 -07001/******************************************************************************
2 *
Reinette Chatre01f81622009-01-08 10:20:02 -08003 * Copyright(c) 2005 - 2009 Intel Corporation. All rights reserved.
Zhu Yib481de92007-09-25 17:54:57 -07004 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
17 *
18 * The full GNU General Public License is included in this distribution in the
19 * file called LICENSE.
20 *
21 * Contact Information:
Winkler, Tomas759ef892008-12-09 11:28:58 -080022 * Intel Linux Wireless <ilw@linux.intel.com>
Zhu Yib481de92007-09-25 17:54:57 -070023 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24 *
25 *****************************************************************************/
26#include <linux/kernel.h>
27#include <linux/init.h>
28#include <linux/skbuff.h>
29#include <linux/wireless.h>
30#include <net/mac80211.h>
Zhu Yib481de92007-09-25 17:54:57 -070031
32#include <linux/netdevice.h>
33#include <linux/etherdevice.h>
34#include <linux/delay.h>
35
36#include <linux/workqueue.h>
37
Tomas Winkler3e0d4cb2008-04-24 11:55:38 -070038#include "iwl-dev.h"
Emmanuel Grumbachbe1f3ab62008-06-12 09:47:18 +080039#include "iwl-sta.h"
Tomas Winkler857485c2008-03-21 13:53:44 -070040#include "iwl-core.h"
Zhu Yib481de92007-09-25 17:54:57 -070041
Tomas Winklere227cea2008-07-18 13:53:05 +080042#define RS_NAME "iwl-agn-rs"
Zhu Yib481de92007-09-25 17:54:57 -070043
Guy Cohenaade00c2008-04-23 17:14:59 -070044#define NUM_TRY_BEFORE_ANT_TOGGLE 1
Zhu Yib481de92007-09-25 17:54:57 -070045#define IWL_NUMBER_TRY 1
46#define IWL_HT_NUMBER_TRY 3
47
Ben Cahill77626352007-11-29 11:09:44 +080048#define IWL_RATE_MAX_WINDOW 62 /* # tx in history window */
49#define IWL_RATE_MIN_FAILURE_TH 6 /* min failures to calc tpt */
50#define IWL_RATE_MIN_SUCCESS_TH 8 /* min successes to calc tpt */
51
Abbas, Mohamed7d049e52009-01-20 21:33:53 -080052/* max allowed rate miss before sync LQ cmd */
53#define IWL_MISSED_RATE_MAX 15
Ben Cahill77626352007-11-29 11:09:44 +080054/* max time to accum history 2 seconds */
Mohamed Abbas447fee72009-04-20 14:37:02 -070055#define IWL_RATE_SCALE_FLUSH_INTVL (3*HZ)
Zhu Yib481de92007-09-25 17:54:57 -070056
57static u8 rs_ht_to_legacy[] = {
58 IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
59 IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
60 IWL_RATE_6M_INDEX,
61 IWL_RATE_6M_INDEX, IWL_RATE_9M_INDEX,
62 IWL_RATE_12M_INDEX, IWL_RATE_18M_INDEX,
63 IWL_RATE_24M_INDEX, IWL_RATE_36M_INDEX,
64 IWL_RATE_48M_INDEX, IWL_RATE_54M_INDEX
65};
66
Guy Cohenaade00c2008-04-23 17:14:59 -070067static const u8 ant_toggle_lookup[] = {
68 /*ANT_NONE -> */ ANT_NONE,
69 /*ANT_A -> */ ANT_B,
70 /*ANT_B -> */ ANT_C,
71 /*ANT_AB -> */ ANT_BC,
72 /*ANT_C -> */ ANT_A,
73 /*ANT_AC -> */ ANT_AB,
74 /*ANT_BC -> */ ANT_AC,
75 /*ANT_ABC -> */ ANT_ABC,
76};
77
Ben Cahill77626352007-11-29 11:09:44 +080078/**
Tomas Winklere227cea2008-07-18 13:53:05 +080079 * struct iwl_rate_scale_data -- tx success history for one rate
Ben Cahill77626352007-11-29 11:09:44 +080080 */
Tomas Winklere227cea2008-07-18 13:53:05 +080081struct iwl_rate_scale_data {
Ben Cahill77626352007-11-29 11:09:44 +080082 u64 data; /* bitmap of successful frames */
83 s32 success_counter; /* number of frames successful */
84 s32 success_ratio; /* per-cent * 128 */
85 s32 counter; /* number of frames attempted */
86 s32 average_tpt; /* success ratio * expected throughput */
Zhu Yib481de92007-09-25 17:54:57 -070087 unsigned long stamp;
88};
89
Ben Cahill77626352007-11-29 11:09:44 +080090/**
Tomas Winklere227cea2008-07-18 13:53:05 +080091 * struct iwl_scale_tbl_info -- tx params and success history for all rates
Ben Cahill77626352007-11-29 11:09:44 +080092 *
Tomas Winklere227cea2008-07-18 13:53:05 +080093 * There are two of these in struct iwl_lq_sta,
Ben Cahill77626352007-11-29 11:09:44 +080094 * one for "active", and one for "search".
95 */
Tomas Winklere227cea2008-07-18 13:53:05 +080096struct iwl_scale_tbl_info {
Guy Cohenfde0db32008-04-21 15:42:01 -070097 enum iwl_table_type lq_type;
98 u8 ant_type;
Ben Cahill77626352007-11-29 11:09:44 +080099 u8 is_SGI; /* 1 = short guard interval */
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 */
Mohamed Abbas447fee72009-04-20 14:37:02 -0700138 u64 flush_timer; /* time staying in mode before new search */
Ben Cahill77626352007-11-29 11:09:44 +0800139
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;
Wey-Yi Guy12b96812009-04-08 11:39:27 -0700170 /* last tx rate_n_flags */
171 u32 last_rate_n_flags;
Zhu Yib481de92007-09-25 17:54:57 -0700172};
173
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700174static void rs_rate_scale_perform(struct iwl_priv *priv,
Gábor Stefanik514d65c2009-04-23 19:36:08 +0200175 struct sk_buff *skb,
Johannes Berg4b7679a2008-09-18 18:14:18 +0200176 struct ieee80211_sta *sta,
177 struct iwl_lq_sta *lq_sta);
Guy Cohenfde0db32008-04-21 15:42:01 -0700178static void rs_fill_link_cmd(const struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +0800179 struct iwl_lq_sta *lq_sta, u32 rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -0700180
181
Zhu Yi98d7e092007-09-27 11:27:42 +0800182#ifdef CONFIG_MAC80211_DEBUGFS
Tomas Winklere227cea2008-07-18 13:53:05 +0800183static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
184 u32 *rate_n_flags, int index);
Zhu Yi98d7e092007-09-27 11:27:42 +0800185#else
Tomas Winklere227cea2008-07-18 13:53:05 +0800186static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
187 u32 *rate_n_flags, int index)
Zhu Yi98d7e092007-09-27 11:27:42 +0800188{}
189#endif
Ben Cahill77626352007-11-29 11:09:44 +0800190
191/*
192 * Expected throughput metrics for following rates:
193 * 1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
194 * "G" is the only table that supports CCK (the first 4 rates).
195 */
Wey-Yi Guy584a0f02009-04-01 14:33:24 -0700196
Zhu Yib481de92007-09-25 17:54:57 -0700197static s32 expected_tpt_A[IWL_RATE_COUNT] = {
198 0, 0, 0, 0, 40, 57, 72, 98, 121, 154, 177, 186, 186
199};
200
201static s32 expected_tpt_G[IWL_RATE_COUNT] = {
202 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 186
203};
204
205static s32 expected_tpt_siso20MHz[IWL_RATE_COUNT] = {
206 0, 0, 0, 0, 42, 42, 76, 102, 124, 159, 183, 193, 202
207};
208
209static s32 expected_tpt_siso20MHzSGI[IWL_RATE_COUNT] = {
210 0, 0, 0, 0, 46, 46, 82, 110, 132, 168, 192, 202, 211
211};
212
Wey-Yi Guy584a0f02009-04-01 14:33:24 -0700213static s32 expected_tpt_mimo2_20MHz[IWL_RATE_COUNT] = {
Zhu Yib481de92007-09-25 17:54:57 -0700214 0, 0, 0, 0, 74, 74, 123, 155, 179, 214, 236, 244, 251
215};
216
Wey-Yi Guy584a0f02009-04-01 14:33:24 -0700217static s32 expected_tpt_mimo2_20MHzSGI[IWL_RATE_COUNT] = {
Zhu Yib481de92007-09-25 17:54:57 -0700218 0, 0, 0, 0, 81, 81, 131, 164, 188, 222, 243, 251, 257
219};
220
221static s32 expected_tpt_siso40MHz[IWL_RATE_COUNT] = {
222 0, 0, 0, 0, 77, 77, 127, 160, 184, 220, 242, 250, 257
223};
224
225static s32 expected_tpt_siso40MHzSGI[IWL_RATE_COUNT] = {
226 0, 0, 0, 0, 83, 83, 135, 169, 193, 229, 250, 257, 264
227};
228
Wey-Yi Guy584a0f02009-04-01 14:33:24 -0700229static s32 expected_tpt_mimo2_40MHz[IWL_RATE_COUNT] = {
Zhu Yib481de92007-09-25 17:54:57 -0700230 0, 0, 0, 0, 123, 123, 182, 214, 235, 264, 279, 285, 289
231};
232
Wey-Yi Guy584a0f02009-04-01 14:33:24 -0700233static s32 expected_tpt_mimo2_40MHzSGI[IWL_RATE_COUNT] = {
Zhu Yib481de92007-09-25 17:54:57 -0700234 0, 0, 0, 0, 131, 131, 191, 222, 242, 270, 284, 289, 293
235};
236
Wey-Yi Guy584a0f02009-04-01 14:33:24 -0700237/* Expected throughput metric MIMO3 */
238static s32 expected_tpt_mimo3_20MHz[IWL_RATE_COUNT] = {
239 0, 0, 0, 0, 99, 99, 153, 186, 208, 239, 256, 263, 268
240};
241
242static s32 expected_tpt_mimo3_20MHzSGI[IWL_RATE_COUNT] = {
243 0, 0, 0, 0, 106, 106, 162, 194, 215, 246, 262, 268, 273
244};
245
246static s32 expected_tpt_mimo3_40MHz[IWL_RATE_COUNT] = {
247 0, 0, 0, 0, 152, 152, 211, 239, 255, 279, 290, 294, 297
248};
249
250static s32 expected_tpt_mimo3_40MHzSGI[IWL_RATE_COUNT] = {
251 0, 0, 0, 0, 160, 160, 219, 245, 261, 284, 294, 297, 300
252};
253
Wey-Yi Guy12b96812009-04-08 11:39:27 -0700254/* mbps, mcs */
255const static struct iwl_rate_mcs_info iwl_rate_mcs[IWL_RATE_COUNT] = {
256 {"1", ""},
257 {"2", ""},
258 {"5.5", ""},
259 {"11", ""},
260 {"6", "BPSK 1/2"},
261 {"9", "BPSK 1/2"},
262 {"12", "QPSK 1/2"},
263 {"18", "QPSK 3/4"},
264 {"24", "16QAM 1/2"},
265 {"36", "16QAM 3/4"},
266 {"48", "64QAM 2/3"},
267 {"54", "64QAM 3/4"},
268 {"60", "64QAM 5/6"}
269};
270
Guy Cohen39e88502008-04-23 17:14:57 -0700271static inline u8 rs_extract_rate(u32 rate_n_flags)
Zhu Yib481de92007-09-25 17:54:57 -0700272{
273 return (u8)(rate_n_flags & 0xFF);
274}
275
Tomas Winklere227cea2008-07-18 13:53:05 +0800276static void rs_rate_scale_clear_window(struct iwl_rate_scale_data *window)
Zhu Yib481de92007-09-25 17:54:57 -0700277{
278 window->data = 0;
279 window->success_counter = 0;
280 window->success_ratio = IWL_INVALID_VALUE;
281 window->counter = 0;
282 window->average_tpt = IWL_INVALID_VALUE;
283 window->stamp = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700284}
285
Guy Cohenaade00c2008-04-23 17:14:59 -0700286static inline u8 rs_is_valid_ant(u8 valid_antenna, u8 ant_type)
287{
Tomas Winkler3ac7f142008-07-21 02:40:14 +0300288 return (ant_type & valid_antenna) == ant_type;
Guy Cohenaade00c2008-04-23 17:14:59 -0700289}
290
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200291/*
292 * removes the old data from the statistics. All data that is older than
293 * TID_MAX_TIME_DIFF, will be deleted.
294 */
Tomas Winklere227cea2008-07-18 13:53:05 +0800295static void rs_tl_rm_old_stats(struct iwl_traffic_load *tl, u32 curr_time)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200296{
297 /* The oldest age we want to keep */
298 u32 oldest_time = curr_time - TID_MAX_TIME_DIFF;
299
300 while (tl->queue_count &&
301 (tl->time_stamp < oldest_time)) {
302 tl->total -= tl->packet_count[tl->head];
303 tl->packet_count[tl->head] = 0;
304 tl->time_stamp += TID_QUEUE_CELL_SPACING;
305 tl->queue_count--;
306 tl->head++;
307 if (tl->head >= TID_QUEUE_MAX_SIZE)
308 tl->head = 0;
309 }
310}
311
312/*
313 * increment traffic load value for tid and also remove
314 * any old values if passed the certain time period
315 */
Tomas Winklere227cea2008-07-18 13:53:05 +0800316static u8 rs_tl_add_packet(struct iwl_lq_sta *lq_data,
Ron Rindjunskyfaa29712008-06-12 09:46:58 +0800317 struct ieee80211_hdr *hdr)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200318{
319 u32 curr_time = jiffies_to_msecs(jiffies);
320 u32 time_diff;
321 s32 index;
Tomas Winklere227cea2008-07-18 13:53:05 +0800322 struct iwl_traffic_load *tl = NULL;
Tomas Winkler54dbb522008-05-15 13:54:06 +0800323 u8 tid;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200324
Tomas Winkler417f1142008-11-12 13:14:06 -0800325 if (ieee80211_is_data_qos(hdr->frame_control)) {
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700326 u8 *qc = ieee80211_get_qos_ctl(hdr);
Tomas Winkler54dbb522008-05-15 13:54:06 +0800327 tid = qc[0] & 0xf;
328 } else
Ron Rindjunskyfaa29712008-06-12 09:46:58 +0800329 return MAX_TID_COUNT;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200330
331 tl = &lq_data->load[tid];
332
333 curr_time -= curr_time % TID_ROUND_VALUE;
334
335 /* Happens only for the first packet. Initialize the data */
336 if (!(tl->queue_count)) {
337 tl->total = 1;
338 tl->time_stamp = curr_time;
339 tl->queue_count = 1;
340 tl->head = 0;
341 tl->packet_count[0] = 1;
Ron Rindjunskyfaa29712008-06-12 09:46:58 +0800342 return MAX_TID_COUNT;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200343 }
344
345 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
346 index = time_diff / TID_QUEUE_CELL_SPACING;
347
348 /* The history is too long: remove data that is older than */
349 /* TID_MAX_TIME_DIFF */
350 if (index >= TID_QUEUE_MAX_SIZE)
351 rs_tl_rm_old_stats(tl, curr_time);
352
353 index = (tl->head + index) % TID_QUEUE_MAX_SIZE;
354 tl->packet_count[index] = tl->packet_count[index] + 1;
355 tl->total = tl->total + 1;
356
357 if ((index + 1) > tl->queue_count)
358 tl->queue_count = index + 1;
Ron Rindjunskyfaa29712008-06-12 09:46:58 +0800359
360 return tid;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200361}
362
363/*
364 get the traffic load value for tid
365*/
Tomas Winklere227cea2008-07-18 13:53:05 +0800366static u32 rs_tl_get_load(struct iwl_lq_sta *lq_data, u8 tid)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200367{
368 u32 curr_time = jiffies_to_msecs(jiffies);
369 u32 time_diff;
370 s32 index;
Tomas Winklere227cea2008-07-18 13:53:05 +0800371 struct iwl_traffic_load *tl = NULL;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200372
373 if (tid >= TID_MAX_LOAD_COUNT)
374 return 0;
375
376 tl = &(lq_data->load[tid]);
377
378 curr_time -= curr_time % TID_ROUND_VALUE;
379
380 if (!(tl->queue_count))
381 return 0;
382
383 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
384 index = time_diff / TID_QUEUE_CELL_SPACING;
385
386 /* The history is too long: remove data that is older than */
387 /* TID_MAX_TIME_DIFF */
388 if (index >= TID_QUEUE_MAX_SIZE)
389 rs_tl_rm_old_stats(tl, curr_time);
390
391 return tl->total;
392}
393
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700394static void rs_tl_turn_on_agg_for_tid(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +0800395 struct iwl_lq_sta *lq_data, u8 tid,
Johannes Berg4b7679a2008-09-18 18:14:18 +0200396 struct ieee80211_sta *sta)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200397{
Johannes Bergff550cb2008-09-11 03:17:05 +0200398 if (rs_tl_get_load(lq_data, tid) > IWL_AGG_LOAD_THRESHOLD) {
Tomas Winklere1623442009-01-27 14:27:56 -0800399 IWL_DEBUG_HT(priv, "Starting Tx agg: STA: %pM tid: %d\n",
Johannes Berge1749612008-10-27 15:59:26 -0700400 sta->addr, tid);
Johannes Berg4b7679a2008-09-18 18:14:18 +0200401 ieee80211_start_tx_ba_session(priv->hw, sta->addr, tid);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200402 }
403}
404
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700405static void rs_tl_turn_on_agg(struct iwl_priv *priv, u8 tid,
Tomas Winklere227cea2008-07-18 13:53:05 +0800406 struct iwl_lq_sta *lq_data,
Johannes Berg4b7679a2008-09-18 18:14:18 +0200407 struct ieee80211_sta *sta)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200408{
409 if ((tid < TID_MAX_LOAD_COUNT))
410 rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
411 else if (tid == IWL_AGG_ALL_TID)
412 for (tid = 0; tid < TID_MAX_LOAD_COUNT; tid++)
413 rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
414}
415
Guy Cohen39e88502008-04-23 17:14:57 -0700416static inline int get_num_of_ant_from_rate(u32 rate_n_flags)
Guy Cohenfde0db32008-04-21 15:42:01 -0700417{
Tomas Winkler3ac7f142008-07-21 02:40:14 +0300418 return !!(rate_n_flags & RATE_MCS_ANT_A_MSK) +
419 !!(rate_n_flags & RATE_MCS_ANT_B_MSK) +
420 !!(rate_n_flags & RATE_MCS_ANT_C_MSK);
Guy Cohenfde0db32008-04-21 15:42:01 -0700421}
422
Ben Cahill77626352007-11-29 11:09:44 +0800423/**
424 * rs_collect_tx_data - Update the success/failure sliding window
425 *
426 * We keep a sliding window of the last 62 packets transmitted
427 * at this rate. window->data contains the bitmask of successful
428 * packets.
429 */
Tomas Winklere227cea2008-07-18 13:53:05 +0800430static int rs_collect_tx_data(struct iwl_rate_scale_data *windows,
Ron Rindjunsky99556432008-01-28 14:07:25 +0200431 int scale_index, s32 tpt, int retries,
432 int successes)
Zhu Yib481de92007-09-25 17:54:57 -0700433{
Tomas Winklere227cea2008-07-18 13:53:05 +0800434 struct iwl_rate_scale_data *window = NULL;
Guy Cohen39e88502008-04-23 17:14:57 -0700435 static const u64 mask = (((u64)1) << (IWL_RATE_MAX_WINDOW - 1));
Zhu Yib481de92007-09-25 17:54:57 -0700436 s32 fail_count;
437
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800438 if (scale_index < 0 || scale_index >= IWL_RATE_COUNT)
439 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -0700440
Ben Cahill77626352007-11-29 11:09:44 +0800441 /* Select data for current tx bit rate */
Zhu Yib481de92007-09-25 17:54:57 -0700442 window = &(windows[scale_index]);
443
Ben Cahill77626352007-11-29 11:09:44 +0800444 /*
445 * Keep track of only the latest 62 tx frame attempts in this rate's
446 * history window; anything older isn't really relevant any more.
447 * If we have filled up the sliding window, drop the oldest attempt;
448 * if the oldest attempt (highest bit in bitmap) shows "success",
449 * subtract "1" from the success counter (this is the main reason
450 * we keep these bitmaps!).
451 */
Ron Rindjunsky99556432008-01-28 14:07:25 +0200452 while (retries > 0) {
Guy Cohen39e88502008-04-23 17:14:57 -0700453 if (window->counter >= IWL_RATE_MAX_WINDOW) {
454
455 /* remove earliest */
456 window->counter = IWL_RATE_MAX_WINDOW - 1;
457
Ron Rindjunsky99556432008-01-28 14:07:25 +0200458 if (window->data & mask) {
459 window->data &= ~mask;
Guy Cohen39e88502008-04-23 17:14:57 -0700460 window->success_counter--;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200461 }
Zhu Yib481de92007-09-25 17:54:57 -0700462 }
Zhu Yib481de92007-09-25 17:54:57 -0700463
Ron Rindjunsky99556432008-01-28 14:07:25 +0200464 /* Increment frames-attempted counter */
465 window->counter++;
Ben Cahill77626352007-11-29 11:09:44 +0800466
Ron Rindjunsky99556432008-01-28 14:07:25 +0200467 /* Shift bitmap by one frame (throw away oldest history),
468 * OR in "1", and increment "success" if this
469 * frame was successful. */
Guy Cohen90d77952008-09-09 10:54:53 +0800470 window->data <<= 1;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200471 if (successes > 0) {
Guy Cohen39e88502008-04-23 17:14:57 -0700472 window->success_counter++;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200473 window->data |= 0x1;
474 successes--;
475 }
476
477 retries--;
Zhu Yib481de92007-09-25 17:54:57 -0700478 }
479
Ben Cahill77626352007-11-29 11:09:44 +0800480 /* Calculate current success ratio, avoid divide-by-0! */
Zhu Yib481de92007-09-25 17:54:57 -0700481 if (window->counter > 0)
482 window->success_ratio = 128 * (100 * window->success_counter)
483 / window->counter;
484 else
485 window->success_ratio = IWL_INVALID_VALUE;
486
487 fail_count = window->counter - window->success_counter;
488
Ben Cahill77626352007-11-29 11:09:44 +0800489 /* Calculate average throughput, if we have enough history. */
Zhu Yib481de92007-09-25 17:54:57 -0700490 if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
491 (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
492 window->average_tpt = (window->success_ratio * tpt + 64) / 128;
493 else
494 window->average_tpt = IWL_INVALID_VALUE;
495
Ben Cahill77626352007-11-29 11:09:44 +0800496 /* Tag this window as having been updated */
Zhu Yib481de92007-09-25 17:54:57 -0700497 window->stamp = jiffies;
498
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800499 return 0;
Zhu Yib481de92007-09-25 17:54:57 -0700500}
501
Ben Cahill77626352007-11-29 11:09:44 +0800502/*
503 * Fill uCode API rate_n_flags field, based on "search" or "active" table.
504 */
Guy Cohen39e88502008-04-23 17:14:57 -0700505/* FIXME:RS:remove this function and put the flags statically in the table */
Tomas Winkler978785a2008-12-19 10:37:31 +0800506static u32 rate_n_flags_from_tbl(struct iwl_priv *priv,
507 struct iwl_scale_tbl_info *tbl,
508 int index, u8 use_green)
Zhu Yib481de92007-09-25 17:54:57 -0700509{
Guy Cohen39e88502008-04-23 17:14:57 -0700510 u32 rate_n_flags = 0;
511
Zhu Yib481de92007-09-25 17:54:57 -0700512 if (is_legacy(tbl->lq_type)) {
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800513 rate_n_flags = iwl_rates[index].plcp;
Zhu Yib481de92007-09-25 17:54:57 -0700514 if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
Guy Cohen39e88502008-04-23 17:14:57 -0700515 rate_n_flags |= RATE_MCS_CCK_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700516
Guy Cohenfde0db32008-04-21 15:42:01 -0700517 } else if (is_Ht(tbl->lq_type)) {
518 if (index > IWL_LAST_OFDM_RATE) {
Tomas Winkler978785a2008-12-19 10:37:31 +0800519 IWL_ERR(priv, "Invalid HT rate index %d\n", index);
Zhu Yib481de92007-09-25 17:54:57 -0700520 index = IWL_LAST_OFDM_RATE;
Guy Cohenfde0db32008-04-21 15:42:01 -0700521 }
Guy Cohen39e88502008-04-23 17:14:57 -0700522 rate_n_flags = RATE_MCS_HT_MSK;
Guy Cohenfde0db32008-04-21 15:42:01 -0700523
524 if (is_siso(tbl->lq_type))
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800525 rate_n_flags |= iwl_rates[index].plcp_siso;
Guy Cohenfde0db32008-04-21 15:42:01 -0700526 else if (is_mimo2(tbl->lq_type))
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800527 rate_n_flags |= iwl_rates[index].plcp_mimo2;
Guy Cohenfde0db32008-04-21 15:42:01 -0700528 else
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800529 rate_n_flags |= iwl_rates[index].plcp_mimo3;
Zhu Yib481de92007-09-25 17:54:57 -0700530 } else {
Tomas Winkler978785a2008-12-19 10:37:31 +0800531 IWL_ERR(priv, "Invalid tbl->lq_type %d\n", tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -0700532 }
533
Guy Cohen39e88502008-04-23 17:14:57 -0700534 rate_n_flags |= ((tbl->ant_type << RATE_MCS_ANT_POS) &
Guy Cohenfde0db32008-04-21 15:42:01 -0700535 RATE_MCS_ANT_ABC_MSK);
Zhu Yib481de92007-09-25 17:54:57 -0700536
Guy Cohen39e88502008-04-23 17:14:57 -0700537 if (is_Ht(tbl->lq_type)) {
538 if (tbl->is_fat) {
539 if (tbl->is_dup)
540 rate_n_flags |= RATE_MCS_DUP_MSK;
541 else
542 rate_n_flags |= RATE_MCS_FAT_MSK;
543 }
544 if (tbl->is_SGI)
545 rate_n_flags |= RATE_MCS_SGI_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700546
Guy Cohen39e88502008-04-23 17:14:57 -0700547 if (use_green) {
548 rate_n_flags |= RATE_MCS_GF_MSK;
549 if (is_siso(tbl->lq_type) && tbl->is_SGI) {
550 rate_n_flags &= ~RATE_MCS_SGI_MSK;
Tomas Winkler978785a2008-12-19 10:37:31 +0800551 IWL_ERR(priv, "GF was set with SGI:SISO\n");
Guy Cohen39e88502008-04-23 17:14:57 -0700552 }
553 }
Zhu Yib481de92007-09-25 17:54:57 -0700554 }
Guy Cohen39e88502008-04-23 17:14:57 -0700555 return rate_n_flags;
Zhu Yib481de92007-09-25 17:54:57 -0700556}
557
Ben Cahill77626352007-11-29 11:09:44 +0800558/*
559 * Interpret uCode API's rate_n_flags format,
560 * fill "search" or "active" tx mode table.
561 */
Guy Cohen39e88502008-04-23 17:14:57 -0700562static int rs_get_tbl_info_from_mcs(const u32 rate_n_flags,
Johannes Berg8318d782008-01-24 19:38:38 +0100563 enum ieee80211_band band,
Tomas Winklere227cea2008-07-18 13:53:05 +0800564 struct iwl_scale_tbl_info *tbl,
Zhu Yib481de92007-09-25 17:54:57 -0700565 int *rate_idx)
566{
Guy Cohen39e88502008-04-23 17:14:57 -0700567 u32 ant_msk = (rate_n_flags & RATE_MCS_ANT_ABC_MSK);
568 u8 num_of_ant = get_num_of_ant_from_rate(rate_n_flags);
569 u8 mcs;
Zhu Yib481de92007-09-25 17:54:57 -0700570
Tomas Winklere7d326ac2008-06-12 09:47:11 +0800571 *rate_idx = iwl_hwrate_to_plcp_idx(rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -0700572
Guy Cohenfde0db32008-04-21 15:42:01 -0700573 if (*rate_idx == IWL_RATE_INVALID) {
Zhu Yib481de92007-09-25 17:54:57 -0700574 *rate_idx = -1;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800575 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -0700576 }
Ben Cahill77626352007-11-29 11:09:44 +0800577 tbl->is_SGI = 0; /* default legacy setup */
Zhu Yib481de92007-09-25 17:54:57 -0700578 tbl->is_fat = 0;
579 tbl->is_dup = 0;
Guy Cohenfde0db32008-04-21 15:42:01 -0700580 tbl->ant_type = (ant_msk >> RATE_MCS_ANT_POS);
581 tbl->lq_type = LQ_NONE;
Zhu Yib481de92007-09-25 17:54:57 -0700582
Ben Cahill77626352007-11-29 11:09:44 +0800583 /* legacy rate format */
Guy Cohen39e88502008-04-23 17:14:57 -0700584 if (!(rate_n_flags & RATE_MCS_HT_MSK)) {
Guy Cohenfde0db32008-04-21 15:42:01 -0700585 if (num_of_ant == 1) {
Johannes Berg8318d782008-01-24 19:38:38 +0100586 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700587 tbl->lq_type = LQ_A;
588 else
589 tbl->lq_type = LQ_G;
Zhu Yib481de92007-09-25 17:54:57 -0700590 }
Guy Cohenfde0db32008-04-21 15:42:01 -0700591 /* HT rate format */
Zhu Yib481de92007-09-25 17:54:57 -0700592 } else {
Guy Cohen39e88502008-04-23 17:14:57 -0700593 if (rate_n_flags & RATE_MCS_SGI_MSK)
Zhu Yib481de92007-09-25 17:54:57 -0700594 tbl->is_SGI = 1;
595
Guy Cohen39e88502008-04-23 17:14:57 -0700596 if ((rate_n_flags & RATE_MCS_FAT_MSK) ||
597 (rate_n_flags & RATE_MCS_DUP_MSK))
Zhu Yib481de92007-09-25 17:54:57 -0700598 tbl->is_fat = 1;
599
Guy Cohen39e88502008-04-23 17:14:57 -0700600 if (rate_n_flags & RATE_MCS_DUP_MSK)
Zhu Yib481de92007-09-25 17:54:57 -0700601 tbl->is_dup = 1;
Guy Cohenfde0db32008-04-21 15:42:01 -0700602
Guy Cohen39e88502008-04-23 17:14:57 -0700603 mcs = rs_extract_rate(rate_n_flags);
Guy Cohenfde0db32008-04-21 15:42:01 -0700604
Guy Cohen39e88502008-04-23 17:14:57 -0700605 /* SISO */
606 if (mcs <= IWL_RATE_SISO_60M_PLCP) {
Guy Cohenfde0db32008-04-21 15:42:01 -0700607 if (num_of_ant == 1)
608 tbl->lq_type = LQ_SISO; /*else NONE*/
609 /* MIMO2 */
Guy Cohen39e88502008-04-23 17:14:57 -0700610 } else if (mcs <= IWL_RATE_MIMO2_60M_PLCP) {
Guy Cohenfde0db32008-04-21 15:42:01 -0700611 if (num_of_ant == 2)
612 tbl->lq_type = LQ_MIMO2;
613 /* MIMO3 */
614 } else {
615 if (num_of_ant == 3)
616 tbl->lq_type = LQ_MIMO3;
617 }
Zhu Yib481de92007-09-25 17:54:57 -0700618 }
619 return 0;
620}
Guy Cohenaade00c2008-04-23 17:14:59 -0700621
622/* switch to another antenna/antennas and return 1 */
623/* if no other valid antenna found, return 0 */
624static int rs_toggle_antenna(u32 valid_ant, u32 *rate_n_flags,
Tomas Winklere227cea2008-07-18 13:53:05 +0800625 struct iwl_scale_tbl_info *tbl)
Zhu Yib481de92007-09-25 17:54:57 -0700626{
Guy Cohenaade00c2008-04-23 17:14:59 -0700627 u8 new_ant_type;
628
629 if (!tbl->ant_type || tbl->ant_type > ANT_ABC)
630 return 0;
631
632 if (!rs_is_valid_ant(valid_ant, tbl->ant_type))
633 return 0;
634
635 new_ant_type = ant_toggle_lookup[tbl->ant_type];
636
637 while ((new_ant_type != tbl->ant_type) &&
638 !rs_is_valid_ant(valid_ant, new_ant_type))
639 new_ant_type = ant_toggle_lookup[new_ant_type];
640
641 if (new_ant_type == tbl->ant_type)
642 return 0;
643
644 tbl->ant_type = new_ant_type;
645 *rate_n_flags &= ~RATE_MCS_ANT_ABC_MSK;
646 *rate_n_flags |= new_ant_type << RATE_MCS_ANT_POS;
647 return 1;
Zhu Yib481de92007-09-25 17:54:57 -0700648}
649
Guy Cohen39e88502008-04-23 17:14:57 -0700650/* FIXME:RS: in 4965 we don't use greenfield at all */
Guy Cohenf935a6d2008-04-23 17:15:02 -0700651/* FIXME:RS: don't use greenfield for now in TX */
Guy Cohenf935a6d2008-04-23 17:15:02 -0700652#if 0
653static inline u8 rs_use_green(struct iwl_priv *priv, struct ieee80211_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -0700654{
Tomas Winkler3ac7f142008-07-21 02:40:14 +0300655 return (conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) &&
Ron Rindjunsky270243a2007-11-26 16:14:41 +0200656 priv->current_ht_config.is_green_field &&
Tomas Winkler3ac7f142008-07-21 02:40:14 +0300657 !priv->current_ht_config.non_GF_STA_present;
Zhu Yib481de92007-09-25 17:54:57 -0700658}
Ron Rindjunsky4f85f5b2008-06-09 22:54:35 +0300659#endif
Guy Cohenf935a6d2008-04-23 17:15:02 -0700660static inline u8 rs_use_green(struct iwl_priv *priv, struct ieee80211_conf *conf)
661{
662 return 0;
663}
Zhu Yib481de92007-09-25 17:54:57 -0700664
665/**
666 * rs_get_supported_rates - get the available rates
667 *
668 * if management frame or broadcast frame only return
669 * basic available rates.
670 *
671 */
Tomas Winklere227cea2008-07-18 13:53:05 +0800672static u16 rs_get_supported_rates(struct iwl_lq_sta *lq_sta,
673 struct ieee80211_hdr *hdr,
674 enum iwl_table_type rate_type)
Zhu Yib481de92007-09-25 17:54:57 -0700675{
Zhu Yib481de92007-09-25 17:54:57 -0700676 if (hdr && is_multicast_ether_addr(hdr->addr1) &&
Guy Coheneecd6e52008-04-23 17:14:58 -0700677 lq_sta->active_rate_basic)
678 return lq_sta->active_rate_basic;
679
680 if (is_legacy(rate_type)) {
681 return lq_sta->active_legacy_rate;
682 } else {
683 if (is_siso(rate_type))
684 return lq_sta->active_siso_rate;
685 else if (is_mimo2(rate_type))
686 return lq_sta->active_mimo2_rate;
687 else
688 return lq_sta->active_mimo3_rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -0800689 }
Zhu Yib481de92007-09-25 17:54:57 -0700690}
691
Ester Kummerbf403db2008-05-05 10:22:40 +0800692static u16 rs_get_adjacent_rate(struct iwl_priv *priv, u8 index, u16 rate_mask,
693 int rate_type)
Zhu Yib481de92007-09-25 17:54:57 -0700694{
695 u8 high = IWL_RATE_INVALID;
696 u8 low = IWL_RATE_INVALID;
697
Ian Schram01ebd062007-10-25 17:15:22 +0800698 /* 802.11A or ht walks to the next literal adjacent rate in
Zhu Yib481de92007-09-25 17:54:57 -0700699 * the rate table */
700 if (is_a_band(rate_type) || !is_legacy(rate_type)) {
701 int i;
702 u32 mask;
703
704 /* Find the previous rate that is in the rate mask */
705 i = index - 1;
706 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
707 if (rate_mask & mask) {
708 low = i;
709 break;
710 }
711 }
712
713 /* Find the next rate that is in the rate mask */
714 i = index + 1;
715 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
716 if (rate_mask & mask) {
717 high = i;
718 break;
719 }
720 }
721
722 return (high << 8) | low;
723 }
724
725 low = index;
726 while (low != IWL_RATE_INVALID) {
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800727 low = iwl_rates[low].prev_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700728 if (low == IWL_RATE_INVALID)
729 break;
730 if (rate_mask & (1 << low))
731 break;
Tomas Winklere1623442009-01-27 14:27:56 -0800732 IWL_DEBUG_RATE(priv, "Skipping masked lower rate: %d\n", low);
Zhu Yib481de92007-09-25 17:54:57 -0700733 }
734
735 high = index;
736 while (high != IWL_RATE_INVALID) {
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800737 high = iwl_rates[high].next_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700738 if (high == IWL_RATE_INVALID)
739 break;
740 if (rate_mask & (1 << high))
741 break;
Tomas Winklere1623442009-01-27 14:27:56 -0800742 IWL_DEBUG_RATE(priv, "Skipping masked higher rate: %d\n", high);
Zhu Yib481de92007-09-25 17:54:57 -0700743 }
744
745 return (high << 8) | low;
746}
747
Tomas Winklere227cea2008-07-18 13:53:05 +0800748static u32 rs_get_lower_rate(struct iwl_lq_sta *lq_sta,
749 struct iwl_scale_tbl_info *tbl,
750 u8 scale_index, u8 ht_possible)
Zhu Yib481de92007-09-25 17:54:57 -0700751{
Zhu Yib481de92007-09-25 17:54:57 -0700752 s32 low;
753 u16 rate_mask;
754 u16 high_low;
755 u8 switch_to_legacy = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -0800756 u8 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -0700757
758 /* check if we need to switch from HT to legacy rates.
759 * assumption is that mandatory rates (1Mbps or 6Mbps)
760 * are always supported (spec demand) */
761 if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_index)) {
762 switch_to_legacy = 1;
763 scale_index = rs_ht_to_legacy[scale_index];
Johannes Berg8318d782008-01-24 19:38:38 +0100764 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700765 tbl->lq_type = LQ_A;
766 else
767 tbl->lq_type = LQ_G;
768
Guy Cohen69fdb302008-04-23 17:15:01 -0700769 if (num_of_ant(tbl->ant_type) > 1)
Guy Cohenfde0db32008-04-21 15:42:01 -0700770 tbl->ant_type = ANT_A;/*FIXME:RS*/
Zhu Yib481de92007-09-25 17:54:57 -0700771
772 tbl->is_fat = 0;
773 tbl->is_SGI = 0;
774 }
775
Guy Coheneecd6e52008-04-23 17:14:58 -0700776 rate_mask = rs_get_supported_rates(lq_sta, NULL, tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -0700777
Ben Cahill77626352007-11-29 11:09:44 +0800778 /* Mask with station rate restriction */
Zhu Yib481de92007-09-25 17:54:57 -0700779 if (is_legacy(tbl->lq_type)) {
Ben Cahill77626352007-11-29 11:09:44 +0800780 /* supp_rates has no CCK bits in A mode */
Johannes Berg8318d782008-01-24 19:38:38 +0100781 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700782 rate_mask = (u16)(rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -0800783 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
Zhu Yib481de92007-09-25 17:54:57 -0700784 else
Tomas Winklerc33104f2008-01-14 17:46:21 -0800785 rate_mask = (u16)(rate_mask & lq_sta->supp_rates);
Zhu Yib481de92007-09-25 17:54:57 -0700786 }
787
Ben Cahill77626352007-11-29 11:09:44 +0800788 /* If we switched from HT to legacy, check current rate */
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800789 if (switch_to_legacy && (rate_mask & (1 << scale_index))) {
Guy Cohen39e88502008-04-23 17:14:57 -0700790 low = scale_index;
791 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700792 }
793
Ester Kummerbf403db2008-05-05 10:22:40 +0800794 high_low = rs_get_adjacent_rate(lq_sta->drv, scale_index, rate_mask,
795 tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -0700796 low = high_low & 0xff;
797
Guy Cohen39e88502008-04-23 17:14:57 -0700798 if (low == IWL_RATE_INVALID)
799 low = scale_index;
800
801out:
Tomas Winkler978785a2008-12-19 10:37:31 +0800802 return rate_n_flags_from_tbl(lq_sta->drv, tbl, low, is_green);
Zhu Yib481de92007-09-25 17:54:57 -0700803}
804
Ben Cahill77626352007-11-29 11:09:44 +0800805/*
806 * mac80211 sends us Tx status
807 */
Johannes Berg4b7679a2008-09-18 18:14:18 +0200808static void rs_tx_status(void *priv_r, struct ieee80211_supported_band *sband,
809 struct ieee80211_sta *sta, void *priv_sta,
Johannes Berge039fa42008-05-15 12:55:29 +0200810 struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -0700811{
812 int status;
813 u8 retries;
814 int rs_index, index = 0;
Tomas Winkler417f1142008-11-12 13:14:06 -0800815 struct iwl_lq_sta *lq_sta = priv_sta;
Tomas Winkler66c73db2008-04-15 16:01:40 -0700816 struct iwl_link_quality_cmd *table;
Zhu Yib481de92007-09-25 17:54:57 -0700817 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berg4b7679a2008-09-18 18:14:18 +0200818 struct iwl_priv *priv = (struct iwl_priv *)priv_r;
819 struct ieee80211_hw *hw = priv->hw;
Johannes Berge039fa42008-05-15 12:55:29 +0200820 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Tomas Winklere227cea2008-07-18 13:53:05 +0800821 struct iwl_rate_scale_data *window = NULL;
822 struct iwl_rate_scale_data *search_win = NULL;
Guy Cohen39e88502008-04-23 17:14:57 -0700823 u32 tx_rate;
Tomas Winklere227cea2008-07-18 13:53:05 +0800824 struct iwl_scale_tbl_info tbl_type;
825 struct iwl_scale_tbl_info *curr_tbl, *search_tbl;
Zhu Yib481de92007-09-25 17:54:57 -0700826 u8 active_index = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700827 s32 tpt = 0;
828
Tomas Winklere1623442009-01-27 14:27:56 -0800829 IWL_DEBUG_RATE_LIMIT(priv, "get frame ack response, update rate scale window\n");
Zhu Yib481de92007-09-25 17:54:57 -0700830
Tomas Winkler417f1142008-11-12 13:14:06 -0800831 if (!ieee80211_is_data(hdr->frame_control) ||
Gábor Stefanik514d65c2009-04-23 19:36:08 +0200832 info->flags & IEEE80211_TX_CTL_NO_ACK)
Zhu Yib481de92007-09-25 17:54:57 -0700833 return;
834
Ron Rindjunsky99556432008-01-28 14:07:25 +0200835 /* This packet was aggregated but doesn't carry rate scale info */
Johannes Berge039fa42008-05-15 12:55:29 +0200836 if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
837 !(info->flags & IEEE80211_TX_STAT_AMPDU))
Ron Rindjunsky99556432008-01-28 14:07:25 +0200838 return;
839
Wey-Yi Guy8fe72312009-03-10 14:35:10 -0700840 if (info->flags & IEEE80211_TX_STAT_AMPDU)
841 retries = 0;
842 else
843 retries = info->status.rates[0].count - 1;
Zhu Yib481de92007-09-25 17:54:57 -0700844
845 if (retries > 15)
846 retries = 15;
847
Johannes Berg05c914f2008-09-11 00:01:58 +0200848 if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) &&
Tomas Winklerc33104f2008-01-14 17:46:21 -0800849 !lq_sta->ibss_sta_added)
Tomas Winklerf4d60822008-03-05 11:31:00 -0800850 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700851
Tomas Winklerc33104f2008-01-14 17:46:21 -0800852 table = &lq_sta->lq;
853 active_index = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -0700854
Tomas Winklerc33104f2008-01-14 17:46:21 -0800855 curr_tbl = &(lq_sta->lq_info[active_index]);
856 search_tbl = &(lq_sta->lq_info[(1 - active_index)]);
Tomas Winklere227cea2008-07-18 13:53:05 +0800857 window = (struct iwl_rate_scale_data *)&(curr_tbl->win[0]);
858 search_win = (struct iwl_rate_scale_data *)&(search_tbl->win[0]);
Zhu Yib481de92007-09-25 17:54:57 -0700859
Ben Cahill77626352007-11-29 11:09:44 +0800860 /*
861 * Ignore this Tx frame response if its initial rate doesn't match
862 * that of latest Link Quality command. There may be stragglers
863 * from a previous Link Quality command, but we're no longer interested
864 * in those; they're either from the "active" mode while we're trying
865 * to check "search" mode, or a prior "search" mode after we've moved
866 * to a new "search" mode (which might become the new "active" mode).
867 */
Guy Cohen39e88502008-04-23 17:14:57 -0700868 tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
869 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800870 if (priv->band == IEEE80211_BAND_5GHZ)
871 rs_index -= IWL_FIRST_OFDM_RATE;
872
Johannes Berge6a98542008-10-21 12:40:02 +0200873 if ((info->status.rates[0].idx < 0) ||
874 (tbl_type.is_SGI != !!(info->status.rates[0].flags & IEEE80211_TX_RC_SHORT_GI)) ||
875 (tbl_type.is_fat != !!(info->status.rates[0].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)) ||
876 (tbl_type.is_dup != !!(info->status.rates[0].flags & IEEE80211_TX_RC_DUP_DATA)) ||
877 (tbl_type.ant_type != info->antenna_sel_tx) ||
878 (!!(tx_rate & RATE_MCS_HT_MSK) != !!(info->status.rates[0].flags & IEEE80211_TX_RC_MCS)) ||
879 (!!(tx_rate & RATE_MCS_GF_MSK) != !!(info->status.rates[0].flags & IEEE80211_TX_RC_GREEN_FIELD)) ||
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800880 (hw->wiphy->bands[priv->band]->bitrates[rs_index].bitrate !=
Johannes Berge6a98542008-10-21 12:40:02 +0200881 hw->wiphy->bands[info->band]->bitrates[info->status.rates[0].idx].bitrate)) {
Tomas Winklere1623442009-01-27 14:27:56 -0800882 IWL_DEBUG_RATE(priv, "initial rate does not match 0x%x\n", tx_rate);
Mohamed Abbasd5e49032008-12-12 08:22:15 -0800883 /* the last LQ command could failed so the LQ in ucode not
884 * the same in driver sync up
885 */
Abbas, Mohamed7d049e52009-01-20 21:33:53 -0800886 lq_sta->missed_rate_counter++;
887 if (lq_sta->missed_rate_counter > IWL_MISSED_RATE_MAX) {
888 lq_sta->missed_rate_counter = 0;
889 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
890 }
Tomas Winklerf4d60822008-03-05 11:31:00 -0800891 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700892 }
893
Abbas, Mohamed7d049e52009-01-20 21:33:53 -0800894 lq_sta->missed_rate_counter = 0;
Ben Cahill77626352007-11-29 11:09:44 +0800895 /* Update frame history window with "failure" for each Tx retry. */
Zhu Yib481de92007-09-25 17:54:57 -0700896 while (retries) {
Ben Cahill77626352007-11-29 11:09:44 +0800897 /* Look up the rate and other info used for each tx attempt.
898 * Each tx attempt steps one entry deeper in the rate table. */
Guy Cohen39e88502008-04-23 17:14:57 -0700899 tx_rate = le32_to_cpu(table->rs_table[index].rate_n_flags);
900 rs_get_tbl_info_from_mcs(tx_rate, priv->band,
Zhu Yib481de92007-09-25 17:54:57 -0700901 &tbl_type, &rs_index);
902
Ben Cahill77626352007-11-29 11:09:44 +0800903 /* If type matches "search" table,
904 * add failure to "search" history */
Zhu Yib481de92007-09-25 17:54:57 -0700905 if ((tbl_type.lq_type == search_tbl->lq_type) &&
Guy Cohenfde0db32008-04-21 15:42:01 -0700906 (tbl_type.ant_type == search_tbl->ant_type) &&
Zhu Yib481de92007-09-25 17:54:57 -0700907 (tbl_type.is_SGI == search_tbl->is_SGI)) {
908 if (search_tbl->expected_tpt)
909 tpt = search_tbl->expected_tpt[rs_index];
910 else
911 tpt = 0;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200912 rs_collect_tx_data(search_win, rs_index, tpt, 1, 0);
Ben Cahill77626352007-11-29 11:09:44 +0800913
914 /* Else if type matches "current/active" table,
915 * add failure to "current/active" history */
Zhu Yib481de92007-09-25 17:54:57 -0700916 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
Guy Cohenfde0db32008-04-21 15:42:01 -0700917 (tbl_type.ant_type == curr_tbl->ant_type) &&
Zhu Yib481de92007-09-25 17:54:57 -0700918 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
919 if (curr_tbl->expected_tpt)
920 tpt = curr_tbl->expected_tpt[rs_index];
921 else
922 tpt = 0;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200923 rs_collect_tx_data(window, rs_index, tpt, 1, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700924 }
Ben Cahill77626352007-11-29 11:09:44 +0800925
926 /* If not searching for a new mode, increment failed counter
927 * ... this helps determine when to start searching again */
Tomas Winklerc33104f2008-01-14 17:46:21 -0800928 if (lq_sta->stay_in_tbl)
929 lq_sta->total_failed++;
Zhu Yib481de92007-09-25 17:54:57 -0700930 --retries;
931 index++;
932
933 }
934
Ben Cahill77626352007-11-29 11:09:44 +0800935 /*
936 * Find (by rate) the history window to update with final Tx attempt;
937 * if Tx was successful first try, use original rate,
938 * else look up the rate that was, finally, successful.
939 */
Guy Cohen39e88502008-04-23 17:14:57 -0700940 tx_rate = le32_to_cpu(table->rs_table[index].rate_n_flags);
Wey-Yi Guy12b96812009-04-08 11:39:27 -0700941 lq_sta->last_rate_n_flags = tx_rate;
Guy Cohen39e88502008-04-23 17:14:57 -0700942 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
Zhu Yib481de92007-09-25 17:54:57 -0700943
Ben Cahill77626352007-11-29 11:09:44 +0800944 /* Update frame history window with "success" if Tx got ACKed ... */
Johannes Berge039fa42008-05-15 12:55:29 +0200945 status = !!(info->flags & IEEE80211_TX_STAT_ACK);
Zhu Yib481de92007-09-25 17:54:57 -0700946
Ben Cahill77626352007-11-29 11:09:44 +0800947 /* If type matches "search" table,
948 * add final tx status to "search" history */
Zhu Yib481de92007-09-25 17:54:57 -0700949 if ((tbl_type.lq_type == search_tbl->lq_type) &&
Guy Cohenfde0db32008-04-21 15:42:01 -0700950 (tbl_type.ant_type == search_tbl->ant_type) &&
Zhu Yib481de92007-09-25 17:54:57 -0700951 (tbl_type.is_SGI == search_tbl->is_SGI)) {
952 if (search_tbl->expected_tpt)
953 tpt = search_tbl->expected_tpt[rs_index];
954 else
955 tpt = 0;
Wey-Yi Guydf36c042009-03-10 14:35:11 -0700956 if (info->flags & IEEE80211_TX_STAT_AMPDU)
Ron Rindjunsky99556432008-01-28 14:07:25 +0200957 rs_collect_tx_data(search_win, rs_index, tpt,
Johannes Berge039fa42008-05-15 12:55:29 +0200958 info->status.ampdu_ack_len,
959 info->status.ampdu_ack_map);
Ron Rindjunsky99556432008-01-28 14:07:25 +0200960 else
961 rs_collect_tx_data(search_win, rs_index, tpt,
962 1, status);
Ben Cahill77626352007-11-29 11:09:44 +0800963 /* Else if type matches "current/active" table,
964 * add final tx status to "current/active" history */
Zhu Yib481de92007-09-25 17:54:57 -0700965 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
Guy Cohenfde0db32008-04-21 15:42:01 -0700966 (tbl_type.ant_type == curr_tbl->ant_type) &&
Zhu Yib481de92007-09-25 17:54:57 -0700967 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
968 if (curr_tbl->expected_tpt)
969 tpt = curr_tbl->expected_tpt[rs_index];
970 else
971 tpt = 0;
Wey-Yi Guydf36c042009-03-10 14:35:11 -0700972 if (info->flags & IEEE80211_TX_STAT_AMPDU)
Ron Rindjunsky99556432008-01-28 14:07:25 +0200973 rs_collect_tx_data(window, rs_index, tpt,
Johannes Berge039fa42008-05-15 12:55:29 +0200974 info->status.ampdu_ack_len,
975 info->status.ampdu_ack_map);
Ron Rindjunsky99556432008-01-28 14:07:25 +0200976 else
977 rs_collect_tx_data(window, rs_index, tpt,
978 1, status);
Zhu Yib481de92007-09-25 17:54:57 -0700979 }
980
Ben Cahill77626352007-11-29 11:09:44 +0800981 /* If not searching for new mode, increment success/failed counter
982 * ... these help determine when to start searching again */
Tomas Winklerc33104f2008-01-14 17:46:21 -0800983 if (lq_sta->stay_in_tbl) {
Wey-Yi Guydf36c042009-03-10 14:35:11 -0700984 if (info->flags & IEEE80211_TX_STAT_AMPDU) {
Johannes Berge039fa42008-05-15 12:55:29 +0200985 lq_sta->total_success += info->status.ampdu_ack_map;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200986 lq_sta->total_failed +=
Johannes Berge039fa42008-05-15 12:55:29 +0200987 (info->status.ampdu_ack_len - info->status.ampdu_ack_map);
Ron Rindjunsky99556432008-01-28 14:07:25 +0200988 } else {
989 if (status)
990 lq_sta->total_success++;
991 else
992 lq_sta->total_failed++;
993 }
Zhu Yib481de92007-09-25 17:54:57 -0700994 }
995
Ben Cahill77626352007-11-29 11:09:44 +0800996 /* See if there's a better rate or modulation mode to try. */
Abbas, Mohamedc338ba32009-01-21 10:58:02 -0800997 if (sta && sta->supp_rates[sband->band])
Gábor Stefanik514d65c2009-04-23 19:36:08 +0200998 rs_rate_scale_perform(priv, skb, sta, lq_sta);
Tomas Winklerf4d60822008-03-05 11:31:00 -0800999out:
Zhu Yib481de92007-09-25 17:54:57 -07001000 return;
1001}
1002
Ben Cahill77626352007-11-29 11:09:44 +08001003/*
1004 * Begin a period of staying with a selected modulation mode.
1005 * Set "stay_in_tbl" flag to prevent any mode switches.
1006 * Set frame tx success limits according to legacy vs. high-throughput,
1007 * and reset overall (spanning all rates) tx success history statistics.
1008 * These control how long we stay using same modulation mode before
1009 * searching for a new mode.
1010 */
Ester Kummerbf403db2008-05-05 10:22:40 +08001011static void rs_set_stay_in_table(struct iwl_priv *priv, u8 is_legacy,
Tomas Winklere227cea2008-07-18 13:53:05 +08001012 struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07001013{
Tomas Winklere1623442009-01-27 14:27:56 -08001014 IWL_DEBUG_RATE(priv, "we are staying in the same table\n");
Tomas Winklerc33104f2008-01-14 17:46:21 -08001015 lq_sta->stay_in_tbl = 1; /* only place this gets set */
Zhu Yib481de92007-09-25 17:54:57 -07001016 if (is_legacy) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001017 lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT;
1018 lq_sta->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
1019 lq_sta->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
Zhu Yib481de92007-09-25 17:54:57 -07001020 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001021 lq_sta->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
1022 lq_sta->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
1023 lq_sta->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
Zhu Yib481de92007-09-25 17:54:57 -07001024 }
Tomas Winklerc33104f2008-01-14 17:46:21 -08001025 lq_sta->table_count = 0;
1026 lq_sta->total_failed = 0;
1027 lq_sta->total_success = 0;
Mohamed Abbas447fee72009-04-20 14:37:02 -07001028 lq_sta->flush_timer = jiffies;
Zhu Yib481de92007-09-25 17:54:57 -07001029}
1030
Ben Cahill77626352007-11-29 11:09:44 +08001031/*
1032 * Find correct throughput table for given mode of modulation
1033 */
Tomas Winklere227cea2008-07-18 13:53:05 +08001034static void rs_set_expected_tpt_table(struct iwl_lq_sta *lq_sta,
1035 struct iwl_scale_tbl_info *tbl)
Zhu Yib481de92007-09-25 17:54:57 -07001036{
1037 if (is_legacy(tbl->lq_type)) {
1038 if (!is_a_band(tbl->lq_type))
1039 tbl->expected_tpt = expected_tpt_G;
1040 else
1041 tbl->expected_tpt = expected_tpt_A;
1042 } else if (is_siso(tbl->lq_type)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001043 if (tbl->is_fat && !lq_sta->is_dup)
Zhu Yib481de92007-09-25 17:54:57 -07001044 if (tbl->is_SGI)
1045 tbl->expected_tpt = expected_tpt_siso40MHzSGI;
1046 else
1047 tbl->expected_tpt = expected_tpt_siso40MHz;
1048 else if (tbl->is_SGI)
1049 tbl->expected_tpt = expected_tpt_siso20MHzSGI;
1050 else
1051 tbl->expected_tpt = expected_tpt_siso20MHz;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001052 } else if (is_mimo2(tbl->lq_type)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001053 if (tbl->is_fat && !lq_sta->is_dup)
Zhu Yib481de92007-09-25 17:54:57 -07001054 if (tbl->is_SGI)
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001055 tbl->expected_tpt = expected_tpt_mimo2_40MHzSGI;
Zhu Yib481de92007-09-25 17:54:57 -07001056 else
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001057 tbl->expected_tpt = expected_tpt_mimo2_40MHz;
Zhu Yib481de92007-09-25 17:54:57 -07001058 else if (tbl->is_SGI)
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001059 tbl->expected_tpt = expected_tpt_mimo2_20MHzSGI;
Zhu Yib481de92007-09-25 17:54:57 -07001060 else
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001061 tbl->expected_tpt = expected_tpt_mimo2_20MHz;
1062 } else if (is_mimo3(tbl->lq_type)) {
1063 if (tbl->is_fat && !lq_sta->is_dup)
1064 if (tbl->is_SGI)
1065 tbl->expected_tpt = expected_tpt_mimo3_40MHzSGI;
1066 else
1067 tbl->expected_tpt = expected_tpt_mimo3_40MHz;
1068 else if (tbl->is_SGI)
1069 tbl->expected_tpt = expected_tpt_mimo3_20MHzSGI;
1070 else
1071 tbl->expected_tpt = expected_tpt_mimo3_20MHz;
Zhu Yib481de92007-09-25 17:54:57 -07001072 } else
1073 tbl->expected_tpt = expected_tpt_G;
1074}
1075
Ben Cahill77626352007-11-29 11:09:44 +08001076/*
1077 * Find starting rate for new "search" high-throughput mode of modulation.
1078 * Goal is to find lowest expected rate (under perfect conditions) that is
1079 * above the current measured throughput of "active" mode, to give new mode
1080 * a fair chance to prove itself without too many challenges.
1081 *
1082 * This gets called when transitioning to more aggressive modulation
1083 * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
1084 * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
1085 * to decrease to match "active" throughput. When moving from MIMO to SISO,
1086 * bit rate will typically need to increase, but not if performance was bad.
1087 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001088static s32 rs_get_best_rate(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001089 struct iwl_lq_sta *lq_sta,
1090 struct iwl_scale_tbl_info *tbl, /* "search" */
Guy Cohenf935a6d2008-04-23 17:15:02 -07001091 u16 rate_mask, s8 index)
Zhu Yib481de92007-09-25 17:54:57 -07001092{
Ben Cahill77626352007-11-29 11:09:44 +08001093 /* "active" values */
Tomas Winklere227cea2008-07-18 13:53:05 +08001094 struct iwl_scale_tbl_info *active_tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001095 &(lq_sta->lq_info[lq_sta->active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07001096 s32 active_sr = active_tbl->win[index].success_ratio;
Zhu Yib481de92007-09-25 17:54:57 -07001097 s32 active_tpt = active_tbl->expected_tpt[index];
Ben Cahill77626352007-11-29 11:09:44 +08001098
1099 /* expected "search" throughput */
1100 s32 *tpt_tbl = tbl->expected_tpt;
1101
1102 s32 new_rate, high, low, start_hi;
Zhu Yib481de92007-09-25 17:54:57 -07001103 u16 high_low;
Guy Cohenf935a6d2008-04-23 17:15:02 -07001104 s8 rate = index;
Zhu Yib481de92007-09-25 17:54:57 -07001105
1106 new_rate = high = low = start_hi = IWL_RATE_INVALID;
1107
1108 for (; ;) {
Ester Kummerbf403db2008-05-05 10:22:40 +08001109 high_low = rs_get_adjacent_rate(priv, rate, rate_mask,
1110 tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -07001111
1112 low = high_low & 0xff;
1113 high = (high_low >> 8) & 0xff;
1114
Ben Cahill77626352007-11-29 11:09:44 +08001115 /*
1116 * Lower the "search" bit rate, to give new "search" mode
1117 * approximately the same throughput as "active" if:
1118 *
1119 * 1) "Active" mode has been working modestly well (but not
1120 * great), and expected "search" throughput (under perfect
1121 * conditions) at candidate rate is above the actual
1122 * measured "active" throughput (but less than expected
1123 * "active" throughput under perfect conditions).
1124 * OR
1125 * 2) "Active" mode has been working perfectly or very well
1126 * and expected "search" throughput (under perfect
1127 * conditions) at candidate rate is above expected
1128 * "active" throughput (under perfect conditions).
1129 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001130 if ((((100 * tpt_tbl[rate]) > lq_sta->last_tpt) &&
Zhu Yib481de92007-09-25 17:54:57 -07001131 ((active_sr > IWL_RATE_DECREASE_TH) &&
1132 (active_sr <= IWL_RATE_HIGH_TH) &&
1133 (tpt_tbl[rate] <= active_tpt))) ||
1134 ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
1135 (tpt_tbl[rate] > active_tpt))) {
1136
Ben Cahill77626352007-11-29 11:09:44 +08001137 /* (2nd or later pass)
1138 * If we've already tried to raise the rate, and are
1139 * now trying to lower it, use the higher rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001140 if (start_hi != IWL_RATE_INVALID) {
1141 new_rate = start_hi;
1142 break;
1143 }
Ben Cahill77626352007-11-29 11:09:44 +08001144
Zhu Yib481de92007-09-25 17:54:57 -07001145 new_rate = rate;
Ben Cahill77626352007-11-29 11:09:44 +08001146
1147 /* Loop again with lower rate */
Zhu Yib481de92007-09-25 17:54:57 -07001148 if (low != IWL_RATE_INVALID)
1149 rate = low;
Ben Cahill77626352007-11-29 11:09:44 +08001150
1151 /* Lower rate not available, use the original */
Zhu Yib481de92007-09-25 17:54:57 -07001152 else
1153 break;
Ben Cahill77626352007-11-29 11:09:44 +08001154
1155 /* Else try to raise the "search" rate to match "active" */
Zhu Yib481de92007-09-25 17:54:57 -07001156 } else {
Ben Cahill77626352007-11-29 11:09:44 +08001157 /* (2nd or later pass)
1158 * If we've already tried to lower the rate, and are
1159 * now trying to raise it, use the lower rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001160 if (new_rate != IWL_RATE_INVALID)
1161 break;
Ben Cahill77626352007-11-29 11:09:44 +08001162
1163 /* Loop again with higher rate */
Zhu Yib481de92007-09-25 17:54:57 -07001164 else if (high != IWL_RATE_INVALID) {
1165 start_hi = high;
1166 rate = high;
Ben Cahill77626352007-11-29 11:09:44 +08001167
1168 /* Higher rate not available, use the original */
Zhu Yib481de92007-09-25 17:54:57 -07001169 } else {
Guy Cohen90d77952008-09-09 10:54:53 +08001170 new_rate = rate;
Zhu Yib481de92007-09-25 17:54:57 -07001171 break;
1172 }
1173 }
1174 }
1175
1176 return new_rate;
1177}
Zhu Yib481de92007-09-25 17:54:57 -07001178
Ben Cahill77626352007-11-29 11:09:44 +08001179/*
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001180 * Set up search table for MIMO2
Ben Cahill77626352007-11-29 11:09:44 +08001181 */
Guy Cohenfde0db32008-04-21 15:42:01 -07001182static int rs_switch_to_mimo2(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001183 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001184 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001185 struct ieee80211_sta *sta,
Tomas Winklere227cea2008-07-18 13:53:05 +08001186 struct iwl_scale_tbl_info *tbl, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001187{
Zhu Yib481de92007-09-25 17:54:57 -07001188 u16 rate_mask;
1189 s32 rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001190 s8 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07001191
Luis R. Rodriguezde27e642008-12-23 15:58:44 -08001192 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
Zhu Yib481de92007-09-25 17:54:57 -07001193 return -1;
1194
Johannes Bergd9fe60d2008-10-09 12:13:49 +02001195 if (((sta->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 2)
Tomas Winkler00c5ae22008-09-03 11:26:42 +08001196 == WLAN_HT_CAP_SM_PS_STATIC)
Zhu Yib481de92007-09-25 17:54:57 -07001197 return -1;
1198
Ben Cahill77626352007-11-29 11:09:44 +08001199 /* Need both Tx chains/antennas to support MIMO */
Guy Cohenaade00c2008-04-23 17:14:59 -07001200 if (priv->hw_params.tx_chains_num < 2)
Zhu Yib481de92007-09-25 17:54:57 -07001201 return -1;
1202
Tomas Winklere1623442009-01-27 14:27:56 -08001203 IWL_DEBUG_RATE(priv, "LQ: try to switch to MIMO2\n");
Guy Cohen39e88502008-04-23 17:14:57 -07001204
1205 tbl->lq_type = LQ_MIMO2;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001206 tbl->is_dup = lq_sta->is_dup;
Zhu Yib481de92007-09-25 17:54:57 -07001207 tbl->action = 0;
Guy Cohen39e88502008-04-23 17:14:57 -07001208 rate_mask = lq_sta->active_mimo2_rate;
1209
Wey-Yi Guya30199f2009-04-30 13:56:28 -07001210 if (iwl_is_fat_tx_allowed(priv, &sta->ht_cap))
Zhu Yib481de92007-09-25 17:54:57 -07001211 tbl->is_fat = 1;
1212 else
1213 tbl->is_fat = 0;
1214
Guy Cohen39e88502008-04-23 17:14:57 -07001215 /* FIXME: - don't toggle SGI here
Zhu Yib481de92007-09-25 17:54:57 -07001216 if (tbl->is_fat) {
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001217 if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
Zhu Yib481de92007-09-25 17:54:57 -07001218 tbl->is_SGI = 1;
1219 else
1220 tbl->is_SGI = 0;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001221 } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
Zhu Yib481de92007-09-25 17:54:57 -07001222 tbl->is_SGI = 1;
1223 else
1224 tbl->is_SGI = 0;
Guy Cohen39e88502008-04-23 17:14:57 -07001225 */
Zhu Yib481de92007-09-25 17:54:57 -07001226
Guy Coheneecd6e52008-04-23 17:14:58 -07001227 rs_set_expected_tpt_table(lq_sta, tbl);
Zhu Yib481de92007-09-25 17:54:57 -07001228
Guy Cohenf935a6d2008-04-23 17:15:02 -07001229 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
Zhu Yib481de92007-09-25 17:54:57 -07001230
Tomas Winklere1623442009-01-27 14:27:56 -08001231 IWL_DEBUG_RATE(priv, "LQ: MIMO2 best rate %d mask %X\n", rate, rate_mask);
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001232 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1233 IWL_DEBUG_RATE(priv, "Can't switch with index %d rate mask %x\n",
1234 rate, rate_mask);
1235 return -1;
1236 }
1237 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
Guy Cohen39e88502008-04-23 17:14:57 -07001238
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001239 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
1240 tbl->current_rate, is_green);
1241 return 0;
1242}
1243
1244/*
1245 * Set up search table for MIMO3
1246 */
1247static int rs_switch_to_mimo3(struct iwl_priv *priv,
1248 struct iwl_lq_sta *lq_sta,
1249 struct ieee80211_conf *conf,
1250 struct ieee80211_sta *sta,
1251 struct iwl_scale_tbl_info *tbl, int index)
1252{
1253 u16 rate_mask;
1254 s32 rate;
1255 s8 is_green = lq_sta->is_green;
1256
1257 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
1258 return -1;
1259
1260 if (((sta->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 2)
1261 == WLAN_HT_CAP_SM_PS_STATIC)
1262 return -1;
1263
1264 /* Need both Tx chains/antennas to support MIMO */
1265 if (priv->hw_params.tx_chains_num < 3)
1266 return -1;
1267
1268 IWL_DEBUG_RATE(priv, "LQ: try to switch to MIMO3\n");
1269
1270 tbl->lq_type = LQ_MIMO3;
1271 tbl->is_dup = lq_sta->is_dup;
1272 tbl->action = 0;
1273 rate_mask = lq_sta->active_mimo3_rate;
1274
Wey-Yi Guya30199f2009-04-30 13:56:28 -07001275 if (iwl_is_fat_tx_allowed(priv, &sta->ht_cap))
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001276 tbl->is_fat = 1;
1277 else
1278 tbl->is_fat = 0;
1279
1280 /* FIXME: - don't toggle SGI here
1281 if (tbl->is_fat) {
1282 if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
1283 tbl->is_SGI = 1;
1284 else
1285 tbl->is_SGI = 0;
1286 } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
1287 tbl->is_SGI = 1;
1288 else
1289 tbl->is_SGI = 0;
1290 */
1291
1292 rs_set_expected_tpt_table(lq_sta, tbl);
1293
1294 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
1295
1296 IWL_DEBUG_RATE(priv, "LQ: MIMO3 best rate %d mask %X\n",
1297 rate, rate_mask);
Guy Cohen39e88502008-04-23 17:14:57 -07001298 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
Tomas Winklere1623442009-01-27 14:27:56 -08001299 IWL_DEBUG_RATE(priv, "Can't switch with index %d rate mask %x\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001300 rate, rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07001301 return -1;
Guy Cohen39e88502008-04-23 17:14:57 -07001302 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001303 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
Zhu Yib481de92007-09-25 17:54:57 -07001304
Tomas Winklere1623442009-01-27 14:27:56 -08001305 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001306 tbl->current_rate, is_green);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001307 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07001308}
1309
Ben Cahill77626352007-11-29 11:09:44 +08001310/*
1311 * Set up search table for SISO
1312 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001313static int rs_switch_to_siso(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001314 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001315 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001316 struct ieee80211_sta *sta,
Tomas Winklere227cea2008-07-18 13:53:05 +08001317 struct iwl_scale_tbl_info *tbl, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001318{
Zhu Yib481de92007-09-25 17:54:57 -07001319 u16 rate_mask;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001320 u8 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07001321 s32 rate;
1322
Luis R. Rodriguezde27e642008-12-23 15:58:44 -08001323 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
Zhu Yib481de92007-09-25 17:54:57 -07001324 return -1;
1325
Tomas Winklere1623442009-01-27 14:27:56 -08001326 IWL_DEBUG_RATE(priv, "LQ: try to switch to SISO\n");
Guy Cohen39e88502008-04-23 17:14:57 -07001327
Tomas Winklerc33104f2008-01-14 17:46:21 -08001328 tbl->is_dup = lq_sta->is_dup;
Zhu Yib481de92007-09-25 17:54:57 -07001329 tbl->lq_type = LQ_SISO;
1330 tbl->action = 0;
Guy Cohen39e88502008-04-23 17:14:57 -07001331 rate_mask = lq_sta->active_siso_rate;
Zhu Yib481de92007-09-25 17:54:57 -07001332
Wey-Yi Guya30199f2009-04-30 13:56:28 -07001333 if (iwl_is_fat_tx_allowed(priv, &sta->ht_cap))
Zhu Yib481de92007-09-25 17:54:57 -07001334 tbl->is_fat = 1;
1335 else
1336 tbl->is_fat = 0;
1337
Guy Cohen39e88502008-04-23 17:14:57 -07001338 /* FIXME: - don't toggle SGI here
Zhu Yib481de92007-09-25 17:54:57 -07001339 if (tbl->is_fat) {
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001340 if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
Zhu Yib481de92007-09-25 17:54:57 -07001341 tbl->is_SGI = 1;
1342 else
1343 tbl->is_SGI = 0;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001344 } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
Zhu Yib481de92007-09-25 17:54:57 -07001345 tbl->is_SGI = 1;
1346 else
1347 tbl->is_SGI = 0;
Guy Cohen39e88502008-04-23 17:14:57 -07001348 */
Zhu Yib481de92007-09-25 17:54:57 -07001349
1350 if (is_green)
Guy Cohen39e88502008-04-23 17:14:57 -07001351 tbl->is_SGI = 0; /*11n spec: no SGI in SISO+Greenfield*/
Zhu Yib481de92007-09-25 17:54:57 -07001352
Guy Coheneecd6e52008-04-23 17:14:58 -07001353 rs_set_expected_tpt_table(lq_sta, tbl);
Guy Cohenf935a6d2008-04-23 17:15:02 -07001354 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
Zhu Yib481de92007-09-25 17:54:57 -07001355
Tomas Winklere1623442009-01-27 14:27:56 -08001356 IWL_DEBUG_RATE(priv, "LQ: get best rate %d mask %X\n", rate, rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07001357 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
Tomas Winklere1623442009-01-27 14:27:56 -08001358 IWL_DEBUG_RATE(priv, "can not switch with index %d rate mask %x\n",
Zhu Yib481de92007-09-25 17:54:57 -07001359 rate, rate_mask);
1360 return -1;
1361 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001362 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
Tomas Winklere1623442009-01-27 14:27:56 -08001363 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001364 tbl->current_rate, is_green);
Mohamed Abbas403ab562007-11-06 22:06:25 -08001365 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07001366}
1367
Ben Cahill77626352007-11-29 11:09:44 +08001368/*
1369 * Try to switch to new modulation mode from legacy
1370 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001371static int rs_move_legacy_other(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001372 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001373 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001374 struct ieee80211_sta *sta,
Zhu Yib481de92007-09-25 17:54:57 -07001375 int index)
1376{
Tomas Winklere227cea2008-07-18 13:53:05 +08001377 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1378 struct iwl_scale_tbl_info *search_tbl =
1379 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1380 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1381 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1382 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001383 u8 start_action = tbl->action;
Guy Cohenfde0db32008-04-21 15:42:01 -07001384 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
Guy Cohen3110bef2008-09-09 10:54:54 +08001385 u8 tx_chains_num = priv->hw_params.tx_chains_num;
Guy Cohenfde0db32008-04-21 15:42:01 -07001386 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001387
1388 for (; ;) {
1389 switch (tbl->action) {
Guy Cohen3110bef2008-09-09 10:54:54 +08001390 case IWL_LEGACY_SWITCH_ANTENNA1:
1391 case IWL_LEGACY_SWITCH_ANTENNA2:
Tomas Winklere1623442009-01-27 14:27:56 -08001392 IWL_DEBUG_RATE(priv, "LQ: Legacy toggle Antenna\n");
Zhu Yib481de92007-09-25 17:54:57 -07001393
Tomas Winklerc33104f2008-01-14 17:46:21 -08001394 lq_sta->action_counter++;
Ben Cahill77626352007-11-29 11:09:44 +08001395
Guy Cohen3110bef2008-09-09 10:54:54 +08001396 if ((tbl->action == IWL_LEGACY_SWITCH_ANTENNA1 &&
1397 tx_chains_num <= 1) ||
1398 (tbl->action == IWL_LEGACY_SWITCH_ANTENNA2 &&
1399 tx_chains_num <= 2))
1400 break;
1401
Ben Cahill77626352007-11-29 11:09:44 +08001402 /* Don't change antenna if success has been great */
Zhu Yib481de92007-09-25 17:54:57 -07001403 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1404 break;
Ben Cahill77626352007-11-29 11:09:44 +08001405
Ben Cahill77626352007-11-29 11:09:44 +08001406 /* Set up search table to try other antenna */
Zhu Yib481de92007-09-25 17:54:57 -07001407 memcpy(search_tbl, tbl, sz);
1408
Guy Cohenaade00c2008-04-23 17:14:59 -07001409 if (rs_toggle_antenna(valid_tx_ant,
1410 &search_tbl->current_rate, search_tbl)) {
Guy Cohen3110bef2008-09-09 10:54:54 +08001411 rs_set_expected_tpt_table(lq_sta, search_tbl);
Guy Cohenaade00c2008-04-23 17:14:59 -07001412 goto out;
1413 }
Guy Cohena5e8b502008-05-29 16:35:11 +08001414 break;
Zhu Yib481de92007-09-25 17:54:57 -07001415 case IWL_LEGACY_SWITCH_SISO:
Tomas Winklere1623442009-01-27 14:27:56 -08001416 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to SISO\n");
Ben Cahill77626352007-11-29 11:09:44 +08001417
1418 /* Set up search table to try SISO */
Zhu Yib481de92007-09-25 17:54:57 -07001419 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001420 search_tbl->is_SGI = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001421 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001422 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001423 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001424 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001425 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001426 }
Zhu Yib481de92007-09-25 17:54:57 -07001427
1428 break;
Guy Cohen3110bef2008-09-09 10:54:54 +08001429 case IWL_LEGACY_SWITCH_MIMO2_AB:
1430 case IWL_LEGACY_SWITCH_MIMO2_AC:
1431 case IWL_LEGACY_SWITCH_MIMO2_BC:
Tomas Winklere1623442009-01-27 14:27:56 -08001432 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to MIMO2\n");
Ben Cahill77626352007-11-29 11:09:44 +08001433
1434 /* Set up search table to try MIMO */
Zhu Yib481de92007-09-25 17:54:57 -07001435 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001436 search_tbl->is_SGI = 0;
Guy Cohen3110bef2008-09-09 10:54:54 +08001437
1438 if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AB)
1439 search_tbl->ant_type = ANT_AB;
1440 else if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AC)
1441 search_tbl->ant_type = ANT_AC;
1442 else
1443 search_tbl->ant_type = ANT_BC;
1444
1445 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1446 break;
1447
Guy Cohenfde0db32008-04-21 15:42:01 -07001448 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001449 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001450 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001451 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001452 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001453 }
Zhu Yib481de92007-09-25 17:54:57 -07001454 break;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001455
1456 case IWL_LEGACY_SWITCH_MIMO3_ABC:
1457 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to MIMO3\n");
1458
1459 /* Set up search table to try MIMO3 */
1460 memcpy(search_tbl, tbl, sz);
1461 search_tbl->is_SGI = 0;
1462
1463 search_tbl->ant_type = ANT_ABC;
1464
1465 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1466 break;
1467
1468 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1469 search_tbl, index);
1470 if (!ret) {
1471 lq_sta->action_counter = 0;
1472 goto out;
1473 }
1474 break;
Zhu Yib481de92007-09-25 17:54:57 -07001475 }
1476 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001477 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001478 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
Zhu Yib481de92007-09-25 17:54:57 -07001479
1480 if (tbl->action == start_action)
1481 break;
1482
1483 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001484 search_tbl->lq_type = LQ_NONE;
Zhu Yib481de92007-09-25 17:54:57 -07001485 return 0;
1486
Guy Cohen3110bef2008-09-09 10:54:54 +08001487out:
1488 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001489 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001490 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001491 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
Zhu Yib481de92007-09-25 17:54:57 -07001492 return 0;
1493
1494}
1495
Ben Cahill77626352007-11-29 11:09:44 +08001496/*
1497 * Try to switch to new modulation mode from SISO
1498 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001499static int rs_move_siso_to_other(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001500 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001501 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001502 struct ieee80211_sta *sta, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001503{
Tomas Winklerc33104f2008-01-14 17:46:21 -08001504 u8 is_green = lq_sta->is_green;
Tomas Winklere227cea2008-07-18 13:53:05 +08001505 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1506 struct iwl_scale_tbl_info *search_tbl =
1507 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1508 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1509 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1510 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001511 u8 start_action = tbl->action;
Guy Cohenfde0db32008-04-21 15:42:01 -07001512 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
Guy Cohen3110bef2008-09-09 10:54:54 +08001513 u8 tx_chains_num = priv->hw_params.tx_chains_num;
Guy Cohenfde0db32008-04-21 15:42:01 -07001514 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001515
1516 for (;;) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001517 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001518 switch (tbl->action) {
Guy Cohen3110bef2008-09-09 10:54:54 +08001519 case IWL_SISO_SWITCH_ANTENNA1:
1520 case IWL_SISO_SWITCH_ANTENNA2:
Tomas Winklere1623442009-01-27 14:27:56 -08001521 IWL_DEBUG_RATE(priv, "LQ: SISO toggle Antenna\n");
Guy Cohen3110bef2008-09-09 10:54:54 +08001522
1523 if ((tbl->action == IWL_SISO_SWITCH_ANTENNA1 &&
1524 tx_chains_num <= 1) ||
1525 (tbl->action == IWL_SISO_SWITCH_ANTENNA2 &&
1526 tx_chains_num <= 2))
1527 break;
1528
Zhu Yib481de92007-09-25 17:54:57 -07001529 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1530 break;
Zhu Yib481de92007-09-25 17:54:57 -07001531
1532 memcpy(search_tbl, tbl, sz);
Guy Cohenaade00c2008-04-23 17:14:59 -07001533 if (rs_toggle_antenna(valid_tx_ant,
Guy Cohen3110bef2008-09-09 10:54:54 +08001534 &search_tbl->current_rate, search_tbl))
Guy Cohenaade00c2008-04-23 17:14:59 -07001535 goto out;
Guy Cohena5e8b502008-05-29 16:35:11 +08001536 break;
Guy Cohen3110bef2008-09-09 10:54:54 +08001537 case IWL_SISO_SWITCH_MIMO2_AB:
1538 case IWL_SISO_SWITCH_MIMO2_AC:
1539 case IWL_SISO_SWITCH_MIMO2_BC:
Tomas Winklere1623442009-01-27 14:27:56 -08001540 IWL_DEBUG_RATE(priv, "LQ: SISO switch to MIMO2\n");
Zhu Yib481de92007-09-25 17:54:57 -07001541 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001542 search_tbl->is_SGI = 0;
Guy Cohen3110bef2008-09-09 10:54:54 +08001543
1544 if (tbl->action == IWL_SISO_SWITCH_MIMO2_AB)
1545 search_tbl->ant_type = ANT_AB;
1546 else if (tbl->action == IWL_SISO_SWITCH_MIMO2_AC)
1547 search_tbl->ant_type = ANT_AC;
1548 else
1549 search_tbl->ant_type = ANT_BC;
1550
1551 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1552 break;
1553
Guy Cohenfde0db32008-04-21 15:42:01 -07001554 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001555 search_tbl, index);
Guy Cohen3110bef2008-09-09 10:54:54 +08001556 if (!ret)
Zhu Yib481de92007-09-25 17:54:57 -07001557 goto out;
1558 break;
1559 case IWL_SISO_SWITCH_GI:
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001560 if (!tbl->is_fat &&
1561 !(priv->current_ht_config.sgf &
1562 HT_SHORT_GI_20MHZ))
1563 break;
1564 if (tbl->is_fat &&
1565 !(priv->current_ht_config.sgf &
1566 HT_SHORT_GI_40MHZ))
1567 break;
1568
Tomas Winklere1623442009-01-27 14:27:56 -08001569 IWL_DEBUG_RATE(priv, "LQ: SISO toggle SGI/NGI\n");
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001570
Zhu Yib481de92007-09-25 17:54:57 -07001571 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001572 if (is_green) {
1573 if (!tbl->is_SGI)
1574 break;
1575 else
Winkler, Tomas15b16872008-12-19 10:37:33 +08001576 IWL_ERR(priv,
1577 "SGI was set in GF+SISO\n");
Zhu Yib481de92007-09-25 17:54:57 -07001578 }
Guy Cohen39e88502008-04-23 17:14:57 -07001579 search_tbl->is_SGI = !tbl->is_SGI;
Guy Coheneecd6e52008-04-23 17:14:58 -07001580 rs_set_expected_tpt_table(lq_sta, search_tbl);
Guy Cohen39e88502008-04-23 17:14:57 -07001581 if (tbl->is_SGI) {
1582 s32 tpt = lq_sta->last_tpt / 100;
1583 if (tpt >= search_tbl->expected_tpt[index])
1584 break;
1585 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001586 search_tbl->current_rate =
1587 rate_n_flags_from_tbl(priv, search_tbl,
1588 index, is_green);
Zhu Yib481de92007-09-25 17:54:57 -07001589 goto out;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001590 case IWL_SISO_SWITCH_MIMO3_ABC:
1591 IWL_DEBUG_RATE(priv, "LQ: SISO switch to MIMO3\n");
1592 memcpy(search_tbl, tbl, sz);
1593 search_tbl->is_SGI = 0;
1594 search_tbl->ant_type = ANT_ABC;
1595
1596 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1597 break;
1598
1599 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1600 search_tbl, index);
1601 if (!ret)
1602 goto out;
1603 break;
Zhu Yib481de92007-09-25 17:54:57 -07001604 }
1605 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001606 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001607 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
Zhu Yib481de92007-09-25 17:54:57 -07001608
1609 if (tbl->action == start_action)
1610 break;
1611 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001612 search_tbl->lq_type = LQ_NONE;
Zhu Yib481de92007-09-25 17:54:57 -07001613 return 0;
1614
1615 out:
Guy Cohen3110bef2008-09-09 10:54:54 +08001616 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001617 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001618 if (tbl->action > IWL_SISO_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001619 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
Zhu Yib481de92007-09-25 17:54:57 -07001620 return 0;
1621}
1622
Ben Cahill77626352007-11-29 11:09:44 +08001623/*
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001624 * Try to switch to new modulation mode from MIMO2
Ben Cahill77626352007-11-29 11:09:44 +08001625 */
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001626static int rs_move_mimo2_to_other(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001627 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001628 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001629 struct ieee80211_sta *sta, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001630{
Tomas Winklerc33104f2008-01-14 17:46:21 -08001631 s8 is_green = lq_sta->is_green;
Tomas Winklere227cea2008-07-18 13:53:05 +08001632 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1633 struct iwl_scale_tbl_info *search_tbl =
1634 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Guy Cohen3110bef2008-09-09 10:54:54 +08001635 struct iwl_rate_scale_data *window = &(tbl->win[index]);
Tomas Winklere227cea2008-07-18 13:53:05 +08001636 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1637 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001638 u8 start_action = tbl->action;
Guy Cohen3110bef2008-09-09 10:54:54 +08001639 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1640 u8 tx_chains_num = priv->hw_params.tx_chains_num;
Guy Cohenaade00c2008-04-23 17:14:59 -07001641 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001642
1643 for (;;) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001644 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001645 switch (tbl->action) {
Guy Cohen3110bef2008-09-09 10:54:54 +08001646 case IWL_MIMO2_SWITCH_ANTENNA1:
1647 case IWL_MIMO2_SWITCH_ANTENNA2:
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001648 IWL_DEBUG_RATE(priv, "LQ: MIMO2 toggle Antennas\n");
Guy Cohen3110bef2008-09-09 10:54:54 +08001649
1650 if (tx_chains_num <= 2)
1651 break;
1652
1653 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1654 break;
1655
1656 memcpy(search_tbl, tbl, sz);
1657 if (rs_toggle_antenna(valid_tx_ant,
1658 &search_tbl->current_rate, search_tbl))
1659 goto out;
1660 break;
1661 case IWL_MIMO2_SWITCH_SISO_A:
1662 case IWL_MIMO2_SWITCH_SISO_B:
1663 case IWL_MIMO2_SWITCH_SISO_C:
Tomas Winklere1623442009-01-27 14:27:56 -08001664 IWL_DEBUG_RATE(priv, "LQ: MIMO2 switch to SISO\n");
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001665
Ben Cahill77626352007-11-29 11:09:44 +08001666 /* Set up new search table for SISO */
Zhu Yib481de92007-09-25 17:54:57 -07001667 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001668
Guy Cohen3110bef2008-09-09 10:54:54 +08001669 if (tbl->action == IWL_MIMO2_SWITCH_SISO_A)
Guy Cohenfde0db32008-04-21 15:42:01 -07001670 search_tbl->ant_type = ANT_A;
Guy Cohen3110bef2008-09-09 10:54:54 +08001671 else if (tbl->action == IWL_MIMO2_SWITCH_SISO_B)
Guy Cohenfde0db32008-04-21 15:42:01 -07001672 search_tbl->ant_type = ANT_B;
Guy Cohen3110bef2008-09-09 10:54:54 +08001673 else
1674 search_tbl->ant_type = ANT_C;
1675
1676 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1677 break;
Zhu Yib481de92007-09-25 17:54:57 -07001678
Tomas Winklerc33104f2008-01-14 17:46:21 -08001679 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001680 search_tbl, index);
Guy Cohen3110bef2008-09-09 10:54:54 +08001681 if (!ret)
Zhu Yib481de92007-09-25 17:54:57 -07001682 goto out;
Guy Cohen3110bef2008-09-09 10:54:54 +08001683
Zhu Yib481de92007-09-25 17:54:57 -07001684 break;
1685
Guy Cohen3110bef2008-09-09 10:54:54 +08001686 case IWL_MIMO2_SWITCH_GI:
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001687 if (!tbl->is_fat &&
1688 !(priv->current_ht_config.sgf &
1689 HT_SHORT_GI_20MHZ))
1690 break;
1691 if (tbl->is_fat &&
1692 !(priv->current_ht_config.sgf &
1693 HT_SHORT_GI_40MHZ))
1694 break;
1695
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001696 IWL_DEBUG_RATE(priv, "LQ: MIMO2 toggle SGI/NGI\n");
1697
1698 /* Set up new search table for MIMO2 */
1699 memcpy(search_tbl, tbl, sz);
1700 search_tbl->is_SGI = !tbl->is_SGI;
1701 rs_set_expected_tpt_table(lq_sta, search_tbl);
1702 /*
1703 * If active table already uses the fastest possible
1704 * modulation (dual stream with short guard interval),
1705 * and it's working well, there's no need to look
1706 * for a better type of modulation!
1707 */
1708 if (tbl->is_SGI) {
1709 s32 tpt = lq_sta->last_tpt / 100;
1710 if (tpt >= search_tbl->expected_tpt[index])
1711 break;
1712 }
1713 search_tbl->current_rate =
1714 rate_n_flags_from_tbl(priv, search_tbl,
1715 index, is_green);
1716 goto out;
1717
1718 case IWL_MIMO2_SWITCH_MIMO3_ABC:
1719 IWL_DEBUG_RATE(priv, "LQ: MIMO2 switch to MIMO3\n");
1720 memcpy(search_tbl, tbl, sz);
1721 search_tbl->is_SGI = 0;
1722 search_tbl->ant_type = ANT_ABC;
1723
1724 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1725 break;
1726
1727 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1728 search_tbl, index);
1729 if (!ret)
1730 goto out;
1731
1732 break;
1733 }
1734 tbl->action++;
1735 if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
1736 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
1737
1738 if (tbl->action == start_action)
1739 break;
1740 }
1741 search_tbl->lq_type = LQ_NONE;
1742 return 0;
1743 out:
1744 lq_sta->search_better_tbl = 1;
1745 tbl->action++;
1746 if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
1747 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
1748 return 0;
1749
1750}
1751
1752/*
1753 * Try to switch to new modulation mode from MIMO3
1754 */
1755static int rs_move_mimo3_to_other(struct iwl_priv *priv,
1756 struct iwl_lq_sta *lq_sta,
1757 struct ieee80211_conf *conf,
1758 struct ieee80211_sta *sta, int index)
1759{
1760 s8 is_green = lq_sta->is_green;
1761 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1762 struct iwl_scale_tbl_info *search_tbl =
1763 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1764 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1765 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1766 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1767 u8 start_action = tbl->action;
1768 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1769 u8 tx_chains_num = priv->hw_params.tx_chains_num;
1770 int ret;
1771
1772 for (;;) {
1773 lq_sta->action_counter++;
1774 switch (tbl->action) {
1775 case IWL_MIMO3_SWITCH_ANTENNA1:
1776 case IWL_MIMO3_SWITCH_ANTENNA2:
1777 IWL_DEBUG_RATE(priv, "LQ: MIMO3 toggle Antennas\n");
1778
1779 if (tx_chains_num <= 3)
1780 break;
1781
1782 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1783 break;
1784
1785 memcpy(search_tbl, tbl, sz);
1786 if (rs_toggle_antenna(valid_tx_ant,
1787 &search_tbl->current_rate, search_tbl))
1788 goto out;
1789 break;
1790 case IWL_MIMO3_SWITCH_SISO_A:
1791 case IWL_MIMO3_SWITCH_SISO_B:
1792 case IWL_MIMO3_SWITCH_SISO_C:
1793 IWL_DEBUG_RATE(priv, "LQ: MIMO3 switch to SISO\n");
1794
1795 /* Set up new search table for SISO */
1796 memcpy(search_tbl, tbl, sz);
1797
1798 if (tbl->action == IWL_MIMO3_SWITCH_SISO_A)
1799 search_tbl->ant_type = ANT_A;
1800 else if (tbl->action == IWL_MIMO3_SWITCH_SISO_B)
1801 search_tbl->ant_type = ANT_B;
1802 else
1803 search_tbl->ant_type = ANT_C;
1804
1805 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1806 break;
1807
1808 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
1809 search_tbl, index);
1810 if (!ret)
1811 goto out;
1812
1813 break;
1814
1815 case IWL_MIMO3_SWITCH_MIMO2_AB:
1816 case IWL_MIMO3_SWITCH_MIMO2_AC:
1817 case IWL_MIMO3_SWITCH_MIMO2_BC:
1818 IWL_DEBUG_RATE(priv, "LQ: MIMO3 switch to MIMO2\n");
1819
1820 memcpy(search_tbl, tbl, sz);
1821 search_tbl->is_SGI = 0;
1822 if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AB)
1823 search_tbl->ant_type = ANT_AB;
1824 else if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AC)
1825 search_tbl->ant_type = ANT_AC;
1826 else
1827 search_tbl->ant_type = ANT_BC;
1828
1829 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1830 break;
1831
1832 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
1833 search_tbl, index);
1834 if (!ret)
1835 goto out;
1836
1837 break;
1838
1839 case IWL_MIMO3_SWITCH_GI:
1840 if (!tbl->is_fat &&
1841 !(priv->current_ht_config.sgf &
1842 HT_SHORT_GI_20MHZ))
1843 break;
1844 if (tbl->is_fat &&
1845 !(priv->current_ht_config.sgf &
1846 HT_SHORT_GI_40MHZ))
1847 break;
1848
1849 IWL_DEBUG_RATE(priv, "LQ: MIMO3 toggle SGI/NGI\n");
Ben Cahill77626352007-11-29 11:09:44 +08001850
1851 /* Set up new search table for MIMO */
Zhu Yib481de92007-09-25 17:54:57 -07001852 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001853 search_tbl->is_SGI = !tbl->is_SGI;
Guy Coheneecd6e52008-04-23 17:14:58 -07001854 rs_set_expected_tpt_table(lq_sta, search_tbl);
Ben Cahill77626352007-11-29 11:09:44 +08001855 /*
1856 * If active table already uses the fastest possible
1857 * modulation (dual stream with short guard interval),
1858 * and it's working well, there's no need to look
1859 * for a better type of modulation!
1860 */
Guy Cohen39e88502008-04-23 17:14:57 -07001861 if (tbl->is_SGI) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001862 s32 tpt = lq_sta->last_tpt / 100;
Guy Cohen39e88502008-04-23 17:14:57 -07001863 if (tpt >= search_tbl->expected_tpt[index])
1864 break;
Zhu Yib481de92007-09-25 17:54:57 -07001865 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001866 search_tbl->current_rate =
1867 rate_n_flags_from_tbl(priv, search_tbl,
1868 index, is_green);
Zhu Yib481de92007-09-25 17:54:57 -07001869 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07001870 }
1871 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001872 if (tbl->action > IWL_MIMO3_SWITCH_GI)
1873 tbl->action = IWL_MIMO3_SWITCH_ANTENNA1;
Zhu Yib481de92007-09-25 17:54:57 -07001874
1875 if (tbl->action == start_action)
1876 break;
1877 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001878 search_tbl->lq_type = LQ_NONE;
Zhu Yib481de92007-09-25 17:54:57 -07001879 return 0;
1880 out:
Guy Cohen3110bef2008-09-09 10:54:54 +08001881 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001882 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001883 if (tbl->action > IWL_MIMO3_SWITCH_GI)
1884 tbl->action = IWL_MIMO3_SWITCH_ANTENNA1;
Zhu Yib481de92007-09-25 17:54:57 -07001885 return 0;
1886
1887}
1888
Ben Cahill77626352007-11-29 11:09:44 +08001889/*
1890 * Check whether we should continue using same modulation mode, or
1891 * begin search for a new mode, based on:
1892 * 1) # tx successes or failures while using this mode
1893 * 2) # times calling this function
1894 * 3) elapsed time in this mode (not used, for now)
1895 */
Tomas Winklere227cea2008-07-18 13:53:05 +08001896static void rs_stay_in_table(struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07001897{
Tomas Winklere227cea2008-07-18 13:53:05 +08001898 struct iwl_scale_tbl_info *tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001899 int i;
1900 int active_tbl;
1901 int flush_interval_passed = 0;
Ester Kummerbf403db2008-05-05 10:22:40 +08001902 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07001903
Ester Kummerbf403db2008-05-05 10:22:40 +08001904 priv = lq_sta->drv;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001905 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001906
Tomas Winklerc33104f2008-01-14 17:46:21 -08001907 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07001908
Ben Cahill77626352007-11-29 11:09:44 +08001909 /* If we've been disallowing search, see if we should now allow it */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001910 if (lq_sta->stay_in_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07001911
Ben Cahill77626352007-11-29 11:09:44 +08001912 /* Elapsed time using current modulation mode */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001913 if (lq_sta->flush_timer)
Zhu Yib481de92007-09-25 17:54:57 -07001914 flush_interval_passed =
Mohamed Abbas447fee72009-04-20 14:37:02 -07001915 time_after(jiffies,
1916 (unsigned long)(lq_sta->flush_timer +
Zhu Yib481de92007-09-25 17:54:57 -07001917 IWL_RATE_SCALE_FLUSH_INTVL));
1918
Ben Cahill77626352007-11-29 11:09:44 +08001919 /*
1920 * Check if we should allow search for new modulation mode.
1921 * If many frames have failed or succeeded, or we've used
1922 * this same modulation for a long time, allow search, and
1923 * reset history stats that keep track of whether we should
1924 * allow a new search. Also (below) reset all bitmaps and
1925 * stats in active history.
1926 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001927 if ((lq_sta->total_failed > lq_sta->max_failure_limit) ||
1928 (lq_sta->total_success > lq_sta->max_success_limit) ||
1929 ((!lq_sta->search_better_tbl) && (lq_sta->flush_timer)
Zhu Yib481de92007-09-25 17:54:57 -07001930 && (flush_interval_passed))) {
Tomas Winklere1623442009-01-27 14:27:56 -08001931 IWL_DEBUG_RATE(priv, "LQ: stay is expired %d %d %d\n:",
Tomas Winklerc33104f2008-01-14 17:46:21 -08001932 lq_sta->total_failed,
1933 lq_sta->total_success,
Zhu Yib481de92007-09-25 17:54:57 -07001934 flush_interval_passed);
Ben Cahill77626352007-11-29 11:09:44 +08001935
1936 /* Allow search for new mode */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001937 lq_sta->stay_in_tbl = 0; /* only place reset */
1938 lq_sta->total_failed = 0;
1939 lq_sta->total_success = 0;
1940 lq_sta->flush_timer = 0;
Ben Cahill77626352007-11-29 11:09:44 +08001941
1942 /*
1943 * Else if we've used this modulation mode enough repetitions
1944 * (regardless of elapsed time or success/failure), reset
1945 * history bitmaps and rate-specific stats for all rates in
1946 * active table.
1947 */
Mohamed Abbas403ab562007-11-06 22:06:25 -08001948 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001949 lq_sta->table_count++;
1950 if (lq_sta->table_count >=
1951 lq_sta->table_count_limit) {
1952 lq_sta->table_count = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001953
Tomas Winklere1623442009-01-27 14:27:56 -08001954 IWL_DEBUG_RATE(priv, "LQ: stay in table clear win\n");
Zhu Yib481de92007-09-25 17:54:57 -07001955 for (i = 0; i < IWL_RATE_COUNT; i++)
1956 rs_rate_scale_clear_window(
1957 &(tbl->win[i]));
1958 }
1959 }
1960
Ben Cahill77626352007-11-29 11:09:44 +08001961 /* If transitioning to allow "search", reset all history
1962 * bitmaps and stats in active table (this will become the new
1963 * "search" table). */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001964 if (!lq_sta->stay_in_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07001965 for (i = 0; i < IWL_RATE_COUNT; i++)
1966 rs_rate_scale_clear_window(&(tbl->win[i]));
1967 }
1968 }
1969}
1970
Ben Cahill77626352007-11-29 11:09:44 +08001971/*
1972 * Do rate scaling and search for new modulation mode.
1973 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001974static void rs_rate_scale_perform(struct iwl_priv *priv,
Gábor Stefanik514d65c2009-04-23 19:36:08 +02001975 struct sk_buff *skb,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001976 struct ieee80211_sta *sta,
1977 struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07001978{
Johannes Berg4b7679a2008-09-18 18:14:18 +02001979 struct ieee80211_hw *hw = priv->hw;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001980 struct ieee80211_conf *conf = &hw->conf;
Gábor Stefanik514d65c2009-04-23 19:36:08 +02001981 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1982 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001983 int low = IWL_RATE_INVALID;
1984 int high = IWL_RATE_INVALID;
1985 int index;
1986 int i;
Tomas Winklere227cea2008-07-18 13:53:05 +08001987 struct iwl_rate_scale_data *window = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001988 int current_tpt = IWL_INVALID_VALUE;
1989 int low_tpt = IWL_INVALID_VALUE;
1990 int high_tpt = IWL_INVALID_VALUE;
1991 u32 fail_count;
1992 s8 scale_action = 0;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07001993 u16 rate_mask;
Zhu Yib481de92007-09-25 17:54:57 -07001994 u8 update_lq = 0;
Tomas Winklere227cea2008-07-18 13:53:05 +08001995 struct iwl_scale_tbl_info *tbl, *tbl1;
Zhu Yib481de92007-09-25 17:54:57 -07001996 u16 rate_scale_index_msk = 0;
Guy Cohen39e88502008-04-23 17:14:57 -07001997 u32 rate;
Zhu Yib481de92007-09-25 17:54:57 -07001998 u8 is_green = 0;
1999 u8 active_tbl = 0;
2000 u8 done_search = 0;
2001 u16 high_low;
Guy Cohena5e8b502008-05-29 16:35:11 +08002002 s32 sr;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002003 u8 tid = MAX_TID_COUNT;
Wey-Yi Guy86b47662009-03-10 14:35:09 -07002004 struct iwl_tid_data *tid_data;
Zhu Yib481de92007-09-25 17:54:57 -07002005
Tomas Winklere1623442009-01-27 14:27:56 -08002006 IWL_DEBUG_RATE(priv, "rate scale calculate new rate for skb\n");
Zhu Yib481de92007-09-25 17:54:57 -07002007
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002008 /* Send management frames and NO_ACK data using lowest rate. */
Tomas Winkler417f1142008-11-12 13:14:06 -08002009 /* TODO: this could probably be improved.. */
2010 if (!ieee80211_is_data(hdr->frame_control) ||
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002011 info->flags & IEEE80211_TX_CTL_NO_ACK)
Zhu Yib481de92007-09-25 17:54:57 -07002012 return;
Zhu Yib481de92007-09-25 17:54:57 -07002013
Johannes Berg4b7679a2008-09-18 18:14:18 +02002014 if (!sta || !lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002015 return;
2016
Johannes Berg4b7679a2008-09-18 18:14:18 +02002017 lq_sta->supp_rates = sta->supp_rates[lq_sta->band];
Zhu Yib481de92007-09-25 17:54:57 -07002018
Ron Rindjunskyfaa29712008-06-12 09:46:58 +08002019 tid = rs_tl_add_packet(lq_sta, hdr);
2020
Ben Cahill77626352007-11-29 11:09:44 +08002021 /*
2022 * Select rate-scale / modulation-mode table to work with in
2023 * the rest of this function: "search" if searching for better
2024 * modulation mode, or "active" if doing rate scaling within a mode.
2025 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002026 if (!lq_sta->search_better_tbl)
2027 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002028 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002029 active_tbl = 1 - lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002030
Tomas Winklerc33104f2008-01-14 17:46:21 -08002031 tbl = &(lq_sta->lq_info[active_tbl]);
2032 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07002033
Ben Cahill77626352007-11-29 11:09:44 +08002034 /* current tx rate */
Johannes Bergb7e35002008-09-11 02:22:58 +02002035 index = lq_sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002036
Tomas Winklere1623442009-01-27 14:27:56 -08002037 IWL_DEBUG_RATE(priv, "Rate scale index %d for type %d\n", index,
Zhu Yib481de92007-09-25 17:54:57 -07002038 tbl->lq_type);
2039
Ben Cahill77626352007-11-29 11:09:44 +08002040 /* rates available for this association, and for modulation mode */
Guy Coheneecd6e52008-04-23 17:14:58 -07002041 rate_mask = rs_get_supported_rates(lq_sta, hdr, tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -07002042
Tomas Winklere1623442009-01-27 14:27:56 -08002043 IWL_DEBUG_RATE(priv, "mask 0x%04X \n", rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07002044
2045 /* mask with station rate restriction */
2046 if (is_legacy(tbl->lq_type)) {
Johannes Berg8318d782008-01-24 19:38:38 +01002047 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Ben Cahill77626352007-11-29 11:09:44 +08002048 /* supp_rates has no CCK bits in A mode */
Zhu Yib481de92007-09-25 17:54:57 -07002049 rate_scale_index_msk = (u16) (rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -08002050 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
Zhu Yib481de92007-09-25 17:54:57 -07002051 else
2052 rate_scale_index_msk = (u16) (rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -08002053 lq_sta->supp_rates);
Zhu Yib481de92007-09-25 17:54:57 -07002054
2055 } else
2056 rate_scale_index_msk = rate_mask;
2057
2058 if (!rate_scale_index_msk)
2059 rate_scale_index_msk = rate_mask;
2060
Guy Cohen07bc28e2008-04-23 17:15:03 -07002061 if (!((1 << index) & rate_scale_index_msk)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002062 IWL_ERR(priv, "Current Rate is not valid\n");
Guy Cohen07bc28e2008-04-23 17:15:03 -07002063 return;
Zhu Yib481de92007-09-25 17:54:57 -07002064 }
2065
Ben Cahill77626352007-11-29 11:09:44 +08002066 /* Get expected throughput table and history window for current rate */
Guy Cohen07bc28e2008-04-23 17:15:03 -07002067 if (!tbl->expected_tpt) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002068 IWL_ERR(priv, "tbl->expected_tpt is NULL\n");
Guy Cohen07bc28e2008-04-23 17:15:03 -07002069 return;
2070 }
Zhu Yib481de92007-09-25 17:54:57 -07002071
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002072 /* force user max rate if set by user */
2073 if ((lq_sta->max_rate_idx != -1) &&
2074 (lq_sta->max_rate_idx < index)) {
2075 index = lq_sta->max_rate_idx;
2076 update_lq = 1;
2077 window = &(tbl->win[index]);
2078 goto lq_update;
2079 }
2080
Zhu Yib481de92007-09-25 17:54:57 -07002081 window = &(tbl->win[index]);
2082
Ben Cahill77626352007-11-29 11:09:44 +08002083 /*
2084 * If there is not enough history to calculate actual average
2085 * throughput, keep analyzing results of more tx frames, without
2086 * changing rate or mode (bypass most of the rest of this function).
2087 * Set up new rate table in uCode only if old rate is not supported
2088 * in current association (use new rate found above).
2089 */
Zhu Yib481de92007-09-25 17:54:57 -07002090 fail_count = window->counter - window->success_counter;
Guy Cohen07bc28e2008-04-23 17:15:03 -07002091 if ((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
2092 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002093 IWL_DEBUG_RATE(priv, "LQ: still below TH. succ=%d total=%d "
Zhu Yib481de92007-09-25 17:54:57 -07002094 "for index %d\n",
2095 window->success_counter, window->counter, index);
Ben Cahill77626352007-11-29 11:09:44 +08002096
2097 /* Can't calculate this yet; not enough history */
Zhu Yib481de92007-09-25 17:54:57 -07002098 window->average_tpt = IWL_INVALID_VALUE;
Ben Cahill77626352007-11-29 11:09:44 +08002099
2100 /* Should we stay with this modulation mode,
2101 * or search for a new one? */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002102 rs_stay_in_table(lq_sta);
Ben Cahill77626352007-11-29 11:09:44 +08002103
Zhu Yib481de92007-09-25 17:54:57 -07002104 goto out;
Guy Cohen3110bef2008-09-09 10:54:54 +08002105 }
Zhu Yib481de92007-09-25 17:54:57 -07002106
Ben Cahill77626352007-11-29 11:09:44 +08002107 /* Else we have enough samples; calculate estimate of
2108 * actual average throughput */
Guy Cohen3110bef2008-09-09 10:54:54 +08002109
2110 BUG_ON(window->average_tpt != ((window->success_ratio *
2111 tbl->expected_tpt[index] + 64) / 128));
Zhu Yib481de92007-09-25 17:54:57 -07002112
Ben Cahill77626352007-11-29 11:09:44 +08002113 /* If we are searching for better modulation mode, check success. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002114 if (lq_sta->search_better_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07002115
Ben Cahill77626352007-11-29 11:09:44 +08002116 /* If good success, continue using the "search" mode;
2117 * no need to send new link quality command, since we're
2118 * continuing to use the setup that we've been trying. */
Guy Cohen07bc28e2008-04-23 17:15:03 -07002119 if (window->average_tpt > lq_sta->last_tpt) {
2120
Tomas Winklere1623442009-01-27 14:27:56 -08002121 IWL_DEBUG_RATE(priv, "LQ: SWITCHING TO NEW TABLE "
Guy Cohen07bc28e2008-04-23 17:15:03 -07002122 "suc=%d cur-tpt=%d old-tpt=%d\n",
2123 window->success_ratio,
2124 window->average_tpt,
2125 lq_sta->last_tpt);
2126
2127 if (!is_legacy(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08002128 lq_sta->enable_counter = 1;
Guy Cohen07bc28e2008-04-23 17:15:03 -07002129
Ben Cahill77626352007-11-29 11:09:44 +08002130 /* Swap tables; "search" becomes "active" */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002131 lq_sta->active_tbl = active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002132 current_tpt = window->average_tpt;
Ben Cahill77626352007-11-29 11:09:44 +08002133
2134 /* Else poor success; go back to mode in "active" table */
Zhu Yib481de92007-09-25 17:54:57 -07002135 } else {
Guy Cohen07bc28e2008-04-23 17:15:03 -07002136
Tomas Winklere1623442009-01-27 14:27:56 -08002137 IWL_DEBUG_RATE(priv, "LQ: GOING BACK TO THE OLD TABLE "
Guy Cohen07bc28e2008-04-23 17:15:03 -07002138 "suc=%d cur-tpt=%d old-tpt=%d\n",
2139 window->success_ratio,
2140 window->average_tpt,
2141 lq_sta->last_tpt);
2142
Ben Cahill77626352007-11-29 11:09:44 +08002143 /* Nullify "search" table */
Zhu Yib481de92007-09-25 17:54:57 -07002144 tbl->lq_type = LQ_NONE;
Ben Cahill77626352007-11-29 11:09:44 +08002145
2146 /* Revert to "active" table */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002147 active_tbl = lq_sta->active_tbl;
2148 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07002149
Ben Cahill77626352007-11-29 11:09:44 +08002150 /* Revert to "active" rate and throughput info */
Tomas Winklere7d326ac2008-06-12 09:47:11 +08002151 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
Tomas Winklerc33104f2008-01-14 17:46:21 -08002152 current_tpt = lq_sta->last_tpt;
Ben Cahill77626352007-11-29 11:09:44 +08002153
2154 /* Need to set up a new rate table in uCode */
2155 update_lq = 1;
Zhu Yib481de92007-09-25 17:54:57 -07002156 }
Ben Cahill77626352007-11-29 11:09:44 +08002157
2158 /* Either way, we've made a decision; modulation mode
2159 * search is done, allow rate adjustment next time. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002160 lq_sta->search_better_tbl = 0;
Ben Cahill77626352007-11-29 11:09:44 +08002161 done_search = 1; /* Don't switch modes below! */
Zhu Yib481de92007-09-25 17:54:57 -07002162 goto lq_update;
2163 }
2164
Ben Cahill77626352007-11-29 11:09:44 +08002165 /* (Else) not in search of better modulation mode, try for better
2166 * starting rate, while staying in this mode. */
Ester Kummerbf403db2008-05-05 10:22:40 +08002167 high_low = rs_get_adjacent_rate(priv, index, rate_scale_index_msk,
Zhu Yib481de92007-09-25 17:54:57 -07002168 tbl->lq_type);
2169 low = high_low & 0xff;
2170 high = (high_low >> 8) & 0xff;
2171
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002172 /* If user set max rate, dont allow higher than user constrain */
2173 if ((lq_sta->max_rate_idx != -1) &&
2174 (lq_sta->max_rate_idx < high))
2175 high = IWL_RATE_INVALID;
2176
Guy Cohena5e8b502008-05-29 16:35:11 +08002177 sr = window->success_ratio;
2178
Ben Cahill77626352007-11-29 11:09:44 +08002179 /* Collect measured throughputs for current and adjacent rates */
Zhu Yib481de92007-09-25 17:54:57 -07002180 current_tpt = window->average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07002181 if (low != IWL_RATE_INVALID)
2182 low_tpt = tbl->win[low].average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07002183 if (high != IWL_RATE_INVALID)
2184 high_tpt = tbl->win[high].average_tpt;
2185
Guy Cohena5e8b502008-05-29 16:35:11 +08002186 scale_action = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002187
Ben Cahill77626352007-11-29 11:09:44 +08002188 /* Too many failures, decrease rate */
Guy Cohena5e8b502008-05-29 16:35:11 +08002189 if ((sr <= IWL_RATE_DECREASE_TH) || (current_tpt == 0)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002190 IWL_DEBUG_RATE(priv, "decrease rate because of low success_ratio\n");
Zhu Yib481de92007-09-25 17:54:57 -07002191 scale_action = -1;
Ben Cahill77626352007-11-29 11:09:44 +08002192
2193 /* No throughput measured yet for adjacent rates; try increase. */
Zhu Yib481de92007-09-25 17:54:57 -07002194 } else if ((low_tpt == IWL_INVALID_VALUE) &&
Guy Cohena5e8b502008-05-29 16:35:11 +08002195 (high_tpt == IWL_INVALID_VALUE)) {
2196
2197 if (high != IWL_RATE_INVALID && sr >= IWL_RATE_INCREASE_TH)
2198 scale_action = 1;
2199 else if (low != IWL_RATE_INVALID)
Wey-Yi Guy8fe72312009-03-10 14:35:10 -07002200 scale_action = 0;
Guy Cohena5e8b502008-05-29 16:35:11 +08002201 }
Ben Cahill77626352007-11-29 11:09:44 +08002202
2203 /* Both adjacent throughputs are measured, but neither one has better
2204 * throughput; we're using the best rate, don't change it! */
Zhu Yib481de92007-09-25 17:54:57 -07002205 else if ((low_tpt != IWL_INVALID_VALUE) &&
2206 (high_tpt != IWL_INVALID_VALUE) &&
2207 (low_tpt < current_tpt) &&
2208 (high_tpt < current_tpt))
2209 scale_action = 0;
Ben Cahill77626352007-11-29 11:09:44 +08002210
2211 /* At least one adjacent rate's throughput is measured,
2212 * and may have better performance. */
Zhu Yib481de92007-09-25 17:54:57 -07002213 else {
Ben Cahill77626352007-11-29 11:09:44 +08002214 /* Higher adjacent rate's throughput is measured */
Zhu Yib481de92007-09-25 17:54:57 -07002215 if (high_tpt != IWL_INVALID_VALUE) {
Ben Cahill77626352007-11-29 11:09:44 +08002216 /* Higher rate has better throughput */
Guy Cohena5e8b502008-05-29 16:35:11 +08002217 if (high_tpt > current_tpt &&
2218 sr >= IWL_RATE_INCREASE_TH) {
Zhu Yib481de92007-09-25 17:54:57 -07002219 scale_action = 1;
Guy Cohena5e8b502008-05-29 16:35:11 +08002220 } else {
Wey-Yi Guy8fe72312009-03-10 14:35:10 -07002221 scale_action = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002222 }
Ben Cahill77626352007-11-29 11:09:44 +08002223
2224 /* Lower adjacent rate's throughput is measured */
Zhu Yib481de92007-09-25 17:54:57 -07002225 } else if (low_tpt != IWL_INVALID_VALUE) {
Ben Cahill77626352007-11-29 11:09:44 +08002226 /* Lower rate has better throughput */
Zhu Yib481de92007-09-25 17:54:57 -07002227 if (low_tpt > current_tpt) {
Tomas Winklere1623442009-01-27 14:27:56 -08002228 IWL_DEBUG_RATE(priv,
2229 "decrease rate because of low tpt\n");
Zhu Yib481de92007-09-25 17:54:57 -07002230 scale_action = -1;
Guy Cohena5e8b502008-05-29 16:35:11 +08002231 } else if (sr >= IWL_RATE_INCREASE_TH) {
Zhu Yib481de92007-09-25 17:54:57 -07002232 scale_action = 1;
Guy Cohena5e8b502008-05-29 16:35:11 +08002233 }
Zhu Yib481de92007-09-25 17:54:57 -07002234 }
2235 }
2236
Ben Cahill77626352007-11-29 11:09:44 +08002237 /* Sanity check; asked for decrease, but success rate or throughput
2238 * has been good at old rate. Don't change it. */
Guy Cohena5e8b502008-05-29 16:35:11 +08002239 if ((scale_action == -1) && (low != IWL_RATE_INVALID) &&
2240 ((sr > IWL_RATE_HIGH_TH) ||
Zhu Yib481de92007-09-25 17:54:57 -07002241 (current_tpt > (100 * tbl->expected_tpt[low]))))
Zhu Yib481de92007-09-25 17:54:57 -07002242 scale_action = 0;
2243
2244 switch (scale_action) {
2245 case -1:
Ben Cahill77626352007-11-29 11:09:44 +08002246 /* Decrease starting rate, update uCode's rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002247 if (low != IWL_RATE_INVALID) {
2248 update_lq = 1;
2249 index = low;
2250 }
Mohamed Abbas447fee72009-04-20 14:37:02 -07002251
Zhu Yib481de92007-09-25 17:54:57 -07002252 break;
2253 case 1:
Ben Cahill77626352007-11-29 11:09:44 +08002254 /* Increase starting rate, update uCode's rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002255 if (high != IWL_RATE_INVALID) {
2256 update_lq = 1;
2257 index = high;
2258 }
2259
2260 break;
2261 case 0:
Ben Cahill77626352007-11-29 11:09:44 +08002262 /* No change */
Zhu Yib481de92007-09-25 17:54:57 -07002263 default:
2264 break;
2265 }
2266
Tomas Winklere1623442009-01-27 14:27:56 -08002267 IWL_DEBUG_RATE(priv, "choose rate scale index %d action %d low %d "
Zhu Yib481de92007-09-25 17:54:57 -07002268 "high %d type %d\n",
2269 index, scale_action, low, high, tbl->lq_type);
2270
Guy Cohena5e8b502008-05-29 16:35:11 +08002271lq_update:
Ben Cahill77626352007-11-29 11:09:44 +08002272 /* Replace uCode's rate table for the destination station. */
Zhu Yib481de92007-09-25 17:54:57 -07002273 if (update_lq) {
Tomas Winkler978785a2008-12-19 10:37:31 +08002274 rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
Guy Cohen07bc28e2008-04-23 17:15:03 -07002275 rs_fill_link_cmd(priv, lq_sta, rate);
Tomas Winkler66c73db2008-04-15 16:01:40 -07002276 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07002277 }
Ben Cahill77626352007-11-29 11:09:44 +08002278
2279 /* Should we stay with this modulation mode, or search for a new one? */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002280 rs_stay_in_table(lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002281
Ben Cahill77626352007-11-29 11:09:44 +08002282 /*
2283 * Search for new modulation mode if we're:
2284 * 1) Not changing rates right now
2285 * 2) Not just finishing up a search
2286 * 3) Allowing a new search
2287 */
Guy Cohen47cfd462008-05-27 11:29:34 +08002288 if (!update_lq && !done_search && !lq_sta->stay_in_tbl && window->counter) {
Ben Cahill77626352007-11-29 11:09:44 +08002289 /* Save current throughput to compare with "search" throughput*/
Tomas Winklerc33104f2008-01-14 17:46:21 -08002290 lq_sta->last_tpt = current_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07002291
Ben Cahill77626352007-11-29 11:09:44 +08002292 /* Select a new "search" modulation mode to try.
2293 * If one is found, set up the new "search" table. */
Zhu Yib481de92007-09-25 17:54:57 -07002294 if (is_legacy(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08002295 rs_move_legacy_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07002296 else if (is_siso(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08002297 rs_move_siso_to_other(priv, lq_sta, conf, sta, index);
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07002298 else if (is_mimo2(tbl->lq_type))
2299 rs_move_mimo2_to_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07002300 else
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07002301 rs_move_mimo3_to_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07002302
Ben Cahill77626352007-11-29 11:09:44 +08002303 /* If new "search" mode was selected, set up in uCode table */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002304 if (lq_sta->search_better_tbl) {
Ben Cahill77626352007-11-29 11:09:44 +08002305 /* Access the "search" table, clear its history. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002306 tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Zhu Yib481de92007-09-25 17:54:57 -07002307 for (i = 0; i < IWL_RATE_COUNT; i++)
2308 rs_rate_scale_clear_window(&(tbl->win[i]));
2309
Ben Cahill77626352007-11-29 11:09:44 +08002310 /* Use new "search" start rate */
Tomas Winklere7d326ac2008-06-12 09:47:11 +08002311 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002312
Tomas Winklere1623442009-01-27 14:27:56 -08002313 IWL_DEBUG_RATE(priv, "Switch current mcs: %X index: %d\n",
Guy Cohen39e88502008-04-23 17:14:57 -07002314 tbl->current_rate, index);
Guy Cohen07bc28e2008-04-23 17:15:03 -07002315 rs_fill_link_cmd(priv, lq_sta, tbl->current_rate);
Tomas Winkler66c73db2008-04-15 16:01:40 -07002316 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
Mohamed Abbas447fee72009-04-20 14:37:02 -07002317 } else
2318 done_search = 1;
2319 }
Zhu Yib481de92007-09-25 17:54:57 -07002320
Mohamed Abbas447fee72009-04-20 14:37:02 -07002321 if (done_search && !lq_sta->stay_in_tbl) {
Ben Cahill77626352007-11-29 11:09:44 +08002322 /* If the "active" (non-search) mode was legacy,
2323 * and we've tried switching antennas,
2324 * but we haven't been able to try HT modes (not available),
2325 * stay with best antenna legacy modulation for a while
2326 * before next round of mode comparisons. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002327 tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
Luis R. Rodriguezde27e642008-12-23 15:58:44 -08002328 if (is_legacy(tbl1->lq_type) && !conf_is_ht(conf) &&
Johannes Bergae5eb022008-10-14 16:58:37 +02002329 lq_sta->action_counter >= 1) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002330 lq_sta->action_counter = 0;
Tomas Winklere1623442009-01-27 14:27:56 -08002331 IWL_DEBUG_RATE(priv, "LQ: STAY in legacy table\n");
Ester Kummerbf403db2008-05-05 10:22:40 +08002332 rs_set_stay_in_table(priv, 1, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002333 }
2334
Ben Cahill77626352007-11-29 11:09:44 +08002335 /* If we're in an HT mode, and all 3 mode switch actions
2336 * have been tried and compared, stay in this best modulation
2337 * mode for a while before next round of mode comparisons. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002338 if (lq_sta->enable_counter &&
2339 (lq_sta->action_counter >= IWL_ACTION_LIMIT)) {
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002340 if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
2341 (lq_sta->tx_agg_tid_en & (1 << tid)) &&
2342 (tid != MAX_TID_COUNT)) {
Wey-Yi Guy86b47662009-03-10 14:35:09 -07002343 tid_data =
2344 &priv->stations[lq_sta->lq.sta_id].tid[tid];
2345 if (tid_data->agg.state == IWL_AGG_OFF) {
2346 IWL_DEBUG_RATE(priv,
2347 "try to aggregate tid %d\n",
2348 tid);
2349 rs_tl_turn_on_agg(priv, tid,
2350 lq_sta, sta);
2351 }
Zhu Yib481de92007-09-25 17:54:57 -07002352 }
Tomas Winklerc33104f2008-01-14 17:46:21 -08002353 lq_sta->action_counter = 0;
Ester Kummerbf403db2008-05-05 10:22:40 +08002354 rs_set_stay_in_table(priv, 0, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002355 }
Zhu Yib481de92007-09-25 17:54:57 -07002356 }
2357
2358out:
Tomas Winkler978785a2008-12-19 10:37:31 +08002359 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
Zhu Yib481de92007-09-25 17:54:57 -07002360 i = index;
Johannes Bergb7e35002008-09-11 02:22:58 +02002361 lq_sta->last_txrate_idx = i;
Zhu Yib481de92007-09-25 17:54:57 -07002362
Zhu Yib481de92007-09-25 17:54:57 -07002363 return;
2364}
2365
2366
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002367static void rs_initialize_lq(struct iwl_priv *priv,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002368 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02002369 struct ieee80211_sta *sta,
2370 struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002371{
Tomas Winklere227cea2008-07-18 13:53:05 +08002372 struct iwl_scale_tbl_info *tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002373 int rate_idx;
Guy Cohenaade00c2008-04-23 17:14:59 -07002374 int i;
Guy Cohen39e88502008-04-23 17:14:57 -07002375 u32 rate;
Guy Cohenaade00c2008-04-23 17:14:59 -07002376 u8 use_green = rs_use_green(priv, conf);
2377 u8 active_tbl = 0;
2378 u8 valid_tx_ant;
Zhu Yib481de92007-09-25 17:54:57 -07002379
Johannes Berg4b7679a2008-09-18 18:14:18 +02002380 if (!sta || !lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002381 goto out;
2382
Johannes Bergb7e35002008-09-11 02:22:58 +02002383 i = lq_sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002384
Tomas Winklerc33104f2008-01-14 17:46:21 -08002385 if ((lq_sta->lq.sta_id == 0xff) &&
Johannes Berg05c914f2008-09-11 00:01:58 +02002386 (priv->iw_mode == NL80211_IFTYPE_ADHOC))
Zhu Yib481de92007-09-25 17:54:57 -07002387 goto out;
2388
Guy Cohenaade00c2008-04-23 17:14:59 -07002389 valid_tx_ant = priv->hw_params.valid_tx_ant;
2390
Tomas Winklerc33104f2008-01-14 17:46:21 -08002391 if (!lq_sta->search_better_tbl)
2392 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002393 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002394 active_tbl = 1 - lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002395
Tomas Winklerc33104f2008-01-14 17:46:21 -08002396 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07002397
2398 if ((i < 0) || (i >= IWL_RATE_COUNT))
2399 i = 0;
2400
Tomas Winkler1826dcc2008-05-15 13:54:02 +08002401 rate = iwl_rates[i].plcp;
Winkler, Tomaseb48dca2008-10-29 14:05:48 -07002402 tbl->ant_type = first_antenna(valid_tx_ant);
2403 rate |= tbl->ant_type << RATE_MCS_ANT_POS;
Zhu Yib481de92007-09-25 17:54:57 -07002404
2405 if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
Guy Cohen39e88502008-04-23 17:14:57 -07002406 rate |= RATE_MCS_CCK_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002407
Guy Cohen39e88502008-04-23 17:14:57 -07002408 rs_get_tbl_info_from_mcs(rate, priv->band, tbl, &rate_idx);
Guy Cohenaade00c2008-04-23 17:14:59 -07002409 if (!rs_is_valid_ant(valid_tx_ant, tbl->ant_type))
2410 rs_toggle_antenna(valid_tx_ant, &rate, tbl);
Zhu Yib481de92007-09-25 17:54:57 -07002411
Tomas Winkler978785a2008-12-19 10:37:31 +08002412 rate = rate_n_flags_from_tbl(priv, tbl, rate_idx, use_green);
Guy Cohen39e88502008-04-23 17:14:57 -07002413 tbl->current_rate = rate;
Guy Coheneecd6e52008-04-23 17:14:58 -07002414 rs_set_expected_tpt_table(lq_sta, tbl);
Guy Cohen07bc28e2008-04-23 17:15:03 -07002415 rs_fill_link_cmd(NULL, lq_sta, rate);
Tomas Winkler66c73db2008-04-15 16:01:40 -07002416 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07002417 out:
2418 return;
2419}
2420
Johannes Berge6a98542008-10-21 12:40:02 +02002421static void rs_get_rate(void *priv_r, struct ieee80211_sta *sta, void *priv_sta,
2422 struct ieee80211_tx_rate_control *txrc)
Zhu Yib481de92007-09-25 17:54:57 -07002423{
2424
Johannes Berge6a98542008-10-21 12:40:02 +02002425 struct sk_buff *skb = txrc->skb;
2426 struct ieee80211_supported_band *sband = txrc->sband;
Johannes Berg4b7679a2008-09-18 18:14:18 +02002427 struct iwl_priv *priv = (struct iwl_priv *)priv_r;
2428 struct ieee80211_conf *conf = &priv->hw->conf;
Zhu Yib481de92007-09-25 17:54:57 -07002429 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berge6a98542008-10-21 12:40:02 +02002430 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Tomas Winkler417f1142008-11-12 13:14:06 -08002431 struct iwl_lq_sta *lq_sta = priv_sta;
2432 int rate_idx;
Abbas, Mohamedc338ba32009-01-21 10:58:02 -08002433 u64 mask_bit = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002434
Tomas Winklere1623442009-01-27 14:27:56 -08002435 IWL_DEBUG_RATE_LIMIT(priv, "rate scale calculate new rate for skb\n");
Zhu Yib481de92007-09-25 17:54:57 -07002436
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002437 /* Get max rate if user set max rate */
2438 if (lq_sta) {
2439 lq_sta->max_rate_idx = txrc->max_rate_idx;
2440 if ((sband->band == IEEE80211_BAND_5GHZ) &&
2441 (lq_sta->max_rate_idx != -1))
2442 lq_sta->max_rate_idx += IWL_FIRST_OFDM_RATE;
2443 if ((lq_sta->max_rate_idx < 0) ||
2444 (lq_sta->max_rate_idx >= IWL_RATE_COUNT))
2445 lq_sta->max_rate_idx = -1;
2446 }
2447
Abbas, Mohamedc338ba32009-01-21 10:58:02 -08002448 if (sta)
2449 mask_bit = sta->supp_rates[sband->band];
2450
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002451 /* Send management frames and NO_ACK data using lowest rate. */
Tomas Winkler417f1142008-11-12 13:14:06 -08002452 if (!ieee80211_is_data(hdr->frame_control) ||
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002453 info->flags & IEEE80211_TX_CTL_NO_ACK || !sta || !lq_sta) {
Abbas, Mohamedc338ba32009-01-21 10:58:02 -08002454 if (!mask_bit)
2455 info->control.rates[0].idx =
2456 rate_lowest_index(sband, NULL);
2457 else
2458 info->control.rates[0].idx =
2459 rate_lowest_index(sband, sta);
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002460 if (info->flags & IEEE80211_TX_CTL_NO_ACK)
2461 info->control.rates[0].count = 1;
Johannes Berg4b7679a2008-09-18 18:14:18 +02002462 return;
Zhu Yib481de92007-09-25 17:54:57 -07002463 }
2464
Tomas Winkler417f1142008-11-12 13:14:06 -08002465 rate_idx = lq_sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002466
Johannes Berg05c914f2008-09-11 00:01:58 +02002467 if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) &&
Tomas Winklerc33104f2008-01-14 17:46:21 -08002468 !lq_sta->ibss_sta_added) {
Abhijeet Kolekare11bc022009-04-08 11:26:41 -07002469 u8 sta_id = priv->cfg->ops->smgmt->find_station(priv,
2470 hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002471
2472 if (sta_id == IWL_INVALID_STATION) {
Tomas Winklere1623442009-01-27 14:27:56 -08002473 IWL_DEBUG_RATE(priv, "LQ: ADD station %pM\n",
Johannes Berge1749612008-10-27 15:59:26 -07002474 hdr->addr1);
Abhijeet Kolekar06fd3d82009-04-08 11:26:42 -07002475 sta_id = priv->cfg->ops->smgmt->add_station(priv,
Abhijeet Kolekare11bc022009-04-08 11:26:41 -07002476 hdr->addr1, 0,
2477 CMD_ASYNC, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07002478 }
2479 if ((sta_id != IWL_INVALID_STATION)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002480 lq_sta->lq.sta_id = sta_id;
2481 lq_sta->lq.rs_table[0].rate_n_flags = 0;
2482 lq_sta->ibss_sta_added = 1;
Johannes Berg4b7679a2008-09-18 18:14:18 +02002483 rs_initialize_lq(priv, conf, sta, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002484 }
Zhu Yib481de92007-09-25 17:54:57 -07002485 }
2486
Tomas Winkler417f1142008-11-12 13:14:06 -08002487 if (rate_idx < 0 || rate_idx > IWL_RATE_COUNT)
2488 rate_idx = rate_lowest_index(sband, sta);
2489 else if (sband->band == IEEE80211_BAND_5GHZ)
2490 rate_idx -= IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -07002491
Tomas Winkler417f1142008-11-12 13:14:06 -08002492 info->control.rates[0].idx = rate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002493}
2494
Johannes Berg4b7679a2008-09-18 18:14:18 +02002495static void *rs_alloc_sta(void *priv_rate, struct ieee80211_sta *sta,
2496 gfp_t gfp)
Zhu Yib481de92007-09-25 17:54:57 -07002497{
Tomas Winklere227cea2008-07-18 13:53:05 +08002498 struct iwl_lq_sta *lq_sta;
Ester Kummerbf403db2008-05-05 10:22:40 +08002499 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07002500 int i, j;
2501
Ester Kummerbf403db2008-05-05 10:22:40 +08002502 priv = (struct iwl_priv *)priv_rate;
Tomas Winklere1623442009-01-27 14:27:56 -08002503 IWL_DEBUG_RATE(priv, "create station rate scale window\n");
Zhu Yib481de92007-09-25 17:54:57 -07002504
Tomas Winklere227cea2008-07-18 13:53:05 +08002505 lq_sta = kzalloc(sizeof(struct iwl_lq_sta), gfp);
Zhu Yib481de92007-09-25 17:54:57 -07002506
Tomas Winklerc33104f2008-01-14 17:46:21 -08002507 if (lq_sta == NULL)
Zhu Yib481de92007-09-25 17:54:57 -07002508 return NULL;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002509 lq_sta->lq.sta_id = 0xff;
Zhu Yib481de92007-09-25 17:54:57 -07002510
Zhu Yi63fddb92007-09-27 11:27:36 +08002511
Zhu Yib481de92007-09-25 17:54:57 -07002512 for (j = 0; j < LQ_SIZE; j++)
2513 for (i = 0; i < IWL_RATE_COUNT; i++)
Tomas Winkler3ac7f142008-07-21 02:40:14 +03002514 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
Zhu Yib481de92007-09-25 17:54:57 -07002515
Tomas Winklerc33104f2008-01-14 17:46:21 -08002516 return lq_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002517}
2518
Johannes Berg4b7679a2008-09-18 18:14:18 +02002519static void rs_rate_init(void *priv_r, struct ieee80211_supported_band *sband,
2520 struct ieee80211_sta *sta, void *priv_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002521{
2522 int i, j;
Johannes Berg4b7679a2008-09-18 18:14:18 +02002523 struct iwl_priv *priv = (struct iwl_priv *)priv_r;
2524 struct ieee80211_conf *conf = &priv->hw->conf;
Tomas Winklere227cea2008-07-18 13:53:05 +08002525 struct iwl_lq_sta *lq_sta = priv_sta;
Mohamed Abbasd5e49032008-12-12 08:22:15 -08002526 u16 mask_bit = 0;
Abbas, Mohamed7d049e52009-01-20 21:33:53 -08002527 int count;
2528 int start_rate = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002529
Tomas Winklerc33104f2008-01-14 17:46:21 -08002530 lq_sta->flush_timer = 0;
Johannes Berg4b7679a2008-09-18 18:14:18 +02002531 lq_sta->supp_rates = sta->supp_rates[sband->band];
Zhu Yib481de92007-09-25 17:54:57 -07002532 for (j = 0; j < LQ_SIZE; j++)
2533 for (i = 0; i < IWL_RATE_COUNT; i++)
Tomas Winkler3ac7f142008-07-21 02:40:14 +03002534 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
Zhu Yib481de92007-09-25 17:54:57 -07002535
Tomas Winklere1623442009-01-27 14:27:56 -08002536 IWL_DEBUG_RATE(priv, "LQ: *** rate scale station global init ***\n");
Zhu Yib481de92007-09-25 17:54:57 -07002537 /* TODO: what is a good starting rate for STA? About middle? Maybe not
2538 * the lowest or the highest rate.. Could consider using RSSI from
2539 * previous packets? Need to have IEEE 802.1X auth succeed immediately
2540 * after assoc.. */
2541
Tomas Winklerc33104f2008-01-14 17:46:21 -08002542 lq_sta->ibss_sta_added = 0;
Johannes Berg05c914f2008-09-11 00:01:58 +02002543 if (priv->iw_mode == NL80211_IFTYPE_AP) {
Abhijeet Kolekare11bc022009-04-08 11:26:41 -07002544 u8 sta_id = priv->cfg->ops->smgmt->find_station(priv,
2545 sta->addr);
Joe Perches0795af52007-10-03 17:59:30 -07002546
Zhu Yib481de92007-09-25 17:54:57 -07002547 /* for IBSS the call are from tasklet */
Tomas Winklere1623442009-01-27 14:27:56 -08002548 IWL_DEBUG_RATE(priv, "LQ: ADD station %pM\n", sta->addr);
Zhu Yib481de92007-09-25 17:54:57 -07002549
2550 if (sta_id == IWL_INVALID_STATION) {
Tomas Winklere1623442009-01-27 14:27:56 -08002551 IWL_DEBUG_RATE(priv, "LQ: ADD station %pM\n", sta->addr);
Abhijeet Kolekar06fd3d82009-04-08 11:26:42 -07002552 sta_id = priv->cfg->ops->smgmt->add_station(priv,
Abhijeet Kolekare11bc022009-04-08 11:26:41 -07002553 sta->addr, 0,
2554 CMD_ASYNC, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07002555 }
2556 if ((sta_id != IWL_INVALID_STATION)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002557 lq_sta->lq.sta_id = sta_id;
2558 lq_sta->lq.rs_table[0].rate_n_flags = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002559 }
2560 /* FIXME: this is w/a remove it later */
2561 priv->assoc_station_added = 1;
2562 }
2563
Tomas Winklerc33104f2008-01-14 17:46:21 -08002564 lq_sta->is_dup = 0;
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002565 lq_sta->max_rate_idx = -1;
Abbas, Mohamed7d049e52009-01-20 21:33:53 -08002566 lq_sta->missed_rate_counter = IWL_MISSED_RATE_MAX;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002567 lq_sta->is_green = rs_use_green(priv, conf);
Guy Cohen39e88502008-04-23 17:14:57 -07002568 lq_sta->active_legacy_rate = priv->active_rate & ~(0x1000);
Tomas Winklerc33104f2008-01-14 17:46:21 -08002569 lq_sta->active_rate_basic = priv->active_rate_basic;
Johannes Berg8318d782008-01-24 19:38:38 +01002570 lq_sta->band = priv->band;
Ben Cahill77626352007-11-29 11:09:44 +08002571 /*
2572 * active_siso_rate mask includes 9 MBits (bit 5), and CCK (bits 0-3),
2573 * supp_rates[] does not; shift to convert format, force 9 MBits off.
2574 */
Johannes Bergae5eb022008-10-14 16:58:37 +02002575 lq_sta->active_siso_rate = sta->ht_cap.mcs.rx_mask[0] << 1;
2576 lq_sta->active_siso_rate |= sta->ht_cap.mcs.rx_mask[0] & 0x1;
Guy Cohenfde0db32008-04-21 15:42:01 -07002577 lq_sta->active_siso_rate &= ~((u16)0x2);
2578 lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -07002579
Ben Cahill77626352007-11-29 11:09:44 +08002580 /* Same here */
Johannes Bergae5eb022008-10-14 16:58:37 +02002581 lq_sta->active_mimo2_rate = sta->ht_cap.mcs.rx_mask[1] << 1;
2582 lq_sta->active_mimo2_rate |= sta->ht_cap.mcs.rx_mask[1] & 0x1;
Guy Cohenfde0db32008-04-21 15:42:01 -07002583 lq_sta->active_mimo2_rate &= ~((u16)0x2);
2584 lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE;
2585
Johannes Bergae5eb022008-10-14 16:58:37 +02002586 lq_sta->active_mimo3_rate = sta->ht_cap.mcs.rx_mask[2] << 1;
2587 lq_sta->active_mimo3_rate |= sta->ht_cap.mcs.rx_mask[2] & 0x1;
Guy Cohenfde0db32008-04-21 15:42:01 -07002588 lq_sta->active_mimo3_rate &= ~((u16)0x2);
2589 lq_sta->active_mimo3_rate <<= IWL_FIRST_OFDM_RATE;
2590
Tomas Winklere1623442009-01-27 14:27:56 -08002591 IWL_DEBUG_RATE(priv, "SISO-RATE=%X MIMO2-RATE=%X MIMO3-RATE=%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002592 lq_sta->active_siso_rate,
Guy Cohenfde0db32008-04-21 15:42:01 -07002593 lq_sta->active_mimo2_rate,
2594 lq_sta->active_mimo3_rate);
2595
Tomas Winklera96a27f2008-10-23 23:48:56 -07002596 /* These values will be overridden later */
Guy Cohen07bc28e2008-04-23 17:15:03 -07002597 lq_sta->lq.general_params.single_stream_ant_msk = ANT_A;
2598 lq_sta->lq.general_params.dual_stream_ant_msk = ANT_AB;
2599
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002600 /* as default allow aggregation for all tids */
2601 lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002602 lq_sta->drv = priv;
Zhu Yib481de92007-09-25 17:54:57 -07002603
Mohamed Abbasd5e49032008-12-12 08:22:15 -08002604 /* Find highest tx rate supported by hardware and destination station */
Abbas, Mohamed7d049e52009-01-20 21:33:53 -08002605 mask_bit = sta->supp_rates[sband->band];
2606 count = sband->n_bitrates;
2607 if (sband->band == IEEE80211_BAND_5GHZ) {
2608 count += IWL_FIRST_OFDM_RATE;
2609 start_rate = IWL_FIRST_OFDM_RATE;
2610 mask_bit <<= IWL_FIRST_OFDM_RATE;
2611 }
2612
2613 mask_bit = mask_bit & lq_sta->active_legacy_rate;
2614 lq_sta->last_txrate_idx = 4;
2615 for (i = start_rate; i < count; i++)
Mohamed Abbasd5e49032008-12-12 08:22:15 -08002616 if (mask_bit & BIT(i))
2617 lq_sta->last_txrate_idx = i;
2618
Johannes Berg4b7679a2008-09-18 18:14:18 +02002619 rs_initialize_lq(priv, conf, sta, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002620}
2621
Guy Cohenfde0db32008-04-21 15:42:01 -07002622static void rs_fill_link_cmd(const struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08002623 struct iwl_lq_sta *lq_sta, u32 new_rate)
Zhu Yib481de92007-09-25 17:54:57 -07002624{
Tomas Winklere227cea2008-07-18 13:53:05 +08002625 struct iwl_scale_tbl_info tbl_type;
Zhu Yib481de92007-09-25 17:54:57 -07002626 int index = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002627 int rate_idx;
Zhu Yi1b696de2007-09-27 11:27:41 +08002628 int repeat_rate = 0;
Guy Cohenaade00c2008-04-23 17:14:59 -07002629 u8 ant_toggle_cnt = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002630 u8 use_ht_possible = 1;
Guy Cohenaade00c2008-04-23 17:14:59 -07002631 u8 valid_tx_ant = 0;
Guy Cohen07bc28e2008-04-23 17:15:03 -07002632 struct iwl_link_quality_cmd *lq_cmd = &lq_sta->lq;
Zhu Yib481de92007-09-25 17:54:57 -07002633
Ben Cahill77626352007-11-29 11:09:44 +08002634 /* Override starting rate (index 0) if needed for debug purposes */
Guy Cohen39e88502008-04-23 17:14:57 -07002635 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Zhu Yi98d7e092007-09-27 11:27:42 +08002636
Guy Cohen39e88502008-04-23 17:14:57 -07002637 /* Interpret new_rate (rate_n_flags) */
Guy Cohenaade00c2008-04-23 17:14:59 -07002638 memset(&tbl_type, 0, sizeof(tbl_type));
Guy Cohen39e88502008-04-23 17:14:57 -07002639 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band,
Zhu Yib481de92007-09-25 17:54:57 -07002640 &tbl_type, &rate_idx);
2641
Ben Cahill77626352007-11-29 11:09:44 +08002642 /* How many times should we repeat the initial rate? */
Zhu Yib481de92007-09-25 17:54:57 -07002643 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07002644 ant_toggle_cnt = 1;
Zhu Yi1b696de2007-09-27 11:27:41 +08002645 repeat_rate = IWL_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002646 } else {
Zhu Yi1b696de2007-09-27 11:27:41 +08002647 repeat_rate = IWL_HT_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002648 }
Zhu Yib481de92007-09-25 17:54:57 -07002649
2650 lq_cmd->general_params.mimo_delimiter =
2651 is_mimo(tbl_type.lq_type) ? 1 : 0;
Ben Cahill77626352007-11-29 11:09:44 +08002652
2653 /* Fill 1st table entry (index 0) */
Guy Cohen39e88502008-04-23 17:14:57 -07002654 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002655
Guy Cohen07bc28e2008-04-23 17:15:03 -07002656 if (num_of_ant(tbl_type.ant_type) == 1) {
2657 lq_cmd->general_params.single_stream_ant_msk =
2658 tbl_type.ant_type;
2659 } else if (num_of_ant(tbl_type.ant_type) == 2) {
2660 lq_cmd->general_params.dual_stream_ant_msk =
2661 tbl_type.ant_type;
2662 } /* otherwise we don't modify the existing value */
Zhu Yib481de92007-09-25 17:54:57 -07002663
2664 index++;
Zhu Yi1b696de2007-09-27 11:27:41 +08002665 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002666
Guy Cohenaade00c2008-04-23 17:14:59 -07002667 if (priv)
2668 valid_tx_ant = priv->hw_params.valid_tx_ant;
2669
Ben Cahill77626352007-11-29 11:09:44 +08002670 /* Fill rest of rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002671 while (index < LINK_QUAL_MAX_RETRY_NUM) {
Ben Cahill77626352007-11-29 11:09:44 +08002672 /* Repeat initial/next rate.
2673 * For legacy IWL_NUMBER_TRY == 1, this loop will not execute.
2674 * For HT IWL_HT_NUMBER_TRY == 3, this executes twice. */
Zhu Yi1b696de2007-09-27 11:27:41 +08002675 while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
Zhu Yib481de92007-09-25 17:54:57 -07002676 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07002677 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2678 ant_toggle_cnt++;
2679 else if (priv &&
2680 rs_toggle_antenna(valid_tx_ant,
2681 &new_rate, &tbl_type))
2682 ant_toggle_cnt = 1;
2683}
Zhu Yi98d7e092007-09-27 11:27:42 +08002684
Ben Cahill77626352007-11-29 11:09:44 +08002685 /* Override next rate if needed for debug purposes */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002686 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Ben Cahill77626352007-11-29 11:09:44 +08002687
2688 /* Fill next table entry */
Zhu Yib481de92007-09-25 17:54:57 -07002689 lq_cmd->rs_table[index].rate_n_flags =
Guy Cohen39e88502008-04-23 17:14:57 -07002690 cpu_to_le32(new_rate);
Zhu Yi1b696de2007-09-27 11:27:41 +08002691 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002692 index++;
2693 }
2694
Guy Cohen39e88502008-04-23 17:14:57 -07002695 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band, &tbl_type,
Zhu Yib481de92007-09-25 17:54:57 -07002696 &rate_idx);
2697
Ben Cahill77626352007-11-29 11:09:44 +08002698 /* Indicate to uCode which entries might be MIMO.
2699 * If initial rate was MIMO, this will finally end up
2700 * as (IWL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
Zhu Yib481de92007-09-25 17:54:57 -07002701 if (is_mimo(tbl_type.lq_type))
2702 lq_cmd->general_params.mimo_delimiter = index;
2703
Ben Cahill77626352007-11-29 11:09:44 +08002704 /* Get next rate */
Guy Cohen39e88502008-04-23 17:14:57 -07002705 new_rate = rs_get_lower_rate(lq_sta, &tbl_type, rate_idx,
2706 use_ht_possible);
Zhu Yib481de92007-09-25 17:54:57 -07002707
Ben Cahill77626352007-11-29 11:09:44 +08002708 /* How many times should we repeat the next rate? */
Zhu Yib481de92007-09-25 17:54:57 -07002709 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07002710 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2711 ant_toggle_cnt++;
2712 else if (priv &&
2713 rs_toggle_antenna(valid_tx_ant,
2714 &new_rate, &tbl_type))
2715 ant_toggle_cnt = 1;
2716
Zhu Yi1b696de2007-09-27 11:27:41 +08002717 repeat_rate = IWL_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002718 } else {
Zhu Yi1b696de2007-09-27 11:27:41 +08002719 repeat_rate = IWL_HT_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002720 }
Zhu Yib481de92007-09-25 17:54:57 -07002721
Ben Cahill77626352007-11-29 11:09:44 +08002722 /* Don't allow HT rates after next pass.
2723 * rs_get_lower_rate() will change type to LQ_A or LQ_G. */
Zhu Yib481de92007-09-25 17:54:57 -07002724 use_ht_possible = 0;
2725
Ben Cahill77626352007-11-29 11:09:44 +08002726 /* Override next rate if needed for debug purposes */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002727 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Ben Cahill77626352007-11-29 11:09:44 +08002728
2729 /* Fill next table entry */
Guy Cohen39e88502008-04-23 17:14:57 -07002730 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002731
2732 index++;
Zhu Yi1b696de2007-09-27 11:27:41 +08002733 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002734 }
2735
Tomas Winkler5e1dd8d2008-05-29 16:35:17 +08002736 lq_cmd->agg_params.agg_frame_cnt_limit = 64;
Zhu Yib481de92007-09-25 17:54:57 -07002737 lq_cmd->agg_params.agg_dis_start_th = 3;
2738 lq_cmd->agg_params.agg_time_limit = cpu_to_le16(4000);
Zhu Yib481de92007-09-25 17:54:57 -07002739}
2740
Johannes Berg4b7679a2008-09-18 18:14:18 +02002741static void *rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
Zhu Yib481de92007-09-25 17:54:57 -07002742{
Johannes Berg4b7679a2008-09-18 18:14:18 +02002743 return hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07002744}
2745/* rate scale requires free function to be implemented */
2746static void rs_free(void *priv_rate)
2747{
2748 return;
2749}
2750
Johannes Berg4b7679a2008-09-18 18:14:18 +02002751static void rs_free_sta(void *priv_r, struct ieee80211_sta *sta,
2752 void *priv_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002753{
Tomas Winklere227cea2008-07-18 13:53:05 +08002754 struct iwl_lq_sta *lq_sta = priv_sta;
Rami Rosen45527c22008-10-07 09:50:01 +02002755 struct iwl_priv *priv __maybe_unused = priv_r;
Zhu Yib481de92007-09-25 17:54:57 -07002756
Tomas Winklere1623442009-01-27 14:27:56 -08002757 IWL_DEBUG_RATE(priv, "enter\n");
Tomas Winklerc33104f2008-01-14 17:46:21 -08002758 kfree(lq_sta);
Tomas Winklere1623442009-01-27 14:27:56 -08002759 IWL_DEBUG_RATE(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07002760}
2761
2762
Zhu Yi93dc6462007-09-27 11:27:37 +08002763#ifdef CONFIG_MAC80211_DEBUGFS
Zhu Yi5ae212c2007-09-27 11:27:38 +08002764static int open_file_generic(struct inode *inode, struct file *file)
2765{
2766 file->private_data = inode->i_private;
2767 return 0;
2768}
Tomas Winklere227cea2008-07-18 13:53:05 +08002769static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
2770 u32 *rate_n_flags, int index)
Zhu Yi98d7e092007-09-27 11:27:42 +08002771{
Ester Kummerbf403db2008-05-05 10:22:40 +08002772 struct iwl_priv *priv;
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07002773 u8 valid_tx_ant;
2774 u8 ant_sel_tx;
Ester Kummerbf403db2008-05-05 10:22:40 +08002775
2776 priv = lq_sta->drv;
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07002777 valid_tx_ant = priv->hw_params.valid_tx_ant;
Guy Cohen39e88502008-04-23 17:14:57 -07002778 if (lq_sta->dbg_fixed_rate) {
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07002779 ant_sel_tx =
2780 ((lq_sta->dbg_fixed_rate & RATE_MCS_ANT_ABC_MSK)
2781 >> RATE_MCS_ANT_POS);
2782 if ((valid_tx_ant & ant_sel_tx) == ant_sel_tx) {
Guy Cohen39e88502008-04-23 17:14:57 -07002783 *rate_n_flags = lq_sta->dbg_fixed_rate;
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07002784 IWL_DEBUG_RATE(priv, "Fixed rate ON\n");
Guy Cohen39e88502008-04-23 17:14:57 -07002785 } else {
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07002786 lq_sta->dbg_fixed_rate = 0;
2787 IWL_ERR(priv,
2788 "Invalid antenna selection 0x%X, Valid is 0x%X\n",
2789 ant_sel_tx, valid_tx_ant);
2790 IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
Guy Cohen39e88502008-04-23 17:14:57 -07002791 }
Guy Cohen39e88502008-04-23 17:14:57 -07002792 } else {
Tomas Winklere1623442009-01-27 14:27:56 -08002793 IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
Zhu Yi98d7e092007-09-27 11:27:42 +08002794 }
Zhu Yi98d7e092007-09-27 11:27:42 +08002795}
2796
2797static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
2798 const char __user *user_buf, size_t count, loff_t *ppos)
2799{
Tomas Winklere227cea2008-07-18 13:53:05 +08002800 struct iwl_lq_sta *lq_sta = file->private_data;
Ester Kummerbf403db2008-05-05 10:22:40 +08002801 struct iwl_priv *priv;
Zhu Yi98d7e092007-09-27 11:27:42 +08002802 char buf[64];
2803 int buf_size;
2804 u32 parsed_rate;
2805
Ester Kummerbf403db2008-05-05 10:22:40 +08002806 priv = lq_sta->drv;
Zhu Yi98d7e092007-09-27 11:27:42 +08002807 memset(buf, 0, sizeof(buf));
2808 buf_size = min(count, sizeof(buf) - 1);
2809 if (copy_from_user(buf, user_buf, buf_size))
2810 return -EFAULT;
2811
2812 if (sscanf(buf, "%x", &parsed_rate) == 1)
Guy Cohen39e88502008-04-23 17:14:57 -07002813 lq_sta->dbg_fixed_rate = parsed_rate;
Zhu Yi98d7e092007-09-27 11:27:42 +08002814 else
Guy Cohen39e88502008-04-23 17:14:57 -07002815 lq_sta->dbg_fixed_rate = 0;
Zhu Yi98d7e092007-09-27 11:27:42 +08002816
Guy Cohen39e88502008-04-23 17:14:57 -07002817 lq_sta->active_legacy_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
2818 lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2819 lq_sta->active_mimo2_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2820 lq_sta->active_mimo3_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
Zhu Yi98d7e092007-09-27 11:27:42 +08002821
Tomas Winklere1623442009-01-27 14:27:56 -08002822 IWL_DEBUG_RATE(priv, "sta_id %d rate 0x%X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07002823 lq_sta->lq.sta_id, lq_sta->dbg_fixed_rate);
Zhu Yi98d7e092007-09-27 11:27:42 +08002824
Guy Cohen39e88502008-04-23 17:14:57 -07002825 if (lq_sta->dbg_fixed_rate) {
Guy Cohen07bc28e2008-04-23 17:15:03 -07002826 rs_fill_link_cmd(NULL, lq_sta, lq_sta->dbg_fixed_rate);
Tomas Winkler66c73db2008-04-15 16:01:40 -07002827 iwl_send_lq_cmd(lq_sta->drv, &lq_sta->lq, CMD_ASYNC);
Zhu Yi98d7e092007-09-27 11:27:42 +08002828 }
2829
2830 return count;
2831}
Zhu Yi0209dc12007-09-27 11:27:43 +08002832
Zhu Yi5ae212c2007-09-27 11:27:38 +08002833static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
2834 char __user *user_buf, size_t count, loff_t *ppos)
2835{
Frank Seidela412c802009-03-01 20:25:38 +01002836 char *buff;
Zhu Yi5ae212c2007-09-27 11:27:38 +08002837 int desc = 0;
2838 int i = 0;
Wey-Yi Guy12b96812009-04-08 11:39:27 -07002839 int index = 0;
Frank Seidela412c802009-03-01 20:25:38 +01002840 ssize_t ret;
Zhu Yi5ae212c2007-09-27 11:27:38 +08002841
Tomas Winklere227cea2008-07-18 13:53:05 +08002842 struct iwl_lq_sta *lq_sta = file->private_data;
Wey-Yi Guyd8ae4f52009-03-10 14:35:07 -07002843 struct iwl_priv *priv;
Wey-Yi Guyadb7b5e2009-03-10 14:35:08 -07002844 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002845
Wey-Yi Guyd8ae4f52009-03-10 14:35:07 -07002846 priv = lq_sta->drv;
Frank Seidela412c802009-03-01 20:25:38 +01002847 buff = kmalloc(1024, GFP_KERNEL);
2848 if (!buff)
2849 return -ENOMEM;
2850
Tomas Winklerc33104f2008-01-14 17:46:21 -08002851 desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
Zhu Yi98d7e092007-09-27 11:27:42 +08002852 desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002853 lq_sta->total_failed, lq_sta->total_success,
Guy Cohen39e88502008-04-23 17:14:57 -07002854 lq_sta->active_legacy_rate);
Zhu Yi98d7e092007-09-27 11:27:42 +08002855 desc += sprintf(buff+desc, "fixed rate 0x%X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07002856 lq_sta->dbg_fixed_rate);
Wey-Yi Guyd8ae4f52009-03-10 14:35:07 -07002857 desc += sprintf(buff+desc, "valid_tx_ant %s%s%s\n",
2858 (priv->hw_params.valid_tx_ant & ANT_A) ? "ANT_A," : "",
2859 (priv->hw_params.valid_tx_ant & ANT_B) ? "ANT_B," : "",
2860 (priv->hw_params.valid_tx_ant & ANT_C) ? "ANT_C" : "");
Wey-Yi Guyadb7b5e2009-03-10 14:35:08 -07002861 desc += sprintf(buff+desc, "lq type %s\n",
2862 (is_legacy(tbl->lq_type)) ? "legacy" : "HT");
2863 if (is_Ht(tbl->lq_type)) {
2864 desc += sprintf(buff+desc, " %s",
2865 (is_siso(tbl->lq_type)) ? "SISO" :
2866 ((is_mimo2(tbl->lq_type)) ? "MIMO2" : "MIMO3"));
2867 desc += sprintf(buff+desc, " %s",
2868 (tbl->is_fat) ? "40MHz" : "20MHz");
2869 desc += sprintf(buff+desc, " %s\n", (tbl->is_SGI) ? "SGI" : "");
2870 }
Wey-Yi Guy12b96812009-04-08 11:39:27 -07002871 desc += sprintf(buff+desc, "last tx rate=0x%X\n",
2872 lq_sta->last_rate_n_flags);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002873 desc += sprintf(buff+desc, "general:"
2874 "flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002875 lq_sta->lq.general_params.flags,
2876 lq_sta->lq.general_params.mimo_delimiter,
2877 lq_sta->lq.general_params.single_stream_ant_msk,
2878 lq_sta->lq.general_params.dual_stream_ant_msk);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002879
2880 desc += sprintf(buff+desc, "agg:"
2881 "time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002882 le16_to_cpu(lq_sta->lq.agg_params.agg_time_limit),
2883 lq_sta->lq.agg_params.agg_dis_start_th,
2884 lq_sta->lq.agg_params.agg_frame_cnt_limit);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002885
2886 desc += sprintf(buff+desc,
2887 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002888 lq_sta->lq.general_params.start_rate_index[0],
2889 lq_sta->lq.general_params.start_rate_index[1],
2890 lq_sta->lq.general_params.start_rate_index[2],
2891 lq_sta->lq.general_params.start_rate_index[3]);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002892
Wey-Yi Guy12b96812009-04-08 11:39:27 -07002893 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
2894 index = iwl_hwrate_to_plcp_idx(
2895 le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags));
2896 if (is_legacy(tbl->lq_type)) {
2897 desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps\n",
2898 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags),
2899 iwl_rate_mcs[index].mbps);
2900 } else {
2901 desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps (%s)\n",
2902 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags),
2903 iwl_rate_mcs[index].mbps, iwl_rate_mcs[index].mcs);
2904 }
2905 }
Zhu Yi5ae212c2007-09-27 11:27:38 +08002906
Frank Seidela412c802009-03-01 20:25:38 +01002907 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2908 kfree(buff);
2909 return ret;
Zhu Yi5ae212c2007-09-27 11:27:38 +08002910}
2911
2912static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
Zhu Yi98d7e092007-09-27 11:27:42 +08002913 .write = rs_sta_dbgfs_scale_table_write,
Zhu Yi5ae212c2007-09-27 11:27:38 +08002914 .read = rs_sta_dbgfs_scale_table_read,
2915 .open = open_file_generic,
2916};
Zhu Yi0209dc12007-09-27 11:27:43 +08002917static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
2918 char __user *user_buf, size_t count, loff_t *ppos)
2919{
Frank Seidela412c802009-03-01 20:25:38 +01002920 char *buff;
Zhu Yi0209dc12007-09-27 11:27:43 +08002921 int desc = 0;
2922 int i, j;
Frank Seidela412c802009-03-01 20:25:38 +01002923 ssize_t ret;
Zhu Yi0209dc12007-09-27 11:27:43 +08002924
Tomas Winklere227cea2008-07-18 13:53:05 +08002925 struct iwl_lq_sta *lq_sta = file->private_data;
Frank Seidela412c802009-03-01 20:25:38 +01002926
2927 buff = kmalloc(1024, GFP_KERNEL);
2928 if (!buff)
2929 return -ENOMEM;
2930
Zhu Yi0209dc12007-09-27 11:27:43 +08002931 for (i = 0; i < LQ_SIZE; i++) {
2932 desc += sprintf(buff+desc, "%s type=%d SGI=%d FAT=%d DUP=%d\n"
2933 "rate=0x%X\n",
Abhijeet Kolekarc3056062008-11-12 13:14:08 -08002934 lq_sta->active_tbl == i ? "*" : "x",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002935 lq_sta->lq_info[i].lq_type,
2936 lq_sta->lq_info[i].is_SGI,
2937 lq_sta->lq_info[i].is_fat,
2938 lq_sta->lq_info[i].is_dup,
Guy Cohen39e88502008-04-23 17:14:57 -07002939 lq_sta->lq_info[i].current_rate);
Zhu Yi0209dc12007-09-27 11:27:43 +08002940 for (j = 0; j < IWL_RATE_COUNT; j++) {
2941 desc += sprintf(buff+desc,
Tomas Winklerc33104f2008-01-14 17:46:21 -08002942 "counter=%d success=%d %%=%d\n",
2943 lq_sta->lq_info[i].win[j].counter,
2944 lq_sta->lq_info[i].win[j].success_counter,
2945 lq_sta->lq_info[i].win[j].success_ratio);
Zhu Yi0209dc12007-09-27 11:27:43 +08002946 }
2947 }
Frank Seidela412c802009-03-01 20:25:38 +01002948 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2949 kfree(buff);
2950 return ret;
Zhu Yi0209dc12007-09-27 11:27:43 +08002951}
2952
2953static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
2954 .read = rs_sta_dbgfs_stats_table_read,
2955 .open = open_file_generic,
2956};
Zhu Yi5ae212c2007-09-27 11:27:38 +08002957
Zhu Yi93dc6462007-09-27 11:27:37 +08002958static void rs_add_debugfs(void *priv, void *priv_sta,
2959 struct dentry *dir)
2960{
Tomas Winklere227cea2008-07-18 13:53:05 +08002961 struct iwl_lq_sta *lq_sta = priv_sta;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002962 lq_sta->rs_sta_dbgfs_scale_table_file =
Zhu Yi0209dc12007-09-27 11:27:43 +08002963 debugfs_create_file("rate_scale_table", 0600, dir,
Tomas Winklerc33104f2008-01-14 17:46:21 -08002964 lq_sta, &rs_sta_dbgfs_scale_table_ops);
2965 lq_sta->rs_sta_dbgfs_stats_table_file =
Zhu Yi0209dc12007-09-27 11:27:43 +08002966 debugfs_create_file("rate_stats_table", 0600, dir,
Tomas Winklerc33104f2008-01-14 17:46:21 -08002967 lq_sta, &rs_sta_dbgfs_stats_table_ops);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002968 lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file =
2969 debugfs_create_u8("tx_agg_tid_enable", 0600, dir,
2970 &lq_sta->tx_agg_tid_en);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002971
Zhu Yi93dc6462007-09-27 11:27:37 +08002972}
2973
2974static void rs_remove_debugfs(void *priv, void *priv_sta)
2975{
Tomas Winklere227cea2008-07-18 13:53:05 +08002976 struct iwl_lq_sta *lq_sta = priv_sta;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002977 debugfs_remove(lq_sta->rs_sta_dbgfs_scale_table_file);
2978 debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002979 debugfs_remove(lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file);
Zhu Yi93dc6462007-09-27 11:27:37 +08002980}
2981#endif
2982
Zhu Yib481de92007-09-25 17:54:57 -07002983static struct rate_control_ops rs_ops = {
2984 .module = NULL,
2985 .name = RS_NAME,
2986 .tx_status = rs_tx_status,
2987 .get_rate = rs_get_rate,
2988 .rate_init = rs_rate_init,
Zhu Yib481de92007-09-25 17:54:57 -07002989 .alloc = rs_alloc,
2990 .free = rs_free,
2991 .alloc_sta = rs_alloc_sta,
2992 .free_sta = rs_free_sta,
Zhu Yi93dc6462007-09-27 11:27:37 +08002993#ifdef CONFIG_MAC80211_DEBUGFS
2994 .add_sta_debugfs = rs_add_debugfs,
2995 .remove_sta_debugfs = rs_remove_debugfs,
2996#endif
Zhu Yib481de92007-09-25 17:54:57 -07002997};
2998
Tomas Winklere227cea2008-07-18 13:53:05 +08002999int iwlagn_rate_control_register(void)
Zhu Yib481de92007-09-25 17:54:57 -07003000{
Reinette Chatre897e1cf2008-03-28 16:21:09 -07003001 return ieee80211_rate_control_register(&rs_ops);
Zhu Yib481de92007-09-25 17:54:57 -07003002}
3003
Tomas Winklere227cea2008-07-18 13:53:05 +08003004void iwlagn_rate_control_unregister(void)
Zhu Yib481de92007-09-25 17:54:57 -07003005{
3006 ieee80211_rate_control_unregister(&rs_ops);
3007}
3008