iwlwifi: remove un-necessary rs_tl_turn_on_agg() after agg enabled
[linux/fpc-iii.git] / drivers / net / wireless / iwlwifi / iwl-agn-rs.c
bloba6f4c74ff2b49bf060cbfa1423f40ef4c3c179e2
1 /******************************************************************************
3 * Copyright(c) 2005 - 2009 Intel Corporation. All rights reserved.
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.
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.
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
18 * The full GNU General Public License is included in this distribution in the
19 * file called LICENSE.
21 * Contact Information:
22 * Intel Linux Wireless <ilw@linux.intel.com>
23 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
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>
32 #include <linux/netdevice.h>
33 #include <linux/etherdevice.h>
34 #include <linux/delay.h>
36 #include <linux/workqueue.h>
38 #include "iwl-dev.h"
39 #include "iwl-sta.h"
40 #include "iwl-core.h"
42 #define RS_NAME "iwl-agn-rs"
44 #define NUM_TRY_BEFORE_ANT_TOGGLE 1
45 #define IWL_NUMBER_TRY 1
46 #define IWL_HT_NUMBER_TRY 3
48 #define IWL_RATE_MAX_WINDOW 62 /* # tx in history window */
49 #define IWL_RATE_MIN_FAILURE_TH 6 /* min failures to calc tpt */
50 #define IWL_RATE_MIN_SUCCESS_TH 8 /* min successes to calc tpt */
52 /* max allowed rate miss before sync LQ cmd */
53 #define IWL_MISSED_RATE_MAX 15
54 /* max time to accum history 2 seconds */
55 #define IWL_RATE_SCALE_FLUSH_INTVL (2*HZ)
57 static u8 rs_ht_to_legacy[] = {
58 IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
59 IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
60 IWL_RATE_6M_INDEX,
61 IWL_RATE_6M_INDEX, IWL_RATE_9M_INDEX,
62 IWL_RATE_12M_INDEX, IWL_RATE_18M_INDEX,
63 IWL_RATE_24M_INDEX, IWL_RATE_36M_INDEX,
64 IWL_RATE_48M_INDEX, IWL_RATE_54M_INDEX
67 static const u8 ant_toggle_lookup[] = {
68 /*ANT_NONE -> */ ANT_NONE,
69 /*ANT_A -> */ ANT_B,
70 /*ANT_B -> */ ANT_C,
71 /*ANT_AB -> */ ANT_BC,
72 /*ANT_C -> */ ANT_A,
73 /*ANT_AC -> */ ANT_AB,
74 /*ANT_BC -> */ ANT_AC,
75 /*ANT_ABC -> */ ANT_ABC,
78 /**
79 * struct iwl_rate_scale_data -- tx success history for one rate
81 struct iwl_rate_scale_data {
82 u64 data; /* bitmap of successful frames */
83 s32 success_counter; /* number of frames successful */
84 s32 success_ratio; /* per-cent * 128 */
85 s32 counter; /* number of frames attempted */
86 s32 average_tpt; /* success ratio * expected throughput */
87 unsigned long stamp;
90 /**
91 * struct iwl_scale_tbl_info -- tx params and success history for all rates
93 * There are two of these in struct iwl_lq_sta,
94 * one for "active", and one for "search".
96 struct iwl_scale_tbl_info {
97 enum iwl_table_type lq_type;
98 u8 ant_type;
99 u8 is_SGI; /* 1 = short guard interval */
100 u8 is_fat; /* 1 = 40 MHz channel width */
101 u8 is_dup; /* 1 = duplicated data streams */
102 u8 action; /* change modulation; IWL_[LEGACY/SISO/MIMO]_SWITCH_* */
103 s32 *expected_tpt; /* throughput metrics; expected_tpt_G, etc. */
104 u32 current_rate; /* rate_n_flags, uCode API format */
105 struct iwl_rate_scale_data win[IWL_RATE_COUNT]; /* rate histories */
108 struct iwl_traffic_load {
109 unsigned long time_stamp; /* age of the oldest statistics */
110 u32 packet_count[TID_QUEUE_MAX_SIZE]; /* packet count in this time
111 * slice */
112 u32 total; /* total num of packets during the
113 * last TID_MAX_TIME_DIFF */
114 u8 queue_count; /* number of queues that has
115 * been used since the last cleanup */
116 u8 head; /* start of the circular buffer */
120 * struct iwl_lq_sta -- driver's rate scaling private structure
122 * Pointer to this gets passed back and forth between driver and mac80211.
124 struct iwl_lq_sta {
125 u8 active_tbl; /* index of active table, range 0-1 */
126 u8 enable_counter; /* indicates HT mode */
127 u8 stay_in_tbl; /* 1: disallow, 0: allow search for new mode */
128 u8 search_better_tbl; /* 1: currently trying alternate mode */
129 s32 last_tpt;
131 /* The following determine when to search for a new mode */
132 u32 table_count_limit;
133 u32 max_failure_limit; /* # failed frames before new search */
134 u32 max_success_limit; /* # successful frames before new search */
135 u32 table_count;
136 u32 total_failed; /* total failed frames, any/all rates */
137 u32 total_success; /* total successful frames, any/all rates */
138 u32 flush_timer; /* time staying in mode before new search */
140 u8 action_counter; /* # mode-switch actions tried */
141 u8 is_green;
142 u8 is_dup;
143 enum ieee80211_band band;
144 u8 ibss_sta_added;
146 /* The following are bitmaps of rates; IWL_RATE_6M_MASK, etc. */
147 u32 supp_rates;
148 u16 active_legacy_rate;
149 u16 active_siso_rate;
150 u16 active_mimo2_rate;
151 u16 active_mimo3_rate;
152 u16 active_rate_basic;
153 s8 max_rate_idx; /* Max rate set by user */
154 u8 missed_rate_counter;
156 struct iwl_link_quality_cmd lq;
157 struct iwl_scale_tbl_info lq_info[LQ_SIZE]; /* "active", "search" */
158 struct iwl_traffic_load load[TID_MAX_LOAD_COUNT];
159 u8 tx_agg_tid_en;
160 #ifdef CONFIG_MAC80211_DEBUGFS
161 struct dentry *rs_sta_dbgfs_scale_table_file;
162 struct dentry *rs_sta_dbgfs_stats_table_file;
163 struct dentry *rs_sta_dbgfs_tx_agg_tid_en_file;
164 u32 dbg_fixed_rate;
165 #endif
166 struct iwl_priv *drv;
168 /* used to be in sta_info */
169 int last_txrate_idx;
172 static void rs_rate_scale_perform(struct iwl_priv *priv,
173 struct ieee80211_hdr *hdr,
174 struct ieee80211_sta *sta,
175 struct iwl_lq_sta *lq_sta);
176 static void rs_fill_link_cmd(const struct iwl_priv *priv,
177 struct iwl_lq_sta *lq_sta, u32 rate_n_flags);
180 #ifdef CONFIG_MAC80211_DEBUGFS
181 static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
182 u32 *rate_n_flags, int index);
183 #else
184 static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
185 u32 *rate_n_flags, int index)
187 #endif
190 * Expected throughput metrics for following rates:
191 * 1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
192 * "G" is the only table that supports CCK (the first 4 rates).
194 /*FIXME:RS:need to separate tables for MIMO2/MIMO3*/
195 static s32 expected_tpt_A[IWL_RATE_COUNT] = {
196 0, 0, 0, 0, 40, 57, 72, 98, 121, 154, 177, 186, 186
199 static s32 expected_tpt_G[IWL_RATE_COUNT] = {
200 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 186
203 static s32 expected_tpt_siso20MHz[IWL_RATE_COUNT] = {
204 0, 0, 0, 0, 42, 42, 76, 102, 124, 159, 183, 193, 202
207 static s32 expected_tpt_siso20MHzSGI[IWL_RATE_COUNT] = {
208 0, 0, 0, 0, 46, 46, 82, 110, 132, 168, 192, 202, 211
211 static s32 expected_tpt_mimo20MHz[IWL_RATE_COUNT] = {
212 0, 0, 0, 0, 74, 74, 123, 155, 179, 214, 236, 244, 251
215 static s32 expected_tpt_mimo20MHzSGI[IWL_RATE_COUNT] = {
216 0, 0, 0, 0, 81, 81, 131, 164, 188, 222, 243, 251, 257
219 static s32 expected_tpt_siso40MHz[IWL_RATE_COUNT] = {
220 0, 0, 0, 0, 77, 77, 127, 160, 184, 220, 242, 250, 257
223 static s32 expected_tpt_siso40MHzSGI[IWL_RATE_COUNT] = {
224 0, 0, 0, 0, 83, 83, 135, 169, 193, 229, 250, 257, 264
227 static s32 expected_tpt_mimo40MHz[IWL_RATE_COUNT] = {
228 0, 0, 0, 0, 123, 123, 182, 214, 235, 264, 279, 285, 289
231 static s32 expected_tpt_mimo40MHzSGI[IWL_RATE_COUNT] = {
232 0, 0, 0, 0, 131, 131, 191, 222, 242, 270, 284, 289, 293
235 static inline u8 rs_extract_rate(u32 rate_n_flags)
237 return (u8)(rate_n_flags & 0xFF);
240 static void rs_rate_scale_clear_window(struct iwl_rate_scale_data *window)
242 window->data = 0;
243 window->success_counter = 0;
244 window->success_ratio = IWL_INVALID_VALUE;
245 window->counter = 0;
246 window->average_tpt = IWL_INVALID_VALUE;
247 window->stamp = 0;
250 static inline u8 rs_is_valid_ant(u8 valid_antenna, u8 ant_type)
252 return (ant_type & valid_antenna) == ant_type;
256 * removes the old data from the statistics. All data that is older than
257 * TID_MAX_TIME_DIFF, will be deleted.
259 static void rs_tl_rm_old_stats(struct iwl_traffic_load *tl, u32 curr_time)
261 /* The oldest age we want to keep */
262 u32 oldest_time = curr_time - TID_MAX_TIME_DIFF;
264 while (tl->queue_count &&
265 (tl->time_stamp < oldest_time)) {
266 tl->total -= tl->packet_count[tl->head];
267 tl->packet_count[tl->head] = 0;
268 tl->time_stamp += TID_QUEUE_CELL_SPACING;
269 tl->queue_count--;
270 tl->head++;
271 if (tl->head >= TID_QUEUE_MAX_SIZE)
272 tl->head = 0;
277 * increment traffic load value for tid and also remove
278 * any old values if passed the certain time period
280 static u8 rs_tl_add_packet(struct iwl_lq_sta *lq_data,
281 struct ieee80211_hdr *hdr)
283 u32 curr_time = jiffies_to_msecs(jiffies);
284 u32 time_diff;
285 s32 index;
286 struct iwl_traffic_load *tl = NULL;
287 u8 tid;
289 if (ieee80211_is_data_qos(hdr->frame_control)) {
290 u8 *qc = ieee80211_get_qos_ctl(hdr);
291 tid = qc[0] & 0xf;
292 } else
293 return MAX_TID_COUNT;
295 tl = &lq_data->load[tid];
297 curr_time -= curr_time % TID_ROUND_VALUE;
299 /* Happens only for the first packet. Initialize the data */
300 if (!(tl->queue_count)) {
301 tl->total = 1;
302 tl->time_stamp = curr_time;
303 tl->queue_count = 1;
304 tl->head = 0;
305 tl->packet_count[0] = 1;
306 return MAX_TID_COUNT;
309 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
310 index = time_diff / TID_QUEUE_CELL_SPACING;
312 /* The history is too long: remove data that is older than */
313 /* TID_MAX_TIME_DIFF */
314 if (index >= TID_QUEUE_MAX_SIZE)
315 rs_tl_rm_old_stats(tl, curr_time);
317 index = (tl->head + index) % TID_QUEUE_MAX_SIZE;
318 tl->packet_count[index] = tl->packet_count[index] + 1;
319 tl->total = tl->total + 1;
321 if ((index + 1) > tl->queue_count)
322 tl->queue_count = index + 1;
324 return tid;
328 get the traffic load value for tid
330 static u32 rs_tl_get_load(struct iwl_lq_sta *lq_data, u8 tid)
332 u32 curr_time = jiffies_to_msecs(jiffies);
333 u32 time_diff;
334 s32 index;
335 struct iwl_traffic_load *tl = NULL;
337 if (tid >= TID_MAX_LOAD_COUNT)
338 return 0;
340 tl = &(lq_data->load[tid]);
342 curr_time -= curr_time % TID_ROUND_VALUE;
344 if (!(tl->queue_count))
345 return 0;
347 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
348 index = time_diff / TID_QUEUE_CELL_SPACING;
350 /* The history is too long: remove data that is older than */
351 /* TID_MAX_TIME_DIFF */
352 if (index >= TID_QUEUE_MAX_SIZE)
353 rs_tl_rm_old_stats(tl, curr_time);
355 return tl->total;
358 static void rs_tl_turn_on_agg_for_tid(struct iwl_priv *priv,
359 struct iwl_lq_sta *lq_data, u8 tid,
360 struct ieee80211_sta *sta)
362 if (rs_tl_get_load(lq_data, tid) > IWL_AGG_LOAD_THRESHOLD) {
363 IWL_DEBUG_HT(priv, "Starting Tx agg: STA: %pM tid: %d\n",
364 sta->addr, tid);
365 ieee80211_start_tx_ba_session(priv->hw, sta->addr, tid);
369 static void rs_tl_turn_on_agg(struct iwl_priv *priv, u8 tid,
370 struct iwl_lq_sta *lq_data,
371 struct ieee80211_sta *sta)
373 if ((tid < TID_MAX_LOAD_COUNT))
374 rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
375 else if (tid == IWL_AGG_ALL_TID)
376 for (tid = 0; tid < TID_MAX_LOAD_COUNT; tid++)
377 rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
380 static inline int get_num_of_ant_from_rate(u32 rate_n_flags)
382 return !!(rate_n_flags & RATE_MCS_ANT_A_MSK) +
383 !!(rate_n_flags & RATE_MCS_ANT_B_MSK) +
384 !!(rate_n_flags & RATE_MCS_ANT_C_MSK);
388 * rs_collect_tx_data - Update the success/failure sliding window
390 * We keep a sliding window of the last 62 packets transmitted
391 * at this rate. window->data contains the bitmask of successful
392 * packets.
394 static int rs_collect_tx_data(struct iwl_rate_scale_data *windows,
395 int scale_index, s32 tpt, int retries,
396 int successes)
398 struct iwl_rate_scale_data *window = NULL;
399 static const u64 mask = (((u64)1) << (IWL_RATE_MAX_WINDOW - 1));
400 s32 fail_count;
402 if (scale_index < 0 || scale_index >= IWL_RATE_COUNT)
403 return -EINVAL;
405 /* Select data for current tx bit rate */
406 window = &(windows[scale_index]);
409 * Keep track of only the latest 62 tx frame attempts in this rate's
410 * history window; anything older isn't really relevant any more.
411 * If we have filled up the sliding window, drop the oldest attempt;
412 * if the oldest attempt (highest bit in bitmap) shows "success",
413 * subtract "1" from the success counter (this is the main reason
414 * we keep these bitmaps!).
416 while (retries > 0) {
417 if (window->counter >= IWL_RATE_MAX_WINDOW) {
419 /* remove earliest */
420 window->counter = IWL_RATE_MAX_WINDOW - 1;
422 if (window->data & mask) {
423 window->data &= ~mask;
424 window->success_counter--;
428 /* Increment frames-attempted counter */
429 window->counter++;
431 /* Shift bitmap by one frame (throw away oldest history),
432 * OR in "1", and increment "success" if this
433 * frame was successful. */
434 window->data <<= 1;
435 if (successes > 0) {
436 window->success_counter++;
437 window->data |= 0x1;
438 successes--;
441 retries--;
444 /* Calculate current success ratio, avoid divide-by-0! */
445 if (window->counter > 0)
446 window->success_ratio = 128 * (100 * window->success_counter)
447 / window->counter;
448 else
449 window->success_ratio = IWL_INVALID_VALUE;
451 fail_count = window->counter - window->success_counter;
453 /* Calculate average throughput, if we have enough history. */
454 if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
455 (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
456 window->average_tpt = (window->success_ratio * tpt + 64) / 128;
457 else
458 window->average_tpt = IWL_INVALID_VALUE;
460 /* Tag this window as having been updated */
461 window->stamp = jiffies;
463 return 0;
467 * Fill uCode API rate_n_flags field, based on "search" or "active" table.
469 /* FIXME:RS:remove this function and put the flags statically in the table */
470 static u32 rate_n_flags_from_tbl(struct iwl_priv *priv,
471 struct iwl_scale_tbl_info *tbl,
472 int index, u8 use_green)
474 u32 rate_n_flags = 0;
476 if (is_legacy(tbl->lq_type)) {
477 rate_n_flags = iwl_rates[index].plcp;
478 if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
479 rate_n_flags |= RATE_MCS_CCK_MSK;
481 } else if (is_Ht(tbl->lq_type)) {
482 if (index > IWL_LAST_OFDM_RATE) {
483 IWL_ERR(priv, "Invalid HT rate index %d\n", index);
484 index = IWL_LAST_OFDM_RATE;
486 rate_n_flags = RATE_MCS_HT_MSK;
488 if (is_siso(tbl->lq_type))
489 rate_n_flags |= iwl_rates[index].plcp_siso;
490 else if (is_mimo2(tbl->lq_type))
491 rate_n_flags |= iwl_rates[index].plcp_mimo2;
492 else
493 rate_n_flags |= iwl_rates[index].plcp_mimo3;
494 } else {
495 IWL_ERR(priv, "Invalid tbl->lq_type %d\n", tbl->lq_type);
498 rate_n_flags |= ((tbl->ant_type << RATE_MCS_ANT_POS) &
499 RATE_MCS_ANT_ABC_MSK);
501 if (is_Ht(tbl->lq_type)) {
502 if (tbl->is_fat) {
503 if (tbl->is_dup)
504 rate_n_flags |= RATE_MCS_DUP_MSK;
505 else
506 rate_n_flags |= RATE_MCS_FAT_MSK;
508 if (tbl->is_SGI)
509 rate_n_flags |= RATE_MCS_SGI_MSK;
511 if (use_green) {
512 rate_n_flags |= RATE_MCS_GF_MSK;
513 if (is_siso(tbl->lq_type) && tbl->is_SGI) {
514 rate_n_flags &= ~RATE_MCS_SGI_MSK;
515 IWL_ERR(priv, "GF was set with SGI:SISO\n");
519 return rate_n_flags;
523 * Interpret uCode API's rate_n_flags format,
524 * fill "search" or "active" tx mode table.
526 static int rs_get_tbl_info_from_mcs(const u32 rate_n_flags,
527 enum ieee80211_band band,
528 struct iwl_scale_tbl_info *tbl,
529 int *rate_idx)
531 u32 ant_msk = (rate_n_flags & RATE_MCS_ANT_ABC_MSK);
532 u8 num_of_ant = get_num_of_ant_from_rate(rate_n_flags);
533 u8 mcs;
535 *rate_idx = iwl_hwrate_to_plcp_idx(rate_n_flags);
537 if (*rate_idx == IWL_RATE_INVALID) {
538 *rate_idx = -1;
539 return -EINVAL;
541 tbl->is_SGI = 0; /* default legacy setup */
542 tbl->is_fat = 0;
543 tbl->is_dup = 0;
544 tbl->ant_type = (ant_msk >> RATE_MCS_ANT_POS);
545 tbl->lq_type = LQ_NONE;
547 /* legacy rate format */
548 if (!(rate_n_flags & RATE_MCS_HT_MSK)) {
549 if (num_of_ant == 1) {
550 if (band == IEEE80211_BAND_5GHZ)
551 tbl->lq_type = LQ_A;
552 else
553 tbl->lq_type = LQ_G;
555 /* HT rate format */
556 } else {
557 if (rate_n_flags & RATE_MCS_SGI_MSK)
558 tbl->is_SGI = 1;
560 if ((rate_n_flags & RATE_MCS_FAT_MSK) ||
561 (rate_n_flags & RATE_MCS_DUP_MSK))
562 tbl->is_fat = 1;
564 if (rate_n_flags & RATE_MCS_DUP_MSK)
565 tbl->is_dup = 1;
567 mcs = rs_extract_rate(rate_n_flags);
569 /* SISO */
570 if (mcs <= IWL_RATE_SISO_60M_PLCP) {
571 if (num_of_ant == 1)
572 tbl->lq_type = LQ_SISO; /*else NONE*/
573 /* MIMO2 */
574 } else if (mcs <= IWL_RATE_MIMO2_60M_PLCP) {
575 if (num_of_ant == 2)
576 tbl->lq_type = LQ_MIMO2;
577 /* MIMO3 */
578 } else {
579 if (num_of_ant == 3)
580 tbl->lq_type = LQ_MIMO3;
583 return 0;
586 /* switch to another antenna/antennas and return 1 */
587 /* if no other valid antenna found, return 0 */
588 static int rs_toggle_antenna(u32 valid_ant, u32 *rate_n_flags,
589 struct iwl_scale_tbl_info *tbl)
591 u8 new_ant_type;
593 if (!tbl->ant_type || tbl->ant_type > ANT_ABC)
594 return 0;
596 if (!rs_is_valid_ant(valid_ant, tbl->ant_type))
597 return 0;
599 new_ant_type = ant_toggle_lookup[tbl->ant_type];
601 while ((new_ant_type != tbl->ant_type) &&
602 !rs_is_valid_ant(valid_ant, new_ant_type))
603 new_ant_type = ant_toggle_lookup[new_ant_type];
605 if (new_ant_type == tbl->ant_type)
606 return 0;
608 tbl->ant_type = new_ant_type;
609 *rate_n_flags &= ~RATE_MCS_ANT_ABC_MSK;
610 *rate_n_flags |= new_ant_type << RATE_MCS_ANT_POS;
611 return 1;
614 /* FIXME:RS: in 4965 we don't use greenfield at all */
615 /* FIXME:RS: don't use greenfield for now in TX */
616 #if 0
617 static inline u8 rs_use_green(struct iwl_priv *priv, struct ieee80211_conf *conf)
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;
623 #endif
624 static inline u8 rs_use_green(struct iwl_priv *priv, struct ieee80211_conf *conf)
626 return 0;
630 * rs_get_supported_rates - get the available rates
632 * if management frame or broadcast frame only return
633 * basic available rates.
636 static u16 rs_get_supported_rates(struct iwl_lq_sta *lq_sta,
637 struct ieee80211_hdr *hdr,
638 enum iwl_table_type rate_type)
640 if (hdr && is_multicast_ether_addr(hdr->addr1) &&
641 lq_sta->active_rate_basic)
642 return lq_sta->active_rate_basic;
644 if (is_legacy(rate_type)) {
645 return lq_sta->active_legacy_rate;
646 } else {
647 if (is_siso(rate_type))
648 return lq_sta->active_siso_rate;
649 else if (is_mimo2(rate_type))
650 return lq_sta->active_mimo2_rate;
651 else
652 return lq_sta->active_mimo3_rate;
656 static u16 rs_get_adjacent_rate(struct iwl_priv *priv, u8 index, u16 rate_mask,
657 int rate_type)
659 u8 high = IWL_RATE_INVALID;
660 u8 low = IWL_RATE_INVALID;
662 /* 802.11A or ht walks to the next literal adjacent rate in
663 * the rate table */
664 if (is_a_band(rate_type) || !is_legacy(rate_type)) {
665 int i;
666 u32 mask;
668 /* Find the previous rate that is in the rate mask */
669 i = index - 1;
670 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
671 if (rate_mask & mask) {
672 low = i;
673 break;
677 /* Find the next rate that is in the rate mask */
678 i = index + 1;
679 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
680 if (rate_mask & mask) {
681 high = i;
682 break;
686 return (high << 8) | low;
689 low = index;
690 while (low != IWL_RATE_INVALID) {
691 low = iwl_rates[low].prev_rs;
692 if (low == IWL_RATE_INVALID)
693 break;
694 if (rate_mask & (1 << low))
695 break;
696 IWL_DEBUG_RATE(priv, "Skipping masked lower rate: %d\n", low);
699 high = index;
700 while (high != IWL_RATE_INVALID) {
701 high = iwl_rates[high].next_rs;
702 if (high == IWL_RATE_INVALID)
703 break;
704 if (rate_mask & (1 << high))
705 break;
706 IWL_DEBUG_RATE(priv, "Skipping masked higher rate: %d\n", high);
709 return (high << 8) | low;
712 static u32 rs_get_lower_rate(struct iwl_lq_sta *lq_sta,
713 struct iwl_scale_tbl_info *tbl,
714 u8 scale_index, u8 ht_possible)
716 s32 low;
717 u16 rate_mask;
718 u16 high_low;
719 u8 switch_to_legacy = 0;
720 u8 is_green = lq_sta->is_green;
722 /* check if we need to switch from HT to legacy rates.
723 * assumption is that mandatory rates (1Mbps or 6Mbps)
724 * are always supported (spec demand) */
725 if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_index)) {
726 switch_to_legacy = 1;
727 scale_index = rs_ht_to_legacy[scale_index];
728 if (lq_sta->band == IEEE80211_BAND_5GHZ)
729 tbl->lq_type = LQ_A;
730 else
731 tbl->lq_type = LQ_G;
733 if (num_of_ant(tbl->ant_type) > 1)
734 tbl->ant_type = ANT_A;/*FIXME:RS*/
736 tbl->is_fat = 0;
737 tbl->is_SGI = 0;
740 rate_mask = rs_get_supported_rates(lq_sta, NULL, tbl->lq_type);
742 /* Mask with station rate restriction */
743 if (is_legacy(tbl->lq_type)) {
744 /* supp_rates has no CCK bits in A mode */
745 if (lq_sta->band == IEEE80211_BAND_5GHZ)
746 rate_mask = (u16)(rate_mask &
747 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
748 else
749 rate_mask = (u16)(rate_mask & lq_sta->supp_rates);
752 /* If we switched from HT to legacy, check current rate */
753 if (switch_to_legacy && (rate_mask & (1 << scale_index))) {
754 low = scale_index;
755 goto out;
758 high_low = rs_get_adjacent_rate(lq_sta->drv, scale_index, rate_mask,
759 tbl->lq_type);
760 low = high_low & 0xff;
762 if (low == IWL_RATE_INVALID)
763 low = scale_index;
765 out:
766 return rate_n_flags_from_tbl(lq_sta->drv, tbl, low, is_green);
770 * mac80211 sends us Tx status
772 static void rs_tx_status(void *priv_r, struct ieee80211_supported_band *sband,
773 struct ieee80211_sta *sta, void *priv_sta,
774 struct sk_buff *skb)
776 int status;
777 u8 retries;
778 int rs_index, index = 0;
779 struct iwl_lq_sta *lq_sta = priv_sta;
780 struct iwl_link_quality_cmd *table;
781 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
782 struct iwl_priv *priv = (struct iwl_priv *)priv_r;
783 struct ieee80211_hw *hw = priv->hw;
784 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
785 struct iwl_rate_scale_data *window = NULL;
786 struct iwl_rate_scale_data *search_win = NULL;
787 u32 tx_rate;
788 struct iwl_scale_tbl_info tbl_type;
789 struct iwl_scale_tbl_info *curr_tbl, *search_tbl;
790 u8 active_index = 0;
791 s32 tpt = 0;
793 IWL_DEBUG_RATE_LIMIT(priv, "get frame ack response, update rate scale window\n");
795 if (!ieee80211_is_data(hdr->frame_control) ||
796 is_multicast_ether_addr(hdr->addr1))
797 return;
799 /* This packet was aggregated but doesn't carry rate scale info */
800 if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
801 !(info->flags & IEEE80211_TX_STAT_AMPDU))
802 return;
804 retries = info->status.rates[0].count - 1;
806 if (retries > 15)
807 retries = 15;
809 if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) &&
810 !lq_sta->ibss_sta_added)
811 goto out;
813 table = &lq_sta->lq;
814 active_index = lq_sta->active_tbl;
816 curr_tbl = &(lq_sta->lq_info[active_index]);
817 search_tbl = &(lq_sta->lq_info[(1 - active_index)]);
818 window = (struct iwl_rate_scale_data *)&(curr_tbl->win[0]);
819 search_win = (struct iwl_rate_scale_data *)&(search_tbl->win[0]);
822 * Ignore this Tx frame response if its initial rate doesn't match
823 * that of latest Link Quality command. There may be stragglers
824 * from a previous Link Quality command, but we're no longer interested
825 * in those; they're either from the "active" mode while we're trying
826 * to check "search" mode, or a prior "search" mode after we've moved
827 * to a new "search" mode (which might become the new "active" mode).
829 tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
830 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
831 if (priv->band == IEEE80211_BAND_5GHZ)
832 rs_index -= IWL_FIRST_OFDM_RATE;
834 if ((info->status.rates[0].idx < 0) ||
835 (tbl_type.is_SGI != !!(info->status.rates[0].flags & IEEE80211_TX_RC_SHORT_GI)) ||
836 (tbl_type.is_fat != !!(info->status.rates[0].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)) ||
837 (tbl_type.is_dup != !!(info->status.rates[0].flags & IEEE80211_TX_RC_DUP_DATA)) ||
838 (tbl_type.ant_type != info->antenna_sel_tx) ||
839 (!!(tx_rate & RATE_MCS_HT_MSK) != !!(info->status.rates[0].flags & IEEE80211_TX_RC_MCS)) ||
840 (!!(tx_rate & RATE_MCS_GF_MSK) != !!(info->status.rates[0].flags & IEEE80211_TX_RC_GREEN_FIELD)) ||
841 (hw->wiphy->bands[priv->band]->bitrates[rs_index].bitrate !=
842 hw->wiphy->bands[info->band]->bitrates[info->status.rates[0].idx].bitrate)) {
843 IWL_DEBUG_RATE(priv, "initial rate does not match 0x%x\n", tx_rate);
844 /* the last LQ command could failed so the LQ in ucode not
845 * the same in driver sync up
847 lq_sta->missed_rate_counter++;
848 if (lq_sta->missed_rate_counter > IWL_MISSED_RATE_MAX) {
849 lq_sta->missed_rate_counter = 0;
850 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
852 goto out;
855 lq_sta->missed_rate_counter = 0;
856 /* Update frame history window with "failure" for each Tx retry. */
857 while (retries) {
858 /* Look up the rate and other info used for each tx attempt.
859 * Each tx attempt steps one entry deeper in the rate table. */
860 tx_rate = le32_to_cpu(table->rs_table[index].rate_n_flags);
861 rs_get_tbl_info_from_mcs(tx_rate, priv->band,
862 &tbl_type, &rs_index);
864 /* If type matches "search" table,
865 * add failure to "search" history */
866 if ((tbl_type.lq_type == search_tbl->lq_type) &&
867 (tbl_type.ant_type == search_tbl->ant_type) &&
868 (tbl_type.is_SGI == search_tbl->is_SGI)) {
869 if (search_tbl->expected_tpt)
870 tpt = search_tbl->expected_tpt[rs_index];
871 else
872 tpt = 0;
873 rs_collect_tx_data(search_win, rs_index, tpt, 1, 0);
875 /* Else if type matches "current/active" table,
876 * add failure to "current/active" history */
877 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
878 (tbl_type.ant_type == curr_tbl->ant_type) &&
879 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
880 if (curr_tbl->expected_tpt)
881 tpt = curr_tbl->expected_tpt[rs_index];
882 else
883 tpt = 0;
884 rs_collect_tx_data(window, rs_index, tpt, 1, 0);
887 /* If not searching for a new mode, increment failed counter
888 * ... this helps determine when to start searching again */
889 if (lq_sta->stay_in_tbl)
890 lq_sta->total_failed++;
891 --retries;
892 index++;
897 * Find (by rate) the history window to update with final Tx attempt;
898 * if Tx was successful first try, use original rate,
899 * else look up the rate that was, finally, successful.
901 tx_rate = le32_to_cpu(table->rs_table[index].rate_n_flags);
902 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
904 /* Update frame history window with "success" if Tx got ACKed ... */
905 status = !!(info->flags & IEEE80211_TX_STAT_ACK);
907 /* If type matches "search" table,
908 * add final tx status to "search" history */
909 if ((tbl_type.lq_type == search_tbl->lq_type) &&
910 (tbl_type.ant_type == search_tbl->ant_type) &&
911 (tbl_type.is_SGI == search_tbl->is_SGI)) {
912 if (search_tbl->expected_tpt)
913 tpt = search_tbl->expected_tpt[rs_index];
914 else
915 tpt = 0;
916 if (info->flags & IEEE80211_TX_CTL_AMPDU)
917 rs_collect_tx_data(search_win, rs_index, tpt,
918 info->status.ampdu_ack_len,
919 info->status.ampdu_ack_map);
920 else
921 rs_collect_tx_data(search_win, rs_index, tpt,
922 1, status);
923 /* Else if type matches "current/active" table,
924 * add final tx status to "current/active" history */
925 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
926 (tbl_type.ant_type == curr_tbl->ant_type) &&
927 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
928 if (curr_tbl->expected_tpt)
929 tpt = curr_tbl->expected_tpt[rs_index];
930 else
931 tpt = 0;
932 if (info->flags & IEEE80211_TX_CTL_AMPDU)
933 rs_collect_tx_data(window, rs_index, tpt,
934 info->status.ampdu_ack_len,
935 info->status.ampdu_ack_map);
936 else
937 rs_collect_tx_data(window, rs_index, tpt,
938 1, status);
941 /* If not searching for new mode, increment success/failed counter
942 * ... these help determine when to start searching again */
943 if (lq_sta->stay_in_tbl) {
944 if (info->flags & IEEE80211_TX_CTL_AMPDU) {
945 lq_sta->total_success += info->status.ampdu_ack_map;
946 lq_sta->total_failed +=
947 (info->status.ampdu_ack_len - info->status.ampdu_ack_map);
948 } else {
949 if (status)
950 lq_sta->total_success++;
951 else
952 lq_sta->total_failed++;
956 /* See if there's a better rate or modulation mode to try. */
957 if (sta && sta->supp_rates[sband->band])
958 rs_rate_scale_perform(priv, hdr, sta, lq_sta);
959 out:
960 return;
964 * Begin a period of staying with a selected modulation mode.
965 * Set "stay_in_tbl" flag to prevent any mode switches.
966 * Set frame tx success limits according to legacy vs. high-throughput,
967 * and reset overall (spanning all rates) tx success history statistics.
968 * These control how long we stay using same modulation mode before
969 * searching for a new mode.
971 static void rs_set_stay_in_table(struct iwl_priv *priv, u8 is_legacy,
972 struct iwl_lq_sta *lq_sta)
974 IWL_DEBUG_RATE(priv, "we are staying in the same table\n");
975 lq_sta->stay_in_tbl = 1; /* only place this gets set */
976 if (is_legacy) {
977 lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT;
978 lq_sta->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
979 lq_sta->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
980 } else {
981 lq_sta->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
982 lq_sta->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
983 lq_sta->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
985 lq_sta->table_count = 0;
986 lq_sta->total_failed = 0;
987 lq_sta->total_success = 0;
991 * Find correct throughput table for given mode of modulation
993 static void rs_set_expected_tpt_table(struct iwl_lq_sta *lq_sta,
994 struct iwl_scale_tbl_info *tbl)
996 if (is_legacy(tbl->lq_type)) {
997 if (!is_a_band(tbl->lq_type))
998 tbl->expected_tpt = expected_tpt_G;
999 else
1000 tbl->expected_tpt = expected_tpt_A;
1001 } else if (is_siso(tbl->lq_type)) {
1002 if (tbl->is_fat && !lq_sta->is_dup)
1003 if (tbl->is_SGI)
1004 tbl->expected_tpt = expected_tpt_siso40MHzSGI;
1005 else
1006 tbl->expected_tpt = expected_tpt_siso40MHz;
1007 else if (tbl->is_SGI)
1008 tbl->expected_tpt = expected_tpt_siso20MHzSGI;
1009 else
1010 tbl->expected_tpt = expected_tpt_siso20MHz;
1012 } else if (is_mimo(tbl->lq_type)) { /* FIXME:need to separate mimo2/3 */
1013 if (tbl->is_fat && !lq_sta->is_dup)
1014 if (tbl->is_SGI)
1015 tbl->expected_tpt = expected_tpt_mimo40MHzSGI;
1016 else
1017 tbl->expected_tpt = expected_tpt_mimo40MHz;
1018 else if (tbl->is_SGI)
1019 tbl->expected_tpt = expected_tpt_mimo20MHzSGI;
1020 else
1021 tbl->expected_tpt = expected_tpt_mimo20MHz;
1022 } else
1023 tbl->expected_tpt = expected_tpt_G;
1027 * Find starting rate for new "search" high-throughput mode of modulation.
1028 * Goal is to find lowest expected rate (under perfect conditions) that is
1029 * above the current measured throughput of "active" mode, to give new mode
1030 * a fair chance to prove itself without too many challenges.
1032 * This gets called when transitioning to more aggressive modulation
1033 * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
1034 * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
1035 * to decrease to match "active" throughput. When moving from MIMO to SISO,
1036 * bit rate will typically need to increase, but not if performance was bad.
1038 static s32 rs_get_best_rate(struct iwl_priv *priv,
1039 struct iwl_lq_sta *lq_sta,
1040 struct iwl_scale_tbl_info *tbl, /* "search" */
1041 u16 rate_mask, s8 index)
1043 /* "active" values */
1044 struct iwl_scale_tbl_info *active_tbl =
1045 &(lq_sta->lq_info[lq_sta->active_tbl]);
1046 s32 active_sr = active_tbl->win[index].success_ratio;
1047 s32 active_tpt = active_tbl->expected_tpt[index];
1049 /* expected "search" throughput */
1050 s32 *tpt_tbl = tbl->expected_tpt;
1052 s32 new_rate, high, low, start_hi;
1053 u16 high_low;
1054 s8 rate = index;
1056 new_rate = high = low = start_hi = IWL_RATE_INVALID;
1058 for (; ;) {
1059 high_low = rs_get_adjacent_rate(priv, rate, rate_mask,
1060 tbl->lq_type);
1062 low = high_low & 0xff;
1063 high = (high_low >> 8) & 0xff;
1066 * Lower the "search" bit rate, to give new "search" mode
1067 * approximately the same throughput as "active" if:
1069 * 1) "Active" mode has been working modestly well (but not
1070 * great), and expected "search" throughput (under perfect
1071 * conditions) at candidate rate is above the actual
1072 * measured "active" throughput (but less than expected
1073 * "active" throughput under perfect conditions).
1074 * OR
1075 * 2) "Active" mode has been working perfectly or very well
1076 * and expected "search" throughput (under perfect
1077 * conditions) at candidate rate is above expected
1078 * "active" throughput (under perfect conditions).
1080 if ((((100 * tpt_tbl[rate]) > lq_sta->last_tpt) &&
1081 ((active_sr > IWL_RATE_DECREASE_TH) &&
1082 (active_sr <= IWL_RATE_HIGH_TH) &&
1083 (tpt_tbl[rate] <= active_tpt))) ||
1084 ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
1085 (tpt_tbl[rate] > active_tpt))) {
1087 /* (2nd or later pass)
1088 * If we've already tried to raise the rate, and are
1089 * now trying to lower it, use the higher rate. */
1090 if (start_hi != IWL_RATE_INVALID) {
1091 new_rate = start_hi;
1092 break;
1095 new_rate = rate;
1097 /* Loop again with lower rate */
1098 if (low != IWL_RATE_INVALID)
1099 rate = low;
1101 /* Lower rate not available, use the original */
1102 else
1103 break;
1105 /* Else try to raise the "search" rate to match "active" */
1106 } else {
1107 /* (2nd or later pass)
1108 * If we've already tried to lower the rate, and are
1109 * now trying to raise it, use the lower rate. */
1110 if (new_rate != IWL_RATE_INVALID)
1111 break;
1113 /* Loop again with higher rate */
1114 else if (high != IWL_RATE_INVALID) {
1115 start_hi = high;
1116 rate = high;
1118 /* Higher rate not available, use the original */
1119 } else {
1120 new_rate = rate;
1121 break;
1126 return new_rate;
1130 * Set up search table for MIMO
1132 static int rs_switch_to_mimo2(struct iwl_priv *priv,
1133 struct iwl_lq_sta *lq_sta,
1134 struct ieee80211_conf *conf,
1135 struct ieee80211_sta *sta,
1136 struct iwl_scale_tbl_info *tbl, int index)
1138 u16 rate_mask;
1139 s32 rate;
1140 s8 is_green = lq_sta->is_green;
1142 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
1143 return -1;
1145 if (((sta->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 2)
1146 == WLAN_HT_CAP_SM_PS_STATIC)
1147 return -1;
1149 /* Need both Tx chains/antennas to support MIMO */
1150 if (priv->hw_params.tx_chains_num < 2)
1151 return -1;
1153 IWL_DEBUG_RATE(priv, "LQ: try to switch to MIMO2\n");
1155 tbl->lq_type = LQ_MIMO2;
1156 tbl->is_dup = lq_sta->is_dup;
1157 tbl->action = 0;
1158 rate_mask = lq_sta->active_mimo2_rate;
1160 if (priv->current_ht_config.supported_chan_width
1161 == IWL_CHANNEL_WIDTH_40MHZ)
1162 tbl->is_fat = 1;
1163 else
1164 tbl->is_fat = 0;
1166 /* FIXME: - don't toggle SGI here
1167 if (tbl->is_fat) {
1168 if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
1169 tbl->is_SGI = 1;
1170 else
1171 tbl->is_SGI = 0;
1172 } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
1173 tbl->is_SGI = 1;
1174 else
1175 tbl->is_SGI = 0;
1178 rs_set_expected_tpt_table(lq_sta, tbl);
1180 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
1182 IWL_DEBUG_RATE(priv, "LQ: MIMO2 best rate %d mask %X\n", rate, rate_mask);
1184 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1185 IWL_DEBUG_RATE(priv, "Can't switch with index %d rate mask %x\n",
1186 rate, rate_mask);
1187 return -1;
1189 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
1191 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
1192 tbl->current_rate, is_green);
1193 return 0;
1197 * Set up search table for SISO
1199 static int rs_switch_to_siso(struct iwl_priv *priv,
1200 struct iwl_lq_sta *lq_sta,
1201 struct ieee80211_conf *conf,
1202 struct ieee80211_sta *sta,
1203 struct iwl_scale_tbl_info *tbl, int index)
1205 u16 rate_mask;
1206 u8 is_green = lq_sta->is_green;
1207 s32 rate;
1209 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
1210 return -1;
1212 IWL_DEBUG_RATE(priv, "LQ: try to switch to SISO\n");
1214 tbl->is_dup = lq_sta->is_dup;
1215 tbl->lq_type = LQ_SISO;
1216 tbl->action = 0;
1217 rate_mask = lq_sta->active_siso_rate;
1219 if (priv->current_ht_config.supported_chan_width
1220 == IWL_CHANNEL_WIDTH_40MHZ)
1221 tbl->is_fat = 1;
1222 else
1223 tbl->is_fat = 0;
1225 /* FIXME: - don't toggle SGI here
1226 if (tbl->is_fat) {
1227 if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
1228 tbl->is_SGI = 1;
1229 else
1230 tbl->is_SGI = 0;
1231 } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
1232 tbl->is_SGI = 1;
1233 else
1234 tbl->is_SGI = 0;
1237 if (is_green)
1238 tbl->is_SGI = 0; /*11n spec: no SGI in SISO+Greenfield*/
1240 rs_set_expected_tpt_table(lq_sta, tbl);
1241 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
1243 IWL_DEBUG_RATE(priv, "LQ: get best rate %d mask %X\n", rate, rate_mask);
1244 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1245 IWL_DEBUG_RATE(priv, "can not switch with index %d rate mask %x\n",
1246 rate, rate_mask);
1247 return -1;
1249 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
1250 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
1251 tbl->current_rate, is_green);
1252 return 0;
1256 * Try to switch to new modulation mode from legacy
1258 static int rs_move_legacy_other(struct iwl_priv *priv,
1259 struct iwl_lq_sta *lq_sta,
1260 struct ieee80211_conf *conf,
1261 struct ieee80211_sta *sta,
1262 int index)
1264 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1265 struct iwl_scale_tbl_info *search_tbl =
1266 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1267 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1268 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1269 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1270 u8 start_action = tbl->action;
1271 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1272 u8 tx_chains_num = priv->hw_params.tx_chains_num;
1273 int ret = 0;
1275 for (; ;) {
1276 switch (tbl->action) {
1277 case IWL_LEGACY_SWITCH_ANTENNA1:
1278 case IWL_LEGACY_SWITCH_ANTENNA2:
1279 IWL_DEBUG_RATE(priv, "LQ: Legacy toggle Antenna\n");
1281 lq_sta->action_counter++;
1283 if ((tbl->action == IWL_LEGACY_SWITCH_ANTENNA1 &&
1284 tx_chains_num <= 1) ||
1285 (tbl->action == IWL_LEGACY_SWITCH_ANTENNA2 &&
1286 tx_chains_num <= 2))
1287 break;
1289 /* Don't change antenna if success has been great */
1290 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1291 break;
1293 /* Set up search table to try other antenna */
1294 memcpy(search_tbl, tbl, sz);
1296 if (rs_toggle_antenna(valid_tx_ant,
1297 &search_tbl->current_rate, search_tbl)) {
1298 rs_set_expected_tpt_table(lq_sta, search_tbl);
1299 goto out;
1301 break;
1302 case IWL_LEGACY_SWITCH_SISO:
1303 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to SISO\n");
1305 /* Set up search table to try SISO */
1306 memcpy(search_tbl, tbl, sz);
1307 search_tbl->is_SGI = 0;
1308 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
1309 search_tbl, index);
1310 if (!ret) {
1311 lq_sta->action_counter = 0;
1312 goto out;
1315 break;
1316 case IWL_LEGACY_SWITCH_MIMO2_AB:
1317 case IWL_LEGACY_SWITCH_MIMO2_AC:
1318 case IWL_LEGACY_SWITCH_MIMO2_BC:
1319 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to MIMO2\n");
1321 /* Set up search table to try MIMO */
1322 memcpy(search_tbl, tbl, sz);
1323 search_tbl->is_SGI = 0;
1325 if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AB)
1326 search_tbl->ant_type = ANT_AB;
1327 else if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AC)
1328 search_tbl->ant_type = ANT_AC;
1329 else
1330 search_tbl->ant_type = ANT_BC;
1332 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1333 break;
1335 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
1336 search_tbl, index);
1337 if (!ret) {
1338 lq_sta->action_counter = 0;
1339 goto out;
1341 break;
1343 tbl->action++;
1344 if (tbl->action > IWL_LEGACY_SWITCH_MIMO2_BC)
1345 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
1347 if (tbl->action == start_action)
1348 break;
1351 search_tbl->lq_type = LQ_NONE;
1352 return 0;
1354 out:
1355 lq_sta->search_better_tbl = 1;
1356 tbl->action++;
1357 if (tbl->action > IWL_LEGACY_SWITCH_MIMO2_BC)
1358 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
1359 return 0;
1364 * Try to switch to new modulation mode from SISO
1366 static int rs_move_siso_to_other(struct iwl_priv *priv,
1367 struct iwl_lq_sta *lq_sta,
1368 struct ieee80211_conf *conf,
1369 struct ieee80211_sta *sta, int index)
1371 u8 is_green = lq_sta->is_green;
1372 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1373 struct iwl_scale_tbl_info *search_tbl =
1374 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1375 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1376 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1377 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1378 u8 start_action = tbl->action;
1379 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1380 u8 tx_chains_num = priv->hw_params.tx_chains_num;
1381 int ret;
1383 for (;;) {
1384 lq_sta->action_counter++;
1385 switch (tbl->action) {
1386 case IWL_SISO_SWITCH_ANTENNA1:
1387 case IWL_SISO_SWITCH_ANTENNA2:
1388 IWL_DEBUG_RATE(priv, "LQ: SISO toggle Antenna\n");
1390 if ((tbl->action == IWL_SISO_SWITCH_ANTENNA1 &&
1391 tx_chains_num <= 1) ||
1392 (tbl->action == IWL_SISO_SWITCH_ANTENNA2 &&
1393 tx_chains_num <= 2))
1394 break;
1396 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1397 break;
1399 memcpy(search_tbl, tbl, sz);
1400 if (rs_toggle_antenna(valid_tx_ant,
1401 &search_tbl->current_rate, search_tbl))
1402 goto out;
1403 break;
1404 case IWL_SISO_SWITCH_MIMO2_AB:
1405 case IWL_SISO_SWITCH_MIMO2_AC:
1406 case IWL_SISO_SWITCH_MIMO2_BC:
1407 IWL_DEBUG_RATE(priv, "LQ: SISO switch to MIMO2\n");
1408 memcpy(search_tbl, tbl, sz);
1409 search_tbl->is_SGI = 0;
1411 if (tbl->action == IWL_SISO_SWITCH_MIMO2_AB)
1412 search_tbl->ant_type = ANT_AB;
1413 else if (tbl->action == IWL_SISO_SWITCH_MIMO2_AC)
1414 search_tbl->ant_type = ANT_AC;
1415 else
1416 search_tbl->ant_type = ANT_BC;
1418 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1419 break;
1421 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
1422 search_tbl, index);
1423 if (!ret)
1424 goto out;
1425 break;
1426 case IWL_SISO_SWITCH_GI:
1427 if (!tbl->is_fat &&
1428 !(priv->current_ht_config.sgf &
1429 HT_SHORT_GI_20MHZ))
1430 break;
1431 if (tbl->is_fat &&
1432 !(priv->current_ht_config.sgf &
1433 HT_SHORT_GI_40MHZ))
1434 break;
1436 IWL_DEBUG_RATE(priv, "LQ: SISO toggle SGI/NGI\n");
1438 memcpy(search_tbl, tbl, sz);
1439 if (is_green) {
1440 if (!tbl->is_SGI)
1441 break;
1442 else
1443 IWL_ERR(priv,
1444 "SGI was set in GF+SISO\n");
1446 search_tbl->is_SGI = !tbl->is_SGI;
1447 rs_set_expected_tpt_table(lq_sta, search_tbl);
1448 if (tbl->is_SGI) {
1449 s32 tpt = lq_sta->last_tpt / 100;
1450 if (tpt >= search_tbl->expected_tpt[index])
1451 break;
1453 search_tbl->current_rate =
1454 rate_n_flags_from_tbl(priv, search_tbl,
1455 index, is_green);
1456 goto out;
1458 tbl->action++;
1459 if (tbl->action > IWL_SISO_SWITCH_GI)
1460 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1462 if (tbl->action == start_action)
1463 break;
1465 search_tbl->lq_type = LQ_NONE;
1466 return 0;
1468 out:
1469 lq_sta->search_better_tbl = 1;
1470 tbl->action++;
1471 if (tbl->action > IWL_SISO_SWITCH_GI)
1472 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1473 return 0;
1477 * Try to switch to new modulation mode from MIMO
1479 static int rs_move_mimo_to_other(struct iwl_priv *priv,
1480 struct iwl_lq_sta *lq_sta,
1481 struct ieee80211_conf *conf,
1482 struct ieee80211_sta *sta, int index)
1484 s8 is_green = lq_sta->is_green;
1485 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1486 struct iwl_scale_tbl_info *search_tbl =
1487 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1488 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1489 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1490 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1491 u8 start_action = tbl->action;
1492 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1493 u8 tx_chains_num = priv->hw_params.tx_chains_num;
1494 int ret;
1496 for (;;) {
1497 lq_sta->action_counter++;
1498 switch (tbl->action) {
1499 case IWL_MIMO2_SWITCH_ANTENNA1:
1500 case IWL_MIMO2_SWITCH_ANTENNA2:
1501 IWL_DEBUG_RATE(priv, "LQ: MIMO toggle Antennas\n");
1503 if (tx_chains_num <= 2)
1504 break;
1506 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1507 break;
1509 memcpy(search_tbl, tbl, sz);
1510 if (rs_toggle_antenna(valid_tx_ant,
1511 &search_tbl->current_rate, search_tbl))
1512 goto out;
1513 break;
1514 case IWL_MIMO2_SWITCH_SISO_A:
1515 case IWL_MIMO2_SWITCH_SISO_B:
1516 case IWL_MIMO2_SWITCH_SISO_C:
1517 IWL_DEBUG_RATE(priv, "LQ: MIMO2 switch to SISO\n");
1519 /* Set up new search table for SISO */
1520 memcpy(search_tbl, tbl, sz);
1522 if (tbl->action == IWL_MIMO2_SWITCH_SISO_A)
1523 search_tbl->ant_type = ANT_A;
1524 else if (tbl->action == IWL_MIMO2_SWITCH_SISO_B)
1525 search_tbl->ant_type = ANT_B;
1526 else
1527 search_tbl->ant_type = ANT_C;
1529 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1530 break;
1532 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
1533 search_tbl, index);
1534 if (!ret)
1535 goto out;
1537 break;
1539 case IWL_MIMO2_SWITCH_GI:
1540 if (!tbl->is_fat &&
1541 !(priv->current_ht_config.sgf &
1542 HT_SHORT_GI_20MHZ))
1543 break;
1544 if (tbl->is_fat &&
1545 !(priv->current_ht_config.sgf &
1546 HT_SHORT_GI_40MHZ))
1547 break;
1549 IWL_DEBUG_RATE(priv, "LQ: MIMO toggle SGI/NGI\n");
1551 /* Set up new search table for MIMO */
1552 memcpy(search_tbl, tbl, sz);
1553 search_tbl->is_SGI = !tbl->is_SGI;
1554 rs_set_expected_tpt_table(lq_sta, search_tbl);
1556 * If active table already uses the fastest possible
1557 * modulation (dual stream with short guard interval),
1558 * and it's working well, there's no need to look
1559 * for a better type of modulation!
1561 if (tbl->is_SGI) {
1562 s32 tpt = lq_sta->last_tpt / 100;
1563 if (tpt >= search_tbl->expected_tpt[index])
1564 break;
1566 search_tbl->current_rate =
1567 rate_n_flags_from_tbl(priv, search_tbl,
1568 index, is_green);
1569 goto out;
1572 tbl->action++;
1573 if (tbl->action > IWL_MIMO2_SWITCH_GI)
1574 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
1576 if (tbl->action == start_action)
1577 break;
1579 search_tbl->lq_type = LQ_NONE;
1580 return 0;
1581 out:
1582 lq_sta->search_better_tbl = 1;
1583 tbl->action++;
1584 if (tbl->action > IWL_MIMO2_SWITCH_GI)
1585 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
1586 return 0;
1591 * Check whether we should continue using same modulation mode, or
1592 * begin search for a new mode, based on:
1593 * 1) # tx successes or failures while using this mode
1594 * 2) # times calling this function
1595 * 3) elapsed time in this mode (not used, for now)
1597 static void rs_stay_in_table(struct iwl_lq_sta *lq_sta)
1599 struct iwl_scale_tbl_info *tbl;
1600 int i;
1601 int active_tbl;
1602 int flush_interval_passed = 0;
1603 struct iwl_priv *priv;
1605 priv = lq_sta->drv;
1606 active_tbl = lq_sta->active_tbl;
1608 tbl = &(lq_sta->lq_info[active_tbl]);
1610 /* If we've been disallowing search, see if we should now allow it */
1611 if (lq_sta->stay_in_tbl) {
1613 /* Elapsed time using current modulation mode */
1614 if (lq_sta->flush_timer)
1615 flush_interval_passed =
1616 time_after(jiffies,
1617 (unsigned long)(lq_sta->flush_timer +
1618 IWL_RATE_SCALE_FLUSH_INTVL));
1621 * Check if we should allow search for new modulation mode.
1622 * If many frames have failed or succeeded, or we've used
1623 * this same modulation for a long time, allow search, and
1624 * reset history stats that keep track of whether we should
1625 * allow a new search. Also (below) reset all bitmaps and
1626 * stats in active history.
1628 if ((lq_sta->total_failed > lq_sta->max_failure_limit) ||
1629 (lq_sta->total_success > lq_sta->max_success_limit) ||
1630 ((!lq_sta->search_better_tbl) && (lq_sta->flush_timer)
1631 && (flush_interval_passed))) {
1632 IWL_DEBUG_RATE(priv, "LQ: stay is expired %d %d %d\n:",
1633 lq_sta->total_failed,
1634 lq_sta->total_success,
1635 flush_interval_passed);
1637 /* Allow search for new mode */
1638 lq_sta->stay_in_tbl = 0; /* only place reset */
1639 lq_sta->total_failed = 0;
1640 lq_sta->total_success = 0;
1641 lq_sta->flush_timer = 0;
1644 * Else if we've used this modulation mode enough repetitions
1645 * (regardless of elapsed time or success/failure), reset
1646 * history bitmaps and rate-specific stats for all rates in
1647 * active table.
1649 } else {
1650 lq_sta->table_count++;
1651 if (lq_sta->table_count >=
1652 lq_sta->table_count_limit) {
1653 lq_sta->table_count = 0;
1655 IWL_DEBUG_RATE(priv, "LQ: stay in table clear win\n");
1656 for (i = 0; i < IWL_RATE_COUNT; i++)
1657 rs_rate_scale_clear_window(
1658 &(tbl->win[i]));
1662 /* If transitioning to allow "search", reset all history
1663 * bitmaps and stats in active table (this will become the new
1664 * "search" table). */
1665 if (!lq_sta->stay_in_tbl) {
1666 for (i = 0; i < IWL_RATE_COUNT; i++)
1667 rs_rate_scale_clear_window(&(tbl->win[i]));
1673 * Do rate scaling and search for new modulation mode.
1675 static void rs_rate_scale_perform(struct iwl_priv *priv,
1676 struct ieee80211_hdr *hdr,
1677 struct ieee80211_sta *sta,
1678 struct iwl_lq_sta *lq_sta)
1680 struct ieee80211_hw *hw = priv->hw;
1681 struct ieee80211_conf *conf = &hw->conf;
1682 int low = IWL_RATE_INVALID;
1683 int high = IWL_RATE_INVALID;
1684 int index;
1685 int i;
1686 struct iwl_rate_scale_data *window = NULL;
1687 int current_tpt = IWL_INVALID_VALUE;
1688 int low_tpt = IWL_INVALID_VALUE;
1689 int high_tpt = IWL_INVALID_VALUE;
1690 u32 fail_count;
1691 s8 scale_action = 0;
1692 u16 rate_mask;
1693 u8 update_lq = 0;
1694 struct iwl_scale_tbl_info *tbl, *tbl1;
1695 u16 rate_scale_index_msk = 0;
1696 u32 rate;
1697 u8 is_green = 0;
1698 u8 active_tbl = 0;
1699 u8 done_search = 0;
1700 u16 high_low;
1701 s32 sr;
1702 u8 tid = MAX_TID_COUNT;
1703 struct iwl_tid_data *tid_data;
1705 IWL_DEBUG_RATE(priv, "rate scale calculate new rate for skb\n");
1707 /* Send management frames and broadcast/multicast data using
1708 * lowest rate. */
1709 /* TODO: this could probably be improved.. */
1710 if (!ieee80211_is_data(hdr->frame_control) ||
1711 is_multicast_ether_addr(hdr->addr1))
1712 return;
1714 if (!sta || !lq_sta)
1715 return;
1717 lq_sta->supp_rates = sta->supp_rates[lq_sta->band];
1719 tid = rs_tl_add_packet(lq_sta, hdr);
1722 * Select rate-scale / modulation-mode table to work with in
1723 * the rest of this function: "search" if searching for better
1724 * modulation mode, or "active" if doing rate scaling within a mode.
1726 if (!lq_sta->search_better_tbl)
1727 active_tbl = lq_sta->active_tbl;
1728 else
1729 active_tbl = 1 - lq_sta->active_tbl;
1731 tbl = &(lq_sta->lq_info[active_tbl]);
1732 is_green = lq_sta->is_green;
1734 /* current tx rate */
1735 index = lq_sta->last_txrate_idx;
1737 IWL_DEBUG_RATE(priv, "Rate scale index %d for type %d\n", index,
1738 tbl->lq_type);
1740 /* rates available for this association, and for modulation mode */
1741 rate_mask = rs_get_supported_rates(lq_sta, hdr, tbl->lq_type);
1743 IWL_DEBUG_RATE(priv, "mask 0x%04X \n", rate_mask);
1745 /* mask with station rate restriction */
1746 if (is_legacy(tbl->lq_type)) {
1747 if (lq_sta->band == IEEE80211_BAND_5GHZ)
1748 /* supp_rates has no CCK bits in A mode */
1749 rate_scale_index_msk = (u16) (rate_mask &
1750 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
1751 else
1752 rate_scale_index_msk = (u16) (rate_mask &
1753 lq_sta->supp_rates);
1755 } else
1756 rate_scale_index_msk = rate_mask;
1758 if (!rate_scale_index_msk)
1759 rate_scale_index_msk = rate_mask;
1761 if (!((1 << index) & rate_scale_index_msk)) {
1762 IWL_ERR(priv, "Current Rate is not valid\n");
1763 return;
1766 /* Get expected throughput table and history window for current rate */
1767 if (!tbl->expected_tpt) {
1768 IWL_ERR(priv, "tbl->expected_tpt is NULL\n");
1769 return;
1772 /* force user max rate if set by user */
1773 if ((lq_sta->max_rate_idx != -1) &&
1774 (lq_sta->max_rate_idx < index)) {
1775 index = lq_sta->max_rate_idx;
1776 update_lq = 1;
1777 window = &(tbl->win[index]);
1778 goto lq_update;
1781 window = &(tbl->win[index]);
1784 * If there is not enough history to calculate actual average
1785 * throughput, keep analyzing results of more tx frames, without
1786 * changing rate or mode (bypass most of the rest of this function).
1787 * Set up new rate table in uCode only if old rate is not supported
1788 * in current association (use new rate found above).
1790 fail_count = window->counter - window->success_counter;
1791 if ((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
1792 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH)) {
1793 IWL_DEBUG_RATE(priv, "LQ: still below TH. succ=%d total=%d "
1794 "for index %d\n",
1795 window->success_counter, window->counter, index);
1797 /* Can't calculate this yet; not enough history */
1798 window->average_tpt = IWL_INVALID_VALUE;
1800 /* Should we stay with this modulation mode,
1801 * or search for a new one? */
1802 rs_stay_in_table(lq_sta);
1804 goto out;
1807 /* Else we have enough samples; calculate estimate of
1808 * actual average throughput */
1810 BUG_ON(window->average_tpt != ((window->success_ratio *
1811 tbl->expected_tpt[index] + 64) / 128));
1813 /* If we are searching for better modulation mode, check success. */
1814 if (lq_sta->search_better_tbl) {
1816 /* If good success, continue using the "search" mode;
1817 * no need to send new link quality command, since we're
1818 * continuing to use the setup that we've been trying. */
1819 if (window->average_tpt > lq_sta->last_tpt) {
1821 IWL_DEBUG_RATE(priv, "LQ: SWITCHING TO NEW TABLE "
1822 "suc=%d cur-tpt=%d old-tpt=%d\n",
1823 window->success_ratio,
1824 window->average_tpt,
1825 lq_sta->last_tpt);
1827 if (!is_legacy(tbl->lq_type))
1828 lq_sta->enable_counter = 1;
1830 /* Swap tables; "search" becomes "active" */
1831 lq_sta->active_tbl = active_tbl;
1832 current_tpt = window->average_tpt;
1834 /* Else poor success; go back to mode in "active" table */
1835 } else {
1837 IWL_DEBUG_RATE(priv, "LQ: GOING BACK TO THE OLD TABLE "
1838 "suc=%d cur-tpt=%d old-tpt=%d\n",
1839 window->success_ratio,
1840 window->average_tpt,
1841 lq_sta->last_tpt);
1843 /* Nullify "search" table */
1844 tbl->lq_type = LQ_NONE;
1846 /* Revert to "active" table */
1847 active_tbl = lq_sta->active_tbl;
1848 tbl = &(lq_sta->lq_info[active_tbl]);
1850 /* Revert to "active" rate and throughput info */
1851 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
1852 current_tpt = lq_sta->last_tpt;
1854 /* Need to set up a new rate table in uCode */
1855 update_lq = 1;
1858 /* Either way, we've made a decision; modulation mode
1859 * search is done, allow rate adjustment next time. */
1860 lq_sta->search_better_tbl = 0;
1861 done_search = 1; /* Don't switch modes below! */
1862 goto lq_update;
1865 /* (Else) not in search of better modulation mode, try for better
1866 * starting rate, while staying in this mode. */
1867 high_low = rs_get_adjacent_rate(priv, index, rate_scale_index_msk,
1868 tbl->lq_type);
1869 low = high_low & 0xff;
1870 high = (high_low >> 8) & 0xff;
1872 /* If user set max rate, dont allow higher than user constrain */
1873 if ((lq_sta->max_rate_idx != -1) &&
1874 (lq_sta->max_rate_idx < high))
1875 high = IWL_RATE_INVALID;
1877 sr = window->success_ratio;
1879 /* Collect measured throughputs for current and adjacent rates */
1880 current_tpt = window->average_tpt;
1881 if (low != IWL_RATE_INVALID)
1882 low_tpt = tbl->win[low].average_tpt;
1883 if (high != IWL_RATE_INVALID)
1884 high_tpt = tbl->win[high].average_tpt;
1886 scale_action = 0;
1888 /* Too many failures, decrease rate */
1889 if ((sr <= IWL_RATE_DECREASE_TH) || (current_tpt == 0)) {
1890 IWL_DEBUG_RATE(priv, "decrease rate because of low success_ratio\n");
1891 scale_action = -1;
1893 /* No throughput measured yet for adjacent rates; try increase. */
1894 } else if ((low_tpt == IWL_INVALID_VALUE) &&
1895 (high_tpt == IWL_INVALID_VALUE)) {
1897 if (high != IWL_RATE_INVALID && sr >= IWL_RATE_INCREASE_TH)
1898 scale_action = 1;
1899 else if (low != IWL_RATE_INVALID)
1900 scale_action = -1;
1903 /* Both adjacent throughputs are measured, but neither one has better
1904 * throughput; we're using the best rate, don't change it! */
1905 else if ((low_tpt != IWL_INVALID_VALUE) &&
1906 (high_tpt != IWL_INVALID_VALUE) &&
1907 (low_tpt < current_tpt) &&
1908 (high_tpt < current_tpt))
1909 scale_action = 0;
1911 /* At least one adjacent rate's throughput is measured,
1912 * and may have better performance. */
1913 else {
1914 /* Higher adjacent rate's throughput is measured */
1915 if (high_tpt != IWL_INVALID_VALUE) {
1916 /* Higher rate has better throughput */
1917 if (high_tpt > current_tpt &&
1918 sr >= IWL_RATE_INCREASE_TH) {
1919 scale_action = 1;
1920 } else {
1921 IWL_DEBUG_RATE(priv,
1922 "decrease rate because of high tpt\n");
1923 scale_action = -1;
1926 /* Lower adjacent rate's throughput is measured */
1927 } else if (low_tpt != IWL_INVALID_VALUE) {
1928 /* Lower rate has better throughput */
1929 if (low_tpt > current_tpt) {
1930 IWL_DEBUG_RATE(priv,
1931 "decrease rate because of low tpt\n");
1932 scale_action = -1;
1933 } else if (sr >= IWL_RATE_INCREASE_TH) {
1934 scale_action = 1;
1939 /* Sanity check; asked for decrease, but success rate or throughput
1940 * has been good at old rate. Don't change it. */
1941 if ((scale_action == -1) && (low != IWL_RATE_INVALID) &&
1942 ((sr > IWL_RATE_HIGH_TH) ||
1943 (current_tpt > (100 * tbl->expected_tpt[low]))))
1944 scale_action = 0;
1946 switch (scale_action) {
1947 case -1:
1948 /* Decrease starting rate, update uCode's rate table */
1949 if (low != IWL_RATE_INVALID) {
1950 update_lq = 1;
1951 index = low;
1953 break;
1954 case 1:
1955 /* Increase starting rate, update uCode's rate table */
1956 if (high != IWL_RATE_INVALID) {
1957 update_lq = 1;
1958 index = high;
1961 break;
1962 case 0:
1963 /* No change */
1964 default:
1965 break;
1968 IWL_DEBUG_RATE(priv, "choose rate scale index %d action %d low %d "
1969 "high %d type %d\n",
1970 index, scale_action, low, high, tbl->lq_type);
1972 lq_update:
1973 /* Replace uCode's rate table for the destination station. */
1974 if (update_lq) {
1975 rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
1976 rs_fill_link_cmd(priv, lq_sta, rate);
1977 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
1980 /* Should we stay with this modulation mode, or search for a new one? */
1981 rs_stay_in_table(lq_sta);
1984 * Search for new modulation mode if we're:
1985 * 1) Not changing rates right now
1986 * 2) Not just finishing up a search
1987 * 3) Allowing a new search
1989 if (!update_lq && !done_search && !lq_sta->stay_in_tbl && window->counter) {
1990 /* Save current throughput to compare with "search" throughput*/
1991 lq_sta->last_tpt = current_tpt;
1993 /* Select a new "search" modulation mode to try.
1994 * If one is found, set up the new "search" table. */
1995 if (is_legacy(tbl->lq_type))
1996 rs_move_legacy_other(priv, lq_sta, conf, sta, index);
1997 else if (is_siso(tbl->lq_type))
1998 rs_move_siso_to_other(priv, lq_sta, conf, sta, index);
1999 else
2000 rs_move_mimo_to_other(priv, lq_sta, conf, sta, index);
2002 /* If new "search" mode was selected, set up in uCode table */
2003 if (lq_sta->search_better_tbl) {
2004 /* Access the "search" table, clear its history. */
2005 tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
2006 for (i = 0; i < IWL_RATE_COUNT; i++)
2007 rs_rate_scale_clear_window(&(tbl->win[i]));
2009 /* Use new "search" start rate */
2010 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
2012 IWL_DEBUG_RATE(priv, "Switch current mcs: %X index: %d\n",
2013 tbl->current_rate, index);
2014 rs_fill_link_cmd(priv, lq_sta, tbl->current_rate);
2015 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
2018 /* If the "active" (non-search) mode was legacy,
2019 * and we've tried switching antennas,
2020 * but we haven't been able to try HT modes (not available),
2021 * stay with best antenna legacy modulation for a while
2022 * before next round of mode comparisons. */
2023 tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
2024 if (is_legacy(tbl1->lq_type) && !conf_is_ht(conf) &&
2025 lq_sta->action_counter >= 1) {
2026 lq_sta->action_counter = 0;
2027 IWL_DEBUG_RATE(priv, "LQ: STAY in legacy table\n");
2028 rs_set_stay_in_table(priv, 1, lq_sta);
2031 /* If we're in an HT mode, and all 3 mode switch actions
2032 * have been tried and compared, stay in this best modulation
2033 * mode for a while before next round of mode comparisons. */
2034 if (lq_sta->enable_counter &&
2035 (lq_sta->action_counter >= IWL_ACTION_LIMIT)) {
2036 if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
2037 (lq_sta->tx_agg_tid_en & (1 << tid)) &&
2038 (tid != MAX_TID_COUNT)) {
2039 tid_data =
2040 &priv->stations[lq_sta->lq.sta_id].tid[tid];
2041 if (tid_data->agg.state == IWL_AGG_OFF) {
2042 IWL_DEBUG_RATE(priv,
2043 "try to aggregate tid %d\n",
2044 tid);
2045 rs_tl_turn_on_agg(priv, tid,
2046 lq_sta, sta);
2049 lq_sta->action_counter = 0;
2050 rs_set_stay_in_table(priv, 0, lq_sta);
2054 * Else, don't search for a new modulation mode.
2055 * Put new timestamp in stay-in-modulation-mode flush timer if:
2056 * 1) Not changing rates right now
2057 * 2) Not just finishing up a search
2058 * 3) flush timer is empty
2060 } else {
2061 if ((!update_lq) && (!done_search) && (!lq_sta->flush_timer))
2062 lq_sta->flush_timer = jiffies;
2065 out:
2066 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
2067 i = index;
2068 lq_sta->last_txrate_idx = i;
2070 return;
2074 static void rs_initialize_lq(struct iwl_priv *priv,
2075 struct ieee80211_conf *conf,
2076 struct ieee80211_sta *sta,
2077 struct iwl_lq_sta *lq_sta)
2079 struct iwl_scale_tbl_info *tbl;
2080 int rate_idx;
2081 int i;
2082 u32 rate;
2083 u8 use_green = rs_use_green(priv, conf);
2084 u8 active_tbl = 0;
2085 u8 valid_tx_ant;
2087 if (!sta || !lq_sta)
2088 goto out;
2090 i = lq_sta->last_txrate_idx;
2092 if ((lq_sta->lq.sta_id == 0xff) &&
2093 (priv->iw_mode == NL80211_IFTYPE_ADHOC))
2094 goto out;
2096 valid_tx_ant = priv->hw_params.valid_tx_ant;
2098 if (!lq_sta->search_better_tbl)
2099 active_tbl = lq_sta->active_tbl;
2100 else
2101 active_tbl = 1 - lq_sta->active_tbl;
2103 tbl = &(lq_sta->lq_info[active_tbl]);
2105 if ((i < 0) || (i >= IWL_RATE_COUNT))
2106 i = 0;
2108 rate = iwl_rates[i].plcp;
2109 tbl->ant_type = first_antenna(valid_tx_ant);
2110 rate |= tbl->ant_type << RATE_MCS_ANT_POS;
2112 if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
2113 rate |= RATE_MCS_CCK_MSK;
2115 rs_get_tbl_info_from_mcs(rate, priv->band, tbl, &rate_idx);
2116 if (!rs_is_valid_ant(valid_tx_ant, tbl->ant_type))
2117 rs_toggle_antenna(valid_tx_ant, &rate, tbl);
2119 rate = rate_n_flags_from_tbl(priv, tbl, rate_idx, use_green);
2120 tbl->current_rate = rate;
2121 rs_set_expected_tpt_table(lq_sta, tbl);
2122 rs_fill_link_cmd(NULL, lq_sta, rate);
2123 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
2124 out:
2125 return;
2128 static void rs_get_rate(void *priv_r, struct ieee80211_sta *sta, void *priv_sta,
2129 struct ieee80211_tx_rate_control *txrc)
2132 struct sk_buff *skb = txrc->skb;
2133 struct ieee80211_supported_band *sband = txrc->sband;
2134 struct iwl_priv *priv = (struct iwl_priv *)priv_r;
2135 struct ieee80211_conf *conf = &priv->hw->conf;
2136 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2137 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2138 struct iwl_lq_sta *lq_sta = priv_sta;
2139 int rate_idx;
2140 u64 mask_bit = 0;
2142 IWL_DEBUG_RATE_LIMIT(priv, "rate scale calculate new rate for skb\n");
2144 /* Get max rate if user set max rate */
2145 if (lq_sta) {
2146 lq_sta->max_rate_idx = txrc->max_rate_idx;
2147 if ((sband->band == IEEE80211_BAND_5GHZ) &&
2148 (lq_sta->max_rate_idx != -1))
2149 lq_sta->max_rate_idx += IWL_FIRST_OFDM_RATE;
2150 if ((lq_sta->max_rate_idx < 0) ||
2151 (lq_sta->max_rate_idx >= IWL_RATE_COUNT))
2152 lq_sta->max_rate_idx = -1;
2155 if (sta)
2156 mask_bit = sta->supp_rates[sband->band];
2158 /* Send management frames and broadcast/multicast data using lowest
2159 * rate. */
2160 if (!ieee80211_is_data(hdr->frame_control) ||
2161 is_multicast_ether_addr(hdr->addr1) || !sta || !lq_sta) {
2162 if (!mask_bit)
2163 info->control.rates[0].idx =
2164 rate_lowest_index(sband, NULL);
2165 else
2166 info->control.rates[0].idx =
2167 rate_lowest_index(sband, sta);
2168 return;
2171 rate_idx = lq_sta->last_txrate_idx;
2173 if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) &&
2174 !lq_sta->ibss_sta_added) {
2175 u8 sta_id = iwl_find_station(priv, hdr->addr1);
2177 if (sta_id == IWL_INVALID_STATION) {
2178 IWL_DEBUG_RATE(priv, "LQ: ADD station %pM\n",
2179 hdr->addr1);
2180 sta_id = iwl_add_station_flags(priv, hdr->addr1,
2181 0, CMD_ASYNC, NULL);
2183 if ((sta_id != IWL_INVALID_STATION)) {
2184 lq_sta->lq.sta_id = sta_id;
2185 lq_sta->lq.rs_table[0].rate_n_flags = 0;
2186 lq_sta->ibss_sta_added = 1;
2187 rs_initialize_lq(priv, conf, sta, lq_sta);
2191 if (rate_idx < 0 || rate_idx > IWL_RATE_COUNT)
2192 rate_idx = rate_lowest_index(sband, sta);
2193 else if (sband->band == IEEE80211_BAND_5GHZ)
2194 rate_idx -= IWL_FIRST_OFDM_RATE;
2196 info->control.rates[0].idx = rate_idx;
2199 static void *rs_alloc_sta(void *priv_rate, struct ieee80211_sta *sta,
2200 gfp_t gfp)
2202 struct iwl_lq_sta *lq_sta;
2203 struct iwl_priv *priv;
2204 int i, j;
2206 priv = (struct iwl_priv *)priv_rate;
2207 IWL_DEBUG_RATE(priv, "create station rate scale window\n");
2209 lq_sta = kzalloc(sizeof(struct iwl_lq_sta), gfp);
2211 if (lq_sta == NULL)
2212 return NULL;
2213 lq_sta->lq.sta_id = 0xff;
2216 for (j = 0; j < LQ_SIZE; j++)
2217 for (i = 0; i < IWL_RATE_COUNT; i++)
2218 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
2220 return lq_sta;
2223 static void rs_rate_init(void *priv_r, struct ieee80211_supported_band *sband,
2224 struct ieee80211_sta *sta, void *priv_sta)
2226 int i, j;
2227 struct iwl_priv *priv = (struct iwl_priv *)priv_r;
2228 struct ieee80211_conf *conf = &priv->hw->conf;
2229 struct iwl_lq_sta *lq_sta = priv_sta;
2230 u16 mask_bit = 0;
2231 int count;
2232 int start_rate = 0;
2234 lq_sta->flush_timer = 0;
2235 lq_sta->supp_rates = sta->supp_rates[sband->band];
2236 for (j = 0; j < LQ_SIZE; j++)
2237 for (i = 0; i < IWL_RATE_COUNT; i++)
2238 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
2240 IWL_DEBUG_RATE(priv, "LQ: *** rate scale station global init ***\n");
2241 /* TODO: what is a good starting rate for STA? About middle? Maybe not
2242 * the lowest or the highest rate.. Could consider using RSSI from
2243 * previous packets? Need to have IEEE 802.1X auth succeed immediately
2244 * after assoc.. */
2246 lq_sta->ibss_sta_added = 0;
2247 if (priv->iw_mode == NL80211_IFTYPE_AP) {
2248 u8 sta_id = iwl_find_station(priv, sta->addr);
2250 /* for IBSS the call are from tasklet */
2251 IWL_DEBUG_RATE(priv, "LQ: ADD station %pM\n", sta->addr);
2253 if (sta_id == IWL_INVALID_STATION) {
2254 IWL_DEBUG_RATE(priv, "LQ: ADD station %pM\n", sta->addr);
2255 sta_id = iwl_add_station_flags(priv, sta->addr,
2256 0, CMD_ASYNC, NULL);
2258 if ((sta_id != IWL_INVALID_STATION)) {
2259 lq_sta->lq.sta_id = sta_id;
2260 lq_sta->lq.rs_table[0].rate_n_flags = 0;
2262 /* FIXME: this is w/a remove it later */
2263 priv->assoc_station_added = 1;
2266 lq_sta->is_dup = 0;
2267 lq_sta->max_rate_idx = -1;
2268 lq_sta->missed_rate_counter = IWL_MISSED_RATE_MAX;
2269 lq_sta->is_green = rs_use_green(priv, conf);
2270 lq_sta->active_legacy_rate = priv->active_rate & ~(0x1000);
2271 lq_sta->active_rate_basic = priv->active_rate_basic;
2272 lq_sta->band = priv->band;
2274 * active_siso_rate mask includes 9 MBits (bit 5), and CCK (bits 0-3),
2275 * supp_rates[] does not; shift to convert format, force 9 MBits off.
2277 lq_sta->active_siso_rate = sta->ht_cap.mcs.rx_mask[0] << 1;
2278 lq_sta->active_siso_rate |= sta->ht_cap.mcs.rx_mask[0] & 0x1;
2279 lq_sta->active_siso_rate &= ~((u16)0x2);
2280 lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE;
2282 /* Same here */
2283 lq_sta->active_mimo2_rate = sta->ht_cap.mcs.rx_mask[1] << 1;
2284 lq_sta->active_mimo2_rate |= sta->ht_cap.mcs.rx_mask[1] & 0x1;
2285 lq_sta->active_mimo2_rate &= ~((u16)0x2);
2286 lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE;
2288 lq_sta->active_mimo3_rate = sta->ht_cap.mcs.rx_mask[2] << 1;
2289 lq_sta->active_mimo3_rate |= sta->ht_cap.mcs.rx_mask[2] & 0x1;
2290 lq_sta->active_mimo3_rate &= ~((u16)0x2);
2291 lq_sta->active_mimo3_rate <<= IWL_FIRST_OFDM_RATE;
2293 IWL_DEBUG_RATE(priv, "SISO-RATE=%X MIMO2-RATE=%X MIMO3-RATE=%X\n",
2294 lq_sta->active_siso_rate,
2295 lq_sta->active_mimo2_rate,
2296 lq_sta->active_mimo3_rate);
2298 /* These values will be overridden later */
2299 lq_sta->lq.general_params.single_stream_ant_msk = ANT_A;
2300 lq_sta->lq.general_params.dual_stream_ant_msk = ANT_AB;
2302 /* as default allow aggregation for all tids */
2303 lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
2304 lq_sta->drv = priv;
2306 /* Find highest tx rate supported by hardware and destination station */
2307 mask_bit = sta->supp_rates[sband->band];
2308 count = sband->n_bitrates;
2309 if (sband->band == IEEE80211_BAND_5GHZ) {
2310 count += IWL_FIRST_OFDM_RATE;
2311 start_rate = IWL_FIRST_OFDM_RATE;
2312 mask_bit <<= IWL_FIRST_OFDM_RATE;
2315 mask_bit = mask_bit & lq_sta->active_legacy_rate;
2316 lq_sta->last_txrate_idx = 4;
2317 for (i = start_rate; i < count; i++)
2318 if (mask_bit & BIT(i))
2319 lq_sta->last_txrate_idx = i;
2321 rs_initialize_lq(priv, conf, sta, lq_sta);
2324 static void rs_fill_link_cmd(const struct iwl_priv *priv,
2325 struct iwl_lq_sta *lq_sta, u32 new_rate)
2327 struct iwl_scale_tbl_info tbl_type;
2328 int index = 0;
2329 int rate_idx;
2330 int repeat_rate = 0;
2331 u8 ant_toggle_cnt = 0;
2332 u8 use_ht_possible = 1;
2333 u8 valid_tx_ant = 0;
2334 struct iwl_link_quality_cmd *lq_cmd = &lq_sta->lq;
2336 /* Override starting rate (index 0) if needed for debug purposes */
2337 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
2339 /* Interpret new_rate (rate_n_flags) */
2340 memset(&tbl_type, 0, sizeof(tbl_type));
2341 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band,
2342 &tbl_type, &rate_idx);
2344 /* How many times should we repeat the initial rate? */
2345 if (is_legacy(tbl_type.lq_type)) {
2346 ant_toggle_cnt = 1;
2347 repeat_rate = IWL_NUMBER_TRY;
2348 } else {
2349 repeat_rate = IWL_HT_NUMBER_TRY;
2352 lq_cmd->general_params.mimo_delimiter =
2353 is_mimo(tbl_type.lq_type) ? 1 : 0;
2355 /* Fill 1st table entry (index 0) */
2356 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
2358 if (num_of_ant(tbl_type.ant_type) == 1) {
2359 lq_cmd->general_params.single_stream_ant_msk =
2360 tbl_type.ant_type;
2361 } else if (num_of_ant(tbl_type.ant_type) == 2) {
2362 lq_cmd->general_params.dual_stream_ant_msk =
2363 tbl_type.ant_type;
2364 } /* otherwise we don't modify the existing value */
2366 index++;
2367 repeat_rate--;
2369 if (priv)
2370 valid_tx_ant = priv->hw_params.valid_tx_ant;
2372 /* Fill rest of rate table */
2373 while (index < LINK_QUAL_MAX_RETRY_NUM) {
2374 /* Repeat initial/next rate.
2375 * For legacy IWL_NUMBER_TRY == 1, this loop will not execute.
2376 * For HT IWL_HT_NUMBER_TRY == 3, this executes twice. */
2377 while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
2378 if (is_legacy(tbl_type.lq_type)) {
2379 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2380 ant_toggle_cnt++;
2381 else if (priv &&
2382 rs_toggle_antenna(valid_tx_ant,
2383 &new_rate, &tbl_type))
2384 ant_toggle_cnt = 1;
2387 /* Override next rate if needed for debug purposes */
2388 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
2390 /* Fill next table entry */
2391 lq_cmd->rs_table[index].rate_n_flags =
2392 cpu_to_le32(new_rate);
2393 repeat_rate--;
2394 index++;
2397 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band, &tbl_type,
2398 &rate_idx);
2400 /* Indicate to uCode which entries might be MIMO.
2401 * If initial rate was MIMO, this will finally end up
2402 * as (IWL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
2403 if (is_mimo(tbl_type.lq_type))
2404 lq_cmd->general_params.mimo_delimiter = index;
2406 /* Get next rate */
2407 new_rate = rs_get_lower_rate(lq_sta, &tbl_type, rate_idx,
2408 use_ht_possible);
2410 /* How many times should we repeat the next rate? */
2411 if (is_legacy(tbl_type.lq_type)) {
2412 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2413 ant_toggle_cnt++;
2414 else if (priv &&
2415 rs_toggle_antenna(valid_tx_ant,
2416 &new_rate, &tbl_type))
2417 ant_toggle_cnt = 1;
2419 repeat_rate = IWL_NUMBER_TRY;
2420 } else {
2421 repeat_rate = IWL_HT_NUMBER_TRY;
2424 /* Don't allow HT rates after next pass.
2425 * rs_get_lower_rate() will change type to LQ_A or LQ_G. */
2426 use_ht_possible = 0;
2428 /* Override next rate if needed for debug purposes */
2429 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
2431 /* Fill next table entry */
2432 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
2434 index++;
2435 repeat_rate--;
2438 lq_cmd->agg_params.agg_frame_cnt_limit = 64;
2439 lq_cmd->agg_params.agg_dis_start_th = 3;
2440 lq_cmd->agg_params.agg_time_limit = cpu_to_le16(4000);
2443 static void *rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
2445 return hw->priv;
2447 /* rate scale requires free function to be implemented */
2448 static void rs_free(void *priv_rate)
2450 return;
2453 static void rs_free_sta(void *priv_r, struct ieee80211_sta *sta,
2454 void *priv_sta)
2456 struct iwl_lq_sta *lq_sta = priv_sta;
2457 struct iwl_priv *priv __maybe_unused = priv_r;
2459 IWL_DEBUG_RATE(priv, "enter\n");
2460 kfree(lq_sta);
2461 IWL_DEBUG_RATE(priv, "leave\n");
2465 #ifdef CONFIG_MAC80211_DEBUGFS
2466 static int open_file_generic(struct inode *inode, struct file *file)
2468 file->private_data = inode->i_private;
2469 return 0;
2471 static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
2472 u32 *rate_n_flags, int index)
2474 struct iwl_priv *priv;
2476 priv = lq_sta->drv;
2477 if (lq_sta->dbg_fixed_rate) {
2478 if (index < 12) {
2479 *rate_n_flags = lq_sta->dbg_fixed_rate;
2480 } else {
2481 if (lq_sta->band == IEEE80211_BAND_5GHZ)
2482 *rate_n_flags = 0x800D;
2483 else
2484 *rate_n_flags = 0x820A;
2486 IWL_DEBUG_RATE(priv, "Fixed rate ON\n");
2487 } else {
2488 IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
2492 static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
2493 const char __user *user_buf, size_t count, loff_t *ppos)
2495 struct iwl_lq_sta *lq_sta = file->private_data;
2496 struct iwl_priv *priv;
2497 char buf[64];
2498 int buf_size;
2499 u32 parsed_rate;
2501 priv = lq_sta->drv;
2502 memset(buf, 0, sizeof(buf));
2503 buf_size = min(count, sizeof(buf) - 1);
2504 if (copy_from_user(buf, user_buf, buf_size))
2505 return -EFAULT;
2507 if (sscanf(buf, "%x", &parsed_rate) == 1)
2508 lq_sta->dbg_fixed_rate = parsed_rate;
2509 else
2510 lq_sta->dbg_fixed_rate = 0;
2512 lq_sta->active_legacy_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
2513 lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2514 lq_sta->active_mimo2_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2515 lq_sta->active_mimo3_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2517 IWL_DEBUG_RATE(priv, "sta_id %d rate 0x%X\n",
2518 lq_sta->lq.sta_id, lq_sta->dbg_fixed_rate);
2520 if (lq_sta->dbg_fixed_rate) {
2521 rs_fill_link_cmd(NULL, lq_sta, lq_sta->dbg_fixed_rate);
2522 iwl_send_lq_cmd(lq_sta->drv, &lq_sta->lq, CMD_ASYNC);
2525 return count;
2528 static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
2529 char __user *user_buf, size_t count, loff_t *ppos)
2531 char *buff;
2532 int desc = 0;
2533 int i = 0;
2534 ssize_t ret;
2536 struct iwl_lq_sta *lq_sta = file->private_data;
2537 struct iwl_priv *priv;
2538 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
2540 priv = lq_sta->drv;
2541 buff = kmalloc(1024, GFP_KERNEL);
2542 if (!buff)
2543 return -ENOMEM;
2545 desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
2546 desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
2547 lq_sta->total_failed, lq_sta->total_success,
2548 lq_sta->active_legacy_rate);
2549 desc += sprintf(buff+desc, "fixed rate 0x%X\n",
2550 lq_sta->dbg_fixed_rate);
2551 desc += sprintf(buff+desc, "valid_tx_ant %s%s%s\n",
2552 (priv->hw_params.valid_tx_ant & ANT_A) ? "ANT_A," : "",
2553 (priv->hw_params.valid_tx_ant & ANT_B) ? "ANT_B," : "",
2554 (priv->hw_params.valid_tx_ant & ANT_C) ? "ANT_C" : "");
2555 desc += sprintf(buff+desc, "lq type %s\n",
2556 (is_legacy(tbl->lq_type)) ? "legacy" : "HT");
2557 if (is_Ht(tbl->lq_type)) {
2558 desc += sprintf(buff+desc, " %s",
2559 (is_siso(tbl->lq_type)) ? "SISO" :
2560 ((is_mimo2(tbl->lq_type)) ? "MIMO2" : "MIMO3"));
2561 desc += sprintf(buff+desc, " %s",
2562 (tbl->is_fat) ? "40MHz" : "20MHz");
2563 desc += sprintf(buff+desc, " %s\n", (tbl->is_SGI) ? "SGI" : "");
2565 desc += sprintf(buff+desc, "general:"
2566 "flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
2567 lq_sta->lq.general_params.flags,
2568 lq_sta->lq.general_params.mimo_delimiter,
2569 lq_sta->lq.general_params.single_stream_ant_msk,
2570 lq_sta->lq.general_params.dual_stream_ant_msk);
2572 desc += sprintf(buff+desc, "agg:"
2573 "time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
2574 le16_to_cpu(lq_sta->lq.agg_params.agg_time_limit),
2575 lq_sta->lq.agg_params.agg_dis_start_th,
2576 lq_sta->lq.agg_params.agg_frame_cnt_limit);
2578 desc += sprintf(buff+desc,
2579 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
2580 lq_sta->lq.general_params.start_rate_index[0],
2581 lq_sta->lq.general_params.start_rate_index[1],
2582 lq_sta->lq.general_params.start_rate_index[2],
2583 lq_sta->lq.general_params.start_rate_index[3]);
2586 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
2587 desc += sprintf(buff+desc, " rate[%d] 0x%X\n",
2588 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags));
2590 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2591 kfree(buff);
2592 return ret;
2595 static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
2596 .write = rs_sta_dbgfs_scale_table_write,
2597 .read = rs_sta_dbgfs_scale_table_read,
2598 .open = open_file_generic,
2600 static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
2601 char __user *user_buf, size_t count, loff_t *ppos)
2603 char *buff;
2604 int desc = 0;
2605 int i, j;
2606 ssize_t ret;
2608 struct iwl_lq_sta *lq_sta = file->private_data;
2610 buff = kmalloc(1024, GFP_KERNEL);
2611 if (!buff)
2612 return -ENOMEM;
2614 for (i = 0; i < LQ_SIZE; i++) {
2615 desc += sprintf(buff+desc, "%s type=%d SGI=%d FAT=%d DUP=%d\n"
2616 "rate=0x%X\n",
2617 lq_sta->active_tbl == i ? "*" : "x",
2618 lq_sta->lq_info[i].lq_type,
2619 lq_sta->lq_info[i].is_SGI,
2620 lq_sta->lq_info[i].is_fat,
2621 lq_sta->lq_info[i].is_dup,
2622 lq_sta->lq_info[i].current_rate);
2623 for (j = 0; j < IWL_RATE_COUNT; j++) {
2624 desc += sprintf(buff+desc,
2625 "counter=%d success=%d %%=%d\n",
2626 lq_sta->lq_info[i].win[j].counter,
2627 lq_sta->lq_info[i].win[j].success_counter,
2628 lq_sta->lq_info[i].win[j].success_ratio);
2631 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2632 kfree(buff);
2633 return ret;
2636 static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
2637 .read = rs_sta_dbgfs_stats_table_read,
2638 .open = open_file_generic,
2641 static void rs_add_debugfs(void *priv, void *priv_sta,
2642 struct dentry *dir)
2644 struct iwl_lq_sta *lq_sta = priv_sta;
2645 lq_sta->rs_sta_dbgfs_scale_table_file =
2646 debugfs_create_file("rate_scale_table", 0600, dir,
2647 lq_sta, &rs_sta_dbgfs_scale_table_ops);
2648 lq_sta->rs_sta_dbgfs_stats_table_file =
2649 debugfs_create_file("rate_stats_table", 0600, dir,
2650 lq_sta, &rs_sta_dbgfs_stats_table_ops);
2651 lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file =
2652 debugfs_create_u8("tx_agg_tid_enable", 0600, dir,
2653 &lq_sta->tx_agg_tid_en);
2657 static void rs_remove_debugfs(void *priv, void *priv_sta)
2659 struct iwl_lq_sta *lq_sta = priv_sta;
2660 debugfs_remove(lq_sta->rs_sta_dbgfs_scale_table_file);
2661 debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
2662 debugfs_remove(lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file);
2664 #endif
2666 static struct rate_control_ops rs_ops = {
2667 .module = NULL,
2668 .name = RS_NAME,
2669 .tx_status = rs_tx_status,
2670 .get_rate = rs_get_rate,
2671 .rate_init = rs_rate_init,
2672 .alloc = rs_alloc,
2673 .free = rs_free,
2674 .alloc_sta = rs_alloc_sta,
2675 .free_sta = rs_free_sta,
2676 #ifdef CONFIG_MAC80211_DEBUGFS
2677 .add_sta_debugfs = rs_add_debugfs,
2678 .remove_sta_debugfs = rs_remove_debugfs,
2679 #endif
2682 int iwlagn_rate_control_register(void)
2684 return ieee80211_rate_control_register(&rs_ops);
2687 void iwlagn_rate_control_unregister(void)
2689 ieee80211_rate_control_unregister(&rs_ops);