blob: 31a0451f7a4da335bbf10386016c8dff7b31989e [file] [log] [blame]
Zhu Yib481de92007-09-25 17:54:57 -07001/******************************************************************************
2 *
Reinette Chatreeb7ae892008-03-11 16:17:17 -07003 * Copyright(c) 2005 - 2008 Intel Corporation. All rights reserved.
Zhu Yib481de92007-09-25 17:54:57 -07004 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
17 *
18 * The full GNU General Public License is included in this distribution in the
19 * file called LICENSE.
20 *
21 * Contact Information:
22 * James P. Ketrenos <ipw2100-admin@linux.intel.com>
23 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24 *
25 *****************************************************************************/
26#include <linux/kernel.h>
27#include <linux/init.h>
28#include <linux/skbuff.h>
29#include <linux/wireless.h>
30#include <net/mac80211.h>
Zhu Yib481de92007-09-25 17:54:57 -070031
32#include <linux/netdevice.h>
33#include <linux/etherdevice.h>
34#include <linux/delay.h>
35
36#include <linux/workqueue.h>
37
Johannes Berg2c8dccc2008-04-08 15:14:40 -040038#include "../net/mac80211/rate.h"
Zhu Yib481de92007-09-25 17:54:57 -070039
Christoph Hellwig5d08cd12007-10-25 17:15:50 +080040#include "iwl-4965.h"
Tomas Winkler857485c2008-03-21 13:53:44 -070041#include "iwl-core.h"
Zhu Yib481de92007-09-25 17:54:57 -070042#include "iwl-helpers.h"
43
44#define RS_NAME "iwl-4965-rs"
45
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 *
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +020086 * There are two of these in struct iwl4965_lq_sta,
Ben Cahill77626352007-11-29 11:09:44 +080087 * one for "active", and one for "search".
88 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080089struct iwl4965_scale_tbl_info {
Guy Cohenfde0db32008-04-21 15:42:01 -070090 enum iwl_table_type lq_type;
91 u8 ant_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
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200101#ifdef CONFIG_IWL4965_HT
102
103struct iwl4965_traffic_load {
104 unsigned long time_stamp; /* age of the oldest statistics */
105 u32 packet_count[TID_QUEUE_MAX_SIZE]; /* packet count in this time
106 * slice */
107 u32 total; /* total num of packets during the
108 * last TID_MAX_TIME_DIFF */
109 u8 queue_count; /* number of queues that has
110 * been used since the last cleanup */
111 u8 head; /* start of the circular buffer */
112};
113
114#endif /* CONFIG_IWL4965_HT */
115
Ben Cahill77626352007-11-29 11:09:44 +0800116/**
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200117 * struct iwl4965_lq_sta -- driver's rate scaling private structure
Ben Cahill77626352007-11-29 11:09:44 +0800118 *
119 * Pointer to this gets passed back and forth between driver and mac80211.
120 */
Tomas Winklerc33104f2008-01-14 17:46:21 -0800121struct iwl4965_lq_sta {
Ben Cahill77626352007-11-29 11:09:44 +0800122 u8 active_tbl; /* index of active table, range 0-1 */
123 u8 enable_counter; /* indicates HT mode */
124 u8 stay_in_tbl; /* 1: disallow, 0: allow search for new mode */
125 u8 search_better_tbl; /* 1: currently trying alternate mode */
Zhu Yib481de92007-09-25 17:54:57 -0700126 s32 last_tpt;
Ben Cahill77626352007-11-29 11:09:44 +0800127
128 /* The following determine when to search for a new mode */
Zhu Yib481de92007-09-25 17:54:57 -0700129 u32 table_count_limit;
Ben Cahill77626352007-11-29 11:09:44 +0800130 u32 max_failure_limit; /* # failed frames before new search */
131 u32 max_success_limit; /* # successful frames before new search */
Zhu Yib481de92007-09-25 17:54:57 -0700132 u32 table_count;
Ben Cahill77626352007-11-29 11:09:44 +0800133 u32 total_failed; /* total failed frames, any/all rates */
134 u32 total_success; /* total successful frames, any/all rates */
135 u32 flush_timer; /* time staying in mode before new search */
136
137 u8 action_counter; /* # mode-switch actions tried */
Zhu Yib481de92007-09-25 17:54:57 -0700138 u8 antenna;
139 u8 valid_antenna;
140 u8 is_green;
141 u8 is_dup;
Johannes Berg8318d782008-01-24 19:38:38 +0100142 enum ieee80211_band band;
Zhu Yib481de92007-09-25 17:54:57 -0700143 u8 ibss_sta_added;
Ben Cahill77626352007-11-29 11:09:44 +0800144
145 /* The following are bitmaps of rates; IWL_RATE_6M_MASK, etc. */
Zhu Yi02dede02007-09-27 11:27:40 +0800146 u32 supp_rates;
Zhu Yib481de92007-09-25 17:54:57 -0700147 u16 active_rate;
148 u16 active_siso_rate;
Guy Cohenfde0db32008-04-21 15:42:01 -0700149 u16 active_mimo2_rate;
150 u16 active_mimo3_rate;
Zhu Yib481de92007-09-25 17:54:57 -0700151 u16 active_rate_basic;
Ben Cahill77626352007-11-29 11:09:44 +0800152
Tomas Winkler66c73db2008-04-15 16:01:40 -0700153 struct iwl_link_quality_cmd lq;
Ben Cahill77626352007-11-29 11:09:44 +0800154 struct iwl4965_scale_tbl_info lq_info[LQ_SIZE]; /* "active", "search" */
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200155#ifdef CONFIG_IWL4965_HT
156 struct iwl4965_traffic_load load[TID_MAX_LOAD_COUNT];
157 u8 tx_agg_tid_en;
158#endif
Zhu Yi5ae212c2007-09-27 11:27:38 +0800159#ifdef CONFIG_MAC80211_DEBUGFS
Zhu Yi98d7e092007-09-27 11:27:42 +0800160 struct dentry *rs_sta_dbgfs_scale_table_file;
Zhu Yi0209dc12007-09-27 11:27:43 +0800161 struct dentry *rs_sta_dbgfs_stats_table_file;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200162#ifdef CONFIG_IWL4965_HT
163 struct dentry *rs_sta_dbgfs_tx_agg_tid_en_file;
164#endif
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800165 struct iwl4965_rate dbg_fixed;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700166 struct iwl_priv *drv;
Zhu Yi5ae212c2007-09-27 11:27:38 +0800167#endif
Zhu Yib481de92007-09-25 17:54:57 -0700168};
169
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700170static void rs_rate_scale_perform(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700171 struct net_device *dev,
172 struct ieee80211_hdr *hdr,
173 struct sta_info *sta);
Guy Cohenfde0db32008-04-21 15:42:01 -0700174static void rs_fill_link_cmd(const struct iwl_priv *priv,
175 struct iwl4965_lq_sta *lq_sta,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800176 struct iwl4965_rate *tx_mcs,
Tomas Winkler66c73db2008-04-15 16:01:40 -0700177 struct iwl_link_quality_cmd *tbl);
Zhu Yib481de92007-09-25 17:54:57 -0700178
179
Zhu Yi98d7e092007-09-27 11:27:42 +0800180#ifdef CONFIG_MAC80211_DEBUGFS
Tomas Winklerc33104f2008-01-14 17:46:21 -0800181static void rs_dbgfs_set_mcs(struct iwl4965_lq_sta *lq_sta,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800182 struct iwl4965_rate *mcs, int index);
Zhu Yi98d7e092007-09-27 11:27:42 +0800183#else
Tomas Winklerc33104f2008-01-14 17:46:21 -0800184static void rs_dbgfs_set_mcs(struct iwl4965_lq_sta *lq_sta,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800185 struct iwl4965_rate *mcs, int index)
Zhu Yi98d7e092007-09-27 11:27:42 +0800186{}
187#endif
Ben Cahill77626352007-11-29 11:09:44 +0800188
189/*
190 * Expected throughput metrics for following rates:
191 * 1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
192 * "G" is the only table that supports CCK (the first 4 rates).
193 */
Guy Cohenfde0db32008-04-21 15:42:01 -0700194/*FIXME:RS:need to spearate tables for MIMO2/MIMO3*/
Zhu Yib481de92007-09-25 17:54:57 -0700195static s32 expected_tpt_A[IWL_RATE_COUNT] = {
196 0, 0, 0, 0, 40, 57, 72, 98, 121, 154, 177, 186, 186
197};
198
199static s32 expected_tpt_G[IWL_RATE_COUNT] = {
200 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 186
201};
202
203static s32 expected_tpt_siso20MHz[IWL_RATE_COUNT] = {
204 0, 0, 0, 0, 42, 42, 76, 102, 124, 159, 183, 193, 202
205};
206
207static s32 expected_tpt_siso20MHzSGI[IWL_RATE_COUNT] = {
208 0, 0, 0, 0, 46, 46, 82, 110, 132, 168, 192, 202, 211
209};
210
211static s32 expected_tpt_mimo20MHz[IWL_RATE_COUNT] = {
212 0, 0, 0, 0, 74, 74, 123, 155, 179, 214, 236, 244, 251
213};
214
215static s32 expected_tpt_mimo20MHzSGI[IWL_RATE_COUNT] = {
216 0, 0, 0, 0, 81, 81, 131, 164, 188, 222, 243, 251, 257
217};
218
219static s32 expected_tpt_siso40MHz[IWL_RATE_COUNT] = {
220 0, 0, 0, 0, 77, 77, 127, 160, 184, 220, 242, 250, 257
221};
222
223static s32 expected_tpt_siso40MHzSGI[IWL_RATE_COUNT] = {
224 0, 0, 0, 0, 83, 83, 135, 169, 193, 229, 250, 257, 264
225};
226
227static s32 expected_tpt_mimo40MHz[IWL_RATE_COUNT] = {
228 0, 0, 0, 0, 123, 123, 182, 214, 235, 264, 279, 285, 289
229};
230
231static s32 expected_tpt_mimo40MHzSGI[IWL_RATE_COUNT] = {
232 0, 0, 0, 0, 131, 131, 191, 222, 242, 270, 284, 289, 293
233};
234
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800235static inline u8 iwl4965_rate_get_rate(u32 rate_n_flags)
Zhu Yib481de92007-09-25 17:54:57 -0700236{
237 return (u8)(rate_n_flags & 0xFF);
238}
239
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800240static void rs_rate_scale_clear_window(struct iwl4965_rate_scale_data *window)
Zhu Yib481de92007-09-25 17:54:57 -0700241{
242 window->data = 0;
243 window->success_counter = 0;
244 window->success_ratio = IWL_INVALID_VALUE;
245 window->counter = 0;
246 window->average_tpt = IWL_INVALID_VALUE;
247 window->stamp = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700248}
249
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200250#ifdef CONFIG_IWL4965_HT
251/*
252 * removes the old data from the statistics. All data that is older than
253 * TID_MAX_TIME_DIFF, will be deleted.
254 */
255static void rs_tl_rm_old_stats(struct iwl4965_traffic_load *tl, u32 curr_time)
256{
257 /* The oldest age we want to keep */
258 u32 oldest_time = curr_time - TID_MAX_TIME_DIFF;
259
260 while (tl->queue_count &&
261 (tl->time_stamp < oldest_time)) {
262 tl->total -= tl->packet_count[tl->head];
263 tl->packet_count[tl->head] = 0;
264 tl->time_stamp += TID_QUEUE_CELL_SPACING;
265 tl->queue_count--;
266 tl->head++;
267 if (tl->head >= TID_QUEUE_MAX_SIZE)
268 tl->head = 0;
269 }
270}
271
272/*
273 * increment traffic load value for tid and also remove
274 * any old values if passed the certain time period
275 */
276static void rs_tl_add_packet(struct iwl4965_lq_sta *lq_data, u8 tid)
277{
278 u32 curr_time = jiffies_to_msecs(jiffies);
279 u32 time_diff;
280 s32 index;
281 struct iwl4965_traffic_load *tl = NULL;
282
283 if (tid >= TID_MAX_LOAD_COUNT)
284 return;
285
286 tl = &lq_data->load[tid];
287
288 curr_time -= curr_time % TID_ROUND_VALUE;
289
290 /* Happens only for the first packet. Initialize the data */
291 if (!(tl->queue_count)) {
292 tl->total = 1;
293 tl->time_stamp = curr_time;
294 tl->queue_count = 1;
295 tl->head = 0;
296 tl->packet_count[0] = 1;
297 return;
298 }
299
300 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
301 index = time_diff / TID_QUEUE_CELL_SPACING;
302
303 /* The history is too long: remove data that is older than */
304 /* TID_MAX_TIME_DIFF */
305 if (index >= TID_QUEUE_MAX_SIZE)
306 rs_tl_rm_old_stats(tl, curr_time);
307
308 index = (tl->head + index) % TID_QUEUE_MAX_SIZE;
309 tl->packet_count[index] = tl->packet_count[index] + 1;
310 tl->total = tl->total + 1;
311
312 if ((index + 1) > tl->queue_count)
313 tl->queue_count = index + 1;
314}
315
316/*
317 get the traffic load value for tid
318*/
319static u32 rs_tl_get_load(struct iwl4965_lq_sta *lq_data, u8 tid)
320{
321 u32 curr_time = jiffies_to_msecs(jiffies);
322 u32 time_diff;
323 s32 index;
324 struct iwl4965_traffic_load *tl = NULL;
325
326 if (tid >= TID_MAX_LOAD_COUNT)
327 return 0;
328
329 tl = &(lq_data->load[tid]);
330
331 curr_time -= curr_time % TID_ROUND_VALUE;
332
333 if (!(tl->queue_count))
334 return 0;
335
336 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
337 index = time_diff / TID_QUEUE_CELL_SPACING;
338
339 /* The history is too long: remove data that is older than */
340 /* TID_MAX_TIME_DIFF */
341 if (index >= TID_QUEUE_MAX_SIZE)
342 rs_tl_rm_old_stats(tl, curr_time);
343
344 return tl->total;
345}
346
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700347static void rs_tl_turn_on_agg_for_tid(struct iwl_priv *priv,
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200348 struct iwl4965_lq_sta *lq_data, u8 tid,
349 struct sta_info *sta)
350{
351 unsigned long state;
352 DECLARE_MAC_BUF(mac);
353
354 spin_lock_bh(&sta->ampdu_mlme.ampdu_tx);
Ron Rindjunskycee24a32008-03-26 20:36:03 +0200355 state = sta->ampdu_mlme.tid_state_tx[tid];
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200356 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
357
358 if (state == HT_AGG_STATE_IDLE &&
359 rs_tl_get_load(lq_data, tid) > IWL_AGG_LOAD_THRESHOLD) {
360 IWL_DEBUG_HT("Starting Tx agg: STA: %s tid: %d\n",
361 print_mac(mac, sta->addr), tid);
362 ieee80211_start_tx_ba_session(priv->hw, sta->addr, tid);
363 }
364}
365
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700366static void rs_tl_turn_on_agg(struct iwl_priv *priv, u8 tid,
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200367 struct iwl4965_lq_sta *lq_data,
368 struct sta_info *sta)
369{
370 if ((tid < TID_MAX_LOAD_COUNT))
371 rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
372 else if (tid == IWL_AGG_ALL_TID)
373 for (tid = 0; tid < TID_MAX_LOAD_COUNT; tid++)
374 rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
375}
376
377#endif /* CONFIG_IWLWIFI_HT */
378
Guy Cohenfde0db32008-04-21 15:42:01 -0700379static inline int get_num_of_ant_from_mcs(u32 mcs)
380{
381 return (!!(mcs & RATE_MCS_ANT_A_MSK) +
382 !!(mcs & RATE_MCS_ANT_B_MSK) +
383 !!(mcs & RATE_MCS_ANT_C_MSK));
384}
385
Ben Cahill77626352007-11-29 11:09:44 +0800386/**
387 * rs_collect_tx_data - Update the success/failure sliding window
388 *
389 * We keep a sliding window of the last 62 packets transmitted
390 * at this rate. window->data contains the bitmask of successful
391 * packets.
392 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800393static int rs_collect_tx_data(struct iwl4965_rate_scale_data *windows,
Ron Rindjunsky99556432008-01-28 14:07:25 +0200394 int scale_index, s32 tpt, int retries,
395 int successes)
Zhu Yib481de92007-09-25 17:54:57 -0700396{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800397 struct iwl4965_rate_scale_data *window = NULL;
Zhu Yib481de92007-09-25 17:54:57 -0700398 u64 mask;
399 u8 win_size = IWL_RATE_MAX_WINDOW;
400 s32 fail_count;
401
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800402 if (scale_index < 0 || scale_index >= IWL_RATE_COUNT)
403 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -0700404
Ben Cahill77626352007-11-29 11:09:44 +0800405 /* Select data for current tx bit rate */
Zhu Yib481de92007-09-25 17:54:57 -0700406 window = &(windows[scale_index]);
407
Ben Cahill77626352007-11-29 11:09:44 +0800408 /*
409 * Keep track of only the latest 62 tx frame attempts in this rate's
410 * history window; anything older isn't really relevant any more.
411 * If we have filled up the sliding window, drop the oldest attempt;
412 * if the oldest attempt (highest bit in bitmap) shows "success",
413 * subtract "1" from the success counter (this is the main reason
414 * we keep these bitmaps!).
415 */
Ron Rindjunsky99556432008-01-28 14:07:25 +0200416 while (retries > 0) {
417 if (window->counter >= win_size) {
418 window->counter = win_size - 1;
419 mask = 1;
420 mask = (mask << (win_size - 1));
421 if (window->data & mask) {
422 window->data &= ~mask;
423 window->success_counter =
424 window->success_counter - 1;
425 }
Zhu Yib481de92007-09-25 17:54:57 -0700426 }
Zhu Yib481de92007-09-25 17:54:57 -0700427
Ron Rindjunsky99556432008-01-28 14:07:25 +0200428 /* Increment frames-attempted counter */
429 window->counter++;
Ben Cahill77626352007-11-29 11:09:44 +0800430
Ron Rindjunsky99556432008-01-28 14:07:25 +0200431 /* Shift bitmap by one frame (throw away oldest history),
432 * OR in "1", and increment "success" if this
433 * frame was successful. */
434 mask = window->data;
435 window->data = (mask << 1);
436 if (successes > 0) {
437 window->success_counter = window->success_counter + 1;
438 window->data |= 0x1;
439 successes--;
440 }
441
442 retries--;
Zhu Yib481de92007-09-25 17:54:57 -0700443 }
444
Ben Cahill77626352007-11-29 11:09:44 +0800445 /* Calculate current success ratio, avoid divide-by-0! */
Zhu Yib481de92007-09-25 17:54:57 -0700446 if (window->counter > 0)
447 window->success_ratio = 128 * (100 * window->success_counter)
448 / window->counter;
449 else
450 window->success_ratio = IWL_INVALID_VALUE;
451
452 fail_count = window->counter - window->success_counter;
453
Ben Cahill77626352007-11-29 11:09:44 +0800454 /* Calculate average throughput, if we have enough history. */
Zhu Yib481de92007-09-25 17:54:57 -0700455 if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
456 (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
457 window->average_tpt = (window->success_ratio * tpt + 64) / 128;
458 else
459 window->average_tpt = IWL_INVALID_VALUE;
460
Ben Cahill77626352007-11-29 11:09:44 +0800461 /* Tag this window as having been updated */
Zhu Yib481de92007-09-25 17:54:57 -0700462 window->stamp = jiffies;
463
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800464 return 0;
Zhu Yib481de92007-09-25 17:54:57 -0700465}
466
Ben Cahill77626352007-11-29 11:09:44 +0800467/*
468 * Fill uCode API rate_n_flags field, based on "search" or "active" table.
469 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800470static void rs_mcs_from_tbl(struct iwl4965_rate *mcs_rate,
471 struct iwl4965_scale_tbl_info *tbl,
Zhu Yib481de92007-09-25 17:54:57 -0700472 int index, u8 use_green)
473{
Zhu Yib481de92007-09-25 17:54:57 -0700474 if (is_legacy(tbl->lq_type)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800475 mcs_rate->rate_n_flags = iwl4965_rates[index].plcp;
Zhu Yib481de92007-09-25 17:54:57 -0700476 if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
477 mcs_rate->rate_n_flags |= RATE_MCS_CCK_MSK;
478
Guy Cohenfde0db32008-04-21 15:42:01 -0700479 } else if (is_Ht(tbl->lq_type)) {
480 if (index > IWL_LAST_OFDM_RATE) {
481 IWL_ERROR("invalid HT rate index %d\n", index);
Zhu Yib481de92007-09-25 17:54:57 -0700482 index = IWL_LAST_OFDM_RATE;
Guy Cohenfde0db32008-04-21 15:42:01 -0700483 }
484 mcs_rate->rate_n_flags = RATE_MCS_HT_MSK;
485
486 if (is_siso(tbl->lq_type))
487 mcs_rate->rate_n_flags |=
488 iwl4965_rates[index].plcp_siso;
489 else if (is_mimo2(tbl->lq_type))
490 mcs_rate->rate_n_flags |=
491 iwl4965_rates[index].plcp_mimo2;
492 else
493 mcs_rate->rate_n_flags |=
494 iwl4965_rates[index].plcp_mimo3;
Zhu Yib481de92007-09-25 17:54:57 -0700495 } else {
Guy Cohenfde0db32008-04-21 15:42:01 -0700496 IWL_ERROR("Invalid tbl->lq_type %d\n", tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -0700497 }
498
Guy Cohenfde0db32008-04-21 15:42:01 -0700499 mcs_rate->rate_n_flags |= ((tbl->ant_type << RATE_MCS_ANT_POS) &
500 RATE_MCS_ANT_ABC_MSK);
Zhu Yib481de92007-09-25 17:54:57 -0700501
502 if (is_legacy(tbl->lq_type))
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800503 return;
Zhu Yib481de92007-09-25 17:54:57 -0700504
505 if (tbl->is_fat) {
506 if (tbl->is_dup)
507 mcs_rate->rate_n_flags |= RATE_MCS_DUP_MSK;
508 else
509 mcs_rate->rate_n_flags |= RATE_MCS_FAT_MSK;
510 }
511 if (tbl->is_SGI)
512 mcs_rate->rate_n_flags |= RATE_MCS_SGI_MSK;
513
514 if (use_green) {
515 mcs_rate->rate_n_flags |= RATE_MCS_GF_MSK;
516 if (is_siso(tbl->lq_type))
517 mcs_rate->rate_n_flags &= ~RATE_MCS_SGI_MSK;
518 }
Zhu Yib481de92007-09-25 17:54:57 -0700519}
520
Ben Cahill77626352007-11-29 11:09:44 +0800521/*
522 * Interpret uCode API's rate_n_flags format,
523 * fill "search" or "active" tx mode table.
524 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800525static int rs_get_tbl_info_from_mcs(const struct iwl4965_rate *mcs_rate,
Johannes Berg8318d782008-01-24 19:38:38 +0100526 enum ieee80211_band band,
527 struct iwl4965_scale_tbl_info *tbl,
Zhu Yib481de92007-09-25 17:54:57 -0700528 int *rate_idx)
529{
Guy Cohenfde0db32008-04-21 15:42:01 -0700530 u32 ant_msk = (mcs_rate->rate_n_flags & RATE_MCS_ANT_ABC_MSK);
531 u8 num_of_ant = get_num_of_ant_from_mcs(ant_msk);
Zhu Yib481de92007-09-25 17:54:57 -0700532
Guy Cohenfde0db32008-04-21 15:42:01 -0700533 *rate_idx = iwl4965_hwrate_to_plcp_idx(mcs_rate->rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -0700534
Guy Cohenfde0db32008-04-21 15:42:01 -0700535 if (*rate_idx == IWL_RATE_INVALID) {
Zhu Yib481de92007-09-25 17:54:57 -0700536 *rate_idx = -1;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800537 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -0700538 }
Ben Cahill77626352007-11-29 11:09:44 +0800539 tbl->is_SGI = 0; /* default legacy setup */
Zhu Yib481de92007-09-25 17:54:57 -0700540 tbl->is_fat = 0;
541 tbl->is_dup = 0;
Guy Cohenfde0db32008-04-21 15:42:01 -0700542 tbl->ant_type = (ant_msk >> RATE_MCS_ANT_POS);
543 tbl->lq_type = LQ_NONE;
Zhu Yib481de92007-09-25 17:54:57 -0700544
Ben Cahill77626352007-11-29 11:09:44 +0800545 /* legacy rate format */
Zhu Yib481de92007-09-25 17:54:57 -0700546 if (!(mcs_rate->rate_n_flags & RATE_MCS_HT_MSK)) {
Guy Cohenfde0db32008-04-21 15:42:01 -0700547 if (num_of_ant == 1) {
Johannes Berg8318d782008-01-24 19:38:38 +0100548 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700549 tbl->lq_type = LQ_A;
550 else
551 tbl->lq_type = LQ_G;
Zhu Yib481de92007-09-25 17:54:57 -0700552 }
Guy Cohenfde0db32008-04-21 15:42:01 -0700553 /* HT rate format */
Zhu Yib481de92007-09-25 17:54:57 -0700554 } else {
Zhu Yib481de92007-09-25 17:54:57 -0700555 if (mcs_rate->rate_n_flags & RATE_MCS_SGI_MSK)
556 tbl->is_SGI = 1;
557
558 if ((mcs_rate->rate_n_flags & RATE_MCS_FAT_MSK) ||
559 (mcs_rate->rate_n_flags & RATE_MCS_DUP_MSK))
560 tbl->is_fat = 1;
561
562 if (mcs_rate->rate_n_flags & RATE_MCS_DUP_MSK)
563 tbl->is_dup = 1;
Guy Cohenfde0db32008-04-21 15:42:01 -0700564
565 /* SISO */
566 if (iwl4965_rate_get_rate(mcs_rate->rate_n_flags)
567 <= IWL_RATE_SISO_60M_PLCP) {
568
569 if (num_of_ant == 1)
570 tbl->lq_type = LQ_SISO; /*else NONE*/
571 /* MIMO2 */
572 } else if (iwl4965_rate_get_rate(mcs_rate->rate_n_flags)
573 <= IWL_RATE_MIMO2_60M_PLCP) {
574 if (num_of_ant == 2)
575 tbl->lq_type = LQ_MIMO2;
576 /* MIMO3 */
577 } else {
578 if (num_of_ant == 3)
579 tbl->lq_type = LQ_MIMO3;
580 }
Zhu Yib481de92007-09-25 17:54:57 -0700581 }
582 return 0;
583}
Guy Cohenfde0db32008-04-21 15:42:01 -0700584/* FIXME:RS: need to toggle also ANT_C, and also AB,AC,BC */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800585static inline void rs_toggle_antenna(struct iwl4965_rate *new_rate,
586 struct iwl4965_scale_tbl_info *tbl)
Zhu Yib481de92007-09-25 17:54:57 -0700587{
Guy Cohenfde0db32008-04-21 15:42:01 -0700588 tbl->ant_type ^= ANT_AB;
589 new_rate->rate_n_flags ^= (RATE_MCS_ANT_A_MSK|RATE_MCS_ANT_B_MSK);
Zhu Yib481de92007-09-25 17:54:57 -0700590}
591
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700592static inline u8 rs_use_green(struct iwl_priv *priv,
Ron Rindjunsky270243a2007-11-26 16:14:41 +0200593 struct ieee80211_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -0700594{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800595#ifdef CONFIG_IWL4965_HT
Ron Rindjunsky270243a2007-11-26 16:14:41 +0200596 return ((conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) &&
597 priv->current_ht_config.is_green_field &&
598 !priv->current_ht_config.non_GF_STA_present);
599#endif /* CONFIG_IWL4965_HT */
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800600 return 0;
Zhu Yib481de92007-09-25 17:54:57 -0700601}
602
603/**
604 * rs_get_supported_rates - get the available rates
605 *
606 * if management frame or broadcast frame only return
607 * basic available rates.
608 *
609 */
Tomas Winklerc33104f2008-01-14 17:46:21 -0800610static void rs_get_supported_rates(struct iwl4965_lq_sta *lq_sta,
Zhu Yib481de92007-09-25 17:54:57 -0700611 struct ieee80211_hdr *hdr,
Guy Cohenfde0db32008-04-21 15:42:01 -0700612 enum iwl_table_type rate_type,
Zhu Yib481de92007-09-25 17:54:57 -0700613 u16 *data_rate)
614{
615 if (is_legacy(rate_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -0800616 *data_rate = lq_sta->active_rate;
Zhu Yib481de92007-09-25 17:54:57 -0700617 else {
618 if (is_siso(rate_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -0800619 *data_rate = lq_sta->active_siso_rate;
Guy Cohenfde0db32008-04-21 15:42:01 -0700620 else if (is_mimo2(rate_type))
621 *data_rate = lq_sta->active_mimo2_rate;
Zhu Yib481de92007-09-25 17:54:57 -0700622 else
Guy Cohenfde0db32008-04-21 15:42:01 -0700623 *data_rate = lq_sta->active_mimo3_rate;
Zhu Yib481de92007-09-25 17:54:57 -0700624 }
625
626 if (hdr && is_multicast_ether_addr(hdr->addr1) &&
Tomas Winklerc33104f2008-01-14 17:46:21 -0800627 lq_sta->active_rate_basic) {
628 *data_rate = lq_sta->active_rate_basic;
629 }
Zhu Yib481de92007-09-25 17:54:57 -0700630}
631
632static u16 rs_get_adjacent_rate(u8 index, u16 rate_mask, int rate_type)
633{
634 u8 high = IWL_RATE_INVALID;
635 u8 low = IWL_RATE_INVALID;
636
Ian Schram01ebd062007-10-25 17:15:22 +0800637 /* 802.11A or ht walks to the next literal adjacent rate in
Zhu Yib481de92007-09-25 17:54:57 -0700638 * the rate table */
639 if (is_a_band(rate_type) || !is_legacy(rate_type)) {
640 int i;
641 u32 mask;
642
643 /* Find the previous rate that is in the rate mask */
644 i = index - 1;
645 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
646 if (rate_mask & mask) {
647 low = i;
648 break;
649 }
650 }
651
652 /* Find the next rate that is in the rate mask */
653 i = index + 1;
654 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
655 if (rate_mask & mask) {
656 high = i;
657 break;
658 }
659 }
660
661 return (high << 8) | low;
662 }
663
664 low = index;
665 while (low != IWL_RATE_INVALID) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800666 low = iwl4965_rates[low].prev_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700667 if (low == IWL_RATE_INVALID)
668 break;
669 if (rate_mask & (1 << low))
670 break;
671 IWL_DEBUG_RATE("Skipping masked lower rate: %d\n", low);
672 }
673
674 high = index;
675 while (high != IWL_RATE_INVALID) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800676 high = iwl4965_rates[high].next_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700677 if (high == IWL_RATE_INVALID)
678 break;
679 if (rate_mask & (1 << high))
680 break;
681 IWL_DEBUG_RATE("Skipping masked higher rate: %d\n", high);
682 }
683
684 return (high << 8) | low;
685}
686
Tomas Winklerc33104f2008-01-14 17:46:21 -0800687static void rs_get_lower_rate(struct iwl4965_lq_sta *lq_sta,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800688 struct iwl4965_scale_tbl_info *tbl, u8 scale_index,
689 u8 ht_possible, struct iwl4965_rate *mcs_rate)
Zhu Yib481de92007-09-25 17:54:57 -0700690{
Zhu Yib481de92007-09-25 17:54:57 -0700691 s32 low;
692 u16 rate_mask;
693 u16 high_low;
694 u8 switch_to_legacy = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -0800695 u8 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -0700696
697 /* check if we need to switch from HT to legacy rates.
698 * assumption is that mandatory rates (1Mbps or 6Mbps)
699 * are always supported (spec demand) */
700 if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_index)) {
701 switch_to_legacy = 1;
702 scale_index = rs_ht_to_legacy[scale_index];
Johannes Berg8318d782008-01-24 19:38:38 +0100703 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700704 tbl->lq_type = LQ_A;
705 else
706 tbl->lq_type = LQ_G;
707
Guy Cohenfde0db32008-04-21 15:42:01 -0700708 if (num_of_ant(tbl->ant_type > 1))
709 tbl->ant_type = ANT_A;/*FIXME:RS*/
Zhu Yib481de92007-09-25 17:54:57 -0700710
711 tbl->is_fat = 0;
712 tbl->is_SGI = 0;
713 }
714
Tomas Winklerc33104f2008-01-14 17:46:21 -0800715 rs_get_supported_rates(lq_sta, NULL, tbl->lq_type, &rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -0700716
Ben Cahill77626352007-11-29 11:09:44 +0800717 /* Mask with station rate restriction */
Zhu Yib481de92007-09-25 17:54:57 -0700718 if (is_legacy(tbl->lq_type)) {
Ben Cahill77626352007-11-29 11:09:44 +0800719 /* supp_rates has no CCK bits in A mode */
Johannes Berg8318d782008-01-24 19:38:38 +0100720 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700721 rate_mask = (u16)(rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -0800722 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
Zhu Yib481de92007-09-25 17:54:57 -0700723 else
Tomas Winklerc33104f2008-01-14 17:46:21 -0800724 rate_mask = (u16)(rate_mask & lq_sta->supp_rates);
Zhu Yib481de92007-09-25 17:54:57 -0700725 }
726
Ben Cahill77626352007-11-29 11:09:44 +0800727 /* If we switched from HT to legacy, check current rate */
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800728 if (switch_to_legacy && (rate_mask & (1 << scale_index))) {
Zhu Yib481de92007-09-25 17:54:57 -0700729 rs_mcs_from_tbl(mcs_rate, tbl, scale_index, is_green);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800730 return;
Zhu Yib481de92007-09-25 17:54:57 -0700731 }
732
733 high_low = rs_get_adjacent_rate(scale_index, rate_mask, tbl->lq_type);
734 low = high_low & 0xff;
735
736 if (low != IWL_RATE_INVALID)
737 rs_mcs_from_tbl(mcs_rate, tbl, low, is_green);
738 else
739 rs_mcs_from_tbl(mcs_rate, tbl, scale_index, is_green);
Zhu Yib481de92007-09-25 17:54:57 -0700740}
741
Ben Cahill77626352007-11-29 11:09:44 +0800742/*
743 * mac80211 sends us Tx status
744 */
Tomas Winklerc33104f2008-01-14 17:46:21 -0800745static void rs_tx_status(void *priv_rate, struct net_device *dev,
Zhu Yib481de92007-09-25 17:54:57 -0700746 struct sk_buff *skb,
747 struct ieee80211_tx_status *tx_resp)
748{
749 int status;
750 u8 retries;
751 int rs_index, index = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -0800752 struct iwl4965_lq_sta *lq_sta;
Tomas Winkler66c73db2008-04-15 16:01:40 -0700753 struct iwl_link_quality_cmd *table;
Zhu Yib481de92007-09-25 17:54:57 -0700754 struct sta_info *sta;
755 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700756 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
Zhu Yib481de92007-09-25 17:54:57 -0700757 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800758 struct ieee80211_hw *hw = local_to_hw(local);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800759 struct iwl4965_rate_scale_data *window = NULL;
760 struct iwl4965_rate_scale_data *search_win = NULL;
761 struct iwl4965_rate tx_mcs;
762 struct iwl4965_scale_tbl_info tbl_type;
763 struct iwl4965_scale_tbl_info *curr_tbl, *search_tbl;
Zhu Yib481de92007-09-25 17:54:57 -0700764 u8 active_index = 0;
765 u16 fc = le16_to_cpu(hdr->frame_control);
766 s32 tpt = 0;
767
Zhu Yi58826352007-09-27 11:27:39 +0800768 IWL_DEBUG_RATE_LIMIT("get frame ack response, update rate scale window\n");
Zhu Yib481de92007-09-25 17:54:57 -0700769
770 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1))
771 return;
772
Ron Rindjunsky99556432008-01-28 14:07:25 +0200773 /* This packet was aggregated but doesn't carry rate scale info */
774 if ((tx_resp->control.flags & IEEE80211_TXCTL_AMPDU) &&
775 !(tx_resp->flags & IEEE80211_TX_STATUS_AMPDU))
776 return;
777
Zhu Yib481de92007-09-25 17:54:57 -0700778 retries = tx_resp->retry_count;
779
780 if (retries > 15)
781 retries = 15;
782
Johannes Bergd0709a62008-02-25 16:27:46 +0100783 rcu_read_lock();
Zhu Yib481de92007-09-25 17:54:57 -0700784
785 sta = sta_info_get(local, hdr->addr1);
786
Tomas Winklerf4d60822008-03-05 11:31:00 -0800787 if (!sta || !sta->rate_ctrl_priv)
788 goto out;
789
Zhu Yib481de92007-09-25 17:54:57 -0700790
Tomas Winklerc33104f2008-01-14 17:46:21 -0800791 lq_sta = (struct iwl4965_lq_sta *)sta->rate_ctrl_priv;
Zhu Yib481de92007-09-25 17:54:57 -0700792
793 if (!priv->lq_mngr.lq_ready)
Tomas Winklerf4d60822008-03-05 11:31:00 -0800794 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700795
Tomas Winklerc33104f2008-01-14 17:46:21 -0800796 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
797 !lq_sta->ibss_sta_added)
Tomas Winklerf4d60822008-03-05 11:31:00 -0800798 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700799
Tomas Winklerc33104f2008-01-14 17:46:21 -0800800 table = &lq_sta->lq;
801 active_index = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -0700802
Tomas Winklerc33104f2008-01-14 17:46:21 -0800803 lq_sta->antenna = lq_sta->valid_antenna;
Ben Cahill77626352007-11-29 11:09:44 +0800804
Tomas Winklerc33104f2008-01-14 17:46:21 -0800805 curr_tbl = &(lq_sta->lq_info[active_index]);
806 search_tbl = &(lq_sta->lq_info[(1 - active_index)]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800807 window = (struct iwl4965_rate_scale_data *)
Zhu Yib481de92007-09-25 17:54:57 -0700808 &(curr_tbl->win[0]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800809 search_win = (struct iwl4965_rate_scale_data *)
Zhu Yib481de92007-09-25 17:54:57 -0700810 &(search_tbl->win[0]);
811
Ben Cahill77626352007-11-29 11:09:44 +0800812 /*
813 * Ignore this Tx frame response if its initial rate doesn't match
814 * that of latest Link Quality command. There may be stragglers
815 * from a previous Link Quality command, but we're no longer interested
816 * in those; they're either from the "active" mode while we're trying
817 * to check "search" mode, or a prior "search" mode after we've moved
818 * to a new "search" mode (which might become the new "active" mode).
819 */
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800820 tx_mcs.rate_n_flags = le32_to_cpu(table->rs_table[0].rate_n_flags);
821 rs_get_tbl_info_from_mcs(&tx_mcs, priv->band, &tbl_type, &rs_index);
822 if (priv->band == IEEE80211_BAND_5GHZ)
823 rs_index -= IWL_FIRST_OFDM_RATE;
824
825 if ((tx_resp->control.tx_rate == NULL) ||
826 (tbl_type.is_SGI ^
827 !!(tx_resp->control.flags & IEEE80211_TXCTL_SHORT_GI)) ||
828 (tbl_type.is_fat ^
829 !!(tx_resp->control.flags & IEEE80211_TXCTL_40_MHZ_WIDTH)) ||
830 (tbl_type.is_dup ^
831 !!(tx_resp->control.flags & IEEE80211_TXCTL_DUP_DATA)) ||
Guy Cohenfde0db32008-04-21 15:42:01 -0700832 (tbl_type.ant_type ^ tx_resp->control.antenna_sel_tx) ||
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800833 (!!(tx_mcs.rate_n_flags & RATE_MCS_HT_MSK) ^
834 !!(tx_resp->control.flags & IEEE80211_TXCTL_OFDM_HT)) ||
835 (!!(tx_mcs.rate_n_flags & RATE_MCS_GF_MSK) ^
836 !!(tx_resp->control.flags & IEEE80211_TXCTL_GREEN_FIELD)) ||
837 (hw->wiphy->bands[priv->band]->bitrates[rs_index].bitrate !=
838 tx_resp->control.tx_rate->bitrate)) {
839 IWL_DEBUG_RATE("initial rate does not match 0x%x\n",
840 tx_mcs.rate_n_flags);
Tomas Winklerf4d60822008-03-05 11:31:00 -0800841 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700842 }
843
Ben Cahill77626352007-11-29 11:09:44 +0800844 /* Update frame history window with "failure" for each Tx retry. */
Zhu Yib481de92007-09-25 17:54:57 -0700845 while (retries) {
Ben Cahill77626352007-11-29 11:09:44 +0800846 /* Look up the rate and other info used for each tx attempt.
847 * Each tx attempt steps one entry deeper in the rate table. */
Zhu Yib481de92007-09-25 17:54:57 -0700848 tx_mcs.rate_n_flags =
849 le32_to_cpu(table->rs_table[index].rate_n_flags);
Johannes Berg8318d782008-01-24 19:38:38 +0100850 rs_get_tbl_info_from_mcs(&tx_mcs, priv->band,
Zhu Yib481de92007-09-25 17:54:57 -0700851 &tbl_type, &rs_index);
852
Ben Cahill77626352007-11-29 11:09:44 +0800853 /* If type matches "search" table,
854 * add failure to "search" history */
Zhu Yib481de92007-09-25 17:54:57 -0700855 if ((tbl_type.lq_type == search_tbl->lq_type) &&
Guy Cohenfde0db32008-04-21 15:42:01 -0700856 (tbl_type.ant_type == search_tbl->ant_type) &&
Zhu Yib481de92007-09-25 17:54:57 -0700857 (tbl_type.is_SGI == search_tbl->is_SGI)) {
858 if (search_tbl->expected_tpt)
859 tpt = search_tbl->expected_tpt[rs_index];
860 else
861 tpt = 0;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200862 rs_collect_tx_data(search_win, rs_index, tpt, 1, 0);
Ben Cahill77626352007-11-29 11:09:44 +0800863
864 /* Else if type matches "current/active" table,
865 * add failure to "current/active" history */
Zhu Yib481de92007-09-25 17:54:57 -0700866 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
Guy Cohenfde0db32008-04-21 15:42:01 -0700867 (tbl_type.ant_type == curr_tbl->ant_type) &&
Zhu Yib481de92007-09-25 17:54:57 -0700868 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
869 if (curr_tbl->expected_tpt)
870 tpt = curr_tbl->expected_tpt[rs_index];
871 else
872 tpt = 0;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200873 rs_collect_tx_data(window, rs_index, tpt, 1, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700874 }
Ben Cahill77626352007-11-29 11:09:44 +0800875
876 /* If not searching for a new mode, increment failed counter
877 * ... this helps determine when to start searching again */
Tomas Winklerc33104f2008-01-14 17:46:21 -0800878 if (lq_sta->stay_in_tbl)
879 lq_sta->total_failed++;
Zhu Yib481de92007-09-25 17:54:57 -0700880 --retries;
881 index++;
882
883 }
884
Ben Cahill77626352007-11-29 11:09:44 +0800885 /*
886 * Find (by rate) the history window to update with final Tx attempt;
887 * if Tx was successful first try, use original rate,
888 * else look up the rate that was, finally, successful.
889 */
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800890 tx_mcs.rate_n_flags = le32_to_cpu(table->rs_table[index].rate_n_flags);
891 rs_get_tbl_info_from_mcs(&tx_mcs, priv->band, &tbl_type, &rs_index);
Zhu Yib481de92007-09-25 17:54:57 -0700892
Ben Cahill77626352007-11-29 11:09:44 +0800893 /* Update frame history window with "success" if Tx got ACKed ... */
Zhu Yib481de92007-09-25 17:54:57 -0700894 if (tx_resp->flags & IEEE80211_TX_STATUS_ACK)
895 status = 1;
896 else
897 status = 0;
898
Ben Cahill77626352007-11-29 11:09:44 +0800899 /* If type matches "search" table,
900 * add final tx status to "search" history */
Zhu Yib481de92007-09-25 17:54:57 -0700901 if ((tbl_type.lq_type == search_tbl->lq_type) &&
Guy Cohenfde0db32008-04-21 15:42:01 -0700902 (tbl_type.ant_type == search_tbl->ant_type) &&
Zhu Yib481de92007-09-25 17:54:57 -0700903 (tbl_type.is_SGI == search_tbl->is_SGI)) {
904 if (search_tbl->expected_tpt)
905 tpt = search_tbl->expected_tpt[rs_index];
906 else
907 tpt = 0;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200908 if (tx_resp->control.flags & IEEE80211_TXCTL_AMPDU)
909 rs_collect_tx_data(search_win, rs_index, tpt,
910 tx_resp->ampdu_ack_len,
911 tx_resp->ampdu_ack_map);
912 else
913 rs_collect_tx_data(search_win, rs_index, tpt,
914 1, status);
Ben Cahill77626352007-11-29 11:09:44 +0800915 /* Else if type matches "current/active" table,
916 * add final tx status to "current/active" history */
Zhu Yib481de92007-09-25 17:54:57 -0700917 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
Guy Cohenfde0db32008-04-21 15:42:01 -0700918 (tbl_type.ant_type == curr_tbl->ant_type) &&
Zhu Yib481de92007-09-25 17:54:57 -0700919 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
920 if (curr_tbl->expected_tpt)
921 tpt = curr_tbl->expected_tpt[rs_index];
922 else
923 tpt = 0;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200924 if (tx_resp->control.flags & IEEE80211_TXCTL_AMPDU)
925 rs_collect_tx_data(window, rs_index, tpt,
926 tx_resp->ampdu_ack_len,
927 tx_resp->ampdu_ack_map);
928 else
929 rs_collect_tx_data(window, rs_index, tpt,
930 1, status);
Zhu Yib481de92007-09-25 17:54:57 -0700931 }
932
Ben Cahill77626352007-11-29 11:09:44 +0800933 /* If not searching for new mode, increment success/failed counter
934 * ... these help determine when to start searching again */
Tomas Winklerc33104f2008-01-14 17:46:21 -0800935 if (lq_sta->stay_in_tbl) {
Ron Rindjunsky99556432008-01-28 14:07:25 +0200936 if (tx_resp->control.flags & IEEE80211_TXCTL_AMPDU) {
937 lq_sta->total_success += tx_resp->ampdu_ack_map;
938 lq_sta->total_failed +=
939 (tx_resp->ampdu_ack_len - tx_resp->ampdu_ack_map);
940 } else {
941 if (status)
942 lq_sta->total_success++;
943 else
944 lq_sta->total_failed++;
945 }
Zhu Yib481de92007-09-25 17:54:57 -0700946 }
947
Ben Cahill77626352007-11-29 11:09:44 +0800948 /* See if there's a better rate or modulation mode to try. */
Zhu Yib481de92007-09-25 17:54:57 -0700949 rs_rate_scale_perform(priv, dev, hdr, sta);
Tomas Winklerf4d60822008-03-05 11:31:00 -0800950out:
Johannes Bergd0709a62008-02-25 16:27:46 +0100951 rcu_read_unlock();
Zhu Yib481de92007-09-25 17:54:57 -0700952 return;
953}
954
Guy Cohenfde0db32008-04-21 15:42:01 -0700955static inline u8 rs_is_ant_connected(u8 valid_antenna, u8 ant_type)
Zhu Yib481de92007-09-25 17:54:57 -0700956{
Guy Cohenfde0db32008-04-21 15:42:01 -0700957 return ((ant_type & valid_antenna) == ant_type);
Zhu Yib481de92007-09-25 17:54:57 -0700958}
959
Guy Cohenfde0db32008-04-21 15:42:01 -0700960/*FIXME:RS: this function should be replaced*/
961static u8 rs_is_other_ant_connected(u8 valid_antenna, u8 ant_type)
Zhu Yib481de92007-09-25 17:54:57 -0700962{
Guy Cohenfde0db32008-04-21 15:42:01 -0700963 if (ant_type == ANT_B)
964 return rs_is_ant_connected(valid_antenna, ANT_A);
Zhu Yib481de92007-09-25 17:54:57 -0700965 else
Guy Cohenfde0db32008-04-21 15:42:01 -0700966 return rs_is_ant_connected(valid_antenna, ANT_B);
Zhu Yib481de92007-09-25 17:54:57 -0700967
968 return 0;
969}
970
Ben Cahill77626352007-11-29 11:09:44 +0800971/*
972 * Begin a period of staying with a selected modulation mode.
973 * Set "stay_in_tbl" flag to prevent any mode switches.
974 * Set frame tx success limits according to legacy vs. high-throughput,
975 * and reset overall (spanning all rates) tx success history statistics.
976 * These control how long we stay using same modulation mode before
977 * searching for a new mode.
978 */
Zhu Yib481de92007-09-25 17:54:57 -0700979static void rs_set_stay_in_table(u8 is_legacy,
Tomas Winklerc33104f2008-01-14 17:46:21 -0800980 struct iwl4965_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -0700981{
982 IWL_DEBUG_HT("we are staying in the same table\n");
Tomas Winklerc33104f2008-01-14 17:46:21 -0800983 lq_sta->stay_in_tbl = 1; /* only place this gets set */
Zhu Yib481de92007-09-25 17:54:57 -0700984 if (is_legacy) {
Tomas Winklerc33104f2008-01-14 17:46:21 -0800985 lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT;
986 lq_sta->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
987 lq_sta->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
Zhu Yib481de92007-09-25 17:54:57 -0700988 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -0800989 lq_sta->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
990 lq_sta->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
991 lq_sta->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
Zhu Yib481de92007-09-25 17:54:57 -0700992 }
Tomas Winklerc33104f2008-01-14 17:46:21 -0800993 lq_sta->table_count = 0;
994 lq_sta->total_failed = 0;
995 lq_sta->total_success = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700996}
997
Ben Cahill77626352007-11-29 11:09:44 +0800998/*
999 * Find correct throughput table for given mode of modulation
1000 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001001static void rs_get_expected_tpt_table(struct iwl4965_lq_sta *lq_sta,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001002 struct iwl4965_scale_tbl_info *tbl)
Zhu Yib481de92007-09-25 17:54:57 -07001003{
1004 if (is_legacy(tbl->lq_type)) {
1005 if (!is_a_band(tbl->lq_type))
1006 tbl->expected_tpt = expected_tpt_G;
1007 else
1008 tbl->expected_tpt = expected_tpt_A;
1009 } else if (is_siso(tbl->lq_type)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001010 if (tbl->is_fat && !lq_sta->is_dup)
Zhu Yib481de92007-09-25 17:54:57 -07001011 if (tbl->is_SGI)
1012 tbl->expected_tpt = expected_tpt_siso40MHzSGI;
1013 else
1014 tbl->expected_tpt = expected_tpt_siso40MHz;
1015 else if (tbl->is_SGI)
1016 tbl->expected_tpt = expected_tpt_siso20MHzSGI;
1017 else
1018 tbl->expected_tpt = expected_tpt_siso20MHz;
1019
Guy Cohenfde0db32008-04-21 15:42:01 -07001020 } else if (is_mimo(tbl->lq_type)) { /* FIXME:need to separate mimo2/3 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001021 if (tbl->is_fat && !lq_sta->is_dup)
Zhu Yib481de92007-09-25 17:54:57 -07001022 if (tbl->is_SGI)
1023 tbl->expected_tpt = expected_tpt_mimo40MHzSGI;
1024 else
1025 tbl->expected_tpt = expected_tpt_mimo40MHz;
1026 else if (tbl->is_SGI)
1027 tbl->expected_tpt = expected_tpt_mimo20MHzSGI;
1028 else
1029 tbl->expected_tpt = expected_tpt_mimo20MHz;
1030 } else
1031 tbl->expected_tpt = expected_tpt_G;
1032}
1033
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001034#ifdef CONFIG_IWL4965_HT
Ben Cahill77626352007-11-29 11:09:44 +08001035/*
1036 * Find starting rate for new "search" high-throughput mode of modulation.
1037 * Goal is to find lowest expected rate (under perfect conditions) that is
1038 * above the current measured throughput of "active" mode, to give new mode
1039 * a fair chance to prove itself without too many challenges.
1040 *
1041 * This gets called when transitioning to more aggressive modulation
1042 * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
1043 * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
1044 * to decrease to match "active" throughput. When moving from MIMO to SISO,
1045 * bit rate will typically need to increase, but not if performance was bad.
1046 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001047static s32 rs_get_best_rate(struct iwl_priv *priv,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001048 struct iwl4965_lq_sta *lq_sta,
Ben Cahill77626352007-11-29 11:09:44 +08001049 struct iwl4965_scale_tbl_info *tbl, /* "search" */
Zhu Yib481de92007-09-25 17:54:57 -07001050 u16 rate_mask, s8 index, s8 rate)
1051{
Ben Cahill77626352007-11-29 11:09:44 +08001052 /* "active" values */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001053 struct iwl4965_scale_tbl_info *active_tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001054 &(lq_sta->lq_info[lq_sta->active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07001055 s32 active_sr = active_tbl->win[index].success_ratio;
Zhu Yib481de92007-09-25 17:54:57 -07001056 s32 active_tpt = active_tbl->expected_tpt[index];
Ben Cahill77626352007-11-29 11:09:44 +08001057
1058 /* expected "search" throughput */
1059 s32 *tpt_tbl = tbl->expected_tpt;
1060
1061 s32 new_rate, high, low, start_hi;
Zhu Yib481de92007-09-25 17:54:57 -07001062 u16 high_low;
1063
1064 new_rate = high = low = start_hi = IWL_RATE_INVALID;
1065
1066 for (; ;) {
1067 high_low = rs_get_adjacent_rate(rate, rate_mask, tbl->lq_type);
1068
1069 low = high_low & 0xff;
1070 high = (high_low >> 8) & 0xff;
1071
Ben Cahill77626352007-11-29 11:09:44 +08001072 /*
1073 * Lower the "search" bit rate, to give new "search" mode
1074 * approximately the same throughput as "active" if:
1075 *
1076 * 1) "Active" mode has been working modestly well (but not
1077 * great), and expected "search" throughput (under perfect
1078 * conditions) at candidate rate is above the actual
1079 * measured "active" throughput (but less than expected
1080 * "active" throughput under perfect conditions).
1081 * OR
1082 * 2) "Active" mode has been working perfectly or very well
1083 * and expected "search" throughput (under perfect
1084 * conditions) at candidate rate is above expected
1085 * "active" throughput (under perfect conditions).
1086 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001087 if ((((100 * tpt_tbl[rate]) > lq_sta->last_tpt) &&
Zhu Yib481de92007-09-25 17:54:57 -07001088 ((active_sr > IWL_RATE_DECREASE_TH) &&
1089 (active_sr <= IWL_RATE_HIGH_TH) &&
1090 (tpt_tbl[rate] <= active_tpt))) ||
1091 ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
1092 (tpt_tbl[rate] > active_tpt))) {
1093
Ben Cahill77626352007-11-29 11:09:44 +08001094 /* (2nd or later pass)
1095 * If we've already tried to raise the rate, and are
1096 * now trying to lower it, use the higher rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001097 if (start_hi != IWL_RATE_INVALID) {
1098 new_rate = start_hi;
1099 break;
1100 }
Ben Cahill77626352007-11-29 11:09:44 +08001101
Zhu Yib481de92007-09-25 17:54:57 -07001102 new_rate = rate;
Ben Cahill77626352007-11-29 11:09:44 +08001103
1104 /* Loop again with lower rate */
Zhu Yib481de92007-09-25 17:54:57 -07001105 if (low != IWL_RATE_INVALID)
1106 rate = low;
Ben Cahill77626352007-11-29 11:09:44 +08001107
1108 /* Lower rate not available, use the original */
Zhu Yib481de92007-09-25 17:54:57 -07001109 else
1110 break;
Ben Cahill77626352007-11-29 11:09:44 +08001111
1112 /* Else try to raise the "search" rate to match "active" */
Zhu Yib481de92007-09-25 17:54:57 -07001113 } else {
Ben Cahill77626352007-11-29 11:09:44 +08001114 /* (2nd or later pass)
1115 * If we've already tried to lower the rate, and are
1116 * now trying to raise it, use the lower rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001117 if (new_rate != IWL_RATE_INVALID)
1118 break;
Ben Cahill77626352007-11-29 11:09:44 +08001119
1120 /* Loop again with higher rate */
Zhu Yib481de92007-09-25 17:54:57 -07001121 else if (high != IWL_RATE_INVALID) {
1122 start_hi = high;
1123 rate = high;
Ben Cahill77626352007-11-29 11:09:44 +08001124
1125 /* Higher rate not available, use the original */
Zhu Yib481de92007-09-25 17:54:57 -07001126 } else {
1127 new_rate = rate;
1128 break;
1129 }
1130 }
1131 }
1132
1133 return new_rate;
1134}
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001135#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001136
Guy Cohenfde0db32008-04-21 15:42:01 -07001137/*FIXME:RS:this function should be replaced*/
Zhu Yib481de92007-09-25 17:54:57 -07001138static inline u8 rs_is_both_ant_supp(u8 valid_antenna)
1139{
Guy Cohenfde0db32008-04-21 15:42:01 -07001140 return (rs_is_ant_connected(valid_antenna, ANT_AB));
Zhu Yib481de92007-09-25 17:54:57 -07001141}
1142
Ben Cahill77626352007-11-29 11:09:44 +08001143/*
1144 * Set up search table for MIMO
1145 */
Guy Cohenfde0db32008-04-21 15:42:01 -07001146#ifdef CONFIG_IWL4965_HT
1147static int rs_switch_to_mimo2(struct iwl_priv *priv,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001148 struct iwl4965_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001149 struct ieee80211_conf *conf,
1150 struct sta_info *sta,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001151 struct iwl4965_scale_tbl_info *tbl, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001152{
Zhu Yib481de92007-09-25 17:54:57 -07001153 u16 rate_mask;
1154 s32 rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001155 s8 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07001156
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001157 if (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) ||
1158 !sta->ht_info.ht_supported)
Zhu Yib481de92007-09-25 17:54:57 -07001159 return -1;
1160
1161 IWL_DEBUG_HT("LQ: try to switch to MIMO\n");
Guy Cohenfde0db32008-04-21 15:42:01 -07001162 tbl->lq_type = LQ_MIMO2;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001163 rs_get_supported_rates(lq_sta, NULL, tbl->lq_type,
Zhu Yib481de92007-09-25 17:54:57 -07001164 &rate_mask);
1165
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001166 if (priv->current_ht_config.tx_mimo_ps_mode == IWL_MIMO_PS_STATIC)
Zhu Yib481de92007-09-25 17:54:57 -07001167 return -1;
1168
Ben Cahill77626352007-11-29 11:09:44 +08001169 /* Need both Tx chains/antennas to support MIMO */
Guy Cohenfde0db32008-04-21 15:42:01 -07001170 if (!rs_is_both_ant_supp(priv->hw_params.valid_tx_ant))
Zhu Yib481de92007-09-25 17:54:57 -07001171 return -1;
1172
Tomas Winklerc33104f2008-01-14 17:46:21 -08001173 tbl->is_dup = lq_sta->is_dup;
Zhu Yib481de92007-09-25 17:54:57 -07001174 tbl->action = 0;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001175 if (priv->current_ht_config.supported_chan_width
1176 == IWL_CHANNEL_WIDTH_40MHZ)
Zhu Yib481de92007-09-25 17:54:57 -07001177 tbl->is_fat = 1;
1178 else
1179 tbl->is_fat = 0;
1180
1181 if (tbl->is_fat) {
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001182 if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
Zhu Yib481de92007-09-25 17:54:57 -07001183 tbl->is_SGI = 1;
1184 else
1185 tbl->is_SGI = 0;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001186 } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
Zhu Yib481de92007-09-25 17:54:57 -07001187 tbl->is_SGI = 1;
1188 else
1189 tbl->is_SGI = 0;
1190
Tomas Winklerc33104f2008-01-14 17:46:21 -08001191 rs_get_expected_tpt_table(lq_sta, tbl);
Zhu Yib481de92007-09-25 17:54:57 -07001192
Tomas Winklerc33104f2008-01-14 17:46:21 -08001193 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index, index);
Zhu Yib481de92007-09-25 17:54:57 -07001194
1195 IWL_DEBUG_HT("LQ: MIMO best rate %d mask %X\n", rate, rate_mask);
1196 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask))
1197 return -1;
1198 rs_mcs_from_tbl(&tbl->current_rate, tbl, rate, is_green);
1199
1200 IWL_DEBUG_HT("LQ: Switch to new mcs %X index is green %X\n",
1201 tbl->current_rate.rate_n_flags, is_green);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001202 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07001203}
Guy Cohenfde0db32008-04-21 15:42:01 -07001204#else
1205static int rs_switch_to_mimo2(struct iwl_priv *priv,
1206 struct iwl4965_lq_sta *lq_sta,
1207 struct ieee80211_conf *conf,
1208 struct sta_info *sta,
1209 struct iwl4965_scale_tbl_info *tbl, int index)
1210{
1211 return -1;
1212}
1213#endif /*CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001214
Ben Cahill77626352007-11-29 11:09:44 +08001215/*
1216 * Set up search table for SISO
1217 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001218static int rs_switch_to_siso(struct iwl_priv *priv,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001219 struct iwl4965_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001220 struct ieee80211_conf *conf,
1221 struct sta_info *sta,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001222 struct iwl4965_scale_tbl_info *tbl, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001223{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001224#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07001225 u16 rate_mask;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001226 u8 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07001227 s32 rate;
1228
1229 IWL_DEBUG_HT("LQ: try to switch to SISO\n");
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001230 if (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) ||
1231 !sta->ht_info.ht_supported)
Zhu Yib481de92007-09-25 17:54:57 -07001232 return -1;
1233
Tomas Winklerc33104f2008-01-14 17:46:21 -08001234 tbl->is_dup = lq_sta->is_dup;
Zhu Yib481de92007-09-25 17:54:57 -07001235 tbl->lq_type = LQ_SISO;
1236 tbl->action = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001237 rs_get_supported_rates(lq_sta, NULL, tbl->lq_type,
Zhu Yib481de92007-09-25 17:54:57 -07001238 &rate_mask);
1239
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001240 if (priv->current_ht_config.supported_chan_width
1241 == IWL_CHANNEL_WIDTH_40MHZ)
Zhu Yib481de92007-09-25 17:54:57 -07001242 tbl->is_fat = 1;
1243 else
1244 tbl->is_fat = 0;
1245
1246 if (tbl->is_fat) {
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001247 if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
Zhu Yib481de92007-09-25 17:54:57 -07001248 tbl->is_SGI = 1;
1249 else
1250 tbl->is_SGI = 0;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001251 } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
Zhu Yib481de92007-09-25 17:54:57 -07001252 tbl->is_SGI = 1;
1253 else
1254 tbl->is_SGI = 0;
1255
1256 if (is_green)
1257 tbl->is_SGI = 0;
1258
Tomas Winklerc33104f2008-01-14 17:46:21 -08001259 rs_get_expected_tpt_table(lq_sta, tbl);
1260 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index, index);
Zhu Yib481de92007-09-25 17:54:57 -07001261
1262 IWL_DEBUG_HT("LQ: get best rate %d mask %X\n", rate, rate_mask);
1263 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1264 IWL_DEBUG_HT("can not switch with index %d rate mask %x\n",
1265 rate, rate_mask);
1266 return -1;
1267 }
1268 rs_mcs_from_tbl(&tbl->current_rate, tbl, rate, is_green);
1269 IWL_DEBUG_HT("LQ: Switch to new mcs %X index is green %X\n",
1270 tbl->current_rate.rate_n_flags, is_green);
Mohamed Abbas403ab562007-11-06 22:06:25 -08001271 return 0;
1272#else
1273 return -1;
Zhu Yib481de92007-09-25 17:54:57 -07001274
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001275#endif /*CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001276}
1277
Ben Cahill77626352007-11-29 11:09:44 +08001278/*
1279 * Try to switch to new modulation mode from legacy
1280 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001281static int rs_move_legacy_other(struct iwl_priv *priv,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001282 struct iwl4965_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001283 struct ieee80211_conf *conf,
1284 struct sta_info *sta,
Zhu Yib481de92007-09-25 17:54:57 -07001285 int index)
1286{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001287 struct iwl4965_scale_tbl_info *tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001288 &(lq_sta->lq_info[lq_sta->active_tbl]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001289 struct iwl4965_scale_tbl_info *search_tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001290 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001291 struct iwl4965_rate_scale_data *window = &(tbl->win[index]);
1292 u32 sz = (sizeof(struct iwl4965_scale_tbl_info) -
1293 (sizeof(struct iwl4965_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001294 u8 start_action = tbl->action;
Guy Cohenfde0db32008-04-21 15:42:01 -07001295 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1296 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001297
1298 for (; ;) {
1299 switch (tbl->action) {
1300 case IWL_LEGACY_SWITCH_ANTENNA:
1301 IWL_DEBUG_HT("LQ Legacy switch Antenna\n");
1302
1303 search_tbl->lq_type = LQ_NONE;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001304 lq_sta->action_counter++;
Ben Cahill77626352007-11-29 11:09:44 +08001305
1306 /* Don't change antenna if success has been great */
Zhu Yib481de92007-09-25 17:54:57 -07001307 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1308 break;
Ben Cahill77626352007-11-29 11:09:44 +08001309
1310 /* Don't change antenna if other one is not connected */
Guy Cohenfde0db32008-04-21 15:42:01 -07001311 if (!rs_is_other_ant_connected(valid_tx_ant,
1312 tbl->ant_type))
Zhu Yib481de92007-09-25 17:54:57 -07001313 break;
1314
Ben Cahill77626352007-11-29 11:09:44 +08001315 /* Set up search table to try other antenna */
Zhu Yib481de92007-09-25 17:54:57 -07001316 memcpy(search_tbl, tbl, sz);
1317
1318 rs_toggle_antenna(&(search_tbl->current_rate),
1319 search_tbl);
Tomas Winklerc33104f2008-01-14 17:46:21 -08001320 rs_get_expected_tpt_table(lq_sta, search_tbl);
1321 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001322 goto out;
1323
1324 case IWL_LEGACY_SWITCH_SISO:
1325 IWL_DEBUG_HT("LQ: Legacy switch to SISO\n");
Ben Cahill77626352007-11-29 11:09:44 +08001326
1327 /* Set up search table to try SISO */
Zhu Yib481de92007-09-25 17:54:57 -07001328 memcpy(search_tbl, tbl, sz);
1329 search_tbl->lq_type = LQ_SISO;
1330 search_tbl->is_SGI = 0;
1331 search_tbl->is_fat = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001332 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001333 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001334 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001335 lq_sta->search_better_tbl = 1;
1336 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001337 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001338 }
Zhu Yib481de92007-09-25 17:54:57 -07001339
1340 break;
Guy Cohenfde0db32008-04-21 15:42:01 -07001341 case IWL_LEGACY_SWITCH_MIMO2:
Zhu Yib481de92007-09-25 17:54:57 -07001342 IWL_DEBUG_HT("LQ: Legacy switch MIMO\n");
Ben Cahill77626352007-11-29 11:09:44 +08001343
1344 /* Set up search table to try MIMO */
Zhu Yib481de92007-09-25 17:54:57 -07001345 memcpy(search_tbl, tbl, sz);
Guy Cohenfde0db32008-04-21 15:42:01 -07001346 search_tbl->lq_type = LQ_MIMO2;
Zhu Yib481de92007-09-25 17:54:57 -07001347 search_tbl->is_SGI = 0;
1348 search_tbl->is_fat = 0;
Guy Cohenfde0db32008-04-21 15:42:01 -07001349 search_tbl->ant_type = ANT_AB;/*FIXME:RS*/
1350 /*FIXME:RS:need to check ant validity*/
1351 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001352 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001353 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001354 lq_sta->search_better_tbl = 1;
1355 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001356 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001357 }
Zhu Yib481de92007-09-25 17:54:57 -07001358 break;
1359 }
1360 tbl->action++;
Guy Cohenfde0db32008-04-21 15:42:01 -07001361 if (tbl->action > IWL_LEGACY_SWITCH_MIMO2)
Zhu Yib481de92007-09-25 17:54:57 -07001362 tbl->action = IWL_LEGACY_SWITCH_ANTENNA;
1363
1364 if (tbl->action == start_action)
1365 break;
1366
1367 }
1368 return 0;
1369
1370 out:
1371 tbl->action++;
Guy Cohenfde0db32008-04-21 15:42:01 -07001372 if (tbl->action > IWL_LEGACY_SWITCH_MIMO2)
Zhu Yib481de92007-09-25 17:54:57 -07001373 tbl->action = IWL_LEGACY_SWITCH_ANTENNA;
1374 return 0;
1375
1376}
1377
Ben Cahill77626352007-11-29 11:09:44 +08001378/*
1379 * Try to switch to new modulation mode from SISO
1380 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001381static int rs_move_siso_to_other(struct iwl_priv *priv,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001382 struct iwl4965_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001383 struct ieee80211_conf *conf,
1384 struct sta_info *sta,
Zhu Yib481de92007-09-25 17:54:57 -07001385 int index)
1386{
Tomas Winklerc33104f2008-01-14 17:46:21 -08001387 u8 is_green = lq_sta->is_green;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001388 struct iwl4965_scale_tbl_info *tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001389 &(lq_sta->lq_info[lq_sta->active_tbl]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001390 struct iwl4965_scale_tbl_info *search_tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001391 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001392 struct iwl4965_rate_scale_data *window = &(tbl->win[index]);
1393 u32 sz = (sizeof(struct iwl4965_scale_tbl_info) -
1394 (sizeof(struct iwl4965_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001395 u8 start_action = tbl->action;
Guy Cohenfde0db32008-04-21 15:42:01 -07001396 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1397 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001398
1399 for (;;) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001400 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001401 switch (tbl->action) {
1402 case IWL_SISO_SWITCH_ANTENNA:
1403 IWL_DEBUG_HT("LQ: SISO SWITCH ANTENNA SISO\n");
1404 search_tbl->lq_type = LQ_NONE;
1405 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1406 break;
Guy Cohenfde0db32008-04-21 15:42:01 -07001407 if (!rs_is_other_ant_connected(valid_tx_ant,
1408 tbl->ant_type))
Zhu Yib481de92007-09-25 17:54:57 -07001409 break;
1410
1411 memcpy(search_tbl, tbl, sz);
Guy Cohenfde0db32008-04-21 15:42:01 -07001412 search_tbl->action = IWL_SISO_SWITCH_MIMO2;
Zhu Yib481de92007-09-25 17:54:57 -07001413 rs_toggle_antenna(&(search_tbl->current_rate),
1414 search_tbl);
Tomas Winklerc33104f2008-01-14 17:46:21 -08001415 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001416
1417 goto out;
1418
Guy Cohenfde0db32008-04-21 15:42:01 -07001419 case IWL_SISO_SWITCH_MIMO2:
1420 IWL_DEBUG_HT("LQ: SISO SWITCH TO MIMO2 FROM SISO\n");
Zhu Yib481de92007-09-25 17:54:57 -07001421 memcpy(search_tbl, tbl, sz);
Guy Cohenfde0db32008-04-21 15:42:01 -07001422 search_tbl->lq_type = LQ_MIMO2;
Zhu Yib481de92007-09-25 17:54:57 -07001423 search_tbl->is_SGI = 0;
1424 search_tbl->is_fat = 0;
Guy Cohenfde0db32008-04-21 15:42:01 -07001425 search_tbl->ant_type = ANT_AB; /*FIXME:RS*/
1426 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001427 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001428 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001429 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001430 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001431 }
Zhu Yib481de92007-09-25 17:54:57 -07001432 break;
1433 case IWL_SISO_SWITCH_GI:
1434 IWL_DEBUG_HT("LQ: SISO SWITCH TO GI\n");
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001435
Zhu Yib481de92007-09-25 17:54:57 -07001436 memcpy(search_tbl, tbl, sz);
1437 search_tbl->action = 0;
1438 if (search_tbl->is_SGI)
1439 search_tbl->is_SGI = 0;
1440 else if (!is_green)
1441 search_tbl->is_SGI = 1;
1442 else
1443 break;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001444 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001445 if ((tbl->lq_type == LQ_SISO) &&
1446 (tbl->is_SGI)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001447 s32 tpt = lq_sta->last_tpt / 100;
Zhu Yib481de92007-09-25 17:54:57 -07001448 if (((!tbl->is_fat) &&
1449 (tpt >= expected_tpt_siso20MHz[index])) ||
1450 ((tbl->is_fat) &&
1451 (tpt >= expected_tpt_siso40MHz[index])))
Tomas Winklerc33104f2008-01-14 17:46:21 -08001452 lq_sta->search_better_tbl = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001453 }
Tomas Winklerc33104f2008-01-14 17:46:21 -08001454 rs_get_expected_tpt_table(lq_sta, search_tbl);
Zhu Yib481de92007-09-25 17:54:57 -07001455 rs_mcs_from_tbl(&search_tbl->current_rate,
1456 search_tbl, index, is_green);
1457 goto out;
1458 }
1459 tbl->action++;
1460 if (tbl->action > IWL_SISO_SWITCH_GI)
1461 tbl->action = IWL_SISO_SWITCH_ANTENNA;
1462
1463 if (tbl->action == start_action)
1464 break;
1465 }
1466 return 0;
1467
1468 out:
1469 tbl->action++;
1470 if (tbl->action > IWL_SISO_SWITCH_GI)
1471 tbl->action = IWL_SISO_SWITCH_ANTENNA;
1472 return 0;
1473}
1474
Ben Cahill77626352007-11-29 11:09:44 +08001475/*
1476 * Try to switch to new modulation mode from MIMO
1477 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001478static int rs_move_mimo_to_other(struct iwl_priv *priv,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001479 struct iwl4965_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001480 struct ieee80211_conf *conf,
1481 struct sta_info *sta,
Zhu Yib481de92007-09-25 17:54:57 -07001482 int index)
1483{
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001484 int ret;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001485 s8 is_green = lq_sta->is_green;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001486 struct iwl4965_scale_tbl_info *tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001487 &(lq_sta->lq_info[lq_sta->active_tbl]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001488 struct iwl4965_scale_tbl_info *search_tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001489 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001490 u32 sz = (sizeof(struct iwl4965_scale_tbl_info) -
1491 (sizeof(struct iwl4965_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001492 u8 start_action = tbl->action;
1493
1494 for (;;) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001495 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001496 switch (tbl->action) {
1497 case IWL_MIMO_SWITCH_ANTENNA_A:
1498 case IWL_MIMO_SWITCH_ANTENNA_B:
1499 IWL_DEBUG_HT("LQ: MIMO SWITCH TO SISO\n");
Ben Cahill77626352007-11-29 11:09:44 +08001500
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001501
Ben Cahill77626352007-11-29 11:09:44 +08001502 /* Set up new search table for SISO */
Zhu Yib481de92007-09-25 17:54:57 -07001503 memcpy(search_tbl, tbl, sz);
1504 search_tbl->lq_type = LQ_SISO;
1505 search_tbl->is_SGI = 0;
1506 search_tbl->is_fat = 0;
Guy Cohenfde0db32008-04-21 15:42:01 -07001507 /*FIXME:RS:need to check ant validity + C*/
Zhu Yib481de92007-09-25 17:54:57 -07001508 if (tbl->action == IWL_MIMO_SWITCH_ANTENNA_A)
Guy Cohenfde0db32008-04-21 15:42:01 -07001509 search_tbl->ant_type = ANT_A;
Zhu Yib481de92007-09-25 17:54:57 -07001510 else
Guy Cohenfde0db32008-04-21 15:42:01 -07001511 search_tbl->ant_type = ANT_B;
Zhu Yib481de92007-09-25 17:54:57 -07001512
Tomas Winklerc33104f2008-01-14 17:46:21 -08001513 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001514 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001515 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001516 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001517 goto out;
1518 }
1519 break;
1520
1521 case IWL_MIMO_SWITCH_GI:
1522 IWL_DEBUG_HT("LQ: MIMO SWITCH TO GI\n");
Ben Cahill77626352007-11-29 11:09:44 +08001523
1524 /* Set up new search table for MIMO */
Zhu Yib481de92007-09-25 17:54:57 -07001525 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001526 search_tbl->action = 0;
1527 if (search_tbl->is_SGI)
1528 search_tbl->is_SGI = 0;
1529 else
1530 search_tbl->is_SGI = 1;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001531 lq_sta->search_better_tbl = 1;
Ben Cahill77626352007-11-29 11:09:44 +08001532
1533 /*
1534 * If active table already uses the fastest possible
1535 * modulation (dual stream with short guard interval),
1536 * and it's working well, there's no need to look
1537 * for a better type of modulation!
1538 */
Guy Cohenfde0db32008-04-21 15:42:01 -07001539 if ((tbl->lq_type == LQ_MIMO2) &&
Zhu Yib481de92007-09-25 17:54:57 -07001540 (tbl->is_SGI)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001541 s32 tpt = lq_sta->last_tpt / 100;
Zhu Yib481de92007-09-25 17:54:57 -07001542 if (((!tbl->is_fat) &&
1543 (tpt >= expected_tpt_mimo20MHz[index])) ||
1544 ((tbl->is_fat) &&
1545 (tpt >= expected_tpt_mimo40MHz[index])))
Tomas Winklerc33104f2008-01-14 17:46:21 -08001546 lq_sta->search_better_tbl = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001547 }
Tomas Winklerc33104f2008-01-14 17:46:21 -08001548 rs_get_expected_tpt_table(lq_sta, search_tbl);
Zhu Yib481de92007-09-25 17:54:57 -07001549 rs_mcs_from_tbl(&search_tbl->current_rate,
1550 search_tbl, index, is_green);
1551 goto out;
1552
1553 }
1554 tbl->action++;
1555 if (tbl->action > IWL_MIMO_SWITCH_GI)
1556 tbl->action = IWL_MIMO_SWITCH_ANTENNA_A;
1557
1558 if (tbl->action == start_action)
1559 break;
1560 }
1561
1562 return 0;
1563 out:
1564 tbl->action++;
1565 if (tbl->action > IWL_MIMO_SWITCH_GI)
1566 tbl->action = IWL_MIMO_SWITCH_ANTENNA_A;
1567 return 0;
1568
1569}
1570
Ben Cahill77626352007-11-29 11:09:44 +08001571/*
1572 * Check whether we should continue using same modulation mode, or
1573 * begin search for a new mode, based on:
1574 * 1) # tx successes or failures while using this mode
1575 * 2) # times calling this function
1576 * 3) elapsed time in this mode (not used, for now)
1577 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001578static void rs_stay_in_table(struct iwl4965_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07001579{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001580 struct iwl4965_scale_tbl_info *tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001581 int i;
1582 int active_tbl;
1583 int flush_interval_passed = 0;
1584
Tomas Winklerc33104f2008-01-14 17:46:21 -08001585 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001586
Tomas Winklerc33104f2008-01-14 17:46:21 -08001587 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07001588
Ben Cahill77626352007-11-29 11:09:44 +08001589 /* If we've been disallowing search, see if we should now allow it */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001590 if (lq_sta->stay_in_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07001591
Ben Cahill77626352007-11-29 11:09:44 +08001592 /* Elapsed time using current modulation mode */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001593 if (lq_sta->flush_timer)
Zhu Yib481de92007-09-25 17:54:57 -07001594 flush_interval_passed =
1595 time_after(jiffies,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001596 (unsigned long)(lq_sta->flush_timer +
Zhu Yib481de92007-09-25 17:54:57 -07001597 IWL_RATE_SCALE_FLUSH_INTVL));
1598
Ben Cahill77626352007-11-29 11:09:44 +08001599 /* For now, disable the elapsed time criterion */
Zhu Yib481de92007-09-25 17:54:57 -07001600 flush_interval_passed = 0;
Ben Cahill77626352007-11-29 11:09:44 +08001601
1602 /*
1603 * Check if we should allow search for new modulation mode.
1604 * If many frames have failed or succeeded, or we've used
1605 * this same modulation for a long time, allow search, and
1606 * reset history stats that keep track of whether we should
1607 * allow a new search. Also (below) reset all bitmaps and
1608 * stats in active history.
1609 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001610 if ((lq_sta->total_failed > lq_sta->max_failure_limit) ||
1611 (lq_sta->total_success > lq_sta->max_success_limit) ||
1612 ((!lq_sta->search_better_tbl) && (lq_sta->flush_timer)
Zhu Yib481de92007-09-25 17:54:57 -07001613 && (flush_interval_passed))) {
1614 IWL_DEBUG_HT("LQ: stay is expired %d %d %d\n:",
Tomas Winklerc33104f2008-01-14 17:46:21 -08001615 lq_sta->total_failed,
1616 lq_sta->total_success,
Zhu Yib481de92007-09-25 17:54:57 -07001617 flush_interval_passed);
Ben Cahill77626352007-11-29 11:09:44 +08001618
1619 /* Allow search for new mode */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001620 lq_sta->stay_in_tbl = 0; /* only place reset */
1621 lq_sta->total_failed = 0;
1622 lq_sta->total_success = 0;
1623 lq_sta->flush_timer = 0;
Ben Cahill77626352007-11-29 11:09:44 +08001624
1625 /*
1626 * Else if we've used this modulation mode enough repetitions
1627 * (regardless of elapsed time or success/failure), reset
1628 * history bitmaps and rate-specific stats for all rates in
1629 * active table.
1630 */
Mohamed Abbas403ab562007-11-06 22:06:25 -08001631 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001632 lq_sta->table_count++;
1633 if (lq_sta->table_count >=
1634 lq_sta->table_count_limit) {
1635 lq_sta->table_count = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001636
1637 IWL_DEBUG_HT("LQ: stay in table clear win\n");
1638 for (i = 0; i < IWL_RATE_COUNT; i++)
1639 rs_rate_scale_clear_window(
1640 &(tbl->win[i]));
1641 }
1642 }
1643
Ben Cahill77626352007-11-29 11:09:44 +08001644 /* If transitioning to allow "search", reset all history
1645 * bitmaps and stats in active table (this will become the new
1646 * "search" table). */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001647 if (!lq_sta->stay_in_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07001648 for (i = 0; i < IWL_RATE_COUNT; i++)
1649 rs_rate_scale_clear_window(&(tbl->win[i]));
1650 }
1651 }
1652}
1653
Ben Cahill77626352007-11-29 11:09:44 +08001654/*
1655 * Do rate scaling and search for new modulation mode.
1656 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001657static void rs_rate_scale_perform(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001658 struct net_device *dev,
1659 struct ieee80211_hdr *hdr,
1660 struct sta_info *sta)
1661{
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001662 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1663 struct ieee80211_hw *hw = local_to_hw(local);
1664 struct ieee80211_conf *conf = &hw->conf;
Zhu Yib481de92007-09-25 17:54:57 -07001665 int low = IWL_RATE_INVALID;
1666 int high = IWL_RATE_INVALID;
1667 int index;
1668 int i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001669 struct iwl4965_rate_scale_data *window = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001670 int current_tpt = IWL_INVALID_VALUE;
1671 int low_tpt = IWL_INVALID_VALUE;
1672 int high_tpt = IWL_INVALID_VALUE;
1673 u32 fail_count;
1674 s8 scale_action = 0;
1675 u16 fc, rate_mask;
1676 u8 update_lq = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001677 struct iwl4965_lq_sta *lq_sta;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001678 struct iwl4965_scale_tbl_info *tbl, *tbl1;
Zhu Yib481de92007-09-25 17:54:57 -07001679 u16 rate_scale_index_msk = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001680 struct iwl4965_rate mcs_rate;
Zhu Yib481de92007-09-25 17:54:57 -07001681 u8 is_green = 0;
1682 u8 active_tbl = 0;
1683 u8 done_search = 0;
1684 u16 high_low;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001685#ifdef CONFIG_IWL4965_HT
1686 u8 tid = MAX_TID_COUNT;
1687 __le16 *qc;
1688#endif
Zhu Yib481de92007-09-25 17:54:57 -07001689
1690 IWL_DEBUG_RATE("rate scale calculate new rate for skb\n");
1691
1692 fc = le16_to_cpu(hdr->frame_control);
1693 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1)) {
1694 /* Send management frames and broadcast/multicast data using
1695 * lowest rate. */
1696 /* TODO: this could probably be improved.. */
1697 return;
1698 }
1699
1700 if (!sta || !sta->rate_ctrl_priv)
1701 return;
1702
1703 if (!priv->lq_mngr.lq_ready) {
1704 IWL_DEBUG_RATE("still rate scaling not ready\n");
1705 return;
1706 }
Tomas Winklerc33104f2008-01-14 17:46:21 -08001707 lq_sta = (struct iwl4965_lq_sta *)sta->rate_ctrl_priv;
Zhu Yib481de92007-09-25 17:54:57 -07001708
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001709#ifdef CONFIG_IWL4965_HT
1710 qc = ieee80211_get_qos_ctrl(hdr);
1711 if (qc) {
1712 tid = (u8)(le16_to_cpu(*qc) & 0xf);
1713 rs_tl_add_packet(lq_sta, tid);
1714 }
1715#endif
Ben Cahill77626352007-11-29 11:09:44 +08001716 /*
1717 * Select rate-scale / modulation-mode table to work with in
1718 * the rest of this function: "search" if searching for better
1719 * modulation mode, or "active" if doing rate scaling within a mode.
1720 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001721 if (!lq_sta->search_better_tbl)
1722 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001723 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08001724 active_tbl = 1 - lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001725
Tomas Winklerc33104f2008-01-14 17:46:21 -08001726 tbl = &(lq_sta->lq_info[active_tbl]);
1727 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07001728
Ben Cahill77626352007-11-29 11:09:44 +08001729 /* current tx rate */
Johannes Berg8318d782008-01-24 19:38:38 +01001730 index = sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07001731
1732 IWL_DEBUG_RATE("Rate scale index %d for type %d\n", index,
1733 tbl->lq_type);
1734
Ben Cahill77626352007-11-29 11:09:44 +08001735 /* rates available for this association, and for modulation mode */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001736 rs_get_supported_rates(lq_sta, hdr, tbl->lq_type,
Zhu Yib481de92007-09-25 17:54:57 -07001737 &rate_mask);
1738
1739 IWL_DEBUG_RATE("mask 0x%04X \n", rate_mask);
1740
1741 /* mask with station rate restriction */
1742 if (is_legacy(tbl->lq_type)) {
Johannes Berg8318d782008-01-24 19:38:38 +01001743 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Ben Cahill77626352007-11-29 11:09:44 +08001744 /* supp_rates has no CCK bits in A mode */
Zhu Yib481de92007-09-25 17:54:57 -07001745 rate_scale_index_msk = (u16) (rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -08001746 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
Zhu Yib481de92007-09-25 17:54:57 -07001747 else
1748 rate_scale_index_msk = (u16) (rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -08001749 lq_sta->supp_rates);
Zhu Yib481de92007-09-25 17:54:57 -07001750
1751 } else
1752 rate_scale_index_msk = rate_mask;
1753
1754 if (!rate_scale_index_msk)
1755 rate_scale_index_msk = rate_mask;
1756
Ben Cahill77626352007-11-29 11:09:44 +08001757 /* If current rate is no longer supported on current association,
1758 * or user changed preferences for rates, find a new supported rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001759 if (index < 0 || !((1 << index) & rate_scale_index_msk)) {
1760 index = IWL_INVALID_VALUE;
1761 update_lq = 1;
1762
Ben Cahill77626352007-11-29 11:09:44 +08001763 /* get the highest available rate */
Zhu Yib481de92007-09-25 17:54:57 -07001764 for (i = 0; i <= IWL_RATE_COUNT; i++) {
1765 if ((1 << i) & rate_scale_index_msk)
1766 index = i;
1767 }
1768
1769 if (index == IWL_INVALID_VALUE) {
1770 IWL_WARNING("Can not find a suitable rate\n");
1771 return;
1772 }
1773 }
1774
Ben Cahill77626352007-11-29 11:09:44 +08001775 /* Get expected throughput table and history window for current rate */
Zhu Yib481de92007-09-25 17:54:57 -07001776 if (!tbl->expected_tpt)
Tomas Winklerc33104f2008-01-14 17:46:21 -08001777 rs_get_expected_tpt_table(lq_sta, tbl);
Zhu Yib481de92007-09-25 17:54:57 -07001778
1779 window = &(tbl->win[index]);
1780
Ben Cahill77626352007-11-29 11:09:44 +08001781 /*
1782 * If there is not enough history to calculate actual average
1783 * throughput, keep analyzing results of more tx frames, without
1784 * changing rate or mode (bypass most of the rest of this function).
1785 * Set up new rate table in uCode only if old rate is not supported
1786 * in current association (use new rate found above).
1787 */
Zhu Yib481de92007-09-25 17:54:57 -07001788 fail_count = window->counter - window->success_counter;
1789 if (((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
1790 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH))
1791 || (tbl->expected_tpt == NULL)) {
1792 IWL_DEBUG_RATE("LQ: still below TH succ %d total %d "
1793 "for index %d\n",
1794 window->success_counter, window->counter, index);
Ben Cahill77626352007-11-29 11:09:44 +08001795
1796 /* Can't calculate this yet; not enough history */
Zhu Yib481de92007-09-25 17:54:57 -07001797 window->average_tpt = IWL_INVALID_VALUE;
Ben Cahill77626352007-11-29 11:09:44 +08001798
1799 /* Should we stay with this modulation mode,
1800 * or search for a new one? */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001801 rs_stay_in_table(lq_sta);
Ben Cahill77626352007-11-29 11:09:44 +08001802
1803 /* Set up new rate table in uCode, if needed */
Zhu Yib481de92007-09-25 17:54:57 -07001804 if (update_lq) {
1805 rs_mcs_from_tbl(&mcs_rate, tbl, index, is_green);
Guy Cohenfde0db32008-04-21 15:42:01 -07001806 rs_fill_link_cmd(priv, lq_sta, &mcs_rate, &lq_sta->lq);
Tomas Winkler66c73db2008-04-15 16:01:40 -07001807 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07001808 }
1809 goto out;
1810
Ben Cahill77626352007-11-29 11:09:44 +08001811 /* Else we have enough samples; calculate estimate of
1812 * actual average throughput */
Zhu Yib481de92007-09-25 17:54:57 -07001813 } else
1814 window->average_tpt = ((window->success_ratio *
1815 tbl->expected_tpt[index] + 64) / 128);
1816
Ben Cahill77626352007-11-29 11:09:44 +08001817 /* If we are searching for better modulation mode, check success. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001818 if (lq_sta->search_better_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07001819 int success_limit = IWL_RATE_SCALE_SWITCH;
1820
Ben Cahill77626352007-11-29 11:09:44 +08001821 /* If good success, continue using the "search" mode;
1822 * no need to send new link quality command, since we're
1823 * continuing to use the setup that we've been trying. */
Zhu Yib481de92007-09-25 17:54:57 -07001824 if ((window->success_ratio > success_limit) ||
Tomas Winklerc33104f2008-01-14 17:46:21 -08001825 (window->average_tpt > lq_sta->last_tpt)) {
Zhu Yib481de92007-09-25 17:54:57 -07001826 if (!is_legacy(tbl->lq_type)) {
1827 IWL_DEBUG_HT("LQ: we are switching to HT"
1828 " rate suc %d current tpt %d"
1829 " old tpt %d\n",
1830 window->success_ratio,
1831 window->average_tpt,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001832 lq_sta->last_tpt);
1833 lq_sta->enable_counter = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001834 }
Ben Cahill77626352007-11-29 11:09:44 +08001835 /* Swap tables; "search" becomes "active" */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001836 lq_sta->active_tbl = active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001837 current_tpt = window->average_tpt;
Ben Cahill77626352007-11-29 11:09:44 +08001838
1839 /* Else poor success; go back to mode in "active" table */
Zhu Yib481de92007-09-25 17:54:57 -07001840 } else {
Ben Cahill77626352007-11-29 11:09:44 +08001841 /* Nullify "search" table */
Zhu Yib481de92007-09-25 17:54:57 -07001842 tbl->lq_type = LQ_NONE;
Ben Cahill77626352007-11-29 11:09:44 +08001843
1844 /* Revert to "active" table */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001845 active_tbl = lq_sta->active_tbl;
1846 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07001847
Ben Cahill77626352007-11-29 11:09:44 +08001848 /* Revert to "active" rate and throughput info */
Tomas Winkler17744ff2008-03-02 01:52:00 +02001849 index = iwl4965_hwrate_to_plcp_idx(
Zhu Yib481de92007-09-25 17:54:57 -07001850 tbl->current_rate.rate_n_flags);
Tomas Winklerc33104f2008-01-14 17:46:21 -08001851 current_tpt = lq_sta->last_tpt;
Ben Cahill77626352007-11-29 11:09:44 +08001852
1853 /* Need to set up a new rate table in uCode */
1854 update_lq = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001855 IWL_DEBUG_HT("XXY GO BACK TO OLD TABLE\n");
1856 }
Ben Cahill77626352007-11-29 11:09:44 +08001857
1858 /* Either way, we've made a decision; modulation mode
1859 * search is done, allow rate adjustment next time. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001860 lq_sta->search_better_tbl = 0;
Ben Cahill77626352007-11-29 11:09:44 +08001861 done_search = 1; /* Don't switch modes below! */
Zhu Yib481de92007-09-25 17:54:57 -07001862 goto lq_update;
1863 }
1864
Ben Cahill77626352007-11-29 11:09:44 +08001865 /* (Else) not in search of better modulation mode, try for better
1866 * starting rate, while staying in this mode. */
Zhu Yib481de92007-09-25 17:54:57 -07001867 high_low = rs_get_adjacent_rate(index, rate_scale_index_msk,
1868 tbl->lq_type);
1869 low = high_low & 0xff;
1870 high = (high_low >> 8) & 0xff;
1871
Ben Cahill77626352007-11-29 11:09:44 +08001872 /* Collect measured throughputs for current and adjacent rates */
Zhu Yib481de92007-09-25 17:54:57 -07001873 current_tpt = window->average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07001874 if (low != IWL_RATE_INVALID)
1875 low_tpt = tbl->win[low].average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07001876 if (high != IWL_RATE_INVALID)
1877 high_tpt = tbl->win[high].average_tpt;
1878
Ben Cahill77626352007-11-29 11:09:44 +08001879 /* Assume rate increase */
Zhu Yib481de92007-09-25 17:54:57 -07001880 scale_action = 1;
1881
Ben Cahill77626352007-11-29 11:09:44 +08001882 /* Too many failures, decrease rate */
Zhu Yib481de92007-09-25 17:54:57 -07001883 if ((window->success_ratio <= IWL_RATE_DECREASE_TH) ||
1884 (current_tpt == 0)) {
1885 IWL_DEBUG_RATE("decrease rate because of low success_ratio\n");
1886 scale_action = -1;
Ben Cahill77626352007-11-29 11:09:44 +08001887
1888 /* No throughput measured yet for adjacent rates; try increase. */
Zhu Yib481de92007-09-25 17:54:57 -07001889 } else if ((low_tpt == IWL_INVALID_VALUE) &&
1890 (high_tpt == IWL_INVALID_VALUE))
1891 scale_action = 1;
Ben Cahill77626352007-11-29 11:09:44 +08001892
1893 /* Both adjacent throughputs are measured, but neither one has better
1894 * throughput; we're using the best rate, don't change it! */
Zhu Yib481de92007-09-25 17:54:57 -07001895 else if ((low_tpt != IWL_INVALID_VALUE) &&
1896 (high_tpt != IWL_INVALID_VALUE) &&
1897 (low_tpt < current_tpt) &&
1898 (high_tpt < current_tpt))
1899 scale_action = 0;
Ben Cahill77626352007-11-29 11:09:44 +08001900
1901 /* At least one adjacent rate's throughput is measured,
1902 * and may have better performance. */
Zhu Yib481de92007-09-25 17:54:57 -07001903 else {
Ben Cahill77626352007-11-29 11:09:44 +08001904 /* Higher adjacent rate's throughput is measured */
Zhu Yib481de92007-09-25 17:54:57 -07001905 if (high_tpt != IWL_INVALID_VALUE) {
Ben Cahill77626352007-11-29 11:09:44 +08001906 /* Higher rate has better throughput */
Zhu Yib481de92007-09-25 17:54:57 -07001907 if (high_tpt > current_tpt)
1908 scale_action = 1;
1909 else {
1910 IWL_DEBUG_RATE
1911 ("decrease rate because of high tpt\n");
1912 scale_action = -1;
1913 }
Ben Cahill77626352007-11-29 11:09:44 +08001914
1915 /* Lower adjacent rate's throughput is measured */
Zhu Yib481de92007-09-25 17:54:57 -07001916 } else if (low_tpt != IWL_INVALID_VALUE) {
Ben Cahill77626352007-11-29 11:09:44 +08001917 /* Lower rate has better throughput */
Zhu Yib481de92007-09-25 17:54:57 -07001918 if (low_tpt > current_tpt) {
1919 IWL_DEBUG_RATE
1920 ("decrease rate because of low tpt\n");
1921 scale_action = -1;
1922 } else
1923 scale_action = 1;
1924 }
1925 }
1926
Ben Cahill77626352007-11-29 11:09:44 +08001927 /* Sanity check; asked for decrease, but success rate or throughput
1928 * has been good at old rate. Don't change it. */
Zhu Yib481de92007-09-25 17:54:57 -07001929 if (scale_action == -1) {
1930 if ((low != IWL_RATE_INVALID) &&
1931 ((window->success_ratio > IWL_RATE_HIGH_TH) ||
1932 (current_tpt > (100 * tbl->expected_tpt[low]))))
1933 scale_action = 0;
Ben Cahill77626352007-11-29 11:09:44 +08001934
1935 /* Sanity check; asked for increase, but success rate has not been great
1936 * even at old rate, higher rate will be worse. Don't change it. */
Zhu Yib481de92007-09-25 17:54:57 -07001937 } else if ((scale_action == 1) &&
1938 (window->success_ratio < IWL_RATE_INCREASE_TH))
1939 scale_action = 0;
1940
1941 switch (scale_action) {
1942 case -1:
Ben Cahill77626352007-11-29 11:09:44 +08001943 /* Decrease starting rate, update uCode's rate table */
Zhu Yib481de92007-09-25 17:54:57 -07001944 if (low != IWL_RATE_INVALID) {
1945 update_lq = 1;
1946 index = low;
1947 }
1948 break;
1949 case 1:
Ben Cahill77626352007-11-29 11:09:44 +08001950 /* Increase starting rate, update uCode's rate table */
Zhu Yib481de92007-09-25 17:54:57 -07001951 if (high != IWL_RATE_INVALID) {
1952 update_lq = 1;
1953 index = high;
1954 }
1955
1956 break;
1957 case 0:
Ben Cahill77626352007-11-29 11:09:44 +08001958 /* No change */
Zhu Yib481de92007-09-25 17:54:57 -07001959 default:
1960 break;
1961 }
1962
1963 IWL_DEBUG_HT("choose rate scale index %d action %d low %d "
1964 "high %d type %d\n",
1965 index, scale_action, low, high, tbl->lq_type);
1966
1967 lq_update:
Ben Cahill77626352007-11-29 11:09:44 +08001968 /* Replace uCode's rate table for the destination station. */
Zhu Yib481de92007-09-25 17:54:57 -07001969 if (update_lq) {
1970 rs_mcs_from_tbl(&mcs_rate, tbl, index, is_green);
Guy Cohenfde0db32008-04-21 15:42:01 -07001971 rs_fill_link_cmd(priv, lq_sta, &mcs_rate, &lq_sta->lq);
Tomas Winkler66c73db2008-04-15 16:01:40 -07001972 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07001973 }
Ben Cahill77626352007-11-29 11:09:44 +08001974
1975 /* Should we stay with this modulation mode, or search for a new one? */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001976 rs_stay_in_table(lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07001977
Ben Cahill77626352007-11-29 11:09:44 +08001978 /*
1979 * Search for new modulation mode if we're:
1980 * 1) Not changing rates right now
1981 * 2) Not just finishing up a search
1982 * 3) Allowing a new search
1983 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001984 if (!update_lq && !done_search && !lq_sta->stay_in_tbl) {
Ben Cahill77626352007-11-29 11:09:44 +08001985 /* Save current throughput to compare with "search" throughput*/
Tomas Winklerc33104f2008-01-14 17:46:21 -08001986 lq_sta->last_tpt = current_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07001987
Ben Cahill77626352007-11-29 11:09:44 +08001988 /* Select a new "search" modulation mode to try.
1989 * If one is found, set up the new "search" table. */
Zhu Yib481de92007-09-25 17:54:57 -07001990 if (is_legacy(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08001991 rs_move_legacy_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07001992 else if (is_siso(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08001993 rs_move_siso_to_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07001994 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08001995 rs_move_mimo_to_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07001996
Ben Cahill77626352007-11-29 11:09:44 +08001997 /* If new "search" mode was selected, set up in uCode table */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001998 if (lq_sta->search_better_tbl) {
Ben Cahill77626352007-11-29 11:09:44 +08001999 /* Access the "search" table, clear its history. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002000 tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Zhu Yib481de92007-09-25 17:54:57 -07002001 for (i = 0; i < IWL_RATE_COUNT; i++)
2002 rs_rate_scale_clear_window(&(tbl->win[i]));
2003
Ben Cahill77626352007-11-29 11:09:44 +08002004 /* Use new "search" start rate */
Tomas Winkler17744ff2008-03-02 01:52:00 +02002005 index = iwl4965_hwrate_to_plcp_idx(
Zhu Yib481de92007-09-25 17:54:57 -07002006 tbl->current_rate.rate_n_flags);
2007
2008 IWL_DEBUG_HT("Switch current mcs: %X index: %d\n",
2009 tbl->current_rate.rate_n_flags, index);
Guy Cohenfde0db32008-04-21 15:42:01 -07002010 rs_fill_link_cmd(priv, lq_sta, &tbl->current_rate,
Tomas Winklerc33104f2008-01-14 17:46:21 -08002011 &lq_sta->lq);
Tomas Winkler66c73db2008-04-15 16:01:40 -07002012 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07002013 }
Zhu Yib481de92007-09-25 17:54:57 -07002014
Ben Cahill77626352007-11-29 11:09:44 +08002015 /* If the "active" (non-search) mode was legacy,
2016 * and we've tried switching antennas,
2017 * but we haven't been able to try HT modes (not available),
2018 * stay with best antenna legacy modulation for a while
2019 * before next round of mode comparisons. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002020 tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07002021 if (is_legacy(tbl1->lq_type) &&
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002022#ifdef CONFIG_IWL4965_HT
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002023 (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE)) &&
Zhu Yib481de92007-09-25 17:54:57 -07002024#endif
Tomas Winklerc33104f2008-01-14 17:46:21 -08002025 (lq_sta->action_counter >= 1)) {
2026 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002027 IWL_DEBUG_HT("LQ: STAY in legacy table\n");
Tomas Winklerc33104f2008-01-14 17:46:21 -08002028 rs_set_stay_in_table(1, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002029 }
2030
Ben Cahill77626352007-11-29 11:09:44 +08002031 /* If we're in an HT mode, and all 3 mode switch actions
2032 * have been tried and compared, stay in this best modulation
2033 * mode for a while before next round of mode comparisons. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002034 if (lq_sta->enable_counter &&
2035 (lq_sta->action_counter >= IWL_ACTION_LIMIT)) {
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002036#ifdef CONFIG_IWL4965_HT
2037 if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
2038 (lq_sta->tx_agg_tid_en & (1 << tid)) &&
2039 (tid != MAX_TID_COUNT)) {
2040 IWL_DEBUG_HT("try to aggregate tid %d\n", tid);
2041 rs_tl_turn_on_agg(priv, tid, lq_sta, sta);
Zhu Yib481de92007-09-25 17:54:57 -07002042 }
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002043#endif /*CONFIG_IWL4965_HT */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002044 lq_sta->action_counter = 0;
2045 rs_set_stay_in_table(0, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002046 }
Ben Cahill77626352007-11-29 11:09:44 +08002047
2048 /*
2049 * Else, don't search for a new modulation mode.
2050 * Put new timestamp in stay-in-modulation-mode flush timer if:
2051 * 1) Not changing rates right now
2052 * 2) Not just finishing up a search
2053 * 3) flush timer is empty
2054 */
Zhu Yib481de92007-09-25 17:54:57 -07002055 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002056 if ((!update_lq) && (!done_search) && (!lq_sta->flush_timer))
2057 lq_sta->flush_timer = jiffies;
Zhu Yib481de92007-09-25 17:54:57 -07002058 }
2059
2060out:
2061 rs_mcs_from_tbl(&tbl->current_rate, tbl, index, is_green);
2062 i = index;
Johannes Berg8318d782008-01-24 19:38:38 +01002063 sta->last_txrate_idx = i;
Zhu Yib481de92007-09-25 17:54:57 -07002064
Johannes Berg8318d782008-01-24 19:38:38 +01002065 /* sta->txrate_idx is an index to A mode rates which start
Zhu Yib481de92007-09-25 17:54:57 -07002066 * at IWL_FIRST_OFDM_RATE
2067 */
Johannes Berg8318d782008-01-24 19:38:38 +01002068 if (lq_sta->band == IEEE80211_BAND_5GHZ)
2069 sta->txrate_idx = i - IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -07002070 else
Johannes Berg8318d782008-01-24 19:38:38 +01002071 sta->txrate_idx = i;
Zhu Yib481de92007-09-25 17:54:57 -07002072
2073 return;
2074}
2075
2076
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002077static void rs_initialize_lq(struct iwl_priv *priv,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002078 struct ieee80211_conf *conf,
Zhu Yib481de92007-09-25 17:54:57 -07002079 struct sta_info *sta)
2080{
2081 int i;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002082 struct iwl4965_lq_sta *lq_sta;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002083 struct iwl4965_scale_tbl_info *tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002084 u8 active_tbl = 0;
2085 int rate_idx;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002086 u8 use_green = rs_use_green(priv, conf);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002087 struct iwl4965_rate mcs_rate;
Zhu Yib481de92007-09-25 17:54:57 -07002088
2089 if (!sta || !sta->rate_ctrl_priv)
2090 goto out;
2091
Tomas Winklerc33104f2008-01-14 17:46:21 -08002092 lq_sta = (struct iwl4965_lq_sta *)sta->rate_ctrl_priv;
Johannes Berg8318d782008-01-24 19:38:38 +01002093 i = sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002094
Tomas Winklerc33104f2008-01-14 17:46:21 -08002095 if ((lq_sta->lq.sta_id == 0xff) &&
Zhu Yib481de92007-09-25 17:54:57 -07002096 (priv->iw_mode == IEEE80211_IF_TYPE_IBSS))
2097 goto out;
2098
Tomas Winklerc33104f2008-01-14 17:46:21 -08002099 if (!lq_sta->search_better_tbl)
2100 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002101 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002102 active_tbl = 1 - lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002103
Tomas Winklerc33104f2008-01-14 17:46:21 -08002104 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07002105
2106 if ((i < 0) || (i >= IWL_RATE_COUNT))
2107 i = 0;
2108
Guy Cohenfde0db32008-04-21 15:42:01 -07002109 /* FIXME:RS: This is also wrong in 4965 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002110 mcs_rate.rate_n_flags = iwl4965_rates[i].plcp ;
Zhu Yib481de92007-09-25 17:54:57 -07002111 mcs_rate.rate_n_flags |= RATE_MCS_ANT_B_MSK;
2112 mcs_rate.rate_n_flags &= ~RATE_MCS_ANT_A_MSK;
2113
2114 if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
2115 mcs_rate.rate_n_flags |= RATE_MCS_CCK_MSK;
2116
Guy Cohenfde0db32008-04-21 15:42:01 -07002117 tbl->ant_type = ANT_B;
Johannes Berg8318d782008-01-24 19:38:38 +01002118 rs_get_tbl_info_from_mcs(&mcs_rate, priv->band, tbl, &rate_idx);
Guy Cohenfde0db32008-04-21 15:42:01 -07002119 if (!rs_is_ant_connected(priv->hw_params.valid_tx_ant, tbl->ant_type))
Zhu Yi46640a82007-09-27 11:27:34 +08002120 rs_toggle_antenna(&mcs_rate, tbl);
Zhu Yib481de92007-09-25 17:54:57 -07002121
2122 rs_mcs_from_tbl(&mcs_rate, tbl, rate_idx, use_green);
2123 tbl->current_rate.rate_n_flags = mcs_rate.rate_n_flags;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002124 rs_get_expected_tpt_table(lq_sta, tbl);
Guy Cohenfde0db32008-04-21 15:42:01 -07002125 rs_fill_link_cmd(NULL, lq_sta, &mcs_rate, &lq_sta->lq);
Tomas Winkler66c73db2008-04-15 16:01:40 -07002126 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07002127 out:
2128 return;
2129}
2130
Mattias Nissler1abbe492007-12-20 13:50:07 +01002131static void rs_get_rate(void *priv_rate, struct net_device *dev,
Johannes Berg8318d782008-01-24 19:38:38 +01002132 struct ieee80211_supported_band *sband,
2133 struct sk_buff *skb,
Mattias Nissler1abbe492007-12-20 13:50:07 +01002134 struct rate_selection *sel)
Zhu Yib481de92007-09-25 17:54:57 -07002135{
2136
2137 int i;
2138 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002139 struct ieee80211_conf *conf = &local->hw.conf;
Zhu Yib481de92007-09-25 17:54:57 -07002140 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2141 struct sta_info *sta;
Michael Wu2bc454b2007-12-25 19:33:16 -05002142 u16 fc;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002143 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002144 struct iwl4965_lq_sta *lq_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002145
Zhu Yi58826352007-09-27 11:27:39 +08002146 IWL_DEBUG_RATE_LIMIT("rate scale calculate new rate for skb\n");
Zhu Yib481de92007-09-25 17:54:57 -07002147
Johannes Bergd0709a62008-02-25 16:27:46 +01002148 rcu_read_lock();
2149
Zhu Yib481de92007-09-25 17:54:57 -07002150 sta = sta_info_get(local, hdr->addr1);
2151
Michael Wu2bc454b2007-12-25 19:33:16 -05002152 /* Send management frames and broadcast/multicast data using lowest
2153 * rate. */
2154 fc = le16_to_cpu(hdr->frame_control);
2155 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1) ||
2156 !sta || !sta->rate_ctrl_priv) {
Johannes Berg8318d782008-01-24 19:38:38 +01002157 sel->rate = rate_lowest(local, sband, sta);
Tomas Winklerf4d60822008-03-05 11:31:00 -08002158 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07002159 }
2160
Tomas Winklerc33104f2008-01-14 17:46:21 -08002161 lq_sta = (struct iwl4965_lq_sta *)sta->rate_ctrl_priv;
Johannes Berg8318d782008-01-24 19:38:38 +01002162 i = sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002163
Tomas Winklerc33104f2008-01-14 17:46:21 -08002164 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
2165 !lq_sta->ibss_sta_added) {
Tomas Winkler947b13a2008-04-16 16:34:48 -07002166 u8 sta_id = iwl_find_station(priv, hdr->addr1);
Joe Perches0795af52007-10-03 17:59:30 -07002167 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07002168
2169 if (sta_id == IWL_INVALID_STATION) {
Joe Perches0795af52007-10-03 17:59:30 -07002170 IWL_DEBUG_RATE("LQ: ADD station %s\n",
2171 print_mac(mac, hdr->addr1));
Ron Rindjunsky67d62032007-11-26 16:14:40 +02002172 sta_id = iwl4965_add_station_flags(priv, hdr->addr1,
2173 0, CMD_ASYNC, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07002174 }
2175 if ((sta_id != IWL_INVALID_STATION)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002176 lq_sta->lq.sta_id = sta_id;
2177 lq_sta->lq.rs_table[0].rate_n_flags = 0;
2178 lq_sta->ibss_sta_added = 1;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002179 rs_initialize_lq(priv, conf, sta);
Zhu Yib481de92007-09-25 17:54:57 -07002180 }
Tomas Winklerc33104f2008-01-14 17:46:21 -08002181 if (!lq_sta->ibss_sta_added)
Zhu Yib481de92007-09-25 17:54:57 -07002182 goto done;
2183 }
2184
Tomas Winklerf4d60822008-03-05 11:31:00 -08002185done:
Mattias Nissler1abbe492007-12-20 13:50:07 +01002186 if ((i < 0) || (i > IWL_RATE_COUNT)) {
Johannes Berg8318d782008-01-24 19:38:38 +01002187 sel->rate = rate_lowest(local, sband, sta);
Tomas Winklerf4d60822008-03-05 11:31:00 -08002188 goto out;
Mattias Nissler1abbe492007-12-20 13:50:07 +01002189 }
Zhu Yib481de92007-09-25 17:54:57 -07002190
Mattias Nissler1abbe492007-12-20 13:50:07 +01002191 sel->rate = &priv->ieee_rates[i];
Tomas Winklerf4d60822008-03-05 11:31:00 -08002192out:
2193 rcu_read_unlock();
Zhu Yib481de92007-09-25 17:54:57 -07002194}
2195
2196static void *rs_alloc_sta(void *priv, gfp_t gfp)
2197{
Tomas Winklerc33104f2008-01-14 17:46:21 -08002198 struct iwl4965_lq_sta *lq_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002199 int i, j;
2200
2201 IWL_DEBUG_RATE("create station rate scale window\n");
2202
Tomas Winklerc33104f2008-01-14 17:46:21 -08002203 lq_sta = kzalloc(sizeof(struct iwl4965_lq_sta), gfp);
Zhu Yib481de92007-09-25 17:54:57 -07002204
Tomas Winklerc33104f2008-01-14 17:46:21 -08002205 if (lq_sta == NULL)
Zhu Yib481de92007-09-25 17:54:57 -07002206 return NULL;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002207 lq_sta->lq.sta_id = 0xff;
Zhu Yib481de92007-09-25 17:54:57 -07002208
Zhu Yi63fddb92007-09-27 11:27:36 +08002209
Zhu Yib481de92007-09-25 17:54:57 -07002210 for (j = 0; j < LQ_SIZE; j++)
2211 for (i = 0; i < IWL_RATE_COUNT; i++)
Tomas Winklerc33104f2008-01-14 17:46:21 -08002212 rs_rate_scale_clear_window(&(lq_sta->lq_info[j].win[i]));
Zhu Yib481de92007-09-25 17:54:57 -07002213
Tomas Winklerc33104f2008-01-14 17:46:21 -08002214 return lq_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002215}
2216
2217static void rs_rate_init(void *priv_rate, void *priv_sta,
2218 struct ieee80211_local *local,
2219 struct sta_info *sta)
2220{
2221 int i, j;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002222 struct ieee80211_conf *conf = &local->hw.conf;
Johannes Berg8318d782008-01-24 19:38:38 +01002223 struct ieee80211_supported_band *sband;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002224 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002225 struct iwl4965_lq_sta *lq_sta = priv_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002226
Johannes Berg8318d782008-01-24 19:38:38 +01002227 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2228
Tomas Winklerc33104f2008-01-14 17:46:21 -08002229 lq_sta->flush_timer = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01002230 lq_sta->supp_rates = sta->supp_rates[sband->band];
2231 sta->txrate_idx = 3;
Zhu Yib481de92007-09-25 17:54:57 -07002232 for (j = 0; j < LQ_SIZE; j++)
2233 for (i = 0; i < IWL_RATE_COUNT; i++)
Tomas Winklerc33104f2008-01-14 17:46:21 -08002234 rs_rate_scale_clear_window(&(lq_sta->lq_info[j].win[i]));
Zhu Yib481de92007-09-25 17:54:57 -07002235
2236 IWL_DEBUG_RATE("rate scale global init\n");
2237 /* TODO: what is a good starting rate for STA? About middle? Maybe not
2238 * the lowest or the highest rate.. Could consider using RSSI from
2239 * previous packets? Need to have IEEE 802.1X auth succeed immediately
2240 * after assoc.. */
2241
Tomas Winklerc33104f2008-01-14 17:46:21 -08002242 lq_sta->ibss_sta_added = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002243 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
Tomas Winkler947b13a2008-04-16 16:34:48 -07002244 u8 sta_id = iwl_find_station(priv, sta->addr);
Joe Perches0795af52007-10-03 17:59:30 -07002245 DECLARE_MAC_BUF(mac);
2246
Zhu Yib481de92007-09-25 17:54:57 -07002247 /* for IBSS the call are from tasklet */
Joe Perches0795af52007-10-03 17:59:30 -07002248 IWL_DEBUG_HT("LQ: ADD station %s\n",
2249 print_mac(mac, sta->addr));
Zhu Yib481de92007-09-25 17:54:57 -07002250
2251 if (sta_id == IWL_INVALID_STATION) {
Joe Perches0795af52007-10-03 17:59:30 -07002252 IWL_DEBUG_RATE("LQ: ADD station %s\n",
2253 print_mac(mac, sta->addr));
Ron Rindjunsky67d62032007-11-26 16:14:40 +02002254 sta_id = iwl4965_add_station_flags(priv, sta->addr,
2255 0, CMD_ASYNC, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07002256 }
2257 if ((sta_id != IWL_INVALID_STATION)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002258 lq_sta->lq.sta_id = sta_id;
2259 lq_sta->lq.rs_table[0].rate_n_flags = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002260 }
2261 /* FIXME: this is w/a remove it later */
2262 priv->assoc_station_added = 1;
2263 }
2264
Ben Cahill77626352007-11-29 11:09:44 +08002265 /* Find highest tx rate supported by hardware and destination station */
Johannes Berg8318d782008-01-24 19:38:38 +01002266 for (i = 0; i < sband->n_bitrates; i++)
2267 if (sta->supp_rates[sband->band] & BIT(i))
2268 sta->txrate_idx = i;
2269
2270 sta->last_txrate_idx = sta->txrate_idx;
2271 /* WTF is with this bogus comment? A doesn't have cck rates */
Ben Cahill77626352007-11-29 11:09:44 +08002272 /* For MODE_IEEE80211A, cck rates are at end of rate table */
Johannes Berg8318d782008-01-24 19:38:38 +01002273 if (local->hw.conf.channel->band == IEEE80211_BAND_5GHZ)
2274 sta->last_txrate_idx += IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -07002275
Tomas Winklerc33104f2008-01-14 17:46:21 -08002276 lq_sta->is_dup = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002277 lq_sta->is_green = rs_use_green(priv, conf);
2278 lq_sta->active_rate = priv->active_rate;
2279 lq_sta->active_rate &= ~(0x1000);
2280 lq_sta->active_rate_basic = priv->active_rate_basic;
Johannes Berg8318d782008-01-24 19:38:38 +01002281 lq_sta->band = priv->band;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002282#ifdef CONFIG_IWL4965_HT
Ben Cahill77626352007-11-29 11:09:44 +08002283 /*
2284 * active_siso_rate mask includes 9 MBits (bit 5), and CCK (bits 0-3),
2285 * supp_rates[] does not; shift to convert format, force 9 MBits off.
2286 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002287 lq_sta->active_siso_rate =
Guy Cohenfde0db32008-04-21 15:42:01 -07002288 priv->current_ht_config.supp_mcs_set[0] << 1;
2289 lq_sta->active_siso_rate |=
2290 priv->current_ht_config.supp_mcs_set[0] & 0x1;
2291 lq_sta->active_siso_rate &= ~((u16)0x2);
2292 lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -07002293
Ben Cahill77626352007-11-29 11:09:44 +08002294 /* Same here */
Guy Cohenfde0db32008-04-21 15:42:01 -07002295 lq_sta->active_mimo2_rate =
2296 priv->current_ht_config.supp_mcs_set[1] << 1;
2297 lq_sta->active_mimo2_rate |=
2298 priv->current_ht_config.supp_mcs_set[1] & 0x1;
2299 lq_sta->active_mimo2_rate &= ~((u16)0x2);
2300 lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE;
2301
2302 lq_sta->active_mimo3_rate =
2303 priv->current_ht_config.supp_mcs_set[2] << 1;
2304 lq_sta->active_mimo3_rate |=
2305 priv->current_ht_config.supp_mcs_set[2] & 0x1;
2306 lq_sta->active_mimo3_rate &= ~((u16)0x2);
2307 lq_sta->active_mimo3_rate <<= IWL_FIRST_OFDM_RATE;
2308
2309 IWL_DEBUG_HT("SISO RATE %X MIMO2 RATE %X MIMO3 RATE %X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002310 lq_sta->active_siso_rate,
Guy Cohenfde0db32008-04-21 15:42:01 -07002311 lq_sta->active_mimo2_rate,
2312 lq_sta->active_mimo3_rate);
2313
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002314 /* as default allow aggregation for all tids */
2315 lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002316#endif /*CONFIG_IWL4965_HT*/
Zhu Yi98d7e092007-09-27 11:27:42 +08002317#ifdef CONFIG_MAC80211_DEBUGFS
Tomas Winklerc33104f2008-01-14 17:46:21 -08002318 lq_sta->drv = priv;
Zhu Yi98d7e092007-09-27 11:27:42 +08002319#endif
Zhu Yib481de92007-09-25 17:54:57 -07002320
2321 if (priv->assoc_station_added)
2322 priv->lq_mngr.lq_ready = 1;
2323
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002324 rs_initialize_lq(priv, conf, sta);
Zhu Yib481de92007-09-25 17:54:57 -07002325}
2326
Guy Cohenfde0db32008-04-21 15:42:01 -07002327static void rs_fill_link_cmd(const struct iwl_priv *priv,
2328 struct iwl4965_lq_sta *lq_sta,
2329 struct iwl4965_rate *tx_mcs,
2330 struct iwl_link_quality_cmd *lq_cmd)
Zhu Yib481de92007-09-25 17:54:57 -07002331{
2332 int index = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002333 int rate_idx;
Zhu Yi1b696de2007-09-27 11:27:41 +08002334 int repeat_rate = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002335 u8 ant_toggle_count = 0;
2336 u8 use_ht_possible = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002337 struct iwl4965_rate new_rate;
2338 struct iwl4965_scale_tbl_info tbl_type = { 0 };
Zhu Yib481de92007-09-25 17:54:57 -07002339
Ben Cahill77626352007-11-29 11:09:44 +08002340 /* Override starting rate (index 0) if needed for debug purposes */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002341 rs_dbgfs_set_mcs(lq_sta, tx_mcs, index);
Zhu Yi98d7e092007-09-27 11:27:42 +08002342
Ben Cahill77626352007-11-29 11:09:44 +08002343 /* Interpret rate_n_flags */
Johannes Berg8318d782008-01-24 19:38:38 +01002344 rs_get_tbl_info_from_mcs(tx_mcs, lq_sta->band,
Zhu Yib481de92007-09-25 17:54:57 -07002345 &tbl_type, &rate_idx);
2346
Ben Cahill77626352007-11-29 11:09:44 +08002347 /* How many times should we repeat the initial rate? */
Zhu Yib481de92007-09-25 17:54:57 -07002348 if (is_legacy(tbl_type.lq_type)) {
2349 ant_toggle_count = 1;
Zhu Yi1b696de2007-09-27 11:27:41 +08002350 repeat_rate = IWL_NUMBER_TRY;
Zhu Yib481de92007-09-25 17:54:57 -07002351 } else
Zhu Yi1b696de2007-09-27 11:27:41 +08002352 repeat_rate = IWL_HT_NUMBER_TRY;
Zhu Yib481de92007-09-25 17:54:57 -07002353
2354 lq_cmd->general_params.mimo_delimiter =
2355 is_mimo(tbl_type.lq_type) ? 1 : 0;
Ben Cahill77626352007-11-29 11:09:44 +08002356
2357 /* Fill 1st table entry (index 0) */
Zhu Yib481de92007-09-25 17:54:57 -07002358 lq_cmd->rs_table[index].rate_n_flags =
2359 cpu_to_le32(tx_mcs->rate_n_flags);
2360 new_rate.rate_n_flags = tx_mcs->rate_n_flags;
2361
Guy Cohenfde0db32008-04-21 15:42:01 -07002362 /*FIXME:RS*/
2363 if (is_mimo(tbl_type.lq_type) || (tbl_type.ant_type == ANT_A))
Ben Cahill77626352007-11-29 11:09:44 +08002364 lq_cmd->general_params.single_stream_ant_msk
2365 = LINK_QUAL_ANT_A_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002366 else
Ben Cahill77626352007-11-29 11:09:44 +08002367 lq_cmd->general_params.single_stream_ant_msk
2368 = LINK_QUAL_ANT_B_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002369
2370 index++;
Zhu Yi1b696de2007-09-27 11:27:41 +08002371 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002372
Ben Cahill77626352007-11-29 11:09:44 +08002373 /* Fill rest of rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002374 while (index < LINK_QUAL_MAX_RETRY_NUM) {
Ben Cahill77626352007-11-29 11:09:44 +08002375 /* Repeat initial/next rate.
2376 * For legacy IWL_NUMBER_TRY == 1, this loop will not execute.
2377 * For HT IWL_HT_NUMBER_TRY == 3, this executes twice. */
Zhu Yi1b696de2007-09-27 11:27:41 +08002378 while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
Zhu Yib481de92007-09-25 17:54:57 -07002379 if (is_legacy(tbl_type.lq_type)) {
2380 if (ant_toggle_count <
2381 NUM_TRY_BEFORE_ANTENNA_TOGGLE)
2382 ant_toggle_count++;
Guy Cohenfde0db32008-04-21 15:42:01 -07002383 else if (priv && rs_is_other_ant_connected(
2384 priv->hw_params.valid_tx_ant,
2385 tbl_type.ant_type)) {
Zhu Yib481de92007-09-25 17:54:57 -07002386 rs_toggle_antenna(&new_rate, &tbl_type);
2387 ant_toggle_count = 1;
2388 }
2389 }
Zhu Yi98d7e092007-09-27 11:27:42 +08002390
Ben Cahill77626352007-11-29 11:09:44 +08002391 /* Override next rate if needed for debug purposes */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002392 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Ben Cahill77626352007-11-29 11:09:44 +08002393
2394 /* Fill next table entry */
Zhu Yib481de92007-09-25 17:54:57 -07002395 lq_cmd->rs_table[index].rate_n_flags =
2396 cpu_to_le32(new_rate.rate_n_flags);
Zhu Yi1b696de2007-09-27 11:27:41 +08002397 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002398 index++;
2399 }
2400
Johannes Berg8318d782008-01-24 19:38:38 +01002401 rs_get_tbl_info_from_mcs(&new_rate, lq_sta->band, &tbl_type,
Zhu Yib481de92007-09-25 17:54:57 -07002402 &rate_idx);
2403
Ben Cahill77626352007-11-29 11:09:44 +08002404 /* Indicate to uCode which entries might be MIMO.
2405 * If initial rate was MIMO, this will finally end up
2406 * as (IWL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
Zhu Yib481de92007-09-25 17:54:57 -07002407 if (is_mimo(tbl_type.lq_type))
2408 lq_cmd->general_params.mimo_delimiter = index;
2409
Ben Cahill77626352007-11-29 11:09:44 +08002410 /* Get next rate */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002411 rs_get_lower_rate(lq_sta, &tbl_type, rate_idx,
Zhu Yi02dede02007-09-27 11:27:40 +08002412 use_ht_possible, &new_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002413
Ben Cahill77626352007-11-29 11:09:44 +08002414 /* How many times should we repeat the next rate? */
Zhu Yib481de92007-09-25 17:54:57 -07002415 if (is_legacy(tbl_type.lq_type)) {
2416 if (ant_toggle_count < NUM_TRY_BEFORE_ANTENNA_TOGGLE)
2417 ant_toggle_count++;
Guy Cohenfde0db32008-04-21 15:42:01 -07002418 else if (priv && rs_is_other_ant_connected(
2419 priv->hw_params.valid_tx_ant,
2420 tbl_type.ant_type)) {
Zhu Yib481de92007-09-25 17:54:57 -07002421 rs_toggle_antenna(&new_rate, &tbl_type);
2422 ant_toggle_count = 1;
2423 }
Zhu Yi1b696de2007-09-27 11:27:41 +08002424 repeat_rate = IWL_NUMBER_TRY;
Zhu Yib481de92007-09-25 17:54:57 -07002425 } else
Zhu Yi1b696de2007-09-27 11:27:41 +08002426 repeat_rate = IWL_HT_NUMBER_TRY;
Zhu Yib481de92007-09-25 17:54:57 -07002427
Ben Cahill77626352007-11-29 11:09:44 +08002428 /* Don't allow HT rates after next pass.
2429 * rs_get_lower_rate() will change type to LQ_A or LQ_G. */
Zhu Yib481de92007-09-25 17:54:57 -07002430 use_ht_possible = 0;
2431
Ben Cahill77626352007-11-29 11:09:44 +08002432 /* Override next rate if needed for debug purposes */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002433 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Ben Cahill77626352007-11-29 11:09:44 +08002434
2435 /* Fill next table entry */
Zhu Yib481de92007-09-25 17:54:57 -07002436 lq_cmd->rs_table[index].rate_n_flags =
2437 cpu_to_le32(new_rate.rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -07002438
2439 index++;
Zhu Yi1b696de2007-09-27 11:27:41 +08002440 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002441 }
2442
Zhu Yib481de92007-09-25 17:54:57 -07002443 lq_cmd->general_params.dual_stream_ant_msk = 3;
2444 lq_cmd->agg_params.agg_dis_start_th = 3;
2445 lq_cmd->agg_params.agg_time_limit = cpu_to_le16(4000);
Zhu Yib481de92007-09-25 17:54:57 -07002446}
2447
2448static void *rs_alloc(struct ieee80211_local *local)
2449{
2450 return local->hw.priv;
2451}
2452/* rate scale requires free function to be implemented */
2453static void rs_free(void *priv_rate)
2454{
2455 return;
2456}
2457
2458static void rs_clear(void *priv_rate)
2459{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002460 struct iwl_priv *priv = (struct iwl_priv *) priv_rate;
Zhu Yib481de92007-09-25 17:54:57 -07002461
2462 IWL_DEBUG_RATE("enter\n");
2463
2464 priv->lq_mngr.lq_ready = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002465
2466 IWL_DEBUG_RATE("leave\n");
2467}
2468
2469static void rs_free_sta(void *priv, void *priv_sta)
2470{
Tomas Winklerc33104f2008-01-14 17:46:21 -08002471 struct iwl4965_lq_sta *lq_sta = priv_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002472
2473 IWL_DEBUG_RATE("enter\n");
Tomas Winklerc33104f2008-01-14 17:46:21 -08002474 kfree(lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002475 IWL_DEBUG_RATE("leave\n");
2476}
2477
2478
Zhu Yi93dc6462007-09-27 11:27:37 +08002479#ifdef CONFIG_MAC80211_DEBUGFS
Zhu Yi5ae212c2007-09-27 11:27:38 +08002480static int open_file_generic(struct inode *inode, struct file *file)
2481{
2482 file->private_data = inode->i_private;
2483 return 0;
2484}
Tomas Winklerc33104f2008-01-14 17:46:21 -08002485static void rs_dbgfs_set_mcs(struct iwl4965_lq_sta *lq_sta,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002486 struct iwl4965_rate *mcs, int index)
Zhu Yi98d7e092007-09-27 11:27:42 +08002487{
Ron Rindjunsky4457e1a2007-10-15 14:40:56 +02002488 u32 base_rate;
2489
Johannes Berg8318d782008-01-24 19:38:38 +01002490 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Ron Rindjunsky4457e1a2007-10-15 14:40:56 +02002491 base_rate = 0x800D;
2492 else
2493 base_rate = 0x820A;
2494
Tomas Winklerc33104f2008-01-14 17:46:21 -08002495 if (lq_sta->dbg_fixed.rate_n_flags) {
Zhu Yi98d7e092007-09-27 11:27:42 +08002496 if (index < 12)
Tomas Winklerc33104f2008-01-14 17:46:21 -08002497 mcs->rate_n_flags = lq_sta->dbg_fixed.rate_n_flags;
Zhu Yi98d7e092007-09-27 11:27:42 +08002498 else
Ron Rindjunsky4457e1a2007-10-15 14:40:56 +02002499 mcs->rate_n_flags = base_rate;
Zhu Yi98d7e092007-09-27 11:27:42 +08002500 IWL_DEBUG_RATE("Fixed rate ON\n");
2501 return;
2502 }
Zhu Yi5ae212c2007-09-27 11:27:38 +08002503
Zhu Yi98d7e092007-09-27 11:27:42 +08002504 IWL_DEBUG_RATE("Fixed rate OFF\n");
2505}
2506
2507static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
2508 const char __user *user_buf, size_t count, loff_t *ppos)
2509{
Tomas Winklerc33104f2008-01-14 17:46:21 -08002510 struct iwl4965_lq_sta *lq_sta = file->private_data;
Zhu Yi98d7e092007-09-27 11:27:42 +08002511 char buf[64];
2512 int buf_size;
2513 u32 parsed_rate;
2514
2515 memset(buf, 0, sizeof(buf));
2516 buf_size = min(count, sizeof(buf) - 1);
2517 if (copy_from_user(buf, user_buf, buf_size))
2518 return -EFAULT;
2519
2520 if (sscanf(buf, "%x", &parsed_rate) == 1)
Tomas Winklerc33104f2008-01-14 17:46:21 -08002521 lq_sta->dbg_fixed.rate_n_flags = parsed_rate;
Zhu Yi98d7e092007-09-27 11:27:42 +08002522 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002523 lq_sta->dbg_fixed.rate_n_flags = 0;
Zhu Yi98d7e092007-09-27 11:27:42 +08002524
Tomas Winklerc33104f2008-01-14 17:46:21 -08002525 lq_sta->active_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
2526 lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
Guy Cohenfde0db32008-04-21 15:42:01 -07002527 lq_sta->active_mimo2_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2528 lq_sta->active_mimo3_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
Zhu Yi98d7e092007-09-27 11:27:42 +08002529
2530 IWL_DEBUG_RATE("sta_id %d rate 0x%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002531 lq_sta->lq.sta_id, lq_sta->dbg_fixed.rate_n_flags);
Zhu Yi98d7e092007-09-27 11:27:42 +08002532
Tomas Winklerc33104f2008-01-14 17:46:21 -08002533 if (lq_sta->dbg_fixed.rate_n_flags) {
Guy Cohenfde0db32008-04-21 15:42:01 -07002534 rs_fill_link_cmd(NULL, lq_sta, &lq_sta->dbg_fixed, &lq_sta->lq);
Tomas Winkler66c73db2008-04-15 16:01:40 -07002535 iwl_send_lq_cmd(lq_sta->drv, &lq_sta->lq, CMD_ASYNC);
Zhu Yi98d7e092007-09-27 11:27:42 +08002536 }
2537
2538 return count;
2539}
Zhu Yi0209dc12007-09-27 11:27:43 +08002540
Zhu Yi5ae212c2007-09-27 11:27:38 +08002541static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
2542 char __user *user_buf, size_t count, loff_t *ppos)
2543{
2544 char buff[1024];
2545 int desc = 0;
2546 int i = 0;
2547
Tomas Winklerc33104f2008-01-14 17:46:21 -08002548 struct iwl4965_lq_sta *lq_sta = file->private_data;
Zhu Yi5ae212c2007-09-27 11:27:38 +08002549
Tomas Winklerc33104f2008-01-14 17:46:21 -08002550 desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
Zhu Yi98d7e092007-09-27 11:27:42 +08002551 desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002552 lq_sta->total_failed, lq_sta->total_success,
2553 lq_sta->active_rate);
Zhu Yi98d7e092007-09-27 11:27:42 +08002554 desc += sprintf(buff+desc, "fixed rate 0x%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002555 lq_sta->dbg_fixed.rate_n_flags);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002556 desc += sprintf(buff+desc, "general:"
2557 "flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002558 lq_sta->lq.general_params.flags,
2559 lq_sta->lq.general_params.mimo_delimiter,
2560 lq_sta->lq.general_params.single_stream_ant_msk,
2561 lq_sta->lq.general_params.dual_stream_ant_msk);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002562
2563 desc += sprintf(buff+desc, "agg:"
2564 "time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002565 le16_to_cpu(lq_sta->lq.agg_params.agg_time_limit),
2566 lq_sta->lq.agg_params.agg_dis_start_th,
2567 lq_sta->lq.agg_params.agg_frame_cnt_limit);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002568
2569 desc += sprintf(buff+desc,
2570 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002571 lq_sta->lq.general_params.start_rate_index[0],
2572 lq_sta->lq.general_params.start_rate_index[1],
2573 lq_sta->lq.general_params.start_rate_index[2],
2574 lq_sta->lq.general_params.start_rate_index[3]);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002575
2576
2577 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
2578 desc += sprintf(buff+desc, " rate[%d] 0x%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002579 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags));
Zhu Yi5ae212c2007-09-27 11:27:38 +08002580
2581 return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2582}
2583
2584static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
Zhu Yi98d7e092007-09-27 11:27:42 +08002585 .write = rs_sta_dbgfs_scale_table_write,
Zhu Yi5ae212c2007-09-27 11:27:38 +08002586 .read = rs_sta_dbgfs_scale_table_read,
2587 .open = open_file_generic,
2588};
Zhu Yi0209dc12007-09-27 11:27:43 +08002589static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
2590 char __user *user_buf, size_t count, loff_t *ppos)
2591{
2592 char buff[1024];
2593 int desc = 0;
2594 int i, j;
2595
Tomas Winklerc33104f2008-01-14 17:46:21 -08002596 struct iwl4965_lq_sta *lq_sta = file->private_data;
Zhu Yi0209dc12007-09-27 11:27:43 +08002597 for (i = 0; i < LQ_SIZE; i++) {
2598 desc += sprintf(buff+desc, "%s type=%d SGI=%d FAT=%d DUP=%d\n"
2599 "rate=0x%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002600 lq_sta->active_tbl == i?"*":"x",
2601 lq_sta->lq_info[i].lq_type,
2602 lq_sta->lq_info[i].is_SGI,
2603 lq_sta->lq_info[i].is_fat,
2604 lq_sta->lq_info[i].is_dup,
2605 lq_sta->lq_info[i].current_rate.rate_n_flags);
Zhu Yi0209dc12007-09-27 11:27:43 +08002606 for (j = 0; j < IWL_RATE_COUNT; j++) {
2607 desc += sprintf(buff+desc,
Tomas Winklerc33104f2008-01-14 17:46:21 -08002608 "counter=%d success=%d %%=%d\n",
2609 lq_sta->lq_info[i].win[j].counter,
2610 lq_sta->lq_info[i].win[j].success_counter,
2611 lq_sta->lq_info[i].win[j].success_ratio);
Zhu Yi0209dc12007-09-27 11:27:43 +08002612 }
2613 }
2614 return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2615}
2616
2617static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
2618 .read = rs_sta_dbgfs_stats_table_read,
2619 .open = open_file_generic,
2620};
Zhu Yi5ae212c2007-09-27 11:27:38 +08002621
Zhu Yi93dc6462007-09-27 11:27:37 +08002622static void rs_add_debugfs(void *priv, void *priv_sta,
2623 struct dentry *dir)
2624{
Tomas Winklerc33104f2008-01-14 17:46:21 -08002625 struct iwl4965_lq_sta *lq_sta = priv_sta;
2626 lq_sta->rs_sta_dbgfs_scale_table_file =
Zhu Yi0209dc12007-09-27 11:27:43 +08002627 debugfs_create_file("rate_scale_table", 0600, dir,
Tomas Winklerc33104f2008-01-14 17:46:21 -08002628 lq_sta, &rs_sta_dbgfs_scale_table_ops);
2629 lq_sta->rs_sta_dbgfs_stats_table_file =
Zhu Yi0209dc12007-09-27 11:27:43 +08002630 debugfs_create_file("rate_stats_table", 0600, dir,
Tomas Winklerc33104f2008-01-14 17:46:21 -08002631 lq_sta, &rs_sta_dbgfs_stats_table_ops);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002632#ifdef CONFIG_IWL4965_HT
2633 lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file =
2634 debugfs_create_u8("tx_agg_tid_enable", 0600, dir,
2635 &lq_sta->tx_agg_tid_en);
2636#endif
2637
Zhu Yi93dc6462007-09-27 11:27:37 +08002638}
2639
2640static void rs_remove_debugfs(void *priv, void *priv_sta)
2641{
Tomas Winklerc33104f2008-01-14 17:46:21 -08002642 struct iwl4965_lq_sta *lq_sta = priv_sta;
2643 debugfs_remove(lq_sta->rs_sta_dbgfs_scale_table_file);
2644 debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002645#ifdef CONFIG_IWL4965_HT
2646 debugfs_remove(lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file);
2647#endif
Zhu Yi93dc6462007-09-27 11:27:37 +08002648}
2649#endif
2650
Zhu Yib481de92007-09-25 17:54:57 -07002651static struct rate_control_ops rs_ops = {
2652 .module = NULL,
2653 .name = RS_NAME,
2654 .tx_status = rs_tx_status,
2655 .get_rate = rs_get_rate,
2656 .rate_init = rs_rate_init,
2657 .clear = rs_clear,
2658 .alloc = rs_alloc,
2659 .free = rs_free,
2660 .alloc_sta = rs_alloc_sta,
2661 .free_sta = rs_free_sta,
Zhu Yi93dc6462007-09-27 11:27:37 +08002662#ifdef CONFIG_MAC80211_DEBUGFS
2663 .add_sta_debugfs = rs_add_debugfs,
2664 .remove_sta_debugfs = rs_remove_debugfs,
2665#endif
Zhu Yib481de92007-09-25 17:54:57 -07002666};
2667
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002668int iwl4965_fill_rs_info(struct ieee80211_hw *hw, char *buf, u8 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07002669{
2670 struct ieee80211_local *local = hw_to_local(hw);
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002671 struct iwl_priv *priv = hw->priv;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002672 struct iwl4965_lq_sta *lq_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002673 struct sta_info *sta;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002674 int cnt = 0, i;
Zhu Yib481de92007-09-25 17:54:57 -07002675 u32 samples = 0, success = 0, good = 0;
2676 unsigned long now = jiffies;
2677 u32 max_time = 0;
2678 u8 lq_type, antenna;
2679
Johannes Bergd0709a62008-02-25 16:27:46 +01002680 rcu_read_lock();
2681
Zhu Yib481de92007-09-25 17:54:57 -07002682 sta = sta_info_get(local, priv->stations[sta_id].sta.sta.addr);
2683 if (!sta || !sta->rate_ctrl_priv) {
Johannes Bergd0709a62008-02-25 16:27:46 +01002684 if (sta)
Zhu Yib481de92007-09-25 17:54:57 -07002685 IWL_DEBUG_RATE("leave - no private rate data!\n");
Johannes Bergd0709a62008-02-25 16:27:46 +01002686 else
Zhu Yib481de92007-09-25 17:54:57 -07002687 IWL_DEBUG_RATE("leave - no station!\n");
Johannes Bergd0709a62008-02-25 16:27:46 +01002688 rcu_read_unlock();
Zhu Yib481de92007-09-25 17:54:57 -07002689 return sprintf(buf, "station %d not found\n", sta_id);
2690 }
2691
Tomas Winklerc33104f2008-01-14 17:46:21 -08002692 lq_sta = (void *)sta->rate_ctrl_priv;
Zhu Yib481de92007-09-25 17:54:57 -07002693
Tomas Winklerc33104f2008-01-14 17:46:21 -08002694 lq_type = lq_sta->lq_info[lq_sta->active_tbl].lq_type;
Guy Cohenfde0db32008-04-21 15:42:01 -07002695 antenna = lq_sta->lq_info[lq_sta->active_tbl].ant_type;
Zhu Yib481de92007-09-25 17:54:57 -07002696
2697 if (is_legacy(lq_type))
2698 i = IWL_RATE_54M_INDEX;
2699 else
2700 i = IWL_RATE_60M_INDEX;
2701 while (1) {
2702 u64 mask;
2703 int j;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002704 int active = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002705
Tomas Winklerc33104f2008-01-14 17:46:21 -08002706 cnt +=
2707 sprintf(&buf[cnt], " %2dMbs: ", iwl4965_rates[i].ieee / 2);
Zhu Yib481de92007-09-25 17:54:57 -07002708
2709 mask = (1ULL << (IWL_RATE_MAX_WINDOW - 1));
2710 for (j = 0; j < IWL_RATE_MAX_WINDOW; j++, mask >>= 1)
Tomas Winklerc33104f2008-01-14 17:46:21 -08002711 buf[cnt++] =
2712 (lq_sta->lq_info[active].win[i].data & mask)
Zhu Yib481de92007-09-25 17:54:57 -07002713 ? '1' : '0';
2714
Tomas Winklerc33104f2008-01-14 17:46:21 -08002715 samples += lq_sta->lq_info[active].win[i].counter;
2716 good += lq_sta->lq_info[active].win[i].success_counter;
2717 success += lq_sta->lq_info[active].win[i].success_counter *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002718 iwl4965_rates[i].ieee;
Zhu Yib481de92007-09-25 17:54:57 -07002719
Tomas Winklerc33104f2008-01-14 17:46:21 -08002720 if (lq_sta->lq_info[active].win[i].stamp) {
Zhu Yib481de92007-09-25 17:54:57 -07002721 int delta =
2722 jiffies_to_msecs(now -
Tomas Winklerc33104f2008-01-14 17:46:21 -08002723 lq_sta->lq_info[active].win[i].stamp);
Zhu Yib481de92007-09-25 17:54:57 -07002724
2725 if (delta > max_time)
2726 max_time = delta;
2727
Tomas Winklerc33104f2008-01-14 17:46:21 -08002728 cnt += sprintf(&buf[cnt], "%5dms\n", delta);
Zhu Yib481de92007-09-25 17:54:57 -07002729 } else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002730 buf[cnt++] = '\n';
Zhu Yib481de92007-09-25 17:54:57 -07002731
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002732 j = iwl4965_get_prev_ieee_rate(i);
Zhu Yib481de92007-09-25 17:54:57 -07002733 if (j == i)
2734 break;
2735 i = j;
2736 }
2737
2738 /* Display the average rate of all samples taken.
2739 *
Ben Cahill77626352007-11-29 11:09:44 +08002740 * NOTE: We multiply # of samples by 2 since the IEEE measurement
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002741 * added from iwl4965_rates is actually 2X the rate */
Zhu Yib481de92007-09-25 17:54:57 -07002742 if (samples)
Tomas Winklerc33104f2008-01-14 17:46:21 -08002743 cnt += sprintf(&buf[cnt],
Zhu Yib481de92007-09-25 17:54:57 -07002744 "\nAverage rate is %3d.%02dMbs over last %4dms\n"
2745 "%3d%% success (%d good packets over %d tries)\n",
2746 success / (2 * samples), (success * 5 / samples) % 10,
2747 max_time, good * 100 / samples, good, samples);
2748 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002749 cnt += sprintf(&buf[cnt], "\nAverage rate: 0Mbs\n");
2750
2751 cnt += sprintf(&buf[cnt], "\nrate scale type %d antenna %d "
Zhu Yib481de92007-09-25 17:54:57 -07002752 "active_search %d rate index %d\n", lq_type, antenna,
Johannes Berg8318d782008-01-24 19:38:38 +01002753 lq_sta->search_better_tbl, sta->last_txrate_idx);
Zhu Yib481de92007-09-25 17:54:57 -07002754
Johannes Bergd0709a62008-02-25 16:27:46 +01002755 rcu_read_unlock();
Tomas Winklerc33104f2008-01-14 17:46:21 -08002756 return cnt;
Zhu Yib481de92007-09-25 17:54:57 -07002757}
2758
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002759void iwl4965_rate_scale_init(struct ieee80211_hw *hw, s32 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07002760{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002761 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07002762
2763 priv->lq_mngr.lq_ready = 1;
2764}
2765
Reinette Chatre897e1cf2008-03-28 16:21:09 -07002766int iwl4965_rate_control_register(void)
Zhu Yib481de92007-09-25 17:54:57 -07002767{
Reinette Chatre897e1cf2008-03-28 16:21:09 -07002768 return ieee80211_rate_control_register(&rs_ops);
Zhu Yib481de92007-09-25 17:54:57 -07002769}
2770
Reinette Chatre897e1cf2008-03-28 16:21:09 -07002771void iwl4965_rate_control_unregister(void)
Zhu Yib481de92007-09-25 17:54:57 -07002772{
2773 ieee80211_rate_control_unregister(&rs_ops);
2774}
2775