blob: 315b0433801eae7e3963e2a0e3b2c074305437fd [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
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800153 struct iwl4965_link_quality_cmd lq;
Ben Cahill77626352007-11-29 11:09:44 +0800154 struct iwl4965_scale_tbl_info lq_info[LQ_SIZE]; /* "active", "search" */
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200155#ifdef CONFIG_IWL4965_HT
156 struct iwl4965_traffic_load load[TID_MAX_LOAD_COUNT];
157 u8 tx_agg_tid_en;
158#endif
Zhu Yi5ae212c2007-09-27 11:27:38 +0800159#ifdef CONFIG_MAC80211_DEBUGFS
Zhu Yi98d7e092007-09-27 11:27:42 +0800160 struct dentry *rs_sta_dbgfs_scale_table_file;
Zhu Yi0209dc12007-09-27 11:27:43 +0800161 struct dentry *rs_sta_dbgfs_stats_table_file;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200162#ifdef CONFIG_IWL4965_HT
163 struct dentry *rs_sta_dbgfs_tx_agg_tid_en_file;
164#endif
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800165 struct iwl4965_rate dbg_fixed;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700166 struct iwl_priv *drv;
Zhu Yi5ae212c2007-09-27 11:27:38 +0800167#endif
Zhu Yib481de92007-09-25 17:54:57 -0700168};
169
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700170static void rs_rate_scale_perform(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700171 struct net_device *dev,
172 struct ieee80211_hdr *hdr,
173 struct sta_info *sta);
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,
176 struct iwl4965_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
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700233static int iwl4965_lq_sync_callback(struct iwl_priv *priv,
Tomas Winkler857485c2008-03-21 13:53:44 -0700234 struct iwl_cmd *cmd, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -0700235{
236 /*We didn't cache the SKB; let the caller free it */
237 return 1;
238}
239
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800240static inline u8 iwl4965_rate_get_rate(u32 rate_n_flags)
Zhu Yib481de92007-09-25 17:54:57 -0700241{
242 return (u8)(rate_n_flags & 0xFF);
243}
244
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700245static int rs_send_lq_cmd(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800246 struct iwl4965_link_quality_cmd *lq, u8 flags)
Zhu Yib481de92007-09-25 17:54:57 -0700247{
Tomas Winkler0a6857e2008-03-12 16:58:49 -0700248#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -0700249 int i;
250#endif
Tomas Winkler857485c2008-03-21 13:53:44 -0700251 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700252 .id = REPLY_TX_LINK_QUALITY_CMD,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800253 .len = sizeof(struct iwl4965_link_quality_cmd),
Zhu Yib481de92007-09-25 17:54:57 -0700254 .meta.flags = flags,
255 .data = lq,
256 };
257
258 if ((lq->sta_id == 0xFF) &&
259 (priv->iw_mode == IEEE80211_IF_TYPE_IBSS))
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800260 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -0700261
262 if (lq->sta_id == 0xFF)
263 lq->sta_id = IWL_AP_ID;
264
265 IWL_DEBUG_RATE("lq station id 0x%x\n", lq->sta_id);
266 IWL_DEBUG_RATE("lq dta 0x%X 0x%X\n",
267 lq->general_params.single_stream_ant_msk,
268 lq->general_params.dual_stream_ant_msk);
Tomas Winkler0a6857e2008-03-12 16:58:49 -0700269#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -0700270 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
271 IWL_DEBUG_RATE("lq index %d 0x%X\n",
272 i, lq->rs_table[i].rate_n_flags);
273#endif
274
275 if (flags & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800276 cmd.meta.u.callback = iwl4965_lq_sync_callback;
Zhu Yib481de92007-09-25 17:54:57 -0700277
Tomas Winkler3109ece2008-03-28 16:33:35 -0700278 if (iwl_is_associated(priv) && priv->assoc_station_added &&
Zhu Yib481de92007-09-25 17:54:57 -0700279 priv->lq_mngr.lq_ready)
Tomas Winkler857485c2008-03-21 13:53:44 -0700280 return iwl_send_cmd(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700281
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800282 return 0;
Zhu Yib481de92007-09-25 17:54:57 -0700283}
284
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800285static void rs_rate_scale_clear_window(struct iwl4965_rate_scale_data *window)
Zhu Yib481de92007-09-25 17:54:57 -0700286{
287 window->data = 0;
288 window->success_counter = 0;
289 window->success_ratio = IWL_INVALID_VALUE;
290 window->counter = 0;
291 window->average_tpt = IWL_INVALID_VALUE;
292 window->stamp = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700293}
294
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200295#ifdef CONFIG_IWL4965_HT
296/*
297 * removes the old data from the statistics. All data that is older than
298 * TID_MAX_TIME_DIFF, will be deleted.
299 */
300static void rs_tl_rm_old_stats(struct iwl4965_traffic_load *tl, u32 curr_time)
301{
302 /* The oldest age we want to keep */
303 u32 oldest_time = curr_time - TID_MAX_TIME_DIFF;
304
305 while (tl->queue_count &&
306 (tl->time_stamp < oldest_time)) {
307 tl->total -= tl->packet_count[tl->head];
308 tl->packet_count[tl->head] = 0;
309 tl->time_stamp += TID_QUEUE_CELL_SPACING;
310 tl->queue_count--;
311 tl->head++;
312 if (tl->head >= TID_QUEUE_MAX_SIZE)
313 tl->head = 0;
314 }
315}
316
317/*
318 * increment traffic load value for tid and also remove
319 * any old values if passed the certain time period
320 */
321static void rs_tl_add_packet(struct iwl4965_lq_sta *lq_data, u8 tid)
322{
323 u32 curr_time = jiffies_to_msecs(jiffies);
324 u32 time_diff;
325 s32 index;
326 struct iwl4965_traffic_load *tl = NULL;
327
328 if (tid >= TID_MAX_LOAD_COUNT)
329 return;
330
331 tl = &lq_data->load[tid];
332
333 curr_time -= curr_time % TID_ROUND_VALUE;
334
335 /* Happens only for the first packet. Initialize the data */
336 if (!(tl->queue_count)) {
337 tl->total = 1;
338 tl->time_stamp = curr_time;
339 tl->queue_count = 1;
340 tl->head = 0;
341 tl->packet_count[0] = 1;
342 return;
343 }
344
345 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
346 index = time_diff / TID_QUEUE_CELL_SPACING;
347
348 /* The history is too long: remove data that is older than */
349 /* TID_MAX_TIME_DIFF */
350 if (index >= TID_QUEUE_MAX_SIZE)
351 rs_tl_rm_old_stats(tl, curr_time);
352
353 index = (tl->head + index) % TID_QUEUE_MAX_SIZE;
354 tl->packet_count[index] = tl->packet_count[index] + 1;
355 tl->total = tl->total + 1;
356
357 if ((index + 1) > tl->queue_count)
358 tl->queue_count = index + 1;
359}
360
361/*
362 get the traffic load value for tid
363*/
364static u32 rs_tl_get_load(struct iwl4965_lq_sta *lq_data, u8 tid)
365{
366 u32 curr_time = jiffies_to_msecs(jiffies);
367 u32 time_diff;
368 s32 index;
369 struct iwl4965_traffic_load *tl = NULL;
370
371 if (tid >= TID_MAX_LOAD_COUNT)
372 return 0;
373
374 tl = &(lq_data->load[tid]);
375
376 curr_time -= curr_time % TID_ROUND_VALUE;
377
378 if (!(tl->queue_count))
379 return 0;
380
381 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
382 index = time_diff / TID_QUEUE_CELL_SPACING;
383
384 /* The history is too long: remove data that is older than */
385 /* TID_MAX_TIME_DIFF */
386 if (index >= TID_QUEUE_MAX_SIZE)
387 rs_tl_rm_old_stats(tl, curr_time);
388
389 return tl->total;
390}
391
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700392static void rs_tl_turn_on_agg_for_tid(struct iwl_priv *priv,
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200393 struct iwl4965_lq_sta *lq_data, u8 tid,
394 struct sta_info *sta)
395{
396 unsigned long state;
397 DECLARE_MAC_BUF(mac);
398
399 spin_lock_bh(&sta->ampdu_mlme.ampdu_tx);
Ron Rindjunskycee24a32008-03-26 20:36:03 +0200400 state = sta->ampdu_mlme.tid_state_tx[tid];
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200401 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
402
403 if (state == HT_AGG_STATE_IDLE &&
404 rs_tl_get_load(lq_data, tid) > IWL_AGG_LOAD_THRESHOLD) {
405 IWL_DEBUG_HT("Starting Tx agg: STA: %s tid: %d\n",
406 print_mac(mac, sta->addr), tid);
407 ieee80211_start_tx_ba_session(priv->hw, sta->addr, tid);
408 }
409}
410
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700411static void rs_tl_turn_on_agg(struct iwl_priv *priv, u8 tid,
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200412 struct iwl4965_lq_sta *lq_data,
413 struct sta_info *sta)
414{
415 if ((tid < TID_MAX_LOAD_COUNT))
416 rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
417 else if (tid == IWL_AGG_ALL_TID)
418 for (tid = 0; tid < TID_MAX_LOAD_COUNT; tid++)
419 rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
420}
421
422#endif /* CONFIG_IWLWIFI_HT */
423
Ben Cahill77626352007-11-29 11:09:44 +0800424/**
425 * rs_collect_tx_data - Update the success/failure sliding window
426 *
427 * We keep a sliding window of the last 62 packets transmitted
428 * at this rate. window->data contains the bitmask of successful
429 * packets.
430 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800431static int rs_collect_tx_data(struct iwl4965_rate_scale_data *windows,
Ron Rindjunsky99556432008-01-28 14:07:25 +0200432 int scale_index, s32 tpt, int retries,
433 int successes)
Zhu Yib481de92007-09-25 17:54:57 -0700434{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800435 struct iwl4965_rate_scale_data *window = NULL;
Zhu Yib481de92007-09-25 17:54:57 -0700436 u64 mask;
437 u8 win_size = IWL_RATE_MAX_WINDOW;
438 s32 fail_count;
439
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800440 if (scale_index < 0 || scale_index >= IWL_RATE_COUNT)
441 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -0700442
Ben Cahill77626352007-11-29 11:09:44 +0800443 /* Select data for current tx bit rate */
Zhu Yib481de92007-09-25 17:54:57 -0700444 window = &(windows[scale_index]);
445
Ben Cahill77626352007-11-29 11:09:44 +0800446 /*
447 * Keep track of only the latest 62 tx frame attempts in this rate's
448 * history window; anything older isn't really relevant any more.
449 * If we have filled up the sliding window, drop the oldest attempt;
450 * if the oldest attempt (highest bit in bitmap) shows "success",
451 * subtract "1" from the success counter (this is the main reason
452 * we keep these bitmaps!).
453 */
Ron Rindjunsky99556432008-01-28 14:07:25 +0200454 while (retries > 0) {
455 if (window->counter >= win_size) {
456 window->counter = win_size - 1;
457 mask = 1;
458 mask = (mask << (win_size - 1));
459 if (window->data & mask) {
460 window->data &= ~mask;
461 window->success_counter =
462 window->success_counter - 1;
463 }
Zhu Yib481de92007-09-25 17:54:57 -0700464 }
Zhu Yib481de92007-09-25 17:54:57 -0700465
Ron Rindjunsky99556432008-01-28 14:07:25 +0200466 /* Increment frames-attempted counter */
467 window->counter++;
Ben Cahill77626352007-11-29 11:09:44 +0800468
Ron Rindjunsky99556432008-01-28 14:07:25 +0200469 /* Shift bitmap by one frame (throw away oldest history),
470 * OR in "1", and increment "success" if this
471 * frame was successful. */
472 mask = window->data;
473 window->data = (mask << 1);
474 if (successes > 0) {
475 window->success_counter = window->success_counter + 1;
476 window->data |= 0x1;
477 successes--;
478 }
479
480 retries--;
Zhu Yib481de92007-09-25 17:54:57 -0700481 }
482
Ben Cahill77626352007-11-29 11:09:44 +0800483 /* Calculate current success ratio, avoid divide-by-0! */
Zhu Yib481de92007-09-25 17:54:57 -0700484 if (window->counter > 0)
485 window->success_ratio = 128 * (100 * window->success_counter)
486 / window->counter;
487 else
488 window->success_ratio = IWL_INVALID_VALUE;
489
490 fail_count = window->counter - window->success_counter;
491
Ben Cahill77626352007-11-29 11:09:44 +0800492 /* Calculate average throughput, if we have enough history. */
Zhu Yib481de92007-09-25 17:54:57 -0700493 if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
494 (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
495 window->average_tpt = (window->success_ratio * tpt + 64) / 128;
496 else
497 window->average_tpt = IWL_INVALID_VALUE;
498
Ben Cahill77626352007-11-29 11:09:44 +0800499 /* Tag this window as having been updated */
Zhu Yib481de92007-09-25 17:54:57 -0700500 window->stamp = jiffies;
501
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800502 return 0;
Zhu Yib481de92007-09-25 17:54:57 -0700503}
504
Ben Cahill77626352007-11-29 11:09:44 +0800505/*
506 * Fill uCode API rate_n_flags field, based on "search" or "active" table.
507 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800508static void rs_mcs_from_tbl(struct iwl4965_rate *mcs_rate,
509 struct iwl4965_scale_tbl_info *tbl,
Zhu Yib481de92007-09-25 17:54:57 -0700510 int index, u8 use_green)
511{
Zhu Yib481de92007-09-25 17:54:57 -0700512 if (is_legacy(tbl->lq_type)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800513 mcs_rate->rate_n_flags = iwl4965_rates[index].plcp;
Zhu Yib481de92007-09-25 17:54:57 -0700514 if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
515 mcs_rate->rate_n_flags |= RATE_MCS_CCK_MSK;
516
517 } else if (is_siso(tbl->lq_type)) {
518 if (index > IWL_LAST_OFDM_RATE)
519 index = IWL_LAST_OFDM_RATE;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800520 mcs_rate->rate_n_flags = iwl4965_rates[index].plcp_siso |
Zhu Yib481de92007-09-25 17:54:57 -0700521 RATE_MCS_HT_MSK;
522 } else {
523 if (index > IWL_LAST_OFDM_RATE)
524 index = IWL_LAST_OFDM_RATE;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800525 mcs_rate->rate_n_flags = iwl4965_rates[index].plcp_mimo |
Zhu Yib481de92007-09-25 17:54:57 -0700526 RATE_MCS_HT_MSK;
527 }
528
529 switch (tbl->antenna_type) {
530 case ANT_BOTH:
531 mcs_rate->rate_n_flags |= RATE_MCS_ANT_AB_MSK;
532 break;
533 case ANT_MAIN:
534 mcs_rate->rate_n_flags |= RATE_MCS_ANT_A_MSK;
535 break;
536 case ANT_AUX:
537 mcs_rate->rate_n_flags |= RATE_MCS_ANT_B_MSK;
538 break;
539 case ANT_NONE:
540 break;
541 }
542
543 if (is_legacy(tbl->lq_type))
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800544 return;
Zhu Yib481de92007-09-25 17:54:57 -0700545
546 if (tbl->is_fat) {
547 if (tbl->is_dup)
548 mcs_rate->rate_n_flags |= RATE_MCS_DUP_MSK;
549 else
550 mcs_rate->rate_n_flags |= RATE_MCS_FAT_MSK;
551 }
552 if (tbl->is_SGI)
553 mcs_rate->rate_n_flags |= RATE_MCS_SGI_MSK;
554
555 if (use_green) {
556 mcs_rate->rate_n_flags |= RATE_MCS_GF_MSK;
557 if (is_siso(tbl->lq_type))
558 mcs_rate->rate_n_flags &= ~RATE_MCS_SGI_MSK;
559 }
Zhu Yib481de92007-09-25 17:54:57 -0700560}
561
Ben Cahill77626352007-11-29 11:09:44 +0800562/*
563 * Interpret uCode API's rate_n_flags format,
564 * fill "search" or "active" tx mode table.
565 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800566static int rs_get_tbl_info_from_mcs(const struct iwl4965_rate *mcs_rate,
Johannes Berg8318d782008-01-24 19:38:38 +0100567 enum ieee80211_band band,
568 struct iwl4965_scale_tbl_info *tbl,
Zhu Yib481de92007-09-25 17:54:57 -0700569 int *rate_idx)
570{
571 int index;
572 u32 ant_msk;
573
Tomas Winkler17744ff2008-03-02 01:52:00 +0200574 index = iwl4965_hwrate_to_plcp_idx(mcs_rate->rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -0700575
576 if (index == IWL_RATE_INVALID) {
577 *rate_idx = -1;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800578 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -0700579 }
Ben Cahill77626352007-11-29 11:09:44 +0800580 tbl->is_SGI = 0; /* default legacy setup */
Zhu Yib481de92007-09-25 17:54:57 -0700581 tbl->is_fat = 0;
582 tbl->is_dup = 0;
Ben Cahill77626352007-11-29 11:09:44 +0800583 tbl->antenna_type = ANT_BOTH; /* default MIMO setup */
Zhu Yib481de92007-09-25 17:54:57 -0700584
Ben Cahill77626352007-11-29 11:09:44 +0800585 /* legacy rate format */
Zhu Yib481de92007-09-25 17:54:57 -0700586 if (!(mcs_rate->rate_n_flags & RATE_MCS_HT_MSK)) {
587 ant_msk = (mcs_rate->rate_n_flags & RATE_MCS_ANT_AB_MSK);
588
589 if (ant_msk == RATE_MCS_ANT_AB_MSK)
590 tbl->lq_type = LQ_NONE;
591 else {
592
Johannes Berg8318d782008-01-24 19:38:38 +0100593 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700594 tbl->lq_type = LQ_A;
595 else
596 tbl->lq_type = LQ_G;
597
598 if (mcs_rate->rate_n_flags & RATE_MCS_ANT_A_MSK)
599 tbl->antenna_type = ANT_MAIN;
600 else
601 tbl->antenna_type = ANT_AUX;
602 }
603 *rate_idx = index;
604
Ben Cahill77626352007-11-29 11:09:44 +0800605 /* HT rate format, SISO (might be 20 MHz legacy or 40 MHz fat width) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800606 } else if (iwl4965_rate_get_rate(mcs_rate->rate_n_flags)
Zhu Yib481de92007-09-25 17:54:57 -0700607 <= IWL_RATE_SISO_60M_PLCP) {
608 tbl->lq_type = LQ_SISO;
609
610 ant_msk = (mcs_rate->rate_n_flags & RATE_MCS_ANT_AB_MSK);
611 if (ant_msk == RATE_MCS_ANT_AB_MSK)
612 tbl->lq_type = LQ_NONE;
613 else {
614 if (mcs_rate->rate_n_flags & RATE_MCS_ANT_A_MSK)
615 tbl->antenna_type = ANT_MAIN;
616 else
617 tbl->antenna_type = ANT_AUX;
618 }
619 if (mcs_rate->rate_n_flags & RATE_MCS_SGI_MSK)
620 tbl->is_SGI = 1;
621
622 if ((mcs_rate->rate_n_flags & RATE_MCS_FAT_MSK) ||
623 (mcs_rate->rate_n_flags & RATE_MCS_DUP_MSK))
624 tbl->is_fat = 1;
625
626 if (mcs_rate->rate_n_flags & RATE_MCS_DUP_MSK)
627 tbl->is_dup = 1;
628
629 *rate_idx = index;
Ben Cahill77626352007-11-29 11:09:44 +0800630
631 /* HT rate format, MIMO (might be 20 MHz legacy or 40 MHz fat width) */
Zhu Yib481de92007-09-25 17:54:57 -0700632 } else {
633 tbl->lq_type = LQ_MIMO;
634 if (mcs_rate->rate_n_flags & RATE_MCS_SGI_MSK)
635 tbl->is_SGI = 1;
636
637 if ((mcs_rate->rate_n_flags & RATE_MCS_FAT_MSK) ||
638 (mcs_rate->rate_n_flags & RATE_MCS_DUP_MSK))
639 tbl->is_fat = 1;
640
641 if (mcs_rate->rate_n_flags & RATE_MCS_DUP_MSK)
642 tbl->is_dup = 1;
643 *rate_idx = index;
644 }
645 return 0;
646}
647
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800648static inline void rs_toggle_antenna(struct iwl4965_rate *new_rate,
649 struct iwl4965_scale_tbl_info *tbl)
Zhu Yib481de92007-09-25 17:54:57 -0700650{
651 if (tbl->antenna_type == ANT_AUX) {
652 tbl->antenna_type = ANT_MAIN;
653 new_rate->rate_n_flags &= ~RATE_MCS_ANT_B_MSK;
654 new_rate->rate_n_flags |= RATE_MCS_ANT_A_MSK;
655 } else {
656 tbl->antenna_type = ANT_AUX;
657 new_rate->rate_n_flags &= ~RATE_MCS_ANT_A_MSK;
658 new_rate->rate_n_flags |= RATE_MCS_ANT_B_MSK;
659 }
660}
661
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700662static inline u8 rs_use_green(struct iwl_priv *priv,
Ron Rindjunsky270243a2007-11-26 16:14:41 +0200663 struct ieee80211_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -0700664{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800665#ifdef CONFIG_IWL4965_HT
Ron Rindjunsky270243a2007-11-26 16:14:41 +0200666 return ((conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) &&
667 priv->current_ht_config.is_green_field &&
668 !priv->current_ht_config.non_GF_STA_present);
669#endif /* CONFIG_IWL4965_HT */
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800670 return 0;
Zhu Yib481de92007-09-25 17:54:57 -0700671}
672
673/**
674 * rs_get_supported_rates - get the available rates
675 *
676 * if management frame or broadcast frame only return
677 * basic available rates.
678 *
679 */
Tomas Winklerc33104f2008-01-14 17:46:21 -0800680static void rs_get_supported_rates(struct iwl4965_lq_sta *lq_sta,
Zhu Yib481de92007-09-25 17:54:57 -0700681 struct ieee80211_hdr *hdr,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800682 enum iwl4965_table_type rate_type,
Zhu Yib481de92007-09-25 17:54:57 -0700683 u16 *data_rate)
684{
685 if (is_legacy(rate_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -0800686 *data_rate = lq_sta->active_rate;
Zhu Yib481de92007-09-25 17:54:57 -0700687 else {
688 if (is_siso(rate_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -0800689 *data_rate = lq_sta->active_siso_rate;
Zhu Yib481de92007-09-25 17:54:57 -0700690 else
Tomas Winklerc33104f2008-01-14 17:46:21 -0800691 *data_rate = lq_sta->active_mimo_rate;
Zhu Yib481de92007-09-25 17:54:57 -0700692 }
693
694 if (hdr && is_multicast_ether_addr(hdr->addr1) &&
Tomas Winklerc33104f2008-01-14 17:46:21 -0800695 lq_sta->active_rate_basic) {
696 *data_rate = lq_sta->active_rate_basic;
697 }
Zhu Yib481de92007-09-25 17:54:57 -0700698}
699
700static u16 rs_get_adjacent_rate(u8 index, u16 rate_mask, int rate_type)
701{
702 u8 high = IWL_RATE_INVALID;
703 u8 low = IWL_RATE_INVALID;
704
Ian Schram01ebd062007-10-25 17:15:22 +0800705 /* 802.11A or ht walks to the next literal adjacent rate in
Zhu Yib481de92007-09-25 17:54:57 -0700706 * the rate table */
707 if (is_a_band(rate_type) || !is_legacy(rate_type)) {
708 int i;
709 u32 mask;
710
711 /* Find the previous rate that is in the rate mask */
712 i = index - 1;
713 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
714 if (rate_mask & mask) {
715 low = i;
716 break;
717 }
718 }
719
720 /* Find the next rate that is in the rate mask */
721 i = index + 1;
722 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
723 if (rate_mask & mask) {
724 high = i;
725 break;
726 }
727 }
728
729 return (high << 8) | low;
730 }
731
732 low = index;
733 while (low != IWL_RATE_INVALID) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800734 low = iwl4965_rates[low].prev_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700735 if (low == IWL_RATE_INVALID)
736 break;
737 if (rate_mask & (1 << low))
738 break;
739 IWL_DEBUG_RATE("Skipping masked lower rate: %d\n", low);
740 }
741
742 high = index;
743 while (high != IWL_RATE_INVALID) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800744 high = iwl4965_rates[high].next_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700745 if (high == IWL_RATE_INVALID)
746 break;
747 if (rate_mask & (1 << high))
748 break;
749 IWL_DEBUG_RATE("Skipping masked higher rate: %d\n", high);
750 }
751
752 return (high << 8) | low;
753}
754
Tomas Winklerc33104f2008-01-14 17:46:21 -0800755static void rs_get_lower_rate(struct iwl4965_lq_sta *lq_sta,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800756 struct iwl4965_scale_tbl_info *tbl, u8 scale_index,
757 u8 ht_possible, struct iwl4965_rate *mcs_rate)
Zhu Yib481de92007-09-25 17:54:57 -0700758{
Zhu Yib481de92007-09-25 17:54:57 -0700759 s32 low;
760 u16 rate_mask;
761 u16 high_low;
762 u8 switch_to_legacy = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -0800763 u8 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -0700764
765 /* check if we need to switch from HT to legacy rates.
766 * assumption is that mandatory rates (1Mbps or 6Mbps)
767 * are always supported (spec demand) */
768 if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_index)) {
769 switch_to_legacy = 1;
770 scale_index = rs_ht_to_legacy[scale_index];
Johannes Berg8318d782008-01-24 19:38:38 +0100771 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700772 tbl->lq_type = LQ_A;
773 else
774 tbl->lq_type = LQ_G;
775
776 if ((tbl->antenna_type == ANT_BOTH) ||
777 (tbl->antenna_type == ANT_NONE))
778 tbl->antenna_type = ANT_MAIN;
779
780 tbl->is_fat = 0;
781 tbl->is_SGI = 0;
782 }
783
Tomas Winklerc33104f2008-01-14 17:46:21 -0800784 rs_get_supported_rates(lq_sta, NULL, tbl->lq_type, &rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -0700785
Ben Cahill77626352007-11-29 11:09:44 +0800786 /* Mask with station rate restriction */
Zhu Yib481de92007-09-25 17:54:57 -0700787 if (is_legacy(tbl->lq_type)) {
Ben Cahill77626352007-11-29 11:09:44 +0800788 /* supp_rates has no CCK bits in A mode */
Johannes Berg8318d782008-01-24 19:38:38 +0100789 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700790 rate_mask = (u16)(rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -0800791 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
Zhu Yib481de92007-09-25 17:54:57 -0700792 else
Tomas Winklerc33104f2008-01-14 17:46:21 -0800793 rate_mask = (u16)(rate_mask & lq_sta->supp_rates);
Zhu Yib481de92007-09-25 17:54:57 -0700794 }
795
Ben Cahill77626352007-11-29 11:09:44 +0800796 /* If we switched from HT to legacy, check current rate */
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800797 if (switch_to_legacy && (rate_mask & (1 << scale_index))) {
Zhu Yib481de92007-09-25 17:54:57 -0700798 rs_mcs_from_tbl(mcs_rate, tbl, scale_index, is_green);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800799 return;
Zhu Yib481de92007-09-25 17:54:57 -0700800 }
801
802 high_low = rs_get_adjacent_rate(scale_index, rate_mask, tbl->lq_type);
803 low = high_low & 0xff;
804
805 if (low != IWL_RATE_INVALID)
806 rs_mcs_from_tbl(mcs_rate, tbl, low, is_green);
807 else
808 rs_mcs_from_tbl(mcs_rate, tbl, scale_index, is_green);
Zhu Yib481de92007-09-25 17:54:57 -0700809}
810
Ben Cahill77626352007-11-29 11:09:44 +0800811/*
812 * mac80211 sends us Tx status
813 */
Tomas Winklerc33104f2008-01-14 17:46:21 -0800814static void rs_tx_status(void *priv_rate, struct net_device *dev,
Zhu Yib481de92007-09-25 17:54:57 -0700815 struct sk_buff *skb,
816 struct ieee80211_tx_status *tx_resp)
817{
818 int status;
819 u8 retries;
820 int rs_index, index = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -0800821 struct iwl4965_lq_sta *lq_sta;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800822 struct iwl4965_link_quality_cmd *table;
Zhu Yib481de92007-09-25 17:54:57 -0700823 struct sta_info *sta;
824 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700825 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
Zhu Yib481de92007-09-25 17:54:57 -0700826 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800827 struct ieee80211_hw *hw = local_to_hw(local);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800828 struct iwl4965_rate_scale_data *window = NULL;
829 struct iwl4965_rate_scale_data *search_win = NULL;
830 struct iwl4965_rate tx_mcs;
831 struct iwl4965_scale_tbl_info tbl_type;
832 struct iwl4965_scale_tbl_info *curr_tbl, *search_tbl;
Zhu Yib481de92007-09-25 17:54:57 -0700833 u8 active_index = 0;
834 u16 fc = le16_to_cpu(hdr->frame_control);
835 s32 tpt = 0;
836
Zhu Yi58826352007-09-27 11:27:39 +0800837 IWL_DEBUG_RATE_LIMIT("get frame ack response, update rate scale window\n");
Zhu Yib481de92007-09-25 17:54:57 -0700838
839 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1))
840 return;
841
Ron Rindjunsky99556432008-01-28 14:07:25 +0200842 /* This packet was aggregated but doesn't carry rate scale info */
843 if ((tx_resp->control.flags & IEEE80211_TXCTL_AMPDU) &&
844 !(tx_resp->flags & IEEE80211_TX_STATUS_AMPDU))
845 return;
846
Zhu Yib481de92007-09-25 17:54:57 -0700847 retries = tx_resp->retry_count;
848
849 if (retries > 15)
850 retries = 15;
851
Johannes Bergd0709a62008-02-25 16:27:46 +0100852 rcu_read_lock();
Zhu Yib481de92007-09-25 17:54:57 -0700853
854 sta = sta_info_get(local, hdr->addr1);
855
Tomas Winklerf4d60822008-03-05 11:31:00 -0800856 if (!sta || !sta->rate_ctrl_priv)
857 goto out;
858
Zhu Yib481de92007-09-25 17:54:57 -0700859
Tomas Winklerc33104f2008-01-14 17:46:21 -0800860 lq_sta = (struct iwl4965_lq_sta *)sta->rate_ctrl_priv;
Zhu Yib481de92007-09-25 17:54:57 -0700861
862 if (!priv->lq_mngr.lq_ready)
Tomas Winklerf4d60822008-03-05 11:31:00 -0800863 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700864
Tomas Winklerc33104f2008-01-14 17:46:21 -0800865 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
866 !lq_sta->ibss_sta_added)
Tomas Winklerf4d60822008-03-05 11:31:00 -0800867 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700868
Tomas Winklerc33104f2008-01-14 17:46:21 -0800869 table = &lq_sta->lq;
870 active_index = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -0700871
Ben Cahill77626352007-11-29 11:09:44 +0800872 /* Get mac80211 antenna info */
Tomas Winklerc33104f2008-01-14 17:46:21 -0800873 lq_sta->antenna =
874 (lq_sta->valid_antenna & local->hw.conf.antenna_sel_tx);
875 if (!lq_sta->antenna)
876 lq_sta->antenna = lq_sta->valid_antenna;
Zhu Yib481de92007-09-25 17:54:57 -0700877
Ben Cahill77626352007-11-29 11:09:44 +0800878 /* Ignore mac80211 antenna info for now */
Tomas Winklerc33104f2008-01-14 17:46:21 -0800879 lq_sta->antenna = lq_sta->valid_antenna;
Ben Cahill77626352007-11-29 11:09:44 +0800880
Tomas Winklerc33104f2008-01-14 17:46:21 -0800881 curr_tbl = &(lq_sta->lq_info[active_index]);
882 search_tbl = &(lq_sta->lq_info[(1 - active_index)]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800883 window = (struct iwl4965_rate_scale_data *)
Zhu Yib481de92007-09-25 17:54:57 -0700884 &(curr_tbl->win[0]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800885 search_win = (struct iwl4965_rate_scale_data *)
Zhu Yib481de92007-09-25 17:54:57 -0700886 &(search_tbl->win[0]);
887
Ben Cahill77626352007-11-29 11:09:44 +0800888 /*
889 * Ignore this Tx frame response if its initial rate doesn't match
890 * that of latest Link Quality command. There may be stragglers
891 * from a previous Link Quality command, but we're no longer interested
892 * in those; they're either from the "active" mode while we're trying
893 * to check "search" mode, or a prior "search" mode after we've moved
894 * to a new "search" mode (which might become the new "active" mode).
895 */
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800896 tx_mcs.rate_n_flags = le32_to_cpu(table->rs_table[0].rate_n_flags);
897 rs_get_tbl_info_from_mcs(&tx_mcs, priv->band, &tbl_type, &rs_index);
898 if (priv->band == IEEE80211_BAND_5GHZ)
899 rs_index -= IWL_FIRST_OFDM_RATE;
900
901 if ((tx_resp->control.tx_rate == NULL) ||
902 (tbl_type.is_SGI ^
903 !!(tx_resp->control.flags & IEEE80211_TXCTL_SHORT_GI)) ||
904 (tbl_type.is_fat ^
905 !!(tx_resp->control.flags & IEEE80211_TXCTL_40_MHZ_WIDTH)) ||
906 (tbl_type.is_dup ^
907 !!(tx_resp->control.flags & IEEE80211_TXCTL_DUP_DATA)) ||
908 (tbl_type.antenna_type ^
909 tx_resp->control.antenna_sel_tx) ||
910 (!!(tx_mcs.rate_n_flags & RATE_MCS_HT_MSK) ^
911 !!(tx_resp->control.flags & IEEE80211_TXCTL_OFDM_HT)) ||
912 (!!(tx_mcs.rate_n_flags & RATE_MCS_GF_MSK) ^
913 !!(tx_resp->control.flags & IEEE80211_TXCTL_GREEN_FIELD)) ||
914 (hw->wiphy->bands[priv->band]->bitrates[rs_index].bitrate !=
915 tx_resp->control.tx_rate->bitrate)) {
916 IWL_DEBUG_RATE("initial rate does not match 0x%x\n",
917 tx_mcs.rate_n_flags);
Tomas Winklerf4d60822008-03-05 11:31:00 -0800918 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700919 }
920
Ben Cahill77626352007-11-29 11:09:44 +0800921 /* Update frame history window with "failure" for each Tx retry. */
Zhu Yib481de92007-09-25 17:54:57 -0700922 while (retries) {
Ben Cahill77626352007-11-29 11:09:44 +0800923 /* Look up the rate and other info used for each tx attempt.
924 * Each tx attempt steps one entry deeper in the rate table. */
Zhu Yib481de92007-09-25 17:54:57 -0700925 tx_mcs.rate_n_flags =
926 le32_to_cpu(table->rs_table[index].rate_n_flags);
Johannes Berg8318d782008-01-24 19:38:38 +0100927 rs_get_tbl_info_from_mcs(&tx_mcs, priv->band,
Zhu Yib481de92007-09-25 17:54:57 -0700928 &tbl_type, &rs_index);
929
Ben Cahill77626352007-11-29 11:09:44 +0800930 /* If type matches "search" table,
931 * add failure to "search" history */
Zhu Yib481de92007-09-25 17:54:57 -0700932 if ((tbl_type.lq_type == search_tbl->lq_type) &&
933 (tbl_type.antenna_type == search_tbl->antenna_type) &&
934 (tbl_type.is_SGI == search_tbl->is_SGI)) {
935 if (search_tbl->expected_tpt)
936 tpt = search_tbl->expected_tpt[rs_index];
937 else
938 tpt = 0;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200939 rs_collect_tx_data(search_win, rs_index, tpt, 1, 0);
Ben Cahill77626352007-11-29 11:09:44 +0800940
941 /* Else if type matches "current/active" table,
942 * add failure to "current/active" history */
Zhu Yib481de92007-09-25 17:54:57 -0700943 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
944 (tbl_type.antenna_type == curr_tbl->antenna_type) &&
945 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
946 if (curr_tbl->expected_tpt)
947 tpt = curr_tbl->expected_tpt[rs_index];
948 else
949 tpt = 0;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200950 rs_collect_tx_data(window, rs_index, tpt, 1, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700951 }
Ben Cahill77626352007-11-29 11:09:44 +0800952
953 /* If not searching for a new mode, increment failed counter
954 * ... this helps determine when to start searching again */
Tomas Winklerc33104f2008-01-14 17:46:21 -0800955 if (lq_sta->stay_in_tbl)
956 lq_sta->total_failed++;
Zhu Yib481de92007-09-25 17:54:57 -0700957 --retries;
958 index++;
959
960 }
961
Ben Cahill77626352007-11-29 11:09:44 +0800962 /*
963 * Find (by rate) the history window to update with final Tx attempt;
964 * if Tx was successful first try, use original rate,
965 * else look up the rate that was, finally, successful.
966 */
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800967 tx_mcs.rate_n_flags = le32_to_cpu(table->rs_table[index].rate_n_flags);
968 rs_get_tbl_info_from_mcs(&tx_mcs, priv->band, &tbl_type, &rs_index);
Zhu Yib481de92007-09-25 17:54:57 -0700969
Ben Cahill77626352007-11-29 11:09:44 +0800970 /* Update frame history window with "success" if Tx got ACKed ... */
Zhu Yib481de92007-09-25 17:54:57 -0700971 if (tx_resp->flags & IEEE80211_TX_STATUS_ACK)
972 status = 1;
973 else
974 status = 0;
975
Ben Cahill77626352007-11-29 11:09:44 +0800976 /* If type matches "search" table,
977 * add final tx status to "search" history */
Zhu Yib481de92007-09-25 17:54:57 -0700978 if ((tbl_type.lq_type == search_tbl->lq_type) &&
979 (tbl_type.antenna_type == search_tbl->antenna_type) &&
980 (tbl_type.is_SGI == search_tbl->is_SGI)) {
981 if (search_tbl->expected_tpt)
982 tpt = search_tbl->expected_tpt[rs_index];
983 else
984 tpt = 0;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200985 if (tx_resp->control.flags & IEEE80211_TXCTL_AMPDU)
986 rs_collect_tx_data(search_win, rs_index, tpt,
987 tx_resp->ampdu_ack_len,
988 tx_resp->ampdu_ack_map);
989 else
990 rs_collect_tx_data(search_win, rs_index, tpt,
991 1, status);
Ben Cahill77626352007-11-29 11:09:44 +0800992 /* Else if type matches "current/active" table,
993 * add final tx status to "current/active" history */
Zhu Yib481de92007-09-25 17:54:57 -0700994 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
995 (tbl_type.antenna_type == curr_tbl->antenna_type) &&
996 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
997 if (curr_tbl->expected_tpt)
998 tpt = curr_tbl->expected_tpt[rs_index];
999 else
1000 tpt = 0;
Ron Rindjunsky99556432008-01-28 14:07:25 +02001001 if (tx_resp->control.flags & IEEE80211_TXCTL_AMPDU)
1002 rs_collect_tx_data(window, rs_index, tpt,
1003 tx_resp->ampdu_ack_len,
1004 tx_resp->ampdu_ack_map);
1005 else
1006 rs_collect_tx_data(window, rs_index, tpt,
1007 1, status);
Zhu Yib481de92007-09-25 17:54:57 -07001008 }
1009
Ben Cahill77626352007-11-29 11:09:44 +08001010 /* If not searching for new mode, increment success/failed counter
1011 * ... these help determine when to start searching again */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001012 if (lq_sta->stay_in_tbl) {
Ron Rindjunsky99556432008-01-28 14:07:25 +02001013 if (tx_resp->control.flags & IEEE80211_TXCTL_AMPDU) {
1014 lq_sta->total_success += tx_resp->ampdu_ack_map;
1015 lq_sta->total_failed +=
1016 (tx_resp->ampdu_ack_len - tx_resp->ampdu_ack_map);
1017 } else {
1018 if (status)
1019 lq_sta->total_success++;
1020 else
1021 lq_sta->total_failed++;
1022 }
Zhu Yib481de92007-09-25 17:54:57 -07001023 }
1024
Ben Cahill77626352007-11-29 11:09:44 +08001025 /* See if there's a better rate or modulation mode to try. */
Zhu Yib481de92007-09-25 17:54:57 -07001026 rs_rate_scale_perform(priv, dev, hdr, sta);
Tomas Winklerf4d60822008-03-05 11:31:00 -08001027out:
Johannes Bergd0709a62008-02-25 16:27:46 +01001028 rcu_read_unlock();
Zhu Yib481de92007-09-25 17:54:57 -07001029 return;
1030}
1031
1032static u8 rs_is_ant_connected(u8 valid_antenna,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001033 enum iwl4965_antenna_type antenna_type)
Zhu Yib481de92007-09-25 17:54:57 -07001034{
1035 if (antenna_type == ANT_AUX)
1036 return ((valid_antenna & 0x2) ? 1:0);
1037 else if (antenna_type == ANT_MAIN)
1038 return ((valid_antenna & 0x1) ? 1:0);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001039 else if (antenna_type == ANT_BOTH)
1040 return ((valid_antenna & 0x3) == 0x3);
Zhu Yib481de92007-09-25 17:54:57 -07001041
1042 return 1;
1043}
1044
1045static u8 rs_is_other_ant_connected(u8 valid_antenna,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001046 enum iwl4965_antenna_type antenna_type)
Zhu Yib481de92007-09-25 17:54:57 -07001047{
1048 if (antenna_type == ANT_AUX)
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001049 return rs_is_ant_connected(valid_antenna, ANT_MAIN);
Zhu Yib481de92007-09-25 17:54:57 -07001050 else
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001051 return rs_is_ant_connected(valid_antenna, ANT_AUX);
Zhu Yib481de92007-09-25 17:54:57 -07001052
1053 return 0;
1054}
1055
Ben Cahill77626352007-11-29 11:09:44 +08001056/*
1057 * Begin a period of staying with a selected modulation mode.
1058 * Set "stay_in_tbl" flag to prevent any mode switches.
1059 * Set frame tx success limits according to legacy vs. high-throughput,
1060 * and reset overall (spanning all rates) tx success history statistics.
1061 * These control how long we stay using same modulation mode before
1062 * searching for a new mode.
1063 */
Zhu Yib481de92007-09-25 17:54:57 -07001064static void rs_set_stay_in_table(u8 is_legacy,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001065 struct iwl4965_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07001066{
1067 IWL_DEBUG_HT("we are staying in the same table\n");
Tomas Winklerc33104f2008-01-14 17:46:21 -08001068 lq_sta->stay_in_tbl = 1; /* only place this gets set */
Zhu Yib481de92007-09-25 17:54:57 -07001069 if (is_legacy) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001070 lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT;
1071 lq_sta->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
1072 lq_sta->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
Zhu Yib481de92007-09-25 17:54:57 -07001073 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001074 lq_sta->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
1075 lq_sta->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
1076 lq_sta->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
Zhu Yib481de92007-09-25 17:54:57 -07001077 }
Tomas Winklerc33104f2008-01-14 17:46:21 -08001078 lq_sta->table_count = 0;
1079 lq_sta->total_failed = 0;
1080 lq_sta->total_success = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001081}
1082
Ben Cahill77626352007-11-29 11:09:44 +08001083/*
1084 * Find correct throughput table for given mode of modulation
1085 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001086static void rs_get_expected_tpt_table(struct iwl4965_lq_sta *lq_sta,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001087 struct iwl4965_scale_tbl_info *tbl)
Zhu Yib481de92007-09-25 17:54:57 -07001088{
1089 if (is_legacy(tbl->lq_type)) {
1090 if (!is_a_band(tbl->lq_type))
1091 tbl->expected_tpt = expected_tpt_G;
1092 else
1093 tbl->expected_tpt = expected_tpt_A;
1094 } else if (is_siso(tbl->lq_type)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001095 if (tbl->is_fat && !lq_sta->is_dup)
Zhu Yib481de92007-09-25 17:54:57 -07001096 if (tbl->is_SGI)
1097 tbl->expected_tpt = expected_tpt_siso40MHzSGI;
1098 else
1099 tbl->expected_tpt = expected_tpt_siso40MHz;
1100 else if (tbl->is_SGI)
1101 tbl->expected_tpt = expected_tpt_siso20MHzSGI;
1102 else
1103 tbl->expected_tpt = expected_tpt_siso20MHz;
1104
1105 } else if (is_mimo(tbl->lq_type)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001106 if (tbl->is_fat && !lq_sta->is_dup)
Zhu Yib481de92007-09-25 17:54:57 -07001107 if (tbl->is_SGI)
1108 tbl->expected_tpt = expected_tpt_mimo40MHzSGI;
1109 else
1110 tbl->expected_tpt = expected_tpt_mimo40MHz;
1111 else if (tbl->is_SGI)
1112 tbl->expected_tpt = expected_tpt_mimo20MHzSGI;
1113 else
1114 tbl->expected_tpt = expected_tpt_mimo20MHz;
1115 } else
1116 tbl->expected_tpt = expected_tpt_G;
1117}
1118
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001119#ifdef CONFIG_IWL4965_HT
Ben Cahill77626352007-11-29 11:09:44 +08001120/*
1121 * Find starting rate for new "search" high-throughput mode of modulation.
1122 * Goal is to find lowest expected rate (under perfect conditions) that is
1123 * above the current measured throughput of "active" mode, to give new mode
1124 * a fair chance to prove itself without too many challenges.
1125 *
1126 * This gets called when transitioning to more aggressive modulation
1127 * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
1128 * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
1129 * to decrease to match "active" throughput. When moving from MIMO to SISO,
1130 * bit rate will typically need to increase, but not if performance was bad.
1131 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001132static s32 rs_get_best_rate(struct iwl_priv *priv,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001133 struct iwl4965_lq_sta *lq_sta,
Ben Cahill77626352007-11-29 11:09:44 +08001134 struct iwl4965_scale_tbl_info *tbl, /* "search" */
Zhu Yib481de92007-09-25 17:54:57 -07001135 u16 rate_mask, s8 index, s8 rate)
1136{
Ben Cahill77626352007-11-29 11:09:44 +08001137 /* "active" values */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001138 struct iwl4965_scale_tbl_info *active_tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001139 &(lq_sta->lq_info[lq_sta->active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07001140 s32 active_sr = active_tbl->win[index].success_ratio;
Zhu Yib481de92007-09-25 17:54:57 -07001141 s32 active_tpt = active_tbl->expected_tpt[index];
Ben Cahill77626352007-11-29 11:09:44 +08001142
1143 /* expected "search" throughput */
1144 s32 *tpt_tbl = tbl->expected_tpt;
1145
1146 s32 new_rate, high, low, start_hi;
Zhu Yib481de92007-09-25 17:54:57 -07001147 u16 high_low;
1148
1149 new_rate = high = low = start_hi = IWL_RATE_INVALID;
1150
1151 for (; ;) {
1152 high_low = rs_get_adjacent_rate(rate, rate_mask, tbl->lq_type);
1153
1154 low = high_low & 0xff;
1155 high = (high_low >> 8) & 0xff;
1156
Ben Cahill77626352007-11-29 11:09:44 +08001157 /*
1158 * Lower the "search" bit rate, to give new "search" mode
1159 * approximately the same throughput as "active" if:
1160 *
1161 * 1) "Active" mode has been working modestly well (but not
1162 * great), and expected "search" throughput (under perfect
1163 * conditions) at candidate rate is above the actual
1164 * measured "active" throughput (but less than expected
1165 * "active" throughput under perfect conditions).
1166 * OR
1167 * 2) "Active" mode has been working perfectly or very well
1168 * and expected "search" throughput (under perfect
1169 * conditions) at candidate rate is above expected
1170 * "active" throughput (under perfect conditions).
1171 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001172 if ((((100 * tpt_tbl[rate]) > lq_sta->last_tpt) &&
Zhu Yib481de92007-09-25 17:54:57 -07001173 ((active_sr > IWL_RATE_DECREASE_TH) &&
1174 (active_sr <= IWL_RATE_HIGH_TH) &&
1175 (tpt_tbl[rate] <= active_tpt))) ||
1176 ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
1177 (tpt_tbl[rate] > active_tpt))) {
1178
Ben Cahill77626352007-11-29 11:09:44 +08001179 /* (2nd or later pass)
1180 * If we've already tried to raise the rate, and are
1181 * now trying to lower it, use the higher rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001182 if (start_hi != IWL_RATE_INVALID) {
1183 new_rate = start_hi;
1184 break;
1185 }
Ben Cahill77626352007-11-29 11:09:44 +08001186
Zhu Yib481de92007-09-25 17:54:57 -07001187 new_rate = rate;
Ben Cahill77626352007-11-29 11:09:44 +08001188
1189 /* Loop again with lower rate */
Zhu Yib481de92007-09-25 17:54:57 -07001190 if (low != IWL_RATE_INVALID)
1191 rate = low;
Ben Cahill77626352007-11-29 11:09:44 +08001192
1193 /* Lower rate not available, use the original */
Zhu Yib481de92007-09-25 17:54:57 -07001194 else
1195 break;
Ben Cahill77626352007-11-29 11:09:44 +08001196
1197 /* Else try to raise the "search" rate to match "active" */
Zhu Yib481de92007-09-25 17:54:57 -07001198 } else {
Ben Cahill77626352007-11-29 11:09:44 +08001199 /* (2nd or later pass)
1200 * If we've already tried to lower the rate, and are
1201 * now trying to raise it, use the lower rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001202 if (new_rate != IWL_RATE_INVALID)
1203 break;
Ben Cahill77626352007-11-29 11:09:44 +08001204
1205 /* Loop again with higher rate */
Zhu Yib481de92007-09-25 17:54:57 -07001206 else if (high != IWL_RATE_INVALID) {
1207 start_hi = high;
1208 rate = high;
Ben Cahill77626352007-11-29 11:09:44 +08001209
1210 /* Higher rate not available, use the original */
Zhu Yib481de92007-09-25 17:54:57 -07001211 } else {
1212 new_rate = rate;
1213 break;
1214 }
1215 }
1216 }
1217
1218 return new_rate;
1219}
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001220#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001221
1222static inline u8 rs_is_both_ant_supp(u8 valid_antenna)
1223{
1224 return (rs_is_ant_connected(valid_antenna, ANT_BOTH));
1225}
1226
Ben Cahill77626352007-11-29 11:09:44 +08001227/*
1228 * Set up search table for MIMO
1229 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001230static int rs_switch_to_mimo(struct iwl_priv *priv,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001231 struct iwl4965_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001232 struct ieee80211_conf *conf,
1233 struct sta_info *sta,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001234 struct iwl4965_scale_tbl_info *tbl, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001235{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001236#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07001237 u16 rate_mask;
1238 s32 rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001239 s8 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07001240
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001241 if (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) ||
1242 !sta->ht_info.ht_supported)
Zhu Yib481de92007-09-25 17:54:57 -07001243 return -1;
1244
1245 IWL_DEBUG_HT("LQ: try to switch to MIMO\n");
1246 tbl->lq_type = LQ_MIMO;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001247 rs_get_supported_rates(lq_sta, NULL, tbl->lq_type,
Zhu Yib481de92007-09-25 17:54:57 -07001248 &rate_mask);
1249
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001250 if (priv->current_ht_config.tx_mimo_ps_mode == IWL_MIMO_PS_STATIC)
Zhu Yib481de92007-09-25 17:54:57 -07001251 return -1;
1252
Ben Cahill77626352007-11-29 11:09:44 +08001253 /* Need both Tx chains/antennas to support MIMO */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001254 if (!rs_is_both_ant_supp(lq_sta->antenna))
Zhu Yib481de92007-09-25 17:54:57 -07001255 return -1;
1256
Tomas Winklerc33104f2008-01-14 17:46:21 -08001257 tbl->is_dup = lq_sta->is_dup;
Zhu Yib481de92007-09-25 17:54:57 -07001258 tbl->action = 0;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001259 if (priv->current_ht_config.supported_chan_width
1260 == IWL_CHANNEL_WIDTH_40MHZ)
Zhu Yib481de92007-09-25 17:54:57 -07001261 tbl->is_fat = 1;
1262 else
1263 tbl->is_fat = 0;
1264
1265 if (tbl->is_fat) {
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001266 if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
Zhu Yib481de92007-09-25 17:54:57 -07001267 tbl->is_SGI = 1;
1268 else
1269 tbl->is_SGI = 0;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001270 } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
Zhu Yib481de92007-09-25 17:54:57 -07001271 tbl->is_SGI = 1;
1272 else
1273 tbl->is_SGI = 0;
1274
Tomas Winklerc33104f2008-01-14 17:46:21 -08001275 rs_get_expected_tpt_table(lq_sta, tbl);
Zhu Yib481de92007-09-25 17:54:57 -07001276
Tomas Winklerc33104f2008-01-14 17:46:21 -08001277 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index, index);
Zhu Yib481de92007-09-25 17:54:57 -07001278
1279 IWL_DEBUG_HT("LQ: MIMO best rate %d mask %X\n", rate, rate_mask);
1280 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask))
1281 return -1;
1282 rs_mcs_from_tbl(&tbl->current_rate, tbl, rate, is_green);
1283
1284 IWL_DEBUG_HT("LQ: Switch to new mcs %X index is green %X\n",
1285 tbl->current_rate.rate_n_flags, is_green);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001286 return 0;
Mohamed Abbas403ab562007-11-06 22:06:25 -08001287#else
1288 return -1;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001289#endif /*CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001290}
1291
Ben Cahill77626352007-11-29 11:09:44 +08001292/*
1293 * Set up search table for SISO
1294 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001295static int rs_switch_to_siso(struct iwl_priv *priv,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001296 struct iwl4965_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001297 struct ieee80211_conf *conf,
1298 struct sta_info *sta,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001299 struct iwl4965_scale_tbl_info *tbl, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001300{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001301#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07001302 u16 rate_mask;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001303 u8 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07001304 s32 rate;
1305
1306 IWL_DEBUG_HT("LQ: try to switch to SISO\n");
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001307 if (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) ||
1308 !sta->ht_info.ht_supported)
Zhu Yib481de92007-09-25 17:54:57 -07001309 return -1;
1310
Tomas Winklerc33104f2008-01-14 17:46:21 -08001311 tbl->is_dup = lq_sta->is_dup;
Zhu Yib481de92007-09-25 17:54:57 -07001312 tbl->lq_type = LQ_SISO;
1313 tbl->action = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001314 rs_get_supported_rates(lq_sta, NULL, tbl->lq_type,
Zhu Yib481de92007-09-25 17:54:57 -07001315 &rate_mask);
1316
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001317 if (priv->current_ht_config.supported_chan_width
1318 == IWL_CHANNEL_WIDTH_40MHZ)
Zhu Yib481de92007-09-25 17:54:57 -07001319 tbl->is_fat = 1;
1320 else
1321 tbl->is_fat = 0;
1322
1323 if (tbl->is_fat) {
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001324 if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
Zhu Yib481de92007-09-25 17:54:57 -07001325 tbl->is_SGI = 1;
1326 else
1327 tbl->is_SGI = 0;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001328 } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
Zhu Yib481de92007-09-25 17:54:57 -07001329 tbl->is_SGI = 1;
1330 else
1331 tbl->is_SGI = 0;
1332
1333 if (is_green)
1334 tbl->is_SGI = 0;
1335
Tomas Winklerc33104f2008-01-14 17:46:21 -08001336 rs_get_expected_tpt_table(lq_sta, tbl);
1337 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index, index);
Zhu Yib481de92007-09-25 17:54:57 -07001338
1339 IWL_DEBUG_HT("LQ: get best rate %d mask %X\n", rate, rate_mask);
1340 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1341 IWL_DEBUG_HT("can not switch with index %d rate mask %x\n",
1342 rate, rate_mask);
1343 return -1;
1344 }
1345 rs_mcs_from_tbl(&tbl->current_rate, tbl, rate, is_green);
1346 IWL_DEBUG_HT("LQ: Switch to new mcs %X index is green %X\n",
1347 tbl->current_rate.rate_n_flags, is_green);
Mohamed Abbas403ab562007-11-06 22:06:25 -08001348 return 0;
1349#else
1350 return -1;
Zhu Yib481de92007-09-25 17:54:57 -07001351
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001352#endif /*CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001353}
1354
Ben Cahill77626352007-11-29 11:09:44 +08001355/*
1356 * Try to switch to new modulation mode from legacy
1357 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001358static int rs_move_legacy_other(struct iwl_priv *priv,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001359 struct iwl4965_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001360 struct ieee80211_conf *conf,
1361 struct sta_info *sta,
Zhu Yib481de92007-09-25 17:54:57 -07001362 int index)
1363{
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001364 int ret = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001365 struct iwl4965_scale_tbl_info *tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001366 &(lq_sta->lq_info[lq_sta->active_tbl]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001367 struct iwl4965_scale_tbl_info *search_tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001368 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001369 struct iwl4965_rate_scale_data *window = &(tbl->win[index]);
1370 u32 sz = (sizeof(struct iwl4965_scale_tbl_info) -
1371 (sizeof(struct iwl4965_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001372 u8 start_action = tbl->action;
1373
1374 for (; ;) {
1375 switch (tbl->action) {
1376 case IWL_LEGACY_SWITCH_ANTENNA:
1377 IWL_DEBUG_HT("LQ Legacy switch Antenna\n");
1378
1379 search_tbl->lq_type = LQ_NONE;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001380 lq_sta->action_counter++;
Ben Cahill77626352007-11-29 11:09:44 +08001381
1382 /* Don't change antenna if success has been great */
Zhu Yib481de92007-09-25 17:54:57 -07001383 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1384 break;
Ben Cahill77626352007-11-29 11:09:44 +08001385
1386 /* Don't change antenna if other one is not connected */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001387 if (!rs_is_other_ant_connected(lq_sta->antenna,
Zhu Yib481de92007-09-25 17:54:57 -07001388 tbl->antenna_type))
1389 break;
1390
Ben Cahill77626352007-11-29 11:09:44 +08001391 /* Set up search table to try other antenna */
Zhu Yib481de92007-09-25 17:54:57 -07001392 memcpy(search_tbl, tbl, sz);
1393
1394 rs_toggle_antenna(&(search_tbl->current_rate),
1395 search_tbl);
Tomas Winklerc33104f2008-01-14 17:46:21 -08001396 rs_get_expected_tpt_table(lq_sta, search_tbl);
1397 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001398 goto out;
1399
1400 case IWL_LEGACY_SWITCH_SISO:
1401 IWL_DEBUG_HT("LQ: Legacy switch to SISO\n");
Ben Cahill77626352007-11-29 11:09:44 +08001402
1403 /* Set up search table to try SISO */
Zhu Yib481de92007-09-25 17:54:57 -07001404 memcpy(search_tbl, tbl, sz);
1405 search_tbl->lq_type = LQ_SISO;
1406 search_tbl->is_SGI = 0;
1407 search_tbl->is_fat = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001408 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001409 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001410 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001411 lq_sta->search_better_tbl = 1;
1412 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001413 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001414 }
Zhu Yib481de92007-09-25 17:54:57 -07001415
1416 break;
1417 case IWL_LEGACY_SWITCH_MIMO:
1418 IWL_DEBUG_HT("LQ: Legacy switch MIMO\n");
Ben Cahill77626352007-11-29 11:09:44 +08001419
1420 /* Set up search table to try MIMO */
Zhu Yib481de92007-09-25 17:54:57 -07001421 memcpy(search_tbl, tbl, sz);
1422 search_tbl->lq_type = LQ_MIMO;
1423 search_tbl->is_SGI = 0;
1424 search_tbl->is_fat = 0;
1425 search_tbl->antenna_type = ANT_BOTH;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001426 ret = rs_switch_to_mimo(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001427 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001428 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001429 lq_sta->search_better_tbl = 1;
1430 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001431 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001432 }
Zhu Yib481de92007-09-25 17:54:57 -07001433 break;
1434 }
1435 tbl->action++;
1436 if (tbl->action > IWL_LEGACY_SWITCH_MIMO)
1437 tbl->action = IWL_LEGACY_SWITCH_ANTENNA;
1438
1439 if (tbl->action == start_action)
1440 break;
1441
1442 }
1443 return 0;
1444
1445 out:
1446 tbl->action++;
1447 if (tbl->action > IWL_LEGACY_SWITCH_MIMO)
1448 tbl->action = IWL_LEGACY_SWITCH_ANTENNA;
1449 return 0;
1450
1451}
1452
Ben Cahill77626352007-11-29 11:09:44 +08001453/*
1454 * Try to switch to new modulation mode from SISO
1455 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001456static int rs_move_siso_to_other(struct iwl_priv *priv,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001457 struct iwl4965_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001458 struct ieee80211_conf *conf,
1459 struct sta_info *sta,
Zhu Yib481de92007-09-25 17:54:57 -07001460 int index)
1461{
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001462 int ret;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001463 u8 is_green = lq_sta->is_green;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001464 struct iwl4965_scale_tbl_info *tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001465 &(lq_sta->lq_info[lq_sta->active_tbl]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001466 struct iwl4965_scale_tbl_info *search_tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001467 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001468 struct iwl4965_rate_scale_data *window = &(tbl->win[index]);
1469 u32 sz = (sizeof(struct iwl4965_scale_tbl_info) -
1470 (sizeof(struct iwl4965_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001471 u8 start_action = tbl->action;
1472
1473 for (;;) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001474 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001475 switch (tbl->action) {
1476 case IWL_SISO_SWITCH_ANTENNA:
1477 IWL_DEBUG_HT("LQ: SISO SWITCH ANTENNA SISO\n");
1478 search_tbl->lq_type = LQ_NONE;
1479 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1480 break;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001481 if (!rs_is_other_ant_connected(lq_sta->antenna,
Zhu Yib481de92007-09-25 17:54:57 -07001482 tbl->antenna_type))
1483 break;
1484
1485 memcpy(search_tbl, tbl, sz);
1486 search_tbl->action = IWL_SISO_SWITCH_MIMO;
1487 rs_toggle_antenna(&(search_tbl->current_rate),
1488 search_tbl);
Tomas Winklerc33104f2008-01-14 17:46:21 -08001489 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001490
1491 goto out;
1492
1493 case IWL_SISO_SWITCH_MIMO:
1494 IWL_DEBUG_HT("LQ: SISO SWITCH TO MIMO FROM SISO\n");
1495 memcpy(search_tbl, tbl, sz);
1496 search_tbl->lq_type = LQ_MIMO;
1497 search_tbl->is_SGI = 0;
1498 search_tbl->is_fat = 0;
1499 search_tbl->antenna_type = ANT_BOTH;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001500 ret = rs_switch_to_mimo(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001501 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001502 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001503 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001504 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001505 }
Zhu Yib481de92007-09-25 17:54:57 -07001506 break;
1507 case IWL_SISO_SWITCH_GI:
1508 IWL_DEBUG_HT("LQ: SISO SWITCH TO GI\n");
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001509
Zhu Yib481de92007-09-25 17:54:57 -07001510 memcpy(search_tbl, tbl, sz);
1511 search_tbl->action = 0;
1512 if (search_tbl->is_SGI)
1513 search_tbl->is_SGI = 0;
1514 else if (!is_green)
1515 search_tbl->is_SGI = 1;
1516 else
1517 break;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001518 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001519 if ((tbl->lq_type == LQ_SISO) &&
1520 (tbl->is_SGI)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001521 s32 tpt = lq_sta->last_tpt / 100;
Zhu Yib481de92007-09-25 17:54:57 -07001522 if (((!tbl->is_fat) &&
1523 (tpt >= expected_tpt_siso20MHz[index])) ||
1524 ((tbl->is_fat) &&
1525 (tpt >= expected_tpt_siso40MHz[index])))
Tomas Winklerc33104f2008-01-14 17:46:21 -08001526 lq_sta->search_better_tbl = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001527 }
Tomas Winklerc33104f2008-01-14 17:46:21 -08001528 rs_get_expected_tpt_table(lq_sta, search_tbl);
Zhu Yib481de92007-09-25 17:54:57 -07001529 rs_mcs_from_tbl(&search_tbl->current_rate,
1530 search_tbl, index, is_green);
1531 goto out;
1532 }
1533 tbl->action++;
1534 if (tbl->action > IWL_SISO_SWITCH_GI)
1535 tbl->action = IWL_SISO_SWITCH_ANTENNA;
1536
1537 if (tbl->action == start_action)
1538 break;
1539 }
1540 return 0;
1541
1542 out:
1543 tbl->action++;
1544 if (tbl->action > IWL_SISO_SWITCH_GI)
1545 tbl->action = IWL_SISO_SWITCH_ANTENNA;
1546 return 0;
1547}
1548
Ben Cahill77626352007-11-29 11:09:44 +08001549/*
1550 * Try to switch to new modulation mode from MIMO
1551 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001552static int rs_move_mimo_to_other(struct iwl_priv *priv,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001553 struct iwl4965_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001554 struct ieee80211_conf *conf,
1555 struct sta_info *sta,
Zhu Yib481de92007-09-25 17:54:57 -07001556 int index)
1557{
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001558 int ret;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001559 s8 is_green = lq_sta->is_green;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001560 struct iwl4965_scale_tbl_info *tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001561 &(lq_sta->lq_info[lq_sta->active_tbl]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001562 struct iwl4965_scale_tbl_info *search_tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001563 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001564 u32 sz = (sizeof(struct iwl4965_scale_tbl_info) -
1565 (sizeof(struct iwl4965_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001566 u8 start_action = tbl->action;
1567
1568 for (;;) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001569 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001570 switch (tbl->action) {
1571 case IWL_MIMO_SWITCH_ANTENNA_A:
1572 case IWL_MIMO_SWITCH_ANTENNA_B:
1573 IWL_DEBUG_HT("LQ: MIMO SWITCH TO SISO\n");
Ben Cahill77626352007-11-29 11:09:44 +08001574
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001575
Ben Cahill77626352007-11-29 11:09:44 +08001576 /* Set up new search table for SISO */
Zhu Yib481de92007-09-25 17:54:57 -07001577 memcpy(search_tbl, tbl, sz);
1578 search_tbl->lq_type = LQ_SISO;
1579 search_tbl->is_SGI = 0;
1580 search_tbl->is_fat = 0;
1581 if (tbl->action == IWL_MIMO_SWITCH_ANTENNA_A)
1582 search_tbl->antenna_type = ANT_MAIN;
1583 else
1584 search_tbl->antenna_type = ANT_AUX;
1585
Tomas Winklerc33104f2008-01-14 17:46:21 -08001586 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001587 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001588 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001589 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001590 goto out;
1591 }
1592 break;
1593
1594 case IWL_MIMO_SWITCH_GI:
1595 IWL_DEBUG_HT("LQ: MIMO SWITCH TO GI\n");
Ben Cahill77626352007-11-29 11:09:44 +08001596
1597 /* Set up new search table for MIMO */
Zhu Yib481de92007-09-25 17:54:57 -07001598 memcpy(search_tbl, tbl, sz);
1599 search_tbl->lq_type = LQ_MIMO;
1600 search_tbl->antenna_type = ANT_BOTH;
1601 search_tbl->action = 0;
1602 if (search_tbl->is_SGI)
1603 search_tbl->is_SGI = 0;
1604 else
1605 search_tbl->is_SGI = 1;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001606 lq_sta->search_better_tbl = 1;
Ben Cahill77626352007-11-29 11:09:44 +08001607
1608 /*
1609 * If active table already uses the fastest possible
1610 * modulation (dual stream with short guard interval),
1611 * and it's working well, there's no need to look
1612 * for a better type of modulation!
1613 */
Zhu Yib481de92007-09-25 17:54:57 -07001614 if ((tbl->lq_type == LQ_MIMO) &&
1615 (tbl->is_SGI)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001616 s32 tpt = lq_sta->last_tpt / 100;
Zhu Yib481de92007-09-25 17:54:57 -07001617 if (((!tbl->is_fat) &&
1618 (tpt >= expected_tpt_mimo20MHz[index])) ||
1619 ((tbl->is_fat) &&
1620 (tpt >= expected_tpt_mimo40MHz[index])))
Tomas Winklerc33104f2008-01-14 17:46:21 -08001621 lq_sta->search_better_tbl = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001622 }
Tomas Winklerc33104f2008-01-14 17:46:21 -08001623 rs_get_expected_tpt_table(lq_sta, search_tbl);
Zhu Yib481de92007-09-25 17:54:57 -07001624 rs_mcs_from_tbl(&search_tbl->current_rate,
1625 search_tbl, index, is_green);
1626 goto out;
1627
1628 }
1629 tbl->action++;
1630 if (tbl->action > IWL_MIMO_SWITCH_GI)
1631 tbl->action = IWL_MIMO_SWITCH_ANTENNA_A;
1632
1633 if (tbl->action == start_action)
1634 break;
1635 }
1636
1637 return 0;
1638 out:
1639 tbl->action++;
1640 if (tbl->action > IWL_MIMO_SWITCH_GI)
1641 tbl->action = IWL_MIMO_SWITCH_ANTENNA_A;
1642 return 0;
1643
1644}
1645
Ben Cahill77626352007-11-29 11:09:44 +08001646/*
1647 * Check whether we should continue using same modulation mode, or
1648 * begin search for a new mode, based on:
1649 * 1) # tx successes or failures while using this mode
1650 * 2) # times calling this function
1651 * 3) elapsed time in this mode (not used, for now)
1652 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001653static void rs_stay_in_table(struct iwl4965_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07001654{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001655 struct iwl4965_scale_tbl_info *tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001656 int i;
1657 int active_tbl;
1658 int flush_interval_passed = 0;
1659
Tomas Winklerc33104f2008-01-14 17:46:21 -08001660 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001661
Tomas Winklerc33104f2008-01-14 17:46:21 -08001662 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07001663
Ben Cahill77626352007-11-29 11:09:44 +08001664 /* If we've been disallowing search, see if we should now allow it */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001665 if (lq_sta->stay_in_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07001666
Ben Cahill77626352007-11-29 11:09:44 +08001667 /* Elapsed time using current modulation mode */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001668 if (lq_sta->flush_timer)
Zhu Yib481de92007-09-25 17:54:57 -07001669 flush_interval_passed =
1670 time_after(jiffies,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001671 (unsigned long)(lq_sta->flush_timer +
Zhu Yib481de92007-09-25 17:54:57 -07001672 IWL_RATE_SCALE_FLUSH_INTVL));
1673
Ben Cahill77626352007-11-29 11:09:44 +08001674 /* For now, disable the elapsed time criterion */
Zhu Yib481de92007-09-25 17:54:57 -07001675 flush_interval_passed = 0;
Ben Cahill77626352007-11-29 11:09:44 +08001676
1677 /*
1678 * Check if we should allow search for new modulation mode.
1679 * If many frames have failed or succeeded, or we've used
1680 * this same modulation for a long time, allow search, and
1681 * reset history stats that keep track of whether we should
1682 * allow a new search. Also (below) reset all bitmaps and
1683 * stats in active history.
1684 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001685 if ((lq_sta->total_failed > lq_sta->max_failure_limit) ||
1686 (lq_sta->total_success > lq_sta->max_success_limit) ||
1687 ((!lq_sta->search_better_tbl) && (lq_sta->flush_timer)
Zhu Yib481de92007-09-25 17:54:57 -07001688 && (flush_interval_passed))) {
1689 IWL_DEBUG_HT("LQ: stay is expired %d %d %d\n:",
Tomas Winklerc33104f2008-01-14 17:46:21 -08001690 lq_sta->total_failed,
1691 lq_sta->total_success,
Zhu Yib481de92007-09-25 17:54:57 -07001692 flush_interval_passed);
Ben Cahill77626352007-11-29 11:09:44 +08001693
1694 /* Allow search for new mode */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001695 lq_sta->stay_in_tbl = 0; /* only place reset */
1696 lq_sta->total_failed = 0;
1697 lq_sta->total_success = 0;
1698 lq_sta->flush_timer = 0;
Ben Cahill77626352007-11-29 11:09:44 +08001699
1700 /*
1701 * Else if we've used this modulation mode enough repetitions
1702 * (regardless of elapsed time or success/failure), reset
1703 * history bitmaps and rate-specific stats for all rates in
1704 * active table.
1705 */
Mohamed Abbas403ab562007-11-06 22:06:25 -08001706 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001707 lq_sta->table_count++;
1708 if (lq_sta->table_count >=
1709 lq_sta->table_count_limit) {
1710 lq_sta->table_count = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001711
1712 IWL_DEBUG_HT("LQ: stay in table clear win\n");
1713 for (i = 0; i < IWL_RATE_COUNT; i++)
1714 rs_rate_scale_clear_window(
1715 &(tbl->win[i]));
1716 }
1717 }
1718
Ben Cahill77626352007-11-29 11:09:44 +08001719 /* If transitioning to allow "search", reset all history
1720 * bitmaps and stats in active table (this will become the new
1721 * "search" table). */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001722 if (!lq_sta->stay_in_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07001723 for (i = 0; i < IWL_RATE_COUNT; i++)
1724 rs_rate_scale_clear_window(&(tbl->win[i]));
1725 }
1726 }
1727}
1728
Ben Cahill77626352007-11-29 11:09:44 +08001729/*
1730 * Do rate scaling and search for new modulation mode.
1731 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001732static void rs_rate_scale_perform(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001733 struct net_device *dev,
1734 struct ieee80211_hdr *hdr,
1735 struct sta_info *sta)
1736{
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001737 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1738 struct ieee80211_hw *hw = local_to_hw(local);
1739 struct ieee80211_conf *conf = &hw->conf;
Zhu Yib481de92007-09-25 17:54:57 -07001740 int low = IWL_RATE_INVALID;
1741 int high = IWL_RATE_INVALID;
1742 int index;
1743 int i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001744 struct iwl4965_rate_scale_data *window = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001745 int current_tpt = IWL_INVALID_VALUE;
1746 int low_tpt = IWL_INVALID_VALUE;
1747 int high_tpt = IWL_INVALID_VALUE;
1748 u32 fail_count;
1749 s8 scale_action = 0;
1750 u16 fc, rate_mask;
1751 u8 update_lq = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001752 struct iwl4965_lq_sta *lq_sta;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001753 struct iwl4965_scale_tbl_info *tbl, *tbl1;
Zhu Yib481de92007-09-25 17:54:57 -07001754 u16 rate_scale_index_msk = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001755 struct iwl4965_rate mcs_rate;
Zhu Yib481de92007-09-25 17:54:57 -07001756 u8 is_green = 0;
1757 u8 active_tbl = 0;
1758 u8 done_search = 0;
1759 u16 high_low;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001760#ifdef CONFIG_IWL4965_HT
1761 u8 tid = MAX_TID_COUNT;
1762 __le16 *qc;
1763#endif
Zhu Yib481de92007-09-25 17:54:57 -07001764
1765 IWL_DEBUG_RATE("rate scale calculate new rate for skb\n");
1766
1767 fc = le16_to_cpu(hdr->frame_control);
1768 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1)) {
1769 /* Send management frames and broadcast/multicast data using
1770 * lowest rate. */
1771 /* TODO: this could probably be improved.. */
1772 return;
1773 }
1774
1775 if (!sta || !sta->rate_ctrl_priv)
1776 return;
1777
1778 if (!priv->lq_mngr.lq_ready) {
1779 IWL_DEBUG_RATE("still rate scaling not ready\n");
1780 return;
1781 }
Tomas Winklerc33104f2008-01-14 17:46:21 -08001782 lq_sta = (struct iwl4965_lq_sta *)sta->rate_ctrl_priv;
Zhu Yib481de92007-09-25 17:54:57 -07001783
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001784#ifdef CONFIG_IWL4965_HT
1785 qc = ieee80211_get_qos_ctrl(hdr);
1786 if (qc) {
1787 tid = (u8)(le16_to_cpu(*qc) & 0xf);
1788 rs_tl_add_packet(lq_sta, tid);
1789 }
1790#endif
Ben Cahill77626352007-11-29 11:09:44 +08001791 /*
1792 * Select rate-scale / modulation-mode table to work with in
1793 * the rest of this function: "search" if searching for better
1794 * modulation mode, or "active" if doing rate scaling within a mode.
1795 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001796 if (!lq_sta->search_better_tbl)
1797 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001798 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08001799 active_tbl = 1 - lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001800
Tomas Winklerc33104f2008-01-14 17:46:21 -08001801 tbl = &(lq_sta->lq_info[active_tbl]);
1802 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07001803
Ben Cahill77626352007-11-29 11:09:44 +08001804 /* current tx rate */
Johannes Berg8318d782008-01-24 19:38:38 +01001805 index = sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07001806
1807 IWL_DEBUG_RATE("Rate scale index %d for type %d\n", index,
1808 tbl->lq_type);
1809
Ben Cahill77626352007-11-29 11:09:44 +08001810 /* rates available for this association, and for modulation mode */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001811 rs_get_supported_rates(lq_sta, hdr, tbl->lq_type,
Zhu Yib481de92007-09-25 17:54:57 -07001812 &rate_mask);
1813
1814 IWL_DEBUG_RATE("mask 0x%04X \n", rate_mask);
1815
1816 /* mask with station rate restriction */
1817 if (is_legacy(tbl->lq_type)) {
Johannes Berg8318d782008-01-24 19:38:38 +01001818 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Ben Cahill77626352007-11-29 11:09:44 +08001819 /* supp_rates has no CCK bits in A mode */
Zhu Yib481de92007-09-25 17:54:57 -07001820 rate_scale_index_msk = (u16) (rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -08001821 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
Zhu Yib481de92007-09-25 17:54:57 -07001822 else
1823 rate_scale_index_msk = (u16) (rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -08001824 lq_sta->supp_rates);
Zhu Yib481de92007-09-25 17:54:57 -07001825
1826 } else
1827 rate_scale_index_msk = rate_mask;
1828
1829 if (!rate_scale_index_msk)
1830 rate_scale_index_msk = rate_mask;
1831
Ben Cahill77626352007-11-29 11:09:44 +08001832 /* If current rate is no longer supported on current association,
1833 * or user changed preferences for rates, find a new supported rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001834 if (index < 0 || !((1 << index) & rate_scale_index_msk)) {
1835 index = IWL_INVALID_VALUE;
1836 update_lq = 1;
1837
Ben Cahill77626352007-11-29 11:09:44 +08001838 /* get the highest available rate */
Zhu Yib481de92007-09-25 17:54:57 -07001839 for (i = 0; i <= IWL_RATE_COUNT; i++) {
1840 if ((1 << i) & rate_scale_index_msk)
1841 index = i;
1842 }
1843
1844 if (index == IWL_INVALID_VALUE) {
1845 IWL_WARNING("Can not find a suitable rate\n");
1846 return;
1847 }
1848 }
1849
Ben Cahill77626352007-11-29 11:09:44 +08001850 /* Get expected throughput table and history window for current rate */
Zhu Yib481de92007-09-25 17:54:57 -07001851 if (!tbl->expected_tpt)
Tomas Winklerc33104f2008-01-14 17:46:21 -08001852 rs_get_expected_tpt_table(lq_sta, tbl);
Zhu Yib481de92007-09-25 17:54:57 -07001853
1854 window = &(tbl->win[index]);
1855
Ben Cahill77626352007-11-29 11:09:44 +08001856 /*
1857 * If there is not enough history to calculate actual average
1858 * throughput, keep analyzing results of more tx frames, without
1859 * changing rate or mode (bypass most of the rest of this function).
1860 * Set up new rate table in uCode only if old rate is not supported
1861 * in current association (use new rate found above).
1862 */
Zhu Yib481de92007-09-25 17:54:57 -07001863 fail_count = window->counter - window->success_counter;
1864 if (((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
1865 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH))
1866 || (tbl->expected_tpt == NULL)) {
1867 IWL_DEBUG_RATE("LQ: still below TH succ %d total %d "
1868 "for index %d\n",
1869 window->success_counter, window->counter, index);
Ben Cahill77626352007-11-29 11:09:44 +08001870
1871 /* Can't calculate this yet; not enough history */
Zhu Yib481de92007-09-25 17:54:57 -07001872 window->average_tpt = IWL_INVALID_VALUE;
Ben Cahill77626352007-11-29 11:09:44 +08001873
1874 /* Should we stay with this modulation mode,
1875 * or search for a new one? */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001876 rs_stay_in_table(lq_sta);
Ben Cahill77626352007-11-29 11:09:44 +08001877
1878 /* Set up new rate table in uCode, if needed */
Zhu Yib481de92007-09-25 17:54:57 -07001879 if (update_lq) {
1880 rs_mcs_from_tbl(&mcs_rate, tbl, index, is_green);
Tomas Winklerc33104f2008-01-14 17:46:21 -08001881 rs_fill_link_cmd(lq_sta, &mcs_rate, &lq_sta->lq);
1882 rs_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07001883 }
1884 goto out;
1885
Ben Cahill77626352007-11-29 11:09:44 +08001886 /* Else we have enough samples; calculate estimate of
1887 * actual average throughput */
Zhu Yib481de92007-09-25 17:54:57 -07001888 } else
1889 window->average_tpt = ((window->success_ratio *
1890 tbl->expected_tpt[index] + 64) / 128);
1891
Ben Cahill77626352007-11-29 11:09:44 +08001892 /* If we are searching for better modulation mode, check success. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001893 if (lq_sta->search_better_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07001894 int success_limit = IWL_RATE_SCALE_SWITCH;
1895
Ben Cahill77626352007-11-29 11:09:44 +08001896 /* If good success, continue using the "search" mode;
1897 * no need to send new link quality command, since we're
1898 * continuing to use the setup that we've been trying. */
Zhu Yib481de92007-09-25 17:54:57 -07001899 if ((window->success_ratio > success_limit) ||
Tomas Winklerc33104f2008-01-14 17:46:21 -08001900 (window->average_tpt > lq_sta->last_tpt)) {
Zhu Yib481de92007-09-25 17:54:57 -07001901 if (!is_legacy(tbl->lq_type)) {
1902 IWL_DEBUG_HT("LQ: we are switching to HT"
1903 " rate suc %d current tpt %d"
1904 " old tpt %d\n",
1905 window->success_ratio,
1906 window->average_tpt,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001907 lq_sta->last_tpt);
1908 lq_sta->enable_counter = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001909 }
Ben Cahill77626352007-11-29 11:09:44 +08001910 /* Swap tables; "search" becomes "active" */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001911 lq_sta->active_tbl = active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001912 current_tpt = window->average_tpt;
Ben Cahill77626352007-11-29 11:09:44 +08001913
1914 /* Else poor success; go back to mode in "active" table */
Zhu Yib481de92007-09-25 17:54:57 -07001915 } else {
Ben Cahill77626352007-11-29 11:09:44 +08001916 /* Nullify "search" table */
Zhu Yib481de92007-09-25 17:54:57 -07001917 tbl->lq_type = LQ_NONE;
Ben Cahill77626352007-11-29 11:09:44 +08001918
1919 /* Revert to "active" table */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001920 active_tbl = lq_sta->active_tbl;
1921 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07001922
Ben Cahill77626352007-11-29 11:09:44 +08001923 /* Revert to "active" rate and throughput info */
Tomas Winkler17744ff2008-03-02 01:52:00 +02001924 index = iwl4965_hwrate_to_plcp_idx(
Zhu Yib481de92007-09-25 17:54:57 -07001925 tbl->current_rate.rate_n_flags);
Tomas Winklerc33104f2008-01-14 17:46:21 -08001926 current_tpt = lq_sta->last_tpt;
Ben Cahill77626352007-11-29 11:09:44 +08001927
1928 /* Need to set up a new rate table in uCode */
1929 update_lq = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001930 IWL_DEBUG_HT("XXY GO BACK TO OLD TABLE\n");
1931 }
Ben Cahill77626352007-11-29 11:09:44 +08001932
1933 /* Either way, we've made a decision; modulation mode
1934 * search is done, allow rate adjustment next time. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001935 lq_sta->search_better_tbl = 0;
Ben Cahill77626352007-11-29 11:09:44 +08001936 done_search = 1; /* Don't switch modes below! */
Zhu Yib481de92007-09-25 17:54:57 -07001937 goto lq_update;
1938 }
1939
Ben Cahill77626352007-11-29 11:09:44 +08001940 /* (Else) not in search of better modulation mode, try for better
1941 * starting rate, while staying in this mode. */
Zhu Yib481de92007-09-25 17:54:57 -07001942 high_low = rs_get_adjacent_rate(index, rate_scale_index_msk,
1943 tbl->lq_type);
1944 low = high_low & 0xff;
1945 high = (high_low >> 8) & 0xff;
1946
Ben Cahill77626352007-11-29 11:09:44 +08001947 /* Collect measured throughputs for current and adjacent rates */
Zhu Yib481de92007-09-25 17:54:57 -07001948 current_tpt = window->average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07001949 if (low != IWL_RATE_INVALID)
1950 low_tpt = tbl->win[low].average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07001951 if (high != IWL_RATE_INVALID)
1952 high_tpt = tbl->win[high].average_tpt;
1953
Ben Cahill77626352007-11-29 11:09:44 +08001954 /* Assume rate increase */
Zhu Yib481de92007-09-25 17:54:57 -07001955 scale_action = 1;
1956
Ben Cahill77626352007-11-29 11:09:44 +08001957 /* Too many failures, decrease rate */
Zhu Yib481de92007-09-25 17:54:57 -07001958 if ((window->success_ratio <= IWL_RATE_DECREASE_TH) ||
1959 (current_tpt == 0)) {
1960 IWL_DEBUG_RATE("decrease rate because of low success_ratio\n");
1961 scale_action = -1;
Ben Cahill77626352007-11-29 11:09:44 +08001962
1963 /* No throughput measured yet for adjacent rates; try increase. */
Zhu Yib481de92007-09-25 17:54:57 -07001964 } else if ((low_tpt == IWL_INVALID_VALUE) &&
1965 (high_tpt == IWL_INVALID_VALUE))
1966 scale_action = 1;
Ben Cahill77626352007-11-29 11:09:44 +08001967
1968 /* Both adjacent throughputs are measured, but neither one has better
1969 * throughput; we're using the best rate, don't change it! */
Zhu Yib481de92007-09-25 17:54:57 -07001970 else if ((low_tpt != IWL_INVALID_VALUE) &&
1971 (high_tpt != IWL_INVALID_VALUE) &&
1972 (low_tpt < current_tpt) &&
1973 (high_tpt < current_tpt))
1974 scale_action = 0;
Ben Cahill77626352007-11-29 11:09:44 +08001975
1976 /* At least one adjacent rate's throughput is measured,
1977 * and may have better performance. */
Zhu Yib481de92007-09-25 17:54:57 -07001978 else {
Ben Cahill77626352007-11-29 11:09:44 +08001979 /* Higher adjacent rate's throughput is measured */
Zhu Yib481de92007-09-25 17:54:57 -07001980 if (high_tpt != IWL_INVALID_VALUE) {
Ben Cahill77626352007-11-29 11:09:44 +08001981 /* Higher rate has better throughput */
Zhu Yib481de92007-09-25 17:54:57 -07001982 if (high_tpt > current_tpt)
1983 scale_action = 1;
1984 else {
1985 IWL_DEBUG_RATE
1986 ("decrease rate because of high tpt\n");
1987 scale_action = -1;
1988 }
Ben Cahill77626352007-11-29 11:09:44 +08001989
1990 /* Lower adjacent rate's throughput is measured */
Zhu Yib481de92007-09-25 17:54:57 -07001991 } else if (low_tpt != IWL_INVALID_VALUE) {
Ben Cahill77626352007-11-29 11:09:44 +08001992 /* Lower rate has better throughput */
Zhu Yib481de92007-09-25 17:54:57 -07001993 if (low_tpt > current_tpt) {
1994 IWL_DEBUG_RATE
1995 ("decrease rate because of low tpt\n");
1996 scale_action = -1;
1997 } else
1998 scale_action = 1;
1999 }
2000 }
2001
Ben Cahill77626352007-11-29 11:09:44 +08002002 /* Sanity check; asked for decrease, but success rate or throughput
2003 * has been good at old rate. Don't change it. */
Zhu Yib481de92007-09-25 17:54:57 -07002004 if (scale_action == -1) {
2005 if ((low != IWL_RATE_INVALID) &&
2006 ((window->success_ratio > IWL_RATE_HIGH_TH) ||
2007 (current_tpt > (100 * tbl->expected_tpt[low]))))
2008 scale_action = 0;
Ben Cahill77626352007-11-29 11:09:44 +08002009
2010 /* Sanity check; asked for increase, but success rate has not been great
2011 * even at old rate, higher rate will be worse. Don't change it. */
Zhu Yib481de92007-09-25 17:54:57 -07002012 } else if ((scale_action == 1) &&
2013 (window->success_ratio < IWL_RATE_INCREASE_TH))
2014 scale_action = 0;
2015
2016 switch (scale_action) {
2017 case -1:
Ben Cahill77626352007-11-29 11:09:44 +08002018 /* Decrease starting rate, update uCode's rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002019 if (low != IWL_RATE_INVALID) {
2020 update_lq = 1;
2021 index = low;
2022 }
2023 break;
2024 case 1:
Ben Cahill77626352007-11-29 11:09:44 +08002025 /* Increase starting rate, update uCode's rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002026 if (high != IWL_RATE_INVALID) {
2027 update_lq = 1;
2028 index = high;
2029 }
2030
2031 break;
2032 case 0:
Ben Cahill77626352007-11-29 11:09:44 +08002033 /* No change */
Zhu Yib481de92007-09-25 17:54:57 -07002034 default:
2035 break;
2036 }
2037
2038 IWL_DEBUG_HT("choose rate scale index %d action %d low %d "
2039 "high %d type %d\n",
2040 index, scale_action, low, high, tbl->lq_type);
2041
2042 lq_update:
Ben Cahill77626352007-11-29 11:09:44 +08002043 /* Replace uCode's rate table for the destination station. */
Zhu Yib481de92007-09-25 17:54:57 -07002044 if (update_lq) {
2045 rs_mcs_from_tbl(&mcs_rate, tbl, index, is_green);
Tomas Winklerc33104f2008-01-14 17:46:21 -08002046 rs_fill_link_cmd(lq_sta, &mcs_rate, &lq_sta->lq);
2047 rs_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07002048 }
Ben Cahill77626352007-11-29 11:09:44 +08002049
2050 /* Should we stay with this modulation mode, or search for a new one? */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002051 rs_stay_in_table(lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002052
Ben Cahill77626352007-11-29 11:09:44 +08002053 /*
2054 * Search for new modulation mode if we're:
2055 * 1) Not changing rates right now
2056 * 2) Not just finishing up a search
2057 * 3) Allowing a new search
2058 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002059 if (!update_lq && !done_search && !lq_sta->stay_in_tbl) {
Ben Cahill77626352007-11-29 11:09:44 +08002060 /* Save current throughput to compare with "search" throughput*/
Tomas Winklerc33104f2008-01-14 17:46:21 -08002061 lq_sta->last_tpt = current_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07002062
Ben Cahill77626352007-11-29 11:09:44 +08002063 /* Select a new "search" modulation mode to try.
2064 * If one is found, set up the new "search" table. */
Zhu Yib481de92007-09-25 17:54:57 -07002065 if (is_legacy(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08002066 rs_move_legacy_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07002067 else if (is_siso(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08002068 rs_move_siso_to_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07002069 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002070 rs_move_mimo_to_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07002071
Ben Cahill77626352007-11-29 11:09:44 +08002072 /* If new "search" mode was selected, set up in uCode table */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002073 if (lq_sta->search_better_tbl) {
Ben Cahill77626352007-11-29 11:09:44 +08002074 /* Access the "search" table, clear its history. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002075 tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Zhu Yib481de92007-09-25 17:54:57 -07002076 for (i = 0; i < IWL_RATE_COUNT; i++)
2077 rs_rate_scale_clear_window(&(tbl->win[i]));
2078
Ben Cahill77626352007-11-29 11:09:44 +08002079 /* Use new "search" start rate */
Tomas Winkler17744ff2008-03-02 01:52:00 +02002080 index = iwl4965_hwrate_to_plcp_idx(
Zhu Yib481de92007-09-25 17:54:57 -07002081 tbl->current_rate.rate_n_flags);
2082
2083 IWL_DEBUG_HT("Switch current mcs: %X index: %d\n",
2084 tbl->current_rate.rate_n_flags, index);
Tomas Winklerc33104f2008-01-14 17:46:21 -08002085 rs_fill_link_cmd(lq_sta, &tbl->current_rate,
2086 &lq_sta->lq);
2087 rs_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07002088 }
Zhu Yib481de92007-09-25 17:54:57 -07002089
Ben Cahill77626352007-11-29 11:09:44 +08002090 /* If the "active" (non-search) mode was legacy,
2091 * and we've tried switching antennas,
2092 * but we haven't been able to try HT modes (not available),
2093 * stay with best antenna legacy modulation for a while
2094 * before next round of mode comparisons. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002095 tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07002096 if (is_legacy(tbl1->lq_type) &&
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002097#ifdef CONFIG_IWL4965_HT
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002098 (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE)) &&
Zhu Yib481de92007-09-25 17:54:57 -07002099#endif
Tomas Winklerc33104f2008-01-14 17:46:21 -08002100 (lq_sta->action_counter >= 1)) {
2101 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002102 IWL_DEBUG_HT("LQ: STAY in legacy table\n");
Tomas Winklerc33104f2008-01-14 17:46:21 -08002103 rs_set_stay_in_table(1, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002104 }
2105
Ben Cahill77626352007-11-29 11:09:44 +08002106 /* If we're in an HT mode, and all 3 mode switch actions
2107 * have been tried and compared, stay in this best modulation
2108 * mode for a while before next round of mode comparisons. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002109 if (lq_sta->enable_counter &&
2110 (lq_sta->action_counter >= IWL_ACTION_LIMIT)) {
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002111#ifdef CONFIG_IWL4965_HT
2112 if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
2113 (lq_sta->tx_agg_tid_en & (1 << tid)) &&
2114 (tid != MAX_TID_COUNT)) {
2115 IWL_DEBUG_HT("try to aggregate tid %d\n", tid);
2116 rs_tl_turn_on_agg(priv, tid, lq_sta, sta);
Zhu Yib481de92007-09-25 17:54:57 -07002117 }
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002118#endif /*CONFIG_IWL4965_HT */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002119 lq_sta->action_counter = 0;
2120 rs_set_stay_in_table(0, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002121 }
Ben Cahill77626352007-11-29 11:09:44 +08002122
2123 /*
2124 * Else, don't search for a new modulation mode.
2125 * Put new timestamp in stay-in-modulation-mode flush timer if:
2126 * 1) Not changing rates right now
2127 * 2) Not just finishing up a search
2128 * 3) flush timer is empty
2129 */
Zhu Yib481de92007-09-25 17:54:57 -07002130 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002131 if ((!update_lq) && (!done_search) && (!lq_sta->flush_timer))
2132 lq_sta->flush_timer = jiffies;
Zhu Yib481de92007-09-25 17:54:57 -07002133 }
2134
2135out:
2136 rs_mcs_from_tbl(&tbl->current_rate, tbl, index, is_green);
2137 i = index;
Johannes Berg8318d782008-01-24 19:38:38 +01002138 sta->last_txrate_idx = i;
Zhu Yib481de92007-09-25 17:54:57 -07002139
Johannes Berg8318d782008-01-24 19:38:38 +01002140 /* sta->txrate_idx is an index to A mode rates which start
Zhu Yib481de92007-09-25 17:54:57 -07002141 * at IWL_FIRST_OFDM_RATE
2142 */
Johannes Berg8318d782008-01-24 19:38:38 +01002143 if (lq_sta->band == IEEE80211_BAND_5GHZ)
2144 sta->txrate_idx = i - IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -07002145 else
Johannes Berg8318d782008-01-24 19:38:38 +01002146 sta->txrate_idx = i;
Zhu Yib481de92007-09-25 17:54:57 -07002147
2148 return;
2149}
2150
2151
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002152static void rs_initialize_lq(struct iwl_priv *priv,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002153 struct ieee80211_conf *conf,
Zhu Yib481de92007-09-25 17:54:57 -07002154 struct sta_info *sta)
2155{
2156 int i;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002157 struct iwl4965_lq_sta *lq_sta;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002158 struct iwl4965_scale_tbl_info *tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002159 u8 active_tbl = 0;
2160 int rate_idx;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002161 u8 use_green = rs_use_green(priv, conf);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002162 struct iwl4965_rate mcs_rate;
Zhu Yib481de92007-09-25 17:54:57 -07002163
2164 if (!sta || !sta->rate_ctrl_priv)
2165 goto out;
2166
Tomas Winklerc33104f2008-01-14 17:46:21 -08002167 lq_sta = (struct iwl4965_lq_sta *)sta->rate_ctrl_priv;
Johannes Berg8318d782008-01-24 19:38:38 +01002168 i = sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002169
Tomas Winklerc33104f2008-01-14 17:46:21 -08002170 if ((lq_sta->lq.sta_id == 0xff) &&
Zhu Yib481de92007-09-25 17:54:57 -07002171 (priv->iw_mode == IEEE80211_IF_TYPE_IBSS))
2172 goto out;
2173
Tomas Winklerc33104f2008-01-14 17:46:21 -08002174 if (!lq_sta->search_better_tbl)
2175 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002176 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002177 active_tbl = 1 - lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002178
Tomas Winklerc33104f2008-01-14 17:46:21 -08002179 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07002180
2181 if ((i < 0) || (i >= IWL_RATE_COUNT))
2182 i = 0;
2183
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002184 mcs_rate.rate_n_flags = iwl4965_rates[i].plcp ;
Zhu Yib481de92007-09-25 17:54:57 -07002185 mcs_rate.rate_n_flags |= RATE_MCS_ANT_B_MSK;
2186 mcs_rate.rate_n_flags &= ~RATE_MCS_ANT_A_MSK;
2187
2188 if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
2189 mcs_rate.rate_n_flags |= RATE_MCS_CCK_MSK;
2190
2191 tbl->antenna_type = ANT_AUX;
Johannes Berg8318d782008-01-24 19:38:38 +01002192 rs_get_tbl_info_from_mcs(&mcs_rate, priv->band, tbl, &rate_idx);
Zhu Yib481de92007-09-25 17:54:57 -07002193 if (!rs_is_ant_connected(priv->valid_antenna, tbl->antenna_type))
Zhu Yi46640a82007-09-27 11:27:34 +08002194 rs_toggle_antenna(&mcs_rate, tbl);
Zhu Yib481de92007-09-25 17:54:57 -07002195
2196 rs_mcs_from_tbl(&mcs_rate, tbl, rate_idx, use_green);
2197 tbl->current_rate.rate_n_flags = mcs_rate.rate_n_flags;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002198 rs_get_expected_tpt_table(lq_sta, tbl);
2199 rs_fill_link_cmd(lq_sta, &mcs_rate, &lq_sta->lq);
2200 rs_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07002201 out:
2202 return;
2203}
2204
Mattias Nissler1abbe492007-12-20 13:50:07 +01002205static void rs_get_rate(void *priv_rate, struct net_device *dev,
Johannes Berg8318d782008-01-24 19:38:38 +01002206 struct ieee80211_supported_band *sband,
2207 struct sk_buff *skb,
Mattias Nissler1abbe492007-12-20 13:50:07 +01002208 struct rate_selection *sel)
Zhu Yib481de92007-09-25 17:54:57 -07002209{
2210
2211 int i;
2212 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002213 struct ieee80211_conf *conf = &local->hw.conf;
Zhu Yib481de92007-09-25 17:54:57 -07002214 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2215 struct sta_info *sta;
Michael Wu2bc454b2007-12-25 19:33:16 -05002216 u16 fc;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002217 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002218 struct iwl4965_lq_sta *lq_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002219
Zhu Yi58826352007-09-27 11:27:39 +08002220 IWL_DEBUG_RATE_LIMIT("rate scale calculate new rate for skb\n");
Zhu Yib481de92007-09-25 17:54:57 -07002221
Johannes Bergd0709a62008-02-25 16:27:46 +01002222 rcu_read_lock();
2223
Zhu Yib481de92007-09-25 17:54:57 -07002224 sta = sta_info_get(local, hdr->addr1);
2225
Michael Wu2bc454b2007-12-25 19:33:16 -05002226 /* Send management frames and broadcast/multicast data using lowest
2227 * rate. */
2228 fc = le16_to_cpu(hdr->frame_control);
2229 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1) ||
2230 !sta || !sta->rate_ctrl_priv) {
Johannes Berg8318d782008-01-24 19:38:38 +01002231 sel->rate = rate_lowest(local, sband, sta);
Tomas Winklerf4d60822008-03-05 11:31:00 -08002232 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07002233 }
2234
Tomas Winklerc33104f2008-01-14 17:46:21 -08002235 lq_sta = (struct iwl4965_lq_sta *)sta->rate_ctrl_priv;
Johannes Berg8318d782008-01-24 19:38:38 +01002236 i = sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002237
Tomas Winklerc33104f2008-01-14 17:46:21 -08002238 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
2239 !lq_sta->ibss_sta_added) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002240 u8 sta_id = iwl4965_hw_find_station(priv, hdr->addr1);
Joe Perches0795af52007-10-03 17:59:30 -07002241 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07002242
2243 if (sta_id == IWL_INVALID_STATION) {
Joe Perches0795af52007-10-03 17:59:30 -07002244 IWL_DEBUG_RATE("LQ: ADD station %s\n",
2245 print_mac(mac, hdr->addr1));
Ron Rindjunsky67d62032007-11-26 16:14:40 +02002246 sta_id = iwl4965_add_station_flags(priv, hdr->addr1,
2247 0, CMD_ASYNC, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07002248 }
2249 if ((sta_id != IWL_INVALID_STATION)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002250 lq_sta->lq.sta_id = sta_id;
2251 lq_sta->lq.rs_table[0].rate_n_flags = 0;
2252 lq_sta->ibss_sta_added = 1;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002253 rs_initialize_lq(priv, conf, sta);
Zhu Yib481de92007-09-25 17:54:57 -07002254 }
Tomas Winklerc33104f2008-01-14 17:46:21 -08002255 if (!lq_sta->ibss_sta_added)
Zhu Yib481de92007-09-25 17:54:57 -07002256 goto done;
2257 }
2258
Tomas Winklerf4d60822008-03-05 11:31:00 -08002259done:
Mattias Nissler1abbe492007-12-20 13:50:07 +01002260 if ((i < 0) || (i > IWL_RATE_COUNT)) {
Johannes Berg8318d782008-01-24 19:38:38 +01002261 sel->rate = rate_lowest(local, sband, sta);
Tomas Winklerf4d60822008-03-05 11:31:00 -08002262 goto out;
Mattias Nissler1abbe492007-12-20 13:50:07 +01002263 }
Zhu Yib481de92007-09-25 17:54:57 -07002264
Mattias Nissler1abbe492007-12-20 13:50:07 +01002265 sel->rate = &priv->ieee_rates[i];
Tomas Winklerf4d60822008-03-05 11:31:00 -08002266out:
2267 rcu_read_unlock();
Zhu Yib481de92007-09-25 17:54:57 -07002268}
2269
2270static void *rs_alloc_sta(void *priv, gfp_t gfp)
2271{
Tomas Winklerc33104f2008-01-14 17:46:21 -08002272 struct iwl4965_lq_sta *lq_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002273 int i, j;
2274
2275 IWL_DEBUG_RATE("create station rate scale window\n");
2276
Tomas Winklerc33104f2008-01-14 17:46:21 -08002277 lq_sta = kzalloc(sizeof(struct iwl4965_lq_sta), gfp);
Zhu Yib481de92007-09-25 17:54:57 -07002278
Tomas Winklerc33104f2008-01-14 17:46:21 -08002279 if (lq_sta == NULL)
Zhu Yib481de92007-09-25 17:54:57 -07002280 return NULL;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002281 lq_sta->lq.sta_id = 0xff;
Zhu Yib481de92007-09-25 17:54:57 -07002282
Zhu Yi63fddb92007-09-27 11:27:36 +08002283
Zhu Yib481de92007-09-25 17:54:57 -07002284 for (j = 0; j < LQ_SIZE; j++)
2285 for (i = 0; i < IWL_RATE_COUNT; i++)
Tomas Winklerc33104f2008-01-14 17:46:21 -08002286 rs_rate_scale_clear_window(&(lq_sta->lq_info[j].win[i]));
Zhu Yib481de92007-09-25 17:54:57 -07002287
Tomas Winklerc33104f2008-01-14 17:46:21 -08002288 return lq_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002289}
2290
2291static void rs_rate_init(void *priv_rate, void *priv_sta,
2292 struct ieee80211_local *local,
2293 struct sta_info *sta)
2294{
2295 int i, j;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002296 struct ieee80211_conf *conf = &local->hw.conf;
Johannes Berg8318d782008-01-24 19:38:38 +01002297 struct ieee80211_supported_band *sband;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002298 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002299 struct iwl4965_lq_sta *lq_sta = priv_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002300
Johannes Berg8318d782008-01-24 19:38:38 +01002301 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2302
Tomas Winklerc33104f2008-01-14 17:46:21 -08002303 lq_sta->flush_timer = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01002304 lq_sta->supp_rates = sta->supp_rates[sband->band];
2305 sta->txrate_idx = 3;
Zhu Yib481de92007-09-25 17:54:57 -07002306 for (j = 0; j < LQ_SIZE; j++)
2307 for (i = 0; i < IWL_RATE_COUNT; i++)
Tomas Winklerc33104f2008-01-14 17:46:21 -08002308 rs_rate_scale_clear_window(&(lq_sta->lq_info[j].win[i]));
Zhu Yib481de92007-09-25 17:54:57 -07002309
2310 IWL_DEBUG_RATE("rate scale global init\n");
2311 /* TODO: what is a good starting rate for STA? About middle? Maybe not
2312 * the lowest or the highest rate.. Could consider using RSSI from
2313 * previous packets? Need to have IEEE 802.1X auth succeed immediately
2314 * after assoc.. */
2315
Tomas Winklerc33104f2008-01-14 17:46:21 -08002316 lq_sta->ibss_sta_added = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002317 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002318 u8 sta_id = iwl4965_hw_find_station(priv, sta->addr);
Joe Perches0795af52007-10-03 17:59:30 -07002319 DECLARE_MAC_BUF(mac);
2320
Zhu Yib481de92007-09-25 17:54:57 -07002321 /* for IBSS the call are from tasklet */
Joe Perches0795af52007-10-03 17:59:30 -07002322 IWL_DEBUG_HT("LQ: ADD station %s\n",
2323 print_mac(mac, sta->addr));
Zhu Yib481de92007-09-25 17:54:57 -07002324
2325 if (sta_id == IWL_INVALID_STATION) {
Joe Perches0795af52007-10-03 17:59:30 -07002326 IWL_DEBUG_RATE("LQ: ADD station %s\n",
2327 print_mac(mac, sta->addr));
Ron Rindjunsky67d62032007-11-26 16:14:40 +02002328 sta_id = iwl4965_add_station_flags(priv, sta->addr,
2329 0, CMD_ASYNC, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07002330 }
2331 if ((sta_id != IWL_INVALID_STATION)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002332 lq_sta->lq.sta_id = sta_id;
2333 lq_sta->lq.rs_table[0].rate_n_flags = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002334 }
2335 /* FIXME: this is w/a remove it later */
2336 priv->assoc_station_added = 1;
2337 }
2338
Ben Cahill77626352007-11-29 11:09:44 +08002339 /* Find highest tx rate supported by hardware and destination station */
Johannes Berg8318d782008-01-24 19:38:38 +01002340 for (i = 0; i < sband->n_bitrates; i++)
2341 if (sta->supp_rates[sband->band] & BIT(i))
2342 sta->txrate_idx = i;
2343
2344 sta->last_txrate_idx = sta->txrate_idx;
2345 /* WTF is with this bogus comment? A doesn't have cck rates */
Ben Cahill77626352007-11-29 11:09:44 +08002346 /* For MODE_IEEE80211A, cck rates are at end of rate table */
Johannes Berg8318d782008-01-24 19:38:38 +01002347 if (local->hw.conf.channel->band == IEEE80211_BAND_5GHZ)
2348 sta->last_txrate_idx += IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -07002349
Tomas Winklerc33104f2008-01-14 17:46:21 -08002350 lq_sta->is_dup = 0;
2351 lq_sta->valid_antenna = priv->valid_antenna;
2352 lq_sta->antenna = priv->antenna;
2353 lq_sta->is_green = rs_use_green(priv, conf);
2354 lq_sta->active_rate = priv->active_rate;
2355 lq_sta->active_rate &= ~(0x1000);
2356 lq_sta->active_rate_basic = priv->active_rate_basic;
Johannes Berg8318d782008-01-24 19:38:38 +01002357 lq_sta->band = priv->band;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002358#ifdef CONFIG_IWL4965_HT
Ben Cahill77626352007-11-29 11:09:44 +08002359 /*
2360 * active_siso_rate mask includes 9 MBits (bit 5), and CCK (bits 0-3),
2361 * supp_rates[] does not; shift to convert format, force 9 MBits off.
2362 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002363 lq_sta->active_siso_rate = (priv->current_ht_config.supp_mcs_set[0] << 1);
2364 lq_sta->active_siso_rate |=
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002365 (priv->current_ht_config.supp_mcs_set[0] & 0x1);
Tomas Winklerc33104f2008-01-14 17:46:21 -08002366 lq_sta->active_siso_rate &= ~((u16)0x2);
2367 lq_sta->active_siso_rate =
2368 lq_sta->active_siso_rate << IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -07002369
Ben Cahill77626352007-11-29 11:09:44 +08002370 /* Same here */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002371 lq_sta->active_mimo_rate = (priv->current_ht_config.supp_mcs_set[1] << 1);
2372 lq_sta->active_mimo_rate |=
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002373 (priv->current_ht_config.supp_mcs_set[1] & 0x1);
Tomas Winklerc33104f2008-01-14 17:46:21 -08002374 lq_sta->active_mimo_rate &= ~((u16)0x2);
2375 lq_sta->active_mimo_rate =
2376 lq_sta->active_mimo_rate << IWL_FIRST_OFDM_RATE;
2377 IWL_DEBUG_HT("SISO RATE 0x%X MIMO RATE 0x%X\n",
2378 lq_sta->active_siso_rate,
2379 lq_sta->active_mimo_rate);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002380 /* as default allow aggregation for all tids */
2381 lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002382#endif /*CONFIG_IWL4965_HT*/
Zhu Yi98d7e092007-09-27 11:27:42 +08002383#ifdef CONFIG_MAC80211_DEBUGFS
Tomas Winklerc33104f2008-01-14 17:46:21 -08002384 lq_sta->drv = priv;
Zhu Yi98d7e092007-09-27 11:27:42 +08002385#endif
Zhu Yib481de92007-09-25 17:54:57 -07002386
2387 if (priv->assoc_station_added)
2388 priv->lq_mngr.lq_ready = 1;
2389
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002390 rs_initialize_lq(priv, conf, sta);
Zhu Yib481de92007-09-25 17:54:57 -07002391}
2392
Tomas Winklerc33104f2008-01-14 17:46:21 -08002393static void rs_fill_link_cmd(struct iwl4965_lq_sta *lq_sta,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002394 struct iwl4965_rate *tx_mcs,
2395 struct iwl4965_link_quality_cmd *lq_cmd)
Zhu Yib481de92007-09-25 17:54:57 -07002396{
2397 int index = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002398 int rate_idx;
Zhu Yi1b696de2007-09-27 11:27:41 +08002399 int repeat_rate = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002400 u8 ant_toggle_count = 0;
2401 u8 use_ht_possible = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002402 struct iwl4965_rate new_rate;
2403 struct iwl4965_scale_tbl_info tbl_type = { 0 };
Zhu Yib481de92007-09-25 17:54:57 -07002404
Ben Cahill77626352007-11-29 11:09:44 +08002405 /* Override starting rate (index 0) if needed for debug purposes */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002406 rs_dbgfs_set_mcs(lq_sta, tx_mcs, index);
Zhu Yi98d7e092007-09-27 11:27:42 +08002407
Ben Cahill77626352007-11-29 11:09:44 +08002408 /* Interpret rate_n_flags */
Johannes Berg8318d782008-01-24 19:38:38 +01002409 rs_get_tbl_info_from_mcs(tx_mcs, lq_sta->band,
Zhu Yib481de92007-09-25 17:54:57 -07002410 &tbl_type, &rate_idx);
2411
Ben Cahill77626352007-11-29 11:09:44 +08002412 /* How many times should we repeat the initial rate? */
Zhu Yib481de92007-09-25 17:54:57 -07002413 if (is_legacy(tbl_type.lq_type)) {
2414 ant_toggle_count = 1;
Zhu Yi1b696de2007-09-27 11:27:41 +08002415 repeat_rate = IWL_NUMBER_TRY;
Zhu Yib481de92007-09-25 17:54:57 -07002416 } else
Zhu Yi1b696de2007-09-27 11:27:41 +08002417 repeat_rate = IWL_HT_NUMBER_TRY;
Zhu Yib481de92007-09-25 17:54:57 -07002418
2419 lq_cmd->general_params.mimo_delimiter =
2420 is_mimo(tbl_type.lq_type) ? 1 : 0;
Ben Cahill77626352007-11-29 11:09:44 +08002421
2422 /* Fill 1st table entry (index 0) */
Zhu Yib481de92007-09-25 17:54:57 -07002423 lq_cmd->rs_table[index].rate_n_flags =
2424 cpu_to_le32(tx_mcs->rate_n_flags);
2425 new_rate.rate_n_flags = tx_mcs->rate_n_flags;
2426
2427 if (is_mimo(tbl_type.lq_type) || (tbl_type.antenna_type == ANT_MAIN))
Ben Cahill77626352007-11-29 11:09:44 +08002428 lq_cmd->general_params.single_stream_ant_msk
2429 = LINK_QUAL_ANT_A_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002430 else
Ben Cahill77626352007-11-29 11:09:44 +08002431 lq_cmd->general_params.single_stream_ant_msk
2432 = LINK_QUAL_ANT_B_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002433
2434 index++;
Zhu Yi1b696de2007-09-27 11:27:41 +08002435 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002436
Ben Cahill77626352007-11-29 11:09:44 +08002437 /* Fill rest of rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002438 while (index < LINK_QUAL_MAX_RETRY_NUM) {
Ben Cahill77626352007-11-29 11:09:44 +08002439 /* Repeat initial/next rate.
2440 * For legacy IWL_NUMBER_TRY == 1, this loop will not execute.
2441 * For HT IWL_HT_NUMBER_TRY == 3, this executes twice. */
Zhu Yi1b696de2007-09-27 11:27:41 +08002442 while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
Zhu Yib481de92007-09-25 17:54:57 -07002443 if (is_legacy(tbl_type.lq_type)) {
2444 if (ant_toggle_count <
2445 NUM_TRY_BEFORE_ANTENNA_TOGGLE)
2446 ant_toggle_count++;
2447 else {
2448 rs_toggle_antenna(&new_rate, &tbl_type);
2449 ant_toggle_count = 1;
2450 }
2451 }
Zhu Yi98d7e092007-09-27 11:27:42 +08002452
Ben Cahill77626352007-11-29 11:09:44 +08002453 /* Override next rate if needed for debug purposes */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002454 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Ben Cahill77626352007-11-29 11:09:44 +08002455
2456 /* Fill next table entry */
Zhu Yib481de92007-09-25 17:54:57 -07002457 lq_cmd->rs_table[index].rate_n_flags =
2458 cpu_to_le32(new_rate.rate_n_flags);
Zhu Yi1b696de2007-09-27 11:27:41 +08002459 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002460 index++;
2461 }
2462
Johannes Berg8318d782008-01-24 19:38:38 +01002463 rs_get_tbl_info_from_mcs(&new_rate, lq_sta->band, &tbl_type,
Zhu Yib481de92007-09-25 17:54:57 -07002464 &rate_idx);
2465
Ben Cahill77626352007-11-29 11:09:44 +08002466 /* Indicate to uCode which entries might be MIMO.
2467 * If initial rate was MIMO, this will finally end up
2468 * as (IWL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
Zhu Yib481de92007-09-25 17:54:57 -07002469 if (is_mimo(tbl_type.lq_type))
2470 lq_cmd->general_params.mimo_delimiter = index;
2471
Ben Cahill77626352007-11-29 11:09:44 +08002472 /* Get next rate */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002473 rs_get_lower_rate(lq_sta, &tbl_type, rate_idx,
Zhu Yi02dede02007-09-27 11:27:40 +08002474 use_ht_possible, &new_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002475
Ben Cahill77626352007-11-29 11:09:44 +08002476 /* How many times should we repeat the next rate? */
Zhu Yib481de92007-09-25 17:54:57 -07002477 if (is_legacy(tbl_type.lq_type)) {
2478 if (ant_toggle_count < NUM_TRY_BEFORE_ANTENNA_TOGGLE)
2479 ant_toggle_count++;
2480 else {
2481 rs_toggle_antenna(&new_rate, &tbl_type);
2482 ant_toggle_count = 1;
2483 }
Zhu Yi1b696de2007-09-27 11:27:41 +08002484 repeat_rate = IWL_NUMBER_TRY;
Zhu Yib481de92007-09-25 17:54:57 -07002485 } else
Zhu Yi1b696de2007-09-27 11:27:41 +08002486 repeat_rate = IWL_HT_NUMBER_TRY;
Zhu Yib481de92007-09-25 17:54:57 -07002487
Ben Cahill77626352007-11-29 11:09:44 +08002488 /* Don't allow HT rates after next pass.
2489 * rs_get_lower_rate() will change type to LQ_A or LQ_G. */
Zhu Yib481de92007-09-25 17:54:57 -07002490 use_ht_possible = 0;
2491
Ben Cahill77626352007-11-29 11:09:44 +08002492 /* Override next rate if needed for debug purposes */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002493 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Ben Cahill77626352007-11-29 11:09:44 +08002494
2495 /* Fill next table entry */
Zhu Yib481de92007-09-25 17:54:57 -07002496 lq_cmd->rs_table[index].rate_n_flags =
2497 cpu_to_le32(new_rate.rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -07002498
2499 index++;
Zhu Yi1b696de2007-09-27 11:27:41 +08002500 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002501 }
2502
Zhu Yib481de92007-09-25 17:54:57 -07002503 lq_cmd->general_params.dual_stream_ant_msk = 3;
2504 lq_cmd->agg_params.agg_dis_start_th = 3;
2505 lq_cmd->agg_params.agg_time_limit = cpu_to_le16(4000);
Zhu Yib481de92007-09-25 17:54:57 -07002506}
2507
2508static void *rs_alloc(struct ieee80211_local *local)
2509{
2510 return local->hw.priv;
2511}
2512/* rate scale requires free function to be implemented */
2513static void rs_free(void *priv_rate)
2514{
2515 return;
2516}
2517
2518static void rs_clear(void *priv_rate)
2519{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002520 struct iwl_priv *priv = (struct iwl_priv *) priv_rate;
Zhu Yib481de92007-09-25 17:54:57 -07002521
2522 IWL_DEBUG_RATE("enter\n");
2523
2524 priv->lq_mngr.lq_ready = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002525
2526 IWL_DEBUG_RATE("leave\n");
2527}
2528
2529static void rs_free_sta(void *priv, void *priv_sta)
2530{
Tomas Winklerc33104f2008-01-14 17:46:21 -08002531 struct iwl4965_lq_sta *lq_sta = priv_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002532
2533 IWL_DEBUG_RATE("enter\n");
Tomas Winklerc33104f2008-01-14 17:46:21 -08002534 kfree(lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002535 IWL_DEBUG_RATE("leave\n");
2536}
2537
2538
Zhu Yi93dc6462007-09-27 11:27:37 +08002539#ifdef CONFIG_MAC80211_DEBUGFS
Zhu Yi5ae212c2007-09-27 11:27:38 +08002540static int open_file_generic(struct inode *inode, struct file *file)
2541{
2542 file->private_data = inode->i_private;
2543 return 0;
2544}
Tomas Winklerc33104f2008-01-14 17:46:21 -08002545static void rs_dbgfs_set_mcs(struct iwl4965_lq_sta *lq_sta,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002546 struct iwl4965_rate *mcs, int index)
Zhu Yi98d7e092007-09-27 11:27:42 +08002547{
Ron Rindjunsky4457e1a2007-10-15 14:40:56 +02002548 u32 base_rate;
2549
Johannes Berg8318d782008-01-24 19:38:38 +01002550 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Ron Rindjunsky4457e1a2007-10-15 14:40:56 +02002551 base_rate = 0x800D;
2552 else
2553 base_rate = 0x820A;
2554
Tomas Winklerc33104f2008-01-14 17:46:21 -08002555 if (lq_sta->dbg_fixed.rate_n_flags) {
Zhu Yi98d7e092007-09-27 11:27:42 +08002556 if (index < 12)
Tomas Winklerc33104f2008-01-14 17:46:21 -08002557 mcs->rate_n_flags = lq_sta->dbg_fixed.rate_n_flags;
Zhu Yi98d7e092007-09-27 11:27:42 +08002558 else
Ron Rindjunsky4457e1a2007-10-15 14:40:56 +02002559 mcs->rate_n_flags = base_rate;
Zhu Yi98d7e092007-09-27 11:27:42 +08002560 IWL_DEBUG_RATE("Fixed rate ON\n");
2561 return;
2562 }
Zhu Yi5ae212c2007-09-27 11:27:38 +08002563
Zhu Yi98d7e092007-09-27 11:27:42 +08002564 IWL_DEBUG_RATE("Fixed rate OFF\n");
2565}
2566
2567static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
2568 const char __user *user_buf, size_t count, loff_t *ppos)
2569{
Tomas Winklerc33104f2008-01-14 17:46:21 -08002570 struct iwl4965_lq_sta *lq_sta = file->private_data;
Zhu Yi98d7e092007-09-27 11:27:42 +08002571 char buf[64];
2572 int buf_size;
2573 u32 parsed_rate;
2574
2575 memset(buf, 0, sizeof(buf));
2576 buf_size = min(count, sizeof(buf) - 1);
2577 if (copy_from_user(buf, user_buf, buf_size))
2578 return -EFAULT;
2579
2580 if (sscanf(buf, "%x", &parsed_rate) == 1)
Tomas Winklerc33104f2008-01-14 17:46:21 -08002581 lq_sta->dbg_fixed.rate_n_flags = parsed_rate;
Zhu Yi98d7e092007-09-27 11:27:42 +08002582 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002583 lq_sta->dbg_fixed.rate_n_flags = 0;
Zhu Yi98d7e092007-09-27 11:27:42 +08002584
Tomas Winklerc33104f2008-01-14 17:46:21 -08002585 lq_sta->active_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
2586 lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2587 lq_sta->active_mimo_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
Zhu Yi98d7e092007-09-27 11:27:42 +08002588
2589 IWL_DEBUG_RATE("sta_id %d rate 0x%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002590 lq_sta->lq.sta_id, lq_sta->dbg_fixed.rate_n_flags);
Zhu Yi98d7e092007-09-27 11:27:42 +08002591
Tomas Winklerc33104f2008-01-14 17:46:21 -08002592 if (lq_sta->dbg_fixed.rate_n_flags) {
2593 rs_fill_link_cmd(lq_sta, &lq_sta->dbg_fixed, &lq_sta->lq);
2594 rs_send_lq_cmd(lq_sta->drv, &lq_sta->lq, CMD_ASYNC);
Zhu Yi98d7e092007-09-27 11:27:42 +08002595 }
2596
2597 return count;
2598}
Zhu Yi0209dc12007-09-27 11:27:43 +08002599
Zhu Yi5ae212c2007-09-27 11:27:38 +08002600static ssize_t rs_sta_dbgfs_scale_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 = 0;
2606
Tomas Winklerc33104f2008-01-14 17:46:21 -08002607 struct iwl4965_lq_sta *lq_sta = file->private_data;
Zhu Yi5ae212c2007-09-27 11:27:38 +08002608
Tomas Winklerc33104f2008-01-14 17:46:21 -08002609 desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
Zhu Yi98d7e092007-09-27 11:27:42 +08002610 desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002611 lq_sta->total_failed, lq_sta->total_success,
2612 lq_sta->active_rate);
Zhu Yi98d7e092007-09-27 11:27:42 +08002613 desc += sprintf(buff+desc, "fixed rate 0x%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002614 lq_sta->dbg_fixed.rate_n_flags);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002615 desc += sprintf(buff+desc, "general:"
2616 "flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002617 lq_sta->lq.general_params.flags,
2618 lq_sta->lq.general_params.mimo_delimiter,
2619 lq_sta->lq.general_params.single_stream_ant_msk,
2620 lq_sta->lq.general_params.dual_stream_ant_msk);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002621
2622 desc += sprintf(buff+desc, "agg:"
2623 "time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002624 le16_to_cpu(lq_sta->lq.agg_params.agg_time_limit),
2625 lq_sta->lq.agg_params.agg_dis_start_th,
2626 lq_sta->lq.agg_params.agg_frame_cnt_limit);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002627
2628 desc += sprintf(buff+desc,
2629 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002630 lq_sta->lq.general_params.start_rate_index[0],
2631 lq_sta->lq.general_params.start_rate_index[1],
2632 lq_sta->lq.general_params.start_rate_index[2],
2633 lq_sta->lq.general_params.start_rate_index[3]);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002634
2635
2636 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
2637 desc += sprintf(buff+desc, " rate[%d] 0x%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002638 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags));
Zhu Yi5ae212c2007-09-27 11:27:38 +08002639
2640 return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2641}
2642
2643static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
Zhu Yi98d7e092007-09-27 11:27:42 +08002644 .write = rs_sta_dbgfs_scale_table_write,
Zhu Yi5ae212c2007-09-27 11:27:38 +08002645 .read = rs_sta_dbgfs_scale_table_read,
2646 .open = open_file_generic,
2647};
Zhu Yi0209dc12007-09-27 11:27:43 +08002648static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
2649 char __user *user_buf, size_t count, loff_t *ppos)
2650{
2651 char buff[1024];
2652 int desc = 0;
2653 int i, j;
2654
Tomas Winklerc33104f2008-01-14 17:46:21 -08002655 struct iwl4965_lq_sta *lq_sta = file->private_data;
Zhu Yi0209dc12007-09-27 11:27:43 +08002656 for (i = 0; i < LQ_SIZE; i++) {
2657 desc += sprintf(buff+desc, "%s type=%d SGI=%d FAT=%d DUP=%d\n"
2658 "rate=0x%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002659 lq_sta->active_tbl == i?"*":"x",
2660 lq_sta->lq_info[i].lq_type,
2661 lq_sta->lq_info[i].is_SGI,
2662 lq_sta->lq_info[i].is_fat,
2663 lq_sta->lq_info[i].is_dup,
2664 lq_sta->lq_info[i].current_rate.rate_n_flags);
Zhu Yi0209dc12007-09-27 11:27:43 +08002665 for (j = 0; j < IWL_RATE_COUNT; j++) {
2666 desc += sprintf(buff+desc,
Tomas Winklerc33104f2008-01-14 17:46:21 -08002667 "counter=%d success=%d %%=%d\n",
2668 lq_sta->lq_info[i].win[j].counter,
2669 lq_sta->lq_info[i].win[j].success_counter,
2670 lq_sta->lq_info[i].win[j].success_ratio);
Zhu Yi0209dc12007-09-27 11:27:43 +08002671 }
2672 }
2673 return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2674}
2675
2676static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
2677 .read = rs_sta_dbgfs_stats_table_read,
2678 .open = open_file_generic,
2679};
Zhu Yi5ae212c2007-09-27 11:27:38 +08002680
Zhu Yi93dc6462007-09-27 11:27:37 +08002681static void rs_add_debugfs(void *priv, void *priv_sta,
2682 struct dentry *dir)
2683{
Tomas Winklerc33104f2008-01-14 17:46:21 -08002684 struct iwl4965_lq_sta *lq_sta = priv_sta;
2685 lq_sta->rs_sta_dbgfs_scale_table_file =
Zhu Yi0209dc12007-09-27 11:27:43 +08002686 debugfs_create_file("rate_scale_table", 0600, dir,
Tomas Winklerc33104f2008-01-14 17:46:21 -08002687 lq_sta, &rs_sta_dbgfs_scale_table_ops);
2688 lq_sta->rs_sta_dbgfs_stats_table_file =
Zhu Yi0209dc12007-09-27 11:27:43 +08002689 debugfs_create_file("rate_stats_table", 0600, dir,
Tomas Winklerc33104f2008-01-14 17:46:21 -08002690 lq_sta, &rs_sta_dbgfs_stats_table_ops);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002691#ifdef CONFIG_IWL4965_HT
2692 lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file =
2693 debugfs_create_u8("tx_agg_tid_enable", 0600, dir,
2694 &lq_sta->tx_agg_tid_en);
2695#endif
2696
Zhu Yi93dc6462007-09-27 11:27:37 +08002697}
2698
2699static void rs_remove_debugfs(void *priv, void *priv_sta)
2700{
Tomas Winklerc33104f2008-01-14 17:46:21 -08002701 struct iwl4965_lq_sta *lq_sta = priv_sta;
2702 debugfs_remove(lq_sta->rs_sta_dbgfs_scale_table_file);
2703 debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002704#ifdef CONFIG_IWL4965_HT
2705 debugfs_remove(lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file);
2706#endif
Zhu Yi93dc6462007-09-27 11:27:37 +08002707}
2708#endif
2709
Zhu Yib481de92007-09-25 17:54:57 -07002710static struct rate_control_ops rs_ops = {
2711 .module = NULL,
2712 .name = RS_NAME,
2713 .tx_status = rs_tx_status,
2714 .get_rate = rs_get_rate,
2715 .rate_init = rs_rate_init,
2716 .clear = rs_clear,
2717 .alloc = rs_alloc,
2718 .free = rs_free,
2719 .alloc_sta = rs_alloc_sta,
2720 .free_sta = rs_free_sta,
Zhu Yi93dc6462007-09-27 11:27:37 +08002721#ifdef CONFIG_MAC80211_DEBUGFS
2722 .add_sta_debugfs = rs_add_debugfs,
2723 .remove_sta_debugfs = rs_remove_debugfs,
2724#endif
Zhu Yib481de92007-09-25 17:54:57 -07002725};
2726
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002727int iwl4965_fill_rs_info(struct ieee80211_hw *hw, char *buf, u8 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07002728{
2729 struct ieee80211_local *local = hw_to_local(hw);
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002730 struct iwl_priv *priv = hw->priv;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002731 struct iwl4965_lq_sta *lq_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002732 struct sta_info *sta;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002733 int cnt = 0, i;
Zhu Yib481de92007-09-25 17:54:57 -07002734 u32 samples = 0, success = 0, good = 0;
2735 unsigned long now = jiffies;
2736 u32 max_time = 0;
2737 u8 lq_type, antenna;
2738
Johannes Bergd0709a62008-02-25 16:27:46 +01002739 rcu_read_lock();
2740
Zhu Yib481de92007-09-25 17:54:57 -07002741 sta = sta_info_get(local, priv->stations[sta_id].sta.sta.addr);
2742 if (!sta || !sta->rate_ctrl_priv) {
Johannes Bergd0709a62008-02-25 16:27:46 +01002743 if (sta)
Zhu Yib481de92007-09-25 17:54:57 -07002744 IWL_DEBUG_RATE("leave - no private rate data!\n");
Johannes Bergd0709a62008-02-25 16:27:46 +01002745 else
Zhu Yib481de92007-09-25 17:54:57 -07002746 IWL_DEBUG_RATE("leave - no station!\n");
Johannes Bergd0709a62008-02-25 16:27:46 +01002747 rcu_read_unlock();
Zhu Yib481de92007-09-25 17:54:57 -07002748 return sprintf(buf, "station %d not found\n", sta_id);
2749 }
2750
Tomas Winklerc33104f2008-01-14 17:46:21 -08002751 lq_sta = (void *)sta->rate_ctrl_priv;
Zhu Yib481de92007-09-25 17:54:57 -07002752
Tomas Winklerc33104f2008-01-14 17:46:21 -08002753 lq_type = lq_sta->lq_info[lq_sta->active_tbl].lq_type;
2754 antenna = lq_sta->lq_info[lq_sta->active_tbl].antenna_type;
Zhu Yib481de92007-09-25 17:54:57 -07002755
2756 if (is_legacy(lq_type))
2757 i = IWL_RATE_54M_INDEX;
2758 else
2759 i = IWL_RATE_60M_INDEX;
2760 while (1) {
2761 u64 mask;
2762 int j;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002763 int active = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002764
Tomas Winklerc33104f2008-01-14 17:46:21 -08002765 cnt +=
2766 sprintf(&buf[cnt], " %2dMbs: ", iwl4965_rates[i].ieee / 2);
Zhu Yib481de92007-09-25 17:54:57 -07002767
2768 mask = (1ULL << (IWL_RATE_MAX_WINDOW - 1));
2769 for (j = 0; j < IWL_RATE_MAX_WINDOW; j++, mask >>= 1)
Tomas Winklerc33104f2008-01-14 17:46:21 -08002770 buf[cnt++] =
2771 (lq_sta->lq_info[active].win[i].data & mask)
Zhu Yib481de92007-09-25 17:54:57 -07002772 ? '1' : '0';
2773
Tomas Winklerc33104f2008-01-14 17:46:21 -08002774 samples += lq_sta->lq_info[active].win[i].counter;
2775 good += lq_sta->lq_info[active].win[i].success_counter;
2776 success += lq_sta->lq_info[active].win[i].success_counter *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002777 iwl4965_rates[i].ieee;
Zhu Yib481de92007-09-25 17:54:57 -07002778
Tomas Winklerc33104f2008-01-14 17:46:21 -08002779 if (lq_sta->lq_info[active].win[i].stamp) {
Zhu Yib481de92007-09-25 17:54:57 -07002780 int delta =
2781 jiffies_to_msecs(now -
Tomas Winklerc33104f2008-01-14 17:46:21 -08002782 lq_sta->lq_info[active].win[i].stamp);
Zhu Yib481de92007-09-25 17:54:57 -07002783
2784 if (delta > max_time)
2785 max_time = delta;
2786
Tomas Winklerc33104f2008-01-14 17:46:21 -08002787 cnt += sprintf(&buf[cnt], "%5dms\n", delta);
Zhu Yib481de92007-09-25 17:54:57 -07002788 } else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002789 buf[cnt++] = '\n';
Zhu Yib481de92007-09-25 17:54:57 -07002790
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002791 j = iwl4965_get_prev_ieee_rate(i);
Zhu Yib481de92007-09-25 17:54:57 -07002792 if (j == i)
2793 break;
2794 i = j;
2795 }
2796
2797 /* Display the average rate of all samples taken.
2798 *
Ben Cahill77626352007-11-29 11:09:44 +08002799 * NOTE: We multiply # of samples by 2 since the IEEE measurement
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002800 * added from iwl4965_rates is actually 2X the rate */
Zhu Yib481de92007-09-25 17:54:57 -07002801 if (samples)
Tomas Winklerc33104f2008-01-14 17:46:21 -08002802 cnt += sprintf(&buf[cnt],
Zhu Yib481de92007-09-25 17:54:57 -07002803 "\nAverage rate is %3d.%02dMbs over last %4dms\n"
2804 "%3d%% success (%d good packets over %d tries)\n",
2805 success / (2 * samples), (success * 5 / samples) % 10,
2806 max_time, good * 100 / samples, good, samples);
2807 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002808 cnt += sprintf(&buf[cnt], "\nAverage rate: 0Mbs\n");
2809
2810 cnt += sprintf(&buf[cnt], "\nrate scale type %d antenna %d "
Zhu Yib481de92007-09-25 17:54:57 -07002811 "active_search %d rate index %d\n", lq_type, antenna,
Johannes Berg8318d782008-01-24 19:38:38 +01002812 lq_sta->search_better_tbl, sta->last_txrate_idx);
Zhu Yib481de92007-09-25 17:54:57 -07002813
Johannes Bergd0709a62008-02-25 16:27:46 +01002814 rcu_read_unlock();
Tomas Winklerc33104f2008-01-14 17:46:21 -08002815 return cnt;
Zhu Yib481de92007-09-25 17:54:57 -07002816}
2817
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002818void iwl4965_rate_scale_init(struct ieee80211_hw *hw, s32 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07002819{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002820 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07002821
2822 priv->lq_mngr.lq_ready = 1;
2823}
2824
Reinette Chatre897e1cf2008-03-28 16:21:09 -07002825int iwl4965_rate_control_register(void)
Zhu Yib481de92007-09-25 17:54:57 -07002826{
Reinette Chatre897e1cf2008-03-28 16:21:09 -07002827 return ieee80211_rate_control_register(&rs_ops);
Zhu Yib481de92007-09-25 17:54:57 -07002828}
2829
Reinette Chatre897e1cf2008-03-28 16:21:09 -07002830void iwl4965_rate_control_unregister(void)
Zhu Yib481de92007-09-25 17:54:57 -07002831{
2832 ieee80211_rate_control_unregister(&rs_ops);
2833}
2834