iwlwifi: don't switch to SGI if not supported by AP
[deliverable/linux.git] / drivers / net / wireless / iwlwifi / iwl-4965-rs.c
CommitLineData
b481de9c
ZY
1/******************************************************************************
2 *
eb7ae89c 3 * Copyright(c) 2005 - 2008 Intel Corporation. All rights reserved.
b481de9c
ZY
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
17 *
18 * The full GNU General Public License is included in this distribution in the
19 * file called LICENSE.
20 *
21 * Contact Information:
22 * James P. Ketrenos <ipw2100-admin@linux.intel.com>
23 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24 *
25 *****************************************************************************/
26#include <linux/kernel.h>
27#include <linux/init.h>
28#include <linux/skbuff.h>
29#include <linux/wireless.h>
30#include <net/mac80211.h>
b481de9c
ZY
31
32#include <linux/netdevice.h>
33#include <linux/etherdevice.h>
34#include <linux/delay.h>
35
36#include <linux/workqueue.h>
37
2c8dccc7 38#include "../net/mac80211/rate.h"
b481de9c 39
3e0d4cb1 40#include "iwl-dev.h"
857485c0 41#include "iwl-core.h"
b481de9c
ZY
42#include "iwl-helpers.h"
43
44#define RS_NAME "iwl-4965-rs"
45
aade00ce 46#define NUM_TRY_BEFORE_ANT_TOGGLE 1
b481de9c
ZY
47#define IWL_NUMBER_TRY 1
48#define IWL_HT_NUMBER_TRY 3
49
77626355
BC
50#define IWL_RATE_MAX_WINDOW 62 /* # tx in history window */
51#define IWL_RATE_MIN_FAILURE_TH 6 /* min failures to calc tpt */
52#define IWL_RATE_MIN_SUCCESS_TH 8 /* min successes to calc tpt */
53
54/* max time to accum history 2 seconds */
55#define IWL_RATE_SCALE_FLUSH_INTVL (2*HZ)
b481de9c
ZY
56
57static u8 rs_ht_to_legacy[] = {
58 IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
59 IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
60 IWL_RATE_6M_INDEX,
61 IWL_RATE_6M_INDEX, IWL_RATE_9M_INDEX,
62 IWL_RATE_12M_INDEX, IWL_RATE_18M_INDEX,
63 IWL_RATE_24M_INDEX, IWL_RATE_36M_INDEX,
64 IWL_RATE_48M_INDEX, IWL_RATE_54M_INDEX
65};
66
aade00ce
GC
67static const u8 ant_toggle_lookup[] = {
68 /*ANT_NONE -> */ ANT_NONE,
69 /*ANT_A -> */ ANT_B,
70 /*ANT_B -> */ ANT_C,
71 /*ANT_AB -> */ ANT_BC,
72 /*ANT_C -> */ ANT_A,
73 /*ANT_AC -> */ ANT_AB,
74 /*ANT_BC -> */ ANT_AC,
75 /*ANT_ABC -> */ ANT_ABC,
76};
77
77626355
BC
78/**
79 * struct iwl4965_rate_scale_data -- tx success history for one rate
80 */
bb8c093b 81struct iwl4965_rate_scale_data {
77626355
BC
82 u64 data; /* bitmap of successful frames */
83 s32 success_counter; /* number of frames successful */
84 s32 success_ratio; /* per-cent * 128 */
85 s32 counter; /* number of frames attempted */
86 s32 average_tpt; /* success ratio * expected throughput */
b481de9c
ZY
87 unsigned long stamp;
88};
89
77626355
BC
90/**
91 * struct iwl4965_scale_tbl_info -- tx params and success history for all rates
92 *
0c11b4de 93 * There are two of these in struct iwl4965_lq_sta,
77626355
BC
94 * one for "active", and one for "search".
95 */
bb8c093b 96struct iwl4965_scale_tbl_info {
fde0db31
GC
97 enum iwl_table_type lq_type;
98 u8 ant_type;
77626355
BC
99 u8 is_SGI; /* 1 = short guard interval */
100 u8 is_fat; /* 1 = 40 MHz channel width */
101 u8 is_dup; /* 1 = duplicated data streams */
102 u8 action; /* change modulation; IWL_[LEGACY/SISO/MIMO]_SWITCH_* */
103 s32 *expected_tpt; /* throughput metrics; expected_tpt_G, etc. */
39e88504 104 u32 current_rate; /* rate_n_flags, uCode API format */
77626355 105 struct iwl4965_rate_scale_data win[IWL_RATE_COUNT]; /* rate histories */
b481de9c
ZY
106};
107
0c11b4de
RR
108#ifdef CONFIG_IWL4965_HT
109
110struct iwl4965_traffic_load {
111 unsigned long time_stamp; /* age of the oldest statistics */
112 u32 packet_count[TID_QUEUE_MAX_SIZE]; /* packet count in this time
113 * slice */
114 u32 total; /* total num of packets during the
115 * last TID_MAX_TIME_DIFF */
116 u8 queue_count; /* number of queues that has
117 * been used since the last cleanup */
118 u8 head; /* start of the circular buffer */
119};
120
121#endif /* CONFIG_IWL4965_HT */
122
77626355 123/**
0c11b4de 124 * struct iwl4965_lq_sta -- driver's rate scaling private structure
77626355
BC
125 *
126 * Pointer to this gets passed back and forth between driver and mac80211.
127 */
c33104f0 128struct iwl4965_lq_sta {
77626355
BC
129 u8 active_tbl; /* index of active table, range 0-1 */
130 u8 enable_counter; /* indicates HT mode */
131 u8 stay_in_tbl; /* 1: disallow, 0: allow search for new mode */
132 u8 search_better_tbl; /* 1: currently trying alternate mode */
b481de9c 133 s32 last_tpt;
77626355
BC
134
135 /* The following determine when to search for a new mode */
b481de9c 136 u32 table_count_limit;
77626355
BC
137 u32 max_failure_limit; /* # failed frames before new search */
138 u32 max_success_limit; /* # successful frames before new search */
b481de9c 139 u32 table_count;
77626355
BC
140 u32 total_failed; /* total failed frames, any/all rates */
141 u32 total_success; /* total successful frames, any/all rates */
142 u32 flush_timer; /* time staying in mode before new search */
143
144 u8 action_counter; /* # mode-switch actions tried */
b481de9c
ZY
145 u8 is_green;
146 u8 is_dup;
8318d78a 147 enum ieee80211_band band;
b481de9c 148 u8 ibss_sta_added;
77626355
BC
149
150 /* The following are bitmaps of rates; IWL_RATE_6M_MASK, etc. */
02dede04 151 u32 supp_rates;
39e88504 152 u16 active_legacy_rate;
b481de9c 153 u16 active_siso_rate;
fde0db31
GC
154 u16 active_mimo2_rate;
155 u16 active_mimo3_rate;
b481de9c 156 u16 active_rate_basic;
77626355 157
66c73db7 158 struct iwl_link_quality_cmd lq;
77626355 159 struct iwl4965_scale_tbl_info lq_info[LQ_SIZE]; /* "active", "search" */
0c11b4de
RR
160#ifdef CONFIG_IWL4965_HT
161 struct iwl4965_traffic_load load[TID_MAX_LOAD_COUNT];
162 u8 tx_agg_tid_en;
163#endif
5ae212c9 164#ifdef CONFIG_MAC80211_DEBUGFS
98d7e09a 165 struct dentry *rs_sta_dbgfs_scale_table_file;
0209dc11 166 struct dentry *rs_sta_dbgfs_stats_table_file;
0c11b4de
RR
167#ifdef CONFIG_IWL4965_HT
168 struct dentry *rs_sta_dbgfs_tx_agg_tid_en_file;
169#endif
39e88504 170 u32 dbg_fixed_rate;
5ae212c9 171#endif
6243065d 172 struct iwl_priv *drv;
b481de9c
ZY
173};
174
c79dd5b5 175static void rs_rate_scale_perform(struct iwl_priv *priv,
b481de9c
ZY
176 struct net_device *dev,
177 struct ieee80211_hdr *hdr,
178 struct sta_info *sta);
fde0db31
GC
179static void rs_fill_link_cmd(const struct iwl_priv *priv,
180 struct iwl4965_lq_sta *lq_sta,
07bc28ed 181 u32 rate_n_flags);
b481de9c
ZY
182
183
98d7e09a 184#ifdef CONFIG_MAC80211_DEBUGFS
c33104f0 185static void rs_dbgfs_set_mcs(struct iwl4965_lq_sta *lq_sta,
39e88504 186 u32 *rate_n_flags, int index);
98d7e09a 187#else
c33104f0 188static void rs_dbgfs_set_mcs(struct iwl4965_lq_sta *lq_sta,
39e88504 189 u32 *rate_n_flags, int index)
98d7e09a
ZY
190{}
191#endif
77626355
BC
192
193/*
194 * Expected throughput metrics for following rates:
195 * 1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
196 * "G" is the only table that supports CCK (the first 4 rates).
197 */
fde0db31 198/*FIXME:RS:need to spearate tables for MIMO2/MIMO3*/
b481de9c
ZY
199static s32 expected_tpt_A[IWL_RATE_COUNT] = {
200 0, 0, 0, 0, 40, 57, 72, 98, 121, 154, 177, 186, 186
201};
202
203static s32 expected_tpt_G[IWL_RATE_COUNT] = {
204 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 186
205};
206
207static s32 expected_tpt_siso20MHz[IWL_RATE_COUNT] = {
208 0, 0, 0, 0, 42, 42, 76, 102, 124, 159, 183, 193, 202
209};
210
211static s32 expected_tpt_siso20MHzSGI[IWL_RATE_COUNT] = {
212 0, 0, 0, 0, 46, 46, 82, 110, 132, 168, 192, 202, 211
213};
214
215static s32 expected_tpt_mimo20MHz[IWL_RATE_COUNT] = {
216 0, 0, 0, 0, 74, 74, 123, 155, 179, 214, 236, 244, 251
217};
218
219static s32 expected_tpt_mimo20MHzSGI[IWL_RATE_COUNT] = {
220 0, 0, 0, 0, 81, 81, 131, 164, 188, 222, 243, 251, 257
221};
222
223static s32 expected_tpt_siso40MHz[IWL_RATE_COUNT] = {
224 0, 0, 0, 0, 77, 77, 127, 160, 184, 220, 242, 250, 257
225};
226
227static s32 expected_tpt_siso40MHzSGI[IWL_RATE_COUNT] = {
228 0, 0, 0, 0, 83, 83, 135, 169, 193, 229, 250, 257, 264
229};
230
231static s32 expected_tpt_mimo40MHz[IWL_RATE_COUNT] = {
232 0, 0, 0, 0, 123, 123, 182, 214, 235, 264, 279, 285, 289
233};
234
235static s32 expected_tpt_mimo40MHzSGI[IWL_RATE_COUNT] = {
236 0, 0, 0, 0, 131, 131, 191, 222, 242, 270, 284, 289, 293
237};
238
39e88504 239static inline u8 rs_extract_rate(u32 rate_n_flags)
b481de9c
ZY
240{
241 return (u8)(rate_n_flags & 0xFF);
242}
243
bb8c093b 244static void rs_rate_scale_clear_window(struct iwl4965_rate_scale_data *window)
b481de9c
ZY
245{
246 window->data = 0;
247 window->success_counter = 0;
248 window->success_ratio = IWL_INVALID_VALUE;
249 window->counter = 0;
250 window->average_tpt = IWL_INVALID_VALUE;
251 window->stamp = 0;
b481de9c
ZY
252}
253
aade00ce
GC
254static inline u8 rs_is_valid_ant(u8 valid_antenna, u8 ant_type)
255{
256 return ((ant_type & valid_antenna) == ant_type);
257}
258
0c11b4de
RR
259#ifdef CONFIG_IWL4965_HT
260/*
261 * removes the old data from the statistics. All data that is older than
262 * TID_MAX_TIME_DIFF, will be deleted.
263 */
264static void rs_tl_rm_old_stats(struct iwl4965_traffic_load *tl, u32 curr_time)
265{
266 /* The oldest age we want to keep */
267 u32 oldest_time = curr_time - TID_MAX_TIME_DIFF;
268
269 while (tl->queue_count &&
270 (tl->time_stamp < oldest_time)) {
271 tl->total -= tl->packet_count[tl->head];
272 tl->packet_count[tl->head] = 0;
273 tl->time_stamp += TID_QUEUE_CELL_SPACING;
274 tl->queue_count--;
275 tl->head++;
276 if (tl->head >= TID_QUEUE_MAX_SIZE)
277 tl->head = 0;
278 }
279}
280
281/*
282 * increment traffic load value for tid and also remove
283 * any old values if passed the certain time period
284 */
54dbb525
TW
285static void rs_tl_add_packet(struct iwl4965_lq_sta *lq_data,
286 struct ieee80211_hdr *hdr)
0c11b4de
RR
287{
288 u32 curr_time = jiffies_to_msecs(jiffies);
289 u32 time_diff;
290 s32 index;
291 struct iwl4965_traffic_load *tl = NULL;
54dbb525
TW
292 u16 fc = le16_to_cpu(hdr->frame_control);
293 u8 tid;
0c11b4de 294
54dbb525
TW
295 if (ieee80211_is_qos_data(fc)) {
296 u8 *qc = ieee80211_get_qos_ctrl(hdr, ieee80211_get_hdrlen(fc));
297 tid = qc[0] & 0xf;
298 } else
0c11b4de
RR
299 return;
300
301 tl = &lq_data->load[tid];
302
303 curr_time -= curr_time % TID_ROUND_VALUE;
304
305 /* Happens only for the first packet. Initialize the data */
306 if (!(tl->queue_count)) {
307 tl->total = 1;
308 tl->time_stamp = curr_time;
309 tl->queue_count = 1;
310 tl->head = 0;
311 tl->packet_count[0] = 1;
312 return;
313 }
314
315 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
316 index = time_diff / TID_QUEUE_CELL_SPACING;
317
318 /* The history is too long: remove data that is older than */
319 /* TID_MAX_TIME_DIFF */
320 if (index >= TID_QUEUE_MAX_SIZE)
321 rs_tl_rm_old_stats(tl, curr_time);
322
323 index = (tl->head + index) % TID_QUEUE_MAX_SIZE;
324 tl->packet_count[index] = tl->packet_count[index] + 1;
325 tl->total = tl->total + 1;
326
327 if ((index + 1) > tl->queue_count)
328 tl->queue_count = index + 1;
329}
330
331/*
332 get the traffic load value for tid
333*/
334static u32 rs_tl_get_load(struct iwl4965_lq_sta *lq_data, u8 tid)
335{
336 u32 curr_time = jiffies_to_msecs(jiffies);
337 u32 time_diff;
338 s32 index;
339 struct iwl4965_traffic_load *tl = NULL;
340
341 if (tid >= TID_MAX_LOAD_COUNT)
342 return 0;
343
344 tl = &(lq_data->load[tid]);
345
346 curr_time -= curr_time % TID_ROUND_VALUE;
347
348 if (!(tl->queue_count))
349 return 0;
350
351 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
352 index = time_diff / TID_QUEUE_CELL_SPACING;
353
354 /* The history is too long: remove data that is older than */
355 /* TID_MAX_TIME_DIFF */
356 if (index >= TID_QUEUE_MAX_SIZE)
357 rs_tl_rm_old_stats(tl, curr_time);
358
359 return tl->total;
360}
361
c79dd5b5 362static void rs_tl_turn_on_agg_for_tid(struct iwl_priv *priv,
0c11b4de
RR
363 struct iwl4965_lq_sta *lq_data, u8 tid,
364 struct sta_info *sta)
365{
366 unsigned long state;
367 DECLARE_MAC_BUF(mac);
368
07346f81 369 spin_lock_bh(&sta->lock);
cee24a3e 370 state = sta->ampdu_mlme.tid_state_tx[tid];
07346f81 371 spin_unlock_bh(&sta->lock);
0c11b4de
RR
372
373 if (state == HT_AGG_STATE_IDLE &&
374 rs_tl_get_load(lq_data, tid) > IWL_AGG_LOAD_THRESHOLD) {
375 IWL_DEBUG_HT("Starting Tx agg: STA: %s tid: %d\n",
376 print_mac(mac, sta->addr), tid);
377 ieee80211_start_tx_ba_session(priv->hw, sta->addr, tid);
378 }
379}
380
c79dd5b5 381static void rs_tl_turn_on_agg(struct iwl_priv *priv, u8 tid,
0c11b4de
RR
382 struct iwl4965_lq_sta *lq_data,
383 struct sta_info *sta)
384{
385 if ((tid < TID_MAX_LOAD_COUNT))
386 rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
387 else if (tid == IWL_AGG_ALL_TID)
388 for (tid = 0; tid < TID_MAX_LOAD_COUNT; tid++)
389 rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
390}
391
392#endif /* CONFIG_IWLWIFI_HT */
393
39e88504 394static inline int get_num_of_ant_from_rate(u32 rate_n_flags)
fde0db31 395{
39e88504
GC
396 return (!!(rate_n_flags & RATE_MCS_ANT_A_MSK) +
397 !!(rate_n_flags & RATE_MCS_ANT_B_MSK) +
398 !!(rate_n_flags & RATE_MCS_ANT_C_MSK));
fde0db31
GC
399}
400
77626355
BC
401/**
402 * rs_collect_tx_data - Update the success/failure sliding window
403 *
404 * We keep a sliding window of the last 62 packets transmitted
405 * at this rate. window->data contains the bitmask of successful
406 * packets.
407 */
bb8c093b 408static int rs_collect_tx_data(struct iwl4965_rate_scale_data *windows,
99556438
RR
409 int scale_index, s32 tpt, int retries,
410 int successes)
b481de9c 411{
bb8c093b 412 struct iwl4965_rate_scale_data *window = NULL;
39e88504 413 static const u64 mask = (((u64)1) << (IWL_RATE_MAX_WINDOW - 1));
b481de9c
ZY
414 s32 fail_count;
415
dc2453ae
TW
416 if (scale_index < 0 || scale_index >= IWL_RATE_COUNT)
417 return -EINVAL;
b481de9c 418
77626355 419 /* Select data for current tx bit rate */
b481de9c
ZY
420 window = &(windows[scale_index]);
421
77626355
BC
422 /*
423 * Keep track of only the latest 62 tx frame attempts in this rate's
424 * history window; anything older isn't really relevant any more.
425 * If we have filled up the sliding window, drop the oldest attempt;
426 * if the oldest attempt (highest bit in bitmap) shows "success",
427 * subtract "1" from the success counter (this is the main reason
428 * we keep these bitmaps!).
429 */
99556438 430 while (retries > 0) {
39e88504
GC
431 if (window->counter >= IWL_RATE_MAX_WINDOW) {
432
433 /* remove earliest */
434 window->counter = IWL_RATE_MAX_WINDOW - 1;
435
99556438
RR
436 if (window->data & mask) {
437 window->data &= ~mask;
39e88504 438 window->success_counter--;
99556438 439 }
b481de9c 440 }
b481de9c 441
99556438
RR
442 /* Increment frames-attempted counter */
443 window->counter++;
444
445 /* Shift bitmap by one frame (throw away oldest history),
446 * OR in "1", and increment "success" if this
447 * frame was successful. */
39e88504 448 window->data <<= 1;;
99556438 449 if (successes > 0) {
39e88504 450 window->success_counter++;
99556438
RR
451 window->data |= 0x1;
452 successes--;
453 }
77626355 454
99556438 455 retries--;
b481de9c
ZY
456 }
457
77626355 458 /* Calculate current success ratio, avoid divide-by-0! */
b481de9c
ZY
459 if (window->counter > 0)
460 window->success_ratio = 128 * (100 * window->success_counter)
461 / window->counter;
462 else
463 window->success_ratio = IWL_INVALID_VALUE;
464
465 fail_count = window->counter - window->success_counter;
466
77626355 467 /* Calculate average throughput, if we have enough history. */
b481de9c
ZY
468 if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
469 (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
470 window->average_tpt = (window->success_ratio * tpt + 64) / 128;
471 else
472 window->average_tpt = IWL_INVALID_VALUE;
473
77626355 474 /* Tag this window as having been updated */
b481de9c
ZY
475 window->stamp = jiffies;
476
dc2453ae 477 return 0;
b481de9c
ZY
478}
479
77626355
BC
480/*
481 * Fill uCode API rate_n_flags field, based on "search" or "active" table.
482 */
39e88504
GC
483/* FIXME:RS:remove this function and put the flags statically in the table */
484static u32 rate_n_flags_from_tbl(struct iwl4965_scale_tbl_info *tbl,
485 int index, u8 use_green)
b481de9c 486{
39e88504
GC
487 u32 rate_n_flags = 0;
488
b481de9c 489 if (is_legacy(tbl->lq_type)) {
1826dcc0 490 rate_n_flags = iwl_rates[index].plcp;
b481de9c 491 if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
39e88504 492 rate_n_flags |= RATE_MCS_CCK_MSK;
b481de9c 493
fde0db31
GC
494 } else if (is_Ht(tbl->lq_type)) {
495 if (index > IWL_LAST_OFDM_RATE) {
496 IWL_ERROR("invalid HT rate index %d\n", index);
b481de9c 497 index = IWL_LAST_OFDM_RATE;
fde0db31 498 }
39e88504 499 rate_n_flags = RATE_MCS_HT_MSK;
fde0db31
GC
500
501 if (is_siso(tbl->lq_type))
1826dcc0 502 rate_n_flags |= iwl_rates[index].plcp_siso;
fde0db31 503 else if (is_mimo2(tbl->lq_type))
1826dcc0 504 rate_n_flags |= iwl_rates[index].plcp_mimo2;
fde0db31 505 else
1826dcc0 506 rate_n_flags |= iwl_rates[index].plcp_mimo3;
b481de9c 507 } else {
fde0db31 508 IWL_ERROR("Invalid tbl->lq_type %d\n", tbl->lq_type);
b481de9c
ZY
509 }
510
39e88504 511 rate_n_flags |= ((tbl->ant_type << RATE_MCS_ANT_POS) &
fde0db31 512 RATE_MCS_ANT_ABC_MSK);
b481de9c 513
39e88504
GC
514 if (is_Ht(tbl->lq_type)) {
515 if (tbl->is_fat) {
516 if (tbl->is_dup)
517 rate_n_flags |= RATE_MCS_DUP_MSK;
518 else
519 rate_n_flags |= RATE_MCS_FAT_MSK;
520 }
521 if (tbl->is_SGI)
522 rate_n_flags |= RATE_MCS_SGI_MSK;
523
524 if (use_green) {
525 rate_n_flags |= RATE_MCS_GF_MSK;
526 if (is_siso(tbl->lq_type) && tbl->is_SGI) {
527 rate_n_flags &= ~RATE_MCS_SGI_MSK;
528 IWL_ERROR("GF was set with SGI:SISO\n");
529 }
530 }
b481de9c 531 }
39e88504 532 return rate_n_flags;
b481de9c
ZY
533}
534
77626355
BC
535/*
536 * Interpret uCode API's rate_n_flags format,
537 * fill "search" or "active" tx mode table.
538 */
39e88504 539static int rs_get_tbl_info_from_mcs(const u32 rate_n_flags,
8318d78a
JB
540 enum ieee80211_band band,
541 struct iwl4965_scale_tbl_info *tbl,
b481de9c
ZY
542 int *rate_idx)
543{
39e88504
GC
544 u32 ant_msk = (rate_n_flags & RATE_MCS_ANT_ABC_MSK);
545 u8 num_of_ant = get_num_of_ant_from_rate(rate_n_flags);
546 u8 mcs;
b481de9c 547
39e88504 548 *rate_idx = iwl4965_hwrate_to_plcp_idx(rate_n_flags);
b481de9c 549
fde0db31 550 if (*rate_idx == IWL_RATE_INVALID) {
b481de9c 551 *rate_idx = -1;
dc2453ae 552 return -EINVAL;
b481de9c 553 }
77626355 554 tbl->is_SGI = 0; /* default legacy setup */
b481de9c
ZY
555 tbl->is_fat = 0;
556 tbl->is_dup = 0;
fde0db31
GC
557 tbl->ant_type = (ant_msk >> RATE_MCS_ANT_POS);
558 tbl->lq_type = LQ_NONE;
b481de9c 559
77626355 560 /* legacy rate format */
39e88504 561 if (!(rate_n_flags & RATE_MCS_HT_MSK)) {
fde0db31 562 if (num_of_ant == 1) {
8318d78a 563 if (band == IEEE80211_BAND_5GHZ)
b481de9c
ZY
564 tbl->lq_type = LQ_A;
565 else
566 tbl->lq_type = LQ_G;
b481de9c 567 }
fde0db31 568 /* HT rate format */
b481de9c 569 } else {
39e88504 570 if (rate_n_flags & RATE_MCS_SGI_MSK)
b481de9c
ZY
571 tbl->is_SGI = 1;
572
39e88504
GC
573 if ((rate_n_flags & RATE_MCS_FAT_MSK) ||
574 (rate_n_flags & RATE_MCS_DUP_MSK))
b481de9c
ZY
575 tbl->is_fat = 1;
576
39e88504 577 if (rate_n_flags & RATE_MCS_DUP_MSK)
b481de9c 578 tbl->is_dup = 1;
fde0db31 579
39e88504 580 mcs = rs_extract_rate(rate_n_flags);
fde0db31 581
39e88504
GC
582 /* SISO */
583 if (mcs <= IWL_RATE_SISO_60M_PLCP) {
fde0db31
GC
584 if (num_of_ant == 1)
585 tbl->lq_type = LQ_SISO; /*else NONE*/
586 /* MIMO2 */
39e88504 587 } else if (mcs <= IWL_RATE_MIMO2_60M_PLCP) {
fde0db31
GC
588 if (num_of_ant == 2)
589 tbl->lq_type = LQ_MIMO2;
590 /* MIMO3 */
591 } else {
592 if (num_of_ant == 3)
593 tbl->lq_type = LQ_MIMO3;
594 }
b481de9c
ZY
595 }
596 return 0;
597}
aade00ce
GC
598
599/* switch to another antenna/antennas and return 1 */
600/* if no other valid antenna found, return 0 */
601static int rs_toggle_antenna(u32 valid_ant, u32 *rate_n_flags,
602 struct iwl4965_scale_tbl_info *tbl)
b481de9c 603{
aade00ce
GC
604 u8 new_ant_type;
605
606 if (!tbl->ant_type || tbl->ant_type > ANT_ABC)
607 return 0;
608
609 if (!rs_is_valid_ant(valid_ant, tbl->ant_type))
610 return 0;
611
612 new_ant_type = ant_toggle_lookup[tbl->ant_type];
613
614 while ((new_ant_type != tbl->ant_type) &&
615 !rs_is_valid_ant(valid_ant, new_ant_type))
616 new_ant_type = ant_toggle_lookup[new_ant_type];
617
618 if (new_ant_type == tbl->ant_type)
619 return 0;
620
621 tbl->ant_type = new_ant_type;
622 *rate_n_flags &= ~RATE_MCS_ANT_ABC_MSK;
623 *rate_n_flags |= new_ant_type << RATE_MCS_ANT_POS;
624 return 1;
b481de9c
ZY
625}
626
39e88504 627/* FIXME:RS: in 4965 we don't use greenfield at all */
f935a6da
GC
628/* FIXME:RS: don't use greenfield for now in TX */
629/* #ifdef CONFIG_IWL4965_HT */
630#if 0
631static inline u8 rs_use_green(struct iwl_priv *priv, struct ieee80211_conf *conf)
b481de9c 632{
270243a5
RR
633 return ((conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) &&
634 priv->current_ht_config.is_green_field &&
635 !priv->current_ht_config.non_GF_STA_present);
f935a6da 636}
39e88504 637#else
f935a6da
GC
638static inline u8 rs_use_green(struct iwl_priv *priv, struct ieee80211_conf *conf)
639{
dc2453ae 640 return 0;
b481de9c 641}
f935a6da 642#endif /* CONFIG_IWL4965_HT */
b481de9c
ZY
643
644/**
645 * rs_get_supported_rates - get the available rates
646 *
647 * if management frame or broadcast frame only return
648 * basic available rates.
649 *
650 */
eecd6e57 651static u16 rs_get_supported_rates(struct iwl4965_lq_sta *lq_sta,
b481de9c 652 struct ieee80211_hdr *hdr,
eecd6e57 653 enum iwl_table_type rate_type)
b481de9c 654{
eecd6e57
GC
655 if (hdr && is_multicast_ether_addr(hdr->addr1) &&
656 lq_sta->active_rate_basic)
657 return lq_sta->active_rate_basic;
658
659 if (is_legacy(rate_type)) {
660 return lq_sta->active_legacy_rate;
661 } else {
b481de9c 662 if (is_siso(rate_type))
eecd6e57 663 return lq_sta->active_siso_rate;
fde0db31 664 else if (is_mimo2(rate_type))
eecd6e57 665 return lq_sta->active_mimo2_rate;
b481de9c 666 else
eecd6e57 667 return lq_sta->active_mimo3_rate;
c33104f0 668 }
b481de9c
ZY
669}
670
bf403db8
EK
671static u16 rs_get_adjacent_rate(struct iwl_priv *priv, u8 index, u16 rate_mask,
672 int rate_type)
b481de9c
ZY
673{
674 u8 high = IWL_RATE_INVALID;
675 u8 low = IWL_RATE_INVALID;
676
01ebd063 677 /* 802.11A or ht walks to the next literal adjacent rate in
b481de9c
ZY
678 * the rate table */
679 if (is_a_band(rate_type) || !is_legacy(rate_type)) {
680 int i;
681 u32 mask;
682
683 /* Find the previous rate that is in the rate mask */
684 i = index - 1;
685 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
686 if (rate_mask & mask) {
687 low = i;
688 break;
689 }
690 }
691
692 /* Find the next rate that is in the rate mask */
693 i = index + 1;
694 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
695 if (rate_mask & mask) {
696 high = i;
697 break;
698 }
699 }
700
701 return (high << 8) | low;
702 }
703
704 low = index;
705 while (low != IWL_RATE_INVALID) {
1826dcc0 706 low = iwl_rates[low].prev_rs;
b481de9c
ZY
707 if (low == IWL_RATE_INVALID)
708 break;
709 if (rate_mask & (1 << low))
710 break;
711 IWL_DEBUG_RATE("Skipping masked lower rate: %d\n", low);
712 }
713
714 high = index;
715 while (high != IWL_RATE_INVALID) {
1826dcc0 716 high = iwl_rates[high].next_rs;
b481de9c
ZY
717 if (high == IWL_RATE_INVALID)
718 break;
719 if (rate_mask & (1 << high))
720 break;
721 IWL_DEBUG_RATE("Skipping masked higher rate: %d\n", high);
722 }
723
724 return (high << 8) | low;
725}
726
39e88504 727static u32 rs_get_lower_rate(struct iwl4965_lq_sta *lq_sta,
bb8c093b 728 struct iwl4965_scale_tbl_info *tbl, u8 scale_index,
39e88504 729 u8 ht_possible)
b481de9c 730{
b481de9c
ZY
731 s32 low;
732 u16 rate_mask;
733 u16 high_low;
734 u8 switch_to_legacy = 0;
c33104f0 735 u8 is_green = lq_sta->is_green;
b481de9c
ZY
736
737 /* check if we need to switch from HT to legacy rates.
738 * assumption is that mandatory rates (1Mbps or 6Mbps)
739 * are always supported (spec demand) */
740 if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_index)) {
741 switch_to_legacy = 1;
742 scale_index = rs_ht_to_legacy[scale_index];
8318d78a 743 if (lq_sta->band == IEEE80211_BAND_5GHZ)
b481de9c
ZY
744 tbl->lq_type = LQ_A;
745 else
746 tbl->lq_type = LQ_G;
747
69fdb309 748 if (num_of_ant(tbl->ant_type) > 1)
fde0db31 749 tbl->ant_type = ANT_A;/*FIXME:RS*/
b481de9c
ZY
750
751 tbl->is_fat = 0;
752 tbl->is_SGI = 0;
753 }
754
eecd6e57 755 rate_mask = rs_get_supported_rates(lq_sta, NULL, tbl->lq_type);
b481de9c 756
77626355 757 /* Mask with station rate restriction */
b481de9c 758 if (is_legacy(tbl->lq_type)) {
77626355 759 /* supp_rates has no CCK bits in A mode */
8318d78a 760 if (lq_sta->band == IEEE80211_BAND_5GHZ)
b481de9c 761 rate_mask = (u16)(rate_mask &
c33104f0 762 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
b481de9c 763 else
c33104f0 764 rate_mask = (u16)(rate_mask & lq_sta->supp_rates);
b481de9c
ZY
765 }
766
77626355 767 /* If we switched from HT to legacy, check current rate */
dc2453ae 768 if (switch_to_legacy && (rate_mask & (1 << scale_index))) {
39e88504
GC
769 low = scale_index;
770 goto out;
b481de9c
ZY
771 }
772
bf403db8
EK
773 high_low = rs_get_adjacent_rate(lq_sta->drv, scale_index, rate_mask,
774 tbl->lq_type);
b481de9c
ZY
775 low = high_low & 0xff;
776
39e88504
GC
777 if (low == IWL_RATE_INVALID)
778 low = scale_index;
779
780out:
781 return rate_n_flags_from_tbl(tbl, low, is_green);
b481de9c
ZY
782}
783
77626355
BC
784/*
785 * mac80211 sends us Tx status
786 */
c33104f0 787static void rs_tx_status(void *priv_rate, struct net_device *dev,
b481de9c
ZY
788 struct sk_buff *skb,
789 struct ieee80211_tx_status *tx_resp)
790{
791 int status;
792 u8 retries;
793 int rs_index, index = 0;
c33104f0 794 struct iwl4965_lq_sta *lq_sta;
66c73db7 795 struct iwl_link_quality_cmd *table;
b481de9c
ZY
796 struct sta_info *sta;
797 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
c79dd5b5 798 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
b481de9c 799 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4c424e4c 800 struct ieee80211_hw *hw = local_to_hw(local);
bb8c093b
CH
801 struct iwl4965_rate_scale_data *window = NULL;
802 struct iwl4965_rate_scale_data *search_win = NULL;
39e88504 803 u32 tx_rate;
bb8c093b
CH
804 struct iwl4965_scale_tbl_info tbl_type;
805 struct iwl4965_scale_tbl_info *curr_tbl, *search_tbl;
b481de9c
ZY
806 u8 active_index = 0;
807 u16 fc = le16_to_cpu(hdr->frame_control);
808 s32 tpt = 0;
809
58826351 810 IWL_DEBUG_RATE_LIMIT("get frame ack response, update rate scale window\n");
b481de9c
ZY
811
812 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1))
813 return;
814
99556438
RR
815 /* This packet was aggregated but doesn't carry rate scale info */
816 if ((tx_resp->control.flags & IEEE80211_TXCTL_AMPDU) &&
817 !(tx_resp->flags & IEEE80211_TX_STATUS_AMPDU))
818 return;
819
b481de9c
ZY
820 retries = tx_resp->retry_count;
821
822 if (retries > 15)
823 retries = 15;
824
d0709a65 825 rcu_read_lock();
b481de9c
ZY
826
827 sta = sta_info_get(local, hdr->addr1);
828
f4d6082d
TW
829 if (!sta || !sta->rate_ctrl_priv)
830 goto out;
831
b481de9c 832
c33104f0 833 lq_sta = (struct iwl4965_lq_sta *)sta->rate_ctrl_priv;
b481de9c
ZY
834
835 if (!priv->lq_mngr.lq_ready)
f4d6082d 836 goto out;
b481de9c 837
c33104f0
TW
838 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
839 !lq_sta->ibss_sta_added)
f4d6082d 840 goto out;
b481de9c 841
c33104f0
TW
842 table = &lq_sta->lq;
843 active_index = lq_sta->active_tbl;
b481de9c 844
c33104f0
TW
845 curr_tbl = &(lq_sta->lq_info[active_index]);
846 search_tbl = &(lq_sta->lq_info[(1 - active_index)]);
bb8c093b 847 window = (struct iwl4965_rate_scale_data *)
b481de9c 848 &(curr_tbl->win[0]);
bb8c093b 849 search_win = (struct iwl4965_rate_scale_data *)
b481de9c
ZY
850 &(search_tbl->win[0]);
851
77626355
BC
852 /*
853 * Ignore this Tx frame response if its initial rate doesn't match
854 * that of latest Link Quality command. There may be stragglers
855 * from a previous Link Quality command, but we're no longer interested
856 * in those; they're either from the "active" mode while we're trying
857 * to check "search" mode, or a prior "search" mode after we've moved
858 * to a new "search" mode (which might become the new "active" mode).
859 */
39e88504
GC
860 tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
861 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
4c424e4c
RR
862 if (priv->band == IEEE80211_BAND_5GHZ)
863 rs_index -= IWL_FIRST_OFDM_RATE;
864
865 if ((tx_resp->control.tx_rate == NULL) ||
866 (tbl_type.is_SGI ^
867 !!(tx_resp->control.flags & IEEE80211_TXCTL_SHORT_GI)) ||
868 (tbl_type.is_fat ^
869 !!(tx_resp->control.flags & IEEE80211_TXCTL_40_MHZ_WIDTH)) ||
870 (tbl_type.is_dup ^
871 !!(tx_resp->control.flags & IEEE80211_TXCTL_DUP_DATA)) ||
fde0db31 872 (tbl_type.ant_type ^ tx_resp->control.antenna_sel_tx) ||
39e88504 873 (!!(tx_rate & RATE_MCS_HT_MSK) ^
4c424e4c 874 !!(tx_resp->control.flags & IEEE80211_TXCTL_OFDM_HT)) ||
39e88504 875 (!!(tx_rate & RATE_MCS_GF_MSK) ^
4c424e4c
RR
876 !!(tx_resp->control.flags & IEEE80211_TXCTL_GREEN_FIELD)) ||
877 (hw->wiphy->bands[priv->band]->bitrates[rs_index].bitrate !=
878 tx_resp->control.tx_rate->bitrate)) {
39e88504 879 IWL_DEBUG_RATE("initial rate does not match 0x%x\n", tx_rate);
f4d6082d 880 goto out;
b481de9c
ZY
881 }
882
77626355 883 /* Update frame history window with "failure" for each Tx retry. */
b481de9c 884 while (retries) {
77626355
BC
885 /* Look up the rate and other info used for each tx attempt.
886 * Each tx attempt steps one entry deeper in the rate table. */
39e88504
GC
887 tx_rate = le32_to_cpu(table->rs_table[index].rate_n_flags);
888 rs_get_tbl_info_from_mcs(tx_rate, priv->band,
b481de9c
ZY
889 &tbl_type, &rs_index);
890
77626355
BC
891 /* If type matches "search" table,
892 * add failure to "search" history */
b481de9c 893 if ((tbl_type.lq_type == search_tbl->lq_type) &&
fde0db31 894 (tbl_type.ant_type == search_tbl->ant_type) &&
b481de9c
ZY
895 (tbl_type.is_SGI == search_tbl->is_SGI)) {
896 if (search_tbl->expected_tpt)
897 tpt = search_tbl->expected_tpt[rs_index];
898 else
899 tpt = 0;
99556438 900 rs_collect_tx_data(search_win, rs_index, tpt, 1, 0);
77626355
BC
901
902 /* Else if type matches "current/active" table,
903 * add failure to "current/active" history */
b481de9c 904 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
fde0db31 905 (tbl_type.ant_type == curr_tbl->ant_type) &&
b481de9c
ZY
906 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
907 if (curr_tbl->expected_tpt)
908 tpt = curr_tbl->expected_tpt[rs_index];
909 else
910 tpt = 0;
99556438 911 rs_collect_tx_data(window, rs_index, tpt, 1, 0);
b481de9c 912 }
77626355
BC
913
914 /* If not searching for a new mode, increment failed counter
915 * ... this helps determine when to start searching again */
c33104f0
TW
916 if (lq_sta->stay_in_tbl)
917 lq_sta->total_failed++;
b481de9c
ZY
918 --retries;
919 index++;
920
921 }
922
77626355
BC
923 /*
924 * Find (by rate) the history window to update with final Tx attempt;
925 * if Tx was successful first try, use original rate,
926 * else look up the rate that was, finally, successful.
927 */
39e88504
GC
928 tx_rate = le32_to_cpu(table->rs_table[index].rate_n_flags);
929 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
b481de9c 930
77626355 931 /* Update frame history window with "success" if Tx got ACKed ... */
b481de9c
ZY
932 if (tx_resp->flags & IEEE80211_TX_STATUS_ACK)
933 status = 1;
934 else
935 status = 0;
936
77626355
BC
937 /* If type matches "search" table,
938 * add final tx status to "search" history */
b481de9c 939 if ((tbl_type.lq_type == search_tbl->lq_type) &&
fde0db31 940 (tbl_type.ant_type == search_tbl->ant_type) &&
b481de9c
ZY
941 (tbl_type.is_SGI == search_tbl->is_SGI)) {
942 if (search_tbl->expected_tpt)
943 tpt = search_tbl->expected_tpt[rs_index];
944 else
945 tpt = 0;
99556438
RR
946 if (tx_resp->control.flags & IEEE80211_TXCTL_AMPDU)
947 rs_collect_tx_data(search_win, rs_index, tpt,
948 tx_resp->ampdu_ack_len,
949 tx_resp->ampdu_ack_map);
950 else
951 rs_collect_tx_data(search_win, rs_index, tpt,
952 1, status);
77626355
BC
953 /* Else if type matches "current/active" table,
954 * add final tx status to "current/active" history */
b481de9c 955 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
fde0db31 956 (tbl_type.ant_type == curr_tbl->ant_type) &&
b481de9c
ZY
957 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
958 if (curr_tbl->expected_tpt)
959 tpt = curr_tbl->expected_tpt[rs_index];
960 else
961 tpt = 0;
99556438
RR
962 if (tx_resp->control.flags & IEEE80211_TXCTL_AMPDU)
963 rs_collect_tx_data(window, rs_index, tpt,
964 tx_resp->ampdu_ack_len,
965 tx_resp->ampdu_ack_map);
966 else
967 rs_collect_tx_data(window, rs_index, tpt,
968 1, status);
b481de9c
ZY
969 }
970
77626355
BC
971 /* If not searching for new mode, increment success/failed counter
972 * ... these help determine when to start searching again */
c33104f0 973 if (lq_sta->stay_in_tbl) {
99556438
RR
974 if (tx_resp->control.flags & IEEE80211_TXCTL_AMPDU) {
975 lq_sta->total_success += tx_resp->ampdu_ack_map;
976 lq_sta->total_failed +=
977 (tx_resp->ampdu_ack_len - tx_resp->ampdu_ack_map);
978 } else {
979 if (status)
980 lq_sta->total_success++;
981 else
982 lq_sta->total_failed++;
983 }
b481de9c
ZY
984 }
985
77626355 986 /* See if there's a better rate or modulation mode to try. */
b481de9c 987 rs_rate_scale_perform(priv, dev, hdr, sta);
f4d6082d 988out:
d0709a65 989 rcu_read_unlock();
b481de9c
ZY
990 return;
991}
992
77626355
BC
993/*
994 * Begin a period of staying with a selected modulation mode.
995 * Set "stay_in_tbl" flag to prevent any mode switches.
996 * Set frame tx success limits according to legacy vs. high-throughput,
997 * and reset overall (spanning all rates) tx success history statistics.
998 * These control how long we stay using same modulation mode before
999 * searching for a new mode.
1000 */
bf403db8 1001static void rs_set_stay_in_table(struct iwl_priv *priv, u8 is_legacy,
c33104f0 1002 struct iwl4965_lq_sta *lq_sta)
b481de9c 1003{
eecd6e57 1004 IWL_DEBUG_RATE("we are staying in the same table\n");
c33104f0 1005 lq_sta->stay_in_tbl = 1; /* only place this gets set */
b481de9c 1006 if (is_legacy) {
c33104f0
TW
1007 lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT;
1008 lq_sta->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
1009 lq_sta->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
b481de9c 1010 } else {
c33104f0
TW
1011 lq_sta->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
1012 lq_sta->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
1013 lq_sta->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
b481de9c 1014 }
c33104f0
TW
1015 lq_sta->table_count = 0;
1016 lq_sta->total_failed = 0;
1017 lq_sta->total_success = 0;
b481de9c
ZY
1018}
1019
77626355
BC
1020/*
1021 * Find correct throughput table for given mode of modulation
1022 */
eecd6e57 1023static void rs_set_expected_tpt_table(struct iwl4965_lq_sta *lq_sta,
bb8c093b 1024 struct iwl4965_scale_tbl_info *tbl)
b481de9c
ZY
1025{
1026 if (is_legacy(tbl->lq_type)) {
1027 if (!is_a_band(tbl->lq_type))
1028 tbl->expected_tpt = expected_tpt_G;
1029 else
1030 tbl->expected_tpt = expected_tpt_A;
1031 } else if (is_siso(tbl->lq_type)) {
c33104f0 1032 if (tbl->is_fat && !lq_sta->is_dup)
b481de9c
ZY
1033 if (tbl->is_SGI)
1034 tbl->expected_tpt = expected_tpt_siso40MHzSGI;
1035 else
1036 tbl->expected_tpt = expected_tpt_siso40MHz;
1037 else if (tbl->is_SGI)
1038 tbl->expected_tpt = expected_tpt_siso20MHzSGI;
1039 else
1040 tbl->expected_tpt = expected_tpt_siso20MHz;
1041
fde0db31 1042 } else if (is_mimo(tbl->lq_type)) { /* FIXME:need to separate mimo2/3 */
c33104f0 1043 if (tbl->is_fat && !lq_sta->is_dup)
b481de9c
ZY
1044 if (tbl->is_SGI)
1045 tbl->expected_tpt = expected_tpt_mimo40MHzSGI;
1046 else
1047 tbl->expected_tpt = expected_tpt_mimo40MHz;
1048 else if (tbl->is_SGI)
1049 tbl->expected_tpt = expected_tpt_mimo20MHzSGI;
1050 else
1051 tbl->expected_tpt = expected_tpt_mimo20MHz;
1052 } else
1053 tbl->expected_tpt = expected_tpt_G;
1054}
1055
c8b0e6e1 1056#ifdef CONFIG_IWL4965_HT
77626355
BC
1057/*
1058 * Find starting rate for new "search" high-throughput mode of modulation.
1059 * Goal is to find lowest expected rate (under perfect conditions) that is
1060 * above the current measured throughput of "active" mode, to give new mode
1061 * a fair chance to prove itself without too many challenges.
1062 *
1063 * This gets called when transitioning to more aggressive modulation
1064 * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
1065 * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
1066 * to decrease to match "active" throughput. When moving from MIMO to SISO,
1067 * bit rate will typically need to increase, but not if performance was bad.
1068 */
c79dd5b5 1069static s32 rs_get_best_rate(struct iwl_priv *priv,
c33104f0 1070 struct iwl4965_lq_sta *lq_sta,
77626355 1071 struct iwl4965_scale_tbl_info *tbl, /* "search" */
f935a6da 1072 u16 rate_mask, s8 index)
b481de9c 1073{
77626355 1074 /* "active" values */
bb8c093b 1075 struct iwl4965_scale_tbl_info *active_tbl =
c33104f0 1076 &(lq_sta->lq_info[lq_sta->active_tbl]);
b481de9c 1077 s32 active_sr = active_tbl->win[index].success_ratio;
b481de9c 1078 s32 active_tpt = active_tbl->expected_tpt[index];
77626355
BC
1079
1080 /* expected "search" throughput */
1081 s32 *tpt_tbl = tbl->expected_tpt;
1082
1083 s32 new_rate, high, low, start_hi;
b481de9c 1084 u16 high_low;
f935a6da 1085 s8 rate = index;
b481de9c
ZY
1086
1087 new_rate = high = low = start_hi = IWL_RATE_INVALID;
1088
1089 for (; ;) {
bf403db8
EK
1090 high_low = rs_get_adjacent_rate(priv, rate, rate_mask,
1091 tbl->lq_type);
b481de9c
ZY
1092
1093 low = high_low & 0xff;
1094 high = (high_low >> 8) & 0xff;
1095
77626355
BC
1096 /*
1097 * Lower the "search" bit rate, to give new "search" mode
1098 * approximately the same throughput as "active" if:
1099 *
1100 * 1) "Active" mode has been working modestly well (but not
1101 * great), and expected "search" throughput (under perfect
1102 * conditions) at candidate rate is above the actual
1103 * measured "active" throughput (but less than expected
1104 * "active" throughput under perfect conditions).
1105 * OR
1106 * 2) "Active" mode has been working perfectly or very well
1107 * and expected "search" throughput (under perfect
1108 * conditions) at candidate rate is above expected
1109 * "active" throughput (under perfect conditions).
1110 */
c33104f0 1111 if ((((100 * tpt_tbl[rate]) > lq_sta->last_tpt) &&
b481de9c
ZY
1112 ((active_sr > IWL_RATE_DECREASE_TH) &&
1113 (active_sr <= IWL_RATE_HIGH_TH) &&
1114 (tpt_tbl[rate] <= active_tpt))) ||
1115 ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
1116 (tpt_tbl[rate] > active_tpt))) {
1117
77626355
BC
1118 /* (2nd or later pass)
1119 * If we've already tried to raise the rate, and are
1120 * now trying to lower it, use the higher rate. */
b481de9c
ZY
1121 if (start_hi != IWL_RATE_INVALID) {
1122 new_rate = start_hi;
1123 break;
1124 }
77626355 1125
b481de9c 1126 new_rate = rate;
77626355
BC
1127
1128 /* Loop again with lower rate */
b481de9c
ZY
1129 if (low != IWL_RATE_INVALID)
1130 rate = low;
77626355
BC
1131
1132 /* Lower rate not available, use the original */
b481de9c
ZY
1133 else
1134 break;
77626355
BC
1135
1136 /* Else try to raise the "search" rate to match "active" */
b481de9c 1137 } else {
77626355
BC
1138 /* (2nd or later pass)
1139 * If we've already tried to lower the rate, and are
1140 * now trying to raise it, use the lower rate. */
b481de9c
ZY
1141 if (new_rate != IWL_RATE_INVALID)
1142 break;
77626355
BC
1143
1144 /* Loop again with higher rate */
b481de9c
ZY
1145 else if (high != IWL_RATE_INVALID) {
1146 start_hi = high;
1147 rate = high;
77626355
BC
1148
1149 /* Higher rate not available, use the original */
b481de9c
ZY
1150 } else {
1151 new_rate = rate;
1152 break;
1153 }
1154 }
1155 }
1156
1157 return new_rate;
1158}
c8b0e6e1 1159#endif /* CONFIG_IWL4965_HT */
b481de9c 1160
77626355
BC
1161/*
1162 * Set up search table for MIMO
1163 */
fde0db31
GC
1164#ifdef CONFIG_IWL4965_HT
1165static int rs_switch_to_mimo2(struct iwl_priv *priv,
c33104f0 1166 struct iwl4965_lq_sta *lq_sta,
270243a5
RR
1167 struct ieee80211_conf *conf,
1168 struct sta_info *sta,
bb8c093b 1169 struct iwl4965_scale_tbl_info *tbl, int index)
b481de9c 1170{
b481de9c
ZY
1171 u16 rate_mask;
1172 s32 rate;
c33104f0 1173 s8 is_green = lq_sta->is_green;
b481de9c 1174
270243a5
RR
1175 if (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) ||
1176 !sta->ht_info.ht_supported)
b481de9c
ZY
1177 return -1;
1178
270243a5 1179 if (priv->current_ht_config.tx_mimo_ps_mode == IWL_MIMO_PS_STATIC)
b481de9c
ZY
1180 return -1;
1181
77626355 1182 /* Need both Tx chains/antennas to support MIMO */
aade00ce 1183 if (priv->hw_params.tx_chains_num < 2)
b481de9c
ZY
1184 return -1;
1185
eecd6e57 1186 IWL_DEBUG_RATE("LQ: try to switch to MIMO2\n");
39e88504
GC
1187
1188 tbl->lq_type = LQ_MIMO2;
c33104f0 1189 tbl->is_dup = lq_sta->is_dup;
b481de9c 1190 tbl->action = 0;
39e88504
GC
1191 rate_mask = lq_sta->active_mimo2_rate;
1192
270243a5 1193 if (priv->current_ht_config.supported_chan_width
39e88504 1194 == IWL_CHANNEL_WIDTH_40MHZ)
b481de9c
ZY
1195 tbl->is_fat = 1;
1196 else
1197 tbl->is_fat = 0;
1198
39e88504 1199 /* FIXME: - don't toggle SGI here
b481de9c 1200 if (tbl->is_fat) {
270243a5 1201 if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
b481de9c
ZY
1202 tbl->is_SGI = 1;
1203 else
1204 tbl->is_SGI = 0;
270243a5 1205 } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
b481de9c
ZY
1206 tbl->is_SGI = 1;
1207 else
1208 tbl->is_SGI = 0;
39e88504 1209 */
b481de9c 1210
eecd6e57 1211 rs_set_expected_tpt_table(lq_sta, tbl);
b481de9c 1212
f935a6da 1213 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
b481de9c 1214
eecd6e57 1215 IWL_DEBUG_RATE("LQ: MIMO2 best rate %d mask %X\n", rate, rate_mask);
39e88504
GC
1216
1217 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
eecd6e57 1218 IWL_DEBUG_RATE("Can't switch with index %d rate mask %x\n",
39e88504 1219 rate, rate_mask);
b481de9c 1220 return -1;
39e88504
GC
1221 }
1222 tbl->current_rate = rate_n_flags_from_tbl(tbl, rate, is_green);
b481de9c 1223
eecd6e57 1224 IWL_DEBUG_RATE("LQ: Switch to new mcs %X index is green %X\n",
39e88504 1225 tbl->current_rate, is_green);
dc2453ae 1226 return 0;
fde0db31 1227}
403ab56b 1228#else
fde0db31
GC
1229static int rs_switch_to_mimo2(struct iwl_priv *priv,
1230 struct iwl4965_lq_sta *lq_sta,
1231 struct ieee80211_conf *conf,
1232 struct sta_info *sta,
1233 struct iwl4965_scale_tbl_info *tbl, int index)
1234{
403ab56b 1235 return -1;
b481de9c 1236}
fde0db31 1237#endif /*CONFIG_IWL4965_HT */
b481de9c 1238
77626355
BC
1239/*
1240 * Set up search table for SISO
1241 */
c79dd5b5 1242static int rs_switch_to_siso(struct iwl_priv *priv,
c33104f0 1243 struct iwl4965_lq_sta *lq_sta,
270243a5
RR
1244 struct ieee80211_conf *conf,
1245 struct sta_info *sta,
bb8c093b 1246 struct iwl4965_scale_tbl_info *tbl, int index)
b481de9c 1247{
c8b0e6e1 1248#ifdef CONFIG_IWL4965_HT
b481de9c 1249 u16 rate_mask;
c33104f0 1250 u8 is_green = lq_sta->is_green;
b481de9c
ZY
1251 s32 rate;
1252
270243a5
RR
1253 if (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) ||
1254 !sta->ht_info.ht_supported)
b481de9c
ZY
1255 return -1;
1256
eecd6e57 1257 IWL_DEBUG_RATE("LQ: try to switch to SISO\n");
39e88504 1258
c33104f0 1259 tbl->is_dup = lq_sta->is_dup;
b481de9c
ZY
1260 tbl->lq_type = LQ_SISO;
1261 tbl->action = 0;
39e88504 1262 rate_mask = lq_sta->active_siso_rate;
b481de9c 1263
270243a5
RR
1264 if (priv->current_ht_config.supported_chan_width
1265 == IWL_CHANNEL_WIDTH_40MHZ)
b481de9c
ZY
1266 tbl->is_fat = 1;
1267 else
1268 tbl->is_fat = 0;
1269
39e88504 1270 /* FIXME: - don't toggle SGI here
b481de9c 1271 if (tbl->is_fat) {
270243a5 1272 if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
b481de9c
ZY
1273 tbl->is_SGI = 1;
1274 else
1275 tbl->is_SGI = 0;
270243a5 1276 } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
b481de9c
ZY
1277 tbl->is_SGI = 1;
1278 else
1279 tbl->is_SGI = 0;
39e88504 1280 */
b481de9c
ZY
1281
1282 if (is_green)
39e88504 1283 tbl->is_SGI = 0; /*11n spec: no SGI in SISO+Greenfield*/
b481de9c 1284
eecd6e57 1285 rs_set_expected_tpt_table(lq_sta, tbl);
f935a6da 1286 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
b481de9c 1287
eecd6e57 1288 IWL_DEBUG_RATE("LQ: get best rate %d mask %X\n", rate, rate_mask);
b481de9c 1289 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
eecd6e57 1290 IWL_DEBUG_RATE("can not switch with index %d rate mask %x\n",
b481de9c
ZY
1291 rate, rate_mask);
1292 return -1;
1293 }
39e88504 1294 tbl->current_rate = rate_n_flags_from_tbl(tbl, rate, is_green);
eecd6e57 1295 IWL_DEBUG_RATE("LQ: Switch to new mcs %X index is green %X\n",
39e88504 1296 tbl->current_rate, is_green);
403ab56b
MA
1297 return 0;
1298#else
1299 return -1;
0c11b4de 1300#endif /*CONFIG_IWL4965_HT */
b481de9c
ZY
1301}
1302
77626355
BC
1303/*
1304 * Try to switch to new modulation mode from legacy
1305 */
c79dd5b5 1306static int rs_move_legacy_other(struct iwl_priv *priv,
c33104f0 1307 struct iwl4965_lq_sta *lq_sta,
270243a5
RR
1308 struct ieee80211_conf *conf,
1309 struct sta_info *sta,
b481de9c
ZY
1310 int index)
1311{
bb8c093b 1312 struct iwl4965_scale_tbl_info *tbl =
c33104f0 1313 &(lq_sta->lq_info[lq_sta->active_tbl]);
bb8c093b 1314 struct iwl4965_scale_tbl_info *search_tbl =
c33104f0 1315 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
bb8c093b
CH
1316 struct iwl4965_rate_scale_data *window = &(tbl->win[index]);
1317 u32 sz = (sizeof(struct iwl4965_scale_tbl_info) -
1318 (sizeof(struct iwl4965_rate_scale_data) * IWL_RATE_COUNT));
b481de9c 1319 u8 start_action = tbl->action;
fde0db31
GC
1320 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1321 int ret = 0;
b481de9c
ZY
1322
1323 for (; ;) {
1324 switch (tbl->action) {
1325 case IWL_LEGACY_SWITCH_ANTENNA:
f935a6da 1326 IWL_DEBUG_RATE("LQ: Legacy toggle Antenna\n");
b481de9c 1327
c33104f0 1328 lq_sta->action_counter++;
77626355
BC
1329
1330 /* Don't change antenna if success has been great */
b481de9c
ZY
1331 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1332 break;
77626355 1333
77626355 1334 /* Set up search table to try other antenna */
b481de9c
ZY
1335 memcpy(search_tbl, tbl, sz);
1336
aade00ce
GC
1337 if (rs_toggle_antenna(valid_tx_ant,
1338 &search_tbl->current_rate, search_tbl)) {
aade00ce
GC
1339 lq_sta->search_better_tbl = 1;
1340 goto out;
1341 }
b481de9c
ZY
1342
1343 case IWL_LEGACY_SWITCH_SISO:
eecd6e57 1344 IWL_DEBUG_RATE("LQ: Legacy switch to SISO\n");
77626355
BC
1345
1346 /* Set up search table to try SISO */
b481de9c 1347 memcpy(search_tbl, tbl, sz);
b481de9c 1348 search_tbl->is_SGI = 0;
c33104f0 1349 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
270243a5 1350 search_tbl, index);
dc2453ae 1351 if (!ret) {
c33104f0
TW
1352 lq_sta->search_better_tbl = 1;
1353 lq_sta->action_counter = 0;
b481de9c 1354 goto out;
dc2453ae 1355 }
b481de9c
ZY
1356
1357 break;
fde0db31 1358 case IWL_LEGACY_SWITCH_MIMO2:
eecd6e57 1359 IWL_DEBUG_RATE("LQ: Legacy switch to MIMO2\n");
77626355
BC
1360
1361 /* Set up search table to try MIMO */
b481de9c 1362 memcpy(search_tbl, tbl, sz);
b481de9c 1363 search_tbl->is_SGI = 0;
fde0db31
GC
1364 search_tbl->ant_type = ANT_AB;/*FIXME:RS*/
1365 /*FIXME:RS:need to check ant validity*/
1366 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
270243a5 1367 search_tbl, index);
dc2453ae 1368 if (!ret) {
c33104f0
TW
1369 lq_sta->search_better_tbl = 1;
1370 lq_sta->action_counter = 0;
b481de9c 1371 goto out;
dc2453ae 1372 }
b481de9c
ZY
1373 break;
1374 }
1375 tbl->action++;
fde0db31 1376 if (tbl->action > IWL_LEGACY_SWITCH_MIMO2)
b481de9c
ZY
1377 tbl->action = IWL_LEGACY_SWITCH_ANTENNA;
1378
1379 if (tbl->action == start_action)
1380 break;
1381
1382 }
1383 return 0;
1384
1385 out:
1386 tbl->action++;
fde0db31 1387 if (tbl->action > IWL_LEGACY_SWITCH_MIMO2)
b481de9c
ZY
1388 tbl->action = IWL_LEGACY_SWITCH_ANTENNA;
1389 return 0;
1390
1391}
1392
77626355
BC
1393/*
1394 * Try to switch to new modulation mode from SISO
1395 */
c79dd5b5 1396static int rs_move_siso_to_other(struct iwl_priv *priv,
c33104f0 1397 struct iwl4965_lq_sta *lq_sta,
270243a5
RR
1398 struct ieee80211_conf *conf,
1399 struct sta_info *sta,
b481de9c
ZY
1400 int index)
1401{
c33104f0 1402 u8 is_green = lq_sta->is_green;
bb8c093b 1403 struct iwl4965_scale_tbl_info *tbl =
c33104f0 1404 &(lq_sta->lq_info[lq_sta->active_tbl]);
bb8c093b 1405 struct iwl4965_scale_tbl_info *search_tbl =
c33104f0 1406 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
bb8c093b
CH
1407 struct iwl4965_rate_scale_data *window = &(tbl->win[index]);
1408 u32 sz = (sizeof(struct iwl4965_scale_tbl_info) -
1409 (sizeof(struct iwl4965_rate_scale_data) * IWL_RATE_COUNT));
b481de9c 1410 u8 start_action = tbl->action;
fde0db31
GC
1411 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1412 int ret;
b481de9c
ZY
1413
1414 for (;;) {
c33104f0 1415 lq_sta->action_counter++;
b481de9c
ZY
1416 switch (tbl->action) {
1417 case IWL_SISO_SWITCH_ANTENNA:
eecd6e57 1418 IWL_DEBUG_RATE("LQ: SISO toggle Antenna\n");
b481de9c
ZY
1419 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1420 break;
b481de9c
ZY
1421
1422 memcpy(search_tbl, tbl, sz);
aade00ce
GC
1423 if (rs_toggle_antenna(valid_tx_ant,
1424 &search_tbl->current_rate, search_tbl)) {
1425 lq_sta->search_better_tbl = 1;
1426 goto out;
1427 }
b481de9c 1428
fde0db31 1429 case IWL_SISO_SWITCH_MIMO2:
eecd6e57 1430 IWL_DEBUG_RATE("LQ: SISO switch to MIMO\n");
b481de9c 1431 memcpy(search_tbl, tbl, sz);
b481de9c 1432 search_tbl->is_SGI = 0;
fde0db31
GC
1433 search_tbl->ant_type = ANT_AB; /*FIXME:RS*/
1434 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
270243a5 1435 search_tbl, index);
dc2453ae 1436 if (!ret) {
c33104f0 1437 lq_sta->search_better_tbl = 1;
b481de9c 1438 goto out;
dc2453ae 1439 }
b481de9c
ZY
1440 break;
1441 case IWL_SISO_SWITCH_GI:
a9841013
EG
1442 if (!tbl->is_fat &&
1443 !(priv->current_ht_config.sgf &
1444 HT_SHORT_GI_20MHZ))
1445 break;
1446 if (tbl->is_fat &&
1447 !(priv->current_ht_config.sgf &
1448 HT_SHORT_GI_40MHZ))
1449 break;
1450
eecd6e57 1451 IWL_DEBUG_RATE("LQ: SISO toggle SGI/NGI\n");
0c11b4de 1452
b481de9c 1453 memcpy(search_tbl, tbl, sz);
39e88504
GC
1454 if (is_green) {
1455 if (!tbl->is_SGI)
1456 break;
1457 else
1458 IWL_ERROR("SGI was set in GF+SISO\n");
b481de9c 1459 }
39e88504 1460 search_tbl->is_SGI = !tbl->is_SGI;
eecd6e57 1461 rs_set_expected_tpt_table(lq_sta, search_tbl);
39e88504
GC
1462 if (tbl->is_SGI) {
1463 s32 tpt = lq_sta->last_tpt / 100;
1464 if (tpt >= search_tbl->expected_tpt[index])
1465 break;
1466 }
1467 search_tbl->current_rate = rate_n_flags_from_tbl(
1468 search_tbl, index, is_green);
1469 lq_sta->search_better_tbl = 1;
b481de9c
ZY
1470 goto out;
1471 }
1472 tbl->action++;
1473 if (tbl->action > IWL_SISO_SWITCH_GI)
1474 tbl->action = IWL_SISO_SWITCH_ANTENNA;
1475
1476 if (tbl->action == start_action)
1477 break;
1478 }
1479 return 0;
1480
1481 out:
1482 tbl->action++;
1483 if (tbl->action > IWL_SISO_SWITCH_GI)
1484 tbl->action = IWL_SISO_SWITCH_ANTENNA;
1485 return 0;
1486}
1487
77626355
BC
1488/*
1489 * Try to switch to new modulation mode from MIMO
1490 */
c79dd5b5 1491static int rs_move_mimo_to_other(struct iwl_priv *priv,
c33104f0 1492 struct iwl4965_lq_sta *lq_sta,
270243a5
RR
1493 struct ieee80211_conf *conf,
1494 struct sta_info *sta,
b481de9c
ZY
1495 int index)
1496{
c33104f0 1497 s8 is_green = lq_sta->is_green;
bb8c093b 1498 struct iwl4965_scale_tbl_info *tbl =
c33104f0 1499 &(lq_sta->lq_info[lq_sta->active_tbl]);
bb8c093b 1500 struct iwl4965_scale_tbl_info *search_tbl =
c33104f0 1501 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
bb8c093b
CH
1502 u32 sz = (sizeof(struct iwl4965_scale_tbl_info) -
1503 (sizeof(struct iwl4965_rate_scale_data) * IWL_RATE_COUNT));
b481de9c 1504 u8 start_action = tbl->action;
aade00ce
GC
1505 /*u8 valid_tx_ant = priv->hw_params.valid_tx_ant;*/
1506 int ret;
b481de9c
ZY
1507
1508 for (;;) {
c33104f0 1509 lq_sta->action_counter++;
b481de9c
ZY
1510 switch (tbl->action) {
1511 case IWL_MIMO_SWITCH_ANTENNA_A:
1512 case IWL_MIMO_SWITCH_ANTENNA_B:
eecd6e57 1513 IWL_DEBUG_RATE("LQ: MIMO2 switch to SISO\n");
0c11b4de 1514
77626355 1515 /* Set up new search table for SISO */
b481de9c 1516 memcpy(search_tbl, tbl, sz);
39e88504
GC
1517
1518 /*FIXME:RS:need to check ant validity + C*/
b481de9c 1519 if (tbl->action == IWL_MIMO_SWITCH_ANTENNA_A)
fde0db31 1520 search_tbl->ant_type = ANT_A;
b481de9c 1521 else
fde0db31 1522 search_tbl->ant_type = ANT_B;
b481de9c 1523
c33104f0 1524 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
270243a5 1525 search_tbl, index);
dc2453ae 1526 if (!ret) {
c33104f0 1527 lq_sta->search_better_tbl = 1;
b481de9c
ZY
1528 goto out;
1529 }
1530 break;
1531
1532 case IWL_MIMO_SWITCH_GI:
a9841013
EG
1533 if (!tbl->is_fat &&
1534 !(priv->current_ht_config.sgf &
1535 HT_SHORT_GI_20MHZ))
1536 break;
1537 if (tbl->is_fat &&
1538 !(priv->current_ht_config.sgf &
1539 HT_SHORT_GI_40MHZ))
1540 break;
1541
eecd6e57 1542 IWL_DEBUG_RATE("LQ: MIMO toggle SGI/NGI\n");
77626355
BC
1543
1544 /* Set up new search table for MIMO */
b481de9c 1545 memcpy(search_tbl, tbl, sz);
39e88504 1546 search_tbl->is_SGI = !tbl->is_SGI;
eecd6e57 1547 rs_set_expected_tpt_table(lq_sta, search_tbl);
77626355
BC
1548 /*
1549 * If active table already uses the fastest possible
1550 * modulation (dual stream with short guard interval),
1551 * and it's working well, there's no need to look
1552 * for a better type of modulation!
1553 */
39e88504 1554 if (tbl->is_SGI) {
c33104f0 1555 s32 tpt = lq_sta->last_tpt / 100;
39e88504
GC
1556 if (tpt >= search_tbl->expected_tpt[index])
1557 break;
b481de9c 1558 }
39e88504
GC
1559 search_tbl->current_rate = rate_n_flags_from_tbl(
1560 search_tbl, index, is_green);
1561 lq_sta->search_better_tbl = 1;
b481de9c
ZY
1562 goto out;
1563
1564 }
1565 tbl->action++;
1566 if (tbl->action > IWL_MIMO_SWITCH_GI)
1567 tbl->action = IWL_MIMO_SWITCH_ANTENNA_A;
1568
1569 if (tbl->action == start_action)
1570 break;
1571 }
1572
1573 return 0;
1574 out:
1575 tbl->action++;
1576 if (tbl->action > IWL_MIMO_SWITCH_GI)
1577 tbl->action = IWL_MIMO_SWITCH_ANTENNA_A;
1578 return 0;
1579
1580}
1581
77626355
BC
1582/*
1583 * Check whether we should continue using same modulation mode, or
1584 * begin search for a new mode, based on:
1585 * 1) # tx successes or failures while using this mode
1586 * 2) # times calling this function
1587 * 3) elapsed time in this mode (not used, for now)
1588 */
c33104f0 1589static void rs_stay_in_table(struct iwl4965_lq_sta *lq_sta)
b481de9c 1590{
bb8c093b 1591 struct iwl4965_scale_tbl_info *tbl;
b481de9c
ZY
1592 int i;
1593 int active_tbl;
1594 int flush_interval_passed = 0;
bf403db8 1595 struct iwl_priv *priv;
b481de9c 1596
bf403db8 1597 priv = lq_sta->drv;
c33104f0 1598 active_tbl = lq_sta->active_tbl;
b481de9c 1599
c33104f0 1600 tbl = &(lq_sta->lq_info[active_tbl]);
b481de9c 1601
77626355 1602 /* If we've been disallowing search, see if we should now allow it */
c33104f0 1603 if (lq_sta->stay_in_tbl) {
b481de9c 1604
77626355 1605 /* Elapsed time using current modulation mode */
c33104f0 1606 if (lq_sta->flush_timer)
b481de9c
ZY
1607 flush_interval_passed =
1608 time_after(jiffies,
c33104f0 1609 (unsigned long)(lq_sta->flush_timer +
b481de9c
ZY
1610 IWL_RATE_SCALE_FLUSH_INTVL));
1611
77626355
BC
1612 /*
1613 * Check if we should allow search for new modulation mode.
1614 * If many frames have failed or succeeded, or we've used
1615 * this same modulation for a long time, allow search, and
1616 * reset history stats that keep track of whether we should
1617 * allow a new search. Also (below) reset all bitmaps and
1618 * stats in active history.
1619 */
c33104f0
TW
1620 if ((lq_sta->total_failed > lq_sta->max_failure_limit) ||
1621 (lq_sta->total_success > lq_sta->max_success_limit) ||
1622 ((!lq_sta->search_better_tbl) && (lq_sta->flush_timer)
b481de9c 1623 && (flush_interval_passed))) {
eecd6e57 1624 IWL_DEBUG_RATE("LQ: stay is expired %d %d %d\n:",
c33104f0
TW
1625 lq_sta->total_failed,
1626 lq_sta->total_success,
b481de9c 1627 flush_interval_passed);
77626355
BC
1628
1629 /* Allow search for new mode */
c33104f0
TW
1630 lq_sta->stay_in_tbl = 0; /* only place reset */
1631 lq_sta->total_failed = 0;
1632 lq_sta->total_success = 0;
1633 lq_sta->flush_timer = 0;
77626355
BC
1634
1635 /*
1636 * Else if we've used this modulation mode enough repetitions
1637 * (regardless of elapsed time or success/failure), reset
1638 * history bitmaps and rate-specific stats for all rates in
1639 * active table.
1640 */
403ab56b 1641 } else {
c33104f0
TW
1642 lq_sta->table_count++;
1643 if (lq_sta->table_count >=
1644 lq_sta->table_count_limit) {
1645 lq_sta->table_count = 0;
b481de9c 1646
eecd6e57 1647 IWL_DEBUG_RATE("LQ: stay in table clear win\n");
b481de9c
ZY
1648 for (i = 0; i < IWL_RATE_COUNT; i++)
1649 rs_rate_scale_clear_window(
1650 &(tbl->win[i]));
1651 }
1652 }
1653
77626355
BC
1654 /* If transitioning to allow "search", reset all history
1655 * bitmaps and stats in active table (this will become the new
1656 * "search" table). */
c33104f0 1657 if (!lq_sta->stay_in_tbl) {
b481de9c
ZY
1658 for (i = 0; i < IWL_RATE_COUNT; i++)
1659 rs_rate_scale_clear_window(&(tbl->win[i]));
1660 }
1661 }
1662}
1663
77626355
BC
1664/*
1665 * Do rate scaling and search for new modulation mode.
1666 */
c79dd5b5 1667static void rs_rate_scale_perform(struct iwl_priv *priv,
b481de9c
ZY
1668 struct net_device *dev,
1669 struct ieee80211_hdr *hdr,
1670 struct sta_info *sta)
1671{
270243a5
RR
1672 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1673 struct ieee80211_hw *hw = local_to_hw(local);
1674 struct ieee80211_conf *conf = &hw->conf;
b481de9c
ZY
1675 int low = IWL_RATE_INVALID;
1676 int high = IWL_RATE_INVALID;
1677 int index;
1678 int i;
bb8c093b 1679 struct iwl4965_rate_scale_data *window = NULL;
b481de9c
ZY
1680 int current_tpt = IWL_INVALID_VALUE;
1681 int low_tpt = IWL_INVALID_VALUE;
1682 int high_tpt = IWL_INVALID_VALUE;
1683 u32 fail_count;
1684 s8 scale_action = 0;
1685 u16 fc, rate_mask;
1686 u8 update_lq = 0;
c33104f0 1687 struct iwl4965_lq_sta *lq_sta;
bb8c093b 1688 struct iwl4965_scale_tbl_info *tbl, *tbl1;
b481de9c 1689 u16 rate_scale_index_msk = 0;
39e88504 1690 u32 rate;
b481de9c
ZY
1691 u8 is_green = 0;
1692 u8 active_tbl = 0;
1693 u8 done_search = 0;
1694 u16 high_low;
0c11b4de
RR
1695#ifdef CONFIG_IWL4965_HT
1696 u8 tid = MAX_TID_COUNT;
0c11b4de 1697#endif
b481de9c
ZY
1698
1699 IWL_DEBUG_RATE("rate scale calculate new rate for skb\n");
1700
1701 fc = le16_to_cpu(hdr->frame_control);
1702 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1)) {
1703 /* Send management frames and broadcast/multicast data using
1704 * lowest rate. */
1705 /* TODO: this could probably be improved.. */
1706 return;
1707 }
1708
1709 if (!sta || !sta->rate_ctrl_priv)
1710 return;
1711
1712 if (!priv->lq_mngr.lq_ready) {
1713 IWL_DEBUG_RATE("still rate scaling not ready\n");
1714 return;
1715 }
c33104f0 1716 lq_sta = (struct iwl4965_lq_sta *)sta->rate_ctrl_priv;
b481de9c 1717
0c11b4de 1718#ifdef CONFIG_IWL4965_HT
54dbb525 1719 rs_tl_add_packet(lq_sta, hdr);
0c11b4de 1720#endif
77626355
BC
1721 /*
1722 * Select rate-scale / modulation-mode table to work with in
1723 * the rest of this function: "search" if searching for better
1724 * modulation mode, or "active" if doing rate scaling within a mode.
1725 */
c33104f0
TW
1726 if (!lq_sta->search_better_tbl)
1727 active_tbl = lq_sta->active_tbl;
b481de9c 1728 else
c33104f0 1729 active_tbl = 1 - lq_sta->active_tbl;
b481de9c 1730
c33104f0
TW
1731 tbl = &(lq_sta->lq_info[active_tbl]);
1732 is_green = lq_sta->is_green;
b481de9c 1733
77626355 1734 /* current tx rate */
8318d78a 1735 index = sta->last_txrate_idx;
b481de9c
ZY
1736
1737 IWL_DEBUG_RATE("Rate scale index %d for type %d\n", index,
1738 tbl->lq_type);
1739
77626355 1740 /* rates available for this association, and for modulation mode */
eecd6e57 1741 rate_mask = rs_get_supported_rates(lq_sta, hdr, tbl->lq_type);
b481de9c
ZY
1742
1743 IWL_DEBUG_RATE("mask 0x%04X \n", rate_mask);
1744
1745 /* mask with station rate restriction */
1746 if (is_legacy(tbl->lq_type)) {
8318d78a 1747 if (lq_sta->band == IEEE80211_BAND_5GHZ)
77626355 1748 /* supp_rates has no CCK bits in A mode */
b481de9c 1749 rate_scale_index_msk = (u16) (rate_mask &
c33104f0 1750 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
b481de9c
ZY
1751 else
1752 rate_scale_index_msk = (u16) (rate_mask &
c33104f0 1753 lq_sta->supp_rates);
b481de9c
ZY
1754
1755 } else
1756 rate_scale_index_msk = rate_mask;
1757
1758 if (!rate_scale_index_msk)
1759 rate_scale_index_msk = rate_mask;
1760
07bc28ed
GC
1761 if (!((1 << index) & rate_scale_index_msk)) {
1762 IWL_ERROR("Current Rate is not valid\n");
1763 return;
b481de9c
ZY
1764 }
1765
77626355 1766 /* Get expected throughput table and history window for current rate */
07bc28ed
GC
1767 if (!tbl->expected_tpt) {
1768 IWL_ERROR("tbl->expected_tpt is NULL\n");
1769 return;
1770 }
b481de9c
ZY
1771
1772 window = &(tbl->win[index]);
1773
77626355
BC
1774 /*
1775 * If there is not enough history to calculate actual average
1776 * throughput, keep analyzing results of more tx frames, without
1777 * changing rate or mode (bypass most of the rest of this function).
1778 * Set up new rate table in uCode only if old rate is not supported
1779 * in current association (use new rate found above).
1780 */
b481de9c 1781 fail_count = window->counter - window->success_counter;
07bc28ed
GC
1782 if ((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
1783 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH)) {
1784 IWL_DEBUG_RATE("LQ: still below TH. succ=%d total=%d "
b481de9c
ZY
1785 "for index %d\n",
1786 window->success_counter, window->counter, index);
77626355
BC
1787
1788 /* Can't calculate this yet; not enough history */
b481de9c 1789 window->average_tpt = IWL_INVALID_VALUE;
77626355
BC
1790
1791 /* Should we stay with this modulation mode,
1792 * or search for a new one? */
c33104f0 1793 rs_stay_in_table(lq_sta);
77626355 1794
b481de9c
ZY
1795 goto out;
1796
77626355
BC
1797 /* Else we have enough samples; calculate estimate of
1798 * actual average throughput */
07bc28ed
GC
1799 } else {
1800 /*FIXME:RS remove this else if we don't get this error*/
1801 if (window->average_tpt != ((window->success_ratio *
1802 tbl->expected_tpt[index] + 64) / 128)) {
1803 IWL_ERROR("expected_tpt should have been calculated"
1804 " by now\n");
1805 window->average_tpt = ((window->success_ratio *
b481de9c 1806 tbl->expected_tpt[index] + 64) / 128);
07bc28ed
GC
1807 }
1808 }
b481de9c 1809
77626355 1810 /* If we are searching for better modulation mode, check success. */
c33104f0 1811 if (lq_sta->search_better_tbl) {
b481de9c 1812
77626355
BC
1813 /* If good success, continue using the "search" mode;
1814 * no need to send new link quality command, since we're
1815 * continuing to use the setup that we've been trying. */
07bc28ed
GC
1816 if (window->average_tpt > lq_sta->last_tpt) {
1817
1818 IWL_DEBUG_RATE("LQ: SWITCHING TO CURRENT TABLE "
1819 "suc=%d cur-tpt=%d old-tpt=%d\n",
1820 window->success_ratio,
1821 window->average_tpt,
1822 lq_sta->last_tpt);
1823
1824 if (!is_legacy(tbl->lq_type))
c33104f0 1825 lq_sta->enable_counter = 1;
07bc28ed 1826
77626355 1827 /* Swap tables; "search" becomes "active" */
c33104f0 1828 lq_sta->active_tbl = active_tbl;
b481de9c 1829 current_tpt = window->average_tpt;
77626355
BC
1830
1831 /* Else poor success; go back to mode in "active" table */
b481de9c 1832 } else {
07bc28ed
GC
1833
1834 IWL_DEBUG_RATE("LQ: GOING BACK TO THE OLD TABLE "
1835 "suc=%d cur-tpt=%d old-tpt=%d\n",
1836 window->success_ratio,
1837 window->average_tpt,
1838 lq_sta->last_tpt);
1839
77626355 1840 /* Nullify "search" table */
b481de9c 1841 tbl->lq_type = LQ_NONE;
77626355
BC
1842
1843 /* Revert to "active" table */
c33104f0
TW
1844 active_tbl = lq_sta->active_tbl;
1845 tbl = &(lq_sta->lq_info[active_tbl]);
b481de9c 1846
77626355 1847 /* Revert to "active" rate and throughput info */
17744ff6 1848 index = iwl4965_hwrate_to_plcp_idx(
39e88504 1849 tbl->current_rate);
c33104f0 1850 current_tpt = lq_sta->last_tpt;
b481de9c 1851
77626355 1852 /* Need to set up a new rate table in uCode */
b481de9c 1853 update_lq = 1;
b481de9c 1854 }
77626355
BC
1855
1856 /* Either way, we've made a decision; modulation mode
1857 * search is done, allow rate adjustment next time. */
c33104f0 1858 lq_sta->search_better_tbl = 0;
77626355 1859 done_search = 1; /* Don't switch modes below! */
b481de9c
ZY
1860 goto lq_update;
1861 }
1862
77626355
BC
1863 /* (Else) not in search of better modulation mode, try for better
1864 * starting rate, while staying in this mode. */
bf403db8 1865 high_low = rs_get_adjacent_rate(priv, index, rate_scale_index_msk,
b481de9c
ZY
1866 tbl->lq_type);
1867 low = high_low & 0xff;
1868 high = (high_low >> 8) & 0xff;
1869
77626355 1870 /* Collect measured throughputs for current and adjacent rates */
b481de9c 1871 current_tpt = window->average_tpt;
b481de9c
ZY
1872 if (low != IWL_RATE_INVALID)
1873 low_tpt = tbl->win[low].average_tpt;
b481de9c
ZY
1874 if (high != IWL_RATE_INVALID)
1875 high_tpt = tbl->win[high].average_tpt;
1876
77626355 1877 /* Assume rate increase */
b481de9c
ZY
1878 scale_action = 1;
1879
77626355 1880 /* Too many failures, decrease rate */
b481de9c
ZY
1881 if ((window->success_ratio <= IWL_RATE_DECREASE_TH) ||
1882 (current_tpt == 0)) {
1883 IWL_DEBUG_RATE("decrease rate because of low success_ratio\n");
1884 scale_action = -1;
77626355
BC
1885
1886 /* No throughput measured yet for adjacent rates; try increase. */
b481de9c
ZY
1887 } else if ((low_tpt == IWL_INVALID_VALUE) &&
1888 (high_tpt == IWL_INVALID_VALUE))
1889 scale_action = 1;
77626355
BC
1890
1891 /* Both adjacent throughputs are measured, but neither one has better
1892 * throughput; we're using the best rate, don't change it! */
b481de9c
ZY
1893 else if ((low_tpt != IWL_INVALID_VALUE) &&
1894 (high_tpt != IWL_INVALID_VALUE) &&
1895 (low_tpt < current_tpt) &&
1896 (high_tpt < current_tpt))
1897 scale_action = 0;
77626355
BC
1898
1899 /* At least one adjacent rate's throughput is measured,
1900 * and may have better performance. */
b481de9c 1901 else {
77626355 1902 /* Higher adjacent rate's throughput is measured */
b481de9c 1903 if (high_tpt != IWL_INVALID_VALUE) {
77626355 1904 /* Higher rate has better throughput */
b481de9c
ZY
1905 if (high_tpt > current_tpt)
1906 scale_action = 1;
1907 else {
1908 IWL_DEBUG_RATE
1909 ("decrease rate because of high tpt\n");
1910 scale_action = -1;
1911 }
77626355
BC
1912
1913 /* Lower adjacent rate's throughput is measured */
b481de9c 1914 } else if (low_tpt != IWL_INVALID_VALUE) {
77626355 1915 /* Lower rate has better throughput */
b481de9c
ZY
1916 if (low_tpt > current_tpt) {
1917 IWL_DEBUG_RATE
1918 ("decrease rate because of low tpt\n");
1919 scale_action = -1;
1920 } else
1921 scale_action = 1;
1922 }
1923 }
1924
77626355
BC
1925 /* Sanity check; asked for decrease, but success rate or throughput
1926 * has been good at old rate. Don't change it. */
b481de9c
ZY
1927 if (scale_action == -1) {
1928 if ((low != IWL_RATE_INVALID) &&
1929 ((window->success_ratio > IWL_RATE_HIGH_TH) ||
1930 (current_tpt > (100 * tbl->expected_tpt[low]))))
1931 scale_action = 0;
77626355
BC
1932
1933 /* Sanity check; asked for increase, but success rate has not been great
1934 * even at old rate, higher rate will be worse. Don't change it. */
b481de9c
ZY
1935 } else if ((scale_action == 1) &&
1936 (window->success_ratio < IWL_RATE_INCREASE_TH))
1937 scale_action = 0;
1938
1939 switch (scale_action) {
1940 case -1:
77626355 1941 /* Decrease starting rate, update uCode's rate table */
b481de9c
ZY
1942 if (low != IWL_RATE_INVALID) {
1943 update_lq = 1;
1944 index = low;
1945 }
1946 break;
1947 case 1:
77626355 1948 /* Increase starting rate, update uCode's rate table */
b481de9c
ZY
1949 if (high != IWL_RATE_INVALID) {
1950 update_lq = 1;
1951 index = high;
1952 }
1953
1954 break;
1955 case 0:
77626355 1956 /* No change */
b481de9c
ZY
1957 default:
1958 break;
1959 }
1960
eecd6e57 1961 IWL_DEBUG_RATE("choose rate scale index %d action %d low %d "
b481de9c
ZY
1962 "high %d type %d\n",
1963 index, scale_action, low, high, tbl->lq_type);
1964
1965 lq_update:
77626355 1966 /* Replace uCode's rate table for the destination station. */
b481de9c 1967 if (update_lq) {
39e88504 1968 rate = rate_n_flags_from_tbl(tbl, index, is_green);
07bc28ed 1969 rs_fill_link_cmd(priv, lq_sta, rate);
66c73db7 1970 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
b481de9c 1971 }
77626355
BC
1972
1973 /* Should we stay with this modulation mode, or search for a new one? */
c33104f0 1974 rs_stay_in_table(lq_sta);
b481de9c 1975
77626355
BC
1976 /*
1977 * Search for new modulation mode if we're:
1978 * 1) Not changing rates right now
1979 * 2) Not just finishing up a search
1980 * 3) Allowing a new search
1981 */
c33104f0 1982 if (!update_lq && !done_search && !lq_sta->stay_in_tbl) {
77626355 1983 /* Save current throughput to compare with "search" throughput*/
c33104f0 1984 lq_sta->last_tpt = current_tpt;
b481de9c 1985
77626355
BC
1986 /* Select a new "search" modulation mode to try.
1987 * If one is found, set up the new "search" table. */
b481de9c 1988 if (is_legacy(tbl->lq_type))
c33104f0 1989 rs_move_legacy_other(priv, lq_sta, conf, sta, index);
b481de9c 1990 else if (is_siso(tbl->lq_type))
c33104f0 1991 rs_move_siso_to_other(priv, lq_sta, conf, sta, index);
b481de9c 1992 else
c33104f0 1993 rs_move_mimo_to_other(priv, lq_sta, conf, sta, index);
b481de9c 1994
77626355 1995 /* If new "search" mode was selected, set up in uCode table */
c33104f0 1996 if (lq_sta->search_better_tbl) {
77626355 1997 /* Access the "search" table, clear its history. */
c33104f0 1998 tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
b481de9c
ZY
1999 for (i = 0; i < IWL_RATE_COUNT; i++)
2000 rs_rate_scale_clear_window(&(tbl->win[i]));
2001
77626355 2002 /* Use new "search" start rate */
17744ff6 2003 index = iwl4965_hwrate_to_plcp_idx(
39e88504 2004 tbl->current_rate);
b481de9c 2005
eecd6e57 2006 IWL_DEBUG_RATE("Switch current mcs: %X index: %d\n",
39e88504 2007 tbl->current_rate, index);
07bc28ed 2008 rs_fill_link_cmd(priv, lq_sta, tbl->current_rate);
66c73db7 2009 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
b481de9c 2010 }
b481de9c 2011
77626355
BC
2012 /* If the "active" (non-search) mode was legacy,
2013 * and we've tried switching antennas,
2014 * but we haven't been able to try HT modes (not available),
2015 * stay with best antenna legacy modulation for a while
2016 * before next round of mode comparisons. */
c33104f0 2017 tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
b481de9c 2018 if (is_legacy(tbl1->lq_type) &&
c8b0e6e1 2019#ifdef CONFIG_IWL4965_HT
270243a5 2020 (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE)) &&
b481de9c 2021#endif
c33104f0
TW
2022 (lq_sta->action_counter >= 1)) {
2023 lq_sta->action_counter = 0;
eecd6e57 2024 IWL_DEBUG_RATE("LQ: STAY in legacy table\n");
bf403db8 2025 rs_set_stay_in_table(priv, 1, lq_sta);
b481de9c
ZY
2026 }
2027
77626355
BC
2028 /* If we're in an HT mode, and all 3 mode switch actions
2029 * have been tried and compared, stay in this best modulation
2030 * mode for a while before next round of mode comparisons. */
c33104f0
TW
2031 if (lq_sta->enable_counter &&
2032 (lq_sta->action_counter >= IWL_ACTION_LIMIT)) {
0c11b4de
RR
2033#ifdef CONFIG_IWL4965_HT
2034 if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
2035 (lq_sta->tx_agg_tid_en & (1 << tid)) &&
2036 (tid != MAX_TID_COUNT)) {
eecd6e57 2037 IWL_DEBUG_RATE("try to aggregate tid %d\n", tid);
0c11b4de 2038 rs_tl_turn_on_agg(priv, tid, lq_sta, sta);
b481de9c 2039 }
0c11b4de 2040#endif /*CONFIG_IWL4965_HT */
c33104f0 2041 lq_sta->action_counter = 0;
bf403db8 2042 rs_set_stay_in_table(priv, 0, lq_sta);
b481de9c 2043 }
77626355
BC
2044
2045 /*
2046 * Else, don't search for a new modulation mode.
2047 * Put new timestamp in stay-in-modulation-mode flush timer if:
2048 * 1) Not changing rates right now
2049 * 2) Not just finishing up a search
2050 * 3) flush timer is empty
2051 */
b481de9c 2052 } else {
c33104f0
TW
2053 if ((!update_lq) && (!done_search) && (!lq_sta->flush_timer))
2054 lq_sta->flush_timer = jiffies;
b481de9c
ZY
2055 }
2056
2057out:
39e88504 2058 tbl->current_rate = rate_n_flags_from_tbl(tbl, index, is_green);
b481de9c 2059 i = index;
8318d78a 2060 sta->last_txrate_idx = i;
b481de9c 2061
8318d78a 2062 /* sta->txrate_idx is an index to A mode rates which start
b481de9c
ZY
2063 * at IWL_FIRST_OFDM_RATE
2064 */
8318d78a
JB
2065 if (lq_sta->band == IEEE80211_BAND_5GHZ)
2066 sta->txrate_idx = i - IWL_FIRST_OFDM_RATE;
b481de9c 2067 else
8318d78a 2068 sta->txrate_idx = i;
b481de9c
ZY
2069
2070 return;
2071}
2072
2073
c79dd5b5 2074static void rs_initialize_lq(struct iwl_priv *priv,
270243a5 2075 struct ieee80211_conf *conf,
b481de9c
ZY
2076 struct sta_info *sta)
2077{
c33104f0 2078 struct iwl4965_lq_sta *lq_sta;
bb8c093b 2079 struct iwl4965_scale_tbl_info *tbl;
b481de9c 2080 int rate_idx;
aade00ce 2081 int i;
39e88504 2082 u32 rate;
aade00ce
GC
2083 u8 use_green = rs_use_green(priv, conf);
2084 u8 active_tbl = 0;
2085 u8 valid_tx_ant;
b481de9c
ZY
2086
2087 if (!sta || !sta->rate_ctrl_priv)
2088 goto out;
2089
c33104f0 2090 lq_sta = (struct iwl4965_lq_sta *)sta->rate_ctrl_priv;
8318d78a 2091 i = sta->last_txrate_idx;
b481de9c 2092
c33104f0 2093 if ((lq_sta->lq.sta_id == 0xff) &&
b481de9c
ZY
2094 (priv->iw_mode == IEEE80211_IF_TYPE_IBSS))
2095 goto out;
2096
aade00ce
GC
2097 valid_tx_ant = priv->hw_params.valid_tx_ant;
2098
c33104f0
TW
2099 if (!lq_sta->search_better_tbl)
2100 active_tbl = lq_sta->active_tbl;
b481de9c 2101 else
c33104f0 2102 active_tbl = 1 - lq_sta->active_tbl;
b481de9c 2103
c33104f0 2104 tbl = &(lq_sta->lq_info[active_tbl]);
b481de9c
ZY
2105
2106 if ((i < 0) || (i >= IWL_RATE_COUNT))
2107 i = 0;
2108
fde0db31 2109 /* FIXME:RS: This is also wrong in 4965 */
1826dcc0 2110 rate = iwl_rates[i].plcp;
39e88504
GC
2111 rate |= RATE_MCS_ANT_B_MSK;
2112 rate &= ~RATE_MCS_ANT_A_MSK;
b481de9c
ZY
2113
2114 if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
39e88504 2115 rate |= RATE_MCS_CCK_MSK;
b481de9c 2116
fde0db31 2117 tbl->ant_type = ANT_B;
39e88504 2118 rs_get_tbl_info_from_mcs(rate, priv->band, tbl, &rate_idx);
aade00ce
GC
2119 if (!rs_is_valid_ant(valid_tx_ant, tbl->ant_type))
2120 rs_toggle_antenna(valid_tx_ant, &rate, tbl);
b481de9c 2121
39e88504
GC
2122 rate = rate_n_flags_from_tbl(tbl, rate_idx, use_green);
2123 tbl->current_rate = rate;
eecd6e57 2124 rs_set_expected_tpt_table(lq_sta, tbl);
07bc28ed 2125 rs_fill_link_cmd(NULL, lq_sta, rate);
66c73db7 2126 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
b481de9c
ZY
2127 out:
2128 return;
2129}
2130
1abbe498 2131static void rs_get_rate(void *priv_rate, struct net_device *dev,
8318d78a
JB
2132 struct ieee80211_supported_band *sband,
2133 struct sk_buff *skb,
1abbe498 2134 struct rate_selection *sel)
b481de9c
ZY
2135{
2136
2137 int i;
2138 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
270243a5 2139 struct ieee80211_conf *conf = &local->hw.conf;
b481de9c
ZY
2140 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2141 struct sta_info *sta;
2bc454b0 2142 u16 fc;
c79dd5b5 2143 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
c33104f0 2144 struct iwl4965_lq_sta *lq_sta;
b481de9c 2145
58826351 2146 IWL_DEBUG_RATE_LIMIT("rate scale calculate new rate for skb\n");
b481de9c 2147
d0709a65
JB
2148 rcu_read_lock();
2149
b481de9c
ZY
2150 sta = sta_info_get(local, hdr->addr1);
2151
2bc454b0
MW
2152 /* Send management frames and broadcast/multicast data using lowest
2153 * rate. */
2154 fc = le16_to_cpu(hdr->frame_control);
2155 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1) ||
2156 !sta || !sta->rate_ctrl_priv) {
8318d78a 2157 sel->rate = rate_lowest(local, sband, sta);
f4d6082d 2158 goto out;
b481de9c
ZY
2159 }
2160
c33104f0 2161 lq_sta = (struct iwl4965_lq_sta *)sta->rate_ctrl_priv;
8318d78a 2162 i = sta->last_txrate_idx;
b481de9c 2163
c33104f0
TW
2164 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
2165 !lq_sta->ibss_sta_added) {
947b13a7 2166 u8 sta_id = iwl_find_station(priv, hdr->addr1);
0795af57 2167 DECLARE_MAC_BUF(mac);
b481de9c
ZY
2168
2169 if (sta_id == IWL_INVALID_STATION) {
0795af57
JP
2170 IWL_DEBUG_RATE("LQ: ADD station %s\n",
2171 print_mac(mac, hdr->addr1));
4f40e4d9 2172 sta_id = iwl_add_station_flags(priv, hdr->addr1,
67d62035 2173 0, CMD_ASYNC, NULL);
b481de9c
ZY
2174 }
2175 if ((sta_id != IWL_INVALID_STATION)) {
c33104f0
TW
2176 lq_sta->lq.sta_id = sta_id;
2177 lq_sta->lq.rs_table[0].rate_n_flags = 0;
2178 lq_sta->ibss_sta_added = 1;
270243a5 2179 rs_initialize_lq(priv, conf, sta);
b481de9c 2180 }
c33104f0 2181 if (!lq_sta->ibss_sta_added)
b481de9c
ZY
2182 goto done;
2183 }
2184
f4d6082d 2185done:
1abbe498 2186 if ((i < 0) || (i > IWL_RATE_COUNT)) {
8318d78a 2187 sel->rate = rate_lowest(local, sband, sta);
f4d6082d 2188 goto out;
1abbe498 2189 }
b481de9c 2190
1abbe498 2191 sel->rate = &priv->ieee_rates[i];
f4d6082d
TW
2192out:
2193 rcu_read_unlock();
b481de9c
ZY
2194}
2195
bf403db8 2196static void *rs_alloc_sta(void *priv_rate, gfp_t gfp)
b481de9c 2197{
c33104f0 2198 struct iwl4965_lq_sta *lq_sta;
bf403db8 2199 struct iwl_priv *priv;
b481de9c
ZY
2200 int i, j;
2201
bf403db8 2202 priv = (struct iwl_priv *)priv_rate;
b481de9c
ZY
2203 IWL_DEBUG_RATE("create station rate scale window\n");
2204
c33104f0 2205 lq_sta = kzalloc(sizeof(struct iwl4965_lq_sta), gfp);
b481de9c 2206
c33104f0 2207 if (lq_sta == NULL)
b481de9c 2208 return NULL;
c33104f0 2209 lq_sta->lq.sta_id = 0xff;
b481de9c 2210
63fddb9f 2211
b481de9c
ZY
2212 for (j = 0; j < LQ_SIZE; j++)
2213 for (i = 0; i < IWL_RATE_COUNT; i++)
c33104f0 2214 rs_rate_scale_clear_window(&(lq_sta->lq_info[j].win[i]));
b481de9c 2215
c33104f0 2216 return lq_sta;
b481de9c
ZY
2217}
2218
2219static void rs_rate_init(void *priv_rate, void *priv_sta,
2220 struct ieee80211_local *local,
2221 struct sta_info *sta)
2222{
2223 int i, j;
270243a5 2224 struct ieee80211_conf *conf = &local->hw.conf;
8318d78a 2225 struct ieee80211_supported_band *sband;
c79dd5b5 2226 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
c33104f0 2227 struct iwl4965_lq_sta *lq_sta = priv_sta;
b481de9c 2228
8318d78a
JB
2229 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2230
c33104f0 2231 lq_sta->flush_timer = 0;
8318d78a
JB
2232 lq_sta->supp_rates = sta->supp_rates[sband->band];
2233 sta->txrate_idx = 3;
b481de9c
ZY
2234 for (j = 0; j < LQ_SIZE; j++)
2235 for (i = 0; i < IWL_RATE_COUNT; i++)
c33104f0 2236 rs_rate_scale_clear_window(&(lq_sta->lq_info[j].win[i]));
b481de9c 2237
07bc28ed 2238 IWL_DEBUG_RATE("LQ: *** rate scale global init ***\n");
b481de9c
ZY
2239 /* TODO: what is a good starting rate for STA? About middle? Maybe not
2240 * the lowest or the highest rate.. Could consider using RSSI from
2241 * previous packets? Need to have IEEE 802.1X auth succeed immediately
2242 * after assoc.. */
2243
c33104f0 2244 lq_sta->ibss_sta_added = 0;
b481de9c 2245 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
947b13a7 2246 u8 sta_id = iwl_find_station(priv, sta->addr);
0795af57
JP
2247 DECLARE_MAC_BUF(mac);
2248
b481de9c 2249 /* for IBSS the call are from tasklet */
eecd6e57 2250 IWL_DEBUG_RATE("LQ: ADD station %s\n",
0795af57 2251 print_mac(mac, sta->addr));
b481de9c
ZY
2252
2253 if (sta_id == IWL_INVALID_STATION) {
0795af57
JP
2254 IWL_DEBUG_RATE("LQ: ADD station %s\n",
2255 print_mac(mac, sta->addr));
4f40e4d9 2256 sta_id = iwl_add_station_flags(priv, sta->addr,
67d62035 2257 0, CMD_ASYNC, NULL);
b481de9c
ZY
2258 }
2259 if ((sta_id != IWL_INVALID_STATION)) {
c33104f0
TW
2260 lq_sta->lq.sta_id = sta_id;
2261 lq_sta->lq.rs_table[0].rate_n_flags = 0;
b481de9c
ZY
2262 }
2263 /* FIXME: this is w/a remove it later */
2264 priv->assoc_station_added = 1;
2265 }
2266
77626355 2267 /* Find highest tx rate supported by hardware and destination station */
8318d78a
JB
2268 for (i = 0; i < sband->n_bitrates; i++)
2269 if (sta->supp_rates[sband->band] & BIT(i))
2270 sta->txrate_idx = i;
2271
2272 sta->last_txrate_idx = sta->txrate_idx;
2273 /* WTF is with this bogus comment? A doesn't have cck rates */
77626355 2274 /* For MODE_IEEE80211A, cck rates are at end of rate table */
8318d78a
JB
2275 if (local->hw.conf.channel->band == IEEE80211_BAND_5GHZ)
2276 sta->last_txrate_idx += IWL_FIRST_OFDM_RATE;
b481de9c 2277
c33104f0 2278 lq_sta->is_dup = 0;
c33104f0 2279 lq_sta->is_green = rs_use_green(priv, conf);
39e88504 2280 lq_sta->active_legacy_rate = priv->active_rate & ~(0x1000);
c33104f0 2281 lq_sta->active_rate_basic = priv->active_rate_basic;
8318d78a 2282 lq_sta->band = priv->band;
c8b0e6e1 2283#ifdef CONFIG_IWL4965_HT
77626355
BC
2284 /*
2285 * active_siso_rate mask includes 9 MBits (bit 5), and CCK (bits 0-3),
2286 * supp_rates[] does not; shift to convert format, force 9 MBits off.
2287 */
fde0db31
GC
2288 lq_sta->active_siso_rate =
2289 priv->current_ht_config.supp_mcs_set[0] << 1;
c33104f0 2290 lq_sta->active_siso_rate |=
fde0db31 2291 priv->current_ht_config.supp_mcs_set[0] & 0x1;
c33104f0 2292 lq_sta->active_siso_rate &= ~((u16)0x2);
fde0db31 2293 lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE;
b481de9c 2294
77626355 2295 /* Same here */
fde0db31
GC
2296 lq_sta->active_mimo2_rate =
2297 priv->current_ht_config.supp_mcs_set[1] << 1;
2298 lq_sta->active_mimo2_rate |=
2299 priv->current_ht_config.supp_mcs_set[1] & 0x1;
2300 lq_sta->active_mimo2_rate &= ~((u16)0x2);
2301 lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE;
2302
2303 lq_sta->active_mimo3_rate =
2304 priv->current_ht_config.supp_mcs_set[2] << 1;
2305 lq_sta->active_mimo3_rate |=
2306 priv->current_ht_config.supp_mcs_set[2] & 0x1;
2307 lq_sta->active_mimo3_rate &= ~((u16)0x2);
2308 lq_sta->active_mimo3_rate <<= IWL_FIRST_OFDM_RATE;
2309
07bc28ed 2310 IWL_DEBUG_RATE("SISO-RATE=%X MIMO2-RATE=%X MIMO3-RATE=%X\n",
c33104f0 2311 lq_sta->active_siso_rate,
fde0db31
GC
2312 lq_sta->active_mimo2_rate,
2313 lq_sta->active_mimo3_rate);
2314
07bc28ed
GC
2315 /* These values will be overriden later */
2316 lq_sta->lq.general_params.single_stream_ant_msk = ANT_A;
2317 lq_sta->lq.general_params.dual_stream_ant_msk = ANT_AB;
2318
0c11b4de
RR
2319 /* as default allow aggregation for all tids */
2320 lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
c8b0e6e1 2321#endif /*CONFIG_IWL4965_HT*/
98d7e09a 2322#ifdef CONFIG_MAC80211_DEBUGFS
c33104f0 2323 lq_sta->drv = priv;
98d7e09a 2324#endif
b481de9c
ZY
2325
2326 if (priv->assoc_station_added)
2327 priv->lq_mngr.lq_ready = 1;
2328
270243a5 2329 rs_initialize_lq(priv, conf, sta);
b481de9c
ZY
2330}
2331
fde0db31
GC
2332static void rs_fill_link_cmd(const struct iwl_priv *priv,
2333 struct iwl4965_lq_sta *lq_sta,
07bc28ed 2334 u32 new_rate)
b481de9c 2335{
aade00ce 2336 struct iwl4965_scale_tbl_info tbl_type;
b481de9c 2337 int index = 0;
b481de9c 2338 int rate_idx;
1b696de2 2339 int repeat_rate = 0;
aade00ce 2340 u8 ant_toggle_cnt = 0;
b481de9c 2341 u8 use_ht_possible = 1;
aade00ce 2342 u8 valid_tx_ant = 0;
07bc28ed 2343 struct iwl_link_quality_cmd *lq_cmd = &lq_sta->lq;
b481de9c 2344
77626355 2345 /* Override starting rate (index 0) if needed for debug purposes */
39e88504 2346 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
98d7e09a 2347
39e88504 2348 /* Interpret new_rate (rate_n_flags) */
aade00ce 2349 memset(&tbl_type, 0, sizeof(tbl_type));
39e88504 2350 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band,
b481de9c
ZY
2351 &tbl_type, &rate_idx);
2352
77626355 2353 /* How many times should we repeat the initial rate? */
b481de9c 2354 if (is_legacy(tbl_type.lq_type)) {
aade00ce 2355 ant_toggle_cnt = 1;
1b696de2 2356 repeat_rate = IWL_NUMBER_TRY;
aade00ce 2357 } else {
1b696de2 2358 repeat_rate = IWL_HT_NUMBER_TRY;
aade00ce 2359 }
b481de9c
ZY
2360
2361 lq_cmd->general_params.mimo_delimiter =
2362 is_mimo(tbl_type.lq_type) ? 1 : 0;
77626355
BC
2363
2364 /* Fill 1st table entry (index 0) */
39e88504 2365 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
b481de9c 2366
07bc28ed
GC
2367 if (num_of_ant(tbl_type.ant_type) == 1) {
2368 lq_cmd->general_params.single_stream_ant_msk =
2369 tbl_type.ant_type;
2370 } else if (num_of_ant(tbl_type.ant_type) == 2) {
2371 lq_cmd->general_params.dual_stream_ant_msk =
2372 tbl_type.ant_type;
2373 } /* otherwise we don't modify the existing value */
b481de9c
ZY
2374
2375 index++;
1b696de2 2376 repeat_rate--;
b481de9c 2377
aade00ce
GC
2378 if (priv)
2379 valid_tx_ant = priv->hw_params.valid_tx_ant;
2380
77626355 2381 /* Fill rest of rate table */
b481de9c 2382 while (index < LINK_QUAL_MAX_RETRY_NUM) {
77626355
BC
2383 /* Repeat initial/next rate.
2384 * For legacy IWL_NUMBER_TRY == 1, this loop will not execute.
2385 * For HT IWL_HT_NUMBER_TRY == 3, this executes twice. */
1b696de2 2386 while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
b481de9c 2387 if (is_legacy(tbl_type.lq_type)) {
aade00ce
GC
2388 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2389 ant_toggle_cnt++;
2390 else if (priv &&
2391 rs_toggle_antenna(valid_tx_ant,
2392 &new_rate, &tbl_type))
2393 ant_toggle_cnt = 1;
2394}
98d7e09a 2395
77626355 2396 /* Override next rate if needed for debug purposes */
c33104f0 2397 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
77626355
BC
2398
2399 /* Fill next table entry */
b481de9c 2400 lq_cmd->rs_table[index].rate_n_flags =
39e88504 2401 cpu_to_le32(new_rate);
1b696de2 2402 repeat_rate--;
b481de9c
ZY
2403 index++;
2404 }
2405
39e88504 2406 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band, &tbl_type,
b481de9c
ZY
2407 &rate_idx);
2408
77626355
BC
2409 /* Indicate to uCode which entries might be MIMO.
2410 * If initial rate was MIMO, this will finally end up
2411 * as (IWL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
b481de9c
ZY
2412 if (is_mimo(tbl_type.lq_type))
2413 lq_cmd->general_params.mimo_delimiter = index;
2414
77626355 2415 /* Get next rate */
39e88504
GC
2416 new_rate = rs_get_lower_rate(lq_sta, &tbl_type, rate_idx,
2417 use_ht_possible);
b481de9c 2418
77626355 2419 /* How many times should we repeat the next rate? */
b481de9c 2420 if (is_legacy(tbl_type.lq_type)) {
aade00ce
GC
2421 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2422 ant_toggle_cnt++;
2423 else if (priv &&
2424 rs_toggle_antenna(valid_tx_ant,
2425 &new_rate, &tbl_type))
2426 ant_toggle_cnt = 1;
2427
1b696de2 2428 repeat_rate = IWL_NUMBER_TRY;
aade00ce 2429 } else {
1b696de2 2430 repeat_rate = IWL_HT_NUMBER_TRY;
aade00ce 2431 }
b481de9c 2432
77626355
BC
2433 /* Don't allow HT rates after next pass.
2434 * rs_get_lower_rate() will change type to LQ_A or LQ_G. */
b481de9c
ZY
2435 use_ht_possible = 0;
2436
77626355 2437 /* Override next rate if needed for debug purposes */
c33104f0 2438 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
77626355
BC
2439
2440 /* Fill next table entry */
39e88504 2441 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
b481de9c
ZY
2442
2443 index++;
1b696de2 2444 repeat_rate--;
b481de9c
ZY
2445 }
2446
b481de9c
ZY
2447 lq_cmd->agg_params.agg_dis_start_th = 3;
2448 lq_cmd->agg_params.agg_time_limit = cpu_to_le16(4000);
b481de9c
ZY
2449}
2450
2451static void *rs_alloc(struct ieee80211_local *local)
2452{
2453 return local->hw.priv;
2454}
2455/* rate scale requires free function to be implemented */
2456static void rs_free(void *priv_rate)
2457{
2458 return;
2459}
2460
2461static void rs_clear(void *priv_rate)
2462{
c79dd5b5 2463 struct iwl_priv *priv = (struct iwl_priv *) priv_rate;
b481de9c
ZY
2464
2465 IWL_DEBUG_RATE("enter\n");
2466
2467 priv->lq_mngr.lq_ready = 0;
b481de9c
ZY
2468
2469 IWL_DEBUG_RATE("leave\n");
2470}
2471
bf403db8 2472static void rs_free_sta(void *priv_rate, void *priv_sta)
b481de9c 2473{
c33104f0 2474 struct iwl4965_lq_sta *lq_sta = priv_sta;
bf403db8 2475 struct iwl_priv *priv;
b481de9c 2476
bf403db8 2477 priv = (struct iwl_priv *)priv_rate;
b481de9c 2478 IWL_DEBUG_RATE("enter\n");
c33104f0 2479 kfree(lq_sta);
b481de9c
ZY
2480 IWL_DEBUG_RATE("leave\n");
2481}
2482
2483
93dc646a 2484#ifdef CONFIG_MAC80211_DEBUGFS
5ae212c9
ZY
2485static int open_file_generic(struct inode *inode, struct file *file)
2486{
2487 file->private_data = inode->i_private;
2488 return 0;
2489}
c33104f0 2490static void rs_dbgfs_set_mcs(struct iwl4965_lq_sta *lq_sta,
39e88504 2491 u32 *rate_n_flags, int index)
98d7e09a 2492{
bf403db8
EK
2493 struct iwl_priv *priv;
2494
2495 priv = lq_sta->drv;
39e88504
GC
2496 if (lq_sta->dbg_fixed_rate) {
2497 if (index < 12) {
2498 *rate_n_flags = lq_sta->dbg_fixed_rate;
2499 } else {
2500 if (lq_sta->band == IEEE80211_BAND_5GHZ)
2501 *rate_n_flags = 0x800D;
2502 else
2503 *rate_n_flags = 0x820A;
2504 }
98d7e09a 2505 IWL_DEBUG_RATE("Fixed rate ON\n");
39e88504
GC
2506 } else {
2507 IWL_DEBUG_RATE("Fixed rate OFF\n");
98d7e09a 2508 }
98d7e09a 2509}
5ae212c9 2510
98d7e09a
ZY
2511static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
2512 const char __user *user_buf, size_t count, loff_t *ppos)
2513{
c33104f0 2514 struct iwl4965_lq_sta *lq_sta = file->private_data;
bf403db8 2515 struct iwl_priv *priv;
98d7e09a
ZY
2516 char buf[64];
2517 int buf_size;
2518 u32 parsed_rate;
2519
bf403db8 2520 priv = lq_sta->drv;
98d7e09a
ZY
2521 memset(buf, 0, sizeof(buf));
2522 buf_size = min(count, sizeof(buf) - 1);
2523 if (copy_from_user(buf, user_buf, buf_size))
2524 return -EFAULT;
2525
2526 if (sscanf(buf, "%x", &parsed_rate) == 1)
39e88504 2527 lq_sta->dbg_fixed_rate = parsed_rate;
98d7e09a 2528 else
39e88504 2529 lq_sta->dbg_fixed_rate = 0;
98d7e09a 2530
39e88504
GC
2531 lq_sta->active_legacy_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
2532 lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2533 lq_sta->active_mimo2_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2534 lq_sta->active_mimo3_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
98d7e09a
ZY
2535
2536 IWL_DEBUG_RATE("sta_id %d rate 0x%X\n",
39e88504 2537 lq_sta->lq.sta_id, lq_sta->dbg_fixed_rate);
98d7e09a 2538
39e88504 2539 if (lq_sta->dbg_fixed_rate) {
07bc28ed 2540 rs_fill_link_cmd(NULL, lq_sta, lq_sta->dbg_fixed_rate);
66c73db7 2541 iwl_send_lq_cmd(lq_sta->drv, &lq_sta->lq, CMD_ASYNC);
98d7e09a
ZY
2542 }
2543
2544 return count;
2545}
0209dc11 2546
5ae212c9
ZY
2547static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
2548 char __user *user_buf, size_t count, loff_t *ppos)
2549{
2550 char buff[1024];
2551 int desc = 0;
2552 int i = 0;
2553
c33104f0 2554 struct iwl4965_lq_sta *lq_sta = file->private_data;
5ae212c9 2555
c33104f0 2556 desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
98d7e09a 2557 desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
c33104f0 2558 lq_sta->total_failed, lq_sta->total_success,
39e88504 2559 lq_sta->active_legacy_rate);
98d7e09a 2560 desc += sprintf(buff+desc, "fixed rate 0x%X\n",
39e88504 2561 lq_sta->dbg_fixed_rate);
5ae212c9
ZY
2562 desc += sprintf(buff+desc, "general:"
2563 "flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
c33104f0
TW
2564 lq_sta->lq.general_params.flags,
2565 lq_sta->lq.general_params.mimo_delimiter,
2566 lq_sta->lq.general_params.single_stream_ant_msk,
2567 lq_sta->lq.general_params.dual_stream_ant_msk);
5ae212c9
ZY
2568
2569 desc += sprintf(buff+desc, "agg:"
2570 "time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
c33104f0
TW
2571 le16_to_cpu(lq_sta->lq.agg_params.agg_time_limit),
2572 lq_sta->lq.agg_params.agg_dis_start_th,
2573 lq_sta->lq.agg_params.agg_frame_cnt_limit);
5ae212c9
ZY
2574
2575 desc += sprintf(buff+desc,
2576 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
c33104f0
TW
2577 lq_sta->lq.general_params.start_rate_index[0],
2578 lq_sta->lq.general_params.start_rate_index[1],
2579 lq_sta->lq.general_params.start_rate_index[2],
2580 lq_sta->lq.general_params.start_rate_index[3]);
5ae212c9
ZY
2581
2582
2583 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
2584 desc += sprintf(buff+desc, " rate[%d] 0x%X\n",
c33104f0 2585 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags));
5ae212c9
ZY
2586
2587 return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2588}
2589
2590static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
98d7e09a 2591 .write = rs_sta_dbgfs_scale_table_write,
5ae212c9
ZY
2592 .read = rs_sta_dbgfs_scale_table_read,
2593 .open = open_file_generic,
2594};
0209dc11
ZY
2595static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
2596 char __user *user_buf, size_t count, loff_t *ppos)
2597{
2598 char buff[1024];
2599 int desc = 0;
2600 int i, j;
2601
c33104f0 2602 struct iwl4965_lq_sta *lq_sta = file->private_data;
0209dc11
ZY
2603 for (i = 0; i < LQ_SIZE; i++) {
2604 desc += sprintf(buff+desc, "%s type=%d SGI=%d FAT=%d DUP=%d\n"
2605 "rate=0x%X\n",
c33104f0
TW
2606 lq_sta->active_tbl == i?"*":"x",
2607 lq_sta->lq_info[i].lq_type,
2608 lq_sta->lq_info[i].is_SGI,
2609 lq_sta->lq_info[i].is_fat,
2610 lq_sta->lq_info[i].is_dup,
39e88504 2611 lq_sta->lq_info[i].current_rate);
0209dc11
ZY
2612 for (j = 0; j < IWL_RATE_COUNT; j++) {
2613 desc += sprintf(buff+desc,
c33104f0
TW
2614 "counter=%d success=%d %%=%d\n",
2615 lq_sta->lq_info[i].win[j].counter,
2616 lq_sta->lq_info[i].win[j].success_counter,
2617 lq_sta->lq_info[i].win[j].success_ratio);
0209dc11
ZY
2618 }
2619 }
2620 return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2621}
2622
2623static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
2624 .read = rs_sta_dbgfs_stats_table_read,
2625 .open = open_file_generic,
2626};
5ae212c9 2627
93dc646a
ZY
2628static void rs_add_debugfs(void *priv, void *priv_sta,
2629 struct dentry *dir)
2630{
c33104f0
TW
2631 struct iwl4965_lq_sta *lq_sta = priv_sta;
2632 lq_sta->rs_sta_dbgfs_scale_table_file =
0209dc11 2633 debugfs_create_file("rate_scale_table", 0600, dir,
c33104f0
TW
2634 lq_sta, &rs_sta_dbgfs_scale_table_ops);
2635 lq_sta->rs_sta_dbgfs_stats_table_file =
0209dc11 2636 debugfs_create_file("rate_stats_table", 0600, dir,
c33104f0 2637 lq_sta, &rs_sta_dbgfs_stats_table_ops);
0c11b4de
RR
2638#ifdef CONFIG_IWL4965_HT
2639 lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file =
2640 debugfs_create_u8("tx_agg_tid_enable", 0600, dir,
2641 &lq_sta->tx_agg_tid_en);
2642#endif
2643
93dc646a
ZY
2644}
2645
2646static void rs_remove_debugfs(void *priv, void *priv_sta)
2647{
c33104f0
TW
2648 struct iwl4965_lq_sta *lq_sta = priv_sta;
2649 debugfs_remove(lq_sta->rs_sta_dbgfs_scale_table_file);
2650 debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
0c11b4de
RR
2651#ifdef CONFIG_IWL4965_HT
2652 debugfs_remove(lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file);
2653#endif
93dc646a
ZY
2654}
2655#endif
2656
b481de9c
ZY
2657static struct rate_control_ops rs_ops = {
2658 .module = NULL,
2659 .name = RS_NAME,
2660 .tx_status = rs_tx_status,
2661 .get_rate = rs_get_rate,
2662 .rate_init = rs_rate_init,
2663 .clear = rs_clear,
2664 .alloc = rs_alloc,
2665 .free = rs_free,
2666 .alloc_sta = rs_alloc_sta,
2667 .free_sta = rs_free_sta,
93dc646a
ZY
2668#ifdef CONFIG_MAC80211_DEBUGFS
2669 .add_sta_debugfs = rs_add_debugfs,
2670 .remove_sta_debugfs = rs_remove_debugfs,
2671#endif
b481de9c
ZY
2672};
2673
bb8c093b 2674int iwl4965_fill_rs_info(struct ieee80211_hw *hw, char *buf, u8 sta_id)
b481de9c
ZY
2675{
2676 struct ieee80211_local *local = hw_to_local(hw);
c79dd5b5 2677 struct iwl_priv *priv = hw->priv;
c33104f0 2678 struct iwl4965_lq_sta *lq_sta;
b481de9c 2679 struct sta_info *sta;
c33104f0 2680 int cnt = 0, i;
b481de9c
ZY
2681 u32 samples = 0, success = 0, good = 0;
2682 unsigned long now = jiffies;
2683 u32 max_time = 0;
2684 u8 lq_type, antenna;
2685
d0709a65
JB
2686 rcu_read_lock();
2687
b481de9c
ZY
2688 sta = sta_info_get(local, priv->stations[sta_id].sta.sta.addr);
2689 if (!sta || !sta->rate_ctrl_priv) {
d0709a65 2690 if (sta)
b481de9c 2691 IWL_DEBUG_RATE("leave - no private rate data!\n");
d0709a65 2692 else
b481de9c 2693 IWL_DEBUG_RATE("leave - no station!\n");
d0709a65 2694 rcu_read_unlock();
b481de9c
ZY
2695 return sprintf(buf, "station %d not found\n", sta_id);
2696 }
2697
c33104f0 2698 lq_sta = (void *)sta->rate_ctrl_priv;
b481de9c 2699
c33104f0 2700 lq_type = lq_sta->lq_info[lq_sta->active_tbl].lq_type;
fde0db31 2701 antenna = lq_sta->lq_info[lq_sta->active_tbl].ant_type;
b481de9c
ZY
2702
2703 if (is_legacy(lq_type))
2704 i = IWL_RATE_54M_INDEX;
2705 else
2706 i = IWL_RATE_60M_INDEX;
2707 while (1) {
2708 u64 mask;
2709 int j;
c33104f0 2710 int active = lq_sta->active_tbl;
b481de9c 2711
c33104f0 2712 cnt +=
1826dcc0 2713 sprintf(&buf[cnt], " %2dMbs: ", iwl_rates[i].ieee / 2);
b481de9c
ZY
2714
2715 mask = (1ULL << (IWL_RATE_MAX_WINDOW - 1));
2716 for (j = 0; j < IWL_RATE_MAX_WINDOW; j++, mask >>= 1)
c33104f0
TW
2717 buf[cnt++] =
2718 (lq_sta->lq_info[active].win[i].data & mask)
b481de9c
ZY
2719 ? '1' : '0';
2720
c33104f0
TW
2721 samples += lq_sta->lq_info[active].win[i].counter;
2722 good += lq_sta->lq_info[active].win[i].success_counter;
2723 success += lq_sta->lq_info[active].win[i].success_counter *
1826dcc0 2724 iwl_rates[i].ieee;
b481de9c 2725
c33104f0 2726 if (lq_sta->lq_info[active].win[i].stamp) {
b481de9c
ZY
2727 int delta =
2728 jiffies_to_msecs(now -
c33104f0 2729 lq_sta->lq_info[active].win[i].stamp);
b481de9c
ZY
2730
2731 if (delta > max_time)
2732 max_time = delta;
2733
c33104f0 2734 cnt += sprintf(&buf[cnt], "%5dms\n", delta);
b481de9c 2735 } else
c33104f0 2736 buf[cnt++] = '\n';
b481de9c 2737
bb8c093b 2738 j = iwl4965_get_prev_ieee_rate(i);
b481de9c
ZY
2739 if (j == i)
2740 break;
2741 i = j;
2742 }
2743
1826dcc0
TW
2744 /*
2745 * Display the average rate of all samples taken.
2746 * NOTE: We multiply # of samples by 2 since the IEEE measurement
2747 * added from iwl_rates is actually 2X the rate.
2748 */
b481de9c 2749 if (samples)
c33104f0 2750 cnt += sprintf(&buf[cnt],
b481de9c
ZY
2751 "\nAverage rate is %3d.%02dMbs over last %4dms\n"
2752 "%3d%% success (%d good packets over %d tries)\n",
2753 success / (2 * samples), (success * 5 / samples) % 10,
2754 max_time, good * 100 / samples, good, samples);
2755 else
c33104f0
TW
2756 cnt += sprintf(&buf[cnt], "\nAverage rate: 0Mbs\n");
2757
2758 cnt += sprintf(&buf[cnt], "\nrate scale type %d antenna %d "
b481de9c 2759 "active_search %d rate index %d\n", lq_type, antenna,
8318d78a 2760 lq_sta->search_better_tbl, sta->last_txrate_idx);
b481de9c 2761
d0709a65 2762 rcu_read_unlock();
c33104f0 2763 return cnt;
b481de9c
ZY
2764}
2765
bb8c093b 2766void iwl4965_rate_scale_init(struct ieee80211_hw *hw, s32 sta_id)
b481de9c 2767{
c79dd5b5 2768 struct iwl_priv *priv = hw->priv;
b481de9c
ZY
2769
2770 priv->lq_mngr.lq_ready = 1;
2771}
2772
897e1cf2 2773int iwl4965_rate_control_register(void)
b481de9c 2774{
897e1cf2 2775 return ieee80211_rate_control_register(&rs_ops);
b481de9c
ZY
2776}
2777
897e1cf2 2778void iwl4965_rate_control_unregister(void)
b481de9c
ZY
2779{
2780 ieee80211_rate_control_unregister(&rs_ops);
2781}
2782
This page took 0.348553 seconds and 5 git commands to generate.