blob: 5cab26ecc17ad970b86882796a570d35562d72f4 [file] [log] [blame]
Johannes Berg8ca151b2013-01-24 14:25:36 +01001/******************************************************************************
2 *
Emmanuel Grumbach51368bf2013-12-30 13:15:54 +02003 * Copyright(c) 2005 - 2014 Intel Corporation. All rights reserved.
Johannes Berg8ca151b2013-01-24 14:25:36 +01004 *
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>
Johannes Berg8ca151b2013-01-24 14:25:36 +010027#include <linux/skbuff.h>
28#include <linux/slab.h>
29#include <net/mac80211.h>
30
31#include <linux/netdevice.h>
32#include <linux/etherdevice.h>
33#include <linux/delay.h>
34
35#include <linux/workqueue.h>
36#include "rs.h"
37#include "fw-api.h"
38#include "sta.h"
39#include "iwl-op-mode.h"
40#include "mvm.h"
41
42#define RS_NAME "iwl-mvm-rs"
43
Eyal Shapirae07be6d2013-12-09 13:02:56 +020044#define NUM_TRY_BEFORE_ANT_TOGGLE 1
45#define RS_LEGACY_RETRIES_PER_RATE 1
46#define RS_HT_VHT_RETRIES_PER_RATE 2
47#define RS_HT_VHT_RETRIES_PER_RATE_TW 1
48#define RS_INITIAL_MIMO_NUM_RATES 3
49#define RS_INITIAL_SISO_NUM_RATES 3
50#define RS_INITIAL_LEGACY_NUM_RATES LINK_QUAL_MAX_RETRY_NUM
51#define RS_SECONDARY_LEGACY_NUM_RATES LINK_QUAL_MAX_RETRY_NUM
52#define RS_SECONDARY_SISO_NUM_RATES 3
53#define RS_SECONDARY_SISO_RETRIES 1
Johannes Berg8ca151b2013-01-24 14:25:36 +010054
55#define IWL_RATE_MAX_WINDOW 62 /* # tx in history window */
Eyal Shapirad17334c2013-11-20 02:08:12 +020056#define IWL_RATE_MIN_FAILURE_TH 3 /* min failures to calc tpt */
Johannes Berg8ca151b2013-01-24 14:25:36 +010057#define IWL_RATE_MIN_SUCCESS_TH 8 /* min successes to calc tpt */
58
59/* max allowed rate miss before sync LQ cmd */
60#define IWL_MISSED_RATE_MAX 15
Eyal Shapira32b01722013-11-22 09:41:38 +020061#define RS_STAY_IN_COLUMN_TIMEOUT (5*HZ)
Eyal Shapira87d5e412014-04-06 18:13:22 +030062#define RS_IDLE_TIMEOUT (5*HZ)
Johannes Berg8ca151b2013-01-24 14:25:36 +010063
64static u8 rs_ht_to_legacy[] = {
Eyal Shapirada87d7d2013-11-28 12:27:03 +020065 [IWL_RATE_MCS_0_INDEX] = IWL_RATE_6M_INDEX,
66 [IWL_RATE_MCS_1_INDEX] = IWL_RATE_9M_INDEX,
67 [IWL_RATE_MCS_2_INDEX] = IWL_RATE_12M_INDEX,
68 [IWL_RATE_MCS_3_INDEX] = IWL_RATE_18M_INDEX,
69 [IWL_RATE_MCS_4_INDEX] = IWL_RATE_24M_INDEX,
70 [IWL_RATE_MCS_5_INDEX] = IWL_RATE_36M_INDEX,
71 [IWL_RATE_MCS_6_INDEX] = IWL_RATE_48M_INDEX,
72 [IWL_RATE_MCS_7_INDEX] = IWL_RATE_54M_INDEX,
73 [IWL_RATE_MCS_8_INDEX] = IWL_RATE_54M_INDEX,
74 [IWL_RATE_MCS_9_INDEX] = IWL_RATE_54M_INDEX,
Johannes Berg8ca151b2013-01-24 14:25:36 +010075};
76
77static const u8 ant_toggle_lookup[] = {
Eliad Pelleref4394b2013-07-16 18:07:20 +030078 [ANT_NONE] = ANT_NONE,
79 [ANT_A] = ANT_B,
80 [ANT_B] = ANT_C,
81 [ANT_AB] = ANT_BC,
82 [ANT_C] = ANT_A,
83 [ANT_AC] = ANT_AB,
84 [ANT_BC] = ANT_AC,
85 [ANT_ABC] = ANT_ABC,
Johannes Berg8ca151b2013-01-24 14:25:36 +010086};
87
Eyal Shapirad310e402013-08-11 18:43:47 +030088#define IWL_DECLARE_RATE_INFO(r, s, rp, rn) \
89 [IWL_RATE_##r##M_INDEX] = { IWL_RATE_##r##M_PLCP, \
90 IWL_RATE_HT_SISO_MCS_##s##_PLCP, \
91 IWL_RATE_HT_MIMO2_MCS_##s##_PLCP, \
92 IWL_RATE_VHT_SISO_MCS_##s##_PLCP, \
93 IWL_RATE_VHT_MIMO2_MCS_##s##_PLCP,\
94 IWL_RATE_##rp##M_INDEX, \
Eyal Shapiraab055ce2013-08-04 13:10:31 +030095 IWL_RATE_##rn##M_INDEX }
Johannes Berg8ca151b2013-01-24 14:25:36 +010096
Eyal Shapirad310e402013-08-11 18:43:47 +030097#define IWL_DECLARE_MCS_RATE(s) \
98 [IWL_RATE_MCS_##s##_INDEX] = { IWL_RATE_INVM_PLCP, \
99 IWL_RATE_HT_SISO_MCS_##s##_PLCP, \
100 IWL_RATE_HT_MIMO2_MCS_##s##_PLCP, \
101 IWL_RATE_VHT_SISO_MCS_##s##_PLCP, \
102 IWL_RATE_VHT_MIMO2_MCS_##s##_PLCP, \
103 IWL_RATE_INVM_INDEX, \
104 IWL_RATE_INVM_INDEX }
105
Johannes Berg8ca151b2013-01-24 14:25:36 +0100106/*
107 * Parameter order:
Eyal Shapiraab055ce2013-08-04 13:10:31 +0300108 * rate, ht rate, prev rate, next rate
Johannes Berg8ca151b2013-01-24 14:25:36 +0100109 *
110 * If there isn't a valid next or previous rate then INV is used which
111 * maps to IWL_RATE_INVALID
112 *
113 */
114static const struct iwl_rs_rate_info iwl_rates[IWL_RATE_COUNT] = {
Eyal Shapiraab055ce2013-08-04 13:10:31 +0300115 IWL_DECLARE_RATE_INFO(1, INV, INV, 2), /* 1mbps */
116 IWL_DECLARE_RATE_INFO(2, INV, 1, 5), /* 2mbps */
117 IWL_DECLARE_RATE_INFO(5, INV, 2, 11), /*5.5mbps */
118 IWL_DECLARE_RATE_INFO(11, INV, 9, 12), /* 11mbps */
Eyal Shapirad310e402013-08-11 18:43:47 +0300119 IWL_DECLARE_RATE_INFO(6, 0, 5, 11), /* 6mbps ; MCS 0 */
120 IWL_DECLARE_RATE_INFO(9, INV, 6, 11), /* 9mbps */
121 IWL_DECLARE_RATE_INFO(12, 1, 11, 18), /* 12mbps ; MCS 1 */
122 IWL_DECLARE_RATE_INFO(18, 2, 12, 24), /* 18mbps ; MCS 2 */
123 IWL_DECLARE_RATE_INFO(24, 3, 18, 36), /* 24mbps ; MCS 3 */
124 IWL_DECLARE_RATE_INFO(36, 4, 24, 48), /* 36mbps ; MCS 4 */
125 IWL_DECLARE_RATE_INFO(48, 5, 36, 54), /* 48mbps ; MCS 5 */
126 IWL_DECLARE_RATE_INFO(54, 6, 48, INV), /* 54mbps ; MCS 6 */
127 IWL_DECLARE_MCS_RATE(7), /* MCS 7 */
128 IWL_DECLARE_MCS_RATE(8), /* MCS 8 */
129 IWL_DECLARE_MCS_RATE(9), /* MCS 9 */
Johannes Berg8ca151b2013-01-24 14:25:36 +0100130};
131
Eyal Shapira4e4b8152013-12-11 00:55:36 +0200132enum rs_action {
133 RS_ACTION_STAY = 0,
134 RS_ACTION_DOWNSCALE = -1,
135 RS_ACTION_UPSCALE = 1,
136};
137
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200138enum rs_column_mode {
139 RS_INVALID = 0,
140 RS_LEGACY,
141 RS_SISO,
142 RS_MIMO2,
143};
144
Eyal Shapirafd7dbee2014-04-06 04:39:23 +0300145#define MAX_NEXT_COLUMNS 7
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200146#define MAX_COLUMN_CHECKS 3
147
148typedef bool (*allow_column_func_t) (struct iwl_mvm *mvm,
149 struct ieee80211_sta *sta,
150 struct iwl_scale_tbl_info *tbl);
151
152struct rs_tx_column {
153 enum rs_column_mode mode;
154 u8 ant;
155 bool sgi;
156 enum rs_column next_columns[MAX_NEXT_COLUMNS];
157 allow_column_func_t checks[MAX_COLUMN_CHECKS];
158};
159
160static bool rs_mimo_allow(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
161 struct iwl_scale_tbl_info *tbl)
162{
163 if (!sta->ht_cap.ht_supported)
164 return false;
165
166 if (sta->smps_mode == IEEE80211_SMPS_STATIC)
167 return false;
168
Johannes Berg4ed735e2014-02-12 21:47:44 +0100169 if (num_of_ant(mvm->fw->valid_tx_ant) < 2)
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200170 return false;
171
172 if (!iwl_mvm_bt_coex_is_mimo_allowed(mvm, sta))
173 return false;
174
175 return true;
176}
177
178static bool rs_siso_allow(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
179 struct iwl_scale_tbl_info *tbl)
180{
181 if (!sta->ht_cap.ht_supported)
182 return false;
183
184 return true;
185}
186
187static bool rs_sgi_allow(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
188 struct iwl_scale_tbl_info *tbl)
189{
190 struct rs_rate *rate = &tbl->rate;
191 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
192 struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
193
194 if (is_ht20(rate) && (ht_cap->cap &
195 IEEE80211_HT_CAP_SGI_20))
196 return true;
197 if (is_ht40(rate) && (ht_cap->cap &
198 IEEE80211_HT_CAP_SGI_40))
199 return true;
200 if (is_ht80(rate) && (vht_cap->cap &
201 IEEE80211_VHT_CAP_SHORT_GI_80))
202 return true;
203
204 return false;
205}
206
207static const struct rs_tx_column rs_tx_columns[] = {
208 [RS_COLUMN_LEGACY_ANT_A] = {
209 .mode = RS_LEGACY,
210 .ant = ANT_A,
211 .next_columns = {
212 RS_COLUMN_LEGACY_ANT_B,
213 RS_COLUMN_SISO_ANT_A,
Eyal Shapira8930b052014-03-16 05:23:21 +0200214 RS_COLUMN_SISO_ANT_B,
Eyal Shapirad8fff912014-04-06 05:27:36 +0300215 RS_COLUMN_INVALID,
216 RS_COLUMN_INVALID,
Eyal Shapirafd7dbee2014-04-06 04:39:23 +0300217 RS_COLUMN_INVALID,
218 RS_COLUMN_INVALID,
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200219 },
220 },
221 [RS_COLUMN_LEGACY_ANT_B] = {
222 .mode = RS_LEGACY,
223 .ant = ANT_B,
224 .next_columns = {
225 RS_COLUMN_LEGACY_ANT_A,
Eyal Shapira8930b052014-03-16 05:23:21 +0200226 RS_COLUMN_SISO_ANT_A,
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200227 RS_COLUMN_SISO_ANT_B,
Eyal Shapirad8fff912014-04-06 05:27:36 +0300228 RS_COLUMN_INVALID,
229 RS_COLUMN_INVALID,
Eyal Shapirafd7dbee2014-04-06 04:39:23 +0300230 RS_COLUMN_INVALID,
231 RS_COLUMN_INVALID,
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200232 },
233 },
234 [RS_COLUMN_SISO_ANT_A] = {
235 .mode = RS_SISO,
236 .ant = ANT_A,
237 .next_columns = {
238 RS_COLUMN_SISO_ANT_B,
239 RS_COLUMN_MIMO2,
240 RS_COLUMN_SISO_ANT_A_SGI,
Eyal Shapira8930b052014-03-16 05:23:21 +0200241 RS_COLUMN_SISO_ANT_B_SGI,
Eyal Shapirafd7dbee2014-04-06 04:39:23 +0300242 RS_COLUMN_LEGACY_ANT_A,
243 RS_COLUMN_LEGACY_ANT_B,
Eyal Shapirad8fff912014-04-06 05:27:36 +0300244 RS_COLUMN_INVALID,
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200245 },
246 .checks = {
247 rs_siso_allow,
248 },
249 },
250 [RS_COLUMN_SISO_ANT_B] = {
251 .mode = RS_SISO,
252 .ant = ANT_B,
253 .next_columns = {
254 RS_COLUMN_SISO_ANT_A,
255 RS_COLUMN_MIMO2,
256 RS_COLUMN_SISO_ANT_B_SGI,
Eyal Shapira8930b052014-03-16 05:23:21 +0200257 RS_COLUMN_SISO_ANT_A_SGI,
Eyal Shapirafd7dbee2014-04-06 04:39:23 +0300258 RS_COLUMN_LEGACY_ANT_A,
259 RS_COLUMN_LEGACY_ANT_B,
Eyal Shapirad8fff912014-04-06 05:27:36 +0300260 RS_COLUMN_INVALID,
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200261 },
262 .checks = {
263 rs_siso_allow,
264 },
265 },
266 [RS_COLUMN_SISO_ANT_A_SGI] = {
267 .mode = RS_SISO,
268 .ant = ANT_A,
269 .sgi = true,
270 .next_columns = {
271 RS_COLUMN_SISO_ANT_B_SGI,
272 RS_COLUMN_MIMO2_SGI,
273 RS_COLUMN_SISO_ANT_A,
Eyal Shapira8930b052014-03-16 05:23:21 +0200274 RS_COLUMN_SISO_ANT_B,
275 RS_COLUMN_MIMO2,
Eyal Shapirafd7dbee2014-04-06 04:39:23 +0300276 RS_COLUMN_LEGACY_ANT_A,
277 RS_COLUMN_LEGACY_ANT_B,
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200278 },
279 .checks = {
280 rs_siso_allow,
281 rs_sgi_allow,
282 },
283 },
284 [RS_COLUMN_SISO_ANT_B_SGI] = {
285 .mode = RS_SISO,
286 .ant = ANT_B,
287 .sgi = true,
288 .next_columns = {
289 RS_COLUMN_SISO_ANT_A_SGI,
290 RS_COLUMN_MIMO2_SGI,
291 RS_COLUMN_SISO_ANT_B,
Eyal Shapira8930b052014-03-16 05:23:21 +0200292 RS_COLUMN_SISO_ANT_A,
293 RS_COLUMN_MIMO2,
Eyal Shapirafd7dbee2014-04-06 04:39:23 +0300294 RS_COLUMN_LEGACY_ANT_A,
295 RS_COLUMN_LEGACY_ANT_B,
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200296 },
297 .checks = {
298 rs_siso_allow,
299 rs_sgi_allow,
300 },
301 },
302 [RS_COLUMN_MIMO2] = {
303 .mode = RS_MIMO2,
304 .ant = ANT_AB,
305 .next_columns = {
306 RS_COLUMN_SISO_ANT_A,
Eyal Shapira8930b052014-03-16 05:23:21 +0200307 RS_COLUMN_SISO_ANT_B,
308 RS_COLUMN_SISO_ANT_A_SGI,
309 RS_COLUMN_SISO_ANT_B_SGI,
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200310 RS_COLUMN_MIMO2_SGI,
Eyal Shapirafd7dbee2014-04-06 04:39:23 +0300311 RS_COLUMN_LEGACY_ANT_A,
312 RS_COLUMN_LEGACY_ANT_B,
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200313 },
314 .checks = {
315 rs_mimo_allow,
316 },
317 },
318 [RS_COLUMN_MIMO2_SGI] = {
319 .mode = RS_MIMO2,
320 .ant = ANT_AB,
321 .sgi = true,
322 .next_columns = {
323 RS_COLUMN_SISO_ANT_A_SGI,
Eyal Shapira8930b052014-03-16 05:23:21 +0200324 RS_COLUMN_SISO_ANT_B_SGI,
325 RS_COLUMN_SISO_ANT_A,
326 RS_COLUMN_SISO_ANT_B,
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200327 RS_COLUMN_MIMO2,
Eyal Shapirafd7dbee2014-04-06 04:39:23 +0300328 RS_COLUMN_LEGACY_ANT_A,
329 RS_COLUMN_LEGACY_ANT_B,
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200330 },
331 .checks = {
332 rs_mimo_allow,
333 rs_sgi_allow,
334 },
335 },
336};
337
Johannes Berg8ca151b2013-01-24 14:25:36 +0100338static inline u8 rs_extract_rate(u32 rate_n_flags)
339{
340 /* also works for HT because bits 7:6 are zero there */
341 return (u8)(rate_n_flags & RATE_LEGACY_RATE_MSK);
342}
343
344static int iwl_hwrate_to_plcp_idx(u32 rate_n_flags)
345{
346 int idx = 0;
347
Johannes Berg8ca151b2013-01-24 14:25:36 +0100348 if (rate_n_flags & RATE_MCS_HT_MSK) {
Eyal Shapirad310e402013-08-11 18:43:47 +0300349 idx = rate_n_flags & RATE_HT_MCS_RATE_CODE_MSK;
350 idx += IWL_RATE_MCS_0_INDEX;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100351
Eyal Shapirad310e402013-08-11 18:43:47 +0300352 /* skip 9M not supported in HT*/
Johannes Berg8ca151b2013-01-24 14:25:36 +0100353 if (idx >= IWL_RATE_9M_INDEX)
354 idx += 1;
Eyal Shapira4e82dd32013-08-04 23:58:37 +0300355 if ((idx >= IWL_FIRST_HT_RATE) && (idx <= IWL_LAST_HT_RATE))
Johannes Berg8ca151b2013-01-24 14:25:36 +0100356 return idx;
Eyal Shapirad310e402013-08-11 18:43:47 +0300357 } else if (rate_n_flags & RATE_MCS_VHT_MSK) {
358 idx = rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK;
359 idx += IWL_RATE_MCS_0_INDEX;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100360
Eyal Shapirad310e402013-08-11 18:43:47 +0300361 /* skip 9M not supported in VHT*/
362 if (idx >= IWL_RATE_9M_INDEX)
363 idx++;
364 if ((idx >= IWL_FIRST_VHT_RATE) && (idx <= IWL_LAST_VHT_RATE))
365 return idx;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100366 } else {
Eyal Shapirad310e402013-08-11 18:43:47 +0300367 /* legacy rate format, search for match in table */
368
369 u8 legacy_rate = rs_extract_rate(rate_n_flags);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100370 for (idx = 0; idx < ARRAY_SIZE(iwl_rates); idx++)
Eyal Shapirad310e402013-08-11 18:43:47 +0300371 if (iwl_rates[idx].plcp == legacy_rate)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100372 return idx;
373 }
374
Eyal Shapira560843f2014-01-05 21:04:19 +0200375 return IWL_RATE_INVALID;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100376}
377
378static void rs_rate_scale_perform(struct iwl_mvm *mvm,
379 struct sk_buff *skb,
380 struct ieee80211_sta *sta,
381 struct iwl_lq_sta *lq_sta);
Eyal Shapira8fc7c582013-12-04 02:15:46 +0200382static void rs_fill_lq_cmd(struct iwl_mvm *mvm,
383 struct ieee80211_sta *sta,
384 struct iwl_lq_sta *lq_sta,
385 const struct rs_rate *initial_rate);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100386static void rs_stay_in_table(struct iwl_lq_sta *lq_sta, bool force_search);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100387
Johannes Berg8ca151b2013-01-24 14:25:36 +0100388/**
389 * The following tables contain the expected throughput metrics for all rates
390 *
391 * 1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
392 *
393 * where invalid entries are zeros.
394 *
395 * CCK rates are only valid in legacy table and will only be used in G
396 * (2.4 GHz) band.
397 */
398
Johannes Berga34529e2014-01-20 22:57:21 +0100399static const u16 expected_tpt_legacy[IWL_RATE_COUNT] = {
Eyal Shapira4e82dd32013-08-04 23:58:37 +0300400 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 0, 0, 0
Johannes Berg8ca151b2013-01-24 14:25:36 +0100401};
402
Eyal Shapira4e82dd32013-08-04 23:58:37 +0300403/* Expected TpT tables. 4 indexes:
404 * 0 - NGI, 1 - SGI, 2 - AGG+NGI, 3 - AGG+SGI
405 */
Johannes Berga34529e2014-01-20 22:57:21 +0100406static const u16 expected_tpt_siso_20MHz[4][IWL_RATE_COUNT] = {
Eyal Shapira4e82dd32013-08-04 23:58:37 +0300407 {0, 0, 0, 0, 42, 0, 76, 102, 124, 159, 183, 193, 202, 216, 0},
408 {0, 0, 0, 0, 46, 0, 82, 110, 132, 168, 192, 202, 210, 225, 0},
409 {0, 0, 0, 0, 49, 0, 97, 145, 192, 285, 375, 420, 464, 551, 0},
410 {0, 0, 0, 0, 54, 0, 108, 160, 213, 315, 415, 465, 513, 608, 0},
Johannes Berg8ca151b2013-01-24 14:25:36 +0100411};
412
Johannes Berga34529e2014-01-20 22:57:21 +0100413static const u16 expected_tpt_siso_40MHz[4][IWL_RATE_COUNT] = {
Eyal Shapira4e82dd32013-08-04 23:58:37 +0300414 {0, 0, 0, 0, 77, 0, 127, 160, 184, 220, 242, 250, 257, 269, 275},
415 {0, 0, 0, 0, 83, 0, 135, 169, 193, 229, 250, 257, 264, 275, 280},
416 {0, 0, 0, 0, 101, 0, 199, 295, 389, 570, 744, 828, 911, 1070, 1173},
417 {0, 0, 0, 0, 112, 0, 220, 326, 429, 629, 819, 912, 1000, 1173, 1284},
Johannes Berg8ca151b2013-01-24 14:25:36 +0100418};
419
Johannes Berga34529e2014-01-20 22:57:21 +0100420static const u16 expected_tpt_siso_80MHz[4][IWL_RATE_COUNT] = {
Eyal Shapirad310e402013-08-11 18:43:47 +0300421 {0, 0, 0, 0, 130, 0, 191, 223, 244, 273, 288, 294, 298, 305, 308},
422 {0, 0, 0, 0, 138, 0, 200, 231, 251, 279, 293, 298, 302, 308, 312},
423 {0, 0, 0, 0, 217, 0, 429, 634, 834, 1220, 1585, 1760, 1931, 2258, 2466},
424 {0, 0, 0, 0, 241, 0, 475, 701, 921, 1343, 1741, 1931, 2117, 2468, 2691},
425};
426
Johannes Berga34529e2014-01-20 22:57:21 +0100427static const u16 expected_tpt_mimo2_20MHz[4][IWL_RATE_COUNT] = {
Eyal Shapira4e82dd32013-08-04 23:58:37 +0300428 {0, 0, 0, 0, 74, 0, 123, 155, 179, 213, 235, 243, 250, 261, 0},
429 {0, 0, 0, 0, 81, 0, 131, 164, 187, 221, 242, 250, 256, 267, 0},
430 {0, 0, 0, 0, 98, 0, 193, 286, 375, 550, 718, 799, 878, 1032, 0},
431 {0, 0, 0, 0, 109, 0, 214, 316, 414, 607, 790, 879, 965, 1132, 0},
Johannes Berg8ca151b2013-01-24 14:25:36 +0100432};
433
Johannes Berga34529e2014-01-20 22:57:21 +0100434static const u16 expected_tpt_mimo2_40MHz[4][IWL_RATE_COUNT] = {
Eyal Shapira4e82dd32013-08-04 23:58:37 +0300435 {0, 0, 0, 0, 123, 0, 182, 214, 235, 264, 279, 285, 289, 296, 300},
436 {0, 0, 0, 0, 131, 0, 191, 222, 242, 270, 284, 289, 293, 300, 303},
437 {0, 0, 0, 0, 200, 0, 390, 571, 741, 1067, 1365, 1505, 1640, 1894, 2053},
438 {0, 0, 0, 0, 221, 0, 430, 630, 816, 1169, 1490, 1641, 1784, 2053, 2221},
Johannes Berg8ca151b2013-01-24 14:25:36 +0100439};
440
Johannes Berga34529e2014-01-20 22:57:21 +0100441static const u16 expected_tpt_mimo2_80MHz[4][IWL_RATE_COUNT] = {
Eyal Shapirad310e402013-08-11 18:43:47 +0300442 {0, 0, 0, 0, 182, 0, 240, 264, 278, 299, 308, 311, 313, 317, 319},
443 {0, 0, 0, 0, 190, 0, 247, 269, 282, 302, 310, 313, 315, 319, 320},
444 {0, 0, 0, 0, 428, 0, 833, 1215, 1577, 2254, 2863, 3147, 3418, 3913, 4219},
445 {0, 0, 0, 0, 474, 0, 920, 1338, 1732, 2464, 3116, 3418, 3705, 4225, 4545},
446};
447
Johannes Berg8ca151b2013-01-24 14:25:36 +0100448/* mbps, mcs */
449static const struct iwl_rate_mcs_info iwl_rate_mcs[IWL_RATE_COUNT] = {
450 { "1", "BPSK DSSS"},
451 { "2", "QPSK DSSS"},
452 {"5.5", "BPSK CCK"},
453 { "11", "QPSK CCK"},
454 { "6", "BPSK 1/2"},
455 { "9", "BPSK 1/2"},
456 { "12", "QPSK 1/2"},
457 { "18", "QPSK 3/4"},
458 { "24", "16QAM 1/2"},
459 { "36", "16QAM 3/4"},
460 { "48", "64QAM 2/3"},
461 { "54", "64QAM 3/4"},
462 { "60", "64QAM 5/6"},
463};
464
465#define MCS_INDEX_PER_STREAM (8)
466
Emmanuel Grumbach9d108492013-12-03 11:50:30 +0200467static const char *rs_pretty_ant(u8 ant)
468{
469 static const char * const ant_name[] = {
470 [ANT_NONE] = "None",
471 [ANT_A] = "A",
472 [ANT_B] = "B",
473 [ANT_AB] = "AB",
474 [ANT_C] = "C",
475 [ANT_AC] = "AC",
476 [ANT_BC] = "BC",
477 [ANT_ABC] = "ABC",
478 };
479
480 if (ant > ANT_ABC)
481 return "UNKNOWN";
482
483 return ant_name[ant];
484}
485
486static const char *rs_pretty_lq_type(enum iwl_table_type type)
487{
488 static const char * const lq_types[] = {
489 [LQ_NONE] = "NONE",
490 [LQ_LEGACY_A] = "LEGACY_A",
491 [LQ_LEGACY_G] = "LEGACY_G",
492 [LQ_HT_SISO] = "HT SISO",
493 [LQ_HT_MIMO2] = "HT MIMO",
494 [LQ_VHT_SISO] = "VHT SISO",
495 [LQ_VHT_MIMO2] = "VHT MIMO",
496 };
497
498 if (type < LQ_NONE || type >= LQ_MAX)
499 return "UNKNOWN";
500
501 return lq_types[type];
502}
503
Eyal Shapira5aa33552013-11-23 01:06:36 +0200504static inline void rs_dump_rate(struct iwl_mvm *mvm, const struct rs_rate *rate,
505 const char *prefix)
506{
507 IWL_DEBUG_RATE(mvm, "%s: (%s: %d) ANT: %s BW: %d SGI: %d\n",
508 prefix, rs_pretty_lq_type(rate->type),
509 rate->index, rs_pretty_ant(rate->ant),
510 rate->bw, rate->sgi);
511}
512
Johannes Berg8ca151b2013-01-24 14:25:36 +0100513static void rs_rate_scale_clear_window(struct iwl_rate_scale_data *window)
514{
515 window->data = 0;
516 window->success_counter = 0;
517 window->success_ratio = IWL_INVALID_VALUE;
518 window->counter = 0;
519 window->average_tpt = IWL_INVALID_VALUE;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100520}
521
Eliad Peller3ca71f62014-03-10 16:33:43 +0200522static void rs_rate_scale_clear_tbl_windows(struct iwl_scale_tbl_info *tbl)
523{
524 int i;
525
526 for (i = 0; i < IWL_RATE_COUNT; i++)
527 rs_rate_scale_clear_window(&tbl->win[i]);
528}
529
Johannes Berg8ca151b2013-01-24 14:25:36 +0100530static inline u8 rs_is_valid_ant(u8 valid_antenna, u8 ant_type)
531{
532 return (ant_type & valid_antenna) == ant_type;
533}
534
Johannes Berg8ca151b2013-01-24 14:25:36 +0100535static int rs_tl_turn_on_agg_for_tid(struct iwl_mvm *mvm,
536 struct iwl_lq_sta *lq_data, u8 tid,
537 struct ieee80211_sta *sta)
538{
539 int ret = -EAGAIN;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100540
Emmanuel Grumbach44cc4292013-06-18 06:33:49 +0300541 IWL_DEBUG_HT(mvm, "Starting Tx agg: STA: %pM tid: %d\n",
542 sta->addr, tid);
543 ret = ieee80211_start_tx_ba_session(sta, tid, 5000);
544 if (ret == -EAGAIN) {
545 /*
546 * driver and mac80211 is out of sync
547 * this might be cause by reloading firmware
548 * stop the tx ba session here
549 */
550 IWL_ERR(mvm, "Fail start Tx agg on tid: %d\n",
551 tid);
552 ieee80211_stop_tx_ba_session(sta, tid);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100553 }
554 return ret;
555}
556
557static void rs_tl_turn_on_agg(struct iwl_mvm *mvm, u8 tid,
558 struct iwl_lq_sta *lq_data,
559 struct ieee80211_sta *sta)
560{
561 if (tid < IWL_MAX_TID_COUNT)
562 rs_tl_turn_on_agg_for_tid(mvm, lq_data, tid, sta);
563 else
564 IWL_ERR(mvm, "tid exceeds max TID count: %d/%d\n",
565 tid, IWL_MAX_TID_COUNT);
566}
567
568static inline int get_num_of_ant_from_rate(u32 rate_n_flags)
569{
570 return !!(rate_n_flags & RATE_MCS_ANT_A_MSK) +
571 !!(rate_n_flags & RATE_MCS_ANT_B_MSK) +
572 !!(rate_n_flags & RATE_MCS_ANT_C_MSK);
573}
574
575/*
576 * Static function to get the expected throughput from an iwl_scale_tbl_info
577 * that wraps a NULL pointer check
578 */
579static s32 get_expected_tpt(struct iwl_scale_tbl_info *tbl, int rs_index)
580{
581 if (tbl->expected_tpt)
582 return tbl->expected_tpt[rs_index];
583 return 0;
584}
585
586/**
587 * rs_collect_tx_data - Update the success/failure sliding window
588 *
589 * We keep a sliding window of the last 62 packets transmitted
590 * at this rate. window->data contains the bitmask of successful
591 * packets.
592 */
Eliad Peller0bd3c5a2014-03-10 16:07:04 +0200593static int _rs_collect_tx_data(struct iwl_scale_tbl_info *tbl,
594 int scale_index, int attempts, int successes,
595 struct iwl_rate_scale_data *window)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100596{
Johannes Berg8ca151b2013-01-24 14:25:36 +0100597 static const u64 mask = (((u64)1) << (IWL_RATE_MAX_WINDOW - 1));
598 s32 fail_count, tpt;
599
Johannes Berg8ca151b2013-01-24 14:25:36 +0100600 /* Get expected throughput */
601 tpt = get_expected_tpt(tbl, scale_index);
602
603 /*
604 * Keep track of only the latest 62 tx frame attempts in this rate's
605 * history window; anything older isn't really relevant any more.
606 * If we have filled up the sliding window, drop the oldest attempt;
607 * if the oldest attempt (highest bit in bitmap) shows "success",
608 * subtract "1" from the success counter (this is the main reason
609 * we keep these bitmaps!).
610 */
611 while (attempts > 0) {
612 if (window->counter >= IWL_RATE_MAX_WINDOW) {
613 /* remove earliest */
614 window->counter = IWL_RATE_MAX_WINDOW - 1;
615
616 if (window->data & mask) {
617 window->data &= ~mask;
618 window->success_counter--;
619 }
620 }
621
622 /* Increment frames-attempted counter */
623 window->counter++;
624
625 /* Shift bitmap by one frame to throw away oldest history */
626 window->data <<= 1;
627
628 /* Mark the most recent #successes attempts as successful */
629 if (successes > 0) {
630 window->success_counter++;
631 window->data |= 0x1;
632 successes--;
633 }
634
635 attempts--;
636 }
637
638 /* Calculate current success ratio, avoid divide-by-0! */
639 if (window->counter > 0)
640 window->success_ratio = 128 * (100 * window->success_counter)
641 / window->counter;
642 else
643 window->success_ratio = IWL_INVALID_VALUE;
644
645 fail_count = window->counter - window->success_counter;
646
647 /* Calculate average throughput, if we have enough history. */
648 if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
649 (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
650 window->average_tpt = (window->success_ratio * tpt + 64) / 128;
651 else
652 window->average_tpt = IWL_INVALID_VALUE;
653
Johannes Berg8ca151b2013-01-24 14:25:36 +0100654 return 0;
655}
656
Eliad Peller0bd3c5a2014-03-10 16:07:04 +0200657static int rs_collect_tx_data(struct iwl_scale_tbl_info *tbl,
658 int scale_index, int attempts, int successes)
659{
660 struct iwl_rate_scale_data *window = NULL;
661
662 if (scale_index < 0 || scale_index >= IWL_RATE_COUNT)
663 return -EINVAL;
664
665 /* Select window for current tx bit rate */
666 window = &(tbl->win[scale_index]);
667
668 return _rs_collect_tx_data(tbl, scale_index, attempts, successes,
669 window);
670}
671
Eyal Shapira5aa33552013-11-23 01:06:36 +0200672/* Convert rs_rate object into ucode rate bitmask */
673static u32 ucode_rate_from_rs_rate(struct iwl_mvm *mvm,
Eyal Shapira8fc7c582013-12-04 02:15:46 +0200674 struct rs_rate *rate)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100675{
Eyal Shapira5aa33552013-11-23 01:06:36 +0200676 u32 ucode_rate = 0;
677 int index = rate->index;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100678
Eyal Shapira5aa33552013-11-23 01:06:36 +0200679 ucode_rate |= ((rate->ant << RATE_MCS_ANT_POS) &
Eyal Shapirad310e402013-08-11 18:43:47 +0300680 RATE_MCS_ANT_ABC_MSK);
681
Eyal Shapira5aa33552013-11-23 01:06:36 +0200682 if (is_legacy(rate)) {
683 ucode_rate |= iwl_rates[index].plcp;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100684 if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
Eyal Shapira5aa33552013-11-23 01:06:36 +0200685 ucode_rate |= RATE_MCS_CCK_MSK;
686 return ucode_rate;
Eyal Shapirad310e402013-08-11 18:43:47 +0300687 }
688
Eyal Shapira5aa33552013-11-23 01:06:36 +0200689 if (is_ht(rate)) {
Eyal Shapirad310e402013-08-11 18:43:47 +0300690 if (index < IWL_FIRST_HT_RATE || index > IWL_LAST_HT_RATE) {
Johannes Berg8ca151b2013-01-24 14:25:36 +0100691 IWL_ERR(mvm, "Invalid HT rate index %d\n", index);
Eyal Shapira4e82dd32013-08-04 23:58:37 +0300692 index = IWL_LAST_HT_RATE;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100693 }
Eyal Shapira5aa33552013-11-23 01:06:36 +0200694 ucode_rate |= RATE_MCS_HT_MSK;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100695
Eyal Shapira5aa33552013-11-23 01:06:36 +0200696 if (is_ht_siso(rate))
697 ucode_rate |= iwl_rates[index].plcp_ht_siso;
698 else if (is_ht_mimo2(rate))
699 ucode_rate |= iwl_rates[index].plcp_ht_mimo2;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100700 else
Eyal Shapirad972ab32013-07-28 23:02:45 +0000701 WARN_ON_ONCE(1);
Eyal Shapira5aa33552013-11-23 01:06:36 +0200702 } else if (is_vht(rate)) {
Eyal Shapirad310e402013-08-11 18:43:47 +0300703 if (index < IWL_FIRST_VHT_RATE || index > IWL_LAST_VHT_RATE) {
704 IWL_ERR(mvm, "Invalid VHT rate index %d\n", index);
705 index = IWL_LAST_VHT_RATE;
706 }
Eyal Shapira5aa33552013-11-23 01:06:36 +0200707 ucode_rate |= RATE_MCS_VHT_MSK;
708 if (is_vht_siso(rate))
709 ucode_rate |= iwl_rates[index].plcp_vht_siso;
710 else if (is_vht_mimo2(rate))
711 ucode_rate |= iwl_rates[index].plcp_vht_mimo2;
Eyal Shapirad310e402013-08-11 18:43:47 +0300712 else
713 WARN_ON_ONCE(1);
714
Johannes Berg8ca151b2013-01-24 14:25:36 +0100715 } else {
Eyal Shapira5aa33552013-11-23 01:06:36 +0200716 IWL_ERR(mvm, "Invalid rate->type %d\n", rate->type);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100717 }
718
Eyal Shapira5aa33552013-11-23 01:06:36 +0200719 ucode_rate |= rate->bw;
720 if (rate->sgi)
721 ucode_rate |= RATE_MCS_SGI_MSK;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100722
Eyal Shapira5aa33552013-11-23 01:06:36 +0200723 return ucode_rate;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100724}
725
Eyal Shapira5aa33552013-11-23 01:06:36 +0200726/* Convert a ucode rate into an rs_rate object */
727static int rs_rate_from_ucode_rate(const u32 ucode_rate,
728 enum ieee80211_band band,
729 struct rs_rate *rate)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100730{
Eyal Shapira5aa33552013-11-23 01:06:36 +0200731 u32 ant_msk = ucode_rate & RATE_MCS_ANT_ABC_MSK;
732 u8 num_of_ant = get_num_of_ant_from_rate(ucode_rate);
Eyal Shapirad310e402013-08-11 18:43:47 +0300733 u8 nss;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100734
Emmanuel Grumbach9e898f12013-12-22 10:55:47 +0200735 memset(rate, 0, sizeof(*rate));
Eyal Shapira5aa33552013-11-23 01:06:36 +0200736 rate->index = iwl_hwrate_to_plcp_idx(ucode_rate);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100737
Eyal Shapira560843f2014-01-05 21:04:19 +0200738 if (rate->index == IWL_RATE_INVALID)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100739 return -EINVAL;
Eyal Shapira5aa33552013-11-23 01:06:36 +0200740
741 rate->ant = (ant_msk >> RATE_MCS_ANT_POS);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100742
Eyal Shapirad310e402013-08-11 18:43:47 +0300743 /* Legacy */
Eyal Shapira5aa33552013-11-23 01:06:36 +0200744 if (!(ucode_rate & RATE_MCS_HT_MSK) &&
745 !(ucode_rate & RATE_MCS_VHT_MSK)) {
Johannes Berg8ca151b2013-01-24 14:25:36 +0100746 if (num_of_ant == 1) {
747 if (band == IEEE80211_BAND_5GHZ)
Eyal Shapira5aa33552013-11-23 01:06:36 +0200748 rate->type = LQ_LEGACY_A;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100749 else
Eyal Shapira5aa33552013-11-23 01:06:36 +0200750 rate->type = LQ_LEGACY_G;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100751 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100752
Eyal Shapirad310e402013-08-11 18:43:47 +0300753 return 0;
754 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100755
Eyal Shapirad310e402013-08-11 18:43:47 +0300756 /* HT or VHT */
Eyal Shapira5aa33552013-11-23 01:06:36 +0200757 if (ucode_rate & RATE_MCS_SGI_MSK)
758 rate->sgi = true;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100759
Eyal Shapira5aa33552013-11-23 01:06:36 +0200760 rate->bw = ucode_rate & RATE_MCS_CHAN_WIDTH_MSK;
Eyal Shapirad310e402013-08-11 18:43:47 +0300761
Eyal Shapira5aa33552013-11-23 01:06:36 +0200762 if (ucode_rate & RATE_MCS_HT_MSK) {
763 nss = ((ucode_rate & RATE_HT_MCS_NSS_MSK) >>
Eyal Shapirad310e402013-08-11 18:43:47 +0300764 RATE_HT_MCS_NSS_POS) + 1;
765
766 if (nss == 1) {
Eyal Shapira5aa33552013-11-23 01:06:36 +0200767 rate->type = LQ_HT_SISO;
Eyal Shapirad310e402013-08-11 18:43:47 +0300768 WARN_ON_ONCE(num_of_ant != 1);
769 } else if (nss == 2) {
Eyal Shapira5aa33552013-11-23 01:06:36 +0200770 rate->type = LQ_HT_MIMO2;
Eyal Shapirad310e402013-08-11 18:43:47 +0300771 WARN_ON_ONCE(num_of_ant != 2);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100772 } else {
Eyal Shapirad310e402013-08-11 18:43:47 +0300773 WARN_ON_ONCE(1);
774 }
Eyal Shapira5aa33552013-11-23 01:06:36 +0200775 } else if (ucode_rate & RATE_MCS_VHT_MSK) {
776 nss = ((ucode_rate & RATE_VHT_MCS_NSS_MSK) >>
Eyal Shapirad310e402013-08-11 18:43:47 +0300777 RATE_VHT_MCS_NSS_POS) + 1;
778
779 if (nss == 1) {
Eyal Shapira5aa33552013-11-23 01:06:36 +0200780 rate->type = LQ_VHT_SISO;
Eyal Shapirad310e402013-08-11 18:43:47 +0300781 WARN_ON_ONCE(num_of_ant != 1);
782 } else if (nss == 2) {
Eyal Shapira5aa33552013-11-23 01:06:36 +0200783 rate->type = LQ_VHT_MIMO2;
Eyal Shapirad310e402013-08-11 18:43:47 +0300784 WARN_ON_ONCE(num_of_ant != 2);
785 } else {
786 WARN_ON_ONCE(1);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100787 }
788 }
Eyal Shapirad310e402013-08-11 18:43:47 +0300789
Eyal Shapira5aa33552013-11-23 01:06:36 +0200790 WARN_ON_ONCE(rate->bw == RATE_MCS_CHAN_WIDTH_160);
791 WARN_ON_ONCE(rate->bw == RATE_MCS_CHAN_WIDTH_80 &&
792 !is_vht(rate));
Eyal Shapirad310e402013-08-11 18:43:47 +0300793
Johannes Berg8ca151b2013-01-24 14:25:36 +0100794 return 0;
795}
796
797/* switch to another antenna/antennas and return 1 */
798/* if no other valid antenna found, return 0 */
Eyal Shapira8fc7c582013-12-04 02:15:46 +0200799static int rs_toggle_antenna(u32 valid_ant, struct rs_rate *rate)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100800{
801 u8 new_ant_type;
802
Eyal Shapira5aa33552013-11-23 01:06:36 +0200803 if (!rate->ant || rate->ant > ANT_ABC)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100804 return 0;
805
Eyal Shapira5aa33552013-11-23 01:06:36 +0200806 if (!rs_is_valid_ant(valid_ant, rate->ant))
Johannes Berg8ca151b2013-01-24 14:25:36 +0100807 return 0;
808
Eyal Shapira5aa33552013-11-23 01:06:36 +0200809 new_ant_type = ant_toggle_lookup[rate->ant];
Johannes Berg8ca151b2013-01-24 14:25:36 +0100810
Eyal Shapira5aa33552013-11-23 01:06:36 +0200811 while ((new_ant_type != rate->ant) &&
Johannes Berg8ca151b2013-01-24 14:25:36 +0100812 !rs_is_valid_ant(valid_ant, new_ant_type))
813 new_ant_type = ant_toggle_lookup[new_ant_type];
814
Eyal Shapira5aa33552013-11-23 01:06:36 +0200815 if (new_ant_type == rate->ant)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100816 return 0;
817
Eyal Shapira5aa33552013-11-23 01:06:36 +0200818 rate->ant = new_ant_type;
819
Johannes Berg8ca151b2013-01-24 14:25:36 +0100820 return 1;
821}
822
Johannes Berg8ca151b2013-01-24 14:25:36 +0100823static u16 rs_get_supported_rates(struct iwl_lq_sta *lq_sta,
Eyal Shapira5aa33552013-11-23 01:06:36 +0200824 struct rs_rate *rate)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100825{
Eyal Shapira5aa33552013-11-23 01:06:36 +0200826 if (is_legacy(rate))
Johannes Berg8ca151b2013-01-24 14:25:36 +0100827 return lq_sta->active_legacy_rate;
Eyal Shapira5aa33552013-11-23 01:06:36 +0200828 else if (is_siso(rate))
Eyal Shapirad310e402013-08-11 18:43:47 +0300829 return lq_sta->active_siso_rate;
Eyal Shapira5aa33552013-11-23 01:06:36 +0200830 else if (is_mimo2(rate))
Eyal Shapirad310e402013-08-11 18:43:47 +0300831 return lq_sta->active_mimo2_rate;
832
833 WARN_ON_ONCE(1);
834 return 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100835}
836
837static u16 rs_get_adjacent_rate(struct iwl_mvm *mvm, u8 index, u16 rate_mask,
838 int rate_type)
839{
840 u8 high = IWL_RATE_INVALID;
841 u8 low = IWL_RATE_INVALID;
842
843 /* 802.11A or ht walks to the next literal adjacent rate in
844 * the rate table */
Eyal Shapira5aa33552013-11-23 01:06:36 +0200845 if (is_type_a_band(rate_type) || !is_type_legacy(rate_type)) {
Johannes Berg8ca151b2013-01-24 14:25:36 +0100846 int i;
847 u32 mask;
848
849 /* Find the previous rate that is in the rate mask */
850 i = index - 1;
851 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
852 if (rate_mask & mask) {
853 low = i;
854 break;
855 }
856 }
857
858 /* Find the next rate that is in the rate mask */
859 i = index + 1;
860 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
861 if (rate_mask & mask) {
862 high = i;
863 break;
864 }
865 }
866
867 return (high << 8) | low;
868 }
869
870 low = index;
871 while (low != IWL_RATE_INVALID) {
872 low = iwl_rates[low].prev_rs;
873 if (low == IWL_RATE_INVALID)
874 break;
875 if (rate_mask & (1 << low))
876 break;
877 IWL_DEBUG_RATE(mvm, "Skipping masked lower rate: %d\n", low);
878 }
879
880 high = index;
881 while (high != IWL_RATE_INVALID) {
882 high = iwl_rates[high].next_rs;
883 if (high == IWL_RATE_INVALID)
884 break;
885 if (rate_mask & (1 << high))
886 break;
887 IWL_DEBUG_RATE(mvm, "Skipping masked higher rate: %d\n", high);
888 }
889
890 return (high << 8) | low;
891}
892
Eyal Shapirae07be6d2013-12-09 13:02:56 +0200893static inline bool rs_rate_supported(struct iwl_lq_sta *lq_sta,
894 struct rs_rate *rate)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100895{
Eyal Shapirae07be6d2013-12-09 13:02:56 +0200896 return BIT(rate->index) & rs_get_supported_rates(lq_sta, rate);
897}
898
899/* Get the next supported lower rate in the current column.
900 * Return true if bottom rate in the current column was reached
901 */
902static bool rs_get_lower_rate_in_column(struct iwl_lq_sta *lq_sta,
903 struct rs_rate *rate)
904{
905 u8 low;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100906 u16 high_low;
Eyal Shapirae07be6d2013-12-09 13:02:56 +0200907 u16 rate_mask;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100908 struct iwl_mvm *mvm = lq_sta->drv;
909
Eyal Shapirae07be6d2013-12-09 13:02:56 +0200910 rate_mask = rs_get_supported_rates(lq_sta, rate);
911 high_low = rs_get_adjacent_rate(mvm, rate->index, rate_mask,
912 rate->type);
913 low = high_low & 0xff;
914
915 /* Bottom rate of column reached */
916 if (low == IWL_RATE_INVALID)
917 return true;
918
919 rate->index = low;
920 return false;
921}
922
923/* Get the next rate to use following a column downgrade */
924static void rs_get_lower_rate_down_column(struct iwl_lq_sta *lq_sta,
925 struct rs_rate *rate)
926{
927 struct iwl_mvm *mvm = lq_sta->drv;
928
929 if (is_legacy(rate)) {
930 /* No column to downgrade from Legacy */
931 return;
932 } else if (is_siso(rate)) {
933 /* Downgrade to Legacy if we were in SISO */
Johannes Berg8ca151b2013-01-24 14:25:36 +0100934 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Eyal Shapira5aa33552013-11-23 01:06:36 +0200935 rate->type = LQ_LEGACY_A;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100936 else
Eyal Shapira5aa33552013-11-23 01:06:36 +0200937 rate->type = LQ_LEGACY_G;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100938
Eyal Shapiraa56db7d2013-11-28 12:35:42 +0200939 rate->bw = RATE_MCS_CHAN_WIDTH_20;
Eyal Shapirae07be6d2013-12-09 13:02:56 +0200940
Emmanuel Grumbachb8000402014-02-03 22:17:19 +0200941 WARN_ON_ONCE(rate->index < IWL_RATE_MCS_0_INDEX ||
Eyal Shapirae07be6d2013-12-09 13:02:56 +0200942 rate->index > IWL_RATE_MCS_9_INDEX);
943
944 rate->index = rs_ht_to_legacy[rate->index];
945 } else {
946 /* Downgrade to SISO with same MCS if in MIMO */
947 rate->type = is_vht_mimo2(rate) ?
948 LQ_VHT_SISO : LQ_HT_SISO;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100949 }
950
Johannes Berg8ca151b2013-01-24 14:25:36 +0100951
Eyal Shapirae07be6d2013-12-09 13:02:56 +0200952 if (num_of_ant(rate->ant) > 1)
Johannes Berg4ed735e2014-02-12 21:47:44 +0100953 rate->ant = first_antenna(mvm->fw->valid_tx_ant);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100954
Eyal Shapirae07be6d2013-12-09 13:02:56 +0200955 /* Relevant in both switching to SISO or Legacy */
956 rate->sgi = false;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100957
Eyal Shapirae07be6d2013-12-09 13:02:56 +0200958 if (!rs_rate_supported(lq_sta, rate))
959 rs_get_lower_rate_in_column(lq_sta, rate);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100960}
961
Eyal Shapira5aa33552013-11-23 01:06:36 +0200962/* Simple function to compare two rate scale table types */
963static inline bool rs_rate_match(struct rs_rate *a,
964 struct rs_rate *b)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100965{
Eyal Shapira5aa33552013-11-23 01:06:36 +0200966 return (a->type == b->type) && (a->ant == b->ant) && (a->sgi == b->sgi);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100967}
968
Eyal Shapirad310e402013-08-11 18:43:47 +0300969static u32 rs_ch_width_from_mac_flags(enum mac80211_rate_control_flags flags)
970{
971 if (flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
972 return RATE_MCS_CHAN_WIDTH_40;
973 else if (flags & IEEE80211_TX_RC_80_MHZ_WIDTH)
974 return RATE_MCS_CHAN_WIDTH_80;
975 else if (flags & IEEE80211_TX_RC_160_MHZ_WIDTH)
976 return RATE_MCS_CHAN_WIDTH_160;
977
978 return RATE_MCS_CHAN_WIDTH_20;
979}
980
Johannes Berg8ca151b2013-01-24 14:25:36 +0100981/*
982 * mac80211 sends us Tx status
983 */
984static void rs_tx_status(void *mvm_r, struct ieee80211_supported_band *sband,
985 struct ieee80211_sta *sta, void *priv_sta,
986 struct sk_buff *skb)
987{
988 int legacy_success;
989 int retries;
Eyal Shapira5aa33552013-11-23 01:06:36 +0200990 int mac_index, i;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100991 struct iwl_lq_sta *lq_sta = priv_sta;
992 struct iwl_lq_cmd *table;
993 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
994 struct iwl_op_mode *op_mode = (struct iwl_op_mode *)mvm_r;
995 struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
996 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
997 enum mac80211_rate_control_flags mac_flags;
Eyal Shapira5aa33552013-11-23 01:06:36 +0200998 u32 ucode_rate;
999 struct rs_rate rate;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001000 struct iwl_scale_tbl_info *curr_tbl, *other_tbl, *tmp_tbl;
1001
Johannes Berg8ca151b2013-01-24 14:25:36 +01001002 /* Treat uninitialized rate scaling data same as non-existing. */
1003 if (!lq_sta) {
1004 IWL_DEBUG_RATE(mvm, "Station rate scaling not created yet.\n");
1005 return;
1006 } else if (!lq_sta->drv) {
1007 IWL_DEBUG_RATE(mvm, "Rate scaling not initialized yet.\n");
1008 return;
1009 }
1010
Eyal Shapira87d5e412014-04-06 18:13:22 +03001011#ifdef CPTCFG_MAC80211_DEBUGFS
1012 /* Disable last tx check if we are debugging with fixed rate */
1013 if (lq_sta->dbg_fixed_rate) {
1014 IWL_DEBUG_RATE(mvm, "Fixed rate. avoid rate scaling\n");
1015 return;
1016 }
1017#endif
Johannes Berg8ca151b2013-01-24 14:25:36 +01001018 if (!ieee80211_is_data(hdr->frame_control) ||
1019 info->flags & IEEE80211_TX_CTL_NO_ACK)
1020 return;
1021
1022 /* This packet was aggregated but doesn't carry status info */
1023 if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
1024 !(info->flags & IEEE80211_TX_STAT_AMPDU))
1025 return;
1026
1027 /*
1028 * Ignore this Tx frame response if its initial rate doesn't match
1029 * that of latest Link Quality command. There may be stragglers
1030 * from a previous Link Quality command, but we're no longer interested
1031 * in those; they're either from the "active" mode while we're trying
1032 * to check "search" mode, or a prior "search" mode after we've moved
1033 * to a new "search" mode (which might become the new "active" mode).
1034 */
1035 table = &lq_sta->lq;
Eyal Shapira5aa33552013-11-23 01:06:36 +02001036 ucode_rate = le32_to_cpu(table->rs_table[0]);
1037 rs_rate_from_ucode_rate(ucode_rate, info->band, &rate);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001038 if (info->band == IEEE80211_BAND_5GHZ)
Eyal Shapira5aa33552013-11-23 01:06:36 +02001039 rate.index -= IWL_FIRST_OFDM_RATE;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001040 mac_flags = info->status.rates[0].flags;
1041 mac_index = info->status.rates[0].idx;
1042 /* For HT packets, map MCS to PLCP */
1043 if (mac_flags & IEEE80211_TX_RC_MCS) {
1044 /* Remove # of streams */
1045 mac_index &= RATE_HT_MCS_RATE_CODE_MSK;
1046 if (mac_index >= (IWL_RATE_9M_INDEX - IWL_FIRST_OFDM_RATE))
1047 mac_index++;
1048 /*
1049 * mac80211 HT index is always zero-indexed; we need to move
1050 * HT OFDM rates after CCK rates in 2.4 GHz band
1051 */
1052 if (info->band == IEEE80211_BAND_2GHZ)
1053 mac_index += IWL_FIRST_OFDM_RATE;
Eyal Shapirad310e402013-08-11 18:43:47 +03001054 } else if (mac_flags & IEEE80211_TX_RC_VHT_MCS) {
1055 mac_index &= RATE_VHT_MCS_RATE_CODE_MSK;
1056 if (mac_index >= (IWL_RATE_9M_INDEX - IWL_FIRST_OFDM_RATE))
1057 mac_index++;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001058 }
Eyal Shapirad310e402013-08-11 18:43:47 +03001059
Eyal Shapira87d5e412014-04-06 18:13:22 +03001060 if (time_after(jiffies,
1061 (unsigned long)(lq_sta->last_tx + RS_IDLE_TIMEOUT))) {
1062 int tid;
1063 IWL_DEBUG_RATE(mvm, "Tx idle for too long. reinit rs\n");
1064 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++)
1065 ieee80211_stop_tx_ba_session(sta, tid);
1066
1067 iwl_mvm_rs_rate_init(mvm, sta, sband->band, false);
1068 return;
1069 }
1070 lq_sta->last_tx = jiffies;
1071
Johannes Berg8ca151b2013-01-24 14:25:36 +01001072 /* Here we actually compare this rate to the latest LQ command */
1073 if ((mac_index < 0) ||
Eyal Shapira5aa33552013-11-23 01:06:36 +02001074 (rate.sgi != !!(mac_flags & IEEE80211_TX_RC_SHORT_GI)) ||
1075 (rate.bw != rs_ch_width_from_mac_flags(mac_flags)) ||
1076 (rate.ant != info->status.antenna) ||
1077 (!!(ucode_rate & RATE_MCS_HT_MSK) !=
Eyal Shapirad310e402013-08-11 18:43:47 +03001078 !!(mac_flags & IEEE80211_TX_RC_MCS)) ||
Eyal Shapira5aa33552013-11-23 01:06:36 +02001079 (!!(ucode_rate & RATE_MCS_VHT_MSK) !=
Eyal Shapirad310e402013-08-11 18:43:47 +03001080 !!(mac_flags & IEEE80211_TX_RC_VHT_MCS)) ||
Eyal Shapira5aa33552013-11-23 01:06:36 +02001081 (!!(ucode_rate & RATE_HT_MCS_GF_MSK) !=
Eyal Shapirad310e402013-08-11 18:43:47 +03001082 !!(mac_flags & IEEE80211_TX_RC_GREEN_FIELD)) ||
Eyal Shapira5aa33552013-11-23 01:06:36 +02001083 (rate.index != mac_index)) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001084 IWL_DEBUG_RATE(mvm,
1085 "initial rate %d does not match %d (0x%x)\n",
Eyal Shapira5aa33552013-11-23 01:06:36 +02001086 mac_index, rate.index, ucode_rate);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001087 /*
1088 * Since rates mis-match, the last LQ command may have failed.
1089 * After IWL_MISSED_RATE_MAX mis-matches, resync the uCode with
1090 * ... driver.
1091 */
1092 lq_sta->missed_rate_counter++;
1093 if (lq_sta->missed_rate_counter > IWL_MISSED_RATE_MAX) {
1094 lq_sta->missed_rate_counter = 0;
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001095 IWL_DEBUG_RATE(mvm,
1096 "Too many rates mismatch. Send sync LQ. rs_state %d\n",
1097 lq_sta->rs_state);
Eyal Shapira9e680942013-11-09 00:16:16 +02001098 iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, false);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001099 }
1100 /* Regardless, ignore this status info for outdated rate */
1101 return;
1102 } else
1103 /* Rate did match, so reset the missed_rate_counter */
1104 lq_sta->missed_rate_counter = 0;
1105
1106 /* Figure out if rate scale algorithm is in active or search table */
Eyal Shapira5aa33552013-11-23 01:06:36 +02001107 if (rs_rate_match(&rate,
1108 &(lq_sta->lq_info[lq_sta->active_tbl].rate))) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001109 curr_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1110 other_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
Eyal Shapira5aa33552013-11-23 01:06:36 +02001111 } else if (rs_rate_match(&rate,
1112 &lq_sta->lq_info[1 - lq_sta->active_tbl].rate)) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001113 curr_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
1114 other_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1115 } else {
1116 IWL_DEBUG_RATE(mvm,
1117 "Neither active nor search matches tx rate\n");
1118 tmp_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
Eyal Shapira5aa33552013-11-23 01:06:36 +02001119 rs_dump_rate(mvm, &tmp_tbl->rate, "ACTIVE");
Johannes Berg8ca151b2013-01-24 14:25:36 +01001120 tmp_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
Eyal Shapira5aa33552013-11-23 01:06:36 +02001121 rs_dump_rate(mvm, &tmp_tbl->rate, "SEARCH");
1122 rs_dump_rate(mvm, &rate, "ACTUAL");
1123
Johannes Berg8ca151b2013-01-24 14:25:36 +01001124 /*
1125 * no matching table found, let's by-pass the data collection
1126 * and continue to perform rate scale to find the rate table
1127 */
1128 rs_stay_in_table(lq_sta, true);
1129 goto done;
1130 }
1131
1132 /*
1133 * Updating the frame history depends on whether packets were
1134 * aggregated.
1135 *
1136 * For aggregation, all packets were transmitted at the same rate, the
1137 * first index into rate scale table.
1138 */
1139 if (info->flags & IEEE80211_TX_STAT_AMPDU) {
Eyal Shapira5aa33552013-11-23 01:06:36 +02001140 ucode_rate = le32_to_cpu(table->rs_table[0]);
1141 rs_rate_from_ucode_rate(ucode_rate, info->band, &rate);
1142 rs_collect_tx_data(curr_tbl, rate.index,
Johannes Berg8ca151b2013-01-24 14:25:36 +01001143 info->status.ampdu_len,
1144 info->status.ampdu_ack_len);
1145
1146 /* Update success/fail counts if not searching for new mode */
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001147 if (lq_sta->rs_state == RS_STATE_STAY_IN_COLUMN) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001148 lq_sta->total_success += info->status.ampdu_ack_len;
1149 lq_sta->total_failed += (info->status.ampdu_len -
1150 info->status.ampdu_ack_len);
1151 }
1152 } else {
1153 /*
1154 * For legacy, update frame history with for each Tx retry.
1155 */
1156 retries = info->status.rates[0].count - 1;
1157 /* HW doesn't send more than 15 retries */
1158 retries = min(retries, 15);
1159
1160 /* The last transmission may have been successful */
1161 legacy_success = !!(info->flags & IEEE80211_TX_STAT_ACK);
1162 /* Collect data for each rate used during failed TX attempts */
1163 for (i = 0; i <= retries; ++i) {
Eyal Shapira5aa33552013-11-23 01:06:36 +02001164 ucode_rate = le32_to_cpu(table->rs_table[i]);
1165 rs_rate_from_ucode_rate(ucode_rate, info->band, &rate);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001166 /*
1167 * Only collect stats if retried rate is in the same RS
1168 * table as active/search.
1169 */
Eyal Shapira5aa33552013-11-23 01:06:36 +02001170 if (rs_rate_match(&rate, &curr_tbl->rate))
Johannes Berg8ca151b2013-01-24 14:25:36 +01001171 tmp_tbl = curr_tbl;
Eyal Shapira5aa33552013-11-23 01:06:36 +02001172 else if (rs_rate_match(&rate, &other_tbl->rate))
Johannes Berg8ca151b2013-01-24 14:25:36 +01001173 tmp_tbl = other_tbl;
Eyal Shapira4107dbd2013-12-10 12:20:37 +02001174 else
Johannes Berg8ca151b2013-01-24 14:25:36 +01001175 continue;
Eyal Shapira4107dbd2013-12-10 12:20:37 +02001176
Eyal Shapira5aa33552013-11-23 01:06:36 +02001177 rs_collect_tx_data(tmp_tbl, rate.index, 1,
Johannes Berg8ca151b2013-01-24 14:25:36 +01001178 i < retries ? 0 : legacy_success);
1179 }
1180
1181 /* Update success/fail counts if not searching for new mode */
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001182 if (lq_sta->rs_state == RS_STATE_STAY_IN_COLUMN) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001183 lq_sta->total_success += legacy_success;
1184 lq_sta->total_failed += retries + (1 - legacy_success);
1185 }
1186 }
1187 /* The last TX rate is cached in lq_sta; it's set in if/else above */
Eyal Shapira5aa33552013-11-23 01:06:36 +02001188 lq_sta->last_rate_n_flags = ucode_rate;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001189done:
1190 /* See if there's a better rate or modulation mode to try. */
1191 if (sta && sta->supp_rates[sband->band])
1192 rs_rate_scale_perform(mvm, skb, sta, lq_sta);
1193}
1194
1195/*
1196 * Begin a period of staying with a selected modulation mode.
1197 * Set "stay_in_tbl" flag to prevent any mode switches.
1198 * Set frame tx success limits according to legacy vs. high-throughput,
1199 * and reset overall (spanning all rates) tx success history statistics.
1200 * These control how long we stay using same modulation mode before
1201 * searching for a new mode.
1202 */
1203static void rs_set_stay_in_table(struct iwl_mvm *mvm, u8 is_legacy,
1204 struct iwl_lq_sta *lq_sta)
1205{
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001206 IWL_DEBUG_RATE(mvm, "Moving to RS_STATE_STAY_IN_COLUMN\n");
1207 lq_sta->rs_state = RS_STATE_STAY_IN_COLUMN;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001208 if (is_legacy) {
1209 lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT;
1210 lq_sta->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
1211 lq_sta->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
1212 } else {
1213 lq_sta->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
1214 lq_sta->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
1215 lq_sta->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
1216 }
1217 lq_sta->table_count = 0;
1218 lq_sta->total_failed = 0;
1219 lq_sta->total_success = 0;
1220 lq_sta->flush_timer = jiffies;
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001221 lq_sta->visited_columns = 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001222}
1223
Eyal Shapira198266a2014-03-31 22:37:39 +03001224static int rs_get_max_allowed_rate(struct iwl_lq_sta *lq_sta,
1225 const struct rs_tx_column *column)
1226{
1227 switch (column->mode) {
1228 case RS_LEGACY:
1229 return lq_sta->max_legacy_rate_idx;
1230 case RS_SISO:
1231 return lq_sta->max_siso_rate_idx;
1232 case RS_MIMO2:
1233 return lq_sta->max_mimo2_rate_idx;
1234 default:
1235 WARN_ON_ONCE(1);
1236 }
1237
1238 return lq_sta->max_legacy_rate_idx;
1239}
1240
Johannes Berga34529e2014-01-20 22:57:21 +01001241static const u16 *rs_get_expected_tpt_table(struct iwl_lq_sta *lq_sta,
Eyal Shapira198266a2014-03-31 22:37:39 +03001242 const struct rs_tx_column *column,
1243 u32 bw)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001244{
1245 /* Used to choose among HT tables */
Johannes Berga34529e2014-01-20 22:57:21 +01001246 const u16 (*ht_tbl_pointer)[IWL_RATE_COUNT];
Johannes Berg8ca151b2013-01-24 14:25:36 +01001247
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001248 if (WARN_ON_ONCE(column->mode != RS_LEGACY &&
1249 column->mode != RS_SISO &&
1250 column->mode != RS_MIMO2))
1251 return expected_tpt_legacy;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001252
1253 /* Legacy rates have only one table */
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001254 if (column->mode == RS_LEGACY)
1255 return expected_tpt_legacy;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001256
Eyal Shapirad310e402013-08-11 18:43:47 +03001257 ht_tbl_pointer = expected_tpt_mimo2_20MHz;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001258 /* Choose among many HT tables depending on number of streams
Eyal Shapirad310e402013-08-11 18:43:47 +03001259 * (SISO/MIMO2), channel width (20/40/80), SGI, and aggregation
Johannes Berg8ca151b2013-01-24 14:25:36 +01001260 * status */
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001261 if (column->mode == RS_SISO) {
1262 switch (bw) {
Eyal Shapirad310e402013-08-11 18:43:47 +03001263 case RATE_MCS_CHAN_WIDTH_20:
1264 ht_tbl_pointer = expected_tpt_siso_20MHz;
1265 break;
1266 case RATE_MCS_CHAN_WIDTH_40:
1267 ht_tbl_pointer = expected_tpt_siso_40MHz;
1268 break;
1269 case RATE_MCS_CHAN_WIDTH_80:
1270 ht_tbl_pointer = expected_tpt_siso_80MHz;
1271 break;
1272 default:
1273 WARN_ON_ONCE(1);
1274 }
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001275 } else if (column->mode == RS_MIMO2) {
1276 switch (bw) {
Eyal Shapirad310e402013-08-11 18:43:47 +03001277 case RATE_MCS_CHAN_WIDTH_20:
1278 ht_tbl_pointer = expected_tpt_mimo2_20MHz;
1279 break;
1280 case RATE_MCS_CHAN_WIDTH_40:
1281 ht_tbl_pointer = expected_tpt_mimo2_40MHz;
1282 break;
1283 case RATE_MCS_CHAN_WIDTH_80:
1284 ht_tbl_pointer = expected_tpt_mimo2_80MHz;
1285 break;
1286 default:
1287 WARN_ON_ONCE(1);
1288 }
1289 } else {
1290 WARN_ON_ONCE(1);
Eyal Shapirad972ab32013-07-28 23:02:45 +00001291 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01001292
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001293 if (!column->sgi && !lq_sta->is_agg) /* Normal */
1294 return ht_tbl_pointer[0];
1295 else if (column->sgi && !lq_sta->is_agg) /* SGI */
1296 return ht_tbl_pointer[1];
1297 else if (!column->sgi && lq_sta->is_agg) /* AGG */
1298 return ht_tbl_pointer[2];
Johannes Berg8ca151b2013-01-24 14:25:36 +01001299 else /* AGG+SGI */
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001300 return ht_tbl_pointer[3];
1301}
1302
1303static void rs_set_expected_tpt_table(struct iwl_lq_sta *lq_sta,
1304 struct iwl_scale_tbl_info *tbl)
1305{
1306 struct rs_rate *rate = &tbl->rate;
1307 const struct rs_tx_column *column = &rs_tx_columns[tbl->column];
1308
1309 tbl->expected_tpt = rs_get_expected_tpt_table(lq_sta, column, rate->bw);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001310}
1311
1312/*
1313 * Find starting rate for new "search" high-throughput mode of modulation.
1314 * Goal is to find lowest expected rate (under perfect conditions) that is
1315 * above the current measured throughput of "active" mode, to give new mode
1316 * a fair chance to prove itself without too many challenges.
1317 *
1318 * This gets called when transitioning to more aggressive modulation
1319 * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
1320 * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
1321 * to decrease to match "active" throughput. When moving from MIMO to SISO,
1322 * bit rate will typically need to increase, but not if performance was bad.
1323 */
1324static s32 rs_get_best_rate(struct iwl_mvm *mvm,
1325 struct iwl_lq_sta *lq_sta,
1326 struct iwl_scale_tbl_info *tbl, /* "search" */
1327 u16 rate_mask, s8 index)
1328{
1329 /* "active" values */
1330 struct iwl_scale_tbl_info *active_tbl =
1331 &(lq_sta->lq_info[lq_sta->active_tbl]);
1332 s32 active_sr = active_tbl->win[index].success_ratio;
1333 s32 active_tpt = active_tbl->expected_tpt[index];
Johannes Berg8ca151b2013-01-24 14:25:36 +01001334 /* expected "search" throughput */
Johannes Berga34529e2014-01-20 22:57:21 +01001335 const u16 *tpt_tbl = tbl->expected_tpt;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001336
1337 s32 new_rate, high, low, start_hi;
1338 u16 high_low;
1339 s8 rate = index;
1340
1341 new_rate = high = low = start_hi = IWL_RATE_INVALID;
1342
1343 while (1) {
1344 high_low = rs_get_adjacent_rate(mvm, rate, rate_mask,
Eyal Shapira5aa33552013-11-23 01:06:36 +02001345 tbl->rate.type);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001346
1347 low = high_low & 0xff;
1348 high = (high_low >> 8) & 0xff;
1349
1350 /*
1351 * Lower the "search" bit rate, to give new "search" mode
1352 * approximately the same throughput as "active" if:
1353 *
1354 * 1) "Active" mode has been working modestly well (but not
1355 * great), and expected "search" throughput (under perfect
1356 * conditions) at candidate rate is above the actual
1357 * measured "active" throughput (but less than expected
1358 * "active" throughput under perfect conditions).
1359 * OR
1360 * 2) "Active" mode has been working perfectly or very well
1361 * and expected "search" throughput (under perfect
1362 * conditions) at candidate rate is above expected
1363 * "active" throughput (under perfect conditions).
1364 */
1365 if ((((100 * tpt_tbl[rate]) > lq_sta->last_tpt) &&
Eyal Shapirac48075d2013-11-21 01:18:47 +02001366 ((active_sr > RS_SR_FORCE_DECREASE) &&
Johannes Berg8ca151b2013-01-24 14:25:36 +01001367 (active_sr <= IWL_RATE_HIGH_TH) &&
1368 (tpt_tbl[rate] <= active_tpt))) ||
1369 ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
1370 (tpt_tbl[rate] > active_tpt))) {
1371 /* (2nd or later pass)
1372 * If we've already tried to raise the rate, and are
1373 * now trying to lower it, use the higher rate. */
1374 if (start_hi != IWL_RATE_INVALID) {
1375 new_rate = start_hi;
1376 break;
1377 }
1378
1379 new_rate = rate;
1380
1381 /* Loop again with lower rate */
1382 if (low != IWL_RATE_INVALID)
1383 rate = low;
1384
1385 /* Lower rate not available, use the original */
1386 else
1387 break;
1388
1389 /* Else try to raise the "search" rate to match "active" */
1390 } else {
1391 /* (2nd or later pass)
1392 * If we've already tried to lower the rate, and are
1393 * now trying to raise it, use the lower rate. */
1394 if (new_rate != IWL_RATE_INVALID)
1395 break;
1396
1397 /* Loop again with higher rate */
1398 else if (high != IWL_RATE_INVALID) {
1399 start_hi = high;
1400 rate = high;
1401
1402 /* Higher rate not available, use the original */
1403 } else {
1404 new_rate = rate;
1405 break;
1406 }
1407 }
1408 }
1409
1410 return new_rate;
1411}
1412
Eyal Shapira5aa33552013-11-23 01:06:36 +02001413static u32 rs_bw_from_sta_bw(struct ieee80211_sta *sta)
Eyal Shapirad310e402013-08-11 18:43:47 +03001414{
1415 if (sta->bandwidth >= IEEE80211_STA_RX_BW_80)
Eyal Shapira5aa33552013-11-23 01:06:36 +02001416 return RATE_MCS_CHAN_WIDTH_80;
Eyal Shapirad310e402013-08-11 18:43:47 +03001417 else if (sta->bandwidth >= IEEE80211_STA_RX_BW_40)
Eyal Shapira5aa33552013-11-23 01:06:36 +02001418 return RATE_MCS_CHAN_WIDTH_40;
1419
1420 return RATE_MCS_CHAN_WIDTH_20;
Eyal Shapirad310e402013-08-11 18:43:47 +03001421}
1422
Johannes Berg8ca151b2013-01-24 14:25:36 +01001423/*
Johannes Berg8ca151b2013-01-24 14:25:36 +01001424 * Check whether we should continue using same modulation mode, or
1425 * begin search for a new mode, based on:
1426 * 1) # tx successes or failures while using this mode
1427 * 2) # times calling this function
1428 * 3) elapsed time in this mode (not used, for now)
1429 */
1430static void rs_stay_in_table(struct iwl_lq_sta *lq_sta, bool force_search)
1431{
1432 struct iwl_scale_tbl_info *tbl;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001433 int active_tbl;
1434 int flush_interval_passed = 0;
1435 struct iwl_mvm *mvm;
1436
1437 mvm = lq_sta->drv;
1438 active_tbl = lq_sta->active_tbl;
1439
1440 tbl = &(lq_sta->lq_info[active_tbl]);
1441
1442 /* If we've been disallowing search, see if we should now allow it */
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001443 if (lq_sta->rs_state == RS_STATE_STAY_IN_COLUMN) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001444 /* Elapsed time using current modulation mode */
1445 if (lq_sta->flush_timer)
1446 flush_interval_passed =
1447 time_after(jiffies,
1448 (unsigned long)(lq_sta->flush_timer +
Eyal Shapira32b01722013-11-22 09:41:38 +02001449 RS_STAY_IN_COLUMN_TIMEOUT));
Johannes Berg8ca151b2013-01-24 14:25:36 +01001450
1451 /*
1452 * Check if we should allow search for new modulation mode.
1453 * If many frames have failed or succeeded, or we've used
1454 * this same modulation for a long time, allow search, and
1455 * reset history stats that keep track of whether we should
1456 * allow a new search. Also (below) reset all bitmaps and
1457 * stats in active history.
1458 */
1459 if (force_search ||
1460 (lq_sta->total_failed > lq_sta->max_failure_limit) ||
1461 (lq_sta->total_success > lq_sta->max_success_limit) ||
1462 ((!lq_sta->search_better_tbl) &&
1463 (lq_sta->flush_timer) && (flush_interval_passed))) {
1464 IWL_DEBUG_RATE(mvm,
1465 "LQ: stay is expired %d %d %d\n",
1466 lq_sta->total_failed,
1467 lq_sta->total_success,
1468 flush_interval_passed);
1469
1470 /* Allow search for new mode */
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001471 lq_sta->rs_state = RS_STATE_SEARCH_CYCLE_STARTED;
1472 IWL_DEBUG_RATE(mvm,
1473 "Moving to RS_STATE_SEARCH_CYCLE_STARTED\n");
Johannes Berg8ca151b2013-01-24 14:25:36 +01001474 lq_sta->total_failed = 0;
1475 lq_sta->total_success = 0;
1476 lq_sta->flush_timer = 0;
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001477 /* mark the current column as visited */
1478 lq_sta->visited_columns = BIT(tbl->column);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001479 /*
1480 * Else if we've used this modulation mode enough repetitions
1481 * (regardless of elapsed time or success/failure), reset
1482 * history bitmaps and rate-specific stats for all rates in
1483 * active table.
1484 */
1485 } else {
1486 lq_sta->table_count++;
1487 if (lq_sta->table_count >=
1488 lq_sta->table_count_limit) {
1489 lq_sta->table_count = 0;
1490
1491 IWL_DEBUG_RATE(mvm,
1492 "LQ: stay in table clear win\n");
Eliad Peller3ca71f62014-03-10 16:33:43 +02001493 rs_rate_scale_clear_tbl_windows(tbl);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001494 }
1495 }
1496
1497 /* If transitioning to allow "search", reset all history
1498 * bitmaps and stats in active table (this will become the new
1499 * "search" table). */
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001500 if (lq_sta->rs_state == RS_STATE_SEARCH_CYCLE_STARTED) {
1501 IWL_DEBUG_RATE(mvm, "Clearing up window stats\n");
Eliad Peller3ca71f62014-03-10 16:33:43 +02001502 rs_rate_scale_clear_tbl_windows(tbl);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001503 }
1504 }
1505}
1506
1507/*
1508 * setup rate table in uCode
1509 */
1510static void rs_update_rate_tbl(struct iwl_mvm *mvm,
Emmanuel Grumbach9145d152013-07-18 08:45:41 +03001511 struct ieee80211_sta *sta,
Johannes Berg8ca151b2013-01-24 14:25:36 +01001512 struct iwl_lq_sta *lq_sta,
Eyal Shapira5aa33552013-11-23 01:06:36 +02001513 struct rs_rate *rate)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001514{
Eyal Shapira8fc7c582013-12-04 02:15:46 +02001515 rs_fill_lq_cmd(mvm, sta, lq_sta, rate);
Eyal Shapira9e680942013-11-09 00:16:16 +02001516 iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, false);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001517}
1518
Eyal Shapira977342b2013-07-28 23:02:44 +00001519static u8 rs_get_tid(struct iwl_lq_sta *lq_data,
1520 struct ieee80211_hdr *hdr)
1521{
1522 u8 tid = IWL_MAX_TID_COUNT;
1523
1524 if (ieee80211_is_data_qos(hdr->frame_control)) {
1525 u8 *qc = ieee80211_get_qos_ctl(hdr);
1526 tid = qc[0] & 0xf;
1527 }
1528
1529 if (unlikely(tid > IWL_MAX_TID_COUNT))
1530 tid = IWL_MAX_TID_COUNT;
1531
1532 return tid;
1533}
1534
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001535static enum rs_column rs_get_next_column(struct iwl_mvm *mvm,
1536 struct iwl_lq_sta *lq_sta,
1537 struct ieee80211_sta *sta,
1538 struct iwl_scale_tbl_info *tbl)
1539{
Eyal Shapira198266a2014-03-31 22:37:39 +03001540 int i, j, max_rate;
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001541 enum rs_column next_col_id;
1542 const struct rs_tx_column *curr_col = &rs_tx_columns[tbl->column];
1543 const struct rs_tx_column *next_col;
1544 allow_column_func_t allow_func;
Johannes Berg4ed735e2014-02-12 21:47:44 +01001545 u8 valid_ants = mvm->fw->valid_tx_ant;
Johannes Berga34529e2014-01-20 22:57:21 +01001546 const u16 *expected_tpt_tbl;
Eyal Shapira198266a2014-03-31 22:37:39 +03001547 u16 tpt, max_expected_tpt;
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001548
1549 for (i = 0; i < MAX_NEXT_COLUMNS; i++) {
1550 next_col_id = curr_col->next_columns[i];
1551
1552 if (next_col_id == RS_COLUMN_INVALID)
1553 continue;
1554
1555 if (lq_sta->visited_columns & BIT(next_col_id)) {
1556 IWL_DEBUG_RATE(mvm, "Skip already visited column %d\n",
1557 next_col_id);
1558 continue;
1559 }
1560
1561 next_col = &rs_tx_columns[next_col_id];
1562
1563 if (!rs_is_valid_ant(valid_ants, next_col->ant)) {
1564 IWL_DEBUG_RATE(mvm,
1565 "Skip column %d as ANT config isn't supported by chip. valid_ants 0x%x column ant 0x%x\n",
1566 next_col_id, valid_ants, next_col->ant);
1567 continue;
1568 }
1569
1570 for (j = 0; j < MAX_COLUMN_CHECKS; j++) {
1571 allow_func = next_col->checks[j];
1572 if (allow_func && !allow_func(mvm, sta, tbl))
1573 break;
1574 }
1575
1576 if (j != MAX_COLUMN_CHECKS) {
1577 IWL_DEBUG_RATE(mvm,
1578 "Skip column %d: not allowed (check %d failed)\n",
1579 next_col_id, j);
1580
1581 continue;
1582 }
1583
1584 tpt = lq_sta->last_tpt / 100;
1585 expected_tpt_tbl = rs_get_expected_tpt_table(lq_sta, next_col,
1586 tbl->rate.bw);
1587 if (WARN_ON_ONCE(!expected_tpt_tbl))
1588 continue;
1589
Eyal Shapira198266a2014-03-31 22:37:39 +03001590 max_rate = rs_get_max_allowed_rate(lq_sta, next_col);
1591 if (WARN_ON_ONCE(max_rate == IWL_RATE_INVALID))
1592 continue;
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001593
Eyal Shapira198266a2014-03-31 22:37:39 +03001594 max_expected_tpt = expected_tpt_tbl[max_rate];
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001595 if (tpt >= max_expected_tpt) {
1596 IWL_DEBUG_RATE(mvm,
1597 "Skip column %d: can't beat current TPT. Max expected %d current %d\n",
1598 next_col_id, max_expected_tpt, tpt);
1599 continue;
1600 }
1601
Eyal Shapira198266a2014-03-31 22:37:39 +03001602 IWL_DEBUG_RATE(mvm,
1603 "Found potential column %d. Max expected %d current %d\n",
1604 next_col_id, max_expected_tpt, tpt);
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001605 break;
1606 }
1607
1608 if (i == MAX_NEXT_COLUMNS)
1609 return RS_COLUMN_INVALID;
1610
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001611 return next_col_id;
1612}
1613
1614static int rs_switch_to_column(struct iwl_mvm *mvm,
1615 struct iwl_lq_sta *lq_sta,
1616 struct ieee80211_sta *sta,
1617 enum rs_column col_id)
1618{
1619 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1620 struct iwl_scale_tbl_info *search_tbl =
1621 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1622 struct rs_rate *rate = &search_tbl->rate;
1623 const struct rs_tx_column *column = &rs_tx_columns[col_id];
1624 const struct rs_tx_column *curr_column = &rs_tx_columns[tbl->column];
1625 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1626 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1627 u16 rate_mask = 0;
1628 u32 rate_idx = 0;
1629
1630 memcpy(search_tbl, tbl, sz);
1631
1632 rate->sgi = column->sgi;
1633 rate->ant = column->ant;
1634
1635 if (column->mode == RS_LEGACY) {
1636 if (lq_sta->band == IEEE80211_BAND_5GHZ)
1637 rate->type = LQ_LEGACY_A;
1638 else
1639 rate->type = LQ_LEGACY_G;
1640
1641 rate_mask = lq_sta->active_legacy_rate;
1642 } else if (column->mode == RS_SISO) {
1643 rate->type = lq_sta->is_vht ? LQ_VHT_SISO : LQ_HT_SISO;
1644 rate_mask = lq_sta->active_siso_rate;
1645 } else if (column->mode == RS_MIMO2) {
1646 rate->type = lq_sta->is_vht ? LQ_VHT_MIMO2 : LQ_HT_MIMO2;
1647 rate_mask = lq_sta->active_mimo2_rate;
1648 } else {
1649 WARN_ON_ONCE("Bad column mode");
1650 }
1651
1652 rate->bw = rs_bw_from_sta_bw(sta);
1653 search_tbl->column = col_id;
1654 rs_set_expected_tpt_table(lq_sta, search_tbl);
1655
Eyal Shapira75d3e2d2014-01-07 18:19:35 +02001656 lq_sta->visited_columns |= BIT(col_id);
1657
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001658 /* Get the best matching rate if we're changing modes. e.g.
1659 * SISO->MIMO, LEGACY->SISO, MIMO->SISO
1660 */
1661 if (curr_column->mode != column->mode) {
1662 rate_idx = rs_get_best_rate(mvm, lq_sta, search_tbl,
1663 rate_mask, rate->index);
1664
1665 if ((rate_idx == IWL_RATE_INVALID) ||
1666 !(BIT(rate_idx) & rate_mask)) {
1667 IWL_DEBUG_RATE(mvm,
1668 "can not switch with index %d"
1669 " rate mask %x\n",
1670 rate_idx, rate_mask);
1671
1672 goto err;
1673 }
1674
1675 rate->index = rate_idx;
1676 }
1677
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001678 IWL_DEBUG_RATE(mvm, "Switched to column %d: Index %d\n",
1679 col_id, rate->index);
1680
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001681 return 0;
1682
1683err:
1684 rate->type = LQ_NONE;
1685 return -1;
1686}
1687
Eyal Shapira4e4b8152013-12-11 00:55:36 +02001688static enum rs_action rs_get_rate_action(struct iwl_mvm *mvm,
1689 struct iwl_scale_tbl_info *tbl,
1690 s32 sr, int low, int high,
1691 int current_tpt,
1692 int low_tpt, int high_tpt)
1693{
1694 enum rs_action action = RS_ACTION_STAY;
1695
Eyal Shapira4e4b8152013-12-11 00:55:36 +02001696 if ((sr <= RS_SR_FORCE_DECREASE) || (current_tpt == 0)) {
1697 IWL_DEBUG_RATE(mvm,
Eyal Shapirae53839e2014-04-06 02:42:18 +03001698 "Decrease rate because of low SR\n");
1699 return RS_ACTION_DOWNSCALE;
1700 }
1701
1702 if ((low_tpt == IWL_INVALID_VALUE) &&
1703 (high_tpt == IWL_INVALID_VALUE) &&
1704 (high != IWL_RATE_INVALID)) {
1705 IWL_DEBUG_RATE(mvm,
1706 "No data about high/low rates. Increase rate\n");
1707 return RS_ACTION_UPSCALE;
1708 }
1709
1710 if ((high_tpt == IWL_INVALID_VALUE) &&
1711 (high != IWL_RATE_INVALID) &&
1712 (low_tpt != IWL_INVALID_VALUE) &&
1713 (low_tpt < current_tpt)) {
1714 IWL_DEBUG_RATE(mvm,
1715 "No data about high rate and low rate is worse. Increase rate\n");
1716 return RS_ACTION_UPSCALE;
1717 }
1718
1719 if ((high_tpt != IWL_INVALID_VALUE) &&
1720 (high_tpt > current_tpt)) {
1721 IWL_DEBUG_RATE(mvm,
1722 "Higher rate is better. Increate rate\n");
1723 return RS_ACTION_UPSCALE;
1724 }
1725
1726 if ((low_tpt != IWL_INVALID_VALUE) &&
1727 (high_tpt != IWL_INVALID_VALUE) &&
1728 (low_tpt < current_tpt) &&
1729 (high_tpt < current_tpt)) {
1730 IWL_DEBUG_RATE(mvm,
1731 "Both high and low are worse. Maintain rate\n");
1732 return RS_ACTION_STAY;
1733 }
1734
1735 if ((low_tpt != IWL_INVALID_VALUE) &&
1736 (low_tpt > current_tpt)) {
1737 IWL_DEBUG_RATE(mvm,
1738 "Lower rate is better\n");
Eyal Shapira4e4b8152013-12-11 00:55:36 +02001739 action = RS_ACTION_DOWNSCALE;
Eyal Shapirae53839e2014-04-06 02:42:18 +03001740 goto out;
1741 }
1742
1743 if ((low_tpt == IWL_INVALID_VALUE) &&
1744 (low != IWL_RATE_INVALID)) {
1745 IWL_DEBUG_RATE(mvm,
1746 "No data about lower rate\n");
1747 action = RS_ACTION_DOWNSCALE;
1748 goto out;
1749 }
1750
1751 IWL_DEBUG_RATE(mvm, "Maintain rate\n");
1752
1753out:
1754 if ((action == RS_ACTION_DOWNSCALE) && (low != IWL_RATE_INVALID)) {
1755 if (sr >= RS_SR_NO_DECREASE) {
Eyal Shapira4e4b8152013-12-11 00:55:36 +02001756 IWL_DEBUG_RATE(mvm,
Eyal Shapirae53839e2014-04-06 02:42:18 +03001757 "SR is above NO DECREASE. Avoid downscale\n");
Eyal Shapira4e4b8152013-12-11 00:55:36 +02001758 action = RS_ACTION_STAY;
Eyal Shapirae53839e2014-04-06 02:42:18 +03001759 } else if (current_tpt > (100 * tbl->expected_tpt[low])) {
1760 IWL_DEBUG_RATE(mvm,
1761 "Current TPT is higher than max expected in low rate. Avoid downscale\n");
1762 action = RS_ACTION_STAY;
1763 } else {
1764 IWL_DEBUG_RATE(mvm, "Decrease rate\n");
Eyal Shapira4e4b8152013-12-11 00:55:36 +02001765 }
1766 }
1767
Eyal Shapira4e4b8152013-12-11 00:55:36 +02001768 return action;
1769}
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001770
Johannes Berg8ca151b2013-01-24 14:25:36 +01001771/*
1772 * Do rate scaling and search for new modulation mode.
1773 */
1774static void rs_rate_scale_perform(struct iwl_mvm *mvm,
1775 struct sk_buff *skb,
1776 struct ieee80211_sta *sta,
1777 struct iwl_lq_sta *lq_sta)
1778{
1779 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1780 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1781 int low = IWL_RATE_INVALID;
1782 int high = IWL_RATE_INVALID;
1783 int index;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001784 struct iwl_rate_scale_data *window = NULL;
1785 int current_tpt = IWL_INVALID_VALUE;
1786 int low_tpt = IWL_INVALID_VALUE;
1787 int high_tpt = IWL_INVALID_VALUE;
1788 u32 fail_count;
Eyal Shapira4e4b8152013-12-11 00:55:36 +02001789 enum rs_action scale_action = RS_ACTION_STAY;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001790 u16 rate_mask;
1791 u8 update_lq = 0;
1792 struct iwl_scale_tbl_info *tbl, *tbl1;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001793 u8 active_tbl = 0;
1794 u8 done_search = 0;
1795 u16 high_low;
1796 s32 sr;
1797 u8 tid = IWL_MAX_TID_COUNT;
Eyal Shapira9d015a02013-11-21 01:12:11 +02001798 u8 prev_agg = lq_sta->is_agg;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001799 struct iwl_mvm_sta *sta_priv = (void *)sta->drv_priv;
1800 struct iwl_mvm_tid_data *tid_data;
Eyal Shapira5aa33552013-11-23 01:06:36 +02001801 struct rs_rate *rate;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001802
Johannes Berg8ca151b2013-01-24 14:25:36 +01001803 /* Send management frames and NO_ACK data using lowest rate. */
1804 /* TODO: this could probably be improved.. */
1805 if (!ieee80211_is_data(hdr->frame_control) ||
1806 info->flags & IEEE80211_TX_CTL_NO_ACK)
1807 return;
1808
Eyal Shapira977342b2013-07-28 23:02:44 +00001809 tid = rs_get_tid(lq_sta, hdr);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001810 if ((tid != IWL_MAX_TID_COUNT) &&
1811 (lq_sta->tx_agg_tid_en & (1 << tid))) {
1812 tid_data = &sta_priv->tid_data[tid];
1813 if (tid_data->state == IWL_AGG_OFF)
1814 lq_sta->is_agg = 0;
1815 else
1816 lq_sta->is_agg = 1;
1817 } else {
1818 lq_sta->is_agg = 0;
1819 }
1820
1821 /*
1822 * Select rate-scale / modulation-mode table to work with in
1823 * the rest of this function: "search" if searching for better
1824 * modulation mode, or "active" if doing rate scaling within a mode.
1825 */
1826 if (!lq_sta->search_better_tbl)
1827 active_tbl = lq_sta->active_tbl;
1828 else
1829 active_tbl = 1 - lq_sta->active_tbl;
1830
1831 tbl = &(lq_sta->lq_info[active_tbl]);
Eyal Shapira5aa33552013-11-23 01:06:36 +02001832 rate = &tbl->rate;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001833
Eyal Shapira9d015a02013-11-21 01:12:11 +02001834 if (prev_agg != lq_sta->is_agg) {
1835 IWL_DEBUG_RATE(mvm,
1836 "Aggregation changed: prev %d current %d. Update expected TPT table\n",
1837 prev_agg, lq_sta->is_agg);
1838 rs_set_expected_tpt_table(lq_sta, tbl);
1839 }
1840
Johannes Berg8ca151b2013-01-24 14:25:36 +01001841 /* current tx rate */
1842 index = lq_sta->last_txrate_idx;
1843
Johannes Berg8ca151b2013-01-24 14:25:36 +01001844 /* rates available for this association, and for modulation mode */
Eyal Shapiraa8ff14f2013-11-23 01:30:44 +02001845 rate_mask = rs_get_supported_rates(lq_sta, rate);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001846
Eyal Shapiraca6f38f2013-12-09 11:14:11 +02001847 if (!(BIT(index) & rate_mask)) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001848 IWL_ERR(mvm, "Current Rate is not valid\n");
1849 if (lq_sta->search_better_tbl) {
1850 /* revert to active table if search table is not valid*/
Eyal Shapira5aa33552013-11-23 01:06:36 +02001851 rate->type = LQ_NONE;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001852 lq_sta->search_better_tbl = 0;
1853 tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
Eyal Shapira5aa33552013-11-23 01:06:36 +02001854 rs_update_rate_tbl(mvm, sta, lq_sta, &tbl->rate);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001855 }
1856 return;
1857 }
1858
1859 /* Get expected throughput table and history window for current rate */
1860 if (!tbl->expected_tpt) {
1861 IWL_ERR(mvm, "tbl->expected_tpt is NULL\n");
1862 return;
1863 }
1864
1865 /* force user max rate if set by user */
1866 if ((lq_sta->max_rate_idx != -1) &&
1867 (lq_sta->max_rate_idx < index)) {
1868 index = lq_sta->max_rate_idx;
1869 update_lq = 1;
1870 window = &(tbl->win[index]);
Eyal Shapira4d30ee82013-11-20 01:45:38 +02001871 IWL_DEBUG_RATE(mvm,
1872 "Forcing user max rate %d\n",
1873 index);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001874 goto lq_update;
1875 }
1876
1877 window = &(tbl->win[index]);
1878
1879 /*
1880 * If there is not enough history to calculate actual average
1881 * throughput, keep analyzing results of more tx frames, without
1882 * changing rate or mode (bypass most of the rest of this function).
1883 * Set up new rate table in uCode only if old rate is not supported
1884 * in current association (use new rate found above).
1885 */
1886 fail_count = window->counter - window->success_counter;
1887 if ((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
1888 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH)) {
1889 IWL_DEBUG_RATE(mvm,
Eyal Shapira4d30ee82013-11-20 01:45:38 +02001890 "(%s: %d): Test Window: succ %d total %d\n",
Eyal Shapira5aa33552013-11-23 01:06:36 +02001891 rs_pretty_lq_type(rate->type),
Eyal Shapira4d30ee82013-11-20 01:45:38 +02001892 index, window->success_counter, window->counter);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001893
1894 /* Can't calculate this yet; not enough history */
1895 window->average_tpt = IWL_INVALID_VALUE;
1896
1897 /* Should we stay with this modulation mode,
1898 * or search for a new one? */
1899 rs_stay_in_table(lq_sta, false);
1900
1901 goto out;
1902 }
1903 /* Else we have enough samples; calculate estimate of
1904 * actual average throughput */
1905 if (window->average_tpt != ((window->success_ratio *
1906 tbl->expected_tpt[index] + 64) / 128)) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001907 window->average_tpt = ((window->success_ratio *
1908 tbl->expected_tpt[index] + 64) / 128);
1909 }
1910
1911 /* If we are searching for better modulation mode, check success. */
1912 if (lq_sta->search_better_tbl) {
1913 /* If good success, continue using the "search" mode;
1914 * no need to send new link quality command, since we're
1915 * continuing to use the setup that we've been trying. */
1916 if (window->average_tpt > lq_sta->last_tpt) {
1917 IWL_DEBUG_RATE(mvm,
Eyal Shapira4d30ee82013-11-20 01:45:38 +02001918 "SWITCHING TO NEW TABLE SR: %d "
1919 "cur-tpt %d old-tpt %d\n",
Johannes Berg8ca151b2013-01-24 14:25:36 +01001920 window->success_ratio,
1921 window->average_tpt,
1922 lq_sta->last_tpt);
1923
Johannes Berg8ca151b2013-01-24 14:25:36 +01001924 /* Swap tables; "search" becomes "active" */
1925 lq_sta->active_tbl = active_tbl;
1926 current_tpt = window->average_tpt;
1927 /* Else poor success; go back to mode in "active" table */
1928 } else {
1929 IWL_DEBUG_RATE(mvm,
Eyal Shapira4d30ee82013-11-20 01:45:38 +02001930 "GOING BACK TO THE OLD TABLE: SR %d "
1931 "cur-tpt %d old-tpt %d\n",
Johannes Berg8ca151b2013-01-24 14:25:36 +01001932 window->success_ratio,
1933 window->average_tpt,
1934 lq_sta->last_tpt);
1935
1936 /* Nullify "search" table */
Eyal Shapira5aa33552013-11-23 01:06:36 +02001937 rate->type = LQ_NONE;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001938
1939 /* Revert to "active" table */
1940 active_tbl = lq_sta->active_tbl;
1941 tbl = &(lq_sta->lq_info[active_tbl]);
1942
1943 /* Revert to "active" rate and throughput info */
Eyal Shapira8fc7c582013-12-04 02:15:46 +02001944 index = tbl->rate.index;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001945 current_tpt = lq_sta->last_tpt;
1946
1947 /* Need to set up a new rate table in uCode */
1948 update_lq = 1;
1949 }
1950
1951 /* Either way, we've made a decision; modulation mode
1952 * search is done, allow rate adjustment next time. */
1953 lq_sta->search_better_tbl = 0;
1954 done_search = 1; /* Don't switch modes below! */
1955 goto lq_update;
1956 }
1957
1958 /* (Else) not in search of better modulation mode, try for better
1959 * starting rate, while staying in this mode. */
Eyal Shapiraca6f38f2013-12-09 11:14:11 +02001960 high_low = rs_get_adjacent_rate(mvm, index, rate_mask, rate->type);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001961 low = high_low & 0xff;
1962 high = (high_low >> 8) & 0xff;
1963
1964 /* If user set max rate, dont allow higher than user constrain */
1965 if ((lq_sta->max_rate_idx != -1) &&
1966 (lq_sta->max_rate_idx < high))
1967 high = IWL_RATE_INVALID;
1968
1969 sr = window->success_ratio;
1970
1971 /* Collect measured throughputs for current and adjacent rates */
1972 current_tpt = window->average_tpt;
1973 if (low != IWL_RATE_INVALID)
1974 low_tpt = tbl->win[low].average_tpt;
1975 if (high != IWL_RATE_INVALID)
1976 high_tpt = tbl->win[high].average_tpt;
1977
Eyal Shapira4d30ee82013-11-20 01:45:38 +02001978 IWL_DEBUG_RATE(mvm,
1979 "(%s: %d): cur_tpt %d SR %d low %d high %d low_tpt %d high_tpt %d\n",
Eyal Shapira5aa33552013-11-23 01:06:36 +02001980 rs_pretty_lq_type(rate->type), index, current_tpt, sr,
1981 low, high, low_tpt, high_tpt);
Eyal Shapira4d30ee82013-11-20 01:45:38 +02001982
Eyal Shapira4e4b8152013-12-11 00:55:36 +02001983 scale_action = rs_get_rate_action(mvm, tbl, sr, low, high,
1984 current_tpt, low_tpt, high_tpt);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001985
Eyal Shapira393b9e52013-11-13 16:46:19 +02001986 /* Force a search in case BT doesn't like us being in MIMO */
Eyal Shapira5aa33552013-11-23 01:06:36 +02001987 if (is_mimo(rate) &&
Eyal Shapira393b9e52013-11-13 16:46:19 +02001988 !iwl_mvm_bt_coex_is_mimo_allowed(mvm, sta)) {
Eyal Shapira4d30ee82013-11-20 01:45:38 +02001989 IWL_DEBUG_RATE(mvm,
1990 "BT Coex forbids MIMO. Search for new config\n");
Emmanuel Grumbach36946ce2013-05-28 23:12:47 +03001991 rs_stay_in_table(lq_sta, true);
1992 goto lq_update;
1993 }
1994
Johannes Berg8ca151b2013-01-24 14:25:36 +01001995 switch (scale_action) {
Eyal Shapira4e4b8152013-12-11 00:55:36 +02001996 case RS_ACTION_DOWNSCALE:
Johannes Berg8ca151b2013-01-24 14:25:36 +01001997 /* Decrease starting rate, update uCode's rate table */
1998 if (low != IWL_RATE_INVALID) {
1999 update_lq = 1;
2000 index = low;
Eyal Shapira4d30ee82013-11-20 01:45:38 +02002001 } else {
2002 IWL_DEBUG_RATE(mvm,
2003 "At the bottom rate. Can't decrease\n");
Johannes Berg8ca151b2013-01-24 14:25:36 +01002004 }
2005
2006 break;
Eyal Shapira4e4b8152013-12-11 00:55:36 +02002007 case RS_ACTION_UPSCALE:
Johannes Berg8ca151b2013-01-24 14:25:36 +01002008 /* Increase starting rate, update uCode's rate table */
2009 if (high != IWL_RATE_INVALID) {
2010 update_lq = 1;
2011 index = high;
Eyal Shapira4d30ee82013-11-20 01:45:38 +02002012 } else {
2013 IWL_DEBUG_RATE(mvm,
2014 "At the top rate. Can't increase\n");
Johannes Berg8ca151b2013-01-24 14:25:36 +01002015 }
2016
2017 break;
Eyal Shapira4e4b8152013-12-11 00:55:36 +02002018 case RS_ACTION_STAY:
Johannes Berg8ca151b2013-01-24 14:25:36 +01002019 /* No change */
2020 default:
2021 break;
2022 }
2023
Johannes Berg8ca151b2013-01-24 14:25:36 +01002024lq_update:
2025 /* Replace uCode's rate table for the destination station. */
Eyal Shapira5aa33552013-11-23 01:06:36 +02002026 if (update_lq) {
2027 tbl->rate.index = index;
2028 rs_update_rate_tbl(mvm, sta, lq_sta, &tbl->rate);
2029 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01002030
2031 rs_stay_in_table(lq_sta, false);
2032
2033 /*
2034 * Search for new modulation mode if we're:
2035 * 1) Not changing rates right now
2036 * 2) Not just finishing up a search
2037 * 3) Allowing a new search
2038 */
2039 if (!update_lq && !done_search &&
Eyal Shapirab3b06a32013-11-24 21:30:13 +02002040 lq_sta->rs_state == RS_STATE_SEARCH_CYCLE_STARTED
2041 && window->counter) {
2042 enum rs_column next_column;
2043
Johannes Berg8ca151b2013-01-24 14:25:36 +01002044 /* Save current throughput to compare with "search" throughput*/
2045 lq_sta->last_tpt = current_tpt;
2046
Eyal Shapira4d30ee82013-11-20 01:45:38 +02002047 IWL_DEBUG_RATE(mvm,
Eyal Shapirab3b06a32013-11-24 21:30:13 +02002048 "Start Search: update_lq %d done_search %d rs_state %d win->counter %d\n",
2049 update_lq, done_search, lq_sta->rs_state,
Eyal Shapira4d30ee82013-11-20 01:45:38 +02002050 window->counter);
Eyal Shapirab3b06a32013-11-24 21:30:13 +02002051
2052 next_column = rs_get_next_column(mvm, lq_sta, sta, tbl);
2053 if (next_column != RS_COLUMN_INVALID) {
2054 int ret = rs_switch_to_column(mvm, lq_sta, sta,
2055 next_column);
2056 if (!ret)
2057 lq_sta->search_better_tbl = 1;
2058 } else {
2059 IWL_DEBUG_RATE(mvm,
2060 "No more columns to explore in search cycle. Go to RS_STATE_SEARCH_CYCLE_ENDED\n");
2061 lq_sta->rs_state = RS_STATE_SEARCH_CYCLE_ENDED;
2062 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01002063
2064 /* If new "search" mode was selected, set up in uCode table */
2065 if (lq_sta->search_better_tbl) {
2066 /* Access the "search" table, clear its history. */
2067 tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Eliad Peller3ca71f62014-03-10 16:33:43 +02002068 rs_rate_scale_clear_tbl_windows(tbl);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002069
2070 /* Use new "search" start rate */
Eyal Shapira8fc7c582013-12-04 02:15:46 +02002071 index = tbl->rate.index;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002072
Eyal Shapirab3b06a32013-11-24 21:30:13 +02002073 rs_dump_rate(mvm, &tbl->rate,
2074 "Switch to SEARCH TABLE:");
Eyal Shapira8fc7c582013-12-04 02:15:46 +02002075 rs_fill_lq_cmd(mvm, sta, lq_sta, &tbl->rate);
Eyal Shapira9e680942013-11-09 00:16:16 +02002076 iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, false);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002077 } else {
2078 done_search = 1;
2079 }
2080 }
2081
Eyal Shapirab3b06a32013-11-24 21:30:13 +02002082 if (done_search && lq_sta->rs_state == RS_STATE_SEARCH_CYCLE_ENDED) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002083 /* If the "active" (non-search) mode was legacy,
2084 * and we've tried switching antennas,
2085 * but we haven't been able to try HT modes (not available),
2086 * stay with best antenna legacy modulation for a while
2087 * before next round of mode comparisons. */
2088 tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
Eyal Shapirafd7dbee2014-04-06 04:39:23 +03002089 if (is_legacy(&tbl1->rate)) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002090 IWL_DEBUG_RATE(mvm, "LQ: STAY in legacy table\n");
Eyal Shapirafd7dbee2014-04-06 04:39:23 +03002091
2092 if (tid != IWL_MAX_TID_COUNT) {
2093 tid_data = &sta_priv->tid_data[tid];
2094 if (tid_data->state != IWL_AGG_OFF) {
2095 IWL_DEBUG_RATE(mvm,
2096 "Stop aggregation on tid %d\n",
2097 tid);
2098 ieee80211_stop_tx_ba_session(sta, tid);
2099 }
2100 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01002101 rs_set_stay_in_table(mvm, 1, lq_sta);
Eyal Shapirab3b06a32013-11-24 21:30:13 +02002102 } else {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002103 /* If we're in an HT mode, and all 3 mode switch actions
2104 * have been tried and compared, stay in this best modulation
2105 * mode for a while before next round of mode comparisons. */
Johannes Berg8ca151b2013-01-24 14:25:36 +01002106 if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
2107 (lq_sta->tx_agg_tid_en & (1 << tid)) &&
2108 (tid != IWL_MAX_TID_COUNT)) {
2109 tid_data = &sta_priv->tid_data[tid];
2110 if (tid_data->state == IWL_AGG_OFF) {
2111 IWL_DEBUG_RATE(mvm,
2112 "try to aggregate tid %d\n",
2113 tid);
2114 rs_tl_turn_on_agg(mvm, tid,
2115 lq_sta, sta);
2116 }
2117 }
2118 rs_set_stay_in_table(mvm, 0, lq_sta);
2119 }
2120 }
2121
2122out:
Johannes Berg8ca151b2013-01-24 14:25:36 +01002123 lq_sta->last_txrate_idx = index;
2124}
2125
2126/**
2127 * rs_initialize_lq - Initialize a station's hardware rate table
2128 *
2129 * The uCode's station table contains a table of fallback rates
2130 * for automatic fallback during transmission.
2131 *
2132 * NOTE: This sets up a default set of values. These will be replaced later
2133 * if the driver's iwl-agn-rs rate scaling algorithm is used, instead of
2134 * rc80211_simple.
2135 *
2136 * NOTE: Run REPLY_ADD_STA command to set up station table entry, before
2137 * calling this function (which runs REPLY_TX_LINK_QUALITY_CMD,
2138 * which requires station table entry to exist).
2139 */
2140static void rs_initialize_lq(struct iwl_mvm *mvm,
2141 struct ieee80211_sta *sta,
2142 struct iwl_lq_sta *lq_sta,
Eyal Shapirab87c2172013-11-09 23:37:55 +02002143 enum ieee80211_band band,
2144 bool init)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002145{
2146 struct iwl_scale_tbl_info *tbl;
Eyal Shapira5aa33552013-11-23 01:06:36 +02002147 struct rs_rate *rate;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002148 int i;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002149 u8 active_tbl = 0;
2150 u8 valid_tx_ant;
2151
2152 if (!sta || !lq_sta)
2153 return;
2154
2155 i = lq_sta->last_txrate_idx;
2156
Johannes Berg4ed735e2014-02-12 21:47:44 +01002157 valid_tx_ant = mvm->fw->valid_tx_ant;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002158
2159 if (!lq_sta->search_better_tbl)
2160 active_tbl = lq_sta->active_tbl;
2161 else
2162 active_tbl = 1 - lq_sta->active_tbl;
2163
2164 tbl = &(lq_sta->lq_info[active_tbl]);
Eyal Shapira5aa33552013-11-23 01:06:36 +02002165 rate = &tbl->rate;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002166
2167 if ((i < 0) || (i >= IWL_RATE_COUNT))
2168 i = 0;
2169
Eyal Shapirab3b06a32013-11-24 21:30:13 +02002170 rate->index = i;
Eyal Shapira5aa33552013-11-23 01:06:36 +02002171 rate->ant = first_antenna(valid_tx_ant);
Eyal Shapirab3b06a32013-11-24 21:30:13 +02002172 rate->sgi = false;
2173 rate->bw = RATE_MCS_CHAN_WIDTH_20;
2174 if (band == IEEE80211_BAND_5GHZ)
2175 rate->type = LQ_LEGACY_A;
2176 else
2177 rate->type = LQ_LEGACY_G;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002178
Eyal Shapirab3b06a32013-11-24 21:30:13 +02002179 WARN_ON_ONCE(rate->ant != ANT_A && rate->ant != ANT_B);
2180 if (rate->ant == ANT_A)
2181 tbl->column = RS_COLUMN_LEGACY_ANT_A;
2182 else
2183 tbl->column = RS_COLUMN_LEGACY_ANT_B;
2184
Johannes Berg8ca151b2013-01-24 14:25:36 +01002185 rs_set_expected_tpt_table(lq_sta, tbl);
Eyal Shapira80e9e5c2013-12-23 16:26:41 +02002186 rs_fill_lq_cmd(mvm, sta, lq_sta, rate);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002187 /* TODO restore station should remember the lq cmd */
Eyal Shapirab87c2172013-11-09 23:37:55 +02002188 iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, init);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002189}
2190
2191static void rs_get_rate(void *mvm_r, struct ieee80211_sta *sta, void *mvm_sta,
2192 struct ieee80211_tx_rate_control *txrc)
2193{
2194 struct sk_buff *skb = txrc->skb;
2195 struct ieee80211_supported_band *sband = txrc->sband;
2196 struct iwl_op_mode *op_mode __maybe_unused =
2197 (struct iwl_op_mode *)mvm_r;
2198 struct iwl_mvm *mvm __maybe_unused = IWL_OP_MODE_GET_MVM(op_mode);
2199 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2200 struct iwl_lq_sta *lq_sta = mvm_sta;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002201
Johannes Berg8ca151b2013-01-24 14:25:36 +01002202 /* Get max rate if user set max rate */
2203 if (lq_sta) {
2204 lq_sta->max_rate_idx = txrc->max_rate_idx;
2205 if ((sband->band == IEEE80211_BAND_5GHZ) &&
2206 (lq_sta->max_rate_idx != -1))
2207 lq_sta->max_rate_idx += IWL_FIRST_OFDM_RATE;
2208 if ((lq_sta->max_rate_idx < 0) ||
2209 (lq_sta->max_rate_idx >= IWL_RATE_COUNT))
2210 lq_sta->max_rate_idx = -1;
2211 }
2212
2213 /* Treat uninitialized rate scaling data same as non-existing. */
2214 if (lq_sta && !lq_sta->drv) {
2215 IWL_DEBUG_RATE(mvm, "Rate scaling not initialized yet.\n");
2216 mvm_sta = NULL;
2217 }
2218
2219 /* Send management frames and NO_ACK data using lowest rate. */
2220 if (rate_control_send_low(sta, mvm_sta, txrc))
2221 return;
2222
Eyal Shapirad310e402013-08-11 18:43:47 +03002223 iwl_mvm_hwrate_to_tx_rate(lq_sta->last_rate_n_flags,
2224 info->band, &info->control.rates[0]);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002225
Moshe Benji622ebe92013-06-03 19:27:16 +03002226 info->control.rates[0].count = 1;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002227}
2228
2229static void *rs_alloc_sta(void *mvm_rate, struct ieee80211_sta *sta,
2230 gfp_t gfp)
2231{
2232 struct iwl_mvm_sta *sta_priv = (struct iwl_mvm_sta *)sta->drv_priv;
2233 struct iwl_op_mode *op_mode __maybe_unused =
2234 (struct iwl_op_mode *)mvm_rate;
2235 struct iwl_mvm *mvm __maybe_unused = IWL_OP_MODE_GET_MVM(op_mode);
2236
2237 IWL_DEBUG_RATE(mvm, "create station rate scale window\n");
2238
2239 return &sta_priv->lq_sta;
2240}
2241
Eyal Shapirad310e402013-08-11 18:43:47 +03002242static int rs_vht_highest_rx_mcs_index(struct ieee80211_sta_vht_cap *vht_cap,
2243 int nss)
2244{
2245 u16 rx_mcs = le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map) &
2246 (0x3 << (2 * (nss - 1)));
2247 rx_mcs >>= (2 * (nss - 1));
2248
2249 if (rx_mcs == IEEE80211_VHT_MCS_SUPPORT_0_7)
2250 return IWL_RATE_MCS_7_INDEX;
2251 else if (rx_mcs == IEEE80211_VHT_MCS_SUPPORT_0_8)
2252 return IWL_RATE_MCS_8_INDEX;
2253 else if (rx_mcs == IEEE80211_VHT_MCS_SUPPORT_0_9)
2254 return IWL_RATE_MCS_9_INDEX;
2255
2256 WARN_ON_ONCE(rx_mcs != IEEE80211_VHT_MCS_NOT_SUPPORTED);
2257 return -1;
2258}
2259
Eyal Shapira750a1b42013-11-09 22:48:56 +02002260static void rs_vht_set_enabled_rates(struct ieee80211_sta *sta,
2261 struct ieee80211_sta_vht_cap *vht_cap,
2262 struct iwl_lq_sta *lq_sta)
2263{
2264 int i;
2265 int highest_mcs = rs_vht_highest_rx_mcs_index(vht_cap, 1);
2266
2267 if (highest_mcs >= IWL_RATE_MCS_0_INDEX) {
2268 for (i = IWL_RATE_MCS_0_INDEX; i <= highest_mcs; i++) {
2269 if (i == IWL_RATE_9M_INDEX)
2270 continue;
2271
Eyal Shapira4623a262013-12-11 09:32:28 +02002272 /* Disable MCS9 as a workaround */
2273 if (i == IWL_RATE_MCS_9_INDEX)
2274 continue;
2275
Eyal Shapira271518a2013-11-09 23:25:06 +02002276 /* VHT MCS9 isn't valid for 20Mhz for NSS=1,2 */
2277 if (i == IWL_RATE_MCS_9_INDEX &&
2278 sta->bandwidth == IEEE80211_STA_RX_BW_20)
2279 continue;
2280
Eyal Shapira750a1b42013-11-09 22:48:56 +02002281 lq_sta->active_siso_rate |= BIT(i);
2282 }
2283 }
2284
Eyal Shapiraecc90e72013-11-09 23:31:38 +02002285 if (sta->rx_nss < 2)
2286 return;
2287
Eyal Shapira750a1b42013-11-09 22:48:56 +02002288 highest_mcs = rs_vht_highest_rx_mcs_index(vht_cap, 2);
2289 if (highest_mcs >= IWL_RATE_MCS_0_INDEX) {
2290 for (i = IWL_RATE_MCS_0_INDEX; i <= highest_mcs; i++) {
2291 if (i == IWL_RATE_9M_INDEX)
2292 continue;
2293
Eyal Shapira4623a262013-12-11 09:32:28 +02002294 /* Disable MCS9 as a workaround */
2295 if (i == IWL_RATE_MCS_9_INDEX)
2296 continue;
2297
Eyal Shapira271518a2013-11-09 23:25:06 +02002298 /* VHT MCS9 isn't valid for 20Mhz for NSS=1,2 */
2299 if (i == IWL_RATE_MCS_9_INDEX &&
2300 sta->bandwidth == IEEE80211_STA_RX_BW_20)
2301 continue;
2302
Eyal Shapira750a1b42013-11-09 22:48:56 +02002303 lq_sta->active_mimo2_rate |= BIT(i);
2304 }
2305 }
2306}
2307
Eyal Shapira5fc0f762014-01-28 01:35:32 +02002308#ifdef CONFIG_IWLWIFI_DEBUGFS
2309static void iwl_mvm_reset_frame_stats(struct iwl_mvm *mvm,
2310 struct iwl_mvm_frame_stats *stats)
2311{
2312 spin_lock_bh(&mvm->drv_stats_lock);
2313 memset(stats, 0, sizeof(*stats));
2314 spin_unlock_bh(&mvm->drv_stats_lock);
2315}
2316
2317void iwl_mvm_update_frame_stats(struct iwl_mvm *mvm,
2318 struct iwl_mvm_frame_stats *stats,
2319 u32 rate, bool agg)
2320{
2321 u8 nss = 0, mcs = 0;
2322
2323 spin_lock(&mvm->drv_stats_lock);
2324
2325 if (agg)
2326 stats->agg_frames++;
2327
2328 stats->success_frames++;
2329
2330 switch (rate & RATE_MCS_CHAN_WIDTH_MSK) {
2331 case RATE_MCS_CHAN_WIDTH_20:
2332 stats->bw_20_frames++;
2333 break;
2334 case RATE_MCS_CHAN_WIDTH_40:
2335 stats->bw_40_frames++;
2336 break;
2337 case RATE_MCS_CHAN_WIDTH_80:
2338 stats->bw_80_frames++;
2339 break;
2340 default:
2341 WARN_ONCE(1, "bad BW. rate 0x%x", rate);
2342 }
2343
2344 if (rate & RATE_MCS_HT_MSK) {
2345 stats->ht_frames++;
2346 mcs = rate & RATE_HT_MCS_RATE_CODE_MSK;
2347 nss = ((rate & RATE_HT_MCS_NSS_MSK) >> RATE_HT_MCS_NSS_POS) + 1;
2348 } else if (rate & RATE_MCS_VHT_MSK) {
2349 stats->vht_frames++;
2350 mcs = rate & RATE_VHT_MCS_RATE_CODE_MSK;
2351 nss = ((rate & RATE_VHT_MCS_NSS_MSK) >>
2352 RATE_VHT_MCS_NSS_POS) + 1;
2353 } else {
2354 stats->legacy_frames++;
2355 }
2356
2357 if (nss == 1)
2358 stats->siso_frames++;
2359 else if (nss == 2)
2360 stats->mimo2_frames++;
2361
2362 if (rate & RATE_MCS_SGI_MSK)
2363 stats->sgi_frames++;
2364 else
2365 stats->ngi_frames++;
2366
2367 stats->last_rates[stats->last_frame_idx] = rate;
2368 stats->last_frame_idx = (stats->last_frame_idx + 1) %
2369 ARRAY_SIZE(stats->last_rates);
2370
2371 spin_unlock(&mvm->drv_stats_lock);
2372}
2373#endif
2374
Johannes Berg8ca151b2013-01-24 14:25:36 +01002375/*
2376 * Called after adding a new station to initialize rate scaling
2377 */
2378void iwl_mvm_rs_rate_init(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
Eyal Shapirab87c2172013-11-09 23:37:55 +02002379 enum ieee80211_band band, bool init)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002380{
2381 int i, j;
2382 struct ieee80211_hw *hw = mvm->hw;
2383 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Eyal Shapirad310e402013-08-11 18:43:47 +03002384 struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002385 struct iwl_mvm_sta *sta_priv;
2386 struct iwl_lq_sta *lq_sta;
2387 struct ieee80211_supported_band *sband;
2388 unsigned long supp; /* must be unsigned long for for_each_set_bit */
2389
2390 sta_priv = (struct iwl_mvm_sta *)sta->drv_priv;
2391 lq_sta = &sta_priv->lq_sta;
Eyal Shapirab87c2172013-11-09 23:37:55 +02002392 memset(lq_sta, 0, sizeof(*lq_sta));
2393
Johannes Berg8ca151b2013-01-24 14:25:36 +01002394 sband = hw->wiphy->bands[band];
2395
2396 lq_sta->lq.sta_id = sta_priv->sta_id;
2397
2398 for (j = 0; j < LQ_SIZE; j++)
Eliad Peller3ca71f62014-03-10 16:33:43 +02002399 rs_rate_scale_clear_tbl_windows(&lq_sta->lq_info[j]);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002400
2401 lq_sta->flush_timer = 0;
Eyal Shapira87d5e412014-04-06 18:13:22 +03002402 lq_sta->last_tx = jiffies;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002403
2404 IWL_DEBUG_RATE(mvm,
2405 "LQ: *** rate scale station global init for station %d ***\n",
2406 sta_priv->sta_id);
2407 /* TODO: what is a good starting rate for STA? About middle? Maybe not
2408 * the lowest or the highest rate.. Could consider using RSSI from
2409 * previous packets? Need to have IEEE 802.1X auth succeed immediately
2410 * after assoc.. */
2411
2412 lq_sta->max_rate_idx = -1;
2413 lq_sta->missed_rate_counter = IWL_MISSED_RATE_MAX;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002414 lq_sta->band = sband->band;
2415 /*
2416 * active legacy rates as per supported rates bitmap
2417 */
2418 supp = sta->supp_rates[sband->band];
2419 lq_sta->active_legacy_rate = 0;
2420 for_each_set_bit(i, &supp, BITS_PER_LONG)
2421 lq_sta->active_legacy_rate |= BIT(sband->bitrates[i].hw_value);
2422
Eyal Shapirad310e402013-08-11 18:43:47 +03002423 /* TODO: should probably account for rx_highest for both HT/VHT */
2424 if (!vht_cap || !vht_cap->vht_supported) {
2425 /* active_siso_rate mask includes 9 MBits (bit 5),
2426 * and CCK (bits 0-3), supp_rates[] does not;
2427 * shift to convert format, force 9 MBits off.
2428 */
2429 lq_sta->active_siso_rate = ht_cap->mcs.rx_mask[0] << 1;
2430 lq_sta->active_siso_rate |= ht_cap->mcs.rx_mask[0] & 0x1;
2431 lq_sta->active_siso_rate &= ~((u16)0x2);
2432 lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002433
Eyal Shapirad310e402013-08-11 18:43:47 +03002434 /* Same here */
2435 lq_sta->active_mimo2_rate = ht_cap->mcs.rx_mask[1] << 1;
2436 lq_sta->active_mimo2_rate |= ht_cap->mcs.rx_mask[1] & 0x1;
2437 lq_sta->active_mimo2_rate &= ~((u16)0x2);
2438 lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE;
2439
2440 lq_sta->is_vht = false;
2441 } else {
Eyal Shapira750a1b42013-11-09 22:48:56 +02002442 rs_vht_set_enabled_rates(sta, vht_cap, lq_sta);
Eyal Shapirad310e402013-08-11 18:43:47 +03002443 lq_sta->is_vht = true;
2444 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01002445
Eyal Shapira198266a2014-03-31 22:37:39 +03002446 lq_sta->max_legacy_rate_idx = find_last_bit(&lq_sta->active_legacy_rate,
2447 BITS_PER_LONG);
2448 lq_sta->max_siso_rate_idx = find_last_bit(&lq_sta->active_siso_rate,
2449 BITS_PER_LONG);
2450 lq_sta->max_mimo2_rate_idx = find_last_bit(&lq_sta->active_mimo2_rate,
2451 BITS_PER_LONG);
2452
2453 IWL_DEBUG_RATE(mvm, "RATE MASK: LEGACY=%lX SISO=%lX MIMO2=%lX VHT=%d\n",
2454 lq_sta->active_legacy_rate,
Johannes Berg8ca151b2013-01-24 14:25:36 +01002455 lq_sta->active_siso_rate,
Eyal Shapirad310e402013-08-11 18:43:47 +03002456 lq_sta->active_mimo2_rate,
2457 lq_sta->is_vht);
Eyal Shapira198266a2014-03-31 22:37:39 +03002458 IWL_DEBUG_RATE(mvm, "MAX RATE: LEGACY=%d SISO=%d MIMO2=%d\n",
2459 lq_sta->max_legacy_rate_idx,
2460 lq_sta->max_siso_rate_idx,
2461 lq_sta->max_mimo2_rate_idx);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002462
2463 /* These values will be overridden later */
2464 lq_sta->lq.single_stream_ant_msk =
Johannes Berg4ed735e2014-02-12 21:47:44 +01002465 first_antenna(mvm->fw->valid_tx_ant);
Eyal Shapira809bccf2013-11-28 02:25:24 +02002466 lq_sta->lq.dual_stream_ant_msk = ANT_AB;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002467
2468 /* as default allow aggregation for all tids */
2469 lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
2470 lq_sta->drv = mvm;
2471
2472 /* Set last_txrate_idx to lowest rate */
2473 lq_sta->last_txrate_idx = rate_lowest_index(sband, sta);
2474 if (sband->band == IEEE80211_BAND_5GHZ)
2475 lq_sta->last_txrate_idx += IWL_FIRST_OFDM_RATE;
2476 lq_sta->is_agg = 0;
2477#ifdef CONFIG_MAC80211_DEBUGFS
2478 lq_sta->dbg_fixed_rate = 0;
2479#endif
Eyal Shapira5fc0f762014-01-28 01:35:32 +02002480#ifdef CONFIG_IWLWIFI_DEBUGFS
2481 iwl_mvm_reset_frame_stats(mvm, &mvm->drv_rx_stats);
2482#endif
Eyal Shapirab87c2172013-11-09 23:37:55 +02002483 rs_initialize_lq(mvm, sta, lq_sta, band, init);
2484}
2485
2486static void rs_rate_update(void *mvm_r,
2487 struct ieee80211_supported_band *sband,
2488 struct cfg80211_chan_def *chandef,
2489 struct ieee80211_sta *sta, void *priv_sta,
2490 u32 changed)
2491{
2492 u8 tid;
2493 struct iwl_op_mode *op_mode =
2494 (struct iwl_op_mode *)mvm_r;
2495 struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
2496
2497 /* Stop any ongoing aggregations as rs starts off assuming no agg */
2498 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++)
2499 ieee80211_stop_tx_ba_session(sta, tid);
2500
2501 iwl_mvm_rs_rate_init(mvm, sta, sband->band, false);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002502}
2503
Eyal Shapira8fc7c582013-12-04 02:15:46 +02002504#ifdef CONFIG_MAC80211_DEBUGFS
2505static void rs_build_rates_table_from_fixed(struct iwl_mvm *mvm,
2506 struct iwl_lq_cmd *lq_cmd,
2507 enum ieee80211_band band,
2508 u32 ucode_rate)
2509{
2510 struct rs_rate rate;
2511 int i;
2512 int num_rates = ARRAY_SIZE(lq_cmd->rs_table);
2513 __le32 ucode_rate_le32 = cpu_to_le32(ucode_rate);
2514
2515 for (i = 0; i < num_rates; i++)
2516 lq_cmd->rs_table[i] = ucode_rate_le32;
2517
2518 rs_rate_from_ucode_rate(ucode_rate, band, &rate);
2519
2520 if (is_mimo(&rate))
2521 lq_cmd->mimo_delim = num_rates - 1;
2522 else
2523 lq_cmd->mimo_delim = 0;
2524}
2525#endif /* CONFIG_MAC80211_DEBUGFS */
2526
Eyal Shapirae07be6d2013-12-09 13:02:56 +02002527static void rs_fill_rates_for_column(struct iwl_mvm *mvm,
2528 struct iwl_lq_sta *lq_sta,
2529 struct rs_rate *rate,
2530 __le32 *rs_table, int *rs_table_index,
2531 int num_rates, int num_retries,
2532 u8 valid_tx_ant, bool toggle_ant)
2533{
2534 int i, j;
2535 __le32 ucode_rate;
2536 bool bottom_reached = false;
2537 int prev_rate_idx = rate->index;
2538 int end = LINK_QUAL_MAX_RETRY_NUM;
2539 int index = *rs_table_index;
2540
2541 for (i = 0; i < num_rates && index < end; i++) {
2542 ucode_rate = cpu_to_le32(ucode_rate_from_rs_rate(mvm, rate));
2543 for (j = 0; j < num_retries && index < end; j++, index++)
2544 rs_table[index] = ucode_rate;
2545
2546 if (toggle_ant)
2547 rs_toggle_antenna(valid_tx_ant, rate);
2548
2549 prev_rate_idx = rate->index;
2550 bottom_reached = rs_get_lower_rate_in_column(lq_sta, rate);
2551 if (bottom_reached && !is_legacy(rate))
2552 break;
2553 }
2554
2555 if (!bottom_reached)
2556 rate->index = prev_rate_idx;
2557
2558 *rs_table_index = index;
2559}
2560
2561/* Building the rate table is non trivial. When we're in MIMO2/VHT/80Mhz/SGI
2562 * column the rate table should look like this:
2563 *
2564 * rate[0] 0x400D019 VHT | ANT: AB BW: 80Mhz MCS: 9 NSS: 2 SGI
2565 * rate[1] 0x400D019 VHT | ANT: AB BW: 80Mhz MCS: 9 NSS: 2 SGI
2566 * rate[2] 0x400D018 VHT | ANT: AB BW: 80Mhz MCS: 8 NSS: 2 SGI
2567 * rate[3] 0x400D018 VHT | ANT: AB BW: 80Mhz MCS: 8 NSS: 2 SGI
2568 * rate[4] 0x400D017 VHT | ANT: AB BW: 80Mhz MCS: 7 NSS: 2 SGI
2569 * rate[5] 0x400D017 VHT | ANT: AB BW: 80Mhz MCS: 7 NSS: 2 SGI
2570 * rate[6] 0x4005007 VHT | ANT: A BW: 80Mhz MCS: 7 NSS: 1 NGI
2571 * rate[7] 0x4009006 VHT | ANT: B BW: 80Mhz MCS: 6 NSS: 1 NGI
2572 * rate[8] 0x4005005 VHT | ANT: A BW: 80Mhz MCS: 5 NSS: 1 NGI
2573 * rate[9] 0x800B Legacy | ANT: B Rate: 36 Mbps
2574 * rate[10] 0x4009 Legacy | ANT: A Rate: 24 Mbps
2575 * rate[11] 0x8007 Legacy | ANT: B Rate: 18 Mbps
2576 * rate[12] 0x4005 Legacy | ANT: A Rate: 12 Mbps
2577 * rate[13] 0x800F Legacy | ANT: B Rate: 9 Mbps
2578 * rate[14] 0x400D Legacy | ANT: A Rate: 6 Mbps
2579 * rate[15] 0x800D Legacy | ANT: B Rate: 6 Mbps
2580 */
Eyal Shapira8fc7c582013-12-04 02:15:46 +02002581static void rs_build_rates_table(struct iwl_mvm *mvm,
2582 struct iwl_lq_sta *lq_sta,
2583 const struct rs_rate *initial_rate)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002584{
Eyal Shapira5aa33552013-11-23 01:06:36 +02002585 struct rs_rate rate;
Eyal Shapirae07be6d2013-12-09 13:02:56 +02002586 int num_rates, num_retries, index = 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002587 u8 valid_tx_ant = 0;
2588 struct iwl_lq_cmd *lq_cmd = &lq_sta->lq;
Eyal Shapirae07be6d2013-12-09 13:02:56 +02002589 bool toggle_ant = false;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002590
Emmanuel Grumbach9e898f12013-12-22 10:55:47 +02002591 memcpy(&rate, initial_rate, sizeof(rate));
Johannes Berg8ca151b2013-01-24 14:25:36 +01002592
Johannes Berg4ed735e2014-02-12 21:47:44 +01002593 valid_tx_ant = mvm->fw->valid_tx_ant;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002594
Eyal Shapirae07be6d2013-12-09 13:02:56 +02002595 if (is_siso(&rate)) {
2596 num_rates = RS_INITIAL_SISO_NUM_RATES;
2597 num_retries = RS_HT_VHT_RETRIES_PER_RATE;
2598 } else if (is_mimo(&rate)) {
2599 num_rates = RS_INITIAL_MIMO_NUM_RATES;
2600 num_retries = RS_HT_VHT_RETRIES_PER_RATE;
2601 } else {
2602 num_rates = RS_INITIAL_LEGACY_NUM_RATES;
2603 num_retries = RS_LEGACY_RETRIES_PER_RATE;
2604 toggle_ant = true;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002605 }
Eyal Shapirae07be6d2013-12-09 13:02:56 +02002606
2607 rs_fill_rates_for_column(mvm, lq_sta, &rate, lq_cmd->rs_table, &index,
2608 num_rates, num_retries, valid_tx_ant,
2609 toggle_ant);
2610
2611 rs_get_lower_rate_down_column(lq_sta, &rate);
2612
2613 if (is_siso(&rate)) {
2614 num_rates = RS_SECONDARY_SISO_NUM_RATES;
2615 num_retries = RS_SECONDARY_SISO_RETRIES;
Eyal Shapirad9088f62014-03-25 10:25:44 +02002616 lq_cmd->mimo_delim = index;
Eyal Shapirae07be6d2013-12-09 13:02:56 +02002617 } else if (is_legacy(&rate)) {
2618 num_rates = RS_SECONDARY_LEGACY_NUM_RATES;
2619 num_retries = RS_LEGACY_RETRIES_PER_RATE;
2620 } else {
2621 WARN_ON_ONCE(1);
2622 }
2623
2624 toggle_ant = true;
2625
2626 rs_fill_rates_for_column(mvm, lq_sta, &rate, lq_cmd->rs_table, &index,
2627 num_rates, num_retries, valid_tx_ant,
2628 toggle_ant);
2629
2630 rs_get_lower_rate_down_column(lq_sta, &rate);
2631
2632 num_rates = RS_SECONDARY_LEGACY_NUM_RATES;
2633 num_retries = RS_LEGACY_RETRIES_PER_RATE;
2634
2635 rs_fill_rates_for_column(mvm, lq_sta, &rate, lq_cmd->rs_table, &index,
2636 num_rates, num_retries, valid_tx_ant,
2637 toggle_ant);
2638
Eyal Shapira8fc7c582013-12-04 02:15:46 +02002639}
2640
2641static void rs_fill_lq_cmd(struct iwl_mvm *mvm,
2642 struct ieee80211_sta *sta,
2643 struct iwl_lq_sta *lq_sta,
2644 const struct rs_rate *initial_rate)
2645{
2646 struct iwl_lq_cmd *lq_cmd = &lq_sta->lq;
2647 u8 ant = initial_rate->ant;
2648
2649#ifdef CONFIG_MAC80211_DEBUGFS
2650 if (lq_sta->dbg_fixed_rate) {
2651 rs_build_rates_table_from_fixed(mvm, lq_cmd,
2652 lq_sta->band,
2653 lq_sta->dbg_fixed_rate);
2654 ant = (lq_sta->dbg_fixed_rate & RATE_MCS_ANT_ABC_MSK) >>
2655 RATE_MCS_ANT_POS;
2656 } else
2657#endif
2658 rs_build_rates_table(mvm, lq_sta, initial_rate);
2659
2660 if (num_of_ant(ant) == 1)
2661 lq_cmd->single_stream_ant_msk = ant;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002662
2663 lq_cmd->agg_frame_cnt_limit = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
2664 lq_cmd->agg_disable_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
2665
2666 lq_cmd->agg_time_limit =
2667 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
Emmanuel Grumbache96d5512013-05-28 21:31:50 +03002668
Emmanuel Grumbach9145d152013-07-18 08:45:41 +03002669 if (sta)
2670 lq_cmd->agg_time_limit =
Emmanuel Grumbach5b7ff612014-03-11 19:27:45 +02002671 cpu_to_le16(iwl_mvm_coex_agg_time_limit(mvm, sta));
Johannes Berg8ca151b2013-01-24 14:25:36 +01002672}
2673
2674static void *rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
2675{
2676 return hw->priv;
2677}
2678/* rate scale requires free function to be implemented */
2679static void rs_free(void *mvm_rate)
2680{
2681 return;
2682}
2683
2684static void rs_free_sta(void *mvm_r, struct ieee80211_sta *sta,
2685 void *mvm_sta)
2686{
2687 struct iwl_op_mode *op_mode __maybe_unused = mvm_r;
2688 struct iwl_mvm *mvm __maybe_unused = IWL_OP_MODE_GET_MVM(op_mode);
2689
2690 IWL_DEBUG_RATE(mvm, "enter\n");
2691 IWL_DEBUG_RATE(mvm, "leave\n");
2692}
2693
2694#ifdef CONFIG_MAC80211_DEBUGFS
Eyal Shapira5fc0f762014-01-28 01:35:32 +02002695int rs_pretty_print_rate(char *buf, const u32 rate)
Eyal Shapira5aa33552013-11-23 01:06:36 +02002696{
2697
Eyal Shapira1a61b342013-11-09 02:11:46 +02002698 char *type, *bw;
2699 u8 mcs = 0, nss = 0;
2700 u8 ant = (rate & RATE_MCS_ANT_ABC_MSK) >> RATE_MCS_ANT_POS;
2701
2702 if (!(rate & RATE_MCS_HT_MSK) &&
2703 !(rate & RATE_MCS_VHT_MSK)) {
2704 int index = iwl_hwrate_to_plcp_idx(rate);
2705
2706 return sprintf(buf, "Legacy | ANT: %s Rate: %s Mbps\n",
Eyal Shapira560843f2014-01-05 21:04:19 +02002707 rs_pretty_ant(ant),
2708 index == IWL_RATE_INVALID ? "BAD" :
2709 iwl_rate_mcs[index].mbps);
Eyal Shapira1a61b342013-11-09 02:11:46 +02002710 }
2711
2712 if (rate & RATE_MCS_VHT_MSK) {
2713 type = "VHT";
2714 mcs = rate & RATE_VHT_MCS_RATE_CODE_MSK;
2715 nss = ((rate & RATE_VHT_MCS_NSS_MSK)
2716 >> RATE_VHT_MCS_NSS_POS) + 1;
2717 } else if (rate & RATE_MCS_HT_MSK) {
2718 type = "HT";
2719 mcs = rate & RATE_HT_MCS_INDEX_MSK;
2720 } else {
2721 type = "Unknown"; /* shouldn't happen */
2722 }
2723
2724 switch (rate & RATE_MCS_CHAN_WIDTH_MSK) {
2725 case RATE_MCS_CHAN_WIDTH_20:
2726 bw = "20Mhz";
2727 break;
2728 case RATE_MCS_CHAN_WIDTH_40:
2729 bw = "40Mhz";
2730 break;
2731 case RATE_MCS_CHAN_WIDTH_80:
2732 bw = "80Mhz";
2733 break;
2734 case RATE_MCS_CHAN_WIDTH_160:
2735 bw = "160Mhz";
2736 break;
2737 default:
2738 bw = "BAD BW";
2739 }
2740
2741 return sprintf(buf, "%s | ANT: %s BW: %s MCS: %d NSS: %d %s%s%s%s%s\n",
Eyal Shapira5aa33552013-11-23 01:06:36 +02002742 type, rs_pretty_ant(ant), bw, mcs, nss,
Eyal Shapira1a61b342013-11-09 02:11:46 +02002743 (rate & RATE_MCS_SGI_MSK) ? "SGI " : "NGI ",
Emmanuel Grumbach7b1dd042014-02-04 15:32:43 +02002744 (rate & RATE_MCS_HT_STBC_MSK) ? "STBC " : "",
Eyal Shapira1a61b342013-11-09 02:11:46 +02002745 (rate & RATE_MCS_LDPC_MSK) ? "LDPC " : "",
2746 (rate & RATE_MCS_BF_MSK) ? "BF " : "",
2747 (rate & RATE_MCS_ZLF_MSK) ? "ZLF " : "");
2748}
2749
Eyal Shapiraa0c38b22013-12-03 18:34:52 +02002750/**
2751 * Program the device to use fixed rate for frame transmit
2752 * This is for debugging/testing only
2753 * once the device start use fixed rate, we need to reload the module
2754 * to being back the normal operation.
2755 */
2756static void rs_program_fix_rate(struct iwl_mvm *mvm,
2757 struct iwl_lq_sta *lq_sta)
2758{
2759 lq_sta->active_legacy_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
2760 lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2761 lq_sta->active_mimo2_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2762
2763 IWL_DEBUG_RATE(mvm, "sta_id %d rate 0x%X\n",
2764 lq_sta->lq.sta_id, lq_sta->dbg_fixed_rate);
2765
2766 if (lq_sta->dbg_fixed_rate) {
Eyal Shapira8fc7c582013-12-04 02:15:46 +02002767 struct rs_rate rate;
2768 rs_rate_from_ucode_rate(lq_sta->dbg_fixed_rate,
2769 lq_sta->band, &rate);
Eyal Shapira80e9e5c2013-12-23 16:26:41 +02002770 rs_fill_lq_cmd(mvm, NULL, lq_sta, &rate);
Eyal Shapiraa0c38b22013-12-03 18:34:52 +02002771 iwl_mvm_send_lq_cmd(lq_sta->drv, &lq_sta->lq, false);
2772 }
2773}
2774
Emmanuel Grumbach9d108492013-12-03 11:50:30 +02002775static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
2776 const char __user *user_buf, size_t count, loff_t *ppos)
2777{
2778 struct iwl_lq_sta *lq_sta = file->private_data;
2779 struct iwl_mvm *mvm;
2780 char buf[64];
2781 size_t buf_size;
2782 u32 parsed_rate;
2783
2784
2785 mvm = lq_sta->drv;
2786 memset(buf, 0, sizeof(buf));
2787 buf_size = min(count, sizeof(buf) - 1);
2788 if (copy_from_user(buf, user_buf, buf_size))
2789 return -EFAULT;
2790
2791 if (sscanf(buf, "%x", &parsed_rate) == 1)
2792 lq_sta->dbg_fixed_rate = parsed_rate;
2793 else
2794 lq_sta->dbg_fixed_rate = 0;
2795
2796 rs_program_fix_rate(mvm, lq_sta);
2797
2798 return count;
2799}
2800
Johannes Berg8ca151b2013-01-24 14:25:36 +01002801static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
2802 char __user *user_buf, size_t count, loff_t *ppos)
2803{
2804 char *buff;
2805 int desc = 0;
2806 int i = 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002807 ssize_t ret;
2808
2809 struct iwl_lq_sta *lq_sta = file->private_data;
2810 struct iwl_mvm *mvm;
2811 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
Eyal Shapira5aa33552013-11-23 01:06:36 +02002812 struct rs_rate *rate = &tbl->rate;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002813 mvm = lq_sta->drv;
Eyal Shapira1a61b342013-11-09 02:11:46 +02002814 buff = kmalloc(2048, GFP_KERNEL);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002815 if (!buff)
2816 return -ENOMEM;
2817
2818 desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
Eyal Shapira198266a2014-03-31 22:37:39 +03002819 desc += sprintf(buff+desc, "failed=%d success=%d rate=0%lX\n",
Johannes Berg8ca151b2013-01-24 14:25:36 +01002820 lq_sta->total_failed, lq_sta->total_success,
2821 lq_sta->active_legacy_rate);
2822 desc += sprintf(buff+desc, "fixed rate 0x%X\n",
2823 lq_sta->dbg_fixed_rate);
2824 desc += sprintf(buff+desc, "valid_tx_ant %s%s%s\n",
Johannes Berg4ed735e2014-02-12 21:47:44 +01002825 (mvm->fw->valid_tx_ant & ANT_A) ? "ANT_A," : "",
2826 (mvm->fw->valid_tx_ant & ANT_B) ? "ANT_B," : "",
2827 (mvm->fw->valid_tx_ant & ANT_C) ? "ANT_C" : "");
Johannes Berg8ca151b2013-01-24 14:25:36 +01002828 desc += sprintf(buff+desc, "lq type %s\n",
Eyal Shapira5aa33552013-11-23 01:06:36 +02002829 (is_legacy(rate)) ? "legacy" :
2830 is_vht(rate) ? "VHT" : "HT");
2831 if (!is_legacy(rate)) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002832 desc += sprintf(buff+desc, " %s",
Eyal Shapira5aa33552013-11-23 01:06:36 +02002833 (is_siso(rate)) ? "SISO" : "MIMO2");
Johannes Berg8ca151b2013-01-24 14:25:36 +01002834 desc += sprintf(buff+desc, " %s",
Eyal Shapira5aa33552013-11-23 01:06:36 +02002835 (is_ht20(rate)) ? "20MHz" :
2836 (is_ht40(rate)) ? "40MHz" :
2837 (is_ht80(rate)) ? "80Mhz" : "BAD BW");
Eyal Shapira6a524f42013-08-11 20:27:18 +03002838 desc += sprintf(buff+desc, " %s %s\n",
Eyal Shapira5aa33552013-11-23 01:06:36 +02002839 (rate->sgi) ? "SGI" : "NGI",
Eyal Shapira6a524f42013-08-11 20:27:18 +03002840 (lq_sta->is_agg) ? "AGG on" : "");
Johannes Berg8ca151b2013-01-24 14:25:36 +01002841 }
2842 desc += sprintf(buff+desc, "last tx rate=0x%X\n",
2843 lq_sta->last_rate_n_flags);
2844 desc += sprintf(buff+desc,
Eyal Shapira1a61b342013-11-09 02:11:46 +02002845 "general: flags=0x%X mimo-d=%d s-ant=0x%x d-ant=0x%x\n",
Johannes Berg8ca151b2013-01-24 14:25:36 +01002846 lq_sta->lq.flags,
2847 lq_sta->lq.mimo_delim,
2848 lq_sta->lq.single_stream_ant_msk,
2849 lq_sta->lq.dual_stream_ant_msk);
2850
2851 desc += sprintf(buff+desc,
2852 "agg: time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
2853 le16_to_cpu(lq_sta->lq.agg_time_limit),
2854 lq_sta->lq.agg_disable_start_th,
2855 lq_sta->lq.agg_frame_cnt_limit);
2856
2857 desc += sprintf(buff+desc,
2858 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
2859 lq_sta->lq.initial_rate_index[0],
2860 lq_sta->lq.initial_rate_index[1],
2861 lq_sta->lq.initial_rate_index[2],
2862 lq_sta->lq.initial_rate_index[3]);
2863
2864 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
Johannes Bergcf4ef652013-12-17 11:34:25 +01002865 u32 r = le32_to_cpu(lq_sta->lq.rs_table[i]);
Eyal Shapira1a61b342013-11-09 02:11:46 +02002866
Johannes Bergcf4ef652013-12-17 11:34:25 +01002867 desc += sprintf(buff+desc, " rate[%d] 0x%X ", i, r);
2868 desc += rs_pretty_print_rate(buff+desc, r);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002869 }
2870
2871 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2872 kfree(buff);
2873 return ret;
2874}
2875
2876static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
2877 .write = rs_sta_dbgfs_scale_table_write,
2878 .read = rs_sta_dbgfs_scale_table_read,
2879 .open = simple_open,
2880 .llseek = default_llseek,
2881};
2882static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
2883 char __user *user_buf, size_t count, loff_t *ppos)
2884{
2885 char *buff;
2886 int desc = 0;
2887 int i, j;
2888 ssize_t ret;
Eyal Shapirad310e402013-08-11 18:43:47 +03002889 struct iwl_scale_tbl_info *tbl;
Eyal Shapira5aa33552013-11-23 01:06:36 +02002890 struct rs_rate *rate;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002891 struct iwl_lq_sta *lq_sta = file->private_data;
2892
2893 buff = kmalloc(1024, GFP_KERNEL);
2894 if (!buff)
2895 return -ENOMEM;
2896
2897 for (i = 0; i < LQ_SIZE; i++) {
Eyal Shapirad310e402013-08-11 18:43:47 +03002898 tbl = &(lq_sta->lq_info[i]);
Eyal Shapira5aa33552013-11-23 01:06:36 +02002899 rate = &tbl->rate;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002900 desc += sprintf(buff+desc,
Eyal Shapira6a524f42013-08-11 20:27:18 +03002901 "%s type=%d SGI=%d BW=%s DUP=0\n"
Eyal Shapira8fc7c582013-12-04 02:15:46 +02002902 "index=%d\n",
Johannes Berg8ca151b2013-01-24 14:25:36 +01002903 lq_sta->active_tbl == i ? "*" : "x",
Eyal Shapira5aa33552013-11-23 01:06:36 +02002904 rate->type,
2905 rate->sgi,
2906 is_ht20(rate) ? "20Mhz" :
2907 is_ht40(rate) ? "40Mhz" :
2908 is_ht80(rate) ? "80Mhz" : "ERR",
Eyal Shapira8fc7c582013-12-04 02:15:46 +02002909 rate->index);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002910 for (j = 0; j < IWL_RATE_COUNT; j++) {
2911 desc += sprintf(buff+desc,
2912 "counter=%d success=%d %%=%d\n",
Eyal Shapirad310e402013-08-11 18:43:47 +03002913 tbl->win[j].counter,
2914 tbl->win[j].success_counter,
2915 tbl->win[j].success_ratio);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002916 }
2917 }
2918 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2919 kfree(buff);
2920 return ret;
2921}
2922
2923static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
2924 .read = rs_sta_dbgfs_stats_table_read,
2925 .open = simple_open,
2926 .llseek = default_llseek,
2927};
2928
Johannes Berg8ca151b2013-01-24 14:25:36 +01002929static void rs_add_debugfs(void *mvm, void *mvm_sta, struct dentry *dir)
2930{
2931 struct iwl_lq_sta *lq_sta = mvm_sta;
2932 lq_sta->rs_sta_dbgfs_scale_table_file =
2933 debugfs_create_file("rate_scale_table", S_IRUSR | S_IWUSR, dir,
2934 lq_sta, &rs_sta_dbgfs_scale_table_ops);
2935 lq_sta->rs_sta_dbgfs_stats_table_file =
2936 debugfs_create_file("rate_stats_table", S_IRUSR, dir,
2937 lq_sta, &rs_sta_dbgfs_stats_table_ops);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002938 lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file =
2939 debugfs_create_u8("tx_agg_tid_enable", S_IRUSR | S_IWUSR, dir,
2940 &lq_sta->tx_agg_tid_en);
2941}
2942
2943static void rs_remove_debugfs(void *mvm, void *mvm_sta)
2944{
2945 struct iwl_lq_sta *lq_sta = mvm_sta;
2946 debugfs_remove(lq_sta->rs_sta_dbgfs_scale_table_file);
2947 debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002948 debugfs_remove(lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file);
2949}
2950#endif
2951
2952/*
2953 * Initialization of rate scaling information is done by driver after
2954 * the station is added. Since mac80211 calls this function before a
2955 * station is added we ignore it.
2956 */
2957static void rs_rate_init_stub(void *mvm_r,
Simon Wunderlich3de805c2013-07-08 16:55:50 +02002958 struct ieee80211_supported_band *sband,
2959 struct cfg80211_chan_def *chandef,
2960 struct ieee80211_sta *sta, void *mvm_sta)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002961{
2962}
Johannes Berg631ad702014-01-20 23:29:34 +01002963
2964static const struct rate_control_ops rs_mvm_ops = {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002965 .name = RS_NAME,
2966 .tx_status = rs_tx_status,
2967 .get_rate = rs_get_rate,
2968 .rate_init = rs_rate_init_stub,
2969 .alloc = rs_alloc,
2970 .free = rs_free,
2971 .alloc_sta = rs_alloc_sta,
2972 .free_sta = rs_free_sta,
Eyal Shapirab87c2172013-11-09 23:37:55 +02002973 .rate_update = rs_rate_update,
Johannes Berg8ca151b2013-01-24 14:25:36 +01002974#ifdef CONFIG_MAC80211_DEBUGFS
2975 .add_sta_debugfs = rs_add_debugfs,
2976 .remove_sta_debugfs = rs_remove_debugfs,
2977#endif
2978};
2979
2980int iwl_mvm_rate_control_register(void)
2981{
2982 return ieee80211_rate_control_register(&rs_mvm_ops);
2983}
2984
2985void iwl_mvm_rate_control_unregister(void)
2986{
2987 ieee80211_rate_control_unregister(&rs_mvm_ops);
2988}
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +03002989
2990/**
2991 * iwl_mvm_tx_protection - Gets LQ command, change it to enable/disable
2992 * Tx protection, according to this rquest and previous requests,
2993 * and send the LQ command.
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +03002994 * @mvmsta: The station
2995 * @enable: Enable Tx protection?
2996 */
Johannes Berge126b5d2013-06-28 13:39:18 +02002997int iwl_mvm_tx_protection(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
2998 bool enable)
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +03002999{
Johannes Berge126b5d2013-06-28 13:39:18 +02003000 struct iwl_lq_cmd *lq = &mvmsta->lq_sta.lq;
3001
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +03003002 lockdep_assert_held(&mvm->mutex);
3003
3004 if (enable) {
3005 if (mvmsta->tx_protection == 0)
Eyal Shapirad307ec82013-11-11 19:56:21 +02003006 lq->flags |= LQ_FLAG_USE_RTS_MSK;
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +03003007 mvmsta->tx_protection++;
3008 } else {
3009 mvmsta->tx_protection--;
3010 if (mvmsta->tx_protection == 0)
Eyal Shapirad307ec82013-11-11 19:56:21 +02003011 lq->flags &= ~LQ_FLAG_USE_RTS_MSK;
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +03003012 }
3013
Eyal Shapira9e680942013-11-09 00:16:16 +02003014 return iwl_mvm_send_lq_cmd(mvm, lq, false);
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +03003015}