blob: a0b4cc8d9c3b412c931d04606d02b72c4d769e61 [file] [log] [blame]
Johannes Berg8ca151b2013-01-24 14:25:36 +01001/******************************************************************************
2 *
3 * Copyright(c) 2005 - 2013 Intel Corporation. All rights reserved.
4 *
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.
8 *
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
12 * more details.
13 *
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
17 *
18 * The full GNU General Public License is included in this distribution in the
19 * file called LICENSE.
20 *
21 * Contact Information:
22 * Intel Linux Wireless <ilw@linux.intel.com>
23 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24 *
25 *****************************************************************************/
26#include <linux/kernel.h>
27#include <linux/init.h>
28#include <linux/skbuff.h>
29#include <linux/slab.h>
30#include <net/mac80211.h>
31
32#include <linux/netdevice.h>
33#include <linux/etherdevice.h>
34#include <linux/delay.h>
35
36#include <linux/workqueue.h>
37#include "rs.h"
38#include "fw-api.h"
39#include "sta.h"
40#include "iwl-op-mode.h"
41#include "mvm.h"
42
43#define RS_NAME "iwl-mvm-rs"
44
45#define NUM_TRY_BEFORE_ANT_TOGGLE 1
46#define IWL_NUMBER_TRY 1
47#define IWL_HT_NUMBER_TRY 3
48
49#define IWL_RATE_MAX_WINDOW 62 /* # tx in history window */
50#define IWL_RATE_MIN_FAILURE_TH 6 /* min failures to calc tpt */
51#define IWL_RATE_MIN_SUCCESS_TH 8 /* min successes to calc tpt */
52
53/* max allowed rate miss before sync LQ cmd */
54#define IWL_MISSED_RATE_MAX 15
55/* max time to accum history 2 seconds */
56#define IWL_RATE_SCALE_FLUSH_INTVL (3*HZ)
57
58static u8 rs_ht_to_legacy[] = {
Eliad Pelleref4394b2013-07-16 18:07:20 +030059 [IWL_RATE_1M_INDEX] = IWL_RATE_6M_INDEX,
60 [IWL_RATE_2M_INDEX] = IWL_RATE_6M_INDEX,
61 [IWL_RATE_5M_INDEX] = IWL_RATE_6M_INDEX,
62 [IWL_RATE_11M_INDEX] = IWL_RATE_6M_INDEX,
63 [IWL_RATE_6M_INDEX] = IWL_RATE_6M_INDEX,
64 [IWL_RATE_9M_INDEX] = IWL_RATE_6M_INDEX,
65 [IWL_RATE_12M_INDEX] = IWL_RATE_9M_INDEX,
66 [IWL_RATE_18M_INDEX] = IWL_RATE_12M_INDEX,
67 [IWL_RATE_24M_INDEX] = IWL_RATE_18M_INDEX,
68 [IWL_RATE_36M_INDEX] = IWL_RATE_24M_INDEX,
69 [IWL_RATE_48M_INDEX] = IWL_RATE_36M_INDEX,
70 [IWL_RATE_54M_INDEX] = IWL_RATE_48M_INDEX,
71 [IWL_RATE_60M_INDEX] = IWL_RATE_54M_INDEX,
Johannes Berg8ca151b2013-01-24 14:25:36 +010072};
73
74static const u8 ant_toggle_lookup[] = {
Eliad Pelleref4394b2013-07-16 18:07:20 +030075 [ANT_NONE] = ANT_NONE,
76 [ANT_A] = ANT_B,
77 [ANT_B] = ANT_C,
78 [ANT_AB] = ANT_BC,
79 [ANT_C] = ANT_A,
80 [ANT_AC] = ANT_AB,
81 [ANT_BC] = ANT_AC,
82 [ANT_ABC] = ANT_ABC,
Johannes Berg8ca151b2013-01-24 14:25:36 +010083};
84
Eyal Shapirad310e402013-08-11 18:43:47 +030085#define IWL_DECLARE_RATE_INFO(r, s, rp, rn) \
86 [IWL_RATE_##r##M_INDEX] = { IWL_RATE_##r##M_PLCP, \
87 IWL_RATE_HT_SISO_MCS_##s##_PLCP, \
88 IWL_RATE_HT_MIMO2_MCS_##s##_PLCP, \
89 IWL_RATE_VHT_SISO_MCS_##s##_PLCP, \
90 IWL_RATE_VHT_MIMO2_MCS_##s##_PLCP,\
91 IWL_RATE_##rp##M_INDEX, \
Eyal Shapiraab055ce2013-08-04 13:10:31 +030092 IWL_RATE_##rn##M_INDEX }
Johannes Berg8ca151b2013-01-24 14:25:36 +010093
Eyal Shapirad310e402013-08-11 18:43:47 +030094#define IWL_DECLARE_MCS_RATE(s) \
95 [IWL_RATE_MCS_##s##_INDEX] = { IWL_RATE_INVM_PLCP, \
96 IWL_RATE_HT_SISO_MCS_##s##_PLCP, \
97 IWL_RATE_HT_MIMO2_MCS_##s##_PLCP, \
98 IWL_RATE_VHT_SISO_MCS_##s##_PLCP, \
99 IWL_RATE_VHT_MIMO2_MCS_##s##_PLCP, \
100 IWL_RATE_INVM_INDEX, \
101 IWL_RATE_INVM_INDEX }
102
Johannes Berg8ca151b2013-01-24 14:25:36 +0100103/*
104 * Parameter order:
Eyal Shapiraab055ce2013-08-04 13:10:31 +0300105 * rate, ht rate, prev rate, next rate
Johannes Berg8ca151b2013-01-24 14:25:36 +0100106 *
107 * If there isn't a valid next or previous rate then INV is used which
108 * maps to IWL_RATE_INVALID
109 *
110 */
111static const struct iwl_rs_rate_info iwl_rates[IWL_RATE_COUNT] = {
Eyal Shapiraab055ce2013-08-04 13:10:31 +0300112 IWL_DECLARE_RATE_INFO(1, INV, INV, 2), /* 1mbps */
113 IWL_DECLARE_RATE_INFO(2, INV, 1, 5), /* 2mbps */
114 IWL_DECLARE_RATE_INFO(5, INV, 2, 11), /*5.5mbps */
115 IWL_DECLARE_RATE_INFO(11, INV, 9, 12), /* 11mbps */
Eyal Shapirad310e402013-08-11 18:43:47 +0300116 IWL_DECLARE_RATE_INFO(6, 0, 5, 11), /* 6mbps ; MCS 0 */
117 IWL_DECLARE_RATE_INFO(9, INV, 6, 11), /* 9mbps */
118 IWL_DECLARE_RATE_INFO(12, 1, 11, 18), /* 12mbps ; MCS 1 */
119 IWL_DECLARE_RATE_INFO(18, 2, 12, 24), /* 18mbps ; MCS 2 */
120 IWL_DECLARE_RATE_INFO(24, 3, 18, 36), /* 24mbps ; MCS 3 */
121 IWL_DECLARE_RATE_INFO(36, 4, 24, 48), /* 36mbps ; MCS 4 */
122 IWL_DECLARE_RATE_INFO(48, 5, 36, 54), /* 48mbps ; MCS 5 */
123 IWL_DECLARE_RATE_INFO(54, 6, 48, INV), /* 54mbps ; MCS 6 */
124 IWL_DECLARE_MCS_RATE(7), /* MCS 7 */
125 IWL_DECLARE_MCS_RATE(8), /* MCS 8 */
126 IWL_DECLARE_MCS_RATE(9), /* MCS 9 */
Johannes Berg8ca151b2013-01-24 14:25:36 +0100127};
128
129static inline u8 rs_extract_rate(u32 rate_n_flags)
130{
131 /* also works for HT because bits 7:6 are zero there */
132 return (u8)(rate_n_flags & RATE_LEGACY_RATE_MSK);
133}
134
135static int iwl_hwrate_to_plcp_idx(u32 rate_n_flags)
136{
137 int idx = 0;
138
Johannes Berg8ca151b2013-01-24 14:25:36 +0100139 if (rate_n_flags & RATE_MCS_HT_MSK) {
Eyal Shapirad310e402013-08-11 18:43:47 +0300140 idx = rate_n_flags & RATE_HT_MCS_RATE_CODE_MSK;
141 idx += IWL_RATE_MCS_0_INDEX;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100142
Eyal Shapirad310e402013-08-11 18:43:47 +0300143 /* skip 9M not supported in HT*/
Johannes Berg8ca151b2013-01-24 14:25:36 +0100144 if (idx >= IWL_RATE_9M_INDEX)
145 idx += 1;
Eyal Shapira4e82dd32013-08-04 23:58:37 +0300146 if ((idx >= IWL_FIRST_HT_RATE) && (idx <= IWL_LAST_HT_RATE))
Johannes Berg8ca151b2013-01-24 14:25:36 +0100147 return idx;
Eyal Shapirad310e402013-08-11 18:43:47 +0300148 } else if (rate_n_flags & RATE_MCS_VHT_MSK) {
149 idx = rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK;
150 idx += IWL_RATE_MCS_0_INDEX;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100151
Eyal Shapirad310e402013-08-11 18:43:47 +0300152 /* skip 9M not supported in VHT*/
153 if (idx >= IWL_RATE_9M_INDEX)
154 idx++;
155 if ((idx >= IWL_FIRST_VHT_RATE) && (idx <= IWL_LAST_VHT_RATE))
156 return idx;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100157 } else {
Eyal Shapirad310e402013-08-11 18:43:47 +0300158 /* legacy rate format, search for match in table */
159
160 u8 legacy_rate = rs_extract_rate(rate_n_flags);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100161 for (idx = 0; idx < ARRAY_SIZE(iwl_rates); idx++)
Eyal Shapirad310e402013-08-11 18:43:47 +0300162 if (iwl_rates[idx].plcp == legacy_rate)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100163 return idx;
164 }
165
166 return -1;
167}
168
169static void rs_rate_scale_perform(struct iwl_mvm *mvm,
170 struct sk_buff *skb,
171 struct ieee80211_sta *sta,
172 struct iwl_lq_sta *lq_sta);
173static void rs_fill_link_cmd(struct iwl_mvm *mvm,
Emmanuel Grumbach9145d152013-07-18 08:45:41 +0300174 struct ieee80211_sta *sta,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100175 struct iwl_lq_sta *lq_sta, u32 rate_n_flags);
176static void rs_stay_in_table(struct iwl_lq_sta *lq_sta, bool force_search);
177
178
179#ifdef CONFIG_MAC80211_DEBUGFS
180static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
Eyal Shapira3571ac32013-07-29 17:08:48 +0300181 u32 *rate_n_flags);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100182#else
183static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
Eyal Shapira3571ac32013-07-29 17:08:48 +0300184 u32 *rate_n_flags)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100185{}
186#endif
187
188/**
189 * The following tables contain the expected throughput metrics for all rates
190 *
191 * 1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
192 *
193 * where invalid entries are zeros.
194 *
195 * CCK rates are only valid in legacy table and will only be used in G
196 * (2.4 GHz) band.
197 */
198
199static s32 expected_tpt_legacy[IWL_RATE_COUNT] = {
Eyal Shapira4e82dd32013-08-04 23:58:37 +0300200 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 0, 0, 0
Johannes Berg8ca151b2013-01-24 14:25:36 +0100201};
202
Eyal Shapira4e82dd32013-08-04 23:58:37 +0300203/* Expected TpT tables. 4 indexes:
204 * 0 - NGI, 1 - SGI, 2 - AGG+NGI, 3 - AGG+SGI
205 */
206static s32 expected_tpt_siso_20MHz[4][IWL_RATE_COUNT] = {
207 {0, 0, 0, 0, 42, 0, 76, 102, 124, 159, 183, 193, 202, 216, 0},
208 {0, 0, 0, 0, 46, 0, 82, 110, 132, 168, 192, 202, 210, 225, 0},
209 {0, 0, 0, 0, 49, 0, 97, 145, 192, 285, 375, 420, 464, 551, 0},
210 {0, 0, 0, 0, 54, 0, 108, 160, 213, 315, 415, 465, 513, 608, 0},
Johannes Berg8ca151b2013-01-24 14:25:36 +0100211};
212
Eyal Shapira4e82dd32013-08-04 23:58:37 +0300213static s32 expected_tpt_siso_40MHz[4][IWL_RATE_COUNT] = {
214 {0, 0, 0, 0, 77, 0, 127, 160, 184, 220, 242, 250, 257, 269, 275},
215 {0, 0, 0, 0, 83, 0, 135, 169, 193, 229, 250, 257, 264, 275, 280},
216 {0, 0, 0, 0, 101, 0, 199, 295, 389, 570, 744, 828, 911, 1070, 1173},
217 {0, 0, 0, 0, 112, 0, 220, 326, 429, 629, 819, 912, 1000, 1173, 1284},
Johannes Berg8ca151b2013-01-24 14:25:36 +0100218};
219
Eyal Shapirad310e402013-08-11 18:43:47 +0300220static s32 expected_tpt_siso_80MHz[4][IWL_RATE_COUNT] = {
221 {0, 0, 0, 0, 130, 0, 191, 223, 244, 273, 288, 294, 298, 305, 308},
222 {0, 0, 0, 0, 138, 0, 200, 231, 251, 279, 293, 298, 302, 308, 312},
223 {0, 0, 0, 0, 217, 0, 429, 634, 834, 1220, 1585, 1760, 1931, 2258, 2466},
224 {0, 0, 0, 0, 241, 0, 475, 701, 921, 1343, 1741, 1931, 2117, 2468, 2691},
225};
226
Johannes Berg8ca151b2013-01-24 14:25:36 +0100227static s32 expected_tpt_mimo2_20MHz[4][IWL_RATE_COUNT] = {
Eyal Shapira4e82dd32013-08-04 23:58:37 +0300228 {0, 0, 0, 0, 74, 0, 123, 155, 179, 213, 235, 243, 250, 261, 0},
229 {0, 0, 0, 0, 81, 0, 131, 164, 187, 221, 242, 250, 256, 267, 0},
230 {0, 0, 0, 0, 98, 0, 193, 286, 375, 550, 718, 799, 878, 1032, 0},
231 {0, 0, 0, 0, 109, 0, 214, 316, 414, 607, 790, 879, 965, 1132, 0},
Johannes Berg8ca151b2013-01-24 14:25:36 +0100232};
233
234static s32 expected_tpt_mimo2_40MHz[4][IWL_RATE_COUNT] = {
Eyal Shapira4e82dd32013-08-04 23:58:37 +0300235 {0, 0, 0, 0, 123, 0, 182, 214, 235, 264, 279, 285, 289, 296, 300},
236 {0, 0, 0, 0, 131, 0, 191, 222, 242, 270, 284, 289, 293, 300, 303},
237 {0, 0, 0, 0, 200, 0, 390, 571, 741, 1067, 1365, 1505, 1640, 1894, 2053},
238 {0, 0, 0, 0, 221, 0, 430, 630, 816, 1169, 1490, 1641, 1784, 2053, 2221},
Johannes Berg8ca151b2013-01-24 14:25:36 +0100239};
240
Eyal Shapirad310e402013-08-11 18:43:47 +0300241static s32 expected_tpt_mimo2_80MHz[4][IWL_RATE_COUNT] = {
242 {0, 0, 0, 0, 182, 0, 240, 264, 278, 299, 308, 311, 313, 317, 319},
243 {0, 0, 0, 0, 190, 0, 247, 269, 282, 302, 310, 313, 315, 319, 320},
244 {0, 0, 0, 0, 428, 0, 833, 1215, 1577, 2254, 2863, 3147, 3418, 3913, 4219},
245 {0, 0, 0, 0, 474, 0, 920, 1338, 1732, 2464, 3116, 3418, 3705, 4225, 4545},
246};
247
Johannes Berg8ca151b2013-01-24 14:25:36 +0100248/* mbps, mcs */
249static const struct iwl_rate_mcs_info iwl_rate_mcs[IWL_RATE_COUNT] = {
250 { "1", "BPSK DSSS"},
251 { "2", "QPSK DSSS"},
252 {"5.5", "BPSK CCK"},
253 { "11", "QPSK CCK"},
254 { "6", "BPSK 1/2"},
255 { "9", "BPSK 1/2"},
256 { "12", "QPSK 1/2"},
257 { "18", "QPSK 3/4"},
258 { "24", "16QAM 1/2"},
259 { "36", "16QAM 3/4"},
260 { "48", "64QAM 2/3"},
261 { "54", "64QAM 3/4"},
262 { "60", "64QAM 5/6"},
263};
264
265#define MCS_INDEX_PER_STREAM (8)
266
267static void rs_rate_scale_clear_window(struct iwl_rate_scale_data *window)
268{
269 window->data = 0;
270 window->success_counter = 0;
271 window->success_ratio = IWL_INVALID_VALUE;
272 window->counter = 0;
273 window->average_tpt = IWL_INVALID_VALUE;
274 window->stamp = 0;
275}
276
277static inline u8 rs_is_valid_ant(u8 valid_antenna, u8 ant_type)
278{
279 return (ant_type & valid_antenna) == ant_type;
280}
281
Johannes Berg8ca151b2013-01-24 14:25:36 +0100282#ifdef CONFIG_MAC80211_DEBUGFS
283/**
284 * Program the device to use fixed rate for frame transmit
285 * This is for debugging/testing only
286 * once the device start use fixed rate, we need to reload the module
287 * to being back the normal operation.
288 */
289static void rs_program_fix_rate(struct iwl_mvm *mvm,
290 struct iwl_lq_sta *lq_sta)
291{
292 lq_sta->active_legacy_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
293 lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
294 lq_sta->active_mimo2_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
Johannes Berg8ca151b2013-01-24 14:25:36 +0100295
296 IWL_DEBUG_RATE(mvm, "sta_id %d rate 0x%X\n",
297 lq_sta->lq.sta_id, lq_sta->dbg_fixed_rate);
298
299 if (lq_sta->dbg_fixed_rate) {
Emmanuel Grumbach9145d152013-07-18 08:45:41 +0300300 rs_fill_link_cmd(NULL, NULL, lq_sta, lq_sta->dbg_fixed_rate);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100301 iwl_mvm_send_lq_cmd(lq_sta->drv, &lq_sta->lq, CMD_ASYNC, false);
302 }
303}
304#endif
305
Johannes Berg8ca151b2013-01-24 14:25:36 +0100306static int rs_tl_turn_on_agg_for_tid(struct iwl_mvm *mvm,
307 struct iwl_lq_sta *lq_data, u8 tid,
308 struct ieee80211_sta *sta)
309{
310 int ret = -EAGAIN;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100311
Emmanuel Grumbach44cc4292013-06-18 06:33:49 +0300312 IWL_DEBUG_HT(mvm, "Starting Tx agg: STA: %pM tid: %d\n",
313 sta->addr, tid);
314 ret = ieee80211_start_tx_ba_session(sta, tid, 5000);
315 if (ret == -EAGAIN) {
316 /*
317 * driver and mac80211 is out of sync
318 * this might be cause by reloading firmware
319 * stop the tx ba session here
320 */
321 IWL_ERR(mvm, "Fail start Tx agg on tid: %d\n",
322 tid);
323 ieee80211_stop_tx_ba_session(sta, tid);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100324 }
325 return ret;
326}
327
328static void rs_tl_turn_on_agg(struct iwl_mvm *mvm, u8 tid,
329 struct iwl_lq_sta *lq_data,
330 struct ieee80211_sta *sta)
331{
332 if (tid < IWL_MAX_TID_COUNT)
333 rs_tl_turn_on_agg_for_tid(mvm, lq_data, tid, sta);
334 else
335 IWL_ERR(mvm, "tid exceeds max TID count: %d/%d\n",
336 tid, IWL_MAX_TID_COUNT);
337}
338
339static inline int get_num_of_ant_from_rate(u32 rate_n_flags)
340{
341 return !!(rate_n_flags & RATE_MCS_ANT_A_MSK) +
342 !!(rate_n_flags & RATE_MCS_ANT_B_MSK) +
343 !!(rate_n_flags & RATE_MCS_ANT_C_MSK);
344}
345
346/*
347 * Static function to get the expected throughput from an iwl_scale_tbl_info
348 * that wraps a NULL pointer check
349 */
350static s32 get_expected_tpt(struct iwl_scale_tbl_info *tbl, int rs_index)
351{
352 if (tbl->expected_tpt)
353 return tbl->expected_tpt[rs_index];
354 return 0;
355}
356
357/**
358 * rs_collect_tx_data - Update the success/failure sliding window
359 *
360 * We keep a sliding window of the last 62 packets transmitted
361 * at this rate. window->data contains the bitmask of successful
362 * packets.
363 */
364static int rs_collect_tx_data(struct iwl_scale_tbl_info *tbl,
365 int scale_index, int attempts, int successes)
366{
367 struct iwl_rate_scale_data *window = NULL;
368 static const u64 mask = (((u64)1) << (IWL_RATE_MAX_WINDOW - 1));
369 s32 fail_count, tpt;
370
371 if (scale_index < 0 || scale_index >= IWL_RATE_COUNT)
372 return -EINVAL;
373
374 /* Select window for current tx bit rate */
375 window = &(tbl->win[scale_index]);
376
377 /* Get expected throughput */
378 tpt = get_expected_tpt(tbl, scale_index);
379
380 /*
381 * Keep track of only the latest 62 tx frame attempts in this rate's
382 * history window; anything older isn't really relevant any more.
383 * If we have filled up the sliding window, drop the oldest attempt;
384 * if the oldest attempt (highest bit in bitmap) shows "success",
385 * subtract "1" from the success counter (this is the main reason
386 * we keep these bitmaps!).
387 */
388 while (attempts > 0) {
389 if (window->counter >= IWL_RATE_MAX_WINDOW) {
390 /* remove earliest */
391 window->counter = IWL_RATE_MAX_WINDOW - 1;
392
393 if (window->data & mask) {
394 window->data &= ~mask;
395 window->success_counter--;
396 }
397 }
398
399 /* Increment frames-attempted counter */
400 window->counter++;
401
402 /* Shift bitmap by one frame to throw away oldest history */
403 window->data <<= 1;
404
405 /* Mark the most recent #successes attempts as successful */
406 if (successes > 0) {
407 window->success_counter++;
408 window->data |= 0x1;
409 successes--;
410 }
411
412 attempts--;
413 }
414
415 /* Calculate current success ratio, avoid divide-by-0! */
416 if (window->counter > 0)
417 window->success_ratio = 128 * (100 * window->success_counter)
418 / window->counter;
419 else
420 window->success_ratio = IWL_INVALID_VALUE;
421
422 fail_count = window->counter - window->success_counter;
423
424 /* Calculate average throughput, if we have enough history. */
425 if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
426 (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
427 window->average_tpt = (window->success_ratio * tpt + 64) / 128;
428 else
429 window->average_tpt = IWL_INVALID_VALUE;
430
431 /* Tag this window as having been updated */
432 window->stamp = jiffies;
433
434 return 0;
435}
436
437/*
438 * Fill uCode API rate_n_flags field, based on "search" or "active" table.
439 */
440/* FIXME:RS:remove this function and put the flags statically in the table */
441static u32 rate_n_flags_from_tbl(struct iwl_mvm *mvm,
Eyal Shapira6a524f42013-08-11 20:27:18 +0300442 struct iwl_scale_tbl_info *tbl, int index)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100443{
444 u32 rate_n_flags = 0;
445
Eyal Shapirad310e402013-08-11 18:43:47 +0300446 rate_n_flags |= ((tbl->ant_type << RATE_MCS_ANT_POS) &
447 RATE_MCS_ANT_ABC_MSK);
448
Johannes Berg8ca151b2013-01-24 14:25:36 +0100449 if (is_legacy(tbl->lq_type)) {
Eyal Shapirad310e402013-08-11 18:43:47 +0300450 rate_n_flags |= iwl_rates[index].plcp;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100451 if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
452 rate_n_flags |= RATE_MCS_CCK_MSK;
Eyal Shapirad310e402013-08-11 18:43:47 +0300453 return rate_n_flags;
454 }
455
456 if (is_ht(tbl->lq_type)) {
457 if (index < IWL_FIRST_HT_RATE || index > IWL_LAST_HT_RATE) {
Johannes Berg8ca151b2013-01-24 14:25:36 +0100458 IWL_ERR(mvm, "Invalid HT rate index %d\n", index);
Eyal Shapira4e82dd32013-08-04 23:58:37 +0300459 index = IWL_LAST_HT_RATE;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100460 }
Eyal Shapirad310e402013-08-11 18:43:47 +0300461 rate_n_flags |= RATE_MCS_HT_MSK;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100462
Eyal Shapirad310e402013-08-11 18:43:47 +0300463 if (is_ht_siso(tbl->lq_type))
464 rate_n_flags |= iwl_rates[index].plcp_ht_siso;
465 else if (is_ht_mimo2(tbl->lq_type))
466 rate_n_flags |= iwl_rates[index].plcp_ht_mimo2;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100467 else
Eyal Shapirad972ab32013-07-28 23:02:45 +0000468 WARN_ON_ONCE(1);
Eyal Shapirad310e402013-08-11 18:43:47 +0300469 } else if (is_vht(tbl->lq_type)) {
470 if (index < IWL_FIRST_VHT_RATE || index > IWL_LAST_VHT_RATE) {
471 IWL_ERR(mvm, "Invalid VHT rate index %d\n", index);
472 index = IWL_LAST_VHT_RATE;
473 }
474 rate_n_flags |= RATE_MCS_VHT_MSK;
475 if (is_vht_siso(tbl->lq_type))
476 rate_n_flags |= iwl_rates[index].plcp_vht_siso;
477 else if (is_vht_mimo2(tbl->lq_type))
478 rate_n_flags |= iwl_rates[index].plcp_vht_mimo2;
479 else
480 WARN_ON_ONCE(1);
481
Johannes Berg8ca151b2013-01-24 14:25:36 +0100482 } else {
483 IWL_ERR(mvm, "Invalid tbl->lq_type %d\n", tbl->lq_type);
484 }
485
Eyal Shapirad310e402013-08-11 18:43:47 +0300486 rate_n_flags |= tbl->bw;
487 if (tbl->is_SGI)
488 rate_n_flags |= RATE_MCS_SGI_MSK;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100489
Johannes Berg8ca151b2013-01-24 14:25:36 +0100490 return rate_n_flags;
491}
492
493/*
494 * Interpret uCode API's rate_n_flags format,
495 * fill "search" or "active" tx mode table.
496 */
497static int rs_get_tbl_info_from_mcs(const u32 rate_n_flags,
498 enum ieee80211_band band,
499 struct iwl_scale_tbl_info *tbl,
500 int *rate_idx)
501{
502 u32 ant_msk = (rate_n_flags & RATE_MCS_ANT_ABC_MSK);
503 u8 num_of_ant = get_num_of_ant_from_rate(rate_n_flags);
Eyal Shapirad310e402013-08-11 18:43:47 +0300504 u8 nss;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100505
Eliad Pellerfaec6f92013-07-17 09:16:30 +0300506 memset(tbl, 0, offsetof(struct iwl_scale_tbl_info, win));
Johannes Berg8ca151b2013-01-24 14:25:36 +0100507 *rate_idx = iwl_hwrate_to_plcp_idx(rate_n_flags);
508
509 if (*rate_idx == IWL_RATE_INVALID) {
510 *rate_idx = -1;
511 return -EINVAL;
512 }
513 tbl->is_SGI = 0; /* default legacy setup */
Eyal Shapirad310e402013-08-11 18:43:47 +0300514 tbl->bw = 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100515 tbl->ant_type = (ant_msk >> RATE_MCS_ANT_POS);
516 tbl->lq_type = LQ_NONE;
517 tbl->max_search = IWL_MAX_SEARCH;
518
Eyal Shapirad310e402013-08-11 18:43:47 +0300519 /* Legacy */
520 if (!(rate_n_flags & RATE_MCS_HT_MSK) &&
521 !(rate_n_flags & RATE_MCS_VHT_MSK)) {
Johannes Berg8ca151b2013-01-24 14:25:36 +0100522 if (num_of_ant == 1) {
523 if (band == IEEE80211_BAND_5GHZ)
Eyal Shapirad310e402013-08-11 18:43:47 +0300524 tbl->lq_type = LQ_LEGACY_A;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100525 else
Eyal Shapirad310e402013-08-11 18:43:47 +0300526 tbl->lq_type = LQ_LEGACY_G;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100527 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100528
Eyal Shapirad310e402013-08-11 18:43:47 +0300529 return 0;
530 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100531
Eyal Shapirad310e402013-08-11 18:43:47 +0300532 /* HT or VHT */
533 if (rate_n_flags & RATE_MCS_SGI_MSK)
534 tbl->is_SGI = 1;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100535
Eyal Shapirad310e402013-08-11 18:43:47 +0300536 tbl->bw = rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK;
537
538 if (rate_n_flags & RATE_MCS_HT_MSK) {
539 nss = ((rate_n_flags & RATE_HT_MCS_NSS_MSK) >>
540 RATE_HT_MCS_NSS_POS) + 1;
541
542 if (nss == 1) {
543 tbl->lq_type = LQ_HT_SISO;
544 WARN_ON_ONCE(num_of_ant != 1);
545 } else if (nss == 2) {
546 tbl->lq_type = LQ_HT_MIMO2;
547 WARN_ON_ONCE(num_of_ant != 2);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100548 } else {
Eyal Shapirad310e402013-08-11 18:43:47 +0300549 WARN_ON_ONCE(1);
550 }
551 } else if (rate_n_flags & RATE_MCS_VHT_MSK) {
552 nss = ((rate_n_flags & RATE_VHT_MCS_NSS_MSK) >>
553 RATE_VHT_MCS_NSS_POS) + 1;
554
555 if (nss == 1) {
556 tbl->lq_type = LQ_VHT_SISO;
557 WARN_ON_ONCE(num_of_ant != 1);
558 } else if (nss == 2) {
559 tbl->lq_type = LQ_VHT_MIMO2;
560 WARN_ON_ONCE(num_of_ant != 2);
561 } else {
562 WARN_ON_ONCE(1);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100563 }
564 }
Eyal Shapirad310e402013-08-11 18:43:47 +0300565
566 WARN_ON_ONCE(tbl->bw == RATE_MCS_CHAN_WIDTH_160);
567 WARN_ON_ONCE(tbl->bw == RATE_MCS_CHAN_WIDTH_80 &&
568 !is_vht(tbl->lq_type));
569
Johannes Berg8ca151b2013-01-24 14:25:36 +0100570 return 0;
571}
572
573/* switch to another antenna/antennas and return 1 */
574/* if no other valid antenna found, return 0 */
575static int rs_toggle_antenna(u32 valid_ant, u32 *rate_n_flags,
576 struct iwl_scale_tbl_info *tbl)
577{
578 u8 new_ant_type;
579
580 if (!tbl->ant_type || tbl->ant_type > ANT_ABC)
581 return 0;
582
583 if (!rs_is_valid_ant(valid_ant, tbl->ant_type))
584 return 0;
585
586 new_ant_type = ant_toggle_lookup[tbl->ant_type];
587
588 while ((new_ant_type != tbl->ant_type) &&
589 !rs_is_valid_ant(valid_ant, new_ant_type))
590 new_ant_type = ant_toggle_lookup[new_ant_type];
591
592 if (new_ant_type == tbl->ant_type)
593 return 0;
594
595 tbl->ant_type = new_ant_type;
596 *rate_n_flags &= ~RATE_MCS_ANT_ABC_MSK;
597 *rate_n_flags |= new_ant_type << RATE_MCS_ANT_POS;
598 return 1;
599}
600
601/**
Johannes Berg8ca151b2013-01-24 14:25:36 +0100602 * rs_get_supported_rates - get the available rates
603 *
604 * if management frame or broadcast frame only return
605 * basic available rates.
606 *
607 */
608static u16 rs_get_supported_rates(struct iwl_lq_sta *lq_sta,
609 struct ieee80211_hdr *hdr,
610 enum iwl_table_type rate_type)
611{
Eyal Shapirad310e402013-08-11 18:43:47 +0300612 if (is_legacy(rate_type))
Johannes Berg8ca151b2013-01-24 14:25:36 +0100613 return lq_sta->active_legacy_rate;
Eyal Shapirad310e402013-08-11 18:43:47 +0300614 else if (is_siso(rate_type))
615 return lq_sta->active_siso_rate;
616 else if (is_mimo2(rate_type))
617 return lq_sta->active_mimo2_rate;
618
619 WARN_ON_ONCE(1);
620 return 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100621}
622
623static u16 rs_get_adjacent_rate(struct iwl_mvm *mvm, u8 index, u16 rate_mask,
624 int rate_type)
625{
626 u8 high = IWL_RATE_INVALID;
627 u8 low = IWL_RATE_INVALID;
628
629 /* 802.11A or ht walks to the next literal adjacent rate in
630 * the rate table */
631 if (is_a_band(rate_type) || !is_legacy(rate_type)) {
632 int i;
633 u32 mask;
634
635 /* Find the previous rate that is in the rate mask */
636 i = index - 1;
637 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
638 if (rate_mask & mask) {
639 low = i;
640 break;
641 }
642 }
643
644 /* Find the next rate that is in the rate mask */
645 i = index + 1;
646 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
647 if (rate_mask & mask) {
648 high = i;
649 break;
650 }
651 }
652
653 return (high << 8) | low;
654 }
655
656 low = index;
657 while (low != IWL_RATE_INVALID) {
658 low = iwl_rates[low].prev_rs;
659 if (low == IWL_RATE_INVALID)
660 break;
661 if (rate_mask & (1 << low))
662 break;
663 IWL_DEBUG_RATE(mvm, "Skipping masked lower rate: %d\n", low);
664 }
665
666 high = index;
667 while (high != IWL_RATE_INVALID) {
668 high = iwl_rates[high].next_rs;
669 if (high == IWL_RATE_INVALID)
670 break;
671 if (rate_mask & (1 << high))
672 break;
673 IWL_DEBUG_RATE(mvm, "Skipping masked higher rate: %d\n", high);
674 }
675
676 return (high << 8) | low;
677}
678
679static u32 rs_get_lower_rate(struct iwl_lq_sta *lq_sta,
680 struct iwl_scale_tbl_info *tbl,
681 u8 scale_index, u8 ht_possible)
682{
683 s32 low;
684 u16 rate_mask;
685 u16 high_low;
686 u8 switch_to_legacy = 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100687 struct iwl_mvm *mvm = lq_sta->drv;
688
689 /* check if we need to switch from HT to legacy rates.
690 * assumption is that mandatory rates (1Mbps or 6Mbps)
691 * are always supported (spec demand) */
692 if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_index)) {
693 switch_to_legacy = 1;
694 scale_index = rs_ht_to_legacy[scale_index];
695 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Eyal Shapirad310e402013-08-11 18:43:47 +0300696 tbl->lq_type = LQ_LEGACY_A;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100697 else
Eyal Shapirad310e402013-08-11 18:43:47 +0300698 tbl->lq_type = LQ_LEGACY_G;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100699
700 if (num_of_ant(tbl->ant_type) > 1)
701 tbl->ant_type =
Emmanuel Grumbachff402312013-04-03 20:10:06 +0300702 first_antenna(iwl_fw_valid_tx_ant(mvm->fw));
Johannes Berg8ca151b2013-01-24 14:25:36 +0100703
Eyal Shapirad310e402013-08-11 18:43:47 +0300704 tbl->bw = 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100705 tbl->is_SGI = 0;
706 tbl->max_search = IWL_MAX_SEARCH;
707 }
708
709 rate_mask = rs_get_supported_rates(lq_sta, NULL, tbl->lq_type);
710
711 /* Mask with station rate restriction */
712 if (is_legacy(tbl->lq_type)) {
713 /* supp_rates has no CCK bits in A mode */
714 if (lq_sta->band == IEEE80211_BAND_5GHZ)
715 rate_mask = (u16)(rate_mask &
716 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
717 else
718 rate_mask = (u16)(rate_mask & lq_sta->supp_rates);
719 }
720
721 /* If we switched from HT to legacy, check current rate */
722 if (switch_to_legacy && (rate_mask & (1 << scale_index))) {
723 low = scale_index;
724 goto out;
725 }
726
727 high_low = rs_get_adjacent_rate(lq_sta->drv, scale_index, rate_mask,
728 tbl->lq_type);
729 low = high_low & 0xff;
730
731 if (low == IWL_RATE_INVALID)
732 low = scale_index;
733
734out:
Eyal Shapira6a524f42013-08-11 20:27:18 +0300735 return rate_n_flags_from_tbl(lq_sta->drv, tbl, low);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100736}
737
738/*
739 * Simple function to compare two rate scale table types
740 */
741static bool table_type_matches(struct iwl_scale_tbl_info *a,
742 struct iwl_scale_tbl_info *b)
743{
744 return (a->lq_type == b->lq_type) && (a->ant_type == b->ant_type) &&
745 (a->is_SGI == b->is_SGI);
746}
747
Eyal Shapirad310e402013-08-11 18:43:47 +0300748static u32 rs_ch_width_from_mac_flags(enum mac80211_rate_control_flags flags)
749{
750 if (flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
751 return RATE_MCS_CHAN_WIDTH_40;
752 else if (flags & IEEE80211_TX_RC_80_MHZ_WIDTH)
753 return RATE_MCS_CHAN_WIDTH_80;
754 else if (flags & IEEE80211_TX_RC_160_MHZ_WIDTH)
755 return RATE_MCS_CHAN_WIDTH_160;
756
757 return RATE_MCS_CHAN_WIDTH_20;
758}
759
Johannes Berg8ca151b2013-01-24 14:25:36 +0100760/*
761 * mac80211 sends us Tx status
762 */
763static void rs_tx_status(void *mvm_r, struct ieee80211_supported_band *sband,
764 struct ieee80211_sta *sta, void *priv_sta,
765 struct sk_buff *skb)
766{
767 int legacy_success;
768 int retries;
769 int rs_index, mac_index, i;
770 struct iwl_lq_sta *lq_sta = priv_sta;
771 struct iwl_lq_cmd *table;
772 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
773 struct iwl_op_mode *op_mode = (struct iwl_op_mode *)mvm_r;
774 struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
775 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
776 enum mac80211_rate_control_flags mac_flags;
777 u32 tx_rate;
778 struct iwl_scale_tbl_info tbl_type;
779 struct iwl_scale_tbl_info *curr_tbl, *other_tbl, *tmp_tbl;
780
781 IWL_DEBUG_RATE_LIMIT(mvm,
782 "get frame ack response, update rate scale window\n");
783
784 /* Treat uninitialized rate scaling data same as non-existing. */
785 if (!lq_sta) {
786 IWL_DEBUG_RATE(mvm, "Station rate scaling not created yet.\n");
787 return;
788 } else if (!lq_sta->drv) {
789 IWL_DEBUG_RATE(mvm, "Rate scaling not initialized yet.\n");
790 return;
791 }
792
793 if (!ieee80211_is_data(hdr->frame_control) ||
794 info->flags & IEEE80211_TX_CTL_NO_ACK)
795 return;
796
797 /* This packet was aggregated but doesn't carry status info */
798 if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
799 !(info->flags & IEEE80211_TX_STAT_AMPDU))
800 return;
801
802 /*
803 * Ignore this Tx frame response if its initial rate doesn't match
804 * that of latest Link Quality command. There may be stragglers
805 * from a previous Link Quality command, but we're no longer interested
806 * in those; they're either from the "active" mode while we're trying
807 * to check "search" mode, or a prior "search" mode after we've moved
808 * to a new "search" mode (which might become the new "active" mode).
809 */
810 table = &lq_sta->lq;
811 tx_rate = le32_to_cpu(table->rs_table[0]);
812 rs_get_tbl_info_from_mcs(tx_rate, info->band, &tbl_type, &rs_index);
813 if (info->band == IEEE80211_BAND_5GHZ)
814 rs_index -= IWL_FIRST_OFDM_RATE;
815 mac_flags = info->status.rates[0].flags;
816 mac_index = info->status.rates[0].idx;
817 /* For HT packets, map MCS to PLCP */
818 if (mac_flags & IEEE80211_TX_RC_MCS) {
819 /* Remove # of streams */
820 mac_index &= RATE_HT_MCS_RATE_CODE_MSK;
821 if (mac_index >= (IWL_RATE_9M_INDEX - IWL_FIRST_OFDM_RATE))
822 mac_index++;
823 /*
824 * mac80211 HT index is always zero-indexed; we need to move
825 * HT OFDM rates after CCK rates in 2.4 GHz band
826 */
827 if (info->band == IEEE80211_BAND_2GHZ)
828 mac_index += IWL_FIRST_OFDM_RATE;
Eyal Shapirad310e402013-08-11 18:43:47 +0300829 } else if (mac_flags & IEEE80211_TX_RC_VHT_MCS) {
830 mac_index &= RATE_VHT_MCS_RATE_CODE_MSK;
831 if (mac_index >= (IWL_RATE_9M_INDEX - IWL_FIRST_OFDM_RATE))
832 mac_index++;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100833 }
Eyal Shapirad310e402013-08-11 18:43:47 +0300834
Johannes Berg8ca151b2013-01-24 14:25:36 +0100835 /* Here we actually compare this rate to the latest LQ command */
836 if ((mac_index < 0) ||
837 (tbl_type.is_SGI != !!(mac_flags & IEEE80211_TX_RC_SHORT_GI)) ||
Eyal Shapirad310e402013-08-11 18:43:47 +0300838 (tbl_type.bw != rs_ch_width_from_mac_flags(mac_flags)) ||
Johannes Berg8ca151b2013-01-24 14:25:36 +0100839 (tbl_type.ant_type != info->status.antenna) ||
840 (!!(tx_rate & RATE_MCS_HT_MSK) !=
Eyal Shapirad310e402013-08-11 18:43:47 +0300841 !!(mac_flags & IEEE80211_TX_RC_MCS)) ||
842 (!!(tx_rate & RATE_MCS_VHT_MSK) !=
843 !!(mac_flags & IEEE80211_TX_RC_VHT_MCS)) ||
Johannes Berg8ca151b2013-01-24 14:25:36 +0100844 (!!(tx_rate & RATE_HT_MCS_GF_MSK) !=
Eyal Shapirad310e402013-08-11 18:43:47 +0300845 !!(mac_flags & IEEE80211_TX_RC_GREEN_FIELD)) ||
Johannes Berg8ca151b2013-01-24 14:25:36 +0100846 (rs_index != mac_index)) {
847 IWL_DEBUG_RATE(mvm,
848 "initial rate %d does not match %d (0x%x)\n",
849 mac_index, rs_index, tx_rate);
850 /*
851 * Since rates mis-match, the last LQ command may have failed.
852 * After IWL_MISSED_RATE_MAX mis-matches, resync the uCode with
853 * ... driver.
854 */
855 lq_sta->missed_rate_counter++;
856 if (lq_sta->missed_rate_counter > IWL_MISSED_RATE_MAX) {
857 lq_sta->missed_rate_counter = 0;
858 iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, CMD_ASYNC, false);
859 }
860 /* Regardless, ignore this status info for outdated rate */
861 return;
862 } else
863 /* Rate did match, so reset the missed_rate_counter */
864 lq_sta->missed_rate_counter = 0;
865
866 /* Figure out if rate scale algorithm is in active or search table */
867 if (table_type_matches(&tbl_type,
868 &(lq_sta->lq_info[lq_sta->active_tbl]))) {
869 curr_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
870 other_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
871 } else if (table_type_matches(
872 &tbl_type, &lq_sta->lq_info[1 - lq_sta->active_tbl])) {
873 curr_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
874 other_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
875 } else {
876 IWL_DEBUG_RATE(mvm,
877 "Neither active nor search matches tx rate\n");
878 tmp_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
879 IWL_DEBUG_RATE(mvm, "active- lq:%x, ant:%x, SGI:%d\n",
880 tmp_tbl->lq_type, tmp_tbl->ant_type,
881 tmp_tbl->is_SGI);
882 tmp_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
883 IWL_DEBUG_RATE(mvm, "search- lq:%x, ant:%x, SGI:%d\n",
884 tmp_tbl->lq_type, tmp_tbl->ant_type,
885 tmp_tbl->is_SGI);
886 IWL_DEBUG_RATE(mvm, "actual- lq:%x, ant:%x, SGI:%d\n",
887 tbl_type.lq_type, tbl_type.ant_type,
888 tbl_type.is_SGI);
889 /*
890 * no matching table found, let's by-pass the data collection
891 * and continue to perform rate scale to find the rate table
892 */
893 rs_stay_in_table(lq_sta, true);
894 goto done;
895 }
896
897 /*
898 * Updating the frame history depends on whether packets were
899 * aggregated.
900 *
901 * For aggregation, all packets were transmitted at the same rate, the
902 * first index into rate scale table.
903 */
904 if (info->flags & IEEE80211_TX_STAT_AMPDU) {
905 tx_rate = le32_to_cpu(table->rs_table[0]);
906 rs_get_tbl_info_from_mcs(tx_rate, info->band, &tbl_type,
907 &rs_index);
908 rs_collect_tx_data(curr_tbl, rs_index,
909 info->status.ampdu_len,
910 info->status.ampdu_ack_len);
911
912 /* Update success/fail counts if not searching for new mode */
913 if (lq_sta->stay_in_tbl) {
914 lq_sta->total_success += info->status.ampdu_ack_len;
915 lq_sta->total_failed += (info->status.ampdu_len -
916 info->status.ampdu_ack_len);
917 }
918 } else {
919 /*
920 * For legacy, update frame history with for each Tx retry.
921 */
922 retries = info->status.rates[0].count - 1;
923 /* HW doesn't send more than 15 retries */
924 retries = min(retries, 15);
925
926 /* The last transmission may have been successful */
927 legacy_success = !!(info->flags & IEEE80211_TX_STAT_ACK);
928 /* Collect data for each rate used during failed TX attempts */
929 for (i = 0; i <= retries; ++i) {
930 tx_rate = le32_to_cpu(table->rs_table[i]);
931 rs_get_tbl_info_from_mcs(tx_rate, info->band,
932 &tbl_type, &rs_index);
933 /*
934 * Only collect stats if retried rate is in the same RS
935 * table as active/search.
936 */
937 if (table_type_matches(&tbl_type, curr_tbl))
938 tmp_tbl = curr_tbl;
939 else if (table_type_matches(&tbl_type, other_tbl))
940 tmp_tbl = other_tbl;
941 else
942 continue;
943 rs_collect_tx_data(tmp_tbl, rs_index, 1,
944 i < retries ? 0 : legacy_success);
945 }
946
947 /* Update success/fail counts if not searching for new mode */
948 if (lq_sta->stay_in_tbl) {
949 lq_sta->total_success += legacy_success;
950 lq_sta->total_failed += retries + (1 - legacy_success);
951 }
952 }
953 /* The last TX rate is cached in lq_sta; it's set in if/else above */
954 lq_sta->last_rate_n_flags = tx_rate;
955done:
956 /* See if there's a better rate or modulation mode to try. */
957 if (sta && sta->supp_rates[sband->band])
958 rs_rate_scale_perform(mvm, skb, sta, lq_sta);
959}
960
961/*
962 * Begin a period of staying with a selected modulation mode.
963 * Set "stay_in_tbl" flag to prevent any mode switches.
964 * Set frame tx success limits according to legacy vs. high-throughput,
965 * and reset overall (spanning all rates) tx success history statistics.
966 * These control how long we stay using same modulation mode before
967 * searching for a new mode.
968 */
969static void rs_set_stay_in_table(struct iwl_mvm *mvm, u8 is_legacy,
970 struct iwl_lq_sta *lq_sta)
971{
972 IWL_DEBUG_RATE(mvm, "we are staying in the same table\n");
973 lq_sta->stay_in_tbl = 1; /* only place this gets set */
974 if (is_legacy) {
975 lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT;
976 lq_sta->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
977 lq_sta->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
978 } else {
979 lq_sta->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
980 lq_sta->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
981 lq_sta->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
982 }
983 lq_sta->table_count = 0;
984 lq_sta->total_failed = 0;
985 lq_sta->total_success = 0;
986 lq_sta->flush_timer = jiffies;
987 lq_sta->action_counter = 0;
988}
989
990/*
991 * Find correct throughput table for given mode of modulation
992 */
993static void rs_set_expected_tpt_table(struct iwl_lq_sta *lq_sta,
994 struct iwl_scale_tbl_info *tbl)
995{
996 /* Used to choose among HT tables */
997 s32 (*ht_tbl_pointer)[IWL_RATE_COUNT];
998
999 /* Check for invalid LQ type */
Eyal Shapirad310e402013-08-11 18:43:47 +03001000 if (WARN_ON_ONCE(!is_legacy(tbl->lq_type) && !is_ht(tbl->lq_type) &&
1001 !(is_vht(tbl->lq_type)))) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001002 tbl->expected_tpt = expected_tpt_legacy;
1003 return;
1004 }
1005
1006 /* Legacy rates have only one table */
1007 if (is_legacy(tbl->lq_type)) {
1008 tbl->expected_tpt = expected_tpt_legacy;
1009 return;
1010 }
1011
Eyal Shapirad310e402013-08-11 18:43:47 +03001012 ht_tbl_pointer = expected_tpt_mimo2_20MHz;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001013 /* Choose among many HT tables depending on number of streams
Eyal Shapirad310e402013-08-11 18:43:47 +03001014 * (SISO/MIMO2), channel width (20/40/80), SGI, and aggregation
Johannes Berg8ca151b2013-01-24 14:25:36 +01001015 * status */
Eyal Shapirad310e402013-08-11 18:43:47 +03001016 if (is_siso(tbl->lq_type)) {
1017 switch (tbl->bw) {
1018 case RATE_MCS_CHAN_WIDTH_20:
1019 ht_tbl_pointer = expected_tpt_siso_20MHz;
1020 break;
1021 case RATE_MCS_CHAN_WIDTH_40:
1022 ht_tbl_pointer = expected_tpt_siso_40MHz;
1023 break;
1024 case RATE_MCS_CHAN_WIDTH_80:
1025 ht_tbl_pointer = expected_tpt_siso_80MHz;
1026 break;
1027 default:
1028 WARN_ON_ONCE(1);
1029 }
1030 } else if (is_mimo2(tbl->lq_type)) {
1031 switch (tbl->bw) {
1032 case RATE_MCS_CHAN_WIDTH_20:
1033 ht_tbl_pointer = expected_tpt_mimo2_20MHz;
1034 break;
1035 case RATE_MCS_CHAN_WIDTH_40:
1036 ht_tbl_pointer = expected_tpt_mimo2_40MHz;
1037 break;
1038 case RATE_MCS_CHAN_WIDTH_80:
1039 ht_tbl_pointer = expected_tpt_mimo2_80MHz;
1040 break;
1041 default:
1042 WARN_ON_ONCE(1);
1043 }
1044 } else {
1045 WARN_ON_ONCE(1);
Eyal Shapirad972ab32013-07-28 23:02:45 +00001046 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01001047
1048 if (!tbl->is_SGI && !lq_sta->is_agg) /* Normal */
1049 tbl->expected_tpt = ht_tbl_pointer[0];
1050 else if (tbl->is_SGI && !lq_sta->is_agg) /* SGI */
1051 tbl->expected_tpt = ht_tbl_pointer[1];
1052 else if (!tbl->is_SGI && lq_sta->is_agg) /* AGG */
1053 tbl->expected_tpt = ht_tbl_pointer[2];
1054 else /* AGG+SGI */
1055 tbl->expected_tpt = ht_tbl_pointer[3];
1056}
1057
1058/*
1059 * Find starting rate for new "search" high-throughput mode of modulation.
1060 * Goal is to find lowest expected rate (under perfect conditions) that is
1061 * above the current measured throughput of "active" mode, to give new mode
1062 * a fair chance to prove itself without too many challenges.
1063 *
1064 * This gets called when transitioning to more aggressive modulation
1065 * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
1066 * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
1067 * to decrease to match "active" throughput. When moving from MIMO to SISO,
1068 * bit rate will typically need to increase, but not if performance was bad.
1069 */
1070static s32 rs_get_best_rate(struct iwl_mvm *mvm,
1071 struct iwl_lq_sta *lq_sta,
1072 struct iwl_scale_tbl_info *tbl, /* "search" */
1073 u16 rate_mask, s8 index)
1074{
1075 /* "active" values */
1076 struct iwl_scale_tbl_info *active_tbl =
1077 &(lq_sta->lq_info[lq_sta->active_tbl]);
1078 s32 active_sr = active_tbl->win[index].success_ratio;
1079 s32 active_tpt = active_tbl->expected_tpt[index];
1080
1081 /* expected "search" throughput */
1082 s32 *tpt_tbl = tbl->expected_tpt;
1083
1084 s32 new_rate, high, low, start_hi;
1085 u16 high_low;
1086 s8 rate = index;
1087
1088 new_rate = high = low = start_hi = IWL_RATE_INVALID;
1089
1090 while (1) {
1091 high_low = rs_get_adjacent_rate(mvm, rate, rate_mask,
1092 tbl->lq_type);
1093
1094 low = high_low & 0xff;
1095 high = (high_low >> 8) & 0xff;
1096
1097 /*
1098 * Lower the "search" bit rate, to give new "search" mode
1099 * approximately the same throughput as "active" if:
1100 *
1101 * 1) "Active" mode has been working modestly well (but not
1102 * great), and expected "search" throughput (under perfect
1103 * conditions) at candidate rate is above the actual
1104 * measured "active" throughput (but less than expected
1105 * "active" throughput under perfect conditions).
1106 * OR
1107 * 2) "Active" mode has been working perfectly or very well
1108 * and expected "search" throughput (under perfect
1109 * conditions) at candidate rate is above expected
1110 * "active" throughput (under perfect conditions).
1111 */
1112 if ((((100 * tpt_tbl[rate]) > lq_sta->last_tpt) &&
1113 ((active_sr > IWL_RATE_DECREASE_TH) &&
1114 (active_sr <= IWL_RATE_HIGH_TH) &&
1115 (tpt_tbl[rate] <= active_tpt))) ||
1116 ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
1117 (tpt_tbl[rate] > active_tpt))) {
1118 /* (2nd or later pass)
1119 * If we've already tried to raise the rate, and are
1120 * now trying to lower it, use the higher rate. */
1121 if (start_hi != IWL_RATE_INVALID) {
1122 new_rate = start_hi;
1123 break;
1124 }
1125
1126 new_rate = rate;
1127
1128 /* Loop again with lower rate */
1129 if (low != IWL_RATE_INVALID)
1130 rate = low;
1131
1132 /* Lower rate not available, use the original */
1133 else
1134 break;
1135
1136 /* Else try to raise the "search" rate to match "active" */
1137 } else {
1138 /* (2nd or later pass)
1139 * If we've already tried to lower the rate, and are
1140 * now trying to raise it, use the lower rate. */
1141 if (new_rate != IWL_RATE_INVALID)
1142 break;
1143
1144 /* Loop again with higher rate */
1145 else if (high != IWL_RATE_INVALID) {
1146 start_hi = high;
1147 rate = high;
1148
1149 /* Higher rate not available, use the original */
1150 } else {
1151 new_rate = rate;
1152 break;
1153 }
1154 }
1155 }
1156
1157 return new_rate;
1158}
1159
Eyal Shapira4837b442013-07-28 23:02:46 +00001160/* Move to the next action and wrap around to the first action in case
1161 * we're at the last action. Assumes actions start at 0.
1162 */
1163static inline void rs_move_next_action(struct iwl_scale_tbl_info *tbl,
1164 u8 last_action)
1165{
1166 BUILD_BUG_ON(IWL_LEGACY_FIRST_ACTION != 0);
1167 BUILD_BUG_ON(IWL_SISO_FIRST_ACTION != 0);
1168 BUILD_BUG_ON(IWL_MIMO2_FIRST_ACTION != 0);
1169
1170 tbl->action = (tbl->action + 1) % (last_action + 1);
1171}
1172
Eyal Shapirad310e402013-08-11 18:43:47 +03001173static void rs_set_bw_from_sta(struct iwl_scale_tbl_info *tbl,
1174 struct ieee80211_sta *sta)
1175{
1176 if (sta->bandwidth >= IEEE80211_STA_RX_BW_80)
1177 tbl->bw = RATE_MCS_CHAN_WIDTH_80;
1178 else if (sta->bandwidth >= IEEE80211_STA_RX_BW_40)
1179 tbl->bw = RATE_MCS_CHAN_WIDTH_40;
1180 else
1181 tbl->bw = RATE_MCS_CHAN_WIDTH_20;
1182}
1183
1184static bool rs_sgi_allowed(struct iwl_scale_tbl_info *tbl,
1185 struct ieee80211_sta *sta)
1186{
1187 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
1188 struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
1189
1190 if (is_ht20(tbl) && (ht_cap->cap &
1191 IEEE80211_HT_CAP_SGI_20))
1192 return true;
1193 if (is_ht40(tbl) && (ht_cap->cap &
1194 IEEE80211_HT_CAP_SGI_40))
1195 return true;
1196 if (is_ht80(tbl) && (vht_cap->cap &
1197 IEEE80211_VHT_CAP_SHORT_GI_80))
1198 return true;
1199
1200 return false;
1201}
1202
Johannes Berg8ca151b2013-01-24 14:25:36 +01001203/*
1204 * Set up search table for MIMO2
1205 */
1206static int rs_switch_to_mimo2(struct iwl_mvm *mvm,
1207 struct iwl_lq_sta *lq_sta,
1208 struct ieee80211_sta *sta,
1209 struct iwl_scale_tbl_info *tbl, int index)
1210{
1211 u16 rate_mask;
1212 s32 rate;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001213
1214 if (!sta->ht_cap.ht_supported)
1215 return -1;
1216
Johannes Bergaf0ed692013-02-12 14:21:00 +01001217 if (sta->smps_mode == IEEE80211_SMPS_STATIC)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001218 return -1;
1219
1220 /* Need both Tx chains/antennas to support MIMO */
Emmanuel Grumbachff402312013-04-03 20:10:06 +03001221 if (num_of_ant(iwl_fw_valid_tx_ant(mvm->fw)) < 2)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001222 return -1;
1223
1224 IWL_DEBUG_RATE(mvm, "LQ: try to switch to MIMO2\n");
1225
Eyal Shapirad310e402013-08-11 18:43:47 +03001226 tbl->lq_type = lq_sta->is_vht ? LQ_VHT_MIMO2 : LQ_HT_MIMO2;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001227 tbl->action = 0;
1228 tbl->max_search = IWL_MAX_SEARCH;
1229 rate_mask = lq_sta->active_mimo2_rate;
1230
Eyal Shapirad310e402013-08-11 18:43:47 +03001231 rs_set_bw_from_sta(tbl, sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001232 rs_set_expected_tpt_table(lq_sta, tbl);
1233
1234 rate = rs_get_best_rate(mvm, lq_sta, tbl, rate_mask, index);
1235
1236 IWL_DEBUG_RATE(mvm, "LQ: MIMO2 best rate %d mask %X\n",
1237 rate, rate_mask);
1238 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1239 IWL_DEBUG_RATE(mvm, "Can't switch with index %d rate mask %x\n",
1240 rate, rate_mask);
1241 return -1;
1242 }
Eyal Shapira6a524f42013-08-11 20:27:18 +03001243 tbl->current_rate = rate_n_flags_from_tbl(mvm, tbl, rate);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001244
Eyal Shapira6a524f42013-08-11 20:27:18 +03001245 IWL_DEBUG_RATE(mvm, "LQ: Switch to new mcs %X index\n",
1246 tbl->current_rate);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001247 return 0;
1248}
1249
1250/*
Johannes Berg8ca151b2013-01-24 14:25:36 +01001251 * Set up search table for SISO
1252 */
1253static int rs_switch_to_siso(struct iwl_mvm *mvm,
1254 struct iwl_lq_sta *lq_sta,
1255 struct ieee80211_sta *sta,
1256 struct iwl_scale_tbl_info *tbl, int index)
1257{
1258 u16 rate_mask;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001259 s32 rate;
1260
1261 if (!sta->ht_cap.ht_supported)
1262 return -1;
1263
1264 IWL_DEBUG_RATE(mvm, "LQ: try to switch to SISO\n");
1265
Eyal Shapirad310e402013-08-11 18:43:47 +03001266 tbl->lq_type = lq_sta->is_vht ? LQ_VHT_SISO : LQ_HT_SISO;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001267 tbl->action = 0;
1268 tbl->max_search = IWL_MAX_SEARCH;
1269 rate_mask = lq_sta->active_siso_rate;
1270
Eyal Shapirad310e402013-08-11 18:43:47 +03001271 rs_set_bw_from_sta(tbl, sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001272 rs_set_expected_tpt_table(lq_sta, tbl);
1273 rate = rs_get_best_rate(mvm, lq_sta, tbl, rate_mask, index);
1274
1275 IWL_DEBUG_RATE(mvm, "LQ: get best rate %d mask %X\n", rate, rate_mask);
1276 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1277 IWL_DEBUG_RATE(mvm,
1278 "can not switch with index %d rate mask %x\n",
1279 rate, rate_mask);
1280 return -1;
1281 }
Eyal Shapira6a524f42013-08-11 20:27:18 +03001282 tbl->current_rate = rate_n_flags_from_tbl(mvm, tbl, rate);
1283 IWL_DEBUG_RATE(mvm, "LQ: Switch to new mcs %X index\n",
1284 tbl->current_rate);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001285 return 0;
1286}
1287
1288/*
1289 * Try to switch to new modulation mode from legacy
1290 */
1291static int rs_move_legacy_other(struct iwl_mvm *mvm,
1292 struct iwl_lq_sta *lq_sta,
1293 struct ieee80211_sta *sta,
1294 int index)
1295{
1296 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1297 struct iwl_scale_tbl_info *search_tbl =
1298 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1299 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1300 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1301 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1302 u8 start_action;
Emmanuel Grumbachff402312013-04-03 20:10:06 +03001303 u8 valid_tx_ant = iwl_fw_valid_tx_ant(mvm->fw);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001304 u8 tx_chains_num = num_of_ant(valid_tx_ant);
1305 int ret;
1306 u8 update_search_tbl_counter = 0;
1307
1308 start_action = tbl->action;
1309 while (1) {
1310 lq_sta->action_counter++;
1311 switch (tbl->action) {
Eyal Shapira4837b442013-07-28 23:02:46 +00001312 case IWL_LEGACY_SWITCH_ANTENNA:
Johannes Berg8ca151b2013-01-24 14:25:36 +01001313 IWL_DEBUG_RATE(mvm, "LQ: Legacy toggle Antenna\n");
1314
Eyal Shapira4837b442013-07-28 23:02:46 +00001315 if (tx_chains_num <= 1)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001316 break;
1317
1318 /* Don't change antenna if success has been great */
1319 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1320 break;
1321
1322 /* Set up search table to try other antenna */
1323 memcpy(search_tbl, tbl, sz);
1324
1325 if (rs_toggle_antenna(valid_tx_ant,
1326 &search_tbl->current_rate,
1327 search_tbl)) {
1328 update_search_tbl_counter = 1;
1329 rs_set_expected_tpt_table(lq_sta, search_tbl);
1330 goto out;
1331 }
1332 break;
1333 case IWL_LEGACY_SWITCH_SISO:
1334 IWL_DEBUG_RATE(mvm, "LQ: Legacy switch to SISO\n");
1335
1336 /* Set up search table to try SISO */
1337 memcpy(search_tbl, tbl, sz);
1338 search_tbl->is_SGI = 0;
1339 ret = rs_switch_to_siso(mvm, lq_sta, sta,
1340 search_tbl, index);
1341 if (!ret) {
1342 lq_sta->action_counter = 0;
1343 goto out;
1344 }
1345
1346 break;
Eyal Shapirad972ab32013-07-28 23:02:45 +00001347 case IWL_LEGACY_SWITCH_MIMO2:
Johannes Berg8ca151b2013-01-24 14:25:36 +01001348 IWL_DEBUG_RATE(mvm, "LQ: Legacy switch to MIMO2\n");
1349
1350 /* Set up search table to try MIMO */
1351 memcpy(search_tbl, tbl, sz);
1352 search_tbl->is_SGI = 0;
1353
Eyal Shapirad972ab32013-07-28 23:02:45 +00001354 search_tbl->ant_type = ANT_AB;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001355
1356 if (!rs_is_valid_ant(valid_tx_ant,
1357 search_tbl->ant_type))
1358 break;
1359
1360 ret = rs_switch_to_mimo2(mvm, lq_sta, sta,
1361 search_tbl, index);
1362 if (!ret) {
1363 lq_sta->action_counter = 0;
1364 goto out;
1365 }
1366 break;
Eyal Shapirad972ab32013-07-28 23:02:45 +00001367 default:
1368 WARN_ON_ONCE(1);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001369 }
Eyal Shapira4837b442013-07-28 23:02:46 +00001370 rs_move_next_action(tbl, IWL_LEGACY_LAST_ACTION);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001371
1372 if (tbl->action == start_action)
1373 break;
1374 }
1375 search_tbl->lq_type = LQ_NONE;
1376 return 0;
1377
1378out:
1379 lq_sta->search_better_tbl = 1;
Eyal Shapira4837b442013-07-28 23:02:46 +00001380 rs_move_next_action(tbl, IWL_LEGACY_LAST_ACTION);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001381 if (update_search_tbl_counter)
1382 search_tbl->action = tbl->action;
1383 return 0;
1384}
1385
1386/*
1387 * Try to switch to new modulation mode from SISO
1388 */
1389static int rs_move_siso_to_other(struct iwl_mvm *mvm,
1390 struct iwl_lq_sta *lq_sta,
1391 struct ieee80211_sta *sta, int index)
1392{
Johannes Berg8ca151b2013-01-24 14:25:36 +01001393 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1394 struct iwl_scale_tbl_info *search_tbl =
1395 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1396 struct iwl_rate_scale_data *window = &(tbl->win[index]);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001397 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1398 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1399 u8 start_action;
Emmanuel Grumbachff402312013-04-03 20:10:06 +03001400 u8 valid_tx_ant = iwl_fw_valid_tx_ant(mvm->fw);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001401 u8 tx_chains_num = num_of_ant(valid_tx_ant);
1402 u8 update_search_tbl_counter = 0;
1403 int ret;
1404
Emmanuel Grumbachffa6c702013-10-06 11:41:20 +03001405 if (tbl->action == IWL_SISO_SWITCH_MIMO2 &&
1406 !iwl_mvm_bt_coex_is_mimo_allowed(mvm, sta))
Eyal Shapira22f66422013-07-28 23:02:47 +00001407 tbl->action = IWL_SISO_SWITCH_ANTENNA;
Emmanuel Grumbach36946ce2013-05-28 23:12:47 +03001408
Johannes Berg8ca151b2013-01-24 14:25:36 +01001409 start_action = tbl->action;
1410 while (1) {
1411 lq_sta->action_counter++;
1412 switch (tbl->action) {
Eyal Shapira4837b442013-07-28 23:02:46 +00001413 case IWL_SISO_SWITCH_ANTENNA:
Johannes Berg8ca151b2013-01-24 14:25:36 +01001414 IWL_DEBUG_RATE(mvm, "LQ: SISO toggle Antenna\n");
Eyal Shapira4837b442013-07-28 23:02:46 +00001415 if (tx_chains_num <= 1)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001416 break;
1417
Emmanuel Grumbach36946ce2013-05-28 23:12:47 +03001418 if (window->success_ratio >= IWL_RS_GOOD_RATIO &&
1419 BT_MBOX_MSG(&mvm->last_bt_notif, 3,
1420 TRAFFIC_LOAD) == 0)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001421 break;
1422
1423 memcpy(search_tbl, tbl, sz);
1424 if (rs_toggle_antenna(valid_tx_ant,
1425 &search_tbl->current_rate,
1426 search_tbl)) {
1427 update_search_tbl_counter = 1;
1428 goto out;
1429 }
1430 break;
Eyal Shapirad972ab32013-07-28 23:02:45 +00001431 case IWL_SISO_SWITCH_MIMO2:
Johannes Berg8ca151b2013-01-24 14:25:36 +01001432 IWL_DEBUG_RATE(mvm, "LQ: SISO switch to MIMO2\n");
1433 memcpy(search_tbl, tbl, sz);
1434 search_tbl->is_SGI = 0;
1435
Eyal Shapirad972ab32013-07-28 23:02:45 +00001436 search_tbl->ant_type = ANT_AB;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001437
1438 if (!rs_is_valid_ant(valid_tx_ant,
1439 search_tbl->ant_type))
1440 break;
1441
1442 ret = rs_switch_to_mimo2(mvm, lq_sta, sta,
1443 search_tbl, index);
1444 if (!ret)
1445 goto out;
1446 break;
1447 case IWL_SISO_SWITCH_GI:
Eyal Shapirad310e402013-08-11 18:43:47 +03001448 if (!rs_sgi_allowed(tbl, sta))
Johannes Berg8ca151b2013-01-24 14:25:36 +01001449 break;
1450
1451 IWL_DEBUG_RATE(mvm, "LQ: SISO toggle SGI/NGI\n");
1452
1453 memcpy(search_tbl, tbl, sz);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001454 search_tbl->is_SGI = !tbl->is_SGI;
1455 rs_set_expected_tpt_table(lq_sta, search_tbl);
1456 if (tbl->is_SGI) {
1457 s32 tpt = lq_sta->last_tpt / 100;
1458 if (tpt >= search_tbl->expected_tpt[index])
1459 break;
1460 }
1461 search_tbl->current_rate =
Eyal Shapira6a524f42013-08-11 20:27:18 +03001462 rate_n_flags_from_tbl(mvm, search_tbl, index);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001463 update_search_tbl_counter = 1;
1464 goto out;
Eyal Shapirad972ab32013-07-28 23:02:45 +00001465 default:
1466 WARN_ON_ONCE(1);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001467 }
Eyal Shapira4837b442013-07-28 23:02:46 +00001468 rs_move_next_action(tbl, IWL_SISO_LAST_ACTION);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001469
1470 if (tbl->action == start_action)
1471 break;
1472 }
1473 search_tbl->lq_type = LQ_NONE;
1474 return 0;
1475
1476 out:
1477 lq_sta->search_better_tbl = 1;
Eyal Shapira4837b442013-07-28 23:02:46 +00001478 rs_move_next_action(tbl, IWL_SISO_LAST_ACTION);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001479 if (update_search_tbl_counter)
1480 search_tbl->action = tbl->action;
1481
1482 return 0;
1483}
1484
1485/*
1486 * Try to switch to new modulation mode from MIMO2
1487 */
1488static int rs_move_mimo2_to_other(struct iwl_mvm *mvm,
1489 struct iwl_lq_sta *lq_sta,
1490 struct ieee80211_sta *sta, int index)
1491{
Johannes Berg8ca151b2013-01-24 14:25:36 +01001492 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1493 struct iwl_scale_tbl_info *search_tbl =
1494 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001495 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1496 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1497 u8 start_action;
Emmanuel Grumbachff402312013-04-03 20:10:06 +03001498 u8 valid_tx_ant = iwl_fw_valid_tx_ant(mvm->fw);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001499 u8 update_search_tbl_counter = 0;
1500 int ret;
1501
1502 start_action = tbl->action;
1503 while (1) {
1504 lq_sta->action_counter++;
1505 switch (tbl->action) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001506 case IWL_MIMO2_SWITCH_SISO_A:
1507 case IWL_MIMO2_SWITCH_SISO_B:
Johannes Berg8ca151b2013-01-24 14:25:36 +01001508 IWL_DEBUG_RATE(mvm, "LQ: MIMO2 switch to SISO\n");
1509
1510 /* Set up new search table for SISO */
1511 memcpy(search_tbl, tbl, sz);
1512
1513 if (tbl->action == IWL_MIMO2_SWITCH_SISO_A)
1514 search_tbl->ant_type = ANT_A;
Eyal Shapirad972ab32013-07-28 23:02:45 +00001515 else /* tbl->action == IWL_MIMO2_SWITCH_SISO_B */
Johannes Berg8ca151b2013-01-24 14:25:36 +01001516 search_tbl->ant_type = ANT_B;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001517
1518 if (!rs_is_valid_ant(valid_tx_ant,
1519 search_tbl->ant_type))
1520 break;
1521
1522 ret = rs_switch_to_siso(mvm, lq_sta, sta,
1523 search_tbl, index);
1524 if (!ret)
1525 goto out;
1526
1527 break;
1528
1529 case IWL_MIMO2_SWITCH_GI:
Eyal Shapirad310e402013-08-11 18:43:47 +03001530 if (!rs_sgi_allowed(tbl, sta))
Johannes Berg8ca151b2013-01-24 14:25:36 +01001531 break;
1532
1533 IWL_DEBUG_RATE(mvm, "LQ: MIMO2 toggle SGI/NGI\n");
1534
1535 /* Set up new search table for MIMO2 */
1536 memcpy(search_tbl, tbl, sz);
1537 search_tbl->is_SGI = !tbl->is_SGI;
1538 rs_set_expected_tpt_table(lq_sta, search_tbl);
1539 /*
1540 * If active table already uses the fastest possible
1541 * modulation (dual stream with short guard interval),
1542 * and it's working well, there's no need to look
1543 * for a better type of modulation!
1544 */
1545 if (tbl->is_SGI) {
1546 s32 tpt = lq_sta->last_tpt / 100;
1547 if (tpt >= search_tbl->expected_tpt[index])
1548 break;
1549 }
1550 search_tbl->current_rate =
Eyal Shapira6a524f42013-08-11 20:27:18 +03001551 rate_n_flags_from_tbl(mvm, search_tbl, index);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001552 update_search_tbl_counter = 1;
1553 goto out;
Eyal Shapirad972ab32013-07-28 23:02:45 +00001554 default:
1555 WARN_ON_ONCE(1);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001556 }
Eyal Shapira4837b442013-07-28 23:02:46 +00001557 rs_move_next_action(tbl, IWL_MIMO2_LAST_ACTION);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001558
1559 if (tbl->action == start_action)
1560 break;
1561 }
1562 search_tbl->lq_type = LQ_NONE;
1563 return 0;
1564 out:
1565 lq_sta->search_better_tbl = 1;
Eyal Shapira4837b442013-07-28 23:02:46 +00001566 rs_move_next_action(tbl, IWL_MIMO2_LAST_ACTION);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001567 if (update_search_tbl_counter)
1568 search_tbl->action = tbl->action;
1569
1570 return 0;
1571}
1572
1573/*
Johannes Berg8ca151b2013-01-24 14:25:36 +01001574 * Check whether we should continue using same modulation mode, or
1575 * begin search for a new mode, based on:
1576 * 1) # tx successes or failures while using this mode
1577 * 2) # times calling this function
1578 * 3) elapsed time in this mode (not used, for now)
1579 */
1580static void rs_stay_in_table(struct iwl_lq_sta *lq_sta, bool force_search)
1581{
1582 struct iwl_scale_tbl_info *tbl;
1583 int i;
1584 int active_tbl;
1585 int flush_interval_passed = 0;
1586 struct iwl_mvm *mvm;
1587
1588 mvm = lq_sta->drv;
1589 active_tbl = lq_sta->active_tbl;
1590
1591 tbl = &(lq_sta->lq_info[active_tbl]);
1592
1593 /* If we've been disallowing search, see if we should now allow it */
1594 if (lq_sta->stay_in_tbl) {
1595 /* Elapsed time using current modulation mode */
1596 if (lq_sta->flush_timer)
1597 flush_interval_passed =
1598 time_after(jiffies,
1599 (unsigned long)(lq_sta->flush_timer +
1600 IWL_RATE_SCALE_FLUSH_INTVL));
1601
1602 /*
1603 * Check if we should allow search for new modulation mode.
1604 * If many frames have failed or succeeded, or we've used
1605 * this same modulation for a long time, allow search, and
1606 * reset history stats that keep track of whether we should
1607 * allow a new search. Also (below) reset all bitmaps and
1608 * stats in active history.
1609 */
1610 if (force_search ||
1611 (lq_sta->total_failed > lq_sta->max_failure_limit) ||
1612 (lq_sta->total_success > lq_sta->max_success_limit) ||
1613 ((!lq_sta->search_better_tbl) &&
1614 (lq_sta->flush_timer) && (flush_interval_passed))) {
1615 IWL_DEBUG_RATE(mvm,
1616 "LQ: stay is expired %d %d %d\n",
1617 lq_sta->total_failed,
1618 lq_sta->total_success,
1619 flush_interval_passed);
1620
1621 /* Allow search for new mode */
1622 lq_sta->stay_in_tbl = 0; /* only place reset */
1623 lq_sta->total_failed = 0;
1624 lq_sta->total_success = 0;
1625 lq_sta->flush_timer = 0;
1626 /*
1627 * Else if we've used this modulation mode enough repetitions
1628 * (regardless of elapsed time or success/failure), reset
1629 * history bitmaps and rate-specific stats for all rates in
1630 * active table.
1631 */
1632 } else {
1633 lq_sta->table_count++;
1634 if (lq_sta->table_count >=
1635 lq_sta->table_count_limit) {
1636 lq_sta->table_count = 0;
1637
1638 IWL_DEBUG_RATE(mvm,
1639 "LQ: stay in table clear win\n");
1640 for (i = 0; i < IWL_RATE_COUNT; i++)
1641 rs_rate_scale_clear_window(
1642 &(tbl->win[i]));
1643 }
1644 }
1645
1646 /* If transitioning to allow "search", reset all history
1647 * bitmaps and stats in active table (this will become the new
1648 * "search" table). */
1649 if (!lq_sta->stay_in_tbl) {
1650 for (i = 0; i < IWL_RATE_COUNT; i++)
1651 rs_rate_scale_clear_window(&(tbl->win[i]));
1652 }
1653 }
1654}
1655
1656/*
1657 * setup rate table in uCode
1658 */
1659static void rs_update_rate_tbl(struct iwl_mvm *mvm,
Emmanuel Grumbach9145d152013-07-18 08:45:41 +03001660 struct ieee80211_sta *sta,
Johannes Berg8ca151b2013-01-24 14:25:36 +01001661 struct iwl_lq_sta *lq_sta,
1662 struct iwl_scale_tbl_info *tbl,
Eyal Shapira6a524f42013-08-11 20:27:18 +03001663 int index)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001664{
1665 u32 rate;
1666
1667 /* Update uCode's rate table. */
Eyal Shapira6a524f42013-08-11 20:27:18 +03001668 rate = rate_n_flags_from_tbl(mvm, tbl, index);
Emmanuel Grumbach9145d152013-07-18 08:45:41 +03001669 rs_fill_link_cmd(mvm, sta, lq_sta, rate);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001670 iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, CMD_ASYNC, false);
1671}
1672
Eyal Shapira977342b2013-07-28 23:02:44 +00001673static u8 rs_get_tid(struct iwl_lq_sta *lq_data,
1674 struct ieee80211_hdr *hdr)
1675{
1676 u8 tid = IWL_MAX_TID_COUNT;
1677
1678 if (ieee80211_is_data_qos(hdr->frame_control)) {
1679 u8 *qc = ieee80211_get_qos_ctl(hdr);
1680 tid = qc[0] & 0xf;
1681 }
1682
1683 if (unlikely(tid > IWL_MAX_TID_COUNT))
1684 tid = IWL_MAX_TID_COUNT;
1685
1686 return tid;
1687}
1688
Johannes Berg8ca151b2013-01-24 14:25:36 +01001689/*
1690 * Do rate scaling and search for new modulation mode.
1691 */
1692static void rs_rate_scale_perform(struct iwl_mvm *mvm,
1693 struct sk_buff *skb,
1694 struct ieee80211_sta *sta,
1695 struct iwl_lq_sta *lq_sta)
1696{
1697 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1698 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1699 int low = IWL_RATE_INVALID;
1700 int high = IWL_RATE_INVALID;
1701 int index;
1702 int i;
1703 struct iwl_rate_scale_data *window = NULL;
1704 int current_tpt = IWL_INVALID_VALUE;
1705 int low_tpt = IWL_INVALID_VALUE;
1706 int high_tpt = IWL_INVALID_VALUE;
1707 u32 fail_count;
1708 s8 scale_action = 0;
1709 u16 rate_mask;
1710 u8 update_lq = 0;
1711 struct iwl_scale_tbl_info *tbl, *tbl1;
1712 u16 rate_scale_index_msk = 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001713 u8 active_tbl = 0;
1714 u8 done_search = 0;
1715 u16 high_low;
1716 s32 sr;
1717 u8 tid = IWL_MAX_TID_COUNT;
1718 struct iwl_mvm_sta *sta_priv = (void *)sta->drv_priv;
1719 struct iwl_mvm_tid_data *tid_data;
1720
1721 IWL_DEBUG_RATE(mvm, "rate scale calculate new rate for skb\n");
1722
1723 /* Send management frames and NO_ACK data using lowest rate. */
1724 /* TODO: this could probably be improved.. */
1725 if (!ieee80211_is_data(hdr->frame_control) ||
1726 info->flags & IEEE80211_TX_CTL_NO_ACK)
1727 return;
1728
1729 lq_sta->supp_rates = sta->supp_rates[lq_sta->band];
1730
Eyal Shapira977342b2013-07-28 23:02:44 +00001731 tid = rs_get_tid(lq_sta, hdr);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001732 if ((tid != IWL_MAX_TID_COUNT) &&
1733 (lq_sta->tx_agg_tid_en & (1 << tid))) {
1734 tid_data = &sta_priv->tid_data[tid];
1735 if (tid_data->state == IWL_AGG_OFF)
1736 lq_sta->is_agg = 0;
1737 else
1738 lq_sta->is_agg = 1;
1739 } else {
1740 lq_sta->is_agg = 0;
1741 }
1742
1743 /*
1744 * Select rate-scale / modulation-mode table to work with in
1745 * the rest of this function: "search" if searching for better
1746 * modulation mode, or "active" if doing rate scaling within a mode.
1747 */
1748 if (!lq_sta->search_better_tbl)
1749 active_tbl = lq_sta->active_tbl;
1750 else
1751 active_tbl = 1 - lq_sta->active_tbl;
1752
1753 tbl = &(lq_sta->lq_info[active_tbl]);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001754
1755 /* current tx rate */
1756 index = lq_sta->last_txrate_idx;
1757
1758 IWL_DEBUG_RATE(mvm, "Rate scale index %d for type %d\n", index,
1759 tbl->lq_type);
1760
1761 /* rates available for this association, and for modulation mode */
1762 rate_mask = rs_get_supported_rates(lq_sta, hdr, tbl->lq_type);
1763
1764 IWL_DEBUG_RATE(mvm, "mask 0x%04X\n", rate_mask);
1765
1766 /* mask with station rate restriction */
1767 if (is_legacy(tbl->lq_type)) {
1768 if (lq_sta->band == IEEE80211_BAND_5GHZ)
1769 /* supp_rates has no CCK bits in A mode */
1770 rate_scale_index_msk = (u16) (rate_mask &
1771 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
1772 else
1773 rate_scale_index_msk = (u16) (rate_mask &
1774 lq_sta->supp_rates);
1775
1776 } else {
1777 rate_scale_index_msk = rate_mask;
1778 }
1779
1780 if (!rate_scale_index_msk)
1781 rate_scale_index_msk = rate_mask;
1782
1783 if (!((1 << index) & rate_scale_index_msk)) {
1784 IWL_ERR(mvm, "Current Rate is not valid\n");
1785 if (lq_sta->search_better_tbl) {
1786 /* revert to active table if search table is not valid*/
1787 tbl->lq_type = LQ_NONE;
1788 lq_sta->search_better_tbl = 0;
1789 tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1790 /* get "active" rate info */
1791 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
Emmanuel Grumbach9145d152013-07-18 08:45:41 +03001792 rs_update_rate_tbl(mvm, sta, lq_sta, tbl, index);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001793 }
1794 return;
1795 }
1796
1797 /* Get expected throughput table and history window for current rate */
1798 if (!tbl->expected_tpt) {
1799 IWL_ERR(mvm, "tbl->expected_tpt is NULL\n");
1800 return;
1801 }
1802
1803 /* force user max rate if set by user */
1804 if ((lq_sta->max_rate_idx != -1) &&
1805 (lq_sta->max_rate_idx < index)) {
1806 index = lq_sta->max_rate_idx;
1807 update_lq = 1;
1808 window = &(tbl->win[index]);
1809 goto lq_update;
1810 }
1811
1812 window = &(tbl->win[index]);
1813
1814 /*
1815 * If there is not enough history to calculate actual average
1816 * throughput, keep analyzing results of more tx frames, without
1817 * changing rate or mode (bypass most of the rest of this function).
1818 * Set up new rate table in uCode only if old rate is not supported
1819 * in current association (use new rate found above).
1820 */
1821 fail_count = window->counter - window->success_counter;
1822 if ((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
1823 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH)) {
1824 IWL_DEBUG_RATE(mvm,
1825 "LQ: still below TH. succ=%d total=%d for index %d\n",
1826 window->success_counter, window->counter, index);
1827
1828 /* Can't calculate this yet; not enough history */
1829 window->average_tpt = IWL_INVALID_VALUE;
1830
1831 /* Should we stay with this modulation mode,
1832 * or search for a new one? */
1833 rs_stay_in_table(lq_sta, false);
1834
1835 goto out;
1836 }
1837 /* Else we have enough samples; calculate estimate of
1838 * actual average throughput */
1839 if (window->average_tpt != ((window->success_ratio *
1840 tbl->expected_tpt[index] + 64) / 128)) {
1841 IWL_ERR(mvm,
1842 "expected_tpt should have been calculated by now\n");
1843 window->average_tpt = ((window->success_ratio *
1844 tbl->expected_tpt[index] + 64) / 128);
1845 }
1846
1847 /* If we are searching for better modulation mode, check success. */
1848 if (lq_sta->search_better_tbl) {
1849 /* If good success, continue using the "search" mode;
1850 * no need to send new link quality command, since we're
1851 * continuing to use the setup that we've been trying. */
1852 if (window->average_tpt > lq_sta->last_tpt) {
1853 IWL_DEBUG_RATE(mvm,
1854 "LQ: SWITCHING TO NEW TABLE suc=%d cur-tpt=%d old-tpt=%d\n",
1855 window->success_ratio,
1856 window->average_tpt,
1857 lq_sta->last_tpt);
1858
1859 if (!is_legacy(tbl->lq_type))
1860 lq_sta->enable_counter = 1;
1861
1862 /* Swap tables; "search" becomes "active" */
1863 lq_sta->active_tbl = active_tbl;
1864 current_tpt = window->average_tpt;
1865 /* Else poor success; go back to mode in "active" table */
1866 } else {
1867 IWL_DEBUG_RATE(mvm,
1868 "LQ: GOING BACK TO THE OLD TABLE suc=%d cur-tpt=%d old-tpt=%d\n",
1869 window->success_ratio,
1870 window->average_tpt,
1871 lq_sta->last_tpt);
1872
1873 /* Nullify "search" table */
1874 tbl->lq_type = LQ_NONE;
1875
1876 /* Revert to "active" table */
1877 active_tbl = lq_sta->active_tbl;
1878 tbl = &(lq_sta->lq_info[active_tbl]);
1879
1880 /* Revert to "active" rate and throughput info */
1881 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
1882 current_tpt = lq_sta->last_tpt;
1883
1884 /* Need to set up a new rate table in uCode */
1885 update_lq = 1;
1886 }
1887
1888 /* Either way, we've made a decision; modulation mode
1889 * search is done, allow rate adjustment next time. */
1890 lq_sta->search_better_tbl = 0;
1891 done_search = 1; /* Don't switch modes below! */
1892 goto lq_update;
1893 }
1894
1895 /* (Else) not in search of better modulation mode, try for better
1896 * starting rate, while staying in this mode. */
1897 high_low = rs_get_adjacent_rate(mvm, index, rate_scale_index_msk,
1898 tbl->lq_type);
1899 low = high_low & 0xff;
1900 high = (high_low >> 8) & 0xff;
1901
1902 /* If user set max rate, dont allow higher than user constrain */
1903 if ((lq_sta->max_rate_idx != -1) &&
1904 (lq_sta->max_rate_idx < high))
1905 high = IWL_RATE_INVALID;
1906
1907 sr = window->success_ratio;
1908
1909 /* Collect measured throughputs for current and adjacent rates */
1910 current_tpt = window->average_tpt;
1911 if (low != IWL_RATE_INVALID)
1912 low_tpt = tbl->win[low].average_tpt;
1913 if (high != IWL_RATE_INVALID)
1914 high_tpt = tbl->win[high].average_tpt;
1915
1916 scale_action = 0;
1917
1918 /* Too many failures, decrease rate */
1919 if ((sr <= IWL_RATE_DECREASE_TH) || (current_tpt == 0)) {
1920 IWL_DEBUG_RATE(mvm,
1921 "decrease rate because of low success_ratio\n");
1922 scale_action = -1;
1923 /* No throughput measured yet for adjacent rates; try increase. */
1924 } else if ((low_tpt == IWL_INVALID_VALUE) &&
1925 (high_tpt == IWL_INVALID_VALUE)) {
1926 if (high != IWL_RATE_INVALID && sr >= IWL_RATE_INCREASE_TH)
1927 scale_action = 1;
1928 else if (low != IWL_RATE_INVALID)
1929 scale_action = 0;
1930 }
1931
1932 /* Both adjacent throughputs are measured, but neither one has better
1933 * throughput; we're using the best rate, don't change it! */
1934 else if ((low_tpt != IWL_INVALID_VALUE) &&
1935 (high_tpt != IWL_INVALID_VALUE) &&
1936 (low_tpt < current_tpt) &&
1937 (high_tpt < current_tpt))
1938 scale_action = 0;
1939
1940 /* At least one adjacent rate's throughput is measured,
1941 * and may have better performance. */
1942 else {
1943 /* Higher adjacent rate's throughput is measured */
1944 if (high_tpt != IWL_INVALID_VALUE) {
1945 /* Higher rate has better throughput */
1946 if (high_tpt > current_tpt &&
1947 sr >= IWL_RATE_INCREASE_TH) {
1948 scale_action = 1;
1949 } else {
1950 scale_action = 0;
1951 }
1952
1953 /* Lower adjacent rate's throughput is measured */
1954 } else if (low_tpt != IWL_INVALID_VALUE) {
1955 /* Lower rate has better throughput */
1956 if (low_tpt > current_tpt) {
1957 IWL_DEBUG_RATE(mvm,
1958 "decrease rate because of low tpt\n");
1959 scale_action = -1;
1960 } else if (sr >= IWL_RATE_INCREASE_TH) {
1961 scale_action = 1;
1962 }
1963 }
1964 }
1965
1966 /* Sanity check; asked for decrease, but success rate or throughput
1967 * has been good at old rate. Don't change it. */
1968 if ((scale_action == -1) && (low != IWL_RATE_INVALID) &&
1969 ((sr > IWL_RATE_HIGH_TH) ||
1970 (current_tpt > (100 * tbl->expected_tpt[low]))))
1971 scale_action = 0;
1972
Emmanuel Grumbach4515f302013-06-30 07:51:54 +03001973 if ((le32_to_cpu(mvm->last_bt_notif.bt_activity_grading) >=
Eyal Shapirad972ab32013-07-28 23:02:45 +00001974 IWL_BT_COEX_TRAFFIC_LOAD_HIGH) && (is_mimo(tbl->lq_type))) {
Emmanuel Grumbach36946ce2013-05-28 23:12:47 +03001975 if (lq_sta->last_bt_traffic >
Emmanuel Grumbach4515f302013-06-30 07:51:54 +03001976 le32_to_cpu(mvm->last_bt_notif.bt_activity_grading)) {
Emmanuel Grumbach36946ce2013-05-28 23:12:47 +03001977 /*
1978 * don't set scale_action, don't want to scale up if
1979 * the rate scale doesn't otherwise think that is a
1980 * good idea.
1981 */
1982 } else if (lq_sta->last_bt_traffic <=
Emmanuel Grumbach4515f302013-06-30 07:51:54 +03001983 le32_to_cpu(mvm->last_bt_notif.bt_activity_grading)) {
Emmanuel Grumbach36946ce2013-05-28 23:12:47 +03001984 scale_action = -1;
1985 }
1986 }
1987 lq_sta->last_bt_traffic =
Emmanuel Grumbach4515f302013-06-30 07:51:54 +03001988 le32_to_cpu(mvm->last_bt_notif.bt_activity_grading);
Emmanuel Grumbach36946ce2013-05-28 23:12:47 +03001989
Emmanuel Grumbach4515f302013-06-30 07:51:54 +03001990 if ((le32_to_cpu(mvm->last_bt_notif.bt_activity_grading) >=
Eyal Shapirad972ab32013-07-28 23:02:45 +00001991 IWL_BT_COEX_TRAFFIC_LOAD_HIGH) && is_mimo(tbl->lq_type)) {
Emmanuel Grumbach36946ce2013-05-28 23:12:47 +03001992 /* search for a new modulation */
1993 rs_stay_in_table(lq_sta, true);
1994 goto lq_update;
1995 }
1996
Johannes Berg8ca151b2013-01-24 14:25:36 +01001997 switch (scale_action) {
1998 case -1:
1999 /* Decrease starting rate, update uCode's rate table */
2000 if (low != IWL_RATE_INVALID) {
2001 update_lq = 1;
2002 index = low;
2003 }
2004
2005 break;
2006 case 1:
2007 /* Increase starting rate, update uCode's rate table */
2008 if (high != IWL_RATE_INVALID) {
2009 update_lq = 1;
2010 index = high;
2011 }
2012
2013 break;
2014 case 0:
2015 /* No change */
2016 default:
2017 break;
2018 }
2019
2020 IWL_DEBUG_RATE(mvm,
2021 "choose rate scale index %d action %d low %d high %d type %d\n",
2022 index, scale_action, low, high, tbl->lq_type);
2023
2024lq_update:
2025 /* Replace uCode's rate table for the destination station. */
2026 if (update_lq)
Emmanuel Grumbach9145d152013-07-18 08:45:41 +03002027 rs_update_rate_tbl(mvm, sta, lq_sta, tbl, index);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002028
2029 rs_stay_in_table(lq_sta, false);
2030
2031 /*
2032 * Search for new modulation mode if we're:
2033 * 1) Not changing rates right now
2034 * 2) Not just finishing up a search
2035 * 3) Allowing a new search
2036 */
2037 if (!update_lq && !done_search &&
2038 !lq_sta->stay_in_tbl && window->counter) {
2039 /* Save current throughput to compare with "search" throughput*/
2040 lq_sta->last_tpt = current_tpt;
2041
2042 /* Select a new "search" modulation mode to try.
2043 * If one is found, set up the new "search" table. */
2044 if (is_legacy(tbl->lq_type))
2045 rs_move_legacy_other(mvm, lq_sta, sta, index);
2046 else if (is_siso(tbl->lq_type))
2047 rs_move_siso_to_other(mvm, lq_sta, sta, index);
2048 else if (is_mimo2(tbl->lq_type))
2049 rs_move_mimo2_to_other(mvm, lq_sta, sta, index);
2050 else
Eyal Shapirad972ab32013-07-28 23:02:45 +00002051 WARN_ON_ONCE(1);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002052
2053 /* If new "search" mode was selected, set up in uCode table */
2054 if (lq_sta->search_better_tbl) {
2055 /* Access the "search" table, clear its history. */
2056 tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
2057 for (i = 0; i < IWL_RATE_COUNT; i++)
2058 rs_rate_scale_clear_window(&(tbl->win[i]));
2059
2060 /* Use new "search" start rate */
2061 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
2062
2063 IWL_DEBUG_RATE(mvm,
2064 "Switch current mcs: %X index: %d\n",
2065 tbl->current_rate, index);
Emmanuel Grumbach9145d152013-07-18 08:45:41 +03002066 rs_fill_link_cmd(mvm, sta, lq_sta, tbl->current_rate);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002067 iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, CMD_ASYNC, false);
2068 } else {
2069 done_search = 1;
2070 }
2071 }
2072
2073 if (done_search && !lq_sta->stay_in_tbl) {
2074 /* If the "active" (non-search) mode was legacy,
2075 * and we've tried switching antennas,
2076 * but we haven't been able to try HT modes (not available),
2077 * stay with best antenna legacy modulation for a while
2078 * before next round of mode comparisons. */
2079 tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
2080 if (is_legacy(tbl1->lq_type) && !sta->ht_cap.ht_supported &&
2081 lq_sta->action_counter > tbl1->max_search) {
2082 IWL_DEBUG_RATE(mvm, "LQ: STAY in legacy table\n");
2083 rs_set_stay_in_table(mvm, 1, lq_sta);
2084 }
2085
2086 /* If we're in an HT mode, and all 3 mode switch actions
2087 * have been tried and compared, stay in this best modulation
2088 * mode for a while before next round of mode comparisons. */
2089 if (lq_sta->enable_counter &&
2090 (lq_sta->action_counter >= tbl1->max_search)) {
2091 if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
2092 (lq_sta->tx_agg_tid_en & (1 << tid)) &&
2093 (tid != IWL_MAX_TID_COUNT)) {
2094 tid_data = &sta_priv->tid_data[tid];
2095 if (tid_data->state == IWL_AGG_OFF) {
2096 IWL_DEBUG_RATE(mvm,
2097 "try to aggregate tid %d\n",
2098 tid);
2099 rs_tl_turn_on_agg(mvm, tid,
2100 lq_sta, sta);
2101 }
2102 }
2103 rs_set_stay_in_table(mvm, 0, lq_sta);
2104 }
2105 }
2106
2107out:
Eyal Shapira6a524f42013-08-11 20:27:18 +03002108 tbl->current_rate = rate_n_flags_from_tbl(mvm, tbl, index);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002109 lq_sta->last_txrate_idx = index;
2110}
2111
2112/**
2113 * rs_initialize_lq - Initialize a station's hardware rate table
2114 *
2115 * The uCode's station table contains a table of fallback rates
2116 * for automatic fallback during transmission.
2117 *
2118 * NOTE: This sets up a default set of values. These will be replaced later
2119 * if the driver's iwl-agn-rs rate scaling algorithm is used, instead of
2120 * rc80211_simple.
2121 *
2122 * NOTE: Run REPLY_ADD_STA command to set up station table entry, before
2123 * calling this function (which runs REPLY_TX_LINK_QUALITY_CMD,
2124 * which requires station table entry to exist).
2125 */
2126static void rs_initialize_lq(struct iwl_mvm *mvm,
2127 struct ieee80211_sta *sta,
2128 struct iwl_lq_sta *lq_sta,
2129 enum ieee80211_band band)
2130{
2131 struct iwl_scale_tbl_info *tbl;
2132 int rate_idx;
2133 int i;
2134 u32 rate;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002135 u8 active_tbl = 0;
2136 u8 valid_tx_ant;
2137
2138 if (!sta || !lq_sta)
2139 return;
2140
2141 i = lq_sta->last_txrate_idx;
2142
Emmanuel Grumbachff402312013-04-03 20:10:06 +03002143 valid_tx_ant = iwl_fw_valid_tx_ant(mvm->fw);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002144
2145 if (!lq_sta->search_better_tbl)
2146 active_tbl = lq_sta->active_tbl;
2147 else
2148 active_tbl = 1 - lq_sta->active_tbl;
2149
2150 tbl = &(lq_sta->lq_info[active_tbl]);
2151
2152 if ((i < 0) || (i >= IWL_RATE_COUNT))
2153 i = 0;
2154
2155 rate = iwl_rates[i].plcp;
2156 tbl->ant_type = first_antenna(valid_tx_ant);
2157 rate |= tbl->ant_type << RATE_MCS_ANT_POS;
2158
2159 if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
2160 rate |= RATE_MCS_CCK_MSK;
2161
2162 rs_get_tbl_info_from_mcs(rate, band, tbl, &rate_idx);
2163 if (!rs_is_valid_ant(valid_tx_ant, tbl->ant_type))
2164 rs_toggle_antenna(valid_tx_ant, &rate, tbl);
2165
Eyal Shapira6a524f42013-08-11 20:27:18 +03002166 rate = rate_n_flags_from_tbl(mvm, tbl, rate_idx);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002167 tbl->current_rate = rate;
2168 rs_set_expected_tpt_table(lq_sta, tbl);
Emmanuel Grumbach9145d152013-07-18 08:45:41 +03002169 rs_fill_link_cmd(NULL, NULL, lq_sta, rate);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002170 /* TODO restore station should remember the lq cmd */
2171 iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, CMD_SYNC, true);
2172}
2173
2174static void rs_get_rate(void *mvm_r, struct ieee80211_sta *sta, void *mvm_sta,
2175 struct ieee80211_tx_rate_control *txrc)
2176{
2177 struct sk_buff *skb = txrc->skb;
2178 struct ieee80211_supported_band *sband = txrc->sband;
2179 struct iwl_op_mode *op_mode __maybe_unused =
2180 (struct iwl_op_mode *)mvm_r;
2181 struct iwl_mvm *mvm __maybe_unused = IWL_OP_MODE_GET_MVM(op_mode);
2182 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2183 struct iwl_lq_sta *lq_sta = mvm_sta;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002184
2185 IWL_DEBUG_RATE_LIMIT(mvm, "rate scale calculate new rate for skb\n");
2186
2187 /* Get max rate if user set max rate */
2188 if (lq_sta) {
2189 lq_sta->max_rate_idx = txrc->max_rate_idx;
2190 if ((sband->band == IEEE80211_BAND_5GHZ) &&
2191 (lq_sta->max_rate_idx != -1))
2192 lq_sta->max_rate_idx += IWL_FIRST_OFDM_RATE;
2193 if ((lq_sta->max_rate_idx < 0) ||
2194 (lq_sta->max_rate_idx >= IWL_RATE_COUNT))
2195 lq_sta->max_rate_idx = -1;
2196 }
2197
2198 /* Treat uninitialized rate scaling data same as non-existing. */
2199 if (lq_sta && !lq_sta->drv) {
2200 IWL_DEBUG_RATE(mvm, "Rate scaling not initialized yet.\n");
2201 mvm_sta = NULL;
2202 }
2203
2204 /* Send management frames and NO_ACK data using lowest rate. */
2205 if (rate_control_send_low(sta, mvm_sta, txrc))
2206 return;
2207
Eyal Shapirad310e402013-08-11 18:43:47 +03002208 iwl_mvm_hwrate_to_tx_rate(lq_sta->last_rate_n_flags,
2209 info->band, &info->control.rates[0]);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002210
Moshe Benji622ebe92013-06-03 19:27:16 +03002211 info->control.rates[0].count = 1;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002212}
2213
2214static void *rs_alloc_sta(void *mvm_rate, struct ieee80211_sta *sta,
2215 gfp_t gfp)
2216{
2217 struct iwl_mvm_sta *sta_priv = (struct iwl_mvm_sta *)sta->drv_priv;
2218 struct iwl_op_mode *op_mode __maybe_unused =
2219 (struct iwl_op_mode *)mvm_rate;
2220 struct iwl_mvm *mvm __maybe_unused = IWL_OP_MODE_GET_MVM(op_mode);
2221
2222 IWL_DEBUG_RATE(mvm, "create station rate scale window\n");
2223
2224 return &sta_priv->lq_sta;
2225}
2226
Eyal Shapirad310e402013-08-11 18:43:47 +03002227static int rs_vht_highest_rx_mcs_index(struct ieee80211_sta_vht_cap *vht_cap,
2228 int nss)
2229{
2230 u16 rx_mcs = le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map) &
2231 (0x3 << (2 * (nss - 1)));
2232 rx_mcs >>= (2 * (nss - 1));
2233
2234 if (rx_mcs == IEEE80211_VHT_MCS_SUPPORT_0_7)
2235 return IWL_RATE_MCS_7_INDEX;
2236 else if (rx_mcs == IEEE80211_VHT_MCS_SUPPORT_0_8)
2237 return IWL_RATE_MCS_8_INDEX;
2238 else if (rx_mcs == IEEE80211_VHT_MCS_SUPPORT_0_9)
2239 return IWL_RATE_MCS_9_INDEX;
2240
2241 WARN_ON_ONCE(rx_mcs != IEEE80211_VHT_MCS_NOT_SUPPORTED);
2242 return -1;
2243}
2244
Johannes Berg8ca151b2013-01-24 14:25:36 +01002245/*
2246 * Called after adding a new station to initialize rate scaling
2247 */
2248void iwl_mvm_rs_rate_init(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
2249 enum ieee80211_band band)
2250{
2251 int i, j;
2252 struct ieee80211_hw *hw = mvm->hw;
2253 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Eyal Shapirad310e402013-08-11 18:43:47 +03002254 struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002255 struct iwl_mvm_sta *sta_priv;
2256 struct iwl_lq_sta *lq_sta;
2257 struct ieee80211_supported_band *sband;
2258 unsigned long supp; /* must be unsigned long for for_each_set_bit */
2259
2260 sta_priv = (struct iwl_mvm_sta *)sta->drv_priv;
2261 lq_sta = &sta_priv->lq_sta;
2262 sband = hw->wiphy->bands[band];
2263
2264 lq_sta->lq.sta_id = sta_priv->sta_id;
2265
2266 for (j = 0; j < LQ_SIZE; j++)
2267 for (i = 0; i < IWL_RATE_COUNT; i++)
2268 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
2269
2270 lq_sta->flush_timer = 0;
2271 lq_sta->supp_rates = sta->supp_rates[sband->band];
Johannes Berg8ca151b2013-01-24 14:25:36 +01002272
2273 IWL_DEBUG_RATE(mvm,
2274 "LQ: *** rate scale station global init for station %d ***\n",
2275 sta_priv->sta_id);
2276 /* TODO: what is a good starting rate for STA? About middle? Maybe not
2277 * the lowest or the highest rate.. Could consider using RSSI from
2278 * previous packets? Need to have IEEE 802.1X auth succeed immediately
2279 * after assoc.. */
2280
2281 lq_sta->max_rate_idx = -1;
2282 lq_sta->missed_rate_counter = IWL_MISSED_RATE_MAX;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002283 lq_sta->band = sband->band;
2284 /*
2285 * active legacy rates as per supported rates bitmap
2286 */
2287 supp = sta->supp_rates[sband->band];
2288 lq_sta->active_legacy_rate = 0;
2289 for_each_set_bit(i, &supp, BITS_PER_LONG)
2290 lq_sta->active_legacy_rate |= BIT(sband->bitrates[i].hw_value);
2291
Eyal Shapirad310e402013-08-11 18:43:47 +03002292 /* TODO: should probably account for rx_highest for both HT/VHT */
2293 if (!vht_cap || !vht_cap->vht_supported) {
2294 /* active_siso_rate mask includes 9 MBits (bit 5),
2295 * and CCK (bits 0-3), supp_rates[] does not;
2296 * shift to convert format, force 9 MBits off.
2297 */
2298 lq_sta->active_siso_rate = ht_cap->mcs.rx_mask[0] << 1;
2299 lq_sta->active_siso_rate |= ht_cap->mcs.rx_mask[0] & 0x1;
2300 lq_sta->active_siso_rate &= ~((u16)0x2);
2301 lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002302
Eyal Shapirad310e402013-08-11 18:43:47 +03002303 /* Same here */
2304 lq_sta->active_mimo2_rate = ht_cap->mcs.rx_mask[1] << 1;
2305 lq_sta->active_mimo2_rate |= ht_cap->mcs.rx_mask[1] & 0x1;
2306 lq_sta->active_mimo2_rate &= ~((u16)0x2);
2307 lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE;
2308
2309 lq_sta->is_vht = false;
2310 } else {
2311 int highest_mcs = rs_vht_highest_rx_mcs_index(vht_cap, 1);
2312 if (highest_mcs >= IWL_RATE_MCS_0_INDEX) {
2313 for (i = IWL_RATE_MCS_0_INDEX; i <= highest_mcs; i++) {
2314 if (i == IWL_RATE_9M_INDEX)
2315 continue;
2316
2317 lq_sta->active_siso_rate |= BIT(i);
2318 }
2319 }
2320
2321 highest_mcs = rs_vht_highest_rx_mcs_index(vht_cap, 2);
2322 if (highest_mcs >= IWL_RATE_MCS_0_INDEX) {
2323 for (i = IWL_RATE_MCS_0_INDEX; i <= highest_mcs; i++) {
2324 if (i == IWL_RATE_9M_INDEX)
2325 continue;
2326
2327 lq_sta->active_mimo2_rate |= BIT(i);
2328 }
2329 }
2330
2331 /* TODO: avoid MCS9 in 20Mhz which isn't valid for 11ac */
2332 lq_sta->is_vht = true;
2333 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01002334
Johannes Berg8ca151b2013-01-24 14:25:36 +01002335 IWL_DEBUG_RATE(mvm,
Eyal Shapirad310e402013-08-11 18:43:47 +03002336 "SISO-RATE=%X MIMO2-RATE=%X VHT=%d\n",
Johannes Berg8ca151b2013-01-24 14:25:36 +01002337 lq_sta->active_siso_rate,
Eyal Shapirad310e402013-08-11 18:43:47 +03002338 lq_sta->active_mimo2_rate,
2339 lq_sta->is_vht);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002340
2341 /* These values will be overridden later */
2342 lq_sta->lq.single_stream_ant_msk =
Emmanuel Grumbachff402312013-04-03 20:10:06 +03002343 first_antenna(iwl_fw_valid_tx_ant(mvm->fw));
Johannes Berg8ca151b2013-01-24 14:25:36 +01002344 lq_sta->lq.dual_stream_ant_msk =
Emmanuel Grumbachff402312013-04-03 20:10:06 +03002345 iwl_fw_valid_tx_ant(mvm->fw) &
2346 ~first_antenna(iwl_fw_valid_tx_ant(mvm->fw));
Johannes Berg8ca151b2013-01-24 14:25:36 +01002347 if (!lq_sta->lq.dual_stream_ant_msk) {
2348 lq_sta->lq.dual_stream_ant_msk = ANT_AB;
Emmanuel Grumbachff402312013-04-03 20:10:06 +03002349 } else if (num_of_ant(iwl_fw_valid_tx_ant(mvm->fw)) == 2) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002350 lq_sta->lq.dual_stream_ant_msk =
Emmanuel Grumbachff402312013-04-03 20:10:06 +03002351 iwl_fw_valid_tx_ant(mvm->fw);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002352 }
2353
2354 /* as default allow aggregation for all tids */
2355 lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
2356 lq_sta->drv = mvm;
2357
2358 /* Set last_txrate_idx to lowest rate */
2359 lq_sta->last_txrate_idx = rate_lowest_index(sband, sta);
2360 if (sband->band == IEEE80211_BAND_5GHZ)
2361 lq_sta->last_txrate_idx += IWL_FIRST_OFDM_RATE;
2362 lq_sta->is_agg = 0;
2363#ifdef CONFIG_MAC80211_DEBUGFS
2364 lq_sta->dbg_fixed_rate = 0;
2365#endif
2366
2367 rs_initialize_lq(mvm, sta, lq_sta, band);
2368}
2369
2370static void rs_fill_link_cmd(struct iwl_mvm *mvm,
Emmanuel Grumbach9145d152013-07-18 08:45:41 +03002371 struct ieee80211_sta *sta,
Johannes Berg8ca151b2013-01-24 14:25:36 +01002372 struct iwl_lq_sta *lq_sta, u32 new_rate)
2373{
2374 struct iwl_scale_tbl_info tbl_type;
2375 int index = 0;
2376 int rate_idx;
2377 int repeat_rate = 0;
2378 u8 ant_toggle_cnt = 0;
2379 u8 use_ht_possible = 1;
2380 u8 valid_tx_ant = 0;
2381 struct iwl_lq_cmd *lq_cmd = &lq_sta->lq;
2382
2383 /* Override starting rate (index 0) if needed for debug purposes */
Eyal Shapira3571ac32013-07-29 17:08:48 +03002384 rs_dbgfs_set_mcs(lq_sta, &new_rate);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002385
2386 /* Interpret new_rate (rate_n_flags) */
2387 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band,
2388 &tbl_type, &rate_idx);
2389
2390 /* How many times should we repeat the initial rate? */
2391 if (is_legacy(tbl_type.lq_type)) {
2392 ant_toggle_cnt = 1;
2393 repeat_rate = IWL_NUMBER_TRY;
2394 } else {
2395 repeat_rate = min(IWL_HT_NUMBER_TRY,
2396 LINK_QUAL_AGG_DISABLE_START_DEF - 1);
2397 }
2398
2399 lq_cmd->mimo_delim = is_mimo(tbl_type.lq_type) ? 1 : 0;
2400
2401 /* Fill 1st table entry (index 0) */
2402 lq_cmd->rs_table[index] = cpu_to_le32(new_rate);
2403
2404 if (num_of_ant(tbl_type.ant_type) == 1)
2405 lq_cmd->single_stream_ant_msk = tbl_type.ant_type;
2406 else if (num_of_ant(tbl_type.ant_type) == 2)
2407 lq_cmd->dual_stream_ant_msk = tbl_type.ant_type;
2408 /* otherwise we don't modify the existing value */
2409
2410 index++;
2411 repeat_rate--;
2412 if (mvm)
Emmanuel Grumbachff402312013-04-03 20:10:06 +03002413 valid_tx_ant = iwl_fw_valid_tx_ant(mvm->fw);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002414
2415 /* Fill rest of rate table */
2416 while (index < LINK_QUAL_MAX_RETRY_NUM) {
2417 /* Repeat initial/next rate.
2418 * For legacy IWL_NUMBER_TRY == 1, this loop will not execute.
2419 * For HT IWL_HT_NUMBER_TRY == 3, this executes twice. */
2420 while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
2421 if (is_legacy(tbl_type.lq_type)) {
2422 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2423 ant_toggle_cnt++;
2424 else if (mvm &&
2425 rs_toggle_antenna(valid_tx_ant,
2426 &new_rate, &tbl_type))
2427 ant_toggle_cnt = 1;
2428 }
2429
2430 /* Override next rate if needed for debug purposes */
Eyal Shapira3571ac32013-07-29 17:08:48 +03002431 rs_dbgfs_set_mcs(lq_sta, &new_rate);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002432
2433 /* Fill next table entry */
2434 lq_cmd->rs_table[index] =
2435 cpu_to_le32(new_rate);
2436 repeat_rate--;
2437 index++;
2438 }
2439
2440 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band, &tbl_type,
2441 &rate_idx);
2442
Johannes Berg8ca151b2013-01-24 14:25:36 +01002443 /* Indicate to uCode which entries might be MIMO.
2444 * If initial rate was MIMO, this will finally end up
2445 * as (IWL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
2446 if (is_mimo(tbl_type.lq_type))
2447 lq_cmd->mimo_delim = index;
2448
2449 /* Get next rate */
2450 new_rate = rs_get_lower_rate(lq_sta, &tbl_type, rate_idx,
2451 use_ht_possible);
2452
2453 /* How many times should we repeat the next rate? */
2454 if (is_legacy(tbl_type.lq_type)) {
2455 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2456 ant_toggle_cnt++;
2457 else if (mvm &&
2458 rs_toggle_antenna(valid_tx_ant,
2459 &new_rate, &tbl_type))
2460 ant_toggle_cnt = 1;
2461
2462 repeat_rate = IWL_NUMBER_TRY;
2463 } else {
2464 repeat_rate = IWL_HT_NUMBER_TRY;
2465 }
2466
2467 /* Don't allow HT rates after next pass.
Eyal Shapirad310e402013-08-11 18:43:47 +03002468 * rs_get_lower_rate() will change type to LQ_LEGACY_A
2469 * or LQ_LEGACY_G.
2470 */
Johannes Berg8ca151b2013-01-24 14:25:36 +01002471 use_ht_possible = 0;
2472
2473 /* Override next rate if needed for debug purposes */
Eyal Shapira3571ac32013-07-29 17:08:48 +03002474 rs_dbgfs_set_mcs(lq_sta, &new_rate);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002475
2476 /* Fill next table entry */
2477 lq_cmd->rs_table[index] = cpu_to_le32(new_rate);
2478
2479 index++;
2480 repeat_rate--;
2481 }
2482
2483 lq_cmd->agg_frame_cnt_limit = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
2484 lq_cmd->agg_disable_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
2485
2486 lq_cmd->agg_time_limit =
2487 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
Emmanuel Grumbache96d5512013-05-28 21:31:50 +03002488
Emmanuel Grumbach9145d152013-07-18 08:45:41 +03002489 if (sta)
2490 lq_cmd->agg_time_limit =
2491 cpu_to_le16(iwl_mvm_bt_coex_agg_time_limit(mvm, sta));
Johannes Berg8ca151b2013-01-24 14:25:36 +01002492}
2493
2494static void *rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
2495{
2496 return hw->priv;
2497}
2498/* rate scale requires free function to be implemented */
2499static void rs_free(void *mvm_rate)
2500{
2501 return;
2502}
2503
2504static void rs_free_sta(void *mvm_r, struct ieee80211_sta *sta,
2505 void *mvm_sta)
2506{
2507 struct iwl_op_mode *op_mode __maybe_unused = mvm_r;
2508 struct iwl_mvm *mvm __maybe_unused = IWL_OP_MODE_GET_MVM(op_mode);
2509
2510 IWL_DEBUG_RATE(mvm, "enter\n");
2511 IWL_DEBUG_RATE(mvm, "leave\n");
2512}
2513
2514#ifdef CONFIG_MAC80211_DEBUGFS
2515static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
Eyal Shapira3571ac32013-07-29 17:08:48 +03002516 u32 *rate_n_flags)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002517{
2518 struct iwl_mvm *mvm;
2519 u8 valid_tx_ant;
2520 u8 ant_sel_tx;
2521
2522 mvm = lq_sta->drv;
Emmanuel Grumbachff402312013-04-03 20:10:06 +03002523 valid_tx_ant = iwl_fw_valid_tx_ant(mvm->fw);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002524 if (lq_sta->dbg_fixed_rate) {
2525 ant_sel_tx =
2526 ((lq_sta->dbg_fixed_rate & RATE_MCS_ANT_ABC_MSK)
2527 >> RATE_MCS_ANT_POS);
2528 if ((valid_tx_ant & ant_sel_tx) == ant_sel_tx) {
2529 *rate_n_flags = lq_sta->dbg_fixed_rate;
2530 IWL_DEBUG_RATE(mvm, "Fixed rate ON\n");
2531 } else {
2532 lq_sta->dbg_fixed_rate = 0;
2533 IWL_ERR(mvm,
2534 "Invalid antenna selection 0x%X, Valid is 0x%X\n",
2535 ant_sel_tx, valid_tx_ant);
2536 IWL_DEBUG_RATE(mvm, "Fixed rate OFF\n");
2537 }
2538 } else {
2539 IWL_DEBUG_RATE(mvm, "Fixed rate OFF\n");
2540 }
2541}
2542
2543static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
2544 const char __user *user_buf, size_t count, loff_t *ppos)
2545{
2546 struct iwl_lq_sta *lq_sta = file->private_data;
2547 struct iwl_mvm *mvm;
2548 char buf[64];
2549 size_t buf_size;
2550 u32 parsed_rate;
2551
2552
2553 mvm = lq_sta->drv;
2554 memset(buf, 0, sizeof(buf));
2555 buf_size = min(count, sizeof(buf) - 1);
2556 if (copy_from_user(buf, user_buf, buf_size))
2557 return -EFAULT;
2558
2559 if (sscanf(buf, "%x", &parsed_rate) == 1)
2560 lq_sta->dbg_fixed_rate = parsed_rate;
2561 else
2562 lq_sta->dbg_fixed_rate = 0;
2563
2564 rs_program_fix_rate(mvm, lq_sta);
2565
2566 return count;
2567}
2568
2569static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
2570 char __user *user_buf, size_t count, loff_t *ppos)
2571{
2572 char *buff;
2573 int desc = 0;
2574 int i = 0;
2575 int index = 0;
2576 ssize_t ret;
2577
2578 struct iwl_lq_sta *lq_sta = file->private_data;
2579 struct iwl_mvm *mvm;
2580 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
2581
2582 mvm = lq_sta->drv;
2583 buff = kmalloc(1024, GFP_KERNEL);
2584 if (!buff)
2585 return -ENOMEM;
2586
2587 desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
2588 desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
2589 lq_sta->total_failed, lq_sta->total_success,
2590 lq_sta->active_legacy_rate);
2591 desc += sprintf(buff+desc, "fixed rate 0x%X\n",
2592 lq_sta->dbg_fixed_rate);
2593 desc += sprintf(buff+desc, "valid_tx_ant %s%s%s\n",
Emmanuel Grumbachff402312013-04-03 20:10:06 +03002594 (iwl_fw_valid_tx_ant(mvm->fw) & ANT_A) ? "ANT_A," : "",
2595 (iwl_fw_valid_tx_ant(mvm->fw) & ANT_B) ? "ANT_B," : "",
2596 (iwl_fw_valid_tx_ant(mvm->fw) & ANT_C) ? "ANT_C" : "");
Johannes Berg8ca151b2013-01-24 14:25:36 +01002597 desc += sprintf(buff+desc, "lq type %s\n",
Eyal Shapirad310e402013-08-11 18:43:47 +03002598 (is_legacy(tbl->lq_type)) ? "legacy" :
2599 is_vht(tbl->lq_type) ? "VHT" : "HT");
2600 if (is_ht(tbl->lq_type)) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002601 desc += sprintf(buff+desc, " %s",
Eyal Shapirad972ab32013-07-28 23:02:45 +00002602 (is_siso(tbl->lq_type)) ? "SISO" : "MIMO2");
Johannes Berg8ca151b2013-01-24 14:25:36 +01002603 desc += sprintf(buff+desc, " %s",
Eyal Shapirad310e402013-08-11 18:43:47 +03002604 (is_ht20(tbl)) ? "20MHz" :
2605 (is_ht40(tbl)) ? "40MHz" :
2606 (is_ht80(tbl)) ? "80Mhz" : "BAD BW");
Eyal Shapira6a524f42013-08-11 20:27:18 +03002607 desc += sprintf(buff+desc, " %s %s\n",
Johannes Berg8ca151b2013-01-24 14:25:36 +01002608 (tbl->is_SGI) ? "SGI" : "",
Eyal Shapira6a524f42013-08-11 20:27:18 +03002609 (lq_sta->is_agg) ? "AGG on" : "");
Johannes Berg8ca151b2013-01-24 14:25:36 +01002610 }
2611 desc += sprintf(buff+desc, "last tx rate=0x%X\n",
2612 lq_sta->last_rate_n_flags);
2613 desc += sprintf(buff+desc,
2614 "general: flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
2615 lq_sta->lq.flags,
2616 lq_sta->lq.mimo_delim,
2617 lq_sta->lq.single_stream_ant_msk,
2618 lq_sta->lq.dual_stream_ant_msk);
2619
2620 desc += sprintf(buff+desc,
2621 "agg: time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
2622 le16_to_cpu(lq_sta->lq.agg_time_limit),
2623 lq_sta->lq.agg_disable_start_th,
2624 lq_sta->lq.agg_frame_cnt_limit);
2625
2626 desc += sprintf(buff+desc,
2627 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
2628 lq_sta->lq.initial_rate_index[0],
2629 lq_sta->lq.initial_rate_index[1],
2630 lq_sta->lq.initial_rate_index[2],
2631 lq_sta->lq.initial_rate_index[3]);
2632
2633 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
2634 index = iwl_hwrate_to_plcp_idx(
2635 le32_to_cpu(lq_sta->lq.rs_table[i]));
2636 if (is_legacy(tbl->lq_type)) {
2637 desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps\n",
2638 i, le32_to_cpu(lq_sta->lq.rs_table[i]),
2639 iwl_rate_mcs[index].mbps);
2640 } else {
2641 desc += sprintf(buff+desc,
2642 " rate[%d] 0x%X %smbps (%s)\n",
2643 i, le32_to_cpu(lq_sta->lq.rs_table[i]),
2644 iwl_rate_mcs[index].mbps,
2645 iwl_rate_mcs[index].mcs);
2646 }
2647 }
2648
2649 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2650 kfree(buff);
2651 return ret;
2652}
2653
2654static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
2655 .write = rs_sta_dbgfs_scale_table_write,
2656 .read = rs_sta_dbgfs_scale_table_read,
2657 .open = simple_open,
2658 .llseek = default_llseek,
2659};
2660static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
2661 char __user *user_buf, size_t count, loff_t *ppos)
2662{
2663 char *buff;
2664 int desc = 0;
2665 int i, j;
2666 ssize_t ret;
Eyal Shapirad310e402013-08-11 18:43:47 +03002667 struct iwl_scale_tbl_info *tbl;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002668 struct iwl_lq_sta *lq_sta = file->private_data;
2669
2670 buff = kmalloc(1024, GFP_KERNEL);
2671 if (!buff)
2672 return -ENOMEM;
2673
2674 for (i = 0; i < LQ_SIZE; i++) {
Eyal Shapirad310e402013-08-11 18:43:47 +03002675 tbl = &(lq_sta->lq_info[i]);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002676 desc += sprintf(buff+desc,
Eyal Shapira6a524f42013-08-11 20:27:18 +03002677 "%s type=%d SGI=%d BW=%s DUP=0\n"
Johannes Berg8ca151b2013-01-24 14:25:36 +01002678 "rate=0x%X\n",
2679 lq_sta->active_tbl == i ? "*" : "x",
Eyal Shapirad310e402013-08-11 18:43:47 +03002680 tbl->lq_type,
2681 tbl->is_SGI,
2682 is_ht20(tbl) ? "20Mhz" :
2683 is_ht40(tbl) ? "40Mhz" :
2684 is_ht80(tbl) ? "80Mhz" : "ERR",
Eyal Shapirad310e402013-08-11 18:43:47 +03002685 tbl->current_rate);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002686 for (j = 0; j < IWL_RATE_COUNT; j++) {
2687 desc += sprintf(buff+desc,
2688 "counter=%d success=%d %%=%d\n",
Eyal Shapirad310e402013-08-11 18:43:47 +03002689 tbl->win[j].counter,
2690 tbl->win[j].success_counter,
2691 tbl->win[j].success_ratio);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002692 }
2693 }
2694 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2695 kfree(buff);
2696 return ret;
2697}
2698
2699static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
2700 .read = rs_sta_dbgfs_stats_table_read,
2701 .open = simple_open,
2702 .llseek = default_llseek,
2703};
2704
Johannes Berg8ca151b2013-01-24 14:25:36 +01002705static void rs_add_debugfs(void *mvm, void *mvm_sta, struct dentry *dir)
2706{
2707 struct iwl_lq_sta *lq_sta = mvm_sta;
2708 lq_sta->rs_sta_dbgfs_scale_table_file =
2709 debugfs_create_file("rate_scale_table", S_IRUSR | S_IWUSR, dir,
2710 lq_sta, &rs_sta_dbgfs_scale_table_ops);
2711 lq_sta->rs_sta_dbgfs_stats_table_file =
2712 debugfs_create_file("rate_stats_table", S_IRUSR, dir,
2713 lq_sta, &rs_sta_dbgfs_stats_table_ops);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002714 lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file =
2715 debugfs_create_u8("tx_agg_tid_enable", S_IRUSR | S_IWUSR, dir,
2716 &lq_sta->tx_agg_tid_en);
2717}
2718
2719static void rs_remove_debugfs(void *mvm, void *mvm_sta)
2720{
2721 struct iwl_lq_sta *lq_sta = mvm_sta;
2722 debugfs_remove(lq_sta->rs_sta_dbgfs_scale_table_file);
2723 debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002724 debugfs_remove(lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file);
2725}
2726#endif
2727
2728/*
2729 * Initialization of rate scaling information is done by driver after
2730 * the station is added. Since mac80211 calls this function before a
2731 * station is added we ignore it.
2732 */
2733static void rs_rate_init_stub(void *mvm_r,
Simon Wunderlich3de805c2013-07-08 16:55:50 +02002734 struct ieee80211_supported_band *sband,
2735 struct cfg80211_chan_def *chandef,
2736 struct ieee80211_sta *sta, void *mvm_sta)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002737{
2738}
2739static struct rate_control_ops rs_mvm_ops = {
2740 .module = NULL,
2741 .name = RS_NAME,
2742 .tx_status = rs_tx_status,
2743 .get_rate = rs_get_rate,
2744 .rate_init = rs_rate_init_stub,
2745 .alloc = rs_alloc,
2746 .free = rs_free,
2747 .alloc_sta = rs_alloc_sta,
2748 .free_sta = rs_free_sta,
2749#ifdef CONFIG_MAC80211_DEBUGFS
2750 .add_sta_debugfs = rs_add_debugfs,
2751 .remove_sta_debugfs = rs_remove_debugfs,
2752#endif
2753};
2754
2755int iwl_mvm_rate_control_register(void)
2756{
2757 return ieee80211_rate_control_register(&rs_mvm_ops);
2758}
2759
2760void iwl_mvm_rate_control_unregister(void)
2761{
2762 ieee80211_rate_control_unregister(&rs_mvm_ops);
2763}
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +03002764
2765/**
2766 * iwl_mvm_tx_protection - Gets LQ command, change it to enable/disable
2767 * Tx protection, according to this rquest and previous requests,
2768 * and send the LQ command.
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +03002769 * @mvmsta: The station
2770 * @enable: Enable Tx protection?
2771 */
Johannes Berge126b5d2013-06-28 13:39:18 +02002772int iwl_mvm_tx_protection(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
2773 bool enable)
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +03002774{
Johannes Berge126b5d2013-06-28 13:39:18 +02002775 struct iwl_lq_cmd *lq = &mvmsta->lq_sta.lq;
2776
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +03002777 lockdep_assert_held(&mvm->mutex);
2778
2779 if (enable) {
2780 if (mvmsta->tx_protection == 0)
2781 lq->flags |= LQ_FLAG_SET_STA_TLC_RTS_MSK;
2782 mvmsta->tx_protection++;
2783 } else {
2784 mvmsta->tx_protection--;
2785 if (mvmsta->tx_protection == 0)
2786 lq->flags &= ~LQ_FLAG_SET_STA_TLC_RTS_MSK;
2787 }
2788
2789 return iwl_mvm_send_lq_cmd(mvm, lq, CMD_ASYNC, false);
2790}