1 /******************************************************************************
3 * Copyright(c) 2005 - 2011 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 <linux/wireless.h>
31 #include <net/mac80211.h>
33 #include <linux/netdevice.h>
34 #include <linux/etherdevice.h>
35 #include <linux/delay.h>
37 #include <linux/workqueue.h>
44 #define RS_NAME "iwl-agn-rs"
46 #define NUM_TRY_BEFORE_ANT_TOGGLE 1
47 #define IWL_NUMBER_TRY 1
48 #define IWL_HT_NUMBER_TRY 3
50 #define IWL_RATE_MAX_WINDOW 62 /* # tx in history window */
51 #define IWL_RATE_MIN_FAILURE_TH 6 /* min failures to calc tpt */
52 #define IWL_RATE_MIN_SUCCESS_TH 8 /* min successes to calc tpt */
54 /* max allowed rate miss before sync LQ cmd */
55 #define IWL_MISSED_RATE_MAX 15
56 /* max time to accum history 2 seconds */
57 #define IWL_RATE_SCALE_FLUSH_INTVL (3*HZ)
59 static u8 rs_ht_to_legacy[] = {
60 IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
61 IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
63 IWL_RATE_6M_INDEX, IWL_RATE_9M_INDEX,
64 IWL_RATE_12M_INDEX, IWL_RATE_18M_INDEX,
65 IWL_RATE_24M_INDEX, IWL_RATE_36M_INDEX,
66 IWL_RATE_48M_INDEX, IWL_RATE_54M_INDEX
69 static const u8 ant_toggle_lookup[] = {
70 /*ANT_NONE -> */ ANT_NONE,
73 /*ANT_AB -> */ ANT_BC,
75 /*ANT_AC -> */ ANT_AB,
76 /*ANT_BC -> */ ANT_AC,
77 /*ANT_ABC -> */ ANT_ABC,
80 #define IWL_DECLARE_RATE_INFO(r, s, ip, in, rp, rn, pp, np) \
81 [IWL_RATE_##r##M_INDEX] = { IWL_RATE_##r##M_PLCP, \
82 IWL_RATE_SISO_##s##M_PLCP, \
83 IWL_RATE_MIMO2_##s##M_PLCP,\
84 IWL_RATE_MIMO3_##s##M_PLCP,\
85 IWL_RATE_##r##M_IEEE, \
86 IWL_RATE_##ip##M_INDEX, \
87 IWL_RATE_##in##M_INDEX, \
88 IWL_RATE_##rp##M_INDEX, \
89 IWL_RATE_##rn##M_INDEX, \
90 IWL_RATE_##pp##M_INDEX, \
91 IWL_RATE_##np##M_INDEX }
95 * rate, ht rate, prev rate, next rate, prev tgg rate, next tgg rate
97 * If there isn't a valid next or previous rate then INV is used which
98 * maps to IWL_RATE_INVALID
101 const struct iwl_rate_info iwl_rates[IWL_RATE_COUNT] = {
102 IWL_DECLARE_RATE_INFO(1, INV, INV, 2, INV, 2, INV, 2), /* 1mbps */
103 IWL_DECLARE_RATE_INFO(2, INV, 1, 5, 1, 5, 1, 5), /* 2mbps */
104 IWL_DECLARE_RATE_INFO(5, INV, 2, 6, 2, 11, 2, 11), /*5.5mbps */
105 IWL_DECLARE_RATE_INFO(11, INV, 9, 12, 9, 12, 5, 18), /* 11mbps */
106 IWL_DECLARE_RATE_INFO(6, 6, 5, 9, 5, 11, 5, 11), /* 6mbps */
107 IWL_DECLARE_RATE_INFO(9, 6, 6, 11, 6, 11, 5, 11), /* 9mbps */
108 IWL_DECLARE_RATE_INFO(12, 12, 11, 18, 11, 18, 11, 18), /* 12mbps */
109 IWL_DECLARE_RATE_INFO(18, 18, 12, 24, 12, 24, 11, 24), /* 18mbps */
110 IWL_DECLARE_RATE_INFO(24, 24, 18, 36, 18, 36, 18, 36), /* 24mbps */
111 IWL_DECLARE_RATE_INFO(36, 36, 24, 48, 24, 48, 24, 48), /* 36mbps */
112 IWL_DECLARE_RATE_INFO(48, 48, 36, 54, 36, 54, 36, 54), /* 48mbps */
113 IWL_DECLARE_RATE_INFO(54, 54, 48, INV, 48, INV, 48, INV),/* 54mbps */
114 IWL_DECLARE_RATE_INFO(60, 60, 48, INV, 48, INV, 48, INV),/* 60mbps */
115 /* FIXME:RS: ^^ should be INV (legacy) */
118 static inline u8 rs_extract_rate(u32 rate_n_flags)
120 return (u8)(rate_n_flags & RATE_MCS_RATE_MSK);
123 static int iwl_hwrate_to_plcp_idx(u32 rate_n_flags)
128 if (rate_n_flags & RATE_MCS_HT_MSK) {
129 idx = rs_extract_rate(rate_n_flags);
131 if (idx >= IWL_RATE_MIMO3_6M_PLCP)
132 idx = idx - IWL_RATE_MIMO3_6M_PLCP;
133 else if (idx >= IWL_RATE_MIMO2_6M_PLCP)
134 idx = idx - IWL_RATE_MIMO2_6M_PLCP;
136 idx += IWL_FIRST_OFDM_RATE;
137 /* skip 9M not supported in ht*/
138 if (idx >= IWL_RATE_9M_INDEX)
140 if ((idx >= IWL_FIRST_OFDM_RATE) && (idx <= IWL_LAST_OFDM_RATE))
143 /* legacy rate format, search for match in table */
145 for (idx = 0; idx < ARRAY_SIZE(iwl_rates); idx++)
146 if (iwl_rates[idx].plcp ==
147 rs_extract_rate(rate_n_flags))
154 static void rs_rate_scale_perform(struct iwl_priv *priv,
156 struct ieee80211_sta *sta,
157 struct iwl_lq_sta *lq_sta);
158 static void rs_fill_link_cmd(struct iwl_priv *priv,
159 struct iwl_lq_sta *lq_sta, u32 rate_n_flags);
160 static void rs_stay_in_table(struct iwl_lq_sta *lq_sta, bool force_search);
163 #ifdef CONFIG_MAC80211_DEBUGFS
164 static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
165 u32 *rate_n_flags, int index);
167 static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
168 u32 *rate_n_flags, int index)
173 * The following tables contain the expected throughput metrics for all rates
175 * 1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
177 * where invalid entries are zeros.
179 * CCK rates are only valid in legacy table and will only be used in G
183 static s32 expected_tpt_legacy[IWL_RATE_COUNT] = {
184 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 0
187 static s32 expected_tpt_siso20MHz[4][IWL_RATE_COUNT] = {
188 {0, 0, 0, 0, 42, 0, 76, 102, 124, 159, 183, 193, 202}, /* Norm */
189 {0, 0, 0, 0, 46, 0, 82, 110, 132, 168, 192, 202, 210}, /* SGI */
190 {0, 0, 0, 0, 47, 0, 91, 133, 171, 242, 305, 334, 362}, /* AGG */
191 {0, 0, 0, 0, 52, 0, 101, 145, 187, 264, 330, 361, 390}, /* AGG+SGI */
194 static s32 expected_tpt_siso40MHz[4][IWL_RATE_COUNT] = {
195 {0, 0, 0, 0, 77, 0, 127, 160, 184, 220, 242, 250, 257}, /* Norm */
196 {0, 0, 0, 0, 83, 0, 135, 169, 193, 229, 250, 257, 264}, /* SGI */
197 {0, 0, 0, 0, 94, 0, 177, 249, 313, 423, 512, 550, 586}, /* AGG */
198 {0, 0, 0, 0, 104, 0, 193, 270, 338, 454, 545, 584, 620}, /* AGG+SGI */
201 static s32 expected_tpt_mimo2_20MHz[4][IWL_RATE_COUNT] = {
202 {0, 0, 0, 0, 74, 0, 123, 155, 179, 214, 236, 244, 251}, /* Norm */
203 {0, 0, 0, 0, 81, 0, 131, 164, 188, 223, 243, 251, 257}, /* SGI */
204 {0, 0, 0, 0, 89, 0, 167, 235, 296, 402, 488, 526, 560}, /* AGG */
205 {0, 0, 0, 0, 97, 0, 182, 255, 320, 431, 520, 558, 593}, /* AGG+SGI*/
208 static s32 expected_tpt_mimo2_40MHz[4][IWL_RATE_COUNT] = {
209 {0, 0, 0, 0, 123, 0, 182, 214, 235, 264, 279, 285, 289}, /* Norm */
210 {0, 0, 0, 0, 131, 0, 191, 222, 242, 270, 284, 289, 293}, /* SGI */
211 {0, 0, 0, 0, 171, 0, 305, 410, 496, 634, 731, 771, 805}, /* AGG */
212 {0, 0, 0, 0, 186, 0, 329, 439, 527, 667, 764, 803, 838}, /* AGG+SGI */
215 static s32 expected_tpt_mimo3_20MHz[4][IWL_RATE_COUNT] = {
216 {0, 0, 0, 0, 99, 0, 153, 186, 208, 239, 256, 263, 268}, /* Norm */
217 {0, 0, 0, 0, 106, 0, 162, 194, 215, 246, 262, 268, 273}, /* SGI */
218 {0, 0, 0, 0, 134, 0, 249, 346, 431, 574, 685, 732, 775}, /* AGG */
219 {0, 0, 0, 0, 148, 0, 272, 376, 465, 614, 727, 775, 818}, /* AGG+SGI */
222 static s32 expected_tpt_mimo3_40MHz[4][IWL_RATE_COUNT] = {
223 {0, 0, 0, 0, 152, 0, 211, 239, 255, 279, 290, 294, 297}, /* Norm */
224 {0, 0, 0, 0, 160, 0, 219, 245, 261, 284, 294, 297, 300}, /* SGI */
225 {0, 0, 0, 0, 254, 0, 443, 584, 695, 868, 984, 1030, 1070}, /* AGG */
226 {0, 0, 0, 0, 277, 0, 478, 624, 737, 911, 1026, 1070, 1109}, /* AGG+SGI */
230 static const struct iwl_rate_mcs_info iwl_rate_mcs[IWL_RATE_COUNT] = {
239 { "24", "16QAM 1/2"},
240 { "36", "16QAM 3/4"},
241 { "48", "64QAM 2/3"},
242 { "54", "64QAM 3/4"},
243 { "60", "64QAM 5/6"},
246 #define MCS_INDEX_PER_STREAM (8)
248 static void rs_rate_scale_clear_window(struct iwl_rate_scale_data *window)
251 window->success_counter = 0;
252 window->success_ratio = IWL_INVALID_VALUE;
254 window->average_tpt = IWL_INVALID_VALUE;
258 static inline u8 rs_is_valid_ant(u8 valid_antenna, u8 ant_type)
260 return (ant_type & valid_antenna) == ant_type;
264 * removes the old data from the statistics. All data that is older than
265 * TID_MAX_TIME_DIFF, will be deleted.
267 static void rs_tl_rm_old_stats(struct iwl_traffic_load *tl, u32 curr_time)
269 /* The oldest age we want to keep */
270 u32 oldest_time = curr_time - TID_MAX_TIME_DIFF;
272 while (tl->queue_count &&
273 (tl->time_stamp < oldest_time)) {
274 tl->total -= tl->packet_count[tl->head];
275 tl->packet_count[tl->head] = 0;
276 tl->time_stamp += TID_QUEUE_CELL_SPACING;
279 if (tl->head >= TID_QUEUE_MAX_SIZE)
285 * increment traffic load value for tid and also remove
286 * any old values if passed the certain time period
288 static u8 rs_tl_add_packet(struct iwl_lq_sta *lq_data,
289 struct ieee80211_hdr *hdr)
291 u32 curr_time = jiffies_to_msecs(jiffies);
294 struct iwl_traffic_load *tl = NULL;
297 if (ieee80211_is_data_qos(hdr->frame_control)) {
298 u8 *qc = ieee80211_get_qos_ctl(hdr);
301 return MAX_TID_COUNT;
303 if (unlikely(tid >= TID_MAX_LOAD_COUNT))
304 return MAX_TID_COUNT;
306 tl = &lq_data->load[tid];
308 curr_time -= curr_time % TID_ROUND_VALUE;
310 /* Happens only for the first packet. Initialize the data */
311 if (!(tl->queue_count)) {
313 tl->time_stamp = curr_time;
316 tl->packet_count[0] = 1;
317 return MAX_TID_COUNT;
320 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
321 index = time_diff / TID_QUEUE_CELL_SPACING;
323 /* The history is too long: remove data that is older than */
324 /* TID_MAX_TIME_DIFF */
325 if (index >= TID_QUEUE_MAX_SIZE)
326 rs_tl_rm_old_stats(tl, curr_time);
328 index = (tl->head + index) % TID_QUEUE_MAX_SIZE;
329 tl->packet_count[index] = tl->packet_count[index] + 1;
330 tl->total = tl->total + 1;
332 if ((index + 1) > tl->queue_count)
333 tl->queue_count = index + 1;
339 get the traffic load value for tid
341 static u32 rs_tl_get_load(struct iwl_lq_sta *lq_data, u8 tid)
343 u32 curr_time = jiffies_to_msecs(jiffies);
346 struct iwl_traffic_load *tl = NULL;
348 if (tid >= TID_MAX_LOAD_COUNT)
351 tl = &(lq_data->load[tid]);
353 curr_time -= curr_time % TID_ROUND_VALUE;
355 if (!(tl->queue_count))
358 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
359 index = time_diff / TID_QUEUE_CELL_SPACING;
361 /* The history is too long: remove data that is older than */
362 /* TID_MAX_TIME_DIFF */
363 if (index >= TID_QUEUE_MAX_SIZE)
364 rs_tl_rm_old_stats(tl, curr_time);
369 static int rs_tl_turn_on_agg_for_tid(struct iwl_priv *priv,
370 struct iwl_lq_sta *lq_data, u8 tid,
371 struct ieee80211_sta *sta)
377 * Don't create TX aggregation sessions when in high
378 * BT traffic, as they would just be disrupted by BT.
380 if (priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH) {
381 IWL_ERR(priv, "BT traffic (%d), no aggregation allowed\n",
382 priv->bt_traffic_load);
386 load = rs_tl_get_load(lq_data, tid);
388 if (load > IWL_AGG_LOAD_THRESHOLD) {
389 IWL_DEBUG_HT(priv, "Starting Tx agg: STA: %pM tid: %d\n",
391 ret = ieee80211_start_tx_ba_session(sta, tid, 5000);
392 if (ret == -EAGAIN) {
394 * driver and mac80211 is out of sync
395 * this might be cause by reloading firmware
396 * stop the tx ba session here
398 IWL_ERR(priv, "Fail start Tx agg on tid: %d\n",
400 ieee80211_stop_tx_ba_session(sta, tid);
403 IWL_ERR(priv, "Aggregation not enabled for tid %d "
404 "because load = %u\n", tid, load);
409 static void rs_tl_turn_on_agg(struct iwl_priv *priv, u8 tid,
410 struct iwl_lq_sta *lq_data,
411 struct ieee80211_sta *sta)
413 if (tid < TID_MAX_LOAD_COUNT)
414 rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
416 IWL_ERR(priv, "tid exceeds max load count: %d/%d\n",
417 tid, TID_MAX_LOAD_COUNT);
420 static inline int get_num_of_ant_from_rate(u32 rate_n_flags)
422 return !!(rate_n_flags & RATE_MCS_ANT_A_MSK) +
423 !!(rate_n_flags & RATE_MCS_ANT_B_MSK) +
424 !!(rate_n_flags & RATE_MCS_ANT_C_MSK);
428 * Static function to get the expected throughput from an iwl_scale_tbl_info
429 * that wraps a NULL pointer check
431 static s32 get_expected_tpt(struct iwl_scale_tbl_info *tbl, int rs_index)
433 if (tbl->expected_tpt)
434 return tbl->expected_tpt[rs_index];
439 * rs_collect_tx_data - Update the success/failure sliding window
441 * We keep a sliding window of the last 62 packets transmitted
442 * at this rate. window->data contains the bitmask of successful
445 static int rs_collect_tx_data(struct iwl_scale_tbl_info *tbl,
446 int scale_index, int attempts, int successes)
448 struct iwl_rate_scale_data *window = NULL;
449 static const u64 mask = (((u64)1) << (IWL_RATE_MAX_WINDOW - 1));
452 if (scale_index < 0 || scale_index >= IWL_RATE_COUNT)
455 /* Select window for current tx bit rate */
456 window = &(tbl->win[scale_index]);
458 /* Get expected throughput */
459 tpt = get_expected_tpt(tbl, scale_index);
462 * Keep track of only the latest 62 tx frame attempts in this rate's
463 * history window; anything older isn't really relevant any more.
464 * If we have filled up the sliding window, drop the oldest attempt;
465 * if the oldest attempt (highest bit in bitmap) shows "success",
466 * subtract "1" from the success counter (this is the main reason
467 * we keep these bitmaps!).
469 while (attempts > 0) {
470 if (window->counter >= IWL_RATE_MAX_WINDOW) {
472 /* remove earliest */
473 window->counter = IWL_RATE_MAX_WINDOW - 1;
475 if (window->data & mask) {
476 window->data &= ~mask;
477 window->success_counter--;
481 /* Increment frames-attempted counter */
484 /* Shift bitmap by one frame to throw away oldest history */
487 /* Mark the most recent #successes attempts as successful */
489 window->success_counter++;
497 /* Calculate current success ratio, avoid divide-by-0! */
498 if (window->counter > 0)
499 window->success_ratio = 128 * (100 * window->success_counter)
502 window->success_ratio = IWL_INVALID_VALUE;
504 fail_count = window->counter - window->success_counter;
506 /* Calculate average throughput, if we have enough history. */
507 if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
508 (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
509 window->average_tpt = (window->success_ratio * tpt + 64) / 128;
511 window->average_tpt = IWL_INVALID_VALUE;
513 /* Tag this window as having been updated */
514 window->stamp = jiffies;
520 * Fill uCode API rate_n_flags field, based on "search" or "active" table.
522 /* FIXME:RS:remove this function and put the flags statically in the table */
523 static u32 rate_n_flags_from_tbl(struct iwl_priv *priv,
524 struct iwl_scale_tbl_info *tbl,
525 int index, u8 use_green)
527 u32 rate_n_flags = 0;
529 if (is_legacy(tbl->lq_type)) {
530 rate_n_flags = iwl_rates[index].plcp;
531 if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
532 rate_n_flags |= RATE_MCS_CCK_MSK;
534 } else if (is_Ht(tbl->lq_type)) {
535 if (index > IWL_LAST_OFDM_RATE) {
536 IWL_ERR(priv, "Invalid HT rate index %d\n", index);
537 index = IWL_LAST_OFDM_RATE;
539 rate_n_flags = RATE_MCS_HT_MSK;
541 if (is_siso(tbl->lq_type))
542 rate_n_flags |= iwl_rates[index].plcp_siso;
543 else if (is_mimo2(tbl->lq_type))
544 rate_n_flags |= iwl_rates[index].plcp_mimo2;
546 rate_n_flags |= iwl_rates[index].plcp_mimo3;
548 IWL_ERR(priv, "Invalid tbl->lq_type %d\n", tbl->lq_type);
551 rate_n_flags |= ((tbl->ant_type << RATE_MCS_ANT_POS) &
552 RATE_MCS_ANT_ABC_MSK);
554 if (is_Ht(tbl->lq_type)) {
557 rate_n_flags |= RATE_MCS_DUP_MSK;
559 rate_n_flags |= RATE_MCS_HT40_MSK;
562 rate_n_flags |= RATE_MCS_SGI_MSK;
565 rate_n_flags |= RATE_MCS_GF_MSK;
566 if (is_siso(tbl->lq_type) && tbl->is_SGI) {
567 rate_n_flags &= ~RATE_MCS_SGI_MSK;
568 IWL_ERR(priv, "GF was set with SGI:SISO\n");
576 * Interpret uCode API's rate_n_flags format,
577 * fill "search" or "active" tx mode table.
579 static int rs_get_tbl_info_from_mcs(const u32 rate_n_flags,
580 enum ieee80211_band band,
581 struct iwl_scale_tbl_info *tbl,
584 u32 ant_msk = (rate_n_flags & RATE_MCS_ANT_ABC_MSK);
585 u8 num_of_ant = get_num_of_ant_from_rate(rate_n_flags);
588 memset(tbl, 0, sizeof(struct iwl_scale_tbl_info));
589 *rate_idx = iwl_hwrate_to_plcp_idx(rate_n_flags);
591 if (*rate_idx == IWL_RATE_INVALID) {
595 tbl->is_SGI = 0; /* default legacy setup */
598 tbl->ant_type = (ant_msk >> RATE_MCS_ANT_POS);
599 tbl->lq_type = LQ_NONE;
600 tbl->max_search = IWL_MAX_SEARCH;
602 /* legacy rate format */
603 if (!(rate_n_flags & RATE_MCS_HT_MSK)) {
604 if (num_of_ant == 1) {
605 if (band == IEEE80211_BAND_5GHZ)
612 if (rate_n_flags & RATE_MCS_SGI_MSK)
615 if ((rate_n_flags & RATE_MCS_HT40_MSK) ||
616 (rate_n_flags & RATE_MCS_DUP_MSK))
619 if (rate_n_flags & RATE_MCS_DUP_MSK)
622 mcs = rs_extract_rate(rate_n_flags);
625 if (mcs <= IWL_RATE_SISO_60M_PLCP) {
627 tbl->lq_type = LQ_SISO; /*else NONE*/
629 } else if (mcs <= IWL_RATE_MIMO2_60M_PLCP) {
631 tbl->lq_type = LQ_MIMO2;
634 if (num_of_ant == 3) {
635 tbl->max_search = IWL_MAX_11N_MIMO3_SEARCH;
636 tbl->lq_type = LQ_MIMO3;
643 /* switch to another antenna/antennas and return 1 */
644 /* if no other valid antenna found, return 0 */
645 static int rs_toggle_antenna(u32 valid_ant, u32 *rate_n_flags,
646 struct iwl_scale_tbl_info *tbl)
650 if (!tbl->ant_type || tbl->ant_type > ANT_ABC)
653 if (!rs_is_valid_ant(valid_ant, tbl->ant_type))
656 new_ant_type = ant_toggle_lookup[tbl->ant_type];
658 while ((new_ant_type != tbl->ant_type) &&
659 !rs_is_valid_ant(valid_ant, new_ant_type))
660 new_ant_type = ant_toggle_lookup[new_ant_type];
662 if (new_ant_type == tbl->ant_type)
665 tbl->ant_type = new_ant_type;
666 *rate_n_flags &= ~RATE_MCS_ANT_ABC_MSK;
667 *rate_n_flags |= new_ant_type << RATE_MCS_ANT_POS;
672 * Green-field mode is valid if the station supports it and
673 * there are no non-GF stations present in the BSS.
675 static bool rs_use_green(struct ieee80211_sta *sta)
677 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
678 struct iwl_rxon_context *ctx = sta_priv->common.ctx;
680 return (sta->ht_cap.cap & IEEE80211_HT_CAP_GRN_FLD) &&
681 !(ctx->ht.non_gf_sta_present);
685 * rs_get_supported_rates - get the available rates
687 * if management frame or broadcast frame only return
688 * basic available rates.
691 static u16 rs_get_supported_rates(struct iwl_lq_sta *lq_sta,
692 struct ieee80211_hdr *hdr,
693 enum iwl_table_type rate_type)
695 if (is_legacy(rate_type)) {
696 return lq_sta->active_legacy_rate;
698 if (is_siso(rate_type))
699 return lq_sta->active_siso_rate;
700 else if (is_mimo2(rate_type))
701 return lq_sta->active_mimo2_rate;
703 return lq_sta->active_mimo3_rate;
707 static u16 rs_get_adjacent_rate(struct iwl_priv *priv, u8 index, u16 rate_mask,
710 u8 high = IWL_RATE_INVALID;
711 u8 low = IWL_RATE_INVALID;
713 /* 802.11A or ht walks to the next literal adjacent rate in
715 if (is_a_band(rate_type) || !is_legacy(rate_type)) {
719 /* Find the previous rate that is in the rate mask */
721 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
722 if (rate_mask & mask) {
728 /* Find the next rate that is in the rate mask */
730 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
731 if (rate_mask & mask) {
737 return (high << 8) | low;
741 while (low != IWL_RATE_INVALID) {
742 low = iwl_rates[low].prev_rs;
743 if (low == IWL_RATE_INVALID)
745 if (rate_mask & (1 << low))
747 IWL_DEBUG_RATE(priv, "Skipping masked lower rate: %d\n", low);
751 while (high != IWL_RATE_INVALID) {
752 high = iwl_rates[high].next_rs;
753 if (high == IWL_RATE_INVALID)
755 if (rate_mask & (1 << high))
757 IWL_DEBUG_RATE(priv, "Skipping masked higher rate: %d\n", high);
760 return (high << 8) | low;
763 static u32 rs_get_lower_rate(struct iwl_lq_sta *lq_sta,
764 struct iwl_scale_tbl_info *tbl,
765 u8 scale_index, u8 ht_possible)
770 u8 switch_to_legacy = 0;
771 u8 is_green = lq_sta->is_green;
772 struct iwl_priv *priv = lq_sta->drv;
774 /* check if we need to switch from HT to legacy rates.
775 * assumption is that mandatory rates (1Mbps or 6Mbps)
776 * are always supported (spec demand) */
777 if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_index)) {
778 switch_to_legacy = 1;
779 scale_index = rs_ht_to_legacy[scale_index];
780 if (lq_sta->band == IEEE80211_BAND_5GHZ)
785 if (num_of_ant(tbl->ant_type) > 1)
787 first_antenna(priv->hw_params.valid_tx_ant);
791 tbl->max_search = IWL_MAX_SEARCH;
794 rate_mask = rs_get_supported_rates(lq_sta, NULL, tbl->lq_type);
796 /* Mask with station rate restriction */
797 if (is_legacy(tbl->lq_type)) {
798 /* supp_rates has no CCK bits in A mode */
799 if (lq_sta->band == IEEE80211_BAND_5GHZ)
800 rate_mask = (u16)(rate_mask &
801 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
803 rate_mask = (u16)(rate_mask & lq_sta->supp_rates);
806 /* If we switched from HT to legacy, check current rate */
807 if (switch_to_legacy && (rate_mask & (1 << scale_index))) {
812 high_low = rs_get_adjacent_rate(lq_sta->drv, scale_index, rate_mask,
814 low = high_low & 0xff;
816 if (low == IWL_RATE_INVALID)
820 return rate_n_flags_from_tbl(lq_sta->drv, tbl, low, is_green);
824 * Simple function to compare two rate scale table types
826 static bool table_type_matches(struct iwl_scale_tbl_info *a,
827 struct iwl_scale_tbl_info *b)
829 return (a->lq_type == b->lq_type) && (a->ant_type == b->ant_type) &&
830 (a->is_SGI == b->is_SGI);
833 static void rs_bt_update_lq(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
834 struct iwl_lq_sta *lq_sta)
836 struct iwl_scale_tbl_info *tbl;
837 bool full_concurrent = priv->bt_full_concurrent;
840 if (priv->bt_ant_couple_ok) {
842 * Is there a need to switch between
843 * full concurrency and 3-wire?
845 spin_lock_irqsave(&priv->lock, flags);
846 if (priv->bt_ci_compliance && priv->bt_ant_couple_ok)
847 full_concurrent = true;
849 full_concurrent = false;
850 spin_unlock_irqrestore(&priv->lock, flags);
852 if ((priv->bt_traffic_load != priv->last_bt_traffic_load) ||
853 (priv->bt_full_concurrent != full_concurrent)) {
854 priv->bt_full_concurrent = full_concurrent;
856 /* Update uCode's rate table. */
857 tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
858 rs_fill_link_cmd(priv, lq_sta, tbl->current_rate);
859 iwl_send_lq_cmd(priv, ctx, &lq_sta->lq, CMD_ASYNC, false);
861 queue_work(priv->workqueue, &priv->bt_full_concurrency);
866 * mac80211 sends us Tx status
868 static void rs_tx_status(void *priv_r, struct ieee80211_supported_band *sband,
869 struct ieee80211_sta *sta, void *priv_sta,
874 int rs_index, mac_index, i;
875 struct iwl_lq_sta *lq_sta = priv_sta;
876 struct iwl_link_quality_cmd *table;
877 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
878 struct iwl_priv *priv = (struct iwl_priv *)priv_r;
879 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
880 enum mac80211_rate_control_flags mac_flags;
882 struct iwl_scale_tbl_info tbl_type;
883 struct iwl_scale_tbl_info *curr_tbl, *other_tbl, *tmp_tbl;
884 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
885 struct iwl_rxon_context *ctx = sta_priv->common.ctx;
887 IWL_DEBUG_RATE_LIMIT(priv, "get frame ack response, update rate scale window\n");
889 /* Treat uninitialized rate scaling data same as non-existing. */
891 IWL_DEBUG_RATE(priv, "Station rate scaling not created yet.\n");
893 } else if (!lq_sta->drv) {
894 IWL_DEBUG_RATE(priv, "Rate scaling not initialized yet.\n");
898 if (!ieee80211_is_data(hdr->frame_control) ||
899 info->flags & IEEE80211_TX_CTL_NO_ACK)
902 /* This packet was aggregated but doesn't carry status info */
903 if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
904 !(info->flags & IEEE80211_TX_STAT_AMPDU))
908 * Ignore this Tx frame response if its initial rate doesn't match
909 * that of latest Link Quality command. There may be stragglers
910 * from a previous Link Quality command, but we're no longer interested
911 * in those; they're either from the "active" mode while we're trying
912 * to check "search" mode, or a prior "search" mode after we've moved
913 * to a new "search" mode (which might become the new "active" mode).
916 tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
917 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
918 if (priv->band == IEEE80211_BAND_5GHZ)
919 rs_index -= IWL_FIRST_OFDM_RATE;
920 mac_flags = info->status.rates[0].flags;
921 mac_index = info->status.rates[0].idx;
922 /* For HT packets, map MCS to PLCP */
923 if (mac_flags & IEEE80211_TX_RC_MCS) {
924 mac_index &= RATE_MCS_CODE_MSK; /* Remove # of streams */
925 if (mac_index >= (IWL_RATE_9M_INDEX - IWL_FIRST_OFDM_RATE))
928 * mac80211 HT index is always zero-indexed; we need to move
929 * HT OFDM rates after CCK rates in 2.4 GHz band
931 if (priv->band == IEEE80211_BAND_2GHZ)
932 mac_index += IWL_FIRST_OFDM_RATE;
934 /* Here we actually compare this rate to the latest LQ command */
935 if ((mac_index < 0) ||
936 (tbl_type.is_SGI != !!(mac_flags & IEEE80211_TX_RC_SHORT_GI)) ||
937 (tbl_type.is_ht40 != !!(mac_flags & IEEE80211_TX_RC_40_MHZ_WIDTH)) ||
938 (tbl_type.is_dup != !!(mac_flags & IEEE80211_TX_RC_DUP_DATA)) ||
939 (tbl_type.ant_type != info->antenna_sel_tx) ||
940 (!!(tx_rate & RATE_MCS_HT_MSK) != !!(mac_flags & IEEE80211_TX_RC_MCS)) ||
941 (!!(tx_rate & RATE_MCS_GF_MSK) != !!(mac_flags & IEEE80211_TX_RC_GREEN_FIELD)) ||
942 (rs_index != mac_index)) {
943 IWL_DEBUG_RATE(priv, "initial rate %d does not match %d (0x%x)\n", mac_index, rs_index, tx_rate);
945 * Since rates mis-match, the last LQ command may have failed.
946 * After IWL_MISSED_RATE_MAX mis-matches, resync the uCode with
949 lq_sta->missed_rate_counter++;
950 if (lq_sta->missed_rate_counter > IWL_MISSED_RATE_MAX) {
951 lq_sta->missed_rate_counter = 0;
952 iwl_send_lq_cmd(priv, ctx, &lq_sta->lq, CMD_ASYNC, false);
954 /* Regardless, ignore this status info for outdated rate */
957 /* Rate did match, so reset the missed_rate_counter */
958 lq_sta->missed_rate_counter = 0;
960 /* Figure out if rate scale algorithm is in active or search table */
961 if (table_type_matches(&tbl_type,
962 &(lq_sta->lq_info[lq_sta->active_tbl]))) {
963 curr_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
964 other_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
965 } else if (table_type_matches(&tbl_type,
966 &lq_sta->lq_info[1 - lq_sta->active_tbl])) {
967 curr_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
968 other_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
970 IWL_DEBUG_RATE(priv, "Neither active nor search matches tx rate\n");
971 tmp_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
972 IWL_DEBUG_RATE(priv, "active- lq:%x, ant:%x, SGI:%d\n",
973 tmp_tbl->lq_type, tmp_tbl->ant_type, tmp_tbl->is_SGI);
974 tmp_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
975 IWL_DEBUG_RATE(priv, "search- lq:%x, ant:%x, SGI:%d\n",
976 tmp_tbl->lq_type, tmp_tbl->ant_type, tmp_tbl->is_SGI);
977 IWL_DEBUG_RATE(priv, "actual- lq:%x, ant:%x, SGI:%d\n",
978 tbl_type.lq_type, tbl_type.ant_type, tbl_type.is_SGI);
980 * no matching table found, let's by-pass the data collection
981 * and continue to perform rate scale to find the rate table
983 rs_stay_in_table(lq_sta, true);
988 * Updating the frame history depends on whether packets were
991 * For aggregation, all packets were transmitted at the same rate, the
992 * first index into rate scale table.
994 if (info->flags & IEEE80211_TX_STAT_AMPDU) {
995 tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
996 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type,
998 rs_collect_tx_data(curr_tbl, rs_index,
999 info->status.ampdu_len,
1000 info->status.ampdu_ack_len);
1002 /* Update success/fail counts if not searching for new mode */
1003 if (lq_sta->stay_in_tbl) {
1004 lq_sta->total_success += info->status.ampdu_ack_len;
1005 lq_sta->total_failed += (info->status.ampdu_len -
1006 info->status.ampdu_ack_len);
1010 * For legacy, update frame history with for each Tx retry.
1012 retries = info->status.rates[0].count - 1;
1013 /* HW doesn't send more than 15 retries */
1014 retries = min(retries, 15);
1016 /* The last transmission may have been successful */
1017 legacy_success = !!(info->flags & IEEE80211_TX_STAT_ACK);
1018 /* Collect data for each rate used during failed TX attempts */
1019 for (i = 0; i <= retries; ++i) {
1020 tx_rate = le32_to_cpu(table->rs_table[i].rate_n_flags);
1021 rs_get_tbl_info_from_mcs(tx_rate, priv->band,
1022 &tbl_type, &rs_index);
1024 * Only collect stats if retried rate is in the same RS
1025 * table as active/search.
1027 if (table_type_matches(&tbl_type, curr_tbl))
1029 else if (table_type_matches(&tbl_type, other_tbl))
1030 tmp_tbl = other_tbl;
1033 rs_collect_tx_data(tmp_tbl, rs_index, 1,
1034 i < retries ? 0 : legacy_success);
1037 /* Update success/fail counts if not searching for new mode */
1038 if (lq_sta->stay_in_tbl) {
1039 lq_sta->total_success += legacy_success;
1040 lq_sta->total_failed += retries + (1 - legacy_success);
1043 /* The last TX rate is cached in lq_sta; it's set in if/else above */
1044 lq_sta->last_rate_n_flags = tx_rate;
1046 /* See if there's a better rate or modulation mode to try. */
1047 if (sta && sta->supp_rates[sband->band])
1048 rs_rate_scale_perform(priv, skb, sta, lq_sta);
1050 if (priv->cfg->bt_params && priv->cfg->bt_params->advanced_bt_coexist)
1051 rs_bt_update_lq(priv, ctx, lq_sta);
1055 * Begin a period of staying with a selected modulation mode.
1056 * Set "stay_in_tbl" flag to prevent any mode switches.
1057 * Set frame tx success limits according to legacy vs. high-throughput,
1058 * and reset overall (spanning all rates) tx success history statistics.
1059 * These control how long we stay using same modulation mode before
1060 * searching for a new mode.
1062 static void rs_set_stay_in_table(struct iwl_priv *priv, u8 is_legacy,
1063 struct iwl_lq_sta *lq_sta)
1065 IWL_DEBUG_RATE(priv, "we are staying in the same table\n");
1066 lq_sta->stay_in_tbl = 1; /* only place this gets set */
1068 lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT;
1069 lq_sta->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
1070 lq_sta->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
1072 lq_sta->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
1073 lq_sta->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
1074 lq_sta->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
1076 lq_sta->table_count = 0;
1077 lq_sta->total_failed = 0;
1078 lq_sta->total_success = 0;
1079 lq_sta->flush_timer = jiffies;
1080 lq_sta->action_counter = 0;
1084 * Find correct throughput table for given mode of modulation
1086 static void rs_set_expected_tpt_table(struct iwl_lq_sta *lq_sta,
1087 struct iwl_scale_tbl_info *tbl)
1089 /* Used to choose among HT tables */
1090 s32 (*ht_tbl_pointer)[IWL_RATE_COUNT];
1092 /* Check for invalid LQ type */
1093 if (WARN_ON_ONCE(!is_legacy(tbl->lq_type) && !is_Ht(tbl->lq_type))) {
1094 tbl->expected_tpt = expected_tpt_legacy;
1098 /* Legacy rates have only one table */
1099 if (is_legacy(tbl->lq_type)) {
1100 tbl->expected_tpt = expected_tpt_legacy;
1104 /* Choose among many HT tables depending on number of streams
1105 * (SISO/MIMO2/MIMO3), channel width (20/40), SGI, and aggregation
1107 if (is_siso(tbl->lq_type) && (!tbl->is_ht40 || lq_sta->is_dup))
1108 ht_tbl_pointer = expected_tpt_siso20MHz;
1109 else if (is_siso(tbl->lq_type))
1110 ht_tbl_pointer = expected_tpt_siso40MHz;
1111 else if (is_mimo2(tbl->lq_type) && (!tbl->is_ht40 || lq_sta->is_dup))
1112 ht_tbl_pointer = expected_tpt_mimo2_20MHz;
1113 else if (is_mimo2(tbl->lq_type))
1114 ht_tbl_pointer = expected_tpt_mimo2_40MHz;
1115 else if (is_mimo3(tbl->lq_type) && (!tbl->is_ht40 || lq_sta->is_dup))
1116 ht_tbl_pointer = expected_tpt_mimo3_20MHz;
1117 else /* if (is_mimo3(tbl->lq_type)) <-- must be true */
1118 ht_tbl_pointer = expected_tpt_mimo3_40MHz;
1120 if (!tbl->is_SGI && !lq_sta->is_agg) /* Normal */
1121 tbl->expected_tpt = ht_tbl_pointer[0];
1122 else if (tbl->is_SGI && !lq_sta->is_agg) /* SGI */
1123 tbl->expected_tpt = ht_tbl_pointer[1];
1124 else if (!tbl->is_SGI && lq_sta->is_agg) /* AGG */
1125 tbl->expected_tpt = ht_tbl_pointer[2];
1127 tbl->expected_tpt = ht_tbl_pointer[3];
1131 * Find starting rate for new "search" high-throughput mode of modulation.
1132 * Goal is to find lowest expected rate (under perfect conditions) that is
1133 * above the current measured throughput of "active" mode, to give new mode
1134 * a fair chance to prove itself without too many challenges.
1136 * This gets called when transitioning to more aggressive modulation
1137 * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
1138 * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
1139 * to decrease to match "active" throughput. When moving from MIMO to SISO,
1140 * bit rate will typically need to increase, but not if performance was bad.
1142 static s32 rs_get_best_rate(struct iwl_priv *priv,
1143 struct iwl_lq_sta *lq_sta,
1144 struct iwl_scale_tbl_info *tbl, /* "search" */
1145 u16 rate_mask, s8 index)
1147 /* "active" values */
1148 struct iwl_scale_tbl_info *active_tbl =
1149 &(lq_sta->lq_info[lq_sta->active_tbl]);
1150 s32 active_sr = active_tbl->win[index].success_ratio;
1151 s32 active_tpt = active_tbl->expected_tpt[index];
1153 /* expected "search" throughput */
1154 s32 *tpt_tbl = tbl->expected_tpt;
1156 s32 new_rate, high, low, start_hi;
1160 new_rate = high = low = start_hi = IWL_RATE_INVALID;
1163 high_low = rs_get_adjacent_rate(priv, rate, rate_mask,
1166 low = high_low & 0xff;
1167 high = (high_low >> 8) & 0xff;
1170 * Lower the "search" bit rate, to give new "search" mode
1171 * approximately the same throughput as "active" if:
1173 * 1) "Active" mode has been working modestly well (but not
1174 * great), and expected "search" throughput (under perfect
1175 * conditions) at candidate rate is above the actual
1176 * measured "active" throughput (but less than expected
1177 * "active" throughput under perfect conditions).
1179 * 2) "Active" mode has been working perfectly or very well
1180 * and expected "search" throughput (under perfect
1181 * conditions) at candidate rate is above expected
1182 * "active" throughput (under perfect conditions).
1184 if ((((100 * tpt_tbl[rate]) > lq_sta->last_tpt) &&
1185 ((active_sr > IWL_RATE_DECREASE_TH) &&
1186 (active_sr <= IWL_RATE_HIGH_TH) &&
1187 (tpt_tbl[rate] <= active_tpt))) ||
1188 ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
1189 (tpt_tbl[rate] > active_tpt))) {
1191 /* (2nd or later pass)
1192 * If we've already tried to raise the rate, and are
1193 * now trying to lower it, use the higher rate. */
1194 if (start_hi != IWL_RATE_INVALID) {
1195 new_rate = start_hi;
1201 /* Loop again with lower rate */
1202 if (low != IWL_RATE_INVALID)
1205 /* Lower rate not available, use the original */
1209 /* Else try to raise the "search" rate to match "active" */
1211 /* (2nd or later pass)
1212 * If we've already tried to lower the rate, and are
1213 * now trying to raise it, use the lower rate. */
1214 if (new_rate != IWL_RATE_INVALID)
1217 /* Loop again with higher rate */
1218 else if (high != IWL_RATE_INVALID) {
1222 /* Higher rate not available, use the original */
1234 * Set up search table for MIMO2
1236 static int rs_switch_to_mimo2(struct iwl_priv *priv,
1237 struct iwl_lq_sta *lq_sta,
1238 struct ieee80211_conf *conf,
1239 struct ieee80211_sta *sta,
1240 struct iwl_scale_tbl_info *tbl, int index)
1244 s8 is_green = lq_sta->is_green;
1245 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
1246 struct iwl_rxon_context *ctx = sta_priv->common.ctx;
1248 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
1251 if (((sta->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 2)
1252 == WLAN_HT_CAP_SM_PS_STATIC)
1255 /* Need both Tx chains/antennas to support MIMO */
1256 if (priv->hw_params.tx_chains_num < 2)
1259 IWL_DEBUG_RATE(priv, "LQ: try to switch to MIMO2\n");
1261 tbl->lq_type = LQ_MIMO2;
1262 tbl->is_dup = lq_sta->is_dup;
1264 tbl->max_search = IWL_MAX_SEARCH;
1265 rate_mask = lq_sta->active_mimo2_rate;
1267 if (iwl_is_ht40_tx_allowed(priv, ctx, &sta->ht_cap))
1272 rs_set_expected_tpt_table(lq_sta, tbl);
1274 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
1276 IWL_DEBUG_RATE(priv, "LQ: MIMO2 best rate %d mask %X\n", rate, rate_mask);
1277 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1278 IWL_DEBUG_RATE(priv, "Can't switch with index %d rate mask %x\n",
1282 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
1284 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
1285 tbl->current_rate, is_green);
1290 * Set up search table for MIMO3
1292 static int rs_switch_to_mimo3(struct iwl_priv *priv,
1293 struct iwl_lq_sta *lq_sta,
1294 struct ieee80211_conf *conf,
1295 struct ieee80211_sta *sta,
1296 struct iwl_scale_tbl_info *tbl, int index)
1300 s8 is_green = lq_sta->is_green;
1301 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
1302 struct iwl_rxon_context *ctx = sta_priv->common.ctx;
1304 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
1307 if (((sta->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 2)
1308 == WLAN_HT_CAP_SM_PS_STATIC)
1311 /* Need both Tx chains/antennas to support MIMO */
1312 if (priv->hw_params.tx_chains_num < 3)
1315 IWL_DEBUG_RATE(priv, "LQ: try to switch to MIMO3\n");
1317 tbl->lq_type = LQ_MIMO3;
1318 tbl->is_dup = lq_sta->is_dup;
1320 tbl->max_search = IWL_MAX_11N_MIMO3_SEARCH;
1321 rate_mask = lq_sta->active_mimo3_rate;
1323 if (iwl_is_ht40_tx_allowed(priv, ctx, &sta->ht_cap))
1328 rs_set_expected_tpt_table(lq_sta, tbl);
1330 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
1332 IWL_DEBUG_RATE(priv, "LQ: MIMO3 best rate %d mask %X\n",
1334 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1335 IWL_DEBUG_RATE(priv, "Can't switch with index %d rate mask %x\n",
1339 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
1341 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
1342 tbl->current_rate, is_green);
1347 * Set up search table for SISO
1349 static int rs_switch_to_siso(struct iwl_priv *priv,
1350 struct iwl_lq_sta *lq_sta,
1351 struct ieee80211_conf *conf,
1352 struct ieee80211_sta *sta,
1353 struct iwl_scale_tbl_info *tbl, int index)
1356 u8 is_green = lq_sta->is_green;
1358 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
1359 struct iwl_rxon_context *ctx = sta_priv->common.ctx;
1361 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
1364 IWL_DEBUG_RATE(priv, "LQ: try to switch to SISO\n");
1366 tbl->is_dup = lq_sta->is_dup;
1367 tbl->lq_type = LQ_SISO;
1369 tbl->max_search = IWL_MAX_SEARCH;
1370 rate_mask = lq_sta->active_siso_rate;
1372 if (iwl_is_ht40_tx_allowed(priv, ctx, &sta->ht_cap))
1378 tbl->is_SGI = 0; /*11n spec: no SGI in SISO+Greenfield*/
1380 rs_set_expected_tpt_table(lq_sta, tbl);
1381 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
1383 IWL_DEBUG_RATE(priv, "LQ: get best rate %d mask %X\n", rate, rate_mask);
1384 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1385 IWL_DEBUG_RATE(priv, "can not switch with index %d rate mask %x\n",
1389 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
1390 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
1391 tbl->current_rate, is_green);
1396 * Try to switch to new modulation mode from legacy
1398 static int rs_move_legacy_other(struct iwl_priv *priv,
1399 struct iwl_lq_sta *lq_sta,
1400 struct ieee80211_conf *conf,
1401 struct ieee80211_sta *sta,
1404 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1405 struct iwl_scale_tbl_info *search_tbl =
1406 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1407 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1408 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1409 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1411 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1412 u8 tx_chains_num = priv->hw_params.tx_chains_num;
1414 u8 update_search_tbl_counter = 0;
1416 switch (priv->bt_traffic_load) {
1417 case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
1420 case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
1421 /* avoid antenna B unless MIMO */
1422 valid_tx_ant = first_antenna(priv->hw_params.valid_tx_ant);
1423 if (tbl->action == IWL_LEGACY_SWITCH_ANTENNA2)
1424 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
1426 case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
1427 case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
1428 /* avoid antenna B and MIMO */
1429 valid_tx_ant = first_antenna(priv->hw_params.valid_tx_ant);
1430 if (tbl->action >= IWL_LEGACY_SWITCH_ANTENNA2 &&
1431 tbl->action != IWL_LEGACY_SWITCH_SISO)
1432 tbl->action = IWL_LEGACY_SWITCH_SISO;
1435 IWL_ERR(priv, "Invalid BT load %d", priv->bt_traffic_load);
1439 if (!iwl_ht_enabled(priv))
1440 /* stay in Legacy */
1441 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
1442 else if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE &&
1443 tbl->action > IWL_LEGACY_SWITCH_SISO)
1444 tbl->action = IWL_LEGACY_SWITCH_SISO;
1446 /* configure as 1x1 if bt full concurrency */
1447 if (priv->bt_full_concurrent) {
1448 if (!iwl_ht_enabled(priv))
1449 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
1450 else if (tbl->action >= IWL_LEGACY_SWITCH_ANTENNA2)
1451 tbl->action = IWL_LEGACY_SWITCH_SISO;
1452 valid_tx_ant = first_antenna(priv->hw_params.valid_tx_ant);
1455 start_action = tbl->action;
1457 lq_sta->action_counter++;
1458 switch (tbl->action) {
1459 case IWL_LEGACY_SWITCH_ANTENNA1:
1460 case IWL_LEGACY_SWITCH_ANTENNA2:
1461 IWL_DEBUG_RATE(priv, "LQ: Legacy toggle Antenna\n");
1463 if ((tbl->action == IWL_LEGACY_SWITCH_ANTENNA1 &&
1464 tx_chains_num <= 1) ||
1465 (tbl->action == IWL_LEGACY_SWITCH_ANTENNA2 &&
1466 tx_chains_num <= 2))
1469 /* Don't change antenna if success has been great */
1470 if (window->success_ratio >= IWL_RS_GOOD_RATIO &&
1471 !priv->bt_full_concurrent &&
1472 priv->bt_traffic_load ==
1473 IWL_BT_COEX_TRAFFIC_LOAD_NONE)
1476 /* Set up search table to try other antenna */
1477 memcpy(search_tbl, tbl, sz);
1479 if (rs_toggle_antenna(valid_tx_ant,
1480 &search_tbl->current_rate, search_tbl)) {
1481 update_search_tbl_counter = 1;
1482 rs_set_expected_tpt_table(lq_sta, search_tbl);
1486 case IWL_LEGACY_SWITCH_SISO:
1487 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to SISO\n");
1489 /* Set up search table to try SISO */
1490 memcpy(search_tbl, tbl, sz);
1491 search_tbl->is_SGI = 0;
1492 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
1495 lq_sta->action_counter = 0;
1500 case IWL_LEGACY_SWITCH_MIMO2_AB:
1501 case IWL_LEGACY_SWITCH_MIMO2_AC:
1502 case IWL_LEGACY_SWITCH_MIMO2_BC:
1503 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to MIMO2\n");
1505 /* Set up search table to try MIMO */
1506 memcpy(search_tbl, tbl, sz);
1507 search_tbl->is_SGI = 0;
1509 if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AB)
1510 search_tbl->ant_type = ANT_AB;
1511 else if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AC)
1512 search_tbl->ant_type = ANT_AC;
1514 search_tbl->ant_type = ANT_BC;
1516 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1519 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
1522 lq_sta->action_counter = 0;
1527 case IWL_LEGACY_SWITCH_MIMO3_ABC:
1528 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to MIMO3\n");
1530 /* Set up search table to try MIMO3 */
1531 memcpy(search_tbl, tbl, sz);
1532 search_tbl->is_SGI = 0;
1534 search_tbl->ant_type = ANT_ABC;
1536 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1539 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1542 lq_sta->action_counter = 0;
1548 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
1549 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
1551 if (tbl->action == start_action)
1555 search_tbl->lq_type = LQ_NONE;
1559 lq_sta->search_better_tbl = 1;
1561 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
1562 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
1563 if (update_search_tbl_counter)
1564 search_tbl->action = tbl->action;
1570 * Try to switch to new modulation mode from SISO
1572 static int rs_move_siso_to_other(struct iwl_priv *priv,
1573 struct iwl_lq_sta *lq_sta,
1574 struct ieee80211_conf *conf,
1575 struct ieee80211_sta *sta, int index)
1577 u8 is_green = lq_sta->is_green;
1578 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1579 struct iwl_scale_tbl_info *search_tbl =
1580 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1581 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1582 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
1583 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1584 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1586 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1587 u8 tx_chains_num = priv->hw_params.tx_chains_num;
1588 u8 update_search_tbl_counter = 0;
1591 switch (priv->bt_traffic_load) {
1592 case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
1595 case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
1596 /* avoid antenna B unless MIMO */
1597 valid_tx_ant = first_antenna(priv->hw_params.valid_tx_ant);
1598 if (tbl->action == IWL_SISO_SWITCH_ANTENNA2)
1599 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1601 case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
1602 case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
1603 /* avoid antenna B and MIMO */
1604 valid_tx_ant = first_antenna(priv->hw_params.valid_tx_ant);
1605 if (tbl->action != IWL_SISO_SWITCH_ANTENNA1)
1606 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1609 IWL_ERR(priv, "Invalid BT load %d", priv->bt_traffic_load);
1613 if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE &&
1614 tbl->action > IWL_SISO_SWITCH_ANTENNA2) {
1616 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1619 /* configure as 1x1 if bt full concurrency */
1620 if (priv->bt_full_concurrent) {
1621 valid_tx_ant = first_antenna(priv->hw_params.valid_tx_ant);
1622 if (tbl->action >= IWL_LEGACY_SWITCH_ANTENNA2)
1623 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1626 start_action = tbl->action;
1628 lq_sta->action_counter++;
1629 switch (tbl->action) {
1630 case IWL_SISO_SWITCH_ANTENNA1:
1631 case IWL_SISO_SWITCH_ANTENNA2:
1632 IWL_DEBUG_RATE(priv, "LQ: SISO toggle Antenna\n");
1633 if ((tbl->action == IWL_SISO_SWITCH_ANTENNA1 &&
1634 tx_chains_num <= 1) ||
1635 (tbl->action == IWL_SISO_SWITCH_ANTENNA2 &&
1636 tx_chains_num <= 2))
1639 if (window->success_ratio >= IWL_RS_GOOD_RATIO &&
1640 !priv->bt_full_concurrent &&
1641 priv->bt_traffic_load ==
1642 IWL_BT_COEX_TRAFFIC_LOAD_NONE)
1645 memcpy(search_tbl, tbl, sz);
1646 if (rs_toggle_antenna(valid_tx_ant,
1647 &search_tbl->current_rate, search_tbl)) {
1648 update_search_tbl_counter = 1;
1652 case IWL_SISO_SWITCH_MIMO2_AB:
1653 case IWL_SISO_SWITCH_MIMO2_AC:
1654 case IWL_SISO_SWITCH_MIMO2_BC:
1655 IWL_DEBUG_RATE(priv, "LQ: SISO switch to MIMO2\n");
1656 memcpy(search_tbl, tbl, sz);
1657 search_tbl->is_SGI = 0;
1659 if (tbl->action == IWL_SISO_SWITCH_MIMO2_AB)
1660 search_tbl->ant_type = ANT_AB;
1661 else if (tbl->action == IWL_SISO_SWITCH_MIMO2_AC)
1662 search_tbl->ant_type = ANT_AC;
1664 search_tbl->ant_type = ANT_BC;
1666 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1669 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
1674 case IWL_SISO_SWITCH_GI:
1675 if (!tbl->is_ht40 && !(ht_cap->cap &
1676 IEEE80211_HT_CAP_SGI_20))
1678 if (tbl->is_ht40 && !(ht_cap->cap &
1679 IEEE80211_HT_CAP_SGI_40))
1682 IWL_DEBUG_RATE(priv, "LQ: SISO toggle SGI/NGI\n");
1684 memcpy(search_tbl, tbl, sz);
1690 "SGI was set in GF+SISO\n");
1692 search_tbl->is_SGI = !tbl->is_SGI;
1693 rs_set_expected_tpt_table(lq_sta, search_tbl);
1695 s32 tpt = lq_sta->last_tpt / 100;
1696 if (tpt >= search_tbl->expected_tpt[index])
1699 search_tbl->current_rate =
1700 rate_n_flags_from_tbl(priv, search_tbl,
1702 update_search_tbl_counter = 1;
1704 case IWL_SISO_SWITCH_MIMO3_ABC:
1705 IWL_DEBUG_RATE(priv, "LQ: SISO switch to MIMO3\n");
1706 memcpy(search_tbl, tbl, sz);
1707 search_tbl->is_SGI = 0;
1708 search_tbl->ant_type = ANT_ABC;
1710 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1713 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1720 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
1721 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1723 if (tbl->action == start_action)
1726 search_tbl->lq_type = LQ_NONE;
1730 lq_sta->search_better_tbl = 1;
1732 if (tbl->action > IWL_SISO_SWITCH_MIMO3_ABC)
1733 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1734 if (update_search_tbl_counter)
1735 search_tbl->action = tbl->action;
1741 * Try to switch to new modulation mode from MIMO2
1743 static int rs_move_mimo2_to_other(struct iwl_priv *priv,
1744 struct iwl_lq_sta *lq_sta,
1745 struct ieee80211_conf *conf,
1746 struct ieee80211_sta *sta, int index)
1748 s8 is_green = lq_sta->is_green;
1749 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1750 struct iwl_scale_tbl_info *search_tbl =
1751 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1752 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1753 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
1754 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1755 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1757 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1758 u8 tx_chains_num = priv->hw_params.tx_chains_num;
1759 u8 update_search_tbl_counter = 0;
1762 switch (priv->bt_traffic_load) {
1763 case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
1766 case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
1767 case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
1768 /* avoid antenna B and MIMO */
1769 if (tbl->action != IWL_MIMO2_SWITCH_SISO_A)
1770 tbl->action = IWL_MIMO2_SWITCH_SISO_A;
1772 case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
1773 /* avoid antenna B unless MIMO */
1774 if (tbl->action == IWL_MIMO2_SWITCH_SISO_B ||
1775 tbl->action == IWL_MIMO2_SWITCH_SISO_C)
1776 tbl->action = IWL_MIMO2_SWITCH_SISO_A;
1779 IWL_ERR(priv, "Invalid BT load %d", priv->bt_traffic_load);
1783 if ((iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE) &&
1784 (tbl->action < IWL_MIMO2_SWITCH_SISO_A ||
1785 tbl->action > IWL_MIMO2_SWITCH_SISO_C)) {
1786 /* switch in SISO */
1787 tbl->action = IWL_MIMO2_SWITCH_SISO_A;
1790 /* configure as 1x1 if bt full concurrency */
1791 if (priv->bt_full_concurrent &&
1792 (tbl->action < IWL_MIMO2_SWITCH_SISO_A ||
1793 tbl->action > IWL_MIMO2_SWITCH_SISO_C))
1794 tbl->action = IWL_MIMO2_SWITCH_SISO_A;
1796 start_action = tbl->action;
1798 lq_sta->action_counter++;
1799 switch (tbl->action) {
1800 case IWL_MIMO2_SWITCH_ANTENNA1:
1801 case IWL_MIMO2_SWITCH_ANTENNA2:
1802 IWL_DEBUG_RATE(priv, "LQ: MIMO2 toggle Antennas\n");
1804 if (tx_chains_num <= 2)
1807 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1810 memcpy(search_tbl, tbl, sz);
1811 if (rs_toggle_antenna(valid_tx_ant,
1812 &search_tbl->current_rate, search_tbl)) {
1813 update_search_tbl_counter = 1;
1817 case IWL_MIMO2_SWITCH_SISO_A:
1818 case IWL_MIMO2_SWITCH_SISO_B:
1819 case IWL_MIMO2_SWITCH_SISO_C:
1820 IWL_DEBUG_RATE(priv, "LQ: MIMO2 switch to SISO\n");
1822 /* Set up new search table for SISO */
1823 memcpy(search_tbl, tbl, sz);
1825 if (tbl->action == IWL_MIMO2_SWITCH_SISO_A)
1826 search_tbl->ant_type = ANT_A;
1827 else if (tbl->action == IWL_MIMO2_SWITCH_SISO_B)
1828 search_tbl->ant_type = ANT_B;
1830 search_tbl->ant_type = ANT_C;
1832 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1835 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
1842 case IWL_MIMO2_SWITCH_GI:
1843 if (!tbl->is_ht40 && !(ht_cap->cap &
1844 IEEE80211_HT_CAP_SGI_20))
1846 if (tbl->is_ht40 && !(ht_cap->cap &
1847 IEEE80211_HT_CAP_SGI_40))
1850 IWL_DEBUG_RATE(priv, "LQ: MIMO2 toggle SGI/NGI\n");
1852 /* Set up new search table for MIMO2 */
1853 memcpy(search_tbl, tbl, sz);
1854 search_tbl->is_SGI = !tbl->is_SGI;
1855 rs_set_expected_tpt_table(lq_sta, search_tbl);
1857 * If active table already uses the fastest possible
1858 * modulation (dual stream with short guard interval),
1859 * and it's working well, there's no need to look
1860 * for a better type of modulation!
1863 s32 tpt = lq_sta->last_tpt / 100;
1864 if (tpt >= search_tbl->expected_tpt[index])
1867 search_tbl->current_rate =
1868 rate_n_flags_from_tbl(priv, search_tbl,
1870 update_search_tbl_counter = 1;
1873 case IWL_MIMO2_SWITCH_MIMO3_ABC:
1874 IWL_DEBUG_RATE(priv, "LQ: MIMO2 switch to MIMO3\n");
1875 memcpy(search_tbl, tbl, sz);
1876 search_tbl->is_SGI = 0;
1877 search_tbl->ant_type = ANT_ABC;
1879 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1882 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1890 if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
1891 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
1893 if (tbl->action == start_action)
1896 search_tbl->lq_type = LQ_NONE;
1899 lq_sta->search_better_tbl = 1;
1901 if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
1902 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
1903 if (update_search_tbl_counter)
1904 search_tbl->action = tbl->action;
1911 * Try to switch to new modulation mode from MIMO3
1913 static int rs_move_mimo3_to_other(struct iwl_priv *priv,
1914 struct iwl_lq_sta *lq_sta,
1915 struct ieee80211_conf *conf,
1916 struct ieee80211_sta *sta, int index)
1918 s8 is_green = lq_sta->is_green;
1919 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1920 struct iwl_scale_tbl_info *search_tbl =
1921 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1922 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1923 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
1924 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1925 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1927 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1928 u8 tx_chains_num = priv->hw_params.tx_chains_num;
1930 u8 update_search_tbl_counter = 0;
1932 switch (priv->bt_traffic_load) {
1933 case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
1936 case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
1937 case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
1938 /* avoid antenna B and MIMO */
1939 if (tbl->action != IWL_MIMO3_SWITCH_SISO_A)
1940 tbl->action = IWL_MIMO3_SWITCH_SISO_A;
1942 case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
1943 /* avoid antenna B unless MIMO */
1944 if (tbl->action == IWL_MIMO3_SWITCH_SISO_B ||
1945 tbl->action == IWL_MIMO3_SWITCH_SISO_C)
1946 tbl->action = IWL_MIMO3_SWITCH_SISO_A;
1949 IWL_ERR(priv, "Invalid BT load %d", priv->bt_traffic_load);
1953 if ((iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE) &&
1954 (tbl->action < IWL_MIMO3_SWITCH_SISO_A ||
1955 tbl->action > IWL_MIMO3_SWITCH_SISO_C)) {
1956 /* switch in SISO */
1957 tbl->action = IWL_MIMO3_SWITCH_SISO_A;
1960 /* configure as 1x1 if bt full concurrency */
1961 if (priv->bt_full_concurrent &&
1962 (tbl->action < IWL_MIMO3_SWITCH_SISO_A ||
1963 tbl->action > IWL_MIMO3_SWITCH_SISO_C))
1964 tbl->action = IWL_MIMO3_SWITCH_SISO_A;
1966 start_action = tbl->action;
1968 lq_sta->action_counter++;
1969 switch (tbl->action) {
1970 case IWL_MIMO3_SWITCH_ANTENNA1:
1971 case IWL_MIMO3_SWITCH_ANTENNA2:
1972 IWL_DEBUG_RATE(priv, "LQ: MIMO3 toggle Antennas\n");
1974 if (tx_chains_num <= 3)
1977 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1980 memcpy(search_tbl, tbl, sz);
1981 if (rs_toggle_antenna(valid_tx_ant,
1982 &search_tbl->current_rate, search_tbl))
1985 case IWL_MIMO3_SWITCH_SISO_A:
1986 case IWL_MIMO3_SWITCH_SISO_B:
1987 case IWL_MIMO3_SWITCH_SISO_C:
1988 IWL_DEBUG_RATE(priv, "LQ: MIMO3 switch to SISO\n");
1990 /* Set up new search table for SISO */
1991 memcpy(search_tbl, tbl, sz);
1993 if (tbl->action == IWL_MIMO3_SWITCH_SISO_A)
1994 search_tbl->ant_type = ANT_A;
1995 else if (tbl->action == IWL_MIMO3_SWITCH_SISO_B)
1996 search_tbl->ant_type = ANT_B;
1998 search_tbl->ant_type = ANT_C;
2000 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
2003 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
2010 case IWL_MIMO3_SWITCH_MIMO2_AB:
2011 case IWL_MIMO3_SWITCH_MIMO2_AC:
2012 case IWL_MIMO3_SWITCH_MIMO2_BC:
2013 IWL_DEBUG_RATE(priv, "LQ: MIMO3 switch to MIMO2\n");
2015 memcpy(search_tbl, tbl, sz);
2016 search_tbl->is_SGI = 0;
2017 if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AB)
2018 search_tbl->ant_type = ANT_AB;
2019 else if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AC)
2020 search_tbl->ant_type = ANT_AC;
2022 search_tbl->ant_type = ANT_BC;
2024 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
2027 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
2034 case IWL_MIMO3_SWITCH_GI:
2035 if (!tbl->is_ht40 && !(ht_cap->cap &
2036 IEEE80211_HT_CAP_SGI_20))
2038 if (tbl->is_ht40 && !(ht_cap->cap &
2039 IEEE80211_HT_CAP_SGI_40))
2042 IWL_DEBUG_RATE(priv, "LQ: MIMO3 toggle SGI/NGI\n");
2044 /* Set up new search table for MIMO */
2045 memcpy(search_tbl, tbl, sz);
2046 search_tbl->is_SGI = !tbl->is_SGI;
2047 rs_set_expected_tpt_table(lq_sta, search_tbl);
2049 * If active table already uses the fastest possible
2050 * modulation (dual stream with short guard interval),
2051 * and it's working well, there's no need to look
2052 * for a better type of modulation!
2055 s32 tpt = lq_sta->last_tpt / 100;
2056 if (tpt >= search_tbl->expected_tpt[index])
2059 search_tbl->current_rate =
2060 rate_n_flags_from_tbl(priv, search_tbl,
2062 update_search_tbl_counter = 1;
2066 if (tbl->action > IWL_MIMO3_SWITCH_GI)
2067 tbl->action = IWL_MIMO3_SWITCH_ANTENNA1;
2069 if (tbl->action == start_action)
2072 search_tbl->lq_type = LQ_NONE;
2075 lq_sta->search_better_tbl = 1;
2077 if (tbl->action > IWL_MIMO3_SWITCH_GI)
2078 tbl->action = IWL_MIMO3_SWITCH_ANTENNA1;
2079 if (update_search_tbl_counter)
2080 search_tbl->action = tbl->action;
2087 * Check whether we should continue using same modulation mode, or
2088 * begin search for a new mode, based on:
2089 * 1) # tx successes or failures while using this mode
2090 * 2) # times calling this function
2091 * 3) elapsed time in this mode (not used, for now)
2093 static void rs_stay_in_table(struct iwl_lq_sta *lq_sta, bool force_search)
2095 struct iwl_scale_tbl_info *tbl;
2098 int flush_interval_passed = 0;
2099 struct iwl_priv *priv;
2102 active_tbl = lq_sta->active_tbl;
2104 tbl = &(lq_sta->lq_info[active_tbl]);
2106 /* If we've been disallowing search, see if we should now allow it */
2107 if (lq_sta->stay_in_tbl) {
2109 /* Elapsed time using current modulation mode */
2110 if (lq_sta->flush_timer)
2111 flush_interval_passed =
2113 (unsigned long)(lq_sta->flush_timer +
2114 IWL_RATE_SCALE_FLUSH_INTVL));
2117 * Check if we should allow search for new modulation mode.
2118 * If many frames have failed or succeeded, or we've used
2119 * this same modulation for a long time, allow search, and
2120 * reset history stats that keep track of whether we should
2121 * allow a new search. Also (below) reset all bitmaps and
2122 * stats in active history.
2125 (lq_sta->total_failed > lq_sta->max_failure_limit) ||
2126 (lq_sta->total_success > lq_sta->max_success_limit) ||
2127 ((!lq_sta->search_better_tbl) && (lq_sta->flush_timer)
2128 && (flush_interval_passed))) {
2129 IWL_DEBUG_RATE(priv, "LQ: stay is expired %d %d %d\n:",
2130 lq_sta->total_failed,
2131 lq_sta->total_success,
2132 flush_interval_passed);
2134 /* Allow search for new mode */
2135 lq_sta->stay_in_tbl = 0; /* only place reset */
2136 lq_sta->total_failed = 0;
2137 lq_sta->total_success = 0;
2138 lq_sta->flush_timer = 0;
2141 * Else if we've used this modulation mode enough repetitions
2142 * (regardless of elapsed time or success/failure), reset
2143 * history bitmaps and rate-specific stats for all rates in
2147 lq_sta->table_count++;
2148 if (lq_sta->table_count >=
2149 lq_sta->table_count_limit) {
2150 lq_sta->table_count = 0;
2152 IWL_DEBUG_RATE(priv, "LQ: stay in table clear win\n");
2153 for (i = 0; i < IWL_RATE_COUNT; i++)
2154 rs_rate_scale_clear_window(
2159 /* If transitioning to allow "search", reset all history
2160 * bitmaps and stats in active table (this will become the new
2161 * "search" table). */
2162 if (!lq_sta->stay_in_tbl) {
2163 for (i = 0; i < IWL_RATE_COUNT; i++)
2164 rs_rate_scale_clear_window(&(tbl->win[i]));
2170 * setup rate table in uCode
2171 * return rate_n_flags as used in the table
2173 static u32 rs_update_rate_tbl(struct iwl_priv *priv,
2174 struct iwl_rxon_context *ctx,
2175 struct iwl_lq_sta *lq_sta,
2176 struct iwl_scale_tbl_info *tbl,
2177 int index, u8 is_green)
2181 /* Update uCode's rate table. */
2182 rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
2183 rs_fill_link_cmd(priv, lq_sta, rate);
2184 iwl_send_lq_cmd(priv, ctx, &lq_sta->lq, CMD_ASYNC, false);
2190 * Do rate scaling and search for new modulation mode.
2192 static void rs_rate_scale_perform(struct iwl_priv *priv,
2193 struct sk_buff *skb,
2194 struct ieee80211_sta *sta,
2195 struct iwl_lq_sta *lq_sta)
2197 struct ieee80211_hw *hw = priv->hw;
2198 struct ieee80211_conf *conf = &hw->conf;
2199 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2200 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2201 int low = IWL_RATE_INVALID;
2202 int high = IWL_RATE_INVALID;
2205 struct iwl_rate_scale_data *window = NULL;
2206 int current_tpt = IWL_INVALID_VALUE;
2207 int low_tpt = IWL_INVALID_VALUE;
2208 int high_tpt = IWL_INVALID_VALUE;
2210 s8 scale_action = 0;
2213 struct iwl_scale_tbl_info *tbl, *tbl1;
2214 u16 rate_scale_index_msk = 0;
2221 u8 tid = MAX_TID_COUNT;
2222 struct iwl_tid_data *tid_data;
2223 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
2224 struct iwl_rxon_context *ctx = sta_priv->common.ctx;
2226 IWL_DEBUG_RATE(priv, "rate scale calculate new rate for skb\n");
2228 /* Send management frames and NO_ACK data using lowest rate. */
2229 /* TODO: this could probably be improved.. */
2230 if (!ieee80211_is_data(hdr->frame_control) ||
2231 info->flags & IEEE80211_TX_CTL_NO_ACK)
2234 if (!sta || !lq_sta)
2237 lq_sta->supp_rates = sta->supp_rates[lq_sta->band];
2239 tid = rs_tl_add_packet(lq_sta, hdr);
2240 if ((tid != MAX_TID_COUNT) && (lq_sta->tx_agg_tid_en & (1 << tid))) {
2241 tid_data = &priv->stations[lq_sta->lq.sta_id].tid[tid];
2242 if (tid_data->agg.state == IWL_AGG_OFF)
2250 * Select rate-scale / modulation-mode table to work with in
2251 * the rest of this function: "search" if searching for better
2252 * modulation mode, or "active" if doing rate scaling within a mode.
2254 if (!lq_sta->search_better_tbl)
2255 active_tbl = lq_sta->active_tbl;
2257 active_tbl = 1 - lq_sta->active_tbl;
2259 tbl = &(lq_sta->lq_info[active_tbl]);
2260 if (is_legacy(tbl->lq_type))
2261 lq_sta->is_green = 0;
2263 lq_sta->is_green = rs_use_green(sta);
2264 is_green = lq_sta->is_green;
2266 /* current tx rate */
2267 index = lq_sta->last_txrate_idx;
2269 IWL_DEBUG_RATE(priv, "Rate scale index %d for type %d\n", index,
2272 /* rates available for this association, and for modulation mode */
2273 rate_mask = rs_get_supported_rates(lq_sta, hdr, tbl->lq_type);
2275 IWL_DEBUG_RATE(priv, "mask 0x%04X\n", rate_mask);
2277 /* mask with station rate restriction */
2278 if (is_legacy(tbl->lq_type)) {
2279 if (lq_sta->band == IEEE80211_BAND_5GHZ)
2280 /* supp_rates has no CCK bits in A mode */
2281 rate_scale_index_msk = (u16) (rate_mask &
2282 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
2284 rate_scale_index_msk = (u16) (rate_mask &
2285 lq_sta->supp_rates);
2288 rate_scale_index_msk = rate_mask;
2290 if (!rate_scale_index_msk)
2291 rate_scale_index_msk = rate_mask;
2293 if (!((1 << index) & rate_scale_index_msk)) {
2294 IWL_ERR(priv, "Current Rate is not valid\n");
2295 if (lq_sta->search_better_tbl) {
2296 /* revert to active table if search table is not valid*/
2297 tbl->lq_type = LQ_NONE;
2298 lq_sta->search_better_tbl = 0;
2299 tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
2300 /* get "active" rate info */
2301 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
2302 rate = rs_update_rate_tbl(priv, ctx, lq_sta,
2303 tbl, index, is_green);
2308 /* Get expected throughput table and history window for current rate */
2309 if (!tbl->expected_tpt) {
2310 IWL_ERR(priv, "tbl->expected_tpt is NULL\n");
2314 /* force user max rate if set by user */
2315 if ((lq_sta->max_rate_idx != -1) &&
2316 (lq_sta->max_rate_idx < index)) {
2317 index = lq_sta->max_rate_idx;
2319 window = &(tbl->win[index]);
2323 window = &(tbl->win[index]);
2326 * If there is not enough history to calculate actual average
2327 * throughput, keep analyzing results of more tx frames, without
2328 * changing rate or mode (bypass most of the rest of this function).
2329 * Set up new rate table in uCode only if old rate is not supported
2330 * in current association (use new rate found above).
2332 fail_count = window->counter - window->success_counter;
2333 if ((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
2334 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH)) {
2335 IWL_DEBUG_RATE(priv, "LQ: still below TH. succ=%d total=%d "
2337 window->success_counter, window->counter, index);
2339 /* Can't calculate this yet; not enough history */
2340 window->average_tpt = IWL_INVALID_VALUE;
2342 /* Should we stay with this modulation mode,
2343 * or search for a new one? */
2344 rs_stay_in_table(lq_sta, false);
2348 /* Else we have enough samples; calculate estimate of
2349 * actual average throughput */
2350 if (window->average_tpt != ((window->success_ratio *
2351 tbl->expected_tpt[index] + 64) / 128)) {
2352 IWL_ERR(priv, "expected_tpt should have been calculated by now\n");
2353 window->average_tpt = ((window->success_ratio *
2354 tbl->expected_tpt[index] + 64) / 128);
2357 /* If we are searching for better modulation mode, check success. */
2358 if (lq_sta->search_better_tbl &&
2359 (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_MULTI)) {
2360 /* If good success, continue using the "search" mode;
2361 * no need to send new link quality command, since we're
2362 * continuing to use the setup that we've been trying. */
2363 if (window->average_tpt > lq_sta->last_tpt) {
2365 IWL_DEBUG_RATE(priv, "LQ: SWITCHING TO NEW TABLE "
2366 "suc=%d cur-tpt=%d old-tpt=%d\n",
2367 window->success_ratio,
2368 window->average_tpt,
2371 if (!is_legacy(tbl->lq_type))
2372 lq_sta->enable_counter = 1;
2374 /* Swap tables; "search" becomes "active" */
2375 lq_sta->active_tbl = active_tbl;
2376 current_tpt = window->average_tpt;
2378 /* Else poor success; go back to mode in "active" table */
2381 IWL_DEBUG_RATE(priv, "LQ: GOING BACK TO THE OLD TABLE "
2382 "suc=%d cur-tpt=%d old-tpt=%d\n",
2383 window->success_ratio,
2384 window->average_tpt,
2387 /* Nullify "search" table */
2388 tbl->lq_type = LQ_NONE;
2390 /* Revert to "active" table */
2391 active_tbl = lq_sta->active_tbl;
2392 tbl = &(lq_sta->lq_info[active_tbl]);
2394 /* Revert to "active" rate and throughput info */
2395 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
2396 current_tpt = lq_sta->last_tpt;
2398 /* Need to set up a new rate table in uCode */
2402 /* Either way, we've made a decision; modulation mode
2403 * search is done, allow rate adjustment next time. */
2404 lq_sta->search_better_tbl = 0;
2405 done_search = 1; /* Don't switch modes below! */
2409 /* (Else) not in search of better modulation mode, try for better
2410 * starting rate, while staying in this mode. */
2411 high_low = rs_get_adjacent_rate(priv, index, rate_scale_index_msk,
2413 low = high_low & 0xff;
2414 high = (high_low >> 8) & 0xff;
2416 /* If user set max rate, dont allow higher than user constrain */
2417 if ((lq_sta->max_rate_idx != -1) &&
2418 (lq_sta->max_rate_idx < high))
2419 high = IWL_RATE_INVALID;
2421 sr = window->success_ratio;
2423 /* Collect measured throughputs for current and adjacent rates */
2424 current_tpt = window->average_tpt;
2425 if (low != IWL_RATE_INVALID)
2426 low_tpt = tbl->win[low].average_tpt;
2427 if (high != IWL_RATE_INVALID)
2428 high_tpt = tbl->win[high].average_tpt;
2432 /* Too many failures, decrease rate */
2433 if ((sr <= IWL_RATE_DECREASE_TH) || (current_tpt == 0)) {
2434 IWL_DEBUG_RATE(priv, "decrease rate because of low success_ratio\n");
2437 /* No throughput measured yet for adjacent rates; try increase. */
2438 } else if ((low_tpt == IWL_INVALID_VALUE) &&
2439 (high_tpt == IWL_INVALID_VALUE)) {
2441 if (high != IWL_RATE_INVALID && sr >= IWL_RATE_INCREASE_TH)
2443 else if (low != IWL_RATE_INVALID)
2447 /* Both adjacent throughputs are measured, but neither one has better
2448 * throughput; we're using the best rate, don't change it! */
2449 else if ((low_tpt != IWL_INVALID_VALUE) &&
2450 (high_tpt != IWL_INVALID_VALUE) &&
2451 (low_tpt < current_tpt) &&
2452 (high_tpt < current_tpt))
2455 /* At least one adjacent rate's throughput is measured,
2456 * and may have better performance. */
2458 /* Higher adjacent rate's throughput is measured */
2459 if (high_tpt != IWL_INVALID_VALUE) {
2460 /* Higher rate has better throughput */
2461 if (high_tpt > current_tpt &&
2462 sr >= IWL_RATE_INCREASE_TH) {
2468 /* Lower adjacent rate's throughput is measured */
2469 } else if (low_tpt != IWL_INVALID_VALUE) {
2470 /* Lower rate has better throughput */
2471 if (low_tpt > current_tpt) {
2472 IWL_DEBUG_RATE(priv,
2473 "decrease rate because of low tpt\n");
2475 } else if (sr >= IWL_RATE_INCREASE_TH) {
2481 /* Sanity check; asked for decrease, but success rate or throughput
2482 * has been good at old rate. Don't change it. */
2483 if ((scale_action == -1) && (low != IWL_RATE_INVALID) &&
2484 ((sr > IWL_RATE_HIGH_TH) ||
2485 (current_tpt > (100 * tbl->expected_tpt[low]))))
2487 if (!iwl_ht_enabled(priv) && !is_legacy(tbl->lq_type))
2489 if (iwl_tx_ant_restriction(priv) != IWL_ANT_OK_MULTI &&
2490 (is_mimo2(tbl->lq_type) || is_mimo3(tbl->lq_type)))
2493 if ((priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH) &&
2494 (is_mimo2(tbl->lq_type) || is_mimo3(tbl->lq_type))) {
2495 if (lq_sta->last_bt_traffic > priv->bt_traffic_load) {
2497 * don't set scale_action, don't want to scale up if
2498 * the rate scale doesn't otherwise think that is a
2501 } else if (lq_sta->last_bt_traffic <= priv->bt_traffic_load) {
2505 lq_sta->last_bt_traffic = priv->bt_traffic_load;
2507 if ((priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH) &&
2508 (is_mimo2(tbl->lq_type) || is_mimo3(tbl->lq_type))) {
2509 /* search for a new modulation */
2510 rs_stay_in_table(lq_sta, true);
2514 switch (scale_action) {
2516 /* Decrease starting rate, update uCode's rate table */
2517 if (low != IWL_RATE_INVALID) {
2524 /* Increase starting rate, update uCode's rate table */
2525 if (high != IWL_RATE_INVALID) {
2537 IWL_DEBUG_RATE(priv, "choose rate scale index %d action %d low %d "
2538 "high %d type %d\n",
2539 index, scale_action, low, high, tbl->lq_type);
2542 /* Replace uCode's rate table for the destination station. */
2544 rate = rs_update_rate_tbl(priv, ctx, lq_sta,
2545 tbl, index, is_green);
2547 if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_MULTI) {
2548 /* Should we stay with this modulation mode,
2549 * or search for a new one? */
2550 rs_stay_in_table(lq_sta, false);
2553 * Search for new modulation mode if we're:
2554 * 1) Not changing rates right now
2555 * 2) Not just finishing up a search
2556 * 3) Allowing a new search
2558 if (!update_lq && !done_search && !lq_sta->stay_in_tbl && window->counter) {
2559 /* Save current throughput to compare with "search" throughput*/
2560 lq_sta->last_tpt = current_tpt;
2562 /* Select a new "search" modulation mode to try.
2563 * If one is found, set up the new "search" table. */
2564 if (is_legacy(tbl->lq_type))
2565 rs_move_legacy_other(priv, lq_sta, conf, sta, index);
2566 else if (is_siso(tbl->lq_type))
2567 rs_move_siso_to_other(priv, lq_sta, conf, sta, index);
2568 else if (is_mimo2(tbl->lq_type))
2569 rs_move_mimo2_to_other(priv, lq_sta, conf, sta, index);
2571 rs_move_mimo3_to_other(priv, lq_sta, conf, sta, index);
2573 /* If new "search" mode was selected, set up in uCode table */
2574 if (lq_sta->search_better_tbl) {
2575 /* Access the "search" table, clear its history. */
2576 tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
2577 for (i = 0; i < IWL_RATE_COUNT; i++)
2578 rs_rate_scale_clear_window(&(tbl->win[i]));
2580 /* Use new "search" start rate */
2581 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
2583 IWL_DEBUG_RATE(priv, "Switch current mcs: %X index: %d\n",
2584 tbl->current_rate, index);
2585 rs_fill_link_cmd(priv, lq_sta, tbl->current_rate);
2586 iwl_send_lq_cmd(priv, ctx, &lq_sta->lq, CMD_ASYNC, false);
2591 if (done_search && !lq_sta->stay_in_tbl) {
2592 /* If the "active" (non-search) mode was legacy,
2593 * and we've tried switching antennas,
2594 * but we haven't been able to try HT modes (not available),
2595 * stay with best antenna legacy modulation for a while
2596 * before next round of mode comparisons. */
2597 tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
2598 if (is_legacy(tbl1->lq_type) && !conf_is_ht(conf) &&
2599 lq_sta->action_counter > tbl1->max_search) {
2600 IWL_DEBUG_RATE(priv, "LQ: STAY in legacy table\n");
2601 rs_set_stay_in_table(priv, 1, lq_sta);
2604 /* If we're in an HT mode, and all 3 mode switch actions
2605 * have been tried and compared, stay in this best modulation
2606 * mode for a while before next round of mode comparisons. */
2607 if (lq_sta->enable_counter &&
2608 (lq_sta->action_counter >= tbl1->max_search) &&
2609 iwl_ht_enabled(priv)) {
2610 if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
2611 (lq_sta->tx_agg_tid_en & (1 << tid)) &&
2612 (tid != MAX_TID_COUNT)) {
2614 &priv->stations[lq_sta->lq.sta_id].tid[tid];
2615 if (tid_data->agg.state == IWL_AGG_OFF) {
2616 IWL_DEBUG_RATE(priv,
2617 "try to aggregate tid %d\n",
2619 rs_tl_turn_on_agg(priv, tid,
2623 rs_set_stay_in_table(priv, 0, lq_sta);
2628 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
2630 lq_sta->last_txrate_idx = i;
2634 * rs_initialize_lq - Initialize a station's hardware rate table
2636 * The uCode's station table contains a table of fallback rates
2637 * for automatic fallback during transmission.
2639 * NOTE: This sets up a default set of values. These will be replaced later
2640 * if the driver's iwl-agn-rs rate scaling algorithm is used, instead of
2643 * NOTE: Run REPLY_ADD_STA command to set up station table entry, before
2644 * calling this function (which runs REPLY_TX_LINK_QUALITY_CMD,
2645 * which requires station table entry to exist).
2647 static void rs_initialize_lq(struct iwl_priv *priv,
2648 struct ieee80211_conf *conf,
2649 struct ieee80211_sta *sta,
2650 struct iwl_lq_sta *lq_sta)
2652 struct iwl_scale_tbl_info *tbl;
2656 u8 use_green = rs_use_green(sta);
2659 struct iwl_station_priv *sta_priv;
2660 struct iwl_rxon_context *ctx;
2662 if (!sta || !lq_sta)
2665 sta_priv = (void *)sta->drv_priv;
2666 ctx = sta_priv->common.ctx;
2668 i = lq_sta->last_txrate_idx;
2670 valid_tx_ant = priv->hw_params.valid_tx_ant;
2672 if (!lq_sta->search_better_tbl)
2673 active_tbl = lq_sta->active_tbl;
2675 active_tbl = 1 - lq_sta->active_tbl;
2677 tbl = &(lq_sta->lq_info[active_tbl]);
2679 if ((i < 0) || (i >= IWL_RATE_COUNT))
2682 rate = iwl_rates[i].plcp;
2683 tbl->ant_type = first_antenna(valid_tx_ant);
2684 rate |= tbl->ant_type << RATE_MCS_ANT_POS;
2686 if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
2687 rate |= RATE_MCS_CCK_MSK;
2689 rs_get_tbl_info_from_mcs(rate, priv->band, tbl, &rate_idx);
2690 if (!rs_is_valid_ant(valid_tx_ant, tbl->ant_type))
2691 rs_toggle_antenna(valid_tx_ant, &rate, tbl);
2693 rate = rate_n_flags_from_tbl(priv, tbl, rate_idx, use_green);
2694 tbl->current_rate = rate;
2695 rs_set_expected_tpt_table(lq_sta, tbl);
2696 rs_fill_link_cmd(NULL, lq_sta, rate);
2697 priv->stations[lq_sta->lq.sta_id].lq = &lq_sta->lq;
2698 iwl_send_lq_cmd(priv, ctx, &lq_sta->lq, CMD_SYNC, true);
2701 static void rs_get_rate(void *priv_r, struct ieee80211_sta *sta, void *priv_sta,
2702 struct ieee80211_tx_rate_control *txrc)
2705 struct sk_buff *skb = txrc->skb;
2706 struct ieee80211_supported_band *sband = txrc->sband;
2707 struct iwl_priv *priv __maybe_unused = (struct iwl_priv *)priv_r;
2708 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2709 struct iwl_lq_sta *lq_sta = priv_sta;
2712 IWL_DEBUG_RATE_LIMIT(priv, "rate scale calculate new rate for skb\n");
2714 /* Get max rate if user set max rate */
2716 lq_sta->max_rate_idx = txrc->max_rate_idx;
2717 if ((sband->band == IEEE80211_BAND_5GHZ) &&
2718 (lq_sta->max_rate_idx != -1))
2719 lq_sta->max_rate_idx += IWL_FIRST_OFDM_RATE;
2720 if ((lq_sta->max_rate_idx < 0) ||
2721 (lq_sta->max_rate_idx >= IWL_RATE_COUNT))
2722 lq_sta->max_rate_idx = -1;
2725 /* Treat uninitialized rate scaling data same as non-existing. */
2726 if (lq_sta && !lq_sta->drv) {
2727 IWL_DEBUG_RATE(priv, "Rate scaling not initialized yet.\n");
2731 /* Send management frames and NO_ACK data using lowest rate. */
2732 if (rate_control_send_low(sta, priv_sta, txrc))
2735 rate_idx = lq_sta->last_txrate_idx;
2737 if (lq_sta->last_rate_n_flags & RATE_MCS_HT_MSK) {
2738 rate_idx -= IWL_FIRST_OFDM_RATE;
2739 /* 6M and 9M shared same MCS index */
2740 rate_idx = (rate_idx > 0) ? (rate_idx - 1) : 0;
2741 if (rs_extract_rate(lq_sta->last_rate_n_flags) >=
2742 IWL_RATE_MIMO3_6M_PLCP)
2743 rate_idx = rate_idx + (2 * MCS_INDEX_PER_STREAM);
2744 else if (rs_extract_rate(lq_sta->last_rate_n_flags) >=
2745 IWL_RATE_MIMO2_6M_PLCP)
2746 rate_idx = rate_idx + MCS_INDEX_PER_STREAM;
2747 info->control.rates[0].flags = IEEE80211_TX_RC_MCS;
2748 if (lq_sta->last_rate_n_flags & RATE_MCS_SGI_MSK)
2749 info->control.rates[0].flags |= IEEE80211_TX_RC_SHORT_GI;
2750 if (lq_sta->last_rate_n_flags & RATE_MCS_DUP_MSK)
2751 info->control.rates[0].flags |= IEEE80211_TX_RC_DUP_DATA;
2752 if (lq_sta->last_rate_n_flags & RATE_MCS_HT40_MSK)
2753 info->control.rates[0].flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
2754 if (lq_sta->last_rate_n_flags & RATE_MCS_GF_MSK)
2755 info->control.rates[0].flags |= IEEE80211_TX_RC_GREEN_FIELD;
2757 /* Check for invalid rates */
2758 if ((rate_idx < 0) || (rate_idx >= IWL_RATE_COUNT_LEGACY) ||
2759 ((sband->band == IEEE80211_BAND_5GHZ) &&
2760 (rate_idx < IWL_FIRST_OFDM_RATE)))
2761 rate_idx = rate_lowest_index(sband, sta);
2762 /* On valid 5 GHz rate, adjust index */
2763 else if (sband->band == IEEE80211_BAND_5GHZ)
2764 rate_idx -= IWL_FIRST_OFDM_RATE;
2765 info->control.rates[0].flags = 0;
2767 info->control.rates[0].idx = rate_idx;
2771 static void *rs_alloc_sta(void *priv_rate, struct ieee80211_sta *sta,
2774 struct iwl_station_priv *sta_priv = (struct iwl_station_priv *) sta->drv_priv;
2775 struct iwl_priv *priv;
2777 priv = (struct iwl_priv *)priv_rate;
2778 IWL_DEBUG_RATE(priv, "create station rate scale window\n");
2780 return &sta_priv->lq_sta;
2784 * Called after adding a new station to initialize rate scaling
2786 void iwl_rs_rate_init(struct iwl_priv *priv, struct ieee80211_sta *sta, u8 sta_id)
2789 struct ieee80211_hw *hw = priv->hw;
2790 struct ieee80211_conf *conf = &priv->hw->conf;
2791 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
2792 struct iwl_station_priv *sta_priv;
2793 struct iwl_lq_sta *lq_sta;
2794 struct ieee80211_supported_band *sband;
2796 sta_priv = (struct iwl_station_priv *) sta->drv_priv;
2797 lq_sta = &sta_priv->lq_sta;
2798 sband = hw->wiphy->bands[conf->channel->band];
2801 lq_sta->lq.sta_id = sta_id;
2803 for (j = 0; j < LQ_SIZE; j++)
2804 for (i = 0; i < IWL_RATE_COUNT; i++)
2805 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
2807 lq_sta->flush_timer = 0;
2808 lq_sta->supp_rates = sta->supp_rates[sband->band];
2809 for (j = 0; j < LQ_SIZE; j++)
2810 for (i = 0; i < IWL_RATE_COUNT; i++)
2811 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
2813 IWL_DEBUG_RATE(priv, "LQ: *** rate scale station global init for station %d ***\n",
2815 /* TODO: what is a good starting rate for STA? About middle? Maybe not
2816 * the lowest or the highest rate.. Could consider using RSSI from
2817 * previous packets? Need to have IEEE 802.1X auth succeed immediately
2821 lq_sta->max_rate_idx = -1;
2822 lq_sta->missed_rate_counter = IWL_MISSED_RATE_MAX;
2823 lq_sta->is_green = rs_use_green(sta);
2824 lq_sta->active_legacy_rate = priv->active_rate & ~(0x1000);
2825 lq_sta->band = priv->band;
2827 * active_siso_rate mask includes 9 MBits (bit 5), and CCK (bits 0-3),
2828 * supp_rates[] does not; shift to convert format, force 9 MBits off.
2830 lq_sta->active_siso_rate = ht_cap->mcs.rx_mask[0] << 1;
2831 lq_sta->active_siso_rate |= ht_cap->mcs.rx_mask[0] & 0x1;
2832 lq_sta->active_siso_rate &= ~((u16)0x2);
2833 lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE;
2836 lq_sta->active_mimo2_rate = ht_cap->mcs.rx_mask[1] << 1;
2837 lq_sta->active_mimo2_rate |= ht_cap->mcs.rx_mask[1] & 0x1;
2838 lq_sta->active_mimo2_rate &= ~((u16)0x2);
2839 lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE;
2841 lq_sta->active_mimo3_rate = ht_cap->mcs.rx_mask[2] << 1;
2842 lq_sta->active_mimo3_rate |= ht_cap->mcs.rx_mask[2] & 0x1;
2843 lq_sta->active_mimo3_rate &= ~((u16)0x2);
2844 lq_sta->active_mimo3_rate <<= IWL_FIRST_OFDM_RATE;
2846 IWL_DEBUG_RATE(priv, "SISO-RATE=%X MIMO2-RATE=%X MIMO3-RATE=%X\n",
2847 lq_sta->active_siso_rate,
2848 lq_sta->active_mimo2_rate,
2849 lq_sta->active_mimo3_rate);
2851 /* These values will be overridden later */
2852 lq_sta->lq.general_params.single_stream_ant_msk =
2853 first_antenna(priv->hw_params.valid_tx_ant);
2854 lq_sta->lq.general_params.dual_stream_ant_msk =
2855 priv->hw_params.valid_tx_ant &
2856 ~first_antenna(priv->hw_params.valid_tx_ant);
2857 if (!lq_sta->lq.general_params.dual_stream_ant_msk) {
2858 lq_sta->lq.general_params.dual_stream_ant_msk = ANT_AB;
2859 } else if (num_of_ant(priv->hw_params.valid_tx_ant) == 2) {
2860 lq_sta->lq.general_params.dual_stream_ant_msk =
2861 priv->hw_params.valid_tx_ant;
2864 /* as default allow aggregation for all tids */
2865 lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
2868 /* Set last_txrate_idx to lowest rate */
2869 lq_sta->last_txrate_idx = rate_lowest_index(sband, sta);
2870 if (sband->band == IEEE80211_BAND_5GHZ)
2871 lq_sta->last_txrate_idx += IWL_FIRST_OFDM_RATE;
2874 #ifdef CONFIG_MAC80211_DEBUGFS
2875 lq_sta->dbg_fixed_rate = 0;
2878 rs_initialize_lq(priv, conf, sta, lq_sta);
2881 static void rs_fill_link_cmd(struct iwl_priv *priv,
2882 struct iwl_lq_sta *lq_sta, u32 new_rate)
2884 struct iwl_scale_tbl_info tbl_type;
2887 int repeat_rate = 0;
2888 u8 ant_toggle_cnt = 0;
2889 u8 use_ht_possible = 1;
2890 u8 valid_tx_ant = 0;
2891 struct iwl_station_priv *sta_priv =
2892 container_of(lq_sta, struct iwl_station_priv, lq_sta);
2893 struct iwl_link_quality_cmd *lq_cmd = &lq_sta->lq;
2895 /* Override starting rate (index 0) if needed for debug purposes */
2896 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
2898 /* Interpret new_rate (rate_n_flags) */
2899 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band,
2900 &tbl_type, &rate_idx);
2902 if (priv && priv->bt_full_concurrent) {
2905 first_antenna(priv->hw_params.valid_tx_ant);
2908 /* How many times should we repeat the initial rate? */
2909 if (is_legacy(tbl_type.lq_type)) {
2911 repeat_rate = IWL_NUMBER_TRY;
2913 repeat_rate = min(IWL_HT_NUMBER_TRY,
2914 LINK_QUAL_AGG_DISABLE_START_DEF - 1);
2917 lq_cmd->general_params.mimo_delimiter =
2918 is_mimo(tbl_type.lq_type) ? 1 : 0;
2920 /* Fill 1st table entry (index 0) */
2921 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
2923 if (num_of_ant(tbl_type.ant_type) == 1) {
2924 lq_cmd->general_params.single_stream_ant_msk =
2926 } else if (num_of_ant(tbl_type.ant_type) == 2) {
2927 lq_cmd->general_params.dual_stream_ant_msk =
2929 } /* otherwise we don't modify the existing value */
2934 if (priv->bt_full_concurrent)
2935 valid_tx_ant = ANT_A;
2937 valid_tx_ant = priv->hw_params.valid_tx_ant;
2940 /* Fill rest of rate table */
2941 while (index < LINK_QUAL_MAX_RETRY_NUM) {
2942 /* Repeat initial/next rate.
2943 * For legacy IWL_NUMBER_TRY == 1, this loop will not execute.
2944 * For HT IWL_HT_NUMBER_TRY == 3, this executes twice. */
2945 while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
2946 if (is_legacy(tbl_type.lq_type)) {
2947 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2950 rs_toggle_antenna(valid_tx_ant,
2951 &new_rate, &tbl_type))
2955 /* Override next rate if needed for debug purposes */
2956 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
2958 /* Fill next table entry */
2959 lq_cmd->rs_table[index].rate_n_flags =
2960 cpu_to_le32(new_rate);
2965 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band, &tbl_type,
2968 if (priv && priv->bt_full_concurrent) {
2971 first_antenna(priv->hw_params.valid_tx_ant);
2974 /* Indicate to uCode which entries might be MIMO.
2975 * If initial rate was MIMO, this will finally end up
2976 * as (IWL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
2977 if (is_mimo(tbl_type.lq_type))
2978 lq_cmd->general_params.mimo_delimiter = index;
2981 new_rate = rs_get_lower_rate(lq_sta, &tbl_type, rate_idx,
2984 /* How many times should we repeat the next rate? */
2985 if (is_legacy(tbl_type.lq_type)) {
2986 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2989 rs_toggle_antenna(valid_tx_ant,
2990 &new_rate, &tbl_type))
2993 repeat_rate = IWL_NUMBER_TRY;
2995 repeat_rate = IWL_HT_NUMBER_TRY;
2998 /* Don't allow HT rates after next pass.
2999 * rs_get_lower_rate() will change type to LQ_A or LQ_G. */
3000 use_ht_possible = 0;
3002 /* Override next rate if needed for debug purposes */
3003 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
3005 /* Fill next table entry */
3006 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
3012 lq_cmd->agg_params.agg_frame_cnt_limit =
3013 sta_priv->max_agg_bufsize ?: LINK_QUAL_AGG_FRAME_LIMIT_DEF;
3014 lq_cmd->agg_params.agg_dis_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
3016 lq_cmd->agg_params.agg_time_limit =
3017 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
3019 * overwrite if needed, pass aggregation time limit
3022 if (priv && priv->cfg->bt_params &&
3023 priv->cfg->bt_params->agg_time_limit &&
3024 priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH)
3025 lq_cmd->agg_params.agg_time_limit =
3026 cpu_to_le16(priv->cfg->bt_params->agg_time_limit);
3029 static void *rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
3033 /* rate scale requires free function to be implemented */
3034 static void rs_free(void *priv_rate)
3039 static void rs_free_sta(void *priv_r, struct ieee80211_sta *sta,
3042 struct iwl_priv *priv __maybe_unused = priv_r;
3044 IWL_DEBUG_RATE(priv, "enter\n");
3045 IWL_DEBUG_RATE(priv, "leave\n");
3049 #ifdef CONFIG_MAC80211_DEBUGFS
3050 static int open_file_generic(struct inode *inode, struct file *file)
3052 file->private_data = inode->i_private;
3055 static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
3056 u32 *rate_n_flags, int index)
3058 struct iwl_priv *priv;
3063 valid_tx_ant = priv->hw_params.valid_tx_ant;
3064 if (lq_sta->dbg_fixed_rate) {
3066 ((lq_sta->dbg_fixed_rate & RATE_MCS_ANT_ABC_MSK)
3067 >> RATE_MCS_ANT_POS);
3068 if ((valid_tx_ant & ant_sel_tx) == ant_sel_tx) {
3069 *rate_n_flags = lq_sta->dbg_fixed_rate;
3070 IWL_DEBUG_RATE(priv, "Fixed rate ON\n");
3072 lq_sta->dbg_fixed_rate = 0;
3074 "Invalid antenna selection 0x%X, Valid is 0x%X\n",
3075 ant_sel_tx, valid_tx_ant);
3076 IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
3079 IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
3083 static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
3084 const char __user *user_buf, size_t count, loff_t *ppos)
3086 struct iwl_lq_sta *lq_sta = file->private_data;
3087 struct iwl_priv *priv;
3091 struct iwl_station_priv *sta_priv =
3092 container_of(lq_sta, struct iwl_station_priv, lq_sta);
3093 struct iwl_rxon_context *ctx = sta_priv->common.ctx;
3096 memset(buf, 0, sizeof(buf));
3097 buf_size = min(count, sizeof(buf) - 1);
3098 if (copy_from_user(buf, user_buf, buf_size))
3101 if (sscanf(buf, "%x", &parsed_rate) == 1)
3102 lq_sta->dbg_fixed_rate = parsed_rate;
3104 lq_sta->dbg_fixed_rate = 0;
3106 lq_sta->active_legacy_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
3107 lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
3108 lq_sta->active_mimo2_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
3109 lq_sta->active_mimo3_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
3111 IWL_DEBUG_RATE(priv, "sta_id %d rate 0x%X\n",
3112 lq_sta->lq.sta_id, lq_sta->dbg_fixed_rate);
3114 if (lq_sta->dbg_fixed_rate) {
3115 rs_fill_link_cmd(NULL, lq_sta, lq_sta->dbg_fixed_rate);
3116 iwl_send_lq_cmd(lq_sta->drv, ctx, &lq_sta->lq, CMD_ASYNC,
3123 static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
3124 char __user *user_buf, size_t count, loff_t *ppos)
3132 struct iwl_lq_sta *lq_sta = file->private_data;
3133 struct iwl_priv *priv;
3134 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
3137 buff = kmalloc(1024, GFP_KERNEL);
3141 desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
3142 desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
3143 lq_sta->total_failed, lq_sta->total_success,
3144 lq_sta->active_legacy_rate);
3145 desc += sprintf(buff+desc, "fixed rate 0x%X\n",
3146 lq_sta->dbg_fixed_rate);
3147 desc += sprintf(buff+desc, "valid_tx_ant %s%s%s\n",
3148 (priv->hw_params.valid_tx_ant & ANT_A) ? "ANT_A," : "",
3149 (priv->hw_params.valid_tx_ant & ANT_B) ? "ANT_B," : "",
3150 (priv->hw_params.valid_tx_ant & ANT_C) ? "ANT_C" : "");
3151 desc += sprintf(buff+desc, "lq type %s\n",
3152 (is_legacy(tbl->lq_type)) ? "legacy" : "HT");
3153 if (is_Ht(tbl->lq_type)) {
3154 desc += sprintf(buff+desc, " %s",
3155 (is_siso(tbl->lq_type)) ? "SISO" :
3156 ((is_mimo2(tbl->lq_type)) ? "MIMO2" : "MIMO3"));
3157 desc += sprintf(buff+desc, " %s",
3158 (tbl->is_ht40) ? "40MHz" : "20MHz");
3159 desc += sprintf(buff+desc, " %s %s %s\n", (tbl->is_SGI) ? "SGI" : "",
3160 (lq_sta->is_green) ? "GF enabled" : "",
3161 (lq_sta->is_agg) ? "AGG on" : "");
3163 desc += sprintf(buff+desc, "last tx rate=0x%X\n",
3164 lq_sta->last_rate_n_flags);
3165 desc += sprintf(buff+desc, "general:"
3166 "flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
3167 lq_sta->lq.general_params.flags,
3168 lq_sta->lq.general_params.mimo_delimiter,
3169 lq_sta->lq.general_params.single_stream_ant_msk,
3170 lq_sta->lq.general_params.dual_stream_ant_msk);
3172 desc += sprintf(buff+desc, "agg:"
3173 "time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
3174 le16_to_cpu(lq_sta->lq.agg_params.agg_time_limit),
3175 lq_sta->lq.agg_params.agg_dis_start_th,
3176 lq_sta->lq.agg_params.agg_frame_cnt_limit);
3178 desc += sprintf(buff+desc,
3179 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
3180 lq_sta->lq.general_params.start_rate_index[0],
3181 lq_sta->lq.general_params.start_rate_index[1],
3182 lq_sta->lq.general_params.start_rate_index[2],
3183 lq_sta->lq.general_params.start_rate_index[3]);
3185 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
3186 index = iwl_hwrate_to_plcp_idx(
3187 le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags));
3188 if (is_legacy(tbl->lq_type)) {
3189 desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps\n",
3190 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags),
3191 iwl_rate_mcs[index].mbps);
3193 desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps (%s)\n",
3194 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags),
3195 iwl_rate_mcs[index].mbps, iwl_rate_mcs[index].mcs);
3199 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
3204 static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
3205 .write = rs_sta_dbgfs_scale_table_write,
3206 .read = rs_sta_dbgfs_scale_table_read,
3207 .open = open_file_generic,
3208 .llseek = default_llseek,
3210 static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
3211 char __user *user_buf, size_t count, loff_t *ppos)
3218 struct iwl_lq_sta *lq_sta = file->private_data;
3220 buff = kmalloc(1024, GFP_KERNEL);
3224 for (i = 0; i < LQ_SIZE; i++) {
3225 desc += sprintf(buff+desc,
3226 "%s type=%d SGI=%d HT40=%d DUP=%d GF=%d\n"
3228 lq_sta->active_tbl == i ? "*" : "x",
3229 lq_sta->lq_info[i].lq_type,
3230 lq_sta->lq_info[i].is_SGI,
3231 lq_sta->lq_info[i].is_ht40,
3232 lq_sta->lq_info[i].is_dup,
3234 lq_sta->lq_info[i].current_rate);
3235 for (j = 0; j < IWL_RATE_COUNT; j++) {
3236 desc += sprintf(buff+desc,
3237 "counter=%d success=%d %%=%d\n",
3238 lq_sta->lq_info[i].win[j].counter,
3239 lq_sta->lq_info[i].win[j].success_counter,
3240 lq_sta->lq_info[i].win[j].success_ratio);
3243 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
3248 static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
3249 .read = rs_sta_dbgfs_stats_table_read,
3250 .open = open_file_generic,
3251 .llseek = default_llseek,
3254 static ssize_t rs_sta_dbgfs_rate_scale_data_read(struct file *file,
3255 char __user *user_buf, size_t count, loff_t *ppos)
3260 struct iwl_lq_sta *lq_sta = file->private_data;
3261 struct iwl_priv *priv;
3262 struct iwl_scale_tbl_info *tbl = &lq_sta->lq_info[lq_sta->active_tbl];
3266 if (is_Ht(tbl->lq_type))
3267 desc += sprintf(buff+desc,
3268 "Bit Rate= %d Mb/s\n",
3269 tbl->expected_tpt[lq_sta->last_txrate_idx]);
3271 desc += sprintf(buff+desc,
3272 "Bit Rate= %d Mb/s\n",
3273 iwl_rates[lq_sta->last_txrate_idx].ieee >> 1);
3275 return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
3278 static const struct file_operations rs_sta_dbgfs_rate_scale_data_ops = {
3279 .read = rs_sta_dbgfs_rate_scale_data_read,
3280 .open = open_file_generic,
3281 .llseek = default_llseek,
3284 static void rs_add_debugfs(void *priv, void *priv_sta,
3287 struct iwl_lq_sta *lq_sta = priv_sta;
3288 lq_sta->rs_sta_dbgfs_scale_table_file =
3289 debugfs_create_file("rate_scale_table", S_IRUSR | S_IWUSR, dir,
3290 lq_sta, &rs_sta_dbgfs_scale_table_ops);
3291 lq_sta->rs_sta_dbgfs_stats_table_file =
3292 debugfs_create_file("rate_stats_table", S_IRUSR, dir,
3293 lq_sta, &rs_sta_dbgfs_stats_table_ops);
3294 lq_sta->rs_sta_dbgfs_rate_scale_data_file =
3295 debugfs_create_file("rate_scale_data", S_IRUSR, dir,
3296 lq_sta, &rs_sta_dbgfs_rate_scale_data_ops);
3297 lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file =
3298 debugfs_create_u8("tx_agg_tid_enable", S_IRUSR | S_IWUSR, dir,
3299 &lq_sta->tx_agg_tid_en);
3303 static void rs_remove_debugfs(void *priv, void *priv_sta)
3305 struct iwl_lq_sta *lq_sta = priv_sta;
3306 debugfs_remove(lq_sta->rs_sta_dbgfs_scale_table_file);
3307 debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
3308 debugfs_remove(lq_sta->rs_sta_dbgfs_rate_scale_data_file);
3309 debugfs_remove(lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file);
3314 * Initialization of rate scaling information is done by driver after
3315 * the station is added. Since mac80211 calls this function before a
3316 * station is added we ignore it.
3318 static void rs_rate_init_stub(void *priv_r, struct ieee80211_supported_band *sband,
3319 struct ieee80211_sta *sta, void *priv_sta)
3322 static struct rate_control_ops rs_ops = {
3325 .tx_status = rs_tx_status,
3326 .get_rate = rs_get_rate,
3327 .rate_init = rs_rate_init_stub,
3330 .alloc_sta = rs_alloc_sta,
3331 .free_sta = rs_free_sta,
3332 #ifdef CONFIG_MAC80211_DEBUGFS
3333 .add_sta_debugfs = rs_add_debugfs,
3334 .remove_sta_debugfs = rs_remove_debugfs,
3338 int iwlagn_rate_control_register(void)
3340 return ieee80211_rate_control_register(&rs_ops);
3343 void iwlagn_rate_control_unregister(void)
3345 ieee80211_rate_control_unregister(&rs_ops);