blob: 60abb0084ee5905dd38f89a694542ddf1094a742 [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.
Eyal Shapirafcc5e852015-04-12 23:45:27 +03004 * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
Gregory Greenmanea42d1c2017-03-06 11:15:41 +02005 * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
Johannes Berg8ca151b2013-01-24 14:25:36 +01006 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of version 2 of the GNU General Public License as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
19 *
20 * The full GNU General Public License is included in this distribution in the
21 * file called LICENSE.
22 *
23 * Contact Information:
Emmanuel Grumbachcb2f8272015-11-17 15:39:56 +020024 * Intel Linux Wireless <linuxwifi@intel.com>
Johannes Berg8ca151b2013-01-24 14:25:36 +010025 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
26 *
27 *****************************************************************************/
28#include <linux/kernel.h>
Johannes Berg8ca151b2013-01-24 14:25:36 +010029#include <linux/skbuff.h>
30#include <linux/slab.h>
31#include <net/mac80211.h>
32
33#include <linux/netdevice.h>
34#include <linux/etherdevice.h>
35#include <linux/delay.h>
36
37#include <linux/workqueue.h>
38#include "rs.h"
39#include "fw-api.h"
40#include "sta.h"
41#include "iwl-op-mode.h"
42#include "mvm.h"
Eyal Shapira1e9c62f2015-01-28 14:44:06 +020043#include "debugfs.h"
Johannes Berg8ca151b2013-01-24 14:25:36 +010044
Johannes Berg8ca151b2013-01-24 14:25:36 +010045#define IWL_RATE_MAX_WINDOW 62 /* # tx in history window */
Johannes Berg8ca151b2013-01-24 14:25:36 +010046
Eyal Shapira834437d2014-12-17 15:38:58 +020047/* Calculations of success ratio are done in fixed point where 12800 is 100%.
48 * Use this macro when dealing with thresholds consts set as a percentage
49 */
50#define RS_PERCENT(x) (128 * x)
Johannes Berg8ca151b2013-01-24 14:25:36 +010051
52static u8 rs_ht_to_legacy[] = {
Eyal Shapirada87d7d2013-11-28 12:27:03 +020053 [IWL_RATE_MCS_0_INDEX] = IWL_RATE_6M_INDEX,
54 [IWL_RATE_MCS_1_INDEX] = IWL_RATE_9M_INDEX,
55 [IWL_RATE_MCS_2_INDEX] = IWL_RATE_12M_INDEX,
56 [IWL_RATE_MCS_3_INDEX] = IWL_RATE_18M_INDEX,
57 [IWL_RATE_MCS_4_INDEX] = IWL_RATE_24M_INDEX,
58 [IWL_RATE_MCS_5_INDEX] = IWL_RATE_36M_INDEX,
59 [IWL_RATE_MCS_6_INDEX] = IWL_RATE_48M_INDEX,
60 [IWL_RATE_MCS_7_INDEX] = IWL_RATE_54M_INDEX,
61 [IWL_RATE_MCS_8_INDEX] = IWL_RATE_54M_INDEX,
62 [IWL_RATE_MCS_9_INDEX] = IWL_RATE_54M_INDEX,
Johannes Berg8ca151b2013-01-24 14:25:36 +010063};
64
65static const u8 ant_toggle_lookup[] = {
Eliad Pelleref4394b2013-07-16 18:07:20 +030066 [ANT_NONE] = ANT_NONE,
67 [ANT_A] = ANT_B,
Emmanuel Grumbach82d2b9a2017-10-18 13:09:38 +030068 [ANT_B] = ANT_A,
69 [ANT_AB] = ANT_AB,
Johannes Berg8ca151b2013-01-24 14:25:36 +010070};
71
Eyal Shapirad310e402013-08-11 18:43:47 +030072#define IWL_DECLARE_RATE_INFO(r, s, rp, rn) \
73 [IWL_RATE_##r##M_INDEX] = { IWL_RATE_##r##M_PLCP, \
74 IWL_RATE_HT_SISO_MCS_##s##_PLCP, \
75 IWL_RATE_HT_MIMO2_MCS_##s##_PLCP, \
76 IWL_RATE_VHT_SISO_MCS_##s##_PLCP, \
77 IWL_RATE_VHT_MIMO2_MCS_##s##_PLCP,\
78 IWL_RATE_##rp##M_INDEX, \
Eyal Shapiraab055ce2013-08-04 13:10:31 +030079 IWL_RATE_##rn##M_INDEX }
Johannes Berg8ca151b2013-01-24 14:25:36 +010080
Eyal Shapirad310e402013-08-11 18:43:47 +030081#define IWL_DECLARE_MCS_RATE(s) \
82 [IWL_RATE_MCS_##s##_INDEX] = { IWL_RATE_INVM_PLCP, \
83 IWL_RATE_HT_SISO_MCS_##s##_PLCP, \
84 IWL_RATE_HT_MIMO2_MCS_##s##_PLCP, \
85 IWL_RATE_VHT_SISO_MCS_##s##_PLCP, \
86 IWL_RATE_VHT_MIMO2_MCS_##s##_PLCP, \
87 IWL_RATE_INVM_INDEX, \
88 IWL_RATE_INVM_INDEX }
89
Johannes Berg8ca151b2013-01-24 14:25:36 +010090/*
91 * Parameter order:
Eyal Shapiraab055ce2013-08-04 13:10:31 +030092 * rate, ht rate, prev rate, next rate
Johannes Berg8ca151b2013-01-24 14:25:36 +010093 *
94 * If there isn't a valid next or previous rate then INV is used which
95 * maps to IWL_RATE_INVALID
96 *
97 */
98static const struct iwl_rs_rate_info iwl_rates[IWL_RATE_COUNT] = {
Eyal Shapiraab055ce2013-08-04 13:10:31 +030099 IWL_DECLARE_RATE_INFO(1, INV, INV, 2), /* 1mbps */
100 IWL_DECLARE_RATE_INFO(2, INV, 1, 5), /* 2mbps */
101 IWL_DECLARE_RATE_INFO(5, INV, 2, 11), /*5.5mbps */
102 IWL_DECLARE_RATE_INFO(11, INV, 9, 12), /* 11mbps */
Eyal Shapirad310e402013-08-11 18:43:47 +0300103 IWL_DECLARE_RATE_INFO(6, 0, 5, 11), /* 6mbps ; MCS 0 */
104 IWL_DECLARE_RATE_INFO(9, INV, 6, 11), /* 9mbps */
105 IWL_DECLARE_RATE_INFO(12, 1, 11, 18), /* 12mbps ; MCS 1 */
106 IWL_DECLARE_RATE_INFO(18, 2, 12, 24), /* 18mbps ; MCS 2 */
107 IWL_DECLARE_RATE_INFO(24, 3, 18, 36), /* 24mbps ; MCS 3 */
108 IWL_DECLARE_RATE_INFO(36, 4, 24, 48), /* 36mbps ; MCS 4 */
109 IWL_DECLARE_RATE_INFO(48, 5, 36, 54), /* 48mbps ; MCS 5 */
110 IWL_DECLARE_RATE_INFO(54, 6, 48, INV), /* 54mbps ; MCS 6 */
111 IWL_DECLARE_MCS_RATE(7), /* MCS 7 */
112 IWL_DECLARE_MCS_RATE(8), /* MCS 8 */
113 IWL_DECLARE_MCS_RATE(9), /* MCS 9 */
Johannes Berg8ca151b2013-01-24 14:25:36 +0100114};
115
Eyal Shapira4e4b8152013-12-11 00:55:36 +0200116enum rs_action {
117 RS_ACTION_STAY = 0,
118 RS_ACTION_DOWNSCALE = -1,
119 RS_ACTION_UPSCALE = 1,
120};
121
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200122enum rs_column_mode {
123 RS_INVALID = 0,
124 RS_LEGACY,
125 RS_SISO,
126 RS_MIMO2,
127};
128
Eyal Shapirafd7dbee2014-04-06 04:39:23 +0300129#define MAX_NEXT_COLUMNS 7
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200130#define MAX_COLUMN_CHECKS 3
131
Eyal Shapira0b8d17f2015-02-02 15:21:27 +0200132struct rs_tx_column;
133
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200134typedef bool (*allow_column_func_t) (struct iwl_mvm *mvm,
135 struct ieee80211_sta *sta,
Eyal Shapirabb8f44c2015-05-27 22:00:03 +0300136 struct rs_rate *rate,
Eyal Shapira0b8d17f2015-02-02 15:21:27 +0200137 const struct rs_tx_column *next_col);
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200138
139struct rs_tx_column {
140 enum rs_column_mode mode;
141 u8 ant;
142 bool sgi;
143 enum rs_column next_columns[MAX_NEXT_COLUMNS];
144 allow_column_func_t checks[MAX_COLUMN_CHECKS];
145};
146
Emmanuel Grumbach219fb662014-10-30 11:59:40 +0200147static bool rs_ant_allow(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
Eyal Shapirabb8f44c2015-05-27 22:00:03 +0300148 struct rs_rate *rate,
Eyal Shapira0b8d17f2015-02-02 15:21:27 +0200149 const struct rs_tx_column *next_col)
Emmanuel Grumbach219fb662014-10-30 11:59:40 +0200150{
Eyal Shapira0b8d17f2015-02-02 15:21:27 +0200151 return iwl_mvm_bt_coex_is_ant_avail(mvm, next_col->ant);
Emmanuel Grumbach219fb662014-10-30 11:59:40 +0200152}
153
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200154static bool rs_mimo_allow(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
Eyal Shapirabb8f44c2015-05-27 22:00:03 +0300155 struct rs_rate *rate,
Eyal Shapira0b8d17f2015-02-02 15:21:27 +0200156 const struct rs_tx_column *next_col)
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200157{
158 if (!sta->ht_cap.ht_supported)
159 return false;
160
161 if (sta->smps_mode == IEEE80211_SMPS_STATIC)
162 return false;
163
Moshe Harela0544272014-12-08 21:13:14 +0200164 if (num_of_ant(iwl_mvm_get_valid_tx_ant(mvm)) < 2)
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200165 return false;
166
167 if (!iwl_mvm_bt_coex_is_mimo_allowed(mvm, sta))
168 return false;
169
Emmanuel Grumbach5f0d98f2014-10-30 10:19:38 +0200170 if (mvm->nvm_data->sku_cap_mimo_disabled)
171 return false;
172
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200173 return true;
174}
175
176static bool rs_siso_allow(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
Eyal Shapirabb8f44c2015-05-27 22:00:03 +0300177 struct rs_rate *rate,
Eyal Shapira0b8d17f2015-02-02 15:21:27 +0200178 const struct rs_tx_column *next_col)
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200179{
180 if (!sta->ht_cap.ht_supported)
181 return false;
182
183 return true;
184}
185
186static bool rs_sgi_allow(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
Eyal Shapirabb8f44c2015-05-27 22:00:03 +0300187 struct rs_rate *rate,
Eyal Shapira0b8d17f2015-02-02 15:21:27 +0200188 const struct rs_tx_column *next_col)
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200189{
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200190 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
191 struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
192
193 if (is_ht20(rate) && (ht_cap->cap &
194 IEEE80211_HT_CAP_SGI_20))
195 return true;
196 if (is_ht40(rate) && (ht_cap->cap &
197 IEEE80211_HT_CAP_SGI_40))
198 return true;
199 if (is_ht80(rate) && (vht_cap->cap &
200 IEEE80211_VHT_CAP_SHORT_GI_80))
201 return true;
Gregory Greenman855f4922016-05-25 18:31:39 +0300202 if (is_ht160(rate) && (vht_cap->cap &
203 IEEE80211_VHT_CAP_SHORT_GI_160))
204 return true;
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200205
206 return false;
207}
208
209static const struct rs_tx_column rs_tx_columns[] = {
210 [RS_COLUMN_LEGACY_ANT_A] = {
211 .mode = RS_LEGACY,
212 .ant = ANT_A,
213 .next_columns = {
214 RS_COLUMN_LEGACY_ANT_B,
215 RS_COLUMN_SISO_ANT_A,
Eyal Shapira31b20452014-04-08 17:52:27 +0300216 RS_COLUMN_MIMO2,
Eyal Shapirad8fff912014-04-06 05:27:36 +0300217 RS_COLUMN_INVALID,
218 RS_COLUMN_INVALID,
Eyal Shapirafd7dbee2014-04-06 04:39:23 +0300219 RS_COLUMN_INVALID,
220 RS_COLUMN_INVALID,
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200221 },
Emmanuel Grumbach219fb662014-10-30 11:59:40 +0200222 .checks = {
223 rs_ant_allow,
224 },
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200225 },
226 [RS_COLUMN_LEGACY_ANT_B] = {
227 .mode = RS_LEGACY,
228 .ant = ANT_B,
229 .next_columns = {
230 RS_COLUMN_LEGACY_ANT_A,
231 RS_COLUMN_SISO_ANT_B,
Eyal Shapira31b20452014-04-08 17:52:27 +0300232 RS_COLUMN_MIMO2,
Eyal Shapirad8fff912014-04-06 05:27:36 +0300233 RS_COLUMN_INVALID,
234 RS_COLUMN_INVALID,
Eyal Shapirafd7dbee2014-04-06 04:39:23 +0300235 RS_COLUMN_INVALID,
236 RS_COLUMN_INVALID,
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200237 },
Emmanuel Grumbach219fb662014-10-30 11:59:40 +0200238 .checks = {
239 rs_ant_allow,
240 },
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200241 },
242 [RS_COLUMN_SISO_ANT_A] = {
243 .mode = RS_SISO,
244 .ant = ANT_A,
245 .next_columns = {
246 RS_COLUMN_SISO_ANT_B,
247 RS_COLUMN_MIMO2,
248 RS_COLUMN_SISO_ANT_A_SGI,
Eyal Shapirafd7dbee2014-04-06 04:39:23 +0300249 RS_COLUMN_LEGACY_ANT_A,
250 RS_COLUMN_LEGACY_ANT_B,
Eyal Shapirad8fff912014-04-06 05:27:36 +0300251 RS_COLUMN_INVALID,
Eyal Shapira31b20452014-04-08 17:52:27 +0300252 RS_COLUMN_INVALID,
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200253 },
254 .checks = {
255 rs_siso_allow,
Emmanuel Grumbach219fb662014-10-30 11:59:40 +0200256 rs_ant_allow,
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200257 },
258 },
259 [RS_COLUMN_SISO_ANT_B] = {
260 .mode = RS_SISO,
261 .ant = ANT_B,
262 .next_columns = {
263 RS_COLUMN_SISO_ANT_A,
264 RS_COLUMN_MIMO2,
265 RS_COLUMN_SISO_ANT_B_SGI,
Eyal Shapirafd7dbee2014-04-06 04:39:23 +0300266 RS_COLUMN_LEGACY_ANT_A,
267 RS_COLUMN_LEGACY_ANT_B,
Eyal Shapirad8fff912014-04-06 05:27:36 +0300268 RS_COLUMN_INVALID,
Eyal Shapira31b20452014-04-08 17:52:27 +0300269 RS_COLUMN_INVALID,
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200270 },
271 .checks = {
272 rs_siso_allow,
Emmanuel Grumbach219fb662014-10-30 11:59:40 +0200273 rs_ant_allow,
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200274 },
275 },
276 [RS_COLUMN_SISO_ANT_A_SGI] = {
277 .mode = RS_SISO,
278 .ant = ANT_A,
279 .sgi = true,
280 .next_columns = {
281 RS_COLUMN_SISO_ANT_B_SGI,
282 RS_COLUMN_MIMO2_SGI,
283 RS_COLUMN_SISO_ANT_A,
Eyal Shapirafd7dbee2014-04-06 04:39:23 +0300284 RS_COLUMN_LEGACY_ANT_A,
285 RS_COLUMN_LEGACY_ANT_B,
Eyal Shapira31b20452014-04-08 17:52:27 +0300286 RS_COLUMN_INVALID,
287 RS_COLUMN_INVALID,
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200288 },
289 .checks = {
290 rs_siso_allow,
Emmanuel Grumbach219fb662014-10-30 11:59:40 +0200291 rs_ant_allow,
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200292 rs_sgi_allow,
293 },
294 },
295 [RS_COLUMN_SISO_ANT_B_SGI] = {
296 .mode = RS_SISO,
297 .ant = ANT_B,
298 .sgi = true,
299 .next_columns = {
300 RS_COLUMN_SISO_ANT_A_SGI,
301 RS_COLUMN_MIMO2_SGI,
302 RS_COLUMN_SISO_ANT_B,
Eyal Shapirafd7dbee2014-04-06 04:39:23 +0300303 RS_COLUMN_LEGACY_ANT_A,
304 RS_COLUMN_LEGACY_ANT_B,
Eyal Shapira31b20452014-04-08 17:52:27 +0300305 RS_COLUMN_INVALID,
306 RS_COLUMN_INVALID,
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200307 },
308 .checks = {
309 rs_siso_allow,
Emmanuel Grumbach219fb662014-10-30 11:59:40 +0200310 rs_ant_allow,
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200311 rs_sgi_allow,
312 },
313 },
314 [RS_COLUMN_MIMO2] = {
315 .mode = RS_MIMO2,
316 .ant = ANT_AB,
317 .next_columns = {
318 RS_COLUMN_SISO_ANT_A,
319 RS_COLUMN_MIMO2_SGI,
Eyal Shapirafd7dbee2014-04-06 04:39:23 +0300320 RS_COLUMN_LEGACY_ANT_A,
321 RS_COLUMN_LEGACY_ANT_B,
Eyal Shapira31b20452014-04-08 17:52:27 +0300322 RS_COLUMN_INVALID,
323 RS_COLUMN_INVALID,
324 RS_COLUMN_INVALID,
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200325 },
326 .checks = {
327 rs_mimo_allow,
328 },
329 },
330 [RS_COLUMN_MIMO2_SGI] = {
331 .mode = RS_MIMO2,
332 .ant = ANT_AB,
333 .sgi = true,
334 .next_columns = {
335 RS_COLUMN_SISO_ANT_A_SGI,
336 RS_COLUMN_MIMO2,
Eyal Shapirafd7dbee2014-04-06 04:39:23 +0300337 RS_COLUMN_LEGACY_ANT_A,
338 RS_COLUMN_LEGACY_ANT_B,
Eyal Shapira31b20452014-04-08 17:52:27 +0300339 RS_COLUMN_INVALID,
340 RS_COLUMN_INVALID,
341 RS_COLUMN_INVALID,
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200342 },
343 .checks = {
344 rs_mimo_allow,
345 rs_sgi_allow,
346 },
347 },
348};
349
Johannes Berg8ca151b2013-01-24 14:25:36 +0100350static inline u8 rs_extract_rate(u32 rate_n_flags)
351{
352 /* also works for HT because bits 7:6 are zero there */
353 return (u8)(rate_n_flags & RATE_LEGACY_RATE_MSK);
354}
355
356static int iwl_hwrate_to_plcp_idx(u32 rate_n_flags)
357{
358 int idx = 0;
359
Johannes Berg8ca151b2013-01-24 14:25:36 +0100360 if (rate_n_flags & RATE_MCS_HT_MSK) {
Eyal Shapirad310e402013-08-11 18:43:47 +0300361 idx = rate_n_flags & RATE_HT_MCS_RATE_CODE_MSK;
362 idx += IWL_RATE_MCS_0_INDEX;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100363
Eyal Shapirad310e402013-08-11 18:43:47 +0300364 /* skip 9M not supported in HT*/
Johannes Berg8ca151b2013-01-24 14:25:36 +0100365 if (idx >= IWL_RATE_9M_INDEX)
366 idx += 1;
Eyal Shapira4e82dd32013-08-04 23:58:37 +0300367 if ((idx >= IWL_FIRST_HT_RATE) && (idx <= IWL_LAST_HT_RATE))
Johannes Berg8ca151b2013-01-24 14:25:36 +0100368 return idx;
Eyal Shapirad310e402013-08-11 18:43:47 +0300369 } else if (rate_n_flags & RATE_MCS_VHT_MSK) {
370 idx = rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK;
371 idx += IWL_RATE_MCS_0_INDEX;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100372
Eyal Shapirad310e402013-08-11 18:43:47 +0300373 /* skip 9M not supported in VHT*/
374 if (idx >= IWL_RATE_9M_INDEX)
375 idx++;
376 if ((idx >= IWL_FIRST_VHT_RATE) && (idx <= IWL_LAST_VHT_RATE))
377 return idx;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100378 } else {
Eyal Shapirad310e402013-08-11 18:43:47 +0300379 /* legacy rate format, search for match in table */
380
381 u8 legacy_rate = rs_extract_rate(rate_n_flags);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100382 for (idx = 0; idx < ARRAY_SIZE(iwl_rates); idx++)
Eyal Shapirad310e402013-08-11 18:43:47 +0300383 if (iwl_rates[idx].plcp == legacy_rate)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100384 return idx;
385 }
386
Eyal Shapira560843f2014-01-05 21:04:19 +0200387 return IWL_RATE_INVALID;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100388}
389
390static void rs_rate_scale_perform(struct iwl_mvm *mvm,
Eyal Shapira1ebc8f22014-09-14 15:23:23 +0300391 struct ieee80211_sta *sta,
392 struct iwl_lq_sta *lq_sta,
Matti Gottliebff7a68d2016-05-17 10:50:08 +0300393 int tid, bool ndp);
Eyal Shapira8fc7c582013-12-04 02:15:46 +0200394static void rs_fill_lq_cmd(struct iwl_mvm *mvm,
395 struct ieee80211_sta *sta,
396 struct iwl_lq_sta *lq_sta,
397 const struct rs_rate *initial_rate);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100398static void rs_stay_in_table(struct iwl_lq_sta *lq_sta, bool force_search);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100399
Johannes Berg8ca151b2013-01-24 14:25:36 +0100400/**
401 * The following tables contain the expected throughput metrics for all rates
402 *
403 * 1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
404 *
405 * where invalid entries are zeros.
406 *
407 * CCK rates are only valid in legacy table and will only be used in G
408 * (2.4 GHz) band.
409 */
410
Johannes Berga34529e2014-01-20 22:57:21 +0100411static const u16 expected_tpt_legacy[IWL_RATE_COUNT] = {
Eyal Shapira4e82dd32013-08-04 23:58:37 +0300412 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 0, 0, 0
Johannes Berg8ca151b2013-01-24 14:25:36 +0100413};
414
Eyal Shapira4e82dd32013-08-04 23:58:37 +0300415/* Expected TpT tables. 4 indexes:
416 * 0 - NGI, 1 - SGI, 2 - AGG+NGI, 3 - AGG+SGI
417 */
Johannes Berga34529e2014-01-20 22:57:21 +0100418static const u16 expected_tpt_siso_20MHz[4][IWL_RATE_COUNT] = {
Eyal Shapira4e82dd32013-08-04 23:58:37 +0300419 {0, 0, 0, 0, 42, 0, 76, 102, 124, 159, 183, 193, 202, 216, 0},
420 {0, 0, 0, 0, 46, 0, 82, 110, 132, 168, 192, 202, 210, 225, 0},
421 {0, 0, 0, 0, 49, 0, 97, 145, 192, 285, 375, 420, 464, 551, 0},
422 {0, 0, 0, 0, 54, 0, 108, 160, 213, 315, 415, 465, 513, 608, 0},
Johannes Berg8ca151b2013-01-24 14:25:36 +0100423};
424
Johannes Berga34529e2014-01-20 22:57:21 +0100425static const u16 expected_tpt_siso_40MHz[4][IWL_RATE_COUNT] = {
Eyal Shapira4e82dd32013-08-04 23:58:37 +0300426 {0, 0, 0, 0, 77, 0, 127, 160, 184, 220, 242, 250, 257, 269, 275},
427 {0, 0, 0, 0, 83, 0, 135, 169, 193, 229, 250, 257, 264, 275, 280},
428 {0, 0, 0, 0, 101, 0, 199, 295, 389, 570, 744, 828, 911, 1070, 1173},
429 {0, 0, 0, 0, 112, 0, 220, 326, 429, 629, 819, 912, 1000, 1173, 1284},
Johannes Berg8ca151b2013-01-24 14:25:36 +0100430};
431
Johannes Berga34529e2014-01-20 22:57:21 +0100432static const u16 expected_tpt_siso_80MHz[4][IWL_RATE_COUNT] = {
Eyal Shapirad310e402013-08-11 18:43:47 +0300433 {0, 0, 0, 0, 130, 0, 191, 223, 244, 273, 288, 294, 298, 305, 308},
434 {0, 0, 0, 0, 138, 0, 200, 231, 251, 279, 293, 298, 302, 308, 312},
435 {0, 0, 0, 0, 217, 0, 429, 634, 834, 1220, 1585, 1760, 1931, 2258, 2466},
436 {0, 0, 0, 0, 241, 0, 475, 701, 921, 1343, 1741, 1931, 2117, 2468, 2691},
437};
438
Gregory Greenman855f4922016-05-25 18:31:39 +0300439static const u16 expected_tpt_siso_160MHz[4][IWL_RATE_COUNT] = {
440 {0, 0, 0, 0, 191, 0, 244, 288, 298, 308, 313, 318, 323, 328, 330},
441 {0, 0, 0, 0, 200, 0, 251, 293, 302, 312, 317, 322, 327, 332, 334},
442 {0, 0, 0, 0, 439, 0, 875, 1307, 1736, 2584, 3419, 3831, 4240, 5049, 5581},
443 {0, 0, 0, 0, 488, 0, 972, 1451, 1925, 2864, 3785, 4240, 4691, 5581, 6165},
444};
445
Johannes Berga34529e2014-01-20 22:57:21 +0100446static const u16 expected_tpt_mimo2_20MHz[4][IWL_RATE_COUNT] = {
Eyal Shapira4e82dd32013-08-04 23:58:37 +0300447 {0, 0, 0, 0, 74, 0, 123, 155, 179, 213, 235, 243, 250, 261, 0},
448 {0, 0, 0, 0, 81, 0, 131, 164, 187, 221, 242, 250, 256, 267, 0},
449 {0, 0, 0, 0, 98, 0, 193, 286, 375, 550, 718, 799, 878, 1032, 0},
450 {0, 0, 0, 0, 109, 0, 214, 316, 414, 607, 790, 879, 965, 1132, 0},
Johannes Berg8ca151b2013-01-24 14:25:36 +0100451};
452
Johannes Berga34529e2014-01-20 22:57:21 +0100453static const u16 expected_tpt_mimo2_40MHz[4][IWL_RATE_COUNT] = {
Eyal Shapira4e82dd32013-08-04 23:58:37 +0300454 {0, 0, 0, 0, 123, 0, 182, 214, 235, 264, 279, 285, 289, 296, 300},
455 {0, 0, 0, 0, 131, 0, 191, 222, 242, 270, 284, 289, 293, 300, 303},
456 {0, 0, 0, 0, 200, 0, 390, 571, 741, 1067, 1365, 1505, 1640, 1894, 2053},
457 {0, 0, 0, 0, 221, 0, 430, 630, 816, 1169, 1490, 1641, 1784, 2053, 2221},
Johannes Berg8ca151b2013-01-24 14:25:36 +0100458};
459
Johannes Berga34529e2014-01-20 22:57:21 +0100460static const u16 expected_tpt_mimo2_80MHz[4][IWL_RATE_COUNT] = {
Eyal Shapirad310e402013-08-11 18:43:47 +0300461 {0, 0, 0, 0, 182, 0, 240, 264, 278, 299, 308, 311, 313, 317, 319},
462 {0, 0, 0, 0, 190, 0, 247, 269, 282, 302, 310, 313, 315, 319, 320},
463 {0, 0, 0, 0, 428, 0, 833, 1215, 1577, 2254, 2863, 3147, 3418, 3913, 4219},
464 {0, 0, 0, 0, 474, 0, 920, 1338, 1732, 2464, 3116, 3418, 3705, 4225, 4545},
465};
466
Gregory Greenman855f4922016-05-25 18:31:39 +0300467static const u16 expected_tpt_mimo2_160MHz[4][IWL_RATE_COUNT] = {
468 {0, 0, 0, 0, 240, 0, 278, 308, 313, 319, 322, 324, 328, 330, 334},
469 {0, 0, 0, 0, 247, 0, 282, 310, 315, 320, 323, 325, 329, 332, 338},
470 {0, 0, 0, 0, 875, 0, 1735, 2582, 3414, 5043, 6619, 7389, 8147, 9629, 10592},
471 {0, 0, 0, 0, 971, 0, 1925, 2861, 3779, 5574, 7304, 8147, 8976, 10592, 11640},
472};
473
Johannes Berg8ca151b2013-01-24 14:25:36 +0100474/* mbps, mcs */
475static const struct iwl_rate_mcs_info iwl_rate_mcs[IWL_RATE_COUNT] = {
476 { "1", "BPSK DSSS"},
477 { "2", "QPSK DSSS"},
478 {"5.5", "BPSK CCK"},
479 { "11", "QPSK CCK"},
480 { "6", "BPSK 1/2"},
481 { "9", "BPSK 1/2"},
482 { "12", "QPSK 1/2"},
483 { "18", "QPSK 3/4"},
484 { "24", "16QAM 1/2"},
485 { "36", "16QAM 3/4"},
486 { "48", "64QAM 2/3"},
487 { "54", "64QAM 3/4"},
488 { "60", "64QAM 5/6"},
489};
490
491#define MCS_INDEX_PER_STREAM (8)
492
Emmanuel Grumbach9d108492013-12-03 11:50:30 +0200493static const char *rs_pretty_ant(u8 ant)
494{
495 static const char * const ant_name[] = {
496 [ANT_NONE] = "None",
497 [ANT_A] = "A",
498 [ANT_B] = "B",
499 [ANT_AB] = "AB",
500 [ANT_C] = "C",
501 [ANT_AC] = "AC",
502 [ANT_BC] = "BC",
503 [ANT_ABC] = "ABC",
504 };
505
506 if (ant > ANT_ABC)
507 return "UNKNOWN";
508
509 return ant_name[ant];
510}
511
512static const char *rs_pretty_lq_type(enum iwl_table_type type)
513{
514 static const char * const lq_types[] = {
515 [LQ_NONE] = "NONE",
516 [LQ_LEGACY_A] = "LEGACY_A",
517 [LQ_LEGACY_G] = "LEGACY_G",
518 [LQ_HT_SISO] = "HT SISO",
519 [LQ_HT_MIMO2] = "HT MIMO",
520 [LQ_VHT_SISO] = "VHT SISO",
521 [LQ_VHT_MIMO2] = "VHT MIMO",
522 };
523
524 if (type < LQ_NONE || type >= LQ_MAX)
525 return "UNKNOWN";
526
527 return lq_types[type];
528}
529
Eyal Shapiracc60c6e2015-09-19 23:37:45 +0300530static char *rs_pretty_rate(const struct rs_rate *rate)
531{
532 static char buf[40];
533 static const char * const legacy_rates[] = {
534 [IWL_RATE_1M_INDEX] = "1M",
535 [IWL_RATE_2M_INDEX] = "2M",
536 [IWL_RATE_5M_INDEX] = "5.5M",
537 [IWL_RATE_11M_INDEX] = "11M",
538 [IWL_RATE_6M_INDEX] = "6M",
539 [IWL_RATE_9M_INDEX] = "9M",
540 [IWL_RATE_12M_INDEX] = "12M",
541 [IWL_RATE_18M_INDEX] = "18M",
542 [IWL_RATE_24M_INDEX] = "24M",
543 [IWL_RATE_36M_INDEX] = "36M",
544 [IWL_RATE_48M_INDEX] = "48M",
545 [IWL_RATE_54M_INDEX] = "54M",
546 };
547 static const char *const ht_vht_rates[] = {
548 [IWL_RATE_MCS_0_INDEX] = "MCS0",
549 [IWL_RATE_MCS_1_INDEX] = "MCS1",
550 [IWL_RATE_MCS_2_INDEX] = "MCS2",
551 [IWL_RATE_MCS_3_INDEX] = "MCS3",
552 [IWL_RATE_MCS_4_INDEX] = "MCS4",
553 [IWL_RATE_MCS_5_INDEX] = "MCS5",
554 [IWL_RATE_MCS_6_INDEX] = "MCS6",
555 [IWL_RATE_MCS_7_INDEX] = "MCS7",
556 [IWL_RATE_MCS_8_INDEX] = "MCS8",
557 [IWL_RATE_MCS_9_INDEX] = "MCS9",
558 };
559 const char *rate_str;
560
Eyal Shapirae8b3f7b2015-11-19 18:37:31 +0200561 if (is_type_legacy(rate->type) && (rate->index <= IWL_RATE_54M_INDEX))
Eyal Shapiracc60c6e2015-09-19 23:37:45 +0300562 rate_str = legacy_rates[rate->index];
Eyal Shapirae8b3f7b2015-11-19 18:37:31 +0200563 else if ((is_type_ht(rate->type) || is_type_vht(rate->type)) &&
Eyal Shapira44945412016-02-01 09:07:05 +0200564 (rate->index >= IWL_RATE_MCS_0_INDEX) &&
Eyal Shapirae8b3f7b2015-11-19 18:37:31 +0200565 (rate->index <= IWL_RATE_MCS_9_INDEX))
Eyal Shapiracc60c6e2015-09-19 23:37:45 +0300566 rate_str = ht_vht_rates[rate->index];
567 else
568 rate_str = "BAD_RATE";
569
570 sprintf(buf, "(%s|%s|%s)", rs_pretty_lq_type(rate->type),
571 rs_pretty_ant(rate->ant), rate_str);
572 return buf;
573}
574
Eyal Shapira5aa33552013-11-23 01:06:36 +0200575static inline void rs_dump_rate(struct iwl_mvm *mvm, const struct rs_rate *rate,
576 const char *prefix)
577{
Eyal Shapirad8bafea2014-10-23 13:58:38 +0300578 IWL_DEBUG_RATE(mvm,
Eyal Shapiracc60c6e2015-09-19 23:37:45 +0300579 "%s: %s BW: %d SGI: %d LDPC: %d STBC: %d\n",
580 prefix, rs_pretty_rate(rate), rate->bw,
581 rate->sgi, rate->ldpc, rate->stbc);
Eyal Shapira5aa33552013-11-23 01:06:36 +0200582}
583
Johannes Berg8ca151b2013-01-24 14:25:36 +0100584static void rs_rate_scale_clear_window(struct iwl_rate_scale_data *window)
585{
586 window->data = 0;
587 window->success_counter = 0;
588 window->success_ratio = IWL_INVALID_VALUE;
589 window->counter = 0;
590 window->average_tpt = IWL_INVALID_VALUE;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100591}
592
Eyal Shapirab804eeb2014-04-06 04:27:06 +0300593static void rs_rate_scale_clear_tbl_windows(struct iwl_mvm *mvm,
594 struct iwl_scale_tbl_info *tbl)
Eliad Peller3ca71f62014-03-10 16:33:43 +0200595{
596 int i;
597
Eyal Shapirab804eeb2014-04-06 04:27:06 +0300598 IWL_DEBUG_RATE(mvm, "Clearing up window stats\n");
Eliad Peller3ca71f62014-03-10 16:33:43 +0200599 for (i = 0; i < IWL_RATE_COUNT; i++)
600 rs_rate_scale_clear_window(&tbl->win[i]);
Eliad Peller2fd647f2014-03-13 17:21:36 +0200601
602 for (i = 0; i < ARRAY_SIZE(tbl->tpc_win); i++)
603 rs_rate_scale_clear_window(&tbl->tpc_win[i]);
Eliad Peller3ca71f62014-03-10 16:33:43 +0200604}
605
Johannes Berg8ca151b2013-01-24 14:25:36 +0100606static inline u8 rs_is_valid_ant(u8 valid_antenna, u8 ant_type)
607{
608 return (ant_type & valid_antenna) == ant_type;
609}
610
Johannes Berg8ca151b2013-01-24 14:25:36 +0100611static int rs_tl_turn_on_agg_for_tid(struct iwl_mvm *mvm,
Eyal Shapira40ce5ed2015-09-19 23:54:38 +0300612 struct iwl_lq_sta *lq_data, u8 tid,
613 struct ieee80211_sta *sta)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100614{
615 int ret = -EAGAIN;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100616
Emmanuel Grumbach44cc4292013-06-18 06:33:49 +0300617 IWL_DEBUG_HT(mvm, "Starting Tx agg: STA: %pM tid: %d\n",
618 sta->addr, tid);
Gregory Greenman482e4842017-08-15 12:27:01 +0300619
620 /* start BA session until the peer sends del BA */
621 ret = ieee80211_start_tx_ba_session(sta, tid, 0);
Emmanuel Grumbach44cc4292013-06-18 06:33:49 +0300622 if (ret == -EAGAIN) {
623 /*
624 * driver and mac80211 is out of sync
625 * this might be cause by reloading firmware
626 * stop the tx ba session here
627 */
628 IWL_ERR(mvm, "Fail start Tx agg on tid: %d\n",
629 tid);
630 ieee80211_stop_tx_ba_session(sta, tid);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100631 }
632 return ret;
633}
634
Gregory Greenman482e4842017-08-15 12:27:01 +0300635static void rs_tl_turn_on_agg(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
636 u8 tid, struct iwl_lq_sta *lq_sta,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100637 struct ieee80211_sta *sta)
638{
Gregory Greenman482e4842017-08-15 12:27:01 +0300639 struct iwl_mvm_tid_data *tid_data;
640
641 /*
642 * In AP mode, tid can be equal to IWL_MAX_TID_COUNT
643 * when the frame is not QoS
644 */
645 if (WARN_ON_ONCE(tid > IWL_MAX_TID_COUNT)) {
Johannes Berg8ca151b2013-01-24 14:25:36 +0100646 IWL_ERR(mvm, "tid exceeds max TID count: %d/%d\n",
647 tid, IWL_MAX_TID_COUNT);
Gregory Greenman482e4842017-08-15 12:27:01 +0300648 return;
649 } else if (tid == IWL_MAX_TID_COUNT) {
650 return;
651 }
652
653 tid_data = &mvmsta->tid_data[tid];
654 if ((tid_data->state == IWL_AGG_OFF) &&
655 (lq_sta->tx_agg_tid_en & BIT(tid)) &&
656 (tid_data->tx_count_last >= IWL_MVM_RS_AGG_START_THRESHOLD)) {
657 IWL_DEBUG_RATE(mvm, "try to aggregate tid %d\n", tid);
Naftali Goldsteinbd800e42017-08-28 11:51:05 +0300658 if (rs_tl_turn_on_agg_for_tid(mvm, lq_sta, tid, sta) == 0)
659 tid_data->state = IWL_AGG_QUEUED;
Gregory Greenman482e4842017-08-15 12:27:01 +0300660 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100661}
662
663static inline int get_num_of_ant_from_rate(u32 rate_n_flags)
664{
665 return !!(rate_n_flags & RATE_MCS_ANT_A_MSK) +
666 !!(rate_n_flags & RATE_MCS_ANT_B_MSK) +
667 !!(rate_n_flags & RATE_MCS_ANT_C_MSK);
668}
669
670/*
671 * Static function to get the expected throughput from an iwl_scale_tbl_info
672 * that wraps a NULL pointer check
673 */
674static s32 get_expected_tpt(struct iwl_scale_tbl_info *tbl, int rs_index)
675{
676 if (tbl->expected_tpt)
677 return tbl->expected_tpt[rs_index];
678 return 0;
679}
680
681/**
682 * rs_collect_tx_data - Update the success/failure sliding window
683 *
684 * We keep a sliding window of the last 62 packets transmitted
685 * at this rate. window->data contains the bitmask of successful
686 * packets.
687 */
Eyal Shapira834437d2014-12-17 15:38:58 +0200688static int _rs_collect_tx_data(struct iwl_mvm *mvm,
689 struct iwl_scale_tbl_info *tbl,
Eliad Peller0bd3c5a2014-03-10 16:07:04 +0200690 int scale_index, int attempts, int successes,
691 struct iwl_rate_scale_data *window)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100692{
Johannes Berg8ca151b2013-01-24 14:25:36 +0100693 static const u64 mask = (((u64)1) << (IWL_RATE_MAX_WINDOW - 1));
694 s32 fail_count, tpt;
695
Johannes Berg8ca151b2013-01-24 14:25:36 +0100696 /* Get expected throughput */
697 tpt = get_expected_tpt(tbl, scale_index);
698
699 /*
700 * Keep track of only the latest 62 tx frame attempts in this rate's
701 * history window; anything older isn't really relevant any more.
702 * If we have filled up the sliding window, drop the oldest attempt;
703 * if the oldest attempt (highest bit in bitmap) shows "success",
704 * subtract "1" from the success counter (this is the main reason
705 * we keep these bitmaps!).
706 */
707 while (attempts > 0) {
708 if (window->counter >= IWL_RATE_MAX_WINDOW) {
709 /* remove earliest */
710 window->counter = IWL_RATE_MAX_WINDOW - 1;
711
712 if (window->data & mask) {
713 window->data &= ~mask;
714 window->success_counter--;
715 }
716 }
717
718 /* Increment frames-attempted counter */
719 window->counter++;
720
721 /* Shift bitmap by one frame to throw away oldest history */
722 window->data <<= 1;
723
724 /* Mark the most recent #successes attempts as successful */
725 if (successes > 0) {
726 window->success_counter++;
727 window->data |= 0x1;
728 successes--;
729 }
730
731 attempts--;
732 }
733
734 /* Calculate current success ratio, avoid divide-by-0! */
735 if (window->counter > 0)
736 window->success_ratio = 128 * (100 * window->success_counter)
737 / window->counter;
738 else
739 window->success_ratio = IWL_INVALID_VALUE;
740
741 fail_count = window->counter - window->success_counter;
742
743 /* Calculate average throughput, if we have enough history. */
Eyal Shapira834437d2014-12-17 15:38:58 +0200744 if ((fail_count >= IWL_MVM_RS_RATE_MIN_FAILURE_TH) ||
745 (window->success_counter >= IWL_MVM_RS_RATE_MIN_SUCCESS_TH))
Johannes Berg8ca151b2013-01-24 14:25:36 +0100746 window->average_tpt = (window->success_ratio * tpt + 64) / 128;
747 else
748 window->average_tpt = IWL_INVALID_VALUE;
749
Johannes Berg8ca151b2013-01-24 14:25:36 +0100750 return 0;
751}
752
Gregory Greenman69c7fda2015-12-29 11:26:35 +0200753static int rs_collect_tpc_data(struct iwl_mvm *mvm,
754 struct iwl_lq_sta *lq_sta,
755 struct iwl_scale_tbl_info *tbl,
756 int scale_index, int attempts, int successes,
757 u8 reduced_txp)
Eliad Peller0bd3c5a2014-03-10 16:07:04 +0200758{
759 struct iwl_rate_scale_data *window = NULL;
Gregory Greenman69c7fda2015-12-29 11:26:35 +0200760
761 if (WARN_ON_ONCE(reduced_txp > TPC_MAX_REDUCTION))
762 return -EINVAL;
763
764 window = &tbl->tpc_win[reduced_txp];
765 return _rs_collect_tx_data(mvm, tbl, scale_index, attempts, successes,
766 window);
767}
768
Gregory Greenman482e4842017-08-15 12:27:01 +0300769static void rs_update_tid_tpt_stats(struct iwl_mvm *mvm,
770 struct iwl_mvm_sta *mvmsta,
771 u8 tid, int successes)
772{
773 struct iwl_mvm_tid_data *tid_data;
774
775 if (tid >= IWL_MAX_TID_COUNT)
776 return;
777
778 tid_data = &mvmsta->tid_data[tid];
779
780 /*
781 * Measure if there're enough successful transmits per second.
782 * These statistics are used only to decide if we can start a
783 * BA session, so it should be updated only when A-MPDU is
784 * off.
785 */
786 if (tid_data->state != IWL_AGG_OFF)
787 return;
788
789 if (time_is_before_jiffies(tid_data->tpt_meas_start + HZ) ||
790 (tid_data->tx_count >= IWL_MVM_RS_AGG_START_THRESHOLD)) {
791 tid_data->tx_count_last = tid_data->tx_count;
792 tid_data->tx_count = 0;
793 tid_data->tpt_meas_start = jiffies;
794 } else {
795 tid_data->tx_count += successes;
796 }
797}
798
Gregory Greenman69c7fda2015-12-29 11:26:35 +0200799static int rs_collect_tlc_data(struct iwl_mvm *mvm,
Gregory Greenman482e4842017-08-15 12:27:01 +0300800 struct iwl_mvm_sta *mvmsta, u8 tid,
Gregory Greenman69c7fda2015-12-29 11:26:35 +0200801 struct iwl_scale_tbl_info *tbl,
802 int scale_index, int attempts, int successes)
803{
804 struct iwl_rate_scale_data *window = NULL;
Eliad Peller0bd3c5a2014-03-10 16:07:04 +0200805
806 if (scale_index < 0 || scale_index >= IWL_RATE_COUNT)
807 return -EINVAL;
808
Eyal Shapira757cf23b2014-03-24 02:12:00 +0200809 if (tbl->column != RS_COLUMN_INVALID) {
Gregory Greenmanecaf71d2017-11-01 07:16:29 +0200810 struct lq_sta_pers *pers = &mvmsta->lq_sta.rs_drv.pers;
Eyal Shapiraf58220f2014-08-16 01:30:30 +0300811
812 pers->tx_stats[tbl->column][scale_index].total += attempts;
813 pers->tx_stats[tbl->column][scale_index].success += successes;
Eyal Shapira757cf23b2014-03-24 02:12:00 +0200814 }
815
Gregory Greenman482e4842017-08-15 12:27:01 +0300816 rs_update_tid_tpt_stats(mvm, mvmsta, tid, successes);
817
Eliad Peller0bd3c5a2014-03-10 16:07:04 +0200818 /* Select window for current tx bit rate */
819 window = &(tbl->win[scale_index]);
Eyal Shapira834437d2014-12-17 15:38:58 +0200820 return _rs_collect_tx_data(mvm, tbl, scale_index, attempts, successes,
Eliad Peller0bd3c5a2014-03-10 16:07:04 +0200821 window);
822}
823
Eyal Shapira5aa33552013-11-23 01:06:36 +0200824/* Convert rs_rate object into ucode rate bitmask */
825static u32 ucode_rate_from_rs_rate(struct iwl_mvm *mvm,
Eyal Shapira8fc7c582013-12-04 02:15:46 +0200826 struct rs_rate *rate)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100827{
Eyal Shapira5aa33552013-11-23 01:06:36 +0200828 u32 ucode_rate = 0;
829 int index = rate->index;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100830
Eyal Shapira5aa33552013-11-23 01:06:36 +0200831 ucode_rate |= ((rate->ant << RATE_MCS_ANT_POS) &
Eyal Shapirad310e402013-08-11 18:43:47 +0300832 RATE_MCS_ANT_ABC_MSK);
833
Eyal Shapira5aa33552013-11-23 01:06:36 +0200834 if (is_legacy(rate)) {
835 ucode_rate |= iwl_rates[index].plcp;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100836 if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
Eyal Shapira5aa33552013-11-23 01:06:36 +0200837 ucode_rate |= RATE_MCS_CCK_MSK;
838 return ucode_rate;
Eyal Shapirad310e402013-08-11 18:43:47 +0300839 }
840
Eyal Shapira5aa33552013-11-23 01:06:36 +0200841 if (is_ht(rate)) {
Eyal Shapirad310e402013-08-11 18:43:47 +0300842 if (index < IWL_FIRST_HT_RATE || index > IWL_LAST_HT_RATE) {
Johannes Berg8ca151b2013-01-24 14:25:36 +0100843 IWL_ERR(mvm, "Invalid HT rate index %d\n", index);
Eyal Shapira4e82dd32013-08-04 23:58:37 +0300844 index = IWL_LAST_HT_RATE;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100845 }
Eyal Shapira5aa33552013-11-23 01:06:36 +0200846 ucode_rate |= RATE_MCS_HT_MSK;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100847
Eyal Shapira5aa33552013-11-23 01:06:36 +0200848 if (is_ht_siso(rate))
849 ucode_rate |= iwl_rates[index].plcp_ht_siso;
850 else if (is_ht_mimo2(rate))
851 ucode_rate |= iwl_rates[index].plcp_ht_mimo2;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100852 else
Eyal Shapirad972ab32013-07-28 23:02:45 +0000853 WARN_ON_ONCE(1);
Eyal Shapira5aa33552013-11-23 01:06:36 +0200854 } else if (is_vht(rate)) {
Eyal Shapirad310e402013-08-11 18:43:47 +0300855 if (index < IWL_FIRST_VHT_RATE || index > IWL_LAST_VHT_RATE) {
856 IWL_ERR(mvm, "Invalid VHT rate index %d\n", index);
857 index = IWL_LAST_VHT_RATE;
858 }
Eyal Shapira5aa33552013-11-23 01:06:36 +0200859 ucode_rate |= RATE_MCS_VHT_MSK;
860 if (is_vht_siso(rate))
861 ucode_rate |= iwl_rates[index].plcp_vht_siso;
862 else if (is_vht_mimo2(rate))
863 ucode_rate |= iwl_rates[index].plcp_vht_mimo2;
Eyal Shapirad310e402013-08-11 18:43:47 +0300864 else
865 WARN_ON_ONCE(1);
866
Johannes Berg8ca151b2013-01-24 14:25:36 +0100867 } else {
Eyal Shapira5aa33552013-11-23 01:06:36 +0200868 IWL_ERR(mvm, "Invalid rate->type %d\n", rate->type);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100869 }
870
Eyal Shapirad8bafea2014-10-23 13:58:38 +0300871 if (is_siso(rate) && rate->stbc) {
872 /* To enable STBC we need to set both a flag and ANT_AB */
873 ucode_rate |= RATE_MCS_ANT_AB_MSK;
Sara Sharon77e40942017-01-11 11:58:38 +0200874 ucode_rate |= RATE_MCS_STBC_MSK;
Eyal Shapirad8bafea2014-10-23 13:58:38 +0300875 }
876
Eyal Shapira5aa33552013-11-23 01:06:36 +0200877 ucode_rate |= rate->bw;
878 if (rate->sgi)
879 ucode_rate |= RATE_MCS_SGI_MSK;
Eyal Shapiraa3576ff2014-08-09 10:57:59 +0300880 if (rate->ldpc)
881 ucode_rate |= RATE_MCS_LDPC_MSK;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100882
Eyal Shapira5aa33552013-11-23 01:06:36 +0200883 return ucode_rate;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100884}
885
Eyal Shapira5aa33552013-11-23 01:06:36 +0200886/* Convert a ucode rate into an rs_rate object */
887static int rs_rate_from_ucode_rate(const u32 ucode_rate,
Johannes Berg57fbcce2016-04-12 15:56:15 +0200888 enum nl80211_band band,
Eyal Shapira5aa33552013-11-23 01:06:36 +0200889 struct rs_rate *rate)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100890{
Eyal Shapira5aa33552013-11-23 01:06:36 +0200891 u32 ant_msk = ucode_rate & RATE_MCS_ANT_ABC_MSK;
892 u8 num_of_ant = get_num_of_ant_from_rate(ucode_rate);
Eyal Shapirad310e402013-08-11 18:43:47 +0300893 u8 nss;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100894
Emmanuel Grumbach9e898f12013-12-22 10:55:47 +0200895 memset(rate, 0, sizeof(*rate));
Eyal Shapira5aa33552013-11-23 01:06:36 +0200896 rate->index = iwl_hwrate_to_plcp_idx(ucode_rate);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100897
Eyal Shapira560843f2014-01-05 21:04:19 +0200898 if (rate->index == IWL_RATE_INVALID)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100899 return -EINVAL;
Eyal Shapira5aa33552013-11-23 01:06:36 +0200900
901 rate->ant = (ant_msk >> RATE_MCS_ANT_POS);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100902
Eyal Shapirad310e402013-08-11 18:43:47 +0300903 /* Legacy */
Eyal Shapira5aa33552013-11-23 01:06:36 +0200904 if (!(ucode_rate & RATE_MCS_HT_MSK) &&
905 !(ucode_rate & RATE_MCS_VHT_MSK)) {
Johannes Berg8ca151b2013-01-24 14:25:36 +0100906 if (num_of_ant == 1) {
Johannes Berg57fbcce2016-04-12 15:56:15 +0200907 if (band == NL80211_BAND_5GHZ)
Eyal Shapira5aa33552013-11-23 01:06:36 +0200908 rate->type = LQ_LEGACY_A;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100909 else
Eyal Shapira5aa33552013-11-23 01:06:36 +0200910 rate->type = LQ_LEGACY_G;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100911 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100912
Eyal Shapirad310e402013-08-11 18:43:47 +0300913 return 0;
914 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100915
Eyal Shapirad310e402013-08-11 18:43:47 +0300916 /* HT or VHT */
Eyal Shapira5aa33552013-11-23 01:06:36 +0200917 if (ucode_rate & RATE_MCS_SGI_MSK)
918 rate->sgi = true;
Eyal Shapiraa3576ff2014-08-09 10:57:59 +0300919 if (ucode_rate & RATE_MCS_LDPC_MSK)
920 rate->ldpc = true;
Sara Sharon77e40942017-01-11 11:58:38 +0200921 if (ucode_rate & RATE_MCS_STBC_MSK)
Eyal Shapirad8bafea2014-10-23 13:58:38 +0300922 rate->stbc = true;
Eyal Shapiraba69d0e2015-01-30 01:38:29 +0200923 if (ucode_rate & RATE_MCS_BF_MSK)
924 rate->bfer = true;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100925
Eyal Shapira5aa33552013-11-23 01:06:36 +0200926 rate->bw = ucode_rate & RATE_MCS_CHAN_WIDTH_MSK;
Eyal Shapirad310e402013-08-11 18:43:47 +0300927
Eyal Shapira5aa33552013-11-23 01:06:36 +0200928 if (ucode_rate & RATE_MCS_HT_MSK) {
929 nss = ((ucode_rate & RATE_HT_MCS_NSS_MSK) >>
Eyal Shapirad310e402013-08-11 18:43:47 +0300930 RATE_HT_MCS_NSS_POS) + 1;
931
932 if (nss == 1) {
Eyal Shapira5aa33552013-11-23 01:06:36 +0200933 rate->type = LQ_HT_SISO;
Eyal Shapiraba69d0e2015-01-30 01:38:29 +0200934 WARN_ONCE(!rate->stbc && !rate->bfer && num_of_ant != 1,
935 "stbc %d bfer %d",
936 rate->stbc, rate->bfer);
Eyal Shapirad310e402013-08-11 18:43:47 +0300937 } else if (nss == 2) {
Eyal Shapira5aa33552013-11-23 01:06:36 +0200938 rate->type = LQ_HT_MIMO2;
Eyal Shapirad310e402013-08-11 18:43:47 +0300939 WARN_ON_ONCE(num_of_ant != 2);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100940 } else {
Eyal Shapirad310e402013-08-11 18:43:47 +0300941 WARN_ON_ONCE(1);
942 }
Eyal Shapira5aa33552013-11-23 01:06:36 +0200943 } else if (ucode_rate & RATE_MCS_VHT_MSK) {
944 nss = ((ucode_rate & RATE_VHT_MCS_NSS_MSK) >>
Eyal Shapirad310e402013-08-11 18:43:47 +0300945 RATE_VHT_MCS_NSS_POS) + 1;
946
947 if (nss == 1) {
Eyal Shapira5aa33552013-11-23 01:06:36 +0200948 rate->type = LQ_VHT_SISO;
Eyal Shapiraba69d0e2015-01-30 01:38:29 +0200949 WARN_ONCE(!rate->stbc && !rate->bfer && num_of_ant != 1,
950 "stbc %d bfer %d",
951 rate->stbc, rate->bfer);
Eyal Shapirad310e402013-08-11 18:43:47 +0300952 } else if (nss == 2) {
Eyal Shapira5aa33552013-11-23 01:06:36 +0200953 rate->type = LQ_VHT_MIMO2;
Eyal Shapirad310e402013-08-11 18:43:47 +0300954 WARN_ON_ONCE(num_of_ant != 2);
955 } else {
956 WARN_ON_ONCE(1);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100957 }
958 }
Eyal Shapirad310e402013-08-11 18:43:47 +0300959
Eyal Shapira5aa33552013-11-23 01:06:36 +0200960 WARN_ON_ONCE(rate->bw == RATE_MCS_CHAN_WIDTH_80 &&
961 !is_vht(rate));
Eyal Shapirad310e402013-08-11 18:43:47 +0300962
Johannes Berg8ca151b2013-01-24 14:25:36 +0100963 return 0;
964}
965
966/* switch to another antenna/antennas and return 1 */
967/* if no other valid antenna found, return 0 */
Eyal Shapira8fc7c582013-12-04 02:15:46 +0200968static int rs_toggle_antenna(u32 valid_ant, struct rs_rate *rate)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100969{
970 u8 new_ant_type;
971
Emmanuel Grumbach82d2b9a2017-10-18 13:09:38 +0300972 if (!rate->ant || WARN_ON_ONCE(rate->ant & ANT_C))
Johannes Berg8ca151b2013-01-24 14:25:36 +0100973 return 0;
974
Eyal Shapira5aa33552013-11-23 01:06:36 +0200975 if (!rs_is_valid_ant(valid_ant, rate->ant))
Johannes Berg8ca151b2013-01-24 14:25:36 +0100976 return 0;
977
Eyal Shapira5aa33552013-11-23 01:06:36 +0200978 new_ant_type = ant_toggle_lookup[rate->ant];
Johannes Berg8ca151b2013-01-24 14:25:36 +0100979
Eyal Shapira5aa33552013-11-23 01:06:36 +0200980 while ((new_ant_type != rate->ant) &&
Johannes Berg8ca151b2013-01-24 14:25:36 +0100981 !rs_is_valid_ant(valid_ant, new_ant_type))
982 new_ant_type = ant_toggle_lookup[new_ant_type];
983
Eyal Shapira5aa33552013-11-23 01:06:36 +0200984 if (new_ant_type == rate->ant)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100985 return 0;
986
Eyal Shapira5aa33552013-11-23 01:06:36 +0200987 rate->ant = new_ant_type;
988
Johannes Berg8ca151b2013-01-24 14:25:36 +0100989 return 1;
990}
991
Johannes Berg8ca151b2013-01-24 14:25:36 +0100992static u16 rs_get_supported_rates(struct iwl_lq_sta *lq_sta,
Eyal Shapira5aa33552013-11-23 01:06:36 +0200993 struct rs_rate *rate)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100994{
Eyal Shapira5aa33552013-11-23 01:06:36 +0200995 if (is_legacy(rate))
Johannes Berg8ca151b2013-01-24 14:25:36 +0100996 return lq_sta->active_legacy_rate;
Eyal Shapira5aa33552013-11-23 01:06:36 +0200997 else if (is_siso(rate))
Eyal Shapirad310e402013-08-11 18:43:47 +0300998 return lq_sta->active_siso_rate;
Eyal Shapira5aa33552013-11-23 01:06:36 +0200999 else if (is_mimo2(rate))
Eyal Shapirad310e402013-08-11 18:43:47 +03001000 return lq_sta->active_mimo2_rate;
1001
1002 WARN_ON_ONCE(1);
1003 return 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001004}
1005
1006static u16 rs_get_adjacent_rate(struct iwl_mvm *mvm, u8 index, u16 rate_mask,
1007 int rate_type)
1008{
1009 u8 high = IWL_RATE_INVALID;
1010 u8 low = IWL_RATE_INVALID;
1011
1012 /* 802.11A or ht walks to the next literal adjacent rate in
1013 * the rate table */
Eyal Shapira5aa33552013-11-23 01:06:36 +02001014 if (is_type_a_band(rate_type) || !is_type_legacy(rate_type)) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001015 int i;
1016 u32 mask;
1017
1018 /* Find the previous rate that is in the rate mask */
1019 i = index - 1;
Emmanuel Grumbach43d59a42017-01-07 19:57:46 +02001020 if (i >= 0)
1021 mask = BIT(i);
1022 for (; i >= 0; i--, mask >>= 1) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001023 if (rate_mask & mask) {
1024 low = i;
1025 break;
1026 }
1027 }
1028
1029 /* Find the next rate that is in the rate mask */
1030 i = index + 1;
1031 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
1032 if (rate_mask & mask) {
1033 high = i;
1034 break;
1035 }
1036 }
1037
1038 return (high << 8) | low;
1039 }
1040
1041 low = index;
1042 while (low != IWL_RATE_INVALID) {
1043 low = iwl_rates[low].prev_rs;
1044 if (low == IWL_RATE_INVALID)
1045 break;
1046 if (rate_mask & (1 << low))
1047 break;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001048 }
1049
1050 high = index;
1051 while (high != IWL_RATE_INVALID) {
1052 high = iwl_rates[high].next_rs;
1053 if (high == IWL_RATE_INVALID)
1054 break;
1055 if (rate_mask & (1 << high))
1056 break;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001057 }
1058
1059 return (high << 8) | low;
1060}
1061
Eyal Shapirae07be6d2013-12-09 13:02:56 +02001062static inline bool rs_rate_supported(struct iwl_lq_sta *lq_sta,
1063 struct rs_rate *rate)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001064{
Eyal Shapirae07be6d2013-12-09 13:02:56 +02001065 return BIT(rate->index) & rs_get_supported_rates(lq_sta, rate);
1066}
1067
1068/* Get the next supported lower rate in the current column.
1069 * Return true if bottom rate in the current column was reached
1070 */
1071static bool rs_get_lower_rate_in_column(struct iwl_lq_sta *lq_sta,
1072 struct rs_rate *rate)
1073{
1074 u8 low;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001075 u16 high_low;
Eyal Shapirae07be6d2013-12-09 13:02:56 +02001076 u16 rate_mask;
Eliad Pellerae969af2014-06-11 15:51:33 +03001077 struct iwl_mvm *mvm = lq_sta->pers.drv;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001078
Eyal Shapirae07be6d2013-12-09 13:02:56 +02001079 rate_mask = rs_get_supported_rates(lq_sta, rate);
1080 high_low = rs_get_adjacent_rate(mvm, rate->index, rate_mask,
1081 rate->type);
1082 low = high_low & 0xff;
1083
1084 /* Bottom rate of column reached */
1085 if (low == IWL_RATE_INVALID)
1086 return true;
1087
1088 rate->index = low;
1089 return false;
1090}
1091
1092/* Get the next rate to use following a column downgrade */
1093static void rs_get_lower_rate_down_column(struct iwl_lq_sta *lq_sta,
1094 struct rs_rate *rate)
1095{
Eliad Pellerae969af2014-06-11 15:51:33 +03001096 struct iwl_mvm *mvm = lq_sta->pers.drv;
Eyal Shapirae07be6d2013-12-09 13:02:56 +02001097
1098 if (is_legacy(rate)) {
1099 /* No column to downgrade from Legacy */
1100 return;
1101 } else if (is_siso(rate)) {
1102 /* Downgrade to Legacy if we were in SISO */
Johannes Berg57fbcce2016-04-12 15:56:15 +02001103 if (lq_sta->band == NL80211_BAND_5GHZ)
Eyal Shapira5aa33552013-11-23 01:06:36 +02001104 rate->type = LQ_LEGACY_A;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001105 else
Eyal Shapira5aa33552013-11-23 01:06:36 +02001106 rate->type = LQ_LEGACY_G;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001107
Eyal Shapiraa56db7d2013-11-28 12:35:42 +02001108 rate->bw = RATE_MCS_CHAN_WIDTH_20;
Eyal Shapirae07be6d2013-12-09 13:02:56 +02001109
Emmanuel Grumbachb8000402014-02-03 22:17:19 +02001110 WARN_ON_ONCE(rate->index < IWL_RATE_MCS_0_INDEX ||
Eyal Shapirae07be6d2013-12-09 13:02:56 +02001111 rate->index > IWL_RATE_MCS_9_INDEX);
1112
1113 rate->index = rs_ht_to_legacy[rate->index];
Eyal Shapiraa3576ff2014-08-09 10:57:59 +03001114 rate->ldpc = false;
Eyal Shapirae07be6d2013-12-09 13:02:56 +02001115 } else {
1116 /* Downgrade to SISO with same MCS if in MIMO */
1117 rate->type = is_vht_mimo2(rate) ?
1118 LQ_VHT_SISO : LQ_HT_SISO;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001119 }
1120
Eyal Shapirae07be6d2013-12-09 13:02:56 +02001121 if (num_of_ant(rate->ant) > 1)
Moshe Harela0544272014-12-08 21:13:14 +02001122 rate->ant = first_antenna(iwl_mvm_get_valid_tx_ant(mvm));
Johannes Berg8ca151b2013-01-24 14:25:36 +01001123
Eyal Shapirae07be6d2013-12-09 13:02:56 +02001124 /* Relevant in both switching to SISO or Legacy */
1125 rate->sgi = false;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001126
Eyal Shapirae07be6d2013-12-09 13:02:56 +02001127 if (!rs_rate_supported(lq_sta, rate))
1128 rs_get_lower_rate_in_column(lq_sta, rate);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001129}
1130
Eyal Shapira929e6ed2015-01-30 13:40:02 +02001131/* Check if both rates share the same column */
1132static inline bool rs_rate_column_match(struct rs_rate *a,
1133 struct rs_rate *b)
1134{
Eyal Shapirad8bafea2014-10-23 13:58:38 +03001135 bool ant_match;
1136
Eyal Shapiraba69d0e2015-01-30 01:38:29 +02001137 if (a->stbc || a->bfer)
Eyal Shapirad8bafea2014-10-23 13:58:38 +03001138 ant_match = (b->ant == ANT_A || b->ant == ANT_B);
1139 else
1140 ant_match = (a->ant == b->ant);
1141
1142 return (a->type == b->type) && (a->bw == b->bw) && (a->sgi == b->sgi)
1143 && ant_match;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001144}
1145
Eyal Shapiraad82d8a2015-03-08 19:43:41 +02001146static inline enum rs_column rs_get_column_from_rate(struct rs_rate *rate)
1147{
1148 if (is_legacy(rate)) {
1149 if (rate->ant == ANT_A)
1150 return RS_COLUMN_LEGACY_ANT_A;
1151
1152 if (rate->ant == ANT_B)
1153 return RS_COLUMN_LEGACY_ANT_B;
1154
1155 goto err;
1156 }
1157
1158 if (is_siso(rate)) {
1159 if (rate->ant == ANT_A || rate->stbc || rate->bfer)
1160 return rate->sgi ? RS_COLUMN_SISO_ANT_A_SGI :
1161 RS_COLUMN_SISO_ANT_A;
1162
1163 if (rate->ant == ANT_B)
1164 return rate->sgi ? RS_COLUMN_SISO_ANT_B_SGI :
1165 RS_COLUMN_SISO_ANT_B;
1166
1167 goto err;
1168 }
1169
1170 if (is_mimo(rate))
1171 return rate->sgi ? RS_COLUMN_MIMO2_SGI : RS_COLUMN_MIMO2;
1172
1173err:
1174 return RS_COLUMN_INVALID;
1175}
1176
Eyal Shapira1ebc8f22014-09-14 15:23:23 +03001177static u8 rs_get_tid(struct ieee80211_hdr *hdr)
1178{
1179 u8 tid = IWL_MAX_TID_COUNT;
1180
1181 if (ieee80211_is_data_qos(hdr->frame_control)) {
1182 u8 *qc = ieee80211_get_qos_ctl(hdr);
1183 tid = qc[0] & 0xf;
1184 }
1185
1186 if (unlikely(tid > IWL_MAX_TID_COUNT))
1187 tid = IWL_MAX_TID_COUNT;
1188
1189 return tid;
1190}
1191
1192void iwl_mvm_rs_tx_status(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
Matti Gottliebff7a68d2016-05-17 10:50:08 +03001193 int tid, struct ieee80211_tx_info *info, bool ndp)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001194{
1195 int legacy_success;
1196 int retries;
Eyal Shapira929e6ed2015-01-30 13:40:02 +02001197 int i;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001198 struct iwl_lq_cmd *table;
Eyal Shapira929e6ed2015-01-30 13:40:02 +02001199 u32 lq_hwrate;
1200 struct rs_rate lq_rate, tx_resp_rate;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001201 struct iwl_scale_tbl_info *curr_tbl, *other_tbl, *tmp_tbl;
Gregory Greenmanea42d1c2017-03-06 11:15:41 +02001202 u32 tlc_info = (uintptr_t)info->status.status_driver_data[0];
1203 u8 reduced_txp = tlc_info & RS_DRV_DATA_TXP_MSK;
1204 u8 lq_color = RS_DRV_DATA_LQ_COLOR_GET(tlc_info);
Eyal Shapira929e6ed2015-01-30 13:40:02 +02001205 u32 tx_resp_hwrate = (uintptr_t)info->status.status_driver_data[1];
Eyal Shapira1ebc8f22014-09-14 15:23:23 +03001206 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Gregory Greenmanecaf71d2017-11-01 07:16:29 +02001207 struct iwl_lq_sta *lq_sta = &mvmsta->lq_sta.rs_drv;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001208
Johannes Berg8ca151b2013-01-24 14:25:36 +01001209 /* Treat uninitialized rate scaling data same as non-existing. */
1210 if (!lq_sta) {
1211 IWL_DEBUG_RATE(mvm, "Station rate scaling not created yet.\n");
1212 return;
Eliad Pellerae969af2014-06-11 15:51:33 +03001213 } else if (!lq_sta->pers.drv) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001214 IWL_DEBUG_RATE(mvm, "Rate scaling not initialized yet.\n");
1215 return;
1216 }
1217
Johannes Berg8ca151b2013-01-24 14:25:36 +01001218 /* This packet was aggregated but doesn't carry status info */
1219 if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
1220 !(info->flags & IEEE80211_TX_STAT_AMPDU))
1221 return;
1222
Eyal Shapiraad82d8a2015-03-08 19:43:41 +02001223 rs_rate_from_ucode_rate(tx_resp_hwrate, info->band, &tx_resp_rate);
1224
1225#ifdef CONFIG_MAC80211_DEBUGFS
1226 /* Disable last tx check if we are debugging with fixed rate but
1227 * update tx stats */
1228 if (lq_sta->pers.dbg_fixed_rate) {
1229 int index = tx_resp_rate.index;
1230 enum rs_column column;
1231 int attempts, success;
1232
1233 column = rs_get_column_from_rate(&tx_resp_rate);
1234 if (WARN_ONCE(column == RS_COLUMN_INVALID,
1235 "Can't map rate 0x%x to column",
1236 tx_resp_hwrate))
1237 return;
1238
1239 if (info->flags & IEEE80211_TX_STAT_AMPDU) {
1240 attempts = info->status.ampdu_len;
1241 success = info->status.ampdu_ack_len;
1242 } else {
1243 attempts = info->status.rates[0].count;
1244 success = !!(info->flags & IEEE80211_TX_STAT_ACK);
1245 }
1246
1247 lq_sta->pers.tx_stats[column][index].total += attempts;
1248 lq_sta->pers.tx_stats[column][index].success += success;
1249
1250 IWL_DEBUG_RATE(mvm, "Fixed rate 0x%x success %d attempts %d\n",
1251 tx_resp_hwrate, success, attempts);
1252 return;
1253 }
1254#endif
1255
Eyal Shapira87d5e412014-04-06 18:13:22 +03001256 if (time_after(jiffies,
Eyal Shapira834437d2014-12-17 15:38:58 +02001257 (unsigned long)(lq_sta->last_tx +
1258 (IWL_MVM_RS_IDLE_TIMEOUT * HZ)))) {
Eyal Shapira87d5e412014-04-06 18:13:22 +03001259 IWL_DEBUG_RATE(mvm, "Tx idle for too long. reinit rs\n");
Eyal Shapira1ebc8f22014-09-14 15:23:23 +03001260 iwl_mvm_rs_rate_init(mvm, sta, info->band, false);
Eyal Shapira87d5e412014-04-06 18:13:22 +03001261 return;
1262 }
1263 lq_sta->last_tx = jiffies;
1264
Eyal Shapira929e6ed2015-01-30 13:40:02 +02001265 /* Ignore this Tx frame response if its initial rate doesn't match
1266 * that of latest Link Quality command. There may be stragglers
1267 * from a previous Link Quality command, but we're no longer interested
1268 * in those; they're either from the "active" mode while we're trying
1269 * to check "search" mode, or a prior "search" mode after we've moved
1270 * to a new "search" mode (which might become the new "active" mode).
1271 */
1272 table = &lq_sta->lq;
1273 lq_hwrate = le32_to_cpu(table->rs_table[0]);
1274 rs_rate_from_ucode_rate(lq_hwrate, info->band, &lq_rate);
Eyal Shapira929e6ed2015-01-30 13:40:02 +02001275
Johannes Berg8ca151b2013-01-24 14:25:36 +01001276 /* Here we actually compare this rate to the latest LQ command */
Gregory Greenmanea42d1c2017-03-06 11:15:41 +02001277 if (lq_color != LQ_FLAG_COLOR_GET(table->flags)) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001278 IWL_DEBUG_RATE(mvm,
Gregory Greenmanea42d1c2017-03-06 11:15:41 +02001279 "tx resp color 0x%x does not match 0x%x\n",
1280 lq_color, LQ_FLAG_COLOR_GET(table->flags));
Eyal Shapira929e6ed2015-01-30 13:40:02 +02001281
Johannes Berg8ca151b2013-01-24 14:25:36 +01001282 /*
1283 * Since rates mis-match, the last LQ command may have failed.
1284 * After IWL_MISSED_RATE_MAX mis-matches, resync the uCode with
1285 * ... driver.
1286 */
1287 lq_sta->missed_rate_counter++;
Eyal Shapira834437d2014-12-17 15:38:58 +02001288 if (lq_sta->missed_rate_counter > IWL_MVM_RS_MISSED_RATE_MAX) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001289 lq_sta->missed_rate_counter = 0;
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001290 IWL_DEBUG_RATE(mvm,
1291 "Too many rates mismatch. Send sync LQ. rs_state %d\n",
1292 lq_sta->rs_state);
Eyal Shapira9e680942013-11-09 00:16:16 +02001293 iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, false);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001294 }
1295 /* Regardless, ignore this status info for outdated rate */
1296 return;
1297 } else
1298 /* Rate did match, so reset the missed_rate_counter */
1299 lq_sta->missed_rate_counter = 0;
1300
Eyal Shapira26a2cc02014-11-05 12:03:59 +02001301 if (!lq_sta->search_better_tbl) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001302 curr_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1303 other_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
Eyal Shapira26a2cc02014-11-05 12:03:59 +02001304 } else {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001305 curr_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
1306 other_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
Eyal Shapira26a2cc02014-11-05 12:03:59 +02001307 }
1308
Eyal Shapira929e6ed2015-01-30 13:40:02 +02001309 if (WARN_ON_ONCE(!rs_rate_column_match(&lq_rate, &curr_tbl->rate))) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001310 IWL_DEBUG_RATE(mvm,
1311 "Neither active nor search matches tx rate\n");
1312 tmp_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
Eyal Shapira5aa33552013-11-23 01:06:36 +02001313 rs_dump_rate(mvm, &tmp_tbl->rate, "ACTIVE");
Johannes Berg8ca151b2013-01-24 14:25:36 +01001314 tmp_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
Eyal Shapira5aa33552013-11-23 01:06:36 +02001315 rs_dump_rate(mvm, &tmp_tbl->rate, "SEARCH");
Eyal Shapira929e6ed2015-01-30 13:40:02 +02001316 rs_dump_rate(mvm, &lq_rate, "ACTUAL");
Eyal Shapira5aa33552013-11-23 01:06:36 +02001317
Johannes Berg8ca151b2013-01-24 14:25:36 +01001318 /*
1319 * no matching table found, let's by-pass the data collection
1320 * and continue to perform rate scale to find the rate table
1321 */
1322 rs_stay_in_table(lq_sta, true);
1323 goto done;
1324 }
1325
1326 /*
1327 * Updating the frame history depends on whether packets were
1328 * aggregated.
1329 *
1330 * For aggregation, all packets were transmitted at the same rate, the
1331 * first index into rate scale table.
1332 */
1333 if (info->flags & IEEE80211_TX_STAT_AMPDU) {
Gregory Greenman87f55612017-07-06 05:27:55 +03001334 rs_collect_tpc_data(mvm, lq_sta, curr_tbl, tx_resp_rate.index,
Gregory Greenman69c7fda2015-12-29 11:26:35 +02001335 info->status.ampdu_len,
1336 info->status.ampdu_ack_len,
1337 reduced_txp);
1338
1339 /* ampdu_ack_len = 0 marks no BA was received. For TLC, treat
1340 * it as a single frame loss as we don't want the success ratio
1341 * to dip too quickly because a BA wasn't received.
1342 * For TPC, there's no need for this optimisation since we want
1343 * to recover very quickly from a bad power reduction and,
1344 * therefore we'd like the success ratio to get an immediate hit
1345 * when failing to get a BA, so we'd switch back to a lower or
1346 * zero power reduction. When FW transmits agg with a rate
1347 * different from the initial rate, it will not use reduced txp
1348 * and will send BA notification twice (one empty with reduced
1349 * txp equal to the value from LQ and one with reduced txp 0).
1350 * We need to update counters for each txp level accordingly.
Eyal Shapirad6bda662014-11-12 13:30:20 +02001351 */
1352 if (info->status.ampdu_ack_len == 0)
1353 info->status.ampdu_len = 1;
1354
Kalle Valo5307eca2017-08-24 14:29:12 +03001355 rs_collect_tlc_data(mvm, mvmsta, tid, curr_tbl, tx_resp_rate.index,
Gregory Greenman69c7fda2015-12-29 11:26:35 +02001356 info->status.ampdu_len,
1357 info->status.ampdu_ack_len);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001358
1359 /* Update success/fail counts if not searching for new mode */
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001360 if (lq_sta->rs_state == RS_STATE_STAY_IN_COLUMN) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001361 lq_sta->total_success += info->status.ampdu_ack_len;
1362 lq_sta->total_failed += (info->status.ampdu_len -
1363 info->status.ampdu_ack_len);
1364 }
1365 } else {
Liad Kaufman90cb1232015-03-29 11:39:34 +03001366 /* For legacy, update frame history with for each Tx retry. */
Johannes Berg8ca151b2013-01-24 14:25:36 +01001367 retries = info->status.rates[0].count - 1;
1368 /* HW doesn't send more than 15 retries */
1369 retries = min(retries, 15);
1370
1371 /* The last transmission may have been successful */
1372 legacy_success = !!(info->flags & IEEE80211_TX_STAT_ACK);
1373 /* Collect data for each rate used during failed TX attempts */
1374 for (i = 0; i <= retries; ++i) {
Eyal Shapira929e6ed2015-01-30 13:40:02 +02001375 lq_hwrate = le32_to_cpu(table->rs_table[i]);
1376 rs_rate_from_ucode_rate(lq_hwrate, info->band,
1377 &lq_rate);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001378 /*
1379 * Only collect stats if retried rate is in the same RS
1380 * table as active/search.
1381 */
Eyal Shapira929e6ed2015-01-30 13:40:02 +02001382 if (rs_rate_column_match(&lq_rate, &curr_tbl->rate))
Johannes Berg8ca151b2013-01-24 14:25:36 +01001383 tmp_tbl = curr_tbl;
Eyal Shapira929e6ed2015-01-30 13:40:02 +02001384 else if (rs_rate_column_match(&lq_rate,
1385 &other_tbl->rate))
Johannes Berg8ca151b2013-01-24 14:25:36 +01001386 tmp_tbl = other_tbl;
Eyal Shapira4107dbd2013-12-10 12:20:37 +02001387 else
Johannes Berg8ca151b2013-01-24 14:25:36 +01001388 continue;
Eyal Shapira4107dbd2013-12-10 12:20:37 +02001389
Gregory Greenman69c7fda2015-12-29 11:26:35 +02001390 rs_collect_tpc_data(mvm, lq_sta, tmp_tbl,
Gregory Greenman87f55612017-07-06 05:27:55 +03001391 tx_resp_rate.index, 1,
Gregory Greenman69c7fda2015-12-29 11:26:35 +02001392 i < retries ? 0 : legacy_success,
1393 reduced_txp);
Gregory Greenman482e4842017-08-15 12:27:01 +03001394 rs_collect_tlc_data(mvm, mvmsta, tid, tmp_tbl,
Gregory Greenman87f55612017-07-06 05:27:55 +03001395 tx_resp_rate.index, 1,
Gregory Greenman69c7fda2015-12-29 11:26:35 +02001396 i < retries ? 0 : legacy_success);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001397 }
1398
1399 /* Update success/fail counts if not searching for new mode */
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001400 if (lq_sta->rs_state == RS_STATE_STAY_IN_COLUMN) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001401 lq_sta->total_success += legacy_success;
1402 lq_sta->total_failed += retries + (1 - legacy_success);
1403 }
1404 }
1405 /* The last TX rate is cached in lq_sta; it's set in if/else above */
Eyal Shapira929e6ed2015-01-30 13:40:02 +02001406 lq_sta->last_rate_n_flags = lq_hwrate;
Eliad Peller2fd647f2014-03-13 17:21:36 +02001407 IWL_DEBUG_RATE(mvm, "reduced txpower: %d\n", reduced_txp);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001408done:
1409 /* See if there's a better rate or modulation mode to try. */
Emmanuel Grumbach8dfc2752014-09-21 13:29:48 +03001410 if (sta->supp_rates[info->band])
Matti Gottliebff7a68d2016-05-17 10:50:08 +03001411 rs_rate_scale_perform(mvm, sta, lq_sta, tid, ndp);
Eyal Shapira1ebc8f22014-09-14 15:23:23 +03001412}
1413
1414/*
1415 * mac80211 sends us Tx status
1416 */
Gregory Greenman9f66a392017-11-05 18:49:48 +02001417static void rs_drv_mac80211_tx_status(void *mvm_r,
1418 struct ieee80211_supported_band *sband,
1419 struct ieee80211_sta *sta, void *priv_sta,
1420 struct sk_buff *skb)
Eyal Shapira1ebc8f22014-09-14 15:23:23 +03001421{
1422 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Gregory Greenman9f66a392017-11-05 18:49:48 +02001423 struct iwl_op_mode *op_mode = mvm_r;
Eyal Shapira1ebc8f22014-09-14 15:23:23 +03001424 struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1425 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1426
Johannes Bergdcbc17a2015-03-10 21:24:34 +01001427 if (!iwl_mvm_sta_from_mac80211(sta)->vif)
1428 return;
1429
Eyal Shapira1ebc8f22014-09-14 15:23:23 +03001430 if (!ieee80211_is_data(hdr->frame_control) ||
1431 info->flags & IEEE80211_TX_CTL_NO_ACK)
1432 return;
1433
Matti Gottliebff7a68d2016-05-17 10:50:08 +03001434 iwl_mvm_rs_tx_status(mvm, sta, rs_get_tid(hdr), info,
1435 ieee80211_is_qos_nullfunc(hdr->frame_control));
Johannes Berg8ca151b2013-01-24 14:25:36 +01001436}
1437
1438/*
1439 * Begin a period of staying with a selected modulation mode.
1440 * Set "stay_in_tbl" flag to prevent any mode switches.
1441 * Set frame tx success limits according to legacy vs. high-throughput,
1442 * and reset overall (spanning all rates) tx success history statistics.
1443 * These control how long we stay using same modulation mode before
1444 * searching for a new mode.
1445 */
1446static void rs_set_stay_in_table(struct iwl_mvm *mvm, u8 is_legacy,
1447 struct iwl_lq_sta *lq_sta)
1448{
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001449 IWL_DEBUG_RATE(mvm, "Moving to RS_STATE_STAY_IN_COLUMN\n");
1450 lq_sta->rs_state = RS_STATE_STAY_IN_COLUMN;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001451 if (is_legacy) {
Eyal Shapira834437d2014-12-17 15:38:58 +02001452 lq_sta->table_count_limit = IWL_MVM_RS_LEGACY_TABLE_COUNT;
1453 lq_sta->max_failure_limit = IWL_MVM_RS_LEGACY_FAILURE_LIMIT;
1454 lq_sta->max_success_limit = IWL_MVM_RS_LEGACY_SUCCESS_LIMIT;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001455 } else {
Eyal Shapira834437d2014-12-17 15:38:58 +02001456 lq_sta->table_count_limit = IWL_MVM_RS_NON_LEGACY_TABLE_COUNT;
1457 lq_sta->max_failure_limit = IWL_MVM_RS_NON_LEGACY_FAILURE_LIMIT;
1458 lq_sta->max_success_limit = IWL_MVM_RS_NON_LEGACY_SUCCESS_LIMIT;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001459 }
1460 lq_sta->table_count = 0;
1461 lq_sta->total_failed = 0;
1462 lq_sta->total_success = 0;
1463 lq_sta->flush_timer = jiffies;
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001464 lq_sta->visited_columns = 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001465}
1466
Eyal Shapira6a028d92014-12-07 18:18:27 +02001467static inline int rs_get_max_rate_from_mask(unsigned long rate_mask)
1468{
1469 if (rate_mask)
1470 return find_last_bit(&rate_mask, BITS_PER_LONG);
1471 return IWL_RATE_INVALID;
1472}
1473
Eyal Shapira198266a32014-03-31 22:37:39 +03001474static int rs_get_max_allowed_rate(struct iwl_lq_sta *lq_sta,
1475 const struct rs_tx_column *column)
1476{
1477 switch (column->mode) {
1478 case RS_LEGACY:
1479 return lq_sta->max_legacy_rate_idx;
1480 case RS_SISO:
1481 return lq_sta->max_siso_rate_idx;
1482 case RS_MIMO2:
1483 return lq_sta->max_mimo2_rate_idx;
1484 default:
1485 WARN_ON_ONCE(1);
1486 }
1487
1488 return lq_sta->max_legacy_rate_idx;
1489}
1490
Johannes Berga34529e2014-01-20 22:57:21 +01001491static const u16 *rs_get_expected_tpt_table(struct iwl_lq_sta *lq_sta,
Eyal Shapira198266a32014-03-31 22:37:39 +03001492 const struct rs_tx_column *column,
1493 u32 bw)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001494{
1495 /* Used to choose among HT tables */
Johannes Berga34529e2014-01-20 22:57:21 +01001496 const u16 (*ht_tbl_pointer)[IWL_RATE_COUNT];
Johannes Berg8ca151b2013-01-24 14:25:36 +01001497
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001498 if (WARN_ON_ONCE(column->mode != RS_LEGACY &&
1499 column->mode != RS_SISO &&
1500 column->mode != RS_MIMO2))
1501 return expected_tpt_legacy;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001502
1503 /* Legacy rates have only one table */
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001504 if (column->mode == RS_LEGACY)
1505 return expected_tpt_legacy;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001506
Eyal Shapirad310e402013-08-11 18:43:47 +03001507 ht_tbl_pointer = expected_tpt_mimo2_20MHz;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001508 /* Choose among many HT tables depending on number of streams
Eyal Shapirad310e402013-08-11 18:43:47 +03001509 * (SISO/MIMO2), channel width (20/40/80), SGI, and aggregation
Johannes Berg8ca151b2013-01-24 14:25:36 +01001510 * status */
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001511 if (column->mode == RS_SISO) {
1512 switch (bw) {
Eyal Shapirad310e402013-08-11 18:43:47 +03001513 case RATE_MCS_CHAN_WIDTH_20:
1514 ht_tbl_pointer = expected_tpt_siso_20MHz;
1515 break;
1516 case RATE_MCS_CHAN_WIDTH_40:
1517 ht_tbl_pointer = expected_tpt_siso_40MHz;
1518 break;
1519 case RATE_MCS_CHAN_WIDTH_80:
1520 ht_tbl_pointer = expected_tpt_siso_80MHz;
1521 break;
Gregory Greenman855f4922016-05-25 18:31:39 +03001522 case RATE_MCS_CHAN_WIDTH_160:
1523 ht_tbl_pointer = expected_tpt_siso_160MHz;
1524 break;
Eyal Shapirad310e402013-08-11 18:43:47 +03001525 default:
1526 WARN_ON_ONCE(1);
1527 }
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001528 } else if (column->mode == RS_MIMO2) {
1529 switch (bw) {
Eyal Shapirad310e402013-08-11 18:43:47 +03001530 case RATE_MCS_CHAN_WIDTH_20:
1531 ht_tbl_pointer = expected_tpt_mimo2_20MHz;
1532 break;
1533 case RATE_MCS_CHAN_WIDTH_40:
1534 ht_tbl_pointer = expected_tpt_mimo2_40MHz;
1535 break;
1536 case RATE_MCS_CHAN_WIDTH_80:
1537 ht_tbl_pointer = expected_tpt_mimo2_80MHz;
1538 break;
Gregory Greenman855f4922016-05-25 18:31:39 +03001539 case RATE_MCS_CHAN_WIDTH_160:
1540 ht_tbl_pointer = expected_tpt_mimo2_160MHz;
1541 break;
Eyal Shapirad310e402013-08-11 18:43:47 +03001542 default:
1543 WARN_ON_ONCE(1);
1544 }
1545 } else {
1546 WARN_ON_ONCE(1);
Eyal Shapirad972ab32013-07-28 23:02:45 +00001547 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01001548
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001549 if (!column->sgi && !lq_sta->is_agg) /* Normal */
1550 return ht_tbl_pointer[0];
1551 else if (column->sgi && !lq_sta->is_agg) /* SGI */
1552 return ht_tbl_pointer[1];
1553 else if (!column->sgi && lq_sta->is_agg) /* AGG */
1554 return ht_tbl_pointer[2];
Johannes Berg8ca151b2013-01-24 14:25:36 +01001555 else /* AGG+SGI */
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001556 return ht_tbl_pointer[3];
1557}
1558
1559static void rs_set_expected_tpt_table(struct iwl_lq_sta *lq_sta,
1560 struct iwl_scale_tbl_info *tbl)
1561{
1562 struct rs_rate *rate = &tbl->rate;
1563 const struct rs_tx_column *column = &rs_tx_columns[tbl->column];
1564
1565 tbl->expected_tpt = rs_get_expected_tpt_table(lq_sta, column, rate->bw);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001566}
1567
Johannes Berg8ca151b2013-01-24 14:25:36 +01001568static s32 rs_get_best_rate(struct iwl_mvm *mvm,
1569 struct iwl_lq_sta *lq_sta,
1570 struct iwl_scale_tbl_info *tbl, /* "search" */
Eyal Shapira80763512014-04-09 04:31:44 +03001571 unsigned long rate_mask, s8 index)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001572{
Johannes Berg8ca151b2013-01-24 14:25:36 +01001573 struct iwl_scale_tbl_info *active_tbl =
1574 &(lq_sta->lq_info[lq_sta->active_tbl]);
Eyal Shapira80763512014-04-09 04:31:44 +03001575 s32 success_ratio = active_tbl->win[index].success_ratio;
1576 u16 expected_current_tpt = active_tbl->expected_tpt[index];
Johannes Berga34529e2014-01-20 22:57:21 +01001577 const u16 *tpt_tbl = tbl->expected_tpt;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001578 u16 high_low;
Eyal Shapira80763512014-04-09 04:31:44 +03001579 u32 target_tpt;
1580 int rate_idx;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001581
Eyal Shapiraed21a382015-09-21 20:03:22 +03001582 if (success_ratio >= RS_PERCENT(IWL_MVM_RS_SR_NO_DECREASE)) {
Eyal Shapira80763512014-04-09 04:31:44 +03001583 target_tpt = 100 * expected_current_tpt;
1584 IWL_DEBUG_RATE(mvm,
1585 "SR %d high. Find rate exceeding EXPECTED_CURRENT %d\n",
1586 success_ratio, target_tpt);
1587 } else {
1588 target_tpt = lq_sta->last_tpt;
1589 IWL_DEBUG_RATE(mvm,
Eyal Shapiraed21a382015-09-21 20:03:22 +03001590 "SR %d not that good. Find rate exceeding ACTUAL_TPT %d\n",
Eyal Shapira80763512014-04-09 04:31:44 +03001591 success_ratio, target_tpt);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001592 }
1593
Eyal Shapira80763512014-04-09 04:31:44 +03001594 rate_idx = find_first_bit(&rate_mask, BITS_PER_LONG);
1595
1596 while (rate_idx != IWL_RATE_INVALID) {
1597 if (target_tpt < (100 * tpt_tbl[rate_idx]))
1598 break;
1599
1600 high_low = rs_get_adjacent_rate(mvm, rate_idx, rate_mask,
1601 tbl->rate.type);
1602
1603 rate_idx = (high_low >> 8) & 0xff;
1604 }
1605
1606 IWL_DEBUG_RATE(mvm, "Best rate found %d target_tp %d expected_new %d\n",
1607 rate_idx, target_tpt,
1608 rate_idx != IWL_RATE_INVALID ?
1609 100 * tpt_tbl[rate_idx] : IWL_INVALID_VALUE);
1610
1611 return rate_idx;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001612}
1613
Eyal Shapira5aa33552013-11-23 01:06:36 +02001614static u32 rs_bw_from_sta_bw(struct ieee80211_sta *sta)
Eyal Shapirad310e402013-08-11 18:43:47 +03001615{
Gregory Greenman855f4922016-05-25 18:31:39 +03001616 switch (sta->bandwidth) {
1617 case IEEE80211_STA_RX_BW_160:
1618 return RATE_MCS_CHAN_WIDTH_160;
1619 case IEEE80211_STA_RX_BW_80:
Eyal Shapira5aa33552013-11-23 01:06:36 +02001620 return RATE_MCS_CHAN_WIDTH_80;
Gregory Greenman855f4922016-05-25 18:31:39 +03001621 case IEEE80211_STA_RX_BW_40:
Eyal Shapira5aa33552013-11-23 01:06:36 +02001622 return RATE_MCS_CHAN_WIDTH_40;
Gregory Greenman855f4922016-05-25 18:31:39 +03001623 case IEEE80211_STA_RX_BW_20:
1624 default:
1625 return RATE_MCS_CHAN_WIDTH_20;
1626 }
Eyal Shapirad310e402013-08-11 18:43:47 +03001627}
1628
Johannes Berg8ca151b2013-01-24 14:25:36 +01001629/*
Johannes Berg8ca151b2013-01-24 14:25:36 +01001630 * Check whether we should continue using same modulation mode, or
1631 * begin search for a new mode, based on:
1632 * 1) # tx successes or failures while using this mode
1633 * 2) # times calling this function
1634 * 3) elapsed time in this mode (not used, for now)
1635 */
1636static void rs_stay_in_table(struct iwl_lq_sta *lq_sta, bool force_search)
1637{
1638 struct iwl_scale_tbl_info *tbl;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001639 int active_tbl;
1640 int flush_interval_passed = 0;
1641 struct iwl_mvm *mvm;
1642
Eliad Pellerae969af2014-06-11 15:51:33 +03001643 mvm = lq_sta->pers.drv;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001644 active_tbl = lq_sta->active_tbl;
1645
1646 tbl = &(lq_sta->lq_info[active_tbl]);
1647
1648 /* If we've been disallowing search, see if we should now allow it */
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001649 if (lq_sta->rs_state == RS_STATE_STAY_IN_COLUMN) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001650 /* Elapsed time using current modulation mode */
1651 if (lq_sta->flush_timer)
1652 flush_interval_passed =
1653 time_after(jiffies,
1654 (unsigned long)(lq_sta->flush_timer +
Eyal Shapira834437d2014-12-17 15:38:58 +02001655 (IWL_MVM_RS_STAY_IN_COLUMN_TIMEOUT * HZ)));
Johannes Berg8ca151b2013-01-24 14:25:36 +01001656
1657 /*
1658 * Check if we should allow search for new modulation mode.
1659 * If many frames have failed or succeeded, or we've used
1660 * this same modulation for a long time, allow search, and
1661 * reset history stats that keep track of whether we should
1662 * allow a new search. Also (below) reset all bitmaps and
1663 * stats in active history.
1664 */
1665 if (force_search ||
1666 (lq_sta->total_failed > lq_sta->max_failure_limit) ||
1667 (lq_sta->total_success > lq_sta->max_success_limit) ||
1668 ((!lq_sta->search_better_tbl) &&
1669 (lq_sta->flush_timer) && (flush_interval_passed))) {
1670 IWL_DEBUG_RATE(mvm,
1671 "LQ: stay is expired %d %d %d\n",
1672 lq_sta->total_failed,
1673 lq_sta->total_success,
1674 flush_interval_passed);
1675
1676 /* Allow search for new mode */
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001677 lq_sta->rs_state = RS_STATE_SEARCH_CYCLE_STARTED;
1678 IWL_DEBUG_RATE(mvm,
1679 "Moving to RS_STATE_SEARCH_CYCLE_STARTED\n");
Johannes Berg8ca151b2013-01-24 14:25:36 +01001680 lq_sta->total_failed = 0;
1681 lq_sta->total_success = 0;
1682 lq_sta->flush_timer = 0;
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001683 /* mark the current column as visited */
1684 lq_sta->visited_columns = BIT(tbl->column);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001685 /*
1686 * Else if we've used this modulation mode enough repetitions
1687 * (regardless of elapsed time or success/failure), reset
1688 * history bitmaps and rate-specific stats for all rates in
1689 * active table.
1690 */
1691 } else {
1692 lq_sta->table_count++;
1693 if (lq_sta->table_count >=
1694 lq_sta->table_count_limit) {
1695 lq_sta->table_count = 0;
1696
1697 IWL_DEBUG_RATE(mvm,
1698 "LQ: stay in table clear win\n");
Eyal Shapirab804eeb2014-04-06 04:27:06 +03001699 rs_rate_scale_clear_tbl_windows(mvm, tbl);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001700 }
1701 }
1702
1703 /* If transitioning to allow "search", reset all history
1704 * bitmaps and stats in active table (this will become the new
1705 * "search" table). */
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001706 if (lq_sta->rs_state == RS_STATE_SEARCH_CYCLE_STARTED) {
Eyal Shapirab804eeb2014-04-06 04:27:06 +03001707 rs_rate_scale_clear_tbl_windows(mvm, tbl);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001708 }
1709 }
1710}
1711
Emmanuel Grumbach04e3a5d2015-10-28 09:47:41 +02001712static void rs_set_amsdu_len(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
1713 struct iwl_scale_tbl_info *tbl,
1714 enum rs_action scale_action)
1715{
Luca Coelho7ccb4982017-08-01 09:01:34 +03001716 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbach04e3a5d2015-10-28 09:47:41 +02001717
1718 if ((!is_vht(&tbl->rate) && !is_ht(&tbl->rate)) ||
1719 tbl->rate.index < IWL_RATE_MCS_5_INDEX ||
1720 scale_action == RS_ACTION_DOWNSCALE)
Luca Coelho7ccb4982017-08-01 09:01:34 +03001721 mvmsta->tlc_amsdu = false;
Emmanuel Grumbach04e3a5d2015-10-28 09:47:41 +02001722 else
Luca Coelho7ccb4982017-08-01 09:01:34 +03001723 mvmsta->tlc_amsdu = true;
Emmanuel Grumbach04e3a5d2015-10-28 09:47:41 +02001724}
1725
Johannes Berg8ca151b2013-01-24 14:25:36 +01001726/*
1727 * setup rate table in uCode
1728 */
1729static void rs_update_rate_tbl(struct iwl_mvm *mvm,
Emmanuel Grumbach9145d152013-07-18 08:45:41 +03001730 struct ieee80211_sta *sta,
Johannes Berg8ca151b2013-01-24 14:25:36 +01001731 struct iwl_lq_sta *lq_sta,
Eyal Shapira7b9d74e2015-03-29 18:38:07 +03001732 struct iwl_scale_tbl_info *tbl)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001733{
Eyal Shapira7b9d74e2015-03-29 18:38:07 +03001734 rs_fill_lq_cmd(mvm, sta, lq_sta, &tbl->rate);
Eyal Shapira9e680942013-11-09 00:16:16 +02001735 iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, false);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001736}
1737
Eyal Shapira1191f642015-09-19 23:48:45 +03001738static bool rs_tweak_rate_tbl(struct iwl_mvm *mvm,
1739 struct ieee80211_sta *sta,
1740 struct iwl_lq_sta *lq_sta,
1741 struct iwl_scale_tbl_info *tbl,
1742 enum rs_action scale_action)
1743{
1744 if (sta->bandwidth != IEEE80211_STA_RX_BW_80)
1745 return false;
1746
1747 if (!is_vht_siso(&tbl->rate))
1748 return false;
1749
1750 if ((tbl->rate.bw == RATE_MCS_CHAN_WIDTH_80) &&
1751 (tbl->rate.index == IWL_RATE_MCS_0_INDEX) &&
1752 (scale_action == RS_ACTION_DOWNSCALE)) {
1753 tbl->rate.bw = RATE_MCS_CHAN_WIDTH_20;
1754 tbl->rate.index = IWL_RATE_MCS_4_INDEX;
1755 IWL_DEBUG_RATE(mvm, "Switch 80Mhz SISO MCS0 -> 20Mhz MCS4\n");
1756 goto tweaked;
1757 }
1758
1759 /* Go back to 80Mhz MCS1 only if we've established that 20Mhz MCS5 is
1760 * sustainable, i.e. we're past the test window. We can't go back
1761 * if MCS5 is just tested as this will happen always after switching
1762 * to 20Mhz MCS4 because the rate stats are cleared.
1763 */
1764 if ((tbl->rate.bw == RATE_MCS_CHAN_WIDTH_20) &&
1765 (((tbl->rate.index == IWL_RATE_MCS_5_INDEX) &&
1766 (scale_action == RS_ACTION_STAY)) ||
1767 ((tbl->rate.index > IWL_RATE_MCS_5_INDEX) &&
1768 (scale_action == RS_ACTION_UPSCALE)))) {
1769 tbl->rate.bw = RATE_MCS_CHAN_WIDTH_80;
1770 tbl->rate.index = IWL_RATE_MCS_1_INDEX;
1771 IWL_DEBUG_RATE(mvm, "Switch 20Mhz SISO MCS5 -> 80Mhz MCS1\n");
1772 goto tweaked;
1773 }
1774
1775 return false;
1776
1777tweaked:
1778 rs_set_expected_tpt_table(lq_sta, tbl);
1779 rs_rate_scale_clear_tbl_windows(mvm, tbl);
1780 return true;
1781}
1782
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001783static enum rs_column rs_get_next_column(struct iwl_mvm *mvm,
1784 struct iwl_lq_sta *lq_sta,
1785 struct ieee80211_sta *sta,
1786 struct iwl_scale_tbl_info *tbl)
1787{
Eyal Shapira198266a32014-03-31 22:37:39 +03001788 int i, j, max_rate;
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001789 enum rs_column next_col_id;
1790 const struct rs_tx_column *curr_col = &rs_tx_columns[tbl->column];
1791 const struct rs_tx_column *next_col;
1792 allow_column_func_t allow_func;
Moshe Harela0544272014-12-08 21:13:14 +02001793 u8 valid_ants = iwl_mvm_get_valid_tx_ant(mvm);
Johannes Berga34529e2014-01-20 22:57:21 +01001794 const u16 *expected_tpt_tbl;
Eyal Shapira198266a32014-03-31 22:37:39 +03001795 u16 tpt, max_expected_tpt;
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001796
1797 for (i = 0; i < MAX_NEXT_COLUMNS; i++) {
1798 next_col_id = curr_col->next_columns[i];
1799
1800 if (next_col_id == RS_COLUMN_INVALID)
1801 continue;
1802
1803 if (lq_sta->visited_columns & BIT(next_col_id)) {
1804 IWL_DEBUG_RATE(mvm, "Skip already visited column %d\n",
1805 next_col_id);
1806 continue;
1807 }
1808
1809 next_col = &rs_tx_columns[next_col_id];
1810
1811 if (!rs_is_valid_ant(valid_ants, next_col->ant)) {
1812 IWL_DEBUG_RATE(mvm,
1813 "Skip column %d as ANT config isn't supported by chip. valid_ants 0x%x column ant 0x%x\n",
1814 next_col_id, valid_ants, next_col->ant);
1815 continue;
1816 }
1817
1818 for (j = 0; j < MAX_COLUMN_CHECKS; j++) {
1819 allow_func = next_col->checks[j];
Eyal Shapirabb8f44c2015-05-27 22:00:03 +03001820 if (allow_func && !allow_func(mvm, sta, &tbl->rate,
1821 next_col))
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001822 break;
1823 }
1824
1825 if (j != MAX_COLUMN_CHECKS) {
1826 IWL_DEBUG_RATE(mvm,
1827 "Skip column %d: not allowed (check %d failed)\n",
1828 next_col_id, j);
1829
1830 continue;
1831 }
1832
1833 tpt = lq_sta->last_tpt / 100;
1834 expected_tpt_tbl = rs_get_expected_tpt_table(lq_sta, next_col,
Eyal Shapira049e0c32014-05-01 01:28:48 +03001835 rs_bw_from_sta_bw(sta));
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001836 if (WARN_ON_ONCE(!expected_tpt_tbl))
1837 continue;
1838
Eyal Shapira198266a32014-03-31 22:37:39 +03001839 max_rate = rs_get_max_allowed_rate(lq_sta, next_col);
Eyal Shapira6a028d92014-12-07 18:18:27 +02001840 if (max_rate == IWL_RATE_INVALID) {
1841 IWL_DEBUG_RATE(mvm,
1842 "Skip column %d: no rate is allowed in this column\n",
1843 next_col_id);
Eyal Shapira198266a32014-03-31 22:37:39 +03001844 continue;
Eyal Shapira6a028d92014-12-07 18:18:27 +02001845 }
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001846
Eyal Shapira198266a32014-03-31 22:37:39 +03001847 max_expected_tpt = expected_tpt_tbl[max_rate];
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001848 if (tpt >= max_expected_tpt) {
1849 IWL_DEBUG_RATE(mvm,
1850 "Skip column %d: can't beat current TPT. Max expected %d current %d\n",
1851 next_col_id, max_expected_tpt, tpt);
1852 continue;
1853 }
1854
Eyal Shapira198266a32014-03-31 22:37:39 +03001855 IWL_DEBUG_RATE(mvm,
1856 "Found potential column %d. Max expected %d current %d\n",
1857 next_col_id, max_expected_tpt, tpt);
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001858 break;
1859 }
1860
1861 if (i == MAX_NEXT_COLUMNS)
1862 return RS_COLUMN_INVALID;
1863
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001864 return next_col_id;
1865}
1866
1867static int rs_switch_to_column(struct iwl_mvm *mvm,
1868 struct iwl_lq_sta *lq_sta,
1869 struct ieee80211_sta *sta,
1870 enum rs_column col_id)
1871{
1872 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1873 struct iwl_scale_tbl_info *search_tbl =
1874 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1875 struct rs_rate *rate = &search_tbl->rate;
1876 const struct rs_tx_column *column = &rs_tx_columns[col_id];
1877 const struct rs_tx_column *curr_column = &rs_tx_columns[tbl->column];
Eyal Shapira80763512014-04-09 04:31:44 +03001878 unsigned long rate_mask = 0;
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001879 u32 rate_idx = 0;
1880
Emmanuel Grumbach992172e2017-10-19 21:36:04 +03001881 memcpy(search_tbl, tbl, offsetof(struct iwl_scale_tbl_info, win));
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001882
1883 rate->sgi = column->sgi;
1884 rate->ant = column->ant;
1885
1886 if (column->mode == RS_LEGACY) {
Johannes Berg57fbcce2016-04-12 15:56:15 +02001887 if (lq_sta->band == NL80211_BAND_5GHZ)
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001888 rate->type = LQ_LEGACY_A;
1889 else
1890 rate->type = LQ_LEGACY_G;
1891
Eyal Shapirad8bafea2014-10-23 13:58:38 +03001892 rate->bw = RATE_MCS_CHAN_WIDTH_20;
1893 rate->ldpc = false;
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001894 rate_mask = lq_sta->active_legacy_rate;
1895 } else if (column->mode == RS_SISO) {
1896 rate->type = lq_sta->is_vht ? LQ_VHT_SISO : LQ_HT_SISO;
1897 rate_mask = lq_sta->active_siso_rate;
1898 } else if (column->mode == RS_MIMO2) {
1899 rate->type = lq_sta->is_vht ? LQ_VHT_MIMO2 : LQ_HT_MIMO2;
1900 rate_mask = lq_sta->active_mimo2_rate;
1901 } else {
Dan Carpenter7281b162015-11-21 13:33:38 +03001902 WARN_ONCE(1, "Bad column mode");
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001903 }
1904
Eyal Shapirad8bafea2014-10-23 13:58:38 +03001905 if (column->mode != RS_LEGACY) {
1906 rate->bw = rs_bw_from_sta_bw(sta);
1907 rate->ldpc = lq_sta->ldpc;
1908 }
1909
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001910 search_tbl->column = col_id;
1911 rs_set_expected_tpt_table(lq_sta, search_tbl);
1912
Eyal Shapira75d3e2d2014-01-07 18:19:35 +02001913 lq_sta->visited_columns |= BIT(col_id);
1914
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001915 /* Get the best matching rate if we're changing modes. e.g.
1916 * SISO->MIMO, LEGACY->SISO, MIMO->SISO
1917 */
1918 if (curr_column->mode != column->mode) {
1919 rate_idx = rs_get_best_rate(mvm, lq_sta, search_tbl,
1920 rate_mask, rate->index);
1921
1922 if ((rate_idx == IWL_RATE_INVALID) ||
1923 !(BIT(rate_idx) & rate_mask)) {
1924 IWL_DEBUG_RATE(mvm,
1925 "can not switch with index %d"
Eyal Shapira80763512014-04-09 04:31:44 +03001926 " rate mask %lx\n",
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001927 rate_idx, rate_mask);
1928
1929 goto err;
1930 }
1931
1932 rate->index = rate_idx;
1933 }
1934
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001935 IWL_DEBUG_RATE(mvm, "Switched to column %d: Index %d\n",
1936 col_id, rate->index);
1937
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001938 return 0;
1939
1940err:
1941 rate->type = LQ_NONE;
1942 return -1;
1943}
1944
Eyal Shapira4e4b8152013-12-11 00:55:36 +02001945static enum rs_action rs_get_rate_action(struct iwl_mvm *mvm,
1946 struct iwl_scale_tbl_info *tbl,
1947 s32 sr, int low, int high,
1948 int current_tpt,
1949 int low_tpt, int high_tpt)
1950{
1951 enum rs_action action = RS_ACTION_STAY;
1952
Eyal Shapira834437d2014-12-17 15:38:58 +02001953 if ((sr <= RS_PERCENT(IWL_MVM_RS_SR_FORCE_DECREASE)) ||
1954 (current_tpt == 0)) {
Eyal Shapira4e4b8152013-12-11 00:55:36 +02001955 IWL_DEBUG_RATE(mvm,
Eyal Shapirae53839e2014-04-06 02:42:18 +03001956 "Decrease rate because of low SR\n");
1957 return RS_ACTION_DOWNSCALE;
1958 }
1959
1960 if ((low_tpt == IWL_INVALID_VALUE) &&
1961 (high_tpt == IWL_INVALID_VALUE) &&
1962 (high != IWL_RATE_INVALID)) {
1963 IWL_DEBUG_RATE(mvm,
1964 "No data about high/low rates. Increase rate\n");
1965 return RS_ACTION_UPSCALE;
1966 }
1967
1968 if ((high_tpt == IWL_INVALID_VALUE) &&
1969 (high != IWL_RATE_INVALID) &&
1970 (low_tpt != IWL_INVALID_VALUE) &&
1971 (low_tpt < current_tpt)) {
1972 IWL_DEBUG_RATE(mvm,
1973 "No data about high rate and low rate is worse. Increase rate\n");
1974 return RS_ACTION_UPSCALE;
1975 }
1976
1977 if ((high_tpt != IWL_INVALID_VALUE) &&
1978 (high_tpt > current_tpt)) {
1979 IWL_DEBUG_RATE(mvm,
1980 "Higher rate is better. Increate rate\n");
1981 return RS_ACTION_UPSCALE;
1982 }
1983
1984 if ((low_tpt != IWL_INVALID_VALUE) &&
1985 (high_tpt != IWL_INVALID_VALUE) &&
1986 (low_tpt < current_tpt) &&
1987 (high_tpt < current_tpt)) {
1988 IWL_DEBUG_RATE(mvm,
1989 "Both high and low are worse. Maintain rate\n");
1990 return RS_ACTION_STAY;
1991 }
1992
1993 if ((low_tpt != IWL_INVALID_VALUE) &&
1994 (low_tpt > current_tpt)) {
1995 IWL_DEBUG_RATE(mvm,
1996 "Lower rate is better\n");
Eyal Shapira4e4b8152013-12-11 00:55:36 +02001997 action = RS_ACTION_DOWNSCALE;
Eyal Shapirae53839e2014-04-06 02:42:18 +03001998 goto out;
1999 }
2000
2001 if ((low_tpt == IWL_INVALID_VALUE) &&
2002 (low != IWL_RATE_INVALID)) {
2003 IWL_DEBUG_RATE(mvm,
2004 "No data about lower rate\n");
2005 action = RS_ACTION_DOWNSCALE;
2006 goto out;
2007 }
2008
2009 IWL_DEBUG_RATE(mvm, "Maintain rate\n");
2010
2011out:
2012 if ((action == RS_ACTION_DOWNSCALE) && (low != IWL_RATE_INVALID)) {
Eyal Shapira834437d2014-12-17 15:38:58 +02002013 if (sr >= RS_PERCENT(IWL_MVM_RS_SR_NO_DECREASE)) {
Eyal Shapira4e4b8152013-12-11 00:55:36 +02002014 IWL_DEBUG_RATE(mvm,
Eyal Shapirae53839e2014-04-06 02:42:18 +03002015 "SR is above NO DECREASE. Avoid downscale\n");
Eyal Shapira4e4b8152013-12-11 00:55:36 +02002016 action = RS_ACTION_STAY;
Eyal Shapirae53839e2014-04-06 02:42:18 +03002017 } else if (current_tpt > (100 * tbl->expected_tpt[low])) {
2018 IWL_DEBUG_RATE(mvm,
2019 "Current TPT is higher than max expected in low rate. Avoid downscale\n");
2020 action = RS_ACTION_STAY;
2021 } else {
2022 IWL_DEBUG_RATE(mvm, "Decrease rate\n");
Eyal Shapira4e4b8152013-12-11 00:55:36 +02002023 }
2024 }
2025
Eyal Shapira4e4b8152013-12-11 00:55:36 +02002026 return action;
2027}
Eyal Shapirab3b06a32013-11-24 21:30:13 +02002028
Eyal Shapirad8bafea2014-10-23 13:58:38 +03002029static bool rs_stbc_allow(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
2030 struct iwl_lq_sta *lq_sta)
2031{
Eyal Shapirad8bafea2014-10-23 13:58:38 +03002032 /* Our chip supports Tx STBC and the peer is an HT/VHT STA which
2033 * supports STBC of at least 1*SS
2034 */
Eyal Shapira3d44eeb2015-01-16 22:37:04 +02002035 if (!lq_sta->stbc_capable)
Eyal Shapirad8bafea2014-10-23 13:58:38 +03002036 return false;
2037
Eyal Shapirad8bafea2014-10-23 13:58:38 +03002038 if (!iwl_mvm_bt_coex_is_mimo_allowed(mvm, sta))
2039 return false;
2040
2041 return true;
2042}
2043
Eliad Peller2fd647f2014-03-13 17:21:36 +02002044static void rs_get_adjacent_txp(struct iwl_mvm *mvm, int index,
2045 int *weaker, int *stronger)
2046{
Eyal Shapira834437d2014-12-17 15:38:58 +02002047 *weaker = index + IWL_MVM_RS_TPC_TX_POWER_STEP;
Eliad Peller2fd647f2014-03-13 17:21:36 +02002048 if (*weaker > TPC_MAX_REDUCTION)
2049 *weaker = TPC_INVALID;
2050
Eyal Shapira834437d2014-12-17 15:38:58 +02002051 *stronger = index - IWL_MVM_RS_TPC_TX_POWER_STEP;
Eliad Peller2fd647f2014-03-13 17:21:36 +02002052 if (*stronger < 0)
2053 *stronger = TPC_INVALID;
2054}
2055
Eyal Shapira1e9551d2014-04-21 21:33:17 +03002056static bool rs_tpc_allowed(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
Johannes Berg57fbcce2016-04-12 15:56:15 +02002057 struct rs_rate *rate, enum nl80211_band band)
Eliad Peller2fd647f2014-03-13 17:21:36 +02002058{
2059 int index = rate->index;
Eyal Shapira1e9551d2014-04-21 21:33:17 +03002060 bool cam = (iwlmvm_mod_params.power_scheme == IWL_POWER_SCHEME_CAM);
2061 bool sta_ps_disabled = (vif->type == NL80211_IFTYPE_STATION &&
2062 !vif->bss_conf.ps);
Eliad Peller2fd647f2014-03-13 17:21:36 +02002063
Eyal Shapira1e9551d2014-04-21 21:33:17 +03002064 IWL_DEBUG_RATE(mvm, "cam: %d sta_ps_disabled %d\n",
2065 cam, sta_ps_disabled);
Eliad Peller2fd647f2014-03-13 17:21:36 +02002066 /*
2067 * allow tpc only if power management is enabled, or bt coex
2068 * activity grade allows it and we are on 2.4Ghz.
2069 */
Eyal Shapira1e9551d2014-04-21 21:33:17 +03002070 if ((cam || sta_ps_disabled) &&
Eliad Peller2fd647f2014-03-13 17:21:36 +02002071 !iwl_mvm_bt_coex_is_tpc_allowed(mvm, band))
2072 return false;
2073
2074 IWL_DEBUG_RATE(mvm, "check rate, table type: %d\n", rate->type);
2075 if (is_legacy(rate))
2076 return index == IWL_RATE_54M_INDEX;
2077 if (is_ht(rate))
2078 return index == IWL_RATE_MCS_7_INDEX;
2079 if (is_vht(rate))
2080 return index == IWL_RATE_MCS_7_INDEX ||
2081 index == IWL_RATE_MCS_8_INDEX ||
2082 index == IWL_RATE_MCS_9_INDEX;
2083
2084 WARN_ON_ONCE(1);
2085 return false;
2086}
2087
2088enum tpc_action {
2089 TPC_ACTION_STAY,
2090 TPC_ACTION_DECREASE,
2091 TPC_ACTION_INCREASE,
2092 TPC_ACTION_NO_RESTIRCTION,
2093};
2094
2095static enum tpc_action rs_get_tpc_action(struct iwl_mvm *mvm,
2096 s32 sr, int weak, int strong,
2097 int current_tpt,
2098 int weak_tpt, int strong_tpt)
2099{
2100 /* stay until we have valid tpt */
2101 if (current_tpt == IWL_INVALID_VALUE) {
2102 IWL_DEBUG_RATE(mvm, "no current tpt. stay.\n");
2103 return TPC_ACTION_STAY;
2104 }
2105
2106 /* Too many failures, increase txp */
Eyal Shapira834437d2014-12-17 15:38:58 +02002107 if (sr <= RS_PERCENT(IWL_MVM_RS_TPC_SR_FORCE_INCREASE) ||
2108 current_tpt == 0) {
Eliad Peller2fd647f2014-03-13 17:21:36 +02002109 IWL_DEBUG_RATE(mvm, "increase txp because of weak SR\n");
2110 return TPC_ACTION_NO_RESTIRCTION;
2111 }
2112
2113 /* try decreasing first if applicable */
Gregory Greenman6ad6c012016-01-27 00:10:15 +02002114 if (sr >= RS_PERCENT(IWL_MVM_RS_TPC_SR_NO_INCREASE) &&
2115 weak != TPC_INVALID) {
Eliad Peller2fd647f2014-03-13 17:21:36 +02002116 if (weak_tpt == IWL_INVALID_VALUE &&
2117 (strong_tpt == IWL_INVALID_VALUE ||
2118 current_tpt >= strong_tpt)) {
2119 IWL_DEBUG_RATE(mvm,
2120 "no weak txp measurement. decrease txp\n");
2121 return TPC_ACTION_DECREASE;
2122 }
2123
2124 if (weak_tpt > current_tpt) {
2125 IWL_DEBUG_RATE(mvm,
2126 "lower txp has better tpt. decrease txp\n");
2127 return TPC_ACTION_DECREASE;
2128 }
2129 }
2130
2131 /* next, increase if needed */
Eyal Shapira834437d2014-12-17 15:38:58 +02002132 if (sr < RS_PERCENT(IWL_MVM_RS_TPC_SR_NO_INCREASE) &&
2133 strong != TPC_INVALID) {
Eliad Peller2fd647f2014-03-13 17:21:36 +02002134 if (weak_tpt == IWL_INVALID_VALUE &&
2135 strong_tpt != IWL_INVALID_VALUE &&
2136 current_tpt < strong_tpt) {
2137 IWL_DEBUG_RATE(mvm,
2138 "higher txp has better tpt. increase txp\n");
2139 return TPC_ACTION_INCREASE;
2140 }
2141
2142 if (weak_tpt < current_tpt &&
2143 (strong_tpt == IWL_INVALID_VALUE ||
2144 strong_tpt > current_tpt)) {
2145 IWL_DEBUG_RATE(mvm,
2146 "lower txp has worse tpt. increase txp\n");
2147 return TPC_ACTION_INCREASE;
2148 }
2149 }
2150
2151 IWL_DEBUG_RATE(mvm, "no need to increase or decrease txp - stay\n");
2152 return TPC_ACTION_STAY;
2153}
2154
2155static bool rs_tpc_perform(struct iwl_mvm *mvm,
2156 struct ieee80211_sta *sta,
2157 struct iwl_lq_sta *lq_sta,
2158 struct iwl_scale_tbl_info *tbl)
2159{
Johannes Berg9d8ce6a2014-12-23 16:02:40 +01002160 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Eliad Peller2fd647f2014-03-13 17:21:36 +02002161 struct ieee80211_vif *vif = mvm_sta->vif;
2162 struct ieee80211_chanctx_conf *chanctx_conf;
Johannes Berg57fbcce2016-04-12 15:56:15 +02002163 enum nl80211_band band;
Eliad Peller2fd647f2014-03-13 17:21:36 +02002164 struct iwl_rate_scale_data *window;
2165 struct rs_rate *rate = &tbl->rate;
2166 enum tpc_action action;
2167 s32 sr;
2168 u8 cur = lq_sta->lq.reduced_tpc;
2169 int current_tpt;
2170 int weak, strong;
2171 int weak_tpt = IWL_INVALID_VALUE, strong_tpt = IWL_INVALID_VALUE;
2172
Eliad Peller9a75b3d2014-03-13 17:15:04 +02002173#ifdef CONFIG_MAC80211_DEBUGFS
Eliad Pellerae969af2014-06-11 15:51:33 +03002174 if (lq_sta->pers.dbg_fixed_txp_reduction <= TPC_MAX_REDUCTION) {
Johannes Berg3c6acb62014-05-07 11:47:53 +02002175 IWL_DEBUG_RATE(mvm, "fixed tpc: %d\n",
Eliad Pellerae969af2014-06-11 15:51:33 +03002176 lq_sta->pers.dbg_fixed_txp_reduction);
2177 lq_sta->lq.reduced_tpc = lq_sta->pers.dbg_fixed_txp_reduction;
2178 return cur != lq_sta->pers.dbg_fixed_txp_reduction;
Eliad Peller9a75b3d2014-03-13 17:15:04 +02002179 }
2180#endif
2181
Eliad Peller2fd647f2014-03-13 17:21:36 +02002182 rcu_read_lock();
2183 chanctx_conf = rcu_dereference(vif->chanctx_conf);
2184 if (WARN_ON(!chanctx_conf))
Johannes Berg57fbcce2016-04-12 15:56:15 +02002185 band = NUM_NL80211_BANDS;
Eliad Peller2fd647f2014-03-13 17:21:36 +02002186 else
2187 band = chanctx_conf->def.chan->band;
2188 rcu_read_unlock();
2189
Eyal Shapira1e9551d2014-04-21 21:33:17 +03002190 if (!rs_tpc_allowed(mvm, vif, rate, band)) {
Eliad Peller2fd647f2014-03-13 17:21:36 +02002191 IWL_DEBUG_RATE(mvm,
Johannes Berg3c6acb62014-05-07 11:47:53 +02002192 "tpc is not allowed. remove txp restrictions\n");
Eliad Peller2fd647f2014-03-13 17:21:36 +02002193 lq_sta->lq.reduced_tpc = TPC_NO_REDUCTION;
2194 return cur != TPC_NO_REDUCTION;
2195 }
2196
2197 rs_get_adjacent_txp(mvm, cur, &weak, &strong);
2198
2199 /* Collect measured throughputs for current and adjacent rates */
2200 window = tbl->tpc_win;
2201 sr = window[cur].success_ratio;
2202 current_tpt = window[cur].average_tpt;
2203 if (weak != TPC_INVALID)
2204 weak_tpt = window[weak].average_tpt;
2205 if (strong != TPC_INVALID)
2206 strong_tpt = window[strong].average_tpt;
2207
2208 IWL_DEBUG_RATE(mvm,
2209 "(TPC: %d): cur_tpt %d SR %d weak %d strong %d weak_tpt %d strong_tpt %d\n",
2210 cur, current_tpt, sr, weak, strong,
2211 weak_tpt, strong_tpt);
2212
2213 action = rs_get_tpc_action(mvm, sr, weak, strong,
2214 current_tpt, weak_tpt, strong_tpt);
2215
2216 /* override actions if we are on the edge */
2217 if (weak == TPC_INVALID && action == TPC_ACTION_DECREASE) {
Johannes Berg3c6acb62014-05-07 11:47:53 +02002218 IWL_DEBUG_RATE(mvm, "already in lowest txp, stay\n");
Eliad Peller2fd647f2014-03-13 17:21:36 +02002219 action = TPC_ACTION_STAY;
2220 } else if (strong == TPC_INVALID &&
2221 (action == TPC_ACTION_INCREASE ||
2222 action == TPC_ACTION_NO_RESTIRCTION)) {
Johannes Berg3c6acb62014-05-07 11:47:53 +02002223 IWL_DEBUG_RATE(mvm, "already in highest txp, stay\n");
Eliad Peller2fd647f2014-03-13 17:21:36 +02002224 action = TPC_ACTION_STAY;
2225 }
2226
2227 switch (action) {
2228 case TPC_ACTION_DECREASE:
2229 lq_sta->lq.reduced_tpc = weak;
2230 return true;
2231 case TPC_ACTION_INCREASE:
2232 lq_sta->lq.reduced_tpc = strong;
2233 return true;
2234 case TPC_ACTION_NO_RESTIRCTION:
2235 lq_sta->lq.reduced_tpc = TPC_NO_REDUCTION;
2236 return true;
2237 case TPC_ACTION_STAY:
2238 /* do nothing */
2239 break;
2240 }
2241 return false;
2242}
2243
Johannes Berg8ca151b2013-01-24 14:25:36 +01002244/*
2245 * Do rate scaling and search for new modulation mode.
2246 */
2247static void rs_rate_scale_perform(struct iwl_mvm *mvm,
Johannes Berg8ca151b2013-01-24 14:25:36 +01002248 struct ieee80211_sta *sta,
Eyal Shapira1ebc8f22014-09-14 15:23:23 +03002249 struct iwl_lq_sta *lq_sta,
Matti Gottliebff7a68d2016-05-17 10:50:08 +03002250 int tid, bool ndp)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002251{
Johannes Berg8ca151b2013-01-24 14:25:36 +01002252 int low = IWL_RATE_INVALID;
2253 int high = IWL_RATE_INVALID;
2254 int index;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002255 struct iwl_rate_scale_data *window = NULL;
2256 int current_tpt = IWL_INVALID_VALUE;
2257 int low_tpt = IWL_INVALID_VALUE;
2258 int high_tpt = IWL_INVALID_VALUE;
2259 u32 fail_count;
Eyal Shapira4e4b8152013-12-11 00:55:36 +02002260 enum rs_action scale_action = RS_ACTION_STAY;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002261 u16 rate_mask;
2262 u8 update_lq = 0;
2263 struct iwl_scale_tbl_info *tbl, *tbl1;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002264 u8 active_tbl = 0;
2265 u8 done_search = 0;
2266 u16 high_low;
2267 s32 sr;
Eyal Shapira9d015a02013-11-21 01:12:11 +02002268 u8 prev_agg = lq_sta->is_agg;
Luca Coelho7ccb4982017-08-01 09:01:34 +03002269 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Eyal Shapira5aa33552013-11-23 01:06:36 +02002270 struct rs_rate *rate;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002271
Luca Coelho7ccb4982017-08-01 09:01:34 +03002272 lq_sta->is_agg = !!mvmsta->agg_tids;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002273
2274 /*
2275 * Select rate-scale / modulation-mode table to work with in
2276 * the rest of this function: "search" if searching for better
2277 * modulation mode, or "active" if doing rate scaling within a mode.
2278 */
2279 if (!lq_sta->search_better_tbl)
2280 active_tbl = lq_sta->active_tbl;
2281 else
2282 active_tbl = 1 - lq_sta->active_tbl;
2283
2284 tbl = &(lq_sta->lq_info[active_tbl]);
Eyal Shapira5aa33552013-11-23 01:06:36 +02002285 rate = &tbl->rate;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002286
Eyal Shapira9d015a02013-11-21 01:12:11 +02002287 if (prev_agg != lq_sta->is_agg) {
2288 IWL_DEBUG_RATE(mvm,
2289 "Aggregation changed: prev %d current %d. Update expected TPT table\n",
2290 prev_agg, lq_sta->is_agg);
2291 rs_set_expected_tpt_table(lq_sta, tbl);
Eyal Shapirab804eeb2014-04-06 04:27:06 +03002292 rs_rate_scale_clear_tbl_windows(mvm, tbl);
Eyal Shapira9d015a02013-11-21 01:12:11 +02002293 }
2294
Johannes Berg8ca151b2013-01-24 14:25:36 +01002295 /* current tx rate */
Eyal Shapirafcc5e852015-04-12 23:45:27 +03002296 index = rate->index;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002297
Johannes Berg8ca151b2013-01-24 14:25:36 +01002298 /* rates available for this association, and for modulation mode */
Eyal Shapiraa8ff14f2013-11-23 01:30:44 +02002299 rate_mask = rs_get_supported_rates(lq_sta, rate);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002300
Eyal Shapiraca6f38f2013-12-09 11:14:11 +02002301 if (!(BIT(index) & rate_mask)) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002302 IWL_ERR(mvm, "Current Rate is not valid\n");
2303 if (lq_sta->search_better_tbl) {
2304 /* revert to active table if search table is not valid*/
Eyal Shapira5aa33552013-11-23 01:06:36 +02002305 rate->type = LQ_NONE;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002306 lq_sta->search_better_tbl = 0;
2307 tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
Eyal Shapira7b9d74e2015-03-29 18:38:07 +03002308 rs_update_rate_tbl(mvm, sta, lq_sta, tbl);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002309 }
2310 return;
2311 }
2312
2313 /* Get expected throughput table and history window for current rate */
2314 if (!tbl->expected_tpt) {
2315 IWL_ERR(mvm, "tbl->expected_tpt is NULL\n");
2316 return;
2317 }
2318
Eyal Shapira2536e7c2014-08-31 02:33:33 +03002319 /* TODO: handle rate_idx_mask and rate_idx_mcs_mask */
Johannes Berg8ca151b2013-01-24 14:25:36 +01002320 window = &(tbl->win[index]);
2321
2322 /*
2323 * If there is not enough history to calculate actual average
2324 * throughput, keep analyzing results of more tx frames, without
2325 * changing rate or mode (bypass most of the rest of this function).
2326 * Set up new rate table in uCode only if old rate is not supported
2327 * in current association (use new rate found above).
2328 */
2329 fail_count = window->counter - window->success_counter;
Eyal Shapira834437d2014-12-17 15:38:58 +02002330 if ((fail_count < IWL_MVM_RS_RATE_MIN_FAILURE_TH) &&
2331 (window->success_counter < IWL_MVM_RS_RATE_MIN_SUCCESS_TH)) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002332 IWL_DEBUG_RATE(mvm,
Eyal Shapiracc60c6e2015-09-19 23:37:45 +03002333 "%s: Test Window: succ %d total %d\n",
2334 rs_pretty_rate(rate),
2335 window->success_counter, window->counter);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002336
2337 /* Can't calculate this yet; not enough history */
2338 window->average_tpt = IWL_INVALID_VALUE;
2339
2340 /* Should we stay with this modulation mode,
2341 * or search for a new one? */
2342 rs_stay_in_table(lq_sta, false);
2343
Eyal Shapirafcc5e852015-04-12 23:45:27 +03002344 return;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002345 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01002346
2347 /* If we are searching for better modulation mode, check success. */
2348 if (lq_sta->search_better_tbl) {
2349 /* If good success, continue using the "search" mode;
2350 * no need to send new link quality command, since we're
2351 * continuing to use the setup that we've been trying. */
2352 if (window->average_tpt > lq_sta->last_tpt) {
2353 IWL_DEBUG_RATE(mvm,
Eyal Shapira4d30ee82013-11-20 01:45:38 +02002354 "SWITCHING TO NEW TABLE SR: %d "
2355 "cur-tpt %d old-tpt %d\n",
Johannes Berg8ca151b2013-01-24 14:25:36 +01002356 window->success_ratio,
2357 window->average_tpt,
2358 lq_sta->last_tpt);
2359
Johannes Berg8ca151b2013-01-24 14:25:36 +01002360 /* Swap tables; "search" becomes "active" */
2361 lq_sta->active_tbl = active_tbl;
2362 current_tpt = window->average_tpt;
2363 /* Else poor success; go back to mode in "active" table */
2364 } else {
2365 IWL_DEBUG_RATE(mvm,
Eyal Shapira4d30ee82013-11-20 01:45:38 +02002366 "GOING BACK TO THE OLD TABLE: SR %d "
2367 "cur-tpt %d old-tpt %d\n",
Johannes Berg8ca151b2013-01-24 14:25:36 +01002368 window->success_ratio,
2369 window->average_tpt,
2370 lq_sta->last_tpt);
2371
2372 /* Nullify "search" table */
Eyal Shapira5aa33552013-11-23 01:06:36 +02002373 rate->type = LQ_NONE;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002374
2375 /* Revert to "active" table */
2376 active_tbl = lq_sta->active_tbl;
2377 tbl = &(lq_sta->lq_info[active_tbl]);
2378
2379 /* Revert to "active" rate and throughput info */
Eyal Shapira8fc7c582013-12-04 02:15:46 +02002380 index = tbl->rate.index;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002381 current_tpt = lq_sta->last_tpt;
2382
2383 /* Need to set up a new rate table in uCode */
2384 update_lq = 1;
2385 }
2386
2387 /* Either way, we've made a decision; modulation mode
2388 * search is done, allow rate adjustment next time. */
2389 lq_sta->search_better_tbl = 0;
2390 done_search = 1; /* Don't switch modes below! */
2391 goto lq_update;
2392 }
2393
2394 /* (Else) not in search of better modulation mode, try for better
2395 * starting rate, while staying in this mode. */
Eyal Shapiraca6f38f2013-12-09 11:14:11 +02002396 high_low = rs_get_adjacent_rate(mvm, index, rate_mask, rate->type);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002397 low = high_low & 0xff;
2398 high = (high_low >> 8) & 0xff;
2399
Eyal Shapira2536e7c2014-08-31 02:33:33 +03002400 /* TODO: handle rate_idx_mask and rate_idx_mcs_mask */
Johannes Berg8ca151b2013-01-24 14:25:36 +01002401
2402 sr = window->success_ratio;
2403
2404 /* Collect measured throughputs for current and adjacent rates */
2405 current_tpt = window->average_tpt;
2406 if (low != IWL_RATE_INVALID)
2407 low_tpt = tbl->win[low].average_tpt;
2408 if (high != IWL_RATE_INVALID)
2409 high_tpt = tbl->win[high].average_tpt;
2410
Eyal Shapira4d30ee82013-11-20 01:45:38 +02002411 IWL_DEBUG_RATE(mvm,
Eyal Shapiracc60c6e2015-09-19 23:37:45 +03002412 "%s: cur_tpt %d SR %d low %d high %d low_tpt %d high_tpt %d\n",
2413 rs_pretty_rate(rate), current_tpt, sr,
Eyal Shapira5aa33552013-11-23 01:06:36 +02002414 low, high, low_tpt, high_tpt);
Eyal Shapira4d30ee82013-11-20 01:45:38 +02002415
Eyal Shapira4e4b8152013-12-11 00:55:36 +02002416 scale_action = rs_get_rate_action(mvm, tbl, sr, low, high,
2417 current_tpt, low_tpt, high_tpt);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002418
Eyal Shapira393b9e52013-11-13 16:46:19 +02002419 /* Force a search in case BT doesn't like us being in MIMO */
Eyal Shapira5aa33552013-11-23 01:06:36 +02002420 if (is_mimo(rate) &&
Eyal Shapira393b9e52013-11-13 16:46:19 +02002421 !iwl_mvm_bt_coex_is_mimo_allowed(mvm, sta)) {
Eyal Shapira4d30ee82013-11-20 01:45:38 +02002422 IWL_DEBUG_RATE(mvm,
2423 "BT Coex forbids MIMO. Search for new config\n");
Emmanuel Grumbach36946ce2013-05-28 23:12:47 +03002424 rs_stay_in_table(lq_sta, true);
2425 goto lq_update;
2426 }
2427
Johannes Berg8ca151b2013-01-24 14:25:36 +01002428 switch (scale_action) {
Eyal Shapira4e4b8152013-12-11 00:55:36 +02002429 case RS_ACTION_DOWNSCALE:
Johannes Berg8ca151b2013-01-24 14:25:36 +01002430 /* Decrease starting rate, update uCode's rate table */
2431 if (low != IWL_RATE_INVALID) {
2432 update_lq = 1;
2433 index = low;
Eyal Shapira4d30ee82013-11-20 01:45:38 +02002434 } else {
2435 IWL_DEBUG_RATE(mvm,
2436 "At the bottom rate. Can't decrease\n");
Johannes Berg8ca151b2013-01-24 14:25:36 +01002437 }
2438
2439 break;
Eyal Shapira4e4b8152013-12-11 00:55:36 +02002440 case RS_ACTION_UPSCALE:
Johannes Berg8ca151b2013-01-24 14:25:36 +01002441 /* Increase starting rate, update uCode's rate table */
2442 if (high != IWL_RATE_INVALID) {
2443 update_lq = 1;
2444 index = high;
Eyal Shapira4d30ee82013-11-20 01:45:38 +02002445 } else {
2446 IWL_DEBUG_RATE(mvm,
2447 "At the top rate. Can't increase\n");
Johannes Berg8ca151b2013-01-24 14:25:36 +01002448 }
2449
2450 break;
Eyal Shapira4e4b8152013-12-11 00:55:36 +02002451 case RS_ACTION_STAY:
Johannes Berg8ca151b2013-01-24 14:25:36 +01002452 /* No change */
Eyal Shapirae6c8d602014-04-21 21:49:08 +03002453 if (lq_sta->rs_state == RS_STATE_STAY_IN_COLUMN)
2454 update_lq = rs_tpc_perform(mvm, sta, lq_sta, tbl);
Eliad Peller2fd647f2014-03-13 17:21:36 +02002455 break;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002456 default:
2457 break;
2458 }
2459
Johannes Berg8ca151b2013-01-24 14:25:36 +01002460lq_update:
2461 /* Replace uCode's rate table for the destination station. */
Eyal Shapira5aa33552013-11-23 01:06:36 +02002462 if (update_lq) {
2463 tbl->rate.index = index;
Eyal Shapira1191f642015-09-19 23:48:45 +03002464 if (IWL_MVM_RS_80_20_FAR_RANGE_TWEAK)
2465 rs_tweak_rate_tbl(mvm, sta, lq_sta, tbl, scale_action);
Emmanuel Grumbach04e3a5d2015-10-28 09:47:41 +02002466 rs_set_amsdu_len(mvm, sta, tbl, scale_action);
Eyal Shapira7b9d74e2015-03-29 18:38:07 +03002467 rs_update_rate_tbl(mvm, sta, lq_sta, tbl);
Eyal Shapira5aa33552013-11-23 01:06:36 +02002468 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01002469
2470 rs_stay_in_table(lq_sta, false);
2471
2472 /*
2473 * Search for new modulation mode if we're:
2474 * 1) Not changing rates right now
2475 * 2) Not just finishing up a search
2476 * 3) Allowing a new search
2477 */
2478 if (!update_lq && !done_search &&
Eyal Shapirab3b06a32013-11-24 21:30:13 +02002479 lq_sta->rs_state == RS_STATE_SEARCH_CYCLE_STARTED
2480 && window->counter) {
2481 enum rs_column next_column;
2482
Johannes Berg8ca151b2013-01-24 14:25:36 +01002483 /* Save current throughput to compare with "search" throughput*/
2484 lq_sta->last_tpt = current_tpt;
2485
Eyal Shapira4d30ee82013-11-20 01:45:38 +02002486 IWL_DEBUG_RATE(mvm,
Eyal Shapirab3b06a32013-11-24 21:30:13 +02002487 "Start Search: update_lq %d done_search %d rs_state %d win->counter %d\n",
2488 update_lq, done_search, lq_sta->rs_state,
Eyal Shapira4d30ee82013-11-20 01:45:38 +02002489 window->counter);
Eyal Shapirab3b06a32013-11-24 21:30:13 +02002490
2491 next_column = rs_get_next_column(mvm, lq_sta, sta, tbl);
2492 if (next_column != RS_COLUMN_INVALID) {
2493 int ret = rs_switch_to_column(mvm, lq_sta, sta,
2494 next_column);
2495 if (!ret)
2496 lq_sta->search_better_tbl = 1;
2497 } else {
2498 IWL_DEBUG_RATE(mvm,
2499 "No more columns to explore in search cycle. Go to RS_STATE_SEARCH_CYCLE_ENDED\n");
2500 lq_sta->rs_state = RS_STATE_SEARCH_CYCLE_ENDED;
2501 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01002502
2503 /* If new "search" mode was selected, set up in uCode table */
2504 if (lq_sta->search_better_tbl) {
2505 /* Access the "search" table, clear its history. */
2506 tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Eyal Shapirab804eeb2014-04-06 04:27:06 +03002507 rs_rate_scale_clear_tbl_windows(mvm, tbl);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002508
2509 /* Use new "search" start rate */
Eyal Shapira8fc7c582013-12-04 02:15:46 +02002510 index = tbl->rate.index;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002511
Eyal Shapirab3b06a32013-11-24 21:30:13 +02002512 rs_dump_rate(mvm, &tbl->rate,
2513 "Switch to SEARCH TABLE:");
Eyal Shapira7b9d74e2015-03-29 18:38:07 +03002514 rs_update_rate_tbl(mvm, sta, lq_sta, tbl);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002515 } else {
2516 done_search = 1;
2517 }
2518 }
2519
Gregory Greenman482e4842017-08-15 12:27:01 +03002520 if (!ndp)
2521 rs_tl_turn_on_agg(mvm, mvmsta, tid, lq_sta, sta);
Eyal Shapirafd7dbee2014-04-06 04:39:23 +03002522
Gregory Greenman482e4842017-08-15 12:27:01 +03002523 if (done_search && lq_sta->rs_state == RS_STATE_SEARCH_CYCLE_ENDED) {
2524 tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
2525 rs_set_stay_in_table(mvm, is_legacy(&tbl1->rate), lq_sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002526 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01002527}
2528
Eyal Shapira361dbec2014-08-13 00:31:13 +03002529struct rs_init_rate_info {
2530 s8 rssi;
2531 u8 rate_idx;
2532};
2533
Eyal Shapirab503ed62015-05-27 22:20:46 +03002534static const struct rs_init_rate_info rs_optimal_rates_24ghz_legacy[] = {
Eyal Shapira361dbec2014-08-13 00:31:13 +03002535 { -60, IWL_RATE_54M_INDEX },
2536 { -64, IWL_RATE_48M_INDEX },
2537 { -68, IWL_RATE_36M_INDEX },
2538 { -80, IWL_RATE_24M_INDEX },
2539 { -84, IWL_RATE_18M_INDEX },
2540 { -85, IWL_RATE_12M_INDEX },
2541 { -86, IWL_RATE_11M_INDEX },
2542 { -88, IWL_RATE_5M_INDEX },
2543 { -90, IWL_RATE_2M_INDEX },
2544 { S8_MIN, IWL_RATE_1M_INDEX },
2545};
2546
Eyal Shapirab503ed62015-05-27 22:20:46 +03002547static const struct rs_init_rate_info rs_optimal_rates_5ghz_legacy[] = {
Eyal Shapira361dbec2014-08-13 00:31:13 +03002548 { -60, IWL_RATE_54M_INDEX },
2549 { -64, IWL_RATE_48M_INDEX },
2550 { -72, IWL_RATE_36M_INDEX },
2551 { -80, IWL_RATE_24M_INDEX },
2552 { -84, IWL_RATE_18M_INDEX },
2553 { -85, IWL_RATE_12M_INDEX },
2554 { -87, IWL_RATE_9M_INDEX },
2555 { S8_MIN, IWL_RATE_6M_INDEX },
2556};
2557
Eyal Shapirab503ed62015-05-27 22:20:46 +03002558static const struct rs_init_rate_info rs_optimal_rates_ht[] = {
2559 { -60, IWL_RATE_MCS_7_INDEX },
2560 { -64, IWL_RATE_MCS_6_INDEX },
2561 { -68, IWL_RATE_MCS_5_INDEX },
2562 { -72, IWL_RATE_MCS_4_INDEX },
2563 { -80, IWL_RATE_MCS_3_INDEX },
2564 { -84, IWL_RATE_MCS_2_INDEX },
2565 { -85, IWL_RATE_MCS_1_INDEX },
2566 { S8_MIN, IWL_RATE_MCS_0_INDEX},
2567};
2568
Gregory Greenman855f4922016-05-25 18:31:39 +03002569/* MCS index 9 is not valid for 20MHz VHT channel width,
2570 * but is ok for 40, 80 and 160MHz channels.
2571 */
Eyal Shapirab503ed62015-05-27 22:20:46 +03002572static const struct rs_init_rate_info rs_optimal_rates_vht_20mhz[] = {
2573 { -60, IWL_RATE_MCS_8_INDEX },
2574 { -64, IWL_RATE_MCS_7_INDEX },
2575 { -68, IWL_RATE_MCS_6_INDEX },
2576 { -72, IWL_RATE_MCS_5_INDEX },
2577 { -80, IWL_RATE_MCS_4_INDEX },
2578 { -84, IWL_RATE_MCS_3_INDEX },
2579 { -85, IWL_RATE_MCS_2_INDEX },
2580 { -87, IWL_RATE_MCS_1_INDEX },
2581 { S8_MIN, IWL_RATE_MCS_0_INDEX},
2582};
2583
Gregory Greenman855f4922016-05-25 18:31:39 +03002584static const struct rs_init_rate_info rs_optimal_rates_vht[] = {
Eyal Shapirab503ed62015-05-27 22:20:46 +03002585 { -60, IWL_RATE_MCS_9_INDEX },
2586 { -64, IWL_RATE_MCS_8_INDEX },
2587 { -68, IWL_RATE_MCS_7_INDEX },
2588 { -72, IWL_RATE_MCS_6_INDEX },
2589 { -80, IWL_RATE_MCS_5_INDEX },
2590 { -84, IWL_RATE_MCS_4_INDEX },
2591 { -85, IWL_RATE_MCS_3_INDEX },
2592 { -87, IWL_RATE_MCS_2_INDEX },
2593 { -88, IWL_RATE_MCS_1_INDEX },
2594 { S8_MIN, IWL_RATE_MCS_0_INDEX },
2595};
2596
Gregory Greenman99319b82015-11-24 19:13:27 +02002597#define IWL_RS_LOW_RSSI_THRESHOLD (-76) /* dBm */
2598
Eyal Shapirab503ed62015-05-27 22:20:46 +03002599/* Init the optimal rate based on STA caps
2600 * This combined with rssi is used to report the last tx rate
2601 * to userspace when we haven't transmitted enough frames.
2602 */
2603static void rs_init_optimal_rate(struct iwl_mvm *mvm,
2604 struct ieee80211_sta *sta,
2605 struct iwl_lq_sta *lq_sta)
2606{
2607 struct rs_rate *rate = &lq_sta->optimal_rate;
2608
2609 if (lq_sta->max_mimo2_rate_idx != IWL_RATE_INVALID)
2610 rate->type = lq_sta->is_vht ? LQ_VHT_MIMO2 : LQ_HT_MIMO2;
2611 else if (lq_sta->max_siso_rate_idx != IWL_RATE_INVALID)
2612 rate->type = lq_sta->is_vht ? LQ_VHT_SISO : LQ_HT_SISO;
Johannes Berg57fbcce2016-04-12 15:56:15 +02002613 else if (lq_sta->band == NL80211_BAND_5GHZ)
Eyal Shapirab503ed62015-05-27 22:20:46 +03002614 rate->type = LQ_LEGACY_A;
2615 else
2616 rate->type = LQ_LEGACY_G;
2617
2618 rate->bw = rs_bw_from_sta_bw(sta);
2619 rate->sgi = rs_sgi_allow(mvm, sta, rate, NULL);
2620
2621 /* ANT/LDPC/STBC aren't relevant for the rate reported to userspace */
2622
2623 if (is_mimo(rate)) {
2624 lq_sta->optimal_rate_mask = lq_sta->active_mimo2_rate;
2625 } else if (is_siso(rate)) {
2626 lq_sta->optimal_rate_mask = lq_sta->active_siso_rate;
2627 } else {
2628 lq_sta->optimal_rate_mask = lq_sta->active_legacy_rate;
2629
Johannes Berg57fbcce2016-04-12 15:56:15 +02002630 if (lq_sta->band == NL80211_BAND_5GHZ) {
Eyal Shapirab503ed62015-05-27 22:20:46 +03002631 lq_sta->optimal_rates = rs_optimal_rates_5ghz_legacy;
2632 lq_sta->optimal_nentries =
2633 ARRAY_SIZE(rs_optimal_rates_5ghz_legacy);
2634 } else {
2635 lq_sta->optimal_rates = rs_optimal_rates_24ghz_legacy;
2636 lq_sta->optimal_nentries =
2637 ARRAY_SIZE(rs_optimal_rates_24ghz_legacy);
2638 }
2639 }
2640
2641 if (is_vht(rate)) {
2642 if (rate->bw == RATE_MCS_CHAN_WIDTH_20) {
2643 lq_sta->optimal_rates = rs_optimal_rates_vht_20mhz;
2644 lq_sta->optimal_nentries =
2645 ARRAY_SIZE(rs_optimal_rates_vht_20mhz);
2646 } else {
Gregory Greenman855f4922016-05-25 18:31:39 +03002647 lq_sta->optimal_rates = rs_optimal_rates_vht;
Eyal Shapirab503ed62015-05-27 22:20:46 +03002648 lq_sta->optimal_nentries =
Gregory Greenman855f4922016-05-25 18:31:39 +03002649 ARRAY_SIZE(rs_optimal_rates_vht);
Eyal Shapirab503ed62015-05-27 22:20:46 +03002650 }
2651 } else if (is_ht(rate)) {
2652 lq_sta->optimal_rates = rs_optimal_rates_ht;
2653 lq_sta->optimal_nentries = ARRAY_SIZE(rs_optimal_rates_ht);
2654 }
2655}
2656
2657/* Compute the optimal rate index based on RSSI */
2658static struct rs_rate *rs_get_optimal_rate(struct iwl_mvm *mvm,
2659 struct iwl_lq_sta *lq_sta)
2660{
2661 struct rs_rate *rate = &lq_sta->optimal_rate;
2662 int i;
2663
2664 rate->index = find_first_bit(&lq_sta->optimal_rate_mask,
2665 BITS_PER_LONG);
2666
2667 for (i = 0; i < lq_sta->optimal_nentries; i++) {
2668 int rate_idx = lq_sta->optimal_rates[i].rate_idx;
2669
2670 if ((lq_sta->pers.last_rssi >= lq_sta->optimal_rates[i].rssi) &&
2671 (BIT(rate_idx) & lq_sta->optimal_rate_mask)) {
2672 rate->index = rate_idx;
2673 break;
2674 }
2675 }
2676
Eyal Shapirab503ed62015-05-27 22:20:46 +03002677 return rate;
2678}
2679
Eyal Shapira361dbec2014-08-13 00:31:13 +03002680/* Choose an initial legacy rate and antenna to use based on the RSSI
2681 * of last Rx
2682 */
2683static void rs_get_initial_rate(struct iwl_mvm *mvm,
Gregory Greenman99319b82015-11-24 19:13:27 +02002684 struct ieee80211_sta *sta,
Eyal Shapira361dbec2014-08-13 00:31:13 +03002685 struct iwl_lq_sta *lq_sta,
Johannes Berg57fbcce2016-04-12 15:56:15 +02002686 enum nl80211_band band,
Eyal Shapira361dbec2014-08-13 00:31:13 +03002687 struct rs_rate *rate)
2688{
2689 int i, nentries;
Gregory Greenman99319b82015-11-24 19:13:27 +02002690 unsigned long active_rate;
Eyal Shapira361dbec2014-08-13 00:31:13 +03002691 s8 best_rssi = S8_MIN;
2692 u8 best_ant = ANT_NONE;
Moshe Harela0544272014-12-08 21:13:14 +02002693 u8 valid_tx_ant = iwl_mvm_get_valid_tx_ant(mvm);
Eyal Shapira361dbec2014-08-13 00:31:13 +03002694 const struct rs_init_rate_info *initial_rates;
2695
2696 for (i = 0; i < ARRAY_SIZE(lq_sta->pers.chain_signal); i++) {
2697 if (!(lq_sta->pers.chains & BIT(i)))
2698 continue;
2699
2700 if (lq_sta->pers.chain_signal[i] > best_rssi) {
2701 best_rssi = lq_sta->pers.chain_signal[i];
2702 best_ant = BIT(i);
2703 }
2704 }
2705
2706 IWL_DEBUG_RATE(mvm, "Best ANT: %s Best RSSI: %d\n",
2707 rs_pretty_ant(best_ant), best_rssi);
2708
2709 if (best_ant != ANT_A && best_ant != ANT_B)
2710 rate->ant = first_antenna(valid_tx_ant);
2711 else
2712 rate->ant = best_ant;
2713
2714 rate->sgi = false;
2715 rate->ldpc = false;
2716 rate->bw = RATE_MCS_CHAN_WIDTH_20;
2717
2718 rate->index = find_first_bit(&lq_sta->active_legacy_rate,
2719 BITS_PER_LONG);
2720
Johannes Berg57fbcce2016-04-12 15:56:15 +02002721 if (band == NL80211_BAND_5GHZ) {
Eyal Shapira361dbec2014-08-13 00:31:13 +03002722 rate->type = LQ_LEGACY_A;
Eyal Shapirab503ed62015-05-27 22:20:46 +03002723 initial_rates = rs_optimal_rates_5ghz_legacy;
2724 nentries = ARRAY_SIZE(rs_optimal_rates_5ghz_legacy);
Eyal Shapira361dbec2014-08-13 00:31:13 +03002725 } else {
2726 rate->type = LQ_LEGACY_G;
Eyal Shapirab503ed62015-05-27 22:20:46 +03002727 initial_rates = rs_optimal_rates_24ghz_legacy;
2728 nentries = ARRAY_SIZE(rs_optimal_rates_24ghz_legacy);
Eyal Shapira361dbec2014-08-13 00:31:13 +03002729 }
2730
Gregory Greenman99319b82015-11-24 19:13:27 +02002731 if (!IWL_MVM_RS_RSSI_BASED_INIT_RATE)
2732 goto out;
2733
2734 /* Start from a higher rate if the corresponding debug capability
2735 * is enabled. The rate is chosen according to AP capabilities.
2736 * In case of VHT/HT when the rssi is low fallback to the case of
2737 * legacy rates.
2738 */
2739 if (sta->vht_cap.vht_supported &&
2740 best_rssi > IWL_RS_LOW_RSSI_THRESHOLD) {
Gregory Greenman855f4922016-05-25 18:31:39 +03002741 switch (sta->bandwidth) {
2742 case IEEE80211_STA_RX_BW_160:
2743 case IEEE80211_STA_RX_BW_80:
2744 case IEEE80211_STA_RX_BW_40:
2745 initial_rates = rs_optimal_rates_vht;
2746 nentries = ARRAY_SIZE(rs_optimal_rates_vht);
2747 break;
2748 case IEEE80211_STA_RX_BW_20:
Gregory Greenman99319b82015-11-24 19:13:27 +02002749 initial_rates = rs_optimal_rates_vht_20mhz;
2750 nentries = ARRAY_SIZE(rs_optimal_rates_vht_20mhz);
Gregory Greenman855f4922016-05-25 18:31:39 +03002751 break;
2752 default:
Gregory Greenman99319b82015-11-24 19:13:27 +02002753 IWL_ERR(mvm, "Invalid BW %d\n", sta->bandwidth);
2754 goto out;
2755 }
Gregory Greenman855f4922016-05-25 18:31:39 +03002756
Gregory Greenman99319b82015-11-24 19:13:27 +02002757 active_rate = lq_sta->active_siso_rate;
2758 rate->type = LQ_VHT_SISO;
Gregory Greenman855f4922016-05-25 18:31:39 +03002759 rate->bw = rs_bw_from_sta_bw(sta);
Gregory Greenman99319b82015-11-24 19:13:27 +02002760 } else if (sta->ht_cap.ht_supported &&
2761 best_rssi > IWL_RS_LOW_RSSI_THRESHOLD) {
2762 initial_rates = rs_optimal_rates_ht;
2763 nentries = ARRAY_SIZE(rs_optimal_rates_ht);
2764 active_rate = lq_sta->active_siso_rate;
2765 rate->type = LQ_HT_SISO;
2766 } else {
2767 active_rate = lq_sta->active_legacy_rate;
2768 }
2769
2770 for (i = 0; i < nentries; i++) {
2771 int rate_idx = initial_rates[i].rate_idx;
2772
2773 if ((best_rssi >= initial_rates[i].rssi) &&
2774 (BIT(rate_idx) & active_rate)) {
2775 rate->index = rate_idx;
2776 break;
Eyal Shapira361dbec2014-08-13 00:31:13 +03002777 }
2778 }
2779
Gregory Greenman99319b82015-11-24 19:13:27 +02002780out:
2781 rs_dump_rate(mvm, rate, "INITIAL");
Eyal Shapira361dbec2014-08-13 00:31:13 +03002782}
2783
2784/* Save info about RSSI of last Rx */
2785void rs_update_last_rssi(struct iwl_mvm *mvm,
Gregory Greenmanecaf71d2017-11-01 07:16:29 +02002786 struct iwl_mvm_sta *mvmsta,
Eyal Shapira361dbec2014-08-13 00:31:13 +03002787 struct ieee80211_rx_status *rx_status)
2788{
Gregory Greenmanecaf71d2017-11-01 07:16:29 +02002789 struct iwl_lq_sta *lq_sta = &mvmsta->lq_sta.rs_drv;
Eyal Shapirab503ed62015-05-27 22:20:46 +03002790 int i;
2791
Eyal Shapira361dbec2014-08-13 00:31:13 +03002792 lq_sta->pers.chains = rx_status->chains;
2793 lq_sta->pers.chain_signal[0] = rx_status->chain_signal[0];
2794 lq_sta->pers.chain_signal[1] = rx_status->chain_signal[1];
2795 lq_sta->pers.chain_signal[2] = rx_status->chain_signal[2];
Eyal Shapirab503ed62015-05-27 22:20:46 +03002796 lq_sta->pers.last_rssi = S8_MIN;
2797
2798 for (i = 0; i < ARRAY_SIZE(lq_sta->pers.chain_signal); i++) {
2799 if (!(lq_sta->pers.chains & BIT(i)))
2800 continue;
2801
2802 if (lq_sta->pers.chain_signal[i] > lq_sta->pers.last_rssi)
2803 lq_sta->pers.last_rssi = lq_sta->pers.chain_signal[i];
2804 }
Eyal Shapira361dbec2014-08-13 00:31:13 +03002805}
2806
Johannes Berg8ca151b2013-01-24 14:25:36 +01002807/**
2808 * rs_initialize_lq - Initialize a station's hardware rate table
2809 *
2810 * The uCode's station table contains a table of fallback rates
2811 * for automatic fallback during transmission.
2812 *
2813 * NOTE: This sets up a default set of values. These will be replaced later
2814 * if the driver's iwl-agn-rs rate scaling algorithm is used, instead of
2815 * rc80211_simple.
2816 *
2817 * NOTE: Run REPLY_ADD_STA command to set up station table entry, before
2818 * calling this function (which runs REPLY_TX_LINK_QUALITY_CMD,
2819 * which requires station table entry to exist).
2820 */
2821static void rs_initialize_lq(struct iwl_mvm *mvm,
2822 struct ieee80211_sta *sta,
2823 struct iwl_lq_sta *lq_sta,
Johannes Berg57fbcce2016-04-12 15:56:15 +02002824 enum nl80211_band band,
Eyal Shapirab87c2172013-11-09 23:37:55 +02002825 bool init)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002826{
2827 struct iwl_scale_tbl_info *tbl;
Eyal Shapira5aa33552013-11-23 01:06:36 +02002828 struct rs_rate *rate;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002829 u8 active_tbl = 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002830
2831 if (!sta || !lq_sta)
2832 return;
2833
Johannes Berg8ca151b2013-01-24 14:25:36 +01002834 if (!lq_sta->search_better_tbl)
2835 active_tbl = lq_sta->active_tbl;
2836 else
2837 active_tbl = 1 - lq_sta->active_tbl;
2838
2839 tbl = &(lq_sta->lq_info[active_tbl]);
Eyal Shapira5aa33552013-11-23 01:06:36 +02002840 rate = &tbl->rate;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002841
Gregory Greenman99319b82015-11-24 19:13:27 +02002842 rs_get_initial_rate(mvm, sta, lq_sta, band, rate);
Eyal Shapirab503ed62015-05-27 22:20:46 +03002843 rs_init_optimal_rate(mvm, sta, lq_sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002844
Gregory Greenman8f6438f2017-04-24 09:24:37 +03002845 WARN_ONCE(rate->ant != ANT_A && rate->ant != ANT_B,
2846 "ant: 0x%x, chains 0x%x, fw tx ant: 0x%x, nvm tx ant: 0x%x\n",
2847 rate->ant, lq_sta->pers.chains, mvm->fw->valid_tx_ant,
2848 mvm->nvm_data ? mvm->nvm_data->valid_tx_ant : ANT_INVALID);
2849
Gregory Greenman99319b82015-11-24 19:13:27 +02002850 tbl->column = rs_get_column_from_rate(rate);
Eyal Shapirab3b06a32013-11-24 21:30:13 +02002851
Johannes Berg8ca151b2013-01-24 14:25:36 +01002852 rs_set_expected_tpt_table(lq_sta, tbl);
Eyal Shapira80e9e5c2013-12-23 16:26:41 +02002853 rs_fill_lq_cmd(mvm, sta, lq_sta, rate);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002854 /* TODO restore station should remember the lq cmd */
Eyal Shapirab87c2172013-11-09 23:37:55 +02002855 iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, init);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002856}
2857
Gregory Greenman9f66a392017-11-05 18:49:48 +02002858static void rs_drv_get_rate(void *mvm_r, struct ieee80211_sta *sta,
2859 void *mvm_sta,
2860 struct ieee80211_tx_rate_control *txrc)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002861{
Gregory Greenmanecaf71d2017-11-01 07:16:29 +02002862 struct iwl_op_mode *op_mode = mvm_r;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002863 struct iwl_mvm *mvm __maybe_unused = IWL_OP_MODE_GET_MVM(op_mode);
Gregory Greenmanecaf71d2017-11-01 07:16:29 +02002864 struct sk_buff *skb = txrc->skb;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002865 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Gregory Greenmanecaf71d2017-11-01 07:16:29 +02002866 struct iwl_lq_sta *lq_sta;
Eyal Shapirab503ed62015-05-27 22:20:46 +03002867 struct rs_rate *optimal_rate;
2868 u32 last_ucode_rate;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002869
Johannes Bergdcbc17a2015-03-10 21:24:34 +01002870 if (sta && !iwl_mvm_sta_from_mac80211(sta)->vif) {
2871 /* if vif isn't initialized mvm doesn't know about
2872 * this station, so don't do anything with the it
2873 */
2874 sta = NULL;
2875 mvm_sta = NULL;
2876 }
2877
Johannes Berg8ca151b2013-01-24 14:25:36 +01002878 /* Send management frames and NO_ACK data using lowest rate. */
2879 if (rate_control_send_low(sta, mvm_sta, txrc))
2880 return;
2881
Gregory Greenmanecaf71d2017-11-01 07:16:29 +02002882 if (!mvm_sta)
2883 return;
2884
2885 lq_sta = mvm_sta;
Eyal Shapirad310e402013-08-11 18:43:47 +03002886 iwl_mvm_hwrate_to_tx_rate(lq_sta->last_rate_n_flags,
2887 info->band, &info->control.rates[0]);
Moshe Benji622ebe92013-06-03 19:27:16 +03002888 info->control.rates[0].count = 1;
Eyal Shapirab503ed62015-05-27 22:20:46 +03002889
2890 /* Report the optimal rate based on rssi and STA caps if we haven't
2891 * converged yet (too little traffic) or exploring other modulations
2892 */
2893 if (lq_sta->rs_state != RS_STATE_STAY_IN_COLUMN) {
2894 optimal_rate = rs_get_optimal_rate(mvm, lq_sta);
2895 last_ucode_rate = ucode_rate_from_rs_rate(mvm,
2896 optimal_rate);
2897 iwl_mvm_hwrate_to_tx_rate(last_ucode_rate, info->band,
2898 &txrc->reported_rate);
2899 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01002900}
2901
Gregory Greenman9f66a392017-11-05 18:49:48 +02002902static void *rs_drv_alloc_sta(void *mvm_rate, struct ieee80211_sta *sta,
2903 gfp_t gfp)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002904{
Luca Coelho7ccb4982017-08-01 09:01:34 +03002905 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Eliad Pellerae969af2014-06-11 15:51:33 +03002906 struct iwl_op_mode *op_mode = (struct iwl_op_mode *)mvm_rate;
2907 struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
Gregory Greenmanecaf71d2017-11-01 07:16:29 +02002908 struct iwl_lq_sta *lq_sta = &mvmsta->lq_sta.rs_drv;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002909
2910 IWL_DEBUG_RATE(mvm, "create station rate scale window\n");
2911
Eliad Pellerae969af2014-06-11 15:51:33 +03002912 lq_sta->pers.drv = mvm;
2913#ifdef CONFIG_MAC80211_DEBUGFS
2914 lq_sta->pers.dbg_fixed_rate = 0;
2915 lq_sta->pers.dbg_fixed_txp_reduction = TPC_INVALID;
Eyal Shapira878985ce2015-02-02 12:14:54 +02002916 lq_sta->pers.ss_force = RS_SS_FORCE_NONE;
Eliad Pellerae969af2014-06-11 15:51:33 +03002917#endif
Eyal Shapira361dbec2014-08-13 00:31:13 +03002918 lq_sta->pers.chains = 0;
2919 memset(lq_sta->pers.chain_signal, 0, sizeof(lq_sta->pers.chain_signal));
Eyal Shapirab503ed62015-05-27 22:20:46 +03002920 lq_sta->pers.last_rssi = S8_MIN;
Eliad Pellerae969af2014-06-11 15:51:33 +03002921
Gregory Greenmanecaf71d2017-11-01 07:16:29 +02002922 return lq_sta;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002923}
2924
Eyal Shapirad310e402013-08-11 18:43:47 +03002925static int rs_vht_highest_rx_mcs_index(struct ieee80211_sta_vht_cap *vht_cap,
2926 int nss)
2927{
2928 u16 rx_mcs = le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map) &
2929 (0x3 << (2 * (nss - 1)));
2930 rx_mcs >>= (2 * (nss - 1));
2931
2932 if (rx_mcs == IEEE80211_VHT_MCS_SUPPORT_0_7)
2933 return IWL_RATE_MCS_7_INDEX;
2934 else if (rx_mcs == IEEE80211_VHT_MCS_SUPPORT_0_8)
2935 return IWL_RATE_MCS_8_INDEX;
2936 else if (rx_mcs == IEEE80211_VHT_MCS_SUPPORT_0_9)
2937 return IWL_RATE_MCS_9_INDEX;
2938
2939 WARN_ON_ONCE(rx_mcs != IEEE80211_VHT_MCS_NOT_SUPPORTED);
2940 return -1;
2941}
2942
Eyal Shapira750a1b42013-11-09 22:48:56 +02002943static void rs_vht_set_enabled_rates(struct ieee80211_sta *sta,
2944 struct ieee80211_sta_vht_cap *vht_cap,
2945 struct iwl_lq_sta *lq_sta)
2946{
2947 int i;
2948 int highest_mcs = rs_vht_highest_rx_mcs_index(vht_cap, 1);
2949
2950 if (highest_mcs >= IWL_RATE_MCS_0_INDEX) {
2951 for (i = IWL_RATE_MCS_0_INDEX; i <= highest_mcs; i++) {
2952 if (i == IWL_RATE_9M_INDEX)
2953 continue;
2954
Eyal Shapira271518a2013-11-09 23:25:06 +02002955 /* VHT MCS9 isn't valid for 20Mhz for NSS=1,2 */
2956 if (i == IWL_RATE_MCS_9_INDEX &&
2957 sta->bandwidth == IEEE80211_STA_RX_BW_20)
2958 continue;
2959
Eyal Shapira750a1b42013-11-09 22:48:56 +02002960 lq_sta->active_siso_rate |= BIT(i);
2961 }
2962 }
2963
Eyal Shapiraecc90e72013-11-09 23:31:38 +02002964 if (sta->rx_nss < 2)
2965 return;
2966
Eyal Shapira750a1b42013-11-09 22:48:56 +02002967 highest_mcs = rs_vht_highest_rx_mcs_index(vht_cap, 2);
2968 if (highest_mcs >= IWL_RATE_MCS_0_INDEX) {
2969 for (i = IWL_RATE_MCS_0_INDEX; i <= highest_mcs; i++) {
2970 if (i == IWL_RATE_9M_INDEX)
2971 continue;
2972
Eyal Shapira271518a2013-11-09 23:25:06 +02002973 /* VHT MCS9 isn't valid for 20Mhz for NSS=1,2 */
2974 if (i == IWL_RATE_MCS_9_INDEX &&
2975 sta->bandwidth == IEEE80211_STA_RX_BW_20)
2976 continue;
2977
Eyal Shapira750a1b42013-11-09 22:48:56 +02002978 lq_sta->active_mimo2_rate |= BIT(i);
2979 }
2980 }
2981}
2982
Johannes Berg2f15a822015-01-21 18:05:04 +01002983static void rs_ht_init(struct iwl_mvm *mvm,
2984 struct ieee80211_sta *sta,
2985 struct iwl_lq_sta *lq_sta,
2986 struct ieee80211_sta_ht_cap *ht_cap)
2987{
2988 /* active_siso_rate mask includes 9 MBits (bit 5),
2989 * and CCK (bits 0-3), supp_rates[] does not;
2990 * shift to convert format, force 9 MBits off.
2991 */
2992 lq_sta->active_siso_rate = ht_cap->mcs.rx_mask[0] << 1;
2993 lq_sta->active_siso_rate |= ht_cap->mcs.rx_mask[0] & 0x1;
2994 lq_sta->active_siso_rate &= ~((u16)0x2);
2995 lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE;
2996
2997 lq_sta->active_mimo2_rate = ht_cap->mcs.rx_mask[1] << 1;
2998 lq_sta->active_mimo2_rate |= ht_cap->mcs.rx_mask[1] & 0x1;
2999 lq_sta->active_mimo2_rate &= ~((u16)0x2);
3000 lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE;
3001
3002 if (mvm->cfg->ht_params->ldpc &&
3003 (ht_cap->cap & IEEE80211_HT_CAP_LDPC_CODING))
3004 lq_sta->ldpc = true;
3005
3006 if (mvm->cfg->ht_params->stbc &&
3007 (num_of_ant(iwl_mvm_get_valid_tx_ant(mvm)) > 1) &&
3008 (ht_cap->cap & IEEE80211_HT_CAP_RX_STBC))
Eyal Shapira3d44eeb2015-01-16 22:37:04 +02003009 lq_sta->stbc_capable = true;
Johannes Berg2f15a822015-01-21 18:05:04 +01003010
3011 lq_sta->is_vht = false;
3012}
3013
3014static void rs_vht_init(struct iwl_mvm *mvm,
3015 struct ieee80211_sta *sta,
3016 struct iwl_lq_sta *lq_sta,
3017 struct ieee80211_sta_vht_cap *vht_cap)
3018{
3019 rs_vht_set_enabled_rates(sta, vht_cap, lq_sta);
3020
3021 if (mvm->cfg->ht_params->ldpc &&
3022 (vht_cap->cap & IEEE80211_VHT_CAP_RXLDPC))
3023 lq_sta->ldpc = true;
3024
3025 if (mvm->cfg->ht_params->stbc &&
3026 (num_of_ant(iwl_mvm_get_valid_tx_ant(mvm)) > 1) &&
3027 (vht_cap->cap & IEEE80211_VHT_CAP_RXSTBC_MASK))
Eyal Shapira3d44eeb2015-01-16 22:37:04 +02003028 lq_sta->stbc_capable = true;
3029
Johannes Berg859d9142015-06-01 17:11:11 +02003030 if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_BEAMFORMER) &&
Eyal Shapira3d44eeb2015-01-16 22:37:04 +02003031 (num_of_ant(iwl_mvm_get_valid_tx_ant(mvm)) > 1) &&
3032 (vht_cap->cap & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE))
3033 lq_sta->bfer_capable = true;
Johannes Berg2f15a822015-01-21 18:05:04 +01003034
3035 lq_sta->is_vht = true;
3036}
3037
Eyal Shapira5fc0f762014-01-28 01:35:32 +02003038#ifdef CONFIG_IWLWIFI_DEBUGFS
Gregory Greenman9f66a392017-11-05 18:49:48 +02003039void iwl_mvm_reset_frame_stats(struct iwl_mvm *mvm)
Eyal Shapira5fc0f762014-01-28 01:35:32 +02003040{
3041 spin_lock_bh(&mvm->drv_stats_lock);
Johannes Berg2f15a822015-01-21 18:05:04 +01003042 memset(&mvm->drv_rx_stats, 0, sizeof(mvm->drv_rx_stats));
Eyal Shapira5fc0f762014-01-28 01:35:32 +02003043 spin_unlock_bh(&mvm->drv_stats_lock);
3044}
3045
Johannes Berg2f15a822015-01-21 18:05:04 +01003046void iwl_mvm_update_frame_stats(struct iwl_mvm *mvm, u32 rate, bool agg)
Eyal Shapira5fc0f762014-01-28 01:35:32 +02003047{
Kirtika Ruchandani9d504432016-11-08 21:50:33 -08003048 u8 nss = 0;
Eyal Shapira5fc0f762014-01-28 01:35:32 +02003049
3050 spin_lock(&mvm->drv_stats_lock);
3051
3052 if (agg)
Johannes Berg2f15a822015-01-21 18:05:04 +01003053 mvm->drv_rx_stats.agg_frames++;
Eyal Shapira5fc0f762014-01-28 01:35:32 +02003054
Johannes Berg2f15a822015-01-21 18:05:04 +01003055 mvm->drv_rx_stats.success_frames++;
Eyal Shapira5fc0f762014-01-28 01:35:32 +02003056
3057 switch (rate & RATE_MCS_CHAN_WIDTH_MSK) {
3058 case RATE_MCS_CHAN_WIDTH_20:
Johannes Berg2f15a822015-01-21 18:05:04 +01003059 mvm->drv_rx_stats.bw_20_frames++;
Eyal Shapira5fc0f762014-01-28 01:35:32 +02003060 break;
3061 case RATE_MCS_CHAN_WIDTH_40:
Johannes Berg2f15a822015-01-21 18:05:04 +01003062 mvm->drv_rx_stats.bw_40_frames++;
Eyal Shapira5fc0f762014-01-28 01:35:32 +02003063 break;
3064 case RATE_MCS_CHAN_WIDTH_80:
Johannes Berg2f15a822015-01-21 18:05:04 +01003065 mvm->drv_rx_stats.bw_80_frames++;
Eyal Shapira5fc0f762014-01-28 01:35:32 +02003066 break;
Gregory Greenman855f4922016-05-25 18:31:39 +03003067 case RATE_MCS_CHAN_WIDTH_160:
3068 mvm->drv_rx_stats.bw_160_frames++;
3069 break;
Eyal Shapira5fc0f762014-01-28 01:35:32 +02003070 default:
3071 WARN_ONCE(1, "bad BW. rate 0x%x", rate);
3072 }
3073
3074 if (rate & RATE_MCS_HT_MSK) {
Johannes Berg2f15a822015-01-21 18:05:04 +01003075 mvm->drv_rx_stats.ht_frames++;
Eyal Shapira5fc0f762014-01-28 01:35:32 +02003076 nss = ((rate & RATE_HT_MCS_NSS_MSK) >> RATE_HT_MCS_NSS_POS) + 1;
3077 } else if (rate & RATE_MCS_VHT_MSK) {
Johannes Berg2f15a822015-01-21 18:05:04 +01003078 mvm->drv_rx_stats.vht_frames++;
Eyal Shapira5fc0f762014-01-28 01:35:32 +02003079 nss = ((rate & RATE_VHT_MCS_NSS_MSK) >>
3080 RATE_VHT_MCS_NSS_POS) + 1;
3081 } else {
Johannes Berg2f15a822015-01-21 18:05:04 +01003082 mvm->drv_rx_stats.legacy_frames++;
Eyal Shapira5fc0f762014-01-28 01:35:32 +02003083 }
3084
3085 if (nss == 1)
Johannes Berg2f15a822015-01-21 18:05:04 +01003086 mvm->drv_rx_stats.siso_frames++;
Eyal Shapira5fc0f762014-01-28 01:35:32 +02003087 else if (nss == 2)
Johannes Berg2f15a822015-01-21 18:05:04 +01003088 mvm->drv_rx_stats.mimo2_frames++;
Eyal Shapira5fc0f762014-01-28 01:35:32 +02003089
3090 if (rate & RATE_MCS_SGI_MSK)
Johannes Berg2f15a822015-01-21 18:05:04 +01003091 mvm->drv_rx_stats.sgi_frames++;
Eyal Shapira5fc0f762014-01-28 01:35:32 +02003092 else
Johannes Berg2f15a822015-01-21 18:05:04 +01003093 mvm->drv_rx_stats.ngi_frames++;
Eyal Shapira5fc0f762014-01-28 01:35:32 +02003094
Johannes Berg2f15a822015-01-21 18:05:04 +01003095 mvm->drv_rx_stats.last_rates[mvm->drv_rx_stats.last_frame_idx] = rate;
3096 mvm->drv_rx_stats.last_frame_idx =
3097 (mvm->drv_rx_stats.last_frame_idx + 1) %
3098 ARRAY_SIZE(mvm->drv_rx_stats.last_rates);
Eyal Shapira5fc0f762014-01-28 01:35:32 +02003099
3100 spin_unlock(&mvm->drv_stats_lock);
3101}
3102#endif
3103
Johannes Berg8ca151b2013-01-24 14:25:36 +01003104/*
3105 * Called after adding a new station to initialize rate scaling
3106 */
Gregory Greenman9f66a392017-11-05 18:49:48 +02003107static void rs_drv_rate_init(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
3108 enum nl80211_band band, bool init)
Johannes Berg8ca151b2013-01-24 14:25:36 +01003109{
3110 int i, j;
3111 struct ieee80211_hw *hw = mvm->hw;
3112 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Eyal Shapirad310e402013-08-11 18:43:47 +03003113 struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
Luca Coelho7ccb4982017-08-01 09:01:34 +03003114 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Gregory Greenmanecaf71d2017-11-01 07:16:29 +02003115 struct iwl_lq_sta *lq_sta = &mvmsta->lq_sta.rs_drv;
Johannes Berg8ca151b2013-01-24 14:25:36 +01003116 struct ieee80211_supported_band *sband;
3117 unsigned long supp; /* must be unsigned long for for_each_set_bit */
3118
Eliad Pellerae969af2014-06-11 15:51:33 +03003119 /* clear all non-persistent lq data */
3120 memset(lq_sta, 0, offsetof(typeof(*lq_sta), pers));
Eyal Shapirab87c2172013-11-09 23:37:55 +02003121
Johannes Berg8ca151b2013-01-24 14:25:36 +01003122 sband = hw->wiphy->bands[band];
3123
Luca Coelho7ccb4982017-08-01 09:01:34 +03003124 lq_sta->lq.sta_id = mvmsta->sta_id;
3125 mvmsta->tlc_amsdu = false;
Johannes Berg8ca151b2013-01-24 14:25:36 +01003126
3127 for (j = 0; j < LQ_SIZE; j++)
Eyal Shapirab804eeb2014-04-06 04:27:06 +03003128 rs_rate_scale_clear_tbl_windows(mvm, &lq_sta->lq_info[j]);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003129
3130 lq_sta->flush_timer = 0;
Eyal Shapira87d5e412014-04-06 18:13:22 +03003131 lq_sta->last_tx = jiffies;
Johannes Berg8ca151b2013-01-24 14:25:36 +01003132
3133 IWL_DEBUG_RATE(mvm,
3134 "LQ: *** rate scale station global init for station %d ***\n",
Luca Coelho7ccb4982017-08-01 09:01:34 +03003135 mvmsta->sta_id);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003136 /* TODO: what is a good starting rate for STA? About middle? Maybe not
3137 * the lowest or the highest rate.. Could consider using RSSI from
3138 * previous packets? Need to have IEEE 802.1X auth succeed immediately
3139 * after assoc.. */
3140
Eyal Shapira834437d2014-12-17 15:38:58 +02003141 lq_sta->missed_rate_counter = IWL_MVM_RS_MISSED_RATE_MAX;
Johannes Berg8ca151b2013-01-24 14:25:36 +01003142 lq_sta->band = sband->band;
3143 /*
3144 * active legacy rates as per supported rates bitmap
3145 */
3146 supp = sta->supp_rates[sband->band];
3147 lq_sta->active_legacy_rate = 0;
3148 for_each_set_bit(i, &supp, BITS_PER_LONG)
3149 lq_sta->active_legacy_rate |= BIT(sband->bitrates[i].hw_value);
3150
Eyal Shapirad310e402013-08-11 18:43:47 +03003151 /* TODO: should probably account for rx_highest for both HT/VHT */
Eyal Shapira2c59d802015-01-16 11:01:51 +02003152 if (!vht_cap || !vht_cap->vht_supported)
3153 rs_ht_init(mvm, sta, lq_sta, ht_cap);
3154 else
3155 rs_vht_init(mvm, sta, lq_sta, vht_cap);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003156
Eyal Shapira6a028d92014-12-07 18:18:27 +02003157 lq_sta->max_legacy_rate_idx =
3158 rs_get_max_rate_from_mask(lq_sta->active_legacy_rate);
3159 lq_sta->max_siso_rate_idx =
3160 rs_get_max_rate_from_mask(lq_sta->active_siso_rate);
3161 lq_sta->max_mimo2_rate_idx =
3162 rs_get_max_rate_from_mask(lq_sta->active_mimo2_rate);
Eyal Shapira198266a32014-03-31 22:37:39 +03003163
Eyal Shapiraa3576ff2014-08-09 10:57:59 +03003164 IWL_DEBUG_RATE(mvm,
Eyal Shapira3d44eeb2015-01-16 22:37:04 +02003165 "LEGACY=%lX SISO=%lX MIMO2=%lX VHT=%d LDPC=%d STBC=%d BFER=%d\n",
Eyal Shapira198266a32014-03-31 22:37:39 +03003166 lq_sta->active_legacy_rate,
Johannes Berg8ca151b2013-01-24 14:25:36 +01003167 lq_sta->active_siso_rate,
Eyal Shapirad310e402013-08-11 18:43:47 +03003168 lq_sta->active_mimo2_rate,
Eyal Shapira3d44eeb2015-01-16 22:37:04 +02003169 lq_sta->is_vht, lq_sta->ldpc, lq_sta->stbc_capable,
3170 lq_sta->bfer_capable);
Eyal Shapira198266a32014-03-31 22:37:39 +03003171 IWL_DEBUG_RATE(mvm, "MAX RATE: LEGACY=%d SISO=%d MIMO2=%d\n",
3172 lq_sta->max_legacy_rate_idx,
3173 lq_sta->max_siso_rate_idx,
3174 lq_sta->max_mimo2_rate_idx);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003175
3176 /* These values will be overridden later */
3177 lq_sta->lq.single_stream_ant_msk =
Moshe Harela0544272014-12-08 21:13:14 +02003178 first_antenna(iwl_mvm_get_valid_tx_ant(mvm));
Eyal Shapira809bccf2013-11-28 02:25:24 +02003179 lq_sta->lq.dual_stream_ant_msk = ANT_AB;
Johannes Berg8ca151b2013-01-24 14:25:36 +01003180
3181 /* as default allow aggregation for all tids */
3182 lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
Johannes Berg8ca151b2013-01-24 14:25:36 +01003183 lq_sta->is_agg = 0;
Eyal Shapira5fc0f762014-01-28 01:35:32 +02003184#ifdef CONFIG_IWLWIFI_DEBUGFS
Johannes Berg2f15a822015-01-21 18:05:04 +01003185 iwl_mvm_reset_frame_stats(mvm);
Eyal Shapira5fc0f762014-01-28 01:35:32 +02003186#endif
Eyal Shapirab87c2172013-11-09 23:37:55 +02003187 rs_initialize_lq(mvm, sta, lq_sta, band, init);
3188}
3189
Gregory Greenman9f66a392017-11-05 18:49:48 +02003190static void rs_drv_rate_update(void *mvm_r,
3191 struct ieee80211_supported_band *sband,
3192 struct cfg80211_chan_def *chandef,
3193 struct ieee80211_sta *sta,
3194 void *priv_sta, u32 changed)
Eyal Shapirab87c2172013-11-09 23:37:55 +02003195{
Gregory Greenman9f66a392017-11-05 18:49:48 +02003196 struct iwl_op_mode *op_mode = mvm_r;
3197 struct iwl_mvm *mvm __maybe_unused = IWL_OP_MODE_GET_MVM(op_mode);
Eyal Shapirab87c2172013-11-09 23:37:55 +02003198 u8 tid;
Eyal Shapirab87c2172013-11-09 23:37:55 +02003199
Johannes Bergdcbc17a2015-03-10 21:24:34 +01003200 if (!iwl_mvm_sta_from_mac80211(sta)->vif)
3201 return;
3202
Eyal Shapirab87c2172013-11-09 23:37:55 +02003203 /* Stop any ongoing aggregations as rs starts off assuming no agg */
3204 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++)
3205 ieee80211_stop_tx_ba_session(sta, tid);
3206
3207 iwl_mvm_rs_rate_init(mvm, sta, sband->band, false);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003208}
3209
Eyal Shapira8fc7c582013-12-04 02:15:46 +02003210#ifdef CONFIG_MAC80211_DEBUGFS
3211static void rs_build_rates_table_from_fixed(struct iwl_mvm *mvm,
3212 struct iwl_lq_cmd *lq_cmd,
Johannes Berg57fbcce2016-04-12 15:56:15 +02003213 enum nl80211_band band,
Eyal Shapira8fc7c582013-12-04 02:15:46 +02003214 u32 ucode_rate)
3215{
3216 struct rs_rate rate;
3217 int i;
3218 int num_rates = ARRAY_SIZE(lq_cmd->rs_table);
3219 __le32 ucode_rate_le32 = cpu_to_le32(ucode_rate);
Emmanuel Grumbach7ceb98a2014-07-28 12:59:45 +03003220 u8 ant = (ucode_rate & RATE_MCS_ANT_ABC_MSK) >> RATE_MCS_ANT_POS;
Eyal Shapira8fc7c582013-12-04 02:15:46 +02003221
3222 for (i = 0; i < num_rates; i++)
3223 lq_cmd->rs_table[i] = ucode_rate_le32;
3224
3225 rs_rate_from_ucode_rate(ucode_rate, band, &rate);
3226
3227 if (is_mimo(&rate))
3228 lq_cmd->mimo_delim = num_rates - 1;
3229 else
3230 lq_cmd->mimo_delim = 0;
Emmanuel Grumbach7ceb98a2014-07-28 12:59:45 +03003231
3232 lq_cmd->reduced_tpc = 0;
3233
3234 if (num_of_ant(ant) == 1)
3235 lq_cmd->single_stream_ant_msk = ant;
3236
Liad Kaufmana58bb462017-05-28 14:20:04 +03003237 if (!mvm->trans->cfg->gen2)
3238 lq_cmd->agg_frame_cnt_limit = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
3239 else
3240 lq_cmd->agg_frame_cnt_limit =
3241 LINK_QUAL_AGG_FRAME_LIMIT_GEN2_DEF;
Eyal Shapira8fc7c582013-12-04 02:15:46 +02003242}
3243#endif /* CONFIG_MAC80211_DEBUGFS */
3244
Eyal Shapirae07be6d2013-12-09 13:02:56 +02003245static void rs_fill_rates_for_column(struct iwl_mvm *mvm,
3246 struct iwl_lq_sta *lq_sta,
3247 struct rs_rate *rate,
3248 __le32 *rs_table, int *rs_table_index,
3249 int num_rates, int num_retries,
3250 u8 valid_tx_ant, bool toggle_ant)
3251{
3252 int i, j;
3253 __le32 ucode_rate;
3254 bool bottom_reached = false;
3255 int prev_rate_idx = rate->index;
3256 int end = LINK_QUAL_MAX_RETRY_NUM;
3257 int index = *rs_table_index;
3258
3259 for (i = 0; i < num_rates && index < end; i++) {
Eyal Shapirabd999312014-12-28 22:12:38 +02003260 for (j = 0; j < num_retries && index < end; j++, index++) {
3261 ucode_rate = cpu_to_le32(ucode_rate_from_rs_rate(mvm,
3262 rate));
Eyal Shapirae07be6d2013-12-09 13:02:56 +02003263 rs_table[index] = ucode_rate;
Eyal Shapirabd999312014-12-28 22:12:38 +02003264 if (toggle_ant)
3265 rs_toggle_antenna(valid_tx_ant, rate);
3266 }
Eyal Shapirae07be6d2013-12-09 13:02:56 +02003267
3268 prev_rate_idx = rate->index;
3269 bottom_reached = rs_get_lower_rate_in_column(lq_sta, rate);
3270 if (bottom_reached && !is_legacy(rate))
3271 break;
3272 }
3273
Eyal Shapirabd999312014-12-28 22:12:38 +02003274 if (!bottom_reached && !is_legacy(rate))
Eyal Shapirae07be6d2013-12-09 13:02:56 +02003275 rate->index = prev_rate_idx;
3276
3277 *rs_table_index = index;
3278}
3279
3280/* Building the rate table is non trivial. When we're in MIMO2/VHT/80Mhz/SGI
3281 * column the rate table should look like this:
3282 *
3283 * rate[0] 0x400D019 VHT | ANT: AB BW: 80Mhz MCS: 9 NSS: 2 SGI
3284 * rate[1] 0x400D019 VHT | ANT: AB BW: 80Mhz MCS: 9 NSS: 2 SGI
3285 * rate[2] 0x400D018 VHT | ANT: AB BW: 80Mhz MCS: 8 NSS: 2 SGI
3286 * rate[3] 0x400D018 VHT | ANT: AB BW: 80Mhz MCS: 8 NSS: 2 SGI
3287 * rate[4] 0x400D017 VHT | ANT: AB BW: 80Mhz MCS: 7 NSS: 2 SGI
3288 * rate[5] 0x400D017 VHT | ANT: AB BW: 80Mhz MCS: 7 NSS: 2 SGI
3289 * rate[6] 0x4005007 VHT | ANT: A BW: 80Mhz MCS: 7 NSS: 1 NGI
3290 * rate[7] 0x4009006 VHT | ANT: B BW: 80Mhz MCS: 6 NSS: 1 NGI
3291 * rate[8] 0x4005005 VHT | ANT: A BW: 80Mhz MCS: 5 NSS: 1 NGI
3292 * rate[9] 0x800B Legacy | ANT: B Rate: 36 Mbps
3293 * rate[10] 0x4009 Legacy | ANT: A Rate: 24 Mbps
3294 * rate[11] 0x8007 Legacy | ANT: B Rate: 18 Mbps
3295 * rate[12] 0x4005 Legacy | ANT: A Rate: 12 Mbps
3296 * rate[13] 0x800F Legacy | ANT: B Rate: 9 Mbps
3297 * rate[14] 0x400D Legacy | ANT: A Rate: 6 Mbps
3298 * rate[15] 0x800D Legacy | ANT: B Rate: 6 Mbps
3299 */
Eyal Shapira8fc7c582013-12-04 02:15:46 +02003300static void rs_build_rates_table(struct iwl_mvm *mvm,
Eyal Shapirad8bafea2014-10-23 13:58:38 +03003301 struct ieee80211_sta *sta,
Eyal Shapira8fc7c582013-12-04 02:15:46 +02003302 struct iwl_lq_sta *lq_sta,
3303 const struct rs_rate *initial_rate)
Johannes Berg8ca151b2013-01-24 14:25:36 +01003304{
Eyal Shapira5aa33552013-11-23 01:06:36 +02003305 struct rs_rate rate;
Eyal Shapirae07be6d2013-12-09 13:02:56 +02003306 int num_rates, num_retries, index = 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +01003307 u8 valid_tx_ant = 0;
3308 struct iwl_lq_cmd *lq_cmd = &lq_sta->lq;
Eyal Shapirae07be6d2013-12-09 13:02:56 +02003309 bool toggle_ant = false;
Gregory Greenmanea42d1c2017-03-06 11:15:41 +02003310 u32 color;
Johannes Berg8ca151b2013-01-24 14:25:36 +01003311
Emmanuel Grumbach9e898f12013-12-22 10:55:47 +02003312 memcpy(&rate, initial_rate, sizeof(rate));
Johannes Berg8ca151b2013-01-24 14:25:36 +01003313
Moshe Harela0544272014-12-08 21:13:14 +02003314 valid_tx_ant = iwl_mvm_get_valid_tx_ant(mvm);
Eyal Shapirac5d679a2015-01-15 20:22:34 +02003315
Eyal Shapira3d44eeb2015-01-16 22:37:04 +02003316 /* TODO: remove old API when min FW API hits 14 */
Johannes Berg859d9142015-06-01 17:11:11 +02003317 if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_LQ_SS_PARAMS) &&
Eyal Shapira3d44eeb2015-01-16 22:37:04 +02003318 rs_stbc_allow(mvm, sta, lq_sta))
3319 rate.stbc = true;
Johannes Berg8ca151b2013-01-24 14:25:36 +01003320
Eyal Shapirae07be6d2013-12-09 13:02:56 +02003321 if (is_siso(&rate)) {
Eyal Shapira834437d2014-12-17 15:38:58 +02003322 num_rates = IWL_MVM_RS_INITIAL_SISO_NUM_RATES;
3323 num_retries = IWL_MVM_RS_HT_VHT_RETRIES_PER_RATE;
Eyal Shapirae07be6d2013-12-09 13:02:56 +02003324 } else if (is_mimo(&rate)) {
Eyal Shapira834437d2014-12-17 15:38:58 +02003325 num_rates = IWL_MVM_RS_INITIAL_MIMO_NUM_RATES;
3326 num_retries = IWL_MVM_RS_HT_VHT_RETRIES_PER_RATE;
Eyal Shapirae07be6d2013-12-09 13:02:56 +02003327 } else {
Eyal Shapira834437d2014-12-17 15:38:58 +02003328 num_rates = IWL_MVM_RS_INITIAL_LEGACY_NUM_RATES;
Eyal Shapirabd999312014-12-28 22:12:38 +02003329 num_retries = IWL_MVM_RS_INITIAL_LEGACY_RETRIES;
Eyal Shapirae07be6d2013-12-09 13:02:56 +02003330 toggle_ant = true;
Johannes Berg8ca151b2013-01-24 14:25:36 +01003331 }
Eyal Shapirae07be6d2013-12-09 13:02:56 +02003332
3333 rs_fill_rates_for_column(mvm, lq_sta, &rate, lq_cmd->rs_table, &index,
3334 num_rates, num_retries, valid_tx_ant,
3335 toggle_ant);
3336
3337 rs_get_lower_rate_down_column(lq_sta, &rate);
3338
3339 if (is_siso(&rate)) {
Eyal Shapira834437d2014-12-17 15:38:58 +02003340 num_rates = IWL_MVM_RS_SECONDARY_SISO_NUM_RATES;
3341 num_retries = IWL_MVM_RS_SECONDARY_SISO_RETRIES;
Eyal Shapirad9088f62014-03-25 10:25:44 +02003342 lq_cmd->mimo_delim = index;
Eyal Shapirae07be6d2013-12-09 13:02:56 +02003343 } else if (is_legacy(&rate)) {
Eyal Shapira834437d2014-12-17 15:38:58 +02003344 num_rates = IWL_MVM_RS_SECONDARY_LEGACY_NUM_RATES;
Eyal Shapirabd999312014-12-28 22:12:38 +02003345 num_retries = IWL_MVM_RS_SECONDARY_LEGACY_RETRIES;
Eyal Shapirae07be6d2013-12-09 13:02:56 +02003346 } else {
3347 WARN_ON_ONCE(1);
3348 }
3349
3350 toggle_ant = true;
3351
3352 rs_fill_rates_for_column(mvm, lq_sta, &rate, lq_cmd->rs_table, &index,
3353 num_rates, num_retries, valid_tx_ant,
3354 toggle_ant);
3355
3356 rs_get_lower_rate_down_column(lq_sta, &rate);
3357
Eyal Shapira834437d2014-12-17 15:38:58 +02003358 num_rates = IWL_MVM_RS_SECONDARY_LEGACY_NUM_RATES;
Eyal Shapirabd999312014-12-28 22:12:38 +02003359 num_retries = IWL_MVM_RS_SECONDARY_LEGACY_RETRIES;
Eyal Shapirae07be6d2013-12-09 13:02:56 +02003360
3361 rs_fill_rates_for_column(mvm, lq_sta, &rate, lq_cmd->rs_table, &index,
3362 num_rates, num_retries, valid_tx_ant,
3363 toggle_ant);
3364
Gregory Greenmanea42d1c2017-03-06 11:15:41 +02003365 /* update the color of the LQ command (as a counter at bits 1-3) */
3366 color = LQ_FLAGS_COLOR_INC(LQ_FLAG_COLOR_GET(lq_cmd->flags));
3367 lq_cmd->flags = LQ_FLAG_COLOR_SET(lq_cmd->flags, color);
Eyal Shapira8fc7c582013-12-04 02:15:46 +02003368}
3369
Eyal Shapira3d44eeb2015-01-16 22:37:04 +02003370struct rs_bfer_active_iter_data {
3371 struct ieee80211_sta *exclude_sta;
3372 struct iwl_mvm_sta *bfer_mvmsta;
3373};
3374
3375static void rs_bfer_active_iter(void *_data,
3376 struct ieee80211_sta *sta)
3377{
3378 struct rs_bfer_active_iter_data *data = _data;
3379 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Gregory Greenmanecaf71d2017-11-01 07:16:29 +02003380 struct iwl_lq_cmd *lq_cmd = &mvmsta->lq_sta.rs_drv.lq;
Eyal Shapira3d44eeb2015-01-16 22:37:04 +02003381 u32 ss_params = le32_to_cpu(lq_cmd->ss_params);
3382
3383 if (sta == data->exclude_sta)
3384 return;
3385
3386 /* The current sta has BFER allowed */
3387 if (ss_params & LQ_SS_BFER_ALLOWED) {
3388 WARN_ON_ONCE(data->bfer_mvmsta != NULL);
3389
3390 data->bfer_mvmsta = mvmsta;
3391 }
3392}
3393
3394static int rs_bfer_priority(struct iwl_mvm_sta *sta)
3395{
3396 int prio = -1;
3397 enum nl80211_iftype viftype = ieee80211_vif_type_p2p(sta->vif);
3398
3399 switch (viftype) {
3400 case NL80211_IFTYPE_AP:
3401 case NL80211_IFTYPE_P2P_GO:
3402 prio = 3;
3403 break;
3404 case NL80211_IFTYPE_P2P_CLIENT:
3405 prio = 2;
3406 break;
3407 case NL80211_IFTYPE_STATION:
3408 prio = 1;
3409 break;
3410 default:
3411 WARN_ONCE(true, "viftype %d sta_id %d", viftype, sta->sta_id);
3412 prio = -1;
3413 }
3414
3415 return prio;
3416}
3417
3418/* Returns >0 if sta1 has a higher BFER priority compared to sta2 */
3419static int rs_bfer_priority_cmp(struct iwl_mvm_sta *sta1,
3420 struct iwl_mvm_sta *sta2)
3421{
3422 int prio1 = rs_bfer_priority(sta1);
3423 int prio2 = rs_bfer_priority(sta2);
3424
3425 if (prio1 > prio2)
3426 return 1;
3427 if (prio1 < prio2)
3428 return -1;
3429 return 0;
3430}
3431
3432static void rs_set_lq_ss_params(struct iwl_mvm *mvm,
3433 struct ieee80211_sta *sta,
3434 struct iwl_lq_sta *lq_sta,
3435 const struct rs_rate *initial_rate)
3436{
3437 struct iwl_lq_cmd *lq_cmd = &lq_sta->lq;
3438 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3439 struct rs_bfer_active_iter_data data = {
3440 .exclude_sta = sta,
3441 .bfer_mvmsta = NULL,
3442 };
3443 struct iwl_mvm_sta *bfer_mvmsta = NULL;
3444 u32 ss_params = LQ_SS_PARAMS_VALID;
3445
3446 if (!iwl_mvm_bt_coex_is_mimo_allowed(mvm, sta))
3447 goto out;
3448
Eyal Shapira878985ce2015-02-02 12:14:54 +02003449#ifdef CONFIG_MAC80211_DEBUGFS
Eyal Shapira1e9c62f2015-01-28 14:44:06 +02003450 /* Check if forcing the decision is configured.
3451 * Note that SISO is forced by not allowing STBC or BFER
3452 */
Eyal Shapira878985ce2015-02-02 12:14:54 +02003453 if (lq_sta->pers.ss_force == RS_SS_FORCE_STBC)
Eyal Shapira1e9c62f2015-01-28 14:44:06 +02003454 ss_params |= (LQ_SS_STBC_1SS_ALLOWED | LQ_SS_FORCE);
Eyal Shapira878985ce2015-02-02 12:14:54 +02003455 else if (lq_sta->pers.ss_force == RS_SS_FORCE_BFER)
Eyal Shapira1e9c62f2015-01-28 14:44:06 +02003456 ss_params |= (LQ_SS_BFER_ALLOWED | LQ_SS_FORCE);
3457
Eyal Shapira878985ce2015-02-02 12:14:54 +02003458 if (lq_sta->pers.ss_force != RS_SS_FORCE_NONE) {
Eyal Shapira1e9c62f2015-01-28 14:44:06 +02003459 IWL_DEBUG_RATE(mvm, "Forcing single stream Tx decision %d\n",
Eyal Shapira878985ce2015-02-02 12:14:54 +02003460 lq_sta->pers.ss_force);
Eyal Shapira1e9c62f2015-01-28 14:44:06 +02003461 goto out;
3462 }
Eyal Shapira878985ce2015-02-02 12:14:54 +02003463#endif
Eyal Shapira1e9c62f2015-01-28 14:44:06 +02003464
Eyal Shapira3d44eeb2015-01-16 22:37:04 +02003465 if (lq_sta->stbc_capable)
3466 ss_params |= LQ_SS_STBC_1SS_ALLOWED;
3467
3468 if (!lq_sta->bfer_capable)
3469 goto out;
3470
3471 ieee80211_iterate_stations_atomic(mvm->hw,
3472 rs_bfer_active_iter,
3473 &data);
3474 bfer_mvmsta = data.bfer_mvmsta;
3475
3476 /* This code is safe as it doesn't run concurrently for different
3477 * stations. This is guaranteed by the fact that calls to
3478 * ieee80211_tx_status wouldn't run concurrently for a single HW.
3479 */
3480 if (!bfer_mvmsta) {
3481 IWL_DEBUG_RATE(mvm, "No sta with BFER allowed found. Allow\n");
3482
3483 ss_params |= LQ_SS_BFER_ALLOWED;
3484 goto out;
3485 }
3486
3487 IWL_DEBUG_RATE(mvm, "Found existing sta %d with BFER activated\n",
3488 bfer_mvmsta->sta_id);
3489
3490 /* Disallow BFER on another STA if active and we're a higher priority */
3491 if (rs_bfer_priority_cmp(mvmsta, bfer_mvmsta) > 0) {
Gregory Greenmanecaf71d2017-11-01 07:16:29 +02003492 struct iwl_lq_cmd *bfersta_lq_cmd =
3493 &bfer_mvmsta->lq_sta.rs_drv.lq;
Eyal Shapira3d44eeb2015-01-16 22:37:04 +02003494 u32 bfersta_ss_params = le32_to_cpu(bfersta_lq_cmd->ss_params);
3495
3496 bfersta_ss_params &= ~LQ_SS_BFER_ALLOWED;
3497 bfersta_lq_cmd->ss_params = cpu_to_le32(bfersta_ss_params);
3498 iwl_mvm_send_lq_cmd(mvm, bfersta_lq_cmd, false);
3499
3500 ss_params |= LQ_SS_BFER_ALLOWED;
3501 IWL_DEBUG_RATE(mvm,
3502 "Lower priority BFER sta found (%d). Switch BFER\n",
3503 bfer_mvmsta->sta_id);
3504 }
3505out:
3506 lq_cmd->ss_params = cpu_to_le32(ss_params);
3507}
3508
Eyal Shapira8fc7c582013-12-04 02:15:46 +02003509static void rs_fill_lq_cmd(struct iwl_mvm *mvm,
3510 struct ieee80211_sta *sta,
3511 struct iwl_lq_sta *lq_sta,
3512 const struct rs_rate *initial_rate)
3513{
3514 struct iwl_lq_cmd *lq_cmd = &lq_sta->lq;
Emmanuel Grumbach7ceb98a2014-07-28 12:59:45 +03003515 struct iwl_mvm_sta *mvmsta;
3516 struct iwl_mvm_vif *mvmvif;
3517
Eyal Shapira834437d2014-12-17 15:38:58 +02003518 lq_cmd->agg_disable_start_th = IWL_MVM_RS_AGG_DISABLE_START;
Emmanuel Grumbach7ceb98a2014-07-28 12:59:45 +03003519 lq_cmd->agg_time_limit =
Eyal Shapira834437d2014-12-17 15:38:58 +02003520 cpu_to_le16(IWL_MVM_RS_AGG_TIME_LIMIT);
Eyal Shapira8fc7c582013-12-04 02:15:46 +02003521
3522#ifdef CONFIG_MAC80211_DEBUGFS
Eliad Pellerae969af2014-06-11 15:51:33 +03003523 if (lq_sta->pers.dbg_fixed_rate) {
Eyal Shapira8fc7c582013-12-04 02:15:46 +02003524 rs_build_rates_table_from_fixed(mvm, lq_cmd,
3525 lq_sta->band,
Eliad Pellerae969af2014-06-11 15:51:33 +03003526 lq_sta->pers.dbg_fixed_rate);
Emmanuel Grumbach7ceb98a2014-07-28 12:59:45 +03003527 return;
3528 }
Eyal Shapira8fc7c582013-12-04 02:15:46 +02003529#endif
Emmanuel Grumbach7ceb98a2014-07-28 12:59:45 +03003530 if (WARN_ON_ONCE(!sta || !initial_rate))
3531 return;
Eyal Shapira8fc7c582013-12-04 02:15:46 +02003532
Eyal Shapirad8bafea2014-10-23 13:58:38 +03003533 rs_build_rates_table(mvm, sta, lq_sta, initial_rate);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003534
Johannes Berg859d9142015-06-01 17:11:11 +02003535 if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_LQ_SS_PARAMS))
Eyal Shapira3d44eeb2015-01-16 22:37:04 +02003536 rs_set_lq_ss_params(mvm, sta, lq_sta, initial_rate);
3537
Emmanuel Grumbach7ceb98a2014-07-28 12:59:45 +03003538 mvmsta = iwl_mvm_sta_from_mac80211(sta);
3539 mvmvif = iwl_mvm_vif_from_mac80211(mvmsta->vif);
3540
3541 if (num_of_ant(initial_rate->ant) == 1)
3542 lq_cmd->single_stream_ant_msk = initial_rate->ant;
3543
3544 lq_cmd->agg_frame_cnt_limit = mvmsta->max_agg_bufsize;
3545
3546 /*
Sara Sharon0d365ae2015-03-31 12:24:05 +03003547 * In case of low latency, tell the firmware to leave a frame in the
Emmanuel Grumbach7ceb98a2014-07-28 12:59:45 +03003548 * Tx Fifo so that it can start a transaction in the same TxOP. This
3549 * basically allows the firmware to send bursts.
3550 */
Eyal Shapira1412ee32015-10-22 13:10:36 +03003551 if (iwl_mvm_vif_low_latency(mvmvif))
Emmanuel Grumbach7ceb98a2014-07-28 12:59:45 +03003552 lq_cmd->agg_frame_cnt_limit--;
Johannes Berg8ca151b2013-01-24 14:25:36 +01003553
Emmanuel Grumbacha4db8482014-07-31 13:58:53 +03003554 if (mvmsta->vif->p2p)
3555 lq_cmd->flags |= LQ_FLAG_USE_RTS_MSK;
3556
Johannes Berg8ca151b2013-01-24 14:25:36 +01003557 lq_cmd->agg_time_limit =
Emmanuel Grumbach5b7ff612014-03-11 19:27:45 +02003558 cpu_to_le16(iwl_mvm_coex_agg_time_limit(mvm, sta));
Johannes Berg8ca151b2013-01-24 14:25:36 +01003559}
3560
3561static void *rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
3562{
3563 return hw->priv;
3564}
Gregory Greenman9f66a392017-11-05 18:49:48 +02003565
Johannes Berg8ca151b2013-01-24 14:25:36 +01003566/* rate scale requires free function to be implemented */
3567static void rs_free(void *mvm_rate)
3568{
3569 return;
3570}
3571
Gregory Greenman9f66a392017-11-05 18:49:48 +02003572static void rs_free_sta(void *mvm_r, struct ieee80211_sta *sta, void *mvm_sta)
Johannes Berg8ca151b2013-01-24 14:25:36 +01003573{
3574 struct iwl_op_mode *op_mode __maybe_unused = mvm_r;
3575 struct iwl_mvm *mvm __maybe_unused = IWL_OP_MODE_GET_MVM(op_mode);
3576
3577 IWL_DEBUG_RATE(mvm, "enter\n");
3578 IWL_DEBUG_RATE(mvm, "leave\n");
3579}
3580
3581#ifdef CONFIG_MAC80211_DEBUGFS
Gregory Greenmanf6f046f2017-11-26 15:51:31 +02003582int rs_pretty_print_rate(char *buf, int bufsz, const u32 rate)
Eyal Shapira5aa33552013-11-23 01:06:36 +02003583{
3584
Eyal Shapira1a61b342013-11-09 02:11:46 +02003585 char *type, *bw;
3586 u8 mcs = 0, nss = 0;
3587 u8 ant = (rate & RATE_MCS_ANT_ABC_MSK) >> RATE_MCS_ANT_POS;
3588
3589 if (!(rate & RATE_MCS_HT_MSK) &&
3590 !(rate & RATE_MCS_VHT_MSK)) {
3591 int index = iwl_hwrate_to_plcp_idx(rate);
3592
Gregory Greenmanf6f046f2017-11-26 15:51:31 +02003593 return scnprintf(buf, bufsz, "Legacy | ANT: %s Rate: %s Mbps\n",
3594 rs_pretty_ant(ant),
3595 index == IWL_RATE_INVALID ? "BAD" :
3596 iwl_rate_mcs[index].mbps);
Eyal Shapira1a61b342013-11-09 02:11:46 +02003597 }
3598
3599 if (rate & RATE_MCS_VHT_MSK) {
3600 type = "VHT";
3601 mcs = rate & RATE_VHT_MCS_RATE_CODE_MSK;
3602 nss = ((rate & RATE_VHT_MCS_NSS_MSK)
3603 >> RATE_VHT_MCS_NSS_POS) + 1;
3604 } else if (rate & RATE_MCS_HT_MSK) {
3605 type = "HT";
3606 mcs = rate & RATE_HT_MCS_INDEX_MSK;
Gregory Greenman3374c3a2016-12-21 16:18:44 +02003607 nss = ((rate & RATE_HT_MCS_NSS_MSK)
3608 >> RATE_HT_MCS_NSS_POS) + 1;
Eyal Shapira1a61b342013-11-09 02:11:46 +02003609 } else {
3610 type = "Unknown"; /* shouldn't happen */
3611 }
3612
3613 switch (rate & RATE_MCS_CHAN_WIDTH_MSK) {
3614 case RATE_MCS_CHAN_WIDTH_20:
3615 bw = "20Mhz";
3616 break;
3617 case RATE_MCS_CHAN_WIDTH_40:
3618 bw = "40Mhz";
3619 break;
3620 case RATE_MCS_CHAN_WIDTH_80:
3621 bw = "80Mhz";
3622 break;
3623 case RATE_MCS_CHAN_WIDTH_160:
3624 bw = "160Mhz";
3625 break;
3626 default:
3627 bw = "BAD BW";
3628 }
3629
Gregory Greenmanf6f046f2017-11-26 15:51:31 +02003630 return scnprintf(buf, bufsz,
3631 "%s | ANT: %s BW: %s MCS: %d NSS: %d %s%s%s%s\n",
3632 type, rs_pretty_ant(ant), bw, mcs, nss,
3633 (rate & RATE_MCS_SGI_MSK) ? "SGI " : "NGI ",
3634 (rate & RATE_MCS_STBC_MSK) ? "STBC " : "",
3635 (rate & RATE_MCS_LDPC_MSK) ? "LDPC " : "",
3636 (rate & RATE_MCS_BF_MSK) ? "BF " : "");
Eyal Shapira1a61b342013-11-09 02:11:46 +02003637}
3638
Eyal Shapiraa0c38b22013-12-03 18:34:52 +02003639/**
3640 * Program the device to use fixed rate for frame transmit
3641 * This is for debugging/testing only
3642 * once the device start use fixed rate, we need to reload the module
3643 * to being back the normal operation.
3644 */
3645static void rs_program_fix_rate(struct iwl_mvm *mvm,
3646 struct iwl_lq_sta *lq_sta)
3647{
3648 lq_sta->active_legacy_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
3649 lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
3650 lq_sta->active_mimo2_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
3651
3652 IWL_DEBUG_RATE(mvm, "sta_id %d rate 0x%X\n",
Eliad Pellerae969af2014-06-11 15:51:33 +03003653 lq_sta->lq.sta_id, lq_sta->pers.dbg_fixed_rate);
Eyal Shapiraa0c38b22013-12-03 18:34:52 +02003654
Eliad Pellerae969af2014-06-11 15:51:33 +03003655 if (lq_sta->pers.dbg_fixed_rate) {
Emmanuel Grumbach7ceb98a2014-07-28 12:59:45 +03003656 rs_fill_lq_cmd(mvm, NULL, lq_sta, NULL);
Eliad Pellerae969af2014-06-11 15:51:33 +03003657 iwl_mvm_send_lq_cmd(lq_sta->pers.drv, &lq_sta->lq, false);
Eyal Shapiraa0c38b22013-12-03 18:34:52 +02003658 }
3659}
3660
Emmanuel Grumbach9d108492013-12-03 11:50:30 +02003661static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
3662 const char __user *user_buf, size_t count, loff_t *ppos)
3663{
3664 struct iwl_lq_sta *lq_sta = file->private_data;
3665 struct iwl_mvm *mvm;
3666 char buf[64];
3667 size_t buf_size;
3668 u32 parsed_rate;
3669
Eliad Pellerae969af2014-06-11 15:51:33 +03003670 mvm = lq_sta->pers.drv;
Emmanuel Grumbach9d108492013-12-03 11:50:30 +02003671 memset(buf, 0, sizeof(buf));
3672 buf_size = min(count, sizeof(buf) - 1);
3673 if (copy_from_user(buf, user_buf, buf_size))
3674 return -EFAULT;
3675
3676 if (sscanf(buf, "%x", &parsed_rate) == 1)
Eliad Pellerae969af2014-06-11 15:51:33 +03003677 lq_sta->pers.dbg_fixed_rate = parsed_rate;
Emmanuel Grumbach9d108492013-12-03 11:50:30 +02003678 else
Eliad Pellerae969af2014-06-11 15:51:33 +03003679 lq_sta->pers.dbg_fixed_rate = 0;
Emmanuel Grumbach9d108492013-12-03 11:50:30 +02003680
3681 rs_program_fix_rate(mvm, lq_sta);
3682
3683 return count;
3684}
3685
Johannes Berg8ca151b2013-01-24 14:25:36 +01003686static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
3687 char __user *user_buf, size_t count, loff_t *ppos)
3688{
3689 char *buff;
3690 int desc = 0;
3691 int i = 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +01003692 ssize_t ret;
Gregory Greenmanf6f046f2017-11-26 15:51:31 +02003693 static const size_t bufsz = 2048;
Johannes Berg8ca151b2013-01-24 14:25:36 +01003694
3695 struct iwl_lq_sta *lq_sta = file->private_data;
Emmanuel Grumbach04e3a5d2015-10-28 09:47:41 +02003696 struct iwl_mvm_sta *mvmsta =
Gregory Greenmanecaf71d2017-11-01 07:16:29 +02003697 container_of(lq_sta, struct iwl_mvm_sta, lq_sta.rs_drv);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003698 struct iwl_mvm *mvm;
3699 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
Eyal Shapira5aa33552013-11-23 01:06:36 +02003700 struct rs_rate *rate = &tbl->rate;
Eyal Shapira16c426f2015-02-02 12:46:22 +02003701 u32 ss_params;
Emmanuel Grumbach04e3a5d2015-10-28 09:47:41 +02003702
Eliad Pellerae969af2014-06-11 15:51:33 +03003703 mvm = lq_sta->pers.drv;
Gregory Greenmanf6f046f2017-11-26 15:51:31 +02003704 buff = kmalloc(bufsz, GFP_KERNEL);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003705 if (!buff)
3706 return -ENOMEM;
3707
Gregory Greenmanf6f046f2017-11-26 15:51:31 +02003708 desc += scnprintf(buff + desc, bufsz - desc,
3709 "sta_id %d\n", lq_sta->lq.sta_id);
3710 desc += scnprintf(buff + desc, bufsz - desc,
3711 "failed=%d success=%d rate=0%lX\n",
3712 lq_sta->total_failed, lq_sta->total_success,
3713 lq_sta->active_legacy_rate);
3714 desc += scnprintf(buff + desc, bufsz - desc, "fixed rate 0x%X\n",
3715 lq_sta->pers.dbg_fixed_rate);
3716 desc += scnprintf(buff + desc, bufsz - desc, "valid_tx_ant %s%s%s\n",
Moshe Harela0544272014-12-08 21:13:14 +02003717 (iwl_mvm_get_valid_tx_ant(mvm) & ANT_A) ? "ANT_A," : "",
3718 (iwl_mvm_get_valid_tx_ant(mvm) & ANT_B) ? "ANT_B," : "",
3719 (iwl_mvm_get_valid_tx_ant(mvm) & ANT_C) ? "ANT_C" : "");
Gregory Greenmanf6f046f2017-11-26 15:51:31 +02003720 desc += scnprintf(buff + desc, bufsz - desc, "lq type %s\n",
3721 (is_legacy(rate)) ? "legacy" :
3722 is_vht(rate) ? "VHT" : "HT");
Eyal Shapira5aa33552013-11-23 01:06:36 +02003723 if (!is_legacy(rate)) {
Gregory Greenmanf6f046f2017-11-26 15:51:31 +02003724 desc += scnprintf(buff + desc, bufsz - desc, " %s",
Eyal Shapira5aa33552013-11-23 01:06:36 +02003725 (is_siso(rate)) ? "SISO" : "MIMO2");
Gregory Greenmanf6f046f2017-11-26 15:51:31 +02003726 desc += scnprintf(buff + desc, bufsz - desc, " %s",
Emmanuel Grumbache70fe7e2015-03-01 17:18:00 +02003727 (is_ht20(rate)) ? "20MHz" :
3728 (is_ht40(rate)) ? "40MHz" :
Gregory Greenman855f4922016-05-25 18:31:39 +03003729 (is_ht80(rate)) ? "80MHz" :
3730 (is_ht160(rate)) ? "160MHz" : "BAD BW");
Gregory Greenmanf6f046f2017-11-26 15:51:31 +02003731 desc += scnprintf(buff + desc, bufsz - desc, " %s %s %s %s\n",
Emmanuel Grumbache70fe7e2015-03-01 17:18:00 +02003732 (rate->sgi) ? "SGI" : "NGI",
3733 (rate->ldpc) ? "LDPC" : "BCC",
Emmanuel Grumbach04e3a5d2015-10-28 09:47:41 +02003734 (lq_sta->is_agg) ? "AGG on" : "",
3735 (mvmsta->tlc_amsdu) ? "AMSDU on" : "");
Johannes Berg8ca151b2013-01-24 14:25:36 +01003736 }
Gregory Greenmanf6f046f2017-11-26 15:51:31 +02003737 desc += scnprintf(buff + desc, bufsz - desc, "last tx rate=0x%X\n",
Johannes Berg8ca151b2013-01-24 14:25:36 +01003738 lq_sta->last_rate_n_flags);
Gregory Greenmanf6f046f2017-11-26 15:51:31 +02003739 desc += scnprintf(buff + desc, bufsz - desc,
Eyal Shapira1a61b342013-11-09 02:11:46 +02003740 "general: flags=0x%X mimo-d=%d s-ant=0x%x d-ant=0x%x\n",
Johannes Berg8ca151b2013-01-24 14:25:36 +01003741 lq_sta->lq.flags,
3742 lq_sta->lq.mimo_delim,
3743 lq_sta->lq.single_stream_ant_msk,
3744 lq_sta->lq.dual_stream_ant_msk);
3745
Gregory Greenmanf6f046f2017-11-26 15:51:31 +02003746 desc += scnprintf(buff + desc, bufsz - desc,
Johannes Berg8ca151b2013-01-24 14:25:36 +01003747 "agg: time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
3748 le16_to_cpu(lq_sta->lq.agg_time_limit),
3749 lq_sta->lq.agg_disable_start_th,
3750 lq_sta->lq.agg_frame_cnt_limit);
3751
Gregory Greenmanf6f046f2017-11-26 15:51:31 +02003752 desc += scnprintf(buff + desc, bufsz - desc, "reduced tpc=%d\n",
3753 lq_sta->lq.reduced_tpc);
Eyal Shapira16c426f2015-02-02 12:46:22 +02003754 ss_params = le32_to_cpu(lq_sta->lq.ss_params);
Gregory Greenmanf6f046f2017-11-26 15:51:31 +02003755 desc += scnprintf(buff + desc, bufsz - desc,
3756 "single stream params: %s%s%s%s\n",
Eyal Shapira16c426f2015-02-02 12:46:22 +02003757 (ss_params & LQ_SS_PARAMS_VALID) ?
Eyal Shapiradb7c6892015-03-02 10:35:19 +02003758 "VALID" : "INVALID",
Eyal Shapira16c426f2015-02-02 12:46:22 +02003759 (ss_params & LQ_SS_BFER_ALLOWED) ?
Eyal Shapiradb7c6892015-03-02 10:35:19 +02003760 ", BFER" : "",
Eyal Shapira16c426f2015-02-02 12:46:22 +02003761 (ss_params & LQ_SS_STBC_1SS_ALLOWED) ?
Eyal Shapiradb7c6892015-03-02 10:35:19 +02003762 ", STBC" : "",
Eyal Shapira16c426f2015-02-02 12:46:22 +02003763 (ss_params & LQ_SS_FORCE) ?
Eyal Shapiradb7c6892015-03-02 10:35:19 +02003764 ", FORCE" : "");
Gregory Greenmanf6f046f2017-11-26 15:51:31 +02003765 desc += scnprintf(buff + desc, bufsz - desc,
Johannes Berg8ca151b2013-01-24 14:25:36 +01003766 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
3767 lq_sta->lq.initial_rate_index[0],
3768 lq_sta->lq.initial_rate_index[1],
3769 lq_sta->lq.initial_rate_index[2],
3770 lq_sta->lq.initial_rate_index[3]);
3771
3772 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
Johannes Bergcf4ef652013-12-17 11:34:25 +01003773 u32 r = le32_to_cpu(lq_sta->lq.rs_table[i]);
Eyal Shapira1a61b342013-11-09 02:11:46 +02003774
Gregory Greenmanf6f046f2017-11-26 15:51:31 +02003775 desc += scnprintf(buff + desc, bufsz - desc,
3776 " rate[%d] 0x%X ", i, r);
3777 desc += rs_pretty_print_rate(buff + desc, bufsz - desc, r);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003778 }
3779
3780 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
3781 kfree(buff);
3782 return ret;
3783}
3784
3785static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
3786 .write = rs_sta_dbgfs_scale_table_write,
3787 .read = rs_sta_dbgfs_scale_table_read,
3788 .open = simple_open,
3789 .llseek = default_llseek,
3790};
3791static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
3792 char __user *user_buf, size_t count, loff_t *ppos)
3793{
3794 char *buff;
3795 int desc = 0;
3796 int i, j;
3797 ssize_t ret;
Eyal Shapirad310e402013-08-11 18:43:47 +03003798 struct iwl_scale_tbl_info *tbl;
Eyal Shapira5aa33552013-11-23 01:06:36 +02003799 struct rs_rate *rate;
Johannes Berg8ca151b2013-01-24 14:25:36 +01003800 struct iwl_lq_sta *lq_sta = file->private_data;
3801
3802 buff = kmalloc(1024, GFP_KERNEL);
3803 if (!buff)
3804 return -ENOMEM;
3805
3806 for (i = 0; i < LQ_SIZE; i++) {
Eyal Shapirad310e402013-08-11 18:43:47 +03003807 tbl = &(lq_sta->lq_info[i]);
Eyal Shapira5aa33552013-11-23 01:06:36 +02003808 rate = &tbl->rate;
Johannes Berg8ca151b2013-01-24 14:25:36 +01003809 desc += sprintf(buff+desc,
Eyal Shapira6a524f42013-08-11 20:27:18 +03003810 "%s type=%d SGI=%d BW=%s DUP=0\n"
Eyal Shapira8fc7c582013-12-04 02:15:46 +02003811 "index=%d\n",
Johannes Berg8ca151b2013-01-24 14:25:36 +01003812 lq_sta->active_tbl == i ? "*" : "x",
Eyal Shapira5aa33552013-11-23 01:06:36 +02003813 rate->type,
3814 rate->sgi,
Gregory Greenman855f4922016-05-25 18:31:39 +03003815 is_ht20(rate) ? "20MHz" :
3816 is_ht40(rate) ? "40MHz" :
3817 is_ht80(rate) ? "80MHz" :
3818 is_ht160(rate) ? "160MHz" : "ERR",
Eyal Shapira8fc7c582013-12-04 02:15:46 +02003819 rate->index);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003820 for (j = 0; j < IWL_RATE_COUNT; j++) {
3821 desc += sprintf(buff+desc,
3822 "counter=%d success=%d %%=%d\n",
Eyal Shapirad310e402013-08-11 18:43:47 +03003823 tbl->win[j].counter,
3824 tbl->win[j].success_counter,
3825 tbl->win[j].success_ratio);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003826 }
3827 }
3828 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
3829 kfree(buff);
3830 return ret;
3831}
3832
3833static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
3834 .read = rs_sta_dbgfs_stats_table_read,
3835 .open = simple_open,
3836 .llseek = default_llseek,
3837};
3838
Eyal Shapira757cf23b2014-03-24 02:12:00 +02003839static ssize_t rs_sta_dbgfs_drv_tx_stats_read(struct file *file,
3840 char __user *user_buf,
3841 size_t count, loff_t *ppos)
3842{
3843 static const char * const column_name[] = {
3844 [RS_COLUMN_LEGACY_ANT_A] = "LEGACY_ANT_A",
3845 [RS_COLUMN_LEGACY_ANT_B] = "LEGACY_ANT_B",
3846 [RS_COLUMN_SISO_ANT_A] = "SISO_ANT_A",
3847 [RS_COLUMN_SISO_ANT_B] = "SISO_ANT_B",
3848 [RS_COLUMN_SISO_ANT_A_SGI] = "SISO_ANT_A_SGI",
3849 [RS_COLUMN_SISO_ANT_B_SGI] = "SISO_ANT_B_SGI",
3850 [RS_COLUMN_MIMO2] = "MIMO2",
3851 [RS_COLUMN_MIMO2_SGI] = "MIMO2_SGI",
3852 };
3853
3854 static const char * const rate_name[] = {
3855 [IWL_RATE_1M_INDEX] = "1M",
3856 [IWL_RATE_2M_INDEX] = "2M",
3857 [IWL_RATE_5M_INDEX] = "5.5M",
3858 [IWL_RATE_11M_INDEX] = "11M",
3859 [IWL_RATE_6M_INDEX] = "6M|MCS0",
3860 [IWL_RATE_9M_INDEX] = "9M",
3861 [IWL_RATE_12M_INDEX] = "12M|MCS1",
3862 [IWL_RATE_18M_INDEX] = "18M|MCS2",
3863 [IWL_RATE_24M_INDEX] = "24M|MCS3",
3864 [IWL_RATE_36M_INDEX] = "36M|MCS4",
3865 [IWL_RATE_48M_INDEX] = "48M|MCS5",
3866 [IWL_RATE_54M_INDEX] = "54M|MCS6",
3867 [IWL_RATE_MCS_7_INDEX] = "MCS7",
3868 [IWL_RATE_MCS_8_INDEX] = "MCS8",
3869 [IWL_RATE_MCS_9_INDEX] = "MCS9",
3870 };
3871
3872 char *buff, *pos, *endpos;
3873 int col, rate;
3874 ssize_t ret;
3875 struct iwl_lq_sta *lq_sta = file->private_data;
3876 struct rs_rate_stats *stats;
3877 static const size_t bufsz = 1024;
3878
3879 buff = kmalloc(bufsz, GFP_KERNEL);
3880 if (!buff)
3881 return -ENOMEM;
3882
3883 pos = buff;
3884 endpos = pos + bufsz;
3885
3886 pos += scnprintf(pos, endpos - pos, "COLUMN,");
3887 for (rate = 0; rate < IWL_RATE_COUNT; rate++)
3888 pos += scnprintf(pos, endpos - pos, "%s,", rate_name[rate]);
3889 pos += scnprintf(pos, endpos - pos, "\n");
3890
3891 for (col = 0; col < RS_COLUMN_COUNT; col++) {
3892 pos += scnprintf(pos, endpos - pos,
3893 "%s,", column_name[col]);
3894
3895 for (rate = 0; rate < IWL_RATE_COUNT; rate++) {
Eyal Shapiraf58220f2014-08-16 01:30:30 +03003896 stats = &(lq_sta->pers.tx_stats[col][rate]);
Eyal Shapira757cf23b2014-03-24 02:12:00 +02003897 pos += scnprintf(pos, endpos - pos,
3898 "%llu/%llu,",
3899 stats->success,
3900 stats->total);
3901 }
3902 pos += scnprintf(pos, endpos - pos, "\n");
3903 }
3904
3905 ret = simple_read_from_buffer(user_buf, count, ppos, buff, pos - buff);
3906 kfree(buff);
3907 return ret;
3908}
3909
3910static ssize_t rs_sta_dbgfs_drv_tx_stats_write(struct file *file,
3911 const char __user *user_buf,
3912 size_t count, loff_t *ppos)
3913{
3914 struct iwl_lq_sta *lq_sta = file->private_data;
Eyal Shapiraf58220f2014-08-16 01:30:30 +03003915 memset(lq_sta->pers.tx_stats, 0, sizeof(lq_sta->pers.tx_stats));
Eyal Shapira757cf23b2014-03-24 02:12:00 +02003916
3917 return count;
3918}
3919
3920static const struct file_operations rs_sta_dbgfs_drv_tx_stats_ops = {
3921 .read = rs_sta_dbgfs_drv_tx_stats_read,
3922 .write = rs_sta_dbgfs_drv_tx_stats_write,
3923 .open = simple_open,
3924 .llseek = default_llseek,
3925};
3926
Eyal Shapira1e9c62f2015-01-28 14:44:06 +02003927static ssize_t iwl_dbgfs_ss_force_read(struct file *file,
3928 char __user *user_buf,
3929 size_t count, loff_t *ppos)
3930{
3931 struct iwl_lq_sta *lq_sta = file->private_data;
3932 char buf[12];
3933 int bufsz = sizeof(buf);
3934 int pos = 0;
3935 static const char * const ss_force_name[] = {
3936 [RS_SS_FORCE_NONE] = "none",
3937 [RS_SS_FORCE_STBC] = "stbc",
3938 [RS_SS_FORCE_BFER] = "bfer",
3939 [RS_SS_FORCE_SISO] = "siso",
3940 };
3941
3942 pos += scnprintf(buf+pos, bufsz-pos, "%s\n",
Eyal Shapira878985ce2015-02-02 12:14:54 +02003943 ss_force_name[lq_sta->pers.ss_force]);
Eyal Shapira1e9c62f2015-01-28 14:44:06 +02003944 return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
3945}
3946
3947static ssize_t iwl_dbgfs_ss_force_write(struct iwl_lq_sta *lq_sta, char *buf,
3948 size_t count, loff_t *ppos)
3949{
3950 struct iwl_mvm *mvm = lq_sta->pers.drv;
3951 int ret = 0;
3952
3953 if (!strncmp("none", buf, 4)) {
Eyal Shapira878985ce2015-02-02 12:14:54 +02003954 lq_sta->pers.ss_force = RS_SS_FORCE_NONE;
Eyal Shapira1e9c62f2015-01-28 14:44:06 +02003955 } else if (!strncmp("siso", buf, 4)) {
Eyal Shapira878985ce2015-02-02 12:14:54 +02003956 lq_sta->pers.ss_force = RS_SS_FORCE_SISO;
Eyal Shapira1e9c62f2015-01-28 14:44:06 +02003957 } else if (!strncmp("stbc", buf, 4)) {
3958 if (lq_sta->stbc_capable) {
Eyal Shapira878985ce2015-02-02 12:14:54 +02003959 lq_sta->pers.ss_force = RS_SS_FORCE_STBC;
Eyal Shapira1e9c62f2015-01-28 14:44:06 +02003960 } else {
3961 IWL_ERR(mvm,
3962 "can't force STBC. peer doesn't support\n");
3963 ret = -EINVAL;
3964 }
3965 } else if (!strncmp("bfer", buf, 4)) {
3966 if (lq_sta->bfer_capable) {
Eyal Shapira878985ce2015-02-02 12:14:54 +02003967 lq_sta->pers.ss_force = RS_SS_FORCE_BFER;
Eyal Shapira1e9c62f2015-01-28 14:44:06 +02003968 } else {
3969 IWL_ERR(mvm,
3970 "can't force BFER. peer doesn't support\n");
3971 ret = -EINVAL;
3972 }
3973 } else {
3974 IWL_ERR(mvm, "valid values none|siso|stbc|bfer\n");
3975 ret = -EINVAL;
3976 }
3977 return ret ?: count;
3978}
3979
3980#define MVM_DEBUGFS_READ_WRITE_FILE_OPS(name, bufsz) \
3981 _MVM_DEBUGFS_READ_WRITE_FILE_OPS(name, bufsz, struct iwl_lq_sta)
3982#define MVM_DEBUGFS_ADD_FILE_RS(name, parent, mode) do { \
3983 if (!debugfs_create_file(#name, mode, parent, lq_sta, \
3984 &iwl_dbgfs_##name##_ops)) \
3985 goto err; \
3986 } while (0)
3987
3988MVM_DEBUGFS_READ_WRITE_FILE_OPS(ss_force, 32);
3989
Gregory Greenman9f66a392017-11-05 18:49:48 +02003990static void rs_drv_add_sta_debugfs(void *mvm, void *priv_sta,
3991 struct dentry *dir)
Johannes Berg8ca151b2013-01-24 14:25:36 +01003992{
Johannes Bergdcbc17a2015-03-10 21:24:34 +01003993 struct iwl_lq_sta *lq_sta = priv_sta;
3994 struct iwl_mvm_sta *mvmsta;
3995
Gregory Greenmanecaf71d2017-11-01 07:16:29 +02003996 mvmsta = container_of(lq_sta, struct iwl_mvm_sta, lq_sta.rs_drv);
Johannes Bergdcbc17a2015-03-10 21:24:34 +01003997
3998 if (!mvmsta->vif)
3999 return;
Eyal Shapira1e9c62f2015-01-28 14:44:06 +02004000
Eliad Pellerc6e1faa2014-06-15 12:02:20 +03004001 debugfs_create_file("rate_scale_table", S_IRUSR | S_IWUSR, dir,
4002 lq_sta, &rs_sta_dbgfs_scale_table_ops);
4003 debugfs_create_file("rate_stats_table", S_IRUSR, dir,
4004 lq_sta, &rs_sta_dbgfs_stats_table_ops);
4005 debugfs_create_file("drv_tx_stats", S_IRUSR | S_IWUSR, dir,
4006 lq_sta, &rs_sta_dbgfs_drv_tx_stats_ops);
4007 debugfs_create_u8("tx_agg_tid_enable", S_IRUSR | S_IWUSR, dir,
4008 &lq_sta->tx_agg_tid_en);
4009 debugfs_create_u8("reduced_tpc", S_IRUSR | S_IWUSR, dir,
4010 &lq_sta->pers.dbg_fixed_txp_reduction);
Eyal Shapira1e9c62f2015-01-28 14:44:06 +02004011
4012 MVM_DEBUGFS_ADD_FILE_RS(ss_force, dir, S_IRUSR | S_IWUSR);
4013 return;
4014err:
4015 IWL_ERR((struct iwl_mvm *)mvm, "Can't create debugfs entity\n");
Johannes Berg8ca151b2013-01-24 14:25:36 +01004016}
4017
Gregory Greenman9f66a392017-11-05 18:49:48 +02004018void rs_remove_sta_debugfs(void *mvm, void *mvm_sta)
Johannes Berg8ca151b2013-01-24 14:25:36 +01004019{
Johannes Berg8ca151b2013-01-24 14:25:36 +01004020}
4021#endif
4022
4023/*
4024 * Initialization of rate scaling information is done by driver after
4025 * the station is added. Since mac80211 calls this function before a
4026 * station is added we ignore it.
4027 */
Gregory Greenman9f66a392017-11-05 18:49:48 +02004028static void rs_rate_init_ops(void *mvm_r,
4029 struct ieee80211_supported_band *sband,
4030 struct cfg80211_chan_def *chandef,
4031 struct ieee80211_sta *sta, void *mvm_sta)
Johannes Berg8ca151b2013-01-24 14:25:36 +01004032{
4033}
Johannes Berg631ad702014-01-20 23:29:34 +01004034
Gregory Greenman9f66a392017-11-05 18:49:48 +02004035/* ops for rate scaling implemented in the driver */
4036static const struct rate_control_ops rs_mvm_ops_drv = {
Johannes Berg8ca151b2013-01-24 14:25:36 +01004037 .name = RS_NAME,
Gregory Greenman9f66a392017-11-05 18:49:48 +02004038 .tx_status = rs_drv_mac80211_tx_status,
4039 .get_rate = rs_drv_get_rate,
4040 .rate_init = rs_rate_init_ops,
Johannes Berg8ca151b2013-01-24 14:25:36 +01004041 .alloc = rs_alloc,
4042 .free = rs_free,
Gregory Greenman9f66a392017-11-05 18:49:48 +02004043 .alloc_sta = rs_drv_alloc_sta,
Johannes Berg8ca151b2013-01-24 14:25:36 +01004044 .free_sta = rs_free_sta,
Gregory Greenman9f66a392017-11-05 18:49:48 +02004045 .rate_update = rs_drv_rate_update,
Johannes Berg8ca151b2013-01-24 14:25:36 +01004046#ifdef CONFIG_MAC80211_DEBUGFS
Gregory Greenman9f66a392017-11-05 18:49:48 +02004047 .add_sta_debugfs = rs_drv_add_sta_debugfs,
4048 .remove_sta_debugfs = rs_remove_sta_debugfs,
Johannes Berg8ca151b2013-01-24 14:25:36 +01004049#endif
4050};
4051
Gregory Greenman9f66a392017-11-05 18:49:48 +02004052void iwl_mvm_rs_rate_init(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
4053 enum nl80211_band band, bool init)
4054{
Emmanuel Grumbach4243edb2017-12-13 11:38:48 +02004055 if (iwl_mvm_has_tlc_offload(mvm))
Gregory Greenman9f66a392017-11-05 18:49:48 +02004056 rs_fw_rate_init(mvm, sta, band);
4057 else
4058 rs_drv_rate_init(mvm, sta, band, init);
4059}
4060
Johannes Berg8ca151b2013-01-24 14:25:36 +01004061int iwl_mvm_rate_control_register(void)
4062{
Gregory Greenman9f66a392017-11-05 18:49:48 +02004063 return ieee80211_rate_control_register(&rs_mvm_ops_drv);
Johannes Berg8ca151b2013-01-24 14:25:36 +01004064}
4065
4066void iwl_mvm_rate_control_unregister(void)
4067{
Gregory Greenman9f66a392017-11-05 18:49:48 +02004068 ieee80211_rate_control_unregister(&rs_mvm_ops_drv);
Johannes Berg8ca151b2013-01-24 14:25:36 +01004069}
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +03004070
Gregory Greenman110b32f2017-11-01 09:04:38 +02004071static int rs_drv_tx_protection(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
4072 bool enable)
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +03004073{
Gregory Greenmanecaf71d2017-11-01 07:16:29 +02004074 struct iwl_lq_cmd *lq = &mvmsta->lq_sta.rs_drv.lq;
Johannes Berge126b5d2013-06-28 13:39:18 +02004075
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +03004076 lockdep_assert_held(&mvm->mutex);
4077
4078 if (enable) {
4079 if (mvmsta->tx_protection == 0)
Eyal Shapirad307ec82013-11-11 19:56:21 +02004080 lq->flags |= LQ_FLAG_USE_RTS_MSK;
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +03004081 mvmsta->tx_protection++;
4082 } else {
4083 mvmsta->tx_protection--;
4084 if (mvmsta->tx_protection == 0)
Eyal Shapirad307ec82013-11-11 19:56:21 +02004085 lq->flags &= ~LQ_FLAG_USE_RTS_MSK;
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +03004086 }
4087
Eyal Shapira9e680942013-11-09 00:16:16 +02004088 return iwl_mvm_send_lq_cmd(mvm, lq, false);
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +03004089}
Gregory Greenman110b32f2017-11-01 09:04:38 +02004090
4091/**
4092 * iwl_mvm_tx_protection - ask FW to enable RTS/CTS protection
4093 * @mvmsta: The station
4094 * @enable: Enable Tx protection?
4095 */
4096int iwl_mvm_tx_protection(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
4097 bool enable)
4098{
Emmanuel Grumbach4243edb2017-12-13 11:38:48 +02004099 if (iwl_mvm_has_tlc_offload(mvm))
Gregory Greenman110b32f2017-11-01 09:04:38 +02004100 return rs_fw_tx_protection(mvm, mvmsta, enable);
4101 else
4102 return rs_drv_tx_protection(mvm, mvmsta, enable);
4103}