blob: 8d3d9f72a8ce254b64308beb786ed8d39c92a523 [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
50#define IWL_RATE_MAX_WINDOW 62
51#define IWL_RATE_HIGH_TH 10880
52#define IWL_RATE_MIN_FAILURE_TH 6
53#define IWL_RATE_MIN_SUCCESS_TH 8
54#define IWL_RATE_DECREASE_TH 1920
55#define IWL_RATE_INCREASE_TH 8960
56#define IWL_RATE_SCALE_FLUSH_INTVL (2*HZ) /*2 seconds */
57
58static u8 rs_ht_to_legacy[] = {
59 IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
60 IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
61 IWL_RATE_6M_INDEX,
62 IWL_RATE_6M_INDEX, IWL_RATE_9M_INDEX,
63 IWL_RATE_12M_INDEX, IWL_RATE_18M_INDEX,
64 IWL_RATE_24M_INDEX, IWL_RATE_36M_INDEX,
65 IWL_RATE_48M_INDEX, IWL_RATE_54M_INDEX
66};
67
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080068struct iwl4965_rate {
Zhu Yib481de92007-09-25 17:54:57 -070069 u32 rate_n_flags;
70} __attribute__ ((packed));
71
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080072struct iwl4965_rate_scale_data {
Zhu Yib481de92007-09-25 17:54:57 -070073 u64 data;
74 s32 success_counter;
75 s32 success_ratio;
76 s32 counter;
77 s32 average_tpt;
78 unsigned long stamp;
79};
80
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080081struct iwl4965_scale_tbl_info {
82 enum iwl4965_table_type lq_type;
83 enum iwl4965_antenna_type antenna_type;
Zhu Yib481de92007-09-25 17:54:57 -070084 u8 is_SGI;
85 u8 is_fat;
86 u8 is_dup;
87 u8 action;
88 s32 *expected_tpt;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080089 struct iwl4965_rate current_rate;
90 struct iwl4965_rate_scale_data win[IWL_RATE_COUNT];
Zhu Yib481de92007-09-25 17:54:57 -070091};
92
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080093struct iwl4965_rate_scale_priv {
Zhu Yib481de92007-09-25 17:54:57 -070094 u8 active_tbl;
95 u8 enable_counter;
96 u8 stay_in_tbl;
97 u8 search_better_tbl;
98 s32 last_tpt;
99 u32 table_count_limit;
100 u32 max_failure_limit;
101 u32 max_success_limit;
102 u32 table_count;
103 u32 total_failed;
104 u32 total_success;
105 u32 flush_timer;
106 u8 action_counter;
107 u8 antenna;
108 u8 valid_antenna;
109 u8 is_green;
110 u8 is_dup;
111 u8 phymode;
112 u8 ibss_sta_added;
Zhu Yi02dede02007-09-27 11:27:40 +0800113 u32 supp_rates;
Zhu Yib481de92007-09-25 17:54:57 -0700114 u16 active_rate;
115 u16 active_siso_rate;
116 u16 active_mimo_rate;
117 u16 active_rate_basic;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800118 struct iwl4965_link_quality_cmd lq;
119 struct iwl4965_scale_tbl_info lq_info[LQ_SIZE];
Zhu Yi5ae212c2007-09-27 11:27:38 +0800120#ifdef CONFIG_MAC80211_DEBUGFS
Zhu Yi98d7e092007-09-27 11:27:42 +0800121 struct dentry *rs_sta_dbgfs_scale_table_file;
Zhu Yi0209dc12007-09-27 11:27:43 +0800122 struct dentry *rs_sta_dbgfs_stats_table_file;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800123 struct iwl4965_rate dbg_fixed;
124 struct iwl4965_priv *drv;
Zhu Yi5ae212c2007-09-27 11:27:38 +0800125#endif
Zhu Yib481de92007-09-25 17:54:57 -0700126};
127
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800128static void rs_rate_scale_perform(struct iwl4965_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700129 struct net_device *dev,
130 struct ieee80211_hdr *hdr,
131 struct sta_info *sta);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800132static void rs_fill_link_cmd(struct iwl4965_rate_scale_priv *lq_data,
133 struct iwl4965_rate *tx_mcs,
134 struct iwl4965_link_quality_cmd *tbl);
Zhu Yib481de92007-09-25 17:54:57 -0700135
136
Zhu Yi98d7e092007-09-27 11:27:42 +0800137#ifdef CONFIG_MAC80211_DEBUGFS
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800138static void rs_dbgfs_set_mcs(struct iwl4965_rate_scale_priv *rs_priv,
139 struct iwl4965_rate *mcs, int index);
Zhu Yi98d7e092007-09-27 11:27:42 +0800140#else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800141static void rs_dbgfs_set_mcs(struct iwl4965_rate_scale_priv *rs_priv,
142 struct iwl4965_rate *mcs, int index)
Zhu Yi98d7e092007-09-27 11:27:42 +0800143{}
144#endif
Zhu Yib481de92007-09-25 17:54:57 -0700145static s32 expected_tpt_A[IWL_RATE_COUNT] = {
146 0, 0, 0, 0, 40, 57, 72, 98, 121, 154, 177, 186, 186
147};
148
149static s32 expected_tpt_G[IWL_RATE_COUNT] = {
150 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 186
151};
152
153static s32 expected_tpt_siso20MHz[IWL_RATE_COUNT] = {
154 0, 0, 0, 0, 42, 42, 76, 102, 124, 159, 183, 193, 202
155};
156
157static s32 expected_tpt_siso20MHzSGI[IWL_RATE_COUNT] = {
158 0, 0, 0, 0, 46, 46, 82, 110, 132, 168, 192, 202, 211
159};
160
161static s32 expected_tpt_mimo20MHz[IWL_RATE_COUNT] = {
162 0, 0, 0, 0, 74, 74, 123, 155, 179, 214, 236, 244, 251
163};
164
165static s32 expected_tpt_mimo20MHzSGI[IWL_RATE_COUNT] = {
166 0, 0, 0, 0, 81, 81, 131, 164, 188, 222, 243, 251, 257
167};
168
169static s32 expected_tpt_siso40MHz[IWL_RATE_COUNT] = {
170 0, 0, 0, 0, 77, 77, 127, 160, 184, 220, 242, 250, 257
171};
172
173static s32 expected_tpt_siso40MHzSGI[IWL_RATE_COUNT] = {
174 0, 0, 0, 0, 83, 83, 135, 169, 193, 229, 250, 257, 264
175};
176
177static s32 expected_tpt_mimo40MHz[IWL_RATE_COUNT] = {
178 0, 0, 0, 0, 123, 123, 182, 214, 235, 264, 279, 285, 289
179};
180
181static s32 expected_tpt_mimo40MHzSGI[IWL_RATE_COUNT] = {
182 0, 0, 0, 0, 131, 131, 191, 222, 242, 270, 284, 289, 293
183};
184
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800185static int iwl4965_lq_sync_callback(struct iwl4965_priv *priv,
186 struct iwl4965_cmd *cmd, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -0700187{
188 /*We didn't cache the SKB; let the caller free it */
189 return 1;
190}
191
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800192static inline u8 iwl4965_rate_get_rate(u32 rate_n_flags)
Zhu Yib481de92007-09-25 17:54:57 -0700193{
194 return (u8)(rate_n_flags & 0xFF);
195}
196
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800197static int rs_send_lq_cmd(struct iwl4965_priv *priv,
198 struct iwl4965_link_quality_cmd *lq, u8 flags)
Zhu Yib481de92007-09-25 17:54:57 -0700199{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800200#ifdef CONFIG_IWL4965_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -0700201 int i;
202#endif
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800203 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700204 .id = REPLY_TX_LINK_QUALITY_CMD,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800205 .len = sizeof(struct iwl4965_link_quality_cmd),
Zhu Yib481de92007-09-25 17:54:57 -0700206 .meta.flags = flags,
207 .data = lq,
208 };
209
210 if ((lq->sta_id == 0xFF) &&
211 (priv->iw_mode == IEEE80211_IF_TYPE_IBSS))
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800212 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -0700213
214 if (lq->sta_id == 0xFF)
215 lq->sta_id = IWL_AP_ID;
216
217 IWL_DEBUG_RATE("lq station id 0x%x\n", lq->sta_id);
218 IWL_DEBUG_RATE("lq dta 0x%X 0x%X\n",
219 lq->general_params.single_stream_ant_msk,
220 lq->general_params.dual_stream_ant_msk);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800221#ifdef CONFIG_IWL4965_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -0700222 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
223 IWL_DEBUG_RATE("lq index %d 0x%X\n",
224 i, lq->rs_table[i].rate_n_flags);
225#endif
226
227 if (flags & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800228 cmd.meta.u.callback = iwl4965_lq_sync_callback;
Zhu Yib481de92007-09-25 17:54:57 -0700229
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800230 if (iwl4965_is_associated(priv) && priv->assoc_station_added &&
Zhu Yib481de92007-09-25 17:54:57 -0700231 priv->lq_mngr.lq_ready)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800232 return iwl4965_send_cmd(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700233
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800234 return 0;
Zhu Yib481de92007-09-25 17:54:57 -0700235}
236
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800237static void rs_rate_scale_clear_window(struct iwl4965_rate_scale_data *window)
Zhu Yib481de92007-09-25 17:54:57 -0700238{
239 window->data = 0;
240 window->success_counter = 0;
241 window->success_ratio = IWL_INVALID_VALUE;
242 window->counter = 0;
243 window->average_tpt = IWL_INVALID_VALUE;
244 window->stamp = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700245}
246
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800247static int rs_collect_tx_data(struct iwl4965_rate_scale_data *windows,
Zhu Yib481de92007-09-25 17:54:57 -0700248 int scale_index, s32 tpt, u32 status)
249{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800250 struct iwl4965_rate_scale_data *window = NULL;
Zhu Yib481de92007-09-25 17:54:57 -0700251 u64 mask;
252 u8 win_size = IWL_RATE_MAX_WINDOW;
253 s32 fail_count;
254
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800255 if (scale_index < 0 || scale_index >= IWL_RATE_COUNT)
256 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -0700257
258 window = &(windows[scale_index]);
259
260 if (window->counter >= win_size) {
261
262 window->counter = win_size - 1;
263 mask = 1;
264 mask = (mask << (win_size - 1));
265 if ((window->data & mask)) {
266 window->data &= ~mask;
267 window->success_counter = window->success_counter - 1;
268 }
269 }
270
271 window->counter = window->counter + 1;
272 mask = window->data;
273 window->data = (mask << 1);
274 if (status != 0) {
275 window->success_counter = window->success_counter + 1;
276 window->data |= 0x1;
277 }
278
279 if (window->counter > 0)
280 window->success_ratio = 128 * (100 * window->success_counter)
281 / window->counter;
282 else
283 window->success_ratio = IWL_INVALID_VALUE;
284
285 fail_count = window->counter - window->success_counter;
286
287 if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
288 (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
289 window->average_tpt = (window->success_ratio * tpt + 64) / 128;
290 else
291 window->average_tpt = IWL_INVALID_VALUE;
292
293 window->stamp = jiffies;
294
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800295 return 0;
Zhu Yib481de92007-09-25 17:54:57 -0700296}
297
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800298static void rs_mcs_from_tbl(struct iwl4965_rate *mcs_rate,
299 struct iwl4965_scale_tbl_info *tbl,
Zhu Yib481de92007-09-25 17:54:57 -0700300 int index, u8 use_green)
301{
Zhu Yib481de92007-09-25 17:54:57 -0700302 if (is_legacy(tbl->lq_type)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800303 mcs_rate->rate_n_flags = iwl4965_rates[index].plcp;
Zhu Yib481de92007-09-25 17:54:57 -0700304 if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
305 mcs_rate->rate_n_flags |= RATE_MCS_CCK_MSK;
306
307 } else if (is_siso(tbl->lq_type)) {
308 if (index > IWL_LAST_OFDM_RATE)
309 index = IWL_LAST_OFDM_RATE;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800310 mcs_rate->rate_n_flags = iwl4965_rates[index].plcp_siso |
Zhu Yib481de92007-09-25 17:54:57 -0700311 RATE_MCS_HT_MSK;
312 } else {
313 if (index > IWL_LAST_OFDM_RATE)
314 index = IWL_LAST_OFDM_RATE;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800315 mcs_rate->rate_n_flags = iwl4965_rates[index].plcp_mimo |
Zhu Yib481de92007-09-25 17:54:57 -0700316 RATE_MCS_HT_MSK;
317 }
318
319 switch (tbl->antenna_type) {
320 case ANT_BOTH:
321 mcs_rate->rate_n_flags |= RATE_MCS_ANT_AB_MSK;
322 break;
323 case ANT_MAIN:
324 mcs_rate->rate_n_flags |= RATE_MCS_ANT_A_MSK;
325 break;
326 case ANT_AUX:
327 mcs_rate->rate_n_flags |= RATE_MCS_ANT_B_MSK;
328 break;
329 case ANT_NONE:
330 break;
331 }
332
333 if (is_legacy(tbl->lq_type))
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800334 return;
Zhu Yib481de92007-09-25 17:54:57 -0700335
336 if (tbl->is_fat) {
337 if (tbl->is_dup)
338 mcs_rate->rate_n_flags |= RATE_MCS_DUP_MSK;
339 else
340 mcs_rate->rate_n_flags |= RATE_MCS_FAT_MSK;
341 }
342 if (tbl->is_SGI)
343 mcs_rate->rate_n_flags |= RATE_MCS_SGI_MSK;
344
345 if (use_green) {
346 mcs_rate->rate_n_flags |= RATE_MCS_GF_MSK;
347 if (is_siso(tbl->lq_type))
348 mcs_rate->rate_n_flags &= ~RATE_MCS_SGI_MSK;
349 }
Zhu Yib481de92007-09-25 17:54:57 -0700350}
351
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800352static int rs_get_tbl_info_from_mcs(const struct iwl4965_rate *mcs_rate,
353 int phymode, struct iwl4965_scale_tbl_info *tbl,
Zhu Yib481de92007-09-25 17:54:57 -0700354 int *rate_idx)
355{
356 int index;
357 u32 ant_msk;
358
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800359 index = iwl4965_rate_index_from_plcp(mcs_rate->rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -0700360
361 if (index == IWL_RATE_INVALID) {
362 *rate_idx = -1;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800363 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -0700364 }
365 tbl->is_SGI = 0;
366 tbl->is_fat = 0;
367 tbl->is_dup = 0;
368 tbl->antenna_type = ANT_BOTH;
369
370 if (!(mcs_rate->rate_n_flags & RATE_MCS_HT_MSK)) {
371 ant_msk = (mcs_rate->rate_n_flags & RATE_MCS_ANT_AB_MSK);
372
373 if (ant_msk == RATE_MCS_ANT_AB_MSK)
374 tbl->lq_type = LQ_NONE;
375 else {
376
377 if (phymode == MODE_IEEE80211A)
378 tbl->lq_type = LQ_A;
379 else
380 tbl->lq_type = LQ_G;
381
382 if (mcs_rate->rate_n_flags & RATE_MCS_ANT_A_MSK)
383 tbl->antenna_type = ANT_MAIN;
384 else
385 tbl->antenna_type = ANT_AUX;
386 }
387 *rate_idx = index;
388
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800389 } else if (iwl4965_rate_get_rate(mcs_rate->rate_n_flags)
Zhu Yib481de92007-09-25 17:54:57 -0700390 <= IWL_RATE_SISO_60M_PLCP) {
391 tbl->lq_type = LQ_SISO;
392
393 ant_msk = (mcs_rate->rate_n_flags & RATE_MCS_ANT_AB_MSK);
394 if (ant_msk == RATE_MCS_ANT_AB_MSK)
395 tbl->lq_type = LQ_NONE;
396 else {
397 if (mcs_rate->rate_n_flags & RATE_MCS_ANT_A_MSK)
398 tbl->antenna_type = ANT_MAIN;
399 else
400 tbl->antenna_type = ANT_AUX;
401 }
402 if (mcs_rate->rate_n_flags & RATE_MCS_SGI_MSK)
403 tbl->is_SGI = 1;
404
405 if ((mcs_rate->rate_n_flags & RATE_MCS_FAT_MSK) ||
406 (mcs_rate->rate_n_flags & RATE_MCS_DUP_MSK))
407 tbl->is_fat = 1;
408
409 if (mcs_rate->rate_n_flags & RATE_MCS_DUP_MSK)
410 tbl->is_dup = 1;
411
412 *rate_idx = index;
413 } else {
414 tbl->lq_type = LQ_MIMO;
415 if (mcs_rate->rate_n_flags & RATE_MCS_SGI_MSK)
416 tbl->is_SGI = 1;
417
418 if ((mcs_rate->rate_n_flags & RATE_MCS_FAT_MSK) ||
419 (mcs_rate->rate_n_flags & RATE_MCS_DUP_MSK))
420 tbl->is_fat = 1;
421
422 if (mcs_rate->rate_n_flags & RATE_MCS_DUP_MSK)
423 tbl->is_dup = 1;
424 *rate_idx = index;
425 }
426 return 0;
427}
428
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800429static inline void rs_toggle_antenna(struct iwl4965_rate *new_rate,
430 struct iwl4965_scale_tbl_info *tbl)
Zhu Yib481de92007-09-25 17:54:57 -0700431{
432 if (tbl->antenna_type == ANT_AUX) {
433 tbl->antenna_type = ANT_MAIN;
434 new_rate->rate_n_flags &= ~RATE_MCS_ANT_B_MSK;
435 new_rate->rate_n_flags |= RATE_MCS_ANT_A_MSK;
436 } else {
437 tbl->antenna_type = ANT_AUX;
438 new_rate->rate_n_flags &= ~RATE_MCS_ANT_A_MSK;
439 new_rate->rate_n_flags |= RATE_MCS_ANT_B_MSK;
440 }
441}
442
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800443static inline u8 rs_use_green(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700444{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800445#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -0700446 if (!priv->is_ht_enabled || !priv->current_assoc_ht.is_ht)
447 return 0;
448
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800449 return ((priv->current_assoc_ht.is_green_field) &&
450 !(priv->current_assoc_ht.operating_mode & 0x4));
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800451#endif /*CONFIG_IWL4965_HT */
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800452 return 0;
Zhu Yib481de92007-09-25 17:54:57 -0700453}
454
455/**
456 * rs_get_supported_rates - get the available rates
457 *
458 * if management frame or broadcast frame only return
459 * basic available rates.
460 *
461 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800462static void rs_get_supported_rates(struct iwl4965_rate_scale_priv *lq_data,
Zhu Yib481de92007-09-25 17:54:57 -0700463 struct ieee80211_hdr *hdr,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800464 enum iwl4965_table_type rate_type,
Zhu Yib481de92007-09-25 17:54:57 -0700465 u16 *data_rate)
466{
467 if (is_legacy(rate_type))
468 *data_rate = lq_data->active_rate;
469 else {
470 if (is_siso(rate_type))
471 *data_rate = lq_data->active_siso_rate;
472 else
473 *data_rate = lq_data->active_mimo_rate;
474 }
475
476 if (hdr && is_multicast_ether_addr(hdr->addr1) &&
477 lq_data->active_rate_basic)
478 *data_rate = lq_data->active_rate_basic;
479}
480
481static u16 rs_get_adjacent_rate(u8 index, u16 rate_mask, int rate_type)
482{
483 u8 high = IWL_RATE_INVALID;
484 u8 low = IWL_RATE_INVALID;
485
Ian Schram01ebd062007-10-25 17:15:22 +0800486 /* 802.11A or ht walks to the next literal adjacent rate in
Zhu Yib481de92007-09-25 17:54:57 -0700487 * the rate table */
488 if (is_a_band(rate_type) || !is_legacy(rate_type)) {
489 int i;
490 u32 mask;
491
492 /* Find the previous rate that is in the rate mask */
493 i = index - 1;
494 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
495 if (rate_mask & mask) {
496 low = i;
497 break;
498 }
499 }
500
501 /* Find the next rate that is in the rate mask */
502 i = index + 1;
503 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
504 if (rate_mask & mask) {
505 high = i;
506 break;
507 }
508 }
509
510 return (high << 8) | low;
511 }
512
513 low = index;
514 while (low != IWL_RATE_INVALID) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800515 low = iwl4965_rates[low].prev_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700516 if (low == IWL_RATE_INVALID)
517 break;
518 if (rate_mask & (1 << low))
519 break;
520 IWL_DEBUG_RATE("Skipping masked lower rate: %d\n", low);
521 }
522
523 high = index;
524 while (high != IWL_RATE_INVALID) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800525 high = iwl4965_rates[high].next_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700526 if (high == IWL_RATE_INVALID)
527 break;
528 if (rate_mask & (1 << high))
529 break;
530 IWL_DEBUG_RATE("Skipping masked higher rate: %d\n", high);
531 }
532
533 return (high << 8) | low;
534}
535
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800536static void rs_get_lower_rate(struct iwl4965_rate_scale_priv *lq_data,
537 struct iwl4965_scale_tbl_info *tbl, u8 scale_index,
538 u8 ht_possible, struct iwl4965_rate *mcs_rate)
Zhu Yib481de92007-09-25 17:54:57 -0700539{
Zhu Yib481de92007-09-25 17:54:57 -0700540 s32 low;
541 u16 rate_mask;
542 u16 high_low;
543 u8 switch_to_legacy = 0;
Zhu Yi02dede02007-09-27 11:27:40 +0800544 u8 is_green = lq_data->is_green;
Zhu Yib481de92007-09-25 17:54:57 -0700545
546 /* check if we need to switch from HT to legacy rates.
547 * assumption is that mandatory rates (1Mbps or 6Mbps)
548 * are always supported (spec demand) */
549 if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_index)) {
550 switch_to_legacy = 1;
551 scale_index = rs_ht_to_legacy[scale_index];
552 if (lq_data->phymode == MODE_IEEE80211A)
553 tbl->lq_type = LQ_A;
554 else
555 tbl->lq_type = LQ_G;
556
557 if ((tbl->antenna_type == ANT_BOTH) ||
558 (tbl->antenna_type == ANT_NONE))
559 tbl->antenna_type = ANT_MAIN;
560
561 tbl->is_fat = 0;
562 tbl->is_SGI = 0;
563 }
564
565 rs_get_supported_rates(lq_data, NULL, tbl->lq_type, &rate_mask);
566
567 /* mask with station rate restriction */
568 if (is_legacy(tbl->lq_type)) {
569 if (lq_data->phymode == (u8) MODE_IEEE80211A)
570 rate_mask = (u16)(rate_mask &
Zhu Yi02dede02007-09-27 11:27:40 +0800571 (lq_data->supp_rates << IWL_FIRST_OFDM_RATE));
Zhu Yib481de92007-09-25 17:54:57 -0700572 else
Zhu Yi02dede02007-09-27 11:27:40 +0800573 rate_mask = (u16)(rate_mask & lq_data->supp_rates);
Zhu Yib481de92007-09-25 17:54:57 -0700574 }
575
576 /* if we did switched from HT to legacy check current rate */
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800577 if (switch_to_legacy && (rate_mask & (1 << scale_index))) {
Zhu Yib481de92007-09-25 17:54:57 -0700578 rs_mcs_from_tbl(mcs_rate, tbl, scale_index, is_green);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800579 return;
Zhu Yib481de92007-09-25 17:54:57 -0700580 }
581
582 high_low = rs_get_adjacent_rate(scale_index, rate_mask, tbl->lq_type);
583 low = high_low & 0xff;
584
585 if (low != IWL_RATE_INVALID)
586 rs_mcs_from_tbl(mcs_rate, tbl, low, is_green);
587 else
588 rs_mcs_from_tbl(mcs_rate, tbl, scale_index, is_green);
Zhu Yib481de92007-09-25 17:54:57 -0700589}
590
591static void rs_tx_status(void *priv_rate,
592 struct net_device *dev,
593 struct sk_buff *skb,
594 struct ieee80211_tx_status *tx_resp)
595{
596 int status;
597 u8 retries;
598 int rs_index, index = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800599 struct iwl4965_rate_scale_priv *lq;
600 struct iwl4965_link_quality_cmd *table;
Zhu Yib481de92007-09-25 17:54:57 -0700601 struct sta_info *sta;
602 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800603 struct iwl4965_priv *priv = (struct iwl4965_priv *)priv_rate;
Zhu Yib481de92007-09-25 17:54:57 -0700604 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800605 struct iwl4965_rate_scale_data *window = NULL;
606 struct iwl4965_rate_scale_data *search_win = NULL;
607 struct iwl4965_rate tx_mcs;
608 struct iwl4965_scale_tbl_info tbl_type;
609 struct iwl4965_scale_tbl_info *curr_tbl, *search_tbl;
Zhu Yib481de92007-09-25 17:54:57 -0700610 u8 active_index = 0;
611 u16 fc = le16_to_cpu(hdr->frame_control);
612 s32 tpt = 0;
613
Zhu Yi58826352007-09-27 11:27:39 +0800614 IWL_DEBUG_RATE_LIMIT("get frame ack response, update rate scale window\n");
Zhu Yib481de92007-09-25 17:54:57 -0700615
616 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1))
617 return;
618
619 retries = tx_resp->retry_count;
620
621 if (retries > 15)
622 retries = 15;
623
624
625 sta = sta_info_get(local, hdr->addr1);
626
627 if (!sta || !sta->rate_ctrl_priv) {
628 if (sta)
629 sta_info_put(sta);
630 return;
631 }
632
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800633 lq = (struct iwl4965_rate_scale_priv *)sta->rate_ctrl_priv;
Zhu Yib481de92007-09-25 17:54:57 -0700634
635 if (!priv->lq_mngr.lq_ready)
636 return;
637
638 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) && !lq->ibss_sta_added)
639 return;
640
641 table = &lq->lq;
642 active_index = lq->active_tbl;
643
644 lq->antenna = (lq->valid_antenna & local->hw.conf.antenna_sel_tx);
645 if (!lq->antenna)
646 lq->antenna = lq->valid_antenna;
647
648 lq->antenna = lq->valid_antenna;
649 curr_tbl = &(lq->lq_info[active_index]);
650 search_tbl = &(lq->lq_info[(1 - active_index)]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800651 window = (struct iwl4965_rate_scale_data *)
Zhu Yib481de92007-09-25 17:54:57 -0700652 &(curr_tbl->win[0]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800653 search_win = (struct iwl4965_rate_scale_data *)
Zhu Yib481de92007-09-25 17:54:57 -0700654 &(search_tbl->win[0]);
655
656 tx_mcs.rate_n_flags = tx_resp->control.tx_rate;
657
658 rs_get_tbl_info_from_mcs(&tx_mcs, priv->phymode,
659 &tbl_type, &rs_index);
660 if ((rs_index < 0) || (rs_index >= IWL_RATE_COUNT)) {
661 IWL_DEBUG_RATE("bad rate index at: %d rate 0x%X\n",
662 rs_index, tx_mcs.rate_n_flags);
663 sta_info_put(sta);
664 return;
665 }
666
667 if (retries &&
668 (tx_mcs.rate_n_flags !=
669 le32_to_cpu(table->rs_table[0].rate_n_flags))) {
670 IWL_DEBUG_RATE("initial rate does not match 0x%x 0x%x\n",
671 tx_mcs.rate_n_flags,
672 le32_to_cpu(table->rs_table[0].rate_n_flags));
673 sta_info_put(sta);
674 return;
675 }
676
677 while (retries) {
678 tx_mcs.rate_n_flags =
679 le32_to_cpu(table->rs_table[index].rate_n_flags);
680 rs_get_tbl_info_from_mcs(&tx_mcs, priv->phymode,
681 &tbl_type, &rs_index);
682
683 if ((tbl_type.lq_type == search_tbl->lq_type) &&
684 (tbl_type.antenna_type == search_tbl->antenna_type) &&
685 (tbl_type.is_SGI == search_tbl->is_SGI)) {
686 if (search_tbl->expected_tpt)
687 tpt = search_tbl->expected_tpt[rs_index];
688 else
689 tpt = 0;
690 rs_collect_tx_data(search_win,
691 rs_index, tpt, 0);
692 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
693 (tbl_type.antenna_type == curr_tbl->antenna_type) &&
694 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
695 if (curr_tbl->expected_tpt)
696 tpt = curr_tbl->expected_tpt[rs_index];
697 else
698 tpt = 0;
699 rs_collect_tx_data(window, rs_index, tpt, 0);
700 }
701 if (lq->stay_in_tbl)
702 lq->total_failed++;
703 --retries;
704 index++;
705
706 }
707
708 if (!tx_resp->retry_count)
709 tx_mcs.rate_n_flags = tx_resp->control.tx_rate;
710 else
711 tx_mcs.rate_n_flags =
712 le32_to_cpu(table->rs_table[index].rate_n_flags);
713
714 rs_get_tbl_info_from_mcs(&tx_mcs, priv->phymode,
715 &tbl_type, &rs_index);
716
717 if (tx_resp->flags & IEEE80211_TX_STATUS_ACK)
718 status = 1;
719 else
720 status = 0;
721
722 if ((tbl_type.lq_type == search_tbl->lq_type) &&
723 (tbl_type.antenna_type == search_tbl->antenna_type) &&
724 (tbl_type.is_SGI == search_tbl->is_SGI)) {
725 if (search_tbl->expected_tpt)
726 tpt = search_tbl->expected_tpt[rs_index];
727 else
728 tpt = 0;
729 rs_collect_tx_data(search_win,
730 rs_index, tpt, status);
731 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
732 (tbl_type.antenna_type == curr_tbl->antenna_type) &&
733 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
734 if (curr_tbl->expected_tpt)
735 tpt = curr_tbl->expected_tpt[rs_index];
736 else
737 tpt = 0;
738 rs_collect_tx_data(window, rs_index, tpt, status);
739 }
740
741 if (lq->stay_in_tbl) {
742 if (status)
743 lq->total_success++;
744 else
745 lq->total_failed++;
746 }
747
748 rs_rate_scale_perform(priv, dev, hdr, sta);
749 sta_info_put(sta);
750 return;
751}
752
753static u8 rs_is_ant_connected(u8 valid_antenna,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800754 enum iwl4965_antenna_type antenna_type)
Zhu Yib481de92007-09-25 17:54:57 -0700755{
756 if (antenna_type == ANT_AUX)
757 return ((valid_antenna & 0x2) ? 1:0);
758 else if (antenna_type == ANT_MAIN)
759 return ((valid_antenna & 0x1) ? 1:0);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800760 else if (antenna_type == ANT_BOTH)
761 return ((valid_antenna & 0x3) == 0x3);
Zhu Yib481de92007-09-25 17:54:57 -0700762
763 return 1;
764}
765
766static u8 rs_is_other_ant_connected(u8 valid_antenna,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800767 enum iwl4965_antenna_type antenna_type)
Zhu Yib481de92007-09-25 17:54:57 -0700768{
769 if (antenna_type == ANT_AUX)
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800770 return rs_is_ant_connected(valid_antenna, ANT_MAIN);
Zhu Yib481de92007-09-25 17:54:57 -0700771 else
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800772 return rs_is_ant_connected(valid_antenna, ANT_AUX);
Zhu Yib481de92007-09-25 17:54:57 -0700773
774 return 0;
775}
776
777static void rs_set_stay_in_table(u8 is_legacy,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800778 struct iwl4965_rate_scale_priv *lq_data)
Zhu Yib481de92007-09-25 17:54:57 -0700779{
780 IWL_DEBUG_HT("we are staying in the same table\n");
781 lq_data->stay_in_tbl = 1;
782 if (is_legacy) {
783 lq_data->table_count_limit = IWL_LEGACY_TABLE_COUNT;
784 lq_data->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
Mohamed Abbas403ab562007-11-06 22:06:25 -0800785 lq_data->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
Zhu Yib481de92007-09-25 17:54:57 -0700786 } else {
787 lq_data->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
788 lq_data->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
789 lq_data->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
790 }
791 lq_data->table_count = 0;
792 lq_data->total_failed = 0;
793 lq_data->total_success = 0;
794}
795
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800796static void rs_get_expected_tpt_table(struct iwl4965_rate_scale_priv *lq_data,
797 struct iwl4965_scale_tbl_info *tbl)
Zhu Yib481de92007-09-25 17:54:57 -0700798{
799 if (is_legacy(tbl->lq_type)) {
800 if (!is_a_band(tbl->lq_type))
801 tbl->expected_tpt = expected_tpt_G;
802 else
803 tbl->expected_tpt = expected_tpt_A;
804 } else if (is_siso(tbl->lq_type)) {
805 if (tbl->is_fat && !lq_data->is_dup)
806 if (tbl->is_SGI)
807 tbl->expected_tpt = expected_tpt_siso40MHzSGI;
808 else
809 tbl->expected_tpt = expected_tpt_siso40MHz;
810 else if (tbl->is_SGI)
811 tbl->expected_tpt = expected_tpt_siso20MHzSGI;
812 else
813 tbl->expected_tpt = expected_tpt_siso20MHz;
814
815 } else if (is_mimo(tbl->lq_type)) {
816 if (tbl->is_fat && !lq_data->is_dup)
817 if (tbl->is_SGI)
818 tbl->expected_tpt = expected_tpt_mimo40MHzSGI;
819 else
820 tbl->expected_tpt = expected_tpt_mimo40MHz;
821 else if (tbl->is_SGI)
822 tbl->expected_tpt = expected_tpt_mimo20MHzSGI;
823 else
824 tbl->expected_tpt = expected_tpt_mimo20MHz;
825 } else
826 tbl->expected_tpt = expected_tpt_G;
827}
828
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800829#ifdef CONFIG_IWL4965_HT
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800830static s32 rs_get_best_rate(struct iwl4965_priv *priv,
831 struct iwl4965_rate_scale_priv *lq_data,
832 struct iwl4965_scale_tbl_info *tbl,
Zhu Yib481de92007-09-25 17:54:57 -0700833 u16 rate_mask, s8 index, s8 rate)
834{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800835 struct iwl4965_scale_tbl_info *active_tbl =
Zhu Yib481de92007-09-25 17:54:57 -0700836 &(lq_data->lq_info[lq_data->active_tbl]);
837 s32 new_rate, high, low, start_hi;
838 s32 active_sr = active_tbl->win[index].success_ratio;
839 s32 *tpt_tbl = tbl->expected_tpt;
840 s32 active_tpt = active_tbl->expected_tpt[index];
841 u16 high_low;
842
843 new_rate = high = low = start_hi = IWL_RATE_INVALID;
844
845 for (; ;) {
846 high_low = rs_get_adjacent_rate(rate, rate_mask, tbl->lq_type);
847
848 low = high_low & 0xff;
849 high = (high_low >> 8) & 0xff;
850
851 if ((((100 * tpt_tbl[rate]) > lq_data->last_tpt) &&
852 ((active_sr > IWL_RATE_DECREASE_TH) &&
853 (active_sr <= IWL_RATE_HIGH_TH) &&
854 (tpt_tbl[rate] <= active_tpt))) ||
855 ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
856 (tpt_tbl[rate] > active_tpt))) {
857
858 if (start_hi != IWL_RATE_INVALID) {
859 new_rate = start_hi;
860 break;
861 }
862 new_rate = rate;
863 if (low != IWL_RATE_INVALID)
864 rate = low;
865 else
866 break;
867 } else {
868 if (new_rate != IWL_RATE_INVALID)
869 break;
870 else if (high != IWL_RATE_INVALID) {
871 start_hi = high;
872 rate = high;
873 } else {
874 new_rate = rate;
875 break;
876 }
877 }
878 }
879
880 return new_rate;
881}
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800882#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -0700883
884static inline u8 rs_is_both_ant_supp(u8 valid_antenna)
885{
886 return (rs_is_ant_connected(valid_antenna, ANT_BOTH));
887}
888
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800889static int rs_switch_to_mimo(struct iwl4965_priv *priv,
890 struct iwl4965_rate_scale_priv *lq_data,
891 struct iwl4965_scale_tbl_info *tbl, int index)
Zhu Yib481de92007-09-25 17:54:57 -0700892{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800893#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -0700894 u16 rate_mask;
895 s32 rate;
896 s8 is_green = lq_data->is_green;
897
898 if (!priv->is_ht_enabled || !priv->current_assoc_ht.is_ht)
899 return -1;
900
901 IWL_DEBUG_HT("LQ: try to switch to MIMO\n");
902 tbl->lq_type = LQ_MIMO;
903 rs_get_supported_rates(lq_data, NULL, tbl->lq_type,
904 &rate_mask);
905
906 if (priv->current_assoc_ht.tx_mimo_ps_mode == IWL_MIMO_PS_STATIC)
907 return -1;
908
909 if (!rs_is_both_ant_supp(lq_data->antenna))
910 return -1;
911
Zhu Yib481de92007-09-25 17:54:57 -0700912 tbl->is_dup = lq_data->is_dup;
913 tbl->action = 0;
914 if (priv->current_channel_width == IWL_CHANNEL_WIDTH_40MHZ)
915 tbl->is_fat = 1;
916 else
917 tbl->is_fat = 0;
918
919 if (tbl->is_fat) {
920 if (priv->current_assoc_ht.sgf & HT_SHORT_GI_40MHZ_ONLY)
921 tbl->is_SGI = 1;
922 else
923 tbl->is_SGI = 0;
924 } else if (priv->current_assoc_ht.sgf & HT_SHORT_GI_20MHZ_ONLY)
925 tbl->is_SGI = 1;
926 else
927 tbl->is_SGI = 0;
928
929 rs_get_expected_tpt_table(lq_data, tbl);
930
931 rate = rs_get_best_rate(priv, lq_data, tbl, rate_mask, index, index);
932
933 IWL_DEBUG_HT("LQ: MIMO best rate %d mask %X\n", rate, rate_mask);
934 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask))
935 return -1;
936 rs_mcs_from_tbl(&tbl->current_rate, tbl, rate, is_green);
937
938 IWL_DEBUG_HT("LQ: Switch to new mcs %X index is green %X\n",
939 tbl->current_rate.rate_n_flags, is_green);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800940 return 0;
Mohamed Abbas403ab562007-11-06 22:06:25 -0800941#else
942 return -1;
943#endif /*CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -0700944}
945
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800946static int rs_switch_to_siso(struct iwl4965_priv *priv,
947 struct iwl4965_rate_scale_priv *lq_data,
948 struct iwl4965_scale_tbl_info *tbl, int index)
Zhu Yib481de92007-09-25 17:54:57 -0700949{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800950#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -0700951 u16 rate_mask;
952 u8 is_green = lq_data->is_green;
953 s32 rate;
954
955 IWL_DEBUG_HT("LQ: try to switch to SISO\n");
956 if (!priv->is_ht_enabled || !priv->current_assoc_ht.is_ht)
957 return -1;
958
Zhu Yib481de92007-09-25 17:54:57 -0700959 tbl->is_dup = lq_data->is_dup;
960 tbl->lq_type = LQ_SISO;
961 tbl->action = 0;
962 rs_get_supported_rates(lq_data, NULL, tbl->lq_type,
963 &rate_mask);
964
965 if (priv->current_channel_width == IWL_CHANNEL_WIDTH_40MHZ)
966 tbl->is_fat = 1;
967 else
968 tbl->is_fat = 0;
969
970 if (tbl->is_fat) {
971 if (priv->current_assoc_ht.sgf & HT_SHORT_GI_40MHZ_ONLY)
972 tbl->is_SGI = 1;
973 else
974 tbl->is_SGI = 0;
975 } else if (priv->current_assoc_ht.sgf & HT_SHORT_GI_20MHZ_ONLY)
976 tbl->is_SGI = 1;
977 else
978 tbl->is_SGI = 0;
979
980 if (is_green)
981 tbl->is_SGI = 0;
982
983 rs_get_expected_tpt_table(lq_data, tbl);
984 rate = rs_get_best_rate(priv, lq_data, tbl, rate_mask, index, index);
985
986 IWL_DEBUG_HT("LQ: get best rate %d mask %X\n", rate, rate_mask);
987 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
988 IWL_DEBUG_HT("can not switch with index %d rate mask %x\n",
989 rate, rate_mask);
990 return -1;
991 }
992 rs_mcs_from_tbl(&tbl->current_rate, tbl, rate, is_green);
993 IWL_DEBUG_HT("LQ: Switch to new mcs %X index is green %X\n",
994 tbl->current_rate.rate_n_flags, is_green);
Mohamed Abbas403ab562007-11-06 22:06:25 -0800995 return 0;
996#else
997 return -1;
Zhu Yib481de92007-09-25 17:54:57 -0700998
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800999#endif /*CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001000}
1001
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001002static int rs_move_legacy_other(struct iwl4965_priv *priv,
1003 struct iwl4965_rate_scale_priv *lq_data,
Zhu Yib481de92007-09-25 17:54:57 -07001004 int index)
1005{
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001006 int ret = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001007 struct iwl4965_scale_tbl_info *tbl =
Zhu Yib481de92007-09-25 17:54:57 -07001008 &(lq_data->lq_info[lq_data->active_tbl]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001009 struct iwl4965_scale_tbl_info *search_tbl =
Zhu Yib481de92007-09-25 17:54:57 -07001010 &(lq_data->lq_info[(1 - lq_data->active_tbl)]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001011 struct iwl4965_rate_scale_data *window = &(tbl->win[index]);
1012 u32 sz = (sizeof(struct iwl4965_scale_tbl_info) -
1013 (sizeof(struct iwl4965_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001014 u8 start_action = tbl->action;
1015
1016 for (; ;) {
1017 switch (tbl->action) {
1018 case IWL_LEGACY_SWITCH_ANTENNA:
1019 IWL_DEBUG_HT("LQ Legacy switch Antenna\n");
1020
1021 search_tbl->lq_type = LQ_NONE;
1022 lq_data->action_counter++;
1023 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1024 break;
1025 if (!rs_is_other_ant_connected(lq_data->antenna,
1026 tbl->antenna_type))
1027 break;
1028
1029 memcpy(search_tbl, tbl, sz);
1030
1031 rs_toggle_antenna(&(search_tbl->current_rate),
1032 search_tbl);
1033 rs_get_expected_tpt_table(lq_data, search_tbl);
1034 lq_data->search_better_tbl = 1;
1035 goto out;
1036
1037 case IWL_LEGACY_SWITCH_SISO:
1038 IWL_DEBUG_HT("LQ: Legacy switch to SISO\n");
1039 memcpy(search_tbl, tbl, sz);
1040 search_tbl->lq_type = LQ_SISO;
1041 search_tbl->is_SGI = 0;
1042 search_tbl->is_fat = 0;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001043 ret = rs_switch_to_siso(priv, lq_data, search_tbl,
Zhu Yib481de92007-09-25 17:54:57 -07001044 index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001045 if (!ret) {
Zhu Yib481de92007-09-25 17:54:57 -07001046 lq_data->search_better_tbl = 1;
1047 lq_data->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001048 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001049 }
Zhu Yib481de92007-09-25 17:54:57 -07001050
1051 break;
1052 case IWL_LEGACY_SWITCH_MIMO:
1053 IWL_DEBUG_HT("LQ: Legacy switch MIMO\n");
1054 memcpy(search_tbl, tbl, sz);
1055 search_tbl->lq_type = LQ_MIMO;
1056 search_tbl->is_SGI = 0;
1057 search_tbl->is_fat = 0;
1058 search_tbl->antenna_type = ANT_BOTH;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001059 ret = rs_switch_to_mimo(priv, lq_data, search_tbl,
Zhu Yib481de92007-09-25 17:54:57 -07001060 index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001061 if (!ret) {
Zhu Yib481de92007-09-25 17:54:57 -07001062 lq_data->search_better_tbl = 1;
1063 lq_data->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001064 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001065 }
Zhu Yib481de92007-09-25 17:54:57 -07001066 break;
1067 }
1068 tbl->action++;
1069 if (tbl->action > IWL_LEGACY_SWITCH_MIMO)
1070 tbl->action = IWL_LEGACY_SWITCH_ANTENNA;
1071
1072 if (tbl->action == start_action)
1073 break;
1074
1075 }
1076 return 0;
1077
1078 out:
1079 tbl->action++;
1080 if (tbl->action > IWL_LEGACY_SWITCH_MIMO)
1081 tbl->action = IWL_LEGACY_SWITCH_ANTENNA;
1082 return 0;
1083
1084}
1085
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001086static int rs_move_siso_to_other(struct iwl4965_priv *priv,
1087 struct iwl4965_rate_scale_priv *lq_data,
Zhu Yib481de92007-09-25 17:54:57 -07001088 int index)
1089{
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001090 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001091 u8 is_green = lq_data->is_green;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001092 struct iwl4965_scale_tbl_info *tbl =
Zhu Yib481de92007-09-25 17:54:57 -07001093 &(lq_data->lq_info[lq_data->active_tbl]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001094 struct iwl4965_scale_tbl_info *search_tbl =
Zhu Yib481de92007-09-25 17:54:57 -07001095 &(lq_data->lq_info[(1 - lq_data->active_tbl)]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001096 struct iwl4965_rate_scale_data *window = &(tbl->win[index]);
1097 u32 sz = (sizeof(struct iwl4965_scale_tbl_info) -
1098 (sizeof(struct iwl4965_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001099 u8 start_action = tbl->action;
1100
1101 for (;;) {
1102 lq_data->action_counter++;
1103 switch (tbl->action) {
1104 case IWL_SISO_SWITCH_ANTENNA:
1105 IWL_DEBUG_HT("LQ: SISO SWITCH ANTENNA SISO\n");
1106 search_tbl->lq_type = LQ_NONE;
1107 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1108 break;
1109 if (!rs_is_other_ant_connected(lq_data->antenna,
1110 tbl->antenna_type))
1111 break;
1112
1113 memcpy(search_tbl, tbl, sz);
1114 search_tbl->action = IWL_SISO_SWITCH_MIMO;
1115 rs_toggle_antenna(&(search_tbl->current_rate),
1116 search_tbl);
1117 lq_data->search_better_tbl = 1;
1118
1119 goto out;
1120
1121 case IWL_SISO_SWITCH_MIMO:
1122 IWL_DEBUG_HT("LQ: SISO SWITCH TO MIMO FROM SISO\n");
1123 memcpy(search_tbl, tbl, sz);
1124 search_tbl->lq_type = LQ_MIMO;
1125 search_tbl->is_SGI = 0;
1126 search_tbl->is_fat = 0;
1127 search_tbl->antenna_type = ANT_BOTH;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001128 ret = rs_switch_to_mimo(priv, lq_data, search_tbl,
Zhu Yib481de92007-09-25 17:54:57 -07001129 index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001130 if (!ret) {
Zhu Yib481de92007-09-25 17:54:57 -07001131 lq_data->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001132 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001133 }
Zhu Yib481de92007-09-25 17:54:57 -07001134 break;
1135 case IWL_SISO_SWITCH_GI:
1136 IWL_DEBUG_HT("LQ: SISO SWITCH TO GI\n");
1137 memcpy(search_tbl, tbl, sz);
1138 search_tbl->action = 0;
1139 if (search_tbl->is_SGI)
1140 search_tbl->is_SGI = 0;
1141 else if (!is_green)
1142 search_tbl->is_SGI = 1;
1143 else
1144 break;
1145 lq_data->search_better_tbl = 1;
1146 if ((tbl->lq_type == LQ_SISO) &&
1147 (tbl->is_SGI)) {
1148 s32 tpt = lq_data->last_tpt / 100;
1149 if (((!tbl->is_fat) &&
1150 (tpt >= expected_tpt_siso20MHz[index])) ||
1151 ((tbl->is_fat) &&
1152 (tpt >= expected_tpt_siso40MHz[index])))
1153 lq_data->search_better_tbl = 0;
1154 }
1155 rs_get_expected_tpt_table(lq_data, search_tbl);
1156 rs_mcs_from_tbl(&search_tbl->current_rate,
1157 search_tbl, index, is_green);
1158 goto out;
1159 }
1160 tbl->action++;
1161 if (tbl->action > IWL_SISO_SWITCH_GI)
1162 tbl->action = IWL_SISO_SWITCH_ANTENNA;
1163
1164 if (tbl->action == start_action)
1165 break;
1166 }
1167 return 0;
1168
1169 out:
1170 tbl->action++;
1171 if (tbl->action > IWL_SISO_SWITCH_GI)
1172 tbl->action = IWL_SISO_SWITCH_ANTENNA;
1173 return 0;
1174}
1175
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001176static int rs_move_mimo_to_other(struct iwl4965_priv *priv,
1177 struct iwl4965_rate_scale_priv *lq_data,
Zhu Yib481de92007-09-25 17:54:57 -07001178 int index)
1179{
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001180 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001181 s8 is_green = lq_data->is_green;
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 u32 sz = (sizeof(struct iwl4965_scale_tbl_info) -
1187 (sizeof(struct iwl4965_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001188 u8 start_action = tbl->action;
1189
1190 for (;;) {
1191 lq_data->action_counter++;
1192 switch (tbl->action) {
1193 case IWL_MIMO_SWITCH_ANTENNA_A:
1194 case IWL_MIMO_SWITCH_ANTENNA_B:
1195 IWL_DEBUG_HT("LQ: MIMO SWITCH TO SISO\n");
1196 memcpy(search_tbl, tbl, sz);
1197 search_tbl->lq_type = LQ_SISO;
1198 search_tbl->is_SGI = 0;
1199 search_tbl->is_fat = 0;
1200 if (tbl->action == IWL_MIMO_SWITCH_ANTENNA_A)
1201 search_tbl->antenna_type = ANT_MAIN;
1202 else
1203 search_tbl->antenna_type = ANT_AUX;
1204
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001205 ret = rs_switch_to_siso(priv, lq_data, search_tbl,
Zhu Yib481de92007-09-25 17:54:57 -07001206 index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001207 if (!ret) {
Zhu Yib481de92007-09-25 17:54:57 -07001208 lq_data->search_better_tbl = 1;
1209 goto out;
1210 }
1211 break;
1212
1213 case IWL_MIMO_SWITCH_GI:
1214 IWL_DEBUG_HT("LQ: MIMO SWITCH TO GI\n");
1215 memcpy(search_tbl, tbl, sz);
1216 search_tbl->lq_type = LQ_MIMO;
1217 search_tbl->antenna_type = ANT_BOTH;
1218 search_tbl->action = 0;
1219 if (search_tbl->is_SGI)
1220 search_tbl->is_SGI = 0;
1221 else
1222 search_tbl->is_SGI = 1;
1223 lq_data->search_better_tbl = 1;
1224 if ((tbl->lq_type == LQ_MIMO) &&
1225 (tbl->is_SGI)) {
1226 s32 tpt = lq_data->last_tpt / 100;
1227 if (((!tbl->is_fat) &&
1228 (tpt >= expected_tpt_mimo20MHz[index])) ||
1229 ((tbl->is_fat) &&
1230 (tpt >= expected_tpt_mimo40MHz[index])))
1231 lq_data->search_better_tbl = 0;
1232 }
1233 rs_get_expected_tpt_table(lq_data, search_tbl);
1234 rs_mcs_from_tbl(&search_tbl->current_rate,
1235 search_tbl, index, is_green);
1236 goto out;
1237
1238 }
1239 tbl->action++;
1240 if (tbl->action > IWL_MIMO_SWITCH_GI)
1241 tbl->action = IWL_MIMO_SWITCH_ANTENNA_A;
1242
1243 if (tbl->action == start_action)
1244 break;
1245 }
1246
1247 return 0;
1248 out:
1249 tbl->action++;
1250 if (tbl->action > IWL_MIMO_SWITCH_GI)
1251 tbl->action = IWL_MIMO_SWITCH_ANTENNA_A;
1252 return 0;
1253
1254}
1255
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001256static void rs_stay_in_table(struct iwl4965_rate_scale_priv *lq_data)
Zhu Yib481de92007-09-25 17:54:57 -07001257{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001258 struct iwl4965_scale_tbl_info *tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001259 int i;
1260 int active_tbl;
1261 int flush_interval_passed = 0;
1262
1263 active_tbl = lq_data->active_tbl;
1264
1265 tbl = &(lq_data->lq_info[active_tbl]);
1266
1267 if (lq_data->stay_in_tbl) {
1268
1269 if (lq_data->flush_timer)
1270 flush_interval_passed =
1271 time_after(jiffies,
1272 (unsigned long)(lq_data->flush_timer +
1273 IWL_RATE_SCALE_FLUSH_INTVL));
1274
1275 flush_interval_passed = 0;
1276 if ((lq_data->total_failed > lq_data->max_failure_limit) ||
1277 (lq_data->total_success > lq_data->max_success_limit) ||
1278 ((!lq_data->search_better_tbl) && (lq_data->flush_timer)
1279 && (flush_interval_passed))) {
1280 IWL_DEBUG_HT("LQ: stay is expired %d %d %d\n:",
1281 lq_data->total_failed,
1282 lq_data->total_success,
1283 flush_interval_passed);
1284 lq_data->stay_in_tbl = 0;
1285 lq_data->total_failed = 0;
1286 lq_data->total_success = 0;
1287 lq_data->flush_timer = 0;
Mohamed Abbas403ab562007-11-06 22:06:25 -08001288 } else {
Zhu Yib481de92007-09-25 17:54:57 -07001289 lq_data->table_count++;
1290 if (lq_data->table_count >=
1291 lq_data->table_count_limit) {
1292 lq_data->table_count = 0;
1293
1294 IWL_DEBUG_HT("LQ: stay in table clear win\n");
1295 for (i = 0; i < IWL_RATE_COUNT; i++)
1296 rs_rate_scale_clear_window(
1297 &(tbl->win[i]));
1298 }
1299 }
1300
1301 if (!lq_data->stay_in_tbl) {
1302 for (i = 0; i < IWL_RATE_COUNT; i++)
1303 rs_rate_scale_clear_window(&(tbl->win[i]));
1304 }
1305 }
1306}
1307
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001308static void rs_rate_scale_perform(struct iwl4965_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001309 struct net_device *dev,
1310 struct ieee80211_hdr *hdr,
1311 struct sta_info *sta)
1312{
1313 int low = IWL_RATE_INVALID;
1314 int high = IWL_RATE_INVALID;
1315 int index;
1316 int i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001317 struct iwl4965_rate_scale_data *window = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001318 int current_tpt = IWL_INVALID_VALUE;
1319 int low_tpt = IWL_INVALID_VALUE;
1320 int high_tpt = IWL_INVALID_VALUE;
1321 u32 fail_count;
1322 s8 scale_action = 0;
1323 u16 fc, rate_mask;
1324 u8 update_lq = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001325 struct iwl4965_rate_scale_priv *lq_data;
1326 struct iwl4965_scale_tbl_info *tbl, *tbl1;
Zhu Yib481de92007-09-25 17:54:57 -07001327 u16 rate_scale_index_msk = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001328 struct iwl4965_rate mcs_rate;
Zhu Yib481de92007-09-25 17:54:57 -07001329 u8 is_green = 0;
1330 u8 active_tbl = 0;
1331 u8 done_search = 0;
1332 u16 high_low;
1333
1334 IWL_DEBUG_RATE("rate scale calculate new rate for skb\n");
1335
1336 fc = le16_to_cpu(hdr->frame_control);
1337 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1)) {
1338 /* Send management frames and broadcast/multicast data using
1339 * lowest rate. */
1340 /* TODO: this could probably be improved.. */
1341 return;
1342 }
1343
1344 if (!sta || !sta->rate_ctrl_priv)
1345 return;
1346
1347 if (!priv->lq_mngr.lq_ready) {
1348 IWL_DEBUG_RATE("still rate scaling not ready\n");
1349 return;
1350 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001351 lq_data = (struct iwl4965_rate_scale_priv *)sta->rate_ctrl_priv;
Zhu Yib481de92007-09-25 17:54:57 -07001352
1353 if (!lq_data->search_better_tbl)
1354 active_tbl = lq_data->active_tbl;
1355 else
1356 active_tbl = 1 - lq_data->active_tbl;
1357
1358 tbl = &(lq_data->lq_info[active_tbl]);
1359 is_green = lq_data->is_green;
1360
1361 index = sta->last_txrate;
1362
1363 IWL_DEBUG_RATE("Rate scale index %d for type %d\n", index,
1364 tbl->lq_type);
1365
1366 rs_get_supported_rates(lq_data, hdr, tbl->lq_type,
1367 &rate_mask);
1368
1369 IWL_DEBUG_RATE("mask 0x%04X \n", rate_mask);
1370
1371 /* mask with station rate restriction */
1372 if (is_legacy(tbl->lq_type)) {
1373 if (lq_data->phymode == (u8) MODE_IEEE80211A)
1374 rate_scale_index_msk = (u16) (rate_mask &
Zhu Yi02dede02007-09-27 11:27:40 +08001375 (lq_data->supp_rates << IWL_FIRST_OFDM_RATE));
Zhu Yib481de92007-09-25 17:54:57 -07001376 else
1377 rate_scale_index_msk = (u16) (rate_mask &
Zhu Yi02dede02007-09-27 11:27:40 +08001378 lq_data->supp_rates);
Zhu Yib481de92007-09-25 17:54:57 -07001379
1380 } else
1381 rate_scale_index_msk = rate_mask;
1382
1383 if (!rate_scale_index_msk)
1384 rate_scale_index_msk = rate_mask;
1385
1386 if (index < 0 || !((1 << index) & rate_scale_index_msk)) {
1387 index = IWL_INVALID_VALUE;
1388 update_lq = 1;
1389
Ian Schram01ebd062007-10-25 17:15:22 +08001390 /* get the lowest available rate */
Zhu Yib481de92007-09-25 17:54:57 -07001391 for (i = 0; i <= IWL_RATE_COUNT; i++) {
1392 if ((1 << i) & rate_scale_index_msk)
1393 index = i;
1394 }
1395
1396 if (index == IWL_INVALID_VALUE) {
1397 IWL_WARNING("Can not find a suitable rate\n");
1398 return;
1399 }
1400 }
1401
1402 if (!tbl->expected_tpt)
1403 rs_get_expected_tpt_table(lq_data, tbl);
1404
1405 window = &(tbl->win[index]);
1406
1407 fail_count = window->counter - window->success_counter;
1408 if (((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
1409 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH))
1410 || (tbl->expected_tpt == NULL)) {
1411 IWL_DEBUG_RATE("LQ: still below TH succ %d total %d "
1412 "for index %d\n",
1413 window->success_counter, window->counter, index);
1414 window->average_tpt = IWL_INVALID_VALUE;
1415 rs_stay_in_table(lq_data);
1416 if (update_lq) {
1417 rs_mcs_from_tbl(&mcs_rate, tbl, index, is_green);
Zhu Yi02dede02007-09-27 11:27:40 +08001418 rs_fill_link_cmd(lq_data, &mcs_rate, &lq_data->lq);
Zhu Yib481de92007-09-25 17:54:57 -07001419 rs_send_lq_cmd(priv, &lq_data->lq, CMD_ASYNC);
1420 }
1421 goto out;
1422
1423 } else
1424 window->average_tpt = ((window->success_ratio *
1425 tbl->expected_tpt[index] + 64) / 128);
1426
1427 if (lq_data->search_better_tbl) {
1428 int success_limit = IWL_RATE_SCALE_SWITCH;
1429
1430 if ((window->success_ratio > success_limit) ||
1431 (window->average_tpt > lq_data->last_tpt)) {
1432 if (!is_legacy(tbl->lq_type)) {
1433 IWL_DEBUG_HT("LQ: we are switching to HT"
1434 " rate suc %d current tpt %d"
1435 " old tpt %d\n",
1436 window->success_ratio,
1437 window->average_tpt,
1438 lq_data->last_tpt);
1439 lq_data->enable_counter = 1;
1440 }
1441 lq_data->active_tbl = active_tbl;
1442 current_tpt = window->average_tpt;
1443 } else {
1444 tbl->lq_type = LQ_NONE;
1445 active_tbl = lq_data->active_tbl;
1446 tbl = &(lq_data->lq_info[active_tbl]);
1447
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001448 index = iwl4965_rate_index_from_plcp(
Zhu Yib481de92007-09-25 17:54:57 -07001449 tbl->current_rate.rate_n_flags);
1450
1451 update_lq = 1;
1452 current_tpt = lq_data->last_tpt;
1453 IWL_DEBUG_HT("XXY GO BACK TO OLD TABLE\n");
1454 }
1455 lq_data->search_better_tbl = 0;
1456 done_search = 1;
1457 goto lq_update;
1458 }
1459
1460 high_low = rs_get_adjacent_rate(index, rate_scale_index_msk,
1461 tbl->lq_type);
1462 low = high_low & 0xff;
1463 high = (high_low >> 8) & 0xff;
1464
1465 current_tpt = window->average_tpt;
1466
1467 if (low != IWL_RATE_INVALID)
1468 low_tpt = tbl->win[low].average_tpt;
1469
1470 if (high != IWL_RATE_INVALID)
1471 high_tpt = tbl->win[high].average_tpt;
1472
1473
1474 scale_action = 1;
1475
1476 if ((window->success_ratio <= IWL_RATE_DECREASE_TH) ||
1477 (current_tpt == 0)) {
1478 IWL_DEBUG_RATE("decrease rate because of low success_ratio\n");
1479 scale_action = -1;
1480 } else if ((low_tpt == IWL_INVALID_VALUE) &&
1481 (high_tpt == IWL_INVALID_VALUE))
1482 scale_action = 1;
1483 else if ((low_tpt != IWL_INVALID_VALUE) &&
1484 (high_tpt != IWL_INVALID_VALUE) &&
1485 (low_tpt < current_tpt) &&
1486 (high_tpt < current_tpt))
1487 scale_action = 0;
1488 else {
1489 if (high_tpt != IWL_INVALID_VALUE) {
1490 if (high_tpt > current_tpt)
1491 scale_action = 1;
1492 else {
1493 IWL_DEBUG_RATE
1494 ("decrease rate because of high tpt\n");
1495 scale_action = -1;
1496 }
1497 } else if (low_tpt != IWL_INVALID_VALUE) {
1498 if (low_tpt > current_tpt) {
1499 IWL_DEBUG_RATE
1500 ("decrease rate because of low tpt\n");
1501 scale_action = -1;
1502 } else
1503 scale_action = 1;
1504 }
1505 }
1506
1507 if (scale_action == -1) {
1508 if ((low != IWL_RATE_INVALID) &&
1509 ((window->success_ratio > IWL_RATE_HIGH_TH) ||
1510 (current_tpt > (100 * tbl->expected_tpt[low]))))
1511 scale_action = 0;
1512 } else if ((scale_action == 1) &&
1513 (window->success_ratio < IWL_RATE_INCREASE_TH))
1514 scale_action = 0;
1515
1516 switch (scale_action) {
1517 case -1:
1518 if (low != IWL_RATE_INVALID) {
1519 update_lq = 1;
1520 index = low;
1521 }
1522 break;
1523 case 1:
1524 if (high != IWL_RATE_INVALID) {
1525 update_lq = 1;
1526 index = high;
1527 }
1528
1529 break;
1530 case 0:
1531 default:
1532 break;
1533 }
1534
1535 IWL_DEBUG_HT("choose rate scale index %d action %d low %d "
1536 "high %d type %d\n",
1537 index, scale_action, low, high, tbl->lq_type);
1538
1539 lq_update:
1540 if (update_lq) {
1541 rs_mcs_from_tbl(&mcs_rate, tbl, index, is_green);
Zhu Yi02dede02007-09-27 11:27:40 +08001542 rs_fill_link_cmd(lq_data, &mcs_rate, &lq_data->lq);
Zhu Yib481de92007-09-25 17:54:57 -07001543 rs_send_lq_cmd(priv, &lq_data->lq, CMD_ASYNC);
1544 }
1545 rs_stay_in_table(lq_data);
1546
1547 if (!update_lq && !done_search && !lq_data->stay_in_tbl) {
1548 lq_data->last_tpt = current_tpt;
1549
1550 if (is_legacy(tbl->lq_type))
1551 rs_move_legacy_other(priv, lq_data, index);
1552 else if (is_siso(tbl->lq_type))
1553 rs_move_siso_to_other(priv, lq_data, index);
1554 else
1555 rs_move_mimo_to_other(priv, lq_data, index);
1556
1557 if (lq_data->search_better_tbl) {
1558 tbl = &(lq_data->lq_info[(1 - lq_data->active_tbl)]);
1559 for (i = 0; i < IWL_RATE_COUNT; i++)
1560 rs_rate_scale_clear_window(&(tbl->win[i]));
1561
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001562 index = iwl4965_rate_index_from_plcp(
Zhu Yib481de92007-09-25 17:54:57 -07001563 tbl->current_rate.rate_n_flags);
1564
1565 IWL_DEBUG_HT("Switch current mcs: %X index: %d\n",
1566 tbl->current_rate.rate_n_flags, index);
1567 rs_fill_link_cmd(lq_data, &tbl->current_rate,
Zhu Yi02dede02007-09-27 11:27:40 +08001568 &lq_data->lq);
Zhu Yib481de92007-09-25 17:54:57 -07001569 rs_send_lq_cmd(priv, &lq_data->lq, CMD_ASYNC);
1570 }
1571 tbl1 = &(lq_data->lq_info[lq_data->active_tbl]);
1572
1573 if (is_legacy(tbl1->lq_type) &&
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001574#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07001575 !priv->current_assoc_ht.is_ht &&
1576#endif
1577 (lq_data->action_counter >= 1)) {
1578 lq_data->action_counter = 0;
1579 IWL_DEBUG_HT("LQ: STAY in legacy table\n");
1580 rs_set_stay_in_table(1, lq_data);
1581 }
1582
1583 if (lq_data->enable_counter &&
1584 (lq_data->action_counter >= IWL_ACTION_LIMIT)) {
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001585#ifdef CONFIG_IWL4965_HT_AGG
Zhu Yib481de92007-09-25 17:54:57 -07001586 if ((lq_data->last_tpt > TID_AGG_TPT_THREHOLD) &&
1587 (priv->lq_mngr.agg_ctrl.auto_agg)) {
1588 priv->lq_mngr.agg_ctrl.tid_retry =
1589 TID_ALL_SPECIFIED;
1590 schedule_work(&priv->agg_work);
1591 }
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001592#endif /*CONFIG_IWL4965_HT_AGG */
Zhu Yib481de92007-09-25 17:54:57 -07001593 lq_data->action_counter = 0;
1594 rs_set_stay_in_table(0, lq_data);
1595 }
1596 } else {
1597 if ((!update_lq) && (!done_search) && (!lq_data->flush_timer))
1598 lq_data->flush_timer = jiffies;
1599 }
1600
1601out:
1602 rs_mcs_from_tbl(&tbl->current_rate, tbl, index, is_green);
1603 i = index;
1604 sta->last_txrate = i;
1605
1606 /* sta->txrate is an index to A mode rates which start
1607 * at IWL_FIRST_OFDM_RATE
1608 */
1609 if (lq_data->phymode == (u8) MODE_IEEE80211A)
1610 sta->txrate = i - IWL_FIRST_OFDM_RATE;
1611 else
1612 sta->txrate = i;
1613
1614 return;
1615}
1616
1617
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001618static void rs_initialize_lq(struct iwl4965_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001619 struct sta_info *sta)
1620{
1621 int i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001622 struct iwl4965_rate_scale_priv *lq;
1623 struct iwl4965_scale_tbl_info *tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001624 u8 active_tbl = 0;
1625 int rate_idx;
1626 u8 use_green = rs_use_green(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001627 struct iwl4965_rate mcs_rate;
Zhu Yib481de92007-09-25 17:54:57 -07001628
1629 if (!sta || !sta->rate_ctrl_priv)
1630 goto out;
1631
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001632 lq = (struct iwl4965_rate_scale_priv *)sta->rate_ctrl_priv;
Zhu Yib481de92007-09-25 17:54:57 -07001633 i = sta->last_txrate;
1634
1635 if ((lq->lq.sta_id == 0xff) &&
1636 (priv->iw_mode == IEEE80211_IF_TYPE_IBSS))
1637 goto out;
1638
1639 if (!lq->search_better_tbl)
1640 active_tbl = lq->active_tbl;
1641 else
1642 active_tbl = 1 - lq->active_tbl;
1643
1644 tbl = &(lq->lq_info[active_tbl]);
1645
1646 if ((i < 0) || (i >= IWL_RATE_COUNT))
1647 i = 0;
1648
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001649 mcs_rate.rate_n_flags = iwl4965_rates[i].plcp ;
Zhu Yib481de92007-09-25 17:54:57 -07001650 mcs_rate.rate_n_flags |= RATE_MCS_ANT_B_MSK;
1651 mcs_rate.rate_n_flags &= ~RATE_MCS_ANT_A_MSK;
1652
1653 if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
1654 mcs_rate.rate_n_flags |= RATE_MCS_CCK_MSK;
1655
1656 tbl->antenna_type = ANT_AUX;
1657 rs_get_tbl_info_from_mcs(&mcs_rate, priv->phymode, tbl, &rate_idx);
1658 if (!rs_is_ant_connected(priv->valid_antenna, tbl->antenna_type))
Zhu Yi46640a82007-09-27 11:27:34 +08001659 rs_toggle_antenna(&mcs_rate, tbl);
Zhu Yib481de92007-09-25 17:54:57 -07001660
1661 rs_mcs_from_tbl(&mcs_rate, tbl, rate_idx, use_green);
1662 tbl->current_rate.rate_n_flags = mcs_rate.rate_n_flags;
1663 rs_get_expected_tpt_table(lq, tbl);
Zhu Yi02dede02007-09-27 11:27:40 +08001664 rs_fill_link_cmd(lq, &mcs_rate, &lq->lq);
Zhu Yib481de92007-09-25 17:54:57 -07001665 rs_send_lq_cmd(priv, &lq->lq, CMD_ASYNC);
1666 out:
1667 return;
1668}
1669
Mattias Nissler1abbe492007-12-20 13:50:07 +01001670static void rs_get_rate(void *priv_rate, struct net_device *dev,
1671 struct ieee80211_hw_mode *mode, struct sk_buff *skb,
1672 struct rate_selection *sel)
Zhu Yib481de92007-09-25 17:54:57 -07001673{
1674
1675 int i;
1676 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1677 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1678 struct sta_info *sta;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001679 struct iwl4965_priv *priv = (struct iwl4965_priv *)priv_rate;
1680 struct iwl4965_rate_scale_priv *lq;
Zhu Yib481de92007-09-25 17:54:57 -07001681
Zhu Yi58826352007-09-27 11:27:39 +08001682 IWL_DEBUG_RATE_LIMIT("rate scale calculate new rate for skb\n");
Zhu Yib481de92007-09-25 17:54:57 -07001683
Zhu Yib481de92007-09-25 17:54:57 -07001684 sta = sta_info_get(local, hdr->addr1);
1685
1686 if (!sta || !sta->rate_ctrl_priv) {
Mattias Nissler1abbe492007-12-20 13:50:07 +01001687 sel->rate = rate_lowest(local, local->oper_hw_mode, sta);
Zhu Yib481de92007-09-25 17:54:57 -07001688 if (sta)
1689 sta_info_put(sta);
Mattias Nissler1abbe492007-12-20 13:50:07 +01001690 return;
Zhu Yib481de92007-09-25 17:54:57 -07001691 }
1692
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001693 lq = (struct iwl4965_rate_scale_priv *)sta->rate_ctrl_priv;
Zhu Yib481de92007-09-25 17:54:57 -07001694 i = sta->last_txrate;
1695
1696 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) && !lq->ibss_sta_added) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001697 u8 sta_id = iwl4965_hw_find_station(priv, hdr->addr1);
Joe Perches0795af52007-10-03 17:59:30 -07001698 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07001699
1700 if (sta_id == IWL_INVALID_STATION) {
Joe Perches0795af52007-10-03 17:59:30 -07001701 IWL_DEBUG_RATE("LQ: ADD station %s\n",
1702 print_mac(mac, hdr->addr1));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001703 sta_id = iwl4965_add_station_flags(priv,
Zhu Yib481de92007-09-25 17:54:57 -07001704 hdr->addr1, 0, CMD_ASYNC);
1705 }
1706 if ((sta_id != IWL_INVALID_STATION)) {
1707 lq->lq.sta_id = sta_id;
1708 lq->lq.rs_table[0].rate_n_flags = 0;
1709 lq->ibss_sta_added = 1;
1710 rs_initialize_lq(priv, sta);
1711 }
1712 if (!lq->ibss_sta_added)
1713 goto done;
1714 }
1715
1716 done:
Mattias Nissler1abbe492007-12-20 13:50:07 +01001717 if ((i < 0) || (i > IWL_RATE_COUNT)) {
1718 sel->rate = rate_lowest(local, local->oper_hw_mode, sta);
1719 return;
1720 }
Zhu Yib481de92007-09-25 17:54:57 -07001721 sta_info_put(sta);
Zhu Yib481de92007-09-25 17:54:57 -07001722
Mattias Nissler1abbe492007-12-20 13:50:07 +01001723 sel->rate = &priv->ieee_rates[i];
Zhu Yib481de92007-09-25 17:54:57 -07001724}
1725
1726static void *rs_alloc_sta(void *priv, gfp_t gfp)
1727{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001728 struct iwl4965_rate_scale_priv *crl;
Zhu Yib481de92007-09-25 17:54:57 -07001729 int i, j;
1730
1731 IWL_DEBUG_RATE("create station rate scale window\n");
1732
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001733 crl = kzalloc(sizeof(struct iwl4965_rate_scale_priv), gfp);
Zhu Yib481de92007-09-25 17:54:57 -07001734
1735 if (crl == NULL)
1736 return NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001737 crl->lq.sta_id = 0xff;
1738
Zhu Yi63fddb92007-09-27 11:27:36 +08001739
Zhu Yib481de92007-09-25 17:54:57 -07001740 for (j = 0; j < LQ_SIZE; j++)
1741 for (i = 0; i < IWL_RATE_COUNT; i++)
1742 rs_rate_scale_clear_window(&(crl->lq_info[j].win[i]));
1743
1744 return crl;
1745}
1746
1747static void rs_rate_init(void *priv_rate, void *priv_sta,
1748 struct ieee80211_local *local,
1749 struct sta_info *sta)
1750{
1751 int i, j;
1752 struct ieee80211_hw_mode *mode = local->oper_hw_mode;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001753 struct iwl4965_priv *priv = (struct iwl4965_priv *)priv_rate;
1754 struct iwl4965_rate_scale_priv *crl = priv_sta;
Zhu Yib481de92007-09-25 17:54:57 -07001755
Zhu Yib481de92007-09-25 17:54:57 -07001756 crl->flush_timer = 0;
Zhu Yi02dede02007-09-27 11:27:40 +08001757 crl->supp_rates = sta->supp_rates;
Zhu Yib481de92007-09-25 17:54:57 -07001758 sta->txrate = 3;
1759 for (j = 0; j < LQ_SIZE; j++)
1760 for (i = 0; i < IWL_RATE_COUNT; i++)
1761 rs_rate_scale_clear_window(&(crl->lq_info[j].win[i]));
1762
1763 IWL_DEBUG_RATE("rate scale global init\n");
1764 /* TODO: what is a good starting rate for STA? About middle? Maybe not
1765 * the lowest or the highest rate.. Could consider using RSSI from
1766 * previous packets? Need to have IEEE 802.1X auth succeed immediately
1767 * after assoc.. */
1768
1769 crl->ibss_sta_added = 0;
1770 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001771 u8 sta_id = iwl4965_hw_find_station(priv, sta->addr);
Joe Perches0795af52007-10-03 17:59:30 -07001772 DECLARE_MAC_BUF(mac);
1773
Zhu Yib481de92007-09-25 17:54:57 -07001774 /* for IBSS the call are from tasklet */
Joe Perches0795af52007-10-03 17:59:30 -07001775 IWL_DEBUG_HT("LQ: ADD station %s\n",
1776 print_mac(mac, sta->addr));
Zhu Yib481de92007-09-25 17:54:57 -07001777
1778 if (sta_id == IWL_INVALID_STATION) {
Joe Perches0795af52007-10-03 17:59:30 -07001779 IWL_DEBUG_RATE("LQ: ADD station %s\n",
1780 print_mac(mac, sta->addr));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001781 sta_id = iwl4965_add_station_flags(priv,
Zhu Yib481de92007-09-25 17:54:57 -07001782 sta->addr, 0, CMD_ASYNC);
1783 }
1784 if ((sta_id != IWL_INVALID_STATION)) {
1785 crl->lq.sta_id = sta_id;
1786 crl->lq.rs_table[0].rate_n_flags = 0;
1787 }
1788 /* FIXME: this is w/a remove it later */
1789 priv->assoc_station_added = 1;
1790 }
1791
1792 for (i = 0; i < mode->num_rates; i++) {
1793 if ((sta->supp_rates & BIT(i)) &&
1794 (mode->rates[i].flags & IEEE80211_RATE_SUPPORTED))
1795 sta->txrate = i;
1796 }
1797 sta->last_txrate = sta->txrate;
1798 /* For MODE_IEEE80211A mode cck rate are at end
1799 * rate table
1800 */
1801 if (local->hw.conf.phymode == MODE_IEEE80211A)
1802 sta->last_txrate += IWL_FIRST_OFDM_RATE;
1803
1804 crl->is_dup = priv->is_dup;
1805 crl->valid_antenna = priv->valid_antenna;
1806 crl->antenna = priv->antenna;
1807 crl->is_green = rs_use_green(priv);
1808 crl->active_rate = priv->active_rate;
1809 crl->active_rate &= ~(0x1000);
1810 crl->active_rate_basic = priv->active_rate_basic;
1811 crl->phymode = priv->phymode;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001812#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07001813 crl->active_siso_rate = (priv->current_assoc_ht.supp_rates[0] << 1);
1814 crl->active_siso_rate |= (priv->current_assoc_ht.supp_rates[0] & 0x1);
1815 crl->active_siso_rate &= ~((u16)0x2);
1816 crl->active_siso_rate = crl->active_siso_rate << IWL_FIRST_OFDM_RATE;
1817
1818 crl->active_mimo_rate = (priv->current_assoc_ht.supp_rates[1] << 1);
1819 crl->active_mimo_rate |= (priv->current_assoc_ht.supp_rates[1] & 0x1);
1820 crl->active_mimo_rate &= ~((u16)0x2);
1821 crl->active_mimo_rate = crl->active_mimo_rate << IWL_FIRST_OFDM_RATE;
1822 IWL_DEBUG_HT("MIMO RATE 0x%X SISO MASK 0x%X\n", crl->active_siso_rate,
1823 crl->active_mimo_rate);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001824#endif /*CONFIG_IWL4965_HT*/
Zhu Yi98d7e092007-09-27 11:27:42 +08001825#ifdef CONFIG_MAC80211_DEBUGFS
1826 crl->drv = priv;
1827#endif
Zhu Yib481de92007-09-25 17:54:57 -07001828
1829 if (priv->assoc_station_added)
1830 priv->lq_mngr.lq_ready = 1;
1831
1832 rs_initialize_lq(priv, sta);
1833}
1834
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001835static void rs_fill_link_cmd(struct iwl4965_rate_scale_priv *lq_data,
1836 struct iwl4965_rate *tx_mcs,
1837 struct iwl4965_link_quality_cmd *lq_cmd)
Zhu Yib481de92007-09-25 17:54:57 -07001838{
1839 int index = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001840 int rate_idx;
Zhu Yi1b696de2007-09-27 11:27:41 +08001841 int repeat_rate = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001842 u8 ant_toggle_count = 0;
1843 u8 use_ht_possible = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001844 struct iwl4965_rate new_rate;
1845 struct iwl4965_scale_tbl_info tbl_type = { 0 };
Zhu Yib481de92007-09-25 17:54:57 -07001846
Zhu Yi98d7e092007-09-27 11:27:42 +08001847 rs_dbgfs_set_mcs(lq_data, tx_mcs, index);
1848
Zhu Yib481de92007-09-25 17:54:57 -07001849 rs_get_tbl_info_from_mcs(tx_mcs, lq_data->phymode,
1850 &tbl_type, &rate_idx);
1851
1852 if (is_legacy(tbl_type.lq_type)) {
1853 ant_toggle_count = 1;
Zhu Yi1b696de2007-09-27 11:27:41 +08001854 repeat_rate = IWL_NUMBER_TRY;
Zhu Yib481de92007-09-25 17:54:57 -07001855 } else
Zhu Yi1b696de2007-09-27 11:27:41 +08001856 repeat_rate = IWL_HT_NUMBER_TRY;
Zhu Yib481de92007-09-25 17:54:57 -07001857
1858 lq_cmd->general_params.mimo_delimiter =
1859 is_mimo(tbl_type.lq_type) ? 1 : 0;
1860 lq_cmd->rs_table[index].rate_n_flags =
1861 cpu_to_le32(tx_mcs->rate_n_flags);
1862 new_rate.rate_n_flags = tx_mcs->rate_n_flags;
1863
1864 if (is_mimo(tbl_type.lq_type) || (tbl_type.antenna_type == ANT_MAIN))
1865 lq_cmd->general_params.single_stream_ant_msk = 1;
1866 else
1867 lq_cmd->general_params.single_stream_ant_msk = 2;
1868
1869 index++;
Zhu Yi1b696de2007-09-27 11:27:41 +08001870 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07001871
1872 while (index < LINK_QUAL_MAX_RETRY_NUM) {
Zhu Yi1b696de2007-09-27 11:27:41 +08001873 while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
Zhu Yib481de92007-09-25 17:54:57 -07001874 if (is_legacy(tbl_type.lq_type)) {
1875 if (ant_toggle_count <
1876 NUM_TRY_BEFORE_ANTENNA_TOGGLE)
1877 ant_toggle_count++;
1878 else {
1879 rs_toggle_antenna(&new_rate, &tbl_type);
1880 ant_toggle_count = 1;
1881 }
1882 }
Zhu Yi98d7e092007-09-27 11:27:42 +08001883
1884 rs_dbgfs_set_mcs(lq_data, &new_rate, index);
Zhu Yib481de92007-09-25 17:54:57 -07001885 lq_cmd->rs_table[index].rate_n_flags =
1886 cpu_to_le32(new_rate.rate_n_flags);
Zhu Yi1b696de2007-09-27 11:27:41 +08001887 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07001888 index++;
1889 }
1890
1891 rs_get_tbl_info_from_mcs(&new_rate, lq_data->phymode, &tbl_type,
1892 &rate_idx);
1893
1894 if (is_mimo(tbl_type.lq_type))
1895 lq_cmd->general_params.mimo_delimiter = index;
1896
1897 rs_get_lower_rate(lq_data, &tbl_type, rate_idx,
Zhu Yi02dede02007-09-27 11:27:40 +08001898 use_ht_possible, &new_rate);
Zhu Yib481de92007-09-25 17:54:57 -07001899
1900 if (is_legacy(tbl_type.lq_type)) {
1901 if (ant_toggle_count < NUM_TRY_BEFORE_ANTENNA_TOGGLE)
1902 ant_toggle_count++;
1903 else {
1904 rs_toggle_antenna(&new_rate, &tbl_type);
1905 ant_toggle_count = 1;
1906 }
Zhu Yi1b696de2007-09-27 11:27:41 +08001907 repeat_rate = IWL_NUMBER_TRY;
Zhu Yib481de92007-09-25 17:54:57 -07001908 } else
Zhu Yi1b696de2007-09-27 11:27:41 +08001909 repeat_rate = IWL_HT_NUMBER_TRY;
Zhu Yib481de92007-09-25 17:54:57 -07001910
1911 use_ht_possible = 0;
1912
Zhu Yi98d7e092007-09-27 11:27:42 +08001913 rs_dbgfs_set_mcs(lq_data, &new_rate, index);
Zhu Yib481de92007-09-25 17:54:57 -07001914 lq_cmd->rs_table[index].rate_n_flags =
1915 cpu_to_le32(new_rate.rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -07001916
1917 index++;
Zhu Yi1b696de2007-09-27 11:27:41 +08001918 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07001919 }
1920
Zhu Yib481de92007-09-25 17:54:57 -07001921 lq_cmd->general_params.dual_stream_ant_msk = 3;
1922 lq_cmd->agg_params.agg_dis_start_th = 3;
1923 lq_cmd->agg_params.agg_time_limit = cpu_to_le16(4000);
Zhu Yib481de92007-09-25 17:54:57 -07001924}
1925
1926static void *rs_alloc(struct ieee80211_local *local)
1927{
1928 return local->hw.priv;
1929}
1930/* rate scale requires free function to be implemented */
1931static void rs_free(void *priv_rate)
1932{
1933 return;
1934}
1935
1936static void rs_clear(void *priv_rate)
1937{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001938 struct iwl4965_priv *priv = (struct iwl4965_priv *) priv_rate;
Zhu Yib481de92007-09-25 17:54:57 -07001939
1940 IWL_DEBUG_RATE("enter\n");
1941
1942 priv->lq_mngr.lq_ready = 0;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001943#ifdef CONFIG_IWL4965_HT
1944#ifdef CONFIG_IWL4965_HT_AGG
Zhu Yib481de92007-09-25 17:54:57 -07001945 if (priv->lq_mngr.agg_ctrl.granted_ba)
1946 iwl4965_turn_off_agg(priv, TID_ALL_SPECIFIED);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001947#endif /*CONFIG_IWL4965_HT_AGG */
1948#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001949
1950 IWL_DEBUG_RATE("leave\n");
1951}
1952
1953static void rs_free_sta(void *priv, void *priv_sta)
1954{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001955 struct iwl4965_rate_scale_priv *rs_priv = priv_sta;
Zhu Yib481de92007-09-25 17:54:57 -07001956
1957 IWL_DEBUG_RATE("enter\n");
1958 kfree(rs_priv);
1959 IWL_DEBUG_RATE("leave\n");
1960}
1961
1962
Zhu Yi93dc6462007-09-27 11:27:37 +08001963#ifdef CONFIG_MAC80211_DEBUGFS
Zhu Yi5ae212c2007-09-27 11:27:38 +08001964static int open_file_generic(struct inode *inode, struct file *file)
1965{
1966 file->private_data = inode->i_private;
1967 return 0;
1968}
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001969static void rs_dbgfs_set_mcs(struct iwl4965_rate_scale_priv *rs_priv,
1970 struct iwl4965_rate *mcs, int index)
Zhu Yi98d7e092007-09-27 11:27:42 +08001971{
Ron Rindjunsky4457e1a2007-10-15 14:40:56 +02001972 u32 base_rate;
1973
1974 if (rs_priv->phymode == (u8) MODE_IEEE80211A)
1975 base_rate = 0x800D;
1976 else
1977 base_rate = 0x820A;
1978
Zhu Yi98d7e092007-09-27 11:27:42 +08001979 if (rs_priv->dbg_fixed.rate_n_flags) {
1980 if (index < 12)
1981 mcs->rate_n_flags = rs_priv->dbg_fixed.rate_n_flags;
1982 else
Ron Rindjunsky4457e1a2007-10-15 14:40:56 +02001983 mcs->rate_n_flags = base_rate;
Zhu Yi98d7e092007-09-27 11:27:42 +08001984 IWL_DEBUG_RATE("Fixed rate ON\n");
1985 return;
1986 }
Zhu Yi5ae212c2007-09-27 11:27:38 +08001987
Zhu Yi98d7e092007-09-27 11:27:42 +08001988 IWL_DEBUG_RATE("Fixed rate OFF\n");
1989}
1990
1991static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
1992 const char __user *user_buf, size_t count, loff_t *ppos)
1993{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001994 struct iwl4965_rate_scale_priv *rs_priv = file->private_data;
Zhu Yi98d7e092007-09-27 11:27:42 +08001995 char buf[64];
1996 int buf_size;
1997 u32 parsed_rate;
1998
1999 memset(buf, 0, sizeof(buf));
2000 buf_size = min(count, sizeof(buf) - 1);
2001 if (copy_from_user(buf, user_buf, buf_size))
2002 return -EFAULT;
2003
2004 if (sscanf(buf, "%x", &parsed_rate) == 1)
2005 rs_priv->dbg_fixed.rate_n_flags = parsed_rate;
2006 else
2007 rs_priv->dbg_fixed.rate_n_flags = 0;
2008
2009 rs_priv->active_rate = 0x0FFF;
2010 rs_priv->active_siso_rate = 0x1FD0;
2011 rs_priv->active_mimo_rate = 0x1FD0;
2012
2013 IWL_DEBUG_RATE("sta_id %d rate 0x%X\n",
2014 rs_priv->lq.sta_id, rs_priv->dbg_fixed.rate_n_flags);
2015
2016 if (rs_priv->dbg_fixed.rate_n_flags) {
2017 rs_fill_link_cmd(rs_priv, &rs_priv->dbg_fixed, &rs_priv->lq);
2018 rs_send_lq_cmd(rs_priv->drv, &rs_priv->lq, CMD_ASYNC);
2019 }
2020
2021 return count;
2022}
Zhu Yi0209dc12007-09-27 11:27:43 +08002023
Zhu Yi5ae212c2007-09-27 11:27:38 +08002024static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
2025 char __user *user_buf, size_t count, loff_t *ppos)
2026{
2027 char buff[1024];
2028 int desc = 0;
2029 int i = 0;
2030
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002031 struct iwl4965_rate_scale_priv *rs_priv = file->private_data;
Zhu Yi5ae212c2007-09-27 11:27:38 +08002032
2033 desc += sprintf(buff+desc, "sta_id %d\n", rs_priv->lq.sta_id);
Zhu Yi98d7e092007-09-27 11:27:42 +08002034 desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
Zhu Yi5ae212c2007-09-27 11:27:38 +08002035 rs_priv->total_failed, rs_priv->total_success,
2036 rs_priv->active_rate);
Zhu Yi98d7e092007-09-27 11:27:42 +08002037 desc += sprintf(buff+desc, "fixed rate 0x%X\n",
2038 rs_priv->dbg_fixed.rate_n_flags);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002039 desc += sprintf(buff+desc, "general:"
2040 "flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
2041 rs_priv->lq.general_params.flags,
2042 rs_priv->lq.general_params.mimo_delimiter,
2043 rs_priv->lq.general_params.single_stream_ant_msk,
2044 rs_priv->lq.general_params.dual_stream_ant_msk);
2045
2046 desc += sprintf(buff+desc, "agg:"
2047 "time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
2048 le16_to_cpu(rs_priv->lq.agg_params.agg_time_limit),
2049 rs_priv->lq.agg_params.agg_dis_start_th,
2050 rs_priv->lq.agg_params.agg_frame_cnt_limit);
2051
2052 desc += sprintf(buff+desc,
2053 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
2054 rs_priv->lq.general_params.start_rate_index[0],
2055 rs_priv->lq.general_params.start_rate_index[1],
2056 rs_priv->lq.general_params.start_rate_index[2],
2057 rs_priv->lq.general_params.start_rate_index[3]);
2058
2059
2060 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
2061 desc += sprintf(buff+desc, " rate[%d] 0x%X\n",
2062 i, le32_to_cpu(rs_priv->lq.rs_table[i].rate_n_flags));
2063
2064 return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2065}
2066
2067static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
Zhu Yi98d7e092007-09-27 11:27:42 +08002068 .write = rs_sta_dbgfs_scale_table_write,
Zhu Yi5ae212c2007-09-27 11:27:38 +08002069 .read = rs_sta_dbgfs_scale_table_read,
2070 .open = open_file_generic,
2071};
Zhu Yi0209dc12007-09-27 11:27:43 +08002072static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
2073 char __user *user_buf, size_t count, loff_t *ppos)
2074{
2075 char buff[1024];
2076 int desc = 0;
2077 int i, j;
2078
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002079 struct iwl4965_rate_scale_priv *rs_priv = file->private_data;
Zhu Yi0209dc12007-09-27 11:27:43 +08002080 for (i = 0; i < LQ_SIZE; i++) {
2081 desc += sprintf(buff+desc, "%s type=%d SGI=%d FAT=%d DUP=%d\n"
2082 "rate=0x%X\n",
2083 rs_priv->active_tbl == i?"*":"x",
2084 rs_priv->lq_info[i].lq_type,
2085 rs_priv->lq_info[i].is_SGI,
2086 rs_priv->lq_info[i].is_fat,
2087 rs_priv->lq_info[i].is_dup,
2088 rs_priv->lq_info[i].current_rate.rate_n_flags);
2089 for (j = 0; j < IWL_RATE_COUNT; j++) {
2090 desc += sprintf(buff+desc,
2091 "counter=%d success=%d %%=%d\n",
2092 rs_priv->lq_info[i].win[j].counter,
2093 rs_priv->lq_info[i].win[j].success_counter,
2094 rs_priv->lq_info[i].win[j].success_ratio);
2095 }
2096 }
2097 return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2098}
2099
2100static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
2101 .read = rs_sta_dbgfs_stats_table_read,
2102 .open = open_file_generic,
2103};
Zhu Yi5ae212c2007-09-27 11:27:38 +08002104
Zhu Yi93dc6462007-09-27 11:27:37 +08002105static void rs_add_debugfs(void *priv, void *priv_sta,
2106 struct dentry *dir)
2107{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002108 struct iwl4965_rate_scale_priv *rs_priv = priv_sta;
Zhu Yi5ae212c2007-09-27 11:27:38 +08002109 rs_priv->rs_sta_dbgfs_scale_table_file =
Zhu Yi0209dc12007-09-27 11:27:43 +08002110 debugfs_create_file("rate_scale_table", 0600, dir,
Zhu Yi5ae212c2007-09-27 11:27:38 +08002111 rs_priv, &rs_sta_dbgfs_scale_table_ops);
Zhu Yi0209dc12007-09-27 11:27:43 +08002112 rs_priv->rs_sta_dbgfs_stats_table_file =
2113 debugfs_create_file("rate_stats_table", 0600, dir,
2114 rs_priv, &rs_sta_dbgfs_stats_table_ops);
Zhu Yi93dc6462007-09-27 11:27:37 +08002115}
2116
2117static void rs_remove_debugfs(void *priv, void *priv_sta)
2118{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002119 struct iwl4965_rate_scale_priv *rs_priv = priv_sta;
Zhu Yi5ae212c2007-09-27 11:27:38 +08002120 debugfs_remove(rs_priv->rs_sta_dbgfs_scale_table_file);
Zhu Yi0209dc12007-09-27 11:27:43 +08002121 debugfs_remove(rs_priv->rs_sta_dbgfs_stats_table_file);
Zhu Yi93dc6462007-09-27 11:27:37 +08002122}
2123#endif
2124
Zhu Yib481de92007-09-25 17:54:57 -07002125static struct rate_control_ops rs_ops = {
2126 .module = NULL,
2127 .name = RS_NAME,
2128 .tx_status = rs_tx_status,
2129 .get_rate = rs_get_rate,
2130 .rate_init = rs_rate_init,
2131 .clear = rs_clear,
2132 .alloc = rs_alloc,
2133 .free = rs_free,
2134 .alloc_sta = rs_alloc_sta,
2135 .free_sta = rs_free_sta,
Zhu Yi93dc6462007-09-27 11:27:37 +08002136#ifdef CONFIG_MAC80211_DEBUGFS
2137 .add_sta_debugfs = rs_add_debugfs,
2138 .remove_sta_debugfs = rs_remove_debugfs,
2139#endif
Zhu Yib481de92007-09-25 17:54:57 -07002140};
2141
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002142int iwl4965_fill_rs_info(struct ieee80211_hw *hw, char *buf, u8 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07002143{
2144 struct ieee80211_local *local = hw_to_local(hw);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002145 struct iwl4965_priv *priv = hw->priv;
2146 struct iwl4965_rate_scale_priv *rs_priv;
Zhu Yib481de92007-09-25 17:54:57 -07002147 struct sta_info *sta;
2148 int count = 0, i;
2149 u32 samples = 0, success = 0, good = 0;
2150 unsigned long now = jiffies;
2151 u32 max_time = 0;
2152 u8 lq_type, antenna;
2153
2154 sta = sta_info_get(local, priv->stations[sta_id].sta.sta.addr);
2155 if (!sta || !sta->rate_ctrl_priv) {
2156 if (sta) {
2157 sta_info_put(sta);
2158 IWL_DEBUG_RATE("leave - no private rate data!\n");
2159 } else
2160 IWL_DEBUG_RATE("leave - no station!\n");
2161 return sprintf(buf, "station %d not found\n", sta_id);
2162 }
2163
2164 rs_priv = (void *)sta->rate_ctrl_priv;
2165
2166 lq_type = rs_priv->lq_info[rs_priv->active_tbl].lq_type;
2167 antenna = rs_priv->lq_info[rs_priv->active_tbl].antenna_type;
2168
2169 if (is_legacy(lq_type))
2170 i = IWL_RATE_54M_INDEX;
2171 else
2172 i = IWL_RATE_60M_INDEX;
2173 while (1) {
2174 u64 mask;
2175 int j;
2176 int active = rs_priv->active_tbl;
2177
2178 count +=
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002179 sprintf(&buf[count], " %2dMbs: ", iwl4965_rates[i].ieee / 2);
Zhu Yib481de92007-09-25 17:54:57 -07002180
2181 mask = (1ULL << (IWL_RATE_MAX_WINDOW - 1));
2182 for (j = 0; j < IWL_RATE_MAX_WINDOW; j++, mask >>= 1)
2183 buf[count++] =
2184 (rs_priv->lq_info[active].win[i].data & mask)
2185 ? '1' : '0';
2186
2187 samples += rs_priv->lq_info[active].win[i].counter;
2188 good += rs_priv->lq_info[active].win[i].success_counter;
2189 success += rs_priv->lq_info[active].win[i].success_counter *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002190 iwl4965_rates[i].ieee;
Zhu Yib481de92007-09-25 17:54:57 -07002191
2192 if (rs_priv->lq_info[active].win[i].stamp) {
2193 int delta =
2194 jiffies_to_msecs(now -
2195 rs_priv->lq_info[active].win[i].stamp);
2196
2197 if (delta > max_time)
2198 max_time = delta;
2199
2200 count += sprintf(&buf[count], "%5dms\n", delta);
2201 } else
2202 buf[count++] = '\n';
2203
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002204 j = iwl4965_get_prev_ieee_rate(i);
Zhu Yib481de92007-09-25 17:54:57 -07002205 if (j == i)
2206 break;
2207 i = j;
2208 }
2209
2210 /* Display the average rate of all samples taken.
2211 *
2212 * NOTE: We multiple # of samples by 2 since the IEEE measurement
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002213 * added from iwl4965_rates is actually 2X the rate */
Zhu Yib481de92007-09-25 17:54:57 -07002214 if (samples)
2215 count += sprintf(&buf[count],
2216 "\nAverage rate is %3d.%02dMbs over last %4dms\n"
2217 "%3d%% success (%d good packets over %d tries)\n",
2218 success / (2 * samples), (success * 5 / samples) % 10,
2219 max_time, good * 100 / samples, good, samples);
2220 else
2221 count += sprintf(&buf[count], "\nAverage rate: 0Mbs\n");
Ian Schram01ebd062007-10-25 17:15:22 +08002222 count += sprintf(&buf[count], "\nrate scale type %d antenna %d "
Zhu Yib481de92007-09-25 17:54:57 -07002223 "active_search %d rate index %d\n", lq_type, antenna,
2224 rs_priv->search_better_tbl, sta->last_txrate);
2225
2226 sta_info_put(sta);
2227 return count;
2228}
2229
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002230void iwl4965_rate_scale_init(struct ieee80211_hw *hw, s32 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07002231{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002232 struct iwl4965_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07002233
2234 priv->lq_mngr.lq_ready = 1;
2235}
2236
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002237void iwl4965_rate_control_register(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07002238{
2239 ieee80211_rate_control_register(&rs_ops);
2240}
2241
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002242void iwl4965_rate_control_unregister(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07002243{
2244 ieee80211_rate_control_unregister(&rs_ops);
2245}
2246