blob: 3fa06afce86a8a1b0c9c008c2e52d06d4cd705a9 [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_* */
Mohamed Abbasd6e93392009-05-08 13:44:39 -0700103 u8 max_search; /* maximun number of tables we can search */
Ben Cahill77626352007-11-29 11:09:44 +0800104 s32 *expected_tpt; /* throughput metrics; expected_tpt_G, etc. */
Guy Cohen39e88502008-04-23 17:14:57 -0700105 u32 current_rate; /* rate_n_flags, uCode API format */
Tomas Winklere227cea2008-07-18 13:53:05 +0800106 struct iwl_rate_scale_data win[IWL_RATE_COUNT]; /* rate histories */
Zhu Yib481de92007-09-25 17:54:57 -0700107};
108
Tomas Winklere227cea2008-07-18 13:53:05 +0800109struct iwl_traffic_load {
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200110 unsigned long time_stamp; /* age of the oldest statistics */
111 u32 packet_count[TID_QUEUE_MAX_SIZE]; /* packet count in this time
112 * slice */
113 u32 total; /* total num of packets during the
114 * last TID_MAX_TIME_DIFF */
115 u8 queue_count; /* number of queues that has
116 * been used since the last cleanup */
117 u8 head; /* start of the circular buffer */
118};
119
Ben Cahill77626352007-11-29 11:09:44 +0800120/**
Tomas Winklere227cea2008-07-18 13:53:05 +0800121 * struct iwl_lq_sta -- driver's rate scaling private structure
Ben Cahill77626352007-11-29 11:09:44 +0800122 *
123 * Pointer to this gets passed back and forth between driver and mac80211.
124 */
Tomas Winklere227cea2008-07-18 13:53:05 +0800125struct iwl_lq_sta {
Ben Cahill77626352007-11-29 11:09:44 +0800126 u8 active_tbl; /* index of active table, range 0-1 */
127 u8 enable_counter; /* indicates HT mode */
128 u8 stay_in_tbl; /* 1: disallow, 0: allow search for new mode */
129 u8 search_better_tbl; /* 1: currently trying alternate mode */
Zhu Yib481de92007-09-25 17:54:57 -0700130 s32 last_tpt;
Ben Cahill77626352007-11-29 11:09:44 +0800131
132 /* The following determine when to search for a new mode */
Zhu Yib481de92007-09-25 17:54:57 -0700133 u32 table_count_limit;
Ben Cahill77626352007-11-29 11:09:44 +0800134 u32 max_failure_limit; /* # failed frames before new search */
135 u32 max_success_limit; /* # successful frames before new search */
Zhu Yib481de92007-09-25 17:54:57 -0700136 u32 table_count;
Ben Cahill77626352007-11-29 11:09:44 +0800137 u32 total_failed; /* total failed frames, any/all rates */
138 u32 total_success; /* total successful frames, any/all rates */
Mohamed Abbas447fee72009-04-20 14:37:02 -0700139 u64 flush_timer; /* time staying in mode before new search */
Ben Cahill77626352007-11-29 11:09:44 +0800140
141 u8 action_counter; /* # mode-switch actions tried */
Zhu Yib481de92007-09-25 17:54:57 -0700142 u8 is_green;
143 u8 is_dup;
Johannes Berg8318d782008-01-24 19:38:38 +0100144 enum ieee80211_band band;
Zhu Yib481de92007-09-25 17:54:57 -0700145 u8 ibss_sta_added;
Ben Cahill77626352007-11-29 11:09:44 +0800146
147 /* The following are bitmaps of rates; IWL_RATE_6M_MASK, etc. */
Zhu Yi02dede02007-09-27 11:27:40 +0800148 u32 supp_rates;
Guy Cohen39e88502008-04-23 17:14:57 -0700149 u16 active_legacy_rate;
Zhu Yib481de92007-09-25 17:54:57 -0700150 u16 active_siso_rate;
Guy Cohenfde0db32008-04-21 15:42:01 -0700151 u16 active_mimo2_rate;
152 u16 active_mimo3_rate;
Zhu Yib481de92007-09-25 17:54:57 -0700153 u16 active_rate_basic;
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -0800154 s8 max_rate_idx; /* Max rate set by user */
Abbas, Mohamed7d049e52009-01-20 21:33:53 -0800155 u8 missed_rate_counter;
Ben Cahill77626352007-11-29 11:09:44 +0800156
Tomas Winkler66c73db2008-04-15 16:01:40 -0700157 struct iwl_link_quality_cmd lq;
Tomas Winklere227cea2008-07-18 13:53:05 +0800158 struct iwl_scale_tbl_info lq_info[LQ_SIZE]; /* "active", "search" */
159 struct iwl_traffic_load load[TID_MAX_LOAD_COUNT];
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200160 u8 tx_agg_tid_en;
Zhu Yi5ae212c2007-09-27 11:27:38 +0800161#ifdef CONFIG_MAC80211_DEBUGFS
Zhu Yi98d7e092007-09-27 11:27:42 +0800162 struct dentry *rs_sta_dbgfs_scale_table_file;
Zhu Yi0209dc12007-09-27 11:27:43 +0800163 struct dentry *rs_sta_dbgfs_stats_table_file;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200164 struct dentry *rs_sta_dbgfs_tx_agg_tid_en_file;
Guy Cohen39e88502008-04-23 17:14:57 -0700165 u32 dbg_fixed_rate;
Zhu Yi5ae212c2007-09-27 11:27:38 +0800166#endif
Zhu Yi62430652008-05-05 10:22:46 +0800167 struct iwl_priv *drv;
Johannes Bergb7e35002008-09-11 02:22:58 +0200168
169 /* used to be in sta_info */
170 int last_txrate_idx;
Wey-Yi Guy12b96812009-04-08 11:39:27 -0700171 /* last tx rate_n_flags */
172 u32 last_rate_n_flags;
Zhu Yib481de92007-09-25 17:54:57 -0700173};
174
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700175static void rs_rate_scale_perform(struct iwl_priv *priv,
Gábor Stefanik514d65c2009-04-23 19:36:08 +0200176 struct sk_buff *skb,
Johannes Berg4b7679a2008-09-18 18:14:18 +0200177 struct ieee80211_sta *sta,
178 struct iwl_lq_sta *lq_sta);
Guy Cohenfde0db32008-04-21 15:42:01 -0700179static void rs_fill_link_cmd(const struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +0800180 struct iwl_lq_sta *lq_sta, u32 rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -0700181
182
Zhu Yi98d7e092007-09-27 11:27:42 +0800183#ifdef CONFIG_MAC80211_DEBUGFS
Tomas Winklere227cea2008-07-18 13:53:05 +0800184static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
185 u32 *rate_n_flags, int index);
Zhu Yi98d7e092007-09-27 11:27:42 +0800186#else
Tomas Winklere227cea2008-07-18 13:53:05 +0800187static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
188 u32 *rate_n_flags, int index)
Zhu Yi98d7e092007-09-27 11:27:42 +0800189{}
190#endif
Ben Cahill77626352007-11-29 11:09:44 +0800191
192/*
193 * Expected throughput metrics for following rates:
194 * 1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
195 * "G" is the only table that supports CCK (the first 4 rates).
196 */
Wey-Yi Guy584a0f02009-04-01 14:33:24 -0700197
Zhu Yib481de92007-09-25 17:54:57 -0700198static s32 expected_tpt_A[IWL_RATE_COUNT] = {
199 0, 0, 0, 0, 40, 57, 72, 98, 121, 154, 177, 186, 186
200};
201
202static s32 expected_tpt_G[IWL_RATE_COUNT] = {
203 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 186
204};
205
206static s32 expected_tpt_siso20MHz[IWL_RATE_COUNT] = {
207 0, 0, 0, 0, 42, 42, 76, 102, 124, 159, 183, 193, 202
208};
209
210static s32 expected_tpt_siso20MHzSGI[IWL_RATE_COUNT] = {
211 0, 0, 0, 0, 46, 46, 82, 110, 132, 168, 192, 202, 211
212};
213
Wey-Yi Guy584a0f02009-04-01 14:33:24 -0700214static s32 expected_tpt_mimo2_20MHz[IWL_RATE_COUNT] = {
Zhu Yib481de92007-09-25 17:54:57 -0700215 0, 0, 0, 0, 74, 74, 123, 155, 179, 214, 236, 244, 251
216};
217
Wey-Yi Guy584a0f02009-04-01 14:33:24 -0700218static s32 expected_tpt_mimo2_20MHzSGI[IWL_RATE_COUNT] = {
Zhu Yib481de92007-09-25 17:54:57 -0700219 0, 0, 0, 0, 81, 81, 131, 164, 188, 222, 243, 251, 257
220};
221
222static s32 expected_tpt_siso40MHz[IWL_RATE_COUNT] = {
223 0, 0, 0, 0, 77, 77, 127, 160, 184, 220, 242, 250, 257
224};
225
226static s32 expected_tpt_siso40MHzSGI[IWL_RATE_COUNT] = {
227 0, 0, 0, 0, 83, 83, 135, 169, 193, 229, 250, 257, 264
228};
229
Wey-Yi Guy584a0f02009-04-01 14:33:24 -0700230static s32 expected_tpt_mimo2_40MHz[IWL_RATE_COUNT] = {
Zhu Yib481de92007-09-25 17:54:57 -0700231 0, 0, 0, 0, 123, 123, 182, 214, 235, 264, 279, 285, 289
232};
233
Wey-Yi Guy584a0f02009-04-01 14:33:24 -0700234static s32 expected_tpt_mimo2_40MHzSGI[IWL_RATE_COUNT] = {
Zhu Yib481de92007-09-25 17:54:57 -0700235 0, 0, 0, 0, 131, 131, 191, 222, 242, 270, 284, 289, 293
236};
237
Wey-Yi Guy584a0f02009-04-01 14:33:24 -0700238/* Expected throughput metric MIMO3 */
239static s32 expected_tpt_mimo3_20MHz[IWL_RATE_COUNT] = {
240 0, 0, 0, 0, 99, 99, 153, 186, 208, 239, 256, 263, 268
241};
242
243static s32 expected_tpt_mimo3_20MHzSGI[IWL_RATE_COUNT] = {
244 0, 0, 0, 0, 106, 106, 162, 194, 215, 246, 262, 268, 273
245};
246
247static s32 expected_tpt_mimo3_40MHz[IWL_RATE_COUNT] = {
248 0, 0, 0, 0, 152, 152, 211, 239, 255, 279, 290, 294, 297
249};
250
251static s32 expected_tpt_mimo3_40MHzSGI[IWL_RATE_COUNT] = {
252 0, 0, 0, 0, 160, 160, 219, 245, 261, 284, 294, 297, 300
253};
254
Wey-Yi Guy12b96812009-04-08 11:39:27 -0700255/* mbps, mcs */
256const static struct iwl_rate_mcs_info iwl_rate_mcs[IWL_RATE_COUNT] = {
257 {"1", ""},
258 {"2", ""},
259 {"5.5", ""},
260 {"11", ""},
261 {"6", "BPSK 1/2"},
262 {"9", "BPSK 1/2"},
263 {"12", "QPSK 1/2"},
264 {"18", "QPSK 3/4"},
265 {"24", "16QAM 1/2"},
266 {"36", "16QAM 3/4"},
267 {"48", "64QAM 2/3"},
268 {"54", "64QAM 3/4"},
269 {"60", "64QAM 5/6"}
270};
271
Guy Cohen39e88502008-04-23 17:14:57 -0700272static inline u8 rs_extract_rate(u32 rate_n_flags)
Zhu Yib481de92007-09-25 17:54:57 -0700273{
274 return (u8)(rate_n_flags & 0xFF);
275}
276
Tomas Winklere227cea2008-07-18 13:53:05 +0800277static void rs_rate_scale_clear_window(struct iwl_rate_scale_data *window)
Zhu Yib481de92007-09-25 17:54:57 -0700278{
279 window->data = 0;
280 window->success_counter = 0;
281 window->success_ratio = IWL_INVALID_VALUE;
282 window->counter = 0;
283 window->average_tpt = IWL_INVALID_VALUE;
284 window->stamp = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700285}
286
Guy Cohenaade00c2008-04-23 17:14:59 -0700287static inline u8 rs_is_valid_ant(u8 valid_antenna, u8 ant_type)
288{
Tomas Winkler3ac7f142008-07-21 02:40:14 +0300289 return (ant_type & valid_antenna) == ant_type;
Guy Cohenaade00c2008-04-23 17:14:59 -0700290}
291
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200292/*
293 * removes the old data from the statistics. All data that is older than
294 * TID_MAX_TIME_DIFF, will be deleted.
295 */
Tomas Winklere227cea2008-07-18 13:53:05 +0800296static void rs_tl_rm_old_stats(struct iwl_traffic_load *tl, u32 curr_time)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200297{
298 /* The oldest age we want to keep */
299 u32 oldest_time = curr_time - TID_MAX_TIME_DIFF;
300
301 while (tl->queue_count &&
302 (tl->time_stamp < oldest_time)) {
303 tl->total -= tl->packet_count[tl->head];
304 tl->packet_count[tl->head] = 0;
305 tl->time_stamp += TID_QUEUE_CELL_SPACING;
306 tl->queue_count--;
307 tl->head++;
308 if (tl->head >= TID_QUEUE_MAX_SIZE)
309 tl->head = 0;
310 }
311}
312
313/*
314 * increment traffic load value for tid and also remove
315 * any old values if passed the certain time period
316 */
Tomas Winklere227cea2008-07-18 13:53:05 +0800317static u8 rs_tl_add_packet(struct iwl_lq_sta *lq_data,
Ron Rindjunskyfaa29712008-06-12 09:46:58 +0800318 struct ieee80211_hdr *hdr)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200319{
320 u32 curr_time = jiffies_to_msecs(jiffies);
321 u32 time_diff;
322 s32 index;
Tomas Winklere227cea2008-07-18 13:53:05 +0800323 struct iwl_traffic_load *tl = NULL;
Tomas Winkler54dbb522008-05-15 13:54:06 +0800324 u8 tid;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200325
Tomas Winkler417f1142008-11-12 13:14:06 -0800326 if (ieee80211_is_data_qos(hdr->frame_control)) {
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700327 u8 *qc = ieee80211_get_qos_ctl(hdr);
Tomas Winkler54dbb522008-05-15 13:54:06 +0800328 tid = qc[0] & 0xf;
329 } else
Ron Rindjunskyfaa29712008-06-12 09:46:58 +0800330 return MAX_TID_COUNT;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200331
332 tl = &lq_data->load[tid];
333
334 curr_time -= curr_time % TID_ROUND_VALUE;
335
336 /* Happens only for the first packet. Initialize the data */
337 if (!(tl->queue_count)) {
338 tl->total = 1;
339 tl->time_stamp = curr_time;
340 tl->queue_count = 1;
341 tl->head = 0;
342 tl->packet_count[0] = 1;
Ron Rindjunskyfaa29712008-06-12 09:46:58 +0800343 return MAX_TID_COUNT;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200344 }
345
346 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
347 index = time_diff / TID_QUEUE_CELL_SPACING;
348
349 /* The history is too long: remove data that is older than */
350 /* TID_MAX_TIME_DIFF */
351 if (index >= TID_QUEUE_MAX_SIZE)
352 rs_tl_rm_old_stats(tl, curr_time);
353
354 index = (tl->head + index) % TID_QUEUE_MAX_SIZE;
355 tl->packet_count[index] = tl->packet_count[index] + 1;
356 tl->total = tl->total + 1;
357
358 if ((index + 1) > tl->queue_count)
359 tl->queue_count = index + 1;
Ron Rindjunskyfaa29712008-06-12 09:46:58 +0800360
361 return tid;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200362}
363
364/*
365 get the traffic load value for tid
366*/
Tomas Winklere227cea2008-07-18 13:53:05 +0800367static u32 rs_tl_get_load(struct iwl_lq_sta *lq_data, u8 tid)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200368{
369 u32 curr_time = jiffies_to_msecs(jiffies);
370 u32 time_diff;
371 s32 index;
Tomas Winklere227cea2008-07-18 13:53:05 +0800372 struct iwl_traffic_load *tl = NULL;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200373
374 if (tid >= TID_MAX_LOAD_COUNT)
375 return 0;
376
377 tl = &(lq_data->load[tid]);
378
379 curr_time -= curr_time % TID_ROUND_VALUE;
380
381 if (!(tl->queue_count))
382 return 0;
383
384 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
385 index = time_diff / TID_QUEUE_CELL_SPACING;
386
387 /* The history is too long: remove data that is older than */
388 /* TID_MAX_TIME_DIFF */
389 if (index >= TID_QUEUE_MAX_SIZE)
390 rs_tl_rm_old_stats(tl, curr_time);
391
392 return tl->total;
393}
394
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700395static void rs_tl_turn_on_agg_for_tid(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +0800396 struct iwl_lq_sta *lq_data, u8 tid,
Johannes Berg4b7679a2008-09-18 18:14:18 +0200397 struct ieee80211_sta *sta)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200398{
Johannes Bergff550cb2008-09-11 03:17:05 +0200399 if (rs_tl_get_load(lq_data, tid) > IWL_AGG_LOAD_THRESHOLD) {
Tomas Winklere1623442009-01-27 14:27:56 -0800400 IWL_DEBUG_HT(priv, "Starting Tx agg: STA: %pM tid: %d\n",
Johannes Berge1749612008-10-27 15:59:26 -0700401 sta->addr, tid);
Johannes Berg4b7679a2008-09-18 18:14:18 +0200402 ieee80211_start_tx_ba_session(priv->hw, sta->addr, tid);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200403 }
404}
405
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700406static void rs_tl_turn_on_agg(struct iwl_priv *priv, u8 tid,
Tomas Winklere227cea2008-07-18 13:53:05 +0800407 struct iwl_lq_sta *lq_data,
Johannes Berg4b7679a2008-09-18 18:14:18 +0200408 struct ieee80211_sta *sta)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200409{
410 if ((tid < TID_MAX_LOAD_COUNT))
411 rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
412 else if (tid == IWL_AGG_ALL_TID)
413 for (tid = 0; tid < TID_MAX_LOAD_COUNT; tid++)
414 rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
415}
416
Guy Cohen39e88502008-04-23 17:14:57 -0700417static inline int get_num_of_ant_from_rate(u32 rate_n_flags)
Guy Cohenfde0db32008-04-21 15:42:01 -0700418{
Tomas Winkler3ac7f142008-07-21 02:40:14 +0300419 return !!(rate_n_flags & RATE_MCS_ANT_A_MSK) +
420 !!(rate_n_flags & RATE_MCS_ANT_B_MSK) +
421 !!(rate_n_flags & RATE_MCS_ANT_C_MSK);
Guy Cohenfde0db32008-04-21 15:42:01 -0700422}
423
Ben Cahill77626352007-11-29 11:09:44 +0800424/**
425 * rs_collect_tx_data - Update the success/failure sliding window
426 *
427 * We keep a sliding window of the last 62 packets transmitted
428 * at this rate. window->data contains the bitmask of successful
429 * packets.
430 */
Tomas Winklere227cea2008-07-18 13:53:05 +0800431static int rs_collect_tx_data(struct iwl_rate_scale_data *windows,
Ron Rindjunsky99556432008-01-28 14:07:25 +0200432 int scale_index, s32 tpt, int retries,
433 int successes)
Zhu Yib481de92007-09-25 17:54:57 -0700434{
Tomas Winklere227cea2008-07-18 13:53:05 +0800435 struct iwl_rate_scale_data *window = NULL;
Guy Cohen39e88502008-04-23 17:14:57 -0700436 static const u64 mask = (((u64)1) << (IWL_RATE_MAX_WINDOW - 1));
Zhu Yib481de92007-09-25 17:54:57 -0700437 s32 fail_count;
438
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800439 if (scale_index < 0 || scale_index >= IWL_RATE_COUNT)
440 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -0700441
Ben Cahill77626352007-11-29 11:09:44 +0800442 /* Select data for current tx bit rate */
Zhu Yib481de92007-09-25 17:54:57 -0700443 window = &(windows[scale_index]);
444
Ben Cahill77626352007-11-29 11:09:44 +0800445 /*
446 * Keep track of only the latest 62 tx frame attempts in this rate's
447 * history window; anything older isn't really relevant any more.
448 * If we have filled up the sliding window, drop the oldest attempt;
449 * if the oldest attempt (highest bit in bitmap) shows "success",
450 * subtract "1" from the success counter (this is the main reason
451 * we keep these bitmaps!).
452 */
Ron Rindjunsky99556432008-01-28 14:07:25 +0200453 while (retries > 0) {
Guy Cohen39e88502008-04-23 17:14:57 -0700454 if (window->counter >= IWL_RATE_MAX_WINDOW) {
455
456 /* remove earliest */
457 window->counter = IWL_RATE_MAX_WINDOW - 1;
458
Ron Rindjunsky99556432008-01-28 14:07:25 +0200459 if (window->data & mask) {
460 window->data &= ~mask;
Guy Cohen39e88502008-04-23 17:14:57 -0700461 window->success_counter--;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200462 }
Zhu Yib481de92007-09-25 17:54:57 -0700463 }
Zhu Yib481de92007-09-25 17:54:57 -0700464
Ron Rindjunsky99556432008-01-28 14:07:25 +0200465 /* Increment frames-attempted counter */
466 window->counter++;
Ben Cahill77626352007-11-29 11:09:44 +0800467
Ron Rindjunsky99556432008-01-28 14:07:25 +0200468 /* Shift bitmap by one frame (throw away oldest history),
469 * OR in "1", and increment "success" if this
470 * frame was successful. */
Guy Cohen90d77952008-09-09 10:54:53 +0800471 window->data <<= 1;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200472 if (successes > 0) {
Guy Cohen39e88502008-04-23 17:14:57 -0700473 window->success_counter++;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200474 window->data |= 0x1;
475 successes--;
476 }
477
478 retries--;
Zhu Yib481de92007-09-25 17:54:57 -0700479 }
480
Ben Cahill77626352007-11-29 11:09:44 +0800481 /* Calculate current success ratio, avoid divide-by-0! */
Zhu Yib481de92007-09-25 17:54:57 -0700482 if (window->counter > 0)
483 window->success_ratio = 128 * (100 * window->success_counter)
484 / window->counter;
485 else
486 window->success_ratio = IWL_INVALID_VALUE;
487
488 fail_count = window->counter - window->success_counter;
489
Ben Cahill77626352007-11-29 11:09:44 +0800490 /* Calculate average throughput, if we have enough history. */
Zhu Yib481de92007-09-25 17:54:57 -0700491 if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
492 (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
493 window->average_tpt = (window->success_ratio * tpt + 64) / 128;
494 else
495 window->average_tpt = IWL_INVALID_VALUE;
496
Ben Cahill77626352007-11-29 11:09:44 +0800497 /* Tag this window as having been updated */
Zhu Yib481de92007-09-25 17:54:57 -0700498 window->stamp = jiffies;
499
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800500 return 0;
Zhu Yib481de92007-09-25 17:54:57 -0700501}
502
Ben Cahill77626352007-11-29 11:09:44 +0800503/*
504 * Fill uCode API rate_n_flags field, based on "search" or "active" table.
505 */
Guy Cohen39e88502008-04-23 17:14:57 -0700506/* FIXME:RS:remove this function and put the flags statically in the table */
Tomas Winkler978785a2008-12-19 10:37:31 +0800507static u32 rate_n_flags_from_tbl(struct iwl_priv *priv,
508 struct iwl_scale_tbl_info *tbl,
509 int index, u8 use_green)
Zhu Yib481de92007-09-25 17:54:57 -0700510{
Guy Cohen39e88502008-04-23 17:14:57 -0700511 u32 rate_n_flags = 0;
512
Zhu Yib481de92007-09-25 17:54:57 -0700513 if (is_legacy(tbl->lq_type)) {
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800514 rate_n_flags = iwl_rates[index].plcp;
Zhu Yib481de92007-09-25 17:54:57 -0700515 if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
Guy Cohen39e88502008-04-23 17:14:57 -0700516 rate_n_flags |= RATE_MCS_CCK_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700517
Guy Cohenfde0db32008-04-21 15:42:01 -0700518 } else if (is_Ht(tbl->lq_type)) {
519 if (index > IWL_LAST_OFDM_RATE) {
Tomas Winkler978785a2008-12-19 10:37:31 +0800520 IWL_ERR(priv, "Invalid HT rate index %d\n", index);
Zhu Yib481de92007-09-25 17:54:57 -0700521 index = IWL_LAST_OFDM_RATE;
Guy Cohenfde0db32008-04-21 15:42:01 -0700522 }
Guy Cohen39e88502008-04-23 17:14:57 -0700523 rate_n_flags = RATE_MCS_HT_MSK;
Guy Cohenfde0db32008-04-21 15:42:01 -0700524
525 if (is_siso(tbl->lq_type))
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800526 rate_n_flags |= iwl_rates[index].plcp_siso;
Guy Cohenfde0db32008-04-21 15:42:01 -0700527 else if (is_mimo2(tbl->lq_type))
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800528 rate_n_flags |= iwl_rates[index].plcp_mimo2;
Guy Cohenfde0db32008-04-21 15:42:01 -0700529 else
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800530 rate_n_flags |= iwl_rates[index].plcp_mimo3;
Zhu Yib481de92007-09-25 17:54:57 -0700531 } else {
Tomas Winkler978785a2008-12-19 10:37:31 +0800532 IWL_ERR(priv, "Invalid tbl->lq_type %d\n", tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -0700533 }
534
Guy Cohen39e88502008-04-23 17:14:57 -0700535 rate_n_flags |= ((tbl->ant_type << RATE_MCS_ANT_POS) &
Guy Cohenfde0db32008-04-21 15:42:01 -0700536 RATE_MCS_ANT_ABC_MSK);
Zhu Yib481de92007-09-25 17:54:57 -0700537
Guy Cohen39e88502008-04-23 17:14:57 -0700538 if (is_Ht(tbl->lq_type)) {
539 if (tbl->is_fat) {
540 if (tbl->is_dup)
541 rate_n_flags |= RATE_MCS_DUP_MSK;
542 else
543 rate_n_flags |= RATE_MCS_FAT_MSK;
544 }
545 if (tbl->is_SGI)
546 rate_n_flags |= RATE_MCS_SGI_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700547
Guy Cohen39e88502008-04-23 17:14:57 -0700548 if (use_green) {
549 rate_n_flags |= RATE_MCS_GF_MSK;
550 if (is_siso(tbl->lq_type) && tbl->is_SGI) {
551 rate_n_flags &= ~RATE_MCS_SGI_MSK;
Tomas Winkler978785a2008-12-19 10:37:31 +0800552 IWL_ERR(priv, "GF was set with SGI:SISO\n");
Guy Cohen39e88502008-04-23 17:14:57 -0700553 }
554 }
Zhu Yib481de92007-09-25 17:54:57 -0700555 }
Guy Cohen39e88502008-04-23 17:14:57 -0700556 return rate_n_flags;
Zhu Yib481de92007-09-25 17:54:57 -0700557}
558
Ben Cahill77626352007-11-29 11:09:44 +0800559/*
560 * Interpret uCode API's rate_n_flags format,
561 * fill "search" or "active" tx mode table.
562 */
Guy Cohen39e88502008-04-23 17:14:57 -0700563static int rs_get_tbl_info_from_mcs(const u32 rate_n_flags,
Johannes Berg8318d782008-01-24 19:38:38 +0100564 enum ieee80211_band band,
Tomas Winklere227cea2008-07-18 13:53:05 +0800565 struct iwl_scale_tbl_info *tbl,
Zhu Yib481de92007-09-25 17:54:57 -0700566 int *rate_idx)
567{
Guy Cohen39e88502008-04-23 17:14:57 -0700568 u32 ant_msk = (rate_n_flags & RATE_MCS_ANT_ABC_MSK);
569 u8 num_of_ant = get_num_of_ant_from_rate(rate_n_flags);
570 u8 mcs;
Zhu Yib481de92007-09-25 17:54:57 -0700571
Tomas Winklere7d326a2008-06-12 09:47:11 +0800572 *rate_idx = iwl_hwrate_to_plcp_idx(rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -0700573
Guy Cohenfde0db32008-04-21 15:42:01 -0700574 if (*rate_idx == IWL_RATE_INVALID) {
Zhu Yib481de92007-09-25 17:54:57 -0700575 *rate_idx = -1;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800576 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -0700577 }
Ben Cahill77626352007-11-29 11:09:44 +0800578 tbl->is_SGI = 0; /* default legacy setup */
Zhu Yib481de92007-09-25 17:54:57 -0700579 tbl->is_fat = 0;
580 tbl->is_dup = 0;
Guy Cohenfde0db32008-04-21 15:42:01 -0700581 tbl->ant_type = (ant_msk >> RATE_MCS_ANT_POS);
582 tbl->lq_type = LQ_NONE;
Mohamed Abbasd6e93392009-05-08 13:44:39 -0700583 tbl->max_search = IWL_MAX_SEARCH;
Zhu Yib481de92007-09-25 17:54:57 -0700584
Ben Cahill77626352007-11-29 11:09:44 +0800585 /* legacy rate format */
Guy Cohen39e88502008-04-23 17:14:57 -0700586 if (!(rate_n_flags & RATE_MCS_HT_MSK)) {
Guy Cohenfde0db32008-04-21 15:42:01 -0700587 if (num_of_ant == 1) {
Johannes Berg8318d782008-01-24 19:38:38 +0100588 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700589 tbl->lq_type = LQ_A;
590 else
591 tbl->lq_type = LQ_G;
Zhu Yib481de92007-09-25 17:54:57 -0700592 }
Guy Cohenfde0db32008-04-21 15:42:01 -0700593 /* HT rate format */
Zhu Yib481de92007-09-25 17:54:57 -0700594 } else {
Guy Cohen39e88502008-04-23 17:14:57 -0700595 if (rate_n_flags & RATE_MCS_SGI_MSK)
Zhu Yib481de92007-09-25 17:54:57 -0700596 tbl->is_SGI = 1;
597
Guy Cohen39e88502008-04-23 17:14:57 -0700598 if ((rate_n_flags & RATE_MCS_FAT_MSK) ||
599 (rate_n_flags & RATE_MCS_DUP_MSK))
Zhu Yib481de92007-09-25 17:54:57 -0700600 tbl->is_fat = 1;
601
Guy Cohen39e88502008-04-23 17:14:57 -0700602 if (rate_n_flags & RATE_MCS_DUP_MSK)
Zhu Yib481de92007-09-25 17:54:57 -0700603 tbl->is_dup = 1;
Guy Cohenfde0db32008-04-21 15:42:01 -0700604
Guy Cohen39e88502008-04-23 17:14:57 -0700605 mcs = rs_extract_rate(rate_n_flags);
Guy Cohenfde0db32008-04-21 15:42:01 -0700606
Guy Cohen39e88502008-04-23 17:14:57 -0700607 /* SISO */
608 if (mcs <= IWL_RATE_SISO_60M_PLCP) {
Guy Cohenfde0db32008-04-21 15:42:01 -0700609 if (num_of_ant == 1)
610 tbl->lq_type = LQ_SISO; /*else NONE*/
611 /* MIMO2 */
Guy Cohen39e88502008-04-23 17:14:57 -0700612 } else if (mcs <= IWL_RATE_MIMO2_60M_PLCP) {
Guy Cohenfde0db32008-04-21 15:42:01 -0700613 if (num_of_ant == 2)
614 tbl->lq_type = LQ_MIMO2;
615 /* MIMO3 */
616 } else {
Mohamed Abbasd6e93392009-05-08 13:44:39 -0700617 if (num_of_ant == 3) {
618 tbl->max_search = IWL_MAX_11N_MIMO3_SEARCH;
Guy Cohenfde0db32008-04-21 15:42:01 -0700619 tbl->lq_type = LQ_MIMO3;
Mohamed Abbasd6e93392009-05-08 13:44:39 -0700620 }
Guy Cohenfde0db32008-04-21 15:42:01 -0700621 }
Zhu Yib481de92007-09-25 17:54:57 -0700622 }
623 return 0;
624}
Guy Cohenaade00c2008-04-23 17:14:59 -0700625
626/* switch to another antenna/antennas and return 1 */
627/* if no other valid antenna found, return 0 */
628static int rs_toggle_antenna(u32 valid_ant, u32 *rate_n_flags,
Tomas Winklere227cea2008-07-18 13:53:05 +0800629 struct iwl_scale_tbl_info *tbl)
Zhu Yib481de92007-09-25 17:54:57 -0700630{
Guy Cohenaade00c2008-04-23 17:14:59 -0700631 u8 new_ant_type;
632
633 if (!tbl->ant_type || tbl->ant_type > ANT_ABC)
634 return 0;
635
636 if (!rs_is_valid_ant(valid_ant, tbl->ant_type))
637 return 0;
638
639 new_ant_type = ant_toggle_lookup[tbl->ant_type];
640
641 while ((new_ant_type != tbl->ant_type) &&
642 !rs_is_valid_ant(valid_ant, new_ant_type))
643 new_ant_type = ant_toggle_lookup[new_ant_type];
644
645 if (new_ant_type == tbl->ant_type)
646 return 0;
647
648 tbl->ant_type = new_ant_type;
649 *rate_n_flags &= ~RATE_MCS_ANT_ABC_MSK;
650 *rate_n_flags |= new_ant_type << RATE_MCS_ANT_POS;
651 return 1;
Zhu Yib481de92007-09-25 17:54:57 -0700652}
653
Guy Cohen39e88502008-04-23 17:14:57 -0700654/* FIXME:RS: in 4965 we don't use greenfield at all */
Guy Cohenf935a6d2008-04-23 17:15:02 -0700655/* FIXME:RS: don't use greenfield for now in TX */
Guy Cohenf935a6d2008-04-23 17:15:02 -0700656#if 0
657static inline u8 rs_use_green(struct iwl_priv *priv, struct ieee80211_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -0700658{
Tomas Winkler3ac7f142008-07-21 02:40:14 +0300659 return (conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) &&
Ron Rindjunsky270243a2007-11-26 16:14:41 +0200660 priv->current_ht_config.is_green_field &&
Tomas Winkler3ac7f142008-07-21 02:40:14 +0300661 !priv->current_ht_config.non_GF_STA_present;
Zhu Yib481de92007-09-25 17:54:57 -0700662}
Ron Rindjunsky4f85f5b2008-06-09 22:54:35 +0300663#endif
Guy Cohenf935a6d2008-04-23 17:15:02 -0700664static inline u8 rs_use_green(struct iwl_priv *priv, struct ieee80211_conf *conf)
665{
666 return 0;
667}
Zhu Yib481de92007-09-25 17:54:57 -0700668
669/**
670 * rs_get_supported_rates - get the available rates
671 *
672 * if management frame or broadcast frame only return
673 * basic available rates.
674 *
675 */
Tomas Winklere227cea2008-07-18 13:53:05 +0800676static u16 rs_get_supported_rates(struct iwl_lq_sta *lq_sta,
677 struct ieee80211_hdr *hdr,
678 enum iwl_table_type rate_type)
Zhu Yib481de92007-09-25 17:54:57 -0700679{
Zhu Yib481de92007-09-25 17:54:57 -0700680 if (hdr && is_multicast_ether_addr(hdr->addr1) &&
Guy Coheneecd6e52008-04-23 17:14:58 -0700681 lq_sta->active_rate_basic)
682 return lq_sta->active_rate_basic;
683
684 if (is_legacy(rate_type)) {
685 return lq_sta->active_legacy_rate;
686 } else {
687 if (is_siso(rate_type))
688 return lq_sta->active_siso_rate;
689 else if (is_mimo2(rate_type))
690 return lq_sta->active_mimo2_rate;
691 else
692 return lq_sta->active_mimo3_rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -0800693 }
Zhu Yib481de92007-09-25 17:54:57 -0700694}
695
Ester Kummerbf403db2008-05-05 10:22:40 +0800696static u16 rs_get_adjacent_rate(struct iwl_priv *priv, u8 index, u16 rate_mask,
697 int rate_type)
Zhu Yib481de92007-09-25 17:54:57 -0700698{
699 u8 high = IWL_RATE_INVALID;
700 u8 low = IWL_RATE_INVALID;
701
Ian Schram01ebd062007-10-25 17:15:22 +0800702 /* 802.11A or ht walks to the next literal adjacent rate in
Zhu Yib481de92007-09-25 17:54:57 -0700703 * the rate table */
704 if (is_a_band(rate_type) || !is_legacy(rate_type)) {
705 int i;
706 u32 mask;
707
708 /* Find the previous rate that is in the rate mask */
709 i = index - 1;
710 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
711 if (rate_mask & mask) {
712 low = i;
713 break;
714 }
715 }
716
717 /* Find the next rate that is in the rate mask */
718 i = index + 1;
719 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
720 if (rate_mask & mask) {
721 high = i;
722 break;
723 }
724 }
725
726 return (high << 8) | low;
727 }
728
729 low = index;
730 while (low != IWL_RATE_INVALID) {
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800731 low = iwl_rates[low].prev_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700732 if (low == IWL_RATE_INVALID)
733 break;
734 if (rate_mask & (1 << low))
735 break;
Tomas Winklere1623442009-01-27 14:27:56 -0800736 IWL_DEBUG_RATE(priv, "Skipping masked lower rate: %d\n", low);
Zhu Yib481de92007-09-25 17:54:57 -0700737 }
738
739 high = index;
740 while (high != IWL_RATE_INVALID) {
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800741 high = iwl_rates[high].next_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700742 if (high == IWL_RATE_INVALID)
743 break;
744 if (rate_mask & (1 << high))
745 break;
Tomas Winklere1623442009-01-27 14:27:56 -0800746 IWL_DEBUG_RATE(priv, "Skipping masked higher rate: %d\n", high);
Zhu Yib481de92007-09-25 17:54:57 -0700747 }
748
749 return (high << 8) | low;
750}
751
Tomas Winklere227cea2008-07-18 13:53:05 +0800752static u32 rs_get_lower_rate(struct iwl_lq_sta *lq_sta,
753 struct iwl_scale_tbl_info *tbl,
754 u8 scale_index, u8 ht_possible)
Zhu Yib481de92007-09-25 17:54:57 -0700755{
Zhu Yib481de92007-09-25 17:54:57 -0700756 s32 low;
757 u16 rate_mask;
758 u16 high_low;
759 u8 switch_to_legacy = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -0800760 u8 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -0700761
762 /* check if we need to switch from HT to legacy rates.
763 * assumption is that mandatory rates (1Mbps or 6Mbps)
764 * are always supported (spec demand) */
765 if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_index)) {
766 switch_to_legacy = 1;
767 scale_index = rs_ht_to_legacy[scale_index];
Johannes Berg8318d782008-01-24 19:38:38 +0100768 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700769 tbl->lq_type = LQ_A;
770 else
771 tbl->lq_type = LQ_G;
772
Guy Cohen69fdb302008-04-23 17:15:01 -0700773 if (num_of_ant(tbl->ant_type) > 1)
Guy Cohenfde0db32008-04-21 15:42:01 -0700774 tbl->ant_type = ANT_A;/*FIXME:RS*/
Zhu Yib481de92007-09-25 17:54:57 -0700775
776 tbl->is_fat = 0;
777 tbl->is_SGI = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -0700778 tbl->max_search = IWL_MAX_SEARCH;
Zhu Yib481de92007-09-25 17:54:57 -0700779 }
780
Guy Coheneecd6e52008-04-23 17:14:58 -0700781 rate_mask = rs_get_supported_rates(lq_sta, NULL, tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -0700782
Ben Cahill77626352007-11-29 11:09:44 +0800783 /* Mask with station rate restriction */
Zhu Yib481de92007-09-25 17:54:57 -0700784 if (is_legacy(tbl->lq_type)) {
Ben Cahill77626352007-11-29 11:09:44 +0800785 /* supp_rates has no CCK bits in A mode */
Johannes Berg8318d782008-01-24 19:38:38 +0100786 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700787 rate_mask = (u16)(rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -0800788 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
Zhu Yib481de92007-09-25 17:54:57 -0700789 else
Tomas Winklerc33104f2008-01-14 17:46:21 -0800790 rate_mask = (u16)(rate_mask & lq_sta->supp_rates);
Zhu Yib481de92007-09-25 17:54:57 -0700791 }
792
Ben Cahill77626352007-11-29 11:09:44 +0800793 /* If we switched from HT to legacy, check current rate */
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800794 if (switch_to_legacy && (rate_mask & (1 << scale_index))) {
Guy Cohen39e88502008-04-23 17:14:57 -0700795 low = scale_index;
796 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700797 }
798
Ester Kummerbf403db2008-05-05 10:22:40 +0800799 high_low = rs_get_adjacent_rate(lq_sta->drv, scale_index, rate_mask,
800 tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -0700801 low = high_low & 0xff;
802
Guy Cohen39e88502008-04-23 17:14:57 -0700803 if (low == IWL_RATE_INVALID)
804 low = scale_index;
805
806out:
Tomas Winkler978785a2008-12-19 10:37:31 +0800807 return rate_n_flags_from_tbl(lq_sta->drv, tbl, low, is_green);
Zhu Yib481de92007-09-25 17:54:57 -0700808}
809
Ben Cahill77626352007-11-29 11:09:44 +0800810/*
811 * mac80211 sends us Tx status
812 */
Johannes Berg4b7679a2008-09-18 18:14:18 +0200813static void rs_tx_status(void *priv_r, struct ieee80211_supported_band *sband,
814 struct ieee80211_sta *sta, void *priv_sta,
Johannes Berge039fa42008-05-15 12:55:29 +0200815 struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -0700816{
817 int status;
818 u8 retries;
819 int rs_index, index = 0;
Tomas Winkler417f1142008-11-12 13:14:06 -0800820 struct iwl_lq_sta *lq_sta = priv_sta;
Tomas Winkler66c73db2008-04-15 16:01:40 -0700821 struct iwl_link_quality_cmd *table;
Zhu Yib481de92007-09-25 17:54:57 -0700822 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berg4b7679a2008-09-18 18:14:18 +0200823 struct iwl_priv *priv = (struct iwl_priv *)priv_r;
824 struct ieee80211_hw *hw = priv->hw;
Johannes Berge039fa42008-05-15 12:55:29 +0200825 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Tomas Winklere227cea2008-07-18 13:53:05 +0800826 struct iwl_rate_scale_data *window = NULL;
827 struct iwl_rate_scale_data *search_win = NULL;
Guy Cohen39e88502008-04-23 17:14:57 -0700828 u32 tx_rate;
Tomas Winklere227cea2008-07-18 13:53:05 +0800829 struct iwl_scale_tbl_info tbl_type;
830 struct iwl_scale_tbl_info *curr_tbl, *search_tbl;
Zhu Yib481de92007-09-25 17:54:57 -0700831 u8 active_index = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700832 s32 tpt = 0;
833
Tomas Winklere1623442009-01-27 14:27:56 -0800834 IWL_DEBUG_RATE_LIMIT(priv, "get frame ack response, update rate scale window\n");
Zhu Yib481de92007-09-25 17:54:57 -0700835
Tomas Winkler417f1142008-11-12 13:14:06 -0800836 if (!ieee80211_is_data(hdr->frame_control) ||
Gábor Stefanik514d65c2009-04-23 19:36:08 +0200837 info->flags & IEEE80211_TX_CTL_NO_ACK)
Zhu Yib481de92007-09-25 17:54:57 -0700838 return;
839
Ron Rindjunsky99556432008-01-28 14:07:25 +0200840 /* This packet was aggregated but doesn't carry rate scale info */
Johannes Berge039fa42008-05-15 12:55:29 +0200841 if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
842 !(info->flags & IEEE80211_TX_STAT_AMPDU))
Ron Rindjunsky99556432008-01-28 14:07:25 +0200843 return;
844
Wey-Yi Guy8fe72312009-03-10 14:35:10 -0700845 if (info->flags & IEEE80211_TX_STAT_AMPDU)
846 retries = 0;
847 else
848 retries = info->status.rates[0].count - 1;
Zhu Yib481de92007-09-25 17:54:57 -0700849
850 if (retries > 15)
851 retries = 15;
852
Johannes Berg05c914f2008-09-11 00:01:58 +0200853 if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) &&
Tomas Winklerc33104f2008-01-14 17:46:21 -0800854 !lq_sta->ibss_sta_added)
Tomas Winklerf4d60822008-03-05 11:31:00 -0800855 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700856
Tomas Winklerc33104f2008-01-14 17:46:21 -0800857 table = &lq_sta->lq;
858 active_index = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -0700859
Tomas Winklerc33104f2008-01-14 17:46:21 -0800860 curr_tbl = &(lq_sta->lq_info[active_index]);
861 search_tbl = &(lq_sta->lq_info[(1 - active_index)]);
Tomas Winklere227cea2008-07-18 13:53:05 +0800862 window = (struct iwl_rate_scale_data *)&(curr_tbl->win[0]);
863 search_win = (struct iwl_rate_scale_data *)&(search_tbl->win[0]);
Zhu Yib481de92007-09-25 17:54:57 -0700864
Ben Cahill77626352007-11-29 11:09:44 +0800865 /*
866 * Ignore this Tx frame response if its initial rate doesn't match
867 * that of latest Link Quality command. There may be stragglers
868 * from a previous Link Quality command, but we're no longer interested
869 * in those; they're either from the "active" mode while we're trying
870 * to check "search" mode, or a prior "search" mode after we've moved
871 * to a new "search" mode (which might become the new "active" mode).
872 */
Guy Cohen39e88502008-04-23 17:14:57 -0700873 tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
874 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800875 if (priv->band == IEEE80211_BAND_5GHZ)
876 rs_index -= IWL_FIRST_OFDM_RATE;
877
Johannes Berge6a98542008-10-21 12:40:02 +0200878 if ((info->status.rates[0].idx < 0) ||
879 (tbl_type.is_SGI != !!(info->status.rates[0].flags & IEEE80211_TX_RC_SHORT_GI)) ||
880 (tbl_type.is_fat != !!(info->status.rates[0].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)) ||
881 (tbl_type.is_dup != !!(info->status.rates[0].flags & IEEE80211_TX_RC_DUP_DATA)) ||
882 (tbl_type.ant_type != info->antenna_sel_tx) ||
883 (!!(tx_rate & RATE_MCS_HT_MSK) != !!(info->status.rates[0].flags & IEEE80211_TX_RC_MCS)) ||
884 (!!(tx_rate & RATE_MCS_GF_MSK) != !!(info->status.rates[0].flags & IEEE80211_TX_RC_GREEN_FIELD)) ||
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800885 (hw->wiphy->bands[priv->band]->bitrates[rs_index].bitrate !=
Johannes Berge6a98542008-10-21 12:40:02 +0200886 hw->wiphy->bands[info->band]->bitrates[info->status.rates[0].idx].bitrate)) {
Tomas Winklere1623442009-01-27 14:27:56 -0800887 IWL_DEBUG_RATE(priv, "initial rate does not match 0x%x\n", tx_rate);
Mohamed Abbasd5e49032008-12-12 08:22:15 -0800888 /* the last LQ command could failed so the LQ in ucode not
889 * the same in driver sync up
890 */
Abbas, Mohamed7d049e52009-01-20 21:33:53 -0800891 lq_sta->missed_rate_counter++;
892 if (lq_sta->missed_rate_counter > IWL_MISSED_RATE_MAX) {
893 lq_sta->missed_rate_counter = 0;
894 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
895 }
Tomas Winklerf4d60822008-03-05 11:31:00 -0800896 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700897 }
898
Abbas, Mohamed7d049e52009-01-20 21:33:53 -0800899 lq_sta->missed_rate_counter = 0;
Ben Cahill77626352007-11-29 11:09:44 +0800900 /* Update frame history window with "failure" for each Tx retry. */
Zhu Yib481de92007-09-25 17:54:57 -0700901 while (retries) {
Ben Cahill77626352007-11-29 11:09:44 +0800902 /* Look up the rate and other info used for each tx attempt.
903 * Each tx attempt steps one entry deeper in the rate table. */
Guy Cohen39e88502008-04-23 17:14:57 -0700904 tx_rate = le32_to_cpu(table->rs_table[index].rate_n_flags);
905 rs_get_tbl_info_from_mcs(tx_rate, priv->band,
Zhu Yib481de92007-09-25 17:54:57 -0700906 &tbl_type, &rs_index);
907
Ben Cahill77626352007-11-29 11:09:44 +0800908 /* If type matches "search" table,
909 * add failure to "search" history */
Zhu Yib481de92007-09-25 17:54:57 -0700910 if ((tbl_type.lq_type == search_tbl->lq_type) &&
Guy Cohenfde0db32008-04-21 15:42:01 -0700911 (tbl_type.ant_type == search_tbl->ant_type) &&
Zhu Yib481de92007-09-25 17:54:57 -0700912 (tbl_type.is_SGI == search_tbl->is_SGI)) {
913 if (search_tbl->expected_tpt)
914 tpt = search_tbl->expected_tpt[rs_index];
915 else
916 tpt = 0;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200917 rs_collect_tx_data(search_win, rs_index, tpt, 1, 0);
Ben Cahill77626352007-11-29 11:09:44 +0800918
919 /* Else if type matches "current/active" table,
920 * add failure to "current/active" history */
Zhu Yib481de92007-09-25 17:54:57 -0700921 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
Guy Cohenfde0db32008-04-21 15:42:01 -0700922 (tbl_type.ant_type == curr_tbl->ant_type) &&
Zhu Yib481de92007-09-25 17:54:57 -0700923 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
924 if (curr_tbl->expected_tpt)
925 tpt = curr_tbl->expected_tpt[rs_index];
926 else
927 tpt = 0;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200928 rs_collect_tx_data(window, rs_index, tpt, 1, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700929 }
Ben Cahill77626352007-11-29 11:09:44 +0800930
931 /* If not searching for a new mode, increment failed counter
932 * ... this helps determine when to start searching again */
Tomas Winklerc33104f2008-01-14 17:46:21 -0800933 if (lq_sta->stay_in_tbl)
934 lq_sta->total_failed++;
Zhu Yib481de92007-09-25 17:54:57 -0700935 --retries;
936 index++;
937
938 }
939
Ben Cahill77626352007-11-29 11:09:44 +0800940 /*
941 * Find (by rate) the history window to update with final Tx attempt;
942 * if Tx was successful first try, use original rate,
943 * else look up the rate that was, finally, successful.
944 */
Guy Cohen39e88502008-04-23 17:14:57 -0700945 tx_rate = le32_to_cpu(table->rs_table[index].rate_n_flags);
Wey-Yi Guy12b96812009-04-08 11:39:27 -0700946 lq_sta->last_rate_n_flags = tx_rate;
Guy Cohen39e88502008-04-23 17:14:57 -0700947 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
Zhu Yib481de92007-09-25 17:54:57 -0700948
Ben Cahill77626352007-11-29 11:09:44 +0800949 /* Update frame history window with "success" if Tx got ACKed ... */
Johannes Berge039fa42008-05-15 12:55:29 +0200950 status = !!(info->flags & IEEE80211_TX_STAT_ACK);
Zhu Yib481de92007-09-25 17:54:57 -0700951
Ben Cahill77626352007-11-29 11:09:44 +0800952 /* If type matches "search" table,
953 * add final tx status to "search" history */
Zhu Yib481de92007-09-25 17:54:57 -0700954 if ((tbl_type.lq_type == search_tbl->lq_type) &&
Guy Cohenfde0db32008-04-21 15:42:01 -0700955 (tbl_type.ant_type == search_tbl->ant_type) &&
Zhu Yib481de92007-09-25 17:54:57 -0700956 (tbl_type.is_SGI == search_tbl->is_SGI)) {
957 if (search_tbl->expected_tpt)
958 tpt = search_tbl->expected_tpt[rs_index];
959 else
960 tpt = 0;
Wey-Yi Guydf36c042009-03-10 14:35:11 -0700961 if (info->flags & IEEE80211_TX_STAT_AMPDU)
Ron Rindjunsky99556432008-01-28 14:07:25 +0200962 rs_collect_tx_data(search_win, rs_index, tpt,
Johannes Berge039fa42008-05-15 12:55:29 +0200963 info->status.ampdu_ack_len,
964 info->status.ampdu_ack_map);
Ron Rindjunsky99556432008-01-28 14:07:25 +0200965 else
966 rs_collect_tx_data(search_win, rs_index, tpt,
967 1, status);
Ben Cahill77626352007-11-29 11:09:44 +0800968 /* Else if type matches "current/active" table,
969 * add final tx status to "current/active" history */
Zhu Yib481de92007-09-25 17:54:57 -0700970 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
Guy Cohenfde0db32008-04-21 15:42:01 -0700971 (tbl_type.ant_type == curr_tbl->ant_type) &&
Zhu Yib481de92007-09-25 17:54:57 -0700972 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
973 if (curr_tbl->expected_tpt)
974 tpt = curr_tbl->expected_tpt[rs_index];
975 else
976 tpt = 0;
Wey-Yi Guydf36c042009-03-10 14:35:11 -0700977 if (info->flags & IEEE80211_TX_STAT_AMPDU)
Ron Rindjunsky99556432008-01-28 14:07:25 +0200978 rs_collect_tx_data(window, rs_index, tpt,
Johannes Berge039fa42008-05-15 12:55:29 +0200979 info->status.ampdu_ack_len,
980 info->status.ampdu_ack_map);
Ron Rindjunsky99556432008-01-28 14:07:25 +0200981 else
982 rs_collect_tx_data(window, rs_index, tpt,
983 1, status);
Zhu Yib481de92007-09-25 17:54:57 -0700984 }
985
Ben Cahill77626352007-11-29 11:09:44 +0800986 /* If not searching for new mode, increment success/failed counter
987 * ... these help determine when to start searching again */
Tomas Winklerc33104f2008-01-14 17:46:21 -0800988 if (lq_sta->stay_in_tbl) {
Wey-Yi Guydf36c042009-03-10 14:35:11 -0700989 if (info->flags & IEEE80211_TX_STAT_AMPDU) {
Johannes Berge039fa42008-05-15 12:55:29 +0200990 lq_sta->total_success += info->status.ampdu_ack_map;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200991 lq_sta->total_failed +=
Johannes Berge039fa42008-05-15 12:55:29 +0200992 (info->status.ampdu_ack_len - info->status.ampdu_ack_map);
Ron Rindjunsky99556432008-01-28 14:07:25 +0200993 } else {
994 if (status)
995 lq_sta->total_success++;
996 else
997 lq_sta->total_failed++;
998 }
Zhu Yib481de92007-09-25 17:54:57 -0700999 }
1000
Ben Cahill77626352007-11-29 11:09:44 +08001001 /* See if there's a better rate or modulation mode to try. */
Abbas, Mohamedc338ba32009-01-21 10:58:02 -08001002 if (sta && sta->supp_rates[sband->band])
Gábor Stefanik514d65c2009-04-23 19:36:08 +02001003 rs_rate_scale_perform(priv, skb, sta, lq_sta);
Tomas Winklerf4d60822008-03-05 11:31:00 -08001004out:
Zhu Yib481de92007-09-25 17:54:57 -07001005 return;
1006}
1007
Ben Cahill77626352007-11-29 11:09:44 +08001008/*
1009 * Begin a period of staying with a selected modulation mode.
1010 * Set "stay_in_tbl" flag to prevent any mode switches.
1011 * Set frame tx success limits according to legacy vs. high-throughput,
1012 * and reset overall (spanning all rates) tx success history statistics.
1013 * These control how long we stay using same modulation mode before
1014 * searching for a new mode.
1015 */
Ester Kummerbf403db2008-05-05 10:22:40 +08001016static void rs_set_stay_in_table(struct iwl_priv *priv, u8 is_legacy,
Tomas Winklere227cea2008-07-18 13:53:05 +08001017 struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07001018{
Tomas Winklere1623442009-01-27 14:27:56 -08001019 IWL_DEBUG_RATE(priv, "we are staying in the same table\n");
Tomas Winklerc33104f2008-01-14 17:46:21 -08001020 lq_sta->stay_in_tbl = 1; /* only place this gets set */
Zhu Yib481de92007-09-25 17:54:57 -07001021 if (is_legacy) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001022 lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT;
1023 lq_sta->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
1024 lq_sta->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
Zhu Yib481de92007-09-25 17:54:57 -07001025 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001026 lq_sta->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
1027 lq_sta->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
1028 lq_sta->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
Zhu Yib481de92007-09-25 17:54:57 -07001029 }
Tomas Winklerc33104f2008-01-14 17:46:21 -08001030 lq_sta->table_count = 0;
1031 lq_sta->total_failed = 0;
1032 lq_sta->total_success = 0;
Mohamed Abbas447fee72009-04-20 14:37:02 -07001033 lq_sta->flush_timer = jiffies;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001034 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001035}
1036
Ben Cahill77626352007-11-29 11:09:44 +08001037/*
1038 * Find correct throughput table for given mode of modulation
1039 */
Tomas Winklere227cea2008-07-18 13:53:05 +08001040static void rs_set_expected_tpt_table(struct iwl_lq_sta *lq_sta,
1041 struct iwl_scale_tbl_info *tbl)
Zhu Yib481de92007-09-25 17:54:57 -07001042{
1043 if (is_legacy(tbl->lq_type)) {
1044 if (!is_a_band(tbl->lq_type))
1045 tbl->expected_tpt = expected_tpt_G;
1046 else
1047 tbl->expected_tpt = expected_tpt_A;
1048 } else if (is_siso(tbl->lq_type)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001049 if (tbl->is_fat && !lq_sta->is_dup)
Zhu Yib481de92007-09-25 17:54:57 -07001050 if (tbl->is_SGI)
1051 tbl->expected_tpt = expected_tpt_siso40MHzSGI;
1052 else
1053 tbl->expected_tpt = expected_tpt_siso40MHz;
1054 else if (tbl->is_SGI)
1055 tbl->expected_tpt = expected_tpt_siso20MHzSGI;
1056 else
1057 tbl->expected_tpt = expected_tpt_siso20MHz;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001058 } else if (is_mimo2(tbl->lq_type)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001059 if (tbl->is_fat && !lq_sta->is_dup)
Zhu Yib481de92007-09-25 17:54:57 -07001060 if (tbl->is_SGI)
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001061 tbl->expected_tpt = expected_tpt_mimo2_40MHzSGI;
Zhu Yib481de92007-09-25 17:54:57 -07001062 else
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001063 tbl->expected_tpt = expected_tpt_mimo2_40MHz;
Zhu Yib481de92007-09-25 17:54:57 -07001064 else if (tbl->is_SGI)
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001065 tbl->expected_tpt = expected_tpt_mimo2_20MHzSGI;
Zhu Yib481de92007-09-25 17:54:57 -07001066 else
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001067 tbl->expected_tpt = expected_tpt_mimo2_20MHz;
1068 } else if (is_mimo3(tbl->lq_type)) {
1069 if (tbl->is_fat && !lq_sta->is_dup)
1070 if (tbl->is_SGI)
1071 tbl->expected_tpt = expected_tpt_mimo3_40MHzSGI;
1072 else
1073 tbl->expected_tpt = expected_tpt_mimo3_40MHz;
1074 else if (tbl->is_SGI)
1075 tbl->expected_tpt = expected_tpt_mimo3_20MHzSGI;
1076 else
1077 tbl->expected_tpt = expected_tpt_mimo3_20MHz;
Zhu Yib481de92007-09-25 17:54:57 -07001078 } else
1079 tbl->expected_tpt = expected_tpt_G;
1080}
1081
Ben Cahill77626352007-11-29 11:09:44 +08001082/*
1083 * Find starting rate for new "search" high-throughput mode of modulation.
1084 * Goal is to find lowest expected rate (under perfect conditions) that is
1085 * above the current measured throughput of "active" mode, to give new mode
1086 * a fair chance to prove itself without too many challenges.
1087 *
1088 * This gets called when transitioning to more aggressive modulation
1089 * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
1090 * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
1091 * to decrease to match "active" throughput. When moving from MIMO to SISO,
1092 * bit rate will typically need to increase, but not if performance was bad.
1093 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001094static s32 rs_get_best_rate(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001095 struct iwl_lq_sta *lq_sta,
1096 struct iwl_scale_tbl_info *tbl, /* "search" */
Guy Cohenf935a6d2008-04-23 17:15:02 -07001097 u16 rate_mask, s8 index)
Zhu Yib481de92007-09-25 17:54:57 -07001098{
Ben Cahill77626352007-11-29 11:09:44 +08001099 /* "active" values */
Tomas Winklere227cea2008-07-18 13:53:05 +08001100 struct iwl_scale_tbl_info *active_tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001101 &(lq_sta->lq_info[lq_sta->active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07001102 s32 active_sr = active_tbl->win[index].success_ratio;
Zhu Yib481de92007-09-25 17:54:57 -07001103 s32 active_tpt = active_tbl->expected_tpt[index];
Ben Cahill77626352007-11-29 11:09:44 +08001104
1105 /* expected "search" throughput */
1106 s32 *tpt_tbl = tbl->expected_tpt;
1107
1108 s32 new_rate, high, low, start_hi;
Zhu Yib481de92007-09-25 17:54:57 -07001109 u16 high_low;
Guy Cohenf935a6d2008-04-23 17:15:02 -07001110 s8 rate = index;
Zhu Yib481de92007-09-25 17:54:57 -07001111
1112 new_rate = high = low = start_hi = IWL_RATE_INVALID;
1113
1114 for (; ;) {
Ester Kummerbf403db2008-05-05 10:22:40 +08001115 high_low = rs_get_adjacent_rate(priv, rate, rate_mask,
1116 tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -07001117
1118 low = high_low & 0xff;
1119 high = (high_low >> 8) & 0xff;
1120
Ben Cahill77626352007-11-29 11:09:44 +08001121 /*
1122 * Lower the "search" bit rate, to give new "search" mode
1123 * approximately the same throughput as "active" if:
1124 *
1125 * 1) "Active" mode has been working modestly well (but not
1126 * great), and expected "search" throughput (under perfect
1127 * conditions) at candidate rate is above the actual
1128 * measured "active" throughput (but less than expected
1129 * "active" throughput under perfect conditions).
1130 * OR
1131 * 2) "Active" mode has been working perfectly or very well
1132 * and expected "search" throughput (under perfect
1133 * conditions) at candidate rate is above expected
1134 * "active" throughput (under perfect conditions).
1135 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001136 if ((((100 * tpt_tbl[rate]) > lq_sta->last_tpt) &&
Zhu Yib481de92007-09-25 17:54:57 -07001137 ((active_sr > IWL_RATE_DECREASE_TH) &&
1138 (active_sr <= IWL_RATE_HIGH_TH) &&
1139 (tpt_tbl[rate] <= active_tpt))) ||
1140 ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
1141 (tpt_tbl[rate] > active_tpt))) {
1142
Ben Cahill77626352007-11-29 11:09:44 +08001143 /* (2nd or later pass)
1144 * If we've already tried to raise the rate, and are
1145 * now trying to lower it, use the higher rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001146 if (start_hi != IWL_RATE_INVALID) {
1147 new_rate = start_hi;
1148 break;
1149 }
Ben Cahill77626352007-11-29 11:09:44 +08001150
Zhu Yib481de92007-09-25 17:54:57 -07001151 new_rate = rate;
Ben Cahill77626352007-11-29 11:09:44 +08001152
1153 /* Loop again with lower rate */
Zhu Yib481de92007-09-25 17:54:57 -07001154 if (low != IWL_RATE_INVALID)
1155 rate = low;
Ben Cahill77626352007-11-29 11:09:44 +08001156
1157 /* Lower rate not available, use the original */
Zhu Yib481de92007-09-25 17:54:57 -07001158 else
1159 break;
Ben Cahill77626352007-11-29 11:09:44 +08001160
1161 /* Else try to raise the "search" rate to match "active" */
Zhu Yib481de92007-09-25 17:54:57 -07001162 } else {
Ben Cahill77626352007-11-29 11:09:44 +08001163 /* (2nd or later pass)
1164 * If we've already tried to lower the rate, and are
1165 * now trying to raise it, use the lower rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001166 if (new_rate != IWL_RATE_INVALID)
1167 break;
Ben Cahill77626352007-11-29 11:09:44 +08001168
1169 /* Loop again with higher rate */
Zhu Yib481de92007-09-25 17:54:57 -07001170 else if (high != IWL_RATE_INVALID) {
1171 start_hi = high;
1172 rate = high;
Ben Cahill77626352007-11-29 11:09:44 +08001173
1174 /* Higher rate not available, use the original */
Zhu Yib481de92007-09-25 17:54:57 -07001175 } else {
Guy Cohen90d77952008-09-09 10:54:53 +08001176 new_rate = rate;
Zhu Yib481de92007-09-25 17:54:57 -07001177 break;
1178 }
1179 }
1180 }
1181
1182 return new_rate;
1183}
Zhu Yib481de92007-09-25 17:54:57 -07001184
Ben Cahill77626352007-11-29 11:09:44 +08001185/*
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001186 * Set up search table for MIMO2
Ben Cahill77626352007-11-29 11:09:44 +08001187 */
Guy Cohenfde0db32008-04-21 15:42:01 -07001188static int rs_switch_to_mimo2(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001189 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001190 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001191 struct ieee80211_sta *sta,
Tomas Winklere227cea2008-07-18 13:53:05 +08001192 struct iwl_scale_tbl_info *tbl, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001193{
Zhu Yib481de92007-09-25 17:54:57 -07001194 u16 rate_mask;
1195 s32 rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001196 s8 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07001197
Luis R. Rodriguezde27e642008-12-23 15:58:44 -08001198 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
Zhu Yib481de92007-09-25 17:54:57 -07001199 return -1;
1200
Johannes Bergd9fe60d2008-10-09 12:13:49 +02001201 if (((sta->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 2)
Tomas Winkler00c5ae22008-09-03 11:26:42 +08001202 == WLAN_HT_CAP_SM_PS_STATIC)
Zhu Yib481de92007-09-25 17:54:57 -07001203 return -1;
1204
Ben Cahill77626352007-11-29 11:09:44 +08001205 /* Need both Tx chains/antennas to support MIMO */
Guy Cohenaade00c2008-04-23 17:14:59 -07001206 if (priv->hw_params.tx_chains_num < 2)
Zhu Yib481de92007-09-25 17:54:57 -07001207 return -1;
1208
Tomas Winklere1623442009-01-27 14:27:56 -08001209 IWL_DEBUG_RATE(priv, "LQ: try to switch to MIMO2\n");
Guy Cohen39e88502008-04-23 17:14:57 -07001210
1211 tbl->lq_type = LQ_MIMO2;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001212 tbl->is_dup = lq_sta->is_dup;
Zhu Yib481de92007-09-25 17:54:57 -07001213 tbl->action = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001214 tbl->max_search = IWL_MAX_SEARCH;
Guy Cohen39e88502008-04-23 17:14:57 -07001215 rate_mask = lq_sta->active_mimo2_rate;
1216
Wey-Yi Guya30199f2009-04-30 13:56:28 -07001217 if (iwl_is_fat_tx_allowed(priv, &sta->ht_cap))
Zhu Yib481de92007-09-25 17:54:57 -07001218 tbl->is_fat = 1;
1219 else
1220 tbl->is_fat = 0;
1221
Guy Cohen39e88502008-04-23 17:14:57 -07001222 /* FIXME: - don't toggle SGI here
Zhu Yib481de92007-09-25 17:54:57 -07001223 if (tbl->is_fat) {
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001224 if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
Zhu Yib481de92007-09-25 17:54:57 -07001225 tbl->is_SGI = 1;
1226 else
1227 tbl->is_SGI = 0;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001228 } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
Zhu Yib481de92007-09-25 17:54:57 -07001229 tbl->is_SGI = 1;
1230 else
1231 tbl->is_SGI = 0;
Guy Cohen39e88502008-04-23 17:14:57 -07001232 */
Zhu Yib481de92007-09-25 17:54:57 -07001233
Guy Coheneecd6e52008-04-23 17:14:58 -07001234 rs_set_expected_tpt_table(lq_sta, tbl);
Zhu Yib481de92007-09-25 17:54:57 -07001235
Guy Cohenf935a6d2008-04-23 17:15:02 -07001236 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
Zhu Yib481de92007-09-25 17:54:57 -07001237
Tomas Winklere1623442009-01-27 14:27:56 -08001238 IWL_DEBUG_RATE(priv, "LQ: MIMO2 best rate %d mask %X\n", rate, rate_mask);
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001239 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1240 IWL_DEBUG_RATE(priv, "Can't switch with index %d rate mask %x\n",
1241 rate, rate_mask);
1242 return -1;
1243 }
1244 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
Guy Cohen39e88502008-04-23 17:14:57 -07001245
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001246 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
1247 tbl->current_rate, is_green);
1248 return 0;
1249}
1250
1251/*
1252 * Set up search table for MIMO3
1253 */
1254static int rs_switch_to_mimo3(struct iwl_priv *priv,
1255 struct iwl_lq_sta *lq_sta,
1256 struct ieee80211_conf *conf,
1257 struct ieee80211_sta *sta,
1258 struct iwl_scale_tbl_info *tbl, int index)
1259{
1260 u16 rate_mask;
1261 s32 rate;
1262 s8 is_green = lq_sta->is_green;
1263
1264 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
1265 return -1;
1266
1267 if (((sta->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 2)
1268 == WLAN_HT_CAP_SM_PS_STATIC)
1269 return -1;
1270
1271 /* Need both Tx chains/antennas to support MIMO */
1272 if (priv->hw_params.tx_chains_num < 3)
1273 return -1;
1274
1275 IWL_DEBUG_RATE(priv, "LQ: try to switch to MIMO3\n");
1276
1277 tbl->lq_type = LQ_MIMO3;
1278 tbl->is_dup = lq_sta->is_dup;
1279 tbl->action = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001280 tbl->max_search = IWL_MAX_11N_MIMO3_SEARCH;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001281 rate_mask = lq_sta->active_mimo3_rate;
1282
Wey-Yi Guya30199f2009-04-30 13:56:28 -07001283 if (iwl_is_fat_tx_allowed(priv, &sta->ht_cap))
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001284 tbl->is_fat = 1;
1285 else
1286 tbl->is_fat = 0;
1287
1288 /* FIXME: - don't toggle SGI here
1289 if (tbl->is_fat) {
1290 if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
1291 tbl->is_SGI = 1;
1292 else
1293 tbl->is_SGI = 0;
1294 } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
1295 tbl->is_SGI = 1;
1296 else
1297 tbl->is_SGI = 0;
1298 */
1299
1300 rs_set_expected_tpt_table(lq_sta, tbl);
1301
1302 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
1303
1304 IWL_DEBUG_RATE(priv, "LQ: MIMO3 best rate %d mask %X\n",
1305 rate, rate_mask);
Guy Cohen39e88502008-04-23 17:14:57 -07001306 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
Tomas Winklere1623442009-01-27 14:27:56 -08001307 IWL_DEBUG_RATE(priv, "Can't switch with index %d rate mask %x\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001308 rate, rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07001309 return -1;
Guy Cohen39e88502008-04-23 17:14:57 -07001310 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001311 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
Zhu Yib481de92007-09-25 17:54:57 -07001312
Tomas Winklere1623442009-01-27 14:27:56 -08001313 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001314 tbl->current_rate, is_green);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001315 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07001316}
1317
Ben Cahill77626352007-11-29 11:09:44 +08001318/*
1319 * Set up search table for SISO
1320 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001321static int rs_switch_to_siso(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001322 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001323 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001324 struct ieee80211_sta *sta,
Tomas Winklere227cea2008-07-18 13:53:05 +08001325 struct iwl_scale_tbl_info *tbl, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001326{
Zhu Yib481de92007-09-25 17:54:57 -07001327 u16 rate_mask;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001328 u8 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07001329 s32 rate;
1330
Luis R. Rodriguezde27e642008-12-23 15:58:44 -08001331 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
Zhu Yib481de92007-09-25 17:54:57 -07001332 return -1;
1333
Tomas Winklere1623442009-01-27 14:27:56 -08001334 IWL_DEBUG_RATE(priv, "LQ: try to switch to SISO\n");
Guy Cohen39e88502008-04-23 17:14:57 -07001335
Tomas Winklerc33104f2008-01-14 17:46:21 -08001336 tbl->is_dup = lq_sta->is_dup;
Zhu Yib481de92007-09-25 17:54:57 -07001337 tbl->lq_type = LQ_SISO;
1338 tbl->action = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001339 tbl->max_search = IWL_MAX_SEARCH;
Guy Cohen39e88502008-04-23 17:14:57 -07001340 rate_mask = lq_sta->active_siso_rate;
Zhu Yib481de92007-09-25 17:54:57 -07001341
Wey-Yi Guya30199f2009-04-30 13:56:28 -07001342 if (iwl_is_fat_tx_allowed(priv, &sta->ht_cap))
Zhu Yib481de92007-09-25 17:54:57 -07001343 tbl->is_fat = 1;
1344 else
1345 tbl->is_fat = 0;
1346
Guy Cohen39e88502008-04-23 17:14:57 -07001347 /* FIXME: - don't toggle SGI here
Zhu Yib481de92007-09-25 17:54:57 -07001348 if (tbl->is_fat) {
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001349 if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
Zhu Yib481de92007-09-25 17:54:57 -07001350 tbl->is_SGI = 1;
1351 else
1352 tbl->is_SGI = 0;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001353 } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
Zhu Yib481de92007-09-25 17:54:57 -07001354 tbl->is_SGI = 1;
1355 else
1356 tbl->is_SGI = 0;
Guy Cohen39e88502008-04-23 17:14:57 -07001357 */
Zhu Yib481de92007-09-25 17:54:57 -07001358
1359 if (is_green)
Guy Cohen39e88502008-04-23 17:14:57 -07001360 tbl->is_SGI = 0; /*11n spec: no SGI in SISO+Greenfield*/
Zhu Yib481de92007-09-25 17:54:57 -07001361
Guy Coheneecd6e52008-04-23 17:14:58 -07001362 rs_set_expected_tpt_table(lq_sta, tbl);
Guy Cohenf935a6d2008-04-23 17:15:02 -07001363 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
Zhu Yib481de92007-09-25 17:54:57 -07001364
Tomas Winklere1623442009-01-27 14:27:56 -08001365 IWL_DEBUG_RATE(priv, "LQ: get best rate %d mask %X\n", rate, rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07001366 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
Tomas Winklere1623442009-01-27 14:27:56 -08001367 IWL_DEBUG_RATE(priv, "can not switch with index %d rate mask %x\n",
Zhu Yib481de92007-09-25 17:54:57 -07001368 rate, rate_mask);
1369 return -1;
1370 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001371 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
Tomas Winklere1623442009-01-27 14:27:56 -08001372 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001373 tbl->current_rate, is_green);
Mohamed Abbas403ab562007-11-06 22:06:25 -08001374 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07001375}
1376
Ben Cahill77626352007-11-29 11:09:44 +08001377/*
1378 * Try to switch to new modulation mode from legacy
1379 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001380static int rs_move_legacy_other(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001381 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001382 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001383 struct ieee80211_sta *sta,
Zhu Yib481de92007-09-25 17:54:57 -07001384 int index)
1385{
Tomas Winklere227cea2008-07-18 13:53:05 +08001386 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1387 struct iwl_scale_tbl_info *search_tbl =
1388 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1389 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1390 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1391 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001392 u8 start_action = tbl->action;
Guy Cohenfde0db32008-04-21 15:42:01 -07001393 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
Guy Cohen3110bef2008-09-09 10:54:54 +08001394 u8 tx_chains_num = priv->hw_params.tx_chains_num;
Guy Cohenfde0db32008-04-21 15:42:01 -07001395 int ret = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001396 u8 update_search_tbl_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001397
1398 for (; ;) {
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001399 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001400 switch (tbl->action) {
Guy Cohen3110bef2008-09-09 10:54:54 +08001401 case IWL_LEGACY_SWITCH_ANTENNA1:
1402 case IWL_LEGACY_SWITCH_ANTENNA2:
Tomas Winklere1623442009-01-27 14:27:56 -08001403 IWL_DEBUG_RATE(priv, "LQ: Legacy toggle Antenna\n");
Zhu Yib481de92007-09-25 17:54:57 -07001404
Guy Cohen3110bef2008-09-09 10:54:54 +08001405 if ((tbl->action == IWL_LEGACY_SWITCH_ANTENNA1 &&
1406 tx_chains_num <= 1) ||
1407 (tbl->action == IWL_LEGACY_SWITCH_ANTENNA2 &&
1408 tx_chains_num <= 2))
1409 break;
1410
Ben Cahill77626352007-11-29 11:09:44 +08001411 /* Don't change antenna if success has been great */
Zhu Yib481de92007-09-25 17:54:57 -07001412 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1413 break;
Ben Cahill77626352007-11-29 11:09:44 +08001414
Ben Cahill77626352007-11-29 11:09:44 +08001415 /* Set up search table to try other antenna */
Zhu Yib481de92007-09-25 17:54:57 -07001416 memcpy(search_tbl, tbl, sz);
1417
Guy Cohenaade00c2008-04-23 17:14:59 -07001418 if (rs_toggle_antenna(valid_tx_ant,
1419 &search_tbl->current_rate, search_tbl)) {
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001420 update_search_tbl_counter = 1;
Guy Cohen3110bef2008-09-09 10:54:54 +08001421 rs_set_expected_tpt_table(lq_sta, search_tbl);
Guy Cohenaade00c2008-04-23 17:14:59 -07001422 goto out;
1423 }
Guy Cohena5e8b502008-05-29 16:35:11 +08001424 break;
Zhu Yib481de92007-09-25 17:54:57 -07001425 case IWL_LEGACY_SWITCH_SISO:
Tomas Winklere1623442009-01-27 14:27:56 -08001426 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to SISO\n");
Ben Cahill77626352007-11-29 11:09:44 +08001427
1428 /* Set up search table to try SISO */
Zhu Yib481de92007-09-25 17:54:57 -07001429 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001430 search_tbl->is_SGI = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001431 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001432 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001433 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001434 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001435 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001436 }
Zhu Yib481de92007-09-25 17:54:57 -07001437
1438 break;
Guy Cohen3110bef2008-09-09 10:54:54 +08001439 case IWL_LEGACY_SWITCH_MIMO2_AB:
1440 case IWL_LEGACY_SWITCH_MIMO2_AC:
1441 case IWL_LEGACY_SWITCH_MIMO2_BC:
Tomas Winklere1623442009-01-27 14:27:56 -08001442 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to MIMO2\n");
Ben Cahill77626352007-11-29 11:09:44 +08001443
1444 /* Set up search table to try MIMO */
Zhu Yib481de92007-09-25 17:54:57 -07001445 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001446 search_tbl->is_SGI = 0;
Guy Cohen3110bef2008-09-09 10:54:54 +08001447
1448 if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AB)
1449 search_tbl->ant_type = ANT_AB;
1450 else if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AC)
1451 search_tbl->ant_type = ANT_AC;
1452 else
1453 search_tbl->ant_type = ANT_BC;
1454
1455 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1456 break;
1457
Guy Cohenfde0db32008-04-21 15:42:01 -07001458 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001459 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001460 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001461 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001462 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001463 }
Zhu Yib481de92007-09-25 17:54:57 -07001464 break;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001465
1466 case IWL_LEGACY_SWITCH_MIMO3_ABC:
1467 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to MIMO3\n");
1468
1469 /* Set up search table to try MIMO3 */
1470 memcpy(search_tbl, tbl, sz);
1471 search_tbl->is_SGI = 0;
1472
1473 search_tbl->ant_type = ANT_ABC;
1474
1475 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1476 break;
1477
1478 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1479 search_tbl, index);
1480 if (!ret) {
1481 lq_sta->action_counter = 0;
1482 goto out;
1483 }
1484 break;
Zhu Yib481de92007-09-25 17:54:57 -07001485 }
1486 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001487 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001488 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
Zhu Yib481de92007-09-25 17:54:57 -07001489
1490 if (tbl->action == start_action)
1491 break;
1492
1493 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001494 search_tbl->lq_type = LQ_NONE;
Zhu Yib481de92007-09-25 17:54:57 -07001495 return 0;
1496
Guy Cohen3110bef2008-09-09 10:54:54 +08001497out:
1498 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001499 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001500 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001501 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001502 if (update_search_tbl_counter)
1503 search_tbl->action = tbl->action;
Zhu Yib481de92007-09-25 17:54:57 -07001504 return 0;
1505
1506}
1507
Ben Cahill77626352007-11-29 11:09:44 +08001508/*
1509 * Try to switch to new modulation mode from SISO
1510 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001511static int rs_move_siso_to_other(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001512 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001513 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001514 struct ieee80211_sta *sta, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001515{
Tomas Winklerc33104f2008-01-14 17:46:21 -08001516 u8 is_green = lq_sta->is_green;
Tomas Winklere227cea2008-07-18 13:53:05 +08001517 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1518 struct iwl_scale_tbl_info *search_tbl =
1519 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1520 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1521 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1522 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001523 u8 start_action = tbl->action;
Guy Cohenfde0db32008-04-21 15:42:01 -07001524 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
Guy Cohen3110bef2008-09-09 10:54:54 +08001525 u8 tx_chains_num = priv->hw_params.tx_chains_num;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001526 u8 update_search_tbl_counter = 0;
Guy Cohenfde0db32008-04-21 15:42:01 -07001527 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001528
1529 for (;;) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001530 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001531 switch (tbl->action) {
Guy Cohen3110bef2008-09-09 10:54:54 +08001532 case IWL_SISO_SWITCH_ANTENNA1:
1533 case IWL_SISO_SWITCH_ANTENNA2:
Tomas Winklere1623442009-01-27 14:27:56 -08001534 IWL_DEBUG_RATE(priv, "LQ: SISO toggle Antenna\n");
Guy Cohen3110bef2008-09-09 10:54:54 +08001535
1536 if ((tbl->action == IWL_SISO_SWITCH_ANTENNA1 &&
1537 tx_chains_num <= 1) ||
1538 (tbl->action == IWL_SISO_SWITCH_ANTENNA2 &&
1539 tx_chains_num <= 2))
1540 break;
1541
Zhu Yib481de92007-09-25 17:54:57 -07001542 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1543 break;
Zhu Yib481de92007-09-25 17:54:57 -07001544
1545 memcpy(search_tbl, tbl, sz);
Guy Cohenaade00c2008-04-23 17:14:59 -07001546 if (rs_toggle_antenna(valid_tx_ant,
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001547 &search_tbl->current_rate, search_tbl)) {
1548 update_search_tbl_counter = 1;
Guy Cohenaade00c2008-04-23 17:14:59 -07001549 goto out;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001550 }
Guy Cohena5e8b502008-05-29 16:35:11 +08001551 break;
Guy Cohen3110bef2008-09-09 10:54:54 +08001552 case IWL_SISO_SWITCH_MIMO2_AB:
1553 case IWL_SISO_SWITCH_MIMO2_AC:
1554 case IWL_SISO_SWITCH_MIMO2_BC:
Tomas Winklere1623442009-01-27 14:27:56 -08001555 IWL_DEBUG_RATE(priv, "LQ: SISO switch to MIMO2\n");
Zhu Yib481de92007-09-25 17:54:57 -07001556 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001557 search_tbl->is_SGI = 0;
Guy Cohen3110bef2008-09-09 10:54:54 +08001558
1559 if (tbl->action == IWL_SISO_SWITCH_MIMO2_AB)
1560 search_tbl->ant_type = ANT_AB;
1561 else if (tbl->action == IWL_SISO_SWITCH_MIMO2_AC)
1562 search_tbl->ant_type = ANT_AC;
1563 else
1564 search_tbl->ant_type = ANT_BC;
1565
1566 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1567 break;
1568
Guy Cohenfde0db32008-04-21 15:42:01 -07001569 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001570 search_tbl, index);
Guy Cohen3110bef2008-09-09 10:54:54 +08001571 if (!ret)
Zhu Yib481de92007-09-25 17:54:57 -07001572 goto out;
1573 break;
1574 case IWL_SISO_SWITCH_GI:
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001575 if (!tbl->is_fat &&
1576 !(priv->current_ht_config.sgf &
1577 HT_SHORT_GI_20MHZ))
1578 break;
1579 if (tbl->is_fat &&
1580 !(priv->current_ht_config.sgf &
1581 HT_SHORT_GI_40MHZ))
1582 break;
1583
Tomas Winklere1623442009-01-27 14:27:56 -08001584 IWL_DEBUG_RATE(priv, "LQ: SISO toggle SGI/NGI\n");
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001585
Zhu Yib481de92007-09-25 17:54:57 -07001586 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001587 if (is_green) {
1588 if (!tbl->is_SGI)
1589 break;
1590 else
Winkler, Tomas15b16872008-12-19 10:37:33 +08001591 IWL_ERR(priv,
1592 "SGI was set in GF+SISO\n");
Zhu Yib481de92007-09-25 17:54:57 -07001593 }
Guy Cohen39e88502008-04-23 17:14:57 -07001594 search_tbl->is_SGI = !tbl->is_SGI;
Guy Coheneecd6e52008-04-23 17:14:58 -07001595 rs_set_expected_tpt_table(lq_sta, search_tbl);
Guy Cohen39e88502008-04-23 17:14:57 -07001596 if (tbl->is_SGI) {
1597 s32 tpt = lq_sta->last_tpt / 100;
1598 if (tpt >= search_tbl->expected_tpt[index])
1599 break;
1600 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001601 search_tbl->current_rate =
1602 rate_n_flags_from_tbl(priv, search_tbl,
1603 index, is_green);
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001604 update_search_tbl_counter = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001605 goto out;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001606 case IWL_SISO_SWITCH_MIMO3_ABC:
1607 IWL_DEBUG_RATE(priv, "LQ: SISO switch to MIMO3\n");
1608 memcpy(search_tbl, tbl, sz);
1609 search_tbl->is_SGI = 0;
1610 search_tbl->ant_type = ANT_ABC;
1611
1612 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1613 break;
1614
1615 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1616 search_tbl, index);
1617 if (!ret)
1618 goto out;
1619 break;
Zhu Yib481de92007-09-25 17:54:57 -07001620 }
1621 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001622 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001623 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
Zhu Yib481de92007-09-25 17:54:57 -07001624
1625 if (tbl->action == start_action)
1626 break;
1627 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001628 search_tbl->lq_type = LQ_NONE;
Zhu Yib481de92007-09-25 17:54:57 -07001629 return 0;
1630
1631 out:
Guy Cohen3110bef2008-09-09 10:54:54 +08001632 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001633 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001634 if (tbl->action > IWL_SISO_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001635 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001636 if (update_search_tbl_counter)
1637 search_tbl->action = tbl->action;
1638
Zhu Yib481de92007-09-25 17:54:57 -07001639 return 0;
1640}
1641
Ben Cahill77626352007-11-29 11:09:44 +08001642/*
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001643 * Try to switch to new modulation mode from MIMO2
Ben Cahill77626352007-11-29 11:09:44 +08001644 */
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001645static int rs_move_mimo2_to_other(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001646 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001647 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001648 struct ieee80211_sta *sta, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001649{
Tomas Winklerc33104f2008-01-14 17:46:21 -08001650 s8 is_green = lq_sta->is_green;
Tomas Winklere227cea2008-07-18 13:53:05 +08001651 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1652 struct iwl_scale_tbl_info *search_tbl =
1653 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Guy Cohen3110bef2008-09-09 10:54:54 +08001654 struct iwl_rate_scale_data *window = &(tbl->win[index]);
Tomas Winklere227cea2008-07-18 13:53:05 +08001655 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1656 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001657 u8 start_action = tbl->action;
Guy Cohen3110bef2008-09-09 10:54:54 +08001658 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1659 u8 tx_chains_num = priv->hw_params.tx_chains_num;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001660 u8 update_search_tbl_counter = 0;
Guy Cohenaade00c2008-04-23 17:14:59 -07001661 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001662
1663 for (;;) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001664 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001665 switch (tbl->action) {
Guy Cohen3110bef2008-09-09 10:54:54 +08001666 case IWL_MIMO2_SWITCH_ANTENNA1:
1667 case IWL_MIMO2_SWITCH_ANTENNA2:
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001668 IWL_DEBUG_RATE(priv, "LQ: MIMO2 toggle Antennas\n");
Guy Cohen3110bef2008-09-09 10:54:54 +08001669
1670 if (tx_chains_num <= 2)
1671 break;
1672
1673 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1674 break;
1675
1676 memcpy(search_tbl, tbl, sz);
1677 if (rs_toggle_antenna(valid_tx_ant,
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001678 &search_tbl->current_rate, search_tbl)) {
1679 update_search_tbl_counter = 1;
Guy Cohen3110bef2008-09-09 10:54:54 +08001680 goto out;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001681 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001682 break;
1683 case IWL_MIMO2_SWITCH_SISO_A:
1684 case IWL_MIMO2_SWITCH_SISO_B:
1685 case IWL_MIMO2_SWITCH_SISO_C:
Tomas Winklere1623442009-01-27 14:27:56 -08001686 IWL_DEBUG_RATE(priv, "LQ: MIMO2 switch to SISO\n");
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001687
Ben Cahill77626352007-11-29 11:09:44 +08001688 /* Set up new search table for SISO */
Zhu Yib481de92007-09-25 17:54:57 -07001689 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001690
Guy Cohen3110bef2008-09-09 10:54:54 +08001691 if (tbl->action == IWL_MIMO2_SWITCH_SISO_A)
Guy Cohenfde0db32008-04-21 15:42:01 -07001692 search_tbl->ant_type = ANT_A;
Guy Cohen3110bef2008-09-09 10:54:54 +08001693 else if (tbl->action == IWL_MIMO2_SWITCH_SISO_B)
Guy Cohenfde0db32008-04-21 15:42:01 -07001694 search_tbl->ant_type = ANT_B;
Guy Cohen3110bef2008-09-09 10:54:54 +08001695 else
1696 search_tbl->ant_type = ANT_C;
1697
1698 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1699 break;
Zhu Yib481de92007-09-25 17:54:57 -07001700
Tomas Winklerc33104f2008-01-14 17:46:21 -08001701 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001702 search_tbl, index);
Guy Cohen3110bef2008-09-09 10:54:54 +08001703 if (!ret)
Zhu Yib481de92007-09-25 17:54:57 -07001704 goto out;
Guy Cohen3110bef2008-09-09 10:54:54 +08001705
Zhu Yib481de92007-09-25 17:54:57 -07001706 break;
1707
Guy Cohen3110bef2008-09-09 10:54:54 +08001708 case IWL_MIMO2_SWITCH_GI:
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001709 if (!tbl->is_fat &&
1710 !(priv->current_ht_config.sgf &
1711 HT_SHORT_GI_20MHZ))
1712 break;
1713 if (tbl->is_fat &&
1714 !(priv->current_ht_config.sgf &
1715 HT_SHORT_GI_40MHZ))
1716 break;
1717
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001718 IWL_DEBUG_RATE(priv, "LQ: MIMO2 toggle SGI/NGI\n");
1719
1720 /* Set up new search table for MIMO2 */
1721 memcpy(search_tbl, tbl, sz);
1722 search_tbl->is_SGI = !tbl->is_SGI;
1723 rs_set_expected_tpt_table(lq_sta, search_tbl);
1724 /*
1725 * If active table already uses the fastest possible
1726 * modulation (dual stream with short guard interval),
1727 * and it's working well, there's no need to look
1728 * for a better type of modulation!
1729 */
1730 if (tbl->is_SGI) {
1731 s32 tpt = lq_sta->last_tpt / 100;
1732 if (tpt >= search_tbl->expected_tpt[index])
1733 break;
1734 }
1735 search_tbl->current_rate =
1736 rate_n_flags_from_tbl(priv, search_tbl,
1737 index, is_green);
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001738 update_search_tbl_counter = 1;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001739 goto out;
1740
1741 case IWL_MIMO2_SWITCH_MIMO3_ABC:
1742 IWL_DEBUG_RATE(priv, "LQ: MIMO2 switch to MIMO3\n");
1743 memcpy(search_tbl, tbl, sz);
1744 search_tbl->is_SGI = 0;
1745 search_tbl->ant_type = ANT_ABC;
1746
1747 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1748 break;
1749
1750 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1751 search_tbl, index);
1752 if (!ret)
1753 goto out;
1754
1755 break;
1756 }
1757 tbl->action++;
1758 if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
1759 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
1760
1761 if (tbl->action == start_action)
1762 break;
1763 }
1764 search_tbl->lq_type = LQ_NONE;
1765 return 0;
1766 out:
1767 lq_sta->search_better_tbl = 1;
1768 tbl->action++;
1769 if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
1770 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001771 if (update_search_tbl_counter)
1772 search_tbl->action = tbl->action;
1773
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001774 return 0;
1775
1776}
1777
1778/*
1779 * Try to switch to new modulation mode from MIMO3
1780 */
1781static int rs_move_mimo3_to_other(struct iwl_priv *priv,
1782 struct iwl_lq_sta *lq_sta,
1783 struct ieee80211_conf *conf,
1784 struct ieee80211_sta *sta, int index)
1785{
1786 s8 is_green = lq_sta->is_green;
1787 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1788 struct iwl_scale_tbl_info *search_tbl =
1789 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1790 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1791 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1792 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1793 u8 start_action = tbl->action;
1794 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1795 u8 tx_chains_num = priv->hw_params.tx_chains_num;
1796 int ret;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001797 u8 update_search_tbl_counter = 0;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001798
1799 for (;;) {
1800 lq_sta->action_counter++;
1801 switch (tbl->action) {
1802 case IWL_MIMO3_SWITCH_ANTENNA1:
1803 case IWL_MIMO3_SWITCH_ANTENNA2:
1804 IWL_DEBUG_RATE(priv, "LQ: MIMO3 toggle Antennas\n");
1805
1806 if (tx_chains_num <= 3)
1807 break;
1808
1809 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1810 break;
1811
1812 memcpy(search_tbl, tbl, sz);
1813 if (rs_toggle_antenna(valid_tx_ant,
1814 &search_tbl->current_rate, search_tbl))
1815 goto out;
1816 break;
1817 case IWL_MIMO3_SWITCH_SISO_A:
1818 case IWL_MIMO3_SWITCH_SISO_B:
1819 case IWL_MIMO3_SWITCH_SISO_C:
1820 IWL_DEBUG_RATE(priv, "LQ: MIMO3 switch to SISO\n");
1821
1822 /* Set up new search table for SISO */
1823 memcpy(search_tbl, tbl, sz);
1824
1825 if (tbl->action == IWL_MIMO3_SWITCH_SISO_A)
1826 search_tbl->ant_type = ANT_A;
1827 else if (tbl->action == IWL_MIMO3_SWITCH_SISO_B)
1828 search_tbl->ant_type = ANT_B;
1829 else
1830 search_tbl->ant_type = ANT_C;
1831
1832 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1833 break;
1834
1835 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
1836 search_tbl, index);
1837 if (!ret)
1838 goto out;
1839
1840 break;
1841
1842 case IWL_MIMO3_SWITCH_MIMO2_AB:
1843 case IWL_MIMO3_SWITCH_MIMO2_AC:
1844 case IWL_MIMO3_SWITCH_MIMO2_BC:
1845 IWL_DEBUG_RATE(priv, "LQ: MIMO3 switch to MIMO2\n");
1846
1847 memcpy(search_tbl, tbl, sz);
1848 search_tbl->is_SGI = 0;
1849 if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AB)
1850 search_tbl->ant_type = ANT_AB;
1851 else if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AC)
1852 search_tbl->ant_type = ANT_AC;
1853 else
1854 search_tbl->ant_type = ANT_BC;
1855
1856 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1857 break;
1858
1859 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
1860 search_tbl, index);
1861 if (!ret)
1862 goto out;
1863
1864 break;
1865
1866 case IWL_MIMO3_SWITCH_GI:
1867 if (!tbl->is_fat &&
1868 !(priv->current_ht_config.sgf &
1869 HT_SHORT_GI_20MHZ))
1870 break;
1871 if (tbl->is_fat &&
1872 !(priv->current_ht_config.sgf &
1873 HT_SHORT_GI_40MHZ))
1874 break;
1875
1876 IWL_DEBUG_RATE(priv, "LQ: MIMO3 toggle SGI/NGI\n");
Ben Cahill77626352007-11-29 11:09:44 +08001877
1878 /* Set up new search table for MIMO */
Zhu Yib481de92007-09-25 17:54:57 -07001879 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001880 search_tbl->is_SGI = !tbl->is_SGI;
Guy Coheneecd6e52008-04-23 17:14:58 -07001881 rs_set_expected_tpt_table(lq_sta, search_tbl);
Ben Cahill77626352007-11-29 11:09:44 +08001882 /*
1883 * If active table already uses the fastest possible
1884 * modulation (dual stream with short guard interval),
1885 * and it's working well, there's no need to look
1886 * for a better type of modulation!
1887 */
Guy Cohen39e88502008-04-23 17:14:57 -07001888 if (tbl->is_SGI) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001889 s32 tpt = lq_sta->last_tpt / 100;
Guy Cohen39e88502008-04-23 17:14:57 -07001890 if (tpt >= search_tbl->expected_tpt[index])
1891 break;
Zhu Yib481de92007-09-25 17:54:57 -07001892 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001893 search_tbl->current_rate =
1894 rate_n_flags_from_tbl(priv, search_tbl,
1895 index, is_green);
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001896 update_search_tbl_counter = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001897 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07001898 }
1899 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001900 if (tbl->action > IWL_MIMO3_SWITCH_GI)
1901 tbl->action = IWL_MIMO3_SWITCH_ANTENNA1;
Zhu Yib481de92007-09-25 17:54:57 -07001902
1903 if (tbl->action == start_action)
1904 break;
1905 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001906 search_tbl->lq_type = LQ_NONE;
Zhu Yib481de92007-09-25 17:54:57 -07001907 return 0;
1908 out:
Guy Cohen3110bef2008-09-09 10:54:54 +08001909 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001910 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001911 if (tbl->action > IWL_MIMO3_SWITCH_GI)
1912 tbl->action = IWL_MIMO3_SWITCH_ANTENNA1;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001913 if (update_search_tbl_counter)
1914 search_tbl->action = tbl->action;
1915
Zhu Yib481de92007-09-25 17:54:57 -07001916 return 0;
1917
1918}
1919
Ben Cahill77626352007-11-29 11:09:44 +08001920/*
1921 * Check whether we should continue using same modulation mode, or
1922 * begin search for a new mode, based on:
1923 * 1) # tx successes or failures while using this mode
1924 * 2) # times calling this function
1925 * 3) elapsed time in this mode (not used, for now)
1926 */
Tomas Winklere227cea2008-07-18 13:53:05 +08001927static void rs_stay_in_table(struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07001928{
Tomas Winklere227cea2008-07-18 13:53:05 +08001929 struct iwl_scale_tbl_info *tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001930 int i;
1931 int active_tbl;
1932 int flush_interval_passed = 0;
Ester Kummerbf403db2008-05-05 10:22:40 +08001933 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07001934
Ester Kummerbf403db2008-05-05 10:22:40 +08001935 priv = lq_sta->drv;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001936 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001937
Tomas Winklerc33104f2008-01-14 17:46:21 -08001938 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07001939
Ben Cahill77626352007-11-29 11:09:44 +08001940 /* If we've been disallowing search, see if we should now allow it */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001941 if (lq_sta->stay_in_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07001942
Ben Cahill77626352007-11-29 11:09:44 +08001943 /* Elapsed time using current modulation mode */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001944 if (lq_sta->flush_timer)
Zhu Yib481de92007-09-25 17:54:57 -07001945 flush_interval_passed =
Mohamed Abbas447fee72009-04-20 14:37:02 -07001946 time_after(jiffies,
1947 (unsigned long)(lq_sta->flush_timer +
Zhu Yib481de92007-09-25 17:54:57 -07001948 IWL_RATE_SCALE_FLUSH_INTVL));
1949
Ben Cahill77626352007-11-29 11:09:44 +08001950 /*
1951 * Check if we should allow search for new modulation mode.
1952 * If many frames have failed or succeeded, or we've used
1953 * this same modulation for a long time, allow search, and
1954 * reset history stats that keep track of whether we should
1955 * allow a new search. Also (below) reset all bitmaps and
1956 * stats in active history.
1957 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001958 if ((lq_sta->total_failed > lq_sta->max_failure_limit) ||
1959 (lq_sta->total_success > lq_sta->max_success_limit) ||
1960 ((!lq_sta->search_better_tbl) && (lq_sta->flush_timer)
Zhu Yib481de92007-09-25 17:54:57 -07001961 && (flush_interval_passed))) {
Tomas Winklere1623442009-01-27 14:27:56 -08001962 IWL_DEBUG_RATE(priv, "LQ: stay is expired %d %d %d\n:",
Tomas Winklerc33104f2008-01-14 17:46:21 -08001963 lq_sta->total_failed,
1964 lq_sta->total_success,
Zhu Yib481de92007-09-25 17:54:57 -07001965 flush_interval_passed);
Ben Cahill77626352007-11-29 11:09:44 +08001966
1967 /* Allow search for new mode */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001968 lq_sta->stay_in_tbl = 0; /* only place reset */
1969 lq_sta->total_failed = 0;
1970 lq_sta->total_success = 0;
1971 lq_sta->flush_timer = 0;
Ben Cahill77626352007-11-29 11:09:44 +08001972
1973 /*
1974 * Else if we've used this modulation mode enough repetitions
1975 * (regardless of elapsed time or success/failure), reset
1976 * history bitmaps and rate-specific stats for all rates in
1977 * active table.
1978 */
Mohamed Abbas403ab562007-11-06 22:06:25 -08001979 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001980 lq_sta->table_count++;
1981 if (lq_sta->table_count >=
1982 lq_sta->table_count_limit) {
1983 lq_sta->table_count = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001984
Tomas Winklere1623442009-01-27 14:27:56 -08001985 IWL_DEBUG_RATE(priv, "LQ: stay in table clear win\n");
Zhu Yib481de92007-09-25 17:54:57 -07001986 for (i = 0; i < IWL_RATE_COUNT; i++)
1987 rs_rate_scale_clear_window(
1988 &(tbl->win[i]));
1989 }
1990 }
1991
Ben Cahill77626352007-11-29 11:09:44 +08001992 /* If transitioning to allow "search", reset all history
1993 * bitmaps and stats in active table (this will become the new
1994 * "search" table). */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001995 if (!lq_sta->stay_in_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07001996 for (i = 0; i < IWL_RATE_COUNT; i++)
1997 rs_rate_scale_clear_window(&(tbl->win[i]));
1998 }
1999 }
2000}
2001
Ben Cahill77626352007-11-29 11:09:44 +08002002/*
2003 * Do rate scaling and search for new modulation mode.
2004 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002005static void rs_rate_scale_perform(struct iwl_priv *priv,
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002006 struct sk_buff *skb,
Johannes Berg4b7679a2008-09-18 18:14:18 +02002007 struct ieee80211_sta *sta,
2008 struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002009{
Johannes Berg4b7679a2008-09-18 18:14:18 +02002010 struct ieee80211_hw *hw = priv->hw;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002011 struct ieee80211_conf *conf = &hw->conf;
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002012 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2013 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002014 int low = IWL_RATE_INVALID;
2015 int high = IWL_RATE_INVALID;
2016 int index;
2017 int i;
Tomas Winklere227cea2008-07-18 13:53:05 +08002018 struct iwl_rate_scale_data *window = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002019 int current_tpt = IWL_INVALID_VALUE;
2020 int low_tpt = IWL_INVALID_VALUE;
2021 int high_tpt = IWL_INVALID_VALUE;
2022 u32 fail_count;
2023 s8 scale_action = 0;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002024 u16 rate_mask;
Zhu Yib481de92007-09-25 17:54:57 -07002025 u8 update_lq = 0;
Tomas Winklere227cea2008-07-18 13:53:05 +08002026 struct iwl_scale_tbl_info *tbl, *tbl1;
Zhu Yib481de92007-09-25 17:54:57 -07002027 u16 rate_scale_index_msk = 0;
Guy Cohen39e88502008-04-23 17:14:57 -07002028 u32 rate;
Zhu Yib481de92007-09-25 17:54:57 -07002029 u8 is_green = 0;
2030 u8 active_tbl = 0;
2031 u8 done_search = 0;
2032 u16 high_low;
Guy Cohena5e8b502008-05-29 16:35:11 +08002033 s32 sr;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002034 u8 tid = MAX_TID_COUNT;
Wey-Yi Guy86b47662009-03-10 14:35:09 -07002035 struct iwl_tid_data *tid_data;
Zhu Yib481de92007-09-25 17:54:57 -07002036
Tomas Winklere1623442009-01-27 14:27:56 -08002037 IWL_DEBUG_RATE(priv, "rate scale calculate new rate for skb\n");
Zhu Yib481de92007-09-25 17:54:57 -07002038
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002039 /* Send management frames and NO_ACK data using lowest rate. */
Tomas Winkler417f1142008-11-12 13:14:06 -08002040 /* TODO: this could probably be improved.. */
2041 if (!ieee80211_is_data(hdr->frame_control) ||
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002042 info->flags & IEEE80211_TX_CTL_NO_ACK)
Zhu Yib481de92007-09-25 17:54:57 -07002043 return;
Zhu Yib481de92007-09-25 17:54:57 -07002044
Johannes Berg4b7679a2008-09-18 18:14:18 +02002045 if (!sta || !lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002046 return;
2047
Johannes Berg4b7679a2008-09-18 18:14:18 +02002048 lq_sta->supp_rates = sta->supp_rates[lq_sta->band];
Zhu Yib481de92007-09-25 17:54:57 -07002049
Ron Rindjunskyfaa29712008-06-12 09:46:58 +08002050 tid = rs_tl_add_packet(lq_sta, hdr);
2051
Ben Cahill77626352007-11-29 11:09:44 +08002052 /*
2053 * Select rate-scale / modulation-mode table to work with in
2054 * the rest of this function: "search" if searching for better
2055 * modulation mode, or "active" if doing rate scaling within a mode.
2056 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002057 if (!lq_sta->search_better_tbl)
2058 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002059 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002060 active_tbl = 1 - lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002061
Tomas Winklerc33104f2008-01-14 17:46:21 -08002062 tbl = &(lq_sta->lq_info[active_tbl]);
2063 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07002064
Ben Cahill77626352007-11-29 11:09:44 +08002065 /* current tx rate */
Johannes Bergb7e35002008-09-11 02:22:58 +02002066 index = lq_sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002067
Tomas Winklere1623442009-01-27 14:27:56 -08002068 IWL_DEBUG_RATE(priv, "Rate scale index %d for type %d\n", index,
Zhu Yib481de92007-09-25 17:54:57 -07002069 tbl->lq_type);
2070
Ben Cahill77626352007-11-29 11:09:44 +08002071 /* rates available for this association, and for modulation mode */
Guy Coheneecd6e52008-04-23 17:14:58 -07002072 rate_mask = rs_get_supported_rates(lq_sta, hdr, tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -07002073
Tomas Winklere1623442009-01-27 14:27:56 -08002074 IWL_DEBUG_RATE(priv, "mask 0x%04X \n", rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07002075
2076 /* mask with station rate restriction */
2077 if (is_legacy(tbl->lq_type)) {
Johannes Berg8318d782008-01-24 19:38:38 +01002078 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Ben Cahill77626352007-11-29 11:09:44 +08002079 /* supp_rates has no CCK bits in A mode */
Zhu Yib481de92007-09-25 17:54:57 -07002080 rate_scale_index_msk = (u16) (rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -08002081 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
Zhu Yib481de92007-09-25 17:54:57 -07002082 else
2083 rate_scale_index_msk = (u16) (rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -08002084 lq_sta->supp_rates);
Zhu Yib481de92007-09-25 17:54:57 -07002085
2086 } else
2087 rate_scale_index_msk = rate_mask;
2088
2089 if (!rate_scale_index_msk)
2090 rate_scale_index_msk = rate_mask;
2091
Guy Cohen07bc28e2008-04-23 17:15:03 -07002092 if (!((1 << index) & rate_scale_index_msk)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002093 IWL_ERR(priv, "Current Rate is not valid\n");
Guy Cohen07bc28e2008-04-23 17:15:03 -07002094 return;
Zhu Yib481de92007-09-25 17:54:57 -07002095 }
2096
Ben Cahill77626352007-11-29 11:09:44 +08002097 /* Get expected throughput table and history window for current rate */
Guy Cohen07bc28e2008-04-23 17:15:03 -07002098 if (!tbl->expected_tpt) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002099 IWL_ERR(priv, "tbl->expected_tpt is NULL\n");
Guy Cohen07bc28e2008-04-23 17:15:03 -07002100 return;
2101 }
Zhu Yib481de92007-09-25 17:54:57 -07002102
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002103 /* force user max rate if set by user */
2104 if ((lq_sta->max_rate_idx != -1) &&
2105 (lq_sta->max_rate_idx < index)) {
2106 index = lq_sta->max_rate_idx;
2107 update_lq = 1;
2108 window = &(tbl->win[index]);
2109 goto lq_update;
2110 }
2111
Zhu Yib481de92007-09-25 17:54:57 -07002112 window = &(tbl->win[index]);
2113
Ben Cahill77626352007-11-29 11:09:44 +08002114 /*
2115 * If there is not enough history to calculate actual average
2116 * throughput, keep analyzing results of more tx frames, without
2117 * changing rate or mode (bypass most of the rest of this function).
2118 * Set up new rate table in uCode only if old rate is not supported
2119 * in current association (use new rate found above).
2120 */
Zhu Yib481de92007-09-25 17:54:57 -07002121 fail_count = window->counter - window->success_counter;
Guy Cohen07bc28e2008-04-23 17:15:03 -07002122 if ((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
2123 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002124 IWL_DEBUG_RATE(priv, "LQ: still below TH. succ=%d total=%d "
Zhu Yib481de92007-09-25 17:54:57 -07002125 "for index %d\n",
2126 window->success_counter, window->counter, index);
Ben Cahill77626352007-11-29 11:09:44 +08002127
2128 /* Can't calculate this yet; not enough history */
Zhu Yib481de92007-09-25 17:54:57 -07002129 window->average_tpt = IWL_INVALID_VALUE;
Ben Cahill77626352007-11-29 11:09:44 +08002130
2131 /* Should we stay with this modulation mode,
2132 * or search for a new one? */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002133 rs_stay_in_table(lq_sta);
Ben Cahill77626352007-11-29 11:09:44 +08002134
Zhu Yib481de92007-09-25 17:54:57 -07002135 goto out;
Guy Cohen3110bef2008-09-09 10:54:54 +08002136 }
Zhu Yib481de92007-09-25 17:54:57 -07002137
Ben Cahill77626352007-11-29 11:09:44 +08002138 /* Else we have enough samples; calculate estimate of
2139 * actual average throughput */
Guy Cohen3110bef2008-09-09 10:54:54 +08002140
2141 BUG_ON(window->average_tpt != ((window->success_ratio *
2142 tbl->expected_tpt[index] + 64) / 128));
Zhu Yib481de92007-09-25 17:54:57 -07002143
Ben Cahill77626352007-11-29 11:09:44 +08002144 /* If we are searching for better modulation mode, check success. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002145 if (lq_sta->search_better_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07002146
Ben Cahill77626352007-11-29 11:09:44 +08002147 /* If good success, continue using the "search" mode;
2148 * no need to send new link quality command, since we're
2149 * continuing to use the setup that we've been trying. */
Guy Cohen07bc28e2008-04-23 17:15:03 -07002150 if (window->average_tpt > lq_sta->last_tpt) {
2151
Tomas Winklere1623442009-01-27 14:27:56 -08002152 IWL_DEBUG_RATE(priv, "LQ: SWITCHING TO NEW TABLE "
Guy Cohen07bc28e2008-04-23 17:15:03 -07002153 "suc=%d cur-tpt=%d old-tpt=%d\n",
2154 window->success_ratio,
2155 window->average_tpt,
2156 lq_sta->last_tpt);
2157
2158 if (!is_legacy(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08002159 lq_sta->enable_counter = 1;
Guy Cohen07bc28e2008-04-23 17:15:03 -07002160
Ben Cahill77626352007-11-29 11:09:44 +08002161 /* Swap tables; "search" becomes "active" */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002162 lq_sta->active_tbl = active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002163 current_tpt = window->average_tpt;
Ben Cahill77626352007-11-29 11:09:44 +08002164
2165 /* Else poor success; go back to mode in "active" table */
Zhu Yib481de92007-09-25 17:54:57 -07002166 } else {
Guy Cohen07bc28e2008-04-23 17:15:03 -07002167
Tomas Winklere1623442009-01-27 14:27:56 -08002168 IWL_DEBUG_RATE(priv, "LQ: GOING BACK TO THE OLD TABLE "
Guy Cohen07bc28e2008-04-23 17:15:03 -07002169 "suc=%d cur-tpt=%d old-tpt=%d\n",
2170 window->success_ratio,
2171 window->average_tpt,
2172 lq_sta->last_tpt);
2173
Ben Cahill77626352007-11-29 11:09:44 +08002174 /* Nullify "search" table */
Zhu Yib481de92007-09-25 17:54:57 -07002175 tbl->lq_type = LQ_NONE;
Ben Cahill77626352007-11-29 11:09:44 +08002176
2177 /* Revert to "active" table */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002178 active_tbl = lq_sta->active_tbl;
2179 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07002180
Ben Cahill77626352007-11-29 11:09:44 +08002181 /* Revert to "active" rate and throughput info */
Tomas Winklere7d326a2008-06-12 09:47:11 +08002182 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
Tomas Winklerc33104f2008-01-14 17:46:21 -08002183 current_tpt = lq_sta->last_tpt;
Ben Cahill77626352007-11-29 11:09:44 +08002184
2185 /* Need to set up a new rate table in uCode */
2186 update_lq = 1;
Zhu Yib481de92007-09-25 17:54:57 -07002187 }
Ben Cahill77626352007-11-29 11:09:44 +08002188
2189 /* Either way, we've made a decision; modulation mode
2190 * search is done, allow rate adjustment next time. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002191 lq_sta->search_better_tbl = 0;
Ben Cahill77626352007-11-29 11:09:44 +08002192 done_search = 1; /* Don't switch modes below! */
Zhu Yib481de92007-09-25 17:54:57 -07002193 goto lq_update;
2194 }
2195
Ben Cahill77626352007-11-29 11:09:44 +08002196 /* (Else) not in search of better modulation mode, try for better
2197 * starting rate, while staying in this mode. */
Ester Kummerbf403db2008-05-05 10:22:40 +08002198 high_low = rs_get_adjacent_rate(priv, index, rate_scale_index_msk,
Zhu Yib481de92007-09-25 17:54:57 -07002199 tbl->lq_type);
2200 low = high_low & 0xff;
2201 high = (high_low >> 8) & 0xff;
2202
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002203 /* If user set max rate, dont allow higher than user constrain */
2204 if ((lq_sta->max_rate_idx != -1) &&
2205 (lq_sta->max_rate_idx < high))
2206 high = IWL_RATE_INVALID;
2207
Guy Cohena5e8b502008-05-29 16:35:11 +08002208 sr = window->success_ratio;
2209
Ben Cahill77626352007-11-29 11:09:44 +08002210 /* Collect measured throughputs for current and adjacent rates */
Zhu Yib481de92007-09-25 17:54:57 -07002211 current_tpt = window->average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07002212 if (low != IWL_RATE_INVALID)
2213 low_tpt = tbl->win[low].average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07002214 if (high != IWL_RATE_INVALID)
2215 high_tpt = tbl->win[high].average_tpt;
2216
Guy Cohena5e8b502008-05-29 16:35:11 +08002217 scale_action = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002218
Ben Cahill77626352007-11-29 11:09:44 +08002219 /* Too many failures, decrease rate */
Guy Cohena5e8b502008-05-29 16:35:11 +08002220 if ((sr <= IWL_RATE_DECREASE_TH) || (current_tpt == 0)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002221 IWL_DEBUG_RATE(priv, "decrease rate because of low success_ratio\n");
Zhu Yib481de92007-09-25 17:54:57 -07002222 scale_action = -1;
Ben Cahill77626352007-11-29 11:09:44 +08002223
2224 /* No throughput measured yet for adjacent rates; try increase. */
Zhu Yib481de92007-09-25 17:54:57 -07002225 } else if ((low_tpt == IWL_INVALID_VALUE) &&
Guy Cohena5e8b502008-05-29 16:35:11 +08002226 (high_tpt == IWL_INVALID_VALUE)) {
2227
2228 if (high != IWL_RATE_INVALID && sr >= IWL_RATE_INCREASE_TH)
2229 scale_action = 1;
2230 else if (low != IWL_RATE_INVALID)
Wey-Yi Guy8fe72312009-03-10 14:35:10 -07002231 scale_action = 0;
Guy Cohena5e8b502008-05-29 16:35:11 +08002232 }
Ben Cahill77626352007-11-29 11:09:44 +08002233
2234 /* Both adjacent throughputs are measured, but neither one has better
2235 * throughput; we're using the best rate, don't change it! */
Zhu Yib481de92007-09-25 17:54:57 -07002236 else if ((low_tpt != IWL_INVALID_VALUE) &&
2237 (high_tpt != IWL_INVALID_VALUE) &&
2238 (low_tpt < current_tpt) &&
2239 (high_tpt < current_tpt))
2240 scale_action = 0;
Ben Cahill77626352007-11-29 11:09:44 +08002241
2242 /* At least one adjacent rate's throughput is measured,
2243 * and may have better performance. */
Zhu Yib481de92007-09-25 17:54:57 -07002244 else {
Ben Cahill77626352007-11-29 11:09:44 +08002245 /* Higher adjacent rate's throughput is measured */
Zhu Yib481de92007-09-25 17:54:57 -07002246 if (high_tpt != IWL_INVALID_VALUE) {
Ben Cahill77626352007-11-29 11:09:44 +08002247 /* Higher rate has better throughput */
Guy Cohena5e8b502008-05-29 16:35:11 +08002248 if (high_tpt > current_tpt &&
2249 sr >= IWL_RATE_INCREASE_TH) {
Zhu Yib481de92007-09-25 17:54:57 -07002250 scale_action = 1;
Guy Cohena5e8b502008-05-29 16:35:11 +08002251 } else {
Wey-Yi Guy8fe72312009-03-10 14:35:10 -07002252 scale_action = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002253 }
Ben Cahill77626352007-11-29 11:09:44 +08002254
2255 /* Lower adjacent rate's throughput is measured */
Zhu Yib481de92007-09-25 17:54:57 -07002256 } else if (low_tpt != IWL_INVALID_VALUE) {
Ben Cahill77626352007-11-29 11:09:44 +08002257 /* Lower rate has better throughput */
Zhu Yib481de92007-09-25 17:54:57 -07002258 if (low_tpt > current_tpt) {
Tomas Winklere1623442009-01-27 14:27:56 -08002259 IWL_DEBUG_RATE(priv,
2260 "decrease rate because of low tpt\n");
Zhu Yib481de92007-09-25 17:54:57 -07002261 scale_action = -1;
Guy Cohena5e8b502008-05-29 16:35:11 +08002262 } else if (sr >= IWL_RATE_INCREASE_TH) {
Zhu Yib481de92007-09-25 17:54:57 -07002263 scale_action = 1;
Guy Cohena5e8b502008-05-29 16:35:11 +08002264 }
Zhu Yib481de92007-09-25 17:54:57 -07002265 }
2266 }
2267
Ben Cahill77626352007-11-29 11:09:44 +08002268 /* Sanity check; asked for decrease, but success rate or throughput
2269 * has been good at old rate. Don't change it. */
Guy Cohena5e8b502008-05-29 16:35:11 +08002270 if ((scale_action == -1) && (low != IWL_RATE_INVALID) &&
2271 ((sr > IWL_RATE_HIGH_TH) ||
Zhu Yib481de92007-09-25 17:54:57 -07002272 (current_tpt > (100 * tbl->expected_tpt[low]))))
Zhu Yib481de92007-09-25 17:54:57 -07002273 scale_action = 0;
2274
2275 switch (scale_action) {
2276 case -1:
Ben Cahill77626352007-11-29 11:09:44 +08002277 /* Decrease starting rate, update uCode's rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002278 if (low != IWL_RATE_INVALID) {
2279 update_lq = 1;
2280 index = low;
2281 }
Mohamed Abbas447fee72009-04-20 14:37:02 -07002282
Zhu Yib481de92007-09-25 17:54:57 -07002283 break;
2284 case 1:
Ben Cahill77626352007-11-29 11:09:44 +08002285 /* Increase starting rate, update uCode's rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002286 if (high != IWL_RATE_INVALID) {
2287 update_lq = 1;
2288 index = high;
2289 }
2290
2291 break;
2292 case 0:
Ben Cahill77626352007-11-29 11:09:44 +08002293 /* No change */
Zhu Yib481de92007-09-25 17:54:57 -07002294 default:
2295 break;
2296 }
2297
Tomas Winklere1623442009-01-27 14:27:56 -08002298 IWL_DEBUG_RATE(priv, "choose rate scale index %d action %d low %d "
Zhu Yib481de92007-09-25 17:54:57 -07002299 "high %d type %d\n",
2300 index, scale_action, low, high, tbl->lq_type);
2301
Guy Cohena5e8b502008-05-29 16:35:11 +08002302lq_update:
Ben Cahill77626352007-11-29 11:09:44 +08002303 /* Replace uCode's rate table for the destination station. */
Zhu Yib481de92007-09-25 17:54:57 -07002304 if (update_lq) {
Tomas Winkler978785a2008-12-19 10:37:31 +08002305 rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
Guy Cohen07bc28e2008-04-23 17:15:03 -07002306 rs_fill_link_cmd(priv, lq_sta, rate);
Tomas Winkler66c73db2008-04-15 16:01:40 -07002307 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07002308 }
Ben Cahill77626352007-11-29 11:09:44 +08002309
2310 /* Should we stay with this modulation mode, or search for a new one? */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002311 rs_stay_in_table(lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002312
Ben Cahill77626352007-11-29 11:09:44 +08002313 /*
2314 * Search for new modulation mode if we're:
2315 * 1) Not changing rates right now
2316 * 2) Not just finishing up a search
2317 * 3) Allowing a new search
2318 */
Guy Cohen47cfd462008-05-27 11:29:34 +08002319 if (!update_lq && !done_search && !lq_sta->stay_in_tbl && window->counter) {
Ben Cahill77626352007-11-29 11:09:44 +08002320 /* Save current throughput to compare with "search" throughput*/
Tomas Winklerc33104f2008-01-14 17:46:21 -08002321 lq_sta->last_tpt = current_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07002322
Ben Cahill77626352007-11-29 11:09:44 +08002323 /* Select a new "search" modulation mode to try.
2324 * If one is found, set up the new "search" table. */
Zhu Yib481de92007-09-25 17:54:57 -07002325 if (is_legacy(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08002326 rs_move_legacy_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07002327 else if (is_siso(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08002328 rs_move_siso_to_other(priv, lq_sta, conf, sta, index);
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07002329 else if (is_mimo2(tbl->lq_type))
2330 rs_move_mimo2_to_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07002331 else
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07002332 rs_move_mimo3_to_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07002333
Ben Cahill77626352007-11-29 11:09:44 +08002334 /* If new "search" mode was selected, set up in uCode table */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002335 if (lq_sta->search_better_tbl) {
Ben Cahill77626352007-11-29 11:09:44 +08002336 /* Access the "search" table, clear its history. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002337 tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Zhu Yib481de92007-09-25 17:54:57 -07002338 for (i = 0; i < IWL_RATE_COUNT; i++)
2339 rs_rate_scale_clear_window(&(tbl->win[i]));
2340
Ben Cahill77626352007-11-29 11:09:44 +08002341 /* Use new "search" start rate */
Tomas Winklere7d326a2008-06-12 09:47:11 +08002342 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002343
Tomas Winklere1623442009-01-27 14:27:56 -08002344 IWL_DEBUG_RATE(priv, "Switch current mcs: %X index: %d\n",
Guy Cohen39e88502008-04-23 17:14:57 -07002345 tbl->current_rate, index);
Guy Cohen07bc28e2008-04-23 17:15:03 -07002346 rs_fill_link_cmd(priv, lq_sta, tbl->current_rate);
Tomas Winkler66c73db2008-04-15 16:01:40 -07002347 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
Mohamed Abbas447fee72009-04-20 14:37:02 -07002348 } else
2349 done_search = 1;
2350 }
Zhu Yib481de92007-09-25 17:54:57 -07002351
Mohamed Abbas447fee72009-04-20 14:37:02 -07002352 if (done_search && !lq_sta->stay_in_tbl) {
Ben Cahill77626352007-11-29 11:09:44 +08002353 /* If the "active" (non-search) mode was legacy,
2354 * and we've tried switching antennas,
2355 * but we haven't been able to try HT modes (not available),
2356 * stay with best antenna legacy modulation for a while
2357 * before next round of mode comparisons. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002358 tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
Luis R. Rodriguezde27e642008-12-23 15:58:44 -08002359 if (is_legacy(tbl1->lq_type) && !conf_is_ht(conf) &&
Mohamed Abbasd6e93392009-05-08 13:44:39 -07002360 lq_sta->action_counter > tbl1->max_search) {
Tomas Winklere1623442009-01-27 14:27:56 -08002361 IWL_DEBUG_RATE(priv, "LQ: STAY in legacy table\n");
Ester Kummerbf403db2008-05-05 10:22:40 +08002362 rs_set_stay_in_table(priv, 1, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002363 }
2364
Ben Cahill77626352007-11-29 11:09:44 +08002365 /* If we're in an HT mode, and all 3 mode switch actions
2366 * have been tried and compared, stay in this best modulation
2367 * mode for a while before next round of mode comparisons. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002368 if (lq_sta->enable_counter &&
Mohamed Abbasd6e93392009-05-08 13:44:39 -07002369 (lq_sta->action_counter >= tbl1->max_search)) {
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002370 if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
2371 (lq_sta->tx_agg_tid_en & (1 << tid)) &&
2372 (tid != MAX_TID_COUNT)) {
Wey-Yi Guy86b47662009-03-10 14:35:09 -07002373 tid_data =
2374 &priv->stations[lq_sta->lq.sta_id].tid[tid];
2375 if (tid_data->agg.state == IWL_AGG_OFF) {
2376 IWL_DEBUG_RATE(priv,
2377 "try to aggregate tid %d\n",
2378 tid);
2379 rs_tl_turn_on_agg(priv, tid,
2380 lq_sta, sta);
2381 }
Zhu Yib481de92007-09-25 17:54:57 -07002382 }
Ester Kummerbf403db2008-05-05 10:22:40 +08002383 rs_set_stay_in_table(priv, 0, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002384 }
Zhu Yib481de92007-09-25 17:54:57 -07002385 }
2386
2387out:
Tomas Winkler978785a2008-12-19 10:37:31 +08002388 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
Zhu Yib481de92007-09-25 17:54:57 -07002389 i = index;
Johannes Bergb7e35002008-09-11 02:22:58 +02002390 lq_sta->last_txrate_idx = i;
Zhu Yib481de92007-09-25 17:54:57 -07002391
Zhu Yib481de92007-09-25 17:54:57 -07002392 return;
2393}
2394
2395
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002396static void rs_initialize_lq(struct iwl_priv *priv,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002397 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02002398 struct ieee80211_sta *sta,
2399 struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002400{
Tomas Winklere227cea2008-07-18 13:53:05 +08002401 struct iwl_scale_tbl_info *tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002402 int rate_idx;
Guy Cohenaade00c2008-04-23 17:14:59 -07002403 int i;
Guy Cohen39e88502008-04-23 17:14:57 -07002404 u32 rate;
Guy Cohenaade00c2008-04-23 17:14:59 -07002405 u8 use_green = rs_use_green(priv, conf);
2406 u8 active_tbl = 0;
2407 u8 valid_tx_ant;
Zhu Yib481de92007-09-25 17:54:57 -07002408
Johannes Berg4b7679a2008-09-18 18:14:18 +02002409 if (!sta || !lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002410 goto out;
2411
Johannes Bergb7e35002008-09-11 02:22:58 +02002412 i = lq_sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002413
Tomas Winklerc33104f2008-01-14 17:46:21 -08002414 if ((lq_sta->lq.sta_id == 0xff) &&
Johannes Berg05c914f2008-09-11 00:01:58 +02002415 (priv->iw_mode == NL80211_IFTYPE_ADHOC))
Zhu Yib481de92007-09-25 17:54:57 -07002416 goto out;
2417
Guy Cohenaade00c2008-04-23 17:14:59 -07002418 valid_tx_ant = priv->hw_params.valid_tx_ant;
2419
Tomas Winklerc33104f2008-01-14 17:46:21 -08002420 if (!lq_sta->search_better_tbl)
2421 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002422 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002423 active_tbl = 1 - lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002424
Tomas Winklerc33104f2008-01-14 17:46:21 -08002425 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07002426
2427 if ((i < 0) || (i >= IWL_RATE_COUNT))
2428 i = 0;
2429
Tomas Winkler1826dcc2008-05-15 13:54:02 +08002430 rate = iwl_rates[i].plcp;
Winkler, Tomaseb48dca2008-10-29 14:05:48 -07002431 tbl->ant_type = first_antenna(valid_tx_ant);
2432 rate |= tbl->ant_type << RATE_MCS_ANT_POS;
Zhu Yib481de92007-09-25 17:54:57 -07002433
2434 if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
Guy Cohen39e88502008-04-23 17:14:57 -07002435 rate |= RATE_MCS_CCK_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002436
Guy Cohen39e88502008-04-23 17:14:57 -07002437 rs_get_tbl_info_from_mcs(rate, priv->band, tbl, &rate_idx);
Guy Cohenaade00c2008-04-23 17:14:59 -07002438 if (!rs_is_valid_ant(valid_tx_ant, tbl->ant_type))
2439 rs_toggle_antenna(valid_tx_ant, &rate, tbl);
Zhu Yib481de92007-09-25 17:54:57 -07002440
Tomas Winkler978785a2008-12-19 10:37:31 +08002441 rate = rate_n_flags_from_tbl(priv, tbl, rate_idx, use_green);
Guy Cohen39e88502008-04-23 17:14:57 -07002442 tbl->current_rate = rate;
Guy Coheneecd6e52008-04-23 17:14:58 -07002443 rs_set_expected_tpt_table(lq_sta, tbl);
Guy Cohen07bc28e2008-04-23 17:15:03 -07002444 rs_fill_link_cmd(NULL, lq_sta, rate);
Tomas Winkler66c73db2008-04-15 16:01:40 -07002445 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07002446 out:
2447 return;
2448}
2449
Johannes Berge6a98542008-10-21 12:40:02 +02002450static void rs_get_rate(void *priv_r, struct ieee80211_sta *sta, void *priv_sta,
2451 struct ieee80211_tx_rate_control *txrc)
Zhu Yib481de92007-09-25 17:54:57 -07002452{
2453
Johannes Berge6a98542008-10-21 12:40:02 +02002454 struct sk_buff *skb = txrc->skb;
2455 struct ieee80211_supported_band *sband = txrc->sband;
Johannes Berg4b7679a2008-09-18 18:14:18 +02002456 struct iwl_priv *priv = (struct iwl_priv *)priv_r;
2457 struct ieee80211_conf *conf = &priv->hw->conf;
Zhu Yib481de92007-09-25 17:54:57 -07002458 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berge6a98542008-10-21 12:40:02 +02002459 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Tomas Winkler417f1142008-11-12 13:14:06 -08002460 struct iwl_lq_sta *lq_sta = priv_sta;
2461 int rate_idx;
Abbas, Mohamedc338ba32009-01-21 10:58:02 -08002462 u64 mask_bit = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002463
Tomas Winklere1623442009-01-27 14:27:56 -08002464 IWL_DEBUG_RATE_LIMIT(priv, "rate scale calculate new rate for skb\n");
Zhu Yib481de92007-09-25 17:54:57 -07002465
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002466 /* Get max rate if user set max rate */
2467 if (lq_sta) {
2468 lq_sta->max_rate_idx = txrc->max_rate_idx;
2469 if ((sband->band == IEEE80211_BAND_5GHZ) &&
2470 (lq_sta->max_rate_idx != -1))
2471 lq_sta->max_rate_idx += IWL_FIRST_OFDM_RATE;
2472 if ((lq_sta->max_rate_idx < 0) ||
2473 (lq_sta->max_rate_idx >= IWL_RATE_COUNT))
2474 lq_sta->max_rate_idx = -1;
2475 }
2476
Abbas, Mohamedc338ba32009-01-21 10:58:02 -08002477 if (sta)
2478 mask_bit = sta->supp_rates[sband->band];
2479
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002480 /* Send management frames and NO_ACK data using lowest rate. */
Tomas Winkler417f1142008-11-12 13:14:06 -08002481 if (!ieee80211_is_data(hdr->frame_control) ||
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002482 info->flags & IEEE80211_TX_CTL_NO_ACK || !sta || !lq_sta) {
Abbas, Mohamedc338ba32009-01-21 10:58:02 -08002483 if (!mask_bit)
2484 info->control.rates[0].idx =
2485 rate_lowest_index(sband, NULL);
2486 else
2487 info->control.rates[0].idx =
2488 rate_lowest_index(sband, sta);
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002489 if (info->flags & IEEE80211_TX_CTL_NO_ACK)
2490 info->control.rates[0].count = 1;
Johannes Berg4b7679a2008-09-18 18:14:18 +02002491 return;
Zhu Yib481de92007-09-25 17:54:57 -07002492 }
2493
Tomas Winkler417f1142008-11-12 13:14:06 -08002494 rate_idx = lq_sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002495
Johannes Berg05c914f2008-09-11 00:01:58 +02002496 if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) &&
Tomas Winklerc33104f2008-01-14 17:46:21 -08002497 !lq_sta->ibss_sta_added) {
Abhijeet Kolekare11bc022009-04-08 11:26:41 -07002498 u8 sta_id = priv->cfg->ops->smgmt->find_station(priv,
2499 hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002500
2501 if (sta_id == IWL_INVALID_STATION) {
Tomas Winklere1623442009-01-27 14:27:56 -08002502 IWL_DEBUG_RATE(priv, "LQ: ADD station %pM\n",
Johannes Berge1749612008-10-27 15:59:26 -07002503 hdr->addr1);
Abhijeet Kolekar06fd3d82009-04-08 11:26:42 -07002504 sta_id = priv->cfg->ops->smgmt->add_station(priv,
Abhijeet Kolekare11bc022009-04-08 11:26:41 -07002505 hdr->addr1, 0,
2506 CMD_ASYNC, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07002507 }
2508 if ((sta_id != IWL_INVALID_STATION)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002509 lq_sta->lq.sta_id = sta_id;
2510 lq_sta->lq.rs_table[0].rate_n_flags = 0;
2511 lq_sta->ibss_sta_added = 1;
Johannes Berg4b7679a2008-09-18 18:14:18 +02002512 rs_initialize_lq(priv, conf, sta, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002513 }
Zhu Yib481de92007-09-25 17:54:57 -07002514 }
2515
Tomas Winkler417f1142008-11-12 13:14:06 -08002516 if (rate_idx < 0 || rate_idx > IWL_RATE_COUNT)
2517 rate_idx = rate_lowest_index(sband, sta);
2518 else if (sband->band == IEEE80211_BAND_5GHZ)
2519 rate_idx -= IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -07002520
Tomas Winkler417f1142008-11-12 13:14:06 -08002521 info->control.rates[0].idx = rate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002522}
2523
Johannes Berg4b7679a2008-09-18 18:14:18 +02002524static void *rs_alloc_sta(void *priv_rate, struct ieee80211_sta *sta,
2525 gfp_t gfp)
Zhu Yib481de92007-09-25 17:54:57 -07002526{
Tomas Winklere227cea2008-07-18 13:53:05 +08002527 struct iwl_lq_sta *lq_sta;
Ester Kummerbf403db2008-05-05 10:22:40 +08002528 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07002529 int i, j;
2530
Ester Kummerbf403db2008-05-05 10:22:40 +08002531 priv = (struct iwl_priv *)priv_rate;
Tomas Winklere1623442009-01-27 14:27:56 -08002532 IWL_DEBUG_RATE(priv, "create station rate scale window\n");
Zhu Yib481de92007-09-25 17:54:57 -07002533
Tomas Winklere227cea2008-07-18 13:53:05 +08002534 lq_sta = kzalloc(sizeof(struct iwl_lq_sta), gfp);
Zhu Yib481de92007-09-25 17:54:57 -07002535
Tomas Winklerc33104f2008-01-14 17:46:21 -08002536 if (lq_sta == NULL)
Zhu Yib481de92007-09-25 17:54:57 -07002537 return NULL;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002538 lq_sta->lq.sta_id = 0xff;
Zhu Yib481de92007-09-25 17:54:57 -07002539
Zhu Yi63fddb92007-09-27 11:27:36 +08002540
Zhu Yib481de92007-09-25 17:54:57 -07002541 for (j = 0; j < LQ_SIZE; j++)
2542 for (i = 0; i < IWL_RATE_COUNT; i++)
Tomas Winkler3ac7f142008-07-21 02:40:14 +03002543 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
Zhu Yib481de92007-09-25 17:54:57 -07002544
Tomas Winklerc33104f2008-01-14 17:46:21 -08002545 return lq_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002546}
2547
Johannes Berg4b7679a2008-09-18 18:14:18 +02002548static void rs_rate_init(void *priv_r, struct ieee80211_supported_band *sband,
2549 struct ieee80211_sta *sta, void *priv_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002550{
2551 int i, j;
Johannes Berg4b7679a2008-09-18 18:14:18 +02002552 struct iwl_priv *priv = (struct iwl_priv *)priv_r;
2553 struct ieee80211_conf *conf = &priv->hw->conf;
Tomas Winklere227cea2008-07-18 13:53:05 +08002554 struct iwl_lq_sta *lq_sta = priv_sta;
Mohamed Abbasd5e49032008-12-12 08:22:15 -08002555 u16 mask_bit = 0;
Abbas, Mohamed7d049e52009-01-20 21:33:53 -08002556 int count;
2557 int start_rate = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002558
Tomas Winklerc33104f2008-01-14 17:46:21 -08002559 lq_sta->flush_timer = 0;
Johannes Berg4b7679a2008-09-18 18:14:18 +02002560 lq_sta->supp_rates = sta->supp_rates[sband->band];
Zhu Yib481de92007-09-25 17:54:57 -07002561 for (j = 0; j < LQ_SIZE; j++)
2562 for (i = 0; i < IWL_RATE_COUNT; i++)
Tomas Winkler3ac7f142008-07-21 02:40:14 +03002563 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
Zhu Yib481de92007-09-25 17:54:57 -07002564
Tomas Winklere1623442009-01-27 14:27:56 -08002565 IWL_DEBUG_RATE(priv, "LQ: *** rate scale station global init ***\n");
Zhu Yib481de92007-09-25 17:54:57 -07002566 /* TODO: what is a good starting rate for STA? About middle? Maybe not
2567 * the lowest or the highest rate.. Could consider using RSSI from
2568 * previous packets? Need to have IEEE 802.1X auth succeed immediately
2569 * after assoc.. */
2570
Tomas Winklerc33104f2008-01-14 17:46:21 -08002571 lq_sta->ibss_sta_added = 0;
Johannes Berg05c914f2008-09-11 00:01:58 +02002572 if (priv->iw_mode == NL80211_IFTYPE_AP) {
Abhijeet Kolekare11bc022009-04-08 11:26:41 -07002573 u8 sta_id = priv->cfg->ops->smgmt->find_station(priv,
2574 sta->addr);
Joe Perches0795af52007-10-03 17:59:30 -07002575
Zhu Yib481de92007-09-25 17:54:57 -07002576 /* for IBSS the call are from tasklet */
Tomas Winklere1623442009-01-27 14:27:56 -08002577 IWL_DEBUG_RATE(priv, "LQ: ADD station %pM\n", sta->addr);
Zhu Yib481de92007-09-25 17:54:57 -07002578
2579 if (sta_id == IWL_INVALID_STATION) {
Tomas Winklere1623442009-01-27 14:27:56 -08002580 IWL_DEBUG_RATE(priv, "LQ: ADD station %pM\n", sta->addr);
Abhijeet Kolekar06fd3d82009-04-08 11:26:42 -07002581 sta_id = priv->cfg->ops->smgmt->add_station(priv,
Abhijeet Kolekare11bc022009-04-08 11:26:41 -07002582 sta->addr, 0,
2583 CMD_ASYNC, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07002584 }
2585 if ((sta_id != IWL_INVALID_STATION)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002586 lq_sta->lq.sta_id = sta_id;
2587 lq_sta->lq.rs_table[0].rate_n_flags = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002588 }
2589 /* FIXME: this is w/a remove it later */
2590 priv->assoc_station_added = 1;
2591 }
2592
Tomas Winklerc33104f2008-01-14 17:46:21 -08002593 lq_sta->is_dup = 0;
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002594 lq_sta->max_rate_idx = -1;
Abbas, Mohamed7d049e52009-01-20 21:33:53 -08002595 lq_sta->missed_rate_counter = IWL_MISSED_RATE_MAX;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002596 lq_sta->is_green = rs_use_green(priv, conf);
Guy Cohen39e88502008-04-23 17:14:57 -07002597 lq_sta->active_legacy_rate = priv->active_rate & ~(0x1000);
Tomas Winklerc33104f2008-01-14 17:46:21 -08002598 lq_sta->active_rate_basic = priv->active_rate_basic;
Johannes Berg8318d782008-01-24 19:38:38 +01002599 lq_sta->band = priv->band;
Ben Cahill77626352007-11-29 11:09:44 +08002600 /*
2601 * active_siso_rate mask includes 9 MBits (bit 5), and CCK (bits 0-3),
2602 * supp_rates[] does not; shift to convert format, force 9 MBits off.
2603 */
Johannes Bergae5eb022008-10-14 16:58:37 +02002604 lq_sta->active_siso_rate = sta->ht_cap.mcs.rx_mask[0] << 1;
2605 lq_sta->active_siso_rate |= sta->ht_cap.mcs.rx_mask[0] & 0x1;
Guy Cohenfde0db32008-04-21 15:42:01 -07002606 lq_sta->active_siso_rate &= ~((u16)0x2);
2607 lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -07002608
Ben Cahill77626352007-11-29 11:09:44 +08002609 /* Same here */
Johannes Bergae5eb022008-10-14 16:58:37 +02002610 lq_sta->active_mimo2_rate = sta->ht_cap.mcs.rx_mask[1] << 1;
2611 lq_sta->active_mimo2_rate |= sta->ht_cap.mcs.rx_mask[1] & 0x1;
Guy Cohenfde0db32008-04-21 15:42:01 -07002612 lq_sta->active_mimo2_rate &= ~((u16)0x2);
2613 lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE;
2614
Johannes Bergae5eb022008-10-14 16:58:37 +02002615 lq_sta->active_mimo3_rate = sta->ht_cap.mcs.rx_mask[2] << 1;
2616 lq_sta->active_mimo3_rate |= sta->ht_cap.mcs.rx_mask[2] & 0x1;
Guy Cohenfde0db32008-04-21 15:42:01 -07002617 lq_sta->active_mimo3_rate &= ~((u16)0x2);
2618 lq_sta->active_mimo3_rate <<= IWL_FIRST_OFDM_RATE;
2619
Tomas Winklere1623442009-01-27 14:27:56 -08002620 IWL_DEBUG_RATE(priv, "SISO-RATE=%X MIMO2-RATE=%X MIMO3-RATE=%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002621 lq_sta->active_siso_rate,
Guy Cohenfde0db32008-04-21 15:42:01 -07002622 lq_sta->active_mimo2_rate,
2623 lq_sta->active_mimo3_rate);
2624
Tomas Winklera96a27f2008-10-23 23:48:56 -07002625 /* These values will be overridden later */
Guy Cohen07bc28e2008-04-23 17:15:03 -07002626 lq_sta->lq.general_params.single_stream_ant_msk = ANT_A;
2627 lq_sta->lq.general_params.dual_stream_ant_msk = ANT_AB;
2628
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002629 /* as default allow aggregation for all tids */
2630 lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002631 lq_sta->drv = priv;
Zhu Yib481de92007-09-25 17:54:57 -07002632
Mohamed Abbasd5e49032008-12-12 08:22:15 -08002633 /* Find highest tx rate supported by hardware and destination station */
Abbas, Mohamed7d049e52009-01-20 21:33:53 -08002634 mask_bit = sta->supp_rates[sband->band];
2635 count = sband->n_bitrates;
2636 if (sband->band == IEEE80211_BAND_5GHZ) {
2637 count += IWL_FIRST_OFDM_RATE;
2638 start_rate = IWL_FIRST_OFDM_RATE;
2639 mask_bit <<= IWL_FIRST_OFDM_RATE;
2640 }
2641
2642 mask_bit = mask_bit & lq_sta->active_legacy_rate;
2643 lq_sta->last_txrate_idx = 4;
2644 for (i = start_rate; i < count; i++)
Mohamed Abbasd5e49032008-12-12 08:22:15 -08002645 if (mask_bit & BIT(i))
2646 lq_sta->last_txrate_idx = i;
2647
Johannes Berg4b7679a2008-09-18 18:14:18 +02002648 rs_initialize_lq(priv, conf, sta, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002649}
2650
Guy Cohenfde0db32008-04-21 15:42:01 -07002651static void rs_fill_link_cmd(const struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08002652 struct iwl_lq_sta *lq_sta, u32 new_rate)
Zhu Yib481de92007-09-25 17:54:57 -07002653{
Tomas Winklere227cea2008-07-18 13:53:05 +08002654 struct iwl_scale_tbl_info tbl_type;
Zhu Yib481de92007-09-25 17:54:57 -07002655 int index = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002656 int rate_idx;
Zhu Yi1b696de2007-09-27 11:27:41 +08002657 int repeat_rate = 0;
Guy Cohenaade00c2008-04-23 17:14:59 -07002658 u8 ant_toggle_cnt = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002659 u8 use_ht_possible = 1;
Guy Cohenaade00c2008-04-23 17:14:59 -07002660 u8 valid_tx_ant = 0;
Guy Cohen07bc28e2008-04-23 17:15:03 -07002661 struct iwl_link_quality_cmd *lq_cmd = &lq_sta->lq;
Zhu Yib481de92007-09-25 17:54:57 -07002662
Ben Cahill77626352007-11-29 11:09:44 +08002663 /* Override starting rate (index 0) if needed for debug purposes */
Guy Cohen39e88502008-04-23 17:14:57 -07002664 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Zhu Yi98d7e092007-09-27 11:27:42 +08002665
Guy Cohen39e88502008-04-23 17:14:57 -07002666 /* Interpret new_rate (rate_n_flags) */
Guy Cohenaade00c2008-04-23 17:14:59 -07002667 memset(&tbl_type, 0, sizeof(tbl_type));
Guy Cohen39e88502008-04-23 17:14:57 -07002668 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band,
Zhu Yib481de92007-09-25 17:54:57 -07002669 &tbl_type, &rate_idx);
2670
Ben Cahill77626352007-11-29 11:09:44 +08002671 /* How many times should we repeat the initial rate? */
Zhu Yib481de92007-09-25 17:54:57 -07002672 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07002673 ant_toggle_cnt = 1;
Zhu Yi1b696de2007-09-27 11:27:41 +08002674 repeat_rate = IWL_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002675 } else {
Zhu Yi1b696de2007-09-27 11:27:41 +08002676 repeat_rate = IWL_HT_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002677 }
Zhu Yib481de92007-09-25 17:54:57 -07002678
2679 lq_cmd->general_params.mimo_delimiter =
2680 is_mimo(tbl_type.lq_type) ? 1 : 0;
Ben Cahill77626352007-11-29 11:09:44 +08002681
2682 /* Fill 1st table entry (index 0) */
Guy Cohen39e88502008-04-23 17:14:57 -07002683 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002684
Guy Cohen07bc28e2008-04-23 17:15:03 -07002685 if (num_of_ant(tbl_type.ant_type) == 1) {
2686 lq_cmd->general_params.single_stream_ant_msk =
2687 tbl_type.ant_type;
2688 } else if (num_of_ant(tbl_type.ant_type) == 2) {
2689 lq_cmd->general_params.dual_stream_ant_msk =
2690 tbl_type.ant_type;
2691 } /* otherwise we don't modify the existing value */
Zhu Yib481de92007-09-25 17:54:57 -07002692
2693 index++;
Zhu Yi1b696de2007-09-27 11:27:41 +08002694 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002695
Guy Cohenaade00c2008-04-23 17:14:59 -07002696 if (priv)
2697 valid_tx_ant = priv->hw_params.valid_tx_ant;
2698
Ben Cahill77626352007-11-29 11:09:44 +08002699 /* Fill rest of rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002700 while (index < LINK_QUAL_MAX_RETRY_NUM) {
Ben Cahill77626352007-11-29 11:09:44 +08002701 /* Repeat initial/next rate.
2702 * For legacy IWL_NUMBER_TRY == 1, this loop will not execute.
2703 * For HT IWL_HT_NUMBER_TRY == 3, this executes twice. */
Zhu Yi1b696de2007-09-27 11:27:41 +08002704 while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
Zhu Yib481de92007-09-25 17:54:57 -07002705 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07002706 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2707 ant_toggle_cnt++;
2708 else if (priv &&
2709 rs_toggle_antenna(valid_tx_ant,
2710 &new_rate, &tbl_type))
2711 ant_toggle_cnt = 1;
2712}
Zhu Yi98d7e092007-09-27 11:27:42 +08002713
Ben Cahill77626352007-11-29 11:09:44 +08002714 /* Override next rate if needed for debug purposes */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002715 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Ben Cahill77626352007-11-29 11:09:44 +08002716
2717 /* Fill next table entry */
Zhu Yib481de92007-09-25 17:54:57 -07002718 lq_cmd->rs_table[index].rate_n_flags =
Guy Cohen39e88502008-04-23 17:14:57 -07002719 cpu_to_le32(new_rate);
Zhu Yi1b696de2007-09-27 11:27:41 +08002720 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002721 index++;
2722 }
2723
Guy Cohen39e88502008-04-23 17:14:57 -07002724 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band, &tbl_type,
Zhu Yib481de92007-09-25 17:54:57 -07002725 &rate_idx);
2726
Ben Cahill77626352007-11-29 11:09:44 +08002727 /* Indicate to uCode which entries might be MIMO.
2728 * If initial rate was MIMO, this will finally end up
2729 * as (IWL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
Zhu Yib481de92007-09-25 17:54:57 -07002730 if (is_mimo(tbl_type.lq_type))
2731 lq_cmd->general_params.mimo_delimiter = index;
2732
Ben Cahill77626352007-11-29 11:09:44 +08002733 /* Get next rate */
Guy Cohen39e88502008-04-23 17:14:57 -07002734 new_rate = rs_get_lower_rate(lq_sta, &tbl_type, rate_idx,
2735 use_ht_possible);
Zhu Yib481de92007-09-25 17:54:57 -07002736
Ben Cahill77626352007-11-29 11:09:44 +08002737 /* How many times should we repeat the next rate? */
Zhu Yib481de92007-09-25 17:54:57 -07002738 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07002739 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2740 ant_toggle_cnt++;
2741 else if (priv &&
2742 rs_toggle_antenna(valid_tx_ant,
2743 &new_rate, &tbl_type))
2744 ant_toggle_cnt = 1;
2745
Zhu Yi1b696de2007-09-27 11:27:41 +08002746 repeat_rate = IWL_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002747 } else {
Zhu Yi1b696de2007-09-27 11:27:41 +08002748 repeat_rate = IWL_HT_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002749 }
Zhu Yib481de92007-09-25 17:54:57 -07002750
Ben Cahill77626352007-11-29 11:09:44 +08002751 /* Don't allow HT rates after next pass.
2752 * rs_get_lower_rate() will change type to LQ_A or LQ_G. */
Zhu Yib481de92007-09-25 17:54:57 -07002753 use_ht_possible = 0;
2754
Ben Cahill77626352007-11-29 11:09:44 +08002755 /* Override next rate if needed for debug purposes */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002756 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Ben Cahill77626352007-11-29 11:09:44 +08002757
2758 /* Fill next table entry */
Guy Cohen39e88502008-04-23 17:14:57 -07002759 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002760
2761 index++;
Zhu Yi1b696de2007-09-27 11:27:41 +08002762 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002763 }
2764
Tomas Winkler5e1dd8d2008-05-29 16:35:17 +08002765 lq_cmd->agg_params.agg_frame_cnt_limit = 64;
Zhu Yib481de92007-09-25 17:54:57 -07002766 lq_cmd->agg_params.agg_dis_start_th = 3;
2767 lq_cmd->agg_params.agg_time_limit = cpu_to_le16(4000);
Zhu Yib481de92007-09-25 17:54:57 -07002768}
2769
Johannes Berg4b7679a2008-09-18 18:14:18 +02002770static void *rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
Zhu Yib481de92007-09-25 17:54:57 -07002771{
Johannes Berg4b7679a2008-09-18 18:14:18 +02002772 return hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07002773}
2774/* rate scale requires free function to be implemented */
2775static void rs_free(void *priv_rate)
2776{
2777 return;
2778}
2779
Johannes Berg4b7679a2008-09-18 18:14:18 +02002780static void rs_free_sta(void *priv_r, struct ieee80211_sta *sta,
2781 void *priv_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002782{
Tomas Winklere227cea2008-07-18 13:53:05 +08002783 struct iwl_lq_sta *lq_sta = priv_sta;
Rami Rosen45527c22008-10-07 09:50:01 +02002784 struct iwl_priv *priv __maybe_unused = priv_r;
Zhu Yib481de92007-09-25 17:54:57 -07002785
Tomas Winklere1623442009-01-27 14:27:56 -08002786 IWL_DEBUG_RATE(priv, "enter\n");
Tomas Winklerc33104f2008-01-14 17:46:21 -08002787 kfree(lq_sta);
Tomas Winklere1623442009-01-27 14:27:56 -08002788 IWL_DEBUG_RATE(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07002789}
2790
2791
Zhu Yi93dc6462007-09-27 11:27:37 +08002792#ifdef CONFIG_MAC80211_DEBUGFS
Zhu Yi5ae212c2007-09-27 11:27:38 +08002793static int open_file_generic(struct inode *inode, struct file *file)
2794{
2795 file->private_data = inode->i_private;
2796 return 0;
2797}
Tomas Winklere227cea2008-07-18 13:53:05 +08002798static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
2799 u32 *rate_n_flags, int index)
Zhu Yi98d7e092007-09-27 11:27:42 +08002800{
Ester Kummerbf403db2008-05-05 10:22:40 +08002801 struct iwl_priv *priv;
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07002802 u8 valid_tx_ant;
2803 u8 ant_sel_tx;
Ester Kummerbf403db2008-05-05 10:22:40 +08002804
2805 priv = lq_sta->drv;
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07002806 valid_tx_ant = priv->hw_params.valid_tx_ant;
Guy Cohen39e88502008-04-23 17:14:57 -07002807 if (lq_sta->dbg_fixed_rate) {
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07002808 ant_sel_tx =
2809 ((lq_sta->dbg_fixed_rate & RATE_MCS_ANT_ABC_MSK)
2810 >> RATE_MCS_ANT_POS);
2811 if ((valid_tx_ant & ant_sel_tx) == ant_sel_tx) {
Guy Cohen39e88502008-04-23 17:14:57 -07002812 *rate_n_flags = lq_sta->dbg_fixed_rate;
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07002813 IWL_DEBUG_RATE(priv, "Fixed rate ON\n");
Guy Cohen39e88502008-04-23 17:14:57 -07002814 } else {
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07002815 lq_sta->dbg_fixed_rate = 0;
2816 IWL_ERR(priv,
2817 "Invalid antenna selection 0x%X, Valid is 0x%X\n",
2818 ant_sel_tx, valid_tx_ant);
2819 IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
Guy Cohen39e88502008-04-23 17:14:57 -07002820 }
Guy Cohen39e88502008-04-23 17:14:57 -07002821 } else {
Tomas Winklere1623442009-01-27 14:27:56 -08002822 IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
Zhu Yi98d7e092007-09-27 11:27:42 +08002823 }
Zhu Yi98d7e092007-09-27 11:27:42 +08002824}
2825
2826static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
2827 const char __user *user_buf, size_t count, loff_t *ppos)
2828{
Tomas Winklere227cea2008-07-18 13:53:05 +08002829 struct iwl_lq_sta *lq_sta = file->private_data;
Ester Kummerbf403db2008-05-05 10:22:40 +08002830 struct iwl_priv *priv;
Zhu Yi98d7e092007-09-27 11:27:42 +08002831 char buf[64];
2832 int buf_size;
2833 u32 parsed_rate;
2834
Ester Kummerbf403db2008-05-05 10:22:40 +08002835 priv = lq_sta->drv;
Zhu Yi98d7e092007-09-27 11:27:42 +08002836 memset(buf, 0, sizeof(buf));
2837 buf_size = min(count, sizeof(buf) - 1);
2838 if (copy_from_user(buf, user_buf, buf_size))
2839 return -EFAULT;
2840
2841 if (sscanf(buf, "%x", &parsed_rate) == 1)
Guy Cohen39e88502008-04-23 17:14:57 -07002842 lq_sta->dbg_fixed_rate = parsed_rate;
Zhu Yi98d7e092007-09-27 11:27:42 +08002843 else
Guy Cohen39e88502008-04-23 17:14:57 -07002844 lq_sta->dbg_fixed_rate = 0;
Zhu Yi98d7e092007-09-27 11:27:42 +08002845
Guy Cohen39e88502008-04-23 17:14:57 -07002846 lq_sta->active_legacy_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
2847 lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2848 lq_sta->active_mimo2_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2849 lq_sta->active_mimo3_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
Zhu Yi98d7e092007-09-27 11:27:42 +08002850
Tomas Winklere1623442009-01-27 14:27:56 -08002851 IWL_DEBUG_RATE(priv, "sta_id %d rate 0x%X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07002852 lq_sta->lq.sta_id, lq_sta->dbg_fixed_rate);
Zhu Yi98d7e092007-09-27 11:27:42 +08002853
Guy Cohen39e88502008-04-23 17:14:57 -07002854 if (lq_sta->dbg_fixed_rate) {
Guy Cohen07bc28e2008-04-23 17:15:03 -07002855 rs_fill_link_cmd(NULL, lq_sta, lq_sta->dbg_fixed_rate);
Tomas Winkler66c73db2008-04-15 16:01:40 -07002856 iwl_send_lq_cmd(lq_sta->drv, &lq_sta->lq, CMD_ASYNC);
Zhu Yi98d7e092007-09-27 11:27:42 +08002857 }
2858
2859 return count;
2860}
Zhu Yi0209dc12007-09-27 11:27:43 +08002861
Zhu Yi5ae212c2007-09-27 11:27:38 +08002862static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
2863 char __user *user_buf, size_t count, loff_t *ppos)
2864{
Frank Seidela412c802009-03-01 20:25:38 +01002865 char *buff;
Zhu Yi5ae212c2007-09-27 11:27:38 +08002866 int desc = 0;
2867 int i = 0;
Wey-Yi Guy12b96812009-04-08 11:39:27 -07002868 int index = 0;
Frank Seidela412c802009-03-01 20:25:38 +01002869 ssize_t ret;
Zhu Yi5ae212c2007-09-27 11:27:38 +08002870
Tomas Winklere227cea2008-07-18 13:53:05 +08002871 struct iwl_lq_sta *lq_sta = file->private_data;
Wey-Yi Guyd8ae4f52009-03-10 14:35:07 -07002872 struct iwl_priv *priv;
Wey-Yi Guyadb7b5e2009-03-10 14:35:08 -07002873 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002874
Wey-Yi Guyd8ae4f52009-03-10 14:35:07 -07002875 priv = lq_sta->drv;
Frank Seidela412c802009-03-01 20:25:38 +01002876 buff = kmalloc(1024, GFP_KERNEL);
2877 if (!buff)
2878 return -ENOMEM;
2879
Tomas Winklerc33104f2008-01-14 17:46:21 -08002880 desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
Zhu Yi98d7e092007-09-27 11:27:42 +08002881 desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002882 lq_sta->total_failed, lq_sta->total_success,
Guy Cohen39e88502008-04-23 17:14:57 -07002883 lq_sta->active_legacy_rate);
Zhu Yi98d7e092007-09-27 11:27:42 +08002884 desc += sprintf(buff+desc, "fixed rate 0x%X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07002885 lq_sta->dbg_fixed_rate);
Wey-Yi Guyd8ae4f52009-03-10 14:35:07 -07002886 desc += sprintf(buff+desc, "valid_tx_ant %s%s%s\n",
2887 (priv->hw_params.valid_tx_ant & ANT_A) ? "ANT_A," : "",
2888 (priv->hw_params.valid_tx_ant & ANT_B) ? "ANT_B," : "",
2889 (priv->hw_params.valid_tx_ant & ANT_C) ? "ANT_C" : "");
Wey-Yi Guyadb7b5e2009-03-10 14:35:08 -07002890 desc += sprintf(buff+desc, "lq type %s\n",
2891 (is_legacy(tbl->lq_type)) ? "legacy" : "HT");
2892 if (is_Ht(tbl->lq_type)) {
2893 desc += sprintf(buff+desc, " %s",
2894 (is_siso(tbl->lq_type)) ? "SISO" :
2895 ((is_mimo2(tbl->lq_type)) ? "MIMO2" : "MIMO3"));
2896 desc += sprintf(buff+desc, " %s",
2897 (tbl->is_fat) ? "40MHz" : "20MHz");
2898 desc += sprintf(buff+desc, " %s\n", (tbl->is_SGI) ? "SGI" : "");
2899 }
Wey-Yi Guy12b96812009-04-08 11:39:27 -07002900 desc += sprintf(buff+desc, "last tx rate=0x%X\n",
2901 lq_sta->last_rate_n_flags);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002902 desc += sprintf(buff+desc, "general:"
2903 "flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002904 lq_sta->lq.general_params.flags,
2905 lq_sta->lq.general_params.mimo_delimiter,
2906 lq_sta->lq.general_params.single_stream_ant_msk,
2907 lq_sta->lq.general_params.dual_stream_ant_msk);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002908
2909 desc += sprintf(buff+desc, "agg:"
2910 "time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002911 le16_to_cpu(lq_sta->lq.agg_params.agg_time_limit),
2912 lq_sta->lq.agg_params.agg_dis_start_th,
2913 lq_sta->lq.agg_params.agg_frame_cnt_limit);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002914
2915 desc += sprintf(buff+desc,
2916 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002917 lq_sta->lq.general_params.start_rate_index[0],
2918 lq_sta->lq.general_params.start_rate_index[1],
2919 lq_sta->lq.general_params.start_rate_index[2],
2920 lq_sta->lq.general_params.start_rate_index[3]);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002921
Wey-Yi Guy12b96812009-04-08 11:39:27 -07002922 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
2923 index = iwl_hwrate_to_plcp_idx(
2924 le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags));
2925 if (is_legacy(tbl->lq_type)) {
2926 desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps\n",
2927 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags),
2928 iwl_rate_mcs[index].mbps);
2929 } else {
2930 desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps (%s)\n",
2931 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags),
2932 iwl_rate_mcs[index].mbps, iwl_rate_mcs[index].mcs);
2933 }
2934 }
Zhu Yi5ae212c2007-09-27 11:27:38 +08002935
Frank Seidela412c802009-03-01 20:25:38 +01002936 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2937 kfree(buff);
2938 return ret;
Zhu Yi5ae212c2007-09-27 11:27:38 +08002939}
2940
2941static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
Zhu Yi98d7e092007-09-27 11:27:42 +08002942 .write = rs_sta_dbgfs_scale_table_write,
Zhu Yi5ae212c2007-09-27 11:27:38 +08002943 .read = rs_sta_dbgfs_scale_table_read,
2944 .open = open_file_generic,
2945};
Zhu Yi0209dc12007-09-27 11:27:43 +08002946static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
2947 char __user *user_buf, size_t count, loff_t *ppos)
2948{
Frank Seidela412c802009-03-01 20:25:38 +01002949 char *buff;
Zhu Yi0209dc12007-09-27 11:27:43 +08002950 int desc = 0;
2951 int i, j;
Frank Seidela412c802009-03-01 20:25:38 +01002952 ssize_t ret;
Zhu Yi0209dc12007-09-27 11:27:43 +08002953
Tomas Winklere227cea2008-07-18 13:53:05 +08002954 struct iwl_lq_sta *lq_sta = file->private_data;
Frank Seidela412c802009-03-01 20:25:38 +01002955
2956 buff = kmalloc(1024, GFP_KERNEL);
2957 if (!buff)
2958 return -ENOMEM;
2959
Zhu Yi0209dc12007-09-27 11:27:43 +08002960 for (i = 0; i < LQ_SIZE; i++) {
2961 desc += sprintf(buff+desc, "%s type=%d SGI=%d FAT=%d DUP=%d\n"
2962 "rate=0x%X\n",
Abhijeet Kolekarc3056062008-11-12 13:14:08 -08002963 lq_sta->active_tbl == i ? "*" : "x",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002964 lq_sta->lq_info[i].lq_type,
2965 lq_sta->lq_info[i].is_SGI,
2966 lq_sta->lq_info[i].is_fat,
2967 lq_sta->lq_info[i].is_dup,
Guy Cohen39e88502008-04-23 17:14:57 -07002968 lq_sta->lq_info[i].current_rate);
Zhu Yi0209dc12007-09-27 11:27:43 +08002969 for (j = 0; j < IWL_RATE_COUNT; j++) {
2970 desc += sprintf(buff+desc,
Tomas Winklerc33104f2008-01-14 17:46:21 -08002971 "counter=%d success=%d %%=%d\n",
2972 lq_sta->lq_info[i].win[j].counter,
2973 lq_sta->lq_info[i].win[j].success_counter,
2974 lq_sta->lq_info[i].win[j].success_ratio);
Zhu Yi0209dc12007-09-27 11:27:43 +08002975 }
2976 }
Frank Seidela412c802009-03-01 20:25:38 +01002977 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2978 kfree(buff);
2979 return ret;
Zhu Yi0209dc12007-09-27 11:27:43 +08002980}
2981
2982static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
2983 .read = rs_sta_dbgfs_stats_table_read,
2984 .open = open_file_generic,
2985};
Zhu Yi5ae212c2007-09-27 11:27:38 +08002986
Zhu Yi93dc6462007-09-27 11:27:37 +08002987static void rs_add_debugfs(void *priv, void *priv_sta,
2988 struct dentry *dir)
2989{
Tomas Winklere227cea2008-07-18 13:53:05 +08002990 struct iwl_lq_sta *lq_sta = priv_sta;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002991 lq_sta->rs_sta_dbgfs_scale_table_file =
Zhu Yi0209dc12007-09-27 11:27:43 +08002992 debugfs_create_file("rate_scale_table", 0600, dir,
Tomas Winklerc33104f2008-01-14 17:46:21 -08002993 lq_sta, &rs_sta_dbgfs_scale_table_ops);
2994 lq_sta->rs_sta_dbgfs_stats_table_file =
Zhu Yi0209dc12007-09-27 11:27:43 +08002995 debugfs_create_file("rate_stats_table", 0600, dir,
Tomas Winklerc33104f2008-01-14 17:46:21 -08002996 lq_sta, &rs_sta_dbgfs_stats_table_ops);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002997 lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file =
2998 debugfs_create_u8("tx_agg_tid_enable", 0600, dir,
2999 &lq_sta->tx_agg_tid_en);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02003000
Zhu Yi93dc6462007-09-27 11:27:37 +08003001}
3002
3003static void rs_remove_debugfs(void *priv, void *priv_sta)
3004{
Tomas Winklere227cea2008-07-18 13:53:05 +08003005 struct iwl_lq_sta *lq_sta = priv_sta;
Tomas Winklerc33104f2008-01-14 17:46:21 -08003006 debugfs_remove(lq_sta->rs_sta_dbgfs_scale_table_file);
3007 debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02003008 debugfs_remove(lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file);
Zhu Yi93dc6462007-09-27 11:27:37 +08003009}
3010#endif
3011
Zhu Yib481de92007-09-25 17:54:57 -07003012static struct rate_control_ops rs_ops = {
3013 .module = NULL,
3014 .name = RS_NAME,
3015 .tx_status = rs_tx_status,
3016 .get_rate = rs_get_rate,
3017 .rate_init = rs_rate_init,
Zhu Yib481de92007-09-25 17:54:57 -07003018 .alloc = rs_alloc,
3019 .free = rs_free,
3020 .alloc_sta = rs_alloc_sta,
3021 .free_sta = rs_free_sta,
Zhu Yi93dc6462007-09-27 11:27:37 +08003022#ifdef CONFIG_MAC80211_DEBUGFS
3023 .add_sta_debugfs = rs_add_debugfs,
3024 .remove_sta_debugfs = rs_remove_debugfs,
3025#endif
Zhu Yib481de92007-09-25 17:54:57 -07003026};
3027
Tomas Winklere227cea2008-07-18 13:53:05 +08003028int iwlagn_rate_control_register(void)
Zhu Yib481de92007-09-25 17:54:57 -07003029{
Reinette Chatre897e1cf2008-03-28 16:21:09 -07003030 return ieee80211_rate_control_register(&rs_ops);
Zhu Yib481de92007-09-25 17:54:57 -07003031}
3032
Tomas Winklere227cea2008-07-18 13:53:05 +08003033void iwlagn_rate_control_unregister(void)
Zhu Yib481de92007-09-25 17:54:57 -07003034{
3035 ieee80211_rate_control_unregister(&rs_ops);
3036}
3037