1 /******************************************************************************
3 * Copyright(c) 2005 - 2014 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/skbuff.h>
28 #include <linux/slab.h>
29 #include <net/mac80211.h>
31 #include <linux/netdevice.h>
32 #include <linux/etherdevice.h>
33 #include <linux/delay.h>
35 #include <linux/workqueue.h>
40 #define RS_NAME "iwl-agn-rs"
42 #define NUM_TRY_BEFORE_ANT_TOGGLE 1
43 #define IWL_NUMBER_TRY 1
44 #define IWL_HT_NUMBER_TRY 3
46 #define IWL_RATE_MAX_WINDOW 62 /* # tx in history window */
47 #define IWL_RATE_MIN_FAILURE_TH 6 /* min failures to calc tpt */
48 #define IWL_RATE_MIN_SUCCESS_TH 8 /* min successes to calc tpt */
50 /* max allowed rate miss before sync LQ cmd */
51 #define IWL_MISSED_RATE_MAX 15
52 /* max time to accum history 2 seconds */
53 #define IWL_RATE_SCALE_FLUSH_INTVL (3*HZ)
55 static u8 rs_ht_to_legacy
[] = {
56 IWL_RATE_6M_INDEX
, IWL_RATE_6M_INDEX
,
57 IWL_RATE_6M_INDEX
, IWL_RATE_6M_INDEX
,
59 IWL_RATE_6M_INDEX
, IWL_RATE_9M_INDEX
,
60 IWL_RATE_12M_INDEX
, IWL_RATE_18M_INDEX
,
61 IWL_RATE_24M_INDEX
, IWL_RATE_36M_INDEX
,
62 IWL_RATE_48M_INDEX
, IWL_RATE_54M_INDEX
65 static const u8 ant_toggle_lookup
[] = {
66 /*ANT_NONE -> */ ANT_NONE
,
69 /*ANT_AB -> */ ANT_BC
,
71 /*ANT_AC -> */ ANT_AB
,
72 /*ANT_BC -> */ ANT_AC
,
73 /*ANT_ABC -> */ ANT_ABC
,
76 #define IWL_DECLARE_RATE_INFO(r, s, ip, in, rp, rn, pp, np) \
77 [IWL_RATE_##r##M_INDEX] = { IWL_RATE_##r##M_PLCP, \
78 IWL_RATE_SISO_##s##M_PLCP, \
79 IWL_RATE_MIMO2_##s##M_PLCP,\
80 IWL_RATE_MIMO3_##s##M_PLCP,\
81 IWL_RATE_##r##M_IEEE, \
82 IWL_RATE_##ip##M_INDEX, \
83 IWL_RATE_##in##M_INDEX, \
84 IWL_RATE_##rp##M_INDEX, \
85 IWL_RATE_##rn##M_INDEX, \
86 IWL_RATE_##pp##M_INDEX, \
87 IWL_RATE_##np##M_INDEX }
91 * rate, ht rate, prev rate, next rate, prev tgg rate, next tgg rate
93 * If there isn't a valid next or previous rate then INV is used which
94 * maps to IWL_RATE_INVALID
97 const struct iwl_rate_info iwl_rates
[IWL_RATE_COUNT
] = {
98 IWL_DECLARE_RATE_INFO(1, INV
, INV
, 2, INV
, 2, INV
, 2), /* 1mbps */
99 IWL_DECLARE_RATE_INFO(2, INV
, 1, 5, 1, 5, 1, 5), /* 2mbps */
100 IWL_DECLARE_RATE_INFO(5, INV
, 2, 6, 2, 11, 2, 11), /*5.5mbps */
101 IWL_DECLARE_RATE_INFO(11, INV
, 9, 12, 9, 12, 5, 18), /* 11mbps */
102 IWL_DECLARE_RATE_INFO(6, 6, 5, 9, 5, 11, 5, 11), /* 6mbps */
103 IWL_DECLARE_RATE_INFO(9, 6, 6, 11, 6, 11, 5, 11), /* 9mbps */
104 IWL_DECLARE_RATE_INFO(12, 12, 11, 18, 11, 18, 11, 18), /* 12mbps */
105 IWL_DECLARE_RATE_INFO(18, 18, 12, 24, 12, 24, 11, 24), /* 18mbps */
106 IWL_DECLARE_RATE_INFO(24, 24, 18, 36, 18, 36, 18, 36), /* 24mbps */
107 IWL_DECLARE_RATE_INFO(36, 36, 24, 48, 24, 48, 24, 48), /* 36mbps */
108 IWL_DECLARE_RATE_INFO(48, 48, 36, 54, 36, 54, 36, 54), /* 48mbps */
109 IWL_DECLARE_RATE_INFO(54, 54, 48, INV
, 48, INV
, 48, INV
),/* 54mbps */
110 IWL_DECLARE_RATE_INFO(60, 60, 48, INV
, 48, INV
, 48, INV
),/* 60mbps */
111 /* FIXME:RS: ^^ should be INV (legacy) */
114 static inline u8
rs_extract_rate(u32 rate_n_flags
)
116 return (u8
)(rate_n_flags
& RATE_MCS_RATE_MSK
);
119 static int iwl_hwrate_to_plcp_idx(u32 rate_n_flags
)
124 if (rate_n_flags
& RATE_MCS_HT_MSK
) {
125 idx
= rs_extract_rate(rate_n_flags
);
127 if (idx
>= IWL_RATE_MIMO3_6M_PLCP
)
128 idx
= idx
- IWL_RATE_MIMO3_6M_PLCP
;
129 else if (idx
>= IWL_RATE_MIMO2_6M_PLCP
)
130 idx
= idx
- IWL_RATE_MIMO2_6M_PLCP
;
132 idx
+= IWL_FIRST_OFDM_RATE
;
133 /* skip 9M not supported in ht*/
134 if (idx
>= IWL_RATE_9M_INDEX
)
136 if ((idx
>= IWL_FIRST_OFDM_RATE
) && (idx
<= IWL_LAST_OFDM_RATE
))
139 /* legacy rate format, search for match in table */
141 for (idx
= 0; idx
< ARRAY_SIZE(iwl_rates
); idx
++)
142 if (iwl_rates
[idx
].plcp
==
143 rs_extract_rate(rate_n_flags
))
150 static void rs_rate_scale_perform(struct iwl_priv
*priv
,
152 struct ieee80211_sta
*sta
,
153 struct iwl_lq_sta
*lq_sta
);
154 static void rs_fill_link_cmd(struct iwl_priv
*priv
,
155 struct iwl_lq_sta
*lq_sta
, u32 rate_n_flags
);
156 static void rs_stay_in_table(struct iwl_lq_sta
*lq_sta
, bool force_search
);
159 #ifdef CONFIG_MAC80211_DEBUGFS
160 static void rs_dbgfs_set_mcs(struct iwl_lq_sta
*lq_sta
,
161 u32
*rate_n_flags
, int index
);
163 static void rs_dbgfs_set_mcs(struct iwl_lq_sta
*lq_sta
,
164 u32
*rate_n_flags
, int index
)
169 * The following tables contain the expected throughput metrics for all rates
171 * 1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
173 * where invalid entries are zeros.
175 * CCK rates are only valid in legacy table and will only be used in G
179 static const u16 expected_tpt_legacy
[IWL_RATE_COUNT
] = {
180 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 0
183 static const u16 expected_tpt_siso20MHz
[4][IWL_RATE_COUNT
] = {
184 {0, 0, 0, 0, 42, 0, 76, 102, 124, 159, 183, 193, 202}, /* Norm */
185 {0, 0, 0, 0, 46, 0, 82, 110, 132, 168, 192, 202, 210}, /* SGI */
186 {0, 0, 0, 0, 47, 0, 91, 133, 171, 242, 305, 334, 362}, /* AGG */
187 {0, 0, 0, 0, 52, 0, 101, 145, 187, 264, 330, 361, 390}, /* AGG+SGI */
190 static const u16 expected_tpt_siso40MHz
[4][IWL_RATE_COUNT
] = {
191 {0, 0, 0, 0, 77, 0, 127, 160, 184, 220, 242, 250, 257}, /* Norm */
192 {0, 0, 0, 0, 83, 0, 135, 169, 193, 229, 250, 257, 264}, /* SGI */
193 {0, 0, 0, 0, 94, 0, 177, 249, 313, 423, 512, 550, 586}, /* AGG */
194 {0, 0, 0, 0, 104, 0, 193, 270, 338, 454, 545, 584, 620}, /* AGG+SGI */
197 static const u16 expected_tpt_mimo2_20MHz
[4][IWL_RATE_COUNT
] = {
198 {0, 0, 0, 0, 74, 0, 123, 155, 179, 214, 236, 244, 251}, /* Norm */
199 {0, 0, 0, 0, 81, 0, 131, 164, 188, 223, 243, 251, 257}, /* SGI */
200 {0, 0, 0, 0, 89, 0, 167, 235, 296, 402, 488, 526, 560}, /* AGG */
201 {0, 0, 0, 0, 97, 0, 182, 255, 320, 431, 520, 558, 593}, /* AGG+SGI*/
204 static const u16 expected_tpt_mimo2_40MHz
[4][IWL_RATE_COUNT
] = {
205 {0, 0, 0, 0, 123, 0, 182, 214, 235, 264, 279, 285, 289}, /* Norm */
206 {0, 0, 0, 0, 131, 0, 191, 222, 242, 270, 284, 289, 293}, /* SGI */
207 {0, 0, 0, 0, 171, 0, 305, 410, 496, 634, 731, 771, 805}, /* AGG */
208 {0, 0, 0, 0, 186, 0, 329, 439, 527, 667, 764, 803, 838}, /* AGG+SGI */
211 static const u16 expected_tpt_mimo3_20MHz
[4][IWL_RATE_COUNT
] = {
212 {0, 0, 0, 0, 99, 0, 153, 186, 208, 239, 256, 263, 268}, /* Norm */
213 {0, 0, 0, 0, 106, 0, 162, 194, 215, 246, 262, 268, 273}, /* SGI */
214 {0, 0, 0, 0, 134, 0, 249, 346, 431, 574, 685, 732, 775}, /* AGG */
215 {0, 0, 0, 0, 148, 0, 272, 376, 465, 614, 727, 775, 818}, /* AGG+SGI */
218 static const u16 expected_tpt_mimo3_40MHz
[4][IWL_RATE_COUNT
] = {
219 {0, 0, 0, 0, 152, 0, 211, 239, 255, 279, 290, 294, 297}, /* Norm */
220 {0, 0, 0, 0, 160, 0, 219, 245, 261, 284, 294, 297, 300}, /* SGI */
221 {0, 0, 0, 0, 254, 0, 443, 584, 695, 868, 984, 1030, 1070}, /* AGG */
222 {0, 0, 0, 0, 277, 0, 478, 624, 737, 911, 1026, 1070, 1109}, /* AGG+SGI */
226 static const struct iwl_rate_mcs_info iwl_rate_mcs
[IWL_RATE_COUNT
] = {
235 { "24", "16QAM 1/2"},
236 { "36", "16QAM 3/4"},
237 { "48", "64QAM 2/3"},
238 { "54", "64QAM 3/4"},
239 { "60", "64QAM 5/6"},
242 #define MCS_INDEX_PER_STREAM (8)
244 static void rs_rate_scale_clear_window(struct iwl_rate_scale_data
*window
)
247 window
->success_counter
= 0;
248 window
->success_ratio
= IWL_INVALID_VALUE
;
250 window
->average_tpt
= IWL_INVALID_VALUE
;
254 static inline u8
rs_is_valid_ant(u8 valid_antenna
, u8 ant_type
)
256 return (ant_type
& valid_antenna
) == ant_type
;
260 * removes the old data from the statistics. All data that is older than
261 * TID_MAX_TIME_DIFF, will be deleted.
263 static void rs_tl_rm_old_stats(struct iwl_traffic_load
*tl
, u32 curr_time
)
265 /* The oldest age we want to keep */
266 u32 oldest_time
= curr_time
- TID_MAX_TIME_DIFF
;
268 while (tl
->queue_count
&&
269 (tl
->time_stamp
< oldest_time
)) {
270 tl
->total
-= tl
->packet_count
[tl
->head
];
271 tl
->packet_count
[tl
->head
] = 0;
272 tl
->time_stamp
+= TID_QUEUE_CELL_SPACING
;
275 if (tl
->head
>= TID_QUEUE_MAX_SIZE
)
281 * increment traffic load value for tid and also remove
282 * any old values if passed the certain time period
284 static u8
rs_tl_add_packet(struct iwl_lq_sta
*lq_data
,
285 struct ieee80211_hdr
*hdr
)
287 u32 curr_time
= jiffies_to_msecs(jiffies
);
290 struct iwl_traffic_load
*tl
= NULL
;
293 if (ieee80211_is_data_qos(hdr
->frame_control
)) {
294 u8
*qc
= ieee80211_get_qos_ctl(hdr
);
297 return IWL_MAX_TID_COUNT
;
299 if (unlikely(tid
>= IWL_MAX_TID_COUNT
))
300 return IWL_MAX_TID_COUNT
;
302 tl
= &lq_data
->load
[tid
];
304 curr_time
-= curr_time
% TID_ROUND_VALUE
;
306 /* Happens only for the first packet. Initialize the data */
307 if (!(tl
->queue_count
)) {
309 tl
->time_stamp
= curr_time
;
312 tl
->packet_count
[0] = 1;
313 return IWL_MAX_TID_COUNT
;
316 time_diff
= TIME_WRAP_AROUND(tl
->time_stamp
, curr_time
);
317 index
= time_diff
/ TID_QUEUE_CELL_SPACING
;
319 /* The history is too long: remove data that is older than */
320 /* TID_MAX_TIME_DIFF */
321 if (index
>= TID_QUEUE_MAX_SIZE
)
322 rs_tl_rm_old_stats(tl
, curr_time
);
324 index
= (tl
->head
+ index
) % TID_QUEUE_MAX_SIZE
;
325 tl
->packet_count
[index
] = tl
->packet_count
[index
] + 1;
326 tl
->total
= tl
->total
+ 1;
328 if ((index
+ 1) > tl
->queue_count
)
329 tl
->queue_count
= index
+ 1;
334 #ifdef CONFIG_MAC80211_DEBUGFS
336 * Program the device to use fixed rate for frame transmit
337 * This is for debugging/testing only
338 * once the device start use fixed rate, we need to reload the module
339 * to being back the normal operation.
341 static void rs_program_fix_rate(struct iwl_priv
*priv
,
342 struct iwl_lq_sta
*lq_sta
)
344 struct iwl_station_priv
*sta_priv
=
345 container_of(lq_sta
, struct iwl_station_priv
, lq_sta
);
346 struct iwl_rxon_context
*ctx
= sta_priv
->ctx
;
348 lq_sta
->active_legacy_rate
= 0x0FFF; /* 1 - 54 MBits, includes CCK */
349 lq_sta
->active_siso_rate
= 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
350 lq_sta
->active_mimo2_rate
= 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
351 lq_sta
->active_mimo3_rate
= 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
353 IWL_DEBUG_RATE(priv
, "sta_id %d rate 0x%X\n",
354 lq_sta
->lq
.sta_id
, lq_sta
->dbg_fixed_rate
);
356 if (lq_sta
->dbg_fixed_rate
) {
357 rs_fill_link_cmd(NULL
, lq_sta
, lq_sta
->dbg_fixed_rate
);
358 iwl_send_lq_cmd(lq_sta
->drv
, ctx
, &lq_sta
->lq
, CMD_ASYNC
,
365 get the traffic load value for tid
367 static u32
rs_tl_get_load(struct iwl_lq_sta
*lq_data
, u8 tid
)
369 u32 curr_time
= jiffies_to_msecs(jiffies
);
372 struct iwl_traffic_load
*tl
= NULL
;
374 if (tid
>= IWL_MAX_TID_COUNT
)
377 tl
= &(lq_data
->load
[tid
]);
379 curr_time
-= curr_time
% TID_ROUND_VALUE
;
381 if (!(tl
->queue_count
))
384 time_diff
= TIME_WRAP_AROUND(tl
->time_stamp
, curr_time
);
385 index
= time_diff
/ TID_QUEUE_CELL_SPACING
;
387 /* The history is too long: remove data that is older than */
388 /* TID_MAX_TIME_DIFF */
389 if (index
>= TID_QUEUE_MAX_SIZE
)
390 rs_tl_rm_old_stats(tl
, curr_time
);
395 static int rs_tl_turn_on_agg_for_tid(struct iwl_priv
*priv
,
396 struct iwl_lq_sta
*lq_data
, u8 tid
,
397 struct ieee80211_sta
*sta
)
403 * Don't create TX aggregation sessions when in high
404 * BT traffic, as they would just be disrupted by BT.
406 if (priv
->bt_traffic_load
>= IWL_BT_COEX_TRAFFIC_LOAD_HIGH
) {
408 "BT traffic (%d), no aggregation allowed\n",
409 priv
->bt_traffic_load
);
413 load
= rs_tl_get_load(lq_data
, tid
);
415 IWL_DEBUG_HT(priv
, "Starting Tx agg: STA: %pM tid: %d\n",
417 ret
= ieee80211_start_tx_ba_session(sta
, tid
, 5000);
418 if (ret
== -EAGAIN
) {
420 * driver and mac80211 is out of sync
421 * this might be cause by reloading firmware
422 * stop the tx ba session here
424 IWL_ERR(priv
, "Fail start Tx agg on tid: %d\n",
426 ieee80211_stop_tx_ba_session(sta
, tid
);
431 static void rs_tl_turn_on_agg(struct iwl_priv
*priv
, u8 tid
,
432 struct iwl_lq_sta
*lq_data
,
433 struct ieee80211_sta
*sta
)
435 if (tid
< IWL_MAX_TID_COUNT
)
436 rs_tl_turn_on_agg_for_tid(priv
, lq_data
, tid
, sta
);
438 IWL_ERR(priv
, "tid exceeds max TID count: %d/%d\n",
439 tid
, IWL_MAX_TID_COUNT
);
442 static inline int get_num_of_ant_from_rate(u32 rate_n_flags
)
444 return !!(rate_n_flags
& RATE_MCS_ANT_A_MSK
) +
445 !!(rate_n_flags
& RATE_MCS_ANT_B_MSK
) +
446 !!(rate_n_flags
& RATE_MCS_ANT_C_MSK
);
450 * Static function to get the expected throughput from an iwl_scale_tbl_info
451 * that wraps a NULL pointer check
453 static s32
get_expected_tpt(struct iwl_scale_tbl_info
*tbl
, int rs_index
)
455 if (tbl
->expected_tpt
)
456 return tbl
->expected_tpt
[rs_index
];
461 * rs_collect_tx_data - Update the success/failure sliding window
463 * We keep a sliding window of the last 62 packets transmitted
464 * at this rate. window->data contains the bitmask of successful
467 static int rs_collect_tx_data(struct iwl_scale_tbl_info
*tbl
,
468 int scale_index
, int attempts
, int successes
)
470 struct iwl_rate_scale_data
*window
= NULL
;
471 static const u64 mask
= (((u64
)1) << (IWL_RATE_MAX_WINDOW
- 1));
474 if (scale_index
< 0 || scale_index
>= IWL_RATE_COUNT
)
477 /* Select window for current tx bit rate */
478 window
= &(tbl
->win
[scale_index
]);
480 /* Get expected throughput */
481 tpt
= get_expected_tpt(tbl
, scale_index
);
484 * Keep track of only the latest 62 tx frame attempts in this rate's
485 * history window; anything older isn't really relevant any more.
486 * If we have filled up the sliding window, drop the oldest attempt;
487 * if the oldest attempt (highest bit in bitmap) shows "success",
488 * subtract "1" from the success counter (this is the main reason
489 * we keep these bitmaps!).
491 while (attempts
> 0) {
492 if (window
->counter
>= IWL_RATE_MAX_WINDOW
) {
494 /* remove earliest */
495 window
->counter
= IWL_RATE_MAX_WINDOW
- 1;
497 if (window
->data
& mask
) {
498 window
->data
&= ~mask
;
499 window
->success_counter
--;
503 /* Increment frames-attempted counter */
506 /* Shift bitmap by one frame to throw away oldest history */
509 /* Mark the most recent #successes attempts as successful */
511 window
->success_counter
++;
519 /* Calculate current success ratio, avoid divide-by-0! */
520 if (window
->counter
> 0)
521 window
->success_ratio
= 128 * (100 * window
->success_counter
)
524 window
->success_ratio
= IWL_INVALID_VALUE
;
526 fail_count
= window
->counter
- window
->success_counter
;
528 /* Calculate average throughput, if we have enough history. */
529 if ((fail_count
>= IWL_RATE_MIN_FAILURE_TH
) ||
530 (window
->success_counter
>= IWL_RATE_MIN_SUCCESS_TH
))
531 window
->average_tpt
= (window
->success_ratio
* tpt
+ 64) / 128;
533 window
->average_tpt
= IWL_INVALID_VALUE
;
535 /* Tag this window as having been updated */
536 window
->stamp
= jiffies
;
542 * Fill uCode API rate_n_flags field, based on "search" or "active" table.
544 /* FIXME:RS:remove this function and put the flags statically in the table */
545 static u32
rate_n_flags_from_tbl(struct iwl_priv
*priv
,
546 struct iwl_scale_tbl_info
*tbl
,
547 int index
, u8 use_green
)
549 u32 rate_n_flags
= 0;
551 if (is_legacy(tbl
->lq_type
)) {
552 rate_n_flags
= iwl_rates
[index
].plcp
;
553 if (index
>= IWL_FIRST_CCK_RATE
&& index
<= IWL_LAST_CCK_RATE
)
554 rate_n_flags
|= RATE_MCS_CCK_MSK
;
556 } else if (is_Ht(tbl
->lq_type
)) {
557 if (index
> IWL_LAST_OFDM_RATE
) {
558 IWL_ERR(priv
, "Invalid HT rate index %d\n", index
);
559 index
= IWL_LAST_OFDM_RATE
;
561 rate_n_flags
= RATE_MCS_HT_MSK
;
563 if (is_siso(tbl
->lq_type
))
564 rate_n_flags
|= iwl_rates
[index
].plcp_siso
;
565 else if (is_mimo2(tbl
->lq_type
))
566 rate_n_flags
|= iwl_rates
[index
].plcp_mimo2
;
568 rate_n_flags
|= iwl_rates
[index
].plcp_mimo3
;
570 IWL_ERR(priv
, "Invalid tbl->lq_type %d\n", tbl
->lq_type
);
573 rate_n_flags
|= ((tbl
->ant_type
<< RATE_MCS_ANT_POS
) &
574 RATE_MCS_ANT_ABC_MSK
);
576 if (is_Ht(tbl
->lq_type
)) {
579 rate_n_flags
|= RATE_MCS_DUP_MSK
;
581 rate_n_flags
|= RATE_MCS_HT40_MSK
;
584 rate_n_flags
|= RATE_MCS_SGI_MSK
;
587 rate_n_flags
|= RATE_MCS_GF_MSK
;
588 if (is_siso(tbl
->lq_type
) && tbl
->is_SGI
) {
589 rate_n_flags
&= ~RATE_MCS_SGI_MSK
;
590 IWL_ERR(priv
, "GF was set with SGI:SISO\n");
598 * Interpret uCode API's rate_n_flags format,
599 * fill "search" or "active" tx mode table.
601 static int rs_get_tbl_info_from_mcs(const u32 rate_n_flags
,
602 enum ieee80211_band band
,
603 struct iwl_scale_tbl_info
*tbl
,
606 u32 ant_msk
= (rate_n_flags
& RATE_MCS_ANT_ABC_MSK
);
607 u8 num_of_ant
= get_num_of_ant_from_rate(rate_n_flags
);
610 memset(tbl
, 0, sizeof(struct iwl_scale_tbl_info
));
611 *rate_idx
= iwl_hwrate_to_plcp_idx(rate_n_flags
);
613 if (*rate_idx
== IWL_RATE_INVALID
) {
617 tbl
->is_SGI
= 0; /* default legacy setup */
620 tbl
->ant_type
= (ant_msk
>> RATE_MCS_ANT_POS
);
621 tbl
->lq_type
= LQ_NONE
;
622 tbl
->max_search
= IWL_MAX_SEARCH
;
624 /* legacy rate format */
625 if (!(rate_n_flags
& RATE_MCS_HT_MSK
)) {
626 if (num_of_ant
== 1) {
627 if (band
== IEEE80211_BAND_5GHZ
)
634 if (rate_n_flags
& RATE_MCS_SGI_MSK
)
637 if ((rate_n_flags
& RATE_MCS_HT40_MSK
) ||
638 (rate_n_flags
& RATE_MCS_DUP_MSK
))
641 if (rate_n_flags
& RATE_MCS_DUP_MSK
)
644 mcs
= rs_extract_rate(rate_n_flags
);
647 if (mcs
<= IWL_RATE_SISO_60M_PLCP
) {
649 tbl
->lq_type
= LQ_SISO
; /*else NONE*/
651 } else if (mcs
<= IWL_RATE_MIMO2_60M_PLCP
) {
653 tbl
->lq_type
= LQ_MIMO2
;
656 if (num_of_ant
== 3) {
657 tbl
->max_search
= IWL_MAX_11N_MIMO3_SEARCH
;
658 tbl
->lq_type
= LQ_MIMO3
;
665 /* switch to another antenna/antennas and return 1 */
666 /* if no other valid antenna found, return 0 */
667 static int rs_toggle_antenna(u32 valid_ant
, u32
*rate_n_flags
,
668 struct iwl_scale_tbl_info
*tbl
)
672 if (!tbl
->ant_type
|| tbl
->ant_type
> ANT_ABC
)
675 if (!rs_is_valid_ant(valid_ant
, tbl
->ant_type
))
678 new_ant_type
= ant_toggle_lookup
[tbl
->ant_type
];
680 while ((new_ant_type
!= tbl
->ant_type
) &&
681 !rs_is_valid_ant(valid_ant
, new_ant_type
))
682 new_ant_type
= ant_toggle_lookup
[new_ant_type
];
684 if (new_ant_type
== tbl
->ant_type
)
687 tbl
->ant_type
= new_ant_type
;
688 *rate_n_flags
&= ~RATE_MCS_ANT_ABC_MSK
;
689 *rate_n_flags
|= new_ant_type
<< RATE_MCS_ANT_POS
;
694 * Green-field mode is valid if the station supports it and
695 * there are no non-GF stations present in the BSS.
697 static bool rs_use_green(struct ieee80211_sta
*sta
)
700 * There's a bug somewhere in this code that causes the
701 * scaling to get stuck because GF+SGI can't be combined
702 * in SISO rates. Until we find that bug, disable GF, it
703 * has only limited benefit and we still interoperate with
704 * GF APs since we can always receive GF transmissions.
710 * rs_get_supported_rates - get the available rates
712 * if management frame or broadcast frame only return
713 * basic available rates.
716 static u16
rs_get_supported_rates(struct iwl_lq_sta
*lq_sta
,
717 struct ieee80211_hdr
*hdr
,
718 enum iwl_table_type rate_type
)
720 if (is_legacy(rate_type
)) {
721 return lq_sta
->active_legacy_rate
;
723 if (is_siso(rate_type
))
724 return lq_sta
->active_siso_rate
;
725 else if (is_mimo2(rate_type
))
726 return lq_sta
->active_mimo2_rate
;
728 return lq_sta
->active_mimo3_rate
;
732 static u16
rs_get_adjacent_rate(struct iwl_priv
*priv
, u8 index
, u16 rate_mask
,
735 u8 high
= IWL_RATE_INVALID
;
736 u8 low
= IWL_RATE_INVALID
;
738 /* 802.11A or ht walks to the next literal adjacent rate in
740 if (is_a_band(rate_type
) || !is_legacy(rate_type
)) {
744 /* Find the previous rate that is in the rate mask */
746 for (mask
= (1 << i
); i
>= 0; i
--, mask
>>= 1) {
747 if (rate_mask
& mask
) {
753 /* Find the next rate that is in the rate mask */
755 for (mask
= (1 << i
); i
< IWL_RATE_COUNT
; i
++, mask
<<= 1) {
756 if (rate_mask
& mask
) {
762 return (high
<< 8) | low
;
766 while (low
!= IWL_RATE_INVALID
) {
767 low
= iwl_rates
[low
].prev_rs
;
768 if (low
== IWL_RATE_INVALID
)
770 if (rate_mask
& (1 << low
))
772 IWL_DEBUG_RATE(priv
, "Skipping masked lower rate: %d\n", low
);
776 while (high
!= IWL_RATE_INVALID
) {
777 high
= iwl_rates
[high
].next_rs
;
778 if (high
== IWL_RATE_INVALID
)
780 if (rate_mask
& (1 << high
))
782 IWL_DEBUG_RATE(priv
, "Skipping masked higher rate: %d\n", high
);
785 return (high
<< 8) | low
;
788 static u32
rs_get_lower_rate(struct iwl_lq_sta
*lq_sta
,
789 struct iwl_scale_tbl_info
*tbl
,
790 u8 scale_index
, u8 ht_possible
)
795 u8 switch_to_legacy
= 0;
796 u8 is_green
= lq_sta
->is_green
;
797 struct iwl_priv
*priv
= lq_sta
->drv
;
799 /* check if we need to switch from HT to legacy rates.
800 * assumption is that mandatory rates (1Mbps or 6Mbps)
801 * are always supported (spec demand) */
802 if (!is_legacy(tbl
->lq_type
) && (!ht_possible
|| !scale_index
)) {
803 switch_to_legacy
= 1;
804 scale_index
= rs_ht_to_legacy
[scale_index
];
805 if (lq_sta
->band
== IEEE80211_BAND_5GHZ
)
810 if (num_of_ant(tbl
->ant_type
) > 1)
812 first_antenna(priv
->nvm_data
->valid_tx_ant
);
816 tbl
->max_search
= IWL_MAX_SEARCH
;
819 rate_mask
= rs_get_supported_rates(lq_sta
, NULL
, tbl
->lq_type
);
821 /* Mask with station rate restriction */
822 if (is_legacy(tbl
->lq_type
)) {
823 /* supp_rates has no CCK bits in A mode */
824 if (lq_sta
->band
== IEEE80211_BAND_5GHZ
)
825 rate_mask
= (u16
)(rate_mask
&
826 (lq_sta
->supp_rates
<< IWL_FIRST_OFDM_RATE
));
828 rate_mask
= (u16
)(rate_mask
& lq_sta
->supp_rates
);
831 /* If we switched from HT to legacy, check current rate */
832 if (switch_to_legacy
&& (rate_mask
& (1 << scale_index
))) {
837 high_low
= rs_get_adjacent_rate(lq_sta
->drv
, scale_index
, rate_mask
,
839 low
= high_low
& 0xff;
841 if (low
== IWL_RATE_INVALID
)
845 return rate_n_flags_from_tbl(lq_sta
->drv
, tbl
, low
, is_green
);
849 * Simple function to compare two rate scale table types
851 static bool table_type_matches(struct iwl_scale_tbl_info
*a
,
852 struct iwl_scale_tbl_info
*b
)
854 return (a
->lq_type
== b
->lq_type
) && (a
->ant_type
== b
->ant_type
) &&
855 (a
->is_SGI
== b
->is_SGI
);
858 static void rs_bt_update_lq(struct iwl_priv
*priv
, struct iwl_rxon_context
*ctx
,
859 struct iwl_lq_sta
*lq_sta
)
861 struct iwl_scale_tbl_info
*tbl
;
862 bool full_concurrent
= priv
->bt_full_concurrent
;
864 if (priv
->bt_ant_couple_ok
) {
866 * Is there a need to switch between
867 * full concurrency and 3-wire?
869 if (priv
->bt_ci_compliance
&& priv
->bt_ant_couple_ok
)
870 full_concurrent
= true;
872 full_concurrent
= false;
874 if ((priv
->bt_traffic_load
!= priv
->last_bt_traffic_load
) ||
875 (priv
->bt_full_concurrent
!= full_concurrent
)) {
876 priv
->bt_full_concurrent
= full_concurrent
;
877 priv
->last_bt_traffic_load
= priv
->bt_traffic_load
;
879 /* Update uCode's rate table. */
880 tbl
= &(lq_sta
->lq_info
[lq_sta
->active_tbl
]);
881 rs_fill_link_cmd(priv
, lq_sta
, tbl
->current_rate
);
882 iwl_send_lq_cmd(priv
, ctx
, &lq_sta
->lq
, CMD_ASYNC
, false);
884 queue_work(priv
->workqueue
, &priv
->bt_full_concurrency
);
889 * mac80211 sends us Tx status
891 static void rs_tx_status(void *priv_r
, struct ieee80211_supported_band
*sband
,
892 struct ieee80211_sta
*sta
, void *priv_sta
,
897 int rs_index
, mac_index
, i
;
898 struct iwl_lq_sta
*lq_sta
= priv_sta
;
899 struct iwl_link_quality_cmd
*table
;
900 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*)skb
->data
;
901 struct iwl_op_mode
*op_mode
= (struct iwl_op_mode
*)priv_r
;
902 struct iwl_priv
*priv
= IWL_OP_MODE_GET_DVM(op_mode
);
903 struct ieee80211_tx_info
*info
= IEEE80211_SKB_CB(skb
);
904 enum mac80211_rate_control_flags mac_flags
;
906 struct iwl_scale_tbl_info tbl_type
;
907 struct iwl_scale_tbl_info
*curr_tbl
, *other_tbl
, *tmp_tbl
;
908 struct iwl_station_priv
*sta_priv
= (void *)sta
->drv_priv
;
909 struct iwl_rxon_context
*ctx
= sta_priv
->ctx
;
911 IWL_DEBUG_RATE_LIMIT(priv
, "get frame ack response, update rate scale window\n");
913 /* Treat uninitialized rate scaling data same as non-existing. */
915 IWL_DEBUG_RATE(priv
, "Station rate scaling not created yet.\n");
917 } else if (!lq_sta
->drv
) {
918 IWL_DEBUG_RATE(priv
, "Rate scaling not initialized yet.\n");
922 if (!ieee80211_is_data(hdr
->frame_control
) ||
923 info
->flags
& IEEE80211_TX_CTL_NO_ACK
)
926 /* This packet was aggregated but doesn't carry status info */
927 if ((info
->flags
& IEEE80211_TX_CTL_AMPDU
) &&
928 !(info
->flags
& IEEE80211_TX_STAT_AMPDU
))
932 * Ignore this Tx frame response if its initial rate doesn't match
933 * that of latest Link Quality command. There may be stragglers
934 * from a previous Link Quality command, but we're no longer interested
935 * in those; they're either from the "active" mode while we're trying
936 * to check "search" mode, or a prior "search" mode after we've moved
937 * to a new "search" mode (which might become the new "active" mode).
940 tx_rate
= le32_to_cpu(table
->rs_table
[0].rate_n_flags
);
941 rs_get_tbl_info_from_mcs(tx_rate
, priv
->band
, &tbl_type
, &rs_index
);
942 if (priv
->band
== IEEE80211_BAND_5GHZ
)
943 rs_index
-= IWL_FIRST_OFDM_RATE
;
944 mac_flags
= info
->status
.rates
[0].flags
;
945 mac_index
= info
->status
.rates
[0].idx
;
946 /* For HT packets, map MCS to PLCP */
947 if (mac_flags
& IEEE80211_TX_RC_MCS
) {
948 mac_index
&= RATE_MCS_CODE_MSK
; /* Remove # of streams */
949 if (mac_index
>= (IWL_RATE_9M_INDEX
- IWL_FIRST_OFDM_RATE
))
952 * mac80211 HT index is always zero-indexed; we need to move
953 * HT OFDM rates after CCK rates in 2.4 GHz band
955 if (priv
->band
== IEEE80211_BAND_2GHZ
)
956 mac_index
+= IWL_FIRST_OFDM_RATE
;
958 /* Here we actually compare this rate to the latest LQ command */
959 if ((mac_index
< 0) ||
960 (tbl_type
.is_SGI
!= !!(mac_flags
& IEEE80211_TX_RC_SHORT_GI
)) ||
961 (tbl_type
.is_ht40
!= !!(mac_flags
& IEEE80211_TX_RC_40_MHZ_WIDTH
)) ||
962 (tbl_type
.is_dup
!= !!(mac_flags
& IEEE80211_TX_RC_DUP_DATA
)) ||
963 (tbl_type
.ant_type
!= info
->status
.antenna
) ||
964 (!!(tx_rate
& RATE_MCS_HT_MSK
) != !!(mac_flags
& IEEE80211_TX_RC_MCS
)) ||
965 (!!(tx_rate
& RATE_MCS_GF_MSK
) != !!(mac_flags
& IEEE80211_TX_RC_GREEN_FIELD
)) ||
966 (rs_index
!= mac_index
)) {
967 IWL_DEBUG_RATE(priv
, "initial rate %d does not match %d (0x%x)\n", mac_index
, rs_index
, tx_rate
);
969 * Since rates mis-match, the last LQ command may have failed.
970 * After IWL_MISSED_RATE_MAX mis-matches, resync the uCode with
973 lq_sta
->missed_rate_counter
++;
974 if (lq_sta
->missed_rate_counter
> IWL_MISSED_RATE_MAX
) {
975 lq_sta
->missed_rate_counter
= 0;
976 iwl_send_lq_cmd(priv
, ctx
, &lq_sta
->lq
, CMD_ASYNC
, false);
978 /* Regardless, ignore this status info for outdated rate */
981 /* Rate did match, so reset the missed_rate_counter */
982 lq_sta
->missed_rate_counter
= 0;
984 /* Figure out if rate scale algorithm is in active or search table */
985 if (table_type_matches(&tbl_type
,
986 &(lq_sta
->lq_info
[lq_sta
->active_tbl
]))) {
987 curr_tbl
= &(lq_sta
->lq_info
[lq_sta
->active_tbl
]);
988 other_tbl
= &(lq_sta
->lq_info
[1 - lq_sta
->active_tbl
]);
989 } else if (table_type_matches(&tbl_type
,
990 &lq_sta
->lq_info
[1 - lq_sta
->active_tbl
])) {
991 curr_tbl
= &(lq_sta
->lq_info
[1 - lq_sta
->active_tbl
]);
992 other_tbl
= &(lq_sta
->lq_info
[lq_sta
->active_tbl
]);
994 IWL_DEBUG_RATE(priv
, "Neither active nor search matches tx rate\n");
995 tmp_tbl
= &(lq_sta
->lq_info
[lq_sta
->active_tbl
]);
996 IWL_DEBUG_RATE(priv
, "active- lq:%x, ant:%x, SGI:%d\n",
997 tmp_tbl
->lq_type
, tmp_tbl
->ant_type
, tmp_tbl
->is_SGI
);
998 tmp_tbl
= &(lq_sta
->lq_info
[1 - lq_sta
->active_tbl
]);
999 IWL_DEBUG_RATE(priv
, "search- lq:%x, ant:%x, SGI:%d\n",
1000 tmp_tbl
->lq_type
, tmp_tbl
->ant_type
, tmp_tbl
->is_SGI
);
1001 IWL_DEBUG_RATE(priv
, "actual- lq:%x, ant:%x, SGI:%d\n",
1002 tbl_type
.lq_type
, tbl_type
.ant_type
, tbl_type
.is_SGI
);
1004 * no matching table found, let's by-pass the data collection
1005 * and continue to perform rate scale to find the rate table
1007 rs_stay_in_table(lq_sta
, true);
1012 * Updating the frame history depends on whether packets were
1015 * For aggregation, all packets were transmitted at the same rate, the
1016 * first index into rate scale table.
1018 if (info
->flags
& IEEE80211_TX_STAT_AMPDU
) {
1019 tx_rate
= le32_to_cpu(table
->rs_table
[0].rate_n_flags
);
1020 rs_get_tbl_info_from_mcs(tx_rate
, priv
->band
, &tbl_type
,
1022 rs_collect_tx_data(curr_tbl
, rs_index
,
1023 info
->status
.ampdu_len
,
1024 info
->status
.ampdu_ack_len
);
1026 /* Update success/fail counts if not searching for new mode */
1027 if (lq_sta
->stay_in_tbl
) {
1028 lq_sta
->total_success
+= info
->status
.ampdu_ack_len
;
1029 lq_sta
->total_failed
+= (info
->status
.ampdu_len
-
1030 info
->status
.ampdu_ack_len
);
1034 * For legacy, update frame history with for each Tx retry.
1036 retries
= info
->status
.rates
[0].count
- 1;
1037 /* HW doesn't send more than 15 retries */
1038 retries
= min(retries
, 15);
1040 /* The last transmission may have been successful */
1041 legacy_success
= !!(info
->flags
& IEEE80211_TX_STAT_ACK
);
1042 /* Collect data for each rate used during failed TX attempts */
1043 for (i
= 0; i
<= retries
; ++i
) {
1044 tx_rate
= le32_to_cpu(table
->rs_table
[i
].rate_n_flags
);
1045 rs_get_tbl_info_from_mcs(tx_rate
, priv
->band
,
1046 &tbl_type
, &rs_index
);
1048 * Only collect stats if retried rate is in the same RS
1049 * table as active/search.
1051 if (table_type_matches(&tbl_type
, curr_tbl
))
1053 else if (table_type_matches(&tbl_type
, other_tbl
))
1054 tmp_tbl
= other_tbl
;
1057 rs_collect_tx_data(tmp_tbl
, rs_index
, 1,
1058 i
< retries
? 0 : legacy_success
);
1061 /* Update success/fail counts if not searching for new mode */
1062 if (lq_sta
->stay_in_tbl
) {
1063 lq_sta
->total_success
+= legacy_success
;
1064 lq_sta
->total_failed
+= retries
+ (1 - legacy_success
);
1067 /* The last TX rate is cached in lq_sta; it's set in if/else above */
1068 lq_sta
->last_rate_n_flags
= tx_rate
;
1070 /* See if there's a better rate or modulation mode to try. */
1071 if (sta
&& sta
->supp_rates
[sband
->band
])
1072 rs_rate_scale_perform(priv
, skb
, sta
, lq_sta
);
1074 if (priv
->lib
->bt_params
&& priv
->lib
->bt_params
->advanced_bt_coexist
)
1075 rs_bt_update_lq(priv
, ctx
, lq_sta
);
1079 * Begin a period of staying with a selected modulation mode.
1080 * Set "stay_in_tbl" flag to prevent any mode switches.
1081 * Set frame tx success limits according to legacy vs. high-throughput,
1082 * and reset overall (spanning all rates) tx success history statistics.
1083 * These control how long we stay using same modulation mode before
1084 * searching for a new mode.
1086 static void rs_set_stay_in_table(struct iwl_priv
*priv
, u8 is_legacy
,
1087 struct iwl_lq_sta
*lq_sta
)
1089 IWL_DEBUG_RATE(priv
, "we are staying in the same table\n");
1090 lq_sta
->stay_in_tbl
= 1; /* only place this gets set */
1092 lq_sta
->table_count_limit
= IWL_LEGACY_TABLE_COUNT
;
1093 lq_sta
->max_failure_limit
= IWL_LEGACY_FAILURE_LIMIT
;
1094 lq_sta
->max_success_limit
= IWL_LEGACY_SUCCESS_LIMIT
;
1096 lq_sta
->table_count_limit
= IWL_NONE_LEGACY_TABLE_COUNT
;
1097 lq_sta
->max_failure_limit
= IWL_NONE_LEGACY_FAILURE_LIMIT
;
1098 lq_sta
->max_success_limit
= IWL_NONE_LEGACY_SUCCESS_LIMIT
;
1100 lq_sta
->table_count
= 0;
1101 lq_sta
->total_failed
= 0;
1102 lq_sta
->total_success
= 0;
1103 lq_sta
->flush_timer
= jiffies
;
1104 lq_sta
->action_counter
= 0;
1108 * Find correct throughput table for given mode of modulation
1110 static void rs_set_expected_tpt_table(struct iwl_lq_sta
*lq_sta
,
1111 struct iwl_scale_tbl_info
*tbl
)
1113 /* Used to choose among HT tables */
1114 const u16 (*ht_tbl_pointer
)[IWL_RATE_COUNT
];
1116 /* Check for invalid LQ type */
1117 if (WARN_ON_ONCE(!is_legacy(tbl
->lq_type
) && !is_Ht(tbl
->lq_type
))) {
1118 tbl
->expected_tpt
= expected_tpt_legacy
;
1122 /* Legacy rates have only one table */
1123 if (is_legacy(tbl
->lq_type
)) {
1124 tbl
->expected_tpt
= expected_tpt_legacy
;
1128 /* Choose among many HT tables depending on number of streams
1129 * (SISO/MIMO2/MIMO3), channel width (20/40), SGI, and aggregation
1131 if (is_siso(tbl
->lq_type
) && (!tbl
->is_ht40
|| lq_sta
->is_dup
))
1132 ht_tbl_pointer
= expected_tpt_siso20MHz
;
1133 else if (is_siso(tbl
->lq_type
))
1134 ht_tbl_pointer
= expected_tpt_siso40MHz
;
1135 else if (is_mimo2(tbl
->lq_type
) && (!tbl
->is_ht40
|| lq_sta
->is_dup
))
1136 ht_tbl_pointer
= expected_tpt_mimo2_20MHz
;
1137 else if (is_mimo2(tbl
->lq_type
))
1138 ht_tbl_pointer
= expected_tpt_mimo2_40MHz
;
1139 else if (is_mimo3(tbl
->lq_type
) && (!tbl
->is_ht40
|| lq_sta
->is_dup
))
1140 ht_tbl_pointer
= expected_tpt_mimo3_20MHz
;
1141 else /* if (is_mimo3(tbl->lq_type)) <-- must be true */
1142 ht_tbl_pointer
= expected_tpt_mimo3_40MHz
;
1144 if (!tbl
->is_SGI
&& !lq_sta
->is_agg
) /* Normal */
1145 tbl
->expected_tpt
= ht_tbl_pointer
[0];
1146 else if (tbl
->is_SGI
&& !lq_sta
->is_agg
) /* SGI */
1147 tbl
->expected_tpt
= ht_tbl_pointer
[1];
1148 else if (!tbl
->is_SGI
&& lq_sta
->is_agg
) /* AGG */
1149 tbl
->expected_tpt
= ht_tbl_pointer
[2];
1151 tbl
->expected_tpt
= ht_tbl_pointer
[3];
1155 * Find starting rate for new "search" high-throughput mode of modulation.
1156 * Goal is to find lowest expected rate (under perfect conditions) that is
1157 * above the current measured throughput of "active" mode, to give new mode
1158 * a fair chance to prove itself without too many challenges.
1160 * This gets called when transitioning to more aggressive modulation
1161 * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
1162 * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
1163 * to decrease to match "active" throughput. When moving from MIMO to SISO,
1164 * bit rate will typically need to increase, but not if performance was bad.
1166 static s32
rs_get_best_rate(struct iwl_priv
*priv
,
1167 struct iwl_lq_sta
*lq_sta
,
1168 struct iwl_scale_tbl_info
*tbl
, /* "search" */
1169 u16 rate_mask
, s8 index
)
1171 /* "active" values */
1172 struct iwl_scale_tbl_info
*active_tbl
=
1173 &(lq_sta
->lq_info
[lq_sta
->active_tbl
]);
1174 s32 active_sr
= active_tbl
->win
[index
].success_ratio
;
1175 s32 active_tpt
= active_tbl
->expected_tpt
[index
];
1176 /* expected "search" throughput */
1177 const u16
*tpt_tbl
= tbl
->expected_tpt
;
1179 s32 new_rate
, high
, low
, start_hi
;
1183 new_rate
= high
= low
= start_hi
= IWL_RATE_INVALID
;
1186 high_low
= rs_get_adjacent_rate(priv
, rate
, rate_mask
,
1189 low
= high_low
& 0xff;
1190 high
= (high_low
>> 8) & 0xff;
1193 * Lower the "search" bit rate, to give new "search" mode
1194 * approximately the same throughput as "active" if:
1196 * 1) "Active" mode has been working modestly well (but not
1197 * great), and expected "search" throughput (under perfect
1198 * conditions) at candidate rate is above the actual
1199 * measured "active" throughput (but less than expected
1200 * "active" throughput under perfect conditions).
1202 * 2) "Active" mode has been working perfectly or very well
1203 * and expected "search" throughput (under perfect
1204 * conditions) at candidate rate is above expected
1205 * "active" throughput (under perfect conditions).
1207 if ((((100 * tpt_tbl
[rate
]) > lq_sta
->last_tpt
) &&
1208 ((active_sr
> IWL_RATE_DECREASE_TH
) &&
1209 (active_sr
<= IWL_RATE_HIGH_TH
) &&
1210 (tpt_tbl
[rate
] <= active_tpt
))) ||
1211 ((active_sr
>= IWL_RATE_SCALE_SWITCH
) &&
1212 (tpt_tbl
[rate
] > active_tpt
))) {
1214 /* (2nd or later pass)
1215 * If we've already tried to raise the rate, and are
1216 * now trying to lower it, use the higher rate. */
1217 if (start_hi
!= IWL_RATE_INVALID
) {
1218 new_rate
= start_hi
;
1224 /* Loop again with lower rate */
1225 if (low
!= IWL_RATE_INVALID
)
1228 /* Lower rate not available, use the original */
1232 /* Else try to raise the "search" rate to match "active" */
1234 /* (2nd or later pass)
1235 * If we've already tried to lower the rate, and are
1236 * now trying to raise it, use the lower rate. */
1237 if (new_rate
!= IWL_RATE_INVALID
)
1240 /* Loop again with higher rate */
1241 else if (high
!= IWL_RATE_INVALID
) {
1245 /* Higher rate not available, use the original */
1257 * Set up search table for MIMO2
1259 static int rs_switch_to_mimo2(struct iwl_priv
*priv
,
1260 struct iwl_lq_sta
*lq_sta
,
1261 struct ieee80211_conf
*conf
,
1262 struct ieee80211_sta
*sta
,
1263 struct iwl_scale_tbl_info
*tbl
, int index
)
1267 s8 is_green
= lq_sta
->is_green
;
1268 struct iwl_station_priv
*sta_priv
= (void *)sta
->drv_priv
;
1269 struct iwl_rxon_context
*ctx
= sta_priv
->ctx
;
1271 if (!conf_is_ht(conf
) || !sta
->ht_cap
.ht_supported
)
1274 if (sta
->smps_mode
== IEEE80211_SMPS_STATIC
)
1277 /* Need both Tx chains/antennas to support MIMO */
1278 if (priv
->hw_params
.tx_chains_num
< 2)
1281 IWL_DEBUG_RATE(priv
, "LQ: try to switch to MIMO2\n");
1283 tbl
->lq_type
= LQ_MIMO2
;
1284 tbl
->is_dup
= lq_sta
->is_dup
;
1286 tbl
->max_search
= IWL_MAX_SEARCH
;
1287 rate_mask
= lq_sta
->active_mimo2_rate
;
1289 if (iwl_is_ht40_tx_allowed(priv
, ctx
, sta
))
1294 rs_set_expected_tpt_table(lq_sta
, tbl
);
1296 rate
= rs_get_best_rate(priv
, lq_sta
, tbl
, rate_mask
, index
);
1298 IWL_DEBUG_RATE(priv
, "LQ: MIMO2 best rate %d mask %X\n", rate
, rate_mask
);
1299 if ((rate
== IWL_RATE_INVALID
) || !((1 << rate
) & rate_mask
)) {
1300 IWL_DEBUG_RATE(priv
, "Can't switch with index %d rate mask %x\n",
1304 tbl
->current_rate
= rate_n_flags_from_tbl(priv
, tbl
, rate
, is_green
);
1306 IWL_DEBUG_RATE(priv
, "LQ: Switch to new mcs %X index is green %X\n",
1307 tbl
->current_rate
, is_green
);
1312 * Set up search table for MIMO3
1314 static int rs_switch_to_mimo3(struct iwl_priv
*priv
,
1315 struct iwl_lq_sta
*lq_sta
,
1316 struct ieee80211_conf
*conf
,
1317 struct ieee80211_sta
*sta
,
1318 struct iwl_scale_tbl_info
*tbl
, int index
)
1322 s8 is_green
= lq_sta
->is_green
;
1323 struct iwl_station_priv
*sta_priv
= (void *)sta
->drv_priv
;
1324 struct iwl_rxon_context
*ctx
= sta_priv
->ctx
;
1326 if (!conf_is_ht(conf
) || !sta
->ht_cap
.ht_supported
)
1329 if (sta
->smps_mode
== IEEE80211_SMPS_STATIC
)
1332 /* Need both Tx chains/antennas to support MIMO */
1333 if (priv
->hw_params
.tx_chains_num
< 3)
1336 IWL_DEBUG_RATE(priv
, "LQ: try to switch to MIMO3\n");
1338 tbl
->lq_type
= LQ_MIMO3
;
1339 tbl
->is_dup
= lq_sta
->is_dup
;
1341 tbl
->max_search
= IWL_MAX_11N_MIMO3_SEARCH
;
1342 rate_mask
= lq_sta
->active_mimo3_rate
;
1344 if (iwl_is_ht40_tx_allowed(priv
, ctx
, sta
))
1349 rs_set_expected_tpt_table(lq_sta
, tbl
);
1351 rate
= rs_get_best_rate(priv
, lq_sta
, tbl
, rate_mask
, index
);
1353 IWL_DEBUG_RATE(priv
, "LQ: MIMO3 best rate %d mask %X\n",
1355 if ((rate
== IWL_RATE_INVALID
) || !((1 << rate
) & rate_mask
)) {
1356 IWL_DEBUG_RATE(priv
, "Can't switch with index %d rate mask %x\n",
1360 tbl
->current_rate
= rate_n_flags_from_tbl(priv
, tbl
, rate
, is_green
);
1362 IWL_DEBUG_RATE(priv
, "LQ: Switch to new mcs %X index is green %X\n",
1363 tbl
->current_rate
, is_green
);
1368 * Set up search table for SISO
1370 static int rs_switch_to_siso(struct iwl_priv
*priv
,
1371 struct iwl_lq_sta
*lq_sta
,
1372 struct ieee80211_conf
*conf
,
1373 struct ieee80211_sta
*sta
,
1374 struct iwl_scale_tbl_info
*tbl
, int index
)
1377 u8 is_green
= lq_sta
->is_green
;
1379 struct iwl_station_priv
*sta_priv
= (void *)sta
->drv_priv
;
1380 struct iwl_rxon_context
*ctx
= sta_priv
->ctx
;
1382 if (!conf_is_ht(conf
) || !sta
->ht_cap
.ht_supported
)
1385 IWL_DEBUG_RATE(priv
, "LQ: try to switch to SISO\n");
1387 tbl
->is_dup
= lq_sta
->is_dup
;
1388 tbl
->lq_type
= LQ_SISO
;
1390 tbl
->max_search
= IWL_MAX_SEARCH
;
1391 rate_mask
= lq_sta
->active_siso_rate
;
1393 if (iwl_is_ht40_tx_allowed(priv
, ctx
, sta
))
1399 tbl
->is_SGI
= 0; /*11n spec: no SGI in SISO+Greenfield*/
1401 rs_set_expected_tpt_table(lq_sta
, tbl
);
1402 rate
= rs_get_best_rate(priv
, lq_sta
, tbl
, rate_mask
, index
);
1404 IWL_DEBUG_RATE(priv
, "LQ: get best rate %d mask %X\n", rate
, rate_mask
);
1405 if ((rate
== IWL_RATE_INVALID
) || !((1 << rate
) & rate_mask
)) {
1406 IWL_DEBUG_RATE(priv
, "can not switch with index %d rate mask %x\n",
1410 tbl
->current_rate
= rate_n_flags_from_tbl(priv
, tbl
, rate
, is_green
);
1411 IWL_DEBUG_RATE(priv
, "LQ: Switch to new mcs %X index is green %X\n",
1412 tbl
->current_rate
, is_green
);
1417 * Try to switch to new modulation mode from legacy
1419 static int rs_move_legacy_other(struct iwl_priv
*priv
,
1420 struct iwl_lq_sta
*lq_sta
,
1421 struct ieee80211_conf
*conf
,
1422 struct ieee80211_sta
*sta
,
1425 struct iwl_scale_tbl_info
*tbl
= &(lq_sta
->lq_info
[lq_sta
->active_tbl
]);
1426 struct iwl_scale_tbl_info
*search_tbl
=
1427 &(lq_sta
->lq_info
[(1 - lq_sta
->active_tbl
)]);
1428 struct iwl_rate_scale_data
*window
= &(tbl
->win
[index
]);
1429 u32 sz
= (sizeof(struct iwl_scale_tbl_info
) -
1430 (sizeof(struct iwl_rate_scale_data
) * IWL_RATE_COUNT
));
1432 u8 valid_tx_ant
= priv
->nvm_data
->valid_tx_ant
;
1433 u8 tx_chains_num
= priv
->hw_params
.tx_chains_num
;
1435 u8 update_search_tbl_counter
= 0;
1437 switch (priv
->bt_traffic_load
) {
1438 case IWL_BT_COEX_TRAFFIC_LOAD_NONE
:
1441 case IWL_BT_COEX_TRAFFIC_LOAD_LOW
:
1442 /* avoid antenna B unless MIMO */
1443 if (tbl
->action
== IWL_LEGACY_SWITCH_ANTENNA2
)
1444 tbl
->action
= IWL_LEGACY_SWITCH_SISO
;
1446 case IWL_BT_COEX_TRAFFIC_LOAD_HIGH
:
1447 case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS
:
1448 /* avoid antenna B and MIMO */
1450 first_antenna(priv
->nvm_data
->valid_tx_ant
);
1451 if (tbl
->action
>= IWL_LEGACY_SWITCH_ANTENNA2
&&
1452 tbl
->action
!= IWL_LEGACY_SWITCH_SISO
)
1453 tbl
->action
= IWL_LEGACY_SWITCH_SISO
;
1456 IWL_ERR(priv
, "Invalid BT load %d", priv
->bt_traffic_load
);
1460 if (!iwl_ht_enabled(priv
))
1461 /* stay in Legacy */
1462 tbl
->action
= IWL_LEGACY_SWITCH_ANTENNA1
;
1463 else if (iwl_tx_ant_restriction(priv
) == IWL_ANT_OK_SINGLE
&&
1464 tbl
->action
> IWL_LEGACY_SWITCH_SISO
)
1465 tbl
->action
= IWL_LEGACY_SWITCH_SISO
;
1467 /* configure as 1x1 if bt full concurrency */
1468 if (priv
->bt_full_concurrent
) {
1469 if (!iwl_ht_enabled(priv
))
1470 tbl
->action
= IWL_LEGACY_SWITCH_ANTENNA1
;
1471 else if (tbl
->action
>= IWL_LEGACY_SWITCH_ANTENNA2
)
1472 tbl
->action
= IWL_LEGACY_SWITCH_SISO
;
1474 first_antenna(priv
->nvm_data
->valid_tx_ant
);
1477 start_action
= tbl
->action
;
1479 lq_sta
->action_counter
++;
1480 switch (tbl
->action
) {
1481 case IWL_LEGACY_SWITCH_ANTENNA1
:
1482 case IWL_LEGACY_SWITCH_ANTENNA2
:
1483 IWL_DEBUG_RATE(priv
, "LQ: Legacy toggle Antenna\n");
1485 if ((tbl
->action
== IWL_LEGACY_SWITCH_ANTENNA1
&&
1486 tx_chains_num
<= 1) ||
1487 (tbl
->action
== IWL_LEGACY_SWITCH_ANTENNA2
&&
1488 tx_chains_num
<= 2))
1491 /* Don't change antenna if success has been great */
1492 if (window
->success_ratio
>= IWL_RS_GOOD_RATIO
&&
1493 !priv
->bt_full_concurrent
&&
1494 priv
->bt_traffic_load
==
1495 IWL_BT_COEX_TRAFFIC_LOAD_NONE
)
1498 /* Set up search table to try other antenna */
1499 memcpy(search_tbl
, tbl
, sz
);
1501 if (rs_toggle_antenna(valid_tx_ant
,
1502 &search_tbl
->current_rate
, search_tbl
)) {
1503 update_search_tbl_counter
= 1;
1504 rs_set_expected_tpt_table(lq_sta
, search_tbl
);
1508 case IWL_LEGACY_SWITCH_SISO
:
1509 IWL_DEBUG_RATE(priv
, "LQ: Legacy switch to SISO\n");
1511 /* Set up search table to try SISO */
1512 memcpy(search_tbl
, tbl
, sz
);
1513 search_tbl
->is_SGI
= 0;
1514 ret
= rs_switch_to_siso(priv
, lq_sta
, conf
, sta
,
1517 lq_sta
->action_counter
= 0;
1522 case IWL_LEGACY_SWITCH_MIMO2_AB
:
1523 case IWL_LEGACY_SWITCH_MIMO2_AC
:
1524 case IWL_LEGACY_SWITCH_MIMO2_BC
:
1525 IWL_DEBUG_RATE(priv
, "LQ: Legacy switch to MIMO2\n");
1527 /* Set up search table to try MIMO */
1528 memcpy(search_tbl
, tbl
, sz
);
1529 search_tbl
->is_SGI
= 0;
1531 if (tbl
->action
== IWL_LEGACY_SWITCH_MIMO2_AB
)
1532 search_tbl
->ant_type
= ANT_AB
;
1533 else if (tbl
->action
== IWL_LEGACY_SWITCH_MIMO2_AC
)
1534 search_tbl
->ant_type
= ANT_AC
;
1536 search_tbl
->ant_type
= ANT_BC
;
1538 if (!rs_is_valid_ant(valid_tx_ant
, search_tbl
->ant_type
))
1541 ret
= rs_switch_to_mimo2(priv
, lq_sta
, conf
, sta
,
1544 lq_sta
->action_counter
= 0;
1549 case IWL_LEGACY_SWITCH_MIMO3_ABC
:
1550 IWL_DEBUG_RATE(priv
, "LQ: Legacy switch to MIMO3\n");
1552 /* Set up search table to try MIMO3 */
1553 memcpy(search_tbl
, tbl
, sz
);
1554 search_tbl
->is_SGI
= 0;
1556 search_tbl
->ant_type
= ANT_ABC
;
1558 if (!rs_is_valid_ant(valid_tx_ant
, search_tbl
->ant_type
))
1561 ret
= rs_switch_to_mimo3(priv
, lq_sta
, conf
, sta
,
1564 lq_sta
->action_counter
= 0;
1570 if (tbl
->action
> IWL_LEGACY_SWITCH_MIMO3_ABC
)
1571 tbl
->action
= IWL_LEGACY_SWITCH_ANTENNA1
;
1573 if (tbl
->action
== start_action
)
1577 search_tbl
->lq_type
= LQ_NONE
;
1581 lq_sta
->search_better_tbl
= 1;
1583 if (tbl
->action
> IWL_LEGACY_SWITCH_MIMO3_ABC
)
1584 tbl
->action
= IWL_LEGACY_SWITCH_ANTENNA1
;
1585 if (update_search_tbl_counter
)
1586 search_tbl
->action
= tbl
->action
;
1592 * Try to switch to new modulation mode from SISO
1594 static int rs_move_siso_to_other(struct iwl_priv
*priv
,
1595 struct iwl_lq_sta
*lq_sta
,
1596 struct ieee80211_conf
*conf
,
1597 struct ieee80211_sta
*sta
, int index
)
1599 u8 is_green
= lq_sta
->is_green
;
1600 struct iwl_scale_tbl_info
*tbl
= &(lq_sta
->lq_info
[lq_sta
->active_tbl
]);
1601 struct iwl_scale_tbl_info
*search_tbl
=
1602 &(lq_sta
->lq_info
[(1 - lq_sta
->active_tbl
)]);
1603 struct iwl_rate_scale_data
*window
= &(tbl
->win
[index
]);
1604 struct ieee80211_sta_ht_cap
*ht_cap
= &sta
->ht_cap
;
1605 u32 sz
= (sizeof(struct iwl_scale_tbl_info
) -
1606 (sizeof(struct iwl_rate_scale_data
) * IWL_RATE_COUNT
));
1608 u8 valid_tx_ant
= priv
->nvm_data
->valid_tx_ant
;
1609 u8 tx_chains_num
= priv
->hw_params
.tx_chains_num
;
1610 u8 update_search_tbl_counter
= 0;
1613 switch (priv
->bt_traffic_load
) {
1614 case IWL_BT_COEX_TRAFFIC_LOAD_NONE
:
1617 case IWL_BT_COEX_TRAFFIC_LOAD_LOW
:
1618 /* avoid antenna B unless MIMO */
1619 if (tbl
->action
== IWL_SISO_SWITCH_ANTENNA2
)
1620 tbl
->action
= IWL_SISO_SWITCH_MIMO2_AB
;
1622 case IWL_BT_COEX_TRAFFIC_LOAD_HIGH
:
1623 case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS
:
1624 /* avoid antenna B and MIMO */
1626 first_antenna(priv
->nvm_data
->valid_tx_ant
);
1627 if (tbl
->action
!= IWL_SISO_SWITCH_ANTENNA1
)
1628 tbl
->action
= IWL_SISO_SWITCH_ANTENNA1
;
1631 IWL_ERR(priv
, "Invalid BT load %d", priv
->bt_traffic_load
);
1635 if (iwl_tx_ant_restriction(priv
) == IWL_ANT_OK_SINGLE
&&
1636 tbl
->action
> IWL_SISO_SWITCH_ANTENNA2
) {
1638 tbl
->action
= IWL_SISO_SWITCH_ANTENNA1
;
1641 /* configure as 1x1 if bt full concurrency */
1642 if (priv
->bt_full_concurrent
) {
1644 first_antenna(priv
->nvm_data
->valid_tx_ant
);
1645 if (tbl
->action
>= IWL_LEGACY_SWITCH_ANTENNA2
)
1646 tbl
->action
= IWL_SISO_SWITCH_ANTENNA1
;
1649 start_action
= tbl
->action
;
1651 lq_sta
->action_counter
++;
1652 switch (tbl
->action
) {
1653 case IWL_SISO_SWITCH_ANTENNA1
:
1654 case IWL_SISO_SWITCH_ANTENNA2
:
1655 IWL_DEBUG_RATE(priv
, "LQ: SISO toggle Antenna\n");
1656 if ((tbl
->action
== IWL_SISO_SWITCH_ANTENNA1
&&
1657 tx_chains_num
<= 1) ||
1658 (tbl
->action
== IWL_SISO_SWITCH_ANTENNA2
&&
1659 tx_chains_num
<= 2))
1662 if (window
->success_ratio
>= IWL_RS_GOOD_RATIO
&&
1663 !priv
->bt_full_concurrent
&&
1664 priv
->bt_traffic_load
==
1665 IWL_BT_COEX_TRAFFIC_LOAD_NONE
)
1668 memcpy(search_tbl
, tbl
, sz
);
1669 if (rs_toggle_antenna(valid_tx_ant
,
1670 &search_tbl
->current_rate
, search_tbl
)) {
1671 update_search_tbl_counter
= 1;
1675 case IWL_SISO_SWITCH_MIMO2_AB
:
1676 case IWL_SISO_SWITCH_MIMO2_AC
:
1677 case IWL_SISO_SWITCH_MIMO2_BC
:
1678 IWL_DEBUG_RATE(priv
, "LQ: SISO switch to MIMO2\n");
1679 memcpy(search_tbl
, tbl
, sz
);
1680 search_tbl
->is_SGI
= 0;
1682 if (tbl
->action
== IWL_SISO_SWITCH_MIMO2_AB
)
1683 search_tbl
->ant_type
= ANT_AB
;
1684 else if (tbl
->action
== IWL_SISO_SWITCH_MIMO2_AC
)
1685 search_tbl
->ant_type
= ANT_AC
;
1687 search_tbl
->ant_type
= ANT_BC
;
1689 if (!rs_is_valid_ant(valid_tx_ant
, search_tbl
->ant_type
))
1692 ret
= rs_switch_to_mimo2(priv
, lq_sta
, conf
, sta
,
1697 case IWL_SISO_SWITCH_GI
:
1698 if (!tbl
->is_ht40
&& !(ht_cap
->cap
&
1699 IEEE80211_HT_CAP_SGI_20
))
1701 if (tbl
->is_ht40
&& !(ht_cap
->cap
&
1702 IEEE80211_HT_CAP_SGI_40
))
1705 IWL_DEBUG_RATE(priv
, "LQ: SISO toggle SGI/NGI\n");
1707 memcpy(search_tbl
, tbl
, sz
);
1713 "SGI was set in GF+SISO\n");
1715 search_tbl
->is_SGI
= !tbl
->is_SGI
;
1716 rs_set_expected_tpt_table(lq_sta
, search_tbl
);
1718 s32 tpt
= lq_sta
->last_tpt
/ 100;
1719 if (tpt
>= search_tbl
->expected_tpt
[index
])
1722 search_tbl
->current_rate
=
1723 rate_n_flags_from_tbl(priv
, search_tbl
,
1725 update_search_tbl_counter
= 1;
1727 case IWL_SISO_SWITCH_MIMO3_ABC
:
1728 IWL_DEBUG_RATE(priv
, "LQ: SISO switch to MIMO3\n");
1729 memcpy(search_tbl
, tbl
, sz
);
1730 search_tbl
->is_SGI
= 0;
1731 search_tbl
->ant_type
= ANT_ABC
;
1733 if (!rs_is_valid_ant(valid_tx_ant
, search_tbl
->ant_type
))
1736 ret
= rs_switch_to_mimo3(priv
, lq_sta
, conf
, sta
,
1743 if (tbl
->action
> IWL_LEGACY_SWITCH_MIMO3_ABC
)
1744 tbl
->action
= IWL_SISO_SWITCH_ANTENNA1
;
1746 if (tbl
->action
== start_action
)
1749 search_tbl
->lq_type
= LQ_NONE
;
1753 lq_sta
->search_better_tbl
= 1;
1755 if (tbl
->action
> IWL_SISO_SWITCH_MIMO3_ABC
)
1756 tbl
->action
= IWL_SISO_SWITCH_ANTENNA1
;
1757 if (update_search_tbl_counter
)
1758 search_tbl
->action
= tbl
->action
;
1764 * Try to switch to new modulation mode from MIMO2
1766 static int rs_move_mimo2_to_other(struct iwl_priv
*priv
,
1767 struct iwl_lq_sta
*lq_sta
,
1768 struct ieee80211_conf
*conf
,
1769 struct ieee80211_sta
*sta
, int index
)
1771 s8 is_green
= lq_sta
->is_green
;
1772 struct iwl_scale_tbl_info
*tbl
= &(lq_sta
->lq_info
[lq_sta
->active_tbl
]);
1773 struct iwl_scale_tbl_info
*search_tbl
=
1774 &(lq_sta
->lq_info
[(1 - lq_sta
->active_tbl
)]);
1775 struct iwl_rate_scale_data
*window
= &(tbl
->win
[index
]);
1776 struct ieee80211_sta_ht_cap
*ht_cap
= &sta
->ht_cap
;
1777 u32 sz
= (sizeof(struct iwl_scale_tbl_info
) -
1778 (sizeof(struct iwl_rate_scale_data
) * IWL_RATE_COUNT
));
1780 u8 valid_tx_ant
= priv
->nvm_data
->valid_tx_ant
;
1781 u8 tx_chains_num
= priv
->hw_params
.tx_chains_num
;
1782 u8 update_search_tbl_counter
= 0;
1785 switch (priv
->bt_traffic_load
) {
1786 case IWL_BT_COEX_TRAFFIC_LOAD_NONE
:
1789 case IWL_BT_COEX_TRAFFIC_LOAD_HIGH
:
1790 case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS
:
1791 /* avoid antenna B and MIMO */
1792 if (tbl
->action
!= IWL_MIMO2_SWITCH_SISO_A
)
1793 tbl
->action
= IWL_MIMO2_SWITCH_SISO_A
;
1795 case IWL_BT_COEX_TRAFFIC_LOAD_LOW
:
1796 /* avoid antenna B unless MIMO */
1797 if (tbl
->action
== IWL_MIMO2_SWITCH_SISO_B
||
1798 tbl
->action
== IWL_MIMO2_SWITCH_SISO_C
)
1799 tbl
->action
= IWL_MIMO2_SWITCH_SISO_A
;
1802 IWL_ERR(priv
, "Invalid BT load %d", priv
->bt_traffic_load
);
1806 if ((iwl_tx_ant_restriction(priv
) == IWL_ANT_OK_SINGLE
) &&
1807 (tbl
->action
< IWL_MIMO2_SWITCH_SISO_A
||
1808 tbl
->action
> IWL_MIMO2_SWITCH_SISO_C
)) {
1809 /* switch in SISO */
1810 tbl
->action
= IWL_MIMO2_SWITCH_SISO_A
;
1813 /* configure as 1x1 if bt full concurrency */
1814 if (priv
->bt_full_concurrent
&&
1815 (tbl
->action
< IWL_MIMO2_SWITCH_SISO_A
||
1816 tbl
->action
> IWL_MIMO2_SWITCH_SISO_C
))
1817 tbl
->action
= IWL_MIMO2_SWITCH_SISO_A
;
1819 start_action
= tbl
->action
;
1821 lq_sta
->action_counter
++;
1822 switch (tbl
->action
) {
1823 case IWL_MIMO2_SWITCH_ANTENNA1
:
1824 case IWL_MIMO2_SWITCH_ANTENNA2
:
1825 IWL_DEBUG_RATE(priv
, "LQ: MIMO2 toggle Antennas\n");
1827 if (tx_chains_num
<= 2)
1830 if (window
->success_ratio
>= IWL_RS_GOOD_RATIO
)
1833 memcpy(search_tbl
, tbl
, sz
);
1834 if (rs_toggle_antenna(valid_tx_ant
,
1835 &search_tbl
->current_rate
, search_tbl
)) {
1836 update_search_tbl_counter
= 1;
1840 case IWL_MIMO2_SWITCH_SISO_A
:
1841 case IWL_MIMO2_SWITCH_SISO_B
:
1842 case IWL_MIMO2_SWITCH_SISO_C
:
1843 IWL_DEBUG_RATE(priv
, "LQ: MIMO2 switch to SISO\n");
1845 /* Set up new search table for SISO */
1846 memcpy(search_tbl
, tbl
, sz
);
1848 if (tbl
->action
== IWL_MIMO2_SWITCH_SISO_A
)
1849 search_tbl
->ant_type
= ANT_A
;
1850 else if (tbl
->action
== IWL_MIMO2_SWITCH_SISO_B
)
1851 search_tbl
->ant_type
= ANT_B
;
1853 search_tbl
->ant_type
= ANT_C
;
1855 if (!rs_is_valid_ant(valid_tx_ant
, search_tbl
->ant_type
))
1858 ret
= rs_switch_to_siso(priv
, lq_sta
, conf
, sta
,
1865 case IWL_MIMO2_SWITCH_GI
:
1866 if (!tbl
->is_ht40
&& !(ht_cap
->cap
&
1867 IEEE80211_HT_CAP_SGI_20
))
1869 if (tbl
->is_ht40
&& !(ht_cap
->cap
&
1870 IEEE80211_HT_CAP_SGI_40
))
1873 IWL_DEBUG_RATE(priv
, "LQ: MIMO2 toggle SGI/NGI\n");
1875 /* Set up new search table for MIMO2 */
1876 memcpy(search_tbl
, tbl
, sz
);
1877 search_tbl
->is_SGI
= !tbl
->is_SGI
;
1878 rs_set_expected_tpt_table(lq_sta
, search_tbl
);
1880 * If active table already uses the fastest possible
1881 * modulation (dual stream with short guard interval),
1882 * and it's working well, there's no need to look
1883 * for a better type of modulation!
1886 s32 tpt
= lq_sta
->last_tpt
/ 100;
1887 if (tpt
>= search_tbl
->expected_tpt
[index
])
1890 search_tbl
->current_rate
=
1891 rate_n_flags_from_tbl(priv
, search_tbl
,
1893 update_search_tbl_counter
= 1;
1896 case IWL_MIMO2_SWITCH_MIMO3_ABC
:
1897 IWL_DEBUG_RATE(priv
, "LQ: MIMO2 switch to MIMO3\n");
1898 memcpy(search_tbl
, tbl
, sz
);
1899 search_tbl
->is_SGI
= 0;
1900 search_tbl
->ant_type
= ANT_ABC
;
1902 if (!rs_is_valid_ant(valid_tx_ant
, search_tbl
->ant_type
))
1905 ret
= rs_switch_to_mimo3(priv
, lq_sta
, conf
, sta
,
1913 if (tbl
->action
> IWL_MIMO2_SWITCH_MIMO3_ABC
)
1914 tbl
->action
= IWL_MIMO2_SWITCH_ANTENNA1
;
1916 if (tbl
->action
== start_action
)
1919 search_tbl
->lq_type
= LQ_NONE
;
1922 lq_sta
->search_better_tbl
= 1;
1924 if (tbl
->action
> IWL_MIMO2_SWITCH_MIMO3_ABC
)
1925 tbl
->action
= IWL_MIMO2_SWITCH_ANTENNA1
;
1926 if (update_search_tbl_counter
)
1927 search_tbl
->action
= tbl
->action
;
1934 * Try to switch to new modulation mode from MIMO3
1936 static int rs_move_mimo3_to_other(struct iwl_priv
*priv
,
1937 struct iwl_lq_sta
*lq_sta
,
1938 struct ieee80211_conf
*conf
,
1939 struct ieee80211_sta
*sta
, int index
)
1941 s8 is_green
= lq_sta
->is_green
;
1942 struct iwl_scale_tbl_info
*tbl
= &(lq_sta
->lq_info
[lq_sta
->active_tbl
]);
1943 struct iwl_scale_tbl_info
*search_tbl
=
1944 &(lq_sta
->lq_info
[(1 - lq_sta
->active_tbl
)]);
1945 struct iwl_rate_scale_data
*window
= &(tbl
->win
[index
]);
1946 struct ieee80211_sta_ht_cap
*ht_cap
= &sta
->ht_cap
;
1947 u32 sz
= (sizeof(struct iwl_scale_tbl_info
) -
1948 (sizeof(struct iwl_rate_scale_data
) * IWL_RATE_COUNT
));
1950 u8 valid_tx_ant
= priv
->nvm_data
->valid_tx_ant
;
1951 u8 tx_chains_num
= priv
->hw_params
.tx_chains_num
;
1953 u8 update_search_tbl_counter
= 0;
1955 switch (priv
->bt_traffic_load
) {
1956 case IWL_BT_COEX_TRAFFIC_LOAD_NONE
:
1959 case IWL_BT_COEX_TRAFFIC_LOAD_HIGH
:
1960 case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS
:
1961 /* avoid antenna B and MIMO */
1962 if (tbl
->action
!= IWL_MIMO3_SWITCH_SISO_A
)
1963 tbl
->action
= IWL_MIMO3_SWITCH_SISO_A
;
1965 case IWL_BT_COEX_TRAFFIC_LOAD_LOW
:
1966 /* avoid antenna B unless MIMO */
1967 if (tbl
->action
== IWL_MIMO3_SWITCH_SISO_B
||
1968 tbl
->action
== IWL_MIMO3_SWITCH_SISO_C
)
1969 tbl
->action
= IWL_MIMO3_SWITCH_SISO_A
;
1972 IWL_ERR(priv
, "Invalid BT load %d", priv
->bt_traffic_load
);
1976 if ((iwl_tx_ant_restriction(priv
) == IWL_ANT_OK_SINGLE
) &&
1977 (tbl
->action
< IWL_MIMO3_SWITCH_SISO_A
||
1978 tbl
->action
> IWL_MIMO3_SWITCH_SISO_C
)) {
1979 /* switch in SISO */
1980 tbl
->action
= IWL_MIMO3_SWITCH_SISO_A
;
1983 /* configure as 1x1 if bt full concurrency */
1984 if (priv
->bt_full_concurrent
&&
1985 (tbl
->action
< IWL_MIMO3_SWITCH_SISO_A
||
1986 tbl
->action
> IWL_MIMO3_SWITCH_SISO_C
))
1987 tbl
->action
= IWL_MIMO3_SWITCH_SISO_A
;
1989 start_action
= tbl
->action
;
1991 lq_sta
->action_counter
++;
1992 switch (tbl
->action
) {
1993 case IWL_MIMO3_SWITCH_ANTENNA1
:
1994 case IWL_MIMO3_SWITCH_ANTENNA2
:
1995 IWL_DEBUG_RATE(priv
, "LQ: MIMO3 toggle Antennas\n");
1997 if (tx_chains_num
<= 3)
2000 if (window
->success_ratio
>= IWL_RS_GOOD_RATIO
)
2003 memcpy(search_tbl
, tbl
, sz
);
2004 if (rs_toggle_antenna(valid_tx_ant
,
2005 &search_tbl
->current_rate
, search_tbl
))
2008 case IWL_MIMO3_SWITCH_SISO_A
:
2009 case IWL_MIMO3_SWITCH_SISO_B
:
2010 case IWL_MIMO3_SWITCH_SISO_C
:
2011 IWL_DEBUG_RATE(priv
, "LQ: MIMO3 switch to SISO\n");
2013 /* Set up new search table for SISO */
2014 memcpy(search_tbl
, tbl
, sz
);
2016 if (tbl
->action
== IWL_MIMO3_SWITCH_SISO_A
)
2017 search_tbl
->ant_type
= ANT_A
;
2018 else if (tbl
->action
== IWL_MIMO3_SWITCH_SISO_B
)
2019 search_tbl
->ant_type
= ANT_B
;
2021 search_tbl
->ant_type
= ANT_C
;
2023 if (!rs_is_valid_ant(valid_tx_ant
, search_tbl
->ant_type
))
2026 ret
= rs_switch_to_siso(priv
, lq_sta
, conf
, sta
,
2033 case IWL_MIMO3_SWITCH_MIMO2_AB
:
2034 case IWL_MIMO3_SWITCH_MIMO2_AC
:
2035 case IWL_MIMO3_SWITCH_MIMO2_BC
:
2036 IWL_DEBUG_RATE(priv
, "LQ: MIMO3 switch to MIMO2\n");
2038 memcpy(search_tbl
, tbl
, sz
);
2039 search_tbl
->is_SGI
= 0;
2040 if (tbl
->action
== IWL_MIMO3_SWITCH_MIMO2_AB
)
2041 search_tbl
->ant_type
= ANT_AB
;
2042 else if (tbl
->action
== IWL_MIMO3_SWITCH_MIMO2_AC
)
2043 search_tbl
->ant_type
= ANT_AC
;
2045 search_tbl
->ant_type
= ANT_BC
;
2047 if (!rs_is_valid_ant(valid_tx_ant
, search_tbl
->ant_type
))
2050 ret
= rs_switch_to_mimo2(priv
, lq_sta
, conf
, sta
,
2057 case IWL_MIMO3_SWITCH_GI
:
2058 if (!tbl
->is_ht40
&& !(ht_cap
->cap
&
2059 IEEE80211_HT_CAP_SGI_20
))
2061 if (tbl
->is_ht40
&& !(ht_cap
->cap
&
2062 IEEE80211_HT_CAP_SGI_40
))
2065 IWL_DEBUG_RATE(priv
, "LQ: MIMO3 toggle SGI/NGI\n");
2067 /* Set up new search table for MIMO */
2068 memcpy(search_tbl
, tbl
, sz
);
2069 search_tbl
->is_SGI
= !tbl
->is_SGI
;
2070 rs_set_expected_tpt_table(lq_sta
, search_tbl
);
2072 * If active table already uses the fastest possible
2073 * modulation (dual stream with short guard interval),
2074 * and it's working well, there's no need to look
2075 * for a better type of modulation!
2078 s32 tpt
= lq_sta
->last_tpt
/ 100;
2079 if (tpt
>= search_tbl
->expected_tpt
[index
])
2082 search_tbl
->current_rate
=
2083 rate_n_flags_from_tbl(priv
, search_tbl
,
2085 update_search_tbl_counter
= 1;
2089 if (tbl
->action
> IWL_MIMO3_SWITCH_GI
)
2090 tbl
->action
= IWL_MIMO3_SWITCH_ANTENNA1
;
2092 if (tbl
->action
== start_action
)
2095 search_tbl
->lq_type
= LQ_NONE
;
2098 lq_sta
->search_better_tbl
= 1;
2100 if (tbl
->action
> IWL_MIMO3_SWITCH_GI
)
2101 tbl
->action
= IWL_MIMO3_SWITCH_ANTENNA1
;
2102 if (update_search_tbl_counter
)
2103 search_tbl
->action
= tbl
->action
;
2110 * Check whether we should continue using same modulation mode, or
2111 * begin search for a new mode, based on:
2112 * 1) # tx successes or failures while using this mode
2113 * 2) # times calling this function
2114 * 3) elapsed time in this mode (not used, for now)
2116 static void rs_stay_in_table(struct iwl_lq_sta
*lq_sta
, bool force_search
)
2118 struct iwl_scale_tbl_info
*tbl
;
2121 int flush_interval_passed
= 0;
2122 struct iwl_priv
*priv
;
2125 active_tbl
= lq_sta
->active_tbl
;
2127 tbl
= &(lq_sta
->lq_info
[active_tbl
]);
2129 /* If we've been disallowing search, see if we should now allow it */
2130 if (lq_sta
->stay_in_tbl
) {
2132 /* Elapsed time using current modulation mode */
2133 if (lq_sta
->flush_timer
)
2134 flush_interval_passed
=
2136 (unsigned long)(lq_sta
->flush_timer
+
2137 IWL_RATE_SCALE_FLUSH_INTVL
));
2140 * Check if we should allow search for new modulation mode.
2141 * If many frames have failed or succeeded, or we've used
2142 * this same modulation for a long time, allow search, and
2143 * reset history stats that keep track of whether we should
2144 * allow a new search. Also (below) reset all bitmaps and
2145 * stats in active history.
2148 (lq_sta
->total_failed
> lq_sta
->max_failure_limit
) ||
2149 (lq_sta
->total_success
> lq_sta
->max_success_limit
) ||
2150 ((!lq_sta
->search_better_tbl
) && (lq_sta
->flush_timer
)
2151 && (flush_interval_passed
))) {
2152 IWL_DEBUG_RATE(priv
, "LQ: stay is expired %d %d %d\n",
2153 lq_sta
->total_failed
,
2154 lq_sta
->total_success
,
2155 flush_interval_passed
);
2157 /* Allow search for new mode */
2158 lq_sta
->stay_in_tbl
= 0; /* only place reset */
2159 lq_sta
->total_failed
= 0;
2160 lq_sta
->total_success
= 0;
2161 lq_sta
->flush_timer
= 0;
2164 * Else if we've used this modulation mode enough repetitions
2165 * (regardless of elapsed time or success/failure), reset
2166 * history bitmaps and rate-specific stats for all rates in
2170 lq_sta
->table_count
++;
2171 if (lq_sta
->table_count
>=
2172 lq_sta
->table_count_limit
) {
2173 lq_sta
->table_count
= 0;
2175 IWL_DEBUG_RATE(priv
, "LQ: stay in table clear win\n");
2176 for (i
= 0; i
< IWL_RATE_COUNT
; i
++)
2177 rs_rate_scale_clear_window(
2182 /* If transitioning to allow "search", reset all history
2183 * bitmaps and stats in active table (this will become the new
2184 * "search" table). */
2185 if (!lq_sta
->stay_in_tbl
) {
2186 for (i
= 0; i
< IWL_RATE_COUNT
; i
++)
2187 rs_rate_scale_clear_window(&(tbl
->win
[i
]));
2193 * setup rate table in uCode
2195 static void rs_update_rate_tbl(struct iwl_priv
*priv
,
2196 struct iwl_rxon_context
*ctx
,
2197 struct iwl_lq_sta
*lq_sta
,
2198 struct iwl_scale_tbl_info
*tbl
,
2199 int index
, u8 is_green
)
2203 /* Update uCode's rate table. */
2204 rate
= rate_n_flags_from_tbl(priv
, tbl
, index
, is_green
);
2205 rs_fill_link_cmd(priv
, lq_sta
, rate
);
2206 iwl_send_lq_cmd(priv
, ctx
, &lq_sta
->lq
, CMD_ASYNC
, false);
2210 * Do rate scaling and search for new modulation mode.
2212 static void rs_rate_scale_perform(struct iwl_priv
*priv
,
2213 struct sk_buff
*skb
,
2214 struct ieee80211_sta
*sta
,
2215 struct iwl_lq_sta
*lq_sta
)
2217 struct ieee80211_hw
*hw
= priv
->hw
;
2218 struct ieee80211_conf
*conf
= &hw
->conf
;
2219 struct ieee80211_tx_info
*info
= IEEE80211_SKB_CB(skb
);
2220 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*)skb
->data
;
2221 int low
= IWL_RATE_INVALID
;
2222 int high
= IWL_RATE_INVALID
;
2225 struct iwl_rate_scale_data
*window
= NULL
;
2226 int current_tpt
= IWL_INVALID_VALUE
;
2227 int low_tpt
= IWL_INVALID_VALUE
;
2228 int high_tpt
= IWL_INVALID_VALUE
;
2230 s8 scale_action
= 0;
2233 struct iwl_scale_tbl_info
*tbl
, *tbl1
;
2234 u16 rate_scale_index_msk
= 0;
2240 u8 tid
= IWL_MAX_TID_COUNT
;
2241 struct iwl_tid_data
*tid_data
;
2242 struct iwl_station_priv
*sta_priv
= (void *)sta
->drv_priv
;
2243 struct iwl_rxon_context
*ctx
= sta_priv
->ctx
;
2245 IWL_DEBUG_RATE(priv
, "rate scale calculate new rate for skb\n");
2247 /* Send management frames and NO_ACK data using lowest rate. */
2248 /* TODO: this could probably be improved.. */
2249 if (!ieee80211_is_data(hdr
->frame_control
) ||
2250 info
->flags
& IEEE80211_TX_CTL_NO_ACK
)
2253 lq_sta
->supp_rates
= sta
->supp_rates
[lq_sta
->band
];
2255 tid
= rs_tl_add_packet(lq_sta
, hdr
);
2256 if ((tid
!= IWL_MAX_TID_COUNT
) &&
2257 (lq_sta
->tx_agg_tid_en
& (1 << tid
))) {
2258 tid_data
= &priv
->tid_data
[lq_sta
->lq
.sta_id
][tid
];
2259 if (tid_data
->agg
.state
== IWL_AGG_OFF
)
2267 * Select rate-scale / modulation-mode table to work with in
2268 * the rest of this function: "search" if searching for better
2269 * modulation mode, or "active" if doing rate scaling within a mode.
2271 if (!lq_sta
->search_better_tbl
)
2272 active_tbl
= lq_sta
->active_tbl
;
2274 active_tbl
= 1 - lq_sta
->active_tbl
;
2276 tbl
= &(lq_sta
->lq_info
[active_tbl
]);
2277 if (is_legacy(tbl
->lq_type
))
2278 lq_sta
->is_green
= 0;
2280 lq_sta
->is_green
= rs_use_green(sta
);
2281 is_green
= lq_sta
->is_green
;
2283 /* current tx rate */
2284 index
= lq_sta
->last_txrate_idx
;
2286 IWL_DEBUG_RATE(priv
, "Rate scale index %d for type %d\n", index
,
2289 /* rates available for this association, and for modulation mode */
2290 rate_mask
= rs_get_supported_rates(lq_sta
, hdr
, tbl
->lq_type
);
2292 IWL_DEBUG_RATE(priv
, "mask 0x%04X\n", rate_mask
);
2294 /* mask with station rate restriction */
2295 if (is_legacy(tbl
->lq_type
)) {
2296 if (lq_sta
->band
== IEEE80211_BAND_5GHZ
)
2297 /* supp_rates has no CCK bits in A mode */
2298 rate_scale_index_msk
= (u16
) (rate_mask
&
2299 (lq_sta
->supp_rates
<< IWL_FIRST_OFDM_RATE
));
2301 rate_scale_index_msk
= (u16
) (rate_mask
&
2302 lq_sta
->supp_rates
);
2305 rate_scale_index_msk
= rate_mask
;
2307 if (!rate_scale_index_msk
)
2308 rate_scale_index_msk
= rate_mask
;
2310 if (!((1 << index
) & rate_scale_index_msk
)) {
2311 IWL_ERR(priv
, "Current Rate is not valid\n");
2312 if (lq_sta
->search_better_tbl
) {
2313 /* revert to active table if search table is not valid*/
2314 tbl
->lq_type
= LQ_NONE
;
2315 lq_sta
->search_better_tbl
= 0;
2316 tbl
= &(lq_sta
->lq_info
[lq_sta
->active_tbl
]);
2317 /* get "active" rate info */
2318 index
= iwl_hwrate_to_plcp_idx(tbl
->current_rate
);
2319 rs_update_rate_tbl(priv
, ctx
, lq_sta
, tbl
,
2325 /* Get expected throughput table and history window for current rate */
2326 if (!tbl
->expected_tpt
) {
2327 IWL_ERR(priv
, "tbl->expected_tpt is NULL\n");
2331 /* force user max rate if set by user */
2332 if ((lq_sta
->max_rate_idx
!= -1) &&
2333 (lq_sta
->max_rate_idx
< index
)) {
2334 index
= lq_sta
->max_rate_idx
;
2336 window
= &(tbl
->win
[index
]);
2340 window
= &(tbl
->win
[index
]);
2343 * If there is not enough history to calculate actual average
2344 * throughput, keep analyzing results of more tx frames, without
2345 * changing rate or mode (bypass most of the rest of this function).
2346 * Set up new rate table in uCode only if old rate is not supported
2347 * in current association (use new rate found above).
2349 fail_count
= window
->counter
- window
->success_counter
;
2350 if ((fail_count
< IWL_RATE_MIN_FAILURE_TH
) &&
2351 (window
->success_counter
< IWL_RATE_MIN_SUCCESS_TH
)) {
2352 IWL_DEBUG_RATE(priv
, "LQ: still below TH. succ=%d total=%d "
2354 window
->success_counter
, window
->counter
, index
);
2356 /* Can't calculate this yet; not enough history */
2357 window
->average_tpt
= IWL_INVALID_VALUE
;
2359 /* Should we stay with this modulation mode,
2360 * or search for a new one? */
2361 rs_stay_in_table(lq_sta
, false);
2365 /* Else we have enough samples; calculate estimate of
2366 * actual average throughput */
2367 if (window
->average_tpt
!= ((window
->success_ratio
*
2368 tbl
->expected_tpt
[index
] + 64) / 128)) {
2369 IWL_ERR(priv
, "expected_tpt should have been calculated by now\n");
2370 window
->average_tpt
= ((window
->success_ratio
*
2371 tbl
->expected_tpt
[index
] + 64) / 128);
2374 /* If we are searching for better modulation mode, check success. */
2375 if (lq_sta
->search_better_tbl
&&
2376 (iwl_tx_ant_restriction(priv
) == IWL_ANT_OK_MULTI
)) {
2377 /* If good success, continue using the "search" mode;
2378 * no need to send new link quality command, since we're
2379 * continuing to use the setup that we've been trying. */
2380 if (window
->average_tpt
> lq_sta
->last_tpt
) {
2382 IWL_DEBUG_RATE(priv
, "LQ: SWITCHING TO NEW TABLE "
2383 "suc=%d cur-tpt=%d old-tpt=%d\n",
2384 window
->success_ratio
,
2385 window
->average_tpt
,
2388 if (!is_legacy(tbl
->lq_type
))
2389 lq_sta
->enable_counter
= 1;
2391 /* Swap tables; "search" becomes "active" */
2392 lq_sta
->active_tbl
= active_tbl
;
2393 current_tpt
= window
->average_tpt
;
2395 /* Else poor success; go back to mode in "active" table */
2398 IWL_DEBUG_RATE(priv
, "LQ: GOING BACK TO THE OLD TABLE "
2399 "suc=%d cur-tpt=%d old-tpt=%d\n",
2400 window
->success_ratio
,
2401 window
->average_tpt
,
2404 /* Nullify "search" table */
2405 tbl
->lq_type
= LQ_NONE
;
2407 /* Revert to "active" table */
2408 active_tbl
= lq_sta
->active_tbl
;
2409 tbl
= &(lq_sta
->lq_info
[active_tbl
]);
2411 /* Revert to "active" rate and throughput info */
2412 index
= iwl_hwrate_to_plcp_idx(tbl
->current_rate
);
2413 current_tpt
= lq_sta
->last_tpt
;
2415 /* Need to set up a new rate table in uCode */
2419 /* Either way, we've made a decision; modulation mode
2420 * search is done, allow rate adjustment next time. */
2421 lq_sta
->search_better_tbl
= 0;
2422 done_search
= 1; /* Don't switch modes below! */
2426 /* (Else) not in search of better modulation mode, try for better
2427 * starting rate, while staying in this mode. */
2428 high_low
= rs_get_adjacent_rate(priv
, index
, rate_scale_index_msk
,
2430 low
= high_low
& 0xff;
2431 high
= (high_low
>> 8) & 0xff;
2433 /* If user set max rate, dont allow higher than user constrain */
2434 if ((lq_sta
->max_rate_idx
!= -1) &&
2435 (lq_sta
->max_rate_idx
< high
))
2436 high
= IWL_RATE_INVALID
;
2438 sr
= window
->success_ratio
;
2440 /* Collect measured throughputs for current and adjacent rates */
2441 current_tpt
= window
->average_tpt
;
2442 if (low
!= IWL_RATE_INVALID
)
2443 low_tpt
= tbl
->win
[low
].average_tpt
;
2444 if (high
!= IWL_RATE_INVALID
)
2445 high_tpt
= tbl
->win
[high
].average_tpt
;
2449 /* Too many failures, decrease rate */
2450 if ((sr
<= IWL_RATE_DECREASE_TH
) || (current_tpt
== 0)) {
2451 IWL_DEBUG_RATE(priv
, "decrease rate because of low success_ratio\n");
2454 /* No throughput measured yet for adjacent rates; try increase. */
2455 } else if ((low_tpt
== IWL_INVALID_VALUE
) &&
2456 (high_tpt
== IWL_INVALID_VALUE
)) {
2458 if (high
!= IWL_RATE_INVALID
&& sr
>= IWL_RATE_INCREASE_TH
)
2460 else if (low
!= IWL_RATE_INVALID
)
2464 /* Both adjacent throughputs are measured, but neither one has better
2465 * throughput; we're using the best rate, don't change it! */
2466 else if ((low_tpt
!= IWL_INVALID_VALUE
) &&
2467 (high_tpt
!= IWL_INVALID_VALUE
) &&
2468 (low_tpt
< current_tpt
) &&
2469 (high_tpt
< current_tpt
))
2472 /* At least one adjacent rate's throughput is measured,
2473 * and may have better performance. */
2475 /* Higher adjacent rate's throughput is measured */
2476 if (high_tpt
!= IWL_INVALID_VALUE
) {
2477 /* Higher rate has better throughput */
2478 if (high_tpt
> current_tpt
&&
2479 sr
>= IWL_RATE_INCREASE_TH
) {
2485 /* Lower adjacent rate's throughput is measured */
2486 } else if (low_tpt
!= IWL_INVALID_VALUE
) {
2487 /* Lower rate has better throughput */
2488 if (low_tpt
> current_tpt
) {
2489 IWL_DEBUG_RATE(priv
,
2490 "decrease rate because of low tpt\n");
2492 } else if (sr
>= IWL_RATE_INCREASE_TH
) {
2498 /* Sanity check; asked for decrease, but success rate or throughput
2499 * has been good at old rate. Don't change it. */
2500 if ((scale_action
== -1) && (low
!= IWL_RATE_INVALID
) &&
2501 ((sr
> IWL_RATE_HIGH_TH
) ||
2502 (current_tpt
> (100 * tbl
->expected_tpt
[low
]))))
2504 if (!iwl_ht_enabled(priv
) && !is_legacy(tbl
->lq_type
))
2506 if (iwl_tx_ant_restriction(priv
) != IWL_ANT_OK_MULTI
&&
2507 (is_mimo2(tbl
->lq_type
) || is_mimo3(tbl
->lq_type
)))
2510 if ((priv
->bt_traffic_load
>= IWL_BT_COEX_TRAFFIC_LOAD_HIGH
) &&
2511 (is_mimo2(tbl
->lq_type
) || is_mimo3(tbl
->lq_type
))) {
2512 if (lq_sta
->last_bt_traffic
> priv
->bt_traffic_load
) {
2514 * don't set scale_action, don't want to scale up if
2515 * the rate scale doesn't otherwise think that is a
2518 } else if (lq_sta
->last_bt_traffic
<= priv
->bt_traffic_load
) {
2522 lq_sta
->last_bt_traffic
= priv
->bt_traffic_load
;
2524 if ((priv
->bt_traffic_load
>= IWL_BT_COEX_TRAFFIC_LOAD_HIGH
) &&
2525 (is_mimo2(tbl
->lq_type
) || is_mimo3(tbl
->lq_type
))) {
2526 /* search for a new modulation */
2527 rs_stay_in_table(lq_sta
, true);
2531 switch (scale_action
) {
2533 /* Decrease starting rate, update uCode's rate table */
2534 if (low
!= IWL_RATE_INVALID
) {
2541 /* Increase starting rate, update uCode's rate table */
2542 if (high
!= IWL_RATE_INVALID
) {
2554 IWL_DEBUG_RATE(priv
, "choose rate scale index %d action %d low %d "
2555 "high %d type %d\n",
2556 index
, scale_action
, low
, high
, tbl
->lq_type
);
2559 /* Replace uCode's rate table for the destination station. */
2561 rs_update_rate_tbl(priv
, ctx
, lq_sta
, tbl
, index
, is_green
);
2563 if (iwl_tx_ant_restriction(priv
) == IWL_ANT_OK_MULTI
) {
2564 /* Should we stay with this modulation mode,
2565 * or search for a new one? */
2566 rs_stay_in_table(lq_sta
, false);
2569 * Search for new modulation mode if we're:
2570 * 1) Not changing rates right now
2571 * 2) Not just finishing up a search
2572 * 3) Allowing a new search
2574 if (!update_lq
&& !done_search
&& !lq_sta
->stay_in_tbl
&& window
->counter
) {
2575 /* Save current throughput to compare with "search" throughput*/
2576 lq_sta
->last_tpt
= current_tpt
;
2578 /* Select a new "search" modulation mode to try.
2579 * If one is found, set up the new "search" table. */
2580 if (is_legacy(tbl
->lq_type
))
2581 rs_move_legacy_other(priv
, lq_sta
, conf
, sta
, index
);
2582 else if (is_siso(tbl
->lq_type
))
2583 rs_move_siso_to_other(priv
, lq_sta
, conf
, sta
, index
);
2584 else if (is_mimo2(tbl
->lq_type
))
2585 rs_move_mimo2_to_other(priv
, lq_sta
, conf
, sta
, index
);
2587 rs_move_mimo3_to_other(priv
, lq_sta
, conf
, sta
, index
);
2589 /* If new "search" mode was selected, set up in uCode table */
2590 if (lq_sta
->search_better_tbl
) {
2591 /* Access the "search" table, clear its history. */
2592 tbl
= &(lq_sta
->lq_info
[(1 - lq_sta
->active_tbl
)]);
2593 for (i
= 0; i
< IWL_RATE_COUNT
; i
++)
2594 rs_rate_scale_clear_window(&(tbl
->win
[i
]));
2596 /* Use new "search" start rate */
2597 index
= iwl_hwrate_to_plcp_idx(tbl
->current_rate
);
2599 IWL_DEBUG_RATE(priv
, "Switch current mcs: %X index: %d\n",
2600 tbl
->current_rate
, index
);
2601 rs_fill_link_cmd(priv
, lq_sta
, tbl
->current_rate
);
2602 iwl_send_lq_cmd(priv
, ctx
, &lq_sta
->lq
, CMD_ASYNC
, false);
2607 if (done_search
&& !lq_sta
->stay_in_tbl
) {
2608 /* If the "active" (non-search) mode was legacy,
2609 * and we've tried switching antennas,
2610 * but we haven't been able to try HT modes (not available),
2611 * stay with best antenna legacy modulation for a while
2612 * before next round of mode comparisons. */
2613 tbl1
= &(lq_sta
->lq_info
[lq_sta
->active_tbl
]);
2614 if (is_legacy(tbl1
->lq_type
) && !conf_is_ht(conf
) &&
2615 lq_sta
->action_counter
> tbl1
->max_search
) {
2616 IWL_DEBUG_RATE(priv
, "LQ: STAY in legacy table\n");
2617 rs_set_stay_in_table(priv
, 1, lq_sta
);
2620 /* If we're in an HT mode, and all 3 mode switch actions
2621 * have been tried and compared, stay in this best modulation
2622 * mode for a while before next round of mode comparisons. */
2623 if (lq_sta
->enable_counter
&&
2624 (lq_sta
->action_counter
>= tbl1
->max_search
) &&
2625 iwl_ht_enabled(priv
)) {
2626 if ((lq_sta
->last_tpt
> IWL_AGG_TPT_THREHOLD
) &&
2627 (lq_sta
->tx_agg_tid_en
& (1 << tid
)) &&
2628 (tid
!= IWL_MAX_TID_COUNT
)) {
2629 u8 sta_id
= lq_sta
->lq
.sta_id
;
2630 tid_data
= &priv
->tid_data
[sta_id
][tid
];
2631 if (tid_data
->agg
.state
== IWL_AGG_OFF
) {
2632 IWL_DEBUG_RATE(priv
,
2633 "try to aggregate tid %d\n",
2635 rs_tl_turn_on_agg(priv
, tid
,
2639 rs_set_stay_in_table(priv
, 0, lq_sta
);
2644 tbl
->current_rate
= rate_n_flags_from_tbl(priv
, tbl
, index
, is_green
);
2645 lq_sta
->last_txrate_idx
= index
;
2649 * rs_initialize_lq - Initialize a station's hardware rate table
2651 * The uCode's station table contains a table of fallback rates
2652 * for automatic fallback during transmission.
2654 * NOTE: This sets up a default set of values. These will be replaced later
2655 * if the driver's iwl-agn-rs rate scaling algorithm is used, instead of
2658 * NOTE: Run REPLY_ADD_STA command to set up station table entry, before
2659 * calling this function (which runs REPLY_TX_LINK_QUALITY_CMD,
2660 * which requires station table entry to exist).
2662 static void rs_initialize_lq(struct iwl_priv
*priv
,
2663 struct ieee80211_sta
*sta
,
2664 struct iwl_lq_sta
*lq_sta
)
2666 struct iwl_scale_tbl_info
*tbl
;
2670 u8 use_green
= rs_use_green(sta
);
2673 struct iwl_station_priv
*sta_priv
;
2674 struct iwl_rxon_context
*ctx
;
2676 if (!sta
|| !lq_sta
)
2679 sta_priv
= (void *)sta
->drv_priv
;
2680 ctx
= sta_priv
->ctx
;
2682 i
= lq_sta
->last_txrate_idx
;
2684 valid_tx_ant
= priv
->nvm_data
->valid_tx_ant
;
2686 if (!lq_sta
->search_better_tbl
)
2687 active_tbl
= lq_sta
->active_tbl
;
2689 active_tbl
= 1 - lq_sta
->active_tbl
;
2691 tbl
= &(lq_sta
->lq_info
[active_tbl
]);
2693 if ((i
< 0) || (i
>= IWL_RATE_COUNT
))
2696 rate
= iwl_rates
[i
].plcp
;
2697 tbl
->ant_type
= first_antenna(valid_tx_ant
);
2698 rate
|= tbl
->ant_type
<< RATE_MCS_ANT_POS
;
2700 if (i
>= IWL_FIRST_CCK_RATE
&& i
<= IWL_LAST_CCK_RATE
)
2701 rate
|= RATE_MCS_CCK_MSK
;
2703 rs_get_tbl_info_from_mcs(rate
, priv
->band
, tbl
, &rate_idx
);
2704 if (!rs_is_valid_ant(valid_tx_ant
, tbl
->ant_type
))
2705 rs_toggle_antenna(valid_tx_ant
, &rate
, tbl
);
2707 rate
= rate_n_flags_from_tbl(priv
, tbl
, rate_idx
, use_green
);
2708 tbl
->current_rate
= rate
;
2709 rs_set_expected_tpt_table(lq_sta
, tbl
);
2710 rs_fill_link_cmd(NULL
, lq_sta
, rate
);
2711 priv
->stations
[lq_sta
->lq
.sta_id
].lq
= &lq_sta
->lq
;
2712 iwl_send_lq_cmd(priv
, ctx
, &lq_sta
->lq
, CMD_SYNC
, true);
2715 static void rs_get_rate(void *priv_r
, struct ieee80211_sta
*sta
, void *priv_sta
,
2716 struct ieee80211_tx_rate_control
*txrc
)
2719 struct sk_buff
*skb
= txrc
->skb
;
2720 struct ieee80211_supported_band
*sband
= txrc
->sband
;
2721 struct iwl_op_mode
*op_mode __maybe_unused
=
2722 (struct iwl_op_mode
*)priv_r
;
2723 struct iwl_priv
*priv __maybe_unused
= IWL_OP_MODE_GET_DVM(op_mode
);
2724 struct ieee80211_tx_info
*info
= IEEE80211_SKB_CB(skb
);
2725 struct iwl_lq_sta
*lq_sta
= priv_sta
;
2728 IWL_DEBUG_RATE_LIMIT(priv
, "rate scale calculate new rate for skb\n");
2730 /* Get max rate if user set max rate */
2732 lq_sta
->max_rate_idx
= txrc
->max_rate_idx
;
2733 if ((sband
->band
== IEEE80211_BAND_5GHZ
) &&
2734 (lq_sta
->max_rate_idx
!= -1))
2735 lq_sta
->max_rate_idx
+= IWL_FIRST_OFDM_RATE
;
2736 if ((lq_sta
->max_rate_idx
< 0) ||
2737 (lq_sta
->max_rate_idx
>= IWL_RATE_COUNT
))
2738 lq_sta
->max_rate_idx
= -1;
2741 /* Treat uninitialized rate scaling data same as non-existing. */
2742 if (lq_sta
&& !lq_sta
->drv
) {
2743 IWL_DEBUG_RATE(priv
, "Rate scaling not initialized yet.\n");
2747 /* Send management frames and NO_ACK data using lowest rate. */
2748 if (rate_control_send_low(sta
, priv_sta
, txrc
))
2751 rate_idx
= lq_sta
->last_txrate_idx
;
2753 if (lq_sta
->last_rate_n_flags
& RATE_MCS_HT_MSK
) {
2754 rate_idx
-= IWL_FIRST_OFDM_RATE
;
2755 /* 6M and 9M shared same MCS index */
2756 rate_idx
= (rate_idx
> 0) ? (rate_idx
- 1) : 0;
2757 if (rs_extract_rate(lq_sta
->last_rate_n_flags
) >=
2758 IWL_RATE_MIMO3_6M_PLCP
)
2759 rate_idx
= rate_idx
+ (2 * MCS_INDEX_PER_STREAM
);
2760 else if (rs_extract_rate(lq_sta
->last_rate_n_flags
) >=
2761 IWL_RATE_MIMO2_6M_PLCP
)
2762 rate_idx
= rate_idx
+ MCS_INDEX_PER_STREAM
;
2763 info
->control
.rates
[0].flags
= IEEE80211_TX_RC_MCS
;
2764 if (lq_sta
->last_rate_n_flags
& RATE_MCS_SGI_MSK
)
2765 info
->control
.rates
[0].flags
|= IEEE80211_TX_RC_SHORT_GI
;
2766 if (lq_sta
->last_rate_n_flags
& RATE_MCS_DUP_MSK
)
2767 info
->control
.rates
[0].flags
|= IEEE80211_TX_RC_DUP_DATA
;
2768 if (lq_sta
->last_rate_n_flags
& RATE_MCS_HT40_MSK
)
2769 info
->control
.rates
[0].flags
|= IEEE80211_TX_RC_40_MHZ_WIDTH
;
2770 if (lq_sta
->last_rate_n_flags
& RATE_MCS_GF_MSK
)
2771 info
->control
.rates
[0].flags
|= IEEE80211_TX_RC_GREEN_FIELD
;
2773 /* Check for invalid rates */
2774 if ((rate_idx
< 0) || (rate_idx
>= IWL_RATE_COUNT_LEGACY
) ||
2775 ((sband
->band
== IEEE80211_BAND_5GHZ
) &&
2776 (rate_idx
< IWL_FIRST_OFDM_RATE
)))
2777 rate_idx
= rate_lowest_index(sband
, sta
);
2778 /* On valid 5 GHz rate, adjust index */
2779 else if (sband
->band
== IEEE80211_BAND_5GHZ
)
2780 rate_idx
-= IWL_FIRST_OFDM_RATE
;
2781 info
->control
.rates
[0].flags
= 0;
2783 info
->control
.rates
[0].idx
= rate_idx
;
2784 info
->control
.rates
[0].count
= 1;
2787 static void *rs_alloc_sta(void *priv_rate
, struct ieee80211_sta
*sta
,
2790 struct iwl_station_priv
*sta_priv
= (struct iwl_station_priv
*) sta
->drv_priv
;
2791 struct iwl_op_mode
*op_mode __maybe_unused
=
2792 (struct iwl_op_mode
*)priv_rate
;
2793 struct iwl_priv
*priv __maybe_unused
= IWL_OP_MODE_GET_DVM(op_mode
);
2795 IWL_DEBUG_RATE(priv
, "create station rate scale window\n");
2797 return &sta_priv
->lq_sta
;
2801 * Called after adding a new station to initialize rate scaling
2803 void iwl_rs_rate_init(struct iwl_priv
*priv
, struct ieee80211_sta
*sta
, u8 sta_id
)
2806 struct ieee80211_hw
*hw
= priv
->hw
;
2807 struct ieee80211_conf
*conf
= &priv
->hw
->conf
;
2808 struct ieee80211_sta_ht_cap
*ht_cap
= &sta
->ht_cap
;
2809 struct iwl_station_priv
*sta_priv
;
2810 struct iwl_lq_sta
*lq_sta
;
2811 struct ieee80211_supported_band
*sband
;
2812 unsigned long supp
; /* must be unsigned long for for_each_set_bit */
2814 sta_priv
= (struct iwl_station_priv
*) sta
->drv_priv
;
2815 lq_sta
= &sta_priv
->lq_sta
;
2816 sband
= hw
->wiphy
->bands
[conf
->chandef
.chan
->band
];
2819 lq_sta
->lq
.sta_id
= sta_id
;
2821 for (j
= 0; j
< LQ_SIZE
; j
++)
2822 for (i
= 0; i
< IWL_RATE_COUNT
; i
++)
2823 rs_rate_scale_clear_window(&lq_sta
->lq_info
[j
].win
[i
]);
2825 lq_sta
->flush_timer
= 0;
2826 lq_sta
->supp_rates
= sta
->supp_rates
[sband
->band
];
2828 IWL_DEBUG_RATE(priv
, "LQ: *** rate scale station global init for station %d ***\n",
2830 /* TODO: what is a good starting rate for STA? About middle? Maybe not
2831 * the lowest or the highest rate.. Could consider using RSSI from
2832 * previous packets? Need to have IEEE 802.1X auth succeed immediately
2836 lq_sta
->max_rate_idx
= -1;
2837 lq_sta
->missed_rate_counter
= IWL_MISSED_RATE_MAX
;
2838 lq_sta
->is_green
= rs_use_green(sta
);
2839 lq_sta
->band
= sband
->band
;
2841 * active legacy rates as per supported rates bitmap
2843 supp
= sta
->supp_rates
[sband
->band
];
2844 lq_sta
->active_legacy_rate
= 0;
2845 for_each_set_bit(i
, &supp
, BITS_PER_LONG
)
2846 lq_sta
->active_legacy_rate
|= BIT(sband
->bitrates
[i
].hw_value
);
2849 * active_siso_rate mask includes 9 MBits (bit 5), and CCK (bits 0-3),
2850 * supp_rates[] does not; shift to convert format, force 9 MBits off.
2852 lq_sta
->active_siso_rate
= ht_cap
->mcs
.rx_mask
[0] << 1;
2853 lq_sta
->active_siso_rate
|= ht_cap
->mcs
.rx_mask
[0] & 0x1;
2854 lq_sta
->active_siso_rate
&= ~((u16
)0x2);
2855 lq_sta
->active_siso_rate
<<= IWL_FIRST_OFDM_RATE
;
2858 lq_sta
->active_mimo2_rate
= ht_cap
->mcs
.rx_mask
[1] << 1;
2859 lq_sta
->active_mimo2_rate
|= ht_cap
->mcs
.rx_mask
[1] & 0x1;
2860 lq_sta
->active_mimo2_rate
&= ~((u16
)0x2);
2861 lq_sta
->active_mimo2_rate
<<= IWL_FIRST_OFDM_RATE
;
2863 lq_sta
->active_mimo3_rate
= ht_cap
->mcs
.rx_mask
[2] << 1;
2864 lq_sta
->active_mimo3_rate
|= ht_cap
->mcs
.rx_mask
[2] & 0x1;
2865 lq_sta
->active_mimo3_rate
&= ~((u16
)0x2);
2866 lq_sta
->active_mimo3_rate
<<= IWL_FIRST_OFDM_RATE
;
2868 IWL_DEBUG_RATE(priv
, "SISO-RATE=%X MIMO2-RATE=%X MIMO3-RATE=%X\n",
2869 lq_sta
->active_siso_rate
,
2870 lq_sta
->active_mimo2_rate
,
2871 lq_sta
->active_mimo3_rate
);
2873 /* These values will be overridden later */
2874 lq_sta
->lq
.general_params
.single_stream_ant_msk
=
2875 first_antenna(priv
->nvm_data
->valid_tx_ant
);
2876 lq_sta
->lq
.general_params
.dual_stream_ant_msk
=
2877 priv
->nvm_data
->valid_tx_ant
&
2878 ~first_antenna(priv
->nvm_data
->valid_tx_ant
);
2879 if (!lq_sta
->lq
.general_params
.dual_stream_ant_msk
) {
2880 lq_sta
->lq
.general_params
.dual_stream_ant_msk
= ANT_AB
;
2881 } else if (num_of_ant(priv
->nvm_data
->valid_tx_ant
) == 2) {
2882 lq_sta
->lq
.general_params
.dual_stream_ant_msk
=
2883 priv
->nvm_data
->valid_tx_ant
;
2886 /* as default allow aggregation for all tids */
2887 lq_sta
->tx_agg_tid_en
= IWL_AGG_ALL_TID
;
2890 /* Set last_txrate_idx to lowest rate */
2891 lq_sta
->last_txrate_idx
= rate_lowest_index(sband
, sta
);
2892 if (sband
->band
== IEEE80211_BAND_5GHZ
)
2893 lq_sta
->last_txrate_idx
+= IWL_FIRST_OFDM_RATE
;
2895 #ifdef CONFIG_MAC80211_DEBUGFS
2896 lq_sta
->dbg_fixed_rate
= 0;
2899 rs_initialize_lq(priv
, sta
, lq_sta
);
2902 static void rs_fill_link_cmd(struct iwl_priv
*priv
,
2903 struct iwl_lq_sta
*lq_sta
, u32 new_rate
)
2905 struct iwl_scale_tbl_info tbl_type
;
2908 int repeat_rate
= 0;
2909 u8 ant_toggle_cnt
= 0;
2910 u8 use_ht_possible
= 1;
2911 u8 valid_tx_ant
= 0;
2912 struct iwl_station_priv
*sta_priv
=
2913 container_of(lq_sta
, struct iwl_station_priv
, lq_sta
);
2914 struct iwl_link_quality_cmd
*lq_cmd
= &lq_sta
->lq
;
2916 /* Override starting rate (index 0) if needed for debug purposes */
2917 rs_dbgfs_set_mcs(lq_sta
, &new_rate
, index
);
2919 /* Interpret new_rate (rate_n_flags) */
2920 rs_get_tbl_info_from_mcs(new_rate
, lq_sta
->band
,
2921 &tbl_type
, &rate_idx
);
2923 if (priv
&& priv
->bt_full_concurrent
) {
2926 first_antenna(priv
->nvm_data
->valid_tx_ant
);
2929 /* How many times should we repeat the initial rate? */
2930 if (is_legacy(tbl_type
.lq_type
)) {
2932 repeat_rate
= IWL_NUMBER_TRY
;
2934 repeat_rate
= min(IWL_HT_NUMBER_TRY
,
2935 LINK_QUAL_AGG_DISABLE_START_DEF
- 1);
2938 lq_cmd
->general_params
.mimo_delimiter
=
2939 is_mimo(tbl_type
.lq_type
) ? 1 : 0;
2941 /* Fill 1st table entry (index 0) */
2942 lq_cmd
->rs_table
[index
].rate_n_flags
= cpu_to_le32(new_rate
);
2944 if (num_of_ant(tbl_type
.ant_type
) == 1) {
2945 lq_cmd
->general_params
.single_stream_ant_msk
=
2947 } else if (num_of_ant(tbl_type
.ant_type
) == 2) {
2948 lq_cmd
->general_params
.dual_stream_ant_msk
=
2950 } /* otherwise we don't modify the existing value */
2955 if (priv
->bt_full_concurrent
)
2956 valid_tx_ant
= ANT_A
;
2958 valid_tx_ant
= priv
->nvm_data
->valid_tx_ant
;
2961 /* Fill rest of rate table */
2962 while (index
< LINK_QUAL_MAX_RETRY_NUM
) {
2963 /* Repeat initial/next rate.
2964 * For legacy IWL_NUMBER_TRY == 1, this loop will not execute.
2965 * For HT IWL_HT_NUMBER_TRY == 3, this executes twice. */
2966 while (repeat_rate
> 0 && (index
< LINK_QUAL_MAX_RETRY_NUM
)) {
2967 if (is_legacy(tbl_type
.lq_type
)) {
2968 if (ant_toggle_cnt
< NUM_TRY_BEFORE_ANT_TOGGLE
)
2971 rs_toggle_antenna(valid_tx_ant
,
2972 &new_rate
, &tbl_type
))
2976 /* Override next rate if needed for debug purposes */
2977 rs_dbgfs_set_mcs(lq_sta
, &new_rate
, index
);
2979 /* Fill next table entry */
2980 lq_cmd
->rs_table
[index
].rate_n_flags
=
2981 cpu_to_le32(new_rate
);
2986 rs_get_tbl_info_from_mcs(new_rate
, lq_sta
->band
, &tbl_type
,
2989 if (priv
&& priv
->bt_full_concurrent
) {
2992 first_antenna(priv
->nvm_data
->valid_tx_ant
);
2995 /* Indicate to uCode which entries might be MIMO.
2996 * If initial rate was MIMO, this will finally end up
2997 * as (IWL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
2998 if (is_mimo(tbl_type
.lq_type
))
2999 lq_cmd
->general_params
.mimo_delimiter
= index
;
3002 new_rate
= rs_get_lower_rate(lq_sta
, &tbl_type
, rate_idx
,
3005 /* How many times should we repeat the next rate? */
3006 if (is_legacy(tbl_type
.lq_type
)) {
3007 if (ant_toggle_cnt
< NUM_TRY_BEFORE_ANT_TOGGLE
)
3010 rs_toggle_antenna(valid_tx_ant
,
3011 &new_rate
, &tbl_type
))
3014 repeat_rate
= IWL_NUMBER_TRY
;
3016 repeat_rate
= IWL_HT_NUMBER_TRY
;
3019 /* Don't allow HT rates after next pass.
3020 * rs_get_lower_rate() will change type to LQ_A or LQ_G. */
3021 use_ht_possible
= 0;
3023 /* Override next rate if needed for debug purposes */
3024 rs_dbgfs_set_mcs(lq_sta
, &new_rate
, index
);
3026 /* Fill next table entry */
3027 lq_cmd
->rs_table
[index
].rate_n_flags
= cpu_to_le32(new_rate
);
3033 lq_cmd
->agg_params
.agg_frame_cnt_limit
=
3034 sta_priv
->max_agg_bufsize
?: LINK_QUAL_AGG_FRAME_LIMIT_DEF
;
3035 lq_cmd
->agg_params
.agg_dis_start_th
= LINK_QUAL_AGG_DISABLE_START_DEF
;
3037 lq_cmd
->agg_params
.agg_time_limit
=
3038 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF
);
3040 * overwrite if needed, pass aggregation time limit
3043 if (priv
&& priv
->lib
->bt_params
&&
3044 priv
->lib
->bt_params
->agg_time_limit
&&
3045 priv
->bt_traffic_load
>= IWL_BT_COEX_TRAFFIC_LOAD_HIGH
)
3046 lq_cmd
->agg_params
.agg_time_limit
=
3047 cpu_to_le16(priv
->lib
->bt_params
->agg_time_limit
);
3050 static void *rs_alloc(struct ieee80211_hw
*hw
, struct dentry
*debugfsdir
)
3054 /* rate scale requires free function to be implemented */
3055 static void rs_free(void *priv_rate
)
3060 static void rs_free_sta(void *priv_r
, struct ieee80211_sta
*sta
,
3063 struct iwl_op_mode
*op_mode __maybe_unused
= priv_r
;
3064 struct iwl_priv
*priv __maybe_unused
= IWL_OP_MODE_GET_DVM(op_mode
);
3066 IWL_DEBUG_RATE(priv
, "enter\n");
3067 IWL_DEBUG_RATE(priv
, "leave\n");
3070 #ifdef CONFIG_MAC80211_DEBUGFS
3071 static void rs_dbgfs_set_mcs(struct iwl_lq_sta
*lq_sta
,
3072 u32
*rate_n_flags
, int index
)
3074 struct iwl_priv
*priv
;
3079 valid_tx_ant
= priv
->nvm_data
->valid_tx_ant
;
3080 if (lq_sta
->dbg_fixed_rate
) {
3082 ((lq_sta
->dbg_fixed_rate
& RATE_MCS_ANT_ABC_MSK
)
3083 >> RATE_MCS_ANT_POS
);
3084 if ((valid_tx_ant
& ant_sel_tx
) == ant_sel_tx
) {
3085 *rate_n_flags
= lq_sta
->dbg_fixed_rate
;
3086 IWL_DEBUG_RATE(priv
, "Fixed rate ON\n");
3088 lq_sta
->dbg_fixed_rate
= 0;
3090 "Invalid antenna selection 0x%X, Valid is 0x%X\n",
3091 ant_sel_tx
, valid_tx_ant
);
3092 IWL_DEBUG_RATE(priv
, "Fixed rate OFF\n");
3095 IWL_DEBUG_RATE(priv
, "Fixed rate OFF\n");
3099 static ssize_t
rs_sta_dbgfs_scale_table_write(struct file
*file
,
3100 const char __user
*user_buf
, size_t count
, loff_t
*ppos
)
3102 struct iwl_lq_sta
*lq_sta
= file
->private_data
;
3103 struct iwl_priv
*priv
;
3110 memset(buf
, 0, sizeof(buf
));
3111 buf_size
= min(count
, sizeof(buf
) - 1);
3112 if (copy_from_user(buf
, user_buf
, buf_size
))
3115 if (sscanf(buf
, "%x", &parsed_rate
) == 1)
3116 lq_sta
->dbg_fixed_rate
= parsed_rate
;
3118 lq_sta
->dbg_fixed_rate
= 0;
3120 rs_program_fix_rate(priv
, lq_sta
);
3125 static ssize_t
rs_sta_dbgfs_scale_table_read(struct file
*file
,
3126 char __user
*user_buf
, size_t count
, loff_t
*ppos
)
3134 struct iwl_lq_sta
*lq_sta
= file
->private_data
;
3135 struct iwl_priv
*priv
;
3136 struct iwl_scale_tbl_info
*tbl
= &(lq_sta
->lq_info
[lq_sta
->active_tbl
]);
3139 buff
= kmalloc(1024, GFP_KERNEL
);
3143 desc
+= sprintf(buff
+desc
, "sta_id %d\n", lq_sta
->lq
.sta_id
);
3144 desc
+= sprintf(buff
+desc
, "failed=%d success=%d rate=0%X\n",
3145 lq_sta
->total_failed
, lq_sta
->total_success
,
3146 lq_sta
->active_legacy_rate
);
3147 desc
+= sprintf(buff
+desc
, "fixed rate 0x%X\n",
3148 lq_sta
->dbg_fixed_rate
);
3149 desc
+= sprintf(buff
+desc
, "valid_tx_ant %s%s%s\n",
3150 (priv
->nvm_data
->valid_tx_ant
& ANT_A
) ? "ANT_A," : "",
3151 (priv
->nvm_data
->valid_tx_ant
& ANT_B
) ? "ANT_B," : "",
3152 (priv
->nvm_data
->valid_tx_ant
& ANT_C
) ? "ANT_C" : "");
3153 desc
+= sprintf(buff
+desc
, "lq type %s\n",
3154 (is_legacy(tbl
->lq_type
)) ? "legacy" : "HT");
3155 if (is_Ht(tbl
->lq_type
)) {
3156 desc
+= sprintf(buff
+desc
, " %s",
3157 (is_siso(tbl
->lq_type
)) ? "SISO" :
3158 ((is_mimo2(tbl
->lq_type
)) ? "MIMO2" : "MIMO3"));
3159 desc
+= sprintf(buff
+desc
, " %s",
3160 (tbl
->is_ht40
) ? "40MHz" : "20MHz");
3161 desc
+= sprintf(buff
+desc
, " %s %s %s\n", (tbl
->is_SGI
) ? "SGI" : "",
3162 (lq_sta
->is_green
) ? "GF enabled" : "",
3163 (lq_sta
->is_agg
) ? "AGG on" : "");
3165 desc
+= sprintf(buff
+desc
, "last tx rate=0x%X\n",
3166 lq_sta
->last_rate_n_flags
);
3167 desc
+= sprintf(buff
+desc
, "general:"
3168 "flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
3169 lq_sta
->lq
.general_params
.flags
,
3170 lq_sta
->lq
.general_params
.mimo_delimiter
,
3171 lq_sta
->lq
.general_params
.single_stream_ant_msk
,
3172 lq_sta
->lq
.general_params
.dual_stream_ant_msk
);
3174 desc
+= sprintf(buff
+desc
, "agg:"
3175 "time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
3176 le16_to_cpu(lq_sta
->lq
.agg_params
.agg_time_limit
),
3177 lq_sta
->lq
.agg_params
.agg_dis_start_th
,
3178 lq_sta
->lq
.agg_params
.agg_frame_cnt_limit
);
3180 desc
+= sprintf(buff
+desc
,
3181 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
3182 lq_sta
->lq
.general_params
.start_rate_index
[0],
3183 lq_sta
->lq
.general_params
.start_rate_index
[1],
3184 lq_sta
->lq
.general_params
.start_rate_index
[2],
3185 lq_sta
->lq
.general_params
.start_rate_index
[3]);
3187 for (i
= 0; i
< LINK_QUAL_MAX_RETRY_NUM
; i
++) {
3188 index
= iwl_hwrate_to_plcp_idx(
3189 le32_to_cpu(lq_sta
->lq
.rs_table
[i
].rate_n_flags
));
3190 if (is_legacy(tbl
->lq_type
)) {
3191 desc
+= sprintf(buff
+desc
, " rate[%d] 0x%X %smbps\n",
3192 i
, le32_to_cpu(lq_sta
->lq
.rs_table
[i
].rate_n_flags
),
3193 iwl_rate_mcs
[index
].mbps
);
3195 desc
+= sprintf(buff
+desc
, " rate[%d] 0x%X %smbps (%s)\n",
3196 i
, le32_to_cpu(lq_sta
->lq
.rs_table
[i
].rate_n_flags
),
3197 iwl_rate_mcs
[index
].mbps
, iwl_rate_mcs
[index
].mcs
);
3201 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buff
, desc
);
3206 static const struct file_operations rs_sta_dbgfs_scale_table_ops
= {
3207 .write
= rs_sta_dbgfs_scale_table_write
,
3208 .read
= rs_sta_dbgfs_scale_table_read
,
3209 .open
= simple_open
,
3210 .llseek
= default_llseek
,
3212 static ssize_t
rs_sta_dbgfs_stats_table_read(struct file
*file
,
3213 char __user
*user_buf
, size_t count
, loff_t
*ppos
)
3220 struct iwl_lq_sta
*lq_sta
= file
->private_data
;
3222 buff
= kmalloc(1024, GFP_KERNEL
);
3226 for (i
= 0; i
< LQ_SIZE
; i
++) {
3227 desc
+= sprintf(buff
+desc
,
3228 "%s type=%d SGI=%d HT40=%d DUP=%d GF=%d\n"
3230 lq_sta
->active_tbl
== i
? "*" : "x",
3231 lq_sta
->lq_info
[i
].lq_type
,
3232 lq_sta
->lq_info
[i
].is_SGI
,
3233 lq_sta
->lq_info
[i
].is_ht40
,
3234 lq_sta
->lq_info
[i
].is_dup
,
3236 lq_sta
->lq_info
[i
].current_rate
);
3237 for (j
= 0; j
< IWL_RATE_COUNT
; j
++) {
3238 desc
+= sprintf(buff
+desc
,
3239 "counter=%d success=%d %%=%d\n",
3240 lq_sta
->lq_info
[i
].win
[j
].counter
,
3241 lq_sta
->lq_info
[i
].win
[j
].success_counter
,
3242 lq_sta
->lq_info
[i
].win
[j
].success_ratio
);
3245 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buff
, desc
);
3250 static const struct file_operations rs_sta_dbgfs_stats_table_ops
= {
3251 .read
= rs_sta_dbgfs_stats_table_read
,
3252 .open
= simple_open
,
3253 .llseek
= default_llseek
,
3256 static ssize_t
rs_sta_dbgfs_rate_scale_data_read(struct file
*file
,
3257 char __user
*user_buf
, size_t count
, loff_t
*ppos
)
3259 struct iwl_lq_sta
*lq_sta
= file
->private_data
;
3260 struct iwl_scale_tbl_info
*tbl
= &lq_sta
->lq_info
[lq_sta
->active_tbl
];
3264 if (is_Ht(tbl
->lq_type
))
3265 desc
+= sprintf(buff
+desc
,
3266 "Bit Rate= %d Mb/s\n",
3267 tbl
->expected_tpt
[lq_sta
->last_txrate_idx
]);
3269 desc
+= sprintf(buff
+desc
,
3270 "Bit Rate= %d Mb/s\n",
3271 iwl_rates
[lq_sta
->last_txrate_idx
].ieee
>> 1);
3273 return simple_read_from_buffer(user_buf
, count
, ppos
, buff
, desc
);
3276 static const struct file_operations rs_sta_dbgfs_rate_scale_data_ops
= {
3277 .read
= rs_sta_dbgfs_rate_scale_data_read
,
3278 .open
= simple_open
,
3279 .llseek
= default_llseek
,
3282 static void rs_add_debugfs(void *priv
, void *priv_sta
,
3285 struct iwl_lq_sta
*lq_sta
= priv_sta
;
3286 lq_sta
->rs_sta_dbgfs_scale_table_file
=
3287 debugfs_create_file("rate_scale_table", S_IRUSR
| S_IWUSR
, dir
,
3288 lq_sta
, &rs_sta_dbgfs_scale_table_ops
);
3289 lq_sta
->rs_sta_dbgfs_stats_table_file
=
3290 debugfs_create_file("rate_stats_table", S_IRUSR
, dir
,
3291 lq_sta
, &rs_sta_dbgfs_stats_table_ops
);
3292 lq_sta
->rs_sta_dbgfs_rate_scale_data_file
=
3293 debugfs_create_file("rate_scale_data", S_IRUSR
, dir
,
3294 lq_sta
, &rs_sta_dbgfs_rate_scale_data_ops
);
3295 lq_sta
->rs_sta_dbgfs_tx_agg_tid_en_file
=
3296 debugfs_create_u8("tx_agg_tid_enable", S_IRUSR
| S_IWUSR
, dir
,
3297 &lq_sta
->tx_agg_tid_en
);
3301 static void rs_remove_debugfs(void *priv
, void *priv_sta
)
3303 struct iwl_lq_sta
*lq_sta
= priv_sta
;
3304 debugfs_remove(lq_sta
->rs_sta_dbgfs_scale_table_file
);
3305 debugfs_remove(lq_sta
->rs_sta_dbgfs_stats_table_file
);
3306 debugfs_remove(lq_sta
->rs_sta_dbgfs_rate_scale_data_file
);
3307 debugfs_remove(lq_sta
->rs_sta_dbgfs_tx_agg_tid_en_file
);
3312 * Initialization of rate scaling information is done by driver after
3313 * the station is added. Since mac80211 calls this function before a
3314 * station is added we ignore it.
3316 static void rs_rate_init_stub(void *priv_r
, struct ieee80211_supported_band
*sband
,
3317 struct cfg80211_chan_def
*chandef
,
3318 struct ieee80211_sta
*sta
, void *priv_sta
)
3322 static const struct rate_control_ops rs_ops
= {
3324 .tx_status
= rs_tx_status
,
3325 .get_rate
= rs_get_rate
,
3326 .rate_init
= rs_rate_init_stub
,
3329 .alloc_sta
= rs_alloc_sta
,
3330 .free_sta
= rs_free_sta
,
3331 #ifdef CONFIG_MAC80211_DEBUGFS
3332 .add_sta_debugfs
= rs_add_debugfs
,
3333 .remove_sta_debugfs
= rs_remove_debugfs
,
3337 int iwlagn_rate_control_register(void)
3339 return ieee80211_rate_control_register(&rs_ops
);
3342 void iwlagn_rate_control_unregister(void)
3344 ieee80211_rate_control_unregister(&rs_ops
);