1 // SPDX-License-Identifier: GPL-2.0-only
2 /******************************************************************************
4 * Copyright(c) 2005 - 2014 Intel Corporation. All rights reserved.
7 * Intel Linux Wireless <linuxwifi@intel.com>
8 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
10 *****************************************************************************/
11 #include <linux/kernel.h>
12 #include <linux/skbuff.h>
13 #include <linux/slab.h>
14 #include <net/mac80211.h>
16 #include <linux/netdevice.h>
17 #include <linux/etherdevice.h>
18 #include <linux/delay.h>
20 #include <linux/workqueue.h>
25 #define RS_NAME "iwl-agn-rs"
27 #define NUM_TRY_BEFORE_ANT_TOGGLE 1
28 #define IWL_NUMBER_TRY 1
29 #define IWL_HT_NUMBER_TRY 3
31 #define IWL_RATE_MAX_WINDOW 62 /* # tx in history window */
32 #define IWL_RATE_MIN_FAILURE_TH 6 /* min failures to calc tpt */
33 #define IWL_RATE_MIN_SUCCESS_TH 8 /* min successes to calc tpt */
35 /* max allowed rate miss before sync LQ cmd */
36 #define IWL_MISSED_RATE_MAX 15
37 /* max time to accum history 2 seconds */
38 #define IWL_RATE_SCALE_FLUSH_INTVL (3*HZ)
40 static u8 rs_ht_to_legacy
[] = {
41 IWL_RATE_6M_INDEX
, IWL_RATE_6M_INDEX
,
42 IWL_RATE_6M_INDEX
, IWL_RATE_6M_INDEX
,
44 IWL_RATE_6M_INDEX
, IWL_RATE_9M_INDEX
,
45 IWL_RATE_12M_INDEX
, IWL_RATE_18M_INDEX
,
46 IWL_RATE_24M_INDEX
, IWL_RATE_36M_INDEX
,
47 IWL_RATE_48M_INDEX
, IWL_RATE_54M_INDEX
50 static const u8 ant_toggle_lookup
[] = {
51 /*ANT_NONE -> */ ANT_NONE
,
54 /*ANT_AB -> */ ANT_BC
,
56 /*ANT_AC -> */ ANT_AB
,
57 /*ANT_BC -> */ ANT_AC
,
58 /*ANT_ABC -> */ ANT_ABC
,
61 #define IWL_DECLARE_RATE_INFO(r, s, ip, in, rp, rn, pp, np) \
62 [IWL_RATE_##r##M_INDEX] = { IWL_RATE_##r##M_PLCP, \
63 IWL_RATE_SISO_##s##M_PLCP, \
64 IWL_RATE_MIMO2_##s##M_PLCP,\
65 IWL_RATE_MIMO3_##s##M_PLCP,\
66 IWL_RATE_##r##M_IEEE, \
67 IWL_RATE_##ip##M_INDEX, \
68 IWL_RATE_##in##M_INDEX, \
69 IWL_RATE_##rp##M_INDEX, \
70 IWL_RATE_##rn##M_INDEX, \
71 IWL_RATE_##pp##M_INDEX, \
72 IWL_RATE_##np##M_INDEX }
76 * rate, ht rate, prev rate, next rate, prev tgg rate, next tgg rate
78 * If there isn't a valid next or previous rate then INV is used which
79 * maps to IWL_RATE_INVALID
82 const struct iwl_rate_info iwl_rates
[IWL_RATE_COUNT
] = {
83 IWL_DECLARE_RATE_INFO(1, INV
, INV
, 2, INV
, 2, INV
, 2), /* 1mbps */
84 IWL_DECLARE_RATE_INFO(2, INV
, 1, 5, 1, 5, 1, 5), /* 2mbps */
85 IWL_DECLARE_RATE_INFO(5, INV
, 2, 6, 2, 11, 2, 11), /*5.5mbps */
86 IWL_DECLARE_RATE_INFO(11, INV
, 9, 12, 9, 12, 5, 18), /* 11mbps */
87 IWL_DECLARE_RATE_INFO(6, 6, 5, 9, 5, 11, 5, 11), /* 6mbps */
88 IWL_DECLARE_RATE_INFO(9, 6, 6, 11, 6, 11, 5, 11), /* 9mbps */
89 IWL_DECLARE_RATE_INFO(12, 12, 11, 18, 11, 18, 11, 18), /* 12mbps */
90 IWL_DECLARE_RATE_INFO(18, 18, 12, 24, 12, 24, 11, 24), /* 18mbps */
91 IWL_DECLARE_RATE_INFO(24, 24, 18, 36, 18, 36, 18, 36), /* 24mbps */
92 IWL_DECLARE_RATE_INFO(36, 36, 24, 48, 24, 48, 24, 48), /* 36mbps */
93 IWL_DECLARE_RATE_INFO(48, 48, 36, 54, 36, 54, 36, 54), /* 48mbps */
94 IWL_DECLARE_RATE_INFO(54, 54, 48, INV
, 48, INV
, 48, INV
),/* 54mbps */
95 IWL_DECLARE_RATE_INFO(60, 60, 48, INV
, 48, INV
, 48, INV
),/* 60mbps */
96 /* FIXME:RS: ^^ should be INV (legacy) */
99 static inline u8
rs_extract_rate(u32 rate_n_flags
)
101 return (u8
)(rate_n_flags
& RATE_MCS_RATE_MSK
);
104 static int iwl_hwrate_to_plcp_idx(u32 rate_n_flags
)
109 if (rate_n_flags
& RATE_MCS_HT_MSK
) {
110 idx
= rs_extract_rate(rate_n_flags
);
112 if (idx
>= IWL_RATE_MIMO3_6M_PLCP
)
113 idx
= idx
- IWL_RATE_MIMO3_6M_PLCP
;
114 else if (idx
>= IWL_RATE_MIMO2_6M_PLCP
)
115 idx
= idx
- IWL_RATE_MIMO2_6M_PLCP
;
117 idx
+= IWL_FIRST_OFDM_RATE
;
118 /* skip 9M not supported in ht*/
119 if (idx
>= IWL_RATE_9M_INDEX
)
121 if ((idx
>= IWL_FIRST_OFDM_RATE
) && (idx
<= IWL_LAST_OFDM_RATE
))
124 /* legacy rate format, search for match in table */
126 for (idx
= 0; idx
< ARRAY_SIZE(iwl_rates
); idx
++)
127 if (iwl_rates
[idx
].plcp
==
128 rs_extract_rate(rate_n_flags
))
135 static void rs_rate_scale_perform(struct iwl_priv
*priv
,
137 struct ieee80211_sta
*sta
,
138 struct iwl_lq_sta
*lq_sta
);
139 static void rs_fill_link_cmd(struct iwl_priv
*priv
,
140 struct iwl_lq_sta
*lq_sta
, u32 rate_n_flags
);
141 static void rs_stay_in_table(struct iwl_lq_sta
*lq_sta
, bool force_search
);
144 #ifdef CONFIG_MAC80211_DEBUGFS
145 static void rs_dbgfs_set_mcs(struct iwl_lq_sta
*lq_sta
,
146 u32
*rate_n_flags
, int index
);
148 static void rs_dbgfs_set_mcs(struct iwl_lq_sta
*lq_sta
,
149 u32
*rate_n_flags
, int index
)
154 * The following tables contain the expected throughput metrics for all rates
156 * 1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
158 * where invalid entries are zeros.
160 * CCK rates are only valid in legacy table and will only be used in G
164 static const u16 expected_tpt_legacy
[IWL_RATE_COUNT
] = {
165 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 0
168 static const u16 expected_tpt_siso20MHz
[4][IWL_RATE_COUNT
] = {
169 {0, 0, 0, 0, 42, 0, 76, 102, 124, 159, 183, 193, 202}, /* Norm */
170 {0, 0, 0, 0, 46, 0, 82, 110, 132, 168, 192, 202, 210}, /* SGI */
171 {0, 0, 0, 0, 47, 0, 91, 133, 171, 242, 305, 334, 362}, /* AGG */
172 {0, 0, 0, 0, 52, 0, 101, 145, 187, 264, 330, 361, 390}, /* AGG+SGI */
175 static const u16 expected_tpt_siso40MHz
[4][IWL_RATE_COUNT
] = {
176 {0, 0, 0, 0, 77, 0, 127, 160, 184, 220, 242, 250, 257}, /* Norm */
177 {0, 0, 0, 0, 83, 0, 135, 169, 193, 229, 250, 257, 264}, /* SGI */
178 {0, 0, 0, 0, 94, 0, 177, 249, 313, 423, 512, 550, 586}, /* AGG */
179 {0, 0, 0, 0, 104, 0, 193, 270, 338, 454, 545, 584, 620}, /* AGG+SGI */
182 static const u16 expected_tpt_mimo2_20MHz
[4][IWL_RATE_COUNT
] = {
183 {0, 0, 0, 0, 74, 0, 123, 155, 179, 214, 236, 244, 251}, /* Norm */
184 {0, 0, 0, 0, 81, 0, 131, 164, 188, 223, 243, 251, 257}, /* SGI */
185 {0, 0, 0, 0, 89, 0, 167, 235, 296, 402, 488, 526, 560}, /* AGG */
186 {0, 0, 0, 0, 97, 0, 182, 255, 320, 431, 520, 558, 593}, /* AGG+SGI*/
189 static const u16 expected_tpt_mimo2_40MHz
[4][IWL_RATE_COUNT
] = {
190 {0, 0, 0, 0, 123, 0, 182, 214, 235, 264, 279, 285, 289}, /* Norm */
191 {0, 0, 0, 0, 131, 0, 191, 222, 242, 270, 284, 289, 293}, /* SGI */
192 {0, 0, 0, 0, 171, 0, 305, 410, 496, 634, 731, 771, 805}, /* AGG */
193 {0, 0, 0, 0, 186, 0, 329, 439, 527, 667, 764, 803, 838}, /* AGG+SGI */
196 static const u16 expected_tpt_mimo3_20MHz
[4][IWL_RATE_COUNT
] = {
197 {0, 0, 0, 0, 99, 0, 153, 186, 208, 239, 256, 263, 268}, /* Norm */
198 {0, 0, 0, 0, 106, 0, 162, 194, 215, 246, 262, 268, 273}, /* SGI */
199 {0, 0, 0, 0, 134, 0, 249, 346, 431, 574, 685, 732, 775}, /* AGG */
200 {0, 0, 0, 0, 148, 0, 272, 376, 465, 614, 727, 775, 818}, /* AGG+SGI */
203 static const u16 expected_tpt_mimo3_40MHz
[4][IWL_RATE_COUNT
] = {
204 {0, 0, 0, 0, 152, 0, 211, 239, 255, 279, 290, 294, 297}, /* Norm */
205 {0, 0, 0, 0, 160, 0, 219, 245, 261, 284, 294, 297, 300}, /* SGI */
206 {0, 0, 0, 0, 254, 0, 443, 584, 695, 868, 984, 1030, 1070}, /* AGG */
207 {0, 0, 0, 0, 277, 0, 478, 624, 737, 911, 1026, 1070, 1109}, /* AGG+SGI */
211 static const struct iwl_rate_mcs_info iwl_rate_mcs
[IWL_RATE_COUNT
] = {
220 { "24", "16QAM 1/2"},
221 { "36", "16QAM 3/4"},
222 { "48", "64QAM 2/3"},
223 { "54", "64QAM 3/4"},
224 { "60", "64QAM 5/6"},
227 #define MCS_INDEX_PER_STREAM (8)
229 static void rs_rate_scale_clear_window(struct iwl_rate_scale_data
*window
)
232 window
->success_counter
= 0;
233 window
->success_ratio
= IWL_INVALID_VALUE
;
235 window
->average_tpt
= IWL_INVALID_VALUE
;
239 static inline u8
rs_is_valid_ant(u8 valid_antenna
, u8 ant_type
)
241 return (ant_type
& valid_antenna
) == ant_type
;
245 * removes the old data from the statistics. All data that is older than
246 * TID_MAX_TIME_DIFF, will be deleted.
248 static void rs_tl_rm_old_stats(struct iwl_traffic_load
*tl
, u32 curr_time
)
250 /* The oldest age we want to keep */
251 u32 oldest_time
= curr_time
- TID_MAX_TIME_DIFF
;
253 while (tl
->queue_count
&&
254 (tl
->time_stamp
< oldest_time
)) {
255 tl
->total
-= tl
->packet_count
[tl
->head
];
256 tl
->packet_count
[tl
->head
] = 0;
257 tl
->time_stamp
+= TID_QUEUE_CELL_SPACING
;
260 if (tl
->head
>= TID_QUEUE_MAX_SIZE
)
266 * increment traffic load value for tid and also remove
267 * any old values if passed the certain time period
269 static u8
rs_tl_add_packet(struct iwl_lq_sta
*lq_data
,
270 struct ieee80211_hdr
*hdr
)
272 u32 curr_time
= jiffies_to_msecs(jiffies
);
275 struct iwl_traffic_load
*tl
= NULL
;
278 if (ieee80211_is_data_qos(hdr
->frame_control
)) {
279 u8
*qc
= ieee80211_get_qos_ctl(hdr
);
282 return IWL_MAX_TID_COUNT
;
284 if (unlikely(tid
>= IWL_MAX_TID_COUNT
))
285 return IWL_MAX_TID_COUNT
;
287 tl
= &lq_data
->load
[tid
];
289 curr_time
-= curr_time
% TID_ROUND_VALUE
;
291 /* Happens only for the first packet. Initialize the data */
292 if (!(tl
->queue_count
)) {
294 tl
->time_stamp
= curr_time
;
297 tl
->packet_count
[0] = 1;
298 return IWL_MAX_TID_COUNT
;
301 time_diff
= TIME_WRAP_AROUND(tl
->time_stamp
, curr_time
);
302 index
= time_diff
/ TID_QUEUE_CELL_SPACING
;
304 /* The history is too long: remove data that is older than */
305 /* TID_MAX_TIME_DIFF */
306 if (index
>= TID_QUEUE_MAX_SIZE
)
307 rs_tl_rm_old_stats(tl
, curr_time
);
309 index
= (tl
->head
+ index
) % TID_QUEUE_MAX_SIZE
;
310 tl
->packet_count
[index
] = tl
->packet_count
[index
] + 1;
311 tl
->total
= tl
->total
+ 1;
313 if ((index
+ 1) > tl
->queue_count
)
314 tl
->queue_count
= index
+ 1;
319 #ifdef CONFIG_MAC80211_DEBUGFS
321 * Program the device to use fixed rate for frame transmit
322 * This is for debugging/testing only
323 * once the device start use fixed rate, we need to reload the module
324 * to being back the normal operation.
326 static void rs_program_fix_rate(struct iwl_priv
*priv
,
327 struct iwl_lq_sta
*lq_sta
)
329 struct iwl_station_priv
*sta_priv
=
330 container_of(lq_sta
, struct iwl_station_priv
, lq_sta
);
331 struct iwl_rxon_context
*ctx
= sta_priv
->ctx
;
333 lq_sta
->active_legacy_rate
= 0x0FFF; /* 1 - 54 MBits, includes CCK */
334 lq_sta
->active_siso_rate
= 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
335 lq_sta
->active_mimo2_rate
= 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
336 lq_sta
->active_mimo3_rate
= 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
338 IWL_DEBUG_RATE(priv
, "sta_id %d rate 0x%X\n",
339 lq_sta
->lq
.sta_id
, lq_sta
->dbg_fixed_rate
);
341 if (lq_sta
->dbg_fixed_rate
) {
342 rs_fill_link_cmd(NULL
, lq_sta
, lq_sta
->dbg_fixed_rate
);
343 iwl_send_lq_cmd(lq_sta
->drv
, ctx
, &lq_sta
->lq
, CMD_ASYNC
,
350 get the traffic load value for tid
352 static void rs_tl_get_load(struct iwl_lq_sta
*lq_data
, u8 tid
)
354 u32 curr_time
= jiffies_to_msecs(jiffies
);
357 struct iwl_traffic_load
*tl
= NULL
;
359 if (tid
>= IWL_MAX_TID_COUNT
)
362 tl
= &(lq_data
->load
[tid
]);
364 curr_time
-= curr_time
% TID_ROUND_VALUE
;
366 if (!(tl
->queue_count
))
369 time_diff
= TIME_WRAP_AROUND(tl
->time_stamp
, curr_time
);
370 index
= time_diff
/ TID_QUEUE_CELL_SPACING
;
372 /* The history is too long: remove data that is older than */
373 /* TID_MAX_TIME_DIFF */
374 if (index
>= TID_QUEUE_MAX_SIZE
)
375 rs_tl_rm_old_stats(tl
, curr_time
);
378 static int rs_tl_turn_on_agg_for_tid(struct iwl_priv
*priv
,
379 struct iwl_lq_sta
*lq_data
, u8 tid
,
380 struct ieee80211_sta
*sta
)
385 * Don't create TX aggregation sessions when in high
386 * BT traffic, as they would just be disrupted by BT.
388 if (priv
->bt_traffic_load
>= IWL_BT_COEX_TRAFFIC_LOAD_HIGH
) {
390 "BT traffic (%d), no aggregation allowed\n",
391 priv
->bt_traffic_load
);
395 rs_tl_get_load(lq_data
, tid
);
397 IWL_DEBUG_HT(priv
, "Starting Tx agg: STA: %pM tid: %d\n",
399 ret
= ieee80211_start_tx_ba_session(sta
, tid
, 5000);
400 if (ret
== -EAGAIN
) {
402 * driver and mac80211 is out of sync
403 * this might be cause by reloading firmware
404 * stop the tx ba session here
406 IWL_ERR(priv
, "Fail start Tx agg on tid: %d\n",
408 ieee80211_stop_tx_ba_session(sta
, tid
);
413 static void rs_tl_turn_on_agg(struct iwl_priv
*priv
, u8 tid
,
414 struct iwl_lq_sta
*lq_data
,
415 struct ieee80211_sta
*sta
)
417 if (tid
< IWL_MAX_TID_COUNT
)
418 rs_tl_turn_on_agg_for_tid(priv
, lq_data
, tid
, sta
);
420 IWL_ERR(priv
, "tid exceeds max TID count: %d/%d\n",
421 tid
, IWL_MAX_TID_COUNT
);
424 static inline int get_num_of_ant_from_rate(u32 rate_n_flags
)
426 return !!(rate_n_flags
& RATE_MCS_ANT_A_MSK
) +
427 !!(rate_n_flags
& RATE_MCS_ANT_B_MSK
) +
428 !!(rate_n_flags
& RATE_MCS_ANT_C_MSK
);
432 * Static function to get the expected throughput from an iwl_scale_tbl_info
433 * that wraps a NULL pointer check
435 static s32
get_expected_tpt(struct iwl_scale_tbl_info
*tbl
, int rs_index
)
437 if (tbl
->expected_tpt
)
438 return tbl
->expected_tpt
[rs_index
];
443 * rs_collect_tx_data - Update the success/failure sliding window
445 * We keep a sliding window of the last 62 packets transmitted
446 * at this rate. window->data contains the bitmask of successful
449 static int rs_collect_tx_data(struct iwl_scale_tbl_info
*tbl
,
450 int scale_index
, int attempts
, int successes
)
452 struct iwl_rate_scale_data
*window
= NULL
;
453 static const u64 mask
= (((u64
)1) << (IWL_RATE_MAX_WINDOW
- 1));
456 if (scale_index
< 0 || scale_index
>= IWL_RATE_COUNT
)
459 /* Select window for current tx bit rate */
460 window
= &(tbl
->win
[scale_index
]);
462 /* Get expected throughput */
463 tpt
= get_expected_tpt(tbl
, scale_index
);
466 * Keep track of only the latest 62 tx frame attempts in this rate's
467 * history window; anything older isn't really relevant any more.
468 * If we have filled up the sliding window, drop the oldest attempt;
469 * if the oldest attempt (highest bit in bitmap) shows "success",
470 * subtract "1" from the success counter (this is the main reason
471 * we keep these bitmaps!).
473 while (attempts
> 0) {
474 if (window
->counter
>= IWL_RATE_MAX_WINDOW
) {
476 /* remove earliest */
477 window
->counter
= IWL_RATE_MAX_WINDOW
- 1;
479 if (window
->data
& mask
) {
480 window
->data
&= ~mask
;
481 window
->success_counter
--;
485 /* Increment frames-attempted counter */
488 /* Shift bitmap by one frame to throw away oldest history */
491 /* Mark the most recent #successes attempts as successful */
493 window
->success_counter
++;
501 /* Calculate current success ratio, avoid divide-by-0! */
502 if (window
->counter
> 0)
503 window
->success_ratio
= 128 * (100 * window
->success_counter
)
506 window
->success_ratio
= IWL_INVALID_VALUE
;
508 fail_count
= window
->counter
- window
->success_counter
;
510 /* Calculate average throughput, if we have enough history. */
511 if ((fail_count
>= IWL_RATE_MIN_FAILURE_TH
) ||
512 (window
->success_counter
>= IWL_RATE_MIN_SUCCESS_TH
))
513 window
->average_tpt
= (window
->success_ratio
* tpt
+ 64) / 128;
515 window
->average_tpt
= IWL_INVALID_VALUE
;
517 /* Tag this window as having been updated */
518 window
->stamp
= jiffies
;
524 * Fill uCode API rate_n_flags field, based on "search" or "active" table.
526 /* FIXME:RS:remove this function and put the flags statically in the table */
527 static u32
rate_n_flags_from_tbl(struct iwl_priv
*priv
,
528 struct iwl_scale_tbl_info
*tbl
,
529 int index
, u8 use_green
)
531 u32 rate_n_flags
= 0;
533 if (is_legacy(tbl
->lq_type
)) {
534 rate_n_flags
= iwl_rates
[index
].plcp
;
535 if (index
>= IWL_FIRST_CCK_RATE
&& index
<= IWL_LAST_CCK_RATE
)
536 rate_n_flags
|= RATE_MCS_CCK_MSK
;
538 } else if (is_Ht(tbl
->lq_type
)) {
539 if (index
> IWL_LAST_OFDM_RATE
) {
540 IWL_ERR(priv
, "Invalid HT rate index %d\n", index
);
541 index
= IWL_LAST_OFDM_RATE
;
543 rate_n_flags
= RATE_MCS_HT_MSK
;
545 if (is_siso(tbl
->lq_type
))
546 rate_n_flags
|= iwl_rates
[index
].plcp_siso
;
547 else if (is_mimo2(tbl
->lq_type
))
548 rate_n_flags
|= iwl_rates
[index
].plcp_mimo2
;
550 rate_n_flags
|= iwl_rates
[index
].plcp_mimo3
;
552 IWL_ERR(priv
, "Invalid tbl->lq_type %d\n", tbl
->lq_type
);
555 rate_n_flags
|= ((tbl
->ant_type
<< RATE_MCS_ANT_POS
) &
556 RATE_MCS_ANT_ABC_MSK
);
558 if (is_Ht(tbl
->lq_type
)) {
561 rate_n_flags
|= RATE_MCS_DUP_MSK
;
563 rate_n_flags
|= RATE_MCS_HT40_MSK
;
566 rate_n_flags
|= RATE_MCS_SGI_MSK
;
569 rate_n_flags
|= RATE_MCS_GF_MSK
;
570 if (is_siso(tbl
->lq_type
) && tbl
->is_SGI
) {
571 rate_n_flags
&= ~RATE_MCS_SGI_MSK
;
572 IWL_ERR(priv
, "GF was set with SGI:SISO\n");
580 * Interpret uCode API's rate_n_flags format,
581 * fill "search" or "active" tx mode table.
583 static int rs_get_tbl_info_from_mcs(const u32 rate_n_flags
,
584 enum nl80211_band band
,
585 struct iwl_scale_tbl_info
*tbl
,
588 u32 ant_msk
= (rate_n_flags
& RATE_MCS_ANT_ABC_MSK
);
589 u8 num_of_ant
= get_num_of_ant_from_rate(rate_n_flags
);
592 memset(tbl
, 0, sizeof(struct iwl_scale_tbl_info
));
593 *rate_idx
= iwl_hwrate_to_plcp_idx(rate_n_flags
);
595 if (*rate_idx
== IWL_RATE_INVALID
) {
599 tbl
->is_SGI
= 0; /* default legacy setup */
602 tbl
->ant_type
= (ant_msk
>> RATE_MCS_ANT_POS
);
603 tbl
->lq_type
= LQ_NONE
;
604 tbl
->max_search
= IWL_MAX_SEARCH
;
606 /* legacy rate format */
607 if (!(rate_n_flags
& RATE_MCS_HT_MSK
)) {
608 if (num_of_ant
== 1) {
609 if (band
== NL80211_BAND_5GHZ
)
616 if (rate_n_flags
& RATE_MCS_SGI_MSK
)
619 if ((rate_n_flags
& RATE_MCS_HT40_MSK
) ||
620 (rate_n_flags
& RATE_MCS_DUP_MSK
))
623 if (rate_n_flags
& RATE_MCS_DUP_MSK
)
626 mcs
= rs_extract_rate(rate_n_flags
);
629 if (mcs
<= IWL_RATE_SISO_60M_PLCP
) {
631 tbl
->lq_type
= LQ_SISO
; /*else NONE*/
633 } else if (mcs
<= IWL_RATE_MIMO2_60M_PLCP
) {
635 tbl
->lq_type
= LQ_MIMO2
;
638 if (num_of_ant
== 3) {
639 tbl
->max_search
= IWL_MAX_11N_MIMO3_SEARCH
;
640 tbl
->lq_type
= LQ_MIMO3
;
647 /* switch to another antenna/antennas and return 1 */
648 /* if no other valid antenna found, return 0 */
649 static int rs_toggle_antenna(u32 valid_ant
, u32
*rate_n_flags
,
650 struct iwl_scale_tbl_info
*tbl
)
654 if (!tbl
->ant_type
|| tbl
->ant_type
> ANT_ABC
)
657 if (!rs_is_valid_ant(valid_ant
, tbl
->ant_type
))
660 new_ant_type
= ant_toggle_lookup
[tbl
->ant_type
];
662 while ((new_ant_type
!= tbl
->ant_type
) &&
663 !rs_is_valid_ant(valid_ant
, new_ant_type
))
664 new_ant_type
= ant_toggle_lookup
[new_ant_type
];
666 if (new_ant_type
== tbl
->ant_type
)
669 tbl
->ant_type
= new_ant_type
;
670 *rate_n_flags
&= ~RATE_MCS_ANT_ABC_MSK
;
671 *rate_n_flags
|= new_ant_type
<< RATE_MCS_ANT_POS
;
676 * Green-field mode is valid if the station supports it and
677 * there are no non-GF stations present in the BSS.
679 static bool rs_use_green(struct ieee80211_sta
*sta
)
682 * There's a bug somewhere in this code that causes the
683 * scaling to get stuck because GF+SGI can't be combined
684 * in SISO rates. Until we find that bug, disable GF, it
685 * has only limited benefit and we still interoperate with
686 * GF APs since we can always receive GF transmissions.
692 * rs_get_supported_rates - get the available rates
694 * if management frame or broadcast frame only return
695 * basic available rates.
698 static u16
rs_get_supported_rates(struct iwl_lq_sta
*lq_sta
,
699 struct ieee80211_hdr
*hdr
,
700 enum iwl_table_type rate_type
)
702 if (is_legacy(rate_type
)) {
703 return lq_sta
->active_legacy_rate
;
705 if (is_siso(rate_type
))
706 return lq_sta
->active_siso_rate
;
707 else if (is_mimo2(rate_type
))
708 return lq_sta
->active_mimo2_rate
;
710 return lq_sta
->active_mimo3_rate
;
714 static u16
rs_get_adjacent_rate(struct iwl_priv
*priv
, u8 index
, u16 rate_mask
,
717 u8 high
= IWL_RATE_INVALID
;
718 u8 low
= IWL_RATE_INVALID
;
720 /* 802.11A or ht walks to the next literal adjacent rate in
722 if (is_a_band(rate_type
) || !is_legacy(rate_type
)) {
726 /* Find the previous rate that is in the rate mask */
731 for (; i
>= 0; i
--, mask
>>= 1) {
732 if (rate_mask
& mask
) {
738 /* Find the next rate that is in the rate mask */
740 for (mask
= (1 << i
); i
< IWL_RATE_COUNT
; i
++, mask
<<= 1) {
741 if (rate_mask
& mask
) {
747 return (high
<< 8) | low
;
751 while (low
!= IWL_RATE_INVALID
) {
752 low
= iwl_rates
[low
].prev_rs
;
753 if (low
== IWL_RATE_INVALID
)
755 if (rate_mask
& (1 << low
))
757 IWL_DEBUG_RATE(priv
, "Skipping masked lower rate: %d\n", low
);
761 while (high
!= IWL_RATE_INVALID
) {
762 high
= iwl_rates
[high
].next_rs
;
763 if (high
== IWL_RATE_INVALID
)
765 if (rate_mask
& (1 << high
))
767 IWL_DEBUG_RATE(priv
, "Skipping masked higher rate: %d\n", high
);
770 return (high
<< 8) | low
;
773 static u32
rs_get_lower_rate(struct iwl_lq_sta
*lq_sta
,
774 struct iwl_scale_tbl_info
*tbl
,
775 u8 scale_index
, u8 ht_possible
)
780 u8 switch_to_legacy
= 0;
781 u8 is_green
= lq_sta
->is_green
;
782 struct iwl_priv
*priv
= lq_sta
->drv
;
784 /* check if we need to switch from HT to legacy rates.
785 * assumption is that mandatory rates (1Mbps or 6Mbps)
786 * are always supported (spec demand) */
787 if (!is_legacy(tbl
->lq_type
) && (!ht_possible
|| !scale_index
)) {
788 switch_to_legacy
= 1;
789 scale_index
= rs_ht_to_legacy
[scale_index
];
790 if (lq_sta
->band
== NL80211_BAND_5GHZ
)
795 if (num_of_ant(tbl
->ant_type
) > 1)
797 first_antenna(priv
->nvm_data
->valid_tx_ant
);
801 tbl
->max_search
= IWL_MAX_SEARCH
;
804 rate_mask
= rs_get_supported_rates(lq_sta
, NULL
, tbl
->lq_type
);
806 /* Mask with station rate restriction */
807 if (is_legacy(tbl
->lq_type
)) {
808 /* supp_rates has no CCK bits in A mode */
809 if (lq_sta
->band
== NL80211_BAND_5GHZ
)
810 rate_mask
= (u16
)(rate_mask
&
811 (lq_sta
->supp_rates
<< IWL_FIRST_OFDM_RATE
));
813 rate_mask
= (u16
)(rate_mask
& lq_sta
->supp_rates
);
816 /* If we switched from HT to legacy, check current rate */
817 if (switch_to_legacy
&& (rate_mask
& (1 << scale_index
))) {
822 high_low
= rs_get_adjacent_rate(lq_sta
->drv
, scale_index
, rate_mask
,
824 low
= high_low
& 0xff;
826 if (low
== IWL_RATE_INVALID
)
830 return rate_n_flags_from_tbl(lq_sta
->drv
, tbl
, low
, is_green
);
834 * Simple function to compare two rate scale table types
836 static bool table_type_matches(struct iwl_scale_tbl_info
*a
,
837 struct iwl_scale_tbl_info
*b
)
839 return (a
->lq_type
== b
->lq_type
) && (a
->ant_type
== b
->ant_type
) &&
840 (a
->is_SGI
== b
->is_SGI
);
843 static void rs_bt_update_lq(struct iwl_priv
*priv
, struct iwl_rxon_context
*ctx
,
844 struct iwl_lq_sta
*lq_sta
)
846 struct iwl_scale_tbl_info
*tbl
;
847 bool full_concurrent
= priv
->bt_full_concurrent
;
849 if (priv
->bt_ant_couple_ok
) {
851 * Is there a need to switch between
852 * full concurrency and 3-wire?
854 if (priv
->bt_ci_compliance
)
855 full_concurrent
= true;
857 full_concurrent
= false;
859 if ((priv
->bt_traffic_load
!= priv
->last_bt_traffic_load
) ||
860 (priv
->bt_full_concurrent
!= full_concurrent
)) {
861 priv
->bt_full_concurrent
= full_concurrent
;
862 priv
->last_bt_traffic_load
= priv
->bt_traffic_load
;
864 /* Update uCode's rate table. */
865 tbl
= &(lq_sta
->lq_info
[lq_sta
->active_tbl
]);
866 rs_fill_link_cmd(priv
, lq_sta
, tbl
->current_rate
);
867 iwl_send_lq_cmd(priv
, ctx
, &lq_sta
->lq
, CMD_ASYNC
, false);
869 queue_work(priv
->workqueue
, &priv
->bt_full_concurrency
);
874 * mac80211 sends us Tx status
876 static void rs_tx_status(void *priv_r
, struct ieee80211_supported_band
*sband
,
877 struct ieee80211_sta
*sta
, void *priv_sta
,
882 int rs_index
, mac_index
, i
;
883 struct iwl_lq_sta
*lq_sta
= priv_sta
;
884 struct iwl_link_quality_cmd
*table
;
885 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*)skb
->data
;
886 struct iwl_op_mode
*op_mode
= (struct iwl_op_mode
*)priv_r
;
887 struct iwl_priv
*priv
= IWL_OP_MODE_GET_DVM(op_mode
);
888 struct ieee80211_tx_info
*info
= IEEE80211_SKB_CB(skb
);
889 enum mac80211_rate_control_flags mac_flags
;
891 struct iwl_scale_tbl_info tbl_type
;
892 struct iwl_scale_tbl_info
*curr_tbl
, *other_tbl
, *tmp_tbl
;
893 struct iwl_station_priv
*sta_priv
= (void *)sta
->drv_priv
;
894 struct iwl_rxon_context
*ctx
= sta_priv
->ctx
;
896 IWL_DEBUG_RATE_LIMIT(priv
, "get frame ack response, update rate scale window\n");
898 /* Treat uninitialized rate scaling data same as non-existing. */
900 IWL_DEBUG_RATE(priv
, "Station rate scaling not created yet.\n");
902 } else if (!lq_sta
->drv
) {
903 IWL_DEBUG_RATE(priv
, "Rate scaling not initialized yet.\n");
907 if (!ieee80211_is_data(hdr
->frame_control
) ||
908 info
->flags
& IEEE80211_TX_CTL_NO_ACK
)
911 /* This packet was aggregated but doesn't carry status info */
912 if ((info
->flags
& IEEE80211_TX_CTL_AMPDU
) &&
913 !(info
->flags
& IEEE80211_TX_STAT_AMPDU
))
917 * Ignore this Tx frame response if its initial rate doesn't match
918 * that of latest Link Quality command. There may be stragglers
919 * from a previous Link Quality command, but we're no longer interested
920 * in those; they're either from the "active" mode while we're trying
921 * to check "search" mode, or a prior "search" mode after we've moved
922 * to a new "search" mode (which might become the new "active" mode).
925 tx_rate
= le32_to_cpu(table
->rs_table
[0].rate_n_flags
);
926 rs_get_tbl_info_from_mcs(tx_rate
, priv
->band
, &tbl_type
, &rs_index
);
927 if (priv
->band
== NL80211_BAND_5GHZ
)
928 rs_index
-= IWL_FIRST_OFDM_RATE
;
929 mac_flags
= info
->status
.rates
[0].flags
;
930 mac_index
= info
->status
.rates
[0].idx
;
931 /* For HT packets, map MCS to PLCP */
932 if (mac_flags
& IEEE80211_TX_RC_MCS
) {
933 mac_index
&= RATE_MCS_CODE_MSK
; /* Remove # of streams */
934 if (mac_index
>= (IWL_RATE_9M_INDEX
- IWL_FIRST_OFDM_RATE
))
937 * mac80211 HT index is always zero-indexed; we need to move
938 * HT OFDM rates after CCK rates in 2.4 GHz band
940 if (priv
->band
== NL80211_BAND_2GHZ
)
941 mac_index
+= IWL_FIRST_OFDM_RATE
;
943 /* Here we actually compare this rate to the latest LQ command */
944 if ((mac_index
< 0) ||
945 (tbl_type
.is_SGI
!= !!(mac_flags
& IEEE80211_TX_RC_SHORT_GI
)) ||
946 (tbl_type
.is_ht40
!= !!(mac_flags
& IEEE80211_TX_RC_40_MHZ_WIDTH
)) ||
947 (tbl_type
.is_dup
!= !!(mac_flags
& IEEE80211_TX_RC_DUP_DATA
)) ||
948 (tbl_type
.ant_type
!= info
->status
.antenna
) ||
949 (!!(tx_rate
& RATE_MCS_HT_MSK
) != !!(mac_flags
& IEEE80211_TX_RC_MCS
)) ||
950 (!!(tx_rate
& RATE_MCS_GF_MSK
) != !!(mac_flags
& IEEE80211_TX_RC_GREEN_FIELD
)) ||
951 (rs_index
!= mac_index
)) {
952 IWL_DEBUG_RATE(priv
, "initial rate %d does not match %d (0x%x)\n", mac_index
, rs_index
, tx_rate
);
954 * Since rates mis-match, the last LQ command may have failed.
955 * After IWL_MISSED_RATE_MAX mis-matches, resync the uCode with
958 lq_sta
->missed_rate_counter
++;
959 if (lq_sta
->missed_rate_counter
> IWL_MISSED_RATE_MAX
) {
960 lq_sta
->missed_rate_counter
= 0;
961 iwl_send_lq_cmd(priv
, ctx
, &lq_sta
->lq
, CMD_ASYNC
, false);
963 /* Regardless, ignore this status info for outdated rate */
966 /* Rate did match, so reset the missed_rate_counter */
967 lq_sta
->missed_rate_counter
= 0;
969 /* Figure out if rate scale algorithm is in active or search table */
970 if (table_type_matches(&tbl_type
,
971 &(lq_sta
->lq_info
[lq_sta
->active_tbl
]))) {
972 curr_tbl
= &(lq_sta
->lq_info
[lq_sta
->active_tbl
]);
973 other_tbl
= &(lq_sta
->lq_info
[1 - lq_sta
->active_tbl
]);
974 } else if (table_type_matches(&tbl_type
,
975 &lq_sta
->lq_info
[1 - lq_sta
->active_tbl
])) {
976 curr_tbl
= &(lq_sta
->lq_info
[1 - lq_sta
->active_tbl
]);
977 other_tbl
= &(lq_sta
->lq_info
[lq_sta
->active_tbl
]);
979 IWL_DEBUG_RATE(priv
, "Neither active nor search matches tx rate\n");
980 tmp_tbl
= &(lq_sta
->lq_info
[lq_sta
->active_tbl
]);
981 IWL_DEBUG_RATE(priv
, "active- lq:%x, ant:%x, SGI:%d\n",
982 tmp_tbl
->lq_type
, tmp_tbl
->ant_type
, tmp_tbl
->is_SGI
);
983 tmp_tbl
= &(lq_sta
->lq_info
[1 - lq_sta
->active_tbl
]);
984 IWL_DEBUG_RATE(priv
, "search- lq:%x, ant:%x, SGI:%d\n",
985 tmp_tbl
->lq_type
, tmp_tbl
->ant_type
, tmp_tbl
->is_SGI
);
986 IWL_DEBUG_RATE(priv
, "actual- lq:%x, ant:%x, SGI:%d\n",
987 tbl_type
.lq_type
, tbl_type
.ant_type
, tbl_type
.is_SGI
);
989 * no matching table found, let's by-pass the data collection
990 * and continue to perform rate scale to find the rate table
992 rs_stay_in_table(lq_sta
, true);
997 * Updating the frame history depends on whether packets were
1000 * For aggregation, all packets were transmitted at the same rate, the
1001 * first index into rate scale table.
1003 if (info
->flags
& IEEE80211_TX_STAT_AMPDU
) {
1004 tx_rate
= le32_to_cpu(table
->rs_table
[0].rate_n_flags
);
1005 rs_get_tbl_info_from_mcs(tx_rate
, priv
->band
, &tbl_type
,
1007 rs_collect_tx_data(curr_tbl
, rs_index
,
1008 info
->status
.ampdu_len
,
1009 info
->status
.ampdu_ack_len
);
1011 /* Update success/fail counts if not searching for new mode */
1012 if (lq_sta
->stay_in_tbl
) {
1013 lq_sta
->total_success
+= info
->status
.ampdu_ack_len
;
1014 lq_sta
->total_failed
+= (info
->status
.ampdu_len
-
1015 info
->status
.ampdu_ack_len
);
1019 * For legacy, update frame history with for each Tx retry.
1021 retries
= info
->status
.rates
[0].count
- 1;
1022 /* HW doesn't send more than 15 retries */
1023 retries
= min(retries
, 15);
1025 /* The last transmission may have been successful */
1026 legacy_success
= !!(info
->flags
& IEEE80211_TX_STAT_ACK
);
1027 /* Collect data for each rate used during failed TX attempts */
1028 for (i
= 0; i
<= retries
; ++i
) {
1029 tx_rate
= le32_to_cpu(table
->rs_table
[i
].rate_n_flags
);
1030 rs_get_tbl_info_from_mcs(tx_rate
, priv
->band
,
1031 &tbl_type
, &rs_index
);
1033 * Only collect stats if retried rate is in the same RS
1034 * table as active/search.
1036 if (table_type_matches(&tbl_type
, curr_tbl
))
1038 else if (table_type_matches(&tbl_type
, other_tbl
))
1039 tmp_tbl
= other_tbl
;
1042 rs_collect_tx_data(tmp_tbl
, rs_index
, 1,
1043 i
< retries
? 0 : legacy_success
);
1046 /* Update success/fail counts if not searching for new mode */
1047 if (lq_sta
->stay_in_tbl
) {
1048 lq_sta
->total_success
+= legacy_success
;
1049 lq_sta
->total_failed
+= retries
+ (1 - legacy_success
);
1052 /* The last TX rate is cached in lq_sta; it's set in if/else above */
1053 lq_sta
->last_rate_n_flags
= tx_rate
;
1055 /* See if there's a better rate or modulation mode to try. */
1056 if (sta
&& sta
->supp_rates
[sband
->band
])
1057 rs_rate_scale_perform(priv
, skb
, sta
, lq_sta
);
1059 if (priv
->lib
->bt_params
&& priv
->lib
->bt_params
->advanced_bt_coexist
)
1060 rs_bt_update_lq(priv
, ctx
, lq_sta
);
1064 * Begin a period of staying with a selected modulation mode.
1065 * Set "stay_in_tbl" flag to prevent any mode switches.
1066 * Set frame tx success limits according to legacy vs. high-throughput,
1067 * and reset overall (spanning all rates) tx success history statistics.
1068 * These control how long we stay using same modulation mode before
1069 * searching for a new mode.
1071 static void rs_set_stay_in_table(struct iwl_priv
*priv
, u8 is_legacy
,
1072 struct iwl_lq_sta
*lq_sta
)
1074 IWL_DEBUG_RATE(priv
, "we are staying in the same table\n");
1075 lq_sta
->stay_in_tbl
= 1; /* only place this gets set */
1077 lq_sta
->table_count_limit
= IWL_LEGACY_TABLE_COUNT
;
1078 lq_sta
->max_failure_limit
= IWL_LEGACY_FAILURE_LIMIT
;
1079 lq_sta
->max_success_limit
= IWL_LEGACY_SUCCESS_LIMIT
;
1081 lq_sta
->table_count_limit
= IWL_NONE_LEGACY_TABLE_COUNT
;
1082 lq_sta
->max_failure_limit
= IWL_NONE_LEGACY_FAILURE_LIMIT
;
1083 lq_sta
->max_success_limit
= IWL_NONE_LEGACY_SUCCESS_LIMIT
;
1085 lq_sta
->table_count
= 0;
1086 lq_sta
->total_failed
= 0;
1087 lq_sta
->total_success
= 0;
1088 lq_sta
->flush_timer
= jiffies
;
1089 lq_sta
->action_counter
= 0;
1093 * Find correct throughput table for given mode of modulation
1095 static void rs_set_expected_tpt_table(struct iwl_lq_sta
*lq_sta
,
1096 struct iwl_scale_tbl_info
*tbl
)
1098 /* Used to choose among HT tables */
1099 const u16 (*ht_tbl_pointer
)[IWL_RATE_COUNT
];
1101 /* Check for invalid LQ type */
1102 if (WARN_ON_ONCE(!is_legacy(tbl
->lq_type
) && !is_Ht(tbl
->lq_type
))) {
1103 tbl
->expected_tpt
= expected_tpt_legacy
;
1107 /* Legacy rates have only one table */
1108 if (is_legacy(tbl
->lq_type
)) {
1109 tbl
->expected_tpt
= expected_tpt_legacy
;
1113 /* Choose among many HT tables depending on number of streams
1114 * (SISO/MIMO2/MIMO3), channel width (20/40), SGI, and aggregation
1116 if (is_siso(tbl
->lq_type
) && (!tbl
->is_ht40
|| lq_sta
->is_dup
))
1117 ht_tbl_pointer
= expected_tpt_siso20MHz
;
1118 else if (is_siso(tbl
->lq_type
))
1119 ht_tbl_pointer
= expected_tpt_siso40MHz
;
1120 else if (is_mimo2(tbl
->lq_type
) && (!tbl
->is_ht40
|| lq_sta
->is_dup
))
1121 ht_tbl_pointer
= expected_tpt_mimo2_20MHz
;
1122 else if (is_mimo2(tbl
->lq_type
))
1123 ht_tbl_pointer
= expected_tpt_mimo2_40MHz
;
1124 else if (is_mimo3(tbl
->lq_type
) && (!tbl
->is_ht40
|| lq_sta
->is_dup
))
1125 ht_tbl_pointer
= expected_tpt_mimo3_20MHz
;
1126 else /* if (is_mimo3(tbl->lq_type)) <-- must be true */
1127 ht_tbl_pointer
= expected_tpt_mimo3_40MHz
;
1129 if (!tbl
->is_SGI
&& !lq_sta
->is_agg
) /* Normal */
1130 tbl
->expected_tpt
= ht_tbl_pointer
[0];
1131 else if (tbl
->is_SGI
&& !lq_sta
->is_agg
) /* SGI */
1132 tbl
->expected_tpt
= ht_tbl_pointer
[1];
1133 else if (!tbl
->is_SGI
&& lq_sta
->is_agg
) /* AGG */
1134 tbl
->expected_tpt
= ht_tbl_pointer
[2];
1136 tbl
->expected_tpt
= ht_tbl_pointer
[3];
1140 * Find starting rate for new "search" high-throughput mode of modulation.
1141 * Goal is to find lowest expected rate (under perfect conditions) that is
1142 * above the current measured throughput of "active" mode, to give new mode
1143 * a fair chance to prove itself without too many challenges.
1145 * This gets called when transitioning to more aggressive modulation
1146 * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
1147 * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
1148 * to decrease to match "active" throughput. When moving from MIMO to SISO,
1149 * bit rate will typically need to increase, but not if performance was bad.
1151 static s32
rs_get_best_rate(struct iwl_priv
*priv
,
1152 struct iwl_lq_sta
*lq_sta
,
1153 struct iwl_scale_tbl_info
*tbl
, /* "search" */
1154 u16 rate_mask
, s8 index
)
1156 /* "active" values */
1157 struct iwl_scale_tbl_info
*active_tbl
=
1158 &(lq_sta
->lq_info
[lq_sta
->active_tbl
]);
1159 s32 active_sr
= active_tbl
->win
[index
].success_ratio
;
1160 s32 active_tpt
= active_tbl
->expected_tpt
[index
];
1161 /* expected "search" throughput */
1162 const u16
*tpt_tbl
= tbl
->expected_tpt
;
1164 s32 new_rate
, high
, low
, start_hi
;
1168 new_rate
= high
= low
= start_hi
= IWL_RATE_INVALID
;
1171 high_low
= rs_get_adjacent_rate(priv
, rate
, rate_mask
,
1174 low
= high_low
& 0xff;
1175 high
= (high_low
>> 8) & 0xff;
1178 * Lower the "search" bit rate, to give new "search" mode
1179 * approximately the same throughput as "active" if:
1181 * 1) "Active" mode has been working modestly well (but not
1182 * great), and expected "search" throughput (under perfect
1183 * conditions) at candidate rate is above the actual
1184 * measured "active" throughput (but less than expected
1185 * "active" throughput under perfect conditions).
1187 * 2) "Active" mode has been working perfectly or very well
1188 * and expected "search" throughput (under perfect
1189 * conditions) at candidate rate is above expected
1190 * "active" throughput (under perfect conditions).
1192 if ((((100 * tpt_tbl
[rate
]) > lq_sta
->last_tpt
) &&
1193 ((active_sr
> IWL_RATE_DECREASE_TH
) &&
1194 (active_sr
<= IWL_RATE_HIGH_TH
) &&
1195 (tpt_tbl
[rate
] <= active_tpt
))) ||
1196 ((active_sr
>= IWL_RATE_SCALE_SWITCH
) &&
1197 (tpt_tbl
[rate
] > active_tpt
))) {
1199 /* (2nd or later pass)
1200 * If we've already tried to raise the rate, and are
1201 * now trying to lower it, use the higher rate. */
1202 if (start_hi
!= IWL_RATE_INVALID
) {
1203 new_rate
= start_hi
;
1209 /* Loop again with lower rate */
1210 if (low
!= IWL_RATE_INVALID
)
1213 /* Lower rate not available, use the original */
1217 /* Else try to raise the "search" rate to match "active" */
1219 /* (2nd or later pass)
1220 * If we've already tried to lower the rate, and are
1221 * now trying to raise it, use the lower rate. */
1222 if (new_rate
!= IWL_RATE_INVALID
)
1225 /* Loop again with higher rate */
1226 else if (high
!= IWL_RATE_INVALID
) {
1230 /* Higher rate not available, use the original */
1242 * Set up search table for MIMO2
1244 static int rs_switch_to_mimo2(struct iwl_priv
*priv
,
1245 struct iwl_lq_sta
*lq_sta
,
1246 struct ieee80211_conf
*conf
,
1247 struct ieee80211_sta
*sta
,
1248 struct iwl_scale_tbl_info
*tbl
, int index
)
1252 s8 is_green
= lq_sta
->is_green
;
1253 struct iwl_station_priv
*sta_priv
= (void *)sta
->drv_priv
;
1254 struct iwl_rxon_context
*ctx
= sta_priv
->ctx
;
1256 if (!conf_is_ht(conf
) || !sta
->ht_cap
.ht_supported
)
1259 if (sta
->smps_mode
== IEEE80211_SMPS_STATIC
)
1262 /* Need both Tx chains/antennas to support MIMO */
1263 if (priv
->hw_params
.tx_chains_num
< 2)
1266 IWL_DEBUG_RATE(priv
, "LQ: try to switch to MIMO2\n");
1268 tbl
->lq_type
= LQ_MIMO2
;
1269 tbl
->is_dup
= lq_sta
->is_dup
;
1271 tbl
->max_search
= IWL_MAX_SEARCH
;
1272 rate_mask
= lq_sta
->active_mimo2_rate
;
1274 if (iwl_is_ht40_tx_allowed(priv
, ctx
, sta
))
1279 rs_set_expected_tpt_table(lq_sta
, tbl
);
1281 rate
= rs_get_best_rate(priv
, lq_sta
, tbl
, rate_mask
, index
);
1283 IWL_DEBUG_RATE(priv
, "LQ: MIMO2 best rate %d mask %X\n", rate
, rate_mask
);
1284 if ((rate
== IWL_RATE_INVALID
) || !((1 << rate
) & rate_mask
)) {
1285 IWL_DEBUG_RATE(priv
, "Can't switch with index %d rate mask %x\n",
1289 tbl
->current_rate
= rate_n_flags_from_tbl(priv
, tbl
, rate
, is_green
);
1291 IWL_DEBUG_RATE(priv
, "LQ: Switch to new mcs %X index is green %X\n",
1292 tbl
->current_rate
, is_green
);
1297 * Set up search table for MIMO3
1299 static int rs_switch_to_mimo3(struct iwl_priv
*priv
,
1300 struct iwl_lq_sta
*lq_sta
,
1301 struct ieee80211_conf
*conf
,
1302 struct ieee80211_sta
*sta
,
1303 struct iwl_scale_tbl_info
*tbl
, int index
)
1307 s8 is_green
= lq_sta
->is_green
;
1308 struct iwl_station_priv
*sta_priv
= (void *)sta
->drv_priv
;
1309 struct iwl_rxon_context
*ctx
= sta_priv
->ctx
;
1311 if (!conf_is_ht(conf
) || !sta
->ht_cap
.ht_supported
)
1314 if (sta
->smps_mode
== IEEE80211_SMPS_STATIC
)
1317 /* Need both Tx chains/antennas to support MIMO */
1318 if (priv
->hw_params
.tx_chains_num
< 3)
1321 IWL_DEBUG_RATE(priv
, "LQ: try to switch to MIMO3\n");
1323 tbl
->lq_type
= LQ_MIMO3
;
1324 tbl
->is_dup
= lq_sta
->is_dup
;
1326 tbl
->max_search
= IWL_MAX_11N_MIMO3_SEARCH
;
1327 rate_mask
= lq_sta
->active_mimo3_rate
;
1329 if (iwl_is_ht40_tx_allowed(priv
, ctx
, sta
))
1334 rs_set_expected_tpt_table(lq_sta
, tbl
);
1336 rate
= rs_get_best_rate(priv
, lq_sta
, tbl
, rate_mask
, index
);
1338 IWL_DEBUG_RATE(priv
, "LQ: MIMO3 best rate %d mask %X\n",
1340 if ((rate
== IWL_RATE_INVALID
) || !((1 << rate
) & rate_mask
)) {
1341 IWL_DEBUG_RATE(priv
, "Can't switch with index %d rate mask %x\n",
1345 tbl
->current_rate
= rate_n_flags_from_tbl(priv
, tbl
, rate
, is_green
);
1347 IWL_DEBUG_RATE(priv
, "LQ: Switch to new mcs %X index is green %X\n",
1348 tbl
->current_rate
, is_green
);
1353 * Set up search table for SISO
1355 static int rs_switch_to_siso(struct iwl_priv
*priv
,
1356 struct iwl_lq_sta
*lq_sta
,
1357 struct ieee80211_conf
*conf
,
1358 struct ieee80211_sta
*sta
,
1359 struct iwl_scale_tbl_info
*tbl
, int index
)
1362 u8 is_green
= lq_sta
->is_green
;
1364 struct iwl_station_priv
*sta_priv
= (void *)sta
->drv_priv
;
1365 struct iwl_rxon_context
*ctx
= sta_priv
->ctx
;
1367 if (!conf_is_ht(conf
) || !sta
->ht_cap
.ht_supported
)
1370 IWL_DEBUG_RATE(priv
, "LQ: try to switch to SISO\n");
1372 tbl
->is_dup
= lq_sta
->is_dup
;
1373 tbl
->lq_type
= LQ_SISO
;
1375 tbl
->max_search
= IWL_MAX_SEARCH
;
1376 rate_mask
= lq_sta
->active_siso_rate
;
1378 if (iwl_is_ht40_tx_allowed(priv
, ctx
, sta
))
1384 tbl
->is_SGI
= 0; /*11n spec: no SGI in SISO+Greenfield*/
1386 rs_set_expected_tpt_table(lq_sta
, tbl
);
1387 rate
= rs_get_best_rate(priv
, lq_sta
, tbl
, rate_mask
, index
);
1389 IWL_DEBUG_RATE(priv
, "LQ: get best rate %d mask %X\n", rate
, rate_mask
);
1390 if ((rate
== IWL_RATE_INVALID
) || !((1 << rate
) & rate_mask
)) {
1391 IWL_DEBUG_RATE(priv
, "can not switch with index %d rate mask %x\n",
1395 tbl
->current_rate
= rate_n_flags_from_tbl(priv
, tbl
, rate
, is_green
);
1396 IWL_DEBUG_RATE(priv
, "LQ: Switch to new mcs %X index is green %X\n",
1397 tbl
->current_rate
, is_green
);
1402 * Try to switch to new modulation mode from legacy
1404 static void rs_move_legacy_other(struct iwl_priv
*priv
,
1405 struct iwl_lq_sta
*lq_sta
,
1406 struct ieee80211_conf
*conf
,
1407 struct ieee80211_sta
*sta
,
1410 struct iwl_scale_tbl_info
*tbl
= &(lq_sta
->lq_info
[lq_sta
->active_tbl
]);
1411 struct iwl_scale_tbl_info
*search_tbl
=
1412 &(lq_sta
->lq_info
[(1 - lq_sta
->active_tbl
)]);
1413 struct iwl_rate_scale_data
*window
= &(tbl
->win
[index
]);
1414 u32 sz
= (sizeof(struct iwl_scale_tbl_info
) -
1415 (sizeof(struct iwl_rate_scale_data
) * IWL_RATE_COUNT
));
1417 u8 valid_tx_ant
= priv
->nvm_data
->valid_tx_ant
;
1418 u8 tx_chains_num
= priv
->hw_params
.tx_chains_num
;
1420 u8 update_search_tbl_counter
= 0;
1422 switch (priv
->bt_traffic_load
) {
1423 case IWL_BT_COEX_TRAFFIC_LOAD_NONE
:
1426 case IWL_BT_COEX_TRAFFIC_LOAD_LOW
:
1427 /* avoid antenna B unless MIMO */
1428 if (tbl
->action
== IWL_LEGACY_SWITCH_ANTENNA2
)
1429 tbl
->action
= IWL_LEGACY_SWITCH_SISO
;
1431 case IWL_BT_COEX_TRAFFIC_LOAD_HIGH
:
1432 case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS
:
1433 /* avoid antenna B and MIMO */
1435 first_antenna(priv
->nvm_data
->valid_tx_ant
);
1436 if (tbl
->action
>= IWL_LEGACY_SWITCH_ANTENNA2
&&
1437 tbl
->action
!= IWL_LEGACY_SWITCH_SISO
)
1438 tbl
->action
= IWL_LEGACY_SWITCH_SISO
;
1441 IWL_ERR(priv
, "Invalid BT load %d\n", priv
->bt_traffic_load
);
1445 if (!iwl_ht_enabled(priv
))
1446 /* stay in Legacy */
1447 tbl
->action
= IWL_LEGACY_SWITCH_ANTENNA1
;
1448 else if (iwl_tx_ant_restriction(priv
) == IWL_ANT_OK_SINGLE
&&
1449 tbl
->action
> IWL_LEGACY_SWITCH_SISO
)
1450 tbl
->action
= IWL_LEGACY_SWITCH_SISO
;
1452 /* configure as 1x1 if bt full concurrency */
1453 if (priv
->bt_full_concurrent
) {
1454 if (!iwl_ht_enabled(priv
))
1455 tbl
->action
= IWL_LEGACY_SWITCH_ANTENNA1
;
1456 else if (tbl
->action
>= IWL_LEGACY_SWITCH_ANTENNA2
)
1457 tbl
->action
= IWL_LEGACY_SWITCH_SISO
;
1459 first_antenna(priv
->nvm_data
->valid_tx_ant
);
1462 start_action
= tbl
->action
;
1464 lq_sta
->action_counter
++;
1465 switch (tbl
->action
) {
1466 case IWL_LEGACY_SWITCH_ANTENNA1
:
1467 case IWL_LEGACY_SWITCH_ANTENNA2
:
1468 IWL_DEBUG_RATE(priv
, "LQ: Legacy toggle Antenna\n");
1470 if ((tbl
->action
== IWL_LEGACY_SWITCH_ANTENNA1
&&
1471 tx_chains_num
<= 1) ||
1472 (tbl
->action
== IWL_LEGACY_SWITCH_ANTENNA2
&&
1473 tx_chains_num
<= 2))
1476 /* Don't change antenna if success has been great */
1477 if (window
->success_ratio
>= IWL_RS_GOOD_RATIO
&&
1478 !priv
->bt_full_concurrent
&&
1479 priv
->bt_traffic_load
==
1480 IWL_BT_COEX_TRAFFIC_LOAD_NONE
)
1483 /* Set up search table to try other antenna */
1484 memcpy(search_tbl
, tbl
, sz
);
1486 if (rs_toggle_antenna(valid_tx_ant
,
1487 &search_tbl
->current_rate
, search_tbl
)) {
1488 update_search_tbl_counter
= 1;
1489 rs_set_expected_tpt_table(lq_sta
, search_tbl
);
1493 case IWL_LEGACY_SWITCH_SISO
:
1494 IWL_DEBUG_RATE(priv
, "LQ: Legacy switch to SISO\n");
1496 /* Set up search table to try SISO */
1497 memcpy(search_tbl
, tbl
, sz
);
1498 search_tbl
->is_SGI
= 0;
1499 ret
= rs_switch_to_siso(priv
, lq_sta
, conf
, sta
,
1502 lq_sta
->action_counter
= 0;
1507 case IWL_LEGACY_SWITCH_MIMO2_AB
:
1508 case IWL_LEGACY_SWITCH_MIMO2_AC
:
1509 case IWL_LEGACY_SWITCH_MIMO2_BC
:
1510 IWL_DEBUG_RATE(priv
, "LQ: Legacy switch to MIMO2\n");
1512 /* Set up search table to try MIMO */
1513 memcpy(search_tbl
, tbl
, sz
);
1514 search_tbl
->is_SGI
= 0;
1516 if (tbl
->action
== IWL_LEGACY_SWITCH_MIMO2_AB
)
1517 search_tbl
->ant_type
= ANT_AB
;
1518 else if (tbl
->action
== IWL_LEGACY_SWITCH_MIMO2_AC
)
1519 search_tbl
->ant_type
= ANT_AC
;
1521 search_tbl
->ant_type
= ANT_BC
;
1523 if (!rs_is_valid_ant(valid_tx_ant
, search_tbl
->ant_type
))
1526 ret
= rs_switch_to_mimo2(priv
, lq_sta
, conf
, sta
,
1529 lq_sta
->action_counter
= 0;
1534 case IWL_LEGACY_SWITCH_MIMO3_ABC
:
1535 IWL_DEBUG_RATE(priv
, "LQ: Legacy switch to MIMO3\n");
1537 /* Set up search table to try MIMO3 */
1538 memcpy(search_tbl
, tbl
, sz
);
1539 search_tbl
->is_SGI
= 0;
1541 search_tbl
->ant_type
= ANT_ABC
;
1543 if (!rs_is_valid_ant(valid_tx_ant
, search_tbl
->ant_type
))
1546 ret
= rs_switch_to_mimo3(priv
, lq_sta
, conf
, sta
,
1549 lq_sta
->action_counter
= 0;
1555 if (tbl
->action
> IWL_LEGACY_SWITCH_MIMO3_ABC
)
1556 tbl
->action
= IWL_LEGACY_SWITCH_ANTENNA1
;
1558 if (tbl
->action
== start_action
)
1562 search_tbl
->lq_type
= LQ_NONE
;
1566 lq_sta
->search_better_tbl
= 1;
1568 if (tbl
->action
> IWL_LEGACY_SWITCH_MIMO3_ABC
)
1569 tbl
->action
= IWL_LEGACY_SWITCH_ANTENNA1
;
1570 if (update_search_tbl_counter
)
1571 search_tbl
->action
= tbl
->action
;
1575 * Try to switch to new modulation mode from SISO
1577 static void rs_move_siso_to_other(struct iwl_priv
*priv
,
1578 struct iwl_lq_sta
*lq_sta
,
1579 struct ieee80211_conf
*conf
,
1580 struct ieee80211_sta
*sta
, int index
)
1582 u8 is_green
= lq_sta
->is_green
;
1583 struct iwl_scale_tbl_info
*tbl
= &(lq_sta
->lq_info
[lq_sta
->active_tbl
]);
1584 struct iwl_scale_tbl_info
*search_tbl
=
1585 &(lq_sta
->lq_info
[(1 - lq_sta
->active_tbl
)]);
1586 struct iwl_rate_scale_data
*window
= &(tbl
->win
[index
]);
1587 struct ieee80211_sta_ht_cap
*ht_cap
= &sta
->ht_cap
;
1588 u32 sz
= (sizeof(struct iwl_scale_tbl_info
) -
1589 (sizeof(struct iwl_rate_scale_data
) * IWL_RATE_COUNT
));
1591 u8 valid_tx_ant
= priv
->nvm_data
->valid_tx_ant
;
1592 u8 tx_chains_num
= priv
->hw_params
.tx_chains_num
;
1593 u8 update_search_tbl_counter
= 0;
1596 switch (priv
->bt_traffic_load
) {
1597 case IWL_BT_COEX_TRAFFIC_LOAD_NONE
:
1600 case IWL_BT_COEX_TRAFFIC_LOAD_LOW
:
1601 /* avoid antenna B unless MIMO */
1602 if (tbl
->action
== IWL_SISO_SWITCH_ANTENNA2
)
1603 tbl
->action
= IWL_SISO_SWITCH_MIMO2_AB
;
1605 case IWL_BT_COEX_TRAFFIC_LOAD_HIGH
:
1606 case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS
:
1607 /* avoid antenna B and MIMO */
1609 first_antenna(priv
->nvm_data
->valid_tx_ant
);
1610 if (tbl
->action
!= IWL_SISO_SWITCH_ANTENNA1
)
1611 tbl
->action
= IWL_SISO_SWITCH_ANTENNA1
;
1614 IWL_ERR(priv
, "Invalid BT load %d\n", priv
->bt_traffic_load
);
1618 if (iwl_tx_ant_restriction(priv
) == IWL_ANT_OK_SINGLE
&&
1619 tbl
->action
> IWL_SISO_SWITCH_ANTENNA2
) {
1621 tbl
->action
= IWL_SISO_SWITCH_ANTENNA1
;
1624 /* configure as 1x1 if bt full concurrency */
1625 if (priv
->bt_full_concurrent
) {
1627 first_antenna(priv
->nvm_data
->valid_tx_ant
);
1628 if (tbl
->action
>= IWL_LEGACY_SWITCH_ANTENNA2
)
1629 tbl
->action
= IWL_SISO_SWITCH_ANTENNA1
;
1632 start_action
= tbl
->action
;
1634 lq_sta
->action_counter
++;
1635 switch (tbl
->action
) {
1636 case IWL_SISO_SWITCH_ANTENNA1
:
1637 case IWL_SISO_SWITCH_ANTENNA2
:
1638 IWL_DEBUG_RATE(priv
, "LQ: SISO toggle Antenna\n");
1639 if ((tbl
->action
== IWL_SISO_SWITCH_ANTENNA1
&&
1640 tx_chains_num
<= 1) ||
1641 (tbl
->action
== IWL_SISO_SWITCH_ANTENNA2
&&
1642 tx_chains_num
<= 2))
1645 if (window
->success_ratio
>= IWL_RS_GOOD_RATIO
&&
1646 !priv
->bt_full_concurrent
&&
1647 priv
->bt_traffic_load
==
1648 IWL_BT_COEX_TRAFFIC_LOAD_NONE
)
1651 memcpy(search_tbl
, tbl
, sz
);
1652 if (rs_toggle_antenna(valid_tx_ant
,
1653 &search_tbl
->current_rate
, search_tbl
)) {
1654 update_search_tbl_counter
= 1;
1658 case IWL_SISO_SWITCH_MIMO2_AB
:
1659 case IWL_SISO_SWITCH_MIMO2_AC
:
1660 case IWL_SISO_SWITCH_MIMO2_BC
:
1661 IWL_DEBUG_RATE(priv
, "LQ: SISO switch to MIMO2\n");
1662 memcpy(search_tbl
, tbl
, sz
);
1663 search_tbl
->is_SGI
= 0;
1665 if (tbl
->action
== IWL_SISO_SWITCH_MIMO2_AB
)
1666 search_tbl
->ant_type
= ANT_AB
;
1667 else if (tbl
->action
== IWL_SISO_SWITCH_MIMO2_AC
)
1668 search_tbl
->ant_type
= ANT_AC
;
1670 search_tbl
->ant_type
= ANT_BC
;
1672 if (!rs_is_valid_ant(valid_tx_ant
, search_tbl
->ant_type
))
1675 ret
= rs_switch_to_mimo2(priv
, lq_sta
, conf
, sta
,
1680 case IWL_SISO_SWITCH_GI
:
1681 if (!tbl
->is_ht40
&& !(ht_cap
->cap
&
1682 IEEE80211_HT_CAP_SGI_20
))
1684 if (tbl
->is_ht40
&& !(ht_cap
->cap
&
1685 IEEE80211_HT_CAP_SGI_40
))
1688 IWL_DEBUG_RATE(priv
, "LQ: SISO toggle SGI/NGI\n");
1690 memcpy(search_tbl
, tbl
, sz
);
1696 "SGI was set in GF+SISO\n");
1698 search_tbl
->is_SGI
= !tbl
->is_SGI
;
1699 rs_set_expected_tpt_table(lq_sta
, search_tbl
);
1701 s32 tpt
= lq_sta
->last_tpt
/ 100;
1702 if (tpt
>= search_tbl
->expected_tpt
[index
])
1705 search_tbl
->current_rate
=
1706 rate_n_flags_from_tbl(priv
, search_tbl
,
1708 update_search_tbl_counter
= 1;
1710 case IWL_SISO_SWITCH_MIMO3_ABC
:
1711 IWL_DEBUG_RATE(priv
, "LQ: SISO switch to MIMO3\n");
1712 memcpy(search_tbl
, tbl
, sz
);
1713 search_tbl
->is_SGI
= 0;
1714 search_tbl
->ant_type
= ANT_ABC
;
1716 if (!rs_is_valid_ant(valid_tx_ant
, search_tbl
->ant_type
))
1719 ret
= rs_switch_to_mimo3(priv
, lq_sta
, conf
, sta
,
1726 if (tbl
->action
> IWL_LEGACY_SWITCH_MIMO3_ABC
)
1727 tbl
->action
= IWL_SISO_SWITCH_ANTENNA1
;
1729 if (tbl
->action
== start_action
)
1732 search_tbl
->lq_type
= LQ_NONE
;
1736 lq_sta
->search_better_tbl
= 1;
1738 if (tbl
->action
> IWL_SISO_SWITCH_MIMO3_ABC
)
1739 tbl
->action
= IWL_SISO_SWITCH_ANTENNA1
;
1740 if (update_search_tbl_counter
)
1741 search_tbl
->action
= tbl
->action
;
1745 * Try to switch to new modulation mode from MIMO2
1747 static void rs_move_mimo2_to_other(struct iwl_priv
*priv
,
1748 struct iwl_lq_sta
*lq_sta
,
1749 struct ieee80211_conf
*conf
,
1750 struct ieee80211_sta
*sta
, int index
)
1752 s8 is_green
= lq_sta
->is_green
;
1753 struct iwl_scale_tbl_info
*tbl
= &(lq_sta
->lq_info
[lq_sta
->active_tbl
]);
1754 struct iwl_scale_tbl_info
*search_tbl
=
1755 &(lq_sta
->lq_info
[(1 - lq_sta
->active_tbl
)]);
1756 struct iwl_rate_scale_data
*window
= &(tbl
->win
[index
]);
1757 struct ieee80211_sta_ht_cap
*ht_cap
= &sta
->ht_cap
;
1758 u32 sz
= (sizeof(struct iwl_scale_tbl_info
) -
1759 (sizeof(struct iwl_rate_scale_data
) * IWL_RATE_COUNT
));
1761 u8 valid_tx_ant
= priv
->nvm_data
->valid_tx_ant
;
1762 u8 tx_chains_num
= priv
->hw_params
.tx_chains_num
;
1763 u8 update_search_tbl_counter
= 0;
1766 switch (priv
->bt_traffic_load
) {
1767 case IWL_BT_COEX_TRAFFIC_LOAD_NONE
:
1770 case IWL_BT_COEX_TRAFFIC_LOAD_HIGH
:
1771 case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS
:
1772 /* avoid antenna B and MIMO */
1773 if (tbl
->action
!= IWL_MIMO2_SWITCH_SISO_A
)
1774 tbl
->action
= IWL_MIMO2_SWITCH_SISO_A
;
1776 case IWL_BT_COEX_TRAFFIC_LOAD_LOW
:
1777 /* avoid antenna B unless MIMO */
1778 if (tbl
->action
== IWL_MIMO2_SWITCH_SISO_B
||
1779 tbl
->action
== IWL_MIMO2_SWITCH_SISO_C
)
1780 tbl
->action
= IWL_MIMO2_SWITCH_SISO_A
;
1783 IWL_ERR(priv
, "Invalid BT load %d\n", priv
->bt_traffic_load
);
1787 if ((iwl_tx_ant_restriction(priv
) == IWL_ANT_OK_SINGLE
) &&
1788 (tbl
->action
< IWL_MIMO2_SWITCH_SISO_A
||
1789 tbl
->action
> IWL_MIMO2_SWITCH_SISO_C
)) {
1790 /* switch in SISO */
1791 tbl
->action
= IWL_MIMO2_SWITCH_SISO_A
;
1794 /* configure as 1x1 if bt full concurrency */
1795 if (priv
->bt_full_concurrent
&&
1796 (tbl
->action
< IWL_MIMO2_SWITCH_SISO_A
||
1797 tbl
->action
> IWL_MIMO2_SWITCH_SISO_C
))
1798 tbl
->action
= IWL_MIMO2_SWITCH_SISO_A
;
1800 start_action
= tbl
->action
;
1802 lq_sta
->action_counter
++;
1803 switch (tbl
->action
) {
1804 case IWL_MIMO2_SWITCH_ANTENNA1
:
1805 case IWL_MIMO2_SWITCH_ANTENNA2
:
1806 IWL_DEBUG_RATE(priv
, "LQ: MIMO2 toggle Antennas\n");
1808 if (tx_chains_num
<= 2)
1811 if (window
->success_ratio
>= IWL_RS_GOOD_RATIO
)
1814 memcpy(search_tbl
, tbl
, sz
);
1815 if (rs_toggle_antenna(valid_tx_ant
,
1816 &search_tbl
->current_rate
, search_tbl
)) {
1817 update_search_tbl_counter
= 1;
1821 case IWL_MIMO2_SWITCH_SISO_A
:
1822 case IWL_MIMO2_SWITCH_SISO_B
:
1823 case IWL_MIMO2_SWITCH_SISO_C
:
1824 IWL_DEBUG_RATE(priv
, "LQ: MIMO2 switch to SISO\n");
1826 /* Set up new search table for SISO */
1827 memcpy(search_tbl
, tbl
, sz
);
1829 if (tbl
->action
== IWL_MIMO2_SWITCH_SISO_A
)
1830 search_tbl
->ant_type
= ANT_A
;
1831 else if (tbl
->action
== IWL_MIMO2_SWITCH_SISO_B
)
1832 search_tbl
->ant_type
= ANT_B
;
1834 search_tbl
->ant_type
= ANT_C
;
1836 if (!rs_is_valid_ant(valid_tx_ant
, search_tbl
->ant_type
))
1839 ret
= rs_switch_to_siso(priv
, lq_sta
, conf
, sta
,
1846 case IWL_MIMO2_SWITCH_GI
:
1847 if (!tbl
->is_ht40
&& !(ht_cap
->cap
&
1848 IEEE80211_HT_CAP_SGI_20
))
1850 if (tbl
->is_ht40
&& !(ht_cap
->cap
&
1851 IEEE80211_HT_CAP_SGI_40
))
1854 IWL_DEBUG_RATE(priv
, "LQ: MIMO2 toggle SGI/NGI\n");
1856 /* Set up new search table for MIMO2 */
1857 memcpy(search_tbl
, tbl
, sz
);
1858 search_tbl
->is_SGI
= !tbl
->is_SGI
;
1859 rs_set_expected_tpt_table(lq_sta
, search_tbl
);
1861 * If active table already uses the fastest possible
1862 * modulation (dual stream with short guard interval),
1863 * and it's working well, there's no need to look
1864 * for a better type of modulation!
1867 s32 tpt
= lq_sta
->last_tpt
/ 100;
1868 if (tpt
>= search_tbl
->expected_tpt
[index
])
1871 search_tbl
->current_rate
=
1872 rate_n_flags_from_tbl(priv
, search_tbl
,
1874 update_search_tbl_counter
= 1;
1877 case IWL_MIMO2_SWITCH_MIMO3_ABC
:
1878 IWL_DEBUG_RATE(priv
, "LQ: MIMO2 switch to MIMO3\n");
1879 memcpy(search_tbl
, tbl
, sz
);
1880 search_tbl
->is_SGI
= 0;
1881 search_tbl
->ant_type
= ANT_ABC
;
1883 if (!rs_is_valid_ant(valid_tx_ant
, search_tbl
->ant_type
))
1886 ret
= rs_switch_to_mimo3(priv
, lq_sta
, conf
, sta
,
1894 if (tbl
->action
> IWL_MIMO2_SWITCH_MIMO3_ABC
)
1895 tbl
->action
= IWL_MIMO2_SWITCH_ANTENNA1
;
1897 if (tbl
->action
== start_action
)
1900 search_tbl
->lq_type
= LQ_NONE
;
1903 lq_sta
->search_better_tbl
= 1;
1905 if (tbl
->action
> IWL_MIMO2_SWITCH_MIMO3_ABC
)
1906 tbl
->action
= IWL_MIMO2_SWITCH_ANTENNA1
;
1907 if (update_search_tbl_counter
)
1908 search_tbl
->action
= tbl
->action
;
1913 * Try to switch to new modulation mode from MIMO3
1915 static void rs_move_mimo3_to_other(struct iwl_priv
*priv
,
1916 struct iwl_lq_sta
*lq_sta
,
1917 struct ieee80211_conf
*conf
,
1918 struct ieee80211_sta
*sta
, int index
)
1920 s8 is_green
= lq_sta
->is_green
;
1921 struct iwl_scale_tbl_info
*tbl
= &(lq_sta
->lq_info
[lq_sta
->active_tbl
]);
1922 struct iwl_scale_tbl_info
*search_tbl
=
1923 &(lq_sta
->lq_info
[(1 - lq_sta
->active_tbl
)]);
1924 struct iwl_rate_scale_data
*window
= &(tbl
->win
[index
]);
1925 struct ieee80211_sta_ht_cap
*ht_cap
= &sta
->ht_cap
;
1926 u32 sz
= (sizeof(struct iwl_scale_tbl_info
) -
1927 (sizeof(struct iwl_rate_scale_data
) * IWL_RATE_COUNT
));
1929 u8 valid_tx_ant
= priv
->nvm_data
->valid_tx_ant
;
1930 u8 tx_chains_num
= priv
->hw_params
.tx_chains_num
;
1932 u8 update_search_tbl_counter
= 0;
1934 switch (priv
->bt_traffic_load
) {
1935 case IWL_BT_COEX_TRAFFIC_LOAD_NONE
:
1938 case IWL_BT_COEX_TRAFFIC_LOAD_HIGH
:
1939 case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS
:
1940 /* avoid antenna B and MIMO */
1941 if (tbl
->action
!= IWL_MIMO3_SWITCH_SISO_A
)
1942 tbl
->action
= IWL_MIMO3_SWITCH_SISO_A
;
1944 case IWL_BT_COEX_TRAFFIC_LOAD_LOW
:
1945 /* avoid antenna B unless MIMO */
1946 if (tbl
->action
== IWL_MIMO3_SWITCH_SISO_B
||
1947 tbl
->action
== IWL_MIMO3_SWITCH_SISO_C
)
1948 tbl
->action
= IWL_MIMO3_SWITCH_SISO_A
;
1951 IWL_ERR(priv
, "Invalid BT load %d\n", priv
->bt_traffic_load
);
1955 if ((iwl_tx_ant_restriction(priv
) == IWL_ANT_OK_SINGLE
) &&
1956 (tbl
->action
< IWL_MIMO3_SWITCH_SISO_A
||
1957 tbl
->action
> IWL_MIMO3_SWITCH_SISO_C
)) {
1958 /* switch in SISO */
1959 tbl
->action
= IWL_MIMO3_SWITCH_SISO_A
;
1962 /* configure as 1x1 if bt full concurrency */
1963 if (priv
->bt_full_concurrent
&&
1964 (tbl
->action
< IWL_MIMO3_SWITCH_SISO_A
||
1965 tbl
->action
> IWL_MIMO3_SWITCH_SISO_C
))
1966 tbl
->action
= IWL_MIMO3_SWITCH_SISO_A
;
1968 start_action
= tbl
->action
;
1970 lq_sta
->action_counter
++;
1971 switch (tbl
->action
) {
1972 case IWL_MIMO3_SWITCH_ANTENNA1
:
1973 case IWL_MIMO3_SWITCH_ANTENNA2
:
1974 IWL_DEBUG_RATE(priv
, "LQ: MIMO3 toggle Antennas\n");
1976 if (tx_chains_num
<= 3)
1979 if (window
->success_ratio
>= IWL_RS_GOOD_RATIO
)
1982 memcpy(search_tbl
, tbl
, sz
);
1983 if (rs_toggle_antenna(valid_tx_ant
,
1984 &search_tbl
->current_rate
, search_tbl
))
1987 case IWL_MIMO3_SWITCH_SISO_A
:
1988 case IWL_MIMO3_SWITCH_SISO_B
:
1989 case IWL_MIMO3_SWITCH_SISO_C
:
1990 IWL_DEBUG_RATE(priv
, "LQ: MIMO3 switch to SISO\n");
1992 /* Set up new search table for SISO */
1993 memcpy(search_tbl
, tbl
, sz
);
1995 if (tbl
->action
== IWL_MIMO3_SWITCH_SISO_A
)
1996 search_tbl
->ant_type
= ANT_A
;
1997 else if (tbl
->action
== IWL_MIMO3_SWITCH_SISO_B
)
1998 search_tbl
->ant_type
= ANT_B
;
2000 search_tbl
->ant_type
= ANT_C
;
2002 if (!rs_is_valid_ant(valid_tx_ant
, search_tbl
->ant_type
))
2005 ret
= rs_switch_to_siso(priv
, lq_sta
, conf
, sta
,
2012 case IWL_MIMO3_SWITCH_MIMO2_AB
:
2013 case IWL_MIMO3_SWITCH_MIMO2_AC
:
2014 case IWL_MIMO3_SWITCH_MIMO2_BC
:
2015 IWL_DEBUG_RATE(priv
, "LQ: MIMO3 switch to MIMO2\n");
2017 memcpy(search_tbl
, tbl
, sz
);
2018 search_tbl
->is_SGI
= 0;
2019 if (tbl
->action
== IWL_MIMO3_SWITCH_MIMO2_AB
)
2020 search_tbl
->ant_type
= ANT_AB
;
2021 else if (tbl
->action
== IWL_MIMO3_SWITCH_MIMO2_AC
)
2022 search_tbl
->ant_type
= ANT_AC
;
2024 search_tbl
->ant_type
= ANT_BC
;
2026 if (!rs_is_valid_ant(valid_tx_ant
, search_tbl
->ant_type
))
2029 ret
= rs_switch_to_mimo2(priv
, lq_sta
, conf
, sta
,
2036 case IWL_MIMO3_SWITCH_GI
:
2037 if (!tbl
->is_ht40
&& !(ht_cap
->cap
&
2038 IEEE80211_HT_CAP_SGI_20
))
2040 if (tbl
->is_ht40
&& !(ht_cap
->cap
&
2041 IEEE80211_HT_CAP_SGI_40
))
2044 IWL_DEBUG_RATE(priv
, "LQ: MIMO3 toggle SGI/NGI\n");
2046 /* Set up new search table for MIMO */
2047 memcpy(search_tbl
, tbl
, sz
);
2048 search_tbl
->is_SGI
= !tbl
->is_SGI
;
2049 rs_set_expected_tpt_table(lq_sta
, search_tbl
);
2051 * If active table already uses the fastest possible
2052 * modulation (dual stream with short guard interval),
2053 * and it's working well, there's no need to look
2054 * for a better type of modulation!
2057 s32 tpt
= lq_sta
->last_tpt
/ 100;
2058 if (tpt
>= search_tbl
->expected_tpt
[index
])
2061 search_tbl
->current_rate
=
2062 rate_n_flags_from_tbl(priv
, search_tbl
,
2064 update_search_tbl_counter
= 1;
2068 if (tbl
->action
> IWL_MIMO3_SWITCH_GI
)
2069 tbl
->action
= IWL_MIMO3_SWITCH_ANTENNA1
;
2071 if (tbl
->action
== start_action
)
2074 search_tbl
->lq_type
= LQ_NONE
;
2077 lq_sta
->search_better_tbl
= 1;
2079 if (tbl
->action
> IWL_MIMO3_SWITCH_GI
)
2080 tbl
->action
= IWL_MIMO3_SWITCH_ANTENNA1
;
2081 if (update_search_tbl_counter
)
2082 search_tbl
->action
= tbl
->action
;
2086 * Check whether we should continue using same modulation mode, or
2087 * begin search for a new mode, based on:
2088 * 1) # tx successes or failures while using this mode
2089 * 2) # times calling this function
2090 * 3) elapsed time in this mode (not used, for now)
2092 static void rs_stay_in_table(struct iwl_lq_sta
*lq_sta
, bool force_search
)
2094 struct iwl_scale_tbl_info
*tbl
;
2097 int flush_interval_passed
= 0;
2098 struct iwl_priv
*priv
;
2101 active_tbl
= lq_sta
->active_tbl
;
2103 tbl
= &(lq_sta
->lq_info
[active_tbl
]);
2105 /* If we've been disallowing search, see if we should now allow it */
2106 if (lq_sta
->stay_in_tbl
) {
2108 /* Elapsed time using current modulation mode */
2109 if (lq_sta
->flush_timer
)
2110 flush_interval_passed
=
2112 (unsigned long)(lq_sta
->flush_timer
+
2113 IWL_RATE_SCALE_FLUSH_INTVL
));
2116 * Check if we should allow search for new modulation mode.
2117 * If many frames have failed or succeeded, or we've used
2118 * this same modulation for a long time, allow search, and
2119 * reset history stats that keep track of whether we should
2120 * allow a new search. Also (below) reset all bitmaps and
2121 * stats in active history.
2124 (lq_sta
->total_failed
> lq_sta
->max_failure_limit
) ||
2125 (lq_sta
->total_success
> lq_sta
->max_success_limit
) ||
2126 ((!lq_sta
->search_better_tbl
) && (lq_sta
->flush_timer
)
2127 && (flush_interval_passed
))) {
2128 IWL_DEBUG_RATE(priv
, "LQ: stay is expired %d %d %d\n",
2129 lq_sta
->total_failed
,
2130 lq_sta
->total_success
,
2131 flush_interval_passed
);
2133 /* Allow search for new mode */
2134 lq_sta
->stay_in_tbl
= 0; /* only place reset */
2135 lq_sta
->total_failed
= 0;
2136 lq_sta
->total_success
= 0;
2137 lq_sta
->flush_timer
= 0;
2140 * Else if we've used this modulation mode enough repetitions
2141 * (regardless of elapsed time or success/failure), reset
2142 * history bitmaps and rate-specific stats for all rates in
2146 lq_sta
->table_count
++;
2147 if (lq_sta
->table_count
>=
2148 lq_sta
->table_count_limit
) {
2149 lq_sta
->table_count
= 0;
2151 IWL_DEBUG_RATE(priv
, "LQ: stay in table clear win\n");
2152 for (i
= 0; i
< IWL_RATE_COUNT
; i
++)
2153 rs_rate_scale_clear_window(
2158 /* If transitioning to allow "search", reset all history
2159 * bitmaps and stats in active table (this will become the new
2160 * "search" table). */
2161 if (!lq_sta
->stay_in_tbl
) {
2162 for (i
= 0; i
< IWL_RATE_COUNT
; i
++)
2163 rs_rate_scale_clear_window(&(tbl
->win
[i
]));
2169 * setup rate table in uCode
2171 static void rs_update_rate_tbl(struct iwl_priv
*priv
,
2172 struct iwl_rxon_context
*ctx
,
2173 struct iwl_lq_sta
*lq_sta
,
2174 struct iwl_scale_tbl_info
*tbl
,
2175 int index
, u8 is_green
)
2179 /* Update uCode's rate table. */
2180 rate
= rate_n_flags_from_tbl(priv
, tbl
, index
, is_green
);
2181 rs_fill_link_cmd(priv
, lq_sta
, rate
);
2182 iwl_send_lq_cmd(priv
, ctx
, &lq_sta
->lq
, CMD_ASYNC
, false);
2186 * Do rate scaling and search for new modulation mode.
2188 static void rs_rate_scale_perform(struct iwl_priv
*priv
,
2189 struct sk_buff
*skb
,
2190 struct ieee80211_sta
*sta
,
2191 struct iwl_lq_sta
*lq_sta
)
2193 struct ieee80211_hw
*hw
= priv
->hw
;
2194 struct ieee80211_conf
*conf
= &hw
->conf
;
2195 struct ieee80211_tx_info
*info
= IEEE80211_SKB_CB(skb
);
2196 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*)skb
->data
;
2197 int low
= IWL_RATE_INVALID
;
2198 int high
= IWL_RATE_INVALID
;
2201 struct iwl_rate_scale_data
*window
= NULL
;
2202 int current_tpt
= IWL_INVALID_VALUE
;
2203 int low_tpt
= IWL_INVALID_VALUE
;
2204 int high_tpt
= IWL_INVALID_VALUE
;
2206 s8 scale_action
= 0;
2209 struct iwl_scale_tbl_info
*tbl
, *tbl1
;
2210 u16 rate_scale_index_msk
= 0;
2216 u8 tid
= IWL_MAX_TID_COUNT
;
2217 struct iwl_tid_data
*tid_data
;
2218 struct iwl_station_priv
*sta_priv
= (void *)sta
->drv_priv
;
2219 struct iwl_rxon_context
*ctx
= sta_priv
->ctx
;
2221 IWL_DEBUG_RATE(priv
, "rate scale calculate new rate for skb\n");
2223 /* Send management frames and NO_ACK data using lowest rate. */
2224 /* TODO: this could probably be improved.. */
2225 if (!ieee80211_is_data(hdr
->frame_control
) ||
2226 info
->flags
& IEEE80211_TX_CTL_NO_ACK
)
2229 lq_sta
->supp_rates
= sta
->supp_rates
[lq_sta
->band
];
2231 tid
= rs_tl_add_packet(lq_sta
, hdr
);
2232 if ((tid
!= IWL_MAX_TID_COUNT
) &&
2233 (lq_sta
->tx_agg_tid_en
& (1 << tid
))) {
2234 tid_data
= &priv
->tid_data
[lq_sta
->lq
.sta_id
][tid
];
2235 if (tid_data
->agg
.state
== IWL_AGG_OFF
)
2243 * Select rate-scale / modulation-mode table to work with in
2244 * the rest of this function: "search" if searching for better
2245 * modulation mode, or "active" if doing rate scaling within a mode.
2247 if (!lq_sta
->search_better_tbl
)
2248 active_tbl
= lq_sta
->active_tbl
;
2250 active_tbl
= 1 - lq_sta
->active_tbl
;
2252 tbl
= &(lq_sta
->lq_info
[active_tbl
]);
2253 if (is_legacy(tbl
->lq_type
))
2254 lq_sta
->is_green
= 0;
2256 lq_sta
->is_green
= rs_use_green(sta
);
2257 is_green
= lq_sta
->is_green
;
2259 /* current tx rate */
2260 index
= lq_sta
->last_txrate_idx
;
2262 IWL_DEBUG_RATE(priv
, "Rate scale index %d for type %d\n", index
,
2265 /* rates available for this association, and for modulation mode */
2266 rate_mask
= rs_get_supported_rates(lq_sta
, hdr
, tbl
->lq_type
);
2268 IWL_DEBUG_RATE(priv
, "mask 0x%04X\n", rate_mask
);
2270 /* mask with station rate restriction */
2271 if (is_legacy(tbl
->lq_type
)) {
2272 if (lq_sta
->band
== NL80211_BAND_5GHZ
)
2273 /* supp_rates has no CCK bits in A mode */
2274 rate_scale_index_msk
= (u16
) (rate_mask
&
2275 (lq_sta
->supp_rates
<< IWL_FIRST_OFDM_RATE
));
2277 rate_scale_index_msk
= (u16
) (rate_mask
&
2278 lq_sta
->supp_rates
);
2281 rate_scale_index_msk
= rate_mask
;
2283 if (!rate_scale_index_msk
)
2284 rate_scale_index_msk
= rate_mask
;
2286 if (!((1 << index
) & rate_scale_index_msk
)) {
2287 IWL_ERR(priv
, "Current Rate is not valid\n");
2288 if (lq_sta
->search_better_tbl
) {
2289 /* revert to active table if search table is not valid*/
2290 tbl
->lq_type
= LQ_NONE
;
2291 lq_sta
->search_better_tbl
= 0;
2292 tbl
= &(lq_sta
->lq_info
[lq_sta
->active_tbl
]);
2293 /* get "active" rate info */
2294 index
= iwl_hwrate_to_plcp_idx(tbl
->current_rate
);
2295 rs_update_rate_tbl(priv
, ctx
, lq_sta
, tbl
,
2301 /* Get expected throughput table and history window for current rate */
2302 if (!tbl
->expected_tpt
) {
2303 IWL_ERR(priv
, "tbl->expected_tpt is NULL\n");
2307 /* force user max rate if set by user */
2308 if ((lq_sta
->max_rate_idx
!= -1) &&
2309 (lq_sta
->max_rate_idx
< index
)) {
2310 index
= lq_sta
->max_rate_idx
;
2312 window
= &(tbl
->win
[index
]);
2316 window
= &(tbl
->win
[index
]);
2319 * If there is not enough history to calculate actual average
2320 * throughput, keep analyzing results of more tx frames, without
2321 * changing rate or mode (bypass most of the rest of this function).
2322 * Set up new rate table in uCode only if old rate is not supported
2323 * in current association (use new rate found above).
2325 fail_count
= window
->counter
- window
->success_counter
;
2326 if ((fail_count
< IWL_RATE_MIN_FAILURE_TH
) &&
2327 (window
->success_counter
< IWL_RATE_MIN_SUCCESS_TH
)) {
2328 IWL_DEBUG_RATE(priv
, "LQ: still below TH. succ=%d total=%d "
2330 window
->success_counter
, window
->counter
, index
);
2332 /* Can't calculate this yet; not enough history */
2333 window
->average_tpt
= IWL_INVALID_VALUE
;
2335 /* Should we stay with this modulation mode,
2336 * or search for a new one? */
2337 rs_stay_in_table(lq_sta
, false);
2341 /* Else we have enough samples; calculate estimate of
2342 * actual average throughput */
2343 if (window
->average_tpt
!= ((window
->success_ratio
*
2344 tbl
->expected_tpt
[index
] + 64) / 128)) {
2345 IWL_ERR(priv
, "expected_tpt should have been calculated by now\n");
2346 window
->average_tpt
= ((window
->success_ratio
*
2347 tbl
->expected_tpt
[index
] + 64) / 128);
2350 /* If we are searching for better modulation mode, check success. */
2351 if (lq_sta
->search_better_tbl
&&
2352 (iwl_tx_ant_restriction(priv
) == IWL_ANT_OK_MULTI
)) {
2353 /* If good success, continue using the "search" mode;
2354 * no need to send new link quality command, since we're
2355 * continuing to use the setup that we've been trying. */
2356 if (window
->average_tpt
> lq_sta
->last_tpt
) {
2358 IWL_DEBUG_RATE(priv
, "LQ: SWITCHING TO NEW TABLE "
2359 "suc=%d cur-tpt=%d old-tpt=%d\n",
2360 window
->success_ratio
,
2361 window
->average_tpt
,
2364 if (!is_legacy(tbl
->lq_type
))
2365 lq_sta
->enable_counter
= 1;
2367 /* Swap tables; "search" becomes "active" */
2368 lq_sta
->active_tbl
= active_tbl
;
2369 current_tpt
= window
->average_tpt
;
2371 /* Else poor success; go back to mode in "active" table */
2374 IWL_DEBUG_RATE(priv
, "LQ: GOING BACK TO THE OLD TABLE "
2375 "suc=%d cur-tpt=%d old-tpt=%d\n",
2376 window
->success_ratio
,
2377 window
->average_tpt
,
2380 /* Nullify "search" table */
2381 tbl
->lq_type
= LQ_NONE
;
2383 /* Revert to "active" table */
2384 active_tbl
= lq_sta
->active_tbl
;
2385 tbl
= &(lq_sta
->lq_info
[active_tbl
]);
2387 /* Revert to "active" rate and throughput info */
2388 index
= iwl_hwrate_to_plcp_idx(tbl
->current_rate
);
2389 current_tpt
= lq_sta
->last_tpt
;
2391 /* Need to set up a new rate table in uCode */
2395 /* Either way, we've made a decision; modulation mode
2396 * search is done, allow rate adjustment next time. */
2397 lq_sta
->search_better_tbl
= 0;
2398 done_search
= 1; /* Don't switch modes below! */
2402 /* (Else) not in search of better modulation mode, try for better
2403 * starting rate, while staying in this mode. */
2404 high_low
= rs_get_adjacent_rate(priv
, index
, rate_scale_index_msk
,
2406 low
= high_low
& 0xff;
2407 high
= (high_low
>> 8) & 0xff;
2409 /* If user set max rate, dont allow higher than user constrain */
2410 if ((lq_sta
->max_rate_idx
!= -1) &&
2411 (lq_sta
->max_rate_idx
< high
))
2412 high
= IWL_RATE_INVALID
;
2414 sr
= window
->success_ratio
;
2416 /* Collect measured throughputs for current and adjacent rates */
2417 current_tpt
= window
->average_tpt
;
2418 if (low
!= IWL_RATE_INVALID
)
2419 low_tpt
= tbl
->win
[low
].average_tpt
;
2420 if (high
!= IWL_RATE_INVALID
)
2421 high_tpt
= tbl
->win
[high
].average_tpt
;
2425 /* Too many failures, decrease rate */
2426 if ((sr
<= IWL_RATE_DECREASE_TH
) || (current_tpt
== 0)) {
2427 IWL_DEBUG_RATE(priv
, "decrease rate because of low success_ratio\n");
2430 /* No throughput measured yet for adjacent rates; try increase. */
2431 } else if ((low_tpt
== IWL_INVALID_VALUE
) &&
2432 (high_tpt
== IWL_INVALID_VALUE
)) {
2434 if (high
!= IWL_RATE_INVALID
&& sr
>= IWL_RATE_INCREASE_TH
)
2436 else if (low
!= IWL_RATE_INVALID
)
2440 /* Both adjacent throughputs are measured, but neither one has better
2441 * throughput; we're using the best rate, don't change it! */
2442 else if ((low_tpt
!= IWL_INVALID_VALUE
) &&
2443 (high_tpt
!= IWL_INVALID_VALUE
) &&
2444 (low_tpt
< current_tpt
) &&
2445 (high_tpt
< current_tpt
))
2448 /* At least one adjacent rate's throughput is measured,
2449 * and may have better performance. */
2451 /* Higher adjacent rate's throughput is measured */
2452 if (high_tpt
!= IWL_INVALID_VALUE
) {
2453 /* Higher rate has better throughput */
2454 if (high_tpt
> current_tpt
&&
2455 sr
>= IWL_RATE_INCREASE_TH
) {
2461 /* Lower adjacent rate's throughput is measured */
2462 } else if (low_tpt
!= IWL_INVALID_VALUE
) {
2463 /* Lower rate has better throughput */
2464 if (low_tpt
> current_tpt
) {
2465 IWL_DEBUG_RATE(priv
,
2466 "decrease rate because of low tpt\n");
2468 } else if (sr
>= IWL_RATE_INCREASE_TH
) {
2474 /* Sanity check; asked for decrease, but success rate or throughput
2475 * has been good at old rate. Don't change it. */
2476 if ((scale_action
== -1) && (low
!= IWL_RATE_INVALID
) &&
2477 ((sr
> IWL_RATE_HIGH_TH
) ||
2478 (current_tpt
> (100 * tbl
->expected_tpt
[low
]))))
2480 if (!iwl_ht_enabled(priv
) && !is_legacy(tbl
->lq_type
))
2482 if (iwl_tx_ant_restriction(priv
) != IWL_ANT_OK_MULTI
&&
2483 (is_mimo2(tbl
->lq_type
) || is_mimo3(tbl
->lq_type
)))
2486 if ((priv
->bt_traffic_load
>= IWL_BT_COEX_TRAFFIC_LOAD_HIGH
) &&
2487 (is_mimo2(tbl
->lq_type
) || is_mimo3(tbl
->lq_type
))) {
2488 if (lq_sta
->last_bt_traffic
> priv
->bt_traffic_load
) {
2490 * don't set scale_action, don't want to scale up if
2491 * the rate scale doesn't otherwise think that is a
2494 } else if (lq_sta
->last_bt_traffic
<= priv
->bt_traffic_load
) {
2498 lq_sta
->last_bt_traffic
= priv
->bt_traffic_load
;
2500 if ((priv
->bt_traffic_load
>= IWL_BT_COEX_TRAFFIC_LOAD_HIGH
) &&
2501 (is_mimo2(tbl
->lq_type
) || is_mimo3(tbl
->lq_type
))) {
2502 /* search for a new modulation */
2503 rs_stay_in_table(lq_sta
, true);
2507 switch (scale_action
) {
2509 /* Decrease starting rate, update uCode's rate table */
2510 if (low
!= IWL_RATE_INVALID
) {
2517 /* Increase starting rate, update uCode's rate table */
2518 if (high
!= IWL_RATE_INVALID
) {
2530 IWL_DEBUG_RATE(priv
, "choose rate scale index %d action %d low %d "
2531 "high %d type %d\n",
2532 index
, scale_action
, low
, high
, tbl
->lq_type
);
2535 /* Replace uCode's rate table for the destination station. */
2537 rs_update_rate_tbl(priv
, ctx
, lq_sta
, tbl
, index
, is_green
);
2539 if (iwl_tx_ant_restriction(priv
) == IWL_ANT_OK_MULTI
) {
2540 /* Should we stay with this modulation mode,
2541 * or search for a new one? */
2542 rs_stay_in_table(lq_sta
, false);
2545 * Search for new modulation mode if we're:
2546 * 1) Not changing rates right now
2547 * 2) Not just finishing up a search
2548 * 3) Allowing a new search
2550 if (!update_lq
&& !done_search
&& !lq_sta
->stay_in_tbl
&& window
->counter
) {
2551 /* Save current throughput to compare with "search" throughput*/
2552 lq_sta
->last_tpt
= current_tpt
;
2554 /* Select a new "search" modulation mode to try.
2555 * If one is found, set up the new "search" table. */
2556 if (is_legacy(tbl
->lq_type
))
2557 rs_move_legacy_other(priv
, lq_sta
, conf
, sta
, index
);
2558 else if (is_siso(tbl
->lq_type
))
2559 rs_move_siso_to_other(priv
, lq_sta
, conf
, sta
, index
);
2560 else if (is_mimo2(tbl
->lq_type
))
2561 rs_move_mimo2_to_other(priv
, lq_sta
, conf
, sta
, index
);
2563 rs_move_mimo3_to_other(priv
, lq_sta
, conf
, sta
, index
);
2565 /* If new "search" mode was selected, set up in uCode table */
2566 if (lq_sta
->search_better_tbl
) {
2567 /* Access the "search" table, clear its history. */
2568 tbl
= &(lq_sta
->lq_info
[(1 - lq_sta
->active_tbl
)]);
2569 for (i
= 0; i
< IWL_RATE_COUNT
; i
++)
2570 rs_rate_scale_clear_window(&(tbl
->win
[i
]));
2572 /* Use new "search" start rate */
2573 index
= iwl_hwrate_to_plcp_idx(tbl
->current_rate
);
2575 IWL_DEBUG_RATE(priv
, "Switch current mcs: %X index: %d\n",
2576 tbl
->current_rate
, index
);
2577 rs_fill_link_cmd(priv
, lq_sta
, tbl
->current_rate
);
2578 iwl_send_lq_cmd(priv
, ctx
, &lq_sta
->lq
, CMD_ASYNC
, false);
2583 if (done_search
&& !lq_sta
->stay_in_tbl
) {
2584 /* If the "active" (non-search) mode was legacy,
2585 * and we've tried switching antennas,
2586 * but we haven't been able to try HT modes (not available),
2587 * stay with best antenna legacy modulation for a while
2588 * before next round of mode comparisons. */
2589 tbl1
= &(lq_sta
->lq_info
[lq_sta
->active_tbl
]);
2590 if (is_legacy(tbl1
->lq_type
) && !conf_is_ht(conf
) &&
2591 lq_sta
->action_counter
> tbl1
->max_search
) {
2592 IWL_DEBUG_RATE(priv
, "LQ: STAY in legacy table\n");
2593 rs_set_stay_in_table(priv
, 1, lq_sta
);
2596 /* If we're in an HT mode, and all 3 mode switch actions
2597 * have been tried and compared, stay in this best modulation
2598 * mode for a while before next round of mode comparisons. */
2599 if (lq_sta
->enable_counter
&&
2600 (lq_sta
->action_counter
>= tbl1
->max_search
) &&
2601 iwl_ht_enabled(priv
)) {
2602 if ((lq_sta
->last_tpt
> IWL_AGG_TPT_THREHOLD
) &&
2603 (lq_sta
->tx_agg_tid_en
& (1 << tid
)) &&
2604 (tid
!= IWL_MAX_TID_COUNT
)) {
2605 u8 sta_id
= lq_sta
->lq
.sta_id
;
2606 tid_data
= &priv
->tid_data
[sta_id
][tid
];
2607 if (tid_data
->agg
.state
== IWL_AGG_OFF
) {
2608 IWL_DEBUG_RATE(priv
,
2609 "try to aggregate tid %d\n",
2611 rs_tl_turn_on_agg(priv
, tid
,
2615 rs_set_stay_in_table(priv
, 0, lq_sta
);
2620 tbl
->current_rate
= rate_n_flags_from_tbl(priv
, tbl
, index
, is_green
);
2621 lq_sta
->last_txrate_idx
= index
;
2625 * rs_initialize_lq - Initialize a station's hardware rate table
2627 * The uCode's station table contains a table of fallback rates
2628 * for automatic fallback during transmission.
2630 * NOTE: This sets up a default set of values. These will be replaced later
2631 * if the driver's iwl-agn-rs rate scaling algorithm is used, instead of
2634 * NOTE: Run REPLY_ADD_STA command to set up station table entry, before
2635 * calling this function (which runs REPLY_TX_LINK_QUALITY_CMD,
2636 * which requires station table entry to exist).
2638 static void rs_initialize_lq(struct iwl_priv
*priv
,
2639 struct ieee80211_sta
*sta
,
2640 struct iwl_lq_sta
*lq_sta
)
2642 struct iwl_scale_tbl_info
*tbl
;
2646 u8 use_green
= rs_use_green(sta
);
2649 struct iwl_station_priv
*sta_priv
;
2650 struct iwl_rxon_context
*ctx
;
2652 if (!sta
|| !lq_sta
)
2655 sta_priv
= (void *)sta
->drv_priv
;
2656 ctx
= sta_priv
->ctx
;
2658 i
= lq_sta
->last_txrate_idx
;
2660 valid_tx_ant
= priv
->nvm_data
->valid_tx_ant
;
2662 if (!lq_sta
->search_better_tbl
)
2663 active_tbl
= lq_sta
->active_tbl
;
2665 active_tbl
= 1 - lq_sta
->active_tbl
;
2667 tbl
= &(lq_sta
->lq_info
[active_tbl
]);
2669 if ((i
< 0) || (i
>= IWL_RATE_COUNT
))
2672 rate
= iwl_rates
[i
].plcp
;
2673 tbl
->ant_type
= first_antenna(valid_tx_ant
);
2674 rate
|= tbl
->ant_type
<< RATE_MCS_ANT_POS
;
2676 if (i
>= IWL_FIRST_CCK_RATE
&& i
<= IWL_LAST_CCK_RATE
)
2677 rate
|= RATE_MCS_CCK_MSK
;
2679 rs_get_tbl_info_from_mcs(rate
, priv
->band
, tbl
, &rate_idx
);
2680 if (!rs_is_valid_ant(valid_tx_ant
, tbl
->ant_type
))
2681 rs_toggle_antenna(valid_tx_ant
, &rate
, tbl
);
2683 rate
= rate_n_flags_from_tbl(priv
, tbl
, rate_idx
, use_green
);
2684 tbl
->current_rate
= rate
;
2685 rs_set_expected_tpt_table(lq_sta
, tbl
);
2686 rs_fill_link_cmd(NULL
, lq_sta
, rate
);
2687 priv
->stations
[lq_sta
->lq
.sta_id
].lq
= &lq_sta
->lq
;
2688 iwl_send_lq_cmd(priv
, ctx
, &lq_sta
->lq
, 0, true);
2691 static void rs_get_rate(void *priv_r
, struct ieee80211_sta
*sta
, void *priv_sta
,
2692 struct ieee80211_tx_rate_control
*txrc
)
2695 struct sk_buff
*skb
= txrc
->skb
;
2696 struct ieee80211_supported_band
*sband
= txrc
->sband
;
2697 struct iwl_op_mode
*op_mode __maybe_unused
=
2698 (struct iwl_op_mode
*)priv_r
;
2699 struct iwl_priv
*priv __maybe_unused
= IWL_OP_MODE_GET_DVM(op_mode
);
2700 struct ieee80211_tx_info
*info
= IEEE80211_SKB_CB(skb
);
2701 struct iwl_lq_sta
*lq_sta
= priv_sta
;
2704 IWL_DEBUG_RATE_LIMIT(priv
, "rate scale calculate new rate for skb\n");
2706 /* Get max rate if user set max rate */
2708 lq_sta
->max_rate_idx
= fls(txrc
->rate_idx_mask
) - 1;
2709 if ((sband
->band
== NL80211_BAND_5GHZ
) &&
2710 (lq_sta
->max_rate_idx
!= -1))
2711 lq_sta
->max_rate_idx
+= IWL_FIRST_OFDM_RATE
;
2712 if ((lq_sta
->max_rate_idx
< 0) ||
2713 (lq_sta
->max_rate_idx
>= IWL_RATE_COUNT
))
2714 lq_sta
->max_rate_idx
= -1;
2717 /* Treat uninitialized rate scaling data same as non-existing. */
2718 if (lq_sta
&& !lq_sta
->drv
) {
2719 IWL_DEBUG_RATE(priv
, "Rate scaling not initialized yet.\n");
2723 rate_idx
= lq_sta
->last_txrate_idx
;
2725 if (lq_sta
->last_rate_n_flags
& RATE_MCS_HT_MSK
) {
2726 rate_idx
-= IWL_FIRST_OFDM_RATE
;
2727 /* 6M and 9M shared same MCS index */
2728 rate_idx
= (rate_idx
> 0) ? (rate_idx
- 1) : 0;
2729 if (rs_extract_rate(lq_sta
->last_rate_n_flags
) >=
2730 IWL_RATE_MIMO3_6M_PLCP
)
2731 rate_idx
= rate_idx
+ (2 * MCS_INDEX_PER_STREAM
);
2732 else if (rs_extract_rate(lq_sta
->last_rate_n_flags
) >=
2733 IWL_RATE_MIMO2_6M_PLCP
)
2734 rate_idx
= rate_idx
+ MCS_INDEX_PER_STREAM
;
2735 info
->control
.rates
[0].flags
= IEEE80211_TX_RC_MCS
;
2736 if (lq_sta
->last_rate_n_flags
& RATE_MCS_SGI_MSK
)
2737 info
->control
.rates
[0].flags
|= IEEE80211_TX_RC_SHORT_GI
;
2738 if (lq_sta
->last_rate_n_flags
& RATE_MCS_DUP_MSK
)
2739 info
->control
.rates
[0].flags
|= IEEE80211_TX_RC_DUP_DATA
;
2740 if (lq_sta
->last_rate_n_flags
& RATE_MCS_HT40_MSK
)
2741 info
->control
.rates
[0].flags
|= IEEE80211_TX_RC_40_MHZ_WIDTH
;
2742 if (lq_sta
->last_rate_n_flags
& RATE_MCS_GF_MSK
)
2743 info
->control
.rates
[0].flags
|= IEEE80211_TX_RC_GREEN_FIELD
;
2745 /* Check for invalid rates */
2746 if ((rate_idx
< 0) || (rate_idx
>= IWL_RATE_COUNT_LEGACY
) ||
2747 ((sband
->band
== NL80211_BAND_5GHZ
) &&
2748 (rate_idx
< IWL_FIRST_OFDM_RATE
)))
2749 rate_idx
= rate_lowest_index(sband
, sta
);
2750 /* On valid 5 GHz rate, adjust index */
2751 else if (sband
->band
== NL80211_BAND_5GHZ
)
2752 rate_idx
-= IWL_FIRST_OFDM_RATE
;
2753 info
->control
.rates
[0].flags
= 0;
2755 info
->control
.rates
[0].idx
= rate_idx
;
2756 info
->control
.rates
[0].count
= 1;
2759 static void *rs_alloc_sta(void *priv_rate
, struct ieee80211_sta
*sta
,
2762 struct iwl_station_priv
*sta_priv
= (struct iwl_station_priv
*) sta
->drv_priv
;
2763 struct iwl_op_mode
*op_mode __maybe_unused
=
2764 (struct iwl_op_mode
*)priv_rate
;
2765 struct iwl_priv
*priv __maybe_unused
= IWL_OP_MODE_GET_DVM(op_mode
);
2767 IWL_DEBUG_RATE(priv
, "create station rate scale window\n");
2769 return &sta_priv
->lq_sta
;
2773 * Called after adding a new station to initialize rate scaling
2775 void iwl_rs_rate_init(struct iwl_priv
*priv
, struct ieee80211_sta
*sta
, u8 sta_id
)
2778 struct ieee80211_hw
*hw
= priv
->hw
;
2779 struct ieee80211_conf
*conf
= &priv
->hw
->conf
;
2780 struct ieee80211_sta_ht_cap
*ht_cap
= &sta
->ht_cap
;
2781 struct iwl_station_priv
*sta_priv
;
2782 struct iwl_lq_sta
*lq_sta
;
2783 struct ieee80211_supported_band
*sband
;
2784 unsigned long supp
; /* must be unsigned long for for_each_set_bit */
2786 sta_priv
= (struct iwl_station_priv
*) sta
->drv_priv
;
2787 lq_sta
= &sta_priv
->lq_sta
;
2788 sband
= hw
->wiphy
->bands
[conf
->chandef
.chan
->band
];
2791 lq_sta
->lq
.sta_id
= sta_id
;
2793 for (j
= 0; j
< LQ_SIZE
; j
++)
2794 for (i
= 0; i
< IWL_RATE_COUNT
; i
++)
2795 rs_rate_scale_clear_window(&lq_sta
->lq_info
[j
].win
[i
]);
2797 lq_sta
->flush_timer
= 0;
2798 lq_sta
->supp_rates
= sta
->supp_rates
[sband
->band
];
2800 IWL_DEBUG_RATE(priv
, "LQ: *** rate scale station global init for station %d ***\n",
2802 /* TODO: what is a good starting rate for STA? About middle? Maybe not
2803 * the lowest or the highest rate.. Could consider using RSSI from
2804 * previous packets? Need to have IEEE 802.1X auth succeed immediately
2808 lq_sta
->max_rate_idx
= -1;
2809 lq_sta
->missed_rate_counter
= IWL_MISSED_RATE_MAX
;
2810 lq_sta
->is_green
= rs_use_green(sta
);
2811 lq_sta
->band
= sband
->band
;
2813 * active legacy rates as per supported rates bitmap
2815 supp
= sta
->supp_rates
[sband
->band
];
2816 lq_sta
->active_legacy_rate
= 0;
2817 for_each_set_bit(i
, &supp
, BITS_PER_LONG
)
2818 lq_sta
->active_legacy_rate
|= BIT(sband
->bitrates
[i
].hw_value
);
2821 * active_siso_rate mask includes 9 MBits (bit 5), and CCK (bits 0-3),
2822 * supp_rates[] does not; shift to convert format, force 9 MBits off.
2824 lq_sta
->active_siso_rate
= ht_cap
->mcs
.rx_mask
[0] << 1;
2825 lq_sta
->active_siso_rate
|= ht_cap
->mcs
.rx_mask
[0] & 0x1;
2826 lq_sta
->active_siso_rate
&= ~((u16
)0x2);
2827 lq_sta
->active_siso_rate
<<= IWL_FIRST_OFDM_RATE
;
2830 lq_sta
->active_mimo2_rate
= ht_cap
->mcs
.rx_mask
[1] << 1;
2831 lq_sta
->active_mimo2_rate
|= ht_cap
->mcs
.rx_mask
[1] & 0x1;
2832 lq_sta
->active_mimo2_rate
&= ~((u16
)0x2);
2833 lq_sta
->active_mimo2_rate
<<= IWL_FIRST_OFDM_RATE
;
2835 lq_sta
->active_mimo3_rate
= ht_cap
->mcs
.rx_mask
[2] << 1;
2836 lq_sta
->active_mimo3_rate
|= ht_cap
->mcs
.rx_mask
[2] & 0x1;
2837 lq_sta
->active_mimo3_rate
&= ~((u16
)0x2);
2838 lq_sta
->active_mimo3_rate
<<= IWL_FIRST_OFDM_RATE
;
2840 IWL_DEBUG_RATE(priv
, "SISO-RATE=%X MIMO2-RATE=%X MIMO3-RATE=%X\n",
2841 lq_sta
->active_siso_rate
,
2842 lq_sta
->active_mimo2_rate
,
2843 lq_sta
->active_mimo3_rate
);
2845 /* These values will be overridden later */
2846 lq_sta
->lq
.general_params
.single_stream_ant_msk
=
2847 first_antenna(priv
->nvm_data
->valid_tx_ant
);
2848 lq_sta
->lq
.general_params
.dual_stream_ant_msk
=
2849 priv
->nvm_data
->valid_tx_ant
&
2850 ~first_antenna(priv
->nvm_data
->valid_tx_ant
);
2851 if (!lq_sta
->lq
.general_params
.dual_stream_ant_msk
) {
2852 lq_sta
->lq
.general_params
.dual_stream_ant_msk
= ANT_AB
;
2853 } else if (num_of_ant(priv
->nvm_data
->valid_tx_ant
) == 2) {
2854 lq_sta
->lq
.general_params
.dual_stream_ant_msk
=
2855 priv
->nvm_data
->valid_tx_ant
;
2858 /* as default allow aggregation for all tids */
2859 lq_sta
->tx_agg_tid_en
= IWL_AGG_ALL_TID
;
2862 /* Set last_txrate_idx to lowest rate */
2863 lq_sta
->last_txrate_idx
= rate_lowest_index(sband
, sta
);
2864 if (sband
->band
== NL80211_BAND_5GHZ
)
2865 lq_sta
->last_txrate_idx
+= IWL_FIRST_OFDM_RATE
;
2867 #ifdef CONFIG_MAC80211_DEBUGFS
2868 lq_sta
->dbg_fixed_rate
= 0;
2871 rs_initialize_lq(priv
, sta
, lq_sta
);
2874 static void rs_fill_link_cmd(struct iwl_priv
*priv
,
2875 struct iwl_lq_sta
*lq_sta
, u32 new_rate
)
2877 struct iwl_scale_tbl_info tbl_type
;
2880 int repeat_rate
= 0;
2881 u8 ant_toggle_cnt
= 0;
2882 u8 use_ht_possible
= 1;
2883 u8 valid_tx_ant
= 0;
2884 struct iwl_station_priv
*sta_priv
=
2885 container_of(lq_sta
, struct iwl_station_priv
, lq_sta
);
2886 struct iwl_link_quality_cmd
*lq_cmd
= &lq_sta
->lq
;
2888 /* Override starting rate (index 0) if needed for debug purposes */
2889 rs_dbgfs_set_mcs(lq_sta
, &new_rate
, index
);
2891 /* Interpret new_rate (rate_n_flags) */
2892 rs_get_tbl_info_from_mcs(new_rate
, lq_sta
->band
,
2893 &tbl_type
, &rate_idx
);
2895 if (priv
&& priv
->bt_full_concurrent
) {
2898 first_antenna(priv
->nvm_data
->valid_tx_ant
);
2901 /* How many times should we repeat the initial rate? */
2902 if (is_legacy(tbl_type
.lq_type
)) {
2904 repeat_rate
= IWL_NUMBER_TRY
;
2906 repeat_rate
= min(IWL_HT_NUMBER_TRY
,
2907 LINK_QUAL_AGG_DISABLE_START_DEF
- 1);
2910 lq_cmd
->general_params
.mimo_delimiter
=
2911 is_mimo(tbl_type
.lq_type
) ? 1 : 0;
2913 /* Fill 1st table entry (index 0) */
2914 lq_cmd
->rs_table
[index
].rate_n_flags
= cpu_to_le32(new_rate
);
2916 if (num_of_ant(tbl_type
.ant_type
) == 1) {
2917 lq_cmd
->general_params
.single_stream_ant_msk
=
2919 } else if (num_of_ant(tbl_type
.ant_type
) == 2) {
2920 lq_cmd
->general_params
.dual_stream_ant_msk
=
2922 } /* otherwise we don't modify the existing value */
2927 if (priv
->bt_full_concurrent
)
2928 valid_tx_ant
= ANT_A
;
2930 valid_tx_ant
= priv
->nvm_data
->valid_tx_ant
;
2933 /* Fill rest of rate table */
2934 while (index
< LINK_QUAL_MAX_RETRY_NUM
) {
2935 /* Repeat initial/next rate.
2936 * For legacy IWL_NUMBER_TRY == 1, this loop will not execute.
2937 * For HT IWL_HT_NUMBER_TRY == 3, this executes twice. */
2938 while (repeat_rate
> 0 && (index
< LINK_QUAL_MAX_RETRY_NUM
)) {
2939 if (is_legacy(tbl_type
.lq_type
)) {
2940 if (ant_toggle_cnt
< NUM_TRY_BEFORE_ANT_TOGGLE
)
2943 rs_toggle_antenna(valid_tx_ant
,
2944 &new_rate
, &tbl_type
))
2948 /* Override next rate if needed for debug purposes */
2949 rs_dbgfs_set_mcs(lq_sta
, &new_rate
, index
);
2951 /* Fill next table entry */
2952 lq_cmd
->rs_table
[index
].rate_n_flags
=
2953 cpu_to_le32(new_rate
);
2958 rs_get_tbl_info_from_mcs(new_rate
, lq_sta
->band
, &tbl_type
,
2961 if (priv
&& priv
->bt_full_concurrent
) {
2964 first_antenna(priv
->nvm_data
->valid_tx_ant
);
2967 /* Indicate to uCode which entries might be MIMO.
2968 * If initial rate was MIMO, this will finally end up
2969 * as (IWL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
2970 if (is_mimo(tbl_type
.lq_type
))
2971 lq_cmd
->general_params
.mimo_delimiter
= index
;
2974 new_rate
= rs_get_lower_rate(lq_sta
, &tbl_type
, rate_idx
,
2977 /* How many times should we repeat the next rate? */
2978 if (is_legacy(tbl_type
.lq_type
)) {
2979 if (ant_toggle_cnt
< NUM_TRY_BEFORE_ANT_TOGGLE
)
2982 rs_toggle_antenna(valid_tx_ant
,
2983 &new_rate
, &tbl_type
))
2986 repeat_rate
= IWL_NUMBER_TRY
;
2988 repeat_rate
= IWL_HT_NUMBER_TRY
;
2991 /* Don't allow HT rates after next pass.
2992 * rs_get_lower_rate() will change type to LQ_A or LQ_G. */
2993 use_ht_possible
= 0;
2995 /* Override next rate if needed for debug purposes */
2996 rs_dbgfs_set_mcs(lq_sta
, &new_rate
, index
);
2998 /* Fill next table entry */
2999 lq_cmd
->rs_table
[index
].rate_n_flags
= cpu_to_le32(new_rate
);
3005 lq_cmd
->agg_params
.agg_frame_cnt_limit
=
3006 sta_priv
->max_agg_bufsize
?: LINK_QUAL_AGG_FRAME_LIMIT_DEF
;
3007 lq_cmd
->agg_params
.agg_dis_start_th
= LINK_QUAL_AGG_DISABLE_START_DEF
;
3009 lq_cmd
->agg_params
.agg_time_limit
=
3010 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF
);
3012 * overwrite if needed, pass aggregation time limit
3015 if (priv
&& priv
->lib
->bt_params
&&
3016 priv
->lib
->bt_params
->agg_time_limit
&&
3017 priv
->bt_traffic_load
>= IWL_BT_COEX_TRAFFIC_LOAD_HIGH
)
3018 lq_cmd
->agg_params
.agg_time_limit
=
3019 cpu_to_le16(priv
->lib
->bt_params
->agg_time_limit
);
3022 static void *rs_alloc(struct ieee80211_hw
*hw
, struct dentry
*debugfsdir
)
3026 /* rate scale requires free function to be implemented */
3027 static void rs_free(void *priv_rate
)
3032 static void rs_free_sta(void *priv_r
, struct ieee80211_sta
*sta
,
3035 struct iwl_op_mode
*op_mode __maybe_unused
= priv_r
;
3036 struct iwl_priv
*priv __maybe_unused
= IWL_OP_MODE_GET_DVM(op_mode
);
3038 IWL_DEBUG_RATE(priv
, "enter\n");
3039 IWL_DEBUG_RATE(priv
, "leave\n");
3042 #ifdef CONFIG_MAC80211_DEBUGFS
3043 static void rs_dbgfs_set_mcs(struct iwl_lq_sta
*lq_sta
,
3044 u32
*rate_n_flags
, int index
)
3046 struct iwl_priv
*priv
;
3051 valid_tx_ant
= priv
->nvm_data
->valid_tx_ant
;
3052 if (lq_sta
->dbg_fixed_rate
) {
3054 ((lq_sta
->dbg_fixed_rate
& RATE_MCS_ANT_ABC_MSK
)
3055 >> RATE_MCS_ANT_POS
);
3056 if ((valid_tx_ant
& ant_sel_tx
) == ant_sel_tx
) {
3057 *rate_n_flags
= lq_sta
->dbg_fixed_rate
;
3058 IWL_DEBUG_RATE(priv
, "Fixed rate ON\n");
3060 lq_sta
->dbg_fixed_rate
= 0;
3062 "Invalid antenna selection 0x%X, Valid is 0x%X\n",
3063 ant_sel_tx
, valid_tx_ant
);
3064 IWL_DEBUG_RATE(priv
, "Fixed rate OFF\n");
3067 IWL_DEBUG_RATE(priv
, "Fixed rate OFF\n");
3071 static ssize_t
rs_sta_dbgfs_scale_table_write(struct file
*file
,
3072 const char __user
*user_buf
, size_t count
, loff_t
*ppos
)
3074 struct iwl_lq_sta
*lq_sta
= file
->private_data
;
3075 struct iwl_priv
*priv
;
3082 memset(buf
, 0, sizeof(buf
));
3083 buf_size
= min(count
, sizeof(buf
) - 1);
3084 if (copy_from_user(buf
, user_buf
, buf_size
))
3087 if (sscanf(buf
, "%x", &parsed_rate
) == 1)
3088 lq_sta
->dbg_fixed_rate
= parsed_rate
;
3090 lq_sta
->dbg_fixed_rate
= 0;
3092 rs_program_fix_rate(priv
, lq_sta
);
3097 static ssize_t
rs_sta_dbgfs_scale_table_read(struct file
*file
,
3098 char __user
*user_buf
, size_t count
, loff_t
*ppos
)
3106 struct iwl_lq_sta
*lq_sta
= file
->private_data
;
3107 struct iwl_priv
*priv
;
3108 struct iwl_scale_tbl_info
*tbl
= &(lq_sta
->lq_info
[lq_sta
->active_tbl
]);
3111 buff
= kmalloc(1024, GFP_KERNEL
);
3115 desc
+= sprintf(buff
+desc
, "sta_id %d\n", lq_sta
->lq
.sta_id
);
3116 desc
+= sprintf(buff
+desc
, "failed=%d success=%d rate=0%X\n",
3117 lq_sta
->total_failed
, lq_sta
->total_success
,
3118 lq_sta
->active_legacy_rate
);
3119 desc
+= sprintf(buff
+desc
, "fixed rate 0x%X\n",
3120 lq_sta
->dbg_fixed_rate
);
3121 desc
+= sprintf(buff
+desc
, "valid_tx_ant %s%s%s\n",
3122 (priv
->nvm_data
->valid_tx_ant
& ANT_A
) ? "ANT_A," : "",
3123 (priv
->nvm_data
->valid_tx_ant
& ANT_B
) ? "ANT_B," : "",
3124 (priv
->nvm_data
->valid_tx_ant
& ANT_C
) ? "ANT_C" : "");
3125 desc
+= sprintf(buff
+desc
, "lq type %s\n",
3126 (is_legacy(tbl
->lq_type
)) ? "legacy" : "HT");
3127 if (is_Ht(tbl
->lq_type
)) {
3128 desc
+= sprintf(buff
+ desc
, " %s",
3129 (is_siso(tbl
->lq_type
)) ? "SISO" :
3130 ((is_mimo2(tbl
->lq_type
)) ? "MIMO2" : "MIMO3"));
3131 desc
+= sprintf(buff
+ desc
, " %s",
3132 (tbl
->is_ht40
) ? "40MHz" : "20MHz");
3133 desc
+= sprintf(buff
+ desc
, " %s %s %s\n",
3134 (tbl
->is_SGI
) ? "SGI" : "",
3135 (lq_sta
->is_green
) ? "GF enabled" : "",
3136 (lq_sta
->is_agg
) ? "AGG on" : "");
3138 desc
+= sprintf(buff
+desc
, "last tx rate=0x%X\n",
3139 lq_sta
->last_rate_n_flags
);
3140 desc
+= sprintf(buff
+desc
, "general:"
3141 "flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
3142 lq_sta
->lq
.general_params
.flags
,
3143 lq_sta
->lq
.general_params
.mimo_delimiter
,
3144 lq_sta
->lq
.general_params
.single_stream_ant_msk
,
3145 lq_sta
->lq
.general_params
.dual_stream_ant_msk
);
3147 desc
+= sprintf(buff
+desc
, "agg:"
3148 "time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
3149 le16_to_cpu(lq_sta
->lq
.agg_params
.agg_time_limit
),
3150 lq_sta
->lq
.agg_params
.agg_dis_start_th
,
3151 lq_sta
->lq
.agg_params
.agg_frame_cnt_limit
);
3153 desc
+= sprintf(buff
+desc
,
3154 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
3155 lq_sta
->lq
.general_params
.start_rate_index
[0],
3156 lq_sta
->lq
.general_params
.start_rate_index
[1],
3157 lq_sta
->lq
.general_params
.start_rate_index
[2],
3158 lq_sta
->lq
.general_params
.start_rate_index
[3]);
3160 for (i
= 0; i
< LINK_QUAL_MAX_RETRY_NUM
; i
++) {
3161 index
= iwl_hwrate_to_plcp_idx(
3162 le32_to_cpu(lq_sta
->lq
.rs_table
[i
].rate_n_flags
));
3163 if (is_legacy(tbl
->lq_type
)) {
3164 desc
+= sprintf(buff
+desc
, " rate[%d] 0x%X %smbps\n",
3165 i
, le32_to_cpu(lq_sta
->lq
.rs_table
[i
].rate_n_flags
),
3166 iwl_rate_mcs
[index
].mbps
);
3168 desc
+= sprintf(buff
+desc
, " rate[%d] 0x%X %smbps (%s)\n",
3169 i
, le32_to_cpu(lq_sta
->lq
.rs_table
[i
].rate_n_flags
),
3170 iwl_rate_mcs
[index
].mbps
, iwl_rate_mcs
[index
].mcs
);
3174 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buff
, desc
);
3179 static const struct file_operations rs_sta_dbgfs_scale_table_ops
= {
3180 .write
= rs_sta_dbgfs_scale_table_write
,
3181 .read
= rs_sta_dbgfs_scale_table_read
,
3182 .open
= simple_open
,
3183 .llseek
= default_llseek
,
3185 static ssize_t
rs_sta_dbgfs_stats_table_read(struct file
*file
,
3186 char __user
*user_buf
, size_t count
, loff_t
*ppos
)
3193 struct iwl_lq_sta
*lq_sta
= file
->private_data
;
3195 buff
= kmalloc(1024, GFP_KERNEL
);
3199 for (i
= 0; i
< LQ_SIZE
; i
++) {
3200 desc
+= sprintf(buff
+desc
,
3201 "%s type=%d SGI=%d HT40=%d DUP=%d GF=%d\n"
3203 lq_sta
->active_tbl
== i
? "*" : "x",
3204 lq_sta
->lq_info
[i
].lq_type
,
3205 lq_sta
->lq_info
[i
].is_SGI
,
3206 lq_sta
->lq_info
[i
].is_ht40
,
3207 lq_sta
->lq_info
[i
].is_dup
,
3209 lq_sta
->lq_info
[i
].current_rate
);
3210 for (j
= 0; j
< IWL_RATE_COUNT
; j
++) {
3211 desc
+= sprintf(buff
+desc
,
3212 "counter=%d success=%d %%=%d\n",
3213 lq_sta
->lq_info
[i
].win
[j
].counter
,
3214 lq_sta
->lq_info
[i
].win
[j
].success_counter
,
3215 lq_sta
->lq_info
[i
].win
[j
].success_ratio
);
3218 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buff
, desc
);
3223 static const struct file_operations rs_sta_dbgfs_stats_table_ops
= {
3224 .read
= rs_sta_dbgfs_stats_table_read
,
3225 .open
= simple_open
,
3226 .llseek
= default_llseek
,
3229 static ssize_t
rs_sta_dbgfs_rate_scale_data_read(struct file
*file
,
3230 char __user
*user_buf
, size_t count
, loff_t
*ppos
)
3232 struct iwl_lq_sta
*lq_sta
= file
->private_data
;
3233 struct iwl_scale_tbl_info
*tbl
= &lq_sta
->lq_info
[lq_sta
->active_tbl
];
3237 if (is_Ht(tbl
->lq_type
))
3238 desc
+= sprintf(buff
+desc
,
3239 "Bit Rate= %d Mb/s\n",
3240 tbl
->expected_tpt
[lq_sta
->last_txrate_idx
]);
3242 desc
+= sprintf(buff
+desc
,
3243 "Bit Rate= %d Mb/s\n",
3244 iwl_rates
[lq_sta
->last_txrate_idx
].ieee
>> 1);
3246 return simple_read_from_buffer(user_buf
, count
, ppos
, buff
, desc
);
3249 static const struct file_operations rs_sta_dbgfs_rate_scale_data_ops
= {
3250 .read
= rs_sta_dbgfs_rate_scale_data_read
,
3251 .open
= simple_open
,
3252 .llseek
= default_llseek
,
3255 static void rs_add_debugfs(void *priv
, void *priv_sta
,
3258 struct iwl_lq_sta
*lq_sta
= priv_sta
;
3260 debugfs_create_file("rate_scale_table", 0600, dir
, lq_sta
,
3261 &rs_sta_dbgfs_scale_table_ops
);
3262 debugfs_create_file("rate_stats_table", 0400, dir
, lq_sta
,
3263 &rs_sta_dbgfs_stats_table_ops
);
3264 debugfs_create_file("rate_scale_data", 0400, dir
, lq_sta
,
3265 &rs_sta_dbgfs_rate_scale_data_ops
);
3266 debugfs_create_u8("tx_agg_tid_enable", 0600, dir
,
3267 &lq_sta
->tx_agg_tid_en
);
3273 * Initialization of rate scaling information is done by driver after
3274 * the station is added. Since mac80211 calls this function before a
3275 * station is added we ignore it.
3277 static void rs_rate_init_stub(void *priv_r
, struct ieee80211_supported_band
*sband
,
3278 struct cfg80211_chan_def
*chandef
,
3279 struct ieee80211_sta
*sta
, void *priv_sta
)
3283 static const struct rate_control_ops rs_ops
= {
3285 .tx_status
= rs_tx_status
,
3286 .get_rate
= rs_get_rate
,
3287 .rate_init
= rs_rate_init_stub
,
3290 .alloc_sta
= rs_alloc_sta
,
3291 .free_sta
= rs_free_sta
,
3292 #ifdef CONFIG_MAC80211_DEBUGFS
3293 .add_sta_debugfs
= rs_add_debugfs
,
3297 int iwlagn_rate_control_register(void)
3299 return ieee80211_rate_control_register(&rs_ops
);
3302 void iwlagn_rate_control_unregister(void)
3304 ieee80211_rate_control_unregister(&rs_ops
);