blob: 229b34199f92d6026c304dcb3e10fad8b01ee46b [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
Ron Rindjunsky270243a2007-11-26 16:14:41 +0200501static inline u8 rs_use_green(struct iwl4965_priv *priv,
502 struct ieee80211_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -0700503{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800504#ifdef CONFIG_IWL4965_HT
Ron Rindjunsky270243a2007-11-26 16:14:41 +0200505 return ((conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) &&
506 priv->current_ht_config.is_green_field &&
507 !priv->current_ht_config.non_GF_STA_present);
508#endif /* CONFIG_IWL4965_HT */
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800509 return 0;
Zhu Yib481de92007-09-25 17:54:57 -0700510}
511
512/**
513 * rs_get_supported_rates - get the available rates
514 *
515 * if management frame or broadcast frame only return
516 * basic available rates.
517 *
518 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800519static void rs_get_supported_rates(struct iwl4965_rate_scale_priv *lq_data,
Zhu Yib481de92007-09-25 17:54:57 -0700520 struct ieee80211_hdr *hdr,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800521 enum iwl4965_table_type rate_type,
Zhu Yib481de92007-09-25 17:54:57 -0700522 u16 *data_rate)
523{
524 if (is_legacy(rate_type))
525 *data_rate = lq_data->active_rate;
526 else {
527 if (is_siso(rate_type))
528 *data_rate = lq_data->active_siso_rate;
529 else
530 *data_rate = lq_data->active_mimo_rate;
531 }
532
533 if (hdr && is_multicast_ether_addr(hdr->addr1) &&
534 lq_data->active_rate_basic)
535 *data_rate = lq_data->active_rate_basic;
536}
537
538static u16 rs_get_adjacent_rate(u8 index, u16 rate_mask, int rate_type)
539{
540 u8 high = IWL_RATE_INVALID;
541 u8 low = IWL_RATE_INVALID;
542
Ian Schram01ebd062007-10-25 17:15:22 +0800543 /* 802.11A or ht walks to the next literal adjacent rate in
Zhu Yib481de92007-09-25 17:54:57 -0700544 * the rate table */
545 if (is_a_band(rate_type) || !is_legacy(rate_type)) {
546 int i;
547 u32 mask;
548
549 /* Find the previous rate that is in the rate mask */
550 i = index - 1;
551 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
552 if (rate_mask & mask) {
553 low = i;
554 break;
555 }
556 }
557
558 /* Find the next rate that is in the rate mask */
559 i = index + 1;
560 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
561 if (rate_mask & mask) {
562 high = i;
563 break;
564 }
565 }
566
567 return (high << 8) | low;
568 }
569
570 low = index;
571 while (low != IWL_RATE_INVALID) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800572 low = iwl4965_rates[low].prev_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700573 if (low == IWL_RATE_INVALID)
574 break;
575 if (rate_mask & (1 << low))
576 break;
577 IWL_DEBUG_RATE("Skipping masked lower rate: %d\n", low);
578 }
579
580 high = index;
581 while (high != IWL_RATE_INVALID) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800582 high = iwl4965_rates[high].next_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700583 if (high == IWL_RATE_INVALID)
584 break;
585 if (rate_mask & (1 << high))
586 break;
587 IWL_DEBUG_RATE("Skipping masked higher rate: %d\n", high);
588 }
589
590 return (high << 8) | low;
591}
592
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800593static void rs_get_lower_rate(struct iwl4965_rate_scale_priv *lq_data,
594 struct iwl4965_scale_tbl_info *tbl, u8 scale_index,
595 u8 ht_possible, struct iwl4965_rate *mcs_rate)
Zhu Yib481de92007-09-25 17:54:57 -0700596{
Zhu Yib481de92007-09-25 17:54:57 -0700597 s32 low;
598 u16 rate_mask;
599 u16 high_low;
600 u8 switch_to_legacy = 0;
Zhu Yi02dede02007-09-27 11:27:40 +0800601 u8 is_green = lq_data->is_green;
Zhu Yib481de92007-09-25 17:54:57 -0700602
603 /* check if we need to switch from HT to legacy rates.
604 * assumption is that mandatory rates (1Mbps or 6Mbps)
605 * are always supported (spec demand) */
606 if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_index)) {
607 switch_to_legacy = 1;
608 scale_index = rs_ht_to_legacy[scale_index];
609 if (lq_data->phymode == MODE_IEEE80211A)
610 tbl->lq_type = LQ_A;
611 else
612 tbl->lq_type = LQ_G;
613
614 if ((tbl->antenna_type == ANT_BOTH) ||
615 (tbl->antenna_type == ANT_NONE))
616 tbl->antenna_type = ANT_MAIN;
617
618 tbl->is_fat = 0;
619 tbl->is_SGI = 0;
620 }
621
622 rs_get_supported_rates(lq_data, NULL, tbl->lq_type, &rate_mask);
623
Ben Cahill77626352007-11-29 11:09:44 +0800624 /* Mask with station rate restriction */
Zhu Yib481de92007-09-25 17:54:57 -0700625 if (is_legacy(tbl->lq_type)) {
Ben Cahill77626352007-11-29 11:09:44 +0800626 /* supp_rates has no CCK bits in A mode */
Zhu Yib481de92007-09-25 17:54:57 -0700627 if (lq_data->phymode == (u8) MODE_IEEE80211A)
628 rate_mask = (u16)(rate_mask &
Zhu Yi02dede02007-09-27 11:27:40 +0800629 (lq_data->supp_rates << IWL_FIRST_OFDM_RATE));
Zhu Yib481de92007-09-25 17:54:57 -0700630 else
Zhu Yi02dede02007-09-27 11:27:40 +0800631 rate_mask = (u16)(rate_mask & lq_data->supp_rates);
Zhu Yib481de92007-09-25 17:54:57 -0700632 }
633
Ben Cahill77626352007-11-29 11:09:44 +0800634 /* If we switched from HT to legacy, check current rate */
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800635 if (switch_to_legacy && (rate_mask & (1 << scale_index))) {
Zhu Yib481de92007-09-25 17:54:57 -0700636 rs_mcs_from_tbl(mcs_rate, tbl, scale_index, is_green);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800637 return;
Zhu Yib481de92007-09-25 17:54:57 -0700638 }
639
640 high_low = rs_get_adjacent_rate(scale_index, rate_mask, tbl->lq_type);
641 low = high_low & 0xff;
642
643 if (low != IWL_RATE_INVALID)
644 rs_mcs_from_tbl(mcs_rate, tbl, low, is_green);
645 else
646 rs_mcs_from_tbl(mcs_rate, tbl, scale_index, is_green);
Zhu Yib481de92007-09-25 17:54:57 -0700647}
648
Ben Cahill77626352007-11-29 11:09:44 +0800649/*
650 * mac80211 sends us Tx status
651 */
Zhu Yib481de92007-09-25 17:54:57 -0700652static void rs_tx_status(void *priv_rate,
653 struct net_device *dev,
654 struct sk_buff *skb,
655 struct ieee80211_tx_status *tx_resp)
656{
657 int status;
658 u8 retries;
659 int rs_index, index = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800660 struct iwl4965_rate_scale_priv *lq;
661 struct iwl4965_link_quality_cmd *table;
Zhu Yib481de92007-09-25 17:54:57 -0700662 struct sta_info *sta;
663 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800664 struct iwl4965_priv *priv = (struct iwl4965_priv *)priv_rate;
Zhu Yib481de92007-09-25 17:54:57 -0700665 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800666 struct iwl4965_rate_scale_data *window = NULL;
667 struct iwl4965_rate_scale_data *search_win = NULL;
668 struct iwl4965_rate tx_mcs;
669 struct iwl4965_scale_tbl_info tbl_type;
670 struct iwl4965_scale_tbl_info *curr_tbl, *search_tbl;
Zhu Yib481de92007-09-25 17:54:57 -0700671 u8 active_index = 0;
672 u16 fc = le16_to_cpu(hdr->frame_control);
673 s32 tpt = 0;
674
Zhu Yi58826352007-09-27 11:27:39 +0800675 IWL_DEBUG_RATE_LIMIT("get frame ack response, update rate scale window\n");
Zhu Yib481de92007-09-25 17:54:57 -0700676
677 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1))
678 return;
679
680 retries = tx_resp->retry_count;
681
682 if (retries > 15)
683 retries = 15;
684
685
686 sta = sta_info_get(local, hdr->addr1);
687
688 if (!sta || !sta->rate_ctrl_priv) {
689 if (sta)
690 sta_info_put(sta);
691 return;
692 }
693
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800694 lq = (struct iwl4965_rate_scale_priv *)sta->rate_ctrl_priv;
Zhu Yib481de92007-09-25 17:54:57 -0700695
696 if (!priv->lq_mngr.lq_ready)
697 return;
698
699 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) && !lq->ibss_sta_added)
700 return;
701
702 table = &lq->lq;
703 active_index = lq->active_tbl;
704
Ben Cahill77626352007-11-29 11:09:44 +0800705 /* Get mac80211 antenna info */
Zhu Yib481de92007-09-25 17:54:57 -0700706 lq->antenna = (lq->valid_antenna & local->hw.conf.antenna_sel_tx);
707 if (!lq->antenna)
708 lq->antenna = lq->valid_antenna;
709
Ben Cahill77626352007-11-29 11:09:44 +0800710 /* Ignore mac80211 antenna info for now */
Zhu Yib481de92007-09-25 17:54:57 -0700711 lq->antenna = lq->valid_antenna;
Ben Cahill77626352007-11-29 11:09:44 +0800712
Zhu Yib481de92007-09-25 17:54:57 -0700713 curr_tbl = &(lq->lq_info[active_index]);
714 search_tbl = &(lq->lq_info[(1 - active_index)]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800715 window = (struct iwl4965_rate_scale_data *)
Zhu Yib481de92007-09-25 17:54:57 -0700716 &(curr_tbl->win[0]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800717 search_win = (struct iwl4965_rate_scale_data *)
Zhu Yib481de92007-09-25 17:54:57 -0700718 &(search_tbl->win[0]);
719
720 tx_mcs.rate_n_flags = tx_resp->control.tx_rate;
721
722 rs_get_tbl_info_from_mcs(&tx_mcs, priv->phymode,
723 &tbl_type, &rs_index);
724 if ((rs_index < 0) || (rs_index >= IWL_RATE_COUNT)) {
725 IWL_DEBUG_RATE("bad rate index at: %d rate 0x%X\n",
726 rs_index, tx_mcs.rate_n_flags);
727 sta_info_put(sta);
728 return;
729 }
730
Ben Cahill77626352007-11-29 11:09:44 +0800731 /*
732 * Ignore this Tx frame response if its initial rate doesn't match
733 * that of latest Link Quality command. There may be stragglers
734 * from a previous Link Quality command, but we're no longer interested
735 * in those; they're either from the "active" mode while we're trying
736 * to check "search" mode, or a prior "search" mode after we've moved
737 * to a new "search" mode (which might become the new "active" mode).
738 */
Zhu Yib481de92007-09-25 17:54:57 -0700739 if (retries &&
740 (tx_mcs.rate_n_flags !=
741 le32_to_cpu(table->rs_table[0].rate_n_flags))) {
742 IWL_DEBUG_RATE("initial rate does not match 0x%x 0x%x\n",
743 tx_mcs.rate_n_flags,
744 le32_to_cpu(table->rs_table[0].rate_n_flags));
745 sta_info_put(sta);
746 return;
747 }
748
Ben Cahill77626352007-11-29 11:09:44 +0800749 /* Update frame history window with "failure" for each Tx retry. */
Zhu Yib481de92007-09-25 17:54:57 -0700750 while (retries) {
Ben Cahill77626352007-11-29 11:09:44 +0800751 /* Look up the rate and other info used for each tx attempt.
752 * Each tx attempt steps one entry deeper in the rate table. */
Zhu Yib481de92007-09-25 17:54:57 -0700753 tx_mcs.rate_n_flags =
754 le32_to_cpu(table->rs_table[index].rate_n_flags);
755 rs_get_tbl_info_from_mcs(&tx_mcs, priv->phymode,
756 &tbl_type, &rs_index);
757
Ben Cahill77626352007-11-29 11:09:44 +0800758 /* If type matches "search" table,
759 * add failure to "search" history */
Zhu Yib481de92007-09-25 17:54:57 -0700760 if ((tbl_type.lq_type == search_tbl->lq_type) &&
761 (tbl_type.antenna_type == search_tbl->antenna_type) &&
762 (tbl_type.is_SGI == search_tbl->is_SGI)) {
763 if (search_tbl->expected_tpt)
764 tpt = search_tbl->expected_tpt[rs_index];
765 else
766 tpt = 0;
Ben Cahill77626352007-11-29 11:09:44 +0800767 rs_collect_tx_data(search_win, rs_index, tpt, 0);
768
769 /* Else if type matches "current/active" table,
770 * add failure to "current/active" history */
Zhu Yib481de92007-09-25 17:54:57 -0700771 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
772 (tbl_type.antenna_type == curr_tbl->antenna_type) &&
773 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
774 if (curr_tbl->expected_tpt)
775 tpt = curr_tbl->expected_tpt[rs_index];
776 else
777 tpt = 0;
778 rs_collect_tx_data(window, rs_index, tpt, 0);
779 }
Ben Cahill77626352007-11-29 11:09:44 +0800780
781 /* If not searching for a new mode, increment failed counter
782 * ... this helps determine when to start searching again */
Zhu Yib481de92007-09-25 17:54:57 -0700783 if (lq->stay_in_tbl)
784 lq->total_failed++;
785 --retries;
786 index++;
787
788 }
789
Ben Cahill77626352007-11-29 11:09:44 +0800790 /*
791 * Find (by rate) the history window to update with final Tx attempt;
792 * if Tx was successful first try, use original rate,
793 * else look up the rate that was, finally, successful.
794 */
Zhu Yib481de92007-09-25 17:54:57 -0700795 if (!tx_resp->retry_count)
796 tx_mcs.rate_n_flags = tx_resp->control.tx_rate;
797 else
798 tx_mcs.rate_n_flags =
799 le32_to_cpu(table->rs_table[index].rate_n_flags);
800
801 rs_get_tbl_info_from_mcs(&tx_mcs, priv->phymode,
802 &tbl_type, &rs_index);
803
Ben Cahill77626352007-11-29 11:09:44 +0800804 /* Update frame history window with "success" if Tx got ACKed ... */
Zhu Yib481de92007-09-25 17:54:57 -0700805 if (tx_resp->flags & IEEE80211_TX_STATUS_ACK)
806 status = 1;
807 else
808 status = 0;
809
Ben Cahill77626352007-11-29 11:09:44 +0800810 /* If type matches "search" table,
811 * add final tx status to "search" history */
Zhu Yib481de92007-09-25 17:54:57 -0700812 if ((tbl_type.lq_type == search_tbl->lq_type) &&
813 (tbl_type.antenna_type == search_tbl->antenna_type) &&
814 (tbl_type.is_SGI == search_tbl->is_SGI)) {
815 if (search_tbl->expected_tpt)
816 tpt = search_tbl->expected_tpt[rs_index];
817 else
818 tpt = 0;
819 rs_collect_tx_data(search_win,
820 rs_index, tpt, status);
Ben Cahill77626352007-11-29 11:09:44 +0800821
822 /* Else if type matches "current/active" table,
823 * add final tx status to "current/active" history */
Zhu Yib481de92007-09-25 17:54:57 -0700824 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
825 (tbl_type.antenna_type == curr_tbl->antenna_type) &&
826 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
827 if (curr_tbl->expected_tpt)
828 tpt = curr_tbl->expected_tpt[rs_index];
829 else
830 tpt = 0;
831 rs_collect_tx_data(window, rs_index, tpt, status);
832 }
833
Ben Cahill77626352007-11-29 11:09:44 +0800834 /* If not searching for new mode, increment success/failed counter
835 * ... these help determine when to start searching again */
Zhu Yib481de92007-09-25 17:54:57 -0700836 if (lq->stay_in_tbl) {
837 if (status)
838 lq->total_success++;
839 else
840 lq->total_failed++;
841 }
842
Ben Cahill77626352007-11-29 11:09:44 +0800843 /* See if there's a better rate or modulation mode to try. */
Zhu Yib481de92007-09-25 17:54:57 -0700844 rs_rate_scale_perform(priv, dev, hdr, sta);
845 sta_info_put(sta);
846 return;
847}
848
849static u8 rs_is_ant_connected(u8 valid_antenna,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800850 enum iwl4965_antenna_type antenna_type)
Zhu Yib481de92007-09-25 17:54:57 -0700851{
852 if (antenna_type == ANT_AUX)
853 return ((valid_antenna & 0x2) ? 1:0);
854 else if (antenna_type == ANT_MAIN)
855 return ((valid_antenna & 0x1) ? 1:0);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800856 else if (antenna_type == ANT_BOTH)
857 return ((valid_antenna & 0x3) == 0x3);
Zhu Yib481de92007-09-25 17:54:57 -0700858
859 return 1;
860}
861
862static u8 rs_is_other_ant_connected(u8 valid_antenna,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800863 enum iwl4965_antenna_type antenna_type)
Zhu Yib481de92007-09-25 17:54:57 -0700864{
865 if (antenna_type == ANT_AUX)
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800866 return rs_is_ant_connected(valid_antenna, ANT_MAIN);
Zhu Yib481de92007-09-25 17:54:57 -0700867 else
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800868 return rs_is_ant_connected(valid_antenna, ANT_AUX);
Zhu Yib481de92007-09-25 17:54:57 -0700869
870 return 0;
871}
872
Ben Cahill77626352007-11-29 11:09:44 +0800873/*
874 * Begin a period of staying with a selected modulation mode.
875 * Set "stay_in_tbl" flag to prevent any mode switches.
876 * Set frame tx success limits according to legacy vs. high-throughput,
877 * and reset overall (spanning all rates) tx success history statistics.
878 * These control how long we stay using same modulation mode before
879 * searching for a new mode.
880 */
Zhu Yib481de92007-09-25 17:54:57 -0700881static void rs_set_stay_in_table(u8 is_legacy,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800882 struct iwl4965_rate_scale_priv *lq_data)
Zhu Yib481de92007-09-25 17:54:57 -0700883{
884 IWL_DEBUG_HT("we are staying in the same table\n");
Ben Cahill77626352007-11-29 11:09:44 +0800885 lq_data->stay_in_tbl = 1; /* only place this gets set */
Zhu Yib481de92007-09-25 17:54:57 -0700886 if (is_legacy) {
887 lq_data->table_count_limit = IWL_LEGACY_TABLE_COUNT;
888 lq_data->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
Mohamed Abbas403ab562007-11-06 22:06:25 -0800889 lq_data->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
Zhu Yib481de92007-09-25 17:54:57 -0700890 } else {
891 lq_data->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
892 lq_data->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
893 lq_data->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
894 }
895 lq_data->table_count = 0;
896 lq_data->total_failed = 0;
897 lq_data->total_success = 0;
898}
899
Ben Cahill77626352007-11-29 11:09:44 +0800900/*
901 * Find correct throughput table for given mode of modulation
902 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800903static void rs_get_expected_tpt_table(struct iwl4965_rate_scale_priv *lq_data,
904 struct iwl4965_scale_tbl_info *tbl)
Zhu Yib481de92007-09-25 17:54:57 -0700905{
906 if (is_legacy(tbl->lq_type)) {
907 if (!is_a_band(tbl->lq_type))
908 tbl->expected_tpt = expected_tpt_G;
909 else
910 tbl->expected_tpt = expected_tpt_A;
911 } else if (is_siso(tbl->lq_type)) {
912 if (tbl->is_fat && !lq_data->is_dup)
913 if (tbl->is_SGI)
914 tbl->expected_tpt = expected_tpt_siso40MHzSGI;
915 else
916 tbl->expected_tpt = expected_tpt_siso40MHz;
917 else if (tbl->is_SGI)
918 tbl->expected_tpt = expected_tpt_siso20MHzSGI;
919 else
920 tbl->expected_tpt = expected_tpt_siso20MHz;
921
922 } else if (is_mimo(tbl->lq_type)) {
923 if (tbl->is_fat && !lq_data->is_dup)
924 if (tbl->is_SGI)
925 tbl->expected_tpt = expected_tpt_mimo40MHzSGI;
926 else
927 tbl->expected_tpt = expected_tpt_mimo40MHz;
928 else if (tbl->is_SGI)
929 tbl->expected_tpt = expected_tpt_mimo20MHzSGI;
930 else
931 tbl->expected_tpt = expected_tpt_mimo20MHz;
932 } else
933 tbl->expected_tpt = expected_tpt_G;
934}
935
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800936#ifdef CONFIG_IWL4965_HT
Ben Cahill77626352007-11-29 11:09:44 +0800937/*
938 * Find starting rate for new "search" high-throughput mode of modulation.
939 * Goal is to find lowest expected rate (under perfect conditions) that is
940 * above the current measured throughput of "active" mode, to give new mode
941 * a fair chance to prove itself without too many challenges.
942 *
943 * This gets called when transitioning to more aggressive modulation
944 * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
945 * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
946 * to decrease to match "active" throughput. When moving from MIMO to SISO,
947 * bit rate will typically need to increase, but not if performance was bad.
948 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800949static s32 rs_get_best_rate(struct iwl4965_priv *priv,
950 struct iwl4965_rate_scale_priv *lq_data,
Ben Cahill77626352007-11-29 11:09:44 +0800951 struct iwl4965_scale_tbl_info *tbl, /* "search" */
Zhu Yib481de92007-09-25 17:54:57 -0700952 u16 rate_mask, s8 index, s8 rate)
953{
Ben Cahill77626352007-11-29 11:09:44 +0800954 /* "active" values */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800955 struct iwl4965_scale_tbl_info *active_tbl =
Zhu Yib481de92007-09-25 17:54:57 -0700956 &(lq_data->lq_info[lq_data->active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -0700957 s32 active_sr = active_tbl->win[index].success_ratio;
Zhu Yib481de92007-09-25 17:54:57 -0700958 s32 active_tpt = active_tbl->expected_tpt[index];
Ben Cahill77626352007-11-29 11:09:44 +0800959
960 /* expected "search" throughput */
961 s32 *tpt_tbl = tbl->expected_tpt;
962
963 s32 new_rate, high, low, start_hi;
Zhu Yib481de92007-09-25 17:54:57 -0700964 u16 high_low;
965
966 new_rate = high = low = start_hi = IWL_RATE_INVALID;
967
968 for (; ;) {
969 high_low = rs_get_adjacent_rate(rate, rate_mask, tbl->lq_type);
970
971 low = high_low & 0xff;
972 high = (high_low >> 8) & 0xff;
973
Ben Cahill77626352007-11-29 11:09:44 +0800974 /*
975 * Lower the "search" bit rate, to give new "search" mode
976 * approximately the same throughput as "active" if:
977 *
978 * 1) "Active" mode has been working modestly well (but not
979 * great), and expected "search" throughput (under perfect
980 * conditions) at candidate rate is above the actual
981 * measured "active" throughput (but less than expected
982 * "active" throughput under perfect conditions).
983 * OR
984 * 2) "Active" mode has been working perfectly or very well
985 * and expected "search" throughput (under perfect
986 * conditions) at candidate rate is above expected
987 * "active" throughput (under perfect conditions).
988 */
Zhu Yib481de92007-09-25 17:54:57 -0700989 if ((((100 * tpt_tbl[rate]) > lq_data->last_tpt) &&
990 ((active_sr > IWL_RATE_DECREASE_TH) &&
991 (active_sr <= IWL_RATE_HIGH_TH) &&
992 (tpt_tbl[rate] <= active_tpt))) ||
993 ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
994 (tpt_tbl[rate] > active_tpt))) {
995
Ben Cahill77626352007-11-29 11:09:44 +0800996 /* (2nd or later pass)
997 * If we've already tried to raise the rate, and are
998 * now trying to lower it, use the higher rate. */
Zhu Yib481de92007-09-25 17:54:57 -0700999 if (start_hi != IWL_RATE_INVALID) {
1000 new_rate = start_hi;
1001 break;
1002 }
Ben Cahill77626352007-11-29 11:09:44 +08001003
Zhu Yib481de92007-09-25 17:54:57 -07001004 new_rate = rate;
Ben Cahill77626352007-11-29 11:09:44 +08001005
1006 /* Loop again with lower rate */
Zhu Yib481de92007-09-25 17:54:57 -07001007 if (low != IWL_RATE_INVALID)
1008 rate = low;
Ben Cahill77626352007-11-29 11:09:44 +08001009
1010 /* Lower rate not available, use the original */
Zhu Yib481de92007-09-25 17:54:57 -07001011 else
1012 break;
Ben Cahill77626352007-11-29 11:09:44 +08001013
1014 /* Else try to raise the "search" rate to match "active" */
Zhu Yib481de92007-09-25 17:54:57 -07001015 } else {
Ben Cahill77626352007-11-29 11:09:44 +08001016 /* (2nd or later pass)
1017 * If we've already tried to lower the rate, and are
1018 * now trying to raise it, use the lower rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001019 if (new_rate != IWL_RATE_INVALID)
1020 break;
Ben Cahill77626352007-11-29 11:09:44 +08001021
1022 /* Loop again with higher rate */
Zhu Yib481de92007-09-25 17:54:57 -07001023 else if (high != IWL_RATE_INVALID) {
1024 start_hi = high;
1025 rate = high;
Ben Cahill77626352007-11-29 11:09:44 +08001026
1027 /* Higher rate not available, use the original */
Zhu Yib481de92007-09-25 17:54:57 -07001028 } else {
1029 new_rate = rate;
1030 break;
1031 }
1032 }
1033 }
1034
1035 return new_rate;
1036}
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001037#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001038
1039static inline u8 rs_is_both_ant_supp(u8 valid_antenna)
1040{
1041 return (rs_is_ant_connected(valid_antenna, ANT_BOTH));
1042}
1043
Ben Cahill77626352007-11-29 11:09:44 +08001044/*
1045 * Set up search table for MIMO
1046 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001047static int rs_switch_to_mimo(struct iwl4965_priv *priv,
1048 struct iwl4965_rate_scale_priv *lq_data,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001049 struct ieee80211_conf *conf,
1050 struct sta_info *sta,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001051 struct iwl4965_scale_tbl_info *tbl, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001052{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001053#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07001054 u16 rate_mask;
1055 s32 rate;
1056 s8 is_green = lq_data->is_green;
1057
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001058 if (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) ||
1059 !sta->ht_info.ht_supported)
Zhu Yib481de92007-09-25 17:54:57 -07001060 return -1;
1061
1062 IWL_DEBUG_HT("LQ: try to switch to MIMO\n");
1063 tbl->lq_type = LQ_MIMO;
1064 rs_get_supported_rates(lq_data, NULL, tbl->lq_type,
1065 &rate_mask);
1066
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001067 if (priv->current_ht_config.tx_mimo_ps_mode == IWL_MIMO_PS_STATIC)
Zhu Yib481de92007-09-25 17:54:57 -07001068 return -1;
1069
Ben Cahill77626352007-11-29 11:09:44 +08001070 /* Need both Tx chains/antennas to support MIMO */
Zhu Yib481de92007-09-25 17:54:57 -07001071 if (!rs_is_both_ant_supp(lq_data->antenna))
1072 return -1;
1073
Zhu Yib481de92007-09-25 17:54:57 -07001074 tbl->is_dup = lq_data->is_dup;
1075 tbl->action = 0;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001076 if (priv->current_ht_config.supported_chan_width
1077 == IWL_CHANNEL_WIDTH_40MHZ)
Zhu Yib481de92007-09-25 17:54:57 -07001078 tbl->is_fat = 1;
1079 else
1080 tbl->is_fat = 0;
1081
1082 if (tbl->is_fat) {
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001083 if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
Zhu Yib481de92007-09-25 17:54:57 -07001084 tbl->is_SGI = 1;
1085 else
1086 tbl->is_SGI = 0;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001087 } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
Zhu Yib481de92007-09-25 17:54:57 -07001088 tbl->is_SGI = 1;
1089 else
1090 tbl->is_SGI = 0;
1091
1092 rs_get_expected_tpt_table(lq_data, tbl);
1093
1094 rate = rs_get_best_rate(priv, lq_data, tbl, rate_mask, index, index);
1095
1096 IWL_DEBUG_HT("LQ: MIMO best rate %d mask %X\n", rate, rate_mask);
1097 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask))
1098 return -1;
1099 rs_mcs_from_tbl(&tbl->current_rate, tbl, rate, is_green);
1100
1101 IWL_DEBUG_HT("LQ: Switch to new mcs %X index is green %X\n",
1102 tbl->current_rate.rate_n_flags, is_green);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001103 return 0;
Mohamed Abbas403ab562007-11-06 22:06:25 -08001104#else
1105 return -1;
1106#endif /*CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001107}
1108
Ben Cahill77626352007-11-29 11:09:44 +08001109/*
1110 * Set up search table for SISO
1111 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001112static int rs_switch_to_siso(struct iwl4965_priv *priv,
1113 struct iwl4965_rate_scale_priv *lq_data,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001114 struct ieee80211_conf *conf,
1115 struct sta_info *sta,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001116 struct iwl4965_scale_tbl_info *tbl, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001117{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001118#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07001119 u16 rate_mask;
1120 u8 is_green = lq_data->is_green;
1121 s32 rate;
1122
1123 IWL_DEBUG_HT("LQ: try to switch to SISO\n");
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001124 if (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) ||
1125 !sta->ht_info.ht_supported)
Zhu Yib481de92007-09-25 17:54:57 -07001126 return -1;
1127
Zhu Yib481de92007-09-25 17:54:57 -07001128 tbl->is_dup = lq_data->is_dup;
1129 tbl->lq_type = LQ_SISO;
1130 tbl->action = 0;
1131 rs_get_supported_rates(lq_data, NULL, tbl->lq_type,
1132 &rate_mask);
1133
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001134 if (priv->current_ht_config.supported_chan_width
1135 == IWL_CHANNEL_WIDTH_40MHZ)
Zhu Yib481de92007-09-25 17:54:57 -07001136 tbl->is_fat = 1;
1137 else
1138 tbl->is_fat = 0;
1139
1140 if (tbl->is_fat) {
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001141 if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
Zhu Yib481de92007-09-25 17:54:57 -07001142 tbl->is_SGI = 1;
1143 else
1144 tbl->is_SGI = 0;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001145 } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
Zhu Yib481de92007-09-25 17:54:57 -07001146 tbl->is_SGI = 1;
1147 else
1148 tbl->is_SGI = 0;
1149
1150 if (is_green)
1151 tbl->is_SGI = 0;
1152
1153 rs_get_expected_tpt_table(lq_data, tbl);
1154 rate = rs_get_best_rate(priv, lq_data, tbl, rate_mask, index, index);
1155
1156 IWL_DEBUG_HT("LQ: get best rate %d mask %X\n", rate, rate_mask);
1157 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1158 IWL_DEBUG_HT("can not switch with index %d rate mask %x\n",
1159 rate, rate_mask);
1160 return -1;
1161 }
1162 rs_mcs_from_tbl(&tbl->current_rate, tbl, rate, is_green);
1163 IWL_DEBUG_HT("LQ: Switch to new mcs %X index is green %X\n",
1164 tbl->current_rate.rate_n_flags, is_green);
Mohamed Abbas403ab562007-11-06 22:06:25 -08001165 return 0;
1166#else
1167 return -1;
Zhu Yib481de92007-09-25 17:54:57 -07001168
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001169#endif /*CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001170}
1171
Ben Cahill77626352007-11-29 11:09:44 +08001172/*
1173 * Try to switch to new modulation mode from legacy
1174 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001175static int rs_move_legacy_other(struct iwl4965_priv *priv,
1176 struct iwl4965_rate_scale_priv *lq_data,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001177 struct ieee80211_conf *conf,
1178 struct sta_info *sta,
Zhu Yib481de92007-09-25 17:54:57 -07001179 int index)
1180{
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001181 int ret = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001182 struct iwl4965_scale_tbl_info *tbl =
Zhu Yib481de92007-09-25 17:54:57 -07001183 &(lq_data->lq_info[lq_data->active_tbl]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001184 struct iwl4965_scale_tbl_info *search_tbl =
Zhu Yib481de92007-09-25 17:54:57 -07001185 &(lq_data->lq_info[(1 - lq_data->active_tbl)]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001186 struct iwl4965_rate_scale_data *window = &(tbl->win[index]);
1187 u32 sz = (sizeof(struct iwl4965_scale_tbl_info) -
1188 (sizeof(struct iwl4965_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001189 u8 start_action = tbl->action;
1190
1191 for (; ;) {
1192 switch (tbl->action) {
1193 case IWL_LEGACY_SWITCH_ANTENNA:
1194 IWL_DEBUG_HT("LQ Legacy switch Antenna\n");
1195
1196 search_tbl->lq_type = LQ_NONE;
1197 lq_data->action_counter++;
Ben Cahill77626352007-11-29 11:09:44 +08001198
1199 /* Don't change antenna if success has been great */
Zhu Yib481de92007-09-25 17:54:57 -07001200 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1201 break;
Ben Cahill77626352007-11-29 11:09:44 +08001202
1203 /* Don't change antenna if other one is not connected */
Zhu Yib481de92007-09-25 17:54:57 -07001204 if (!rs_is_other_ant_connected(lq_data->antenna,
1205 tbl->antenna_type))
1206 break;
1207
Ben Cahill77626352007-11-29 11:09:44 +08001208 /* Set up search table to try other antenna */
Zhu Yib481de92007-09-25 17:54:57 -07001209 memcpy(search_tbl, tbl, sz);
1210
1211 rs_toggle_antenna(&(search_tbl->current_rate),
1212 search_tbl);
1213 rs_get_expected_tpt_table(lq_data, search_tbl);
1214 lq_data->search_better_tbl = 1;
1215 goto out;
1216
1217 case IWL_LEGACY_SWITCH_SISO:
1218 IWL_DEBUG_HT("LQ: Legacy switch to SISO\n");
Ben Cahill77626352007-11-29 11:09:44 +08001219
1220 /* Set up search table to try SISO */
Zhu Yib481de92007-09-25 17:54:57 -07001221 memcpy(search_tbl, tbl, sz);
1222 search_tbl->lq_type = LQ_SISO;
1223 search_tbl->is_SGI = 0;
1224 search_tbl->is_fat = 0;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001225 ret = rs_switch_to_siso(priv, lq_data, conf, sta,
1226 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001227 if (!ret) {
Zhu Yib481de92007-09-25 17:54:57 -07001228 lq_data->search_better_tbl = 1;
1229 lq_data->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001230 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001231 }
Zhu Yib481de92007-09-25 17:54:57 -07001232
1233 break;
1234 case IWL_LEGACY_SWITCH_MIMO:
1235 IWL_DEBUG_HT("LQ: Legacy switch MIMO\n");
Ben Cahill77626352007-11-29 11:09:44 +08001236
1237 /* Set up search table to try MIMO */
Zhu Yib481de92007-09-25 17:54:57 -07001238 memcpy(search_tbl, tbl, sz);
1239 search_tbl->lq_type = LQ_MIMO;
1240 search_tbl->is_SGI = 0;
1241 search_tbl->is_fat = 0;
1242 search_tbl->antenna_type = ANT_BOTH;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001243 ret = rs_switch_to_mimo(priv, lq_data, conf, sta,
1244 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001245 if (!ret) {
Zhu Yib481de92007-09-25 17:54:57 -07001246 lq_data->search_better_tbl = 1;
1247 lq_data->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001248 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001249 }
Zhu Yib481de92007-09-25 17:54:57 -07001250 break;
1251 }
1252 tbl->action++;
1253 if (tbl->action > IWL_LEGACY_SWITCH_MIMO)
1254 tbl->action = IWL_LEGACY_SWITCH_ANTENNA;
1255
1256 if (tbl->action == start_action)
1257 break;
1258
1259 }
1260 return 0;
1261
1262 out:
1263 tbl->action++;
1264 if (tbl->action > IWL_LEGACY_SWITCH_MIMO)
1265 tbl->action = IWL_LEGACY_SWITCH_ANTENNA;
1266 return 0;
1267
1268}
1269
Ben Cahill77626352007-11-29 11:09:44 +08001270/*
1271 * Try to switch to new modulation mode from SISO
1272 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001273static int rs_move_siso_to_other(struct iwl4965_priv *priv,
1274 struct iwl4965_rate_scale_priv *lq_data,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001275 struct ieee80211_conf *conf,
1276 struct sta_info *sta,
Zhu Yib481de92007-09-25 17:54:57 -07001277 int index)
1278{
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001279 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001280 u8 is_green = lq_data->is_green;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001281 struct iwl4965_scale_tbl_info *tbl =
Zhu Yib481de92007-09-25 17:54:57 -07001282 &(lq_data->lq_info[lq_data->active_tbl]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001283 struct iwl4965_scale_tbl_info *search_tbl =
Zhu Yib481de92007-09-25 17:54:57 -07001284 &(lq_data->lq_info[(1 - lq_data->active_tbl)]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001285 struct iwl4965_rate_scale_data *window = &(tbl->win[index]);
1286 u32 sz = (sizeof(struct iwl4965_scale_tbl_info) -
1287 (sizeof(struct iwl4965_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001288 u8 start_action = tbl->action;
1289
1290 for (;;) {
1291 lq_data->action_counter++;
1292 switch (tbl->action) {
1293 case IWL_SISO_SWITCH_ANTENNA:
1294 IWL_DEBUG_HT("LQ: SISO SWITCH ANTENNA SISO\n");
1295 search_tbl->lq_type = LQ_NONE;
1296 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1297 break;
1298 if (!rs_is_other_ant_connected(lq_data->antenna,
1299 tbl->antenna_type))
1300 break;
1301
1302 memcpy(search_tbl, tbl, sz);
1303 search_tbl->action = IWL_SISO_SWITCH_MIMO;
1304 rs_toggle_antenna(&(search_tbl->current_rate),
1305 search_tbl);
1306 lq_data->search_better_tbl = 1;
1307
1308 goto out;
1309
1310 case IWL_SISO_SWITCH_MIMO:
1311 IWL_DEBUG_HT("LQ: SISO SWITCH TO MIMO FROM SISO\n");
1312 memcpy(search_tbl, tbl, sz);
1313 search_tbl->lq_type = LQ_MIMO;
1314 search_tbl->is_SGI = 0;
1315 search_tbl->is_fat = 0;
1316 search_tbl->antenna_type = ANT_BOTH;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001317 ret = rs_switch_to_mimo(priv, lq_data, conf, sta,
1318 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001319 if (!ret) {
Zhu Yib481de92007-09-25 17:54:57 -07001320 lq_data->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001321 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001322 }
Zhu Yib481de92007-09-25 17:54:57 -07001323 break;
1324 case IWL_SISO_SWITCH_GI:
1325 IWL_DEBUG_HT("LQ: SISO SWITCH TO GI\n");
1326 memcpy(search_tbl, tbl, sz);
1327 search_tbl->action = 0;
1328 if (search_tbl->is_SGI)
1329 search_tbl->is_SGI = 0;
1330 else if (!is_green)
1331 search_tbl->is_SGI = 1;
1332 else
1333 break;
1334 lq_data->search_better_tbl = 1;
1335 if ((tbl->lq_type == LQ_SISO) &&
1336 (tbl->is_SGI)) {
1337 s32 tpt = lq_data->last_tpt / 100;
1338 if (((!tbl->is_fat) &&
1339 (tpt >= expected_tpt_siso20MHz[index])) ||
1340 ((tbl->is_fat) &&
1341 (tpt >= expected_tpt_siso40MHz[index])))
1342 lq_data->search_better_tbl = 0;
1343 }
1344 rs_get_expected_tpt_table(lq_data, search_tbl);
1345 rs_mcs_from_tbl(&search_tbl->current_rate,
1346 search_tbl, index, is_green);
1347 goto out;
1348 }
1349 tbl->action++;
1350 if (tbl->action > IWL_SISO_SWITCH_GI)
1351 tbl->action = IWL_SISO_SWITCH_ANTENNA;
1352
1353 if (tbl->action == start_action)
1354 break;
1355 }
1356 return 0;
1357
1358 out:
1359 tbl->action++;
1360 if (tbl->action > IWL_SISO_SWITCH_GI)
1361 tbl->action = IWL_SISO_SWITCH_ANTENNA;
1362 return 0;
1363}
1364
Ben Cahill77626352007-11-29 11:09:44 +08001365/*
1366 * Try to switch to new modulation mode from MIMO
1367 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001368static int rs_move_mimo_to_other(struct iwl4965_priv *priv,
1369 struct iwl4965_rate_scale_priv *lq_data,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001370 struct ieee80211_conf *conf,
1371 struct sta_info *sta,
Zhu Yib481de92007-09-25 17:54:57 -07001372 int index)
1373{
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001374 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001375 s8 is_green = lq_data->is_green;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001376 struct iwl4965_scale_tbl_info *tbl =
Zhu Yib481de92007-09-25 17:54:57 -07001377 &(lq_data->lq_info[lq_data->active_tbl]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001378 struct iwl4965_scale_tbl_info *search_tbl =
Zhu Yib481de92007-09-25 17:54:57 -07001379 &(lq_data->lq_info[(1 - lq_data->active_tbl)]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001380 u32 sz = (sizeof(struct iwl4965_scale_tbl_info) -
1381 (sizeof(struct iwl4965_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001382 u8 start_action = tbl->action;
1383
1384 for (;;) {
1385 lq_data->action_counter++;
1386 switch (tbl->action) {
1387 case IWL_MIMO_SWITCH_ANTENNA_A:
1388 case IWL_MIMO_SWITCH_ANTENNA_B:
1389 IWL_DEBUG_HT("LQ: MIMO SWITCH TO SISO\n");
Ben Cahill77626352007-11-29 11:09:44 +08001390
1391 /* Set up new search table for SISO */
Zhu Yib481de92007-09-25 17:54:57 -07001392 memcpy(search_tbl, tbl, sz);
1393 search_tbl->lq_type = LQ_SISO;
1394 search_tbl->is_SGI = 0;
1395 search_tbl->is_fat = 0;
1396 if (tbl->action == IWL_MIMO_SWITCH_ANTENNA_A)
1397 search_tbl->antenna_type = ANT_MAIN;
1398 else
1399 search_tbl->antenna_type = ANT_AUX;
1400
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001401 ret = rs_switch_to_siso(priv, lq_data, conf, sta,
1402 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001403 if (!ret) {
Zhu Yib481de92007-09-25 17:54:57 -07001404 lq_data->search_better_tbl = 1;
1405 goto out;
1406 }
1407 break;
1408
1409 case IWL_MIMO_SWITCH_GI:
1410 IWL_DEBUG_HT("LQ: MIMO SWITCH TO GI\n");
Ben Cahill77626352007-11-29 11:09:44 +08001411
1412 /* Set up new search table for MIMO */
Zhu Yib481de92007-09-25 17:54:57 -07001413 memcpy(search_tbl, tbl, sz);
1414 search_tbl->lq_type = LQ_MIMO;
1415 search_tbl->antenna_type = ANT_BOTH;
1416 search_tbl->action = 0;
1417 if (search_tbl->is_SGI)
1418 search_tbl->is_SGI = 0;
1419 else
1420 search_tbl->is_SGI = 1;
1421 lq_data->search_better_tbl = 1;
Ben Cahill77626352007-11-29 11:09:44 +08001422
1423 /*
1424 * If active table already uses the fastest possible
1425 * modulation (dual stream with short guard interval),
1426 * and it's working well, there's no need to look
1427 * for a better type of modulation!
1428 */
Zhu Yib481de92007-09-25 17:54:57 -07001429 if ((tbl->lq_type == LQ_MIMO) &&
1430 (tbl->is_SGI)) {
1431 s32 tpt = lq_data->last_tpt / 100;
1432 if (((!tbl->is_fat) &&
1433 (tpt >= expected_tpt_mimo20MHz[index])) ||
1434 ((tbl->is_fat) &&
1435 (tpt >= expected_tpt_mimo40MHz[index])))
1436 lq_data->search_better_tbl = 0;
1437 }
1438 rs_get_expected_tpt_table(lq_data, search_tbl);
1439 rs_mcs_from_tbl(&search_tbl->current_rate,
1440 search_tbl, index, is_green);
1441 goto out;
1442
1443 }
1444 tbl->action++;
1445 if (tbl->action > IWL_MIMO_SWITCH_GI)
1446 tbl->action = IWL_MIMO_SWITCH_ANTENNA_A;
1447
1448 if (tbl->action == start_action)
1449 break;
1450 }
1451
1452 return 0;
1453 out:
1454 tbl->action++;
1455 if (tbl->action > IWL_MIMO_SWITCH_GI)
1456 tbl->action = IWL_MIMO_SWITCH_ANTENNA_A;
1457 return 0;
1458
1459}
1460
Ben Cahill77626352007-11-29 11:09:44 +08001461/*
1462 * Check whether we should continue using same modulation mode, or
1463 * begin search for a new mode, based on:
1464 * 1) # tx successes or failures while using this mode
1465 * 2) # times calling this function
1466 * 3) elapsed time in this mode (not used, for now)
1467 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001468static void rs_stay_in_table(struct iwl4965_rate_scale_priv *lq_data)
Zhu Yib481de92007-09-25 17:54:57 -07001469{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001470 struct iwl4965_scale_tbl_info *tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001471 int i;
1472 int active_tbl;
1473 int flush_interval_passed = 0;
1474
1475 active_tbl = lq_data->active_tbl;
1476
1477 tbl = &(lq_data->lq_info[active_tbl]);
1478
Ben Cahill77626352007-11-29 11:09:44 +08001479 /* If we've been disallowing search, see if we should now allow it */
Zhu Yib481de92007-09-25 17:54:57 -07001480 if (lq_data->stay_in_tbl) {
1481
Ben Cahill77626352007-11-29 11:09:44 +08001482 /* Elapsed time using current modulation mode */
Zhu Yib481de92007-09-25 17:54:57 -07001483 if (lq_data->flush_timer)
1484 flush_interval_passed =
1485 time_after(jiffies,
1486 (unsigned long)(lq_data->flush_timer +
1487 IWL_RATE_SCALE_FLUSH_INTVL));
1488
Ben Cahill77626352007-11-29 11:09:44 +08001489 /* For now, disable the elapsed time criterion */
Zhu Yib481de92007-09-25 17:54:57 -07001490 flush_interval_passed = 0;
Ben Cahill77626352007-11-29 11:09:44 +08001491
1492 /*
1493 * Check if we should allow search for new modulation mode.
1494 * If many frames have failed or succeeded, or we've used
1495 * this same modulation for a long time, allow search, and
1496 * reset history stats that keep track of whether we should
1497 * allow a new search. Also (below) reset all bitmaps and
1498 * stats in active history.
1499 */
Zhu Yib481de92007-09-25 17:54:57 -07001500 if ((lq_data->total_failed > lq_data->max_failure_limit) ||
1501 (lq_data->total_success > lq_data->max_success_limit) ||
1502 ((!lq_data->search_better_tbl) && (lq_data->flush_timer)
1503 && (flush_interval_passed))) {
1504 IWL_DEBUG_HT("LQ: stay is expired %d %d %d\n:",
1505 lq_data->total_failed,
1506 lq_data->total_success,
1507 flush_interval_passed);
Ben Cahill77626352007-11-29 11:09:44 +08001508
1509 /* Allow search for new mode */
1510 lq_data->stay_in_tbl = 0; /* only place reset */
Zhu Yib481de92007-09-25 17:54:57 -07001511 lq_data->total_failed = 0;
1512 lq_data->total_success = 0;
1513 lq_data->flush_timer = 0;
Ben Cahill77626352007-11-29 11:09:44 +08001514
1515 /*
1516 * Else if we've used this modulation mode enough repetitions
1517 * (regardless of elapsed time or success/failure), reset
1518 * history bitmaps and rate-specific stats for all rates in
1519 * active table.
1520 */
Mohamed Abbas403ab562007-11-06 22:06:25 -08001521 } else {
Zhu Yib481de92007-09-25 17:54:57 -07001522 lq_data->table_count++;
1523 if (lq_data->table_count >=
1524 lq_data->table_count_limit) {
1525 lq_data->table_count = 0;
1526
1527 IWL_DEBUG_HT("LQ: stay in table clear win\n");
1528 for (i = 0; i < IWL_RATE_COUNT; i++)
1529 rs_rate_scale_clear_window(
1530 &(tbl->win[i]));
1531 }
1532 }
1533
Ben Cahill77626352007-11-29 11:09:44 +08001534 /* If transitioning to allow "search", reset all history
1535 * bitmaps and stats in active table (this will become the new
1536 * "search" table). */
Zhu Yib481de92007-09-25 17:54:57 -07001537 if (!lq_data->stay_in_tbl) {
1538 for (i = 0; i < IWL_RATE_COUNT; i++)
1539 rs_rate_scale_clear_window(&(tbl->win[i]));
1540 }
1541 }
1542}
1543
Ben Cahill77626352007-11-29 11:09:44 +08001544/*
1545 * Do rate scaling and search for new modulation mode.
1546 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001547static void rs_rate_scale_perform(struct iwl4965_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001548 struct net_device *dev,
1549 struct ieee80211_hdr *hdr,
1550 struct sta_info *sta)
1551{
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001552 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1553 struct ieee80211_hw *hw = local_to_hw(local);
1554 struct ieee80211_conf *conf = &hw->conf;
Zhu Yib481de92007-09-25 17:54:57 -07001555 int low = IWL_RATE_INVALID;
1556 int high = IWL_RATE_INVALID;
1557 int index;
1558 int i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001559 struct iwl4965_rate_scale_data *window = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001560 int current_tpt = IWL_INVALID_VALUE;
1561 int low_tpt = IWL_INVALID_VALUE;
1562 int high_tpt = IWL_INVALID_VALUE;
1563 u32 fail_count;
1564 s8 scale_action = 0;
1565 u16 fc, rate_mask;
1566 u8 update_lq = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001567 struct iwl4965_rate_scale_priv *lq_data;
1568 struct iwl4965_scale_tbl_info *tbl, *tbl1;
Zhu Yib481de92007-09-25 17:54:57 -07001569 u16 rate_scale_index_msk = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001570 struct iwl4965_rate mcs_rate;
Zhu Yib481de92007-09-25 17:54:57 -07001571 u8 is_green = 0;
1572 u8 active_tbl = 0;
1573 u8 done_search = 0;
1574 u16 high_low;
1575
1576 IWL_DEBUG_RATE("rate scale calculate new rate for skb\n");
1577
1578 fc = le16_to_cpu(hdr->frame_control);
1579 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1)) {
1580 /* Send management frames and broadcast/multicast data using
1581 * lowest rate. */
1582 /* TODO: this could probably be improved.. */
1583 return;
1584 }
1585
1586 if (!sta || !sta->rate_ctrl_priv)
1587 return;
1588
1589 if (!priv->lq_mngr.lq_ready) {
1590 IWL_DEBUG_RATE("still rate scaling not ready\n");
1591 return;
1592 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001593 lq_data = (struct iwl4965_rate_scale_priv *)sta->rate_ctrl_priv;
Zhu Yib481de92007-09-25 17:54:57 -07001594
Ben Cahill77626352007-11-29 11:09:44 +08001595 /*
1596 * Select rate-scale / modulation-mode table to work with in
1597 * the rest of this function: "search" if searching for better
1598 * modulation mode, or "active" if doing rate scaling within a mode.
1599 */
Zhu Yib481de92007-09-25 17:54:57 -07001600 if (!lq_data->search_better_tbl)
1601 active_tbl = lq_data->active_tbl;
1602 else
1603 active_tbl = 1 - lq_data->active_tbl;
1604
1605 tbl = &(lq_data->lq_info[active_tbl]);
1606 is_green = lq_data->is_green;
1607
Ben Cahill77626352007-11-29 11:09:44 +08001608 /* current tx rate */
Zhu Yib481de92007-09-25 17:54:57 -07001609 index = sta->last_txrate;
1610
1611 IWL_DEBUG_RATE("Rate scale index %d for type %d\n", index,
1612 tbl->lq_type);
1613
Ben Cahill77626352007-11-29 11:09:44 +08001614 /* rates available for this association, and for modulation mode */
Zhu Yib481de92007-09-25 17:54:57 -07001615 rs_get_supported_rates(lq_data, hdr, tbl->lq_type,
1616 &rate_mask);
1617
1618 IWL_DEBUG_RATE("mask 0x%04X \n", rate_mask);
1619
1620 /* mask with station rate restriction */
1621 if (is_legacy(tbl->lq_type)) {
1622 if (lq_data->phymode == (u8) MODE_IEEE80211A)
Ben Cahill77626352007-11-29 11:09:44 +08001623 /* supp_rates has no CCK bits in A mode */
Zhu Yib481de92007-09-25 17:54:57 -07001624 rate_scale_index_msk = (u16) (rate_mask &
Zhu Yi02dede02007-09-27 11:27:40 +08001625 (lq_data->supp_rates << IWL_FIRST_OFDM_RATE));
Zhu Yib481de92007-09-25 17:54:57 -07001626 else
1627 rate_scale_index_msk = (u16) (rate_mask &
Zhu Yi02dede02007-09-27 11:27:40 +08001628 lq_data->supp_rates);
Zhu Yib481de92007-09-25 17:54:57 -07001629
1630 } else
1631 rate_scale_index_msk = rate_mask;
1632
1633 if (!rate_scale_index_msk)
1634 rate_scale_index_msk = rate_mask;
1635
Ben Cahill77626352007-11-29 11:09:44 +08001636 /* If current rate is no longer supported on current association,
1637 * or user changed preferences for rates, find a new supported rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001638 if (index < 0 || !((1 << index) & rate_scale_index_msk)) {
1639 index = IWL_INVALID_VALUE;
1640 update_lq = 1;
1641
Ben Cahill77626352007-11-29 11:09:44 +08001642 /* get the highest available rate */
Zhu Yib481de92007-09-25 17:54:57 -07001643 for (i = 0; i <= IWL_RATE_COUNT; i++) {
1644 if ((1 << i) & rate_scale_index_msk)
1645 index = i;
1646 }
1647
1648 if (index == IWL_INVALID_VALUE) {
1649 IWL_WARNING("Can not find a suitable rate\n");
1650 return;
1651 }
1652 }
1653
Ben Cahill77626352007-11-29 11:09:44 +08001654 /* Get expected throughput table and history window for current rate */
Zhu Yib481de92007-09-25 17:54:57 -07001655 if (!tbl->expected_tpt)
1656 rs_get_expected_tpt_table(lq_data, tbl);
1657
1658 window = &(tbl->win[index]);
1659
Ben Cahill77626352007-11-29 11:09:44 +08001660 /*
1661 * If there is not enough history to calculate actual average
1662 * throughput, keep analyzing results of more tx frames, without
1663 * changing rate or mode (bypass most of the rest of this function).
1664 * Set up new rate table in uCode only if old rate is not supported
1665 * in current association (use new rate found above).
1666 */
Zhu Yib481de92007-09-25 17:54:57 -07001667 fail_count = window->counter - window->success_counter;
1668 if (((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
1669 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH))
1670 || (tbl->expected_tpt == NULL)) {
1671 IWL_DEBUG_RATE("LQ: still below TH succ %d total %d "
1672 "for index %d\n",
1673 window->success_counter, window->counter, index);
Ben Cahill77626352007-11-29 11:09:44 +08001674
1675 /* Can't calculate this yet; not enough history */
Zhu Yib481de92007-09-25 17:54:57 -07001676 window->average_tpt = IWL_INVALID_VALUE;
Ben Cahill77626352007-11-29 11:09:44 +08001677
1678 /* Should we stay with this modulation mode,
1679 * or search for a new one? */
Zhu Yib481de92007-09-25 17:54:57 -07001680 rs_stay_in_table(lq_data);
Ben Cahill77626352007-11-29 11:09:44 +08001681
1682 /* Set up new rate table in uCode, if needed */
Zhu Yib481de92007-09-25 17:54:57 -07001683 if (update_lq) {
1684 rs_mcs_from_tbl(&mcs_rate, tbl, index, is_green);
Zhu Yi02dede02007-09-27 11:27:40 +08001685 rs_fill_link_cmd(lq_data, &mcs_rate, &lq_data->lq);
Zhu Yib481de92007-09-25 17:54:57 -07001686 rs_send_lq_cmd(priv, &lq_data->lq, CMD_ASYNC);
1687 }
1688 goto out;
1689
Ben Cahill77626352007-11-29 11:09:44 +08001690 /* Else we have enough samples; calculate estimate of
1691 * actual average throughput */
Zhu Yib481de92007-09-25 17:54:57 -07001692 } else
1693 window->average_tpt = ((window->success_ratio *
1694 tbl->expected_tpt[index] + 64) / 128);
1695
Ben Cahill77626352007-11-29 11:09:44 +08001696 /* If we are searching for better modulation mode, check success. */
Zhu Yib481de92007-09-25 17:54:57 -07001697 if (lq_data->search_better_tbl) {
1698 int success_limit = IWL_RATE_SCALE_SWITCH;
1699
Ben Cahill77626352007-11-29 11:09:44 +08001700 /* If good success, continue using the "search" mode;
1701 * no need to send new link quality command, since we're
1702 * continuing to use the setup that we've been trying. */
Zhu Yib481de92007-09-25 17:54:57 -07001703 if ((window->success_ratio > success_limit) ||
1704 (window->average_tpt > lq_data->last_tpt)) {
1705 if (!is_legacy(tbl->lq_type)) {
1706 IWL_DEBUG_HT("LQ: we are switching to HT"
1707 " rate suc %d current tpt %d"
1708 " old tpt %d\n",
1709 window->success_ratio,
1710 window->average_tpt,
1711 lq_data->last_tpt);
1712 lq_data->enable_counter = 1;
1713 }
Ben Cahill77626352007-11-29 11:09:44 +08001714 /* Swap tables; "search" becomes "active" */
Zhu Yib481de92007-09-25 17:54:57 -07001715 lq_data->active_tbl = active_tbl;
1716 current_tpt = window->average_tpt;
Ben Cahill77626352007-11-29 11:09:44 +08001717
1718 /* Else poor success; go back to mode in "active" table */
Zhu Yib481de92007-09-25 17:54:57 -07001719 } else {
Ben Cahill77626352007-11-29 11:09:44 +08001720 /* Nullify "search" table */
Zhu Yib481de92007-09-25 17:54:57 -07001721 tbl->lq_type = LQ_NONE;
Ben Cahill77626352007-11-29 11:09:44 +08001722
1723 /* Revert to "active" table */
Zhu Yib481de92007-09-25 17:54:57 -07001724 active_tbl = lq_data->active_tbl;
1725 tbl = &(lq_data->lq_info[active_tbl]);
1726
Ben Cahill77626352007-11-29 11:09:44 +08001727 /* Revert to "active" rate and throughput info */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001728 index = iwl4965_rate_index_from_plcp(
Zhu Yib481de92007-09-25 17:54:57 -07001729 tbl->current_rate.rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -07001730 current_tpt = lq_data->last_tpt;
Ben Cahill77626352007-11-29 11:09:44 +08001731
1732 /* Need to set up a new rate table in uCode */
1733 update_lq = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001734 IWL_DEBUG_HT("XXY GO BACK TO OLD TABLE\n");
1735 }
Ben Cahill77626352007-11-29 11:09:44 +08001736
1737 /* Either way, we've made a decision; modulation mode
1738 * search is done, allow rate adjustment next time. */
Zhu Yib481de92007-09-25 17:54:57 -07001739 lq_data->search_better_tbl = 0;
Ben Cahill77626352007-11-29 11:09:44 +08001740 done_search = 1; /* Don't switch modes below! */
Zhu Yib481de92007-09-25 17:54:57 -07001741 goto lq_update;
1742 }
1743
Ben Cahill77626352007-11-29 11:09:44 +08001744 /* (Else) not in search of better modulation mode, try for better
1745 * starting rate, while staying in this mode. */
Zhu Yib481de92007-09-25 17:54:57 -07001746 high_low = rs_get_adjacent_rate(index, rate_scale_index_msk,
1747 tbl->lq_type);
1748 low = high_low & 0xff;
1749 high = (high_low >> 8) & 0xff;
1750
Ben Cahill77626352007-11-29 11:09:44 +08001751 /* Collect measured throughputs for current and adjacent rates */
Zhu Yib481de92007-09-25 17:54:57 -07001752 current_tpt = window->average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07001753 if (low != IWL_RATE_INVALID)
1754 low_tpt = tbl->win[low].average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07001755 if (high != IWL_RATE_INVALID)
1756 high_tpt = tbl->win[high].average_tpt;
1757
Ben Cahill77626352007-11-29 11:09:44 +08001758 /* Assume rate increase */
Zhu Yib481de92007-09-25 17:54:57 -07001759 scale_action = 1;
1760
Ben Cahill77626352007-11-29 11:09:44 +08001761 /* Too many failures, decrease rate */
Zhu Yib481de92007-09-25 17:54:57 -07001762 if ((window->success_ratio <= IWL_RATE_DECREASE_TH) ||
1763 (current_tpt == 0)) {
1764 IWL_DEBUG_RATE("decrease rate because of low success_ratio\n");
1765 scale_action = -1;
Ben Cahill77626352007-11-29 11:09:44 +08001766
1767 /* No throughput measured yet for adjacent rates; try increase. */
Zhu Yib481de92007-09-25 17:54:57 -07001768 } else if ((low_tpt == IWL_INVALID_VALUE) &&
1769 (high_tpt == IWL_INVALID_VALUE))
1770 scale_action = 1;
Ben Cahill77626352007-11-29 11:09:44 +08001771
1772 /* Both adjacent throughputs are measured, but neither one has better
1773 * throughput; we're using the best rate, don't change it! */
Zhu Yib481de92007-09-25 17:54:57 -07001774 else if ((low_tpt != IWL_INVALID_VALUE) &&
1775 (high_tpt != IWL_INVALID_VALUE) &&
1776 (low_tpt < current_tpt) &&
1777 (high_tpt < current_tpt))
1778 scale_action = 0;
Ben Cahill77626352007-11-29 11:09:44 +08001779
1780 /* At least one adjacent rate's throughput is measured,
1781 * and may have better performance. */
Zhu Yib481de92007-09-25 17:54:57 -07001782 else {
Ben Cahill77626352007-11-29 11:09:44 +08001783 /* Higher adjacent rate's throughput is measured */
Zhu Yib481de92007-09-25 17:54:57 -07001784 if (high_tpt != IWL_INVALID_VALUE) {
Ben Cahill77626352007-11-29 11:09:44 +08001785 /* Higher rate has better throughput */
Zhu Yib481de92007-09-25 17:54:57 -07001786 if (high_tpt > current_tpt)
1787 scale_action = 1;
1788 else {
1789 IWL_DEBUG_RATE
1790 ("decrease rate because of high tpt\n");
1791 scale_action = -1;
1792 }
Ben Cahill77626352007-11-29 11:09:44 +08001793
1794 /* Lower adjacent rate's throughput is measured */
Zhu Yib481de92007-09-25 17:54:57 -07001795 } else if (low_tpt != IWL_INVALID_VALUE) {
Ben Cahill77626352007-11-29 11:09:44 +08001796 /* Lower rate has better throughput */
Zhu Yib481de92007-09-25 17:54:57 -07001797 if (low_tpt > current_tpt) {
1798 IWL_DEBUG_RATE
1799 ("decrease rate because of low tpt\n");
1800 scale_action = -1;
1801 } else
1802 scale_action = 1;
1803 }
1804 }
1805
Ben Cahill77626352007-11-29 11:09:44 +08001806 /* Sanity check; asked for decrease, but success rate or throughput
1807 * has been good at old rate. Don't change it. */
Zhu Yib481de92007-09-25 17:54:57 -07001808 if (scale_action == -1) {
1809 if ((low != IWL_RATE_INVALID) &&
1810 ((window->success_ratio > IWL_RATE_HIGH_TH) ||
1811 (current_tpt > (100 * tbl->expected_tpt[low]))))
1812 scale_action = 0;
Ben Cahill77626352007-11-29 11:09:44 +08001813
1814 /* Sanity check; asked for increase, but success rate has not been great
1815 * even at old rate, higher rate will be worse. Don't change it. */
Zhu Yib481de92007-09-25 17:54:57 -07001816 } else if ((scale_action == 1) &&
1817 (window->success_ratio < IWL_RATE_INCREASE_TH))
1818 scale_action = 0;
1819
1820 switch (scale_action) {
1821 case -1:
Ben Cahill77626352007-11-29 11:09:44 +08001822 /* Decrease starting rate, update uCode's rate table */
Zhu Yib481de92007-09-25 17:54:57 -07001823 if (low != IWL_RATE_INVALID) {
1824 update_lq = 1;
1825 index = low;
1826 }
1827 break;
1828 case 1:
Ben Cahill77626352007-11-29 11:09:44 +08001829 /* Increase starting rate, update uCode's rate table */
Zhu Yib481de92007-09-25 17:54:57 -07001830 if (high != IWL_RATE_INVALID) {
1831 update_lq = 1;
1832 index = high;
1833 }
1834
1835 break;
1836 case 0:
Ben Cahill77626352007-11-29 11:09:44 +08001837 /* No change */
Zhu Yib481de92007-09-25 17:54:57 -07001838 default:
1839 break;
1840 }
1841
1842 IWL_DEBUG_HT("choose rate scale index %d action %d low %d "
1843 "high %d type %d\n",
1844 index, scale_action, low, high, tbl->lq_type);
1845
1846 lq_update:
Ben Cahill77626352007-11-29 11:09:44 +08001847 /* Replace uCode's rate table for the destination station. */
Zhu Yib481de92007-09-25 17:54:57 -07001848 if (update_lq) {
1849 rs_mcs_from_tbl(&mcs_rate, tbl, index, is_green);
Zhu Yi02dede02007-09-27 11:27:40 +08001850 rs_fill_link_cmd(lq_data, &mcs_rate, &lq_data->lq);
Zhu Yib481de92007-09-25 17:54:57 -07001851 rs_send_lq_cmd(priv, &lq_data->lq, CMD_ASYNC);
1852 }
Ben Cahill77626352007-11-29 11:09:44 +08001853
1854 /* Should we stay with this modulation mode, or search for a new one? */
Zhu Yib481de92007-09-25 17:54:57 -07001855 rs_stay_in_table(lq_data);
1856
Ben Cahill77626352007-11-29 11:09:44 +08001857 /*
1858 * Search for new modulation mode if we're:
1859 * 1) Not changing rates right now
1860 * 2) Not just finishing up a search
1861 * 3) Allowing a new search
1862 */
Zhu Yib481de92007-09-25 17:54:57 -07001863 if (!update_lq && !done_search && !lq_data->stay_in_tbl) {
Ben Cahill77626352007-11-29 11:09:44 +08001864 /* Save current throughput to compare with "search" throughput*/
Zhu Yib481de92007-09-25 17:54:57 -07001865 lq_data->last_tpt = current_tpt;
1866
Ben Cahill77626352007-11-29 11:09:44 +08001867 /* Select a new "search" modulation mode to try.
1868 * If one is found, set up the new "search" table. */
Zhu Yib481de92007-09-25 17:54:57 -07001869 if (is_legacy(tbl->lq_type))
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001870 rs_move_legacy_other(priv, lq_data, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07001871 else if (is_siso(tbl->lq_type))
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001872 rs_move_siso_to_other(priv, lq_data, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07001873 else
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001874 rs_move_mimo_to_other(priv, lq_data, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07001875
Ben Cahill77626352007-11-29 11:09:44 +08001876 /* If new "search" mode was selected, set up in uCode table */
Zhu Yib481de92007-09-25 17:54:57 -07001877 if (lq_data->search_better_tbl) {
Ben Cahill77626352007-11-29 11:09:44 +08001878 /* Access the "search" table, clear its history. */
Zhu Yib481de92007-09-25 17:54:57 -07001879 tbl = &(lq_data->lq_info[(1 - lq_data->active_tbl)]);
1880 for (i = 0; i < IWL_RATE_COUNT; i++)
1881 rs_rate_scale_clear_window(&(tbl->win[i]));
1882
Ben Cahill77626352007-11-29 11:09:44 +08001883 /* Use new "search" start rate */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001884 index = iwl4965_rate_index_from_plcp(
Zhu Yib481de92007-09-25 17:54:57 -07001885 tbl->current_rate.rate_n_flags);
1886
1887 IWL_DEBUG_HT("Switch current mcs: %X index: %d\n",
1888 tbl->current_rate.rate_n_flags, index);
1889 rs_fill_link_cmd(lq_data, &tbl->current_rate,
Zhu Yi02dede02007-09-27 11:27:40 +08001890 &lq_data->lq);
Zhu Yib481de92007-09-25 17:54:57 -07001891 rs_send_lq_cmd(priv, &lq_data->lq, CMD_ASYNC);
1892 }
Zhu Yib481de92007-09-25 17:54:57 -07001893
Ben Cahill77626352007-11-29 11:09:44 +08001894 /* If the "active" (non-search) mode was legacy,
1895 * and we've tried switching antennas,
1896 * but we haven't been able to try HT modes (not available),
1897 * stay with best antenna legacy modulation for a while
1898 * before next round of mode comparisons. */
1899 tbl1 = &(lq_data->lq_info[lq_data->active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07001900 if (is_legacy(tbl1->lq_type) &&
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001901#ifdef CONFIG_IWL4965_HT
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001902 (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE)) &&
Zhu Yib481de92007-09-25 17:54:57 -07001903#endif
1904 (lq_data->action_counter >= 1)) {
1905 lq_data->action_counter = 0;
1906 IWL_DEBUG_HT("LQ: STAY in legacy table\n");
1907 rs_set_stay_in_table(1, lq_data);
1908 }
1909
Ben Cahill77626352007-11-29 11:09:44 +08001910 /* If we're in an HT mode, and all 3 mode switch actions
1911 * have been tried and compared, stay in this best modulation
1912 * mode for a while before next round of mode comparisons. */
Zhu Yib481de92007-09-25 17:54:57 -07001913 if (lq_data->enable_counter &&
1914 (lq_data->action_counter >= IWL_ACTION_LIMIT)) {
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001915#ifdef CONFIG_IWL4965_HT_AGG
Ben Cahill77626352007-11-29 11:09:44 +08001916 /* If appropriate, set up aggregation! */
Zhu Yib481de92007-09-25 17:54:57 -07001917 if ((lq_data->last_tpt > TID_AGG_TPT_THREHOLD) &&
1918 (priv->lq_mngr.agg_ctrl.auto_agg)) {
1919 priv->lq_mngr.agg_ctrl.tid_retry =
1920 TID_ALL_SPECIFIED;
1921 schedule_work(&priv->agg_work);
1922 }
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001923#endif /*CONFIG_IWL4965_HT_AGG */
Zhu Yib481de92007-09-25 17:54:57 -07001924 lq_data->action_counter = 0;
1925 rs_set_stay_in_table(0, lq_data);
1926 }
Ben Cahill77626352007-11-29 11:09:44 +08001927
1928 /*
1929 * Else, don't search for a new modulation mode.
1930 * Put new timestamp in stay-in-modulation-mode flush timer if:
1931 * 1) Not changing rates right now
1932 * 2) Not just finishing up a search
1933 * 3) flush timer is empty
1934 */
Zhu Yib481de92007-09-25 17:54:57 -07001935 } else {
1936 if ((!update_lq) && (!done_search) && (!lq_data->flush_timer))
1937 lq_data->flush_timer = jiffies;
1938 }
1939
1940out:
1941 rs_mcs_from_tbl(&tbl->current_rate, tbl, index, is_green);
1942 i = index;
1943 sta->last_txrate = i;
1944
1945 /* sta->txrate is an index to A mode rates which start
1946 * at IWL_FIRST_OFDM_RATE
1947 */
1948 if (lq_data->phymode == (u8) MODE_IEEE80211A)
1949 sta->txrate = i - IWL_FIRST_OFDM_RATE;
1950 else
1951 sta->txrate = i;
1952
1953 return;
1954}
1955
1956
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001957static void rs_initialize_lq(struct iwl4965_priv *priv,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001958 struct ieee80211_conf *conf,
Zhu Yib481de92007-09-25 17:54:57 -07001959 struct sta_info *sta)
1960{
1961 int i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001962 struct iwl4965_rate_scale_priv *lq;
1963 struct iwl4965_scale_tbl_info *tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001964 u8 active_tbl = 0;
1965 int rate_idx;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001966 u8 use_green = rs_use_green(priv, conf);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001967 struct iwl4965_rate mcs_rate;
Zhu Yib481de92007-09-25 17:54:57 -07001968
1969 if (!sta || !sta->rate_ctrl_priv)
1970 goto out;
1971
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001972 lq = (struct iwl4965_rate_scale_priv *)sta->rate_ctrl_priv;
Zhu Yib481de92007-09-25 17:54:57 -07001973 i = sta->last_txrate;
1974
1975 if ((lq->lq.sta_id == 0xff) &&
1976 (priv->iw_mode == IEEE80211_IF_TYPE_IBSS))
1977 goto out;
1978
1979 if (!lq->search_better_tbl)
1980 active_tbl = lq->active_tbl;
1981 else
1982 active_tbl = 1 - lq->active_tbl;
1983
1984 tbl = &(lq->lq_info[active_tbl]);
1985
1986 if ((i < 0) || (i >= IWL_RATE_COUNT))
1987 i = 0;
1988
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001989 mcs_rate.rate_n_flags = iwl4965_rates[i].plcp ;
Zhu Yib481de92007-09-25 17:54:57 -07001990 mcs_rate.rate_n_flags |= RATE_MCS_ANT_B_MSK;
1991 mcs_rate.rate_n_flags &= ~RATE_MCS_ANT_A_MSK;
1992
1993 if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
1994 mcs_rate.rate_n_flags |= RATE_MCS_CCK_MSK;
1995
1996 tbl->antenna_type = ANT_AUX;
1997 rs_get_tbl_info_from_mcs(&mcs_rate, priv->phymode, tbl, &rate_idx);
1998 if (!rs_is_ant_connected(priv->valid_antenna, tbl->antenna_type))
Zhu Yi46640a82007-09-27 11:27:34 +08001999 rs_toggle_antenna(&mcs_rate, tbl);
Zhu Yib481de92007-09-25 17:54:57 -07002000
2001 rs_mcs_from_tbl(&mcs_rate, tbl, rate_idx, use_green);
2002 tbl->current_rate.rate_n_flags = mcs_rate.rate_n_flags;
2003 rs_get_expected_tpt_table(lq, tbl);
Zhu Yi02dede02007-09-27 11:27:40 +08002004 rs_fill_link_cmd(lq, &mcs_rate, &lq->lq);
Zhu Yib481de92007-09-25 17:54:57 -07002005 rs_send_lq_cmd(priv, &lq->lq, CMD_ASYNC);
2006 out:
2007 return;
2008}
2009
Mattias Nissler1abbe492007-12-20 13:50:07 +01002010static void rs_get_rate(void *priv_rate, struct net_device *dev,
2011 struct ieee80211_hw_mode *mode, struct sk_buff *skb,
2012 struct rate_selection *sel)
Zhu Yib481de92007-09-25 17:54:57 -07002013{
2014
2015 int i;
2016 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002017 struct ieee80211_conf *conf = &local->hw.conf;
Zhu Yib481de92007-09-25 17:54:57 -07002018 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2019 struct sta_info *sta;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002020 struct iwl4965_priv *priv = (struct iwl4965_priv *)priv_rate;
2021 struct iwl4965_rate_scale_priv *lq;
Zhu Yib481de92007-09-25 17:54:57 -07002022
Zhu Yi58826352007-09-27 11:27:39 +08002023 IWL_DEBUG_RATE_LIMIT("rate scale calculate new rate for skb\n");
Zhu Yib481de92007-09-25 17:54:57 -07002024
Zhu Yib481de92007-09-25 17:54:57 -07002025 sta = sta_info_get(local, hdr->addr1);
2026
2027 if (!sta || !sta->rate_ctrl_priv) {
Mattias Nissler1abbe492007-12-20 13:50:07 +01002028 sel->rate = rate_lowest(local, local->oper_hw_mode, sta);
Zhu Yib481de92007-09-25 17:54:57 -07002029 if (sta)
2030 sta_info_put(sta);
Mattias Nissler1abbe492007-12-20 13:50:07 +01002031 return;
Zhu Yib481de92007-09-25 17:54:57 -07002032 }
2033
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002034 lq = (struct iwl4965_rate_scale_priv *)sta->rate_ctrl_priv;
Zhu Yib481de92007-09-25 17:54:57 -07002035 i = sta->last_txrate;
2036
2037 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) && !lq->ibss_sta_added) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002038 u8 sta_id = iwl4965_hw_find_station(priv, hdr->addr1);
Joe Perches0795af52007-10-03 17:59:30 -07002039 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07002040
2041 if (sta_id == IWL_INVALID_STATION) {
Joe Perches0795af52007-10-03 17:59:30 -07002042 IWL_DEBUG_RATE("LQ: ADD station %s\n",
2043 print_mac(mac, hdr->addr1));
Ron Rindjunsky67d62032007-11-26 16:14:40 +02002044 sta_id = iwl4965_add_station_flags(priv, hdr->addr1,
2045 0, CMD_ASYNC, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07002046 }
2047 if ((sta_id != IWL_INVALID_STATION)) {
2048 lq->lq.sta_id = sta_id;
2049 lq->lq.rs_table[0].rate_n_flags = 0;
2050 lq->ibss_sta_added = 1;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002051 rs_initialize_lq(priv, conf, sta);
Zhu Yib481de92007-09-25 17:54:57 -07002052 }
2053 if (!lq->ibss_sta_added)
2054 goto done;
2055 }
2056
2057 done:
Mattias Nissler1abbe492007-12-20 13:50:07 +01002058 if ((i < 0) || (i > IWL_RATE_COUNT)) {
2059 sel->rate = rate_lowest(local, local->oper_hw_mode, sta);
2060 return;
2061 }
Zhu Yib481de92007-09-25 17:54:57 -07002062 sta_info_put(sta);
Zhu Yib481de92007-09-25 17:54:57 -07002063
Mattias Nissler1abbe492007-12-20 13:50:07 +01002064 sel->rate = &priv->ieee_rates[i];
Zhu Yib481de92007-09-25 17:54:57 -07002065}
2066
2067static void *rs_alloc_sta(void *priv, gfp_t gfp)
2068{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002069 struct iwl4965_rate_scale_priv *crl;
Zhu Yib481de92007-09-25 17:54:57 -07002070 int i, j;
2071
2072 IWL_DEBUG_RATE("create station rate scale window\n");
2073
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002074 crl = kzalloc(sizeof(struct iwl4965_rate_scale_priv), gfp);
Zhu Yib481de92007-09-25 17:54:57 -07002075
2076 if (crl == NULL)
2077 return NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002078 crl->lq.sta_id = 0xff;
2079
Zhu Yi63fddb92007-09-27 11:27:36 +08002080
Zhu Yib481de92007-09-25 17:54:57 -07002081 for (j = 0; j < LQ_SIZE; j++)
2082 for (i = 0; i < IWL_RATE_COUNT; i++)
2083 rs_rate_scale_clear_window(&(crl->lq_info[j].win[i]));
2084
2085 return crl;
2086}
2087
2088static void rs_rate_init(void *priv_rate, void *priv_sta,
2089 struct ieee80211_local *local,
2090 struct sta_info *sta)
2091{
2092 int i, j;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002093 struct ieee80211_conf *conf = &local->hw.conf;
Zhu Yib481de92007-09-25 17:54:57 -07002094 struct ieee80211_hw_mode *mode = local->oper_hw_mode;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002095 struct iwl4965_priv *priv = (struct iwl4965_priv *)priv_rate;
2096 struct iwl4965_rate_scale_priv *crl = priv_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002097
Zhu Yib481de92007-09-25 17:54:57 -07002098 crl->flush_timer = 0;
Zhu Yi02dede02007-09-27 11:27:40 +08002099 crl->supp_rates = sta->supp_rates;
Zhu Yib481de92007-09-25 17:54:57 -07002100 sta->txrate = 3;
2101 for (j = 0; j < LQ_SIZE; j++)
2102 for (i = 0; i < IWL_RATE_COUNT; i++)
2103 rs_rate_scale_clear_window(&(crl->lq_info[j].win[i]));
2104
2105 IWL_DEBUG_RATE("rate scale global init\n");
2106 /* TODO: what is a good starting rate for STA? About middle? Maybe not
2107 * the lowest or the highest rate.. Could consider using RSSI from
2108 * previous packets? Need to have IEEE 802.1X auth succeed immediately
2109 * after assoc.. */
2110
2111 crl->ibss_sta_added = 0;
2112 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002113 u8 sta_id = iwl4965_hw_find_station(priv, sta->addr);
Joe Perches0795af52007-10-03 17:59:30 -07002114 DECLARE_MAC_BUF(mac);
2115
Zhu Yib481de92007-09-25 17:54:57 -07002116 /* for IBSS the call are from tasklet */
Joe Perches0795af52007-10-03 17:59:30 -07002117 IWL_DEBUG_HT("LQ: ADD station %s\n",
2118 print_mac(mac, sta->addr));
Zhu Yib481de92007-09-25 17:54:57 -07002119
2120 if (sta_id == IWL_INVALID_STATION) {
Joe Perches0795af52007-10-03 17:59:30 -07002121 IWL_DEBUG_RATE("LQ: ADD station %s\n",
2122 print_mac(mac, sta->addr));
Ron Rindjunsky67d62032007-11-26 16:14:40 +02002123 sta_id = iwl4965_add_station_flags(priv, sta->addr,
2124 0, CMD_ASYNC, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07002125 }
2126 if ((sta_id != IWL_INVALID_STATION)) {
2127 crl->lq.sta_id = sta_id;
2128 crl->lq.rs_table[0].rate_n_flags = 0;
2129 }
2130 /* FIXME: this is w/a remove it later */
2131 priv->assoc_station_added = 1;
2132 }
2133
Ben Cahill77626352007-11-29 11:09:44 +08002134 /* Find highest tx rate supported by hardware and destination station */
Zhu Yib481de92007-09-25 17:54:57 -07002135 for (i = 0; i < mode->num_rates; i++) {
2136 if ((sta->supp_rates & BIT(i)) &&
2137 (mode->rates[i].flags & IEEE80211_RATE_SUPPORTED))
2138 sta->txrate = i;
2139 }
2140 sta->last_txrate = sta->txrate;
Ben Cahill77626352007-11-29 11:09:44 +08002141 /* For MODE_IEEE80211A, cck rates are at end of rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002142 if (local->hw.conf.phymode == MODE_IEEE80211A)
2143 sta->last_txrate += IWL_FIRST_OFDM_RATE;
2144
Ron Rindjunsky9e0cc6d2007-11-26 16:14:36 +02002145 crl->is_dup = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002146 crl->valid_antenna = priv->valid_antenna;
2147 crl->antenna = priv->antenna;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002148 crl->is_green = rs_use_green(priv, conf);
Zhu Yib481de92007-09-25 17:54:57 -07002149 crl->active_rate = priv->active_rate;
2150 crl->active_rate &= ~(0x1000);
2151 crl->active_rate_basic = priv->active_rate_basic;
2152 crl->phymode = priv->phymode;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002153#ifdef CONFIG_IWL4965_HT
Ben Cahill77626352007-11-29 11:09:44 +08002154 /*
2155 * active_siso_rate mask includes 9 MBits (bit 5), and CCK (bits 0-3),
2156 * supp_rates[] does not; shift to convert format, force 9 MBits off.
2157 */
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002158 crl->active_siso_rate = (priv->current_ht_config.supp_mcs_set[0] << 1);
2159 crl->active_siso_rate |=
2160 (priv->current_ht_config.supp_mcs_set[0] & 0x1);
Zhu Yib481de92007-09-25 17:54:57 -07002161 crl->active_siso_rate &= ~((u16)0x2);
2162 crl->active_siso_rate = crl->active_siso_rate << IWL_FIRST_OFDM_RATE;
2163
Ben Cahill77626352007-11-29 11:09:44 +08002164 /* Same here */
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002165 crl->active_mimo_rate = (priv->current_ht_config.supp_mcs_set[1] << 1);
2166 crl->active_mimo_rate |=
2167 (priv->current_ht_config.supp_mcs_set[1] & 0x1);
Zhu Yib481de92007-09-25 17:54:57 -07002168 crl->active_mimo_rate &= ~((u16)0x2);
2169 crl->active_mimo_rate = crl->active_mimo_rate << IWL_FIRST_OFDM_RATE;
2170 IWL_DEBUG_HT("MIMO RATE 0x%X SISO MASK 0x%X\n", crl->active_siso_rate,
2171 crl->active_mimo_rate);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002172#endif /*CONFIG_IWL4965_HT*/
Zhu Yi98d7e092007-09-27 11:27:42 +08002173#ifdef CONFIG_MAC80211_DEBUGFS
2174 crl->drv = priv;
2175#endif
Zhu Yib481de92007-09-25 17:54:57 -07002176
2177 if (priv->assoc_station_added)
2178 priv->lq_mngr.lq_ready = 1;
2179
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002180 rs_initialize_lq(priv, conf, sta);
Zhu Yib481de92007-09-25 17:54:57 -07002181}
2182
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002183static void rs_fill_link_cmd(struct iwl4965_rate_scale_priv *lq_data,
2184 struct iwl4965_rate *tx_mcs,
2185 struct iwl4965_link_quality_cmd *lq_cmd)
Zhu Yib481de92007-09-25 17:54:57 -07002186{
2187 int index = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002188 int rate_idx;
Zhu Yi1b696de2007-09-27 11:27:41 +08002189 int repeat_rate = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002190 u8 ant_toggle_count = 0;
2191 u8 use_ht_possible = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002192 struct iwl4965_rate new_rate;
2193 struct iwl4965_scale_tbl_info tbl_type = { 0 };
Zhu Yib481de92007-09-25 17:54:57 -07002194
Ben Cahill77626352007-11-29 11:09:44 +08002195 /* Override starting rate (index 0) if needed for debug purposes */
Zhu Yi98d7e092007-09-27 11:27:42 +08002196 rs_dbgfs_set_mcs(lq_data, tx_mcs, index);
2197
Ben Cahill77626352007-11-29 11:09:44 +08002198 /* Interpret rate_n_flags */
Zhu Yib481de92007-09-25 17:54:57 -07002199 rs_get_tbl_info_from_mcs(tx_mcs, lq_data->phymode,
2200 &tbl_type, &rate_idx);
2201
Ben Cahill77626352007-11-29 11:09:44 +08002202 /* How many times should we repeat the initial rate? */
Zhu Yib481de92007-09-25 17:54:57 -07002203 if (is_legacy(tbl_type.lq_type)) {
2204 ant_toggle_count = 1;
Zhu Yi1b696de2007-09-27 11:27:41 +08002205 repeat_rate = IWL_NUMBER_TRY;
Zhu Yib481de92007-09-25 17:54:57 -07002206 } else
Zhu Yi1b696de2007-09-27 11:27:41 +08002207 repeat_rate = IWL_HT_NUMBER_TRY;
Zhu Yib481de92007-09-25 17:54:57 -07002208
2209 lq_cmd->general_params.mimo_delimiter =
2210 is_mimo(tbl_type.lq_type) ? 1 : 0;
Ben Cahill77626352007-11-29 11:09:44 +08002211
2212 /* Fill 1st table entry (index 0) */
Zhu Yib481de92007-09-25 17:54:57 -07002213 lq_cmd->rs_table[index].rate_n_flags =
2214 cpu_to_le32(tx_mcs->rate_n_flags);
2215 new_rate.rate_n_flags = tx_mcs->rate_n_flags;
2216
2217 if (is_mimo(tbl_type.lq_type) || (tbl_type.antenna_type == ANT_MAIN))
Ben Cahill77626352007-11-29 11:09:44 +08002218 lq_cmd->general_params.single_stream_ant_msk
2219 = LINK_QUAL_ANT_A_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002220 else
Ben Cahill77626352007-11-29 11:09:44 +08002221 lq_cmd->general_params.single_stream_ant_msk
2222 = LINK_QUAL_ANT_B_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002223
2224 index++;
Zhu Yi1b696de2007-09-27 11:27:41 +08002225 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002226
Ben Cahill77626352007-11-29 11:09:44 +08002227 /* Fill rest of rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002228 while (index < LINK_QUAL_MAX_RETRY_NUM) {
Ben Cahill77626352007-11-29 11:09:44 +08002229 /* Repeat initial/next rate.
2230 * For legacy IWL_NUMBER_TRY == 1, this loop will not execute.
2231 * For HT IWL_HT_NUMBER_TRY == 3, this executes twice. */
Zhu Yi1b696de2007-09-27 11:27:41 +08002232 while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
Zhu Yib481de92007-09-25 17:54:57 -07002233 if (is_legacy(tbl_type.lq_type)) {
2234 if (ant_toggle_count <
2235 NUM_TRY_BEFORE_ANTENNA_TOGGLE)
2236 ant_toggle_count++;
2237 else {
2238 rs_toggle_antenna(&new_rate, &tbl_type);
2239 ant_toggle_count = 1;
2240 }
2241 }
Zhu Yi98d7e092007-09-27 11:27:42 +08002242
Ben Cahill77626352007-11-29 11:09:44 +08002243 /* Override next rate if needed for debug purposes */
Zhu Yi98d7e092007-09-27 11:27:42 +08002244 rs_dbgfs_set_mcs(lq_data, &new_rate, index);
Ben Cahill77626352007-11-29 11:09:44 +08002245
2246 /* Fill next table entry */
Zhu Yib481de92007-09-25 17:54:57 -07002247 lq_cmd->rs_table[index].rate_n_flags =
2248 cpu_to_le32(new_rate.rate_n_flags);
Zhu Yi1b696de2007-09-27 11:27:41 +08002249 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002250 index++;
2251 }
2252
2253 rs_get_tbl_info_from_mcs(&new_rate, lq_data->phymode, &tbl_type,
2254 &rate_idx);
2255
Ben Cahill77626352007-11-29 11:09:44 +08002256 /* Indicate to uCode which entries might be MIMO.
2257 * If initial rate was MIMO, this will finally end up
2258 * as (IWL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
Zhu Yib481de92007-09-25 17:54:57 -07002259 if (is_mimo(tbl_type.lq_type))
2260 lq_cmd->general_params.mimo_delimiter = index;
2261
Ben Cahill77626352007-11-29 11:09:44 +08002262 /* Get next rate */
Zhu Yib481de92007-09-25 17:54:57 -07002263 rs_get_lower_rate(lq_data, &tbl_type, rate_idx,
Zhu Yi02dede02007-09-27 11:27:40 +08002264 use_ht_possible, &new_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002265
Ben Cahill77626352007-11-29 11:09:44 +08002266 /* How many times should we repeat the next rate? */
Zhu Yib481de92007-09-25 17:54:57 -07002267 if (is_legacy(tbl_type.lq_type)) {
2268 if (ant_toggle_count < NUM_TRY_BEFORE_ANTENNA_TOGGLE)
2269 ant_toggle_count++;
2270 else {
2271 rs_toggle_antenna(&new_rate, &tbl_type);
2272 ant_toggle_count = 1;
2273 }
Zhu Yi1b696de2007-09-27 11:27:41 +08002274 repeat_rate = IWL_NUMBER_TRY;
Zhu Yib481de92007-09-25 17:54:57 -07002275 } else
Zhu Yi1b696de2007-09-27 11:27:41 +08002276 repeat_rate = IWL_HT_NUMBER_TRY;
Zhu Yib481de92007-09-25 17:54:57 -07002277
Ben Cahill77626352007-11-29 11:09:44 +08002278 /* Don't allow HT rates after next pass.
2279 * rs_get_lower_rate() will change type to LQ_A or LQ_G. */
Zhu Yib481de92007-09-25 17:54:57 -07002280 use_ht_possible = 0;
2281
Ben Cahill77626352007-11-29 11:09:44 +08002282 /* Override next rate if needed for debug purposes */
Zhu Yi98d7e092007-09-27 11:27:42 +08002283 rs_dbgfs_set_mcs(lq_data, &new_rate, index);
Ben Cahill77626352007-11-29 11:09:44 +08002284
2285 /* Fill next table entry */
Zhu Yib481de92007-09-25 17:54:57 -07002286 lq_cmd->rs_table[index].rate_n_flags =
2287 cpu_to_le32(new_rate.rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -07002288
2289 index++;
Zhu Yi1b696de2007-09-27 11:27:41 +08002290 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002291 }
2292
Zhu Yib481de92007-09-25 17:54:57 -07002293 lq_cmd->general_params.dual_stream_ant_msk = 3;
2294 lq_cmd->agg_params.agg_dis_start_th = 3;
2295 lq_cmd->agg_params.agg_time_limit = cpu_to_le16(4000);
Zhu Yib481de92007-09-25 17:54:57 -07002296}
2297
2298static void *rs_alloc(struct ieee80211_local *local)
2299{
2300 return local->hw.priv;
2301}
2302/* rate scale requires free function to be implemented */
2303static void rs_free(void *priv_rate)
2304{
2305 return;
2306}
2307
2308static void rs_clear(void *priv_rate)
2309{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002310 struct iwl4965_priv *priv = (struct iwl4965_priv *) priv_rate;
Zhu Yib481de92007-09-25 17:54:57 -07002311
2312 IWL_DEBUG_RATE("enter\n");
2313
2314 priv->lq_mngr.lq_ready = 0;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002315#ifdef CONFIG_IWL4965_HT
2316#ifdef CONFIG_IWL4965_HT_AGG
Zhu Yib481de92007-09-25 17:54:57 -07002317 if (priv->lq_mngr.agg_ctrl.granted_ba)
2318 iwl4965_turn_off_agg(priv, TID_ALL_SPECIFIED);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002319#endif /*CONFIG_IWL4965_HT_AGG */
2320#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07002321
2322 IWL_DEBUG_RATE("leave\n");
2323}
2324
2325static void rs_free_sta(void *priv, void *priv_sta)
2326{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002327 struct iwl4965_rate_scale_priv *rs_priv = priv_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002328
2329 IWL_DEBUG_RATE("enter\n");
2330 kfree(rs_priv);
2331 IWL_DEBUG_RATE("leave\n");
2332}
2333
2334
Zhu Yi93dc6462007-09-27 11:27:37 +08002335#ifdef CONFIG_MAC80211_DEBUGFS
Zhu Yi5ae212c2007-09-27 11:27:38 +08002336static int open_file_generic(struct inode *inode, struct file *file)
2337{
2338 file->private_data = inode->i_private;
2339 return 0;
2340}
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002341static void rs_dbgfs_set_mcs(struct iwl4965_rate_scale_priv *rs_priv,
2342 struct iwl4965_rate *mcs, int index)
Zhu Yi98d7e092007-09-27 11:27:42 +08002343{
Ron Rindjunsky4457e1a2007-10-15 14:40:56 +02002344 u32 base_rate;
2345
2346 if (rs_priv->phymode == (u8) MODE_IEEE80211A)
2347 base_rate = 0x800D;
2348 else
2349 base_rate = 0x820A;
2350
Zhu Yi98d7e092007-09-27 11:27:42 +08002351 if (rs_priv->dbg_fixed.rate_n_flags) {
2352 if (index < 12)
2353 mcs->rate_n_flags = rs_priv->dbg_fixed.rate_n_flags;
2354 else
Ron Rindjunsky4457e1a2007-10-15 14:40:56 +02002355 mcs->rate_n_flags = base_rate;
Zhu Yi98d7e092007-09-27 11:27:42 +08002356 IWL_DEBUG_RATE("Fixed rate ON\n");
2357 return;
2358 }
Zhu Yi5ae212c2007-09-27 11:27:38 +08002359
Zhu Yi98d7e092007-09-27 11:27:42 +08002360 IWL_DEBUG_RATE("Fixed rate OFF\n");
2361}
2362
2363static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
2364 const char __user *user_buf, size_t count, loff_t *ppos)
2365{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002366 struct iwl4965_rate_scale_priv *rs_priv = file->private_data;
Zhu Yi98d7e092007-09-27 11:27:42 +08002367 char buf[64];
2368 int buf_size;
2369 u32 parsed_rate;
2370
2371 memset(buf, 0, sizeof(buf));
2372 buf_size = min(count, sizeof(buf) - 1);
2373 if (copy_from_user(buf, user_buf, buf_size))
2374 return -EFAULT;
2375
2376 if (sscanf(buf, "%x", &parsed_rate) == 1)
2377 rs_priv->dbg_fixed.rate_n_flags = parsed_rate;
2378 else
2379 rs_priv->dbg_fixed.rate_n_flags = 0;
2380
Ben Cahill77626352007-11-29 11:09:44 +08002381 rs_priv->active_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
2382 rs_priv->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2383 rs_priv->active_mimo_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
Zhu Yi98d7e092007-09-27 11:27:42 +08002384
2385 IWL_DEBUG_RATE("sta_id %d rate 0x%X\n",
2386 rs_priv->lq.sta_id, rs_priv->dbg_fixed.rate_n_flags);
2387
2388 if (rs_priv->dbg_fixed.rate_n_flags) {
2389 rs_fill_link_cmd(rs_priv, &rs_priv->dbg_fixed, &rs_priv->lq);
2390 rs_send_lq_cmd(rs_priv->drv, &rs_priv->lq, CMD_ASYNC);
2391 }
2392
2393 return count;
2394}
Zhu Yi0209dc12007-09-27 11:27:43 +08002395
Zhu Yi5ae212c2007-09-27 11:27:38 +08002396static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
2397 char __user *user_buf, size_t count, loff_t *ppos)
2398{
2399 char buff[1024];
2400 int desc = 0;
2401 int i = 0;
2402
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002403 struct iwl4965_rate_scale_priv *rs_priv = file->private_data;
Zhu Yi5ae212c2007-09-27 11:27:38 +08002404
2405 desc += sprintf(buff+desc, "sta_id %d\n", rs_priv->lq.sta_id);
Zhu Yi98d7e092007-09-27 11:27:42 +08002406 desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
Zhu Yi5ae212c2007-09-27 11:27:38 +08002407 rs_priv->total_failed, rs_priv->total_success,
2408 rs_priv->active_rate);
Zhu Yi98d7e092007-09-27 11:27:42 +08002409 desc += sprintf(buff+desc, "fixed rate 0x%X\n",
2410 rs_priv->dbg_fixed.rate_n_flags);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002411 desc += sprintf(buff+desc, "general:"
2412 "flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
2413 rs_priv->lq.general_params.flags,
2414 rs_priv->lq.general_params.mimo_delimiter,
2415 rs_priv->lq.general_params.single_stream_ant_msk,
2416 rs_priv->lq.general_params.dual_stream_ant_msk);
2417
2418 desc += sprintf(buff+desc, "agg:"
2419 "time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
2420 le16_to_cpu(rs_priv->lq.agg_params.agg_time_limit),
2421 rs_priv->lq.agg_params.agg_dis_start_th,
2422 rs_priv->lq.agg_params.agg_frame_cnt_limit);
2423
2424 desc += sprintf(buff+desc,
2425 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
2426 rs_priv->lq.general_params.start_rate_index[0],
2427 rs_priv->lq.general_params.start_rate_index[1],
2428 rs_priv->lq.general_params.start_rate_index[2],
2429 rs_priv->lq.general_params.start_rate_index[3]);
2430
2431
2432 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
2433 desc += sprintf(buff+desc, " rate[%d] 0x%X\n",
2434 i, le32_to_cpu(rs_priv->lq.rs_table[i].rate_n_flags));
2435
2436 return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2437}
2438
2439static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
Zhu Yi98d7e092007-09-27 11:27:42 +08002440 .write = rs_sta_dbgfs_scale_table_write,
Zhu Yi5ae212c2007-09-27 11:27:38 +08002441 .read = rs_sta_dbgfs_scale_table_read,
2442 .open = open_file_generic,
2443};
Zhu Yi0209dc12007-09-27 11:27:43 +08002444static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
2445 char __user *user_buf, size_t count, loff_t *ppos)
2446{
2447 char buff[1024];
2448 int desc = 0;
2449 int i, j;
2450
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002451 struct iwl4965_rate_scale_priv *rs_priv = file->private_data;
Zhu Yi0209dc12007-09-27 11:27:43 +08002452 for (i = 0; i < LQ_SIZE; i++) {
2453 desc += sprintf(buff+desc, "%s type=%d SGI=%d FAT=%d DUP=%d\n"
2454 "rate=0x%X\n",
2455 rs_priv->active_tbl == i?"*":"x",
2456 rs_priv->lq_info[i].lq_type,
2457 rs_priv->lq_info[i].is_SGI,
2458 rs_priv->lq_info[i].is_fat,
2459 rs_priv->lq_info[i].is_dup,
2460 rs_priv->lq_info[i].current_rate.rate_n_flags);
2461 for (j = 0; j < IWL_RATE_COUNT; j++) {
2462 desc += sprintf(buff+desc,
2463 "counter=%d success=%d %%=%d\n",
2464 rs_priv->lq_info[i].win[j].counter,
2465 rs_priv->lq_info[i].win[j].success_counter,
2466 rs_priv->lq_info[i].win[j].success_ratio);
2467 }
2468 }
2469 return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2470}
2471
2472static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
2473 .read = rs_sta_dbgfs_stats_table_read,
2474 .open = open_file_generic,
2475};
Zhu Yi5ae212c2007-09-27 11:27:38 +08002476
Zhu Yi93dc6462007-09-27 11:27:37 +08002477static void rs_add_debugfs(void *priv, void *priv_sta,
2478 struct dentry *dir)
2479{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002480 struct iwl4965_rate_scale_priv *rs_priv = priv_sta;
Zhu Yi5ae212c2007-09-27 11:27:38 +08002481 rs_priv->rs_sta_dbgfs_scale_table_file =
Zhu Yi0209dc12007-09-27 11:27:43 +08002482 debugfs_create_file("rate_scale_table", 0600, dir,
Zhu Yi5ae212c2007-09-27 11:27:38 +08002483 rs_priv, &rs_sta_dbgfs_scale_table_ops);
Zhu Yi0209dc12007-09-27 11:27:43 +08002484 rs_priv->rs_sta_dbgfs_stats_table_file =
2485 debugfs_create_file("rate_stats_table", 0600, dir,
2486 rs_priv, &rs_sta_dbgfs_stats_table_ops);
Zhu Yi93dc6462007-09-27 11:27:37 +08002487}
2488
2489static void rs_remove_debugfs(void *priv, void *priv_sta)
2490{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002491 struct iwl4965_rate_scale_priv *rs_priv = priv_sta;
Zhu Yi5ae212c2007-09-27 11:27:38 +08002492 debugfs_remove(rs_priv->rs_sta_dbgfs_scale_table_file);
Zhu Yi0209dc12007-09-27 11:27:43 +08002493 debugfs_remove(rs_priv->rs_sta_dbgfs_stats_table_file);
Zhu Yi93dc6462007-09-27 11:27:37 +08002494}
2495#endif
2496
Zhu Yib481de92007-09-25 17:54:57 -07002497static struct rate_control_ops rs_ops = {
2498 .module = NULL,
2499 .name = RS_NAME,
2500 .tx_status = rs_tx_status,
2501 .get_rate = rs_get_rate,
2502 .rate_init = rs_rate_init,
2503 .clear = rs_clear,
2504 .alloc = rs_alloc,
2505 .free = rs_free,
2506 .alloc_sta = rs_alloc_sta,
2507 .free_sta = rs_free_sta,
Zhu Yi93dc6462007-09-27 11:27:37 +08002508#ifdef CONFIG_MAC80211_DEBUGFS
2509 .add_sta_debugfs = rs_add_debugfs,
2510 .remove_sta_debugfs = rs_remove_debugfs,
2511#endif
Zhu Yib481de92007-09-25 17:54:57 -07002512};
2513
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002514int iwl4965_fill_rs_info(struct ieee80211_hw *hw, char *buf, u8 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07002515{
2516 struct ieee80211_local *local = hw_to_local(hw);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002517 struct iwl4965_priv *priv = hw->priv;
2518 struct iwl4965_rate_scale_priv *rs_priv;
Zhu Yib481de92007-09-25 17:54:57 -07002519 struct sta_info *sta;
2520 int count = 0, i;
2521 u32 samples = 0, success = 0, good = 0;
2522 unsigned long now = jiffies;
2523 u32 max_time = 0;
2524 u8 lq_type, antenna;
2525
2526 sta = sta_info_get(local, priv->stations[sta_id].sta.sta.addr);
2527 if (!sta || !sta->rate_ctrl_priv) {
2528 if (sta) {
2529 sta_info_put(sta);
2530 IWL_DEBUG_RATE("leave - no private rate data!\n");
2531 } else
2532 IWL_DEBUG_RATE("leave - no station!\n");
2533 return sprintf(buf, "station %d not found\n", sta_id);
2534 }
2535
2536 rs_priv = (void *)sta->rate_ctrl_priv;
2537
2538 lq_type = rs_priv->lq_info[rs_priv->active_tbl].lq_type;
2539 antenna = rs_priv->lq_info[rs_priv->active_tbl].antenna_type;
2540
2541 if (is_legacy(lq_type))
2542 i = IWL_RATE_54M_INDEX;
2543 else
2544 i = IWL_RATE_60M_INDEX;
2545 while (1) {
2546 u64 mask;
2547 int j;
2548 int active = rs_priv->active_tbl;
2549
2550 count +=
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002551 sprintf(&buf[count], " %2dMbs: ", iwl4965_rates[i].ieee / 2);
Zhu Yib481de92007-09-25 17:54:57 -07002552
2553 mask = (1ULL << (IWL_RATE_MAX_WINDOW - 1));
2554 for (j = 0; j < IWL_RATE_MAX_WINDOW; j++, mask >>= 1)
2555 buf[count++] =
2556 (rs_priv->lq_info[active].win[i].data & mask)
2557 ? '1' : '0';
2558
2559 samples += rs_priv->lq_info[active].win[i].counter;
2560 good += rs_priv->lq_info[active].win[i].success_counter;
2561 success += rs_priv->lq_info[active].win[i].success_counter *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002562 iwl4965_rates[i].ieee;
Zhu Yib481de92007-09-25 17:54:57 -07002563
2564 if (rs_priv->lq_info[active].win[i].stamp) {
2565 int delta =
2566 jiffies_to_msecs(now -
2567 rs_priv->lq_info[active].win[i].stamp);
2568
2569 if (delta > max_time)
2570 max_time = delta;
2571
2572 count += sprintf(&buf[count], "%5dms\n", delta);
2573 } else
2574 buf[count++] = '\n';
2575
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002576 j = iwl4965_get_prev_ieee_rate(i);
Zhu Yib481de92007-09-25 17:54:57 -07002577 if (j == i)
2578 break;
2579 i = j;
2580 }
2581
2582 /* Display the average rate of all samples taken.
2583 *
Ben Cahill77626352007-11-29 11:09:44 +08002584 * NOTE: We multiply # of samples by 2 since the IEEE measurement
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002585 * added from iwl4965_rates is actually 2X the rate */
Zhu Yib481de92007-09-25 17:54:57 -07002586 if (samples)
2587 count += sprintf(&buf[count],
2588 "\nAverage rate is %3d.%02dMbs over last %4dms\n"
2589 "%3d%% success (%d good packets over %d tries)\n",
2590 success / (2 * samples), (success * 5 / samples) % 10,
2591 max_time, good * 100 / samples, good, samples);
2592 else
2593 count += sprintf(&buf[count], "\nAverage rate: 0Mbs\n");
Ian Schram01ebd062007-10-25 17:15:22 +08002594 count += sprintf(&buf[count], "\nrate scale type %d antenna %d "
Zhu Yib481de92007-09-25 17:54:57 -07002595 "active_search %d rate index %d\n", lq_type, antenna,
2596 rs_priv->search_better_tbl, sta->last_txrate);
2597
2598 sta_info_put(sta);
2599 return count;
2600}
2601
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002602void iwl4965_rate_scale_init(struct ieee80211_hw *hw, s32 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07002603{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002604 struct iwl4965_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07002605
2606 priv->lq_mngr.lq_ready = 1;
2607}
2608
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002609void iwl4965_rate_control_register(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07002610{
2611 ieee80211_rate_control_register(&rs_ops);
2612}
2613
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002614void iwl4965_rate_control_unregister(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07002615{
2616 ieee80211_rate_control_unregister(&rs_ops);
2617}
2618