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