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