blob: a955f9c1b9ee3045363984d3015a68dafc2334b8 [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,
Johannes Berge039fa42008-05-15 12:55:29 +0200788 struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -0700789{
790 int status;
791 u8 retries;
792 int rs_index, index = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -0800793 struct iwl4965_lq_sta *lq_sta;
Tomas Winkler66c73db2008-04-15 16:01:40 -0700794 struct iwl_link_quality_cmd *table;
Zhu Yib481de92007-09-25 17:54:57 -0700795 struct sta_info *sta;
796 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700797 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
Zhu Yib481de92007-09-25 17:54:57 -0700798 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800799 struct ieee80211_hw *hw = local_to_hw(local);
Johannes Berge039fa42008-05-15 12:55:29 +0200800 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
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 */
Johannes Berge039fa42008-05-15 12:55:29 +0200816 if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
817 !(info->flags & IEEE80211_TX_STAT_AMPDU))
Ron Rindjunsky99556432008-01-28 14:07:25 +0200818 return;
819
Johannes Berge039fa42008-05-15 12:55:29 +0200820 retries = info->status.retry_count;
Zhu Yib481de92007-09-25 17:54:57 -0700821
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 Berge039fa42008-05-15 12:55:29 +0200865 if ((info->tx_rate_idx < 0) ||
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800866 (tbl_type.is_SGI ^
Johannes Berge039fa42008-05-15 12:55:29 +0200867 !!(info->flags & IEEE80211_TX_CTL_SHORT_GI)) ||
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800868 (tbl_type.is_fat ^
Johannes Berge039fa42008-05-15 12:55:29 +0200869 !!(info->flags & IEEE80211_TX_CTL_40_MHZ_WIDTH)) ||
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800870 (tbl_type.is_dup ^
Johannes Berge039fa42008-05-15 12:55:29 +0200871 !!(info->flags & IEEE80211_TX_CTL_DUP_DATA)) ||
872 (tbl_type.ant_type ^ info->antenna_sel_tx) ||
Guy Cohen39e88502008-04-23 17:14:57 -0700873 (!!(tx_rate & RATE_MCS_HT_MSK) ^
Johannes Berge039fa42008-05-15 12:55:29 +0200874 !!(info->flags & IEEE80211_TX_CTL_OFDM_HT)) ||
Guy Cohen39e88502008-04-23 17:14:57 -0700875 (!!(tx_rate & RATE_MCS_GF_MSK) ^
Johannes Berge039fa42008-05-15 12:55:29 +0200876 !!(info->flags & IEEE80211_TX_CTL_GREEN_FIELD)) ||
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800877 (hw->wiphy->bands[priv->band]->bitrates[rs_index].bitrate !=
Johannes Berge039fa42008-05-15 12:55:29 +0200878 hw->wiphy->bands[info->band]->bitrates[info->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 ... */
Johannes Berge039fa42008-05-15 12:55:29 +0200932 status = !!(info->flags & IEEE80211_TX_STAT_ACK);
Zhu Yib481de92007-09-25 17:54:57 -0700933
Ben Cahill77626352007-11-29 11:09:44 +0800934 /* If type matches "search" table,
935 * add final tx status to "search" history */
Zhu Yib481de92007-09-25 17:54:57 -0700936 if ((tbl_type.lq_type == search_tbl->lq_type) &&
Guy Cohenfde0db32008-04-21 15:42:01 -0700937 (tbl_type.ant_type == search_tbl->ant_type) &&
Zhu Yib481de92007-09-25 17:54:57 -0700938 (tbl_type.is_SGI == search_tbl->is_SGI)) {
939 if (search_tbl->expected_tpt)
940 tpt = search_tbl->expected_tpt[rs_index];
941 else
942 tpt = 0;
Johannes Berge039fa42008-05-15 12:55:29 +0200943 if (info->flags & IEEE80211_TX_CTL_AMPDU)
Ron Rindjunsky99556432008-01-28 14:07:25 +0200944 rs_collect_tx_data(search_win, rs_index, tpt,
Johannes Berge039fa42008-05-15 12:55:29 +0200945 info->status.ampdu_ack_len,
946 info->status.ampdu_ack_map);
Ron Rindjunsky99556432008-01-28 14:07:25 +0200947 else
948 rs_collect_tx_data(search_win, rs_index, tpt,
949 1, status);
Ben Cahill77626352007-11-29 11:09:44 +0800950 /* Else if type matches "current/active" table,
951 * add final tx status to "current/active" history */
Zhu Yib481de92007-09-25 17:54:57 -0700952 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
Guy Cohenfde0db32008-04-21 15:42:01 -0700953 (tbl_type.ant_type == curr_tbl->ant_type) &&
Zhu Yib481de92007-09-25 17:54:57 -0700954 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
955 if (curr_tbl->expected_tpt)
956 tpt = curr_tbl->expected_tpt[rs_index];
957 else
958 tpt = 0;
Johannes Berge039fa42008-05-15 12:55:29 +0200959 if (info->flags & IEEE80211_TX_CTL_AMPDU)
Ron Rindjunsky99556432008-01-28 14:07:25 +0200960 rs_collect_tx_data(window, rs_index, tpt,
Johannes Berge039fa42008-05-15 12:55:29 +0200961 info->status.ampdu_ack_len,
962 info->status.ampdu_ack_map);
Ron Rindjunsky99556432008-01-28 14:07:25 +0200963 else
964 rs_collect_tx_data(window, rs_index, tpt,
965 1, status);
Zhu Yib481de92007-09-25 17:54:57 -0700966 }
967
Ben Cahill77626352007-11-29 11:09:44 +0800968 /* If not searching for new mode, increment success/failed counter
969 * ... these help determine when to start searching again */
Tomas Winklerc33104f2008-01-14 17:46:21 -0800970 if (lq_sta->stay_in_tbl) {
Johannes Berge039fa42008-05-15 12:55:29 +0200971 if (info->flags & IEEE80211_TX_CTL_AMPDU) {
972 lq_sta->total_success += info->status.ampdu_ack_map;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200973 lq_sta->total_failed +=
Johannes Berge039fa42008-05-15 12:55:29 +0200974 (info->status.ampdu_ack_len - info->status.ampdu_ack_map);
Ron Rindjunsky99556432008-01-28 14:07:25 +0200975 } else {
976 if (status)
977 lq_sta->total_success++;
978 else
979 lq_sta->total_failed++;
980 }
Zhu Yib481de92007-09-25 17:54:57 -0700981 }
982
Ben Cahill77626352007-11-29 11:09:44 +0800983 /* See if there's a better rate or modulation mode to try. */
Zhu Yib481de92007-09-25 17:54:57 -0700984 rs_rate_scale_perform(priv, dev, hdr, sta);
Tomas Winklerf4d60822008-03-05 11:31:00 -0800985out:
Johannes Bergd0709a62008-02-25 16:27:46 +0100986 rcu_read_unlock();
Zhu Yib481de92007-09-25 17:54:57 -0700987 return;
988}
989
Ben Cahill77626352007-11-29 11:09:44 +0800990/*
991 * Begin a period of staying with a selected modulation mode.
992 * Set "stay_in_tbl" flag to prevent any mode switches.
993 * Set frame tx success limits according to legacy vs. high-throughput,
994 * and reset overall (spanning all rates) tx success history statistics.
995 * These control how long we stay using same modulation mode before
996 * searching for a new mode.
997 */
Ester Kummerbf403db2008-05-05 10:22:40 +0800998static void rs_set_stay_in_table(struct iwl_priv *priv, u8 is_legacy,
Tomas Winklerc33104f2008-01-14 17:46:21 -0800999 struct iwl4965_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07001000{
Guy Coheneecd6e52008-04-23 17:14:58 -07001001 IWL_DEBUG_RATE("we are staying in the same table\n");
Tomas Winklerc33104f2008-01-14 17:46:21 -08001002 lq_sta->stay_in_tbl = 1; /* only place this gets set */
Zhu Yib481de92007-09-25 17:54:57 -07001003 if (is_legacy) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001004 lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT;
1005 lq_sta->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
1006 lq_sta->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
Zhu Yib481de92007-09-25 17:54:57 -07001007 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001008 lq_sta->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
1009 lq_sta->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
1010 lq_sta->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
Zhu Yib481de92007-09-25 17:54:57 -07001011 }
Tomas Winklerc33104f2008-01-14 17:46:21 -08001012 lq_sta->table_count = 0;
1013 lq_sta->total_failed = 0;
1014 lq_sta->total_success = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001015}
1016
Ben Cahill77626352007-11-29 11:09:44 +08001017/*
1018 * Find correct throughput table for given mode of modulation
1019 */
Guy Coheneecd6e52008-04-23 17:14:58 -07001020static void rs_set_expected_tpt_table(struct iwl4965_lq_sta *lq_sta,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001021 struct iwl4965_scale_tbl_info *tbl)
Zhu Yib481de92007-09-25 17:54:57 -07001022{
1023 if (is_legacy(tbl->lq_type)) {
1024 if (!is_a_band(tbl->lq_type))
1025 tbl->expected_tpt = expected_tpt_G;
1026 else
1027 tbl->expected_tpt = expected_tpt_A;
1028 } else if (is_siso(tbl->lq_type)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001029 if (tbl->is_fat && !lq_sta->is_dup)
Zhu Yib481de92007-09-25 17:54:57 -07001030 if (tbl->is_SGI)
1031 tbl->expected_tpt = expected_tpt_siso40MHzSGI;
1032 else
1033 tbl->expected_tpt = expected_tpt_siso40MHz;
1034 else if (tbl->is_SGI)
1035 tbl->expected_tpt = expected_tpt_siso20MHzSGI;
1036 else
1037 tbl->expected_tpt = expected_tpt_siso20MHz;
1038
Guy Cohenfde0db32008-04-21 15:42:01 -07001039 } else if (is_mimo(tbl->lq_type)) { /* FIXME:need to separate mimo2/3 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001040 if (tbl->is_fat && !lq_sta->is_dup)
Zhu Yib481de92007-09-25 17:54:57 -07001041 if (tbl->is_SGI)
1042 tbl->expected_tpt = expected_tpt_mimo40MHzSGI;
1043 else
1044 tbl->expected_tpt = expected_tpt_mimo40MHz;
1045 else if (tbl->is_SGI)
1046 tbl->expected_tpt = expected_tpt_mimo20MHzSGI;
1047 else
1048 tbl->expected_tpt = expected_tpt_mimo20MHz;
1049 } else
1050 tbl->expected_tpt = expected_tpt_G;
1051}
1052
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001053#ifdef CONFIG_IWL4965_HT
Ben Cahill77626352007-11-29 11:09:44 +08001054/*
1055 * Find starting rate for new "search" high-throughput mode of modulation.
1056 * Goal is to find lowest expected rate (under perfect conditions) that is
1057 * above the current measured throughput of "active" mode, to give new mode
1058 * a fair chance to prove itself without too many challenges.
1059 *
1060 * This gets called when transitioning to more aggressive modulation
1061 * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
1062 * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
1063 * to decrease to match "active" throughput. When moving from MIMO to SISO,
1064 * bit rate will typically need to increase, but not if performance was bad.
1065 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001066static s32 rs_get_best_rate(struct iwl_priv *priv,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001067 struct iwl4965_lq_sta *lq_sta,
Ben Cahill77626352007-11-29 11:09:44 +08001068 struct iwl4965_scale_tbl_info *tbl, /* "search" */
Guy Cohenf935a6d2008-04-23 17:15:02 -07001069 u16 rate_mask, s8 index)
Zhu Yib481de92007-09-25 17:54:57 -07001070{
Ben Cahill77626352007-11-29 11:09:44 +08001071 /* "active" values */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001072 struct iwl4965_scale_tbl_info *active_tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001073 &(lq_sta->lq_info[lq_sta->active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07001074 s32 active_sr = active_tbl->win[index].success_ratio;
Zhu Yib481de92007-09-25 17:54:57 -07001075 s32 active_tpt = active_tbl->expected_tpt[index];
Ben Cahill77626352007-11-29 11:09:44 +08001076
1077 /* expected "search" throughput */
1078 s32 *tpt_tbl = tbl->expected_tpt;
1079
1080 s32 new_rate, high, low, start_hi;
Zhu Yib481de92007-09-25 17:54:57 -07001081 u16 high_low;
Guy Cohenf935a6d2008-04-23 17:15:02 -07001082 s8 rate = index;
Zhu Yib481de92007-09-25 17:54:57 -07001083
1084 new_rate = high = low = start_hi = IWL_RATE_INVALID;
1085
1086 for (; ;) {
Ester Kummerbf403db2008-05-05 10:22:40 +08001087 high_low = rs_get_adjacent_rate(priv, rate, rate_mask,
1088 tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -07001089
1090 low = high_low & 0xff;
1091 high = (high_low >> 8) & 0xff;
1092
Ben Cahill77626352007-11-29 11:09:44 +08001093 /*
1094 * Lower the "search" bit rate, to give new "search" mode
1095 * approximately the same throughput as "active" if:
1096 *
1097 * 1) "Active" mode has been working modestly well (but not
1098 * great), and expected "search" throughput (under perfect
1099 * conditions) at candidate rate is above the actual
1100 * measured "active" throughput (but less than expected
1101 * "active" throughput under perfect conditions).
1102 * OR
1103 * 2) "Active" mode has been working perfectly or very well
1104 * and expected "search" throughput (under perfect
1105 * conditions) at candidate rate is above expected
1106 * "active" throughput (under perfect conditions).
1107 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001108 if ((((100 * tpt_tbl[rate]) > lq_sta->last_tpt) &&
Zhu Yib481de92007-09-25 17:54:57 -07001109 ((active_sr > IWL_RATE_DECREASE_TH) &&
1110 (active_sr <= IWL_RATE_HIGH_TH) &&
1111 (tpt_tbl[rate] <= active_tpt))) ||
1112 ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
1113 (tpt_tbl[rate] > active_tpt))) {
1114
Ben Cahill77626352007-11-29 11:09:44 +08001115 /* (2nd or later pass)
1116 * If we've already tried to raise the rate, and are
1117 * now trying to lower it, use the higher rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001118 if (start_hi != IWL_RATE_INVALID) {
1119 new_rate = start_hi;
1120 break;
1121 }
Ben Cahill77626352007-11-29 11:09:44 +08001122
Zhu Yib481de92007-09-25 17:54:57 -07001123 new_rate = rate;
Ben Cahill77626352007-11-29 11:09:44 +08001124
1125 /* Loop again with lower rate */
Zhu Yib481de92007-09-25 17:54:57 -07001126 if (low != IWL_RATE_INVALID)
1127 rate = low;
Ben Cahill77626352007-11-29 11:09:44 +08001128
1129 /* Lower rate not available, use the original */
Zhu Yib481de92007-09-25 17:54:57 -07001130 else
1131 break;
Ben Cahill77626352007-11-29 11:09:44 +08001132
1133 /* Else try to raise the "search" rate to match "active" */
Zhu Yib481de92007-09-25 17:54:57 -07001134 } else {
Ben Cahill77626352007-11-29 11:09:44 +08001135 /* (2nd or later pass)
1136 * If we've already tried to lower the rate, and are
1137 * now trying to raise it, use the lower rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001138 if (new_rate != IWL_RATE_INVALID)
1139 break;
Ben Cahill77626352007-11-29 11:09:44 +08001140
1141 /* Loop again with higher rate */
Zhu Yib481de92007-09-25 17:54:57 -07001142 else if (high != IWL_RATE_INVALID) {
1143 start_hi = high;
1144 rate = high;
Ben Cahill77626352007-11-29 11:09:44 +08001145
1146 /* Higher rate not available, use the original */
Zhu Yib481de92007-09-25 17:54:57 -07001147 } else {
1148 new_rate = rate;
1149 break;
1150 }
1151 }
1152 }
1153
1154 return new_rate;
1155}
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001156#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001157
Ben Cahill77626352007-11-29 11:09:44 +08001158/*
1159 * Set up search table for MIMO
1160 */
Guy Cohenfde0db32008-04-21 15:42:01 -07001161#ifdef CONFIG_IWL4965_HT
1162static int rs_switch_to_mimo2(struct iwl_priv *priv,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001163 struct iwl4965_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001164 struct ieee80211_conf *conf,
1165 struct sta_info *sta,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001166 struct iwl4965_scale_tbl_info *tbl, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001167{
Zhu Yib481de92007-09-25 17:54:57 -07001168 u16 rate_mask;
1169 s32 rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001170 s8 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07001171
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001172 if (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) ||
1173 !sta->ht_info.ht_supported)
Zhu Yib481de92007-09-25 17:54:57 -07001174 return -1;
1175
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001176 if (priv->current_ht_config.tx_mimo_ps_mode == IWL_MIMO_PS_STATIC)
Zhu Yib481de92007-09-25 17:54:57 -07001177 return -1;
1178
Ben Cahill77626352007-11-29 11:09:44 +08001179 /* Need both Tx chains/antennas to support MIMO */
Guy Cohenaade00c2008-04-23 17:14:59 -07001180 if (priv->hw_params.tx_chains_num < 2)
Zhu Yib481de92007-09-25 17:54:57 -07001181 return -1;
1182
Guy Coheneecd6e52008-04-23 17:14:58 -07001183 IWL_DEBUG_RATE("LQ: try to switch to MIMO2\n");
Guy Cohen39e88502008-04-23 17:14:57 -07001184
1185 tbl->lq_type = LQ_MIMO2;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001186 tbl->is_dup = lq_sta->is_dup;
Zhu Yib481de92007-09-25 17:54:57 -07001187 tbl->action = 0;
Guy Cohen39e88502008-04-23 17:14:57 -07001188 rate_mask = lq_sta->active_mimo2_rate;
1189
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001190 if (priv->current_ht_config.supported_chan_width
Guy Cohen39e88502008-04-23 17:14:57 -07001191 == IWL_CHANNEL_WIDTH_40MHZ)
Zhu Yib481de92007-09-25 17:54:57 -07001192 tbl->is_fat = 1;
1193 else
1194 tbl->is_fat = 0;
1195
Guy Cohen39e88502008-04-23 17:14:57 -07001196 /* FIXME: - don't toggle SGI here
Zhu Yib481de92007-09-25 17:54:57 -07001197 if (tbl->is_fat) {
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001198 if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
Zhu Yib481de92007-09-25 17:54:57 -07001199 tbl->is_SGI = 1;
1200 else
1201 tbl->is_SGI = 0;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001202 } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
Zhu Yib481de92007-09-25 17:54:57 -07001203 tbl->is_SGI = 1;
1204 else
1205 tbl->is_SGI = 0;
Guy Cohen39e88502008-04-23 17:14:57 -07001206 */
Zhu Yib481de92007-09-25 17:54:57 -07001207
Guy Coheneecd6e52008-04-23 17:14:58 -07001208 rs_set_expected_tpt_table(lq_sta, tbl);
Zhu Yib481de92007-09-25 17:54:57 -07001209
Guy Cohenf935a6d2008-04-23 17:15:02 -07001210 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
Zhu Yib481de92007-09-25 17:54:57 -07001211
Guy Coheneecd6e52008-04-23 17:14:58 -07001212 IWL_DEBUG_RATE("LQ: MIMO2 best rate %d mask %X\n", rate, rate_mask);
Guy Cohen39e88502008-04-23 17:14:57 -07001213
1214 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
Guy Coheneecd6e52008-04-23 17:14:58 -07001215 IWL_DEBUG_RATE("Can't switch with index %d rate mask %x\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001216 rate, rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07001217 return -1;
Guy Cohen39e88502008-04-23 17:14:57 -07001218 }
1219 tbl->current_rate = rate_n_flags_from_tbl(tbl, rate, is_green);
Zhu Yib481de92007-09-25 17:54:57 -07001220
Guy Coheneecd6e52008-04-23 17:14:58 -07001221 IWL_DEBUG_RATE("LQ: Switch to new mcs %X index is green %X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001222 tbl->current_rate, is_green);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001223 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07001224}
Guy Cohenfde0db32008-04-21 15:42:01 -07001225#else
1226static int rs_switch_to_mimo2(struct iwl_priv *priv,
1227 struct iwl4965_lq_sta *lq_sta,
1228 struct ieee80211_conf *conf,
1229 struct sta_info *sta,
1230 struct iwl4965_scale_tbl_info *tbl, int index)
1231{
1232 return -1;
1233}
1234#endif /*CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001235
Ben Cahill77626352007-11-29 11:09:44 +08001236/*
1237 * Set up search table for SISO
1238 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001239static int rs_switch_to_siso(struct iwl_priv *priv,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001240 struct iwl4965_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001241 struct ieee80211_conf *conf,
1242 struct sta_info *sta,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001243 struct iwl4965_scale_tbl_info *tbl, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001244{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001245#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07001246 u16 rate_mask;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001247 u8 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07001248 s32 rate;
1249
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001250 if (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) ||
1251 !sta->ht_info.ht_supported)
Zhu Yib481de92007-09-25 17:54:57 -07001252 return -1;
1253
Guy Coheneecd6e52008-04-23 17:14:58 -07001254 IWL_DEBUG_RATE("LQ: try to switch to SISO\n");
Guy Cohen39e88502008-04-23 17:14:57 -07001255
Tomas Winklerc33104f2008-01-14 17:46:21 -08001256 tbl->is_dup = lq_sta->is_dup;
Zhu Yib481de92007-09-25 17:54:57 -07001257 tbl->lq_type = LQ_SISO;
1258 tbl->action = 0;
Guy Cohen39e88502008-04-23 17:14:57 -07001259 rate_mask = lq_sta->active_siso_rate;
Zhu Yib481de92007-09-25 17:54:57 -07001260
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001261 if (priv->current_ht_config.supported_chan_width
1262 == IWL_CHANNEL_WIDTH_40MHZ)
Zhu Yib481de92007-09-25 17:54:57 -07001263 tbl->is_fat = 1;
1264 else
1265 tbl->is_fat = 0;
1266
Guy Cohen39e88502008-04-23 17:14:57 -07001267 /* FIXME: - don't toggle SGI here
Zhu Yib481de92007-09-25 17:54:57 -07001268 if (tbl->is_fat) {
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001269 if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
Zhu Yib481de92007-09-25 17:54:57 -07001270 tbl->is_SGI = 1;
1271 else
1272 tbl->is_SGI = 0;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001273 } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
Zhu Yib481de92007-09-25 17:54:57 -07001274 tbl->is_SGI = 1;
1275 else
1276 tbl->is_SGI = 0;
Guy Cohen39e88502008-04-23 17:14:57 -07001277 */
Zhu Yib481de92007-09-25 17:54:57 -07001278
1279 if (is_green)
Guy Cohen39e88502008-04-23 17:14:57 -07001280 tbl->is_SGI = 0; /*11n spec: no SGI in SISO+Greenfield*/
Zhu Yib481de92007-09-25 17:54:57 -07001281
Guy Coheneecd6e52008-04-23 17:14:58 -07001282 rs_set_expected_tpt_table(lq_sta, tbl);
Guy Cohenf935a6d2008-04-23 17:15:02 -07001283 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
Zhu Yib481de92007-09-25 17:54:57 -07001284
Guy Coheneecd6e52008-04-23 17:14:58 -07001285 IWL_DEBUG_RATE("LQ: get best rate %d mask %X\n", rate, rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07001286 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
Guy Coheneecd6e52008-04-23 17:14:58 -07001287 IWL_DEBUG_RATE("can not switch with index %d rate mask %x\n",
Zhu Yib481de92007-09-25 17:54:57 -07001288 rate, rate_mask);
1289 return -1;
1290 }
Guy Cohen39e88502008-04-23 17:14:57 -07001291 tbl->current_rate = rate_n_flags_from_tbl(tbl, rate, is_green);
Guy Coheneecd6e52008-04-23 17:14:58 -07001292 IWL_DEBUG_RATE("LQ: Switch to new mcs %X index is green %X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001293 tbl->current_rate, is_green);
Mohamed Abbas403ab562007-11-06 22:06:25 -08001294 return 0;
1295#else
1296 return -1;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001297#endif /*CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001298}
1299
Ben Cahill77626352007-11-29 11:09:44 +08001300/*
1301 * Try to switch to new modulation mode from legacy
1302 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001303static int rs_move_legacy_other(struct iwl_priv *priv,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001304 struct iwl4965_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001305 struct ieee80211_conf *conf,
1306 struct sta_info *sta,
Zhu Yib481de92007-09-25 17:54:57 -07001307 int index)
1308{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001309 struct iwl4965_scale_tbl_info *tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001310 &(lq_sta->lq_info[lq_sta->active_tbl]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001311 struct iwl4965_scale_tbl_info *search_tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001312 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001313 struct iwl4965_rate_scale_data *window = &(tbl->win[index]);
1314 u32 sz = (sizeof(struct iwl4965_scale_tbl_info) -
1315 (sizeof(struct iwl4965_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001316 u8 start_action = tbl->action;
Guy Cohenfde0db32008-04-21 15:42:01 -07001317 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1318 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001319
1320 for (; ;) {
1321 switch (tbl->action) {
1322 case IWL_LEGACY_SWITCH_ANTENNA:
Guy Cohenf935a6d2008-04-23 17:15:02 -07001323 IWL_DEBUG_RATE("LQ: Legacy toggle Antenna\n");
Zhu Yib481de92007-09-25 17:54:57 -07001324
Tomas Winklerc33104f2008-01-14 17:46:21 -08001325 lq_sta->action_counter++;
Ben Cahill77626352007-11-29 11:09:44 +08001326
1327 /* Don't change antenna if success has been great */
Zhu Yib481de92007-09-25 17:54:57 -07001328 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1329 break;
Ben Cahill77626352007-11-29 11:09:44 +08001330
Ben Cahill77626352007-11-29 11:09:44 +08001331 /* Set up search table to try other antenna */
Zhu Yib481de92007-09-25 17:54:57 -07001332 memcpy(search_tbl, tbl, sz);
1333
Guy Cohenaade00c2008-04-23 17:14:59 -07001334 if (rs_toggle_antenna(valid_tx_ant,
1335 &search_tbl->current_rate, search_tbl)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07001336 lq_sta->search_better_tbl = 1;
1337 goto out;
1338 }
Guy Cohena5e8b502008-05-29 16:35:11 +08001339 break;
Zhu Yib481de92007-09-25 17:54:57 -07001340 case IWL_LEGACY_SWITCH_SISO:
Guy Coheneecd6e52008-04-23 17:14:58 -07001341 IWL_DEBUG_RATE("LQ: Legacy switch to SISO\n");
Ben Cahill77626352007-11-29 11:09:44 +08001342
1343 /* Set up search table to try SISO */
Zhu Yib481de92007-09-25 17:54:57 -07001344 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001345 search_tbl->is_SGI = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001346 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001347 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001348 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001349 lq_sta->search_better_tbl = 1;
1350 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001351 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001352 }
Zhu Yib481de92007-09-25 17:54:57 -07001353
1354 break;
Guy Cohenfde0db32008-04-21 15:42:01 -07001355 case IWL_LEGACY_SWITCH_MIMO2:
Guy Coheneecd6e52008-04-23 17:14:58 -07001356 IWL_DEBUG_RATE("LQ: Legacy switch to MIMO2\n");
Ben Cahill77626352007-11-29 11:09:44 +08001357
1358 /* Set up search table to try MIMO */
Zhu Yib481de92007-09-25 17:54:57 -07001359 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001360 search_tbl->is_SGI = 0;
Guy Cohenfde0db32008-04-21 15:42:01 -07001361 search_tbl->ant_type = ANT_AB;/*FIXME:RS*/
1362 /*FIXME:RS:need to check ant validity*/
1363 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001364 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001365 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001366 lq_sta->search_better_tbl = 1;
1367 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001368 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001369 }
Zhu Yib481de92007-09-25 17:54:57 -07001370 break;
1371 }
1372 tbl->action++;
Guy Cohenfde0db32008-04-21 15:42:01 -07001373 if (tbl->action > IWL_LEGACY_SWITCH_MIMO2)
Zhu Yib481de92007-09-25 17:54:57 -07001374 tbl->action = IWL_LEGACY_SWITCH_ANTENNA;
1375
1376 if (tbl->action == start_action)
1377 break;
1378
1379 }
1380 return 0;
1381
1382 out:
1383 tbl->action++;
Guy Cohenfde0db32008-04-21 15:42:01 -07001384 if (tbl->action > IWL_LEGACY_SWITCH_MIMO2)
Zhu Yib481de92007-09-25 17:54:57 -07001385 tbl->action = IWL_LEGACY_SWITCH_ANTENNA;
1386 return 0;
1387
1388}
1389
Ben Cahill77626352007-11-29 11:09:44 +08001390/*
1391 * Try to switch to new modulation mode from SISO
1392 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001393static int rs_move_siso_to_other(struct iwl_priv *priv,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001394 struct iwl4965_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001395 struct ieee80211_conf *conf,
1396 struct sta_info *sta,
Zhu Yib481de92007-09-25 17:54:57 -07001397 int index)
1398{
Tomas Winklerc33104f2008-01-14 17:46:21 -08001399 u8 is_green = lq_sta->is_green;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001400 struct iwl4965_scale_tbl_info *tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001401 &(lq_sta->lq_info[lq_sta->active_tbl]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001402 struct iwl4965_scale_tbl_info *search_tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001403 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001404 struct iwl4965_rate_scale_data *window = &(tbl->win[index]);
1405 u32 sz = (sizeof(struct iwl4965_scale_tbl_info) -
1406 (sizeof(struct iwl4965_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001407 u8 start_action = tbl->action;
Guy Cohenfde0db32008-04-21 15:42:01 -07001408 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1409 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001410
1411 for (;;) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001412 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001413 switch (tbl->action) {
1414 case IWL_SISO_SWITCH_ANTENNA:
Guy Coheneecd6e52008-04-23 17:14:58 -07001415 IWL_DEBUG_RATE("LQ: SISO toggle Antenna\n");
Zhu Yib481de92007-09-25 17:54:57 -07001416 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1417 break;
Zhu Yib481de92007-09-25 17:54:57 -07001418
1419 memcpy(search_tbl, tbl, sz);
Guy Cohenaade00c2008-04-23 17:14:59 -07001420 if (rs_toggle_antenna(valid_tx_ant,
1421 &search_tbl->current_rate, search_tbl)) {
1422 lq_sta->search_better_tbl = 1;
1423 goto out;
1424 }
Guy Cohena5e8b502008-05-29 16:35:11 +08001425 break;
Guy Cohenfde0db32008-04-21 15:42:01 -07001426 case IWL_SISO_SWITCH_MIMO2:
Guy Cohena5e8b502008-05-29 16:35:11 +08001427 IWL_DEBUG_RATE("LQ: SISO switch to MIMO2\n");
Zhu Yib481de92007-09-25 17:54:57 -07001428 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001429 search_tbl->is_SGI = 0;
Guy Cohenfde0db32008-04-21 15:42:01 -07001430 search_tbl->ant_type = ANT_AB; /*FIXME:RS*/
1431 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001432 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001433 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001434 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001435 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001436 }
Zhu Yib481de92007-09-25 17:54:57 -07001437 break;
1438 case IWL_SISO_SWITCH_GI:
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001439 if (!tbl->is_fat &&
1440 !(priv->current_ht_config.sgf &
1441 HT_SHORT_GI_20MHZ))
1442 break;
1443 if (tbl->is_fat &&
1444 !(priv->current_ht_config.sgf &
1445 HT_SHORT_GI_40MHZ))
1446 break;
1447
Guy Coheneecd6e52008-04-23 17:14:58 -07001448 IWL_DEBUG_RATE("LQ: SISO toggle SGI/NGI\n");
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001449
Zhu Yib481de92007-09-25 17:54:57 -07001450 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001451 if (is_green) {
1452 if (!tbl->is_SGI)
1453 break;
1454 else
1455 IWL_ERROR("SGI was set in GF+SISO\n");
Zhu Yib481de92007-09-25 17:54:57 -07001456 }
Guy Cohen39e88502008-04-23 17:14:57 -07001457 search_tbl->is_SGI = !tbl->is_SGI;
Guy Coheneecd6e52008-04-23 17:14:58 -07001458 rs_set_expected_tpt_table(lq_sta, search_tbl);
Guy Cohen39e88502008-04-23 17:14:57 -07001459 if (tbl->is_SGI) {
1460 s32 tpt = lq_sta->last_tpt / 100;
1461 if (tpt >= search_tbl->expected_tpt[index])
1462 break;
1463 }
1464 search_tbl->current_rate = rate_n_flags_from_tbl(
1465 search_tbl, index, is_green);
1466 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001467 goto out;
1468 }
1469 tbl->action++;
1470 if (tbl->action > IWL_SISO_SWITCH_GI)
1471 tbl->action = IWL_SISO_SWITCH_ANTENNA;
1472
1473 if (tbl->action == start_action)
1474 break;
1475 }
1476 return 0;
1477
1478 out:
1479 tbl->action++;
1480 if (tbl->action > IWL_SISO_SWITCH_GI)
1481 tbl->action = IWL_SISO_SWITCH_ANTENNA;
1482 return 0;
1483}
1484
Ben Cahill77626352007-11-29 11:09:44 +08001485/*
1486 * Try to switch to new modulation mode from MIMO
1487 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001488static int rs_move_mimo_to_other(struct iwl_priv *priv,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001489 struct iwl4965_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001490 struct ieee80211_conf *conf,
1491 struct sta_info *sta,
Zhu Yib481de92007-09-25 17:54:57 -07001492 int index)
1493{
Tomas Winklerc33104f2008-01-14 17:46:21 -08001494 s8 is_green = lq_sta->is_green;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001495 struct iwl4965_scale_tbl_info *tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001496 &(lq_sta->lq_info[lq_sta->active_tbl]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001497 struct iwl4965_scale_tbl_info *search_tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001498 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001499 u32 sz = (sizeof(struct iwl4965_scale_tbl_info) -
1500 (sizeof(struct iwl4965_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001501 u8 start_action = tbl->action;
Guy Cohenaade00c2008-04-23 17:14:59 -07001502 /*u8 valid_tx_ant = priv->hw_params.valid_tx_ant;*/
1503 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001504
1505 for (;;) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001506 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001507 switch (tbl->action) {
1508 case IWL_MIMO_SWITCH_ANTENNA_A:
1509 case IWL_MIMO_SWITCH_ANTENNA_B:
Guy Coheneecd6e52008-04-23 17:14:58 -07001510 IWL_DEBUG_RATE("LQ: MIMO2 switch to SISO\n");
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001511
Ben Cahill77626352007-11-29 11:09:44 +08001512 /* Set up new search table for SISO */
Zhu Yib481de92007-09-25 17:54:57 -07001513 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001514
1515 /*FIXME:RS:need to check ant validity + C*/
Zhu Yib481de92007-09-25 17:54:57 -07001516 if (tbl->action == IWL_MIMO_SWITCH_ANTENNA_A)
Guy Cohenfde0db32008-04-21 15:42:01 -07001517 search_tbl->ant_type = ANT_A;
Zhu Yib481de92007-09-25 17:54:57 -07001518 else
Guy Cohenfde0db32008-04-21 15:42:01 -07001519 search_tbl->ant_type = ANT_B;
Zhu Yib481de92007-09-25 17:54:57 -07001520
Tomas Winklerc33104f2008-01-14 17:46:21 -08001521 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001522 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001523 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001524 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001525 goto out;
1526 }
1527 break;
1528
1529 case IWL_MIMO_SWITCH_GI:
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001530 if (!tbl->is_fat &&
1531 !(priv->current_ht_config.sgf &
1532 HT_SHORT_GI_20MHZ))
1533 break;
1534 if (tbl->is_fat &&
1535 !(priv->current_ht_config.sgf &
1536 HT_SHORT_GI_40MHZ))
1537 break;
1538
Guy Coheneecd6e52008-04-23 17:14:58 -07001539 IWL_DEBUG_RATE("LQ: MIMO toggle SGI/NGI\n");
Ben Cahill77626352007-11-29 11:09:44 +08001540
1541 /* Set up new search table for MIMO */
Zhu Yib481de92007-09-25 17:54:57 -07001542 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001543 search_tbl->is_SGI = !tbl->is_SGI;
Guy Coheneecd6e52008-04-23 17:14:58 -07001544 rs_set_expected_tpt_table(lq_sta, search_tbl);
Ben Cahill77626352007-11-29 11:09:44 +08001545 /*
1546 * If active table already uses the fastest possible
1547 * modulation (dual stream with short guard interval),
1548 * and it's working well, there's no need to look
1549 * for a better type of modulation!
1550 */
Guy Cohen39e88502008-04-23 17:14:57 -07001551 if (tbl->is_SGI) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001552 s32 tpt = lq_sta->last_tpt / 100;
Guy Cohen39e88502008-04-23 17:14:57 -07001553 if (tpt >= search_tbl->expected_tpt[index])
1554 break;
Zhu Yib481de92007-09-25 17:54:57 -07001555 }
Guy Cohen39e88502008-04-23 17:14:57 -07001556 search_tbl->current_rate = rate_n_flags_from_tbl(
1557 search_tbl, index, is_green);
1558 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001559 goto out;
1560
1561 }
1562 tbl->action++;
1563 if (tbl->action > IWL_MIMO_SWITCH_GI)
1564 tbl->action = IWL_MIMO_SWITCH_ANTENNA_A;
1565
1566 if (tbl->action == start_action)
1567 break;
1568 }
1569
1570 return 0;
1571 out:
1572 tbl->action++;
1573 if (tbl->action > IWL_MIMO_SWITCH_GI)
1574 tbl->action = IWL_MIMO_SWITCH_ANTENNA_A;
1575 return 0;
1576
1577}
1578
Ben Cahill77626352007-11-29 11:09:44 +08001579/*
1580 * Check whether we should continue using same modulation mode, or
1581 * begin search for a new mode, based on:
1582 * 1) # tx successes or failures while using this mode
1583 * 2) # times calling this function
1584 * 3) elapsed time in this mode (not used, for now)
1585 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001586static void rs_stay_in_table(struct iwl4965_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07001587{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001588 struct iwl4965_scale_tbl_info *tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001589 int i;
1590 int active_tbl;
1591 int flush_interval_passed = 0;
Ester Kummerbf403db2008-05-05 10:22:40 +08001592 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07001593
Ester Kummerbf403db2008-05-05 10:22:40 +08001594 priv = lq_sta->drv;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001595 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001596
Tomas Winklerc33104f2008-01-14 17:46:21 -08001597 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07001598
Ben Cahill77626352007-11-29 11:09:44 +08001599 /* If we've been disallowing search, see if we should now allow it */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001600 if (lq_sta->stay_in_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07001601
Ben Cahill77626352007-11-29 11:09:44 +08001602 /* Elapsed time using current modulation mode */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001603 if (lq_sta->flush_timer)
Zhu Yib481de92007-09-25 17:54:57 -07001604 flush_interval_passed =
1605 time_after(jiffies,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001606 (unsigned long)(lq_sta->flush_timer +
Zhu Yib481de92007-09-25 17:54:57 -07001607 IWL_RATE_SCALE_FLUSH_INTVL));
1608
Ben Cahill77626352007-11-29 11:09:44 +08001609 /*
1610 * Check if we should allow search for new modulation mode.
1611 * If many frames have failed or succeeded, or we've used
1612 * this same modulation for a long time, allow search, and
1613 * reset history stats that keep track of whether we should
1614 * allow a new search. Also (below) reset all bitmaps and
1615 * stats in active history.
1616 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001617 if ((lq_sta->total_failed > lq_sta->max_failure_limit) ||
1618 (lq_sta->total_success > lq_sta->max_success_limit) ||
1619 ((!lq_sta->search_better_tbl) && (lq_sta->flush_timer)
Zhu Yib481de92007-09-25 17:54:57 -07001620 && (flush_interval_passed))) {
Guy Coheneecd6e52008-04-23 17:14:58 -07001621 IWL_DEBUG_RATE("LQ: stay is expired %d %d %d\n:",
Tomas Winklerc33104f2008-01-14 17:46:21 -08001622 lq_sta->total_failed,
1623 lq_sta->total_success,
Zhu Yib481de92007-09-25 17:54:57 -07001624 flush_interval_passed);
Ben Cahill77626352007-11-29 11:09:44 +08001625
1626 /* Allow search for new mode */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001627 lq_sta->stay_in_tbl = 0; /* only place reset */
1628 lq_sta->total_failed = 0;
1629 lq_sta->total_success = 0;
1630 lq_sta->flush_timer = 0;
Ben Cahill77626352007-11-29 11:09:44 +08001631
1632 /*
1633 * Else if we've used this modulation mode enough repetitions
1634 * (regardless of elapsed time or success/failure), reset
1635 * history bitmaps and rate-specific stats for all rates in
1636 * active table.
1637 */
Mohamed Abbas403ab562007-11-06 22:06:25 -08001638 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001639 lq_sta->table_count++;
1640 if (lq_sta->table_count >=
1641 lq_sta->table_count_limit) {
1642 lq_sta->table_count = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001643
Guy Coheneecd6e52008-04-23 17:14:58 -07001644 IWL_DEBUG_RATE("LQ: stay in table clear win\n");
Zhu Yib481de92007-09-25 17:54:57 -07001645 for (i = 0; i < IWL_RATE_COUNT; i++)
1646 rs_rate_scale_clear_window(
1647 &(tbl->win[i]));
1648 }
1649 }
1650
Ben Cahill77626352007-11-29 11:09:44 +08001651 /* If transitioning to allow "search", reset all history
1652 * bitmaps and stats in active table (this will become the new
1653 * "search" table). */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001654 if (!lq_sta->stay_in_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07001655 for (i = 0; i < IWL_RATE_COUNT; i++)
1656 rs_rate_scale_clear_window(&(tbl->win[i]));
1657 }
1658 }
1659}
1660
Ben Cahill77626352007-11-29 11:09:44 +08001661/*
1662 * Do rate scaling and search for new modulation mode.
1663 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001664static void rs_rate_scale_perform(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001665 struct net_device *dev,
1666 struct ieee80211_hdr *hdr,
1667 struct sta_info *sta)
1668{
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001669 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1670 struct ieee80211_hw *hw = local_to_hw(local);
1671 struct ieee80211_conf *conf = &hw->conf;
Zhu Yib481de92007-09-25 17:54:57 -07001672 int low = IWL_RATE_INVALID;
1673 int high = IWL_RATE_INVALID;
1674 int index;
1675 int i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001676 struct iwl4965_rate_scale_data *window = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001677 int current_tpt = IWL_INVALID_VALUE;
1678 int low_tpt = IWL_INVALID_VALUE;
1679 int high_tpt = IWL_INVALID_VALUE;
1680 u32 fail_count;
1681 s8 scale_action = 0;
1682 u16 fc, rate_mask;
1683 u8 update_lq = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001684 struct iwl4965_lq_sta *lq_sta;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001685 struct iwl4965_scale_tbl_info *tbl, *tbl1;
Zhu Yib481de92007-09-25 17:54:57 -07001686 u16 rate_scale_index_msk = 0;
Guy Cohen39e88502008-04-23 17:14:57 -07001687 u32 rate;
Zhu Yib481de92007-09-25 17:54:57 -07001688 u8 is_green = 0;
1689 u8 active_tbl = 0;
1690 u8 done_search = 0;
1691 u16 high_low;
Guy Cohena5e8b502008-05-29 16:35:11 +08001692 s32 sr;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001693#ifdef CONFIG_IWL4965_HT
1694 u8 tid = MAX_TID_COUNT;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001695#endif
Zhu Yib481de92007-09-25 17:54:57 -07001696
1697 IWL_DEBUG_RATE("rate scale calculate new rate for skb\n");
1698
1699 fc = le16_to_cpu(hdr->frame_control);
1700 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1)) {
1701 /* Send management frames and broadcast/multicast data using
1702 * lowest rate. */
1703 /* TODO: this could probably be improved.. */
1704 return;
1705 }
1706
1707 if (!sta || !sta->rate_ctrl_priv)
1708 return;
1709
1710 if (!priv->lq_mngr.lq_ready) {
1711 IWL_DEBUG_RATE("still rate scaling not ready\n");
1712 return;
1713 }
Tomas Winklerc33104f2008-01-14 17:46:21 -08001714 lq_sta = (struct iwl4965_lq_sta *)sta->rate_ctrl_priv;
Zhu Yib481de92007-09-25 17:54:57 -07001715
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001716#ifdef CONFIG_IWL4965_HT
Tomas Winkler54dbb522008-05-15 13:54:06 +08001717 rs_tl_add_packet(lq_sta, hdr);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001718#endif
Ben Cahill77626352007-11-29 11:09:44 +08001719 /*
1720 * Select rate-scale / modulation-mode table to work with in
1721 * the rest of this function: "search" if searching for better
1722 * modulation mode, or "active" if doing rate scaling within a mode.
1723 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001724 if (!lq_sta->search_better_tbl)
1725 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001726 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08001727 active_tbl = 1 - lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001728
Tomas Winklerc33104f2008-01-14 17:46:21 -08001729 tbl = &(lq_sta->lq_info[active_tbl]);
1730 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07001731
Ben Cahill77626352007-11-29 11:09:44 +08001732 /* current tx rate */
Johannes Berg8318d782008-01-24 19:38:38 +01001733 index = sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07001734
1735 IWL_DEBUG_RATE("Rate scale index %d for type %d\n", index,
1736 tbl->lq_type);
1737
Ben Cahill77626352007-11-29 11:09:44 +08001738 /* rates available for this association, and for modulation mode */
Guy Coheneecd6e52008-04-23 17:14:58 -07001739 rate_mask = rs_get_supported_rates(lq_sta, hdr, tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -07001740
1741 IWL_DEBUG_RATE("mask 0x%04X \n", rate_mask);
1742
1743 /* mask with station rate restriction */
1744 if (is_legacy(tbl->lq_type)) {
Johannes Berg8318d782008-01-24 19:38:38 +01001745 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Ben Cahill77626352007-11-29 11:09:44 +08001746 /* supp_rates has no CCK bits in A mode */
Zhu Yib481de92007-09-25 17:54:57 -07001747 rate_scale_index_msk = (u16) (rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -08001748 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
Zhu Yib481de92007-09-25 17:54:57 -07001749 else
1750 rate_scale_index_msk = (u16) (rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -08001751 lq_sta->supp_rates);
Zhu Yib481de92007-09-25 17:54:57 -07001752
1753 } else
1754 rate_scale_index_msk = rate_mask;
1755
1756 if (!rate_scale_index_msk)
1757 rate_scale_index_msk = rate_mask;
1758
Guy Cohen07bc28e2008-04-23 17:15:03 -07001759 if (!((1 << index) & rate_scale_index_msk)) {
1760 IWL_ERROR("Current Rate is not valid\n");
1761 return;
Zhu Yib481de92007-09-25 17:54:57 -07001762 }
1763
Ben Cahill77626352007-11-29 11:09:44 +08001764 /* Get expected throughput table and history window for current rate */
Guy Cohen07bc28e2008-04-23 17:15:03 -07001765 if (!tbl->expected_tpt) {
1766 IWL_ERROR("tbl->expected_tpt is NULL\n");
1767 return;
1768 }
Zhu Yib481de92007-09-25 17:54:57 -07001769
1770 window = &(tbl->win[index]);
1771
Ben Cahill77626352007-11-29 11:09:44 +08001772 /*
1773 * If there is not enough history to calculate actual average
1774 * throughput, keep analyzing results of more tx frames, without
1775 * changing rate or mode (bypass most of the rest of this function).
1776 * Set up new rate table in uCode only if old rate is not supported
1777 * in current association (use new rate found above).
1778 */
Zhu Yib481de92007-09-25 17:54:57 -07001779 fail_count = window->counter - window->success_counter;
Guy Cohen07bc28e2008-04-23 17:15:03 -07001780 if ((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
1781 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH)) {
1782 IWL_DEBUG_RATE("LQ: still below TH. succ=%d total=%d "
Zhu Yib481de92007-09-25 17:54:57 -07001783 "for index %d\n",
1784 window->success_counter, window->counter, index);
Ben Cahill77626352007-11-29 11:09:44 +08001785
1786 /* Can't calculate this yet; not enough history */
Zhu Yib481de92007-09-25 17:54:57 -07001787 window->average_tpt = IWL_INVALID_VALUE;
Ben Cahill77626352007-11-29 11:09:44 +08001788
1789 /* Should we stay with this modulation mode,
1790 * or search for a new one? */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001791 rs_stay_in_table(lq_sta);
Ben Cahill77626352007-11-29 11:09:44 +08001792
Zhu Yib481de92007-09-25 17:54:57 -07001793 goto out;
1794
Ben Cahill77626352007-11-29 11:09:44 +08001795 /* Else we have enough samples; calculate estimate of
1796 * actual average throughput */
Guy Cohen07bc28e2008-04-23 17:15:03 -07001797 } else {
1798 /*FIXME:RS remove this else if we don't get this error*/
1799 if (window->average_tpt != ((window->success_ratio *
1800 tbl->expected_tpt[index] + 64) / 128)) {
1801 IWL_ERROR("expected_tpt should have been calculated"
1802 " by now\n");
1803 window->average_tpt = ((window->success_ratio *
Zhu Yib481de92007-09-25 17:54:57 -07001804 tbl->expected_tpt[index] + 64) / 128);
Guy Cohen07bc28e2008-04-23 17:15:03 -07001805 }
1806 }
Zhu Yib481de92007-09-25 17:54:57 -07001807
Ben Cahill77626352007-11-29 11:09:44 +08001808 /* If we are searching for better modulation mode, check success. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001809 if (lq_sta->search_better_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07001810
Ben Cahill77626352007-11-29 11:09:44 +08001811 /* If good success, continue using the "search" mode;
1812 * no need to send new link quality command, since we're
1813 * continuing to use the setup that we've been trying. */
Guy Cohen07bc28e2008-04-23 17:15:03 -07001814 if (window->average_tpt > lq_sta->last_tpt) {
1815
1816 IWL_DEBUG_RATE("LQ: SWITCHING TO CURRENT TABLE "
1817 "suc=%d cur-tpt=%d old-tpt=%d\n",
1818 window->success_ratio,
1819 window->average_tpt,
1820 lq_sta->last_tpt);
1821
1822 if (!is_legacy(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08001823 lq_sta->enable_counter = 1;
Guy Cohen07bc28e2008-04-23 17:15:03 -07001824
Ben Cahill77626352007-11-29 11:09:44 +08001825 /* Swap tables; "search" becomes "active" */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001826 lq_sta->active_tbl = active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001827 current_tpt = window->average_tpt;
Ben Cahill77626352007-11-29 11:09:44 +08001828
1829 /* Else poor success; go back to mode in "active" table */
Zhu Yib481de92007-09-25 17:54:57 -07001830 } else {
Guy Cohen07bc28e2008-04-23 17:15:03 -07001831
1832 IWL_DEBUG_RATE("LQ: GOING BACK TO THE OLD TABLE "
1833 "suc=%d cur-tpt=%d old-tpt=%d\n",
1834 window->success_ratio,
1835 window->average_tpt,
1836 lq_sta->last_tpt);
1837
Ben Cahill77626352007-11-29 11:09:44 +08001838 /* Nullify "search" table */
Zhu Yib481de92007-09-25 17:54:57 -07001839 tbl->lq_type = LQ_NONE;
Ben Cahill77626352007-11-29 11:09:44 +08001840
1841 /* Revert to "active" table */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001842 active_tbl = lq_sta->active_tbl;
1843 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07001844
Ben Cahill77626352007-11-29 11:09:44 +08001845 /* Revert to "active" rate and throughput info */
Tomas Winkler17744ff2008-03-02 01:52:00 +02001846 index = iwl4965_hwrate_to_plcp_idx(
Guy Cohen39e88502008-04-23 17:14:57 -07001847 tbl->current_rate);
Tomas Winklerc33104f2008-01-14 17:46:21 -08001848 current_tpt = lq_sta->last_tpt;
Ben Cahill77626352007-11-29 11:09:44 +08001849
1850 /* Need to set up a new rate table in uCode */
1851 update_lq = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001852 }
Ben Cahill77626352007-11-29 11:09:44 +08001853
1854 /* Either way, we've made a decision; modulation mode
1855 * search is done, allow rate adjustment next time. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001856 lq_sta->search_better_tbl = 0;
Ben Cahill77626352007-11-29 11:09:44 +08001857 done_search = 1; /* Don't switch modes below! */
Zhu Yib481de92007-09-25 17:54:57 -07001858 goto lq_update;
1859 }
1860
Ben Cahill77626352007-11-29 11:09:44 +08001861 /* (Else) not in search of better modulation mode, try for better
1862 * starting rate, while staying in this mode. */
Ester Kummerbf403db2008-05-05 10:22:40 +08001863 high_low = rs_get_adjacent_rate(priv, index, rate_scale_index_msk,
Zhu Yib481de92007-09-25 17:54:57 -07001864 tbl->lq_type);
1865 low = high_low & 0xff;
1866 high = (high_low >> 8) & 0xff;
1867
Guy Cohena5e8b502008-05-29 16:35:11 +08001868 sr = window->success_ratio;
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
Guy Cohena5e8b502008-05-29 16:35:11 +08001877 scale_action = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001878
Ben Cahill77626352007-11-29 11:09:44 +08001879 /* Too many failures, decrease rate */
Guy Cohena5e8b502008-05-29 16:35:11 +08001880 if ((sr <= IWL_RATE_DECREASE_TH) || (current_tpt == 0)) {
Zhu Yib481de92007-09-25 17:54:57 -07001881 IWL_DEBUG_RATE("decrease rate because of low success_ratio\n");
1882 scale_action = -1;
Ben Cahill77626352007-11-29 11:09:44 +08001883
1884 /* No throughput measured yet for adjacent rates; try increase. */
Zhu Yib481de92007-09-25 17:54:57 -07001885 } else if ((low_tpt == IWL_INVALID_VALUE) &&
Guy Cohena5e8b502008-05-29 16:35:11 +08001886 (high_tpt == IWL_INVALID_VALUE)) {
1887
1888 if (high != IWL_RATE_INVALID && sr >= IWL_RATE_INCREASE_TH)
1889 scale_action = 1;
1890 else if (low != IWL_RATE_INVALID)
1891 scale_action = -1;
1892 }
Ben Cahill77626352007-11-29 11:09:44 +08001893
1894 /* Both adjacent throughputs are measured, but neither one has better
1895 * throughput; we're using the best rate, don't change it! */
Zhu Yib481de92007-09-25 17:54:57 -07001896 else if ((low_tpt != IWL_INVALID_VALUE) &&
1897 (high_tpt != IWL_INVALID_VALUE) &&
1898 (low_tpt < current_tpt) &&
1899 (high_tpt < current_tpt))
1900 scale_action = 0;
Ben Cahill77626352007-11-29 11:09:44 +08001901
1902 /* At least one adjacent rate's throughput is measured,
1903 * and may have better performance. */
Zhu Yib481de92007-09-25 17:54:57 -07001904 else {
Ben Cahill77626352007-11-29 11:09:44 +08001905 /* Higher adjacent rate's throughput is measured */
Zhu Yib481de92007-09-25 17:54:57 -07001906 if (high_tpt != IWL_INVALID_VALUE) {
Ben Cahill77626352007-11-29 11:09:44 +08001907 /* Higher rate has better throughput */
Guy Cohena5e8b502008-05-29 16:35:11 +08001908 if (high_tpt > current_tpt &&
1909 sr >= IWL_RATE_INCREASE_TH) {
Zhu Yib481de92007-09-25 17:54:57 -07001910 scale_action = 1;
Guy Cohena5e8b502008-05-29 16:35:11 +08001911 } else {
Zhu Yib481de92007-09-25 17:54:57 -07001912 IWL_DEBUG_RATE
1913 ("decrease rate because of high tpt\n");
1914 scale_action = -1;
1915 }
Ben Cahill77626352007-11-29 11:09:44 +08001916
1917 /* Lower adjacent rate's throughput is measured */
Zhu Yib481de92007-09-25 17:54:57 -07001918 } else if (low_tpt != IWL_INVALID_VALUE) {
Ben Cahill77626352007-11-29 11:09:44 +08001919 /* Lower rate has better throughput */
Zhu Yib481de92007-09-25 17:54:57 -07001920 if (low_tpt > current_tpt) {
1921 IWL_DEBUG_RATE
1922 ("decrease rate because of low tpt\n");
1923 scale_action = -1;
Guy Cohena5e8b502008-05-29 16:35:11 +08001924 } else if (sr >= IWL_RATE_INCREASE_TH) {
Zhu Yib481de92007-09-25 17:54:57 -07001925 scale_action = 1;
Guy Cohena5e8b502008-05-29 16:35:11 +08001926 }
Zhu Yib481de92007-09-25 17:54:57 -07001927 }
1928 }
1929
Ben Cahill77626352007-11-29 11:09:44 +08001930 /* Sanity check; asked for decrease, but success rate or throughput
1931 * has been good at old rate. Don't change it. */
Guy Cohena5e8b502008-05-29 16:35:11 +08001932 if ((scale_action == -1) && (low != IWL_RATE_INVALID) &&
1933 ((sr > IWL_RATE_HIGH_TH) ||
Zhu Yib481de92007-09-25 17:54:57 -07001934 (current_tpt > (100 * tbl->expected_tpt[low]))))
Zhu Yib481de92007-09-25 17:54:57 -07001935 scale_action = 0;
1936
1937 switch (scale_action) {
1938 case -1:
Ben Cahill77626352007-11-29 11:09:44 +08001939 /* Decrease starting rate, update uCode's rate table */
Zhu Yib481de92007-09-25 17:54:57 -07001940 if (low != IWL_RATE_INVALID) {
1941 update_lq = 1;
1942 index = low;
1943 }
1944 break;
1945 case 1:
Ben Cahill77626352007-11-29 11:09:44 +08001946 /* Increase starting rate, update uCode's rate table */
Zhu Yib481de92007-09-25 17:54:57 -07001947 if (high != IWL_RATE_INVALID) {
1948 update_lq = 1;
1949 index = high;
1950 }
1951
1952 break;
1953 case 0:
Ben Cahill77626352007-11-29 11:09:44 +08001954 /* No change */
Zhu Yib481de92007-09-25 17:54:57 -07001955 default:
1956 break;
1957 }
1958
Guy Coheneecd6e52008-04-23 17:14:58 -07001959 IWL_DEBUG_RATE("choose rate scale index %d action %d low %d "
Zhu Yib481de92007-09-25 17:54:57 -07001960 "high %d type %d\n",
1961 index, scale_action, low, high, tbl->lq_type);
1962
Guy Cohena5e8b502008-05-29 16:35:11 +08001963lq_update:
Ben Cahill77626352007-11-29 11:09:44 +08001964 /* Replace uCode's rate table for the destination station. */
Zhu Yib481de92007-09-25 17:54:57 -07001965 if (update_lq) {
Guy Cohen39e88502008-04-23 17:14:57 -07001966 rate = rate_n_flags_from_tbl(tbl, index, is_green);
Guy Cohen07bc28e2008-04-23 17:15:03 -07001967 rs_fill_link_cmd(priv, lq_sta, rate);
Tomas Winkler66c73db2008-04-15 16:01:40 -07001968 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07001969 }
Ben Cahill77626352007-11-29 11:09:44 +08001970
1971 /* Should we stay with this modulation mode, or search for a new one? */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001972 rs_stay_in_table(lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07001973
Ben Cahill77626352007-11-29 11:09:44 +08001974 /*
1975 * Search for new modulation mode if we're:
1976 * 1) Not changing rates right now
1977 * 2) Not just finishing up a search
1978 * 3) Allowing a new search
1979 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001980 if (!update_lq && !done_search && !lq_sta->stay_in_tbl) {
Ben Cahill77626352007-11-29 11:09:44 +08001981 /* Save current throughput to compare with "search" throughput*/
Tomas Winklerc33104f2008-01-14 17:46:21 -08001982 lq_sta->last_tpt = current_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07001983
Ben Cahill77626352007-11-29 11:09:44 +08001984 /* Select a new "search" modulation mode to try.
1985 * If one is found, set up the new "search" table. */
Zhu Yib481de92007-09-25 17:54:57 -07001986 if (is_legacy(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08001987 rs_move_legacy_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07001988 else if (is_siso(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08001989 rs_move_siso_to_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07001990 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08001991 rs_move_mimo_to_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07001992
Ben Cahill77626352007-11-29 11:09:44 +08001993 /* If new "search" mode was selected, set up in uCode table */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001994 if (lq_sta->search_better_tbl) {
Ben Cahill77626352007-11-29 11:09:44 +08001995 /* Access the "search" table, clear its history. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001996 tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Zhu Yib481de92007-09-25 17:54:57 -07001997 for (i = 0; i < IWL_RATE_COUNT; i++)
1998 rs_rate_scale_clear_window(&(tbl->win[i]));
1999
Ben Cahill77626352007-11-29 11:09:44 +08002000 /* Use new "search" start rate */
Tomas Winkler17744ff2008-03-02 01:52:00 +02002001 index = iwl4965_hwrate_to_plcp_idx(
Guy Cohen39e88502008-04-23 17:14:57 -07002002 tbl->current_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002003
Guy Coheneecd6e52008-04-23 17:14:58 -07002004 IWL_DEBUG_RATE("Switch current mcs: %X index: %d\n",
Guy Cohen39e88502008-04-23 17:14:57 -07002005 tbl->current_rate, index);
Guy Cohen07bc28e2008-04-23 17:15:03 -07002006 rs_fill_link_cmd(priv, lq_sta, tbl->current_rate);
Tomas Winkler66c73db2008-04-15 16:01:40 -07002007 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07002008 }
Zhu Yib481de92007-09-25 17:54:57 -07002009
Ben Cahill77626352007-11-29 11:09:44 +08002010 /* If the "active" (non-search) mode was legacy,
2011 * and we've tried switching antennas,
2012 * but we haven't been able to try HT modes (not available),
2013 * stay with best antenna legacy modulation for a while
2014 * before next round of mode comparisons. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002015 tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07002016 if (is_legacy(tbl1->lq_type) &&
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002017#ifdef CONFIG_IWL4965_HT
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002018 (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE)) &&
Zhu Yib481de92007-09-25 17:54:57 -07002019#endif
Tomas Winklerc33104f2008-01-14 17:46:21 -08002020 (lq_sta->action_counter >= 1)) {
2021 lq_sta->action_counter = 0;
Guy Coheneecd6e52008-04-23 17:14:58 -07002022 IWL_DEBUG_RATE("LQ: STAY in legacy table\n");
Ester Kummerbf403db2008-05-05 10:22:40 +08002023 rs_set_stay_in_table(priv, 1, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002024 }
2025
Ben Cahill77626352007-11-29 11:09:44 +08002026 /* If we're in an HT mode, and all 3 mode switch actions
2027 * have been tried and compared, stay in this best modulation
2028 * mode for a while before next round of mode comparisons. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002029 if (lq_sta->enable_counter &&
2030 (lq_sta->action_counter >= IWL_ACTION_LIMIT)) {
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002031#ifdef CONFIG_IWL4965_HT
2032 if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
2033 (lq_sta->tx_agg_tid_en & (1 << tid)) &&
2034 (tid != MAX_TID_COUNT)) {
Guy Coheneecd6e52008-04-23 17:14:58 -07002035 IWL_DEBUG_RATE("try to aggregate tid %d\n", tid);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002036 rs_tl_turn_on_agg(priv, tid, lq_sta, sta);
Zhu Yib481de92007-09-25 17:54:57 -07002037 }
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002038#endif /*CONFIG_IWL4965_HT */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002039 lq_sta->action_counter = 0;
Ester Kummerbf403db2008-05-05 10:22:40 +08002040 rs_set_stay_in_table(priv, 0, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002041 }
Ben Cahill77626352007-11-29 11:09:44 +08002042
2043 /*
2044 * Else, don't search for a new modulation mode.
2045 * Put new timestamp in stay-in-modulation-mode flush timer if:
2046 * 1) Not changing rates right now
2047 * 2) Not just finishing up a search
2048 * 3) flush timer is empty
2049 */
Zhu Yib481de92007-09-25 17:54:57 -07002050 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002051 if ((!update_lq) && (!done_search) && (!lq_sta->flush_timer))
2052 lq_sta->flush_timer = jiffies;
Zhu Yib481de92007-09-25 17:54:57 -07002053 }
2054
2055out:
Guy Cohen39e88502008-04-23 17:14:57 -07002056 tbl->current_rate = rate_n_flags_from_tbl(tbl, index, is_green);
Zhu Yib481de92007-09-25 17:54:57 -07002057 i = index;
Johannes Berg8318d782008-01-24 19:38:38 +01002058 sta->last_txrate_idx = i;
Zhu Yib481de92007-09-25 17:54:57 -07002059
Johannes Berg8318d782008-01-24 19:38:38 +01002060 /* sta->txrate_idx is an index to A mode rates which start
Zhu Yib481de92007-09-25 17:54:57 -07002061 * at IWL_FIRST_OFDM_RATE
2062 */
Johannes Berg8318d782008-01-24 19:38:38 +01002063 if (lq_sta->band == IEEE80211_BAND_5GHZ)
2064 sta->txrate_idx = i - IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -07002065 else
Johannes Berg8318d782008-01-24 19:38:38 +01002066 sta->txrate_idx = i;
Zhu Yib481de92007-09-25 17:54:57 -07002067
2068 return;
2069}
2070
2071
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002072static void rs_initialize_lq(struct iwl_priv *priv,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002073 struct ieee80211_conf *conf,
Zhu Yib481de92007-09-25 17:54:57 -07002074 struct sta_info *sta)
2075{
Tomas Winklerc33104f2008-01-14 17:46:21 -08002076 struct iwl4965_lq_sta *lq_sta;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002077 struct iwl4965_scale_tbl_info *tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002078 int rate_idx;
Guy Cohenaade00c2008-04-23 17:14:59 -07002079 int i;
Guy Cohen39e88502008-04-23 17:14:57 -07002080 u32 rate;
Guy Cohenaade00c2008-04-23 17:14:59 -07002081 u8 use_green = rs_use_green(priv, conf);
2082 u8 active_tbl = 0;
2083 u8 valid_tx_ant;
Zhu Yib481de92007-09-25 17:54:57 -07002084
2085 if (!sta || !sta->rate_ctrl_priv)
2086 goto out;
2087
Tomas Winklerc33104f2008-01-14 17:46:21 -08002088 lq_sta = (struct iwl4965_lq_sta *)sta->rate_ctrl_priv;
Johannes Berg8318d782008-01-24 19:38:38 +01002089 i = sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002090
Tomas Winklerc33104f2008-01-14 17:46:21 -08002091 if ((lq_sta->lq.sta_id == 0xff) &&
Zhu Yib481de92007-09-25 17:54:57 -07002092 (priv->iw_mode == IEEE80211_IF_TYPE_IBSS))
2093 goto out;
2094
Guy Cohenaade00c2008-04-23 17:14:59 -07002095 valid_tx_ant = priv->hw_params.valid_tx_ant;
2096
Tomas Winklerc33104f2008-01-14 17:46:21 -08002097 if (!lq_sta->search_better_tbl)
2098 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002099 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002100 active_tbl = 1 - lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002101
Tomas Winklerc33104f2008-01-14 17:46:21 -08002102 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07002103
2104 if ((i < 0) || (i >= IWL_RATE_COUNT))
2105 i = 0;
2106
Guy Cohenfde0db32008-04-21 15:42:01 -07002107 /* FIXME:RS: This is also wrong in 4965 */
Tomas Winkler1826dcc2008-05-15 13:54:02 +08002108 rate = iwl_rates[i].plcp;
Guy Cohen39e88502008-04-23 17:14:57 -07002109 rate |= RATE_MCS_ANT_B_MSK;
2110 rate &= ~RATE_MCS_ANT_A_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002111
2112 if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
Guy Cohen39e88502008-04-23 17:14:57 -07002113 rate |= RATE_MCS_CCK_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002114
Guy Cohenfde0db32008-04-21 15:42:01 -07002115 tbl->ant_type = ANT_B;
Guy Cohen39e88502008-04-23 17:14:57 -07002116 rs_get_tbl_info_from_mcs(rate, priv->band, tbl, &rate_idx);
Guy Cohenaade00c2008-04-23 17:14:59 -07002117 if (!rs_is_valid_ant(valid_tx_ant, tbl->ant_type))
2118 rs_toggle_antenna(valid_tx_ant, &rate, tbl);
Zhu Yib481de92007-09-25 17:54:57 -07002119
Guy Cohen39e88502008-04-23 17:14:57 -07002120 rate = rate_n_flags_from_tbl(tbl, rate_idx, use_green);
2121 tbl->current_rate = rate;
Guy Coheneecd6e52008-04-23 17:14:58 -07002122 rs_set_expected_tpt_table(lq_sta, tbl);
Guy Cohen07bc28e2008-04-23 17:15:03 -07002123 rs_fill_link_cmd(NULL, lq_sta, rate);
Tomas Winkler66c73db2008-04-15 16:01:40 -07002124 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07002125 out:
2126 return;
2127}
2128
Mattias Nissler1abbe492007-12-20 13:50:07 +01002129static void rs_get_rate(void *priv_rate, struct net_device *dev,
Johannes Berg8318d782008-01-24 19:38:38 +01002130 struct ieee80211_supported_band *sband,
2131 struct sk_buff *skb,
Mattias Nissler1abbe492007-12-20 13:50:07 +01002132 struct rate_selection *sel)
Zhu Yib481de92007-09-25 17:54:57 -07002133{
2134
2135 int i;
2136 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002137 struct ieee80211_conf *conf = &local->hw.conf;
Zhu Yib481de92007-09-25 17:54:57 -07002138 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2139 struct sta_info *sta;
Michael Wu2bc454b2007-12-25 19:33:16 -05002140 u16 fc;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002141 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002142 struct iwl4965_lq_sta *lq_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002143
Zhu Yi58826352007-09-27 11:27:39 +08002144 IWL_DEBUG_RATE_LIMIT("rate scale calculate new rate for skb\n");
Zhu Yib481de92007-09-25 17:54:57 -07002145
Johannes Bergd0709a62008-02-25 16:27:46 +01002146 rcu_read_lock();
2147
Zhu Yib481de92007-09-25 17:54:57 -07002148 sta = sta_info_get(local, hdr->addr1);
2149
Michael Wu2bc454b2007-12-25 19:33:16 -05002150 /* Send management frames and broadcast/multicast data using lowest
2151 * rate. */
2152 fc = le16_to_cpu(hdr->frame_control);
2153 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1) ||
2154 !sta || !sta->rate_ctrl_priv) {
Johannes Berg2e92e6f2008-05-15 12:55:27 +02002155 sel->rate_idx = rate_lowest_index(local, sband, sta);
Tomas Winklerf4d60822008-03-05 11:31:00 -08002156 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07002157 }
2158
Tomas Winklerc33104f2008-01-14 17:46:21 -08002159 lq_sta = (struct iwl4965_lq_sta *)sta->rate_ctrl_priv;
Johannes Berg8318d782008-01-24 19:38:38 +01002160 i = sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002161
Tomas Winklerc33104f2008-01-14 17:46:21 -08002162 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
2163 !lq_sta->ibss_sta_added) {
Tomas Winkler947b13a2008-04-16 16:34:48 -07002164 u8 sta_id = iwl_find_station(priv, hdr->addr1);
Joe Perches0795af52007-10-03 17:59:30 -07002165 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07002166
2167 if (sta_id == IWL_INVALID_STATION) {
Joe Perches0795af52007-10-03 17:59:30 -07002168 IWL_DEBUG_RATE("LQ: ADD station %s\n",
2169 print_mac(mac, hdr->addr1));
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08002170 sta_id = iwl_add_station_flags(priv, hdr->addr1,
Ron Rindjunsky67d62032007-11-26 16:14:40 +02002171 0, CMD_ASYNC, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07002172 }
2173 if ((sta_id != IWL_INVALID_STATION)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002174 lq_sta->lq.sta_id = sta_id;
2175 lq_sta->lq.rs_table[0].rate_n_flags = 0;
2176 lq_sta->ibss_sta_added = 1;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002177 rs_initialize_lq(priv, conf, sta);
Zhu Yib481de92007-09-25 17:54:57 -07002178 }
Tomas Winklerc33104f2008-01-14 17:46:21 -08002179 if (!lq_sta->ibss_sta_added)
Zhu Yib481de92007-09-25 17:54:57 -07002180 goto done;
2181 }
2182
Tomas Winklerf4d60822008-03-05 11:31:00 -08002183done:
Mattias Nissler1abbe492007-12-20 13:50:07 +01002184 if ((i < 0) || (i > IWL_RATE_COUNT)) {
Johannes Berg2e92e6f2008-05-15 12:55:27 +02002185 sel->rate_idx = rate_lowest_index(local, sband, sta);
Tomas Winklerf4d60822008-03-05 11:31:00 -08002186 goto out;
Mattias Nissler1abbe492007-12-20 13:50:07 +01002187 }
Zhu Yib481de92007-09-25 17:54:57 -07002188
Johannes Berg2e92e6f2008-05-15 12:55:27 +02002189 if (sband->band == IEEE80211_BAND_5GHZ)
2190 i -= IWL_FIRST_OFDM_RATE;
2191 sel->rate_idx = i;
Tomas Winklerf4d60822008-03-05 11:31:00 -08002192out:
2193 rcu_read_unlock();
Zhu Yib481de92007-09-25 17:54:57 -07002194}
2195
Ester Kummerbf403db2008-05-05 10:22:40 +08002196static void *rs_alloc_sta(void *priv_rate, gfp_t gfp)
Zhu Yib481de92007-09-25 17:54:57 -07002197{
Tomas Winklerc33104f2008-01-14 17:46:21 -08002198 struct iwl4965_lq_sta *lq_sta;
Ester Kummerbf403db2008-05-05 10:22:40 +08002199 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07002200 int i, j;
2201
Ester Kummerbf403db2008-05-05 10:22:40 +08002202 priv = (struct iwl_priv *)priv_rate;
Zhu Yib481de92007-09-25 17:54:57 -07002203 IWL_DEBUG_RATE("create station rate scale window\n");
2204
Tomas Winklerc33104f2008-01-14 17:46:21 -08002205 lq_sta = kzalloc(sizeof(struct iwl4965_lq_sta), gfp);
Zhu Yib481de92007-09-25 17:54:57 -07002206
Tomas Winklerc33104f2008-01-14 17:46:21 -08002207 if (lq_sta == NULL)
Zhu Yib481de92007-09-25 17:54:57 -07002208 return NULL;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002209 lq_sta->lq.sta_id = 0xff;
Zhu Yib481de92007-09-25 17:54:57 -07002210
Zhu Yi63fddb92007-09-27 11:27:36 +08002211
Zhu Yib481de92007-09-25 17:54:57 -07002212 for (j = 0; j < LQ_SIZE; j++)
2213 for (i = 0; i < IWL_RATE_COUNT; i++)
Tomas Winklerc33104f2008-01-14 17:46:21 -08002214 rs_rate_scale_clear_window(&(lq_sta->lq_info[j].win[i]));
Zhu Yib481de92007-09-25 17:54:57 -07002215
Tomas Winklerc33104f2008-01-14 17:46:21 -08002216 return lq_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002217}
2218
2219static void rs_rate_init(void *priv_rate, void *priv_sta,
2220 struct ieee80211_local *local,
2221 struct sta_info *sta)
2222{
2223 int i, j;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002224 struct ieee80211_conf *conf = &local->hw.conf;
Johannes Berg8318d782008-01-24 19:38:38 +01002225 struct ieee80211_supported_band *sband;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002226 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002227 struct iwl4965_lq_sta *lq_sta = priv_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002228
Johannes Berg8318d782008-01-24 19:38:38 +01002229 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2230
Tomas Winklerc33104f2008-01-14 17:46:21 -08002231 lq_sta->flush_timer = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01002232 lq_sta->supp_rates = sta->supp_rates[sband->band];
2233 sta->txrate_idx = 3;
Zhu Yib481de92007-09-25 17:54:57 -07002234 for (j = 0; j < LQ_SIZE; j++)
2235 for (i = 0; i < IWL_RATE_COUNT; i++)
Tomas Winklerc33104f2008-01-14 17:46:21 -08002236 rs_rate_scale_clear_window(&(lq_sta->lq_info[j].win[i]));
Zhu Yib481de92007-09-25 17:54:57 -07002237
Guy Cohen07bc28e2008-04-23 17:15:03 -07002238 IWL_DEBUG_RATE("LQ: *** rate scale global init ***\n");
Zhu Yib481de92007-09-25 17:54:57 -07002239 /* TODO: what is a good starting rate for STA? About middle? Maybe not
2240 * the lowest or the highest rate.. Could consider using RSSI from
2241 * previous packets? Need to have IEEE 802.1X auth succeed immediately
2242 * after assoc.. */
2243
Tomas Winklerc33104f2008-01-14 17:46:21 -08002244 lq_sta->ibss_sta_added = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002245 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
Tomas Winkler947b13a2008-04-16 16:34:48 -07002246 u8 sta_id = iwl_find_station(priv, sta->addr);
Joe Perches0795af52007-10-03 17:59:30 -07002247 DECLARE_MAC_BUF(mac);
2248
Zhu Yib481de92007-09-25 17:54:57 -07002249 /* for IBSS the call are from tasklet */
Guy Coheneecd6e52008-04-23 17:14:58 -07002250 IWL_DEBUG_RATE("LQ: ADD station %s\n",
Joe Perches0795af52007-10-03 17:59:30 -07002251 print_mac(mac, sta->addr));
Zhu Yib481de92007-09-25 17:54:57 -07002252
2253 if (sta_id == IWL_INVALID_STATION) {
Joe Perches0795af52007-10-03 17:59:30 -07002254 IWL_DEBUG_RATE("LQ: ADD station %s\n",
2255 print_mac(mac, sta->addr));
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08002256 sta_id = iwl_add_station_flags(priv, sta->addr,
Ron Rindjunsky67d62032007-11-26 16:14:40 +02002257 0, CMD_ASYNC, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07002258 }
2259 if ((sta_id != IWL_INVALID_STATION)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002260 lq_sta->lq.sta_id = sta_id;
2261 lq_sta->lq.rs_table[0].rate_n_flags = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002262 }
2263 /* FIXME: this is w/a remove it later */
2264 priv->assoc_station_added = 1;
2265 }
2266
Ben Cahill77626352007-11-29 11:09:44 +08002267 /* Find highest tx rate supported by hardware and destination station */
Johannes Berg8318d782008-01-24 19:38:38 +01002268 for (i = 0; i < sband->n_bitrates; i++)
2269 if (sta->supp_rates[sband->band] & BIT(i))
2270 sta->txrate_idx = i;
2271
2272 sta->last_txrate_idx = sta->txrate_idx;
2273 /* WTF is with this bogus comment? A doesn't have cck rates */
Ben Cahill77626352007-11-29 11:09:44 +08002274 /* For MODE_IEEE80211A, cck rates are at end of rate table */
Johannes Berg8318d782008-01-24 19:38:38 +01002275 if (local->hw.conf.channel->band == IEEE80211_BAND_5GHZ)
2276 sta->last_txrate_idx += IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -07002277
Tomas Winklerc33104f2008-01-14 17:46:21 -08002278 lq_sta->is_dup = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002279 lq_sta->is_green = rs_use_green(priv, conf);
Guy Cohen39e88502008-04-23 17:14:57 -07002280 lq_sta->active_legacy_rate = priv->active_rate & ~(0x1000);
Tomas Winklerc33104f2008-01-14 17:46:21 -08002281 lq_sta->active_rate_basic = priv->active_rate_basic;
Johannes Berg8318d782008-01-24 19:38:38 +01002282 lq_sta->band = priv->band;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002283#ifdef CONFIG_IWL4965_HT
Ben Cahill77626352007-11-29 11:09:44 +08002284 /*
2285 * active_siso_rate mask includes 9 MBits (bit 5), and CCK (bits 0-3),
2286 * supp_rates[] does not; shift to convert format, force 9 MBits off.
2287 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002288 lq_sta->active_siso_rate =
Guy Cohenfde0db32008-04-21 15:42:01 -07002289 priv->current_ht_config.supp_mcs_set[0] << 1;
2290 lq_sta->active_siso_rate |=
2291 priv->current_ht_config.supp_mcs_set[0] & 0x1;
2292 lq_sta->active_siso_rate &= ~((u16)0x2);
2293 lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -07002294
Ben Cahill77626352007-11-29 11:09:44 +08002295 /* Same here */
Guy Cohenfde0db32008-04-21 15:42:01 -07002296 lq_sta->active_mimo2_rate =
2297 priv->current_ht_config.supp_mcs_set[1] << 1;
2298 lq_sta->active_mimo2_rate |=
2299 priv->current_ht_config.supp_mcs_set[1] & 0x1;
2300 lq_sta->active_mimo2_rate &= ~((u16)0x2);
2301 lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE;
2302
2303 lq_sta->active_mimo3_rate =
2304 priv->current_ht_config.supp_mcs_set[2] << 1;
2305 lq_sta->active_mimo3_rate |=
2306 priv->current_ht_config.supp_mcs_set[2] & 0x1;
2307 lq_sta->active_mimo3_rate &= ~((u16)0x2);
2308 lq_sta->active_mimo3_rate <<= IWL_FIRST_OFDM_RATE;
2309
Guy Cohen07bc28e2008-04-23 17:15:03 -07002310 IWL_DEBUG_RATE("SISO-RATE=%X MIMO2-RATE=%X MIMO3-RATE=%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002311 lq_sta->active_siso_rate,
Guy Cohenfde0db32008-04-21 15:42:01 -07002312 lq_sta->active_mimo2_rate,
2313 lq_sta->active_mimo3_rate);
2314
Guy Cohen07bc28e2008-04-23 17:15:03 -07002315 /* These values will be overriden later */
2316 lq_sta->lq.general_params.single_stream_ant_msk = ANT_A;
2317 lq_sta->lq.general_params.dual_stream_ant_msk = ANT_AB;
2318
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002319 /* as default allow aggregation for all tids */
2320 lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002321#endif /*CONFIG_IWL4965_HT*/
Zhu Yi98d7e092007-09-27 11:27:42 +08002322#ifdef CONFIG_MAC80211_DEBUGFS
Tomas Winklerc33104f2008-01-14 17:46:21 -08002323 lq_sta->drv = priv;
Zhu Yi98d7e092007-09-27 11:27:42 +08002324#endif
Zhu Yib481de92007-09-25 17:54:57 -07002325
2326 if (priv->assoc_station_added)
2327 priv->lq_mngr.lq_ready = 1;
2328
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002329 rs_initialize_lq(priv, conf, sta);
Zhu Yib481de92007-09-25 17:54:57 -07002330}
2331
Guy Cohenfde0db32008-04-21 15:42:01 -07002332static void rs_fill_link_cmd(const struct iwl_priv *priv,
2333 struct iwl4965_lq_sta *lq_sta,
Guy Cohen07bc28e2008-04-23 17:15:03 -07002334 u32 new_rate)
Zhu Yib481de92007-09-25 17:54:57 -07002335{
Guy Cohenaade00c2008-04-23 17:14:59 -07002336 struct iwl4965_scale_tbl_info tbl_type;
Zhu Yib481de92007-09-25 17:54:57 -07002337 int index = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002338 int rate_idx;
Zhu Yi1b696de2007-09-27 11:27:41 +08002339 int repeat_rate = 0;
Guy Cohenaade00c2008-04-23 17:14:59 -07002340 u8 ant_toggle_cnt = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002341 u8 use_ht_possible = 1;
Guy Cohenaade00c2008-04-23 17:14:59 -07002342 u8 valid_tx_ant = 0;
Guy Cohen07bc28e2008-04-23 17:15:03 -07002343 struct iwl_link_quality_cmd *lq_cmd = &lq_sta->lq;
Zhu Yib481de92007-09-25 17:54:57 -07002344
Ben Cahill77626352007-11-29 11:09:44 +08002345 /* Override starting rate (index 0) if needed for debug purposes */
Guy Cohen39e88502008-04-23 17:14:57 -07002346 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Zhu Yi98d7e092007-09-27 11:27:42 +08002347
Guy Cohen39e88502008-04-23 17:14:57 -07002348 /* Interpret new_rate (rate_n_flags) */
Guy Cohenaade00c2008-04-23 17:14:59 -07002349 memset(&tbl_type, 0, sizeof(tbl_type));
Guy Cohen39e88502008-04-23 17:14:57 -07002350 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band,
Zhu Yib481de92007-09-25 17:54:57 -07002351 &tbl_type, &rate_idx);
2352
Ben Cahill77626352007-11-29 11:09:44 +08002353 /* How many times should we repeat the initial rate? */
Zhu Yib481de92007-09-25 17:54:57 -07002354 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07002355 ant_toggle_cnt = 1;
Zhu Yi1b696de2007-09-27 11:27:41 +08002356 repeat_rate = IWL_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002357 } else {
Zhu Yi1b696de2007-09-27 11:27:41 +08002358 repeat_rate = IWL_HT_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002359 }
Zhu Yib481de92007-09-25 17:54:57 -07002360
2361 lq_cmd->general_params.mimo_delimiter =
2362 is_mimo(tbl_type.lq_type) ? 1 : 0;
Ben Cahill77626352007-11-29 11:09:44 +08002363
2364 /* Fill 1st table entry (index 0) */
Guy Cohen39e88502008-04-23 17:14:57 -07002365 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002366
Guy Cohen07bc28e2008-04-23 17:15:03 -07002367 if (num_of_ant(tbl_type.ant_type) == 1) {
2368 lq_cmd->general_params.single_stream_ant_msk =
2369 tbl_type.ant_type;
2370 } else if (num_of_ant(tbl_type.ant_type) == 2) {
2371 lq_cmd->general_params.dual_stream_ant_msk =
2372 tbl_type.ant_type;
2373 } /* otherwise we don't modify the existing value */
Zhu Yib481de92007-09-25 17:54:57 -07002374
2375 index++;
Zhu Yi1b696de2007-09-27 11:27:41 +08002376 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002377
Guy Cohenaade00c2008-04-23 17:14:59 -07002378 if (priv)
2379 valid_tx_ant = priv->hw_params.valid_tx_ant;
2380
Ben Cahill77626352007-11-29 11:09:44 +08002381 /* Fill rest of rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002382 while (index < LINK_QUAL_MAX_RETRY_NUM) {
Ben Cahill77626352007-11-29 11:09:44 +08002383 /* Repeat initial/next rate.
2384 * For legacy IWL_NUMBER_TRY == 1, this loop will not execute.
2385 * For HT IWL_HT_NUMBER_TRY == 3, this executes twice. */
Zhu Yi1b696de2007-09-27 11:27:41 +08002386 while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
Zhu Yib481de92007-09-25 17:54:57 -07002387 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07002388 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2389 ant_toggle_cnt++;
2390 else if (priv &&
2391 rs_toggle_antenna(valid_tx_ant,
2392 &new_rate, &tbl_type))
2393 ant_toggle_cnt = 1;
2394}
Zhu Yi98d7e092007-09-27 11:27:42 +08002395
Ben Cahill77626352007-11-29 11:09:44 +08002396 /* Override next rate if needed for debug purposes */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002397 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Ben Cahill77626352007-11-29 11:09:44 +08002398
2399 /* Fill next table entry */
Zhu Yib481de92007-09-25 17:54:57 -07002400 lq_cmd->rs_table[index].rate_n_flags =
Guy Cohen39e88502008-04-23 17:14:57 -07002401 cpu_to_le32(new_rate);
Zhu Yi1b696de2007-09-27 11:27:41 +08002402 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002403 index++;
2404 }
2405
Guy Cohen39e88502008-04-23 17:14:57 -07002406 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band, &tbl_type,
Zhu Yib481de92007-09-25 17:54:57 -07002407 &rate_idx);
2408
Ben Cahill77626352007-11-29 11:09:44 +08002409 /* Indicate to uCode which entries might be MIMO.
2410 * If initial rate was MIMO, this will finally end up
2411 * as (IWL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
Zhu Yib481de92007-09-25 17:54:57 -07002412 if (is_mimo(tbl_type.lq_type))
2413 lq_cmd->general_params.mimo_delimiter = index;
2414
Ben Cahill77626352007-11-29 11:09:44 +08002415 /* Get next rate */
Guy Cohen39e88502008-04-23 17:14:57 -07002416 new_rate = rs_get_lower_rate(lq_sta, &tbl_type, rate_idx,
2417 use_ht_possible);
Zhu Yib481de92007-09-25 17:54:57 -07002418
Ben Cahill77626352007-11-29 11:09:44 +08002419 /* How many times should we repeat the next rate? */
Zhu Yib481de92007-09-25 17:54:57 -07002420 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07002421 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2422 ant_toggle_cnt++;
2423 else if (priv &&
2424 rs_toggle_antenna(valid_tx_ant,
2425 &new_rate, &tbl_type))
2426 ant_toggle_cnt = 1;
2427
Zhu Yi1b696de2007-09-27 11:27:41 +08002428 repeat_rate = IWL_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002429 } else {
Zhu Yi1b696de2007-09-27 11:27:41 +08002430 repeat_rate = IWL_HT_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002431 }
Zhu Yib481de92007-09-25 17:54:57 -07002432
Ben Cahill77626352007-11-29 11:09:44 +08002433 /* Don't allow HT rates after next pass.
2434 * rs_get_lower_rate() will change type to LQ_A or LQ_G. */
Zhu Yib481de92007-09-25 17:54:57 -07002435 use_ht_possible = 0;
2436
Ben Cahill77626352007-11-29 11:09:44 +08002437 /* Override next rate if needed for debug purposes */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002438 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Ben Cahill77626352007-11-29 11:09:44 +08002439
2440 /* Fill next table entry */
Guy Cohen39e88502008-04-23 17:14:57 -07002441 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002442
2443 index++;
Zhu Yi1b696de2007-09-27 11:27:41 +08002444 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002445 }
2446
Tomas Winkler5e1dd8d2008-05-29 16:35:17 +08002447 lq_cmd->agg_params.agg_frame_cnt_limit = 64;
Zhu Yib481de92007-09-25 17:54:57 -07002448 lq_cmd->agg_params.agg_dis_start_th = 3;
2449 lq_cmd->agg_params.agg_time_limit = cpu_to_le16(4000);
Zhu Yib481de92007-09-25 17:54:57 -07002450}
2451
2452static void *rs_alloc(struct ieee80211_local *local)
2453{
2454 return local->hw.priv;
2455}
2456/* rate scale requires free function to be implemented */
2457static void rs_free(void *priv_rate)
2458{
2459 return;
2460}
2461
2462static void rs_clear(void *priv_rate)
2463{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002464 struct iwl_priv *priv = (struct iwl_priv *) priv_rate;
Zhu Yib481de92007-09-25 17:54:57 -07002465
2466 IWL_DEBUG_RATE("enter\n");
2467
2468 priv->lq_mngr.lq_ready = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002469
2470 IWL_DEBUG_RATE("leave\n");
2471}
2472
Ester Kummerbf403db2008-05-05 10:22:40 +08002473static void rs_free_sta(void *priv_rate, void *priv_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002474{
Tomas Winklerc33104f2008-01-14 17:46:21 -08002475 struct iwl4965_lq_sta *lq_sta = priv_sta;
Ester Kummerbf403db2008-05-05 10:22:40 +08002476 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07002477
Ester Kummerbf403db2008-05-05 10:22:40 +08002478 priv = (struct iwl_priv *)priv_rate;
Zhu Yib481de92007-09-25 17:54:57 -07002479 IWL_DEBUG_RATE("enter\n");
Tomas Winklerc33104f2008-01-14 17:46:21 -08002480 kfree(lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002481 IWL_DEBUG_RATE("leave\n");
2482}
2483
2484
Zhu Yi93dc6462007-09-27 11:27:37 +08002485#ifdef CONFIG_MAC80211_DEBUGFS
Zhu Yi5ae212c2007-09-27 11:27:38 +08002486static int open_file_generic(struct inode *inode, struct file *file)
2487{
2488 file->private_data = inode->i_private;
2489 return 0;
2490}
Tomas Winklerc33104f2008-01-14 17:46:21 -08002491static void rs_dbgfs_set_mcs(struct iwl4965_lq_sta *lq_sta,
Guy Cohen39e88502008-04-23 17:14:57 -07002492 u32 *rate_n_flags, int index)
Zhu Yi98d7e092007-09-27 11:27:42 +08002493{
Ester Kummerbf403db2008-05-05 10:22:40 +08002494 struct iwl_priv *priv;
2495
2496 priv = lq_sta->drv;
Guy Cohen39e88502008-04-23 17:14:57 -07002497 if (lq_sta->dbg_fixed_rate) {
2498 if (index < 12) {
2499 *rate_n_flags = lq_sta->dbg_fixed_rate;
2500 } else {
2501 if (lq_sta->band == IEEE80211_BAND_5GHZ)
2502 *rate_n_flags = 0x800D;
2503 else
2504 *rate_n_flags = 0x820A;
2505 }
Zhu Yi98d7e092007-09-27 11:27:42 +08002506 IWL_DEBUG_RATE("Fixed rate ON\n");
Guy Cohen39e88502008-04-23 17:14:57 -07002507 } else {
2508 IWL_DEBUG_RATE("Fixed rate OFF\n");
Zhu Yi98d7e092007-09-27 11:27:42 +08002509 }
Zhu Yi98d7e092007-09-27 11:27:42 +08002510}
2511
2512static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
2513 const char __user *user_buf, size_t count, loff_t *ppos)
2514{
Tomas Winklerc33104f2008-01-14 17:46:21 -08002515 struct iwl4965_lq_sta *lq_sta = file->private_data;
Ester Kummerbf403db2008-05-05 10:22:40 +08002516 struct iwl_priv *priv;
Zhu Yi98d7e092007-09-27 11:27:42 +08002517 char buf[64];
2518 int buf_size;
2519 u32 parsed_rate;
2520
Ester Kummerbf403db2008-05-05 10:22:40 +08002521 priv = lq_sta->drv;
Zhu Yi98d7e092007-09-27 11:27:42 +08002522 memset(buf, 0, sizeof(buf));
2523 buf_size = min(count, sizeof(buf) - 1);
2524 if (copy_from_user(buf, user_buf, buf_size))
2525 return -EFAULT;
2526
2527 if (sscanf(buf, "%x", &parsed_rate) == 1)
Guy Cohen39e88502008-04-23 17:14:57 -07002528 lq_sta->dbg_fixed_rate = parsed_rate;
Zhu Yi98d7e092007-09-27 11:27:42 +08002529 else
Guy Cohen39e88502008-04-23 17:14:57 -07002530 lq_sta->dbg_fixed_rate = 0;
Zhu Yi98d7e092007-09-27 11:27:42 +08002531
Guy Cohen39e88502008-04-23 17:14:57 -07002532 lq_sta->active_legacy_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
2533 lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2534 lq_sta->active_mimo2_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2535 lq_sta->active_mimo3_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
Zhu Yi98d7e092007-09-27 11:27:42 +08002536
2537 IWL_DEBUG_RATE("sta_id %d rate 0x%X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07002538 lq_sta->lq.sta_id, lq_sta->dbg_fixed_rate);
Zhu Yi98d7e092007-09-27 11:27:42 +08002539
Guy Cohen39e88502008-04-23 17:14:57 -07002540 if (lq_sta->dbg_fixed_rate) {
Guy Cohen07bc28e2008-04-23 17:15:03 -07002541 rs_fill_link_cmd(NULL, lq_sta, lq_sta->dbg_fixed_rate);
Tomas Winkler66c73db2008-04-15 16:01:40 -07002542 iwl_send_lq_cmd(lq_sta->drv, &lq_sta->lq, CMD_ASYNC);
Zhu Yi98d7e092007-09-27 11:27:42 +08002543 }
2544
2545 return count;
2546}
Zhu Yi0209dc12007-09-27 11:27:43 +08002547
Zhu Yi5ae212c2007-09-27 11:27:38 +08002548static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
2549 char __user *user_buf, size_t count, loff_t *ppos)
2550{
2551 char buff[1024];
2552 int desc = 0;
2553 int i = 0;
2554
Tomas Winklerc33104f2008-01-14 17:46:21 -08002555 struct iwl4965_lq_sta *lq_sta = file->private_data;
Zhu Yi5ae212c2007-09-27 11:27:38 +08002556
Tomas Winklerc33104f2008-01-14 17:46:21 -08002557 desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
Zhu Yi98d7e092007-09-27 11:27:42 +08002558 desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002559 lq_sta->total_failed, lq_sta->total_success,
Guy Cohen39e88502008-04-23 17:14:57 -07002560 lq_sta->active_legacy_rate);
Zhu Yi98d7e092007-09-27 11:27:42 +08002561 desc += sprintf(buff+desc, "fixed rate 0x%X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07002562 lq_sta->dbg_fixed_rate);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002563 desc += sprintf(buff+desc, "general:"
2564 "flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002565 lq_sta->lq.general_params.flags,
2566 lq_sta->lq.general_params.mimo_delimiter,
2567 lq_sta->lq.general_params.single_stream_ant_msk,
2568 lq_sta->lq.general_params.dual_stream_ant_msk);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002569
2570 desc += sprintf(buff+desc, "agg:"
2571 "time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002572 le16_to_cpu(lq_sta->lq.agg_params.agg_time_limit),
2573 lq_sta->lq.agg_params.agg_dis_start_th,
2574 lq_sta->lq.agg_params.agg_frame_cnt_limit);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002575
2576 desc += sprintf(buff+desc,
2577 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002578 lq_sta->lq.general_params.start_rate_index[0],
2579 lq_sta->lq.general_params.start_rate_index[1],
2580 lq_sta->lq.general_params.start_rate_index[2],
2581 lq_sta->lq.general_params.start_rate_index[3]);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002582
2583
2584 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
2585 desc += sprintf(buff+desc, " rate[%d] 0x%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002586 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags));
Zhu Yi5ae212c2007-09-27 11:27:38 +08002587
2588 return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2589}
2590
2591static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
Zhu Yi98d7e092007-09-27 11:27:42 +08002592 .write = rs_sta_dbgfs_scale_table_write,
Zhu Yi5ae212c2007-09-27 11:27:38 +08002593 .read = rs_sta_dbgfs_scale_table_read,
2594 .open = open_file_generic,
2595};
Zhu Yi0209dc12007-09-27 11:27:43 +08002596static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
2597 char __user *user_buf, size_t count, loff_t *ppos)
2598{
2599 char buff[1024];
2600 int desc = 0;
2601 int i, j;
2602
Tomas Winklerc33104f2008-01-14 17:46:21 -08002603 struct iwl4965_lq_sta *lq_sta = file->private_data;
Zhu Yi0209dc12007-09-27 11:27:43 +08002604 for (i = 0; i < LQ_SIZE; i++) {
2605 desc += sprintf(buff+desc, "%s type=%d SGI=%d FAT=%d DUP=%d\n"
2606 "rate=0x%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002607 lq_sta->active_tbl == i?"*":"x",
2608 lq_sta->lq_info[i].lq_type,
2609 lq_sta->lq_info[i].is_SGI,
2610 lq_sta->lq_info[i].is_fat,
2611 lq_sta->lq_info[i].is_dup,
Guy Cohen39e88502008-04-23 17:14:57 -07002612 lq_sta->lq_info[i].current_rate);
Zhu Yi0209dc12007-09-27 11:27:43 +08002613 for (j = 0; j < IWL_RATE_COUNT; j++) {
2614 desc += sprintf(buff+desc,
Tomas Winklerc33104f2008-01-14 17:46:21 -08002615 "counter=%d success=%d %%=%d\n",
2616 lq_sta->lq_info[i].win[j].counter,
2617 lq_sta->lq_info[i].win[j].success_counter,
2618 lq_sta->lq_info[i].win[j].success_ratio);
Zhu Yi0209dc12007-09-27 11:27:43 +08002619 }
2620 }
2621 return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2622}
2623
2624static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
2625 .read = rs_sta_dbgfs_stats_table_read,
2626 .open = open_file_generic,
2627};
Zhu Yi5ae212c2007-09-27 11:27:38 +08002628
Zhu Yi93dc6462007-09-27 11:27:37 +08002629static void rs_add_debugfs(void *priv, void *priv_sta,
2630 struct dentry *dir)
2631{
Tomas Winklerc33104f2008-01-14 17:46:21 -08002632 struct iwl4965_lq_sta *lq_sta = priv_sta;
2633 lq_sta->rs_sta_dbgfs_scale_table_file =
Zhu Yi0209dc12007-09-27 11:27:43 +08002634 debugfs_create_file("rate_scale_table", 0600, dir,
Tomas Winklerc33104f2008-01-14 17:46:21 -08002635 lq_sta, &rs_sta_dbgfs_scale_table_ops);
2636 lq_sta->rs_sta_dbgfs_stats_table_file =
Zhu Yi0209dc12007-09-27 11:27:43 +08002637 debugfs_create_file("rate_stats_table", 0600, dir,
Tomas Winklerc33104f2008-01-14 17:46:21 -08002638 lq_sta, &rs_sta_dbgfs_stats_table_ops);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002639#ifdef CONFIG_IWL4965_HT
2640 lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file =
2641 debugfs_create_u8("tx_agg_tid_enable", 0600, dir,
2642 &lq_sta->tx_agg_tid_en);
2643#endif
2644
Zhu Yi93dc6462007-09-27 11:27:37 +08002645}
2646
2647static void rs_remove_debugfs(void *priv, void *priv_sta)
2648{
Tomas Winklerc33104f2008-01-14 17:46:21 -08002649 struct iwl4965_lq_sta *lq_sta = priv_sta;
2650 debugfs_remove(lq_sta->rs_sta_dbgfs_scale_table_file);
2651 debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002652#ifdef CONFIG_IWL4965_HT
2653 debugfs_remove(lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file);
2654#endif
Zhu Yi93dc6462007-09-27 11:27:37 +08002655}
2656#endif
2657
Zhu Yib481de92007-09-25 17:54:57 -07002658static struct rate_control_ops rs_ops = {
2659 .module = NULL,
2660 .name = RS_NAME,
2661 .tx_status = rs_tx_status,
2662 .get_rate = rs_get_rate,
2663 .rate_init = rs_rate_init,
2664 .clear = rs_clear,
2665 .alloc = rs_alloc,
2666 .free = rs_free,
2667 .alloc_sta = rs_alloc_sta,
2668 .free_sta = rs_free_sta,
Zhu Yi93dc6462007-09-27 11:27:37 +08002669#ifdef CONFIG_MAC80211_DEBUGFS
2670 .add_sta_debugfs = rs_add_debugfs,
2671 .remove_sta_debugfs = rs_remove_debugfs,
2672#endif
Zhu Yib481de92007-09-25 17:54:57 -07002673};
2674
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002675int iwl4965_fill_rs_info(struct ieee80211_hw *hw, char *buf, u8 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07002676{
2677 struct ieee80211_local *local = hw_to_local(hw);
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002678 struct iwl_priv *priv = hw->priv;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002679 struct iwl4965_lq_sta *lq_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002680 struct sta_info *sta;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002681 int cnt = 0, i;
Zhu Yib481de92007-09-25 17:54:57 -07002682 u32 samples = 0, success = 0, good = 0;
2683 unsigned long now = jiffies;
2684 u32 max_time = 0;
2685 u8 lq_type, antenna;
2686
Johannes Bergd0709a62008-02-25 16:27:46 +01002687 rcu_read_lock();
2688
Zhu Yib481de92007-09-25 17:54:57 -07002689 sta = sta_info_get(local, priv->stations[sta_id].sta.sta.addr);
2690 if (!sta || !sta->rate_ctrl_priv) {
Johannes Bergd0709a62008-02-25 16:27:46 +01002691 if (sta)
Zhu Yib481de92007-09-25 17:54:57 -07002692 IWL_DEBUG_RATE("leave - no private rate data!\n");
Johannes Bergd0709a62008-02-25 16:27:46 +01002693 else
Zhu Yib481de92007-09-25 17:54:57 -07002694 IWL_DEBUG_RATE("leave - no station!\n");
Johannes Bergd0709a62008-02-25 16:27:46 +01002695 rcu_read_unlock();
Zhu Yib481de92007-09-25 17:54:57 -07002696 return sprintf(buf, "station %d not found\n", sta_id);
2697 }
2698
Tomas Winklerc33104f2008-01-14 17:46:21 -08002699 lq_sta = (void *)sta->rate_ctrl_priv;
Zhu Yib481de92007-09-25 17:54:57 -07002700
Tomas Winklerc33104f2008-01-14 17:46:21 -08002701 lq_type = lq_sta->lq_info[lq_sta->active_tbl].lq_type;
Guy Cohenfde0db32008-04-21 15:42:01 -07002702 antenna = lq_sta->lq_info[lq_sta->active_tbl].ant_type;
Zhu Yib481de92007-09-25 17:54:57 -07002703
2704 if (is_legacy(lq_type))
2705 i = IWL_RATE_54M_INDEX;
2706 else
2707 i = IWL_RATE_60M_INDEX;
2708 while (1) {
2709 u64 mask;
2710 int j;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002711 int active = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002712
Tomas Winklerc33104f2008-01-14 17:46:21 -08002713 cnt +=
Tomas Winkler1826dcc2008-05-15 13:54:02 +08002714 sprintf(&buf[cnt], " %2dMbs: ", iwl_rates[i].ieee / 2);
Zhu Yib481de92007-09-25 17:54:57 -07002715
2716 mask = (1ULL << (IWL_RATE_MAX_WINDOW - 1));
2717 for (j = 0; j < IWL_RATE_MAX_WINDOW; j++, mask >>= 1)
Tomas Winklerc33104f2008-01-14 17:46:21 -08002718 buf[cnt++] =
2719 (lq_sta->lq_info[active].win[i].data & mask)
Zhu Yib481de92007-09-25 17:54:57 -07002720 ? '1' : '0';
2721
Tomas Winklerc33104f2008-01-14 17:46:21 -08002722 samples += lq_sta->lq_info[active].win[i].counter;
2723 good += lq_sta->lq_info[active].win[i].success_counter;
2724 success += lq_sta->lq_info[active].win[i].success_counter *
Tomas Winkler1826dcc2008-05-15 13:54:02 +08002725 iwl_rates[i].ieee;
Zhu Yib481de92007-09-25 17:54:57 -07002726
Tomas Winklerc33104f2008-01-14 17:46:21 -08002727 if (lq_sta->lq_info[active].win[i].stamp) {
Zhu Yib481de92007-09-25 17:54:57 -07002728 int delta =
2729 jiffies_to_msecs(now -
Tomas Winklerc33104f2008-01-14 17:46:21 -08002730 lq_sta->lq_info[active].win[i].stamp);
Zhu Yib481de92007-09-25 17:54:57 -07002731
2732 if (delta > max_time)
2733 max_time = delta;
2734
Tomas Winklerc33104f2008-01-14 17:46:21 -08002735 cnt += sprintf(&buf[cnt], "%5dms\n", delta);
Zhu Yib481de92007-09-25 17:54:57 -07002736 } else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002737 buf[cnt++] = '\n';
Zhu Yib481de92007-09-25 17:54:57 -07002738
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002739 j = iwl4965_get_prev_ieee_rate(i);
Zhu Yib481de92007-09-25 17:54:57 -07002740 if (j == i)
2741 break;
2742 i = j;
2743 }
2744
Tomas Winkler1826dcc2008-05-15 13:54:02 +08002745 /*
2746 * Display the average rate of all samples taken.
2747 * NOTE: We multiply # of samples by 2 since the IEEE measurement
2748 * added from iwl_rates is actually 2X the rate.
2749 */
Zhu Yib481de92007-09-25 17:54:57 -07002750 if (samples)
Tomas Winklerc33104f2008-01-14 17:46:21 -08002751 cnt += sprintf(&buf[cnt],
Zhu Yib481de92007-09-25 17:54:57 -07002752 "\nAverage rate is %3d.%02dMbs over last %4dms\n"
2753 "%3d%% success (%d good packets over %d tries)\n",
2754 success / (2 * samples), (success * 5 / samples) % 10,
2755 max_time, good * 100 / samples, good, samples);
2756 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002757 cnt += sprintf(&buf[cnt], "\nAverage rate: 0Mbs\n");
2758
2759 cnt += sprintf(&buf[cnt], "\nrate scale type %d antenna %d "
Zhu Yib481de92007-09-25 17:54:57 -07002760 "active_search %d rate index %d\n", lq_type, antenna,
Johannes Berg8318d782008-01-24 19:38:38 +01002761 lq_sta->search_better_tbl, sta->last_txrate_idx);
Zhu Yib481de92007-09-25 17:54:57 -07002762
Johannes Bergd0709a62008-02-25 16:27:46 +01002763 rcu_read_unlock();
Tomas Winklerc33104f2008-01-14 17:46:21 -08002764 return cnt;
Zhu Yib481de92007-09-25 17:54:57 -07002765}
2766
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002767void iwl4965_rate_scale_init(struct ieee80211_hw *hw, s32 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07002768{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002769 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07002770
2771 priv->lq_mngr.lq_ready = 1;
2772}
2773
Reinette Chatre897e1cf2008-03-28 16:21:09 -07002774int iwl4965_rate_control_register(void)
Zhu Yib481de92007-09-25 17:54:57 -07002775{
Reinette Chatre897e1cf2008-03-28 16:21:09 -07002776 return ieee80211_rate_control_register(&rs_ops);
Zhu Yib481de92007-09-25 17:54:57 -07002777}
2778
Reinette Chatre897e1cf2008-03-28 16:21:09 -07002779void iwl4965_rate_control_unregister(void)
Zhu Yib481de92007-09-25 17:54:57 -07002780{
2781 ieee80211_rate_control_unregister(&rs_ops);
2782}
2783