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