blob: a21875dec6564445c2d2808e3b9f585f17d9ec10 [file] [log] [blame]
Zhu Yib481de92007-09-25 17:54:57 -07001/******************************************************************************
2 *
Reinette Chatreeb7ae892008-03-11 16:17:17 -07003 * Copyright(c) 2005 - 2008 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#include "iwl-helpers.h"
42
Tomas Winklere227cea2008-07-18 13:53:05 +080043#define RS_NAME "iwl-agn-rs"
Zhu Yib481de92007-09-25 17:54:57 -070044
Guy Cohenaade00c2008-04-23 17:14:59 -070045#define NUM_TRY_BEFORE_ANT_TOGGLE 1
Zhu Yib481de92007-09-25 17:54:57 -070046#define IWL_NUMBER_TRY 1
47#define IWL_HT_NUMBER_TRY 3
48
Ben Cahill77626352007-11-29 11:09:44 +080049#define IWL_RATE_MAX_WINDOW 62 /* # tx in history window */
50#define IWL_RATE_MIN_FAILURE_TH 6 /* min failures to calc tpt */
51#define IWL_RATE_MIN_SUCCESS_TH 8 /* min successes to calc tpt */
52
53/* max time to accum history 2 seconds */
54#define IWL_RATE_SCALE_FLUSH_INTVL (2*HZ)
Zhu Yib481de92007-09-25 17:54:57 -070055
56static u8 rs_ht_to_legacy[] = {
57 IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
58 IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
59 IWL_RATE_6M_INDEX,
60 IWL_RATE_6M_INDEX, IWL_RATE_9M_INDEX,
61 IWL_RATE_12M_INDEX, IWL_RATE_18M_INDEX,
62 IWL_RATE_24M_INDEX, IWL_RATE_36M_INDEX,
63 IWL_RATE_48M_INDEX, IWL_RATE_54M_INDEX
64};
65
Guy Cohenaade00c2008-04-23 17:14:59 -070066static const u8 ant_toggle_lookup[] = {
67 /*ANT_NONE -> */ ANT_NONE,
68 /*ANT_A -> */ ANT_B,
69 /*ANT_B -> */ ANT_C,
70 /*ANT_AB -> */ ANT_BC,
71 /*ANT_C -> */ ANT_A,
72 /*ANT_AC -> */ ANT_AB,
73 /*ANT_BC -> */ ANT_AC,
74 /*ANT_ABC -> */ ANT_ABC,
75};
76
Ben Cahill77626352007-11-29 11:09:44 +080077/**
Tomas Winklere227cea2008-07-18 13:53:05 +080078 * struct iwl_rate_scale_data -- tx success history for one rate
Ben Cahill77626352007-11-29 11:09:44 +080079 */
Tomas Winklere227cea2008-07-18 13:53:05 +080080struct iwl_rate_scale_data {
Ben Cahill77626352007-11-29 11:09:44 +080081 u64 data; /* bitmap of successful frames */
82 s32 success_counter; /* number of frames successful */
83 s32 success_ratio; /* per-cent * 128 */
84 s32 counter; /* number of frames attempted */
85 s32 average_tpt; /* success ratio * expected throughput */
Zhu Yib481de92007-09-25 17:54:57 -070086 unsigned long stamp;
87};
88
Ben Cahill77626352007-11-29 11:09:44 +080089/**
Tomas Winklere227cea2008-07-18 13:53:05 +080090 * struct iwl_scale_tbl_info -- tx params and success history for all rates
Ben Cahill77626352007-11-29 11:09:44 +080091 *
Tomas Winklere227cea2008-07-18 13:53:05 +080092 * There are two of these in struct iwl_lq_sta,
Ben Cahill77626352007-11-29 11:09:44 +080093 * one for "active", and one for "search".
94 */
Tomas Winklere227cea2008-07-18 13:53:05 +080095struct iwl_scale_tbl_info {
Guy Cohenfde0db32008-04-21 15:42:01 -070096 enum iwl_table_type lq_type;
97 u8 ant_type;
Ben Cahill77626352007-11-29 11:09:44 +080098 u8 is_SGI; /* 1 = short guard interval */
99 u8 is_fat; /* 1 = 40 MHz channel width */
100 u8 is_dup; /* 1 = duplicated data streams */
101 u8 action; /* change modulation; IWL_[LEGACY/SISO/MIMO]_SWITCH_* */
102 s32 *expected_tpt; /* throughput metrics; expected_tpt_G, etc. */
Guy Cohen39e88502008-04-23 17:14:57 -0700103 u32 current_rate; /* rate_n_flags, uCode API format */
Tomas Winklere227cea2008-07-18 13:53:05 +0800104 struct iwl_rate_scale_data win[IWL_RATE_COUNT]; /* rate histories */
Zhu Yib481de92007-09-25 17:54:57 -0700105};
106
Tomas Winklere227cea2008-07-18 13:53:05 +0800107struct iwl_traffic_load {
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200108 unsigned long time_stamp; /* age of the oldest statistics */
109 u32 packet_count[TID_QUEUE_MAX_SIZE]; /* packet count in this time
110 * slice */
111 u32 total; /* total num of packets during the
112 * last TID_MAX_TIME_DIFF */
113 u8 queue_count; /* number of queues that has
114 * been used since the last cleanup */
115 u8 head; /* start of the circular buffer */
116};
117
Ben Cahill77626352007-11-29 11:09:44 +0800118/**
Tomas Winklere227cea2008-07-18 13:53:05 +0800119 * struct iwl_lq_sta -- driver's rate scaling private structure
Ben Cahill77626352007-11-29 11:09:44 +0800120 *
121 * Pointer to this gets passed back and forth between driver and mac80211.
122 */
Tomas Winklere227cea2008-07-18 13:53:05 +0800123struct iwl_lq_sta {
Ben Cahill77626352007-11-29 11:09:44 +0800124 u8 active_tbl; /* index of active table, range 0-1 */
125 u8 enable_counter; /* indicates HT mode */
126 u8 stay_in_tbl; /* 1: disallow, 0: allow search for new mode */
127 u8 search_better_tbl; /* 1: currently trying alternate mode */
Zhu Yib481de92007-09-25 17:54:57 -0700128 s32 last_tpt;
Ben Cahill77626352007-11-29 11:09:44 +0800129
130 /* The following determine when to search for a new mode */
Zhu Yib481de92007-09-25 17:54:57 -0700131 u32 table_count_limit;
Ben Cahill77626352007-11-29 11:09:44 +0800132 u32 max_failure_limit; /* # failed frames before new search */
133 u32 max_success_limit; /* # successful frames before new search */
Zhu Yib481de92007-09-25 17:54:57 -0700134 u32 table_count;
Ben Cahill77626352007-11-29 11:09:44 +0800135 u32 total_failed; /* total failed frames, any/all rates */
136 u32 total_success; /* total successful frames, any/all rates */
137 u32 flush_timer; /* time staying in mode before new search */
138
139 u8 action_counter; /* # mode-switch actions tried */
Zhu Yib481de92007-09-25 17:54:57 -0700140 u8 is_green;
141 u8 is_dup;
Johannes Berg8318d782008-01-24 19:38:38 +0100142 enum ieee80211_band band;
Zhu Yib481de92007-09-25 17:54:57 -0700143 u8 ibss_sta_added;
Ben Cahill77626352007-11-29 11:09:44 +0800144
145 /* The following are bitmaps of rates; IWL_RATE_6M_MASK, etc. */
Zhu Yi02dede02007-09-27 11:27:40 +0800146 u32 supp_rates;
Guy Cohen39e88502008-04-23 17:14:57 -0700147 u16 active_legacy_rate;
Zhu Yib481de92007-09-25 17:54:57 -0700148 u16 active_siso_rate;
Guy Cohenfde0db32008-04-21 15:42:01 -0700149 u16 active_mimo2_rate;
150 u16 active_mimo3_rate;
Zhu Yib481de92007-09-25 17:54:57 -0700151 u16 active_rate_basic;
Ben Cahill77626352007-11-29 11:09:44 +0800152
Tomas Winkler66c73db2008-04-15 16:01:40 -0700153 struct iwl_link_quality_cmd lq;
Tomas Winklere227cea2008-07-18 13:53:05 +0800154 struct iwl_scale_tbl_info lq_info[LQ_SIZE]; /* "active", "search" */
155 struct iwl_traffic_load load[TID_MAX_LOAD_COUNT];
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200156 u8 tx_agg_tid_en;
Zhu Yi5ae212c2007-09-27 11:27:38 +0800157#ifdef CONFIG_MAC80211_DEBUGFS
Zhu Yi98d7e092007-09-27 11:27:42 +0800158 struct dentry *rs_sta_dbgfs_scale_table_file;
Zhu Yi0209dc12007-09-27 11:27:43 +0800159 struct dentry *rs_sta_dbgfs_stats_table_file;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200160 struct dentry *rs_sta_dbgfs_tx_agg_tid_en_file;
Guy Cohen39e88502008-04-23 17:14:57 -0700161 u32 dbg_fixed_rate;
Zhu Yi5ae212c2007-09-27 11:27:38 +0800162#endif
Zhu Yi62430652008-05-05 10:22:46 +0800163 struct iwl_priv *drv;
Johannes Bergb7e35002008-09-11 02:22:58 +0200164
165 /* used to be in sta_info */
166 int last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -0700167};
168
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700169static void rs_rate_scale_perform(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700170 struct ieee80211_hdr *hdr,
Johannes Berg4b7679a2008-09-18 18:14:18 +0200171 struct ieee80211_sta *sta,
172 struct iwl_lq_sta *lq_sta);
Guy Cohenfde0db32008-04-21 15:42:01 -0700173static void rs_fill_link_cmd(const struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +0800174 struct iwl_lq_sta *lq_sta, u32 rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -0700175
176
Zhu Yi98d7e092007-09-27 11:27:42 +0800177#ifdef CONFIG_MAC80211_DEBUGFS
Tomas Winklere227cea2008-07-18 13:53:05 +0800178static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
179 u32 *rate_n_flags, int index);
Zhu Yi98d7e092007-09-27 11:27:42 +0800180#else
Tomas Winklere227cea2008-07-18 13:53:05 +0800181static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
182 u32 *rate_n_flags, int index)
Zhu Yi98d7e092007-09-27 11:27:42 +0800183{}
184#endif
Ben Cahill77626352007-11-29 11:09:44 +0800185
186/*
187 * Expected throughput metrics for following rates:
188 * 1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
189 * "G" is the only table that supports CCK (the first 4 rates).
190 */
Tomas Winklera96a27f2008-10-23 23:48:56 -0700191/*FIXME:RS:need to separate tables for MIMO2/MIMO3*/
Zhu Yib481de92007-09-25 17:54:57 -0700192static s32 expected_tpt_A[IWL_RATE_COUNT] = {
193 0, 0, 0, 0, 40, 57, 72, 98, 121, 154, 177, 186, 186
194};
195
196static s32 expected_tpt_G[IWL_RATE_COUNT] = {
197 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 186
198};
199
200static s32 expected_tpt_siso20MHz[IWL_RATE_COUNT] = {
201 0, 0, 0, 0, 42, 42, 76, 102, 124, 159, 183, 193, 202
202};
203
204static s32 expected_tpt_siso20MHzSGI[IWL_RATE_COUNT] = {
205 0, 0, 0, 0, 46, 46, 82, 110, 132, 168, 192, 202, 211
206};
207
208static s32 expected_tpt_mimo20MHz[IWL_RATE_COUNT] = {
209 0, 0, 0, 0, 74, 74, 123, 155, 179, 214, 236, 244, 251
210};
211
212static s32 expected_tpt_mimo20MHzSGI[IWL_RATE_COUNT] = {
213 0, 0, 0, 0, 81, 81, 131, 164, 188, 222, 243, 251, 257
214};
215
216static s32 expected_tpt_siso40MHz[IWL_RATE_COUNT] = {
217 0, 0, 0, 0, 77, 77, 127, 160, 184, 220, 242, 250, 257
218};
219
220static s32 expected_tpt_siso40MHzSGI[IWL_RATE_COUNT] = {
221 0, 0, 0, 0, 83, 83, 135, 169, 193, 229, 250, 257, 264
222};
223
224static s32 expected_tpt_mimo40MHz[IWL_RATE_COUNT] = {
225 0, 0, 0, 0, 123, 123, 182, 214, 235, 264, 279, 285, 289
226};
227
228static s32 expected_tpt_mimo40MHzSGI[IWL_RATE_COUNT] = {
229 0, 0, 0, 0, 131, 131, 191, 222, 242, 270, 284, 289, 293
230};
231
Guy Cohen39e88502008-04-23 17:14:57 -0700232static inline u8 rs_extract_rate(u32 rate_n_flags)
Zhu Yib481de92007-09-25 17:54:57 -0700233{
234 return (u8)(rate_n_flags & 0xFF);
235}
236
Tomas Winklere227cea2008-07-18 13:53:05 +0800237static void rs_rate_scale_clear_window(struct iwl_rate_scale_data *window)
Zhu Yib481de92007-09-25 17:54:57 -0700238{
239 window->data = 0;
240 window->success_counter = 0;
241 window->success_ratio = IWL_INVALID_VALUE;
242 window->counter = 0;
243 window->average_tpt = IWL_INVALID_VALUE;
244 window->stamp = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700245}
246
Guy Cohenaade00c2008-04-23 17:14:59 -0700247static inline u8 rs_is_valid_ant(u8 valid_antenna, u8 ant_type)
248{
Tomas Winkler3ac7f142008-07-21 02:40:14 +0300249 return (ant_type & valid_antenna) == ant_type;
Guy Cohenaade00c2008-04-23 17:14:59 -0700250}
251
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200252/*
253 * removes the old data from the statistics. All data that is older than
254 * TID_MAX_TIME_DIFF, will be deleted.
255 */
Tomas Winklere227cea2008-07-18 13:53:05 +0800256static void rs_tl_rm_old_stats(struct iwl_traffic_load *tl, u32 curr_time)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200257{
258 /* The oldest age we want to keep */
259 u32 oldest_time = curr_time - TID_MAX_TIME_DIFF;
260
261 while (tl->queue_count &&
262 (tl->time_stamp < oldest_time)) {
263 tl->total -= tl->packet_count[tl->head];
264 tl->packet_count[tl->head] = 0;
265 tl->time_stamp += TID_QUEUE_CELL_SPACING;
266 tl->queue_count--;
267 tl->head++;
268 if (tl->head >= TID_QUEUE_MAX_SIZE)
269 tl->head = 0;
270 }
271}
272
273/*
274 * increment traffic load value for tid and also remove
275 * any old values if passed the certain time period
276 */
Tomas Winklere227cea2008-07-18 13:53:05 +0800277static u8 rs_tl_add_packet(struct iwl_lq_sta *lq_data,
Ron Rindjunskyfaa29712008-06-12 09:46:58 +0800278 struct ieee80211_hdr *hdr)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200279{
280 u32 curr_time = jiffies_to_msecs(jiffies);
281 u32 time_diff;
282 s32 index;
Tomas Winklere227cea2008-07-18 13:53:05 +0800283 struct iwl_traffic_load *tl = NULL;
Tomas Winkler54dbb522008-05-15 13:54:06 +0800284 u8 tid;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200285
Tomas Winkler417f1142008-11-12 13:14:06 -0800286 if (ieee80211_is_data_qos(hdr->frame_control)) {
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700287 u8 *qc = ieee80211_get_qos_ctl(hdr);
Tomas Winkler54dbb522008-05-15 13:54:06 +0800288 tid = qc[0] & 0xf;
289 } else
Ron Rindjunskyfaa29712008-06-12 09:46:58 +0800290 return MAX_TID_COUNT;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200291
292 tl = &lq_data->load[tid];
293
294 curr_time -= curr_time % TID_ROUND_VALUE;
295
296 /* Happens only for the first packet. Initialize the data */
297 if (!(tl->queue_count)) {
298 tl->total = 1;
299 tl->time_stamp = curr_time;
300 tl->queue_count = 1;
301 tl->head = 0;
302 tl->packet_count[0] = 1;
Ron Rindjunskyfaa29712008-06-12 09:46:58 +0800303 return MAX_TID_COUNT;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200304 }
305
306 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
307 index = time_diff / TID_QUEUE_CELL_SPACING;
308
309 /* The history is too long: remove data that is older than */
310 /* TID_MAX_TIME_DIFF */
311 if (index >= TID_QUEUE_MAX_SIZE)
312 rs_tl_rm_old_stats(tl, curr_time);
313
314 index = (tl->head + index) % TID_QUEUE_MAX_SIZE;
315 tl->packet_count[index] = tl->packet_count[index] + 1;
316 tl->total = tl->total + 1;
317
318 if ((index + 1) > tl->queue_count)
319 tl->queue_count = index + 1;
Ron Rindjunskyfaa29712008-06-12 09:46:58 +0800320
321 return tid;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200322}
323
324/*
325 get the traffic load value for tid
326*/
Tomas Winklere227cea2008-07-18 13:53:05 +0800327static u32 rs_tl_get_load(struct iwl_lq_sta *lq_data, u8 tid)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200328{
329 u32 curr_time = jiffies_to_msecs(jiffies);
330 u32 time_diff;
331 s32 index;
Tomas Winklere227cea2008-07-18 13:53:05 +0800332 struct iwl_traffic_load *tl = NULL;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200333
334 if (tid >= TID_MAX_LOAD_COUNT)
335 return 0;
336
337 tl = &(lq_data->load[tid]);
338
339 curr_time -= curr_time % TID_ROUND_VALUE;
340
341 if (!(tl->queue_count))
342 return 0;
343
344 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
345 index = time_diff / TID_QUEUE_CELL_SPACING;
346
347 /* The history is too long: remove data that is older than */
348 /* TID_MAX_TIME_DIFF */
349 if (index >= TID_QUEUE_MAX_SIZE)
350 rs_tl_rm_old_stats(tl, curr_time);
351
352 return tl->total;
353}
354
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700355static void rs_tl_turn_on_agg_for_tid(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +0800356 struct iwl_lq_sta *lq_data, u8 tid,
Johannes Berg4b7679a2008-09-18 18:14:18 +0200357 struct ieee80211_sta *sta)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200358{
Johannes Bergff550cb2008-09-11 03:17:05 +0200359 if (rs_tl_get_load(lq_data, tid) > IWL_AGG_LOAD_THRESHOLD) {
Johannes Berge1749612008-10-27 15:59:26 -0700360 IWL_DEBUG_HT("Starting Tx agg: STA: %pM tid: %d\n",
361 sta->addr, tid);
Johannes Berg4b7679a2008-09-18 18:14:18 +0200362 ieee80211_start_tx_ba_session(priv->hw, sta->addr, tid);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200363 }
364}
365
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700366static void rs_tl_turn_on_agg(struct iwl_priv *priv, u8 tid,
Tomas Winklere227cea2008-07-18 13:53:05 +0800367 struct iwl_lq_sta *lq_data,
Johannes Berg4b7679a2008-09-18 18:14:18 +0200368 struct ieee80211_sta *sta)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200369{
370 if ((tid < TID_MAX_LOAD_COUNT))
371 rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
372 else if (tid == IWL_AGG_ALL_TID)
373 for (tid = 0; tid < TID_MAX_LOAD_COUNT; tid++)
374 rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
375}
376
Guy Cohen39e88502008-04-23 17:14:57 -0700377static inline int get_num_of_ant_from_rate(u32 rate_n_flags)
Guy Cohenfde0db32008-04-21 15:42:01 -0700378{
Tomas Winkler3ac7f142008-07-21 02:40:14 +0300379 return !!(rate_n_flags & RATE_MCS_ANT_A_MSK) +
380 !!(rate_n_flags & RATE_MCS_ANT_B_MSK) +
381 !!(rate_n_flags & RATE_MCS_ANT_C_MSK);
Guy Cohenfde0db32008-04-21 15:42:01 -0700382}
383
Ben Cahill77626352007-11-29 11:09:44 +0800384/**
385 * rs_collect_tx_data - Update the success/failure sliding window
386 *
387 * We keep a sliding window of the last 62 packets transmitted
388 * at this rate. window->data contains the bitmask of successful
389 * packets.
390 */
Tomas Winklere227cea2008-07-18 13:53:05 +0800391static int rs_collect_tx_data(struct iwl_rate_scale_data *windows,
Ron Rindjunsky99556432008-01-28 14:07:25 +0200392 int scale_index, s32 tpt, int retries,
393 int successes)
Zhu Yib481de92007-09-25 17:54:57 -0700394{
Tomas Winklere227cea2008-07-18 13:53:05 +0800395 struct iwl_rate_scale_data *window = NULL;
Guy Cohen39e88502008-04-23 17:14:57 -0700396 static const u64 mask = (((u64)1) << (IWL_RATE_MAX_WINDOW - 1));
Zhu Yib481de92007-09-25 17:54:57 -0700397 s32 fail_count;
398
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800399 if (scale_index < 0 || scale_index >= IWL_RATE_COUNT)
400 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -0700401
Ben Cahill77626352007-11-29 11:09:44 +0800402 /* Select data for current tx bit rate */
Zhu Yib481de92007-09-25 17:54:57 -0700403 window = &(windows[scale_index]);
404
Ben Cahill77626352007-11-29 11:09:44 +0800405 /*
406 * Keep track of only the latest 62 tx frame attempts in this rate's
407 * history window; anything older isn't really relevant any more.
408 * If we have filled up the sliding window, drop the oldest attempt;
409 * if the oldest attempt (highest bit in bitmap) shows "success",
410 * subtract "1" from the success counter (this is the main reason
411 * we keep these bitmaps!).
412 */
Ron Rindjunsky99556432008-01-28 14:07:25 +0200413 while (retries > 0) {
Guy Cohen39e88502008-04-23 17:14:57 -0700414 if (window->counter >= IWL_RATE_MAX_WINDOW) {
415
416 /* remove earliest */
417 window->counter = IWL_RATE_MAX_WINDOW - 1;
418
Ron Rindjunsky99556432008-01-28 14:07:25 +0200419 if (window->data & mask) {
420 window->data &= ~mask;
Guy Cohen39e88502008-04-23 17:14:57 -0700421 window->success_counter--;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200422 }
Zhu Yib481de92007-09-25 17:54:57 -0700423 }
Zhu Yib481de92007-09-25 17:54:57 -0700424
Ron Rindjunsky99556432008-01-28 14:07:25 +0200425 /* Increment frames-attempted counter */
426 window->counter++;
Ben Cahill77626352007-11-29 11:09:44 +0800427
Ron Rindjunsky99556432008-01-28 14:07:25 +0200428 /* Shift bitmap by one frame (throw away oldest history),
429 * OR in "1", and increment "success" if this
430 * frame was successful. */
Guy Cohen90d77952008-09-09 10:54:53 +0800431 window->data <<= 1;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200432 if (successes > 0) {
Guy Cohen39e88502008-04-23 17:14:57 -0700433 window->success_counter++;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200434 window->data |= 0x1;
435 successes--;
436 }
437
438 retries--;
Zhu Yib481de92007-09-25 17:54:57 -0700439 }
440
Ben Cahill77626352007-11-29 11:09:44 +0800441 /* Calculate current success ratio, avoid divide-by-0! */
Zhu Yib481de92007-09-25 17:54:57 -0700442 if (window->counter > 0)
443 window->success_ratio = 128 * (100 * window->success_counter)
444 / window->counter;
445 else
446 window->success_ratio = IWL_INVALID_VALUE;
447
448 fail_count = window->counter - window->success_counter;
449
Ben Cahill77626352007-11-29 11:09:44 +0800450 /* Calculate average throughput, if we have enough history. */
Zhu Yib481de92007-09-25 17:54:57 -0700451 if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
452 (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
453 window->average_tpt = (window->success_ratio * tpt + 64) / 128;
454 else
455 window->average_tpt = IWL_INVALID_VALUE;
456
Ben Cahill77626352007-11-29 11:09:44 +0800457 /* Tag this window as having been updated */
Zhu Yib481de92007-09-25 17:54:57 -0700458 window->stamp = jiffies;
459
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800460 return 0;
Zhu Yib481de92007-09-25 17:54:57 -0700461}
462
Ben Cahill77626352007-11-29 11:09:44 +0800463/*
464 * Fill uCode API rate_n_flags field, based on "search" or "active" table.
465 */
Guy Cohen39e88502008-04-23 17:14:57 -0700466/* FIXME:RS:remove this function and put the flags statically in the table */
Tomas Winklere227cea2008-07-18 13:53:05 +0800467static u32 rate_n_flags_from_tbl(struct iwl_scale_tbl_info *tbl,
Guy Cohen39e88502008-04-23 17:14:57 -0700468 int index, u8 use_green)
Zhu Yib481de92007-09-25 17:54:57 -0700469{
Guy Cohen39e88502008-04-23 17:14:57 -0700470 u32 rate_n_flags = 0;
471
Zhu Yib481de92007-09-25 17:54:57 -0700472 if (is_legacy(tbl->lq_type)) {
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800473 rate_n_flags = iwl_rates[index].plcp;
Zhu Yib481de92007-09-25 17:54:57 -0700474 if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
Guy Cohen39e88502008-04-23 17:14:57 -0700475 rate_n_flags |= RATE_MCS_CCK_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700476
Guy Cohenfde0db32008-04-21 15:42:01 -0700477 } else if (is_Ht(tbl->lq_type)) {
478 if (index > IWL_LAST_OFDM_RATE) {
479 IWL_ERROR("invalid HT rate index %d\n", index);
Zhu Yib481de92007-09-25 17:54:57 -0700480 index = IWL_LAST_OFDM_RATE;
Guy Cohenfde0db32008-04-21 15:42:01 -0700481 }
Guy Cohen39e88502008-04-23 17:14:57 -0700482 rate_n_flags = RATE_MCS_HT_MSK;
Guy Cohenfde0db32008-04-21 15:42:01 -0700483
484 if (is_siso(tbl->lq_type))
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800485 rate_n_flags |= iwl_rates[index].plcp_siso;
Guy Cohenfde0db32008-04-21 15:42:01 -0700486 else if (is_mimo2(tbl->lq_type))
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800487 rate_n_flags |= iwl_rates[index].plcp_mimo2;
Guy Cohenfde0db32008-04-21 15:42:01 -0700488 else
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800489 rate_n_flags |= iwl_rates[index].plcp_mimo3;
Zhu Yib481de92007-09-25 17:54:57 -0700490 } else {
Guy Cohenfde0db32008-04-21 15:42:01 -0700491 IWL_ERROR("Invalid tbl->lq_type %d\n", tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -0700492 }
493
Guy Cohen39e88502008-04-23 17:14:57 -0700494 rate_n_flags |= ((tbl->ant_type << RATE_MCS_ANT_POS) &
Guy Cohenfde0db32008-04-21 15:42:01 -0700495 RATE_MCS_ANT_ABC_MSK);
Zhu Yib481de92007-09-25 17:54:57 -0700496
Guy Cohen39e88502008-04-23 17:14:57 -0700497 if (is_Ht(tbl->lq_type)) {
498 if (tbl->is_fat) {
499 if (tbl->is_dup)
500 rate_n_flags |= RATE_MCS_DUP_MSK;
501 else
502 rate_n_flags |= RATE_MCS_FAT_MSK;
503 }
504 if (tbl->is_SGI)
505 rate_n_flags |= RATE_MCS_SGI_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700506
Guy Cohen39e88502008-04-23 17:14:57 -0700507 if (use_green) {
508 rate_n_flags |= RATE_MCS_GF_MSK;
509 if (is_siso(tbl->lq_type) && tbl->is_SGI) {
510 rate_n_flags &= ~RATE_MCS_SGI_MSK;
511 IWL_ERROR("GF was set with SGI:SISO\n");
512 }
513 }
Zhu Yib481de92007-09-25 17:54:57 -0700514 }
Guy Cohen39e88502008-04-23 17:14:57 -0700515 return rate_n_flags;
Zhu Yib481de92007-09-25 17:54:57 -0700516}
517
Ben Cahill77626352007-11-29 11:09:44 +0800518/*
519 * Interpret uCode API's rate_n_flags format,
520 * fill "search" or "active" tx mode table.
521 */
Guy Cohen39e88502008-04-23 17:14:57 -0700522static int rs_get_tbl_info_from_mcs(const u32 rate_n_flags,
Johannes Berg8318d782008-01-24 19:38:38 +0100523 enum ieee80211_band band,
Tomas Winklere227cea2008-07-18 13:53:05 +0800524 struct iwl_scale_tbl_info *tbl,
Zhu Yib481de92007-09-25 17:54:57 -0700525 int *rate_idx)
526{
Guy Cohen39e88502008-04-23 17:14:57 -0700527 u32 ant_msk = (rate_n_flags & RATE_MCS_ANT_ABC_MSK);
528 u8 num_of_ant = get_num_of_ant_from_rate(rate_n_flags);
529 u8 mcs;
Zhu Yib481de92007-09-25 17:54:57 -0700530
Tomas Winklere7d326a2008-06-12 09:47:11 +0800531 *rate_idx = iwl_hwrate_to_plcp_idx(rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -0700532
Guy Cohenfde0db32008-04-21 15:42:01 -0700533 if (*rate_idx == IWL_RATE_INVALID) {
Zhu Yib481de92007-09-25 17:54:57 -0700534 *rate_idx = -1;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800535 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -0700536 }
Ben Cahill77626352007-11-29 11:09:44 +0800537 tbl->is_SGI = 0; /* default legacy setup */
Zhu Yib481de92007-09-25 17:54:57 -0700538 tbl->is_fat = 0;
539 tbl->is_dup = 0;
Guy Cohenfde0db32008-04-21 15:42:01 -0700540 tbl->ant_type = (ant_msk >> RATE_MCS_ANT_POS);
541 tbl->lq_type = LQ_NONE;
Zhu Yib481de92007-09-25 17:54:57 -0700542
Ben Cahill77626352007-11-29 11:09:44 +0800543 /* legacy rate format */
Guy Cohen39e88502008-04-23 17:14:57 -0700544 if (!(rate_n_flags & RATE_MCS_HT_MSK)) {
Guy Cohenfde0db32008-04-21 15:42:01 -0700545 if (num_of_ant == 1) {
Johannes Berg8318d782008-01-24 19:38:38 +0100546 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700547 tbl->lq_type = LQ_A;
548 else
549 tbl->lq_type = LQ_G;
Zhu Yib481de92007-09-25 17:54:57 -0700550 }
Guy Cohenfde0db32008-04-21 15:42:01 -0700551 /* HT rate format */
Zhu Yib481de92007-09-25 17:54:57 -0700552 } else {
Guy Cohen39e88502008-04-23 17:14:57 -0700553 if (rate_n_flags & RATE_MCS_SGI_MSK)
Zhu Yib481de92007-09-25 17:54:57 -0700554 tbl->is_SGI = 1;
555
Guy Cohen39e88502008-04-23 17:14:57 -0700556 if ((rate_n_flags & RATE_MCS_FAT_MSK) ||
557 (rate_n_flags & RATE_MCS_DUP_MSK))
Zhu Yib481de92007-09-25 17:54:57 -0700558 tbl->is_fat = 1;
559
Guy Cohen39e88502008-04-23 17:14:57 -0700560 if (rate_n_flags & RATE_MCS_DUP_MSK)
Zhu Yib481de92007-09-25 17:54:57 -0700561 tbl->is_dup = 1;
Guy Cohenfde0db32008-04-21 15:42:01 -0700562
Guy Cohen39e88502008-04-23 17:14:57 -0700563 mcs = rs_extract_rate(rate_n_flags);
Guy Cohenfde0db32008-04-21 15:42:01 -0700564
Guy Cohen39e88502008-04-23 17:14:57 -0700565 /* SISO */
566 if (mcs <= IWL_RATE_SISO_60M_PLCP) {
Guy Cohenfde0db32008-04-21 15:42:01 -0700567 if (num_of_ant == 1)
568 tbl->lq_type = LQ_SISO; /*else NONE*/
569 /* MIMO2 */
Guy Cohen39e88502008-04-23 17:14:57 -0700570 } else if (mcs <= IWL_RATE_MIMO2_60M_PLCP) {
Guy Cohenfde0db32008-04-21 15:42:01 -0700571 if (num_of_ant == 2)
572 tbl->lq_type = LQ_MIMO2;
573 /* MIMO3 */
574 } else {
575 if (num_of_ant == 3)
576 tbl->lq_type = LQ_MIMO3;
577 }
Zhu Yib481de92007-09-25 17:54:57 -0700578 }
579 return 0;
580}
Guy Cohenaade00c2008-04-23 17:14:59 -0700581
582/* switch to another antenna/antennas and return 1 */
583/* if no other valid antenna found, return 0 */
584static int rs_toggle_antenna(u32 valid_ant, u32 *rate_n_flags,
Tomas Winklere227cea2008-07-18 13:53:05 +0800585 struct iwl_scale_tbl_info *tbl)
Zhu Yib481de92007-09-25 17:54:57 -0700586{
Guy Cohenaade00c2008-04-23 17:14:59 -0700587 u8 new_ant_type;
588
589 if (!tbl->ant_type || tbl->ant_type > ANT_ABC)
590 return 0;
591
592 if (!rs_is_valid_ant(valid_ant, tbl->ant_type))
593 return 0;
594
595 new_ant_type = ant_toggle_lookup[tbl->ant_type];
596
597 while ((new_ant_type != tbl->ant_type) &&
598 !rs_is_valid_ant(valid_ant, new_ant_type))
599 new_ant_type = ant_toggle_lookup[new_ant_type];
600
601 if (new_ant_type == tbl->ant_type)
602 return 0;
603
604 tbl->ant_type = new_ant_type;
605 *rate_n_flags &= ~RATE_MCS_ANT_ABC_MSK;
606 *rate_n_flags |= new_ant_type << RATE_MCS_ANT_POS;
607 return 1;
Zhu Yib481de92007-09-25 17:54:57 -0700608}
609
Guy Cohen39e88502008-04-23 17:14:57 -0700610/* FIXME:RS: in 4965 we don't use greenfield at all */
Guy Cohenf935a6d2008-04-23 17:15:02 -0700611/* FIXME:RS: don't use greenfield for now in TX */
Guy Cohenf935a6d2008-04-23 17:15:02 -0700612#if 0
613static inline u8 rs_use_green(struct iwl_priv *priv, struct ieee80211_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -0700614{
Tomas Winkler3ac7f142008-07-21 02:40:14 +0300615 return (conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) &&
Ron Rindjunsky270243a2007-11-26 16:14:41 +0200616 priv->current_ht_config.is_green_field &&
Tomas Winkler3ac7f142008-07-21 02:40:14 +0300617 !priv->current_ht_config.non_GF_STA_present;
Zhu Yib481de92007-09-25 17:54:57 -0700618}
Ron Rindjunsky4f85f5b2008-06-09 22:54:35 +0300619#endif
Guy Cohenf935a6d2008-04-23 17:15:02 -0700620static inline u8 rs_use_green(struct iwl_priv *priv, struct ieee80211_conf *conf)
621{
622 return 0;
623}
Zhu Yib481de92007-09-25 17:54:57 -0700624
625/**
626 * rs_get_supported_rates - get the available rates
627 *
628 * if management frame or broadcast frame only return
629 * basic available rates.
630 *
631 */
Tomas Winklere227cea2008-07-18 13:53:05 +0800632static u16 rs_get_supported_rates(struct iwl_lq_sta *lq_sta,
633 struct ieee80211_hdr *hdr,
634 enum iwl_table_type rate_type)
Zhu Yib481de92007-09-25 17:54:57 -0700635{
Zhu Yib481de92007-09-25 17:54:57 -0700636 if (hdr && is_multicast_ether_addr(hdr->addr1) &&
Guy Coheneecd6e52008-04-23 17:14:58 -0700637 lq_sta->active_rate_basic)
638 return lq_sta->active_rate_basic;
639
640 if (is_legacy(rate_type)) {
641 return lq_sta->active_legacy_rate;
642 } else {
643 if (is_siso(rate_type))
644 return lq_sta->active_siso_rate;
645 else if (is_mimo2(rate_type))
646 return lq_sta->active_mimo2_rate;
647 else
648 return lq_sta->active_mimo3_rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -0800649 }
Zhu Yib481de92007-09-25 17:54:57 -0700650}
651
Ester Kummerbf403db2008-05-05 10:22:40 +0800652static u16 rs_get_adjacent_rate(struct iwl_priv *priv, u8 index, u16 rate_mask,
653 int rate_type)
Zhu Yib481de92007-09-25 17:54:57 -0700654{
655 u8 high = IWL_RATE_INVALID;
656 u8 low = IWL_RATE_INVALID;
657
Ian Schram01ebd062007-10-25 17:15:22 +0800658 /* 802.11A or ht walks to the next literal adjacent rate in
Zhu Yib481de92007-09-25 17:54:57 -0700659 * the rate table */
660 if (is_a_band(rate_type) || !is_legacy(rate_type)) {
661 int i;
662 u32 mask;
663
664 /* Find the previous rate that is in the rate mask */
665 i = index - 1;
666 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
667 if (rate_mask & mask) {
668 low = i;
669 break;
670 }
671 }
672
673 /* Find the next rate that is in the rate mask */
674 i = index + 1;
675 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
676 if (rate_mask & mask) {
677 high = i;
678 break;
679 }
680 }
681
682 return (high << 8) | low;
683 }
684
685 low = index;
686 while (low != IWL_RATE_INVALID) {
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800687 low = iwl_rates[low].prev_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700688 if (low == IWL_RATE_INVALID)
689 break;
690 if (rate_mask & (1 << low))
691 break;
692 IWL_DEBUG_RATE("Skipping masked lower rate: %d\n", low);
693 }
694
695 high = index;
696 while (high != IWL_RATE_INVALID) {
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800697 high = iwl_rates[high].next_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700698 if (high == IWL_RATE_INVALID)
699 break;
700 if (rate_mask & (1 << high))
701 break;
702 IWL_DEBUG_RATE("Skipping masked higher rate: %d\n", high);
703 }
704
705 return (high << 8) | low;
706}
707
Tomas Winklere227cea2008-07-18 13:53:05 +0800708static u32 rs_get_lower_rate(struct iwl_lq_sta *lq_sta,
709 struct iwl_scale_tbl_info *tbl,
710 u8 scale_index, u8 ht_possible)
Zhu Yib481de92007-09-25 17:54:57 -0700711{
Zhu Yib481de92007-09-25 17:54:57 -0700712 s32 low;
713 u16 rate_mask;
714 u16 high_low;
715 u8 switch_to_legacy = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -0800716 u8 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -0700717
718 /* check if we need to switch from HT to legacy rates.
719 * assumption is that mandatory rates (1Mbps or 6Mbps)
720 * are always supported (spec demand) */
721 if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_index)) {
722 switch_to_legacy = 1;
723 scale_index = rs_ht_to_legacy[scale_index];
Johannes Berg8318d782008-01-24 19:38:38 +0100724 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700725 tbl->lq_type = LQ_A;
726 else
727 tbl->lq_type = LQ_G;
728
Guy Cohen69fdb302008-04-23 17:15:01 -0700729 if (num_of_ant(tbl->ant_type) > 1)
Guy Cohenfde0db32008-04-21 15:42:01 -0700730 tbl->ant_type = ANT_A;/*FIXME:RS*/
Zhu Yib481de92007-09-25 17:54:57 -0700731
732 tbl->is_fat = 0;
733 tbl->is_SGI = 0;
734 }
735
Guy Coheneecd6e52008-04-23 17:14:58 -0700736 rate_mask = rs_get_supported_rates(lq_sta, NULL, tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -0700737
Ben Cahill77626352007-11-29 11:09:44 +0800738 /* Mask with station rate restriction */
Zhu Yib481de92007-09-25 17:54:57 -0700739 if (is_legacy(tbl->lq_type)) {
Ben Cahill77626352007-11-29 11:09:44 +0800740 /* supp_rates has no CCK bits in A mode */
Johannes Berg8318d782008-01-24 19:38:38 +0100741 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700742 rate_mask = (u16)(rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -0800743 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
Zhu Yib481de92007-09-25 17:54:57 -0700744 else
Tomas Winklerc33104f2008-01-14 17:46:21 -0800745 rate_mask = (u16)(rate_mask & lq_sta->supp_rates);
Zhu Yib481de92007-09-25 17:54:57 -0700746 }
747
Ben Cahill77626352007-11-29 11:09:44 +0800748 /* If we switched from HT to legacy, check current rate */
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800749 if (switch_to_legacy && (rate_mask & (1 << scale_index))) {
Guy Cohen39e88502008-04-23 17:14:57 -0700750 low = scale_index;
751 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700752 }
753
Ester Kummerbf403db2008-05-05 10:22:40 +0800754 high_low = rs_get_adjacent_rate(lq_sta->drv, scale_index, rate_mask,
755 tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -0700756 low = high_low & 0xff;
757
Guy Cohen39e88502008-04-23 17:14:57 -0700758 if (low == IWL_RATE_INVALID)
759 low = scale_index;
760
761out:
762 return rate_n_flags_from_tbl(tbl, low, is_green);
Zhu Yib481de92007-09-25 17:54:57 -0700763}
764
Ben Cahill77626352007-11-29 11:09:44 +0800765/*
766 * mac80211 sends us Tx status
767 */
Johannes Berg4b7679a2008-09-18 18:14:18 +0200768static void rs_tx_status(void *priv_r, struct ieee80211_supported_band *sband,
769 struct ieee80211_sta *sta, void *priv_sta,
Johannes Berge039fa42008-05-15 12:55:29 +0200770 struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -0700771{
772 int status;
773 u8 retries;
774 int rs_index, index = 0;
Tomas Winkler417f1142008-11-12 13:14:06 -0800775 struct iwl_lq_sta *lq_sta = priv_sta;
Tomas Winkler66c73db2008-04-15 16:01:40 -0700776 struct iwl_link_quality_cmd *table;
Zhu Yib481de92007-09-25 17:54:57 -0700777 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berg4b7679a2008-09-18 18:14:18 +0200778 struct iwl_priv *priv = (struct iwl_priv *)priv_r;
779 struct ieee80211_hw *hw = priv->hw;
Johannes Berge039fa42008-05-15 12:55:29 +0200780 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Tomas Winklere227cea2008-07-18 13:53:05 +0800781 struct iwl_rate_scale_data *window = NULL;
782 struct iwl_rate_scale_data *search_win = NULL;
Guy Cohen39e88502008-04-23 17:14:57 -0700783 u32 tx_rate;
Tomas Winklere227cea2008-07-18 13:53:05 +0800784 struct iwl_scale_tbl_info tbl_type;
785 struct iwl_scale_tbl_info *curr_tbl, *search_tbl;
Zhu Yib481de92007-09-25 17:54:57 -0700786 u8 active_index = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700787 s32 tpt = 0;
788
Zhu Yi58826352007-09-27 11:27:39 +0800789 IWL_DEBUG_RATE_LIMIT("get frame ack response, update rate scale window\n");
Zhu Yib481de92007-09-25 17:54:57 -0700790
Tomas Winkler417f1142008-11-12 13:14:06 -0800791 if (!ieee80211_is_data(hdr->frame_control) ||
792 is_multicast_ether_addr(hdr->addr1))
Zhu Yib481de92007-09-25 17:54:57 -0700793 return;
794
Ron Rindjunsky99556432008-01-28 14:07:25 +0200795 /* This packet was aggregated but doesn't carry rate scale info */
Johannes Berge039fa42008-05-15 12:55:29 +0200796 if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
797 !(info->flags & IEEE80211_TX_STAT_AMPDU))
Ron Rindjunsky99556432008-01-28 14:07:25 +0200798 return;
799
Johannes Berge6a98542008-10-21 12:40:02 +0200800 retries = info->status.rates[0].count - 1;
Zhu Yib481de92007-09-25 17:54:57 -0700801
802 if (retries > 15)
803 retries = 15;
804
Johannes Berg05c914f2008-09-11 00:01:58 +0200805 if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) &&
Tomas Winklerc33104f2008-01-14 17:46:21 -0800806 !lq_sta->ibss_sta_added)
Tomas Winklerf4d60822008-03-05 11:31:00 -0800807 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700808
Tomas Winklerc33104f2008-01-14 17:46:21 -0800809 table = &lq_sta->lq;
810 active_index = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -0700811
Tomas Winklerc33104f2008-01-14 17:46:21 -0800812 curr_tbl = &(lq_sta->lq_info[active_index]);
813 search_tbl = &(lq_sta->lq_info[(1 - active_index)]);
Tomas Winklere227cea2008-07-18 13:53:05 +0800814 window = (struct iwl_rate_scale_data *)&(curr_tbl->win[0]);
815 search_win = (struct iwl_rate_scale_data *)&(search_tbl->win[0]);
Zhu Yib481de92007-09-25 17:54:57 -0700816
Ben Cahill77626352007-11-29 11:09:44 +0800817 /*
818 * Ignore this Tx frame response if its initial rate doesn't match
819 * that of latest Link Quality command. There may be stragglers
820 * from a previous Link Quality command, but we're no longer interested
821 * in those; they're either from the "active" mode while we're trying
822 * to check "search" mode, or a prior "search" mode after we've moved
823 * to a new "search" mode (which might become the new "active" mode).
824 */
Guy Cohen39e88502008-04-23 17:14:57 -0700825 tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
826 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800827 if (priv->band == IEEE80211_BAND_5GHZ)
828 rs_index -= IWL_FIRST_OFDM_RATE;
829
Johannes Berge6a98542008-10-21 12:40:02 +0200830 if ((info->status.rates[0].idx < 0) ||
831 (tbl_type.is_SGI != !!(info->status.rates[0].flags & IEEE80211_TX_RC_SHORT_GI)) ||
832 (tbl_type.is_fat != !!(info->status.rates[0].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)) ||
833 (tbl_type.is_dup != !!(info->status.rates[0].flags & IEEE80211_TX_RC_DUP_DATA)) ||
834 (tbl_type.ant_type != info->antenna_sel_tx) ||
835 (!!(tx_rate & RATE_MCS_HT_MSK) != !!(info->status.rates[0].flags & IEEE80211_TX_RC_MCS)) ||
836 (!!(tx_rate & RATE_MCS_GF_MSK) != !!(info->status.rates[0].flags & IEEE80211_TX_RC_GREEN_FIELD)) ||
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800837 (hw->wiphy->bands[priv->band]->bitrates[rs_index].bitrate !=
Johannes Berge6a98542008-10-21 12:40:02 +0200838 hw->wiphy->bands[info->band]->bitrates[info->status.rates[0].idx].bitrate)) {
Guy Cohen39e88502008-04-23 17:14:57 -0700839 IWL_DEBUG_RATE("initial rate does not match 0x%x\n", tx_rate);
Tomas Winklerf4d60822008-03-05 11:31:00 -0800840 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700841 }
842
Ben Cahill77626352007-11-29 11:09:44 +0800843 /* Update frame history window with "failure" for each Tx retry. */
Zhu Yib481de92007-09-25 17:54:57 -0700844 while (retries) {
Ben Cahill77626352007-11-29 11:09:44 +0800845 /* Look up the rate and other info used for each tx attempt.
846 * Each tx attempt steps one entry deeper in the rate table. */
Guy Cohen39e88502008-04-23 17:14:57 -0700847 tx_rate = le32_to_cpu(table->rs_table[index].rate_n_flags);
848 rs_get_tbl_info_from_mcs(tx_rate, priv->band,
Zhu Yib481de92007-09-25 17:54:57 -0700849 &tbl_type, &rs_index);
850
Ben Cahill77626352007-11-29 11:09:44 +0800851 /* If type matches "search" table,
852 * add failure to "search" history */
Zhu Yib481de92007-09-25 17:54:57 -0700853 if ((tbl_type.lq_type == search_tbl->lq_type) &&
Guy Cohenfde0db32008-04-21 15:42:01 -0700854 (tbl_type.ant_type == search_tbl->ant_type) &&
Zhu Yib481de92007-09-25 17:54:57 -0700855 (tbl_type.is_SGI == search_tbl->is_SGI)) {
856 if (search_tbl->expected_tpt)
857 tpt = search_tbl->expected_tpt[rs_index];
858 else
859 tpt = 0;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200860 rs_collect_tx_data(search_win, rs_index, tpt, 1, 0);
Ben Cahill77626352007-11-29 11:09:44 +0800861
862 /* Else if type matches "current/active" table,
863 * add failure to "current/active" history */
Zhu Yib481de92007-09-25 17:54:57 -0700864 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
Guy Cohenfde0db32008-04-21 15:42:01 -0700865 (tbl_type.ant_type == curr_tbl->ant_type) &&
Zhu Yib481de92007-09-25 17:54:57 -0700866 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
867 if (curr_tbl->expected_tpt)
868 tpt = curr_tbl->expected_tpt[rs_index];
869 else
870 tpt = 0;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200871 rs_collect_tx_data(window, rs_index, tpt, 1, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700872 }
Ben Cahill77626352007-11-29 11:09:44 +0800873
874 /* If not searching for a new mode, increment failed counter
875 * ... this helps determine when to start searching again */
Tomas Winklerc33104f2008-01-14 17:46:21 -0800876 if (lq_sta->stay_in_tbl)
877 lq_sta->total_failed++;
Zhu Yib481de92007-09-25 17:54:57 -0700878 --retries;
879 index++;
880
881 }
882
Ben Cahill77626352007-11-29 11:09:44 +0800883 /*
884 * Find (by rate) the history window to update with final Tx attempt;
885 * if Tx was successful first try, use original rate,
886 * else look up the rate that was, finally, successful.
887 */
Guy Cohen39e88502008-04-23 17:14:57 -0700888 tx_rate = le32_to_cpu(table->rs_table[index].rate_n_flags);
889 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
Zhu Yib481de92007-09-25 17:54:57 -0700890
Ben Cahill77626352007-11-29 11:09:44 +0800891 /* Update frame history window with "success" if Tx got ACKed ... */
Johannes Berge039fa42008-05-15 12:55:29 +0200892 status = !!(info->flags & IEEE80211_TX_STAT_ACK);
Zhu Yib481de92007-09-25 17:54:57 -0700893
Ben Cahill77626352007-11-29 11:09:44 +0800894 /* If type matches "search" table,
895 * add final tx status to "search" history */
Zhu Yib481de92007-09-25 17:54:57 -0700896 if ((tbl_type.lq_type == search_tbl->lq_type) &&
Guy Cohenfde0db32008-04-21 15:42:01 -0700897 (tbl_type.ant_type == search_tbl->ant_type) &&
Zhu Yib481de92007-09-25 17:54:57 -0700898 (tbl_type.is_SGI == search_tbl->is_SGI)) {
899 if (search_tbl->expected_tpt)
900 tpt = search_tbl->expected_tpt[rs_index];
901 else
902 tpt = 0;
Johannes Berge039fa42008-05-15 12:55:29 +0200903 if (info->flags & IEEE80211_TX_CTL_AMPDU)
Ron Rindjunsky99556432008-01-28 14:07:25 +0200904 rs_collect_tx_data(search_win, rs_index, tpt,
Johannes Berge039fa42008-05-15 12:55:29 +0200905 info->status.ampdu_ack_len,
906 info->status.ampdu_ack_map);
Ron Rindjunsky99556432008-01-28 14:07:25 +0200907 else
908 rs_collect_tx_data(search_win, rs_index, tpt,
909 1, status);
Ben Cahill77626352007-11-29 11:09:44 +0800910 /* Else if type matches "current/active" table,
911 * add final tx status to "current/active" history */
Zhu Yib481de92007-09-25 17:54:57 -0700912 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
Guy Cohenfde0db32008-04-21 15:42:01 -0700913 (tbl_type.ant_type == curr_tbl->ant_type) &&
Zhu Yib481de92007-09-25 17:54:57 -0700914 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
915 if (curr_tbl->expected_tpt)
916 tpt = curr_tbl->expected_tpt[rs_index];
917 else
918 tpt = 0;
Johannes Berge039fa42008-05-15 12:55:29 +0200919 if (info->flags & IEEE80211_TX_CTL_AMPDU)
Ron Rindjunsky99556432008-01-28 14:07:25 +0200920 rs_collect_tx_data(window, rs_index, tpt,
Johannes Berge039fa42008-05-15 12:55:29 +0200921 info->status.ampdu_ack_len,
922 info->status.ampdu_ack_map);
Ron Rindjunsky99556432008-01-28 14:07:25 +0200923 else
924 rs_collect_tx_data(window, rs_index, tpt,
925 1, status);
Zhu Yib481de92007-09-25 17:54:57 -0700926 }
927
Ben Cahill77626352007-11-29 11:09:44 +0800928 /* If not searching for new mode, increment success/failed counter
929 * ... these help determine when to start searching again */
Tomas Winklerc33104f2008-01-14 17:46:21 -0800930 if (lq_sta->stay_in_tbl) {
Johannes Berge039fa42008-05-15 12:55:29 +0200931 if (info->flags & IEEE80211_TX_CTL_AMPDU) {
932 lq_sta->total_success += info->status.ampdu_ack_map;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200933 lq_sta->total_failed +=
Johannes Berge039fa42008-05-15 12:55:29 +0200934 (info->status.ampdu_ack_len - info->status.ampdu_ack_map);
Ron Rindjunsky99556432008-01-28 14:07:25 +0200935 } else {
936 if (status)
937 lq_sta->total_success++;
938 else
939 lq_sta->total_failed++;
940 }
Zhu Yib481de92007-09-25 17:54:57 -0700941 }
942
Ben Cahill77626352007-11-29 11:09:44 +0800943 /* See if there's a better rate or modulation mode to try. */
Johannes Berg4b7679a2008-09-18 18:14:18 +0200944 rs_rate_scale_perform(priv, hdr, sta, lq_sta);
Tomas Winklerf4d60822008-03-05 11:31:00 -0800945out:
Zhu Yib481de92007-09-25 17:54:57 -0700946 return;
947}
948
Ben Cahill77626352007-11-29 11:09:44 +0800949/*
950 * Begin a period of staying with a selected modulation mode.
951 * Set "stay_in_tbl" flag to prevent any mode switches.
952 * Set frame tx success limits according to legacy vs. high-throughput,
953 * and reset overall (spanning all rates) tx success history statistics.
954 * These control how long we stay using same modulation mode before
955 * searching for a new mode.
956 */
Ester Kummerbf403db2008-05-05 10:22:40 +0800957static void rs_set_stay_in_table(struct iwl_priv *priv, u8 is_legacy,
Tomas Winklere227cea2008-07-18 13:53:05 +0800958 struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -0700959{
Guy Coheneecd6e52008-04-23 17:14:58 -0700960 IWL_DEBUG_RATE("we are staying in the same table\n");
Tomas Winklerc33104f2008-01-14 17:46:21 -0800961 lq_sta->stay_in_tbl = 1; /* only place this gets set */
Zhu Yib481de92007-09-25 17:54:57 -0700962 if (is_legacy) {
Tomas Winklerc33104f2008-01-14 17:46:21 -0800963 lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT;
964 lq_sta->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
965 lq_sta->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
Zhu Yib481de92007-09-25 17:54:57 -0700966 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -0800967 lq_sta->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
968 lq_sta->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
969 lq_sta->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
Zhu Yib481de92007-09-25 17:54:57 -0700970 }
Tomas Winklerc33104f2008-01-14 17:46:21 -0800971 lq_sta->table_count = 0;
972 lq_sta->total_failed = 0;
973 lq_sta->total_success = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700974}
975
Ben Cahill77626352007-11-29 11:09:44 +0800976/*
977 * Find correct throughput table for given mode of modulation
978 */
Tomas Winklere227cea2008-07-18 13:53:05 +0800979static void rs_set_expected_tpt_table(struct iwl_lq_sta *lq_sta,
980 struct iwl_scale_tbl_info *tbl)
Zhu Yib481de92007-09-25 17:54:57 -0700981{
982 if (is_legacy(tbl->lq_type)) {
983 if (!is_a_band(tbl->lq_type))
984 tbl->expected_tpt = expected_tpt_G;
985 else
986 tbl->expected_tpt = expected_tpt_A;
987 } else if (is_siso(tbl->lq_type)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -0800988 if (tbl->is_fat && !lq_sta->is_dup)
Zhu Yib481de92007-09-25 17:54:57 -0700989 if (tbl->is_SGI)
990 tbl->expected_tpt = expected_tpt_siso40MHzSGI;
991 else
992 tbl->expected_tpt = expected_tpt_siso40MHz;
993 else if (tbl->is_SGI)
994 tbl->expected_tpt = expected_tpt_siso20MHzSGI;
995 else
996 tbl->expected_tpt = expected_tpt_siso20MHz;
997
Guy Cohenfde0db32008-04-21 15:42:01 -0700998 } else if (is_mimo(tbl->lq_type)) { /* FIXME:need to separate mimo2/3 */
Tomas Winklerc33104f2008-01-14 17:46:21 -0800999 if (tbl->is_fat && !lq_sta->is_dup)
Zhu Yib481de92007-09-25 17:54:57 -07001000 if (tbl->is_SGI)
1001 tbl->expected_tpt = expected_tpt_mimo40MHzSGI;
1002 else
1003 tbl->expected_tpt = expected_tpt_mimo40MHz;
1004 else if (tbl->is_SGI)
1005 tbl->expected_tpt = expected_tpt_mimo20MHzSGI;
1006 else
1007 tbl->expected_tpt = expected_tpt_mimo20MHz;
1008 } else
1009 tbl->expected_tpt = expected_tpt_G;
1010}
1011
Ben Cahill77626352007-11-29 11:09:44 +08001012/*
1013 * Find starting rate for new "search" high-throughput mode of modulation.
1014 * Goal is to find lowest expected rate (under perfect conditions) that is
1015 * above the current measured throughput of "active" mode, to give new mode
1016 * a fair chance to prove itself without too many challenges.
1017 *
1018 * This gets called when transitioning to more aggressive modulation
1019 * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
1020 * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
1021 * to decrease to match "active" throughput. When moving from MIMO to SISO,
1022 * bit rate will typically need to increase, but not if performance was bad.
1023 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001024static s32 rs_get_best_rate(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001025 struct iwl_lq_sta *lq_sta,
1026 struct iwl_scale_tbl_info *tbl, /* "search" */
Guy Cohenf935a6d2008-04-23 17:15:02 -07001027 u16 rate_mask, s8 index)
Zhu Yib481de92007-09-25 17:54:57 -07001028{
Ben Cahill77626352007-11-29 11:09:44 +08001029 /* "active" values */
Tomas Winklere227cea2008-07-18 13:53:05 +08001030 struct iwl_scale_tbl_info *active_tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001031 &(lq_sta->lq_info[lq_sta->active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07001032 s32 active_sr = active_tbl->win[index].success_ratio;
Zhu Yib481de92007-09-25 17:54:57 -07001033 s32 active_tpt = active_tbl->expected_tpt[index];
Ben Cahill77626352007-11-29 11:09:44 +08001034
1035 /* expected "search" throughput */
1036 s32 *tpt_tbl = tbl->expected_tpt;
1037
1038 s32 new_rate, high, low, start_hi;
Zhu Yib481de92007-09-25 17:54:57 -07001039 u16 high_low;
Guy Cohenf935a6d2008-04-23 17:15:02 -07001040 s8 rate = index;
Zhu Yib481de92007-09-25 17:54:57 -07001041
1042 new_rate = high = low = start_hi = IWL_RATE_INVALID;
1043
1044 for (; ;) {
Ester Kummerbf403db2008-05-05 10:22:40 +08001045 high_low = rs_get_adjacent_rate(priv, rate, rate_mask,
1046 tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -07001047
1048 low = high_low & 0xff;
1049 high = (high_low >> 8) & 0xff;
1050
Ben Cahill77626352007-11-29 11:09:44 +08001051 /*
1052 * Lower the "search" bit rate, to give new "search" mode
1053 * approximately the same throughput as "active" if:
1054 *
1055 * 1) "Active" mode has been working modestly well (but not
1056 * great), and expected "search" throughput (under perfect
1057 * conditions) at candidate rate is above the actual
1058 * measured "active" throughput (but less than expected
1059 * "active" throughput under perfect conditions).
1060 * OR
1061 * 2) "Active" mode has been working perfectly or very well
1062 * and expected "search" throughput (under perfect
1063 * conditions) at candidate rate is above expected
1064 * "active" throughput (under perfect conditions).
1065 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001066 if ((((100 * tpt_tbl[rate]) > lq_sta->last_tpt) &&
Zhu Yib481de92007-09-25 17:54:57 -07001067 ((active_sr > IWL_RATE_DECREASE_TH) &&
1068 (active_sr <= IWL_RATE_HIGH_TH) &&
1069 (tpt_tbl[rate] <= active_tpt))) ||
1070 ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
1071 (tpt_tbl[rate] > active_tpt))) {
1072
Ben Cahill77626352007-11-29 11:09:44 +08001073 /* (2nd or later pass)
1074 * If we've already tried to raise the rate, and are
1075 * now trying to lower it, use the higher rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001076 if (start_hi != IWL_RATE_INVALID) {
1077 new_rate = start_hi;
1078 break;
1079 }
Ben Cahill77626352007-11-29 11:09:44 +08001080
Zhu Yib481de92007-09-25 17:54:57 -07001081 new_rate = rate;
Ben Cahill77626352007-11-29 11:09:44 +08001082
1083 /* Loop again with lower rate */
Zhu Yib481de92007-09-25 17:54:57 -07001084 if (low != IWL_RATE_INVALID)
1085 rate = low;
Ben Cahill77626352007-11-29 11:09:44 +08001086
1087 /* Lower rate not available, use the original */
Zhu Yib481de92007-09-25 17:54:57 -07001088 else
1089 break;
Ben Cahill77626352007-11-29 11:09:44 +08001090
1091 /* Else try to raise the "search" rate to match "active" */
Zhu Yib481de92007-09-25 17:54:57 -07001092 } else {
Ben Cahill77626352007-11-29 11:09:44 +08001093 /* (2nd or later pass)
1094 * If we've already tried to lower the rate, and are
1095 * now trying to raise it, use the lower rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001096 if (new_rate != IWL_RATE_INVALID)
1097 break;
Ben Cahill77626352007-11-29 11:09:44 +08001098
1099 /* Loop again with higher rate */
Zhu Yib481de92007-09-25 17:54:57 -07001100 else if (high != IWL_RATE_INVALID) {
1101 start_hi = high;
1102 rate = high;
Ben Cahill77626352007-11-29 11:09:44 +08001103
1104 /* Higher rate not available, use the original */
Zhu Yib481de92007-09-25 17:54:57 -07001105 } else {
Guy Cohen90d77952008-09-09 10:54:53 +08001106 new_rate = rate;
Zhu Yib481de92007-09-25 17:54:57 -07001107 break;
1108 }
1109 }
1110 }
1111
1112 return new_rate;
1113}
Zhu Yib481de92007-09-25 17:54:57 -07001114
Ben Cahill77626352007-11-29 11:09:44 +08001115/*
1116 * Set up search table for MIMO
1117 */
Guy Cohenfde0db32008-04-21 15:42:01 -07001118static int rs_switch_to_mimo2(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001119 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001120 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001121 struct ieee80211_sta *sta,
Tomas Winklere227cea2008-07-18 13:53:05 +08001122 struct iwl_scale_tbl_info *tbl, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001123{
Zhu Yib481de92007-09-25 17:54:57 -07001124 u16 rate_mask;
1125 s32 rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001126 s8 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07001127
Johannes Bergae5eb022008-10-14 16:58:37 +02001128 if (!conf->ht.enabled || !sta->ht_cap.ht_supported)
Zhu Yib481de92007-09-25 17:54:57 -07001129 return -1;
1130
Johannes Bergd9fe60d2008-10-09 12:13:49 +02001131 if (((sta->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 2)
Tomas Winkler00c5ae22008-09-03 11:26:42 +08001132 == WLAN_HT_CAP_SM_PS_STATIC)
Zhu Yib481de92007-09-25 17:54:57 -07001133 return -1;
1134
Ben Cahill77626352007-11-29 11:09:44 +08001135 /* Need both Tx chains/antennas to support MIMO */
Guy Cohenaade00c2008-04-23 17:14:59 -07001136 if (priv->hw_params.tx_chains_num < 2)
Zhu Yib481de92007-09-25 17:54:57 -07001137 return -1;
1138
Guy Coheneecd6e52008-04-23 17:14:58 -07001139 IWL_DEBUG_RATE("LQ: try to switch to MIMO2\n");
Guy Cohen39e88502008-04-23 17:14:57 -07001140
1141 tbl->lq_type = LQ_MIMO2;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001142 tbl->is_dup = lq_sta->is_dup;
Zhu Yib481de92007-09-25 17:54:57 -07001143 tbl->action = 0;
Guy Cohen39e88502008-04-23 17:14:57 -07001144 rate_mask = lq_sta->active_mimo2_rate;
1145
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001146 if (priv->current_ht_config.supported_chan_width
Guy Cohen39e88502008-04-23 17:14:57 -07001147 == IWL_CHANNEL_WIDTH_40MHZ)
Zhu Yib481de92007-09-25 17:54:57 -07001148 tbl->is_fat = 1;
1149 else
1150 tbl->is_fat = 0;
1151
Guy Cohen39e88502008-04-23 17:14:57 -07001152 /* FIXME: - don't toggle SGI here
Zhu Yib481de92007-09-25 17:54:57 -07001153 if (tbl->is_fat) {
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001154 if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
Zhu Yib481de92007-09-25 17:54:57 -07001155 tbl->is_SGI = 1;
1156 else
1157 tbl->is_SGI = 0;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001158 } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
Zhu Yib481de92007-09-25 17:54:57 -07001159 tbl->is_SGI = 1;
1160 else
1161 tbl->is_SGI = 0;
Guy Cohen39e88502008-04-23 17:14:57 -07001162 */
Zhu Yib481de92007-09-25 17:54:57 -07001163
Guy Coheneecd6e52008-04-23 17:14:58 -07001164 rs_set_expected_tpt_table(lq_sta, tbl);
Zhu Yib481de92007-09-25 17:54:57 -07001165
Guy Cohenf935a6d2008-04-23 17:15:02 -07001166 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
Zhu Yib481de92007-09-25 17:54:57 -07001167
Guy Coheneecd6e52008-04-23 17:14:58 -07001168 IWL_DEBUG_RATE("LQ: MIMO2 best rate %d mask %X\n", rate, rate_mask);
Guy Cohen39e88502008-04-23 17:14:57 -07001169
1170 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
Guy Coheneecd6e52008-04-23 17:14:58 -07001171 IWL_DEBUG_RATE("Can't switch with index %d rate mask %x\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001172 rate, rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07001173 return -1;
Guy Cohen39e88502008-04-23 17:14:57 -07001174 }
1175 tbl->current_rate = rate_n_flags_from_tbl(tbl, rate, is_green);
Zhu Yib481de92007-09-25 17:54:57 -07001176
Guy Coheneecd6e52008-04-23 17:14:58 -07001177 IWL_DEBUG_RATE("LQ: Switch to new mcs %X index is green %X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001178 tbl->current_rate, is_green);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001179 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07001180}
1181
Ben Cahill77626352007-11-29 11:09:44 +08001182/*
1183 * Set up search table for SISO
1184 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001185static int rs_switch_to_siso(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001186 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001187 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001188 struct ieee80211_sta *sta,
Tomas Winklere227cea2008-07-18 13:53:05 +08001189 struct iwl_scale_tbl_info *tbl, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001190{
Zhu Yib481de92007-09-25 17:54:57 -07001191 u16 rate_mask;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001192 u8 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07001193 s32 rate;
1194
Johannes Bergae5eb022008-10-14 16:58:37 +02001195 if (!conf->ht.enabled || !sta->ht_cap.ht_supported)
Zhu Yib481de92007-09-25 17:54:57 -07001196 return -1;
1197
Guy Coheneecd6e52008-04-23 17:14:58 -07001198 IWL_DEBUG_RATE("LQ: try to switch to SISO\n");
Guy Cohen39e88502008-04-23 17:14:57 -07001199
Tomas Winklerc33104f2008-01-14 17:46:21 -08001200 tbl->is_dup = lq_sta->is_dup;
Zhu Yib481de92007-09-25 17:54:57 -07001201 tbl->lq_type = LQ_SISO;
1202 tbl->action = 0;
Guy Cohen39e88502008-04-23 17:14:57 -07001203 rate_mask = lq_sta->active_siso_rate;
Zhu Yib481de92007-09-25 17:54:57 -07001204
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001205 if (priv->current_ht_config.supported_chan_width
1206 == IWL_CHANNEL_WIDTH_40MHZ)
Zhu Yib481de92007-09-25 17:54:57 -07001207 tbl->is_fat = 1;
1208 else
1209 tbl->is_fat = 0;
1210
Guy Cohen39e88502008-04-23 17:14:57 -07001211 /* FIXME: - don't toggle SGI here
Zhu Yib481de92007-09-25 17:54:57 -07001212 if (tbl->is_fat) {
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001213 if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
Zhu Yib481de92007-09-25 17:54:57 -07001214 tbl->is_SGI = 1;
1215 else
1216 tbl->is_SGI = 0;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001217 } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
Zhu Yib481de92007-09-25 17:54:57 -07001218 tbl->is_SGI = 1;
1219 else
1220 tbl->is_SGI = 0;
Guy Cohen39e88502008-04-23 17:14:57 -07001221 */
Zhu Yib481de92007-09-25 17:54:57 -07001222
1223 if (is_green)
Guy Cohen39e88502008-04-23 17:14:57 -07001224 tbl->is_SGI = 0; /*11n spec: no SGI in SISO+Greenfield*/
Zhu Yib481de92007-09-25 17:54:57 -07001225
Guy Coheneecd6e52008-04-23 17:14:58 -07001226 rs_set_expected_tpt_table(lq_sta, tbl);
Guy Cohenf935a6d2008-04-23 17:15:02 -07001227 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
Zhu Yib481de92007-09-25 17:54:57 -07001228
Guy Coheneecd6e52008-04-23 17:14:58 -07001229 IWL_DEBUG_RATE("LQ: get best rate %d mask %X\n", rate, rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07001230 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
Guy Coheneecd6e52008-04-23 17:14:58 -07001231 IWL_DEBUG_RATE("can not switch with index %d rate mask %x\n",
Zhu Yib481de92007-09-25 17:54:57 -07001232 rate, rate_mask);
1233 return -1;
1234 }
Guy Cohen39e88502008-04-23 17:14:57 -07001235 tbl->current_rate = rate_n_flags_from_tbl(tbl, rate, is_green);
Guy Coheneecd6e52008-04-23 17:14:58 -07001236 IWL_DEBUG_RATE("LQ: Switch to new mcs %X index is green %X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001237 tbl->current_rate, is_green);
Mohamed Abbas403ab562007-11-06 22:06:25 -08001238 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07001239}
1240
Ben Cahill77626352007-11-29 11:09:44 +08001241/*
1242 * Try to switch to new modulation mode from legacy
1243 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001244static int rs_move_legacy_other(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001245 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001246 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001247 struct ieee80211_sta *sta,
Zhu Yib481de92007-09-25 17:54:57 -07001248 int index)
1249{
Tomas Winklere227cea2008-07-18 13:53:05 +08001250 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1251 struct iwl_scale_tbl_info *search_tbl =
1252 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1253 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1254 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1255 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001256 u8 start_action = tbl->action;
Guy Cohenfde0db32008-04-21 15:42:01 -07001257 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
Guy Cohen3110bef2008-09-09 10:54:54 +08001258 u8 tx_chains_num = priv->hw_params.tx_chains_num;
Guy Cohenfde0db32008-04-21 15:42:01 -07001259 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001260
1261 for (; ;) {
1262 switch (tbl->action) {
Guy Cohen3110bef2008-09-09 10:54:54 +08001263 case IWL_LEGACY_SWITCH_ANTENNA1:
1264 case IWL_LEGACY_SWITCH_ANTENNA2:
Guy Cohenf935a6d2008-04-23 17:15:02 -07001265 IWL_DEBUG_RATE("LQ: Legacy toggle Antenna\n");
Zhu Yib481de92007-09-25 17:54:57 -07001266
Tomas Winklerc33104f2008-01-14 17:46:21 -08001267 lq_sta->action_counter++;
Ben Cahill77626352007-11-29 11:09:44 +08001268
Guy Cohen3110bef2008-09-09 10:54:54 +08001269 if ((tbl->action == IWL_LEGACY_SWITCH_ANTENNA1 &&
1270 tx_chains_num <= 1) ||
1271 (tbl->action == IWL_LEGACY_SWITCH_ANTENNA2 &&
1272 tx_chains_num <= 2))
1273 break;
1274
Ben Cahill77626352007-11-29 11:09:44 +08001275 /* Don't change antenna if success has been great */
Zhu Yib481de92007-09-25 17:54:57 -07001276 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1277 break;
Ben Cahill77626352007-11-29 11:09:44 +08001278
Ben Cahill77626352007-11-29 11:09:44 +08001279 /* Set up search table to try other antenna */
Zhu Yib481de92007-09-25 17:54:57 -07001280 memcpy(search_tbl, tbl, sz);
1281
Guy Cohenaade00c2008-04-23 17:14:59 -07001282 if (rs_toggle_antenna(valid_tx_ant,
1283 &search_tbl->current_rate, search_tbl)) {
Guy Cohen3110bef2008-09-09 10:54:54 +08001284 rs_set_expected_tpt_table(lq_sta, search_tbl);
Guy Cohenaade00c2008-04-23 17:14:59 -07001285 goto out;
1286 }
Guy Cohena5e8b502008-05-29 16:35:11 +08001287 break;
Zhu Yib481de92007-09-25 17:54:57 -07001288 case IWL_LEGACY_SWITCH_SISO:
Guy Coheneecd6e52008-04-23 17:14:58 -07001289 IWL_DEBUG_RATE("LQ: Legacy switch to SISO\n");
Ben Cahill77626352007-11-29 11:09:44 +08001290
1291 /* Set up search table to try SISO */
Zhu Yib481de92007-09-25 17:54:57 -07001292 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001293 search_tbl->is_SGI = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001294 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001295 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001296 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001297 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001298 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001299 }
Zhu Yib481de92007-09-25 17:54:57 -07001300
1301 break;
Guy Cohen3110bef2008-09-09 10:54:54 +08001302 case IWL_LEGACY_SWITCH_MIMO2_AB:
1303 case IWL_LEGACY_SWITCH_MIMO2_AC:
1304 case IWL_LEGACY_SWITCH_MIMO2_BC:
Guy Coheneecd6e52008-04-23 17:14:58 -07001305 IWL_DEBUG_RATE("LQ: Legacy switch to MIMO2\n");
Ben Cahill77626352007-11-29 11:09:44 +08001306
1307 /* Set up search table to try MIMO */
Zhu Yib481de92007-09-25 17:54:57 -07001308 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001309 search_tbl->is_SGI = 0;
Guy Cohen3110bef2008-09-09 10:54:54 +08001310
1311 if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AB)
1312 search_tbl->ant_type = ANT_AB;
1313 else if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AC)
1314 search_tbl->ant_type = ANT_AC;
1315 else
1316 search_tbl->ant_type = ANT_BC;
1317
1318 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1319 break;
1320
Guy Cohenfde0db32008-04-21 15:42:01 -07001321 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001322 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001323 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001324 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001325 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001326 }
Zhu Yib481de92007-09-25 17:54:57 -07001327 break;
1328 }
1329 tbl->action++;
Guy Cohen3110bef2008-09-09 10:54:54 +08001330 if (tbl->action > IWL_LEGACY_SWITCH_MIMO2_BC)
1331 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
Zhu Yib481de92007-09-25 17:54:57 -07001332
1333 if (tbl->action == start_action)
1334 break;
1335
1336 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001337 search_tbl->lq_type = LQ_NONE;
Zhu Yib481de92007-09-25 17:54:57 -07001338 return 0;
1339
Guy Cohen3110bef2008-09-09 10:54:54 +08001340out:
1341 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001342 tbl->action++;
Guy Cohen3110bef2008-09-09 10:54:54 +08001343 if (tbl->action > IWL_LEGACY_SWITCH_MIMO2_BC)
1344 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
Zhu Yib481de92007-09-25 17:54:57 -07001345 return 0;
1346
1347}
1348
Ben Cahill77626352007-11-29 11:09:44 +08001349/*
1350 * Try to switch to new modulation mode from SISO
1351 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001352static int rs_move_siso_to_other(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001353 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001354 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001355 struct ieee80211_sta *sta, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001356{
Tomas Winklerc33104f2008-01-14 17:46:21 -08001357 u8 is_green = lq_sta->is_green;
Tomas Winklere227cea2008-07-18 13:53:05 +08001358 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1359 struct iwl_scale_tbl_info *search_tbl =
1360 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1361 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1362 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1363 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001364 u8 start_action = tbl->action;
Guy Cohenfde0db32008-04-21 15:42:01 -07001365 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
Guy Cohen3110bef2008-09-09 10:54:54 +08001366 u8 tx_chains_num = priv->hw_params.tx_chains_num;
Guy Cohenfde0db32008-04-21 15:42:01 -07001367 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001368
1369 for (;;) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001370 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001371 switch (tbl->action) {
Guy Cohen3110bef2008-09-09 10:54:54 +08001372 case IWL_SISO_SWITCH_ANTENNA1:
1373 case IWL_SISO_SWITCH_ANTENNA2:
Guy Coheneecd6e52008-04-23 17:14:58 -07001374 IWL_DEBUG_RATE("LQ: SISO toggle Antenna\n");
Guy Cohen3110bef2008-09-09 10:54:54 +08001375
1376 if ((tbl->action == IWL_SISO_SWITCH_ANTENNA1 &&
1377 tx_chains_num <= 1) ||
1378 (tbl->action == IWL_SISO_SWITCH_ANTENNA2 &&
1379 tx_chains_num <= 2))
1380 break;
1381
Zhu Yib481de92007-09-25 17:54:57 -07001382 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1383 break;
Zhu Yib481de92007-09-25 17:54:57 -07001384
1385 memcpy(search_tbl, tbl, sz);
Guy Cohenaade00c2008-04-23 17:14:59 -07001386 if (rs_toggle_antenna(valid_tx_ant,
Guy Cohen3110bef2008-09-09 10:54:54 +08001387 &search_tbl->current_rate, search_tbl))
Guy Cohenaade00c2008-04-23 17:14:59 -07001388 goto out;
Guy Cohena5e8b502008-05-29 16:35:11 +08001389 break;
Guy Cohen3110bef2008-09-09 10:54:54 +08001390 case IWL_SISO_SWITCH_MIMO2_AB:
1391 case IWL_SISO_SWITCH_MIMO2_AC:
1392 case IWL_SISO_SWITCH_MIMO2_BC:
Guy Cohena5e8b502008-05-29 16:35:11 +08001393 IWL_DEBUG_RATE("LQ: SISO switch to MIMO2\n");
Zhu Yib481de92007-09-25 17:54:57 -07001394 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001395 search_tbl->is_SGI = 0;
Guy Cohen3110bef2008-09-09 10:54:54 +08001396
1397 if (tbl->action == IWL_SISO_SWITCH_MIMO2_AB)
1398 search_tbl->ant_type = ANT_AB;
1399 else if (tbl->action == IWL_SISO_SWITCH_MIMO2_AC)
1400 search_tbl->ant_type = ANT_AC;
1401 else
1402 search_tbl->ant_type = ANT_BC;
1403
1404 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1405 break;
1406
Guy Cohenfde0db32008-04-21 15:42:01 -07001407 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001408 search_tbl, index);
Guy Cohen3110bef2008-09-09 10:54:54 +08001409 if (!ret)
Zhu Yib481de92007-09-25 17:54:57 -07001410 goto out;
1411 break;
1412 case IWL_SISO_SWITCH_GI:
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001413 if (!tbl->is_fat &&
1414 !(priv->current_ht_config.sgf &
1415 HT_SHORT_GI_20MHZ))
1416 break;
1417 if (tbl->is_fat &&
1418 !(priv->current_ht_config.sgf &
1419 HT_SHORT_GI_40MHZ))
1420 break;
1421
Guy Coheneecd6e52008-04-23 17:14:58 -07001422 IWL_DEBUG_RATE("LQ: SISO toggle SGI/NGI\n");
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001423
Zhu Yib481de92007-09-25 17:54:57 -07001424 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001425 if (is_green) {
1426 if (!tbl->is_SGI)
1427 break;
1428 else
1429 IWL_ERROR("SGI was set in GF+SISO\n");
Zhu Yib481de92007-09-25 17:54:57 -07001430 }
Guy Cohen39e88502008-04-23 17:14:57 -07001431 search_tbl->is_SGI = !tbl->is_SGI;
Guy Coheneecd6e52008-04-23 17:14:58 -07001432 rs_set_expected_tpt_table(lq_sta, search_tbl);
Guy Cohen39e88502008-04-23 17:14:57 -07001433 if (tbl->is_SGI) {
1434 s32 tpt = lq_sta->last_tpt / 100;
1435 if (tpt >= search_tbl->expected_tpt[index])
1436 break;
1437 }
1438 search_tbl->current_rate = rate_n_flags_from_tbl(
1439 search_tbl, index, is_green);
Zhu Yib481de92007-09-25 17:54:57 -07001440 goto out;
1441 }
1442 tbl->action++;
1443 if (tbl->action > IWL_SISO_SWITCH_GI)
Guy Cohen3110bef2008-09-09 10:54:54 +08001444 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
Zhu Yib481de92007-09-25 17:54:57 -07001445
1446 if (tbl->action == start_action)
1447 break;
1448 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001449 search_tbl->lq_type = LQ_NONE;
Zhu Yib481de92007-09-25 17:54:57 -07001450 return 0;
1451
1452 out:
Guy Cohen3110bef2008-09-09 10:54:54 +08001453 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001454 tbl->action++;
1455 if (tbl->action > IWL_SISO_SWITCH_GI)
Guy Cohen3110bef2008-09-09 10:54:54 +08001456 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
Zhu Yib481de92007-09-25 17:54:57 -07001457 return 0;
1458}
1459
Ben Cahill77626352007-11-29 11:09:44 +08001460/*
1461 * Try to switch to new modulation mode from MIMO
1462 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001463static int rs_move_mimo_to_other(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001464 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001465 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001466 struct ieee80211_sta *sta, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001467{
Tomas Winklerc33104f2008-01-14 17:46:21 -08001468 s8 is_green = lq_sta->is_green;
Tomas Winklere227cea2008-07-18 13:53:05 +08001469 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1470 struct iwl_scale_tbl_info *search_tbl =
1471 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Guy Cohen3110bef2008-09-09 10:54:54 +08001472 struct iwl_rate_scale_data *window = &(tbl->win[index]);
Tomas Winklere227cea2008-07-18 13:53:05 +08001473 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1474 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001475 u8 start_action = tbl->action;
Guy Cohen3110bef2008-09-09 10:54:54 +08001476 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1477 u8 tx_chains_num = priv->hw_params.tx_chains_num;
Guy Cohenaade00c2008-04-23 17:14:59 -07001478 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001479
1480 for (;;) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001481 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001482 switch (tbl->action) {
Guy Cohen3110bef2008-09-09 10:54:54 +08001483 case IWL_MIMO2_SWITCH_ANTENNA1:
1484 case IWL_MIMO2_SWITCH_ANTENNA2:
1485 IWL_DEBUG_RATE("LQ: MIMO toggle Antennas\n");
1486
1487 if (tx_chains_num <= 2)
1488 break;
1489
1490 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1491 break;
1492
1493 memcpy(search_tbl, tbl, sz);
1494 if (rs_toggle_antenna(valid_tx_ant,
1495 &search_tbl->current_rate, search_tbl))
1496 goto out;
1497 break;
1498 case IWL_MIMO2_SWITCH_SISO_A:
1499 case IWL_MIMO2_SWITCH_SISO_B:
1500 case IWL_MIMO2_SWITCH_SISO_C:
Guy Coheneecd6e52008-04-23 17:14:58 -07001501 IWL_DEBUG_RATE("LQ: MIMO2 switch to SISO\n");
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001502
Ben Cahill77626352007-11-29 11:09:44 +08001503 /* Set up new search table for SISO */
Zhu Yib481de92007-09-25 17:54:57 -07001504 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001505
Guy Cohen3110bef2008-09-09 10:54:54 +08001506 if (tbl->action == IWL_MIMO2_SWITCH_SISO_A)
Guy Cohenfde0db32008-04-21 15:42:01 -07001507 search_tbl->ant_type = ANT_A;
Guy Cohen3110bef2008-09-09 10:54:54 +08001508 else if (tbl->action == IWL_MIMO2_SWITCH_SISO_B)
Guy Cohenfde0db32008-04-21 15:42:01 -07001509 search_tbl->ant_type = ANT_B;
Guy Cohen3110bef2008-09-09 10:54:54 +08001510 else
1511 search_tbl->ant_type = ANT_C;
1512
1513 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1514 break;
Zhu Yib481de92007-09-25 17:54:57 -07001515
Tomas Winklerc33104f2008-01-14 17:46:21 -08001516 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001517 search_tbl, index);
Guy Cohen3110bef2008-09-09 10:54:54 +08001518 if (!ret)
Zhu Yib481de92007-09-25 17:54:57 -07001519 goto out;
Guy Cohen3110bef2008-09-09 10:54:54 +08001520
Zhu Yib481de92007-09-25 17:54:57 -07001521 break;
1522
Guy Cohen3110bef2008-09-09 10:54:54 +08001523 case IWL_MIMO2_SWITCH_GI:
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001524 if (!tbl->is_fat &&
1525 !(priv->current_ht_config.sgf &
1526 HT_SHORT_GI_20MHZ))
1527 break;
1528 if (tbl->is_fat &&
1529 !(priv->current_ht_config.sgf &
1530 HT_SHORT_GI_40MHZ))
1531 break;
1532
Guy Coheneecd6e52008-04-23 17:14:58 -07001533 IWL_DEBUG_RATE("LQ: MIMO toggle SGI/NGI\n");
Ben Cahill77626352007-11-29 11:09:44 +08001534
1535 /* Set up new search table for MIMO */
Zhu Yib481de92007-09-25 17:54:57 -07001536 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001537 search_tbl->is_SGI = !tbl->is_SGI;
Guy Coheneecd6e52008-04-23 17:14:58 -07001538 rs_set_expected_tpt_table(lq_sta, search_tbl);
Ben Cahill77626352007-11-29 11:09:44 +08001539 /*
1540 * If active table already uses the fastest possible
1541 * modulation (dual stream with short guard interval),
1542 * and it's working well, there's no need to look
1543 * for a better type of modulation!
1544 */
Guy Cohen39e88502008-04-23 17:14:57 -07001545 if (tbl->is_SGI) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001546 s32 tpt = lq_sta->last_tpt / 100;
Guy Cohen39e88502008-04-23 17:14:57 -07001547 if (tpt >= search_tbl->expected_tpt[index])
1548 break;
Zhu Yib481de92007-09-25 17:54:57 -07001549 }
Guy Cohen39e88502008-04-23 17:14:57 -07001550 search_tbl->current_rate = rate_n_flags_from_tbl(
1551 search_tbl, index, is_green);
Zhu Yib481de92007-09-25 17:54:57 -07001552 goto out;
1553
1554 }
1555 tbl->action++;
Guy Cohen3110bef2008-09-09 10:54:54 +08001556 if (tbl->action > IWL_MIMO2_SWITCH_GI)
1557 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
Zhu Yib481de92007-09-25 17:54:57 -07001558
1559 if (tbl->action == start_action)
1560 break;
1561 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001562 search_tbl->lq_type = LQ_NONE;
Zhu Yib481de92007-09-25 17:54:57 -07001563 return 0;
1564 out:
Guy Cohen3110bef2008-09-09 10:54:54 +08001565 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001566 tbl->action++;
Guy Cohen3110bef2008-09-09 10:54:54 +08001567 if (tbl->action > IWL_MIMO2_SWITCH_GI)
1568 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
Zhu Yib481de92007-09-25 17:54:57 -07001569 return 0;
1570
1571}
1572
Ben Cahill77626352007-11-29 11:09:44 +08001573/*
1574 * Check whether we should continue using same modulation mode, or
1575 * begin search for a new mode, based on:
1576 * 1) # tx successes or failures while using this mode
1577 * 2) # times calling this function
1578 * 3) elapsed time in this mode (not used, for now)
1579 */
Tomas Winklere227cea2008-07-18 13:53:05 +08001580static void rs_stay_in_table(struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07001581{
Tomas Winklere227cea2008-07-18 13:53:05 +08001582 struct iwl_scale_tbl_info *tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001583 int i;
1584 int active_tbl;
1585 int flush_interval_passed = 0;
Ester Kummerbf403db2008-05-05 10:22:40 +08001586 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07001587
Ester Kummerbf403db2008-05-05 10:22:40 +08001588 priv = lq_sta->drv;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001589 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001590
Tomas Winklerc33104f2008-01-14 17:46:21 -08001591 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07001592
Ben Cahill77626352007-11-29 11:09:44 +08001593 /* If we've been disallowing search, see if we should now allow it */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001594 if (lq_sta->stay_in_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07001595
Ben Cahill77626352007-11-29 11:09:44 +08001596 /* Elapsed time using current modulation mode */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001597 if (lq_sta->flush_timer)
Zhu Yib481de92007-09-25 17:54:57 -07001598 flush_interval_passed =
1599 time_after(jiffies,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001600 (unsigned long)(lq_sta->flush_timer +
Zhu Yib481de92007-09-25 17:54:57 -07001601 IWL_RATE_SCALE_FLUSH_INTVL));
1602
Ben Cahill77626352007-11-29 11:09:44 +08001603 /*
1604 * Check if we should allow search for new modulation mode.
1605 * If many frames have failed or succeeded, or we've used
1606 * this same modulation for a long time, allow search, and
1607 * reset history stats that keep track of whether we should
1608 * allow a new search. Also (below) reset all bitmaps and
1609 * stats in active history.
1610 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001611 if ((lq_sta->total_failed > lq_sta->max_failure_limit) ||
1612 (lq_sta->total_success > lq_sta->max_success_limit) ||
1613 ((!lq_sta->search_better_tbl) && (lq_sta->flush_timer)
Zhu Yib481de92007-09-25 17:54:57 -07001614 && (flush_interval_passed))) {
Guy Coheneecd6e52008-04-23 17:14:58 -07001615 IWL_DEBUG_RATE("LQ: stay is expired %d %d %d\n:",
Tomas Winklerc33104f2008-01-14 17:46:21 -08001616 lq_sta->total_failed,
1617 lq_sta->total_success,
Zhu Yib481de92007-09-25 17:54:57 -07001618 flush_interval_passed);
Ben Cahill77626352007-11-29 11:09:44 +08001619
1620 /* Allow search for new mode */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001621 lq_sta->stay_in_tbl = 0; /* only place reset */
1622 lq_sta->total_failed = 0;
1623 lq_sta->total_success = 0;
1624 lq_sta->flush_timer = 0;
Ben Cahill77626352007-11-29 11:09:44 +08001625
1626 /*
1627 * Else if we've used this modulation mode enough repetitions
1628 * (regardless of elapsed time or success/failure), reset
1629 * history bitmaps and rate-specific stats for all rates in
1630 * active table.
1631 */
Mohamed Abbas403ab562007-11-06 22:06:25 -08001632 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001633 lq_sta->table_count++;
1634 if (lq_sta->table_count >=
1635 lq_sta->table_count_limit) {
1636 lq_sta->table_count = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001637
Guy Coheneecd6e52008-04-23 17:14:58 -07001638 IWL_DEBUG_RATE("LQ: stay in table clear win\n");
Zhu Yib481de92007-09-25 17:54:57 -07001639 for (i = 0; i < IWL_RATE_COUNT; i++)
1640 rs_rate_scale_clear_window(
1641 &(tbl->win[i]));
1642 }
1643 }
1644
Ben Cahill77626352007-11-29 11:09:44 +08001645 /* If transitioning to allow "search", reset all history
1646 * bitmaps and stats in active table (this will become the new
1647 * "search" table). */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001648 if (!lq_sta->stay_in_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07001649 for (i = 0; i < IWL_RATE_COUNT; i++)
1650 rs_rate_scale_clear_window(&(tbl->win[i]));
1651 }
1652 }
1653}
1654
Ben Cahill77626352007-11-29 11:09:44 +08001655/*
1656 * Do rate scaling and search for new modulation mode.
1657 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001658static void rs_rate_scale_perform(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001659 struct ieee80211_hdr *hdr,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001660 struct ieee80211_sta *sta,
1661 struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07001662{
Johannes Berg4b7679a2008-09-18 18:14:18 +02001663 struct ieee80211_hw *hw = priv->hw;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001664 struct ieee80211_conf *conf = &hw->conf;
Zhu Yib481de92007-09-25 17:54:57 -07001665 int low = IWL_RATE_INVALID;
1666 int high = IWL_RATE_INVALID;
1667 int index;
1668 int i;
Tomas Winklere227cea2008-07-18 13:53:05 +08001669 struct iwl_rate_scale_data *window = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001670 int current_tpt = IWL_INVALID_VALUE;
1671 int low_tpt = IWL_INVALID_VALUE;
1672 int high_tpt = IWL_INVALID_VALUE;
1673 u32 fail_count;
1674 s8 scale_action = 0;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07001675 u16 rate_mask;
Zhu Yib481de92007-09-25 17:54:57 -07001676 u8 update_lq = 0;
Tomas Winklere227cea2008-07-18 13:53:05 +08001677 struct iwl_scale_tbl_info *tbl, *tbl1;
Zhu Yib481de92007-09-25 17:54:57 -07001678 u16 rate_scale_index_msk = 0;
Guy Cohen39e88502008-04-23 17:14:57 -07001679 u32 rate;
Zhu Yib481de92007-09-25 17:54:57 -07001680 u8 is_green = 0;
1681 u8 active_tbl = 0;
1682 u8 done_search = 0;
1683 u16 high_low;
Guy Cohena5e8b502008-05-29 16:35:11 +08001684 s32 sr;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001685 u8 tid = MAX_TID_COUNT;
Zhu Yib481de92007-09-25 17:54:57 -07001686
1687 IWL_DEBUG_RATE("rate scale calculate new rate for skb\n");
1688
Tomas Winkler417f1142008-11-12 13:14:06 -08001689 /* Send management frames and broadcast/multicast data using
1690 * lowest rate. */
1691 /* TODO: this could probably be improved.. */
1692 if (!ieee80211_is_data(hdr->frame_control) ||
1693 is_multicast_ether_addr(hdr->addr1))
Zhu Yib481de92007-09-25 17:54:57 -07001694 return;
Zhu Yib481de92007-09-25 17:54:57 -07001695
Johannes Berg4b7679a2008-09-18 18:14:18 +02001696 if (!sta || !lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07001697 return;
1698
Johannes Berg4b7679a2008-09-18 18:14:18 +02001699 lq_sta->supp_rates = sta->supp_rates[lq_sta->band];
Zhu Yib481de92007-09-25 17:54:57 -07001700
Ron Rindjunskyfaa29712008-06-12 09:46:58 +08001701 tid = rs_tl_add_packet(lq_sta, hdr);
1702
Ben Cahill77626352007-11-29 11:09:44 +08001703 /*
1704 * Select rate-scale / modulation-mode table to work with in
1705 * the rest of this function: "search" if searching for better
1706 * modulation mode, or "active" if doing rate scaling within a mode.
1707 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001708 if (!lq_sta->search_better_tbl)
1709 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001710 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08001711 active_tbl = 1 - lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001712
Tomas Winklerc33104f2008-01-14 17:46:21 -08001713 tbl = &(lq_sta->lq_info[active_tbl]);
1714 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07001715
Ben Cahill77626352007-11-29 11:09:44 +08001716 /* current tx rate */
Johannes Bergb7e35002008-09-11 02:22:58 +02001717 index = lq_sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07001718
1719 IWL_DEBUG_RATE("Rate scale index %d for type %d\n", index,
1720 tbl->lq_type);
1721
Ben Cahill77626352007-11-29 11:09:44 +08001722 /* rates available for this association, and for modulation mode */
Guy Coheneecd6e52008-04-23 17:14:58 -07001723 rate_mask = rs_get_supported_rates(lq_sta, hdr, tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -07001724
1725 IWL_DEBUG_RATE("mask 0x%04X \n", rate_mask);
1726
1727 /* mask with station rate restriction */
1728 if (is_legacy(tbl->lq_type)) {
Johannes Berg8318d782008-01-24 19:38:38 +01001729 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Ben Cahill77626352007-11-29 11:09:44 +08001730 /* supp_rates has no CCK bits in A mode */
Zhu Yib481de92007-09-25 17:54:57 -07001731 rate_scale_index_msk = (u16) (rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -08001732 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
Zhu Yib481de92007-09-25 17:54:57 -07001733 else
1734 rate_scale_index_msk = (u16) (rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -08001735 lq_sta->supp_rates);
Zhu Yib481de92007-09-25 17:54:57 -07001736
1737 } else
1738 rate_scale_index_msk = rate_mask;
1739
1740 if (!rate_scale_index_msk)
1741 rate_scale_index_msk = rate_mask;
1742
Guy Cohen07bc28e2008-04-23 17:15:03 -07001743 if (!((1 << index) & rate_scale_index_msk)) {
1744 IWL_ERROR("Current Rate is not valid\n");
1745 return;
Zhu Yib481de92007-09-25 17:54:57 -07001746 }
1747
Ben Cahill77626352007-11-29 11:09:44 +08001748 /* Get expected throughput table and history window for current rate */
Guy Cohen07bc28e2008-04-23 17:15:03 -07001749 if (!tbl->expected_tpt) {
1750 IWL_ERROR("tbl->expected_tpt is NULL\n");
1751 return;
1752 }
Zhu Yib481de92007-09-25 17:54:57 -07001753
1754 window = &(tbl->win[index]);
1755
Ben Cahill77626352007-11-29 11:09:44 +08001756 /*
1757 * If there is not enough history to calculate actual average
1758 * throughput, keep analyzing results of more tx frames, without
1759 * changing rate or mode (bypass most of the rest of this function).
1760 * Set up new rate table in uCode only if old rate is not supported
1761 * in current association (use new rate found above).
1762 */
Zhu Yib481de92007-09-25 17:54:57 -07001763 fail_count = window->counter - window->success_counter;
Guy Cohen07bc28e2008-04-23 17:15:03 -07001764 if ((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
1765 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH)) {
1766 IWL_DEBUG_RATE("LQ: still below TH. succ=%d total=%d "
Zhu Yib481de92007-09-25 17:54:57 -07001767 "for index %d\n",
1768 window->success_counter, window->counter, index);
Ben Cahill77626352007-11-29 11:09:44 +08001769
1770 /* Can't calculate this yet; not enough history */
Zhu Yib481de92007-09-25 17:54:57 -07001771 window->average_tpt = IWL_INVALID_VALUE;
Ben Cahill77626352007-11-29 11:09:44 +08001772
1773 /* Should we stay with this modulation mode,
1774 * or search for a new one? */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001775 rs_stay_in_table(lq_sta);
Ben Cahill77626352007-11-29 11:09:44 +08001776
Zhu Yib481de92007-09-25 17:54:57 -07001777 goto out;
Guy Cohen3110bef2008-09-09 10:54:54 +08001778 }
Zhu Yib481de92007-09-25 17:54:57 -07001779
Ben Cahill77626352007-11-29 11:09:44 +08001780 /* Else we have enough samples; calculate estimate of
1781 * actual average throughput */
Guy Cohen3110bef2008-09-09 10:54:54 +08001782
1783 BUG_ON(window->average_tpt != ((window->success_ratio *
1784 tbl->expected_tpt[index] + 64) / 128));
Zhu Yib481de92007-09-25 17:54:57 -07001785
Ben Cahill77626352007-11-29 11:09:44 +08001786 /* If we are searching for better modulation mode, check success. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001787 if (lq_sta->search_better_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07001788
Ben Cahill77626352007-11-29 11:09:44 +08001789 /* If good success, continue using the "search" mode;
1790 * no need to send new link quality command, since we're
1791 * continuing to use the setup that we've been trying. */
Guy Cohen07bc28e2008-04-23 17:15:03 -07001792 if (window->average_tpt > lq_sta->last_tpt) {
1793
Guy Cohen3110bef2008-09-09 10:54:54 +08001794 IWL_DEBUG_RATE("LQ: SWITCHING TO NEW TABLE "
Guy Cohen07bc28e2008-04-23 17:15:03 -07001795 "suc=%d cur-tpt=%d old-tpt=%d\n",
1796 window->success_ratio,
1797 window->average_tpt,
1798 lq_sta->last_tpt);
1799
1800 if (!is_legacy(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08001801 lq_sta->enable_counter = 1;
Guy Cohen07bc28e2008-04-23 17:15:03 -07001802
Ben Cahill77626352007-11-29 11:09:44 +08001803 /* Swap tables; "search" becomes "active" */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001804 lq_sta->active_tbl = active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001805 current_tpt = window->average_tpt;
Ben Cahill77626352007-11-29 11:09:44 +08001806
1807 /* Else poor success; go back to mode in "active" table */
Zhu Yib481de92007-09-25 17:54:57 -07001808 } else {
Guy Cohen07bc28e2008-04-23 17:15:03 -07001809
1810 IWL_DEBUG_RATE("LQ: GOING BACK TO THE OLD TABLE "
1811 "suc=%d cur-tpt=%d old-tpt=%d\n",
1812 window->success_ratio,
1813 window->average_tpt,
1814 lq_sta->last_tpt);
1815
Ben Cahill77626352007-11-29 11:09:44 +08001816 /* Nullify "search" table */
Zhu Yib481de92007-09-25 17:54:57 -07001817 tbl->lq_type = LQ_NONE;
Ben Cahill77626352007-11-29 11:09:44 +08001818
1819 /* Revert to "active" table */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001820 active_tbl = lq_sta->active_tbl;
1821 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07001822
Ben Cahill77626352007-11-29 11:09:44 +08001823 /* Revert to "active" rate and throughput info */
Tomas Winklere7d326a2008-06-12 09:47:11 +08001824 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
Tomas Winklerc33104f2008-01-14 17:46:21 -08001825 current_tpt = lq_sta->last_tpt;
Ben Cahill77626352007-11-29 11:09:44 +08001826
1827 /* Need to set up a new rate table in uCode */
1828 update_lq = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001829 }
Ben Cahill77626352007-11-29 11:09:44 +08001830
1831 /* Either way, we've made a decision; modulation mode
1832 * search is done, allow rate adjustment next time. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001833 lq_sta->search_better_tbl = 0;
Ben Cahill77626352007-11-29 11:09:44 +08001834 done_search = 1; /* Don't switch modes below! */
Zhu Yib481de92007-09-25 17:54:57 -07001835 goto lq_update;
1836 }
1837
Ben Cahill77626352007-11-29 11:09:44 +08001838 /* (Else) not in search of better modulation mode, try for better
1839 * starting rate, while staying in this mode. */
Ester Kummerbf403db2008-05-05 10:22:40 +08001840 high_low = rs_get_adjacent_rate(priv, index, rate_scale_index_msk,
Zhu Yib481de92007-09-25 17:54:57 -07001841 tbl->lq_type);
1842 low = high_low & 0xff;
1843 high = (high_low >> 8) & 0xff;
1844
Guy Cohena5e8b502008-05-29 16:35:11 +08001845 sr = window->success_ratio;
1846
Ben Cahill77626352007-11-29 11:09:44 +08001847 /* Collect measured throughputs for current and adjacent rates */
Zhu Yib481de92007-09-25 17:54:57 -07001848 current_tpt = window->average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07001849 if (low != IWL_RATE_INVALID)
1850 low_tpt = tbl->win[low].average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07001851 if (high != IWL_RATE_INVALID)
1852 high_tpt = tbl->win[high].average_tpt;
1853
Guy Cohena5e8b502008-05-29 16:35:11 +08001854 scale_action = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001855
Ben Cahill77626352007-11-29 11:09:44 +08001856 /* Too many failures, decrease rate */
Guy Cohena5e8b502008-05-29 16:35:11 +08001857 if ((sr <= IWL_RATE_DECREASE_TH) || (current_tpt == 0)) {
Zhu Yib481de92007-09-25 17:54:57 -07001858 IWL_DEBUG_RATE("decrease rate because of low success_ratio\n");
1859 scale_action = -1;
Ben Cahill77626352007-11-29 11:09:44 +08001860
1861 /* No throughput measured yet for adjacent rates; try increase. */
Zhu Yib481de92007-09-25 17:54:57 -07001862 } else if ((low_tpt == IWL_INVALID_VALUE) &&
Guy Cohena5e8b502008-05-29 16:35:11 +08001863 (high_tpt == IWL_INVALID_VALUE)) {
1864
1865 if (high != IWL_RATE_INVALID && sr >= IWL_RATE_INCREASE_TH)
1866 scale_action = 1;
1867 else if (low != IWL_RATE_INVALID)
1868 scale_action = -1;
1869 }
Ben Cahill77626352007-11-29 11:09:44 +08001870
1871 /* Both adjacent throughputs are measured, but neither one has better
1872 * throughput; we're using the best rate, don't change it! */
Zhu Yib481de92007-09-25 17:54:57 -07001873 else if ((low_tpt != IWL_INVALID_VALUE) &&
1874 (high_tpt != IWL_INVALID_VALUE) &&
1875 (low_tpt < current_tpt) &&
1876 (high_tpt < current_tpt))
1877 scale_action = 0;
Ben Cahill77626352007-11-29 11:09:44 +08001878
1879 /* At least one adjacent rate's throughput is measured,
1880 * and may have better performance. */
Zhu Yib481de92007-09-25 17:54:57 -07001881 else {
Ben Cahill77626352007-11-29 11:09:44 +08001882 /* Higher adjacent rate's throughput is measured */
Zhu Yib481de92007-09-25 17:54:57 -07001883 if (high_tpt != IWL_INVALID_VALUE) {
Ben Cahill77626352007-11-29 11:09:44 +08001884 /* Higher rate has better throughput */
Guy Cohena5e8b502008-05-29 16:35:11 +08001885 if (high_tpt > current_tpt &&
1886 sr >= IWL_RATE_INCREASE_TH) {
Zhu Yib481de92007-09-25 17:54:57 -07001887 scale_action = 1;
Guy Cohena5e8b502008-05-29 16:35:11 +08001888 } else {
Zhu Yib481de92007-09-25 17:54:57 -07001889 IWL_DEBUG_RATE
1890 ("decrease rate because of high tpt\n");
1891 scale_action = -1;
1892 }
Ben Cahill77626352007-11-29 11:09:44 +08001893
1894 /* Lower adjacent rate's throughput is measured */
Zhu Yib481de92007-09-25 17:54:57 -07001895 } else if (low_tpt != IWL_INVALID_VALUE) {
Ben Cahill77626352007-11-29 11:09:44 +08001896 /* Lower rate has better throughput */
Zhu Yib481de92007-09-25 17:54:57 -07001897 if (low_tpt > current_tpt) {
1898 IWL_DEBUG_RATE
1899 ("decrease rate because of low tpt\n");
1900 scale_action = -1;
Guy Cohena5e8b502008-05-29 16:35:11 +08001901 } else if (sr >= IWL_RATE_INCREASE_TH) {
Zhu Yib481de92007-09-25 17:54:57 -07001902 scale_action = 1;
Guy Cohena5e8b502008-05-29 16:35:11 +08001903 }
Zhu Yib481de92007-09-25 17:54:57 -07001904 }
1905 }
1906
Ben Cahill77626352007-11-29 11:09:44 +08001907 /* Sanity check; asked for decrease, but success rate or throughput
1908 * has been good at old rate. Don't change it. */
Guy Cohena5e8b502008-05-29 16:35:11 +08001909 if ((scale_action == -1) && (low != IWL_RATE_INVALID) &&
1910 ((sr > IWL_RATE_HIGH_TH) ||
Zhu Yib481de92007-09-25 17:54:57 -07001911 (current_tpt > (100 * tbl->expected_tpt[low]))))
Zhu Yib481de92007-09-25 17:54:57 -07001912 scale_action = 0;
1913
1914 switch (scale_action) {
1915 case -1:
Ben Cahill77626352007-11-29 11:09:44 +08001916 /* Decrease starting rate, update uCode's rate table */
Zhu Yib481de92007-09-25 17:54:57 -07001917 if (low != IWL_RATE_INVALID) {
1918 update_lq = 1;
1919 index = low;
1920 }
1921 break;
1922 case 1:
Ben Cahill77626352007-11-29 11:09:44 +08001923 /* Increase starting rate, update uCode's rate table */
Zhu Yib481de92007-09-25 17:54:57 -07001924 if (high != IWL_RATE_INVALID) {
1925 update_lq = 1;
1926 index = high;
1927 }
1928
1929 break;
1930 case 0:
Ben Cahill77626352007-11-29 11:09:44 +08001931 /* No change */
Zhu Yib481de92007-09-25 17:54:57 -07001932 default:
1933 break;
1934 }
1935
Guy Coheneecd6e52008-04-23 17:14:58 -07001936 IWL_DEBUG_RATE("choose rate scale index %d action %d low %d "
Zhu Yib481de92007-09-25 17:54:57 -07001937 "high %d type %d\n",
1938 index, scale_action, low, high, tbl->lq_type);
1939
Guy Cohena5e8b502008-05-29 16:35:11 +08001940lq_update:
Ben Cahill77626352007-11-29 11:09:44 +08001941 /* Replace uCode's rate table for the destination station. */
Zhu Yib481de92007-09-25 17:54:57 -07001942 if (update_lq) {
Guy Cohen39e88502008-04-23 17:14:57 -07001943 rate = rate_n_flags_from_tbl(tbl, index, is_green);
Guy Cohen07bc28e2008-04-23 17:15:03 -07001944 rs_fill_link_cmd(priv, lq_sta, rate);
Tomas Winkler66c73db2008-04-15 16:01:40 -07001945 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07001946 }
Ben Cahill77626352007-11-29 11:09:44 +08001947
1948 /* Should we stay with this modulation mode, or search for a new one? */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001949 rs_stay_in_table(lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07001950
Ben Cahill77626352007-11-29 11:09:44 +08001951 /*
1952 * Search for new modulation mode if we're:
1953 * 1) Not changing rates right now
1954 * 2) Not just finishing up a search
1955 * 3) Allowing a new search
1956 */
Guy Cohen47cfd462008-05-27 11:29:34 +08001957 if (!update_lq && !done_search && !lq_sta->stay_in_tbl && window->counter) {
Ben Cahill77626352007-11-29 11:09:44 +08001958 /* Save current throughput to compare with "search" throughput*/
Tomas Winklerc33104f2008-01-14 17:46:21 -08001959 lq_sta->last_tpt = current_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07001960
Ben Cahill77626352007-11-29 11:09:44 +08001961 /* Select a new "search" modulation mode to try.
1962 * If one is found, set up the new "search" table. */
Zhu Yib481de92007-09-25 17:54:57 -07001963 if (is_legacy(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08001964 rs_move_legacy_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07001965 else if (is_siso(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08001966 rs_move_siso_to_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07001967 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08001968 rs_move_mimo_to_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07001969
Ben Cahill77626352007-11-29 11:09:44 +08001970 /* If new "search" mode was selected, set up in uCode table */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001971 if (lq_sta->search_better_tbl) {
Ben Cahill77626352007-11-29 11:09:44 +08001972 /* Access the "search" table, clear its history. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001973 tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Zhu Yib481de92007-09-25 17:54:57 -07001974 for (i = 0; i < IWL_RATE_COUNT; i++)
1975 rs_rate_scale_clear_window(&(tbl->win[i]));
1976
Ben Cahill77626352007-11-29 11:09:44 +08001977 /* Use new "search" start rate */
Tomas Winklere7d326a2008-06-12 09:47:11 +08001978 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
Zhu Yib481de92007-09-25 17:54:57 -07001979
Guy Coheneecd6e52008-04-23 17:14:58 -07001980 IWL_DEBUG_RATE("Switch current mcs: %X index: %d\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001981 tbl->current_rate, index);
Guy Cohen07bc28e2008-04-23 17:15:03 -07001982 rs_fill_link_cmd(priv, lq_sta, tbl->current_rate);
Tomas Winkler66c73db2008-04-15 16:01:40 -07001983 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07001984 }
Zhu Yib481de92007-09-25 17:54:57 -07001985
Ben Cahill77626352007-11-29 11:09:44 +08001986 /* If the "active" (non-search) mode was legacy,
1987 * and we've tried switching antennas,
1988 * but we haven't been able to try HT modes (not available),
1989 * stay with best antenna legacy modulation for a while
1990 * before next round of mode comparisons. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001991 tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
Johannes Bergae5eb022008-10-14 16:58:37 +02001992 if (is_legacy(tbl1->lq_type) && !conf->ht.enabled &&
1993 lq_sta->action_counter >= 1) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001994 lq_sta->action_counter = 0;
Guy Coheneecd6e52008-04-23 17:14:58 -07001995 IWL_DEBUG_RATE("LQ: STAY in legacy table\n");
Ester Kummerbf403db2008-05-05 10:22:40 +08001996 rs_set_stay_in_table(priv, 1, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07001997 }
1998
Ben Cahill77626352007-11-29 11:09:44 +08001999 /* If we're in an HT mode, and all 3 mode switch actions
2000 * have been tried and compared, stay in this best modulation
2001 * mode for a while before next round of mode comparisons. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002002 if (lq_sta->enable_counter &&
2003 (lq_sta->action_counter >= IWL_ACTION_LIMIT)) {
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002004 if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
2005 (lq_sta->tx_agg_tid_en & (1 << tid)) &&
2006 (tid != MAX_TID_COUNT)) {
Guy Coheneecd6e52008-04-23 17:14:58 -07002007 IWL_DEBUG_RATE("try to aggregate tid %d\n", tid);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002008 rs_tl_turn_on_agg(priv, tid, lq_sta, sta);
Zhu Yib481de92007-09-25 17:54:57 -07002009 }
Tomas Winklerc33104f2008-01-14 17:46:21 -08002010 lq_sta->action_counter = 0;
Ester Kummerbf403db2008-05-05 10:22:40 +08002011 rs_set_stay_in_table(priv, 0, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002012 }
Ben Cahill77626352007-11-29 11:09:44 +08002013
2014 /*
2015 * Else, don't search for a new modulation mode.
2016 * Put new timestamp in stay-in-modulation-mode flush timer if:
2017 * 1) Not changing rates right now
2018 * 2) Not just finishing up a search
2019 * 3) flush timer is empty
2020 */
Zhu Yib481de92007-09-25 17:54:57 -07002021 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002022 if ((!update_lq) && (!done_search) && (!lq_sta->flush_timer))
2023 lq_sta->flush_timer = jiffies;
Zhu Yib481de92007-09-25 17:54:57 -07002024 }
2025
2026out:
Guy Cohen39e88502008-04-23 17:14:57 -07002027 tbl->current_rate = rate_n_flags_from_tbl(tbl, index, is_green);
Zhu Yib481de92007-09-25 17:54:57 -07002028 i = index;
Johannes Bergb7e35002008-09-11 02:22:58 +02002029 lq_sta->last_txrate_idx = i;
Zhu Yib481de92007-09-25 17:54:57 -07002030
Zhu Yib481de92007-09-25 17:54:57 -07002031 return;
2032}
2033
2034
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002035static void rs_initialize_lq(struct iwl_priv *priv,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002036 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02002037 struct ieee80211_sta *sta,
2038 struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002039{
Tomas Winklere227cea2008-07-18 13:53:05 +08002040 struct iwl_scale_tbl_info *tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002041 int rate_idx;
Guy Cohenaade00c2008-04-23 17:14:59 -07002042 int i;
Guy Cohen39e88502008-04-23 17:14:57 -07002043 u32 rate;
Guy Cohenaade00c2008-04-23 17:14:59 -07002044 u8 use_green = rs_use_green(priv, conf);
2045 u8 active_tbl = 0;
2046 u8 valid_tx_ant;
Zhu Yib481de92007-09-25 17:54:57 -07002047
Johannes Berg4b7679a2008-09-18 18:14:18 +02002048 if (!sta || !lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002049 goto out;
2050
Johannes Bergb7e35002008-09-11 02:22:58 +02002051 i = lq_sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002052
Tomas Winklerc33104f2008-01-14 17:46:21 -08002053 if ((lq_sta->lq.sta_id == 0xff) &&
Johannes Berg05c914f2008-09-11 00:01:58 +02002054 (priv->iw_mode == NL80211_IFTYPE_ADHOC))
Zhu Yib481de92007-09-25 17:54:57 -07002055 goto out;
2056
Guy Cohenaade00c2008-04-23 17:14:59 -07002057 valid_tx_ant = priv->hw_params.valid_tx_ant;
2058
Tomas Winklerc33104f2008-01-14 17:46:21 -08002059 if (!lq_sta->search_better_tbl)
2060 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002061 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002062 active_tbl = 1 - lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002063
Tomas Winklerc33104f2008-01-14 17:46:21 -08002064 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07002065
2066 if ((i < 0) || (i >= IWL_RATE_COUNT))
2067 i = 0;
2068
Tomas Winkler1826dcc2008-05-15 13:54:02 +08002069 rate = iwl_rates[i].plcp;
Winkler, Tomaseb48dca2008-10-29 14:05:48 -07002070 tbl->ant_type = first_antenna(valid_tx_ant);
2071 rate |= tbl->ant_type << RATE_MCS_ANT_POS;
Zhu Yib481de92007-09-25 17:54:57 -07002072
2073 if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
Guy Cohen39e88502008-04-23 17:14:57 -07002074 rate |= RATE_MCS_CCK_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002075
Guy Cohen39e88502008-04-23 17:14:57 -07002076 rs_get_tbl_info_from_mcs(rate, priv->band, tbl, &rate_idx);
Guy Cohenaade00c2008-04-23 17:14:59 -07002077 if (!rs_is_valid_ant(valid_tx_ant, tbl->ant_type))
2078 rs_toggle_antenna(valid_tx_ant, &rate, tbl);
Zhu Yib481de92007-09-25 17:54:57 -07002079
Guy Cohen39e88502008-04-23 17:14:57 -07002080 rate = rate_n_flags_from_tbl(tbl, rate_idx, use_green);
2081 tbl->current_rate = rate;
Guy Coheneecd6e52008-04-23 17:14:58 -07002082 rs_set_expected_tpt_table(lq_sta, tbl);
Guy Cohen07bc28e2008-04-23 17:15:03 -07002083 rs_fill_link_cmd(NULL, lq_sta, rate);
Tomas Winkler66c73db2008-04-15 16:01:40 -07002084 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07002085 out:
2086 return;
2087}
2088
Johannes Berge6a98542008-10-21 12:40:02 +02002089static void rs_get_rate(void *priv_r, struct ieee80211_sta *sta, void *priv_sta,
2090 struct ieee80211_tx_rate_control *txrc)
Zhu Yib481de92007-09-25 17:54:57 -07002091{
2092
Johannes Berge6a98542008-10-21 12:40:02 +02002093 struct sk_buff *skb = txrc->skb;
2094 struct ieee80211_supported_band *sband = txrc->sband;
Johannes Berg4b7679a2008-09-18 18:14:18 +02002095 struct iwl_priv *priv = (struct iwl_priv *)priv_r;
2096 struct ieee80211_conf *conf = &priv->hw->conf;
Zhu Yib481de92007-09-25 17:54:57 -07002097 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berge6a98542008-10-21 12:40:02 +02002098 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Tomas Winkler417f1142008-11-12 13:14:06 -08002099 struct iwl_lq_sta *lq_sta = priv_sta;
2100 int rate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002101
Zhu Yi58826352007-09-27 11:27:39 +08002102 IWL_DEBUG_RATE_LIMIT("rate scale calculate new rate for skb\n");
Zhu Yib481de92007-09-25 17:54:57 -07002103
Michael Wu2bc454b2007-12-25 19:33:16 -05002104 /* Send management frames and broadcast/multicast data using lowest
2105 * rate. */
Tomas Winkler417f1142008-11-12 13:14:06 -08002106 if (!ieee80211_is_data(hdr->frame_control) ||
2107 is_multicast_ether_addr(hdr->addr1) || !sta || !lq_sta) {
Johannes Berge6a98542008-10-21 12:40:02 +02002108 info->control.rates[0].idx = rate_lowest_index(sband, sta);
Johannes Berg4b7679a2008-09-18 18:14:18 +02002109 return;
Zhu Yib481de92007-09-25 17:54:57 -07002110 }
2111
Tomas Winkler417f1142008-11-12 13:14:06 -08002112 rate_idx = lq_sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002113
Johannes Berg05c914f2008-09-11 00:01:58 +02002114 if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) &&
Tomas Winklerc33104f2008-01-14 17:46:21 -08002115 !lq_sta->ibss_sta_added) {
Tomas Winkler947b13a2008-04-16 16:34:48 -07002116 u8 sta_id = iwl_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002117
2118 if (sta_id == IWL_INVALID_STATION) {
Johannes Berge1749612008-10-27 15:59:26 -07002119 IWL_DEBUG_RATE("LQ: ADD station %pM\n",
2120 hdr->addr1);
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08002121 sta_id = iwl_add_station_flags(priv, hdr->addr1,
Ron Rindjunsky67d62032007-11-26 16:14:40 +02002122 0, CMD_ASYNC, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07002123 }
2124 if ((sta_id != IWL_INVALID_STATION)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002125 lq_sta->lq.sta_id = sta_id;
2126 lq_sta->lq.rs_table[0].rate_n_flags = 0;
2127 lq_sta->ibss_sta_added = 1;
Johannes Berg4b7679a2008-09-18 18:14:18 +02002128 rs_initialize_lq(priv, conf, sta, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002129 }
Zhu Yib481de92007-09-25 17:54:57 -07002130 }
2131
Tomas Winkler417f1142008-11-12 13:14:06 -08002132 if (rate_idx < 0 || rate_idx > IWL_RATE_COUNT)
2133 rate_idx = rate_lowest_index(sband, sta);
2134 else if (sband->band == IEEE80211_BAND_5GHZ)
2135 rate_idx -= IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -07002136
Tomas Winkler417f1142008-11-12 13:14:06 -08002137 info->control.rates[0].idx = rate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002138}
2139
Johannes Berg4b7679a2008-09-18 18:14:18 +02002140static void *rs_alloc_sta(void *priv_rate, struct ieee80211_sta *sta,
2141 gfp_t gfp)
Zhu Yib481de92007-09-25 17:54:57 -07002142{
Tomas Winklere227cea2008-07-18 13:53:05 +08002143 struct iwl_lq_sta *lq_sta;
Ester Kummerbf403db2008-05-05 10:22:40 +08002144 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07002145 int i, j;
2146
Ester Kummerbf403db2008-05-05 10:22:40 +08002147 priv = (struct iwl_priv *)priv_rate;
Zhu Yib481de92007-09-25 17:54:57 -07002148 IWL_DEBUG_RATE("create station rate scale window\n");
2149
Tomas Winklere227cea2008-07-18 13:53:05 +08002150 lq_sta = kzalloc(sizeof(struct iwl_lq_sta), gfp);
Zhu Yib481de92007-09-25 17:54:57 -07002151
Tomas Winklerc33104f2008-01-14 17:46:21 -08002152 if (lq_sta == NULL)
Zhu Yib481de92007-09-25 17:54:57 -07002153 return NULL;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002154 lq_sta->lq.sta_id = 0xff;
Zhu Yib481de92007-09-25 17:54:57 -07002155
Zhu Yi63fddb92007-09-27 11:27:36 +08002156
Zhu Yib481de92007-09-25 17:54:57 -07002157 for (j = 0; j < LQ_SIZE; j++)
2158 for (i = 0; i < IWL_RATE_COUNT; i++)
Tomas Winkler3ac7f142008-07-21 02:40:14 +03002159 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
Zhu Yib481de92007-09-25 17:54:57 -07002160
Tomas Winklerc33104f2008-01-14 17:46:21 -08002161 return lq_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002162}
2163
Johannes Berg4b7679a2008-09-18 18:14:18 +02002164static void rs_rate_init(void *priv_r, struct ieee80211_supported_band *sband,
2165 struct ieee80211_sta *sta, void *priv_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002166{
2167 int i, j;
Johannes Berg4b7679a2008-09-18 18:14:18 +02002168 struct iwl_priv *priv = (struct iwl_priv *)priv_r;
2169 struct ieee80211_conf *conf = &priv->hw->conf;
Tomas Winklere227cea2008-07-18 13:53:05 +08002170 struct iwl_lq_sta *lq_sta = priv_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002171
Tomas Winklerc33104f2008-01-14 17:46:21 -08002172 lq_sta->flush_timer = 0;
Johannes Berg4b7679a2008-09-18 18:14:18 +02002173 lq_sta->supp_rates = sta->supp_rates[sband->band];
Zhu Yib481de92007-09-25 17:54:57 -07002174 for (j = 0; j < LQ_SIZE; j++)
2175 for (i = 0; i < IWL_RATE_COUNT; i++)
Tomas Winkler3ac7f142008-07-21 02:40:14 +03002176 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
Zhu Yib481de92007-09-25 17:54:57 -07002177
Guy Cohen3110bef2008-09-09 10:54:54 +08002178 IWL_DEBUG_RATE("LQ: *** rate scale station global init ***\n");
Zhu Yib481de92007-09-25 17:54:57 -07002179 /* TODO: what is a good starting rate for STA? About middle? Maybe not
2180 * the lowest or the highest rate.. Could consider using RSSI from
2181 * previous packets? Need to have IEEE 802.1X auth succeed immediately
2182 * after assoc.. */
2183
Tomas Winklerc33104f2008-01-14 17:46:21 -08002184 lq_sta->ibss_sta_added = 0;
Johannes Berg05c914f2008-09-11 00:01:58 +02002185 if (priv->iw_mode == NL80211_IFTYPE_AP) {
Johannes Berg4b7679a2008-09-18 18:14:18 +02002186 u8 sta_id = iwl_find_station(priv, sta->addr);
Joe Perches0795af52007-10-03 17:59:30 -07002187
Zhu Yib481de92007-09-25 17:54:57 -07002188 /* for IBSS the call are from tasklet */
Johannes Berge1749612008-10-27 15:59:26 -07002189 IWL_DEBUG_RATE("LQ: ADD station %pM\n", sta->addr);
Zhu Yib481de92007-09-25 17:54:57 -07002190
2191 if (sta_id == IWL_INVALID_STATION) {
Johannes Berge1749612008-10-27 15:59:26 -07002192 IWL_DEBUG_RATE("LQ: ADD station %pM\n", sta->addr);
Johannes Berg4b7679a2008-09-18 18:14:18 +02002193 sta_id = iwl_add_station_flags(priv, sta->addr,
Ron Rindjunsky67d62032007-11-26 16:14:40 +02002194 0, CMD_ASYNC, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07002195 }
2196 if ((sta_id != IWL_INVALID_STATION)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002197 lq_sta->lq.sta_id = sta_id;
2198 lq_sta->lq.rs_table[0].rate_n_flags = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002199 }
2200 /* FIXME: this is w/a remove it later */
2201 priv->assoc_station_added = 1;
2202 }
2203
Ben Cahill77626352007-11-29 11:09:44 +08002204 /* Find highest tx rate supported by hardware and destination station */
Johannes Bergae17e982008-09-11 03:04:36 +02002205 lq_sta->last_txrate_idx = 3;
Johannes Berg8318d782008-01-24 19:38:38 +01002206 for (i = 0; i < sband->n_bitrates; i++)
Johannes Berg4b7679a2008-09-18 18:14:18 +02002207 if (sta->supp_rates[sband->band] & BIT(i))
Johannes Bergae17e982008-09-11 03:04:36 +02002208 lq_sta->last_txrate_idx = i;
Johannes Berg8318d782008-01-24 19:38:38 +01002209
Tomas Winkler05ecc2c2008-09-03 11:26:55 +08002210 /* For MODE_IEEE80211A, skip over cck rates in global rate table */
Johannes Berg4b7679a2008-09-18 18:14:18 +02002211 if (sband->band == IEEE80211_BAND_5GHZ)
Johannes Bergb7e35002008-09-11 02:22:58 +02002212 lq_sta->last_txrate_idx += IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -07002213
Tomas Winklerc33104f2008-01-14 17:46:21 -08002214 lq_sta->is_dup = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002215 lq_sta->is_green = rs_use_green(priv, conf);
Guy Cohen39e88502008-04-23 17:14:57 -07002216 lq_sta->active_legacy_rate = priv->active_rate & ~(0x1000);
Tomas Winklerc33104f2008-01-14 17:46:21 -08002217 lq_sta->active_rate_basic = priv->active_rate_basic;
Johannes Berg8318d782008-01-24 19:38:38 +01002218 lq_sta->band = priv->band;
Ben Cahill77626352007-11-29 11:09:44 +08002219 /*
2220 * active_siso_rate mask includes 9 MBits (bit 5), and CCK (bits 0-3),
2221 * supp_rates[] does not; shift to convert format, force 9 MBits off.
2222 */
Johannes Bergae5eb022008-10-14 16:58:37 +02002223 lq_sta->active_siso_rate = sta->ht_cap.mcs.rx_mask[0] << 1;
2224 lq_sta->active_siso_rate |= sta->ht_cap.mcs.rx_mask[0] & 0x1;
Guy Cohenfde0db32008-04-21 15:42:01 -07002225 lq_sta->active_siso_rate &= ~((u16)0x2);
2226 lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -07002227
Ben Cahill77626352007-11-29 11:09:44 +08002228 /* Same here */
Johannes Bergae5eb022008-10-14 16:58:37 +02002229 lq_sta->active_mimo2_rate = sta->ht_cap.mcs.rx_mask[1] << 1;
2230 lq_sta->active_mimo2_rate |= sta->ht_cap.mcs.rx_mask[1] & 0x1;
Guy Cohenfde0db32008-04-21 15:42:01 -07002231 lq_sta->active_mimo2_rate &= ~((u16)0x2);
2232 lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE;
2233
Johannes Bergae5eb022008-10-14 16:58:37 +02002234 lq_sta->active_mimo3_rate = sta->ht_cap.mcs.rx_mask[2] << 1;
2235 lq_sta->active_mimo3_rate |= sta->ht_cap.mcs.rx_mask[2] & 0x1;
Guy Cohenfde0db32008-04-21 15:42:01 -07002236 lq_sta->active_mimo3_rate &= ~((u16)0x2);
2237 lq_sta->active_mimo3_rate <<= IWL_FIRST_OFDM_RATE;
2238
Guy Cohen07bc28e2008-04-23 17:15:03 -07002239 IWL_DEBUG_RATE("SISO-RATE=%X MIMO2-RATE=%X MIMO3-RATE=%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002240 lq_sta->active_siso_rate,
Guy Cohenfde0db32008-04-21 15:42:01 -07002241 lq_sta->active_mimo2_rate,
2242 lq_sta->active_mimo3_rate);
2243
Tomas Winklera96a27f2008-10-23 23:48:56 -07002244 /* These values will be overridden later */
Guy Cohen07bc28e2008-04-23 17:15:03 -07002245 lq_sta->lq.general_params.single_stream_ant_msk = ANT_A;
2246 lq_sta->lq.general_params.dual_stream_ant_msk = ANT_AB;
2247
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002248 /* as default allow aggregation for all tids */
2249 lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002250 lq_sta->drv = priv;
Zhu Yib481de92007-09-25 17:54:57 -07002251
Johannes Berg4b7679a2008-09-18 18:14:18 +02002252 rs_initialize_lq(priv, conf, sta, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002253}
2254
Guy Cohenfde0db32008-04-21 15:42:01 -07002255static void rs_fill_link_cmd(const struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08002256 struct iwl_lq_sta *lq_sta, u32 new_rate)
Zhu Yib481de92007-09-25 17:54:57 -07002257{
Tomas Winklere227cea2008-07-18 13:53:05 +08002258 struct iwl_scale_tbl_info tbl_type;
Zhu Yib481de92007-09-25 17:54:57 -07002259 int index = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002260 int rate_idx;
Zhu Yi1b696de2007-09-27 11:27:41 +08002261 int repeat_rate = 0;
Guy Cohenaade00c2008-04-23 17:14:59 -07002262 u8 ant_toggle_cnt = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002263 u8 use_ht_possible = 1;
Guy Cohenaade00c2008-04-23 17:14:59 -07002264 u8 valid_tx_ant = 0;
Guy Cohen07bc28e2008-04-23 17:15:03 -07002265 struct iwl_link_quality_cmd *lq_cmd = &lq_sta->lq;
Zhu Yib481de92007-09-25 17:54:57 -07002266
Ben Cahill77626352007-11-29 11:09:44 +08002267 /* Override starting rate (index 0) if needed for debug purposes */
Guy Cohen39e88502008-04-23 17:14:57 -07002268 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Zhu Yi98d7e092007-09-27 11:27:42 +08002269
Guy Cohen39e88502008-04-23 17:14:57 -07002270 /* Interpret new_rate (rate_n_flags) */
Guy Cohenaade00c2008-04-23 17:14:59 -07002271 memset(&tbl_type, 0, sizeof(tbl_type));
Guy Cohen39e88502008-04-23 17:14:57 -07002272 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band,
Zhu Yib481de92007-09-25 17:54:57 -07002273 &tbl_type, &rate_idx);
2274
Ben Cahill77626352007-11-29 11:09:44 +08002275 /* How many times should we repeat the initial rate? */
Zhu Yib481de92007-09-25 17:54:57 -07002276 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07002277 ant_toggle_cnt = 1;
Zhu Yi1b696de2007-09-27 11:27:41 +08002278 repeat_rate = IWL_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002279 } else {
Zhu Yi1b696de2007-09-27 11:27:41 +08002280 repeat_rate = IWL_HT_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002281 }
Zhu Yib481de92007-09-25 17:54:57 -07002282
2283 lq_cmd->general_params.mimo_delimiter =
2284 is_mimo(tbl_type.lq_type) ? 1 : 0;
Ben Cahill77626352007-11-29 11:09:44 +08002285
2286 /* Fill 1st table entry (index 0) */
Guy Cohen39e88502008-04-23 17:14:57 -07002287 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002288
Guy Cohen07bc28e2008-04-23 17:15:03 -07002289 if (num_of_ant(tbl_type.ant_type) == 1) {
2290 lq_cmd->general_params.single_stream_ant_msk =
2291 tbl_type.ant_type;
2292 } else if (num_of_ant(tbl_type.ant_type) == 2) {
2293 lq_cmd->general_params.dual_stream_ant_msk =
2294 tbl_type.ant_type;
2295 } /* otherwise we don't modify the existing value */
Zhu Yib481de92007-09-25 17:54:57 -07002296
2297 index++;
Zhu Yi1b696de2007-09-27 11:27:41 +08002298 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002299
Guy Cohenaade00c2008-04-23 17:14:59 -07002300 if (priv)
2301 valid_tx_ant = priv->hw_params.valid_tx_ant;
2302
Ben Cahill77626352007-11-29 11:09:44 +08002303 /* Fill rest of rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002304 while (index < LINK_QUAL_MAX_RETRY_NUM) {
Ben Cahill77626352007-11-29 11:09:44 +08002305 /* Repeat initial/next rate.
2306 * For legacy IWL_NUMBER_TRY == 1, this loop will not execute.
2307 * For HT IWL_HT_NUMBER_TRY == 3, this executes twice. */
Zhu Yi1b696de2007-09-27 11:27:41 +08002308 while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
Zhu Yib481de92007-09-25 17:54:57 -07002309 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07002310 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2311 ant_toggle_cnt++;
2312 else if (priv &&
2313 rs_toggle_antenna(valid_tx_ant,
2314 &new_rate, &tbl_type))
2315 ant_toggle_cnt = 1;
2316}
Zhu Yi98d7e092007-09-27 11:27:42 +08002317
Ben Cahill77626352007-11-29 11:09:44 +08002318 /* Override next rate if needed for debug purposes */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002319 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Ben Cahill77626352007-11-29 11:09:44 +08002320
2321 /* Fill next table entry */
Zhu Yib481de92007-09-25 17:54:57 -07002322 lq_cmd->rs_table[index].rate_n_flags =
Guy Cohen39e88502008-04-23 17:14:57 -07002323 cpu_to_le32(new_rate);
Zhu Yi1b696de2007-09-27 11:27:41 +08002324 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002325 index++;
2326 }
2327
Guy Cohen39e88502008-04-23 17:14:57 -07002328 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band, &tbl_type,
Zhu Yib481de92007-09-25 17:54:57 -07002329 &rate_idx);
2330
Ben Cahill77626352007-11-29 11:09:44 +08002331 /* Indicate to uCode which entries might be MIMO.
2332 * If initial rate was MIMO, this will finally end up
2333 * as (IWL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
Zhu Yib481de92007-09-25 17:54:57 -07002334 if (is_mimo(tbl_type.lq_type))
2335 lq_cmd->general_params.mimo_delimiter = index;
2336
Ben Cahill77626352007-11-29 11:09:44 +08002337 /* Get next rate */
Guy Cohen39e88502008-04-23 17:14:57 -07002338 new_rate = rs_get_lower_rate(lq_sta, &tbl_type, rate_idx,
2339 use_ht_possible);
Zhu Yib481de92007-09-25 17:54:57 -07002340
Ben Cahill77626352007-11-29 11:09:44 +08002341 /* How many times should we repeat the next rate? */
Zhu Yib481de92007-09-25 17:54:57 -07002342 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07002343 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2344 ant_toggle_cnt++;
2345 else if (priv &&
2346 rs_toggle_antenna(valid_tx_ant,
2347 &new_rate, &tbl_type))
2348 ant_toggle_cnt = 1;
2349
Zhu Yi1b696de2007-09-27 11:27:41 +08002350 repeat_rate = IWL_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002351 } else {
Zhu Yi1b696de2007-09-27 11:27:41 +08002352 repeat_rate = IWL_HT_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002353 }
Zhu Yib481de92007-09-25 17:54:57 -07002354
Ben Cahill77626352007-11-29 11:09:44 +08002355 /* Don't allow HT rates after next pass.
2356 * rs_get_lower_rate() will change type to LQ_A or LQ_G. */
Zhu Yib481de92007-09-25 17:54:57 -07002357 use_ht_possible = 0;
2358
Ben Cahill77626352007-11-29 11:09:44 +08002359 /* Override next rate if needed for debug purposes */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002360 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Ben Cahill77626352007-11-29 11:09:44 +08002361
2362 /* Fill next table entry */
Guy Cohen39e88502008-04-23 17:14:57 -07002363 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002364
2365 index++;
Zhu Yi1b696de2007-09-27 11:27:41 +08002366 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002367 }
2368
Tomas Winkler5e1dd8d2008-05-29 16:35:17 +08002369 lq_cmd->agg_params.agg_frame_cnt_limit = 64;
Zhu Yib481de92007-09-25 17:54:57 -07002370 lq_cmd->agg_params.agg_dis_start_th = 3;
2371 lq_cmd->agg_params.agg_time_limit = cpu_to_le16(4000);
Zhu Yib481de92007-09-25 17:54:57 -07002372}
2373
Johannes Berg4b7679a2008-09-18 18:14:18 +02002374static void *rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
Zhu Yib481de92007-09-25 17:54:57 -07002375{
Johannes Berg4b7679a2008-09-18 18:14:18 +02002376 return hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07002377}
2378/* rate scale requires free function to be implemented */
2379static void rs_free(void *priv_rate)
2380{
2381 return;
2382}
2383
Johannes Berg4b7679a2008-09-18 18:14:18 +02002384static void rs_free_sta(void *priv_r, struct ieee80211_sta *sta,
2385 void *priv_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002386{
Tomas Winklere227cea2008-07-18 13:53:05 +08002387 struct iwl_lq_sta *lq_sta = priv_sta;
Rami Rosen45527c22008-10-07 09:50:01 +02002388 struct iwl_priv *priv __maybe_unused = priv_r;
Zhu Yib481de92007-09-25 17:54:57 -07002389
2390 IWL_DEBUG_RATE("enter\n");
Tomas Winklerc33104f2008-01-14 17:46:21 -08002391 kfree(lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002392 IWL_DEBUG_RATE("leave\n");
2393}
2394
2395
Zhu Yi93dc6462007-09-27 11:27:37 +08002396#ifdef CONFIG_MAC80211_DEBUGFS
Zhu Yi5ae212c2007-09-27 11:27:38 +08002397static int open_file_generic(struct inode *inode, struct file *file)
2398{
2399 file->private_data = inode->i_private;
2400 return 0;
2401}
Tomas Winklere227cea2008-07-18 13:53:05 +08002402static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
2403 u32 *rate_n_flags, int index)
Zhu Yi98d7e092007-09-27 11:27:42 +08002404{
Ester Kummerbf403db2008-05-05 10:22:40 +08002405 struct iwl_priv *priv;
2406
2407 priv = lq_sta->drv;
Guy Cohen39e88502008-04-23 17:14:57 -07002408 if (lq_sta->dbg_fixed_rate) {
2409 if (index < 12) {
2410 *rate_n_flags = lq_sta->dbg_fixed_rate;
2411 } else {
2412 if (lq_sta->band == IEEE80211_BAND_5GHZ)
2413 *rate_n_flags = 0x800D;
2414 else
2415 *rate_n_flags = 0x820A;
2416 }
Zhu Yi98d7e092007-09-27 11:27:42 +08002417 IWL_DEBUG_RATE("Fixed rate ON\n");
Guy Cohen39e88502008-04-23 17:14:57 -07002418 } else {
2419 IWL_DEBUG_RATE("Fixed rate OFF\n");
Zhu Yi98d7e092007-09-27 11:27:42 +08002420 }
Zhu Yi98d7e092007-09-27 11:27:42 +08002421}
2422
2423static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
2424 const char __user *user_buf, size_t count, loff_t *ppos)
2425{
Tomas Winklere227cea2008-07-18 13:53:05 +08002426 struct iwl_lq_sta *lq_sta = file->private_data;
Ester Kummerbf403db2008-05-05 10:22:40 +08002427 struct iwl_priv *priv;
Zhu Yi98d7e092007-09-27 11:27:42 +08002428 char buf[64];
2429 int buf_size;
2430 u32 parsed_rate;
2431
Ester Kummerbf403db2008-05-05 10:22:40 +08002432 priv = lq_sta->drv;
Zhu Yi98d7e092007-09-27 11:27:42 +08002433 memset(buf, 0, sizeof(buf));
2434 buf_size = min(count, sizeof(buf) - 1);
2435 if (copy_from_user(buf, user_buf, buf_size))
2436 return -EFAULT;
2437
2438 if (sscanf(buf, "%x", &parsed_rate) == 1)
Guy Cohen39e88502008-04-23 17:14:57 -07002439 lq_sta->dbg_fixed_rate = parsed_rate;
Zhu Yi98d7e092007-09-27 11:27:42 +08002440 else
Guy Cohen39e88502008-04-23 17:14:57 -07002441 lq_sta->dbg_fixed_rate = 0;
Zhu Yi98d7e092007-09-27 11:27:42 +08002442
Guy Cohen39e88502008-04-23 17:14:57 -07002443 lq_sta->active_legacy_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
2444 lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2445 lq_sta->active_mimo2_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2446 lq_sta->active_mimo3_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
Zhu Yi98d7e092007-09-27 11:27:42 +08002447
2448 IWL_DEBUG_RATE("sta_id %d rate 0x%X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07002449 lq_sta->lq.sta_id, lq_sta->dbg_fixed_rate);
Zhu Yi98d7e092007-09-27 11:27:42 +08002450
Guy Cohen39e88502008-04-23 17:14:57 -07002451 if (lq_sta->dbg_fixed_rate) {
Guy Cohen07bc28e2008-04-23 17:15:03 -07002452 rs_fill_link_cmd(NULL, lq_sta, lq_sta->dbg_fixed_rate);
Tomas Winkler66c73db2008-04-15 16:01:40 -07002453 iwl_send_lq_cmd(lq_sta->drv, &lq_sta->lq, CMD_ASYNC);
Zhu Yi98d7e092007-09-27 11:27:42 +08002454 }
2455
2456 return count;
2457}
Zhu Yi0209dc12007-09-27 11:27:43 +08002458
Zhu Yi5ae212c2007-09-27 11:27:38 +08002459static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
2460 char __user *user_buf, size_t count, loff_t *ppos)
2461{
2462 char buff[1024];
2463 int desc = 0;
2464 int i = 0;
2465
Tomas Winklere227cea2008-07-18 13:53:05 +08002466 struct iwl_lq_sta *lq_sta = file->private_data;
Zhu Yi5ae212c2007-09-27 11:27:38 +08002467
Tomas Winklerc33104f2008-01-14 17:46:21 -08002468 desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
Zhu Yi98d7e092007-09-27 11:27:42 +08002469 desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002470 lq_sta->total_failed, lq_sta->total_success,
Guy Cohen39e88502008-04-23 17:14:57 -07002471 lq_sta->active_legacy_rate);
Zhu Yi98d7e092007-09-27 11:27:42 +08002472 desc += sprintf(buff+desc, "fixed rate 0x%X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07002473 lq_sta->dbg_fixed_rate);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002474 desc += sprintf(buff+desc, "general:"
2475 "flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002476 lq_sta->lq.general_params.flags,
2477 lq_sta->lq.general_params.mimo_delimiter,
2478 lq_sta->lq.general_params.single_stream_ant_msk,
2479 lq_sta->lq.general_params.dual_stream_ant_msk);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002480
2481 desc += sprintf(buff+desc, "agg:"
2482 "time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002483 le16_to_cpu(lq_sta->lq.agg_params.agg_time_limit),
2484 lq_sta->lq.agg_params.agg_dis_start_th,
2485 lq_sta->lq.agg_params.agg_frame_cnt_limit);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002486
2487 desc += sprintf(buff+desc,
2488 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002489 lq_sta->lq.general_params.start_rate_index[0],
2490 lq_sta->lq.general_params.start_rate_index[1],
2491 lq_sta->lq.general_params.start_rate_index[2],
2492 lq_sta->lq.general_params.start_rate_index[3]);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002493
2494
2495 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
2496 desc += sprintf(buff+desc, " rate[%d] 0x%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002497 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags));
Zhu Yi5ae212c2007-09-27 11:27:38 +08002498
2499 return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2500}
2501
2502static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
Zhu Yi98d7e092007-09-27 11:27:42 +08002503 .write = rs_sta_dbgfs_scale_table_write,
Zhu Yi5ae212c2007-09-27 11:27:38 +08002504 .read = rs_sta_dbgfs_scale_table_read,
2505 .open = open_file_generic,
2506};
Zhu Yi0209dc12007-09-27 11:27:43 +08002507static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
2508 char __user *user_buf, size_t count, loff_t *ppos)
2509{
2510 char buff[1024];
2511 int desc = 0;
2512 int i, j;
2513
Tomas Winklere227cea2008-07-18 13:53:05 +08002514 struct iwl_lq_sta *lq_sta = file->private_data;
Zhu Yi0209dc12007-09-27 11:27:43 +08002515 for (i = 0; i < LQ_SIZE; i++) {
2516 desc += sprintf(buff+desc, "%s type=%d SGI=%d FAT=%d DUP=%d\n"
2517 "rate=0x%X\n",
Abhijeet Kolekarc3056062008-11-12 13:14:08 -08002518 lq_sta->active_tbl == i ? "*" : "x",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002519 lq_sta->lq_info[i].lq_type,
2520 lq_sta->lq_info[i].is_SGI,
2521 lq_sta->lq_info[i].is_fat,
2522 lq_sta->lq_info[i].is_dup,
Guy Cohen39e88502008-04-23 17:14:57 -07002523 lq_sta->lq_info[i].current_rate);
Zhu Yi0209dc12007-09-27 11:27:43 +08002524 for (j = 0; j < IWL_RATE_COUNT; j++) {
2525 desc += sprintf(buff+desc,
Tomas Winklerc33104f2008-01-14 17:46:21 -08002526 "counter=%d success=%d %%=%d\n",
2527 lq_sta->lq_info[i].win[j].counter,
2528 lq_sta->lq_info[i].win[j].success_counter,
2529 lq_sta->lq_info[i].win[j].success_ratio);
Zhu Yi0209dc12007-09-27 11:27:43 +08002530 }
2531 }
2532 return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2533}
2534
2535static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
2536 .read = rs_sta_dbgfs_stats_table_read,
2537 .open = open_file_generic,
2538};
Zhu Yi5ae212c2007-09-27 11:27:38 +08002539
Zhu Yi93dc6462007-09-27 11:27:37 +08002540static void rs_add_debugfs(void *priv, void *priv_sta,
2541 struct dentry *dir)
2542{
Tomas Winklere227cea2008-07-18 13:53:05 +08002543 struct iwl_lq_sta *lq_sta = priv_sta;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002544 lq_sta->rs_sta_dbgfs_scale_table_file =
Zhu Yi0209dc12007-09-27 11:27:43 +08002545 debugfs_create_file("rate_scale_table", 0600, dir,
Tomas Winklerc33104f2008-01-14 17:46:21 -08002546 lq_sta, &rs_sta_dbgfs_scale_table_ops);
2547 lq_sta->rs_sta_dbgfs_stats_table_file =
Zhu Yi0209dc12007-09-27 11:27:43 +08002548 debugfs_create_file("rate_stats_table", 0600, dir,
Tomas Winklerc33104f2008-01-14 17:46:21 -08002549 lq_sta, &rs_sta_dbgfs_stats_table_ops);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002550 lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file =
2551 debugfs_create_u8("tx_agg_tid_enable", 0600, dir,
2552 &lq_sta->tx_agg_tid_en);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002553
Zhu Yi93dc6462007-09-27 11:27:37 +08002554}
2555
2556static void rs_remove_debugfs(void *priv, void *priv_sta)
2557{
Tomas Winklere227cea2008-07-18 13:53:05 +08002558 struct iwl_lq_sta *lq_sta = priv_sta;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002559 debugfs_remove(lq_sta->rs_sta_dbgfs_scale_table_file);
2560 debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002561 debugfs_remove(lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file);
Zhu Yi93dc6462007-09-27 11:27:37 +08002562}
2563#endif
2564
Zhu Yib481de92007-09-25 17:54:57 -07002565static struct rate_control_ops rs_ops = {
2566 .module = NULL,
2567 .name = RS_NAME,
2568 .tx_status = rs_tx_status,
2569 .get_rate = rs_get_rate,
2570 .rate_init = rs_rate_init,
Zhu Yib481de92007-09-25 17:54:57 -07002571 .alloc = rs_alloc,
2572 .free = rs_free,
2573 .alloc_sta = rs_alloc_sta,
2574 .free_sta = rs_free_sta,
Zhu Yi93dc6462007-09-27 11:27:37 +08002575#ifdef CONFIG_MAC80211_DEBUGFS
2576 .add_sta_debugfs = rs_add_debugfs,
2577 .remove_sta_debugfs = rs_remove_debugfs,
2578#endif
Zhu Yib481de92007-09-25 17:54:57 -07002579};
2580
Tomas Winklere227cea2008-07-18 13:53:05 +08002581int iwlagn_rate_control_register(void)
Zhu Yib481de92007-09-25 17:54:57 -07002582{
Reinette Chatre897e1cf2008-03-28 16:21:09 -07002583 return ieee80211_rate_control_register(&rs_ops);
Zhu Yib481de92007-09-25 17:54:57 -07002584}
2585
Tomas Winklere227cea2008-07-18 13:53:05 +08002586void iwlagn_rate_control_unregister(void)
Zhu Yib481de92007-09-25 17:54:57 -07002587{
2588 ieee80211_rate_control_unregister(&rs_ops);
2589}
2590