blob: 3a7f0cb710ec62633df55c6609200612c4283ffc [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>
31#include <net/ieee80211.h>
32
33#include <linux/netdevice.h>
34#include <linux/etherdevice.h>
35#include <linux/delay.h>
36
37#include <linux/workqueue.h>
38
Johannes Berg2c8dccc2008-04-08 15:14:40 -040039#include "../net/mac80211/rate.h"
Zhu Yib481de92007-09-25 17:54:57 -070040
Christoph Hellwig5d08cd12007-10-25 17:15:50 +080041#include "iwl-4965.h"
Tomas Winkler857485c2008-03-21 13:53:44 -070042#include "iwl-core.h"
Zhu Yib481de92007-09-25 17:54:57 -070043#include "iwl-helpers.h"
44
45#define RS_NAME "iwl-4965-rs"
46
47#define NUM_TRY_BEFORE_ANTENNA_TOGGLE 1
48#define IWL_NUMBER_TRY 1
49#define IWL_HT_NUMBER_TRY 3
50
Ben Cahill77626352007-11-29 11:09:44 +080051#define IWL_RATE_MAX_WINDOW 62 /* # tx in history window */
52#define IWL_RATE_MIN_FAILURE_TH 6 /* min failures to calc tpt */
53#define IWL_RATE_MIN_SUCCESS_TH 8 /* min successes to calc tpt */
54
55/* max time to accum history 2 seconds */
56#define IWL_RATE_SCALE_FLUSH_INTVL (2*HZ)
Zhu Yib481de92007-09-25 17:54:57 -070057
58static u8 rs_ht_to_legacy[] = {
59 IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
60 IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
61 IWL_RATE_6M_INDEX,
62 IWL_RATE_6M_INDEX, IWL_RATE_9M_INDEX,
63 IWL_RATE_12M_INDEX, IWL_RATE_18M_INDEX,
64 IWL_RATE_24M_INDEX, IWL_RATE_36M_INDEX,
65 IWL_RATE_48M_INDEX, IWL_RATE_54M_INDEX
66};
67
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080068struct iwl4965_rate {
Zhu Yib481de92007-09-25 17:54:57 -070069 u32 rate_n_flags;
70} __attribute__ ((packed));
71
Ben Cahill77626352007-11-29 11:09:44 +080072/**
73 * struct iwl4965_rate_scale_data -- tx success history for one rate
74 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080075struct iwl4965_rate_scale_data {
Ben Cahill77626352007-11-29 11:09:44 +080076 u64 data; /* bitmap of successful frames */
77 s32 success_counter; /* number of frames successful */
78 s32 success_ratio; /* per-cent * 128 */
79 s32 counter; /* number of frames attempted */
80 s32 average_tpt; /* success ratio * expected throughput */
Zhu Yib481de92007-09-25 17:54:57 -070081 unsigned long stamp;
82};
83
Ben Cahill77626352007-11-29 11:09:44 +080084/**
85 * struct iwl4965_scale_tbl_info -- tx params and success history for all rates
86 *
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +020087 * There are two of these in struct iwl4965_lq_sta,
Ben Cahill77626352007-11-29 11:09:44 +080088 * one for "active", and one for "search".
89 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080090struct iwl4965_scale_tbl_info {
91 enum iwl4965_table_type lq_type;
92 enum iwl4965_antenna_type antenna_type;
Ben Cahill77626352007-11-29 11:09:44 +080093 u8 is_SGI; /* 1 = short guard interval */
94 u8 is_fat; /* 1 = 40 MHz channel width */
95 u8 is_dup; /* 1 = duplicated data streams */
96 u8 action; /* change modulation; IWL_[LEGACY/SISO/MIMO]_SWITCH_* */
97 s32 *expected_tpt; /* throughput metrics; expected_tpt_G, etc. */
98 struct iwl4965_rate current_rate; /* rate_n_flags, uCode API format */
99 struct iwl4965_rate_scale_data win[IWL_RATE_COUNT]; /* rate histories */
Zhu Yib481de92007-09-25 17:54:57 -0700100};
101
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200102#ifdef CONFIG_IWL4965_HT
103
104struct iwl4965_traffic_load {
105 unsigned long time_stamp; /* age of the oldest statistics */
106 u32 packet_count[TID_QUEUE_MAX_SIZE]; /* packet count in this time
107 * slice */
108 u32 total; /* total num of packets during the
109 * last TID_MAX_TIME_DIFF */
110 u8 queue_count; /* number of queues that has
111 * been used since the last cleanup */
112 u8 head; /* start of the circular buffer */
113};
114
115#endif /* CONFIG_IWL4965_HT */
116
Ben Cahill77626352007-11-29 11:09:44 +0800117/**
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200118 * struct iwl4965_lq_sta -- driver's rate scaling private structure
Ben Cahill77626352007-11-29 11:09:44 +0800119 *
120 * Pointer to this gets passed back and forth between driver and mac80211.
121 */
Tomas Winklerc33104f2008-01-14 17:46:21 -0800122struct iwl4965_lq_sta {
Ben Cahill77626352007-11-29 11:09:44 +0800123 u8 active_tbl; /* index of active table, range 0-1 */
124 u8 enable_counter; /* indicates HT mode */
125 u8 stay_in_tbl; /* 1: disallow, 0: allow search for new mode */
126 u8 search_better_tbl; /* 1: currently trying alternate mode */
Zhu Yib481de92007-09-25 17:54:57 -0700127 s32 last_tpt;
Ben Cahill77626352007-11-29 11:09:44 +0800128
129 /* The following determine when to search for a new mode */
Zhu Yib481de92007-09-25 17:54:57 -0700130 u32 table_count_limit;
Ben Cahill77626352007-11-29 11:09:44 +0800131 u32 max_failure_limit; /* # failed frames before new search */
132 u32 max_success_limit; /* # successful frames before new search */
Zhu Yib481de92007-09-25 17:54:57 -0700133 u32 table_count;
Ben Cahill77626352007-11-29 11:09:44 +0800134 u32 total_failed; /* total failed frames, any/all rates */
135 u32 total_success; /* total successful frames, any/all rates */
136 u32 flush_timer; /* time staying in mode before new search */
137
138 u8 action_counter; /* # mode-switch actions tried */
Zhu Yib481de92007-09-25 17:54:57 -0700139 u8 antenna;
140 u8 valid_antenna;
141 u8 is_green;
142 u8 is_dup;
Johannes Berg8318d782008-01-24 19:38:38 +0100143 enum ieee80211_band band;
Zhu Yib481de92007-09-25 17:54:57 -0700144 u8 ibss_sta_added;
Ben Cahill77626352007-11-29 11:09:44 +0800145
146 /* The following are bitmaps of rates; IWL_RATE_6M_MASK, etc. */
Zhu Yi02dede02007-09-27 11:27:40 +0800147 u32 supp_rates;
Zhu Yib481de92007-09-25 17:54:57 -0700148 u16 active_rate;
149 u16 active_siso_rate;
150 u16 active_mimo_rate;
151 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;
Zhu Yi5ae212c2007-09-27 11:27:38 +0800166#endif
Zhu Yi62430652008-05-05 10:22:46 +0800167 struct iwl_priv *drv;
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);
Tomas Winklerc33104f2008-01-14 17:46:21 -0800174static void rs_fill_link_cmd(struct iwl4965_lq_sta *lq_sta,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800175 struct iwl4965_rate *tx_mcs,
Tomas Winkler66c73db2008-04-15 16:01:40 -0700176 struct iwl_link_quality_cmd *tbl);
Zhu Yib481de92007-09-25 17:54:57 -0700177
178
Zhu Yi98d7e092007-09-27 11:27:42 +0800179#ifdef CONFIG_MAC80211_DEBUGFS
Tomas Winklerc33104f2008-01-14 17:46:21 -0800180static void rs_dbgfs_set_mcs(struct iwl4965_lq_sta *lq_sta,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800181 struct iwl4965_rate *mcs, int index);
Zhu Yi98d7e092007-09-27 11:27:42 +0800182#else
Tomas Winklerc33104f2008-01-14 17:46:21 -0800183static void rs_dbgfs_set_mcs(struct iwl4965_lq_sta *lq_sta,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800184 struct iwl4965_rate *mcs, int index)
Zhu Yi98d7e092007-09-27 11:27:42 +0800185{}
186#endif
Ben Cahill77626352007-11-29 11:09:44 +0800187
188/*
189 * Expected throughput metrics for following rates:
190 * 1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
191 * "G" is the only table that supports CCK (the first 4 rates).
192 */
Zhu Yib481de92007-09-25 17:54:57 -0700193static s32 expected_tpt_A[IWL_RATE_COUNT] = {
194 0, 0, 0, 0, 40, 57, 72, 98, 121, 154, 177, 186, 186
195};
196
197static s32 expected_tpt_G[IWL_RATE_COUNT] = {
198 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 186
199};
200
201static s32 expected_tpt_siso20MHz[IWL_RATE_COUNT] = {
202 0, 0, 0, 0, 42, 42, 76, 102, 124, 159, 183, 193, 202
203};
204
205static s32 expected_tpt_siso20MHzSGI[IWL_RATE_COUNT] = {
206 0, 0, 0, 0, 46, 46, 82, 110, 132, 168, 192, 202, 211
207};
208
209static s32 expected_tpt_mimo20MHz[IWL_RATE_COUNT] = {
210 0, 0, 0, 0, 74, 74, 123, 155, 179, 214, 236, 244, 251
211};
212
213static s32 expected_tpt_mimo20MHzSGI[IWL_RATE_COUNT] = {
214 0, 0, 0, 0, 81, 81, 131, 164, 188, 222, 243, 251, 257
215};
216
217static s32 expected_tpt_siso40MHz[IWL_RATE_COUNT] = {
218 0, 0, 0, 0, 77, 77, 127, 160, 184, 220, 242, 250, 257
219};
220
221static s32 expected_tpt_siso40MHzSGI[IWL_RATE_COUNT] = {
222 0, 0, 0, 0, 83, 83, 135, 169, 193, 229, 250, 257, 264
223};
224
225static s32 expected_tpt_mimo40MHz[IWL_RATE_COUNT] = {
226 0, 0, 0, 0, 123, 123, 182, 214, 235, 264, 279, 285, 289
227};
228
229static s32 expected_tpt_mimo40MHzSGI[IWL_RATE_COUNT] = {
230 0, 0, 0, 0, 131, 131, 191, 222, 242, 270, 284, 289, 293
231};
232
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800233static inline u8 iwl4965_rate_get_rate(u32 rate_n_flags)
Zhu Yib481de92007-09-25 17:54:57 -0700234{
235 return (u8)(rate_n_flags & 0xFF);
236}
237
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800238static void rs_rate_scale_clear_window(struct iwl4965_rate_scale_data *window)
Zhu Yib481de92007-09-25 17:54:57 -0700239{
240 window->data = 0;
241 window->success_counter = 0;
242 window->success_ratio = IWL_INVALID_VALUE;
243 window->counter = 0;
244 window->average_tpt = IWL_INVALID_VALUE;
245 window->stamp = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700246}
247
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200248#ifdef CONFIG_IWL4965_HT
249/*
250 * removes the old data from the statistics. All data that is older than
251 * TID_MAX_TIME_DIFF, will be deleted.
252 */
253static void rs_tl_rm_old_stats(struct iwl4965_traffic_load *tl, u32 curr_time)
254{
255 /* The oldest age we want to keep */
256 u32 oldest_time = curr_time - TID_MAX_TIME_DIFF;
257
258 while (tl->queue_count &&
259 (tl->time_stamp < oldest_time)) {
260 tl->total -= tl->packet_count[tl->head];
261 tl->packet_count[tl->head] = 0;
262 tl->time_stamp += TID_QUEUE_CELL_SPACING;
263 tl->queue_count--;
264 tl->head++;
265 if (tl->head >= TID_QUEUE_MAX_SIZE)
266 tl->head = 0;
267 }
268}
269
270/*
271 * increment traffic load value for tid and also remove
272 * any old values if passed the certain time period
273 */
274static void rs_tl_add_packet(struct iwl4965_lq_sta *lq_data, u8 tid)
275{
276 u32 curr_time = jiffies_to_msecs(jiffies);
277 u32 time_diff;
278 s32 index;
279 struct iwl4965_traffic_load *tl = NULL;
280
281 if (tid >= TID_MAX_LOAD_COUNT)
282 return;
283
284 tl = &lq_data->load[tid];
285
286 curr_time -= curr_time % TID_ROUND_VALUE;
287
288 /* Happens only for the first packet. Initialize the data */
289 if (!(tl->queue_count)) {
290 tl->total = 1;
291 tl->time_stamp = curr_time;
292 tl->queue_count = 1;
293 tl->head = 0;
294 tl->packet_count[0] = 1;
295 return;
296 }
297
298 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
299 index = time_diff / TID_QUEUE_CELL_SPACING;
300
301 /* The history is too long: remove data that is older than */
302 /* TID_MAX_TIME_DIFF */
303 if (index >= TID_QUEUE_MAX_SIZE)
304 rs_tl_rm_old_stats(tl, curr_time);
305
306 index = (tl->head + index) % TID_QUEUE_MAX_SIZE;
307 tl->packet_count[index] = tl->packet_count[index] + 1;
308 tl->total = tl->total + 1;
309
310 if ((index + 1) > tl->queue_count)
311 tl->queue_count = index + 1;
312}
313
314/*
315 get the traffic load value for tid
316*/
317static u32 rs_tl_get_load(struct iwl4965_lq_sta *lq_data, u8 tid)
318{
319 u32 curr_time = jiffies_to_msecs(jiffies);
320 u32 time_diff;
321 s32 index;
322 struct iwl4965_traffic_load *tl = NULL;
323
324 if (tid >= TID_MAX_LOAD_COUNT)
325 return 0;
326
327 tl = &(lq_data->load[tid]);
328
329 curr_time -= curr_time % TID_ROUND_VALUE;
330
331 if (!(tl->queue_count))
332 return 0;
333
334 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
335 index = time_diff / TID_QUEUE_CELL_SPACING;
336
337 /* The history is too long: remove data that is older than */
338 /* TID_MAX_TIME_DIFF */
339 if (index >= TID_QUEUE_MAX_SIZE)
340 rs_tl_rm_old_stats(tl, curr_time);
341
342 return tl->total;
343}
344
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700345static void rs_tl_turn_on_agg_for_tid(struct iwl_priv *priv,
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200346 struct iwl4965_lq_sta *lq_data, u8 tid,
347 struct sta_info *sta)
348{
349 unsigned long state;
350 DECLARE_MAC_BUF(mac);
351
352 spin_lock_bh(&sta->ampdu_mlme.ampdu_tx);
Ron Rindjunskycee24a32008-03-26 20:36:03 +0200353 state = sta->ampdu_mlme.tid_state_tx[tid];
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200354 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
355
356 if (state == HT_AGG_STATE_IDLE &&
357 rs_tl_get_load(lq_data, tid) > IWL_AGG_LOAD_THRESHOLD) {
358 IWL_DEBUG_HT("Starting Tx agg: STA: %s tid: %d\n",
359 print_mac(mac, sta->addr), tid);
360 ieee80211_start_tx_ba_session(priv->hw, sta->addr, tid);
361 }
362}
363
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700364static void rs_tl_turn_on_agg(struct iwl_priv *priv, u8 tid,
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200365 struct iwl4965_lq_sta *lq_data,
366 struct sta_info *sta)
367{
368 if ((tid < TID_MAX_LOAD_COUNT))
369 rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
370 else if (tid == IWL_AGG_ALL_TID)
371 for (tid = 0; tid < TID_MAX_LOAD_COUNT; tid++)
372 rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
373}
374
375#endif /* CONFIG_IWLWIFI_HT */
376
Ben Cahill77626352007-11-29 11:09:44 +0800377/**
378 * rs_collect_tx_data - Update the success/failure sliding window
379 *
380 * We keep a sliding window of the last 62 packets transmitted
381 * at this rate. window->data contains the bitmask of successful
382 * packets.
383 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800384static int rs_collect_tx_data(struct iwl4965_rate_scale_data *windows,
Ron Rindjunsky99556432008-01-28 14:07:25 +0200385 int scale_index, s32 tpt, int retries,
386 int successes)
Zhu Yib481de92007-09-25 17:54:57 -0700387{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800388 struct iwl4965_rate_scale_data *window = NULL;
Zhu Yib481de92007-09-25 17:54:57 -0700389 u64 mask;
390 u8 win_size = IWL_RATE_MAX_WINDOW;
391 s32 fail_count;
392
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800393 if (scale_index < 0 || scale_index >= IWL_RATE_COUNT)
394 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -0700395
Ben Cahill77626352007-11-29 11:09:44 +0800396 /* Select data for current tx bit rate */
Zhu Yib481de92007-09-25 17:54:57 -0700397 window = &(windows[scale_index]);
398
Ben Cahill77626352007-11-29 11:09:44 +0800399 /*
400 * Keep track of only the latest 62 tx frame attempts in this rate's
401 * history window; anything older isn't really relevant any more.
402 * If we have filled up the sliding window, drop the oldest attempt;
403 * if the oldest attempt (highest bit in bitmap) shows "success",
404 * subtract "1" from the success counter (this is the main reason
405 * we keep these bitmaps!).
406 */
Ron Rindjunsky99556432008-01-28 14:07:25 +0200407 while (retries > 0) {
408 if (window->counter >= win_size) {
409 window->counter = win_size - 1;
410 mask = 1;
411 mask = (mask << (win_size - 1));
412 if (window->data & mask) {
413 window->data &= ~mask;
414 window->success_counter =
415 window->success_counter - 1;
416 }
Zhu Yib481de92007-09-25 17:54:57 -0700417 }
Zhu Yib481de92007-09-25 17:54:57 -0700418
Ron Rindjunsky99556432008-01-28 14:07:25 +0200419 /* Increment frames-attempted counter */
420 window->counter++;
Ben Cahill77626352007-11-29 11:09:44 +0800421
Ron Rindjunsky99556432008-01-28 14:07:25 +0200422 /* Shift bitmap by one frame (throw away oldest history),
423 * OR in "1", and increment "success" if this
424 * frame was successful. */
425 mask = window->data;
426 window->data = (mask << 1);
427 if (successes > 0) {
428 window->success_counter = window->success_counter + 1;
429 window->data |= 0x1;
430 successes--;
431 }
432
433 retries--;
Zhu Yib481de92007-09-25 17:54:57 -0700434 }
435
Ben Cahill77626352007-11-29 11:09:44 +0800436 /* Calculate current success ratio, avoid divide-by-0! */
Zhu Yib481de92007-09-25 17:54:57 -0700437 if (window->counter > 0)
438 window->success_ratio = 128 * (100 * window->success_counter)
439 / window->counter;
440 else
441 window->success_ratio = IWL_INVALID_VALUE;
442
443 fail_count = window->counter - window->success_counter;
444
Ben Cahill77626352007-11-29 11:09:44 +0800445 /* Calculate average throughput, if we have enough history. */
Zhu Yib481de92007-09-25 17:54:57 -0700446 if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
447 (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
448 window->average_tpt = (window->success_ratio * tpt + 64) / 128;
449 else
450 window->average_tpt = IWL_INVALID_VALUE;
451
Ben Cahill77626352007-11-29 11:09:44 +0800452 /* Tag this window as having been updated */
Zhu Yib481de92007-09-25 17:54:57 -0700453 window->stamp = jiffies;
454
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800455 return 0;
Zhu Yib481de92007-09-25 17:54:57 -0700456}
457
Ben Cahill77626352007-11-29 11:09:44 +0800458/*
459 * Fill uCode API rate_n_flags field, based on "search" or "active" table.
460 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800461static void rs_mcs_from_tbl(struct iwl4965_rate *mcs_rate,
462 struct iwl4965_scale_tbl_info *tbl,
Zhu Yib481de92007-09-25 17:54:57 -0700463 int index, u8 use_green)
464{
Zhu Yib481de92007-09-25 17:54:57 -0700465 if (is_legacy(tbl->lq_type)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800466 mcs_rate->rate_n_flags = iwl4965_rates[index].plcp;
Zhu Yib481de92007-09-25 17:54:57 -0700467 if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
468 mcs_rate->rate_n_flags |= RATE_MCS_CCK_MSK;
469
470 } else if (is_siso(tbl->lq_type)) {
471 if (index > IWL_LAST_OFDM_RATE)
472 index = IWL_LAST_OFDM_RATE;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800473 mcs_rate->rate_n_flags = iwl4965_rates[index].plcp_siso |
Zhu Yib481de92007-09-25 17:54:57 -0700474 RATE_MCS_HT_MSK;
475 } else {
476 if (index > IWL_LAST_OFDM_RATE)
477 index = IWL_LAST_OFDM_RATE;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800478 mcs_rate->rate_n_flags = iwl4965_rates[index].plcp_mimo |
Zhu Yib481de92007-09-25 17:54:57 -0700479 RATE_MCS_HT_MSK;
480 }
481
482 switch (tbl->antenna_type) {
483 case ANT_BOTH:
484 mcs_rate->rate_n_flags |= RATE_MCS_ANT_AB_MSK;
485 break;
486 case ANT_MAIN:
487 mcs_rate->rate_n_flags |= RATE_MCS_ANT_A_MSK;
488 break;
489 case ANT_AUX:
490 mcs_rate->rate_n_flags |= RATE_MCS_ANT_B_MSK;
491 break;
492 case ANT_NONE:
493 break;
494 }
495
496 if (is_legacy(tbl->lq_type))
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800497 return;
Zhu Yib481de92007-09-25 17:54:57 -0700498
499 if (tbl->is_fat) {
500 if (tbl->is_dup)
501 mcs_rate->rate_n_flags |= RATE_MCS_DUP_MSK;
502 else
503 mcs_rate->rate_n_flags |= RATE_MCS_FAT_MSK;
504 }
505 if (tbl->is_SGI)
506 mcs_rate->rate_n_flags |= RATE_MCS_SGI_MSK;
507
508 if (use_green) {
509 mcs_rate->rate_n_flags |= RATE_MCS_GF_MSK;
510 if (is_siso(tbl->lq_type))
511 mcs_rate->rate_n_flags &= ~RATE_MCS_SGI_MSK;
512 }
Zhu Yib481de92007-09-25 17:54:57 -0700513}
514
Ben Cahill77626352007-11-29 11:09:44 +0800515/*
516 * Interpret uCode API's rate_n_flags format,
517 * fill "search" or "active" tx mode table.
518 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800519static int rs_get_tbl_info_from_mcs(const struct iwl4965_rate *mcs_rate,
Johannes Berg8318d782008-01-24 19:38:38 +0100520 enum ieee80211_band band,
521 struct iwl4965_scale_tbl_info *tbl,
Zhu Yib481de92007-09-25 17:54:57 -0700522 int *rate_idx)
523{
524 int index;
525 u32 ant_msk;
526
Tomas Winkler17744ff2008-03-02 01:52:00 +0200527 index = iwl4965_hwrate_to_plcp_idx(mcs_rate->rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -0700528
529 if (index == IWL_RATE_INVALID) {
530 *rate_idx = -1;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800531 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -0700532 }
Ben Cahill77626352007-11-29 11:09:44 +0800533 tbl->is_SGI = 0; /* default legacy setup */
Zhu Yib481de92007-09-25 17:54:57 -0700534 tbl->is_fat = 0;
535 tbl->is_dup = 0;
Ben Cahill77626352007-11-29 11:09:44 +0800536 tbl->antenna_type = ANT_BOTH; /* default MIMO setup */
Zhu Yib481de92007-09-25 17:54:57 -0700537
Ben Cahill77626352007-11-29 11:09:44 +0800538 /* legacy rate format */
Zhu Yib481de92007-09-25 17:54:57 -0700539 if (!(mcs_rate->rate_n_flags & RATE_MCS_HT_MSK)) {
540 ant_msk = (mcs_rate->rate_n_flags & RATE_MCS_ANT_AB_MSK);
541
542 if (ant_msk == RATE_MCS_ANT_AB_MSK)
543 tbl->lq_type = LQ_NONE;
544 else {
545
Johannes Berg8318d782008-01-24 19:38:38 +0100546 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700547 tbl->lq_type = LQ_A;
548 else
549 tbl->lq_type = LQ_G;
550
551 if (mcs_rate->rate_n_flags & RATE_MCS_ANT_A_MSK)
552 tbl->antenna_type = ANT_MAIN;
553 else
554 tbl->antenna_type = ANT_AUX;
555 }
556 *rate_idx = index;
557
Ben Cahill77626352007-11-29 11:09:44 +0800558 /* HT rate format, SISO (might be 20 MHz legacy or 40 MHz fat width) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800559 } else if (iwl4965_rate_get_rate(mcs_rate->rate_n_flags)
Zhu Yib481de92007-09-25 17:54:57 -0700560 <= IWL_RATE_SISO_60M_PLCP) {
561 tbl->lq_type = LQ_SISO;
562
563 ant_msk = (mcs_rate->rate_n_flags & RATE_MCS_ANT_AB_MSK);
564 if (ant_msk == RATE_MCS_ANT_AB_MSK)
565 tbl->lq_type = LQ_NONE;
566 else {
567 if (mcs_rate->rate_n_flags & RATE_MCS_ANT_A_MSK)
568 tbl->antenna_type = ANT_MAIN;
569 else
570 tbl->antenna_type = ANT_AUX;
571 }
572 if (mcs_rate->rate_n_flags & RATE_MCS_SGI_MSK)
573 tbl->is_SGI = 1;
574
575 if ((mcs_rate->rate_n_flags & RATE_MCS_FAT_MSK) ||
576 (mcs_rate->rate_n_flags & RATE_MCS_DUP_MSK))
577 tbl->is_fat = 1;
578
579 if (mcs_rate->rate_n_flags & RATE_MCS_DUP_MSK)
580 tbl->is_dup = 1;
581
582 *rate_idx = index;
Ben Cahill77626352007-11-29 11:09:44 +0800583
584 /* HT rate format, MIMO (might be 20 MHz legacy or 40 MHz fat width) */
Zhu Yib481de92007-09-25 17:54:57 -0700585 } else {
586 tbl->lq_type = LQ_MIMO;
587 if (mcs_rate->rate_n_flags & RATE_MCS_SGI_MSK)
588 tbl->is_SGI = 1;
589
590 if ((mcs_rate->rate_n_flags & RATE_MCS_FAT_MSK) ||
591 (mcs_rate->rate_n_flags & RATE_MCS_DUP_MSK))
592 tbl->is_fat = 1;
593
594 if (mcs_rate->rate_n_flags & RATE_MCS_DUP_MSK)
595 tbl->is_dup = 1;
596 *rate_idx = index;
597 }
598 return 0;
599}
600
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800601static inline void rs_toggle_antenna(struct iwl4965_rate *new_rate,
602 struct iwl4965_scale_tbl_info *tbl)
Zhu Yib481de92007-09-25 17:54:57 -0700603{
604 if (tbl->antenna_type == ANT_AUX) {
605 tbl->antenna_type = ANT_MAIN;
606 new_rate->rate_n_flags &= ~RATE_MCS_ANT_B_MSK;
607 new_rate->rate_n_flags |= RATE_MCS_ANT_A_MSK;
608 } else {
609 tbl->antenna_type = ANT_AUX;
610 new_rate->rate_n_flags &= ~RATE_MCS_ANT_A_MSK;
611 new_rate->rate_n_flags |= RATE_MCS_ANT_B_MSK;
612 }
613}
614
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700615static inline u8 rs_use_green(struct iwl_priv *priv,
Ron Rindjunsky270243a2007-11-26 16:14:41 +0200616 struct ieee80211_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -0700617{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800618#ifdef CONFIG_IWL4965_HT
Ron Rindjunsky270243a2007-11-26 16:14:41 +0200619 return ((conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) &&
620 priv->current_ht_config.is_green_field &&
621 !priv->current_ht_config.non_GF_STA_present);
622#endif /* CONFIG_IWL4965_HT */
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800623 return 0;
Zhu Yib481de92007-09-25 17:54:57 -0700624}
625
626/**
627 * rs_get_supported_rates - get the available rates
628 *
629 * if management frame or broadcast frame only return
630 * basic available rates.
631 *
632 */
Tomas Winklerc33104f2008-01-14 17:46:21 -0800633static void rs_get_supported_rates(struct iwl4965_lq_sta *lq_sta,
Zhu Yib481de92007-09-25 17:54:57 -0700634 struct ieee80211_hdr *hdr,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800635 enum iwl4965_table_type rate_type,
Zhu Yib481de92007-09-25 17:54:57 -0700636 u16 *data_rate)
637{
638 if (is_legacy(rate_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -0800639 *data_rate = lq_sta->active_rate;
Zhu Yib481de92007-09-25 17:54:57 -0700640 else {
641 if (is_siso(rate_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -0800642 *data_rate = lq_sta->active_siso_rate;
Zhu Yib481de92007-09-25 17:54:57 -0700643 else
Tomas Winklerc33104f2008-01-14 17:46:21 -0800644 *data_rate = lq_sta->active_mimo_rate;
Zhu Yib481de92007-09-25 17:54:57 -0700645 }
646
647 if (hdr && is_multicast_ether_addr(hdr->addr1) &&
Tomas Winklerc33104f2008-01-14 17:46:21 -0800648 lq_sta->active_rate_basic) {
649 *data_rate = lq_sta->active_rate_basic;
650 }
Zhu Yib481de92007-09-25 17:54:57 -0700651}
652
653static u16 rs_get_adjacent_rate(u8 index, u16 rate_mask, int rate_type)
654{
655 u8 high = IWL_RATE_INVALID;
656 u8 low = IWL_RATE_INVALID;
657
Ian Schram01ebd062007-10-25 17:15:22 +0800658 /* 802.11A or ht walks to the next literal adjacent rate in
Zhu Yib481de92007-09-25 17:54:57 -0700659 * the rate table */
660 if (is_a_band(rate_type) || !is_legacy(rate_type)) {
661 int i;
662 u32 mask;
663
664 /* Find the previous rate that is in the rate mask */
665 i = index - 1;
666 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
667 if (rate_mask & mask) {
668 low = i;
669 break;
670 }
671 }
672
673 /* Find the next rate that is in the rate mask */
674 i = index + 1;
675 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
676 if (rate_mask & mask) {
677 high = i;
678 break;
679 }
680 }
681
682 return (high << 8) | low;
683 }
684
685 low = index;
686 while (low != IWL_RATE_INVALID) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800687 low = iwl4965_rates[low].prev_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700688 if (low == IWL_RATE_INVALID)
689 break;
690 if (rate_mask & (1 << low))
691 break;
692 IWL_DEBUG_RATE("Skipping masked lower rate: %d\n", low);
693 }
694
695 high = index;
696 while (high != IWL_RATE_INVALID) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800697 high = iwl4965_rates[high].next_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700698 if (high == IWL_RATE_INVALID)
699 break;
700 if (rate_mask & (1 << high))
701 break;
702 IWL_DEBUG_RATE("Skipping masked higher rate: %d\n", high);
703 }
704
705 return (high << 8) | low;
706}
707
Tomas Winklerc33104f2008-01-14 17:46:21 -0800708static void rs_get_lower_rate(struct iwl4965_lq_sta *lq_sta,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800709 struct iwl4965_scale_tbl_info *tbl, u8 scale_index,
710 u8 ht_possible, struct iwl4965_rate *mcs_rate)
Zhu Yib481de92007-09-25 17:54:57 -0700711{
Zhu Yib481de92007-09-25 17:54:57 -0700712 s32 low;
713 u16 rate_mask;
714 u16 high_low;
715 u8 switch_to_legacy = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -0800716 u8 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -0700717
718 /* check if we need to switch from HT to legacy rates.
719 * assumption is that mandatory rates (1Mbps or 6Mbps)
720 * are always supported (spec demand) */
721 if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_index)) {
722 switch_to_legacy = 1;
723 scale_index = rs_ht_to_legacy[scale_index];
Johannes Berg8318d782008-01-24 19:38:38 +0100724 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700725 tbl->lq_type = LQ_A;
726 else
727 tbl->lq_type = LQ_G;
728
729 if ((tbl->antenna_type == ANT_BOTH) ||
730 (tbl->antenna_type == ANT_NONE))
731 tbl->antenna_type = ANT_MAIN;
732
733 tbl->is_fat = 0;
734 tbl->is_SGI = 0;
735 }
736
Tomas Winklerc33104f2008-01-14 17:46:21 -0800737 rs_get_supported_rates(lq_sta, NULL, tbl->lq_type, &rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -0700738
Ben Cahill77626352007-11-29 11:09:44 +0800739 /* Mask with station rate restriction */
Zhu Yib481de92007-09-25 17:54:57 -0700740 if (is_legacy(tbl->lq_type)) {
Ben Cahill77626352007-11-29 11:09:44 +0800741 /* supp_rates has no CCK bits in A mode */
Johannes Berg8318d782008-01-24 19:38:38 +0100742 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700743 rate_mask = (u16)(rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -0800744 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
Zhu Yib481de92007-09-25 17:54:57 -0700745 else
Tomas Winklerc33104f2008-01-14 17:46:21 -0800746 rate_mask = (u16)(rate_mask & lq_sta->supp_rates);
Zhu Yib481de92007-09-25 17:54:57 -0700747 }
748
Ben Cahill77626352007-11-29 11:09:44 +0800749 /* If we switched from HT to legacy, check current rate */
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800750 if (switch_to_legacy && (rate_mask & (1 << scale_index))) {
Zhu Yib481de92007-09-25 17:54:57 -0700751 rs_mcs_from_tbl(mcs_rate, tbl, scale_index, is_green);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800752 return;
Zhu Yib481de92007-09-25 17:54:57 -0700753 }
754
755 high_low = rs_get_adjacent_rate(scale_index, rate_mask, tbl->lq_type);
756 low = high_low & 0xff;
757
758 if (low != IWL_RATE_INVALID)
759 rs_mcs_from_tbl(mcs_rate, tbl, low, is_green);
760 else
761 rs_mcs_from_tbl(mcs_rate, tbl, scale_index, is_green);
Zhu Yib481de92007-09-25 17:54:57 -0700762}
763
Ben Cahill77626352007-11-29 11:09:44 +0800764/*
765 * mac80211 sends us Tx status
766 */
Tomas Winklerc33104f2008-01-14 17:46:21 -0800767static void rs_tx_status(void *priv_rate, struct net_device *dev,
Zhu Yib481de92007-09-25 17:54:57 -0700768 struct sk_buff *skb,
769 struct ieee80211_tx_status *tx_resp)
770{
771 int status;
772 u8 retries;
773 int rs_index, index = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -0800774 struct iwl4965_lq_sta *lq_sta;
Tomas Winkler66c73db2008-04-15 16:01:40 -0700775 struct iwl_link_quality_cmd *table;
Zhu Yib481de92007-09-25 17:54:57 -0700776 struct sta_info *sta;
777 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700778 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
Zhu Yib481de92007-09-25 17:54:57 -0700779 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800780 struct ieee80211_hw *hw = local_to_hw(local);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800781 struct iwl4965_rate_scale_data *window = NULL;
782 struct iwl4965_rate_scale_data *search_win = NULL;
783 struct iwl4965_rate tx_mcs;
784 struct iwl4965_scale_tbl_info tbl_type;
785 struct iwl4965_scale_tbl_info *curr_tbl, *search_tbl;
Zhu Yib481de92007-09-25 17:54:57 -0700786 u8 active_index = 0;
787 u16 fc = le16_to_cpu(hdr->frame_control);
788 s32 tpt = 0;
789
Zhu Yi58826352007-09-27 11:27:39 +0800790 IWL_DEBUG_RATE_LIMIT("get frame ack response, update rate scale window\n");
Zhu Yib481de92007-09-25 17:54:57 -0700791
792 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1))
793 return;
794
Ron Rindjunsky99556432008-01-28 14:07:25 +0200795 /* This packet was aggregated but doesn't carry rate scale info */
796 if ((tx_resp->control.flags & IEEE80211_TXCTL_AMPDU) &&
797 !(tx_resp->flags & IEEE80211_TX_STATUS_AMPDU))
798 return;
799
Zhu Yib481de92007-09-25 17:54:57 -0700800 retries = tx_resp->retry_count;
801
802 if (retries > 15)
803 retries = 15;
804
Johannes Bergd0709a62008-02-25 16:27:46 +0100805 rcu_read_lock();
Zhu Yib481de92007-09-25 17:54:57 -0700806
807 sta = sta_info_get(local, hdr->addr1);
808
Tomas Winklerf4d60822008-03-05 11:31:00 -0800809 if (!sta || !sta->rate_ctrl_priv)
810 goto out;
811
Zhu Yib481de92007-09-25 17:54:57 -0700812
Tomas Winklerc33104f2008-01-14 17:46:21 -0800813 lq_sta = (struct iwl4965_lq_sta *)sta->rate_ctrl_priv;
Zhu Yib481de92007-09-25 17:54:57 -0700814
815 if (!priv->lq_mngr.lq_ready)
Tomas Winklerf4d60822008-03-05 11:31:00 -0800816 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700817
Tomas Winklerc33104f2008-01-14 17:46:21 -0800818 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
819 !lq_sta->ibss_sta_added)
Tomas Winklerf4d60822008-03-05 11:31:00 -0800820 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700821
Tomas Winklerc33104f2008-01-14 17:46:21 -0800822 table = &lq_sta->lq;
823 active_index = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -0700824
Ben Cahill77626352007-11-29 11:09:44 +0800825 /* Get mac80211 antenna info */
Tomas Winklerc33104f2008-01-14 17:46:21 -0800826 lq_sta->antenna =
827 (lq_sta->valid_antenna & local->hw.conf.antenna_sel_tx);
828 if (!lq_sta->antenna)
829 lq_sta->antenna = lq_sta->valid_antenna;
Zhu Yib481de92007-09-25 17:54:57 -0700830
Ben Cahill77626352007-11-29 11:09:44 +0800831 /* Ignore mac80211 antenna info for now */
Tomas Winklerc33104f2008-01-14 17:46:21 -0800832 lq_sta->antenna = lq_sta->valid_antenna;
Ben Cahill77626352007-11-29 11:09:44 +0800833
Tomas Winklerc33104f2008-01-14 17:46:21 -0800834 curr_tbl = &(lq_sta->lq_info[active_index]);
835 search_tbl = &(lq_sta->lq_info[(1 - active_index)]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800836 window = (struct iwl4965_rate_scale_data *)
Zhu Yib481de92007-09-25 17:54:57 -0700837 &(curr_tbl->win[0]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800838 search_win = (struct iwl4965_rate_scale_data *)
Zhu Yib481de92007-09-25 17:54:57 -0700839 &(search_tbl->win[0]);
840
Ben Cahill77626352007-11-29 11:09:44 +0800841 /*
842 * Ignore this Tx frame response if its initial rate doesn't match
843 * that of latest Link Quality command. There may be stragglers
844 * from a previous Link Quality command, but we're no longer interested
845 * in those; they're either from the "active" mode while we're trying
846 * to check "search" mode, or a prior "search" mode after we've moved
847 * to a new "search" mode (which might become the new "active" mode).
848 */
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800849 tx_mcs.rate_n_flags = le32_to_cpu(table->rs_table[0].rate_n_flags);
850 rs_get_tbl_info_from_mcs(&tx_mcs, priv->band, &tbl_type, &rs_index);
851 if (priv->band == IEEE80211_BAND_5GHZ)
852 rs_index -= IWL_FIRST_OFDM_RATE;
853
854 if ((tx_resp->control.tx_rate == NULL) ||
855 (tbl_type.is_SGI ^
856 !!(tx_resp->control.flags & IEEE80211_TXCTL_SHORT_GI)) ||
857 (tbl_type.is_fat ^
858 !!(tx_resp->control.flags & IEEE80211_TXCTL_40_MHZ_WIDTH)) ||
859 (tbl_type.is_dup ^
860 !!(tx_resp->control.flags & IEEE80211_TXCTL_DUP_DATA)) ||
861 (tbl_type.antenna_type ^
862 tx_resp->control.antenna_sel_tx) ||
863 (!!(tx_mcs.rate_n_flags & RATE_MCS_HT_MSK) ^
864 !!(tx_resp->control.flags & IEEE80211_TXCTL_OFDM_HT)) ||
865 (!!(tx_mcs.rate_n_flags & RATE_MCS_GF_MSK) ^
866 !!(tx_resp->control.flags & IEEE80211_TXCTL_GREEN_FIELD)) ||
867 (hw->wiphy->bands[priv->band]->bitrates[rs_index].bitrate !=
868 tx_resp->control.tx_rate->bitrate)) {
869 IWL_DEBUG_RATE("initial rate does not match 0x%x\n",
870 tx_mcs.rate_n_flags);
Tomas Winklerf4d60822008-03-05 11:31:00 -0800871 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700872 }
873
Ben Cahill77626352007-11-29 11:09:44 +0800874 /* Update frame history window with "failure" for each Tx retry. */
Zhu Yib481de92007-09-25 17:54:57 -0700875 while (retries) {
Ben Cahill77626352007-11-29 11:09:44 +0800876 /* Look up the rate and other info used for each tx attempt.
877 * Each tx attempt steps one entry deeper in the rate table. */
Zhu Yib481de92007-09-25 17:54:57 -0700878 tx_mcs.rate_n_flags =
879 le32_to_cpu(table->rs_table[index].rate_n_flags);
Johannes Berg8318d782008-01-24 19:38:38 +0100880 rs_get_tbl_info_from_mcs(&tx_mcs, priv->band,
Zhu Yib481de92007-09-25 17:54:57 -0700881 &tbl_type, &rs_index);
882
Ben Cahill77626352007-11-29 11:09:44 +0800883 /* If type matches "search" table,
884 * add failure to "search" history */
Zhu Yib481de92007-09-25 17:54:57 -0700885 if ((tbl_type.lq_type == search_tbl->lq_type) &&
886 (tbl_type.antenna_type == search_tbl->antenna_type) &&
887 (tbl_type.is_SGI == search_tbl->is_SGI)) {
888 if (search_tbl->expected_tpt)
889 tpt = search_tbl->expected_tpt[rs_index];
890 else
891 tpt = 0;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200892 rs_collect_tx_data(search_win, rs_index, tpt, 1, 0);
Ben Cahill77626352007-11-29 11:09:44 +0800893
894 /* Else if type matches "current/active" table,
895 * add failure to "current/active" history */
Zhu Yib481de92007-09-25 17:54:57 -0700896 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
897 (tbl_type.antenna_type == curr_tbl->antenna_type) &&
898 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
899 if (curr_tbl->expected_tpt)
900 tpt = curr_tbl->expected_tpt[rs_index];
901 else
902 tpt = 0;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200903 rs_collect_tx_data(window, rs_index, tpt, 1, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700904 }
Ben Cahill77626352007-11-29 11:09:44 +0800905
906 /* If not searching for a new mode, increment failed counter
907 * ... this helps determine when to start searching again */
Tomas Winklerc33104f2008-01-14 17:46:21 -0800908 if (lq_sta->stay_in_tbl)
909 lq_sta->total_failed++;
Zhu Yib481de92007-09-25 17:54:57 -0700910 --retries;
911 index++;
912
913 }
914
Ben Cahill77626352007-11-29 11:09:44 +0800915 /*
916 * Find (by rate) the history window to update with final Tx attempt;
917 * if Tx was successful first try, use original rate,
918 * else look up the rate that was, finally, successful.
919 */
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800920 tx_mcs.rate_n_flags = le32_to_cpu(table->rs_table[index].rate_n_flags);
921 rs_get_tbl_info_from_mcs(&tx_mcs, priv->band, &tbl_type, &rs_index);
Zhu Yib481de92007-09-25 17:54:57 -0700922
Ben Cahill77626352007-11-29 11:09:44 +0800923 /* Update frame history window with "success" if Tx got ACKed ... */
Zhu Yib481de92007-09-25 17:54:57 -0700924 if (tx_resp->flags & IEEE80211_TX_STATUS_ACK)
925 status = 1;
926 else
927 status = 0;
928
Ben Cahill77626352007-11-29 11:09:44 +0800929 /* If type matches "search" table,
930 * add final tx status to "search" history */
Zhu Yib481de92007-09-25 17:54:57 -0700931 if ((tbl_type.lq_type == search_tbl->lq_type) &&
932 (tbl_type.antenna_type == search_tbl->antenna_type) &&
933 (tbl_type.is_SGI == search_tbl->is_SGI)) {
934 if (search_tbl->expected_tpt)
935 tpt = search_tbl->expected_tpt[rs_index];
936 else
937 tpt = 0;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200938 if (tx_resp->control.flags & IEEE80211_TXCTL_AMPDU)
939 rs_collect_tx_data(search_win, rs_index, tpt,
940 tx_resp->ampdu_ack_len,
941 tx_resp->ampdu_ack_map);
942 else
943 rs_collect_tx_data(search_win, rs_index, tpt,
944 1, status);
Ben Cahill77626352007-11-29 11:09:44 +0800945 /* Else if type matches "current/active" table,
946 * add final tx status to "current/active" history */
Zhu Yib481de92007-09-25 17:54:57 -0700947 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
948 (tbl_type.antenna_type == curr_tbl->antenna_type) &&
949 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
950 if (curr_tbl->expected_tpt)
951 tpt = curr_tbl->expected_tpt[rs_index];
952 else
953 tpt = 0;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200954 if (tx_resp->control.flags & IEEE80211_TXCTL_AMPDU)
955 rs_collect_tx_data(window, rs_index, tpt,
956 tx_resp->ampdu_ack_len,
957 tx_resp->ampdu_ack_map);
958 else
959 rs_collect_tx_data(window, rs_index, tpt,
960 1, status);
Zhu Yib481de92007-09-25 17:54:57 -0700961 }
962
Ben Cahill77626352007-11-29 11:09:44 +0800963 /* If not searching for new mode, increment success/failed counter
964 * ... these help determine when to start searching again */
Tomas Winklerc33104f2008-01-14 17:46:21 -0800965 if (lq_sta->stay_in_tbl) {
Ron Rindjunsky99556432008-01-28 14:07:25 +0200966 if (tx_resp->control.flags & IEEE80211_TXCTL_AMPDU) {
967 lq_sta->total_success += tx_resp->ampdu_ack_map;
968 lq_sta->total_failed +=
969 (tx_resp->ampdu_ack_len - tx_resp->ampdu_ack_map);
970 } else {
971 if (status)
972 lq_sta->total_success++;
973 else
974 lq_sta->total_failed++;
975 }
Zhu Yib481de92007-09-25 17:54:57 -0700976 }
977
Ben Cahill77626352007-11-29 11:09:44 +0800978 /* See if there's a better rate or modulation mode to try. */
Zhu Yib481de92007-09-25 17:54:57 -0700979 rs_rate_scale_perform(priv, dev, hdr, sta);
Tomas Winklerf4d60822008-03-05 11:31:00 -0800980out:
Johannes Bergd0709a62008-02-25 16:27:46 +0100981 rcu_read_unlock();
Zhu Yib481de92007-09-25 17:54:57 -0700982 return;
983}
984
985static u8 rs_is_ant_connected(u8 valid_antenna,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800986 enum iwl4965_antenna_type antenna_type)
Zhu Yib481de92007-09-25 17:54:57 -0700987{
988 if (antenna_type == ANT_AUX)
989 return ((valid_antenna & 0x2) ? 1:0);
990 else if (antenna_type == ANT_MAIN)
991 return ((valid_antenna & 0x1) ? 1:0);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800992 else if (antenna_type == ANT_BOTH)
993 return ((valid_antenna & 0x3) == 0x3);
Zhu Yib481de92007-09-25 17:54:57 -0700994
995 return 1;
996}
997
998static u8 rs_is_other_ant_connected(u8 valid_antenna,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800999 enum iwl4965_antenna_type antenna_type)
Zhu Yib481de92007-09-25 17:54:57 -07001000{
1001 if (antenna_type == ANT_AUX)
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001002 return rs_is_ant_connected(valid_antenna, ANT_MAIN);
Zhu Yib481de92007-09-25 17:54:57 -07001003 else
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001004 return rs_is_ant_connected(valid_antenna, ANT_AUX);
Zhu Yib481de92007-09-25 17:54:57 -07001005
1006 return 0;
1007}
1008
Ben Cahill77626352007-11-29 11:09:44 +08001009/*
1010 * Begin a period of staying with a selected modulation mode.
1011 * Set "stay_in_tbl" flag to prevent any mode switches.
1012 * Set frame tx success limits according to legacy vs. high-throughput,
1013 * and reset overall (spanning all rates) tx success history statistics.
1014 * These control how long we stay using same modulation mode before
1015 * searching for a new mode.
1016 */
Zhu Yib481de92007-09-25 17:54:57 -07001017static void rs_set_stay_in_table(u8 is_legacy,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001018 struct iwl4965_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07001019{
1020 IWL_DEBUG_HT("we are staying in the same table\n");
Tomas Winklerc33104f2008-01-14 17:46:21 -08001021 lq_sta->stay_in_tbl = 1; /* only place this gets set */
Zhu Yib481de92007-09-25 17:54:57 -07001022 if (is_legacy) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001023 lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT;
1024 lq_sta->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
1025 lq_sta->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
Zhu Yib481de92007-09-25 17:54:57 -07001026 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001027 lq_sta->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
1028 lq_sta->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
1029 lq_sta->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
Zhu Yib481de92007-09-25 17:54:57 -07001030 }
Tomas Winklerc33104f2008-01-14 17:46:21 -08001031 lq_sta->table_count = 0;
1032 lq_sta->total_failed = 0;
1033 lq_sta->total_success = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001034}
1035
Ben Cahill77626352007-11-29 11:09:44 +08001036/*
1037 * Find correct throughput table for given mode of modulation
1038 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001039static void rs_get_expected_tpt_table(struct iwl4965_lq_sta *lq_sta,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001040 struct iwl4965_scale_tbl_info *tbl)
Zhu Yib481de92007-09-25 17:54:57 -07001041{
1042 if (is_legacy(tbl->lq_type)) {
1043 if (!is_a_band(tbl->lq_type))
1044 tbl->expected_tpt = expected_tpt_G;
1045 else
1046 tbl->expected_tpt = expected_tpt_A;
1047 } else if (is_siso(tbl->lq_type)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001048 if (tbl->is_fat && !lq_sta->is_dup)
Zhu Yib481de92007-09-25 17:54:57 -07001049 if (tbl->is_SGI)
1050 tbl->expected_tpt = expected_tpt_siso40MHzSGI;
1051 else
1052 tbl->expected_tpt = expected_tpt_siso40MHz;
1053 else if (tbl->is_SGI)
1054 tbl->expected_tpt = expected_tpt_siso20MHzSGI;
1055 else
1056 tbl->expected_tpt = expected_tpt_siso20MHz;
1057
1058 } else if (is_mimo(tbl->lq_type)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001059 if (tbl->is_fat && !lq_sta->is_dup)
Zhu Yib481de92007-09-25 17:54:57 -07001060 if (tbl->is_SGI)
1061 tbl->expected_tpt = expected_tpt_mimo40MHzSGI;
1062 else
1063 tbl->expected_tpt = expected_tpt_mimo40MHz;
1064 else if (tbl->is_SGI)
1065 tbl->expected_tpt = expected_tpt_mimo20MHzSGI;
1066 else
1067 tbl->expected_tpt = expected_tpt_mimo20MHz;
1068 } else
1069 tbl->expected_tpt = expected_tpt_G;
1070}
1071
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001072#ifdef CONFIG_IWL4965_HT
Ben Cahill77626352007-11-29 11:09:44 +08001073/*
1074 * Find starting rate for new "search" high-throughput mode of modulation.
1075 * Goal is to find lowest expected rate (under perfect conditions) that is
1076 * above the current measured throughput of "active" mode, to give new mode
1077 * a fair chance to prove itself without too many challenges.
1078 *
1079 * This gets called when transitioning to more aggressive modulation
1080 * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
1081 * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
1082 * to decrease to match "active" throughput. When moving from MIMO to SISO,
1083 * bit rate will typically need to increase, but not if performance was bad.
1084 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001085static s32 rs_get_best_rate(struct iwl_priv *priv,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001086 struct iwl4965_lq_sta *lq_sta,
Ben Cahill77626352007-11-29 11:09:44 +08001087 struct iwl4965_scale_tbl_info *tbl, /* "search" */
Zhu Yib481de92007-09-25 17:54:57 -07001088 u16 rate_mask, s8 index, s8 rate)
1089{
Ben Cahill77626352007-11-29 11:09:44 +08001090 /* "active" values */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001091 struct iwl4965_scale_tbl_info *active_tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001092 &(lq_sta->lq_info[lq_sta->active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07001093 s32 active_sr = active_tbl->win[index].success_ratio;
Zhu Yib481de92007-09-25 17:54:57 -07001094 s32 active_tpt = active_tbl->expected_tpt[index];
Ben Cahill77626352007-11-29 11:09:44 +08001095
1096 /* expected "search" throughput */
1097 s32 *tpt_tbl = tbl->expected_tpt;
1098
1099 s32 new_rate, high, low, start_hi;
Zhu Yib481de92007-09-25 17:54:57 -07001100 u16 high_low;
1101
1102 new_rate = high = low = start_hi = IWL_RATE_INVALID;
1103
1104 for (; ;) {
1105 high_low = rs_get_adjacent_rate(rate, rate_mask, tbl->lq_type);
1106
1107 low = high_low & 0xff;
1108 high = (high_low >> 8) & 0xff;
1109
Ben Cahill77626352007-11-29 11:09:44 +08001110 /*
1111 * Lower the "search" bit rate, to give new "search" mode
1112 * approximately the same throughput as "active" if:
1113 *
1114 * 1) "Active" mode has been working modestly well (but not
1115 * great), and expected "search" throughput (under perfect
1116 * conditions) at candidate rate is above the actual
1117 * measured "active" throughput (but less than expected
1118 * "active" throughput under perfect conditions).
1119 * OR
1120 * 2) "Active" mode has been working perfectly or very well
1121 * and expected "search" throughput (under perfect
1122 * conditions) at candidate rate is above expected
1123 * "active" throughput (under perfect conditions).
1124 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001125 if ((((100 * tpt_tbl[rate]) > lq_sta->last_tpt) &&
Zhu Yib481de92007-09-25 17:54:57 -07001126 ((active_sr > IWL_RATE_DECREASE_TH) &&
1127 (active_sr <= IWL_RATE_HIGH_TH) &&
1128 (tpt_tbl[rate] <= active_tpt))) ||
1129 ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
1130 (tpt_tbl[rate] > active_tpt))) {
1131
Ben Cahill77626352007-11-29 11:09:44 +08001132 /* (2nd or later pass)
1133 * If we've already tried to raise the rate, and are
1134 * now trying to lower it, use the higher rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001135 if (start_hi != IWL_RATE_INVALID) {
1136 new_rate = start_hi;
1137 break;
1138 }
Ben Cahill77626352007-11-29 11:09:44 +08001139
Zhu Yib481de92007-09-25 17:54:57 -07001140 new_rate = rate;
Ben Cahill77626352007-11-29 11:09:44 +08001141
1142 /* Loop again with lower rate */
Zhu Yib481de92007-09-25 17:54:57 -07001143 if (low != IWL_RATE_INVALID)
1144 rate = low;
Ben Cahill77626352007-11-29 11:09:44 +08001145
1146 /* Lower rate not available, use the original */
Zhu Yib481de92007-09-25 17:54:57 -07001147 else
1148 break;
Ben Cahill77626352007-11-29 11:09:44 +08001149
1150 /* Else try to raise the "search" rate to match "active" */
Zhu Yib481de92007-09-25 17:54:57 -07001151 } else {
Ben Cahill77626352007-11-29 11:09:44 +08001152 /* (2nd or later pass)
1153 * If we've already tried to lower the rate, and are
1154 * now trying to raise it, use the lower rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001155 if (new_rate != IWL_RATE_INVALID)
1156 break;
Ben Cahill77626352007-11-29 11:09:44 +08001157
1158 /* Loop again with higher rate */
Zhu Yib481de92007-09-25 17:54:57 -07001159 else if (high != IWL_RATE_INVALID) {
1160 start_hi = high;
1161 rate = high;
Ben Cahill77626352007-11-29 11:09:44 +08001162
1163 /* Higher rate not available, use the original */
Zhu Yib481de92007-09-25 17:54:57 -07001164 } else {
Zhu Yib481de92007-09-25 17:54:57 -07001165 break;
1166 }
1167 }
1168 }
1169
1170 return new_rate;
1171}
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001172#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001173
1174static inline u8 rs_is_both_ant_supp(u8 valid_antenna)
1175{
1176 return (rs_is_ant_connected(valid_antenna, ANT_BOTH));
1177}
1178
Ben Cahill77626352007-11-29 11:09:44 +08001179/*
1180 * Set up search table for MIMO
1181 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001182static int rs_switch_to_mimo(struct iwl_priv *priv,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001183 struct iwl4965_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001184 struct ieee80211_conf *conf,
1185 struct sta_info *sta,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001186 struct iwl4965_scale_tbl_info *tbl, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001187{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001188#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07001189 u16 rate_mask;
1190 s32 rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001191 s8 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07001192
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001193 if (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) ||
1194 !sta->ht_info.ht_supported)
Zhu Yib481de92007-09-25 17:54:57 -07001195 return -1;
1196
1197 IWL_DEBUG_HT("LQ: try to switch to MIMO\n");
1198 tbl->lq_type = LQ_MIMO;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001199 rs_get_supported_rates(lq_sta, NULL, tbl->lq_type,
Zhu Yib481de92007-09-25 17:54:57 -07001200 &rate_mask);
1201
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001202 if (priv->current_ht_config.tx_mimo_ps_mode == IWL_MIMO_PS_STATIC)
Zhu Yib481de92007-09-25 17:54:57 -07001203 return -1;
1204
Ben Cahill77626352007-11-29 11:09:44 +08001205 /* Need both Tx chains/antennas to support MIMO */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001206 if (!rs_is_both_ant_supp(lq_sta->antenna))
Zhu Yib481de92007-09-25 17:54:57 -07001207 return -1;
1208
Tomas Winklerc33104f2008-01-14 17:46:21 -08001209 tbl->is_dup = lq_sta->is_dup;
Zhu Yib481de92007-09-25 17:54:57 -07001210 tbl->action = 0;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001211 if (priv->current_ht_config.supported_chan_width
1212 == IWL_CHANNEL_WIDTH_40MHZ)
Zhu Yib481de92007-09-25 17:54:57 -07001213 tbl->is_fat = 1;
1214 else
1215 tbl->is_fat = 0;
1216
1217 if (tbl->is_fat) {
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001218 if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
Zhu Yib481de92007-09-25 17:54:57 -07001219 tbl->is_SGI = 1;
1220 else
1221 tbl->is_SGI = 0;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001222 } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
Zhu Yib481de92007-09-25 17:54:57 -07001223 tbl->is_SGI = 1;
1224 else
1225 tbl->is_SGI = 0;
1226
Tomas Winklerc33104f2008-01-14 17:46:21 -08001227 rs_get_expected_tpt_table(lq_sta, tbl);
Zhu Yib481de92007-09-25 17:54:57 -07001228
Tomas Winklerc33104f2008-01-14 17:46:21 -08001229 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index, index);
Zhu Yib481de92007-09-25 17:54:57 -07001230
1231 IWL_DEBUG_HT("LQ: MIMO best rate %d mask %X\n", rate, rate_mask);
1232 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask))
1233 return -1;
1234 rs_mcs_from_tbl(&tbl->current_rate, tbl, rate, is_green);
1235
1236 IWL_DEBUG_HT("LQ: Switch to new mcs %X index is green %X\n",
1237 tbl->current_rate.rate_n_flags, is_green);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001238 return 0;
Mohamed Abbas403ab562007-11-06 22:06:25 -08001239#else
1240 return -1;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001241#endif /*CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001242}
1243
Ben Cahill77626352007-11-29 11:09:44 +08001244/*
1245 * Set up search table for SISO
1246 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001247static int rs_switch_to_siso(struct iwl_priv *priv,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001248 struct iwl4965_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001249 struct ieee80211_conf *conf,
1250 struct sta_info *sta,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001251 struct iwl4965_scale_tbl_info *tbl, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001252{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001253#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07001254 u16 rate_mask;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001255 u8 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07001256 s32 rate;
1257
1258 IWL_DEBUG_HT("LQ: try to switch to SISO\n");
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001259 if (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) ||
1260 !sta->ht_info.ht_supported)
Zhu Yib481de92007-09-25 17:54:57 -07001261 return -1;
1262
Tomas Winklerc33104f2008-01-14 17:46:21 -08001263 tbl->is_dup = lq_sta->is_dup;
Zhu Yib481de92007-09-25 17:54:57 -07001264 tbl->lq_type = LQ_SISO;
1265 tbl->action = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001266 rs_get_supported_rates(lq_sta, NULL, tbl->lq_type,
Zhu Yib481de92007-09-25 17:54:57 -07001267 &rate_mask);
1268
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001269 if (priv->current_ht_config.supported_chan_width
1270 == IWL_CHANNEL_WIDTH_40MHZ)
Zhu Yib481de92007-09-25 17:54:57 -07001271 tbl->is_fat = 1;
1272 else
1273 tbl->is_fat = 0;
1274
1275 if (tbl->is_fat) {
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001276 if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
Zhu Yib481de92007-09-25 17:54:57 -07001277 tbl->is_SGI = 1;
1278 else
1279 tbl->is_SGI = 0;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001280 } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
Zhu Yib481de92007-09-25 17:54:57 -07001281 tbl->is_SGI = 1;
1282 else
1283 tbl->is_SGI = 0;
1284
1285 if (is_green)
1286 tbl->is_SGI = 0;
1287
Tomas Winklerc33104f2008-01-14 17:46:21 -08001288 rs_get_expected_tpt_table(lq_sta, tbl);
1289 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index, index);
Zhu Yib481de92007-09-25 17:54:57 -07001290
1291 IWL_DEBUG_HT("LQ: get best rate %d mask %X\n", rate, rate_mask);
1292 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1293 IWL_DEBUG_HT("can not switch with index %d rate mask %x\n",
1294 rate, rate_mask);
1295 return -1;
1296 }
1297 rs_mcs_from_tbl(&tbl->current_rate, tbl, rate, is_green);
1298 IWL_DEBUG_HT("LQ: Switch to new mcs %X index is green %X\n",
1299 tbl->current_rate.rate_n_flags, is_green);
Mohamed Abbas403ab562007-11-06 22:06:25 -08001300 return 0;
1301#else
1302 return -1;
Zhu Yib481de92007-09-25 17:54:57 -07001303
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001304#endif /*CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001305}
1306
Ben Cahill77626352007-11-29 11:09:44 +08001307/*
1308 * Try to switch to new modulation mode from legacy
1309 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001310static int rs_move_legacy_other(struct iwl_priv *priv,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001311 struct iwl4965_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001312 struct ieee80211_conf *conf,
1313 struct sta_info *sta,
Zhu Yib481de92007-09-25 17:54:57 -07001314 int index)
1315{
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001316 int ret = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001317 struct iwl4965_scale_tbl_info *tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001318 &(lq_sta->lq_info[lq_sta->active_tbl]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001319 struct iwl4965_scale_tbl_info *search_tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001320 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001321 struct iwl4965_rate_scale_data *window = &(tbl->win[index]);
1322 u32 sz = (sizeof(struct iwl4965_scale_tbl_info) -
1323 (sizeof(struct iwl4965_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001324 u8 start_action = tbl->action;
1325
1326 for (; ;) {
1327 switch (tbl->action) {
1328 case IWL_LEGACY_SWITCH_ANTENNA:
1329 IWL_DEBUG_HT("LQ Legacy switch Antenna\n");
1330
1331 search_tbl->lq_type = LQ_NONE;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001332 lq_sta->action_counter++;
Ben Cahill77626352007-11-29 11:09:44 +08001333
1334 /* Don't change antenna if success has been great */
Zhu Yib481de92007-09-25 17:54:57 -07001335 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1336 break;
Ben Cahill77626352007-11-29 11:09:44 +08001337
1338 /* Don't change antenna if other one is not connected */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001339 if (!rs_is_other_ant_connected(lq_sta->antenna,
Zhu Yib481de92007-09-25 17:54:57 -07001340 tbl->antenna_type))
1341 break;
1342
Ben Cahill77626352007-11-29 11:09:44 +08001343 /* Set up search table to try other antenna */
Zhu Yib481de92007-09-25 17:54:57 -07001344 memcpy(search_tbl, tbl, sz);
1345
1346 rs_toggle_antenna(&(search_tbl->current_rate),
1347 search_tbl);
Tomas Winklerc33104f2008-01-14 17:46:21 -08001348 rs_get_expected_tpt_table(lq_sta, search_tbl);
1349 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001350 goto out;
1351
1352 case IWL_LEGACY_SWITCH_SISO:
1353 IWL_DEBUG_HT("LQ: Legacy switch to SISO\n");
Ben Cahill77626352007-11-29 11:09:44 +08001354
1355 /* Set up search table to try SISO */
Zhu Yib481de92007-09-25 17:54:57 -07001356 memcpy(search_tbl, tbl, sz);
1357 search_tbl->lq_type = LQ_SISO;
1358 search_tbl->is_SGI = 0;
1359 search_tbl->is_fat = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001360 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001361 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001362 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001363 lq_sta->search_better_tbl = 1;
1364 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001365 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001366 }
Zhu Yib481de92007-09-25 17:54:57 -07001367
1368 break;
1369 case IWL_LEGACY_SWITCH_MIMO:
1370 IWL_DEBUG_HT("LQ: Legacy switch MIMO\n");
Ben Cahill77626352007-11-29 11:09:44 +08001371
1372 /* Set up search table to try MIMO */
Zhu Yib481de92007-09-25 17:54:57 -07001373 memcpy(search_tbl, tbl, sz);
1374 search_tbl->lq_type = LQ_MIMO;
1375 search_tbl->is_SGI = 0;
1376 search_tbl->is_fat = 0;
1377 search_tbl->antenna_type = ANT_BOTH;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001378 ret = rs_switch_to_mimo(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001379 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001380 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001381 lq_sta->search_better_tbl = 1;
1382 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001383 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001384 }
Zhu Yib481de92007-09-25 17:54:57 -07001385 break;
1386 }
1387 tbl->action++;
1388 if (tbl->action > IWL_LEGACY_SWITCH_MIMO)
1389 tbl->action = IWL_LEGACY_SWITCH_ANTENNA;
1390
1391 if (tbl->action == start_action)
1392 break;
1393
1394 }
1395 return 0;
1396
1397 out:
1398 tbl->action++;
1399 if (tbl->action > IWL_LEGACY_SWITCH_MIMO)
1400 tbl->action = IWL_LEGACY_SWITCH_ANTENNA;
1401 return 0;
1402
1403}
1404
Ben Cahill77626352007-11-29 11:09:44 +08001405/*
1406 * Try to switch to new modulation mode from SISO
1407 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001408static int rs_move_siso_to_other(struct iwl_priv *priv,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001409 struct iwl4965_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001410 struct ieee80211_conf *conf,
1411 struct sta_info *sta,
Zhu Yib481de92007-09-25 17:54:57 -07001412 int index)
1413{
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001414 int ret;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001415 u8 is_green = lq_sta->is_green;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001416 struct iwl4965_scale_tbl_info *tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001417 &(lq_sta->lq_info[lq_sta->active_tbl]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001418 struct iwl4965_scale_tbl_info *search_tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001419 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001420 struct iwl4965_rate_scale_data *window = &(tbl->win[index]);
1421 u32 sz = (sizeof(struct iwl4965_scale_tbl_info) -
1422 (sizeof(struct iwl4965_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001423 u8 start_action = tbl->action;
1424
1425 for (;;) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001426 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001427 switch (tbl->action) {
1428 case IWL_SISO_SWITCH_ANTENNA:
1429 IWL_DEBUG_HT("LQ: SISO SWITCH ANTENNA SISO\n");
1430 search_tbl->lq_type = LQ_NONE;
1431 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1432 break;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001433 if (!rs_is_other_ant_connected(lq_sta->antenna,
Zhu Yib481de92007-09-25 17:54:57 -07001434 tbl->antenna_type))
1435 break;
1436
1437 memcpy(search_tbl, tbl, sz);
1438 search_tbl->action = IWL_SISO_SWITCH_MIMO;
1439 rs_toggle_antenna(&(search_tbl->current_rate),
1440 search_tbl);
Tomas Winklerc33104f2008-01-14 17:46:21 -08001441 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001442
1443 goto out;
1444
1445 case IWL_SISO_SWITCH_MIMO:
1446 IWL_DEBUG_HT("LQ: SISO SWITCH TO MIMO FROM SISO\n");
1447 memcpy(search_tbl, tbl, sz);
1448 search_tbl->lq_type = LQ_MIMO;
1449 search_tbl->is_SGI = 0;
1450 search_tbl->is_fat = 0;
1451 search_tbl->antenna_type = ANT_BOTH;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001452 ret = rs_switch_to_mimo(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001453 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001454 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001455 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001456 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001457 }
Zhu Yib481de92007-09-25 17:54:57 -07001458 break;
1459 case IWL_SISO_SWITCH_GI:
1460 IWL_DEBUG_HT("LQ: SISO SWITCH TO GI\n");
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001461
Zhu Yib481de92007-09-25 17:54:57 -07001462 memcpy(search_tbl, tbl, sz);
1463 search_tbl->action = 0;
1464 if (search_tbl->is_SGI)
1465 search_tbl->is_SGI = 0;
1466 else if (!is_green)
1467 search_tbl->is_SGI = 1;
1468 else
1469 break;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001470 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001471 if ((tbl->lq_type == LQ_SISO) &&
1472 (tbl->is_SGI)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001473 s32 tpt = lq_sta->last_tpt / 100;
Zhu Yib481de92007-09-25 17:54:57 -07001474 if (((!tbl->is_fat) &&
1475 (tpt >= expected_tpt_siso20MHz[index])) ||
1476 ((tbl->is_fat) &&
1477 (tpt >= expected_tpt_siso40MHz[index])))
Tomas Winklerc33104f2008-01-14 17:46:21 -08001478 lq_sta->search_better_tbl = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001479 }
Tomas Winklerc33104f2008-01-14 17:46:21 -08001480 rs_get_expected_tpt_table(lq_sta, search_tbl);
Zhu Yib481de92007-09-25 17:54:57 -07001481 rs_mcs_from_tbl(&search_tbl->current_rate,
1482 search_tbl, index, is_green);
1483 goto out;
1484 }
1485 tbl->action++;
1486 if (tbl->action > IWL_SISO_SWITCH_GI)
1487 tbl->action = IWL_SISO_SWITCH_ANTENNA;
1488
1489 if (tbl->action == start_action)
1490 break;
1491 }
1492 return 0;
1493
1494 out:
1495 tbl->action++;
1496 if (tbl->action > IWL_SISO_SWITCH_GI)
1497 tbl->action = IWL_SISO_SWITCH_ANTENNA;
1498 return 0;
1499}
1500
Ben Cahill77626352007-11-29 11:09:44 +08001501/*
1502 * Try to switch to new modulation mode from MIMO
1503 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001504static int rs_move_mimo_to_other(struct iwl_priv *priv,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001505 struct iwl4965_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001506 struct ieee80211_conf *conf,
1507 struct sta_info *sta,
Zhu Yib481de92007-09-25 17:54:57 -07001508 int index)
1509{
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001510 int ret;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001511 s8 is_green = lq_sta->is_green;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001512 struct iwl4965_scale_tbl_info *tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001513 &(lq_sta->lq_info[lq_sta->active_tbl]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001514 struct iwl4965_scale_tbl_info *search_tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001515 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001516 u32 sz = (sizeof(struct iwl4965_scale_tbl_info) -
1517 (sizeof(struct iwl4965_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001518 u8 start_action = tbl->action;
1519
1520 for (;;) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001521 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001522 switch (tbl->action) {
1523 case IWL_MIMO_SWITCH_ANTENNA_A:
1524 case IWL_MIMO_SWITCH_ANTENNA_B:
1525 IWL_DEBUG_HT("LQ: MIMO SWITCH TO SISO\n");
Ben Cahill77626352007-11-29 11:09:44 +08001526
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001527
Ben Cahill77626352007-11-29 11:09:44 +08001528 /* Set up new search table for SISO */
Zhu Yib481de92007-09-25 17:54:57 -07001529 memcpy(search_tbl, tbl, sz);
1530 search_tbl->lq_type = LQ_SISO;
1531 search_tbl->is_SGI = 0;
1532 search_tbl->is_fat = 0;
1533 if (tbl->action == IWL_MIMO_SWITCH_ANTENNA_A)
1534 search_tbl->antenna_type = ANT_MAIN;
1535 else
1536 search_tbl->antenna_type = ANT_AUX;
1537
Tomas Winklerc33104f2008-01-14 17:46:21 -08001538 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001539 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001540 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001541 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001542 goto out;
1543 }
1544 break;
1545
1546 case IWL_MIMO_SWITCH_GI:
1547 IWL_DEBUG_HT("LQ: MIMO SWITCH TO GI\n");
Ben Cahill77626352007-11-29 11:09:44 +08001548
1549 /* Set up new search table for MIMO */
Zhu Yib481de92007-09-25 17:54:57 -07001550 memcpy(search_tbl, tbl, sz);
1551 search_tbl->lq_type = LQ_MIMO;
1552 search_tbl->antenna_type = ANT_BOTH;
1553 search_tbl->action = 0;
1554 if (search_tbl->is_SGI)
1555 search_tbl->is_SGI = 0;
1556 else
1557 search_tbl->is_SGI = 1;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001558 lq_sta->search_better_tbl = 1;
Ben Cahill77626352007-11-29 11:09:44 +08001559
1560 /*
1561 * If active table already uses the fastest possible
1562 * modulation (dual stream with short guard interval),
1563 * and it's working well, there's no need to look
1564 * for a better type of modulation!
1565 */
Zhu Yib481de92007-09-25 17:54:57 -07001566 if ((tbl->lq_type == LQ_MIMO) &&
1567 (tbl->is_SGI)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001568 s32 tpt = lq_sta->last_tpt / 100;
Zhu Yib481de92007-09-25 17:54:57 -07001569 if (((!tbl->is_fat) &&
1570 (tpt >= expected_tpt_mimo20MHz[index])) ||
1571 ((tbl->is_fat) &&
1572 (tpt >= expected_tpt_mimo40MHz[index])))
Tomas Winklerc33104f2008-01-14 17:46:21 -08001573 lq_sta->search_better_tbl = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001574 }
Tomas Winklerc33104f2008-01-14 17:46:21 -08001575 rs_get_expected_tpt_table(lq_sta, search_tbl);
Zhu Yib481de92007-09-25 17:54:57 -07001576 rs_mcs_from_tbl(&search_tbl->current_rate,
1577 search_tbl, index, is_green);
1578 goto out;
1579
1580 }
1581 tbl->action++;
1582 if (tbl->action > IWL_MIMO_SWITCH_GI)
1583 tbl->action = IWL_MIMO_SWITCH_ANTENNA_A;
1584
1585 if (tbl->action == start_action)
1586 break;
1587 }
1588
1589 return 0;
1590 out:
1591 tbl->action++;
1592 if (tbl->action > IWL_MIMO_SWITCH_GI)
1593 tbl->action = IWL_MIMO_SWITCH_ANTENNA_A;
1594 return 0;
1595
1596}
1597
Ben Cahill77626352007-11-29 11:09:44 +08001598/*
1599 * Check whether we should continue using same modulation mode, or
1600 * begin search for a new mode, based on:
1601 * 1) # tx successes or failures while using this mode
1602 * 2) # times calling this function
1603 * 3) elapsed time in this mode (not used, for now)
1604 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001605static void rs_stay_in_table(struct iwl4965_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07001606{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001607 struct iwl4965_scale_tbl_info *tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001608 int i;
1609 int active_tbl;
1610 int flush_interval_passed = 0;
1611
Tomas Winklerc33104f2008-01-14 17:46:21 -08001612 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001613
Tomas Winklerc33104f2008-01-14 17:46:21 -08001614 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07001615
Ben Cahill77626352007-11-29 11:09:44 +08001616 /* If we've been disallowing search, see if we should now allow it */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001617 if (lq_sta->stay_in_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07001618
Ben Cahill77626352007-11-29 11:09:44 +08001619 /* Elapsed time using current modulation mode */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001620 if (lq_sta->flush_timer)
Zhu Yib481de92007-09-25 17:54:57 -07001621 flush_interval_passed =
1622 time_after(jiffies,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001623 (unsigned long)(lq_sta->flush_timer +
Zhu Yib481de92007-09-25 17:54:57 -07001624 IWL_RATE_SCALE_FLUSH_INTVL));
1625
Ben Cahill77626352007-11-29 11:09:44 +08001626 /* For now, disable the elapsed time criterion */
Zhu Yib481de92007-09-25 17:54:57 -07001627 flush_interval_passed = 0;
Ben Cahill77626352007-11-29 11:09:44 +08001628
1629 /*
1630 * Check if we should allow search for new modulation mode.
1631 * If many frames have failed or succeeded, or we've used
1632 * this same modulation for a long time, allow search, and
1633 * reset history stats that keep track of whether we should
1634 * allow a new search. Also (below) reset all bitmaps and
1635 * stats in active history.
1636 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001637 if ((lq_sta->total_failed > lq_sta->max_failure_limit) ||
1638 (lq_sta->total_success > lq_sta->max_success_limit) ||
1639 ((!lq_sta->search_better_tbl) && (lq_sta->flush_timer)
Zhu Yib481de92007-09-25 17:54:57 -07001640 && (flush_interval_passed))) {
1641 IWL_DEBUG_HT("LQ: stay is expired %d %d %d\n:",
Tomas Winklerc33104f2008-01-14 17:46:21 -08001642 lq_sta->total_failed,
1643 lq_sta->total_success,
Zhu Yib481de92007-09-25 17:54:57 -07001644 flush_interval_passed);
Ben Cahill77626352007-11-29 11:09:44 +08001645
1646 /* Allow search for new mode */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001647 lq_sta->stay_in_tbl = 0; /* only place reset */
1648 lq_sta->total_failed = 0;
1649 lq_sta->total_success = 0;
1650 lq_sta->flush_timer = 0;
Ben Cahill77626352007-11-29 11:09:44 +08001651
1652 /*
1653 * Else if we've used this modulation mode enough repetitions
1654 * (regardless of elapsed time or success/failure), reset
1655 * history bitmaps and rate-specific stats for all rates in
1656 * active table.
1657 */
Mohamed Abbas403ab562007-11-06 22:06:25 -08001658 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001659 lq_sta->table_count++;
1660 if (lq_sta->table_count >=
1661 lq_sta->table_count_limit) {
1662 lq_sta->table_count = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001663
1664 IWL_DEBUG_HT("LQ: stay in table clear win\n");
1665 for (i = 0; i < IWL_RATE_COUNT; i++)
1666 rs_rate_scale_clear_window(
1667 &(tbl->win[i]));
1668 }
1669 }
1670
Ben Cahill77626352007-11-29 11:09:44 +08001671 /* If transitioning to allow "search", reset all history
1672 * bitmaps and stats in active table (this will become the new
1673 * "search" table). */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001674 if (!lq_sta->stay_in_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07001675 for (i = 0; i < IWL_RATE_COUNT; i++)
1676 rs_rate_scale_clear_window(&(tbl->win[i]));
1677 }
1678 }
1679}
1680
Ben Cahill77626352007-11-29 11:09:44 +08001681/*
1682 * Do rate scaling and search for new modulation mode.
1683 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001684static void rs_rate_scale_perform(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001685 struct net_device *dev,
1686 struct ieee80211_hdr *hdr,
1687 struct sta_info *sta)
1688{
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001689 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1690 struct ieee80211_hw *hw = local_to_hw(local);
1691 struct ieee80211_conf *conf = &hw->conf;
Zhu Yib481de92007-09-25 17:54:57 -07001692 int low = IWL_RATE_INVALID;
1693 int high = IWL_RATE_INVALID;
1694 int index;
1695 int i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001696 struct iwl4965_rate_scale_data *window = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001697 int current_tpt = IWL_INVALID_VALUE;
1698 int low_tpt = IWL_INVALID_VALUE;
1699 int high_tpt = IWL_INVALID_VALUE;
1700 u32 fail_count;
1701 s8 scale_action = 0;
1702 u16 fc, rate_mask;
1703 u8 update_lq = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001704 struct iwl4965_lq_sta *lq_sta;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001705 struct iwl4965_scale_tbl_info *tbl, *tbl1;
Zhu Yib481de92007-09-25 17:54:57 -07001706 u16 rate_scale_index_msk = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001707 struct iwl4965_rate mcs_rate;
Zhu Yib481de92007-09-25 17:54:57 -07001708 u8 is_green = 0;
1709 u8 active_tbl = 0;
1710 u8 done_search = 0;
1711 u16 high_low;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001712#ifdef CONFIG_IWL4965_HT
1713 u8 tid = MAX_TID_COUNT;
1714 __le16 *qc;
1715#endif
Zhu Yib481de92007-09-25 17:54:57 -07001716
1717 IWL_DEBUG_RATE("rate scale calculate new rate for skb\n");
1718
1719 fc = le16_to_cpu(hdr->frame_control);
1720 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1)) {
1721 /* Send management frames and broadcast/multicast data using
1722 * lowest rate. */
1723 /* TODO: this could probably be improved.. */
1724 return;
1725 }
1726
1727 if (!sta || !sta->rate_ctrl_priv)
1728 return;
1729
1730 if (!priv->lq_mngr.lq_ready) {
1731 IWL_DEBUG_RATE("still rate scaling not ready\n");
1732 return;
1733 }
Tomas Winklerc33104f2008-01-14 17:46:21 -08001734 lq_sta = (struct iwl4965_lq_sta *)sta->rate_ctrl_priv;
Zhu Yib481de92007-09-25 17:54:57 -07001735
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001736#ifdef CONFIG_IWL4965_HT
1737 qc = ieee80211_get_qos_ctrl(hdr);
1738 if (qc) {
1739 tid = (u8)(le16_to_cpu(*qc) & 0xf);
1740 rs_tl_add_packet(lq_sta, tid);
1741 }
1742#endif
Ben Cahill77626352007-11-29 11:09:44 +08001743 /*
1744 * Select rate-scale / modulation-mode table to work with in
1745 * the rest of this function: "search" if searching for better
1746 * modulation mode, or "active" if doing rate scaling within a mode.
1747 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001748 if (!lq_sta->search_better_tbl)
1749 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001750 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08001751 active_tbl = 1 - lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001752
Tomas Winklerc33104f2008-01-14 17:46:21 -08001753 tbl = &(lq_sta->lq_info[active_tbl]);
1754 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07001755
Ben Cahill77626352007-11-29 11:09:44 +08001756 /* current tx rate */
Johannes Berg8318d782008-01-24 19:38:38 +01001757 index = sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07001758
1759 IWL_DEBUG_RATE("Rate scale index %d for type %d\n", index,
1760 tbl->lq_type);
1761
Ben Cahill77626352007-11-29 11:09:44 +08001762 /* rates available for this association, and for modulation mode */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001763 rs_get_supported_rates(lq_sta, hdr, tbl->lq_type,
Zhu Yib481de92007-09-25 17:54:57 -07001764 &rate_mask);
1765
1766 IWL_DEBUG_RATE("mask 0x%04X \n", rate_mask);
1767
1768 /* mask with station rate restriction */
1769 if (is_legacy(tbl->lq_type)) {
Johannes Berg8318d782008-01-24 19:38:38 +01001770 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Ben Cahill77626352007-11-29 11:09:44 +08001771 /* supp_rates has no CCK bits in A mode */
Zhu Yib481de92007-09-25 17:54:57 -07001772 rate_scale_index_msk = (u16) (rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -08001773 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
Zhu Yib481de92007-09-25 17:54:57 -07001774 else
1775 rate_scale_index_msk = (u16) (rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -08001776 lq_sta->supp_rates);
Zhu Yib481de92007-09-25 17:54:57 -07001777
1778 } else
1779 rate_scale_index_msk = rate_mask;
1780
1781 if (!rate_scale_index_msk)
1782 rate_scale_index_msk = rate_mask;
1783
Ben Cahill77626352007-11-29 11:09:44 +08001784 /* If current rate is no longer supported on current association,
1785 * or user changed preferences for rates, find a new supported rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001786 if (index < 0 || !((1 << index) & rate_scale_index_msk)) {
1787 index = IWL_INVALID_VALUE;
1788 update_lq = 1;
1789
Ben Cahill77626352007-11-29 11:09:44 +08001790 /* get the highest available rate */
Zhu Yib481de92007-09-25 17:54:57 -07001791 for (i = 0; i <= IWL_RATE_COUNT; i++) {
1792 if ((1 << i) & rate_scale_index_msk)
1793 index = i;
1794 }
1795
1796 if (index == IWL_INVALID_VALUE) {
1797 IWL_WARNING("Can not find a suitable rate\n");
1798 return;
1799 }
1800 }
1801
Ben Cahill77626352007-11-29 11:09:44 +08001802 /* Get expected throughput table and history window for current rate */
Zhu Yib481de92007-09-25 17:54:57 -07001803 if (!tbl->expected_tpt)
Tomas Winklerc33104f2008-01-14 17:46:21 -08001804 rs_get_expected_tpt_table(lq_sta, tbl);
Zhu Yib481de92007-09-25 17:54:57 -07001805
1806 window = &(tbl->win[index]);
1807
Ben Cahill77626352007-11-29 11:09:44 +08001808 /*
1809 * If there is not enough history to calculate actual average
1810 * throughput, keep analyzing results of more tx frames, without
1811 * changing rate or mode (bypass most of the rest of this function).
1812 * Set up new rate table in uCode only if old rate is not supported
1813 * in current association (use new rate found above).
1814 */
Zhu Yib481de92007-09-25 17:54:57 -07001815 fail_count = window->counter - window->success_counter;
1816 if (((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
1817 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH))
1818 || (tbl->expected_tpt == NULL)) {
1819 IWL_DEBUG_RATE("LQ: still below TH succ %d total %d "
1820 "for index %d\n",
1821 window->success_counter, window->counter, index);
Ben Cahill77626352007-11-29 11:09:44 +08001822
1823 /* Can't calculate this yet; not enough history */
Zhu Yib481de92007-09-25 17:54:57 -07001824 window->average_tpt = IWL_INVALID_VALUE;
Ben Cahill77626352007-11-29 11:09:44 +08001825
1826 /* Should we stay with this modulation mode,
1827 * or search for a new one? */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001828 rs_stay_in_table(lq_sta);
Ben Cahill77626352007-11-29 11:09:44 +08001829
1830 /* Set up new rate table in uCode, if needed */
Zhu Yib481de92007-09-25 17:54:57 -07001831 if (update_lq) {
1832 rs_mcs_from_tbl(&mcs_rate, tbl, index, is_green);
Tomas Winklerc33104f2008-01-14 17:46:21 -08001833 rs_fill_link_cmd(lq_sta, &mcs_rate, &lq_sta->lq);
Tomas Winkler66c73db2008-04-15 16:01:40 -07001834 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07001835 }
1836 goto out;
1837
Ben Cahill77626352007-11-29 11:09:44 +08001838 /* Else we have enough samples; calculate estimate of
1839 * actual average throughput */
Zhu Yib481de92007-09-25 17:54:57 -07001840 } else
1841 window->average_tpt = ((window->success_ratio *
1842 tbl->expected_tpt[index] + 64) / 128);
1843
Ben Cahill77626352007-11-29 11:09:44 +08001844 /* If we are searching for better modulation mode, check success. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001845 if (lq_sta->search_better_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07001846 int success_limit = IWL_RATE_SCALE_SWITCH;
1847
Ben Cahill77626352007-11-29 11:09:44 +08001848 /* If good success, continue using the "search" mode;
1849 * no need to send new link quality command, since we're
1850 * continuing to use the setup that we've been trying. */
Zhu Yib481de92007-09-25 17:54:57 -07001851 if ((window->success_ratio > success_limit) ||
Tomas Winklerc33104f2008-01-14 17:46:21 -08001852 (window->average_tpt > lq_sta->last_tpt)) {
Zhu Yib481de92007-09-25 17:54:57 -07001853 if (!is_legacy(tbl->lq_type)) {
1854 IWL_DEBUG_HT("LQ: we are switching to HT"
1855 " rate suc %d current tpt %d"
1856 " old tpt %d\n",
1857 window->success_ratio,
1858 window->average_tpt,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001859 lq_sta->last_tpt);
1860 lq_sta->enable_counter = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001861 }
Ben Cahill77626352007-11-29 11:09:44 +08001862 /* Swap tables; "search" becomes "active" */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001863 lq_sta->active_tbl = active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001864 current_tpt = window->average_tpt;
Ben Cahill77626352007-11-29 11:09:44 +08001865
1866 /* Else poor success; go back to mode in "active" table */
Zhu Yib481de92007-09-25 17:54:57 -07001867 } else {
Ben Cahill77626352007-11-29 11:09:44 +08001868 /* Nullify "search" table */
Zhu Yib481de92007-09-25 17:54:57 -07001869 tbl->lq_type = LQ_NONE;
Ben Cahill77626352007-11-29 11:09:44 +08001870
1871 /* Revert to "active" table */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001872 active_tbl = lq_sta->active_tbl;
1873 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07001874
Ben Cahill77626352007-11-29 11:09:44 +08001875 /* Revert to "active" rate and throughput info */
Tomas Winkler17744ff2008-03-02 01:52:00 +02001876 index = iwl4965_hwrate_to_plcp_idx(
Zhu Yib481de92007-09-25 17:54:57 -07001877 tbl->current_rate.rate_n_flags);
Tomas Winklerc33104f2008-01-14 17:46:21 -08001878 current_tpt = lq_sta->last_tpt;
Ben Cahill77626352007-11-29 11:09:44 +08001879
1880 /* Need to set up a new rate table in uCode */
1881 update_lq = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001882 IWL_DEBUG_HT("XXY GO BACK TO OLD TABLE\n");
1883 }
Ben Cahill77626352007-11-29 11:09:44 +08001884
1885 /* Either way, we've made a decision; modulation mode
1886 * search is done, allow rate adjustment next time. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001887 lq_sta->search_better_tbl = 0;
Ben Cahill77626352007-11-29 11:09:44 +08001888 done_search = 1; /* Don't switch modes below! */
Zhu Yib481de92007-09-25 17:54:57 -07001889 goto lq_update;
1890 }
1891
Ben Cahill77626352007-11-29 11:09:44 +08001892 /* (Else) not in search of better modulation mode, try for better
1893 * starting rate, while staying in this mode. */
Zhu Yib481de92007-09-25 17:54:57 -07001894 high_low = rs_get_adjacent_rate(index, rate_scale_index_msk,
1895 tbl->lq_type);
1896 low = high_low & 0xff;
1897 high = (high_low >> 8) & 0xff;
1898
Ben Cahill77626352007-11-29 11:09:44 +08001899 /* Collect measured throughputs for current and adjacent rates */
Zhu Yib481de92007-09-25 17:54:57 -07001900 current_tpt = window->average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07001901 if (low != IWL_RATE_INVALID)
1902 low_tpt = tbl->win[low].average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07001903 if (high != IWL_RATE_INVALID)
1904 high_tpt = tbl->win[high].average_tpt;
1905
Ben Cahill77626352007-11-29 11:09:44 +08001906 /* Assume rate increase */
Zhu Yib481de92007-09-25 17:54:57 -07001907 scale_action = 1;
1908
Ben Cahill77626352007-11-29 11:09:44 +08001909 /* Too many failures, decrease rate */
Zhu Yib481de92007-09-25 17:54:57 -07001910 if ((window->success_ratio <= IWL_RATE_DECREASE_TH) ||
1911 (current_tpt == 0)) {
1912 IWL_DEBUG_RATE("decrease rate because of low success_ratio\n");
1913 scale_action = -1;
Ben Cahill77626352007-11-29 11:09:44 +08001914
1915 /* No throughput measured yet for adjacent rates; try increase. */
Zhu Yib481de92007-09-25 17:54:57 -07001916 } else if ((low_tpt == IWL_INVALID_VALUE) &&
1917 (high_tpt == IWL_INVALID_VALUE))
1918 scale_action = 1;
Ben Cahill77626352007-11-29 11:09:44 +08001919
1920 /* Both adjacent throughputs are measured, but neither one has better
1921 * throughput; we're using the best rate, don't change it! */
Zhu Yib481de92007-09-25 17:54:57 -07001922 else if ((low_tpt != IWL_INVALID_VALUE) &&
1923 (high_tpt != IWL_INVALID_VALUE) &&
1924 (low_tpt < current_tpt) &&
1925 (high_tpt < current_tpt))
1926 scale_action = 0;
Ben Cahill77626352007-11-29 11:09:44 +08001927
1928 /* At least one adjacent rate's throughput is measured,
1929 * and may have better performance. */
Zhu Yib481de92007-09-25 17:54:57 -07001930 else {
Ben Cahill77626352007-11-29 11:09:44 +08001931 /* Higher adjacent rate's throughput is measured */
Zhu Yib481de92007-09-25 17:54:57 -07001932 if (high_tpt != IWL_INVALID_VALUE) {
Ben Cahill77626352007-11-29 11:09:44 +08001933 /* Higher rate has better throughput */
Zhu Yib481de92007-09-25 17:54:57 -07001934 if (high_tpt > current_tpt)
1935 scale_action = 1;
1936 else {
1937 IWL_DEBUG_RATE
1938 ("decrease rate because of high tpt\n");
1939 scale_action = -1;
1940 }
Ben Cahill77626352007-11-29 11:09:44 +08001941
1942 /* Lower adjacent rate's throughput is measured */
Zhu Yib481de92007-09-25 17:54:57 -07001943 } else if (low_tpt != IWL_INVALID_VALUE) {
Ben Cahill77626352007-11-29 11:09:44 +08001944 /* Lower rate has better throughput */
Zhu Yib481de92007-09-25 17:54:57 -07001945 if (low_tpt > current_tpt) {
1946 IWL_DEBUG_RATE
1947 ("decrease rate because of low tpt\n");
1948 scale_action = -1;
1949 } else
1950 scale_action = 1;
1951 }
1952 }
1953
Ben Cahill77626352007-11-29 11:09:44 +08001954 /* Sanity check; asked for decrease, but success rate or throughput
1955 * has been good at old rate. Don't change it. */
Zhu Yib481de92007-09-25 17:54:57 -07001956 if (scale_action == -1) {
1957 if ((low != IWL_RATE_INVALID) &&
1958 ((window->success_ratio > IWL_RATE_HIGH_TH) ||
1959 (current_tpt > (100 * tbl->expected_tpt[low]))))
1960 scale_action = 0;
Ben Cahill77626352007-11-29 11:09:44 +08001961
1962 /* Sanity check; asked for increase, but success rate has not been great
1963 * even at old rate, higher rate will be worse. Don't change it. */
Zhu Yib481de92007-09-25 17:54:57 -07001964 } else if ((scale_action == 1) &&
1965 (window->success_ratio < IWL_RATE_INCREASE_TH))
1966 scale_action = 0;
1967
1968 switch (scale_action) {
1969 case -1:
Ben Cahill77626352007-11-29 11:09:44 +08001970 /* Decrease starting rate, update uCode's rate table */
Zhu Yib481de92007-09-25 17:54:57 -07001971 if (low != IWL_RATE_INVALID) {
1972 update_lq = 1;
1973 index = low;
1974 }
1975 break;
1976 case 1:
Ben Cahill77626352007-11-29 11:09:44 +08001977 /* Increase starting rate, update uCode's rate table */
Zhu Yib481de92007-09-25 17:54:57 -07001978 if (high != IWL_RATE_INVALID) {
1979 update_lq = 1;
1980 index = high;
1981 }
1982
1983 break;
1984 case 0:
Ben Cahill77626352007-11-29 11:09:44 +08001985 /* No change */
Zhu Yib481de92007-09-25 17:54:57 -07001986 default:
1987 break;
1988 }
1989
1990 IWL_DEBUG_HT("choose rate scale index %d action %d low %d "
1991 "high %d type %d\n",
1992 index, scale_action, low, high, tbl->lq_type);
1993
1994 lq_update:
Ben Cahill77626352007-11-29 11:09:44 +08001995 /* Replace uCode's rate table for the destination station. */
Zhu Yib481de92007-09-25 17:54:57 -07001996 if (update_lq) {
1997 rs_mcs_from_tbl(&mcs_rate, tbl, index, is_green);
Tomas Winklerc33104f2008-01-14 17:46:21 -08001998 rs_fill_link_cmd(lq_sta, &mcs_rate, &lq_sta->lq);
Tomas Winkler66c73db2008-04-15 16:01:40 -07001999 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07002000 }
Ben Cahill77626352007-11-29 11:09:44 +08002001
2002 /* Should we stay with this modulation mode, or search for a new one? */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002003 rs_stay_in_table(lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002004
Ben Cahill77626352007-11-29 11:09:44 +08002005 /*
2006 * Search for new modulation mode if we're:
2007 * 1) Not changing rates right now
2008 * 2) Not just finishing up a search
2009 * 3) Allowing a new search
2010 */
Guy Cohen47cfd462008-05-27 11:29:34 +08002011 if (!update_lq && !done_search && !lq_sta->stay_in_tbl && window->counter) {
Ben Cahill77626352007-11-29 11:09:44 +08002012 /* Save current throughput to compare with "search" throughput*/
Tomas Winklerc33104f2008-01-14 17:46:21 -08002013 lq_sta->last_tpt = current_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07002014
Ben Cahill77626352007-11-29 11:09:44 +08002015 /* Select a new "search" modulation mode to try.
2016 * If one is found, set up the new "search" table. */
Zhu Yib481de92007-09-25 17:54:57 -07002017 if (is_legacy(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08002018 rs_move_legacy_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07002019 else if (is_siso(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08002020 rs_move_siso_to_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07002021 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002022 rs_move_mimo_to_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07002023
Ben Cahill77626352007-11-29 11:09:44 +08002024 /* If new "search" mode was selected, set up in uCode table */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002025 if (lq_sta->search_better_tbl) {
Ben Cahill77626352007-11-29 11:09:44 +08002026 /* Access the "search" table, clear its history. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002027 tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Zhu Yib481de92007-09-25 17:54:57 -07002028 for (i = 0; i < IWL_RATE_COUNT; i++)
2029 rs_rate_scale_clear_window(&(tbl->win[i]));
2030
Ben Cahill77626352007-11-29 11:09:44 +08002031 /* Use new "search" start rate */
Tomas Winkler17744ff2008-03-02 01:52:00 +02002032 index = iwl4965_hwrate_to_plcp_idx(
Zhu Yib481de92007-09-25 17:54:57 -07002033 tbl->current_rate.rate_n_flags);
2034
2035 IWL_DEBUG_HT("Switch current mcs: %X index: %d\n",
2036 tbl->current_rate.rate_n_flags, index);
Tomas Winklerc33104f2008-01-14 17:46:21 -08002037 rs_fill_link_cmd(lq_sta, &tbl->current_rate,
2038 &lq_sta->lq);
Tomas Winkler66c73db2008-04-15 16:01:40 -07002039 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07002040 }
Zhu Yib481de92007-09-25 17:54:57 -07002041
Ben Cahill77626352007-11-29 11:09:44 +08002042 /* If the "active" (non-search) mode was legacy,
2043 * and we've tried switching antennas,
2044 * but we haven't been able to try HT modes (not available),
2045 * stay with best antenna legacy modulation for a while
2046 * before next round of mode comparisons. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002047 tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07002048 if (is_legacy(tbl1->lq_type) &&
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002049#ifdef CONFIG_IWL4965_HT
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002050 (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE)) &&
Zhu Yib481de92007-09-25 17:54:57 -07002051#endif
Tomas Winklerc33104f2008-01-14 17:46:21 -08002052 (lq_sta->action_counter >= 1)) {
2053 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002054 IWL_DEBUG_HT("LQ: STAY in legacy table\n");
Tomas Winklerc33104f2008-01-14 17:46:21 -08002055 rs_set_stay_in_table(1, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002056 }
2057
Ben Cahill77626352007-11-29 11:09:44 +08002058 /* If we're in an HT mode, and all 3 mode switch actions
2059 * have been tried and compared, stay in this best modulation
2060 * mode for a while before next round of mode comparisons. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002061 if (lq_sta->enable_counter &&
2062 (lq_sta->action_counter >= IWL_ACTION_LIMIT)) {
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002063#ifdef CONFIG_IWL4965_HT
2064 if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
2065 (lq_sta->tx_agg_tid_en & (1 << tid)) &&
2066 (tid != MAX_TID_COUNT)) {
2067 IWL_DEBUG_HT("try to aggregate tid %d\n", tid);
2068 rs_tl_turn_on_agg(priv, tid, lq_sta, sta);
Zhu Yib481de92007-09-25 17:54:57 -07002069 }
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002070#endif /*CONFIG_IWL4965_HT */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002071 lq_sta->action_counter = 0;
2072 rs_set_stay_in_table(0, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002073 }
Ben Cahill77626352007-11-29 11:09:44 +08002074
2075 /*
2076 * Else, don't search for a new modulation mode.
2077 * Put new timestamp in stay-in-modulation-mode flush timer if:
2078 * 1) Not changing rates right now
2079 * 2) Not just finishing up a search
2080 * 3) flush timer is empty
2081 */
Zhu Yib481de92007-09-25 17:54:57 -07002082 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002083 if ((!update_lq) && (!done_search) && (!lq_sta->flush_timer))
2084 lq_sta->flush_timer = jiffies;
Zhu Yib481de92007-09-25 17:54:57 -07002085 }
2086
2087out:
2088 rs_mcs_from_tbl(&tbl->current_rate, tbl, index, is_green);
2089 i = index;
Johannes Berg8318d782008-01-24 19:38:38 +01002090 sta->last_txrate_idx = i;
Zhu Yib481de92007-09-25 17:54:57 -07002091
Johannes Berg8318d782008-01-24 19:38:38 +01002092 /* sta->txrate_idx is an index to A mode rates which start
Zhu Yib481de92007-09-25 17:54:57 -07002093 * at IWL_FIRST_OFDM_RATE
2094 */
Johannes Berg8318d782008-01-24 19:38:38 +01002095 if (lq_sta->band == IEEE80211_BAND_5GHZ)
2096 sta->txrate_idx = i - IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -07002097 else
Johannes Berg8318d782008-01-24 19:38:38 +01002098 sta->txrate_idx = i;
Zhu Yib481de92007-09-25 17:54:57 -07002099
2100 return;
2101}
2102
2103
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002104static void rs_initialize_lq(struct iwl_priv *priv,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002105 struct ieee80211_conf *conf,
Zhu Yib481de92007-09-25 17:54:57 -07002106 struct sta_info *sta)
2107{
2108 int i;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002109 struct iwl4965_lq_sta *lq_sta;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002110 struct iwl4965_scale_tbl_info *tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002111 u8 active_tbl = 0;
2112 int rate_idx;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002113 u8 use_green = rs_use_green(priv, conf);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002114 struct iwl4965_rate mcs_rate;
Zhu Yib481de92007-09-25 17:54:57 -07002115
2116 if (!sta || !sta->rate_ctrl_priv)
2117 goto out;
2118
Tomas Winklerc33104f2008-01-14 17:46:21 -08002119 lq_sta = (struct iwl4965_lq_sta *)sta->rate_ctrl_priv;
Johannes Berg8318d782008-01-24 19:38:38 +01002120 i = sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002121
Tomas Winklerc33104f2008-01-14 17:46:21 -08002122 if ((lq_sta->lq.sta_id == 0xff) &&
Zhu Yib481de92007-09-25 17:54:57 -07002123 (priv->iw_mode == IEEE80211_IF_TYPE_IBSS))
2124 goto out;
2125
Tomas Winklerc33104f2008-01-14 17:46:21 -08002126 if (!lq_sta->search_better_tbl)
2127 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002128 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002129 active_tbl = 1 - lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002130
Tomas Winklerc33104f2008-01-14 17:46:21 -08002131 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07002132
2133 if ((i < 0) || (i >= IWL_RATE_COUNT))
2134 i = 0;
2135
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002136 mcs_rate.rate_n_flags = iwl4965_rates[i].plcp ;
Zhu Yib481de92007-09-25 17:54:57 -07002137 mcs_rate.rate_n_flags |= RATE_MCS_ANT_B_MSK;
2138 mcs_rate.rate_n_flags &= ~RATE_MCS_ANT_A_MSK;
2139
2140 if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
2141 mcs_rate.rate_n_flags |= RATE_MCS_CCK_MSK;
2142
2143 tbl->antenna_type = ANT_AUX;
Johannes Berg8318d782008-01-24 19:38:38 +01002144 rs_get_tbl_info_from_mcs(&mcs_rate, priv->band, tbl, &rate_idx);
Zhu Yib481de92007-09-25 17:54:57 -07002145 if (!rs_is_ant_connected(priv->valid_antenna, tbl->antenna_type))
Zhu Yi46640a82007-09-27 11:27:34 +08002146 rs_toggle_antenna(&mcs_rate, tbl);
Zhu Yib481de92007-09-25 17:54:57 -07002147
2148 rs_mcs_from_tbl(&mcs_rate, tbl, rate_idx, use_green);
2149 tbl->current_rate.rate_n_flags = mcs_rate.rate_n_flags;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002150 rs_get_expected_tpt_table(lq_sta, tbl);
2151 rs_fill_link_cmd(lq_sta, &mcs_rate, &lq_sta->lq);
Tomas Winkler66c73db2008-04-15 16:01:40 -07002152 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07002153 out:
2154 return;
2155}
2156
Mattias Nissler1abbe492007-12-20 13:50:07 +01002157static void rs_get_rate(void *priv_rate, struct net_device *dev,
Johannes Berg8318d782008-01-24 19:38:38 +01002158 struct ieee80211_supported_band *sband,
2159 struct sk_buff *skb,
Mattias Nissler1abbe492007-12-20 13:50:07 +01002160 struct rate_selection *sel)
Zhu Yib481de92007-09-25 17:54:57 -07002161{
2162
2163 int i;
2164 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002165 struct ieee80211_conf *conf = &local->hw.conf;
Zhu Yib481de92007-09-25 17:54:57 -07002166 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2167 struct sta_info *sta;
Michael Wu2bc454b2007-12-25 19:33:16 -05002168 u16 fc;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002169 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002170 struct iwl4965_lq_sta *lq_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002171
Zhu Yi58826352007-09-27 11:27:39 +08002172 IWL_DEBUG_RATE_LIMIT("rate scale calculate new rate for skb\n");
Zhu Yib481de92007-09-25 17:54:57 -07002173
Johannes Bergd0709a62008-02-25 16:27:46 +01002174 rcu_read_lock();
2175
Zhu Yib481de92007-09-25 17:54:57 -07002176 sta = sta_info_get(local, hdr->addr1);
2177
Michael Wu2bc454b2007-12-25 19:33:16 -05002178 /* Send management frames and broadcast/multicast data using lowest
2179 * rate. */
2180 fc = le16_to_cpu(hdr->frame_control);
2181 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1) ||
2182 !sta || !sta->rate_ctrl_priv) {
Johannes Berg8318d782008-01-24 19:38:38 +01002183 sel->rate = rate_lowest(local, sband, sta);
Tomas Winklerf4d60822008-03-05 11:31:00 -08002184 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07002185 }
2186
Tomas Winklerc33104f2008-01-14 17:46:21 -08002187 lq_sta = (struct iwl4965_lq_sta *)sta->rate_ctrl_priv;
Johannes Berg8318d782008-01-24 19:38:38 +01002188 i = sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002189
Tomas Winklerc33104f2008-01-14 17:46:21 -08002190 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
2191 !lq_sta->ibss_sta_added) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002192 u8 sta_id = iwl4965_hw_find_station(priv, hdr->addr1);
Joe Perches0795af52007-10-03 17:59:30 -07002193 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07002194
2195 if (sta_id == IWL_INVALID_STATION) {
Joe Perches0795af52007-10-03 17:59:30 -07002196 IWL_DEBUG_RATE("LQ: ADD station %s\n",
2197 print_mac(mac, hdr->addr1));
Ron Rindjunsky67d62032007-11-26 16:14:40 +02002198 sta_id = iwl4965_add_station_flags(priv, hdr->addr1,
2199 0, CMD_ASYNC, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07002200 }
2201 if ((sta_id != IWL_INVALID_STATION)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002202 lq_sta->lq.sta_id = sta_id;
2203 lq_sta->lq.rs_table[0].rate_n_flags = 0;
2204 lq_sta->ibss_sta_added = 1;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002205 rs_initialize_lq(priv, conf, sta);
Zhu Yib481de92007-09-25 17:54:57 -07002206 }
Tomas Winklerc33104f2008-01-14 17:46:21 -08002207 if (!lq_sta->ibss_sta_added)
Zhu Yib481de92007-09-25 17:54:57 -07002208 goto done;
2209 }
2210
Tomas Winklerf4d60822008-03-05 11:31:00 -08002211done:
Mattias Nissler1abbe492007-12-20 13:50:07 +01002212 if ((i < 0) || (i > IWL_RATE_COUNT)) {
Johannes Berg8318d782008-01-24 19:38:38 +01002213 sel->rate = rate_lowest(local, sband, sta);
Tomas Winklerf4d60822008-03-05 11:31:00 -08002214 goto out;
Mattias Nissler1abbe492007-12-20 13:50:07 +01002215 }
Zhu Yib481de92007-09-25 17:54:57 -07002216
Mattias Nissler1abbe492007-12-20 13:50:07 +01002217 sel->rate = &priv->ieee_rates[i];
Tomas Winklerf4d60822008-03-05 11:31:00 -08002218out:
2219 rcu_read_unlock();
Zhu Yib481de92007-09-25 17:54:57 -07002220}
2221
2222static void *rs_alloc_sta(void *priv, gfp_t gfp)
2223{
Tomas Winklerc33104f2008-01-14 17:46:21 -08002224 struct iwl4965_lq_sta *lq_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002225 int i, j;
2226
2227 IWL_DEBUG_RATE("create station rate scale window\n");
2228
Tomas Winklerc33104f2008-01-14 17:46:21 -08002229 lq_sta = kzalloc(sizeof(struct iwl4965_lq_sta), gfp);
Zhu Yib481de92007-09-25 17:54:57 -07002230
Tomas Winklerc33104f2008-01-14 17:46:21 -08002231 if (lq_sta == NULL)
Zhu Yib481de92007-09-25 17:54:57 -07002232 return NULL;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002233 lq_sta->lq.sta_id = 0xff;
Zhu Yib481de92007-09-25 17:54:57 -07002234
Zhu Yi63fddb92007-09-27 11:27:36 +08002235
Zhu Yib481de92007-09-25 17:54:57 -07002236 for (j = 0; j < LQ_SIZE; j++)
2237 for (i = 0; i < IWL_RATE_COUNT; i++)
Tomas Winklerc33104f2008-01-14 17:46:21 -08002238 rs_rate_scale_clear_window(&(lq_sta->lq_info[j].win[i]));
Zhu Yib481de92007-09-25 17:54:57 -07002239
Tomas Winklerc33104f2008-01-14 17:46:21 -08002240 return lq_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002241}
2242
2243static void rs_rate_init(void *priv_rate, void *priv_sta,
2244 struct ieee80211_local *local,
2245 struct sta_info *sta)
2246{
2247 int i, j;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002248 struct ieee80211_conf *conf = &local->hw.conf;
Johannes Berg8318d782008-01-24 19:38:38 +01002249 struct ieee80211_supported_band *sband;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002250 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002251 struct iwl4965_lq_sta *lq_sta = priv_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002252
Johannes Berg8318d782008-01-24 19:38:38 +01002253 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2254
Tomas Winklerc33104f2008-01-14 17:46:21 -08002255 lq_sta->flush_timer = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01002256 lq_sta->supp_rates = sta->supp_rates[sband->band];
2257 sta->txrate_idx = 3;
Zhu Yib481de92007-09-25 17:54:57 -07002258 for (j = 0; j < LQ_SIZE; j++)
2259 for (i = 0; i < IWL_RATE_COUNT; i++)
Tomas Winklerc33104f2008-01-14 17:46:21 -08002260 rs_rate_scale_clear_window(&(lq_sta->lq_info[j].win[i]));
Zhu Yib481de92007-09-25 17:54:57 -07002261
2262 IWL_DEBUG_RATE("rate scale global init\n");
2263 /* TODO: what is a good starting rate for STA? About middle? Maybe not
2264 * the lowest or the highest rate.. Could consider using RSSI from
2265 * previous packets? Need to have IEEE 802.1X auth succeed immediately
2266 * after assoc.. */
2267
Tomas Winklerc33104f2008-01-14 17:46:21 -08002268 lq_sta->ibss_sta_added = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002269 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002270 u8 sta_id = iwl4965_hw_find_station(priv, sta->addr);
Joe Perches0795af52007-10-03 17:59:30 -07002271 DECLARE_MAC_BUF(mac);
2272
Zhu Yib481de92007-09-25 17:54:57 -07002273 /* for IBSS the call are from tasklet */
Joe Perches0795af52007-10-03 17:59:30 -07002274 IWL_DEBUG_HT("LQ: ADD station %s\n",
2275 print_mac(mac, sta->addr));
Zhu Yib481de92007-09-25 17:54:57 -07002276
2277 if (sta_id == IWL_INVALID_STATION) {
Joe Perches0795af52007-10-03 17:59:30 -07002278 IWL_DEBUG_RATE("LQ: ADD station %s\n",
2279 print_mac(mac, sta->addr));
Ron Rindjunsky67d62032007-11-26 16:14:40 +02002280 sta_id = iwl4965_add_station_flags(priv, sta->addr,
2281 0, CMD_ASYNC, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07002282 }
2283 if ((sta_id != IWL_INVALID_STATION)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002284 lq_sta->lq.sta_id = sta_id;
2285 lq_sta->lq.rs_table[0].rate_n_flags = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002286 }
2287 /* FIXME: this is w/a remove it later */
2288 priv->assoc_station_added = 1;
2289 }
2290
Ben Cahill77626352007-11-29 11:09:44 +08002291 /* Find highest tx rate supported by hardware and destination station */
Johannes Berg8318d782008-01-24 19:38:38 +01002292 for (i = 0; i < sband->n_bitrates; i++)
2293 if (sta->supp_rates[sband->band] & BIT(i))
2294 sta->txrate_idx = i;
2295
2296 sta->last_txrate_idx = sta->txrate_idx;
2297 /* WTF is with this bogus comment? A doesn't have cck rates */
Ben Cahill77626352007-11-29 11:09:44 +08002298 /* For MODE_IEEE80211A, cck rates are at end of rate table */
Johannes Berg8318d782008-01-24 19:38:38 +01002299 if (local->hw.conf.channel->band == IEEE80211_BAND_5GHZ)
2300 sta->last_txrate_idx += IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -07002301
Tomas Winklerc33104f2008-01-14 17:46:21 -08002302 lq_sta->is_dup = 0;
2303 lq_sta->valid_antenna = priv->valid_antenna;
2304 lq_sta->antenna = priv->antenna;
2305 lq_sta->is_green = rs_use_green(priv, conf);
2306 lq_sta->active_rate = priv->active_rate;
2307 lq_sta->active_rate &= ~(0x1000);
2308 lq_sta->active_rate_basic = priv->active_rate_basic;
Johannes Berg8318d782008-01-24 19:38:38 +01002309 lq_sta->band = priv->band;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002310#ifdef CONFIG_IWL4965_HT
Ben Cahill77626352007-11-29 11:09:44 +08002311 /*
2312 * active_siso_rate mask includes 9 MBits (bit 5), and CCK (bits 0-3),
2313 * supp_rates[] does not; shift to convert format, force 9 MBits off.
2314 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002315 lq_sta->active_siso_rate = (priv->current_ht_config.supp_mcs_set[0] << 1);
2316 lq_sta->active_siso_rate |=
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002317 (priv->current_ht_config.supp_mcs_set[0] & 0x1);
Tomas Winklerc33104f2008-01-14 17:46:21 -08002318 lq_sta->active_siso_rate &= ~((u16)0x2);
2319 lq_sta->active_siso_rate =
2320 lq_sta->active_siso_rate << IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -07002321
Ben Cahill77626352007-11-29 11:09:44 +08002322 /* Same here */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002323 lq_sta->active_mimo_rate = (priv->current_ht_config.supp_mcs_set[1] << 1);
2324 lq_sta->active_mimo_rate |=
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002325 (priv->current_ht_config.supp_mcs_set[1] & 0x1);
Tomas Winklerc33104f2008-01-14 17:46:21 -08002326 lq_sta->active_mimo_rate &= ~((u16)0x2);
2327 lq_sta->active_mimo_rate =
2328 lq_sta->active_mimo_rate << IWL_FIRST_OFDM_RATE;
2329 IWL_DEBUG_HT("SISO RATE 0x%X MIMO RATE 0x%X\n",
2330 lq_sta->active_siso_rate,
2331 lq_sta->active_mimo_rate);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002332 /* as default allow aggregation for all tids */
2333 lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002334#endif /*CONFIG_IWL4965_HT*/
Zhu Yi98d7e092007-09-27 11:27:42 +08002335#ifdef CONFIG_MAC80211_DEBUGFS
Tomas Winklerc33104f2008-01-14 17:46:21 -08002336 lq_sta->drv = priv;
Zhu Yi98d7e092007-09-27 11:27:42 +08002337#endif
Zhu Yib481de92007-09-25 17:54:57 -07002338
2339 if (priv->assoc_station_added)
2340 priv->lq_mngr.lq_ready = 1;
2341
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002342 rs_initialize_lq(priv, conf, sta);
Zhu Yib481de92007-09-25 17:54:57 -07002343}
2344
Tomas Winklerc33104f2008-01-14 17:46:21 -08002345static void rs_fill_link_cmd(struct iwl4965_lq_sta *lq_sta,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002346 struct iwl4965_rate *tx_mcs,
Tomas Winkler66c73db2008-04-15 16:01:40 -07002347 struct iwl_link_quality_cmd *lq_cmd)
Zhu Yib481de92007-09-25 17:54:57 -07002348{
2349 int index = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002350 int rate_idx;
Zhu Yi1b696de2007-09-27 11:27:41 +08002351 int repeat_rate = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002352 u8 ant_toggle_count = 0;
2353 u8 use_ht_possible = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002354 struct iwl4965_rate new_rate;
2355 struct iwl4965_scale_tbl_info tbl_type = { 0 };
Zhu Yib481de92007-09-25 17:54:57 -07002356
Ben Cahill77626352007-11-29 11:09:44 +08002357 /* Override starting rate (index 0) if needed for debug purposes */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002358 rs_dbgfs_set_mcs(lq_sta, tx_mcs, index);
Zhu Yi98d7e092007-09-27 11:27:42 +08002359
Ben Cahill77626352007-11-29 11:09:44 +08002360 /* Interpret rate_n_flags */
Johannes Berg8318d782008-01-24 19:38:38 +01002361 rs_get_tbl_info_from_mcs(tx_mcs, lq_sta->band,
Zhu Yib481de92007-09-25 17:54:57 -07002362 &tbl_type, &rate_idx);
2363
Ben Cahill77626352007-11-29 11:09:44 +08002364 /* How many times should we repeat the initial rate? */
Zhu Yib481de92007-09-25 17:54:57 -07002365 if (is_legacy(tbl_type.lq_type)) {
2366 ant_toggle_count = 1;
Zhu Yi1b696de2007-09-27 11:27:41 +08002367 repeat_rate = IWL_NUMBER_TRY;
Zhu Yib481de92007-09-25 17:54:57 -07002368 } else
Zhu Yi1b696de2007-09-27 11:27:41 +08002369 repeat_rate = IWL_HT_NUMBER_TRY;
Zhu Yib481de92007-09-25 17:54:57 -07002370
2371 lq_cmd->general_params.mimo_delimiter =
2372 is_mimo(tbl_type.lq_type) ? 1 : 0;
Ben Cahill77626352007-11-29 11:09:44 +08002373
2374 /* Fill 1st table entry (index 0) */
Zhu Yib481de92007-09-25 17:54:57 -07002375 lq_cmd->rs_table[index].rate_n_flags =
2376 cpu_to_le32(tx_mcs->rate_n_flags);
2377 new_rate.rate_n_flags = tx_mcs->rate_n_flags;
2378
2379 if (is_mimo(tbl_type.lq_type) || (tbl_type.antenna_type == ANT_MAIN))
Ben Cahill77626352007-11-29 11:09:44 +08002380 lq_cmd->general_params.single_stream_ant_msk
2381 = LINK_QUAL_ANT_A_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002382 else
Ben Cahill77626352007-11-29 11:09:44 +08002383 lq_cmd->general_params.single_stream_ant_msk
2384 = LINK_QUAL_ANT_B_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002385
2386 index++;
Zhu Yi1b696de2007-09-27 11:27:41 +08002387 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002388
Ben Cahill77626352007-11-29 11:09:44 +08002389 /* Fill rest of rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002390 while (index < LINK_QUAL_MAX_RETRY_NUM) {
Ben Cahill77626352007-11-29 11:09:44 +08002391 /* Repeat initial/next rate.
2392 * For legacy IWL_NUMBER_TRY == 1, this loop will not execute.
2393 * For HT IWL_HT_NUMBER_TRY == 3, this executes twice. */
Zhu Yi1b696de2007-09-27 11:27:41 +08002394 while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
Zhu Yib481de92007-09-25 17:54:57 -07002395 if (is_legacy(tbl_type.lq_type)) {
2396 if (ant_toggle_count <
2397 NUM_TRY_BEFORE_ANTENNA_TOGGLE)
2398 ant_toggle_count++;
2399 else {
2400 rs_toggle_antenna(&new_rate, &tbl_type);
2401 ant_toggle_count = 1;
2402 }
2403 }
Zhu Yi98d7e092007-09-27 11:27:42 +08002404
Ben Cahill77626352007-11-29 11:09:44 +08002405 /* Override next rate if needed for debug purposes */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002406 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Ben Cahill77626352007-11-29 11:09:44 +08002407
2408 /* Fill next table entry */
Zhu Yib481de92007-09-25 17:54:57 -07002409 lq_cmd->rs_table[index].rate_n_flags =
2410 cpu_to_le32(new_rate.rate_n_flags);
Zhu Yi1b696de2007-09-27 11:27:41 +08002411 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002412 index++;
2413 }
2414
Johannes Berg8318d782008-01-24 19:38:38 +01002415 rs_get_tbl_info_from_mcs(&new_rate, lq_sta->band, &tbl_type,
Zhu Yib481de92007-09-25 17:54:57 -07002416 &rate_idx);
2417
Ben Cahill77626352007-11-29 11:09:44 +08002418 /* Indicate to uCode which entries might be MIMO.
2419 * If initial rate was MIMO, this will finally end up
2420 * as (IWL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
Zhu Yib481de92007-09-25 17:54:57 -07002421 if (is_mimo(tbl_type.lq_type))
2422 lq_cmd->general_params.mimo_delimiter = index;
2423
Ben Cahill77626352007-11-29 11:09:44 +08002424 /* Get next rate */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002425 rs_get_lower_rate(lq_sta, &tbl_type, rate_idx,
Zhu Yi02dede02007-09-27 11:27:40 +08002426 use_ht_possible, &new_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002427
Ben Cahill77626352007-11-29 11:09:44 +08002428 /* How many times should we repeat the next rate? */
Zhu Yib481de92007-09-25 17:54:57 -07002429 if (is_legacy(tbl_type.lq_type)) {
2430 if (ant_toggle_count < NUM_TRY_BEFORE_ANTENNA_TOGGLE)
2431 ant_toggle_count++;
2432 else {
2433 rs_toggle_antenna(&new_rate, &tbl_type);
2434 ant_toggle_count = 1;
2435 }
Zhu Yi1b696de2007-09-27 11:27:41 +08002436 repeat_rate = IWL_NUMBER_TRY;
Zhu Yib481de92007-09-25 17:54:57 -07002437 } else
Zhu Yi1b696de2007-09-27 11:27:41 +08002438 repeat_rate = IWL_HT_NUMBER_TRY;
Zhu Yib481de92007-09-25 17:54:57 -07002439
Ben Cahill77626352007-11-29 11:09:44 +08002440 /* Don't allow HT rates after next pass.
2441 * rs_get_lower_rate() will change type to LQ_A or LQ_G. */
Zhu Yib481de92007-09-25 17:54:57 -07002442 use_ht_possible = 0;
2443
Ben Cahill77626352007-11-29 11:09:44 +08002444 /* Override next rate if needed for debug purposes */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002445 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Ben Cahill77626352007-11-29 11:09:44 +08002446
2447 /* Fill next table entry */
Zhu Yib481de92007-09-25 17:54:57 -07002448 lq_cmd->rs_table[index].rate_n_flags =
2449 cpu_to_le32(new_rate.rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -07002450
2451 index++;
Zhu Yi1b696de2007-09-27 11:27:41 +08002452 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002453 }
2454
Zhu Yib481de92007-09-25 17:54:57 -07002455 lq_cmd->general_params.dual_stream_ant_msk = 3;
2456 lq_cmd->agg_params.agg_dis_start_th = 3;
2457 lq_cmd->agg_params.agg_time_limit = cpu_to_le16(4000);
Zhu Yib481de92007-09-25 17:54:57 -07002458}
2459
2460static void *rs_alloc(struct ieee80211_local *local)
2461{
2462 return local->hw.priv;
2463}
2464/* rate scale requires free function to be implemented */
2465static void rs_free(void *priv_rate)
2466{
2467 return;
2468}
2469
2470static void rs_clear(void *priv_rate)
2471{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002472 struct iwl_priv *priv = (struct iwl_priv *) priv_rate;
Zhu Yib481de92007-09-25 17:54:57 -07002473
2474 IWL_DEBUG_RATE("enter\n");
2475
2476 priv->lq_mngr.lq_ready = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002477
2478 IWL_DEBUG_RATE("leave\n");
2479}
2480
2481static void rs_free_sta(void *priv, void *priv_sta)
2482{
Tomas Winklerc33104f2008-01-14 17:46:21 -08002483 struct iwl4965_lq_sta *lq_sta = priv_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002484
2485 IWL_DEBUG_RATE("enter\n");
Tomas Winklerc33104f2008-01-14 17:46:21 -08002486 kfree(lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002487 IWL_DEBUG_RATE("leave\n");
2488}
2489
2490
Zhu Yi93dc6462007-09-27 11:27:37 +08002491#ifdef CONFIG_MAC80211_DEBUGFS
Zhu Yi5ae212c2007-09-27 11:27:38 +08002492static int open_file_generic(struct inode *inode, struct file *file)
2493{
2494 file->private_data = inode->i_private;
2495 return 0;
2496}
Tomas Winklerc33104f2008-01-14 17:46:21 -08002497static void rs_dbgfs_set_mcs(struct iwl4965_lq_sta *lq_sta,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002498 struct iwl4965_rate *mcs, int index)
Zhu Yi98d7e092007-09-27 11:27:42 +08002499{
Ron Rindjunsky4457e1a2007-10-15 14:40:56 +02002500 u32 base_rate;
2501
Johannes Berg8318d782008-01-24 19:38:38 +01002502 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Ron Rindjunsky4457e1a2007-10-15 14:40:56 +02002503 base_rate = 0x800D;
2504 else
2505 base_rate = 0x820A;
2506
Tomas Winklerc33104f2008-01-14 17:46:21 -08002507 if (lq_sta->dbg_fixed.rate_n_flags) {
Zhu Yi98d7e092007-09-27 11:27:42 +08002508 if (index < 12)
Tomas Winklerc33104f2008-01-14 17:46:21 -08002509 mcs->rate_n_flags = lq_sta->dbg_fixed.rate_n_flags;
Zhu Yi98d7e092007-09-27 11:27:42 +08002510 else
Ron Rindjunsky4457e1a2007-10-15 14:40:56 +02002511 mcs->rate_n_flags = base_rate;
Zhu Yi98d7e092007-09-27 11:27:42 +08002512 IWL_DEBUG_RATE("Fixed rate ON\n");
2513 return;
2514 }
Zhu Yi5ae212c2007-09-27 11:27:38 +08002515
Zhu Yi98d7e092007-09-27 11:27:42 +08002516 IWL_DEBUG_RATE("Fixed rate OFF\n");
2517}
2518
2519static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
2520 const char __user *user_buf, size_t count, loff_t *ppos)
2521{
Tomas Winklerc33104f2008-01-14 17:46:21 -08002522 struct iwl4965_lq_sta *lq_sta = file->private_data;
Zhu Yi98d7e092007-09-27 11:27:42 +08002523 char buf[64];
2524 int buf_size;
2525 u32 parsed_rate;
2526
2527 memset(buf, 0, sizeof(buf));
2528 buf_size = min(count, sizeof(buf) - 1);
2529 if (copy_from_user(buf, user_buf, buf_size))
2530 return -EFAULT;
2531
2532 if (sscanf(buf, "%x", &parsed_rate) == 1)
Tomas Winklerc33104f2008-01-14 17:46:21 -08002533 lq_sta->dbg_fixed.rate_n_flags = parsed_rate;
Zhu Yi98d7e092007-09-27 11:27:42 +08002534 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002535 lq_sta->dbg_fixed.rate_n_flags = 0;
Zhu Yi98d7e092007-09-27 11:27:42 +08002536
Tomas Winklerc33104f2008-01-14 17:46:21 -08002537 lq_sta->active_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
2538 lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2539 lq_sta->active_mimo_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
Zhu Yi98d7e092007-09-27 11:27:42 +08002540
2541 IWL_DEBUG_RATE("sta_id %d rate 0x%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002542 lq_sta->lq.sta_id, lq_sta->dbg_fixed.rate_n_flags);
Zhu Yi98d7e092007-09-27 11:27:42 +08002543
Tomas Winklerc33104f2008-01-14 17:46:21 -08002544 if (lq_sta->dbg_fixed.rate_n_flags) {
2545 rs_fill_link_cmd(lq_sta, &lq_sta->dbg_fixed, &lq_sta->lq);
Tomas Winkler66c73db2008-04-15 16:01:40 -07002546 iwl_send_lq_cmd(lq_sta->drv, &lq_sta->lq, CMD_ASYNC);
Zhu Yi98d7e092007-09-27 11:27:42 +08002547 }
2548
2549 return count;
2550}
Zhu Yi0209dc12007-09-27 11:27:43 +08002551
Zhu Yi5ae212c2007-09-27 11:27:38 +08002552static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
2553 char __user *user_buf, size_t count, loff_t *ppos)
2554{
2555 char buff[1024];
2556 int desc = 0;
2557 int i = 0;
2558
Tomas Winklerc33104f2008-01-14 17:46:21 -08002559 struct iwl4965_lq_sta *lq_sta = file->private_data;
Zhu Yi5ae212c2007-09-27 11:27:38 +08002560
Tomas Winklerc33104f2008-01-14 17:46:21 -08002561 desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
Zhu Yi98d7e092007-09-27 11:27:42 +08002562 desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002563 lq_sta->total_failed, lq_sta->total_success,
2564 lq_sta->active_rate);
Zhu Yi98d7e092007-09-27 11:27:42 +08002565 desc += sprintf(buff+desc, "fixed rate 0x%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002566 lq_sta->dbg_fixed.rate_n_flags);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002567 desc += sprintf(buff+desc, "general:"
2568 "flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002569 lq_sta->lq.general_params.flags,
2570 lq_sta->lq.general_params.mimo_delimiter,
2571 lq_sta->lq.general_params.single_stream_ant_msk,
2572 lq_sta->lq.general_params.dual_stream_ant_msk);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002573
2574 desc += sprintf(buff+desc, "agg:"
2575 "time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002576 le16_to_cpu(lq_sta->lq.agg_params.agg_time_limit),
2577 lq_sta->lq.agg_params.agg_dis_start_th,
2578 lq_sta->lq.agg_params.agg_frame_cnt_limit);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002579
2580 desc += sprintf(buff+desc,
2581 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002582 lq_sta->lq.general_params.start_rate_index[0],
2583 lq_sta->lq.general_params.start_rate_index[1],
2584 lq_sta->lq.general_params.start_rate_index[2],
2585 lq_sta->lq.general_params.start_rate_index[3]);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002586
2587
2588 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
2589 desc += sprintf(buff+desc, " rate[%d] 0x%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002590 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags));
Zhu Yi5ae212c2007-09-27 11:27:38 +08002591
2592 return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2593}
2594
2595static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
Zhu Yi98d7e092007-09-27 11:27:42 +08002596 .write = rs_sta_dbgfs_scale_table_write,
Zhu Yi5ae212c2007-09-27 11:27:38 +08002597 .read = rs_sta_dbgfs_scale_table_read,
2598 .open = open_file_generic,
2599};
Zhu Yi0209dc12007-09-27 11:27:43 +08002600static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
2601 char __user *user_buf, size_t count, loff_t *ppos)
2602{
2603 char buff[1024];
2604 int desc = 0;
2605 int i, j;
2606
Tomas Winklerc33104f2008-01-14 17:46:21 -08002607 struct iwl4965_lq_sta *lq_sta = file->private_data;
Zhu Yi0209dc12007-09-27 11:27:43 +08002608 for (i = 0; i < LQ_SIZE; i++) {
2609 desc += sprintf(buff+desc, "%s type=%d SGI=%d FAT=%d DUP=%d\n"
2610 "rate=0x%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002611 lq_sta->active_tbl == i?"*":"x",
2612 lq_sta->lq_info[i].lq_type,
2613 lq_sta->lq_info[i].is_SGI,
2614 lq_sta->lq_info[i].is_fat,
2615 lq_sta->lq_info[i].is_dup,
2616 lq_sta->lq_info[i].current_rate.rate_n_flags);
Zhu Yi0209dc12007-09-27 11:27:43 +08002617 for (j = 0; j < IWL_RATE_COUNT; j++) {
2618 desc += sprintf(buff+desc,
Tomas Winklerc33104f2008-01-14 17:46:21 -08002619 "counter=%d success=%d %%=%d\n",
2620 lq_sta->lq_info[i].win[j].counter,
2621 lq_sta->lq_info[i].win[j].success_counter,
2622 lq_sta->lq_info[i].win[j].success_ratio);
Zhu Yi0209dc12007-09-27 11:27:43 +08002623 }
2624 }
2625 return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2626}
2627
2628static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
2629 .read = rs_sta_dbgfs_stats_table_read,
2630 .open = open_file_generic,
2631};
Zhu Yi5ae212c2007-09-27 11:27:38 +08002632
Zhu Yi93dc6462007-09-27 11:27:37 +08002633static void rs_add_debugfs(void *priv, void *priv_sta,
2634 struct dentry *dir)
2635{
Tomas Winklerc33104f2008-01-14 17:46:21 -08002636 struct iwl4965_lq_sta *lq_sta = priv_sta;
2637 lq_sta->rs_sta_dbgfs_scale_table_file =
Zhu Yi0209dc12007-09-27 11:27:43 +08002638 debugfs_create_file("rate_scale_table", 0600, dir,
Tomas Winklerc33104f2008-01-14 17:46:21 -08002639 lq_sta, &rs_sta_dbgfs_scale_table_ops);
2640 lq_sta->rs_sta_dbgfs_stats_table_file =
Zhu Yi0209dc12007-09-27 11:27:43 +08002641 debugfs_create_file("rate_stats_table", 0600, dir,
Tomas Winklerc33104f2008-01-14 17:46:21 -08002642 lq_sta, &rs_sta_dbgfs_stats_table_ops);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002643#ifdef CONFIG_IWL4965_HT
2644 lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file =
2645 debugfs_create_u8("tx_agg_tid_enable", 0600, dir,
2646 &lq_sta->tx_agg_tid_en);
2647#endif
2648
Zhu Yi93dc6462007-09-27 11:27:37 +08002649}
2650
2651static void rs_remove_debugfs(void *priv, void *priv_sta)
2652{
Tomas Winklerc33104f2008-01-14 17:46:21 -08002653 struct iwl4965_lq_sta *lq_sta = priv_sta;
2654 debugfs_remove(lq_sta->rs_sta_dbgfs_scale_table_file);
2655 debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002656#ifdef CONFIG_IWL4965_HT
2657 debugfs_remove(lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file);
2658#endif
Zhu Yi93dc6462007-09-27 11:27:37 +08002659}
2660#endif
2661
Zhu Yib481de92007-09-25 17:54:57 -07002662static struct rate_control_ops rs_ops = {
2663 .module = NULL,
2664 .name = RS_NAME,
2665 .tx_status = rs_tx_status,
2666 .get_rate = rs_get_rate,
2667 .rate_init = rs_rate_init,
2668 .clear = rs_clear,
2669 .alloc = rs_alloc,
2670 .free = rs_free,
2671 .alloc_sta = rs_alloc_sta,
2672 .free_sta = rs_free_sta,
Zhu Yi93dc6462007-09-27 11:27:37 +08002673#ifdef CONFIG_MAC80211_DEBUGFS
2674 .add_sta_debugfs = rs_add_debugfs,
2675 .remove_sta_debugfs = rs_remove_debugfs,
2676#endif
Zhu Yib481de92007-09-25 17:54:57 -07002677};
2678
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002679int iwl4965_fill_rs_info(struct ieee80211_hw *hw, char *buf, u8 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07002680{
2681 struct ieee80211_local *local = hw_to_local(hw);
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002682 struct iwl_priv *priv = hw->priv;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002683 struct iwl4965_lq_sta *lq_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002684 struct sta_info *sta;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002685 int cnt = 0, i;
Zhu Yib481de92007-09-25 17:54:57 -07002686 u32 samples = 0, success = 0, good = 0;
2687 unsigned long now = jiffies;
2688 u32 max_time = 0;
2689 u8 lq_type, antenna;
2690
Johannes Bergd0709a62008-02-25 16:27:46 +01002691 rcu_read_lock();
2692
Zhu Yib481de92007-09-25 17:54:57 -07002693 sta = sta_info_get(local, priv->stations[sta_id].sta.sta.addr);
2694 if (!sta || !sta->rate_ctrl_priv) {
Johannes Bergd0709a62008-02-25 16:27:46 +01002695 if (sta)
Zhu Yib481de92007-09-25 17:54:57 -07002696 IWL_DEBUG_RATE("leave - no private rate data!\n");
Johannes Bergd0709a62008-02-25 16:27:46 +01002697 else
Zhu Yib481de92007-09-25 17:54:57 -07002698 IWL_DEBUG_RATE("leave - no station!\n");
Johannes Bergd0709a62008-02-25 16:27:46 +01002699 rcu_read_unlock();
Zhu Yib481de92007-09-25 17:54:57 -07002700 return sprintf(buf, "station %d not found\n", sta_id);
2701 }
2702
Tomas Winklerc33104f2008-01-14 17:46:21 -08002703 lq_sta = (void *)sta->rate_ctrl_priv;
Zhu Yib481de92007-09-25 17:54:57 -07002704
Tomas Winklerc33104f2008-01-14 17:46:21 -08002705 lq_type = lq_sta->lq_info[lq_sta->active_tbl].lq_type;
2706 antenna = lq_sta->lq_info[lq_sta->active_tbl].antenna_type;
Zhu Yib481de92007-09-25 17:54:57 -07002707
2708 if (is_legacy(lq_type))
2709 i = IWL_RATE_54M_INDEX;
2710 else
2711 i = IWL_RATE_60M_INDEX;
2712 while (1) {
2713 u64 mask;
2714 int j;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002715 int active = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002716
Tomas Winklerc33104f2008-01-14 17:46:21 -08002717 cnt +=
2718 sprintf(&buf[cnt], " %2dMbs: ", iwl4965_rates[i].ieee / 2);
Zhu Yib481de92007-09-25 17:54:57 -07002719
2720 mask = (1ULL << (IWL_RATE_MAX_WINDOW - 1));
2721 for (j = 0; j < IWL_RATE_MAX_WINDOW; j++, mask >>= 1)
Tomas Winklerc33104f2008-01-14 17:46:21 -08002722 buf[cnt++] =
2723 (lq_sta->lq_info[active].win[i].data & mask)
Zhu Yib481de92007-09-25 17:54:57 -07002724 ? '1' : '0';
2725
Tomas Winklerc33104f2008-01-14 17:46:21 -08002726 samples += lq_sta->lq_info[active].win[i].counter;
2727 good += lq_sta->lq_info[active].win[i].success_counter;
2728 success += lq_sta->lq_info[active].win[i].success_counter *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002729 iwl4965_rates[i].ieee;
Zhu Yib481de92007-09-25 17:54:57 -07002730
Tomas Winklerc33104f2008-01-14 17:46:21 -08002731 if (lq_sta->lq_info[active].win[i].stamp) {
Zhu Yib481de92007-09-25 17:54:57 -07002732 int delta =
2733 jiffies_to_msecs(now -
Tomas Winklerc33104f2008-01-14 17:46:21 -08002734 lq_sta->lq_info[active].win[i].stamp);
Zhu Yib481de92007-09-25 17:54:57 -07002735
2736 if (delta > max_time)
2737 max_time = delta;
2738
Tomas Winklerc33104f2008-01-14 17:46:21 -08002739 cnt += sprintf(&buf[cnt], "%5dms\n", delta);
Zhu Yib481de92007-09-25 17:54:57 -07002740 } else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002741 buf[cnt++] = '\n';
Zhu Yib481de92007-09-25 17:54:57 -07002742
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002743 j = iwl4965_get_prev_ieee_rate(i);
Zhu Yib481de92007-09-25 17:54:57 -07002744 if (j == i)
2745 break;
2746 i = j;
2747 }
2748
2749 /* Display the average rate of all samples taken.
2750 *
Ben Cahill77626352007-11-29 11:09:44 +08002751 * NOTE: We multiply # of samples by 2 since the IEEE measurement
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002752 * added from iwl4965_rates is actually 2X the rate */
Zhu Yib481de92007-09-25 17:54:57 -07002753 if (samples)
Tomas Winklerc33104f2008-01-14 17:46:21 -08002754 cnt += sprintf(&buf[cnt],
Zhu Yib481de92007-09-25 17:54:57 -07002755 "\nAverage rate is %3d.%02dMbs over last %4dms\n"
2756 "%3d%% success (%d good packets over %d tries)\n",
2757 success / (2 * samples), (success * 5 / samples) % 10,
2758 max_time, good * 100 / samples, good, samples);
2759 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002760 cnt += sprintf(&buf[cnt], "\nAverage rate: 0Mbs\n");
2761
2762 cnt += sprintf(&buf[cnt], "\nrate scale type %d antenna %d "
Zhu Yib481de92007-09-25 17:54:57 -07002763 "active_search %d rate index %d\n", lq_type, antenna,
Johannes Berg8318d782008-01-24 19:38:38 +01002764 lq_sta->search_better_tbl, sta->last_txrate_idx);
Zhu Yib481de92007-09-25 17:54:57 -07002765
Johannes Bergd0709a62008-02-25 16:27:46 +01002766 rcu_read_unlock();
Tomas Winklerc33104f2008-01-14 17:46:21 -08002767 return cnt;
Zhu Yib481de92007-09-25 17:54:57 -07002768}
2769
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002770void iwl4965_rate_scale_init(struct ieee80211_hw *hw, s32 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07002771{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002772 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07002773
2774 priv->lq_mngr.lq_ready = 1;
2775}
2776
Reinette Chatre897e1cf2008-03-28 16:21:09 -07002777int iwl4965_rate_control_register(void)
Zhu Yib481de92007-09-25 17:54:57 -07002778{
Reinette Chatre897e1cf2008-03-28 16:21:09 -07002779 return ieee80211_rate_control_register(&rs_ops);
Zhu Yib481de92007-09-25 17:54:57 -07002780}
2781
Reinette Chatre897e1cf2008-03-28 16:21:09 -07002782void iwl4965_rate_control_unregister(void)
Zhu Yib481de92007-09-25 17:54:57 -07002783{
2784 ieee80211_rate_control_unregister(&rs_ops);
2785}
2786