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