blob: 3bd7c86e90d9fca5c43c6a95ac7795ec74ddeaf7 [file] [log] [blame]
Zhu Yib481de92007-09-25 17:54:57 -07001/******************************************************************************
2 *
Emmanuel Grumbach51368bf2013-12-30 13:15:54 +02003 * Copyright(c) 2005 - 2014 Intel Corporation. All rights reserved.
Zhu Yib481de92007-09-25 17:54:57 -07004 *
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:
Winkler, Tomas759ef892008-12-09 11:28:58 -080022 * Intel Linux Wireless <ilw@linux.intel.com>
Zhu Yib481de92007-09-25 17:54:57 -070023 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24 *
25 *****************************************************************************/
26#include <linux/kernel.h>
Zhu Yib481de92007-09-25 17:54:57 -070027#include <linux/skbuff.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Zhu Yib481de92007-09-25 17:54:57 -070029#include <net/mac80211.h>
Zhu Yib481de92007-09-25 17:54:57 -070030
31#include <linux/netdevice.h>
32#include <linux/etherdevice.h>
33#include <linux/delay.h>
34
35#include <linux/workqueue.h>
36
Johannes Berg1023fdc2012-05-15 12:16:34 +020037#include "dev.h"
38#include "agn.h"
Zhu Yib481de92007-09-25 17:54:57 -070039
Tomas Winklere227cea2008-07-18 13:53:05 +080040#define RS_NAME "iwl-agn-rs"
Zhu Yib481de92007-09-25 17:54:57 -070041
Guy Cohenaade00c2008-04-23 17:14:59 -070042#define NUM_TRY_BEFORE_ANT_TOGGLE 1
Zhu Yib481de92007-09-25 17:54:57 -070043#define IWL_NUMBER_TRY 1
44#define IWL_HT_NUMBER_TRY 3
45
Ben Cahill77626352007-11-29 11:09:44 +080046#define IWL_RATE_MAX_WINDOW 62 /* # tx in history window */
47#define IWL_RATE_MIN_FAILURE_TH 6 /* min failures to calc tpt */
48#define IWL_RATE_MIN_SUCCESS_TH 8 /* min successes to calc tpt */
49
Abbas, Mohamed7d049e52009-01-20 21:33:53 -080050/* max allowed rate miss before sync LQ cmd */
51#define IWL_MISSED_RATE_MAX 15
Ben Cahill77626352007-11-29 11:09:44 +080052/* max time to accum history 2 seconds */
Mohamed Abbas447fee72009-04-20 14:37:02 -070053#define IWL_RATE_SCALE_FLUSH_INTVL (3*HZ)
Zhu Yib481de92007-09-25 17:54:57 -070054
55static u8 rs_ht_to_legacy[] = {
56 IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
57 IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
58 IWL_RATE_6M_INDEX,
59 IWL_RATE_6M_INDEX, IWL_RATE_9M_INDEX,
60 IWL_RATE_12M_INDEX, IWL_RATE_18M_INDEX,
61 IWL_RATE_24M_INDEX, IWL_RATE_36M_INDEX,
62 IWL_RATE_48M_INDEX, IWL_RATE_54M_INDEX
63};
64
Guy Cohenaade00c2008-04-23 17:14:59 -070065static const u8 ant_toggle_lookup[] = {
66 /*ANT_NONE -> */ ANT_NONE,
67 /*ANT_A -> */ ANT_B,
68 /*ANT_B -> */ ANT_C,
69 /*ANT_AB -> */ ANT_BC,
70 /*ANT_C -> */ ANT_A,
71 /*ANT_AC -> */ ANT_AB,
72 /*ANT_BC -> */ ANT_AC,
73 /*ANT_ABC -> */ ANT_ABC,
74};
75
Johannes Berg635b85b2010-09-22 18:02:04 +020076#define IWL_DECLARE_RATE_INFO(r, s, ip, in, rp, rn, pp, np) \
77 [IWL_RATE_##r##M_INDEX] = { IWL_RATE_##r##M_PLCP, \
78 IWL_RATE_SISO_##s##M_PLCP, \
79 IWL_RATE_MIMO2_##s##M_PLCP,\
80 IWL_RATE_MIMO3_##s##M_PLCP,\
81 IWL_RATE_##r##M_IEEE, \
82 IWL_RATE_##ip##M_INDEX, \
83 IWL_RATE_##in##M_INDEX, \
84 IWL_RATE_##rp##M_INDEX, \
85 IWL_RATE_##rn##M_INDEX, \
86 IWL_RATE_##pp##M_INDEX, \
87 IWL_RATE_##np##M_INDEX }
88
89/*
90 * Parameter order:
91 * rate, ht rate, prev rate, next rate, prev tgg rate, next tgg rate
92 *
93 * If there isn't a valid next or previous rate then INV is used which
94 * maps to IWL_RATE_INVALID
95 *
96 */
97const struct iwl_rate_info iwl_rates[IWL_RATE_COUNT] = {
98 IWL_DECLARE_RATE_INFO(1, INV, INV, 2, INV, 2, INV, 2), /* 1mbps */
99 IWL_DECLARE_RATE_INFO(2, INV, 1, 5, 1, 5, 1, 5), /* 2mbps */
100 IWL_DECLARE_RATE_INFO(5, INV, 2, 6, 2, 11, 2, 11), /*5.5mbps */
101 IWL_DECLARE_RATE_INFO(11, INV, 9, 12, 9, 12, 5, 18), /* 11mbps */
102 IWL_DECLARE_RATE_INFO(6, 6, 5, 9, 5, 11, 5, 11), /* 6mbps */
103 IWL_DECLARE_RATE_INFO(9, 6, 6, 11, 6, 11, 5, 11), /* 9mbps */
104 IWL_DECLARE_RATE_INFO(12, 12, 11, 18, 11, 18, 11, 18), /* 12mbps */
105 IWL_DECLARE_RATE_INFO(18, 18, 12, 24, 12, 24, 11, 24), /* 18mbps */
106 IWL_DECLARE_RATE_INFO(24, 24, 18, 36, 18, 36, 18, 36), /* 24mbps */
107 IWL_DECLARE_RATE_INFO(36, 36, 24, 48, 24, 48, 24, 48), /* 36mbps */
108 IWL_DECLARE_RATE_INFO(48, 48, 36, 54, 36, 54, 36, 54), /* 48mbps */
109 IWL_DECLARE_RATE_INFO(54, 54, 48, INV, 48, INV, 48, INV),/* 54mbps */
110 IWL_DECLARE_RATE_INFO(60, 60, 48, INV, 48, INV, 48, INV),/* 60mbps */
111 /* FIXME:RS: ^^ should be INV (legacy) */
112};
113
Daniel Halperin25205462011-03-18 18:48:55 -0700114static inline u8 rs_extract_rate(u32 rate_n_flags)
115{
116 return (u8)(rate_n_flags & RATE_MCS_RATE_MSK);
117}
118
Johannes Berg635b85b2010-09-22 18:02:04 +0200119static int iwl_hwrate_to_plcp_idx(u32 rate_n_flags)
120{
121 int idx = 0;
122
123 /* HT rate format */
124 if (rate_n_flags & RATE_MCS_HT_MSK) {
Daniel Halperin25205462011-03-18 18:48:55 -0700125 idx = rs_extract_rate(rate_n_flags);
Johannes Berg635b85b2010-09-22 18:02:04 +0200126
127 if (idx >= IWL_RATE_MIMO3_6M_PLCP)
128 idx = idx - IWL_RATE_MIMO3_6M_PLCP;
129 else if (idx >= IWL_RATE_MIMO2_6M_PLCP)
130 idx = idx - IWL_RATE_MIMO2_6M_PLCP;
131
132 idx += IWL_FIRST_OFDM_RATE;
133 /* skip 9M not supported in ht*/
134 if (idx >= IWL_RATE_9M_INDEX)
135 idx += 1;
136 if ((idx >= IWL_FIRST_OFDM_RATE) && (idx <= IWL_LAST_OFDM_RATE))
137 return idx;
138
139 /* legacy rate format, search for match in table */
140 } else {
141 for (idx = 0; idx < ARRAY_SIZE(iwl_rates); idx++)
Daniel Halperin25205462011-03-18 18:48:55 -0700142 if (iwl_rates[idx].plcp ==
143 rs_extract_rate(rate_n_flags))
Johannes Berg635b85b2010-09-22 18:02:04 +0200144 return idx;
145 }
146
147 return -1;
148}
149
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700150static void rs_rate_scale_perform(struct iwl_priv *priv,
Gábor Stefanik514d65c2009-04-23 19:36:08 +0200151 struct sk_buff *skb,
Johannes Berg4b7679a2008-09-18 18:14:18 +0200152 struct ieee80211_sta *sta,
153 struct iwl_lq_sta *lq_sta);
Wey-Yi Guy46f93812009-07-24 11:13:03 -0700154static void rs_fill_link_cmd(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +0800155 struct iwl_lq_sta *lq_sta, u32 rate_n_flags);
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -0700156static void rs_stay_in_table(struct iwl_lq_sta *lq_sta, bool force_search);
Zhu Yib481de92007-09-25 17:54:57 -0700157
158
Zhu Yi98d7e092007-09-27 11:27:42 +0800159#ifdef CONFIG_MAC80211_DEBUGFS
Tomas Winklere227cea2008-07-18 13:53:05 +0800160static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
161 u32 *rate_n_flags, int index);
Zhu Yi98d7e092007-09-27 11:27:42 +0800162#else
Tomas Winklere227cea2008-07-18 13:53:05 +0800163static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
164 u32 *rate_n_flags, int index)
Zhu Yi98d7e092007-09-27 11:27:42 +0800165{}
166#endif
Ben Cahill77626352007-11-29 11:09:44 +0800167
Daniel C Halperine3949d62009-09-17 10:43:50 -0700168/**
169 * The following tables contain the expected throughput metrics for all rates
170 *
171 * 1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
172 *
173 * where invalid entries are zeros.
174 *
175 * CCK rates are only valid in legacy table and will only be used in G
176 * (2.4 GHz) band.
Ben Cahill77626352007-11-29 11:09:44 +0800177 */
Wey-Yi Guy584a0f02009-04-01 14:33:24 -0700178
Johannes Berga34529e2014-01-20 22:57:21 +0100179static const u16 expected_tpt_legacy[IWL_RATE_COUNT] = {
Daniel C Halperine3949d62009-09-17 10:43:50 -0700180 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 0
Zhu Yib481de92007-09-25 17:54:57 -0700181};
182
Johannes Berga34529e2014-01-20 22:57:21 +0100183static const u16 expected_tpt_siso20MHz[4][IWL_RATE_COUNT] = {
Wey-Yi Guy7fc11e92011-01-15 09:16:59 -0800184 {0, 0, 0, 0, 42, 0, 76, 102, 124, 159, 183, 193, 202}, /* Norm */
185 {0, 0, 0, 0, 46, 0, 82, 110, 132, 168, 192, 202, 210}, /* SGI */
186 {0, 0, 0, 0, 47, 0, 91, 133, 171, 242, 305, 334, 362}, /* AGG */
187 {0, 0, 0, 0, 52, 0, 101, 145, 187, 264, 330, 361, 390}, /* AGG+SGI */
Zhu Yib481de92007-09-25 17:54:57 -0700188};
189
Johannes Berga34529e2014-01-20 22:57:21 +0100190static const u16 expected_tpt_siso40MHz[4][IWL_RATE_COUNT] = {
Daniel C Halperine3949d62009-09-17 10:43:50 -0700191 {0, 0, 0, 0, 77, 0, 127, 160, 184, 220, 242, 250, 257}, /* Norm */
192 {0, 0, 0, 0, 83, 0, 135, 169, 193, 229, 250, 257, 264}, /* SGI */
Wey-Yi Guy7fc11e92011-01-15 09:16:59 -0800193 {0, 0, 0, 0, 94, 0, 177, 249, 313, 423, 512, 550, 586}, /* AGG */
194 {0, 0, 0, 0, 104, 0, 193, 270, 338, 454, 545, 584, 620}, /* AGG+SGI */
Zhu Yib481de92007-09-25 17:54:57 -0700195};
196
Johannes Berga34529e2014-01-20 22:57:21 +0100197static const u16 expected_tpt_mimo2_20MHz[4][IWL_RATE_COUNT] = {
Wey-Yi Guy7fc11e92011-01-15 09:16:59 -0800198 {0, 0, 0, 0, 74, 0, 123, 155, 179, 214, 236, 244, 251}, /* Norm */
199 {0, 0, 0, 0, 81, 0, 131, 164, 188, 223, 243, 251, 257}, /* SGI */
200 {0, 0, 0, 0, 89, 0, 167, 235, 296, 402, 488, 526, 560}, /* AGG */
201 {0, 0, 0, 0, 97, 0, 182, 255, 320, 431, 520, 558, 593}, /* AGG+SGI*/
Zhu Yib481de92007-09-25 17:54:57 -0700202};
203
Johannes Berga34529e2014-01-20 22:57:21 +0100204static const u16 expected_tpt_mimo2_40MHz[4][IWL_RATE_COUNT] = {
Daniel C Halperine3949d62009-09-17 10:43:50 -0700205 {0, 0, 0, 0, 123, 0, 182, 214, 235, 264, 279, 285, 289}, /* Norm */
206 {0, 0, 0, 0, 131, 0, 191, 222, 242, 270, 284, 289, 293}, /* SGI */
Wey-Yi Guy7fc11e92011-01-15 09:16:59 -0800207 {0, 0, 0, 0, 171, 0, 305, 410, 496, 634, 731, 771, 805}, /* AGG */
208 {0, 0, 0, 0, 186, 0, 329, 439, 527, 667, 764, 803, 838}, /* AGG+SGI */
Zhu Yib481de92007-09-25 17:54:57 -0700209};
210
Johannes Berga34529e2014-01-20 22:57:21 +0100211static const u16 expected_tpt_mimo3_20MHz[4][IWL_RATE_COUNT] = {
Daniel C Halperine3949d62009-09-17 10:43:50 -0700212 {0, 0, 0, 0, 99, 0, 153, 186, 208, 239, 256, 263, 268}, /* Norm */
213 {0, 0, 0, 0, 106, 0, 162, 194, 215, 246, 262, 268, 273}, /* SGI */
214 {0, 0, 0, 0, 134, 0, 249, 346, 431, 574, 685, 732, 775}, /* AGG */
215 {0, 0, 0, 0, 148, 0, 272, 376, 465, 614, 727, 775, 818}, /* AGG+SGI */
Zhu Yib481de92007-09-25 17:54:57 -0700216};
217
Johannes Berga34529e2014-01-20 22:57:21 +0100218static const u16 expected_tpt_mimo3_40MHz[4][IWL_RATE_COUNT] = {
Daniel C Halperine3949d62009-09-17 10:43:50 -0700219 {0, 0, 0, 0, 152, 0, 211, 239, 255, 279, 290, 294, 297}, /* Norm */
220 {0, 0, 0, 0, 160, 0, 219, 245, 261, 284, 294, 297, 300}, /* SGI */
221 {0, 0, 0, 0, 254, 0, 443, 584, 695, 868, 984, 1030, 1070}, /* AGG */
222 {0, 0, 0, 0, 277, 0, 478, 624, 737, 911, 1026, 1070, 1109}, /* AGG+SGI */
Wey-Yi Guy584a0f02009-04-01 14:33:24 -0700223};
224
Wey-Yi Guy12b96812009-04-08 11:39:27 -0700225/* mbps, mcs */
Tobias Klauser79496732009-12-23 14:18:11 +0100226static const struct iwl_rate_mcs_info iwl_rate_mcs[IWL_RATE_COUNT] = {
Daniel C Halperine3949d62009-09-17 10:43:50 -0700227 { "1", "BPSK DSSS"},
228 { "2", "QPSK DSSS"},
229 {"5.5", "BPSK CCK"},
230 { "11", "QPSK CCK"},
231 { "6", "BPSK 1/2"},
232 { "9", "BPSK 1/2"},
233 { "12", "QPSK 1/2"},
234 { "18", "QPSK 3/4"},
235 { "24", "16QAM 1/2"},
236 { "36", "16QAM 3/4"},
237 { "48", "64QAM 2/3"},
238 { "54", "64QAM 3/4"},
239 { "60", "64QAM 5/6"},
Wey-Yi Guy12b96812009-04-08 11:39:27 -0700240};
241
Wey-Yi Guya9c146b2009-05-22 11:01:48 -0700242#define MCS_INDEX_PER_STREAM (8)
243
Tomas Winklere227cea2008-07-18 13:53:05 +0800244static void rs_rate_scale_clear_window(struct iwl_rate_scale_data *window)
Zhu Yib481de92007-09-25 17:54:57 -0700245{
246 window->data = 0;
247 window->success_counter = 0;
248 window->success_ratio = IWL_INVALID_VALUE;
249 window->counter = 0;
250 window->average_tpt = IWL_INVALID_VALUE;
251 window->stamp = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700252}
253
Guy Cohenaade00c2008-04-23 17:14:59 -0700254static inline u8 rs_is_valid_ant(u8 valid_antenna, u8 ant_type)
255{
Tomas Winkler3ac7f142008-07-21 02:40:14 +0300256 return (ant_type & valid_antenna) == ant_type;
Guy Cohenaade00c2008-04-23 17:14:59 -0700257}
258
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200259/*
260 * removes the old data from the statistics. All data that is older than
261 * TID_MAX_TIME_DIFF, will be deleted.
262 */
Tomas Winklere227cea2008-07-18 13:53:05 +0800263static void rs_tl_rm_old_stats(struct iwl_traffic_load *tl, u32 curr_time)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200264{
265 /* The oldest age we want to keep */
266 u32 oldest_time = curr_time - TID_MAX_TIME_DIFF;
267
268 while (tl->queue_count &&
269 (tl->time_stamp < oldest_time)) {
270 tl->total -= tl->packet_count[tl->head];
271 tl->packet_count[tl->head] = 0;
272 tl->time_stamp += TID_QUEUE_CELL_SPACING;
273 tl->queue_count--;
274 tl->head++;
275 if (tl->head >= TID_QUEUE_MAX_SIZE)
276 tl->head = 0;
277 }
278}
279
280/*
281 * increment traffic load value for tid and also remove
282 * any old values if passed the certain time period
283 */
Tomas Winklere227cea2008-07-18 13:53:05 +0800284static u8 rs_tl_add_packet(struct iwl_lq_sta *lq_data,
Ron Rindjunskyfaa29712008-06-12 09:46:58 +0800285 struct ieee80211_hdr *hdr)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200286{
287 u32 curr_time = jiffies_to_msecs(jiffies);
288 u32 time_diff;
289 s32 index;
Tomas Winklere227cea2008-07-18 13:53:05 +0800290 struct iwl_traffic_load *tl = NULL;
Tomas Winkler54dbb522008-05-15 13:54:06 +0800291 u8 tid;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200292
Tomas Winkler417f1142008-11-12 13:14:06 -0800293 if (ieee80211_is_data_qos(hdr->frame_control)) {
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700294 u8 *qc = ieee80211_get_qos_ctl(hdr);
Tomas Winkler54dbb522008-05-15 13:54:06 +0800295 tid = qc[0] & 0xf;
296 } else
Emmanuel Grumbach5f85a782011-08-25 23:11:18 -0700297 return IWL_MAX_TID_COUNT;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200298
Johannes Berga8448552011-12-02 12:25:12 -0800299 if (unlikely(tid >= IWL_MAX_TID_COUNT))
Emmanuel Grumbach5f85a782011-08-25 23:11:18 -0700300 return IWL_MAX_TID_COUNT;
Reinette Chatree6a6cf42009-08-13 13:30:50 -0700301
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200302 tl = &lq_data->load[tid];
303
304 curr_time -= curr_time % TID_ROUND_VALUE;
305
306 /* Happens only for the first packet. Initialize the data */
307 if (!(tl->queue_count)) {
308 tl->total = 1;
309 tl->time_stamp = curr_time;
310 tl->queue_count = 1;
311 tl->head = 0;
312 tl->packet_count[0] = 1;
Emmanuel Grumbach5f85a782011-08-25 23:11:18 -0700313 return IWL_MAX_TID_COUNT;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200314 }
315
316 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
317 index = time_diff / TID_QUEUE_CELL_SPACING;
318
319 /* The history is too long: remove data that is older than */
320 /* TID_MAX_TIME_DIFF */
321 if (index >= TID_QUEUE_MAX_SIZE)
322 rs_tl_rm_old_stats(tl, curr_time);
323
324 index = (tl->head + index) % TID_QUEUE_MAX_SIZE;
325 tl->packet_count[index] = tl->packet_count[index] + 1;
326 tl->total = tl->total + 1;
327
328 if ((index + 1) > tl->queue_count)
329 tl->queue_count = index + 1;
Ron Rindjunskyfaa29712008-06-12 09:46:58 +0800330
331 return tid;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200332}
333
Wey-Yi Guy54a430c2011-05-20 11:56:18 -0700334#ifdef CONFIG_MAC80211_DEBUGFS
Wey-Yi Guy4e308112011-07-08 08:46:28 -0700335/**
336 * Program the device to use fixed rate for frame transmit
337 * This is for debugging/testing only
338 * once the device start use fixed rate, we need to reload the module
339 * to being back the normal operation.
340 */
Wey-Yi Guy64898542011-05-03 18:05:13 -0700341static void rs_program_fix_rate(struct iwl_priv *priv,
342 struct iwl_lq_sta *lq_sta)
343{
344 struct iwl_station_priv *sta_priv =
345 container_of(lq_sta, struct iwl_station_priv, lq_sta);
Johannes Bergc6892902011-09-20 15:37:21 -0700346 struct iwl_rxon_context *ctx = sta_priv->ctx;
Wey-Yi Guy64898542011-05-03 18:05:13 -0700347
348 lq_sta->active_legacy_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
349 lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
350 lq_sta->active_mimo2_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
351 lq_sta->active_mimo3_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
352
Wey-Yi Guy64898542011-05-03 18:05:13 -0700353 IWL_DEBUG_RATE(priv, "sta_id %d rate 0x%X\n",
Wey-Yi Guy4e308112011-07-08 08:46:28 -0700354 lq_sta->lq.sta_id, lq_sta->dbg_fixed_rate);
Wey-Yi Guy64898542011-05-03 18:05:13 -0700355
Wey-Yi Guy4e308112011-07-08 08:46:28 -0700356 if (lq_sta->dbg_fixed_rate) {
357 rs_fill_link_cmd(NULL, lq_sta, lq_sta->dbg_fixed_rate);
Wey-Yi Guy64898542011-05-03 18:05:13 -0700358 iwl_send_lq_cmd(lq_sta->drv, ctx, &lq_sta->lq, CMD_ASYNC,
359 false);
360 }
361}
362#endif
363
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200364/*
365 get the traffic load value for tid
366*/
Tomas Winklere227cea2008-07-18 13:53:05 +0800367static u32 rs_tl_get_load(struct iwl_lq_sta *lq_data, u8 tid)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200368{
369 u32 curr_time = jiffies_to_msecs(jiffies);
370 u32 time_diff;
371 s32 index;
Tomas Winklere227cea2008-07-18 13:53:05 +0800372 struct iwl_traffic_load *tl = NULL;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200373
Johannes Berga8448552011-12-02 12:25:12 -0800374 if (tid >= IWL_MAX_TID_COUNT)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200375 return 0;
376
377 tl = &(lq_data->load[tid]);
378
379 curr_time -= curr_time % TID_ROUND_VALUE;
380
381 if (!(tl->queue_count))
382 return 0;
383
384 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
385 index = time_diff / TID_QUEUE_CELL_SPACING;
386
387 /* The history is too long: remove data that is older than */
388 /* TID_MAX_TIME_DIFF */
389 if (index >= TID_QUEUE_MAX_SIZE)
390 rs_tl_rm_old_stats(tl, curr_time);
391
392 return tl->total;
393}
394
Wey-Yi Guy82ca9342010-04-12 14:02:36 -0700395static int rs_tl_turn_on_agg_for_tid(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +0800396 struct iwl_lq_sta *lq_data, u8 tid,
Johannes Berg4b7679a2008-09-18 18:14:18 +0200397 struct ieee80211_sta *sta)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200398{
Wey-Yi Guy82ca9342010-04-12 14:02:36 -0700399 int ret = -EAGAIN;
Johannes Berg290f5992010-08-23 07:56:58 -0700400 u32 load;
401
402 /*
403 * Don't create TX aggregation sessions when in high
404 * BT traffic, as they would just be disrupted by BT.
405 */
406 if (priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH) {
Johannes Berg6690c012013-01-28 09:58:31 +0100407 IWL_DEBUG_COEX(priv,
408 "BT traffic (%d), no aggregation allowed\n",
409 priv->bt_traffic_load);
Johannes Berg290f5992010-08-23 07:56:58 -0700410 return ret;
411 }
412
413 load = rs_tl_get_load(lq_data, tid);
Wey-Yi Guy45d42702010-02-03 12:24:44 -0800414
Emmanuel Grumbach44cc4292013-06-18 06:33:49 +0300415 IWL_DEBUG_HT(priv, "Starting Tx agg: STA: %pM tid: %d\n",
416 sta->addr, tid);
417 ret = ieee80211_start_tx_ba_session(sta, tid, 5000);
418 if (ret == -EAGAIN) {
419 /*
420 * driver and mac80211 is out of sync
421 * this might be cause by reloading firmware
422 * stop the tx ba session here
423 */
424 IWL_ERR(priv, "Fail start Tx agg on tid: %d\n",
425 tid);
426 ieee80211_stop_tx_ba_session(sta, tid);
Andy Lutomirski24110542010-07-25 13:14:29 -0400427 }
Wey-Yi Guy82ca9342010-04-12 14:02:36 -0700428 return ret;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200429}
430
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700431static void rs_tl_turn_on_agg(struct iwl_priv *priv, u8 tid,
Tomas Winklere227cea2008-07-18 13:53:05 +0800432 struct iwl_lq_sta *lq_data,
Johannes Berg4b7679a2008-09-18 18:14:18 +0200433 struct ieee80211_sta *sta)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200434{
Johannes Berga8448552011-12-02 12:25:12 -0800435 if (tid < IWL_MAX_TID_COUNT)
Wey-Yi Guycfecc6b2010-06-18 11:33:15 -0700436 rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
437 else
Johannes Berga8448552011-12-02 12:25:12 -0800438 IWL_ERR(priv, "tid exceeds max TID count: %d/%d\n",
439 tid, IWL_MAX_TID_COUNT);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200440}
441
Guy Cohen39e88502008-04-23 17:14:57 -0700442static inline int get_num_of_ant_from_rate(u32 rate_n_flags)
Guy Cohenfde0db32008-04-21 15:42:01 -0700443{
Tomas Winkler3ac7f142008-07-21 02:40:14 +0300444 return !!(rate_n_flags & RATE_MCS_ANT_A_MSK) +
445 !!(rate_n_flags & RATE_MCS_ANT_B_MSK) +
446 !!(rate_n_flags & RATE_MCS_ANT_C_MSK);
Guy Cohenfde0db32008-04-21 15:42:01 -0700447}
448
Shanyu Zhao04f2dec2010-03-19 13:34:45 -0700449/*
450 * Static function to get the expected throughput from an iwl_scale_tbl_info
451 * that wraps a NULL pointer check
452 */
453static s32 get_expected_tpt(struct iwl_scale_tbl_info *tbl, int rs_index)
454{
455 if (tbl->expected_tpt)
456 return tbl->expected_tpt[rs_index];
457 return 0;
458}
459
Ben Cahill77626352007-11-29 11:09:44 +0800460/**
461 * rs_collect_tx_data - Update the success/failure sliding window
462 *
463 * We keep a sliding window of the last 62 packets transmitted
464 * at this rate. window->data contains the bitmask of successful
465 * packets.
466 */
Shanyu Zhao04f2dec2010-03-19 13:34:45 -0700467static int rs_collect_tx_data(struct iwl_scale_tbl_info *tbl,
468 int scale_index, int attempts, int successes)
Zhu Yib481de92007-09-25 17:54:57 -0700469{
Tomas Winklere227cea2008-07-18 13:53:05 +0800470 struct iwl_rate_scale_data *window = NULL;
Guy Cohen39e88502008-04-23 17:14:57 -0700471 static const u64 mask = (((u64)1) << (IWL_RATE_MAX_WINDOW - 1));
Shanyu Zhao04f2dec2010-03-19 13:34:45 -0700472 s32 fail_count, tpt;
Zhu Yib481de92007-09-25 17:54:57 -0700473
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800474 if (scale_index < 0 || scale_index >= IWL_RATE_COUNT)
475 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -0700476
Daniel C Halperine3949d62009-09-17 10:43:50 -0700477 /* Select window for current tx bit rate */
Shanyu Zhao04f2dec2010-03-19 13:34:45 -0700478 window = &(tbl->win[scale_index]);
479
480 /* Get expected throughput */
481 tpt = get_expected_tpt(tbl, scale_index);
Zhu Yib481de92007-09-25 17:54:57 -0700482
Ben Cahill77626352007-11-29 11:09:44 +0800483 /*
484 * Keep track of only the latest 62 tx frame attempts in this rate's
485 * history window; anything older isn't really relevant any more.
486 * If we have filled up the sliding window, drop the oldest attempt;
487 * if the oldest attempt (highest bit in bitmap) shows "success",
488 * subtract "1" from the success counter (this is the main reason
489 * we keep these bitmaps!).
490 */
Daniel C Halperine3949d62009-09-17 10:43:50 -0700491 while (attempts > 0) {
Guy Cohen39e88502008-04-23 17:14:57 -0700492 if (window->counter >= IWL_RATE_MAX_WINDOW) {
493
494 /* remove earliest */
495 window->counter = IWL_RATE_MAX_WINDOW - 1;
496
Ron Rindjunsky99556432008-01-28 14:07:25 +0200497 if (window->data & mask) {
498 window->data &= ~mask;
Guy Cohen39e88502008-04-23 17:14:57 -0700499 window->success_counter--;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200500 }
Zhu Yib481de92007-09-25 17:54:57 -0700501 }
Zhu Yib481de92007-09-25 17:54:57 -0700502
Ron Rindjunsky99556432008-01-28 14:07:25 +0200503 /* Increment frames-attempted counter */
504 window->counter++;
Ben Cahill77626352007-11-29 11:09:44 +0800505
Daniel C Halperine3949d62009-09-17 10:43:50 -0700506 /* Shift bitmap by one frame to throw away oldest history */
Guy Cohen90d77952008-09-09 10:54:53 +0800507 window->data <<= 1;
Daniel C Halperine3949d62009-09-17 10:43:50 -0700508
509 /* Mark the most recent #successes attempts as successful */
Ron Rindjunsky99556432008-01-28 14:07:25 +0200510 if (successes > 0) {
Guy Cohen39e88502008-04-23 17:14:57 -0700511 window->success_counter++;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200512 window->data |= 0x1;
513 successes--;
514 }
515
Daniel C Halperine3949d62009-09-17 10:43:50 -0700516 attempts--;
Zhu Yib481de92007-09-25 17:54:57 -0700517 }
518
Ben Cahill77626352007-11-29 11:09:44 +0800519 /* Calculate current success ratio, avoid divide-by-0! */
Zhu Yib481de92007-09-25 17:54:57 -0700520 if (window->counter > 0)
521 window->success_ratio = 128 * (100 * window->success_counter)
522 / window->counter;
523 else
524 window->success_ratio = IWL_INVALID_VALUE;
525
526 fail_count = window->counter - window->success_counter;
527
Ben Cahill77626352007-11-29 11:09:44 +0800528 /* Calculate average throughput, if we have enough history. */
Zhu Yib481de92007-09-25 17:54:57 -0700529 if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
530 (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
531 window->average_tpt = (window->success_ratio * tpt + 64) / 128;
532 else
533 window->average_tpt = IWL_INVALID_VALUE;
534
Ben Cahill77626352007-11-29 11:09:44 +0800535 /* Tag this window as having been updated */
Zhu Yib481de92007-09-25 17:54:57 -0700536 window->stamp = jiffies;
537
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800538 return 0;
Zhu Yib481de92007-09-25 17:54:57 -0700539}
540
Ben Cahill77626352007-11-29 11:09:44 +0800541/*
542 * Fill uCode API rate_n_flags field, based on "search" or "active" table.
543 */
Guy Cohen39e88502008-04-23 17:14:57 -0700544/* FIXME:RS:remove this function and put the flags statically in the table */
Tomas Winkler978785a2008-12-19 10:37:31 +0800545static u32 rate_n_flags_from_tbl(struct iwl_priv *priv,
546 struct iwl_scale_tbl_info *tbl,
547 int index, u8 use_green)
Zhu Yib481de92007-09-25 17:54:57 -0700548{
Guy Cohen39e88502008-04-23 17:14:57 -0700549 u32 rate_n_flags = 0;
550
Zhu Yib481de92007-09-25 17:54:57 -0700551 if (is_legacy(tbl->lq_type)) {
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800552 rate_n_flags = iwl_rates[index].plcp;
Zhu Yib481de92007-09-25 17:54:57 -0700553 if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
Guy Cohen39e88502008-04-23 17:14:57 -0700554 rate_n_flags |= RATE_MCS_CCK_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700555
Guy Cohenfde0db32008-04-21 15:42:01 -0700556 } else if (is_Ht(tbl->lq_type)) {
557 if (index > IWL_LAST_OFDM_RATE) {
Tomas Winkler978785a2008-12-19 10:37:31 +0800558 IWL_ERR(priv, "Invalid HT rate index %d\n", index);
Zhu Yib481de92007-09-25 17:54:57 -0700559 index = IWL_LAST_OFDM_RATE;
Guy Cohenfde0db32008-04-21 15:42:01 -0700560 }
Guy Cohen39e88502008-04-23 17:14:57 -0700561 rate_n_flags = RATE_MCS_HT_MSK;
Guy Cohenfde0db32008-04-21 15:42:01 -0700562
563 if (is_siso(tbl->lq_type))
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800564 rate_n_flags |= iwl_rates[index].plcp_siso;
Guy Cohenfde0db32008-04-21 15:42:01 -0700565 else if (is_mimo2(tbl->lq_type))
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800566 rate_n_flags |= iwl_rates[index].plcp_mimo2;
Guy Cohenfde0db32008-04-21 15:42:01 -0700567 else
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800568 rate_n_flags |= iwl_rates[index].plcp_mimo3;
Zhu Yib481de92007-09-25 17:54:57 -0700569 } else {
Tomas Winkler978785a2008-12-19 10:37:31 +0800570 IWL_ERR(priv, "Invalid tbl->lq_type %d\n", tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -0700571 }
572
Guy Cohen39e88502008-04-23 17:14:57 -0700573 rate_n_flags |= ((tbl->ant_type << RATE_MCS_ANT_POS) &
Guy Cohenfde0db32008-04-21 15:42:01 -0700574 RATE_MCS_ANT_ABC_MSK);
Zhu Yib481de92007-09-25 17:54:57 -0700575
Guy Cohen39e88502008-04-23 17:14:57 -0700576 if (is_Ht(tbl->lq_type)) {
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700577 if (tbl->is_ht40) {
Guy Cohen39e88502008-04-23 17:14:57 -0700578 if (tbl->is_dup)
579 rate_n_flags |= RATE_MCS_DUP_MSK;
580 else
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700581 rate_n_flags |= RATE_MCS_HT40_MSK;
Guy Cohen39e88502008-04-23 17:14:57 -0700582 }
583 if (tbl->is_SGI)
584 rate_n_flags |= RATE_MCS_SGI_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700585
Guy Cohen39e88502008-04-23 17:14:57 -0700586 if (use_green) {
587 rate_n_flags |= RATE_MCS_GF_MSK;
588 if (is_siso(tbl->lq_type) && tbl->is_SGI) {
589 rate_n_flags &= ~RATE_MCS_SGI_MSK;
Tomas Winkler978785a2008-12-19 10:37:31 +0800590 IWL_ERR(priv, "GF was set with SGI:SISO\n");
Guy Cohen39e88502008-04-23 17:14:57 -0700591 }
592 }
Zhu Yib481de92007-09-25 17:54:57 -0700593 }
Guy Cohen39e88502008-04-23 17:14:57 -0700594 return rate_n_flags;
Zhu Yib481de92007-09-25 17:54:57 -0700595}
596
Ben Cahill77626352007-11-29 11:09:44 +0800597/*
598 * Interpret uCode API's rate_n_flags format,
599 * fill "search" or "active" tx mode table.
600 */
Guy Cohen39e88502008-04-23 17:14:57 -0700601static int rs_get_tbl_info_from_mcs(const u32 rate_n_flags,
Johannes Berg8318d782008-01-24 19:38:38 +0100602 enum ieee80211_band band,
Tomas Winklere227cea2008-07-18 13:53:05 +0800603 struct iwl_scale_tbl_info *tbl,
Zhu Yib481de92007-09-25 17:54:57 -0700604 int *rate_idx)
605{
Guy Cohen39e88502008-04-23 17:14:57 -0700606 u32 ant_msk = (rate_n_flags & RATE_MCS_ANT_ABC_MSK);
607 u8 num_of_ant = get_num_of_ant_from_rate(rate_n_flags);
608 u8 mcs;
Zhu Yib481de92007-09-25 17:54:57 -0700609
Wey-Yi Guy80e91582010-08-03 08:23:32 -0700610 memset(tbl, 0, sizeof(struct iwl_scale_tbl_info));
Tomas Winklere7d326ac2008-06-12 09:47:11 +0800611 *rate_idx = iwl_hwrate_to_plcp_idx(rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -0700612
Guy Cohenfde0db32008-04-21 15:42:01 -0700613 if (*rate_idx == IWL_RATE_INVALID) {
Zhu Yib481de92007-09-25 17:54:57 -0700614 *rate_idx = -1;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800615 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -0700616 }
Ben Cahill77626352007-11-29 11:09:44 +0800617 tbl->is_SGI = 0; /* default legacy setup */
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700618 tbl->is_ht40 = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700619 tbl->is_dup = 0;
Guy Cohenfde0db32008-04-21 15:42:01 -0700620 tbl->ant_type = (ant_msk >> RATE_MCS_ANT_POS);
621 tbl->lq_type = LQ_NONE;
Mohamed Abbasd6e93392009-05-08 13:44:39 -0700622 tbl->max_search = IWL_MAX_SEARCH;
Zhu Yib481de92007-09-25 17:54:57 -0700623
Ben Cahill77626352007-11-29 11:09:44 +0800624 /* legacy rate format */
Guy Cohen39e88502008-04-23 17:14:57 -0700625 if (!(rate_n_flags & RATE_MCS_HT_MSK)) {
Guy Cohenfde0db32008-04-21 15:42:01 -0700626 if (num_of_ant == 1) {
Johannes Berg8318d782008-01-24 19:38:38 +0100627 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700628 tbl->lq_type = LQ_A;
629 else
630 tbl->lq_type = LQ_G;
Zhu Yib481de92007-09-25 17:54:57 -0700631 }
Guy Cohenfde0db32008-04-21 15:42:01 -0700632 /* HT rate format */
Zhu Yib481de92007-09-25 17:54:57 -0700633 } else {
Guy Cohen39e88502008-04-23 17:14:57 -0700634 if (rate_n_flags & RATE_MCS_SGI_MSK)
Zhu Yib481de92007-09-25 17:54:57 -0700635 tbl->is_SGI = 1;
636
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700637 if ((rate_n_flags & RATE_MCS_HT40_MSK) ||
Guy Cohen39e88502008-04-23 17:14:57 -0700638 (rate_n_flags & RATE_MCS_DUP_MSK))
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700639 tbl->is_ht40 = 1;
Zhu Yib481de92007-09-25 17:54:57 -0700640
Guy Cohen39e88502008-04-23 17:14:57 -0700641 if (rate_n_flags & RATE_MCS_DUP_MSK)
Zhu Yib481de92007-09-25 17:54:57 -0700642 tbl->is_dup = 1;
Guy Cohenfde0db32008-04-21 15:42:01 -0700643
Guy Cohen39e88502008-04-23 17:14:57 -0700644 mcs = rs_extract_rate(rate_n_flags);
Guy Cohenfde0db32008-04-21 15:42:01 -0700645
Guy Cohen39e88502008-04-23 17:14:57 -0700646 /* SISO */
647 if (mcs <= IWL_RATE_SISO_60M_PLCP) {
Guy Cohenfde0db32008-04-21 15:42:01 -0700648 if (num_of_ant == 1)
649 tbl->lq_type = LQ_SISO; /*else NONE*/
650 /* MIMO2 */
Guy Cohen39e88502008-04-23 17:14:57 -0700651 } else if (mcs <= IWL_RATE_MIMO2_60M_PLCP) {
Guy Cohenfde0db32008-04-21 15:42:01 -0700652 if (num_of_ant == 2)
653 tbl->lq_type = LQ_MIMO2;
654 /* MIMO3 */
655 } else {
Mohamed Abbasd6e93392009-05-08 13:44:39 -0700656 if (num_of_ant == 3) {
657 tbl->max_search = IWL_MAX_11N_MIMO3_SEARCH;
Guy Cohenfde0db32008-04-21 15:42:01 -0700658 tbl->lq_type = LQ_MIMO3;
Mohamed Abbasd6e93392009-05-08 13:44:39 -0700659 }
Guy Cohenfde0db32008-04-21 15:42:01 -0700660 }
Zhu Yib481de92007-09-25 17:54:57 -0700661 }
662 return 0;
663}
Guy Cohenaade00c2008-04-23 17:14:59 -0700664
665/* switch to another antenna/antennas and return 1 */
666/* if no other valid antenna found, return 0 */
667static int rs_toggle_antenna(u32 valid_ant, u32 *rate_n_flags,
Tomas Winklere227cea2008-07-18 13:53:05 +0800668 struct iwl_scale_tbl_info *tbl)
Zhu Yib481de92007-09-25 17:54:57 -0700669{
Guy Cohenaade00c2008-04-23 17:14:59 -0700670 u8 new_ant_type;
671
672 if (!tbl->ant_type || tbl->ant_type > ANT_ABC)
673 return 0;
674
675 if (!rs_is_valid_ant(valid_ant, tbl->ant_type))
676 return 0;
677
678 new_ant_type = ant_toggle_lookup[tbl->ant_type];
679
680 while ((new_ant_type != tbl->ant_type) &&
681 !rs_is_valid_ant(valid_ant, new_ant_type))
682 new_ant_type = ant_toggle_lookup[new_ant_type];
683
684 if (new_ant_type == tbl->ant_type)
685 return 0;
686
687 tbl->ant_type = new_ant_type;
688 *rate_n_flags &= ~RATE_MCS_ANT_ABC_MSK;
689 *rate_n_flags |= new_ant_type << RATE_MCS_ANT_POS;
690 return 1;
Zhu Yib481de92007-09-25 17:54:57 -0700691}
692
Daniel C Halperinb2617932009-08-13 13:30:59 -0700693/**
694 * Green-field mode is valid if the station supports it and
695 * there are no non-GF stations present in the BSS.
696 */
Johannes Berg7e6a5882010-08-23 10:46:46 +0200697static bool rs_use_green(struct ieee80211_sta *sta)
Zhu Yib481de92007-09-25 17:54:57 -0700698{
Johannes Berg50e2a302012-08-05 18:31:46 +0200699 /*
700 * There's a bug somewhere in this code that causes the
701 * scaling to get stuck because GF+SGI can't be combined
702 * in SISO rates. Until we find that bug, disable GF, it
703 * has only limited benefit and we still interoperate with
704 * GF APs since we can always receive GF transmissions.
705 */
706 return false;
Guy Cohenf935a6d2008-04-23 17:15:02 -0700707}
Zhu Yib481de92007-09-25 17:54:57 -0700708
709/**
710 * rs_get_supported_rates - get the available rates
711 *
712 * if management frame or broadcast frame only return
713 * basic available rates.
714 *
715 */
Tomas Winklere227cea2008-07-18 13:53:05 +0800716static u16 rs_get_supported_rates(struct iwl_lq_sta *lq_sta,
717 struct ieee80211_hdr *hdr,
718 enum iwl_table_type rate_type)
Zhu Yib481de92007-09-25 17:54:57 -0700719{
Guy Coheneecd6e52008-04-23 17:14:58 -0700720 if (is_legacy(rate_type)) {
721 return lq_sta->active_legacy_rate;
722 } else {
723 if (is_siso(rate_type))
724 return lq_sta->active_siso_rate;
725 else if (is_mimo2(rate_type))
726 return lq_sta->active_mimo2_rate;
727 else
728 return lq_sta->active_mimo3_rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -0800729 }
Zhu Yib481de92007-09-25 17:54:57 -0700730}
731
Ester Kummerbf403db2008-05-05 10:22:40 +0800732static u16 rs_get_adjacent_rate(struct iwl_priv *priv, u8 index, u16 rate_mask,
733 int rate_type)
Zhu Yib481de92007-09-25 17:54:57 -0700734{
735 u8 high = IWL_RATE_INVALID;
736 u8 low = IWL_RATE_INVALID;
737
Ian Schram01ebd062007-10-25 17:15:22 +0800738 /* 802.11A or ht walks to the next literal adjacent rate in
Zhu Yib481de92007-09-25 17:54:57 -0700739 * the rate table */
740 if (is_a_band(rate_type) || !is_legacy(rate_type)) {
741 int i;
742 u32 mask;
743
744 /* Find the previous rate that is in the rate mask */
745 i = index - 1;
746 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
747 if (rate_mask & mask) {
748 low = i;
749 break;
750 }
751 }
752
753 /* Find the next rate that is in the rate mask */
754 i = index + 1;
755 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
756 if (rate_mask & mask) {
757 high = i;
758 break;
759 }
760 }
761
762 return (high << 8) | low;
763 }
764
765 low = index;
766 while (low != IWL_RATE_INVALID) {
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800767 low = iwl_rates[low].prev_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700768 if (low == IWL_RATE_INVALID)
769 break;
770 if (rate_mask & (1 << low))
771 break;
Tomas Winklere1623442009-01-27 14:27:56 -0800772 IWL_DEBUG_RATE(priv, "Skipping masked lower rate: %d\n", low);
Zhu Yib481de92007-09-25 17:54:57 -0700773 }
774
775 high = index;
776 while (high != IWL_RATE_INVALID) {
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800777 high = iwl_rates[high].next_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700778 if (high == IWL_RATE_INVALID)
779 break;
780 if (rate_mask & (1 << high))
781 break;
Tomas Winklere1623442009-01-27 14:27:56 -0800782 IWL_DEBUG_RATE(priv, "Skipping masked higher rate: %d\n", high);
Zhu Yib481de92007-09-25 17:54:57 -0700783 }
784
785 return (high << 8) | low;
786}
787
Tomas Winklere227cea2008-07-18 13:53:05 +0800788static u32 rs_get_lower_rate(struct iwl_lq_sta *lq_sta,
789 struct iwl_scale_tbl_info *tbl,
790 u8 scale_index, u8 ht_possible)
Zhu Yib481de92007-09-25 17:54:57 -0700791{
Zhu Yib481de92007-09-25 17:54:57 -0700792 s32 low;
793 u16 rate_mask;
794 u16 high_low;
795 u8 switch_to_legacy = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -0800796 u8 is_green = lq_sta->is_green;
Wey-Yi Guy2ff65782009-09-11 10:38:18 -0700797 struct iwl_priv *priv = lq_sta->drv;
Zhu Yib481de92007-09-25 17:54:57 -0700798
799 /* check if we need to switch from HT to legacy rates.
800 * assumption is that mandatory rates (1Mbps or 6Mbps)
801 * are always supported (spec demand) */
802 if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_index)) {
803 switch_to_legacy = 1;
804 scale_index = rs_ht_to_legacy[scale_index];
Johannes Berg8318d782008-01-24 19:38:38 +0100805 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700806 tbl->lq_type = LQ_A;
807 else
808 tbl->lq_type = LQ_G;
809
Guy Cohen69fdb302008-04-23 17:15:01 -0700810 if (num_of_ant(tbl->ant_type) > 1)
Wey-Yi Guy2ff65782009-09-11 10:38:18 -0700811 tbl->ant_type =
Eytan Lifshitzb7998c82012-12-01 20:59:49 +0200812 first_antenna(priv->nvm_data->valid_tx_ant);
Zhu Yib481de92007-09-25 17:54:57 -0700813
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700814 tbl->is_ht40 = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700815 tbl->is_SGI = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -0700816 tbl->max_search = IWL_MAX_SEARCH;
Zhu Yib481de92007-09-25 17:54:57 -0700817 }
818
Guy Coheneecd6e52008-04-23 17:14:58 -0700819 rate_mask = rs_get_supported_rates(lq_sta, NULL, tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -0700820
Ben Cahill77626352007-11-29 11:09:44 +0800821 /* Mask with station rate restriction */
Zhu Yib481de92007-09-25 17:54:57 -0700822 if (is_legacy(tbl->lq_type)) {
Ben Cahill77626352007-11-29 11:09:44 +0800823 /* supp_rates has no CCK bits in A mode */
Johannes Berg8318d782008-01-24 19:38:38 +0100824 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700825 rate_mask = (u16)(rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -0800826 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
Zhu Yib481de92007-09-25 17:54:57 -0700827 else
Tomas Winklerc33104f2008-01-14 17:46:21 -0800828 rate_mask = (u16)(rate_mask & lq_sta->supp_rates);
Zhu Yib481de92007-09-25 17:54:57 -0700829 }
830
Ben Cahill77626352007-11-29 11:09:44 +0800831 /* If we switched from HT to legacy, check current rate */
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800832 if (switch_to_legacy && (rate_mask & (1 << scale_index))) {
Guy Cohen39e88502008-04-23 17:14:57 -0700833 low = scale_index;
834 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700835 }
836
Ester Kummerbf403db2008-05-05 10:22:40 +0800837 high_low = rs_get_adjacent_rate(lq_sta->drv, scale_index, rate_mask,
838 tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -0700839 low = high_low & 0xff;
840
Guy Cohen39e88502008-04-23 17:14:57 -0700841 if (low == IWL_RATE_INVALID)
842 low = scale_index;
843
844out:
Tomas Winkler978785a2008-12-19 10:37:31 +0800845 return rate_n_flags_from_tbl(lq_sta->drv, tbl, low, is_green);
Zhu Yib481de92007-09-25 17:54:57 -0700846}
847
Ben Cahill77626352007-11-29 11:09:44 +0800848/*
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700849 * Simple function to compare two rate scale table types
850 */
851static bool table_type_matches(struct iwl_scale_tbl_info *a,
852 struct iwl_scale_tbl_info *b)
853{
854 return (a->lq_type == b->lq_type) && (a->ant_type == b->ant_type) &&
855 (a->is_SGI == b->is_SGI);
856}
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700857
Johannes Berg7e6a5882010-08-23 10:46:46 +0200858static void rs_bt_update_lq(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
859 struct iwl_lq_sta *lq_sta)
Wey-Yi Guybee008b2010-08-23 07:57:04 -0700860{
861 struct iwl_scale_tbl_info *tbl;
Wey-Yi Guy66e863a52010-11-08 14:54:37 -0800862 bool full_concurrent = priv->bt_full_concurrent;
Wey-Yi Guybee008b2010-08-23 07:57:04 -0700863
Wey-Yi Guy66e863a52010-11-08 14:54:37 -0800864 if (priv->bt_ant_couple_ok) {
865 /*
866 * Is there a need to switch between
867 * full concurrency and 3-wire?
868 */
Wey-Yi Guy66e863a52010-11-08 14:54:37 -0800869 if (priv->bt_ci_compliance && priv->bt_ant_couple_ok)
870 full_concurrent = true;
871 else
872 full_concurrent = false;
Wey-Yi Guy66e863a52010-11-08 14:54:37 -0800873 }
874 if ((priv->bt_traffic_load != priv->last_bt_traffic_load) ||
875 (priv->bt_full_concurrent != full_concurrent)) {
Wey-Yi Guybee008b2010-08-23 07:57:04 -0700876 priv->bt_full_concurrent = full_concurrent;
Meenakshi Venkataraman882dde82012-05-16 22:35:57 +0200877 priv->last_bt_traffic_load = priv->bt_traffic_load;
Wey-Yi Guybee008b2010-08-23 07:57:04 -0700878
879 /* Update uCode's rate table. */
880 tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
881 rs_fill_link_cmd(priv, lq_sta, tbl->current_rate);
Johannes Berg7e6a5882010-08-23 10:46:46 +0200882 iwl_send_lq_cmd(priv, ctx, &lq_sta->lq, CMD_ASYNC, false);
Wey-Yi Guybee008b2010-08-23 07:57:04 -0700883
Johannes Berg1ee158d2012-02-17 10:07:44 -0800884 queue_work(priv->workqueue, &priv->bt_full_concurrency);
Wey-Yi Guybee008b2010-08-23 07:57:04 -0700885 }
886}
887
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700888/*
Ben Cahill77626352007-11-29 11:09:44 +0800889 * mac80211 sends us Tx status
890 */
Johannes Berg4b7679a2008-09-18 18:14:18 +0200891static void rs_tx_status(void *priv_r, struct ieee80211_supported_band *sband,
892 struct ieee80211_sta *sta, void *priv_sta,
Johannes Berge039fa42008-05-15 12:55:29 +0200893 struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -0700894{
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700895 int legacy_success;
896 int retries;
897 int rs_index, mac_index, i;
Tomas Winkler417f1142008-11-12 13:14:06 -0800898 struct iwl_lq_sta *lq_sta = priv_sta;
Tomas Winkler66c73db2008-04-15 16:01:40 -0700899 struct iwl_link_quality_cmd *table;
Zhu Yib481de92007-09-25 17:54:57 -0700900 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Emmanuel Grumbachd0f76d62012-02-09 16:08:15 +0200901 struct iwl_op_mode *op_mode = (struct iwl_op_mode *)priv_r;
902 struct iwl_priv *priv = IWL_OP_MODE_GET_DVM(op_mode);
Johannes Berge039fa42008-05-15 12:55:29 +0200903 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Daniel C Halperin50273092009-08-28 09:44:45 -0700904 enum mac80211_rate_control_flags mac_flags;
Guy Cohen39e88502008-04-23 17:14:57 -0700905 u32 tx_rate;
Tomas Winklere227cea2008-07-18 13:53:05 +0800906 struct iwl_scale_tbl_info tbl_type;
Shanyu Zhao04f2dec2010-03-19 13:34:45 -0700907 struct iwl_scale_tbl_info *curr_tbl, *other_tbl, *tmp_tbl;
Johannes Berg7e6a5882010-08-23 10:46:46 +0200908 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
Johannes Bergc6892902011-09-20 15:37:21 -0700909 struct iwl_rxon_context *ctx = sta_priv->ctx;
Zhu Yib481de92007-09-25 17:54:57 -0700910
Tomas Winklere1623442009-01-27 14:27:56 -0800911 IWL_DEBUG_RATE_LIMIT(priv, "get frame ack response, update rate scale window\n");
Zhu Yib481de92007-09-25 17:54:57 -0700912
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800913 /* Treat uninitialized rate scaling data same as non-existing. */
914 if (!lq_sta) {
915 IWL_DEBUG_RATE(priv, "Station rate scaling not created yet.\n");
916 return;
917 } else if (!lq_sta->drv) {
918 IWL_DEBUG_RATE(priv, "Rate scaling not initialized yet.\n");
919 return;
920 }
921
Tomas Winkler417f1142008-11-12 13:14:06 -0800922 if (!ieee80211_is_data(hdr->frame_control) ||
Gábor Stefanik514d65c2009-04-23 19:36:08 +0200923 info->flags & IEEE80211_TX_CTL_NO_ACK)
Zhu Yib481de92007-09-25 17:54:57 -0700924 return;
925
Daniel C Halperine3949d62009-09-17 10:43:50 -0700926 /* This packet was aggregated but doesn't carry status info */
Johannes Berge039fa42008-05-15 12:55:29 +0200927 if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
928 !(info->flags & IEEE80211_TX_STAT_AMPDU))
Ron Rindjunsky99556432008-01-28 14:07:25 +0200929 return;
930
Ben Cahill77626352007-11-29 11:09:44 +0800931 /*
932 * Ignore this Tx frame response if its initial rate doesn't match
933 * that of latest Link Quality command. There may be stragglers
934 * from a previous Link Quality command, but we're no longer interested
935 * in those; they're either from the "active" mode while we're trying
936 * to check "search" mode, or a prior "search" mode after we've moved
937 * to a new "search" mode (which might become the new "active" mode).
938 */
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700939 table = &lq_sta->lq;
Guy Cohen39e88502008-04-23 17:14:57 -0700940 tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
941 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800942 if (priv->band == IEEE80211_BAND_5GHZ)
943 rs_index -= IWL_FIRST_OFDM_RATE;
Daniel C Halperin50273092009-08-28 09:44:45 -0700944 mac_flags = info->status.rates[0].flags;
945 mac_index = info->status.rates[0].idx;
Daniel C Halperin31513be2009-08-28 09:44:47 -0700946 /* For HT packets, map MCS to PLCP */
947 if (mac_flags & IEEE80211_TX_RC_MCS) {
948 mac_index &= RATE_MCS_CODE_MSK; /* Remove # of streams */
949 if (mac_index >= (IWL_RATE_9M_INDEX - IWL_FIRST_OFDM_RATE))
950 mac_index++;
Daniel C Halperin392a0ba2009-09-11 10:38:08 -0700951 /*
952 * mac80211 HT index is always zero-indexed; we need to move
953 * HT OFDM rates after CCK rates in 2.4 GHz band
954 */
955 if (priv->band == IEEE80211_BAND_2GHZ)
956 mac_index += IWL_FIRST_OFDM_RATE;
Daniel C Halperin31513be2009-08-28 09:44:47 -0700957 }
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700958 /* Here we actually compare this rate to the latest LQ command */
Daniel C Halperin50273092009-08-28 09:44:45 -0700959 if ((mac_index < 0) ||
960 (tbl_type.is_SGI != !!(mac_flags & IEEE80211_TX_RC_SHORT_GI)) ||
961 (tbl_type.is_ht40 != !!(mac_flags & IEEE80211_TX_RC_40_MHZ_WIDTH)) ||
962 (tbl_type.is_dup != !!(mac_flags & IEEE80211_TX_RC_DUP_DATA)) ||
Johannes Bergd748b462012-03-28 11:04:23 +0200963 (tbl_type.ant_type != info->status.antenna) ||
Daniel C Halperin50273092009-08-28 09:44:45 -0700964 (!!(tx_rate & RATE_MCS_HT_MSK) != !!(mac_flags & IEEE80211_TX_RC_MCS)) ||
965 (!!(tx_rate & RATE_MCS_GF_MSK) != !!(mac_flags & IEEE80211_TX_RC_GREEN_FIELD)) ||
Daniel C Halperin31513be2009-08-28 09:44:47 -0700966 (rs_index != mac_index)) {
967 IWL_DEBUG_RATE(priv, "initial rate %d does not match %d (0x%x)\n", mac_index, rs_index, tx_rate);
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700968 /*
969 * Since rates mis-match, the last LQ command may have failed.
970 * After IWL_MISSED_RATE_MAX mis-matches, resync the uCode with
971 * ... driver.
Mohamed Abbasd5e49032008-12-12 08:22:15 -0800972 */
Abbas, Mohamed7d049e52009-01-20 21:33:53 -0800973 lq_sta->missed_rate_counter++;
974 if (lq_sta->missed_rate_counter > IWL_MISSED_RATE_MAX) {
975 lq_sta->missed_rate_counter = 0;
Johannes Berg7e6a5882010-08-23 10:46:46 +0200976 iwl_send_lq_cmd(priv, ctx, &lq_sta->lq, CMD_ASYNC, false);
Abbas, Mohamed7d049e52009-01-20 21:33:53 -0800977 }
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700978 /* Regardless, ignore this status info for outdated rate */
979 return;
980 } else
981 /* Rate did match, so reset the missed_rate_counter */
982 lq_sta->missed_rate_counter = 0;
983
984 /* Figure out if rate scale algorithm is in active or search table */
985 if (table_type_matches(&tbl_type,
986 &(lq_sta->lq_info[lq_sta->active_tbl]))) {
987 curr_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
988 other_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
989 } else if (table_type_matches(&tbl_type,
990 &lq_sta->lq_info[1 - lq_sta->active_tbl])) {
991 curr_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
992 other_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
993 } else {
994 IWL_DEBUG_RATE(priv, "Neither active nor search matches tx rate\n");
Wey-Yi Guy80e91582010-08-03 08:23:32 -0700995 tmp_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
996 IWL_DEBUG_RATE(priv, "active- lq:%x, ant:%x, SGI:%d\n",
997 tmp_tbl->lq_type, tmp_tbl->ant_type, tmp_tbl->is_SGI);
998 tmp_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
999 IWL_DEBUG_RATE(priv, "search- lq:%x, ant:%x, SGI:%d\n",
1000 tmp_tbl->lq_type, tmp_tbl->ant_type, tmp_tbl->is_SGI);
1001 IWL_DEBUG_RATE(priv, "actual- lq:%x, ant:%x, SGI:%d\n",
1002 tbl_type.lq_type, tbl_type.ant_type, tbl_type.is_SGI);
1003 /*
1004 * no matching table found, let's by-pass the data collection
1005 * and continue to perform rate scale to find the rate table
1006 */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001007 rs_stay_in_table(lq_sta, true);
Wey-Yi Guy80e91582010-08-03 08:23:32 -07001008 goto done;
Zhu Yib481de92007-09-25 17:54:57 -07001009 }
Zhu Yib481de92007-09-25 17:54:57 -07001010
Ben Cahill77626352007-11-29 11:09:44 +08001011 /*
Daniel C Halperin95407aa2009-09-17 10:43:48 -07001012 * Updating the frame history depends on whether packets were
1013 * aggregated.
1014 *
1015 * For aggregation, all packets were transmitted at the same rate, the
1016 * first index into rate scale table.
Ben Cahill77626352007-11-29 11:09:44 +08001017 */
Daniel C Halperin95407aa2009-09-17 10:43:48 -07001018 if (info->flags & IEEE80211_TX_STAT_AMPDU) {
1019 tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
1020 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type,
1021 &rs_index);
Shanyu Zhao04f2dec2010-03-19 13:34:45 -07001022 rs_collect_tx_data(curr_tbl, rs_index,
Daniel Halperine3a3cd82010-04-18 09:27:58 -07001023 info->status.ampdu_len,
1024 info->status.ampdu_ack_len);
Zhu Yib481de92007-09-25 17:54:57 -07001025
Daniel C Halperin95407aa2009-09-17 10:43:48 -07001026 /* Update success/fail counts if not searching for new mode */
1027 if (lq_sta->stay_in_tbl) {
Daniel Halperine3a3cd82010-04-18 09:27:58 -07001028 lq_sta->total_success += info->status.ampdu_ack_len;
1029 lq_sta->total_failed += (info->status.ampdu_len -
1030 info->status.ampdu_ack_len);
Daniel C Halperin95407aa2009-09-17 10:43:48 -07001031 }
1032 } else {
1033 /*
1034 * For legacy, update frame history with for each Tx retry.
1035 */
1036 retries = info->status.rates[0].count - 1;
1037 /* HW doesn't send more than 15 retries */
1038 retries = min(retries, 15);
1039
1040 /* The last transmission may have been successful */
1041 legacy_success = !!(info->flags & IEEE80211_TX_STAT_ACK);
1042 /* Collect data for each rate used during failed TX attempts */
1043 for (i = 0; i <= retries; ++i) {
1044 tx_rate = le32_to_cpu(table->rs_table[i].rate_n_flags);
1045 rs_get_tbl_info_from_mcs(tx_rate, priv->band,
1046 &tbl_type, &rs_index);
1047 /*
1048 * Only collect stats if retried rate is in the same RS
1049 * table as active/search.
1050 */
1051 if (table_type_matches(&tbl_type, curr_tbl))
Shanyu Zhao04f2dec2010-03-19 13:34:45 -07001052 tmp_tbl = curr_tbl;
Daniel C Halperin95407aa2009-09-17 10:43:48 -07001053 else if (table_type_matches(&tbl_type, other_tbl))
Shanyu Zhao04f2dec2010-03-19 13:34:45 -07001054 tmp_tbl = other_tbl;
Ron Rindjunsky99556432008-01-28 14:07:25 +02001055 else
Daniel C Halperin95407aa2009-09-17 10:43:48 -07001056 continue;
Shanyu Zhao04f2dec2010-03-19 13:34:45 -07001057 rs_collect_tx_data(tmp_tbl, rs_index, 1,
1058 i < retries ? 0 : legacy_success);
Daniel C Halperin95407aa2009-09-17 10:43:48 -07001059 }
1060
1061 /* Update success/fail counts if not searching for new mode */
1062 if (lq_sta->stay_in_tbl) {
1063 lq_sta->total_success += legacy_success;
1064 lq_sta->total_failed += retries + (1 - legacy_success);
Ron Rindjunsky99556432008-01-28 14:07:25 +02001065 }
Zhu Yib481de92007-09-25 17:54:57 -07001066 }
Daniel C Halperin95407aa2009-09-17 10:43:48 -07001067 /* The last TX rate is cached in lq_sta; it's set in if/else above */
1068 lq_sta->last_rate_n_flags = tx_rate;
Wey-Yi Guy80e91582010-08-03 08:23:32 -07001069done:
Ben Cahill77626352007-11-29 11:09:44 +08001070 /* See if there's a better rate or modulation mode to try. */
Abbas, Mohamedc338ba32009-01-21 10:58:02 -08001071 if (sta && sta->supp_rates[sband->band])
Gábor Stefanik514d65c2009-04-23 19:36:08 +02001072 rs_rate_scale_perform(priv, skb, sta, lq_sta);
Wey-Yi Guyc10e2c12011-07-13 11:13:46 -07001073
Johannes Berg0d8877a2013-05-17 10:36:29 +02001074 if (priv->lib->bt_params && priv->lib->bt_params->advanced_bt_coexist)
Johannes Berg7e6a5882010-08-23 10:46:46 +02001075 rs_bt_update_lq(priv, ctx, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07001076}
1077
Ben Cahill77626352007-11-29 11:09:44 +08001078/*
1079 * Begin a period of staying with a selected modulation mode.
1080 * Set "stay_in_tbl" flag to prevent any mode switches.
1081 * Set frame tx success limits according to legacy vs. high-throughput,
1082 * and reset overall (spanning all rates) tx success history statistics.
1083 * These control how long we stay using same modulation mode before
1084 * searching for a new mode.
1085 */
Ester Kummerbf403db2008-05-05 10:22:40 +08001086static void rs_set_stay_in_table(struct iwl_priv *priv, u8 is_legacy,
Tomas Winklere227cea2008-07-18 13:53:05 +08001087 struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07001088{
Tomas Winklere1623442009-01-27 14:27:56 -08001089 IWL_DEBUG_RATE(priv, "we are staying in the same table\n");
Tomas Winklerc33104f2008-01-14 17:46:21 -08001090 lq_sta->stay_in_tbl = 1; /* only place this gets set */
Zhu Yib481de92007-09-25 17:54:57 -07001091 if (is_legacy) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001092 lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT;
1093 lq_sta->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
1094 lq_sta->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
Zhu Yib481de92007-09-25 17:54:57 -07001095 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001096 lq_sta->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
1097 lq_sta->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
1098 lq_sta->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
Zhu Yib481de92007-09-25 17:54:57 -07001099 }
Tomas Winklerc33104f2008-01-14 17:46:21 -08001100 lq_sta->table_count = 0;
1101 lq_sta->total_failed = 0;
1102 lq_sta->total_success = 0;
Mohamed Abbas447fee72009-04-20 14:37:02 -07001103 lq_sta->flush_timer = jiffies;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001104 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001105}
1106
Ben Cahill77626352007-11-29 11:09:44 +08001107/*
1108 * Find correct throughput table for given mode of modulation
1109 */
Tomas Winklere227cea2008-07-18 13:53:05 +08001110static void rs_set_expected_tpt_table(struct iwl_lq_sta *lq_sta,
1111 struct iwl_scale_tbl_info *tbl)
Zhu Yib481de92007-09-25 17:54:57 -07001112{
Daniel C Halperine3949d62009-09-17 10:43:50 -07001113 /* Used to choose among HT tables */
Johannes Berga34529e2014-01-20 22:57:21 +01001114 const u16 (*ht_tbl_pointer)[IWL_RATE_COUNT];
Daniel C Halperine3949d62009-09-17 10:43:50 -07001115
1116 /* Check for invalid LQ type */
1117 if (WARN_ON_ONCE(!is_legacy(tbl->lq_type) && !is_Ht(tbl->lq_type))) {
1118 tbl->expected_tpt = expected_tpt_legacy;
1119 return;
1120 }
1121
1122 /* Legacy rates have only one table */
Zhu Yib481de92007-09-25 17:54:57 -07001123 if (is_legacy(tbl->lq_type)) {
Daniel C Halperine3949d62009-09-17 10:43:50 -07001124 tbl->expected_tpt = expected_tpt_legacy;
1125 return;
1126 }
1127
1128 /* Choose among many HT tables depending on number of streams
1129 * (SISO/MIMO2/MIMO3), channel width (20/40), SGI, and aggregation
1130 * status */
1131 if (is_siso(tbl->lq_type) && (!tbl->is_ht40 || lq_sta->is_dup))
1132 ht_tbl_pointer = expected_tpt_siso20MHz;
1133 else if (is_siso(tbl->lq_type))
1134 ht_tbl_pointer = expected_tpt_siso40MHz;
1135 else if (is_mimo2(tbl->lq_type) && (!tbl->is_ht40 || lq_sta->is_dup))
1136 ht_tbl_pointer = expected_tpt_mimo2_20MHz;
1137 else if (is_mimo2(tbl->lq_type))
1138 ht_tbl_pointer = expected_tpt_mimo2_40MHz;
1139 else if (is_mimo3(tbl->lq_type) && (!tbl->is_ht40 || lq_sta->is_dup))
1140 ht_tbl_pointer = expected_tpt_mimo3_20MHz;
1141 else /* if (is_mimo3(tbl->lq_type)) <-- must be true */
1142 ht_tbl_pointer = expected_tpt_mimo3_40MHz;
1143
1144 if (!tbl->is_SGI && !lq_sta->is_agg) /* Normal */
1145 tbl->expected_tpt = ht_tbl_pointer[0];
1146 else if (tbl->is_SGI && !lq_sta->is_agg) /* SGI */
1147 tbl->expected_tpt = ht_tbl_pointer[1];
1148 else if (!tbl->is_SGI && lq_sta->is_agg) /* AGG */
1149 tbl->expected_tpt = ht_tbl_pointer[2];
1150 else /* AGG+SGI */
1151 tbl->expected_tpt = ht_tbl_pointer[3];
Zhu Yib481de92007-09-25 17:54:57 -07001152}
1153
Ben Cahill77626352007-11-29 11:09:44 +08001154/*
1155 * Find starting rate for new "search" high-throughput mode of modulation.
1156 * Goal is to find lowest expected rate (under perfect conditions) that is
1157 * above the current measured throughput of "active" mode, to give new mode
1158 * a fair chance to prove itself without too many challenges.
1159 *
1160 * This gets called when transitioning to more aggressive modulation
1161 * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
1162 * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
1163 * to decrease to match "active" throughput. When moving from MIMO to SISO,
1164 * bit rate will typically need to increase, but not if performance was bad.
1165 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001166static s32 rs_get_best_rate(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001167 struct iwl_lq_sta *lq_sta,
1168 struct iwl_scale_tbl_info *tbl, /* "search" */
Guy Cohenf935a6d2008-04-23 17:15:02 -07001169 u16 rate_mask, s8 index)
Zhu Yib481de92007-09-25 17:54:57 -07001170{
Ben Cahill77626352007-11-29 11:09:44 +08001171 /* "active" values */
Tomas Winklere227cea2008-07-18 13:53:05 +08001172 struct iwl_scale_tbl_info *active_tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001173 &(lq_sta->lq_info[lq_sta->active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07001174 s32 active_sr = active_tbl->win[index].success_ratio;
Zhu Yib481de92007-09-25 17:54:57 -07001175 s32 active_tpt = active_tbl->expected_tpt[index];
Ben Cahill77626352007-11-29 11:09:44 +08001176 /* expected "search" throughput */
Johannes Berga34529e2014-01-20 22:57:21 +01001177 const u16 *tpt_tbl = tbl->expected_tpt;
Ben Cahill77626352007-11-29 11:09:44 +08001178
1179 s32 new_rate, high, low, start_hi;
Zhu Yib481de92007-09-25 17:54:57 -07001180 u16 high_low;
Guy Cohenf935a6d2008-04-23 17:15:02 -07001181 s8 rate = index;
Zhu Yib481de92007-09-25 17:54:57 -07001182
1183 new_rate = high = low = start_hi = IWL_RATE_INVALID;
1184
1185 for (; ;) {
Ester Kummerbf403db2008-05-05 10:22:40 +08001186 high_low = rs_get_adjacent_rate(priv, rate, rate_mask,
1187 tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -07001188
1189 low = high_low & 0xff;
1190 high = (high_low >> 8) & 0xff;
1191
Ben Cahill77626352007-11-29 11:09:44 +08001192 /*
1193 * Lower the "search" bit rate, to give new "search" mode
1194 * approximately the same throughput as "active" if:
1195 *
1196 * 1) "Active" mode has been working modestly well (but not
1197 * great), and expected "search" throughput (under perfect
1198 * conditions) at candidate rate is above the actual
1199 * measured "active" throughput (but less than expected
1200 * "active" throughput under perfect conditions).
1201 * OR
1202 * 2) "Active" mode has been working perfectly or very well
1203 * and expected "search" throughput (under perfect
1204 * conditions) at candidate rate is above expected
1205 * "active" throughput (under perfect conditions).
1206 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001207 if ((((100 * tpt_tbl[rate]) > lq_sta->last_tpt) &&
Zhu Yib481de92007-09-25 17:54:57 -07001208 ((active_sr > IWL_RATE_DECREASE_TH) &&
1209 (active_sr <= IWL_RATE_HIGH_TH) &&
1210 (tpt_tbl[rate] <= active_tpt))) ||
1211 ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
1212 (tpt_tbl[rate] > active_tpt))) {
1213
Ben Cahill77626352007-11-29 11:09:44 +08001214 /* (2nd or later pass)
1215 * If we've already tried to raise the rate, and are
1216 * now trying to lower it, use the higher rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001217 if (start_hi != IWL_RATE_INVALID) {
1218 new_rate = start_hi;
1219 break;
1220 }
Ben Cahill77626352007-11-29 11:09:44 +08001221
Zhu Yib481de92007-09-25 17:54:57 -07001222 new_rate = rate;
Ben Cahill77626352007-11-29 11:09:44 +08001223
1224 /* Loop again with lower rate */
Zhu Yib481de92007-09-25 17:54:57 -07001225 if (low != IWL_RATE_INVALID)
1226 rate = low;
Ben Cahill77626352007-11-29 11:09:44 +08001227
1228 /* Lower rate not available, use the original */
Zhu Yib481de92007-09-25 17:54:57 -07001229 else
1230 break;
Ben Cahill77626352007-11-29 11:09:44 +08001231
1232 /* Else try to raise the "search" rate to match "active" */
Zhu Yib481de92007-09-25 17:54:57 -07001233 } else {
Ben Cahill77626352007-11-29 11:09:44 +08001234 /* (2nd or later pass)
1235 * If we've already tried to lower the rate, and are
1236 * now trying to raise it, use the lower rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001237 if (new_rate != IWL_RATE_INVALID)
1238 break;
Ben Cahill77626352007-11-29 11:09:44 +08001239
1240 /* Loop again with higher rate */
Zhu Yib481de92007-09-25 17:54:57 -07001241 else if (high != IWL_RATE_INVALID) {
1242 start_hi = high;
1243 rate = high;
Ben Cahill77626352007-11-29 11:09:44 +08001244
1245 /* Higher rate not available, use the original */
Zhu Yib481de92007-09-25 17:54:57 -07001246 } else {
Guy Cohen90d77952008-09-09 10:54:53 +08001247 new_rate = rate;
Zhu Yib481de92007-09-25 17:54:57 -07001248 break;
1249 }
1250 }
1251 }
1252
1253 return new_rate;
1254}
Zhu Yib481de92007-09-25 17:54:57 -07001255
Ben Cahill77626352007-11-29 11:09:44 +08001256/*
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001257 * Set up search table for MIMO2
Ben Cahill77626352007-11-29 11:09:44 +08001258 */
Guy Cohenfde0db32008-04-21 15:42:01 -07001259static int rs_switch_to_mimo2(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001260 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001261 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001262 struct ieee80211_sta *sta,
Tomas Winklere227cea2008-07-18 13:53:05 +08001263 struct iwl_scale_tbl_info *tbl, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001264{
Zhu Yib481de92007-09-25 17:54:57 -07001265 u16 rate_mask;
1266 s32 rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001267 s8 is_green = lq_sta->is_green;
Johannes Berg7e6a5882010-08-23 10:46:46 +02001268 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
Johannes Bergc6892902011-09-20 15:37:21 -07001269 struct iwl_rxon_context *ctx = sta_priv->ctx;
Zhu Yib481de92007-09-25 17:54:57 -07001270
Luis R. Rodriguezde27e642008-12-23 15:58:44 -08001271 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
Zhu Yib481de92007-09-25 17:54:57 -07001272 return -1;
1273
Johannes Bergaf0ed692013-02-12 14:21:00 +01001274 if (sta->smps_mode == IEEE80211_SMPS_STATIC)
Zhu Yib481de92007-09-25 17:54:57 -07001275 return -1;
1276
Ben Cahill77626352007-11-29 11:09:44 +08001277 /* Need both Tx chains/antennas to support MIMO */
Johannes Berg9e295112012-04-09 17:46:55 -07001278 if (priv->hw_params.tx_chains_num < 2)
Zhu Yib481de92007-09-25 17:54:57 -07001279 return -1;
1280
Tomas Winklere1623442009-01-27 14:27:56 -08001281 IWL_DEBUG_RATE(priv, "LQ: try to switch to MIMO2\n");
Guy Cohen39e88502008-04-23 17:14:57 -07001282
1283 tbl->lq_type = LQ_MIMO2;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001284 tbl->is_dup = lq_sta->is_dup;
Zhu Yib481de92007-09-25 17:54:57 -07001285 tbl->action = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001286 tbl->max_search = IWL_MAX_SEARCH;
Guy Cohen39e88502008-04-23 17:14:57 -07001287 rate_mask = lq_sta->active_mimo2_rate;
1288
Johannes Berge1a0c6b2013-02-07 11:47:44 +01001289 if (iwl_is_ht40_tx_allowed(priv, ctx, sta))
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001290 tbl->is_ht40 = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001291 else
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001292 tbl->is_ht40 = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001293
Guy Coheneecd6e52008-04-23 17:14:58 -07001294 rs_set_expected_tpt_table(lq_sta, tbl);
Zhu Yib481de92007-09-25 17:54:57 -07001295
Guy Cohenf935a6d2008-04-23 17:15:02 -07001296 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
Zhu Yib481de92007-09-25 17:54:57 -07001297
Tomas Winklere1623442009-01-27 14:27:56 -08001298 IWL_DEBUG_RATE(priv, "LQ: MIMO2 best rate %d mask %X\n", rate, rate_mask);
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001299 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1300 IWL_DEBUG_RATE(priv, "Can't switch with index %d rate mask %x\n",
1301 rate, rate_mask);
1302 return -1;
1303 }
1304 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
Guy Cohen39e88502008-04-23 17:14:57 -07001305
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001306 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
1307 tbl->current_rate, is_green);
1308 return 0;
1309}
1310
1311/*
1312 * Set up search table for MIMO3
1313 */
1314static int rs_switch_to_mimo3(struct iwl_priv *priv,
1315 struct iwl_lq_sta *lq_sta,
1316 struct ieee80211_conf *conf,
1317 struct ieee80211_sta *sta,
1318 struct iwl_scale_tbl_info *tbl, int index)
1319{
1320 u16 rate_mask;
1321 s32 rate;
1322 s8 is_green = lq_sta->is_green;
Johannes Berg7e6a5882010-08-23 10:46:46 +02001323 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
Johannes Bergc6892902011-09-20 15:37:21 -07001324 struct iwl_rxon_context *ctx = sta_priv->ctx;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001325
1326 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
1327 return -1;
1328
Johannes Bergaf0ed692013-02-12 14:21:00 +01001329 if (sta->smps_mode == IEEE80211_SMPS_STATIC)
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001330 return -1;
1331
1332 /* Need both Tx chains/antennas to support MIMO */
Johannes Berg9e295112012-04-09 17:46:55 -07001333 if (priv->hw_params.tx_chains_num < 3)
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001334 return -1;
1335
1336 IWL_DEBUG_RATE(priv, "LQ: try to switch to MIMO3\n");
1337
1338 tbl->lq_type = LQ_MIMO3;
1339 tbl->is_dup = lq_sta->is_dup;
1340 tbl->action = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001341 tbl->max_search = IWL_MAX_11N_MIMO3_SEARCH;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001342 rate_mask = lq_sta->active_mimo3_rate;
1343
Johannes Berge1a0c6b2013-02-07 11:47:44 +01001344 if (iwl_is_ht40_tx_allowed(priv, ctx, sta))
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001345 tbl->is_ht40 = 1;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001346 else
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001347 tbl->is_ht40 = 0;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001348
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001349 rs_set_expected_tpt_table(lq_sta, tbl);
1350
1351 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
1352
1353 IWL_DEBUG_RATE(priv, "LQ: MIMO3 best rate %d mask %X\n",
1354 rate, rate_mask);
Guy Cohen39e88502008-04-23 17:14:57 -07001355 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
Tomas Winklere1623442009-01-27 14:27:56 -08001356 IWL_DEBUG_RATE(priv, "Can't switch with index %d rate mask %x\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001357 rate, rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07001358 return -1;
Guy Cohen39e88502008-04-23 17:14:57 -07001359 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001360 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
Zhu Yib481de92007-09-25 17:54:57 -07001361
Tomas Winklere1623442009-01-27 14:27:56 -08001362 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001363 tbl->current_rate, is_green);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001364 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07001365}
1366
Ben Cahill77626352007-11-29 11:09:44 +08001367/*
1368 * Set up search table for SISO
1369 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001370static int rs_switch_to_siso(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001371 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001372 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001373 struct ieee80211_sta *sta,
Tomas Winklere227cea2008-07-18 13:53:05 +08001374 struct iwl_scale_tbl_info *tbl, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001375{
Zhu Yib481de92007-09-25 17:54:57 -07001376 u16 rate_mask;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001377 u8 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07001378 s32 rate;
Johannes Berg7e6a5882010-08-23 10:46:46 +02001379 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
Johannes Bergc6892902011-09-20 15:37:21 -07001380 struct iwl_rxon_context *ctx = sta_priv->ctx;
Zhu Yib481de92007-09-25 17:54:57 -07001381
Luis R. Rodriguezde27e642008-12-23 15:58:44 -08001382 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
Zhu Yib481de92007-09-25 17:54:57 -07001383 return -1;
1384
Tomas Winklere1623442009-01-27 14:27:56 -08001385 IWL_DEBUG_RATE(priv, "LQ: try to switch to SISO\n");
Guy Cohen39e88502008-04-23 17:14:57 -07001386
Tomas Winklerc33104f2008-01-14 17:46:21 -08001387 tbl->is_dup = lq_sta->is_dup;
Zhu Yib481de92007-09-25 17:54:57 -07001388 tbl->lq_type = LQ_SISO;
1389 tbl->action = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001390 tbl->max_search = IWL_MAX_SEARCH;
Guy Cohen39e88502008-04-23 17:14:57 -07001391 rate_mask = lq_sta->active_siso_rate;
Zhu Yib481de92007-09-25 17:54:57 -07001392
Johannes Berge1a0c6b2013-02-07 11:47:44 +01001393 if (iwl_is_ht40_tx_allowed(priv, ctx, sta))
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001394 tbl->is_ht40 = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001395 else
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001396 tbl->is_ht40 = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001397
Zhu Yib481de92007-09-25 17:54:57 -07001398 if (is_green)
Guy Cohen39e88502008-04-23 17:14:57 -07001399 tbl->is_SGI = 0; /*11n spec: no SGI in SISO+Greenfield*/
Zhu Yib481de92007-09-25 17:54:57 -07001400
Guy Coheneecd6e52008-04-23 17:14:58 -07001401 rs_set_expected_tpt_table(lq_sta, tbl);
Guy Cohenf935a6d2008-04-23 17:15:02 -07001402 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
Zhu Yib481de92007-09-25 17:54:57 -07001403
Tomas Winklere1623442009-01-27 14:27:56 -08001404 IWL_DEBUG_RATE(priv, "LQ: get best rate %d mask %X\n", rate, rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07001405 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
Tomas Winklere1623442009-01-27 14:27:56 -08001406 IWL_DEBUG_RATE(priv, "can not switch with index %d rate mask %x\n",
Zhu Yib481de92007-09-25 17:54:57 -07001407 rate, rate_mask);
1408 return -1;
1409 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001410 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
Tomas Winklere1623442009-01-27 14:27:56 -08001411 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001412 tbl->current_rate, is_green);
Mohamed Abbas403ab562007-11-06 22:06:25 -08001413 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07001414}
1415
Ben Cahill77626352007-11-29 11:09:44 +08001416/*
1417 * Try to switch to new modulation mode from legacy
1418 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001419static int rs_move_legacy_other(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001420 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001421 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001422 struct ieee80211_sta *sta,
Zhu Yib481de92007-09-25 17:54:57 -07001423 int index)
1424{
Tomas Winklere227cea2008-07-18 13:53:05 +08001425 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1426 struct iwl_scale_tbl_info *search_tbl =
1427 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1428 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1429 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1430 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Wey-Yi Guyb1a78852010-07-31 08:52:03 -07001431 u8 start_action;
Eytan Lifshitzb7998c82012-12-01 20:59:49 +02001432 u8 valid_tx_ant = priv->nvm_data->valid_tx_ant;
Johannes Berg9e295112012-04-09 17:46:55 -07001433 u8 tx_chains_num = priv->hw_params.tx_chains_num;
Guy Cohenfde0db32008-04-21 15:42:01 -07001434 int ret = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001435 u8 update_search_tbl_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001436
Johannes Berg290f5992010-08-23 07:56:58 -07001437 switch (priv->bt_traffic_load) {
1438 case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
1439 /* nothing */
1440 break;
1441 case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
1442 /* avoid antenna B unless MIMO */
1443 if (tbl->action == IWL_LEGACY_SWITCH_ANTENNA2)
Venkataraman, Meenakshi383b0872011-11-10 06:55:25 -08001444 tbl->action = IWL_LEGACY_SWITCH_SISO;
Johannes Berg290f5992010-08-23 07:56:58 -07001445 break;
1446 case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
1447 case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
1448 /* avoid antenna B and MIMO */
Emmanuel Grumbachd6189122011-08-25 23:10:39 -07001449 valid_tx_ant =
Eytan Lifshitzb7998c82012-12-01 20:59:49 +02001450 first_antenna(priv->nvm_data->valid_tx_ant);
Johannes Berg290f5992010-08-23 07:56:58 -07001451 if (tbl->action >= IWL_LEGACY_SWITCH_ANTENNA2 &&
1452 tbl->action != IWL_LEGACY_SWITCH_SISO)
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001453 tbl->action = IWL_LEGACY_SWITCH_SISO;
Johannes Berg290f5992010-08-23 07:56:58 -07001454 break;
1455 default:
Johannes Berg3c6acb62014-05-07 11:47:53 +02001456 IWL_ERR(priv, "Invalid BT load %d\n", priv->bt_traffic_load);
Johannes Berg290f5992010-08-23 07:56:58 -07001457 break;
1458 }
1459
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001460 if (!iwl_ht_enabled(priv))
1461 /* stay in Legacy */
1462 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
Johannes Berg3ad3b922009-08-07 15:41:48 -07001463 else if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001464 tbl->action > IWL_LEGACY_SWITCH_SISO)
1465 tbl->action = IWL_LEGACY_SWITCH_SISO;
Wey-Yi Guybee008b2010-08-23 07:57:04 -07001466
1467 /* configure as 1x1 if bt full concurrency */
1468 if (priv->bt_full_concurrent) {
1469 if (!iwl_ht_enabled(priv))
1470 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
1471 else if (tbl->action >= IWL_LEGACY_SWITCH_ANTENNA2)
1472 tbl->action = IWL_LEGACY_SWITCH_SISO;
Emmanuel Grumbachd6189122011-08-25 23:10:39 -07001473 valid_tx_ant =
Eytan Lifshitzb7998c82012-12-01 20:59:49 +02001474 first_antenna(priv->nvm_data->valid_tx_ant);
Wey-Yi Guybee008b2010-08-23 07:57:04 -07001475 }
1476
Wey-Yi Guyb1a78852010-07-31 08:52:03 -07001477 start_action = tbl->action;
Zhu Yib481de92007-09-25 17:54:57 -07001478 for (; ;) {
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001479 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001480 switch (tbl->action) {
Guy Cohen3110bef2008-09-09 10:54:54 +08001481 case IWL_LEGACY_SWITCH_ANTENNA1:
1482 case IWL_LEGACY_SWITCH_ANTENNA2:
Tomas Winklere1623442009-01-27 14:27:56 -08001483 IWL_DEBUG_RATE(priv, "LQ: Legacy toggle Antenna\n");
Zhu Yib481de92007-09-25 17:54:57 -07001484
Guy Cohen3110bef2008-09-09 10:54:54 +08001485 if ((tbl->action == IWL_LEGACY_SWITCH_ANTENNA1 &&
1486 tx_chains_num <= 1) ||
1487 (tbl->action == IWL_LEGACY_SWITCH_ANTENNA2 &&
1488 tx_chains_num <= 2))
1489 break;
1490
Ben Cahill77626352007-11-29 11:09:44 +08001491 /* Don't change antenna if success has been great */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001492 if (window->success_ratio >= IWL_RS_GOOD_RATIO &&
1493 !priv->bt_full_concurrent &&
1494 priv->bt_traffic_load ==
1495 IWL_BT_COEX_TRAFFIC_LOAD_NONE)
Zhu Yib481de92007-09-25 17:54:57 -07001496 break;
Ben Cahill77626352007-11-29 11:09:44 +08001497
Ben Cahill77626352007-11-29 11:09:44 +08001498 /* Set up search table to try other antenna */
Zhu Yib481de92007-09-25 17:54:57 -07001499 memcpy(search_tbl, tbl, sz);
1500
Guy Cohenaade00c2008-04-23 17:14:59 -07001501 if (rs_toggle_antenna(valid_tx_ant,
1502 &search_tbl->current_rate, search_tbl)) {
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001503 update_search_tbl_counter = 1;
Guy Cohen3110bef2008-09-09 10:54:54 +08001504 rs_set_expected_tpt_table(lq_sta, search_tbl);
Guy Cohenaade00c2008-04-23 17:14:59 -07001505 goto out;
1506 }
Guy Cohena5e8b502008-05-29 16:35:11 +08001507 break;
Zhu Yib481de92007-09-25 17:54:57 -07001508 case IWL_LEGACY_SWITCH_SISO:
Tomas Winklere1623442009-01-27 14:27:56 -08001509 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to SISO\n");
Ben Cahill77626352007-11-29 11:09:44 +08001510
1511 /* Set up search table to try SISO */
Zhu Yib481de92007-09-25 17:54:57 -07001512 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001513 search_tbl->is_SGI = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001514 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001515 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001516 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001517 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001518 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001519 }
Zhu Yib481de92007-09-25 17:54:57 -07001520
1521 break;
Guy Cohen3110bef2008-09-09 10:54:54 +08001522 case IWL_LEGACY_SWITCH_MIMO2_AB:
1523 case IWL_LEGACY_SWITCH_MIMO2_AC:
1524 case IWL_LEGACY_SWITCH_MIMO2_BC:
Tomas Winklere1623442009-01-27 14:27:56 -08001525 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to MIMO2\n");
Ben Cahill77626352007-11-29 11:09:44 +08001526
1527 /* Set up search table to try MIMO */
Zhu Yib481de92007-09-25 17:54:57 -07001528 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001529 search_tbl->is_SGI = 0;
Guy Cohen3110bef2008-09-09 10:54:54 +08001530
1531 if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AB)
1532 search_tbl->ant_type = ANT_AB;
1533 else if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AC)
1534 search_tbl->ant_type = ANT_AC;
1535 else
1536 search_tbl->ant_type = ANT_BC;
1537
1538 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1539 break;
1540
Guy Cohenfde0db32008-04-21 15:42:01 -07001541 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001542 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001543 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001544 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001545 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001546 }
Zhu Yib481de92007-09-25 17:54:57 -07001547 break;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001548
1549 case IWL_LEGACY_SWITCH_MIMO3_ABC:
1550 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to MIMO3\n");
1551
1552 /* Set up search table to try MIMO3 */
1553 memcpy(search_tbl, tbl, sz);
1554 search_tbl->is_SGI = 0;
1555
1556 search_tbl->ant_type = ANT_ABC;
1557
1558 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1559 break;
1560
1561 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1562 search_tbl, index);
1563 if (!ret) {
1564 lq_sta->action_counter = 0;
1565 goto out;
1566 }
1567 break;
Zhu Yib481de92007-09-25 17:54:57 -07001568 }
1569 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001570 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001571 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
Zhu Yib481de92007-09-25 17:54:57 -07001572
1573 if (tbl->action == start_action)
1574 break;
1575
1576 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001577 search_tbl->lq_type = LQ_NONE;
Zhu Yib481de92007-09-25 17:54:57 -07001578 return 0;
1579
Guy Cohen3110bef2008-09-09 10:54:54 +08001580out:
1581 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001582 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001583 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001584 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001585 if (update_search_tbl_counter)
1586 search_tbl->action = tbl->action;
Zhu Yib481de92007-09-25 17:54:57 -07001587 return 0;
1588
1589}
1590
Ben Cahill77626352007-11-29 11:09:44 +08001591/*
1592 * Try to switch to new modulation mode from SISO
1593 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001594static int rs_move_siso_to_other(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001595 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001596 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001597 struct ieee80211_sta *sta, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001598{
Tomas Winklerc33104f2008-01-14 17:46:21 -08001599 u8 is_green = lq_sta->is_green;
Tomas Winklere227cea2008-07-18 13:53:05 +08001600 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1601 struct iwl_scale_tbl_info *search_tbl =
1602 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1603 struct iwl_rate_scale_data *window = &(tbl->win[index]);
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001604 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Tomas Winklere227cea2008-07-18 13:53:05 +08001605 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1606 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Wey-Yi Guyb1a78852010-07-31 08:52:03 -07001607 u8 start_action;
Eytan Lifshitzb7998c82012-12-01 20:59:49 +02001608 u8 valid_tx_ant = priv->nvm_data->valid_tx_ant;
Johannes Berg9e295112012-04-09 17:46:55 -07001609 u8 tx_chains_num = priv->hw_params.tx_chains_num;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001610 u8 update_search_tbl_counter = 0;
Guy Cohenfde0db32008-04-21 15:42:01 -07001611 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001612
Johannes Berg290f5992010-08-23 07:56:58 -07001613 switch (priv->bt_traffic_load) {
1614 case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
1615 /* nothing */
1616 break;
1617 case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
1618 /* avoid antenna B unless MIMO */
1619 if (tbl->action == IWL_SISO_SWITCH_ANTENNA2)
Venkataraman, Meenakshi383b0872011-11-10 06:55:25 -08001620 tbl->action = IWL_SISO_SWITCH_MIMO2_AB;
Johannes Berg290f5992010-08-23 07:56:58 -07001621 break;
1622 case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
1623 case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
1624 /* avoid antenna B and MIMO */
Emmanuel Grumbachd6189122011-08-25 23:10:39 -07001625 valid_tx_ant =
Eytan Lifshitzb7998c82012-12-01 20:59:49 +02001626 first_antenna(priv->nvm_data->valid_tx_ant);
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001627 if (tbl->action != IWL_SISO_SWITCH_ANTENNA1)
Johannes Berg290f5992010-08-23 07:56:58 -07001628 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1629 break;
1630 default:
Johannes Berg3c6acb62014-05-07 11:47:53 +02001631 IWL_ERR(priv, "Invalid BT load %d\n", priv->bt_traffic_load);
Johannes Berg290f5992010-08-23 07:56:58 -07001632 break;
1633 }
1634
Johannes Berg3ad3b922009-08-07 15:41:48 -07001635 if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001636 tbl->action > IWL_SISO_SWITCH_ANTENNA2) {
1637 /* stay in SISO */
1638 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1639 }
Wey-Yi Guybee008b2010-08-23 07:57:04 -07001640
1641 /* configure as 1x1 if bt full concurrency */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001642 if (priv->bt_full_concurrent) {
Emmanuel Grumbachd6189122011-08-25 23:10:39 -07001643 valid_tx_ant =
Eytan Lifshitzb7998c82012-12-01 20:59:49 +02001644 first_antenna(priv->nvm_data->valid_tx_ant);
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001645 if (tbl->action >= IWL_LEGACY_SWITCH_ANTENNA2)
1646 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1647 }
Wey-Yi Guybee008b2010-08-23 07:57:04 -07001648
Wey-Yi Guyb1a78852010-07-31 08:52:03 -07001649 start_action = tbl->action;
Zhu Yib481de92007-09-25 17:54:57 -07001650 for (;;) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001651 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001652 switch (tbl->action) {
Guy Cohen3110bef2008-09-09 10:54:54 +08001653 case IWL_SISO_SWITCH_ANTENNA1:
1654 case IWL_SISO_SWITCH_ANTENNA2:
Tomas Winklere1623442009-01-27 14:27:56 -08001655 IWL_DEBUG_RATE(priv, "LQ: SISO toggle Antenna\n");
Guy Cohen3110bef2008-09-09 10:54:54 +08001656 if ((tbl->action == IWL_SISO_SWITCH_ANTENNA1 &&
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001657 tx_chains_num <= 1) ||
Guy Cohen3110bef2008-09-09 10:54:54 +08001658 (tbl->action == IWL_SISO_SWITCH_ANTENNA2 &&
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001659 tx_chains_num <= 2))
Guy Cohen3110bef2008-09-09 10:54:54 +08001660 break;
1661
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001662 if (window->success_ratio >= IWL_RS_GOOD_RATIO &&
1663 !priv->bt_full_concurrent &&
1664 priv->bt_traffic_load ==
1665 IWL_BT_COEX_TRAFFIC_LOAD_NONE)
Zhu Yib481de92007-09-25 17:54:57 -07001666 break;
Zhu Yib481de92007-09-25 17:54:57 -07001667
1668 memcpy(search_tbl, tbl, sz);
Guy Cohenaade00c2008-04-23 17:14:59 -07001669 if (rs_toggle_antenna(valid_tx_ant,
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001670 &search_tbl->current_rate, search_tbl)) {
1671 update_search_tbl_counter = 1;
Guy Cohenaade00c2008-04-23 17:14:59 -07001672 goto out;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001673 }
Guy Cohena5e8b502008-05-29 16:35:11 +08001674 break;
Guy Cohen3110bef2008-09-09 10:54:54 +08001675 case IWL_SISO_SWITCH_MIMO2_AB:
1676 case IWL_SISO_SWITCH_MIMO2_AC:
1677 case IWL_SISO_SWITCH_MIMO2_BC:
Tomas Winklere1623442009-01-27 14:27:56 -08001678 IWL_DEBUG_RATE(priv, "LQ: SISO switch to MIMO2\n");
Zhu Yib481de92007-09-25 17:54:57 -07001679 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001680 search_tbl->is_SGI = 0;
Guy Cohen3110bef2008-09-09 10:54:54 +08001681
1682 if (tbl->action == IWL_SISO_SWITCH_MIMO2_AB)
1683 search_tbl->ant_type = ANT_AB;
1684 else if (tbl->action == IWL_SISO_SWITCH_MIMO2_AC)
1685 search_tbl->ant_type = ANT_AC;
1686 else
1687 search_tbl->ant_type = ANT_BC;
1688
1689 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1690 break;
1691
Guy Cohenfde0db32008-04-21 15:42:01 -07001692 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001693 search_tbl, index);
Guy Cohen3110bef2008-09-09 10:54:54 +08001694 if (!ret)
Zhu Yib481de92007-09-25 17:54:57 -07001695 goto out;
1696 break;
1697 case IWL_SISO_SWITCH_GI:
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001698 if (!tbl->is_ht40 && !(ht_cap->cap &
1699 IEEE80211_HT_CAP_SGI_20))
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001700 break;
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001701 if (tbl->is_ht40 && !(ht_cap->cap &
1702 IEEE80211_HT_CAP_SGI_40))
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001703 break;
1704
Tomas Winklere1623442009-01-27 14:27:56 -08001705 IWL_DEBUG_RATE(priv, "LQ: SISO toggle SGI/NGI\n");
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001706
Zhu Yib481de92007-09-25 17:54:57 -07001707 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001708 if (is_green) {
1709 if (!tbl->is_SGI)
1710 break;
1711 else
Winkler, Tomas15b16872008-12-19 10:37:33 +08001712 IWL_ERR(priv,
1713 "SGI was set in GF+SISO\n");
Zhu Yib481de92007-09-25 17:54:57 -07001714 }
Guy Cohen39e88502008-04-23 17:14:57 -07001715 search_tbl->is_SGI = !tbl->is_SGI;
Guy Coheneecd6e52008-04-23 17:14:58 -07001716 rs_set_expected_tpt_table(lq_sta, search_tbl);
Guy Cohen39e88502008-04-23 17:14:57 -07001717 if (tbl->is_SGI) {
1718 s32 tpt = lq_sta->last_tpt / 100;
1719 if (tpt >= search_tbl->expected_tpt[index])
1720 break;
1721 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001722 search_tbl->current_rate =
1723 rate_n_flags_from_tbl(priv, search_tbl,
1724 index, is_green);
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001725 update_search_tbl_counter = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001726 goto out;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001727 case IWL_SISO_SWITCH_MIMO3_ABC:
1728 IWL_DEBUG_RATE(priv, "LQ: SISO switch to MIMO3\n");
1729 memcpy(search_tbl, tbl, sz);
1730 search_tbl->is_SGI = 0;
1731 search_tbl->ant_type = ANT_ABC;
1732
1733 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1734 break;
1735
1736 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1737 search_tbl, index);
1738 if (!ret)
1739 goto out;
1740 break;
Zhu Yib481de92007-09-25 17:54:57 -07001741 }
1742 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001743 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001744 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
Zhu Yib481de92007-09-25 17:54:57 -07001745
1746 if (tbl->action == start_action)
1747 break;
1748 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001749 search_tbl->lq_type = LQ_NONE;
Zhu Yib481de92007-09-25 17:54:57 -07001750 return 0;
1751
1752 out:
Guy Cohen3110bef2008-09-09 10:54:54 +08001753 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001754 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001755 if (tbl->action > IWL_SISO_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001756 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001757 if (update_search_tbl_counter)
1758 search_tbl->action = tbl->action;
1759
Zhu Yib481de92007-09-25 17:54:57 -07001760 return 0;
1761}
1762
Ben Cahill77626352007-11-29 11:09:44 +08001763/*
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001764 * Try to switch to new modulation mode from MIMO2
Ben Cahill77626352007-11-29 11:09:44 +08001765 */
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001766static int rs_move_mimo2_to_other(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001767 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001768 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001769 struct ieee80211_sta *sta, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001770{
Tomas Winklerc33104f2008-01-14 17:46:21 -08001771 s8 is_green = lq_sta->is_green;
Tomas Winklere227cea2008-07-18 13:53:05 +08001772 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1773 struct iwl_scale_tbl_info *search_tbl =
1774 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Guy Cohen3110bef2008-09-09 10:54:54 +08001775 struct iwl_rate_scale_data *window = &(tbl->win[index]);
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001776 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Tomas Winklere227cea2008-07-18 13:53:05 +08001777 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1778 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Wey-Yi Guyb1a78852010-07-31 08:52:03 -07001779 u8 start_action;
Eytan Lifshitzb7998c82012-12-01 20:59:49 +02001780 u8 valid_tx_ant = priv->nvm_data->valid_tx_ant;
Johannes Berg9e295112012-04-09 17:46:55 -07001781 u8 tx_chains_num = priv->hw_params.tx_chains_num;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001782 u8 update_search_tbl_counter = 0;
Guy Cohenaade00c2008-04-23 17:14:59 -07001783 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001784
Johannes Berg290f5992010-08-23 07:56:58 -07001785 switch (priv->bt_traffic_load) {
1786 case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
1787 /* nothing */
1788 break;
1789 case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
1790 case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
1791 /* avoid antenna B and MIMO */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001792 if (tbl->action != IWL_MIMO2_SWITCH_SISO_A)
1793 tbl->action = IWL_MIMO2_SWITCH_SISO_A;
1794 break;
Johannes Berg290f5992010-08-23 07:56:58 -07001795 case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
1796 /* avoid antenna B unless MIMO */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001797 if (tbl->action == IWL_MIMO2_SWITCH_SISO_B ||
1798 tbl->action == IWL_MIMO2_SWITCH_SISO_C)
Johannes Berg290f5992010-08-23 07:56:58 -07001799 tbl->action = IWL_MIMO2_SWITCH_SISO_A;
1800 break;
1801 default:
Johannes Berg3c6acb62014-05-07 11:47:53 +02001802 IWL_ERR(priv, "Invalid BT load %d\n", priv->bt_traffic_load);
Johannes Berg290f5992010-08-23 07:56:58 -07001803 break;
1804 }
1805
Johannes Berg3ad3b922009-08-07 15:41:48 -07001806 if ((iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE) &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001807 (tbl->action < IWL_MIMO2_SWITCH_SISO_A ||
1808 tbl->action > IWL_MIMO2_SWITCH_SISO_C)) {
1809 /* switch in SISO */
1810 tbl->action = IWL_MIMO2_SWITCH_SISO_A;
1811 }
Wey-Yi Guybee008b2010-08-23 07:57:04 -07001812
1813 /* configure as 1x1 if bt full concurrency */
1814 if (priv->bt_full_concurrent &&
1815 (tbl->action < IWL_MIMO2_SWITCH_SISO_A ||
1816 tbl->action > IWL_MIMO2_SWITCH_SISO_C))
1817 tbl->action = IWL_MIMO2_SWITCH_SISO_A;
1818
Wey-Yi Guyb1a78852010-07-31 08:52:03 -07001819 start_action = tbl->action;
Zhu Yib481de92007-09-25 17:54:57 -07001820 for (;;) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001821 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001822 switch (tbl->action) {
Guy Cohen3110bef2008-09-09 10:54:54 +08001823 case IWL_MIMO2_SWITCH_ANTENNA1:
1824 case IWL_MIMO2_SWITCH_ANTENNA2:
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001825 IWL_DEBUG_RATE(priv, "LQ: MIMO2 toggle Antennas\n");
Guy Cohen3110bef2008-09-09 10:54:54 +08001826
1827 if (tx_chains_num <= 2)
1828 break;
1829
1830 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1831 break;
1832
1833 memcpy(search_tbl, tbl, sz);
1834 if (rs_toggle_antenna(valid_tx_ant,
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001835 &search_tbl->current_rate, search_tbl)) {
1836 update_search_tbl_counter = 1;
Guy Cohen3110bef2008-09-09 10:54:54 +08001837 goto out;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001838 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001839 break;
1840 case IWL_MIMO2_SWITCH_SISO_A:
1841 case IWL_MIMO2_SWITCH_SISO_B:
1842 case IWL_MIMO2_SWITCH_SISO_C:
Tomas Winklere1623442009-01-27 14:27:56 -08001843 IWL_DEBUG_RATE(priv, "LQ: MIMO2 switch to SISO\n");
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001844
Ben Cahill77626352007-11-29 11:09:44 +08001845 /* Set up new search table for SISO */
Zhu Yib481de92007-09-25 17:54:57 -07001846 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001847
Guy Cohen3110bef2008-09-09 10:54:54 +08001848 if (tbl->action == IWL_MIMO2_SWITCH_SISO_A)
Guy Cohenfde0db32008-04-21 15:42:01 -07001849 search_tbl->ant_type = ANT_A;
Guy Cohen3110bef2008-09-09 10:54:54 +08001850 else if (tbl->action == IWL_MIMO2_SWITCH_SISO_B)
Guy Cohenfde0db32008-04-21 15:42:01 -07001851 search_tbl->ant_type = ANT_B;
Guy Cohen3110bef2008-09-09 10:54:54 +08001852 else
1853 search_tbl->ant_type = ANT_C;
1854
1855 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1856 break;
Zhu Yib481de92007-09-25 17:54:57 -07001857
Tomas Winklerc33104f2008-01-14 17:46:21 -08001858 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001859 search_tbl, index);
Guy Cohen3110bef2008-09-09 10:54:54 +08001860 if (!ret)
Zhu Yib481de92007-09-25 17:54:57 -07001861 goto out;
Guy Cohen3110bef2008-09-09 10:54:54 +08001862
Zhu Yib481de92007-09-25 17:54:57 -07001863 break;
1864
Guy Cohen3110bef2008-09-09 10:54:54 +08001865 case IWL_MIMO2_SWITCH_GI:
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001866 if (!tbl->is_ht40 && !(ht_cap->cap &
1867 IEEE80211_HT_CAP_SGI_20))
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001868 break;
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001869 if (tbl->is_ht40 && !(ht_cap->cap &
1870 IEEE80211_HT_CAP_SGI_40))
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001871 break;
1872
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001873 IWL_DEBUG_RATE(priv, "LQ: MIMO2 toggle SGI/NGI\n");
1874
1875 /* Set up new search table for MIMO2 */
1876 memcpy(search_tbl, tbl, sz);
1877 search_tbl->is_SGI = !tbl->is_SGI;
1878 rs_set_expected_tpt_table(lq_sta, search_tbl);
1879 /*
1880 * If active table already uses the fastest possible
1881 * modulation (dual stream with short guard interval),
1882 * and it's working well, there's no need to look
1883 * for a better type of modulation!
1884 */
1885 if (tbl->is_SGI) {
1886 s32 tpt = lq_sta->last_tpt / 100;
1887 if (tpt >= search_tbl->expected_tpt[index])
1888 break;
1889 }
1890 search_tbl->current_rate =
1891 rate_n_flags_from_tbl(priv, search_tbl,
1892 index, is_green);
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001893 update_search_tbl_counter = 1;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001894 goto out;
1895
1896 case IWL_MIMO2_SWITCH_MIMO3_ABC:
1897 IWL_DEBUG_RATE(priv, "LQ: MIMO2 switch to MIMO3\n");
1898 memcpy(search_tbl, tbl, sz);
1899 search_tbl->is_SGI = 0;
1900 search_tbl->ant_type = ANT_ABC;
1901
1902 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1903 break;
1904
1905 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1906 search_tbl, index);
1907 if (!ret)
1908 goto out;
1909
1910 break;
1911 }
1912 tbl->action++;
1913 if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
1914 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
1915
1916 if (tbl->action == start_action)
1917 break;
1918 }
1919 search_tbl->lq_type = LQ_NONE;
1920 return 0;
1921 out:
1922 lq_sta->search_better_tbl = 1;
1923 tbl->action++;
1924 if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
1925 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001926 if (update_search_tbl_counter)
1927 search_tbl->action = tbl->action;
1928
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001929 return 0;
1930
1931}
1932
1933/*
1934 * Try to switch to new modulation mode from MIMO3
1935 */
1936static int rs_move_mimo3_to_other(struct iwl_priv *priv,
1937 struct iwl_lq_sta *lq_sta,
1938 struct ieee80211_conf *conf,
1939 struct ieee80211_sta *sta, int index)
1940{
1941 s8 is_green = lq_sta->is_green;
1942 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1943 struct iwl_scale_tbl_info *search_tbl =
1944 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1945 struct iwl_rate_scale_data *window = &(tbl->win[index]);
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001946 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001947 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1948 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Wey-Yi Guyb1a78852010-07-31 08:52:03 -07001949 u8 start_action;
Eytan Lifshitzb7998c82012-12-01 20:59:49 +02001950 u8 valid_tx_ant = priv->nvm_data->valid_tx_ant;
Johannes Berg9e295112012-04-09 17:46:55 -07001951 u8 tx_chains_num = priv->hw_params.tx_chains_num;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001952 int ret;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001953 u8 update_search_tbl_counter = 0;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001954
Johannes Berg290f5992010-08-23 07:56:58 -07001955 switch (priv->bt_traffic_load) {
1956 case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
1957 /* nothing */
1958 break;
1959 case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
1960 case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
1961 /* avoid antenna B and MIMO */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001962 if (tbl->action != IWL_MIMO3_SWITCH_SISO_A)
Johannes Berg290f5992010-08-23 07:56:58 -07001963 tbl->action = IWL_MIMO3_SWITCH_SISO_A;
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001964 break;
Johannes Berg290f5992010-08-23 07:56:58 -07001965 case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
1966 /* avoid antenna B unless MIMO */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001967 if (tbl->action == IWL_MIMO3_SWITCH_SISO_B ||
1968 tbl->action == IWL_MIMO3_SWITCH_SISO_C)
Johannes Berg290f5992010-08-23 07:56:58 -07001969 tbl->action = IWL_MIMO3_SWITCH_SISO_A;
Johannes Berg290f5992010-08-23 07:56:58 -07001970 break;
1971 default:
Johannes Berg3c6acb62014-05-07 11:47:53 +02001972 IWL_ERR(priv, "Invalid BT load %d\n", priv->bt_traffic_load);
Johannes Berg290f5992010-08-23 07:56:58 -07001973 break;
1974 }
1975
Johannes Berg3ad3b922009-08-07 15:41:48 -07001976 if ((iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE) &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001977 (tbl->action < IWL_MIMO3_SWITCH_SISO_A ||
1978 tbl->action > IWL_MIMO3_SWITCH_SISO_C)) {
1979 /* switch in SISO */
1980 tbl->action = IWL_MIMO3_SWITCH_SISO_A;
1981 }
Wey-Yi Guybee008b2010-08-23 07:57:04 -07001982
1983 /* configure as 1x1 if bt full concurrency */
1984 if (priv->bt_full_concurrent &&
1985 (tbl->action < IWL_MIMO3_SWITCH_SISO_A ||
1986 tbl->action > IWL_MIMO3_SWITCH_SISO_C))
1987 tbl->action = IWL_MIMO3_SWITCH_SISO_A;
1988
Wey-Yi Guyb1a78852010-07-31 08:52:03 -07001989 start_action = tbl->action;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001990 for (;;) {
1991 lq_sta->action_counter++;
1992 switch (tbl->action) {
1993 case IWL_MIMO3_SWITCH_ANTENNA1:
1994 case IWL_MIMO3_SWITCH_ANTENNA2:
1995 IWL_DEBUG_RATE(priv, "LQ: MIMO3 toggle Antennas\n");
1996
1997 if (tx_chains_num <= 3)
1998 break;
1999
2000 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
2001 break;
2002
2003 memcpy(search_tbl, tbl, sz);
2004 if (rs_toggle_antenna(valid_tx_ant,
2005 &search_tbl->current_rate, search_tbl))
2006 goto out;
2007 break;
2008 case IWL_MIMO3_SWITCH_SISO_A:
2009 case IWL_MIMO3_SWITCH_SISO_B:
2010 case IWL_MIMO3_SWITCH_SISO_C:
2011 IWL_DEBUG_RATE(priv, "LQ: MIMO3 switch to SISO\n");
2012
2013 /* Set up new search table for SISO */
2014 memcpy(search_tbl, tbl, sz);
2015
2016 if (tbl->action == IWL_MIMO3_SWITCH_SISO_A)
2017 search_tbl->ant_type = ANT_A;
2018 else if (tbl->action == IWL_MIMO3_SWITCH_SISO_B)
2019 search_tbl->ant_type = ANT_B;
2020 else
2021 search_tbl->ant_type = ANT_C;
2022
2023 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
2024 break;
2025
2026 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
2027 search_tbl, index);
2028 if (!ret)
2029 goto out;
2030
2031 break;
2032
2033 case IWL_MIMO3_SWITCH_MIMO2_AB:
2034 case IWL_MIMO3_SWITCH_MIMO2_AC:
2035 case IWL_MIMO3_SWITCH_MIMO2_BC:
2036 IWL_DEBUG_RATE(priv, "LQ: MIMO3 switch to MIMO2\n");
2037
2038 memcpy(search_tbl, tbl, sz);
2039 search_tbl->is_SGI = 0;
2040 if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AB)
2041 search_tbl->ant_type = ANT_AB;
2042 else if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AC)
2043 search_tbl->ant_type = ANT_AC;
2044 else
2045 search_tbl->ant_type = ANT_BC;
2046
2047 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
2048 break;
2049
2050 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
2051 search_tbl, index);
2052 if (!ret)
2053 goto out;
2054
2055 break;
2056
2057 case IWL_MIMO3_SWITCH_GI:
Daniel C Halperin28e6f482009-08-13 13:30:58 -07002058 if (!tbl->is_ht40 && !(ht_cap->cap &
2059 IEEE80211_HT_CAP_SGI_20))
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07002060 break;
Daniel C Halperin28e6f482009-08-13 13:30:58 -07002061 if (tbl->is_ht40 && !(ht_cap->cap &
2062 IEEE80211_HT_CAP_SGI_40))
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07002063 break;
2064
2065 IWL_DEBUG_RATE(priv, "LQ: MIMO3 toggle SGI/NGI\n");
Ben Cahill77626352007-11-29 11:09:44 +08002066
2067 /* Set up new search table for MIMO */
Zhu Yib481de92007-09-25 17:54:57 -07002068 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07002069 search_tbl->is_SGI = !tbl->is_SGI;
Guy Coheneecd6e52008-04-23 17:14:58 -07002070 rs_set_expected_tpt_table(lq_sta, search_tbl);
Ben Cahill77626352007-11-29 11:09:44 +08002071 /*
2072 * If active table already uses the fastest possible
2073 * modulation (dual stream with short guard interval),
2074 * and it's working well, there's no need to look
2075 * for a better type of modulation!
2076 */
Guy Cohen39e88502008-04-23 17:14:57 -07002077 if (tbl->is_SGI) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002078 s32 tpt = lq_sta->last_tpt / 100;
Guy Cohen39e88502008-04-23 17:14:57 -07002079 if (tpt >= search_tbl->expected_tpt[index])
2080 break;
Zhu Yib481de92007-09-25 17:54:57 -07002081 }
Tomas Winkler978785a2008-12-19 10:37:31 +08002082 search_tbl->current_rate =
2083 rate_n_flags_from_tbl(priv, search_tbl,
2084 index, is_green);
Mohamed Abbasd6e93392009-05-08 13:44:39 -07002085 update_search_tbl_counter = 1;
Zhu Yib481de92007-09-25 17:54:57 -07002086 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07002087 }
2088 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07002089 if (tbl->action > IWL_MIMO3_SWITCH_GI)
2090 tbl->action = IWL_MIMO3_SWITCH_ANTENNA1;
Zhu Yib481de92007-09-25 17:54:57 -07002091
2092 if (tbl->action == start_action)
2093 break;
2094 }
Guy Cohen3110bef2008-09-09 10:54:54 +08002095 search_tbl->lq_type = LQ_NONE;
Zhu Yib481de92007-09-25 17:54:57 -07002096 return 0;
2097 out:
Guy Cohen3110bef2008-09-09 10:54:54 +08002098 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07002099 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07002100 if (tbl->action > IWL_MIMO3_SWITCH_GI)
2101 tbl->action = IWL_MIMO3_SWITCH_ANTENNA1;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07002102 if (update_search_tbl_counter)
2103 search_tbl->action = tbl->action;
2104
Zhu Yib481de92007-09-25 17:54:57 -07002105 return 0;
2106
2107}
2108
Ben Cahill77626352007-11-29 11:09:44 +08002109/*
2110 * Check whether we should continue using same modulation mode, or
2111 * begin search for a new mode, based on:
2112 * 1) # tx successes or failures while using this mode
2113 * 2) # times calling this function
2114 * 3) elapsed time in this mode (not used, for now)
2115 */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07002116static void rs_stay_in_table(struct iwl_lq_sta *lq_sta, bool force_search)
Zhu Yib481de92007-09-25 17:54:57 -07002117{
Tomas Winklere227cea2008-07-18 13:53:05 +08002118 struct iwl_scale_tbl_info *tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002119 int i;
2120 int active_tbl;
2121 int flush_interval_passed = 0;
Ester Kummerbf403db2008-05-05 10:22:40 +08002122 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07002123
Ester Kummerbf403db2008-05-05 10:22:40 +08002124 priv = lq_sta->drv;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002125 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002126
Tomas Winklerc33104f2008-01-14 17:46:21 -08002127 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07002128
Ben Cahill77626352007-11-29 11:09:44 +08002129 /* If we've been disallowing search, see if we should now allow it */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002130 if (lq_sta->stay_in_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07002131
Ben Cahill77626352007-11-29 11:09:44 +08002132 /* Elapsed time using current modulation mode */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002133 if (lq_sta->flush_timer)
Zhu Yib481de92007-09-25 17:54:57 -07002134 flush_interval_passed =
Mohamed Abbas447fee72009-04-20 14:37:02 -07002135 time_after(jiffies,
2136 (unsigned long)(lq_sta->flush_timer +
Zhu Yib481de92007-09-25 17:54:57 -07002137 IWL_RATE_SCALE_FLUSH_INTVL));
2138
Ben Cahill77626352007-11-29 11:09:44 +08002139 /*
2140 * Check if we should allow search for new modulation mode.
2141 * If many frames have failed or succeeded, or we've used
2142 * this same modulation for a long time, allow search, and
2143 * reset history stats that keep track of whether we should
2144 * allow a new search. Also (below) reset all bitmaps and
2145 * stats in active history.
2146 */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07002147 if (force_search ||
2148 (lq_sta->total_failed > lq_sta->max_failure_limit) ||
Tomas Winklerc33104f2008-01-14 17:46:21 -08002149 (lq_sta->total_success > lq_sta->max_success_limit) ||
2150 ((!lq_sta->search_better_tbl) && (lq_sta->flush_timer)
Zhu Yib481de92007-09-25 17:54:57 -07002151 && (flush_interval_passed))) {
Johannes Berg0ca24da2012-03-15 13:26:46 -07002152 IWL_DEBUG_RATE(priv, "LQ: stay is expired %d %d %d\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002153 lq_sta->total_failed,
2154 lq_sta->total_success,
Zhu Yib481de92007-09-25 17:54:57 -07002155 flush_interval_passed);
Ben Cahill77626352007-11-29 11:09:44 +08002156
2157 /* Allow search for new mode */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002158 lq_sta->stay_in_tbl = 0; /* only place reset */
2159 lq_sta->total_failed = 0;
2160 lq_sta->total_success = 0;
2161 lq_sta->flush_timer = 0;
Ben Cahill77626352007-11-29 11:09:44 +08002162
2163 /*
2164 * Else if we've used this modulation mode enough repetitions
2165 * (regardless of elapsed time or success/failure), reset
2166 * history bitmaps and rate-specific stats for all rates in
2167 * active table.
2168 */
Mohamed Abbas403ab562007-11-06 22:06:25 -08002169 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002170 lq_sta->table_count++;
2171 if (lq_sta->table_count >=
2172 lq_sta->table_count_limit) {
2173 lq_sta->table_count = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002174
Tomas Winklere1623442009-01-27 14:27:56 -08002175 IWL_DEBUG_RATE(priv, "LQ: stay in table clear win\n");
Zhu Yib481de92007-09-25 17:54:57 -07002176 for (i = 0; i < IWL_RATE_COUNT; i++)
2177 rs_rate_scale_clear_window(
2178 &(tbl->win[i]));
2179 }
2180 }
2181
Ben Cahill77626352007-11-29 11:09:44 +08002182 /* If transitioning to allow "search", reset all history
2183 * bitmaps and stats in active table (this will become the new
2184 * "search" table). */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002185 if (!lq_sta->stay_in_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07002186 for (i = 0; i < IWL_RATE_COUNT; i++)
2187 rs_rate_scale_clear_window(&(tbl->win[i]));
2188 }
2189 }
2190}
2191
Ben Cahill77626352007-11-29 11:09:44 +08002192/*
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07002193 * setup rate table in uCode
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07002194 */
Johannes Berg70817b52011-05-11 03:29:25 -07002195static void rs_update_rate_tbl(struct iwl_priv *priv,
2196 struct iwl_rxon_context *ctx,
2197 struct iwl_lq_sta *lq_sta,
2198 struct iwl_scale_tbl_info *tbl,
2199 int index, u8 is_green)
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07002200{
2201 u32 rate;
2202
2203 /* Update uCode's rate table. */
2204 rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
2205 rs_fill_link_cmd(priv, lq_sta, rate);
Johannes Berg7e6a5882010-08-23 10:46:46 +02002206 iwl_send_lq_cmd(priv, ctx, &lq_sta->lq, CMD_ASYNC, false);
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07002207}
2208
2209/*
Ben Cahill77626352007-11-29 11:09:44 +08002210 * Do rate scaling and search for new modulation mode.
2211 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002212static void rs_rate_scale_perform(struct iwl_priv *priv,
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002213 struct sk_buff *skb,
Johannes Berg4b7679a2008-09-18 18:14:18 +02002214 struct ieee80211_sta *sta,
2215 struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002216{
Johannes Berg4b7679a2008-09-18 18:14:18 +02002217 struct ieee80211_hw *hw = priv->hw;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002218 struct ieee80211_conf *conf = &hw->conf;
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002219 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2220 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002221 int low = IWL_RATE_INVALID;
2222 int high = IWL_RATE_INVALID;
2223 int index;
2224 int i;
Tomas Winklere227cea2008-07-18 13:53:05 +08002225 struct iwl_rate_scale_data *window = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002226 int current_tpt = IWL_INVALID_VALUE;
2227 int low_tpt = IWL_INVALID_VALUE;
2228 int high_tpt = IWL_INVALID_VALUE;
2229 u32 fail_count;
2230 s8 scale_action = 0;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002231 u16 rate_mask;
Zhu Yib481de92007-09-25 17:54:57 -07002232 u8 update_lq = 0;
Tomas Winklere227cea2008-07-18 13:53:05 +08002233 struct iwl_scale_tbl_info *tbl, *tbl1;
Zhu Yib481de92007-09-25 17:54:57 -07002234 u16 rate_scale_index_msk = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002235 u8 is_green = 0;
2236 u8 active_tbl = 0;
2237 u8 done_search = 0;
2238 u16 high_low;
Guy Cohena5e8b502008-05-29 16:35:11 +08002239 s32 sr;
Emmanuel Grumbach5f85a782011-08-25 23:11:18 -07002240 u8 tid = IWL_MAX_TID_COUNT;
Wey-Yi Guy86b47662009-03-10 14:35:09 -07002241 struct iwl_tid_data *tid_data;
Johannes Berg7e6a5882010-08-23 10:46:46 +02002242 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
Johannes Bergc6892902011-09-20 15:37:21 -07002243 struct iwl_rxon_context *ctx = sta_priv->ctx;
Zhu Yib481de92007-09-25 17:54:57 -07002244
Tomas Winklere1623442009-01-27 14:27:56 -08002245 IWL_DEBUG_RATE(priv, "rate scale calculate new rate for skb\n");
Zhu Yib481de92007-09-25 17:54:57 -07002246
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002247 /* Send management frames and NO_ACK data using lowest rate. */
Tomas Winkler417f1142008-11-12 13:14:06 -08002248 /* TODO: this could probably be improved.. */
2249 if (!ieee80211_is_data(hdr->frame_control) ||
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002250 info->flags & IEEE80211_TX_CTL_NO_ACK)
Zhu Yib481de92007-09-25 17:54:57 -07002251 return;
Zhu Yib481de92007-09-25 17:54:57 -07002252
Johannes Berg4b7679a2008-09-18 18:14:18 +02002253 lq_sta->supp_rates = sta->supp_rates[lq_sta->band];
Zhu Yib481de92007-09-25 17:54:57 -07002254
Ron Rindjunskyfaa29712008-06-12 09:46:58 +08002255 tid = rs_tl_add_packet(lq_sta, hdr);
Emmanuel Grumbach5f85a782011-08-25 23:11:18 -07002256 if ((tid != IWL_MAX_TID_COUNT) &&
2257 (lq_sta->tx_agg_tid_en & (1 << tid))) {
Emmanuel Grumbach04cf6822011-11-23 11:06:12 +02002258 tid_data = &priv->tid_data[lq_sta->lq.sta_id][tid];
Daniel C Halperine3949d62009-09-17 10:43:50 -07002259 if (tid_data->agg.state == IWL_AGG_OFF)
2260 lq_sta->is_agg = 0;
2261 else
2262 lq_sta->is_agg = 1;
2263 } else
2264 lq_sta->is_agg = 0;
Ron Rindjunskyfaa29712008-06-12 09:46:58 +08002265
Ben Cahill77626352007-11-29 11:09:44 +08002266 /*
2267 * Select rate-scale / modulation-mode table to work with in
2268 * the rest of this function: "search" if searching for better
2269 * modulation mode, or "active" if doing rate scaling within a mode.
2270 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002271 if (!lq_sta->search_better_tbl)
2272 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002273 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002274 active_tbl = 1 - lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002275
Tomas Winklerc33104f2008-01-14 17:46:21 -08002276 tbl = &(lq_sta->lq_info[active_tbl]);
Wey-Yi Guy2681b202009-05-21 13:44:22 -07002277 if (is_legacy(tbl->lq_type))
2278 lq_sta->is_green = 0;
2279 else
Johannes Berg7e6a5882010-08-23 10:46:46 +02002280 lq_sta->is_green = rs_use_green(sta);
Tomas Winklerc33104f2008-01-14 17:46:21 -08002281 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07002282
Ben Cahill77626352007-11-29 11:09:44 +08002283 /* current tx rate */
Johannes Bergb7e35002008-09-11 02:22:58 +02002284 index = lq_sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002285
Tomas Winklere1623442009-01-27 14:27:56 -08002286 IWL_DEBUG_RATE(priv, "Rate scale index %d for type %d\n", index,
Zhu Yib481de92007-09-25 17:54:57 -07002287 tbl->lq_type);
2288
Ben Cahill77626352007-11-29 11:09:44 +08002289 /* rates available for this association, and for modulation mode */
Guy Coheneecd6e52008-04-23 17:14:58 -07002290 rate_mask = rs_get_supported_rates(lq_sta, hdr, tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -07002291
Frans Pop91dd6c22010-03-24 14:19:58 -07002292 IWL_DEBUG_RATE(priv, "mask 0x%04X\n", rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07002293
2294 /* mask with station rate restriction */
2295 if (is_legacy(tbl->lq_type)) {
Johannes Berg8318d782008-01-24 19:38:38 +01002296 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Ben Cahill77626352007-11-29 11:09:44 +08002297 /* supp_rates has no CCK bits in A mode */
Zhu Yib481de92007-09-25 17:54:57 -07002298 rate_scale_index_msk = (u16) (rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -08002299 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
Zhu Yib481de92007-09-25 17:54:57 -07002300 else
2301 rate_scale_index_msk = (u16) (rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -08002302 lq_sta->supp_rates);
Zhu Yib481de92007-09-25 17:54:57 -07002303
2304 } else
2305 rate_scale_index_msk = rate_mask;
2306
2307 if (!rate_scale_index_msk)
2308 rate_scale_index_msk = rate_mask;
2309
Guy Cohen07bc28e2008-04-23 17:15:03 -07002310 if (!((1 << index) & rate_scale_index_msk)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002311 IWL_ERR(priv, "Current Rate is not valid\n");
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07002312 if (lq_sta->search_better_tbl) {
2313 /* revert to active table if search table is not valid*/
2314 tbl->lq_type = LQ_NONE;
2315 lq_sta->search_better_tbl = 0;
2316 tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
2317 /* get "active" rate info */
2318 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
Johannes Berg70817b52011-05-11 03:29:25 -07002319 rs_update_rate_tbl(priv, ctx, lq_sta, tbl,
2320 index, is_green);
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07002321 }
Guy Cohen07bc28e2008-04-23 17:15:03 -07002322 return;
Zhu Yib481de92007-09-25 17:54:57 -07002323 }
2324
Ben Cahill77626352007-11-29 11:09:44 +08002325 /* Get expected throughput table and history window for current rate */
Guy Cohen07bc28e2008-04-23 17:15:03 -07002326 if (!tbl->expected_tpt) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002327 IWL_ERR(priv, "tbl->expected_tpt is NULL\n");
Guy Cohen07bc28e2008-04-23 17:15:03 -07002328 return;
2329 }
Zhu Yib481de92007-09-25 17:54:57 -07002330
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002331 /* force user max rate if set by user */
2332 if ((lq_sta->max_rate_idx != -1) &&
2333 (lq_sta->max_rate_idx < index)) {
2334 index = lq_sta->max_rate_idx;
2335 update_lq = 1;
2336 window = &(tbl->win[index]);
2337 goto lq_update;
2338 }
2339
Zhu Yib481de92007-09-25 17:54:57 -07002340 window = &(tbl->win[index]);
2341
Ben Cahill77626352007-11-29 11:09:44 +08002342 /*
2343 * If there is not enough history to calculate actual average
2344 * throughput, keep analyzing results of more tx frames, without
2345 * changing rate or mode (bypass most of the rest of this function).
2346 * Set up new rate table in uCode only if old rate is not supported
2347 * in current association (use new rate found above).
2348 */
Zhu Yib481de92007-09-25 17:54:57 -07002349 fail_count = window->counter - window->success_counter;
Guy Cohen07bc28e2008-04-23 17:15:03 -07002350 if ((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
2351 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002352 IWL_DEBUG_RATE(priv, "LQ: still below TH. succ=%d total=%d "
Zhu Yib481de92007-09-25 17:54:57 -07002353 "for index %d\n",
2354 window->success_counter, window->counter, index);
Ben Cahill77626352007-11-29 11:09:44 +08002355
2356 /* Can't calculate this yet; not enough history */
Zhu Yib481de92007-09-25 17:54:57 -07002357 window->average_tpt = IWL_INVALID_VALUE;
Ben Cahill77626352007-11-29 11:09:44 +08002358
2359 /* Should we stay with this modulation mode,
2360 * or search for a new one? */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07002361 rs_stay_in_table(lq_sta, false);
Ben Cahill77626352007-11-29 11:09:44 +08002362
Zhu Yib481de92007-09-25 17:54:57 -07002363 goto out;
Guy Cohen3110bef2008-09-09 10:54:54 +08002364 }
Ben Cahill77626352007-11-29 11:09:44 +08002365 /* Else we have enough samples; calculate estimate of
2366 * actual average throughput */
Reinette Chatre3d79b2a2010-05-03 10:55:07 -07002367 if (window->average_tpt != ((window->success_ratio *
2368 tbl->expected_tpt[index] + 64) / 128)) {
2369 IWL_ERR(priv, "expected_tpt should have been calculated by now\n");
2370 window->average_tpt = ((window->success_ratio *
2371 tbl->expected_tpt[index] + 64) / 128);
2372 }
Zhu Yib481de92007-09-25 17:54:57 -07002373
Ben Cahill77626352007-11-29 11:09:44 +08002374 /* If we are searching for better modulation mode, check success. */
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002375 if (lq_sta->search_better_tbl &&
Johannes Berg3ad3b922009-08-07 15:41:48 -07002376 (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_MULTI)) {
Ben Cahill77626352007-11-29 11:09:44 +08002377 /* If good success, continue using the "search" mode;
2378 * no need to send new link quality command, since we're
2379 * continuing to use the setup that we've been trying. */
Guy Cohen07bc28e2008-04-23 17:15:03 -07002380 if (window->average_tpt > lq_sta->last_tpt) {
2381
Tomas Winklere1623442009-01-27 14:27:56 -08002382 IWL_DEBUG_RATE(priv, "LQ: SWITCHING TO NEW TABLE "
Guy Cohen07bc28e2008-04-23 17:15:03 -07002383 "suc=%d cur-tpt=%d old-tpt=%d\n",
2384 window->success_ratio,
2385 window->average_tpt,
2386 lq_sta->last_tpt);
2387
2388 if (!is_legacy(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08002389 lq_sta->enable_counter = 1;
Guy Cohen07bc28e2008-04-23 17:15:03 -07002390
Ben Cahill77626352007-11-29 11:09:44 +08002391 /* Swap tables; "search" becomes "active" */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002392 lq_sta->active_tbl = active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002393 current_tpt = window->average_tpt;
Ben Cahill77626352007-11-29 11:09:44 +08002394
2395 /* Else poor success; go back to mode in "active" table */
Zhu Yib481de92007-09-25 17:54:57 -07002396 } else {
Guy Cohen07bc28e2008-04-23 17:15:03 -07002397
Tomas Winklere1623442009-01-27 14:27:56 -08002398 IWL_DEBUG_RATE(priv, "LQ: GOING BACK TO THE OLD TABLE "
Guy Cohen07bc28e2008-04-23 17:15:03 -07002399 "suc=%d cur-tpt=%d old-tpt=%d\n",
2400 window->success_ratio,
2401 window->average_tpt,
2402 lq_sta->last_tpt);
2403
Ben Cahill77626352007-11-29 11:09:44 +08002404 /* Nullify "search" table */
Zhu Yib481de92007-09-25 17:54:57 -07002405 tbl->lq_type = LQ_NONE;
Ben Cahill77626352007-11-29 11:09:44 +08002406
2407 /* Revert to "active" table */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002408 active_tbl = lq_sta->active_tbl;
2409 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07002410
Ben Cahill77626352007-11-29 11:09:44 +08002411 /* Revert to "active" rate and throughput info */
Tomas Winklere7d326ac2008-06-12 09:47:11 +08002412 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
Tomas Winklerc33104f2008-01-14 17:46:21 -08002413 current_tpt = lq_sta->last_tpt;
Ben Cahill77626352007-11-29 11:09:44 +08002414
2415 /* Need to set up a new rate table in uCode */
2416 update_lq = 1;
Zhu Yib481de92007-09-25 17:54:57 -07002417 }
Ben Cahill77626352007-11-29 11:09:44 +08002418
2419 /* Either way, we've made a decision; modulation mode
2420 * search is done, allow rate adjustment next time. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002421 lq_sta->search_better_tbl = 0;
Ben Cahill77626352007-11-29 11:09:44 +08002422 done_search = 1; /* Don't switch modes below! */
Zhu Yib481de92007-09-25 17:54:57 -07002423 goto lq_update;
2424 }
2425
Ben Cahill77626352007-11-29 11:09:44 +08002426 /* (Else) not in search of better modulation mode, try for better
2427 * starting rate, while staying in this mode. */
Ester Kummerbf403db2008-05-05 10:22:40 +08002428 high_low = rs_get_adjacent_rate(priv, index, rate_scale_index_msk,
Zhu Yib481de92007-09-25 17:54:57 -07002429 tbl->lq_type);
2430 low = high_low & 0xff;
2431 high = (high_low >> 8) & 0xff;
2432
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002433 /* If user set max rate, dont allow higher than user constrain */
2434 if ((lq_sta->max_rate_idx != -1) &&
2435 (lq_sta->max_rate_idx < high))
2436 high = IWL_RATE_INVALID;
2437
Guy Cohena5e8b502008-05-29 16:35:11 +08002438 sr = window->success_ratio;
2439
Ben Cahill77626352007-11-29 11:09:44 +08002440 /* Collect measured throughputs for current and adjacent rates */
Zhu Yib481de92007-09-25 17:54:57 -07002441 current_tpt = window->average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07002442 if (low != IWL_RATE_INVALID)
2443 low_tpt = tbl->win[low].average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07002444 if (high != IWL_RATE_INVALID)
2445 high_tpt = tbl->win[high].average_tpt;
2446
Guy Cohena5e8b502008-05-29 16:35:11 +08002447 scale_action = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002448
Ben Cahill77626352007-11-29 11:09:44 +08002449 /* Too many failures, decrease rate */
Guy Cohena5e8b502008-05-29 16:35:11 +08002450 if ((sr <= IWL_RATE_DECREASE_TH) || (current_tpt == 0)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002451 IWL_DEBUG_RATE(priv, "decrease rate because of low success_ratio\n");
Zhu Yib481de92007-09-25 17:54:57 -07002452 scale_action = -1;
Ben Cahill77626352007-11-29 11:09:44 +08002453
2454 /* No throughput measured yet for adjacent rates; try increase. */
Zhu Yib481de92007-09-25 17:54:57 -07002455 } else if ((low_tpt == IWL_INVALID_VALUE) &&
Guy Cohena5e8b502008-05-29 16:35:11 +08002456 (high_tpt == IWL_INVALID_VALUE)) {
2457
2458 if (high != IWL_RATE_INVALID && sr >= IWL_RATE_INCREASE_TH)
2459 scale_action = 1;
2460 else if (low != IWL_RATE_INVALID)
Wey-Yi Guy8fe72312009-03-10 14:35:10 -07002461 scale_action = 0;
Guy Cohena5e8b502008-05-29 16:35:11 +08002462 }
Ben Cahill77626352007-11-29 11:09:44 +08002463
2464 /* Both adjacent throughputs are measured, but neither one has better
2465 * throughput; we're using the best rate, don't change it! */
Zhu Yib481de92007-09-25 17:54:57 -07002466 else if ((low_tpt != IWL_INVALID_VALUE) &&
2467 (high_tpt != IWL_INVALID_VALUE) &&
2468 (low_tpt < current_tpt) &&
2469 (high_tpt < current_tpt))
2470 scale_action = 0;
Ben Cahill77626352007-11-29 11:09:44 +08002471
2472 /* At least one adjacent rate's throughput is measured,
2473 * and may have better performance. */
Zhu Yib481de92007-09-25 17:54:57 -07002474 else {
Ben Cahill77626352007-11-29 11:09:44 +08002475 /* Higher adjacent rate's throughput is measured */
Zhu Yib481de92007-09-25 17:54:57 -07002476 if (high_tpt != IWL_INVALID_VALUE) {
Ben Cahill77626352007-11-29 11:09:44 +08002477 /* Higher rate has better throughput */
Guy Cohena5e8b502008-05-29 16:35:11 +08002478 if (high_tpt > current_tpt &&
2479 sr >= IWL_RATE_INCREASE_TH) {
Zhu Yib481de92007-09-25 17:54:57 -07002480 scale_action = 1;
Guy Cohena5e8b502008-05-29 16:35:11 +08002481 } else {
Wey-Yi Guy8fe72312009-03-10 14:35:10 -07002482 scale_action = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002483 }
Ben Cahill77626352007-11-29 11:09:44 +08002484
2485 /* Lower adjacent rate's throughput is measured */
Zhu Yib481de92007-09-25 17:54:57 -07002486 } else if (low_tpt != IWL_INVALID_VALUE) {
Ben Cahill77626352007-11-29 11:09:44 +08002487 /* Lower rate has better throughput */
Zhu Yib481de92007-09-25 17:54:57 -07002488 if (low_tpt > current_tpt) {
Tomas Winklere1623442009-01-27 14:27:56 -08002489 IWL_DEBUG_RATE(priv,
2490 "decrease rate because of low tpt\n");
Zhu Yib481de92007-09-25 17:54:57 -07002491 scale_action = -1;
Guy Cohena5e8b502008-05-29 16:35:11 +08002492 } else if (sr >= IWL_RATE_INCREASE_TH) {
Zhu Yib481de92007-09-25 17:54:57 -07002493 scale_action = 1;
Guy Cohena5e8b502008-05-29 16:35:11 +08002494 }
Zhu Yib481de92007-09-25 17:54:57 -07002495 }
2496 }
2497
Ben Cahill77626352007-11-29 11:09:44 +08002498 /* Sanity check; asked for decrease, but success rate or throughput
2499 * has been good at old rate. Don't change it. */
Guy Cohena5e8b502008-05-29 16:35:11 +08002500 if ((scale_action == -1) && (low != IWL_RATE_INVALID) &&
2501 ((sr > IWL_RATE_HIGH_TH) ||
Zhu Yib481de92007-09-25 17:54:57 -07002502 (current_tpt > (100 * tbl->expected_tpt[low]))))
Zhu Yib481de92007-09-25 17:54:57 -07002503 scale_action = 0;
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002504 if (!iwl_ht_enabled(priv) && !is_legacy(tbl->lq_type))
2505 scale_action = -1;
Johannes Berg3ad3b922009-08-07 15:41:48 -07002506 if (iwl_tx_ant_restriction(priv) != IWL_ANT_OK_MULTI &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002507 (is_mimo2(tbl->lq_type) || is_mimo3(tbl->lq_type)))
2508 scale_action = -1;
Johannes Berg290f5992010-08-23 07:56:58 -07002509
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07002510 if ((priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH) &&
2511 (is_mimo2(tbl->lq_type) || is_mimo3(tbl->lq_type))) {
2512 if (lq_sta->last_bt_traffic > priv->bt_traffic_load) {
2513 /*
2514 * don't set scale_action, don't want to scale up if
2515 * the rate scale doesn't otherwise think that is a
2516 * good idea.
2517 */
2518 } else if (lq_sta->last_bt_traffic <= priv->bt_traffic_load) {
2519 scale_action = -1;
2520 }
2521 }
2522 lq_sta->last_bt_traffic = priv->bt_traffic_load;
2523
2524 if ((priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH) &&
2525 (is_mimo2(tbl->lq_type) || is_mimo3(tbl->lq_type))) {
2526 /* search for a new modulation */
2527 rs_stay_in_table(lq_sta, true);
2528 goto lq_update;
Johannes Berg290f5992010-08-23 07:56:58 -07002529 }
2530
Zhu Yib481de92007-09-25 17:54:57 -07002531 switch (scale_action) {
2532 case -1:
Ben Cahill77626352007-11-29 11:09:44 +08002533 /* Decrease starting rate, update uCode's rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002534 if (low != IWL_RATE_INVALID) {
2535 update_lq = 1;
2536 index = low;
2537 }
Mohamed Abbas447fee72009-04-20 14:37:02 -07002538
Zhu Yib481de92007-09-25 17:54:57 -07002539 break;
2540 case 1:
Ben Cahill77626352007-11-29 11:09:44 +08002541 /* Increase starting rate, update uCode's rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002542 if (high != IWL_RATE_INVALID) {
2543 update_lq = 1;
2544 index = high;
2545 }
2546
2547 break;
2548 case 0:
Ben Cahill77626352007-11-29 11:09:44 +08002549 /* No change */
Zhu Yib481de92007-09-25 17:54:57 -07002550 default:
2551 break;
2552 }
2553
Tomas Winklere1623442009-01-27 14:27:56 -08002554 IWL_DEBUG_RATE(priv, "choose rate scale index %d action %d low %d "
Zhu Yib481de92007-09-25 17:54:57 -07002555 "high %d type %d\n",
2556 index, scale_action, low, high, tbl->lq_type);
2557
Guy Cohena5e8b502008-05-29 16:35:11 +08002558lq_update:
Ben Cahill77626352007-11-29 11:09:44 +08002559 /* Replace uCode's rate table for the destination station. */
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07002560 if (update_lq)
Johannes Berg70817b52011-05-11 03:29:25 -07002561 rs_update_rate_tbl(priv, ctx, lq_sta, tbl, index, is_green);
Ben Cahill77626352007-11-29 11:09:44 +08002562
Johannes Berg3ad3b922009-08-07 15:41:48 -07002563 if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_MULTI) {
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002564 /* Should we stay with this modulation mode,
2565 * or search for a new one? */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07002566 rs_stay_in_table(lq_sta, false);
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002567 }
Ben Cahill77626352007-11-29 11:09:44 +08002568 /*
2569 * Search for new modulation mode if we're:
2570 * 1) Not changing rates right now
2571 * 2) Not just finishing up a search
2572 * 3) Allowing a new search
2573 */
Guy Cohen47cfd462008-05-27 11:29:34 +08002574 if (!update_lq && !done_search && !lq_sta->stay_in_tbl && window->counter) {
Ben Cahill77626352007-11-29 11:09:44 +08002575 /* Save current throughput to compare with "search" throughput*/
Tomas Winklerc33104f2008-01-14 17:46:21 -08002576 lq_sta->last_tpt = current_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07002577
Ben Cahill77626352007-11-29 11:09:44 +08002578 /* Select a new "search" modulation mode to try.
2579 * If one is found, set up the new "search" table. */
Zhu Yib481de92007-09-25 17:54:57 -07002580 if (is_legacy(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08002581 rs_move_legacy_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07002582 else if (is_siso(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08002583 rs_move_siso_to_other(priv, lq_sta, conf, sta, index);
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07002584 else if (is_mimo2(tbl->lq_type))
2585 rs_move_mimo2_to_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07002586 else
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07002587 rs_move_mimo3_to_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07002588
Ben Cahill77626352007-11-29 11:09:44 +08002589 /* If new "search" mode was selected, set up in uCode table */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002590 if (lq_sta->search_better_tbl) {
Ben Cahill77626352007-11-29 11:09:44 +08002591 /* Access the "search" table, clear its history. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002592 tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Zhu Yib481de92007-09-25 17:54:57 -07002593 for (i = 0; i < IWL_RATE_COUNT; i++)
2594 rs_rate_scale_clear_window(&(tbl->win[i]));
2595
Ben Cahill77626352007-11-29 11:09:44 +08002596 /* Use new "search" start rate */
Tomas Winklere7d326ac2008-06-12 09:47:11 +08002597 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002598
Tomas Winklere1623442009-01-27 14:27:56 -08002599 IWL_DEBUG_RATE(priv, "Switch current mcs: %X index: %d\n",
Guy Cohen39e88502008-04-23 17:14:57 -07002600 tbl->current_rate, index);
Guy Cohen07bc28e2008-04-23 17:15:03 -07002601 rs_fill_link_cmd(priv, lq_sta, tbl->current_rate);
Johannes Berg7e6a5882010-08-23 10:46:46 +02002602 iwl_send_lq_cmd(priv, ctx, &lq_sta->lq, CMD_ASYNC, false);
Mohamed Abbas447fee72009-04-20 14:37:02 -07002603 } else
2604 done_search = 1;
2605 }
Zhu Yib481de92007-09-25 17:54:57 -07002606
Mohamed Abbas447fee72009-04-20 14:37:02 -07002607 if (done_search && !lq_sta->stay_in_tbl) {
Ben Cahill77626352007-11-29 11:09:44 +08002608 /* If the "active" (non-search) mode was legacy,
2609 * and we've tried switching antennas,
2610 * but we haven't been able to try HT modes (not available),
2611 * stay with best antenna legacy modulation for a while
2612 * before next round of mode comparisons. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002613 tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
Luis R. Rodriguezde27e642008-12-23 15:58:44 -08002614 if (is_legacy(tbl1->lq_type) && !conf_is_ht(conf) &&
Mohamed Abbasd6e93392009-05-08 13:44:39 -07002615 lq_sta->action_counter > tbl1->max_search) {
Tomas Winklere1623442009-01-27 14:27:56 -08002616 IWL_DEBUG_RATE(priv, "LQ: STAY in legacy table\n");
Ester Kummerbf403db2008-05-05 10:22:40 +08002617 rs_set_stay_in_table(priv, 1, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002618 }
2619
Ben Cahill77626352007-11-29 11:09:44 +08002620 /* If we're in an HT mode, and all 3 mode switch actions
2621 * have been tried and compared, stay in this best modulation
2622 * mode for a while before next round of mode comparisons. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002623 if (lq_sta->enable_counter &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002624 (lq_sta->action_counter >= tbl1->max_search) &&
2625 iwl_ht_enabled(priv)) {
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002626 if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
2627 (lq_sta->tx_agg_tid_en & (1 << tid)) &&
Emmanuel Grumbach5f85a782011-08-25 23:11:18 -07002628 (tid != IWL_MAX_TID_COUNT)) {
2629 u8 sta_id = lq_sta->lq.sta_id;
Emmanuel Grumbach04cf6822011-11-23 11:06:12 +02002630 tid_data = &priv->tid_data[sta_id][tid];
Wey-Yi Guy86b47662009-03-10 14:35:09 -07002631 if (tid_data->agg.state == IWL_AGG_OFF) {
2632 IWL_DEBUG_RATE(priv,
2633 "try to aggregate tid %d\n",
2634 tid);
2635 rs_tl_turn_on_agg(priv, tid,
2636 lq_sta, sta);
2637 }
Zhu Yib481de92007-09-25 17:54:57 -07002638 }
Ester Kummerbf403db2008-05-05 10:22:40 +08002639 rs_set_stay_in_table(priv, 0, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002640 }
Zhu Yib481de92007-09-25 17:54:57 -07002641 }
2642
2643out:
Tomas Winkler978785a2008-12-19 10:37:31 +08002644 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
Wey-Yi Guyc3322b32011-10-10 07:26:58 -07002645 lq_sta->last_txrate_idx = index;
Zhu Yib481de92007-09-25 17:54:57 -07002646}
2647
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08002648/**
2649 * rs_initialize_lq - Initialize a station's hardware rate table
2650 *
2651 * The uCode's station table contains a table of fallback rates
2652 * for automatic fallback during transmission.
2653 *
2654 * NOTE: This sets up a default set of values. These will be replaced later
2655 * if the driver's iwl-agn-rs rate scaling algorithm is used, instead of
2656 * rc80211_simple.
2657 *
2658 * NOTE: Run REPLY_ADD_STA command to set up station table entry, before
2659 * calling this function (which runs REPLY_TX_LINK_QUALITY_CMD,
2660 * which requires station table entry to exist).
2661 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002662static void rs_initialize_lq(struct iwl_priv *priv,
Johannes Berg4b7679a2008-09-18 18:14:18 +02002663 struct ieee80211_sta *sta,
2664 struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002665{
Tomas Winklere227cea2008-07-18 13:53:05 +08002666 struct iwl_scale_tbl_info *tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002667 int rate_idx;
Guy Cohenaade00c2008-04-23 17:14:59 -07002668 int i;
Guy Cohen39e88502008-04-23 17:14:57 -07002669 u32 rate;
Johannes Berg7e6a5882010-08-23 10:46:46 +02002670 u8 use_green = rs_use_green(sta);
Guy Cohenaade00c2008-04-23 17:14:59 -07002671 u8 active_tbl = 0;
2672 u8 valid_tx_ant;
Johannes Berg7e6a5882010-08-23 10:46:46 +02002673 struct iwl_station_priv *sta_priv;
2674 struct iwl_rxon_context *ctx;
Zhu Yib481de92007-09-25 17:54:57 -07002675
Johannes Berg4b7679a2008-09-18 18:14:18 +02002676 if (!sta || !lq_sta)
Johannes Berg7e6a5882010-08-23 10:46:46 +02002677 return;
2678
2679 sta_priv = (void *)sta->drv_priv;
Johannes Bergc6892902011-09-20 15:37:21 -07002680 ctx = sta_priv->ctx;
Zhu Yib481de92007-09-25 17:54:57 -07002681
Johannes Bergb7e35002008-09-11 02:22:58 +02002682 i = lq_sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002683
Eytan Lifshitzb7998c82012-12-01 20:59:49 +02002684 valid_tx_ant = priv->nvm_data->valid_tx_ant;
Guy Cohenaade00c2008-04-23 17:14:59 -07002685
Tomas Winklerc33104f2008-01-14 17:46:21 -08002686 if (!lq_sta->search_better_tbl)
2687 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002688 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002689 active_tbl = 1 - lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002690
Tomas Winklerc33104f2008-01-14 17:46:21 -08002691 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07002692
2693 if ((i < 0) || (i >= IWL_RATE_COUNT))
2694 i = 0;
2695
Tomas Winkler1826dcc2008-05-15 13:54:02 +08002696 rate = iwl_rates[i].plcp;
Winkler, Tomaseb48dca2008-10-29 14:05:48 -07002697 tbl->ant_type = first_antenna(valid_tx_ant);
2698 rate |= tbl->ant_type << RATE_MCS_ANT_POS;
Zhu Yib481de92007-09-25 17:54:57 -07002699
2700 if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
Guy Cohen39e88502008-04-23 17:14:57 -07002701 rate |= RATE_MCS_CCK_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002702
Guy Cohen39e88502008-04-23 17:14:57 -07002703 rs_get_tbl_info_from_mcs(rate, priv->band, tbl, &rate_idx);
Guy Cohenaade00c2008-04-23 17:14:59 -07002704 if (!rs_is_valid_ant(valid_tx_ant, tbl->ant_type))
2705 rs_toggle_antenna(valid_tx_ant, &rate, tbl);
Zhu Yib481de92007-09-25 17:54:57 -07002706
Tomas Winkler978785a2008-12-19 10:37:31 +08002707 rate = rate_n_flags_from_tbl(priv, tbl, rate_idx, use_green);
Guy Cohen39e88502008-04-23 17:14:57 -07002708 tbl->current_rate = rate;
Guy Coheneecd6e52008-04-23 17:14:58 -07002709 rs_set_expected_tpt_table(lq_sta, tbl);
Guy Cohen07bc28e2008-04-23 17:15:03 -07002710 rs_fill_link_cmd(NULL, lq_sta, rate);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08002711 priv->stations[lq_sta->lq.sta_id].lq = &lq_sta->lq;
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03002712 iwl_send_lq_cmd(priv, ctx, &lq_sta->lq, 0, true);
Zhu Yib481de92007-09-25 17:54:57 -07002713}
2714
Johannes Berge6a98542008-10-21 12:40:02 +02002715static void rs_get_rate(void *priv_r, struct ieee80211_sta *sta, void *priv_sta,
2716 struct ieee80211_tx_rate_control *txrc)
Zhu Yib481de92007-09-25 17:54:57 -07002717{
2718
Johannes Berge6a98542008-10-21 12:40:02 +02002719 struct sk_buff *skb = txrc->skb;
2720 struct ieee80211_supported_band *sband = txrc->sband;
Emmanuel Grumbachd0f76d62012-02-09 16:08:15 +02002721 struct iwl_op_mode *op_mode __maybe_unused =
2722 (struct iwl_op_mode *)priv_r;
2723 struct iwl_priv *priv __maybe_unused = IWL_OP_MODE_GET_DVM(op_mode);
Johannes Berge6a98542008-10-21 12:40:02 +02002724 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Tomas Winkler417f1142008-11-12 13:14:06 -08002725 struct iwl_lq_sta *lq_sta = priv_sta;
2726 int rate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002727
Tomas Winklere1623442009-01-27 14:27:56 -08002728 IWL_DEBUG_RATE_LIMIT(priv, "rate scale calculate new rate for skb\n");
Zhu Yib481de92007-09-25 17:54:57 -07002729
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002730 /* Get max rate if user set max rate */
2731 if (lq_sta) {
2732 lq_sta->max_rate_idx = txrc->max_rate_idx;
2733 if ((sband->band == IEEE80211_BAND_5GHZ) &&
2734 (lq_sta->max_rate_idx != -1))
2735 lq_sta->max_rate_idx += IWL_FIRST_OFDM_RATE;
2736 if ((lq_sta->max_rate_idx < 0) ||
2737 (lq_sta->max_rate_idx >= IWL_RATE_COUNT))
2738 lq_sta->max_rate_idx = -1;
2739 }
2740
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08002741 /* Treat uninitialized rate scaling data same as non-existing. */
2742 if (lq_sta && !lq_sta->drv) {
2743 IWL_DEBUG_RATE(priv, "Rate scaling not initialized yet.\n");
2744 priv_sta = NULL;
2745 }
2746
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002747 /* Send management frames and NO_ACK data using lowest rate. */
Luis R. Rodriguez4c6d4f52009-07-16 10:05:41 -07002748 if (rate_control_send_low(sta, priv_sta, txrc))
Johannes Berg4b7679a2008-09-18 18:14:18 +02002749 return;
Zhu Yib481de92007-09-25 17:54:57 -07002750
Tomas Winkler417f1142008-11-12 13:14:06 -08002751 rate_idx = lq_sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002752
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002753 if (lq_sta->last_rate_n_flags & RATE_MCS_HT_MSK) {
Tomas Winkler417f1142008-11-12 13:14:06 -08002754 rate_idx -= IWL_FIRST_OFDM_RATE;
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002755 /* 6M and 9M shared same MCS index */
2756 rate_idx = (rate_idx > 0) ? (rate_idx - 1) : 0;
2757 if (rs_extract_rate(lq_sta->last_rate_n_flags) >=
2758 IWL_RATE_MIMO3_6M_PLCP)
2759 rate_idx = rate_idx + (2 * MCS_INDEX_PER_STREAM);
2760 else if (rs_extract_rate(lq_sta->last_rate_n_flags) >=
2761 IWL_RATE_MIMO2_6M_PLCP)
2762 rate_idx = rate_idx + MCS_INDEX_PER_STREAM;
2763 info->control.rates[0].flags = IEEE80211_TX_RC_MCS;
2764 if (lq_sta->last_rate_n_flags & RATE_MCS_SGI_MSK)
2765 info->control.rates[0].flags |= IEEE80211_TX_RC_SHORT_GI;
2766 if (lq_sta->last_rate_n_flags & RATE_MCS_DUP_MSK)
2767 info->control.rates[0].flags |= IEEE80211_TX_RC_DUP_DATA;
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07002768 if (lq_sta->last_rate_n_flags & RATE_MCS_HT40_MSK)
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002769 info->control.rates[0].flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
2770 if (lq_sta->last_rate_n_flags & RATE_MCS_GF_MSK)
2771 info->control.rates[0].flags |= IEEE80211_TX_RC_GREEN_FIELD;
2772 } else {
Daniel C Halperin50273092009-08-28 09:44:45 -07002773 /* Check for invalid rates */
2774 if ((rate_idx < 0) || (rate_idx >= IWL_RATE_COUNT_LEGACY) ||
2775 ((sband->band == IEEE80211_BAND_5GHZ) &&
2776 (rate_idx < IWL_FIRST_OFDM_RATE)))
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002777 rate_idx = rate_lowest_index(sband, sta);
Daniel C Halperin50273092009-08-28 09:44:45 -07002778 /* On valid 5 GHz rate, adjust index */
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002779 else if (sband->band == IEEE80211_BAND_5GHZ)
2780 rate_idx -= IWL_FIRST_OFDM_RATE;
Daniel C Halperin7ebaeff2009-08-21 13:34:20 -07002781 info->control.rates[0].flags = 0;
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002782 }
Tomas Winkler417f1142008-11-12 13:14:06 -08002783 info->control.rates[0].idx = rate_idx;
Moshe Benji622ebe92013-06-03 19:27:16 +03002784 info->control.rates[0].count = 1;
Zhu Yib481de92007-09-25 17:54:57 -07002785}
2786
Johannes Berg4b7679a2008-09-18 18:14:18 +02002787static void *rs_alloc_sta(void *priv_rate, struct ieee80211_sta *sta,
2788 gfp_t gfp)
Zhu Yib481de92007-09-25 17:54:57 -07002789{
Reinette Chatre8d9698b2009-10-16 14:25:55 -07002790 struct iwl_station_priv *sta_priv = (struct iwl_station_priv *) sta->drv_priv;
Emmanuel Grumbachd0f76d62012-02-09 16:08:15 +02002791 struct iwl_op_mode *op_mode __maybe_unused =
2792 (struct iwl_op_mode *)priv_rate;
2793 struct iwl_priv *priv __maybe_unused = IWL_OP_MODE_GET_DVM(op_mode);
Zhu Yib481de92007-09-25 17:54:57 -07002794
Tomas Winklere1623442009-01-27 14:27:56 -08002795 IWL_DEBUG_RATE(priv, "create station rate scale window\n");
Zhu Yib481de92007-09-25 17:54:57 -07002796
Johannes Berg08960de2011-04-05 09:41:53 -07002797 return &sta_priv->lq_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002798}
2799
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08002800/*
2801 * Called after adding a new station to initialize rate scaling
2802 */
2803void iwl_rs_rate_init(struct iwl_priv *priv, struct ieee80211_sta *sta, u8 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07002804{
2805 int i, j;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08002806 struct ieee80211_hw *hw = priv->hw;
Johannes Berg4b7679a2008-09-18 18:14:18 +02002807 struct ieee80211_conf *conf = &priv->hw->conf;
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002808 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08002809 struct iwl_station_priv *sta_priv;
2810 struct iwl_lq_sta *lq_sta;
2811 struct ieee80211_supported_band *sband;
Johannes Berg3ac40ed2012-03-08 09:18:04 +01002812 unsigned long supp; /* must be unsigned long for for_each_set_bit */
Zhu Yib481de92007-09-25 17:54:57 -07002813
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08002814 sta_priv = (struct iwl_station_priv *) sta->drv_priv;
2815 lq_sta = &sta_priv->lq_sta;
Karl Beldan675a0b02013-03-25 16:26:57 +01002816 sband = hw->wiphy->bands[conf->chandef.chan->band];
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08002817
2818
2819 lq_sta->lq.sta_id = sta_id;
Reinette Chatre5ad13f82009-10-30 14:36:14 -07002820
2821 for (j = 0; j < LQ_SIZE; j++)
2822 for (i = 0; i < IWL_RATE_COUNT; i++)
2823 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
2824
Tomas Winklerc33104f2008-01-14 17:46:21 -08002825 lq_sta->flush_timer = 0;
Johannes Berg4b7679a2008-09-18 18:14:18 +02002826 lq_sta->supp_rates = sta->supp_rates[sband->band];
Zhu Yib481de92007-09-25 17:54:57 -07002827
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08002828 IWL_DEBUG_RATE(priv, "LQ: *** rate scale station global init for station %d ***\n",
2829 sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07002830 /* TODO: what is a good starting rate for STA? About middle? Maybe not
2831 * the lowest or the highest rate.. Could consider using RSSI from
2832 * previous packets? Need to have IEEE 802.1X auth succeed immediately
2833 * after assoc.. */
2834
Tomas Winklerc33104f2008-01-14 17:46:21 -08002835 lq_sta->is_dup = 0;
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002836 lq_sta->max_rate_idx = -1;
Abbas, Mohamed7d049e52009-01-20 21:33:53 -08002837 lq_sta->missed_rate_counter = IWL_MISSED_RATE_MAX;
Johannes Berg7e6a5882010-08-23 10:46:46 +02002838 lq_sta->is_green = rs_use_green(sta);
Johannes Berg3ac40ed2012-03-08 09:18:04 +01002839 lq_sta->band = sband->band;
2840 /*
2841 * active legacy rates as per supported rates bitmap
2842 */
2843 supp = sta->supp_rates[sband->band];
2844 lq_sta->active_legacy_rate = 0;
2845 for_each_set_bit(i, &supp, BITS_PER_LONG)
2846 lq_sta->active_legacy_rate |= BIT(sband->bitrates[i].hw_value);
2847
Ben Cahill77626352007-11-29 11:09:44 +08002848 /*
2849 * active_siso_rate mask includes 9 MBits (bit 5), and CCK (bits 0-3),
2850 * supp_rates[] does not; shift to convert format, force 9 MBits off.
2851 */
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002852 lq_sta->active_siso_rate = ht_cap->mcs.rx_mask[0] << 1;
2853 lq_sta->active_siso_rate |= ht_cap->mcs.rx_mask[0] & 0x1;
Guy Cohenfde0db32008-04-21 15:42:01 -07002854 lq_sta->active_siso_rate &= ~((u16)0x2);
2855 lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -07002856
Ben Cahill77626352007-11-29 11:09:44 +08002857 /* Same here */
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002858 lq_sta->active_mimo2_rate = ht_cap->mcs.rx_mask[1] << 1;
2859 lq_sta->active_mimo2_rate |= ht_cap->mcs.rx_mask[1] & 0x1;
Guy Cohenfde0db32008-04-21 15:42:01 -07002860 lq_sta->active_mimo2_rate &= ~((u16)0x2);
2861 lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE;
2862
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002863 lq_sta->active_mimo3_rate = ht_cap->mcs.rx_mask[2] << 1;
2864 lq_sta->active_mimo3_rate |= ht_cap->mcs.rx_mask[2] & 0x1;
Guy Cohenfde0db32008-04-21 15:42:01 -07002865 lq_sta->active_mimo3_rate &= ~((u16)0x2);
2866 lq_sta->active_mimo3_rate <<= IWL_FIRST_OFDM_RATE;
2867
Tomas Winklere1623442009-01-27 14:27:56 -08002868 IWL_DEBUG_RATE(priv, "SISO-RATE=%X MIMO2-RATE=%X MIMO3-RATE=%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002869 lq_sta->active_siso_rate,
Guy Cohenfde0db32008-04-21 15:42:01 -07002870 lq_sta->active_mimo2_rate,
2871 lq_sta->active_mimo3_rate);
2872
Tomas Winklera96a27f2008-10-23 23:48:56 -07002873 /* These values will be overridden later */
Wey-Yi Guy3a23d692010-04-03 16:44:39 -07002874 lq_sta->lq.general_params.single_stream_ant_msk =
Eytan Lifshitzb7998c82012-12-01 20:59:49 +02002875 first_antenna(priv->nvm_data->valid_tx_ant);
Wey-Yi Guy3a23d692010-04-03 16:44:39 -07002876 lq_sta->lq.general_params.dual_stream_ant_msk =
Eytan Lifshitzb7998c82012-12-01 20:59:49 +02002877 priv->nvm_data->valid_tx_ant &
2878 ~first_antenna(priv->nvm_data->valid_tx_ant);
Wey-Yi Guy3a23d692010-04-03 16:44:39 -07002879 if (!lq_sta->lq.general_params.dual_stream_ant_msk) {
2880 lq_sta->lq.general_params.dual_stream_ant_msk = ANT_AB;
Eytan Lifshitzb7998c82012-12-01 20:59:49 +02002881 } else if (num_of_ant(priv->nvm_data->valid_tx_ant) == 2) {
Wey-Yi Guy3a23d692010-04-03 16:44:39 -07002882 lq_sta->lq.general_params.dual_stream_ant_msk =
Eytan Lifshitzb7998c82012-12-01 20:59:49 +02002883 priv->nvm_data->valid_tx_ant;
Wey-Yi Guy3a23d692010-04-03 16:44:39 -07002884 }
Guy Cohen07bc28e2008-04-23 17:15:03 -07002885
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002886 /* as default allow aggregation for all tids */
2887 lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002888 lq_sta->drv = priv;
Zhu Yib481de92007-09-25 17:54:57 -07002889
Daniel C Halperin50273092009-08-28 09:44:45 -07002890 /* Set last_txrate_idx to lowest rate */
2891 lq_sta->last_txrate_idx = rate_lowest_index(sband, sta);
2892 if (sband->band == IEEE80211_BAND_5GHZ)
2893 lq_sta->last_txrate_idx += IWL_FIRST_OFDM_RATE;
Daniel C Halperine3949d62009-09-17 10:43:50 -07002894 lq_sta->is_agg = 0;
Sedat Dilek7d5f01a2010-12-16 12:46:23 +01002895#ifdef CONFIG_MAC80211_DEBUGFS
Shanyu Zhaoa1da0772010-12-02 11:02:54 -08002896 lq_sta->dbg_fixed_rate = 0;
Sedat Dilek7d5f01a2010-12-16 12:46:23 +01002897#endif
Shanyu Zhaoa1da0772010-12-02 11:02:54 -08002898
Johannes Berg9b6ca442012-03-07 09:52:11 -08002899 rs_initialize_lq(priv, sta, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002900}
2901
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002902static void rs_fill_link_cmd(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08002903 struct iwl_lq_sta *lq_sta, u32 new_rate)
Zhu Yib481de92007-09-25 17:54:57 -07002904{
Tomas Winklere227cea2008-07-18 13:53:05 +08002905 struct iwl_scale_tbl_info tbl_type;
Zhu Yib481de92007-09-25 17:54:57 -07002906 int index = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002907 int rate_idx;
Zhu Yi1b696de2007-09-27 11:27:41 +08002908 int repeat_rate = 0;
Guy Cohenaade00c2008-04-23 17:14:59 -07002909 u8 ant_toggle_cnt = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002910 u8 use_ht_possible = 1;
Guy Cohenaade00c2008-04-23 17:14:59 -07002911 u8 valid_tx_ant = 0;
Johannes Berg7b090682011-01-19 02:53:54 -08002912 struct iwl_station_priv *sta_priv =
2913 container_of(lq_sta, struct iwl_station_priv, lq_sta);
Guy Cohen07bc28e2008-04-23 17:15:03 -07002914 struct iwl_link_quality_cmd *lq_cmd = &lq_sta->lq;
Zhu Yib481de92007-09-25 17:54:57 -07002915
Ben Cahill77626352007-11-29 11:09:44 +08002916 /* Override starting rate (index 0) if needed for debug purposes */
Guy Cohen39e88502008-04-23 17:14:57 -07002917 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Zhu Yi98d7e092007-09-27 11:27:42 +08002918
Guy Cohen39e88502008-04-23 17:14:57 -07002919 /* Interpret new_rate (rate_n_flags) */
2920 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band,
Zhu Yib481de92007-09-25 17:54:57 -07002921 &tbl_type, &rate_idx);
2922
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07002923 if (priv && priv->bt_full_concurrent) {
2924 /* 1x1 only */
2925 tbl_type.ant_type =
Eytan Lifshitzb7998c82012-12-01 20:59:49 +02002926 first_antenna(priv->nvm_data->valid_tx_ant);
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07002927 }
2928
Ben Cahill77626352007-11-29 11:09:44 +08002929 /* How many times should we repeat the initial rate? */
Zhu Yib481de92007-09-25 17:54:57 -07002930 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07002931 ant_toggle_cnt = 1;
Zhu Yi1b696de2007-09-27 11:27:41 +08002932 repeat_rate = IWL_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002933 } else {
Daniel Halperin374920c2011-03-16 19:16:36 -07002934 repeat_rate = min(IWL_HT_NUMBER_TRY,
2935 LINK_QUAL_AGG_DISABLE_START_DEF - 1);
Guy Cohenaade00c2008-04-23 17:14:59 -07002936 }
Zhu Yib481de92007-09-25 17:54:57 -07002937
2938 lq_cmd->general_params.mimo_delimiter =
2939 is_mimo(tbl_type.lq_type) ? 1 : 0;
Ben Cahill77626352007-11-29 11:09:44 +08002940
2941 /* Fill 1st table entry (index 0) */
Guy Cohen39e88502008-04-23 17:14:57 -07002942 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002943
Guy Cohen07bc28e2008-04-23 17:15:03 -07002944 if (num_of_ant(tbl_type.ant_type) == 1) {
2945 lq_cmd->general_params.single_stream_ant_msk =
2946 tbl_type.ant_type;
2947 } else if (num_of_ant(tbl_type.ant_type) == 2) {
2948 lq_cmd->general_params.dual_stream_ant_msk =
2949 tbl_type.ant_type;
2950 } /* otherwise we don't modify the existing value */
Zhu Yib481de92007-09-25 17:54:57 -07002951
2952 index++;
Zhu Yi1b696de2007-09-27 11:27:41 +08002953 repeat_rate--;
Wey-Yi Guybee008b2010-08-23 07:57:04 -07002954 if (priv) {
2955 if (priv->bt_full_concurrent)
2956 valid_tx_ant = ANT_A;
2957 else
Eytan Lifshitzb7998c82012-12-01 20:59:49 +02002958 valid_tx_ant = priv->nvm_data->valid_tx_ant;
Wey-Yi Guybee008b2010-08-23 07:57:04 -07002959 }
Guy Cohenaade00c2008-04-23 17:14:59 -07002960
Ben Cahill77626352007-11-29 11:09:44 +08002961 /* Fill rest of rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002962 while (index < LINK_QUAL_MAX_RETRY_NUM) {
Ben Cahill77626352007-11-29 11:09:44 +08002963 /* Repeat initial/next rate.
2964 * For legacy IWL_NUMBER_TRY == 1, this loop will not execute.
2965 * For HT IWL_HT_NUMBER_TRY == 3, this executes twice. */
Zhu Yi1b696de2007-09-27 11:27:41 +08002966 while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
Zhu Yib481de92007-09-25 17:54:57 -07002967 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07002968 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2969 ant_toggle_cnt++;
2970 else if (priv &&
2971 rs_toggle_antenna(valid_tx_ant,
2972 &new_rate, &tbl_type))
2973 ant_toggle_cnt = 1;
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07002974 }
Zhu Yi98d7e092007-09-27 11:27:42 +08002975
Ben Cahill77626352007-11-29 11:09:44 +08002976 /* Override next rate if needed for debug purposes */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002977 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Ben Cahill77626352007-11-29 11:09:44 +08002978
2979 /* Fill next table entry */
Zhu Yib481de92007-09-25 17:54:57 -07002980 lq_cmd->rs_table[index].rate_n_flags =
Guy Cohen39e88502008-04-23 17:14:57 -07002981 cpu_to_le32(new_rate);
Zhu Yi1b696de2007-09-27 11:27:41 +08002982 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002983 index++;
2984 }
2985
Guy Cohen39e88502008-04-23 17:14:57 -07002986 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band, &tbl_type,
Zhu Yib481de92007-09-25 17:54:57 -07002987 &rate_idx);
2988
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07002989 if (priv && priv->bt_full_concurrent) {
2990 /* 1x1 only */
2991 tbl_type.ant_type =
Eytan Lifshitzb7998c82012-12-01 20:59:49 +02002992 first_antenna(priv->nvm_data->valid_tx_ant);
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07002993 }
2994
Ben Cahill77626352007-11-29 11:09:44 +08002995 /* Indicate to uCode which entries might be MIMO.
2996 * If initial rate was MIMO, this will finally end up
2997 * as (IWL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
Zhu Yib481de92007-09-25 17:54:57 -07002998 if (is_mimo(tbl_type.lq_type))
2999 lq_cmd->general_params.mimo_delimiter = index;
3000
Ben Cahill77626352007-11-29 11:09:44 +08003001 /* Get next rate */
Guy Cohen39e88502008-04-23 17:14:57 -07003002 new_rate = rs_get_lower_rate(lq_sta, &tbl_type, rate_idx,
3003 use_ht_possible);
Zhu Yib481de92007-09-25 17:54:57 -07003004
Ben Cahill77626352007-11-29 11:09:44 +08003005 /* How many times should we repeat the next rate? */
Zhu Yib481de92007-09-25 17:54:57 -07003006 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07003007 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
3008 ant_toggle_cnt++;
3009 else if (priv &&
3010 rs_toggle_antenna(valid_tx_ant,
3011 &new_rate, &tbl_type))
3012 ant_toggle_cnt = 1;
3013
Zhu Yi1b696de2007-09-27 11:27:41 +08003014 repeat_rate = IWL_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07003015 } else {
Zhu Yi1b696de2007-09-27 11:27:41 +08003016 repeat_rate = IWL_HT_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07003017 }
Zhu Yib481de92007-09-25 17:54:57 -07003018
Ben Cahill77626352007-11-29 11:09:44 +08003019 /* Don't allow HT rates after next pass.
3020 * rs_get_lower_rate() will change type to LQ_A or LQ_G. */
Zhu Yib481de92007-09-25 17:54:57 -07003021 use_ht_possible = 0;
3022
Ben Cahill77626352007-11-29 11:09:44 +08003023 /* Override next rate if needed for debug purposes */
Tomas Winklerc33104f2008-01-14 17:46:21 -08003024 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Ben Cahill77626352007-11-29 11:09:44 +08003025
3026 /* Fill next table entry */
Guy Cohen39e88502008-04-23 17:14:57 -07003027 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
Zhu Yib481de92007-09-25 17:54:57 -07003028
3029 index++;
Zhu Yi1b696de2007-09-27 11:27:41 +08003030 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07003031 }
3032
Johannes Berg7b090682011-01-19 02:53:54 -08003033 lq_cmd->agg_params.agg_frame_cnt_limit =
3034 sta_priv->max_agg_bufsize ?: LINK_QUAL_AGG_FRAME_LIMIT_DEF;
Wey-Yi Guy13c33a02009-06-03 11:44:11 -07003035 lq_cmd->agg_params.agg_dis_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
Wey-Yi Guy74697012010-07-28 09:18:54 -07003036
Wey-Yi Guy13c33a02009-06-03 11:44:11 -07003037 lq_cmd->agg_params.agg_time_limit =
3038 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
Wey-Yi Guy74697012010-07-28 09:18:54 -07003039 /*
3040 * overwrite if needed, pass aggregation time limit
3041 * to uCode in uSec
3042 */
Johannes Berg0d8877a2013-05-17 10:36:29 +02003043 if (priv && priv->lib->bt_params &&
3044 priv->lib->bt_params->agg_time_limit &&
Wey-Yi Guy66e863a52010-11-08 14:54:37 -08003045 priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH)
Wey-Yi Guy74697012010-07-28 09:18:54 -07003046 lq_cmd->agg_params.agg_time_limit =
Johannes Berg0d8877a2013-05-17 10:36:29 +02003047 cpu_to_le16(priv->lib->bt_params->agg_time_limit);
Zhu Yib481de92007-09-25 17:54:57 -07003048}
3049
Johannes Berg4b7679a2008-09-18 18:14:18 +02003050static void *rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
Zhu Yib481de92007-09-25 17:54:57 -07003051{
Johannes Berg4b7679a2008-09-18 18:14:18 +02003052 return hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003053}
3054/* rate scale requires free function to be implemented */
3055static void rs_free(void *priv_rate)
3056{
3057 return;
3058}
3059
Johannes Berg4b7679a2008-09-18 18:14:18 +02003060static void rs_free_sta(void *priv_r, struct ieee80211_sta *sta,
3061 void *priv_sta)
Zhu Yib481de92007-09-25 17:54:57 -07003062{
Emmanuel Grumbachd0f76d62012-02-09 16:08:15 +02003063 struct iwl_op_mode *op_mode __maybe_unused = priv_r;
3064 struct iwl_priv *priv __maybe_unused = IWL_OP_MODE_GET_DVM(op_mode);
Zhu Yib481de92007-09-25 17:54:57 -07003065
Tomas Winklere1623442009-01-27 14:27:56 -08003066 IWL_DEBUG_RATE(priv, "enter\n");
Tomas Winklere1623442009-01-27 14:27:56 -08003067 IWL_DEBUG_RATE(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003068}
3069
Zhu Yi93dc6462007-09-27 11:27:37 +08003070#ifdef CONFIG_MAC80211_DEBUGFS
Tomas Winklere227cea2008-07-18 13:53:05 +08003071static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
3072 u32 *rate_n_flags, int index)
Zhu Yi98d7e092007-09-27 11:27:42 +08003073{
Ester Kummerbf403db2008-05-05 10:22:40 +08003074 struct iwl_priv *priv;
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07003075 u8 valid_tx_ant;
3076 u8 ant_sel_tx;
Ester Kummerbf403db2008-05-05 10:22:40 +08003077
3078 priv = lq_sta->drv;
Eytan Lifshitzb7998c82012-12-01 20:59:49 +02003079 valid_tx_ant = priv->nvm_data->valid_tx_ant;
Guy Cohen39e88502008-04-23 17:14:57 -07003080 if (lq_sta->dbg_fixed_rate) {
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07003081 ant_sel_tx =
3082 ((lq_sta->dbg_fixed_rate & RATE_MCS_ANT_ABC_MSK)
3083 >> RATE_MCS_ANT_POS);
3084 if ((valid_tx_ant & ant_sel_tx) == ant_sel_tx) {
Guy Cohen39e88502008-04-23 17:14:57 -07003085 *rate_n_flags = lq_sta->dbg_fixed_rate;
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07003086 IWL_DEBUG_RATE(priv, "Fixed rate ON\n");
Guy Cohen39e88502008-04-23 17:14:57 -07003087 } else {
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07003088 lq_sta->dbg_fixed_rate = 0;
3089 IWL_ERR(priv,
3090 "Invalid antenna selection 0x%X, Valid is 0x%X\n",
3091 ant_sel_tx, valid_tx_ant);
3092 IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
Guy Cohen39e88502008-04-23 17:14:57 -07003093 }
Guy Cohen39e88502008-04-23 17:14:57 -07003094 } else {
Tomas Winklere1623442009-01-27 14:27:56 -08003095 IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
Zhu Yi98d7e092007-09-27 11:27:42 +08003096 }
Zhu Yi98d7e092007-09-27 11:27:42 +08003097}
3098
3099static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
3100 const char __user *user_buf, size_t count, loff_t *ppos)
3101{
Tomas Winklere227cea2008-07-18 13:53:05 +08003102 struct iwl_lq_sta *lq_sta = file->private_data;
Ester Kummerbf403db2008-05-05 10:22:40 +08003103 struct iwl_priv *priv;
Zhu Yi98d7e092007-09-27 11:27:42 +08003104 char buf[64];
Stephen Boyd805d7d22011-05-12 16:50:05 -07003105 size_t buf_size;
Zhu Yi98d7e092007-09-27 11:27:42 +08003106 u32 parsed_rate;
Wey-Yi Guy64898542011-05-03 18:05:13 -07003107
Zhu Yi98d7e092007-09-27 11:27:42 +08003108
Ester Kummerbf403db2008-05-05 10:22:40 +08003109 priv = lq_sta->drv;
Zhu Yi98d7e092007-09-27 11:27:42 +08003110 memset(buf, 0, sizeof(buf));
3111 buf_size = min(count, sizeof(buf) - 1);
3112 if (copy_from_user(buf, user_buf, buf_size))
3113 return -EFAULT;
3114
3115 if (sscanf(buf, "%x", &parsed_rate) == 1)
Wey-Yi Guy4e308112011-07-08 08:46:28 -07003116 lq_sta->dbg_fixed_rate = parsed_rate;
Zhu Yi98d7e092007-09-27 11:27:42 +08003117 else
Wey-Yi Guy4e308112011-07-08 08:46:28 -07003118 lq_sta->dbg_fixed_rate = 0;
Zhu Yi98d7e092007-09-27 11:27:42 +08003119
Wey-Yi Guy64898542011-05-03 18:05:13 -07003120 rs_program_fix_rate(priv, lq_sta);
Zhu Yi98d7e092007-09-27 11:27:42 +08003121
3122 return count;
3123}
Zhu Yi0209dc12007-09-27 11:27:43 +08003124
Zhu Yi5ae212c2007-09-27 11:27:38 +08003125static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
3126 char __user *user_buf, size_t count, loff_t *ppos)
3127{
Frank Seidela412c802009-03-01 20:25:38 +01003128 char *buff;
Zhu Yi5ae212c2007-09-27 11:27:38 +08003129 int desc = 0;
3130 int i = 0;
Wey-Yi Guy12b96812009-04-08 11:39:27 -07003131 int index = 0;
Frank Seidela412c802009-03-01 20:25:38 +01003132 ssize_t ret;
Zhu Yi5ae212c2007-09-27 11:27:38 +08003133
Tomas Winklere227cea2008-07-18 13:53:05 +08003134 struct iwl_lq_sta *lq_sta = file->private_data;
Wey-Yi Guyd8ae4f52009-03-10 14:35:07 -07003135 struct iwl_priv *priv;
Wey-Yi Guyadb7b5e2009-03-10 14:35:08 -07003136 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
Zhu Yi5ae212c2007-09-27 11:27:38 +08003137
Wey-Yi Guyd8ae4f52009-03-10 14:35:07 -07003138 priv = lq_sta->drv;
Frank Seidela412c802009-03-01 20:25:38 +01003139 buff = kmalloc(1024, GFP_KERNEL);
3140 if (!buff)
3141 return -ENOMEM;
3142
Tomas Winklerc33104f2008-01-14 17:46:21 -08003143 desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
Zhu Yi98d7e092007-09-27 11:27:42 +08003144 desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08003145 lq_sta->total_failed, lq_sta->total_success,
Guy Cohen39e88502008-04-23 17:14:57 -07003146 lq_sta->active_legacy_rate);
Zhu Yi98d7e092007-09-27 11:27:42 +08003147 desc += sprintf(buff+desc, "fixed rate 0x%X\n",
Wey-Yi Guy4e308112011-07-08 08:46:28 -07003148 lq_sta->dbg_fixed_rate);
Wey-Yi Guyd8ae4f52009-03-10 14:35:07 -07003149 desc += sprintf(buff+desc, "valid_tx_ant %s%s%s\n",
Eytan Lifshitzb7998c82012-12-01 20:59:49 +02003150 (priv->nvm_data->valid_tx_ant & ANT_A) ? "ANT_A," : "",
3151 (priv->nvm_data->valid_tx_ant & ANT_B) ? "ANT_B," : "",
3152 (priv->nvm_data->valid_tx_ant & ANT_C) ? "ANT_C" : "");
Wey-Yi Guyadb7b5e2009-03-10 14:35:08 -07003153 desc += sprintf(buff+desc, "lq type %s\n",
3154 (is_legacy(tbl->lq_type)) ? "legacy" : "HT");
3155 if (is_Ht(tbl->lq_type)) {
Emmanuel Grumbache70fe7e2015-03-01 17:18:00 +02003156 desc += sprintf(buff + desc, " %s",
Wey-Yi Guyadb7b5e2009-03-10 14:35:08 -07003157 (is_siso(tbl->lq_type)) ? "SISO" :
3158 ((is_mimo2(tbl->lq_type)) ? "MIMO2" : "MIMO3"));
Emmanuel Grumbache70fe7e2015-03-01 17:18:00 +02003159 desc += sprintf(buff + desc, " %s",
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07003160 (tbl->is_ht40) ? "40MHz" : "20MHz");
Emmanuel Grumbache70fe7e2015-03-01 17:18:00 +02003161 desc += sprintf(buff + desc, " %s %s %s\n",
3162 (tbl->is_SGI) ? "SGI" : "",
Daniel C Halperine3949d62009-09-17 10:43:50 -07003163 (lq_sta->is_green) ? "GF enabled" : "",
3164 (lq_sta->is_agg) ? "AGG on" : "");
Wey-Yi Guyadb7b5e2009-03-10 14:35:08 -07003165 }
Wey-Yi Guy12b96812009-04-08 11:39:27 -07003166 desc += sprintf(buff+desc, "last tx rate=0x%X\n",
3167 lq_sta->last_rate_n_flags);
Zhu Yi5ae212c2007-09-27 11:27:38 +08003168 desc += sprintf(buff+desc, "general:"
3169 "flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08003170 lq_sta->lq.general_params.flags,
3171 lq_sta->lq.general_params.mimo_delimiter,
3172 lq_sta->lq.general_params.single_stream_ant_msk,
3173 lq_sta->lq.general_params.dual_stream_ant_msk);
Zhu Yi5ae212c2007-09-27 11:27:38 +08003174
3175 desc += sprintf(buff+desc, "agg:"
3176 "time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08003177 le16_to_cpu(lq_sta->lq.agg_params.agg_time_limit),
3178 lq_sta->lq.agg_params.agg_dis_start_th,
3179 lq_sta->lq.agg_params.agg_frame_cnt_limit);
Zhu Yi5ae212c2007-09-27 11:27:38 +08003180
3181 desc += sprintf(buff+desc,
3182 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08003183 lq_sta->lq.general_params.start_rate_index[0],
3184 lq_sta->lq.general_params.start_rate_index[1],
3185 lq_sta->lq.general_params.start_rate_index[2],
3186 lq_sta->lq.general_params.start_rate_index[3]);
Zhu Yi5ae212c2007-09-27 11:27:38 +08003187
Wey-Yi Guy12b96812009-04-08 11:39:27 -07003188 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
3189 index = iwl_hwrate_to_plcp_idx(
3190 le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags));
3191 if (is_legacy(tbl->lq_type)) {
3192 desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps\n",
3193 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags),
3194 iwl_rate_mcs[index].mbps);
3195 } else {
3196 desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps (%s)\n",
3197 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags),
3198 iwl_rate_mcs[index].mbps, iwl_rate_mcs[index].mcs);
3199 }
3200 }
Zhu Yi5ae212c2007-09-27 11:27:38 +08003201
Frank Seidela412c802009-03-01 20:25:38 +01003202 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
3203 kfree(buff);
3204 return ret;
Zhu Yi5ae212c2007-09-27 11:27:38 +08003205}
3206
3207static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
Zhu Yi98d7e092007-09-27 11:27:42 +08003208 .write = rs_sta_dbgfs_scale_table_write,
Zhu Yi5ae212c2007-09-27 11:27:38 +08003209 .read = rs_sta_dbgfs_scale_table_read,
Stephen Boyd234e3402012-04-05 14:25:11 -07003210 .open = simple_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003211 .llseek = default_llseek,
Zhu Yi5ae212c2007-09-27 11:27:38 +08003212};
Zhu Yi0209dc12007-09-27 11:27:43 +08003213static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
3214 char __user *user_buf, size_t count, loff_t *ppos)
3215{
Frank Seidela412c802009-03-01 20:25:38 +01003216 char *buff;
Zhu Yi0209dc12007-09-27 11:27:43 +08003217 int desc = 0;
3218 int i, j;
Frank Seidela412c802009-03-01 20:25:38 +01003219 ssize_t ret;
Zhu Yi0209dc12007-09-27 11:27:43 +08003220
Tomas Winklere227cea2008-07-18 13:53:05 +08003221 struct iwl_lq_sta *lq_sta = file->private_data;
Frank Seidela412c802009-03-01 20:25:38 +01003222
3223 buff = kmalloc(1024, GFP_KERNEL);
3224 if (!buff)
3225 return -ENOMEM;
3226
Zhu Yi0209dc12007-09-27 11:27:43 +08003227 for (i = 0; i < LQ_SIZE; i++) {
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07003228 desc += sprintf(buff+desc,
3229 "%s type=%d SGI=%d HT40=%d DUP=%d GF=%d\n"
Zhu Yi0209dc12007-09-27 11:27:43 +08003230 "rate=0x%X\n",
Abhijeet Kolekarc3056062008-11-12 13:14:08 -08003231 lq_sta->active_tbl == i ? "*" : "x",
Tomas Winklerc33104f2008-01-14 17:46:21 -08003232 lq_sta->lq_info[i].lq_type,
3233 lq_sta->lq_info[i].is_SGI,
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07003234 lq_sta->lq_info[i].is_ht40,
Tomas Winklerc33104f2008-01-14 17:46:21 -08003235 lq_sta->lq_info[i].is_dup,
Wey-Yi Guy2681b202009-05-21 13:44:22 -07003236 lq_sta->is_green,
Guy Cohen39e88502008-04-23 17:14:57 -07003237 lq_sta->lq_info[i].current_rate);
Zhu Yi0209dc12007-09-27 11:27:43 +08003238 for (j = 0; j < IWL_RATE_COUNT; j++) {
3239 desc += sprintf(buff+desc,
Tomas Winklerc33104f2008-01-14 17:46:21 -08003240 "counter=%d success=%d %%=%d\n",
3241 lq_sta->lq_info[i].win[j].counter,
3242 lq_sta->lq_info[i].win[j].success_counter,
3243 lq_sta->lq_info[i].win[j].success_ratio);
Zhu Yi0209dc12007-09-27 11:27:43 +08003244 }
3245 }
Frank Seidela412c802009-03-01 20:25:38 +01003246 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
3247 kfree(buff);
3248 return ret;
Zhu Yi0209dc12007-09-27 11:27:43 +08003249}
3250
3251static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
3252 .read = rs_sta_dbgfs_stats_table_read,
Stephen Boyd234e3402012-04-05 14:25:11 -07003253 .open = simple_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003254 .llseek = default_llseek,
Zhu Yi0209dc12007-09-27 11:27:43 +08003255};
Zhu Yi5ae212c2007-09-27 11:27:38 +08003256
Wey-Yi Guy38167452009-05-08 13:44:43 -07003257static ssize_t rs_sta_dbgfs_rate_scale_data_read(struct file *file,
3258 char __user *user_buf, size_t count, loff_t *ppos)
3259{
Johannes Berg70817b52011-05-11 03:29:25 -07003260 struct iwl_lq_sta *lq_sta = file->private_data;
3261 struct iwl_scale_tbl_info *tbl = &lq_sta->lq_info[lq_sta->active_tbl];
Wey-Yi Guy38167452009-05-08 13:44:43 -07003262 char buff[120];
3263 int desc = 0;
Wey-Yi Guy38167452009-05-08 13:44:43 -07003264
Wey-Yi Guy38167452009-05-08 13:44:43 -07003265 if (is_Ht(tbl->lq_type))
3266 desc += sprintf(buff+desc,
3267 "Bit Rate= %d Mb/s\n",
3268 tbl->expected_tpt[lq_sta->last_txrate_idx]);
3269 else
3270 desc += sprintf(buff+desc,
3271 "Bit Rate= %d Mb/s\n",
3272 iwl_rates[lq_sta->last_txrate_idx].ieee >> 1);
Wey-Yi Guy38167452009-05-08 13:44:43 -07003273
Johannes Berg08960de2011-04-05 09:41:53 -07003274 return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
Wey-Yi Guy38167452009-05-08 13:44:43 -07003275}
3276
3277static const struct file_operations rs_sta_dbgfs_rate_scale_data_ops = {
3278 .read = rs_sta_dbgfs_rate_scale_data_read,
Stephen Boyd234e3402012-04-05 14:25:11 -07003279 .open = simple_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003280 .llseek = default_llseek,
Wey-Yi Guy38167452009-05-08 13:44:43 -07003281};
3282
Zhu Yi93dc6462007-09-27 11:27:37 +08003283static void rs_add_debugfs(void *priv, void *priv_sta,
3284 struct dentry *dir)
3285{
Tomas Winklere227cea2008-07-18 13:53:05 +08003286 struct iwl_lq_sta *lq_sta = priv_sta;
Tomas Winklerc33104f2008-01-14 17:46:21 -08003287 lq_sta->rs_sta_dbgfs_scale_table_file =
Wey-Yi Guy43e85112009-11-20 12:04:58 -08003288 debugfs_create_file("rate_scale_table", S_IRUSR | S_IWUSR, dir,
Tomas Winklerc33104f2008-01-14 17:46:21 -08003289 lq_sta, &rs_sta_dbgfs_scale_table_ops);
3290 lq_sta->rs_sta_dbgfs_stats_table_file =
Wey-Yi Guy43e85112009-11-20 12:04:58 -08003291 debugfs_create_file("rate_stats_table", S_IRUSR, dir,
Tomas Winklerc33104f2008-01-14 17:46:21 -08003292 lq_sta, &rs_sta_dbgfs_stats_table_ops);
Wey-Yi Guy38167452009-05-08 13:44:43 -07003293 lq_sta->rs_sta_dbgfs_rate_scale_data_file =
Wey-Yi Guy43e85112009-11-20 12:04:58 -08003294 debugfs_create_file("rate_scale_data", S_IRUSR, dir,
Wey-Yi Guy38167452009-05-08 13:44:43 -07003295 lq_sta, &rs_sta_dbgfs_rate_scale_data_ops);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02003296 lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file =
Wey-Yi Guy43e85112009-11-20 12:04:58 -08003297 debugfs_create_u8("tx_agg_tid_enable", S_IRUSR | S_IWUSR, dir,
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02003298 &lq_sta->tx_agg_tid_en);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02003299
Zhu Yi93dc6462007-09-27 11:27:37 +08003300}
3301
3302static void rs_remove_debugfs(void *priv, void *priv_sta)
3303{
Tomas Winklere227cea2008-07-18 13:53:05 +08003304 struct iwl_lq_sta *lq_sta = priv_sta;
Tomas Winklerc33104f2008-01-14 17:46:21 -08003305 debugfs_remove(lq_sta->rs_sta_dbgfs_scale_table_file);
3306 debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
Wey-Yi Guy38167452009-05-08 13:44:43 -07003307 debugfs_remove(lq_sta->rs_sta_dbgfs_rate_scale_data_file);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02003308 debugfs_remove(lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file);
Zhu Yi93dc6462007-09-27 11:27:37 +08003309}
3310#endif
3311
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003312/*
3313 * Initialization of rate scaling information is done by driver after
3314 * the station is added. Since mac80211 calls this function before a
3315 * station is added we ignore it.
3316 */
3317static void rs_rate_init_stub(void *priv_r, struct ieee80211_supported_band *sband,
Simon Wunderlich3de805c2013-07-08 16:55:50 +02003318 struct cfg80211_chan_def *chandef,
3319 struct ieee80211_sta *sta, void *priv_sta)
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003320{
3321}
Johannes Berg631ad702014-01-20 23:29:34 +01003322
3323static const struct rate_control_ops rs_ops = {
Zhu Yib481de92007-09-25 17:54:57 -07003324 .name = RS_NAME,
3325 .tx_status = rs_tx_status,
3326 .get_rate = rs_get_rate,
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003327 .rate_init = rs_rate_init_stub,
Zhu Yib481de92007-09-25 17:54:57 -07003328 .alloc = rs_alloc,
3329 .free = rs_free,
3330 .alloc_sta = rs_alloc_sta,
3331 .free_sta = rs_free_sta,
Zhu Yi93dc6462007-09-27 11:27:37 +08003332#ifdef CONFIG_MAC80211_DEBUGFS
3333 .add_sta_debugfs = rs_add_debugfs,
3334 .remove_sta_debugfs = rs_remove_debugfs,
3335#endif
Zhu Yib481de92007-09-25 17:54:57 -07003336};
3337
Tomas Winklere227cea2008-07-18 13:53:05 +08003338int iwlagn_rate_control_register(void)
Zhu Yib481de92007-09-25 17:54:57 -07003339{
Reinette Chatre897e1cf2008-03-28 16:21:09 -07003340 return ieee80211_rate_control_register(&rs_ops);
Zhu Yib481de92007-09-25 17:54:57 -07003341}
3342
Tomas Winklere227cea2008-07-18 13:53:05 +08003343void iwlagn_rate_control_unregister(void)
Zhu Yib481de92007-09-25 17:54:57 -07003344{
3345 ieee80211_rate_control_unregister(&rs_ops);
3346}
3347