blob: 3cfcea5474586d98c9aeda6a665de620cbf3dd23 [file] [log] [blame]
Johannes Berg8ca151b2013-01-24 14:25:36 +01001/******************************************************************************
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[] = {
Eliad Pelleref4394b2013-07-16 18:07:20 +030059 [IWL_RATE_1M_INDEX] = IWL_RATE_6M_INDEX,
60 [IWL_RATE_2M_INDEX] = IWL_RATE_6M_INDEX,
61 [IWL_RATE_5M_INDEX] = IWL_RATE_6M_INDEX,
62 [IWL_RATE_11M_INDEX] = IWL_RATE_6M_INDEX,
63 [IWL_RATE_6M_INDEX] = IWL_RATE_6M_INDEX,
64 [IWL_RATE_9M_INDEX] = IWL_RATE_6M_INDEX,
65 [IWL_RATE_12M_INDEX] = IWL_RATE_9M_INDEX,
66 [IWL_RATE_18M_INDEX] = IWL_RATE_12M_INDEX,
67 [IWL_RATE_24M_INDEX] = IWL_RATE_18M_INDEX,
68 [IWL_RATE_36M_INDEX] = IWL_RATE_24M_INDEX,
69 [IWL_RATE_48M_INDEX] = IWL_RATE_36M_INDEX,
70 [IWL_RATE_54M_INDEX] = IWL_RATE_48M_INDEX,
71 [IWL_RATE_60M_INDEX] = IWL_RATE_54M_INDEX,
Johannes Berg8ca151b2013-01-24 14:25:36 +010072};
73
74static const u8 ant_toggle_lookup[] = {
Eliad Pelleref4394b2013-07-16 18:07:20 +030075 [ANT_NONE] = ANT_NONE,
76 [ANT_A] = ANT_B,
77 [ANT_B] = ANT_C,
78 [ANT_AB] = ANT_BC,
79 [ANT_C] = ANT_A,
80 [ANT_AC] = ANT_AB,
81 [ANT_BC] = ANT_AC,
82 [ANT_ABC] = ANT_ABC,
Johannes Berg8ca151b2013-01-24 14:25:36 +010083};
84
Eyal Shapiraab055ce2013-08-04 13:10:31 +030085#define IWL_DECLARE_RATE_INFO(r, s, rp, rn) \
Johannes Berg8ca151b2013-01-24 14:25:36 +010086 [IWL_RATE_##r##M_INDEX] = { IWL_RATE_##r##M_PLCP, \
87 IWL_RATE_SISO_##s##M_PLCP, \
88 IWL_RATE_MIMO2_##s##M_PLCP,\
Johannes Berg8ca151b2013-01-24 14:25:36 +010089 IWL_RATE_##rp##M_INDEX, \
Eyal Shapiraab055ce2013-08-04 13:10:31 +030090 IWL_RATE_##rn##M_INDEX }
Johannes Berg8ca151b2013-01-24 14:25:36 +010091
92/*
93 * Parameter order:
Eyal Shapiraab055ce2013-08-04 13:10:31 +030094 * rate, ht rate, prev rate, next rate
Johannes Berg8ca151b2013-01-24 14:25:36 +010095 *
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] = {
Eyal Shapiraab055ce2013-08-04 13:10:31 +0300101 IWL_DECLARE_RATE_INFO(1, INV, INV, 2), /* 1mbps */
102 IWL_DECLARE_RATE_INFO(2, INV, 1, 5), /* 2mbps */
103 IWL_DECLARE_RATE_INFO(5, INV, 2, 11), /*5.5mbps */
104 IWL_DECLARE_RATE_INFO(11, INV, 9, 12), /* 11mbps */
105 IWL_DECLARE_RATE_INFO(6, 6, 5, 11), /* 6mbps */
106 IWL_DECLARE_RATE_INFO(9, 6, 6, 11), /* 9mbps */
107 IWL_DECLARE_RATE_INFO(12, 12, 11, 18), /* 12mbps */
108 IWL_DECLARE_RATE_INFO(18, 18, 12, 24), /* 18mbps */
109 IWL_DECLARE_RATE_INFO(24, 24, 18, 36), /* 24mbps */
110 IWL_DECLARE_RATE_INFO(36, 36, 24, 48), /* 36mbps */
111 IWL_DECLARE_RATE_INFO(48, 48, 36, 54), /* 48mbps */
112 IWL_DECLARE_RATE_INFO(54, 54, 48, INV), /* 54mbps */
113 IWL_DECLARE_RATE_INFO(60, 60, 48, INV), /* 60mbps */
Johannes Berg8ca151b2013-01-24 14:25:36 +0100114 /* 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
Eyal Shapirad972ab32013-07-28 23:02:45 +0000131 WARN_ON_ONCE(idx >= IWL_RATE_MIMO3_6M_PLCP);
132 if (idx >= IWL_RATE_MIMO2_6M_PLCP)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100133 idx = idx - IWL_RATE_MIMO2_6M_PLCP;
134
135 idx += IWL_FIRST_OFDM_RATE;
136 /* skip 9M not supported in ht*/
137 if (idx >= IWL_RATE_9M_INDEX)
138 idx += 1;
139 if ((idx >= IWL_FIRST_OFDM_RATE) && (idx <= IWL_LAST_OFDM_RATE))
140 return idx;
141
142 /* legacy rate format, search for match in table */
143 } else {
144 for (idx = 0; idx < ARRAY_SIZE(iwl_rates); idx++)
145 if (iwl_rates[idx].plcp ==
146 rs_extract_rate(rate_n_flags))
147 return idx;
148 }
149
150 return -1;
151}
152
153static void rs_rate_scale_perform(struct iwl_mvm *mvm,
154 struct sk_buff *skb,
155 struct ieee80211_sta *sta,
156 struct iwl_lq_sta *lq_sta);
157static void rs_fill_link_cmd(struct iwl_mvm *mvm,
158 struct iwl_lq_sta *lq_sta, u32 rate_n_flags);
159static void rs_stay_in_table(struct iwl_lq_sta *lq_sta, bool force_search);
160
161
162#ifdef CONFIG_MAC80211_DEBUGFS
163static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
Eyal Shapira3571ac32013-07-29 17:08:48 +0300164 u32 *rate_n_flags);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100165#else
166static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
Eyal Shapira3571ac32013-07-29 17:08:48 +0300167 u32 *rate_n_flags)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100168{}
169#endif
170
171/**
172 * The following tables contain the expected throughput metrics for all rates
173 *
174 * 1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
175 *
176 * where invalid entries are zeros.
177 *
178 * CCK rates are only valid in legacy table and will only be used in G
179 * (2.4 GHz) band.
180 */
181
182static s32 expected_tpt_legacy[IWL_RATE_COUNT] = {
183 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 0
184};
185
186static s32 expected_tpt_siso20MHz[4][IWL_RATE_COUNT] = {
187 {0, 0, 0, 0, 42, 0, 76, 102, 124, 159, 183, 193, 202}, /* Norm */
188 {0, 0, 0, 0, 46, 0, 82, 110, 132, 168, 192, 202, 210}, /* SGI */
189 {0, 0, 0, 0, 47, 0, 91, 133, 171, 242, 305, 334, 362}, /* AGG */
190 {0, 0, 0, 0, 52, 0, 101, 145, 187, 264, 330, 361, 390}, /* AGG+SGI */
191};
192
193static s32 expected_tpt_siso40MHz[4][IWL_RATE_COUNT] = {
194 {0, 0, 0, 0, 77, 0, 127, 160, 184, 220, 242, 250, 257}, /* Norm */
195 {0, 0, 0, 0, 83, 0, 135, 169, 193, 229, 250, 257, 264}, /* SGI */
196 {0, 0, 0, 0, 94, 0, 177, 249, 313, 423, 512, 550, 586}, /* AGG */
197 {0, 0, 0, 0, 104, 0, 193, 270, 338, 454, 545, 584, 620}, /* AGG+SGI */
198};
199
200static s32 expected_tpt_mimo2_20MHz[4][IWL_RATE_COUNT] = {
201 {0, 0, 0, 0, 74, 0, 123, 155, 179, 214, 236, 244, 251}, /* Norm */
202 {0, 0, 0, 0, 81, 0, 131, 164, 188, 223, 243, 251, 257}, /* SGI */
203 {0, 0, 0, 0, 89, 0, 167, 235, 296, 402, 488, 526, 560}, /* AGG */
204 {0, 0, 0, 0, 97, 0, 182, 255, 320, 431, 520, 558, 593}, /* AGG+SGI*/
205};
206
207static s32 expected_tpt_mimo2_40MHz[4][IWL_RATE_COUNT] = {
208 {0, 0, 0, 0, 123, 0, 182, 214, 235, 264, 279, 285, 289}, /* Norm */
209 {0, 0, 0, 0, 131, 0, 191, 222, 242, 270, 284, 289, 293}, /* SGI */
210 {0, 0, 0, 0, 171, 0, 305, 410, 496, 634, 731, 771, 805}, /* AGG */
211 {0, 0, 0, 0, 186, 0, 329, 439, 527, 667, 764, 803, 838}, /* AGG+SGI */
212};
213
Johannes Berg8ca151b2013-01-24 14:25:36 +0100214/* mbps, mcs */
215static const struct iwl_rate_mcs_info iwl_rate_mcs[IWL_RATE_COUNT] = {
216 { "1", "BPSK DSSS"},
217 { "2", "QPSK DSSS"},
218 {"5.5", "BPSK CCK"},
219 { "11", "QPSK CCK"},
220 { "6", "BPSK 1/2"},
221 { "9", "BPSK 1/2"},
222 { "12", "QPSK 1/2"},
223 { "18", "QPSK 3/4"},
224 { "24", "16QAM 1/2"},
225 { "36", "16QAM 3/4"},
226 { "48", "64QAM 2/3"},
227 { "54", "64QAM 3/4"},
228 { "60", "64QAM 5/6"},
229};
230
231#define MCS_INDEX_PER_STREAM (8)
232
233static void rs_rate_scale_clear_window(struct iwl_rate_scale_data *window)
234{
235 window->data = 0;
236 window->success_counter = 0;
237 window->success_ratio = IWL_INVALID_VALUE;
238 window->counter = 0;
239 window->average_tpt = IWL_INVALID_VALUE;
240 window->stamp = 0;
241}
242
243static inline u8 rs_is_valid_ant(u8 valid_antenna, u8 ant_type)
244{
245 return (ant_type & valid_antenna) == ant_type;
246}
247
Johannes Berg8ca151b2013-01-24 14:25:36 +0100248#ifdef CONFIG_MAC80211_DEBUGFS
249/**
250 * Program the device to use fixed rate for frame transmit
251 * This is for debugging/testing only
252 * once the device start use fixed rate, we need to reload the module
253 * to being back the normal operation.
254 */
255static void rs_program_fix_rate(struct iwl_mvm *mvm,
256 struct iwl_lq_sta *lq_sta)
257{
258 lq_sta->active_legacy_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
259 lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
260 lq_sta->active_mimo2_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
Johannes Berg8ca151b2013-01-24 14:25:36 +0100261
262 IWL_DEBUG_RATE(mvm, "sta_id %d rate 0x%X\n",
263 lq_sta->lq.sta_id, lq_sta->dbg_fixed_rate);
264
265 if (lq_sta->dbg_fixed_rate) {
266 rs_fill_link_cmd(NULL, lq_sta, lq_sta->dbg_fixed_rate);
267 iwl_mvm_send_lq_cmd(lq_sta->drv, &lq_sta->lq, CMD_ASYNC, false);
268 }
269}
270#endif
271
Johannes Berg8ca151b2013-01-24 14:25:36 +0100272static int rs_tl_turn_on_agg_for_tid(struct iwl_mvm *mvm,
273 struct iwl_lq_sta *lq_data, u8 tid,
274 struct ieee80211_sta *sta)
275{
276 int ret = -EAGAIN;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100277
Emmanuel Grumbach7c8e4152013-05-28 21:36:16 +0300278 /*
279 * Don't create TX aggregation sessions when in high
280 * BT traffic, as they would just be disrupted by BT.
281 */
282 if (BT_MBOX_MSG(&mvm->last_bt_notif, 3, TRAFFIC_LOAD) >= 2) {
283 IWL_DEBUG_COEX(mvm, "BT traffic (%d), no aggregation allowed\n",
284 BT_MBOX_MSG(&mvm->last_bt_notif,
285 3, TRAFFIC_LOAD));
286 return ret;
287 }
288
Emmanuel Grumbach44cc4292013-06-18 06:33:49 +0300289 IWL_DEBUG_HT(mvm, "Starting Tx agg: STA: %pM tid: %d\n",
290 sta->addr, tid);
291 ret = ieee80211_start_tx_ba_session(sta, tid, 5000);
292 if (ret == -EAGAIN) {
293 /*
294 * driver and mac80211 is out of sync
295 * this might be cause by reloading firmware
296 * stop the tx ba session here
297 */
298 IWL_ERR(mvm, "Fail start Tx agg on tid: %d\n",
299 tid);
300 ieee80211_stop_tx_ba_session(sta, tid);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100301 }
302 return ret;
303}
304
305static void rs_tl_turn_on_agg(struct iwl_mvm *mvm, u8 tid,
306 struct iwl_lq_sta *lq_data,
307 struct ieee80211_sta *sta)
308{
309 if (tid < IWL_MAX_TID_COUNT)
310 rs_tl_turn_on_agg_for_tid(mvm, lq_data, tid, sta);
311 else
312 IWL_ERR(mvm, "tid exceeds max TID count: %d/%d\n",
313 tid, IWL_MAX_TID_COUNT);
314}
315
316static inline int get_num_of_ant_from_rate(u32 rate_n_flags)
317{
318 return !!(rate_n_flags & RATE_MCS_ANT_A_MSK) +
319 !!(rate_n_flags & RATE_MCS_ANT_B_MSK) +
320 !!(rate_n_flags & RATE_MCS_ANT_C_MSK);
321}
322
323/*
324 * Static function to get the expected throughput from an iwl_scale_tbl_info
325 * that wraps a NULL pointer check
326 */
327static s32 get_expected_tpt(struct iwl_scale_tbl_info *tbl, int rs_index)
328{
329 if (tbl->expected_tpt)
330 return tbl->expected_tpt[rs_index];
331 return 0;
332}
333
334/**
335 * rs_collect_tx_data - Update the success/failure sliding window
336 *
337 * We keep a sliding window of the last 62 packets transmitted
338 * at this rate. window->data contains the bitmask of successful
339 * packets.
340 */
341static int rs_collect_tx_data(struct iwl_scale_tbl_info *tbl,
342 int scale_index, int attempts, int successes)
343{
344 struct iwl_rate_scale_data *window = NULL;
345 static const u64 mask = (((u64)1) << (IWL_RATE_MAX_WINDOW - 1));
346 s32 fail_count, tpt;
347
348 if (scale_index < 0 || scale_index >= IWL_RATE_COUNT)
349 return -EINVAL;
350
351 /* Select window for current tx bit rate */
352 window = &(tbl->win[scale_index]);
353
354 /* Get expected throughput */
355 tpt = get_expected_tpt(tbl, scale_index);
356
357 /*
358 * Keep track of only the latest 62 tx frame attempts in this rate's
359 * history window; anything older isn't really relevant any more.
360 * If we have filled up the sliding window, drop the oldest attempt;
361 * if the oldest attempt (highest bit in bitmap) shows "success",
362 * subtract "1" from the success counter (this is the main reason
363 * we keep these bitmaps!).
364 */
365 while (attempts > 0) {
366 if (window->counter >= IWL_RATE_MAX_WINDOW) {
367 /* remove earliest */
368 window->counter = IWL_RATE_MAX_WINDOW - 1;
369
370 if (window->data & mask) {
371 window->data &= ~mask;
372 window->success_counter--;
373 }
374 }
375
376 /* Increment frames-attempted counter */
377 window->counter++;
378
379 /* Shift bitmap by one frame to throw away oldest history */
380 window->data <<= 1;
381
382 /* Mark the most recent #successes attempts as successful */
383 if (successes > 0) {
384 window->success_counter++;
385 window->data |= 0x1;
386 successes--;
387 }
388
389 attempts--;
390 }
391
392 /* Calculate current success ratio, avoid divide-by-0! */
393 if (window->counter > 0)
394 window->success_ratio = 128 * (100 * window->success_counter)
395 / window->counter;
396 else
397 window->success_ratio = IWL_INVALID_VALUE;
398
399 fail_count = window->counter - window->success_counter;
400
401 /* Calculate average throughput, if we have enough history. */
402 if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
403 (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
404 window->average_tpt = (window->success_ratio * tpt + 64) / 128;
405 else
406 window->average_tpt = IWL_INVALID_VALUE;
407
408 /* Tag this window as having been updated */
409 window->stamp = jiffies;
410
411 return 0;
412}
413
414/*
415 * Fill uCode API rate_n_flags field, based on "search" or "active" table.
416 */
417/* FIXME:RS:remove this function and put the flags statically in the table */
418static u32 rate_n_flags_from_tbl(struct iwl_mvm *mvm,
419 struct iwl_scale_tbl_info *tbl,
420 int index, u8 use_green)
421{
422 u32 rate_n_flags = 0;
423
424 if (is_legacy(tbl->lq_type)) {
425 rate_n_flags = iwl_rates[index].plcp;
426 if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
427 rate_n_flags |= RATE_MCS_CCK_MSK;
428 } else if (is_Ht(tbl->lq_type)) {
429 if (index > IWL_LAST_OFDM_RATE) {
430 IWL_ERR(mvm, "Invalid HT rate index %d\n", index);
431 index = IWL_LAST_OFDM_RATE;
432 }
433 rate_n_flags = RATE_MCS_HT_MSK;
434
435 if (is_siso(tbl->lq_type))
436 rate_n_flags |= iwl_rates[index].plcp_siso;
437 else if (is_mimo2(tbl->lq_type))
438 rate_n_flags |= iwl_rates[index].plcp_mimo2;
439 else
Eyal Shapirad972ab32013-07-28 23:02:45 +0000440 WARN_ON_ONCE(1);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100441 } else {
442 IWL_ERR(mvm, "Invalid tbl->lq_type %d\n", tbl->lq_type);
443 }
444
445 rate_n_flags |= ((tbl->ant_type << RATE_MCS_ANT_POS) &
446 RATE_MCS_ANT_ABC_MSK);
447
448 if (is_Ht(tbl->lq_type)) {
449 if (tbl->is_ht40)
450 rate_n_flags |= RATE_MCS_CHAN_WIDTH_40;
451 if (tbl->is_SGI)
452 rate_n_flags |= RATE_MCS_SGI_MSK;
453
454 if (use_green) {
455 rate_n_flags |= RATE_HT_MCS_GF_MSK;
456 if (is_siso(tbl->lq_type) && tbl->is_SGI) {
457 rate_n_flags &= ~RATE_MCS_SGI_MSK;
458 IWL_ERR(mvm, "GF was set with SGI:SISO\n");
459 }
460 }
461 }
462 return rate_n_flags;
463}
464
465/*
466 * Interpret uCode API's rate_n_flags format,
467 * fill "search" or "active" tx mode table.
468 */
469static int rs_get_tbl_info_from_mcs(const u32 rate_n_flags,
470 enum ieee80211_band band,
471 struct iwl_scale_tbl_info *tbl,
472 int *rate_idx)
473{
474 u32 ant_msk = (rate_n_flags & RATE_MCS_ANT_ABC_MSK);
475 u8 num_of_ant = get_num_of_ant_from_rate(rate_n_flags);
476 u8 mcs;
477
Eliad Pellerfaec6f92013-07-17 09:16:30 +0300478 memset(tbl, 0, offsetof(struct iwl_scale_tbl_info, win));
Johannes Berg8ca151b2013-01-24 14:25:36 +0100479 *rate_idx = iwl_hwrate_to_plcp_idx(rate_n_flags);
480
481 if (*rate_idx == IWL_RATE_INVALID) {
482 *rate_idx = -1;
483 return -EINVAL;
484 }
485 tbl->is_SGI = 0; /* default legacy setup */
486 tbl->is_ht40 = 0;
487 tbl->ant_type = (ant_msk >> RATE_MCS_ANT_POS);
488 tbl->lq_type = LQ_NONE;
489 tbl->max_search = IWL_MAX_SEARCH;
490
491 /* legacy rate format */
492 if (!(rate_n_flags & RATE_MCS_HT_MSK)) {
493 if (num_of_ant == 1) {
494 if (band == IEEE80211_BAND_5GHZ)
495 tbl->lq_type = LQ_A;
496 else
497 tbl->lq_type = LQ_G;
498 }
499 /* HT rate format */
500 } else {
501 if (rate_n_flags & RATE_MCS_SGI_MSK)
502 tbl->is_SGI = 1;
503
504 if (rate_n_flags & RATE_MCS_CHAN_WIDTH_40) /* TODO */
505 tbl->is_ht40 = 1;
506
507 mcs = rs_extract_rate(rate_n_flags);
508
509 /* SISO */
510 if (mcs <= IWL_RATE_SISO_60M_PLCP) {
511 if (num_of_ant == 1)
512 tbl->lq_type = LQ_SISO; /*else NONE*/
513 /* MIMO2 */
514 } else if (mcs <= IWL_RATE_MIMO2_60M_PLCP) {
515 if (num_of_ant == 2)
516 tbl->lq_type = LQ_MIMO2;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100517 } else {
Eyal Shapirad972ab32013-07-28 23:02:45 +0000518 WARN_ON_ONCE(num_of_ant == 3);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100519 }
520 }
521 return 0;
522}
523
524/* switch to another antenna/antennas and return 1 */
525/* if no other valid antenna found, return 0 */
526static int rs_toggle_antenna(u32 valid_ant, u32 *rate_n_flags,
527 struct iwl_scale_tbl_info *tbl)
528{
529 u8 new_ant_type;
530
531 if (!tbl->ant_type || tbl->ant_type > ANT_ABC)
532 return 0;
533
534 if (!rs_is_valid_ant(valid_ant, tbl->ant_type))
535 return 0;
536
537 new_ant_type = ant_toggle_lookup[tbl->ant_type];
538
539 while ((new_ant_type != tbl->ant_type) &&
540 !rs_is_valid_ant(valid_ant, new_ant_type))
541 new_ant_type = ant_toggle_lookup[new_ant_type];
542
543 if (new_ant_type == tbl->ant_type)
544 return 0;
545
546 tbl->ant_type = new_ant_type;
547 *rate_n_flags &= ~RATE_MCS_ANT_ABC_MSK;
548 *rate_n_flags |= new_ant_type << RATE_MCS_ANT_POS;
549 return 1;
550}
551
552/**
553 * Green-field mode is valid if the station supports it and
554 * there are no non-GF stations present in the BSS.
555 */
556static bool rs_use_green(struct ieee80211_sta *sta)
557{
Beni Lev6e6cc9f2013-02-19 14:40:11 +0200558 /*
559 * There's a bug somewhere in this code that causes the
560 * scaling to get stuck because GF+SGI can't be combined
561 * in SISO rates. Until we find that bug, disable GF, it
562 * has only limited benefit and we still interoperate with
563 * GF APs since we can always receive GF transmissions.
564 */
565 return false;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100566}
567
568/**
569 * rs_get_supported_rates - get the available rates
570 *
571 * if management frame or broadcast frame only return
572 * basic available rates.
573 *
574 */
575static u16 rs_get_supported_rates(struct iwl_lq_sta *lq_sta,
576 struct ieee80211_hdr *hdr,
577 enum iwl_table_type rate_type)
578{
579 if (is_legacy(rate_type)) {
580 return lq_sta->active_legacy_rate;
581 } else {
582 if (is_siso(rate_type))
583 return lq_sta->active_siso_rate;
Eyal Shapirad972ab32013-07-28 23:02:45 +0000584 else {
585 WARN_ON_ONCE(!is_mimo2(rate_type));
Johannes Berg8ca151b2013-01-24 14:25:36 +0100586 return lq_sta->active_mimo2_rate;
Eyal Shapirad972ab32013-07-28 23:02:45 +0000587 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100588 }
589}
590
591static u16 rs_get_adjacent_rate(struct iwl_mvm *mvm, u8 index, u16 rate_mask,
592 int rate_type)
593{
594 u8 high = IWL_RATE_INVALID;
595 u8 low = IWL_RATE_INVALID;
596
597 /* 802.11A or ht walks to the next literal adjacent rate in
598 * the rate table */
599 if (is_a_band(rate_type) || !is_legacy(rate_type)) {
600 int i;
601 u32 mask;
602
603 /* Find the previous rate that is in the rate mask */
604 i = index - 1;
605 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
606 if (rate_mask & mask) {
607 low = i;
608 break;
609 }
610 }
611
612 /* Find the next rate that is in the rate mask */
613 i = index + 1;
614 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
615 if (rate_mask & mask) {
616 high = i;
617 break;
618 }
619 }
620
621 return (high << 8) | low;
622 }
623
624 low = index;
625 while (low != IWL_RATE_INVALID) {
626 low = iwl_rates[low].prev_rs;
627 if (low == IWL_RATE_INVALID)
628 break;
629 if (rate_mask & (1 << low))
630 break;
631 IWL_DEBUG_RATE(mvm, "Skipping masked lower rate: %d\n", low);
632 }
633
634 high = index;
635 while (high != IWL_RATE_INVALID) {
636 high = iwl_rates[high].next_rs;
637 if (high == IWL_RATE_INVALID)
638 break;
639 if (rate_mask & (1 << high))
640 break;
641 IWL_DEBUG_RATE(mvm, "Skipping masked higher rate: %d\n", high);
642 }
643
644 return (high << 8) | low;
645}
646
647static u32 rs_get_lower_rate(struct iwl_lq_sta *lq_sta,
648 struct iwl_scale_tbl_info *tbl,
649 u8 scale_index, u8 ht_possible)
650{
651 s32 low;
652 u16 rate_mask;
653 u16 high_low;
654 u8 switch_to_legacy = 0;
655 u8 is_green = lq_sta->is_green;
656 struct iwl_mvm *mvm = lq_sta->drv;
657
658 /* check if we need to switch from HT to legacy rates.
659 * assumption is that mandatory rates (1Mbps or 6Mbps)
660 * are always supported (spec demand) */
661 if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_index)) {
662 switch_to_legacy = 1;
663 scale_index = rs_ht_to_legacy[scale_index];
664 if (lq_sta->band == IEEE80211_BAND_5GHZ)
665 tbl->lq_type = LQ_A;
666 else
667 tbl->lq_type = LQ_G;
668
669 if (num_of_ant(tbl->ant_type) > 1)
670 tbl->ant_type =
Emmanuel Grumbachff402312013-04-03 20:10:06 +0300671 first_antenna(iwl_fw_valid_tx_ant(mvm->fw));
Johannes Berg8ca151b2013-01-24 14:25:36 +0100672
673 tbl->is_ht40 = 0;
674 tbl->is_SGI = 0;
675 tbl->max_search = IWL_MAX_SEARCH;
676 }
677
678 rate_mask = rs_get_supported_rates(lq_sta, NULL, tbl->lq_type);
679
680 /* Mask with station rate restriction */
681 if (is_legacy(tbl->lq_type)) {
682 /* supp_rates has no CCK bits in A mode */
683 if (lq_sta->band == IEEE80211_BAND_5GHZ)
684 rate_mask = (u16)(rate_mask &
685 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
686 else
687 rate_mask = (u16)(rate_mask & lq_sta->supp_rates);
688 }
689
690 /* If we switched from HT to legacy, check current rate */
691 if (switch_to_legacy && (rate_mask & (1 << scale_index))) {
692 low = scale_index;
693 goto out;
694 }
695
696 high_low = rs_get_adjacent_rate(lq_sta->drv, scale_index, rate_mask,
697 tbl->lq_type);
698 low = high_low & 0xff;
699
700 if (low == IWL_RATE_INVALID)
701 low = scale_index;
702
703out:
704 return rate_n_flags_from_tbl(lq_sta->drv, tbl, low, is_green);
705}
706
707/*
708 * Simple function to compare two rate scale table types
709 */
710static bool table_type_matches(struct iwl_scale_tbl_info *a,
711 struct iwl_scale_tbl_info *b)
712{
713 return (a->lq_type == b->lq_type) && (a->ant_type == b->ant_type) &&
714 (a->is_SGI == b->is_SGI);
715}
716
717/*
718 * mac80211 sends us Tx status
719 */
720static void rs_tx_status(void *mvm_r, struct ieee80211_supported_band *sband,
721 struct ieee80211_sta *sta, void *priv_sta,
722 struct sk_buff *skb)
723{
724 int legacy_success;
725 int retries;
726 int rs_index, mac_index, i;
727 struct iwl_lq_sta *lq_sta = priv_sta;
728 struct iwl_lq_cmd *table;
729 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
730 struct iwl_op_mode *op_mode = (struct iwl_op_mode *)mvm_r;
731 struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
732 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
733 enum mac80211_rate_control_flags mac_flags;
734 u32 tx_rate;
735 struct iwl_scale_tbl_info tbl_type;
736 struct iwl_scale_tbl_info *curr_tbl, *other_tbl, *tmp_tbl;
737
738 IWL_DEBUG_RATE_LIMIT(mvm,
739 "get frame ack response, update rate scale window\n");
740
741 /* Treat uninitialized rate scaling data same as non-existing. */
742 if (!lq_sta) {
743 IWL_DEBUG_RATE(mvm, "Station rate scaling not created yet.\n");
744 return;
745 } else if (!lq_sta->drv) {
746 IWL_DEBUG_RATE(mvm, "Rate scaling not initialized yet.\n");
747 return;
748 }
749
750 if (!ieee80211_is_data(hdr->frame_control) ||
751 info->flags & IEEE80211_TX_CTL_NO_ACK)
752 return;
753
754 /* This packet was aggregated but doesn't carry status info */
755 if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
756 !(info->flags & IEEE80211_TX_STAT_AMPDU))
757 return;
758
759 /*
760 * Ignore this Tx frame response if its initial rate doesn't match
761 * that of latest Link Quality command. There may be stragglers
762 * from a previous Link Quality command, but we're no longer interested
763 * in those; they're either from the "active" mode while we're trying
764 * to check "search" mode, or a prior "search" mode after we've moved
765 * to a new "search" mode (which might become the new "active" mode).
766 */
767 table = &lq_sta->lq;
768 tx_rate = le32_to_cpu(table->rs_table[0]);
769 rs_get_tbl_info_from_mcs(tx_rate, info->band, &tbl_type, &rs_index);
770 if (info->band == IEEE80211_BAND_5GHZ)
771 rs_index -= IWL_FIRST_OFDM_RATE;
772 mac_flags = info->status.rates[0].flags;
773 mac_index = info->status.rates[0].idx;
774 /* For HT packets, map MCS to PLCP */
775 if (mac_flags & IEEE80211_TX_RC_MCS) {
776 /* Remove # of streams */
777 mac_index &= RATE_HT_MCS_RATE_CODE_MSK;
778 if (mac_index >= (IWL_RATE_9M_INDEX - IWL_FIRST_OFDM_RATE))
779 mac_index++;
780 /*
781 * mac80211 HT index is always zero-indexed; we need to move
782 * HT OFDM rates after CCK rates in 2.4 GHz band
783 */
784 if (info->band == IEEE80211_BAND_2GHZ)
785 mac_index += IWL_FIRST_OFDM_RATE;
786 }
787 /* Here we actually compare this rate to the latest LQ command */
788 if ((mac_index < 0) ||
789 (tbl_type.is_SGI != !!(mac_flags & IEEE80211_TX_RC_SHORT_GI)) ||
790 (tbl_type.is_ht40 != !!(mac_flags & IEEE80211_TX_RC_40_MHZ_WIDTH)) ||
791 (tbl_type.ant_type != info->status.antenna) ||
792 (!!(tx_rate & RATE_MCS_HT_MSK) !=
793 !!(mac_flags & IEEE80211_TX_RC_MCS)) ||
794 (!!(tx_rate & RATE_HT_MCS_GF_MSK) !=
795 !!(mac_flags & IEEE80211_TX_RC_GREEN_FIELD)) ||
796 (rs_index != mac_index)) {
797 IWL_DEBUG_RATE(mvm,
798 "initial rate %d does not match %d (0x%x)\n",
799 mac_index, rs_index, tx_rate);
800 /*
801 * Since rates mis-match, the last LQ command may have failed.
802 * After IWL_MISSED_RATE_MAX mis-matches, resync the uCode with
803 * ... driver.
804 */
805 lq_sta->missed_rate_counter++;
806 if (lq_sta->missed_rate_counter > IWL_MISSED_RATE_MAX) {
807 lq_sta->missed_rate_counter = 0;
808 iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, CMD_ASYNC, false);
809 }
810 /* Regardless, ignore this status info for outdated rate */
811 return;
812 } else
813 /* Rate did match, so reset the missed_rate_counter */
814 lq_sta->missed_rate_counter = 0;
815
816 /* Figure out if rate scale algorithm is in active or search table */
817 if (table_type_matches(&tbl_type,
818 &(lq_sta->lq_info[lq_sta->active_tbl]))) {
819 curr_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
820 other_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
821 } else if (table_type_matches(
822 &tbl_type, &lq_sta->lq_info[1 - lq_sta->active_tbl])) {
823 curr_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
824 other_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
825 } else {
826 IWL_DEBUG_RATE(mvm,
827 "Neither active nor search matches tx rate\n");
828 tmp_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
829 IWL_DEBUG_RATE(mvm, "active- lq:%x, ant:%x, SGI:%d\n",
830 tmp_tbl->lq_type, tmp_tbl->ant_type,
831 tmp_tbl->is_SGI);
832 tmp_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
833 IWL_DEBUG_RATE(mvm, "search- lq:%x, ant:%x, SGI:%d\n",
834 tmp_tbl->lq_type, tmp_tbl->ant_type,
835 tmp_tbl->is_SGI);
836 IWL_DEBUG_RATE(mvm, "actual- lq:%x, ant:%x, SGI:%d\n",
837 tbl_type.lq_type, tbl_type.ant_type,
838 tbl_type.is_SGI);
839 /*
840 * no matching table found, let's by-pass the data collection
841 * and continue to perform rate scale to find the rate table
842 */
843 rs_stay_in_table(lq_sta, true);
844 goto done;
845 }
846
847 /*
848 * Updating the frame history depends on whether packets were
849 * aggregated.
850 *
851 * For aggregation, all packets were transmitted at the same rate, the
852 * first index into rate scale table.
853 */
854 if (info->flags & IEEE80211_TX_STAT_AMPDU) {
855 tx_rate = le32_to_cpu(table->rs_table[0]);
856 rs_get_tbl_info_from_mcs(tx_rate, info->band, &tbl_type,
857 &rs_index);
858 rs_collect_tx_data(curr_tbl, rs_index,
859 info->status.ampdu_len,
860 info->status.ampdu_ack_len);
861
862 /* Update success/fail counts if not searching for new mode */
863 if (lq_sta->stay_in_tbl) {
864 lq_sta->total_success += info->status.ampdu_ack_len;
865 lq_sta->total_failed += (info->status.ampdu_len -
866 info->status.ampdu_ack_len);
867 }
868 } else {
869 /*
870 * For legacy, update frame history with for each Tx retry.
871 */
872 retries = info->status.rates[0].count - 1;
873 /* HW doesn't send more than 15 retries */
874 retries = min(retries, 15);
875
876 /* The last transmission may have been successful */
877 legacy_success = !!(info->flags & IEEE80211_TX_STAT_ACK);
878 /* Collect data for each rate used during failed TX attempts */
879 for (i = 0; i <= retries; ++i) {
880 tx_rate = le32_to_cpu(table->rs_table[i]);
881 rs_get_tbl_info_from_mcs(tx_rate, info->band,
882 &tbl_type, &rs_index);
883 /*
884 * Only collect stats if retried rate is in the same RS
885 * table as active/search.
886 */
887 if (table_type_matches(&tbl_type, curr_tbl))
888 tmp_tbl = curr_tbl;
889 else if (table_type_matches(&tbl_type, other_tbl))
890 tmp_tbl = other_tbl;
891 else
892 continue;
893 rs_collect_tx_data(tmp_tbl, rs_index, 1,
894 i < retries ? 0 : legacy_success);
895 }
896
897 /* Update success/fail counts if not searching for new mode */
898 if (lq_sta->stay_in_tbl) {
899 lq_sta->total_success += legacy_success;
900 lq_sta->total_failed += retries + (1 - legacy_success);
901 }
902 }
903 /* The last TX rate is cached in lq_sta; it's set in if/else above */
904 lq_sta->last_rate_n_flags = tx_rate;
905done:
906 /* See if there's a better rate or modulation mode to try. */
907 if (sta && sta->supp_rates[sband->band])
908 rs_rate_scale_perform(mvm, skb, sta, lq_sta);
909}
910
911/*
912 * Begin a period of staying with a selected modulation mode.
913 * Set "stay_in_tbl" flag to prevent any mode switches.
914 * Set frame tx success limits according to legacy vs. high-throughput,
915 * and reset overall (spanning all rates) tx success history statistics.
916 * These control how long we stay using same modulation mode before
917 * searching for a new mode.
918 */
919static void rs_set_stay_in_table(struct iwl_mvm *mvm, u8 is_legacy,
920 struct iwl_lq_sta *lq_sta)
921{
922 IWL_DEBUG_RATE(mvm, "we are staying in the same table\n");
923 lq_sta->stay_in_tbl = 1; /* only place this gets set */
924 if (is_legacy) {
925 lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT;
926 lq_sta->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
927 lq_sta->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
928 } else {
929 lq_sta->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
930 lq_sta->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
931 lq_sta->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
932 }
933 lq_sta->table_count = 0;
934 lq_sta->total_failed = 0;
935 lq_sta->total_success = 0;
936 lq_sta->flush_timer = jiffies;
937 lq_sta->action_counter = 0;
938}
939
940/*
941 * Find correct throughput table for given mode of modulation
942 */
943static void rs_set_expected_tpt_table(struct iwl_lq_sta *lq_sta,
944 struct iwl_scale_tbl_info *tbl)
945{
946 /* Used to choose among HT tables */
947 s32 (*ht_tbl_pointer)[IWL_RATE_COUNT];
948
949 /* Check for invalid LQ type */
950 if (WARN_ON_ONCE(!is_legacy(tbl->lq_type) && !is_Ht(tbl->lq_type))) {
951 tbl->expected_tpt = expected_tpt_legacy;
952 return;
953 }
954
955 /* Legacy rates have only one table */
956 if (is_legacy(tbl->lq_type)) {
957 tbl->expected_tpt = expected_tpt_legacy;
958 return;
959 }
960
961 /* Choose among many HT tables depending on number of streams
Eyal Shapirad972ab32013-07-28 23:02:45 +0000962 * (SISO/MIMO2), channel width (20/40), SGI, and aggregation
Johannes Berg8ca151b2013-01-24 14:25:36 +0100963 * status */
964 if (is_siso(tbl->lq_type) && !tbl->is_ht40)
965 ht_tbl_pointer = expected_tpt_siso20MHz;
966 else if (is_siso(tbl->lq_type))
967 ht_tbl_pointer = expected_tpt_siso40MHz;
968 else if (is_mimo2(tbl->lq_type) && !tbl->is_ht40)
969 ht_tbl_pointer = expected_tpt_mimo2_20MHz;
Eyal Shapirad972ab32013-07-28 23:02:45 +0000970 else {
971 WARN_ON_ONCE(!is_mimo2(tbl->lq_type));
Johannes Berg8ca151b2013-01-24 14:25:36 +0100972 ht_tbl_pointer = expected_tpt_mimo2_40MHz;
Eyal Shapirad972ab32013-07-28 23:02:45 +0000973 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100974
975 if (!tbl->is_SGI && !lq_sta->is_agg) /* Normal */
976 tbl->expected_tpt = ht_tbl_pointer[0];
977 else if (tbl->is_SGI && !lq_sta->is_agg) /* SGI */
978 tbl->expected_tpt = ht_tbl_pointer[1];
979 else if (!tbl->is_SGI && lq_sta->is_agg) /* AGG */
980 tbl->expected_tpt = ht_tbl_pointer[2];
981 else /* AGG+SGI */
982 tbl->expected_tpt = ht_tbl_pointer[3];
983}
984
985/*
986 * Find starting rate for new "search" high-throughput mode of modulation.
987 * Goal is to find lowest expected rate (under perfect conditions) that is
988 * above the current measured throughput of "active" mode, to give new mode
989 * a fair chance to prove itself without too many challenges.
990 *
991 * This gets called when transitioning to more aggressive modulation
992 * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
993 * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
994 * to decrease to match "active" throughput. When moving from MIMO to SISO,
995 * bit rate will typically need to increase, but not if performance was bad.
996 */
997static s32 rs_get_best_rate(struct iwl_mvm *mvm,
998 struct iwl_lq_sta *lq_sta,
999 struct iwl_scale_tbl_info *tbl, /* "search" */
1000 u16 rate_mask, s8 index)
1001{
1002 /* "active" values */
1003 struct iwl_scale_tbl_info *active_tbl =
1004 &(lq_sta->lq_info[lq_sta->active_tbl]);
1005 s32 active_sr = active_tbl->win[index].success_ratio;
1006 s32 active_tpt = active_tbl->expected_tpt[index];
1007
1008 /* expected "search" throughput */
1009 s32 *tpt_tbl = tbl->expected_tpt;
1010
1011 s32 new_rate, high, low, start_hi;
1012 u16 high_low;
1013 s8 rate = index;
1014
1015 new_rate = high = low = start_hi = IWL_RATE_INVALID;
1016
1017 while (1) {
1018 high_low = rs_get_adjacent_rate(mvm, rate, rate_mask,
1019 tbl->lq_type);
1020
1021 low = high_low & 0xff;
1022 high = (high_low >> 8) & 0xff;
1023
1024 /*
1025 * Lower the "search" bit rate, to give new "search" mode
1026 * approximately the same throughput as "active" if:
1027 *
1028 * 1) "Active" mode has been working modestly well (but not
1029 * great), and expected "search" throughput (under perfect
1030 * conditions) at candidate rate is above the actual
1031 * measured "active" throughput (but less than expected
1032 * "active" throughput under perfect conditions).
1033 * OR
1034 * 2) "Active" mode has been working perfectly or very well
1035 * and expected "search" throughput (under perfect
1036 * conditions) at candidate rate is above expected
1037 * "active" throughput (under perfect conditions).
1038 */
1039 if ((((100 * tpt_tbl[rate]) > lq_sta->last_tpt) &&
1040 ((active_sr > IWL_RATE_DECREASE_TH) &&
1041 (active_sr <= IWL_RATE_HIGH_TH) &&
1042 (tpt_tbl[rate] <= active_tpt))) ||
1043 ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
1044 (tpt_tbl[rate] > active_tpt))) {
1045 /* (2nd or later pass)
1046 * If we've already tried to raise the rate, and are
1047 * now trying to lower it, use the higher rate. */
1048 if (start_hi != IWL_RATE_INVALID) {
1049 new_rate = start_hi;
1050 break;
1051 }
1052
1053 new_rate = rate;
1054
1055 /* Loop again with lower rate */
1056 if (low != IWL_RATE_INVALID)
1057 rate = low;
1058
1059 /* Lower rate not available, use the original */
1060 else
1061 break;
1062
1063 /* Else try to raise the "search" rate to match "active" */
1064 } else {
1065 /* (2nd or later pass)
1066 * If we've already tried to lower the rate, and are
1067 * now trying to raise it, use the lower rate. */
1068 if (new_rate != IWL_RATE_INVALID)
1069 break;
1070
1071 /* Loop again with higher rate */
1072 else if (high != IWL_RATE_INVALID) {
1073 start_hi = high;
1074 rate = high;
1075
1076 /* Higher rate not available, use the original */
1077 } else {
1078 new_rate = rate;
1079 break;
1080 }
1081 }
1082 }
1083
1084 return new_rate;
1085}
1086
Johannes Berge1a0c6b2013-02-07 11:47:44 +01001087static bool iwl_is_ht40_tx_allowed(struct ieee80211_sta *sta)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001088{
Johannes Berge1a0c6b2013-02-07 11:47:44 +01001089 return sta->bandwidth >= IEEE80211_STA_RX_BW_40;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001090}
1091
Eyal Shapira4837b442013-07-28 23:02:46 +00001092/* Move to the next action and wrap around to the first action in case
1093 * we're at the last action. Assumes actions start at 0.
1094 */
1095static inline void rs_move_next_action(struct iwl_scale_tbl_info *tbl,
1096 u8 last_action)
1097{
1098 BUILD_BUG_ON(IWL_LEGACY_FIRST_ACTION != 0);
1099 BUILD_BUG_ON(IWL_SISO_FIRST_ACTION != 0);
1100 BUILD_BUG_ON(IWL_MIMO2_FIRST_ACTION != 0);
1101
1102 tbl->action = (tbl->action + 1) % (last_action + 1);
1103}
1104
Johannes Berg8ca151b2013-01-24 14:25:36 +01001105/*
1106 * Set up search table for MIMO2
1107 */
1108static int rs_switch_to_mimo2(struct iwl_mvm *mvm,
1109 struct iwl_lq_sta *lq_sta,
1110 struct ieee80211_sta *sta,
1111 struct iwl_scale_tbl_info *tbl, int index)
1112{
1113 u16 rate_mask;
1114 s32 rate;
1115 s8 is_green = lq_sta->is_green;
1116
1117 if (!sta->ht_cap.ht_supported)
1118 return -1;
1119
Johannes Bergaf0ed692013-02-12 14:21:00 +01001120 if (sta->smps_mode == IEEE80211_SMPS_STATIC)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001121 return -1;
1122
1123 /* Need both Tx chains/antennas to support MIMO */
Emmanuel Grumbachff402312013-04-03 20:10:06 +03001124 if (num_of_ant(iwl_fw_valid_tx_ant(mvm->fw)) < 2)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001125 return -1;
1126
1127 IWL_DEBUG_RATE(mvm, "LQ: try to switch to MIMO2\n");
1128
1129 tbl->lq_type = LQ_MIMO2;
1130 tbl->action = 0;
1131 tbl->max_search = IWL_MAX_SEARCH;
1132 rate_mask = lq_sta->active_mimo2_rate;
1133
Johannes Berge1a0c6b2013-02-07 11:47:44 +01001134 if (iwl_is_ht40_tx_allowed(sta))
Johannes Berg8ca151b2013-01-24 14:25:36 +01001135 tbl->is_ht40 = 1;
1136 else
1137 tbl->is_ht40 = 0;
1138
1139 rs_set_expected_tpt_table(lq_sta, tbl);
1140
1141 rate = rs_get_best_rate(mvm, lq_sta, tbl, rate_mask, index);
1142
1143 IWL_DEBUG_RATE(mvm, "LQ: MIMO2 best rate %d mask %X\n",
1144 rate, rate_mask);
1145 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1146 IWL_DEBUG_RATE(mvm, "Can't switch with index %d rate mask %x\n",
1147 rate, rate_mask);
1148 return -1;
1149 }
1150 tbl->current_rate = rate_n_flags_from_tbl(mvm, tbl, rate, is_green);
1151
1152 IWL_DEBUG_RATE(mvm, "LQ: Switch to new mcs %X index is green %X\n",
1153 tbl->current_rate, is_green);
1154 return 0;
1155}
1156
1157/*
Johannes Berg8ca151b2013-01-24 14:25:36 +01001158 * Set up search table for SISO
1159 */
1160static int rs_switch_to_siso(struct iwl_mvm *mvm,
1161 struct iwl_lq_sta *lq_sta,
1162 struct ieee80211_sta *sta,
1163 struct iwl_scale_tbl_info *tbl, int index)
1164{
1165 u16 rate_mask;
1166 u8 is_green = lq_sta->is_green;
1167 s32 rate;
1168
1169 if (!sta->ht_cap.ht_supported)
1170 return -1;
1171
1172 IWL_DEBUG_RATE(mvm, "LQ: try to switch to SISO\n");
1173
1174 tbl->lq_type = LQ_SISO;
1175 tbl->action = 0;
1176 tbl->max_search = IWL_MAX_SEARCH;
1177 rate_mask = lq_sta->active_siso_rate;
1178
Johannes Berge1a0c6b2013-02-07 11:47:44 +01001179 if (iwl_is_ht40_tx_allowed(sta))
Johannes Berg8ca151b2013-01-24 14:25:36 +01001180 tbl->is_ht40 = 1;
1181 else
1182 tbl->is_ht40 = 0;
1183
1184 if (is_green)
1185 tbl->is_SGI = 0; /*11n spec: no SGI in SISO+Greenfield*/
1186
1187 rs_set_expected_tpt_table(lq_sta, tbl);
1188 rate = rs_get_best_rate(mvm, lq_sta, tbl, rate_mask, index);
1189
1190 IWL_DEBUG_RATE(mvm, "LQ: get best rate %d mask %X\n", rate, rate_mask);
1191 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1192 IWL_DEBUG_RATE(mvm,
1193 "can not switch with index %d rate mask %x\n",
1194 rate, rate_mask);
1195 return -1;
1196 }
1197 tbl->current_rate = rate_n_flags_from_tbl(mvm, tbl, rate, is_green);
1198 IWL_DEBUG_RATE(mvm, "LQ: Switch to new mcs %X index is green %X\n",
1199 tbl->current_rate, is_green);
1200 return 0;
1201}
1202
1203/*
1204 * Try to switch to new modulation mode from legacy
1205 */
1206static int rs_move_legacy_other(struct iwl_mvm *mvm,
1207 struct iwl_lq_sta *lq_sta,
1208 struct ieee80211_sta *sta,
1209 int index)
1210{
1211 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1212 struct iwl_scale_tbl_info *search_tbl =
1213 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1214 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1215 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1216 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1217 u8 start_action;
Emmanuel Grumbachff402312013-04-03 20:10:06 +03001218 u8 valid_tx_ant = iwl_fw_valid_tx_ant(mvm->fw);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001219 u8 tx_chains_num = num_of_ant(valid_tx_ant);
1220 int ret;
1221 u8 update_search_tbl_counter = 0;
1222
1223 start_action = tbl->action;
1224 while (1) {
1225 lq_sta->action_counter++;
1226 switch (tbl->action) {
Eyal Shapira4837b442013-07-28 23:02:46 +00001227 case IWL_LEGACY_SWITCH_ANTENNA:
Johannes Berg8ca151b2013-01-24 14:25:36 +01001228 IWL_DEBUG_RATE(mvm, "LQ: Legacy toggle Antenna\n");
1229
Eyal Shapira4837b442013-07-28 23:02:46 +00001230 if (tx_chains_num <= 1)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001231 break;
1232
1233 /* Don't change antenna if success has been great */
1234 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1235 break;
1236
1237 /* Set up search table to try other antenna */
1238 memcpy(search_tbl, tbl, sz);
1239
1240 if (rs_toggle_antenna(valid_tx_ant,
1241 &search_tbl->current_rate,
1242 search_tbl)) {
1243 update_search_tbl_counter = 1;
1244 rs_set_expected_tpt_table(lq_sta, search_tbl);
1245 goto out;
1246 }
1247 break;
1248 case IWL_LEGACY_SWITCH_SISO:
1249 IWL_DEBUG_RATE(mvm, "LQ: Legacy switch to SISO\n");
1250
1251 /* Set up search table to try SISO */
1252 memcpy(search_tbl, tbl, sz);
1253 search_tbl->is_SGI = 0;
1254 ret = rs_switch_to_siso(mvm, lq_sta, sta,
1255 search_tbl, index);
1256 if (!ret) {
1257 lq_sta->action_counter = 0;
1258 goto out;
1259 }
1260
1261 break;
Eyal Shapirad972ab32013-07-28 23:02:45 +00001262 case IWL_LEGACY_SWITCH_MIMO2:
Johannes Berg8ca151b2013-01-24 14:25:36 +01001263 IWL_DEBUG_RATE(mvm, "LQ: Legacy switch to MIMO2\n");
1264
1265 /* Set up search table to try MIMO */
1266 memcpy(search_tbl, tbl, sz);
1267 search_tbl->is_SGI = 0;
1268
Eyal Shapirad972ab32013-07-28 23:02:45 +00001269 search_tbl->ant_type = ANT_AB;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001270
1271 if (!rs_is_valid_ant(valid_tx_ant,
1272 search_tbl->ant_type))
1273 break;
1274
1275 ret = rs_switch_to_mimo2(mvm, lq_sta, sta,
1276 search_tbl, index);
1277 if (!ret) {
1278 lq_sta->action_counter = 0;
1279 goto out;
1280 }
1281 break;
Eyal Shapirad972ab32013-07-28 23:02:45 +00001282 default:
1283 WARN_ON_ONCE(1);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001284 }
Eyal Shapira4837b442013-07-28 23:02:46 +00001285 rs_move_next_action(tbl, IWL_LEGACY_LAST_ACTION);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001286
1287 if (tbl->action == start_action)
1288 break;
1289 }
1290 search_tbl->lq_type = LQ_NONE;
1291 return 0;
1292
1293out:
1294 lq_sta->search_better_tbl = 1;
Eyal Shapira4837b442013-07-28 23:02:46 +00001295 rs_move_next_action(tbl, IWL_LEGACY_LAST_ACTION);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001296 if (update_search_tbl_counter)
1297 search_tbl->action = tbl->action;
1298 return 0;
1299}
1300
1301/*
1302 * Try to switch to new modulation mode from SISO
1303 */
1304static int rs_move_siso_to_other(struct iwl_mvm *mvm,
1305 struct iwl_lq_sta *lq_sta,
1306 struct ieee80211_sta *sta, int index)
1307{
1308 u8 is_green = lq_sta->is_green;
1309 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1310 struct iwl_scale_tbl_info *search_tbl =
1311 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1312 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1313 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
1314 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1315 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1316 u8 start_action;
Emmanuel Grumbachff402312013-04-03 20:10:06 +03001317 u8 valid_tx_ant = iwl_fw_valid_tx_ant(mvm->fw);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001318 u8 tx_chains_num = num_of_ant(valid_tx_ant);
1319 u8 update_search_tbl_counter = 0;
1320 int ret;
1321
Emmanuel Grumbach36946ce2013-05-28 23:12:47 +03001322 switch (BT_MBOX_MSG(&mvm->last_bt_notif, 3, TRAFFIC_LOAD)) {
1323 case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
1324 /* nothing */
1325 break;
1326 case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
1327 /* avoid antenna B unless MIMO */
Eyal Shapira4837b442013-07-28 23:02:46 +00001328 if (tbl->action == IWL_SISO_SWITCH_ANTENNA)
Eyal Shapirad972ab32013-07-28 23:02:45 +00001329 tbl->action = IWL_SISO_SWITCH_MIMO2;
Emmanuel Grumbach36946ce2013-05-28 23:12:47 +03001330 break;
1331 case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
1332 case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
1333 /* avoid antenna B and MIMO */
1334 valid_tx_ant =
1335 first_antenna(iwl_fw_valid_tx_ant(mvm->fw));
Eyal Shapira4837b442013-07-28 23:02:46 +00001336 if (tbl->action != IWL_SISO_SWITCH_ANTENNA)
1337 tbl->action = IWL_SISO_SWITCH_ANTENNA;
Emmanuel Grumbach36946ce2013-05-28 23:12:47 +03001338 break;
1339 default:
1340 IWL_ERR(mvm, "Invalid BT load %d",
1341 BT_MBOX_MSG(&mvm->last_bt_notif, 3, TRAFFIC_LOAD));
1342 break;
1343 }
1344
Johannes Berg8ca151b2013-01-24 14:25:36 +01001345 start_action = tbl->action;
1346 while (1) {
1347 lq_sta->action_counter++;
1348 switch (tbl->action) {
Eyal Shapira4837b442013-07-28 23:02:46 +00001349 case IWL_SISO_SWITCH_ANTENNA:
Johannes Berg8ca151b2013-01-24 14:25:36 +01001350 IWL_DEBUG_RATE(mvm, "LQ: SISO toggle Antenna\n");
Eyal Shapira4837b442013-07-28 23:02:46 +00001351 if (tx_chains_num <= 1)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001352 break;
1353
Emmanuel Grumbach36946ce2013-05-28 23:12:47 +03001354 if (window->success_ratio >= IWL_RS_GOOD_RATIO &&
1355 BT_MBOX_MSG(&mvm->last_bt_notif, 3,
1356 TRAFFIC_LOAD) == 0)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001357 break;
1358
1359 memcpy(search_tbl, tbl, sz);
1360 if (rs_toggle_antenna(valid_tx_ant,
1361 &search_tbl->current_rate,
1362 search_tbl)) {
1363 update_search_tbl_counter = 1;
1364 goto out;
1365 }
1366 break;
Eyal Shapirad972ab32013-07-28 23:02:45 +00001367 case IWL_SISO_SWITCH_MIMO2:
Johannes Berg8ca151b2013-01-24 14:25:36 +01001368 IWL_DEBUG_RATE(mvm, "LQ: SISO switch to MIMO2\n");
1369 memcpy(search_tbl, tbl, sz);
1370 search_tbl->is_SGI = 0;
1371
Eyal Shapirad972ab32013-07-28 23:02:45 +00001372 search_tbl->ant_type = ANT_AB;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001373
1374 if (!rs_is_valid_ant(valid_tx_ant,
1375 search_tbl->ant_type))
1376 break;
1377
1378 ret = rs_switch_to_mimo2(mvm, lq_sta, sta,
1379 search_tbl, index);
1380 if (!ret)
1381 goto out;
1382 break;
1383 case IWL_SISO_SWITCH_GI:
1384 if (!tbl->is_ht40 && !(ht_cap->cap &
1385 IEEE80211_HT_CAP_SGI_20))
1386 break;
1387 if (tbl->is_ht40 && !(ht_cap->cap &
1388 IEEE80211_HT_CAP_SGI_40))
1389 break;
1390
1391 IWL_DEBUG_RATE(mvm, "LQ: SISO toggle SGI/NGI\n");
1392
1393 memcpy(search_tbl, tbl, sz);
1394 if (is_green) {
1395 if (!tbl->is_SGI)
1396 break;
1397 else
1398 IWL_ERR(mvm,
1399 "SGI was set in GF+SISO\n");
1400 }
1401 search_tbl->is_SGI = !tbl->is_SGI;
1402 rs_set_expected_tpt_table(lq_sta, search_tbl);
1403 if (tbl->is_SGI) {
1404 s32 tpt = lq_sta->last_tpt / 100;
1405 if (tpt >= search_tbl->expected_tpt[index])
1406 break;
1407 }
1408 search_tbl->current_rate =
1409 rate_n_flags_from_tbl(mvm, search_tbl,
1410 index, is_green);
1411 update_search_tbl_counter = 1;
1412 goto out;
Eyal Shapirad972ab32013-07-28 23:02:45 +00001413 default:
1414 WARN_ON_ONCE(1);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001415 }
Eyal Shapira4837b442013-07-28 23:02:46 +00001416 rs_move_next_action(tbl, IWL_SISO_LAST_ACTION);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001417
1418 if (tbl->action == start_action)
1419 break;
1420 }
1421 search_tbl->lq_type = LQ_NONE;
1422 return 0;
1423
1424 out:
1425 lq_sta->search_better_tbl = 1;
Eyal Shapira4837b442013-07-28 23:02:46 +00001426 rs_move_next_action(tbl, IWL_SISO_LAST_ACTION);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001427 if (update_search_tbl_counter)
1428 search_tbl->action = tbl->action;
1429
1430 return 0;
1431}
1432
1433/*
1434 * Try to switch to new modulation mode from MIMO2
1435 */
1436static int rs_move_mimo2_to_other(struct iwl_mvm *mvm,
1437 struct iwl_lq_sta *lq_sta,
1438 struct ieee80211_sta *sta, int index)
1439{
1440 s8 is_green = lq_sta->is_green;
1441 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1442 struct iwl_scale_tbl_info *search_tbl =
1443 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001444 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
1445 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1446 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1447 u8 start_action;
Emmanuel Grumbachff402312013-04-03 20:10:06 +03001448 u8 valid_tx_ant = iwl_fw_valid_tx_ant(mvm->fw);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001449 u8 update_search_tbl_counter = 0;
1450 int ret;
1451
Emmanuel Grumbach36946ce2013-05-28 23:12:47 +03001452 switch (BT_MBOX_MSG(&mvm->last_bt_notif, 3, TRAFFIC_LOAD)) {
1453 case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
1454 /* nothing */
1455 break;
1456 case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
1457 case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
1458 /* avoid antenna B and MIMO */
1459 if (tbl->action != IWL_MIMO2_SWITCH_SISO_A)
1460 tbl->action = IWL_MIMO2_SWITCH_SISO_A;
1461 break;
1462 case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
1463 /* avoid antenna B unless MIMO */
Eyal Shapirad972ab32013-07-28 23:02:45 +00001464 if (tbl->action == IWL_MIMO2_SWITCH_SISO_B)
Emmanuel Grumbach36946ce2013-05-28 23:12:47 +03001465 tbl->action = IWL_MIMO2_SWITCH_SISO_A;
1466 break;
1467 default:
1468 IWL_ERR(mvm, "Invalid BT load %d",
1469 BT_MBOX_MSG(&mvm->last_bt_notif, 3, TRAFFIC_LOAD));
1470 break;
1471 }
1472
Johannes Berg8ca151b2013-01-24 14:25:36 +01001473 start_action = tbl->action;
1474 while (1) {
1475 lq_sta->action_counter++;
1476 switch (tbl->action) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001477 case IWL_MIMO2_SWITCH_SISO_A:
1478 case IWL_MIMO2_SWITCH_SISO_B:
Johannes Berg8ca151b2013-01-24 14:25:36 +01001479 IWL_DEBUG_RATE(mvm, "LQ: MIMO2 switch to SISO\n");
1480
1481 /* Set up new search table for SISO */
1482 memcpy(search_tbl, tbl, sz);
1483
1484 if (tbl->action == IWL_MIMO2_SWITCH_SISO_A)
1485 search_tbl->ant_type = ANT_A;
Eyal Shapirad972ab32013-07-28 23:02:45 +00001486 else /* tbl->action == IWL_MIMO2_SWITCH_SISO_B */
Johannes Berg8ca151b2013-01-24 14:25:36 +01001487 search_tbl->ant_type = ANT_B;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001488
1489 if (!rs_is_valid_ant(valid_tx_ant,
1490 search_tbl->ant_type))
1491 break;
1492
1493 ret = rs_switch_to_siso(mvm, lq_sta, sta,
1494 search_tbl, index);
1495 if (!ret)
1496 goto out;
1497
1498 break;
1499
1500 case IWL_MIMO2_SWITCH_GI:
1501 if (!tbl->is_ht40 && !(ht_cap->cap &
1502 IEEE80211_HT_CAP_SGI_20))
1503 break;
1504 if (tbl->is_ht40 && !(ht_cap->cap &
1505 IEEE80211_HT_CAP_SGI_40))
1506 break;
1507
1508 IWL_DEBUG_RATE(mvm, "LQ: MIMO2 toggle SGI/NGI\n");
1509
1510 /* Set up new search table for MIMO2 */
1511 memcpy(search_tbl, tbl, sz);
1512 search_tbl->is_SGI = !tbl->is_SGI;
1513 rs_set_expected_tpt_table(lq_sta, search_tbl);
1514 /*
1515 * If active table already uses the fastest possible
1516 * modulation (dual stream with short guard interval),
1517 * and it's working well, there's no need to look
1518 * for a better type of modulation!
1519 */
1520 if (tbl->is_SGI) {
1521 s32 tpt = lq_sta->last_tpt / 100;
1522 if (tpt >= search_tbl->expected_tpt[index])
1523 break;
1524 }
1525 search_tbl->current_rate =
1526 rate_n_flags_from_tbl(mvm, search_tbl,
1527 index, is_green);
1528 update_search_tbl_counter = 1;
1529 goto out;
Eyal Shapirad972ab32013-07-28 23:02:45 +00001530 default:
1531 WARN_ON_ONCE(1);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001532 }
Eyal Shapira4837b442013-07-28 23:02:46 +00001533 rs_move_next_action(tbl, IWL_MIMO2_LAST_ACTION);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001534
1535 if (tbl->action == start_action)
1536 break;
1537 }
1538 search_tbl->lq_type = LQ_NONE;
1539 return 0;
1540 out:
1541 lq_sta->search_better_tbl = 1;
Eyal Shapira4837b442013-07-28 23:02:46 +00001542 rs_move_next_action(tbl, IWL_MIMO2_LAST_ACTION);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001543 if (update_search_tbl_counter)
1544 search_tbl->action = tbl->action;
1545
1546 return 0;
1547}
1548
1549/*
Johannes Berg8ca151b2013-01-24 14:25:36 +01001550 * Check whether we should continue using same modulation mode, or
1551 * begin search for a new mode, based on:
1552 * 1) # tx successes or failures while using this mode
1553 * 2) # times calling this function
1554 * 3) elapsed time in this mode (not used, for now)
1555 */
1556static void rs_stay_in_table(struct iwl_lq_sta *lq_sta, bool force_search)
1557{
1558 struct iwl_scale_tbl_info *tbl;
1559 int i;
1560 int active_tbl;
1561 int flush_interval_passed = 0;
1562 struct iwl_mvm *mvm;
1563
1564 mvm = lq_sta->drv;
1565 active_tbl = lq_sta->active_tbl;
1566
1567 tbl = &(lq_sta->lq_info[active_tbl]);
1568
1569 /* If we've been disallowing search, see if we should now allow it */
1570 if (lq_sta->stay_in_tbl) {
1571 /* Elapsed time using current modulation mode */
1572 if (lq_sta->flush_timer)
1573 flush_interval_passed =
1574 time_after(jiffies,
1575 (unsigned long)(lq_sta->flush_timer +
1576 IWL_RATE_SCALE_FLUSH_INTVL));
1577
1578 /*
1579 * Check if we should allow search for new modulation mode.
1580 * If many frames have failed or succeeded, or we've used
1581 * this same modulation for a long time, allow search, and
1582 * reset history stats that keep track of whether we should
1583 * allow a new search. Also (below) reset all bitmaps and
1584 * stats in active history.
1585 */
1586 if (force_search ||
1587 (lq_sta->total_failed > lq_sta->max_failure_limit) ||
1588 (lq_sta->total_success > lq_sta->max_success_limit) ||
1589 ((!lq_sta->search_better_tbl) &&
1590 (lq_sta->flush_timer) && (flush_interval_passed))) {
1591 IWL_DEBUG_RATE(mvm,
1592 "LQ: stay is expired %d %d %d\n",
1593 lq_sta->total_failed,
1594 lq_sta->total_success,
1595 flush_interval_passed);
1596
1597 /* Allow search for new mode */
1598 lq_sta->stay_in_tbl = 0; /* only place reset */
1599 lq_sta->total_failed = 0;
1600 lq_sta->total_success = 0;
1601 lq_sta->flush_timer = 0;
1602 /*
1603 * Else if we've used this modulation mode enough repetitions
1604 * (regardless of elapsed time or success/failure), reset
1605 * history bitmaps and rate-specific stats for all rates in
1606 * active table.
1607 */
1608 } else {
1609 lq_sta->table_count++;
1610 if (lq_sta->table_count >=
1611 lq_sta->table_count_limit) {
1612 lq_sta->table_count = 0;
1613
1614 IWL_DEBUG_RATE(mvm,
1615 "LQ: stay in table clear win\n");
1616 for (i = 0; i < IWL_RATE_COUNT; i++)
1617 rs_rate_scale_clear_window(
1618 &(tbl->win[i]));
1619 }
1620 }
1621
1622 /* If transitioning to allow "search", reset all history
1623 * bitmaps and stats in active table (this will become the new
1624 * "search" table). */
1625 if (!lq_sta->stay_in_tbl) {
1626 for (i = 0; i < IWL_RATE_COUNT; i++)
1627 rs_rate_scale_clear_window(&(tbl->win[i]));
1628 }
1629 }
1630}
1631
1632/*
1633 * setup rate table in uCode
1634 */
1635static void rs_update_rate_tbl(struct iwl_mvm *mvm,
1636 struct iwl_lq_sta *lq_sta,
1637 struct iwl_scale_tbl_info *tbl,
1638 int index, u8 is_green)
1639{
1640 u32 rate;
1641
1642 /* Update uCode's rate table. */
1643 rate = rate_n_flags_from_tbl(mvm, tbl, index, is_green);
1644 rs_fill_link_cmd(mvm, lq_sta, rate);
1645 iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, CMD_ASYNC, false);
1646}
1647
Eyal Shapira977342b2013-07-28 23:02:44 +00001648static u8 rs_get_tid(struct iwl_lq_sta *lq_data,
1649 struct ieee80211_hdr *hdr)
1650{
1651 u8 tid = IWL_MAX_TID_COUNT;
1652
1653 if (ieee80211_is_data_qos(hdr->frame_control)) {
1654 u8 *qc = ieee80211_get_qos_ctl(hdr);
1655 tid = qc[0] & 0xf;
1656 }
1657
1658 if (unlikely(tid > IWL_MAX_TID_COUNT))
1659 tid = IWL_MAX_TID_COUNT;
1660
1661 return tid;
1662}
1663
Johannes Berg8ca151b2013-01-24 14:25:36 +01001664/*
1665 * Do rate scaling and search for new modulation mode.
1666 */
1667static void rs_rate_scale_perform(struct iwl_mvm *mvm,
1668 struct sk_buff *skb,
1669 struct ieee80211_sta *sta,
1670 struct iwl_lq_sta *lq_sta)
1671{
1672 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1673 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1674 int low = IWL_RATE_INVALID;
1675 int high = IWL_RATE_INVALID;
1676 int index;
1677 int i;
1678 struct iwl_rate_scale_data *window = NULL;
1679 int current_tpt = IWL_INVALID_VALUE;
1680 int low_tpt = IWL_INVALID_VALUE;
1681 int high_tpt = IWL_INVALID_VALUE;
1682 u32 fail_count;
1683 s8 scale_action = 0;
1684 u16 rate_mask;
1685 u8 update_lq = 0;
1686 struct iwl_scale_tbl_info *tbl, *tbl1;
1687 u16 rate_scale_index_msk = 0;
1688 u8 is_green = 0;
1689 u8 active_tbl = 0;
1690 u8 done_search = 0;
1691 u16 high_low;
1692 s32 sr;
1693 u8 tid = IWL_MAX_TID_COUNT;
1694 struct iwl_mvm_sta *sta_priv = (void *)sta->drv_priv;
1695 struct iwl_mvm_tid_data *tid_data;
1696
1697 IWL_DEBUG_RATE(mvm, "rate scale calculate new rate for skb\n");
1698
1699 /* Send management frames and NO_ACK data using lowest rate. */
1700 /* TODO: this could probably be improved.. */
1701 if (!ieee80211_is_data(hdr->frame_control) ||
1702 info->flags & IEEE80211_TX_CTL_NO_ACK)
1703 return;
1704
1705 lq_sta->supp_rates = sta->supp_rates[lq_sta->band];
1706
Eyal Shapira977342b2013-07-28 23:02:44 +00001707 tid = rs_get_tid(lq_sta, hdr);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001708 if ((tid != IWL_MAX_TID_COUNT) &&
1709 (lq_sta->tx_agg_tid_en & (1 << tid))) {
1710 tid_data = &sta_priv->tid_data[tid];
1711 if (tid_data->state == IWL_AGG_OFF)
1712 lq_sta->is_agg = 0;
1713 else
1714 lq_sta->is_agg = 1;
1715 } else {
1716 lq_sta->is_agg = 0;
1717 }
1718
1719 /*
1720 * Select rate-scale / modulation-mode table to work with in
1721 * the rest of this function: "search" if searching for better
1722 * modulation mode, or "active" if doing rate scaling within a mode.
1723 */
1724 if (!lq_sta->search_better_tbl)
1725 active_tbl = lq_sta->active_tbl;
1726 else
1727 active_tbl = 1 - lq_sta->active_tbl;
1728
1729 tbl = &(lq_sta->lq_info[active_tbl]);
1730 if (is_legacy(tbl->lq_type))
1731 lq_sta->is_green = 0;
1732 else
1733 lq_sta->is_green = rs_use_green(sta);
1734 is_green = lq_sta->is_green;
1735
1736 /* current tx rate */
1737 index = lq_sta->last_txrate_idx;
1738
1739 IWL_DEBUG_RATE(mvm, "Rate scale index %d for type %d\n", index,
1740 tbl->lq_type);
1741
1742 /* rates available for this association, and for modulation mode */
1743 rate_mask = rs_get_supported_rates(lq_sta, hdr, tbl->lq_type);
1744
1745 IWL_DEBUG_RATE(mvm, "mask 0x%04X\n", rate_mask);
1746
1747 /* mask with station rate restriction */
1748 if (is_legacy(tbl->lq_type)) {
1749 if (lq_sta->band == IEEE80211_BAND_5GHZ)
1750 /* supp_rates has no CCK bits in A mode */
1751 rate_scale_index_msk = (u16) (rate_mask &
1752 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
1753 else
1754 rate_scale_index_msk = (u16) (rate_mask &
1755 lq_sta->supp_rates);
1756
1757 } else {
1758 rate_scale_index_msk = rate_mask;
1759 }
1760
1761 if (!rate_scale_index_msk)
1762 rate_scale_index_msk = rate_mask;
1763
1764 if (!((1 << index) & rate_scale_index_msk)) {
1765 IWL_ERR(mvm, "Current Rate is not valid\n");
1766 if (lq_sta->search_better_tbl) {
1767 /* revert to active table if search table is not valid*/
1768 tbl->lq_type = LQ_NONE;
1769 lq_sta->search_better_tbl = 0;
1770 tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1771 /* get "active" rate info */
1772 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
1773 rs_update_rate_tbl(mvm, lq_sta, tbl, index, is_green);
1774 }
1775 return;
1776 }
1777
1778 /* Get expected throughput table and history window for current rate */
1779 if (!tbl->expected_tpt) {
1780 IWL_ERR(mvm, "tbl->expected_tpt is NULL\n");
1781 return;
1782 }
1783
1784 /* force user max rate if set by user */
1785 if ((lq_sta->max_rate_idx != -1) &&
1786 (lq_sta->max_rate_idx < index)) {
1787 index = lq_sta->max_rate_idx;
1788 update_lq = 1;
1789 window = &(tbl->win[index]);
1790 goto lq_update;
1791 }
1792
1793 window = &(tbl->win[index]);
1794
1795 /*
1796 * If there is not enough history to calculate actual average
1797 * throughput, keep analyzing results of more tx frames, without
1798 * changing rate or mode (bypass most of the rest of this function).
1799 * Set up new rate table in uCode only if old rate is not supported
1800 * in current association (use new rate found above).
1801 */
1802 fail_count = window->counter - window->success_counter;
1803 if ((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
1804 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH)) {
1805 IWL_DEBUG_RATE(mvm,
1806 "LQ: still below TH. succ=%d total=%d for index %d\n",
1807 window->success_counter, window->counter, index);
1808
1809 /* Can't calculate this yet; not enough history */
1810 window->average_tpt = IWL_INVALID_VALUE;
1811
1812 /* Should we stay with this modulation mode,
1813 * or search for a new one? */
1814 rs_stay_in_table(lq_sta, false);
1815
1816 goto out;
1817 }
1818 /* Else we have enough samples; calculate estimate of
1819 * actual average throughput */
1820 if (window->average_tpt != ((window->success_ratio *
1821 tbl->expected_tpt[index] + 64) / 128)) {
1822 IWL_ERR(mvm,
1823 "expected_tpt should have been calculated by now\n");
1824 window->average_tpt = ((window->success_ratio *
1825 tbl->expected_tpt[index] + 64) / 128);
1826 }
1827
1828 /* If we are searching for better modulation mode, check success. */
1829 if (lq_sta->search_better_tbl) {
1830 /* If good success, continue using the "search" mode;
1831 * no need to send new link quality command, since we're
1832 * continuing to use the setup that we've been trying. */
1833 if (window->average_tpt > lq_sta->last_tpt) {
1834 IWL_DEBUG_RATE(mvm,
1835 "LQ: SWITCHING TO NEW TABLE suc=%d cur-tpt=%d old-tpt=%d\n",
1836 window->success_ratio,
1837 window->average_tpt,
1838 lq_sta->last_tpt);
1839
1840 if (!is_legacy(tbl->lq_type))
1841 lq_sta->enable_counter = 1;
1842
1843 /* Swap tables; "search" becomes "active" */
1844 lq_sta->active_tbl = active_tbl;
1845 current_tpt = window->average_tpt;
1846 /* Else poor success; go back to mode in "active" table */
1847 } else {
1848 IWL_DEBUG_RATE(mvm,
1849 "LQ: GOING BACK TO THE OLD TABLE suc=%d cur-tpt=%d old-tpt=%d\n",
1850 window->success_ratio,
1851 window->average_tpt,
1852 lq_sta->last_tpt);
1853
1854 /* Nullify "search" table */
1855 tbl->lq_type = LQ_NONE;
1856
1857 /* Revert to "active" table */
1858 active_tbl = lq_sta->active_tbl;
1859 tbl = &(lq_sta->lq_info[active_tbl]);
1860
1861 /* Revert to "active" rate and throughput info */
1862 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
1863 current_tpt = lq_sta->last_tpt;
1864
1865 /* Need to set up a new rate table in uCode */
1866 update_lq = 1;
1867 }
1868
1869 /* Either way, we've made a decision; modulation mode
1870 * search is done, allow rate adjustment next time. */
1871 lq_sta->search_better_tbl = 0;
1872 done_search = 1; /* Don't switch modes below! */
1873 goto lq_update;
1874 }
1875
1876 /* (Else) not in search of better modulation mode, try for better
1877 * starting rate, while staying in this mode. */
1878 high_low = rs_get_adjacent_rate(mvm, index, rate_scale_index_msk,
1879 tbl->lq_type);
1880 low = high_low & 0xff;
1881 high = (high_low >> 8) & 0xff;
1882
1883 /* If user set max rate, dont allow higher than user constrain */
1884 if ((lq_sta->max_rate_idx != -1) &&
1885 (lq_sta->max_rate_idx < high))
1886 high = IWL_RATE_INVALID;
1887
1888 sr = window->success_ratio;
1889
1890 /* Collect measured throughputs for current and adjacent rates */
1891 current_tpt = window->average_tpt;
1892 if (low != IWL_RATE_INVALID)
1893 low_tpt = tbl->win[low].average_tpt;
1894 if (high != IWL_RATE_INVALID)
1895 high_tpt = tbl->win[high].average_tpt;
1896
1897 scale_action = 0;
1898
1899 /* Too many failures, decrease rate */
1900 if ((sr <= IWL_RATE_DECREASE_TH) || (current_tpt == 0)) {
1901 IWL_DEBUG_RATE(mvm,
1902 "decrease rate because of low success_ratio\n");
1903 scale_action = -1;
1904 /* No throughput measured yet for adjacent rates; try increase. */
1905 } else if ((low_tpt == IWL_INVALID_VALUE) &&
1906 (high_tpt == IWL_INVALID_VALUE)) {
1907 if (high != IWL_RATE_INVALID && sr >= IWL_RATE_INCREASE_TH)
1908 scale_action = 1;
1909 else if (low != IWL_RATE_INVALID)
1910 scale_action = 0;
1911 }
1912
1913 /* Both adjacent throughputs are measured, but neither one has better
1914 * throughput; we're using the best rate, don't change it! */
1915 else if ((low_tpt != IWL_INVALID_VALUE) &&
1916 (high_tpt != IWL_INVALID_VALUE) &&
1917 (low_tpt < current_tpt) &&
1918 (high_tpt < current_tpt))
1919 scale_action = 0;
1920
1921 /* At least one adjacent rate's throughput is measured,
1922 * and may have better performance. */
1923 else {
1924 /* Higher adjacent rate's throughput is measured */
1925 if (high_tpt != IWL_INVALID_VALUE) {
1926 /* Higher rate has better throughput */
1927 if (high_tpt > current_tpt &&
1928 sr >= IWL_RATE_INCREASE_TH) {
1929 scale_action = 1;
1930 } else {
1931 scale_action = 0;
1932 }
1933
1934 /* Lower adjacent rate's throughput is measured */
1935 } else if (low_tpt != IWL_INVALID_VALUE) {
1936 /* Lower rate has better throughput */
1937 if (low_tpt > current_tpt) {
1938 IWL_DEBUG_RATE(mvm,
1939 "decrease rate because of low tpt\n");
1940 scale_action = -1;
1941 } else if (sr >= IWL_RATE_INCREASE_TH) {
1942 scale_action = 1;
1943 }
1944 }
1945 }
1946
1947 /* Sanity check; asked for decrease, but success rate or throughput
1948 * has been good at old rate. Don't change it. */
1949 if ((scale_action == -1) && (low != IWL_RATE_INVALID) &&
1950 ((sr > IWL_RATE_HIGH_TH) ||
1951 (current_tpt > (100 * tbl->expected_tpt[low]))))
1952 scale_action = 0;
1953
Emmanuel Grumbach36946ce2013-05-28 23:12:47 +03001954 if ((BT_MBOX_MSG(&mvm->last_bt_notif, 3, TRAFFIC_LOAD) >=
Eyal Shapirad972ab32013-07-28 23:02:45 +00001955 IWL_BT_COEX_TRAFFIC_LOAD_HIGH) && (is_mimo(tbl->lq_type))) {
Emmanuel Grumbach36946ce2013-05-28 23:12:47 +03001956 if (lq_sta->last_bt_traffic >
1957 BT_MBOX_MSG(&mvm->last_bt_notif, 3, TRAFFIC_LOAD)) {
1958 /*
1959 * don't set scale_action, don't want to scale up if
1960 * the rate scale doesn't otherwise think that is a
1961 * good idea.
1962 */
1963 } else if (lq_sta->last_bt_traffic <=
1964 BT_MBOX_MSG(&mvm->last_bt_notif, 3, TRAFFIC_LOAD)) {
1965 scale_action = -1;
1966 }
1967 }
1968 lq_sta->last_bt_traffic =
1969 BT_MBOX_MSG(&mvm->last_bt_notif, 3, TRAFFIC_LOAD);
1970
1971 if ((BT_MBOX_MSG(&mvm->last_bt_notif, 3, TRAFFIC_LOAD) >=
Eyal Shapirad972ab32013-07-28 23:02:45 +00001972 IWL_BT_COEX_TRAFFIC_LOAD_HIGH) && is_mimo(tbl->lq_type)) {
Emmanuel Grumbach36946ce2013-05-28 23:12:47 +03001973 /* search for a new modulation */
1974 rs_stay_in_table(lq_sta, true);
1975 goto lq_update;
1976 }
1977
Johannes Berg8ca151b2013-01-24 14:25:36 +01001978 switch (scale_action) {
1979 case -1:
1980 /* Decrease starting rate, update uCode's rate table */
1981 if (low != IWL_RATE_INVALID) {
1982 update_lq = 1;
1983 index = low;
1984 }
1985
1986 break;
1987 case 1:
1988 /* Increase starting rate, update uCode's rate table */
1989 if (high != IWL_RATE_INVALID) {
1990 update_lq = 1;
1991 index = high;
1992 }
1993
1994 break;
1995 case 0:
1996 /* No change */
1997 default:
1998 break;
1999 }
2000
2001 IWL_DEBUG_RATE(mvm,
2002 "choose rate scale index %d action %d low %d high %d type %d\n",
2003 index, scale_action, low, high, tbl->lq_type);
2004
2005lq_update:
2006 /* Replace uCode's rate table for the destination station. */
2007 if (update_lq)
2008 rs_update_rate_tbl(mvm, lq_sta, tbl, index, is_green);
2009
2010 rs_stay_in_table(lq_sta, false);
2011
2012 /*
2013 * Search for new modulation mode if we're:
2014 * 1) Not changing rates right now
2015 * 2) Not just finishing up a search
2016 * 3) Allowing a new search
2017 */
2018 if (!update_lq && !done_search &&
2019 !lq_sta->stay_in_tbl && window->counter) {
2020 /* Save current throughput to compare with "search" throughput*/
2021 lq_sta->last_tpt = current_tpt;
2022
2023 /* Select a new "search" modulation mode to try.
2024 * If one is found, set up the new "search" table. */
2025 if (is_legacy(tbl->lq_type))
2026 rs_move_legacy_other(mvm, lq_sta, sta, index);
2027 else if (is_siso(tbl->lq_type))
2028 rs_move_siso_to_other(mvm, lq_sta, sta, index);
2029 else if (is_mimo2(tbl->lq_type))
2030 rs_move_mimo2_to_other(mvm, lq_sta, sta, index);
2031 else
Eyal Shapirad972ab32013-07-28 23:02:45 +00002032 WARN_ON_ONCE(1);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002033
2034 /* If new "search" mode was selected, set up in uCode table */
2035 if (lq_sta->search_better_tbl) {
2036 /* Access the "search" table, clear its history. */
2037 tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
2038 for (i = 0; i < IWL_RATE_COUNT; i++)
2039 rs_rate_scale_clear_window(&(tbl->win[i]));
2040
2041 /* Use new "search" start rate */
2042 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
2043
2044 IWL_DEBUG_RATE(mvm,
2045 "Switch current mcs: %X index: %d\n",
2046 tbl->current_rate, index);
2047 rs_fill_link_cmd(mvm, lq_sta, tbl->current_rate);
2048 iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, CMD_ASYNC, false);
2049 } else {
2050 done_search = 1;
2051 }
2052 }
2053
2054 if (done_search && !lq_sta->stay_in_tbl) {
2055 /* If the "active" (non-search) mode was legacy,
2056 * and we've tried switching antennas,
2057 * but we haven't been able to try HT modes (not available),
2058 * stay with best antenna legacy modulation for a while
2059 * before next round of mode comparisons. */
2060 tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
2061 if (is_legacy(tbl1->lq_type) && !sta->ht_cap.ht_supported &&
2062 lq_sta->action_counter > tbl1->max_search) {
2063 IWL_DEBUG_RATE(mvm, "LQ: STAY in legacy table\n");
2064 rs_set_stay_in_table(mvm, 1, lq_sta);
2065 }
2066
2067 /* If we're in an HT mode, and all 3 mode switch actions
2068 * have been tried and compared, stay in this best modulation
2069 * mode for a while before next round of mode comparisons. */
2070 if (lq_sta->enable_counter &&
2071 (lq_sta->action_counter >= tbl1->max_search)) {
2072 if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
2073 (lq_sta->tx_agg_tid_en & (1 << tid)) &&
2074 (tid != IWL_MAX_TID_COUNT)) {
2075 tid_data = &sta_priv->tid_data[tid];
2076 if (tid_data->state == IWL_AGG_OFF) {
2077 IWL_DEBUG_RATE(mvm,
2078 "try to aggregate tid %d\n",
2079 tid);
2080 rs_tl_turn_on_agg(mvm, tid,
2081 lq_sta, sta);
2082 }
2083 }
2084 rs_set_stay_in_table(mvm, 0, lq_sta);
2085 }
2086 }
2087
2088out:
2089 tbl->current_rate = rate_n_flags_from_tbl(mvm, tbl, index, is_green);
2090 lq_sta->last_txrate_idx = index;
2091}
2092
2093/**
2094 * rs_initialize_lq - Initialize a station's hardware rate table
2095 *
2096 * The uCode's station table contains a table of fallback rates
2097 * for automatic fallback during transmission.
2098 *
2099 * NOTE: This sets up a default set of values. These will be replaced later
2100 * if the driver's iwl-agn-rs rate scaling algorithm is used, instead of
2101 * rc80211_simple.
2102 *
2103 * NOTE: Run REPLY_ADD_STA command to set up station table entry, before
2104 * calling this function (which runs REPLY_TX_LINK_QUALITY_CMD,
2105 * which requires station table entry to exist).
2106 */
2107static void rs_initialize_lq(struct iwl_mvm *mvm,
2108 struct ieee80211_sta *sta,
2109 struct iwl_lq_sta *lq_sta,
2110 enum ieee80211_band band)
2111{
2112 struct iwl_scale_tbl_info *tbl;
2113 int rate_idx;
2114 int i;
2115 u32 rate;
2116 u8 use_green = rs_use_green(sta);
2117 u8 active_tbl = 0;
2118 u8 valid_tx_ant;
2119
2120 if (!sta || !lq_sta)
2121 return;
2122
2123 i = lq_sta->last_txrate_idx;
2124
Emmanuel Grumbachff402312013-04-03 20:10:06 +03002125 valid_tx_ant = iwl_fw_valid_tx_ant(mvm->fw);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002126
2127 if (!lq_sta->search_better_tbl)
2128 active_tbl = lq_sta->active_tbl;
2129 else
2130 active_tbl = 1 - lq_sta->active_tbl;
2131
2132 tbl = &(lq_sta->lq_info[active_tbl]);
2133
2134 if ((i < 0) || (i >= IWL_RATE_COUNT))
2135 i = 0;
2136
2137 rate = iwl_rates[i].plcp;
2138 tbl->ant_type = first_antenna(valid_tx_ant);
2139 rate |= tbl->ant_type << RATE_MCS_ANT_POS;
2140
2141 if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
2142 rate |= RATE_MCS_CCK_MSK;
2143
2144 rs_get_tbl_info_from_mcs(rate, band, tbl, &rate_idx);
2145 if (!rs_is_valid_ant(valid_tx_ant, tbl->ant_type))
2146 rs_toggle_antenna(valid_tx_ant, &rate, tbl);
2147
2148 rate = rate_n_flags_from_tbl(mvm, tbl, rate_idx, use_green);
2149 tbl->current_rate = rate;
2150 rs_set_expected_tpt_table(lq_sta, tbl);
2151 rs_fill_link_cmd(NULL, lq_sta, rate);
2152 /* TODO restore station should remember the lq cmd */
2153 iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, CMD_SYNC, true);
2154}
2155
2156static void rs_get_rate(void *mvm_r, struct ieee80211_sta *sta, void *mvm_sta,
2157 struct ieee80211_tx_rate_control *txrc)
2158{
2159 struct sk_buff *skb = txrc->skb;
2160 struct ieee80211_supported_band *sband = txrc->sband;
2161 struct iwl_op_mode *op_mode __maybe_unused =
2162 (struct iwl_op_mode *)mvm_r;
2163 struct iwl_mvm *mvm __maybe_unused = IWL_OP_MODE_GET_MVM(op_mode);
2164 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2165 struct iwl_lq_sta *lq_sta = mvm_sta;
2166 int rate_idx;
2167
2168 IWL_DEBUG_RATE_LIMIT(mvm, "rate scale calculate new rate for skb\n");
2169
2170 /* Get max rate if user set max rate */
2171 if (lq_sta) {
2172 lq_sta->max_rate_idx = txrc->max_rate_idx;
2173 if ((sband->band == IEEE80211_BAND_5GHZ) &&
2174 (lq_sta->max_rate_idx != -1))
2175 lq_sta->max_rate_idx += IWL_FIRST_OFDM_RATE;
2176 if ((lq_sta->max_rate_idx < 0) ||
2177 (lq_sta->max_rate_idx >= IWL_RATE_COUNT))
2178 lq_sta->max_rate_idx = -1;
2179 }
2180
2181 /* Treat uninitialized rate scaling data same as non-existing. */
2182 if (lq_sta && !lq_sta->drv) {
2183 IWL_DEBUG_RATE(mvm, "Rate scaling not initialized yet.\n");
2184 mvm_sta = NULL;
2185 }
2186
2187 /* Send management frames and NO_ACK data using lowest rate. */
2188 if (rate_control_send_low(sta, mvm_sta, txrc))
2189 return;
2190
2191 rate_idx = lq_sta->last_txrate_idx;
2192
2193 if (lq_sta->last_rate_n_flags & RATE_MCS_HT_MSK) {
2194 rate_idx -= IWL_FIRST_OFDM_RATE;
2195 /* 6M and 9M shared same MCS index */
2196 rate_idx = (rate_idx > 0) ? (rate_idx - 1) : 0;
Eyal Shapirad972ab32013-07-28 23:02:45 +00002197 WARN_ON_ONCE(rs_extract_rate(lq_sta->last_rate_n_flags) >=
2198 IWL_RATE_MIMO3_6M_PLCP);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002199 if (rs_extract_rate(lq_sta->last_rate_n_flags) >=
Eyal Shapirad972ab32013-07-28 23:02:45 +00002200 IWL_RATE_MIMO2_6M_PLCP)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002201 rate_idx = rate_idx + MCS_INDEX_PER_STREAM;
2202 info->control.rates[0].flags = IEEE80211_TX_RC_MCS;
2203 if (lq_sta->last_rate_n_flags & RATE_MCS_SGI_MSK)
2204 info->control.rates[0].flags |= IEEE80211_TX_RC_SHORT_GI;
2205 if (lq_sta->last_rate_n_flags & RATE_MCS_CHAN_WIDTH_40) /* TODO */
2206 info->control.rates[0].flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
2207 if (lq_sta->last_rate_n_flags & RATE_HT_MCS_GF_MSK)
2208 info->control.rates[0].flags |= IEEE80211_TX_RC_GREEN_FIELD;
2209 } else {
2210 /* Check for invalid rates */
2211 if ((rate_idx < 0) || (rate_idx >= IWL_RATE_COUNT_LEGACY) ||
2212 ((sband->band == IEEE80211_BAND_5GHZ) &&
2213 (rate_idx < IWL_FIRST_OFDM_RATE)))
2214 rate_idx = rate_lowest_index(sband, sta);
2215 /* On valid 5 GHz rate, adjust index */
2216 else if (sband->band == IEEE80211_BAND_5GHZ)
2217 rate_idx -= IWL_FIRST_OFDM_RATE;
2218 info->control.rates[0].flags = 0;
2219 }
2220 info->control.rates[0].idx = rate_idx;
Moshe Benji622ebe92013-06-03 19:27:16 +03002221 info->control.rates[0].count = 1;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002222}
2223
2224static void *rs_alloc_sta(void *mvm_rate, struct ieee80211_sta *sta,
2225 gfp_t gfp)
2226{
2227 struct iwl_mvm_sta *sta_priv = (struct iwl_mvm_sta *)sta->drv_priv;
2228 struct iwl_op_mode *op_mode __maybe_unused =
2229 (struct iwl_op_mode *)mvm_rate;
2230 struct iwl_mvm *mvm __maybe_unused = IWL_OP_MODE_GET_MVM(op_mode);
2231
2232 IWL_DEBUG_RATE(mvm, "create station rate scale window\n");
2233
2234 return &sta_priv->lq_sta;
2235}
2236
2237/*
2238 * Called after adding a new station to initialize rate scaling
2239 */
2240void iwl_mvm_rs_rate_init(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
2241 enum ieee80211_band band)
2242{
2243 int i, j;
2244 struct ieee80211_hw *hw = mvm->hw;
2245 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
2246 struct iwl_mvm_sta *sta_priv;
2247 struct iwl_lq_sta *lq_sta;
2248 struct ieee80211_supported_band *sband;
2249 unsigned long supp; /* must be unsigned long for for_each_set_bit */
2250
2251 sta_priv = (struct iwl_mvm_sta *)sta->drv_priv;
2252 lq_sta = &sta_priv->lq_sta;
2253 sband = hw->wiphy->bands[band];
2254
2255 lq_sta->lq.sta_id = sta_priv->sta_id;
2256
2257 for (j = 0; j < LQ_SIZE; j++)
2258 for (i = 0; i < IWL_RATE_COUNT; i++)
2259 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
2260
2261 lq_sta->flush_timer = 0;
2262 lq_sta->supp_rates = sta->supp_rates[sband->band];
Johannes Berg8ca151b2013-01-24 14:25:36 +01002263
2264 IWL_DEBUG_RATE(mvm,
2265 "LQ: *** rate scale station global init for station %d ***\n",
2266 sta_priv->sta_id);
2267 /* TODO: what is a good starting rate for STA? About middle? Maybe not
2268 * the lowest or the highest rate.. Could consider using RSSI from
2269 * previous packets? Need to have IEEE 802.1X auth succeed immediately
2270 * after assoc.. */
2271
2272 lq_sta->max_rate_idx = -1;
2273 lq_sta->missed_rate_counter = IWL_MISSED_RATE_MAX;
2274 lq_sta->is_green = rs_use_green(sta);
2275 lq_sta->band = sband->band;
2276 /*
2277 * active legacy rates as per supported rates bitmap
2278 */
2279 supp = sta->supp_rates[sband->band];
2280 lq_sta->active_legacy_rate = 0;
2281 for_each_set_bit(i, &supp, BITS_PER_LONG)
2282 lq_sta->active_legacy_rate |= BIT(sband->bitrates[i].hw_value);
2283
2284 /*
2285 * active_siso_rate mask includes 9 MBits (bit 5), and CCK (bits 0-3),
2286 * supp_rates[] does not; shift to convert format, force 9 MBits off.
2287 */
2288 lq_sta->active_siso_rate = ht_cap->mcs.rx_mask[0] << 1;
2289 lq_sta->active_siso_rate |= ht_cap->mcs.rx_mask[0] & 0x1;
2290 lq_sta->active_siso_rate &= ~((u16)0x2);
2291 lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE;
2292
2293 /* Same here */
2294 lq_sta->active_mimo2_rate = ht_cap->mcs.rx_mask[1] << 1;
2295 lq_sta->active_mimo2_rate |= ht_cap->mcs.rx_mask[1] & 0x1;
2296 lq_sta->active_mimo2_rate &= ~((u16)0x2);
2297 lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE;
2298
Johannes Berg8ca151b2013-01-24 14:25:36 +01002299 IWL_DEBUG_RATE(mvm,
Eyal Shapirad972ab32013-07-28 23:02:45 +00002300 "SISO-RATE=%X MIMO2-RATE=%X\n",
Johannes Berg8ca151b2013-01-24 14:25:36 +01002301 lq_sta->active_siso_rate,
Eyal Shapirad972ab32013-07-28 23:02:45 +00002302 lq_sta->active_mimo2_rate);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002303
2304 /* These values will be overridden later */
2305 lq_sta->lq.single_stream_ant_msk =
Emmanuel Grumbachff402312013-04-03 20:10:06 +03002306 first_antenna(iwl_fw_valid_tx_ant(mvm->fw));
Johannes Berg8ca151b2013-01-24 14:25:36 +01002307 lq_sta->lq.dual_stream_ant_msk =
Emmanuel Grumbachff402312013-04-03 20:10:06 +03002308 iwl_fw_valid_tx_ant(mvm->fw) &
2309 ~first_antenna(iwl_fw_valid_tx_ant(mvm->fw));
Johannes Berg8ca151b2013-01-24 14:25:36 +01002310 if (!lq_sta->lq.dual_stream_ant_msk) {
2311 lq_sta->lq.dual_stream_ant_msk = ANT_AB;
Emmanuel Grumbachff402312013-04-03 20:10:06 +03002312 } else if (num_of_ant(iwl_fw_valid_tx_ant(mvm->fw)) == 2) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002313 lq_sta->lq.dual_stream_ant_msk =
Emmanuel Grumbachff402312013-04-03 20:10:06 +03002314 iwl_fw_valid_tx_ant(mvm->fw);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002315 }
2316
2317 /* as default allow aggregation for all tids */
2318 lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
2319 lq_sta->drv = mvm;
2320
2321 /* Set last_txrate_idx to lowest rate */
2322 lq_sta->last_txrate_idx = rate_lowest_index(sband, sta);
2323 if (sband->band == IEEE80211_BAND_5GHZ)
2324 lq_sta->last_txrate_idx += IWL_FIRST_OFDM_RATE;
2325 lq_sta->is_agg = 0;
2326#ifdef CONFIG_MAC80211_DEBUGFS
2327 lq_sta->dbg_fixed_rate = 0;
2328#endif
2329
2330 rs_initialize_lq(mvm, sta, lq_sta, band);
2331}
2332
2333static void rs_fill_link_cmd(struct iwl_mvm *mvm,
2334 struct iwl_lq_sta *lq_sta, u32 new_rate)
2335{
2336 struct iwl_scale_tbl_info tbl_type;
2337 int index = 0;
2338 int rate_idx;
2339 int repeat_rate = 0;
2340 u8 ant_toggle_cnt = 0;
2341 u8 use_ht_possible = 1;
2342 u8 valid_tx_ant = 0;
2343 struct iwl_lq_cmd *lq_cmd = &lq_sta->lq;
2344
2345 /* Override starting rate (index 0) if needed for debug purposes */
Eyal Shapira3571ac32013-07-29 17:08:48 +03002346 rs_dbgfs_set_mcs(lq_sta, &new_rate);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002347
2348 /* Interpret new_rate (rate_n_flags) */
2349 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band,
2350 &tbl_type, &rate_idx);
2351
2352 /* How many times should we repeat the initial rate? */
2353 if (is_legacy(tbl_type.lq_type)) {
2354 ant_toggle_cnt = 1;
2355 repeat_rate = IWL_NUMBER_TRY;
2356 } else {
2357 repeat_rate = min(IWL_HT_NUMBER_TRY,
2358 LINK_QUAL_AGG_DISABLE_START_DEF - 1);
2359 }
2360
2361 lq_cmd->mimo_delim = is_mimo(tbl_type.lq_type) ? 1 : 0;
2362
2363 /* Fill 1st table entry (index 0) */
2364 lq_cmd->rs_table[index] = cpu_to_le32(new_rate);
2365
2366 if (num_of_ant(tbl_type.ant_type) == 1)
2367 lq_cmd->single_stream_ant_msk = tbl_type.ant_type;
2368 else if (num_of_ant(tbl_type.ant_type) == 2)
2369 lq_cmd->dual_stream_ant_msk = tbl_type.ant_type;
2370 /* otherwise we don't modify the existing value */
2371
2372 index++;
2373 repeat_rate--;
2374 if (mvm)
Emmanuel Grumbachff402312013-04-03 20:10:06 +03002375 valid_tx_ant = iwl_fw_valid_tx_ant(mvm->fw);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002376
2377 /* Fill rest of rate table */
2378 while (index < LINK_QUAL_MAX_RETRY_NUM) {
2379 /* Repeat initial/next rate.
2380 * For legacy IWL_NUMBER_TRY == 1, this loop will not execute.
2381 * For HT IWL_HT_NUMBER_TRY == 3, this executes twice. */
2382 while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
2383 if (is_legacy(tbl_type.lq_type)) {
2384 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2385 ant_toggle_cnt++;
2386 else if (mvm &&
2387 rs_toggle_antenna(valid_tx_ant,
2388 &new_rate, &tbl_type))
2389 ant_toggle_cnt = 1;
2390 }
2391
2392 /* Override next rate if needed for debug purposes */
Eyal Shapira3571ac32013-07-29 17:08:48 +03002393 rs_dbgfs_set_mcs(lq_sta, &new_rate);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002394
2395 /* Fill next table entry */
2396 lq_cmd->rs_table[index] =
2397 cpu_to_le32(new_rate);
2398 repeat_rate--;
2399 index++;
2400 }
2401
2402 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band, &tbl_type,
2403 &rate_idx);
2404
2405
2406 /* Indicate to uCode which entries might be MIMO.
2407 * If initial rate was MIMO, this will finally end up
2408 * as (IWL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
2409 if (is_mimo(tbl_type.lq_type))
2410 lq_cmd->mimo_delim = index;
2411
2412 /* Get next rate */
2413 new_rate = rs_get_lower_rate(lq_sta, &tbl_type, rate_idx,
2414 use_ht_possible);
2415
2416 /* How many times should we repeat the next rate? */
2417 if (is_legacy(tbl_type.lq_type)) {
2418 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2419 ant_toggle_cnt++;
2420 else if (mvm &&
2421 rs_toggle_antenna(valid_tx_ant,
2422 &new_rate, &tbl_type))
2423 ant_toggle_cnt = 1;
2424
2425 repeat_rate = IWL_NUMBER_TRY;
2426 } else {
2427 repeat_rate = IWL_HT_NUMBER_TRY;
2428 }
2429
2430 /* Don't allow HT rates after next pass.
2431 * rs_get_lower_rate() will change type to LQ_A or LQ_G. */
2432 use_ht_possible = 0;
2433
2434 /* Override next rate if needed for debug purposes */
Eyal Shapira3571ac32013-07-29 17:08:48 +03002435 rs_dbgfs_set_mcs(lq_sta, &new_rate);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002436
2437 /* Fill next table entry */
2438 lq_cmd->rs_table[index] = cpu_to_le32(new_rate);
2439
2440 index++;
2441 repeat_rate--;
2442 }
2443
2444 lq_cmd->agg_frame_cnt_limit = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
2445 lq_cmd->agg_disable_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
2446
2447 lq_cmd->agg_time_limit =
2448 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
Emmanuel Grumbache96d5512013-05-28 21:31:50 +03002449
2450 /*
2451 * overwrite if needed, pass aggregation time limit
2452 * to uCode in uSec - This is racy - but heh, at least it helps...
2453 */
2454 if (mvm && BT_MBOX_MSG(&mvm->last_bt_notif, 3, TRAFFIC_LOAD) >= 2)
2455 lq_cmd->agg_time_limit = cpu_to_le16(1200);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002456}
2457
2458static void *rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
2459{
2460 return hw->priv;
2461}
2462/* rate scale requires free function to be implemented */
2463static void rs_free(void *mvm_rate)
2464{
2465 return;
2466}
2467
2468static void rs_free_sta(void *mvm_r, struct ieee80211_sta *sta,
2469 void *mvm_sta)
2470{
2471 struct iwl_op_mode *op_mode __maybe_unused = mvm_r;
2472 struct iwl_mvm *mvm __maybe_unused = IWL_OP_MODE_GET_MVM(op_mode);
2473
2474 IWL_DEBUG_RATE(mvm, "enter\n");
2475 IWL_DEBUG_RATE(mvm, "leave\n");
2476}
2477
2478#ifdef CONFIG_MAC80211_DEBUGFS
2479static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
Eyal Shapira3571ac32013-07-29 17:08:48 +03002480 u32 *rate_n_flags)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002481{
2482 struct iwl_mvm *mvm;
2483 u8 valid_tx_ant;
2484 u8 ant_sel_tx;
2485
2486 mvm = lq_sta->drv;
Emmanuel Grumbachff402312013-04-03 20:10:06 +03002487 valid_tx_ant = iwl_fw_valid_tx_ant(mvm->fw);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002488 if (lq_sta->dbg_fixed_rate) {
2489 ant_sel_tx =
2490 ((lq_sta->dbg_fixed_rate & RATE_MCS_ANT_ABC_MSK)
2491 >> RATE_MCS_ANT_POS);
2492 if ((valid_tx_ant & ant_sel_tx) == ant_sel_tx) {
2493 *rate_n_flags = lq_sta->dbg_fixed_rate;
2494 IWL_DEBUG_RATE(mvm, "Fixed rate ON\n");
2495 } else {
2496 lq_sta->dbg_fixed_rate = 0;
2497 IWL_ERR(mvm,
2498 "Invalid antenna selection 0x%X, Valid is 0x%X\n",
2499 ant_sel_tx, valid_tx_ant);
2500 IWL_DEBUG_RATE(mvm, "Fixed rate OFF\n");
2501 }
2502 } else {
2503 IWL_DEBUG_RATE(mvm, "Fixed rate OFF\n");
2504 }
2505}
2506
2507static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
2508 const char __user *user_buf, size_t count, loff_t *ppos)
2509{
2510 struct iwl_lq_sta *lq_sta = file->private_data;
2511 struct iwl_mvm *mvm;
2512 char buf[64];
2513 size_t buf_size;
2514 u32 parsed_rate;
2515
2516
2517 mvm = lq_sta->drv;
2518 memset(buf, 0, sizeof(buf));
2519 buf_size = min(count, sizeof(buf) - 1);
2520 if (copy_from_user(buf, user_buf, buf_size))
2521 return -EFAULT;
2522
2523 if (sscanf(buf, "%x", &parsed_rate) == 1)
2524 lq_sta->dbg_fixed_rate = parsed_rate;
2525 else
2526 lq_sta->dbg_fixed_rate = 0;
2527
2528 rs_program_fix_rate(mvm, lq_sta);
2529
2530 return count;
2531}
2532
2533static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
2534 char __user *user_buf, size_t count, loff_t *ppos)
2535{
2536 char *buff;
2537 int desc = 0;
2538 int i = 0;
2539 int index = 0;
2540 ssize_t ret;
2541
2542 struct iwl_lq_sta *lq_sta = file->private_data;
2543 struct iwl_mvm *mvm;
2544 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
2545
2546 mvm = lq_sta->drv;
2547 buff = kmalloc(1024, GFP_KERNEL);
2548 if (!buff)
2549 return -ENOMEM;
2550
2551 desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
2552 desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
2553 lq_sta->total_failed, lq_sta->total_success,
2554 lq_sta->active_legacy_rate);
2555 desc += sprintf(buff+desc, "fixed rate 0x%X\n",
2556 lq_sta->dbg_fixed_rate);
2557 desc += sprintf(buff+desc, "valid_tx_ant %s%s%s\n",
Emmanuel Grumbachff402312013-04-03 20:10:06 +03002558 (iwl_fw_valid_tx_ant(mvm->fw) & ANT_A) ? "ANT_A," : "",
2559 (iwl_fw_valid_tx_ant(mvm->fw) & ANT_B) ? "ANT_B," : "",
2560 (iwl_fw_valid_tx_ant(mvm->fw) & ANT_C) ? "ANT_C" : "");
Johannes Berg8ca151b2013-01-24 14:25:36 +01002561 desc += sprintf(buff+desc, "lq type %s\n",
2562 (is_legacy(tbl->lq_type)) ? "legacy" : "HT");
2563 if (is_Ht(tbl->lq_type)) {
2564 desc += sprintf(buff+desc, " %s",
Eyal Shapirad972ab32013-07-28 23:02:45 +00002565 (is_siso(tbl->lq_type)) ? "SISO" : "MIMO2");
Johannes Berg8ca151b2013-01-24 14:25:36 +01002566 desc += sprintf(buff+desc, " %s",
2567 (tbl->is_ht40) ? "40MHz" : "20MHz");
2568 desc += sprintf(buff+desc, " %s %s %s\n",
2569 (tbl->is_SGI) ? "SGI" : "",
2570 (lq_sta->is_green) ? "GF enabled" : "",
2571 (lq_sta->is_agg) ? "AGG on" : "");
2572 }
2573 desc += sprintf(buff+desc, "last tx rate=0x%X\n",
2574 lq_sta->last_rate_n_flags);
2575 desc += sprintf(buff+desc,
2576 "general: flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
2577 lq_sta->lq.flags,
2578 lq_sta->lq.mimo_delim,
2579 lq_sta->lq.single_stream_ant_msk,
2580 lq_sta->lq.dual_stream_ant_msk);
2581
2582 desc += sprintf(buff+desc,
2583 "agg: time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
2584 le16_to_cpu(lq_sta->lq.agg_time_limit),
2585 lq_sta->lq.agg_disable_start_th,
2586 lq_sta->lq.agg_frame_cnt_limit);
2587
2588 desc += sprintf(buff+desc,
2589 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
2590 lq_sta->lq.initial_rate_index[0],
2591 lq_sta->lq.initial_rate_index[1],
2592 lq_sta->lq.initial_rate_index[2],
2593 lq_sta->lq.initial_rate_index[3]);
2594
2595 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
2596 index = iwl_hwrate_to_plcp_idx(
2597 le32_to_cpu(lq_sta->lq.rs_table[i]));
2598 if (is_legacy(tbl->lq_type)) {
2599 desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps\n",
2600 i, le32_to_cpu(lq_sta->lq.rs_table[i]),
2601 iwl_rate_mcs[index].mbps);
2602 } else {
2603 desc += sprintf(buff+desc,
2604 " rate[%d] 0x%X %smbps (%s)\n",
2605 i, le32_to_cpu(lq_sta->lq.rs_table[i]),
2606 iwl_rate_mcs[index].mbps,
2607 iwl_rate_mcs[index].mcs);
2608 }
2609 }
2610
2611 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2612 kfree(buff);
2613 return ret;
2614}
2615
2616static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
2617 .write = rs_sta_dbgfs_scale_table_write,
2618 .read = rs_sta_dbgfs_scale_table_read,
2619 .open = simple_open,
2620 .llseek = default_llseek,
2621};
2622static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
2623 char __user *user_buf, size_t count, loff_t *ppos)
2624{
2625 char *buff;
2626 int desc = 0;
2627 int i, j;
2628 ssize_t ret;
2629
2630 struct iwl_lq_sta *lq_sta = file->private_data;
2631
2632 buff = kmalloc(1024, GFP_KERNEL);
2633 if (!buff)
2634 return -ENOMEM;
2635
2636 for (i = 0; i < LQ_SIZE; i++) {
2637 desc += sprintf(buff+desc,
2638 "%s type=%d SGI=%d HT40=%d DUP=0 GF=%d\n"
2639 "rate=0x%X\n",
2640 lq_sta->active_tbl == i ? "*" : "x",
2641 lq_sta->lq_info[i].lq_type,
2642 lq_sta->lq_info[i].is_SGI,
2643 lq_sta->lq_info[i].is_ht40,
2644 lq_sta->is_green,
2645 lq_sta->lq_info[i].current_rate);
2646 for (j = 0; j < IWL_RATE_COUNT; j++) {
2647 desc += sprintf(buff+desc,
2648 "counter=%d success=%d %%=%d\n",
2649 lq_sta->lq_info[i].win[j].counter,
2650 lq_sta->lq_info[i].win[j].success_counter,
2651 lq_sta->lq_info[i].win[j].success_ratio);
2652 }
2653 }
2654 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2655 kfree(buff);
2656 return ret;
2657}
2658
2659static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
2660 .read = rs_sta_dbgfs_stats_table_read,
2661 .open = simple_open,
2662 .llseek = default_llseek,
2663};
2664
Johannes Berg8ca151b2013-01-24 14:25:36 +01002665static void rs_add_debugfs(void *mvm, void *mvm_sta, struct dentry *dir)
2666{
2667 struct iwl_lq_sta *lq_sta = mvm_sta;
2668 lq_sta->rs_sta_dbgfs_scale_table_file =
2669 debugfs_create_file("rate_scale_table", S_IRUSR | S_IWUSR, dir,
2670 lq_sta, &rs_sta_dbgfs_scale_table_ops);
2671 lq_sta->rs_sta_dbgfs_stats_table_file =
2672 debugfs_create_file("rate_stats_table", S_IRUSR, dir,
2673 lq_sta, &rs_sta_dbgfs_stats_table_ops);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002674 lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file =
2675 debugfs_create_u8("tx_agg_tid_enable", S_IRUSR | S_IWUSR, dir,
2676 &lq_sta->tx_agg_tid_en);
2677}
2678
2679static void rs_remove_debugfs(void *mvm, void *mvm_sta)
2680{
2681 struct iwl_lq_sta *lq_sta = mvm_sta;
2682 debugfs_remove(lq_sta->rs_sta_dbgfs_scale_table_file);
2683 debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002684 debugfs_remove(lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file);
2685}
2686#endif
2687
2688/*
2689 * Initialization of rate scaling information is done by driver after
2690 * the station is added. Since mac80211 calls this function before a
2691 * station is added we ignore it.
2692 */
2693static void rs_rate_init_stub(void *mvm_r,
Simon Wunderlich3de805c2013-07-08 16:55:50 +02002694 struct ieee80211_supported_band *sband,
2695 struct cfg80211_chan_def *chandef,
2696 struct ieee80211_sta *sta, void *mvm_sta)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002697{
2698}
2699static struct rate_control_ops rs_mvm_ops = {
2700 .module = NULL,
2701 .name = RS_NAME,
2702 .tx_status = rs_tx_status,
2703 .get_rate = rs_get_rate,
2704 .rate_init = rs_rate_init_stub,
2705 .alloc = rs_alloc,
2706 .free = rs_free,
2707 .alloc_sta = rs_alloc_sta,
2708 .free_sta = rs_free_sta,
2709#ifdef CONFIG_MAC80211_DEBUGFS
2710 .add_sta_debugfs = rs_add_debugfs,
2711 .remove_sta_debugfs = rs_remove_debugfs,
2712#endif
2713};
2714
2715int iwl_mvm_rate_control_register(void)
2716{
2717 return ieee80211_rate_control_register(&rs_mvm_ops);
2718}
2719
2720void iwl_mvm_rate_control_unregister(void)
2721{
2722 ieee80211_rate_control_unregister(&rs_mvm_ops);
2723}
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +03002724
2725/**
2726 * iwl_mvm_tx_protection - Gets LQ command, change it to enable/disable
2727 * Tx protection, according to this rquest and previous requests,
2728 * and send the LQ command.
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +03002729 * @mvmsta: The station
2730 * @enable: Enable Tx protection?
2731 */
Johannes Berge126b5d2013-06-28 13:39:18 +02002732int iwl_mvm_tx_protection(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
2733 bool enable)
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +03002734{
Johannes Berge126b5d2013-06-28 13:39:18 +02002735 struct iwl_lq_cmd *lq = &mvmsta->lq_sta.lq;
2736
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +03002737 lockdep_assert_held(&mvm->mutex);
2738
2739 if (enable) {
2740 if (mvmsta->tx_protection == 0)
2741 lq->flags |= LQ_FLAG_SET_STA_TLC_RTS_MSK;
2742 mvmsta->tx_protection++;
2743 } else {
2744 mvmsta->tx_protection--;
2745 if (mvmsta->tx_protection == 0)
2746 lq->flags &= ~LQ_FLAG_SET_STA_TLC_RTS_MSK;
2747 }
2748
2749 return iwl_mvm_send_lq_cmd(mvm, lq, CMD_ASYNC, false);
2750}