blob: ce884847cc8a84c7d04943bf21fed89ad44b9233 [file] [log] [blame]
Johannes Berg8ca151b2013-01-24 14:25:36 +01001/******************************************************************************
2 *
Emmanuel Grumbach51368bf2013-12-30 13:15:54 +02003 * Copyright(c) 2005 - 2014 Intel Corporation. All rights reserved.
Johannes Berg8b4139d2014-07-24 14:05:26 +02004 * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
Johannes Berg8ca151b2013-01-24 14:25:36 +01005 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of version 2 of the GNU General Public License as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
18 *
19 * The full GNU General Public License is included in this distribution in the
20 * file called LICENSE.
21 *
22 * Contact Information:
23 * Intel Linux Wireless <ilw@linux.intel.com>
24 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
25 *
26 *****************************************************************************/
27#include <linux/kernel.h>
Johannes Berg8ca151b2013-01-24 14:25:36 +010028#include <linux/skbuff.h>
29#include <linux/slab.h>
30#include <net/mac80211.h>
31
32#include <linux/netdevice.h>
33#include <linux/etherdevice.h>
34#include <linux/delay.h>
35
36#include <linux/workqueue.h>
37#include "rs.h"
38#include "fw-api.h"
39#include "sta.h"
40#include "iwl-op-mode.h"
41#include "mvm.h"
42
43#define RS_NAME "iwl-mvm-rs"
44
Eyal Shapirae07be6d2013-12-09 13:02:56 +020045#define NUM_TRY_BEFORE_ANT_TOGGLE 1
46#define RS_LEGACY_RETRIES_PER_RATE 1
47#define RS_HT_VHT_RETRIES_PER_RATE 2
48#define RS_HT_VHT_RETRIES_PER_RATE_TW 1
49#define RS_INITIAL_MIMO_NUM_RATES 3
50#define RS_INITIAL_SISO_NUM_RATES 3
51#define RS_INITIAL_LEGACY_NUM_RATES LINK_QUAL_MAX_RETRY_NUM
52#define RS_SECONDARY_LEGACY_NUM_RATES LINK_QUAL_MAX_RETRY_NUM
53#define RS_SECONDARY_SISO_NUM_RATES 3
54#define RS_SECONDARY_SISO_RETRIES 1
Johannes Berg8ca151b2013-01-24 14:25:36 +010055
56#define IWL_RATE_MAX_WINDOW 62 /* # tx in history window */
Eyal Shapirad17334c2013-11-20 02:08:12 +020057#define IWL_RATE_MIN_FAILURE_TH 3 /* min failures to calc tpt */
Johannes Berg8ca151b2013-01-24 14:25:36 +010058#define IWL_RATE_MIN_SUCCESS_TH 8 /* min successes to calc tpt */
59
60/* max allowed rate miss before sync LQ cmd */
61#define IWL_MISSED_RATE_MAX 15
Eyal Shapira32b01722013-11-22 09:41:38 +020062#define RS_STAY_IN_COLUMN_TIMEOUT (5*HZ)
Eyal Shapira87d5e412014-04-06 18:13:22 +030063#define RS_IDLE_TIMEOUT (5*HZ)
Johannes Berg8ca151b2013-01-24 14:25:36 +010064
65static u8 rs_ht_to_legacy[] = {
Eyal Shapirada87d7d2013-11-28 12:27:03 +020066 [IWL_RATE_MCS_0_INDEX] = IWL_RATE_6M_INDEX,
67 [IWL_RATE_MCS_1_INDEX] = IWL_RATE_9M_INDEX,
68 [IWL_RATE_MCS_2_INDEX] = IWL_RATE_12M_INDEX,
69 [IWL_RATE_MCS_3_INDEX] = IWL_RATE_18M_INDEX,
70 [IWL_RATE_MCS_4_INDEX] = IWL_RATE_24M_INDEX,
71 [IWL_RATE_MCS_5_INDEX] = IWL_RATE_36M_INDEX,
72 [IWL_RATE_MCS_6_INDEX] = IWL_RATE_48M_INDEX,
73 [IWL_RATE_MCS_7_INDEX] = IWL_RATE_54M_INDEX,
74 [IWL_RATE_MCS_8_INDEX] = IWL_RATE_54M_INDEX,
75 [IWL_RATE_MCS_9_INDEX] = IWL_RATE_54M_INDEX,
Johannes Berg8ca151b2013-01-24 14:25:36 +010076};
77
78static const u8 ant_toggle_lookup[] = {
Eliad Pelleref4394b2013-07-16 18:07:20 +030079 [ANT_NONE] = ANT_NONE,
80 [ANT_A] = ANT_B,
81 [ANT_B] = ANT_C,
82 [ANT_AB] = ANT_BC,
83 [ANT_C] = ANT_A,
84 [ANT_AC] = ANT_AB,
85 [ANT_BC] = ANT_AC,
86 [ANT_ABC] = ANT_ABC,
Johannes Berg8ca151b2013-01-24 14:25:36 +010087};
88
Eyal Shapirad310e402013-08-11 18:43:47 +030089#define IWL_DECLARE_RATE_INFO(r, s, rp, rn) \
90 [IWL_RATE_##r##M_INDEX] = { IWL_RATE_##r##M_PLCP, \
91 IWL_RATE_HT_SISO_MCS_##s##_PLCP, \
92 IWL_RATE_HT_MIMO2_MCS_##s##_PLCP, \
93 IWL_RATE_VHT_SISO_MCS_##s##_PLCP, \
94 IWL_RATE_VHT_MIMO2_MCS_##s##_PLCP,\
95 IWL_RATE_##rp##M_INDEX, \
Eyal Shapiraab055ce2013-08-04 13:10:31 +030096 IWL_RATE_##rn##M_INDEX }
Johannes Berg8ca151b2013-01-24 14:25:36 +010097
Eyal Shapirad310e402013-08-11 18:43:47 +030098#define IWL_DECLARE_MCS_RATE(s) \
99 [IWL_RATE_MCS_##s##_INDEX] = { IWL_RATE_INVM_PLCP, \
100 IWL_RATE_HT_SISO_MCS_##s##_PLCP, \
101 IWL_RATE_HT_MIMO2_MCS_##s##_PLCP, \
102 IWL_RATE_VHT_SISO_MCS_##s##_PLCP, \
103 IWL_RATE_VHT_MIMO2_MCS_##s##_PLCP, \
104 IWL_RATE_INVM_INDEX, \
105 IWL_RATE_INVM_INDEX }
106
Johannes Berg8ca151b2013-01-24 14:25:36 +0100107/*
108 * Parameter order:
Eyal Shapiraab055ce2013-08-04 13:10:31 +0300109 * rate, ht rate, prev rate, next rate
Johannes Berg8ca151b2013-01-24 14:25:36 +0100110 *
111 * If there isn't a valid next or previous rate then INV is used which
112 * maps to IWL_RATE_INVALID
113 *
114 */
115static const struct iwl_rs_rate_info iwl_rates[IWL_RATE_COUNT] = {
Eyal Shapiraab055ce2013-08-04 13:10:31 +0300116 IWL_DECLARE_RATE_INFO(1, INV, INV, 2), /* 1mbps */
117 IWL_DECLARE_RATE_INFO(2, INV, 1, 5), /* 2mbps */
118 IWL_DECLARE_RATE_INFO(5, INV, 2, 11), /*5.5mbps */
119 IWL_DECLARE_RATE_INFO(11, INV, 9, 12), /* 11mbps */
Eyal Shapirad310e402013-08-11 18:43:47 +0300120 IWL_DECLARE_RATE_INFO(6, 0, 5, 11), /* 6mbps ; MCS 0 */
121 IWL_DECLARE_RATE_INFO(9, INV, 6, 11), /* 9mbps */
122 IWL_DECLARE_RATE_INFO(12, 1, 11, 18), /* 12mbps ; MCS 1 */
123 IWL_DECLARE_RATE_INFO(18, 2, 12, 24), /* 18mbps ; MCS 2 */
124 IWL_DECLARE_RATE_INFO(24, 3, 18, 36), /* 24mbps ; MCS 3 */
125 IWL_DECLARE_RATE_INFO(36, 4, 24, 48), /* 36mbps ; MCS 4 */
126 IWL_DECLARE_RATE_INFO(48, 5, 36, 54), /* 48mbps ; MCS 5 */
127 IWL_DECLARE_RATE_INFO(54, 6, 48, INV), /* 54mbps ; MCS 6 */
128 IWL_DECLARE_MCS_RATE(7), /* MCS 7 */
129 IWL_DECLARE_MCS_RATE(8), /* MCS 8 */
130 IWL_DECLARE_MCS_RATE(9), /* MCS 9 */
Johannes Berg8ca151b2013-01-24 14:25:36 +0100131};
132
Eyal Shapira4e4b8152013-12-11 00:55:36 +0200133enum rs_action {
134 RS_ACTION_STAY = 0,
135 RS_ACTION_DOWNSCALE = -1,
136 RS_ACTION_UPSCALE = 1,
137};
138
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200139enum rs_column_mode {
140 RS_INVALID = 0,
141 RS_LEGACY,
142 RS_SISO,
143 RS_MIMO2,
144};
145
Eyal Shapirafd7dbee2014-04-06 04:39:23 +0300146#define MAX_NEXT_COLUMNS 7
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200147#define MAX_COLUMN_CHECKS 3
148
149typedef bool (*allow_column_func_t) (struct iwl_mvm *mvm,
150 struct ieee80211_sta *sta,
151 struct iwl_scale_tbl_info *tbl);
152
153struct rs_tx_column {
154 enum rs_column_mode mode;
155 u8 ant;
156 bool sgi;
157 enum rs_column next_columns[MAX_NEXT_COLUMNS];
158 allow_column_func_t checks[MAX_COLUMN_CHECKS];
159};
160
161static bool rs_mimo_allow(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
162 struct iwl_scale_tbl_info *tbl)
163{
164 if (!sta->ht_cap.ht_supported)
165 return false;
166
167 if (sta->smps_mode == IEEE80211_SMPS_STATIC)
168 return false;
169
Johannes Berg4ed735e2014-02-12 21:47:44 +0100170 if (num_of_ant(mvm->fw->valid_tx_ant) < 2)
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200171 return false;
172
173 if (!iwl_mvm_bt_coex_is_mimo_allowed(mvm, sta))
174 return false;
175
176 return true;
177}
178
179static bool rs_siso_allow(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
180 struct iwl_scale_tbl_info *tbl)
181{
182 if (!sta->ht_cap.ht_supported)
183 return false;
184
185 return true;
186}
187
188static bool rs_sgi_allow(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
189 struct iwl_scale_tbl_info *tbl)
190{
191 struct rs_rate *rate = &tbl->rate;
192 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
193 struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
194
195 if (is_ht20(rate) && (ht_cap->cap &
196 IEEE80211_HT_CAP_SGI_20))
197 return true;
198 if (is_ht40(rate) && (ht_cap->cap &
199 IEEE80211_HT_CAP_SGI_40))
200 return true;
201 if (is_ht80(rate) && (vht_cap->cap &
202 IEEE80211_VHT_CAP_SHORT_GI_80))
203 return true;
204
205 return false;
206}
207
208static const struct rs_tx_column rs_tx_columns[] = {
209 [RS_COLUMN_LEGACY_ANT_A] = {
210 .mode = RS_LEGACY,
211 .ant = ANT_A,
212 .next_columns = {
213 RS_COLUMN_LEGACY_ANT_B,
214 RS_COLUMN_SISO_ANT_A,
Eyal Shapira31b20452014-04-08 17:52:27 +0300215 RS_COLUMN_MIMO2,
Eyal Shapirad8fff912014-04-06 05:27:36 +0300216 RS_COLUMN_INVALID,
217 RS_COLUMN_INVALID,
Eyal Shapirafd7dbee2014-04-06 04:39:23 +0300218 RS_COLUMN_INVALID,
219 RS_COLUMN_INVALID,
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200220 },
221 },
222 [RS_COLUMN_LEGACY_ANT_B] = {
223 .mode = RS_LEGACY,
224 .ant = ANT_B,
225 .next_columns = {
226 RS_COLUMN_LEGACY_ANT_A,
227 RS_COLUMN_SISO_ANT_B,
Eyal Shapira31b20452014-04-08 17:52:27 +0300228 RS_COLUMN_MIMO2,
Eyal Shapirad8fff912014-04-06 05:27:36 +0300229 RS_COLUMN_INVALID,
230 RS_COLUMN_INVALID,
Eyal Shapirafd7dbee2014-04-06 04:39:23 +0300231 RS_COLUMN_INVALID,
232 RS_COLUMN_INVALID,
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200233 },
234 },
235 [RS_COLUMN_SISO_ANT_A] = {
236 .mode = RS_SISO,
237 .ant = ANT_A,
238 .next_columns = {
239 RS_COLUMN_SISO_ANT_B,
240 RS_COLUMN_MIMO2,
241 RS_COLUMN_SISO_ANT_A_SGI,
Eyal Shapirafd7dbee2014-04-06 04:39:23 +0300242 RS_COLUMN_LEGACY_ANT_A,
243 RS_COLUMN_LEGACY_ANT_B,
Eyal Shapirad8fff912014-04-06 05:27:36 +0300244 RS_COLUMN_INVALID,
Eyal Shapira31b20452014-04-08 17:52:27 +0300245 RS_COLUMN_INVALID,
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200246 },
247 .checks = {
248 rs_siso_allow,
249 },
250 },
251 [RS_COLUMN_SISO_ANT_B] = {
252 .mode = RS_SISO,
253 .ant = ANT_B,
254 .next_columns = {
255 RS_COLUMN_SISO_ANT_A,
256 RS_COLUMN_MIMO2,
257 RS_COLUMN_SISO_ANT_B_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,
265 },
266 },
267 [RS_COLUMN_SISO_ANT_A_SGI] = {
268 .mode = RS_SISO,
269 .ant = ANT_A,
270 .sgi = true,
271 .next_columns = {
272 RS_COLUMN_SISO_ANT_B_SGI,
273 RS_COLUMN_MIMO2_SGI,
274 RS_COLUMN_SISO_ANT_A,
Eyal Shapirafd7dbee2014-04-06 04:39:23 +0300275 RS_COLUMN_LEGACY_ANT_A,
276 RS_COLUMN_LEGACY_ANT_B,
Eyal Shapira31b20452014-04-08 17:52:27 +0300277 RS_COLUMN_INVALID,
278 RS_COLUMN_INVALID,
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200279 },
280 .checks = {
281 rs_siso_allow,
282 rs_sgi_allow,
283 },
284 },
285 [RS_COLUMN_SISO_ANT_B_SGI] = {
286 .mode = RS_SISO,
287 .ant = ANT_B,
288 .sgi = true,
289 .next_columns = {
290 RS_COLUMN_SISO_ANT_A_SGI,
291 RS_COLUMN_MIMO2_SGI,
292 RS_COLUMN_SISO_ANT_B,
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,
300 rs_sgi_allow,
301 },
302 },
303 [RS_COLUMN_MIMO2] = {
304 .mode = RS_MIMO2,
305 .ant = ANT_AB,
306 .next_columns = {
307 RS_COLUMN_SISO_ANT_A,
308 RS_COLUMN_MIMO2_SGI,
Eyal Shapirafd7dbee2014-04-06 04:39:23 +0300309 RS_COLUMN_LEGACY_ANT_A,
310 RS_COLUMN_LEGACY_ANT_B,
Eyal Shapira31b20452014-04-08 17:52:27 +0300311 RS_COLUMN_INVALID,
312 RS_COLUMN_INVALID,
313 RS_COLUMN_INVALID,
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200314 },
315 .checks = {
316 rs_mimo_allow,
317 },
318 },
319 [RS_COLUMN_MIMO2_SGI] = {
320 .mode = RS_MIMO2,
321 .ant = ANT_AB,
322 .sgi = true,
323 .next_columns = {
324 RS_COLUMN_SISO_ANT_A_SGI,
325 RS_COLUMN_MIMO2,
Eyal Shapirafd7dbee2014-04-06 04:39:23 +0300326 RS_COLUMN_LEGACY_ANT_A,
327 RS_COLUMN_LEGACY_ANT_B,
Eyal Shapira31b20452014-04-08 17:52:27 +0300328 RS_COLUMN_INVALID,
329 RS_COLUMN_INVALID,
330 RS_COLUMN_INVALID,
Eyal Shapirab3b06a32013-11-24 21:30:13 +0200331 },
332 .checks = {
333 rs_mimo_allow,
334 rs_sgi_allow,
335 },
336 },
337};
338
Johannes Berg8ca151b2013-01-24 14:25:36 +0100339static inline u8 rs_extract_rate(u32 rate_n_flags)
340{
341 /* also works for HT because bits 7:6 are zero there */
342 return (u8)(rate_n_flags & RATE_LEGACY_RATE_MSK);
343}
344
345static int iwl_hwrate_to_plcp_idx(u32 rate_n_flags)
346{
347 int idx = 0;
348
Johannes Berg8ca151b2013-01-24 14:25:36 +0100349 if (rate_n_flags & RATE_MCS_HT_MSK) {
Eyal Shapirad310e402013-08-11 18:43:47 +0300350 idx = rate_n_flags & RATE_HT_MCS_RATE_CODE_MSK;
351 idx += IWL_RATE_MCS_0_INDEX;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100352
Eyal Shapirad310e402013-08-11 18:43:47 +0300353 /* skip 9M not supported in HT*/
Johannes Berg8ca151b2013-01-24 14:25:36 +0100354 if (idx >= IWL_RATE_9M_INDEX)
355 idx += 1;
Eyal Shapira4e82dd32013-08-04 23:58:37 +0300356 if ((idx >= IWL_FIRST_HT_RATE) && (idx <= IWL_LAST_HT_RATE))
Johannes Berg8ca151b2013-01-24 14:25:36 +0100357 return idx;
Eyal Shapirad310e402013-08-11 18:43:47 +0300358 } else if (rate_n_flags & RATE_MCS_VHT_MSK) {
359 idx = rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK;
360 idx += IWL_RATE_MCS_0_INDEX;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100361
Eyal Shapirad310e402013-08-11 18:43:47 +0300362 /* skip 9M not supported in VHT*/
363 if (idx >= IWL_RATE_9M_INDEX)
364 idx++;
365 if ((idx >= IWL_FIRST_VHT_RATE) && (idx <= IWL_LAST_VHT_RATE))
366 return idx;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100367 } else {
Eyal Shapirad310e402013-08-11 18:43:47 +0300368 /* legacy rate format, search for match in table */
369
370 u8 legacy_rate = rs_extract_rate(rate_n_flags);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100371 for (idx = 0; idx < ARRAY_SIZE(iwl_rates); idx++)
Eyal Shapirad310e402013-08-11 18:43:47 +0300372 if (iwl_rates[idx].plcp == legacy_rate)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100373 return idx;
374 }
375
Eyal Shapira560843f2014-01-05 21:04:19 +0200376 return IWL_RATE_INVALID;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100377}
378
379static void rs_rate_scale_perform(struct iwl_mvm *mvm,
Eyal Shapira1ebc8f22014-09-14 15:23:23 +0300380 struct ieee80211_sta *sta,
381 struct iwl_lq_sta *lq_sta,
382 int tid);
Eyal Shapira8fc7c582013-12-04 02:15:46 +0200383static void rs_fill_lq_cmd(struct iwl_mvm *mvm,
384 struct ieee80211_sta *sta,
385 struct iwl_lq_sta *lq_sta,
386 const struct rs_rate *initial_rate);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100387static void rs_stay_in_table(struct iwl_lq_sta *lq_sta, bool force_search);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100388
Johannes Berg8ca151b2013-01-24 14:25:36 +0100389/**
390 * The following tables contain the expected throughput metrics for all rates
391 *
392 * 1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
393 *
394 * where invalid entries are zeros.
395 *
396 * CCK rates are only valid in legacy table and will only be used in G
397 * (2.4 GHz) band.
398 */
399
Johannes Berga34529e2014-01-20 22:57:21 +0100400static const u16 expected_tpt_legacy[IWL_RATE_COUNT] = {
Eyal Shapira4e82dd32013-08-04 23:58:37 +0300401 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 0, 0, 0
Johannes Berg8ca151b2013-01-24 14:25:36 +0100402};
403
Eyal Shapira4e82dd32013-08-04 23:58:37 +0300404/* Expected TpT tables. 4 indexes:
405 * 0 - NGI, 1 - SGI, 2 - AGG+NGI, 3 - AGG+SGI
406 */
Johannes Berga34529e2014-01-20 22:57:21 +0100407static const u16 expected_tpt_siso_20MHz[4][IWL_RATE_COUNT] = {
Eyal Shapira4e82dd32013-08-04 23:58:37 +0300408 {0, 0, 0, 0, 42, 0, 76, 102, 124, 159, 183, 193, 202, 216, 0},
409 {0, 0, 0, 0, 46, 0, 82, 110, 132, 168, 192, 202, 210, 225, 0},
410 {0, 0, 0, 0, 49, 0, 97, 145, 192, 285, 375, 420, 464, 551, 0},
411 {0, 0, 0, 0, 54, 0, 108, 160, 213, 315, 415, 465, 513, 608, 0},
Johannes Berg8ca151b2013-01-24 14:25:36 +0100412};
413
Johannes Berga34529e2014-01-20 22:57:21 +0100414static const u16 expected_tpt_siso_40MHz[4][IWL_RATE_COUNT] = {
Eyal Shapira4e82dd32013-08-04 23:58:37 +0300415 {0, 0, 0, 0, 77, 0, 127, 160, 184, 220, 242, 250, 257, 269, 275},
416 {0, 0, 0, 0, 83, 0, 135, 169, 193, 229, 250, 257, 264, 275, 280},
417 {0, 0, 0, 0, 101, 0, 199, 295, 389, 570, 744, 828, 911, 1070, 1173},
418 {0, 0, 0, 0, 112, 0, 220, 326, 429, 629, 819, 912, 1000, 1173, 1284},
Johannes Berg8ca151b2013-01-24 14:25:36 +0100419};
420
Johannes Berga34529e2014-01-20 22:57:21 +0100421static const u16 expected_tpt_siso_80MHz[4][IWL_RATE_COUNT] = {
Eyal Shapirad310e402013-08-11 18:43:47 +0300422 {0, 0, 0, 0, 130, 0, 191, 223, 244, 273, 288, 294, 298, 305, 308},
423 {0, 0, 0, 0, 138, 0, 200, 231, 251, 279, 293, 298, 302, 308, 312},
424 {0, 0, 0, 0, 217, 0, 429, 634, 834, 1220, 1585, 1760, 1931, 2258, 2466},
425 {0, 0, 0, 0, 241, 0, 475, 701, 921, 1343, 1741, 1931, 2117, 2468, 2691},
426};
427
Johannes Berga34529e2014-01-20 22:57:21 +0100428static const u16 expected_tpt_mimo2_20MHz[4][IWL_RATE_COUNT] = {
Eyal Shapira4e82dd32013-08-04 23:58:37 +0300429 {0, 0, 0, 0, 74, 0, 123, 155, 179, 213, 235, 243, 250, 261, 0},
430 {0, 0, 0, 0, 81, 0, 131, 164, 187, 221, 242, 250, 256, 267, 0},
431 {0, 0, 0, 0, 98, 0, 193, 286, 375, 550, 718, 799, 878, 1032, 0},
432 {0, 0, 0, 0, 109, 0, 214, 316, 414, 607, 790, 879, 965, 1132, 0},
Johannes Berg8ca151b2013-01-24 14:25:36 +0100433};
434
Johannes Berga34529e2014-01-20 22:57:21 +0100435static const u16 expected_tpt_mimo2_40MHz[4][IWL_RATE_COUNT] = {
Eyal Shapira4e82dd32013-08-04 23:58:37 +0300436 {0, 0, 0, 0, 123, 0, 182, 214, 235, 264, 279, 285, 289, 296, 300},
437 {0, 0, 0, 0, 131, 0, 191, 222, 242, 270, 284, 289, 293, 300, 303},
438 {0, 0, 0, 0, 200, 0, 390, 571, 741, 1067, 1365, 1505, 1640, 1894, 2053},
439 {0, 0, 0, 0, 221, 0, 430, 630, 816, 1169, 1490, 1641, 1784, 2053, 2221},
Johannes Berg8ca151b2013-01-24 14:25:36 +0100440};
441
Johannes Berga34529e2014-01-20 22:57:21 +0100442static const u16 expected_tpt_mimo2_80MHz[4][IWL_RATE_COUNT] = {
Eyal Shapirad310e402013-08-11 18:43:47 +0300443 {0, 0, 0, 0, 182, 0, 240, 264, 278, 299, 308, 311, 313, 317, 319},
444 {0, 0, 0, 0, 190, 0, 247, 269, 282, 302, 310, 313, 315, 319, 320},
445 {0, 0, 0, 0, 428, 0, 833, 1215, 1577, 2254, 2863, 3147, 3418, 3913, 4219},
446 {0, 0, 0, 0, 474, 0, 920, 1338, 1732, 2464, 3116, 3418, 3705, 4225, 4545},
447};
448
Johannes Berg8ca151b2013-01-24 14:25:36 +0100449/* mbps, mcs */
450static const struct iwl_rate_mcs_info iwl_rate_mcs[IWL_RATE_COUNT] = {
451 { "1", "BPSK DSSS"},
452 { "2", "QPSK DSSS"},
453 {"5.5", "BPSK CCK"},
454 { "11", "QPSK CCK"},
455 { "6", "BPSK 1/2"},
456 { "9", "BPSK 1/2"},
457 { "12", "QPSK 1/2"},
458 { "18", "QPSK 3/4"},
459 { "24", "16QAM 1/2"},
460 { "36", "16QAM 3/4"},
461 { "48", "64QAM 2/3"},
462 { "54", "64QAM 3/4"},
463 { "60", "64QAM 5/6"},
464};
465
466#define MCS_INDEX_PER_STREAM (8)
467
Emmanuel Grumbach9d108492013-12-03 11:50:30 +0200468static const char *rs_pretty_ant(u8 ant)
469{
470 static const char * const ant_name[] = {
471 [ANT_NONE] = "None",
472 [ANT_A] = "A",
473 [ANT_B] = "B",
474 [ANT_AB] = "AB",
475 [ANT_C] = "C",
476 [ANT_AC] = "AC",
477 [ANT_BC] = "BC",
478 [ANT_ABC] = "ABC",
479 };
480
481 if (ant > ANT_ABC)
482 return "UNKNOWN";
483
484 return ant_name[ant];
485}
486
487static const char *rs_pretty_lq_type(enum iwl_table_type type)
488{
489 static const char * const lq_types[] = {
490 [LQ_NONE] = "NONE",
491 [LQ_LEGACY_A] = "LEGACY_A",
492 [LQ_LEGACY_G] = "LEGACY_G",
493 [LQ_HT_SISO] = "HT SISO",
494 [LQ_HT_MIMO2] = "HT MIMO",
495 [LQ_VHT_SISO] = "VHT SISO",
496 [LQ_VHT_MIMO2] = "VHT MIMO",
497 };
498
499 if (type < LQ_NONE || type >= LQ_MAX)
500 return "UNKNOWN";
501
502 return lq_types[type];
503}
504
Eyal Shapira5aa33552013-11-23 01:06:36 +0200505static inline void rs_dump_rate(struct iwl_mvm *mvm, const struct rs_rate *rate,
506 const char *prefix)
507{
Eyal Shapirad8bafea2014-10-23 13:58:38 +0300508 IWL_DEBUG_RATE(mvm,
509 "%s: (%s: %d) ANT: %s BW: %d SGI: %d LDPC: %d STBC %d\n",
Eyal Shapira5aa33552013-11-23 01:06:36 +0200510 prefix, rs_pretty_lq_type(rate->type),
511 rate->index, rs_pretty_ant(rate->ant),
Eyal Shapirad8bafea2014-10-23 13:58:38 +0300512 rate->bw, rate->sgi, rate->ldpc, rate->stbc);
Eyal Shapira5aa33552013-11-23 01:06:36 +0200513}
514
Johannes Berg8ca151b2013-01-24 14:25:36 +0100515static void rs_rate_scale_clear_window(struct iwl_rate_scale_data *window)
516{
517 window->data = 0;
518 window->success_counter = 0;
519 window->success_ratio = IWL_INVALID_VALUE;
520 window->counter = 0;
521 window->average_tpt = IWL_INVALID_VALUE;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100522}
523
Eyal Shapirab804eeb2014-04-06 04:27:06 +0300524static void rs_rate_scale_clear_tbl_windows(struct iwl_mvm *mvm,
525 struct iwl_scale_tbl_info *tbl)
Eliad Peller3ca71f62014-03-10 16:33:43 +0200526{
527 int i;
528
Eyal Shapirab804eeb2014-04-06 04:27:06 +0300529 IWL_DEBUG_RATE(mvm, "Clearing up window stats\n");
Eliad Peller3ca71f62014-03-10 16:33:43 +0200530 for (i = 0; i < IWL_RATE_COUNT; i++)
531 rs_rate_scale_clear_window(&tbl->win[i]);
Eliad Peller2fd647f2014-03-13 17:21:36 +0200532
533 for (i = 0; i < ARRAY_SIZE(tbl->tpc_win); i++)
534 rs_rate_scale_clear_window(&tbl->tpc_win[i]);
Eliad Peller3ca71f62014-03-10 16:33:43 +0200535}
536
Johannes Berg8ca151b2013-01-24 14:25:36 +0100537static inline u8 rs_is_valid_ant(u8 valid_antenna, u8 ant_type)
538{
539 return (ant_type & valid_antenna) == ant_type;
540}
541
Johannes Berg8ca151b2013-01-24 14:25:36 +0100542static int rs_tl_turn_on_agg_for_tid(struct iwl_mvm *mvm,
543 struct iwl_lq_sta *lq_data, u8 tid,
544 struct ieee80211_sta *sta)
545{
546 int ret = -EAGAIN;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100547
Emmanuel Grumbach44cc4292013-06-18 06:33:49 +0300548 IWL_DEBUG_HT(mvm, "Starting Tx agg: STA: %pM tid: %d\n",
549 sta->addr, tid);
550 ret = ieee80211_start_tx_ba_session(sta, tid, 5000);
551 if (ret == -EAGAIN) {
552 /*
553 * driver and mac80211 is out of sync
554 * this might be cause by reloading firmware
555 * stop the tx ba session here
556 */
557 IWL_ERR(mvm, "Fail start Tx agg on tid: %d\n",
558 tid);
559 ieee80211_stop_tx_ba_session(sta, tid);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100560 }
561 return ret;
562}
563
564static void rs_tl_turn_on_agg(struct iwl_mvm *mvm, u8 tid,
565 struct iwl_lq_sta *lq_data,
566 struct ieee80211_sta *sta)
567{
568 if (tid < IWL_MAX_TID_COUNT)
569 rs_tl_turn_on_agg_for_tid(mvm, lq_data, tid, sta);
570 else
571 IWL_ERR(mvm, "tid exceeds max TID count: %d/%d\n",
572 tid, IWL_MAX_TID_COUNT);
573}
574
575static inline int get_num_of_ant_from_rate(u32 rate_n_flags)
576{
577 return !!(rate_n_flags & RATE_MCS_ANT_A_MSK) +
578 !!(rate_n_flags & RATE_MCS_ANT_B_MSK) +
579 !!(rate_n_flags & RATE_MCS_ANT_C_MSK);
580}
581
582/*
583 * Static function to get the expected throughput from an iwl_scale_tbl_info
584 * that wraps a NULL pointer check
585 */
586static s32 get_expected_tpt(struct iwl_scale_tbl_info *tbl, int rs_index)
587{
588 if (tbl->expected_tpt)
589 return tbl->expected_tpt[rs_index];
590 return 0;
591}
592
593/**
594 * rs_collect_tx_data - Update the success/failure sliding window
595 *
596 * We keep a sliding window of the last 62 packets transmitted
597 * at this rate. window->data contains the bitmask of successful
598 * packets.
599 */
Eliad Peller0bd3c5a2014-03-10 16:07:04 +0200600static int _rs_collect_tx_data(struct iwl_scale_tbl_info *tbl,
601 int scale_index, int attempts, int successes,
602 struct iwl_rate_scale_data *window)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100603{
Johannes Berg8ca151b2013-01-24 14:25:36 +0100604 static const u64 mask = (((u64)1) << (IWL_RATE_MAX_WINDOW - 1));
605 s32 fail_count, tpt;
606
Johannes Berg8ca151b2013-01-24 14:25:36 +0100607 /* Get expected throughput */
608 tpt = get_expected_tpt(tbl, scale_index);
609
610 /*
611 * Keep track of only the latest 62 tx frame attempts in this rate's
612 * history window; anything older isn't really relevant any more.
613 * If we have filled up the sliding window, drop the oldest attempt;
614 * if the oldest attempt (highest bit in bitmap) shows "success",
615 * subtract "1" from the success counter (this is the main reason
616 * we keep these bitmaps!).
617 */
618 while (attempts > 0) {
619 if (window->counter >= IWL_RATE_MAX_WINDOW) {
620 /* remove earliest */
621 window->counter = IWL_RATE_MAX_WINDOW - 1;
622
623 if (window->data & mask) {
624 window->data &= ~mask;
625 window->success_counter--;
626 }
627 }
628
629 /* Increment frames-attempted counter */
630 window->counter++;
631
632 /* Shift bitmap by one frame to throw away oldest history */
633 window->data <<= 1;
634
635 /* Mark the most recent #successes attempts as successful */
636 if (successes > 0) {
637 window->success_counter++;
638 window->data |= 0x1;
639 successes--;
640 }
641
642 attempts--;
643 }
644
645 /* Calculate current success ratio, avoid divide-by-0! */
646 if (window->counter > 0)
647 window->success_ratio = 128 * (100 * window->success_counter)
648 / window->counter;
649 else
650 window->success_ratio = IWL_INVALID_VALUE;
651
652 fail_count = window->counter - window->success_counter;
653
654 /* Calculate average throughput, if we have enough history. */
655 if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
656 (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
657 window->average_tpt = (window->success_ratio * tpt + 64) / 128;
658 else
659 window->average_tpt = IWL_INVALID_VALUE;
660
Johannes Berg8ca151b2013-01-24 14:25:36 +0100661 return 0;
662}
663
Eyal Shapira757cf23b2014-03-24 02:12:00 +0200664static int rs_collect_tx_data(struct iwl_lq_sta *lq_sta,
665 struct iwl_scale_tbl_info *tbl,
Eliad Peller2fd647f2014-03-13 17:21:36 +0200666 int scale_index, int attempts, int successes,
667 u8 reduced_txp)
Eliad Peller0bd3c5a2014-03-10 16:07:04 +0200668{
669 struct iwl_rate_scale_data *window = NULL;
Eliad Peller2fd647f2014-03-13 17:21:36 +0200670 int ret;
Eliad Peller0bd3c5a2014-03-10 16:07:04 +0200671
672 if (scale_index < 0 || scale_index >= IWL_RATE_COUNT)
673 return -EINVAL;
674
Eyal Shapira757cf23b2014-03-24 02:12:00 +0200675 if (tbl->column != RS_COLUMN_INVALID) {
Eyal Shapiraf58220f2014-08-16 01:30:30 +0300676 struct lq_sta_pers *pers = &lq_sta->pers;
677
678 pers->tx_stats[tbl->column][scale_index].total += attempts;
679 pers->tx_stats[tbl->column][scale_index].success += successes;
Eyal Shapira757cf23b2014-03-24 02:12:00 +0200680 }
681
Eliad Peller0bd3c5a2014-03-10 16:07:04 +0200682 /* Select window for current tx bit rate */
683 window = &(tbl->win[scale_index]);
684
Eliad Peller2fd647f2014-03-13 17:21:36 +0200685 ret = _rs_collect_tx_data(tbl, scale_index, attempts, successes,
686 window);
687 if (ret)
688 return ret;
689
690 if (WARN_ON_ONCE(reduced_txp > TPC_MAX_REDUCTION))
691 return -EINVAL;
692
693 window = &tbl->tpc_win[reduced_txp];
Eliad Peller0bd3c5a2014-03-10 16:07:04 +0200694 return _rs_collect_tx_data(tbl, scale_index, attempts, successes,
695 window);
696}
697
Eyal Shapira5aa33552013-11-23 01:06:36 +0200698/* Convert rs_rate object into ucode rate bitmask */
699static u32 ucode_rate_from_rs_rate(struct iwl_mvm *mvm,
Eyal Shapira8fc7c582013-12-04 02:15:46 +0200700 struct rs_rate *rate)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100701{
Eyal Shapira5aa33552013-11-23 01:06:36 +0200702 u32 ucode_rate = 0;
703 int index = rate->index;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100704
Eyal Shapira5aa33552013-11-23 01:06:36 +0200705 ucode_rate |= ((rate->ant << RATE_MCS_ANT_POS) &
Eyal Shapirad310e402013-08-11 18:43:47 +0300706 RATE_MCS_ANT_ABC_MSK);
707
Eyal Shapira5aa33552013-11-23 01:06:36 +0200708 if (is_legacy(rate)) {
709 ucode_rate |= iwl_rates[index].plcp;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100710 if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
Eyal Shapira5aa33552013-11-23 01:06:36 +0200711 ucode_rate |= RATE_MCS_CCK_MSK;
712 return ucode_rate;
Eyal Shapirad310e402013-08-11 18:43:47 +0300713 }
714
Eyal Shapira5aa33552013-11-23 01:06:36 +0200715 if (is_ht(rate)) {
Eyal Shapirad310e402013-08-11 18:43:47 +0300716 if (index < IWL_FIRST_HT_RATE || index > IWL_LAST_HT_RATE) {
Johannes Berg8ca151b2013-01-24 14:25:36 +0100717 IWL_ERR(mvm, "Invalid HT rate index %d\n", index);
Eyal Shapira4e82dd32013-08-04 23:58:37 +0300718 index = IWL_LAST_HT_RATE;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100719 }
Eyal Shapira5aa33552013-11-23 01:06:36 +0200720 ucode_rate |= RATE_MCS_HT_MSK;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100721
Eyal Shapira5aa33552013-11-23 01:06:36 +0200722 if (is_ht_siso(rate))
723 ucode_rate |= iwl_rates[index].plcp_ht_siso;
724 else if (is_ht_mimo2(rate))
725 ucode_rate |= iwl_rates[index].plcp_ht_mimo2;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100726 else
Eyal Shapirad972ab32013-07-28 23:02:45 +0000727 WARN_ON_ONCE(1);
Eyal Shapira5aa33552013-11-23 01:06:36 +0200728 } else if (is_vht(rate)) {
Eyal Shapirad310e402013-08-11 18:43:47 +0300729 if (index < IWL_FIRST_VHT_RATE || index > IWL_LAST_VHT_RATE) {
730 IWL_ERR(mvm, "Invalid VHT rate index %d\n", index);
731 index = IWL_LAST_VHT_RATE;
732 }
Eyal Shapira5aa33552013-11-23 01:06:36 +0200733 ucode_rate |= RATE_MCS_VHT_MSK;
734 if (is_vht_siso(rate))
735 ucode_rate |= iwl_rates[index].plcp_vht_siso;
736 else if (is_vht_mimo2(rate))
737 ucode_rate |= iwl_rates[index].plcp_vht_mimo2;
Eyal Shapirad310e402013-08-11 18:43:47 +0300738 else
739 WARN_ON_ONCE(1);
740
Johannes Berg8ca151b2013-01-24 14:25:36 +0100741 } else {
Eyal Shapira5aa33552013-11-23 01:06:36 +0200742 IWL_ERR(mvm, "Invalid rate->type %d\n", rate->type);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100743 }
744
Eyal Shapirad8bafea2014-10-23 13:58:38 +0300745 if (is_siso(rate) && rate->stbc) {
746 /* To enable STBC we need to set both a flag and ANT_AB */
747 ucode_rate |= RATE_MCS_ANT_AB_MSK;
748 ucode_rate |= RATE_MCS_VHT_STBC_MSK;
749 }
750
Eyal Shapira5aa33552013-11-23 01:06:36 +0200751 ucode_rate |= rate->bw;
752 if (rate->sgi)
753 ucode_rate |= RATE_MCS_SGI_MSK;
Eyal Shapiraa3576ff2014-08-09 10:57:59 +0300754 if (rate->ldpc)
755 ucode_rate |= RATE_MCS_LDPC_MSK;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100756
Eyal Shapira5aa33552013-11-23 01:06:36 +0200757 return ucode_rate;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100758}
759
Eyal Shapira5aa33552013-11-23 01:06:36 +0200760/* Convert a ucode rate into an rs_rate object */
761static int rs_rate_from_ucode_rate(const u32 ucode_rate,
762 enum ieee80211_band band,
763 struct rs_rate *rate)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100764{
Eyal Shapira5aa33552013-11-23 01:06:36 +0200765 u32 ant_msk = ucode_rate & RATE_MCS_ANT_ABC_MSK;
766 u8 num_of_ant = get_num_of_ant_from_rate(ucode_rate);
Eyal Shapirad310e402013-08-11 18:43:47 +0300767 u8 nss;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100768
Emmanuel Grumbach9e898f12013-12-22 10:55:47 +0200769 memset(rate, 0, sizeof(*rate));
Eyal Shapira5aa33552013-11-23 01:06:36 +0200770 rate->index = iwl_hwrate_to_plcp_idx(ucode_rate);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100771
Eyal Shapira560843f2014-01-05 21:04:19 +0200772 if (rate->index == IWL_RATE_INVALID)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100773 return -EINVAL;
Eyal Shapira5aa33552013-11-23 01:06:36 +0200774
775 rate->ant = (ant_msk >> RATE_MCS_ANT_POS);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100776
Eyal Shapirad310e402013-08-11 18:43:47 +0300777 /* Legacy */
Eyal Shapira5aa33552013-11-23 01:06:36 +0200778 if (!(ucode_rate & RATE_MCS_HT_MSK) &&
779 !(ucode_rate & RATE_MCS_VHT_MSK)) {
Johannes Berg8ca151b2013-01-24 14:25:36 +0100780 if (num_of_ant == 1) {
781 if (band == IEEE80211_BAND_5GHZ)
Eyal Shapira5aa33552013-11-23 01:06:36 +0200782 rate->type = LQ_LEGACY_A;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100783 else
Eyal Shapira5aa33552013-11-23 01:06:36 +0200784 rate->type = LQ_LEGACY_G;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100785 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100786
Eyal Shapirad310e402013-08-11 18:43:47 +0300787 return 0;
788 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100789
Eyal Shapirad310e402013-08-11 18:43:47 +0300790 /* HT or VHT */
Eyal Shapira5aa33552013-11-23 01:06:36 +0200791 if (ucode_rate & RATE_MCS_SGI_MSK)
792 rate->sgi = true;
Eyal Shapiraa3576ff2014-08-09 10:57:59 +0300793 if (ucode_rate & RATE_MCS_LDPC_MSK)
794 rate->ldpc = true;
Eyal Shapirad8bafea2014-10-23 13:58:38 +0300795 if (ucode_rate & RATE_MCS_VHT_STBC_MSK)
796 rate->stbc = true;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100797
Eyal Shapira5aa33552013-11-23 01:06:36 +0200798 rate->bw = ucode_rate & RATE_MCS_CHAN_WIDTH_MSK;
Eyal Shapirad310e402013-08-11 18:43:47 +0300799
Eyal Shapira5aa33552013-11-23 01:06:36 +0200800 if (ucode_rate & RATE_MCS_HT_MSK) {
801 nss = ((ucode_rate & RATE_HT_MCS_NSS_MSK) >>
Eyal Shapirad310e402013-08-11 18:43:47 +0300802 RATE_HT_MCS_NSS_POS) + 1;
803
804 if (nss == 1) {
Eyal Shapira5aa33552013-11-23 01:06:36 +0200805 rate->type = LQ_HT_SISO;
Eyal Shapirad8bafea2014-10-23 13:58:38 +0300806 WARN_ON_ONCE(!rate->stbc && num_of_ant != 1);
Eyal Shapirad310e402013-08-11 18:43:47 +0300807 } else if (nss == 2) {
Eyal Shapira5aa33552013-11-23 01:06:36 +0200808 rate->type = LQ_HT_MIMO2;
Eyal Shapirad310e402013-08-11 18:43:47 +0300809 WARN_ON_ONCE(num_of_ant != 2);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100810 } else {
Eyal Shapirad310e402013-08-11 18:43:47 +0300811 WARN_ON_ONCE(1);
812 }
Eyal Shapira5aa33552013-11-23 01:06:36 +0200813 } else if (ucode_rate & RATE_MCS_VHT_MSK) {
814 nss = ((ucode_rate & RATE_VHT_MCS_NSS_MSK) >>
Eyal Shapirad310e402013-08-11 18:43:47 +0300815 RATE_VHT_MCS_NSS_POS) + 1;
816
817 if (nss == 1) {
Eyal Shapira5aa33552013-11-23 01:06:36 +0200818 rate->type = LQ_VHT_SISO;
Eyal Shapirad310e402013-08-11 18:43:47 +0300819 WARN_ON_ONCE(num_of_ant != 1);
820 } else if (nss == 2) {
Eyal Shapira5aa33552013-11-23 01:06:36 +0200821 rate->type = LQ_VHT_MIMO2;
Eyal Shapirad310e402013-08-11 18:43:47 +0300822 WARN_ON_ONCE(num_of_ant != 2);
823 } else {
824 WARN_ON_ONCE(1);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100825 }
826 }
Eyal Shapirad310e402013-08-11 18:43:47 +0300827
Eyal Shapira5aa33552013-11-23 01:06:36 +0200828 WARN_ON_ONCE(rate->bw == RATE_MCS_CHAN_WIDTH_160);
829 WARN_ON_ONCE(rate->bw == RATE_MCS_CHAN_WIDTH_80 &&
830 !is_vht(rate));
Eyal Shapirad310e402013-08-11 18:43:47 +0300831
Johannes Berg8ca151b2013-01-24 14:25:36 +0100832 return 0;
833}
834
835/* switch to another antenna/antennas and return 1 */
836/* if no other valid antenna found, return 0 */
Eyal Shapira8fc7c582013-12-04 02:15:46 +0200837static int rs_toggle_antenna(u32 valid_ant, struct rs_rate *rate)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100838{
839 u8 new_ant_type;
840
Eyal Shapira5aa33552013-11-23 01:06:36 +0200841 if (!rate->ant || rate->ant > ANT_ABC)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100842 return 0;
843
Eyal Shapira5aa33552013-11-23 01:06:36 +0200844 if (!rs_is_valid_ant(valid_ant, rate->ant))
Johannes Berg8ca151b2013-01-24 14:25:36 +0100845 return 0;
846
Eyal Shapira5aa33552013-11-23 01:06:36 +0200847 new_ant_type = ant_toggle_lookup[rate->ant];
Johannes Berg8ca151b2013-01-24 14:25:36 +0100848
Eyal Shapira5aa33552013-11-23 01:06:36 +0200849 while ((new_ant_type != rate->ant) &&
Johannes Berg8ca151b2013-01-24 14:25:36 +0100850 !rs_is_valid_ant(valid_ant, new_ant_type))
851 new_ant_type = ant_toggle_lookup[new_ant_type];
852
Eyal Shapira5aa33552013-11-23 01:06:36 +0200853 if (new_ant_type == rate->ant)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100854 return 0;
855
Eyal Shapira5aa33552013-11-23 01:06:36 +0200856 rate->ant = new_ant_type;
857
Johannes Berg8ca151b2013-01-24 14:25:36 +0100858 return 1;
859}
860
Johannes Berg8ca151b2013-01-24 14:25:36 +0100861static u16 rs_get_supported_rates(struct iwl_lq_sta *lq_sta,
Eyal Shapira5aa33552013-11-23 01:06:36 +0200862 struct rs_rate *rate)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100863{
Eyal Shapira5aa33552013-11-23 01:06:36 +0200864 if (is_legacy(rate))
Johannes Berg8ca151b2013-01-24 14:25:36 +0100865 return lq_sta->active_legacy_rate;
Eyal Shapira5aa33552013-11-23 01:06:36 +0200866 else if (is_siso(rate))
Eyal Shapirad310e402013-08-11 18:43:47 +0300867 return lq_sta->active_siso_rate;
Eyal Shapira5aa33552013-11-23 01:06:36 +0200868 else if (is_mimo2(rate))
Eyal Shapirad310e402013-08-11 18:43:47 +0300869 return lq_sta->active_mimo2_rate;
870
871 WARN_ON_ONCE(1);
872 return 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100873}
874
875static u16 rs_get_adjacent_rate(struct iwl_mvm *mvm, u8 index, u16 rate_mask,
876 int rate_type)
877{
878 u8 high = IWL_RATE_INVALID;
879 u8 low = IWL_RATE_INVALID;
880
881 /* 802.11A or ht walks to the next literal adjacent rate in
882 * the rate table */
Eyal Shapira5aa33552013-11-23 01:06:36 +0200883 if (is_type_a_band(rate_type) || !is_type_legacy(rate_type)) {
Johannes Berg8ca151b2013-01-24 14:25:36 +0100884 int i;
885 u32 mask;
886
887 /* Find the previous rate that is in the rate mask */
888 i = index - 1;
889 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
890 if (rate_mask & mask) {
891 low = i;
892 break;
893 }
894 }
895
896 /* Find the next rate that is in the rate mask */
897 i = index + 1;
898 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
899 if (rate_mask & mask) {
900 high = i;
901 break;
902 }
903 }
904
905 return (high << 8) | low;
906 }
907
908 low = index;
909 while (low != IWL_RATE_INVALID) {
910 low = iwl_rates[low].prev_rs;
911 if (low == IWL_RATE_INVALID)
912 break;
913 if (rate_mask & (1 << low))
914 break;
915 IWL_DEBUG_RATE(mvm, "Skipping masked lower rate: %d\n", low);
916 }
917
918 high = index;
919 while (high != IWL_RATE_INVALID) {
920 high = iwl_rates[high].next_rs;
921 if (high == IWL_RATE_INVALID)
922 break;
923 if (rate_mask & (1 << high))
924 break;
925 IWL_DEBUG_RATE(mvm, "Skipping masked higher rate: %d\n", high);
926 }
927
928 return (high << 8) | low;
929}
930
Eyal Shapirae07be6d2013-12-09 13:02:56 +0200931static inline bool rs_rate_supported(struct iwl_lq_sta *lq_sta,
932 struct rs_rate *rate)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100933{
Eyal Shapirae07be6d2013-12-09 13:02:56 +0200934 return BIT(rate->index) & rs_get_supported_rates(lq_sta, rate);
935}
936
937/* Get the next supported lower rate in the current column.
938 * Return true if bottom rate in the current column was reached
939 */
940static bool rs_get_lower_rate_in_column(struct iwl_lq_sta *lq_sta,
941 struct rs_rate *rate)
942{
943 u8 low;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100944 u16 high_low;
Eyal Shapirae07be6d2013-12-09 13:02:56 +0200945 u16 rate_mask;
Eliad Pellerae969af2014-06-11 15:51:33 +0300946 struct iwl_mvm *mvm = lq_sta->pers.drv;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100947
Eyal Shapirae07be6d2013-12-09 13:02:56 +0200948 rate_mask = rs_get_supported_rates(lq_sta, rate);
949 high_low = rs_get_adjacent_rate(mvm, rate->index, rate_mask,
950 rate->type);
951 low = high_low & 0xff;
952
953 /* Bottom rate of column reached */
954 if (low == IWL_RATE_INVALID)
955 return true;
956
957 rate->index = low;
958 return false;
959}
960
961/* Get the next rate to use following a column downgrade */
962static void rs_get_lower_rate_down_column(struct iwl_lq_sta *lq_sta,
963 struct rs_rate *rate)
964{
Eliad Pellerae969af2014-06-11 15:51:33 +0300965 struct iwl_mvm *mvm = lq_sta->pers.drv;
Eyal Shapirae07be6d2013-12-09 13:02:56 +0200966
967 if (is_legacy(rate)) {
968 /* No column to downgrade from Legacy */
969 return;
970 } else if (is_siso(rate)) {
971 /* Downgrade to Legacy if we were in SISO */
Johannes Berg8ca151b2013-01-24 14:25:36 +0100972 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Eyal Shapira5aa33552013-11-23 01:06:36 +0200973 rate->type = LQ_LEGACY_A;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100974 else
Eyal Shapira5aa33552013-11-23 01:06:36 +0200975 rate->type = LQ_LEGACY_G;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100976
Eyal Shapiraa56db7d2013-11-28 12:35:42 +0200977 rate->bw = RATE_MCS_CHAN_WIDTH_20;
Eyal Shapirae07be6d2013-12-09 13:02:56 +0200978
Emmanuel Grumbachb8000402014-02-03 22:17:19 +0200979 WARN_ON_ONCE(rate->index < IWL_RATE_MCS_0_INDEX ||
Eyal Shapirae07be6d2013-12-09 13:02:56 +0200980 rate->index > IWL_RATE_MCS_9_INDEX);
981
982 rate->index = rs_ht_to_legacy[rate->index];
Eyal Shapiraa3576ff2014-08-09 10:57:59 +0300983 rate->ldpc = false;
Eyal Shapirae07be6d2013-12-09 13:02:56 +0200984 } else {
985 /* Downgrade to SISO with same MCS if in MIMO */
986 rate->type = is_vht_mimo2(rate) ?
987 LQ_VHT_SISO : LQ_HT_SISO;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100988 }
989
Eyal Shapirae07be6d2013-12-09 13:02:56 +0200990 if (num_of_ant(rate->ant) > 1)
Johannes Berg4ed735e2014-02-12 21:47:44 +0100991 rate->ant = first_antenna(mvm->fw->valid_tx_ant);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100992
Eyal Shapirae07be6d2013-12-09 13:02:56 +0200993 /* Relevant in both switching to SISO or Legacy */
994 rate->sgi = false;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100995
Eyal Shapirae07be6d2013-12-09 13:02:56 +0200996 if (!rs_rate_supported(lq_sta, rate))
997 rs_get_lower_rate_in_column(lq_sta, rate);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100998}
999
Eyal Shapira5aa33552013-11-23 01:06:36 +02001000/* Simple function to compare two rate scale table types */
1001static inline bool rs_rate_match(struct rs_rate *a,
1002 struct rs_rate *b)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001003{
Eyal Shapirad8bafea2014-10-23 13:58:38 +03001004 bool ant_match;
1005
1006 if (a->stbc)
1007 ant_match = (b->ant == ANT_A || b->ant == ANT_B);
1008 else
1009 ant_match = (a->ant == b->ant);
1010
1011 return (a->type == b->type) && (a->bw == b->bw) && (a->sgi == b->sgi)
1012 && ant_match;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001013}
1014
Eyal Shapirad310e402013-08-11 18:43:47 +03001015static u32 rs_ch_width_from_mac_flags(enum mac80211_rate_control_flags flags)
1016{
1017 if (flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
1018 return RATE_MCS_CHAN_WIDTH_40;
1019 else if (flags & IEEE80211_TX_RC_80_MHZ_WIDTH)
1020 return RATE_MCS_CHAN_WIDTH_80;
1021 else if (flags & IEEE80211_TX_RC_160_MHZ_WIDTH)
1022 return RATE_MCS_CHAN_WIDTH_160;
1023
1024 return RATE_MCS_CHAN_WIDTH_20;
1025}
1026
Eyal Shapira1ebc8f22014-09-14 15:23:23 +03001027static u8 rs_get_tid(struct ieee80211_hdr *hdr)
1028{
1029 u8 tid = IWL_MAX_TID_COUNT;
1030
1031 if (ieee80211_is_data_qos(hdr->frame_control)) {
1032 u8 *qc = ieee80211_get_qos_ctl(hdr);
1033 tid = qc[0] & 0xf;
1034 }
1035
1036 if (unlikely(tid > IWL_MAX_TID_COUNT))
1037 tid = IWL_MAX_TID_COUNT;
1038
1039 return tid;
1040}
1041
1042void iwl_mvm_rs_tx_status(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
1043 int tid, struct ieee80211_tx_info *info)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001044{
1045 int legacy_success;
1046 int retries;
Eyal Shapira5aa33552013-11-23 01:06:36 +02001047 int mac_index, i;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001048 struct iwl_lq_cmd *table;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001049 enum mac80211_rate_control_flags mac_flags;
Eyal Shapira5aa33552013-11-23 01:06:36 +02001050 u32 ucode_rate;
1051 struct rs_rate rate;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001052 struct iwl_scale_tbl_info *curr_tbl, *other_tbl, *tmp_tbl;
Eliad Peller2fd647f2014-03-13 17:21:36 +02001053 u8 reduced_txp = (uintptr_t)info->status.status_driver_data[0];
Eyal Shapira1ebc8f22014-09-14 15:23:23 +03001054 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1055 struct iwl_lq_sta *lq_sta = &mvmsta->lq_sta;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001056
Johannes Berg8ca151b2013-01-24 14:25:36 +01001057 /* Treat uninitialized rate scaling data same as non-existing. */
1058 if (!lq_sta) {
1059 IWL_DEBUG_RATE(mvm, "Station rate scaling not created yet.\n");
1060 return;
Eliad Pellerae969af2014-06-11 15:51:33 +03001061 } else if (!lq_sta->pers.drv) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001062 IWL_DEBUG_RATE(mvm, "Rate scaling not initialized yet.\n");
1063 return;
1064 }
1065
Emmanuel Grumbach8e964402014-05-06 20:35:10 +03001066#ifdef CONFIG_MAC80211_DEBUGFS
Eyal Shapira87d5e412014-04-06 18:13:22 +03001067 /* Disable last tx check if we are debugging with fixed rate */
Eliad Pellerae969af2014-06-11 15:51:33 +03001068 if (lq_sta->pers.dbg_fixed_rate) {
Eyal Shapira87d5e412014-04-06 18:13:22 +03001069 IWL_DEBUG_RATE(mvm, "Fixed rate. avoid rate scaling\n");
1070 return;
1071 }
1072#endif
Johannes Berg8ca151b2013-01-24 14:25:36 +01001073 /* This packet was aggregated but doesn't carry status info */
1074 if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
1075 !(info->flags & IEEE80211_TX_STAT_AMPDU))
1076 return;
1077
1078 /*
1079 * Ignore this Tx frame response if its initial rate doesn't match
1080 * that of latest Link Quality command. There may be stragglers
1081 * from a previous Link Quality command, but we're no longer interested
1082 * in those; they're either from the "active" mode while we're trying
1083 * to check "search" mode, or a prior "search" mode after we've moved
1084 * to a new "search" mode (which might become the new "active" mode).
1085 */
1086 table = &lq_sta->lq;
Eyal Shapira5aa33552013-11-23 01:06:36 +02001087 ucode_rate = le32_to_cpu(table->rs_table[0]);
1088 rs_rate_from_ucode_rate(ucode_rate, info->band, &rate);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001089 if (info->band == IEEE80211_BAND_5GHZ)
Eyal Shapira5aa33552013-11-23 01:06:36 +02001090 rate.index -= IWL_FIRST_OFDM_RATE;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001091 mac_flags = info->status.rates[0].flags;
1092 mac_index = info->status.rates[0].idx;
1093 /* For HT packets, map MCS to PLCP */
1094 if (mac_flags & IEEE80211_TX_RC_MCS) {
1095 /* Remove # of streams */
1096 mac_index &= RATE_HT_MCS_RATE_CODE_MSK;
1097 if (mac_index >= (IWL_RATE_9M_INDEX - IWL_FIRST_OFDM_RATE))
1098 mac_index++;
1099 /*
1100 * mac80211 HT index is always zero-indexed; we need to move
1101 * HT OFDM rates after CCK rates in 2.4 GHz band
1102 */
1103 if (info->band == IEEE80211_BAND_2GHZ)
1104 mac_index += IWL_FIRST_OFDM_RATE;
Eyal Shapirad310e402013-08-11 18:43:47 +03001105 } else if (mac_flags & IEEE80211_TX_RC_VHT_MCS) {
1106 mac_index &= RATE_VHT_MCS_RATE_CODE_MSK;
1107 if (mac_index >= (IWL_RATE_9M_INDEX - IWL_FIRST_OFDM_RATE))
1108 mac_index++;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001109 }
Eyal Shapirad310e402013-08-11 18:43:47 +03001110
Eyal Shapira87d5e412014-04-06 18:13:22 +03001111 if (time_after(jiffies,
1112 (unsigned long)(lq_sta->last_tx + RS_IDLE_TIMEOUT))) {
1113 int tid;
1114 IWL_DEBUG_RATE(mvm, "Tx idle for too long. reinit rs\n");
1115 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++)
1116 ieee80211_stop_tx_ba_session(sta, tid);
1117
Eyal Shapira1ebc8f22014-09-14 15:23:23 +03001118 iwl_mvm_rs_rate_init(mvm, sta, info->band, false);
Eyal Shapira87d5e412014-04-06 18:13:22 +03001119 return;
1120 }
1121 lq_sta->last_tx = jiffies;
1122
Johannes Berg8ca151b2013-01-24 14:25:36 +01001123 /* Here we actually compare this rate to the latest LQ command */
1124 if ((mac_index < 0) ||
Eyal Shapira5aa33552013-11-23 01:06:36 +02001125 (rate.sgi != !!(mac_flags & IEEE80211_TX_RC_SHORT_GI)) ||
1126 (rate.bw != rs_ch_width_from_mac_flags(mac_flags)) ||
1127 (rate.ant != info->status.antenna) ||
1128 (!!(ucode_rate & RATE_MCS_HT_MSK) !=
Eyal Shapirad310e402013-08-11 18:43:47 +03001129 !!(mac_flags & IEEE80211_TX_RC_MCS)) ||
Eyal Shapira5aa33552013-11-23 01:06:36 +02001130 (!!(ucode_rate & RATE_MCS_VHT_MSK) !=
Eyal Shapirad310e402013-08-11 18:43:47 +03001131 !!(mac_flags & IEEE80211_TX_RC_VHT_MCS)) ||
Eyal Shapira5aa33552013-11-23 01:06:36 +02001132 (!!(ucode_rate & RATE_HT_MCS_GF_MSK) !=
Eyal Shapirad310e402013-08-11 18:43:47 +03001133 !!(mac_flags & IEEE80211_TX_RC_GREEN_FIELD)) ||
Eyal Shapira5aa33552013-11-23 01:06:36 +02001134 (rate.index != mac_index)) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001135 IWL_DEBUG_RATE(mvm,
1136 "initial rate %d does not match %d (0x%x)\n",
Eyal Shapira5aa33552013-11-23 01:06:36 +02001137 mac_index, rate.index, ucode_rate);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001138 /*
1139 * Since rates mis-match, the last LQ command may have failed.
1140 * After IWL_MISSED_RATE_MAX mis-matches, resync the uCode with
1141 * ... driver.
1142 */
1143 lq_sta->missed_rate_counter++;
1144 if (lq_sta->missed_rate_counter > IWL_MISSED_RATE_MAX) {
1145 lq_sta->missed_rate_counter = 0;
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001146 IWL_DEBUG_RATE(mvm,
1147 "Too many rates mismatch. Send sync LQ. rs_state %d\n",
1148 lq_sta->rs_state);
Eyal Shapira9e680942013-11-09 00:16:16 +02001149 iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, false);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001150 }
1151 /* Regardless, ignore this status info for outdated rate */
1152 return;
1153 } else
1154 /* Rate did match, so reset the missed_rate_counter */
1155 lq_sta->missed_rate_counter = 0;
1156
1157 /* Figure out if rate scale algorithm is in active or search table */
Eyal Shapira5aa33552013-11-23 01:06:36 +02001158 if (rs_rate_match(&rate,
1159 &(lq_sta->lq_info[lq_sta->active_tbl].rate))) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001160 curr_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1161 other_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
Eyal Shapira5aa33552013-11-23 01:06:36 +02001162 } else if (rs_rate_match(&rate,
1163 &lq_sta->lq_info[1 - lq_sta->active_tbl].rate)) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001164 curr_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
1165 other_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1166 } else {
1167 IWL_DEBUG_RATE(mvm,
1168 "Neither active nor search matches tx rate\n");
1169 tmp_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
Eyal Shapira5aa33552013-11-23 01:06:36 +02001170 rs_dump_rate(mvm, &tmp_tbl->rate, "ACTIVE");
Johannes Berg8ca151b2013-01-24 14:25:36 +01001171 tmp_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
Eyal Shapira5aa33552013-11-23 01:06:36 +02001172 rs_dump_rate(mvm, &tmp_tbl->rate, "SEARCH");
1173 rs_dump_rate(mvm, &rate, "ACTUAL");
1174
Johannes Berg8ca151b2013-01-24 14:25:36 +01001175 /*
1176 * no matching table found, let's by-pass the data collection
1177 * and continue to perform rate scale to find the rate table
1178 */
1179 rs_stay_in_table(lq_sta, true);
1180 goto done;
1181 }
1182
1183 /*
1184 * Updating the frame history depends on whether packets were
1185 * aggregated.
1186 *
1187 * For aggregation, all packets were transmitted at the same rate, the
1188 * first index into rate scale table.
1189 */
1190 if (info->flags & IEEE80211_TX_STAT_AMPDU) {
Eyal Shapira5aa33552013-11-23 01:06:36 +02001191 ucode_rate = le32_to_cpu(table->rs_table[0]);
1192 rs_rate_from_ucode_rate(ucode_rate, info->band, &rate);
Eyal Shapira757cf23b2014-03-24 02:12:00 +02001193 rs_collect_tx_data(lq_sta, curr_tbl, rate.index,
Johannes Berg8ca151b2013-01-24 14:25:36 +01001194 info->status.ampdu_len,
Eliad Peller2fd647f2014-03-13 17:21:36 +02001195 info->status.ampdu_ack_len,
1196 reduced_txp);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001197
1198 /* Update success/fail counts if not searching for new mode */
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001199 if (lq_sta->rs_state == RS_STATE_STAY_IN_COLUMN) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001200 lq_sta->total_success += info->status.ampdu_ack_len;
1201 lq_sta->total_failed += (info->status.ampdu_len -
1202 info->status.ampdu_ack_len);
1203 }
1204 } else {
1205 /*
1206 * For legacy, update frame history with for each Tx retry.
1207 */
1208 retries = info->status.rates[0].count - 1;
1209 /* HW doesn't send more than 15 retries */
1210 retries = min(retries, 15);
1211
1212 /* The last transmission may have been successful */
1213 legacy_success = !!(info->flags & IEEE80211_TX_STAT_ACK);
1214 /* Collect data for each rate used during failed TX attempts */
1215 for (i = 0; i <= retries; ++i) {
Eyal Shapira5aa33552013-11-23 01:06:36 +02001216 ucode_rate = le32_to_cpu(table->rs_table[i]);
1217 rs_rate_from_ucode_rate(ucode_rate, info->band, &rate);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001218 /*
1219 * Only collect stats if retried rate is in the same RS
1220 * table as active/search.
1221 */
Eyal Shapira5aa33552013-11-23 01:06:36 +02001222 if (rs_rate_match(&rate, &curr_tbl->rate))
Johannes Berg8ca151b2013-01-24 14:25:36 +01001223 tmp_tbl = curr_tbl;
Eyal Shapira5aa33552013-11-23 01:06:36 +02001224 else if (rs_rate_match(&rate, &other_tbl->rate))
Johannes Berg8ca151b2013-01-24 14:25:36 +01001225 tmp_tbl = other_tbl;
Eyal Shapira4107dbd2013-12-10 12:20:37 +02001226 else
Johannes Berg8ca151b2013-01-24 14:25:36 +01001227 continue;
Eyal Shapira4107dbd2013-12-10 12:20:37 +02001228
Eyal Shapira757cf23b2014-03-24 02:12:00 +02001229 rs_collect_tx_data(lq_sta, tmp_tbl, rate.index, 1,
Eliad Peller2fd647f2014-03-13 17:21:36 +02001230 i < retries ? 0 : legacy_success,
1231 reduced_txp);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001232 }
1233
1234 /* Update success/fail counts if not searching for new mode */
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001235 if (lq_sta->rs_state == RS_STATE_STAY_IN_COLUMN) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001236 lq_sta->total_success += legacy_success;
1237 lq_sta->total_failed += retries + (1 - legacy_success);
1238 }
1239 }
1240 /* The last TX rate is cached in lq_sta; it's set in if/else above */
Eyal Shapira5aa33552013-11-23 01:06:36 +02001241 lq_sta->last_rate_n_flags = ucode_rate;
Eliad Peller2fd647f2014-03-13 17:21:36 +02001242 IWL_DEBUG_RATE(mvm, "reduced txpower: %d\n", reduced_txp);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001243done:
1244 /* See if there's a better rate or modulation mode to try. */
Emmanuel Grumbach8dfc2752014-09-21 13:29:48 +03001245 if (sta->supp_rates[info->band])
Eyal Shapira1ebc8f22014-09-14 15:23:23 +03001246 rs_rate_scale_perform(mvm, sta, lq_sta, tid);
1247}
1248
1249/*
1250 * mac80211 sends us Tx status
1251 */
1252static void rs_mac80211_tx_status(void *mvm_r,
1253 struct ieee80211_supported_band *sband,
1254 struct ieee80211_sta *sta, void *priv_sta,
1255 struct sk_buff *skb)
1256{
1257 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1258 struct iwl_op_mode *op_mode = (struct iwl_op_mode *)mvm_r;
1259 struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1260 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1261
1262 if (!ieee80211_is_data(hdr->frame_control) ||
1263 info->flags & IEEE80211_TX_CTL_NO_ACK)
1264 return;
1265
1266 iwl_mvm_rs_tx_status(mvm, sta, rs_get_tid(hdr), info);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001267}
1268
1269/*
1270 * Begin a period of staying with a selected modulation mode.
1271 * Set "stay_in_tbl" flag to prevent any mode switches.
1272 * Set frame tx success limits according to legacy vs. high-throughput,
1273 * and reset overall (spanning all rates) tx success history statistics.
1274 * These control how long we stay using same modulation mode before
1275 * searching for a new mode.
1276 */
1277static void rs_set_stay_in_table(struct iwl_mvm *mvm, u8 is_legacy,
1278 struct iwl_lq_sta *lq_sta)
1279{
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001280 IWL_DEBUG_RATE(mvm, "Moving to RS_STATE_STAY_IN_COLUMN\n");
1281 lq_sta->rs_state = RS_STATE_STAY_IN_COLUMN;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001282 if (is_legacy) {
1283 lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT;
1284 lq_sta->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
1285 lq_sta->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
1286 } else {
1287 lq_sta->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
1288 lq_sta->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
1289 lq_sta->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
1290 }
1291 lq_sta->table_count = 0;
1292 lq_sta->total_failed = 0;
1293 lq_sta->total_success = 0;
1294 lq_sta->flush_timer = jiffies;
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001295 lq_sta->visited_columns = 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001296}
1297
Eyal Shapira198266a2014-03-31 22:37:39 +03001298static int rs_get_max_allowed_rate(struct iwl_lq_sta *lq_sta,
1299 const struct rs_tx_column *column)
1300{
1301 switch (column->mode) {
1302 case RS_LEGACY:
1303 return lq_sta->max_legacy_rate_idx;
1304 case RS_SISO:
1305 return lq_sta->max_siso_rate_idx;
1306 case RS_MIMO2:
1307 return lq_sta->max_mimo2_rate_idx;
1308 default:
1309 WARN_ON_ONCE(1);
1310 }
1311
1312 return lq_sta->max_legacy_rate_idx;
1313}
1314
Johannes Berga34529e2014-01-20 22:57:21 +01001315static const u16 *rs_get_expected_tpt_table(struct iwl_lq_sta *lq_sta,
Eyal Shapira198266a2014-03-31 22:37:39 +03001316 const struct rs_tx_column *column,
1317 u32 bw)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001318{
1319 /* Used to choose among HT tables */
Johannes Berga34529e2014-01-20 22:57:21 +01001320 const u16 (*ht_tbl_pointer)[IWL_RATE_COUNT];
Johannes Berg8ca151b2013-01-24 14:25:36 +01001321
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001322 if (WARN_ON_ONCE(column->mode != RS_LEGACY &&
1323 column->mode != RS_SISO &&
1324 column->mode != RS_MIMO2))
1325 return expected_tpt_legacy;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001326
1327 /* Legacy rates have only one table */
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001328 if (column->mode == RS_LEGACY)
1329 return expected_tpt_legacy;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001330
Eyal Shapirad310e402013-08-11 18:43:47 +03001331 ht_tbl_pointer = expected_tpt_mimo2_20MHz;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001332 /* Choose among many HT tables depending on number of streams
Eyal Shapirad310e402013-08-11 18:43:47 +03001333 * (SISO/MIMO2), channel width (20/40/80), SGI, and aggregation
Johannes Berg8ca151b2013-01-24 14:25:36 +01001334 * status */
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001335 if (column->mode == RS_SISO) {
1336 switch (bw) {
Eyal Shapirad310e402013-08-11 18:43:47 +03001337 case RATE_MCS_CHAN_WIDTH_20:
1338 ht_tbl_pointer = expected_tpt_siso_20MHz;
1339 break;
1340 case RATE_MCS_CHAN_WIDTH_40:
1341 ht_tbl_pointer = expected_tpt_siso_40MHz;
1342 break;
1343 case RATE_MCS_CHAN_WIDTH_80:
1344 ht_tbl_pointer = expected_tpt_siso_80MHz;
1345 break;
1346 default:
1347 WARN_ON_ONCE(1);
1348 }
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001349 } else if (column->mode == RS_MIMO2) {
1350 switch (bw) {
Eyal Shapirad310e402013-08-11 18:43:47 +03001351 case RATE_MCS_CHAN_WIDTH_20:
1352 ht_tbl_pointer = expected_tpt_mimo2_20MHz;
1353 break;
1354 case RATE_MCS_CHAN_WIDTH_40:
1355 ht_tbl_pointer = expected_tpt_mimo2_40MHz;
1356 break;
1357 case RATE_MCS_CHAN_WIDTH_80:
1358 ht_tbl_pointer = expected_tpt_mimo2_80MHz;
1359 break;
1360 default:
1361 WARN_ON_ONCE(1);
1362 }
1363 } else {
1364 WARN_ON_ONCE(1);
Eyal Shapirad972ab32013-07-28 23:02:45 +00001365 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01001366
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001367 if (!column->sgi && !lq_sta->is_agg) /* Normal */
1368 return ht_tbl_pointer[0];
1369 else if (column->sgi && !lq_sta->is_agg) /* SGI */
1370 return ht_tbl_pointer[1];
1371 else if (!column->sgi && lq_sta->is_agg) /* AGG */
1372 return ht_tbl_pointer[2];
Johannes Berg8ca151b2013-01-24 14:25:36 +01001373 else /* AGG+SGI */
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001374 return ht_tbl_pointer[3];
1375}
1376
1377static void rs_set_expected_tpt_table(struct iwl_lq_sta *lq_sta,
1378 struct iwl_scale_tbl_info *tbl)
1379{
1380 struct rs_rate *rate = &tbl->rate;
1381 const struct rs_tx_column *column = &rs_tx_columns[tbl->column];
1382
1383 tbl->expected_tpt = rs_get_expected_tpt_table(lq_sta, column, rate->bw);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001384}
1385
Johannes Berg8ca151b2013-01-24 14:25:36 +01001386static s32 rs_get_best_rate(struct iwl_mvm *mvm,
1387 struct iwl_lq_sta *lq_sta,
1388 struct iwl_scale_tbl_info *tbl, /* "search" */
Eyal Shapira80763512014-04-09 04:31:44 +03001389 unsigned long rate_mask, s8 index)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001390{
Johannes Berg8ca151b2013-01-24 14:25:36 +01001391 struct iwl_scale_tbl_info *active_tbl =
1392 &(lq_sta->lq_info[lq_sta->active_tbl]);
Eyal Shapira80763512014-04-09 04:31:44 +03001393 s32 success_ratio = active_tbl->win[index].success_ratio;
1394 u16 expected_current_tpt = active_tbl->expected_tpt[index];
Johannes Berga34529e2014-01-20 22:57:21 +01001395 const u16 *tpt_tbl = tbl->expected_tpt;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001396 u16 high_low;
Eyal Shapira80763512014-04-09 04:31:44 +03001397 u32 target_tpt;
1398 int rate_idx;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001399
Eyal Shapira80763512014-04-09 04:31:44 +03001400 if (success_ratio > RS_SR_NO_DECREASE) {
1401 target_tpt = 100 * expected_current_tpt;
1402 IWL_DEBUG_RATE(mvm,
1403 "SR %d high. Find rate exceeding EXPECTED_CURRENT %d\n",
1404 success_ratio, target_tpt);
1405 } else {
1406 target_tpt = lq_sta->last_tpt;
1407 IWL_DEBUG_RATE(mvm,
1408 "SR %d not thag good. Find rate exceeding ACTUAL_TPT %d\n",
1409 success_ratio, target_tpt);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001410 }
1411
Eyal Shapira80763512014-04-09 04:31:44 +03001412 rate_idx = find_first_bit(&rate_mask, BITS_PER_LONG);
1413
1414 while (rate_idx != IWL_RATE_INVALID) {
1415 if (target_tpt < (100 * tpt_tbl[rate_idx]))
1416 break;
1417
1418 high_low = rs_get_adjacent_rate(mvm, rate_idx, rate_mask,
1419 tbl->rate.type);
1420
1421 rate_idx = (high_low >> 8) & 0xff;
1422 }
1423
1424 IWL_DEBUG_RATE(mvm, "Best rate found %d target_tp %d expected_new %d\n",
1425 rate_idx, target_tpt,
1426 rate_idx != IWL_RATE_INVALID ?
1427 100 * tpt_tbl[rate_idx] : IWL_INVALID_VALUE);
1428
1429 return rate_idx;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001430}
1431
Eyal Shapira5aa33552013-11-23 01:06:36 +02001432static u32 rs_bw_from_sta_bw(struct ieee80211_sta *sta)
Eyal Shapirad310e402013-08-11 18:43:47 +03001433{
1434 if (sta->bandwidth >= IEEE80211_STA_RX_BW_80)
Eyal Shapira5aa33552013-11-23 01:06:36 +02001435 return RATE_MCS_CHAN_WIDTH_80;
Eyal Shapirad310e402013-08-11 18:43:47 +03001436 else if (sta->bandwidth >= IEEE80211_STA_RX_BW_40)
Eyal Shapira5aa33552013-11-23 01:06:36 +02001437 return RATE_MCS_CHAN_WIDTH_40;
1438
1439 return RATE_MCS_CHAN_WIDTH_20;
Eyal Shapirad310e402013-08-11 18:43:47 +03001440}
1441
Johannes Berg8ca151b2013-01-24 14:25:36 +01001442/*
Johannes Berg8ca151b2013-01-24 14:25:36 +01001443 * Check whether we should continue using same modulation mode, or
1444 * begin search for a new mode, based on:
1445 * 1) # tx successes or failures while using this mode
1446 * 2) # times calling this function
1447 * 3) elapsed time in this mode (not used, for now)
1448 */
1449static void rs_stay_in_table(struct iwl_lq_sta *lq_sta, bool force_search)
1450{
1451 struct iwl_scale_tbl_info *tbl;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001452 int active_tbl;
1453 int flush_interval_passed = 0;
1454 struct iwl_mvm *mvm;
1455
Eliad Pellerae969af2014-06-11 15:51:33 +03001456 mvm = lq_sta->pers.drv;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001457 active_tbl = lq_sta->active_tbl;
1458
1459 tbl = &(lq_sta->lq_info[active_tbl]);
1460
1461 /* If we've been disallowing search, see if we should now allow it */
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001462 if (lq_sta->rs_state == RS_STATE_STAY_IN_COLUMN) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001463 /* Elapsed time using current modulation mode */
1464 if (lq_sta->flush_timer)
1465 flush_interval_passed =
1466 time_after(jiffies,
1467 (unsigned long)(lq_sta->flush_timer +
Eyal Shapira32b01722013-11-22 09:41:38 +02001468 RS_STAY_IN_COLUMN_TIMEOUT));
Johannes Berg8ca151b2013-01-24 14:25:36 +01001469
1470 /*
1471 * Check if we should allow search for new modulation mode.
1472 * If many frames have failed or succeeded, or we've used
1473 * this same modulation for a long time, allow search, and
1474 * reset history stats that keep track of whether we should
1475 * allow a new search. Also (below) reset all bitmaps and
1476 * stats in active history.
1477 */
1478 if (force_search ||
1479 (lq_sta->total_failed > lq_sta->max_failure_limit) ||
1480 (lq_sta->total_success > lq_sta->max_success_limit) ||
1481 ((!lq_sta->search_better_tbl) &&
1482 (lq_sta->flush_timer) && (flush_interval_passed))) {
1483 IWL_DEBUG_RATE(mvm,
1484 "LQ: stay is expired %d %d %d\n",
1485 lq_sta->total_failed,
1486 lq_sta->total_success,
1487 flush_interval_passed);
1488
1489 /* Allow search for new mode */
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001490 lq_sta->rs_state = RS_STATE_SEARCH_CYCLE_STARTED;
1491 IWL_DEBUG_RATE(mvm,
1492 "Moving to RS_STATE_SEARCH_CYCLE_STARTED\n");
Johannes Berg8ca151b2013-01-24 14:25:36 +01001493 lq_sta->total_failed = 0;
1494 lq_sta->total_success = 0;
1495 lq_sta->flush_timer = 0;
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001496 /* mark the current column as visited */
1497 lq_sta->visited_columns = BIT(tbl->column);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001498 /*
1499 * Else if we've used this modulation mode enough repetitions
1500 * (regardless of elapsed time or success/failure), reset
1501 * history bitmaps and rate-specific stats for all rates in
1502 * active table.
1503 */
1504 } else {
1505 lq_sta->table_count++;
1506 if (lq_sta->table_count >=
1507 lq_sta->table_count_limit) {
1508 lq_sta->table_count = 0;
1509
1510 IWL_DEBUG_RATE(mvm,
1511 "LQ: stay in table clear win\n");
Eyal Shapirab804eeb2014-04-06 04:27:06 +03001512 rs_rate_scale_clear_tbl_windows(mvm, tbl);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001513 }
1514 }
1515
1516 /* If transitioning to allow "search", reset all history
1517 * bitmaps and stats in active table (this will become the new
1518 * "search" table). */
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001519 if (lq_sta->rs_state == RS_STATE_SEARCH_CYCLE_STARTED) {
Eyal Shapirab804eeb2014-04-06 04:27:06 +03001520 rs_rate_scale_clear_tbl_windows(mvm, tbl);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001521 }
1522 }
1523}
1524
1525/*
1526 * setup rate table in uCode
1527 */
1528static void rs_update_rate_tbl(struct iwl_mvm *mvm,
Emmanuel Grumbach9145d152013-07-18 08:45:41 +03001529 struct ieee80211_sta *sta,
Johannes Berg8ca151b2013-01-24 14:25:36 +01001530 struct iwl_lq_sta *lq_sta,
Eyal Shapira5aa33552013-11-23 01:06:36 +02001531 struct rs_rate *rate)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001532{
Eyal Shapira8fc7c582013-12-04 02:15:46 +02001533 rs_fill_lq_cmd(mvm, sta, lq_sta, rate);
Eyal Shapira9e680942013-11-09 00:16:16 +02001534 iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, false);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001535}
1536
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001537static enum rs_column rs_get_next_column(struct iwl_mvm *mvm,
1538 struct iwl_lq_sta *lq_sta,
1539 struct ieee80211_sta *sta,
1540 struct iwl_scale_tbl_info *tbl)
1541{
Eyal Shapira198266a2014-03-31 22:37:39 +03001542 int i, j, max_rate;
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001543 enum rs_column next_col_id;
1544 const struct rs_tx_column *curr_col = &rs_tx_columns[tbl->column];
1545 const struct rs_tx_column *next_col;
1546 allow_column_func_t allow_func;
Johannes Berg4ed735e2014-02-12 21:47:44 +01001547 u8 valid_ants = mvm->fw->valid_tx_ant;
Johannes Berga34529e2014-01-20 22:57:21 +01001548 const u16 *expected_tpt_tbl;
Eyal Shapira198266a2014-03-31 22:37:39 +03001549 u16 tpt, max_expected_tpt;
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001550
1551 for (i = 0; i < MAX_NEXT_COLUMNS; i++) {
1552 next_col_id = curr_col->next_columns[i];
1553
1554 if (next_col_id == RS_COLUMN_INVALID)
1555 continue;
1556
1557 if (lq_sta->visited_columns & BIT(next_col_id)) {
1558 IWL_DEBUG_RATE(mvm, "Skip already visited column %d\n",
1559 next_col_id);
1560 continue;
1561 }
1562
1563 next_col = &rs_tx_columns[next_col_id];
1564
1565 if (!rs_is_valid_ant(valid_ants, next_col->ant)) {
1566 IWL_DEBUG_RATE(mvm,
1567 "Skip column %d as ANT config isn't supported by chip. valid_ants 0x%x column ant 0x%x\n",
1568 next_col_id, valid_ants, next_col->ant);
1569 continue;
1570 }
1571
1572 for (j = 0; j < MAX_COLUMN_CHECKS; j++) {
1573 allow_func = next_col->checks[j];
1574 if (allow_func && !allow_func(mvm, sta, tbl))
1575 break;
1576 }
1577
1578 if (j != MAX_COLUMN_CHECKS) {
1579 IWL_DEBUG_RATE(mvm,
1580 "Skip column %d: not allowed (check %d failed)\n",
1581 next_col_id, j);
1582
1583 continue;
1584 }
1585
1586 tpt = lq_sta->last_tpt / 100;
1587 expected_tpt_tbl = rs_get_expected_tpt_table(lq_sta, next_col,
Eyal Shapira049e0c32014-05-01 01:28:48 +03001588 rs_bw_from_sta_bw(sta));
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001589 if (WARN_ON_ONCE(!expected_tpt_tbl))
1590 continue;
1591
Eyal Shapira198266a2014-03-31 22:37:39 +03001592 max_rate = rs_get_max_allowed_rate(lq_sta, next_col);
1593 if (WARN_ON_ONCE(max_rate == IWL_RATE_INVALID))
1594 continue;
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001595
Eyal Shapira198266a2014-03-31 22:37:39 +03001596 max_expected_tpt = expected_tpt_tbl[max_rate];
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001597 if (tpt >= max_expected_tpt) {
1598 IWL_DEBUG_RATE(mvm,
1599 "Skip column %d: can't beat current TPT. Max expected %d current %d\n",
1600 next_col_id, max_expected_tpt, tpt);
1601 continue;
1602 }
1603
Eyal Shapira198266a2014-03-31 22:37:39 +03001604 IWL_DEBUG_RATE(mvm,
1605 "Found potential column %d. Max expected %d current %d\n",
1606 next_col_id, max_expected_tpt, tpt);
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001607 break;
1608 }
1609
1610 if (i == MAX_NEXT_COLUMNS)
1611 return RS_COLUMN_INVALID;
1612
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001613 return next_col_id;
1614}
1615
1616static int rs_switch_to_column(struct iwl_mvm *mvm,
1617 struct iwl_lq_sta *lq_sta,
1618 struct ieee80211_sta *sta,
1619 enum rs_column col_id)
1620{
1621 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1622 struct iwl_scale_tbl_info *search_tbl =
1623 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1624 struct rs_rate *rate = &search_tbl->rate;
1625 const struct rs_tx_column *column = &rs_tx_columns[col_id];
1626 const struct rs_tx_column *curr_column = &rs_tx_columns[tbl->column];
1627 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1628 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Eyal Shapira80763512014-04-09 04:31:44 +03001629 unsigned long rate_mask = 0;
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001630 u32 rate_idx = 0;
1631
1632 memcpy(search_tbl, tbl, sz);
1633
1634 rate->sgi = column->sgi;
1635 rate->ant = column->ant;
1636
1637 if (column->mode == RS_LEGACY) {
1638 if (lq_sta->band == IEEE80211_BAND_5GHZ)
1639 rate->type = LQ_LEGACY_A;
1640 else
1641 rate->type = LQ_LEGACY_G;
1642
Eyal Shapirad8bafea2014-10-23 13:58:38 +03001643 rate->bw = RATE_MCS_CHAN_WIDTH_20;
1644 rate->ldpc = false;
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001645 rate_mask = lq_sta->active_legacy_rate;
1646 } else if (column->mode == RS_SISO) {
1647 rate->type = lq_sta->is_vht ? LQ_VHT_SISO : LQ_HT_SISO;
1648 rate_mask = lq_sta->active_siso_rate;
1649 } else if (column->mode == RS_MIMO2) {
1650 rate->type = lq_sta->is_vht ? LQ_VHT_MIMO2 : LQ_HT_MIMO2;
1651 rate_mask = lq_sta->active_mimo2_rate;
1652 } else {
1653 WARN_ON_ONCE("Bad column mode");
1654 }
1655
Eyal Shapirad8bafea2014-10-23 13:58:38 +03001656 if (column->mode != RS_LEGACY) {
1657 rate->bw = rs_bw_from_sta_bw(sta);
1658 rate->ldpc = lq_sta->ldpc;
1659 }
1660
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001661 search_tbl->column = col_id;
1662 rs_set_expected_tpt_table(lq_sta, search_tbl);
1663
Eyal Shapira75d3e2d2014-01-07 18:19:35 +02001664 lq_sta->visited_columns |= BIT(col_id);
1665
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001666 /* Get the best matching rate if we're changing modes. e.g.
1667 * SISO->MIMO, LEGACY->SISO, MIMO->SISO
1668 */
1669 if (curr_column->mode != column->mode) {
1670 rate_idx = rs_get_best_rate(mvm, lq_sta, search_tbl,
1671 rate_mask, rate->index);
1672
1673 if ((rate_idx == IWL_RATE_INVALID) ||
1674 !(BIT(rate_idx) & rate_mask)) {
1675 IWL_DEBUG_RATE(mvm,
1676 "can not switch with index %d"
Eyal Shapira80763512014-04-09 04:31:44 +03001677 " rate mask %lx\n",
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001678 rate_idx, rate_mask);
1679
1680 goto err;
1681 }
1682
1683 rate->index = rate_idx;
1684 }
1685
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001686 IWL_DEBUG_RATE(mvm, "Switched to column %d: Index %d\n",
1687 col_id, rate->index);
1688
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001689 return 0;
1690
1691err:
1692 rate->type = LQ_NONE;
1693 return -1;
1694}
1695
Eyal Shapira4e4b8152013-12-11 00:55:36 +02001696static enum rs_action rs_get_rate_action(struct iwl_mvm *mvm,
1697 struct iwl_scale_tbl_info *tbl,
1698 s32 sr, int low, int high,
1699 int current_tpt,
1700 int low_tpt, int high_tpt)
1701{
1702 enum rs_action action = RS_ACTION_STAY;
1703
Eyal Shapira4e4b8152013-12-11 00:55:36 +02001704 if ((sr <= RS_SR_FORCE_DECREASE) || (current_tpt == 0)) {
1705 IWL_DEBUG_RATE(mvm,
Eyal Shapirae53839e2014-04-06 02:42:18 +03001706 "Decrease rate because of low SR\n");
1707 return RS_ACTION_DOWNSCALE;
1708 }
1709
1710 if ((low_tpt == IWL_INVALID_VALUE) &&
1711 (high_tpt == IWL_INVALID_VALUE) &&
1712 (high != IWL_RATE_INVALID)) {
1713 IWL_DEBUG_RATE(mvm,
1714 "No data about high/low rates. Increase rate\n");
1715 return RS_ACTION_UPSCALE;
1716 }
1717
1718 if ((high_tpt == IWL_INVALID_VALUE) &&
1719 (high != IWL_RATE_INVALID) &&
1720 (low_tpt != IWL_INVALID_VALUE) &&
1721 (low_tpt < current_tpt)) {
1722 IWL_DEBUG_RATE(mvm,
1723 "No data about high rate and low rate is worse. Increase rate\n");
1724 return RS_ACTION_UPSCALE;
1725 }
1726
1727 if ((high_tpt != IWL_INVALID_VALUE) &&
1728 (high_tpt > current_tpt)) {
1729 IWL_DEBUG_RATE(mvm,
1730 "Higher rate is better. Increate rate\n");
1731 return RS_ACTION_UPSCALE;
1732 }
1733
1734 if ((low_tpt != IWL_INVALID_VALUE) &&
1735 (high_tpt != IWL_INVALID_VALUE) &&
1736 (low_tpt < current_tpt) &&
1737 (high_tpt < current_tpt)) {
1738 IWL_DEBUG_RATE(mvm,
1739 "Both high and low are worse. Maintain rate\n");
1740 return RS_ACTION_STAY;
1741 }
1742
1743 if ((low_tpt != IWL_INVALID_VALUE) &&
1744 (low_tpt > current_tpt)) {
1745 IWL_DEBUG_RATE(mvm,
1746 "Lower rate is better\n");
Eyal Shapira4e4b8152013-12-11 00:55:36 +02001747 action = RS_ACTION_DOWNSCALE;
Eyal Shapirae53839e2014-04-06 02:42:18 +03001748 goto out;
1749 }
1750
1751 if ((low_tpt == IWL_INVALID_VALUE) &&
1752 (low != IWL_RATE_INVALID)) {
1753 IWL_DEBUG_RATE(mvm,
1754 "No data about lower rate\n");
1755 action = RS_ACTION_DOWNSCALE;
1756 goto out;
1757 }
1758
1759 IWL_DEBUG_RATE(mvm, "Maintain rate\n");
1760
1761out:
1762 if ((action == RS_ACTION_DOWNSCALE) && (low != IWL_RATE_INVALID)) {
1763 if (sr >= RS_SR_NO_DECREASE) {
Eyal Shapira4e4b8152013-12-11 00:55:36 +02001764 IWL_DEBUG_RATE(mvm,
Eyal Shapirae53839e2014-04-06 02:42:18 +03001765 "SR is above NO DECREASE. Avoid downscale\n");
Eyal Shapira4e4b8152013-12-11 00:55:36 +02001766 action = RS_ACTION_STAY;
Eyal Shapirae53839e2014-04-06 02:42:18 +03001767 } else if (current_tpt > (100 * tbl->expected_tpt[low])) {
1768 IWL_DEBUG_RATE(mvm,
1769 "Current TPT is higher than max expected in low rate. Avoid downscale\n");
1770 action = RS_ACTION_STAY;
1771 } else {
1772 IWL_DEBUG_RATE(mvm, "Decrease rate\n");
Eyal Shapira4e4b8152013-12-11 00:55:36 +02001773 }
1774 }
1775
Eyal Shapira4e4b8152013-12-11 00:55:36 +02001776 return action;
1777}
Eyal Shapirab3b06a32013-11-24 21:30:13 +02001778
Eyal Shapirad8bafea2014-10-23 13:58:38 +03001779static bool rs_stbc_allow(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
1780 struct iwl_lq_sta *lq_sta)
1781{
1782 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1783 struct ieee80211_vif *vif = mvmsta->vif;
1784 bool sta_ps_disabled = (vif->type == NL80211_IFTYPE_STATION &&
1785 !vif->bss_conf.ps);
1786
1787 /* Our chip supports Tx STBC and the peer is an HT/VHT STA which
1788 * supports STBC of at least 1*SS
1789 */
1790 if (!lq_sta->stbc)
1791 return false;
1792
1793 if (!mvm->ps_disabled && !sta_ps_disabled)
1794 return false;
1795
1796 if (!iwl_mvm_bt_coex_is_mimo_allowed(mvm, sta))
1797 return false;
1798
1799 return true;
1800}
1801
Eliad Peller2fd647f2014-03-13 17:21:36 +02001802static void rs_get_adjacent_txp(struct iwl_mvm *mvm, int index,
1803 int *weaker, int *stronger)
1804{
1805 *weaker = index + TPC_TX_POWER_STEP;
1806 if (*weaker > TPC_MAX_REDUCTION)
1807 *weaker = TPC_INVALID;
1808
1809 *stronger = index - TPC_TX_POWER_STEP;
1810 if (*stronger < 0)
1811 *stronger = TPC_INVALID;
1812}
1813
Eyal Shapira1e9551d2014-04-21 21:33:17 +03001814static bool rs_tpc_allowed(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1815 struct rs_rate *rate, enum ieee80211_band band)
Eliad Peller2fd647f2014-03-13 17:21:36 +02001816{
1817 int index = rate->index;
Eyal Shapira1e9551d2014-04-21 21:33:17 +03001818 bool cam = (iwlmvm_mod_params.power_scheme == IWL_POWER_SCHEME_CAM);
1819 bool sta_ps_disabled = (vif->type == NL80211_IFTYPE_STATION &&
1820 !vif->bss_conf.ps);
Eliad Peller2fd647f2014-03-13 17:21:36 +02001821
Eyal Shapira1e9551d2014-04-21 21:33:17 +03001822 IWL_DEBUG_RATE(mvm, "cam: %d sta_ps_disabled %d\n",
1823 cam, sta_ps_disabled);
Eliad Peller2fd647f2014-03-13 17:21:36 +02001824 /*
1825 * allow tpc only if power management is enabled, or bt coex
1826 * activity grade allows it and we are on 2.4Ghz.
1827 */
Eyal Shapira1e9551d2014-04-21 21:33:17 +03001828 if ((cam || sta_ps_disabled) &&
Eliad Peller2fd647f2014-03-13 17:21:36 +02001829 !iwl_mvm_bt_coex_is_tpc_allowed(mvm, band))
1830 return false;
1831
1832 IWL_DEBUG_RATE(mvm, "check rate, table type: %d\n", rate->type);
1833 if (is_legacy(rate))
1834 return index == IWL_RATE_54M_INDEX;
1835 if (is_ht(rate))
1836 return index == IWL_RATE_MCS_7_INDEX;
1837 if (is_vht(rate))
1838 return index == IWL_RATE_MCS_7_INDEX ||
1839 index == IWL_RATE_MCS_8_INDEX ||
1840 index == IWL_RATE_MCS_9_INDEX;
1841
1842 WARN_ON_ONCE(1);
1843 return false;
1844}
1845
1846enum tpc_action {
1847 TPC_ACTION_STAY,
1848 TPC_ACTION_DECREASE,
1849 TPC_ACTION_INCREASE,
1850 TPC_ACTION_NO_RESTIRCTION,
1851};
1852
1853static enum tpc_action rs_get_tpc_action(struct iwl_mvm *mvm,
1854 s32 sr, int weak, int strong,
1855 int current_tpt,
1856 int weak_tpt, int strong_tpt)
1857{
1858 /* stay until we have valid tpt */
1859 if (current_tpt == IWL_INVALID_VALUE) {
1860 IWL_DEBUG_RATE(mvm, "no current tpt. stay.\n");
1861 return TPC_ACTION_STAY;
1862 }
1863
1864 /* Too many failures, increase txp */
1865 if (sr <= TPC_SR_FORCE_INCREASE || current_tpt == 0) {
1866 IWL_DEBUG_RATE(mvm, "increase txp because of weak SR\n");
1867 return TPC_ACTION_NO_RESTIRCTION;
1868 }
1869
1870 /* try decreasing first if applicable */
1871 if (weak != TPC_INVALID) {
1872 if (weak_tpt == IWL_INVALID_VALUE &&
1873 (strong_tpt == IWL_INVALID_VALUE ||
1874 current_tpt >= strong_tpt)) {
1875 IWL_DEBUG_RATE(mvm,
1876 "no weak txp measurement. decrease txp\n");
1877 return TPC_ACTION_DECREASE;
1878 }
1879
1880 if (weak_tpt > current_tpt) {
1881 IWL_DEBUG_RATE(mvm,
1882 "lower txp has better tpt. decrease txp\n");
1883 return TPC_ACTION_DECREASE;
1884 }
1885 }
1886
1887 /* next, increase if needed */
1888 if (sr < TPC_SR_NO_INCREASE && strong != TPC_INVALID) {
1889 if (weak_tpt == IWL_INVALID_VALUE &&
1890 strong_tpt != IWL_INVALID_VALUE &&
1891 current_tpt < strong_tpt) {
1892 IWL_DEBUG_RATE(mvm,
1893 "higher txp has better tpt. increase txp\n");
1894 return TPC_ACTION_INCREASE;
1895 }
1896
1897 if (weak_tpt < current_tpt &&
1898 (strong_tpt == IWL_INVALID_VALUE ||
1899 strong_tpt > current_tpt)) {
1900 IWL_DEBUG_RATE(mvm,
1901 "lower txp has worse tpt. increase txp\n");
1902 return TPC_ACTION_INCREASE;
1903 }
1904 }
1905
1906 IWL_DEBUG_RATE(mvm, "no need to increase or decrease txp - stay\n");
1907 return TPC_ACTION_STAY;
1908}
1909
1910static bool rs_tpc_perform(struct iwl_mvm *mvm,
1911 struct ieee80211_sta *sta,
1912 struct iwl_lq_sta *lq_sta,
1913 struct iwl_scale_tbl_info *tbl)
1914{
1915 struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
1916 struct ieee80211_vif *vif = mvm_sta->vif;
1917 struct ieee80211_chanctx_conf *chanctx_conf;
1918 enum ieee80211_band band;
1919 struct iwl_rate_scale_data *window;
1920 struct rs_rate *rate = &tbl->rate;
1921 enum tpc_action action;
1922 s32 sr;
1923 u8 cur = lq_sta->lq.reduced_tpc;
1924 int current_tpt;
1925 int weak, strong;
1926 int weak_tpt = IWL_INVALID_VALUE, strong_tpt = IWL_INVALID_VALUE;
1927
Eliad Peller9a75b3d2014-03-13 17:15:04 +02001928#ifdef CONFIG_MAC80211_DEBUGFS
Eliad Pellerae969af2014-06-11 15:51:33 +03001929 if (lq_sta->pers.dbg_fixed_txp_reduction <= TPC_MAX_REDUCTION) {
Johannes Berg3c6acb62014-05-07 11:47:53 +02001930 IWL_DEBUG_RATE(mvm, "fixed tpc: %d\n",
Eliad Pellerae969af2014-06-11 15:51:33 +03001931 lq_sta->pers.dbg_fixed_txp_reduction);
1932 lq_sta->lq.reduced_tpc = lq_sta->pers.dbg_fixed_txp_reduction;
1933 return cur != lq_sta->pers.dbg_fixed_txp_reduction;
Eliad Peller9a75b3d2014-03-13 17:15:04 +02001934 }
1935#endif
1936
Eliad Peller2fd647f2014-03-13 17:21:36 +02001937 rcu_read_lock();
1938 chanctx_conf = rcu_dereference(vif->chanctx_conf);
1939 if (WARN_ON(!chanctx_conf))
1940 band = IEEE80211_NUM_BANDS;
1941 else
1942 band = chanctx_conf->def.chan->band;
1943 rcu_read_unlock();
1944
Eyal Shapira1e9551d2014-04-21 21:33:17 +03001945 if (!rs_tpc_allowed(mvm, vif, rate, band)) {
Eliad Peller2fd647f2014-03-13 17:21:36 +02001946 IWL_DEBUG_RATE(mvm,
Johannes Berg3c6acb62014-05-07 11:47:53 +02001947 "tpc is not allowed. remove txp restrictions\n");
Eliad Peller2fd647f2014-03-13 17:21:36 +02001948 lq_sta->lq.reduced_tpc = TPC_NO_REDUCTION;
1949 return cur != TPC_NO_REDUCTION;
1950 }
1951
1952 rs_get_adjacent_txp(mvm, cur, &weak, &strong);
1953
1954 /* Collect measured throughputs for current and adjacent rates */
1955 window = tbl->tpc_win;
1956 sr = window[cur].success_ratio;
1957 current_tpt = window[cur].average_tpt;
1958 if (weak != TPC_INVALID)
1959 weak_tpt = window[weak].average_tpt;
1960 if (strong != TPC_INVALID)
1961 strong_tpt = window[strong].average_tpt;
1962
1963 IWL_DEBUG_RATE(mvm,
1964 "(TPC: %d): cur_tpt %d SR %d weak %d strong %d weak_tpt %d strong_tpt %d\n",
1965 cur, current_tpt, sr, weak, strong,
1966 weak_tpt, strong_tpt);
1967
1968 action = rs_get_tpc_action(mvm, sr, weak, strong,
1969 current_tpt, weak_tpt, strong_tpt);
1970
1971 /* override actions if we are on the edge */
1972 if (weak == TPC_INVALID && action == TPC_ACTION_DECREASE) {
Johannes Berg3c6acb62014-05-07 11:47:53 +02001973 IWL_DEBUG_RATE(mvm, "already in lowest txp, stay\n");
Eliad Peller2fd647f2014-03-13 17:21:36 +02001974 action = TPC_ACTION_STAY;
1975 } else if (strong == TPC_INVALID &&
1976 (action == TPC_ACTION_INCREASE ||
1977 action == TPC_ACTION_NO_RESTIRCTION)) {
Johannes Berg3c6acb62014-05-07 11:47:53 +02001978 IWL_DEBUG_RATE(mvm, "already in highest txp, stay\n");
Eliad Peller2fd647f2014-03-13 17:21:36 +02001979 action = TPC_ACTION_STAY;
1980 }
1981
1982 switch (action) {
1983 case TPC_ACTION_DECREASE:
1984 lq_sta->lq.reduced_tpc = weak;
1985 return true;
1986 case TPC_ACTION_INCREASE:
1987 lq_sta->lq.reduced_tpc = strong;
1988 return true;
1989 case TPC_ACTION_NO_RESTIRCTION:
1990 lq_sta->lq.reduced_tpc = TPC_NO_REDUCTION;
1991 return true;
1992 case TPC_ACTION_STAY:
1993 /* do nothing */
1994 break;
1995 }
1996 return false;
1997}
1998
Johannes Berg8ca151b2013-01-24 14:25:36 +01001999/*
2000 * Do rate scaling and search for new modulation mode.
2001 */
2002static void rs_rate_scale_perform(struct iwl_mvm *mvm,
Johannes Berg8ca151b2013-01-24 14:25:36 +01002003 struct ieee80211_sta *sta,
Eyal Shapira1ebc8f22014-09-14 15:23:23 +03002004 struct iwl_lq_sta *lq_sta,
2005 int tid)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002006{
Johannes Berg8ca151b2013-01-24 14:25:36 +01002007 int low = IWL_RATE_INVALID;
2008 int high = IWL_RATE_INVALID;
2009 int index;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002010 struct iwl_rate_scale_data *window = NULL;
2011 int current_tpt = IWL_INVALID_VALUE;
2012 int low_tpt = IWL_INVALID_VALUE;
2013 int high_tpt = IWL_INVALID_VALUE;
2014 u32 fail_count;
Eyal Shapira4e4b8152013-12-11 00:55:36 +02002015 enum rs_action scale_action = RS_ACTION_STAY;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002016 u16 rate_mask;
2017 u8 update_lq = 0;
2018 struct iwl_scale_tbl_info *tbl, *tbl1;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002019 u8 active_tbl = 0;
2020 u8 done_search = 0;
2021 u16 high_low;
2022 s32 sr;
Eyal Shapira9d015a02013-11-21 01:12:11 +02002023 u8 prev_agg = lq_sta->is_agg;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002024 struct iwl_mvm_sta *sta_priv = (void *)sta->drv_priv;
2025 struct iwl_mvm_tid_data *tid_data;
Eyal Shapira5aa33552013-11-23 01:06:36 +02002026 struct rs_rate *rate;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002027
Eyal Shapiraefed6642014-09-14 15:58:53 +03002028 lq_sta->is_agg = !!sta_priv->agg_tids;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002029
2030 /*
2031 * Select rate-scale / modulation-mode table to work with in
2032 * the rest of this function: "search" if searching for better
2033 * modulation mode, or "active" if doing rate scaling within a mode.
2034 */
2035 if (!lq_sta->search_better_tbl)
2036 active_tbl = lq_sta->active_tbl;
2037 else
2038 active_tbl = 1 - lq_sta->active_tbl;
2039
2040 tbl = &(lq_sta->lq_info[active_tbl]);
Eyal Shapira5aa33552013-11-23 01:06:36 +02002041 rate = &tbl->rate;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002042
Eyal Shapira9d015a02013-11-21 01:12:11 +02002043 if (prev_agg != lq_sta->is_agg) {
2044 IWL_DEBUG_RATE(mvm,
2045 "Aggregation changed: prev %d current %d. Update expected TPT table\n",
2046 prev_agg, lq_sta->is_agg);
2047 rs_set_expected_tpt_table(lq_sta, tbl);
Eyal Shapirab804eeb2014-04-06 04:27:06 +03002048 rs_rate_scale_clear_tbl_windows(mvm, tbl);
Eyal Shapira9d015a02013-11-21 01:12:11 +02002049 }
2050
Johannes Berg8ca151b2013-01-24 14:25:36 +01002051 /* current tx rate */
2052 index = lq_sta->last_txrate_idx;
2053
Johannes Berg8ca151b2013-01-24 14:25:36 +01002054 /* rates available for this association, and for modulation mode */
Eyal Shapiraa8ff14f2013-11-23 01:30:44 +02002055 rate_mask = rs_get_supported_rates(lq_sta, rate);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002056
Eyal Shapiraca6f38f2013-12-09 11:14:11 +02002057 if (!(BIT(index) & rate_mask)) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002058 IWL_ERR(mvm, "Current Rate is not valid\n");
2059 if (lq_sta->search_better_tbl) {
2060 /* revert to active table if search table is not valid*/
Eyal Shapira5aa33552013-11-23 01:06:36 +02002061 rate->type = LQ_NONE;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002062 lq_sta->search_better_tbl = 0;
2063 tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
Eyal Shapira5aa33552013-11-23 01:06:36 +02002064 rs_update_rate_tbl(mvm, sta, lq_sta, &tbl->rate);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002065 }
2066 return;
2067 }
2068
2069 /* Get expected throughput table and history window for current rate */
2070 if (!tbl->expected_tpt) {
2071 IWL_ERR(mvm, "tbl->expected_tpt is NULL\n");
2072 return;
2073 }
2074
Eyal Shapira2536e7c2014-08-31 02:33:33 +03002075 /* TODO: handle rate_idx_mask and rate_idx_mcs_mask */
Johannes Berg8ca151b2013-01-24 14:25:36 +01002076 window = &(tbl->win[index]);
2077
2078 /*
2079 * If there is not enough history to calculate actual average
2080 * throughput, keep analyzing results of more tx frames, without
2081 * changing rate or mode (bypass most of the rest of this function).
2082 * Set up new rate table in uCode only if old rate is not supported
2083 * in current association (use new rate found above).
2084 */
2085 fail_count = window->counter - window->success_counter;
2086 if ((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
2087 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH)) {
2088 IWL_DEBUG_RATE(mvm,
Eyal Shapira4d30ee82013-11-20 01:45:38 +02002089 "(%s: %d): Test Window: succ %d total %d\n",
Eyal Shapira5aa33552013-11-23 01:06:36 +02002090 rs_pretty_lq_type(rate->type),
Eyal Shapira4d30ee82013-11-20 01:45:38 +02002091 index, window->success_counter, window->counter);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002092
2093 /* Can't calculate this yet; not enough history */
2094 window->average_tpt = IWL_INVALID_VALUE;
2095
2096 /* Should we stay with this modulation mode,
2097 * or search for a new one? */
2098 rs_stay_in_table(lq_sta, false);
2099
2100 goto out;
2101 }
2102 /* Else we have enough samples; calculate estimate of
2103 * actual average throughput */
2104 if (window->average_tpt != ((window->success_ratio *
2105 tbl->expected_tpt[index] + 64) / 128)) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002106 window->average_tpt = ((window->success_ratio *
2107 tbl->expected_tpt[index] + 64) / 128);
2108 }
2109
2110 /* If we are searching for better modulation mode, check success. */
2111 if (lq_sta->search_better_tbl) {
2112 /* If good success, continue using the "search" mode;
2113 * no need to send new link quality command, since we're
2114 * continuing to use the setup that we've been trying. */
2115 if (window->average_tpt > lq_sta->last_tpt) {
2116 IWL_DEBUG_RATE(mvm,
Eyal Shapira4d30ee82013-11-20 01:45:38 +02002117 "SWITCHING TO NEW TABLE SR: %d "
2118 "cur-tpt %d old-tpt %d\n",
Johannes Berg8ca151b2013-01-24 14:25:36 +01002119 window->success_ratio,
2120 window->average_tpt,
2121 lq_sta->last_tpt);
2122
Johannes Berg8ca151b2013-01-24 14:25:36 +01002123 /* Swap tables; "search" becomes "active" */
2124 lq_sta->active_tbl = active_tbl;
2125 current_tpt = window->average_tpt;
2126 /* Else poor success; go back to mode in "active" table */
2127 } else {
2128 IWL_DEBUG_RATE(mvm,
Eyal Shapira4d30ee82013-11-20 01:45:38 +02002129 "GOING BACK TO THE OLD TABLE: SR %d "
2130 "cur-tpt %d old-tpt %d\n",
Johannes Berg8ca151b2013-01-24 14:25:36 +01002131 window->success_ratio,
2132 window->average_tpt,
2133 lq_sta->last_tpt);
2134
2135 /* Nullify "search" table */
Eyal Shapira5aa33552013-11-23 01:06:36 +02002136 rate->type = LQ_NONE;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002137
2138 /* Revert to "active" table */
2139 active_tbl = lq_sta->active_tbl;
2140 tbl = &(lq_sta->lq_info[active_tbl]);
2141
2142 /* Revert to "active" rate and throughput info */
Eyal Shapira8fc7c582013-12-04 02:15:46 +02002143 index = tbl->rate.index;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002144 current_tpt = lq_sta->last_tpt;
2145
2146 /* Need to set up a new rate table in uCode */
2147 update_lq = 1;
2148 }
2149
2150 /* Either way, we've made a decision; modulation mode
2151 * search is done, allow rate adjustment next time. */
2152 lq_sta->search_better_tbl = 0;
2153 done_search = 1; /* Don't switch modes below! */
2154 goto lq_update;
2155 }
2156
2157 /* (Else) not in search of better modulation mode, try for better
2158 * starting rate, while staying in this mode. */
Eyal Shapiraca6f38f2013-12-09 11:14:11 +02002159 high_low = rs_get_adjacent_rate(mvm, index, rate_mask, rate->type);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002160 low = high_low & 0xff;
2161 high = (high_low >> 8) & 0xff;
2162
Eyal Shapira2536e7c2014-08-31 02:33:33 +03002163 /* TODO: handle rate_idx_mask and rate_idx_mcs_mask */
Johannes Berg8ca151b2013-01-24 14:25:36 +01002164
2165 sr = window->success_ratio;
2166
2167 /* Collect measured throughputs for current and adjacent rates */
2168 current_tpt = window->average_tpt;
2169 if (low != IWL_RATE_INVALID)
2170 low_tpt = tbl->win[low].average_tpt;
2171 if (high != IWL_RATE_INVALID)
2172 high_tpt = tbl->win[high].average_tpt;
2173
Eyal Shapira4d30ee82013-11-20 01:45:38 +02002174 IWL_DEBUG_RATE(mvm,
2175 "(%s: %d): cur_tpt %d SR %d low %d high %d low_tpt %d high_tpt %d\n",
Eyal Shapira5aa33552013-11-23 01:06:36 +02002176 rs_pretty_lq_type(rate->type), index, current_tpt, sr,
2177 low, high, low_tpt, high_tpt);
Eyal Shapira4d30ee82013-11-20 01:45:38 +02002178
Eyal Shapira4e4b8152013-12-11 00:55:36 +02002179 scale_action = rs_get_rate_action(mvm, tbl, sr, low, high,
2180 current_tpt, low_tpt, high_tpt);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002181
Eyal Shapira393b9e52013-11-13 16:46:19 +02002182 /* Force a search in case BT doesn't like us being in MIMO */
Eyal Shapira5aa33552013-11-23 01:06:36 +02002183 if (is_mimo(rate) &&
Eyal Shapira393b9e52013-11-13 16:46:19 +02002184 !iwl_mvm_bt_coex_is_mimo_allowed(mvm, sta)) {
Eyal Shapira4d30ee82013-11-20 01:45:38 +02002185 IWL_DEBUG_RATE(mvm,
2186 "BT Coex forbids MIMO. Search for new config\n");
Emmanuel Grumbach36946ce2013-05-28 23:12:47 +03002187 rs_stay_in_table(lq_sta, true);
2188 goto lq_update;
2189 }
2190
Johannes Berg8ca151b2013-01-24 14:25:36 +01002191 switch (scale_action) {
Eyal Shapira4e4b8152013-12-11 00:55:36 +02002192 case RS_ACTION_DOWNSCALE:
Johannes Berg8ca151b2013-01-24 14:25:36 +01002193 /* Decrease starting rate, update uCode's rate table */
2194 if (low != IWL_RATE_INVALID) {
2195 update_lq = 1;
2196 index = low;
Eyal Shapira4d30ee82013-11-20 01:45:38 +02002197 } else {
2198 IWL_DEBUG_RATE(mvm,
2199 "At the bottom rate. Can't decrease\n");
Johannes Berg8ca151b2013-01-24 14:25:36 +01002200 }
2201
2202 break;
Eyal Shapira4e4b8152013-12-11 00:55:36 +02002203 case RS_ACTION_UPSCALE:
Johannes Berg8ca151b2013-01-24 14:25:36 +01002204 /* Increase starting rate, update uCode's rate table */
2205 if (high != IWL_RATE_INVALID) {
2206 update_lq = 1;
2207 index = high;
Eyal Shapira4d30ee82013-11-20 01:45:38 +02002208 } else {
2209 IWL_DEBUG_RATE(mvm,
2210 "At the top rate. Can't increase\n");
Johannes Berg8ca151b2013-01-24 14:25:36 +01002211 }
2212
2213 break;
Eyal Shapira4e4b8152013-12-11 00:55:36 +02002214 case RS_ACTION_STAY:
Johannes Berg8ca151b2013-01-24 14:25:36 +01002215 /* No change */
Eyal Shapirae6c8d602014-04-21 21:49:08 +03002216 if (lq_sta->rs_state == RS_STATE_STAY_IN_COLUMN)
2217 update_lq = rs_tpc_perform(mvm, sta, lq_sta, tbl);
Eliad Peller2fd647f2014-03-13 17:21:36 +02002218 break;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002219 default:
2220 break;
2221 }
2222
Johannes Berg8ca151b2013-01-24 14:25:36 +01002223lq_update:
2224 /* Replace uCode's rate table for the destination station. */
Eyal Shapira5aa33552013-11-23 01:06:36 +02002225 if (update_lq) {
2226 tbl->rate.index = index;
2227 rs_update_rate_tbl(mvm, sta, lq_sta, &tbl->rate);
2228 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01002229
2230 rs_stay_in_table(lq_sta, false);
2231
2232 /*
2233 * Search for new modulation mode if we're:
2234 * 1) Not changing rates right now
2235 * 2) Not just finishing up a search
2236 * 3) Allowing a new search
2237 */
2238 if (!update_lq && !done_search &&
Eyal Shapirab3b06a32013-11-24 21:30:13 +02002239 lq_sta->rs_state == RS_STATE_SEARCH_CYCLE_STARTED
2240 && window->counter) {
2241 enum rs_column next_column;
2242
Johannes Berg8ca151b2013-01-24 14:25:36 +01002243 /* Save current throughput to compare with "search" throughput*/
2244 lq_sta->last_tpt = current_tpt;
2245
Eyal Shapira4d30ee82013-11-20 01:45:38 +02002246 IWL_DEBUG_RATE(mvm,
Eyal Shapirab3b06a32013-11-24 21:30:13 +02002247 "Start Search: update_lq %d done_search %d rs_state %d win->counter %d\n",
2248 update_lq, done_search, lq_sta->rs_state,
Eyal Shapira4d30ee82013-11-20 01:45:38 +02002249 window->counter);
Eyal Shapirab3b06a32013-11-24 21:30:13 +02002250
2251 next_column = rs_get_next_column(mvm, lq_sta, sta, tbl);
2252 if (next_column != RS_COLUMN_INVALID) {
2253 int ret = rs_switch_to_column(mvm, lq_sta, sta,
2254 next_column);
2255 if (!ret)
2256 lq_sta->search_better_tbl = 1;
2257 } else {
2258 IWL_DEBUG_RATE(mvm,
2259 "No more columns to explore in search cycle. Go to RS_STATE_SEARCH_CYCLE_ENDED\n");
2260 lq_sta->rs_state = RS_STATE_SEARCH_CYCLE_ENDED;
2261 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01002262
2263 /* If new "search" mode was selected, set up in uCode table */
2264 if (lq_sta->search_better_tbl) {
2265 /* Access the "search" table, clear its history. */
2266 tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Eyal Shapirab804eeb2014-04-06 04:27:06 +03002267 rs_rate_scale_clear_tbl_windows(mvm, tbl);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002268
2269 /* Use new "search" start rate */
Eyal Shapira8fc7c582013-12-04 02:15:46 +02002270 index = tbl->rate.index;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002271
Eyal Shapirab3b06a32013-11-24 21:30:13 +02002272 rs_dump_rate(mvm, &tbl->rate,
2273 "Switch to SEARCH TABLE:");
Eyal Shapira8fc7c582013-12-04 02:15:46 +02002274 rs_fill_lq_cmd(mvm, sta, lq_sta, &tbl->rate);
Eyal Shapira9e680942013-11-09 00:16:16 +02002275 iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, false);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002276 } else {
2277 done_search = 1;
2278 }
2279 }
2280
Eyal Shapirab3b06a32013-11-24 21:30:13 +02002281 if (done_search && lq_sta->rs_state == RS_STATE_SEARCH_CYCLE_ENDED) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002282 /* If the "active" (non-search) mode was legacy,
2283 * and we've tried switching antennas,
2284 * but we haven't been able to try HT modes (not available),
2285 * stay with best antenna legacy modulation for a while
2286 * before next round of mode comparisons. */
2287 tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
Eyal Shapirafd7dbee2014-04-06 04:39:23 +03002288 if (is_legacy(&tbl1->rate)) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002289 IWL_DEBUG_RATE(mvm, "LQ: STAY in legacy table\n");
Eyal Shapirafd7dbee2014-04-06 04:39:23 +03002290
2291 if (tid != IWL_MAX_TID_COUNT) {
2292 tid_data = &sta_priv->tid_data[tid];
2293 if (tid_data->state != IWL_AGG_OFF) {
2294 IWL_DEBUG_RATE(mvm,
2295 "Stop aggregation on tid %d\n",
2296 tid);
2297 ieee80211_stop_tx_ba_session(sta, tid);
2298 }
2299 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01002300 rs_set_stay_in_table(mvm, 1, lq_sta);
Eyal Shapirab3b06a32013-11-24 21:30:13 +02002301 } else {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002302 /* If we're in an HT mode, and all 3 mode switch actions
2303 * have been tried and compared, stay in this best modulation
2304 * mode for a while before next round of mode comparisons. */
Johannes Berg8ca151b2013-01-24 14:25:36 +01002305 if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
2306 (lq_sta->tx_agg_tid_en & (1 << tid)) &&
2307 (tid != IWL_MAX_TID_COUNT)) {
2308 tid_data = &sta_priv->tid_data[tid];
2309 if (tid_data->state == IWL_AGG_OFF) {
2310 IWL_DEBUG_RATE(mvm,
2311 "try to aggregate tid %d\n",
2312 tid);
2313 rs_tl_turn_on_agg(mvm, tid,
2314 lq_sta, sta);
2315 }
2316 }
2317 rs_set_stay_in_table(mvm, 0, lq_sta);
2318 }
2319 }
2320
2321out:
Johannes Berg8ca151b2013-01-24 14:25:36 +01002322 lq_sta->last_txrate_idx = index;
2323}
2324
Eyal Shapira361dbec2014-08-13 00:31:13 +03002325struct rs_init_rate_info {
2326 s8 rssi;
2327 u8 rate_idx;
2328};
2329
2330static const struct rs_init_rate_info rs_init_rates_24ghz[] = {
2331 { -60, IWL_RATE_54M_INDEX },
2332 { -64, IWL_RATE_48M_INDEX },
2333 { -68, IWL_RATE_36M_INDEX },
2334 { -80, IWL_RATE_24M_INDEX },
2335 { -84, IWL_RATE_18M_INDEX },
2336 { -85, IWL_RATE_12M_INDEX },
2337 { -86, IWL_RATE_11M_INDEX },
2338 { -88, IWL_RATE_5M_INDEX },
2339 { -90, IWL_RATE_2M_INDEX },
2340 { S8_MIN, IWL_RATE_1M_INDEX },
2341};
2342
2343static const struct rs_init_rate_info rs_init_rates_5ghz[] = {
2344 { -60, IWL_RATE_54M_INDEX },
2345 { -64, IWL_RATE_48M_INDEX },
2346 { -72, IWL_RATE_36M_INDEX },
2347 { -80, IWL_RATE_24M_INDEX },
2348 { -84, IWL_RATE_18M_INDEX },
2349 { -85, IWL_RATE_12M_INDEX },
2350 { -87, IWL_RATE_9M_INDEX },
2351 { S8_MIN, IWL_RATE_6M_INDEX },
2352};
2353
2354/* Choose an initial legacy rate and antenna to use based on the RSSI
2355 * of last Rx
2356 */
2357static void rs_get_initial_rate(struct iwl_mvm *mvm,
2358 struct iwl_lq_sta *lq_sta,
2359 enum ieee80211_band band,
2360 struct rs_rate *rate)
2361{
2362 int i, nentries;
2363 s8 best_rssi = S8_MIN;
2364 u8 best_ant = ANT_NONE;
2365 u8 valid_tx_ant = mvm->fw->valid_tx_ant;
2366 const struct rs_init_rate_info *initial_rates;
2367
2368 for (i = 0; i < ARRAY_SIZE(lq_sta->pers.chain_signal); i++) {
2369 if (!(lq_sta->pers.chains & BIT(i)))
2370 continue;
2371
2372 if (lq_sta->pers.chain_signal[i] > best_rssi) {
2373 best_rssi = lq_sta->pers.chain_signal[i];
2374 best_ant = BIT(i);
2375 }
2376 }
2377
2378 IWL_DEBUG_RATE(mvm, "Best ANT: %s Best RSSI: %d\n",
2379 rs_pretty_ant(best_ant), best_rssi);
2380
2381 if (best_ant != ANT_A && best_ant != ANT_B)
2382 rate->ant = first_antenna(valid_tx_ant);
2383 else
2384 rate->ant = best_ant;
2385
2386 rate->sgi = false;
2387 rate->ldpc = false;
2388 rate->bw = RATE_MCS_CHAN_WIDTH_20;
2389
2390 rate->index = find_first_bit(&lq_sta->active_legacy_rate,
2391 BITS_PER_LONG);
2392
2393 if (band == IEEE80211_BAND_5GHZ) {
2394 rate->type = LQ_LEGACY_A;
2395 initial_rates = rs_init_rates_5ghz;
2396 nentries = ARRAY_SIZE(rs_init_rates_5ghz);
2397 } else {
2398 rate->type = LQ_LEGACY_G;
2399 initial_rates = rs_init_rates_24ghz;
2400 nentries = ARRAY_SIZE(rs_init_rates_24ghz);
2401 }
2402
2403 if (IWL_MVM_RS_RSSI_BASED_INIT_RATE) {
2404 for (i = 0; i < nentries; i++) {
2405 int rate_idx = initial_rates[i].rate_idx;
2406 if ((best_rssi >= initial_rates[i].rssi) &&
2407 (BIT(rate_idx) & lq_sta->active_legacy_rate)) {
2408 rate->index = rate_idx;
2409 break;
2410 }
2411 }
2412 }
2413
2414 IWL_DEBUG_RATE(mvm, "rate_idx %d ANT %s\n", rate->index,
2415 rs_pretty_ant(rate->ant));
2416}
2417
2418/* Save info about RSSI of last Rx */
2419void rs_update_last_rssi(struct iwl_mvm *mvm,
2420 struct iwl_lq_sta *lq_sta,
2421 struct ieee80211_rx_status *rx_status)
2422{
2423 lq_sta->pers.chains = rx_status->chains;
2424 lq_sta->pers.chain_signal[0] = rx_status->chain_signal[0];
2425 lq_sta->pers.chain_signal[1] = rx_status->chain_signal[1];
2426 lq_sta->pers.chain_signal[2] = rx_status->chain_signal[2];
2427}
2428
Johannes Berg8ca151b2013-01-24 14:25:36 +01002429/**
2430 * rs_initialize_lq - Initialize a station's hardware rate table
2431 *
2432 * The uCode's station table contains a table of fallback rates
2433 * for automatic fallback during transmission.
2434 *
2435 * NOTE: This sets up a default set of values. These will be replaced later
2436 * if the driver's iwl-agn-rs rate scaling algorithm is used, instead of
2437 * rc80211_simple.
2438 *
2439 * NOTE: Run REPLY_ADD_STA command to set up station table entry, before
2440 * calling this function (which runs REPLY_TX_LINK_QUALITY_CMD,
2441 * which requires station table entry to exist).
2442 */
2443static void rs_initialize_lq(struct iwl_mvm *mvm,
2444 struct ieee80211_sta *sta,
2445 struct iwl_lq_sta *lq_sta,
Eyal Shapirab87c2172013-11-09 23:37:55 +02002446 enum ieee80211_band band,
2447 bool init)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002448{
2449 struct iwl_scale_tbl_info *tbl;
Eyal Shapira5aa33552013-11-23 01:06:36 +02002450 struct rs_rate *rate;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002451 u8 active_tbl = 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002452
2453 if (!sta || !lq_sta)
2454 return;
2455
Johannes Berg8ca151b2013-01-24 14:25:36 +01002456 if (!lq_sta->search_better_tbl)
2457 active_tbl = lq_sta->active_tbl;
2458 else
2459 active_tbl = 1 - lq_sta->active_tbl;
2460
2461 tbl = &(lq_sta->lq_info[active_tbl]);
Eyal Shapira5aa33552013-11-23 01:06:36 +02002462 rate = &tbl->rate;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002463
Eyal Shapira361dbec2014-08-13 00:31:13 +03002464 rs_get_initial_rate(mvm, lq_sta, band, rate);
2465 lq_sta->last_txrate_idx = rate->index;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002466
Eyal Shapirab3b06a32013-11-24 21:30:13 +02002467 WARN_ON_ONCE(rate->ant != ANT_A && rate->ant != ANT_B);
2468 if (rate->ant == ANT_A)
2469 tbl->column = RS_COLUMN_LEGACY_ANT_A;
2470 else
2471 tbl->column = RS_COLUMN_LEGACY_ANT_B;
2472
Johannes Berg8ca151b2013-01-24 14:25:36 +01002473 rs_set_expected_tpt_table(lq_sta, tbl);
Eyal Shapira80e9e5c2013-12-23 16:26:41 +02002474 rs_fill_lq_cmd(mvm, sta, lq_sta, rate);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002475 /* TODO restore station should remember the lq cmd */
Eyal Shapirab87c2172013-11-09 23:37:55 +02002476 iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, init);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002477}
2478
2479static void rs_get_rate(void *mvm_r, struct ieee80211_sta *sta, void *mvm_sta,
2480 struct ieee80211_tx_rate_control *txrc)
2481{
2482 struct sk_buff *skb = txrc->skb;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002483 struct iwl_op_mode *op_mode __maybe_unused =
2484 (struct iwl_op_mode *)mvm_r;
2485 struct iwl_mvm *mvm __maybe_unused = IWL_OP_MODE_GET_MVM(op_mode);
2486 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2487 struct iwl_lq_sta *lq_sta = mvm_sta;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002488
Eyal Shapira2536e7c2014-08-31 02:33:33 +03002489 /* TODO: handle rate_idx_mask and rate_idx_mcs_mask */
Johannes Berg8ca151b2013-01-24 14:25:36 +01002490
2491 /* Treat uninitialized rate scaling data same as non-existing. */
Eliad Pellerae969af2014-06-11 15:51:33 +03002492 if (lq_sta && !lq_sta->pers.drv) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002493 IWL_DEBUG_RATE(mvm, "Rate scaling not initialized yet.\n");
2494 mvm_sta = NULL;
2495 }
2496
2497 /* Send management frames and NO_ACK data using lowest rate. */
2498 if (rate_control_send_low(sta, mvm_sta, txrc))
2499 return;
2500
Eyal Shapirad310e402013-08-11 18:43:47 +03002501 iwl_mvm_hwrate_to_tx_rate(lq_sta->last_rate_n_flags,
2502 info->band, &info->control.rates[0]);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002503
Moshe Benji622ebe92013-06-03 19:27:16 +03002504 info->control.rates[0].count = 1;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002505}
2506
2507static void *rs_alloc_sta(void *mvm_rate, struct ieee80211_sta *sta,
2508 gfp_t gfp)
2509{
2510 struct iwl_mvm_sta *sta_priv = (struct iwl_mvm_sta *)sta->drv_priv;
Eliad Pellerae969af2014-06-11 15:51:33 +03002511 struct iwl_op_mode *op_mode = (struct iwl_op_mode *)mvm_rate;
2512 struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
2513 struct iwl_lq_sta *lq_sta = &sta_priv->lq_sta;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002514
2515 IWL_DEBUG_RATE(mvm, "create station rate scale window\n");
2516
Eliad Pellerae969af2014-06-11 15:51:33 +03002517 lq_sta->pers.drv = mvm;
2518#ifdef CONFIG_MAC80211_DEBUGFS
2519 lq_sta->pers.dbg_fixed_rate = 0;
2520 lq_sta->pers.dbg_fixed_txp_reduction = TPC_INVALID;
2521#endif
Eyal Shapira361dbec2014-08-13 00:31:13 +03002522 lq_sta->pers.chains = 0;
2523 memset(lq_sta->pers.chain_signal, 0, sizeof(lq_sta->pers.chain_signal));
Eliad Pellerae969af2014-06-11 15:51:33 +03002524
Johannes Berg8ca151b2013-01-24 14:25:36 +01002525 return &sta_priv->lq_sta;
2526}
2527
Eyal Shapirad310e402013-08-11 18:43:47 +03002528static int rs_vht_highest_rx_mcs_index(struct ieee80211_sta_vht_cap *vht_cap,
2529 int nss)
2530{
2531 u16 rx_mcs = le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map) &
2532 (0x3 << (2 * (nss - 1)));
2533 rx_mcs >>= (2 * (nss - 1));
2534
2535 if (rx_mcs == IEEE80211_VHT_MCS_SUPPORT_0_7)
2536 return IWL_RATE_MCS_7_INDEX;
2537 else if (rx_mcs == IEEE80211_VHT_MCS_SUPPORT_0_8)
2538 return IWL_RATE_MCS_8_INDEX;
2539 else if (rx_mcs == IEEE80211_VHT_MCS_SUPPORT_0_9)
2540 return IWL_RATE_MCS_9_INDEX;
2541
2542 WARN_ON_ONCE(rx_mcs != IEEE80211_VHT_MCS_NOT_SUPPORTED);
2543 return -1;
2544}
2545
Eyal Shapira750a1b42013-11-09 22:48:56 +02002546static void rs_vht_set_enabled_rates(struct ieee80211_sta *sta,
2547 struct ieee80211_sta_vht_cap *vht_cap,
2548 struct iwl_lq_sta *lq_sta)
2549{
2550 int i;
2551 int highest_mcs = rs_vht_highest_rx_mcs_index(vht_cap, 1);
2552
2553 if (highest_mcs >= IWL_RATE_MCS_0_INDEX) {
2554 for (i = IWL_RATE_MCS_0_INDEX; i <= highest_mcs; i++) {
2555 if (i == IWL_RATE_9M_INDEX)
2556 continue;
2557
Eyal Shapira271518a2013-11-09 23:25:06 +02002558 /* VHT MCS9 isn't valid for 20Mhz for NSS=1,2 */
2559 if (i == IWL_RATE_MCS_9_INDEX &&
2560 sta->bandwidth == IEEE80211_STA_RX_BW_20)
2561 continue;
2562
Eyal Shapira750a1b42013-11-09 22:48:56 +02002563 lq_sta->active_siso_rate |= BIT(i);
2564 }
2565 }
2566
Eyal Shapiraecc90e72013-11-09 23:31:38 +02002567 if (sta->rx_nss < 2)
2568 return;
2569
Eyal Shapira750a1b42013-11-09 22:48:56 +02002570 highest_mcs = rs_vht_highest_rx_mcs_index(vht_cap, 2);
2571 if (highest_mcs >= IWL_RATE_MCS_0_INDEX) {
2572 for (i = IWL_RATE_MCS_0_INDEX; i <= highest_mcs; i++) {
2573 if (i == IWL_RATE_9M_INDEX)
2574 continue;
2575
Eyal Shapira271518a2013-11-09 23:25:06 +02002576 /* VHT MCS9 isn't valid for 20Mhz for NSS=1,2 */
2577 if (i == IWL_RATE_MCS_9_INDEX &&
2578 sta->bandwidth == IEEE80211_STA_RX_BW_20)
2579 continue;
2580
Eyal Shapira750a1b42013-11-09 22:48:56 +02002581 lq_sta->active_mimo2_rate |= BIT(i);
2582 }
2583 }
2584}
2585
Eyal Shapira5fc0f762014-01-28 01:35:32 +02002586#ifdef CONFIG_IWLWIFI_DEBUGFS
2587static void iwl_mvm_reset_frame_stats(struct iwl_mvm *mvm,
2588 struct iwl_mvm_frame_stats *stats)
2589{
2590 spin_lock_bh(&mvm->drv_stats_lock);
2591 memset(stats, 0, sizeof(*stats));
2592 spin_unlock_bh(&mvm->drv_stats_lock);
2593}
2594
2595void iwl_mvm_update_frame_stats(struct iwl_mvm *mvm,
2596 struct iwl_mvm_frame_stats *stats,
2597 u32 rate, bool agg)
2598{
2599 u8 nss = 0, mcs = 0;
2600
2601 spin_lock(&mvm->drv_stats_lock);
2602
2603 if (agg)
2604 stats->agg_frames++;
2605
2606 stats->success_frames++;
2607
2608 switch (rate & RATE_MCS_CHAN_WIDTH_MSK) {
2609 case RATE_MCS_CHAN_WIDTH_20:
2610 stats->bw_20_frames++;
2611 break;
2612 case RATE_MCS_CHAN_WIDTH_40:
2613 stats->bw_40_frames++;
2614 break;
2615 case RATE_MCS_CHAN_WIDTH_80:
2616 stats->bw_80_frames++;
2617 break;
2618 default:
2619 WARN_ONCE(1, "bad BW. rate 0x%x", rate);
2620 }
2621
2622 if (rate & RATE_MCS_HT_MSK) {
2623 stats->ht_frames++;
2624 mcs = rate & RATE_HT_MCS_RATE_CODE_MSK;
2625 nss = ((rate & RATE_HT_MCS_NSS_MSK) >> RATE_HT_MCS_NSS_POS) + 1;
2626 } else if (rate & RATE_MCS_VHT_MSK) {
2627 stats->vht_frames++;
2628 mcs = rate & RATE_VHT_MCS_RATE_CODE_MSK;
2629 nss = ((rate & RATE_VHT_MCS_NSS_MSK) >>
2630 RATE_VHT_MCS_NSS_POS) + 1;
2631 } else {
2632 stats->legacy_frames++;
2633 }
2634
2635 if (nss == 1)
2636 stats->siso_frames++;
2637 else if (nss == 2)
2638 stats->mimo2_frames++;
2639
2640 if (rate & RATE_MCS_SGI_MSK)
2641 stats->sgi_frames++;
2642 else
2643 stats->ngi_frames++;
2644
2645 stats->last_rates[stats->last_frame_idx] = rate;
2646 stats->last_frame_idx = (stats->last_frame_idx + 1) %
2647 ARRAY_SIZE(stats->last_rates);
2648
2649 spin_unlock(&mvm->drv_stats_lock);
2650}
2651#endif
2652
Johannes Berg8ca151b2013-01-24 14:25:36 +01002653/*
2654 * Called after adding a new station to initialize rate scaling
2655 */
2656void iwl_mvm_rs_rate_init(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
Eyal Shapirab87c2172013-11-09 23:37:55 +02002657 enum ieee80211_band band, bool init)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002658{
2659 int i, j;
2660 struct ieee80211_hw *hw = mvm->hw;
2661 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Eyal Shapirad310e402013-08-11 18:43:47 +03002662 struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002663 struct iwl_mvm_sta *sta_priv;
2664 struct iwl_lq_sta *lq_sta;
2665 struct ieee80211_supported_band *sband;
2666 unsigned long supp; /* must be unsigned long for for_each_set_bit */
2667
2668 sta_priv = (struct iwl_mvm_sta *)sta->drv_priv;
2669 lq_sta = &sta_priv->lq_sta;
Eliad Pellerae969af2014-06-11 15:51:33 +03002670
2671 /* clear all non-persistent lq data */
2672 memset(lq_sta, 0, offsetof(typeof(*lq_sta), pers));
Eyal Shapirab87c2172013-11-09 23:37:55 +02002673
Johannes Berg8ca151b2013-01-24 14:25:36 +01002674 sband = hw->wiphy->bands[band];
2675
2676 lq_sta->lq.sta_id = sta_priv->sta_id;
2677
2678 for (j = 0; j < LQ_SIZE; j++)
Eyal Shapirab804eeb2014-04-06 04:27:06 +03002679 rs_rate_scale_clear_tbl_windows(mvm, &lq_sta->lq_info[j]);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002680
2681 lq_sta->flush_timer = 0;
Eyal Shapira87d5e412014-04-06 18:13:22 +03002682 lq_sta->last_tx = jiffies;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002683
2684 IWL_DEBUG_RATE(mvm,
2685 "LQ: *** rate scale station global init for station %d ***\n",
2686 sta_priv->sta_id);
2687 /* TODO: what is a good starting rate for STA? About middle? Maybe not
2688 * the lowest or the highest rate.. Could consider using RSSI from
2689 * previous packets? Need to have IEEE 802.1X auth succeed immediately
2690 * after assoc.. */
2691
Johannes Berg8ca151b2013-01-24 14:25:36 +01002692 lq_sta->missed_rate_counter = IWL_MISSED_RATE_MAX;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002693 lq_sta->band = sband->band;
2694 /*
2695 * active legacy rates as per supported rates bitmap
2696 */
2697 supp = sta->supp_rates[sband->band];
2698 lq_sta->active_legacy_rate = 0;
2699 for_each_set_bit(i, &supp, BITS_PER_LONG)
2700 lq_sta->active_legacy_rate |= BIT(sband->bitrates[i].hw_value);
2701
Eyal Shapirad310e402013-08-11 18:43:47 +03002702 /* TODO: should probably account for rx_highest for both HT/VHT */
2703 if (!vht_cap || !vht_cap->vht_supported) {
2704 /* active_siso_rate mask includes 9 MBits (bit 5),
2705 * and CCK (bits 0-3), supp_rates[] does not;
2706 * shift to convert format, force 9 MBits off.
2707 */
2708 lq_sta->active_siso_rate = ht_cap->mcs.rx_mask[0] << 1;
2709 lq_sta->active_siso_rate |= ht_cap->mcs.rx_mask[0] & 0x1;
2710 lq_sta->active_siso_rate &= ~((u16)0x2);
2711 lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002712
Eyal Shapirad310e402013-08-11 18:43:47 +03002713 /* Same here */
2714 lq_sta->active_mimo2_rate = ht_cap->mcs.rx_mask[1] << 1;
2715 lq_sta->active_mimo2_rate |= ht_cap->mcs.rx_mask[1] & 0x1;
2716 lq_sta->active_mimo2_rate &= ~((u16)0x2);
2717 lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE;
2718
2719 lq_sta->is_vht = false;
Eyal Shapiraa3576ff2014-08-09 10:57:59 +03002720 if (mvm->cfg->ht_params->ldpc &&
2721 (ht_cap->cap & IEEE80211_HT_CAP_LDPC_CODING))
2722 lq_sta->ldpc = true;
Eyal Shapirad8bafea2014-10-23 13:58:38 +03002723
2724 if (mvm->cfg->ht_params->stbc &&
2725 (num_of_ant(mvm->fw->valid_tx_ant) > 1) &&
2726 (ht_cap->cap & IEEE80211_HT_CAP_RX_STBC))
2727 lq_sta->stbc = true;
Eyal Shapirad310e402013-08-11 18:43:47 +03002728 } else {
Eyal Shapira750a1b42013-11-09 22:48:56 +02002729 rs_vht_set_enabled_rates(sta, vht_cap, lq_sta);
Eyal Shapirad310e402013-08-11 18:43:47 +03002730 lq_sta->is_vht = true;
Eyal Shapiraa3576ff2014-08-09 10:57:59 +03002731
2732 if (mvm->cfg->ht_params->ldpc &&
2733 (vht_cap->cap & IEEE80211_VHT_CAP_RXLDPC))
2734 lq_sta->ldpc = true;
Eyal Shapirad8bafea2014-10-23 13:58:38 +03002735
2736 if (mvm->cfg->ht_params->stbc &&
2737 (num_of_ant(mvm->fw->valid_tx_ant) > 1) &&
2738 (vht_cap->cap & IEEE80211_VHT_CAP_RXSTBC_MASK))
2739 lq_sta->stbc = true;
Eyal Shapirad310e402013-08-11 18:43:47 +03002740 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01002741
Eyal Shapiradc574e52014-09-17 13:56:29 +03002742 if (IWL_MVM_RS_DISABLE_MIMO)
2743 lq_sta->active_mimo2_rate = 0;
2744
Eyal Shapira198266a2014-03-31 22:37:39 +03002745 lq_sta->max_legacy_rate_idx = find_last_bit(&lq_sta->active_legacy_rate,
2746 BITS_PER_LONG);
2747 lq_sta->max_siso_rate_idx = find_last_bit(&lq_sta->active_siso_rate,
2748 BITS_PER_LONG);
2749 lq_sta->max_mimo2_rate_idx = find_last_bit(&lq_sta->active_mimo2_rate,
2750 BITS_PER_LONG);
2751
Eyal Shapiraa3576ff2014-08-09 10:57:59 +03002752 IWL_DEBUG_RATE(mvm,
Eyal Shapirad8bafea2014-10-23 13:58:38 +03002753 "RATE MASK: LEGACY=%lX SISO=%lX MIMO2=%lX VHT=%d LDPC=%d STBC%d\n",
Eyal Shapira198266a2014-03-31 22:37:39 +03002754 lq_sta->active_legacy_rate,
Johannes Berg8ca151b2013-01-24 14:25:36 +01002755 lq_sta->active_siso_rate,
Eyal Shapirad310e402013-08-11 18:43:47 +03002756 lq_sta->active_mimo2_rate,
Eyal Shapirad8bafea2014-10-23 13:58:38 +03002757 lq_sta->is_vht, lq_sta->ldpc, lq_sta->stbc);
Eyal Shapira198266a2014-03-31 22:37:39 +03002758 IWL_DEBUG_RATE(mvm, "MAX RATE: LEGACY=%d SISO=%d MIMO2=%d\n",
2759 lq_sta->max_legacy_rate_idx,
2760 lq_sta->max_siso_rate_idx,
2761 lq_sta->max_mimo2_rate_idx);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002762
2763 /* These values will be overridden later */
2764 lq_sta->lq.single_stream_ant_msk =
Johannes Berg4ed735e2014-02-12 21:47:44 +01002765 first_antenna(mvm->fw->valid_tx_ant);
Eyal Shapira809bccf2013-11-28 02:25:24 +02002766 lq_sta->lq.dual_stream_ant_msk = ANT_AB;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002767
2768 /* as default allow aggregation for all tids */
2769 lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002770 lq_sta->is_agg = 0;
Eyal Shapira5fc0f762014-01-28 01:35:32 +02002771#ifdef CONFIG_IWLWIFI_DEBUGFS
2772 iwl_mvm_reset_frame_stats(mvm, &mvm->drv_rx_stats);
2773#endif
Eyal Shapirab87c2172013-11-09 23:37:55 +02002774 rs_initialize_lq(mvm, sta, lq_sta, band, init);
2775}
2776
2777static void rs_rate_update(void *mvm_r,
2778 struct ieee80211_supported_band *sband,
2779 struct cfg80211_chan_def *chandef,
2780 struct ieee80211_sta *sta, void *priv_sta,
2781 u32 changed)
2782{
2783 u8 tid;
2784 struct iwl_op_mode *op_mode =
2785 (struct iwl_op_mode *)mvm_r;
2786 struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
2787
2788 /* Stop any ongoing aggregations as rs starts off assuming no agg */
2789 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++)
2790 ieee80211_stop_tx_ba_session(sta, tid);
2791
2792 iwl_mvm_rs_rate_init(mvm, sta, sband->band, false);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002793}
2794
Eyal Shapira8fc7c582013-12-04 02:15:46 +02002795#ifdef CONFIG_MAC80211_DEBUGFS
2796static void rs_build_rates_table_from_fixed(struct iwl_mvm *mvm,
2797 struct iwl_lq_cmd *lq_cmd,
2798 enum ieee80211_band band,
2799 u32 ucode_rate)
2800{
2801 struct rs_rate rate;
2802 int i;
2803 int num_rates = ARRAY_SIZE(lq_cmd->rs_table);
2804 __le32 ucode_rate_le32 = cpu_to_le32(ucode_rate);
Emmanuel Grumbach7ceb98a2014-07-28 12:59:45 +03002805 u8 ant = (ucode_rate & RATE_MCS_ANT_ABC_MSK) >> RATE_MCS_ANT_POS;
Eyal Shapira8fc7c582013-12-04 02:15:46 +02002806
2807 for (i = 0; i < num_rates; i++)
2808 lq_cmd->rs_table[i] = ucode_rate_le32;
2809
2810 rs_rate_from_ucode_rate(ucode_rate, band, &rate);
2811
2812 if (is_mimo(&rate))
2813 lq_cmd->mimo_delim = num_rates - 1;
2814 else
2815 lq_cmd->mimo_delim = 0;
Emmanuel Grumbach7ceb98a2014-07-28 12:59:45 +03002816
2817 lq_cmd->reduced_tpc = 0;
2818
2819 if (num_of_ant(ant) == 1)
2820 lq_cmd->single_stream_ant_msk = ant;
2821
2822 lq_cmd->agg_frame_cnt_limit = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
Eyal Shapira8fc7c582013-12-04 02:15:46 +02002823}
2824#endif /* CONFIG_MAC80211_DEBUGFS */
2825
Eyal Shapirae07be6d2013-12-09 13:02:56 +02002826static void rs_fill_rates_for_column(struct iwl_mvm *mvm,
2827 struct iwl_lq_sta *lq_sta,
2828 struct rs_rate *rate,
2829 __le32 *rs_table, int *rs_table_index,
2830 int num_rates, int num_retries,
2831 u8 valid_tx_ant, bool toggle_ant)
2832{
2833 int i, j;
2834 __le32 ucode_rate;
2835 bool bottom_reached = false;
2836 int prev_rate_idx = rate->index;
2837 int end = LINK_QUAL_MAX_RETRY_NUM;
2838 int index = *rs_table_index;
2839
2840 for (i = 0; i < num_rates && index < end; i++) {
2841 ucode_rate = cpu_to_le32(ucode_rate_from_rs_rate(mvm, rate));
2842 for (j = 0; j < num_retries && index < end; j++, index++)
2843 rs_table[index] = ucode_rate;
2844
2845 if (toggle_ant)
2846 rs_toggle_antenna(valid_tx_ant, rate);
2847
2848 prev_rate_idx = rate->index;
2849 bottom_reached = rs_get_lower_rate_in_column(lq_sta, rate);
2850 if (bottom_reached && !is_legacy(rate))
2851 break;
2852 }
2853
2854 if (!bottom_reached)
2855 rate->index = prev_rate_idx;
2856
2857 *rs_table_index = index;
2858}
2859
2860/* Building the rate table is non trivial. When we're in MIMO2/VHT/80Mhz/SGI
2861 * column the rate table should look like this:
2862 *
2863 * rate[0] 0x400D019 VHT | ANT: AB BW: 80Mhz MCS: 9 NSS: 2 SGI
2864 * rate[1] 0x400D019 VHT | ANT: AB BW: 80Mhz MCS: 9 NSS: 2 SGI
2865 * rate[2] 0x400D018 VHT | ANT: AB BW: 80Mhz MCS: 8 NSS: 2 SGI
2866 * rate[3] 0x400D018 VHT | ANT: AB BW: 80Mhz MCS: 8 NSS: 2 SGI
2867 * rate[4] 0x400D017 VHT | ANT: AB BW: 80Mhz MCS: 7 NSS: 2 SGI
2868 * rate[5] 0x400D017 VHT | ANT: AB BW: 80Mhz MCS: 7 NSS: 2 SGI
2869 * rate[6] 0x4005007 VHT | ANT: A BW: 80Mhz MCS: 7 NSS: 1 NGI
2870 * rate[7] 0x4009006 VHT | ANT: B BW: 80Mhz MCS: 6 NSS: 1 NGI
2871 * rate[8] 0x4005005 VHT | ANT: A BW: 80Mhz MCS: 5 NSS: 1 NGI
2872 * rate[9] 0x800B Legacy | ANT: B Rate: 36 Mbps
2873 * rate[10] 0x4009 Legacy | ANT: A Rate: 24 Mbps
2874 * rate[11] 0x8007 Legacy | ANT: B Rate: 18 Mbps
2875 * rate[12] 0x4005 Legacy | ANT: A Rate: 12 Mbps
2876 * rate[13] 0x800F Legacy | ANT: B Rate: 9 Mbps
2877 * rate[14] 0x400D Legacy | ANT: A Rate: 6 Mbps
2878 * rate[15] 0x800D Legacy | ANT: B Rate: 6 Mbps
2879 */
Eyal Shapira8fc7c582013-12-04 02:15:46 +02002880static void rs_build_rates_table(struct iwl_mvm *mvm,
Eyal Shapirad8bafea2014-10-23 13:58:38 +03002881 struct ieee80211_sta *sta,
Eyal Shapira8fc7c582013-12-04 02:15:46 +02002882 struct iwl_lq_sta *lq_sta,
2883 const struct rs_rate *initial_rate)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002884{
Eyal Shapira5aa33552013-11-23 01:06:36 +02002885 struct rs_rate rate;
Eyal Shapirae07be6d2013-12-09 13:02:56 +02002886 int num_rates, num_retries, index = 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002887 u8 valid_tx_ant = 0;
2888 struct iwl_lq_cmd *lq_cmd = &lq_sta->lq;
Eyal Shapirae07be6d2013-12-09 13:02:56 +02002889 bool toggle_ant = false;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002890
Emmanuel Grumbach9e898f12013-12-22 10:55:47 +02002891 memcpy(&rate, initial_rate, sizeof(rate));
Johannes Berg8ca151b2013-01-24 14:25:36 +01002892
Johannes Berg4ed735e2014-02-12 21:47:44 +01002893 valid_tx_ant = mvm->fw->valid_tx_ant;
Eyal Shapirad8bafea2014-10-23 13:58:38 +03002894 rate.stbc = rs_stbc_allow(mvm, sta, lq_sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002895
Eyal Shapirae07be6d2013-12-09 13:02:56 +02002896 if (is_siso(&rate)) {
2897 num_rates = RS_INITIAL_SISO_NUM_RATES;
2898 num_retries = RS_HT_VHT_RETRIES_PER_RATE;
2899 } else if (is_mimo(&rate)) {
2900 num_rates = RS_INITIAL_MIMO_NUM_RATES;
2901 num_retries = RS_HT_VHT_RETRIES_PER_RATE;
2902 } else {
2903 num_rates = RS_INITIAL_LEGACY_NUM_RATES;
2904 num_retries = RS_LEGACY_RETRIES_PER_RATE;
2905 toggle_ant = true;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002906 }
Eyal Shapirae07be6d2013-12-09 13:02:56 +02002907
2908 rs_fill_rates_for_column(mvm, lq_sta, &rate, lq_cmd->rs_table, &index,
2909 num_rates, num_retries, valid_tx_ant,
2910 toggle_ant);
2911
2912 rs_get_lower_rate_down_column(lq_sta, &rate);
2913
2914 if (is_siso(&rate)) {
2915 num_rates = RS_SECONDARY_SISO_NUM_RATES;
2916 num_retries = RS_SECONDARY_SISO_RETRIES;
Eyal Shapirad9088f62014-03-25 10:25:44 +02002917 lq_cmd->mimo_delim = index;
Eyal Shapirae07be6d2013-12-09 13:02:56 +02002918 } else if (is_legacy(&rate)) {
2919 num_rates = RS_SECONDARY_LEGACY_NUM_RATES;
2920 num_retries = RS_LEGACY_RETRIES_PER_RATE;
2921 } else {
2922 WARN_ON_ONCE(1);
2923 }
2924
2925 toggle_ant = true;
2926
2927 rs_fill_rates_for_column(mvm, lq_sta, &rate, lq_cmd->rs_table, &index,
2928 num_rates, num_retries, valid_tx_ant,
2929 toggle_ant);
2930
2931 rs_get_lower_rate_down_column(lq_sta, &rate);
2932
2933 num_rates = RS_SECONDARY_LEGACY_NUM_RATES;
2934 num_retries = RS_LEGACY_RETRIES_PER_RATE;
2935
2936 rs_fill_rates_for_column(mvm, lq_sta, &rate, lq_cmd->rs_table, &index,
2937 num_rates, num_retries, valid_tx_ant,
2938 toggle_ant);
2939
Eyal Shapira8fc7c582013-12-04 02:15:46 +02002940}
2941
2942static void rs_fill_lq_cmd(struct iwl_mvm *mvm,
2943 struct ieee80211_sta *sta,
2944 struct iwl_lq_sta *lq_sta,
2945 const struct rs_rate *initial_rate)
2946{
2947 struct iwl_lq_cmd *lq_cmd = &lq_sta->lq;
Emmanuel Grumbach7ceb98a2014-07-28 12:59:45 +03002948 struct iwl_mvm_sta *mvmsta;
2949 struct iwl_mvm_vif *mvmvif;
2950
2951 lq_cmd->agg_disable_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
2952 lq_cmd->agg_time_limit =
2953 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
Eyal Shapira8fc7c582013-12-04 02:15:46 +02002954
2955#ifdef CONFIG_MAC80211_DEBUGFS
Eliad Pellerae969af2014-06-11 15:51:33 +03002956 if (lq_sta->pers.dbg_fixed_rate) {
Eyal Shapira8fc7c582013-12-04 02:15:46 +02002957 rs_build_rates_table_from_fixed(mvm, lq_cmd,
2958 lq_sta->band,
Eliad Pellerae969af2014-06-11 15:51:33 +03002959 lq_sta->pers.dbg_fixed_rate);
Emmanuel Grumbach7ceb98a2014-07-28 12:59:45 +03002960 return;
2961 }
Eyal Shapira8fc7c582013-12-04 02:15:46 +02002962#endif
Emmanuel Grumbach7ceb98a2014-07-28 12:59:45 +03002963 if (WARN_ON_ONCE(!sta || !initial_rate))
2964 return;
Eyal Shapira8fc7c582013-12-04 02:15:46 +02002965
Eyal Shapirad8bafea2014-10-23 13:58:38 +03002966 rs_build_rates_table(mvm, sta, lq_sta, initial_rate);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002967
Emmanuel Grumbach7ceb98a2014-07-28 12:59:45 +03002968 if (num_of_ant(initial_rate->ant) == 1)
2969 lq_cmd->single_stream_ant_msk = initial_rate->ant;
2970
2971 mvmsta = iwl_mvm_sta_from_mac80211(sta);
2972 mvmvif = iwl_mvm_vif_from_mac80211(mvmsta->vif);
2973
2974 if (num_of_ant(initial_rate->ant) == 1)
2975 lq_cmd->single_stream_ant_msk = initial_rate->ant;
2976
2977 lq_cmd->agg_frame_cnt_limit = mvmsta->max_agg_bufsize;
2978
2979 /*
2980 * In case of low latency, tell the firwmare to leave a frame in the
2981 * Tx Fifo so that it can start a transaction in the same TxOP. This
2982 * basically allows the firmware to send bursts.
2983 */
Eyal Shapira9ecd0512014-08-28 02:21:05 +03002984 if (iwl_mvm_vif_low_latency(mvmvif)) {
Emmanuel Grumbach7ceb98a2014-07-28 12:59:45 +03002985 lq_cmd->agg_frame_cnt_limit--;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002986
Eyal Shapira9ecd0512014-08-28 02:21:05 +03002987 if (mvm->low_latency_agg_frame_limit)
2988 lq_cmd->agg_frame_cnt_limit =
2989 min(lq_cmd->agg_frame_cnt_limit,
2990 mvm->low_latency_agg_frame_limit);
2991 }
2992
Emmanuel Grumbacha4db8482014-07-31 13:58:53 +03002993 if (mvmsta->vif->p2p)
2994 lq_cmd->flags |= LQ_FLAG_USE_RTS_MSK;
2995
Johannes Berg8ca151b2013-01-24 14:25:36 +01002996 lq_cmd->agg_time_limit =
Emmanuel Grumbach5b7ff612014-03-11 19:27:45 +02002997 cpu_to_le16(iwl_mvm_coex_agg_time_limit(mvm, sta));
Johannes Berg8ca151b2013-01-24 14:25:36 +01002998}
2999
3000static void *rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
3001{
3002 return hw->priv;
3003}
3004/* rate scale requires free function to be implemented */
3005static void rs_free(void *mvm_rate)
3006{
3007 return;
3008}
3009
3010static void rs_free_sta(void *mvm_r, struct ieee80211_sta *sta,
3011 void *mvm_sta)
3012{
3013 struct iwl_op_mode *op_mode __maybe_unused = mvm_r;
3014 struct iwl_mvm *mvm __maybe_unused = IWL_OP_MODE_GET_MVM(op_mode);
3015
3016 IWL_DEBUG_RATE(mvm, "enter\n");
3017 IWL_DEBUG_RATE(mvm, "leave\n");
3018}
3019
3020#ifdef CONFIG_MAC80211_DEBUGFS
Eyal Shapira5fc0f762014-01-28 01:35:32 +02003021int rs_pretty_print_rate(char *buf, const u32 rate)
Eyal Shapira5aa33552013-11-23 01:06:36 +02003022{
3023
Eyal Shapira1a61b342013-11-09 02:11:46 +02003024 char *type, *bw;
3025 u8 mcs = 0, nss = 0;
3026 u8 ant = (rate & RATE_MCS_ANT_ABC_MSK) >> RATE_MCS_ANT_POS;
3027
3028 if (!(rate & RATE_MCS_HT_MSK) &&
3029 !(rate & RATE_MCS_VHT_MSK)) {
3030 int index = iwl_hwrate_to_plcp_idx(rate);
3031
3032 return sprintf(buf, "Legacy | ANT: %s Rate: %s Mbps\n",
Eyal Shapira560843f2014-01-05 21:04:19 +02003033 rs_pretty_ant(ant),
3034 index == IWL_RATE_INVALID ? "BAD" :
3035 iwl_rate_mcs[index].mbps);
Eyal Shapira1a61b342013-11-09 02:11:46 +02003036 }
3037
3038 if (rate & RATE_MCS_VHT_MSK) {
3039 type = "VHT";
3040 mcs = rate & RATE_VHT_MCS_RATE_CODE_MSK;
3041 nss = ((rate & RATE_VHT_MCS_NSS_MSK)
3042 >> RATE_VHT_MCS_NSS_POS) + 1;
3043 } else if (rate & RATE_MCS_HT_MSK) {
3044 type = "HT";
3045 mcs = rate & RATE_HT_MCS_INDEX_MSK;
3046 } else {
3047 type = "Unknown"; /* shouldn't happen */
3048 }
3049
3050 switch (rate & RATE_MCS_CHAN_WIDTH_MSK) {
3051 case RATE_MCS_CHAN_WIDTH_20:
3052 bw = "20Mhz";
3053 break;
3054 case RATE_MCS_CHAN_WIDTH_40:
3055 bw = "40Mhz";
3056 break;
3057 case RATE_MCS_CHAN_WIDTH_80:
3058 bw = "80Mhz";
3059 break;
3060 case RATE_MCS_CHAN_WIDTH_160:
3061 bw = "160Mhz";
3062 break;
3063 default:
3064 bw = "BAD BW";
3065 }
3066
3067 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 +02003068 type, rs_pretty_ant(ant), bw, mcs, nss,
Eyal Shapira1a61b342013-11-09 02:11:46 +02003069 (rate & RATE_MCS_SGI_MSK) ? "SGI " : "NGI ",
Emmanuel Grumbach7b1dd042014-02-04 15:32:43 +02003070 (rate & RATE_MCS_HT_STBC_MSK) ? "STBC " : "",
Eyal Shapira1a61b342013-11-09 02:11:46 +02003071 (rate & RATE_MCS_LDPC_MSK) ? "LDPC " : "",
3072 (rate & RATE_MCS_BF_MSK) ? "BF " : "",
3073 (rate & RATE_MCS_ZLF_MSK) ? "ZLF " : "");
3074}
3075
Eyal Shapiraa0c38b22013-12-03 18:34:52 +02003076/**
3077 * Program the device to use fixed rate for frame transmit
3078 * This is for debugging/testing only
3079 * once the device start use fixed rate, we need to reload the module
3080 * to being back the normal operation.
3081 */
3082static void rs_program_fix_rate(struct iwl_mvm *mvm,
3083 struct iwl_lq_sta *lq_sta)
3084{
3085 lq_sta->active_legacy_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
3086 lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
3087 lq_sta->active_mimo2_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
3088
3089 IWL_DEBUG_RATE(mvm, "sta_id %d rate 0x%X\n",
Eliad Pellerae969af2014-06-11 15:51:33 +03003090 lq_sta->lq.sta_id, lq_sta->pers.dbg_fixed_rate);
Eyal Shapiraa0c38b22013-12-03 18:34:52 +02003091
Eliad Pellerae969af2014-06-11 15:51:33 +03003092 if (lq_sta->pers.dbg_fixed_rate) {
Emmanuel Grumbach7ceb98a2014-07-28 12:59:45 +03003093 rs_fill_lq_cmd(mvm, NULL, lq_sta, NULL);
Eliad Pellerae969af2014-06-11 15:51:33 +03003094 iwl_mvm_send_lq_cmd(lq_sta->pers.drv, &lq_sta->lq, false);
Eyal Shapiraa0c38b22013-12-03 18:34:52 +02003095 }
3096}
3097
Emmanuel Grumbach9d108492013-12-03 11:50:30 +02003098static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
3099 const char __user *user_buf, size_t count, loff_t *ppos)
3100{
3101 struct iwl_lq_sta *lq_sta = file->private_data;
3102 struct iwl_mvm *mvm;
3103 char buf[64];
3104 size_t buf_size;
3105 u32 parsed_rate;
3106
Eliad Pellerae969af2014-06-11 15:51:33 +03003107 mvm = lq_sta->pers.drv;
Emmanuel Grumbach9d108492013-12-03 11:50:30 +02003108 memset(buf, 0, sizeof(buf));
3109 buf_size = min(count, sizeof(buf) - 1);
3110 if (copy_from_user(buf, user_buf, buf_size))
3111 return -EFAULT;
3112
3113 if (sscanf(buf, "%x", &parsed_rate) == 1)
Eliad Pellerae969af2014-06-11 15:51:33 +03003114 lq_sta->pers.dbg_fixed_rate = parsed_rate;
Emmanuel Grumbach9d108492013-12-03 11:50:30 +02003115 else
Eliad Pellerae969af2014-06-11 15:51:33 +03003116 lq_sta->pers.dbg_fixed_rate = 0;
Emmanuel Grumbach9d108492013-12-03 11:50:30 +02003117
3118 rs_program_fix_rate(mvm, lq_sta);
3119
3120 return count;
3121}
3122
Johannes Berg8ca151b2013-01-24 14:25:36 +01003123static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
3124 char __user *user_buf, size_t count, loff_t *ppos)
3125{
3126 char *buff;
3127 int desc = 0;
3128 int i = 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +01003129 ssize_t ret;
3130
3131 struct iwl_lq_sta *lq_sta = file->private_data;
3132 struct iwl_mvm *mvm;
3133 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
Eyal Shapira5aa33552013-11-23 01:06:36 +02003134 struct rs_rate *rate = &tbl->rate;
Eliad Pellerae969af2014-06-11 15:51:33 +03003135 mvm = lq_sta->pers.drv;
Eyal Shapira1a61b342013-11-09 02:11:46 +02003136 buff = kmalloc(2048, GFP_KERNEL);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003137 if (!buff)
3138 return -ENOMEM;
3139
3140 desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
Eyal Shapira198266a2014-03-31 22:37:39 +03003141 desc += sprintf(buff+desc, "failed=%d success=%d rate=0%lX\n",
Johannes Berg8ca151b2013-01-24 14:25:36 +01003142 lq_sta->total_failed, lq_sta->total_success,
3143 lq_sta->active_legacy_rate);
3144 desc += sprintf(buff+desc, "fixed rate 0x%X\n",
Eliad Pellerae969af2014-06-11 15:51:33 +03003145 lq_sta->pers.dbg_fixed_rate);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003146 desc += sprintf(buff+desc, "valid_tx_ant %s%s%s\n",
Johannes Berg4ed735e2014-02-12 21:47:44 +01003147 (mvm->fw->valid_tx_ant & ANT_A) ? "ANT_A," : "",
3148 (mvm->fw->valid_tx_ant & ANT_B) ? "ANT_B," : "",
3149 (mvm->fw->valid_tx_ant & ANT_C) ? "ANT_C" : "");
Johannes Berg8ca151b2013-01-24 14:25:36 +01003150 desc += sprintf(buff+desc, "lq type %s\n",
Eyal Shapira5aa33552013-11-23 01:06:36 +02003151 (is_legacy(rate)) ? "legacy" :
3152 is_vht(rate) ? "VHT" : "HT");
3153 if (!is_legacy(rate)) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01003154 desc += sprintf(buff+desc, " %s",
Eyal Shapira5aa33552013-11-23 01:06:36 +02003155 (is_siso(rate)) ? "SISO" : "MIMO2");
Johannes Berg8ca151b2013-01-24 14:25:36 +01003156 desc += sprintf(buff+desc, " %s",
Eyal Shapira5aa33552013-11-23 01:06:36 +02003157 (is_ht20(rate)) ? "20MHz" :
3158 (is_ht40(rate)) ? "40MHz" :
3159 (is_ht80(rate)) ? "80Mhz" : "BAD BW");
Eyal Shapiraa3576ff2014-08-09 10:57:59 +03003160 desc += sprintf(buff+desc, " %s %s %s\n",
Eyal Shapira5aa33552013-11-23 01:06:36 +02003161 (rate->sgi) ? "SGI" : "NGI",
Eyal Shapiraa3576ff2014-08-09 10:57:59 +03003162 (rate->ldpc) ? "LDPC" : "BCC",
Eyal Shapira6a524f42013-08-11 20:27:18 +03003163 (lq_sta->is_agg) ? "AGG on" : "");
Johannes Berg8ca151b2013-01-24 14:25:36 +01003164 }
3165 desc += sprintf(buff+desc, "last tx rate=0x%X\n",
3166 lq_sta->last_rate_n_flags);
3167 desc += sprintf(buff+desc,
Eyal Shapira1a61b342013-11-09 02:11:46 +02003168 "general: flags=0x%X mimo-d=%d s-ant=0x%x d-ant=0x%x\n",
Johannes Berg8ca151b2013-01-24 14:25:36 +01003169 lq_sta->lq.flags,
3170 lq_sta->lq.mimo_delim,
3171 lq_sta->lq.single_stream_ant_msk,
3172 lq_sta->lq.dual_stream_ant_msk);
3173
3174 desc += sprintf(buff+desc,
3175 "agg: time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
3176 le16_to_cpu(lq_sta->lq.agg_time_limit),
3177 lq_sta->lq.agg_disable_start_th,
3178 lq_sta->lq.agg_frame_cnt_limit);
3179
Eliad Peller2fd647f2014-03-13 17:21:36 +02003180 desc += sprintf(buff+desc, "reduced tpc=%d\n", lq_sta->lq.reduced_tpc);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003181 desc += sprintf(buff+desc,
3182 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
3183 lq_sta->lq.initial_rate_index[0],
3184 lq_sta->lq.initial_rate_index[1],
3185 lq_sta->lq.initial_rate_index[2],
3186 lq_sta->lq.initial_rate_index[3]);
3187
3188 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
Johannes Bergcf4ef652013-12-17 11:34:25 +01003189 u32 r = le32_to_cpu(lq_sta->lq.rs_table[i]);
Eyal Shapira1a61b342013-11-09 02:11:46 +02003190
Johannes Bergcf4ef652013-12-17 11:34:25 +01003191 desc += sprintf(buff+desc, " rate[%d] 0x%X ", i, r);
3192 desc += rs_pretty_print_rate(buff+desc, r);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003193 }
3194
3195 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
3196 kfree(buff);
3197 return ret;
3198}
3199
3200static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
3201 .write = rs_sta_dbgfs_scale_table_write,
3202 .read = rs_sta_dbgfs_scale_table_read,
3203 .open = simple_open,
3204 .llseek = default_llseek,
3205};
3206static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
3207 char __user *user_buf, size_t count, loff_t *ppos)
3208{
3209 char *buff;
3210 int desc = 0;
3211 int i, j;
3212 ssize_t ret;
Eyal Shapirad310e402013-08-11 18:43:47 +03003213 struct iwl_scale_tbl_info *tbl;
Eyal Shapira5aa33552013-11-23 01:06:36 +02003214 struct rs_rate *rate;
Johannes Berg8ca151b2013-01-24 14:25:36 +01003215 struct iwl_lq_sta *lq_sta = file->private_data;
3216
3217 buff = kmalloc(1024, GFP_KERNEL);
3218 if (!buff)
3219 return -ENOMEM;
3220
3221 for (i = 0; i < LQ_SIZE; i++) {
Eyal Shapirad310e402013-08-11 18:43:47 +03003222 tbl = &(lq_sta->lq_info[i]);
Eyal Shapira5aa33552013-11-23 01:06:36 +02003223 rate = &tbl->rate;
Johannes Berg8ca151b2013-01-24 14:25:36 +01003224 desc += sprintf(buff+desc,
Eyal Shapira6a524f42013-08-11 20:27:18 +03003225 "%s type=%d SGI=%d BW=%s DUP=0\n"
Eyal Shapira8fc7c582013-12-04 02:15:46 +02003226 "index=%d\n",
Johannes Berg8ca151b2013-01-24 14:25:36 +01003227 lq_sta->active_tbl == i ? "*" : "x",
Eyal Shapira5aa33552013-11-23 01:06:36 +02003228 rate->type,
3229 rate->sgi,
3230 is_ht20(rate) ? "20Mhz" :
3231 is_ht40(rate) ? "40Mhz" :
3232 is_ht80(rate) ? "80Mhz" : "ERR",
Eyal Shapira8fc7c582013-12-04 02:15:46 +02003233 rate->index);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003234 for (j = 0; j < IWL_RATE_COUNT; j++) {
3235 desc += sprintf(buff+desc,
3236 "counter=%d success=%d %%=%d\n",
Eyal Shapirad310e402013-08-11 18:43:47 +03003237 tbl->win[j].counter,
3238 tbl->win[j].success_counter,
3239 tbl->win[j].success_ratio);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003240 }
3241 }
3242 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
3243 kfree(buff);
3244 return ret;
3245}
3246
3247static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
3248 .read = rs_sta_dbgfs_stats_table_read,
3249 .open = simple_open,
3250 .llseek = default_llseek,
3251};
3252
Eyal Shapira757cf23b2014-03-24 02:12:00 +02003253static ssize_t rs_sta_dbgfs_drv_tx_stats_read(struct file *file,
3254 char __user *user_buf,
3255 size_t count, loff_t *ppos)
3256{
3257 static const char * const column_name[] = {
3258 [RS_COLUMN_LEGACY_ANT_A] = "LEGACY_ANT_A",
3259 [RS_COLUMN_LEGACY_ANT_B] = "LEGACY_ANT_B",
3260 [RS_COLUMN_SISO_ANT_A] = "SISO_ANT_A",
3261 [RS_COLUMN_SISO_ANT_B] = "SISO_ANT_B",
3262 [RS_COLUMN_SISO_ANT_A_SGI] = "SISO_ANT_A_SGI",
3263 [RS_COLUMN_SISO_ANT_B_SGI] = "SISO_ANT_B_SGI",
3264 [RS_COLUMN_MIMO2] = "MIMO2",
3265 [RS_COLUMN_MIMO2_SGI] = "MIMO2_SGI",
3266 };
3267
3268 static const char * const rate_name[] = {
3269 [IWL_RATE_1M_INDEX] = "1M",
3270 [IWL_RATE_2M_INDEX] = "2M",
3271 [IWL_RATE_5M_INDEX] = "5.5M",
3272 [IWL_RATE_11M_INDEX] = "11M",
3273 [IWL_RATE_6M_INDEX] = "6M|MCS0",
3274 [IWL_RATE_9M_INDEX] = "9M",
3275 [IWL_RATE_12M_INDEX] = "12M|MCS1",
3276 [IWL_RATE_18M_INDEX] = "18M|MCS2",
3277 [IWL_RATE_24M_INDEX] = "24M|MCS3",
3278 [IWL_RATE_36M_INDEX] = "36M|MCS4",
3279 [IWL_RATE_48M_INDEX] = "48M|MCS5",
3280 [IWL_RATE_54M_INDEX] = "54M|MCS6",
3281 [IWL_RATE_MCS_7_INDEX] = "MCS7",
3282 [IWL_RATE_MCS_8_INDEX] = "MCS8",
3283 [IWL_RATE_MCS_9_INDEX] = "MCS9",
3284 };
3285
3286 char *buff, *pos, *endpos;
3287 int col, rate;
3288 ssize_t ret;
3289 struct iwl_lq_sta *lq_sta = file->private_data;
3290 struct rs_rate_stats *stats;
3291 static const size_t bufsz = 1024;
3292
3293 buff = kmalloc(bufsz, GFP_KERNEL);
3294 if (!buff)
3295 return -ENOMEM;
3296
3297 pos = buff;
3298 endpos = pos + bufsz;
3299
3300 pos += scnprintf(pos, endpos - pos, "COLUMN,");
3301 for (rate = 0; rate < IWL_RATE_COUNT; rate++)
3302 pos += scnprintf(pos, endpos - pos, "%s,", rate_name[rate]);
3303 pos += scnprintf(pos, endpos - pos, "\n");
3304
3305 for (col = 0; col < RS_COLUMN_COUNT; col++) {
3306 pos += scnprintf(pos, endpos - pos,
3307 "%s,", column_name[col]);
3308
3309 for (rate = 0; rate < IWL_RATE_COUNT; rate++) {
Eyal Shapiraf58220f2014-08-16 01:30:30 +03003310 stats = &(lq_sta->pers.tx_stats[col][rate]);
Eyal Shapira757cf23b2014-03-24 02:12:00 +02003311 pos += scnprintf(pos, endpos - pos,
3312 "%llu/%llu,",
3313 stats->success,
3314 stats->total);
3315 }
3316 pos += scnprintf(pos, endpos - pos, "\n");
3317 }
3318
3319 ret = simple_read_from_buffer(user_buf, count, ppos, buff, pos - buff);
3320 kfree(buff);
3321 return ret;
3322}
3323
3324static ssize_t rs_sta_dbgfs_drv_tx_stats_write(struct file *file,
3325 const char __user *user_buf,
3326 size_t count, loff_t *ppos)
3327{
3328 struct iwl_lq_sta *lq_sta = file->private_data;
Eyal Shapiraf58220f2014-08-16 01:30:30 +03003329 memset(lq_sta->pers.tx_stats, 0, sizeof(lq_sta->pers.tx_stats));
Eyal Shapira757cf23b2014-03-24 02:12:00 +02003330
3331 return count;
3332}
3333
3334static const struct file_operations rs_sta_dbgfs_drv_tx_stats_ops = {
3335 .read = rs_sta_dbgfs_drv_tx_stats_read,
3336 .write = rs_sta_dbgfs_drv_tx_stats_write,
3337 .open = simple_open,
3338 .llseek = default_llseek,
3339};
3340
Johannes Berg8ca151b2013-01-24 14:25:36 +01003341static void rs_add_debugfs(void *mvm, void *mvm_sta, struct dentry *dir)
3342{
3343 struct iwl_lq_sta *lq_sta = mvm_sta;
Eliad Pellerc6e1faa2014-06-15 12:02:20 +03003344 debugfs_create_file("rate_scale_table", S_IRUSR | S_IWUSR, dir,
3345 lq_sta, &rs_sta_dbgfs_scale_table_ops);
3346 debugfs_create_file("rate_stats_table", S_IRUSR, dir,
3347 lq_sta, &rs_sta_dbgfs_stats_table_ops);
3348 debugfs_create_file("drv_tx_stats", S_IRUSR | S_IWUSR, dir,
3349 lq_sta, &rs_sta_dbgfs_drv_tx_stats_ops);
3350 debugfs_create_u8("tx_agg_tid_enable", S_IRUSR | S_IWUSR, dir,
3351 &lq_sta->tx_agg_tid_en);
3352 debugfs_create_u8("reduced_tpc", S_IRUSR | S_IWUSR, dir,
3353 &lq_sta->pers.dbg_fixed_txp_reduction);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003354}
3355
3356static void rs_remove_debugfs(void *mvm, void *mvm_sta)
3357{
Johannes Berg8ca151b2013-01-24 14:25:36 +01003358}
3359#endif
3360
3361/*
3362 * Initialization of rate scaling information is done by driver after
3363 * the station is added. Since mac80211 calls this function before a
3364 * station is added we ignore it.
3365 */
3366static void rs_rate_init_stub(void *mvm_r,
Simon Wunderlich3de805c2013-07-08 16:55:50 +02003367 struct ieee80211_supported_band *sband,
3368 struct cfg80211_chan_def *chandef,
3369 struct ieee80211_sta *sta, void *mvm_sta)
Johannes Berg8ca151b2013-01-24 14:25:36 +01003370{
3371}
Johannes Berg631ad702014-01-20 23:29:34 +01003372
3373static const struct rate_control_ops rs_mvm_ops = {
Johannes Berg8ca151b2013-01-24 14:25:36 +01003374 .name = RS_NAME,
Eyal Shapira1ebc8f22014-09-14 15:23:23 +03003375 .tx_status = rs_mac80211_tx_status,
Johannes Berg8ca151b2013-01-24 14:25:36 +01003376 .get_rate = rs_get_rate,
3377 .rate_init = rs_rate_init_stub,
3378 .alloc = rs_alloc,
3379 .free = rs_free,
3380 .alloc_sta = rs_alloc_sta,
3381 .free_sta = rs_free_sta,
Eyal Shapirab87c2172013-11-09 23:37:55 +02003382 .rate_update = rs_rate_update,
Johannes Berg8ca151b2013-01-24 14:25:36 +01003383#ifdef CONFIG_MAC80211_DEBUGFS
3384 .add_sta_debugfs = rs_add_debugfs,
3385 .remove_sta_debugfs = rs_remove_debugfs,
3386#endif
3387};
3388
3389int iwl_mvm_rate_control_register(void)
3390{
3391 return ieee80211_rate_control_register(&rs_mvm_ops);
3392}
3393
3394void iwl_mvm_rate_control_unregister(void)
3395{
3396 ieee80211_rate_control_unregister(&rs_mvm_ops);
3397}
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +03003398
3399/**
3400 * iwl_mvm_tx_protection - Gets LQ command, change it to enable/disable
3401 * Tx protection, according to this rquest and previous requests,
3402 * and send the LQ command.
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +03003403 * @mvmsta: The station
3404 * @enable: Enable Tx protection?
3405 */
Johannes Berge126b5d2013-06-28 13:39:18 +02003406int iwl_mvm_tx_protection(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
3407 bool enable)
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +03003408{
Johannes Berge126b5d2013-06-28 13:39:18 +02003409 struct iwl_lq_cmd *lq = &mvmsta->lq_sta.lq;
3410
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +03003411 lockdep_assert_held(&mvm->mutex);
3412
3413 if (enable) {
3414 if (mvmsta->tx_protection == 0)
Eyal Shapirad307ec82013-11-11 19:56:21 +02003415 lq->flags |= LQ_FLAG_USE_RTS_MSK;
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +03003416 mvmsta->tx_protection++;
3417 } else {
3418 mvmsta->tx_protection--;
3419 if (mvmsta->tx_protection == 0)
Eyal Shapirad307ec82013-11-11 19:56:21 +02003420 lq->flags &= ~LQ_FLAG_USE_RTS_MSK;
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +03003421 }
3422
Eyal Shapira9e680942013-11-09 00:16:16 +02003423 return iwl_mvm_send_lq_cmd(mvm, lq, false);
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +03003424}