]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - drivers/net/wireless/iwlwifi/mvm/rs.c
Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[mirror_ubuntu-zesty-kernel.git] / drivers / net / wireless / iwlwifi / mvm / rs.c
1 /******************************************************************************
2 *
3 * Copyright(c) 2005 - 2014 Intel Corporation. All rights reserved.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
17 *
18 * The full GNU General Public License is included in this distribution in the
19 * file called LICENSE.
20 *
21 * Contact Information:
22 * Intel Linux Wireless <ilw@linux.intel.com>
23 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24 *
25 *****************************************************************************/
26 #include <linux/kernel.h>
27 #include <linux/skbuff.h>
28 #include <linux/slab.h>
29 #include <net/mac80211.h>
30
31 #include <linux/netdevice.h>
32 #include <linux/etherdevice.h>
33 #include <linux/delay.h>
34
35 #include <linux/workqueue.h>
36 #include "rs.h"
37 #include "fw-api.h"
38 #include "sta.h"
39 #include "iwl-op-mode.h"
40 #include "mvm.h"
41
42 #define RS_NAME "iwl-mvm-rs"
43
44 #define NUM_TRY_BEFORE_ANT_TOGGLE 1
45 #define RS_LEGACY_RETRIES_PER_RATE 1
46 #define RS_HT_VHT_RETRIES_PER_RATE 2
47 #define RS_HT_VHT_RETRIES_PER_RATE_TW 1
48 #define RS_INITIAL_MIMO_NUM_RATES 3
49 #define RS_INITIAL_SISO_NUM_RATES 3
50 #define RS_INITIAL_LEGACY_NUM_RATES LINK_QUAL_MAX_RETRY_NUM
51 #define RS_SECONDARY_LEGACY_NUM_RATES LINK_QUAL_MAX_RETRY_NUM
52 #define RS_SECONDARY_SISO_NUM_RATES 3
53 #define RS_SECONDARY_SISO_RETRIES 1
54
55 #define IWL_RATE_MAX_WINDOW 62 /* # tx in history window */
56 #define IWL_RATE_MIN_FAILURE_TH 3 /* min failures to calc tpt */
57 #define IWL_RATE_MIN_SUCCESS_TH 8 /* min successes to calc tpt */
58
59 /* max allowed rate miss before sync LQ cmd */
60 #define IWL_MISSED_RATE_MAX 15
61 #define RS_STAY_IN_COLUMN_TIMEOUT (5*HZ)
62 #define RS_IDLE_TIMEOUT (5*HZ)
63
64 static u8 rs_ht_to_legacy[] = {
65 [IWL_RATE_MCS_0_INDEX] = IWL_RATE_6M_INDEX,
66 [IWL_RATE_MCS_1_INDEX] = IWL_RATE_9M_INDEX,
67 [IWL_RATE_MCS_2_INDEX] = IWL_RATE_12M_INDEX,
68 [IWL_RATE_MCS_3_INDEX] = IWL_RATE_18M_INDEX,
69 [IWL_RATE_MCS_4_INDEX] = IWL_RATE_24M_INDEX,
70 [IWL_RATE_MCS_5_INDEX] = IWL_RATE_36M_INDEX,
71 [IWL_RATE_MCS_6_INDEX] = IWL_RATE_48M_INDEX,
72 [IWL_RATE_MCS_7_INDEX] = IWL_RATE_54M_INDEX,
73 [IWL_RATE_MCS_8_INDEX] = IWL_RATE_54M_INDEX,
74 [IWL_RATE_MCS_9_INDEX] = IWL_RATE_54M_INDEX,
75 };
76
77 static const u8 ant_toggle_lookup[] = {
78 [ANT_NONE] = ANT_NONE,
79 [ANT_A] = ANT_B,
80 [ANT_B] = ANT_C,
81 [ANT_AB] = ANT_BC,
82 [ANT_C] = ANT_A,
83 [ANT_AC] = ANT_AB,
84 [ANT_BC] = ANT_AC,
85 [ANT_ABC] = ANT_ABC,
86 };
87
88 #define IWL_DECLARE_RATE_INFO(r, s, rp, rn) \
89 [IWL_RATE_##r##M_INDEX] = { IWL_RATE_##r##M_PLCP, \
90 IWL_RATE_HT_SISO_MCS_##s##_PLCP, \
91 IWL_RATE_HT_MIMO2_MCS_##s##_PLCP, \
92 IWL_RATE_VHT_SISO_MCS_##s##_PLCP, \
93 IWL_RATE_VHT_MIMO2_MCS_##s##_PLCP,\
94 IWL_RATE_##rp##M_INDEX, \
95 IWL_RATE_##rn##M_INDEX }
96
97 #define IWL_DECLARE_MCS_RATE(s) \
98 [IWL_RATE_MCS_##s##_INDEX] = { IWL_RATE_INVM_PLCP, \
99 IWL_RATE_HT_SISO_MCS_##s##_PLCP, \
100 IWL_RATE_HT_MIMO2_MCS_##s##_PLCP, \
101 IWL_RATE_VHT_SISO_MCS_##s##_PLCP, \
102 IWL_RATE_VHT_MIMO2_MCS_##s##_PLCP, \
103 IWL_RATE_INVM_INDEX, \
104 IWL_RATE_INVM_INDEX }
105
106 /*
107 * Parameter order:
108 * rate, ht rate, prev rate, next rate
109 *
110 * If there isn't a valid next or previous rate then INV is used which
111 * maps to IWL_RATE_INVALID
112 *
113 */
114 static const struct iwl_rs_rate_info iwl_rates[IWL_RATE_COUNT] = {
115 IWL_DECLARE_RATE_INFO(1, INV, INV, 2), /* 1mbps */
116 IWL_DECLARE_RATE_INFO(2, INV, 1, 5), /* 2mbps */
117 IWL_DECLARE_RATE_INFO(5, INV, 2, 11), /*5.5mbps */
118 IWL_DECLARE_RATE_INFO(11, INV, 9, 12), /* 11mbps */
119 IWL_DECLARE_RATE_INFO(6, 0, 5, 11), /* 6mbps ; MCS 0 */
120 IWL_DECLARE_RATE_INFO(9, INV, 6, 11), /* 9mbps */
121 IWL_DECLARE_RATE_INFO(12, 1, 11, 18), /* 12mbps ; MCS 1 */
122 IWL_DECLARE_RATE_INFO(18, 2, 12, 24), /* 18mbps ; MCS 2 */
123 IWL_DECLARE_RATE_INFO(24, 3, 18, 36), /* 24mbps ; MCS 3 */
124 IWL_DECLARE_RATE_INFO(36, 4, 24, 48), /* 36mbps ; MCS 4 */
125 IWL_DECLARE_RATE_INFO(48, 5, 36, 54), /* 48mbps ; MCS 5 */
126 IWL_DECLARE_RATE_INFO(54, 6, 48, INV), /* 54mbps ; MCS 6 */
127 IWL_DECLARE_MCS_RATE(7), /* MCS 7 */
128 IWL_DECLARE_MCS_RATE(8), /* MCS 8 */
129 IWL_DECLARE_MCS_RATE(9), /* MCS 9 */
130 };
131
132 enum rs_action {
133 RS_ACTION_STAY = 0,
134 RS_ACTION_DOWNSCALE = -1,
135 RS_ACTION_UPSCALE = 1,
136 };
137
138 enum rs_column_mode {
139 RS_INVALID = 0,
140 RS_LEGACY,
141 RS_SISO,
142 RS_MIMO2,
143 };
144
145 #define MAX_NEXT_COLUMNS 7
146 #define MAX_COLUMN_CHECKS 3
147
148 typedef bool (*allow_column_func_t) (struct iwl_mvm *mvm,
149 struct ieee80211_sta *sta,
150 struct iwl_scale_tbl_info *tbl);
151
152 struct rs_tx_column {
153 enum rs_column_mode mode;
154 u8 ant;
155 bool sgi;
156 enum rs_column next_columns[MAX_NEXT_COLUMNS];
157 allow_column_func_t checks[MAX_COLUMN_CHECKS];
158 };
159
160 static bool rs_mimo_allow(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
161 struct iwl_scale_tbl_info *tbl)
162 {
163 if (!sta->ht_cap.ht_supported)
164 return false;
165
166 if (sta->smps_mode == IEEE80211_SMPS_STATIC)
167 return false;
168
169 if (num_of_ant(mvm->fw->valid_tx_ant) < 2)
170 return false;
171
172 if (!iwl_mvm_bt_coex_is_mimo_allowed(mvm, sta))
173 return false;
174
175 return true;
176 }
177
178 static bool rs_siso_allow(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
179 struct iwl_scale_tbl_info *tbl)
180 {
181 if (!sta->ht_cap.ht_supported)
182 return false;
183
184 return true;
185 }
186
187 static bool rs_sgi_allow(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
188 struct iwl_scale_tbl_info *tbl)
189 {
190 struct rs_rate *rate = &tbl->rate;
191 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
192 struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
193
194 if (is_ht20(rate) && (ht_cap->cap &
195 IEEE80211_HT_CAP_SGI_20))
196 return true;
197 if (is_ht40(rate) && (ht_cap->cap &
198 IEEE80211_HT_CAP_SGI_40))
199 return true;
200 if (is_ht80(rate) && (vht_cap->cap &
201 IEEE80211_VHT_CAP_SHORT_GI_80))
202 return true;
203
204 return false;
205 }
206
207 static const struct rs_tx_column rs_tx_columns[] = {
208 [RS_COLUMN_LEGACY_ANT_A] = {
209 .mode = RS_LEGACY,
210 .ant = ANT_A,
211 .next_columns = {
212 RS_COLUMN_LEGACY_ANT_B,
213 RS_COLUMN_SISO_ANT_A,
214 RS_COLUMN_SISO_ANT_B,
215 RS_COLUMN_INVALID,
216 RS_COLUMN_INVALID,
217 RS_COLUMN_INVALID,
218 RS_COLUMN_INVALID,
219 },
220 },
221 [RS_COLUMN_LEGACY_ANT_B] = {
222 .mode = RS_LEGACY,
223 .ant = ANT_B,
224 .next_columns = {
225 RS_COLUMN_LEGACY_ANT_A,
226 RS_COLUMN_SISO_ANT_A,
227 RS_COLUMN_SISO_ANT_B,
228 RS_COLUMN_INVALID,
229 RS_COLUMN_INVALID,
230 RS_COLUMN_INVALID,
231 RS_COLUMN_INVALID,
232 },
233 },
234 [RS_COLUMN_SISO_ANT_A] = {
235 .mode = RS_SISO,
236 .ant = ANT_A,
237 .next_columns = {
238 RS_COLUMN_SISO_ANT_B,
239 RS_COLUMN_MIMO2,
240 RS_COLUMN_SISO_ANT_A_SGI,
241 RS_COLUMN_SISO_ANT_B_SGI,
242 RS_COLUMN_LEGACY_ANT_A,
243 RS_COLUMN_LEGACY_ANT_B,
244 RS_COLUMN_INVALID,
245 },
246 .checks = {
247 rs_siso_allow,
248 },
249 },
250 [RS_COLUMN_SISO_ANT_B] = {
251 .mode = RS_SISO,
252 .ant = ANT_B,
253 .next_columns = {
254 RS_COLUMN_SISO_ANT_A,
255 RS_COLUMN_MIMO2,
256 RS_COLUMN_SISO_ANT_B_SGI,
257 RS_COLUMN_SISO_ANT_A_SGI,
258 RS_COLUMN_LEGACY_ANT_A,
259 RS_COLUMN_LEGACY_ANT_B,
260 RS_COLUMN_INVALID,
261 },
262 .checks = {
263 rs_siso_allow,
264 },
265 },
266 [RS_COLUMN_SISO_ANT_A_SGI] = {
267 .mode = RS_SISO,
268 .ant = ANT_A,
269 .sgi = true,
270 .next_columns = {
271 RS_COLUMN_SISO_ANT_B_SGI,
272 RS_COLUMN_MIMO2_SGI,
273 RS_COLUMN_SISO_ANT_A,
274 RS_COLUMN_SISO_ANT_B,
275 RS_COLUMN_MIMO2,
276 RS_COLUMN_LEGACY_ANT_A,
277 RS_COLUMN_LEGACY_ANT_B,
278 },
279 .checks = {
280 rs_siso_allow,
281 rs_sgi_allow,
282 },
283 },
284 [RS_COLUMN_SISO_ANT_B_SGI] = {
285 .mode = RS_SISO,
286 .ant = ANT_B,
287 .sgi = true,
288 .next_columns = {
289 RS_COLUMN_SISO_ANT_A_SGI,
290 RS_COLUMN_MIMO2_SGI,
291 RS_COLUMN_SISO_ANT_B,
292 RS_COLUMN_SISO_ANT_A,
293 RS_COLUMN_MIMO2,
294 RS_COLUMN_LEGACY_ANT_A,
295 RS_COLUMN_LEGACY_ANT_B,
296 },
297 .checks = {
298 rs_siso_allow,
299 rs_sgi_allow,
300 },
301 },
302 [RS_COLUMN_MIMO2] = {
303 .mode = RS_MIMO2,
304 .ant = ANT_AB,
305 .next_columns = {
306 RS_COLUMN_SISO_ANT_A,
307 RS_COLUMN_SISO_ANT_B,
308 RS_COLUMN_SISO_ANT_A_SGI,
309 RS_COLUMN_SISO_ANT_B_SGI,
310 RS_COLUMN_MIMO2_SGI,
311 RS_COLUMN_LEGACY_ANT_A,
312 RS_COLUMN_LEGACY_ANT_B,
313 },
314 .checks = {
315 rs_mimo_allow,
316 },
317 },
318 [RS_COLUMN_MIMO2_SGI] = {
319 .mode = RS_MIMO2,
320 .ant = ANT_AB,
321 .sgi = true,
322 .next_columns = {
323 RS_COLUMN_SISO_ANT_A_SGI,
324 RS_COLUMN_SISO_ANT_B_SGI,
325 RS_COLUMN_SISO_ANT_A,
326 RS_COLUMN_SISO_ANT_B,
327 RS_COLUMN_MIMO2,
328 RS_COLUMN_LEGACY_ANT_A,
329 RS_COLUMN_LEGACY_ANT_B,
330 },
331 .checks = {
332 rs_mimo_allow,
333 rs_sgi_allow,
334 },
335 },
336 };
337
338 static inline u8 rs_extract_rate(u32 rate_n_flags)
339 {
340 /* also works for HT because bits 7:6 are zero there */
341 return (u8)(rate_n_flags & RATE_LEGACY_RATE_MSK);
342 }
343
344 static int iwl_hwrate_to_plcp_idx(u32 rate_n_flags)
345 {
346 int idx = 0;
347
348 if (rate_n_flags & RATE_MCS_HT_MSK) {
349 idx = rate_n_flags & RATE_HT_MCS_RATE_CODE_MSK;
350 idx += IWL_RATE_MCS_0_INDEX;
351
352 /* skip 9M not supported in HT*/
353 if (idx >= IWL_RATE_9M_INDEX)
354 idx += 1;
355 if ((idx >= IWL_FIRST_HT_RATE) && (idx <= IWL_LAST_HT_RATE))
356 return idx;
357 } else if (rate_n_flags & RATE_MCS_VHT_MSK) {
358 idx = rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK;
359 idx += IWL_RATE_MCS_0_INDEX;
360
361 /* skip 9M not supported in VHT*/
362 if (idx >= IWL_RATE_9M_INDEX)
363 idx++;
364 if ((idx >= IWL_FIRST_VHT_RATE) && (idx <= IWL_LAST_VHT_RATE))
365 return idx;
366 } else {
367 /* legacy rate format, search for match in table */
368
369 u8 legacy_rate = rs_extract_rate(rate_n_flags);
370 for (idx = 0; idx < ARRAY_SIZE(iwl_rates); idx++)
371 if (iwl_rates[idx].plcp == legacy_rate)
372 return idx;
373 }
374
375 return IWL_RATE_INVALID;
376 }
377
378 static void rs_rate_scale_perform(struct iwl_mvm *mvm,
379 struct sk_buff *skb,
380 struct ieee80211_sta *sta,
381 struct iwl_lq_sta *lq_sta);
382 static void rs_fill_lq_cmd(struct iwl_mvm *mvm,
383 struct ieee80211_sta *sta,
384 struct iwl_lq_sta *lq_sta,
385 const struct rs_rate *initial_rate);
386 static void rs_stay_in_table(struct iwl_lq_sta *lq_sta, bool force_search);
387
388 /**
389 * The following tables contain the expected throughput metrics for all rates
390 *
391 * 1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
392 *
393 * where invalid entries are zeros.
394 *
395 * CCK rates are only valid in legacy table and will only be used in G
396 * (2.4 GHz) band.
397 */
398
399 static const u16 expected_tpt_legacy[IWL_RATE_COUNT] = {
400 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 0, 0, 0
401 };
402
403 /* Expected TpT tables. 4 indexes:
404 * 0 - NGI, 1 - SGI, 2 - AGG+NGI, 3 - AGG+SGI
405 */
406 static const u16 expected_tpt_siso_20MHz[4][IWL_RATE_COUNT] = {
407 {0, 0, 0, 0, 42, 0, 76, 102, 124, 159, 183, 193, 202, 216, 0},
408 {0, 0, 0, 0, 46, 0, 82, 110, 132, 168, 192, 202, 210, 225, 0},
409 {0, 0, 0, 0, 49, 0, 97, 145, 192, 285, 375, 420, 464, 551, 0},
410 {0, 0, 0, 0, 54, 0, 108, 160, 213, 315, 415, 465, 513, 608, 0},
411 };
412
413 static const u16 expected_tpt_siso_40MHz[4][IWL_RATE_COUNT] = {
414 {0, 0, 0, 0, 77, 0, 127, 160, 184, 220, 242, 250, 257, 269, 275},
415 {0, 0, 0, 0, 83, 0, 135, 169, 193, 229, 250, 257, 264, 275, 280},
416 {0, 0, 0, 0, 101, 0, 199, 295, 389, 570, 744, 828, 911, 1070, 1173},
417 {0, 0, 0, 0, 112, 0, 220, 326, 429, 629, 819, 912, 1000, 1173, 1284},
418 };
419
420 static const u16 expected_tpt_siso_80MHz[4][IWL_RATE_COUNT] = {
421 {0, 0, 0, 0, 130, 0, 191, 223, 244, 273, 288, 294, 298, 305, 308},
422 {0, 0, 0, 0, 138, 0, 200, 231, 251, 279, 293, 298, 302, 308, 312},
423 {0, 0, 0, 0, 217, 0, 429, 634, 834, 1220, 1585, 1760, 1931, 2258, 2466},
424 {0, 0, 0, 0, 241, 0, 475, 701, 921, 1343, 1741, 1931, 2117, 2468, 2691},
425 };
426
427 static const u16 expected_tpt_mimo2_20MHz[4][IWL_RATE_COUNT] = {
428 {0, 0, 0, 0, 74, 0, 123, 155, 179, 213, 235, 243, 250, 261, 0},
429 {0, 0, 0, 0, 81, 0, 131, 164, 187, 221, 242, 250, 256, 267, 0},
430 {0, 0, 0, 0, 98, 0, 193, 286, 375, 550, 718, 799, 878, 1032, 0},
431 {0, 0, 0, 0, 109, 0, 214, 316, 414, 607, 790, 879, 965, 1132, 0},
432 };
433
434 static const u16 expected_tpt_mimo2_40MHz[4][IWL_RATE_COUNT] = {
435 {0, 0, 0, 0, 123, 0, 182, 214, 235, 264, 279, 285, 289, 296, 300},
436 {0, 0, 0, 0, 131, 0, 191, 222, 242, 270, 284, 289, 293, 300, 303},
437 {0, 0, 0, 0, 200, 0, 390, 571, 741, 1067, 1365, 1505, 1640, 1894, 2053},
438 {0, 0, 0, 0, 221, 0, 430, 630, 816, 1169, 1490, 1641, 1784, 2053, 2221},
439 };
440
441 static const u16 expected_tpt_mimo2_80MHz[4][IWL_RATE_COUNT] = {
442 {0, 0, 0, 0, 182, 0, 240, 264, 278, 299, 308, 311, 313, 317, 319},
443 {0, 0, 0, 0, 190, 0, 247, 269, 282, 302, 310, 313, 315, 319, 320},
444 {0, 0, 0, 0, 428, 0, 833, 1215, 1577, 2254, 2863, 3147, 3418, 3913, 4219},
445 {0, 0, 0, 0, 474, 0, 920, 1338, 1732, 2464, 3116, 3418, 3705, 4225, 4545},
446 };
447
448 /* mbps, mcs */
449 static const struct iwl_rate_mcs_info iwl_rate_mcs[IWL_RATE_COUNT] = {
450 { "1", "BPSK DSSS"},
451 { "2", "QPSK DSSS"},
452 {"5.5", "BPSK CCK"},
453 { "11", "QPSK CCK"},
454 { "6", "BPSK 1/2"},
455 { "9", "BPSK 1/2"},
456 { "12", "QPSK 1/2"},
457 { "18", "QPSK 3/4"},
458 { "24", "16QAM 1/2"},
459 { "36", "16QAM 3/4"},
460 { "48", "64QAM 2/3"},
461 { "54", "64QAM 3/4"},
462 { "60", "64QAM 5/6"},
463 };
464
465 #define MCS_INDEX_PER_STREAM (8)
466
467 static const char *rs_pretty_ant(u8 ant)
468 {
469 static const char * const ant_name[] = {
470 [ANT_NONE] = "None",
471 [ANT_A] = "A",
472 [ANT_B] = "B",
473 [ANT_AB] = "AB",
474 [ANT_C] = "C",
475 [ANT_AC] = "AC",
476 [ANT_BC] = "BC",
477 [ANT_ABC] = "ABC",
478 };
479
480 if (ant > ANT_ABC)
481 return "UNKNOWN";
482
483 return ant_name[ant];
484 }
485
486 static const char *rs_pretty_lq_type(enum iwl_table_type type)
487 {
488 static const char * const lq_types[] = {
489 [LQ_NONE] = "NONE",
490 [LQ_LEGACY_A] = "LEGACY_A",
491 [LQ_LEGACY_G] = "LEGACY_G",
492 [LQ_HT_SISO] = "HT SISO",
493 [LQ_HT_MIMO2] = "HT MIMO",
494 [LQ_VHT_SISO] = "VHT SISO",
495 [LQ_VHT_MIMO2] = "VHT MIMO",
496 };
497
498 if (type < LQ_NONE || type >= LQ_MAX)
499 return "UNKNOWN";
500
501 return lq_types[type];
502 }
503
504 static inline void rs_dump_rate(struct iwl_mvm *mvm, const struct rs_rate *rate,
505 const char *prefix)
506 {
507 IWL_DEBUG_RATE(mvm, "%s: (%s: %d) ANT: %s BW: %d SGI: %d\n",
508 prefix, rs_pretty_lq_type(rate->type),
509 rate->index, rs_pretty_ant(rate->ant),
510 rate->bw, rate->sgi);
511 }
512
513 static void rs_rate_scale_clear_window(struct iwl_rate_scale_data *window)
514 {
515 window->data = 0;
516 window->success_counter = 0;
517 window->success_ratio = IWL_INVALID_VALUE;
518 window->counter = 0;
519 window->average_tpt = IWL_INVALID_VALUE;
520 }
521
522 static void rs_rate_scale_clear_tbl_windows(struct iwl_mvm *mvm,
523 struct iwl_scale_tbl_info *tbl)
524 {
525 int i;
526
527 IWL_DEBUG_RATE(mvm, "Clearing up window stats\n");
528 for (i = 0; i < IWL_RATE_COUNT; i++)
529 rs_rate_scale_clear_window(&tbl->win[i]);
530 }
531
532 static inline u8 rs_is_valid_ant(u8 valid_antenna, u8 ant_type)
533 {
534 return (ant_type & valid_antenna) == ant_type;
535 }
536
537 static int rs_tl_turn_on_agg_for_tid(struct iwl_mvm *mvm,
538 struct iwl_lq_sta *lq_data, u8 tid,
539 struct ieee80211_sta *sta)
540 {
541 int ret = -EAGAIN;
542
543 IWL_DEBUG_HT(mvm, "Starting Tx agg: STA: %pM tid: %d\n",
544 sta->addr, tid);
545 ret = ieee80211_start_tx_ba_session(sta, tid, 5000);
546 if (ret == -EAGAIN) {
547 /*
548 * driver and mac80211 is out of sync
549 * this might be cause by reloading firmware
550 * stop the tx ba session here
551 */
552 IWL_ERR(mvm, "Fail start Tx agg on tid: %d\n",
553 tid);
554 ieee80211_stop_tx_ba_session(sta, tid);
555 }
556 return ret;
557 }
558
559 static void rs_tl_turn_on_agg(struct iwl_mvm *mvm, u8 tid,
560 struct iwl_lq_sta *lq_data,
561 struct ieee80211_sta *sta)
562 {
563 if (tid < IWL_MAX_TID_COUNT)
564 rs_tl_turn_on_agg_for_tid(mvm, lq_data, tid, sta);
565 else
566 IWL_ERR(mvm, "tid exceeds max TID count: %d/%d\n",
567 tid, IWL_MAX_TID_COUNT);
568 }
569
570 static inline int get_num_of_ant_from_rate(u32 rate_n_flags)
571 {
572 return !!(rate_n_flags & RATE_MCS_ANT_A_MSK) +
573 !!(rate_n_flags & RATE_MCS_ANT_B_MSK) +
574 !!(rate_n_flags & RATE_MCS_ANT_C_MSK);
575 }
576
577 /*
578 * Static function to get the expected throughput from an iwl_scale_tbl_info
579 * that wraps a NULL pointer check
580 */
581 static s32 get_expected_tpt(struct iwl_scale_tbl_info *tbl, int rs_index)
582 {
583 if (tbl->expected_tpt)
584 return tbl->expected_tpt[rs_index];
585 return 0;
586 }
587
588 /**
589 * rs_collect_tx_data - Update the success/failure sliding window
590 *
591 * We keep a sliding window of the last 62 packets transmitted
592 * at this rate. window->data contains the bitmask of successful
593 * packets.
594 */
595 static int _rs_collect_tx_data(struct iwl_scale_tbl_info *tbl,
596 int scale_index, int attempts, int successes,
597 struct iwl_rate_scale_data *window)
598 {
599 static const u64 mask = (((u64)1) << (IWL_RATE_MAX_WINDOW - 1));
600 s32 fail_count, tpt;
601
602 /* Get expected throughput */
603 tpt = get_expected_tpt(tbl, scale_index);
604
605 /*
606 * Keep track of only the latest 62 tx frame attempts in this rate's
607 * history window; anything older isn't really relevant any more.
608 * If we have filled up the sliding window, drop the oldest attempt;
609 * if the oldest attempt (highest bit in bitmap) shows "success",
610 * subtract "1" from the success counter (this is the main reason
611 * we keep these bitmaps!).
612 */
613 while (attempts > 0) {
614 if (window->counter >= IWL_RATE_MAX_WINDOW) {
615 /* remove earliest */
616 window->counter = IWL_RATE_MAX_WINDOW - 1;
617
618 if (window->data & mask) {
619 window->data &= ~mask;
620 window->success_counter--;
621 }
622 }
623
624 /* Increment frames-attempted counter */
625 window->counter++;
626
627 /* Shift bitmap by one frame to throw away oldest history */
628 window->data <<= 1;
629
630 /* Mark the most recent #successes attempts as successful */
631 if (successes > 0) {
632 window->success_counter++;
633 window->data |= 0x1;
634 successes--;
635 }
636
637 attempts--;
638 }
639
640 /* Calculate current success ratio, avoid divide-by-0! */
641 if (window->counter > 0)
642 window->success_ratio = 128 * (100 * window->success_counter)
643 / window->counter;
644 else
645 window->success_ratio = IWL_INVALID_VALUE;
646
647 fail_count = window->counter - window->success_counter;
648
649 /* Calculate average throughput, if we have enough history. */
650 if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
651 (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
652 window->average_tpt = (window->success_ratio * tpt + 64) / 128;
653 else
654 window->average_tpt = IWL_INVALID_VALUE;
655
656 return 0;
657 }
658
659 static int rs_collect_tx_data(struct iwl_scale_tbl_info *tbl,
660 int scale_index, int attempts, int successes)
661 {
662 struct iwl_rate_scale_data *window = NULL;
663
664 if (scale_index < 0 || scale_index >= IWL_RATE_COUNT)
665 return -EINVAL;
666
667 /* Select window for current tx bit rate */
668 window = &(tbl->win[scale_index]);
669
670 return _rs_collect_tx_data(tbl, scale_index, attempts, successes,
671 window);
672 }
673
674 /* Convert rs_rate object into ucode rate bitmask */
675 static u32 ucode_rate_from_rs_rate(struct iwl_mvm *mvm,
676 struct rs_rate *rate)
677 {
678 u32 ucode_rate = 0;
679 int index = rate->index;
680
681 ucode_rate |= ((rate->ant << RATE_MCS_ANT_POS) &
682 RATE_MCS_ANT_ABC_MSK);
683
684 if (is_legacy(rate)) {
685 ucode_rate |= iwl_rates[index].plcp;
686 if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
687 ucode_rate |= RATE_MCS_CCK_MSK;
688 return ucode_rate;
689 }
690
691 if (is_ht(rate)) {
692 if (index < IWL_FIRST_HT_RATE || index > IWL_LAST_HT_RATE) {
693 IWL_ERR(mvm, "Invalid HT rate index %d\n", index);
694 index = IWL_LAST_HT_RATE;
695 }
696 ucode_rate |= RATE_MCS_HT_MSK;
697
698 if (is_ht_siso(rate))
699 ucode_rate |= iwl_rates[index].plcp_ht_siso;
700 else if (is_ht_mimo2(rate))
701 ucode_rate |= iwl_rates[index].plcp_ht_mimo2;
702 else
703 WARN_ON_ONCE(1);
704 } else if (is_vht(rate)) {
705 if (index < IWL_FIRST_VHT_RATE || index > IWL_LAST_VHT_RATE) {
706 IWL_ERR(mvm, "Invalid VHT rate index %d\n", index);
707 index = IWL_LAST_VHT_RATE;
708 }
709 ucode_rate |= RATE_MCS_VHT_MSK;
710 if (is_vht_siso(rate))
711 ucode_rate |= iwl_rates[index].plcp_vht_siso;
712 else if (is_vht_mimo2(rate))
713 ucode_rate |= iwl_rates[index].plcp_vht_mimo2;
714 else
715 WARN_ON_ONCE(1);
716
717 } else {
718 IWL_ERR(mvm, "Invalid rate->type %d\n", rate->type);
719 }
720
721 ucode_rate |= rate->bw;
722 if (rate->sgi)
723 ucode_rate |= RATE_MCS_SGI_MSK;
724
725 return ucode_rate;
726 }
727
728 /* Convert a ucode rate into an rs_rate object */
729 static int rs_rate_from_ucode_rate(const u32 ucode_rate,
730 enum ieee80211_band band,
731 struct rs_rate *rate)
732 {
733 u32 ant_msk = ucode_rate & RATE_MCS_ANT_ABC_MSK;
734 u8 num_of_ant = get_num_of_ant_from_rate(ucode_rate);
735 u8 nss;
736
737 memset(rate, 0, sizeof(*rate));
738 rate->index = iwl_hwrate_to_plcp_idx(ucode_rate);
739
740 if (rate->index == IWL_RATE_INVALID)
741 return -EINVAL;
742
743 rate->ant = (ant_msk >> RATE_MCS_ANT_POS);
744
745 /* Legacy */
746 if (!(ucode_rate & RATE_MCS_HT_MSK) &&
747 !(ucode_rate & RATE_MCS_VHT_MSK)) {
748 if (num_of_ant == 1) {
749 if (band == IEEE80211_BAND_5GHZ)
750 rate->type = LQ_LEGACY_A;
751 else
752 rate->type = LQ_LEGACY_G;
753 }
754
755 return 0;
756 }
757
758 /* HT or VHT */
759 if (ucode_rate & RATE_MCS_SGI_MSK)
760 rate->sgi = true;
761
762 rate->bw = ucode_rate & RATE_MCS_CHAN_WIDTH_MSK;
763
764 if (ucode_rate & RATE_MCS_HT_MSK) {
765 nss = ((ucode_rate & RATE_HT_MCS_NSS_MSK) >>
766 RATE_HT_MCS_NSS_POS) + 1;
767
768 if (nss == 1) {
769 rate->type = LQ_HT_SISO;
770 WARN_ON_ONCE(num_of_ant != 1);
771 } else if (nss == 2) {
772 rate->type = LQ_HT_MIMO2;
773 WARN_ON_ONCE(num_of_ant != 2);
774 } else {
775 WARN_ON_ONCE(1);
776 }
777 } else if (ucode_rate & RATE_MCS_VHT_MSK) {
778 nss = ((ucode_rate & RATE_VHT_MCS_NSS_MSK) >>
779 RATE_VHT_MCS_NSS_POS) + 1;
780
781 if (nss == 1) {
782 rate->type = LQ_VHT_SISO;
783 WARN_ON_ONCE(num_of_ant != 1);
784 } else if (nss == 2) {
785 rate->type = LQ_VHT_MIMO2;
786 WARN_ON_ONCE(num_of_ant != 2);
787 } else {
788 WARN_ON_ONCE(1);
789 }
790 }
791
792 WARN_ON_ONCE(rate->bw == RATE_MCS_CHAN_WIDTH_160);
793 WARN_ON_ONCE(rate->bw == RATE_MCS_CHAN_WIDTH_80 &&
794 !is_vht(rate));
795
796 return 0;
797 }
798
799 /* switch to another antenna/antennas and return 1 */
800 /* if no other valid antenna found, return 0 */
801 static int rs_toggle_antenna(u32 valid_ant, struct rs_rate *rate)
802 {
803 u8 new_ant_type;
804
805 if (!rate->ant || rate->ant > ANT_ABC)
806 return 0;
807
808 if (!rs_is_valid_ant(valid_ant, rate->ant))
809 return 0;
810
811 new_ant_type = ant_toggle_lookup[rate->ant];
812
813 while ((new_ant_type != rate->ant) &&
814 !rs_is_valid_ant(valid_ant, new_ant_type))
815 new_ant_type = ant_toggle_lookup[new_ant_type];
816
817 if (new_ant_type == rate->ant)
818 return 0;
819
820 rate->ant = new_ant_type;
821
822 return 1;
823 }
824
825 static u16 rs_get_supported_rates(struct iwl_lq_sta *lq_sta,
826 struct rs_rate *rate)
827 {
828 if (is_legacy(rate))
829 return lq_sta->active_legacy_rate;
830 else if (is_siso(rate))
831 return lq_sta->active_siso_rate;
832 else if (is_mimo2(rate))
833 return lq_sta->active_mimo2_rate;
834
835 WARN_ON_ONCE(1);
836 return 0;
837 }
838
839 static u16 rs_get_adjacent_rate(struct iwl_mvm *mvm, u8 index, u16 rate_mask,
840 int rate_type)
841 {
842 u8 high = IWL_RATE_INVALID;
843 u8 low = IWL_RATE_INVALID;
844
845 /* 802.11A or ht walks to the next literal adjacent rate in
846 * the rate table */
847 if (is_type_a_band(rate_type) || !is_type_legacy(rate_type)) {
848 int i;
849 u32 mask;
850
851 /* Find the previous rate that is in the rate mask */
852 i = index - 1;
853 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
854 if (rate_mask & mask) {
855 low = i;
856 break;
857 }
858 }
859
860 /* Find the next rate that is in the rate mask */
861 i = index + 1;
862 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
863 if (rate_mask & mask) {
864 high = i;
865 break;
866 }
867 }
868
869 return (high << 8) | low;
870 }
871
872 low = index;
873 while (low != IWL_RATE_INVALID) {
874 low = iwl_rates[low].prev_rs;
875 if (low == IWL_RATE_INVALID)
876 break;
877 if (rate_mask & (1 << low))
878 break;
879 IWL_DEBUG_RATE(mvm, "Skipping masked lower rate: %d\n", low);
880 }
881
882 high = index;
883 while (high != IWL_RATE_INVALID) {
884 high = iwl_rates[high].next_rs;
885 if (high == IWL_RATE_INVALID)
886 break;
887 if (rate_mask & (1 << high))
888 break;
889 IWL_DEBUG_RATE(mvm, "Skipping masked higher rate: %d\n", high);
890 }
891
892 return (high << 8) | low;
893 }
894
895 static inline bool rs_rate_supported(struct iwl_lq_sta *lq_sta,
896 struct rs_rate *rate)
897 {
898 return BIT(rate->index) & rs_get_supported_rates(lq_sta, rate);
899 }
900
901 /* Get the next supported lower rate in the current column.
902 * Return true if bottom rate in the current column was reached
903 */
904 static bool rs_get_lower_rate_in_column(struct iwl_lq_sta *lq_sta,
905 struct rs_rate *rate)
906 {
907 u8 low;
908 u16 high_low;
909 u16 rate_mask;
910 struct iwl_mvm *mvm = lq_sta->drv;
911
912 rate_mask = rs_get_supported_rates(lq_sta, rate);
913 high_low = rs_get_adjacent_rate(mvm, rate->index, rate_mask,
914 rate->type);
915 low = high_low & 0xff;
916
917 /* Bottom rate of column reached */
918 if (low == IWL_RATE_INVALID)
919 return true;
920
921 rate->index = low;
922 return false;
923 }
924
925 /* Get the next rate to use following a column downgrade */
926 static void rs_get_lower_rate_down_column(struct iwl_lq_sta *lq_sta,
927 struct rs_rate *rate)
928 {
929 struct iwl_mvm *mvm = lq_sta->drv;
930
931 if (is_legacy(rate)) {
932 /* No column to downgrade from Legacy */
933 return;
934 } else if (is_siso(rate)) {
935 /* Downgrade to Legacy if we were in SISO */
936 if (lq_sta->band == IEEE80211_BAND_5GHZ)
937 rate->type = LQ_LEGACY_A;
938 else
939 rate->type = LQ_LEGACY_G;
940
941 rate->bw = RATE_MCS_CHAN_WIDTH_20;
942
943 WARN_ON_ONCE(rate->index < IWL_RATE_MCS_0_INDEX ||
944 rate->index > IWL_RATE_MCS_9_INDEX);
945
946 rate->index = rs_ht_to_legacy[rate->index];
947 } else {
948 /* Downgrade to SISO with same MCS if in MIMO */
949 rate->type = is_vht_mimo2(rate) ?
950 LQ_VHT_SISO : LQ_HT_SISO;
951 }
952
953
954 if (num_of_ant(rate->ant) > 1)
955 rate->ant = first_antenna(mvm->fw->valid_tx_ant);
956
957 /* Relevant in both switching to SISO or Legacy */
958 rate->sgi = false;
959
960 if (!rs_rate_supported(lq_sta, rate))
961 rs_get_lower_rate_in_column(lq_sta, rate);
962 }
963
964 /* Simple function to compare two rate scale table types */
965 static inline bool rs_rate_match(struct rs_rate *a,
966 struct rs_rate *b)
967 {
968 return (a->type == b->type) && (a->ant == b->ant) && (a->sgi == b->sgi);
969 }
970
971 static u32 rs_ch_width_from_mac_flags(enum mac80211_rate_control_flags flags)
972 {
973 if (flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
974 return RATE_MCS_CHAN_WIDTH_40;
975 else if (flags & IEEE80211_TX_RC_80_MHZ_WIDTH)
976 return RATE_MCS_CHAN_WIDTH_80;
977 else if (flags & IEEE80211_TX_RC_160_MHZ_WIDTH)
978 return RATE_MCS_CHAN_WIDTH_160;
979
980 return RATE_MCS_CHAN_WIDTH_20;
981 }
982
983 /*
984 * mac80211 sends us Tx status
985 */
986 static void rs_tx_status(void *mvm_r, struct ieee80211_supported_band *sband,
987 struct ieee80211_sta *sta, void *priv_sta,
988 struct sk_buff *skb)
989 {
990 int legacy_success;
991 int retries;
992 int mac_index, i;
993 struct iwl_lq_sta *lq_sta = priv_sta;
994 struct iwl_lq_cmd *table;
995 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
996 struct iwl_op_mode *op_mode = (struct iwl_op_mode *)mvm_r;
997 struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
998 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
999 enum mac80211_rate_control_flags mac_flags;
1000 u32 ucode_rate;
1001 struct rs_rate rate;
1002 struct iwl_scale_tbl_info *curr_tbl, *other_tbl, *tmp_tbl;
1003
1004 /* Treat uninitialized rate scaling data same as non-existing. */
1005 if (!lq_sta) {
1006 IWL_DEBUG_RATE(mvm, "Station rate scaling not created yet.\n");
1007 return;
1008 } else if (!lq_sta->drv) {
1009 IWL_DEBUG_RATE(mvm, "Rate scaling not initialized yet.\n");
1010 return;
1011 }
1012
1013 #ifdef CPTCFG_MAC80211_DEBUGFS
1014 /* Disable last tx check if we are debugging with fixed rate */
1015 if (lq_sta->dbg_fixed_rate) {
1016 IWL_DEBUG_RATE(mvm, "Fixed rate. avoid rate scaling\n");
1017 return;
1018 }
1019 #endif
1020 if (!ieee80211_is_data(hdr->frame_control) ||
1021 info->flags & IEEE80211_TX_CTL_NO_ACK)
1022 return;
1023
1024 /* This packet was aggregated but doesn't carry status info */
1025 if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
1026 !(info->flags & IEEE80211_TX_STAT_AMPDU))
1027 return;
1028
1029 /*
1030 * Ignore this Tx frame response if its initial rate doesn't match
1031 * that of latest Link Quality command. There may be stragglers
1032 * from a previous Link Quality command, but we're no longer interested
1033 * in those; they're either from the "active" mode while we're trying
1034 * to check "search" mode, or a prior "search" mode after we've moved
1035 * to a new "search" mode (which might become the new "active" mode).
1036 */
1037 table = &lq_sta->lq;
1038 ucode_rate = le32_to_cpu(table->rs_table[0]);
1039 rs_rate_from_ucode_rate(ucode_rate, info->band, &rate);
1040 if (info->band == IEEE80211_BAND_5GHZ)
1041 rate.index -= IWL_FIRST_OFDM_RATE;
1042 mac_flags = info->status.rates[0].flags;
1043 mac_index = info->status.rates[0].idx;
1044 /* For HT packets, map MCS to PLCP */
1045 if (mac_flags & IEEE80211_TX_RC_MCS) {
1046 /* Remove # of streams */
1047 mac_index &= RATE_HT_MCS_RATE_CODE_MSK;
1048 if (mac_index >= (IWL_RATE_9M_INDEX - IWL_FIRST_OFDM_RATE))
1049 mac_index++;
1050 /*
1051 * mac80211 HT index is always zero-indexed; we need to move
1052 * HT OFDM rates after CCK rates in 2.4 GHz band
1053 */
1054 if (info->band == IEEE80211_BAND_2GHZ)
1055 mac_index += IWL_FIRST_OFDM_RATE;
1056 } else if (mac_flags & IEEE80211_TX_RC_VHT_MCS) {
1057 mac_index &= RATE_VHT_MCS_RATE_CODE_MSK;
1058 if (mac_index >= (IWL_RATE_9M_INDEX - IWL_FIRST_OFDM_RATE))
1059 mac_index++;
1060 }
1061
1062 if (time_after(jiffies,
1063 (unsigned long)(lq_sta->last_tx + RS_IDLE_TIMEOUT))) {
1064 int tid;
1065 IWL_DEBUG_RATE(mvm, "Tx idle for too long. reinit rs\n");
1066 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++)
1067 ieee80211_stop_tx_ba_session(sta, tid);
1068
1069 iwl_mvm_rs_rate_init(mvm, sta, sband->band, false);
1070 return;
1071 }
1072 lq_sta->last_tx = jiffies;
1073
1074 /* Here we actually compare this rate to the latest LQ command */
1075 if ((mac_index < 0) ||
1076 (rate.sgi != !!(mac_flags & IEEE80211_TX_RC_SHORT_GI)) ||
1077 (rate.bw != rs_ch_width_from_mac_flags(mac_flags)) ||
1078 (rate.ant != info->status.antenna) ||
1079 (!!(ucode_rate & RATE_MCS_HT_MSK) !=
1080 !!(mac_flags & IEEE80211_TX_RC_MCS)) ||
1081 (!!(ucode_rate & RATE_MCS_VHT_MSK) !=
1082 !!(mac_flags & IEEE80211_TX_RC_VHT_MCS)) ||
1083 (!!(ucode_rate & RATE_HT_MCS_GF_MSK) !=
1084 !!(mac_flags & IEEE80211_TX_RC_GREEN_FIELD)) ||
1085 (rate.index != mac_index)) {
1086 IWL_DEBUG_RATE(mvm,
1087 "initial rate %d does not match %d (0x%x)\n",
1088 mac_index, rate.index, ucode_rate);
1089 /*
1090 * Since rates mis-match, the last LQ command may have failed.
1091 * After IWL_MISSED_RATE_MAX mis-matches, resync the uCode with
1092 * ... driver.
1093 */
1094 lq_sta->missed_rate_counter++;
1095 if (lq_sta->missed_rate_counter > IWL_MISSED_RATE_MAX) {
1096 lq_sta->missed_rate_counter = 0;
1097 IWL_DEBUG_RATE(mvm,
1098 "Too many rates mismatch. Send sync LQ. rs_state %d\n",
1099 lq_sta->rs_state);
1100 iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, false);
1101 }
1102 /* Regardless, ignore this status info for outdated rate */
1103 return;
1104 } else
1105 /* Rate did match, so reset the missed_rate_counter */
1106 lq_sta->missed_rate_counter = 0;
1107
1108 /* Figure out if rate scale algorithm is in active or search table */
1109 if (rs_rate_match(&rate,
1110 &(lq_sta->lq_info[lq_sta->active_tbl].rate))) {
1111 curr_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1112 other_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
1113 } else if (rs_rate_match(&rate,
1114 &lq_sta->lq_info[1 - lq_sta->active_tbl].rate)) {
1115 curr_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
1116 other_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1117 } else {
1118 IWL_DEBUG_RATE(mvm,
1119 "Neither active nor search matches tx rate\n");
1120 tmp_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1121 rs_dump_rate(mvm, &tmp_tbl->rate, "ACTIVE");
1122 tmp_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
1123 rs_dump_rate(mvm, &tmp_tbl->rate, "SEARCH");
1124 rs_dump_rate(mvm, &rate, "ACTUAL");
1125
1126 /*
1127 * no matching table found, let's by-pass the data collection
1128 * and continue to perform rate scale to find the rate table
1129 */
1130 rs_stay_in_table(lq_sta, true);
1131 goto done;
1132 }
1133
1134 /*
1135 * Updating the frame history depends on whether packets were
1136 * aggregated.
1137 *
1138 * For aggregation, all packets were transmitted at the same rate, the
1139 * first index into rate scale table.
1140 */
1141 if (info->flags & IEEE80211_TX_STAT_AMPDU) {
1142 ucode_rate = le32_to_cpu(table->rs_table[0]);
1143 rs_rate_from_ucode_rate(ucode_rate, info->band, &rate);
1144 rs_collect_tx_data(curr_tbl, rate.index,
1145 info->status.ampdu_len,
1146 info->status.ampdu_ack_len);
1147
1148 /* Update success/fail counts if not searching for new mode */
1149 if (lq_sta->rs_state == RS_STATE_STAY_IN_COLUMN) {
1150 lq_sta->total_success += info->status.ampdu_ack_len;
1151 lq_sta->total_failed += (info->status.ampdu_len -
1152 info->status.ampdu_ack_len);
1153 }
1154 } else {
1155 /*
1156 * For legacy, update frame history with for each Tx retry.
1157 */
1158 retries = info->status.rates[0].count - 1;
1159 /* HW doesn't send more than 15 retries */
1160 retries = min(retries, 15);
1161
1162 /* The last transmission may have been successful */
1163 legacy_success = !!(info->flags & IEEE80211_TX_STAT_ACK);
1164 /* Collect data for each rate used during failed TX attempts */
1165 for (i = 0; i <= retries; ++i) {
1166 ucode_rate = le32_to_cpu(table->rs_table[i]);
1167 rs_rate_from_ucode_rate(ucode_rate, info->band, &rate);
1168 /*
1169 * Only collect stats if retried rate is in the same RS
1170 * table as active/search.
1171 */
1172 if (rs_rate_match(&rate, &curr_tbl->rate))
1173 tmp_tbl = curr_tbl;
1174 else if (rs_rate_match(&rate, &other_tbl->rate))
1175 tmp_tbl = other_tbl;
1176 else
1177 continue;
1178
1179 rs_collect_tx_data(tmp_tbl, rate.index, 1,
1180 i < retries ? 0 : legacy_success);
1181 }
1182
1183 /* Update success/fail counts if not searching for new mode */
1184 if (lq_sta->rs_state == RS_STATE_STAY_IN_COLUMN) {
1185 lq_sta->total_success += legacy_success;
1186 lq_sta->total_failed += retries + (1 - legacy_success);
1187 }
1188 }
1189 /* The last TX rate is cached in lq_sta; it's set in if/else above */
1190 lq_sta->last_rate_n_flags = ucode_rate;
1191 done:
1192 /* See if there's a better rate or modulation mode to try. */
1193 if (sta && sta->supp_rates[sband->band])
1194 rs_rate_scale_perform(mvm, skb, sta, lq_sta);
1195 }
1196
1197 /*
1198 * Begin a period of staying with a selected modulation mode.
1199 * Set "stay_in_tbl" flag to prevent any mode switches.
1200 * Set frame tx success limits according to legacy vs. high-throughput,
1201 * and reset overall (spanning all rates) tx success history statistics.
1202 * These control how long we stay using same modulation mode before
1203 * searching for a new mode.
1204 */
1205 static void rs_set_stay_in_table(struct iwl_mvm *mvm, u8 is_legacy,
1206 struct iwl_lq_sta *lq_sta)
1207 {
1208 IWL_DEBUG_RATE(mvm, "Moving to RS_STATE_STAY_IN_COLUMN\n");
1209 lq_sta->rs_state = RS_STATE_STAY_IN_COLUMN;
1210 if (is_legacy) {
1211 lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT;
1212 lq_sta->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
1213 lq_sta->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
1214 } else {
1215 lq_sta->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
1216 lq_sta->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
1217 lq_sta->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
1218 }
1219 lq_sta->table_count = 0;
1220 lq_sta->total_failed = 0;
1221 lq_sta->total_success = 0;
1222 lq_sta->flush_timer = jiffies;
1223 lq_sta->visited_columns = 0;
1224 }
1225
1226 static int rs_get_max_allowed_rate(struct iwl_lq_sta *lq_sta,
1227 const struct rs_tx_column *column)
1228 {
1229 switch (column->mode) {
1230 case RS_LEGACY:
1231 return lq_sta->max_legacy_rate_idx;
1232 case RS_SISO:
1233 return lq_sta->max_siso_rate_idx;
1234 case RS_MIMO2:
1235 return lq_sta->max_mimo2_rate_idx;
1236 default:
1237 WARN_ON_ONCE(1);
1238 }
1239
1240 return lq_sta->max_legacy_rate_idx;
1241 }
1242
1243 static const u16 *rs_get_expected_tpt_table(struct iwl_lq_sta *lq_sta,
1244 const struct rs_tx_column *column,
1245 u32 bw)
1246 {
1247 /* Used to choose among HT tables */
1248 const u16 (*ht_tbl_pointer)[IWL_RATE_COUNT];
1249
1250 if (WARN_ON_ONCE(column->mode != RS_LEGACY &&
1251 column->mode != RS_SISO &&
1252 column->mode != RS_MIMO2))
1253 return expected_tpt_legacy;
1254
1255 /* Legacy rates have only one table */
1256 if (column->mode == RS_LEGACY)
1257 return expected_tpt_legacy;
1258
1259 ht_tbl_pointer = expected_tpt_mimo2_20MHz;
1260 /* Choose among many HT tables depending on number of streams
1261 * (SISO/MIMO2), channel width (20/40/80), SGI, and aggregation
1262 * status */
1263 if (column->mode == RS_SISO) {
1264 switch (bw) {
1265 case RATE_MCS_CHAN_WIDTH_20:
1266 ht_tbl_pointer = expected_tpt_siso_20MHz;
1267 break;
1268 case RATE_MCS_CHAN_WIDTH_40:
1269 ht_tbl_pointer = expected_tpt_siso_40MHz;
1270 break;
1271 case RATE_MCS_CHAN_WIDTH_80:
1272 ht_tbl_pointer = expected_tpt_siso_80MHz;
1273 break;
1274 default:
1275 WARN_ON_ONCE(1);
1276 }
1277 } else if (column->mode == RS_MIMO2) {
1278 switch (bw) {
1279 case RATE_MCS_CHAN_WIDTH_20:
1280 ht_tbl_pointer = expected_tpt_mimo2_20MHz;
1281 break;
1282 case RATE_MCS_CHAN_WIDTH_40:
1283 ht_tbl_pointer = expected_tpt_mimo2_40MHz;
1284 break;
1285 case RATE_MCS_CHAN_WIDTH_80:
1286 ht_tbl_pointer = expected_tpt_mimo2_80MHz;
1287 break;
1288 default:
1289 WARN_ON_ONCE(1);
1290 }
1291 } else {
1292 WARN_ON_ONCE(1);
1293 }
1294
1295 if (!column->sgi && !lq_sta->is_agg) /* Normal */
1296 return ht_tbl_pointer[0];
1297 else if (column->sgi && !lq_sta->is_agg) /* SGI */
1298 return ht_tbl_pointer[1];
1299 else if (!column->sgi && lq_sta->is_agg) /* AGG */
1300 return ht_tbl_pointer[2];
1301 else /* AGG+SGI */
1302 return ht_tbl_pointer[3];
1303 }
1304
1305 static void rs_set_expected_tpt_table(struct iwl_lq_sta *lq_sta,
1306 struct iwl_scale_tbl_info *tbl)
1307 {
1308 struct rs_rate *rate = &tbl->rate;
1309 const struct rs_tx_column *column = &rs_tx_columns[tbl->column];
1310
1311 tbl->expected_tpt = rs_get_expected_tpt_table(lq_sta, column, rate->bw);
1312 }
1313
1314 /*
1315 * Find starting rate for new "search" high-throughput mode of modulation.
1316 * Goal is to find lowest expected rate (under perfect conditions) that is
1317 * above the current measured throughput of "active" mode, to give new mode
1318 * a fair chance to prove itself without too many challenges.
1319 *
1320 * This gets called when transitioning to more aggressive modulation
1321 * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
1322 * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
1323 * to decrease to match "active" throughput. When moving from MIMO to SISO,
1324 * bit rate will typically need to increase, but not if performance was bad.
1325 */
1326 static s32 rs_get_best_rate(struct iwl_mvm *mvm,
1327 struct iwl_lq_sta *lq_sta,
1328 struct iwl_scale_tbl_info *tbl, /* "search" */
1329 u16 rate_mask, s8 index)
1330 {
1331 /* "active" values */
1332 struct iwl_scale_tbl_info *active_tbl =
1333 &(lq_sta->lq_info[lq_sta->active_tbl]);
1334 s32 active_sr = active_tbl->win[index].success_ratio;
1335 s32 active_tpt = active_tbl->expected_tpt[index];
1336 /* expected "search" throughput */
1337 const u16 *tpt_tbl = tbl->expected_tpt;
1338
1339 s32 new_rate, high, low, start_hi;
1340 u16 high_low;
1341 s8 rate = index;
1342
1343 new_rate = high = low = start_hi = IWL_RATE_INVALID;
1344
1345 while (1) {
1346 high_low = rs_get_adjacent_rate(mvm, rate, rate_mask,
1347 tbl->rate.type);
1348
1349 low = high_low & 0xff;
1350 high = (high_low >> 8) & 0xff;
1351
1352 /*
1353 * Lower the "search" bit rate, to give new "search" mode
1354 * approximately the same throughput as "active" if:
1355 *
1356 * 1) "Active" mode has been working modestly well (but not
1357 * great), and expected "search" throughput (under perfect
1358 * conditions) at candidate rate is above the actual
1359 * measured "active" throughput (but less than expected
1360 * "active" throughput under perfect conditions).
1361 * OR
1362 * 2) "Active" mode has been working perfectly or very well
1363 * and expected "search" throughput (under perfect
1364 * conditions) at candidate rate is above expected
1365 * "active" throughput (under perfect conditions).
1366 */
1367 if ((((100 * tpt_tbl[rate]) > lq_sta->last_tpt) &&
1368 ((active_sr > RS_SR_FORCE_DECREASE) &&
1369 (active_sr <= IWL_RATE_HIGH_TH) &&
1370 (tpt_tbl[rate] <= active_tpt))) ||
1371 ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
1372 (tpt_tbl[rate] > active_tpt))) {
1373 /* (2nd or later pass)
1374 * If we've already tried to raise the rate, and are
1375 * now trying to lower it, use the higher rate. */
1376 if (start_hi != IWL_RATE_INVALID) {
1377 new_rate = start_hi;
1378 break;
1379 }
1380
1381 new_rate = rate;
1382
1383 /* Loop again with lower rate */
1384 if (low != IWL_RATE_INVALID)
1385 rate = low;
1386
1387 /* Lower rate not available, use the original */
1388 else
1389 break;
1390
1391 /* Else try to raise the "search" rate to match "active" */
1392 } else {
1393 /* (2nd or later pass)
1394 * If we've already tried to lower the rate, and are
1395 * now trying to raise it, use the lower rate. */
1396 if (new_rate != IWL_RATE_INVALID)
1397 break;
1398
1399 /* Loop again with higher rate */
1400 else if (high != IWL_RATE_INVALID) {
1401 start_hi = high;
1402 rate = high;
1403
1404 /* Higher rate not available, use the original */
1405 } else {
1406 new_rate = rate;
1407 break;
1408 }
1409 }
1410 }
1411
1412 return new_rate;
1413 }
1414
1415 static u32 rs_bw_from_sta_bw(struct ieee80211_sta *sta)
1416 {
1417 if (sta->bandwidth >= IEEE80211_STA_RX_BW_80)
1418 return RATE_MCS_CHAN_WIDTH_80;
1419 else if (sta->bandwidth >= IEEE80211_STA_RX_BW_40)
1420 return RATE_MCS_CHAN_WIDTH_40;
1421
1422 return RATE_MCS_CHAN_WIDTH_20;
1423 }
1424
1425 /*
1426 * Check whether we should continue using same modulation mode, or
1427 * begin search for a new mode, based on:
1428 * 1) # tx successes or failures while using this mode
1429 * 2) # times calling this function
1430 * 3) elapsed time in this mode (not used, for now)
1431 */
1432 static void rs_stay_in_table(struct iwl_lq_sta *lq_sta, bool force_search)
1433 {
1434 struct iwl_scale_tbl_info *tbl;
1435 int active_tbl;
1436 int flush_interval_passed = 0;
1437 struct iwl_mvm *mvm;
1438
1439 mvm = lq_sta->drv;
1440 active_tbl = lq_sta->active_tbl;
1441
1442 tbl = &(lq_sta->lq_info[active_tbl]);
1443
1444 /* If we've been disallowing search, see if we should now allow it */
1445 if (lq_sta->rs_state == RS_STATE_STAY_IN_COLUMN) {
1446 /* Elapsed time using current modulation mode */
1447 if (lq_sta->flush_timer)
1448 flush_interval_passed =
1449 time_after(jiffies,
1450 (unsigned long)(lq_sta->flush_timer +
1451 RS_STAY_IN_COLUMN_TIMEOUT));
1452
1453 /*
1454 * Check if we should allow search for new modulation mode.
1455 * If many frames have failed or succeeded, or we've used
1456 * this same modulation for a long time, allow search, and
1457 * reset history stats that keep track of whether we should
1458 * allow a new search. Also (below) reset all bitmaps and
1459 * stats in active history.
1460 */
1461 if (force_search ||
1462 (lq_sta->total_failed > lq_sta->max_failure_limit) ||
1463 (lq_sta->total_success > lq_sta->max_success_limit) ||
1464 ((!lq_sta->search_better_tbl) &&
1465 (lq_sta->flush_timer) && (flush_interval_passed))) {
1466 IWL_DEBUG_RATE(mvm,
1467 "LQ: stay is expired %d %d %d\n",
1468 lq_sta->total_failed,
1469 lq_sta->total_success,
1470 flush_interval_passed);
1471
1472 /* Allow search for new mode */
1473 lq_sta->rs_state = RS_STATE_SEARCH_CYCLE_STARTED;
1474 IWL_DEBUG_RATE(mvm,
1475 "Moving to RS_STATE_SEARCH_CYCLE_STARTED\n");
1476 lq_sta->total_failed = 0;
1477 lq_sta->total_success = 0;
1478 lq_sta->flush_timer = 0;
1479 /* mark the current column as visited */
1480 lq_sta->visited_columns = BIT(tbl->column);
1481 /*
1482 * Else if we've used this modulation mode enough repetitions
1483 * (regardless of elapsed time or success/failure), reset
1484 * history bitmaps and rate-specific stats for all rates in
1485 * active table.
1486 */
1487 } else {
1488 lq_sta->table_count++;
1489 if (lq_sta->table_count >=
1490 lq_sta->table_count_limit) {
1491 lq_sta->table_count = 0;
1492
1493 IWL_DEBUG_RATE(mvm,
1494 "LQ: stay in table clear win\n");
1495 rs_rate_scale_clear_tbl_windows(mvm, tbl);
1496 }
1497 }
1498
1499 /* If transitioning to allow "search", reset all history
1500 * bitmaps and stats in active table (this will become the new
1501 * "search" table). */
1502 if (lq_sta->rs_state == RS_STATE_SEARCH_CYCLE_STARTED) {
1503 rs_rate_scale_clear_tbl_windows(mvm, tbl);
1504 }
1505 }
1506 }
1507
1508 /*
1509 * setup rate table in uCode
1510 */
1511 static void rs_update_rate_tbl(struct iwl_mvm *mvm,
1512 struct ieee80211_sta *sta,
1513 struct iwl_lq_sta *lq_sta,
1514 struct rs_rate *rate)
1515 {
1516 rs_fill_lq_cmd(mvm, sta, lq_sta, rate);
1517 iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, false);
1518 }
1519
1520 static u8 rs_get_tid(struct iwl_lq_sta *lq_data,
1521 struct ieee80211_hdr *hdr)
1522 {
1523 u8 tid = IWL_MAX_TID_COUNT;
1524
1525 if (ieee80211_is_data_qos(hdr->frame_control)) {
1526 u8 *qc = ieee80211_get_qos_ctl(hdr);
1527 tid = qc[0] & 0xf;
1528 }
1529
1530 if (unlikely(tid > IWL_MAX_TID_COUNT))
1531 tid = IWL_MAX_TID_COUNT;
1532
1533 return tid;
1534 }
1535
1536 static enum rs_column rs_get_next_column(struct iwl_mvm *mvm,
1537 struct iwl_lq_sta *lq_sta,
1538 struct ieee80211_sta *sta,
1539 struct iwl_scale_tbl_info *tbl)
1540 {
1541 int i, j, max_rate;
1542 enum rs_column next_col_id;
1543 const struct rs_tx_column *curr_col = &rs_tx_columns[tbl->column];
1544 const struct rs_tx_column *next_col;
1545 allow_column_func_t allow_func;
1546 u8 valid_ants = mvm->fw->valid_tx_ant;
1547 const u16 *expected_tpt_tbl;
1548 u16 tpt, max_expected_tpt;
1549
1550 for (i = 0; i < MAX_NEXT_COLUMNS; i++) {
1551 next_col_id = curr_col->next_columns[i];
1552
1553 if (next_col_id == RS_COLUMN_INVALID)
1554 continue;
1555
1556 if (lq_sta->visited_columns & BIT(next_col_id)) {
1557 IWL_DEBUG_RATE(mvm, "Skip already visited column %d\n",
1558 next_col_id);
1559 continue;
1560 }
1561
1562 next_col = &rs_tx_columns[next_col_id];
1563
1564 if (!rs_is_valid_ant(valid_ants, next_col->ant)) {
1565 IWL_DEBUG_RATE(mvm,
1566 "Skip column %d as ANT config isn't supported by chip. valid_ants 0x%x column ant 0x%x\n",
1567 next_col_id, valid_ants, next_col->ant);
1568 continue;
1569 }
1570
1571 for (j = 0; j < MAX_COLUMN_CHECKS; j++) {
1572 allow_func = next_col->checks[j];
1573 if (allow_func && !allow_func(mvm, sta, tbl))
1574 break;
1575 }
1576
1577 if (j != MAX_COLUMN_CHECKS) {
1578 IWL_DEBUG_RATE(mvm,
1579 "Skip column %d: not allowed (check %d failed)\n",
1580 next_col_id, j);
1581
1582 continue;
1583 }
1584
1585 tpt = lq_sta->last_tpt / 100;
1586 expected_tpt_tbl = rs_get_expected_tpt_table(lq_sta, next_col,
1587 tbl->rate.bw);
1588 if (WARN_ON_ONCE(!expected_tpt_tbl))
1589 continue;
1590
1591 max_rate = rs_get_max_allowed_rate(lq_sta, next_col);
1592 if (WARN_ON_ONCE(max_rate == IWL_RATE_INVALID))
1593 continue;
1594
1595 max_expected_tpt = expected_tpt_tbl[max_rate];
1596 if (tpt >= max_expected_tpt) {
1597 IWL_DEBUG_RATE(mvm,
1598 "Skip column %d: can't beat current TPT. Max expected %d current %d\n",
1599 next_col_id, max_expected_tpt, tpt);
1600 continue;
1601 }
1602
1603 IWL_DEBUG_RATE(mvm,
1604 "Found potential column %d. Max expected %d current %d\n",
1605 next_col_id, max_expected_tpt, tpt);
1606 break;
1607 }
1608
1609 if (i == MAX_NEXT_COLUMNS)
1610 return RS_COLUMN_INVALID;
1611
1612 return next_col_id;
1613 }
1614
1615 static int rs_switch_to_column(struct iwl_mvm *mvm,
1616 struct iwl_lq_sta *lq_sta,
1617 struct ieee80211_sta *sta,
1618 enum rs_column col_id)
1619 {
1620 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1621 struct iwl_scale_tbl_info *search_tbl =
1622 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1623 struct rs_rate *rate = &search_tbl->rate;
1624 const struct rs_tx_column *column = &rs_tx_columns[col_id];
1625 const struct rs_tx_column *curr_column = &rs_tx_columns[tbl->column];
1626 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1627 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1628 u16 rate_mask = 0;
1629 u32 rate_idx = 0;
1630
1631 memcpy(search_tbl, tbl, sz);
1632
1633 rate->sgi = column->sgi;
1634 rate->ant = column->ant;
1635
1636 if (column->mode == RS_LEGACY) {
1637 if (lq_sta->band == IEEE80211_BAND_5GHZ)
1638 rate->type = LQ_LEGACY_A;
1639 else
1640 rate->type = LQ_LEGACY_G;
1641
1642 rate_mask = lq_sta->active_legacy_rate;
1643 } else if (column->mode == RS_SISO) {
1644 rate->type = lq_sta->is_vht ? LQ_VHT_SISO : LQ_HT_SISO;
1645 rate_mask = lq_sta->active_siso_rate;
1646 } else if (column->mode == RS_MIMO2) {
1647 rate->type = lq_sta->is_vht ? LQ_VHT_MIMO2 : LQ_HT_MIMO2;
1648 rate_mask = lq_sta->active_mimo2_rate;
1649 } else {
1650 WARN_ON_ONCE("Bad column mode");
1651 }
1652
1653 rate->bw = rs_bw_from_sta_bw(sta);
1654 search_tbl->column = col_id;
1655 rs_set_expected_tpt_table(lq_sta, search_tbl);
1656
1657 lq_sta->visited_columns |= BIT(col_id);
1658
1659 /* Get the best matching rate if we're changing modes. e.g.
1660 * SISO->MIMO, LEGACY->SISO, MIMO->SISO
1661 */
1662 if (curr_column->mode != column->mode) {
1663 rate_idx = rs_get_best_rate(mvm, lq_sta, search_tbl,
1664 rate_mask, rate->index);
1665
1666 if ((rate_idx == IWL_RATE_INVALID) ||
1667 !(BIT(rate_idx) & rate_mask)) {
1668 IWL_DEBUG_RATE(mvm,
1669 "can not switch with index %d"
1670 " rate mask %x\n",
1671 rate_idx, rate_mask);
1672
1673 goto err;
1674 }
1675
1676 rate->index = rate_idx;
1677 }
1678
1679 IWL_DEBUG_RATE(mvm, "Switched to column %d: Index %d\n",
1680 col_id, rate->index);
1681
1682 return 0;
1683
1684 err:
1685 rate->type = LQ_NONE;
1686 return -1;
1687 }
1688
1689 static enum rs_action rs_get_rate_action(struct iwl_mvm *mvm,
1690 struct iwl_scale_tbl_info *tbl,
1691 s32 sr, int low, int high,
1692 int current_tpt,
1693 int low_tpt, int high_tpt)
1694 {
1695 enum rs_action action = RS_ACTION_STAY;
1696
1697 if ((sr <= RS_SR_FORCE_DECREASE) || (current_tpt == 0)) {
1698 IWL_DEBUG_RATE(mvm,
1699 "Decrease rate because of low SR\n");
1700 return RS_ACTION_DOWNSCALE;
1701 }
1702
1703 if ((low_tpt == IWL_INVALID_VALUE) &&
1704 (high_tpt == IWL_INVALID_VALUE) &&
1705 (high != IWL_RATE_INVALID)) {
1706 IWL_DEBUG_RATE(mvm,
1707 "No data about high/low rates. Increase rate\n");
1708 return RS_ACTION_UPSCALE;
1709 }
1710
1711 if ((high_tpt == IWL_INVALID_VALUE) &&
1712 (high != IWL_RATE_INVALID) &&
1713 (low_tpt != IWL_INVALID_VALUE) &&
1714 (low_tpt < current_tpt)) {
1715 IWL_DEBUG_RATE(mvm,
1716 "No data about high rate and low rate is worse. Increase rate\n");
1717 return RS_ACTION_UPSCALE;
1718 }
1719
1720 if ((high_tpt != IWL_INVALID_VALUE) &&
1721 (high_tpt > current_tpt)) {
1722 IWL_DEBUG_RATE(mvm,
1723 "Higher rate is better. Increate rate\n");
1724 return RS_ACTION_UPSCALE;
1725 }
1726
1727 if ((low_tpt != IWL_INVALID_VALUE) &&
1728 (high_tpt != IWL_INVALID_VALUE) &&
1729 (low_tpt < current_tpt) &&
1730 (high_tpt < current_tpt)) {
1731 IWL_DEBUG_RATE(mvm,
1732 "Both high and low are worse. Maintain rate\n");
1733 return RS_ACTION_STAY;
1734 }
1735
1736 if ((low_tpt != IWL_INVALID_VALUE) &&
1737 (low_tpt > current_tpt)) {
1738 IWL_DEBUG_RATE(mvm,
1739 "Lower rate is better\n");
1740 action = RS_ACTION_DOWNSCALE;
1741 goto out;
1742 }
1743
1744 if ((low_tpt == IWL_INVALID_VALUE) &&
1745 (low != IWL_RATE_INVALID)) {
1746 IWL_DEBUG_RATE(mvm,
1747 "No data about lower rate\n");
1748 action = RS_ACTION_DOWNSCALE;
1749 goto out;
1750 }
1751
1752 IWL_DEBUG_RATE(mvm, "Maintain rate\n");
1753
1754 out:
1755 if ((action == RS_ACTION_DOWNSCALE) && (low != IWL_RATE_INVALID)) {
1756 if (sr >= RS_SR_NO_DECREASE) {
1757 IWL_DEBUG_RATE(mvm,
1758 "SR is above NO DECREASE. Avoid downscale\n");
1759 action = RS_ACTION_STAY;
1760 } else if (current_tpt > (100 * tbl->expected_tpt[low])) {
1761 IWL_DEBUG_RATE(mvm,
1762 "Current TPT is higher than max expected in low rate. Avoid downscale\n");
1763 action = RS_ACTION_STAY;
1764 } else {
1765 IWL_DEBUG_RATE(mvm, "Decrease rate\n");
1766 }
1767 }
1768
1769 return action;
1770 }
1771
1772 /*
1773 * Do rate scaling and search for new modulation mode.
1774 */
1775 static void rs_rate_scale_perform(struct iwl_mvm *mvm,
1776 struct sk_buff *skb,
1777 struct ieee80211_sta *sta,
1778 struct iwl_lq_sta *lq_sta)
1779 {
1780 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1781 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1782 int low = IWL_RATE_INVALID;
1783 int high = IWL_RATE_INVALID;
1784 int index;
1785 struct iwl_rate_scale_data *window = NULL;
1786 int current_tpt = IWL_INVALID_VALUE;
1787 int low_tpt = IWL_INVALID_VALUE;
1788 int high_tpt = IWL_INVALID_VALUE;
1789 u32 fail_count;
1790 enum rs_action scale_action = RS_ACTION_STAY;
1791 u16 rate_mask;
1792 u8 update_lq = 0;
1793 struct iwl_scale_tbl_info *tbl, *tbl1;
1794 u8 active_tbl = 0;
1795 u8 done_search = 0;
1796 u16 high_low;
1797 s32 sr;
1798 u8 tid = IWL_MAX_TID_COUNT;
1799 u8 prev_agg = lq_sta->is_agg;
1800 struct iwl_mvm_sta *sta_priv = (void *)sta->drv_priv;
1801 struct iwl_mvm_tid_data *tid_data;
1802 struct rs_rate *rate;
1803
1804 /* Send management frames and NO_ACK data using lowest rate. */
1805 /* TODO: this could probably be improved.. */
1806 if (!ieee80211_is_data(hdr->frame_control) ||
1807 info->flags & IEEE80211_TX_CTL_NO_ACK)
1808 return;
1809
1810 tid = rs_get_tid(lq_sta, hdr);
1811 if ((tid != IWL_MAX_TID_COUNT) &&
1812 (lq_sta->tx_agg_tid_en & (1 << tid))) {
1813 tid_data = &sta_priv->tid_data[tid];
1814 if (tid_data->state == IWL_AGG_OFF)
1815 lq_sta->is_agg = 0;
1816 else
1817 lq_sta->is_agg = 1;
1818 } else {
1819 lq_sta->is_agg = 0;
1820 }
1821
1822 /*
1823 * Select rate-scale / modulation-mode table to work with in
1824 * the rest of this function: "search" if searching for better
1825 * modulation mode, or "active" if doing rate scaling within a mode.
1826 */
1827 if (!lq_sta->search_better_tbl)
1828 active_tbl = lq_sta->active_tbl;
1829 else
1830 active_tbl = 1 - lq_sta->active_tbl;
1831
1832 tbl = &(lq_sta->lq_info[active_tbl]);
1833 rate = &tbl->rate;
1834
1835 if (prev_agg != lq_sta->is_agg) {
1836 IWL_DEBUG_RATE(mvm,
1837 "Aggregation changed: prev %d current %d. Update expected TPT table\n",
1838 prev_agg, lq_sta->is_agg);
1839 rs_set_expected_tpt_table(lq_sta, tbl);
1840 rs_rate_scale_clear_tbl_windows(mvm, tbl);
1841 }
1842
1843 /* current tx rate */
1844 index = lq_sta->last_txrate_idx;
1845
1846 /* rates available for this association, and for modulation mode */
1847 rate_mask = rs_get_supported_rates(lq_sta, rate);
1848
1849 if (!(BIT(index) & rate_mask)) {
1850 IWL_ERR(mvm, "Current Rate is not valid\n");
1851 if (lq_sta->search_better_tbl) {
1852 /* revert to active table if search table is not valid*/
1853 rate->type = LQ_NONE;
1854 lq_sta->search_better_tbl = 0;
1855 tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1856 rs_update_rate_tbl(mvm, sta, lq_sta, &tbl->rate);
1857 }
1858 return;
1859 }
1860
1861 /* Get expected throughput table and history window for current rate */
1862 if (!tbl->expected_tpt) {
1863 IWL_ERR(mvm, "tbl->expected_tpt is NULL\n");
1864 return;
1865 }
1866
1867 /* force user max rate if set by user */
1868 if ((lq_sta->max_rate_idx != -1) &&
1869 (lq_sta->max_rate_idx < index)) {
1870 index = lq_sta->max_rate_idx;
1871 update_lq = 1;
1872 window = &(tbl->win[index]);
1873 IWL_DEBUG_RATE(mvm,
1874 "Forcing user max rate %d\n",
1875 index);
1876 goto lq_update;
1877 }
1878
1879 window = &(tbl->win[index]);
1880
1881 /*
1882 * If there is not enough history to calculate actual average
1883 * throughput, keep analyzing results of more tx frames, without
1884 * changing rate or mode (bypass most of the rest of this function).
1885 * Set up new rate table in uCode only if old rate is not supported
1886 * in current association (use new rate found above).
1887 */
1888 fail_count = window->counter - window->success_counter;
1889 if ((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
1890 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH)) {
1891 IWL_DEBUG_RATE(mvm,
1892 "(%s: %d): Test Window: succ %d total %d\n",
1893 rs_pretty_lq_type(rate->type),
1894 index, window->success_counter, window->counter);
1895
1896 /* Can't calculate this yet; not enough history */
1897 window->average_tpt = IWL_INVALID_VALUE;
1898
1899 /* Should we stay with this modulation mode,
1900 * or search for a new one? */
1901 rs_stay_in_table(lq_sta, false);
1902
1903 goto out;
1904 }
1905 /* Else we have enough samples; calculate estimate of
1906 * actual average throughput */
1907 if (window->average_tpt != ((window->success_ratio *
1908 tbl->expected_tpt[index] + 64) / 128)) {
1909 window->average_tpt = ((window->success_ratio *
1910 tbl->expected_tpt[index] + 64) / 128);
1911 }
1912
1913 /* If we are searching for better modulation mode, check success. */
1914 if (lq_sta->search_better_tbl) {
1915 /* If good success, continue using the "search" mode;
1916 * no need to send new link quality command, since we're
1917 * continuing to use the setup that we've been trying. */
1918 if (window->average_tpt > lq_sta->last_tpt) {
1919 IWL_DEBUG_RATE(mvm,
1920 "SWITCHING TO NEW TABLE SR: %d "
1921 "cur-tpt %d old-tpt %d\n",
1922 window->success_ratio,
1923 window->average_tpt,
1924 lq_sta->last_tpt);
1925
1926 /* Swap tables; "search" becomes "active" */
1927 lq_sta->active_tbl = active_tbl;
1928 current_tpt = window->average_tpt;
1929 /* Else poor success; go back to mode in "active" table */
1930 } else {
1931 IWL_DEBUG_RATE(mvm,
1932 "GOING BACK TO THE OLD TABLE: SR %d "
1933 "cur-tpt %d old-tpt %d\n",
1934 window->success_ratio,
1935 window->average_tpt,
1936 lq_sta->last_tpt);
1937
1938 /* Nullify "search" table */
1939 rate->type = LQ_NONE;
1940
1941 /* Revert to "active" table */
1942 active_tbl = lq_sta->active_tbl;
1943 tbl = &(lq_sta->lq_info[active_tbl]);
1944
1945 /* Revert to "active" rate and throughput info */
1946 index = tbl->rate.index;
1947 current_tpt = lq_sta->last_tpt;
1948
1949 /* Need to set up a new rate table in uCode */
1950 update_lq = 1;
1951 }
1952
1953 /* Either way, we've made a decision; modulation mode
1954 * search is done, allow rate adjustment next time. */
1955 lq_sta->search_better_tbl = 0;
1956 done_search = 1; /* Don't switch modes below! */
1957 goto lq_update;
1958 }
1959
1960 /* (Else) not in search of better modulation mode, try for better
1961 * starting rate, while staying in this mode. */
1962 high_low = rs_get_adjacent_rate(mvm, index, rate_mask, rate->type);
1963 low = high_low & 0xff;
1964 high = (high_low >> 8) & 0xff;
1965
1966 /* If user set max rate, dont allow higher than user constrain */
1967 if ((lq_sta->max_rate_idx != -1) &&
1968 (lq_sta->max_rate_idx < high))
1969 high = IWL_RATE_INVALID;
1970
1971 sr = window->success_ratio;
1972
1973 /* Collect measured throughputs for current and adjacent rates */
1974 current_tpt = window->average_tpt;
1975 if (low != IWL_RATE_INVALID)
1976 low_tpt = tbl->win[low].average_tpt;
1977 if (high != IWL_RATE_INVALID)
1978 high_tpt = tbl->win[high].average_tpt;
1979
1980 IWL_DEBUG_RATE(mvm,
1981 "(%s: %d): cur_tpt %d SR %d low %d high %d low_tpt %d high_tpt %d\n",
1982 rs_pretty_lq_type(rate->type), index, current_tpt, sr,
1983 low, high, low_tpt, high_tpt);
1984
1985 scale_action = rs_get_rate_action(mvm, tbl, sr, low, high,
1986 current_tpt, low_tpt, high_tpt);
1987
1988 /* Force a search in case BT doesn't like us being in MIMO */
1989 if (is_mimo(rate) &&
1990 !iwl_mvm_bt_coex_is_mimo_allowed(mvm, sta)) {
1991 IWL_DEBUG_RATE(mvm,
1992 "BT Coex forbids MIMO. Search for new config\n");
1993 rs_stay_in_table(lq_sta, true);
1994 goto lq_update;
1995 }
1996
1997 switch (scale_action) {
1998 case RS_ACTION_DOWNSCALE:
1999 /* Decrease starting rate, update uCode's rate table */
2000 if (low != IWL_RATE_INVALID) {
2001 update_lq = 1;
2002 index = low;
2003 } else {
2004 IWL_DEBUG_RATE(mvm,
2005 "At the bottom rate. Can't decrease\n");
2006 }
2007
2008 break;
2009 case RS_ACTION_UPSCALE:
2010 /* Increase starting rate, update uCode's rate table */
2011 if (high != IWL_RATE_INVALID) {
2012 update_lq = 1;
2013 index = high;
2014 } else {
2015 IWL_DEBUG_RATE(mvm,
2016 "At the top rate. Can't increase\n");
2017 }
2018
2019 break;
2020 case RS_ACTION_STAY:
2021 /* No change */
2022 default:
2023 break;
2024 }
2025
2026 lq_update:
2027 /* Replace uCode's rate table for the destination station. */
2028 if (update_lq) {
2029 tbl->rate.index = index;
2030 rs_update_rate_tbl(mvm, sta, lq_sta, &tbl->rate);
2031 }
2032
2033 rs_stay_in_table(lq_sta, false);
2034
2035 /*
2036 * Search for new modulation mode if we're:
2037 * 1) Not changing rates right now
2038 * 2) Not just finishing up a search
2039 * 3) Allowing a new search
2040 */
2041 if (!update_lq && !done_search &&
2042 lq_sta->rs_state == RS_STATE_SEARCH_CYCLE_STARTED
2043 && window->counter) {
2044 enum rs_column next_column;
2045
2046 /* Save current throughput to compare with "search" throughput*/
2047 lq_sta->last_tpt = current_tpt;
2048
2049 IWL_DEBUG_RATE(mvm,
2050 "Start Search: update_lq %d done_search %d rs_state %d win->counter %d\n",
2051 update_lq, done_search, lq_sta->rs_state,
2052 window->counter);
2053
2054 next_column = rs_get_next_column(mvm, lq_sta, sta, tbl);
2055 if (next_column != RS_COLUMN_INVALID) {
2056 int ret = rs_switch_to_column(mvm, lq_sta, sta,
2057 next_column);
2058 if (!ret)
2059 lq_sta->search_better_tbl = 1;
2060 } else {
2061 IWL_DEBUG_RATE(mvm,
2062 "No more columns to explore in search cycle. Go to RS_STATE_SEARCH_CYCLE_ENDED\n");
2063 lq_sta->rs_state = RS_STATE_SEARCH_CYCLE_ENDED;
2064 }
2065
2066 /* If new "search" mode was selected, set up in uCode table */
2067 if (lq_sta->search_better_tbl) {
2068 /* Access the "search" table, clear its history. */
2069 tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
2070 rs_rate_scale_clear_tbl_windows(mvm, tbl);
2071
2072 /* Use new "search" start rate */
2073 index = tbl->rate.index;
2074
2075 rs_dump_rate(mvm, &tbl->rate,
2076 "Switch to SEARCH TABLE:");
2077 rs_fill_lq_cmd(mvm, sta, lq_sta, &tbl->rate);
2078 iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, false);
2079 } else {
2080 done_search = 1;
2081 }
2082 }
2083
2084 if (done_search && lq_sta->rs_state == RS_STATE_SEARCH_CYCLE_ENDED) {
2085 /* If the "active" (non-search) mode was legacy,
2086 * and we've tried switching antennas,
2087 * but we haven't been able to try HT modes (not available),
2088 * stay with best antenna legacy modulation for a while
2089 * before next round of mode comparisons. */
2090 tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
2091 if (is_legacy(&tbl1->rate)) {
2092 IWL_DEBUG_RATE(mvm, "LQ: STAY in legacy table\n");
2093
2094 if (tid != IWL_MAX_TID_COUNT) {
2095 tid_data = &sta_priv->tid_data[tid];
2096 if (tid_data->state != IWL_AGG_OFF) {
2097 IWL_DEBUG_RATE(mvm,
2098 "Stop aggregation on tid %d\n",
2099 tid);
2100 ieee80211_stop_tx_ba_session(sta, tid);
2101 }
2102 }
2103 rs_set_stay_in_table(mvm, 1, lq_sta);
2104 } else {
2105 /* If we're in an HT mode, and all 3 mode switch actions
2106 * have been tried and compared, stay in this best modulation
2107 * mode for a while before next round of mode comparisons. */
2108 if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
2109 (lq_sta->tx_agg_tid_en & (1 << tid)) &&
2110 (tid != IWL_MAX_TID_COUNT)) {
2111 tid_data = &sta_priv->tid_data[tid];
2112 if (tid_data->state == IWL_AGG_OFF) {
2113 IWL_DEBUG_RATE(mvm,
2114 "try to aggregate tid %d\n",
2115 tid);
2116 rs_tl_turn_on_agg(mvm, tid,
2117 lq_sta, sta);
2118 }
2119 }
2120 rs_set_stay_in_table(mvm, 0, lq_sta);
2121 }
2122 }
2123
2124 out:
2125 lq_sta->last_txrate_idx = index;
2126 }
2127
2128 /**
2129 * rs_initialize_lq - Initialize a station's hardware rate table
2130 *
2131 * The uCode's station table contains a table of fallback rates
2132 * for automatic fallback during transmission.
2133 *
2134 * NOTE: This sets up a default set of values. These will be replaced later
2135 * if the driver's iwl-agn-rs rate scaling algorithm is used, instead of
2136 * rc80211_simple.
2137 *
2138 * NOTE: Run REPLY_ADD_STA command to set up station table entry, before
2139 * calling this function (which runs REPLY_TX_LINK_QUALITY_CMD,
2140 * which requires station table entry to exist).
2141 */
2142 static void rs_initialize_lq(struct iwl_mvm *mvm,
2143 struct ieee80211_sta *sta,
2144 struct iwl_lq_sta *lq_sta,
2145 enum ieee80211_band band,
2146 bool init)
2147 {
2148 struct iwl_scale_tbl_info *tbl;
2149 struct rs_rate *rate;
2150 int i;
2151 u8 active_tbl = 0;
2152 u8 valid_tx_ant;
2153
2154 if (!sta || !lq_sta)
2155 return;
2156
2157 i = lq_sta->last_txrate_idx;
2158
2159 valid_tx_ant = mvm->fw->valid_tx_ant;
2160
2161 if (!lq_sta->search_better_tbl)
2162 active_tbl = lq_sta->active_tbl;
2163 else
2164 active_tbl = 1 - lq_sta->active_tbl;
2165
2166 tbl = &(lq_sta->lq_info[active_tbl]);
2167 rate = &tbl->rate;
2168
2169 if ((i < 0) || (i >= IWL_RATE_COUNT))
2170 i = 0;
2171
2172 rate->index = i;
2173 rate->ant = first_antenna(valid_tx_ant);
2174 rate->sgi = false;
2175 rate->bw = RATE_MCS_CHAN_WIDTH_20;
2176 if (band == IEEE80211_BAND_5GHZ)
2177 rate->type = LQ_LEGACY_A;
2178 else
2179 rate->type = LQ_LEGACY_G;
2180
2181 WARN_ON_ONCE(rate->ant != ANT_A && rate->ant != ANT_B);
2182 if (rate->ant == ANT_A)
2183 tbl->column = RS_COLUMN_LEGACY_ANT_A;
2184 else
2185 tbl->column = RS_COLUMN_LEGACY_ANT_B;
2186
2187 rs_set_expected_tpt_table(lq_sta, tbl);
2188 rs_fill_lq_cmd(mvm, sta, lq_sta, rate);
2189 /* TODO restore station should remember the lq cmd */
2190 iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, init);
2191 }
2192
2193 static void rs_get_rate(void *mvm_r, struct ieee80211_sta *sta, void *mvm_sta,
2194 struct ieee80211_tx_rate_control *txrc)
2195 {
2196 struct sk_buff *skb = txrc->skb;
2197 struct ieee80211_supported_band *sband = txrc->sband;
2198 struct iwl_op_mode *op_mode __maybe_unused =
2199 (struct iwl_op_mode *)mvm_r;
2200 struct iwl_mvm *mvm __maybe_unused = IWL_OP_MODE_GET_MVM(op_mode);
2201 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2202 struct iwl_lq_sta *lq_sta = mvm_sta;
2203
2204 /* Get max rate if user set max rate */
2205 if (lq_sta) {
2206 lq_sta->max_rate_idx = txrc->max_rate_idx;
2207 if ((sband->band == IEEE80211_BAND_5GHZ) &&
2208 (lq_sta->max_rate_idx != -1))
2209 lq_sta->max_rate_idx += IWL_FIRST_OFDM_RATE;
2210 if ((lq_sta->max_rate_idx < 0) ||
2211 (lq_sta->max_rate_idx >= IWL_RATE_COUNT))
2212 lq_sta->max_rate_idx = -1;
2213 }
2214
2215 /* Treat uninitialized rate scaling data same as non-existing. */
2216 if (lq_sta && !lq_sta->drv) {
2217 IWL_DEBUG_RATE(mvm, "Rate scaling not initialized yet.\n");
2218 mvm_sta = NULL;
2219 }
2220
2221 /* Send management frames and NO_ACK data using lowest rate. */
2222 if (rate_control_send_low(sta, mvm_sta, txrc))
2223 return;
2224
2225 iwl_mvm_hwrate_to_tx_rate(lq_sta->last_rate_n_flags,
2226 info->band, &info->control.rates[0]);
2227
2228 info->control.rates[0].count = 1;
2229 }
2230
2231 static void *rs_alloc_sta(void *mvm_rate, struct ieee80211_sta *sta,
2232 gfp_t gfp)
2233 {
2234 struct iwl_mvm_sta *sta_priv = (struct iwl_mvm_sta *)sta->drv_priv;
2235 struct iwl_op_mode *op_mode __maybe_unused =
2236 (struct iwl_op_mode *)mvm_rate;
2237 struct iwl_mvm *mvm __maybe_unused = IWL_OP_MODE_GET_MVM(op_mode);
2238
2239 IWL_DEBUG_RATE(mvm, "create station rate scale window\n");
2240
2241 return &sta_priv->lq_sta;
2242 }
2243
2244 static int rs_vht_highest_rx_mcs_index(struct ieee80211_sta_vht_cap *vht_cap,
2245 int nss)
2246 {
2247 u16 rx_mcs = le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map) &
2248 (0x3 << (2 * (nss - 1)));
2249 rx_mcs >>= (2 * (nss - 1));
2250
2251 if (rx_mcs == IEEE80211_VHT_MCS_SUPPORT_0_7)
2252 return IWL_RATE_MCS_7_INDEX;
2253 else if (rx_mcs == IEEE80211_VHT_MCS_SUPPORT_0_8)
2254 return IWL_RATE_MCS_8_INDEX;
2255 else if (rx_mcs == IEEE80211_VHT_MCS_SUPPORT_0_9)
2256 return IWL_RATE_MCS_9_INDEX;
2257
2258 WARN_ON_ONCE(rx_mcs != IEEE80211_VHT_MCS_NOT_SUPPORTED);
2259 return -1;
2260 }
2261
2262 static void rs_vht_set_enabled_rates(struct ieee80211_sta *sta,
2263 struct ieee80211_sta_vht_cap *vht_cap,
2264 struct iwl_lq_sta *lq_sta)
2265 {
2266 int i;
2267 int highest_mcs = rs_vht_highest_rx_mcs_index(vht_cap, 1);
2268
2269 if (highest_mcs >= IWL_RATE_MCS_0_INDEX) {
2270 for (i = IWL_RATE_MCS_0_INDEX; i <= highest_mcs; i++) {
2271 if (i == IWL_RATE_9M_INDEX)
2272 continue;
2273
2274 /* Disable MCS9 as a workaround */
2275 if (i == IWL_RATE_MCS_9_INDEX)
2276 continue;
2277
2278 /* VHT MCS9 isn't valid for 20Mhz for NSS=1,2 */
2279 if (i == IWL_RATE_MCS_9_INDEX &&
2280 sta->bandwidth == IEEE80211_STA_RX_BW_20)
2281 continue;
2282
2283 lq_sta->active_siso_rate |= BIT(i);
2284 }
2285 }
2286
2287 if (sta->rx_nss < 2)
2288 return;
2289
2290 highest_mcs = rs_vht_highest_rx_mcs_index(vht_cap, 2);
2291 if (highest_mcs >= IWL_RATE_MCS_0_INDEX) {
2292 for (i = IWL_RATE_MCS_0_INDEX; i <= highest_mcs; i++) {
2293 if (i == IWL_RATE_9M_INDEX)
2294 continue;
2295
2296 /* Disable MCS9 as a workaround */
2297 if (i == IWL_RATE_MCS_9_INDEX)
2298 continue;
2299
2300 /* VHT MCS9 isn't valid for 20Mhz for NSS=1,2 */
2301 if (i == IWL_RATE_MCS_9_INDEX &&
2302 sta->bandwidth == IEEE80211_STA_RX_BW_20)
2303 continue;
2304
2305 lq_sta->active_mimo2_rate |= BIT(i);
2306 }
2307 }
2308 }
2309
2310 #ifdef CONFIG_IWLWIFI_DEBUGFS
2311 static void iwl_mvm_reset_frame_stats(struct iwl_mvm *mvm,
2312 struct iwl_mvm_frame_stats *stats)
2313 {
2314 spin_lock_bh(&mvm->drv_stats_lock);
2315 memset(stats, 0, sizeof(*stats));
2316 spin_unlock_bh(&mvm->drv_stats_lock);
2317 }
2318
2319 void iwl_mvm_update_frame_stats(struct iwl_mvm *mvm,
2320 struct iwl_mvm_frame_stats *stats,
2321 u32 rate, bool agg)
2322 {
2323 u8 nss = 0, mcs = 0;
2324
2325 spin_lock(&mvm->drv_stats_lock);
2326
2327 if (agg)
2328 stats->agg_frames++;
2329
2330 stats->success_frames++;
2331
2332 switch (rate & RATE_MCS_CHAN_WIDTH_MSK) {
2333 case RATE_MCS_CHAN_WIDTH_20:
2334 stats->bw_20_frames++;
2335 break;
2336 case RATE_MCS_CHAN_WIDTH_40:
2337 stats->bw_40_frames++;
2338 break;
2339 case RATE_MCS_CHAN_WIDTH_80:
2340 stats->bw_80_frames++;
2341 break;
2342 default:
2343 WARN_ONCE(1, "bad BW. rate 0x%x", rate);
2344 }
2345
2346 if (rate & RATE_MCS_HT_MSK) {
2347 stats->ht_frames++;
2348 mcs = rate & RATE_HT_MCS_RATE_CODE_MSK;
2349 nss = ((rate & RATE_HT_MCS_NSS_MSK) >> RATE_HT_MCS_NSS_POS) + 1;
2350 } else if (rate & RATE_MCS_VHT_MSK) {
2351 stats->vht_frames++;
2352 mcs = rate & RATE_VHT_MCS_RATE_CODE_MSK;
2353 nss = ((rate & RATE_VHT_MCS_NSS_MSK) >>
2354 RATE_VHT_MCS_NSS_POS) + 1;
2355 } else {
2356 stats->legacy_frames++;
2357 }
2358
2359 if (nss == 1)
2360 stats->siso_frames++;
2361 else if (nss == 2)
2362 stats->mimo2_frames++;
2363
2364 if (rate & RATE_MCS_SGI_MSK)
2365 stats->sgi_frames++;
2366 else
2367 stats->ngi_frames++;
2368
2369 stats->last_rates[stats->last_frame_idx] = rate;
2370 stats->last_frame_idx = (stats->last_frame_idx + 1) %
2371 ARRAY_SIZE(stats->last_rates);
2372
2373 spin_unlock(&mvm->drv_stats_lock);
2374 }
2375 #endif
2376
2377 /*
2378 * Called after adding a new station to initialize rate scaling
2379 */
2380 void iwl_mvm_rs_rate_init(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
2381 enum ieee80211_band band, bool init)
2382 {
2383 int i, j;
2384 struct ieee80211_hw *hw = mvm->hw;
2385 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
2386 struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
2387 struct iwl_mvm_sta *sta_priv;
2388 struct iwl_lq_sta *lq_sta;
2389 struct ieee80211_supported_band *sband;
2390 unsigned long supp; /* must be unsigned long for for_each_set_bit */
2391
2392 sta_priv = (struct iwl_mvm_sta *)sta->drv_priv;
2393 lq_sta = &sta_priv->lq_sta;
2394 memset(lq_sta, 0, sizeof(*lq_sta));
2395
2396 sband = hw->wiphy->bands[band];
2397
2398 lq_sta->lq.sta_id = sta_priv->sta_id;
2399
2400 for (j = 0; j < LQ_SIZE; j++)
2401 rs_rate_scale_clear_tbl_windows(mvm, &lq_sta->lq_info[j]);
2402
2403 lq_sta->flush_timer = 0;
2404 lq_sta->last_tx = jiffies;
2405
2406 IWL_DEBUG_RATE(mvm,
2407 "LQ: *** rate scale station global init for station %d ***\n",
2408 sta_priv->sta_id);
2409 /* TODO: what is a good starting rate for STA? About middle? Maybe not
2410 * the lowest or the highest rate.. Could consider using RSSI from
2411 * previous packets? Need to have IEEE 802.1X auth succeed immediately
2412 * after assoc.. */
2413
2414 lq_sta->max_rate_idx = -1;
2415 lq_sta->missed_rate_counter = IWL_MISSED_RATE_MAX;
2416 lq_sta->band = sband->band;
2417 /*
2418 * active legacy rates as per supported rates bitmap
2419 */
2420 supp = sta->supp_rates[sband->band];
2421 lq_sta->active_legacy_rate = 0;
2422 for_each_set_bit(i, &supp, BITS_PER_LONG)
2423 lq_sta->active_legacy_rate |= BIT(sband->bitrates[i].hw_value);
2424
2425 /* TODO: should probably account for rx_highest for both HT/VHT */
2426 if (!vht_cap || !vht_cap->vht_supported) {
2427 /* active_siso_rate mask includes 9 MBits (bit 5),
2428 * and CCK (bits 0-3), supp_rates[] does not;
2429 * shift to convert format, force 9 MBits off.
2430 */
2431 lq_sta->active_siso_rate = ht_cap->mcs.rx_mask[0] << 1;
2432 lq_sta->active_siso_rate |= ht_cap->mcs.rx_mask[0] & 0x1;
2433 lq_sta->active_siso_rate &= ~((u16)0x2);
2434 lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE;
2435
2436 /* Same here */
2437 lq_sta->active_mimo2_rate = ht_cap->mcs.rx_mask[1] << 1;
2438 lq_sta->active_mimo2_rate |= ht_cap->mcs.rx_mask[1] & 0x1;
2439 lq_sta->active_mimo2_rate &= ~((u16)0x2);
2440 lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE;
2441
2442 lq_sta->is_vht = false;
2443 } else {
2444 rs_vht_set_enabled_rates(sta, vht_cap, lq_sta);
2445 lq_sta->is_vht = true;
2446 }
2447
2448 lq_sta->max_legacy_rate_idx = find_last_bit(&lq_sta->active_legacy_rate,
2449 BITS_PER_LONG);
2450 lq_sta->max_siso_rate_idx = find_last_bit(&lq_sta->active_siso_rate,
2451 BITS_PER_LONG);
2452 lq_sta->max_mimo2_rate_idx = find_last_bit(&lq_sta->active_mimo2_rate,
2453 BITS_PER_LONG);
2454
2455 IWL_DEBUG_RATE(mvm, "RATE MASK: LEGACY=%lX SISO=%lX MIMO2=%lX VHT=%d\n",
2456 lq_sta->active_legacy_rate,
2457 lq_sta->active_siso_rate,
2458 lq_sta->active_mimo2_rate,
2459 lq_sta->is_vht);
2460 IWL_DEBUG_RATE(mvm, "MAX RATE: LEGACY=%d SISO=%d MIMO2=%d\n",
2461 lq_sta->max_legacy_rate_idx,
2462 lq_sta->max_siso_rate_idx,
2463 lq_sta->max_mimo2_rate_idx);
2464
2465 /* These values will be overridden later */
2466 lq_sta->lq.single_stream_ant_msk =
2467 first_antenna(mvm->fw->valid_tx_ant);
2468 lq_sta->lq.dual_stream_ant_msk = ANT_AB;
2469
2470 /* as default allow aggregation for all tids */
2471 lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
2472 lq_sta->drv = mvm;
2473
2474 /* Set last_txrate_idx to lowest rate */
2475 lq_sta->last_txrate_idx = rate_lowest_index(sband, sta);
2476 if (sband->band == IEEE80211_BAND_5GHZ)
2477 lq_sta->last_txrate_idx += IWL_FIRST_OFDM_RATE;
2478 lq_sta->is_agg = 0;
2479 #ifdef CONFIG_MAC80211_DEBUGFS
2480 lq_sta->dbg_fixed_rate = 0;
2481 #endif
2482 #ifdef CONFIG_IWLWIFI_DEBUGFS
2483 iwl_mvm_reset_frame_stats(mvm, &mvm->drv_rx_stats);
2484 #endif
2485 rs_initialize_lq(mvm, sta, lq_sta, band, init);
2486 }
2487
2488 static void rs_rate_update(void *mvm_r,
2489 struct ieee80211_supported_band *sband,
2490 struct cfg80211_chan_def *chandef,
2491 struct ieee80211_sta *sta, void *priv_sta,
2492 u32 changed)
2493 {
2494 u8 tid;
2495 struct iwl_op_mode *op_mode =
2496 (struct iwl_op_mode *)mvm_r;
2497 struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
2498
2499 /* Stop any ongoing aggregations as rs starts off assuming no agg */
2500 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++)
2501 ieee80211_stop_tx_ba_session(sta, tid);
2502
2503 iwl_mvm_rs_rate_init(mvm, sta, sband->band, false);
2504 }
2505
2506 #ifdef CONFIG_MAC80211_DEBUGFS
2507 static void rs_build_rates_table_from_fixed(struct iwl_mvm *mvm,
2508 struct iwl_lq_cmd *lq_cmd,
2509 enum ieee80211_band band,
2510 u32 ucode_rate)
2511 {
2512 struct rs_rate rate;
2513 int i;
2514 int num_rates = ARRAY_SIZE(lq_cmd->rs_table);
2515 __le32 ucode_rate_le32 = cpu_to_le32(ucode_rate);
2516
2517 for (i = 0; i < num_rates; i++)
2518 lq_cmd->rs_table[i] = ucode_rate_le32;
2519
2520 rs_rate_from_ucode_rate(ucode_rate, band, &rate);
2521
2522 if (is_mimo(&rate))
2523 lq_cmd->mimo_delim = num_rates - 1;
2524 else
2525 lq_cmd->mimo_delim = 0;
2526 }
2527 #endif /* CONFIG_MAC80211_DEBUGFS */
2528
2529 static void rs_fill_rates_for_column(struct iwl_mvm *mvm,
2530 struct iwl_lq_sta *lq_sta,
2531 struct rs_rate *rate,
2532 __le32 *rs_table, int *rs_table_index,
2533 int num_rates, int num_retries,
2534 u8 valid_tx_ant, bool toggle_ant)
2535 {
2536 int i, j;
2537 __le32 ucode_rate;
2538 bool bottom_reached = false;
2539 int prev_rate_idx = rate->index;
2540 int end = LINK_QUAL_MAX_RETRY_NUM;
2541 int index = *rs_table_index;
2542
2543 for (i = 0; i < num_rates && index < end; i++) {
2544 ucode_rate = cpu_to_le32(ucode_rate_from_rs_rate(mvm, rate));
2545 for (j = 0; j < num_retries && index < end; j++, index++)
2546 rs_table[index] = ucode_rate;
2547
2548 if (toggle_ant)
2549 rs_toggle_antenna(valid_tx_ant, rate);
2550
2551 prev_rate_idx = rate->index;
2552 bottom_reached = rs_get_lower_rate_in_column(lq_sta, rate);
2553 if (bottom_reached && !is_legacy(rate))
2554 break;
2555 }
2556
2557 if (!bottom_reached)
2558 rate->index = prev_rate_idx;
2559
2560 *rs_table_index = index;
2561 }
2562
2563 /* Building the rate table is non trivial. When we're in MIMO2/VHT/80Mhz/SGI
2564 * column the rate table should look like this:
2565 *
2566 * rate[0] 0x400D019 VHT | ANT: AB BW: 80Mhz MCS: 9 NSS: 2 SGI
2567 * rate[1] 0x400D019 VHT | ANT: AB BW: 80Mhz MCS: 9 NSS: 2 SGI
2568 * rate[2] 0x400D018 VHT | ANT: AB BW: 80Mhz MCS: 8 NSS: 2 SGI
2569 * rate[3] 0x400D018 VHT | ANT: AB BW: 80Mhz MCS: 8 NSS: 2 SGI
2570 * rate[4] 0x400D017 VHT | ANT: AB BW: 80Mhz MCS: 7 NSS: 2 SGI
2571 * rate[5] 0x400D017 VHT | ANT: AB BW: 80Mhz MCS: 7 NSS: 2 SGI
2572 * rate[6] 0x4005007 VHT | ANT: A BW: 80Mhz MCS: 7 NSS: 1 NGI
2573 * rate[7] 0x4009006 VHT | ANT: B BW: 80Mhz MCS: 6 NSS: 1 NGI
2574 * rate[8] 0x4005005 VHT | ANT: A BW: 80Mhz MCS: 5 NSS: 1 NGI
2575 * rate[9] 0x800B Legacy | ANT: B Rate: 36 Mbps
2576 * rate[10] 0x4009 Legacy | ANT: A Rate: 24 Mbps
2577 * rate[11] 0x8007 Legacy | ANT: B Rate: 18 Mbps
2578 * rate[12] 0x4005 Legacy | ANT: A Rate: 12 Mbps
2579 * rate[13] 0x800F Legacy | ANT: B Rate: 9 Mbps
2580 * rate[14] 0x400D Legacy | ANT: A Rate: 6 Mbps
2581 * rate[15] 0x800D Legacy | ANT: B Rate: 6 Mbps
2582 */
2583 static void rs_build_rates_table(struct iwl_mvm *mvm,
2584 struct iwl_lq_sta *lq_sta,
2585 const struct rs_rate *initial_rate)
2586 {
2587 struct rs_rate rate;
2588 int num_rates, num_retries, index = 0;
2589 u8 valid_tx_ant = 0;
2590 struct iwl_lq_cmd *lq_cmd = &lq_sta->lq;
2591 bool toggle_ant = false;
2592
2593 memcpy(&rate, initial_rate, sizeof(rate));
2594
2595 valid_tx_ant = mvm->fw->valid_tx_ant;
2596
2597 if (is_siso(&rate)) {
2598 num_rates = RS_INITIAL_SISO_NUM_RATES;
2599 num_retries = RS_HT_VHT_RETRIES_PER_RATE;
2600 } else if (is_mimo(&rate)) {
2601 num_rates = RS_INITIAL_MIMO_NUM_RATES;
2602 num_retries = RS_HT_VHT_RETRIES_PER_RATE;
2603 } else {
2604 num_rates = RS_INITIAL_LEGACY_NUM_RATES;
2605 num_retries = RS_LEGACY_RETRIES_PER_RATE;
2606 toggle_ant = true;
2607 }
2608
2609 rs_fill_rates_for_column(mvm, lq_sta, &rate, lq_cmd->rs_table, &index,
2610 num_rates, num_retries, valid_tx_ant,
2611 toggle_ant);
2612
2613 rs_get_lower_rate_down_column(lq_sta, &rate);
2614
2615 if (is_siso(&rate)) {
2616 num_rates = RS_SECONDARY_SISO_NUM_RATES;
2617 num_retries = RS_SECONDARY_SISO_RETRIES;
2618 lq_cmd->mimo_delim = index;
2619 } else if (is_legacy(&rate)) {
2620 num_rates = RS_SECONDARY_LEGACY_NUM_RATES;
2621 num_retries = RS_LEGACY_RETRIES_PER_RATE;
2622 } else {
2623 WARN_ON_ONCE(1);
2624 }
2625
2626 toggle_ant = true;
2627
2628 rs_fill_rates_for_column(mvm, lq_sta, &rate, lq_cmd->rs_table, &index,
2629 num_rates, num_retries, valid_tx_ant,
2630 toggle_ant);
2631
2632 rs_get_lower_rate_down_column(lq_sta, &rate);
2633
2634 num_rates = RS_SECONDARY_LEGACY_NUM_RATES;
2635 num_retries = RS_LEGACY_RETRIES_PER_RATE;
2636
2637 rs_fill_rates_for_column(mvm, lq_sta, &rate, lq_cmd->rs_table, &index,
2638 num_rates, num_retries, valid_tx_ant,
2639 toggle_ant);
2640
2641 }
2642
2643 static void rs_fill_lq_cmd(struct iwl_mvm *mvm,
2644 struct ieee80211_sta *sta,
2645 struct iwl_lq_sta *lq_sta,
2646 const struct rs_rate *initial_rate)
2647 {
2648 struct iwl_lq_cmd *lq_cmd = &lq_sta->lq;
2649 u8 ant = initial_rate->ant;
2650
2651 #ifdef CONFIG_MAC80211_DEBUGFS
2652 if (lq_sta->dbg_fixed_rate) {
2653 rs_build_rates_table_from_fixed(mvm, lq_cmd,
2654 lq_sta->band,
2655 lq_sta->dbg_fixed_rate);
2656 ant = (lq_sta->dbg_fixed_rate & RATE_MCS_ANT_ABC_MSK) >>
2657 RATE_MCS_ANT_POS;
2658 } else
2659 #endif
2660 rs_build_rates_table(mvm, lq_sta, initial_rate);
2661
2662 if (num_of_ant(ant) == 1)
2663 lq_cmd->single_stream_ant_msk = ant;
2664
2665 lq_cmd->agg_frame_cnt_limit = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
2666 lq_cmd->agg_disable_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
2667
2668 lq_cmd->agg_time_limit =
2669 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
2670
2671 if (sta)
2672 lq_cmd->agg_time_limit =
2673 cpu_to_le16(iwl_mvm_coex_agg_time_limit(mvm, sta));
2674 }
2675
2676 static void *rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
2677 {
2678 return hw->priv;
2679 }
2680 /* rate scale requires free function to be implemented */
2681 static void rs_free(void *mvm_rate)
2682 {
2683 return;
2684 }
2685
2686 static void rs_free_sta(void *mvm_r, struct ieee80211_sta *sta,
2687 void *mvm_sta)
2688 {
2689 struct iwl_op_mode *op_mode __maybe_unused = mvm_r;
2690 struct iwl_mvm *mvm __maybe_unused = IWL_OP_MODE_GET_MVM(op_mode);
2691
2692 IWL_DEBUG_RATE(mvm, "enter\n");
2693 IWL_DEBUG_RATE(mvm, "leave\n");
2694 }
2695
2696 #ifdef CONFIG_MAC80211_DEBUGFS
2697 int rs_pretty_print_rate(char *buf, const u32 rate)
2698 {
2699
2700 char *type, *bw;
2701 u8 mcs = 0, nss = 0;
2702 u8 ant = (rate & RATE_MCS_ANT_ABC_MSK) >> RATE_MCS_ANT_POS;
2703
2704 if (!(rate & RATE_MCS_HT_MSK) &&
2705 !(rate & RATE_MCS_VHT_MSK)) {
2706 int index = iwl_hwrate_to_plcp_idx(rate);
2707
2708 return sprintf(buf, "Legacy | ANT: %s Rate: %s Mbps\n",
2709 rs_pretty_ant(ant),
2710 index == IWL_RATE_INVALID ? "BAD" :
2711 iwl_rate_mcs[index].mbps);
2712 }
2713
2714 if (rate & RATE_MCS_VHT_MSK) {
2715 type = "VHT";
2716 mcs = rate & RATE_VHT_MCS_RATE_CODE_MSK;
2717 nss = ((rate & RATE_VHT_MCS_NSS_MSK)
2718 >> RATE_VHT_MCS_NSS_POS) + 1;
2719 } else if (rate & RATE_MCS_HT_MSK) {
2720 type = "HT";
2721 mcs = rate & RATE_HT_MCS_INDEX_MSK;
2722 } else {
2723 type = "Unknown"; /* shouldn't happen */
2724 }
2725
2726 switch (rate & RATE_MCS_CHAN_WIDTH_MSK) {
2727 case RATE_MCS_CHAN_WIDTH_20:
2728 bw = "20Mhz";
2729 break;
2730 case RATE_MCS_CHAN_WIDTH_40:
2731 bw = "40Mhz";
2732 break;
2733 case RATE_MCS_CHAN_WIDTH_80:
2734 bw = "80Mhz";
2735 break;
2736 case RATE_MCS_CHAN_WIDTH_160:
2737 bw = "160Mhz";
2738 break;
2739 default:
2740 bw = "BAD BW";
2741 }
2742
2743 return sprintf(buf, "%s | ANT: %s BW: %s MCS: %d NSS: %d %s%s%s%s%s\n",
2744 type, rs_pretty_ant(ant), bw, mcs, nss,
2745 (rate & RATE_MCS_SGI_MSK) ? "SGI " : "NGI ",
2746 (rate & RATE_MCS_HT_STBC_MSK) ? "STBC " : "",
2747 (rate & RATE_MCS_LDPC_MSK) ? "LDPC " : "",
2748 (rate & RATE_MCS_BF_MSK) ? "BF " : "",
2749 (rate & RATE_MCS_ZLF_MSK) ? "ZLF " : "");
2750 }
2751
2752 /**
2753 * Program the device to use fixed rate for frame transmit
2754 * This is for debugging/testing only
2755 * once the device start use fixed rate, we need to reload the module
2756 * to being back the normal operation.
2757 */
2758 static void rs_program_fix_rate(struct iwl_mvm *mvm,
2759 struct iwl_lq_sta *lq_sta)
2760 {
2761 lq_sta->active_legacy_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
2762 lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2763 lq_sta->active_mimo2_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2764
2765 IWL_DEBUG_RATE(mvm, "sta_id %d rate 0x%X\n",
2766 lq_sta->lq.sta_id, lq_sta->dbg_fixed_rate);
2767
2768 if (lq_sta->dbg_fixed_rate) {
2769 struct rs_rate rate;
2770 rs_rate_from_ucode_rate(lq_sta->dbg_fixed_rate,
2771 lq_sta->band, &rate);
2772 rs_fill_lq_cmd(mvm, NULL, lq_sta, &rate);
2773 iwl_mvm_send_lq_cmd(lq_sta->drv, &lq_sta->lq, false);
2774 }
2775 }
2776
2777 static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
2778 const char __user *user_buf, size_t count, loff_t *ppos)
2779 {
2780 struct iwl_lq_sta *lq_sta = file->private_data;
2781 struct iwl_mvm *mvm;
2782 char buf[64];
2783 size_t buf_size;
2784 u32 parsed_rate;
2785
2786
2787 mvm = lq_sta->drv;
2788 memset(buf, 0, sizeof(buf));
2789 buf_size = min(count, sizeof(buf) - 1);
2790 if (copy_from_user(buf, user_buf, buf_size))
2791 return -EFAULT;
2792
2793 if (sscanf(buf, "%x", &parsed_rate) == 1)
2794 lq_sta->dbg_fixed_rate = parsed_rate;
2795 else
2796 lq_sta->dbg_fixed_rate = 0;
2797
2798 rs_program_fix_rate(mvm, lq_sta);
2799
2800 return count;
2801 }
2802
2803 static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
2804 char __user *user_buf, size_t count, loff_t *ppos)
2805 {
2806 char *buff;
2807 int desc = 0;
2808 int i = 0;
2809 ssize_t ret;
2810
2811 struct iwl_lq_sta *lq_sta = file->private_data;
2812 struct iwl_mvm *mvm;
2813 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
2814 struct rs_rate *rate = &tbl->rate;
2815 mvm = lq_sta->drv;
2816 buff = kmalloc(2048, GFP_KERNEL);
2817 if (!buff)
2818 return -ENOMEM;
2819
2820 desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
2821 desc += sprintf(buff+desc, "failed=%d success=%d rate=0%lX\n",
2822 lq_sta->total_failed, lq_sta->total_success,
2823 lq_sta->active_legacy_rate);
2824 desc += sprintf(buff+desc, "fixed rate 0x%X\n",
2825 lq_sta->dbg_fixed_rate);
2826 desc += sprintf(buff+desc, "valid_tx_ant %s%s%s\n",
2827 (mvm->fw->valid_tx_ant & ANT_A) ? "ANT_A," : "",
2828 (mvm->fw->valid_tx_ant & ANT_B) ? "ANT_B," : "",
2829 (mvm->fw->valid_tx_ant & ANT_C) ? "ANT_C" : "");
2830 desc += sprintf(buff+desc, "lq type %s\n",
2831 (is_legacy(rate)) ? "legacy" :
2832 is_vht(rate) ? "VHT" : "HT");
2833 if (!is_legacy(rate)) {
2834 desc += sprintf(buff+desc, " %s",
2835 (is_siso(rate)) ? "SISO" : "MIMO2");
2836 desc += sprintf(buff+desc, " %s",
2837 (is_ht20(rate)) ? "20MHz" :
2838 (is_ht40(rate)) ? "40MHz" :
2839 (is_ht80(rate)) ? "80Mhz" : "BAD BW");
2840 desc += sprintf(buff+desc, " %s %s\n",
2841 (rate->sgi) ? "SGI" : "NGI",
2842 (lq_sta->is_agg) ? "AGG on" : "");
2843 }
2844 desc += sprintf(buff+desc, "last tx rate=0x%X\n",
2845 lq_sta->last_rate_n_flags);
2846 desc += sprintf(buff+desc,
2847 "general: flags=0x%X mimo-d=%d s-ant=0x%x d-ant=0x%x\n",
2848 lq_sta->lq.flags,
2849 lq_sta->lq.mimo_delim,
2850 lq_sta->lq.single_stream_ant_msk,
2851 lq_sta->lq.dual_stream_ant_msk);
2852
2853 desc += sprintf(buff+desc,
2854 "agg: time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
2855 le16_to_cpu(lq_sta->lq.agg_time_limit),
2856 lq_sta->lq.agg_disable_start_th,
2857 lq_sta->lq.agg_frame_cnt_limit);
2858
2859 desc += sprintf(buff+desc,
2860 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
2861 lq_sta->lq.initial_rate_index[0],
2862 lq_sta->lq.initial_rate_index[1],
2863 lq_sta->lq.initial_rate_index[2],
2864 lq_sta->lq.initial_rate_index[3]);
2865
2866 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
2867 u32 r = le32_to_cpu(lq_sta->lq.rs_table[i]);
2868
2869 desc += sprintf(buff+desc, " rate[%d] 0x%X ", i, r);
2870 desc += rs_pretty_print_rate(buff+desc, r);
2871 }
2872
2873 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2874 kfree(buff);
2875 return ret;
2876 }
2877
2878 static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
2879 .write = rs_sta_dbgfs_scale_table_write,
2880 .read = rs_sta_dbgfs_scale_table_read,
2881 .open = simple_open,
2882 .llseek = default_llseek,
2883 };
2884 static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
2885 char __user *user_buf, size_t count, loff_t *ppos)
2886 {
2887 char *buff;
2888 int desc = 0;
2889 int i, j;
2890 ssize_t ret;
2891 struct iwl_scale_tbl_info *tbl;
2892 struct rs_rate *rate;
2893 struct iwl_lq_sta *lq_sta = file->private_data;
2894
2895 buff = kmalloc(1024, GFP_KERNEL);
2896 if (!buff)
2897 return -ENOMEM;
2898
2899 for (i = 0; i < LQ_SIZE; i++) {
2900 tbl = &(lq_sta->lq_info[i]);
2901 rate = &tbl->rate;
2902 desc += sprintf(buff+desc,
2903 "%s type=%d SGI=%d BW=%s DUP=0\n"
2904 "index=%d\n",
2905 lq_sta->active_tbl == i ? "*" : "x",
2906 rate->type,
2907 rate->sgi,
2908 is_ht20(rate) ? "20Mhz" :
2909 is_ht40(rate) ? "40Mhz" :
2910 is_ht80(rate) ? "80Mhz" : "ERR",
2911 rate->index);
2912 for (j = 0; j < IWL_RATE_COUNT; j++) {
2913 desc += sprintf(buff+desc,
2914 "counter=%d success=%d %%=%d\n",
2915 tbl->win[j].counter,
2916 tbl->win[j].success_counter,
2917 tbl->win[j].success_ratio);
2918 }
2919 }
2920 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2921 kfree(buff);
2922 return ret;
2923 }
2924
2925 static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
2926 .read = rs_sta_dbgfs_stats_table_read,
2927 .open = simple_open,
2928 .llseek = default_llseek,
2929 };
2930
2931 static void rs_add_debugfs(void *mvm, void *mvm_sta, struct dentry *dir)
2932 {
2933 struct iwl_lq_sta *lq_sta = mvm_sta;
2934 lq_sta->rs_sta_dbgfs_scale_table_file =
2935 debugfs_create_file("rate_scale_table", S_IRUSR | S_IWUSR, dir,
2936 lq_sta, &rs_sta_dbgfs_scale_table_ops);
2937 lq_sta->rs_sta_dbgfs_stats_table_file =
2938 debugfs_create_file("rate_stats_table", S_IRUSR, dir,
2939 lq_sta, &rs_sta_dbgfs_stats_table_ops);
2940 lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file =
2941 debugfs_create_u8("tx_agg_tid_enable", S_IRUSR | S_IWUSR, dir,
2942 &lq_sta->tx_agg_tid_en);
2943 }
2944
2945 static void rs_remove_debugfs(void *mvm, void *mvm_sta)
2946 {
2947 struct iwl_lq_sta *lq_sta = mvm_sta;
2948 debugfs_remove(lq_sta->rs_sta_dbgfs_scale_table_file);
2949 debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
2950 debugfs_remove(lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file);
2951 }
2952 #endif
2953
2954 /*
2955 * Initialization of rate scaling information is done by driver after
2956 * the station is added. Since mac80211 calls this function before a
2957 * station is added we ignore it.
2958 */
2959 static void rs_rate_init_stub(void *mvm_r,
2960 struct ieee80211_supported_band *sband,
2961 struct cfg80211_chan_def *chandef,
2962 struct ieee80211_sta *sta, void *mvm_sta)
2963 {
2964 }
2965
2966 static const struct rate_control_ops rs_mvm_ops = {
2967 .name = RS_NAME,
2968 .tx_status = rs_tx_status,
2969 .get_rate = rs_get_rate,
2970 .rate_init = rs_rate_init_stub,
2971 .alloc = rs_alloc,
2972 .free = rs_free,
2973 .alloc_sta = rs_alloc_sta,
2974 .free_sta = rs_free_sta,
2975 .rate_update = rs_rate_update,
2976 #ifdef CONFIG_MAC80211_DEBUGFS
2977 .add_sta_debugfs = rs_add_debugfs,
2978 .remove_sta_debugfs = rs_remove_debugfs,
2979 #endif
2980 };
2981
2982 int iwl_mvm_rate_control_register(void)
2983 {
2984 return ieee80211_rate_control_register(&rs_mvm_ops);
2985 }
2986
2987 void iwl_mvm_rate_control_unregister(void)
2988 {
2989 ieee80211_rate_control_unregister(&rs_mvm_ops);
2990 }
2991
2992 /**
2993 * iwl_mvm_tx_protection - Gets LQ command, change it to enable/disable
2994 * Tx protection, according to this rquest and previous requests,
2995 * and send the LQ command.
2996 * @mvmsta: The station
2997 * @enable: Enable Tx protection?
2998 */
2999 int iwl_mvm_tx_protection(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
3000 bool enable)
3001 {
3002 struct iwl_lq_cmd *lq = &mvmsta->lq_sta.lq;
3003
3004 lockdep_assert_held(&mvm->mutex);
3005
3006 if (enable) {
3007 if (mvmsta->tx_protection == 0)
3008 lq->flags |= LQ_FLAG_USE_RTS_MSK;
3009 mvmsta->tx_protection++;
3010 } else {
3011 mvmsta->tx_protection--;
3012 if (mvmsta->tx_protection == 0)
3013 lq->flags &= ~LQ_FLAG_USE_RTS_MSK;
3014 }
3015
3016 return iwl_mvm_send_lq_cmd(mvm, lq, false);
3017 }