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