blob: 7993a1d8302530f714bd5df36ce71c05b2fe1aae [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:
22 * James P. Ketrenos <ipw2100-admin@linux.intel.com>
23 * 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
Johannes Berg2c8dccc2008-04-08 15:14:40 -040038#include "../net/mac80211/rate.h"
Zhu Yib481de92007-09-25 17:54:57 -070039
Tomas Winkler3e0d4cb2008-04-24 11:55:38 -070040#include "iwl-dev.h"
Tomas Winkler857485c2008-03-21 13:53:44 -070041#include "iwl-core.h"
Zhu Yib481de92007-09-25 17:54:57 -070042#include "iwl-helpers.h"
43
44#define RS_NAME "iwl-4965-rs"
45
Guy Cohenaade00c2008-04-23 17:14:59 -070046#define NUM_TRY_BEFORE_ANT_TOGGLE 1
Zhu Yib481de92007-09-25 17:54:57 -070047#define IWL_NUMBER_TRY 1
48#define IWL_HT_NUMBER_TRY 3
49
Ben Cahill77626352007-11-29 11:09:44 +080050#define IWL_RATE_MAX_WINDOW 62 /* # tx in history window */
51#define IWL_RATE_MIN_FAILURE_TH 6 /* min failures to calc tpt */
52#define IWL_RATE_MIN_SUCCESS_TH 8 /* min successes to calc tpt */
53
54/* max time to accum history 2 seconds */
55#define IWL_RATE_SCALE_FLUSH_INTVL (2*HZ)
Zhu Yib481de92007-09-25 17:54:57 -070056
57static u8 rs_ht_to_legacy[] = {
58 IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
59 IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
60 IWL_RATE_6M_INDEX,
61 IWL_RATE_6M_INDEX, IWL_RATE_9M_INDEX,
62 IWL_RATE_12M_INDEX, IWL_RATE_18M_INDEX,
63 IWL_RATE_24M_INDEX, IWL_RATE_36M_INDEX,
64 IWL_RATE_48M_INDEX, IWL_RATE_54M_INDEX
65};
66
Guy Cohenaade00c2008-04-23 17:14:59 -070067static const u8 ant_toggle_lookup[] = {
68 /*ANT_NONE -> */ ANT_NONE,
69 /*ANT_A -> */ ANT_B,
70 /*ANT_B -> */ ANT_C,
71 /*ANT_AB -> */ ANT_BC,
72 /*ANT_C -> */ ANT_A,
73 /*ANT_AC -> */ ANT_AB,
74 /*ANT_BC -> */ ANT_AC,
75 /*ANT_ABC -> */ ANT_ABC,
76};
77
Ben Cahill77626352007-11-29 11:09:44 +080078/**
79 * struct iwl4965_rate_scale_data -- tx success history for one rate
80 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080081struct iwl4965_rate_scale_data {
Ben Cahill77626352007-11-29 11:09:44 +080082 u64 data; /* bitmap of successful frames */
83 s32 success_counter; /* number of frames successful */
84 s32 success_ratio; /* per-cent * 128 */
85 s32 counter; /* number of frames attempted */
86 s32 average_tpt; /* success ratio * expected throughput */
Zhu Yib481de92007-09-25 17:54:57 -070087 unsigned long stamp;
88};
89
Ben Cahill77626352007-11-29 11:09:44 +080090/**
91 * struct iwl4965_scale_tbl_info -- tx params and success history for all rates
92 *
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +020093 * There are two of these in struct iwl4965_lq_sta,
Ben Cahill77626352007-11-29 11:09:44 +080094 * one for "active", and one for "search".
95 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080096struct iwl4965_scale_tbl_info {
Guy Cohenfde0db32008-04-21 15:42:01 -070097 enum iwl_table_type lq_type;
98 u8 ant_type;
Ben Cahill77626352007-11-29 11:09:44 +080099 u8 is_SGI; /* 1 = short guard interval */
100 u8 is_fat; /* 1 = 40 MHz channel width */
101 u8 is_dup; /* 1 = duplicated data streams */
102 u8 action; /* change modulation; IWL_[LEGACY/SISO/MIMO]_SWITCH_* */
103 s32 *expected_tpt; /* throughput metrics; expected_tpt_G, etc. */
Guy Cohen39e88502008-04-23 17:14:57 -0700104 u32 current_rate; /* rate_n_flags, uCode API format */
Ben Cahill77626352007-11-29 11:09:44 +0800105 struct iwl4965_rate_scale_data win[IWL_RATE_COUNT]; /* rate histories */
Zhu Yib481de92007-09-25 17:54:57 -0700106};
107
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200108#ifdef CONFIG_IWL4965_HT
109
110struct iwl4965_traffic_load {
111 unsigned long time_stamp; /* age of the oldest statistics */
112 u32 packet_count[TID_QUEUE_MAX_SIZE]; /* packet count in this time
113 * slice */
114 u32 total; /* total num of packets during the
115 * last TID_MAX_TIME_DIFF */
116 u8 queue_count; /* number of queues that has
117 * been used since the last cleanup */
118 u8 head; /* start of the circular buffer */
119};
120
121#endif /* CONFIG_IWL4965_HT */
122
Ben Cahill77626352007-11-29 11:09:44 +0800123/**
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200124 * struct iwl4965_lq_sta -- driver's rate scaling private structure
Ben Cahill77626352007-11-29 11:09:44 +0800125 *
126 * Pointer to this gets passed back and forth between driver and mac80211.
127 */
Tomas Winklerc33104f2008-01-14 17:46:21 -0800128struct iwl4965_lq_sta {
Ben Cahill77626352007-11-29 11:09:44 +0800129 u8 active_tbl; /* index of active table, range 0-1 */
130 u8 enable_counter; /* indicates HT mode */
131 u8 stay_in_tbl; /* 1: disallow, 0: allow search for new mode */
132 u8 search_better_tbl; /* 1: currently trying alternate mode */
Zhu Yib481de92007-09-25 17:54:57 -0700133 s32 last_tpt;
Ben Cahill77626352007-11-29 11:09:44 +0800134
135 /* The following determine when to search for a new mode */
Zhu Yib481de92007-09-25 17:54:57 -0700136 u32 table_count_limit;
Ben Cahill77626352007-11-29 11:09:44 +0800137 u32 max_failure_limit; /* # failed frames before new search */
138 u32 max_success_limit; /* # successful frames before new search */
Zhu Yib481de92007-09-25 17:54:57 -0700139 u32 table_count;
Ben Cahill77626352007-11-29 11:09:44 +0800140 u32 total_failed; /* total failed frames, any/all rates */
141 u32 total_success; /* total successful frames, any/all rates */
142 u32 flush_timer; /* time staying in mode before new search */
143
144 u8 action_counter; /* # mode-switch actions tried */
Zhu Yib481de92007-09-25 17:54:57 -0700145 u8 is_green;
146 u8 is_dup;
Johannes Berg8318d782008-01-24 19:38:38 +0100147 enum ieee80211_band band;
Zhu Yib481de92007-09-25 17:54:57 -0700148 u8 ibss_sta_added;
Ben Cahill77626352007-11-29 11:09:44 +0800149
150 /* The following are bitmaps of rates; IWL_RATE_6M_MASK, etc. */
Zhu Yi02dede02007-09-27 11:27:40 +0800151 u32 supp_rates;
Guy Cohen39e88502008-04-23 17:14:57 -0700152 u16 active_legacy_rate;
Zhu Yib481de92007-09-25 17:54:57 -0700153 u16 active_siso_rate;
Guy Cohenfde0db32008-04-21 15:42:01 -0700154 u16 active_mimo2_rate;
155 u16 active_mimo3_rate;
Zhu Yib481de92007-09-25 17:54:57 -0700156 u16 active_rate_basic;
Ben Cahill77626352007-11-29 11:09:44 +0800157
Tomas Winkler66c73db2008-04-15 16:01:40 -0700158 struct iwl_link_quality_cmd lq;
Ben Cahill77626352007-11-29 11:09:44 +0800159 struct iwl4965_scale_tbl_info lq_info[LQ_SIZE]; /* "active", "search" */
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200160#ifdef CONFIG_IWL4965_HT
161 struct iwl4965_traffic_load load[TID_MAX_LOAD_COUNT];
162 u8 tx_agg_tid_en;
163#endif
Zhu Yi5ae212c2007-09-27 11:27:38 +0800164#ifdef CONFIG_MAC80211_DEBUGFS
Zhu Yi98d7e092007-09-27 11:27:42 +0800165 struct dentry *rs_sta_dbgfs_scale_table_file;
Zhu Yi0209dc12007-09-27 11:27:43 +0800166 struct dentry *rs_sta_dbgfs_stats_table_file;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200167#ifdef CONFIG_IWL4965_HT
168 struct dentry *rs_sta_dbgfs_tx_agg_tid_en_file;
169#endif
Guy Cohen39e88502008-04-23 17:14:57 -0700170 u32 dbg_fixed_rate;
Zhu Yi5ae212c2007-09-27 11:27:38 +0800171#endif
Zhu Yi62430652008-05-05 10:22:46 +0800172 struct iwl_priv *drv;
Zhu Yib481de92007-09-25 17:54:57 -0700173};
174
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700175static void rs_rate_scale_perform(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700176 struct net_device *dev,
177 struct ieee80211_hdr *hdr,
178 struct sta_info *sta);
Guy Cohenfde0db32008-04-21 15:42:01 -0700179static void rs_fill_link_cmd(const struct iwl_priv *priv,
180 struct iwl4965_lq_sta *lq_sta,
Guy Cohen07bc28e2008-04-23 17:15:03 -0700181 u32 rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -0700182
183
Zhu Yi98d7e092007-09-27 11:27:42 +0800184#ifdef CONFIG_MAC80211_DEBUGFS
Tomas Winklerc33104f2008-01-14 17:46:21 -0800185static void rs_dbgfs_set_mcs(struct iwl4965_lq_sta *lq_sta,
Guy Cohen39e88502008-04-23 17:14:57 -0700186 u32 *rate_n_flags, int index);
Zhu Yi98d7e092007-09-27 11:27:42 +0800187#else
Tomas Winklerc33104f2008-01-14 17:46:21 -0800188static void rs_dbgfs_set_mcs(struct iwl4965_lq_sta *lq_sta,
Guy Cohen39e88502008-04-23 17:14:57 -0700189 u32 *rate_n_flags, int index)
Zhu Yi98d7e092007-09-27 11:27:42 +0800190{}
191#endif
Ben Cahill77626352007-11-29 11:09:44 +0800192
193/*
194 * Expected throughput metrics for following rates:
195 * 1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
196 * "G" is the only table that supports CCK (the first 4 rates).
197 */
Guy Cohenfde0db32008-04-21 15:42:01 -0700198/*FIXME:RS:need to spearate tables for MIMO2/MIMO3*/
Zhu Yib481de92007-09-25 17:54:57 -0700199static s32 expected_tpt_A[IWL_RATE_COUNT] = {
200 0, 0, 0, 0, 40, 57, 72, 98, 121, 154, 177, 186, 186
201};
202
203static s32 expected_tpt_G[IWL_RATE_COUNT] = {
204 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 186
205};
206
207static s32 expected_tpt_siso20MHz[IWL_RATE_COUNT] = {
208 0, 0, 0, 0, 42, 42, 76, 102, 124, 159, 183, 193, 202
209};
210
211static s32 expected_tpt_siso20MHzSGI[IWL_RATE_COUNT] = {
212 0, 0, 0, 0, 46, 46, 82, 110, 132, 168, 192, 202, 211
213};
214
215static s32 expected_tpt_mimo20MHz[IWL_RATE_COUNT] = {
216 0, 0, 0, 0, 74, 74, 123, 155, 179, 214, 236, 244, 251
217};
218
219static s32 expected_tpt_mimo20MHzSGI[IWL_RATE_COUNT] = {
220 0, 0, 0, 0, 81, 81, 131, 164, 188, 222, 243, 251, 257
221};
222
223static s32 expected_tpt_siso40MHz[IWL_RATE_COUNT] = {
224 0, 0, 0, 0, 77, 77, 127, 160, 184, 220, 242, 250, 257
225};
226
227static s32 expected_tpt_siso40MHzSGI[IWL_RATE_COUNT] = {
228 0, 0, 0, 0, 83, 83, 135, 169, 193, 229, 250, 257, 264
229};
230
231static s32 expected_tpt_mimo40MHz[IWL_RATE_COUNT] = {
232 0, 0, 0, 0, 123, 123, 182, 214, 235, 264, 279, 285, 289
233};
234
235static s32 expected_tpt_mimo40MHzSGI[IWL_RATE_COUNT] = {
236 0, 0, 0, 0, 131, 131, 191, 222, 242, 270, 284, 289, 293
237};
238
Guy Cohen39e88502008-04-23 17:14:57 -0700239static inline u8 rs_extract_rate(u32 rate_n_flags)
Zhu Yib481de92007-09-25 17:54:57 -0700240{
241 return (u8)(rate_n_flags & 0xFF);
242}
243
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800244static void rs_rate_scale_clear_window(struct iwl4965_rate_scale_data *window)
Zhu Yib481de92007-09-25 17:54:57 -0700245{
246 window->data = 0;
247 window->success_counter = 0;
248 window->success_ratio = IWL_INVALID_VALUE;
249 window->counter = 0;
250 window->average_tpt = IWL_INVALID_VALUE;
251 window->stamp = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700252}
253
Guy Cohenaade00c2008-04-23 17:14:59 -0700254static inline u8 rs_is_valid_ant(u8 valid_antenna, u8 ant_type)
255{
256 return ((ant_type & valid_antenna) == ant_type);
257}
258
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200259#ifdef CONFIG_IWL4965_HT
260/*
261 * removes the old data from the statistics. All data that is older than
262 * TID_MAX_TIME_DIFF, will be deleted.
263 */
264static void rs_tl_rm_old_stats(struct iwl4965_traffic_load *tl, u32 curr_time)
265{
266 /* The oldest age we want to keep */
267 u32 oldest_time = curr_time - TID_MAX_TIME_DIFF;
268
269 while (tl->queue_count &&
270 (tl->time_stamp < oldest_time)) {
271 tl->total -= tl->packet_count[tl->head];
272 tl->packet_count[tl->head] = 0;
273 tl->time_stamp += TID_QUEUE_CELL_SPACING;
274 tl->queue_count--;
275 tl->head++;
276 if (tl->head >= TID_QUEUE_MAX_SIZE)
277 tl->head = 0;
278 }
279}
280
281/*
282 * increment traffic load value for tid and also remove
283 * any old values if passed the certain time period
284 */
Tomas Winkler54dbb522008-05-15 13:54:06 +0800285static void rs_tl_add_packet(struct iwl4965_lq_sta *lq_data,
286 struct ieee80211_hdr *hdr)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200287{
288 u32 curr_time = jiffies_to_msecs(jiffies);
289 u32 time_diff;
290 s32 index;
291 struct iwl4965_traffic_load *tl = NULL;
Tomas Winkler54dbb522008-05-15 13:54:06 +0800292 u16 fc = le16_to_cpu(hdr->frame_control);
293 u8 tid;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200294
Tomas Winkler54dbb522008-05-15 13:54:06 +0800295 if (ieee80211_is_qos_data(fc)) {
296 u8 *qc = ieee80211_get_qos_ctrl(hdr, ieee80211_get_hdrlen(fc));
297 tid = qc[0] & 0xf;
298 } else
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200299 return;
300
301 tl = &lq_data->load[tid];
302
303 curr_time -= curr_time % TID_ROUND_VALUE;
304
305 /* Happens only for the first packet. Initialize the data */
306 if (!(tl->queue_count)) {
307 tl->total = 1;
308 tl->time_stamp = curr_time;
309 tl->queue_count = 1;
310 tl->head = 0;
311 tl->packet_count[0] = 1;
312 return;
313 }
314
315 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
316 index = time_diff / TID_QUEUE_CELL_SPACING;
317
318 /* The history is too long: remove data that is older than */
319 /* TID_MAX_TIME_DIFF */
320 if (index >= TID_QUEUE_MAX_SIZE)
321 rs_tl_rm_old_stats(tl, curr_time);
322
323 index = (tl->head + index) % TID_QUEUE_MAX_SIZE;
324 tl->packet_count[index] = tl->packet_count[index] + 1;
325 tl->total = tl->total + 1;
326
327 if ((index + 1) > tl->queue_count)
328 tl->queue_count = index + 1;
329}
330
331/*
332 get the traffic load value for tid
333*/
334static u32 rs_tl_get_load(struct iwl4965_lq_sta *lq_data, u8 tid)
335{
336 u32 curr_time = jiffies_to_msecs(jiffies);
337 u32 time_diff;
338 s32 index;
339 struct iwl4965_traffic_load *tl = NULL;
340
341 if (tid >= TID_MAX_LOAD_COUNT)
342 return 0;
343
344 tl = &(lq_data->load[tid]);
345
346 curr_time -= curr_time % TID_ROUND_VALUE;
347
348 if (!(tl->queue_count))
349 return 0;
350
351 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
352 index = time_diff / TID_QUEUE_CELL_SPACING;
353
354 /* The history is too long: remove data that is older than */
355 /* TID_MAX_TIME_DIFF */
356 if (index >= TID_QUEUE_MAX_SIZE)
357 rs_tl_rm_old_stats(tl, curr_time);
358
359 return tl->total;
360}
361
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700362static void rs_tl_turn_on_agg_for_tid(struct iwl_priv *priv,
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200363 struct iwl4965_lq_sta *lq_data, u8 tid,
364 struct sta_info *sta)
365{
366 unsigned long state;
367 DECLARE_MAC_BUF(mac);
368
Johannes Berg07346f812008-05-03 01:02:02 +0200369 spin_lock_bh(&sta->lock);
Ron Rindjunskycee24a32008-03-26 20:36:03 +0200370 state = sta->ampdu_mlme.tid_state_tx[tid];
Johannes Berg07346f812008-05-03 01:02:02 +0200371 spin_unlock_bh(&sta->lock);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200372
373 if (state == HT_AGG_STATE_IDLE &&
374 rs_tl_get_load(lq_data, tid) > IWL_AGG_LOAD_THRESHOLD) {
375 IWL_DEBUG_HT("Starting Tx agg: STA: %s tid: %d\n",
376 print_mac(mac, sta->addr), tid);
377 ieee80211_start_tx_ba_session(priv->hw, sta->addr, tid);
378 }
379}
380
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700381static void rs_tl_turn_on_agg(struct iwl_priv *priv, u8 tid,
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200382 struct iwl4965_lq_sta *lq_data,
383 struct sta_info *sta)
384{
385 if ((tid < TID_MAX_LOAD_COUNT))
386 rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
387 else if (tid == IWL_AGG_ALL_TID)
388 for (tid = 0; tid < TID_MAX_LOAD_COUNT; tid++)
389 rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
390}
391
392#endif /* CONFIG_IWLWIFI_HT */
393
Guy Cohen39e88502008-04-23 17:14:57 -0700394static inline int get_num_of_ant_from_rate(u32 rate_n_flags)
Guy Cohenfde0db32008-04-21 15:42:01 -0700395{
Guy Cohen39e88502008-04-23 17:14:57 -0700396 return (!!(rate_n_flags & RATE_MCS_ANT_A_MSK) +
397 !!(rate_n_flags & RATE_MCS_ANT_B_MSK) +
398 !!(rate_n_flags & RATE_MCS_ANT_C_MSK));
Guy Cohenfde0db32008-04-21 15:42:01 -0700399}
400
Ben Cahill77626352007-11-29 11:09:44 +0800401/**
402 * rs_collect_tx_data - Update the success/failure sliding window
403 *
404 * We keep a sliding window of the last 62 packets transmitted
405 * at this rate. window->data contains the bitmask of successful
406 * packets.
407 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800408static int rs_collect_tx_data(struct iwl4965_rate_scale_data *windows,
Ron Rindjunsky99556432008-01-28 14:07:25 +0200409 int scale_index, s32 tpt, int retries,
410 int successes)
Zhu Yib481de92007-09-25 17:54:57 -0700411{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800412 struct iwl4965_rate_scale_data *window = NULL;
Guy Cohen39e88502008-04-23 17:14:57 -0700413 static const u64 mask = (((u64)1) << (IWL_RATE_MAX_WINDOW - 1));
Zhu Yib481de92007-09-25 17:54:57 -0700414 s32 fail_count;
415
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800416 if (scale_index < 0 || scale_index >= IWL_RATE_COUNT)
417 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -0700418
Ben Cahill77626352007-11-29 11:09:44 +0800419 /* Select data for current tx bit rate */
Zhu Yib481de92007-09-25 17:54:57 -0700420 window = &(windows[scale_index]);
421
Ben Cahill77626352007-11-29 11:09:44 +0800422 /*
423 * Keep track of only the latest 62 tx frame attempts in this rate's
424 * history window; anything older isn't really relevant any more.
425 * If we have filled up the sliding window, drop the oldest attempt;
426 * if the oldest attempt (highest bit in bitmap) shows "success",
427 * subtract "1" from the success counter (this is the main reason
428 * we keep these bitmaps!).
429 */
Ron Rindjunsky99556432008-01-28 14:07:25 +0200430 while (retries > 0) {
Guy Cohen39e88502008-04-23 17:14:57 -0700431 if (window->counter >= IWL_RATE_MAX_WINDOW) {
432
433 /* remove earliest */
434 window->counter = IWL_RATE_MAX_WINDOW - 1;
435
Ron Rindjunsky99556432008-01-28 14:07:25 +0200436 if (window->data & mask) {
437 window->data &= ~mask;
Guy Cohen39e88502008-04-23 17:14:57 -0700438 window->success_counter--;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200439 }
Zhu Yib481de92007-09-25 17:54:57 -0700440 }
Zhu Yib481de92007-09-25 17:54:57 -0700441
Ron Rindjunsky99556432008-01-28 14:07:25 +0200442 /* Increment frames-attempted counter */
443 window->counter++;
Ben Cahill77626352007-11-29 11:09:44 +0800444
Ron Rindjunsky99556432008-01-28 14:07:25 +0200445 /* Shift bitmap by one frame (throw away oldest history),
446 * OR in "1", and increment "success" if this
447 * frame was successful. */
Guy Cohen39e88502008-04-23 17:14:57 -0700448 window->data <<= 1;;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200449 if (successes > 0) {
Guy Cohen39e88502008-04-23 17:14:57 -0700450 window->success_counter++;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200451 window->data |= 0x1;
452 successes--;
453 }
454
455 retries--;
Zhu Yib481de92007-09-25 17:54:57 -0700456 }
457
Ben Cahill77626352007-11-29 11:09:44 +0800458 /* Calculate current success ratio, avoid divide-by-0! */
Zhu Yib481de92007-09-25 17:54:57 -0700459 if (window->counter > 0)
460 window->success_ratio = 128 * (100 * window->success_counter)
461 / window->counter;
462 else
463 window->success_ratio = IWL_INVALID_VALUE;
464
465 fail_count = window->counter - window->success_counter;
466
Ben Cahill77626352007-11-29 11:09:44 +0800467 /* Calculate average throughput, if we have enough history. */
Zhu Yib481de92007-09-25 17:54:57 -0700468 if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
469 (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
470 window->average_tpt = (window->success_ratio * tpt + 64) / 128;
471 else
472 window->average_tpt = IWL_INVALID_VALUE;
473
Ben Cahill77626352007-11-29 11:09:44 +0800474 /* Tag this window as having been updated */
Zhu Yib481de92007-09-25 17:54:57 -0700475 window->stamp = jiffies;
476
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800477 return 0;
Zhu Yib481de92007-09-25 17:54:57 -0700478}
479
Ben Cahill77626352007-11-29 11:09:44 +0800480/*
481 * Fill uCode API rate_n_flags field, based on "search" or "active" table.
482 */
Guy Cohen39e88502008-04-23 17:14:57 -0700483/* FIXME:RS:remove this function and put the flags statically in the table */
484static u32 rate_n_flags_from_tbl(struct iwl4965_scale_tbl_info *tbl,
485 int index, u8 use_green)
Zhu Yib481de92007-09-25 17:54:57 -0700486{
Guy Cohen39e88502008-04-23 17:14:57 -0700487 u32 rate_n_flags = 0;
488
Zhu Yib481de92007-09-25 17:54:57 -0700489 if (is_legacy(tbl->lq_type)) {
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800490 rate_n_flags = iwl_rates[index].plcp;
Zhu Yib481de92007-09-25 17:54:57 -0700491 if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
Guy Cohen39e88502008-04-23 17:14:57 -0700492 rate_n_flags |= RATE_MCS_CCK_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700493
Guy Cohenfde0db32008-04-21 15:42:01 -0700494 } else if (is_Ht(tbl->lq_type)) {
495 if (index > IWL_LAST_OFDM_RATE) {
496 IWL_ERROR("invalid HT rate index %d\n", index);
Zhu Yib481de92007-09-25 17:54:57 -0700497 index = IWL_LAST_OFDM_RATE;
Guy Cohenfde0db32008-04-21 15:42:01 -0700498 }
Guy Cohen39e88502008-04-23 17:14:57 -0700499 rate_n_flags = RATE_MCS_HT_MSK;
Guy Cohenfde0db32008-04-21 15:42:01 -0700500
501 if (is_siso(tbl->lq_type))
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800502 rate_n_flags |= iwl_rates[index].plcp_siso;
Guy Cohenfde0db32008-04-21 15:42:01 -0700503 else if (is_mimo2(tbl->lq_type))
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800504 rate_n_flags |= iwl_rates[index].plcp_mimo2;
Guy Cohenfde0db32008-04-21 15:42:01 -0700505 else
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800506 rate_n_flags |= iwl_rates[index].plcp_mimo3;
Zhu Yib481de92007-09-25 17:54:57 -0700507 } else {
Guy Cohenfde0db32008-04-21 15:42:01 -0700508 IWL_ERROR("Invalid tbl->lq_type %d\n", tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -0700509 }
510
Guy Cohen39e88502008-04-23 17:14:57 -0700511 rate_n_flags |= ((tbl->ant_type << RATE_MCS_ANT_POS) &
Guy Cohenfde0db32008-04-21 15:42:01 -0700512 RATE_MCS_ANT_ABC_MSK);
Zhu Yib481de92007-09-25 17:54:57 -0700513
Guy Cohen39e88502008-04-23 17:14:57 -0700514 if (is_Ht(tbl->lq_type)) {
515 if (tbl->is_fat) {
516 if (tbl->is_dup)
517 rate_n_flags |= RATE_MCS_DUP_MSK;
518 else
519 rate_n_flags |= RATE_MCS_FAT_MSK;
520 }
521 if (tbl->is_SGI)
522 rate_n_flags |= RATE_MCS_SGI_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700523
Guy Cohen39e88502008-04-23 17:14:57 -0700524 if (use_green) {
525 rate_n_flags |= RATE_MCS_GF_MSK;
526 if (is_siso(tbl->lq_type) && tbl->is_SGI) {
527 rate_n_flags &= ~RATE_MCS_SGI_MSK;
528 IWL_ERROR("GF was set with SGI:SISO\n");
529 }
530 }
Zhu Yib481de92007-09-25 17:54:57 -0700531 }
Guy Cohen39e88502008-04-23 17:14:57 -0700532 return rate_n_flags;
Zhu Yib481de92007-09-25 17:54:57 -0700533}
534
Ben Cahill77626352007-11-29 11:09:44 +0800535/*
536 * Interpret uCode API's rate_n_flags format,
537 * fill "search" or "active" tx mode table.
538 */
Guy Cohen39e88502008-04-23 17:14:57 -0700539static int rs_get_tbl_info_from_mcs(const u32 rate_n_flags,
Johannes Berg8318d782008-01-24 19:38:38 +0100540 enum ieee80211_band band,
541 struct iwl4965_scale_tbl_info *tbl,
Zhu Yib481de92007-09-25 17:54:57 -0700542 int *rate_idx)
543{
Guy Cohen39e88502008-04-23 17:14:57 -0700544 u32 ant_msk = (rate_n_flags & RATE_MCS_ANT_ABC_MSK);
545 u8 num_of_ant = get_num_of_ant_from_rate(rate_n_flags);
546 u8 mcs;
Zhu Yib481de92007-09-25 17:54:57 -0700547
Guy Cohen39e88502008-04-23 17:14:57 -0700548 *rate_idx = iwl4965_hwrate_to_plcp_idx(rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -0700549
Guy Cohenfde0db32008-04-21 15:42:01 -0700550 if (*rate_idx == IWL_RATE_INVALID) {
Zhu Yib481de92007-09-25 17:54:57 -0700551 *rate_idx = -1;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800552 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -0700553 }
Ben Cahill77626352007-11-29 11:09:44 +0800554 tbl->is_SGI = 0; /* default legacy setup */
Zhu Yib481de92007-09-25 17:54:57 -0700555 tbl->is_fat = 0;
556 tbl->is_dup = 0;
Guy Cohenfde0db32008-04-21 15:42:01 -0700557 tbl->ant_type = (ant_msk >> RATE_MCS_ANT_POS);
558 tbl->lq_type = LQ_NONE;
Zhu Yib481de92007-09-25 17:54:57 -0700559
Ben Cahill77626352007-11-29 11:09:44 +0800560 /* legacy rate format */
Guy Cohen39e88502008-04-23 17:14:57 -0700561 if (!(rate_n_flags & RATE_MCS_HT_MSK)) {
Guy Cohenfde0db32008-04-21 15:42:01 -0700562 if (num_of_ant == 1) {
Johannes Berg8318d782008-01-24 19:38:38 +0100563 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700564 tbl->lq_type = LQ_A;
565 else
566 tbl->lq_type = LQ_G;
Zhu Yib481de92007-09-25 17:54:57 -0700567 }
Guy Cohenfde0db32008-04-21 15:42:01 -0700568 /* HT rate format */
Zhu Yib481de92007-09-25 17:54:57 -0700569 } else {
Guy Cohen39e88502008-04-23 17:14:57 -0700570 if (rate_n_flags & RATE_MCS_SGI_MSK)
Zhu Yib481de92007-09-25 17:54:57 -0700571 tbl->is_SGI = 1;
572
Guy Cohen39e88502008-04-23 17:14:57 -0700573 if ((rate_n_flags & RATE_MCS_FAT_MSK) ||
574 (rate_n_flags & RATE_MCS_DUP_MSK))
Zhu Yib481de92007-09-25 17:54:57 -0700575 tbl->is_fat = 1;
576
Guy Cohen39e88502008-04-23 17:14:57 -0700577 if (rate_n_flags & RATE_MCS_DUP_MSK)
Zhu Yib481de92007-09-25 17:54:57 -0700578 tbl->is_dup = 1;
Guy Cohenfde0db32008-04-21 15:42:01 -0700579
Guy Cohen39e88502008-04-23 17:14:57 -0700580 mcs = rs_extract_rate(rate_n_flags);
Guy Cohenfde0db32008-04-21 15:42:01 -0700581
Guy Cohen39e88502008-04-23 17:14:57 -0700582 /* SISO */
583 if (mcs <= IWL_RATE_SISO_60M_PLCP) {
Guy Cohenfde0db32008-04-21 15:42:01 -0700584 if (num_of_ant == 1)
585 tbl->lq_type = LQ_SISO; /*else NONE*/
586 /* MIMO2 */
Guy Cohen39e88502008-04-23 17:14:57 -0700587 } else if (mcs <= IWL_RATE_MIMO2_60M_PLCP) {
Guy Cohenfde0db32008-04-21 15:42:01 -0700588 if (num_of_ant == 2)
589 tbl->lq_type = LQ_MIMO2;
590 /* MIMO3 */
591 } else {
592 if (num_of_ant == 3)
593 tbl->lq_type = LQ_MIMO3;
594 }
Zhu Yib481de92007-09-25 17:54:57 -0700595 }
596 return 0;
597}
Guy Cohenaade00c2008-04-23 17:14:59 -0700598
599/* switch to another antenna/antennas and return 1 */
600/* if no other valid antenna found, return 0 */
601static int rs_toggle_antenna(u32 valid_ant, u32 *rate_n_flags,
602 struct iwl4965_scale_tbl_info *tbl)
Zhu Yib481de92007-09-25 17:54:57 -0700603{
Guy Cohenaade00c2008-04-23 17:14:59 -0700604 u8 new_ant_type;
605
606 if (!tbl->ant_type || tbl->ant_type > ANT_ABC)
607 return 0;
608
609 if (!rs_is_valid_ant(valid_ant, tbl->ant_type))
610 return 0;
611
612 new_ant_type = ant_toggle_lookup[tbl->ant_type];
613
614 while ((new_ant_type != tbl->ant_type) &&
615 !rs_is_valid_ant(valid_ant, new_ant_type))
616 new_ant_type = ant_toggle_lookup[new_ant_type];
617
618 if (new_ant_type == tbl->ant_type)
619 return 0;
620
621 tbl->ant_type = new_ant_type;
622 *rate_n_flags &= ~RATE_MCS_ANT_ABC_MSK;
623 *rate_n_flags |= new_ant_type << RATE_MCS_ANT_POS;
624 return 1;
Zhu Yib481de92007-09-25 17:54:57 -0700625}
626
Guy Cohen39e88502008-04-23 17:14:57 -0700627/* FIXME:RS: in 4965 we don't use greenfield at all */
Guy Cohenf935a6d2008-04-23 17:15:02 -0700628/* FIXME:RS: don't use greenfield for now in TX */
629/* #ifdef CONFIG_IWL4965_HT */
630#if 0
631static inline u8 rs_use_green(struct iwl_priv *priv, struct ieee80211_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -0700632{
Ron Rindjunsky270243a2007-11-26 16:14:41 +0200633 return ((conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) &&
634 priv->current_ht_config.is_green_field &&
635 !priv->current_ht_config.non_GF_STA_present);
Zhu Yib481de92007-09-25 17:54:57 -0700636}
Guy Cohenf935a6d2008-04-23 17:15:02 -0700637#else
638static inline u8 rs_use_green(struct iwl_priv *priv, struct ieee80211_conf *conf)
639{
640 return 0;
641}
642#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -0700643
644/**
645 * rs_get_supported_rates - get the available rates
646 *
647 * if management frame or broadcast frame only return
648 * basic available rates.
649 *
650 */
Guy Coheneecd6e52008-04-23 17:14:58 -0700651static u16 rs_get_supported_rates(struct iwl4965_lq_sta *lq_sta,
Zhu Yib481de92007-09-25 17:54:57 -0700652 struct ieee80211_hdr *hdr,
Guy Coheneecd6e52008-04-23 17:14:58 -0700653 enum iwl_table_type rate_type)
Zhu Yib481de92007-09-25 17:54:57 -0700654{
Zhu Yib481de92007-09-25 17:54:57 -0700655 if (hdr && is_multicast_ether_addr(hdr->addr1) &&
Guy Coheneecd6e52008-04-23 17:14:58 -0700656 lq_sta->active_rate_basic)
657 return lq_sta->active_rate_basic;
658
659 if (is_legacy(rate_type)) {
660 return lq_sta->active_legacy_rate;
661 } else {
662 if (is_siso(rate_type))
663 return lq_sta->active_siso_rate;
664 else if (is_mimo2(rate_type))
665 return lq_sta->active_mimo2_rate;
666 else
667 return lq_sta->active_mimo3_rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -0800668 }
Zhu Yib481de92007-09-25 17:54:57 -0700669}
670
Ester Kummerbf403db2008-05-05 10:22:40 +0800671static u16 rs_get_adjacent_rate(struct iwl_priv *priv, u8 index, u16 rate_mask,
672 int rate_type)
Zhu Yib481de92007-09-25 17:54:57 -0700673{
674 u8 high = IWL_RATE_INVALID;
675 u8 low = IWL_RATE_INVALID;
676
Ian Schram01ebd062007-10-25 17:15:22 +0800677 /* 802.11A or ht walks to the next literal adjacent rate in
Zhu Yib481de92007-09-25 17:54:57 -0700678 * the rate table */
679 if (is_a_band(rate_type) || !is_legacy(rate_type)) {
680 int i;
681 u32 mask;
682
683 /* Find the previous rate that is in the rate mask */
684 i = index - 1;
685 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
686 if (rate_mask & mask) {
687 low = i;
688 break;
689 }
690 }
691
692 /* Find the next rate that is in the rate mask */
693 i = index + 1;
694 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
695 if (rate_mask & mask) {
696 high = i;
697 break;
698 }
699 }
700
701 return (high << 8) | low;
702 }
703
704 low = index;
705 while (low != IWL_RATE_INVALID) {
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800706 low = iwl_rates[low].prev_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700707 if (low == IWL_RATE_INVALID)
708 break;
709 if (rate_mask & (1 << low))
710 break;
711 IWL_DEBUG_RATE("Skipping masked lower rate: %d\n", low);
712 }
713
714 high = index;
715 while (high != IWL_RATE_INVALID) {
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800716 high = iwl_rates[high].next_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700717 if (high == IWL_RATE_INVALID)
718 break;
719 if (rate_mask & (1 << high))
720 break;
721 IWL_DEBUG_RATE("Skipping masked higher rate: %d\n", high);
722 }
723
724 return (high << 8) | low;
725}
726
Guy Cohen39e88502008-04-23 17:14:57 -0700727static u32 rs_get_lower_rate(struct iwl4965_lq_sta *lq_sta,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800728 struct iwl4965_scale_tbl_info *tbl, u8 scale_index,
Guy Cohen39e88502008-04-23 17:14:57 -0700729 u8 ht_possible)
Zhu Yib481de92007-09-25 17:54:57 -0700730{
Zhu Yib481de92007-09-25 17:54:57 -0700731 s32 low;
732 u16 rate_mask;
733 u16 high_low;
734 u8 switch_to_legacy = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -0800735 u8 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -0700736
737 /* check if we need to switch from HT to legacy rates.
738 * assumption is that mandatory rates (1Mbps or 6Mbps)
739 * are always supported (spec demand) */
740 if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_index)) {
741 switch_to_legacy = 1;
742 scale_index = rs_ht_to_legacy[scale_index];
Johannes Berg8318d782008-01-24 19:38:38 +0100743 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700744 tbl->lq_type = LQ_A;
745 else
746 tbl->lq_type = LQ_G;
747
Guy Cohen69fdb302008-04-23 17:15:01 -0700748 if (num_of_ant(tbl->ant_type) > 1)
Guy Cohenfde0db32008-04-21 15:42:01 -0700749 tbl->ant_type = ANT_A;/*FIXME:RS*/
Zhu Yib481de92007-09-25 17:54:57 -0700750
751 tbl->is_fat = 0;
752 tbl->is_SGI = 0;
753 }
754
Guy Coheneecd6e52008-04-23 17:14:58 -0700755 rate_mask = rs_get_supported_rates(lq_sta, NULL, tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -0700756
Ben Cahill77626352007-11-29 11:09:44 +0800757 /* Mask with station rate restriction */
Zhu Yib481de92007-09-25 17:54:57 -0700758 if (is_legacy(tbl->lq_type)) {
Ben Cahill77626352007-11-29 11:09:44 +0800759 /* supp_rates has no CCK bits in A mode */
Johannes Berg8318d782008-01-24 19:38:38 +0100760 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700761 rate_mask = (u16)(rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -0800762 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
Zhu Yib481de92007-09-25 17:54:57 -0700763 else
Tomas Winklerc33104f2008-01-14 17:46:21 -0800764 rate_mask = (u16)(rate_mask & lq_sta->supp_rates);
Zhu Yib481de92007-09-25 17:54:57 -0700765 }
766
Ben Cahill77626352007-11-29 11:09:44 +0800767 /* If we switched from HT to legacy, check current rate */
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800768 if (switch_to_legacy && (rate_mask & (1 << scale_index))) {
Guy Cohen39e88502008-04-23 17:14:57 -0700769 low = scale_index;
770 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700771 }
772
Ester Kummerbf403db2008-05-05 10:22:40 +0800773 high_low = rs_get_adjacent_rate(lq_sta->drv, scale_index, rate_mask,
774 tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -0700775 low = high_low & 0xff;
776
Guy Cohen39e88502008-04-23 17:14:57 -0700777 if (low == IWL_RATE_INVALID)
778 low = scale_index;
779
780out:
781 return rate_n_flags_from_tbl(tbl, low, is_green);
Zhu Yib481de92007-09-25 17:54:57 -0700782}
783
Ben Cahill77626352007-11-29 11:09:44 +0800784/*
785 * mac80211 sends us Tx status
786 */
Tomas Winklerc33104f2008-01-14 17:46:21 -0800787static void rs_tx_status(void *priv_rate, struct net_device *dev,
Zhu Yib481de92007-09-25 17:54:57 -0700788 struct sk_buff *skb,
789 struct ieee80211_tx_status *tx_resp)
790{
791 int status;
792 u8 retries;
793 int rs_index, index = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -0800794 struct iwl4965_lq_sta *lq_sta;
Tomas Winkler66c73db2008-04-15 16:01:40 -0700795 struct iwl_link_quality_cmd *table;
Zhu Yib481de92007-09-25 17:54:57 -0700796 struct sta_info *sta;
797 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700798 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
Zhu Yib481de92007-09-25 17:54:57 -0700799 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800800 struct ieee80211_hw *hw = local_to_hw(local);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800801 struct iwl4965_rate_scale_data *window = NULL;
802 struct iwl4965_rate_scale_data *search_win = NULL;
Guy Cohen39e88502008-04-23 17:14:57 -0700803 u32 tx_rate;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800804 struct iwl4965_scale_tbl_info tbl_type;
805 struct iwl4965_scale_tbl_info *curr_tbl, *search_tbl;
Zhu Yib481de92007-09-25 17:54:57 -0700806 u8 active_index = 0;
807 u16 fc = le16_to_cpu(hdr->frame_control);
808 s32 tpt = 0;
809
Zhu Yi58826352007-09-27 11:27:39 +0800810 IWL_DEBUG_RATE_LIMIT("get frame ack response, update rate scale window\n");
Zhu Yib481de92007-09-25 17:54:57 -0700811
812 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1))
813 return;
814
Ron Rindjunsky99556432008-01-28 14:07:25 +0200815 /* This packet was aggregated but doesn't carry rate scale info */
816 if ((tx_resp->control.flags & IEEE80211_TXCTL_AMPDU) &&
817 !(tx_resp->flags & IEEE80211_TX_STATUS_AMPDU))
818 return;
819
Zhu Yib481de92007-09-25 17:54:57 -0700820 retries = tx_resp->retry_count;
821
822 if (retries > 15)
823 retries = 15;
824
Johannes Bergd0709a62008-02-25 16:27:46 +0100825 rcu_read_lock();
Zhu Yib481de92007-09-25 17:54:57 -0700826
827 sta = sta_info_get(local, hdr->addr1);
828
Tomas Winklerf4d60822008-03-05 11:31:00 -0800829 if (!sta || !sta->rate_ctrl_priv)
830 goto out;
831
Zhu Yib481de92007-09-25 17:54:57 -0700832
Tomas Winklerc33104f2008-01-14 17:46:21 -0800833 lq_sta = (struct iwl4965_lq_sta *)sta->rate_ctrl_priv;
Zhu Yib481de92007-09-25 17:54:57 -0700834
835 if (!priv->lq_mngr.lq_ready)
Tomas Winklerf4d60822008-03-05 11:31:00 -0800836 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700837
Tomas Winklerc33104f2008-01-14 17:46:21 -0800838 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
839 !lq_sta->ibss_sta_added)
Tomas Winklerf4d60822008-03-05 11:31:00 -0800840 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700841
Tomas Winklerc33104f2008-01-14 17:46:21 -0800842 table = &lq_sta->lq;
843 active_index = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -0700844
Tomas Winklerc33104f2008-01-14 17:46:21 -0800845 curr_tbl = &(lq_sta->lq_info[active_index]);
846 search_tbl = &(lq_sta->lq_info[(1 - active_index)]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800847 window = (struct iwl4965_rate_scale_data *)
Zhu Yib481de92007-09-25 17:54:57 -0700848 &(curr_tbl->win[0]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800849 search_win = (struct iwl4965_rate_scale_data *)
Zhu Yib481de92007-09-25 17:54:57 -0700850 &(search_tbl->win[0]);
851
Ben Cahill77626352007-11-29 11:09:44 +0800852 /*
853 * Ignore this Tx frame response if its initial rate doesn't match
854 * that of latest Link Quality command. There may be stragglers
855 * from a previous Link Quality command, but we're no longer interested
856 * in those; they're either from the "active" mode while we're trying
857 * to check "search" mode, or a prior "search" mode after we've moved
858 * to a new "search" mode (which might become the new "active" mode).
859 */
Guy Cohen39e88502008-04-23 17:14:57 -0700860 tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
861 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800862 if (priv->band == IEEE80211_BAND_5GHZ)
863 rs_index -= IWL_FIRST_OFDM_RATE;
864
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200865 if ((tx_resp->control.tx_rate_idx < 0) ||
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800866 (tbl_type.is_SGI ^
867 !!(tx_resp->control.flags & IEEE80211_TXCTL_SHORT_GI)) ||
868 (tbl_type.is_fat ^
869 !!(tx_resp->control.flags & IEEE80211_TXCTL_40_MHZ_WIDTH)) ||
870 (tbl_type.is_dup ^
871 !!(tx_resp->control.flags & IEEE80211_TXCTL_DUP_DATA)) ||
Guy Cohenfde0db32008-04-21 15:42:01 -0700872 (tbl_type.ant_type ^ tx_resp->control.antenna_sel_tx) ||
Guy Cohen39e88502008-04-23 17:14:57 -0700873 (!!(tx_rate & RATE_MCS_HT_MSK) ^
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800874 !!(tx_resp->control.flags & IEEE80211_TXCTL_OFDM_HT)) ||
Guy Cohen39e88502008-04-23 17:14:57 -0700875 (!!(tx_rate & RATE_MCS_GF_MSK) ^
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800876 !!(tx_resp->control.flags & IEEE80211_TXCTL_GREEN_FIELD)) ||
877 (hw->wiphy->bands[priv->band]->bitrates[rs_index].bitrate !=
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200878 hw->wiphy->bands[tx_resp->control.band]->bitrates[tx_resp->control.tx_rate_idx].bitrate)) {
Guy Cohen39e88502008-04-23 17:14:57 -0700879 IWL_DEBUG_RATE("initial rate does not match 0x%x\n", tx_rate);
Tomas Winklerf4d60822008-03-05 11:31:00 -0800880 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700881 }
882
Ben Cahill77626352007-11-29 11:09:44 +0800883 /* Update frame history window with "failure" for each Tx retry. */
Zhu Yib481de92007-09-25 17:54:57 -0700884 while (retries) {
Ben Cahill77626352007-11-29 11:09:44 +0800885 /* Look up the rate and other info used for each tx attempt.
886 * Each tx attempt steps one entry deeper in the rate table. */
Guy Cohen39e88502008-04-23 17:14:57 -0700887 tx_rate = le32_to_cpu(table->rs_table[index].rate_n_flags);
888 rs_get_tbl_info_from_mcs(tx_rate, priv->band,
Zhu Yib481de92007-09-25 17:54:57 -0700889 &tbl_type, &rs_index);
890
Ben Cahill77626352007-11-29 11:09:44 +0800891 /* If type matches "search" table,
892 * add failure to "search" history */
Zhu Yib481de92007-09-25 17:54:57 -0700893 if ((tbl_type.lq_type == search_tbl->lq_type) &&
Guy Cohenfde0db32008-04-21 15:42:01 -0700894 (tbl_type.ant_type == search_tbl->ant_type) &&
Zhu Yib481de92007-09-25 17:54:57 -0700895 (tbl_type.is_SGI == search_tbl->is_SGI)) {
896 if (search_tbl->expected_tpt)
897 tpt = search_tbl->expected_tpt[rs_index];
898 else
899 tpt = 0;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200900 rs_collect_tx_data(search_win, rs_index, tpt, 1, 0);
Ben Cahill77626352007-11-29 11:09:44 +0800901
902 /* Else if type matches "current/active" table,
903 * add failure to "current/active" history */
Zhu Yib481de92007-09-25 17:54:57 -0700904 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
Guy Cohenfde0db32008-04-21 15:42:01 -0700905 (tbl_type.ant_type == curr_tbl->ant_type) &&
Zhu Yib481de92007-09-25 17:54:57 -0700906 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
907 if (curr_tbl->expected_tpt)
908 tpt = curr_tbl->expected_tpt[rs_index];
909 else
910 tpt = 0;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200911 rs_collect_tx_data(window, rs_index, tpt, 1, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700912 }
Ben Cahill77626352007-11-29 11:09:44 +0800913
914 /* If not searching for a new mode, increment failed counter
915 * ... this helps determine when to start searching again */
Tomas Winklerc33104f2008-01-14 17:46:21 -0800916 if (lq_sta->stay_in_tbl)
917 lq_sta->total_failed++;
Zhu Yib481de92007-09-25 17:54:57 -0700918 --retries;
919 index++;
920
921 }
922
Ben Cahill77626352007-11-29 11:09:44 +0800923 /*
924 * Find (by rate) the history window to update with final Tx attempt;
925 * if Tx was successful first try, use original rate,
926 * else look up the rate that was, finally, successful.
927 */
Guy Cohen39e88502008-04-23 17:14:57 -0700928 tx_rate = le32_to_cpu(table->rs_table[index].rate_n_flags);
929 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
Zhu Yib481de92007-09-25 17:54:57 -0700930
Ben Cahill77626352007-11-29 11:09:44 +0800931 /* Update frame history window with "success" if Tx got ACKed ... */
Zhu Yib481de92007-09-25 17:54:57 -0700932 if (tx_resp->flags & IEEE80211_TX_STATUS_ACK)
933 status = 1;
934 else
935 status = 0;
936
Ben Cahill77626352007-11-29 11:09:44 +0800937 /* If type matches "search" table,
938 * add final tx status to "search" history */
Zhu Yib481de92007-09-25 17:54:57 -0700939 if ((tbl_type.lq_type == search_tbl->lq_type) &&
Guy Cohenfde0db32008-04-21 15:42:01 -0700940 (tbl_type.ant_type == search_tbl->ant_type) &&
Zhu Yib481de92007-09-25 17:54:57 -0700941 (tbl_type.is_SGI == search_tbl->is_SGI)) {
942 if (search_tbl->expected_tpt)
943 tpt = search_tbl->expected_tpt[rs_index];
944 else
945 tpt = 0;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200946 if (tx_resp->control.flags & IEEE80211_TXCTL_AMPDU)
947 rs_collect_tx_data(search_win, rs_index, tpt,
948 tx_resp->ampdu_ack_len,
949 tx_resp->ampdu_ack_map);
950 else
951 rs_collect_tx_data(search_win, rs_index, tpt,
952 1, status);
Ben Cahill77626352007-11-29 11:09:44 +0800953 /* Else if type matches "current/active" table,
954 * add final tx status to "current/active" history */
Zhu Yib481de92007-09-25 17:54:57 -0700955 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
Guy Cohenfde0db32008-04-21 15:42:01 -0700956 (tbl_type.ant_type == curr_tbl->ant_type) &&
Zhu Yib481de92007-09-25 17:54:57 -0700957 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
958 if (curr_tbl->expected_tpt)
959 tpt = curr_tbl->expected_tpt[rs_index];
960 else
961 tpt = 0;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200962 if (tx_resp->control.flags & IEEE80211_TXCTL_AMPDU)
963 rs_collect_tx_data(window, rs_index, tpt,
964 tx_resp->ampdu_ack_len,
965 tx_resp->ampdu_ack_map);
966 else
967 rs_collect_tx_data(window, rs_index, tpt,
968 1, status);
Zhu Yib481de92007-09-25 17:54:57 -0700969 }
970
Ben Cahill77626352007-11-29 11:09:44 +0800971 /* If not searching for new mode, increment success/failed counter
972 * ... these help determine when to start searching again */
Tomas Winklerc33104f2008-01-14 17:46:21 -0800973 if (lq_sta->stay_in_tbl) {
Ron Rindjunsky99556432008-01-28 14:07:25 +0200974 if (tx_resp->control.flags & IEEE80211_TXCTL_AMPDU) {
975 lq_sta->total_success += tx_resp->ampdu_ack_map;
976 lq_sta->total_failed +=
977 (tx_resp->ampdu_ack_len - tx_resp->ampdu_ack_map);
978 } else {
979 if (status)
980 lq_sta->total_success++;
981 else
982 lq_sta->total_failed++;
983 }
Zhu Yib481de92007-09-25 17:54:57 -0700984 }
985
Ben Cahill77626352007-11-29 11:09:44 +0800986 /* See if there's a better rate or modulation mode to try. */
Zhu Yib481de92007-09-25 17:54:57 -0700987 rs_rate_scale_perform(priv, dev, hdr, sta);
Tomas Winklerf4d60822008-03-05 11:31:00 -0800988out:
Johannes Bergd0709a62008-02-25 16:27:46 +0100989 rcu_read_unlock();
Zhu Yib481de92007-09-25 17:54:57 -0700990 return;
991}
992
Ben Cahill77626352007-11-29 11:09:44 +0800993/*
994 * Begin a period of staying with a selected modulation mode.
995 * Set "stay_in_tbl" flag to prevent any mode switches.
996 * Set frame tx success limits according to legacy vs. high-throughput,
997 * and reset overall (spanning all rates) tx success history statistics.
998 * These control how long we stay using same modulation mode before
999 * searching for a new mode.
1000 */
Ester Kummerbf403db2008-05-05 10:22:40 +08001001static void rs_set_stay_in_table(struct iwl_priv *priv, u8 is_legacy,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001002 struct iwl4965_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07001003{
Guy Coheneecd6e52008-04-23 17:14:58 -07001004 IWL_DEBUG_RATE("we are staying in the same table\n");
Tomas Winklerc33104f2008-01-14 17:46:21 -08001005 lq_sta->stay_in_tbl = 1; /* only place this gets set */
Zhu Yib481de92007-09-25 17:54:57 -07001006 if (is_legacy) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001007 lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT;
1008 lq_sta->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
1009 lq_sta->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
Zhu Yib481de92007-09-25 17:54:57 -07001010 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001011 lq_sta->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
1012 lq_sta->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
1013 lq_sta->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
Zhu Yib481de92007-09-25 17:54:57 -07001014 }
Tomas Winklerc33104f2008-01-14 17:46:21 -08001015 lq_sta->table_count = 0;
1016 lq_sta->total_failed = 0;
1017 lq_sta->total_success = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001018}
1019
Ben Cahill77626352007-11-29 11:09:44 +08001020/*
1021 * Find correct throughput table for given mode of modulation
1022 */
Guy Coheneecd6e52008-04-23 17:14:58 -07001023static void rs_set_expected_tpt_table(struct iwl4965_lq_sta *lq_sta,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001024 struct iwl4965_scale_tbl_info *tbl)
Zhu Yib481de92007-09-25 17:54:57 -07001025{
1026 if (is_legacy(tbl->lq_type)) {
1027 if (!is_a_band(tbl->lq_type))
1028 tbl->expected_tpt = expected_tpt_G;
1029 else
1030 tbl->expected_tpt = expected_tpt_A;
1031 } else if (is_siso(tbl->lq_type)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001032 if (tbl->is_fat && !lq_sta->is_dup)
Zhu Yib481de92007-09-25 17:54:57 -07001033 if (tbl->is_SGI)
1034 tbl->expected_tpt = expected_tpt_siso40MHzSGI;
1035 else
1036 tbl->expected_tpt = expected_tpt_siso40MHz;
1037 else if (tbl->is_SGI)
1038 tbl->expected_tpt = expected_tpt_siso20MHzSGI;
1039 else
1040 tbl->expected_tpt = expected_tpt_siso20MHz;
1041
Guy Cohenfde0db32008-04-21 15:42:01 -07001042 } else if (is_mimo(tbl->lq_type)) { /* FIXME:need to separate mimo2/3 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001043 if (tbl->is_fat && !lq_sta->is_dup)
Zhu Yib481de92007-09-25 17:54:57 -07001044 if (tbl->is_SGI)
1045 tbl->expected_tpt = expected_tpt_mimo40MHzSGI;
1046 else
1047 tbl->expected_tpt = expected_tpt_mimo40MHz;
1048 else if (tbl->is_SGI)
1049 tbl->expected_tpt = expected_tpt_mimo20MHzSGI;
1050 else
1051 tbl->expected_tpt = expected_tpt_mimo20MHz;
1052 } else
1053 tbl->expected_tpt = expected_tpt_G;
1054}
1055
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001056#ifdef CONFIG_IWL4965_HT
Ben Cahill77626352007-11-29 11:09:44 +08001057/*
1058 * Find starting rate for new "search" high-throughput mode of modulation.
1059 * Goal is to find lowest expected rate (under perfect conditions) that is
1060 * above the current measured throughput of "active" mode, to give new mode
1061 * a fair chance to prove itself without too many challenges.
1062 *
1063 * This gets called when transitioning to more aggressive modulation
1064 * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
1065 * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
1066 * to decrease to match "active" throughput. When moving from MIMO to SISO,
1067 * bit rate will typically need to increase, but not if performance was bad.
1068 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001069static s32 rs_get_best_rate(struct iwl_priv *priv,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001070 struct iwl4965_lq_sta *lq_sta,
Ben Cahill77626352007-11-29 11:09:44 +08001071 struct iwl4965_scale_tbl_info *tbl, /* "search" */
Guy Cohenf935a6d2008-04-23 17:15:02 -07001072 u16 rate_mask, s8 index)
Zhu Yib481de92007-09-25 17:54:57 -07001073{
Ben Cahill77626352007-11-29 11:09:44 +08001074 /* "active" values */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001075 struct iwl4965_scale_tbl_info *active_tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001076 &(lq_sta->lq_info[lq_sta->active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07001077 s32 active_sr = active_tbl->win[index].success_ratio;
Zhu Yib481de92007-09-25 17:54:57 -07001078 s32 active_tpt = active_tbl->expected_tpt[index];
Ben Cahill77626352007-11-29 11:09:44 +08001079
1080 /* expected "search" throughput */
1081 s32 *tpt_tbl = tbl->expected_tpt;
1082
1083 s32 new_rate, high, low, start_hi;
Zhu Yib481de92007-09-25 17:54:57 -07001084 u16 high_low;
Guy Cohenf935a6d2008-04-23 17:15:02 -07001085 s8 rate = index;
Zhu Yib481de92007-09-25 17:54:57 -07001086
1087 new_rate = high = low = start_hi = IWL_RATE_INVALID;
1088
1089 for (; ;) {
Ester Kummerbf403db2008-05-05 10:22:40 +08001090 high_low = rs_get_adjacent_rate(priv, rate, rate_mask,
1091 tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -07001092
1093 low = high_low & 0xff;
1094 high = (high_low >> 8) & 0xff;
1095
Ben Cahill77626352007-11-29 11:09:44 +08001096 /*
1097 * Lower the "search" bit rate, to give new "search" mode
1098 * approximately the same throughput as "active" if:
1099 *
1100 * 1) "Active" mode has been working modestly well (but not
1101 * great), and expected "search" throughput (under perfect
1102 * conditions) at candidate rate is above the actual
1103 * measured "active" throughput (but less than expected
1104 * "active" throughput under perfect conditions).
1105 * OR
1106 * 2) "Active" mode has been working perfectly or very well
1107 * and expected "search" throughput (under perfect
1108 * conditions) at candidate rate is above expected
1109 * "active" throughput (under perfect conditions).
1110 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001111 if ((((100 * tpt_tbl[rate]) > lq_sta->last_tpt) &&
Zhu Yib481de92007-09-25 17:54:57 -07001112 ((active_sr > IWL_RATE_DECREASE_TH) &&
1113 (active_sr <= IWL_RATE_HIGH_TH) &&
1114 (tpt_tbl[rate] <= active_tpt))) ||
1115 ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
1116 (tpt_tbl[rate] > active_tpt))) {
1117
Ben Cahill77626352007-11-29 11:09:44 +08001118 /* (2nd or later pass)
1119 * If we've already tried to raise the rate, and are
1120 * now trying to lower it, use the higher rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001121 if (start_hi != IWL_RATE_INVALID) {
1122 new_rate = start_hi;
1123 break;
1124 }
Ben Cahill77626352007-11-29 11:09:44 +08001125
Zhu Yib481de92007-09-25 17:54:57 -07001126 new_rate = rate;
Ben Cahill77626352007-11-29 11:09:44 +08001127
1128 /* Loop again with lower rate */
Zhu Yib481de92007-09-25 17:54:57 -07001129 if (low != IWL_RATE_INVALID)
1130 rate = low;
Ben Cahill77626352007-11-29 11:09:44 +08001131
1132 /* Lower rate not available, use the original */
Zhu Yib481de92007-09-25 17:54:57 -07001133 else
1134 break;
Ben Cahill77626352007-11-29 11:09:44 +08001135
1136 /* Else try to raise the "search" rate to match "active" */
Zhu Yib481de92007-09-25 17:54:57 -07001137 } else {
Ben Cahill77626352007-11-29 11:09:44 +08001138 /* (2nd or later pass)
1139 * If we've already tried to lower the rate, and are
1140 * now trying to raise it, use the lower rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001141 if (new_rate != IWL_RATE_INVALID)
1142 break;
Ben Cahill77626352007-11-29 11:09:44 +08001143
1144 /* Loop again with higher rate */
Zhu Yib481de92007-09-25 17:54:57 -07001145 else if (high != IWL_RATE_INVALID) {
1146 start_hi = high;
1147 rate = high;
Ben Cahill77626352007-11-29 11:09:44 +08001148
1149 /* Higher rate not available, use the original */
Zhu Yib481de92007-09-25 17:54:57 -07001150 } else {
1151 new_rate = rate;
1152 break;
1153 }
1154 }
1155 }
1156
1157 return new_rate;
1158}
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001159#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001160
Ben Cahill77626352007-11-29 11:09:44 +08001161/*
1162 * Set up search table for MIMO
1163 */
Guy Cohenfde0db32008-04-21 15:42:01 -07001164#ifdef CONFIG_IWL4965_HT
1165static int rs_switch_to_mimo2(struct iwl_priv *priv,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001166 struct iwl4965_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001167 struct ieee80211_conf *conf,
1168 struct sta_info *sta,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001169 struct iwl4965_scale_tbl_info *tbl, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001170{
Zhu Yib481de92007-09-25 17:54:57 -07001171 u16 rate_mask;
1172 s32 rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001173 s8 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07001174
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001175 if (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) ||
1176 !sta->ht_info.ht_supported)
Zhu Yib481de92007-09-25 17:54:57 -07001177 return -1;
1178
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001179 if (priv->current_ht_config.tx_mimo_ps_mode == IWL_MIMO_PS_STATIC)
Zhu Yib481de92007-09-25 17:54:57 -07001180 return -1;
1181
Ben Cahill77626352007-11-29 11:09:44 +08001182 /* Need both Tx chains/antennas to support MIMO */
Guy Cohenaade00c2008-04-23 17:14:59 -07001183 if (priv->hw_params.tx_chains_num < 2)
Zhu Yib481de92007-09-25 17:54:57 -07001184 return -1;
1185
Guy Coheneecd6e52008-04-23 17:14:58 -07001186 IWL_DEBUG_RATE("LQ: try to switch to MIMO2\n");
Guy Cohen39e88502008-04-23 17:14:57 -07001187
1188 tbl->lq_type = LQ_MIMO2;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001189 tbl->is_dup = lq_sta->is_dup;
Zhu Yib481de92007-09-25 17:54:57 -07001190 tbl->action = 0;
Guy Cohen39e88502008-04-23 17:14:57 -07001191 rate_mask = lq_sta->active_mimo2_rate;
1192
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001193 if (priv->current_ht_config.supported_chan_width
Guy Cohen39e88502008-04-23 17:14:57 -07001194 == IWL_CHANNEL_WIDTH_40MHZ)
Zhu Yib481de92007-09-25 17:54:57 -07001195 tbl->is_fat = 1;
1196 else
1197 tbl->is_fat = 0;
1198
Guy Cohen39e88502008-04-23 17:14:57 -07001199 /* FIXME: - don't toggle SGI here
Zhu Yib481de92007-09-25 17:54:57 -07001200 if (tbl->is_fat) {
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001201 if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
Zhu Yib481de92007-09-25 17:54:57 -07001202 tbl->is_SGI = 1;
1203 else
1204 tbl->is_SGI = 0;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001205 } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
Zhu Yib481de92007-09-25 17:54:57 -07001206 tbl->is_SGI = 1;
1207 else
1208 tbl->is_SGI = 0;
Guy Cohen39e88502008-04-23 17:14:57 -07001209 */
Zhu Yib481de92007-09-25 17:54:57 -07001210
Guy Coheneecd6e52008-04-23 17:14:58 -07001211 rs_set_expected_tpt_table(lq_sta, tbl);
Zhu Yib481de92007-09-25 17:54:57 -07001212
Guy Cohenf935a6d2008-04-23 17:15:02 -07001213 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
Zhu Yib481de92007-09-25 17:54:57 -07001214
Guy Coheneecd6e52008-04-23 17:14:58 -07001215 IWL_DEBUG_RATE("LQ: MIMO2 best rate %d mask %X\n", rate, rate_mask);
Guy Cohen39e88502008-04-23 17:14:57 -07001216
1217 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
Guy Coheneecd6e52008-04-23 17:14:58 -07001218 IWL_DEBUG_RATE("Can't switch with index %d rate mask %x\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001219 rate, rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07001220 return -1;
Guy Cohen39e88502008-04-23 17:14:57 -07001221 }
1222 tbl->current_rate = rate_n_flags_from_tbl(tbl, rate, is_green);
Zhu Yib481de92007-09-25 17:54:57 -07001223
Guy Coheneecd6e52008-04-23 17:14:58 -07001224 IWL_DEBUG_RATE("LQ: Switch to new mcs %X index is green %X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001225 tbl->current_rate, is_green);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001226 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07001227}
Guy Cohenfde0db32008-04-21 15:42:01 -07001228#else
1229static int rs_switch_to_mimo2(struct iwl_priv *priv,
1230 struct iwl4965_lq_sta *lq_sta,
1231 struct ieee80211_conf *conf,
1232 struct sta_info *sta,
1233 struct iwl4965_scale_tbl_info *tbl, int index)
1234{
1235 return -1;
1236}
1237#endif /*CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001238
Ben Cahill77626352007-11-29 11:09:44 +08001239/*
1240 * Set up search table for SISO
1241 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001242static int rs_switch_to_siso(struct iwl_priv *priv,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001243 struct iwl4965_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001244 struct ieee80211_conf *conf,
1245 struct sta_info *sta,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001246 struct iwl4965_scale_tbl_info *tbl, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001247{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001248#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07001249 u16 rate_mask;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001250 u8 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07001251 s32 rate;
1252
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001253 if (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) ||
1254 !sta->ht_info.ht_supported)
Zhu Yib481de92007-09-25 17:54:57 -07001255 return -1;
1256
Guy Coheneecd6e52008-04-23 17:14:58 -07001257 IWL_DEBUG_RATE("LQ: try to switch to SISO\n");
Guy Cohen39e88502008-04-23 17:14:57 -07001258
Tomas Winklerc33104f2008-01-14 17:46:21 -08001259 tbl->is_dup = lq_sta->is_dup;
Zhu Yib481de92007-09-25 17:54:57 -07001260 tbl->lq_type = LQ_SISO;
1261 tbl->action = 0;
Guy Cohen39e88502008-04-23 17:14:57 -07001262 rate_mask = lq_sta->active_siso_rate;
Zhu Yib481de92007-09-25 17:54:57 -07001263
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001264 if (priv->current_ht_config.supported_chan_width
1265 == IWL_CHANNEL_WIDTH_40MHZ)
Zhu Yib481de92007-09-25 17:54:57 -07001266 tbl->is_fat = 1;
1267 else
1268 tbl->is_fat = 0;
1269
Guy Cohen39e88502008-04-23 17:14:57 -07001270 /* FIXME: - don't toggle SGI here
Zhu Yib481de92007-09-25 17:54:57 -07001271 if (tbl->is_fat) {
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001272 if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
Zhu Yib481de92007-09-25 17:54:57 -07001273 tbl->is_SGI = 1;
1274 else
1275 tbl->is_SGI = 0;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001276 } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
Zhu Yib481de92007-09-25 17:54:57 -07001277 tbl->is_SGI = 1;
1278 else
1279 tbl->is_SGI = 0;
Guy Cohen39e88502008-04-23 17:14:57 -07001280 */
Zhu Yib481de92007-09-25 17:54:57 -07001281
1282 if (is_green)
Guy Cohen39e88502008-04-23 17:14:57 -07001283 tbl->is_SGI = 0; /*11n spec: no SGI in SISO+Greenfield*/
Zhu Yib481de92007-09-25 17:54:57 -07001284
Guy Coheneecd6e52008-04-23 17:14:58 -07001285 rs_set_expected_tpt_table(lq_sta, tbl);
Guy Cohenf935a6d2008-04-23 17:15:02 -07001286 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
Zhu Yib481de92007-09-25 17:54:57 -07001287
Guy Coheneecd6e52008-04-23 17:14:58 -07001288 IWL_DEBUG_RATE("LQ: get best rate %d mask %X\n", rate, rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07001289 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
Guy Coheneecd6e52008-04-23 17:14:58 -07001290 IWL_DEBUG_RATE("can not switch with index %d rate mask %x\n",
Zhu Yib481de92007-09-25 17:54:57 -07001291 rate, rate_mask);
1292 return -1;
1293 }
Guy Cohen39e88502008-04-23 17:14:57 -07001294 tbl->current_rate = rate_n_flags_from_tbl(tbl, rate, is_green);
Guy Coheneecd6e52008-04-23 17:14:58 -07001295 IWL_DEBUG_RATE("LQ: Switch to new mcs %X index is green %X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001296 tbl->current_rate, is_green);
Mohamed Abbas403ab562007-11-06 22:06:25 -08001297 return 0;
1298#else
1299 return -1;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001300#endif /*CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001301}
1302
Ben Cahill77626352007-11-29 11:09:44 +08001303/*
1304 * Try to switch to new modulation mode from legacy
1305 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001306static int rs_move_legacy_other(struct iwl_priv *priv,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001307 struct iwl4965_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001308 struct ieee80211_conf *conf,
1309 struct sta_info *sta,
Zhu Yib481de92007-09-25 17:54:57 -07001310 int index)
1311{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001312 struct iwl4965_scale_tbl_info *tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001313 &(lq_sta->lq_info[lq_sta->active_tbl]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001314 struct iwl4965_scale_tbl_info *search_tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001315 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001316 struct iwl4965_rate_scale_data *window = &(tbl->win[index]);
1317 u32 sz = (sizeof(struct iwl4965_scale_tbl_info) -
1318 (sizeof(struct iwl4965_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001319 u8 start_action = tbl->action;
Guy Cohenfde0db32008-04-21 15:42:01 -07001320 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1321 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001322
1323 for (; ;) {
1324 switch (tbl->action) {
1325 case IWL_LEGACY_SWITCH_ANTENNA:
Guy Cohenf935a6d2008-04-23 17:15:02 -07001326 IWL_DEBUG_RATE("LQ: Legacy toggle Antenna\n");
Zhu Yib481de92007-09-25 17:54:57 -07001327
Tomas Winklerc33104f2008-01-14 17:46:21 -08001328 lq_sta->action_counter++;
Ben Cahill77626352007-11-29 11:09:44 +08001329
1330 /* Don't change antenna if success has been great */
Zhu Yib481de92007-09-25 17:54:57 -07001331 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1332 break;
Ben Cahill77626352007-11-29 11:09:44 +08001333
Ben Cahill77626352007-11-29 11:09:44 +08001334 /* Set up search table to try other antenna */
Zhu Yib481de92007-09-25 17:54:57 -07001335 memcpy(search_tbl, tbl, sz);
1336
Guy Cohenaade00c2008-04-23 17:14:59 -07001337 if (rs_toggle_antenna(valid_tx_ant,
1338 &search_tbl->current_rate, search_tbl)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07001339 lq_sta->search_better_tbl = 1;
1340 goto out;
1341 }
Zhu Yib481de92007-09-25 17:54:57 -07001342
1343 case IWL_LEGACY_SWITCH_SISO:
Guy Coheneecd6e52008-04-23 17:14:58 -07001344 IWL_DEBUG_RATE("LQ: Legacy switch to SISO\n");
Ben Cahill77626352007-11-29 11:09:44 +08001345
1346 /* Set up search table to try SISO */
Zhu Yib481de92007-09-25 17:54:57 -07001347 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001348 search_tbl->is_SGI = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001349 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001350 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001351 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001352 lq_sta->search_better_tbl = 1;
1353 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001354 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001355 }
Zhu Yib481de92007-09-25 17:54:57 -07001356
1357 break;
Guy Cohenfde0db32008-04-21 15:42:01 -07001358 case IWL_LEGACY_SWITCH_MIMO2:
Guy Coheneecd6e52008-04-23 17:14:58 -07001359 IWL_DEBUG_RATE("LQ: Legacy switch to MIMO2\n");
Ben Cahill77626352007-11-29 11:09:44 +08001360
1361 /* Set up search table to try MIMO */
Zhu Yib481de92007-09-25 17:54:57 -07001362 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001363 search_tbl->is_SGI = 0;
Guy Cohenfde0db32008-04-21 15:42:01 -07001364 search_tbl->ant_type = ANT_AB;/*FIXME:RS*/
1365 /*FIXME:RS:need to check ant validity*/
1366 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001367 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001368 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001369 lq_sta->search_better_tbl = 1;
1370 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001371 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001372 }
Zhu Yib481de92007-09-25 17:54:57 -07001373 break;
1374 }
1375 tbl->action++;
Guy Cohenfde0db32008-04-21 15:42:01 -07001376 if (tbl->action > IWL_LEGACY_SWITCH_MIMO2)
Zhu Yib481de92007-09-25 17:54:57 -07001377 tbl->action = IWL_LEGACY_SWITCH_ANTENNA;
1378
1379 if (tbl->action == start_action)
1380 break;
1381
1382 }
1383 return 0;
1384
1385 out:
1386 tbl->action++;
Guy Cohenfde0db32008-04-21 15:42:01 -07001387 if (tbl->action > IWL_LEGACY_SWITCH_MIMO2)
Zhu Yib481de92007-09-25 17:54:57 -07001388 tbl->action = IWL_LEGACY_SWITCH_ANTENNA;
1389 return 0;
1390
1391}
1392
Ben Cahill77626352007-11-29 11:09:44 +08001393/*
1394 * Try to switch to new modulation mode from SISO
1395 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001396static int rs_move_siso_to_other(struct iwl_priv *priv,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001397 struct iwl4965_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001398 struct ieee80211_conf *conf,
1399 struct sta_info *sta,
Zhu Yib481de92007-09-25 17:54:57 -07001400 int index)
1401{
Tomas Winklerc33104f2008-01-14 17:46:21 -08001402 u8 is_green = lq_sta->is_green;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001403 struct iwl4965_scale_tbl_info *tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001404 &(lq_sta->lq_info[lq_sta->active_tbl]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001405 struct iwl4965_scale_tbl_info *search_tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001406 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001407 struct iwl4965_rate_scale_data *window = &(tbl->win[index]);
1408 u32 sz = (sizeof(struct iwl4965_scale_tbl_info) -
1409 (sizeof(struct iwl4965_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001410 u8 start_action = tbl->action;
Guy Cohenfde0db32008-04-21 15:42:01 -07001411 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1412 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001413
1414 for (;;) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001415 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001416 switch (tbl->action) {
1417 case IWL_SISO_SWITCH_ANTENNA:
Guy Coheneecd6e52008-04-23 17:14:58 -07001418 IWL_DEBUG_RATE("LQ: SISO toggle Antenna\n");
Zhu Yib481de92007-09-25 17:54:57 -07001419 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1420 break;
Zhu Yib481de92007-09-25 17:54:57 -07001421
1422 memcpy(search_tbl, tbl, sz);
Guy Cohenaade00c2008-04-23 17:14:59 -07001423 if (rs_toggle_antenna(valid_tx_ant,
1424 &search_tbl->current_rate, search_tbl)) {
1425 lq_sta->search_better_tbl = 1;
1426 goto out;
1427 }
Zhu Yib481de92007-09-25 17:54:57 -07001428
Guy Cohenfde0db32008-04-21 15:42:01 -07001429 case IWL_SISO_SWITCH_MIMO2:
Guy Coheneecd6e52008-04-23 17:14:58 -07001430 IWL_DEBUG_RATE("LQ: SISO switch to MIMO\n");
Zhu Yib481de92007-09-25 17:54:57 -07001431 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001432 search_tbl->is_SGI = 0;
Guy Cohenfde0db32008-04-21 15:42:01 -07001433 search_tbl->ant_type = ANT_AB; /*FIXME:RS*/
1434 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001435 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001436 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001437 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001438 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001439 }
Zhu Yib481de92007-09-25 17:54:57 -07001440 break;
1441 case IWL_SISO_SWITCH_GI:
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001442 if (!tbl->is_fat &&
1443 !(priv->current_ht_config.sgf &
1444 HT_SHORT_GI_20MHZ))
1445 break;
1446 if (tbl->is_fat &&
1447 !(priv->current_ht_config.sgf &
1448 HT_SHORT_GI_40MHZ))
1449 break;
1450
Guy Coheneecd6e52008-04-23 17:14:58 -07001451 IWL_DEBUG_RATE("LQ: SISO toggle SGI/NGI\n");
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001452
Zhu Yib481de92007-09-25 17:54:57 -07001453 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001454 if (is_green) {
1455 if (!tbl->is_SGI)
1456 break;
1457 else
1458 IWL_ERROR("SGI was set in GF+SISO\n");
Zhu Yib481de92007-09-25 17:54:57 -07001459 }
Guy Cohen39e88502008-04-23 17:14:57 -07001460 search_tbl->is_SGI = !tbl->is_SGI;
Guy Coheneecd6e52008-04-23 17:14:58 -07001461 rs_set_expected_tpt_table(lq_sta, search_tbl);
Guy Cohen39e88502008-04-23 17:14:57 -07001462 if (tbl->is_SGI) {
1463 s32 tpt = lq_sta->last_tpt / 100;
1464 if (tpt >= search_tbl->expected_tpt[index])
1465 break;
1466 }
1467 search_tbl->current_rate = rate_n_flags_from_tbl(
1468 search_tbl, index, is_green);
1469 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001470 goto out;
1471 }
1472 tbl->action++;
1473 if (tbl->action > IWL_SISO_SWITCH_GI)
1474 tbl->action = IWL_SISO_SWITCH_ANTENNA;
1475
1476 if (tbl->action == start_action)
1477 break;
1478 }
1479 return 0;
1480
1481 out:
1482 tbl->action++;
1483 if (tbl->action > IWL_SISO_SWITCH_GI)
1484 tbl->action = IWL_SISO_SWITCH_ANTENNA;
1485 return 0;
1486}
1487
Ben Cahill77626352007-11-29 11:09:44 +08001488/*
1489 * Try to switch to new modulation mode from MIMO
1490 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001491static int rs_move_mimo_to_other(struct iwl_priv *priv,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001492 struct iwl4965_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001493 struct ieee80211_conf *conf,
1494 struct sta_info *sta,
Zhu Yib481de92007-09-25 17:54:57 -07001495 int index)
1496{
Tomas Winklerc33104f2008-01-14 17:46:21 -08001497 s8 is_green = lq_sta->is_green;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001498 struct iwl4965_scale_tbl_info *tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001499 &(lq_sta->lq_info[lq_sta->active_tbl]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001500 struct iwl4965_scale_tbl_info *search_tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001501 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001502 u32 sz = (sizeof(struct iwl4965_scale_tbl_info) -
1503 (sizeof(struct iwl4965_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001504 u8 start_action = tbl->action;
Guy Cohenaade00c2008-04-23 17:14:59 -07001505 /*u8 valid_tx_ant = priv->hw_params.valid_tx_ant;*/
1506 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001507
1508 for (;;) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001509 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001510 switch (tbl->action) {
1511 case IWL_MIMO_SWITCH_ANTENNA_A:
1512 case IWL_MIMO_SWITCH_ANTENNA_B:
Guy Coheneecd6e52008-04-23 17:14:58 -07001513 IWL_DEBUG_RATE("LQ: MIMO2 switch to SISO\n");
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001514
Ben Cahill77626352007-11-29 11:09:44 +08001515 /* Set up new search table for SISO */
Zhu Yib481de92007-09-25 17:54:57 -07001516 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001517
1518 /*FIXME:RS:need to check ant validity + C*/
Zhu Yib481de92007-09-25 17:54:57 -07001519 if (tbl->action == IWL_MIMO_SWITCH_ANTENNA_A)
Guy Cohenfde0db32008-04-21 15:42:01 -07001520 search_tbl->ant_type = ANT_A;
Zhu Yib481de92007-09-25 17:54:57 -07001521 else
Guy Cohenfde0db32008-04-21 15:42:01 -07001522 search_tbl->ant_type = ANT_B;
Zhu Yib481de92007-09-25 17:54:57 -07001523
Tomas Winklerc33104f2008-01-14 17:46:21 -08001524 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001525 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001526 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001527 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001528 goto out;
1529 }
1530 break;
1531
1532 case IWL_MIMO_SWITCH_GI:
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001533 if (!tbl->is_fat &&
1534 !(priv->current_ht_config.sgf &
1535 HT_SHORT_GI_20MHZ))
1536 break;
1537 if (tbl->is_fat &&
1538 !(priv->current_ht_config.sgf &
1539 HT_SHORT_GI_40MHZ))
1540 break;
1541
Guy Coheneecd6e52008-04-23 17:14:58 -07001542 IWL_DEBUG_RATE("LQ: MIMO toggle SGI/NGI\n");
Ben Cahill77626352007-11-29 11:09:44 +08001543
1544 /* Set up new search table for MIMO */
Zhu Yib481de92007-09-25 17:54:57 -07001545 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001546 search_tbl->is_SGI = !tbl->is_SGI;
Guy Coheneecd6e52008-04-23 17:14:58 -07001547 rs_set_expected_tpt_table(lq_sta, search_tbl);
Ben Cahill77626352007-11-29 11:09:44 +08001548 /*
1549 * If active table already uses the fastest possible
1550 * modulation (dual stream with short guard interval),
1551 * and it's working well, there's no need to look
1552 * for a better type of modulation!
1553 */
Guy Cohen39e88502008-04-23 17:14:57 -07001554 if (tbl->is_SGI) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001555 s32 tpt = lq_sta->last_tpt / 100;
Guy Cohen39e88502008-04-23 17:14:57 -07001556 if (tpt >= search_tbl->expected_tpt[index])
1557 break;
Zhu Yib481de92007-09-25 17:54:57 -07001558 }
Guy Cohen39e88502008-04-23 17:14:57 -07001559 search_tbl->current_rate = rate_n_flags_from_tbl(
1560 search_tbl, index, is_green);
1561 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001562 goto out;
1563
1564 }
1565 tbl->action++;
1566 if (tbl->action > IWL_MIMO_SWITCH_GI)
1567 tbl->action = IWL_MIMO_SWITCH_ANTENNA_A;
1568
1569 if (tbl->action == start_action)
1570 break;
1571 }
1572
1573 return 0;
1574 out:
1575 tbl->action++;
1576 if (tbl->action > IWL_MIMO_SWITCH_GI)
1577 tbl->action = IWL_MIMO_SWITCH_ANTENNA_A;
1578 return 0;
1579
1580}
1581
Ben Cahill77626352007-11-29 11:09:44 +08001582/*
1583 * Check whether we should continue using same modulation mode, or
1584 * begin search for a new mode, based on:
1585 * 1) # tx successes or failures while using this mode
1586 * 2) # times calling this function
1587 * 3) elapsed time in this mode (not used, for now)
1588 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001589static void rs_stay_in_table(struct iwl4965_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07001590{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001591 struct iwl4965_scale_tbl_info *tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001592 int i;
1593 int active_tbl;
1594 int flush_interval_passed = 0;
Ester Kummerbf403db2008-05-05 10:22:40 +08001595 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07001596
Ester Kummerbf403db2008-05-05 10:22:40 +08001597 priv = lq_sta->drv;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001598 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001599
Tomas Winklerc33104f2008-01-14 17:46:21 -08001600 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07001601
Ben Cahill77626352007-11-29 11:09:44 +08001602 /* If we've been disallowing search, see if we should now allow it */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001603 if (lq_sta->stay_in_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07001604
Ben Cahill77626352007-11-29 11:09:44 +08001605 /* Elapsed time using current modulation mode */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001606 if (lq_sta->flush_timer)
Zhu Yib481de92007-09-25 17:54:57 -07001607 flush_interval_passed =
1608 time_after(jiffies,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001609 (unsigned long)(lq_sta->flush_timer +
Zhu Yib481de92007-09-25 17:54:57 -07001610 IWL_RATE_SCALE_FLUSH_INTVL));
1611
Ben Cahill77626352007-11-29 11:09:44 +08001612 /*
1613 * Check if we should allow search for new modulation mode.
1614 * If many frames have failed or succeeded, or we've used
1615 * this same modulation for a long time, allow search, and
1616 * reset history stats that keep track of whether we should
1617 * allow a new search. Also (below) reset all bitmaps and
1618 * stats in active history.
1619 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001620 if ((lq_sta->total_failed > lq_sta->max_failure_limit) ||
1621 (lq_sta->total_success > lq_sta->max_success_limit) ||
1622 ((!lq_sta->search_better_tbl) && (lq_sta->flush_timer)
Zhu Yib481de92007-09-25 17:54:57 -07001623 && (flush_interval_passed))) {
Guy Coheneecd6e52008-04-23 17:14:58 -07001624 IWL_DEBUG_RATE("LQ: stay is expired %d %d %d\n:",
Tomas Winklerc33104f2008-01-14 17:46:21 -08001625 lq_sta->total_failed,
1626 lq_sta->total_success,
Zhu Yib481de92007-09-25 17:54:57 -07001627 flush_interval_passed);
Ben Cahill77626352007-11-29 11:09:44 +08001628
1629 /* Allow search for new mode */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001630 lq_sta->stay_in_tbl = 0; /* only place reset */
1631 lq_sta->total_failed = 0;
1632 lq_sta->total_success = 0;
1633 lq_sta->flush_timer = 0;
Ben Cahill77626352007-11-29 11:09:44 +08001634
1635 /*
1636 * Else if we've used this modulation mode enough repetitions
1637 * (regardless of elapsed time or success/failure), reset
1638 * history bitmaps and rate-specific stats for all rates in
1639 * active table.
1640 */
Mohamed Abbas403ab562007-11-06 22:06:25 -08001641 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001642 lq_sta->table_count++;
1643 if (lq_sta->table_count >=
1644 lq_sta->table_count_limit) {
1645 lq_sta->table_count = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001646
Guy Coheneecd6e52008-04-23 17:14:58 -07001647 IWL_DEBUG_RATE("LQ: stay in table clear win\n");
Zhu Yib481de92007-09-25 17:54:57 -07001648 for (i = 0; i < IWL_RATE_COUNT; i++)
1649 rs_rate_scale_clear_window(
1650 &(tbl->win[i]));
1651 }
1652 }
1653
Ben Cahill77626352007-11-29 11:09:44 +08001654 /* If transitioning to allow "search", reset all history
1655 * bitmaps and stats in active table (this will become the new
1656 * "search" table). */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001657 if (!lq_sta->stay_in_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07001658 for (i = 0; i < IWL_RATE_COUNT; i++)
1659 rs_rate_scale_clear_window(&(tbl->win[i]));
1660 }
1661 }
1662}
1663
Ben Cahill77626352007-11-29 11:09:44 +08001664/*
1665 * Do rate scaling and search for new modulation mode.
1666 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001667static void rs_rate_scale_perform(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001668 struct net_device *dev,
1669 struct ieee80211_hdr *hdr,
1670 struct sta_info *sta)
1671{
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001672 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1673 struct ieee80211_hw *hw = local_to_hw(local);
1674 struct ieee80211_conf *conf = &hw->conf;
Zhu Yib481de92007-09-25 17:54:57 -07001675 int low = IWL_RATE_INVALID;
1676 int high = IWL_RATE_INVALID;
1677 int index;
1678 int i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001679 struct iwl4965_rate_scale_data *window = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001680 int current_tpt = IWL_INVALID_VALUE;
1681 int low_tpt = IWL_INVALID_VALUE;
1682 int high_tpt = IWL_INVALID_VALUE;
1683 u32 fail_count;
1684 s8 scale_action = 0;
1685 u16 fc, rate_mask;
1686 u8 update_lq = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001687 struct iwl4965_lq_sta *lq_sta;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001688 struct iwl4965_scale_tbl_info *tbl, *tbl1;
Zhu Yib481de92007-09-25 17:54:57 -07001689 u16 rate_scale_index_msk = 0;
Guy Cohen39e88502008-04-23 17:14:57 -07001690 u32 rate;
Zhu Yib481de92007-09-25 17:54:57 -07001691 u8 is_green = 0;
1692 u8 active_tbl = 0;
1693 u8 done_search = 0;
1694 u16 high_low;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001695#ifdef CONFIG_IWL4965_HT
1696 u8 tid = MAX_TID_COUNT;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001697#endif
Zhu Yib481de92007-09-25 17:54:57 -07001698
1699 IWL_DEBUG_RATE("rate scale calculate new rate for skb\n");
1700
1701 fc = le16_to_cpu(hdr->frame_control);
1702 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1)) {
1703 /* Send management frames and broadcast/multicast data using
1704 * lowest rate. */
1705 /* TODO: this could probably be improved.. */
1706 return;
1707 }
1708
1709 if (!sta || !sta->rate_ctrl_priv)
1710 return;
1711
1712 if (!priv->lq_mngr.lq_ready) {
1713 IWL_DEBUG_RATE("still rate scaling not ready\n");
1714 return;
1715 }
Tomas Winklerc33104f2008-01-14 17:46:21 -08001716 lq_sta = (struct iwl4965_lq_sta *)sta->rate_ctrl_priv;
Zhu Yib481de92007-09-25 17:54:57 -07001717
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001718#ifdef CONFIG_IWL4965_HT
Tomas Winkler54dbb522008-05-15 13:54:06 +08001719 rs_tl_add_packet(lq_sta, hdr);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001720#endif
Ben Cahill77626352007-11-29 11:09:44 +08001721 /*
1722 * Select rate-scale / modulation-mode table to work with in
1723 * the rest of this function: "search" if searching for better
1724 * modulation mode, or "active" if doing rate scaling within a mode.
1725 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001726 if (!lq_sta->search_better_tbl)
1727 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001728 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08001729 active_tbl = 1 - lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001730
Tomas Winklerc33104f2008-01-14 17:46:21 -08001731 tbl = &(lq_sta->lq_info[active_tbl]);
1732 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07001733
Ben Cahill77626352007-11-29 11:09:44 +08001734 /* current tx rate */
Johannes Berg8318d782008-01-24 19:38:38 +01001735 index = sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07001736
1737 IWL_DEBUG_RATE("Rate scale index %d for type %d\n", index,
1738 tbl->lq_type);
1739
Ben Cahill77626352007-11-29 11:09:44 +08001740 /* rates available for this association, and for modulation mode */
Guy Coheneecd6e52008-04-23 17:14:58 -07001741 rate_mask = rs_get_supported_rates(lq_sta, hdr, tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -07001742
1743 IWL_DEBUG_RATE("mask 0x%04X \n", rate_mask);
1744
1745 /* mask with station rate restriction */
1746 if (is_legacy(tbl->lq_type)) {
Johannes Berg8318d782008-01-24 19:38:38 +01001747 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Ben Cahill77626352007-11-29 11:09:44 +08001748 /* supp_rates has no CCK bits in A mode */
Zhu Yib481de92007-09-25 17:54:57 -07001749 rate_scale_index_msk = (u16) (rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -08001750 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
Zhu Yib481de92007-09-25 17:54:57 -07001751 else
1752 rate_scale_index_msk = (u16) (rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -08001753 lq_sta->supp_rates);
Zhu Yib481de92007-09-25 17:54:57 -07001754
1755 } else
1756 rate_scale_index_msk = rate_mask;
1757
1758 if (!rate_scale_index_msk)
1759 rate_scale_index_msk = rate_mask;
1760
Guy Cohen07bc28e2008-04-23 17:15:03 -07001761 if (!((1 << index) & rate_scale_index_msk)) {
1762 IWL_ERROR("Current Rate is not valid\n");
1763 return;
Zhu Yib481de92007-09-25 17:54:57 -07001764 }
1765
Ben Cahill77626352007-11-29 11:09:44 +08001766 /* Get expected throughput table and history window for current rate */
Guy Cohen07bc28e2008-04-23 17:15:03 -07001767 if (!tbl->expected_tpt) {
1768 IWL_ERROR("tbl->expected_tpt is NULL\n");
1769 return;
1770 }
Zhu Yib481de92007-09-25 17:54:57 -07001771
1772 window = &(tbl->win[index]);
1773
Ben Cahill77626352007-11-29 11:09:44 +08001774 /*
1775 * If there is not enough history to calculate actual average
1776 * throughput, keep analyzing results of more tx frames, without
1777 * changing rate or mode (bypass most of the rest of this function).
1778 * Set up new rate table in uCode only if old rate is not supported
1779 * in current association (use new rate found above).
1780 */
Zhu Yib481de92007-09-25 17:54:57 -07001781 fail_count = window->counter - window->success_counter;
Guy Cohen07bc28e2008-04-23 17:15:03 -07001782 if ((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
1783 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH)) {
1784 IWL_DEBUG_RATE("LQ: still below TH. succ=%d total=%d "
Zhu Yib481de92007-09-25 17:54:57 -07001785 "for index %d\n",
1786 window->success_counter, window->counter, index);
Ben Cahill77626352007-11-29 11:09:44 +08001787
1788 /* Can't calculate this yet; not enough history */
Zhu Yib481de92007-09-25 17:54:57 -07001789 window->average_tpt = IWL_INVALID_VALUE;
Ben Cahill77626352007-11-29 11:09:44 +08001790
1791 /* Should we stay with this modulation mode,
1792 * or search for a new one? */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001793 rs_stay_in_table(lq_sta);
Ben Cahill77626352007-11-29 11:09:44 +08001794
Zhu Yib481de92007-09-25 17:54:57 -07001795 goto out;
1796
Ben Cahill77626352007-11-29 11:09:44 +08001797 /* Else we have enough samples; calculate estimate of
1798 * actual average throughput */
Guy Cohen07bc28e2008-04-23 17:15:03 -07001799 } else {
1800 /*FIXME:RS remove this else if we don't get this error*/
1801 if (window->average_tpt != ((window->success_ratio *
1802 tbl->expected_tpt[index] + 64) / 128)) {
1803 IWL_ERROR("expected_tpt should have been calculated"
1804 " by now\n");
1805 window->average_tpt = ((window->success_ratio *
Zhu Yib481de92007-09-25 17:54:57 -07001806 tbl->expected_tpt[index] + 64) / 128);
Guy Cohen07bc28e2008-04-23 17:15:03 -07001807 }
1808 }
Zhu Yib481de92007-09-25 17:54:57 -07001809
Ben Cahill77626352007-11-29 11:09:44 +08001810 /* If we are searching for better modulation mode, check success. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001811 if (lq_sta->search_better_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07001812
Ben Cahill77626352007-11-29 11:09:44 +08001813 /* If good success, continue using the "search" mode;
1814 * no need to send new link quality command, since we're
1815 * continuing to use the setup that we've been trying. */
Guy Cohen07bc28e2008-04-23 17:15:03 -07001816 if (window->average_tpt > lq_sta->last_tpt) {
1817
1818 IWL_DEBUG_RATE("LQ: SWITCHING TO CURRENT TABLE "
1819 "suc=%d cur-tpt=%d old-tpt=%d\n",
1820 window->success_ratio,
1821 window->average_tpt,
1822 lq_sta->last_tpt);
1823
1824 if (!is_legacy(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08001825 lq_sta->enable_counter = 1;
Guy Cohen07bc28e2008-04-23 17:15:03 -07001826
Ben Cahill77626352007-11-29 11:09:44 +08001827 /* Swap tables; "search" becomes "active" */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001828 lq_sta->active_tbl = active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001829 current_tpt = window->average_tpt;
Ben Cahill77626352007-11-29 11:09:44 +08001830
1831 /* Else poor success; go back to mode in "active" table */
Zhu Yib481de92007-09-25 17:54:57 -07001832 } else {
Guy Cohen07bc28e2008-04-23 17:15:03 -07001833
1834 IWL_DEBUG_RATE("LQ: GOING BACK TO THE OLD TABLE "
1835 "suc=%d cur-tpt=%d old-tpt=%d\n",
1836 window->success_ratio,
1837 window->average_tpt,
1838 lq_sta->last_tpt);
1839
Ben Cahill77626352007-11-29 11:09:44 +08001840 /* Nullify "search" table */
Zhu Yib481de92007-09-25 17:54:57 -07001841 tbl->lq_type = LQ_NONE;
Ben Cahill77626352007-11-29 11:09:44 +08001842
1843 /* Revert to "active" table */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001844 active_tbl = lq_sta->active_tbl;
1845 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07001846
Ben Cahill77626352007-11-29 11:09:44 +08001847 /* Revert to "active" rate and throughput info */
Tomas Winkler17744ff2008-03-02 01:52:00 +02001848 index = iwl4965_hwrate_to_plcp_idx(
Guy Cohen39e88502008-04-23 17:14:57 -07001849 tbl->current_rate);
Tomas Winklerc33104f2008-01-14 17:46:21 -08001850 current_tpt = lq_sta->last_tpt;
Ben Cahill77626352007-11-29 11:09:44 +08001851
1852 /* Need to set up a new rate table in uCode */
1853 update_lq = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001854 }
Ben Cahill77626352007-11-29 11:09:44 +08001855
1856 /* Either way, we've made a decision; modulation mode
1857 * search is done, allow rate adjustment next time. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001858 lq_sta->search_better_tbl = 0;
Ben Cahill77626352007-11-29 11:09:44 +08001859 done_search = 1; /* Don't switch modes below! */
Zhu Yib481de92007-09-25 17:54:57 -07001860 goto lq_update;
1861 }
1862
Ben Cahill77626352007-11-29 11:09:44 +08001863 /* (Else) not in search of better modulation mode, try for better
1864 * starting rate, while staying in this mode. */
Ester Kummerbf403db2008-05-05 10:22:40 +08001865 high_low = rs_get_adjacent_rate(priv, index, rate_scale_index_msk,
Zhu Yib481de92007-09-25 17:54:57 -07001866 tbl->lq_type);
1867 low = high_low & 0xff;
1868 high = (high_low >> 8) & 0xff;
1869
Ben Cahill77626352007-11-29 11:09:44 +08001870 /* Collect measured throughputs for current and adjacent rates */
Zhu Yib481de92007-09-25 17:54:57 -07001871 current_tpt = window->average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07001872 if (low != IWL_RATE_INVALID)
1873 low_tpt = tbl->win[low].average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07001874 if (high != IWL_RATE_INVALID)
1875 high_tpt = tbl->win[high].average_tpt;
1876
Ben Cahill77626352007-11-29 11:09:44 +08001877 /* Assume rate increase */
Zhu Yib481de92007-09-25 17:54:57 -07001878 scale_action = 1;
1879
Ben Cahill77626352007-11-29 11:09:44 +08001880 /* Too many failures, decrease rate */
Zhu Yib481de92007-09-25 17:54:57 -07001881 if ((window->success_ratio <= IWL_RATE_DECREASE_TH) ||
1882 (current_tpt == 0)) {
1883 IWL_DEBUG_RATE("decrease rate because of low success_ratio\n");
1884 scale_action = -1;
Ben Cahill77626352007-11-29 11:09:44 +08001885
1886 /* No throughput measured yet for adjacent rates; try increase. */
Zhu Yib481de92007-09-25 17:54:57 -07001887 } else if ((low_tpt == IWL_INVALID_VALUE) &&
1888 (high_tpt == IWL_INVALID_VALUE))
1889 scale_action = 1;
Ben Cahill77626352007-11-29 11:09:44 +08001890
1891 /* Both adjacent throughputs are measured, but neither one has better
1892 * throughput; we're using the best rate, don't change it! */
Zhu Yib481de92007-09-25 17:54:57 -07001893 else if ((low_tpt != IWL_INVALID_VALUE) &&
1894 (high_tpt != IWL_INVALID_VALUE) &&
1895 (low_tpt < current_tpt) &&
1896 (high_tpt < current_tpt))
1897 scale_action = 0;
Ben Cahill77626352007-11-29 11:09:44 +08001898
1899 /* At least one adjacent rate's throughput is measured,
1900 * and may have better performance. */
Zhu Yib481de92007-09-25 17:54:57 -07001901 else {
Ben Cahill77626352007-11-29 11:09:44 +08001902 /* Higher adjacent rate's throughput is measured */
Zhu Yib481de92007-09-25 17:54:57 -07001903 if (high_tpt != IWL_INVALID_VALUE) {
Ben Cahill77626352007-11-29 11:09:44 +08001904 /* Higher rate has better throughput */
Zhu Yib481de92007-09-25 17:54:57 -07001905 if (high_tpt > current_tpt)
1906 scale_action = 1;
1907 else {
1908 IWL_DEBUG_RATE
1909 ("decrease rate because of high tpt\n");
1910 scale_action = -1;
1911 }
Ben Cahill77626352007-11-29 11:09:44 +08001912
1913 /* Lower adjacent rate's throughput is measured */
Zhu Yib481de92007-09-25 17:54:57 -07001914 } else if (low_tpt != IWL_INVALID_VALUE) {
Ben Cahill77626352007-11-29 11:09:44 +08001915 /* Lower rate has better throughput */
Zhu Yib481de92007-09-25 17:54:57 -07001916 if (low_tpt > current_tpt) {
1917 IWL_DEBUG_RATE
1918 ("decrease rate because of low tpt\n");
1919 scale_action = -1;
1920 } else
1921 scale_action = 1;
1922 }
1923 }
1924
Ben Cahill77626352007-11-29 11:09:44 +08001925 /* Sanity check; asked for decrease, but success rate or throughput
1926 * has been good at old rate. Don't change it. */
Zhu Yib481de92007-09-25 17:54:57 -07001927 if (scale_action == -1) {
1928 if ((low != IWL_RATE_INVALID) &&
1929 ((window->success_ratio > IWL_RATE_HIGH_TH) ||
1930 (current_tpt > (100 * tbl->expected_tpt[low]))))
1931 scale_action = 0;
Ben Cahill77626352007-11-29 11:09:44 +08001932
1933 /* Sanity check; asked for increase, but success rate has not been great
1934 * even at old rate, higher rate will be worse. Don't change it. */
Zhu Yib481de92007-09-25 17:54:57 -07001935 } else if ((scale_action == 1) &&
1936 (window->success_ratio < IWL_RATE_INCREASE_TH))
1937 scale_action = 0;
1938
1939 switch (scale_action) {
1940 case -1:
Ben Cahill77626352007-11-29 11:09:44 +08001941 /* Decrease starting rate, update uCode's rate table */
Zhu Yib481de92007-09-25 17:54:57 -07001942 if (low != IWL_RATE_INVALID) {
1943 update_lq = 1;
1944 index = low;
1945 }
1946 break;
1947 case 1:
Ben Cahill77626352007-11-29 11:09:44 +08001948 /* Increase starting rate, update uCode's rate table */
Zhu Yib481de92007-09-25 17:54:57 -07001949 if (high != IWL_RATE_INVALID) {
1950 update_lq = 1;
1951 index = high;
1952 }
1953
1954 break;
1955 case 0:
Ben Cahill77626352007-11-29 11:09:44 +08001956 /* No change */
Zhu Yib481de92007-09-25 17:54:57 -07001957 default:
1958 break;
1959 }
1960
Guy Coheneecd6e52008-04-23 17:14:58 -07001961 IWL_DEBUG_RATE("choose rate scale index %d action %d low %d "
Zhu Yib481de92007-09-25 17:54:57 -07001962 "high %d type %d\n",
1963 index, scale_action, low, high, tbl->lq_type);
1964
1965 lq_update:
Ben Cahill77626352007-11-29 11:09:44 +08001966 /* Replace uCode's rate table for the destination station. */
Zhu Yib481de92007-09-25 17:54:57 -07001967 if (update_lq) {
Guy Cohen39e88502008-04-23 17:14:57 -07001968 rate = rate_n_flags_from_tbl(tbl, index, is_green);
Guy Cohen07bc28e2008-04-23 17:15:03 -07001969 rs_fill_link_cmd(priv, lq_sta, rate);
Tomas Winkler66c73db2008-04-15 16:01:40 -07001970 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07001971 }
Ben Cahill77626352007-11-29 11:09:44 +08001972
1973 /* Should we stay with this modulation mode, or search for a new one? */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001974 rs_stay_in_table(lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07001975
Ben Cahill77626352007-11-29 11:09:44 +08001976 /*
1977 * Search for new modulation mode if we're:
1978 * 1) Not changing rates right now
1979 * 2) Not just finishing up a search
1980 * 3) Allowing a new search
1981 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001982 if (!update_lq && !done_search && !lq_sta->stay_in_tbl) {
Ben Cahill77626352007-11-29 11:09:44 +08001983 /* Save current throughput to compare with "search" throughput*/
Tomas Winklerc33104f2008-01-14 17:46:21 -08001984 lq_sta->last_tpt = current_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07001985
Ben Cahill77626352007-11-29 11:09:44 +08001986 /* Select a new "search" modulation mode to try.
1987 * If one is found, set up the new "search" table. */
Zhu Yib481de92007-09-25 17:54:57 -07001988 if (is_legacy(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08001989 rs_move_legacy_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07001990 else if (is_siso(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08001991 rs_move_siso_to_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07001992 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08001993 rs_move_mimo_to_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07001994
Ben Cahill77626352007-11-29 11:09:44 +08001995 /* If new "search" mode was selected, set up in uCode table */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001996 if (lq_sta->search_better_tbl) {
Ben Cahill77626352007-11-29 11:09:44 +08001997 /* Access the "search" table, clear its history. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001998 tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Zhu Yib481de92007-09-25 17:54:57 -07001999 for (i = 0; i < IWL_RATE_COUNT; i++)
2000 rs_rate_scale_clear_window(&(tbl->win[i]));
2001
Ben Cahill77626352007-11-29 11:09:44 +08002002 /* Use new "search" start rate */
Tomas Winkler17744ff2008-03-02 01:52:00 +02002003 index = iwl4965_hwrate_to_plcp_idx(
Guy Cohen39e88502008-04-23 17:14:57 -07002004 tbl->current_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002005
Guy Coheneecd6e52008-04-23 17:14:58 -07002006 IWL_DEBUG_RATE("Switch current mcs: %X index: %d\n",
Guy Cohen39e88502008-04-23 17:14:57 -07002007 tbl->current_rate, index);
Guy Cohen07bc28e2008-04-23 17:15:03 -07002008 rs_fill_link_cmd(priv, lq_sta, tbl->current_rate);
Tomas Winkler66c73db2008-04-15 16:01:40 -07002009 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07002010 }
Zhu Yib481de92007-09-25 17:54:57 -07002011
Ben Cahill77626352007-11-29 11:09:44 +08002012 /* If the "active" (non-search) mode was legacy,
2013 * and we've tried switching antennas,
2014 * but we haven't been able to try HT modes (not available),
2015 * stay with best antenna legacy modulation for a while
2016 * before next round of mode comparisons. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002017 tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07002018 if (is_legacy(tbl1->lq_type) &&
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002019#ifdef CONFIG_IWL4965_HT
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002020 (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE)) &&
Zhu Yib481de92007-09-25 17:54:57 -07002021#endif
Tomas Winklerc33104f2008-01-14 17:46:21 -08002022 (lq_sta->action_counter >= 1)) {
2023 lq_sta->action_counter = 0;
Guy Coheneecd6e52008-04-23 17:14:58 -07002024 IWL_DEBUG_RATE("LQ: STAY in legacy table\n");
Ester Kummerbf403db2008-05-05 10:22:40 +08002025 rs_set_stay_in_table(priv, 1, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002026 }
2027
Ben Cahill77626352007-11-29 11:09:44 +08002028 /* If we're in an HT mode, and all 3 mode switch actions
2029 * have been tried and compared, stay in this best modulation
2030 * mode for a while before next round of mode comparisons. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002031 if (lq_sta->enable_counter &&
2032 (lq_sta->action_counter >= IWL_ACTION_LIMIT)) {
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002033#ifdef CONFIG_IWL4965_HT
2034 if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
2035 (lq_sta->tx_agg_tid_en & (1 << tid)) &&
2036 (tid != MAX_TID_COUNT)) {
Guy Coheneecd6e52008-04-23 17:14:58 -07002037 IWL_DEBUG_RATE("try to aggregate tid %d\n", tid);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002038 rs_tl_turn_on_agg(priv, tid, lq_sta, sta);
Zhu Yib481de92007-09-25 17:54:57 -07002039 }
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002040#endif /*CONFIG_IWL4965_HT */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002041 lq_sta->action_counter = 0;
Ester Kummerbf403db2008-05-05 10:22:40 +08002042 rs_set_stay_in_table(priv, 0, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002043 }
Ben Cahill77626352007-11-29 11:09:44 +08002044
2045 /*
2046 * Else, don't search for a new modulation mode.
2047 * Put new timestamp in stay-in-modulation-mode flush timer if:
2048 * 1) Not changing rates right now
2049 * 2) Not just finishing up a search
2050 * 3) flush timer is empty
2051 */
Zhu Yib481de92007-09-25 17:54:57 -07002052 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002053 if ((!update_lq) && (!done_search) && (!lq_sta->flush_timer))
2054 lq_sta->flush_timer = jiffies;
Zhu Yib481de92007-09-25 17:54:57 -07002055 }
2056
2057out:
Guy Cohen39e88502008-04-23 17:14:57 -07002058 tbl->current_rate = rate_n_flags_from_tbl(tbl, index, is_green);
Zhu Yib481de92007-09-25 17:54:57 -07002059 i = index;
Johannes Berg8318d782008-01-24 19:38:38 +01002060 sta->last_txrate_idx = i;
Zhu Yib481de92007-09-25 17:54:57 -07002061
Johannes Berg8318d782008-01-24 19:38:38 +01002062 /* sta->txrate_idx is an index to A mode rates which start
Zhu Yib481de92007-09-25 17:54:57 -07002063 * at IWL_FIRST_OFDM_RATE
2064 */
Johannes Berg8318d782008-01-24 19:38:38 +01002065 if (lq_sta->band == IEEE80211_BAND_5GHZ)
2066 sta->txrate_idx = i - IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -07002067 else
Johannes Berg8318d782008-01-24 19:38:38 +01002068 sta->txrate_idx = i;
Zhu Yib481de92007-09-25 17:54:57 -07002069
2070 return;
2071}
2072
2073
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002074static void rs_initialize_lq(struct iwl_priv *priv,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002075 struct ieee80211_conf *conf,
Zhu Yib481de92007-09-25 17:54:57 -07002076 struct sta_info *sta)
2077{
Tomas Winklerc33104f2008-01-14 17:46:21 -08002078 struct iwl4965_lq_sta *lq_sta;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002079 struct iwl4965_scale_tbl_info *tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002080 int rate_idx;
Guy Cohenaade00c2008-04-23 17:14:59 -07002081 int i;
Guy Cohen39e88502008-04-23 17:14:57 -07002082 u32 rate;
Guy Cohenaade00c2008-04-23 17:14:59 -07002083 u8 use_green = rs_use_green(priv, conf);
2084 u8 active_tbl = 0;
2085 u8 valid_tx_ant;
Zhu Yib481de92007-09-25 17:54:57 -07002086
2087 if (!sta || !sta->rate_ctrl_priv)
2088 goto out;
2089
Tomas Winklerc33104f2008-01-14 17:46:21 -08002090 lq_sta = (struct iwl4965_lq_sta *)sta->rate_ctrl_priv;
Johannes Berg8318d782008-01-24 19:38:38 +01002091 i = sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002092
Tomas Winklerc33104f2008-01-14 17:46:21 -08002093 if ((lq_sta->lq.sta_id == 0xff) &&
Zhu Yib481de92007-09-25 17:54:57 -07002094 (priv->iw_mode == IEEE80211_IF_TYPE_IBSS))
2095 goto out;
2096
Guy Cohenaade00c2008-04-23 17:14:59 -07002097 valid_tx_ant = priv->hw_params.valid_tx_ant;
2098
Tomas Winklerc33104f2008-01-14 17:46:21 -08002099 if (!lq_sta->search_better_tbl)
2100 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002101 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002102 active_tbl = 1 - lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002103
Tomas Winklerc33104f2008-01-14 17:46:21 -08002104 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07002105
2106 if ((i < 0) || (i >= IWL_RATE_COUNT))
2107 i = 0;
2108
Guy Cohenfde0db32008-04-21 15:42:01 -07002109 /* FIXME:RS: This is also wrong in 4965 */
Tomas Winkler1826dcc2008-05-15 13:54:02 +08002110 rate = iwl_rates[i].plcp;
Guy Cohen39e88502008-04-23 17:14:57 -07002111 rate |= RATE_MCS_ANT_B_MSK;
2112 rate &= ~RATE_MCS_ANT_A_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002113
2114 if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
Guy Cohen39e88502008-04-23 17:14:57 -07002115 rate |= RATE_MCS_CCK_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002116
Guy Cohenfde0db32008-04-21 15:42:01 -07002117 tbl->ant_type = ANT_B;
Guy Cohen39e88502008-04-23 17:14:57 -07002118 rs_get_tbl_info_from_mcs(rate, priv->band, tbl, &rate_idx);
Guy Cohenaade00c2008-04-23 17:14:59 -07002119 if (!rs_is_valid_ant(valid_tx_ant, tbl->ant_type))
2120 rs_toggle_antenna(valid_tx_ant, &rate, tbl);
Zhu Yib481de92007-09-25 17:54:57 -07002121
Guy Cohen39e88502008-04-23 17:14:57 -07002122 rate = rate_n_flags_from_tbl(tbl, rate_idx, use_green);
2123 tbl->current_rate = rate;
Guy Coheneecd6e52008-04-23 17:14:58 -07002124 rs_set_expected_tpt_table(lq_sta, tbl);
Guy Cohen07bc28e2008-04-23 17:15:03 -07002125 rs_fill_link_cmd(NULL, lq_sta, rate);
Tomas Winkler66c73db2008-04-15 16:01:40 -07002126 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07002127 out:
2128 return;
2129}
2130
Mattias Nissler1abbe492007-12-20 13:50:07 +01002131static void rs_get_rate(void *priv_rate, struct net_device *dev,
Johannes Berg8318d782008-01-24 19:38:38 +01002132 struct ieee80211_supported_band *sband,
2133 struct sk_buff *skb,
Mattias Nissler1abbe492007-12-20 13:50:07 +01002134 struct rate_selection *sel)
Zhu Yib481de92007-09-25 17:54:57 -07002135{
2136
2137 int i;
2138 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002139 struct ieee80211_conf *conf = &local->hw.conf;
Zhu Yib481de92007-09-25 17:54:57 -07002140 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2141 struct sta_info *sta;
Michael Wu2bc454b2007-12-25 19:33:16 -05002142 u16 fc;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002143 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002144 struct iwl4965_lq_sta *lq_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002145
Zhu Yi58826352007-09-27 11:27:39 +08002146 IWL_DEBUG_RATE_LIMIT("rate scale calculate new rate for skb\n");
Zhu Yib481de92007-09-25 17:54:57 -07002147
Johannes Bergd0709a62008-02-25 16:27:46 +01002148 rcu_read_lock();
2149
Zhu Yib481de92007-09-25 17:54:57 -07002150 sta = sta_info_get(local, hdr->addr1);
2151
Michael Wu2bc454b2007-12-25 19:33:16 -05002152 /* Send management frames and broadcast/multicast data using lowest
2153 * rate. */
2154 fc = le16_to_cpu(hdr->frame_control);
2155 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1) ||
2156 !sta || !sta->rate_ctrl_priv) {
Johannes Berg2e92e6f2008-05-15 12:55:27 +02002157 sel->rate_idx = rate_lowest_index(local, sband, sta);
Tomas Winklerf4d60822008-03-05 11:31:00 -08002158 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07002159 }
2160
Tomas Winklerc33104f2008-01-14 17:46:21 -08002161 lq_sta = (struct iwl4965_lq_sta *)sta->rate_ctrl_priv;
Johannes Berg8318d782008-01-24 19:38:38 +01002162 i = sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002163
Tomas Winklerc33104f2008-01-14 17:46:21 -08002164 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
2165 !lq_sta->ibss_sta_added) {
Tomas Winkler947b13a2008-04-16 16:34:48 -07002166 u8 sta_id = iwl_find_station(priv, hdr->addr1);
Joe Perches0795af52007-10-03 17:59:30 -07002167 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07002168
2169 if (sta_id == IWL_INVALID_STATION) {
Joe Perches0795af52007-10-03 17:59:30 -07002170 IWL_DEBUG_RATE("LQ: ADD station %s\n",
2171 print_mac(mac, hdr->addr1));
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08002172 sta_id = iwl_add_station_flags(priv, hdr->addr1,
Ron Rindjunsky67d62032007-11-26 16:14:40 +02002173 0, CMD_ASYNC, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07002174 }
2175 if ((sta_id != IWL_INVALID_STATION)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002176 lq_sta->lq.sta_id = sta_id;
2177 lq_sta->lq.rs_table[0].rate_n_flags = 0;
2178 lq_sta->ibss_sta_added = 1;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002179 rs_initialize_lq(priv, conf, sta);
Zhu Yib481de92007-09-25 17:54:57 -07002180 }
Tomas Winklerc33104f2008-01-14 17:46:21 -08002181 if (!lq_sta->ibss_sta_added)
Zhu Yib481de92007-09-25 17:54:57 -07002182 goto done;
2183 }
2184
Tomas Winklerf4d60822008-03-05 11:31:00 -08002185done:
Mattias Nissler1abbe492007-12-20 13:50:07 +01002186 if ((i < 0) || (i > IWL_RATE_COUNT)) {
Johannes Berg2e92e6f2008-05-15 12:55:27 +02002187 sel->rate_idx = rate_lowest_index(local, sband, sta);
Tomas Winklerf4d60822008-03-05 11:31:00 -08002188 goto out;
Mattias Nissler1abbe492007-12-20 13:50:07 +01002189 }
Zhu Yib481de92007-09-25 17:54:57 -07002190
Johannes Berg2e92e6f2008-05-15 12:55:27 +02002191 if (sband->band == IEEE80211_BAND_5GHZ)
2192 i -= IWL_FIRST_OFDM_RATE;
2193 sel->rate_idx = i;
Tomas Winklerf4d60822008-03-05 11:31:00 -08002194out:
2195 rcu_read_unlock();
Zhu Yib481de92007-09-25 17:54:57 -07002196}
2197
Ester Kummerbf403db2008-05-05 10:22:40 +08002198static void *rs_alloc_sta(void *priv_rate, gfp_t gfp)
Zhu Yib481de92007-09-25 17:54:57 -07002199{
Tomas Winklerc33104f2008-01-14 17:46:21 -08002200 struct iwl4965_lq_sta *lq_sta;
Ester Kummerbf403db2008-05-05 10:22:40 +08002201 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07002202 int i, j;
2203
Ester Kummerbf403db2008-05-05 10:22:40 +08002204 priv = (struct iwl_priv *)priv_rate;
Zhu Yib481de92007-09-25 17:54:57 -07002205 IWL_DEBUG_RATE("create station rate scale window\n");
2206
Tomas Winklerc33104f2008-01-14 17:46:21 -08002207 lq_sta = kzalloc(sizeof(struct iwl4965_lq_sta), gfp);
Zhu Yib481de92007-09-25 17:54:57 -07002208
Tomas Winklerc33104f2008-01-14 17:46:21 -08002209 if (lq_sta == NULL)
Zhu Yib481de92007-09-25 17:54:57 -07002210 return NULL;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002211 lq_sta->lq.sta_id = 0xff;
Zhu Yib481de92007-09-25 17:54:57 -07002212
Zhu Yi63fddb92007-09-27 11:27:36 +08002213
Zhu Yib481de92007-09-25 17:54:57 -07002214 for (j = 0; j < LQ_SIZE; j++)
2215 for (i = 0; i < IWL_RATE_COUNT; i++)
Tomas Winklerc33104f2008-01-14 17:46:21 -08002216 rs_rate_scale_clear_window(&(lq_sta->lq_info[j].win[i]));
Zhu Yib481de92007-09-25 17:54:57 -07002217
Tomas Winklerc33104f2008-01-14 17:46:21 -08002218 return lq_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002219}
2220
2221static void rs_rate_init(void *priv_rate, void *priv_sta,
2222 struct ieee80211_local *local,
2223 struct sta_info *sta)
2224{
2225 int i, j;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002226 struct ieee80211_conf *conf = &local->hw.conf;
Johannes Berg8318d782008-01-24 19:38:38 +01002227 struct ieee80211_supported_band *sband;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002228 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002229 struct iwl4965_lq_sta *lq_sta = priv_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002230
Johannes Berg8318d782008-01-24 19:38:38 +01002231 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2232
Tomas Winklerc33104f2008-01-14 17:46:21 -08002233 lq_sta->flush_timer = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01002234 lq_sta->supp_rates = sta->supp_rates[sband->band];
2235 sta->txrate_idx = 3;
Zhu Yib481de92007-09-25 17:54:57 -07002236 for (j = 0; j < LQ_SIZE; j++)
2237 for (i = 0; i < IWL_RATE_COUNT; i++)
Tomas Winklerc33104f2008-01-14 17:46:21 -08002238 rs_rate_scale_clear_window(&(lq_sta->lq_info[j].win[i]));
Zhu Yib481de92007-09-25 17:54:57 -07002239
Guy Cohen07bc28e2008-04-23 17:15:03 -07002240 IWL_DEBUG_RATE("LQ: *** rate scale global init ***\n");
Zhu Yib481de92007-09-25 17:54:57 -07002241 /* TODO: what is a good starting rate for STA? About middle? Maybe not
2242 * the lowest or the highest rate.. Could consider using RSSI from
2243 * previous packets? Need to have IEEE 802.1X auth succeed immediately
2244 * after assoc.. */
2245
Tomas Winklerc33104f2008-01-14 17:46:21 -08002246 lq_sta->ibss_sta_added = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002247 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
Tomas Winkler947b13a2008-04-16 16:34:48 -07002248 u8 sta_id = iwl_find_station(priv, sta->addr);
Joe Perches0795af52007-10-03 17:59:30 -07002249 DECLARE_MAC_BUF(mac);
2250
Zhu Yib481de92007-09-25 17:54:57 -07002251 /* for IBSS the call are from tasklet */
Guy Coheneecd6e52008-04-23 17:14:58 -07002252 IWL_DEBUG_RATE("LQ: ADD station %s\n",
Joe Perches0795af52007-10-03 17:59:30 -07002253 print_mac(mac, sta->addr));
Zhu Yib481de92007-09-25 17:54:57 -07002254
2255 if (sta_id == IWL_INVALID_STATION) {
Joe Perches0795af52007-10-03 17:59:30 -07002256 IWL_DEBUG_RATE("LQ: ADD station %s\n",
2257 print_mac(mac, sta->addr));
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08002258 sta_id = iwl_add_station_flags(priv, sta->addr,
Ron Rindjunsky67d62032007-11-26 16:14:40 +02002259 0, CMD_ASYNC, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07002260 }
2261 if ((sta_id != IWL_INVALID_STATION)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002262 lq_sta->lq.sta_id = sta_id;
2263 lq_sta->lq.rs_table[0].rate_n_flags = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002264 }
2265 /* FIXME: this is w/a remove it later */
2266 priv->assoc_station_added = 1;
2267 }
2268
Ben Cahill77626352007-11-29 11:09:44 +08002269 /* Find highest tx rate supported by hardware and destination station */
Johannes Berg8318d782008-01-24 19:38:38 +01002270 for (i = 0; i < sband->n_bitrates; i++)
2271 if (sta->supp_rates[sband->band] & BIT(i))
2272 sta->txrate_idx = i;
2273
2274 sta->last_txrate_idx = sta->txrate_idx;
2275 /* WTF is with this bogus comment? A doesn't have cck rates */
Ben Cahill77626352007-11-29 11:09:44 +08002276 /* For MODE_IEEE80211A, cck rates are at end of rate table */
Johannes Berg8318d782008-01-24 19:38:38 +01002277 if (local->hw.conf.channel->band == IEEE80211_BAND_5GHZ)
2278 sta->last_txrate_idx += IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -07002279
Tomas Winklerc33104f2008-01-14 17:46:21 -08002280 lq_sta->is_dup = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002281 lq_sta->is_green = rs_use_green(priv, conf);
Guy Cohen39e88502008-04-23 17:14:57 -07002282 lq_sta->active_legacy_rate = priv->active_rate & ~(0x1000);
Tomas Winklerc33104f2008-01-14 17:46:21 -08002283 lq_sta->active_rate_basic = priv->active_rate_basic;
Johannes Berg8318d782008-01-24 19:38:38 +01002284 lq_sta->band = priv->band;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002285#ifdef CONFIG_IWL4965_HT
Ben Cahill77626352007-11-29 11:09:44 +08002286 /*
2287 * active_siso_rate mask includes 9 MBits (bit 5), and CCK (bits 0-3),
2288 * supp_rates[] does not; shift to convert format, force 9 MBits off.
2289 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002290 lq_sta->active_siso_rate =
Guy Cohenfde0db32008-04-21 15:42:01 -07002291 priv->current_ht_config.supp_mcs_set[0] << 1;
2292 lq_sta->active_siso_rate |=
2293 priv->current_ht_config.supp_mcs_set[0] & 0x1;
2294 lq_sta->active_siso_rate &= ~((u16)0x2);
2295 lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -07002296
Ben Cahill77626352007-11-29 11:09:44 +08002297 /* Same here */
Guy Cohenfde0db32008-04-21 15:42:01 -07002298 lq_sta->active_mimo2_rate =
2299 priv->current_ht_config.supp_mcs_set[1] << 1;
2300 lq_sta->active_mimo2_rate |=
2301 priv->current_ht_config.supp_mcs_set[1] & 0x1;
2302 lq_sta->active_mimo2_rate &= ~((u16)0x2);
2303 lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE;
2304
2305 lq_sta->active_mimo3_rate =
2306 priv->current_ht_config.supp_mcs_set[2] << 1;
2307 lq_sta->active_mimo3_rate |=
2308 priv->current_ht_config.supp_mcs_set[2] & 0x1;
2309 lq_sta->active_mimo3_rate &= ~((u16)0x2);
2310 lq_sta->active_mimo3_rate <<= IWL_FIRST_OFDM_RATE;
2311
Guy Cohen07bc28e2008-04-23 17:15:03 -07002312 IWL_DEBUG_RATE("SISO-RATE=%X MIMO2-RATE=%X MIMO3-RATE=%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002313 lq_sta->active_siso_rate,
Guy Cohenfde0db32008-04-21 15:42:01 -07002314 lq_sta->active_mimo2_rate,
2315 lq_sta->active_mimo3_rate);
2316
Guy Cohen07bc28e2008-04-23 17:15:03 -07002317 /* These values will be overriden later */
2318 lq_sta->lq.general_params.single_stream_ant_msk = ANT_A;
2319 lq_sta->lq.general_params.dual_stream_ant_msk = ANT_AB;
2320
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002321 /* as default allow aggregation for all tids */
2322 lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002323#endif /*CONFIG_IWL4965_HT*/
Zhu Yi98d7e092007-09-27 11:27:42 +08002324#ifdef CONFIG_MAC80211_DEBUGFS
Tomas Winklerc33104f2008-01-14 17:46:21 -08002325 lq_sta->drv = priv;
Zhu Yi98d7e092007-09-27 11:27:42 +08002326#endif
Zhu Yib481de92007-09-25 17:54:57 -07002327
2328 if (priv->assoc_station_added)
2329 priv->lq_mngr.lq_ready = 1;
2330
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002331 rs_initialize_lq(priv, conf, sta);
Zhu Yib481de92007-09-25 17:54:57 -07002332}
2333
Guy Cohenfde0db32008-04-21 15:42:01 -07002334static void rs_fill_link_cmd(const struct iwl_priv *priv,
2335 struct iwl4965_lq_sta *lq_sta,
Guy Cohen07bc28e2008-04-23 17:15:03 -07002336 u32 new_rate)
Zhu Yib481de92007-09-25 17:54:57 -07002337{
Guy Cohenaade00c2008-04-23 17:14:59 -07002338 struct iwl4965_scale_tbl_info tbl_type;
Zhu Yib481de92007-09-25 17:54:57 -07002339 int index = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002340 int rate_idx;
Zhu Yi1b696de2007-09-27 11:27:41 +08002341 int repeat_rate = 0;
Guy Cohenaade00c2008-04-23 17:14:59 -07002342 u8 ant_toggle_cnt = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002343 u8 use_ht_possible = 1;
Guy Cohenaade00c2008-04-23 17:14:59 -07002344 u8 valid_tx_ant = 0;
Guy Cohen07bc28e2008-04-23 17:15:03 -07002345 struct iwl_link_quality_cmd *lq_cmd = &lq_sta->lq;
Zhu Yib481de92007-09-25 17:54:57 -07002346
Ben Cahill77626352007-11-29 11:09:44 +08002347 /* Override starting rate (index 0) if needed for debug purposes */
Guy Cohen39e88502008-04-23 17:14:57 -07002348 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Zhu Yi98d7e092007-09-27 11:27:42 +08002349
Guy Cohen39e88502008-04-23 17:14:57 -07002350 /* Interpret new_rate (rate_n_flags) */
Guy Cohenaade00c2008-04-23 17:14:59 -07002351 memset(&tbl_type, 0, sizeof(tbl_type));
Guy Cohen39e88502008-04-23 17:14:57 -07002352 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band,
Zhu Yib481de92007-09-25 17:54:57 -07002353 &tbl_type, &rate_idx);
2354
Ben Cahill77626352007-11-29 11:09:44 +08002355 /* How many times should we repeat the initial rate? */
Zhu Yib481de92007-09-25 17:54:57 -07002356 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07002357 ant_toggle_cnt = 1;
Zhu Yi1b696de2007-09-27 11:27:41 +08002358 repeat_rate = IWL_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002359 } else {
Zhu Yi1b696de2007-09-27 11:27:41 +08002360 repeat_rate = IWL_HT_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002361 }
Zhu Yib481de92007-09-25 17:54:57 -07002362
2363 lq_cmd->general_params.mimo_delimiter =
2364 is_mimo(tbl_type.lq_type) ? 1 : 0;
Ben Cahill77626352007-11-29 11:09:44 +08002365
2366 /* Fill 1st table entry (index 0) */
Guy Cohen39e88502008-04-23 17:14:57 -07002367 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002368
Guy Cohen07bc28e2008-04-23 17:15:03 -07002369 if (num_of_ant(tbl_type.ant_type) == 1) {
2370 lq_cmd->general_params.single_stream_ant_msk =
2371 tbl_type.ant_type;
2372 } else if (num_of_ant(tbl_type.ant_type) == 2) {
2373 lq_cmd->general_params.dual_stream_ant_msk =
2374 tbl_type.ant_type;
2375 } /* otherwise we don't modify the existing value */
Zhu Yib481de92007-09-25 17:54:57 -07002376
2377 index++;
Zhu Yi1b696de2007-09-27 11:27:41 +08002378 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002379
Guy Cohenaade00c2008-04-23 17:14:59 -07002380 if (priv)
2381 valid_tx_ant = priv->hw_params.valid_tx_ant;
2382
Ben Cahill77626352007-11-29 11:09:44 +08002383 /* Fill rest of rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002384 while (index < LINK_QUAL_MAX_RETRY_NUM) {
Ben Cahill77626352007-11-29 11:09:44 +08002385 /* Repeat initial/next rate.
2386 * For legacy IWL_NUMBER_TRY == 1, this loop will not execute.
2387 * For HT IWL_HT_NUMBER_TRY == 3, this executes twice. */
Zhu Yi1b696de2007-09-27 11:27:41 +08002388 while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
Zhu Yib481de92007-09-25 17:54:57 -07002389 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07002390 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2391 ant_toggle_cnt++;
2392 else if (priv &&
2393 rs_toggle_antenna(valid_tx_ant,
2394 &new_rate, &tbl_type))
2395 ant_toggle_cnt = 1;
2396}
Zhu Yi98d7e092007-09-27 11:27:42 +08002397
Ben Cahill77626352007-11-29 11:09:44 +08002398 /* Override next rate if needed for debug purposes */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002399 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Ben Cahill77626352007-11-29 11:09:44 +08002400
2401 /* Fill next table entry */
Zhu Yib481de92007-09-25 17:54:57 -07002402 lq_cmd->rs_table[index].rate_n_flags =
Guy Cohen39e88502008-04-23 17:14:57 -07002403 cpu_to_le32(new_rate);
Zhu Yi1b696de2007-09-27 11:27:41 +08002404 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002405 index++;
2406 }
2407
Guy Cohen39e88502008-04-23 17:14:57 -07002408 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band, &tbl_type,
Zhu Yib481de92007-09-25 17:54:57 -07002409 &rate_idx);
2410
Ben Cahill77626352007-11-29 11:09:44 +08002411 /* Indicate to uCode which entries might be MIMO.
2412 * If initial rate was MIMO, this will finally end up
2413 * as (IWL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
Zhu Yib481de92007-09-25 17:54:57 -07002414 if (is_mimo(tbl_type.lq_type))
2415 lq_cmd->general_params.mimo_delimiter = index;
2416
Ben Cahill77626352007-11-29 11:09:44 +08002417 /* Get next rate */
Guy Cohen39e88502008-04-23 17:14:57 -07002418 new_rate = rs_get_lower_rate(lq_sta, &tbl_type, rate_idx,
2419 use_ht_possible);
Zhu Yib481de92007-09-25 17:54:57 -07002420
Ben Cahill77626352007-11-29 11:09:44 +08002421 /* How many times should we repeat the next rate? */
Zhu Yib481de92007-09-25 17:54:57 -07002422 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07002423 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2424 ant_toggle_cnt++;
2425 else if (priv &&
2426 rs_toggle_antenna(valid_tx_ant,
2427 &new_rate, &tbl_type))
2428 ant_toggle_cnt = 1;
2429
Zhu Yi1b696de2007-09-27 11:27:41 +08002430 repeat_rate = IWL_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002431 } else {
Zhu Yi1b696de2007-09-27 11:27:41 +08002432 repeat_rate = IWL_HT_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002433 }
Zhu Yib481de92007-09-25 17:54:57 -07002434
Ben Cahill77626352007-11-29 11:09:44 +08002435 /* Don't allow HT rates after next pass.
2436 * rs_get_lower_rate() will change type to LQ_A or LQ_G. */
Zhu Yib481de92007-09-25 17:54:57 -07002437 use_ht_possible = 0;
2438
Ben Cahill77626352007-11-29 11:09:44 +08002439 /* Override next rate if needed for debug purposes */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002440 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Ben Cahill77626352007-11-29 11:09:44 +08002441
2442 /* Fill next table entry */
Guy Cohen39e88502008-04-23 17:14:57 -07002443 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002444
2445 index++;
Zhu Yi1b696de2007-09-27 11:27:41 +08002446 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002447 }
2448
Zhu Yib481de92007-09-25 17:54:57 -07002449 lq_cmd->agg_params.agg_dis_start_th = 3;
2450 lq_cmd->agg_params.agg_time_limit = cpu_to_le16(4000);
Zhu Yib481de92007-09-25 17:54:57 -07002451}
2452
2453static void *rs_alloc(struct ieee80211_local *local)
2454{
2455 return local->hw.priv;
2456}
2457/* rate scale requires free function to be implemented */
2458static void rs_free(void *priv_rate)
2459{
2460 return;
2461}
2462
2463static void rs_clear(void *priv_rate)
2464{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002465 struct iwl_priv *priv = (struct iwl_priv *) priv_rate;
Zhu Yib481de92007-09-25 17:54:57 -07002466
2467 IWL_DEBUG_RATE("enter\n");
2468
2469 priv->lq_mngr.lq_ready = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002470
2471 IWL_DEBUG_RATE("leave\n");
2472}
2473
Ester Kummerbf403db2008-05-05 10:22:40 +08002474static void rs_free_sta(void *priv_rate, void *priv_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002475{
Tomas Winklerc33104f2008-01-14 17:46:21 -08002476 struct iwl4965_lq_sta *lq_sta = priv_sta;
Ester Kummerbf403db2008-05-05 10:22:40 +08002477 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07002478
Ester Kummerbf403db2008-05-05 10:22:40 +08002479 priv = (struct iwl_priv *)priv_rate;
Zhu Yib481de92007-09-25 17:54:57 -07002480 IWL_DEBUG_RATE("enter\n");
Tomas Winklerc33104f2008-01-14 17:46:21 -08002481 kfree(lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002482 IWL_DEBUG_RATE("leave\n");
2483}
2484
2485
Zhu Yi93dc6462007-09-27 11:27:37 +08002486#ifdef CONFIG_MAC80211_DEBUGFS
Zhu Yi5ae212c2007-09-27 11:27:38 +08002487static int open_file_generic(struct inode *inode, struct file *file)
2488{
2489 file->private_data = inode->i_private;
2490 return 0;
2491}
Tomas Winklerc33104f2008-01-14 17:46:21 -08002492static void rs_dbgfs_set_mcs(struct iwl4965_lq_sta *lq_sta,
Guy Cohen39e88502008-04-23 17:14:57 -07002493 u32 *rate_n_flags, int index)
Zhu Yi98d7e092007-09-27 11:27:42 +08002494{
Ester Kummerbf403db2008-05-05 10:22:40 +08002495 struct iwl_priv *priv;
2496
2497 priv = lq_sta->drv;
Guy Cohen39e88502008-04-23 17:14:57 -07002498 if (lq_sta->dbg_fixed_rate) {
2499 if (index < 12) {
2500 *rate_n_flags = lq_sta->dbg_fixed_rate;
2501 } else {
2502 if (lq_sta->band == IEEE80211_BAND_5GHZ)
2503 *rate_n_flags = 0x800D;
2504 else
2505 *rate_n_flags = 0x820A;
2506 }
Zhu Yi98d7e092007-09-27 11:27:42 +08002507 IWL_DEBUG_RATE("Fixed rate ON\n");
Guy Cohen39e88502008-04-23 17:14:57 -07002508 } else {
2509 IWL_DEBUG_RATE("Fixed rate OFF\n");
Zhu Yi98d7e092007-09-27 11:27:42 +08002510 }
Zhu Yi98d7e092007-09-27 11:27:42 +08002511}
2512
2513static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
2514 const char __user *user_buf, size_t count, loff_t *ppos)
2515{
Tomas Winklerc33104f2008-01-14 17:46:21 -08002516 struct iwl4965_lq_sta *lq_sta = file->private_data;
Ester Kummerbf403db2008-05-05 10:22:40 +08002517 struct iwl_priv *priv;
Zhu Yi98d7e092007-09-27 11:27:42 +08002518 char buf[64];
2519 int buf_size;
2520 u32 parsed_rate;
2521
Ester Kummerbf403db2008-05-05 10:22:40 +08002522 priv = lq_sta->drv;
Zhu Yi98d7e092007-09-27 11:27:42 +08002523 memset(buf, 0, sizeof(buf));
2524 buf_size = min(count, sizeof(buf) - 1);
2525 if (copy_from_user(buf, user_buf, buf_size))
2526 return -EFAULT;
2527
2528 if (sscanf(buf, "%x", &parsed_rate) == 1)
Guy Cohen39e88502008-04-23 17:14:57 -07002529 lq_sta->dbg_fixed_rate = parsed_rate;
Zhu Yi98d7e092007-09-27 11:27:42 +08002530 else
Guy Cohen39e88502008-04-23 17:14:57 -07002531 lq_sta->dbg_fixed_rate = 0;
Zhu Yi98d7e092007-09-27 11:27:42 +08002532
Guy Cohen39e88502008-04-23 17:14:57 -07002533 lq_sta->active_legacy_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
2534 lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2535 lq_sta->active_mimo2_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2536 lq_sta->active_mimo3_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
Zhu Yi98d7e092007-09-27 11:27:42 +08002537
2538 IWL_DEBUG_RATE("sta_id %d rate 0x%X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07002539 lq_sta->lq.sta_id, lq_sta->dbg_fixed_rate);
Zhu Yi98d7e092007-09-27 11:27:42 +08002540
Guy Cohen39e88502008-04-23 17:14:57 -07002541 if (lq_sta->dbg_fixed_rate) {
Guy Cohen07bc28e2008-04-23 17:15:03 -07002542 rs_fill_link_cmd(NULL, lq_sta, lq_sta->dbg_fixed_rate);
Tomas Winkler66c73db2008-04-15 16:01:40 -07002543 iwl_send_lq_cmd(lq_sta->drv, &lq_sta->lq, CMD_ASYNC);
Zhu Yi98d7e092007-09-27 11:27:42 +08002544 }
2545
2546 return count;
2547}
Zhu Yi0209dc12007-09-27 11:27:43 +08002548
Zhu Yi5ae212c2007-09-27 11:27:38 +08002549static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
2550 char __user *user_buf, size_t count, loff_t *ppos)
2551{
2552 char buff[1024];
2553 int desc = 0;
2554 int i = 0;
2555
Tomas Winklerc33104f2008-01-14 17:46:21 -08002556 struct iwl4965_lq_sta *lq_sta = file->private_data;
Zhu Yi5ae212c2007-09-27 11:27:38 +08002557
Tomas Winklerc33104f2008-01-14 17:46:21 -08002558 desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
Zhu Yi98d7e092007-09-27 11:27:42 +08002559 desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002560 lq_sta->total_failed, lq_sta->total_success,
Guy Cohen39e88502008-04-23 17:14:57 -07002561 lq_sta->active_legacy_rate);
Zhu Yi98d7e092007-09-27 11:27:42 +08002562 desc += sprintf(buff+desc, "fixed rate 0x%X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07002563 lq_sta->dbg_fixed_rate);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002564 desc += sprintf(buff+desc, "general:"
2565 "flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002566 lq_sta->lq.general_params.flags,
2567 lq_sta->lq.general_params.mimo_delimiter,
2568 lq_sta->lq.general_params.single_stream_ant_msk,
2569 lq_sta->lq.general_params.dual_stream_ant_msk);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002570
2571 desc += sprintf(buff+desc, "agg:"
2572 "time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002573 le16_to_cpu(lq_sta->lq.agg_params.agg_time_limit),
2574 lq_sta->lq.agg_params.agg_dis_start_th,
2575 lq_sta->lq.agg_params.agg_frame_cnt_limit);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002576
2577 desc += sprintf(buff+desc,
2578 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002579 lq_sta->lq.general_params.start_rate_index[0],
2580 lq_sta->lq.general_params.start_rate_index[1],
2581 lq_sta->lq.general_params.start_rate_index[2],
2582 lq_sta->lq.general_params.start_rate_index[3]);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002583
2584
2585 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
2586 desc += sprintf(buff+desc, " rate[%d] 0x%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002587 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags));
Zhu Yi5ae212c2007-09-27 11:27:38 +08002588
2589 return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2590}
2591
2592static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
Zhu Yi98d7e092007-09-27 11:27:42 +08002593 .write = rs_sta_dbgfs_scale_table_write,
Zhu Yi5ae212c2007-09-27 11:27:38 +08002594 .read = rs_sta_dbgfs_scale_table_read,
2595 .open = open_file_generic,
2596};
Zhu Yi0209dc12007-09-27 11:27:43 +08002597static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
2598 char __user *user_buf, size_t count, loff_t *ppos)
2599{
2600 char buff[1024];
2601 int desc = 0;
2602 int i, j;
2603
Tomas Winklerc33104f2008-01-14 17:46:21 -08002604 struct iwl4965_lq_sta *lq_sta = file->private_data;
Zhu Yi0209dc12007-09-27 11:27:43 +08002605 for (i = 0; i < LQ_SIZE; i++) {
2606 desc += sprintf(buff+desc, "%s type=%d SGI=%d FAT=%d DUP=%d\n"
2607 "rate=0x%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002608 lq_sta->active_tbl == i?"*":"x",
2609 lq_sta->lq_info[i].lq_type,
2610 lq_sta->lq_info[i].is_SGI,
2611 lq_sta->lq_info[i].is_fat,
2612 lq_sta->lq_info[i].is_dup,
Guy Cohen39e88502008-04-23 17:14:57 -07002613 lq_sta->lq_info[i].current_rate);
Zhu Yi0209dc12007-09-27 11:27:43 +08002614 for (j = 0; j < IWL_RATE_COUNT; j++) {
2615 desc += sprintf(buff+desc,
Tomas Winklerc33104f2008-01-14 17:46:21 -08002616 "counter=%d success=%d %%=%d\n",
2617 lq_sta->lq_info[i].win[j].counter,
2618 lq_sta->lq_info[i].win[j].success_counter,
2619 lq_sta->lq_info[i].win[j].success_ratio);
Zhu Yi0209dc12007-09-27 11:27:43 +08002620 }
2621 }
2622 return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2623}
2624
2625static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
2626 .read = rs_sta_dbgfs_stats_table_read,
2627 .open = open_file_generic,
2628};
Zhu Yi5ae212c2007-09-27 11:27:38 +08002629
Zhu Yi93dc6462007-09-27 11:27:37 +08002630static void rs_add_debugfs(void *priv, void *priv_sta,
2631 struct dentry *dir)
2632{
Tomas Winklerc33104f2008-01-14 17:46:21 -08002633 struct iwl4965_lq_sta *lq_sta = priv_sta;
2634 lq_sta->rs_sta_dbgfs_scale_table_file =
Zhu Yi0209dc12007-09-27 11:27:43 +08002635 debugfs_create_file("rate_scale_table", 0600, dir,
Tomas Winklerc33104f2008-01-14 17:46:21 -08002636 lq_sta, &rs_sta_dbgfs_scale_table_ops);
2637 lq_sta->rs_sta_dbgfs_stats_table_file =
Zhu Yi0209dc12007-09-27 11:27:43 +08002638 debugfs_create_file("rate_stats_table", 0600, dir,
Tomas Winklerc33104f2008-01-14 17:46:21 -08002639 lq_sta, &rs_sta_dbgfs_stats_table_ops);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002640#ifdef CONFIG_IWL4965_HT
2641 lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file =
2642 debugfs_create_u8("tx_agg_tid_enable", 0600, dir,
2643 &lq_sta->tx_agg_tid_en);
2644#endif
2645
Zhu Yi93dc6462007-09-27 11:27:37 +08002646}
2647
2648static void rs_remove_debugfs(void *priv, void *priv_sta)
2649{
Tomas Winklerc33104f2008-01-14 17:46:21 -08002650 struct iwl4965_lq_sta *lq_sta = priv_sta;
2651 debugfs_remove(lq_sta->rs_sta_dbgfs_scale_table_file);
2652 debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002653#ifdef CONFIG_IWL4965_HT
2654 debugfs_remove(lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file);
2655#endif
Zhu Yi93dc6462007-09-27 11:27:37 +08002656}
2657#endif
2658
Zhu Yib481de92007-09-25 17:54:57 -07002659static struct rate_control_ops rs_ops = {
2660 .module = NULL,
2661 .name = RS_NAME,
2662 .tx_status = rs_tx_status,
2663 .get_rate = rs_get_rate,
2664 .rate_init = rs_rate_init,
2665 .clear = rs_clear,
2666 .alloc = rs_alloc,
2667 .free = rs_free,
2668 .alloc_sta = rs_alloc_sta,
2669 .free_sta = rs_free_sta,
Zhu Yi93dc6462007-09-27 11:27:37 +08002670#ifdef CONFIG_MAC80211_DEBUGFS
2671 .add_sta_debugfs = rs_add_debugfs,
2672 .remove_sta_debugfs = rs_remove_debugfs,
2673#endif
Zhu Yib481de92007-09-25 17:54:57 -07002674};
2675
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002676int iwl4965_fill_rs_info(struct ieee80211_hw *hw, char *buf, u8 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07002677{
2678 struct ieee80211_local *local = hw_to_local(hw);
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002679 struct iwl_priv *priv = hw->priv;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002680 struct iwl4965_lq_sta *lq_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002681 struct sta_info *sta;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002682 int cnt = 0, i;
Zhu Yib481de92007-09-25 17:54:57 -07002683 u32 samples = 0, success = 0, good = 0;
2684 unsigned long now = jiffies;
2685 u32 max_time = 0;
2686 u8 lq_type, antenna;
2687
Johannes Bergd0709a62008-02-25 16:27:46 +01002688 rcu_read_lock();
2689
Zhu Yib481de92007-09-25 17:54:57 -07002690 sta = sta_info_get(local, priv->stations[sta_id].sta.sta.addr);
2691 if (!sta || !sta->rate_ctrl_priv) {
Johannes Bergd0709a62008-02-25 16:27:46 +01002692 if (sta)
Zhu Yib481de92007-09-25 17:54:57 -07002693 IWL_DEBUG_RATE("leave - no private rate data!\n");
Johannes Bergd0709a62008-02-25 16:27:46 +01002694 else
Zhu Yib481de92007-09-25 17:54:57 -07002695 IWL_DEBUG_RATE("leave - no station!\n");
Johannes Bergd0709a62008-02-25 16:27:46 +01002696 rcu_read_unlock();
Zhu Yib481de92007-09-25 17:54:57 -07002697 return sprintf(buf, "station %d not found\n", sta_id);
2698 }
2699
Tomas Winklerc33104f2008-01-14 17:46:21 -08002700 lq_sta = (void *)sta->rate_ctrl_priv;
Zhu Yib481de92007-09-25 17:54:57 -07002701
Tomas Winklerc33104f2008-01-14 17:46:21 -08002702 lq_type = lq_sta->lq_info[lq_sta->active_tbl].lq_type;
Guy Cohenfde0db32008-04-21 15:42:01 -07002703 antenna = lq_sta->lq_info[lq_sta->active_tbl].ant_type;
Zhu Yib481de92007-09-25 17:54:57 -07002704
2705 if (is_legacy(lq_type))
2706 i = IWL_RATE_54M_INDEX;
2707 else
2708 i = IWL_RATE_60M_INDEX;
2709 while (1) {
2710 u64 mask;
2711 int j;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002712 int active = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002713
Tomas Winklerc33104f2008-01-14 17:46:21 -08002714 cnt +=
Tomas Winkler1826dcc2008-05-15 13:54:02 +08002715 sprintf(&buf[cnt], " %2dMbs: ", iwl_rates[i].ieee / 2);
Zhu Yib481de92007-09-25 17:54:57 -07002716
2717 mask = (1ULL << (IWL_RATE_MAX_WINDOW - 1));
2718 for (j = 0; j < IWL_RATE_MAX_WINDOW; j++, mask >>= 1)
Tomas Winklerc33104f2008-01-14 17:46:21 -08002719 buf[cnt++] =
2720 (lq_sta->lq_info[active].win[i].data & mask)
Zhu Yib481de92007-09-25 17:54:57 -07002721 ? '1' : '0';
2722
Tomas Winklerc33104f2008-01-14 17:46:21 -08002723 samples += lq_sta->lq_info[active].win[i].counter;
2724 good += lq_sta->lq_info[active].win[i].success_counter;
2725 success += lq_sta->lq_info[active].win[i].success_counter *
Tomas Winkler1826dcc2008-05-15 13:54:02 +08002726 iwl_rates[i].ieee;
Zhu Yib481de92007-09-25 17:54:57 -07002727
Tomas Winklerc33104f2008-01-14 17:46:21 -08002728 if (lq_sta->lq_info[active].win[i].stamp) {
Zhu Yib481de92007-09-25 17:54:57 -07002729 int delta =
2730 jiffies_to_msecs(now -
Tomas Winklerc33104f2008-01-14 17:46:21 -08002731 lq_sta->lq_info[active].win[i].stamp);
Zhu Yib481de92007-09-25 17:54:57 -07002732
2733 if (delta > max_time)
2734 max_time = delta;
2735
Tomas Winklerc33104f2008-01-14 17:46:21 -08002736 cnt += sprintf(&buf[cnt], "%5dms\n", delta);
Zhu Yib481de92007-09-25 17:54:57 -07002737 } else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002738 buf[cnt++] = '\n';
Zhu Yib481de92007-09-25 17:54:57 -07002739
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002740 j = iwl4965_get_prev_ieee_rate(i);
Zhu Yib481de92007-09-25 17:54:57 -07002741 if (j == i)
2742 break;
2743 i = j;
2744 }
2745
Tomas Winkler1826dcc2008-05-15 13:54:02 +08002746 /*
2747 * Display the average rate of all samples taken.
2748 * NOTE: We multiply # of samples by 2 since the IEEE measurement
2749 * added from iwl_rates is actually 2X the rate.
2750 */
Zhu Yib481de92007-09-25 17:54:57 -07002751 if (samples)
Tomas Winklerc33104f2008-01-14 17:46:21 -08002752 cnt += sprintf(&buf[cnt],
Zhu Yib481de92007-09-25 17:54:57 -07002753 "\nAverage rate is %3d.%02dMbs over last %4dms\n"
2754 "%3d%% success (%d good packets over %d tries)\n",
2755 success / (2 * samples), (success * 5 / samples) % 10,
2756 max_time, good * 100 / samples, good, samples);
2757 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002758 cnt += sprintf(&buf[cnt], "\nAverage rate: 0Mbs\n");
2759
2760 cnt += sprintf(&buf[cnt], "\nrate scale type %d antenna %d "
Zhu Yib481de92007-09-25 17:54:57 -07002761 "active_search %d rate index %d\n", lq_type, antenna,
Johannes Berg8318d782008-01-24 19:38:38 +01002762 lq_sta->search_better_tbl, sta->last_txrate_idx);
Zhu Yib481de92007-09-25 17:54:57 -07002763
Johannes Bergd0709a62008-02-25 16:27:46 +01002764 rcu_read_unlock();
Tomas Winklerc33104f2008-01-14 17:46:21 -08002765 return cnt;
Zhu Yib481de92007-09-25 17:54:57 -07002766}
2767
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002768void iwl4965_rate_scale_init(struct ieee80211_hw *hw, s32 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07002769{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002770 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07002771
2772 priv->lq_mngr.lq_ready = 1;
2773}
2774
Reinette Chatre897e1cf2008-03-28 16:21:09 -07002775int iwl4965_rate_control_register(void)
Zhu Yib481de92007-09-25 17:54:57 -07002776{
Reinette Chatre897e1cf2008-03-28 16:21:09 -07002777 return ieee80211_rate_control_register(&rs_ops);
Zhu Yib481de92007-09-25 17:54:57 -07002778}
2779
Reinette Chatre897e1cf2008-03-28 16:21:09 -07002780void iwl4965_rate_control_unregister(void)
Zhu Yib481de92007-09-25 17:54:57 -07002781{
2782 ieee80211_rate_control_unregister(&rs_ops);
2783}
2784