blob: b4edadf96ecf73d88817908299a06839400277c0 [file] [log] [blame]
Zhu Yib481de92007-09-25 17:54:57 -07001/******************************************************************************
2 *
3 * Copyright(c) 2005 - 2007 Intel Corporation. All rights reserved.
4 *
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>
31#include <net/ieee80211.h>
32
33#include <linux/netdevice.h>
34#include <linux/etherdevice.h>
35#include <linux/delay.h>
36
37#include <linux/workqueue.h>
38
Zhu Yib481de92007-09-25 17:54:57 -070039#include "../net/mac80211/ieee80211_rate.h"
40
Christoph Hellwig5d08cd12007-10-25 17:15:50 +080041#include "iwl-4965.h"
Zhu Yib481de92007-09-25 17:54:57 -070042#include "iwl-helpers.h"
43
44#define RS_NAME "iwl-4965-rs"
45
46#define NUM_TRY_BEFORE_ANTENNA_TOGGLE 1
47#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
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080067struct iwl4965_rate {
Zhu Yib481de92007-09-25 17:54:57 -070068 u32 rate_n_flags;
69} __attribute__ ((packed));
70
Ben Cahill77626352007-11-29 11:09:44 +080071/**
72 * struct iwl4965_rate_scale_data -- tx success history for one rate
73 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080074struct iwl4965_rate_scale_data {
Ben Cahill77626352007-11-29 11:09:44 +080075 u64 data; /* bitmap of successful frames */
76 s32 success_counter; /* number of frames successful */
77 s32 success_ratio; /* per-cent * 128 */
78 s32 counter; /* number of frames attempted */
79 s32 average_tpt; /* success ratio * expected throughput */
Zhu Yib481de92007-09-25 17:54:57 -070080 unsigned long stamp;
81};
82
Ben Cahill77626352007-11-29 11:09:44 +080083/**
84 * struct iwl4965_scale_tbl_info -- tx params and success history for all rates
85 *
86 * There are two of these in struct iwl_rate_scale_priv,
87 * one for "active", and one for "search".
88 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080089struct iwl4965_scale_tbl_info {
90 enum iwl4965_table_type lq_type;
91 enum iwl4965_antenna_type antenna_type;
Ben Cahill77626352007-11-29 11:09:44 +080092 u8 is_SGI; /* 1 = short guard interval */
93 u8 is_fat; /* 1 = 40 MHz channel width */
94 u8 is_dup; /* 1 = duplicated data streams */
95 u8 action; /* change modulation; IWL_[LEGACY/SISO/MIMO]_SWITCH_* */
96 s32 *expected_tpt; /* throughput metrics; expected_tpt_G, etc. */
97 struct iwl4965_rate current_rate; /* rate_n_flags, uCode API format */
98 struct iwl4965_rate_scale_data win[IWL_RATE_COUNT]; /* rate histories */
Zhu Yib481de92007-09-25 17:54:57 -070099};
100
Ben Cahill77626352007-11-29 11:09:44 +0800101/**
102 * struct iwl_rate_scale_priv -- driver's rate scaling private structure
103 *
104 * Pointer to this gets passed back and forth between driver and mac80211.
105 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800106struct iwl4965_rate_scale_priv {
Ben Cahill77626352007-11-29 11:09:44 +0800107 u8 active_tbl; /* index of active table, range 0-1 */
108 u8 enable_counter; /* indicates HT mode */
109 u8 stay_in_tbl; /* 1: disallow, 0: allow search for new mode */
110 u8 search_better_tbl; /* 1: currently trying alternate mode */
Zhu Yib481de92007-09-25 17:54:57 -0700111 s32 last_tpt;
Ben Cahill77626352007-11-29 11:09:44 +0800112
113 /* The following determine when to search for a new mode */
Zhu Yib481de92007-09-25 17:54:57 -0700114 u32 table_count_limit;
Ben Cahill77626352007-11-29 11:09:44 +0800115 u32 max_failure_limit; /* # failed frames before new search */
116 u32 max_success_limit; /* # successful frames before new search */
Zhu Yib481de92007-09-25 17:54:57 -0700117 u32 table_count;
Ben Cahill77626352007-11-29 11:09:44 +0800118 u32 total_failed; /* total failed frames, any/all rates */
119 u32 total_success; /* total successful frames, any/all rates */
120 u32 flush_timer; /* time staying in mode before new search */
121
122 u8 action_counter; /* # mode-switch actions tried */
Zhu Yib481de92007-09-25 17:54:57 -0700123 u8 antenna;
124 u8 valid_antenna;
125 u8 is_green;
126 u8 is_dup;
127 u8 phymode;
128 u8 ibss_sta_added;
Ben Cahill77626352007-11-29 11:09:44 +0800129
130 /* The following are bitmaps of rates; IWL_RATE_6M_MASK, etc. */
Zhu Yi02dede02007-09-27 11:27:40 +0800131 u32 supp_rates;
Zhu Yib481de92007-09-25 17:54:57 -0700132 u16 active_rate;
133 u16 active_siso_rate;
134 u16 active_mimo_rate;
135 u16 active_rate_basic;
Ben Cahill77626352007-11-29 11:09:44 +0800136
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800137 struct iwl4965_link_quality_cmd lq;
Ben Cahill77626352007-11-29 11:09:44 +0800138 struct iwl4965_scale_tbl_info lq_info[LQ_SIZE]; /* "active", "search" */
Zhu Yi5ae212c2007-09-27 11:27:38 +0800139#ifdef CONFIG_MAC80211_DEBUGFS
Zhu Yi98d7e092007-09-27 11:27:42 +0800140 struct dentry *rs_sta_dbgfs_scale_table_file;
Zhu Yi0209dc12007-09-27 11:27:43 +0800141 struct dentry *rs_sta_dbgfs_stats_table_file;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800142 struct iwl4965_rate dbg_fixed;
143 struct iwl4965_priv *drv;
Zhu Yi5ae212c2007-09-27 11:27:38 +0800144#endif
Zhu Yib481de92007-09-25 17:54:57 -0700145};
146
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800147static void rs_rate_scale_perform(struct iwl4965_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700148 struct net_device *dev,
149 struct ieee80211_hdr *hdr,
150 struct sta_info *sta);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800151static void rs_fill_link_cmd(struct iwl4965_rate_scale_priv *lq_data,
152 struct iwl4965_rate *tx_mcs,
153 struct iwl4965_link_quality_cmd *tbl);
Zhu Yib481de92007-09-25 17:54:57 -0700154
155
Zhu Yi98d7e092007-09-27 11:27:42 +0800156#ifdef CONFIG_MAC80211_DEBUGFS
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800157static void rs_dbgfs_set_mcs(struct iwl4965_rate_scale_priv *rs_priv,
158 struct iwl4965_rate *mcs, int index);
Zhu Yi98d7e092007-09-27 11:27:42 +0800159#else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800160static void rs_dbgfs_set_mcs(struct iwl4965_rate_scale_priv *rs_priv,
161 struct iwl4965_rate *mcs, int index)
Zhu Yi98d7e092007-09-27 11:27:42 +0800162{}
163#endif
Ben Cahill77626352007-11-29 11:09:44 +0800164
165/*
166 * Expected throughput metrics for following rates:
167 * 1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
168 * "G" is the only table that supports CCK (the first 4 rates).
169 */
Zhu Yib481de92007-09-25 17:54:57 -0700170static s32 expected_tpt_A[IWL_RATE_COUNT] = {
171 0, 0, 0, 0, 40, 57, 72, 98, 121, 154, 177, 186, 186
172};
173
174static s32 expected_tpt_G[IWL_RATE_COUNT] = {
175 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 186
176};
177
178static s32 expected_tpt_siso20MHz[IWL_RATE_COUNT] = {
179 0, 0, 0, 0, 42, 42, 76, 102, 124, 159, 183, 193, 202
180};
181
182static s32 expected_tpt_siso20MHzSGI[IWL_RATE_COUNT] = {
183 0, 0, 0, 0, 46, 46, 82, 110, 132, 168, 192, 202, 211
184};
185
186static s32 expected_tpt_mimo20MHz[IWL_RATE_COUNT] = {
187 0, 0, 0, 0, 74, 74, 123, 155, 179, 214, 236, 244, 251
188};
189
190static s32 expected_tpt_mimo20MHzSGI[IWL_RATE_COUNT] = {
191 0, 0, 0, 0, 81, 81, 131, 164, 188, 222, 243, 251, 257
192};
193
194static s32 expected_tpt_siso40MHz[IWL_RATE_COUNT] = {
195 0, 0, 0, 0, 77, 77, 127, 160, 184, 220, 242, 250, 257
196};
197
198static s32 expected_tpt_siso40MHzSGI[IWL_RATE_COUNT] = {
199 0, 0, 0, 0, 83, 83, 135, 169, 193, 229, 250, 257, 264
200};
201
202static s32 expected_tpt_mimo40MHz[IWL_RATE_COUNT] = {
203 0, 0, 0, 0, 123, 123, 182, 214, 235, 264, 279, 285, 289
204};
205
206static s32 expected_tpt_mimo40MHzSGI[IWL_RATE_COUNT] = {
207 0, 0, 0, 0, 131, 131, 191, 222, 242, 270, 284, 289, 293
208};
209
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800210static int iwl4965_lq_sync_callback(struct iwl4965_priv *priv,
211 struct iwl4965_cmd *cmd, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -0700212{
213 /*We didn't cache the SKB; let the caller free it */
214 return 1;
215}
216
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800217static inline u8 iwl4965_rate_get_rate(u32 rate_n_flags)
Zhu Yib481de92007-09-25 17:54:57 -0700218{
219 return (u8)(rate_n_flags & 0xFF);
220}
221
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800222static int rs_send_lq_cmd(struct iwl4965_priv *priv,
223 struct iwl4965_link_quality_cmd *lq, u8 flags)
Zhu Yib481de92007-09-25 17:54:57 -0700224{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800225#ifdef CONFIG_IWL4965_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -0700226 int i;
227#endif
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800228 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700229 .id = REPLY_TX_LINK_QUALITY_CMD,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800230 .len = sizeof(struct iwl4965_link_quality_cmd),
Zhu Yib481de92007-09-25 17:54:57 -0700231 .meta.flags = flags,
232 .data = lq,
233 };
234
235 if ((lq->sta_id == 0xFF) &&
236 (priv->iw_mode == IEEE80211_IF_TYPE_IBSS))
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800237 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -0700238
239 if (lq->sta_id == 0xFF)
240 lq->sta_id = IWL_AP_ID;
241
242 IWL_DEBUG_RATE("lq station id 0x%x\n", lq->sta_id);
243 IWL_DEBUG_RATE("lq dta 0x%X 0x%X\n",
244 lq->general_params.single_stream_ant_msk,
245 lq->general_params.dual_stream_ant_msk);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800246#ifdef CONFIG_IWL4965_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -0700247 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
248 IWL_DEBUG_RATE("lq index %d 0x%X\n",
249 i, lq->rs_table[i].rate_n_flags);
250#endif
251
252 if (flags & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800253 cmd.meta.u.callback = iwl4965_lq_sync_callback;
Zhu Yib481de92007-09-25 17:54:57 -0700254
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800255 if (iwl4965_is_associated(priv) && priv->assoc_station_added &&
Zhu Yib481de92007-09-25 17:54:57 -0700256 priv->lq_mngr.lq_ready)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800257 return iwl4965_send_cmd(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700258
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800259 return 0;
Zhu Yib481de92007-09-25 17:54:57 -0700260}
261
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800262static void rs_rate_scale_clear_window(struct iwl4965_rate_scale_data *window)
Zhu Yib481de92007-09-25 17:54:57 -0700263{
264 window->data = 0;
265 window->success_counter = 0;
266 window->success_ratio = IWL_INVALID_VALUE;
267 window->counter = 0;
268 window->average_tpt = IWL_INVALID_VALUE;
269 window->stamp = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700270}
271
Ben Cahill77626352007-11-29 11:09:44 +0800272/**
273 * rs_collect_tx_data - Update the success/failure sliding window
274 *
275 * We keep a sliding window of the last 62 packets transmitted
276 * at this rate. window->data contains the bitmask of successful
277 * packets.
278 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800279static int rs_collect_tx_data(struct iwl4965_rate_scale_data *windows,
Zhu Yib481de92007-09-25 17:54:57 -0700280 int scale_index, s32 tpt, u32 status)
281{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800282 struct iwl4965_rate_scale_data *window = NULL;
Zhu Yib481de92007-09-25 17:54:57 -0700283 u64 mask;
284 u8 win_size = IWL_RATE_MAX_WINDOW;
285 s32 fail_count;
286
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800287 if (scale_index < 0 || scale_index >= IWL_RATE_COUNT)
288 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -0700289
Ben Cahill77626352007-11-29 11:09:44 +0800290 /* Select data for current tx bit rate */
Zhu Yib481de92007-09-25 17:54:57 -0700291 window = &(windows[scale_index]);
292
Ben Cahill77626352007-11-29 11:09:44 +0800293 /*
294 * Keep track of only the latest 62 tx frame attempts in this rate's
295 * history window; anything older isn't really relevant any more.
296 * If we have filled up the sliding window, drop the oldest attempt;
297 * if the oldest attempt (highest bit in bitmap) shows "success",
298 * subtract "1" from the success counter (this is the main reason
299 * we keep these bitmaps!).
300 */
Zhu Yib481de92007-09-25 17:54:57 -0700301 if (window->counter >= win_size) {
Zhu Yib481de92007-09-25 17:54:57 -0700302 window->counter = win_size - 1;
303 mask = 1;
304 mask = (mask << (win_size - 1));
305 if ((window->data & mask)) {
306 window->data &= ~mask;
307 window->success_counter = window->success_counter - 1;
308 }
309 }
310
Ben Cahill77626352007-11-29 11:09:44 +0800311 /* Increment frames-attempted counter */
Zhu Yib481de92007-09-25 17:54:57 -0700312 window->counter = window->counter + 1;
Ben Cahill77626352007-11-29 11:09:44 +0800313
314 /* Shift bitmap by one frame (throw away oldest history),
315 * OR in "1", and increment "success" if this frame was successful. */
Zhu Yib481de92007-09-25 17:54:57 -0700316 mask = window->data;
317 window->data = (mask << 1);
318 if (status != 0) {
319 window->success_counter = window->success_counter + 1;
320 window->data |= 0x1;
321 }
322
Ben Cahill77626352007-11-29 11:09:44 +0800323 /* Calculate current success ratio, avoid divide-by-0! */
Zhu Yib481de92007-09-25 17:54:57 -0700324 if (window->counter > 0)
325 window->success_ratio = 128 * (100 * window->success_counter)
326 / window->counter;
327 else
328 window->success_ratio = IWL_INVALID_VALUE;
329
330 fail_count = window->counter - window->success_counter;
331
Ben Cahill77626352007-11-29 11:09:44 +0800332 /* Calculate average throughput, if we have enough history. */
Zhu Yib481de92007-09-25 17:54:57 -0700333 if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
334 (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
335 window->average_tpt = (window->success_ratio * tpt + 64) / 128;
336 else
337 window->average_tpt = IWL_INVALID_VALUE;
338
Ben Cahill77626352007-11-29 11:09:44 +0800339 /* Tag this window as having been updated */
Zhu Yib481de92007-09-25 17:54:57 -0700340 window->stamp = jiffies;
341
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800342 return 0;
Zhu Yib481de92007-09-25 17:54:57 -0700343}
344
Ben Cahill77626352007-11-29 11:09:44 +0800345/*
346 * Fill uCode API rate_n_flags field, based on "search" or "active" table.
347 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800348static void rs_mcs_from_tbl(struct iwl4965_rate *mcs_rate,
349 struct iwl4965_scale_tbl_info *tbl,
Zhu Yib481de92007-09-25 17:54:57 -0700350 int index, u8 use_green)
351{
Zhu Yib481de92007-09-25 17:54:57 -0700352 if (is_legacy(tbl->lq_type)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800353 mcs_rate->rate_n_flags = iwl4965_rates[index].plcp;
Zhu Yib481de92007-09-25 17:54:57 -0700354 if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
355 mcs_rate->rate_n_flags |= RATE_MCS_CCK_MSK;
356
357 } else if (is_siso(tbl->lq_type)) {
358 if (index > IWL_LAST_OFDM_RATE)
359 index = IWL_LAST_OFDM_RATE;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800360 mcs_rate->rate_n_flags = iwl4965_rates[index].plcp_siso |
Zhu Yib481de92007-09-25 17:54:57 -0700361 RATE_MCS_HT_MSK;
362 } else {
363 if (index > IWL_LAST_OFDM_RATE)
364 index = IWL_LAST_OFDM_RATE;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800365 mcs_rate->rate_n_flags = iwl4965_rates[index].plcp_mimo |
Zhu Yib481de92007-09-25 17:54:57 -0700366 RATE_MCS_HT_MSK;
367 }
368
369 switch (tbl->antenna_type) {
370 case ANT_BOTH:
371 mcs_rate->rate_n_flags |= RATE_MCS_ANT_AB_MSK;
372 break;
373 case ANT_MAIN:
374 mcs_rate->rate_n_flags |= RATE_MCS_ANT_A_MSK;
375 break;
376 case ANT_AUX:
377 mcs_rate->rate_n_flags |= RATE_MCS_ANT_B_MSK;
378 break;
379 case ANT_NONE:
380 break;
381 }
382
383 if (is_legacy(tbl->lq_type))
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800384 return;
Zhu Yib481de92007-09-25 17:54:57 -0700385
386 if (tbl->is_fat) {
387 if (tbl->is_dup)
388 mcs_rate->rate_n_flags |= RATE_MCS_DUP_MSK;
389 else
390 mcs_rate->rate_n_flags |= RATE_MCS_FAT_MSK;
391 }
392 if (tbl->is_SGI)
393 mcs_rate->rate_n_flags |= RATE_MCS_SGI_MSK;
394
395 if (use_green) {
396 mcs_rate->rate_n_flags |= RATE_MCS_GF_MSK;
397 if (is_siso(tbl->lq_type))
398 mcs_rate->rate_n_flags &= ~RATE_MCS_SGI_MSK;
399 }
Zhu Yib481de92007-09-25 17:54:57 -0700400}
401
Ben Cahill77626352007-11-29 11:09:44 +0800402/*
403 * Interpret uCode API's rate_n_flags format,
404 * fill "search" or "active" tx mode table.
405 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800406static int rs_get_tbl_info_from_mcs(const struct iwl4965_rate *mcs_rate,
407 int phymode, struct iwl4965_scale_tbl_info *tbl,
Zhu Yib481de92007-09-25 17:54:57 -0700408 int *rate_idx)
409{
410 int index;
411 u32 ant_msk;
412
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800413 index = iwl4965_rate_index_from_plcp(mcs_rate->rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -0700414
415 if (index == IWL_RATE_INVALID) {
416 *rate_idx = -1;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800417 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -0700418 }
Ben Cahill77626352007-11-29 11:09:44 +0800419 tbl->is_SGI = 0; /* default legacy setup */
Zhu Yib481de92007-09-25 17:54:57 -0700420 tbl->is_fat = 0;
421 tbl->is_dup = 0;
Ben Cahill77626352007-11-29 11:09:44 +0800422 tbl->antenna_type = ANT_BOTH; /* default MIMO setup */
Zhu Yib481de92007-09-25 17:54:57 -0700423
Ben Cahill77626352007-11-29 11:09:44 +0800424 /* legacy rate format */
Zhu Yib481de92007-09-25 17:54:57 -0700425 if (!(mcs_rate->rate_n_flags & RATE_MCS_HT_MSK)) {
426 ant_msk = (mcs_rate->rate_n_flags & RATE_MCS_ANT_AB_MSK);
427
428 if (ant_msk == RATE_MCS_ANT_AB_MSK)
429 tbl->lq_type = LQ_NONE;
430 else {
431
432 if (phymode == MODE_IEEE80211A)
433 tbl->lq_type = LQ_A;
434 else
435 tbl->lq_type = LQ_G;
436
437 if (mcs_rate->rate_n_flags & RATE_MCS_ANT_A_MSK)
438 tbl->antenna_type = ANT_MAIN;
439 else
440 tbl->antenna_type = ANT_AUX;
441 }
442 *rate_idx = index;
443
Ben Cahill77626352007-11-29 11:09:44 +0800444 /* HT rate format, SISO (might be 20 MHz legacy or 40 MHz fat width) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800445 } else if (iwl4965_rate_get_rate(mcs_rate->rate_n_flags)
Zhu Yib481de92007-09-25 17:54:57 -0700446 <= IWL_RATE_SISO_60M_PLCP) {
447 tbl->lq_type = LQ_SISO;
448
449 ant_msk = (mcs_rate->rate_n_flags & RATE_MCS_ANT_AB_MSK);
450 if (ant_msk == RATE_MCS_ANT_AB_MSK)
451 tbl->lq_type = LQ_NONE;
452 else {
453 if (mcs_rate->rate_n_flags & RATE_MCS_ANT_A_MSK)
454 tbl->antenna_type = ANT_MAIN;
455 else
456 tbl->antenna_type = ANT_AUX;
457 }
458 if (mcs_rate->rate_n_flags & RATE_MCS_SGI_MSK)
459 tbl->is_SGI = 1;
460
461 if ((mcs_rate->rate_n_flags & RATE_MCS_FAT_MSK) ||
462 (mcs_rate->rate_n_flags & RATE_MCS_DUP_MSK))
463 tbl->is_fat = 1;
464
465 if (mcs_rate->rate_n_flags & RATE_MCS_DUP_MSK)
466 tbl->is_dup = 1;
467
468 *rate_idx = index;
Ben Cahill77626352007-11-29 11:09:44 +0800469
470 /* HT rate format, MIMO (might be 20 MHz legacy or 40 MHz fat width) */
Zhu Yib481de92007-09-25 17:54:57 -0700471 } else {
472 tbl->lq_type = LQ_MIMO;
473 if (mcs_rate->rate_n_flags & RATE_MCS_SGI_MSK)
474 tbl->is_SGI = 1;
475
476 if ((mcs_rate->rate_n_flags & RATE_MCS_FAT_MSK) ||
477 (mcs_rate->rate_n_flags & RATE_MCS_DUP_MSK))
478 tbl->is_fat = 1;
479
480 if (mcs_rate->rate_n_flags & RATE_MCS_DUP_MSK)
481 tbl->is_dup = 1;
482 *rate_idx = index;
483 }
484 return 0;
485}
486
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800487static inline void rs_toggle_antenna(struct iwl4965_rate *new_rate,
488 struct iwl4965_scale_tbl_info *tbl)
Zhu Yib481de92007-09-25 17:54:57 -0700489{
490 if (tbl->antenna_type == ANT_AUX) {
491 tbl->antenna_type = ANT_MAIN;
492 new_rate->rate_n_flags &= ~RATE_MCS_ANT_B_MSK;
493 new_rate->rate_n_flags |= RATE_MCS_ANT_A_MSK;
494 } else {
495 tbl->antenna_type = ANT_AUX;
496 new_rate->rate_n_flags &= ~RATE_MCS_ANT_A_MSK;
497 new_rate->rate_n_flags |= RATE_MCS_ANT_B_MSK;
498 }
499}
500
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800501static inline u8 rs_use_green(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700502{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800503#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -0700504 if (!priv->is_ht_enabled || !priv->current_assoc_ht.is_ht)
505 return 0;
506
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800507 return ((priv->current_assoc_ht.is_green_field) &&
508 !(priv->current_assoc_ht.operating_mode & 0x4));
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800509#endif /*CONFIG_IWL4965_HT */
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800510 return 0;
Zhu Yib481de92007-09-25 17:54:57 -0700511}
512
513/**
514 * rs_get_supported_rates - get the available rates
515 *
516 * if management frame or broadcast frame only return
517 * basic available rates.
518 *
519 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800520static void rs_get_supported_rates(struct iwl4965_rate_scale_priv *lq_data,
Zhu Yib481de92007-09-25 17:54:57 -0700521 struct ieee80211_hdr *hdr,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800522 enum iwl4965_table_type rate_type,
Zhu Yib481de92007-09-25 17:54:57 -0700523 u16 *data_rate)
524{
525 if (is_legacy(rate_type))
526 *data_rate = lq_data->active_rate;
527 else {
528 if (is_siso(rate_type))
529 *data_rate = lq_data->active_siso_rate;
530 else
531 *data_rate = lq_data->active_mimo_rate;
532 }
533
534 if (hdr && is_multicast_ether_addr(hdr->addr1) &&
535 lq_data->active_rate_basic)
536 *data_rate = lq_data->active_rate_basic;
537}
538
539static u16 rs_get_adjacent_rate(u8 index, u16 rate_mask, int rate_type)
540{
541 u8 high = IWL_RATE_INVALID;
542 u8 low = IWL_RATE_INVALID;
543
Ian Schram01ebd062007-10-25 17:15:22 +0800544 /* 802.11A or ht walks to the next literal adjacent rate in
Zhu Yib481de92007-09-25 17:54:57 -0700545 * the rate table */
546 if (is_a_band(rate_type) || !is_legacy(rate_type)) {
547 int i;
548 u32 mask;
549
550 /* Find the previous rate that is in the rate mask */
551 i = index - 1;
552 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
553 if (rate_mask & mask) {
554 low = i;
555 break;
556 }
557 }
558
559 /* Find the next rate that is in the rate mask */
560 i = index + 1;
561 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
562 if (rate_mask & mask) {
563 high = i;
564 break;
565 }
566 }
567
568 return (high << 8) | low;
569 }
570
571 low = index;
572 while (low != IWL_RATE_INVALID) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800573 low = iwl4965_rates[low].prev_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700574 if (low == IWL_RATE_INVALID)
575 break;
576 if (rate_mask & (1 << low))
577 break;
578 IWL_DEBUG_RATE("Skipping masked lower rate: %d\n", low);
579 }
580
581 high = index;
582 while (high != IWL_RATE_INVALID) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800583 high = iwl4965_rates[high].next_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700584 if (high == IWL_RATE_INVALID)
585 break;
586 if (rate_mask & (1 << high))
587 break;
588 IWL_DEBUG_RATE("Skipping masked higher rate: %d\n", high);
589 }
590
591 return (high << 8) | low;
592}
593
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800594static void rs_get_lower_rate(struct iwl4965_rate_scale_priv *lq_data,
595 struct iwl4965_scale_tbl_info *tbl, u8 scale_index,
596 u8 ht_possible, struct iwl4965_rate *mcs_rate)
Zhu Yib481de92007-09-25 17:54:57 -0700597{
Zhu Yib481de92007-09-25 17:54:57 -0700598 s32 low;
599 u16 rate_mask;
600 u16 high_low;
601 u8 switch_to_legacy = 0;
Zhu Yi02dede02007-09-27 11:27:40 +0800602 u8 is_green = lq_data->is_green;
Zhu Yib481de92007-09-25 17:54:57 -0700603
604 /* check if we need to switch from HT to legacy rates.
605 * assumption is that mandatory rates (1Mbps or 6Mbps)
606 * are always supported (spec demand) */
607 if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_index)) {
608 switch_to_legacy = 1;
609 scale_index = rs_ht_to_legacy[scale_index];
610 if (lq_data->phymode == MODE_IEEE80211A)
611 tbl->lq_type = LQ_A;
612 else
613 tbl->lq_type = LQ_G;
614
615 if ((tbl->antenna_type == ANT_BOTH) ||
616 (tbl->antenna_type == ANT_NONE))
617 tbl->antenna_type = ANT_MAIN;
618
619 tbl->is_fat = 0;
620 tbl->is_SGI = 0;
621 }
622
623 rs_get_supported_rates(lq_data, NULL, tbl->lq_type, &rate_mask);
624
Ben Cahill77626352007-11-29 11:09:44 +0800625 /* Mask with station rate restriction */
Zhu Yib481de92007-09-25 17:54:57 -0700626 if (is_legacy(tbl->lq_type)) {
Ben Cahill77626352007-11-29 11:09:44 +0800627 /* supp_rates has no CCK bits in A mode */
Zhu Yib481de92007-09-25 17:54:57 -0700628 if (lq_data->phymode == (u8) MODE_IEEE80211A)
629 rate_mask = (u16)(rate_mask &
Zhu Yi02dede02007-09-27 11:27:40 +0800630 (lq_data->supp_rates << IWL_FIRST_OFDM_RATE));
Zhu Yib481de92007-09-25 17:54:57 -0700631 else
Zhu Yi02dede02007-09-27 11:27:40 +0800632 rate_mask = (u16)(rate_mask & lq_data->supp_rates);
Zhu Yib481de92007-09-25 17:54:57 -0700633 }
634
Ben Cahill77626352007-11-29 11:09:44 +0800635 /* If we switched from HT to legacy, check current rate */
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800636 if (switch_to_legacy && (rate_mask & (1 << scale_index))) {
Zhu Yib481de92007-09-25 17:54:57 -0700637 rs_mcs_from_tbl(mcs_rate, tbl, scale_index, is_green);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800638 return;
Zhu Yib481de92007-09-25 17:54:57 -0700639 }
640
641 high_low = rs_get_adjacent_rate(scale_index, rate_mask, tbl->lq_type);
642 low = high_low & 0xff;
643
644 if (low != IWL_RATE_INVALID)
645 rs_mcs_from_tbl(mcs_rate, tbl, low, is_green);
646 else
647 rs_mcs_from_tbl(mcs_rate, tbl, scale_index, is_green);
Zhu Yib481de92007-09-25 17:54:57 -0700648}
649
Ben Cahill77626352007-11-29 11:09:44 +0800650/*
651 * mac80211 sends us Tx status
652 */
Zhu Yib481de92007-09-25 17:54:57 -0700653static void rs_tx_status(void *priv_rate,
654 struct net_device *dev,
655 struct sk_buff *skb,
656 struct ieee80211_tx_status *tx_resp)
657{
658 int status;
659 u8 retries;
660 int rs_index, index = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800661 struct iwl4965_rate_scale_priv *lq;
662 struct iwl4965_link_quality_cmd *table;
Zhu Yib481de92007-09-25 17:54:57 -0700663 struct sta_info *sta;
664 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800665 struct iwl4965_priv *priv = (struct iwl4965_priv *)priv_rate;
Zhu Yib481de92007-09-25 17:54:57 -0700666 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800667 struct iwl4965_rate_scale_data *window = NULL;
668 struct iwl4965_rate_scale_data *search_win = NULL;
669 struct iwl4965_rate tx_mcs;
670 struct iwl4965_scale_tbl_info tbl_type;
671 struct iwl4965_scale_tbl_info *curr_tbl, *search_tbl;
Zhu Yib481de92007-09-25 17:54:57 -0700672 u8 active_index = 0;
673 u16 fc = le16_to_cpu(hdr->frame_control);
674 s32 tpt = 0;
675
Zhu Yi58826352007-09-27 11:27:39 +0800676 IWL_DEBUG_RATE_LIMIT("get frame ack response, update rate scale window\n");
Zhu Yib481de92007-09-25 17:54:57 -0700677
678 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1))
679 return;
680
681 retries = tx_resp->retry_count;
682
683 if (retries > 15)
684 retries = 15;
685
686
687 sta = sta_info_get(local, hdr->addr1);
688
689 if (!sta || !sta->rate_ctrl_priv) {
690 if (sta)
691 sta_info_put(sta);
692 return;
693 }
694
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800695 lq = (struct iwl4965_rate_scale_priv *)sta->rate_ctrl_priv;
Zhu Yib481de92007-09-25 17:54:57 -0700696
697 if (!priv->lq_mngr.lq_ready)
698 return;
699
700 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) && !lq->ibss_sta_added)
701 return;
702
703 table = &lq->lq;
704 active_index = lq->active_tbl;
705
Ben Cahill77626352007-11-29 11:09:44 +0800706 /* Get mac80211 antenna info */
Zhu Yib481de92007-09-25 17:54:57 -0700707 lq->antenna = (lq->valid_antenna & local->hw.conf.antenna_sel_tx);
708 if (!lq->antenna)
709 lq->antenna = lq->valid_antenna;
710
Ben Cahill77626352007-11-29 11:09:44 +0800711 /* Ignore mac80211 antenna info for now */
Zhu Yib481de92007-09-25 17:54:57 -0700712 lq->antenna = lq->valid_antenna;
Ben Cahill77626352007-11-29 11:09:44 +0800713
Zhu Yib481de92007-09-25 17:54:57 -0700714 curr_tbl = &(lq->lq_info[active_index]);
715 search_tbl = &(lq->lq_info[(1 - active_index)]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800716 window = (struct iwl4965_rate_scale_data *)
Zhu Yib481de92007-09-25 17:54:57 -0700717 &(curr_tbl->win[0]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800718 search_win = (struct iwl4965_rate_scale_data *)
Zhu Yib481de92007-09-25 17:54:57 -0700719 &(search_tbl->win[0]);
720
721 tx_mcs.rate_n_flags = tx_resp->control.tx_rate;
722
723 rs_get_tbl_info_from_mcs(&tx_mcs, priv->phymode,
724 &tbl_type, &rs_index);
725 if ((rs_index < 0) || (rs_index >= IWL_RATE_COUNT)) {
726 IWL_DEBUG_RATE("bad rate index at: %d rate 0x%X\n",
727 rs_index, tx_mcs.rate_n_flags);
728 sta_info_put(sta);
729 return;
730 }
731
Ben Cahill77626352007-11-29 11:09:44 +0800732 /*
733 * Ignore this Tx frame response if its initial rate doesn't match
734 * that of latest Link Quality command. There may be stragglers
735 * from a previous Link Quality command, but we're no longer interested
736 * in those; they're either from the "active" mode while we're trying
737 * to check "search" mode, or a prior "search" mode after we've moved
738 * to a new "search" mode (which might become the new "active" mode).
739 */
Zhu Yib481de92007-09-25 17:54:57 -0700740 if (retries &&
741 (tx_mcs.rate_n_flags !=
742 le32_to_cpu(table->rs_table[0].rate_n_flags))) {
743 IWL_DEBUG_RATE("initial rate does not match 0x%x 0x%x\n",
744 tx_mcs.rate_n_flags,
745 le32_to_cpu(table->rs_table[0].rate_n_flags));
746 sta_info_put(sta);
747 return;
748 }
749
Ben Cahill77626352007-11-29 11:09:44 +0800750 /* Update frame history window with "failure" for each Tx retry. */
Zhu Yib481de92007-09-25 17:54:57 -0700751 while (retries) {
Ben Cahill77626352007-11-29 11:09:44 +0800752 /* Look up the rate and other info used for each tx attempt.
753 * Each tx attempt steps one entry deeper in the rate table. */
Zhu Yib481de92007-09-25 17:54:57 -0700754 tx_mcs.rate_n_flags =
755 le32_to_cpu(table->rs_table[index].rate_n_flags);
756 rs_get_tbl_info_from_mcs(&tx_mcs, priv->phymode,
757 &tbl_type, &rs_index);
758
Ben Cahill77626352007-11-29 11:09:44 +0800759 /* If type matches "search" table,
760 * add failure to "search" history */
Zhu Yib481de92007-09-25 17:54:57 -0700761 if ((tbl_type.lq_type == search_tbl->lq_type) &&
762 (tbl_type.antenna_type == search_tbl->antenna_type) &&
763 (tbl_type.is_SGI == search_tbl->is_SGI)) {
764 if (search_tbl->expected_tpt)
765 tpt = search_tbl->expected_tpt[rs_index];
766 else
767 tpt = 0;
Ben Cahill77626352007-11-29 11:09:44 +0800768 rs_collect_tx_data(search_win, rs_index, tpt, 0);
769
770 /* Else if type matches "current/active" table,
771 * add failure to "current/active" history */
Zhu Yib481de92007-09-25 17:54:57 -0700772 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
773 (tbl_type.antenna_type == curr_tbl->antenna_type) &&
774 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
775 if (curr_tbl->expected_tpt)
776 tpt = curr_tbl->expected_tpt[rs_index];
777 else
778 tpt = 0;
779 rs_collect_tx_data(window, rs_index, tpt, 0);
780 }
Ben Cahill77626352007-11-29 11:09:44 +0800781
782 /* If not searching for a new mode, increment failed counter
783 * ... this helps determine when to start searching again */
Zhu Yib481de92007-09-25 17:54:57 -0700784 if (lq->stay_in_tbl)
785 lq->total_failed++;
786 --retries;
787 index++;
788
789 }
790
Ben Cahill77626352007-11-29 11:09:44 +0800791 /*
792 * Find (by rate) the history window to update with final Tx attempt;
793 * if Tx was successful first try, use original rate,
794 * else look up the rate that was, finally, successful.
795 */
Zhu Yib481de92007-09-25 17:54:57 -0700796 if (!tx_resp->retry_count)
797 tx_mcs.rate_n_flags = tx_resp->control.tx_rate;
798 else
799 tx_mcs.rate_n_flags =
800 le32_to_cpu(table->rs_table[index].rate_n_flags);
801
802 rs_get_tbl_info_from_mcs(&tx_mcs, priv->phymode,
803 &tbl_type, &rs_index);
804
Ben Cahill77626352007-11-29 11:09:44 +0800805 /* Update frame history window with "success" if Tx got ACKed ... */
Zhu Yib481de92007-09-25 17:54:57 -0700806 if (tx_resp->flags & IEEE80211_TX_STATUS_ACK)
807 status = 1;
808 else
809 status = 0;
810
Ben Cahill77626352007-11-29 11:09:44 +0800811 /* If type matches "search" table,
812 * add final tx status to "search" history */
Zhu Yib481de92007-09-25 17:54:57 -0700813 if ((tbl_type.lq_type == search_tbl->lq_type) &&
814 (tbl_type.antenna_type == search_tbl->antenna_type) &&
815 (tbl_type.is_SGI == search_tbl->is_SGI)) {
816 if (search_tbl->expected_tpt)
817 tpt = search_tbl->expected_tpt[rs_index];
818 else
819 tpt = 0;
820 rs_collect_tx_data(search_win,
821 rs_index, tpt, status);
Ben Cahill77626352007-11-29 11:09:44 +0800822
823 /* Else if type matches "current/active" table,
824 * add final tx status to "current/active" history */
Zhu Yib481de92007-09-25 17:54:57 -0700825 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
826 (tbl_type.antenna_type == curr_tbl->antenna_type) &&
827 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
828 if (curr_tbl->expected_tpt)
829 tpt = curr_tbl->expected_tpt[rs_index];
830 else
831 tpt = 0;
832 rs_collect_tx_data(window, rs_index, tpt, status);
833 }
834
Ben Cahill77626352007-11-29 11:09:44 +0800835 /* If not searching for new mode, increment success/failed counter
836 * ... these help determine when to start searching again */
Zhu Yib481de92007-09-25 17:54:57 -0700837 if (lq->stay_in_tbl) {
838 if (status)
839 lq->total_success++;
840 else
841 lq->total_failed++;
842 }
843
Ben Cahill77626352007-11-29 11:09:44 +0800844 /* See if there's a better rate or modulation mode to try. */
Zhu Yib481de92007-09-25 17:54:57 -0700845 rs_rate_scale_perform(priv, dev, hdr, sta);
846 sta_info_put(sta);
847 return;
848}
849
850static u8 rs_is_ant_connected(u8 valid_antenna,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800851 enum iwl4965_antenna_type antenna_type)
Zhu Yib481de92007-09-25 17:54:57 -0700852{
853 if (antenna_type == ANT_AUX)
854 return ((valid_antenna & 0x2) ? 1:0);
855 else if (antenna_type == ANT_MAIN)
856 return ((valid_antenna & 0x1) ? 1:0);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800857 else if (antenna_type == ANT_BOTH)
858 return ((valid_antenna & 0x3) == 0x3);
Zhu Yib481de92007-09-25 17:54:57 -0700859
860 return 1;
861}
862
863static u8 rs_is_other_ant_connected(u8 valid_antenna,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800864 enum iwl4965_antenna_type antenna_type)
Zhu Yib481de92007-09-25 17:54:57 -0700865{
866 if (antenna_type == ANT_AUX)
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800867 return rs_is_ant_connected(valid_antenna, ANT_MAIN);
Zhu Yib481de92007-09-25 17:54:57 -0700868 else
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800869 return rs_is_ant_connected(valid_antenna, ANT_AUX);
Zhu Yib481de92007-09-25 17:54:57 -0700870
871 return 0;
872}
873
Ben Cahill77626352007-11-29 11:09:44 +0800874/*
875 * Begin a period of staying with a selected modulation mode.
876 * Set "stay_in_tbl" flag to prevent any mode switches.
877 * Set frame tx success limits according to legacy vs. high-throughput,
878 * and reset overall (spanning all rates) tx success history statistics.
879 * These control how long we stay using same modulation mode before
880 * searching for a new mode.
881 */
Zhu Yib481de92007-09-25 17:54:57 -0700882static void rs_set_stay_in_table(u8 is_legacy,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800883 struct iwl4965_rate_scale_priv *lq_data)
Zhu Yib481de92007-09-25 17:54:57 -0700884{
885 IWL_DEBUG_HT("we are staying in the same table\n");
Ben Cahill77626352007-11-29 11:09:44 +0800886 lq_data->stay_in_tbl = 1; /* only place this gets set */
Zhu Yib481de92007-09-25 17:54:57 -0700887 if (is_legacy) {
888 lq_data->table_count_limit = IWL_LEGACY_TABLE_COUNT;
889 lq_data->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
Mohamed Abbas403ab562007-11-06 22:06:25 -0800890 lq_data->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
Zhu Yib481de92007-09-25 17:54:57 -0700891 } else {
892 lq_data->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
893 lq_data->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
894 lq_data->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
895 }
896 lq_data->table_count = 0;
897 lq_data->total_failed = 0;
898 lq_data->total_success = 0;
899}
900
Ben Cahill77626352007-11-29 11:09:44 +0800901/*
902 * Find correct throughput table for given mode of modulation
903 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800904static void rs_get_expected_tpt_table(struct iwl4965_rate_scale_priv *lq_data,
905 struct iwl4965_scale_tbl_info *tbl)
Zhu Yib481de92007-09-25 17:54:57 -0700906{
907 if (is_legacy(tbl->lq_type)) {
908 if (!is_a_band(tbl->lq_type))
909 tbl->expected_tpt = expected_tpt_G;
910 else
911 tbl->expected_tpt = expected_tpt_A;
912 } else if (is_siso(tbl->lq_type)) {
913 if (tbl->is_fat && !lq_data->is_dup)
914 if (tbl->is_SGI)
915 tbl->expected_tpt = expected_tpt_siso40MHzSGI;
916 else
917 tbl->expected_tpt = expected_tpt_siso40MHz;
918 else if (tbl->is_SGI)
919 tbl->expected_tpt = expected_tpt_siso20MHzSGI;
920 else
921 tbl->expected_tpt = expected_tpt_siso20MHz;
922
923 } else if (is_mimo(tbl->lq_type)) {
924 if (tbl->is_fat && !lq_data->is_dup)
925 if (tbl->is_SGI)
926 tbl->expected_tpt = expected_tpt_mimo40MHzSGI;
927 else
928 tbl->expected_tpt = expected_tpt_mimo40MHz;
929 else if (tbl->is_SGI)
930 tbl->expected_tpt = expected_tpt_mimo20MHzSGI;
931 else
932 tbl->expected_tpt = expected_tpt_mimo20MHz;
933 } else
934 tbl->expected_tpt = expected_tpt_G;
935}
936
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800937#ifdef CONFIG_IWL4965_HT
Ben Cahill77626352007-11-29 11:09:44 +0800938/*
939 * Find starting rate for new "search" high-throughput mode of modulation.
940 * Goal is to find lowest expected rate (under perfect conditions) that is
941 * above the current measured throughput of "active" mode, to give new mode
942 * a fair chance to prove itself without too many challenges.
943 *
944 * This gets called when transitioning to more aggressive modulation
945 * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
946 * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
947 * to decrease to match "active" throughput. When moving from MIMO to SISO,
948 * bit rate will typically need to increase, but not if performance was bad.
949 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800950static s32 rs_get_best_rate(struct iwl4965_priv *priv,
951 struct iwl4965_rate_scale_priv *lq_data,
Ben Cahill77626352007-11-29 11:09:44 +0800952 struct iwl4965_scale_tbl_info *tbl, /* "search" */
Zhu Yib481de92007-09-25 17:54:57 -0700953 u16 rate_mask, s8 index, s8 rate)
954{
Ben Cahill77626352007-11-29 11:09:44 +0800955 /* "active" values */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800956 struct iwl4965_scale_tbl_info *active_tbl =
Zhu Yib481de92007-09-25 17:54:57 -0700957 &(lq_data->lq_info[lq_data->active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -0700958 s32 active_sr = active_tbl->win[index].success_ratio;
Zhu Yib481de92007-09-25 17:54:57 -0700959 s32 active_tpt = active_tbl->expected_tpt[index];
Ben Cahill77626352007-11-29 11:09:44 +0800960
961 /* expected "search" throughput */
962 s32 *tpt_tbl = tbl->expected_tpt;
963
964 s32 new_rate, high, low, start_hi;
Zhu Yib481de92007-09-25 17:54:57 -0700965 u16 high_low;
966
967 new_rate = high = low = start_hi = IWL_RATE_INVALID;
968
969 for (; ;) {
970 high_low = rs_get_adjacent_rate(rate, rate_mask, tbl->lq_type);
971
972 low = high_low & 0xff;
973 high = (high_low >> 8) & 0xff;
974
Ben Cahill77626352007-11-29 11:09:44 +0800975 /*
976 * Lower the "search" bit rate, to give new "search" mode
977 * approximately the same throughput as "active" if:
978 *
979 * 1) "Active" mode has been working modestly well (but not
980 * great), and expected "search" throughput (under perfect
981 * conditions) at candidate rate is above the actual
982 * measured "active" throughput (but less than expected
983 * "active" throughput under perfect conditions).
984 * OR
985 * 2) "Active" mode has been working perfectly or very well
986 * and expected "search" throughput (under perfect
987 * conditions) at candidate rate is above expected
988 * "active" throughput (under perfect conditions).
989 */
Zhu Yib481de92007-09-25 17:54:57 -0700990 if ((((100 * tpt_tbl[rate]) > lq_data->last_tpt) &&
991 ((active_sr > IWL_RATE_DECREASE_TH) &&
992 (active_sr <= IWL_RATE_HIGH_TH) &&
993 (tpt_tbl[rate] <= active_tpt))) ||
994 ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
995 (tpt_tbl[rate] > active_tpt))) {
996
Ben Cahill77626352007-11-29 11:09:44 +0800997 /* (2nd or later pass)
998 * If we've already tried to raise the rate, and are
999 * now trying to lower it, use the higher rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001000 if (start_hi != IWL_RATE_INVALID) {
1001 new_rate = start_hi;
1002 break;
1003 }
Ben Cahill77626352007-11-29 11:09:44 +08001004
Zhu Yib481de92007-09-25 17:54:57 -07001005 new_rate = rate;
Ben Cahill77626352007-11-29 11:09:44 +08001006
1007 /* Loop again with lower rate */
Zhu Yib481de92007-09-25 17:54:57 -07001008 if (low != IWL_RATE_INVALID)
1009 rate = low;
Ben Cahill77626352007-11-29 11:09:44 +08001010
1011 /* Lower rate not available, use the original */
Zhu Yib481de92007-09-25 17:54:57 -07001012 else
1013 break;
Ben Cahill77626352007-11-29 11:09:44 +08001014
1015 /* Else try to raise the "search" rate to match "active" */
Zhu Yib481de92007-09-25 17:54:57 -07001016 } else {
Ben Cahill77626352007-11-29 11:09:44 +08001017 /* (2nd or later pass)
1018 * If we've already tried to lower the rate, and are
1019 * now trying to raise it, use the lower rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001020 if (new_rate != IWL_RATE_INVALID)
1021 break;
Ben Cahill77626352007-11-29 11:09:44 +08001022
1023 /* Loop again with higher rate */
Zhu Yib481de92007-09-25 17:54:57 -07001024 else if (high != IWL_RATE_INVALID) {
1025 start_hi = high;
1026 rate = high;
Ben Cahill77626352007-11-29 11:09:44 +08001027
1028 /* Higher rate not available, use the original */
Zhu Yib481de92007-09-25 17:54:57 -07001029 } else {
1030 new_rate = rate;
1031 break;
1032 }
1033 }
1034 }
1035
1036 return new_rate;
1037}
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001038#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001039
1040static inline u8 rs_is_both_ant_supp(u8 valid_antenna)
1041{
1042 return (rs_is_ant_connected(valid_antenna, ANT_BOTH));
1043}
1044
Ben Cahill77626352007-11-29 11:09:44 +08001045/*
1046 * Set up search table for MIMO
1047 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001048static int rs_switch_to_mimo(struct iwl4965_priv *priv,
1049 struct iwl4965_rate_scale_priv *lq_data,
1050 struct iwl4965_scale_tbl_info *tbl, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001051{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001052#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07001053 u16 rate_mask;
1054 s32 rate;
1055 s8 is_green = lq_data->is_green;
1056
1057 if (!priv->is_ht_enabled || !priv->current_assoc_ht.is_ht)
1058 return -1;
1059
1060 IWL_DEBUG_HT("LQ: try to switch to MIMO\n");
1061 tbl->lq_type = LQ_MIMO;
1062 rs_get_supported_rates(lq_data, NULL, tbl->lq_type,
1063 &rate_mask);
1064
1065 if (priv->current_assoc_ht.tx_mimo_ps_mode == IWL_MIMO_PS_STATIC)
1066 return -1;
1067
Ben Cahill77626352007-11-29 11:09:44 +08001068 /* Need both Tx chains/antennas to support MIMO */
Zhu Yib481de92007-09-25 17:54:57 -07001069 if (!rs_is_both_ant_supp(lq_data->antenna))
1070 return -1;
1071
Zhu Yib481de92007-09-25 17:54:57 -07001072 tbl->is_dup = lq_data->is_dup;
1073 tbl->action = 0;
1074 if (priv->current_channel_width == IWL_CHANNEL_WIDTH_40MHZ)
1075 tbl->is_fat = 1;
1076 else
1077 tbl->is_fat = 0;
1078
1079 if (tbl->is_fat) {
1080 if (priv->current_assoc_ht.sgf & HT_SHORT_GI_40MHZ_ONLY)
1081 tbl->is_SGI = 1;
1082 else
1083 tbl->is_SGI = 0;
1084 } else if (priv->current_assoc_ht.sgf & HT_SHORT_GI_20MHZ_ONLY)
1085 tbl->is_SGI = 1;
1086 else
1087 tbl->is_SGI = 0;
1088
1089 rs_get_expected_tpt_table(lq_data, tbl);
1090
1091 rate = rs_get_best_rate(priv, lq_data, tbl, rate_mask, index, index);
1092
1093 IWL_DEBUG_HT("LQ: MIMO best rate %d mask %X\n", rate, rate_mask);
1094 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask))
1095 return -1;
1096 rs_mcs_from_tbl(&tbl->current_rate, tbl, rate, is_green);
1097
1098 IWL_DEBUG_HT("LQ: Switch to new mcs %X index is green %X\n",
1099 tbl->current_rate.rate_n_flags, is_green);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001100 return 0;
Mohamed Abbas403ab562007-11-06 22:06:25 -08001101#else
1102 return -1;
1103#endif /*CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001104}
1105
Ben Cahill77626352007-11-29 11:09:44 +08001106/*
1107 * Set up search table for SISO
1108 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001109static int rs_switch_to_siso(struct iwl4965_priv *priv,
1110 struct iwl4965_rate_scale_priv *lq_data,
1111 struct iwl4965_scale_tbl_info *tbl, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001112{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001113#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07001114 u16 rate_mask;
1115 u8 is_green = lq_data->is_green;
1116 s32 rate;
1117
1118 IWL_DEBUG_HT("LQ: try to switch to SISO\n");
1119 if (!priv->is_ht_enabled || !priv->current_assoc_ht.is_ht)
1120 return -1;
1121
Zhu Yib481de92007-09-25 17:54:57 -07001122 tbl->is_dup = lq_data->is_dup;
1123 tbl->lq_type = LQ_SISO;
1124 tbl->action = 0;
1125 rs_get_supported_rates(lq_data, NULL, tbl->lq_type,
1126 &rate_mask);
1127
1128 if (priv->current_channel_width == IWL_CHANNEL_WIDTH_40MHZ)
1129 tbl->is_fat = 1;
1130 else
1131 tbl->is_fat = 0;
1132
1133 if (tbl->is_fat) {
1134 if (priv->current_assoc_ht.sgf & HT_SHORT_GI_40MHZ_ONLY)
1135 tbl->is_SGI = 1;
1136 else
1137 tbl->is_SGI = 0;
1138 } else if (priv->current_assoc_ht.sgf & HT_SHORT_GI_20MHZ_ONLY)
1139 tbl->is_SGI = 1;
1140 else
1141 tbl->is_SGI = 0;
1142
1143 if (is_green)
1144 tbl->is_SGI = 0;
1145
1146 rs_get_expected_tpt_table(lq_data, tbl);
1147 rate = rs_get_best_rate(priv, lq_data, tbl, rate_mask, index, index);
1148
1149 IWL_DEBUG_HT("LQ: get best rate %d mask %X\n", rate, rate_mask);
1150 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1151 IWL_DEBUG_HT("can not switch with index %d rate mask %x\n",
1152 rate, rate_mask);
1153 return -1;
1154 }
1155 rs_mcs_from_tbl(&tbl->current_rate, tbl, rate, is_green);
1156 IWL_DEBUG_HT("LQ: Switch to new mcs %X index is green %X\n",
1157 tbl->current_rate.rate_n_flags, is_green);
Mohamed Abbas403ab562007-11-06 22:06:25 -08001158 return 0;
1159#else
1160 return -1;
Zhu Yib481de92007-09-25 17:54:57 -07001161
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001162#endif /*CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001163}
1164
Ben Cahill77626352007-11-29 11:09:44 +08001165/*
1166 * Try to switch to new modulation mode from legacy
1167 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001168static int rs_move_legacy_other(struct iwl4965_priv *priv,
1169 struct iwl4965_rate_scale_priv *lq_data,
Zhu Yib481de92007-09-25 17:54:57 -07001170 int index)
1171{
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001172 int ret = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001173 struct iwl4965_scale_tbl_info *tbl =
Zhu Yib481de92007-09-25 17:54:57 -07001174 &(lq_data->lq_info[lq_data->active_tbl]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001175 struct iwl4965_scale_tbl_info *search_tbl =
Zhu Yib481de92007-09-25 17:54:57 -07001176 &(lq_data->lq_info[(1 - lq_data->active_tbl)]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001177 struct iwl4965_rate_scale_data *window = &(tbl->win[index]);
1178 u32 sz = (sizeof(struct iwl4965_scale_tbl_info) -
1179 (sizeof(struct iwl4965_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001180 u8 start_action = tbl->action;
1181
1182 for (; ;) {
1183 switch (tbl->action) {
1184 case IWL_LEGACY_SWITCH_ANTENNA:
1185 IWL_DEBUG_HT("LQ Legacy switch Antenna\n");
1186
1187 search_tbl->lq_type = LQ_NONE;
1188 lq_data->action_counter++;
Ben Cahill77626352007-11-29 11:09:44 +08001189
1190 /* Don't change antenna if success has been great */
Zhu Yib481de92007-09-25 17:54:57 -07001191 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1192 break;
Ben Cahill77626352007-11-29 11:09:44 +08001193
1194 /* Don't change antenna if other one is not connected */
Zhu Yib481de92007-09-25 17:54:57 -07001195 if (!rs_is_other_ant_connected(lq_data->antenna,
1196 tbl->antenna_type))
1197 break;
1198
Ben Cahill77626352007-11-29 11:09:44 +08001199 /* Set up search table to try other antenna */
Zhu Yib481de92007-09-25 17:54:57 -07001200 memcpy(search_tbl, tbl, sz);
1201
1202 rs_toggle_antenna(&(search_tbl->current_rate),
1203 search_tbl);
1204 rs_get_expected_tpt_table(lq_data, search_tbl);
1205 lq_data->search_better_tbl = 1;
1206 goto out;
1207
1208 case IWL_LEGACY_SWITCH_SISO:
1209 IWL_DEBUG_HT("LQ: Legacy switch to SISO\n");
Ben Cahill77626352007-11-29 11:09:44 +08001210
1211 /* Set up search table to try SISO */
Zhu Yib481de92007-09-25 17:54:57 -07001212 memcpy(search_tbl, tbl, sz);
1213 search_tbl->lq_type = LQ_SISO;
1214 search_tbl->is_SGI = 0;
1215 search_tbl->is_fat = 0;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001216 ret = rs_switch_to_siso(priv, lq_data, search_tbl,
Zhu Yib481de92007-09-25 17:54:57 -07001217 index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001218 if (!ret) {
Zhu Yib481de92007-09-25 17:54:57 -07001219 lq_data->search_better_tbl = 1;
1220 lq_data->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001221 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001222 }
Zhu Yib481de92007-09-25 17:54:57 -07001223
1224 break;
1225 case IWL_LEGACY_SWITCH_MIMO:
1226 IWL_DEBUG_HT("LQ: Legacy switch MIMO\n");
Ben Cahill77626352007-11-29 11:09:44 +08001227
1228 /* Set up search table to try MIMO */
Zhu Yib481de92007-09-25 17:54:57 -07001229 memcpy(search_tbl, tbl, sz);
1230 search_tbl->lq_type = LQ_MIMO;
1231 search_tbl->is_SGI = 0;
1232 search_tbl->is_fat = 0;
1233 search_tbl->antenna_type = ANT_BOTH;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001234 ret = rs_switch_to_mimo(priv, lq_data, search_tbl,
Zhu Yib481de92007-09-25 17:54:57 -07001235 index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001236 if (!ret) {
Zhu Yib481de92007-09-25 17:54:57 -07001237 lq_data->search_better_tbl = 1;
1238 lq_data->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001239 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001240 }
Zhu Yib481de92007-09-25 17:54:57 -07001241 break;
1242 }
1243 tbl->action++;
1244 if (tbl->action > IWL_LEGACY_SWITCH_MIMO)
1245 tbl->action = IWL_LEGACY_SWITCH_ANTENNA;
1246
1247 if (tbl->action == start_action)
1248 break;
1249
1250 }
1251 return 0;
1252
1253 out:
1254 tbl->action++;
1255 if (tbl->action > IWL_LEGACY_SWITCH_MIMO)
1256 tbl->action = IWL_LEGACY_SWITCH_ANTENNA;
1257 return 0;
1258
1259}
1260
Ben Cahill77626352007-11-29 11:09:44 +08001261/*
1262 * Try to switch to new modulation mode from SISO
1263 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001264static int rs_move_siso_to_other(struct iwl4965_priv *priv,
1265 struct iwl4965_rate_scale_priv *lq_data,
Zhu Yib481de92007-09-25 17:54:57 -07001266 int index)
1267{
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001268 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001269 u8 is_green = lq_data->is_green;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001270 struct iwl4965_scale_tbl_info *tbl =
Zhu Yib481de92007-09-25 17:54:57 -07001271 &(lq_data->lq_info[lq_data->active_tbl]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001272 struct iwl4965_scale_tbl_info *search_tbl =
Zhu Yib481de92007-09-25 17:54:57 -07001273 &(lq_data->lq_info[(1 - lq_data->active_tbl)]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001274 struct iwl4965_rate_scale_data *window = &(tbl->win[index]);
1275 u32 sz = (sizeof(struct iwl4965_scale_tbl_info) -
1276 (sizeof(struct iwl4965_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001277 u8 start_action = tbl->action;
1278
1279 for (;;) {
1280 lq_data->action_counter++;
1281 switch (tbl->action) {
1282 case IWL_SISO_SWITCH_ANTENNA:
1283 IWL_DEBUG_HT("LQ: SISO SWITCH ANTENNA SISO\n");
1284 search_tbl->lq_type = LQ_NONE;
1285 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1286 break;
1287 if (!rs_is_other_ant_connected(lq_data->antenna,
1288 tbl->antenna_type))
1289 break;
1290
1291 memcpy(search_tbl, tbl, sz);
1292 search_tbl->action = IWL_SISO_SWITCH_MIMO;
1293 rs_toggle_antenna(&(search_tbl->current_rate),
1294 search_tbl);
1295 lq_data->search_better_tbl = 1;
1296
1297 goto out;
1298
1299 case IWL_SISO_SWITCH_MIMO:
1300 IWL_DEBUG_HT("LQ: SISO SWITCH TO MIMO FROM SISO\n");
1301 memcpy(search_tbl, tbl, sz);
1302 search_tbl->lq_type = LQ_MIMO;
1303 search_tbl->is_SGI = 0;
1304 search_tbl->is_fat = 0;
1305 search_tbl->antenna_type = ANT_BOTH;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001306 ret = rs_switch_to_mimo(priv, lq_data, search_tbl,
Zhu Yib481de92007-09-25 17:54:57 -07001307 index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001308 if (!ret) {
Zhu Yib481de92007-09-25 17:54:57 -07001309 lq_data->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001310 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001311 }
Zhu Yib481de92007-09-25 17:54:57 -07001312 break;
1313 case IWL_SISO_SWITCH_GI:
1314 IWL_DEBUG_HT("LQ: SISO SWITCH TO GI\n");
1315 memcpy(search_tbl, tbl, sz);
1316 search_tbl->action = 0;
1317 if (search_tbl->is_SGI)
1318 search_tbl->is_SGI = 0;
1319 else if (!is_green)
1320 search_tbl->is_SGI = 1;
1321 else
1322 break;
1323 lq_data->search_better_tbl = 1;
1324 if ((tbl->lq_type == LQ_SISO) &&
1325 (tbl->is_SGI)) {
1326 s32 tpt = lq_data->last_tpt / 100;
1327 if (((!tbl->is_fat) &&
1328 (tpt >= expected_tpt_siso20MHz[index])) ||
1329 ((tbl->is_fat) &&
1330 (tpt >= expected_tpt_siso40MHz[index])))
1331 lq_data->search_better_tbl = 0;
1332 }
1333 rs_get_expected_tpt_table(lq_data, search_tbl);
1334 rs_mcs_from_tbl(&search_tbl->current_rate,
1335 search_tbl, index, is_green);
1336 goto out;
1337 }
1338 tbl->action++;
1339 if (tbl->action > IWL_SISO_SWITCH_GI)
1340 tbl->action = IWL_SISO_SWITCH_ANTENNA;
1341
1342 if (tbl->action == start_action)
1343 break;
1344 }
1345 return 0;
1346
1347 out:
1348 tbl->action++;
1349 if (tbl->action > IWL_SISO_SWITCH_GI)
1350 tbl->action = IWL_SISO_SWITCH_ANTENNA;
1351 return 0;
1352}
1353
Ben Cahill77626352007-11-29 11:09:44 +08001354/*
1355 * Try to switch to new modulation mode from MIMO
1356 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001357static int rs_move_mimo_to_other(struct iwl4965_priv *priv,
1358 struct iwl4965_rate_scale_priv *lq_data,
Zhu Yib481de92007-09-25 17:54:57 -07001359 int index)
1360{
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001361 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001362 s8 is_green = lq_data->is_green;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001363 struct iwl4965_scale_tbl_info *tbl =
Zhu Yib481de92007-09-25 17:54:57 -07001364 &(lq_data->lq_info[lq_data->active_tbl]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001365 struct iwl4965_scale_tbl_info *search_tbl =
Zhu Yib481de92007-09-25 17:54:57 -07001366 &(lq_data->lq_info[(1 - lq_data->active_tbl)]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001367 u32 sz = (sizeof(struct iwl4965_scale_tbl_info) -
1368 (sizeof(struct iwl4965_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001369 u8 start_action = tbl->action;
1370
1371 for (;;) {
1372 lq_data->action_counter++;
1373 switch (tbl->action) {
1374 case IWL_MIMO_SWITCH_ANTENNA_A:
1375 case IWL_MIMO_SWITCH_ANTENNA_B:
1376 IWL_DEBUG_HT("LQ: MIMO SWITCH TO SISO\n");
Ben Cahill77626352007-11-29 11:09:44 +08001377
1378 /* Set up new search table for SISO */
Zhu Yib481de92007-09-25 17:54:57 -07001379 memcpy(search_tbl, tbl, sz);
1380 search_tbl->lq_type = LQ_SISO;
1381 search_tbl->is_SGI = 0;
1382 search_tbl->is_fat = 0;
1383 if (tbl->action == IWL_MIMO_SWITCH_ANTENNA_A)
1384 search_tbl->antenna_type = ANT_MAIN;
1385 else
1386 search_tbl->antenna_type = ANT_AUX;
1387
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001388 ret = rs_switch_to_siso(priv, lq_data, search_tbl,
Zhu Yib481de92007-09-25 17:54:57 -07001389 index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001390 if (!ret) {
Zhu Yib481de92007-09-25 17:54:57 -07001391 lq_data->search_better_tbl = 1;
1392 goto out;
1393 }
1394 break;
1395
1396 case IWL_MIMO_SWITCH_GI:
1397 IWL_DEBUG_HT("LQ: MIMO SWITCH TO GI\n");
Ben Cahill77626352007-11-29 11:09:44 +08001398
1399 /* Set up new search table for MIMO */
Zhu Yib481de92007-09-25 17:54:57 -07001400 memcpy(search_tbl, tbl, sz);
1401 search_tbl->lq_type = LQ_MIMO;
1402 search_tbl->antenna_type = ANT_BOTH;
1403 search_tbl->action = 0;
1404 if (search_tbl->is_SGI)
1405 search_tbl->is_SGI = 0;
1406 else
1407 search_tbl->is_SGI = 1;
1408 lq_data->search_better_tbl = 1;
Ben Cahill77626352007-11-29 11:09:44 +08001409
1410 /*
1411 * If active table already uses the fastest possible
1412 * modulation (dual stream with short guard interval),
1413 * and it's working well, there's no need to look
1414 * for a better type of modulation!
1415 */
Zhu Yib481de92007-09-25 17:54:57 -07001416 if ((tbl->lq_type == LQ_MIMO) &&
1417 (tbl->is_SGI)) {
1418 s32 tpt = lq_data->last_tpt / 100;
1419 if (((!tbl->is_fat) &&
1420 (tpt >= expected_tpt_mimo20MHz[index])) ||
1421 ((tbl->is_fat) &&
1422 (tpt >= expected_tpt_mimo40MHz[index])))
1423 lq_data->search_better_tbl = 0;
1424 }
1425 rs_get_expected_tpt_table(lq_data, search_tbl);
1426 rs_mcs_from_tbl(&search_tbl->current_rate,
1427 search_tbl, index, is_green);
1428 goto out;
1429
1430 }
1431 tbl->action++;
1432 if (tbl->action > IWL_MIMO_SWITCH_GI)
1433 tbl->action = IWL_MIMO_SWITCH_ANTENNA_A;
1434
1435 if (tbl->action == start_action)
1436 break;
1437 }
1438
1439 return 0;
1440 out:
1441 tbl->action++;
1442 if (tbl->action > IWL_MIMO_SWITCH_GI)
1443 tbl->action = IWL_MIMO_SWITCH_ANTENNA_A;
1444 return 0;
1445
1446}
1447
Ben Cahill77626352007-11-29 11:09:44 +08001448/*
1449 * Check whether we should continue using same modulation mode, or
1450 * begin search for a new mode, based on:
1451 * 1) # tx successes or failures while using this mode
1452 * 2) # times calling this function
1453 * 3) elapsed time in this mode (not used, for now)
1454 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001455static void rs_stay_in_table(struct iwl4965_rate_scale_priv *lq_data)
Zhu Yib481de92007-09-25 17:54:57 -07001456{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001457 struct iwl4965_scale_tbl_info *tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001458 int i;
1459 int active_tbl;
1460 int flush_interval_passed = 0;
1461
1462 active_tbl = lq_data->active_tbl;
1463
1464 tbl = &(lq_data->lq_info[active_tbl]);
1465
Ben Cahill77626352007-11-29 11:09:44 +08001466 /* If we've been disallowing search, see if we should now allow it */
Zhu Yib481de92007-09-25 17:54:57 -07001467 if (lq_data->stay_in_tbl) {
1468
Ben Cahill77626352007-11-29 11:09:44 +08001469 /* Elapsed time using current modulation mode */
Zhu Yib481de92007-09-25 17:54:57 -07001470 if (lq_data->flush_timer)
1471 flush_interval_passed =
1472 time_after(jiffies,
1473 (unsigned long)(lq_data->flush_timer +
1474 IWL_RATE_SCALE_FLUSH_INTVL));
1475
Ben Cahill77626352007-11-29 11:09:44 +08001476 /* For now, disable the elapsed time criterion */
Zhu Yib481de92007-09-25 17:54:57 -07001477 flush_interval_passed = 0;
Ben Cahill77626352007-11-29 11:09:44 +08001478
1479 /*
1480 * Check if we should allow search for new modulation mode.
1481 * If many frames have failed or succeeded, or we've used
1482 * this same modulation for a long time, allow search, and
1483 * reset history stats that keep track of whether we should
1484 * allow a new search. Also (below) reset all bitmaps and
1485 * stats in active history.
1486 */
Zhu Yib481de92007-09-25 17:54:57 -07001487 if ((lq_data->total_failed > lq_data->max_failure_limit) ||
1488 (lq_data->total_success > lq_data->max_success_limit) ||
1489 ((!lq_data->search_better_tbl) && (lq_data->flush_timer)
1490 && (flush_interval_passed))) {
1491 IWL_DEBUG_HT("LQ: stay is expired %d %d %d\n:",
1492 lq_data->total_failed,
1493 lq_data->total_success,
1494 flush_interval_passed);
Ben Cahill77626352007-11-29 11:09:44 +08001495
1496 /* Allow search for new mode */
1497 lq_data->stay_in_tbl = 0; /* only place reset */
Zhu Yib481de92007-09-25 17:54:57 -07001498 lq_data->total_failed = 0;
1499 lq_data->total_success = 0;
1500 lq_data->flush_timer = 0;
Ben Cahill77626352007-11-29 11:09:44 +08001501
1502 /*
1503 * Else if we've used this modulation mode enough repetitions
1504 * (regardless of elapsed time or success/failure), reset
1505 * history bitmaps and rate-specific stats for all rates in
1506 * active table.
1507 */
Mohamed Abbas403ab562007-11-06 22:06:25 -08001508 } else {
Zhu Yib481de92007-09-25 17:54:57 -07001509 lq_data->table_count++;
1510 if (lq_data->table_count >=
1511 lq_data->table_count_limit) {
1512 lq_data->table_count = 0;
1513
1514 IWL_DEBUG_HT("LQ: stay in table clear win\n");
1515 for (i = 0; i < IWL_RATE_COUNT; i++)
1516 rs_rate_scale_clear_window(
1517 &(tbl->win[i]));
1518 }
1519 }
1520
Ben Cahill77626352007-11-29 11:09:44 +08001521 /* If transitioning to allow "search", reset all history
1522 * bitmaps and stats in active table (this will become the new
1523 * "search" table). */
Zhu Yib481de92007-09-25 17:54:57 -07001524 if (!lq_data->stay_in_tbl) {
1525 for (i = 0; i < IWL_RATE_COUNT; i++)
1526 rs_rate_scale_clear_window(&(tbl->win[i]));
1527 }
1528 }
1529}
1530
Ben Cahill77626352007-11-29 11:09:44 +08001531/*
1532 * Do rate scaling and search for new modulation mode.
1533 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001534static void rs_rate_scale_perform(struct iwl4965_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001535 struct net_device *dev,
1536 struct ieee80211_hdr *hdr,
1537 struct sta_info *sta)
1538{
1539 int low = IWL_RATE_INVALID;
1540 int high = IWL_RATE_INVALID;
1541 int index;
1542 int i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001543 struct iwl4965_rate_scale_data *window = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001544 int current_tpt = IWL_INVALID_VALUE;
1545 int low_tpt = IWL_INVALID_VALUE;
1546 int high_tpt = IWL_INVALID_VALUE;
1547 u32 fail_count;
1548 s8 scale_action = 0;
1549 u16 fc, rate_mask;
1550 u8 update_lq = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001551 struct iwl4965_rate_scale_priv *lq_data;
1552 struct iwl4965_scale_tbl_info *tbl, *tbl1;
Zhu Yib481de92007-09-25 17:54:57 -07001553 u16 rate_scale_index_msk = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001554 struct iwl4965_rate mcs_rate;
Zhu Yib481de92007-09-25 17:54:57 -07001555 u8 is_green = 0;
1556 u8 active_tbl = 0;
1557 u8 done_search = 0;
1558 u16 high_low;
1559
1560 IWL_DEBUG_RATE("rate scale calculate new rate for skb\n");
1561
1562 fc = le16_to_cpu(hdr->frame_control);
1563 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1)) {
1564 /* Send management frames and broadcast/multicast data using
1565 * lowest rate. */
1566 /* TODO: this could probably be improved.. */
1567 return;
1568 }
1569
1570 if (!sta || !sta->rate_ctrl_priv)
1571 return;
1572
1573 if (!priv->lq_mngr.lq_ready) {
1574 IWL_DEBUG_RATE("still rate scaling not ready\n");
1575 return;
1576 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001577 lq_data = (struct iwl4965_rate_scale_priv *)sta->rate_ctrl_priv;
Zhu Yib481de92007-09-25 17:54:57 -07001578
Ben Cahill77626352007-11-29 11:09:44 +08001579 /*
1580 * Select rate-scale / modulation-mode table to work with in
1581 * the rest of this function: "search" if searching for better
1582 * modulation mode, or "active" if doing rate scaling within a mode.
1583 */
Zhu Yib481de92007-09-25 17:54:57 -07001584 if (!lq_data->search_better_tbl)
1585 active_tbl = lq_data->active_tbl;
1586 else
1587 active_tbl = 1 - lq_data->active_tbl;
1588
1589 tbl = &(lq_data->lq_info[active_tbl]);
1590 is_green = lq_data->is_green;
1591
Ben Cahill77626352007-11-29 11:09:44 +08001592 /* current tx rate */
Zhu Yib481de92007-09-25 17:54:57 -07001593 index = sta->last_txrate;
1594
1595 IWL_DEBUG_RATE("Rate scale index %d for type %d\n", index,
1596 tbl->lq_type);
1597
Ben Cahill77626352007-11-29 11:09:44 +08001598 /* rates available for this association, and for modulation mode */
Zhu Yib481de92007-09-25 17:54:57 -07001599 rs_get_supported_rates(lq_data, hdr, tbl->lq_type,
1600 &rate_mask);
1601
1602 IWL_DEBUG_RATE("mask 0x%04X \n", rate_mask);
1603
1604 /* mask with station rate restriction */
1605 if (is_legacy(tbl->lq_type)) {
1606 if (lq_data->phymode == (u8) MODE_IEEE80211A)
Ben Cahill77626352007-11-29 11:09:44 +08001607 /* supp_rates has no CCK bits in A mode */
Zhu Yib481de92007-09-25 17:54:57 -07001608 rate_scale_index_msk = (u16) (rate_mask &
Zhu Yi02dede02007-09-27 11:27:40 +08001609 (lq_data->supp_rates << IWL_FIRST_OFDM_RATE));
Zhu Yib481de92007-09-25 17:54:57 -07001610 else
1611 rate_scale_index_msk = (u16) (rate_mask &
Zhu Yi02dede02007-09-27 11:27:40 +08001612 lq_data->supp_rates);
Zhu Yib481de92007-09-25 17:54:57 -07001613
1614 } else
1615 rate_scale_index_msk = rate_mask;
1616
1617 if (!rate_scale_index_msk)
1618 rate_scale_index_msk = rate_mask;
1619
Ben Cahill77626352007-11-29 11:09:44 +08001620 /* If current rate is no longer supported on current association,
1621 * or user changed preferences for rates, find a new supported rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001622 if (index < 0 || !((1 << index) & rate_scale_index_msk)) {
1623 index = IWL_INVALID_VALUE;
1624 update_lq = 1;
1625
Ben Cahill77626352007-11-29 11:09:44 +08001626 /* get the highest available rate */
Zhu Yib481de92007-09-25 17:54:57 -07001627 for (i = 0; i <= IWL_RATE_COUNT; i++) {
1628 if ((1 << i) & rate_scale_index_msk)
1629 index = i;
1630 }
1631
1632 if (index == IWL_INVALID_VALUE) {
1633 IWL_WARNING("Can not find a suitable rate\n");
1634 return;
1635 }
1636 }
1637
Ben Cahill77626352007-11-29 11:09:44 +08001638 /* Get expected throughput table and history window for current rate */
Zhu Yib481de92007-09-25 17:54:57 -07001639 if (!tbl->expected_tpt)
1640 rs_get_expected_tpt_table(lq_data, tbl);
1641
1642 window = &(tbl->win[index]);
1643
Ben Cahill77626352007-11-29 11:09:44 +08001644 /*
1645 * If there is not enough history to calculate actual average
1646 * throughput, keep analyzing results of more tx frames, without
1647 * changing rate or mode (bypass most of the rest of this function).
1648 * Set up new rate table in uCode only if old rate is not supported
1649 * in current association (use new rate found above).
1650 */
Zhu Yib481de92007-09-25 17:54:57 -07001651 fail_count = window->counter - window->success_counter;
1652 if (((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
1653 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH))
1654 || (tbl->expected_tpt == NULL)) {
1655 IWL_DEBUG_RATE("LQ: still below TH succ %d total %d "
1656 "for index %d\n",
1657 window->success_counter, window->counter, index);
Ben Cahill77626352007-11-29 11:09:44 +08001658
1659 /* Can't calculate this yet; not enough history */
Zhu Yib481de92007-09-25 17:54:57 -07001660 window->average_tpt = IWL_INVALID_VALUE;
Ben Cahill77626352007-11-29 11:09:44 +08001661
1662 /* Should we stay with this modulation mode,
1663 * or search for a new one? */
Zhu Yib481de92007-09-25 17:54:57 -07001664 rs_stay_in_table(lq_data);
Ben Cahill77626352007-11-29 11:09:44 +08001665
1666 /* Set up new rate table in uCode, if needed */
Zhu Yib481de92007-09-25 17:54:57 -07001667 if (update_lq) {
1668 rs_mcs_from_tbl(&mcs_rate, tbl, index, is_green);
Zhu Yi02dede02007-09-27 11:27:40 +08001669 rs_fill_link_cmd(lq_data, &mcs_rate, &lq_data->lq);
Zhu Yib481de92007-09-25 17:54:57 -07001670 rs_send_lq_cmd(priv, &lq_data->lq, CMD_ASYNC);
1671 }
1672 goto out;
1673
Ben Cahill77626352007-11-29 11:09:44 +08001674 /* Else we have enough samples; calculate estimate of
1675 * actual average throughput */
Zhu Yib481de92007-09-25 17:54:57 -07001676 } else
1677 window->average_tpt = ((window->success_ratio *
1678 tbl->expected_tpt[index] + 64) / 128);
1679
Ben Cahill77626352007-11-29 11:09:44 +08001680 /* If we are searching for better modulation mode, check success. */
Zhu Yib481de92007-09-25 17:54:57 -07001681 if (lq_data->search_better_tbl) {
1682 int success_limit = IWL_RATE_SCALE_SWITCH;
1683
Ben Cahill77626352007-11-29 11:09:44 +08001684 /* If good success, continue using the "search" mode;
1685 * no need to send new link quality command, since we're
1686 * continuing to use the setup that we've been trying. */
Zhu Yib481de92007-09-25 17:54:57 -07001687 if ((window->success_ratio > success_limit) ||
1688 (window->average_tpt > lq_data->last_tpt)) {
1689 if (!is_legacy(tbl->lq_type)) {
1690 IWL_DEBUG_HT("LQ: we are switching to HT"
1691 " rate suc %d current tpt %d"
1692 " old tpt %d\n",
1693 window->success_ratio,
1694 window->average_tpt,
1695 lq_data->last_tpt);
1696 lq_data->enable_counter = 1;
1697 }
Ben Cahill77626352007-11-29 11:09:44 +08001698 /* Swap tables; "search" becomes "active" */
Zhu Yib481de92007-09-25 17:54:57 -07001699 lq_data->active_tbl = active_tbl;
1700 current_tpt = window->average_tpt;
Ben Cahill77626352007-11-29 11:09:44 +08001701
1702 /* Else poor success; go back to mode in "active" table */
Zhu Yib481de92007-09-25 17:54:57 -07001703 } else {
Ben Cahill77626352007-11-29 11:09:44 +08001704 /* Nullify "search" table */
Zhu Yib481de92007-09-25 17:54:57 -07001705 tbl->lq_type = LQ_NONE;
Ben Cahill77626352007-11-29 11:09:44 +08001706
1707 /* Revert to "active" table */
Zhu Yib481de92007-09-25 17:54:57 -07001708 active_tbl = lq_data->active_tbl;
1709 tbl = &(lq_data->lq_info[active_tbl]);
1710
Ben Cahill77626352007-11-29 11:09:44 +08001711 /* Revert to "active" rate and throughput info */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001712 index = iwl4965_rate_index_from_plcp(
Zhu Yib481de92007-09-25 17:54:57 -07001713 tbl->current_rate.rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -07001714 current_tpt = lq_data->last_tpt;
Ben Cahill77626352007-11-29 11:09:44 +08001715
1716 /* Need to set up a new rate table in uCode */
1717 update_lq = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001718 IWL_DEBUG_HT("XXY GO BACK TO OLD TABLE\n");
1719 }
Ben Cahill77626352007-11-29 11:09:44 +08001720
1721 /* Either way, we've made a decision; modulation mode
1722 * search is done, allow rate adjustment next time. */
Zhu Yib481de92007-09-25 17:54:57 -07001723 lq_data->search_better_tbl = 0;
Ben Cahill77626352007-11-29 11:09:44 +08001724 done_search = 1; /* Don't switch modes below! */
Zhu Yib481de92007-09-25 17:54:57 -07001725 goto lq_update;
1726 }
1727
Ben Cahill77626352007-11-29 11:09:44 +08001728 /* (Else) not in search of better modulation mode, try for better
1729 * starting rate, while staying in this mode. */
Zhu Yib481de92007-09-25 17:54:57 -07001730 high_low = rs_get_adjacent_rate(index, rate_scale_index_msk,
1731 tbl->lq_type);
1732 low = high_low & 0xff;
1733 high = (high_low >> 8) & 0xff;
1734
Ben Cahill77626352007-11-29 11:09:44 +08001735 /* Collect measured throughputs for current and adjacent rates */
Zhu Yib481de92007-09-25 17:54:57 -07001736 current_tpt = window->average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07001737 if (low != IWL_RATE_INVALID)
1738 low_tpt = tbl->win[low].average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07001739 if (high != IWL_RATE_INVALID)
1740 high_tpt = tbl->win[high].average_tpt;
1741
Ben Cahill77626352007-11-29 11:09:44 +08001742 /* Assume rate increase */
Zhu Yib481de92007-09-25 17:54:57 -07001743 scale_action = 1;
1744
Ben Cahill77626352007-11-29 11:09:44 +08001745 /* Too many failures, decrease rate */
Zhu Yib481de92007-09-25 17:54:57 -07001746 if ((window->success_ratio <= IWL_RATE_DECREASE_TH) ||
1747 (current_tpt == 0)) {
1748 IWL_DEBUG_RATE("decrease rate because of low success_ratio\n");
1749 scale_action = -1;
Ben Cahill77626352007-11-29 11:09:44 +08001750
1751 /* No throughput measured yet for adjacent rates; try increase. */
Zhu Yib481de92007-09-25 17:54:57 -07001752 } else if ((low_tpt == IWL_INVALID_VALUE) &&
1753 (high_tpt == IWL_INVALID_VALUE))
1754 scale_action = 1;
Ben Cahill77626352007-11-29 11:09:44 +08001755
1756 /* Both adjacent throughputs are measured, but neither one has better
1757 * throughput; we're using the best rate, don't change it! */
Zhu Yib481de92007-09-25 17:54:57 -07001758 else if ((low_tpt != IWL_INVALID_VALUE) &&
1759 (high_tpt != IWL_INVALID_VALUE) &&
1760 (low_tpt < current_tpt) &&
1761 (high_tpt < current_tpt))
1762 scale_action = 0;
Ben Cahill77626352007-11-29 11:09:44 +08001763
1764 /* At least one adjacent rate's throughput is measured,
1765 * and may have better performance. */
Zhu Yib481de92007-09-25 17:54:57 -07001766 else {
Ben Cahill77626352007-11-29 11:09:44 +08001767 /* Higher adjacent rate's throughput is measured */
Zhu Yib481de92007-09-25 17:54:57 -07001768 if (high_tpt != IWL_INVALID_VALUE) {
Ben Cahill77626352007-11-29 11:09:44 +08001769 /* Higher rate has better throughput */
Zhu Yib481de92007-09-25 17:54:57 -07001770 if (high_tpt > current_tpt)
1771 scale_action = 1;
1772 else {
1773 IWL_DEBUG_RATE
1774 ("decrease rate because of high tpt\n");
1775 scale_action = -1;
1776 }
Ben Cahill77626352007-11-29 11:09:44 +08001777
1778 /* Lower adjacent rate's throughput is measured */
Zhu Yib481de92007-09-25 17:54:57 -07001779 } else if (low_tpt != IWL_INVALID_VALUE) {
Ben Cahill77626352007-11-29 11:09:44 +08001780 /* Lower rate has better throughput */
Zhu Yib481de92007-09-25 17:54:57 -07001781 if (low_tpt > current_tpt) {
1782 IWL_DEBUG_RATE
1783 ("decrease rate because of low tpt\n");
1784 scale_action = -1;
1785 } else
1786 scale_action = 1;
1787 }
1788 }
1789
Ben Cahill77626352007-11-29 11:09:44 +08001790 /* Sanity check; asked for decrease, but success rate or throughput
1791 * has been good at old rate. Don't change it. */
Zhu Yib481de92007-09-25 17:54:57 -07001792 if (scale_action == -1) {
1793 if ((low != IWL_RATE_INVALID) &&
1794 ((window->success_ratio > IWL_RATE_HIGH_TH) ||
1795 (current_tpt > (100 * tbl->expected_tpt[low]))))
1796 scale_action = 0;
Ben Cahill77626352007-11-29 11:09:44 +08001797
1798 /* Sanity check; asked for increase, but success rate has not been great
1799 * even at old rate, higher rate will be worse. Don't change it. */
Zhu Yib481de92007-09-25 17:54:57 -07001800 } else if ((scale_action == 1) &&
1801 (window->success_ratio < IWL_RATE_INCREASE_TH))
1802 scale_action = 0;
1803
1804 switch (scale_action) {
1805 case -1:
Ben Cahill77626352007-11-29 11:09:44 +08001806 /* Decrease starting rate, update uCode's rate table */
Zhu Yib481de92007-09-25 17:54:57 -07001807 if (low != IWL_RATE_INVALID) {
1808 update_lq = 1;
1809 index = low;
1810 }
1811 break;
1812 case 1:
Ben Cahill77626352007-11-29 11:09:44 +08001813 /* Increase starting rate, update uCode's rate table */
Zhu Yib481de92007-09-25 17:54:57 -07001814 if (high != IWL_RATE_INVALID) {
1815 update_lq = 1;
1816 index = high;
1817 }
1818
1819 break;
1820 case 0:
Ben Cahill77626352007-11-29 11:09:44 +08001821 /* No change */
Zhu Yib481de92007-09-25 17:54:57 -07001822 default:
1823 break;
1824 }
1825
1826 IWL_DEBUG_HT("choose rate scale index %d action %d low %d "
1827 "high %d type %d\n",
1828 index, scale_action, low, high, tbl->lq_type);
1829
1830 lq_update:
Ben Cahill77626352007-11-29 11:09:44 +08001831 /* Replace uCode's rate table for the destination station. */
Zhu Yib481de92007-09-25 17:54:57 -07001832 if (update_lq) {
1833 rs_mcs_from_tbl(&mcs_rate, tbl, index, is_green);
Zhu Yi02dede02007-09-27 11:27:40 +08001834 rs_fill_link_cmd(lq_data, &mcs_rate, &lq_data->lq);
Zhu Yib481de92007-09-25 17:54:57 -07001835 rs_send_lq_cmd(priv, &lq_data->lq, CMD_ASYNC);
1836 }
Ben Cahill77626352007-11-29 11:09:44 +08001837
1838 /* Should we stay with this modulation mode, or search for a new one? */
Zhu Yib481de92007-09-25 17:54:57 -07001839 rs_stay_in_table(lq_data);
1840
Ben Cahill77626352007-11-29 11:09:44 +08001841 /*
1842 * Search for new modulation mode if we're:
1843 * 1) Not changing rates right now
1844 * 2) Not just finishing up a search
1845 * 3) Allowing a new search
1846 */
Zhu Yib481de92007-09-25 17:54:57 -07001847 if (!update_lq && !done_search && !lq_data->stay_in_tbl) {
Ben Cahill77626352007-11-29 11:09:44 +08001848 /* Save current throughput to compare with "search" throughput*/
Zhu Yib481de92007-09-25 17:54:57 -07001849 lq_data->last_tpt = current_tpt;
1850
Ben Cahill77626352007-11-29 11:09:44 +08001851 /* Select a new "search" modulation mode to try.
1852 * If one is found, set up the new "search" table. */
Zhu Yib481de92007-09-25 17:54:57 -07001853 if (is_legacy(tbl->lq_type))
1854 rs_move_legacy_other(priv, lq_data, index);
1855 else if (is_siso(tbl->lq_type))
1856 rs_move_siso_to_other(priv, lq_data, index);
1857 else
1858 rs_move_mimo_to_other(priv, lq_data, index);
1859
Ben Cahill77626352007-11-29 11:09:44 +08001860 /* If new "search" mode was selected, set up in uCode table */
Zhu Yib481de92007-09-25 17:54:57 -07001861 if (lq_data->search_better_tbl) {
Ben Cahill77626352007-11-29 11:09:44 +08001862 /* Access the "search" table, clear its history. */
Zhu Yib481de92007-09-25 17:54:57 -07001863 tbl = &(lq_data->lq_info[(1 - lq_data->active_tbl)]);
1864 for (i = 0; i < IWL_RATE_COUNT; i++)
1865 rs_rate_scale_clear_window(&(tbl->win[i]));
1866
Ben Cahill77626352007-11-29 11:09:44 +08001867 /* Use new "search" start rate */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001868 index = iwl4965_rate_index_from_plcp(
Zhu Yib481de92007-09-25 17:54:57 -07001869 tbl->current_rate.rate_n_flags);
1870
1871 IWL_DEBUG_HT("Switch current mcs: %X index: %d\n",
1872 tbl->current_rate.rate_n_flags, index);
1873 rs_fill_link_cmd(lq_data, &tbl->current_rate,
Zhu Yi02dede02007-09-27 11:27:40 +08001874 &lq_data->lq);
Zhu Yib481de92007-09-25 17:54:57 -07001875 rs_send_lq_cmd(priv, &lq_data->lq, CMD_ASYNC);
1876 }
Zhu Yib481de92007-09-25 17:54:57 -07001877
Ben Cahill77626352007-11-29 11:09:44 +08001878 /* If the "active" (non-search) mode was legacy,
1879 * and we've tried switching antennas,
1880 * but we haven't been able to try HT modes (not available),
1881 * stay with best antenna legacy modulation for a while
1882 * before next round of mode comparisons. */
1883 tbl1 = &(lq_data->lq_info[lq_data->active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07001884 if (is_legacy(tbl1->lq_type) &&
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001885#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07001886 !priv->current_assoc_ht.is_ht &&
1887#endif
1888 (lq_data->action_counter >= 1)) {
1889 lq_data->action_counter = 0;
1890 IWL_DEBUG_HT("LQ: STAY in legacy table\n");
1891 rs_set_stay_in_table(1, lq_data);
1892 }
1893
Ben Cahill77626352007-11-29 11:09:44 +08001894 /* If we're in an HT mode, and all 3 mode switch actions
1895 * have been tried and compared, stay in this best modulation
1896 * mode for a while before next round of mode comparisons. */
Zhu Yib481de92007-09-25 17:54:57 -07001897 if (lq_data->enable_counter &&
1898 (lq_data->action_counter >= IWL_ACTION_LIMIT)) {
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001899#ifdef CONFIG_IWL4965_HT_AGG
Ben Cahill77626352007-11-29 11:09:44 +08001900 /* If appropriate, set up aggregation! */
Zhu Yib481de92007-09-25 17:54:57 -07001901 if ((lq_data->last_tpt > TID_AGG_TPT_THREHOLD) &&
1902 (priv->lq_mngr.agg_ctrl.auto_agg)) {
1903 priv->lq_mngr.agg_ctrl.tid_retry =
1904 TID_ALL_SPECIFIED;
1905 schedule_work(&priv->agg_work);
1906 }
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001907#endif /*CONFIG_IWL4965_HT_AGG */
Zhu Yib481de92007-09-25 17:54:57 -07001908 lq_data->action_counter = 0;
1909 rs_set_stay_in_table(0, lq_data);
1910 }
Ben Cahill77626352007-11-29 11:09:44 +08001911
1912 /*
1913 * Else, don't search for a new modulation mode.
1914 * Put new timestamp in stay-in-modulation-mode flush timer if:
1915 * 1) Not changing rates right now
1916 * 2) Not just finishing up a search
1917 * 3) flush timer is empty
1918 */
Zhu Yib481de92007-09-25 17:54:57 -07001919 } else {
1920 if ((!update_lq) && (!done_search) && (!lq_data->flush_timer))
1921 lq_data->flush_timer = jiffies;
1922 }
1923
1924out:
1925 rs_mcs_from_tbl(&tbl->current_rate, tbl, index, is_green);
1926 i = index;
1927 sta->last_txrate = i;
1928
1929 /* sta->txrate is an index to A mode rates which start
1930 * at IWL_FIRST_OFDM_RATE
1931 */
1932 if (lq_data->phymode == (u8) MODE_IEEE80211A)
1933 sta->txrate = i - IWL_FIRST_OFDM_RATE;
1934 else
1935 sta->txrate = i;
1936
1937 return;
1938}
1939
1940
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001941static void rs_initialize_lq(struct iwl4965_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001942 struct sta_info *sta)
1943{
1944 int i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001945 struct iwl4965_rate_scale_priv *lq;
1946 struct iwl4965_scale_tbl_info *tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001947 u8 active_tbl = 0;
1948 int rate_idx;
1949 u8 use_green = rs_use_green(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001950 struct iwl4965_rate mcs_rate;
Zhu Yib481de92007-09-25 17:54:57 -07001951
1952 if (!sta || !sta->rate_ctrl_priv)
1953 goto out;
1954
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001955 lq = (struct iwl4965_rate_scale_priv *)sta->rate_ctrl_priv;
Zhu Yib481de92007-09-25 17:54:57 -07001956 i = sta->last_txrate;
1957
1958 if ((lq->lq.sta_id == 0xff) &&
1959 (priv->iw_mode == IEEE80211_IF_TYPE_IBSS))
1960 goto out;
1961
1962 if (!lq->search_better_tbl)
1963 active_tbl = lq->active_tbl;
1964 else
1965 active_tbl = 1 - lq->active_tbl;
1966
1967 tbl = &(lq->lq_info[active_tbl]);
1968
1969 if ((i < 0) || (i >= IWL_RATE_COUNT))
1970 i = 0;
1971
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001972 mcs_rate.rate_n_flags = iwl4965_rates[i].plcp ;
Zhu Yib481de92007-09-25 17:54:57 -07001973 mcs_rate.rate_n_flags |= RATE_MCS_ANT_B_MSK;
1974 mcs_rate.rate_n_flags &= ~RATE_MCS_ANT_A_MSK;
1975
1976 if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
1977 mcs_rate.rate_n_flags |= RATE_MCS_CCK_MSK;
1978
1979 tbl->antenna_type = ANT_AUX;
1980 rs_get_tbl_info_from_mcs(&mcs_rate, priv->phymode, tbl, &rate_idx);
1981 if (!rs_is_ant_connected(priv->valid_antenna, tbl->antenna_type))
Zhu Yi46640a82007-09-27 11:27:34 +08001982 rs_toggle_antenna(&mcs_rate, tbl);
Zhu Yib481de92007-09-25 17:54:57 -07001983
1984 rs_mcs_from_tbl(&mcs_rate, tbl, rate_idx, use_green);
1985 tbl->current_rate.rate_n_flags = mcs_rate.rate_n_flags;
1986 rs_get_expected_tpt_table(lq, tbl);
Zhu Yi02dede02007-09-27 11:27:40 +08001987 rs_fill_link_cmd(lq, &mcs_rate, &lq->lq);
Zhu Yib481de92007-09-25 17:54:57 -07001988 rs_send_lq_cmd(priv, &lq->lq, CMD_ASYNC);
1989 out:
1990 return;
1991}
1992
Mattias Nissler1abbe492007-12-20 13:50:07 +01001993static void rs_get_rate(void *priv_rate, struct net_device *dev,
1994 struct ieee80211_hw_mode *mode, struct sk_buff *skb,
1995 struct rate_selection *sel)
Zhu Yib481de92007-09-25 17:54:57 -07001996{
1997
1998 int i;
1999 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2000 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2001 struct sta_info *sta;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002002 struct iwl4965_priv *priv = (struct iwl4965_priv *)priv_rate;
2003 struct iwl4965_rate_scale_priv *lq;
Zhu Yib481de92007-09-25 17:54:57 -07002004
Zhu Yi58826352007-09-27 11:27:39 +08002005 IWL_DEBUG_RATE_LIMIT("rate scale calculate new rate for skb\n");
Zhu Yib481de92007-09-25 17:54:57 -07002006
Zhu Yib481de92007-09-25 17:54:57 -07002007 sta = sta_info_get(local, hdr->addr1);
2008
2009 if (!sta || !sta->rate_ctrl_priv) {
Mattias Nissler1abbe492007-12-20 13:50:07 +01002010 sel->rate = rate_lowest(local, local->oper_hw_mode, sta);
Zhu Yib481de92007-09-25 17:54:57 -07002011 if (sta)
2012 sta_info_put(sta);
Mattias Nissler1abbe492007-12-20 13:50:07 +01002013 return;
Zhu Yib481de92007-09-25 17:54:57 -07002014 }
2015
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002016 lq = (struct iwl4965_rate_scale_priv *)sta->rate_ctrl_priv;
Zhu Yib481de92007-09-25 17:54:57 -07002017 i = sta->last_txrate;
2018
2019 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) && !lq->ibss_sta_added) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002020 u8 sta_id = iwl4965_hw_find_station(priv, hdr->addr1);
Joe Perches0795af52007-10-03 17:59:30 -07002021 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07002022
2023 if (sta_id == IWL_INVALID_STATION) {
Joe Perches0795af52007-10-03 17:59:30 -07002024 IWL_DEBUG_RATE("LQ: ADD station %s\n",
2025 print_mac(mac, hdr->addr1));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002026 sta_id = iwl4965_add_station_flags(priv,
Zhu Yib481de92007-09-25 17:54:57 -07002027 hdr->addr1, 0, CMD_ASYNC);
2028 }
2029 if ((sta_id != IWL_INVALID_STATION)) {
2030 lq->lq.sta_id = sta_id;
2031 lq->lq.rs_table[0].rate_n_flags = 0;
2032 lq->ibss_sta_added = 1;
2033 rs_initialize_lq(priv, sta);
2034 }
2035 if (!lq->ibss_sta_added)
2036 goto done;
2037 }
2038
2039 done:
Mattias Nissler1abbe492007-12-20 13:50:07 +01002040 if ((i < 0) || (i > IWL_RATE_COUNT)) {
2041 sel->rate = rate_lowest(local, local->oper_hw_mode, sta);
2042 return;
2043 }
Zhu Yib481de92007-09-25 17:54:57 -07002044 sta_info_put(sta);
Zhu Yib481de92007-09-25 17:54:57 -07002045
Mattias Nissler1abbe492007-12-20 13:50:07 +01002046 sel->rate = &priv->ieee_rates[i];
Zhu Yib481de92007-09-25 17:54:57 -07002047}
2048
2049static void *rs_alloc_sta(void *priv, gfp_t gfp)
2050{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002051 struct iwl4965_rate_scale_priv *crl;
Zhu Yib481de92007-09-25 17:54:57 -07002052 int i, j;
2053
2054 IWL_DEBUG_RATE("create station rate scale window\n");
2055
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002056 crl = kzalloc(sizeof(struct iwl4965_rate_scale_priv), gfp);
Zhu Yib481de92007-09-25 17:54:57 -07002057
2058 if (crl == NULL)
2059 return NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002060 crl->lq.sta_id = 0xff;
2061
Zhu Yi63fddb92007-09-27 11:27:36 +08002062
Zhu Yib481de92007-09-25 17:54:57 -07002063 for (j = 0; j < LQ_SIZE; j++)
2064 for (i = 0; i < IWL_RATE_COUNT; i++)
2065 rs_rate_scale_clear_window(&(crl->lq_info[j].win[i]));
2066
2067 return crl;
2068}
2069
2070static void rs_rate_init(void *priv_rate, void *priv_sta,
2071 struct ieee80211_local *local,
2072 struct sta_info *sta)
2073{
2074 int i, j;
2075 struct ieee80211_hw_mode *mode = local->oper_hw_mode;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002076 struct iwl4965_priv *priv = (struct iwl4965_priv *)priv_rate;
2077 struct iwl4965_rate_scale_priv *crl = priv_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002078
Zhu Yib481de92007-09-25 17:54:57 -07002079 crl->flush_timer = 0;
Zhu Yi02dede02007-09-27 11:27:40 +08002080 crl->supp_rates = sta->supp_rates;
Zhu Yib481de92007-09-25 17:54:57 -07002081 sta->txrate = 3;
2082 for (j = 0; j < LQ_SIZE; j++)
2083 for (i = 0; i < IWL_RATE_COUNT; i++)
2084 rs_rate_scale_clear_window(&(crl->lq_info[j].win[i]));
2085
2086 IWL_DEBUG_RATE("rate scale global init\n");
2087 /* TODO: what is a good starting rate for STA? About middle? Maybe not
2088 * the lowest or the highest rate.. Could consider using RSSI from
2089 * previous packets? Need to have IEEE 802.1X auth succeed immediately
2090 * after assoc.. */
2091
2092 crl->ibss_sta_added = 0;
2093 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002094 u8 sta_id = iwl4965_hw_find_station(priv, sta->addr);
Joe Perches0795af52007-10-03 17:59:30 -07002095 DECLARE_MAC_BUF(mac);
2096
Zhu Yib481de92007-09-25 17:54:57 -07002097 /* for IBSS the call are from tasklet */
Joe Perches0795af52007-10-03 17:59:30 -07002098 IWL_DEBUG_HT("LQ: ADD station %s\n",
2099 print_mac(mac, sta->addr));
Zhu Yib481de92007-09-25 17:54:57 -07002100
2101 if (sta_id == IWL_INVALID_STATION) {
Joe Perches0795af52007-10-03 17:59:30 -07002102 IWL_DEBUG_RATE("LQ: ADD station %s\n",
2103 print_mac(mac, sta->addr));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002104 sta_id = iwl4965_add_station_flags(priv,
Zhu Yib481de92007-09-25 17:54:57 -07002105 sta->addr, 0, CMD_ASYNC);
2106 }
2107 if ((sta_id != IWL_INVALID_STATION)) {
2108 crl->lq.sta_id = sta_id;
2109 crl->lq.rs_table[0].rate_n_flags = 0;
2110 }
2111 /* FIXME: this is w/a remove it later */
2112 priv->assoc_station_added = 1;
2113 }
2114
Ben Cahill77626352007-11-29 11:09:44 +08002115 /* Find highest tx rate supported by hardware and destination station */
Zhu Yib481de92007-09-25 17:54:57 -07002116 for (i = 0; i < mode->num_rates; i++) {
2117 if ((sta->supp_rates & BIT(i)) &&
2118 (mode->rates[i].flags & IEEE80211_RATE_SUPPORTED))
2119 sta->txrate = i;
2120 }
2121 sta->last_txrate = sta->txrate;
Ben Cahill77626352007-11-29 11:09:44 +08002122 /* For MODE_IEEE80211A, cck rates are at end of rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002123 if (local->hw.conf.phymode == MODE_IEEE80211A)
2124 sta->last_txrate += IWL_FIRST_OFDM_RATE;
2125
Ron Rindjunsky9e0cc6d2007-11-26 16:14:36 +02002126 crl->is_dup = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002127 crl->valid_antenna = priv->valid_antenna;
2128 crl->antenna = priv->antenna;
2129 crl->is_green = rs_use_green(priv);
2130 crl->active_rate = priv->active_rate;
2131 crl->active_rate &= ~(0x1000);
2132 crl->active_rate_basic = priv->active_rate_basic;
2133 crl->phymode = priv->phymode;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002134#ifdef CONFIG_IWL4965_HT
Ben Cahill77626352007-11-29 11:09:44 +08002135 /*
2136 * active_siso_rate mask includes 9 MBits (bit 5), and CCK (bits 0-3),
2137 * supp_rates[] does not; shift to convert format, force 9 MBits off.
2138 */
Zhu Yib481de92007-09-25 17:54:57 -07002139 crl->active_siso_rate = (priv->current_assoc_ht.supp_rates[0] << 1);
2140 crl->active_siso_rate |= (priv->current_assoc_ht.supp_rates[0] & 0x1);
2141 crl->active_siso_rate &= ~((u16)0x2);
2142 crl->active_siso_rate = crl->active_siso_rate << IWL_FIRST_OFDM_RATE;
2143
Ben Cahill77626352007-11-29 11:09:44 +08002144 /* Same here */
Zhu Yib481de92007-09-25 17:54:57 -07002145 crl->active_mimo_rate = (priv->current_assoc_ht.supp_rates[1] << 1);
2146 crl->active_mimo_rate |= (priv->current_assoc_ht.supp_rates[1] & 0x1);
2147 crl->active_mimo_rate &= ~((u16)0x2);
2148 crl->active_mimo_rate = crl->active_mimo_rate << IWL_FIRST_OFDM_RATE;
2149 IWL_DEBUG_HT("MIMO RATE 0x%X SISO MASK 0x%X\n", crl->active_siso_rate,
2150 crl->active_mimo_rate);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002151#endif /*CONFIG_IWL4965_HT*/
Zhu Yi98d7e092007-09-27 11:27:42 +08002152#ifdef CONFIG_MAC80211_DEBUGFS
2153 crl->drv = priv;
2154#endif
Zhu Yib481de92007-09-25 17:54:57 -07002155
2156 if (priv->assoc_station_added)
2157 priv->lq_mngr.lq_ready = 1;
2158
2159 rs_initialize_lq(priv, sta);
2160}
2161
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002162static void rs_fill_link_cmd(struct iwl4965_rate_scale_priv *lq_data,
2163 struct iwl4965_rate *tx_mcs,
2164 struct iwl4965_link_quality_cmd *lq_cmd)
Zhu Yib481de92007-09-25 17:54:57 -07002165{
2166 int index = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002167 int rate_idx;
Zhu Yi1b696de2007-09-27 11:27:41 +08002168 int repeat_rate = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002169 u8 ant_toggle_count = 0;
2170 u8 use_ht_possible = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002171 struct iwl4965_rate new_rate;
2172 struct iwl4965_scale_tbl_info tbl_type = { 0 };
Zhu Yib481de92007-09-25 17:54:57 -07002173
Ben Cahill77626352007-11-29 11:09:44 +08002174 /* Override starting rate (index 0) if needed for debug purposes */
Zhu Yi98d7e092007-09-27 11:27:42 +08002175 rs_dbgfs_set_mcs(lq_data, tx_mcs, index);
2176
Ben Cahill77626352007-11-29 11:09:44 +08002177 /* Interpret rate_n_flags */
Zhu Yib481de92007-09-25 17:54:57 -07002178 rs_get_tbl_info_from_mcs(tx_mcs, lq_data->phymode,
2179 &tbl_type, &rate_idx);
2180
Ben Cahill77626352007-11-29 11:09:44 +08002181 /* How many times should we repeat the initial rate? */
Zhu Yib481de92007-09-25 17:54:57 -07002182 if (is_legacy(tbl_type.lq_type)) {
2183 ant_toggle_count = 1;
Zhu Yi1b696de2007-09-27 11:27:41 +08002184 repeat_rate = IWL_NUMBER_TRY;
Zhu Yib481de92007-09-25 17:54:57 -07002185 } else
Zhu Yi1b696de2007-09-27 11:27:41 +08002186 repeat_rate = IWL_HT_NUMBER_TRY;
Zhu Yib481de92007-09-25 17:54:57 -07002187
2188 lq_cmd->general_params.mimo_delimiter =
2189 is_mimo(tbl_type.lq_type) ? 1 : 0;
Ben Cahill77626352007-11-29 11:09:44 +08002190
2191 /* Fill 1st table entry (index 0) */
Zhu Yib481de92007-09-25 17:54:57 -07002192 lq_cmd->rs_table[index].rate_n_flags =
2193 cpu_to_le32(tx_mcs->rate_n_flags);
2194 new_rate.rate_n_flags = tx_mcs->rate_n_flags;
2195
2196 if (is_mimo(tbl_type.lq_type) || (tbl_type.antenna_type == ANT_MAIN))
Ben Cahill77626352007-11-29 11:09:44 +08002197 lq_cmd->general_params.single_stream_ant_msk
2198 = LINK_QUAL_ANT_A_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002199 else
Ben Cahill77626352007-11-29 11:09:44 +08002200 lq_cmd->general_params.single_stream_ant_msk
2201 = LINK_QUAL_ANT_B_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002202
2203 index++;
Zhu Yi1b696de2007-09-27 11:27:41 +08002204 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002205
Ben Cahill77626352007-11-29 11:09:44 +08002206 /* Fill rest of rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002207 while (index < LINK_QUAL_MAX_RETRY_NUM) {
Ben Cahill77626352007-11-29 11:09:44 +08002208 /* Repeat initial/next rate.
2209 * For legacy IWL_NUMBER_TRY == 1, this loop will not execute.
2210 * For HT IWL_HT_NUMBER_TRY == 3, this executes twice. */
Zhu Yi1b696de2007-09-27 11:27:41 +08002211 while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
Zhu Yib481de92007-09-25 17:54:57 -07002212 if (is_legacy(tbl_type.lq_type)) {
2213 if (ant_toggle_count <
2214 NUM_TRY_BEFORE_ANTENNA_TOGGLE)
2215 ant_toggle_count++;
2216 else {
2217 rs_toggle_antenna(&new_rate, &tbl_type);
2218 ant_toggle_count = 1;
2219 }
2220 }
Zhu Yi98d7e092007-09-27 11:27:42 +08002221
Ben Cahill77626352007-11-29 11:09:44 +08002222 /* Override next rate if needed for debug purposes */
Zhu Yi98d7e092007-09-27 11:27:42 +08002223 rs_dbgfs_set_mcs(lq_data, &new_rate, index);
Ben Cahill77626352007-11-29 11:09:44 +08002224
2225 /* Fill next table entry */
Zhu Yib481de92007-09-25 17:54:57 -07002226 lq_cmd->rs_table[index].rate_n_flags =
2227 cpu_to_le32(new_rate.rate_n_flags);
Zhu Yi1b696de2007-09-27 11:27:41 +08002228 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002229 index++;
2230 }
2231
2232 rs_get_tbl_info_from_mcs(&new_rate, lq_data->phymode, &tbl_type,
2233 &rate_idx);
2234
Ben Cahill77626352007-11-29 11:09:44 +08002235 /* Indicate to uCode which entries might be MIMO.
2236 * If initial rate was MIMO, this will finally end up
2237 * as (IWL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
Zhu Yib481de92007-09-25 17:54:57 -07002238 if (is_mimo(tbl_type.lq_type))
2239 lq_cmd->general_params.mimo_delimiter = index;
2240
Ben Cahill77626352007-11-29 11:09:44 +08002241 /* Get next rate */
Zhu Yib481de92007-09-25 17:54:57 -07002242 rs_get_lower_rate(lq_data, &tbl_type, rate_idx,
Zhu Yi02dede02007-09-27 11:27:40 +08002243 use_ht_possible, &new_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002244
Ben Cahill77626352007-11-29 11:09:44 +08002245 /* How many times should we repeat the next rate? */
Zhu Yib481de92007-09-25 17:54:57 -07002246 if (is_legacy(tbl_type.lq_type)) {
2247 if (ant_toggle_count < NUM_TRY_BEFORE_ANTENNA_TOGGLE)
2248 ant_toggle_count++;
2249 else {
2250 rs_toggle_antenna(&new_rate, &tbl_type);
2251 ant_toggle_count = 1;
2252 }
Zhu Yi1b696de2007-09-27 11:27:41 +08002253 repeat_rate = IWL_NUMBER_TRY;
Zhu Yib481de92007-09-25 17:54:57 -07002254 } else
Zhu Yi1b696de2007-09-27 11:27:41 +08002255 repeat_rate = IWL_HT_NUMBER_TRY;
Zhu Yib481de92007-09-25 17:54:57 -07002256
Ben Cahill77626352007-11-29 11:09:44 +08002257 /* Don't allow HT rates after next pass.
2258 * rs_get_lower_rate() will change type to LQ_A or LQ_G. */
Zhu Yib481de92007-09-25 17:54:57 -07002259 use_ht_possible = 0;
2260
Ben Cahill77626352007-11-29 11:09:44 +08002261 /* Override next rate if needed for debug purposes */
Zhu Yi98d7e092007-09-27 11:27:42 +08002262 rs_dbgfs_set_mcs(lq_data, &new_rate, index);
Ben Cahill77626352007-11-29 11:09:44 +08002263
2264 /* Fill next table entry */
Zhu Yib481de92007-09-25 17:54:57 -07002265 lq_cmd->rs_table[index].rate_n_flags =
2266 cpu_to_le32(new_rate.rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -07002267
2268 index++;
Zhu Yi1b696de2007-09-27 11:27:41 +08002269 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002270 }
2271
Zhu Yib481de92007-09-25 17:54:57 -07002272 lq_cmd->general_params.dual_stream_ant_msk = 3;
2273 lq_cmd->agg_params.agg_dis_start_th = 3;
2274 lq_cmd->agg_params.agg_time_limit = cpu_to_le16(4000);
Zhu Yib481de92007-09-25 17:54:57 -07002275}
2276
2277static void *rs_alloc(struct ieee80211_local *local)
2278{
2279 return local->hw.priv;
2280}
2281/* rate scale requires free function to be implemented */
2282static void rs_free(void *priv_rate)
2283{
2284 return;
2285}
2286
2287static void rs_clear(void *priv_rate)
2288{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002289 struct iwl4965_priv *priv = (struct iwl4965_priv *) priv_rate;
Zhu Yib481de92007-09-25 17:54:57 -07002290
2291 IWL_DEBUG_RATE("enter\n");
2292
2293 priv->lq_mngr.lq_ready = 0;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002294#ifdef CONFIG_IWL4965_HT
2295#ifdef CONFIG_IWL4965_HT_AGG
Zhu Yib481de92007-09-25 17:54:57 -07002296 if (priv->lq_mngr.agg_ctrl.granted_ba)
2297 iwl4965_turn_off_agg(priv, TID_ALL_SPECIFIED);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002298#endif /*CONFIG_IWL4965_HT_AGG */
2299#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07002300
2301 IWL_DEBUG_RATE("leave\n");
2302}
2303
2304static void rs_free_sta(void *priv, void *priv_sta)
2305{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002306 struct iwl4965_rate_scale_priv *rs_priv = priv_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002307
2308 IWL_DEBUG_RATE("enter\n");
2309 kfree(rs_priv);
2310 IWL_DEBUG_RATE("leave\n");
2311}
2312
2313
Zhu Yi93dc6462007-09-27 11:27:37 +08002314#ifdef CONFIG_MAC80211_DEBUGFS
Zhu Yi5ae212c2007-09-27 11:27:38 +08002315static int open_file_generic(struct inode *inode, struct file *file)
2316{
2317 file->private_data = inode->i_private;
2318 return 0;
2319}
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002320static void rs_dbgfs_set_mcs(struct iwl4965_rate_scale_priv *rs_priv,
2321 struct iwl4965_rate *mcs, int index)
Zhu Yi98d7e092007-09-27 11:27:42 +08002322{
Ron Rindjunsky4457e1a2007-10-15 14:40:56 +02002323 u32 base_rate;
2324
2325 if (rs_priv->phymode == (u8) MODE_IEEE80211A)
2326 base_rate = 0x800D;
2327 else
2328 base_rate = 0x820A;
2329
Zhu Yi98d7e092007-09-27 11:27:42 +08002330 if (rs_priv->dbg_fixed.rate_n_flags) {
2331 if (index < 12)
2332 mcs->rate_n_flags = rs_priv->dbg_fixed.rate_n_flags;
2333 else
Ron Rindjunsky4457e1a2007-10-15 14:40:56 +02002334 mcs->rate_n_flags = base_rate;
Zhu Yi98d7e092007-09-27 11:27:42 +08002335 IWL_DEBUG_RATE("Fixed rate ON\n");
2336 return;
2337 }
Zhu Yi5ae212c2007-09-27 11:27:38 +08002338
Zhu Yi98d7e092007-09-27 11:27:42 +08002339 IWL_DEBUG_RATE("Fixed rate OFF\n");
2340}
2341
2342static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
2343 const char __user *user_buf, size_t count, loff_t *ppos)
2344{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002345 struct iwl4965_rate_scale_priv *rs_priv = file->private_data;
Zhu Yi98d7e092007-09-27 11:27:42 +08002346 char buf[64];
2347 int buf_size;
2348 u32 parsed_rate;
2349
2350 memset(buf, 0, sizeof(buf));
2351 buf_size = min(count, sizeof(buf) - 1);
2352 if (copy_from_user(buf, user_buf, buf_size))
2353 return -EFAULT;
2354
2355 if (sscanf(buf, "%x", &parsed_rate) == 1)
2356 rs_priv->dbg_fixed.rate_n_flags = parsed_rate;
2357 else
2358 rs_priv->dbg_fixed.rate_n_flags = 0;
2359
Ben Cahill77626352007-11-29 11:09:44 +08002360 rs_priv->active_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
2361 rs_priv->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2362 rs_priv->active_mimo_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
Zhu Yi98d7e092007-09-27 11:27:42 +08002363
2364 IWL_DEBUG_RATE("sta_id %d rate 0x%X\n",
2365 rs_priv->lq.sta_id, rs_priv->dbg_fixed.rate_n_flags);
2366
2367 if (rs_priv->dbg_fixed.rate_n_flags) {
2368 rs_fill_link_cmd(rs_priv, &rs_priv->dbg_fixed, &rs_priv->lq);
2369 rs_send_lq_cmd(rs_priv->drv, &rs_priv->lq, CMD_ASYNC);
2370 }
2371
2372 return count;
2373}
Zhu Yi0209dc12007-09-27 11:27:43 +08002374
Zhu Yi5ae212c2007-09-27 11:27:38 +08002375static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
2376 char __user *user_buf, size_t count, loff_t *ppos)
2377{
2378 char buff[1024];
2379 int desc = 0;
2380 int i = 0;
2381
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002382 struct iwl4965_rate_scale_priv *rs_priv = file->private_data;
Zhu Yi5ae212c2007-09-27 11:27:38 +08002383
2384 desc += sprintf(buff+desc, "sta_id %d\n", rs_priv->lq.sta_id);
Zhu Yi98d7e092007-09-27 11:27:42 +08002385 desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
Zhu Yi5ae212c2007-09-27 11:27:38 +08002386 rs_priv->total_failed, rs_priv->total_success,
2387 rs_priv->active_rate);
Zhu Yi98d7e092007-09-27 11:27:42 +08002388 desc += sprintf(buff+desc, "fixed rate 0x%X\n",
2389 rs_priv->dbg_fixed.rate_n_flags);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002390 desc += sprintf(buff+desc, "general:"
2391 "flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
2392 rs_priv->lq.general_params.flags,
2393 rs_priv->lq.general_params.mimo_delimiter,
2394 rs_priv->lq.general_params.single_stream_ant_msk,
2395 rs_priv->lq.general_params.dual_stream_ant_msk);
2396
2397 desc += sprintf(buff+desc, "agg:"
2398 "time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
2399 le16_to_cpu(rs_priv->lq.agg_params.agg_time_limit),
2400 rs_priv->lq.agg_params.agg_dis_start_th,
2401 rs_priv->lq.agg_params.agg_frame_cnt_limit);
2402
2403 desc += sprintf(buff+desc,
2404 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
2405 rs_priv->lq.general_params.start_rate_index[0],
2406 rs_priv->lq.general_params.start_rate_index[1],
2407 rs_priv->lq.general_params.start_rate_index[2],
2408 rs_priv->lq.general_params.start_rate_index[3]);
2409
2410
2411 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
2412 desc += sprintf(buff+desc, " rate[%d] 0x%X\n",
2413 i, le32_to_cpu(rs_priv->lq.rs_table[i].rate_n_flags));
2414
2415 return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2416}
2417
2418static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
Zhu Yi98d7e092007-09-27 11:27:42 +08002419 .write = rs_sta_dbgfs_scale_table_write,
Zhu Yi5ae212c2007-09-27 11:27:38 +08002420 .read = rs_sta_dbgfs_scale_table_read,
2421 .open = open_file_generic,
2422};
Zhu Yi0209dc12007-09-27 11:27:43 +08002423static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
2424 char __user *user_buf, size_t count, loff_t *ppos)
2425{
2426 char buff[1024];
2427 int desc = 0;
2428 int i, j;
2429
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002430 struct iwl4965_rate_scale_priv *rs_priv = file->private_data;
Zhu Yi0209dc12007-09-27 11:27:43 +08002431 for (i = 0; i < LQ_SIZE; i++) {
2432 desc += sprintf(buff+desc, "%s type=%d SGI=%d FAT=%d DUP=%d\n"
2433 "rate=0x%X\n",
2434 rs_priv->active_tbl == i?"*":"x",
2435 rs_priv->lq_info[i].lq_type,
2436 rs_priv->lq_info[i].is_SGI,
2437 rs_priv->lq_info[i].is_fat,
2438 rs_priv->lq_info[i].is_dup,
2439 rs_priv->lq_info[i].current_rate.rate_n_flags);
2440 for (j = 0; j < IWL_RATE_COUNT; j++) {
2441 desc += sprintf(buff+desc,
2442 "counter=%d success=%d %%=%d\n",
2443 rs_priv->lq_info[i].win[j].counter,
2444 rs_priv->lq_info[i].win[j].success_counter,
2445 rs_priv->lq_info[i].win[j].success_ratio);
2446 }
2447 }
2448 return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2449}
2450
2451static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
2452 .read = rs_sta_dbgfs_stats_table_read,
2453 .open = open_file_generic,
2454};
Zhu Yi5ae212c2007-09-27 11:27:38 +08002455
Zhu Yi93dc6462007-09-27 11:27:37 +08002456static void rs_add_debugfs(void *priv, void *priv_sta,
2457 struct dentry *dir)
2458{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002459 struct iwl4965_rate_scale_priv *rs_priv = priv_sta;
Zhu Yi5ae212c2007-09-27 11:27:38 +08002460 rs_priv->rs_sta_dbgfs_scale_table_file =
Zhu Yi0209dc12007-09-27 11:27:43 +08002461 debugfs_create_file("rate_scale_table", 0600, dir,
Zhu Yi5ae212c2007-09-27 11:27:38 +08002462 rs_priv, &rs_sta_dbgfs_scale_table_ops);
Zhu Yi0209dc12007-09-27 11:27:43 +08002463 rs_priv->rs_sta_dbgfs_stats_table_file =
2464 debugfs_create_file("rate_stats_table", 0600, dir,
2465 rs_priv, &rs_sta_dbgfs_stats_table_ops);
Zhu Yi93dc6462007-09-27 11:27:37 +08002466}
2467
2468static void rs_remove_debugfs(void *priv, void *priv_sta)
2469{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002470 struct iwl4965_rate_scale_priv *rs_priv = priv_sta;
Zhu Yi5ae212c2007-09-27 11:27:38 +08002471 debugfs_remove(rs_priv->rs_sta_dbgfs_scale_table_file);
Zhu Yi0209dc12007-09-27 11:27:43 +08002472 debugfs_remove(rs_priv->rs_sta_dbgfs_stats_table_file);
Zhu Yi93dc6462007-09-27 11:27:37 +08002473}
2474#endif
2475
Zhu Yib481de92007-09-25 17:54:57 -07002476static struct rate_control_ops rs_ops = {
2477 .module = NULL,
2478 .name = RS_NAME,
2479 .tx_status = rs_tx_status,
2480 .get_rate = rs_get_rate,
2481 .rate_init = rs_rate_init,
2482 .clear = rs_clear,
2483 .alloc = rs_alloc,
2484 .free = rs_free,
2485 .alloc_sta = rs_alloc_sta,
2486 .free_sta = rs_free_sta,
Zhu Yi93dc6462007-09-27 11:27:37 +08002487#ifdef CONFIG_MAC80211_DEBUGFS
2488 .add_sta_debugfs = rs_add_debugfs,
2489 .remove_sta_debugfs = rs_remove_debugfs,
2490#endif
Zhu Yib481de92007-09-25 17:54:57 -07002491};
2492
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002493int iwl4965_fill_rs_info(struct ieee80211_hw *hw, char *buf, u8 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07002494{
2495 struct ieee80211_local *local = hw_to_local(hw);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002496 struct iwl4965_priv *priv = hw->priv;
2497 struct iwl4965_rate_scale_priv *rs_priv;
Zhu Yib481de92007-09-25 17:54:57 -07002498 struct sta_info *sta;
2499 int count = 0, i;
2500 u32 samples = 0, success = 0, good = 0;
2501 unsigned long now = jiffies;
2502 u32 max_time = 0;
2503 u8 lq_type, antenna;
2504
2505 sta = sta_info_get(local, priv->stations[sta_id].sta.sta.addr);
2506 if (!sta || !sta->rate_ctrl_priv) {
2507 if (sta) {
2508 sta_info_put(sta);
2509 IWL_DEBUG_RATE("leave - no private rate data!\n");
2510 } else
2511 IWL_DEBUG_RATE("leave - no station!\n");
2512 return sprintf(buf, "station %d not found\n", sta_id);
2513 }
2514
2515 rs_priv = (void *)sta->rate_ctrl_priv;
2516
2517 lq_type = rs_priv->lq_info[rs_priv->active_tbl].lq_type;
2518 antenna = rs_priv->lq_info[rs_priv->active_tbl].antenna_type;
2519
2520 if (is_legacy(lq_type))
2521 i = IWL_RATE_54M_INDEX;
2522 else
2523 i = IWL_RATE_60M_INDEX;
2524 while (1) {
2525 u64 mask;
2526 int j;
2527 int active = rs_priv->active_tbl;
2528
2529 count +=
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002530 sprintf(&buf[count], " %2dMbs: ", iwl4965_rates[i].ieee / 2);
Zhu Yib481de92007-09-25 17:54:57 -07002531
2532 mask = (1ULL << (IWL_RATE_MAX_WINDOW - 1));
2533 for (j = 0; j < IWL_RATE_MAX_WINDOW; j++, mask >>= 1)
2534 buf[count++] =
2535 (rs_priv->lq_info[active].win[i].data & mask)
2536 ? '1' : '0';
2537
2538 samples += rs_priv->lq_info[active].win[i].counter;
2539 good += rs_priv->lq_info[active].win[i].success_counter;
2540 success += rs_priv->lq_info[active].win[i].success_counter *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002541 iwl4965_rates[i].ieee;
Zhu Yib481de92007-09-25 17:54:57 -07002542
2543 if (rs_priv->lq_info[active].win[i].stamp) {
2544 int delta =
2545 jiffies_to_msecs(now -
2546 rs_priv->lq_info[active].win[i].stamp);
2547
2548 if (delta > max_time)
2549 max_time = delta;
2550
2551 count += sprintf(&buf[count], "%5dms\n", delta);
2552 } else
2553 buf[count++] = '\n';
2554
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002555 j = iwl4965_get_prev_ieee_rate(i);
Zhu Yib481de92007-09-25 17:54:57 -07002556 if (j == i)
2557 break;
2558 i = j;
2559 }
2560
2561 /* Display the average rate of all samples taken.
2562 *
Ben Cahill77626352007-11-29 11:09:44 +08002563 * NOTE: We multiply # of samples by 2 since the IEEE measurement
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002564 * added from iwl4965_rates is actually 2X the rate */
Zhu Yib481de92007-09-25 17:54:57 -07002565 if (samples)
2566 count += sprintf(&buf[count],
2567 "\nAverage rate is %3d.%02dMbs over last %4dms\n"
2568 "%3d%% success (%d good packets over %d tries)\n",
2569 success / (2 * samples), (success * 5 / samples) % 10,
2570 max_time, good * 100 / samples, good, samples);
2571 else
2572 count += sprintf(&buf[count], "\nAverage rate: 0Mbs\n");
Ian Schram01ebd062007-10-25 17:15:22 +08002573 count += sprintf(&buf[count], "\nrate scale type %d antenna %d "
Zhu Yib481de92007-09-25 17:54:57 -07002574 "active_search %d rate index %d\n", lq_type, antenna,
2575 rs_priv->search_better_tbl, sta->last_txrate);
2576
2577 sta_info_put(sta);
2578 return count;
2579}
2580
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002581void iwl4965_rate_scale_init(struct ieee80211_hw *hw, s32 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07002582{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002583 struct iwl4965_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07002584
2585 priv->lq_mngr.lq_ready = 1;
2586}
2587
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002588void iwl4965_rate_control_register(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07002589{
2590 ieee80211_rate_control_register(&rs_ops);
2591}
2592
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002593void iwl4965_rate_control_unregister(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07002594{
2595 ieee80211_rate_control_unregister(&rs_ops);
2596}
2597