blob: 7aecf46e2ab81f53a259bd094146dd9e4a96ba6a [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 Greenman69c7fda2015-12-29 11:26:35 +02005 * Copyright(c) 2016 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
45#define RS_NAME "iwl-mvm-rs"
46
Johannes Berg8ca151b2013-01-24 14:25:36 +010047#define IWL_RATE_MAX_WINDOW 62 /* # tx in history window */
Johannes Berg8ca151b2013-01-24 14:25:36 +010048
Eyal Shapira834437d2014-12-17 15:38:58 +020049/* Calculations of success ratio are done in fixed point where 12800 is 100%.
50 * Use this macro when dealing with thresholds consts set as a percentage
51 */
52#define RS_PERCENT(x) (128 * x)
Johannes Berg8ca151b2013-01-24 14:25:36 +010053
54static u8 rs_ht_to_legacy[] = {
Eyal Shapirada87d7d2013-11-28 12:27:03 +020055 [IWL_RATE_MCS_0_INDEX] = IWL_RATE_6M_INDEX,
56 [IWL_RATE_MCS_1_INDEX] = IWL_RATE_9M_INDEX,
57 [IWL_RATE_MCS_2_INDEX] = IWL_RATE_12M_INDEX,
58 [IWL_RATE_MCS_3_INDEX] = IWL_RATE_18M_INDEX,
59 [IWL_RATE_MCS_4_INDEX] = IWL_RATE_24M_INDEX,
60 [IWL_RATE_MCS_5_INDEX] = IWL_RATE_36M_INDEX,
61 [IWL_RATE_MCS_6_INDEX] = IWL_RATE_48M_INDEX,
62 [IWL_RATE_MCS_7_INDEX] = IWL_RATE_54M_INDEX,
63 [IWL_RATE_MCS_8_INDEX] = IWL_RATE_54M_INDEX,
64 [IWL_RATE_MCS_9_INDEX] = IWL_RATE_54M_INDEX,
Johannes Berg8ca151b2013-01-24 14:25:36 +010065};
66
67static const u8 ant_toggle_lookup[] = {
Eliad Pelleref4394b2013-07-16 18:07:20 +030068 [ANT_NONE] = ANT_NONE,
69 [ANT_A] = ANT_B,
70 [ANT_B] = ANT_C,
71 [ANT_AB] = ANT_BC,
72 [ANT_C] = ANT_A,
73 [ANT_AC] = ANT_AB,
74 [ANT_BC] = ANT_AC,
75 [ANT_ABC] = ANT_ABC,
Johannes Berg8ca151b2013-01-24 14:25:36 +010076};
77
Eyal Shapirad310e402013-08-11 18:43:47 +030078#define IWL_DECLARE_RATE_INFO(r, s, rp, rn) \
79 [IWL_RATE_##r##M_INDEX] = { IWL_RATE_##r##M_PLCP, \
80 IWL_RATE_HT_SISO_MCS_##s##_PLCP, \
81 IWL_RATE_HT_MIMO2_MCS_##s##_PLCP, \
82 IWL_RATE_VHT_SISO_MCS_##s##_PLCP, \
83 IWL_RATE_VHT_MIMO2_MCS_##s##_PLCP,\
84 IWL_RATE_##rp##M_INDEX, \
Eyal Shapiraab055ce2013-08-04 13:10:31 +030085 IWL_RATE_##rn##M_INDEX }
Johannes Berg8ca151b2013-01-24 14:25:36 +010086
Eyal Shapirad310e402013-08-11 18:43:47 +030087#define IWL_DECLARE_MCS_RATE(s) \
88 [IWL_RATE_MCS_##s##_INDEX] = { IWL_RATE_INVM_PLCP, \
89 IWL_RATE_HT_SISO_MCS_##s##_PLCP, \
90 IWL_RATE_HT_MIMO2_MCS_##s##_PLCP, \
91 IWL_RATE_VHT_SISO_MCS_##s##_PLCP, \
92 IWL_RATE_VHT_MIMO2_MCS_##s##_PLCP, \
93 IWL_RATE_INVM_INDEX, \
94 IWL_RATE_INVM_INDEX }
95
Johannes Berg8ca151b2013-01-24 14:25:36 +010096/*
97 * Parameter order:
Eyal Shapiraab055ce2013-08-04 13:10:31 +030098 * rate, ht rate, prev rate, next rate
Johannes Berg8ca151b2013-01-24 14:25:36 +010099 *
100 * If there isn't a valid next or previous rate then INV is used which
101 * maps to IWL_RATE_INVALID
102 *
103 */
104static const struct iwl_rs_rate_info iwl_rates[IWL_RATE_COUNT] = {
Eyal Shapiraab055ce2013-08-04 13:10:31 +0300105 IWL_DECLARE_RATE_INFO(1, INV, INV, 2), /* 1mbps */
106 IWL_DECLARE_RATE_INFO(2, INV, 1, 5), /* 2mbps */
107 IWL_DECLARE_RATE_INFO(5, INV, 2, 11), /*5.5mbps */
108 IWL_DECLARE_RATE_INFO(11, INV, 9, 12), /* 11mbps */
Eyal Shapirad310e402013-08-11 18:43:47 +0300109 IWL_DECLARE_RATE_INFO(6, 0, 5, 11), /* 6mbps ; MCS 0 */
110 IWL_DECLARE_RATE_INFO(9, INV, 6, 11), /* 9mbps */
111 IWL_DECLARE_RATE_INFO(12, 1, 11, 18), /* 12mbps ; MCS 1 */
112 IWL_DECLARE_RATE_INFO(18, 2, 12, 24), /* 18mbps ; MCS 2 */
113 IWL_DECLARE_RATE_INFO(24, 3, 18, 36), /* 24mbps ; MCS 3 */
114 IWL_DECLARE_RATE_INFO(36, 4, 24, 48), /* 36mbps ; MCS 4 */
115 IWL_DECLARE_RATE_INFO(48, 5, 36, 54), /* 48mbps ; MCS 5 */
116 IWL_DECLARE_RATE_INFO(54, 6, 48, INV), /* 54mbps ; MCS 6 */
117 IWL_DECLARE_MCS_RATE(7), /* MCS 7 */
118 IWL_DECLARE_MCS_RATE(8), /* MCS 8 */
119 IWL_DECLARE_MCS_RATE(9), /* MCS 9 */
Johannes Berg8ca151b2013-01-24 14:25:36 +0100120};
121
Eyal Shapira4e4b8152013-12-11 00:55:36 +0200122enum rs_action {
123 RS_ACTION_STAY = 0,
124 RS_ACTION_DOWNSCALE = -1,
125 RS_ACTION_UPSCALE = 1,
126};
127
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200128enum rs_column_mode {
129 RS_INVALID = 0,
130 RS_LEGACY,
131 RS_SISO,
132 RS_MIMO2,
133};
134
Eyal Shapirafd7dbee2014-04-06 04:39:23 +0300135#define MAX_NEXT_COLUMNS 7
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200136#define MAX_COLUMN_CHECKS 3
137
Eyal Shapira0b8d17f2015-02-02 15:21:27 +0200138struct rs_tx_column;
139
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200140typedef bool (*allow_column_func_t) (struct iwl_mvm *mvm,
141 struct ieee80211_sta *sta,
Eyal Shapirabb8f44c2015-05-27 22:00:03 +0300142 struct rs_rate *rate,
Eyal Shapira0b8d17f2015-02-02 15:21:27 +0200143 const struct rs_tx_column *next_col);
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200144
145struct rs_tx_column {
146 enum rs_column_mode mode;
147 u8 ant;
148 bool sgi;
149 enum rs_column next_columns[MAX_NEXT_COLUMNS];
150 allow_column_func_t checks[MAX_COLUMN_CHECKS];
151};
152
Emmanuel Grumbach219fb662014-10-30 11:59:40 +0200153static bool rs_ant_allow(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
Eyal Shapirabb8f44c2015-05-27 22:00:03 +0300154 struct rs_rate *rate,
Eyal Shapira0b8d17f2015-02-02 15:21:27 +0200155 const struct rs_tx_column *next_col)
Emmanuel Grumbach219fb662014-10-30 11:59:40 +0200156{
Eyal Shapira0b8d17f2015-02-02 15:21:27 +0200157 return iwl_mvm_bt_coex_is_ant_avail(mvm, next_col->ant);
Emmanuel Grumbach219fb662014-10-30 11:59:40 +0200158}
159
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200160static bool rs_mimo_allow(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
Eyal Shapirabb8f44c2015-05-27 22:00:03 +0300161 struct rs_rate *rate,
Eyal Shapira0b8d17f2015-02-02 15:21:27 +0200162 const struct rs_tx_column *next_col)
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200163{
Eyal Shapira7ac4a502015-02-01 21:46:00 +0200164 struct iwl_mvm_sta *mvmsta;
165 struct iwl_mvm_vif *mvmvif;
166
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200167 if (!sta->ht_cap.ht_supported)
168 return false;
169
170 if (sta->smps_mode == IEEE80211_SMPS_STATIC)
171 return false;
172
Moshe Harela0544272014-12-08 21:13:14 +0200173 if (num_of_ant(iwl_mvm_get_valid_tx_ant(mvm)) < 2)
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200174 return false;
175
176 if (!iwl_mvm_bt_coex_is_mimo_allowed(mvm, sta))
177 return false;
178
Eyal Shapira7ac4a502015-02-01 21:46:00 +0200179 mvmsta = iwl_mvm_sta_from_mac80211(sta);
180 mvmvif = iwl_mvm_vif_from_mac80211(mvmsta->vif);
Eyal Shapira7ac4a502015-02-01 21:46:00 +0200181
Emmanuel Grumbach5f0d98f2014-10-30 10:19:38 +0200182 if (mvm->nvm_data->sku_cap_mimo_disabled)
183 return false;
184
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200185 return true;
186}
187
188static bool rs_siso_allow(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
Eyal Shapirabb8f44c2015-05-27 22:00:03 +0300189 struct rs_rate *rate,
Eyal Shapira0b8d17f2015-02-02 15:21:27 +0200190 const struct rs_tx_column *next_col)
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200191{
192 if (!sta->ht_cap.ht_supported)
193 return false;
194
195 return true;
196}
197
198static bool rs_sgi_allow(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
Eyal Shapirabb8f44c2015-05-27 22:00:03 +0300199 struct rs_rate *rate,
Eyal Shapira0b8d17f2015-02-02 15:21:27 +0200200 const struct rs_tx_column *next_col)
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200201{
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200202 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
203 struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
204
205 if (is_ht20(rate) && (ht_cap->cap &
206 IEEE80211_HT_CAP_SGI_20))
207 return true;
208 if (is_ht40(rate) && (ht_cap->cap &
209 IEEE80211_HT_CAP_SGI_40))
210 return true;
211 if (is_ht80(rate) && (vht_cap->cap &
212 IEEE80211_VHT_CAP_SHORT_GI_80))
213 return true;
214
215 return false;
216}
217
218static const struct rs_tx_column rs_tx_columns[] = {
219 [RS_COLUMN_LEGACY_ANT_A] = {
220 .mode = RS_LEGACY,
221 .ant = ANT_A,
222 .next_columns = {
223 RS_COLUMN_LEGACY_ANT_B,
224 RS_COLUMN_SISO_ANT_A,
Eyal Shapira31b20452014-04-08 17:52:27 +0300225 RS_COLUMN_MIMO2,
Eyal Shapirad8fff912014-04-06 05:27:36 +0300226 RS_COLUMN_INVALID,
227 RS_COLUMN_INVALID,
Eyal Shapirafd7dbee2014-04-06 04:39:23 +0300228 RS_COLUMN_INVALID,
229 RS_COLUMN_INVALID,
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200230 },
Emmanuel Grumbach219fb662014-10-30 11:59:40 +0200231 .checks = {
232 rs_ant_allow,
233 },
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200234 },
235 [RS_COLUMN_LEGACY_ANT_B] = {
236 .mode = RS_LEGACY,
237 .ant = ANT_B,
238 .next_columns = {
239 RS_COLUMN_LEGACY_ANT_A,
240 RS_COLUMN_SISO_ANT_B,
Eyal Shapira31b20452014-04-08 17:52:27 +0300241 RS_COLUMN_MIMO2,
Eyal Shapirad8fff912014-04-06 05:27:36 +0300242 RS_COLUMN_INVALID,
243 RS_COLUMN_INVALID,
Eyal Shapirafd7dbee2014-04-06 04:39:23 +0300244 RS_COLUMN_INVALID,
245 RS_COLUMN_INVALID,
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200246 },
Emmanuel Grumbach219fb662014-10-30 11:59:40 +0200247 .checks = {
248 rs_ant_allow,
249 },
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200250 },
251 [RS_COLUMN_SISO_ANT_A] = {
252 .mode = RS_SISO,
253 .ant = ANT_A,
254 .next_columns = {
255 RS_COLUMN_SISO_ANT_B,
256 RS_COLUMN_MIMO2,
257 RS_COLUMN_SISO_ANT_A_SGI,
Eyal Shapirafd7dbee2014-04-06 04:39:23 +0300258 RS_COLUMN_LEGACY_ANT_A,
259 RS_COLUMN_LEGACY_ANT_B,
Eyal Shapirad8fff912014-04-06 05:27:36 +0300260 RS_COLUMN_INVALID,
Eyal Shapira31b20452014-04-08 17:52:27 +0300261 RS_COLUMN_INVALID,
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200262 },
263 .checks = {
264 rs_siso_allow,
Emmanuel Grumbach219fb662014-10-30 11:59:40 +0200265 rs_ant_allow,
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200266 },
267 },
268 [RS_COLUMN_SISO_ANT_B] = {
269 .mode = RS_SISO,
270 .ant = ANT_B,
271 .next_columns = {
272 RS_COLUMN_SISO_ANT_A,
273 RS_COLUMN_MIMO2,
274 RS_COLUMN_SISO_ANT_B_SGI,
Eyal Shapirafd7dbee2014-04-06 04:39:23 +0300275 RS_COLUMN_LEGACY_ANT_A,
276 RS_COLUMN_LEGACY_ANT_B,
Eyal Shapirad8fff912014-04-06 05:27:36 +0300277 RS_COLUMN_INVALID,
Eyal Shapira31b20452014-04-08 17:52:27 +0300278 RS_COLUMN_INVALID,
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200279 },
280 .checks = {
281 rs_siso_allow,
Emmanuel Grumbach219fb662014-10-30 11:59:40 +0200282 rs_ant_allow,
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200283 },
284 },
285 [RS_COLUMN_SISO_ANT_A_SGI] = {
286 .mode = RS_SISO,
287 .ant = ANT_A,
288 .sgi = true,
289 .next_columns = {
290 RS_COLUMN_SISO_ANT_B_SGI,
291 RS_COLUMN_MIMO2_SGI,
292 RS_COLUMN_SISO_ANT_A,
Eyal Shapirafd7dbee2014-04-06 04:39:23 +0300293 RS_COLUMN_LEGACY_ANT_A,
294 RS_COLUMN_LEGACY_ANT_B,
Eyal Shapira31b20452014-04-08 17:52:27 +0300295 RS_COLUMN_INVALID,
296 RS_COLUMN_INVALID,
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200297 },
298 .checks = {
299 rs_siso_allow,
Emmanuel Grumbach219fb662014-10-30 11:59:40 +0200300 rs_ant_allow,
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200301 rs_sgi_allow,
302 },
303 },
304 [RS_COLUMN_SISO_ANT_B_SGI] = {
305 .mode = RS_SISO,
306 .ant = ANT_B,
307 .sgi = true,
308 .next_columns = {
309 RS_COLUMN_SISO_ANT_A_SGI,
310 RS_COLUMN_MIMO2_SGI,
311 RS_COLUMN_SISO_ANT_B,
Eyal Shapirafd7dbee2014-04-06 04:39:23 +0300312 RS_COLUMN_LEGACY_ANT_A,
313 RS_COLUMN_LEGACY_ANT_B,
Eyal Shapira31b20452014-04-08 17:52:27 +0300314 RS_COLUMN_INVALID,
315 RS_COLUMN_INVALID,
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200316 },
317 .checks = {
318 rs_siso_allow,
Emmanuel Grumbach219fb662014-10-30 11:59:40 +0200319 rs_ant_allow,
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200320 rs_sgi_allow,
321 },
322 },
323 [RS_COLUMN_MIMO2] = {
324 .mode = RS_MIMO2,
325 .ant = ANT_AB,
326 .next_columns = {
327 RS_COLUMN_SISO_ANT_A,
328 RS_COLUMN_MIMO2_SGI,
Eyal Shapirafd7dbee2014-04-06 04:39:23 +0300329 RS_COLUMN_LEGACY_ANT_A,
330 RS_COLUMN_LEGACY_ANT_B,
Eyal Shapira31b20452014-04-08 17:52:27 +0300331 RS_COLUMN_INVALID,
332 RS_COLUMN_INVALID,
333 RS_COLUMN_INVALID,
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200334 },
335 .checks = {
336 rs_mimo_allow,
337 },
338 },
339 [RS_COLUMN_MIMO2_SGI] = {
340 .mode = RS_MIMO2,
341 .ant = ANT_AB,
342 .sgi = true,
343 .next_columns = {
344 RS_COLUMN_SISO_ANT_A_SGI,
345 RS_COLUMN_MIMO2,
Eyal Shapirafd7dbee2014-04-06 04:39:23 +0300346 RS_COLUMN_LEGACY_ANT_A,
347 RS_COLUMN_LEGACY_ANT_B,
Eyal Shapira31b20452014-04-08 17:52:27 +0300348 RS_COLUMN_INVALID,
349 RS_COLUMN_INVALID,
350 RS_COLUMN_INVALID,
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200351 },
352 .checks = {
353 rs_mimo_allow,
354 rs_sgi_allow,
355 },
356 },
357};
358
Johannes Berg8ca151b2013-01-24 14:25:36 +0100359static inline u8 rs_extract_rate(u32 rate_n_flags)
360{
361 /* also works for HT because bits 7:6 are zero there */
362 return (u8)(rate_n_flags & RATE_LEGACY_RATE_MSK);
363}
364
365static int iwl_hwrate_to_plcp_idx(u32 rate_n_flags)
366{
367 int idx = 0;
368
Johannes Berg8ca151b2013-01-24 14:25:36 +0100369 if (rate_n_flags & RATE_MCS_HT_MSK) {
Eyal Shapirad310e402013-08-11 18:43:47 +0300370 idx = rate_n_flags & RATE_HT_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 HT*/
Johannes Berg8ca151b2013-01-24 14:25:36 +0100374 if (idx >= IWL_RATE_9M_INDEX)
375 idx += 1;
Eyal Shapira4e82dd32013-08-04 23:58:37 +0300376 if ((idx >= IWL_FIRST_HT_RATE) && (idx <= IWL_LAST_HT_RATE))
Johannes Berg8ca151b2013-01-24 14:25:36 +0100377 return idx;
Eyal Shapirad310e402013-08-11 18:43:47 +0300378 } else if (rate_n_flags & RATE_MCS_VHT_MSK) {
379 idx = rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK;
380 idx += IWL_RATE_MCS_0_INDEX;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100381
Eyal Shapirad310e402013-08-11 18:43:47 +0300382 /* skip 9M not supported in VHT*/
383 if (idx >= IWL_RATE_9M_INDEX)
384 idx++;
385 if ((idx >= IWL_FIRST_VHT_RATE) && (idx <= IWL_LAST_VHT_RATE))
386 return idx;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100387 } else {
Eyal Shapirad310e402013-08-11 18:43:47 +0300388 /* legacy rate format, search for match in table */
389
390 u8 legacy_rate = rs_extract_rate(rate_n_flags);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100391 for (idx = 0; idx < ARRAY_SIZE(iwl_rates); idx++)
Eyal Shapirad310e402013-08-11 18:43:47 +0300392 if (iwl_rates[idx].plcp == legacy_rate)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100393 return idx;
394 }
395
Eyal Shapira560843f2014-01-05 21:04:19 +0200396 return IWL_RATE_INVALID;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100397}
398
399static void rs_rate_scale_perform(struct iwl_mvm *mvm,
Eyal Shapira1ebc8f22014-09-14 15:23:23 +0300400 struct ieee80211_sta *sta,
401 struct iwl_lq_sta *lq_sta,
Matti Gottliebff7a68d2016-05-17 10:50:08 +0300402 int tid, bool ndp);
Eyal Shapira8fc7c582013-12-04 02:15:46 +0200403static void rs_fill_lq_cmd(struct iwl_mvm *mvm,
404 struct ieee80211_sta *sta,
405 struct iwl_lq_sta *lq_sta,
406 const struct rs_rate *initial_rate);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100407static void rs_stay_in_table(struct iwl_lq_sta *lq_sta, bool force_search);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100408
Johannes Berg8ca151b2013-01-24 14:25:36 +0100409/**
410 * The following tables contain the expected throughput metrics for all rates
411 *
412 * 1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
413 *
414 * where invalid entries are zeros.
415 *
416 * CCK rates are only valid in legacy table and will only be used in G
417 * (2.4 GHz) band.
418 */
419
Johannes Berga34529e2014-01-20 22:57:21 +0100420static const u16 expected_tpt_legacy[IWL_RATE_COUNT] = {
Eyal Shapira4e82dd32013-08-04 23:58:37 +0300421 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 0, 0, 0
Johannes Berg8ca151b2013-01-24 14:25:36 +0100422};
423
Eyal Shapira4e82dd32013-08-04 23:58:37 +0300424/* Expected TpT tables. 4 indexes:
425 * 0 - NGI, 1 - SGI, 2 - AGG+NGI, 3 - AGG+SGI
426 */
Johannes Berga34529e2014-01-20 22:57:21 +0100427static const u16 expected_tpt_siso_20MHz[4][IWL_RATE_COUNT] = {
Eyal Shapira4e82dd32013-08-04 23:58:37 +0300428 {0, 0, 0, 0, 42, 0, 76, 102, 124, 159, 183, 193, 202, 216, 0},
429 {0, 0, 0, 0, 46, 0, 82, 110, 132, 168, 192, 202, 210, 225, 0},
430 {0, 0, 0, 0, 49, 0, 97, 145, 192, 285, 375, 420, 464, 551, 0},
431 {0, 0, 0, 0, 54, 0, 108, 160, 213, 315, 415, 465, 513, 608, 0},
Johannes Berg8ca151b2013-01-24 14:25:36 +0100432};
433
Johannes Berga34529e2014-01-20 22:57:21 +0100434static const u16 expected_tpt_siso_40MHz[4][IWL_RATE_COUNT] = {
Eyal Shapira4e82dd32013-08-04 23:58:37 +0300435 {0, 0, 0, 0, 77, 0, 127, 160, 184, 220, 242, 250, 257, 269, 275},
436 {0, 0, 0, 0, 83, 0, 135, 169, 193, 229, 250, 257, 264, 275, 280},
437 {0, 0, 0, 0, 101, 0, 199, 295, 389, 570, 744, 828, 911, 1070, 1173},
438 {0, 0, 0, 0, 112, 0, 220, 326, 429, 629, 819, 912, 1000, 1173, 1284},
Johannes Berg8ca151b2013-01-24 14:25:36 +0100439};
440
Johannes Berga34529e2014-01-20 22:57:21 +0100441static const u16 expected_tpt_siso_80MHz[4][IWL_RATE_COUNT] = {
Eyal Shapirad310e402013-08-11 18:43:47 +0300442 {0, 0, 0, 0, 130, 0, 191, 223, 244, 273, 288, 294, 298, 305, 308},
443 {0, 0, 0, 0, 138, 0, 200, 231, 251, 279, 293, 298, 302, 308, 312},
444 {0, 0, 0, 0, 217, 0, 429, 634, 834, 1220, 1585, 1760, 1931, 2258, 2466},
445 {0, 0, 0, 0, 241, 0, 475, 701, 921, 1343, 1741, 1931, 2117, 2468, 2691},
446};
447
Johannes Berga34529e2014-01-20 22:57:21 +0100448static const u16 expected_tpt_mimo2_20MHz[4][IWL_RATE_COUNT] = {
Eyal Shapira4e82dd32013-08-04 23:58:37 +0300449 {0, 0, 0, 0, 74, 0, 123, 155, 179, 213, 235, 243, 250, 261, 0},
450 {0, 0, 0, 0, 81, 0, 131, 164, 187, 221, 242, 250, 256, 267, 0},
451 {0, 0, 0, 0, 98, 0, 193, 286, 375, 550, 718, 799, 878, 1032, 0},
452 {0, 0, 0, 0, 109, 0, 214, 316, 414, 607, 790, 879, 965, 1132, 0},
Johannes Berg8ca151b2013-01-24 14:25:36 +0100453};
454
Johannes Berga34529e2014-01-20 22:57:21 +0100455static const u16 expected_tpt_mimo2_40MHz[4][IWL_RATE_COUNT] = {
Eyal Shapira4e82dd32013-08-04 23:58:37 +0300456 {0, 0, 0, 0, 123, 0, 182, 214, 235, 264, 279, 285, 289, 296, 300},
457 {0, 0, 0, 0, 131, 0, 191, 222, 242, 270, 284, 289, 293, 300, 303},
458 {0, 0, 0, 0, 200, 0, 390, 571, 741, 1067, 1365, 1505, 1640, 1894, 2053},
459 {0, 0, 0, 0, 221, 0, 430, 630, 816, 1169, 1490, 1641, 1784, 2053, 2221},
Johannes Berg8ca151b2013-01-24 14:25:36 +0100460};
461
Johannes Berga34529e2014-01-20 22:57:21 +0100462static const u16 expected_tpt_mimo2_80MHz[4][IWL_RATE_COUNT] = {
Eyal Shapirad310e402013-08-11 18:43:47 +0300463 {0, 0, 0, 0, 182, 0, 240, 264, 278, 299, 308, 311, 313, 317, 319},
464 {0, 0, 0, 0, 190, 0, 247, 269, 282, 302, 310, 313, 315, 319, 320},
465 {0, 0, 0, 0, 428, 0, 833, 1215, 1577, 2254, 2863, 3147, 3418, 3913, 4219},
466 {0, 0, 0, 0, 474, 0, 920, 1338, 1732, 2464, 3116, 3418, 3705, 4225, 4545},
467};
468
Johannes Berg8ca151b2013-01-24 14:25:36 +0100469/* mbps, mcs */
470static const struct iwl_rate_mcs_info iwl_rate_mcs[IWL_RATE_COUNT] = {
471 { "1", "BPSK DSSS"},
472 { "2", "QPSK DSSS"},
473 {"5.5", "BPSK CCK"},
474 { "11", "QPSK CCK"},
475 { "6", "BPSK 1/2"},
476 { "9", "BPSK 1/2"},
477 { "12", "QPSK 1/2"},
478 { "18", "QPSK 3/4"},
479 { "24", "16QAM 1/2"},
480 { "36", "16QAM 3/4"},
481 { "48", "64QAM 2/3"},
482 { "54", "64QAM 3/4"},
483 { "60", "64QAM 5/6"},
484};
485
486#define MCS_INDEX_PER_STREAM (8)
487
Emmanuel Grumbach9d108492013-12-03 11:50:30 +0200488static const char *rs_pretty_ant(u8 ant)
489{
490 static const char * const ant_name[] = {
491 [ANT_NONE] = "None",
492 [ANT_A] = "A",
493 [ANT_B] = "B",
494 [ANT_AB] = "AB",
495 [ANT_C] = "C",
496 [ANT_AC] = "AC",
497 [ANT_BC] = "BC",
498 [ANT_ABC] = "ABC",
499 };
500
501 if (ant > ANT_ABC)
502 return "UNKNOWN";
503
504 return ant_name[ant];
505}
506
507static const char *rs_pretty_lq_type(enum iwl_table_type type)
508{
509 static const char * const lq_types[] = {
510 [LQ_NONE] = "NONE",
511 [LQ_LEGACY_A] = "LEGACY_A",
512 [LQ_LEGACY_G] = "LEGACY_G",
513 [LQ_HT_SISO] = "HT SISO",
514 [LQ_HT_MIMO2] = "HT MIMO",
515 [LQ_VHT_SISO] = "VHT SISO",
516 [LQ_VHT_MIMO2] = "VHT MIMO",
517 };
518
519 if (type < LQ_NONE || type >= LQ_MAX)
520 return "UNKNOWN";
521
522 return lq_types[type];
523}
524
Eyal Shapiracc60c6e2015-09-19 23:37:45 +0300525static char *rs_pretty_rate(const struct rs_rate *rate)
526{
527 static char buf[40];
528 static const char * const legacy_rates[] = {
529 [IWL_RATE_1M_INDEX] = "1M",
530 [IWL_RATE_2M_INDEX] = "2M",
531 [IWL_RATE_5M_INDEX] = "5.5M",
532 [IWL_RATE_11M_INDEX] = "11M",
533 [IWL_RATE_6M_INDEX] = "6M",
534 [IWL_RATE_9M_INDEX] = "9M",
535 [IWL_RATE_12M_INDEX] = "12M",
536 [IWL_RATE_18M_INDEX] = "18M",
537 [IWL_RATE_24M_INDEX] = "24M",
538 [IWL_RATE_36M_INDEX] = "36M",
539 [IWL_RATE_48M_INDEX] = "48M",
540 [IWL_RATE_54M_INDEX] = "54M",
541 };
542 static const char *const ht_vht_rates[] = {
543 [IWL_RATE_MCS_0_INDEX] = "MCS0",
544 [IWL_RATE_MCS_1_INDEX] = "MCS1",
545 [IWL_RATE_MCS_2_INDEX] = "MCS2",
546 [IWL_RATE_MCS_3_INDEX] = "MCS3",
547 [IWL_RATE_MCS_4_INDEX] = "MCS4",
548 [IWL_RATE_MCS_5_INDEX] = "MCS5",
549 [IWL_RATE_MCS_6_INDEX] = "MCS6",
550 [IWL_RATE_MCS_7_INDEX] = "MCS7",
551 [IWL_RATE_MCS_8_INDEX] = "MCS8",
552 [IWL_RATE_MCS_9_INDEX] = "MCS9",
553 };
554 const char *rate_str;
555
Eyal Shapirae8b3f7b2015-11-19 18:37:31 +0200556 if (is_type_legacy(rate->type) && (rate->index <= IWL_RATE_54M_INDEX))
Eyal Shapiracc60c6e2015-09-19 23:37:45 +0300557 rate_str = legacy_rates[rate->index];
Eyal Shapirae8b3f7b2015-11-19 18:37:31 +0200558 else if ((is_type_ht(rate->type) || is_type_vht(rate->type)) &&
Eyal Shapira44945412016-02-01 09:07:05 +0200559 (rate->index >= IWL_RATE_MCS_0_INDEX) &&
Eyal Shapirae8b3f7b2015-11-19 18:37:31 +0200560 (rate->index <= IWL_RATE_MCS_9_INDEX))
Eyal Shapiracc60c6e2015-09-19 23:37:45 +0300561 rate_str = ht_vht_rates[rate->index];
562 else
563 rate_str = "BAD_RATE";
564
565 sprintf(buf, "(%s|%s|%s)", rs_pretty_lq_type(rate->type),
566 rs_pretty_ant(rate->ant), rate_str);
567 return buf;
568}
569
Eyal Shapira5aa33552013-11-23 01:06:36 +0200570static inline void rs_dump_rate(struct iwl_mvm *mvm, const struct rs_rate *rate,
571 const char *prefix)
572{
Eyal Shapirad8bafea2014-10-23 13:58:38 +0300573 IWL_DEBUG_RATE(mvm,
Eyal Shapiracc60c6e2015-09-19 23:37:45 +0300574 "%s: %s BW: %d SGI: %d LDPC: %d STBC: %d\n",
575 prefix, rs_pretty_rate(rate), rate->bw,
576 rate->sgi, rate->ldpc, rate->stbc);
Eyal Shapira5aa33552013-11-23 01:06:36 +0200577}
578
Johannes Berg8ca151b2013-01-24 14:25:36 +0100579static void rs_rate_scale_clear_window(struct iwl_rate_scale_data *window)
580{
581 window->data = 0;
582 window->success_counter = 0;
583 window->success_ratio = IWL_INVALID_VALUE;
584 window->counter = 0;
585 window->average_tpt = IWL_INVALID_VALUE;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100586}
587
Eyal Shapirab804eeb2014-04-06 04:27:06 +0300588static void rs_rate_scale_clear_tbl_windows(struct iwl_mvm *mvm,
589 struct iwl_scale_tbl_info *tbl)
Eliad Peller3ca71f62014-03-10 16:33:43 +0200590{
591 int i;
592
Eyal Shapirab804eeb2014-04-06 04:27:06 +0300593 IWL_DEBUG_RATE(mvm, "Clearing up window stats\n");
Eliad Peller3ca71f62014-03-10 16:33:43 +0200594 for (i = 0; i < IWL_RATE_COUNT; i++)
595 rs_rate_scale_clear_window(&tbl->win[i]);
Eliad Peller2fd647f2014-03-13 17:21:36 +0200596
597 for (i = 0; i < ARRAY_SIZE(tbl->tpc_win); i++)
598 rs_rate_scale_clear_window(&tbl->tpc_win[i]);
Eliad Peller3ca71f62014-03-10 16:33:43 +0200599}
600
Johannes Berg8ca151b2013-01-24 14:25:36 +0100601static inline u8 rs_is_valid_ant(u8 valid_antenna, u8 ant_type)
602{
603 return (ant_type & valid_antenna) == ant_type;
604}
605
Johannes Berg8ca151b2013-01-24 14:25:36 +0100606static int rs_tl_turn_on_agg_for_tid(struct iwl_mvm *mvm,
Eyal Shapira40ce5ed2015-09-19 23:54:38 +0300607 struct iwl_lq_sta *lq_data, u8 tid,
608 struct ieee80211_sta *sta)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100609{
610 int ret = -EAGAIN;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100611
Emmanuel Grumbach44cc4292013-06-18 06:33:49 +0300612 IWL_DEBUG_HT(mvm, "Starting Tx agg: STA: %pM tid: %d\n",
613 sta->addr, tid);
614 ret = ieee80211_start_tx_ba_session(sta, tid, 5000);
615 if (ret == -EAGAIN) {
616 /*
617 * driver and mac80211 is out of sync
618 * this might be cause by reloading firmware
619 * stop the tx ba session here
620 */
621 IWL_ERR(mvm, "Fail start Tx agg on tid: %d\n",
622 tid);
623 ieee80211_stop_tx_ba_session(sta, tid);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100624 }
625 return ret;
626}
627
628static void rs_tl_turn_on_agg(struct iwl_mvm *mvm, u8 tid,
629 struct iwl_lq_sta *lq_data,
630 struct ieee80211_sta *sta)
631{
632 if (tid < IWL_MAX_TID_COUNT)
633 rs_tl_turn_on_agg_for_tid(mvm, lq_data, tid, sta);
634 else
635 IWL_ERR(mvm, "tid exceeds max TID count: %d/%d\n",
636 tid, IWL_MAX_TID_COUNT);
637}
638
639static inline int get_num_of_ant_from_rate(u32 rate_n_flags)
640{
641 return !!(rate_n_flags & RATE_MCS_ANT_A_MSK) +
642 !!(rate_n_flags & RATE_MCS_ANT_B_MSK) +
643 !!(rate_n_flags & RATE_MCS_ANT_C_MSK);
644}
645
646/*
647 * Static function to get the expected throughput from an iwl_scale_tbl_info
648 * that wraps a NULL pointer check
649 */
650static s32 get_expected_tpt(struct iwl_scale_tbl_info *tbl, int rs_index)
651{
652 if (tbl->expected_tpt)
653 return tbl->expected_tpt[rs_index];
654 return 0;
655}
656
657/**
658 * rs_collect_tx_data - Update the success/failure sliding window
659 *
660 * We keep a sliding window of the last 62 packets transmitted
661 * at this rate. window->data contains the bitmask of successful
662 * packets.
663 */
Eyal Shapira834437d2014-12-17 15:38:58 +0200664static int _rs_collect_tx_data(struct iwl_mvm *mvm,
665 struct iwl_scale_tbl_info *tbl,
Eliad Peller0bd3c5a2014-03-10 16:07:04 +0200666 int scale_index, int attempts, int successes,
667 struct iwl_rate_scale_data *window)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100668{
Johannes Berg8ca151b2013-01-24 14:25:36 +0100669 static const u64 mask = (((u64)1) << (IWL_RATE_MAX_WINDOW - 1));
670 s32 fail_count, tpt;
671
Johannes Berg8ca151b2013-01-24 14:25:36 +0100672 /* Get expected throughput */
673 tpt = get_expected_tpt(tbl, scale_index);
674
675 /*
676 * Keep track of only the latest 62 tx frame attempts in this rate's
677 * history window; anything older isn't really relevant any more.
678 * If we have filled up the sliding window, drop the oldest attempt;
679 * if the oldest attempt (highest bit in bitmap) shows "success",
680 * subtract "1" from the success counter (this is the main reason
681 * we keep these bitmaps!).
682 */
683 while (attempts > 0) {
684 if (window->counter >= IWL_RATE_MAX_WINDOW) {
685 /* remove earliest */
686 window->counter = IWL_RATE_MAX_WINDOW - 1;
687
688 if (window->data & mask) {
689 window->data &= ~mask;
690 window->success_counter--;
691 }
692 }
693
694 /* Increment frames-attempted counter */
695 window->counter++;
696
697 /* Shift bitmap by one frame to throw away oldest history */
698 window->data <<= 1;
699
700 /* Mark the most recent #successes attempts as successful */
701 if (successes > 0) {
702 window->success_counter++;
703 window->data |= 0x1;
704 successes--;
705 }
706
707 attempts--;
708 }
709
710 /* Calculate current success ratio, avoid divide-by-0! */
711 if (window->counter > 0)
712 window->success_ratio = 128 * (100 * window->success_counter)
713 / window->counter;
714 else
715 window->success_ratio = IWL_INVALID_VALUE;
716
717 fail_count = window->counter - window->success_counter;
718
719 /* Calculate average throughput, if we have enough history. */
Eyal Shapira834437d2014-12-17 15:38:58 +0200720 if ((fail_count >= IWL_MVM_RS_RATE_MIN_FAILURE_TH) ||
721 (window->success_counter >= IWL_MVM_RS_RATE_MIN_SUCCESS_TH))
Johannes Berg8ca151b2013-01-24 14:25:36 +0100722 window->average_tpt = (window->success_ratio * tpt + 64) / 128;
723 else
724 window->average_tpt = IWL_INVALID_VALUE;
725
Johannes Berg8ca151b2013-01-24 14:25:36 +0100726 return 0;
727}
728
Gregory Greenman69c7fda2015-12-29 11:26:35 +0200729static int rs_collect_tpc_data(struct iwl_mvm *mvm,
730 struct iwl_lq_sta *lq_sta,
731 struct iwl_scale_tbl_info *tbl,
732 int scale_index, int attempts, int successes,
733 u8 reduced_txp)
Eliad Peller0bd3c5a2014-03-10 16:07:04 +0200734{
735 struct iwl_rate_scale_data *window = NULL;
Gregory Greenman69c7fda2015-12-29 11:26:35 +0200736
737 if (WARN_ON_ONCE(reduced_txp > TPC_MAX_REDUCTION))
738 return -EINVAL;
739
740 window = &tbl->tpc_win[reduced_txp];
741 return _rs_collect_tx_data(mvm, tbl, scale_index, attempts, successes,
742 window);
743}
744
745static int rs_collect_tlc_data(struct iwl_mvm *mvm,
746 struct iwl_lq_sta *lq_sta,
747 struct iwl_scale_tbl_info *tbl,
748 int scale_index, int attempts, int successes)
749{
750 struct iwl_rate_scale_data *window = NULL;
Eliad Peller0bd3c5a2014-03-10 16:07:04 +0200751
752 if (scale_index < 0 || scale_index >= IWL_RATE_COUNT)
753 return -EINVAL;
754
Eyal Shapira757cf23b2014-03-24 02:12:00 +0200755 if (tbl->column != RS_COLUMN_INVALID) {
Eyal Shapiraf58220f2014-08-16 01:30:30 +0300756 struct lq_sta_pers *pers = &lq_sta->pers;
757
758 pers->tx_stats[tbl->column][scale_index].total += attempts;
759 pers->tx_stats[tbl->column][scale_index].success += successes;
Eyal Shapira757cf23b2014-03-24 02:12:00 +0200760 }
761
Eliad Peller0bd3c5a2014-03-10 16:07:04 +0200762 /* Select window for current tx bit rate */
763 window = &(tbl->win[scale_index]);
Eyal Shapira834437d2014-12-17 15:38:58 +0200764 return _rs_collect_tx_data(mvm, tbl, scale_index, attempts, successes,
Eliad Peller0bd3c5a2014-03-10 16:07:04 +0200765 window);
766}
767
Eyal Shapira5aa33552013-11-23 01:06:36 +0200768/* Convert rs_rate object into ucode rate bitmask */
769static u32 ucode_rate_from_rs_rate(struct iwl_mvm *mvm,
Eyal Shapira8fc7c582013-12-04 02:15:46 +0200770 struct rs_rate *rate)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100771{
Eyal Shapira5aa33552013-11-23 01:06:36 +0200772 u32 ucode_rate = 0;
773 int index = rate->index;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100774
Eyal Shapira5aa33552013-11-23 01:06:36 +0200775 ucode_rate |= ((rate->ant << RATE_MCS_ANT_POS) &
Eyal Shapirad310e402013-08-11 18:43:47 +0300776 RATE_MCS_ANT_ABC_MSK);
777
Eyal Shapira5aa33552013-11-23 01:06:36 +0200778 if (is_legacy(rate)) {
779 ucode_rate |= iwl_rates[index].plcp;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100780 if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
Eyal Shapira5aa33552013-11-23 01:06:36 +0200781 ucode_rate |= RATE_MCS_CCK_MSK;
782 return ucode_rate;
Eyal Shapirad310e402013-08-11 18:43:47 +0300783 }
784
Eyal Shapira5aa33552013-11-23 01:06:36 +0200785 if (is_ht(rate)) {
Eyal Shapirad310e402013-08-11 18:43:47 +0300786 if (index < IWL_FIRST_HT_RATE || index > IWL_LAST_HT_RATE) {
Johannes Berg8ca151b2013-01-24 14:25:36 +0100787 IWL_ERR(mvm, "Invalid HT rate index %d\n", index);
Eyal Shapira4e82dd32013-08-04 23:58:37 +0300788 index = IWL_LAST_HT_RATE;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100789 }
Eyal Shapira5aa33552013-11-23 01:06:36 +0200790 ucode_rate |= RATE_MCS_HT_MSK;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100791
Eyal Shapira5aa33552013-11-23 01:06:36 +0200792 if (is_ht_siso(rate))
793 ucode_rate |= iwl_rates[index].plcp_ht_siso;
794 else if (is_ht_mimo2(rate))
795 ucode_rate |= iwl_rates[index].plcp_ht_mimo2;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100796 else
Eyal Shapirad972ab32013-07-28 23:02:45 +0000797 WARN_ON_ONCE(1);
Eyal Shapira5aa33552013-11-23 01:06:36 +0200798 } else if (is_vht(rate)) {
Eyal Shapirad310e402013-08-11 18:43:47 +0300799 if (index < IWL_FIRST_VHT_RATE || index > IWL_LAST_VHT_RATE) {
800 IWL_ERR(mvm, "Invalid VHT rate index %d\n", index);
801 index = IWL_LAST_VHT_RATE;
802 }
Eyal Shapira5aa33552013-11-23 01:06:36 +0200803 ucode_rate |= RATE_MCS_VHT_MSK;
804 if (is_vht_siso(rate))
805 ucode_rate |= iwl_rates[index].plcp_vht_siso;
806 else if (is_vht_mimo2(rate))
807 ucode_rate |= iwl_rates[index].plcp_vht_mimo2;
Eyal Shapirad310e402013-08-11 18:43:47 +0300808 else
809 WARN_ON_ONCE(1);
810
Johannes Berg8ca151b2013-01-24 14:25:36 +0100811 } else {
Eyal Shapira5aa33552013-11-23 01:06:36 +0200812 IWL_ERR(mvm, "Invalid rate->type %d\n", rate->type);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100813 }
814
Eyal Shapirad8bafea2014-10-23 13:58:38 +0300815 if (is_siso(rate) && rate->stbc) {
816 /* To enable STBC we need to set both a flag and ANT_AB */
817 ucode_rate |= RATE_MCS_ANT_AB_MSK;
818 ucode_rate |= RATE_MCS_VHT_STBC_MSK;
819 }
820
Eyal Shapira5aa33552013-11-23 01:06:36 +0200821 ucode_rate |= rate->bw;
822 if (rate->sgi)
823 ucode_rate |= RATE_MCS_SGI_MSK;
Eyal Shapiraa3576ff2014-08-09 10:57:59 +0300824 if (rate->ldpc)
825 ucode_rate |= RATE_MCS_LDPC_MSK;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100826
Eyal Shapira5aa33552013-11-23 01:06:36 +0200827 return ucode_rate;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100828}
829
Eyal Shapira5aa33552013-11-23 01:06:36 +0200830/* Convert a ucode rate into an rs_rate object */
831static int rs_rate_from_ucode_rate(const u32 ucode_rate,
Johannes Berg57fbcce2016-04-12 15:56:15 +0200832 enum nl80211_band band,
Eyal Shapira5aa33552013-11-23 01:06:36 +0200833 struct rs_rate *rate)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100834{
Eyal Shapira5aa33552013-11-23 01:06:36 +0200835 u32 ant_msk = ucode_rate & RATE_MCS_ANT_ABC_MSK;
836 u8 num_of_ant = get_num_of_ant_from_rate(ucode_rate);
Eyal Shapirad310e402013-08-11 18:43:47 +0300837 u8 nss;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100838
Emmanuel Grumbach9e898f12013-12-22 10:55:47 +0200839 memset(rate, 0, sizeof(*rate));
Eyal Shapira5aa33552013-11-23 01:06:36 +0200840 rate->index = iwl_hwrate_to_plcp_idx(ucode_rate);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100841
Eyal Shapira560843f2014-01-05 21:04:19 +0200842 if (rate->index == IWL_RATE_INVALID)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100843 return -EINVAL;
Eyal Shapira5aa33552013-11-23 01:06:36 +0200844
845 rate->ant = (ant_msk >> RATE_MCS_ANT_POS);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100846
Eyal Shapirad310e402013-08-11 18:43:47 +0300847 /* Legacy */
Eyal Shapira5aa33552013-11-23 01:06:36 +0200848 if (!(ucode_rate & RATE_MCS_HT_MSK) &&
849 !(ucode_rate & RATE_MCS_VHT_MSK)) {
Johannes Berg8ca151b2013-01-24 14:25:36 +0100850 if (num_of_ant == 1) {
Johannes Berg57fbcce2016-04-12 15:56:15 +0200851 if (band == NL80211_BAND_5GHZ)
Eyal Shapira5aa33552013-11-23 01:06:36 +0200852 rate->type = LQ_LEGACY_A;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100853 else
Eyal Shapira5aa33552013-11-23 01:06:36 +0200854 rate->type = LQ_LEGACY_G;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100855 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100856
Eyal Shapirad310e402013-08-11 18:43:47 +0300857 return 0;
858 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100859
Eyal Shapirad310e402013-08-11 18:43:47 +0300860 /* HT or VHT */
Eyal Shapira5aa33552013-11-23 01:06:36 +0200861 if (ucode_rate & RATE_MCS_SGI_MSK)
862 rate->sgi = true;
Eyal Shapiraa3576ff2014-08-09 10:57:59 +0300863 if (ucode_rate & RATE_MCS_LDPC_MSK)
864 rate->ldpc = true;
Eyal Shapirad8bafea2014-10-23 13:58:38 +0300865 if (ucode_rate & RATE_MCS_VHT_STBC_MSK)
866 rate->stbc = true;
Eyal Shapiraba69d0e2015-01-30 01:38:29 +0200867 if (ucode_rate & RATE_MCS_BF_MSK)
868 rate->bfer = true;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100869
Eyal Shapira5aa33552013-11-23 01:06:36 +0200870 rate->bw = ucode_rate & RATE_MCS_CHAN_WIDTH_MSK;
Eyal Shapirad310e402013-08-11 18:43:47 +0300871
Eyal Shapira5aa33552013-11-23 01:06:36 +0200872 if (ucode_rate & RATE_MCS_HT_MSK) {
873 nss = ((ucode_rate & RATE_HT_MCS_NSS_MSK) >>
Eyal Shapirad310e402013-08-11 18:43:47 +0300874 RATE_HT_MCS_NSS_POS) + 1;
875
876 if (nss == 1) {
Eyal Shapira5aa33552013-11-23 01:06:36 +0200877 rate->type = LQ_HT_SISO;
Eyal Shapiraba69d0e2015-01-30 01:38:29 +0200878 WARN_ONCE(!rate->stbc && !rate->bfer && num_of_ant != 1,
879 "stbc %d bfer %d",
880 rate->stbc, rate->bfer);
Eyal Shapirad310e402013-08-11 18:43:47 +0300881 } else if (nss == 2) {
Eyal Shapira5aa33552013-11-23 01:06:36 +0200882 rate->type = LQ_HT_MIMO2;
Eyal Shapirad310e402013-08-11 18:43:47 +0300883 WARN_ON_ONCE(num_of_ant != 2);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100884 } else {
Eyal Shapirad310e402013-08-11 18:43:47 +0300885 WARN_ON_ONCE(1);
886 }
Eyal Shapira5aa33552013-11-23 01:06:36 +0200887 } else if (ucode_rate & RATE_MCS_VHT_MSK) {
888 nss = ((ucode_rate & RATE_VHT_MCS_NSS_MSK) >>
Eyal Shapirad310e402013-08-11 18:43:47 +0300889 RATE_VHT_MCS_NSS_POS) + 1;
890
891 if (nss == 1) {
Eyal Shapira5aa33552013-11-23 01:06:36 +0200892 rate->type = LQ_VHT_SISO;
Eyal Shapiraba69d0e2015-01-30 01:38:29 +0200893 WARN_ONCE(!rate->stbc && !rate->bfer && num_of_ant != 1,
894 "stbc %d bfer %d",
895 rate->stbc, rate->bfer);
Eyal Shapirad310e402013-08-11 18:43:47 +0300896 } else if (nss == 2) {
Eyal Shapira5aa33552013-11-23 01:06:36 +0200897 rate->type = LQ_VHT_MIMO2;
Eyal Shapirad310e402013-08-11 18:43:47 +0300898 WARN_ON_ONCE(num_of_ant != 2);
899 } else {
900 WARN_ON_ONCE(1);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100901 }
902 }
Eyal Shapirad310e402013-08-11 18:43:47 +0300903
Eyal Shapira5aa33552013-11-23 01:06:36 +0200904 WARN_ON_ONCE(rate->bw == RATE_MCS_CHAN_WIDTH_160);
905 WARN_ON_ONCE(rate->bw == RATE_MCS_CHAN_WIDTH_80 &&
906 !is_vht(rate));
Eyal Shapirad310e402013-08-11 18:43:47 +0300907
Johannes Berg8ca151b2013-01-24 14:25:36 +0100908 return 0;
909}
910
911/* switch to another antenna/antennas and return 1 */
912/* if no other valid antenna found, return 0 */
Eyal Shapira8fc7c582013-12-04 02:15:46 +0200913static int rs_toggle_antenna(u32 valid_ant, struct rs_rate *rate)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100914{
915 u8 new_ant_type;
916
Eyal Shapira5aa33552013-11-23 01:06:36 +0200917 if (!rate->ant || rate->ant > ANT_ABC)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100918 return 0;
919
Eyal Shapira5aa33552013-11-23 01:06:36 +0200920 if (!rs_is_valid_ant(valid_ant, rate->ant))
Johannes Berg8ca151b2013-01-24 14:25:36 +0100921 return 0;
922
Eyal Shapira5aa33552013-11-23 01:06:36 +0200923 new_ant_type = ant_toggle_lookup[rate->ant];
Johannes Berg8ca151b2013-01-24 14:25:36 +0100924
Eyal Shapira5aa33552013-11-23 01:06:36 +0200925 while ((new_ant_type != rate->ant) &&
Johannes Berg8ca151b2013-01-24 14:25:36 +0100926 !rs_is_valid_ant(valid_ant, new_ant_type))
927 new_ant_type = ant_toggle_lookup[new_ant_type];
928
Eyal Shapira5aa33552013-11-23 01:06:36 +0200929 if (new_ant_type == rate->ant)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100930 return 0;
931
Eyal Shapira5aa33552013-11-23 01:06:36 +0200932 rate->ant = new_ant_type;
933
Johannes Berg8ca151b2013-01-24 14:25:36 +0100934 return 1;
935}
936
Johannes Berg8ca151b2013-01-24 14:25:36 +0100937static u16 rs_get_supported_rates(struct iwl_lq_sta *lq_sta,
Eyal Shapira5aa33552013-11-23 01:06:36 +0200938 struct rs_rate *rate)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100939{
Eyal Shapira5aa33552013-11-23 01:06:36 +0200940 if (is_legacy(rate))
Johannes Berg8ca151b2013-01-24 14:25:36 +0100941 return lq_sta->active_legacy_rate;
Eyal Shapira5aa33552013-11-23 01:06:36 +0200942 else if (is_siso(rate))
Eyal Shapirad310e402013-08-11 18:43:47 +0300943 return lq_sta->active_siso_rate;
Eyal Shapira5aa33552013-11-23 01:06:36 +0200944 else if (is_mimo2(rate))
Eyal Shapirad310e402013-08-11 18:43:47 +0300945 return lq_sta->active_mimo2_rate;
946
947 WARN_ON_ONCE(1);
948 return 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100949}
950
951static u16 rs_get_adjacent_rate(struct iwl_mvm *mvm, u8 index, u16 rate_mask,
952 int rate_type)
953{
954 u8 high = IWL_RATE_INVALID;
955 u8 low = IWL_RATE_INVALID;
956
957 /* 802.11A or ht walks to the next literal adjacent rate in
958 * the rate table */
Eyal Shapira5aa33552013-11-23 01:06:36 +0200959 if (is_type_a_band(rate_type) || !is_type_legacy(rate_type)) {
Johannes Berg8ca151b2013-01-24 14:25:36 +0100960 int i;
961 u32 mask;
962
963 /* Find the previous rate that is in the rate mask */
964 i = index - 1;
965 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
966 if (rate_mask & mask) {
967 low = i;
968 break;
969 }
970 }
971
972 /* Find the next rate that is in the rate mask */
973 i = index + 1;
974 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
975 if (rate_mask & mask) {
976 high = i;
977 break;
978 }
979 }
980
981 return (high << 8) | low;
982 }
983
984 low = index;
985 while (low != IWL_RATE_INVALID) {
986 low = iwl_rates[low].prev_rs;
987 if (low == IWL_RATE_INVALID)
988 break;
989 if (rate_mask & (1 << low))
990 break;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100991 }
992
993 high = index;
994 while (high != IWL_RATE_INVALID) {
995 high = iwl_rates[high].next_rs;
996 if (high == IWL_RATE_INVALID)
997 break;
998 if (rate_mask & (1 << high))
999 break;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001000 }
1001
1002 return (high << 8) | low;
1003}
1004
Eyal Shapirae07be6d2013-12-09 13:02:56 +02001005static inline bool rs_rate_supported(struct iwl_lq_sta *lq_sta,
1006 struct rs_rate *rate)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001007{
Eyal Shapirae07be6d2013-12-09 13:02:56 +02001008 return BIT(rate->index) & rs_get_supported_rates(lq_sta, rate);
1009}
1010
1011/* Get the next supported lower rate in the current column.
1012 * Return true if bottom rate in the current column was reached
1013 */
1014static bool rs_get_lower_rate_in_column(struct iwl_lq_sta *lq_sta,
1015 struct rs_rate *rate)
1016{
1017 u8 low;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001018 u16 high_low;
Eyal Shapirae07be6d2013-12-09 13:02:56 +02001019 u16 rate_mask;
Eliad Pellerae969af2014-06-11 15:51:33 +03001020 struct iwl_mvm *mvm = lq_sta->pers.drv;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001021
Eyal Shapirae07be6d2013-12-09 13:02:56 +02001022 rate_mask = rs_get_supported_rates(lq_sta, rate);
1023 high_low = rs_get_adjacent_rate(mvm, rate->index, rate_mask,
1024 rate->type);
1025 low = high_low & 0xff;
1026
1027 /* Bottom rate of column reached */
1028 if (low == IWL_RATE_INVALID)
1029 return true;
1030
1031 rate->index = low;
1032 return false;
1033}
1034
1035/* Get the next rate to use following a column downgrade */
1036static void rs_get_lower_rate_down_column(struct iwl_lq_sta *lq_sta,
1037 struct rs_rate *rate)
1038{
Eliad Pellerae969af2014-06-11 15:51:33 +03001039 struct iwl_mvm *mvm = lq_sta->pers.drv;
Eyal Shapirae07be6d2013-12-09 13:02:56 +02001040
1041 if (is_legacy(rate)) {
1042 /* No column to downgrade from Legacy */
1043 return;
1044 } else if (is_siso(rate)) {
1045 /* Downgrade to Legacy if we were in SISO */
Johannes Berg57fbcce2016-04-12 15:56:15 +02001046 if (lq_sta->band == NL80211_BAND_5GHZ)
Eyal Shapira5aa33552013-11-23 01:06:36 +02001047 rate->type = LQ_LEGACY_A;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001048 else
Eyal Shapira5aa33552013-11-23 01:06:36 +02001049 rate->type = LQ_LEGACY_G;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001050
Eyal Shapiraa56db7d2013-11-28 12:35:42 +02001051 rate->bw = RATE_MCS_CHAN_WIDTH_20;
Eyal Shapirae07be6d2013-12-09 13:02:56 +02001052
Emmanuel Grumbachb8000402014-02-03 22:17:19 +02001053 WARN_ON_ONCE(rate->index < IWL_RATE_MCS_0_INDEX ||
Eyal Shapirae07be6d2013-12-09 13:02:56 +02001054 rate->index > IWL_RATE_MCS_9_INDEX);
1055
1056 rate->index = rs_ht_to_legacy[rate->index];
Eyal Shapiraa3576ff2014-08-09 10:57:59 +03001057 rate->ldpc = false;
Eyal Shapirae07be6d2013-12-09 13:02:56 +02001058 } else {
1059 /* Downgrade to SISO with same MCS if in MIMO */
1060 rate->type = is_vht_mimo2(rate) ?
1061 LQ_VHT_SISO : LQ_HT_SISO;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001062 }
1063
Eyal Shapirae07be6d2013-12-09 13:02:56 +02001064 if (num_of_ant(rate->ant) > 1)
Moshe Harela0544272014-12-08 21:13:14 +02001065 rate->ant = first_antenna(iwl_mvm_get_valid_tx_ant(mvm));
Johannes Berg8ca151b2013-01-24 14:25:36 +01001066
Eyal Shapirae07be6d2013-12-09 13:02:56 +02001067 /* Relevant in both switching to SISO or Legacy */
1068 rate->sgi = false;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001069
Eyal Shapirae07be6d2013-12-09 13:02:56 +02001070 if (!rs_rate_supported(lq_sta, rate))
1071 rs_get_lower_rate_in_column(lq_sta, rate);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001072}
1073
Eyal Shapiraba69d0e2015-01-30 01:38:29 +02001074/* Check if both rates are identical
1075 * allow_ant_mismatch enables matching a SISO rate on ANT_A or ANT_B
1076 * with a rate indicating STBC/BFER and ANT_AB.
1077 */
Eyal Shapira929e6ed2015-01-30 13:40:02 +02001078static inline bool rs_rate_equal(struct rs_rate *a,
Eyal Shapiraba69d0e2015-01-30 01:38:29 +02001079 struct rs_rate *b,
1080 bool allow_ant_mismatch)
1081
Johannes Berg8ca151b2013-01-24 14:25:36 +01001082{
Eyal Shapiraba69d0e2015-01-30 01:38:29 +02001083 bool ant_match = (a->ant == b->ant) && (a->stbc == b->stbc) &&
1084 (a->bfer == b->bfer);
1085
1086 if (allow_ant_mismatch) {
1087 if (a->stbc || a->bfer) {
1088 WARN_ONCE(a->ant != ANT_AB, "stbc %d bfer %d ant %d",
1089 a->stbc, a->bfer, a->ant);
1090 ant_match |= (b->ant == ANT_A || b->ant == ANT_B);
1091 } else if (b->stbc || b->bfer) {
1092 WARN_ONCE(b->ant != ANT_AB, "stbc %d bfer %d ant %d",
1093 b->stbc, b->bfer, b->ant);
1094 ant_match |= (a->ant == ANT_A || a->ant == ANT_B);
1095 }
1096 }
1097
Eyal Shapira929e6ed2015-01-30 13:40:02 +02001098 return (a->type == b->type) && (a->bw == b->bw) && (a->sgi == b->sgi) &&
Eyal Shapiraba69d0e2015-01-30 01:38:29 +02001099 (a->ldpc == b->ldpc) && (a->index == b->index) && ant_match;
Eyal Shapira929e6ed2015-01-30 13:40:02 +02001100}
1101
1102/* Check if both rates share the same column */
1103static inline bool rs_rate_column_match(struct rs_rate *a,
1104 struct rs_rate *b)
1105{
Eyal Shapirad8bafea2014-10-23 13:58:38 +03001106 bool ant_match;
1107
Eyal Shapiraba69d0e2015-01-30 01:38:29 +02001108 if (a->stbc || a->bfer)
Eyal Shapirad8bafea2014-10-23 13:58:38 +03001109 ant_match = (b->ant == ANT_A || b->ant == ANT_B);
1110 else
1111 ant_match = (a->ant == b->ant);
1112
1113 return (a->type == b->type) && (a->bw == b->bw) && (a->sgi == b->sgi)
1114 && ant_match;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001115}
1116
Eyal Shapiraad82d8a2015-03-08 19:43:41 +02001117static inline enum rs_column rs_get_column_from_rate(struct rs_rate *rate)
1118{
1119 if (is_legacy(rate)) {
1120 if (rate->ant == ANT_A)
1121 return RS_COLUMN_LEGACY_ANT_A;
1122
1123 if (rate->ant == ANT_B)
1124 return RS_COLUMN_LEGACY_ANT_B;
1125
1126 goto err;
1127 }
1128
1129 if (is_siso(rate)) {
1130 if (rate->ant == ANT_A || rate->stbc || rate->bfer)
1131 return rate->sgi ? RS_COLUMN_SISO_ANT_A_SGI :
1132 RS_COLUMN_SISO_ANT_A;
1133
1134 if (rate->ant == ANT_B)
1135 return rate->sgi ? RS_COLUMN_SISO_ANT_B_SGI :
1136 RS_COLUMN_SISO_ANT_B;
1137
1138 goto err;
1139 }
1140
1141 if (is_mimo(rate))
1142 return rate->sgi ? RS_COLUMN_MIMO2_SGI : RS_COLUMN_MIMO2;
1143
1144err:
1145 return RS_COLUMN_INVALID;
1146}
1147
Eyal Shapira1ebc8f22014-09-14 15:23:23 +03001148static u8 rs_get_tid(struct ieee80211_hdr *hdr)
1149{
1150 u8 tid = IWL_MAX_TID_COUNT;
1151
1152 if (ieee80211_is_data_qos(hdr->frame_control)) {
1153 u8 *qc = ieee80211_get_qos_ctl(hdr);
1154 tid = qc[0] & 0xf;
1155 }
1156
1157 if (unlikely(tid > IWL_MAX_TID_COUNT))
1158 tid = IWL_MAX_TID_COUNT;
1159
1160 return tid;
1161}
1162
1163void iwl_mvm_rs_tx_status(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
Matti Gottliebff7a68d2016-05-17 10:50:08 +03001164 int tid, struct ieee80211_tx_info *info, bool ndp)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001165{
1166 int legacy_success;
1167 int retries;
Eyal Shapira929e6ed2015-01-30 13:40:02 +02001168 int i;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001169 struct iwl_lq_cmd *table;
Eyal Shapira929e6ed2015-01-30 13:40:02 +02001170 u32 lq_hwrate;
1171 struct rs_rate lq_rate, tx_resp_rate;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001172 struct iwl_scale_tbl_info *curr_tbl, *other_tbl, *tmp_tbl;
Eliad Peller2fd647f2014-03-13 17:21:36 +02001173 u8 reduced_txp = (uintptr_t)info->status.status_driver_data[0];
Eyal Shapira929e6ed2015-01-30 13:40:02 +02001174 u32 tx_resp_hwrate = (uintptr_t)info->status.status_driver_data[1];
Eyal Shapira1ebc8f22014-09-14 15:23:23 +03001175 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1176 struct iwl_lq_sta *lq_sta = &mvmsta->lq_sta;
Johannes Berg859d9142015-06-01 17:11:11 +02001177 bool allow_ant_mismatch = fw_has_api(&mvm->fw->ucode_capa,
1178 IWL_UCODE_TLV_API_LQ_SS_PARAMS);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001179
Johannes Berg8ca151b2013-01-24 14:25:36 +01001180 /* Treat uninitialized rate scaling data same as non-existing. */
1181 if (!lq_sta) {
1182 IWL_DEBUG_RATE(mvm, "Station rate scaling not created yet.\n");
1183 return;
Eliad Pellerae969af2014-06-11 15:51:33 +03001184 } else if (!lq_sta->pers.drv) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001185 IWL_DEBUG_RATE(mvm, "Rate scaling not initialized yet.\n");
1186 return;
1187 }
1188
Johannes Berg8ca151b2013-01-24 14:25:36 +01001189 /* This packet was aggregated but doesn't carry status info */
1190 if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
1191 !(info->flags & IEEE80211_TX_STAT_AMPDU))
1192 return;
1193
Eyal Shapiraad82d8a2015-03-08 19:43:41 +02001194 rs_rate_from_ucode_rate(tx_resp_hwrate, info->band, &tx_resp_rate);
1195
1196#ifdef CONFIG_MAC80211_DEBUGFS
1197 /* Disable last tx check if we are debugging with fixed rate but
1198 * update tx stats */
1199 if (lq_sta->pers.dbg_fixed_rate) {
1200 int index = tx_resp_rate.index;
1201 enum rs_column column;
1202 int attempts, success;
1203
1204 column = rs_get_column_from_rate(&tx_resp_rate);
1205 if (WARN_ONCE(column == RS_COLUMN_INVALID,
1206 "Can't map rate 0x%x to column",
1207 tx_resp_hwrate))
1208 return;
1209
1210 if (info->flags & IEEE80211_TX_STAT_AMPDU) {
1211 attempts = info->status.ampdu_len;
1212 success = info->status.ampdu_ack_len;
1213 } else {
1214 attempts = info->status.rates[0].count;
1215 success = !!(info->flags & IEEE80211_TX_STAT_ACK);
1216 }
1217
1218 lq_sta->pers.tx_stats[column][index].total += attempts;
1219 lq_sta->pers.tx_stats[column][index].success += success;
1220
1221 IWL_DEBUG_RATE(mvm, "Fixed rate 0x%x success %d attempts %d\n",
1222 tx_resp_hwrate, success, attempts);
1223 return;
1224 }
1225#endif
1226
Eyal Shapira87d5e412014-04-06 18:13:22 +03001227 if (time_after(jiffies,
Eyal Shapira834437d2014-12-17 15:38:58 +02001228 (unsigned long)(lq_sta->last_tx +
1229 (IWL_MVM_RS_IDLE_TIMEOUT * HZ)))) {
Johannes Bergefad21d22014-11-03 15:51:47 +01001230 int t;
1231
Eyal Shapira87d5e412014-04-06 18:13:22 +03001232 IWL_DEBUG_RATE(mvm, "Tx idle for too long. reinit rs\n");
Johannes Bergefad21d22014-11-03 15:51:47 +01001233 for (t = 0; t < IWL_MAX_TID_COUNT; t++)
1234 ieee80211_stop_tx_ba_session(sta, t);
Eyal Shapira87d5e412014-04-06 18:13:22 +03001235
Eyal Shapira1ebc8f22014-09-14 15:23:23 +03001236 iwl_mvm_rs_rate_init(mvm, sta, info->band, false);
Eyal Shapira87d5e412014-04-06 18:13:22 +03001237 return;
1238 }
1239 lq_sta->last_tx = jiffies;
1240
Eyal Shapira929e6ed2015-01-30 13:40:02 +02001241 /* Ignore this Tx frame response if its initial rate doesn't match
1242 * that of latest Link Quality command. There may be stragglers
1243 * from a previous Link Quality command, but we're no longer interested
1244 * in those; they're either from the "active" mode while we're trying
1245 * to check "search" mode, or a prior "search" mode after we've moved
1246 * to a new "search" mode (which might become the new "active" mode).
1247 */
1248 table = &lq_sta->lq;
1249 lq_hwrate = le32_to_cpu(table->rs_table[0]);
1250 rs_rate_from_ucode_rate(lq_hwrate, info->band, &lq_rate);
Eyal Shapira929e6ed2015-01-30 13:40:02 +02001251
Johannes Berg8ca151b2013-01-24 14:25:36 +01001252 /* Here we actually compare this rate to the latest LQ command */
Eyal Shapiraba69d0e2015-01-30 01:38:29 +02001253 if (!rs_rate_equal(&tx_resp_rate, &lq_rate, allow_ant_mismatch)) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001254 IWL_DEBUG_RATE(mvm,
Eyal Shapira929e6ed2015-01-30 13:40:02 +02001255 "initial tx resp rate 0x%x does not match 0x%x\n",
1256 tx_resp_hwrate, lq_hwrate);
1257
Johannes Berg8ca151b2013-01-24 14:25:36 +01001258 /*
1259 * Since rates mis-match, the last LQ command may have failed.
1260 * After IWL_MISSED_RATE_MAX mis-matches, resync the uCode with
1261 * ... driver.
1262 */
1263 lq_sta->missed_rate_counter++;
Eyal Shapira834437d2014-12-17 15:38:58 +02001264 if (lq_sta->missed_rate_counter > IWL_MVM_RS_MISSED_RATE_MAX) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001265 lq_sta->missed_rate_counter = 0;
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001266 IWL_DEBUG_RATE(mvm,
1267 "Too many rates mismatch. Send sync LQ. rs_state %d\n",
1268 lq_sta->rs_state);
Eyal Shapira9e680942013-11-09 00:16:16 +02001269 iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, false);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001270 }
1271 /* Regardless, ignore this status info for outdated rate */
1272 return;
1273 } else
1274 /* Rate did match, so reset the missed_rate_counter */
1275 lq_sta->missed_rate_counter = 0;
1276
Eyal Shapira26a2cc02014-11-05 12:03:59 +02001277 if (!lq_sta->search_better_tbl) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001278 curr_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1279 other_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
Eyal Shapira26a2cc02014-11-05 12:03:59 +02001280 } else {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001281 curr_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
1282 other_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
Eyal Shapira26a2cc02014-11-05 12:03:59 +02001283 }
1284
Eyal Shapira929e6ed2015-01-30 13:40:02 +02001285 if (WARN_ON_ONCE(!rs_rate_column_match(&lq_rate, &curr_tbl->rate))) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001286 IWL_DEBUG_RATE(mvm,
1287 "Neither active nor search matches tx rate\n");
1288 tmp_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
Eyal Shapira5aa33552013-11-23 01:06:36 +02001289 rs_dump_rate(mvm, &tmp_tbl->rate, "ACTIVE");
Johannes Berg8ca151b2013-01-24 14:25:36 +01001290 tmp_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
Eyal Shapira5aa33552013-11-23 01:06:36 +02001291 rs_dump_rate(mvm, &tmp_tbl->rate, "SEARCH");
Eyal Shapira929e6ed2015-01-30 13:40:02 +02001292 rs_dump_rate(mvm, &lq_rate, "ACTUAL");
Eyal Shapira5aa33552013-11-23 01:06:36 +02001293
Johannes Berg8ca151b2013-01-24 14:25:36 +01001294 /*
1295 * no matching table found, let's by-pass the data collection
1296 * and continue to perform rate scale to find the rate table
1297 */
1298 rs_stay_in_table(lq_sta, true);
1299 goto done;
1300 }
1301
1302 /*
1303 * Updating the frame history depends on whether packets were
1304 * aggregated.
1305 *
1306 * For aggregation, all packets were transmitted at the same rate, the
1307 * first index into rate scale table.
1308 */
1309 if (info->flags & IEEE80211_TX_STAT_AMPDU) {
Gregory Greenman69c7fda2015-12-29 11:26:35 +02001310 rs_collect_tpc_data(mvm, lq_sta, curr_tbl, lq_rate.index,
1311 info->status.ampdu_len,
1312 info->status.ampdu_ack_len,
1313 reduced_txp);
1314
1315 /* ampdu_ack_len = 0 marks no BA was received. For TLC, treat
1316 * it as a single frame loss as we don't want the success ratio
1317 * to dip too quickly because a BA wasn't received.
1318 * For TPC, there's no need for this optimisation since we want
1319 * to recover very quickly from a bad power reduction and,
1320 * therefore we'd like the success ratio to get an immediate hit
1321 * when failing to get a BA, so we'd switch back to a lower or
1322 * zero power reduction. When FW transmits agg with a rate
1323 * different from the initial rate, it will not use reduced txp
1324 * and will send BA notification twice (one empty with reduced
1325 * txp equal to the value from LQ and one with reduced txp 0).
1326 * We need to update counters for each txp level accordingly.
Eyal Shapirad6bda662014-11-12 13:30:20 +02001327 */
1328 if (info->status.ampdu_ack_len == 0)
1329 info->status.ampdu_len = 1;
1330
Gregory Greenman69c7fda2015-12-29 11:26:35 +02001331 rs_collect_tlc_data(mvm, lq_sta, curr_tbl, lq_rate.index,
1332 info->status.ampdu_len,
1333 info->status.ampdu_ack_len);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001334
1335 /* Update success/fail counts if not searching for new mode */
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001336 if (lq_sta->rs_state == RS_STATE_STAY_IN_COLUMN) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001337 lq_sta->total_success += info->status.ampdu_ack_len;
1338 lq_sta->total_failed += (info->status.ampdu_len -
1339 info->status.ampdu_ack_len);
1340 }
1341 } else {
Liad Kaufman90cb1232015-03-29 11:39:34 +03001342 /* For legacy, update frame history with for each Tx retry. */
Johannes Berg8ca151b2013-01-24 14:25:36 +01001343 retries = info->status.rates[0].count - 1;
1344 /* HW doesn't send more than 15 retries */
1345 retries = min(retries, 15);
1346
1347 /* The last transmission may have been successful */
1348 legacy_success = !!(info->flags & IEEE80211_TX_STAT_ACK);
1349 /* Collect data for each rate used during failed TX attempts */
1350 for (i = 0; i <= retries; ++i) {
Eyal Shapira929e6ed2015-01-30 13:40:02 +02001351 lq_hwrate = le32_to_cpu(table->rs_table[i]);
1352 rs_rate_from_ucode_rate(lq_hwrate, info->band,
1353 &lq_rate);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001354 /*
1355 * Only collect stats if retried rate is in the same RS
1356 * table as active/search.
1357 */
Eyal Shapira929e6ed2015-01-30 13:40:02 +02001358 if (rs_rate_column_match(&lq_rate, &curr_tbl->rate))
Johannes Berg8ca151b2013-01-24 14:25:36 +01001359 tmp_tbl = curr_tbl;
Eyal Shapira929e6ed2015-01-30 13:40:02 +02001360 else if (rs_rate_column_match(&lq_rate,
1361 &other_tbl->rate))
Johannes Berg8ca151b2013-01-24 14:25:36 +01001362 tmp_tbl = other_tbl;
Eyal Shapira4107dbd2013-12-10 12:20:37 +02001363 else
Johannes Berg8ca151b2013-01-24 14:25:36 +01001364 continue;
Eyal Shapira4107dbd2013-12-10 12:20:37 +02001365
Gregory Greenman69c7fda2015-12-29 11:26:35 +02001366 rs_collect_tpc_data(mvm, lq_sta, tmp_tbl,
1367 lq_rate.index, 1,
1368 i < retries ? 0 : legacy_success,
1369 reduced_txp);
1370 rs_collect_tlc_data(mvm, lq_sta, tmp_tbl,
1371 lq_rate.index, 1,
1372 i < retries ? 0 : legacy_success);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001373 }
1374
1375 /* Update success/fail counts if not searching for new mode */
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001376 if (lq_sta->rs_state == RS_STATE_STAY_IN_COLUMN) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001377 lq_sta->total_success += legacy_success;
1378 lq_sta->total_failed += retries + (1 - legacy_success);
1379 }
1380 }
1381 /* The last TX rate is cached in lq_sta; it's set in if/else above */
Eyal Shapira929e6ed2015-01-30 13:40:02 +02001382 lq_sta->last_rate_n_flags = lq_hwrate;
Eliad Peller2fd647f2014-03-13 17:21:36 +02001383 IWL_DEBUG_RATE(mvm, "reduced txpower: %d\n", reduced_txp);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001384done:
1385 /* See if there's a better rate or modulation mode to try. */
Emmanuel Grumbach8dfc2752014-09-21 13:29:48 +03001386 if (sta->supp_rates[info->band])
Matti Gottliebff7a68d2016-05-17 10:50:08 +03001387 rs_rate_scale_perform(mvm, sta, lq_sta, tid, ndp);
Eyal Shapira1ebc8f22014-09-14 15:23:23 +03001388}
1389
1390/*
1391 * mac80211 sends us Tx status
1392 */
1393static void rs_mac80211_tx_status(void *mvm_r,
1394 struct ieee80211_supported_band *sband,
1395 struct ieee80211_sta *sta, void *priv_sta,
1396 struct sk_buff *skb)
1397{
1398 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1399 struct iwl_op_mode *op_mode = (struct iwl_op_mode *)mvm_r;
1400 struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1401 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1402
Johannes Bergdcbc17a2015-03-10 21:24:34 +01001403 if (!iwl_mvm_sta_from_mac80211(sta)->vif)
1404 return;
1405
Eyal Shapira1ebc8f22014-09-14 15:23:23 +03001406 if (!ieee80211_is_data(hdr->frame_control) ||
1407 info->flags & IEEE80211_TX_CTL_NO_ACK)
1408 return;
1409
Matti Gottliebff7a68d2016-05-17 10:50:08 +03001410 iwl_mvm_rs_tx_status(mvm, sta, rs_get_tid(hdr), info,
1411 ieee80211_is_qos_nullfunc(hdr->frame_control));
Johannes Berg8ca151b2013-01-24 14:25:36 +01001412}
1413
1414/*
1415 * Begin a period of staying with a selected modulation mode.
1416 * Set "stay_in_tbl" flag to prevent any mode switches.
1417 * Set frame tx success limits according to legacy vs. high-throughput,
1418 * and reset overall (spanning all rates) tx success history statistics.
1419 * These control how long we stay using same modulation mode before
1420 * searching for a new mode.
1421 */
1422static void rs_set_stay_in_table(struct iwl_mvm *mvm, u8 is_legacy,
1423 struct iwl_lq_sta *lq_sta)
1424{
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001425 IWL_DEBUG_RATE(mvm, "Moving to RS_STATE_STAY_IN_COLUMN\n");
1426 lq_sta->rs_state = RS_STATE_STAY_IN_COLUMN;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001427 if (is_legacy) {
Eyal Shapira834437d2014-12-17 15:38:58 +02001428 lq_sta->table_count_limit = IWL_MVM_RS_LEGACY_TABLE_COUNT;
1429 lq_sta->max_failure_limit = IWL_MVM_RS_LEGACY_FAILURE_LIMIT;
1430 lq_sta->max_success_limit = IWL_MVM_RS_LEGACY_SUCCESS_LIMIT;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001431 } else {
Eyal Shapira834437d2014-12-17 15:38:58 +02001432 lq_sta->table_count_limit = IWL_MVM_RS_NON_LEGACY_TABLE_COUNT;
1433 lq_sta->max_failure_limit = IWL_MVM_RS_NON_LEGACY_FAILURE_LIMIT;
1434 lq_sta->max_success_limit = IWL_MVM_RS_NON_LEGACY_SUCCESS_LIMIT;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001435 }
1436 lq_sta->table_count = 0;
1437 lq_sta->total_failed = 0;
1438 lq_sta->total_success = 0;
1439 lq_sta->flush_timer = jiffies;
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001440 lq_sta->visited_columns = 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001441}
1442
Eyal Shapira6a028d92014-12-07 18:18:27 +02001443static inline int rs_get_max_rate_from_mask(unsigned long rate_mask)
1444{
1445 if (rate_mask)
1446 return find_last_bit(&rate_mask, BITS_PER_LONG);
1447 return IWL_RATE_INVALID;
1448}
1449
Eyal Shapira198266a2014-03-31 22:37:39 +03001450static int rs_get_max_allowed_rate(struct iwl_lq_sta *lq_sta,
1451 const struct rs_tx_column *column)
1452{
1453 switch (column->mode) {
1454 case RS_LEGACY:
1455 return lq_sta->max_legacy_rate_idx;
1456 case RS_SISO:
1457 return lq_sta->max_siso_rate_idx;
1458 case RS_MIMO2:
1459 return lq_sta->max_mimo2_rate_idx;
1460 default:
1461 WARN_ON_ONCE(1);
1462 }
1463
1464 return lq_sta->max_legacy_rate_idx;
1465}
1466
Johannes Berga34529e2014-01-20 22:57:21 +01001467static const u16 *rs_get_expected_tpt_table(struct iwl_lq_sta *lq_sta,
Eyal Shapira198266a2014-03-31 22:37:39 +03001468 const struct rs_tx_column *column,
1469 u32 bw)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001470{
1471 /* Used to choose among HT tables */
Johannes Berga34529e2014-01-20 22:57:21 +01001472 const u16 (*ht_tbl_pointer)[IWL_RATE_COUNT];
Johannes Berg8ca151b2013-01-24 14:25:36 +01001473
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001474 if (WARN_ON_ONCE(column->mode != RS_LEGACY &&
1475 column->mode != RS_SISO &&
1476 column->mode != RS_MIMO2))
1477 return expected_tpt_legacy;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001478
1479 /* Legacy rates have only one table */
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001480 if (column->mode == RS_LEGACY)
1481 return expected_tpt_legacy;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001482
Eyal Shapirad310e402013-08-11 18:43:47 +03001483 ht_tbl_pointer = expected_tpt_mimo2_20MHz;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001484 /* Choose among many HT tables depending on number of streams
Eyal Shapirad310e402013-08-11 18:43:47 +03001485 * (SISO/MIMO2), channel width (20/40/80), SGI, and aggregation
Johannes Berg8ca151b2013-01-24 14:25:36 +01001486 * status */
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001487 if (column->mode == RS_SISO) {
1488 switch (bw) {
Eyal Shapirad310e402013-08-11 18:43:47 +03001489 case RATE_MCS_CHAN_WIDTH_20:
1490 ht_tbl_pointer = expected_tpt_siso_20MHz;
1491 break;
1492 case RATE_MCS_CHAN_WIDTH_40:
1493 ht_tbl_pointer = expected_tpt_siso_40MHz;
1494 break;
1495 case RATE_MCS_CHAN_WIDTH_80:
1496 ht_tbl_pointer = expected_tpt_siso_80MHz;
1497 break;
1498 default:
1499 WARN_ON_ONCE(1);
1500 }
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001501 } else if (column->mode == RS_MIMO2) {
1502 switch (bw) {
Eyal Shapirad310e402013-08-11 18:43:47 +03001503 case RATE_MCS_CHAN_WIDTH_20:
1504 ht_tbl_pointer = expected_tpt_mimo2_20MHz;
1505 break;
1506 case RATE_MCS_CHAN_WIDTH_40:
1507 ht_tbl_pointer = expected_tpt_mimo2_40MHz;
1508 break;
1509 case RATE_MCS_CHAN_WIDTH_80:
1510 ht_tbl_pointer = expected_tpt_mimo2_80MHz;
1511 break;
1512 default:
1513 WARN_ON_ONCE(1);
1514 }
1515 } else {
1516 WARN_ON_ONCE(1);
Eyal Shapirad972ab32013-07-28 23:02:45 +00001517 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01001518
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001519 if (!column->sgi && !lq_sta->is_agg) /* Normal */
1520 return ht_tbl_pointer[0];
1521 else if (column->sgi && !lq_sta->is_agg) /* SGI */
1522 return ht_tbl_pointer[1];
1523 else if (!column->sgi && lq_sta->is_agg) /* AGG */
1524 return ht_tbl_pointer[2];
Johannes Berg8ca151b2013-01-24 14:25:36 +01001525 else /* AGG+SGI */
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001526 return ht_tbl_pointer[3];
1527}
1528
1529static void rs_set_expected_tpt_table(struct iwl_lq_sta *lq_sta,
1530 struct iwl_scale_tbl_info *tbl)
1531{
1532 struct rs_rate *rate = &tbl->rate;
1533 const struct rs_tx_column *column = &rs_tx_columns[tbl->column];
1534
1535 tbl->expected_tpt = rs_get_expected_tpt_table(lq_sta, column, rate->bw);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001536}
1537
Johannes Berg8ca151b2013-01-24 14:25:36 +01001538static s32 rs_get_best_rate(struct iwl_mvm *mvm,
1539 struct iwl_lq_sta *lq_sta,
1540 struct iwl_scale_tbl_info *tbl, /* "search" */
Eyal Shapira80763512014-04-09 04:31:44 +03001541 unsigned long rate_mask, s8 index)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001542{
Johannes Berg8ca151b2013-01-24 14:25:36 +01001543 struct iwl_scale_tbl_info *active_tbl =
1544 &(lq_sta->lq_info[lq_sta->active_tbl]);
Eyal Shapira80763512014-04-09 04:31:44 +03001545 s32 success_ratio = active_tbl->win[index].success_ratio;
1546 u16 expected_current_tpt = active_tbl->expected_tpt[index];
Johannes Berga34529e2014-01-20 22:57:21 +01001547 const u16 *tpt_tbl = tbl->expected_tpt;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001548 u16 high_low;
Eyal Shapira80763512014-04-09 04:31:44 +03001549 u32 target_tpt;
1550 int rate_idx;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001551
Eyal Shapiraed21a382015-09-21 20:03:22 +03001552 if (success_ratio >= RS_PERCENT(IWL_MVM_RS_SR_NO_DECREASE)) {
Eyal Shapira80763512014-04-09 04:31:44 +03001553 target_tpt = 100 * expected_current_tpt;
1554 IWL_DEBUG_RATE(mvm,
1555 "SR %d high. Find rate exceeding EXPECTED_CURRENT %d\n",
1556 success_ratio, target_tpt);
1557 } else {
1558 target_tpt = lq_sta->last_tpt;
1559 IWL_DEBUG_RATE(mvm,
Eyal Shapiraed21a382015-09-21 20:03:22 +03001560 "SR %d not that good. Find rate exceeding ACTUAL_TPT %d\n",
Eyal Shapira80763512014-04-09 04:31:44 +03001561 success_ratio, target_tpt);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001562 }
1563
Eyal Shapira80763512014-04-09 04:31:44 +03001564 rate_idx = find_first_bit(&rate_mask, BITS_PER_LONG);
1565
1566 while (rate_idx != IWL_RATE_INVALID) {
1567 if (target_tpt < (100 * tpt_tbl[rate_idx]))
1568 break;
1569
1570 high_low = rs_get_adjacent_rate(mvm, rate_idx, rate_mask,
1571 tbl->rate.type);
1572
1573 rate_idx = (high_low >> 8) & 0xff;
1574 }
1575
1576 IWL_DEBUG_RATE(mvm, "Best rate found %d target_tp %d expected_new %d\n",
1577 rate_idx, target_tpt,
1578 rate_idx != IWL_RATE_INVALID ?
1579 100 * tpt_tbl[rate_idx] : IWL_INVALID_VALUE);
1580
1581 return rate_idx;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001582}
1583
Eyal Shapira5aa33552013-11-23 01:06:36 +02001584static u32 rs_bw_from_sta_bw(struct ieee80211_sta *sta)
Eyal Shapirad310e402013-08-11 18:43:47 +03001585{
1586 if (sta->bandwidth >= IEEE80211_STA_RX_BW_80)
Eyal Shapira5aa33552013-11-23 01:06:36 +02001587 return RATE_MCS_CHAN_WIDTH_80;
Eyal Shapirad310e402013-08-11 18:43:47 +03001588 else if (sta->bandwidth >= IEEE80211_STA_RX_BW_40)
Eyal Shapira5aa33552013-11-23 01:06:36 +02001589 return RATE_MCS_CHAN_WIDTH_40;
1590
1591 return RATE_MCS_CHAN_WIDTH_20;
Eyal Shapirad310e402013-08-11 18:43:47 +03001592}
1593
Johannes Berg8ca151b2013-01-24 14:25:36 +01001594/*
Johannes Berg8ca151b2013-01-24 14:25:36 +01001595 * Check whether we should continue using same modulation mode, or
1596 * begin search for a new mode, based on:
1597 * 1) # tx successes or failures while using this mode
1598 * 2) # times calling this function
1599 * 3) elapsed time in this mode (not used, for now)
1600 */
1601static void rs_stay_in_table(struct iwl_lq_sta *lq_sta, bool force_search)
1602{
1603 struct iwl_scale_tbl_info *tbl;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001604 int active_tbl;
1605 int flush_interval_passed = 0;
1606 struct iwl_mvm *mvm;
1607
Eliad Pellerae969af2014-06-11 15:51:33 +03001608 mvm = lq_sta->pers.drv;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001609 active_tbl = lq_sta->active_tbl;
1610
1611 tbl = &(lq_sta->lq_info[active_tbl]);
1612
1613 /* If we've been disallowing search, see if we should now allow it */
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001614 if (lq_sta->rs_state == RS_STATE_STAY_IN_COLUMN) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001615 /* Elapsed time using current modulation mode */
1616 if (lq_sta->flush_timer)
1617 flush_interval_passed =
1618 time_after(jiffies,
1619 (unsigned long)(lq_sta->flush_timer +
Eyal Shapira834437d2014-12-17 15:38:58 +02001620 (IWL_MVM_RS_STAY_IN_COLUMN_TIMEOUT * HZ)));
Johannes Berg8ca151b2013-01-24 14:25:36 +01001621
1622 /*
1623 * Check if we should allow search for new modulation mode.
1624 * If many frames have failed or succeeded, or we've used
1625 * this same modulation for a long time, allow search, and
1626 * reset history stats that keep track of whether we should
1627 * allow a new search. Also (below) reset all bitmaps and
1628 * stats in active history.
1629 */
1630 if (force_search ||
1631 (lq_sta->total_failed > lq_sta->max_failure_limit) ||
1632 (lq_sta->total_success > lq_sta->max_success_limit) ||
1633 ((!lq_sta->search_better_tbl) &&
1634 (lq_sta->flush_timer) && (flush_interval_passed))) {
1635 IWL_DEBUG_RATE(mvm,
1636 "LQ: stay is expired %d %d %d\n",
1637 lq_sta->total_failed,
1638 lq_sta->total_success,
1639 flush_interval_passed);
1640
1641 /* Allow search for new mode */
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001642 lq_sta->rs_state = RS_STATE_SEARCH_CYCLE_STARTED;
1643 IWL_DEBUG_RATE(mvm,
1644 "Moving to RS_STATE_SEARCH_CYCLE_STARTED\n");
Johannes Berg8ca151b2013-01-24 14:25:36 +01001645 lq_sta->total_failed = 0;
1646 lq_sta->total_success = 0;
1647 lq_sta->flush_timer = 0;
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001648 /* mark the current column as visited */
1649 lq_sta->visited_columns = BIT(tbl->column);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001650 /*
1651 * Else if we've used this modulation mode enough repetitions
1652 * (regardless of elapsed time or success/failure), reset
1653 * history bitmaps and rate-specific stats for all rates in
1654 * active table.
1655 */
1656 } else {
1657 lq_sta->table_count++;
1658 if (lq_sta->table_count >=
1659 lq_sta->table_count_limit) {
1660 lq_sta->table_count = 0;
1661
1662 IWL_DEBUG_RATE(mvm,
1663 "LQ: stay in table clear win\n");
Eyal Shapirab804eeb2014-04-06 04:27:06 +03001664 rs_rate_scale_clear_tbl_windows(mvm, tbl);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001665 }
1666 }
1667
1668 /* If transitioning to allow "search", reset all history
1669 * bitmaps and stats in active table (this will become the new
1670 * "search" table). */
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001671 if (lq_sta->rs_state == RS_STATE_SEARCH_CYCLE_STARTED) {
Eyal Shapirab804eeb2014-04-06 04:27:06 +03001672 rs_rate_scale_clear_tbl_windows(mvm, tbl);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001673 }
1674 }
1675}
1676
Emmanuel Grumbach04e3a5d2015-10-28 09:47:41 +02001677static void rs_set_amsdu_len(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
1678 struct iwl_scale_tbl_info *tbl,
1679 enum rs_action scale_action)
1680{
1681 struct iwl_mvm_sta *sta_priv = iwl_mvm_sta_from_mac80211(sta);
1682
1683 if ((!is_vht(&tbl->rate) && !is_ht(&tbl->rate)) ||
1684 tbl->rate.index < IWL_RATE_MCS_5_INDEX ||
1685 scale_action == RS_ACTION_DOWNSCALE)
1686 sta_priv->tlc_amsdu = false;
1687 else
1688 sta_priv->tlc_amsdu = true;
1689}
1690
Johannes Berg8ca151b2013-01-24 14:25:36 +01001691/*
1692 * setup rate table in uCode
1693 */
1694static void rs_update_rate_tbl(struct iwl_mvm *mvm,
Emmanuel Grumbach9145d152013-07-18 08:45:41 +03001695 struct ieee80211_sta *sta,
Johannes Berg8ca151b2013-01-24 14:25:36 +01001696 struct iwl_lq_sta *lq_sta,
Eyal Shapira7b9d74e2015-03-29 18:38:07 +03001697 struct iwl_scale_tbl_info *tbl)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001698{
Eyal Shapira7b9d74e2015-03-29 18:38:07 +03001699 rs_fill_lq_cmd(mvm, sta, lq_sta, &tbl->rate);
Eyal Shapira9e680942013-11-09 00:16:16 +02001700 iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, false);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001701}
1702
Eyal Shapira1191f642015-09-19 23:48:45 +03001703static bool rs_tweak_rate_tbl(struct iwl_mvm *mvm,
1704 struct ieee80211_sta *sta,
1705 struct iwl_lq_sta *lq_sta,
1706 struct iwl_scale_tbl_info *tbl,
1707 enum rs_action scale_action)
1708{
1709 if (sta->bandwidth != IEEE80211_STA_RX_BW_80)
1710 return false;
1711
1712 if (!is_vht_siso(&tbl->rate))
1713 return false;
1714
1715 if ((tbl->rate.bw == RATE_MCS_CHAN_WIDTH_80) &&
1716 (tbl->rate.index == IWL_RATE_MCS_0_INDEX) &&
1717 (scale_action == RS_ACTION_DOWNSCALE)) {
1718 tbl->rate.bw = RATE_MCS_CHAN_WIDTH_20;
1719 tbl->rate.index = IWL_RATE_MCS_4_INDEX;
1720 IWL_DEBUG_RATE(mvm, "Switch 80Mhz SISO MCS0 -> 20Mhz MCS4\n");
1721 goto tweaked;
1722 }
1723
1724 /* Go back to 80Mhz MCS1 only if we've established that 20Mhz MCS5 is
1725 * sustainable, i.e. we're past the test window. We can't go back
1726 * if MCS5 is just tested as this will happen always after switching
1727 * to 20Mhz MCS4 because the rate stats are cleared.
1728 */
1729 if ((tbl->rate.bw == RATE_MCS_CHAN_WIDTH_20) &&
1730 (((tbl->rate.index == IWL_RATE_MCS_5_INDEX) &&
1731 (scale_action == RS_ACTION_STAY)) ||
1732 ((tbl->rate.index > IWL_RATE_MCS_5_INDEX) &&
1733 (scale_action == RS_ACTION_UPSCALE)))) {
1734 tbl->rate.bw = RATE_MCS_CHAN_WIDTH_80;
1735 tbl->rate.index = IWL_RATE_MCS_1_INDEX;
1736 IWL_DEBUG_RATE(mvm, "Switch 20Mhz SISO MCS5 -> 80Mhz MCS1\n");
1737 goto tweaked;
1738 }
1739
1740 return false;
1741
1742tweaked:
1743 rs_set_expected_tpt_table(lq_sta, tbl);
1744 rs_rate_scale_clear_tbl_windows(mvm, tbl);
1745 return true;
1746}
1747
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001748static enum rs_column rs_get_next_column(struct iwl_mvm *mvm,
1749 struct iwl_lq_sta *lq_sta,
1750 struct ieee80211_sta *sta,
1751 struct iwl_scale_tbl_info *tbl)
1752{
Eyal Shapira198266a2014-03-31 22:37:39 +03001753 int i, j, max_rate;
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001754 enum rs_column next_col_id;
1755 const struct rs_tx_column *curr_col = &rs_tx_columns[tbl->column];
1756 const struct rs_tx_column *next_col;
1757 allow_column_func_t allow_func;
Moshe Harela0544272014-12-08 21:13:14 +02001758 u8 valid_ants = iwl_mvm_get_valid_tx_ant(mvm);
Johannes Berga34529e2014-01-20 22:57:21 +01001759 const u16 *expected_tpt_tbl;
Eyal Shapira198266a2014-03-31 22:37:39 +03001760 u16 tpt, max_expected_tpt;
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001761
1762 for (i = 0; i < MAX_NEXT_COLUMNS; i++) {
1763 next_col_id = curr_col->next_columns[i];
1764
1765 if (next_col_id == RS_COLUMN_INVALID)
1766 continue;
1767
1768 if (lq_sta->visited_columns & BIT(next_col_id)) {
1769 IWL_DEBUG_RATE(mvm, "Skip already visited column %d\n",
1770 next_col_id);
1771 continue;
1772 }
1773
1774 next_col = &rs_tx_columns[next_col_id];
1775
1776 if (!rs_is_valid_ant(valid_ants, next_col->ant)) {
1777 IWL_DEBUG_RATE(mvm,
1778 "Skip column %d as ANT config isn't supported by chip. valid_ants 0x%x column ant 0x%x\n",
1779 next_col_id, valid_ants, next_col->ant);
1780 continue;
1781 }
1782
1783 for (j = 0; j < MAX_COLUMN_CHECKS; j++) {
1784 allow_func = next_col->checks[j];
Eyal Shapirabb8f44c2015-05-27 22:00:03 +03001785 if (allow_func && !allow_func(mvm, sta, &tbl->rate,
1786 next_col))
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001787 break;
1788 }
1789
1790 if (j != MAX_COLUMN_CHECKS) {
1791 IWL_DEBUG_RATE(mvm,
1792 "Skip column %d: not allowed (check %d failed)\n",
1793 next_col_id, j);
1794
1795 continue;
1796 }
1797
1798 tpt = lq_sta->last_tpt / 100;
1799 expected_tpt_tbl = rs_get_expected_tpt_table(lq_sta, next_col,
Eyal Shapira049e0c32014-05-01 01:28:48 +03001800 rs_bw_from_sta_bw(sta));
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001801 if (WARN_ON_ONCE(!expected_tpt_tbl))
1802 continue;
1803
Eyal Shapira198266a2014-03-31 22:37:39 +03001804 max_rate = rs_get_max_allowed_rate(lq_sta, next_col);
Eyal Shapira6a028d92014-12-07 18:18:27 +02001805 if (max_rate == IWL_RATE_INVALID) {
1806 IWL_DEBUG_RATE(mvm,
1807 "Skip column %d: no rate is allowed in this column\n",
1808 next_col_id);
Eyal Shapira198266a2014-03-31 22:37:39 +03001809 continue;
Eyal Shapira6a028d92014-12-07 18:18:27 +02001810 }
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001811
Eyal Shapira198266a2014-03-31 22:37:39 +03001812 max_expected_tpt = expected_tpt_tbl[max_rate];
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001813 if (tpt >= max_expected_tpt) {
1814 IWL_DEBUG_RATE(mvm,
1815 "Skip column %d: can't beat current TPT. Max expected %d current %d\n",
1816 next_col_id, max_expected_tpt, tpt);
1817 continue;
1818 }
1819
Eyal Shapira198266a2014-03-31 22:37:39 +03001820 IWL_DEBUG_RATE(mvm,
1821 "Found potential column %d. Max expected %d current %d\n",
1822 next_col_id, max_expected_tpt, tpt);
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001823 break;
1824 }
1825
1826 if (i == MAX_NEXT_COLUMNS)
1827 return RS_COLUMN_INVALID;
1828
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001829 return next_col_id;
1830}
1831
1832static int rs_switch_to_column(struct iwl_mvm *mvm,
1833 struct iwl_lq_sta *lq_sta,
1834 struct ieee80211_sta *sta,
1835 enum rs_column col_id)
1836{
1837 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1838 struct iwl_scale_tbl_info *search_tbl =
1839 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1840 struct rs_rate *rate = &search_tbl->rate;
1841 const struct rs_tx_column *column = &rs_tx_columns[col_id];
1842 const struct rs_tx_column *curr_column = &rs_tx_columns[tbl->column];
1843 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1844 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Eyal Shapira80763512014-04-09 04:31:44 +03001845 unsigned long rate_mask = 0;
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001846 u32 rate_idx = 0;
1847
1848 memcpy(search_tbl, tbl, sz);
1849
1850 rate->sgi = column->sgi;
1851 rate->ant = column->ant;
1852
1853 if (column->mode == RS_LEGACY) {
Johannes Berg57fbcce2016-04-12 15:56:15 +02001854 if (lq_sta->band == NL80211_BAND_5GHZ)
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001855 rate->type = LQ_LEGACY_A;
1856 else
1857 rate->type = LQ_LEGACY_G;
1858
Eyal Shapirad8bafea2014-10-23 13:58:38 +03001859 rate->bw = RATE_MCS_CHAN_WIDTH_20;
1860 rate->ldpc = false;
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001861 rate_mask = lq_sta->active_legacy_rate;
1862 } else if (column->mode == RS_SISO) {
1863 rate->type = lq_sta->is_vht ? LQ_VHT_SISO : LQ_HT_SISO;
1864 rate_mask = lq_sta->active_siso_rate;
1865 } else if (column->mode == RS_MIMO2) {
1866 rate->type = lq_sta->is_vht ? LQ_VHT_MIMO2 : LQ_HT_MIMO2;
1867 rate_mask = lq_sta->active_mimo2_rate;
1868 } else {
Dan Carpenter7281b162015-11-21 13:33:38 +03001869 WARN_ONCE(1, "Bad column mode");
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001870 }
1871
Eyal Shapirad8bafea2014-10-23 13:58:38 +03001872 if (column->mode != RS_LEGACY) {
1873 rate->bw = rs_bw_from_sta_bw(sta);
1874 rate->ldpc = lq_sta->ldpc;
1875 }
1876
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001877 search_tbl->column = col_id;
1878 rs_set_expected_tpt_table(lq_sta, search_tbl);
1879
Eyal Shapira75d3e2d2014-01-07 18:19:35 +02001880 lq_sta->visited_columns |= BIT(col_id);
1881
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001882 /* Get the best matching rate if we're changing modes. e.g.
1883 * SISO->MIMO, LEGACY->SISO, MIMO->SISO
1884 */
1885 if (curr_column->mode != column->mode) {
1886 rate_idx = rs_get_best_rate(mvm, lq_sta, search_tbl,
1887 rate_mask, rate->index);
1888
1889 if ((rate_idx == IWL_RATE_INVALID) ||
1890 !(BIT(rate_idx) & rate_mask)) {
1891 IWL_DEBUG_RATE(mvm,
1892 "can not switch with index %d"
Eyal Shapira80763512014-04-09 04:31:44 +03001893 " rate mask %lx\n",
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001894 rate_idx, rate_mask);
1895
1896 goto err;
1897 }
1898
1899 rate->index = rate_idx;
1900 }
1901
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001902 IWL_DEBUG_RATE(mvm, "Switched to column %d: Index %d\n",
1903 col_id, rate->index);
1904
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001905 return 0;
1906
1907err:
1908 rate->type = LQ_NONE;
1909 return -1;
1910}
1911
Eyal Shapira4e4b8152013-12-11 00:55:36 +02001912static enum rs_action rs_get_rate_action(struct iwl_mvm *mvm,
1913 struct iwl_scale_tbl_info *tbl,
1914 s32 sr, int low, int high,
1915 int current_tpt,
1916 int low_tpt, int high_tpt)
1917{
1918 enum rs_action action = RS_ACTION_STAY;
1919
Eyal Shapira834437d2014-12-17 15:38:58 +02001920 if ((sr <= RS_PERCENT(IWL_MVM_RS_SR_FORCE_DECREASE)) ||
1921 (current_tpt == 0)) {
Eyal Shapira4e4b8152013-12-11 00:55:36 +02001922 IWL_DEBUG_RATE(mvm,
Eyal Shapirae53839eb2014-04-06 02:42:18 +03001923 "Decrease rate because of low SR\n");
1924 return RS_ACTION_DOWNSCALE;
1925 }
1926
1927 if ((low_tpt == IWL_INVALID_VALUE) &&
1928 (high_tpt == IWL_INVALID_VALUE) &&
1929 (high != IWL_RATE_INVALID)) {
1930 IWL_DEBUG_RATE(mvm,
1931 "No data about high/low rates. Increase rate\n");
1932 return RS_ACTION_UPSCALE;
1933 }
1934
1935 if ((high_tpt == IWL_INVALID_VALUE) &&
1936 (high != IWL_RATE_INVALID) &&
1937 (low_tpt != IWL_INVALID_VALUE) &&
1938 (low_tpt < current_tpt)) {
1939 IWL_DEBUG_RATE(mvm,
1940 "No data about high rate and low rate is worse. Increase rate\n");
1941 return RS_ACTION_UPSCALE;
1942 }
1943
1944 if ((high_tpt != IWL_INVALID_VALUE) &&
1945 (high_tpt > current_tpt)) {
1946 IWL_DEBUG_RATE(mvm,
1947 "Higher rate is better. Increate rate\n");
1948 return RS_ACTION_UPSCALE;
1949 }
1950
1951 if ((low_tpt != IWL_INVALID_VALUE) &&
1952 (high_tpt != IWL_INVALID_VALUE) &&
1953 (low_tpt < current_tpt) &&
1954 (high_tpt < current_tpt)) {
1955 IWL_DEBUG_RATE(mvm,
1956 "Both high and low are worse. Maintain rate\n");
1957 return RS_ACTION_STAY;
1958 }
1959
1960 if ((low_tpt != IWL_INVALID_VALUE) &&
1961 (low_tpt > current_tpt)) {
1962 IWL_DEBUG_RATE(mvm,
1963 "Lower rate is better\n");
Eyal Shapira4e4b8152013-12-11 00:55:36 +02001964 action = RS_ACTION_DOWNSCALE;
Eyal Shapirae53839eb2014-04-06 02:42:18 +03001965 goto out;
1966 }
1967
1968 if ((low_tpt == IWL_INVALID_VALUE) &&
1969 (low != IWL_RATE_INVALID)) {
1970 IWL_DEBUG_RATE(mvm,
1971 "No data about lower rate\n");
1972 action = RS_ACTION_DOWNSCALE;
1973 goto out;
1974 }
1975
1976 IWL_DEBUG_RATE(mvm, "Maintain rate\n");
1977
1978out:
1979 if ((action == RS_ACTION_DOWNSCALE) && (low != IWL_RATE_INVALID)) {
Eyal Shapira834437d2014-12-17 15:38:58 +02001980 if (sr >= RS_PERCENT(IWL_MVM_RS_SR_NO_DECREASE)) {
Eyal Shapira4e4b8152013-12-11 00:55:36 +02001981 IWL_DEBUG_RATE(mvm,
Eyal Shapirae53839eb2014-04-06 02:42:18 +03001982 "SR is above NO DECREASE. Avoid downscale\n");
Eyal Shapira4e4b8152013-12-11 00:55:36 +02001983 action = RS_ACTION_STAY;
Eyal Shapirae53839eb2014-04-06 02:42:18 +03001984 } else if (current_tpt > (100 * tbl->expected_tpt[low])) {
1985 IWL_DEBUG_RATE(mvm,
1986 "Current TPT is higher than max expected in low rate. Avoid downscale\n");
1987 action = RS_ACTION_STAY;
1988 } else {
1989 IWL_DEBUG_RATE(mvm, "Decrease rate\n");
Eyal Shapira4e4b8152013-12-11 00:55:36 +02001990 }
1991 }
1992
Eyal Shapira4e4b8152013-12-11 00:55:36 +02001993 return action;
1994}
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001995
Eyal Shapirad8bafea2014-10-23 13:58:38 +03001996static bool rs_stbc_allow(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
1997 struct iwl_lq_sta *lq_sta)
1998{
Eyal Shapirad8bafea2014-10-23 13:58:38 +03001999 /* Our chip supports Tx STBC and the peer is an HT/VHT STA which
2000 * supports STBC of at least 1*SS
2001 */
Eyal Shapira3d44eeb2015-01-16 22:37:04 +02002002 if (!lq_sta->stbc_capable)
Eyal Shapirad8bafea2014-10-23 13:58:38 +03002003 return false;
2004
Eyal Shapirad8bafea2014-10-23 13:58:38 +03002005 if (!iwl_mvm_bt_coex_is_mimo_allowed(mvm, sta))
2006 return false;
2007
2008 return true;
2009}
2010
Eliad Peller2fd647f2014-03-13 17:21:36 +02002011static void rs_get_adjacent_txp(struct iwl_mvm *mvm, int index,
2012 int *weaker, int *stronger)
2013{
Eyal Shapira834437d2014-12-17 15:38:58 +02002014 *weaker = index + IWL_MVM_RS_TPC_TX_POWER_STEP;
Eliad Peller2fd647f2014-03-13 17:21:36 +02002015 if (*weaker > TPC_MAX_REDUCTION)
2016 *weaker = TPC_INVALID;
2017
Eyal Shapira834437d2014-12-17 15:38:58 +02002018 *stronger = index - IWL_MVM_RS_TPC_TX_POWER_STEP;
Eliad Peller2fd647f2014-03-13 17:21:36 +02002019 if (*stronger < 0)
2020 *stronger = TPC_INVALID;
2021}
2022
Eyal Shapira1e9551d2014-04-21 21:33:17 +03002023static bool rs_tpc_allowed(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
Johannes Berg57fbcce2016-04-12 15:56:15 +02002024 struct rs_rate *rate, enum nl80211_band band)
Eliad Peller2fd647f2014-03-13 17:21:36 +02002025{
2026 int index = rate->index;
Eyal Shapira1e9551d2014-04-21 21:33:17 +03002027 bool cam = (iwlmvm_mod_params.power_scheme == IWL_POWER_SCHEME_CAM);
2028 bool sta_ps_disabled = (vif->type == NL80211_IFTYPE_STATION &&
2029 !vif->bss_conf.ps);
Eliad Peller2fd647f2014-03-13 17:21:36 +02002030
Eyal Shapira1e9551d2014-04-21 21:33:17 +03002031 IWL_DEBUG_RATE(mvm, "cam: %d sta_ps_disabled %d\n",
2032 cam, sta_ps_disabled);
Eliad Peller2fd647f2014-03-13 17:21:36 +02002033 /*
2034 * allow tpc only if power management is enabled, or bt coex
2035 * activity grade allows it and we are on 2.4Ghz.
2036 */
Eyal Shapira1e9551d2014-04-21 21:33:17 +03002037 if ((cam || sta_ps_disabled) &&
Eliad Peller2fd647f2014-03-13 17:21:36 +02002038 !iwl_mvm_bt_coex_is_tpc_allowed(mvm, band))
2039 return false;
2040
2041 IWL_DEBUG_RATE(mvm, "check rate, table type: %d\n", rate->type);
2042 if (is_legacy(rate))
2043 return index == IWL_RATE_54M_INDEX;
2044 if (is_ht(rate))
2045 return index == IWL_RATE_MCS_7_INDEX;
2046 if (is_vht(rate))
2047 return index == IWL_RATE_MCS_7_INDEX ||
2048 index == IWL_RATE_MCS_8_INDEX ||
2049 index == IWL_RATE_MCS_9_INDEX;
2050
2051 WARN_ON_ONCE(1);
2052 return false;
2053}
2054
2055enum tpc_action {
2056 TPC_ACTION_STAY,
2057 TPC_ACTION_DECREASE,
2058 TPC_ACTION_INCREASE,
2059 TPC_ACTION_NO_RESTIRCTION,
2060};
2061
2062static enum tpc_action rs_get_tpc_action(struct iwl_mvm *mvm,
2063 s32 sr, int weak, int strong,
2064 int current_tpt,
2065 int weak_tpt, int strong_tpt)
2066{
2067 /* stay until we have valid tpt */
2068 if (current_tpt == IWL_INVALID_VALUE) {
2069 IWL_DEBUG_RATE(mvm, "no current tpt. stay.\n");
2070 return TPC_ACTION_STAY;
2071 }
2072
2073 /* Too many failures, increase txp */
Eyal Shapira834437d2014-12-17 15:38:58 +02002074 if (sr <= RS_PERCENT(IWL_MVM_RS_TPC_SR_FORCE_INCREASE) ||
2075 current_tpt == 0) {
Eliad Peller2fd647f2014-03-13 17:21:36 +02002076 IWL_DEBUG_RATE(mvm, "increase txp because of weak SR\n");
2077 return TPC_ACTION_NO_RESTIRCTION;
2078 }
2079
2080 /* try decreasing first if applicable */
Gregory Greenman6ad6c012016-01-27 00:10:15 +02002081 if (sr >= RS_PERCENT(IWL_MVM_RS_TPC_SR_NO_INCREASE) &&
2082 weak != TPC_INVALID) {
Eliad Peller2fd647f2014-03-13 17:21:36 +02002083 if (weak_tpt == IWL_INVALID_VALUE &&
2084 (strong_tpt == IWL_INVALID_VALUE ||
2085 current_tpt >= strong_tpt)) {
2086 IWL_DEBUG_RATE(mvm,
2087 "no weak txp measurement. decrease txp\n");
2088 return TPC_ACTION_DECREASE;
2089 }
2090
2091 if (weak_tpt > current_tpt) {
2092 IWL_DEBUG_RATE(mvm,
2093 "lower txp has better tpt. decrease txp\n");
2094 return TPC_ACTION_DECREASE;
2095 }
2096 }
2097
2098 /* next, increase if needed */
Eyal Shapira834437d2014-12-17 15:38:58 +02002099 if (sr < RS_PERCENT(IWL_MVM_RS_TPC_SR_NO_INCREASE) &&
2100 strong != TPC_INVALID) {
Eliad Peller2fd647f2014-03-13 17:21:36 +02002101 if (weak_tpt == IWL_INVALID_VALUE &&
2102 strong_tpt != IWL_INVALID_VALUE &&
2103 current_tpt < strong_tpt) {
2104 IWL_DEBUG_RATE(mvm,
2105 "higher txp has better tpt. increase txp\n");
2106 return TPC_ACTION_INCREASE;
2107 }
2108
2109 if (weak_tpt < current_tpt &&
2110 (strong_tpt == IWL_INVALID_VALUE ||
2111 strong_tpt > current_tpt)) {
2112 IWL_DEBUG_RATE(mvm,
2113 "lower txp has worse tpt. increase txp\n");
2114 return TPC_ACTION_INCREASE;
2115 }
2116 }
2117
2118 IWL_DEBUG_RATE(mvm, "no need to increase or decrease txp - stay\n");
2119 return TPC_ACTION_STAY;
2120}
2121
2122static bool rs_tpc_perform(struct iwl_mvm *mvm,
2123 struct ieee80211_sta *sta,
2124 struct iwl_lq_sta *lq_sta,
2125 struct iwl_scale_tbl_info *tbl)
2126{
Johannes Berg9d8ce6a2014-12-23 16:02:40 +01002127 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Eliad Peller2fd647f2014-03-13 17:21:36 +02002128 struct ieee80211_vif *vif = mvm_sta->vif;
2129 struct ieee80211_chanctx_conf *chanctx_conf;
Johannes Berg57fbcce2016-04-12 15:56:15 +02002130 enum nl80211_band band;
Eliad Peller2fd647f2014-03-13 17:21:36 +02002131 struct iwl_rate_scale_data *window;
2132 struct rs_rate *rate = &tbl->rate;
2133 enum tpc_action action;
2134 s32 sr;
2135 u8 cur = lq_sta->lq.reduced_tpc;
2136 int current_tpt;
2137 int weak, strong;
2138 int weak_tpt = IWL_INVALID_VALUE, strong_tpt = IWL_INVALID_VALUE;
2139
Eliad Peller9a75b3d2014-03-13 17:15:04 +02002140#ifdef CONFIG_MAC80211_DEBUGFS
Eliad Pellerae969af2014-06-11 15:51:33 +03002141 if (lq_sta->pers.dbg_fixed_txp_reduction <= TPC_MAX_REDUCTION) {
Johannes Berg3c6acb62014-05-07 11:47:53 +02002142 IWL_DEBUG_RATE(mvm, "fixed tpc: %d\n",
Eliad Pellerae969af2014-06-11 15:51:33 +03002143 lq_sta->pers.dbg_fixed_txp_reduction);
2144 lq_sta->lq.reduced_tpc = lq_sta->pers.dbg_fixed_txp_reduction;
2145 return cur != lq_sta->pers.dbg_fixed_txp_reduction;
Eliad Peller9a75b3d2014-03-13 17:15:04 +02002146 }
2147#endif
2148
Eliad Peller2fd647f2014-03-13 17:21:36 +02002149 rcu_read_lock();
2150 chanctx_conf = rcu_dereference(vif->chanctx_conf);
2151 if (WARN_ON(!chanctx_conf))
Johannes Berg57fbcce2016-04-12 15:56:15 +02002152 band = NUM_NL80211_BANDS;
Eliad Peller2fd647f2014-03-13 17:21:36 +02002153 else
2154 band = chanctx_conf->def.chan->band;
2155 rcu_read_unlock();
2156
Eyal Shapira1e9551d2014-04-21 21:33:17 +03002157 if (!rs_tpc_allowed(mvm, vif, rate, band)) {
Eliad Peller2fd647f2014-03-13 17:21:36 +02002158 IWL_DEBUG_RATE(mvm,
Johannes Berg3c6acb62014-05-07 11:47:53 +02002159 "tpc is not allowed. remove txp restrictions\n");
Eliad Peller2fd647f2014-03-13 17:21:36 +02002160 lq_sta->lq.reduced_tpc = TPC_NO_REDUCTION;
2161 return cur != TPC_NO_REDUCTION;
2162 }
2163
2164 rs_get_adjacent_txp(mvm, cur, &weak, &strong);
2165
2166 /* Collect measured throughputs for current and adjacent rates */
2167 window = tbl->tpc_win;
2168 sr = window[cur].success_ratio;
2169 current_tpt = window[cur].average_tpt;
2170 if (weak != TPC_INVALID)
2171 weak_tpt = window[weak].average_tpt;
2172 if (strong != TPC_INVALID)
2173 strong_tpt = window[strong].average_tpt;
2174
2175 IWL_DEBUG_RATE(mvm,
2176 "(TPC: %d): cur_tpt %d SR %d weak %d strong %d weak_tpt %d strong_tpt %d\n",
2177 cur, current_tpt, sr, weak, strong,
2178 weak_tpt, strong_tpt);
2179
2180 action = rs_get_tpc_action(mvm, sr, weak, strong,
2181 current_tpt, weak_tpt, strong_tpt);
2182
2183 /* override actions if we are on the edge */
2184 if (weak == TPC_INVALID && action == TPC_ACTION_DECREASE) {
Johannes Berg3c6acb62014-05-07 11:47:53 +02002185 IWL_DEBUG_RATE(mvm, "already in lowest txp, stay\n");
Eliad Peller2fd647f2014-03-13 17:21:36 +02002186 action = TPC_ACTION_STAY;
2187 } else if (strong == TPC_INVALID &&
2188 (action == TPC_ACTION_INCREASE ||
2189 action == TPC_ACTION_NO_RESTIRCTION)) {
Johannes Berg3c6acb62014-05-07 11:47:53 +02002190 IWL_DEBUG_RATE(mvm, "already in highest txp, stay\n");
Eliad Peller2fd647f2014-03-13 17:21:36 +02002191 action = TPC_ACTION_STAY;
2192 }
2193
2194 switch (action) {
2195 case TPC_ACTION_DECREASE:
2196 lq_sta->lq.reduced_tpc = weak;
2197 return true;
2198 case TPC_ACTION_INCREASE:
2199 lq_sta->lq.reduced_tpc = strong;
2200 return true;
2201 case TPC_ACTION_NO_RESTIRCTION:
2202 lq_sta->lq.reduced_tpc = TPC_NO_REDUCTION;
2203 return true;
2204 case TPC_ACTION_STAY:
2205 /* do nothing */
2206 break;
2207 }
2208 return false;
2209}
2210
Johannes Berg8ca151b2013-01-24 14:25:36 +01002211/*
2212 * Do rate scaling and search for new modulation mode.
2213 */
2214static void rs_rate_scale_perform(struct iwl_mvm *mvm,
Johannes Berg8ca151b2013-01-24 14:25:36 +01002215 struct ieee80211_sta *sta,
Eyal Shapira1ebc8f22014-09-14 15:23:23 +03002216 struct iwl_lq_sta *lq_sta,
Matti Gottliebff7a68d2016-05-17 10:50:08 +03002217 int tid, bool ndp)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002218{
Johannes Berg8ca151b2013-01-24 14:25:36 +01002219 int low = IWL_RATE_INVALID;
2220 int high = IWL_RATE_INVALID;
2221 int index;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002222 struct iwl_rate_scale_data *window = NULL;
2223 int current_tpt = IWL_INVALID_VALUE;
2224 int low_tpt = IWL_INVALID_VALUE;
2225 int high_tpt = IWL_INVALID_VALUE;
2226 u32 fail_count;
Eyal Shapira4e4b8152013-12-11 00:55:36 +02002227 enum rs_action scale_action = RS_ACTION_STAY;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002228 u16 rate_mask;
2229 u8 update_lq = 0;
2230 struct iwl_scale_tbl_info *tbl, *tbl1;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002231 u8 active_tbl = 0;
2232 u8 done_search = 0;
2233 u16 high_low;
2234 s32 sr;
Eyal Shapira9d015a02013-11-21 01:12:11 +02002235 u8 prev_agg = lq_sta->is_agg;
Johannes Berg9d8ce6a2014-12-23 16:02:40 +01002236 struct iwl_mvm_sta *sta_priv = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002237 struct iwl_mvm_tid_data *tid_data;
Eyal Shapira5aa33552013-11-23 01:06:36 +02002238 struct rs_rate *rate;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002239
Eyal Shapiraefed6642014-09-14 15:58:53 +03002240 lq_sta->is_agg = !!sta_priv->agg_tids;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002241
2242 /*
2243 * Select rate-scale / modulation-mode table to work with in
2244 * the rest of this function: "search" if searching for better
2245 * modulation mode, or "active" if doing rate scaling within a mode.
2246 */
2247 if (!lq_sta->search_better_tbl)
2248 active_tbl = lq_sta->active_tbl;
2249 else
2250 active_tbl = 1 - lq_sta->active_tbl;
2251
2252 tbl = &(lq_sta->lq_info[active_tbl]);
Eyal Shapira5aa33552013-11-23 01:06:36 +02002253 rate = &tbl->rate;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002254
Eyal Shapira9d015a02013-11-21 01:12:11 +02002255 if (prev_agg != lq_sta->is_agg) {
2256 IWL_DEBUG_RATE(mvm,
2257 "Aggregation changed: prev %d current %d. Update expected TPT table\n",
2258 prev_agg, lq_sta->is_agg);
2259 rs_set_expected_tpt_table(lq_sta, tbl);
Eyal Shapirab804eeb2014-04-06 04:27:06 +03002260 rs_rate_scale_clear_tbl_windows(mvm, tbl);
Eyal Shapira9d015a02013-11-21 01:12:11 +02002261 }
2262
Johannes Berg8ca151b2013-01-24 14:25:36 +01002263 /* current tx rate */
Eyal Shapirafcc5e852015-04-12 23:45:27 +03002264 index = rate->index;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002265
Johannes Berg8ca151b2013-01-24 14:25:36 +01002266 /* rates available for this association, and for modulation mode */
Eyal Shapiraa8ff14f2013-11-23 01:30:44 +02002267 rate_mask = rs_get_supported_rates(lq_sta, rate);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002268
Eyal Shapiraca6f38f2013-12-09 11:14:11 +02002269 if (!(BIT(index) & rate_mask)) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002270 IWL_ERR(mvm, "Current Rate is not valid\n");
2271 if (lq_sta->search_better_tbl) {
2272 /* revert to active table if search table is not valid*/
Eyal Shapira5aa33552013-11-23 01:06:36 +02002273 rate->type = LQ_NONE;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002274 lq_sta->search_better_tbl = 0;
2275 tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
Eyal Shapira7b9d74e2015-03-29 18:38:07 +03002276 rs_update_rate_tbl(mvm, sta, lq_sta, tbl);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002277 }
2278 return;
2279 }
2280
2281 /* Get expected throughput table and history window for current rate */
2282 if (!tbl->expected_tpt) {
2283 IWL_ERR(mvm, "tbl->expected_tpt is NULL\n");
2284 return;
2285 }
2286
Eyal Shapira2536e7c2014-08-31 02:33:33 +03002287 /* TODO: handle rate_idx_mask and rate_idx_mcs_mask */
Johannes Berg8ca151b2013-01-24 14:25:36 +01002288 window = &(tbl->win[index]);
2289
2290 /*
2291 * If there is not enough history to calculate actual average
2292 * throughput, keep analyzing results of more tx frames, without
2293 * changing rate or mode (bypass most of the rest of this function).
2294 * Set up new rate table in uCode only if old rate is not supported
2295 * in current association (use new rate found above).
2296 */
2297 fail_count = window->counter - window->success_counter;
Eyal Shapira834437d2014-12-17 15:38:58 +02002298 if ((fail_count < IWL_MVM_RS_RATE_MIN_FAILURE_TH) &&
2299 (window->success_counter < IWL_MVM_RS_RATE_MIN_SUCCESS_TH)) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002300 IWL_DEBUG_RATE(mvm,
Eyal Shapiracc60c6e2015-09-19 23:37:45 +03002301 "%s: Test Window: succ %d total %d\n",
2302 rs_pretty_rate(rate),
2303 window->success_counter, window->counter);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002304
2305 /* Can't calculate this yet; not enough history */
2306 window->average_tpt = IWL_INVALID_VALUE;
2307
2308 /* Should we stay with this modulation mode,
2309 * or search for a new one? */
2310 rs_stay_in_table(lq_sta, false);
2311
Eyal Shapirafcc5e852015-04-12 23:45:27 +03002312 return;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002313 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01002314
2315 /* If we are searching for better modulation mode, check success. */
2316 if (lq_sta->search_better_tbl) {
2317 /* If good success, continue using the "search" mode;
2318 * no need to send new link quality command, since we're
2319 * continuing to use the setup that we've been trying. */
2320 if (window->average_tpt > lq_sta->last_tpt) {
2321 IWL_DEBUG_RATE(mvm,
Eyal Shapira4d30ee82013-11-20 01:45:38 +02002322 "SWITCHING TO NEW TABLE SR: %d "
2323 "cur-tpt %d old-tpt %d\n",
Johannes Berg8ca151b2013-01-24 14:25:36 +01002324 window->success_ratio,
2325 window->average_tpt,
2326 lq_sta->last_tpt);
2327
Johannes Berg8ca151b2013-01-24 14:25:36 +01002328 /* Swap tables; "search" becomes "active" */
2329 lq_sta->active_tbl = active_tbl;
2330 current_tpt = window->average_tpt;
2331 /* Else poor success; go back to mode in "active" table */
2332 } else {
2333 IWL_DEBUG_RATE(mvm,
Eyal Shapira4d30ee82013-11-20 01:45:38 +02002334 "GOING BACK TO THE OLD TABLE: SR %d "
2335 "cur-tpt %d old-tpt %d\n",
Johannes Berg8ca151b2013-01-24 14:25:36 +01002336 window->success_ratio,
2337 window->average_tpt,
2338 lq_sta->last_tpt);
2339
2340 /* Nullify "search" table */
Eyal Shapira5aa33552013-11-23 01:06:36 +02002341 rate->type = LQ_NONE;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002342
2343 /* Revert to "active" table */
2344 active_tbl = lq_sta->active_tbl;
2345 tbl = &(lq_sta->lq_info[active_tbl]);
2346
2347 /* Revert to "active" rate and throughput info */
Eyal Shapira8fc7c582013-12-04 02:15:46 +02002348 index = tbl->rate.index;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002349 current_tpt = lq_sta->last_tpt;
2350
2351 /* Need to set up a new rate table in uCode */
2352 update_lq = 1;
2353 }
2354
2355 /* Either way, we've made a decision; modulation mode
2356 * search is done, allow rate adjustment next time. */
2357 lq_sta->search_better_tbl = 0;
2358 done_search = 1; /* Don't switch modes below! */
2359 goto lq_update;
2360 }
2361
2362 /* (Else) not in search of better modulation mode, try for better
2363 * starting rate, while staying in this mode. */
Eyal Shapiraca6f38f2013-12-09 11:14:11 +02002364 high_low = rs_get_adjacent_rate(mvm, index, rate_mask, rate->type);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002365 low = high_low & 0xff;
2366 high = (high_low >> 8) & 0xff;
2367
Eyal Shapira2536e7c2014-08-31 02:33:33 +03002368 /* TODO: handle rate_idx_mask and rate_idx_mcs_mask */
Johannes Berg8ca151b2013-01-24 14:25:36 +01002369
2370 sr = window->success_ratio;
2371
2372 /* Collect measured throughputs for current and adjacent rates */
2373 current_tpt = window->average_tpt;
2374 if (low != IWL_RATE_INVALID)
2375 low_tpt = tbl->win[low].average_tpt;
2376 if (high != IWL_RATE_INVALID)
2377 high_tpt = tbl->win[high].average_tpt;
2378
Eyal Shapira4d30ee82013-11-20 01:45:38 +02002379 IWL_DEBUG_RATE(mvm,
Eyal Shapiracc60c6e2015-09-19 23:37:45 +03002380 "%s: cur_tpt %d SR %d low %d high %d low_tpt %d high_tpt %d\n",
2381 rs_pretty_rate(rate), current_tpt, sr,
Eyal Shapira5aa33552013-11-23 01:06:36 +02002382 low, high, low_tpt, high_tpt);
Eyal Shapira4d30ee82013-11-20 01:45:38 +02002383
Eyal Shapira4e4b8152013-12-11 00:55:36 +02002384 scale_action = rs_get_rate_action(mvm, tbl, sr, low, high,
2385 current_tpt, low_tpt, high_tpt);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002386
Eyal Shapira393b9e52013-11-13 16:46:19 +02002387 /* Force a search in case BT doesn't like us being in MIMO */
Eyal Shapira5aa33552013-11-23 01:06:36 +02002388 if (is_mimo(rate) &&
Eyal Shapira393b9e52013-11-13 16:46:19 +02002389 !iwl_mvm_bt_coex_is_mimo_allowed(mvm, sta)) {
Eyal Shapira4d30ee82013-11-20 01:45:38 +02002390 IWL_DEBUG_RATE(mvm,
2391 "BT Coex forbids MIMO. Search for new config\n");
Emmanuel Grumbach36946ce2013-05-28 23:12:47 +03002392 rs_stay_in_table(lq_sta, true);
2393 goto lq_update;
2394 }
2395
Johannes Berg8ca151b2013-01-24 14:25:36 +01002396 switch (scale_action) {
Eyal Shapira4e4b8152013-12-11 00:55:36 +02002397 case RS_ACTION_DOWNSCALE:
Johannes Berg8ca151b2013-01-24 14:25:36 +01002398 /* Decrease starting rate, update uCode's rate table */
2399 if (low != IWL_RATE_INVALID) {
2400 update_lq = 1;
2401 index = low;
Eyal Shapira4d30ee82013-11-20 01:45:38 +02002402 } else {
2403 IWL_DEBUG_RATE(mvm,
2404 "At the bottom rate. Can't decrease\n");
Johannes Berg8ca151b2013-01-24 14:25:36 +01002405 }
2406
2407 break;
Eyal Shapira4e4b8152013-12-11 00:55:36 +02002408 case RS_ACTION_UPSCALE:
Johannes Berg8ca151b2013-01-24 14:25:36 +01002409 /* Increase starting rate, update uCode's rate table */
2410 if (high != IWL_RATE_INVALID) {
2411 update_lq = 1;
2412 index = high;
Eyal Shapira4d30ee82013-11-20 01:45:38 +02002413 } else {
2414 IWL_DEBUG_RATE(mvm,
2415 "At the top rate. Can't increase\n");
Johannes Berg8ca151b2013-01-24 14:25:36 +01002416 }
2417
2418 break;
Eyal Shapira4e4b8152013-12-11 00:55:36 +02002419 case RS_ACTION_STAY:
Johannes Berg8ca151b2013-01-24 14:25:36 +01002420 /* No change */
Eyal Shapirae6c8d602014-04-21 21:49:08 +03002421 if (lq_sta->rs_state == RS_STATE_STAY_IN_COLUMN)
2422 update_lq = rs_tpc_perform(mvm, sta, lq_sta, tbl);
Eliad Peller2fd647f2014-03-13 17:21:36 +02002423 break;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002424 default:
2425 break;
2426 }
2427
Johannes Berg8ca151b2013-01-24 14:25:36 +01002428lq_update:
2429 /* Replace uCode's rate table for the destination station. */
Eyal Shapira5aa33552013-11-23 01:06:36 +02002430 if (update_lq) {
2431 tbl->rate.index = index;
Eyal Shapira1191f642015-09-19 23:48:45 +03002432 if (IWL_MVM_RS_80_20_FAR_RANGE_TWEAK)
2433 rs_tweak_rate_tbl(mvm, sta, lq_sta, tbl, scale_action);
Emmanuel Grumbach04e3a5d2015-10-28 09:47:41 +02002434 rs_set_amsdu_len(mvm, sta, tbl, scale_action);
Eyal Shapira7b9d74e2015-03-29 18:38:07 +03002435 rs_update_rate_tbl(mvm, sta, lq_sta, tbl);
Eyal Shapira5aa33552013-11-23 01:06:36 +02002436 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01002437
2438 rs_stay_in_table(lq_sta, false);
2439
2440 /*
2441 * Search for new modulation mode if we're:
2442 * 1) Not changing rates right now
2443 * 2) Not just finishing up a search
2444 * 3) Allowing a new search
2445 */
2446 if (!update_lq && !done_search &&
Eyal Shapirab3b06a32013-11-24 21:30:13 +02002447 lq_sta->rs_state == RS_STATE_SEARCH_CYCLE_STARTED
2448 && window->counter) {
2449 enum rs_column next_column;
2450
Johannes Berg8ca151b2013-01-24 14:25:36 +01002451 /* Save current throughput to compare with "search" throughput*/
2452 lq_sta->last_tpt = current_tpt;
2453
Eyal Shapira4d30ee82013-11-20 01:45:38 +02002454 IWL_DEBUG_RATE(mvm,
Eyal Shapirab3b06a32013-11-24 21:30:13 +02002455 "Start Search: update_lq %d done_search %d rs_state %d win->counter %d\n",
2456 update_lq, done_search, lq_sta->rs_state,
Eyal Shapira4d30ee82013-11-20 01:45:38 +02002457 window->counter);
Eyal Shapirab3b06a32013-11-24 21:30:13 +02002458
2459 next_column = rs_get_next_column(mvm, lq_sta, sta, tbl);
2460 if (next_column != RS_COLUMN_INVALID) {
2461 int ret = rs_switch_to_column(mvm, lq_sta, sta,
2462 next_column);
2463 if (!ret)
2464 lq_sta->search_better_tbl = 1;
2465 } else {
2466 IWL_DEBUG_RATE(mvm,
2467 "No more columns to explore in search cycle. Go to RS_STATE_SEARCH_CYCLE_ENDED\n");
2468 lq_sta->rs_state = RS_STATE_SEARCH_CYCLE_ENDED;
2469 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01002470
2471 /* If new "search" mode was selected, set up in uCode table */
2472 if (lq_sta->search_better_tbl) {
2473 /* Access the "search" table, clear its history. */
2474 tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Eyal Shapirab804eeb2014-04-06 04:27:06 +03002475 rs_rate_scale_clear_tbl_windows(mvm, tbl);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002476
2477 /* Use new "search" start rate */
Eyal Shapira8fc7c582013-12-04 02:15:46 +02002478 index = tbl->rate.index;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002479
Eyal Shapirab3b06a32013-11-24 21:30:13 +02002480 rs_dump_rate(mvm, &tbl->rate,
2481 "Switch to SEARCH TABLE:");
Eyal Shapira7b9d74e2015-03-29 18:38:07 +03002482 rs_update_rate_tbl(mvm, sta, lq_sta, tbl);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002483 } else {
2484 done_search = 1;
2485 }
2486 }
2487
Eyal Shapirab3b06a32013-11-24 21:30:13 +02002488 if (done_search && lq_sta->rs_state == RS_STATE_SEARCH_CYCLE_ENDED) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002489 /* If the "active" (non-search) mode was legacy,
2490 * and we've tried switching antennas,
2491 * but we haven't been able to try HT modes (not available),
2492 * stay with best antenna legacy modulation for a while
2493 * before next round of mode comparisons. */
2494 tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
Eyal Shapirafd7dbee2014-04-06 04:39:23 +03002495 if (is_legacy(&tbl1->rate)) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002496 IWL_DEBUG_RATE(mvm, "LQ: STAY in legacy table\n");
Eyal Shapirafd7dbee2014-04-06 04:39:23 +03002497
2498 if (tid != IWL_MAX_TID_COUNT) {
2499 tid_data = &sta_priv->tid_data[tid];
2500 if (tid_data->state != IWL_AGG_OFF) {
2501 IWL_DEBUG_RATE(mvm,
2502 "Stop aggregation on tid %d\n",
2503 tid);
2504 ieee80211_stop_tx_ba_session(sta, tid);
2505 }
2506 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01002507 rs_set_stay_in_table(mvm, 1, lq_sta);
Eyal Shapirab3b06a32013-11-24 21:30:13 +02002508 } else {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002509 /* If we're in an HT mode, and all 3 mode switch actions
2510 * have been tried and compared, stay in this best modulation
2511 * mode for a while before next round of mode comparisons. */
Johannes Berg8ca151b2013-01-24 14:25:36 +01002512 if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
2513 (lq_sta->tx_agg_tid_en & (1 << tid)) &&
2514 (tid != IWL_MAX_TID_COUNT)) {
2515 tid_data = &sta_priv->tid_data[tid];
Matti Gottliebff7a68d2016-05-17 10:50:08 +03002516 if (tid_data->state == IWL_AGG_OFF && !ndp) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002517 IWL_DEBUG_RATE(mvm,
2518 "try to aggregate tid %d\n",
2519 tid);
2520 rs_tl_turn_on_agg(mvm, tid,
2521 lq_sta, sta);
2522 }
2523 }
2524 rs_set_stay_in_table(mvm, 0, lq_sta);
2525 }
2526 }
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
2569static const struct rs_init_rate_info rs_optimal_rates_vht_20mhz[] = {
2570 { -60, IWL_RATE_MCS_8_INDEX },
2571 { -64, IWL_RATE_MCS_7_INDEX },
2572 { -68, IWL_RATE_MCS_6_INDEX },
2573 { -72, IWL_RATE_MCS_5_INDEX },
2574 { -80, IWL_RATE_MCS_4_INDEX },
2575 { -84, IWL_RATE_MCS_3_INDEX },
2576 { -85, IWL_RATE_MCS_2_INDEX },
2577 { -87, IWL_RATE_MCS_1_INDEX },
2578 { S8_MIN, IWL_RATE_MCS_0_INDEX},
2579};
2580
2581static const struct rs_init_rate_info rs_optimal_rates_vht_40_80mhz[] = {
2582 { -60, IWL_RATE_MCS_9_INDEX },
2583 { -64, IWL_RATE_MCS_8_INDEX },
2584 { -68, IWL_RATE_MCS_7_INDEX },
2585 { -72, IWL_RATE_MCS_6_INDEX },
2586 { -80, IWL_RATE_MCS_5_INDEX },
2587 { -84, IWL_RATE_MCS_4_INDEX },
2588 { -85, IWL_RATE_MCS_3_INDEX },
2589 { -87, IWL_RATE_MCS_2_INDEX },
2590 { -88, IWL_RATE_MCS_1_INDEX },
2591 { S8_MIN, IWL_RATE_MCS_0_INDEX },
2592};
2593
Gregory Greenman99319b82015-11-24 19:13:27 +02002594#define IWL_RS_LOW_RSSI_THRESHOLD (-76) /* dBm */
2595
Eyal Shapirab503ed62015-05-27 22:20:46 +03002596/* Init the optimal rate based on STA caps
2597 * This combined with rssi is used to report the last tx rate
2598 * to userspace when we haven't transmitted enough frames.
2599 */
2600static void rs_init_optimal_rate(struct iwl_mvm *mvm,
2601 struct ieee80211_sta *sta,
2602 struct iwl_lq_sta *lq_sta)
2603{
2604 struct rs_rate *rate = &lq_sta->optimal_rate;
2605
2606 if (lq_sta->max_mimo2_rate_idx != IWL_RATE_INVALID)
2607 rate->type = lq_sta->is_vht ? LQ_VHT_MIMO2 : LQ_HT_MIMO2;
2608 else if (lq_sta->max_siso_rate_idx != IWL_RATE_INVALID)
2609 rate->type = lq_sta->is_vht ? LQ_VHT_SISO : LQ_HT_SISO;
Johannes Berg57fbcce2016-04-12 15:56:15 +02002610 else if (lq_sta->band == NL80211_BAND_5GHZ)
Eyal Shapirab503ed62015-05-27 22:20:46 +03002611 rate->type = LQ_LEGACY_A;
2612 else
2613 rate->type = LQ_LEGACY_G;
2614
2615 rate->bw = rs_bw_from_sta_bw(sta);
2616 rate->sgi = rs_sgi_allow(mvm, sta, rate, NULL);
2617
2618 /* ANT/LDPC/STBC aren't relevant for the rate reported to userspace */
2619
2620 if (is_mimo(rate)) {
2621 lq_sta->optimal_rate_mask = lq_sta->active_mimo2_rate;
2622 } else if (is_siso(rate)) {
2623 lq_sta->optimal_rate_mask = lq_sta->active_siso_rate;
2624 } else {
2625 lq_sta->optimal_rate_mask = lq_sta->active_legacy_rate;
2626
Johannes Berg57fbcce2016-04-12 15:56:15 +02002627 if (lq_sta->band == NL80211_BAND_5GHZ) {
Eyal Shapirab503ed62015-05-27 22:20:46 +03002628 lq_sta->optimal_rates = rs_optimal_rates_5ghz_legacy;
2629 lq_sta->optimal_nentries =
2630 ARRAY_SIZE(rs_optimal_rates_5ghz_legacy);
2631 } else {
2632 lq_sta->optimal_rates = rs_optimal_rates_24ghz_legacy;
2633 lq_sta->optimal_nentries =
2634 ARRAY_SIZE(rs_optimal_rates_24ghz_legacy);
2635 }
2636 }
2637
2638 if (is_vht(rate)) {
2639 if (rate->bw == RATE_MCS_CHAN_WIDTH_20) {
2640 lq_sta->optimal_rates = rs_optimal_rates_vht_20mhz;
2641 lq_sta->optimal_nentries =
2642 ARRAY_SIZE(rs_optimal_rates_vht_20mhz);
2643 } else {
2644 lq_sta->optimal_rates = rs_optimal_rates_vht_40_80mhz;
2645 lq_sta->optimal_nentries =
2646 ARRAY_SIZE(rs_optimal_rates_vht_40_80mhz);
2647 }
2648 } else if (is_ht(rate)) {
2649 lq_sta->optimal_rates = rs_optimal_rates_ht;
2650 lq_sta->optimal_nentries = ARRAY_SIZE(rs_optimal_rates_ht);
2651 }
2652}
2653
2654/* Compute the optimal rate index based on RSSI */
2655static struct rs_rate *rs_get_optimal_rate(struct iwl_mvm *mvm,
2656 struct iwl_lq_sta *lq_sta)
2657{
2658 struct rs_rate *rate = &lq_sta->optimal_rate;
2659 int i;
2660
2661 rate->index = find_first_bit(&lq_sta->optimal_rate_mask,
2662 BITS_PER_LONG);
2663
2664 for (i = 0; i < lq_sta->optimal_nentries; i++) {
2665 int rate_idx = lq_sta->optimal_rates[i].rate_idx;
2666
2667 if ((lq_sta->pers.last_rssi >= lq_sta->optimal_rates[i].rssi) &&
2668 (BIT(rate_idx) & lq_sta->optimal_rate_mask)) {
2669 rate->index = rate_idx;
2670 break;
2671 }
2672 }
2673
Eyal Shapirab503ed62015-05-27 22:20:46 +03002674 return rate;
2675}
2676
Eyal Shapira361dbec2014-08-13 00:31:13 +03002677/* Choose an initial legacy rate and antenna to use based on the RSSI
2678 * of last Rx
2679 */
2680static void rs_get_initial_rate(struct iwl_mvm *mvm,
Gregory Greenman99319b82015-11-24 19:13:27 +02002681 struct ieee80211_sta *sta,
Eyal Shapira361dbec2014-08-13 00:31:13 +03002682 struct iwl_lq_sta *lq_sta,
Johannes Berg57fbcce2016-04-12 15:56:15 +02002683 enum nl80211_band band,
Eyal Shapira361dbec2014-08-13 00:31:13 +03002684 struct rs_rate *rate)
2685{
2686 int i, nentries;
Gregory Greenman99319b82015-11-24 19:13:27 +02002687 unsigned long active_rate;
Eyal Shapira361dbec2014-08-13 00:31:13 +03002688 s8 best_rssi = S8_MIN;
2689 u8 best_ant = ANT_NONE;
Moshe Harela0544272014-12-08 21:13:14 +02002690 u8 valid_tx_ant = iwl_mvm_get_valid_tx_ant(mvm);
Eyal Shapira361dbec2014-08-13 00:31:13 +03002691 const struct rs_init_rate_info *initial_rates;
2692
2693 for (i = 0; i < ARRAY_SIZE(lq_sta->pers.chain_signal); i++) {
2694 if (!(lq_sta->pers.chains & BIT(i)))
2695 continue;
2696
2697 if (lq_sta->pers.chain_signal[i] > best_rssi) {
2698 best_rssi = lq_sta->pers.chain_signal[i];
2699 best_ant = BIT(i);
2700 }
2701 }
2702
2703 IWL_DEBUG_RATE(mvm, "Best ANT: %s Best RSSI: %d\n",
2704 rs_pretty_ant(best_ant), best_rssi);
2705
2706 if (best_ant != ANT_A && best_ant != ANT_B)
2707 rate->ant = first_antenna(valid_tx_ant);
2708 else
2709 rate->ant = best_ant;
2710
2711 rate->sgi = false;
2712 rate->ldpc = false;
2713 rate->bw = RATE_MCS_CHAN_WIDTH_20;
2714
2715 rate->index = find_first_bit(&lq_sta->active_legacy_rate,
2716 BITS_PER_LONG);
2717
Johannes Berg57fbcce2016-04-12 15:56:15 +02002718 if (band == NL80211_BAND_5GHZ) {
Eyal Shapira361dbec2014-08-13 00:31:13 +03002719 rate->type = LQ_LEGACY_A;
Eyal Shapirab503ed62015-05-27 22:20:46 +03002720 initial_rates = rs_optimal_rates_5ghz_legacy;
2721 nentries = ARRAY_SIZE(rs_optimal_rates_5ghz_legacy);
Eyal Shapira361dbec2014-08-13 00:31:13 +03002722 } else {
2723 rate->type = LQ_LEGACY_G;
Eyal Shapirab503ed62015-05-27 22:20:46 +03002724 initial_rates = rs_optimal_rates_24ghz_legacy;
2725 nentries = ARRAY_SIZE(rs_optimal_rates_24ghz_legacy);
Eyal Shapira361dbec2014-08-13 00:31:13 +03002726 }
2727
Gregory Greenman99319b82015-11-24 19:13:27 +02002728 if (!IWL_MVM_RS_RSSI_BASED_INIT_RATE)
2729 goto out;
2730
2731 /* Start from a higher rate if the corresponding debug capability
2732 * is enabled. The rate is chosen according to AP capabilities.
2733 * In case of VHT/HT when the rssi is low fallback to the case of
2734 * legacy rates.
2735 */
2736 if (sta->vht_cap.vht_supported &&
2737 best_rssi > IWL_RS_LOW_RSSI_THRESHOLD) {
2738 if (sta->bandwidth >= IEEE80211_STA_RX_BW_40) {
2739 initial_rates = rs_optimal_rates_vht_40_80mhz;
2740 nentries = ARRAY_SIZE(rs_optimal_rates_vht_40_80mhz);
2741 if (sta->bandwidth >= IEEE80211_STA_RX_BW_80)
2742 rate->bw = RATE_MCS_CHAN_WIDTH_80;
2743 else
2744 rate->bw = RATE_MCS_CHAN_WIDTH_40;
2745 } else if (sta->bandwidth == IEEE80211_STA_RX_BW_20) {
2746 initial_rates = rs_optimal_rates_vht_20mhz;
2747 nentries = ARRAY_SIZE(rs_optimal_rates_vht_20mhz);
2748 rate->bw = RATE_MCS_CHAN_WIDTH_20;
2749 } else {
2750 IWL_ERR(mvm, "Invalid BW %d\n", sta->bandwidth);
2751 goto out;
2752 }
2753 active_rate = lq_sta->active_siso_rate;
2754 rate->type = LQ_VHT_SISO;
2755 } else if (sta->ht_cap.ht_supported &&
2756 best_rssi > IWL_RS_LOW_RSSI_THRESHOLD) {
2757 initial_rates = rs_optimal_rates_ht;
2758 nentries = ARRAY_SIZE(rs_optimal_rates_ht);
2759 active_rate = lq_sta->active_siso_rate;
2760 rate->type = LQ_HT_SISO;
2761 } else {
2762 active_rate = lq_sta->active_legacy_rate;
2763 }
2764
2765 for (i = 0; i < nentries; i++) {
2766 int rate_idx = initial_rates[i].rate_idx;
2767
2768 if ((best_rssi >= initial_rates[i].rssi) &&
2769 (BIT(rate_idx) & active_rate)) {
2770 rate->index = rate_idx;
2771 break;
Eyal Shapira361dbec2014-08-13 00:31:13 +03002772 }
2773 }
2774
Gregory Greenman99319b82015-11-24 19:13:27 +02002775out:
2776 rs_dump_rate(mvm, rate, "INITIAL");
Eyal Shapira361dbec2014-08-13 00:31:13 +03002777}
2778
2779/* Save info about RSSI of last Rx */
2780void rs_update_last_rssi(struct iwl_mvm *mvm,
2781 struct iwl_lq_sta *lq_sta,
2782 struct ieee80211_rx_status *rx_status)
2783{
Eyal Shapirab503ed62015-05-27 22:20:46 +03002784 int i;
2785
Eyal Shapira361dbec2014-08-13 00:31:13 +03002786 lq_sta->pers.chains = rx_status->chains;
2787 lq_sta->pers.chain_signal[0] = rx_status->chain_signal[0];
2788 lq_sta->pers.chain_signal[1] = rx_status->chain_signal[1];
2789 lq_sta->pers.chain_signal[2] = rx_status->chain_signal[2];
Eyal Shapirab503ed62015-05-27 22:20:46 +03002790 lq_sta->pers.last_rssi = S8_MIN;
2791
2792 for (i = 0; i < ARRAY_SIZE(lq_sta->pers.chain_signal); i++) {
2793 if (!(lq_sta->pers.chains & BIT(i)))
2794 continue;
2795
2796 if (lq_sta->pers.chain_signal[i] > lq_sta->pers.last_rssi)
2797 lq_sta->pers.last_rssi = lq_sta->pers.chain_signal[i];
2798 }
Eyal Shapira361dbec2014-08-13 00:31:13 +03002799}
2800
Johannes Berg8ca151b2013-01-24 14:25:36 +01002801/**
2802 * rs_initialize_lq - Initialize a station's hardware rate table
2803 *
2804 * The uCode's station table contains a table of fallback rates
2805 * for automatic fallback during transmission.
2806 *
2807 * NOTE: This sets up a default set of values. These will be replaced later
2808 * if the driver's iwl-agn-rs rate scaling algorithm is used, instead of
2809 * rc80211_simple.
2810 *
2811 * NOTE: Run REPLY_ADD_STA command to set up station table entry, before
2812 * calling this function (which runs REPLY_TX_LINK_QUALITY_CMD,
2813 * which requires station table entry to exist).
2814 */
2815static void rs_initialize_lq(struct iwl_mvm *mvm,
2816 struct ieee80211_sta *sta,
2817 struct iwl_lq_sta *lq_sta,
Johannes Berg57fbcce2016-04-12 15:56:15 +02002818 enum nl80211_band band,
Eyal Shapirab87c2172013-11-09 23:37:55 +02002819 bool init)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002820{
2821 struct iwl_scale_tbl_info *tbl;
Eyal Shapira5aa33552013-11-23 01:06:36 +02002822 struct rs_rate *rate;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002823 u8 active_tbl = 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002824
2825 if (!sta || !lq_sta)
2826 return;
2827
Johannes Berg8ca151b2013-01-24 14:25:36 +01002828 if (!lq_sta->search_better_tbl)
2829 active_tbl = lq_sta->active_tbl;
2830 else
2831 active_tbl = 1 - lq_sta->active_tbl;
2832
2833 tbl = &(lq_sta->lq_info[active_tbl]);
Eyal Shapira5aa33552013-11-23 01:06:36 +02002834 rate = &tbl->rate;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002835
Gregory Greenman99319b82015-11-24 19:13:27 +02002836 rs_get_initial_rate(mvm, sta, lq_sta, band, rate);
Eyal Shapirab503ed62015-05-27 22:20:46 +03002837 rs_init_optimal_rate(mvm, sta, lq_sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002838
Eyal Shapirab3b06a32013-11-24 21:30:13 +02002839 WARN_ON_ONCE(rate->ant != ANT_A && rate->ant != ANT_B);
Gregory Greenman99319b82015-11-24 19:13:27 +02002840 tbl->column = rs_get_column_from_rate(rate);
Eyal Shapirab3b06a32013-11-24 21:30:13 +02002841
Johannes Berg8ca151b2013-01-24 14:25:36 +01002842 rs_set_expected_tpt_table(lq_sta, tbl);
Eyal Shapira80e9e5c2013-12-23 16:26:41 +02002843 rs_fill_lq_cmd(mvm, sta, lq_sta, rate);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002844 /* TODO restore station should remember the lq cmd */
Eyal Shapirab87c2172013-11-09 23:37:55 +02002845 iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, init);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002846}
2847
2848static void rs_get_rate(void *mvm_r, struct ieee80211_sta *sta, void *mvm_sta,
2849 struct ieee80211_tx_rate_control *txrc)
2850{
2851 struct sk_buff *skb = txrc->skb;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002852 struct iwl_op_mode *op_mode __maybe_unused =
2853 (struct iwl_op_mode *)mvm_r;
2854 struct iwl_mvm *mvm __maybe_unused = IWL_OP_MODE_GET_MVM(op_mode);
2855 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2856 struct iwl_lq_sta *lq_sta = mvm_sta;
Eyal Shapirab503ed62015-05-27 22:20:46 +03002857 struct rs_rate *optimal_rate;
2858 u32 last_ucode_rate;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002859
Johannes Bergdcbc17a2015-03-10 21:24:34 +01002860 if (sta && !iwl_mvm_sta_from_mac80211(sta)->vif) {
2861 /* if vif isn't initialized mvm doesn't know about
2862 * this station, so don't do anything with the it
2863 */
2864 sta = NULL;
2865 mvm_sta = NULL;
2866 }
2867
Eyal Shapira2536e7c2014-08-31 02:33:33 +03002868 /* TODO: handle rate_idx_mask and rate_idx_mcs_mask */
Johannes Berg8ca151b2013-01-24 14:25:36 +01002869
2870 /* Treat uninitialized rate scaling data same as non-existing. */
Eliad Pellerae969af2014-06-11 15:51:33 +03002871 if (lq_sta && !lq_sta->pers.drv) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002872 IWL_DEBUG_RATE(mvm, "Rate scaling not initialized yet.\n");
2873 mvm_sta = NULL;
2874 }
2875
2876 /* Send management frames and NO_ACK data using lowest rate. */
2877 if (rate_control_send_low(sta, mvm_sta, txrc))
2878 return;
2879
Eyal Shapirad310e402013-08-11 18:43:47 +03002880 iwl_mvm_hwrate_to_tx_rate(lq_sta->last_rate_n_flags,
2881 info->band, &info->control.rates[0]);
Moshe Benji622ebe92013-06-03 19:27:16 +03002882 info->control.rates[0].count = 1;
Eyal Shapirab503ed62015-05-27 22:20:46 +03002883
2884 /* Report the optimal rate based on rssi and STA caps if we haven't
2885 * converged yet (too little traffic) or exploring other modulations
2886 */
2887 if (lq_sta->rs_state != RS_STATE_STAY_IN_COLUMN) {
2888 optimal_rate = rs_get_optimal_rate(mvm, lq_sta);
2889 last_ucode_rate = ucode_rate_from_rs_rate(mvm,
2890 optimal_rate);
2891 iwl_mvm_hwrate_to_tx_rate(last_ucode_rate, info->band,
2892 &txrc->reported_rate);
2893 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01002894}
2895
2896static void *rs_alloc_sta(void *mvm_rate, struct ieee80211_sta *sta,
2897 gfp_t gfp)
2898{
Johannes Berg9d8ce6a2014-12-23 16:02:40 +01002899 struct iwl_mvm_sta *sta_priv = iwl_mvm_sta_from_mac80211(sta);
Eliad Pellerae969af2014-06-11 15:51:33 +03002900 struct iwl_op_mode *op_mode = (struct iwl_op_mode *)mvm_rate;
2901 struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
2902 struct iwl_lq_sta *lq_sta = &sta_priv->lq_sta;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002903
2904 IWL_DEBUG_RATE(mvm, "create station rate scale window\n");
2905
Eliad Pellerae969af2014-06-11 15:51:33 +03002906 lq_sta->pers.drv = mvm;
2907#ifdef CONFIG_MAC80211_DEBUGFS
2908 lq_sta->pers.dbg_fixed_rate = 0;
2909 lq_sta->pers.dbg_fixed_txp_reduction = TPC_INVALID;
Eyal Shapira878985ce2015-02-02 12:14:54 +02002910 lq_sta->pers.ss_force = RS_SS_FORCE_NONE;
Eliad Pellerae969af2014-06-11 15:51:33 +03002911#endif
Eyal Shapira361dbec2014-08-13 00:31:13 +03002912 lq_sta->pers.chains = 0;
2913 memset(lq_sta->pers.chain_signal, 0, sizeof(lq_sta->pers.chain_signal));
Eyal Shapirab503ed62015-05-27 22:20:46 +03002914 lq_sta->pers.last_rssi = S8_MIN;
Eliad Pellerae969af2014-06-11 15:51:33 +03002915
Johannes Berg8ca151b2013-01-24 14:25:36 +01002916 return &sta_priv->lq_sta;
2917}
2918
Eyal Shapirad310e402013-08-11 18:43:47 +03002919static int rs_vht_highest_rx_mcs_index(struct ieee80211_sta_vht_cap *vht_cap,
2920 int nss)
2921{
2922 u16 rx_mcs = le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map) &
2923 (0x3 << (2 * (nss - 1)));
2924 rx_mcs >>= (2 * (nss - 1));
2925
2926 if (rx_mcs == IEEE80211_VHT_MCS_SUPPORT_0_7)
2927 return IWL_RATE_MCS_7_INDEX;
2928 else if (rx_mcs == IEEE80211_VHT_MCS_SUPPORT_0_8)
2929 return IWL_RATE_MCS_8_INDEX;
2930 else if (rx_mcs == IEEE80211_VHT_MCS_SUPPORT_0_9)
2931 return IWL_RATE_MCS_9_INDEX;
2932
2933 WARN_ON_ONCE(rx_mcs != IEEE80211_VHT_MCS_NOT_SUPPORTED);
2934 return -1;
2935}
2936
Eyal Shapira750a1b42013-11-09 22:48:56 +02002937static void rs_vht_set_enabled_rates(struct ieee80211_sta *sta,
2938 struct ieee80211_sta_vht_cap *vht_cap,
2939 struct iwl_lq_sta *lq_sta)
2940{
2941 int i;
2942 int highest_mcs = rs_vht_highest_rx_mcs_index(vht_cap, 1);
2943
2944 if (highest_mcs >= IWL_RATE_MCS_0_INDEX) {
2945 for (i = IWL_RATE_MCS_0_INDEX; i <= highest_mcs; i++) {
2946 if (i == IWL_RATE_9M_INDEX)
2947 continue;
2948
Eyal Shapira271518a2013-11-09 23:25:06 +02002949 /* VHT MCS9 isn't valid for 20Mhz for NSS=1,2 */
2950 if (i == IWL_RATE_MCS_9_INDEX &&
2951 sta->bandwidth == IEEE80211_STA_RX_BW_20)
2952 continue;
2953
Eyal Shapira750a1b42013-11-09 22:48:56 +02002954 lq_sta->active_siso_rate |= BIT(i);
2955 }
2956 }
2957
Eyal Shapiraecc90e72013-11-09 23:31:38 +02002958 if (sta->rx_nss < 2)
2959 return;
2960
Eyal Shapira750a1b42013-11-09 22:48:56 +02002961 highest_mcs = rs_vht_highest_rx_mcs_index(vht_cap, 2);
2962 if (highest_mcs >= IWL_RATE_MCS_0_INDEX) {
2963 for (i = IWL_RATE_MCS_0_INDEX; i <= highest_mcs; i++) {
2964 if (i == IWL_RATE_9M_INDEX)
2965 continue;
2966
Eyal Shapira271518a2013-11-09 23:25:06 +02002967 /* VHT MCS9 isn't valid for 20Mhz for NSS=1,2 */
2968 if (i == IWL_RATE_MCS_9_INDEX &&
2969 sta->bandwidth == IEEE80211_STA_RX_BW_20)
2970 continue;
2971
Eyal Shapira750a1b42013-11-09 22:48:56 +02002972 lq_sta->active_mimo2_rate |= BIT(i);
2973 }
2974 }
2975}
2976
Johannes Berg2f15a822015-01-21 18:05:04 +01002977static void rs_ht_init(struct iwl_mvm *mvm,
2978 struct ieee80211_sta *sta,
2979 struct iwl_lq_sta *lq_sta,
2980 struct ieee80211_sta_ht_cap *ht_cap)
2981{
2982 /* active_siso_rate mask includes 9 MBits (bit 5),
2983 * and CCK (bits 0-3), supp_rates[] does not;
2984 * shift to convert format, force 9 MBits off.
2985 */
2986 lq_sta->active_siso_rate = ht_cap->mcs.rx_mask[0] << 1;
2987 lq_sta->active_siso_rate |= ht_cap->mcs.rx_mask[0] & 0x1;
2988 lq_sta->active_siso_rate &= ~((u16)0x2);
2989 lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE;
2990
2991 lq_sta->active_mimo2_rate = ht_cap->mcs.rx_mask[1] << 1;
2992 lq_sta->active_mimo2_rate |= ht_cap->mcs.rx_mask[1] & 0x1;
2993 lq_sta->active_mimo2_rate &= ~((u16)0x2);
2994 lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE;
2995
2996 if (mvm->cfg->ht_params->ldpc &&
2997 (ht_cap->cap & IEEE80211_HT_CAP_LDPC_CODING))
2998 lq_sta->ldpc = true;
2999
3000 if (mvm->cfg->ht_params->stbc &&
3001 (num_of_ant(iwl_mvm_get_valid_tx_ant(mvm)) > 1) &&
3002 (ht_cap->cap & IEEE80211_HT_CAP_RX_STBC))
Eyal Shapira3d44eeb2015-01-16 22:37:04 +02003003 lq_sta->stbc_capable = true;
Johannes Berg2f15a822015-01-21 18:05:04 +01003004
3005 lq_sta->is_vht = false;
3006}
3007
3008static void rs_vht_init(struct iwl_mvm *mvm,
3009 struct ieee80211_sta *sta,
3010 struct iwl_lq_sta *lq_sta,
3011 struct ieee80211_sta_vht_cap *vht_cap)
3012{
3013 rs_vht_set_enabled_rates(sta, vht_cap, lq_sta);
3014
3015 if (mvm->cfg->ht_params->ldpc &&
3016 (vht_cap->cap & IEEE80211_VHT_CAP_RXLDPC))
3017 lq_sta->ldpc = true;
3018
3019 if (mvm->cfg->ht_params->stbc &&
3020 (num_of_ant(iwl_mvm_get_valid_tx_ant(mvm)) > 1) &&
3021 (vht_cap->cap & IEEE80211_VHT_CAP_RXSTBC_MASK))
Eyal Shapira3d44eeb2015-01-16 22:37:04 +02003022 lq_sta->stbc_capable = true;
3023
Johannes Berg859d9142015-06-01 17:11:11 +02003024 if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_BEAMFORMER) &&
Eyal Shapira3d44eeb2015-01-16 22:37:04 +02003025 (num_of_ant(iwl_mvm_get_valid_tx_ant(mvm)) > 1) &&
3026 (vht_cap->cap & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE))
3027 lq_sta->bfer_capable = true;
Johannes Berg2f15a822015-01-21 18:05:04 +01003028
3029 lq_sta->is_vht = true;
3030}
3031
Eyal Shapira5fc0f762014-01-28 01:35:32 +02003032#ifdef CONFIG_IWLWIFI_DEBUGFS
Johannes Berg2f15a822015-01-21 18:05:04 +01003033static void iwl_mvm_reset_frame_stats(struct iwl_mvm *mvm)
Eyal Shapira5fc0f762014-01-28 01:35:32 +02003034{
3035 spin_lock_bh(&mvm->drv_stats_lock);
Johannes Berg2f15a822015-01-21 18:05:04 +01003036 memset(&mvm->drv_rx_stats, 0, sizeof(mvm->drv_rx_stats));
Eyal Shapira5fc0f762014-01-28 01:35:32 +02003037 spin_unlock_bh(&mvm->drv_stats_lock);
3038}
3039
Johannes Berg2f15a822015-01-21 18:05:04 +01003040void iwl_mvm_update_frame_stats(struct iwl_mvm *mvm, u32 rate, bool agg)
Eyal Shapira5fc0f762014-01-28 01:35:32 +02003041{
3042 u8 nss = 0, mcs = 0;
3043
3044 spin_lock(&mvm->drv_stats_lock);
3045
3046 if (agg)
Johannes Berg2f15a822015-01-21 18:05:04 +01003047 mvm->drv_rx_stats.agg_frames++;
Eyal Shapira5fc0f762014-01-28 01:35:32 +02003048
Johannes Berg2f15a822015-01-21 18:05:04 +01003049 mvm->drv_rx_stats.success_frames++;
Eyal Shapira5fc0f762014-01-28 01:35:32 +02003050
3051 switch (rate & RATE_MCS_CHAN_WIDTH_MSK) {
3052 case RATE_MCS_CHAN_WIDTH_20:
Johannes Berg2f15a822015-01-21 18:05:04 +01003053 mvm->drv_rx_stats.bw_20_frames++;
Eyal Shapira5fc0f762014-01-28 01:35:32 +02003054 break;
3055 case RATE_MCS_CHAN_WIDTH_40:
Johannes Berg2f15a822015-01-21 18:05:04 +01003056 mvm->drv_rx_stats.bw_40_frames++;
Eyal Shapira5fc0f762014-01-28 01:35:32 +02003057 break;
3058 case RATE_MCS_CHAN_WIDTH_80:
Johannes Berg2f15a822015-01-21 18:05:04 +01003059 mvm->drv_rx_stats.bw_80_frames++;
Eyal Shapira5fc0f762014-01-28 01:35:32 +02003060 break;
3061 default:
3062 WARN_ONCE(1, "bad BW. rate 0x%x", rate);
3063 }
3064
3065 if (rate & RATE_MCS_HT_MSK) {
Johannes Berg2f15a822015-01-21 18:05:04 +01003066 mvm->drv_rx_stats.ht_frames++;
Eyal Shapira5fc0f762014-01-28 01:35:32 +02003067 mcs = rate & RATE_HT_MCS_RATE_CODE_MSK;
3068 nss = ((rate & RATE_HT_MCS_NSS_MSK) >> RATE_HT_MCS_NSS_POS) + 1;
3069 } else if (rate & RATE_MCS_VHT_MSK) {
Johannes Berg2f15a822015-01-21 18:05:04 +01003070 mvm->drv_rx_stats.vht_frames++;
Eyal Shapira5fc0f762014-01-28 01:35:32 +02003071 mcs = rate & RATE_VHT_MCS_RATE_CODE_MSK;
3072 nss = ((rate & RATE_VHT_MCS_NSS_MSK) >>
3073 RATE_VHT_MCS_NSS_POS) + 1;
3074 } else {
Johannes Berg2f15a822015-01-21 18:05:04 +01003075 mvm->drv_rx_stats.legacy_frames++;
Eyal Shapira5fc0f762014-01-28 01:35:32 +02003076 }
3077
3078 if (nss == 1)
Johannes Berg2f15a822015-01-21 18:05:04 +01003079 mvm->drv_rx_stats.siso_frames++;
Eyal Shapira5fc0f762014-01-28 01:35:32 +02003080 else if (nss == 2)
Johannes Berg2f15a822015-01-21 18:05:04 +01003081 mvm->drv_rx_stats.mimo2_frames++;
Eyal Shapira5fc0f762014-01-28 01:35:32 +02003082
3083 if (rate & RATE_MCS_SGI_MSK)
Johannes Berg2f15a822015-01-21 18:05:04 +01003084 mvm->drv_rx_stats.sgi_frames++;
Eyal Shapira5fc0f762014-01-28 01:35:32 +02003085 else
Johannes Berg2f15a822015-01-21 18:05:04 +01003086 mvm->drv_rx_stats.ngi_frames++;
Eyal Shapira5fc0f762014-01-28 01:35:32 +02003087
Johannes Berg2f15a822015-01-21 18:05:04 +01003088 mvm->drv_rx_stats.last_rates[mvm->drv_rx_stats.last_frame_idx] = rate;
3089 mvm->drv_rx_stats.last_frame_idx =
3090 (mvm->drv_rx_stats.last_frame_idx + 1) %
3091 ARRAY_SIZE(mvm->drv_rx_stats.last_rates);
Eyal Shapira5fc0f762014-01-28 01:35:32 +02003092
3093 spin_unlock(&mvm->drv_stats_lock);
3094}
3095#endif
3096
Johannes Berg8ca151b2013-01-24 14:25:36 +01003097/*
3098 * Called after adding a new station to initialize rate scaling
3099 */
3100void iwl_mvm_rs_rate_init(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
Johannes Berg57fbcce2016-04-12 15:56:15 +02003101 enum nl80211_band band, bool init)
Johannes Berg8ca151b2013-01-24 14:25:36 +01003102{
3103 int i, j;
3104 struct ieee80211_hw *hw = mvm->hw;
3105 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Eyal Shapirad310e402013-08-11 18:43:47 +03003106 struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
Johannes Berg9d8ce6a2014-12-23 16:02:40 +01003107 struct iwl_mvm_sta *sta_priv = iwl_mvm_sta_from_mac80211(sta);
3108 struct iwl_lq_sta *lq_sta = &sta_priv->lq_sta;
Johannes Berg8ca151b2013-01-24 14:25:36 +01003109 struct ieee80211_supported_band *sband;
3110 unsigned long supp; /* must be unsigned long for for_each_set_bit */
3111
Eliad Pellerae969af2014-06-11 15:51:33 +03003112 /* clear all non-persistent lq data */
3113 memset(lq_sta, 0, offsetof(typeof(*lq_sta), pers));
Eyal Shapirab87c2172013-11-09 23:37:55 +02003114
Johannes Berg8ca151b2013-01-24 14:25:36 +01003115 sband = hw->wiphy->bands[band];
3116
3117 lq_sta->lq.sta_id = sta_priv->sta_id;
Emmanuel Grumbach04e3a5d2015-10-28 09:47:41 +02003118 sta_priv->tlc_amsdu = false;
Johannes Berg8ca151b2013-01-24 14:25:36 +01003119
3120 for (j = 0; j < LQ_SIZE; j++)
Eyal Shapirab804eeb2014-04-06 04:27:06 +03003121 rs_rate_scale_clear_tbl_windows(mvm, &lq_sta->lq_info[j]);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003122
3123 lq_sta->flush_timer = 0;
Eyal Shapira87d5e412014-04-06 18:13:22 +03003124 lq_sta->last_tx = jiffies;
Johannes Berg8ca151b2013-01-24 14:25:36 +01003125
3126 IWL_DEBUG_RATE(mvm,
3127 "LQ: *** rate scale station global init for station %d ***\n",
3128 sta_priv->sta_id);
3129 /* TODO: what is a good starting rate for STA? About middle? Maybe not
3130 * the lowest or the highest rate.. Could consider using RSSI from
3131 * previous packets? Need to have IEEE 802.1X auth succeed immediately
3132 * after assoc.. */
3133
Eyal Shapira834437d2014-12-17 15:38:58 +02003134 lq_sta->missed_rate_counter = IWL_MVM_RS_MISSED_RATE_MAX;
Johannes Berg8ca151b2013-01-24 14:25:36 +01003135 lq_sta->band = sband->band;
3136 /*
3137 * active legacy rates as per supported rates bitmap
3138 */
3139 supp = sta->supp_rates[sband->band];
3140 lq_sta->active_legacy_rate = 0;
3141 for_each_set_bit(i, &supp, BITS_PER_LONG)
3142 lq_sta->active_legacy_rate |= BIT(sband->bitrates[i].hw_value);
3143
Eyal Shapirad310e402013-08-11 18:43:47 +03003144 /* TODO: should probably account for rx_highest for both HT/VHT */
Eyal Shapira2c59d802015-01-16 11:01:51 +02003145 if (!vht_cap || !vht_cap->vht_supported)
3146 rs_ht_init(mvm, sta, lq_sta, ht_cap);
3147 else
3148 rs_vht_init(mvm, sta, lq_sta, vht_cap);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003149
Eyal Shapira6a028d92014-12-07 18:18:27 +02003150 lq_sta->max_legacy_rate_idx =
3151 rs_get_max_rate_from_mask(lq_sta->active_legacy_rate);
3152 lq_sta->max_siso_rate_idx =
3153 rs_get_max_rate_from_mask(lq_sta->active_siso_rate);
3154 lq_sta->max_mimo2_rate_idx =
3155 rs_get_max_rate_from_mask(lq_sta->active_mimo2_rate);
Eyal Shapira198266a2014-03-31 22:37:39 +03003156
Eyal Shapiraa3576ff2014-08-09 10:57:59 +03003157 IWL_DEBUG_RATE(mvm,
Eyal Shapira3d44eeb2015-01-16 22:37:04 +02003158 "LEGACY=%lX SISO=%lX MIMO2=%lX VHT=%d LDPC=%d STBC=%d BFER=%d\n",
Eyal Shapira198266a2014-03-31 22:37:39 +03003159 lq_sta->active_legacy_rate,
Johannes Berg8ca151b2013-01-24 14:25:36 +01003160 lq_sta->active_siso_rate,
Eyal Shapirad310e402013-08-11 18:43:47 +03003161 lq_sta->active_mimo2_rate,
Eyal Shapira3d44eeb2015-01-16 22:37:04 +02003162 lq_sta->is_vht, lq_sta->ldpc, lq_sta->stbc_capable,
3163 lq_sta->bfer_capable);
Eyal Shapira198266a2014-03-31 22:37:39 +03003164 IWL_DEBUG_RATE(mvm, "MAX RATE: LEGACY=%d SISO=%d MIMO2=%d\n",
3165 lq_sta->max_legacy_rate_idx,
3166 lq_sta->max_siso_rate_idx,
3167 lq_sta->max_mimo2_rate_idx);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003168
3169 /* These values will be overridden later */
3170 lq_sta->lq.single_stream_ant_msk =
Moshe Harela0544272014-12-08 21:13:14 +02003171 first_antenna(iwl_mvm_get_valid_tx_ant(mvm));
Eyal Shapira809bccf2013-11-28 02:25:24 +02003172 lq_sta->lq.dual_stream_ant_msk = ANT_AB;
Johannes Berg8ca151b2013-01-24 14:25:36 +01003173
3174 /* as default allow aggregation for all tids */
3175 lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
Johannes Berg8ca151b2013-01-24 14:25:36 +01003176 lq_sta->is_agg = 0;
Eyal Shapira5fc0f762014-01-28 01:35:32 +02003177#ifdef CONFIG_IWLWIFI_DEBUGFS
Johannes Berg2f15a822015-01-21 18:05:04 +01003178 iwl_mvm_reset_frame_stats(mvm);
Eyal Shapira5fc0f762014-01-28 01:35:32 +02003179#endif
Eyal Shapirab87c2172013-11-09 23:37:55 +02003180 rs_initialize_lq(mvm, sta, lq_sta, band, init);
3181}
3182
3183static void rs_rate_update(void *mvm_r,
3184 struct ieee80211_supported_band *sband,
3185 struct cfg80211_chan_def *chandef,
3186 struct ieee80211_sta *sta, void *priv_sta,
3187 u32 changed)
3188{
3189 u8 tid;
3190 struct iwl_op_mode *op_mode =
3191 (struct iwl_op_mode *)mvm_r;
3192 struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
3193
Johannes Bergdcbc17a2015-03-10 21:24:34 +01003194 if (!iwl_mvm_sta_from_mac80211(sta)->vif)
3195 return;
3196
Eyal Shapirab87c2172013-11-09 23:37:55 +02003197 /* Stop any ongoing aggregations as rs starts off assuming no agg */
3198 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++)
3199 ieee80211_stop_tx_ba_session(sta, tid);
3200
3201 iwl_mvm_rs_rate_init(mvm, sta, sband->band, false);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003202}
3203
Eyal Shapira8fc7c582013-12-04 02:15:46 +02003204#ifdef CONFIG_MAC80211_DEBUGFS
3205static void rs_build_rates_table_from_fixed(struct iwl_mvm *mvm,
3206 struct iwl_lq_cmd *lq_cmd,
Johannes Berg57fbcce2016-04-12 15:56:15 +02003207 enum nl80211_band band,
Eyal Shapira8fc7c582013-12-04 02:15:46 +02003208 u32 ucode_rate)
3209{
3210 struct rs_rate rate;
3211 int i;
3212 int num_rates = ARRAY_SIZE(lq_cmd->rs_table);
3213 __le32 ucode_rate_le32 = cpu_to_le32(ucode_rate);
Emmanuel Grumbach7ceb98a2014-07-28 12:59:45 +03003214 u8 ant = (ucode_rate & RATE_MCS_ANT_ABC_MSK) >> RATE_MCS_ANT_POS;
Eyal Shapira8fc7c582013-12-04 02:15:46 +02003215
3216 for (i = 0; i < num_rates; i++)
3217 lq_cmd->rs_table[i] = ucode_rate_le32;
3218
3219 rs_rate_from_ucode_rate(ucode_rate, band, &rate);
3220
3221 if (is_mimo(&rate))
3222 lq_cmd->mimo_delim = num_rates - 1;
3223 else
3224 lq_cmd->mimo_delim = 0;
Emmanuel Grumbach7ceb98a2014-07-28 12:59:45 +03003225
3226 lq_cmd->reduced_tpc = 0;
3227
3228 if (num_of_ant(ant) == 1)
3229 lq_cmd->single_stream_ant_msk = ant;
3230
3231 lq_cmd->agg_frame_cnt_limit = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
Eyal Shapira8fc7c582013-12-04 02:15:46 +02003232}
3233#endif /* CONFIG_MAC80211_DEBUGFS */
3234
Eyal Shapirae07be6d2013-12-09 13:02:56 +02003235static void rs_fill_rates_for_column(struct iwl_mvm *mvm,
3236 struct iwl_lq_sta *lq_sta,
3237 struct rs_rate *rate,
3238 __le32 *rs_table, int *rs_table_index,
3239 int num_rates, int num_retries,
3240 u8 valid_tx_ant, bool toggle_ant)
3241{
3242 int i, j;
3243 __le32 ucode_rate;
3244 bool bottom_reached = false;
3245 int prev_rate_idx = rate->index;
3246 int end = LINK_QUAL_MAX_RETRY_NUM;
3247 int index = *rs_table_index;
3248
3249 for (i = 0; i < num_rates && index < end; i++) {
Eyal Shapirabd999312014-12-28 22:12:38 +02003250 for (j = 0; j < num_retries && index < end; j++, index++) {
3251 ucode_rate = cpu_to_le32(ucode_rate_from_rs_rate(mvm,
3252 rate));
Eyal Shapirae07be6d2013-12-09 13:02:56 +02003253 rs_table[index] = ucode_rate;
Eyal Shapirabd999312014-12-28 22:12:38 +02003254 if (toggle_ant)
3255 rs_toggle_antenna(valid_tx_ant, rate);
3256 }
Eyal Shapirae07be6d2013-12-09 13:02:56 +02003257
3258 prev_rate_idx = rate->index;
3259 bottom_reached = rs_get_lower_rate_in_column(lq_sta, rate);
3260 if (bottom_reached && !is_legacy(rate))
3261 break;
3262 }
3263
Eyal Shapirabd999312014-12-28 22:12:38 +02003264 if (!bottom_reached && !is_legacy(rate))
Eyal Shapirae07be6d2013-12-09 13:02:56 +02003265 rate->index = prev_rate_idx;
3266
3267 *rs_table_index = index;
3268}
3269
3270/* Building the rate table is non trivial. When we're in MIMO2/VHT/80Mhz/SGI
3271 * column the rate table should look like this:
3272 *
3273 * rate[0] 0x400D019 VHT | ANT: AB BW: 80Mhz MCS: 9 NSS: 2 SGI
3274 * rate[1] 0x400D019 VHT | ANT: AB BW: 80Mhz MCS: 9 NSS: 2 SGI
3275 * rate[2] 0x400D018 VHT | ANT: AB BW: 80Mhz MCS: 8 NSS: 2 SGI
3276 * rate[3] 0x400D018 VHT | ANT: AB BW: 80Mhz MCS: 8 NSS: 2 SGI
3277 * rate[4] 0x400D017 VHT | ANT: AB BW: 80Mhz MCS: 7 NSS: 2 SGI
3278 * rate[5] 0x400D017 VHT | ANT: AB BW: 80Mhz MCS: 7 NSS: 2 SGI
3279 * rate[6] 0x4005007 VHT | ANT: A BW: 80Mhz MCS: 7 NSS: 1 NGI
3280 * rate[7] 0x4009006 VHT | ANT: B BW: 80Mhz MCS: 6 NSS: 1 NGI
3281 * rate[8] 0x4005005 VHT | ANT: A BW: 80Mhz MCS: 5 NSS: 1 NGI
3282 * rate[9] 0x800B Legacy | ANT: B Rate: 36 Mbps
3283 * rate[10] 0x4009 Legacy | ANT: A Rate: 24 Mbps
3284 * rate[11] 0x8007 Legacy | ANT: B Rate: 18 Mbps
3285 * rate[12] 0x4005 Legacy | ANT: A Rate: 12 Mbps
3286 * rate[13] 0x800F Legacy | ANT: B Rate: 9 Mbps
3287 * rate[14] 0x400D Legacy | ANT: A Rate: 6 Mbps
3288 * rate[15] 0x800D Legacy | ANT: B Rate: 6 Mbps
3289 */
Eyal Shapira8fc7c582013-12-04 02:15:46 +02003290static void rs_build_rates_table(struct iwl_mvm *mvm,
Eyal Shapirad8bafea2014-10-23 13:58:38 +03003291 struct ieee80211_sta *sta,
Eyal Shapira8fc7c582013-12-04 02:15:46 +02003292 struct iwl_lq_sta *lq_sta,
3293 const struct rs_rate *initial_rate)
Johannes Berg8ca151b2013-01-24 14:25:36 +01003294{
Eyal Shapira5aa33552013-11-23 01:06:36 +02003295 struct rs_rate rate;
Eyal Shapirae07be6d2013-12-09 13:02:56 +02003296 int num_rates, num_retries, index = 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +01003297 u8 valid_tx_ant = 0;
3298 struct iwl_lq_cmd *lq_cmd = &lq_sta->lq;
Eyal Shapirae07be6d2013-12-09 13:02:56 +02003299 bool toggle_ant = false;
Johannes Berg8ca151b2013-01-24 14:25:36 +01003300
Emmanuel Grumbach9e898f12013-12-22 10:55:47 +02003301 memcpy(&rate, initial_rate, sizeof(rate));
Johannes Berg8ca151b2013-01-24 14:25:36 +01003302
Moshe Harela0544272014-12-08 21:13:14 +02003303 valid_tx_ant = iwl_mvm_get_valid_tx_ant(mvm);
Eyal Shapirac5d679a2015-01-15 20:22:34 +02003304
Eyal Shapira3d44eeb2015-01-16 22:37:04 +02003305 /* TODO: remove old API when min FW API hits 14 */
Johannes Berg859d9142015-06-01 17:11:11 +02003306 if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_LQ_SS_PARAMS) &&
Eyal Shapira3d44eeb2015-01-16 22:37:04 +02003307 rs_stbc_allow(mvm, sta, lq_sta))
3308 rate.stbc = true;
Johannes Berg8ca151b2013-01-24 14:25:36 +01003309
Eyal Shapirae07be6d2013-12-09 13:02:56 +02003310 if (is_siso(&rate)) {
Eyal Shapira834437d2014-12-17 15:38:58 +02003311 num_rates = IWL_MVM_RS_INITIAL_SISO_NUM_RATES;
3312 num_retries = IWL_MVM_RS_HT_VHT_RETRIES_PER_RATE;
Eyal Shapirae07be6d2013-12-09 13:02:56 +02003313 } else if (is_mimo(&rate)) {
Eyal Shapira834437d2014-12-17 15:38:58 +02003314 num_rates = IWL_MVM_RS_INITIAL_MIMO_NUM_RATES;
3315 num_retries = IWL_MVM_RS_HT_VHT_RETRIES_PER_RATE;
Eyal Shapirae07be6d2013-12-09 13:02:56 +02003316 } else {
Eyal Shapira834437d2014-12-17 15:38:58 +02003317 num_rates = IWL_MVM_RS_INITIAL_LEGACY_NUM_RATES;
Eyal Shapirabd999312014-12-28 22:12:38 +02003318 num_retries = IWL_MVM_RS_INITIAL_LEGACY_RETRIES;
Eyal Shapirae07be6d2013-12-09 13:02:56 +02003319 toggle_ant = true;
Johannes Berg8ca151b2013-01-24 14:25:36 +01003320 }
Eyal Shapirae07be6d2013-12-09 13:02:56 +02003321
3322 rs_fill_rates_for_column(mvm, lq_sta, &rate, lq_cmd->rs_table, &index,
3323 num_rates, num_retries, valid_tx_ant,
3324 toggle_ant);
3325
3326 rs_get_lower_rate_down_column(lq_sta, &rate);
3327
3328 if (is_siso(&rate)) {
Eyal Shapira834437d2014-12-17 15:38:58 +02003329 num_rates = IWL_MVM_RS_SECONDARY_SISO_NUM_RATES;
3330 num_retries = IWL_MVM_RS_SECONDARY_SISO_RETRIES;
Eyal Shapirad9088f62014-03-25 10:25:44 +02003331 lq_cmd->mimo_delim = index;
Eyal Shapirae07be6d2013-12-09 13:02:56 +02003332 } else if (is_legacy(&rate)) {
Eyal Shapira834437d2014-12-17 15:38:58 +02003333 num_rates = IWL_MVM_RS_SECONDARY_LEGACY_NUM_RATES;
Eyal Shapirabd999312014-12-28 22:12:38 +02003334 num_retries = IWL_MVM_RS_SECONDARY_LEGACY_RETRIES;
Eyal Shapirae07be6d2013-12-09 13:02:56 +02003335 } else {
3336 WARN_ON_ONCE(1);
3337 }
3338
3339 toggle_ant = true;
3340
3341 rs_fill_rates_for_column(mvm, lq_sta, &rate, lq_cmd->rs_table, &index,
3342 num_rates, num_retries, valid_tx_ant,
3343 toggle_ant);
3344
3345 rs_get_lower_rate_down_column(lq_sta, &rate);
3346
Eyal Shapira834437d2014-12-17 15:38:58 +02003347 num_rates = IWL_MVM_RS_SECONDARY_LEGACY_NUM_RATES;
Eyal Shapirabd999312014-12-28 22:12:38 +02003348 num_retries = IWL_MVM_RS_SECONDARY_LEGACY_RETRIES;
Eyal Shapirae07be6d2013-12-09 13:02:56 +02003349
3350 rs_fill_rates_for_column(mvm, lq_sta, &rate, lq_cmd->rs_table, &index,
3351 num_rates, num_retries, valid_tx_ant,
3352 toggle_ant);
3353
Eyal Shapira8fc7c582013-12-04 02:15:46 +02003354}
3355
Eyal Shapira3d44eeb2015-01-16 22:37:04 +02003356struct rs_bfer_active_iter_data {
3357 struct ieee80211_sta *exclude_sta;
3358 struct iwl_mvm_sta *bfer_mvmsta;
3359};
3360
3361static void rs_bfer_active_iter(void *_data,
3362 struct ieee80211_sta *sta)
3363{
3364 struct rs_bfer_active_iter_data *data = _data;
3365 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3366 struct iwl_lq_cmd *lq_cmd = &mvmsta->lq_sta.lq;
3367 u32 ss_params = le32_to_cpu(lq_cmd->ss_params);
3368
3369 if (sta == data->exclude_sta)
3370 return;
3371
3372 /* The current sta has BFER allowed */
3373 if (ss_params & LQ_SS_BFER_ALLOWED) {
3374 WARN_ON_ONCE(data->bfer_mvmsta != NULL);
3375
3376 data->bfer_mvmsta = mvmsta;
3377 }
3378}
3379
3380static int rs_bfer_priority(struct iwl_mvm_sta *sta)
3381{
3382 int prio = -1;
3383 enum nl80211_iftype viftype = ieee80211_vif_type_p2p(sta->vif);
3384
3385 switch (viftype) {
3386 case NL80211_IFTYPE_AP:
3387 case NL80211_IFTYPE_P2P_GO:
3388 prio = 3;
3389 break;
3390 case NL80211_IFTYPE_P2P_CLIENT:
3391 prio = 2;
3392 break;
3393 case NL80211_IFTYPE_STATION:
3394 prio = 1;
3395 break;
3396 default:
3397 WARN_ONCE(true, "viftype %d sta_id %d", viftype, sta->sta_id);
3398 prio = -1;
3399 }
3400
3401 return prio;
3402}
3403
3404/* Returns >0 if sta1 has a higher BFER priority compared to sta2 */
3405static int rs_bfer_priority_cmp(struct iwl_mvm_sta *sta1,
3406 struct iwl_mvm_sta *sta2)
3407{
3408 int prio1 = rs_bfer_priority(sta1);
3409 int prio2 = rs_bfer_priority(sta2);
3410
3411 if (prio1 > prio2)
3412 return 1;
3413 if (prio1 < prio2)
3414 return -1;
3415 return 0;
3416}
3417
3418static void rs_set_lq_ss_params(struct iwl_mvm *mvm,
3419 struct ieee80211_sta *sta,
3420 struct iwl_lq_sta *lq_sta,
3421 const struct rs_rate *initial_rate)
3422{
3423 struct iwl_lq_cmd *lq_cmd = &lq_sta->lq;
3424 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3425 struct rs_bfer_active_iter_data data = {
3426 .exclude_sta = sta,
3427 .bfer_mvmsta = NULL,
3428 };
3429 struct iwl_mvm_sta *bfer_mvmsta = NULL;
3430 u32 ss_params = LQ_SS_PARAMS_VALID;
3431
3432 if (!iwl_mvm_bt_coex_is_mimo_allowed(mvm, sta))
3433 goto out;
3434
Eyal Shapira878985ce2015-02-02 12:14:54 +02003435#ifdef CONFIG_MAC80211_DEBUGFS
Eyal Shapira1e9c62f2015-01-28 14:44:06 +02003436 /* Check if forcing the decision is configured.
3437 * Note that SISO is forced by not allowing STBC or BFER
3438 */
Eyal Shapira878985ce2015-02-02 12:14:54 +02003439 if (lq_sta->pers.ss_force == RS_SS_FORCE_STBC)
Eyal Shapira1e9c62f2015-01-28 14:44:06 +02003440 ss_params |= (LQ_SS_STBC_1SS_ALLOWED | LQ_SS_FORCE);
Eyal Shapira878985ce2015-02-02 12:14:54 +02003441 else if (lq_sta->pers.ss_force == RS_SS_FORCE_BFER)
Eyal Shapira1e9c62f2015-01-28 14:44:06 +02003442 ss_params |= (LQ_SS_BFER_ALLOWED | LQ_SS_FORCE);
3443
Eyal Shapira878985ce2015-02-02 12:14:54 +02003444 if (lq_sta->pers.ss_force != RS_SS_FORCE_NONE) {
Eyal Shapira1e9c62f2015-01-28 14:44:06 +02003445 IWL_DEBUG_RATE(mvm, "Forcing single stream Tx decision %d\n",
Eyal Shapira878985ce2015-02-02 12:14:54 +02003446 lq_sta->pers.ss_force);
Eyal Shapira1e9c62f2015-01-28 14:44:06 +02003447 goto out;
3448 }
Eyal Shapira878985ce2015-02-02 12:14:54 +02003449#endif
Eyal Shapira1e9c62f2015-01-28 14:44:06 +02003450
Eyal Shapira3d44eeb2015-01-16 22:37:04 +02003451 if (lq_sta->stbc_capable)
3452 ss_params |= LQ_SS_STBC_1SS_ALLOWED;
3453
3454 if (!lq_sta->bfer_capable)
3455 goto out;
3456
3457 ieee80211_iterate_stations_atomic(mvm->hw,
3458 rs_bfer_active_iter,
3459 &data);
3460 bfer_mvmsta = data.bfer_mvmsta;
3461
3462 /* This code is safe as it doesn't run concurrently for different
3463 * stations. This is guaranteed by the fact that calls to
3464 * ieee80211_tx_status wouldn't run concurrently for a single HW.
3465 */
3466 if (!bfer_mvmsta) {
3467 IWL_DEBUG_RATE(mvm, "No sta with BFER allowed found. Allow\n");
3468
3469 ss_params |= LQ_SS_BFER_ALLOWED;
3470 goto out;
3471 }
3472
3473 IWL_DEBUG_RATE(mvm, "Found existing sta %d with BFER activated\n",
3474 bfer_mvmsta->sta_id);
3475
3476 /* Disallow BFER on another STA if active and we're a higher priority */
3477 if (rs_bfer_priority_cmp(mvmsta, bfer_mvmsta) > 0) {
3478 struct iwl_lq_cmd *bfersta_lq_cmd = &bfer_mvmsta->lq_sta.lq;
3479 u32 bfersta_ss_params = le32_to_cpu(bfersta_lq_cmd->ss_params);
3480
3481 bfersta_ss_params &= ~LQ_SS_BFER_ALLOWED;
3482 bfersta_lq_cmd->ss_params = cpu_to_le32(bfersta_ss_params);
3483 iwl_mvm_send_lq_cmd(mvm, bfersta_lq_cmd, false);
3484
3485 ss_params |= LQ_SS_BFER_ALLOWED;
3486 IWL_DEBUG_RATE(mvm,
3487 "Lower priority BFER sta found (%d). Switch BFER\n",
3488 bfer_mvmsta->sta_id);
3489 }
3490out:
3491 lq_cmd->ss_params = cpu_to_le32(ss_params);
3492}
3493
Eyal Shapira8fc7c582013-12-04 02:15:46 +02003494static void rs_fill_lq_cmd(struct iwl_mvm *mvm,
3495 struct ieee80211_sta *sta,
3496 struct iwl_lq_sta *lq_sta,
3497 const struct rs_rate *initial_rate)
3498{
3499 struct iwl_lq_cmd *lq_cmd = &lq_sta->lq;
Emmanuel Grumbach7ceb98a2014-07-28 12:59:45 +03003500 struct iwl_mvm_sta *mvmsta;
3501 struct iwl_mvm_vif *mvmvif;
3502
Eyal Shapira834437d2014-12-17 15:38:58 +02003503 lq_cmd->agg_disable_start_th = IWL_MVM_RS_AGG_DISABLE_START;
Emmanuel Grumbach7ceb98a2014-07-28 12:59:45 +03003504 lq_cmd->agg_time_limit =
Eyal Shapira834437d2014-12-17 15:38:58 +02003505 cpu_to_le16(IWL_MVM_RS_AGG_TIME_LIMIT);
Eyal Shapira8fc7c582013-12-04 02:15:46 +02003506
3507#ifdef CONFIG_MAC80211_DEBUGFS
Eliad Pellerae969af2014-06-11 15:51:33 +03003508 if (lq_sta->pers.dbg_fixed_rate) {
Eyal Shapira8fc7c582013-12-04 02:15:46 +02003509 rs_build_rates_table_from_fixed(mvm, lq_cmd,
3510 lq_sta->band,
Eliad Pellerae969af2014-06-11 15:51:33 +03003511 lq_sta->pers.dbg_fixed_rate);
Emmanuel Grumbach7ceb98a2014-07-28 12:59:45 +03003512 return;
3513 }
Eyal Shapira8fc7c582013-12-04 02:15:46 +02003514#endif
Emmanuel Grumbach7ceb98a2014-07-28 12:59:45 +03003515 if (WARN_ON_ONCE(!sta || !initial_rate))
3516 return;
Eyal Shapira8fc7c582013-12-04 02:15:46 +02003517
Eyal Shapirad8bafea2014-10-23 13:58:38 +03003518 rs_build_rates_table(mvm, sta, lq_sta, initial_rate);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003519
Johannes Berg859d9142015-06-01 17:11:11 +02003520 if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_LQ_SS_PARAMS))
Eyal Shapira3d44eeb2015-01-16 22:37:04 +02003521 rs_set_lq_ss_params(mvm, sta, lq_sta, initial_rate);
3522
Emmanuel Grumbach7ceb98a2014-07-28 12:59:45 +03003523 mvmsta = iwl_mvm_sta_from_mac80211(sta);
3524 mvmvif = iwl_mvm_vif_from_mac80211(mvmsta->vif);
3525
3526 if (num_of_ant(initial_rate->ant) == 1)
3527 lq_cmd->single_stream_ant_msk = initial_rate->ant;
3528
3529 lq_cmd->agg_frame_cnt_limit = mvmsta->max_agg_bufsize;
3530
3531 /*
Sara Sharon0d365ae2015-03-31 12:24:05 +03003532 * In case of low latency, tell the firmware to leave a frame in the
Emmanuel Grumbach7ceb98a2014-07-28 12:59:45 +03003533 * Tx Fifo so that it can start a transaction in the same TxOP. This
3534 * basically allows the firmware to send bursts.
3535 */
Eyal Shapira1412ee32015-10-22 13:10:36 +03003536 if (iwl_mvm_vif_low_latency(mvmvif))
Emmanuel Grumbach7ceb98a2014-07-28 12:59:45 +03003537 lq_cmd->agg_frame_cnt_limit--;
Johannes Berg8ca151b2013-01-24 14:25:36 +01003538
Emmanuel Grumbacha4db8482014-07-31 13:58:53 +03003539 if (mvmsta->vif->p2p)
3540 lq_cmd->flags |= LQ_FLAG_USE_RTS_MSK;
3541
Johannes Berg8ca151b2013-01-24 14:25:36 +01003542 lq_cmd->agg_time_limit =
Emmanuel Grumbach5b7ff612014-03-11 19:27:45 +02003543 cpu_to_le16(iwl_mvm_coex_agg_time_limit(mvm, sta));
Johannes Berg8ca151b2013-01-24 14:25:36 +01003544}
3545
3546static void *rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
3547{
3548 return hw->priv;
3549}
3550/* rate scale requires free function to be implemented */
3551static void rs_free(void *mvm_rate)
3552{
3553 return;
3554}
3555
3556static void rs_free_sta(void *mvm_r, struct ieee80211_sta *sta,
3557 void *mvm_sta)
3558{
3559 struct iwl_op_mode *op_mode __maybe_unused = mvm_r;
3560 struct iwl_mvm *mvm __maybe_unused = IWL_OP_MODE_GET_MVM(op_mode);
3561
3562 IWL_DEBUG_RATE(mvm, "enter\n");
3563 IWL_DEBUG_RATE(mvm, "leave\n");
3564}
3565
3566#ifdef CONFIG_MAC80211_DEBUGFS
Eyal Shapira5fc0f762014-01-28 01:35:32 +02003567int rs_pretty_print_rate(char *buf, const u32 rate)
Eyal Shapira5aa33552013-11-23 01:06:36 +02003568{
3569
Eyal Shapira1a61b342013-11-09 02:11:46 +02003570 char *type, *bw;
3571 u8 mcs = 0, nss = 0;
3572 u8 ant = (rate & RATE_MCS_ANT_ABC_MSK) >> RATE_MCS_ANT_POS;
3573
3574 if (!(rate & RATE_MCS_HT_MSK) &&
3575 !(rate & RATE_MCS_VHT_MSK)) {
3576 int index = iwl_hwrate_to_plcp_idx(rate);
3577
3578 return sprintf(buf, "Legacy | ANT: %s Rate: %s Mbps\n",
Eyal Shapira560843f2014-01-05 21:04:19 +02003579 rs_pretty_ant(ant),
3580 index == IWL_RATE_INVALID ? "BAD" :
3581 iwl_rate_mcs[index].mbps);
Eyal Shapira1a61b342013-11-09 02:11:46 +02003582 }
3583
3584 if (rate & RATE_MCS_VHT_MSK) {
3585 type = "VHT";
3586 mcs = rate & RATE_VHT_MCS_RATE_CODE_MSK;
3587 nss = ((rate & RATE_VHT_MCS_NSS_MSK)
3588 >> RATE_VHT_MCS_NSS_POS) + 1;
3589 } else if (rate & RATE_MCS_HT_MSK) {
3590 type = "HT";
3591 mcs = rate & RATE_HT_MCS_INDEX_MSK;
3592 } else {
3593 type = "Unknown"; /* shouldn't happen */
3594 }
3595
3596 switch (rate & RATE_MCS_CHAN_WIDTH_MSK) {
3597 case RATE_MCS_CHAN_WIDTH_20:
3598 bw = "20Mhz";
3599 break;
3600 case RATE_MCS_CHAN_WIDTH_40:
3601 bw = "40Mhz";
3602 break;
3603 case RATE_MCS_CHAN_WIDTH_80:
3604 bw = "80Mhz";
3605 break;
3606 case RATE_MCS_CHAN_WIDTH_160:
3607 bw = "160Mhz";
3608 break;
3609 default:
3610 bw = "BAD BW";
3611 }
3612
3613 return sprintf(buf, "%s | ANT: %s BW: %s MCS: %d NSS: %d %s%s%s%s%s\n",
Eyal Shapira5aa33552013-11-23 01:06:36 +02003614 type, rs_pretty_ant(ant), bw, mcs, nss,
Eyal Shapira1a61b342013-11-09 02:11:46 +02003615 (rate & RATE_MCS_SGI_MSK) ? "SGI " : "NGI ",
Emmanuel Grumbach7b1dd042014-02-04 15:32:43 +02003616 (rate & RATE_MCS_HT_STBC_MSK) ? "STBC " : "",
Eyal Shapira1a61b342013-11-09 02:11:46 +02003617 (rate & RATE_MCS_LDPC_MSK) ? "LDPC " : "",
3618 (rate & RATE_MCS_BF_MSK) ? "BF " : "",
3619 (rate & RATE_MCS_ZLF_MSK) ? "ZLF " : "");
3620}
3621
Eyal Shapiraa0c38b22013-12-03 18:34:52 +02003622/**
3623 * Program the device to use fixed rate for frame transmit
3624 * This is for debugging/testing only
3625 * once the device start use fixed rate, we need to reload the module
3626 * to being back the normal operation.
3627 */
3628static void rs_program_fix_rate(struct iwl_mvm *mvm,
3629 struct iwl_lq_sta *lq_sta)
3630{
3631 lq_sta->active_legacy_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
3632 lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
3633 lq_sta->active_mimo2_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
3634
3635 IWL_DEBUG_RATE(mvm, "sta_id %d rate 0x%X\n",
Eliad Pellerae969af2014-06-11 15:51:33 +03003636 lq_sta->lq.sta_id, lq_sta->pers.dbg_fixed_rate);
Eyal Shapiraa0c38b22013-12-03 18:34:52 +02003637
Eliad Pellerae969af2014-06-11 15:51:33 +03003638 if (lq_sta->pers.dbg_fixed_rate) {
Emmanuel Grumbach7ceb98a2014-07-28 12:59:45 +03003639 rs_fill_lq_cmd(mvm, NULL, lq_sta, NULL);
Eliad Pellerae969af2014-06-11 15:51:33 +03003640 iwl_mvm_send_lq_cmd(lq_sta->pers.drv, &lq_sta->lq, false);
Eyal Shapiraa0c38b22013-12-03 18:34:52 +02003641 }
3642}
3643
Emmanuel Grumbach9d108492013-12-03 11:50:30 +02003644static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
3645 const char __user *user_buf, size_t count, loff_t *ppos)
3646{
3647 struct iwl_lq_sta *lq_sta = file->private_data;
3648 struct iwl_mvm *mvm;
3649 char buf[64];
3650 size_t buf_size;
3651 u32 parsed_rate;
3652
Eliad Pellerae969af2014-06-11 15:51:33 +03003653 mvm = lq_sta->pers.drv;
Emmanuel Grumbach9d108492013-12-03 11:50:30 +02003654 memset(buf, 0, sizeof(buf));
3655 buf_size = min(count, sizeof(buf) - 1);
3656 if (copy_from_user(buf, user_buf, buf_size))
3657 return -EFAULT;
3658
3659 if (sscanf(buf, "%x", &parsed_rate) == 1)
Eliad Pellerae969af2014-06-11 15:51:33 +03003660 lq_sta->pers.dbg_fixed_rate = parsed_rate;
Emmanuel Grumbach9d108492013-12-03 11:50:30 +02003661 else
Eliad Pellerae969af2014-06-11 15:51:33 +03003662 lq_sta->pers.dbg_fixed_rate = 0;
Emmanuel Grumbach9d108492013-12-03 11:50:30 +02003663
3664 rs_program_fix_rate(mvm, lq_sta);
3665
3666 return count;
3667}
3668
Johannes Berg8ca151b2013-01-24 14:25:36 +01003669static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
3670 char __user *user_buf, size_t count, loff_t *ppos)
3671{
3672 char *buff;
3673 int desc = 0;
3674 int i = 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +01003675 ssize_t ret;
3676
3677 struct iwl_lq_sta *lq_sta = file->private_data;
Emmanuel Grumbach04e3a5d2015-10-28 09:47:41 +02003678 struct iwl_mvm_sta *mvmsta =
3679 container_of(lq_sta, struct iwl_mvm_sta, lq_sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003680 struct iwl_mvm *mvm;
3681 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
Eyal Shapira5aa33552013-11-23 01:06:36 +02003682 struct rs_rate *rate = &tbl->rate;
Eyal Shapira16c426f2015-02-02 12:46:22 +02003683 u32 ss_params;
Emmanuel Grumbach04e3a5d2015-10-28 09:47:41 +02003684
Eliad Pellerae969af2014-06-11 15:51:33 +03003685 mvm = lq_sta->pers.drv;
Eyal Shapira1a61b342013-11-09 02:11:46 +02003686 buff = kmalloc(2048, GFP_KERNEL);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003687 if (!buff)
3688 return -ENOMEM;
3689
3690 desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
Eyal Shapira198266a2014-03-31 22:37:39 +03003691 desc += sprintf(buff+desc, "failed=%d success=%d rate=0%lX\n",
Johannes Berg8ca151b2013-01-24 14:25:36 +01003692 lq_sta->total_failed, lq_sta->total_success,
3693 lq_sta->active_legacy_rate);
3694 desc += sprintf(buff+desc, "fixed rate 0x%X\n",
Eliad Pellerae969af2014-06-11 15:51:33 +03003695 lq_sta->pers.dbg_fixed_rate);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003696 desc += sprintf(buff+desc, "valid_tx_ant %s%s%s\n",
Moshe Harela0544272014-12-08 21:13:14 +02003697 (iwl_mvm_get_valid_tx_ant(mvm) & ANT_A) ? "ANT_A," : "",
3698 (iwl_mvm_get_valid_tx_ant(mvm) & ANT_B) ? "ANT_B," : "",
3699 (iwl_mvm_get_valid_tx_ant(mvm) & ANT_C) ? "ANT_C" : "");
Johannes Berg8ca151b2013-01-24 14:25:36 +01003700 desc += sprintf(buff+desc, "lq type %s\n",
Eyal Shapira5aa33552013-11-23 01:06:36 +02003701 (is_legacy(rate)) ? "legacy" :
3702 is_vht(rate) ? "VHT" : "HT");
3703 if (!is_legacy(rate)) {
Emmanuel Grumbache70fe7e2015-03-01 17:18:00 +02003704 desc += sprintf(buff + desc, " %s",
Eyal Shapira5aa33552013-11-23 01:06:36 +02003705 (is_siso(rate)) ? "SISO" : "MIMO2");
Emmanuel Grumbache70fe7e2015-03-01 17:18:00 +02003706 desc += sprintf(buff + desc, " %s",
3707 (is_ht20(rate)) ? "20MHz" :
3708 (is_ht40(rate)) ? "40MHz" :
3709 (is_ht80(rate)) ? "80Mhz" : "BAD BW");
Emmanuel Grumbach04e3a5d2015-10-28 09:47:41 +02003710 desc += sprintf(buff + desc, " %s %s %s %s\n",
Emmanuel Grumbache70fe7e2015-03-01 17:18:00 +02003711 (rate->sgi) ? "SGI" : "NGI",
3712 (rate->ldpc) ? "LDPC" : "BCC",
Emmanuel Grumbach04e3a5d2015-10-28 09:47:41 +02003713 (lq_sta->is_agg) ? "AGG on" : "",
3714 (mvmsta->tlc_amsdu) ? "AMSDU on" : "");
Johannes Berg8ca151b2013-01-24 14:25:36 +01003715 }
3716 desc += sprintf(buff+desc, "last tx rate=0x%X\n",
3717 lq_sta->last_rate_n_flags);
3718 desc += sprintf(buff+desc,
Eyal Shapira1a61b342013-11-09 02:11:46 +02003719 "general: flags=0x%X mimo-d=%d s-ant=0x%x d-ant=0x%x\n",
Johannes Berg8ca151b2013-01-24 14:25:36 +01003720 lq_sta->lq.flags,
3721 lq_sta->lq.mimo_delim,
3722 lq_sta->lq.single_stream_ant_msk,
3723 lq_sta->lq.dual_stream_ant_msk);
3724
3725 desc += sprintf(buff+desc,
3726 "agg: time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
3727 le16_to_cpu(lq_sta->lq.agg_time_limit),
3728 lq_sta->lq.agg_disable_start_th,
3729 lq_sta->lq.agg_frame_cnt_limit);
3730
Eliad Peller2fd647f2014-03-13 17:21:36 +02003731 desc += sprintf(buff+desc, "reduced tpc=%d\n", lq_sta->lq.reduced_tpc);
Eyal Shapira16c426f2015-02-02 12:46:22 +02003732 ss_params = le32_to_cpu(lq_sta->lq.ss_params);
3733 desc += sprintf(buff+desc, "single stream params: %s%s%s%s\n",
3734 (ss_params & LQ_SS_PARAMS_VALID) ?
Eyal Shapiradb7c6892015-03-02 10:35:19 +02003735 "VALID" : "INVALID",
Eyal Shapira16c426f2015-02-02 12:46:22 +02003736 (ss_params & LQ_SS_BFER_ALLOWED) ?
Eyal Shapiradb7c6892015-03-02 10:35:19 +02003737 ", BFER" : "",
Eyal Shapira16c426f2015-02-02 12:46:22 +02003738 (ss_params & LQ_SS_STBC_1SS_ALLOWED) ?
Eyal Shapiradb7c6892015-03-02 10:35:19 +02003739 ", STBC" : "",
Eyal Shapira16c426f2015-02-02 12:46:22 +02003740 (ss_params & LQ_SS_FORCE) ?
Eyal Shapiradb7c6892015-03-02 10:35:19 +02003741 ", FORCE" : "");
Johannes Berg8ca151b2013-01-24 14:25:36 +01003742 desc += sprintf(buff+desc,
3743 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
3744 lq_sta->lq.initial_rate_index[0],
3745 lq_sta->lq.initial_rate_index[1],
3746 lq_sta->lq.initial_rate_index[2],
3747 lq_sta->lq.initial_rate_index[3]);
3748
3749 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
Johannes Bergcf4ef652013-12-17 11:34:25 +01003750 u32 r = le32_to_cpu(lq_sta->lq.rs_table[i]);
Eyal Shapira1a61b342013-11-09 02:11:46 +02003751
Johannes Bergcf4ef652013-12-17 11:34:25 +01003752 desc += sprintf(buff+desc, " rate[%d] 0x%X ", i, r);
3753 desc += rs_pretty_print_rate(buff+desc, r);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003754 }
3755
3756 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
3757 kfree(buff);
3758 return ret;
3759}
3760
3761static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
3762 .write = rs_sta_dbgfs_scale_table_write,
3763 .read = rs_sta_dbgfs_scale_table_read,
3764 .open = simple_open,
3765 .llseek = default_llseek,
3766};
3767static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
3768 char __user *user_buf, size_t count, loff_t *ppos)
3769{
3770 char *buff;
3771 int desc = 0;
3772 int i, j;
3773 ssize_t ret;
Eyal Shapirad310e402013-08-11 18:43:47 +03003774 struct iwl_scale_tbl_info *tbl;
Eyal Shapira5aa33552013-11-23 01:06:36 +02003775 struct rs_rate *rate;
Johannes Berg8ca151b2013-01-24 14:25:36 +01003776 struct iwl_lq_sta *lq_sta = file->private_data;
3777
3778 buff = kmalloc(1024, GFP_KERNEL);
3779 if (!buff)
3780 return -ENOMEM;
3781
3782 for (i = 0; i < LQ_SIZE; i++) {
Eyal Shapirad310e402013-08-11 18:43:47 +03003783 tbl = &(lq_sta->lq_info[i]);
Eyal Shapira5aa33552013-11-23 01:06:36 +02003784 rate = &tbl->rate;
Johannes Berg8ca151b2013-01-24 14:25:36 +01003785 desc += sprintf(buff+desc,
Eyal Shapira6a524f42013-08-11 20:27:18 +03003786 "%s type=%d SGI=%d BW=%s DUP=0\n"
Eyal Shapira8fc7c582013-12-04 02:15:46 +02003787 "index=%d\n",
Johannes Berg8ca151b2013-01-24 14:25:36 +01003788 lq_sta->active_tbl == i ? "*" : "x",
Eyal Shapira5aa33552013-11-23 01:06:36 +02003789 rate->type,
3790 rate->sgi,
3791 is_ht20(rate) ? "20Mhz" :
3792 is_ht40(rate) ? "40Mhz" :
3793 is_ht80(rate) ? "80Mhz" : "ERR",
Eyal Shapira8fc7c582013-12-04 02:15:46 +02003794 rate->index);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003795 for (j = 0; j < IWL_RATE_COUNT; j++) {
3796 desc += sprintf(buff+desc,
3797 "counter=%d success=%d %%=%d\n",
Eyal Shapirad310e402013-08-11 18:43:47 +03003798 tbl->win[j].counter,
3799 tbl->win[j].success_counter,
3800 tbl->win[j].success_ratio);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003801 }
3802 }
3803 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
3804 kfree(buff);
3805 return ret;
3806}
3807
3808static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
3809 .read = rs_sta_dbgfs_stats_table_read,
3810 .open = simple_open,
3811 .llseek = default_llseek,
3812};
3813
Eyal Shapira757cf23b2014-03-24 02:12:00 +02003814static ssize_t rs_sta_dbgfs_drv_tx_stats_read(struct file *file,
3815 char __user *user_buf,
3816 size_t count, loff_t *ppos)
3817{
3818 static const char * const column_name[] = {
3819 [RS_COLUMN_LEGACY_ANT_A] = "LEGACY_ANT_A",
3820 [RS_COLUMN_LEGACY_ANT_B] = "LEGACY_ANT_B",
3821 [RS_COLUMN_SISO_ANT_A] = "SISO_ANT_A",
3822 [RS_COLUMN_SISO_ANT_B] = "SISO_ANT_B",
3823 [RS_COLUMN_SISO_ANT_A_SGI] = "SISO_ANT_A_SGI",
3824 [RS_COLUMN_SISO_ANT_B_SGI] = "SISO_ANT_B_SGI",
3825 [RS_COLUMN_MIMO2] = "MIMO2",
3826 [RS_COLUMN_MIMO2_SGI] = "MIMO2_SGI",
3827 };
3828
3829 static const char * const rate_name[] = {
3830 [IWL_RATE_1M_INDEX] = "1M",
3831 [IWL_RATE_2M_INDEX] = "2M",
3832 [IWL_RATE_5M_INDEX] = "5.5M",
3833 [IWL_RATE_11M_INDEX] = "11M",
3834 [IWL_RATE_6M_INDEX] = "6M|MCS0",
3835 [IWL_RATE_9M_INDEX] = "9M",
3836 [IWL_RATE_12M_INDEX] = "12M|MCS1",
3837 [IWL_RATE_18M_INDEX] = "18M|MCS2",
3838 [IWL_RATE_24M_INDEX] = "24M|MCS3",
3839 [IWL_RATE_36M_INDEX] = "36M|MCS4",
3840 [IWL_RATE_48M_INDEX] = "48M|MCS5",
3841 [IWL_RATE_54M_INDEX] = "54M|MCS6",
3842 [IWL_RATE_MCS_7_INDEX] = "MCS7",
3843 [IWL_RATE_MCS_8_INDEX] = "MCS8",
3844 [IWL_RATE_MCS_9_INDEX] = "MCS9",
3845 };
3846
3847 char *buff, *pos, *endpos;
3848 int col, rate;
3849 ssize_t ret;
3850 struct iwl_lq_sta *lq_sta = file->private_data;
3851 struct rs_rate_stats *stats;
3852 static const size_t bufsz = 1024;
3853
3854 buff = kmalloc(bufsz, GFP_KERNEL);
3855 if (!buff)
3856 return -ENOMEM;
3857
3858 pos = buff;
3859 endpos = pos + bufsz;
3860
3861 pos += scnprintf(pos, endpos - pos, "COLUMN,");
3862 for (rate = 0; rate < IWL_RATE_COUNT; rate++)
3863 pos += scnprintf(pos, endpos - pos, "%s,", rate_name[rate]);
3864 pos += scnprintf(pos, endpos - pos, "\n");
3865
3866 for (col = 0; col < RS_COLUMN_COUNT; col++) {
3867 pos += scnprintf(pos, endpos - pos,
3868 "%s,", column_name[col]);
3869
3870 for (rate = 0; rate < IWL_RATE_COUNT; rate++) {
Eyal Shapiraf58220f2014-08-16 01:30:30 +03003871 stats = &(lq_sta->pers.tx_stats[col][rate]);
Eyal Shapira757cf23b2014-03-24 02:12:00 +02003872 pos += scnprintf(pos, endpos - pos,
3873 "%llu/%llu,",
3874 stats->success,
3875 stats->total);
3876 }
3877 pos += scnprintf(pos, endpos - pos, "\n");
3878 }
3879
3880 ret = simple_read_from_buffer(user_buf, count, ppos, buff, pos - buff);
3881 kfree(buff);
3882 return ret;
3883}
3884
3885static ssize_t rs_sta_dbgfs_drv_tx_stats_write(struct file *file,
3886 const char __user *user_buf,
3887 size_t count, loff_t *ppos)
3888{
3889 struct iwl_lq_sta *lq_sta = file->private_data;
Eyal Shapiraf58220f2014-08-16 01:30:30 +03003890 memset(lq_sta->pers.tx_stats, 0, sizeof(lq_sta->pers.tx_stats));
Eyal Shapira757cf23b2014-03-24 02:12:00 +02003891
3892 return count;
3893}
3894
3895static const struct file_operations rs_sta_dbgfs_drv_tx_stats_ops = {
3896 .read = rs_sta_dbgfs_drv_tx_stats_read,
3897 .write = rs_sta_dbgfs_drv_tx_stats_write,
3898 .open = simple_open,
3899 .llseek = default_llseek,
3900};
3901
Eyal Shapira1e9c62f2015-01-28 14:44:06 +02003902static ssize_t iwl_dbgfs_ss_force_read(struct file *file,
3903 char __user *user_buf,
3904 size_t count, loff_t *ppos)
3905{
3906 struct iwl_lq_sta *lq_sta = file->private_data;
3907 char buf[12];
3908 int bufsz = sizeof(buf);
3909 int pos = 0;
3910 static const char * const ss_force_name[] = {
3911 [RS_SS_FORCE_NONE] = "none",
3912 [RS_SS_FORCE_STBC] = "stbc",
3913 [RS_SS_FORCE_BFER] = "bfer",
3914 [RS_SS_FORCE_SISO] = "siso",
3915 };
3916
3917 pos += scnprintf(buf+pos, bufsz-pos, "%s\n",
Eyal Shapira878985ce2015-02-02 12:14:54 +02003918 ss_force_name[lq_sta->pers.ss_force]);
Eyal Shapira1e9c62f2015-01-28 14:44:06 +02003919 return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
3920}
3921
3922static ssize_t iwl_dbgfs_ss_force_write(struct iwl_lq_sta *lq_sta, char *buf,
3923 size_t count, loff_t *ppos)
3924{
3925 struct iwl_mvm *mvm = lq_sta->pers.drv;
3926 int ret = 0;
3927
3928 if (!strncmp("none", buf, 4)) {
Eyal Shapira878985ce2015-02-02 12:14:54 +02003929 lq_sta->pers.ss_force = RS_SS_FORCE_NONE;
Eyal Shapira1e9c62f2015-01-28 14:44:06 +02003930 } else if (!strncmp("siso", buf, 4)) {
Eyal Shapira878985ce2015-02-02 12:14:54 +02003931 lq_sta->pers.ss_force = RS_SS_FORCE_SISO;
Eyal Shapira1e9c62f2015-01-28 14:44:06 +02003932 } else if (!strncmp("stbc", buf, 4)) {
3933 if (lq_sta->stbc_capable) {
Eyal Shapira878985ce2015-02-02 12:14:54 +02003934 lq_sta->pers.ss_force = RS_SS_FORCE_STBC;
Eyal Shapira1e9c62f2015-01-28 14:44:06 +02003935 } else {
3936 IWL_ERR(mvm,
3937 "can't force STBC. peer doesn't support\n");
3938 ret = -EINVAL;
3939 }
3940 } else if (!strncmp("bfer", buf, 4)) {
3941 if (lq_sta->bfer_capable) {
Eyal Shapira878985ce2015-02-02 12:14:54 +02003942 lq_sta->pers.ss_force = RS_SS_FORCE_BFER;
Eyal Shapira1e9c62f2015-01-28 14:44:06 +02003943 } else {
3944 IWL_ERR(mvm,
3945 "can't force BFER. peer doesn't support\n");
3946 ret = -EINVAL;
3947 }
3948 } else {
3949 IWL_ERR(mvm, "valid values none|siso|stbc|bfer\n");
3950 ret = -EINVAL;
3951 }
3952 return ret ?: count;
3953}
3954
3955#define MVM_DEBUGFS_READ_WRITE_FILE_OPS(name, bufsz) \
3956 _MVM_DEBUGFS_READ_WRITE_FILE_OPS(name, bufsz, struct iwl_lq_sta)
3957#define MVM_DEBUGFS_ADD_FILE_RS(name, parent, mode) do { \
3958 if (!debugfs_create_file(#name, mode, parent, lq_sta, \
3959 &iwl_dbgfs_##name##_ops)) \
3960 goto err; \
3961 } while (0)
3962
3963MVM_DEBUGFS_READ_WRITE_FILE_OPS(ss_force, 32);
3964
Johannes Bergdcbc17a2015-03-10 21:24:34 +01003965static void rs_add_debugfs(void *mvm, void *priv_sta, struct dentry *dir)
Johannes Berg8ca151b2013-01-24 14:25:36 +01003966{
Johannes Bergdcbc17a2015-03-10 21:24:34 +01003967 struct iwl_lq_sta *lq_sta = priv_sta;
3968 struct iwl_mvm_sta *mvmsta;
3969
3970 mvmsta = container_of(lq_sta, struct iwl_mvm_sta, lq_sta);
3971
3972 if (!mvmsta->vif)
3973 return;
Eyal Shapira1e9c62f2015-01-28 14:44:06 +02003974
Eliad Pellerc6e1faa2014-06-15 12:02:20 +03003975 debugfs_create_file("rate_scale_table", S_IRUSR | S_IWUSR, dir,
3976 lq_sta, &rs_sta_dbgfs_scale_table_ops);
3977 debugfs_create_file("rate_stats_table", S_IRUSR, dir,
3978 lq_sta, &rs_sta_dbgfs_stats_table_ops);
3979 debugfs_create_file("drv_tx_stats", S_IRUSR | S_IWUSR, dir,
3980 lq_sta, &rs_sta_dbgfs_drv_tx_stats_ops);
3981 debugfs_create_u8("tx_agg_tid_enable", S_IRUSR | S_IWUSR, dir,
3982 &lq_sta->tx_agg_tid_en);
3983 debugfs_create_u8("reduced_tpc", S_IRUSR | S_IWUSR, dir,
3984 &lq_sta->pers.dbg_fixed_txp_reduction);
Eyal Shapira1e9c62f2015-01-28 14:44:06 +02003985
3986 MVM_DEBUGFS_ADD_FILE_RS(ss_force, dir, S_IRUSR | S_IWUSR);
3987 return;
3988err:
3989 IWL_ERR((struct iwl_mvm *)mvm, "Can't create debugfs entity\n");
Johannes Berg8ca151b2013-01-24 14:25:36 +01003990}
3991
3992static void rs_remove_debugfs(void *mvm, void *mvm_sta)
3993{
Johannes Berg8ca151b2013-01-24 14:25:36 +01003994}
3995#endif
3996
3997/*
3998 * Initialization of rate scaling information is done by driver after
3999 * the station is added. Since mac80211 calls this function before a
4000 * station is added we ignore it.
4001 */
4002static void rs_rate_init_stub(void *mvm_r,
Simon Wunderlich3de805c2013-07-08 16:55:50 +02004003 struct ieee80211_supported_band *sband,
4004 struct cfg80211_chan_def *chandef,
4005 struct ieee80211_sta *sta, void *mvm_sta)
Johannes Berg8ca151b2013-01-24 14:25:36 +01004006{
4007}
Johannes Berg631ad702014-01-20 23:29:34 +01004008
4009static const struct rate_control_ops rs_mvm_ops = {
Johannes Berg8ca151b2013-01-24 14:25:36 +01004010 .name = RS_NAME,
Eyal Shapira1ebc8f22014-09-14 15:23:23 +03004011 .tx_status = rs_mac80211_tx_status,
Johannes Berg8ca151b2013-01-24 14:25:36 +01004012 .get_rate = rs_get_rate,
4013 .rate_init = rs_rate_init_stub,
4014 .alloc = rs_alloc,
4015 .free = rs_free,
4016 .alloc_sta = rs_alloc_sta,
4017 .free_sta = rs_free_sta,
Eyal Shapirab87c2172013-11-09 23:37:55 +02004018 .rate_update = rs_rate_update,
Johannes Berg8ca151b2013-01-24 14:25:36 +01004019#ifdef CONFIG_MAC80211_DEBUGFS
4020 .add_sta_debugfs = rs_add_debugfs,
4021 .remove_sta_debugfs = rs_remove_debugfs,
4022#endif
4023};
4024
4025int iwl_mvm_rate_control_register(void)
4026{
4027 return ieee80211_rate_control_register(&rs_mvm_ops);
4028}
4029
4030void iwl_mvm_rate_control_unregister(void)
4031{
4032 ieee80211_rate_control_unregister(&rs_mvm_ops);
4033}
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +03004034
4035/**
4036 * iwl_mvm_tx_protection - Gets LQ command, change it to enable/disable
Sara Sharon0d365ae2015-03-31 12:24:05 +03004037 * Tx protection, according to this request and previous requests,
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +03004038 * and send the LQ command.
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +03004039 * @mvmsta: The station
4040 * @enable: Enable Tx protection?
4041 */
Johannes Berge126b5d2013-06-28 13:39:18 +02004042int iwl_mvm_tx_protection(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
4043 bool enable)
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +03004044{
Johannes Berge126b5d2013-06-28 13:39:18 +02004045 struct iwl_lq_cmd *lq = &mvmsta->lq_sta.lq;
4046
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +03004047 lockdep_assert_held(&mvm->mutex);
4048
4049 if (enable) {
4050 if (mvmsta->tx_protection == 0)
Eyal Shapirad307ec82013-11-11 19:56:21 +02004051 lq->flags |= LQ_FLAG_USE_RTS_MSK;
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +03004052 mvmsta->tx_protection++;
4053 } else {
4054 mvmsta->tx_protection--;
4055 if (mvmsta->tx_protection == 0)
Eyal Shapirad307ec82013-11-11 19:56:21 +02004056 lq->flags &= ~LQ_FLAG_USE_RTS_MSK;
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +03004057 }
4058
Eyal Shapira9e680942013-11-09 00:16:16 +02004059 return iwl_mvm_send_lq_cmd(mvm, lq, false);
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +03004060}