1 /******************************************************************************
3 * Copyright(c) 2005 - 2013 Intel Corporation. All rights reserved.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
18 * The full GNU General Public License is included in this distribution in the
19 * file called LICENSE.
21 * Contact Information:
22 * Intel Linux Wireless <ilw@linux.intel.com>
23 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
25 *****************************************************************************/
26 #include <linux/kernel.h>
27 #include <linux/init.h>
28 #include <linux/skbuff.h>
29 #include <linux/slab.h>
30 #include <net/mac80211.h>
32 #include <linux/netdevice.h>
33 #include <linux/etherdevice.h>
34 #include <linux/delay.h>
36 #include <linux/workqueue.h>
41 #define RS_NAME "iwl-agn-rs"
43 #define NUM_TRY_BEFORE_ANT_TOGGLE 1
44 #define IWL_NUMBER_TRY 1
45 #define IWL_HT_NUMBER_TRY 3
47 #define IWL_RATE_MAX_WINDOW 62 /* # tx in history window */
48 #define IWL_RATE_MIN_FAILURE_TH 6 /* min failures to calc tpt */
49 #define IWL_RATE_MIN_SUCCESS_TH 8 /* min successes to calc tpt */
51 /* max allowed rate miss before sync LQ cmd */
52 #define IWL_MISSED_RATE_MAX 15
53 /* max time to accum history 2 seconds */
54 #define IWL_RATE_SCALE_FLUSH_INTVL (3*HZ)
56 static u8 rs_ht_to_legacy[] = {
57 IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
58 IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
60 IWL_RATE_6M_INDEX, IWL_RATE_9M_INDEX,
61 IWL_RATE_12M_INDEX, IWL_RATE_18M_INDEX,
62 IWL_RATE_24M_INDEX, IWL_RATE_36M_INDEX,
63 IWL_RATE_48M_INDEX, IWL_RATE_54M_INDEX
66 static const u8 ant_toggle_lookup[] = {
67 /*ANT_NONE -> */ ANT_NONE,
70 /*ANT_AB -> */ ANT_BC,
72 /*ANT_AC -> */ ANT_AB,
73 /*ANT_BC -> */ ANT_AC,
74 /*ANT_ABC -> */ ANT_ABC,
77 #define IWL_DECLARE_RATE_INFO(r, s, ip, in, rp, rn, pp, np) \
78 [IWL_RATE_##r##M_INDEX] = { IWL_RATE_##r##M_PLCP, \
79 IWL_RATE_SISO_##s##M_PLCP, \
80 IWL_RATE_MIMO2_##s##M_PLCP,\
81 IWL_RATE_MIMO3_##s##M_PLCP,\
82 IWL_RATE_##r##M_IEEE, \
83 IWL_RATE_##ip##M_INDEX, \
84 IWL_RATE_##in##M_INDEX, \
85 IWL_RATE_##rp##M_INDEX, \
86 IWL_RATE_##rn##M_INDEX, \
87 IWL_RATE_##pp##M_INDEX, \
88 IWL_RATE_##np##M_INDEX }
92 * rate, ht rate, prev rate, next rate, prev tgg rate, next tgg rate
94 * If there isn't a valid next or previous rate then INV is used which
95 * maps to IWL_RATE_INVALID
98 const struct iwl_rate_info iwl_rates[IWL_RATE_COUNT] = {
99 IWL_DECLARE_RATE_INFO(1, INV, INV, 2, INV, 2, INV, 2), /* 1mbps */
100 IWL_DECLARE_RATE_INFO(2, INV, 1, 5, 1, 5, 1, 5), /* 2mbps */
101 IWL_DECLARE_RATE_INFO(5, INV, 2, 6, 2, 11, 2, 11), /*5.5mbps */
102 IWL_DECLARE_RATE_INFO(11, INV, 9, 12, 9, 12, 5, 18), /* 11mbps */
103 IWL_DECLARE_RATE_INFO(6, 6, 5, 9, 5, 11, 5, 11), /* 6mbps */
104 IWL_DECLARE_RATE_INFO(9, 6, 6, 11, 6, 11, 5, 11), /* 9mbps */
105 IWL_DECLARE_RATE_INFO(12, 12, 11, 18, 11, 18, 11, 18), /* 12mbps */
106 IWL_DECLARE_RATE_INFO(18, 18, 12, 24, 12, 24, 11, 24), /* 18mbps */
107 IWL_DECLARE_RATE_INFO(24, 24, 18, 36, 18, 36, 18, 36), /* 24mbps */
108 IWL_DECLARE_RATE_INFO(36, 36, 24, 48, 24, 48, 24, 48), /* 36mbps */
109 IWL_DECLARE_RATE_INFO(48, 48, 36, 54, 36, 54, 36, 54), /* 48mbps */
110 IWL_DECLARE_RATE_INFO(54, 54, 48, INV, 48, INV, 48, INV),/* 54mbps */
111 IWL_DECLARE_RATE_INFO(60, 60, 48, INV, 48, INV, 48, INV),/* 60mbps */
112 /* FIXME:RS: ^^ should be INV (legacy) */
115 static inline u8 rs_extract_rate(u32 rate_n_flags)
117 return (u8)(rate_n_flags & RATE_MCS_RATE_MSK);
120 static int iwl_hwrate_to_plcp_idx(u32 rate_n_flags)
125 if (rate_n_flags & RATE_MCS_HT_MSK) {
126 idx = rs_extract_rate(rate_n_flags);
128 if (idx >= IWL_RATE_MIMO3_6M_PLCP)
129 idx = idx - IWL_RATE_MIMO3_6M_PLCP;
130 else if (idx >= IWL_RATE_MIMO2_6M_PLCP)
131 idx = idx - IWL_RATE_MIMO2_6M_PLCP;
133 idx += IWL_FIRST_OFDM_RATE;
134 /* skip 9M not supported in ht*/
135 if (idx >= IWL_RATE_9M_INDEX)
137 if ((idx >= IWL_FIRST_OFDM_RATE) && (idx <= IWL_LAST_OFDM_RATE))
140 /* legacy rate format, search for match in table */
142 for (idx = 0; idx < ARRAY_SIZE(iwl_rates); idx++)
143 if (iwl_rates[idx].plcp ==
144 rs_extract_rate(rate_n_flags))
151 static void rs_rate_scale_perform(struct iwl_priv *priv,
153 struct ieee80211_sta *sta,
154 struct iwl_lq_sta *lq_sta);
155 static void rs_fill_link_cmd(struct iwl_priv *priv,
156 struct iwl_lq_sta *lq_sta, u32 rate_n_flags);
157 static void rs_stay_in_table(struct iwl_lq_sta *lq_sta, bool force_search);
160 #ifdef CONFIG_MAC80211_DEBUGFS
161 static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
162 u32 *rate_n_flags, int index);
164 static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
165 u32 *rate_n_flags, int index)
170 * The following tables contain the expected throughput metrics for all rates
172 * 1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
174 * where invalid entries are zeros.
176 * CCK rates are only valid in legacy table and will only be used in G
180 static s32 expected_tpt_legacy[IWL_RATE_COUNT] = {
181 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 0
184 static s32 expected_tpt_siso20MHz[4][IWL_RATE_COUNT] = {
185 {0, 0, 0, 0, 42, 0, 76, 102, 124, 159, 183, 193, 202}, /* Norm */
186 {0, 0, 0, 0, 46, 0, 82, 110, 132, 168, 192, 202, 210}, /* SGI */
187 {0, 0, 0, 0, 47, 0, 91, 133, 171, 242, 305, 334, 362}, /* AGG */
188 {0, 0, 0, 0, 52, 0, 101, 145, 187, 264, 330, 361, 390}, /* AGG+SGI */
191 static s32 expected_tpt_siso40MHz[4][IWL_RATE_COUNT] = {
192 {0, 0, 0, 0, 77, 0, 127, 160, 184, 220, 242, 250, 257}, /* Norm */
193 {0, 0, 0, 0, 83, 0, 135, 169, 193, 229, 250, 257, 264}, /* SGI */
194 {0, 0, 0, 0, 94, 0, 177, 249, 313, 423, 512, 550, 586}, /* AGG */
195 {0, 0, 0, 0, 104, 0, 193, 270, 338, 454, 545, 584, 620}, /* AGG+SGI */
198 static s32 expected_tpt_mimo2_20MHz[4][IWL_RATE_COUNT] = {
199 {0, 0, 0, 0, 74, 0, 123, 155, 179, 214, 236, 244, 251}, /* Norm */
200 {0, 0, 0, 0, 81, 0, 131, 164, 188, 223, 243, 251, 257}, /* SGI */
201 {0, 0, 0, 0, 89, 0, 167, 235, 296, 402, 488, 526, 560}, /* AGG */
202 {0, 0, 0, 0, 97, 0, 182, 255, 320, 431, 520, 558, 593}, /* AGG+SGI*/
205 static s32 expected_tpt_mimo2_40MHz[4][IWL_RATE_COUNT] = {
206 {0, 0, 0, 0, 123, 0, 182, 214, 235, 264, 279, 285, 289}, /* Norm */
207 {0, 0, 0, 0, 131, 0, 191, 222, 242, 270, 284, 289, 293}, /* SGI */
208 {0, 0, 0, 0, 171, 0, 305, 410, 496, 634, 731, 771, 805}, /* AGG */
209 {0, 0, 0, 0, 186, 0, 329, 439, 527, 667, 764, 803, 838}, /* AGG+SGI */
212 static s32 expected_tpt_mimo3_20MHz[4][IWL_RATE_COUNT] = {
213 {0, 0, 0, 0, 99, 0, 153, 186, 208, 239, 256, 263, 268}, /* Norm */
214 {0, 0, 0, 0, 106, 0, 162, 194, 215, 246, 262, 268, 273}, /* SGI */
215 {0, 0, 0, 0, 134, 0, 249, 346, 431, 574, 685, 732, 775}, /* AGG */
216 {0, 0, 0, 0, 148, 0, 272, 376, 465, 614, 727, 775, 818}, /* AGG+SGI */
219 static s32 expected_tpt_mimo3_40MHz[4][IWL_RATE_COUNT] = {
220 {0, 0, 0, 0, 152, 0, 211, 239, 255, 279, 290, 294, 297}, /* Norm */
221 {0, 0, 0, 0, 160, 0, 219, 245, 261, 284, 294, 297, 300}, /* SGI */
222 {0, 0, 0, 0, 254, 0, 443, 584, 695, 868, 984, 1030, 1070}, /* AGG */
223 {0, 0, 0, 0, 277, 0, 478, 624, 737, 911, 1026, 1070, 1109}, /* AGG+SGI */
227 static const struct iwl_rate_mcs_info iwl_rate_mcs[IWL_RATE_COUNT] = {
236 { "24", "16QAM 1/2"},
237 { "36", "16QAM 3/4"},
238 { "48", "64QAM 2/3"},
239 { "54", "64QAM 3/4"},
240 { "60", "64QAM 5/6"},
243 #define MCS_INDEX_PER_STREAM (8)
245 static void rs_rate_scale_clear_window(struct iwl_rate_scale_data *window)
248 window->success_counter = 0;
249 window->success_ratio = IWL_INVALID_VALUE;
251 window->average_tpt = IWL_INVALID_VALUE;
255 static inline u8 rs_is_valid_ant(u8 valid_antenna, u8 ant_type)
257 return (ant_type & valid_antenna) == ant_type;
261 * removes the old data from the statistics. All data that is older than
262 * TID_MAX_TIME_DIFF, will be deleted.
264 static void rs_tl_rm_old_stats(struct iwl_traffic_load *tl, u32 curr_time)
266 /* The oldest age we want to keep */
267 u32 oldest_time = curr_time - TID_MAX_TIME_DIFF;
269 while (tl->queue_count &&
270 (tl->time_stamp < oldest_time)) {
271 tl->total -= tl->packet_count[tl->head];
272 tl->packet_count[tl->head] = 0;
273 tl->time_stamp += TID_QUEUE_CELL_SPACING;
276 if (tl->head >= TID_QUEUE_MAX_SIZE)
282 * increment traffic load value for tid and also remove
283 * any old values if passed the certain time period
285 static u8 rs_tl_add_packet(struct iwl_lq_sta *lq_data,
286 struct ieee80211_hdr *hdr)
288 u32 curr_time = jiffies_to_msecs(jiffies);
291 struct iwl_traffic_load *tl = NULL;
294 if (ieee80211_is_data_qos(hdr->frame_control)) {
295 u8 *qc = ieee80211_get_qos_ctl(hdr);
298 return IWL_MAX_TID_COUNT;
300 if (unlikely(tid >= IWL_MAX_TID_COUNT))
301 return IWL_MAX_TID_COUNT;
303 tl = &lq_data->load[tid];
305 curr_time -= curr_time % TID_ROUND_VALUE;
307 /* Happens only for the first packet. Initialize the data */
308 if (!(tl->queue_count)) {
310 tl->time_stamp = curr_time;
313 tl->packet_count[0] = 1;
314 return IWL_MAX_TID_COUNT;
317 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
318 index = time_diff / TID_QUEUE_CELL_SPACING;
320 /* The history is too long: remove data that is older than */
321 /* TID_MAX_TIME_DIFF */
322 if (index >= TID_QUEUE_MAX_SIZE)
323 rs_tl_rm_old_stats(tl, curr_time);
325 index = (tl->head + index) % TID_QUEUE_MAX_SIZE;
326 tl->packet_count[index] = tl->packet_count[index] + 1;
327 tl->total = tl->total + 1;
329 if ((index + 1) > tl->queue_count)
330 tl->queue_count = index + 1;
335 #ifdef CONFIG_MAC80211_DEBUGFS
337 * Program the device to use fixed rate for frame transmit
338 * This is for debugging/testing only
339 * once the device start use fixed rate, we need to reload the module
340 * to being back the normal operation.
342 static void rs_program_fix_rate(struct iwl_priv *priv,
343 struct iwl_lq_sta *lq_sta)
345 struct iwl_station_priv *sta_priv =
346 container_of(lq_sta, struct iwl_station_priv, lq_sta);
347 struct iwl_rxon_context *ctx = sta_priv->ctx;
349 lq_sta->active_legacy_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
350 lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
351 lq_sta->active_mimo2_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
352 lq_sta->active_mimo3_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
354 #ifdef CONFIG_IWLWIFI_DEVICE_TESTMODE
355 /* testmode has higher priority to overwirte the fixed rate */
356 if (priv->tm_fixed_rate)
357 lq_sta->dbg_fixed_rate = priv->tm_fixed_rate;
360 IWL_DEBUG_RATE(priv, "sta_id %d rate 0x%X\n",
361 lq_sta->lq.sta_id, lq_sta->dbg_fixed_rate);
363 if (lq_sta->dbg_fixed_rate) {
364 rs_fill_link_cmd(NULL, lq_sta, lq_sta->dbg_fixed_rate);
365 iwl_send_lq_cmd(lq_sta->drv, ctx, &lq_sta->lq, CMD_ASYNC,
372 get the traffic load value for tid
374 static u32 rs_tl_get_load(struct iwl_lq_sta *lq_data, u8 tid)
376 u32 curr_time = jiffies_to_msecs(jiffies);
379 struct iwl_traffic_load *tl = NULL;
381 if (tid >= IWL_MAX_TID_COUNT)
384 tl = &(lq_data->load[tid]);
386 curr_time -= curr_time % TID_ROUND_VALUE;
388 if (!(tl->queue_count))
391 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
392 index = time_diff / TID_QUEUE_CELL_SPACING;
394 /* The history is too long: remove data that is older than */
395 /* TID_MAX_TIME_DIFF */
396 if (index >= TID_QUEUE_MAX_SIZE)
397 rs_tl_rm_old_stats(tl, curr_time);
402 static int rs_tl_turn_on_agg_for_tid(struct iwl_priv *priv,
403 struct iwl_lq_sta *lq_data, u8 tid,
404 struct ieee80211_sta *sta)
410 * Don't create TX aggregation sessions when in high
411 * BT traffic, as they would just be disrupted by BT.
413 if (priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH) {
415 "BT traffic (%d), no aggregation allowed\n",
416 priv->bt_traffic_load);
420 load = rs_tl_get_load(lq_data, tid);
422 if ((iwlwifi_mod_params.auto_agg) || (load > IWL_AGG_LOAD_THRESHOLD)) {
423 IWL_DEBUG_HT(priv, "Starting Tx agg: STA: %pM tid: %d\n",
425 ret = ieee80211_start_tx_ba_session(sta, tid, 5000);
426 if (ret == -EAGAIN) {
428 * driver and mac80211 is out of sync
429 * this might be cause by reloading firmware
430 * stop the tx ba session here
432 IWL_ERR(priv, "Fail start Tx agg on tid: %d\n",
434 ieee80211_stop_tx_ba_session(sta, tid);
437 IWL_DEBUG_HT(priv, "Aggregation not enabled for tid %d "
438 "because load = %u\n", tid, load);
443 static void rs_tl_turn_on_agg(struct iwl_priv *priv, u8 tid,
444 struct iwl_lq_sta *lq_data,
445 struct ieee80211_sta *sta)
447 if (tid < IWL_MAX_TID_COUNT)
448 rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
450 IWL_ERR(priv, "tid exceeds max TID count: %d/%d\n",
451 tid, IWL_MAX_TID_COUNT);
454 static inline int get_num_of_ant_from_rate(u32 rate_n_flags)
456 return !!(rate_n_flags & RATE_MCS_ANT_A_MSK) +
457 !!(rate_n_flags & RATE_MCS_ANT_B_MSK) +
458 !!(rate_n_flags & RATE_MCS_ANT_C_MSK);
462 * Static function to get the expected throughput from an iwl_scale_tbl_info
463 * that wraps a NULL pointer check
465 static s32 get_expected_tpt(struct iwl_scale_tbl_info *tbl, int rs_index)
467 if (tbl->expected_tpt)
468 return tbl->expected_tpt[rs_index];
473 * rs_collect_tx_data - Update the success/failure sliding window
475 * We keep a sliding window of the last 62 packets transmitted
476 * at this rate. window->data contains the bitmask of successful
479 static int rs_collect_tx_data(struct iwl_scale_tbl_info *tbl,
480 int scale_index, int attempts, int successes)
482 struct iwl_rate_scale_data *window = NULL;
483 static const u64 mask = (((u64)1) << (IWL_RATE_MAX_WINDOW - 1));
486 if (scale_index < 0 || scale_index >= IWL_RATE_COUNT)
489 /* Select window for current tx bit rate */
490 window = &(tbl->win[scale_index]);
492 /* Get expected throughput */
493 tpt = get_expected_tpt(tbl, scale_index);
496 * Keep track of only the latest 62 tx frame attempts in this rate's
497 * history window; anything older isn't really relevant any more.
498 * If we have filled up the sliding window, drop the oldest attempt;
499 * if the oldest attempt (highest bit in bitmap) shows "success",
500 * subtract "1" from the success counter (this is the main reason
501 * we keep these bitmaps!).
503 while (attempts > 0) {
504 if (window->counter >= IWL_RATE_MAX_WINDOW) {
506 /* remove earliest */
507 window->counter = IWL_RATE_MAX_WINDOW - 1;
509 if (window->data & mask) {
510 window->data &= ~mask;
511 window->success_counter--;
515 /* Increment frames-attempted counter */
518 /* Shift bitmap by one frame to throw away oldest history */
521 /* Mark the most recent #successes attempts as successful */
523 window->success_counter++;
531 /* Calculate current success ratio, avoid divide-by-0! */
532 if (window->counter > 0)
533 window->success_ratio = 128 * (100 * window->success_counter)
536 window->success_ratio = IWL_INVALID_VALUE;
538 fail_count = window->counter - window->success_counter;
540 /* Calculate average throughput, if we have enough history. */
541 if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
542 (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
543 window->average_tpt = (window->success_ratio * tpt + 64) / 128;
545 window->average_tpt = IWL_INVALID_VALUE;
547 /* Tag this window as having been updated */
548 window->stamp = jiffies;
554 * Fill uCode API rate_n_flags field, based on "search" or "active" table.
556 /* FIXME:RS:remove this function and put the flags statically in the table */
557 static u32 rate_n_flags_from_tbl(struct iwl_priv *priv,
558 struct iwl_scale_tbl_info *tbl,
559 int index, u8 use_green)
561 u32 rate_n_flags = 0;
563 if (is_legacy(tbl->lq_type)) {
564 rate_n_flags = iwl_rates[index].plcp;
565 if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
566 rate_n_flags |= RATE_MCS_CCK_MSK;
568 } else if (is_Ht(tbl->lq_type)) {
569 if (index > IWL_LAST_OFDM_RATE) {
570 IWL_ERR(priv, "Invalid HT rate index %d\n", index);
571 index = IWL_LAST_OFDM_RATE;
573 rate_n_flags = RATE_MCS_HT_MSK;
575 if (is_siso(tbl->lq_type))
576 rate_n_flags |= iwl_rates[index].plcp_siso;
577 else if (is_mimo2(tbl->lq_type))
578 rate_n_flags |= iwl_rates[index].plcp_mimo2;
580 rate_n_flags |= iwl_rates[index].plcp_mimo3;
582 IWL_ERR(priv, "Invalid tbl->lq_type %d\n", tbl->lq_type);
585 rate_n_flags |= ((tbl->ant_type << RATE_MCS_ANT_POS) &
586 RATE_MCS_ANT_ABC_MSK);
588 if (is_Ht(tbl->lq_type)) {
591 rate_n_flags |= RATE_MCS_DUP_MSK;
593 rate_n_flags |= RATE_MCS_HT40_MSK;
596 rate_n_flags |= RATE_MCS_SGI_MSK;
599 rate_n_flags |= RATE_MCS_GF_MSK;
600 if (is_siso(tbl->lq_type) && tbl->is_SGI) {
601 rate_n_flags &= ~RATE_MCS_SGI_MSK;
602 IWL_ERR(priv, "GF was set with SGI:SISO\n");
610 * Interpret uCode API's rate_n_flags format,
611 * fill "search" or "active" tx mode table.
613 static int rs_get_tbl_info_from_mcs(const u32 rate_n_flags,
614 enum ieee80211_band band,
615 struct iwl_scale_tbl_info *tbl,
618 u32 ant_msk = (rate_n_flags & RATE_MCS_ANT_ABC_MSK);
619 u8 num_of_ant = get_num_of_ant_from_rate(rate_n_flags);
622 memset(tbl, 0, sizeof(struct iwl_scale_tbl_info));
623 *rate_idx = iwl_hwrate_to_plcp_idx(rate_n_flags);
625 if (*rate_idx == IWL_RATE_INVALID) {
629 tbl->is_SGI = 0; /* default legacy setup */
632 tbl->ant_type = (ant_msk >> RATE_MCS_ANT_POS);
633 tbl->lq_type = LQ_NONE;
634 tbl->max_search = IWL_MAX_SEARCH;
636 /* legacy rate format */
637 if (!(rate_n_flags & RATE_MCS_HT_MSK)) {
638 if (num_of_ant == 1) {
639 if (band == IEEE80211_BAND_5GHZ)
646 if (rate_n_flags & RATE_MCS_SGI_MSK)
649 if ((rate_n_flags & RATE_MCS_HT40_MSK) ||
650 (rate_n_flags & RATE_MCS_DUP_MSK))
653 if (rate_n_flags & RATE_MCS_DUP_MSK)
656 mcs = rs_extract_rate(rate_n_flags);
659 if (mcs <= IWL_RATE_SISO_60M_PLCP) {
661 tbl->lq_type = LQ_SISO; /*else NONE*/
663 } else if (mcs <= IWL_RATE_MIMO2_60M_PLCP) {
665 tbl->lq_type = LQ_MIMO2;
668 if (num_of_ant == 3) {
669 tbl->max_search = IWL_MAX_11N_MIMO3_SEARCH;
670 tbl->lq_type = LQ_MIMO3;
677 /* switch to another antenna/antennas and return 1 */
678 /* if no other valid antenna found, return 0 */
679 static int rs_toggle_antenna(u32 valid_ant, u32 *rate_n_flags,
680 struct iwl_scale_tbl_info *tbl)
684 if (!tbl->ant_type || tbl->ant_type > ANT_ABC)
687 if (!rs_is_valid_ant(valid_ant, tbl->ant_type))
690 new_ant_type = ant_toggle_lookup[tbl->ant_type];
692 while ((new_ant_type != tbl->ant_type) &&
693 !rs_is_valid_ant(valid_ant, new_ant_type))
694 new_ant_type = ant_toggle_lookup[new_ant_type];
696 if (new_ant_type == tbl->ant_type)
699 tbl->ant_type = new_ant_type;
700 *rate_n_flags &= ~RATE_MCS_ANT_ABC_MSK;
701 *rate_n_flags |= new_ant_type << RATE_MCS_ANT_POS;
706 * Green-field mode is valid if the station supports it and
707 * there are no non-GF stations present in the BSS.
709 static bool rs_use_green(struct ieee80211_sta *sta)
712 * There's a bug somewhere in this code that causes the
713 * scaling to get stuck because GF+SGI can't be combined
714 * in SISO rates. Until we find that bug, disable GF, it
715 * has only limited benefit and we still interoperate with
716 * GF APs since we can always receive GF transmissions.
722 * rs_get_supported_rates - get the available rates
724 * if management frame or broadcast frame only return
725 * basic available rates.
728 static u16 rs_get_supported_rates(struct iwl_lq_sta *lq_sta,
729 struct ieee80211_hdr *hdr,
730 enum iwl_table_type rate_type)
732 if (is_legacy(rate_type)) {
733 return lq_sta->active_legacy_rate;
735 if (is_siso(rate_type))
736 return lq_sta->active_siso_rate;
737 else if (is_mimo2(rate_type))
738 return lq_sta->active_mimo2_rate;
740 return lq_sta->active_mimo3_rate;
744 static u16 rs_get_adjacent_rate(struct iwl_priv *priv, u8 index, u16 rate_mask,
747 u8 high = IWL_RATE_INVALID;
748 u8 low = IWL_RATE_INVALID;
750 /* 802.11A or ht walks to the next literal adjacent rate in
752 if (is_a_band(rate_type) || !is_legacy(rate_type)) {
756 /* Find the previous rate that is in the rate mask */
758 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
759 if (rate_mask & mask) {
765 /* Find the next rate that is in the rate mask */
767 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
768 if (rate_mask & mask) {
774 return (high << 8) | low;
778 while (low != IWL_RATE_INVALID) {
779 low = iwl_rates[low].prev_rs;
780 if (low == IWL_RATE_INVALID)
782 if (rate_mask & (1 << low))
784 IWL_DEBUG_RATE(priv, "Skipping masked lower rate: %d\n", low);
788 while (high != IWL_RATE_INVALID) {
789 high = iwl_rates[high].next_rs;
790 if (high == IWL_RATE_INVALID)
792 if (rate_mask & (1 << high))
794 IWL_DEBUG_RATE(priv, "Skipping masked higher rate: %d\n", high);
797 return (high << 8) | low;
800 static u32 rs_get_lower_rate(struct iwl_lq_sta *lq_sta,
801 struct iwl_scale_tbl_info *tbl,
802 u8 scale_index, u8 ht_possible)
807 u8 switch_to_legacy = 0;
808 u8 is_green = lq_sta->is_green;
809 struct iwl_priv *priv = lq_sta->drv;
811 /* check if we need to switch from HT to legacy rates.
812 * assumption is that mandatory rates (1Mbps or 6Mbps)
813 * are always supported (spec demand) */
814 if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_index)) {
815 switch_to_legacy = 1;
816 scale_index = rs_ht_to_legacy[scale_index];
817 if (lq_sta->band == IEEE80211_BAND_5GHZ)
822 if (num_of_ant(tbl->ant_type) > 1)
824 first_antenna(priv->nvm_data->valid_tx_ant);
828 tbl->max_search = IWL_MAX_SEARCH;
831 rate_mask = rs_get_supported_rates(lq_sta, NULL, tbl->lq_type);
833 /* Mask with station rate restriction */
834 if (is_legacy(tbl->lq_type)) {
835 /* supp_rates has no CCK bits in A mode */
836 if (lq_sta->band == IEEE80211_BAND_5GHZ)
837 rate_mask = (u16)(rate_mask &
838 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
840 rate_mask = (u16)(rate_mask & lq_sta->supp_rates);
843 /* If we switched from HT to legacy, check current rate */
844 if (switch_to_legacy && (rate_mask & (1 << scale_index))) {
849 high_low = rs_get_adjacent_rate(lq_sta->drv, scale_index, rate_mask,
851 low = high_low & 0xff;
853 if (low == IWL_RATE_INVALID)
857 return rate_n_flags_from_tbl(lq_sta->drv, tbl, low, is_green);
861 * Simple function to compare two rate scale table types
863 static bool table_type_matches(struct iwl_scale_tbl_info *a,
864 struct iwl_scale_tbl_info *b)
866 return (a->lq_type == b->lq_type) && (a->ant_type == b->ant_type) &&
867 (a->is_SGI == b->is_SGI);
870 static void rs_bt_update_lq(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
871 struct iwl_lq_sta *lq_sta)
873 struct iwl_scale_tbl_info *tbl;
874 bool full_concurrent = priv->bt_full_concurrent;
876 if (priv->bt_ant_couple_ok) {
878 * Is there a need to switch between
879 * full concurrency and 3-wire?
881 if (priv->bt_ci_compliance && priv->bt_ant_couple_ok)
882 full_concurrent = true;
884 full_concurrent = false;
886 if ((priv->bt_traffic_load != priv->last_bt_traffic_load) ||
887 (priv->bt_full_concurrent != full_concurrent)) {
888 priv->bt_full_concurrent = full_concurrent;
889 priv->last_bt_traffic_load = priv->bt_traffic_load;
891 /* Update uCode's rate table. */
892 tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
893 rs_fill_link_cmd(priv, lq_sta, tbl->current_rate);
894 iwl_send_lq_cmd(priv, ctx, &lq_sta->lq, CMD_ASYNC, false);
896 queue_work(priv->workqueue, &priv->bt_full_concurrency);
901 * mac80211 sends us Tx status
903 static void rs_tx_status(void *priv_r, struct ieee80211_supported_band *sband,
904 struct ieee80211_sta *sta, void *priv_sta,
909 int rs_index, mac_index, i;
910 struct iwl_lq_sta *lq_sta = priv_sta;
911 struct iwl_link_quality_cmd *table;
912 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
913 struct iwl_op_mode *op_mode = (struct iwl_op_mode *)priv_r;
914 struct iwl_priv *priv = IWL_OP_MODE_GET_DVM(op_mode);
915 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
916 enum mac80211_rate_control_flags mac_flags;
918 struct iwl_scale_tbl_info tbl_type;
919 struct iwl_scale_tbl_info *curr_tbl, *other_tbl, *tmp_tbl;
920 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
921 struct iwl_rxon_context *ctx = sta_priv->ctx;
923 IWL_DEBUG_RATE_LIMIT(priv, "get frame ack response, update rate scale window\n");
925 /* Treat uninitialized rate scaling data same as non-existing. */
927 IWL_DEBUG_RATE(priv, "Station rate scaling not created yet.\n");
929 } else if (!lq_sta->drv) {
930 IWL_DEBUG_RATE(priv, "Rate scaling not initialized yet.\n");
934 if (!ieee80211_is_data(hdr->frame_control) ||
935 info->flags & IEEE80211_TX_CTL_NO_ACK)
938 /* This packet was aggregated but doesn't carry status info */
939 if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
940 !(info->flags & IEEE80211_TX_STAT_AMPDU))
944 * Ignore this Tx frame response if its initial rate doesn't match
945 * that of latest Link Quality command. There may be stragglers
946 * from a previous Link Quality command, but we're no longer interested
947 * in those; they're either from the "active" mode while we're trying
948 * to check "search" mode, or a prior "search" mode after we've moved
949 * to a new "search" mode (which might become the new "active" mode).
952 tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
953 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
954 if (priv->band == IEEE80211_BAND_5GHZ)
955 rs_index -= IWL_FIRST_OFDM_RATE;
956 mac_flags = info->status.rates[0].flags;
957 mac_index = info->status.rates[0].idx;
958 /* For HT packets, map MCS to PLCP */
959 if (mac_flags & IEEE80211_TX_RC_MCS) {
960 mac_index &= RATE_MCS_CODE_MSK; /* Remove # of streams */
961 if (mac_index >= (IWL_RATE_9M_INDEX - IWL_FIRST_OFDM_RATE))
964 * mac80211 HT index is always zero-indexed; we need to move
965 * HT OFDM rates after CCK rates in 2.4 GHz band
967 if (priv->band == IEEE80211_BAND_2GHZ)
968 mac_index += IWL_FIRST_OFDM_RATE;
970 /* Here we actually compare this rate to the latest LQ command */
971 if ((mac_index < 0) ||
972 (tbl_type.is_SGI != !!(mac_flags & IEEE80211_TX_RC_SHORT_GI)) ||
973 (tbl_type.is_ht40 != !!(mac_flags & IEEE80211_TX_RC_40_MHZ_WIDTH)) ||
974 (tbl_type.is_dup != !!(mac_flags & IEEE80211_TX_RC_DUP_DATA)) ||
975 (tbl_type.ant_type != info->status.antenna) ||
976 (!!(tx_rate & RATE_MCS_HT_MSK) != !!(mac_flags & IEEE80211_TX_RC_MCS)) ||
977 (!!(tx_rate & RATE_MCS_GF_MSK) != !!(mac_flags & IEEE80211_TX_RC_GREEN_FIELD)) ||
978 (rs_index != mac_index)) {
979 IWL_DEBUG_RATE(priv, "initial rate %d does not match %d (0x%x)\n", mac_index, rs_index, tx_rate);
981 * Since rates mis-match, the last LQ command may have failed.
982 * After IWL_MISSED_RATE_MAX mis-matches, resync the uCode with
985 lq_sta->missed_rate_counter++;
986 if (lq_sta->missed_rate_counter > IWL_MISSED_RATE_MAX) {
987 lq_sta->missed_rate_counter = 0;
988 iwl_send_lq_cmd(priv, ctx, &lq_sta->lq, CMD_ASYNC, false);
990 /* Regardless, ignore this status info for outdated rate */
993 /* Rate did match, so reset the missed_rate_counter */
994 lq_sta->missed_rate_counter = 0;
996 /* Figure out if rate scale algorithm is in active or search table */
997 if (table_type_matches(&tbl_type,
998 &(lq_sta->lq_info[lq_sta->active_tbl]))) {
999 curr_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1000 other_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
1001 } else if (table_type_matches(&tbl_type,
1002 &lq_sta->lq_info[1 - lq_sta->active_tbl])) {
1003 curr_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
1004 other_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1006 IWL_DEBUG_RATE(priv, "Neither active nor search matches tx rate\n");
1007 tmp_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1008 IWL_DEBUG_RATE(priv, "active- lq:%x, ant:%x, SGI:%d\n",
1009 tmp_tbl->lq_type, tmp_tbl->ant_type, tmp_tbl->is_SGI);
1010 tmp_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
1011 IWL_DEBUG_RATE(priv, "search- lq:%x, ant:%x, SGI:%d\n",
1012 tmp_tbl->lq_type, tmp_tbl->ant_type, tmp_tbl->is_SGI);
1013 IWL_DEBUG_RATE(priv, "actual- lq:%x, ant:%x, SGI:%d\n",
1014 tbl_type.lq_type, tbl_type.ant_type, tbl_type.is_SGI);
1016 * no matching table found, let's by-pass the data collection
1017 * and continue to perform rate scale to find the rate table
1019 rs_stay_in_table(lq_sta, true);
1024 * Updating the frame history depends on whether packets were
1027 * For aggregation, all packets were transmitted at the same rate, the
1028 * first index into rate scale table.
1030 if (info->flags & IEEE80211_TX_STAT_AMPDU) {
1031 tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
1032 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type,
1034 rs_collect_tx_data(curr_tbl, rs_index,
1035 info->status.ampdu_len,
1036 info->status.ampdu_ack_len);
1038 /* Update success/fail counts if not searching for new mode */
1039 if (lq_sta->stay_in_tbl) {
1040 lq_sta->total_success += info->status.ampdu_ack_len;
1041 lq_sta->total_failed += (info->status.ampdu_len -
1042 info->status.ampdu_ack_len);
1046 * For legacy, update frame history with for each Tx retry.
1048 retries = info->status.rates[0].count - 1;
1049 /* HW doesn't send more than 15 retries */
1050 retries = min(retries, 15);
1052 /* The last transmission may have been successful */
1053 legacy_success = !!(info->flags & IEEE80211_TX_STAT_ACK);
1054 /* Collect data for each rate used during failed TX attempts */
1055 for (i = 0; i <= retries; ++i) {
1056 tx_rate = le32_to_cpu(table->rs_table[i].rate_n_flags);
1057 rs_get_tbl_info_from_mcs(tx_rate, priv->band,
1058 &tbl_type, &rs_index);
1060 * Only collect stats if retried rate is in the same RS
1061 * table as active/search.
1063 if (table_type_matches(&tbl_type, curr_tbl))
1065 else if (table_type_matches(&tbl_type, other_tbl))
1066 tmp_tbl = other_tbl;
1069 rs_collect_tx_data(tmp_tbl, rs_index, 1,
1070 i < retries ? 0 : legacy_success);
1073 /* Update success/fail counts if not searching for new mode */
1074 if (lq_sta->stay_in_tbl) {
1075 lq_sta->total_success += legacy_success;
1076 lq_sta->total_failed += retries + (1 - legacy_success);
1079 /* The last TX rate is cached in lq_sta; it's set in if/else above */
1080 lq_sta->last_rate_n_flags = tx_rate;
1082 /* See if there's a better rate or modulation mode to try. */
1083 if (sta && sta->supp_rates[sband->band])
1084 rs_rate_scale_perform(priv, skb, sta, lq_sta);
1086 #if defined(CONFIG_MAC80211_DEBUGFS) && defined(CONFIG_IWLWIFI_DEVICE_TESTMODE)
1087 if ((priv->tm_fixed_rate) &&
1088 (priv->tm_fixed_rate != lq_sta->dbg_fixed_rate))
1089 rs_program_fix_rate(priv, lq_sta);
1091 if (priv->cfg->bt_params && priv->cfg->bt_params->advanced_bt_coexist)
1092 rs_bt_update_lq(priv, ctx, lq_sta);
1096 * Begin a period of staying with a selected modulation mode.
1097 * Set "stay_in_tbl" flag to prevent any mode switches.
1098 * Set frame tx success limits according to legacy vs. high-throughput,
1099 * and reset overall (spanning all rates) tx success history statistics.
1100 * These control how long we stay using same modulation mode before
1101 * searching for a new mode.
1103 static void rs_set_stay_in_table(struct iwl_priv *priv, u8 is_legacy,
1104 struct iwl_lq_sta *lq_sta)
1106 IWL_DEBUG_RATE(priv, "we are staying in the same table\n");
1107 lq_sta->stay_in_tbl = 1; /* only place this gets set */
1109 lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT;
1110 lq_sta->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
1111 lq_sta->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
1113 lq_sta->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
1114 lq_sta->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
1115 lq_sta->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
1117 lq_sta->table_count = 0;
1118 lq_sta->total_failed = 0;
1119 lq_sta->total_success = 0;
1120 lq_sta->flush_timer = jiffies;
1121 lq_sta->action_counter = 0;
1125 * Find correct throughput table for given mode of modulation
1127 static void rs_set_expected_tpt_table(struct iwl_lq_sta *lq_sta,
1128 struct iwl_scale_tbl_info *tbl)
1130 /* Used to choose among HT tables */
1131 s32 (*ht_tbl_pointer)[IWL_RATE_COUNT];
1133 /* Check for invalid LQ type */
1134 if (WARN_ON_ONCE(!is_legacy(tbl->lq_type) && !is_Ht(tbl->lq_type))) {
1135 tbl->expected_tpt = expected_tpt_legacy;
1139 /* Legacy rates have only one table */
1140 if (is_legacy(tbl->lq_type)) {
1141 tbl->expected_tpt = expected_tpt_legacy;
1145 /* Choose among many HT tables depending on number of streams
1146 * (SISO/MIMO2/MIMO3), channel width (20/40), SGI, and aggregation
1148 if (is_siso(tbl->lq_type) && (!tbl->is_ht40 || lq_sta->is_dup))
1149 ht_tbl_pointer = expected_tpt_siso20MHz;
1150 else if (is_siso(tbl->lq_type))
1151 ht_tbl_pointer = expected_tpt_siso40MHz;
1152 else if (is_mimo2(tbl->lq_type) && (!tbl->is_ht40 || lq_sta->is_dup))
1153 ht_tbl_pointer = expected_tpt_mimo2_20MHz;
1154 else if (is_mimo2(tbl->lq_type))
1155 ht_tbl_pointer = expected_tpt_mimo2_40MHz;
1156 else if (is_mimo3(tbl->lq_type) && (!tbl->is_ht40 || lq_sta->is_dup))
1157 ht_tbl_pointer = expected_tpt_mimo3_20MHz;
1158 else /* if (is_mimo3(tbl->lq_type)) <-- must be true */
1159 ht_tbl_pointer = expected_tpt_mimo3_40MHz;
1161 if (!tbl->is_SGI && !lq_sta->is_agg) /* Normal */
1162 tbl->expected_tpt = ht_tbl_pointer[0];
1163 else if (tbl->is_SGI && !lq_sta->is_agg) /* SGI */
1164 tbl->expected_tpt = ht_tbl_pointer[1];
1165 else if (!tbl->is_SGI && lq_sta->is_agg) /* AGG */
1166 tbl->expected_tpt = ht_tbl_pointer[2];
1168 tbl->expected_tpt = ht_tbl_pointer[3];
1172 * Find starting rate for new "search" high-throughput mode of modulation.
1173 * Goal is to find lowest expected rate (under perfect conditions) that is
1174 * above the current measured throughput of "active" mode, to give new mode
1175 * a fair chance to prove itself without too many challenges.
1177 * This gets called when transitioning to more aggressive modulation
1178 * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
1179 * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
1180 * to decrease to match "active" throughput. When moving from MIMO to SISO,
1181 * bit rate will typically need to increase, but not if performance was bad.
1183 static s32 rs_get_best_rate(struct iwl_priv *priv,
1184 struct iwl_lq_sta *lq_sta,
1185 struct iwl_scale_tbl_info *tbl, /* "search" */
1186 u16 rate_mask, s8 index)
1188 /* "active" values */
1189 struct iwl_scale_tbl_info *active_tbl =
1190 &(lq_sta->lq_info[lq_sta->active_tbl]);
1191 s32 active_sr = active_tbl->win[index].success_ratio;
1192 s32 active_tpt = active_tbl->expected_tpt[index];
1194 /* expected "search" throughput */
1195 s32 *tpt_tbl = tbl->expected_tpt;
1197 s32 new_rate, high, low, start_hi;
1201 new_rate = high = low = start_hi = IWL_RATE_INVALID;
1204 high_low = rs_get_adjacent_rate(priv, rate, rate_mask,
1207 low = high_low & 0xff;
1208 high = (high_low >> 8) & 0xff;
1211 * Lower the "search" bit rate, to give new "search" mode
1212 * approximately the same throughput as "active" if:
1214 * 1) "Active" mode has been working modestly well (but not
1215 * great), and expected "search" throughput (under perfect
1216 * conditions) at candidate rate is above the actual
1217 * measured "active" throughput (but less than expected
1218 * "active" throughput under perfect conditions).
1220 * 2) "Active" mode has been working perfectly or very well
1221 * and expected "search" throughput (under perfect
1222 * conditions) at candidate rate is above expected
1223 * "active" throughput (under perfect conditions).
1225 if ((((100 * tpt_tbl[rate]) > lq_sta->last_tpt) &&
1226 ((active_sr > IWL_RATE_DECREASE_TH) &&
1227 (active_sr <= IWL_RATE_HIGH_TH) &&
1228 (tpt_tbl[rate] <= active_tpt))) ||
1229 ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
1230 (tpt_tbl[rate] > active_tpt))) {
1232 /* (2nd or later pass)
1233 * If we've already tried to raise the rate, and are
1234 * now trying to lower it, use the higher rate. */
1235 if (start_hi != IWL_RATE_INVALID) {
1236 new_rate = start_hi;
1242 /* Loop again with lower rate */
1243 if (low != IWL_RATE_INVALID)
1246 /* Lower rate not available, use the original */
1250 /* Else try to raise the "search" rate to match "active" */
1252 /* (2nd or later pass)
1253 * If we've already tried to lower the rate, and are
1254 * now trying to raise it, use the lower rate. */
1255 if (new_rate != IWL_RATE_INVALID)
1258 /* Loop again with higher rate */
1259 else if (high != IWL_RATE_INVALID) {
1263 /* Higher rate not available, use the original */
1275 * Set up search table for MIMO2
1277 static int rs_switch_to_mimo2(struct iwl_priv *priv,
1278 struct iwl_lq_sta *lq_sta,
1279 struct ieee80211_conf *conf,
1280 struct ieee80211_sta *sta,
1281 struct iwl_scale_tbl_info *tbl, int index)
1285 s8 is_green = lq_sta->is_green;
1286 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
1287 struct iwl_rxon_context *ctx = sta_priv->ctx;
1289 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
1292 if (sta->smps_mode == IEEE80211_SMPS_STATIC)
1295 /* Need both Tx chains/antennas to support MIMO */
1296 if (priv->hw_params.tx_chains_num < 2)
1299 IWL_DEBUG_RATE(priv, "LQ: try to switch to MIMO2\n");
1301 tbl->lq_type = LQ_MIMO2;
1302 tbl->is_dup = lq_sta->is_dup;
1304 tbl->max_search = IWL_MAX_SEARCH;
1305 rate_mask = lq_sta->active_mimo2_rate;
1307 if (iwl_is_ht40_tx_allowed(priv, ctx, sta))
1312 rs_set_expected_tpt_table(lq_sta, tbl);
1314 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
1316 IWL_DEBUG_RATE(priv, "LQ: MIMO2 best rate %d mask %X\n", rate, rate_mask);
1317 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1318 IWL_DEBUG_RATE(priv, "Can't switch with index %d rate mask %x\n",
1322 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
1324 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
1325 tbl->current_rate, is_green);
1330 * Set up search table for MIMO3
1332 static int rs_switch_to_mimo3(struct iwl_priv *priv,
1333 struct iwl_lq_sta *lq_sta,
1334 struct ieee80211_conf *conf,
1335 struct ieee80211_sta *sta,
1336 struct iwl_scale_tbl_info *tbl, int index)
1340 s8 is_green = lq_sta->is_green;
1341 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
1342 struct iwl_rxon_context *ctx = sta_priv->ctx;
1344 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
1347 if (sta->smps_mode == IEEE80211_SMPS_STATIC)
1350 /* Need both Tx chains/antennas to support MIMO */
1351 if (priv->hw_params.tx_chains_num < 3)
1354 IWL_DEBUG_RATE(priv, "LQ: try to switch to MIMO3\n");
1356 tbl->lq_type = LQ_MIMO3;
1357 tbl->is_dup = lq_sta->is_dup;
1359 tbl->max_search = IWL_MAX_11N_MIMO3_SEARCH;
1360 rate_mask = lq_sta->active_mimo3_rate;
1362 if (iwl_is_ht40_tx_allowed(priv, ctx, sta))
1367 rs_set_expected_tpt_table(lq_sta, tbl);
1369 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
1371 IWL_DEBUG_RATE(priv, "LQ: MIMO3 best rate %d mask %X\n",
1373 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1374 IWL_DEBUG_RATE(priv, "Can't switch with index %d rate mask %x\n",
1378 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
1380 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
1381 tbl->current_rate, is_green);
1386 * Set up search table for SISO
1388 static int rs_switch_to_siso(struct iwl_priv *priv,
1389 struct iwl_lq_sta *lq_sta,
1390 struct ieee80211_conf *conf,
1391 struct ieee80211_sta *sta,
1392 struct iwl_scale_tbl_info *tbl, int index)
1395 u8 is_green = lq_sta->is_green;
1397 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
1398 struct iwl_rxon_context *ctx = sta_priv->ctx;
1400 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
1403 IWL_DEBUG_RATE(priv, "LQ: try to switch to SISO\n");
1405 tbl->is_dup = lq_sta->is_dup;
1406 tbl->lq_type = LQ_SISO;
1408 tbl->max_search = IWL_MAX_SEARCH;
1409 rate_mask = lq_sta->active_siso_rate;
1411 if (iwl_is_ht40_tx_allowed(priv, ctx, sta))
1417 tbl->is_SGI = 0; /*11n spec: no SGI in SISO+Greenfield*/
1419 rs_set_expected_tpt_table(lq_sta, tbl);
1420 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
1422 IWL_DEBUG_RATE(priv, "LQ: get best rate %d mask %X\n", rate, rate_mask);
1423 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1424 IWL_DEBUG_RATE(priv, "can not switch with index %d rate mask %x\n",
1428 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
1429 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
1430 tbl->current_rate, is_green);
1435 * Try to switch to new modulation mode from legacy
1437 static int rs_move_legacy_other(struct iwl_priv *priv,
1438 struct iwl_lq_sta *lq_sta,
1439 struct ieee80211_conf *conf,
1440 struct ieee80211_sta *sta,
1443 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1444 struct iwl_scale_tbl_info *search_tbl =
1445 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1446 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1447 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1448 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1450 u8 valid_tx_ant = priv->nvm_data->valid_tx_ant;
1451 u8 tx_chains_num = priv->hw_params.tx_chains_num;
1453 u8 update_search_tbl_counter = 0;
1455 switch (priv->bt_traffic_load) {
1456 case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
1459 case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
1460 /* avoid antenna B unless MIMO */
1461 if (tbl->action == IWL_LEGACY_SWITCH_ANTENNA2)
1462 tbl->action = IWL_LEGACY_SWITCH_SISO;
1464 case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
1465 case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
1466 /* avoid antenna B and MIMO */
1468 first_antenna(priv->nvm_data->valid_tx_ant);
1469 if (tbl->action >= IWL_LEGACY_SWITCH_ANTENNA2 &&
1470 tbl->action != IWL_LEGACY_SWITCH_SISO)
1471 tbl->action = IWL_LEGACY_SWITCH_SISO;
1474 IWL_ERR(priv, "Invalid BT load %d", priv->bt_traffic_load);
1478 if (!iwl_ht_enabled(priv))
1479 /* stay in Legacy */
1480 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
1481 else if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE &&
1482 tbl->action > IWL_LEGACY_SWITCH_SISO)
1483 tbl->action = IWL_LEGACY_SWITCH_SISO;
1485 /* configure as 1x1 if bt full concurrency */
1486 if (priv->bt_full_concurrent) {
1487 if (!iwl_ht_enabled(priv))
1488 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
1489 else if (tbl->action >= IWL_LEGACY_SWITCH_ANTENNA2)
1490 tbl->action = IWL_LEGACY_SWITCH_SISO;
1492 first_antenna(priv->nvm_data->valid_tx_ant);
1495 start_action = tbl->action;
1497 lq_sta->action_counter++;
1498 switch (tbl->action) {
1499 case IWL_LEGACY_SWITCH_ANTENNA1:
1500 case IWL_LEGACY_SWITCH_ANTENNA2:
1501 IWL_DEBUG_RATE(priv, "LQ: Legacy toggle Antenna\n");
1503 if ((tbl->action == IWL_LEGACY_SWITCH_ANTENNA1 &&
1504 tx_chains_num <= 1) ||
1505 (tbl->action == IWL_LEGACY_SWITCH_ANTENNA2 &&
1506 tx_chains_num <= 2))
1509 /* Don't change antenna if success has been great */
1510 if (window->success_ratio >= IWL_RS_GOOD_RATIO &&
1511 !priv->bt_full_concurrent &&
1512 priv->bt_traffic_load ==
1513 IWL_BT_COEX_TRAFFIC_LOAD_NONE)
1516 /* Set up search table to try other antenna */
1517 memcpy(search_tbl, tbl, sz);
1519 if (rs_toggle_antenna(valid_tx_ant,
1520 &search_tbl->current_rate, search_tbl)) {
1521 update_search_tbl_counter = 1;
1522 rs_set_expected_tpt_table(lq_sta, search_tbl);
1526 case IWL_LEGACY_SWITCH_SISO:
1527 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to SISO\n");
1529 /* Set up search table to try SISO */
1530 memcpy(search_tbl, tbl, sz);
1531 search_tbl->is_SGI = 0;
1532 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
1535 lq_sta->action_counter = 0;
1540 case IWL_LEGACY_SWITCH_MIMO2_AB:
1541 case IWL_LEGACY_SWITCH_MIMO2_AC:
1542 case IWL_LEGACY_SWITCH_MIMO2_BC:
1543 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to MIMO2\n");
1545 /* Set up search table to try MIMO */
1546 memcpy(search_tbl, tbl, sz);
1547 search_tbl->is_SGI = 0;
1549 if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AB)
1550 search_tbl->ant_type = ANT_AB;
1551 else if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AC)
1552 search_tbl->ant_type = ANT_AC;
1554 search_tbl->ant_type = ANT_BC;
1556 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1559 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
1562 lq_sta->action_counter = 0;
1567 case IWL_LEGACY_SWITCH_MIMO3_ABC:
1568 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to MIMO3\n");
1570 /* Set up search table to try MIMO3 */
1571 memcpy(search_tbl, tbl, sz);
1572 search_tbl->is_SGI = 0;
1574 search_tbl->ant_type = ANT_ABC;
1576 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1579 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1582 lq_sta->action_counter = 0;
1588 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
1589 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
1591 if (tbl->action == start_action)
1595 search_tbl->lq_type = LQ_NONE;
1599 lq_sta->search_better_tbl = 1;
1601 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
1602 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
1603 if (update_search_tbl_counter)
1604 search_tbl->action = tbl->action;
1610 * Try to switch to new modulation mode from SISO
1612 static int rs_move_siso_to_other(struct iwl_priv *priv,
1613 struct iwl_lq_sta *lq_sta,
1614 struct ieee80211_conf *conf,
1615 struct ieee80211_sta *sta, int index)
1617 u8 is_green = lq_sta->is_green;
1618 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1619 struct iwl_scale_tbl_info *search_tbl =
1620 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1621 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1622 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
1623 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1624 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1626 u8 valid_tx_ant = priv->nvm_data->valid_tx_ant;
1627 u8 tx_chains_num = priv->hw_params.tx_chains_num;
1628 u8 update_search_tbl_counter = 0;
1631 switch (priv->bt_traffic_load) {
1632 case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
1635 case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
1636 /* avoid antenna B unless MIMO */
1637 if (tbl->action == IWL_SISO_SWITCH_ANTENNA2)
1638 tbl->action = IWL_SISO_SWITCH_MIMO2_AB;
1640 case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
1641 case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
1642 /* avoid antenna B and MIMO */
1644 first_antenna(priv->nvm_data->valid_tx_ant);
1645 if (tbl->action != IWL_SISO_SWITCH_ANTENNA1)
1646 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1649 IWL_ERR(priv, "Invalid BT load %d", priv->bt_traffic_load);
1653 if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE &&
1654 tbl->action > IWL_SISO_SWITCH_ANTENNA2) {
1656 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1659 /* configure as 1x1 if bt full concurrency */
1660 if (priv->bt_full_concurrent) {
1662 first_antenna(priv->nvm_data->valid_tx_ant);
1663 if (tbl->action >= IWL_LEGACY_SWITCH_ANTENNA2)
1664 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1667 start_action = tbl->action;
1669 lq_sta->action_counter++;
1670 switch (tbl->action) {
1671 case IWL_SISO_SWITCH_ANTENNA1:
1672 case IWL_SISO_SWITCH_ANTENNA2:
1673 IWL_DEBUG_RATE(priv, "LQ: SISO toggle Antenna\n");
1674 if ((tbl->action == IWL_SISO_SWITCH_ANTENNA1 &&
1675 tx_chains_num <= 1) ||
1676 (tbl->action == IWL_SISO_SWITCH_ANTENNA2 &&
1677 tx_chains_num <= 2))
1680 if (window->success_ratio >= IWL_RS_GOOD_RATIO &&
1681 !priv->bt_full_concurrent &&
1682 priv->bt_traffic_load ==
1683 IWL_BT_COEX_TRAFFIC_LOAD_NONE)
1686 memcpy(search_tbl, tbl, sz);
1687 if (rs_toggle_antenna(valid_tx_ant,
1688 &search_tbl->current_rate, search_tbl)) {
1689 update_search_tbl_counter = 1;
1693 case IWL_SISO_SWITCH_MIMO2_AB:
1694 case IWL_SISO_SWITCH_MIMO2_AC:
1695 case IWL_SISO_SWITCH_MIMO2_BC:
1696 IWL_DEBUG_RATE(priv, "LQ: SISO switch to MIMO2\n");
1697 memcpy(search_tbl, tbl, sz);
1698 search_tbl->is_SGI = 0;
1700 if (tbl->action == IWL_SISO_SWITCH_MIMO2_AB)
1701 search_tbl->ant_type = ANT_AB;
1702 else if (tbl->action == IWL_SISO_SWITCH_MIMO2_AC)
1703 search_tbl->ant_type = ANT_AC;
1705 search_tbl->ant_type = ANT_BC;
1707 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1710 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
1715 case IWL_SISO_SWITCH_GI:
1716 if (!tbl->is_ht40 && !(ht_cap->cap &
1717 IEEE80211_HT_CAP_SGI_20))
1719 if (tbl->is_ht40 && !(ht_cap->cap &
1720 IEEE80211_HT_CAP_SGI_40))
1723 IWL_DEBUG_RATE(priv, "LQ: SISO toggle SGI/NGI\n");
1725 memcpy(search_tbl, tbl, sz);
1731 "SGI was set in GF+SISO\n");
1733 search_tbl->is_SGI = !tbl->is_SGI;
1734 rs_set_expected_tpt_table(lq_sta, search_tbl);
1736 s32 tpt = lq_sta->last_tpt / 100;
1737 if (tpt >= search_tbl->expected_tpt[index])
1740 search_tbl->current_rate =
1741 rate_n_flags_from_tbl(priv, search_tbl,
1743 update_search_tbl_counter = 1;
1745 case IWL_SISO_SWITCH_MIMO3_ABC:
1746 IWL_DEBUG_RATE(priv, "LQ: SISO switch to MIMO3\n");
1747 memcpy(search_tbl, tbl, sz);
1748 search_tbl->is_SGI = 0;
1749 search_tbl->ant_type = ANT_ABC;
1751 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1754 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1761 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
1762 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1764 if (tbl->action == start_action)
1767 search_tbl->lq_type = LQ_NONE;
1771 lq_sta->search_better_tbl = 1;
1773 if (tbl->action > IWL_SISO_SWITCH_MIMO3_ABC)
1774 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1775 if (update_search_tbl_counter)
1776 search_tbl->action = tbl->action;
1782 * Try to switch to new modulation mode from MIMO2
1784 static int rs_move_mimo2_to_other(struct iwl_priv *priv,
1785 struct iwl_lq_sta *lq_sta,
1786 struct ieee80211_conf *conf,
1787 struct ieee80211_sta *sta, int index)
1789 s8 is_green = lq_sta->is_green;
1790 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1791 struct iwl_scale_tbl_info *search_tbl =
1792 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1793 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1794 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
1795 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1796 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1798 u8 valid_tx_ant = priv->nvm_data->valid_tx_ant;
1799 u8 tx_chains_num = priv->hw_params.tx_chains_num;
1800 u8 update_search_tbl_counter = 0;
1803 switch (priv->bt_traffic_load) {
1804 case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
1807 case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
1808 case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
1809 /* avoid antenna B and MIMO */
1810 if (tbl->action != IWL_MIMO2_SWITCH_SISO_A)
1811 tbl->action = IWL_MIMO2_SWITCH_SISO_A;
1813 case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
1814 /* avoid antenna B unless MIMO */
1815 if (tbl->action == IWL_MIMO2_SWITCH_SISO_B ||
1816 tbl->action == IWL_MIMO2_SWITCH_SISO_C)
1817 tbl->action = IWL_MIMO2_SWITCH_SISO_A;
1820 IWL_ERR(priv, "Invalid BT load %d", priv->bt_traffic_load);
1824 if ((iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE) &&
1825 (tbl->action < IWL_MIMO2_SWITCH_SISO_A ||
1826 tbl->action > IWL_MIMO2_SWITCH_SISO_C)) {
1827 /* switch in SISO */
1828 tbl->action = IWL_MIMO2_SWITCH_SISO_A;
1831 /* configure as 1x1 if bt full concurrency */
1832 if (priv->bt_full_concurrent &&
1833 (tbl->action < IWL_MIMO2_SWITCH_SISO_A ||
1834 tbl->action > IWL_MIMO2_SWITCH_SISO_C))
1835 tbl->action = IWL_MIMO2_SWITCH_SISO_A;
1837 start_action = tbl->action;
1839 lq_sta->action_counter++;
1840 switch (tbl->action) {
1841 case IWL_MIMO2_SWITCH_ANTENNA1:
1842 case IWL_MIMO2_SWITCH_ANTENNA2:
1843 IWL_DEBUG_RATE(priv, "LQ: MIMO2 toggle Antennas\n");
1845 if (tx_chains_num <= 2)
1848 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1851 memcpy(search_tbl, tbl, sz);
1852 if (rs_toggle_antenna(valid_tx_ant,
1853 &search_tbl->current_rate, search_tbl)) {
1854 update_search_tbl_counter = 1;
1858 case IWL_MIMO2_SWITCH_SISO_A:
1859 case IWL_MIMO2_SWITCH_SISO_B:
1860 case IWL_MIMO2_SWITCH_SISO_C:
1861 IWL_DEBUG_RATE(priv, "LQ: MIMO2 switch to SISO\n");
1863 /* Set up new search table for SISO */
1864 memcpy(search_tbl, tbl, sz);
1866 if (tbl->action == IWL_MIMO2_SWITCH_SISO_A)
1867 search_tbl->ant_type = ANT_A;
1868 else if (tbl->action == IWL_MIMO2_SWITCH_SISO_B)
1869 search_tbl->ant_type = ANT_B;
1871 search_tbl->ant_type = ANT_C;
1873 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1876 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
1883 case IWL_MIMO2_SWITCH_GI:
1884 if (!tbl->is_ht40 && !(ht_cap->cap &
1885 IEEE80211_HT_CAP_SGI_20))
1887 if (tbl->is_ht40 && !(ht_cap->cap &
1888 IEEE80211_HT_CAP_SGI_40))
1891 IWL_DEBUG_RATE(priv, "LQ: MIMO2 toggle SGI/NGI\n");
1893 /* Set up new search table for MIMO2 */
1894 memcpy(search_tbl, tbl, sz);
1895 search_tbl->is_SGI = !tbl->is_SGI;
1896 rs_set_expected_tpt_table(lq_sta, search_tbl);
1898 * If active table already uses the fastest possible
1899 * modulation (dual stream with short guard interval),
1900 * and it's working well, there's no need to look
1901 * for a better type of modulation!
1904 s32 tpt = lq_sta->last_tpt / 100;
1905 if (tpt >= search_tbl->expected_tpt[index])
1908 search_tbl->current_rate =
1909 rate_n_flags_from_tbl(priv, search_tbl,
1911 update_search_tbl_counter = 1;
1914 case IWL_MIMO2_SWITCH_MIMO3_ABC:
1915 IWL_DEBUG_RATE(priv, "LQ: MIMO2 switch to MIMO3\n");
1916 memcpy(search_tbl, tbl, sz);
1917 search_tbl->is_SGI = 0;
1918 search_tbl->ant_type = ANT_ABC;
1920 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1923 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1931 if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
1932 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
1934 if (tbl->action == start_action)
1937 search_tbl->lq_type = LQ_NONE;
1940 lq_sta->search_better_tbl = 1;
1942 if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
1943 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
1944 if (update_search_tbl_counter)
1945 search_tbl->action = tbl->action;
1952 * Try to switch to new modulation mode from MIMO3
1954 static int rs_move_mimo3_to_other(struct iwl_priv *priv,
1955 struct iwl_lq_sta *lq_sta,
1956 struct ieee80211_conf *conf,
1957 struct ieee80211_sta *sta, int index)
1959 s8 is_green = lq_sta->is_green;
1960 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1961 struct iwl_scale_tbl_info *search_tbl =
1962 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1963 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1964 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
1965 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1966 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1968 u8 valid_tx_ant = priv->nvm_data->valid_tx_ant;
1969 u8 tx_chains_num = priv->hw_params.tx_chains_num;
1971 u8 update_search_tbl_counter = 0;
1973 switch (priv->bt_traffic_load) {
1974 case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
1977 case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
1978 case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
1979 /* avoid antenna B and MIMO */
1980 if (tbl->action != IWL_MIMO3_SWITCH_SISO_A)
1981 tbl->action = IWL_MIMO3_SWITCH_SISO_A;
1983 case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
1984 /* avoid antenna B unless MIMO */
1985 if (tbl->action == IWL_MIMO3_SWITCH_SISO_B ||
1986 tbl->action == IWL_MIMO3_SWITCH_SISO_C)
1987 tbl->action = IWL_MIMO3_SWITCH_SISO_A;
1990 IWL_ERR(priv, "Invalid BT load %d", priv->bt_traffic_load);
1994 if ((iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE) &&
1995 (tbl->action < IWL_MIMO3_SWITCH_SISO_A ||
1996 tbl->action > IWL_MIMO3_SWITCH_SISO_C)) {
1997 /* switch in SISO */
1998 tbl->action = IWL_MIMO3_SWITCH_SISO_A;
2001 /* configure as 1x1 if bt full concurrency */
2002 if (priv->bt_full_concurrent &&
2003 (tbl->action < IWL_MIMO3_SWITCH_SISO_A ||
2004 tbl->action > IWL_MIMO3_SWITCH_SISO_C))
2005 tbl->action = IWL_MIMO3_SWITCH_SISO_A;
2007 start_action = tbl->action;
2009 lq_sta->action_counter++;
2010 switch (tbl->action) {
2011 case IWL_MIMO3_SWITCH_ANTENNA1:
2012 case IWL_MIMO3_SWITCH_ANTENNA2:
2013 IWL_DEBUG_RATE(priv, "LQ: MIMO3 toggle Antennas\n");
2015 if (tx_chains_num <= 3)
2018 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
2021 memcpy(search_tbl, tbl, sz);
2022 if (rs_toggle_antenna(valid_tx_ant,
2023 &search_tbl->current_rate, search_tbl))
2026 case IWL_MIMO3_SWITCH_SISO_A:
2027 case IWL_MIMO3_SWITCH_SISO_B:
2028 case IWL_MIMO3_SWITCH_SISO_C:
2029 IWL_DEBUG_RATE(priv, "LQ: MIMO3 switch to SISO\n");
2031 /* Set up new search table for SISO */
2032 memcpy(search_tbl, tbl, sz);
2034 if (tbl->action == IWL_MIMO3_SWITCH_SISO_A)
2035 search_tbl->ant_type = ANT_A;
2036 else if (tbl->action == IWL_MIMO3_SWITCH_SISO_B)
2037 search_tbl->ant_type = ANT_B;
2039 search_tbl->ant_type = ANT_C;
2041 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
2044 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
2051 case IWL_MIMO3_SWITCH_MIMO2_AB:
2052 case IWL_MIMO3_SWITCH_MIMO2_AC:
2053 case IWL_MIMO3_SWITCH_MIMO2_BC:
2054 IWL_DEBUG_RATE(priv, "LQ: MIMO3 switch to MIMO2\n");
2056 memcpy(search_tbl, tbl, sz);
2057 search_tbl->is_SGI = 0;
2058 if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AB)
2059 search_tbl->ant_type = ANT_AB;
2060 else if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AC)
2061 search_tbl->ant_type = ANT_AC;
2063 search_tbl->ant_type = ANT_BC;
2065 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
2068 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
2075 case IWL_MIMO3_SWITCH_GI:
2076 if (!tbl->is_ht40 && !(ht_cap->cap &
2077 IEEE80211_HT_CAP_SGI_20))
2079 if (tbl->is_ht40 && !(ht_cap->cap &
2080 IEEE80211_HT_CAP_SGI_40))
2083 IWL_DEBUG_RATE(priv, "LQ: MIMO3 toggle SGI/NGI\n");
2085 /* Set up new search table for MIMO */
2086 memcpy(search_tbl, tbl, sz);
2087 search_tbl->is_SGI = !tbl->is_SGI;
2088 rs_set_expected_tpt_table(lq_sta, search_tbl);
2090 * If active table already uses the fastest possible
2091 * modulation (dual stream with short guard interval),
2092 * and it's working well, there's no need to look
2093 * for a better type of modulation!
2096 s32 tpt = lq_sta->last_tpt / 100;
2097 if (tpt >= search_tbl->expected_tpt[index])
2100 search_tbl->current_rate =
2101 rate_n_flags_from_tbl(priv, search_tbl,
2103 update_search_tbl_counter = 1;
2107 if (tbl->action > IWL_MIMO3_SWITCH_GI)
2108 tbl->action = IWL_MIMO3_SWITCH_ANTENNA1;
2110 if (tbl->action == start_action)
2113 search_tbl->lq_type = LQ_NONE;
2116 lq_sta->search_better_tbl = 1;
2118 if (tbl->action > IWL_MIMO3_SWITCH_GI)
2119 tbl->action = IWL_MIMO3_SWITCH_ANTENNA1;
2120 if (update_search_tbl_counter)
2121 search_tbl->action = tbl->action;
2128 * Check whether we should continue using same modulation mode, or
2129 * begin search for a new mode, based on:
2130 * 1) # tx successes or failures while using this mode
2131 * 2) # times calling this function
2132 * 3) elapsed time in this mode (not used, for now)
2134 static void rs_stay_in_table(struct iwl_lq_sta *lq_sta, bool force_search)
2136 struct iwl_scale_tbl_info *tbl;
2139 int flush_interval_passed = 0;
2140 struct iwl_priv *priv;
2143 active_tbl = lq_sta->active_tbl;
2145 tbl = &(lq_sta->lq_info[active_tbl]);
2147 /* If we've been disallowing search, see if we should now allow it */
2148 if (lq_sta->stay_in_tbl) {
2150 /* Elapsed time using current modulation mode */
2151 if (lq_sta->flush_timer)
2152 flush_interval_passed =
2154 (unsigned long)(lq_sta->flush_timer +
2155 IWL_RATE_SCALE_FLUSH_INTVL));
2158 * Check if we should allow search for new modulation mode.
2159 * If many frames have failed or succeeded, or we've used
2160 * this same modulation for a long time, allow search, and
2161 * reset history stats that keep track of whether we should
2162 * allow a new search. Also (below) reset all bitmaps and
2163 * stats in active history.
2166 (lq_sta->total_failed > lq_sta->max_failure_limit) ||
2167 (lq_sta->total_success > lq_sta->max_success_limit) ||
2168 ((!lq_sta->search_better_tbl) && (lq_sta->flush_timer)
2169 && (flush_interval_passed))) {
2170 IWL_DEBUG_RATE(priv, "LQ: stay is expired %d %d %d\n",
2171 lq_sta->total_failed,
2172 lq_sta->total_success,
2173 flush_interval_passed);
2175 /* Allow search for new mode */
2176 lq_sta->stay_in_tbl = 0; /* only place reset */
2177 lq_sta->total_failed = 0;
2178 lq_sta->total_success = 0;
2179 lq_sta->flush_timer = 0;
2182 * Else if we've used this modulation mode enough repetitions
2183 * (regardless of elapsed time or success/failure), reset
2184 * history bitmaps and rate-specific stats for all rates in
2188 lq_sta->table_count++;
2189 if (lq_sta->table_count >=
2190 lq_sta->table_count_limit) {
2191 lq_sta->table_count = 0;
2193 IWL_DEBUG_RATE(priv, "LQ: stay in table clear win\n");
2194 for (i = 0; i < IWL_RATE_COUNT; i++)
2195 rs_rate_scale_clear_window(
2200 /* If transitioning to allow "search", reset all history
2201 * bitmaps and stats in active table (this will become the new
2202 * "search" table). */
2203 if (!lq_sta->stay_in_tbl) {
2204 for (i = 0; i < IWL_RATE_COUNT; i++)
2205 rs_rate_scale_clear_window(&(tbl->win[i]));
2211 * setup rate table in uCode
2213 static void rs_update_rate_tbl(struct iwl_priv *priv,
2214 struct iwl_rxon_context *ctx,
2215 struct iwl_lq_sta *lq_sta,
2216 struct iwl_scale_tbl_info *tbl,
2217 int index, u8 is_green)
2221 /* Update uCode's rate table. */
2222 rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
2223 rs_fill_link_cmd(priv, lq_sta, rate);
2224 iwl_send_lq_cmd(priv, ctx, &lq_sta->lq, CMD_ASYNC, false);
2228 * Do rate scaling and search for new modulation mode.
2230 static void rs_rate_scale_perform(struct iwl_priv *priv,
2231 struct sk_buff *skb,
2232 struct ieee80211_sta *sta,
2233 struct iwl_lq_sta *lq_sta)
2235 struct ieee80211_hw *hw = priv->hw;
2236 struct ieee80211_conf *conf = &hw->conf;
2237 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2238 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2239 int low = IWL_RATE_INVALID;
2240 int high = IWL_RATE_INVALID;
2243 struct iwl_rate_scale_data *window = NULL;
2244 int current_tpt = IWL_INVALID_VALUE;
2245 int low_tpt = IWL_INVALID_VALUE;
2246 int high_tpt = IWL_INVALID_VALUE;
2248 s8 scale_action = 0;
2251 struct iwl_scale_tbl_info *tbl, *tbl1;
2252 u16 rate_scale_index_msk = 0;
2258 u8 tid = IWL_MAX_TID_COUNT;
2259 struct iwl_tid_data *tid_data;
2260 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
2261 struct iwl_rxon_context *ctx = sta_priv->ctx;
2263 IWL_DEBUG_RATE(priv, "rate scale calculate new rate for skb\n");
2265 /* Send management frames and NO_ACK data using lowest rate. */
2266 /* TODO: this could probably be improved.. */
2267 if (!ieee80211_is_data(hdr->frame_control) ||
2268 info->flags & IEEE80211_TX_CTL_NO_ACK)
2271 lq_sta->supp_rates = sta->supp_rates[lq_sta->band];
2273 tid = rs_tl_add_packet(lq_sta, hdr);
2274 if ((tid != IWL_MAX_TID_COUNT) &&
2275 (lq_sta->tx_agg_tid_en & (1 << tid))) {
2276 tid_data = &priv->tid_data[lq_sta->lq.sta_id][tid];
2277 if (tid_data->agg.state == IWL_AGG_OFF)
2285 * Select rate-scale / modulation-mode table to work with in
2286 * the rest of this function: "search" if searching for better
2287 * modulation mode, or "active" if doing rate scaling within a mode.
2289 if (!lq_sta->search_better_tbl)
2290 active_tbl = lq_sta->active_tbl;
2292 active_tbl = 1 - lq_sta->active_tbl;
2294 tbl = &(lq_sta->lq_info[active_tbl]);
2295 if (is_legacy(tbl->lq_type))
2296 lq_sta->is_green = 0;
2298 lq_sta->is_green = rs_use_green(sta);
2299 is_green = lq_sta->is_green;
2301 /* current tx rate */
2302 index = lq_sta->last_txrate_idx;
2304 IWL_DEBUG_RATE(priv, "Rate scale index %d for type %d\n", index,
2307 /* rates available for this association, and for modulation mode */
2308 rate_mask = rs_get_supported_rates(lq_sta, hdr, tbl->lq_type);
2310 IWL_DEBUG_RATE(priv, "mask 0x%04X\n", rate_mask);
2312 /* mask with station rate restriction */
2313 if (is_legacy(tbl->lq_type)) {
2314 if (lq_sta->band == IEEE80211_BAND_5GHZ)
2315 /* supp_rates has no CCK bits in A mode */
2316 rate_scale_index_msk = (u16) (rate_mask &
2317 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
2319 rate_scale_index_msk = (u16) (rate_mask &
2320 lq_sta->supp_rates);
2323 rate_scale_index_msk = rate_mask;
2325 if (!rate_scale_index_msk)
2326 rate_scale_index_msk = rate_mask;
2328 if (!((1 << index) & rate_scale_index_msk)) {
2329 IWL_ERR(priv, "Current Rate is not valid\n");
2330 if (lq_sta->search_better_tbl) {
2331 /* revert to active table if search table is not valid*/
2332 tbl->lq_type = LQ_NONE;
2333 lq_sta->search_better_tbl = 0;
2334 tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
2335 /* get "active" rate info */
2336 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
2337 rs_update_rate_tbl(priv, ctx, lq_sta, tbl,
2343 /* Get expected throughput table and history window for current rate */
2344 if (!tbl->expected_tpt) {
2345 IWL_ERR(priv, "tbl->expected_tpt is NULL\n");
2349 /* force user max rate if set by user */
2350 if ((lq_sta->max_rate_idx != -1) &&
2351 (lq_sta->max_rate_idx < index)) {
2352 index = lq_sta->max_rate_idx;
2354 window = &(tbl->win[index]);
2358 window = &(tbl->win[index]);
2361 * If there is not enough history to calculate actual average
2362 * throughput, keep analyzing results of more tx frames, without
2363 * changing rate or mode (bypass most of the rest of this function).
2364 * Set up new rate table in uCode only if old rate is not supported
2365 * in current association (use new rate found above).
2367 fail_count = window->counter - window->success_counter;
2368 if ((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
2369 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH)) {
2370 IWL_DEBUG_RATE(priv, "LQ: still below TH. succ=%d total=%d "
2372 window->success_counter, window->counter, index);
2374 /* Can't calculate this yet; not enough history */
2375 window->average_tpt = IWL_INVALID_VALUE;
2377 /* Should we stay with this modulation mode,
2378 * or search for a new one? */
2379 rs_stay_in_table(lq_sta, false);
2383 /* Else we have enough samples; calculate estimate of
2384 * actual average throughput */
2385 if (window->average_tpt != ((window->success_ratio *
2386 tbl->expected_tpt[index] + 64) / 128)) {
2387 IWL_ERR(priv, "expected_tpt should have been calculated by now\n");
2388 window->average_tpt = ((window->success_ratio *
2389 tbl->expected_tpt[index] + 64) / 128);
2392 /* If we are searching for better modulation mode, check success. */
2393 if (lq_sta->search_better_tbl &&
2394 (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_MULTI)) {
2395 /* If good success, continue using the "search" mode;
2396 * no need to send new link quality command, since we're
2397 * continuing to use the setup that we've been trying. */
2398 if (window->average_tpt > lq_sta->last_tpt) {
2400 IWL_DEBUG_RATE(priv, "LQ: SWITCHING TO NEW TABLE "
2401 "suc=%d cur-tpt=%d old-tpt=%d\n",
2402 window->success_ratio,
2403 window->average_tpt,
2406 if (!is_legacy(tbl->lq_type))
2407 lq_sta->enable_counter = 1;
2409 /* Swap tables; "search" becomes "active" */
2410 lq_sta->active_tbl = active_tbl;
2411 current_tpt = window->average_tpt;
2413 /* Else poor success; go back to mode in "active" table */
2416 IWL_DEBUG_RATE(priv, "LQ: GOING BACK TO THE OLD TABLE "
2417 "suc=%d cur-tpt=%d old-tpt=%d\n",
2418 window->success_ratio,
2419 window->average_tpt,
2422 /* Nullify "search" table */
2423 tbl->lq_type = LQ_NONE;
2425 /* Revert to "active" table */
2426 active_tbl = lq_sta->active_tbl;
2427 tbl = &(lq_sta->lq_info[active_tbl]);
2429 /* Revert to "active" rate and throughput info */
2430 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
2431 current_tpt = lq_sta->last_tpt;
2433 /* Need to set up a new rate table in uCode */
2437 /* Either way, we've made a decision; modulation mode
2438 * search is done, allow rate adjustment next time. */
2439 lq_sta->search_better_tbl = 0;
2440 done_search = 1; /* Don't switch modes below! */
2444 /* (Else) not in search of better modulation mode, try for better
2445 * starting rate, while staying in this mode. */
2446 high_low = rs_get_adjacent_rate(priv, index, rate_scale_index_msk,
2448 low = high_low & 0xff;
2449 high = (high_low >> 8) & 0xff;
2451 /* If user set max rate, dont allow higher than user constrain */
2452 if ((lq_sta->max_rate_idx != -1) &&
2453 (lq_sta->max_rate_idx < high))
2454 high = IWL_RATE_INVALID;
2456 sr = window->success_ratio;
2458 /* Collect measured throughputs for current and adjacent rates */
2459 current_tpt = window->average_tpt;
2460 if (low != IWL_RATE_INVALID)
2461 low_tpt = tbl->win[low].average_tpt;
2462 if (high != IWL_RATE_INVALID)
2463 high_tpt = tbl->win[high].average_tpt;
2467 /* Too many failures, decrease rate */
2468 if ((sr <= IWL_RATE_DECREASE_TH) || (current_tpt == 0)) {
2469 IWL_DEBUG_RATE(priv, "decrease rate because of low success_ratio\n");
2472 /* No throughput measured yet for adjacent rates; try increase. */
2473 } else if ((low_tpt == IWL_INVALID_VALUE) &&
2474 (high_tpt == IWL_INVALID_VALUE)) {
2476 if (high != IWL_RATE_INVALID && sr >= IWL_RATE_INCREASE_TH)
2478 else if (low != IWL_RATE_INVALID)
2482 /* Both adjacent throughputs are measured, but neither one has better
2483 * throughput; we're using the best rate, don't change it! */
2484 else if ((low_tpt != IWL_INVALID_VALUE) &&
2485 (high_tpt != IWL_INVALID_VALUE) &&
2486 (low_tpt < current_tpt) &&
2487 (high_tpt < current_tpt))
2490 /* At least one adjacent rate's throughput is measured,
2491 * and may have better performance. */
2493 /* Higher adjacent rate's throughput is measured */
2494 if (high_tpt != IWL_INVALID_VALUE) {
2495 /* Higher rate has better throughput */
2496 if (high_tpt > current_tpt &&
2497 sr >= IWL_RATE_INCREASE_TH) {
2503 /* Lower adjacent rate's throughput is measured */
2504 } else if (low_tpt != IWL_INVALID_VALUE) {
2505 /* Lower rate has better throughput */
2506 if (low_tpt > current_tpt) {
2507 IWL_DEBUG_RATE(priv,
2508 "decrease rate because of low tpt\n");
2510 } else if (sr >= IWL_RATE_INCREASE_TH) {
2516 /* Sanity check; asked for decrease, but success rate or throughput
2517 * has been good at old rate. Don't change it. */
2518 if ((scale_action == -1) && (low != IWL_RATE_INVALID) &&
2519 ((sr > IWL_RATE_HIGH_TH) ||
2520 (current_tpt > (100 * tbl->expected_tpt[low]))))
2522 if (!iwl_ht_enabled(priv) && !is_legacy(tbl->lq_type))
2524 if (iwl_tx_ant_restriction(priv) != IWL_ANT_OK_MULTI &&
2525 (is_mimo2(tbl->lq_type) || is_mimo3(tbl->lq_type)))
2528 if ((priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH) &&
2529 (is_mimo2(tbl->lq_type) || is_mimo3(tbl->lq_type))) {
2530 if (lq_sta->last_bt_traffic > priv->bt_traffic_load) {
2532 * don't set scale_action, don't want to scale up if
2533 * the rate scale doesn't otherwise think that is a
2536 } else if (lq_sta->last_bt_traffic <= priv->bt_traffic_load) {
2540 lq_sta->last_bt_traffic = priv->bt_traffic_load;
2542 if ((priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH) &&
2543 (is_mimo2(tbl->lq_type) || is_mimo3(tbl->lq_type))) {
2544 /* search for a new modulation */
2545 rs_stay_in_table(lq_sta, true);
2549 switch (scale_action) {
2551 /* Decrease starting rate, update uCode's rate table */
2552 if (low != IWL_RATE_INVALID) {
2559 /* Increase starting rate, update uCode's rate table */
2560 if (high != IWL_RATE_INVALID) {
2572 IWL_DEBUG_RATE(priv, "choose rate scale index %d action %d low %d "
2573 "high %d type %d\n",
2574 index, scale_action, low, high, tbl->lq_type);
2577 /* Replace uCode's rate table for the destination station. */
2579 rs_update_rate_tbl(priv, ctx, lq_sta, tbl, index, is_green);
2581 if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_MULTI) {
2582 /* Should we stay with this modulation mode,
2583 * or search for a new one? */
2584 rs_stay_in_table(lq_sta, false);
2587 * Search for new modulation mode if we're:
2588 * 1) Not changing rates right now
2589 * 2) Not just finishing up a search
2590 * 3) Allowing a new search
2592 if (!update_lq && !done_search && !lq_sta->stay_in_tbl && window->counter) {
2593 /* Save current throughput to compare with "search" throughput*/
2594 lq_sta->last_tpt = current_tpt;
2596 /* Select a new "search" modulation mode to try.
2597 * If one is found, set up the new "search" table. */
2598 if (is_legacy(tbl->lq_type))
2599 rs_move_legacy_other(priv, lq_sta, conf, sta, index);
2600 else if (is_siso(tbl->lq_type))
2601 rs_move_siso_to_other(priv, lq_sta, conf, sta, index);
2602 else if (is_mimo2(tbl->lq_type))
2603 rs_move_mimo2_to_other(priv, lq_sta, conf, sta, index);
2605 rs_move_mimo3_to_other(priv, lq_sta, conf, sta, index);
2607 /* If new "search" mode was selected, set up in uCode table */
2608 if (lq_sta->search_better_tbl) {
2609 /* Access the "search" table, clear its history. */
2610 tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
2611 for (i = 0; i < IWL_RATE_COUNT; i++)
2612 rs_rate_scale_clear_window(&(tbl->win[i]));
2614 /* Use new "search" start rate */
2615 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
2617 IWL_DEBUG_RATE(priv, "Switch current mcs: %X index: %d\n",
2618 tbl->current_rate, index);
2619 rs_fill_link_cmd(priv, lq_sta, tbl->current_rate);
2620 iwl_send_lq_cmd(priv, ctx, &lq_sta->lq, CMD_ASYNC, false);
2625 if (done_search && !lq_sta->stay_in_tbl) {
2626 /* If the "active" (non-search) mode was legacy,
2627 * and we've tried switching antennas,
2628 * but we haven't been able to try HT modes (not available),
2629 * stay with best antenna legacy modulation for a while
2630 * before next round of mode comparisons. */
2631 tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
2632 if (is_legacy(tbl1->lq_type) && !conf_is_ht(conf) &&
2633 lq_sta->action_counter > tbl1->max_search) {
2634 IWL_DEBUG_RATE(priv, "LQ: STAY in legacy table\n");
2635 rs_set_stay_in_table(priv, 1, lq_sta);
2638 /* If we're in an HT mode, and all 3 mode switch actions
2639 * have been tried and compared, stay in this best modulation
2640 * mode for a while before next round of mode comparisons. */
2641 if (lq_sta->enable_counter &&
2642 (lq_sta->action_counter >= tbl1->max_search) &&
2643 iwl_ht_enabled(priv)) {
2644 if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
2645 (lq_sta->tx_agg_tid_en & (1 << tid)) &&
2646 (tid != IWL_MAX_TID_COUNT)) {
2647 u8 sta_id = lq_sta->lq.sta_id;
2648 tid_data = &priv->tid_data[sta_id][tid];
2649 if (tid_data->agg.state == IWL_AGG_OFF) {
2650 IWL_DEBUG_RATE(priv,
2651 "try to aggregate tid %d\n",
2653 rs_tl_turn_on_agg(priv, tid,
2657 rs_set_stay_in_table(priv, 0, lq_sta);
2662 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
2663 lq_sta->last_txrate_idx = index;
2667 * rs_initialize_lq - Initialize a station's hardware rate table
2669 * The uCode's station table contains a table of fallback rates
2670 * for automatic fallback during transmission.
2672 * NOTE: This sets up a default set of values. These will be replaced later
2673 * if the driver's iwl-agn-rs rate scaling algorithm is used, instead of
2676 * NOTE: Run REPLY_ADD_STA command to set up station table entry, before
2677 * calling this function (which runs REPLY_TX_LINK_QUALITY_CMD,
2678 * which requires station table entry to exist).
2680 static void rs_initialize_lq(struct iwl_priv *priv,
2681 struct ieee80211_sta *sta,
2682 struct iwl_lq_sta *lq_sta)
2684 struct iwl_scale_tbl_info *tbl;
2688 u8 use_green = rs_use_green(sta);
2691 struct iwl_station_priv *sta_priv;
2692 struct iwl_rxon_context *ctx;
2694 if (!sta || !lq_sta)
2697 sta_priv = (void *)sta->drv_priv;
2698 ctx = sta_priv->ctx;
2700 i = lq_sta->last_txrate_idx;
2702 valid_tx_ant = priv->nvm_data->valid_tx_ant;
2704 if (!lq_sta->search_better_tbl)
2705 active_tbl = lq_sta->active_tbl;
2707 active_tbl = 1 - lq_sta->active_tbl;
2709 tbl = &(lq_sta->lq_info[active_tbl]);
2711 if ((i < 0) || (i >= IWL_RATE_COUNT))
2714 rate = iwl_rates[i].plcp;
2715 tbl->ant_type = first_antenna(valid_tx_ant);
2716 rate |= tbl->ant_type << RATE_MCS_ANT_POS;
2718 if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
2719 rate |= RATE_MCS_CCK_MSK;
2721 rs_get_tbl_info_from_mcs(rate, priv->band, tbl, &rate_idx);
2722 if (!rs_is_valid_ant(valid_tx_ant, tbl->ant_type))
2723 rs_toggle_antenna(valid_tx_ant, &rate, tbl);
2725 rate = rate_n_flags_from_tbl(priv, tbl, rate_idx, use_green);
2726 tbl->current_rate = rate;
2727 rs_set_expected_tpt_table(lq_sta, tbl);
2728 rs_fill_link_cmd(NULL, lq_sta, rate);
2729 priv->stations[lq_sta->lq.sta_id].lq = &lq_sta->lq;
2730 iwl_send_lq_cmd(priv, ctx, &lq_sta->lq, CMD_SYNC, true);
2733 static void rs_get_rate(void *priv_r, struct ieee80211_sta *sta, void *priv_sta,
2734 struct ieee80211_tx_rate_control *txrc)
2737 struct sk_buff *skb = txrc->skb;
2738 struct ieee80211_supported_band *sband = txrc->sband;
2739 struct iwl_op_mode *op_mode __maybe_unused =
2740 (struct iwl_op_mode *)priv_r;
2741 struct iwl_priv *priv __maybe_unused = IWL_OP_MODE_GET_DVM(op_mode);
2742 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2743 struct iwl_lq_sta *lq_sta = priv_sta;
2746 IWL_DEBUG_RATE_LIMIT(priv, "rate scale calculate new rate for skb\n");
2748 /* Get max rate if user set max rate */
2750 lq_sta->max_rate_idx = txrc->max_rate_idx;
2751 if ((sband->band == IEEE80211_BAND_5GHZ) &&
2752 (lq_sta->max_rate_idx != -1))
2753 lq_sta->max_rate_idx += IWL_FIRST_OFDM_RATE;
2754 if ((lq_sta->max_rate_idx < 0) ||
2755 (lq_sta->max_rate_idx >= IWL_RATE_COUNT))
2756 lq_sta->max_rate_idx = -1;
2759 /* Treat uninitialized rate scaling data same as non-existing. */
2760 if (lq_sta && !lq_sta->drv) {
2761 IWL_DEBUG_RATE(priv, "Rate scaling not initialized yet.\n");
2765 /* Send management frames and NO_ACK data using lowest rate. */
2766 if (rate_control_send_low(sta, priv_sta, txrc))
2769 rate_idx = lq_sta->last_txrate_idx;
2771 if (lq_sta->last_rate_n_flags & RATE_MCS_HT_MSK) {
2772 rate_idx -= IWL_FIRST_OFDM_RATE;
2773 /* 6M and 9M shared same MCS index */
2774 rate_idx = (rate_idx > 0) ? (rate_idx - 1) : 0;
2775 if (rs_extract_rate(lq_sta->last_rate_n_flags) >=
2776 IWL_RATE_MIMO3_6M_PLCP)
2777 rate_idx = rate_idx + (2 * MCS_INDEX_PER_STREAM);
2778 else if (rs_extract_rate(lq_sta->last_rate_n_flags) >=
2779 IWL_RATE_MIMO2_6M_PLCP)
2780 rate_idx = rate_idx + MCS_INDEX_PER_STREAM;
2781 info->control.rates[0].flags = IEEE80211_TX_RC_MCS;
2782 if (lq_sta->last_rate_n_flags & RATE_MCS_SGI_MSK)
2783 info->control.rates[0].flags |= IEEE80211_TX_RC_SHORT_GI;
2784 if (lq_sta->last_rate_n_flags & RATE_MCS_DUP_MSK)
2785 info->control.rates[0].flags |= IEEE80211_TX_RC_DUP_DATA;
2786 if (lq_sta->last_rate_n_flags & RATE_MCS_HT40_MSK)
2787 info->control.rates[0].flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
2788 if (lq_sta->last_rate_n_flags & RATE_MCS_GF_MSK)
2789 info->control.rates[0].flags |= IEEE80211_TX_RC_GREEN_FIELD;
2791 /* Check for invalid rates */
2792 if ((rate_idx < 0) || (rate_idx >= IWL_RATE_COUNT_LEGACY) ||
2793 ((sband->band == IEEE80211_BAND_5GHZ) &&
2794 (rate_idx < IWL_FIRST_OFDM_RATE)))
2795 rate_idx = rate_lowest_index(sband, sta);
2796 /* On valid 5 GHz rate, adjust index */
2797 else if (sband->band == IEEE80211_BAND_5GHZ)
2798 rate_idx -= IWL_FIRST_OFDM_RATE;
2799 info->control.rates[0].flags = 0;
2801 info->control.rates[0].idx = rate_idx;
2802 info->control.rates[0].count = 1;
2805 static void *rs_alloc_sta(void *priv_rate, struct ieee80211_sta *sta,
2808 struct iwl_station_priv *sta_priv = (struct iwl_station_priv *) sta->drv_priv;
2809 struct iwl_op_mode *op_mode __maybe_unused =
2810 (struct iwl_op_mode *)priv_rate;
2811 struct iwl_priv *priv __maybe_unused = IWL_OP_MODE_GET_DVM(op_mode);
2813 IWL_DEBUG_RATE(priv, "create station rate scale window\n");
2815 return &sta_priv->lq_sta;
2819 * Called after adding a new station to initialize rate scaling
2821 void iwl_rs_rate_init(struct iwl_priv *priv, struct ieee80211_sta *sta, u8 sta_id)
2824 struct ieee80211_hw *hw = priv->hw;
2825 struct ieee80211_conf *conf = &priv->hw->conf;
2826 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
2827 struct iwl_station_priv *sta_priv;
2828 struct iwl_lq_sta *lq_sta;
2829 struct ieee80211_supported_band *sband;
2830 unsigned long supp; /* must be unsigned long for for_each_set_bit */
2832 sta_priv = (struct iwl_station_priv *) sta->drv_priv;
2833 lq_sta = &sta_priv->lq_sta;
2834 sband = hw->wiphy->bands[conf->chandef.chan->band];
2837 lq_sta->lq.sta_id = sta_id;
2839 for (j = 0; j < LQ_SIZE; j++)
2840 for (i = 0; i < IWL_RATE_COUNT; i++)
2841 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
2843 lq_sta->flush_timer = 0;
2844 lq_sta->supp_rates = sta->supp_rates[sband->band];
2845 for (j = 0; j < LQ_SIZE; j++)
2846 for (i = 0; i < IWL_RATE_COUNT; i++)
2847 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
2849 IWL_DEBUG_RATE(priv, "LQ: *** rate scale station global init for station %d ***\n",
2851 /* TODO: what is a good starting rate for STA? About middle? Maybe not
2852 * the lowest or the highest rate.. Could consider using RSSI from
2853 * previous packets? Need to have IEEE 802.1X auth succeed immediately
2857 lq_sta->max_rate_idx = -1;
2858 lq_sta->missed_rate_counter = IWL_MISSED_RATE_MAX;
2859 lq_sta->is_green = rs_use_green(sta);
2860 lq_sta->band = sband->band;
2862 * active legacy rates as per supported rates bitmap
2864 supp = sta->supp_rates[sband->band];
2865 lq_sta->active_legacy_rate = 0;
2866 for_each_set_bit(i, &supp, BITS_PER_LONG)
2867 lq_sta->active_legacy_rate |= BIT(sband->bitrates[i].hw_value);
2870 * active_siso_rate mask includes 9 MBits (bit 5), and CCK (bits 0-3),
2871 * supp_rates[] does not; shift to convert format, force 9 MBits off.
2873 lq_sta->active_siso_rate = ht_cap->mcs.rx_mask[0] << 1;
2874 lq_sta->active_siso_rate |= ht_cap->mcs.rx_mask[0] & 0x1;
2875 lq_sta->active_siso_rate &= ~((u16)0x2);
2876 lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE;
2879 lq_sta->active_mimo2_rate = ht_cap->mcs.rx_mask[1] << 1;
2880 lq_sta->active_mimo2_rate |= ht_cap->mcs.rx_mask[1] & 0x1;
2881 lq_sta->active_mimo2_rate &= ~((u16)0x2);
2882 lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE;
2884 lq_sta->active_mimo3_rate = ht_cap->mcs.rx_mask[2] << 1;
2885 lq_sta->active_mimo3_rate |= ht_cap->mcs.rx_mask[2] & 0x1;
2886 lq_sta->active_mimo3_rate &= ~((u16)0x2);
2887 lq_sta->active_mimo3_rate <<= IWL_FIRST_OFDM_RATE;
2889 IWL_DEBUG_RATE(priv, "SISO-RATE=%X MIMO2-RATE=%X MIMO3-RATE=%X\n",
2890 lq_sta->active_siso_rate,
2891 lq_sta->active_mimo2_rate,
2892 lq_sta->active_mimo3_rate);
2894 /* These values will be overridden later */
2895 lq_sta->lq.general_params.single_stream_ant_msk =
2896 first_antenna(priv->nvm_data->valid_tx_ant);
2897 lq_sta->lq.general_params.dual_stream_ant_msk =
2898 priv->nvm_data->valid_tx_ant &
2899 ~first_antenna(priv->nvm_data->valid_tx_ant);
2900 if (!lq_sta->lq.general_params.dual_stream_ant_msk) {
2901 lq_sta->lq.general_params.dual_stream_ant_msk = ANT_AB;
2902 } else if (num_of_ant(priv->nvm_data->valid_tx_ant) == 2) {
2903 lq_sta->lq.general_params.dual_stream_ant_msk =
2904 priv->nvm_data->valid_tx_ant;
2907 /* as default allow aggregation for all tids */
2908 lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
2911 /* Set last_txrate_idx to lowest rate */
2912 lq_sta->last_txrate_idx = rate_lowest_index(sband, sta);
2913 if (sband->band == IEEE80211_BAND_5GHZ)
2914 lq_sta->last_txrate_idx += IWL_FIRST_OFDM_RATE;
2916 #ifdef CONFIG_IWLWIFI_DEVICE_TESTMODE
2917 priv->tm_fixed_rate = 0;
2919 #ifdef CONFIG_MAC80211_DEBUGFS
2920 lq_sta->dbg_fixed_rate = 0;
2923 rs_initialize_lq(priv, sta, lq_sta);
2926 static void rs_fill_link_cmd(struct iwl_priv *priv,
2927 struct iwl_lq_sta *lq_sta, u32 new_rate)
2929 struct iwl_scale_tbl_info tbl_type;
2932 int repeat_rate = 0;
2933 u8 ant_toggle_cnt = 0;
2934 u8 use_ht_possible = 1;
2935 u8 valid_tx_ant = 0;
2936 struct iwl_station_priv *sta_priv =
2937 container_of(lq_sta, struct iwl_station_priv, lq_sta);
2938 struct iwl_link_quality_cmd *lq_cmd = &lq_sta->lq;
2940 /* Override starting rate (index 0) if needed for debug purposes */
2941 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
2943 /* Interpret new_rate (rate_n_flags) */
2944 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band,
2945 &tbl_type, &rate_idx);
2947 if (priv && priv->bt_full_concurrent) {
2950 first_antenna(priv->nvm_data->valid_tx_ant);
2953 /* How many times should we repeat the initial rate? */
2954 if (is_legacy(tbl_type.lq_type)) {
2956 repeat_rate = IWL_NUMBER_TRY;
2958 repeat_rate = min(IWL_HT_NUMBER_TRY,
2959 LINK_QUAL_AGG_DISABLE_START_DEF - 1);
2962 lq_cmd->general_params.mimo_delimiter =
2963 is_mimo(tbl_type.lq_type) ? 1 : 0;
2965 /* Fill 1st table entry (index 0) */
2966 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
2968 if (num_of_ant(tbl_type.ant_type) == 1) {
2969 lq_cmd->general_params.single_stream_ant_msk =
2971 } else if (num_of_ant(tbl_type.ant_type) == 2) {
2972 lq_cmd->general_params.dual_stream_ant_msk =
2974 } /* otherwise we don't modify the existing value */
2979 if (priv->bt_full_concurrent)
2980 valid_tx_ant = ANT_A;
2982 valid_tx_ant = priv->nvm_data->valid_tx_ant;
2985 /* Fill rest of rate table */
2986 while (index < LINK_QUAL_MAX_RETRY_NUM) {
2987 /* Repeat initial/next rate.
2988 * For legacy IWL_NUMBER_TRY == 1, this loop will not execute.
2989 * For HT IWL_HT_NUMBER_TRY == 3, this executes twice. */
2990 while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
2991 if (is_legacy(tbl_type.lq_type)) {
2992 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2995 rs_toggle_antenna(valid_tx_ant,
2996 &new_rate, &tbl_type))
3000 /* Override next rate if needed for debug purposes */
3001 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
3003 /* Fill next table entry */
3004 lq_cmd->rs_table[index].rate_n_flags =
3005 cpu_to_le32(new_rate);
3010 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band, &tbl_type,
3013 if (priv && priv->bt_full_concurrent) {
3016 first_antenna(priv->nvm_data->valid_tx_ant);
3019 /* Indicate to uCode which entries might be MIMO.
3020 * If initial rate was MIMO, this will finally end up
3021 * as (IWL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
3022 if (is_mimo(tbl_type.lq_type))
3023 lq_cmd->general_params.mimo_delimiter = index;
3026 new_rate = rs_get_lower_rate(lq_sta, &tbl_type, rate_idx,
3029 /* How many times should we repeat the next rate? */
3030 if (is_legacy(tbl_type.lq_type)) {
3031 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
3034 rs_toggle_antenna(valid_tx_ant,
3035 &new_rate, &tbl_type))
3038 repeat_rate = IWL_NUMBER_TRY;
3040 repeat_rate = IWL_HT_NUMBER_TRY;
3043 /* Don't allow HT rates after next pass.
3044 * rs_get_lower_rate() will change type to LQ_A or LQ_G. */
3045 use_ht_possible = 0;
3047 /* Override next rate if needed for debug purposes */
3048 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
3050 /* Fill next table entry */
3051 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
3057 lq_cmd->agg_params.agg_frame_cnt_limit =
3058 sta_priv->max_agg_bufsize ?: LINK_QUAL_AGG_FRAME_LIMIT_DEF;
3059 lq_cmd->agg_params.agg_dis_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
3061 lq_cmd->agg_params.agg_time_limit =
3062 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
3064 * overwrite if needed, pass aggregation time limit
3067 if (priv && priv->cfg->bt_params &&
3068 priv->cfg->bt_params->agg_time_limit &&
3069 priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH)
3070 lq_cmd->agg_params.agg_time_limit =
3071 cpu_to_le16(priv->cfg->bt_params->agg_time_limit);
3074 static void *rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
3078 /* rate scale requires free function to be implemented */
3079 static void rs_free(void *priv_rate)
3084 static void rs_free_sta(void *priv_r, struct ieee80211_sta *sta,
3087 struct iwl_op_mode *op_mode __maybe_unused = priv_r;
3088 struct iwl_priv *priv __maybe_unused = IWL_OP_MODE_GET_DVM(op_mode);
3090 IWL_DEBUG_RATE(priv, "enter\n");
3091 IWL_DEBUG_RATE(priv, "leave\n");
3094 #ifdef CONFIG_MAC80211_DEBUGFS
3095 static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
3096 u32 *rate_n_flags, int index)
3098 struct iwl_priv *priv;
3103 valid_tx_ant = priv->nvm_data->valid_tx_ant;
3104 if (lq_sta->dbg_fixed_rate) {
3106 ((lq_sta->dbg_fixed_rate & RATE_MCS_ANT_ABC_MSK)
3107 >> RATE_MCS_ANT_POS);
3108 if ((valid_tx_ant & ant_sel_tx) == ant_sel_tx) {
3109 *rate_n_flags = lq_sta->dbg_fixed_rate;
3110 IWL_DEBUG_RATE(priv, "Fixed rate ON\n");
3112 lq_sta->dbg_fixed_rate = 0;
3114 "Invalid antenna selection 0x%X, Valid is 0x%X\n",
3115 ant_sel_tx, valid_tx_ant);
3116 IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
3119 IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
3123 static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
3124 const char __user *user_buf, size_t count, loff_t *ppos)
3126 struct iwl_lq_sta *lq_sta = file->private_data;
3127 struct iwl_priv *priv;
3134 memset(buf, 0, sizeof(buf));
3135 buf_size = min(count, sizeof(buf) - 1);
3136 if (copy_from_user(buf, user_buf, buf_size))
3139 if (sscanf(buf, "%x", &parsed_rate) == 1)
3140 lq_sta->dbg_fixed_rate = parsed_rate;
3142 lq_sta->dbg_fixed_rate = 0;
3144 rs_program_fix_rate(priv, lq_sta);
3149 static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
3150 char __user *user_buf, size_t count, loff_t *ppos)
3158 struct iwl_lq_sta *lq_sta = file->private_data;
3159 struct iwl_priv *priv;
3160 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
3163 buff = kmalloc(1024, GFP_KERNEL);
3167 desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
3168 desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
3169 lq_sta->total_failed, lq_sta->total_success,
3170 lq_sta->active_legacy_rate);
3171 desc += sprintf(buff+desc, "fixed rate 0x%X\n",
3172 lq_sta->dbg_fixed_rate);
3173 desc += sprintf(buff+desc, "valid_tx_ant %s%s%s\n",
3174 (priv->nvm_data->valid_tx_ant & ANT_A) ? "ANT_A," : "",
3175 (priv->nvm_data->valid_tx_ant & ANT_B) ? "ANT_B," : "",
3176 (priv->nvm_data->valid_tx_ant & ANT_C) ? "ANT_C" : "");
3177 desc += sprintf(buff+desc, "lq type %s\n",
3178 (is_legacy(tbl->lq_type)) ? "legacy" : "HT");
3179 if (is_Ht(tbl->lq_type)) {
3180 desc += sprintf(buff+desc, " %s",
3181 (is_siso(tbl->lq_type)) ? "SISO" :
3182 ((is_mimo2(tbl->lq_type)) ? "MIMO2" : "MIMO3"));
3183 desc += sprintf(buff+desc, " %s",
3184 (tbl->is_ht40) ? "40MHz" : "20MHz");
3185 desc += sprintf(buff+desc, " %s %s %s\n", (tbl->is_SGI) ? "SGI" : "",
3186 (lq_sta->is_green) ? "GF enabled" : "",
3187 (lq_sta->is_agg) ? "AGG on" : "");
3189 desc += sprintf(buff+desc, "last tx rate=0x%X\n",
3190 lq_sta->last_rate_n_flags);
3191 desc += sprintf(buff+desc, "general:"
3192 "flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
3193 lq_sta->lq.general_params.flags,
3194 lq_sta->lq.general_params.mimo_delimiter,
3195 lq_sta->lq.general_params.single_stream_ant_msk,
3196 lq_sta->lq.general_params.dual_stream_ant_msk);
3198 desc += sprintf(buff+desc, "agg:"
3199 "time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
3200 le16_to_cpu(lq_sta->lq.agg_params.agg_time_limit),
3201 lq_sta->lq.agg_params.agg_dis_start_th,
3202 lq_sta->lq.agg_params.agg_frame_cnt_limit);
3204 desc += sprintf(buff+desc,
3205 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
3206 lq_sta->lq.general_params.start_rate_index[0],
3207 lq_sta->lq.general_params.start_rate_index[1],
3208 lq_sta->lq.general_params.start_rate_index[2],
3209 lq_sta->lq.general_params.start_rate_index[3]);
3211 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
3212 index = iwl_hwrate_to_plcp_idx(
3213 le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags));
3214 if (is_legacy(tbl->lq_type)) {
3215 desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps\n",
3216 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags),
3217 iwl_rate_mcs[index].mbps);
3219 desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps (%s)\n",
3220 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags),
3221 iwl_rate_mcs[index].mbps, iwl_rate_mcs[index].mcs);
3225 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
3230 static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
3231 .write = rs_sta_dbgfs_scale_table_write,
3232 .read = rs_sta_dbgfs_scale_table_read,
3233 .open = simple_open,
3234 .llseek = default_llseek,
3236 static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
3237 char __user *user_buf, size_t count, loff_t *ppos)
3244 struct iwl_lq_sta *lq_sta = file->private_data;
3246 buff = kmalloc(1024, GFP_KERNEL);
3250 for (i = 0; i < LQ_SIZE; i++) {
3251 desc += sprintf(buff+desc,
3252 "%s type=%d SGI=%d HT40=%d DUP=%d GF=%d\n"
3254 lq_sta->active_tbl == i ? "*" : "x",
3255 lq_sta->lq_info[i].lq_type,
3256 lq_sta->lq_info[i].is_SGI,
3257 lq_sta->lq_info[i].is_ht40,
3258 lq_sta->lq_info[i].is_dup,
3260 lq_sta->lq_info[i].current_rate);
3261 for (j = 0; j < IWL_RATE_COUNT; j++) {
3262 desc += sprintf(buff+desc,
3263 "counter=%d success=%d %%=%d\n",
3264 lq_sta->lq_info[i].win[j].counter,
3265 lq_sta->lq_info[i].win[j].success_counter,
3266 lq_sta->lq_info[i].win[j].success_ratio);
3269 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
3274 static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
3275 .read = rs_sta_dbgfs_stats_table_read,
3276 .open = simple_open,
3277 .llseek = default_llseek,
3280 static ssize_t rs_sta_dbgfs_rate_scale_data_read(struct file *file,
3281 char __user *user_buf, size_t count, loff_t *ppos)
3283 struct iwl_lq_sta *lq_sta = file->private_data;
3284 struct iwl_scale_tbl_info *tbl = &lq_sta->lq_info[lq_sta->active_tbl];
3288 if (is_Ht(tbl->lq_type))
3289 desc += sprintf(buff+desc,
3290 "Bit Rate= %d Mb/s\n",
3291 tbl->expected_tpt[lq_sta->last_txrate_idx]);
3293 desc += sprintf(buff+desc,
3294 "Bit Rate= %d Mb/s\n",
3295 iwl_rates[lq_sta->last_txrate_idx].ieee >> 1);
3297 return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
3300 static const struct file_operations rs_sta_dbgfs_rate_scale_data_ops = {
3301 .read = rs_sta_dbgfs_rate_scale_data_read,
3302 .open = simple_open,
3303 .llseek = default_llseek,
3306 static void rs_add_debugfs(void *priv, void *priv_sta,
3309 struct iwl_lq_sta *lq_sta = priv_sta;
3310 lq_sta->rs_sta_dbgfs_scale_table_file =
3311 debugfs_create_file("rate_scale_table", S_IRUSR | S_IWUSR, dir,
3312 lq_sta, &rs_sta_dbgfs_scale_table_ops);
3313 lq_sta->rs_sta_dbgfs_stats_table_file =
3314 debugfs_create_file("rate_stats_table", S_IRUSR, dir,
3315 lq_sta, &rs_sta_dbgfs_stats_table_ops);
3316 lq_sta->rs_sta_dbgfs_rate_scale_data_file =
3317 debugfs_create_file("rate_scale_data", S_IRUSR, dir,
3318 lq_sta, &rs_sta_dbgfs_rate_scale_data_ops);
3319 lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file =
3320 debugfs_create_u8("tx_agg_tid_enable", S_IRUSR | S_IWUSR, dir,
3321 &lq_sta->tx_agg_tid_en);
3325 static void rs_remove_debugfs(void *priv, void *priv_sta)
3327 struct iwl_lq_sta *lq_sta = priv_sta;
3328 debugfs_remove(lq_sta->rs_sta_dbgfs_scale_table_file);
3329 debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
3330 debugfs_remove(lq_sta->rs_sta_dbgfs_rate_scale_data_file);
3331 debugfs_remove(lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file);
3336 * Initialization of rate scaling information is done by driver after
3337 * the station is added. Since mac80211 calls this function before a
3338 * station is added we ignore it.
3340 static void rs_rate_init_stub(void *priv_r, struct ieee80211_supported_band *sband,
3341 struct ieee80211_sta *sta, void *priv_sta)
3344 static struct rate_control_ops rs_ops = {
3347 .tx_status = rs_tx_status,
3348 .get_rate = rs_get_rate,
3349 .rate_init = rs_rate_init_stub,
3352 .alloc_sta = rs_alloc_sta,
3353 .free_sta = rs_free_sta,
3354 #ifdef CONFIG_MAC80211_DEBUGFS
3355 .add_sta_debugfs = rs_add_debugfs,
3356 .remove_sta_debugfs = rs_remove_debugfs,
3360 int iwlagn_rate_control_register(void)
3362 return ieee80211_rate_control_register(&rs_ops);
3365 void iwlagn_rate_control_unregister(void)
3367 ieee80211_rate_control_unregister(&rs_ops);