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
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>
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 (3*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
,
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
,
71 /*ANT_AB -> */ ANT_BC
,
73 /*ANT_AC -> */ ANT_AB
,
74 /*ANT_BC -> */ ANT_AC
,
75 /*ANT_ABC -> */ ANT_ABC
,
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 */
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
;
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
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.
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 */
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 */
136 u32 total_failed
; /* total failed frames, any/all rates */
137 u32 total_success
; /* total successful frames, any/all rates */
138 u64 flush_timer
; /* time staying in mode before new search */
140 u8 action_counter
; /* # mode-switch actions tried */
143 enum ieee80211_band band
;
146 /* The following are bitmaps of rates; IWL_RATE_6M_MASK, etc. */
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
];
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
;
166 struct iwl_priv
*drv
;
168 /* used to be in sta_info */
170 /* last tx rate_n_flags */
171 u32 last_rate_n_flags
;
174 static void rs_rate_scale_perform(struct iwl_priv
*priv
,
176 struct ieee80211_sta
*sta
,
177 struct iwl_lq_sta
*lq_sta
);
178 static void rs_fill_link_cmd(const struct iwl_priv
*priv
,
179 struct iwl_lq_sta
*lq_sta
, u32 rate_n_flags
);
182 #ifdef CONFIG_MAC80211_DEBUGFS
183 static void rs_dbgfs_set_mcs(struct iwl_lq_sta
*lq_sta
,
184 u32
*rate_n_flags
, int index
);
186 static void rs_dbgfs_set_mcs(struct iwl_lq_sta
*lq_sta
,
187 u32
*rate_n_flags
, int index
)
192 * Expected throughput metrics for following rates:
193 * 1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
194 * "G" is the only table that supports CCK (the first 4 rates).
197 static s32 expected_tpt_A
[IWL_RATE_COUNT
] = {
198 0, 0, 0, 0, 40, 57, 72, 98, 121, 154, 177, 186, 186
201 static s32 expected_tpt_G
[IWL_RATE_COUNT
] = {
202 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 186
205 static s32 expected_tpt_siso20MHz
[IWL_RATE_COUNT
] = {
206 0, 0, 0, 0, 42, 42, 76, 102, 124, 159, 183, 193, 202
209 static s32 expected_tpt_siso20MHzSGI
[IWL_RATE_COUNT
] = {
210 0, 0, 0, 0, 46, 46, 82, 110, 132, 168, 192, 202, 211
213 static s32 expected_tpt_mimo2_20MHz
[IWL_RATE_COUNT
] = {
214 0, 0, 0, 0, 74, 74, 123, 155, 179, 214, 236, 244, 251
217 static s32 expected_tpt_mimo2_20MHzSGI
[IWL_RATE_COUNT
] = {
218 0, 0, 0, 0, 81, 81, 131, 164, 188, 222, 243, 251, 257
221 static s32 expected_tpt_siso40MHz
[IWL_RATE_COUNT
] = {
222 0, 0, 0, 0, 77, 77, 127, 160, 184, 220, 242, 250, 257
225 static s32 expected_tpt_siso40MHzSGI
[IWL_RATE_COUNT
] = {
226 0, 0, 0, 0, 83, 83, 135, 169, 193, 229, 250, 257, 264
229 static s32 expected_tpt_mimo2_40MHz
[IWL_RATE_COUNT
] = {
230 0, 0, 0, 0, 123, 123, 182, 214, 235, 264, 279, 285, 289
233 static s32 expected_tpt_mimo2_40MHzSGI
[IWL_RATE_COUNT
] = {
234 0, 0, 0, 0, 131, 131, 191, 222, 242, 270, 284, 289, 293
237 /* Expected throughput metric MIMO3 */
238 static s32 expected_tpt_mimo3_20MHz
[IWL_RATE_COUNT
] = {
239 0, 0, 0, 0, 99, 99, 153, 186, 208, 239, 256, 263, 268
242 static s32 expected_tpt_mimo3_20MHzSGI
[IWL_RATE_COUNT
] = {
243 0, 0, 0, 0, 106, 106, 162, 194, 215, 246, 262, 268, 273
246 static s32 expected_tpt_mimo3_40MHz
[IWL_RATE_COUNT
] = {
247 0, 0, 0, 0, 152, 152, 211, 239, 255, 279, 290, 294, 297
250 static s32 expected_tpt_mimo3_40MHzSGI
[IWL_RATE_COUNT
] = {
251 0, 0, 0, 0, 160, 160, 219, 245, 261, 284, 294, 297, 300
255 const static struct iwl_rate_mcs_info iwl_rate_mcs
[IWL_RATE_COUNT
] = {
271 static inline u8
rs_extract_rate(u32 rate_n_flags
)
273 return (u8
)(rate_n_flags
& 0xFF);
276 static void rs_rate_scale_clear_window(struct iwl_rate_scale_data
*window
)
279 window
->success_counter
= 0;
280 window
->success_ratio
= IWL_INVALID_VALUE
;
282 window
->average_tpt
= IWL_INVALID_VALUE
;
286 static inline u8
rs_is_valid_ant(u8 valid_antenna
, u8 ant_type
)
288 return (ant_type
& valid_antenna
) == ant_type
;
292 * removes the old data from the statistics. All data that is older than
293 * TID_MAX_TIME_DIFF, will be deleted.
295 static void rs_tl_rm_old_stats(struct iwl_traffic_load
*tl
, u32 curr_time
)
297 /* The oldest age we want to keep */
298 u32 oldest_time
= curr_time
- TID_MAX_TIME_DIFF
;
300 while (tl
->queue_count
&&
301 (tl
->time_stamp
< oldest_time
)) {
302 tl
->total
-= tl
->packet_count
[tl
->head
];
303 tl
->packet_count
[tl
->head
] = 0;
304 tl
->time_stamp
+= TID_QUEUE_CELL_SPACING
;
307 if (tl
->head
>= TID_QUEUE_MAX_SIZE
)
313 * increment traffic load value for tid and also remove
314 * any old values if passed the certain time period
316 static u8
rs_tl_add_packet(struct iwl_lq_sta
*lq_data
,
317 struct ieee80211_hdr
*hdr
)
319 u32 curr_time
= jiffies_to_msecs(jiffies
);
322 struct iwl_traffic_load
*tl
= NULL
;
325 if (ieee80211_is_data_qos(hdr
->frame_control
)) {
326 u8
*qc
= ieee80211_get_qos_ctl(hdr
);
329 return MAX_TID_COUNT
;
331 tl
= &lq_data
->load
[tid
];
333 curr_time
-= curr_time
% TID_ROUND_VALUE
;
335 /* Happens only for the first packet. Initialize the data */
336 if (!(tl
->queue_count
)) {
338 tl
->time_stamp
= curr_time
;
341 tl
->packet_count
[0] = 1;
342 return MAX_TID_COUNT
;
345 time_diff
= TIME_WRAP_AROUND(tl
->time_stamp
, curr_time
);
346 index
= time_diff
/ TID_QUEUE_CELL_SPACING
;
348 /* The history is too long: remove data that is older than */
349 /* TID_MAX_TIME_DIFF */
350 if (index
>= TID_QUEUE_MAX_SIZE
)
351 rs_tl_rm_old_stats(tl
, curr_time
);
353 index
= (tl
->head
+ index
) % TID_QUEUE_MAX_SIZE
;
354 tl
->packet_count
[index
] = tl
->packet_count
[index
] + 1;
355 tl
->total
= tl
->total
+ 1;
357 if ((index
+ 1) > tl
->queue_count
)
358 tl
->queue_count
= index
+ 1;
364 get the traffic load value for tid
366 static u32
rs_tl_get_load(struct iwl_lq_sta
*lq_data
, u8 tid
)
368 u32 curr_time
= jiffies_to_msecs(jiffies
);
371 struct iwl_traffic_load
*tl
= NULL
;
373 if (tid
>= TID_MAX_LOAD_COUNT
)
376 tl
= &(lq_data
->load
[tid
]);
378 curr_time
-= curr_time
% TID_ROUND_VALUE
;
380 if (!(tl
->queue_count
))
383 time_diff
= TIME_WRAP_AROUND(tl
->time_stamp
, curr_time
);
384 index
= time_diff
/ TID_QUEUE_CELL_SPACING
;
386 /* The history is too long: remove data that is older than */
387 /* TID_MAX_TIME_DIFF */
388 if (index
>= TID_QUEUE_MAX_SIZE
)
389 rs_tl_rm_old_stats(tl
, curr_time
);
394 static void rs_tl_turn_on_agg_for_tid(struct iwl_priv
*priv
,
395 struct iwl_lq_sta
*lq_data
, u8 tid
,
396 struct ieee80211_sta
*sta
)
398 if (rs_tl_get_load(lq_data
, tid
) > IWL_AGG_LOAD_THRESHOLD
) {
399 IWL_DEBUG_HT(priv
, "Starting Tx agg: STA: %pM tid: %d\n",
401 ieee80211_start_tx_ba_session(priv
->hw
, sta
->addr
, tid
);
405 static void rs_tl_turn_on_agg(struct iwl_priv
*priv
, u8 tid
,
406 struct iwl_lq_sta
*lq_data
,
407 struct ieee80211_sta
*sta
)
409 if ((tid
< TID_MAX_LOAD_COUNT
))
410 rs_tl_turn_on_agg_for_tid(priv
, lq_data
, tid
, sta
);
411 else if (tid
== IWL_AGG_ALL_TID
)
412 for (tid
= 0; tid
< TID_MAX_LOAD_COUNT
; tid
++)
413 rs_tl_turn_on_agg_for_tid(priv
, lq_data
, tid
, sta
);
416 static inline int get_num_of_ant_from_rate(u32 rate_n_flags
)
418 return !!(rate_n_flags
& RATE_MCS_ANT_A_MSK
) +
419 !!(rate_n_flags
& RATE_MCS_ANT_B_MSK
) +
420 !!(rate_n_flags
& RATE_MCS_ANT_C_MSK
);
424 * rs_collect_tx_data - Update the success/failure sliding window
426 * We keep a sliding window of the last 62 packets transmitted
427 * at this rate. window->data contains the bitmask of successful
430 static int rs_collect_tx_data(struct iwl_rate_scale_data
*windows
,
431 int scale_index
, s32 tpt
, int retries
,
434 struct iwl_rate_scale_data
*window
= NULL
;
435 static const u64 mask
= (((u64
)1) << (IWL_RATE_MAX_WINDOW
- 1));
438 if (scale_index
< 0 || scale_index
>= IWL_RATE_COUNT
)
441 /* Select data for current tx bit rate */
442 window
= &(windows
[scale_index
]);
445 * Keep track of only the latest 62 tx frame attempts in this rate's
446 * history window; anything older isn't really relevant any more.
447 * If we have filled up the sliding window, drop the oldest attempt;
448 * if the oldest attempt (highest bit in bitmap) shows "success",
449 * subtract "1" from the success counter (this is the main reason
450 * we keep these bitmaps!).
452 while (retries
> 0) {
453 if (window
->counter
>= IWL_RATE_MAX_WINDOW
) {
455 /* remove earliest */
456 window
->counter
= IWL_RATE_MAX_WINDOW
- 1;
458 if (window
->data
& mask
) {
459 window
->data
&= ~mask
;
460 window
->success_counter
--;
464 /* Increment frames-attempted counter */
467 /* Shift bitmap by one frame (throw away oldest history),
468 * OR in "1", and increment "success" if this
469 * frame was successful. */
472 window
->success_counter
++;
480 /* Calculate current success ratio, avoid divide-by-0! */
481 if (window
->counter
> 0)
482 window
->success_ratio
= 128 * (100 * window
->success_counter
)
485 window
->success_ratio
= IWL_INVALID_VALUE
;
487 fail_count
= window
->counter
- window
->success_counter
;
489 /* Calculate average throughput, if we have enough history. */
490 if ((fail_count
>= IWL_RATE_MIN_FAILURE_TH
) ||
491 (window
->success_counter
>= IWL_RATE_MIN_SUCCESS_TH
))
492 window
->average_tpt
= (window
->success_ratio
* tpt
+ 64) / 128;
494 window
->average_tpt
= IWL_INVALID_VALUE
;
496 /* Tag this window as having been updated */
497 window
->stamp
= jiffies
;
503 * Fill uCode API rate_n_flags field, based on "search" or "active" table.
505 /* FIXME:RS:remove this function and put the flags statically in the table */
506 static u32
rate_n_flags_from_tbl(struct iwl_priv
*priv
,
507 struct iwl_scale_tbl_info
*tbl
,
508 int index
, u8 use_green
)
510 u32 rate_n_flags
= 0;
512 if (is_legacy(tbl
->lq_type
)) {
513 rate_n_flags
= iwl_rates
[index
].plcp
;
514 if (index
>= IWL_FIRST_CCK_RATE
&& index
<= IWL_LAST_CCK_RATE
)
515 rate_n_flags
|= RATE_MCS_CCK_MSK
;
517 } else if (is_Ht(tbl
->lq_type
)) {
518 if (index
> IWL_LAST_OFDM_RATE
) {
519 IWL_ERR(priv
, "Invalid HT rate index %d\n", index
);
520 index
= IWL_LAST_OFDM_RATE
;
522 rate_n_flags
= RATE_MCS_HT_MSK
;
524 if (is_siso(tbl
->lq_type
))
525 rate_n_flags
|= iwl_rates
[index
].plcp_siso
;
526 else if (is_mimo2(tbl
->lq_type
))
527 rate_n_flags
|= iwl_rates
[index
].plcp_mimo2
;
529 rate_n_flags
|= iwl_rates
[index
].plcp_mimo3
;
531 IWL_ERR(priv
, "Invalid tbl->lq_type %d\n", tbl
->lq_type
);
534 rate_n_flags
|= ((tbl
->ant_type
<< RATE_MCS_ANT_POS
) &
535 RATE_MCS_ANT_ABC_MSK
);
537 if (is_Ht(tbl
->lq_type
)) {
540 rate_n_flags
|= RATE_MCS_DUP_MSK
;
542 rate_n_flags
|= RATE_MCS_FAT_MSK
;
545 rate_n_flags
|= RATE_MCS_SGI_MSK
;
548 rate_n_flags
|= RATE_MCS_GF_MSK
;
549 if (is_siso(tbl
->lq_type
) && tbl
->is_SGI
) {
550 rate_n_flags
&= ~RATE_MCS_SGI_MSK
;
551 IWL_ERR(priv
, "GF was set with SGI:SISO\n");
559 * Interpret uCode API's rate_n_flags format,
560 * fill "search" or "active" tx mode table.
562 static int rs_get_tbl_info_from_mcs(const u32 rate_n_flags
,
563 enum ieee80211_band band
,
564 struct iwl_scale_tbl_info
*tbl
,
567 u32 ant_msk
= (rate_n_flags
& RATE_MCS_ANT_ABC_MSK
);
568 u8 num_of_ant
= get_num_of_ant_from_rate(rate_n_flags
);
571 *rate_idx
= iwl_hwrate_to_plcp_idx(rate_n_flags
);
573 if (*rate_idx
== IWL_RATE_INVALID
) {
577 tbl
->is_SGI
= 0; /* default legacy setup */
580 tbl
->ant_type
= (ant_msk
>> RATE_MCS_ANT_POS
);
581 tbl
->lq_type
= LQ_NONE
;
583 /* legacy rate format */
584 if (!(rate_n_flags
& RATE_MCS_HT_MSK
)) {
585 if (num_of_ant
== 1) {
586 if (band
== IEEE80211_BAND_5GHZ
)
593 if (rate_n_flags
& RATE_MCS_SGI_MSK
)
596 if ((rate_n_flags
& RATE_MCS_FAT_MSK
) ||
597 (rate_n_flags
& RATE_MCS_DUP_MSK
))
600 if (rate_n_flags
& RATE_MCS_DUP_MSK
)
603 mcs
= rs_extract_rate(rate_n_flags
);
606 if (mcs
<= IWL_RATE_SISO_60M_PLCP
) {
608 tbl
->lq_type
= LQ_SISO
; /*else NONE*/
610 } else if (mcs
<= IWL_RATE_MIMO2_60M_PLCP
) {
612 tbl
->lq_type
= LQ_MIMO2
;
616 tbl
->lq_type
= LQ_MIMO3
;
622 /* switch to another antenna/antennas and return 1 */
623 /* if no other valid antenna found, return 0 */
624 static int rs_toggle_antenna(u32 valid_ant
, u32
*rate_n_flags
,
625 struct iwl_scale_tbl_info
*tbl
)
629 if (!tbl
->ant_type
|| tbl
->ant_type
> ANT_ABC
)
632 if (!rs_is_valid_ant(valid_ant
, tbl
->ant_type
))
635 new_ant_type
= ant_toggle_lookup
[tbl
->ant_type
];
637 while ((new_ant_type
!= tbl
->ant_type
) &&
638 !rs_is_valid_ant(valid_ant
, new_ant_type
))
639 new_ant_type
= ant_toggle_lookup
[new_ant_type
];
641 if (new_ant_type
== tbl
->ant_type
)
644 tbl
->ant_type
= new_ant_type
;
645 *rate_n_flags
&= ~RATE_MCS_ANT_ABC_MSK
;
646 *rate_n_flags
|= new_ant_type
<< RATE_MCS_ANT_POS
;
650 /* FIXME:RS: in 4965 we don't use greenfield at all */
651 /* FIXME:RS: don't use greenfield for now in TX */
653 static inline u8
rs_use_green(struct iwl_priv
*priv
, struct ieee80211_conf
*conf
)
655 return (conf
->flags
& IEEE80211_CONF_SUPPORT_HT_MODE
) &&
656 priv
->current_ht_config
.is_green_field
&&
657 !priv
->current_ht_config
.non_GF_STA_present
;
660 static inline u8
rs_use_green(struct iwl_priv
*priv
, struct ieee80211_conf
*conf
)
666 * rs_get_supported_rates - get the available rates
668 * if management frame or broadcast frame only return
669 * basic available rates.
672 static u16
rs_get_supported_rates(struct iwl_lq_sta
*lq_sta
,
673 struct ieee80211_hdr
*hdr
,
674 enum iwl_table_type rate_type
)
676 if (hdr
&& is_multicast_ether_addr(hdr
->addr1
) &&
677 lq_sta
->active_rate_basic
)
678 return lq_sta
->active_rate_basic
;
680 if (is_legacy(rate_type
)) {
681 return lq_sta
->active_legacy_rate
;
683 if (is_siso(rate_type
))
684 return lq_sta
->active_siso_rate
;
685 else if (is_mimo2(rate_type
))
686 return lq_sta
->active_mimo2_rate
;
688 return lq_sta
->active_mimo3_rate
;
692 static u16
rs_get_adjacent_rate(struct iwl_priv
*priv
, u8 index
, u16 rate_mask
,
695 u8 high
= IWL_RATE_INVALID
;
696 u8 low
= IWL_RATE_INVALID
;
698 /* 802.11A or ht walks to the next literal adjacent rate in
700 if (is_a_band(rate_type
) || !is_legacy(rate_type
)) {
704 /* Find the previous rate that is in the rate mask */
706 for (mask
= (1 << i
); i
>= 0; i
--, mask
>>= 1) {
707 if (rate_mask
& mask
) {
713 /* Find the next rate that is in the rate mask */
715 for (mask
= (1 << i
); i
< IWL_RATE_COUNT
; i
++, mask
<<= 1) {
716 if (rate_mask
& mask
) {
722 return (high
<< 8) | low
;
726 while (low
!= IWL_RATE_INVALID
) {
727 low
= iwl_rates
[low
].prev_rs
;
728 if (low
== IWL_RATE_INVALID
)
730 if (rate_mask
& (1 << low
))
732 IWL_DEBUG_RATE(priv
, "Skipping masked lower rate: %d\n", low
);
736 while (high
!= IWL_RATE_INVALID
) {
737 high
= iwl_rates
[high
].next_rs
;
738 if (high
== IWL_RATE_INVALID
)
740 if (rate_mask
& (1 << high
))
742 IWL_DEBUG_RATE(priv
, "Skipping masked higher rate: %d\n", high
);
745 return (high
<< 8) | low
;
748 static u32
rs_get_lower_rate(struct iwl_lq_sta
*lq_sta
,
749 struct iwl_scale_tbl_info
*tbl
,
750 u8 scale_index
, u8 ht_possible
)
755 u8 switch_to_legacy
= 0;
756 u8 is_green
= lq_sta
->is_green
;
758 /* check if we need to switch from HT to legacy rates.
759 * assumption is that mandatory rates (1Mbps or 6Mbps)
760 * are always supported (spec demand) */
761 if (!is_legacy(tbl
->lq_type
) && (!ht_possible
|| !scale_index
)) {
762 switch_to_legacy
= 1;
763 scale_index
= rs_ht_to_legacy
[scale_index
];
764 if (lq_sta
->band
== IEEE80211_BAND_5GHZ
)
769 if (num_of_ant(tbl
->ant_type
) > 1)
770 tbl
->ant_type
= ANT_A
;/*FIXME:RS*/
776 rate_mask
= rs_get_supported_rates(lq_sta
, NULL
, tbl
->lq_type
);
778 /* Mask with station rate restriction */
779 if (is_legacy(tbl
->lq_type
)) {
780 /* supp_rates has no CCK bits in A mode */
781 if (lq_sta
->band
== IEEE80211_BAND_5GHZ
)
782 rate_mask
= (u16
)(rate_mask
&
783 (lq_sta
->supp_rates
<< IWL_FIRST_OFDM_RATE
));
785 rate_mask
= (u16
)(rate_mask
& lq_sta
->supp_rates
);
788 /* If we switched from HT to legacy, check current rate */
789 if (switch_to_legacy
&& (rate_mask
& (1 << scale_index
))) {
794 high_low
= rs_get_adjacent_rate(lq_sta
->drv
, scale_index
, rate_mask
,
796 low
= high_low
& 0xff;
798 if (low
== IWL_RATE_INVALID
)
802 return rate_n_flags_from_tbl(lq_sta
->drv
, tbl
, low
, is_green
);
806 * mac80211 sends us Tx status
808 static void rs_tx_status(void *priv_r
, struct ieee80211_supported_band
*sband
,
809 struct ieee80211_sta
*sta
, void *priv_sta
,
814 int rs_index
, index
= 0;
815 struct iwl_lq_sta
*lq_sta
= priv_sta
;
816 struct iwl_link_quality_cmd
*table
;
817 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*)skb
->data
;
818 struct iwl_priv
*priv
= (struct iwl_priv
*)priv_r
;
819 struct ieee80211_hw
*hw
= priv
->hw
;
820 struct ieee80211_tx_info
*info
= IEEE80211_SKB_CB(skb
);
821 struct iwl_rate_scale_data
*window
= NULL
;
822 struct iwl_rate_scale_data
*search_win
= NULL
;
824 struct iwl_scale_tbl_info tbl_type
;
825 struct iwl_scale_tbl_info
*curr_tbl
, *search_tbl
;
829 IWL_DEBUG_RATE_LIMIT(priv
, "get frame ack response, update rate scale window\n");
831 if (!ieee80211_is_data(hdr
->frame_control
) ||
832 info
->flags
& IEEE80211_TX_CTL_NO_ACK
)
835 /* This packet was aggregated but doesn't carry rate scale info */
836 if ((info
->flags
& IEEE80211_TX_CTL_AMPDU
) &&
837 !(info
->flags
& IEEE80211_TX_STAT_AMPDU
))
840 if (info
->flags
& IEEE80211_TX_STAT_AMPDU
)
843 retries
= info
->status
.rates
[0].count
- 1;
848 if ((priv
->iw_mode
== NL80211_IFTYPE_ADHOC
) &&
849 !lq_sta
->ibss_sta_added
)
853 active_index
= lq_sta
->active_tbl
;
855 curr_tbl
= &(lq_sta
->lq_info
[active_index
]);
856 search_tbl
= &(lq_sta
->lq_info
[(1 - active_index
)]);
857 window
= (struct iwl_rate_scale_data
*)&(curr_tbl
->win
[0]);
858 search_win
= (struct iwl_rate_scale_data
*)&(search_tbl
->win
[0]);
861 * Ignore this Tx frame response if its initial rate doesn't match
862 * that of latest Link Quality command. There may be stragglers
863 * from a previous Link Quality command, but we're no longer interested
864 * in those; they're either from the "active" mode while we're trying
865 * to check "search" mode, or a prior "search" mode after we've moved
866 * to a new "search" mode (which might become the new "active" mode).
868 tx_rate
= le32_to_cpu(table
->rs_table
[0].rate_n_flags
);
869 rs_get_tbl_info_from_mcs(tx_rate
, priv
->band
, &tbl_type
, &rs_index
);
870 if (priv
->band
== IEEE80211_BAND_5GHZ
)
871 rs_index
-= IWL_FIRST_OFDM_RATE
;
873 if ((info
->status
.rates
[0].idx
< 0) ||
874 (tbl_type
.is_SGI
!= !!(info
->status
.rates
[0].flags
& IEEE80211_TX_RC_SHORT_GI
)) ||
875 (tbl_type
.is_fat
!= !!(info
->status
.rates
[0].flags
& IEEE80211_TX_RC_40_MHZ_WIDTH
)) ||
876 (tbl_type
.is_dup
!= !!(info
->status
.rates
[0].flags
& IEEE80211_TX_RC_DUP_DATA
)) ||
877 (tbl_type
.ant_type
!= info
->antenna_sel_tx
) ||
878 (!!(tx_rate
& RATE_MCS_HT_MSK
) != !!(info
->status
.rates
[0].flags
& IEEE80211_TX_RC_MCS
)) ||
879 (!!(tx_rate
& RATE_MCS_GF_MSK
) != !!(info
->status
.rates
[0].flags
& IEEE80211_TX_RC_GREEN_FIELD
)) ||
880 (hw
->wiphy
->bands
[priv
->band
]->bitrates
[rs_index
].bitrate
!=
881 hw
->wiphy
->bands
[info
->band
]->bitrates
[info
->status
.rates
[0].idx
].bitrate
)) {
882 IWL_DEBUG_RATE(priv
, "initial rate does not match 0x%x\n", tx_rate
);
883 /* the last LQ command could failed so the LQ in ucode not
884 * the same in driver sync up
886 lq_sta
->missed_rate_counter
++;
887 if (lq_sta
->missed_rate_counter
> IWL_MISSED_RATE_MAX
) {
888 lq_sta
->missed_rate_counter
= 0;
889 iwl_send_lq_cmd(priv
, &lq_sta
->lq
, CMD_ASYNC
);
894 lq_sta
->missed_rate_counter
= 0;
895 /* Update frame history window with "failure" for each Tx retry. */
897 /* Look up the rate and other info used for each tx attempt.
898 * Each tx attempt steps one entry deeper in the rate table. */
899 tx_rate
= le32_to_cpu(table
->rs_table
[index
].rate_n_flags
);
900 rs_get_tbl_info_from_mcs(tx_rate
, priv
->band
,
901 &tbl_type
, &rs_index
);
903 /* If type matches "search" table,
904 * add failure to "search" history */
905 if ((tbl_type
.lq_type
== search_tbl
->lq_type
) &&
906 (tbl_type
.ant_type
== search_tbl
->ant_type
) &&
907 (tbl_type
.is_SGI
== search_tbl
->is_SGI
)) {
908 if (search_tbl
->expected_tpt
)
909 tpt
= search_tbl
->expected_tpt
[rs_index
];
912 rs_collect_tx_data(search_win
, rs_index
, tpt
, 1, 0);
914 /* Else if type matches "current/active" table,
915 * add failure to "current/active" history */
916 } else if ((tbl_type
.lq_type
== curr_tbl
->lq_type
) &&
917 (tbl_type
.ant_type
== curr_tbl
->ant_type
) &&
918 (tbl_type
.is_SGI
== curr_tbl
->is_SGI
)) {
919 if (curr_tbl
->expected_tpt
)
920 tpt
= curr_tbl
->expected_tpt
[rs_index
];
923 rs_collect_tx_data(window
, rs_index
, tpt
, 1, 0);
926 /* If not searching for a new mode, increment failed counter
927 * ... this helps determine when to start searching again */
928 if (lq_sta
->stay_in_tbl
)
929 lq_sta
->total_failed
++;
936 * Find (by rate) the history window to update with final Tx attempt;
937 * if Tx was successful first try, use original rate,
938 * else look up the rate that was, finally, successful.
940 tx_rate
= le32_to_cpu(table
->rs_table
[index
].rate_n_flags
);
941 lq_sta
->last_rate_n_flags
= tx_rate
;
942 rs_get_tbl_info_from_mcs(tx_rate
, priv
->band
, &tbl_type
, &rs_index
);
944 /* Update frame history window with "success" if Tx got ACKed ... */
945 status
= !!(info
->flags
& IEEE80211_TX_STAT_ACK
);
947 /* If type matches "search" table,
948 * add final tx status to "search" history */
949 if ((tbl_type
.lq_type
== search_tbl
->lq_type
) &&
950 (tbl_type
.ant_type
== search_tbl
->ant_type
) &&
951 (tbl_type
.is_SGI
== search_tbl
->is_SGI
)) {
952 if (search_tbl
->expected_tpt
)
953 tpt
= search_tbl
->expected_tpt
[rs_index
];
956 if (info
->flags
& IEEE80211_TX_STAT_AMPDU
)
957 rs_collect_tx_data(search_win
, rs_index
, tpt
,
958 info
->status
.ampdu_ack_len
,
959 info
->status
.ampdu_ack_map
);
961 rs_collect_tx_data(search_win
, rs_index
, tpt
,
963 /* Else if type matches "current/active" table,
964 * add final tx status to "current/active" history */
965 } else if ((tbl_type
.lq_type
== curr_tbl
->lq_type
) &&
966 (tbl_type
.ant_type
== curr_tbl
->ant_type
) &&
967 (tbl_type
.is_SGI
== curr_tbl
->is_SGI
)) {
968 if (curr_tbl
->expected_tpt
)
969 tpt
= curr_tbl
->expected_tpt
[rs_index
];
972 if (info
->flags
& IEEE80211_TX_STAT_AMPDU
)
973 rs_collect_tx_data(window
, rs_index
, tpt
,
974 info
->status
.ampdu_ack_len
,
975 info
->status
.ampdu_ack_map
);
977 rs_collect_tx_data(window
, rs_index
, tpt
,
981 /* If not searching for new mode, increment success/failed counter
982 * ... these help determine when to start searching again */
983 if (lq_sta
->stay_in_tbl
) {
984 if (info
->flags
& IEEE80211_TX_STAT_AMPDU
) {
985 lq_sta
->total_success
+= info
->status
.ampdu_ack_map
;
986 lq_sta
->total_failed
+=
987 (info
->status
.ampdu_ack_len
- info
->status
.ampdu_ack_map
);
990 lq_sta
->total_success
++;
992 lq_sta
->total_failed
++;
996 /* See if there's a better rate or modulation mode to try. */
997 if (sta
&& sta
->supp_rates
[sband
->band
])
998 rs_rate_scale_perform(priv
, skb
, sta
, lq_sta
);
1004 * Begin a period of staying with a selected modulation mode.
1005 * Set "stay_in_tbl" flag to prevent any mode switches.
1006 * Set frame tx success limits according to legacy vs. high-throughput,
1007 * and reset overall (spanning all rates) tx success history statistics.
1008 * These control how long we stay using same modulation mode before
1009 * searching for a new mode.
1011 static void rs_set_stay_in_table(struct iwl_priv
*priv
, u8 is_legacy
,
1012 struct iwl_lq_sta
*lq_sta
)
1014 IWL_DEBUG_RATE(priv
, "we are staying in the same table\n");
1015 lq_sta
->stay_in_tbl
= 1; /* only place this gets set */
1017 lq_sta
->table_count_limit
= IWL_LEGACY_TABLE_COUNT
;
1018 lq_sta
->max_failure_limit
= IWL_LEGACY_FAILURE_LIMIT
;
1019 lq_sta
->max_success_limit
= IWL_LEGACY_SUCCESS_LIMIT
;
1021 lq_sta
->table_count_limit
= IWL_NONE_LEGACY_TABLE_COUNT
;
1022 lq_sta
->max_failure_limit
= IWL_NONE_LEGACY_FAILURE_LIMIT
;
1023 lq_sta
->max_success_limit
= IWL_NONE_LEGACY_SUCCESS_LIMIT
;
1025 lq_sta
->table_count
= 0;
1026 lq_sta
->total_failed
= 0;
1027 lq_sta
->total_success
= 0;
1028 lq_sta
->flush_timer
= jiffies
;
1032 * Find correct throughput table for given mode of modulation
1034 static void rs_set_expected_tpt_table(struct iwl_lq_sta
*lq_sta
,
1035 struct iwl_scale_tbl_info
*tbl
)
1037 if (is_legacy(tbl
->lq_type
)) {
1038 if (!is_a_band(tbl
->lq_type
))
1039 tbl
->expected_tpt
= expected_tpt_G
;
1041 tbl
->expected_tpt
= expected_tpt_A
;
1042 } else if (is_siso(tbl
->lq_type
)) {
1043 if (tbl
->is_fat
&& !lq_sta
->is_dup
)
1045 tbl
->expected_tpt
= expected_tpt_siso40MHzSGI
;
1047 tbl
->expected_tpt
= expected_tpt_siso40MHz
;
1048 else if (tbl
->is_SGI
)
1049 tbl
->expected_tpt
= expected_tpt_siso20MHzSGI
;
1051 tbl
->expected_tpt
= expected_tpt_siso20MHz
;
1052 } else if (is_mimo2(tbl
->lq_type
)) {
1053 if (tbl
->is_fat
&& !lq_sta
->is_dup
)
1055 tbl
->expected_tpt
= expected_tpt_mimo2_40MHzSGI
;
1057 tbl
->expected_tpt
= expected_tpt_mimo2_40MHz
;
1058 else if (tbl
->is_SGI
)
1059 tbl
->expected_tpt
= expected_tpt_mimo2_20MHzSGI
;
1061 tbl
->expected_tpt
= expected_tpt_mimo2_20MHz
;
1062 } else if (is_mimo3(tbl
->lq_type
)) {
1063 if (tbl
->is_fat
&& !lq_sta
->is_dup
)
1065 tbl
->expected_tpt
= expected_tpt_mimo3_40MHzSGI
;
1067 tbl
->expected_tpt
= expected_tpt_mimo3_40MHz
;
1068 else if (tbl
->is_SGI
)
1069 tbl
->expected_tpt
= expected_tpt_mimo3_20MHzSGI
;
1071 tbl
->expected_tpt
= expected_tpt_mimo3_20MHz
;
1073 tbl
->expected_tpt
= expected_tpt_G
;
1077 * Find starting rate for new "search" high-throughput mode of modulation.
1078 * Goal is to find lowest expected rate (under perfect conditions) that is
1079 * above the current measured throughput of "active" mode, to give new mode
1080 * a fair chance to prove itself without too many challenges.
1082 * This gets called when transitioning to more aggressive modulation
1083 * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
1084 * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
1085 * to decrease to match "active" throughput. When moving from MIMO to SISO,
1086 * bit rate will typically need to increase, but not if performance was bad.
1088 static s32
rs_get_best_rate(struct iwl_priv
*priv
,
1089 struct iwl_lq_sta
*lq_sta
,
1090 struct iwl_scale_tbl_info
*tbl
, /* "search" */
1091 u16 rate_mask
, s8 index
)
1093 /* "active" values */
1094 struct iwl_scale_tbl_info
*active_tbl
=
1095 &(lq_sta
->lq_info
[lq_sta
->active_tbl
]);
1096 s32 active_sr
= active_tbl
->win
[index
].success_ratio
;
1097 s32 active_tpt
= active_tbl
->expected_tpt
[index
];
1099 /* expected "search" throughput */
1100 s32
*tpt_tbl
= tbl
->expected_tpt
;
1102 s32 new_rate
, high
, low
, start_hi
;
1106 new_rate
= high
= low
= start_hi
= IWL_RATE_INVALID
;
1109 high_low
= rs_get_adjacent_rate(priv
, rate
, rate_mask
,
1112 low
= high_low
& 0xff;
1113 high
= (high_low
>> 8) & 0xff;
1116 * Lower the "search" bit rate, to give new "search" mode
1117 * approximately the same throughput as "active" if:
1119 * 1) "Active" mode has been working modestly well (but not
1120 * great), and expected "search" throughput (under perfect
1121 * conditions) at candidate rate is above the actual
1122 * measured "active" throughput (but less than expected
1123 * "active" throughput under perfect conditions).
1125 * 2) "Active" mode has been working perfectly or very well
1126 * and expected "search" throughput (under perfect
1127 * conditions) at candidate rate is above expected
1128 * "active" throughput (under perfect conditions).
1130 if ((((100 * tpt_tbl
[rate
]) > lq_sta
->last_tpt
) &&
1131 ((active_sr
> IWL_RATE_DECREASE_TH
) &&
1132 (active_sr
<= IWL_RATE_HIGH_TH
) &&
1133 (tpt_tbl
[rate
] <= active_tpt
))) ||
1134 ((active_sr
>= IWL_RATE_SCALE_SWITCH
) &&
1135 (tpt_tbl
[rate
] > active_tpt
))) {
1137 /* (2nd or later pass)
1138 * If we've already tried to raise the rate, and are
1139 * now trying to lower it, use the higher rate. */
1140 if (start_hi
!= IWL_RATE_INVALID
) {
1141 new_rate
= start_hi
;
1147 /* Loop again with lower rate */
1148 if (low
!= IWL_RATE_INVALID
)
1151 /* Lower rate not available, use the original */
1155 /* Else try to raise the "search" rate to match "active" */
1157 /* (2nd or later pass)
1158 * If we've already tried to lower the rate, and are
1159 * now trying to raise it, use the lower rate. */
1160 if (new_rate
!= IWL_RATE_INVALID
)
1163 /* Loop again with higher rate */
1164 else if (high
!= IWL_RATE_INVALID
) {
1168 /* Higher rate not available, use the original */
1180 * Set up search table for MIMO2
1182 static int rs_switch_to_mimo2(struct iwl_priv
*priv
,
1183 struct iwl_lq_sta
*lq_sta
,
1184 struct ieee80211_conf
*conf
,
1185 struct ieee80211_sta
*sta
,
1186 struct iwl_scale_tbl_info
*tbl
, int index
)
1190 s8 is_green
= lq_sta
->is_green
;
1192 if (!conf_is_ht(conf
) || !sta
->ht_cap
.ht_supported
)
1195 if (((sta
->ht_cap
.cap
& IEEE80211_HT_CAP_SM_PS
) >> 2)
1196 == WLAN_HT_CAP_SM_PS_STATIC
)
1199 /* Need both Tx chains/antennas to support MIMO */
1200 if (priv
->hw_params
.tx_chains_num
< 2)
1203 IWL_DEBUG_RATE(priv
, "LQ: try to switch to MIMO2\n");
1205 tbl
->lq_type
= LQ_MIMO2
;
1206 tbl
->is_dup
= lq_sta
->is_dup
;
1208 rate_mask
= lq_sta
->active_mimo2_rate
;
1210 if (iwl_is_fat_tx_allowed(priv
, &sta
->ht_cap
))
1215 /* FIXME: - don't toggle SGI here
1217 if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
1221 } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
1227 rs_set_expected_tpt_table(lq_sta
, tbl
);
1229 rate
= rs_get_best_rate(priv
, lq_sta
, tbl
, rate_mask
, index
);
1231 IWL_DEBUG_RATE(priv
, "LQ: MIMO2 best rate %d mask %X\n", rate
, rate_mask
);
1232 if ((rate
== IWL_RATE_INVALID
) || !((1 << rate
) & rate_mask
)) {
1233 IWL_DEBUG_RATE(priv
, "Can't switch with index %d rate mask %x\n",
1237 tbl
->current_rate
= rate_n_flags_from_tbl(priv
, tbl
, rate
, is_green
);
1239 IWL_DEBUG_RATE(priv
, "LQ: Switch to new mcs %X index is green %X\n",
1240 tbl
->current_rate
, is_green
);
1245 * Set up search table for MIMO3
1247 static int rs_switch_to_mimo3(struct iwl_priv
*priv
,
1248 struct iwl_lq_sta
*lq_sta
,
1249 struct ieee80211_conf
*conf
,
1250 struct ieee80211_sta
*sta
,
1251 struct iwl_scale_tbl_info
*tbl
, int index
)
1255 s8 is_green
= lq_sta
->is_green
;
1257 if (!conf_is_ht(conf
) || !sta
->ht_cap
.ht_supported
)
1260 if (((sta
->ht_cap
.cap
& IEEE80211_HT_CAP_SM_PS
) >> 2)
1261 == WLAN_HT_CAP_SM_PS_STATIC
)
1264 /* Need both Tx chains/antennas to support MIMO */
1265 if (priv
->hw_params
.tx_chains_num
< 3)
1268 IWL_DEBUG_RATE(priv
, "LQ: try to switch to MIMO3\n");
1270 tbl
->lq_type
= LQ_MIMO3
;
1271 tbl
->is_dup
= lq_sta
->is_dup
;
1273 rate_mask
= lq_sta
->active_mimo3_rate
;
1275 if (iwl_is_fat_tx_allowed(priv
, &sta
->ht_cap
))
1280 /* FIXME: - don't toggle SGI here
1282 if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
1286 } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
1292 rs_set_expected_tpt_table(lq_sta
, tbl
);
1294 rate
= rs_get_best_rate(priv
, lq_sta
, tbl
, rate_mask
, index
);
1296 IWL_DEBUG_RATE(priv
, "LQ: MIMO3 best rate %d mask %X\n",
1298 if ((rate
== IWL_RATE_INVALID
) || !((1 << rate
) & rate_mask
)) {
1299 IWL_DEBUG_RATE(priv
, "Can't switch with index %d rate mask %x\n",
1303 tbl
->current_rate
= rate_n_flags_from_tbl(priv
, tbl
, rate
, is_green
);
1305 IWL_DEBUG_RATE(priv
, "LQ: Switch to new mcs %X index is green %X\n",
1306 tbl
->current_rate
, is_green
);
1311 * Set up search table for SISO
1313 static int rs_switch_to_siso(struct iwl_priv
*priv
,
1314 struct iwl_lq_sta
*lq_sta
,
1315 struct ieee80211_conf
*conf
,
1316 struct ieee80211_sta
*sta
,
1317 struct iwl_scale_tbl_info
*tbl
, int index
)
1320 u8 is_green
= lq_sta
->is_green
;
1323 if (!conf_is_ht(conf
) || !sta
->ht_cap
.ht_supported
)
1326 IWL_DEBUG_RATE(priv
, "LQ: try to switch to SISO\n");
1328 tbl
->is_dup
= lq_sta
->is_dup
;
1329 tbl
->lq_type
= LQ_SISO
;
1331 rate_mask
= lq_sta
->active_siso_rate
;
1333 if (iwl_is_fat_tx_allowed(priv
, &sta
->ht_cap
))
1338 /* FIXME: - don't toggle SGI here
1340 if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
1344 } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
1351 tbl
->is_SGI
= 0; /*11n spec: no SGI in SISO+Greenfield*/
1353 rs_set_expected_tpt_table(lq_sta
, tbl
);
1354 rate
= rs_get_best_rate(priv
, lq_sta
, tbl
, rate_mask
, index
);
1356 IWL_DEBUG_RATE(priv
, "LQ: get best rate %d mask %X\n", rate
, rate_mask
);
1357 if ((rate
== IWL_RATE_INVALID
) || !((1 << rate
) & rate_mask
)) {
1358 IWL_DEBUG_RATE(priv
, "can not switch with index %d rate mask %x\n",
1362 tbl
->current_rate
= rate_n_flags_from_tbl(priv
, tbl
, rate
, is_green
);
1363 IWL_DEBUG_RATE(priv
, "LQ: Switch to new mcs %X index is green %X\n",
1364 tbl
->current_rate
, is_green
);
1369 * Try to switch to new modulation mode from legacy
1371 static int rs_move_legacy_other(struct iwl_priv
*priv
,
1372 struct iwl_lq_sta
*lq_sta
,
1373 struct ieee80211_conf
*conf
,
1374 struct ieee80211_sta
*sta
,
1377 struct iwl_scale_tbl_info
*tbl
= &(lq_sta
->lq_info
[lq_sta
->active_tbl
]);
1378 struct iwl_scale_tbl_info
*search_tbl
=
1379 &(lq_sta
->lq_info
[(1 - lq_sta
->active_tbl
)]);
1380 struct iwl_rate_scale_data
*window
= &(tbl
->win
[index
]);
1381 u32 sz
= (sizeof(struct iwl_scale_tbl_info
) -
1382 (sizeof(struct iwl_rate_scale_data
) * IWL_RATE_COUNT
));
1383 u8 start_action
= tbl
->action
;
1384 u8 valid_tx_ant
= priv
->hw_params
.valid_tx_ant
;
1385 u8 tx_chains_num
= priv
->hw_params
.tx_chains_num
;
1389 switch (tbl
->action
) {
1390 case IWL_LEGACY_SWITCH_ANTENNA1
:
1391 case IWL_LEGACY_SWITCH_ANTENNA2
:
1392 IWL_DEBUG_RATE(priv
, "LQ: Legacy toggle Antenna\n");
1394 lq_sta
->action_counter
++;
1396 if ((tbl
->action
== IWL_LEGACY_SWITCH_ANTENNA1
&&
1397 tx_chains_num
<= 1) ||
1398 (tbl
->action
== IWL_LEGACY_SWITCH_ANTENNA2
&&
1399 tx_chains_num
<= 2))
1402 /* Don't change antenna if success has been great */
1403 if (window
->success_ratio
>= IWL_RS_GOOD_RATIO
)
1406 /* Set up search table to try other antenna */
1407 memcpy(search_tbl
, tbl
, sz
);
1409 if (rs_toggle_antenna(valid_tx_ant
,
1410 &search_tbl
->current_rate
, search_tbl
)) {
1411 rs_set_expected_tpt_table(lq_sta
, search_tbl
);
1415 case IWL_LEGACY_SWITCH_SISO
:
1416 IWL_DEBUG_RATE(priv
, "LQ: Legacy switch to SISO\n");
1418 /* Set up search table to try SISO */
1419 memcpy(search_tbl
, tbl
, sz
);
1420 search_tbl
->is_SGI
= 0;
1421 ret
= rs_switch_to_siso(priv
, lq_sta
, conf
, sta
,
1424 lq_sta
->action_counter
= 0;
1429 case IWL_LEGACY_SWITCH_MIMO2_AB
:
1430 case IWL_LEGACY_SWITCH_MIMO2_AC
:
1431 case IWL_LEGACY_SWITCH_MIMO2_BC
:
1432 IWL_DEBUG_RATE(priv
, "LQ: Legacy switch to MIMO2\n");
1434 /* Set up search table to try MIMO */
1435 memcpy(search_tbl
, tbl
, sz
);
1436 search_tbl
->is_SGI
= 0;
1438 if (tbl
->action
== IWL_LEGACY_SWITCH_MIMO2_AB
)
1439 search_tbl
->ant_type
= ANT_AB
;
1440 else if (tbl
->action
== IWL_LEGACY_SWITCH_MIMO2_AC
)
1441 search_tbl
->ant_type
= ANT_AC
;
1443 search_tbl
->ant_type
= ANT_BC
;
1445 if (!rs_is_valid_ant(valid_tx_ant
, search_tbl
->ant_type
))
1448 ret
= rs_switch_to_mimo2(priv
, lq_sta
, conf
, sta
,
1451 lq_sta
->action_counter
= 0;
1456 case IWL_LEGACY_SWITCH_MIMO3_ABC
:
1457 IWL_DEBUG_RATE(priv
, "LQ: Legacy switch to MIMO3\n");
1459 /* Set up search table to try MIMO3 */
1460 memcpy(search_tbl
, tbl
, sz
);
1461 search_tbl
->is_SGI
= 0;
1463 search_tbl
->ant_type
= ANT_ABC
;
1465 if (!rs_is_valid_ant(valid_tx_ant
, search_tbl
->ant_type
))
1468 ret
= rs_switch_to_mimo3(priv
, lq_sta
, conf
, sta
,
1471 lq_sta
->action_counter
= 0;
1477 if (tbl
->action
> IWL_LEGACY_SWITCH_MIMO3_ABC
)
1478 tbl
->action
= IWL_LEGACY_SWITCH_ANTENNA1
;
1480 if (tbl
->action
== start_action
)
1484 search_tbl
->lq_type
= LQ_NONE
;
1488 lq_sta
->search_better_tbl
= 1;
1490 if (tbl
->action
> IWL_LEGACY_SWITCH_MIMO3_ABC
)
1491 tbl
->action
= IWL_LEGACY_SWITCH_ANTENNA1
;
1497 * Try to switch to new modulation mode from SISO
1499 static int rs_move_siso_to_other(struct iwl_priv
*priv
,
1500 struct iwl_lq_sta
*lq_sta
,
1501 struct ieee80211_conf
*conf
,
1502 struct ieee80211_sta
*sta
, int index
)
1504 u8 is_green
= lq_sta
->is_green
;
1505 struct iwl_scale_tbl_info
*tbl
= &(lq_sta
->lq_info
[lq_sta
->active_tbl
]);
1506 struct iwl_scale_tbl_info
*search_tbl
=
1507 &(lq_sta
->lq_info
[(1 - lq_sta
->active_tbl
)]);
1508 struct iwl_rate_scale_data
*window
= &(tbl
->win
[index
]);
1509 u32 sz
= (sizeof(struct iwl_scale_tbl_info
) -
1510 (sizeof(struct iwl_rate_scale_data
) * IWL_RATE_COUNT
));
1511 u8 start_action
= tbl
->action
;
1512 u8 valid_tx_ant
= priv
->hw_params
.valid_tx_ant
;
1513 u8 tx_chains_num
= priv
->hw_params
.tx_chains_num
;
1517 lq_sta
->action_counter
++;
1518 switch (tbl
->action
) {
1519 case IWL_SISO_SWITCH_ANTENNA1
:
1520 case IWL_SISO_SWITCH_ANTENNA2
:
1521 IWL_DEBUG_RATE(priv
, "LQ: SISO toggle Antenna\n");
1523 if ((tbl
->action
== IWL_SISO_SWITCH_ANTENNA1
&&
1524 tx_chains_num
<= 1) ||
1525 (tbl
->action
== IWL_SISO_SWITCH_ANTENNA2
&&
1526 tx_chains_num
<= 2))
1529 if (window
->success_ratio
>= IWL_RS_GOOD_RATIO
)
1532 memcpy(search_tbl
, tbl
, sz
);
1533 if (rs_toggle_antenna(valid_tx_ant
,
1534 &search_tbl
->current_rate
, search_tbl
))
1537 case IWL_SISO_SWITCH_MIMO2_AB
:
1538 case IWL_SISO_SWITCH_MIMO2_AC
:
1539 case IWL_SISO_SWITCH_MIMO2_BC
:
1540 IWL_DEBUG_RATE(priv
, "LQ: SISO switch to MIMO2\n");
1541 memcpy(search_tbl
, tbl
, sz
);
1542 search_tbl
->is_SGI
= 0;
1544 if (tbl
->action
== IWL_SISO_SWITCH_MIMO2_AB
)
1545 search_tbl
->ant_type
= ANT_AB
;
1546 else if (tbl
->action
== IWL_SISO_SWITCH_MIMO2_AC
)
1547 search_tbl
->ant_type
= ANT_AC
;
1549 search_tbl
->ant_type
= ANT_BC
;
1551 if (!rs_is_valid_ant(valid_tx_ant
, search_tbl
->ant_type
))
1554 ret
= rs_switch_to_mimo2(priv
, lq_sta
, conf
, sta
,
1559 case IWL_SISO_SWITCH_GI
:
1561 !(priv
->current_ht_config
.sgf
&
1565 !(priv
->current_ht_config
.sgf
&
1569 IWL_DEBUG_RATE(priv
, "LQ: SISO toggle SGI/NGI\n");
1571 memcpy(search_tbl
, tbl
, sz
);
1577 "SGI was set in GF+SISO\n");
1579 search_tbl
->is_SGI
= !tbl
->is_SGI
;
1580 rs_set_expected_tpt_table(lq_sta
, search_tbl
);
1582 s32 tpt
= lq_sta
->last_tpt
/ 100;
1583 if (tpt
>= search_tbl
->expected_tpt
[index
])
1586 search_tbl
->current_rate
=
1587 rate_n_flags_from_tbl(priv
, search_tbl
,
1590 case IWL_SISO_SWITCH_MIMO3_ABC
:
1591 IWL_DEBUG_RATE(priv
, "LQ: SISO switch to MIMO3\n");
1592 memcpy(search_tbl
, tbl
, sz
);
1593 search_tbl
->is_SGI
= 0;
1594 search_tbl
->ant_type
= ANT_ABC
;
1596 if (!rs_is_valid_ant(valid_tx_ant
, search_tbl
->ant_type
))
1599 ret
= rs_switch_to_mimo3(priv
, lq_sta
, conf
, sta
,
1606 if (tbl
->action
> IWL_LEGACY_SWITCH_MIMO3_ABC
)
1607 tbl
->action
= IWL_SISO_SWITCH_ANTENNA1
;
1609 if (tbl
->action
== start_action
)
1612 search_tbl
->lq_type
= LQ_NONE
;
1616 lq_sta
->search_better_tbl
= 1;
1618 if (tbl
->action
> IWL_SISO_SWITCH_MIMO3_ABC
)
1619 tbl
->action
= IWL_SISO_SWITCH_ANTENNA1
;
1624 * Try to switch to new modulation mode from MIMO2
1626 static int rs_move_mimo2_to_other(struct iwl_priv
*priv
,
1627 struct iwl_lq_sta
*lq_sta
,
1628 struct ieee80211_conf
*conf
,
1629 struct ieee80211_sta
*sta
, int index
)
1631 s8 is_green
= lq_sta
->is_green
;
1632 struct iwl_scale_tbl_info
*tbl
= &(lq_sta
->lq_info
[lq_sta
->active_tbl
]);
1633 struct iwl_scale_tbl_info
*search_tbl
=
1634 &(lq_sta
->lq_info
[(1 - lq_sta
->active_tbl
)]);
1635 struct iwl_rate_scale_data
*window
= &(tbl
->win
[index
]);
1636 u32 sz
= (sizeof(struct iwl_scale_tbl_info
) -
1637 (sizeof(struct iwl_rate_scale_data
) * IWL_RATE_COUNT
));
1638 u8 start_action
= tbl
->action
;
1639 u8 valid_tx_ant
= priv
->hw_params
.valid_tx_ant
;
1640 u8 tx_chains_num
= priv
->hw_params
.tx_chains_num
;
1644 lq_sta
->action_counter
++;
1645 switch (tbl
->action
) {
1646 case IWL_MIMO2_SWITCH_ANTENNA1
:
1647 case IWL_MIMO2_SWITCH_ANTENNA2
:
1648 IWL_DEBUG_RATE(priv
, "LQ: MIMO2 toggle Antennas\n");
1650 if (tx_chains_num
<= 2)
1653 if (window
->success_ratio
>= IWL_RS_GOOD_RATIO
)
1656 memcpy(search_tbl
, tbl
, sz
);
1657 if (rs_toggle_antenna(valid_tx_ant
,
1658 &search_tbl
->current_rate
, search_tbl
))
1661 case IWL_MIMO2_SWITCH_SISO_A
:
1662 case IWL_MIMO2_SWITCH_SISO_B
:
1663 case IWL_MIMO2_SWITCH_SISO_C
:
1664 IWL_DEBUG_RATE(priv
, "LQ: MIMO2 switch to SISO\n");
1666 /* Set up new search table for SISO */
1667 memcpy(search_tbl
, tbl
, sz
);
1669 if (tbl
->action
== IWL_MIMO2_SWITCH_SISO_A
)
1670 search_tbl
->ant_type
= ANT_A
;
1671 else if (tbl
->action
== IWL_MIMO2_SWITCH_SISO_B
)
1672 search_tbl
->ant_type
= ANT_B
;
1674 search_tbl
->ant_type
= ANT_C
;
1676 if (!rs_is_valid_ant(valid_tx_ant
, search_tbl
->ant_type
))
1679 ret
= rs_switch_to_siso(priv
, lq_sta
, conf
, sta
,
1686 case IWL_MIMO2_SWITCH_GI
:
1688 !(priv
->current_ht_config
.sgf
&
1692 !(priv
->current_ht_config
.sgf
&
1696 IWL_DEBUG_RATE(priv
, "LQ: MIMO2 toggle SGI/NGI\n");
1698 /* Set up new search table for MIMO2 */
1699 memcpy(search_tbl
, tbl
, sz
);
1700 search_tbl
->is_SGI
= !tbl
->is_SGI
;
1701 rs_set_expected_tpt_table(lq_sta
, search_tbl
);
1703 * If active table already uses the fastest possible
1704 * modulation (dual stream with short guard interval),
1705 * and it's working well, there's no need to look
1706 * for a better type of modulation!
1709 s32 tpt
= lq_sta
->last_tpt
/ 100;
1710 if (tpt
>= search_tbl
->expected_tpt
[index
])
1713 search_tbl
->current_rate
=
1714 rate_n_flags_from_tbl(priv
, search_tbl
,
1718 case IWL_MIMO2_SWITCH_MIMO3_ABC
:
1719 IWL_DEBUG_RATE(priv
, "LQ: MIMO2 switch to MIMO3\n");
1720 memcpy(search_tbl
, tbl
, sz
);
1721 search_tbl
->is_SGI
= 0;
1722 search_tbl
->ant_type
= ANT_ABC
;
1724 if (!rs_is_valid_ant(valid_tx_ant
, search_tbl
->ant_type
))
1727 ret
= rs_switch_to_mimo3(priv
, lq_sta
, conf
, sta
,
1735 if (tbl
->action
> IWL_MIMO2_SWITCH_MIMO3_ABC
)
1736 tbl
->action
= IWL_MIMO2_SWITCH_ANTENNA1
;
1738 if (tbl
->action
== start_action
)
1741 search_tbl
->lq_type
= LQ_NONE
;
1744 lq_sta
->search_better_tbl
= 1;
1746 if (tbl
->action
> IWL_MIMO2_SWITCH_MIMO3_ABC
)
1747 tbl
->action
= IWL_MIMO2_SWITCH_ANTENNA1
;
1753 * Try to switch to new modulation mode from MIMO3
1755 static int rs_move_mimo3_to_other(struct iwl_priv
*priv
,
1756 struct iwl_lq_sta
*lq_sta
,
1757 struct ieee80211_conf
*conf
,
1758 struct ieee80211_sta
*sta
, int index
)
1760 s8 is_green
= lq_sta
->is_green
;
1761 struct iwl_scale_tbl_info
*tbl
= &(lq_sta
->lq_info
[lq_sta
->active_tbl
]);
1762 struct iwl_scale_tbl_info
*search_tbl
=
1763 &(lq_sta
->lq_info
[(1 - lq_sta
->active_tbl
)]);
1764 struct iwl_rate_scale_data
*window
= &(tbl
->win
[index
]);
1765 u32 sz
= (sizeof(struct iwl_scale_tbl_info
) -
1766 (sizeof(struct iwl_rate_scale_data
) * IWL_RATE_COUNT
));
1767 u8 start_action
= tbl
->action
;
1768 u8 valid_tx_ant
= priv
->hw_params
.valid_tx_ant
;
1769 u8 tx_chains_num
= priv
->hw_params
.tx_chains_num
;
1773 lq_sta
->action_counter
++;
1774 switch (tbl
->action
) {
1775 case IWL_MIMO3_SWITCH_ANTENNA1
:
1776 case IWL_MIMO3_SWITCH_ANTENNA2
:
1777 IWL_DEBUG_RATE(priv
, "LQ: MIMO3 toggle Antennas\n");
1779 if (tx_chains_num
<= 3)
1782 if (window
->success_ratio
>= IWL_RS_GOOD_RATIO
)
1785 memcpy(search_tbl
, tbl
, sz
);
1786 if (rs_toggle_antenna(valid_tx_ant
,
1787 &search_tbl
->current_rate
, search_tbl
))
1790 case IWL_MIMO3_SWITCH_SISO_A
:
1791 case IWL_MIMO3_SWITCH_SISO_B
:
1792 case IWL_MIMO3_SWITCH_SISO_C
:
1793 IWL_DEBUG_RATE(priv
, "LQ: MIMO3 switch to SISO\n");
1795 /* Set up new search table for SISO */
1796 memcpy(search_tbl
, tbl
, sz
);
1798 if (tbl
->action
== IWL_MIMO3_SWITCH_SISO_A
)
1799 search_tbl
->ant_type
= ANT_A
;
1800 else if (tbl
->action
== IWL_MIMO3_SWITCH_SISO_B
)
1801 search_tbl
->ant_type
= ANT_B
;
1803 search_tbl
->ant_type
= ANT_C
;
1805 if (!rs_is_valid_ant(valid_tx_ant
, search_tbl
->ant_type
))
1808 ret
= rs_switch_to_siso(priv
, lq_sta
, conf
, sta
,
1815 case IWL_MIMO3_SWITCH_MIMO2_AB
:
1816 case IWL_MIMO3_SWITCH_MIMO2_AC
:
1817 case IWL_MIMO3_SWITCH_MIMO2_BC
:
1818 IWL_DEBUG_RATE(priv
, "LQ: MIMO3 switch to MIMO2\n");
1820 memcpy(search_tbl
, tbl
, sz
);
1821 search_tbl
->is_SGI
= 0;
1822 if (tbl
->action
== IWL_MIMO3_SWITCH_MIMO2_AB
)
1823 search_tbl
->ant_type
= ANT_AB
;
1824 else if (tbl
->action
== IWL_MIMO3_SWITCH_MIMO2_AC
)
1825 search_tbl
->ant_type
= ANT_AC
;
1827 search_tbl
->ant_type
= ANT_BC
;
1829 if (!rs_is_valid_ant(valid_tx_ant
, search_tbl
->ant_type
))
1832 ret
= rs_switch_to_mimo2(priv
, lq_sta
, conf
, sta
,
1839 case IWL_MIMO3_SWITCH_GI
:
1841 !(priv
->current_ht_config
.sgf
&
1845 !(priv
->current_ht_config
.sgf
&
1849 IWL_DEBUG_RATE(priv
, "LQ: MIMO3 toggle SGI/NGI\n");
1851 /* Set up new search table for MIMO */
1852 memcpy(search_tbl
, tbl
, sz
);
1853 search_tbl
->is_SGI
= !tbl
->is_SGI
;
1854 rs_set_expected_tpt_table(lq_sta
, search_tbl
);
1856 * If active table already uses the fastest possible
1857 * modulation (dual stream with short guard interval),
1858 * and it's working well, there's no need to look
1859 * for a better type of modulation!
1862 s32 tpt
= lq_sta
->last_tpt
/ 100;
1863 if (tpt
>= search_tbl
->expected_tpt
[index
])
1866 search_tbl
->current_rate
=
1867 rate_n_flags_from_tbl(priv
, search_tbl
,
1872 if (tbl
->action
> IWL_MIMO3_SWITCH_GI
)
1873 tbl
->action
= IWL_MIMO3_SWITCH_ANTENNA1
;
1875 if (tbl
->action
== start_action
)
1878 search_tbl
->lq_type
= LQ_NONE
;
1881 lq_sta
->search_better_tbl
= 1;
1883 if (tbl
->action
> IWL_MIMO3_SWITCH_GI
)
1884 tbl
->action
= IWL_MIMO3_SWITCH_ANTENNA1
;
1890 * Check whether we should continue using same modulation mode, or
1891 * begin search for a new mode, based on:
1892 * 1) # tx successes or failures while using this mode
1893 * 2) # times calling this function
1894 * 3) elapsed time in this mode (not used, for now)
1896 static void rs_stay_in_table(struct iwl_lq_sta
*lq_sta
)
1898 struct iwl_scale_tbl_info
*tbl
;
1901 int flush_interval_passed
= 0;
1902 struct iwl_priv
*priv
;
1905 active_tbl
= lq_sta
->active_tbl
;
1907 tbl
= &(lq_sta
->lq_info
[active_tbl
]);
1909 /* If we've been disallowing search, see if we should now allow it */
1910 if (lq_sta
->stay_in_tbl
) {
1912 /* Elapsed time using current modulation mode */
1913 if (lq_sta
->flush_timer
)
1914 flush_interval_passed
=
1916 (unsigned long)(lq_sta
->flush_timer
+
1917 IWL_RATE_SCALE_FLUSH_INTVL
));
1920 * Check if we should allow search for new modulation mode.
1921 * If many frames have failed or succeeded, or we've used
1922 * this same modulation for a long time, allow search, and
1923 * reset history stats that keep track of whether we should
1924 * allow a new search. Also (below) reset all bitmaps and
1925 * stats in active history.
1927 if ((lq_sta
->total_failed
> lq_sta
->max_failure_limit
) ||
1928 (lq_sta
->total_success
> lq_sta
->max_success_limit
) ||
1929 ((!lq_sta
->search_better_tbl
) && (lq_sta
->flush_timer
)
1930 && (flush_interval_passed
))) {
1931 IWL_DEBUG_RATE(priv
, "LQ: stay is expired %d %d %d\n:",
1932 lq_sta
->total_failed
,
1933 lq_sta
->total_success
,
1934 flush_interval_passed
);
1936 /* Allow search for new mode */
1937 lq_sta
->stay_in_tbl
= 0; /* only place reset */
1938 lq_sta
->total_failed
= 0;
1939 lq_sta
->total_success
= 0;
1940 lq_sta
->flush_timer
= 0;
1943 * Else if we've used this modulation mode enough repetitions
1944 * (regardless of elapsed time or success/failure), reset
1945 * history bitmaps and rate-specific stats for all rates in
1949 lq_sta
->table_count
++;
1950 if (lq_sta
->table_count
>=
1951 lq_sta
->table_count_limit
) {
1952 lq_sta
->table_count
= 0;
1954 IWL_DEBUG_RATE(priv
, "LQ: stay in table clear win\n");
1955 for (i
= 0; i
< IWL_RATE_COUNT
; i
++)
1956 rs_rate_scale_clear_window(
1961 /* If transitioning to allow "search", reset all history
1962 * bitmaps and stats in active table (this will become the new
1963 * "search" table). */
1964 if (!lq_sta
->stay_in_tbl
) {
1965 for (i
= 0; i
< IWL_RATE_COUNT
; i
++)
1966 rs_rate_scale_clear_window(&(tbl
->win
[i
]));
1972 * Do rate scaling and search for new modulation mode.
1974 static void rs_rate_scale_perform(struct iwl_priv
*priv
,
1975 struct sk_buff
*skb
,
1976 struct ieee80211_sta
*sta
,
1977 struct iwl_lq_sta
*lq_sta
)
1979 struct ieee80211_hw
*hw
= priv
->hw
;
1980 struct ieee80211_conf
*conf
= &hw
->conf
;
1981 struct ieee80211_tx_info
*info
= IEEE80211_SKB_CB(skb
);
1982 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*)skb
->data
;
1983 int low
= IWL_RATE_INVALID
;
1984 int high
= IWL_RATE_INVALID
;
1987 struct iwl_rate_scale_data
*window
= NULL
;
1988 int current_tpt
= IWL_INVALID_VALUE
;
1989 int low_tpt
= IWL_INVALID_VALUE
;
1990 int high_tpt
= IWL_INVALID_VALUE
;
1992 s8 scale_action
= 0;
1995 struct iwl_scale_tbl_info
*tbl
, *tbl1
;
1996 u16 rate_scale_index_msk
= 0;
2003 u8 tid
= MAX_TID_COUNT
;
2004 struct iwl_tid_data
*tid_data
;
2006 IWL_DEBUG_RATE(priv
, "rate scale calculate new rate for skb\n");
2008 /* Send management frames and NO_ACK data using lowest rate. */
2009 /* TODO: this could probably be improved.. */
2010 if (!ieee80211_is_data(hdr
->frame_control
) ||
2011 info
->flags
& IEEE80211_TX_CTL_NO_ACK
)
2014 if (!sta
|| !lq_sta
)
2017 lq_sta
->supp_rates
= sta
->supp_rates
[lq_sta
->band
];
2019 tid
= rs_tl_add_packet(lq_sta
, hdr
);
2022 * Select rate-scale / modulation-mode table to work with in
2023 * the rest of this function: "search" if searching for better
2024 * modulation mode, or "active" if doing rate scaling within a mode.
2026 if (!lq_sta
->search_better_tbl
)
2027 active_tbl
= lq_sta
->active_tbl
;
2029 active_tbl
= 1 - lq_sta
->active_tbl
;
2031 tbl
= &(lq_sta
->lq_info
[active_tbl
]);
2032 is_green
= lq_sta
->is_green
;
2034 /* current tx rate */
2035 index
= lq_sta
->last_txrate_idx
;
2037 IWL_DEBUG_RATE(priv
, "Rate scale index %d for type %d\n", index
,
2040 /* rates available for this association, and for modulation mode */
2041 rate_mask
= rs_get_supported_rates(lq_sta
, hdr
, tbl
->lq_type
);
2043 IWL_DEBUG_RATE(priv
, "mask 0x%04X \n", rate_mask
);
2045 /* mask with station rate restriction */
2046 if (is_legacy(tbl
->lq_type
)) {
2047 if (lq_sta
->band
== IEEE80211_BAND_5GHZ
)
2048 /* supp_rates has no CCK bits in A mode */
2049 rate_scale_index_msk
= (u16
) (rate_mask
&
2050 (lq_sta
->supp_rates
<< IWL_FIRST_OFDM_RATE
));
2052 rate_scale_index_msk
= (u16
) (rate_mask
&
2053 lq_sta
->supp_rates
);
2056 rate_scale_index_msk
= rate_mask
;
2058 if (!rate_scale_index_msk
)
2059 rate_scale_index_msk
= rate_mask
;
2061 if (!((1 << index
) & rate_scale_index_msk
)) {
2062 IWL_ERR(priv
, "Current Rate is not valid\n");
2066 /* Get expected throughput table and history window for current rate */
2067 if (!tbl
->expected_tpt
) {
2068 IWL_ERR(priv
, "tbl->expected_tpt is NULL\n");
2072 /* force user max rate if set by user */
2073 if ((lq_sta
->max_rate_idx
!= -1) &&
2074 (lq_sta
->max_rate_idx
< index
)) {
2075 index
= lq_sta
->max_rate_idx
;
2077 window
= &(tbl
->win
[index
]);
2081 window
= &(tbl
->win
[index
]);
2084 * If there is not enough history to calculate actual average
2085 * throughput, keep analyzing results of more tx frames, without
2086 * changing rate or mode (bypass most of the rest of this function).
2087 * Set up new rate table in uCode only if old rate is not supported
2088 * in current association (use new rate found above).
2090 fail_count
= window
->counter
- window
->success_counter
;
2091 if ((fail_count
< IWL_RATE_MIN_FAILURE_TH
) &&
2092 (window
->success_counter
< IWL_RATE_MIN_SUCCESS_TH
)) {
2093 IWL_DEBUG_RATE(priv
, "LQ: still below TH. succ=%d total=%d "
2095 window
->success_counter
, window
->counter
, index
);
2097 /* Can't calculate this yet; not enough history */
2098 window
->average_tpt
= IWL_INVALID_VALUE
;
2100 /* Should we stay with this modulation mode,
2101 * or search for a new one? */
2102 rs_stay_in_table(lq_sta
);
2107 /* Else we have enough samples; calculate estimate of
2108 * actual average throughput */
2110 BUG_ON(window
->average_tpt
!= ((window
->success_ratio
*
2111 tbl
->expected_tpt
[index
] + 64) / 128));
2113 /* If we are searching for better modulation mode, check success. */
2114 if (lq_sta
->search_better_tbl
) {
2116 /* If good success, continue using the "search" mode;
2117 * no need to send new link quality command, since we're
2118 * continuing to use the setup that we've been trying. */
2119 if (window
->average_tpt
> lq_sta
->last_tpt
) {
2121 IWL_DEBUG_RATE(priv
, "LQ: SWITCHING TO NEW TABLE "
2122 "suc=%d cur-tpt=%d old-tpt=%d\n",
2123 window
->success_ratio
,
2124 window
->average_tpt
,
2127 if (!is_legacy(tbl
->lq_type
))
2128 lq_sta
->enable_counter
= 1;
2130 /* Swap tables; "search" becomes "active" */
2131 lq_sta
->active_tbl
= active_tbl
;
2132 current_tpt
= window
->average_tpt
;
2134 /* Else poor success; go back to mode in "active" table */
2137 IWL_DEBUG_RATE(priv
, "LQ: GOING BACK TO THE OLD TABLE "
2138 "suc=%d cur-tpt=%d old-tpt=%d\n",
2139 window
->success_ratio
,
2140 window
->average_tpt
,
2143 /* Nullify "search" table */
2144 tbl
->lq_type
= LQ_NONE
;
2146 /* Revert to "active" table */
2147 active_tbl
= lq_sta
->active_tbl
;
2148 tbl
= &(lq_sta
->lq_info
[active_tbl
]);
2150 /* Revert to "active" rate and throughput info */
2151 index
= iwl_hwrate_to_plcp_idx(tbl
->current_rate
);
2152 current_tpt
= lq_sta
->last_tpt
;
2154 /* Need to set up a new rate table in uCode */
2158 /* Either way, we've made a decision; modulation mode
2159 * search is done, allow rate adjustment next time. */
2160 lq_sta
->search_better_tbl
= 0;
2161 done_search
= 1; /* Don't switch modes below! */
2165 /* (Else) not in search of better modulation mode, try for better
2166 * starting rate, while staying in this mode. */
2167 high_low
= rs_get_adjacent_rate(priv
, index
, rate_scale_index_msk
,
2169 low
= high_low
& 0xff;
2170 high
= (high_low
>> 8) & 0xff;
2172 /* If user set max rate, dont allow higher than user constrain */
2173 if ((lq_sta
->max_rate_idx
!= -1) &&
2174 (lq_sta
->max_rate_idx
< high
))
2175 high
= IWL_RATE_INVALID
;
2177 sr
= window
->success_ratio
;
2179 /* Collect measured throughputs for current and adjacent rates */
2180 current_tpt
= window
->average_tpt
;
2181 if (low
!= IWL_RATE_INVALID
)
2182 low_tpt
= tbl
->win
[low
].average_tpt
;
2183 if (high
!= IWL_RATE_INVALID
)
2184 high_tpt
= tbl
->win
[high
].average_tpt
;
2188 /* Too many failures, decrease rate */
2189 if ((sr
<= IWL_RATE_DECREASE_TH
) || (current_tpt
== 0)) {
2190 IWL_DEBUG_RATE(priv
, "decrease rate because of low success_ratio\n");
2193 /* No throughput measured yet for adjacent rates; try increase. */
2194 } else if ((low_tpt
== IWL_INVALID_VALUE
) &&
2195 (high_tpt
== IWL_INVALID_VALUE
)) {
2197 if (high
!= IWL_RATE_INVALID
&& sr
>= IWL_RATE_INCREASE_TH
)
2199 else if (low
!= IWL_RATE_INVALID
)
2203 /* Both adjacent throughputs are measured, but neither one has better
2204 * throughput; we're using the best rate, don't change it! */
2205 else if ((low_tpt
!= IWL_INVALID_VALUE
) &&
2206 (high_tpt
!= IWL_INVALID_VALUE
) &&
2207 (low_tpt
< current_tpt
) &&
2208 (high_tpt
< current_tpt
))
2211 /* At least one adjacent rate's throughput is measured,
2212 * and may have better performance. */
2214 /* Higher adjacent rate's throughput is measured */
2215 if (high_tpt
!= IWL_INVALID_VALUE
) {
2216 /* Higher rate has better throughput */
2217 if (high_tpt
> current_tpt
&&
2218 sr
>= IWL_RATE_INCREASE_TH
) {
2224 /* Lower adjacent rate's throughput is measured */
2225 } else if (low_tpt
!= IWL_INVALID_VALUE
) {
2226 /* Lower rate has better throughput */
2227 if (low_tpt
> current_tpt
) {
2228 IWL_DEBUG_RATE(priv
,
2229 "decrease rate because of low tpt\n");
2231 } else if (sr
>= IWL_RATE_INCREASE_TH
) {
2237 /* Sanity check; asked for decrease, but success rate or throughput
2238 * has been good at old rate. Don't change it. */
2239 if ((scale_action
== -1) && (low
!= IWL_RATE_INVALID
) &&
2240 ((sr
> IWL_RATE_HIGH_TH
) ||
2241 (current_tpt
> (100 * tbl
->expected_tpt
[low
]))))
2244 switch (scale_action
) {
2246 /* Decrease starting rate, update uCode's rate table */
2247 if (low
!= IWL_RATE_INVALID
) {
2254 /* Increase starting rate, update uCode's rate table */
2255 if (high
!= IWL_RATE_INVALID
) {
2267 IWL_DEBUG_RATE(priv
, "choose rate scale index %d action %d low %d "
2268 "high %d type %d\n",
2269 index
, scale_action
, low
, high
, tbl
->lq_type
);
2272 /* Replace uCode's rate table for the destination station. */
2274 rate
= rate_n_flags_from_tbl(priv
, tbl
, index
, is_green
);
2275 rs_fill_link_cmd(priv
, lq_sta
, rate
);
2276 iwl_send_lq_cmd(priv
, &lq_sta
->lq
, CMD_ASYNC
);
2279 /* Should we stay with this modulation mode, or search for a new one? */
2280 rs_stay_in_table(lq_sta
);
2283 * Search for new modulation mode if we're:
2284 * 1) Not changing rates right now
2285 * 2) Not just finishing up a search
2286 * 3) Allowing a new search
2288 if (!update_lq
&& !done_search
&& !lq_sta
->stay_in_tbl
&& window
->counter
) {
2289 /* Save current throughput to compare with "search" throughput*/
2290 lq_sta
->last_tpt
= current_tpt
;
2292 /* Select a new "search" modulation mode to try.
2293 * If one is found, set up the new "search" table. */
2294 if (is_legacy(tbl
->lq_type
))
2295 rs_move_legacy_other(priv
, lq_sta
, conf
, sta
, index
);
2296 else if (is_siso(tbl
->lq_type
))
2297 rs_move_siso_to_other(priv
, lq_sta
, conf
, sta
, index
);
2298 else if (is_mimo2(tbl
->lq_type
))
2299 rs_move_mimo2_to_other(priv
, lq_sta
, conf
, sta
, index
);
2301 rs_move_mimo3_to_other(priv
, lq_sta
, conf
, sta
, index
);
2303 /* If new "search" mode was selected, set up in uCode table */
2304 if (lq_sta
->search_better_tbl
) {
2305 /* Access the "search" table, clear its history. */
2306 tbl
= &(lq_sta
->lq_info
[(1 - lq_sta
->active_tbl
)]);
2307 for (i
= 0; i
< IWL_RATE_COUNT
; i
++)
2308 rs_rate_scale_clear_window(&(tbl
->win
[i
]));
2310 /* Use new "search" start rate */
2311 index
= iwl_hwrate_to_plcp_idx(tbl
->current_rate
);
2313 IWL_DEBUG_RATE(priv
, "Switch current mcs: %X index: %d\n",
2314 tbl
->current_rate
, index
);
2315 rs_fill_link_cmd(priv
, lq_sta
, tbl
->current_rate
);
2316 iwl_send_lq_cmd(priv
, &lq_sta
->lq
, CMD_ASYNC
);
2321 if (done_search
&& !lq_sta
->stay_in_tbl
) {
2322 /* If the "active" (non-search) mode was legacy,
2323 * and we've tried switching antennas,
2324 * but we haven't been able to try HT modes (not available),
2325 * stay with best antenna legacy modulation for a while
2326 * before next round of mode comparisons. */
2327 tbl1
= &(lq_sta
->lq_info
[lq_sta
->active_tbl
]);
2328 if (is_legacy(tbl1
->lq_type
) && !conf_is_ht(conf
) &&
2329 lq_sta
->action_counter
>= 1) {
2330 lq_sta
->action_counter
= 0;
2331 IWL_DEBUG_RATE(priv
, "LQ: STAY in legacy table\n");
2332 rs_set_stay_in_table(priv
, 1, lq_sta
);
2335 /* If we're in an HT mode, and all 3 mode switch actions
2336 * have been tried and compared, stay in this best modulation
2337 * mode for a while before next round of mode comparisons. */
2338 if (lq_sta
->enable_counter
&&
2339 (lq_sta
->action_counter
>= IWL_ACTION_LIMIT
)) {
2340 if ((lq_sta
->last_tpt
> IWL_AGG_TPT_THREHOLD
) &&
2341 (lq_sta
->tx_agg_tid_en
& (1 << tid
)) &&
2342 (tid
!= MAX_TID_COUNT
)) {
2344 &priv
->stations
[lq_sta
->lq
.sta_id
].tid
[tid
];
2345 if (tid_data
->agg
.state
== IWL_AGG_OFF
) {
2346 IWL_DEBUG_RATE(priv
,
2347 "try to aggregate tid %d\n",
2349 rs_tl_turn_on_agg(priv
, tid
,
2353 lq_sta
->action_counter
= 0;
2354 rs_set_stay_in_table(priv
, 0, lq_sta
);
2359 tbl
->current_rate
= rate_n_flags_from_tbl(priv
, tbl
, index
, is_green
);
2361 lq_sta
->last_txrate_idx
= i
;
2367 static void rs_initialize_lq(struct iwl_priv
*priv
,
2368 struct ieee80211_conf
*conf
,
2369 struct ieee80211_sta
*sta
,
2370 struct iwl_lq_sta
*lq_sta
)
2372 struct iwl_scale_tbl_info
*tbl
;
2376 u8 use_green
= rs_use_green(priv
, conf
);
2380 if (!sta
|| !lq_sta
)
2383 i
= lq_sta
->last_txrate_idx
;
2385 if ((lq_sta
->lq
.sta_id
== 0xff) &&
2386 (priv
->iw_mode
== NL80211_IFTYPE_ADHOC
))
2389 valid_tx_ant
= priv
->hw_params
.valid_tx_ant
;
2391 if (!lq_sta
->search_better_tbl
)
2392 active_tbl
= lq_sta
->active_tbl
;
2394 active_tbl
= 1 - lq_sta
->active_tbl
;
2396 tbl
= &(lq_sta
->lq_info
[active_tbl
]);
2398 if ((i
< 0) || (i
>= IWL_RATE_COUNT
))
2401 rate
= iwl_rates
[i
].plcp
;
2402 tbl
->ant_type
= first_antenna(valid_tx_ant
);
2403 rate
|= tbl
->ant_type
<< RATE_MCS_ANT_POS
;
2405 if (i
>= IWL_FIRST_CCK_RATE
&& i
<= IWL_LAST_CCK_RATE
)
2406 rate
|= RATE_MCS_CCK_MSK
;
2408 rs_get_tbl_info_from_mcs(rate
, priv
->band
, tbl
, &rate_idx
);
2409 if (!rs_is_valid_ant(valid_tx_ant
, tbl
->ant_type
))
2410 rs_toggle_antenna(valid_tx_ant
, &rate
, tbl
);
2412 rate
= rate_n_flags_from_tbl(priv
, tbl
, rate_idx
, use_green
);
2413 tbl
->current_rate
= rate
;
2414 rs_set_expected_tpt_table(lq_sta
, tbl
);
2415 rs_fill_link_cmd(NULL
, lq_sta
, rate
);
2416 iwl_send_lq_cmd(priv
, &lq_sta
->lq
, CMD_ASYNC
);
2421 static void rs_get_rate(void *priv_r
, struct ieee80211_sta
*sta
, void *priv_sta
,
2422 struct ieee80211_tx_rate_control
*txrc
)
2425 struct sk_buff
*skb
= txrc
->skb
;
2426 struct ieee80211_supported_band
*sband
= txrc
->sband
;
2427 struct iwl_priv
*priv
= (struct iwl_priv
*)priv_r
;
2428 struct ieee80211_conf
*conf
= &priv
->hw
->conf
;
2429 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*)skb
->data
;
2430 struct ieee80211_tx_info
*info
= IEEE80211_SKB_CB(skb
);
2431 struct iwl_lq_sta
*lq_sta
= priv_sta
;
2435 IWL_DEBUG_RATE_LIMIT(priv
, "rate scale calculate new rate for skb\n");
2437 /* Get max rate if user set max rate */
2439 lq_sta
->max_rate_idx
= txrc
->max_rate_idx
;
2440 if ((sband
->band
== IEEE80211_BAND_5GHZ
) &&
2441 (lq_sta
->max_rate_idx
!= -1))
2442 lq_sta
->max_rate_idx
+= IWL_FIRST_OFDM_RATE
;
2443 if ((lq_sta
->max_rate_idx
< 0) ||
2444 (lq_sta
->max_rate_idx
>= IWL_RATE_COUNT
))
2445 lq_sta
->max_rate_idx
= -1;
2449 mask_bit
= sta
->supp_rates
[sband
->band
];
2451 /* Send management frames and NO_ACK data using lowest rate. */
2452 if (!ieee80211_is_data(hdr
->frame_control
) ||
2453 info
->flags
& IEEE80211_TX_CTL_NO_ACK
|| !sta
|| !lq_sta
) {
2455 info
->control
.rates
[0].idx
=
2456 rate_lowest_index(sband
, NULL
);
2458 info
->control
.rates
[0].idx
=
2459 rate_lowest_index(sband
, sta
);
2460 if (info
->flags
& IEEE80211_TX_CTL_NO_ACK
)
2461 info
->control
.rates
[0].count
= 1;
2465 rate_idx
= lq_sta
->last_txrate_idx
;
2467 if ((priv
->iw_mode
== NL80211_IFTYPE_ADHOC
) &&
2468 !lq_sta
->ibss_sta_added
) {
2469 u8 sta_id
= priv
->cfg
->ops
->smgmt
->find_station(priv
,
2472 if (sta_id
== IWL_INVALID_STATION
) {
2473 IWL_DEBUG_RATE(priv
, "LQ: ADD station %pM\n",
2475 sta_id
= priv
->cfg
->ops
->smgmt
->add_station(priv
,
2479 if ((sta_id
!= IWL_INVALID_STATION
)) {
2480 lq_sta
->lq
.sta_id
= sta_id
;
2481 lq_sta
->lq
.rs_table
[0].rate_n_flags
= 0;
2482 lq_sta
->ibss_sta_added
= 1;
2483 rs_initialize_lq(priv
, conf
, sta
, lq_sta
);
2487 if (rate_idx
< 0 || rate_idx
> IWL_RATE_COUNT
)
2488 rate_idx
= rate_lowest_index(sband
, sta
);
2489 else if (sband
->band
== IEEE80211_BAND_5GHZ
)
2490 rate_idx
-= IWL_FIRST_OFDM_RATE
;
2492 info
->control
.rates
[0].idx
= rate_idx
;
2495 static void *rs_alloc_sta(void *priv_rate
, struct ieee80211_sta
*sta
,
2498 struct iwl_lq_sta
*lq_sta
;
2499 struct iwl_priv
*priv
;
2502 priv
= (struct iwl_priv
*)priv_rate
;
2503 IWL_DEBUG_RATE(priv
, "create station rate scale window\n");
2505 lq_sta
= kzalloc(sizeof(struct iwl_lq_sta
), gfp
);
2509 lq_sta
->lq
.sta_id
= 0xff;
2512 for (j
= 0; j
< LQ_SIZE
; j
++)
2513 for (i
= 0; i
< IWL_RATE_COUNT
; i
++)
2514 rs_rate_scale_clear_window(&lq_sta
->lq_info
[j
].win
[i
]);
2519 static void rs_rate_init(void *priv_r
, struct ieee80211_supported_band
*sband
,
2520 struct ieee80211_sta
*sta
, void *priv_sta
)
2523 struct iwl_priv
*priv
= (struct iwl_priv
*)priv_r
;
2524 struct ieee80211_conf
*conf
= &priv
->hw
->conf
;
2525 struct iwl_lq_sta
*lq_sta
= priv_sta
;
2530 lq_sta
->flush_timer
= 0;
2531 lq_sta
->supp_rates
= sta
->supp_rates
[sband
->band
];
2532 for (j
= 0; j
< LQ_SIZE
; j
++)
2533 for (i
= 0; i
< IWL_RATE_COUNT
; i
++)
2534 rs_rate_scale_clear_window(&lq_sta
->lq_info
[j
].win
[i
]);
2536 IWL_DEBUG_RATE(priv
, "LQ: *** rate scale station global init ***\n");
2537 /* TODO: what is a good starting rate for STA? About middle? Maybe not
2538 * the lowest or the highest rate.. Could consider using RSSI from
2539 * previous packets? Need to have IEEE 802.1X auth succeed immediately
2542 lq_sta
->ibss_sta_added
= 0;
2543 if (priv
->iw_mode
== NL80211_IFTYPE_AP
) {
2544 u8 sta_id
= priv
->cfg
->ops
->smgmt
->find_station(priv
,
2547 /* for IBSS the call are from tasklet */
2548 IWL_DEBUG_RATE(priv
, "LQ: ADD station %pM\n", sta
->addr
);
2550 if (sta_id
== IWL_INVALID_STATION
) {
2551 IWL_DEBUG_RATE(priv
, "LQ: ADD station %pM\n", sta
->addr
);
2552 sta_id
= priv
->cfg
->ops
->smgmt
->add_station(priv
,
2556 if ((sta_id
!= IWL_INVALID_STATION
)) {
2557 lq_sta
->lq
.sta_id
= sta_id
;
2558 lq_sta
->lq
.rs_table
[0].rate_n_flags
= 0;
2560 /* FIXME: this is w/a remove it later */
2561 priv
->assoc_station_added
= 1;
2565 lq_sta
->max_rate_idx
= -1;
2566 lq_sta
->missed_rate_counter
= IWL_MISSED_RATE_MAX
;
2567 lq_sta
->is_green
= rs_use_green(priv
, conf
);
2568 lq_sta
->active_legacy_rate
= priv
->active_rate
& ~(0x1000);
2569 lq_sta
->active_rate_basic
= priv
->active_rate_basic
;
2570 lq_sta
->band
= priv
->band
;
2572 * active_siso_rate mask includes 9 MBits (bit 5), and CCK (bits 0-3),
2573 * supp_rates[] does not; shift to convert format, force 9 MBits off.
2575 lq_sta
->active_siso_rate
= sta
->ht_cap
.mcs
.rx_mask
[0] << 1;
2576 lq_sta
->active_siso_rate
|= sta
->ht_cap
.mcs
.rx_mask
[0] & 0x1;
2577 lq_sta
->active_siso_rate
&= ~((u16
)0x2);
2578 lq_sta
->active_siso_rate
<<= IWL_FIRST_OFDM_RATE
;
2581 lq_sta
->active_mimo2_rate
= sta
->ht_cap
.mcs
.rx_mask
[1] << 1;
2582 lq_sta
->active_mimo2_rate
|= sta
->ht_cap
.mcs
.rx_mask
[1] & 0x1;
2583 lq_sta
->active_mimo2_rate
&= ~((u16
)0x2);
2584 lq_sta
->active_mimo2_rate
<<= IWL_FIRST_OFDM_RATE
;
2586 lq_sta
->active_mimo3_rate
= sta
->ht_cap
.mcs
.rx_mask
[2] << 1;
2587 lq_sta
->active_mimo3_rate
|= sta
->ht_cap
.mcs
.rx_mask
[2] & 0x1;
2588 lq_sta
->active_mimo3_rate
&= ~((u16
)0x2);
2589 lq_sta
->active_mimo3_rate
<<= IWL_FIRST_OFDM_RATE
;
2591 IWL_DEBUG_RATE(priv
, "SISO-RATE=%X MIMO2-RATE=%X MIMO3-RATE=%X\n",
2592 lq_sta
->active_siso_rate
,
2593 lq_sta
->active_mimo2_rate
,
2594 lq_sta
->active_mimo3_rate
);
2596 /* These values will be overridden later */
2597 lq_sta
->lq
.general_params
.single_stream_ant_msk
= ANT_A
;
2598 lq_sta
->lq
.general_params
.dual_stream_ant_msk
= ANT_AB
;
2600 /* as default allow aggregation for all tids */
2601 lq_sta
->tx_agg_tid_en
= IWL_AGG_ALL_TID
;
2604 /* Find highest tx rate supported by hardware and destination station */
2605 mask_bit
= sta
->supp_rates
[sband
->band
];
2606 count
= sband
->n_bitrates
;
2607 if (sband
->band
== IEEE80211_BAND_5GHZ
) {
2608 count
+= IWL_FIRST_OFDM_RATE
;
2609 start_rate
= IWL_FIRST_OFDM_RATE
;
2610 mask_bit
<<= IWL_FIRST_OFDM_RATE
;
2613 mask_bit
= mask_bit
& lq_sta
->active_legacy_rate
;
2614 lq_sta
->last_txrate_idx
= 4;
2615 for (i
= start_rate
; i
< count
; i
++)
2616 if (mask_bit
& BIT(i
))
2617 lq_sta
->last_txrate_idx
= i
;
2619 rs_initialize_lq(priv
, conf
, sta
, lq_sta
);
2622 static void rs_fill_link_cmd(const struct iwl_priv
*priv
,
2623 struct iwl_lq_sta
*lq_sta
, u32 new_rate
)
2625 struct iwl_scale_tbl_info tbl_type
;
2628 int repeat_rate
= 0;
2629 u8 ant_toggle_cnt
= 0;
2630 u8 use_ht_possible
= 1;
2631 u8 valid_tx_ant
= 0;
2632 struct iwl_link_quality_cmd
*lq_cmd
= &lq_sta
->lq
;
2634 /* Override starting rate (index 0) if needed for debug purposes */
2635 rs_dbgfs_set_mcs(lq_sta
, &new_rate
, index
);
2637 /* Interpret new_rate (rate_n_flags) */
2638 memset(&tbl_type
, 0, sizeof(tbl_type
));
2639 rs_get_tbl_info_from_mcs(new_rate
, lq_sta
->band
,
2640 &tbl_type
, &rate_idx
);
2642 /* How many times should we repeat the initial rate? */
2643 if (is_legacy(tbl_type
.lq_type
)) {
2645 repeat_rate
= IWL_NUMBER_TRY
;
2647 repeat_rate
= IWL_HT_NUMBER_TRY
;
2650 lq_cmd
->general_params
.mimo_delimiter
=
2651 is_mimo(tbl_type
.lq_type
) ? 1 : 0;
2653 /* Fill 1st table entry (index 0) */
2654 lq_cmd
->rs_table
[index
].rate_n_flags
= cpu_to_le32(new_rate
);
2656 if (num_of_ant(tbl_type
.ant_type
) == 1) {
2657 lq_cmd
->general_params
.single_stream_ant_msk
=
2659 } else if (num_of_ant(tbl_type
.ant_type
) == 2) {
2660 lq_cmd
->general_params
.dual_stream_ant_msk
=
2662 } /* otherwise we don't modify the existing value */
2668 valid_tx_ant
= priv
->hw_params
.valid_tx_ant
;
2670 /* Fill rest of rate table */
2671 while (index
< LINK_QUAL_MAX_RETRY_NUM
) {
2672 /* Repeat initial/next rate.
2673 * For legacy IWL_NUMBER_TRY == 1, this loop will not execute.
2674 * For HT IWL_HT_NUMBER_TRY == 3, this executes twice. */
2675 while (repeat_rate
> 0 && (index
< LINK_QUAL_MAX_RETRY_NUM
)) {
2676 if (is_legacy(tbl_type
.lq_type
)) {
2677 if (ant_toggle_cnt
< NUM_TRY_BEFORE_ANT_TOGGLE
)
2680 rs_toggle_antenna(valid_tx_ant
,
2681 &new_rate
, &tbl_type
))
2685 /* Override next rate if needed for debug purposes */
2686 rs_dbgfs_set_mcs(lq_sta
, &new_rate
, index
);
2688 /* Fill next table entry */
2689 lq_cmd
->rs_table
[index
].rate_n_flags
=
2690 cpu_to_le32(new_rate
);
2695 rs_get_tbl_info_from_mcs(new_rate
, lq_sta
->band
, &tbl_type
,
2698 /* Indicate to uCode which entries might be MIMO.
2699 * If initial rate was MIMO, this will finally end up
2700 * as (IWL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
2701 if (is_mimo(tbl_type
.lq_type
))
2702 lq_cmd
->general_params
.mimo_delimiter
= index
;
2705 new_rate
= rs_get_lower_rate(lq_sta
, &tbl_type
, rate_idx
,
2708 /* How many times should we repeat the next rate? */
2709 if (is_legacy(tbl_type
.lq_type
)) {
2710 if (ant_toggle_cnt
< NUM_TRY_BEFORE_ANT_TOGGLE
)
2713 rs_toggle_antenna(valid_tx_ant
,
2714 &new_rate
, &tbl_type
))
2717 repeat_rate
= IWL_NUMBER_TRY
;
2719 repeat_rate
= IWL_HT_NUMBER_TRY
;
2722 /* Don't allow HT rates after next pass.
2723 * rs_get_lower_rate() will change type to LQ_A or LQ_G. */
2724 use_ht_possible
= 0;
2726 /* Override next rate if needed for debug purposes */
2727 rs_dbgfs_set_mcs(lq_sta
, &new_rate
, index
);
2729 /* Fill next table entry */
2730 lq_cmd
->rs_table
[index
].rate_n_flags
= cpu_to_le32(new_rate
);
2736 lq_cmd
->agg_params
.agg_frame_cnt_limit
= 64;
2737 lq_cmd
->agg_params
.agg_dis_start_th
= 3;
2738 lq_cmd
->agg_params
.agg_time_limit
= cpu_to_le16(4000);
2741 static void *rs_alloc(struct ieee80211_hw
*hw
, struct dentry
*debugfsdir
)
2745 /* rate scale requires free function to be implemented */
2746 static void rs_free(void *priv_rate
)
2751 static void rs_free_sta(void *priv_r
, struct ieee80211_sta
*sta
,
2754 struct iwl_lq_sta
*lq_sta
= priv_sta
;
2755 struct iwl_priv
*priv __maybe_unused
= priv_r
;
2757 IWL_DEBUG_RATE(priv
, "enter\n");
2759 IWL_DEBUG_RATE(priv
, "leave\n");
2763 #ifdef CONFIG_MAC80211_DEBUGFS
2764 static int open_file_generic(struct inode
*inode
, struct file
*file
)
2766 file
->private_data
= inode
->i_private
;
2769 static void rs_dbgfs_set_mcs(struct iwl_lq_sta
*lq_sta
,
2770 u32
*rate_n_flags
, int index
)
2772 struct iwl_priv
*priv
;
2777 valid_tx_ant
= priv
->hw_params
.valid_tx_ant
;
2778 if (lq_sta
->dbg_fixed_rate
) {
2780 ((lq_sta
->dbg_fixed_rate
& RATE_MCS_ANT_ABC_MSK
)
2781 >> RATE_MCS_ANT_POS
);
2782 if ((valid_tx_ant
& ant_sel_tx
) == ant_sel_tx
) {
2783 *rate_n_flags
= lq_sta
->dbg_fixed_rate
;
2784 IWL_DEBUG_RATE(priv
, "Fixed rate ON\n");
2786 lq_sta
->dbg_fixed_rate
= 0;
2788 "Invalid antenna selection 0x%X, Valid is 0x%X\n",
2789 ant_sel_tx
, valid_tx_ant
);
2790 IWL_DEBUG_RATE(priv
, "Fixed rate OFF\n");
2793 IWL_DEBUG_RATE(priv
, "Fixed rate OFF\n");
2797 static ssize_t
rs_sta_dbgfs_scale_table_write(struct file
*file
,
2798 const char __user
*user_buf
, size_t count
, loff_t
*ppos
)
2800 struct iwl_lq_sta
*lq_sta
= file
->private_data
;
2801 struct iwl_priv
*priv
;
2807 memset(buf
, 0, sizeof(buf
));
2808 buf_size
= min(count
, sizeof(buf
) - 1);
2809 if (copy_from_user(buf
, user_buf
, buf_size
))
2812 if (sscanf(buf
, "%x", &parsed_rate
) == 1)
2813 lq_sta
->dbg_fixed_rate
= parsed_rate
;
2815 lq_sta
->dbg_fixed_rate
= 0;
2817 lq_sta
->active_legacy_rate
= 0x0FFF; /* 1 - 54 MBits, includes CCK */
2818 lq_sta
->active_siso_rate
= 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2819 lq_sta
->active_mimo2_rate
= 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2820 lq_sta
->active_mimo3_rate
= 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2822 IWL_DEBUG_RATE(priv
, "sta_id %d rate 0x%X\n",
2823 lq_sta
->lq
.sta_id
, lq_sta
->dbg_fixed_rate
);
2825 if (lq_sta
->dbg_fixed_rate
) {
2826 rs_fill_link_cmd(NULL
, lq_sta
, lq_sta
->dbg_fixed_rate
);
2827 iwl_send_lq_cmd(lq_sta
->drv
, &lq_sta
->lq
, CMD_ASYNC
);
2833 static ssize_t
rs_sta_dbgfs_scale_table_read(struct file
*file
,
2834 char __user
*user_buf
, size_t count
, loff_t
*ppos
)
2842 struct iwl_lq_sta
*lq_sta
= file
->private_data
;
2843 struct iwl_priv
*priv
;
2844 struct iwl_scale_tbl_info
*tbl
= &(lq_sta
->lq_info
[lq_sta
->active_tbl
]);
2847 buff
= kmalloc(1024, GFP_KERNEL
);
2851 desc
+= sprintf(buff
+desc
, "sta_id %d\n", lq_sta
->lq
.sta_id
);
2852 desc
+= sprintf(buff
+desc
, "failed=%d success=%d rate=0%X\n",
2853 lq_sta
->total_failed
, lq_sta
->total_success
,
2854 lq_sta
->active_legacy_rate
);
2855 desc
+= sprintf(buff
+desc
, "fixed rate 0x%X\n",
2856 lq_sta
->dbg_fixed_rate
);
2857 desc
+= sprintf(buff
+desc
, "valid_tx_ant %s%s%s\n",
2858 (priv
->hw_params
.valid_tx_ant
& ANT_A
) ? "ANT_A," : "",
2859 (priv
->hw_params
.valid_tx_ant
& ANT_B
) ? "ANT_B," : "",
2860 (priv
->hw_params
.valid_tx_ant
& ANT_C
) ? "ANT_C" : "");
2861 desc
+= sprintf(buff
+desc
, "lq type %s\n",
2862 (is_legacy(tbl
->lq_type
)) ? "legacy" : "HT");
2863 if (is_Ht(tbl
->lq_type
)) {
2864 desc
+= sprintf(buff
+desc
, " %s",
2865 (is_siso(tbl
->lq_type
)) ? "SISO" :
2866 ((is_mimo2(tbl
->lq_type
)) ? "MIMO2" : "MIMO3"));
2867 desc
+= sprintf(buff
+desc
, " %s",
2868 (tbl
->is_fat
) ? "40MHz" : "20MHz");
2869 desc
+= sprintf(buff
+desc
, " %s\n", (tbl
->is_SGI
) ? "SGI" : "");
2871 desc
+= sprintf(buff
+desc
, "last tx rate=0x%X\n",
2872 lq_sta
->last_rate_n_flags
);
2873 desc
+= sprintf(buff
+desc
, "general:"
2874 "flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
2875 lq_sta
->lq
.general_params
.flags
,
2876 lq_sta
->lq
.general_params
.mimo_delimiter
,
2877 lq_sta
->lq
.general_params
.single_stream_ant_msk
,
2878 lq_sta
->lq
.general_params
.dual_stream_ant_msk
);
2880 desc
+= sprintf(buff
+desc
, "agg:"
2881 "time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
2882 le16_to_cpu(lq_sta
->lq
.agg_params
.agg_time_limit
),
2883 lq_sta
->lq
.agg_params
.agg_dis_start_th
,
2884 lq_sta
->lq
.agg_params
.agg_frame_cnt_limit
);
2886 desc
+= sprintf(buff
+desc
,
2887 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
2888 lq_sta
->lq
.general_params
.start_rate_index
[0],
2889 lq_sta
->lq
.general_params
.start_rate_index
[1],
2890 lq_sta
->lq
.general_params
.start_rate_index
[2],
2891 lq_sta
->lq
.general_params
.start_rate_index
[3]);
2893 for (i
= 0; i
< LINK_QUAL_MAX_RETRY_NUM
; i
++) {
2894 index
= iwl_hwrate_to_plcp_idx(
2895 le32_to_cpu(lq_sta
->lq
.rs_table
[i
].rate_n_flags
));
2896 if (is_legacy(tbl
->lq_type
)) {
2897 desc
+= sprintf(buff
+desc
, " rate[%d] 0x%X %smbps\n",
2898 i
, le32_to_cpu(lq_sta
->lq
.rs_table
[i
].rate_n_flags
),
2899 iwl_rate_mcs
[index
].mbps
);
2901 desc
+= sprintf(buff
+desc
, " rate[%d] 0x%X %smbps (%s)\n",
2902 i
, le32_to_cpu(lq_sta
->lq
.rs_table
[i
].rate_n_flags
),
2903 iwl_rate_mcs
[index
].mbps
, iwl_rate_mcs
[index
].mcs
);
2907 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buff
, desc
);
2912 static const struct file_operations rs_sta_dbgfs_scale_table_ops
= {
2913 .write
= rs_sta_dbgfs_scale_table_write
,
2914 .read
= rs_sta_dbgfs_scale_table_read
,
2915 .open
= open_file_generic
,
2917 static ssize_t
rs_sta_dbgfs_stats_table_read(struct file
*file
,
2918 char __user
*user_buf
, size_t count
, loff_t
*ppos
)
2925 struct iwl_lq_sta
*lq_sta
= file
->private_data
;
2927 buff
= kmalloc(1024, GFP_KERNEL
);
2931 for (i
= 0; i
< LQ_SIZE
; i
++) {
2932 desc
+= sprintf(buff
+desc
, "%s type=%d SGI=%d FAT=%d DUP=%d\n"
2934 lq_sta
->active_tbl
== i
? "*" : "x",
2935 lq_sta
->lq_info
[i
].lq_type
,
2936 lq_sta
->lq_info
[i
].is_SGI
,
2937 lq_sta
->lq_info
[i
].is_fat
,
2938 lq_sta
->lq_info
[i
].is_dup
,
2939 lq_sta
->lq_info
[i
].current_rate
);
2940 for (j
= 0; j
< IWL_RATE_COUNT
; j
++) {
2941 desc
+= sprintf(buff
+desc
,
2942 "counter=%d success=%d %%=%d\n",
2943 lq_sta
->lq_info
[i
].win
[j
].counter
,
2944 lq_sta
->lq_info
[i
].win
[j
].success_counter
,
2945 lq_sta
->lq_info
[i
].win
[j
].success_ratio
);
2948 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buff
, desc
);
2953 static const struct file_operations rs_sta_dbgfs_stats_table_ops
= {
2954 .read
= rs_sta_dbgfs_stats_table_read
,
2955 .open
= open_file_generic
,
2958 static void rs_add_debugfs(void *priv
, void *priv_sta
,
2961 struct iwl_lq_sta
*lq_sta
= priv_sta
;
2962 lq_sta
->rs_sta_dbgfs_scale_table_file
=
2963 debugfs_create_file("rate_scale_table", 0600, dir
,
2964 lq_sta
, &rs_sta_dbgfs_scale_table_ops
);
2965 lq_sta
->rs_sta_dbgfs_stats_table_file
=
2966 debugfs_create_file("rate_stats_table", 0600, dir
,
2967 lq_sta
, &rs_sta_dbgfs_stats_table_ops
);
2968 lq_sta
->rs_sta_dbgfs_tx_agg_tid_en_file
=
2969 debugfs_create_u8("tx_agg_tid_enable", 0600, dir
,
2970 &lq_sta
->tx_agg_tid_en
);
2974 static void rs_remove_debugfs(void *priv
, void *priv_sta
)
2976 struct iwl_lq_sta
*lq_sta
= priv_sta
;
2977 debugfs_remove(lq_sta
->rs_sta_dbgfs_scale_table_file
);
2978 debugfs_remove(lq_sta
->rs_sta_dbgfs_stats_table_file
);
2979 debugfs_remove(lq_sta
->rs_sta_dbgfs_tx_agg_tid_en_file
);
2983 static struct rate_control_ops rs_ops
= {
2986 .tx_status
= rs_tx_status
,
2987 .get_rate
= rs_get_rate
,
2988 .rate_init
= rs_rate_init
,
2991 .alloc_sta
= rs_alloc_sta
,
2992 .free_sta
= rs_free_sta
,
2993 #ifdef CONFIG_MAC80211_DEBUGFS
2994 .add_sta_debugfs
= rs_add_debugfs
,
2995 .remove_sta_debugfs
= rs_remove_debugfs
,
2999 int iwlagn_rate_control_register(void)
3001 return ieee80211_rate_control_register(&rs_ops
);
3004 void iwlagn_rate_control_unregister(void)
3006 ieee80211_rate_control_unregister(&rs_ops
);