blob: a131227c49e970bfcbbd18b5a7f05f6af21b81f3 [file] [log] [blame]
Zhu Yib481de92007-09-25 17:54:57 -07001/******************************************************************************
2 *
Johannes Berg128e63e2013-01-21 21:39:26 +01003 * Copyright(c) 2005 - 2013 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>
27#include <linux/init.h>
28#include <linux/skbuff.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Zhu Yib481de92007-09-25 17:54:57 -070030#include <net/mac80211.h>
Zhu Yib481de92007-09-25 17:54:57 -070031
32#include <linux/netdevice.h>
33#include <linux/etherdevice.h>
34#include <linux/delay.h>
35
36#include <linux/workqueue.h>
37
Johannes Berg1023fdc2012-05-15 12:16:34 +020038#include "dev.h"
39#include "agn.h"
Zhu Yib481de92007-09-25 17:54:57 -070040
Tomas Winklere227cea2008-07-18 13:53:05 +080041#define RS_NAME "iwl-agn-rs"
Zhu Yib481de92007-09-25 17:54:57 -070042
Guy Cohenaade00c2008-04-23 17:14:59 -070043#define NUM_TRY_BEFORE_ANT_TOGGLE 1
Zhu Yib481de92007-09-25 17:54:57 -070044#define IWL_NUMBER_TRY 1
45#define IWL_HT_NUMBER_TRY 3
46
Ben Cahill77626352007-11-29 11:09:44 +080047#define IWL_RATE_MAX_WINDOW 62 /* # tx in history window */
48#define IWL_RATE_MIN_FAILURE_TH 6 /* min failures to calc tpt */
49#define IWL_RATE_MIN_SUCCESS_TH 8 /* min successes to calc tpt */
50
Abbas, Mohamed7d049e52009-01-20 21:33:53 -080051/* max allowed rate miss before sync LQ cmd */
52#define IWL_MISSED_RATE_MAX 15
Ben Cahill77626352007-11-29 11:09:44 +080053/* max time to accum history 2 seconds */
Mohamed Abbas447fee72009-04-20 14:37:02 -070054#define IWL_RATE_SCALE_FLUSH_INTVL (3*HZ)
Zhu Yib481de92007-09-25 17:54:57 -070055
56static u8 rs_ht_to_legacy[] = {
57 IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
58 IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
59 IWL_RATE_6M_INDEX,
60 IWL_RATE_6M_INDEX, IWL_RATE_9M_INDEX,
61 IWL_RATE_12M_INDEX, IWL_RATE_18M_INDEX,
62 IWL_RATE_24M_INDEX, IWL_RATE_36M_INDEX,
63 IWL_RATE_48M_INDEX, IWL_RATE_54M_INDEX
64};
65
Guy Cohenaade00c2008-04-23 17:14:59 -070066static const u8 ant_toggle_lookup[] = {
67 /*ANT_NONE -> */ ANT_NONE,
68 /*ANT_A -> */ ANT_B,
69 /*ANT_B -> */ ANT_C,
70 /*ANT_AB -> */ ANT_BC,
71 /*ANT_C -> */ ANT_A,
72 /*ANT_AC -> */ ANT_AB,
73 /*ANT_BC -> */ ANT_AC,
74 /*ANT_ABC -> */ ANT_ABC,
75};
76
Johannes Berg635b85b2010-09-22 18:02:04 +020077#define IWL_DECLARE_RATE_INFO(r, s, ip, in, rp, rn, pp, np) \
78 [IWL_RATE_##r##M_INDEX] = { IWL_RATE_##r##M_PLCP, \
79 IWL_RATE_SISO_##s##M_PLCP, \
80 IWL_RATE_MIMO2_##s##M_PLCP,\
81 IWL_RATE_MIMO3_##s##M_PLCP,\
82 IWL_RATE_##r##M_IEEE, \
83 IWL_RATE_##ip##M_INDEX, \
84 IWL_RATE_##in##M_INDEX, \
85 IWL_RATE_##rp##M_INDEX, \
86 IWL_RATE_##rn##M_INDEX, \
87 IWL_RATE_##pp##M_INDEX, \
88 IWL_RATE_##np##M_INDEX }
89
90/*
91 * Parameter order:
92 * rate, ht rate, prev rate, next rate, prev tgg rate, next tgg rate
93 *
94 * If there isn't a valid next or previous rate then INV is used which
95 * maps to IWL_RATE_INVALID
96 *
97 */
98const struct iwl_rate_info iwl_rates[IWL_RATE_COUNT] = {
99 IWL_DECLARE_RATE_INFO(1, INV, INV, 2, INV, 2, INV, 2), /* 1mbps */
100 IWL_DECLARE_RATE_INFO(2, INV, 1, 5, 1, 5, 1, 5), /* 2mbps */
101 IWL_DECLARE_RATE_INFO(5, INV, 2, 6, 2, 11, 2, 11), /*5.5mbps */
102 IWL_DECLARE_RATE_INFO(11, INV, 9, 12, 9, 12, 5, 18), /* 11mbps */
103 IWL_DECLARE_RATE_INFO(6, 6, 5, 9, 5, 11, 5, 11), /* 6mbps */
104 IWL_DECLARE_RATE_INFO(9, 6, 6, 11, 6, 11, 5, 11), /* 9mbps */
105 IWL_DECLARE_RATE_INFO(12, 12, 11, 18, 11, 18, 11, 18), /* 12mbps */
106 IWL_DECLARE_RATE_INFO(18, 18, 12, 24, 12, 24, 11, 24), /* 18mbps */
107 IWL_DECLARE_RATE_INFO(24, 24, 18, 36, 18, 36, 18, 36), /* 24mbps */
108 IWL_DECLARE_RATE_INFO(36, 36, 24, 48, 24, 48, 24, 48), /* 36mbps */
109 IWL_DECLARE_RATE_INFO(48, 48, 36, 54, 36, 54, 36, 54), /* 48mbps */
110 IWL_DECLARE_RATE_INFO(54, 54, 48, INV, 48, INV, 48, INV),/* 54mbps */
111 IWL_DECLARE_RATE_INFO(60, 60, 48, INV, 48, INV, 48, INV),/* 60mbps */
112 /* FIXME:RS: ^^ should be INV (legacy) */
113};
114
Daniel Halperin25205462011-03-18 18:48:55 -0700115static inline u8 rs_extract_rate(u32 rate_n_flags)
116{
117 return (u8)(rate_n_flags & RATE_MCS_RATE_MSK);
118}
119
Johannes Berg635b85b2010-09-22 18:02:04 +0200120static int iwl_hwrate_to_plcp_idx(u32 rate_n_flags)
121{
122 int idx = 0;
123
124 /* HT rate format */
125 if (rate_n_flags & RATE_MCS_HT_MSK) {
Daniel Halperin25205462011-03-18 18:48:55 -0700126 idx = rs_extract_rate(rate_n_flags);
Johannes Berg635b85b2010-09-22 18:02:04 +0200127
128 if (idx >= IWL_RATE_MIMO3_6M_PLCP)
129 idx = idx - IWL_RATE_MIMO3_6M_PLCP;
130 else if (idx >= IWL_RATE_MIMO2_6M_PLCP)
131 idx = idx - IWL_RATE_MIMO2_6M_PLCP;
132
133 idx += IWL_FIRST_OFDM_RATE;
134 /* skip 9M not supported in ht*/
135 if (idx >= IWL_RATE_9M_INDEX)
136 idx += 1;
137 if ((idx >= IWL_FIRST_OFDM_RATE) && (idx <= IWL_LAST_OFDM_RATE))
138 return idx;
139
140 /* legacy rate format, search for match in table */
141 } else {
142 for (idx = 0; idx < ARRAY_SIZE(iwl_rates); idx++)
Daniel Halperin25205462011-03-18 18:48:55 -0700143 if (iwl_rates[idx].plcp ==
144 rs_extract_rate(rate_n_flags))
Johannes Berg635b85b2010-09-22 18:02:04 +0200145 return idx;
146 }
147
148 return -1;
149}
150
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700151static void rs_rate_scale_perform(struct iwl_priv *priv,
Gábor Stefanik514d65c2009-04-23 19:36:08 +0200152 struct sk_buff *skb,
Johannes Berg4b7679a2008-09-18 18:14:18 +0200153 struct ieee80211_sta *sta,
154 struct iwl_lq_sta *lq_sta);
Wey-Yi Guy46f93812009-07-24 11:13:03 -0700155static void rs_fill_link_cmd(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +0800156 struct iwl_lq_sta *lq_sta, u32 rate_n_flags);
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -0700157static void rs_stay_in_table(struct iwl_lq_sta *lq_sta, bool force_search);
Zhu Yib481de92007-09-25 17:54:57 -0700158
159
Zhu Yi98d7e092007-09-27 11:27:42 +0800160#ifdef CONFIG_MAC80211_DEBUGFS
Tomas Winklere227cea2008-07-18 13:53:05 +0800161static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
162 u32 *rate_n_flags, int index);
Zhu Yi98d7e092007-09-27 11:27:42 +0800163#else
Tomas Winklere227cea2008-07-18 13:53:05 +0800164static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
165 u32 *rate_n_flags, int index)
Zhu Yi98d7e092007-09-27 11:27:42 +0800166{}
167#endif
Ben Cahill77626352007-11-29 11:09:44 +0800168
Daniel C Halperine3949d62009-09-17 10:43:50 -0700169/**
170 * The following tables contain the expected throughput metrics for all rates
171 *
172 * 1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
173 *
174 * where invalid entries are zeros.
175 *
176 * CCK rates are only valid in legacy table and will only be used in G
177 * (2.4 GHz) band.
Ben Cahill77626352007-11-29 11:09:44 +0800178 */
Wey-Yi Guy584a0f02009-04-01 14:33:24 -0700179
Daniel C Halperine3949d62009-09-17 10:43:50 -0700180static s32 expected_tpt_legacy[IWL_RATE_COUNT] = {
181 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 0
Zhu Yib481de92007-09-25 17:54:57 -0700182};
183
Daniel C Halperine3949d62009-09-17 10:43:50 -0700184static s32 expected_tpt_siso20MHz[4][IWL_RATE_COUNT] = {
Wey-Yi Guy7fc11e92011-01-15 09:16:59 -0800185 {0, 0, 0, 0, 42, 0, 76, 102, 124, 159, 183, 193, 202}, /* Norm */
186 {0, 0, 0, 0, 46, 0, 82, 110, 132, 168, 192, 202, 210}, /* SGI */
187 {0, 0, 0, 0, 47, 0, 91, 133, 171, 242, 305, 334, 362}, /* AGG */
188 {0, 0, 0, 0, 52, 0, 101, 145, 187, 264, 330, 361, 390}, /* AGG+SGI */
Zhu Yib481de92007-09-25 17:54:57 -0700189};
190
Daniel C Halperine3949d62009-09-17 10:43:50 -0700191static s32 expected_tpt_siso40MHz[4][IWL_RATE_COUNT] = {
192 {0, 0, 0, 0, 77, 0, 127, 160, 184, 220, 242, 250, 257}, /* Norm */
193 {0, 0, 0, 0, 83, 0, 135, 169, 193, 229, 250, 257, 264}, /* SGI */
Wey-Yi Guy7fc11e92011-01-15 09:16:59 -0800194 {0, 0, 0, 0, 94, 0, 177, 249, 313, 423, 512, 550, 586}, /* AGG */
195 {0, 0, 0, 0, 104, 0, 193, 270, 338, 454, 545, 584, 620}, /* AGG+SGI */
Zhu Yib481de92007-09-25 17:54:57 -0700196};
197
Daniel C Halperine3949d62009-09-17 10:43:50 -0700198static s32 expected_tpt_mimo2_20MHz[4][IWL_RATE_COUNT] = {
Wey-Yi Guy7fc11e92011-01-15 09:16:59 -0800199 {0, 0, 0, 0, 74, 0, 123, 155, 179, 214, 236, 244, 251}, /* Norm */
200 {0, 0, 0, 0, 81, 0, 131, 164, 188, 223, 243, 251, 257}, /* SGI */
201 {0, 0, 0, 0, 89, 0, 167, 235, 296, 402, 488, 526, 560}, /* AGG */
202 {0, 0, 0, 0, 97, 0, 182, 255, 320, 431, 520, 558, 593}, /* AGG+SGI*/
Zhu Yib481de92007-09-25 17:54:57 -0700203};
204
Daniel C Halperine3949d62009-09-17 10:43:50 -0700205static s32 expected_tpt_mimo2_40MHz[4][IWL_RATE_COUNT] = {
206 {0, 0, 0, 0, 123, 0, 182, 214, 235, 264, 279, 285, 289}, /* Norm */
207 {0, 0, 0, 0, 131, 0, 191, 222, 242, 270, 284, 289, 293}, /* SGI */
Wey-Yi Guy7fc11e92011-01-15 09:16:59 -0800208 {0, 0, 0, 0, 171, 0, 305, 410, 496, 634, 731, 771, 805}, /* AGG */
209 {0, 0, 0, 0, 186, 0, 329, 439, 527, 667, 764, 803, 838}, /* AGG+SGI */
Zhu Yib481de92007-09-25 17:54:57 -0700210};
211
Daniel C Halperine3949d62009-09-17 10:43:50 -0700212static s32 expected_tpt_mimo3_20MHz[4][IWL_RATE_COUNT] = {
213 {0, 0, 0, 0, 99, 0, 153, 186, 208, 239, 256, 263, 268}, /* Norm */
214 {0, 0, 0, 0, 106, 0, 162, 194, 215, 246, 262, 268, 273}, /* SGI */
215 {0, 0, 0, 0, 134, 0, 249, 346, 431, 574, 685, 732, 775}, /* AGG */
216 {0, 0, 0, 0, 148, 0, 272, 376, 465, 614, 727, 775, 818}, /* AGG+SGI */
Zhu Yib481de92007-09-25 17:54:57 -0700217};
218
Daniel C Halperine3949d62009-09-17 10:43:50 -0700219static s32 expected_tpt_mimo3_40MHz[4][IWL_RATE_COUNT] = {
220 {0, 0, 0, 0, 152, 0, 211, 239, 255, 279, 290, 294, 297}, /* Norm */
221 {0, 0, 0, 0, 160, 0, 219, 245, 261, 284, 294, 297, 300}, /* SGI */
222 {0, 0, 0, 0, 254, 0, 443, 584, 695, 868, 984, 1030, 1070}, /* AGG */
223 {0, 0, 0, 0, 277, 0, 478, 624, 737, 911, 1026, 1070, 1109}, /* AGG+SGI */
Wey-Yi Guy584a0f02009-04-01 14:33:24 -0700224};
225
Wey-Yi Guy12b96812009-04-08 11:39:27 -0700226/* mbps, mcs */
Tobias Klauser79496732009-12-23 14:18:11 +0100227static const struct iwl_rate_mcs_info iwl_rate_mcs[IWL_RATE_COUNT] = {
Daniel C Halperine3949d62009-09-17 10:43:50 -0700228 { "1", "BPSK DSSS"},
229 { "2", "QPSK DSSS"},
230 {"5.5", "BPSK CCK"},
231 { "11", "QPSK CCK"},
232 { "6", "BPSK 1/2"},
233 { "9", "BPSK 1/2"},
234 { "12", "QPSK 1/2"},
235 { "18", "QPSK 3/4"},
236 { "24", "16QAM 1/2"},
237 { "36", "16QAM 3/4"},
238 { "48", "64QAM 2/3"},
239 { "54", "64QAM 3/4"},
240 { "60", "64QAM 5/6"},
Wey-Yi Guy12b96812009-04-08 11:39:27 -0700241};
242
Wey-Yi Guya9c146b2009-05-22 11:01:48 -0700243#define MCS_INDEX_PER_STREAM (8)
244
Tomas Winklere227cea2008-07-18 13:53:05 +0800245static void rs_rate_scale_clear_window(struct iwl_rate_scale_data *window)
Zhu Yib481de92007-09-25 17:54:57 -0700246{
247 window->data = 0;
248 window->success_counter = 0;
249 window->success_ratio = IWL_INVALID_VALUE;
250 window->counter = 0;
251 window->average_tpt = IWL_INVALID_VALUE;
252 window->stamp = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700253}
254
Guy Cohenaade00c2008-04-23 17:14:59 -0700255static inline u8 rs_is_valid_ant(u8 valid_antenna, u8 ant_type)
256{
Tomas Winkler3ac7f142008-07-21 02:40:14 +0300257 return (ant_type & valid_antenna) == ant_type;
Guy Cohenaade00c2008-04-23 17:14:59 -0700258}
259
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200260/*
261 * removes the old data from the statistics. All data that is older than
262 * TID_MAX_TIME_DIFF, will be deleted.
263 */
Tomas Winklere227cea2008-07-18 13:53:05 +0800264static void rs_tl_rm_old_stats(struct iwl_traffic_load *tl, u32 curr_time)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200265{
266 /* The oldest age we want to keep */
267 u32 oldest_time = curr_time - TID_MAX_TIME_DIFF;
268
269 while (tl->queue_count &&
270 (tl->time_stamp < oldest_time)) {
271 tl->total -= tl->packet_count[tl->head];
272 tl->packet_count[tl->head] = 0;
273 tl->time_stamp += TID_QUEUE_CELL_SPACING;
274 tl->queue_count--;
275 tl->head++;
276 if (tl->head >= TID_QUEUE_MAX_SIZE)
277 tl->head = 0;
278 }
279}
280
281/*
282 * increment traffic load value for tid and also remove
283 * any old values if passed the certain time period
284 */
Tomas Winklere227cea2008-07-18 13:53:05 +0800285static u8 rs_tl_add_packet(struct iwl_lq_sta *lq_data,
Ron Rindjunskyfaa29712008-06-12 09:46:58 +0800286 struct ieee80211_hdr *hdr)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200287{
288 u32 curr_time = jiffies_to_msecs(jiffies);
289 u32 time_diff;
290 s32 index;
Tomas Winklere227cea2008-07-18 13:53:05 +0800291 struct iwl_traffic_load *tl = NULL;
Tomas Winkler54dbb522008-05-15 13:54:06 +0800292 u8 tid;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200293
Tomas Winkler417f1142008-11-12 13:14:06 -0800294 if (ieee80211_is_data_qos(hdr->frame_control)) {
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700295 u8 *qc = ieee80211_get_qos_ctl(hdr);
Tomas Winkler54dbb522008-05-15 13:54:06 +0800296 tid = qc[0] & 0xf;
297 } else
Emmanuel Grumbach5f85a782011-08-25 23:11:18 -0700298 return IWL_MAX_TID_COUNT;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200299
Johannes Berga8448552011-12-02 12:25:12 -0800300 if (unlikely(tid >= IWL_MAX_TID_COUNT))
Emmanuel Grumbach5f85a782011-08-25 23:11:18 -0700301 return IWL_MAX_TID_COUNT;
Reinette Chatree6a6cf42009-08-13 13:30:50 -0700302
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200303 tl = &lq_data->load[tid];
304
305 curr_time -= curr_time % TID_ROUND_VALUE;
306
307 /* Happens only for the first packet. Initialize the data */
308 if (!(tl->queue_count)) {
309 tl->total = 1;
310 tl->time_stamp = curr_time;
311 tl->queue_count = 1;
312 tl->head = 0;
313 tl->packet_count[0] = 1;
Emmanuel Grumbach5f85a782011-08-25 23:11:18 -0700314 return IWL_MAX_TID_COUNT;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200315 }
316
317 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
318 index = time_diff / TID_QUEUE_CELL_SPACING;
319
320 /* The history is too long: remove data that is older than */
321 /* TID_MAX_TIME_DIFF */
322 if (index >= TID_QUEUE_MAX_SIZE)
323 rs_tl_rm_old_stats(tl, curr_time);
324
325 index = (tl->head + index) % TID_QUEUE_MAX_SIZE;
326 tl->packet_count[index] = tl->packet_count[index] + 1;
327 tl->total = tl->total + 1;
328
329 if ((index + 1) > tl->queue_count)
330 tl->queue_count = index + 1;
Ron Rindjunskyfaa29712008-06-12 09:46:58 +0800331
332 return tid;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200333}
334
Wey-Yi Guy54a430c2011-05-20 11:56:18 -0700335#ifdef CONFIG_MAC80211_DEBUGFS
Wey-Yi Guy4e308112011-07-08 08:46:28 -0700336/**
337 * Program the device to use fixed rate for frame transmit
338 * This is for debugging/testing only
339 * once the device start use fixed rate, we need to reload the module
340 * to being back the normal operation.
341 */
Wey-Yi Guy64898542011-05-03 18:05:13 -0700342static void rs_program_fix_rate(struct iwl_priv *priv,
343 struct iwl_lq_sta *lq_sta)
344{
345 struct iwl_station_priv *sta_priv =
346 container_of(lq_sta, struct iwl_station_priv, lq_sta);
Johannes Bergc6892902011-09-20 15:37:21 -0700347 struct iwl_rxon_context *ctx = sta_priv->ctx;
Wey-Yi Guy64898542011-05-03 18:05:13 -0700348
349 lq_sta->active_legacy_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
350 lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
351 lq_sta->active_mimo2_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
352 lq_sta->active_mimo3_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
353
Wey-Yi Guy5ef15cc2011-11-30 13:24:06 -0800354#ifdef CONFIG_IWLWIFI_DEVICE_TESTMODE
Wey-Yi Guy4e308112011-07-08 08:46:28 -0700355 /* testmode has higher priority to overwirte the fixed rate */
356 if (priv->tm_fixed_rate)
357 lq_sta->dbg_fixed_rate = priv->tm_fixed_rate;
Wey-Yi Guyc10e2c12011-07-13 11:13:46 -0700358#endif
Wey-Yi Guy64898542011-05-03 18:05:13 -0700359
360 IWL_DEBUG_RATE(priv, "sta_id %d rate 0x%X\n",
Wey-Yi Guy4e308112011-07-08 08:46:28 -0700361 lq_sta->lq.sta_id, lq_sta->dbg_fixed_rate);
Wey-Yi Guy64898542011-05-03 18:05:13 -0700362
Wey-Yi Guy4e308112011-07-08 08:46:28 -0700363 if (lq_sta->dbg_fixed_rate) {
364 rs_fill_link_cmd(NULL, lq_sta, lq_sta->dbg_fixed_rate);
Wey-Yi Guy64898542011-05-03 18:05:13 -0700365 iwl_send_lq_cmd(lq_sta->drv, ctx, &lq_sta->lq, CMD_ASYNC,
366 false);
367 }
368}
369#endif
370
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200371/*
372 get the traffic load value for tid
373*/
Tomas Winklere227cea2008-07-18 13:53:05 +0800374static u32 rs_tl_get_load(struct iwl_lq_sta *lq_data, u8 tid)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200375{
376 u32 curr_time = jiffies_to_msecs(jiffies);
377 u32 time_diff;
378 s32 index;
Tomas Winklere227cea2008-07-18 13:53:05 +0800379 struct iwl_traffic_load *tl = NULL;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200380
Johannes Berga8448552011-12-02 12:25:12 -0800381 if (tid >= IWL_MAX_TID_COUNT)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200382 return 0;
383
384 tl = &(lq_data->load[tid]);
385
386 curr_time -= curr_time % TID_ROUND_VALUE;
387
388 if (!(tl->queue_count))
389 return 0;
390
391 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
392 index = time_diff / TID_QUEUE_CELL_SPACING;
393
394 /* The history is too long: remove data that is older than */
395 /* TID_MAX_TIME_DIFF */
396 if (index >= TID_QUEUE_MAX_SIZE)
397 rs_tl_rm_old_stats(tl, curr_time);
398
399 return tl->total;
400}
401
Wey-Yi Guy82ca9342010-04-12 14:02:36 -0700402static int rs_tl_turn_on_agg_for_tid(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +0800403 struct iwl_lq_sta *lq_data, u8 tid,
Johannes Berg4b7679a2008-09-18 18:14:18 +0200404 struct ieee80211_sta *sta)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200405{
Wey-Yi Guy82ca9342010-04-12 14:02:36 -0700406 int ret = -EAGAIN;
Johannes Berg290f5992010-08-23 07:56:58 -0700407 u32 load;
408
409 /*
410 * Don't create TX aggregation sessions when in high
411 * BT traffic, as they would just be disrupted by BT.
412 */
413 if (priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH) {
Johannes Berg6690c012013-01-28 09:58:31 +0100414 IWL_DEBUG_COEX(priv,
415 "BT traffic (%d), no aggregation allowed\n",
416 priv->bt_traffic_load);
Johannes Berg290f5992010-08-23 07:56:58 -0700417 return ret;
418 }
419
420 load = rs_tl_get_load(lq_data, tid);
Wey-Yi Guy45d42702010-02-03 12:24:44 -0800421
Johannes Berg65de7e82012-04-17 07:36:30 -0700422 if ((iwlwifi_mod_params.auto_agg) || (load > IWL_AGG_LOAD_THRESHOLD)) {
Tomas Winklere1623442009-01-27 14:27:56 -0800423 IWL_DEBUG_HT(priv, "Starting Tx agg: STA: %pM tid: %d\n",
Johannes Berge1749612008-10-27 15:59:26 -0700424 sta->addr, tid);
Sujith Manoharanbd2ce6e2010-12-15 07:47:10 +0530425 ret = ieee80211_start_tx_ba_session(sta, tid, 5000);
Wey-Yi Guy45d42702010-02-03 12:24:44 -0800426 if (ret == -EAGAIN) {
427 /*
428 * driver and mac80211 is out of sync
429 * this might be cause by reloading firmware
430 * stop the tx ba session here
431 */
Andy Lutomirski24110542010-07-25 13:14:29 -0400432 IWL_ERR(priv, "Fail start Tx agg on tid: %d\n",
Wey-Yi Guy45d42702010-02-03 12:24:44 -0800433 tid);
Johannes Berg6a8579d2010-05-27 14:41:07 +0200434 ieee80211_stop_tx_ba_session(sta, tid);
Wey-Yi Guy45d42702010-02-03 12:24:44 -0800435 }
Andy Lutomirski24110542010-07-25 13:14:29 -0400436 } else {
Wey-Yi Guy5f88ac22011-05-27 08:40:31 -0700437 IWL_DEBUG_HT(priv, "Aggregation not enabled for tid %d "
Andy Lutomirski24110542010-07-25 13:14:29 -0400438 "because load = %u\n", tid, load);
439 }
Wey-Yi Guy82ca9342010-04-12 14:02:36 -0700440 return ret;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200441}
442
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700443static void rs_tl_turn_on_agg(struct iwl_priv *priv, u8 tid,
Tomas Winklere227cea2008-07-18 13:53:05 +0800444 struct iwl_lq_sta *lq_data,
Johannes Berg4b7679a2008-09-18 18:14:18 +0200445 struct ieee80211_sta *sta)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200446{
Johannes Berga8448552011-12-02 12:25:12 -0800447 if (tid < IWL_MAX_TID_COUNT)
Wey-Yi Guycfecc6b2010-06-18 11:33:15 -0700448 rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
449 else
Johannes Berga8448552011-12-02 12:25:12 -0800450 IWL_ERR(priv, "tid exceeds max TID count: %d/%d\n",
451 tid, IWL_MAX_TID_COUNT);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200452}
453
Guy Cohen39e88502008-04-23 17:14:57 -0700454static inline int get_num_of_ant_from_rate(u32 rate_n_flags)
Guy Cohenfde0db32008-04-21 15:42:01 -0700455{
Tomas Winkler3ac7f142008-07-21 02:40:14 +0300456 return !!(rate_n_flags & RATE_MCS_ANT_A_MSK) +
457 !!(rate_n_flags & RATE_MCS_ANT_B_MSK) +
458 !!(rate_n_flags & RATE_MCS_ANT_C_MSK);
Guy Cohenfde0db32008-04-21 15:42:01 -0700459}
460
Shanyu Zhao04f2dec2010-03-19 13:34:45 -0700461/*
462 * Static function to get the expected throughput from an iwl_scale_tbl_info
463 * that wraps a NULL pointer check
464 */
465static s32 get_expected_tpt(struct iwl_scale_tbl_info *tbl, int rs_index)
466{
467 if (tbl->expected_tpt)
468 return tbl->expected_tpt[rs_index];
469 return 0;
470}
471
Ben Cahill77626352007-11-29 11:09:44 +0800472/**
473 * rs_collect_tx_data - Update the success/failure sliding window
474 *
475 * We keep a sliding window of the last 62 packets transmitted
476 * at this rate. window->data contains the bitmask of successful
477 * packets.
478 */
Shanyu Zhao04f2dec2010-03-19 13:34:45 -0700479static int rs_collect_tx_data(struct iwl_scale_tbl_info *tbl,
480 int scale_index, int attempts, int successes)
Zhu Yib481de92007-09-25 17:54:57 -0700481{
Tomas Winklere227cea2008-07-18 13:53:05 +0800482 struct iwl_rate_scale_data *window = NULL;
Guy Cohen39e88502008-04-23 17:14:57 -0700483 static const u64 mask = (((u64)1) << (IWL_RATE_MAX_WINDOW - 1));
Shanyu Zhao04f2dec2010-03-19 13:34:45 -0700484 s32 fail_count, tpt;
Zhu Yib481de92007-09-25 17:54:57 -0700485
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800486 if (scale_index < 0 || scale_index >= IWL_RATE_COUNT)
487 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -0700488
Daniel C Halperine3949d62009-09-17 10:43:50 -0700489 /* Select window for current tx bit rate */
Shanyu Zhao04f2dec2010-03-19 13:34:45 -0700490 window = &(tbl->win[scale_index]);
491
492 /* Get expected throughput */
493 tpt = get_expected_tpt(tbl, scale_index);
Zhu Yib481de92007-09-25 17:54:57 -0700494
Ben Cahill77626352007-11-29 11:09:44 +0800495 /*
496 * Keep track of only the latest 62 tx frame attempts in this rate's
497 * history window; anything older isn't really relevant any more.
498 * If we have filled up the sliding window, drop the oldest attempt;
499 * if the oldest attempt (highest bit in bitmap) shows "success",
500 * subtract "1" from the success counter (this is the main reason
501 * we keep these bitmaps!).
502 */
Daniel C Halperine3949d62009-09-17 10:43:50 -0700503 while (attempts > 0) {
Guy Cohen39e88502008-04-23 17:14:57 -0700504 if (window->counter >= IWL_RATE_MAX_WINDOW) {
505
506 /* remove earliest */
507 window->counter = IWL_RATE_MAX_WINDOW - 1;
508
Ron Rindjunsky99556432008-01-28 14:07:25 +0200509 if (window->data & mask) {
510 window->data &= ~mask;
Guy Cohen39e88502008-04-23 17:14:57 -0700511 window->success_counter--;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200512 }
Zhu Yib481de92007-09-25 17:54:57 -0700513 }
Zhu Yib481de92007-09-25 17:54:57 -0700514
Ron Rindjunsky99556432008-01-28 14:07:25 +0200515 /* Increment frames-attempted counter */
516 window->counter++;
Ben Cahill77626352007-11-29 11:09:44 +0800517
Daniel C Halperine3949d62009-09-17 10:43:50 -0700518 /* Shift bitmap by one frame to throw away oldest history */
Guy Cohen90d77952008-09-09 10:54:53 +0800519 window->data <<= 1;
Daniel C Halperine3949d62009-09-17 10:43:50 -0700520
521 /* Mark the most recent #successes attempts as successful */
Ron Rindjunsky99556432008-01-28 14:07:25 +0200522 if (successes > 0) {
Guy Cohen39e88502008-04-23 17:14:57 -0700523 window->success_counter++;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200524 window->data |= 0x1;
525 successes--;
526 }
527
Daniel C Halperine3949d62009-09-17 10:43:50 -0700528 attempts--;
Zhu Yib481de92007-09-25 17:54:57 -0700529 }
530
Ben Cahill77626352007-11-29 11:09:44 +0800531 /* Calculate current success ratio, avoid divide-by-0! */
Zhu Yib481de92007-09-25 17:54:57 -0700532 if (window->counter > 0)
533 window->success_ratio = 128 * (100 * window->success_counter)
534 / window->counter;
535 else
536 window->success_ratio = IWL_INVALID_VALUE;
537
538 fail_count = window->counter - window->success_counter;
539
Ben Cahill77626352007-11-29 11:09:44 +0800540 /* Calculate average throughput, if we have enough history. */
Zhu Yib481de92007-09-25 17:54:57 -0700541 if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
542 (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
543 window->average_tpt = (window->success_ratio * tpt + 64) / 128;
544 else
545 window->average_tpt = IWL_INVALID_VALUE;
546
Ben Cahill77626352007-11-29 11:09:44 +0800547 /* Tag this window as having been updated */
Zhu Yib481de92007-09-25 17:54:57 -0700548 window->stamp = jiffies;
549
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800550 return 0;
Zhu Yib481de92007-09-25 17:54:57 -0700551}
552
Ben Cahill77626352007-11-29 11:09:44 +0800553/*
554 * Fill uCode API rate_n_flags field, based on "search" or "active" table.
555 */
Guy Cohen39e88502008-04-23 17:14:57 -0700556/* FIXME:RS:remove this function and put the flags statically in the table */
Tomas Winkler978785a2008-12-19 10:37:31 +0800557static u32 rate_n_flags_from_tbl(struct iwl_priv *priv,
558 struct iwl_scale_tbl_info *tbl,
559 int index, u8 use_green)
Zhu Yib481de92007-09-25 17:54:57 -0700560{
Guy Cohen39e88502008-04-23 17:14:57 -0700561 u32 rate_n_flags = 0;
562
Zhu Yib481de92007-09-25 17:54:57 -0700563 if (is_legacy(tbl->lq_type)) {
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800564 rate_n_flags = iwl_rates[index].plcp;
Zhu Yib481de92007-09-25 17:54:57 -0700565 if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
Guy Cohen39e88502008-04-23 17:14:57 -0700566 rate_n_flags |= RATE_MCS_CCK_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700567
Guy Cohenfde0db32008-04-21 15:42:01 -0700568 } else if (is_Ht(tbl->lq_type)) {
569 if (index > IWL_LAST_OFDM_RATE) {
Tomas Winkler978785a2008-12-19 10:37:31 +0800570 IWL_ERR(priv, "Invalid HT rate index %d\n", index);
Zhu Yib481de92007-09-25 17:54:57 -0700571 index = IWL_LAST_OFDM_RATE;
Guy Cohenfde0db32008-04-21 15:42:01 -0700572 }
Guy Cohen39e88502008-04-23 17:14:57 -0700573 rate_n_flags = RATE_MCS_HT_MSK;
Guy Cohenfde0db32008-04-21 15:42:01 -0700574
575 if (is_siso(tbl->lq_type))
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800576 rate_n_flags |= iwl_rates[index].plcp_siso;
Guy Cohenfde0db32008-04-21 15:42:01 -0700577 else if (is_mimo2(tbl->lq_type))
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800578 rate_n_flags |= iwl_rates[index].plcp_mimo2;
Guy Cohenfde0db32008-04-21 15:42:01 -0700579 else
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800580 rate_n_flags |= iwl_rates[index].plcp_mimo3;
Zhu Yib481de92007-09-25 17:54:57 -0700581 } else {
Tomas Winkler978785a2008-12-19 10:37:31 +0800582 IWL_ERR(priv, "Invalid tbl->lq_type %d\n", tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -0700583 }
584
Guy Cohen39e88502008-04-23 17:14:57 -0700585 rate_n_flags |= ((tbl->ant_type << RATE_MCS_ANT_POS) &
Guy Cohenfde0db32008-04-21 15:42:01 -0700586 RATE_MCS_ANT_ABC_MSK);
Zhu Yib481de92007-09-25 17:54:57 -0700587
Guy Cohen39e88502008-04-23 17:14:57 -0700588 if (is_Ht(tbl->lq_type)) {
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700589 if (tbl->is_ht40) {
Guy Cohen39e88502008-04-23 17:14:57 -0700590 if (tbl->is_dup)
591 rate_n_flags |= RATE_MCS_DUP_MSK;
592 else
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700593 rate_n_flags |= RATE_MCS_HT40_MSK;
Guy Cohen39e88502008-04-23 17:14:57 -0700594 }
595 if (tbl->is_SGI)
596 rate_n_flags |= RATE_MCS_SGI_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700597
Guy Cohen39e88502008-04-23 17:14:57 -0700598 if (use_green) {
599 rate_n_flags |= RATE_MCS_GF_MSK;
600 if (is_siso(tbl->lq_type) && tbl->is_SGI) {
601 rate_n_flags &= ~RATE_MCS_SGI_MSK;
Tomas Winkler978785a2008-12-19 10:37:31 +0800602 IWL_ERR(priv, "GF was set with SGI:SISO\n");
Guy Cohen39e88502008-04-23 17:14:57 -0700603 }
604 }
Zhu Yib481de92007-09-25 17:54:57 -0700605 }
Guy Cohen39e88502008-04-23 17:14:57 -0700606 return rate_n_flags;
Zhu Yib481de92007-09-25 17:54:57 -0700607}
608
Ben Cahill77626352007-11-29 11:09:44 +0800609/*
610 * Interpret uCode API's rate_n_flags format,
611 * fill "search" or "active" tx mode table.
612 */
Guy Cohen39e88502008-04-23 17:14:57 -0700613static int rs_get_tbl_info_from_mcs(const u32 rate_n_flags,
Johannes Berg8318d782008-01-24 19:38:38 +0100614 enum ieee80211_band band,
Tomas Winklere227cea2008-07-18 13:53:05 +0800615 struct iwl_scale_tbl_info *tbl,
Zhu Yib481de92007-09-25 17:54:57 -0700616 int *rate_idx)
617{
Guy Cohen39e88502008-04-23 17:14:57 -0700618 u32 ant_msk = (rate_n_flags & RATE_MCS_ANT_ABC_MSK);
619 u8 num_of_ant = get_num_of_ant_from_rate(rate_n_flags);
620 u8 mcs;
Zhu Yib481de92007-09-25 17:54:57 -0700621
Wey-Yi Guy80e91582010-08-03 08:23:32 -0700622 memset(tbl, 0, sizeof(struct iwl_scale_tbl_info));
Tomas Winklere7d326ac2008-06-12 09:47:11 +0800623 *rate_idx = iwl_hwrate_to_plcp_idx(rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -0700624
Guy Cohenfde0db32008-04-21 15:42:01 -0700625 if (*rate_idx == IWL_RATE_INVALID) {
Zhu Yib481de92007-09-25 17:54:57 -0700626 *rate_idx = -1;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800627 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -0700628 }
Ben Cahill77626352007-11-29 11:09:44 +0800629 tbl->is_SGI = 0; /* default legacy setup */
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700630 tbl->is_ht40 = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700631 tbl->is_dup = 0;
Guy Cohenfde0db32008-04-21 15:42:01 -0700632 tbl->ant_type = (ant_msk >> RATE_MCS_ANT_POS);
633 tbl->lq_type = LQ_NONE;
Mohamed Abbasd6e93392009-05-08 13:44:39 -0700634 tbl->max_search = IWL_MAX_SEARCH;
Zhu Yib481de92007-09-25 17:54:57 -0700635
Ben Cahill77626352007-11-29 11:09:44 +0800636 /* legacy rate format */
Guy Cohen39e88502008-04-23 17:14:57 -0700637 if (!(rate_n_flags & RATE_MCS_HT_MSK)) {
Guy Cohenfde0db32008-04-21 15:42:01 -0700638 if (num_of_ant == 1) {
Johannes Berg8318d782008-01-24 19:38:38 +0100639 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700640 tbl->lq_type = LQ_A;
641 else
642 tbl->lq_type = LQ_G;
Zhu Yib481de92007-09-25 17:54:57 -0700643 }
Guy Cohenfde0db32008-04-21 15:42:01 -0700644 /* HT rate format */
Zhu Yib481de92007-09-25 17:54:57 -0700645 } else {
Guy Cohen39e88502008-04-23 17:14:57 -0700646 if (rate_n_flags & RATE_MCS_SGI_MSK)
Zhu Yib481de92007-09-25 17:54:57 -0700647 tbl->is_SGI = 1;
648
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700649 if ((rate_n_flags & RATE_MCS_HT40_MSK) ||
Guy Cohen39e88502008-04-23 17:14:57 -0700650 (rate_n_flags & RATE_MCS_DUP_MSK))
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700651 tbl->is_ht40 = 1;
Zhu Yib481de92007-09-25 17:54:57 -0700652
Guy Cohen39e88502008-04-23 17:14:57 -0700653 if (rate_n_flags & RATE_MCS_DUP_MSK)
Zhu Yib481de92007-09-25 17:54:57 -0700654 tbl->is_dup = 1;
Guy Cohenfde0db32008-04-21 15:42:01 -0700655
Guy Cohen39e88502008-04-23 17:14:57 -0700656 mcs = rs_extract_rate(rate_n_flags);
Guy Cohenfde0db32008-04-21 15:42:01 -0700657
Guy Cohen39e88502008-04-23 17:14:57 -0700658 /* SISO */
659 if (mcs <= IWL_RATE_SISO_60M_PLCP) {
Guy Cohenfde0db32008-04-21 15:42:01 -0700660 if (num_of_ant == 1)
661 tbl->lq_type = LQ_SISO; /*else NONE*/
662 /* MIMO2 */
Guy Cohen39e88502008-04-23 17:14:57 -0700663 } else if (mcs <= IWL_RATE_MIMO2_60M_PLCP) {
Guy Cohenfde0db32008-04-21 15:42:01 -0700664 if (num_of_ant == 2)
665 tbl->lq_type = LQ_MIMO2;
666 /* MIMO3 */
667 } else {
Mohamed Abbasd6e93392009-05-08 13:44:39 -0700668 if (num_of_ant == 3) {
669 tbl->max_search = IWL_MAX_11N_MIMO3_SEARCH;
Guy Cohenfde0db32008-04-21 15:42:01 -0700670 tbl->lq_type = LQ_MIMO3;
Mohamed Abbasd6e93392009-05-08 13:44:39 -0700671 }
Guy Cohenfde0db32008-04-21 15:42:01 -0700672 }
Zhu Yib481de92007-09-25 17:54:57 -0700673 }
674 return 0;
675}
Guy Cohenaade00c2008-04-23 17:14:59 -0700676
677/* switch to another antenna/antennas and return 1 */
678/* if no other valid antenna found, return 0 */
679static int rs_toggle_antenna(u32 valid_ant, u32 *rate_n_flags,
Tomas Winklere227cea2008-07-18 13:53:05 +0800680 struct iwl_scale_tbl_info *tbl)
Zhu Yib481de92007-09-25 17:54:57 -0700681{
Guy Cohenaade00c2008-04-23 17:14:59 -0700682 u8 new_ant_type;
683
684 if (!tbl->ant_type || tbl->ant_type > ANT_ABC)
685 return 0;
686
687 if (!rs_is_valid_ant(valid_ant, tbl->ant_type))
688 return 0;
689
690 new_ant_type = ant_toggle_lookup[tbl->ant_type];
691
692 while ((new_ant_type != tbl->ant_type) &&
693 !rs_is_valid_ant(valid_ant, new_ant_type))
694 new_ant_type = ant_toggle_lookup[new_ant_type];
695
696 if (new_ant_type == tbl->ant_type)
697 return 0;
698
699 tbl->ant_type = new_ant_type;
700 *rate_n_flags &= ~RATE_MCS_ANT_ABC_MSK;
701 *rate_n_flags |= new_ant_type << RATE_MCS_ANT_POS;
702 return 1;
Zhu Yib481de92007-09-25 17:54:57 -0700703}
704
Daniel C Halperinb2617932009-08-13 13:30:59 -0700705/**
706 * Green-field mode is valid if the station supports it and
707 * there are no non-GF stations present in the BSS.
708 */
Johannes Berg7e6a5882010-08-23 10:46:46 +0200709static bool rs_use_green(struct ieee80211_sta *sta)
Zhu Yib481de92007-09-25 17:54:57 -0700710{
Johannes Berg50e2a302012-08-05 18:31:46 +0200711 /*
712 * There's a bug somewhere in this code that causes the
713 * scaling to get stuck because GF+SGI can't be combined
714 * in SISO rates. Until we find that bug, disable GF, it
715 * has only limited benefit and we still interoperate with
716 * GF APs since we can always receive GF transmissions.
717 */
718 return false;
Guy Cohenf935a6d2008-04-23 17:15:02 -0700719}
Zhu Yib481de92007-09-25 17:54:57 -0700720
721/**
722 * rs_get_supported_rates - get the available rates
723 *
724 * if management frame or broadcast frame only return
725 * basic available rates.
726 *
727 */
Tomas Winklere227cea2008-07-18 13:53:05 +0800728static u16 rs_get_supported_rates(struct iwl_lq_sta *lq_sta,
729 struct ieee80211_hdr *hdr,
730 enum iwl_table_type rate_type)
Zhu Yib481de92007-09-25 17:54:57 -0700731{
Guy Coheneecd6e52008-04-23 17:14:58 -0700732 if (is_legacy(rate_type)) {
733 return lq_sta->active_legacy_rate;
734 } else {
735 if (is_siso(rate_type))
736 return lq_sta->active_siso_rate;
737 else if (is_mimo2(rate_type))
738 return lq_sta->active_mimo2_rate;
739 else
740 return lq_sta->active_mimo3_rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -0800741 }
Zhu Yib481de92007-09-25 17:54:57 -0700742}
743
Ester Kummerbf403db2008-05-05 10:22:40 +0800744static u16 rs_get_adjacent_rate(struct iwl_priv *priv, u8 index, u16 rate_mask,
745 int rate_type)
Zhu Yib481de92007-09-25 17:54:57 -0700746{
747 u8 high = IWL_RATE_INVALID;
748 u8 low = IWL_RATE_INVALID;
749
Ian Schram01ebd062007-10-25 17:15:22 +0800750 /* 802.11A or ht walks to the next literal adjacent rate in
Zhu Yib481de92007-09-25 17:54:57 -0700751 * the rate table */
752 if (is_a_band(rate_type) || !is_legacy(rate_type)) {
753 int i;
754 u32 mask;
755
756 /* Find the previous rate that is in the rate mask */
757 i = index - 1;
758 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
759 if (rate_mask & mask) {
760 low = i;
761 break;
762 }
763 }
764
765 /* Find the next rate that is in the rate mask */
766 i = index + 1;
767 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
768 if (rate_mask & mask) {
769 high = i;
770 break;
771 }
772 }
773
774 return (high << 8) | low;
775 }
776
777 low = index;
778 while (low != IWL_RATE_INVALID) {
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800779 low = iwl_rates[low].prev_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700780 if (low == IWL_RATE_INVALID)
781 break;
782 if (rate_mask & (1 << low))
783 break;
Tomas Winklere1623442009-01-27 14:27:56 -0800784 IWL_DEBUG_RATE(priv, "Skipping masked lower rate: %d\n", low);
Zhu Yib481de92007-09-25 17:54:57 -0700785 }
786
787 high = index;
788 while (high != IWL_RATE_INVALID) {
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800789 high = iwl_rates[high].next_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700790 if (high == IWL_RATE_INVALID)
791 break;
792 if (rate_mask & (1 << high))
793 break;
Tomas Winklere1623442009-01-27 14:27:56 -0800794 IWL_DEBUG_RATE(priv, "Skipping masked higher rate: %d\n", high);
Zhu Yib481de92007-09-25 17:54:57 -0700795 }
796
797 return (high << 8) | low;
798}
799
Tomas Winklere227cea2008-07-18 13:53:05 +0800800static u32 rs_get_lower_rate(struct iwl_lq_sta *lq_sta,
801 struct iwl_scale_tbl_info *tbl,
802 u8 scale_index, u8 ht_possible)
Zhu Yib481de92007-09-25 17:54:57 -0700803{
Zhu Yib481de92007-09-25 17:54:57 -0700804 s32 low;
805 u16 rate_mask;
806 u16 high_low;
807 u8 switch_to_legacy = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -0800808 u8 is_green = lq_sta->is_green;
Wey-Yi Guy2ff65782009-09-11 10:38:18 -0700809 struct iwl_priv *priv = lq_sta->drv;
Zhu Yib481de92007-09-25 17:54:57 -0700810
811 /* check if we need to switch from HT to legacy rates.
812 * assumption is that mandatory rates (1Mbps or 6Mbps)
813 * are always supported (spec demand) */
814 if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_index)) {
815 switch_to_legacy = 1;
816 scale_index = rs_ht_to_legacy[scale_index];
Johannes Berg8318d782008-01-24 19:38:38 +0100817 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700818 tbl->lq_type = LQ_A;
819 else
820 tbl->lq_type = LQ_G;
821
Guy Cohen69fdb302008-04-23 17:15:01 -0700822 if (num_of_ant(tbl->ant_type) > 1)
Wey-Yi Guy2ff65782009-09-11 10:38:18 -0700823 tbl->ant_type =
Eytan Lifshitzb7998c82012-12-01 20:59:49 +0200824 first_antenna(priv->nvm_data->valid_tx_ant);
Zhu Yib481de92007-09-25 17:54:57 -0700825
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700826 tbl->is_ht40 = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700827 tbl->is_SGI = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -0700828 tbl->max_search = IWL_MAX_SEARCH;
Zhu Yib481de92007-09-25 17:54:57 -0700829 }
830
Guy Coheneecd6e52008-04-23 17:14:58 -0700831 rate_mask = rs_get_supported_rates(lq_sta, NULL, tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -0700832
Ben Cahill77626352007-11-29 11:09:44 +0800833 /* Mask with station rate restriction */
Zhu Yib481de92007-09-25 17:54:57 -0700834 if (is_legacy(tbl->lq_type)) {
Ben Cahill77626352007-11-29 11:09:44 +0800835 /* supp_rates has no CCK bits in A mode */
Johannes Berg8318d782008-01-24 19:38:38 +0100836 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700837 rate_mask = (u16)(rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -0800838 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
Zhu Yib481de92007-09-25 17:54:57 -0700839 else
Tomas Winklerc33104f2008-01-14 17:46:21 -0800840 rate_mask = (u16)(rate_mask & lq_sta->supp_rates);
Zhu Yib481de92007-09-25 17:54:57 -0700841 }
842
Ben Cahill77626352007-11-29 11:09:44 +0800843 /* If we switched from HT to legacy, check current rate */
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800844 if (switch_to_legacy && (rate_mask & (1 << scale_index))) {
Guy Cohen39e88502008-04-23 17:14:57 -0700845 low = scale_index;
846 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700847 }
848
Ester Kummerbf403db2008-05-05 10:22:40 +0800849 high_low = rs_get_adjacent_rate(lq_sta->drv, scale_index, rate_mask,
850 tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -0700851 low = high_low & 0xff;
852
Guy Cohen39e88502008-04-23 17:14:57 -0700853 if (low == IWL_RATE_INVALID)
854 low = scale_index;
855
856out:
Tomas Winkler978785a2008-12-19 10:37:31 +0800857 return rate_n_flags_from_tbl(lq_sta->drv, tbl, low, is_green);
Zhu Yib481de92007-09-25 17:54:57 -0700858}
859
Ben Cahill77626352007-11-29 11:09:44 +0800860/*
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700861 * Simple function to compare two rate scale table types
862 */
863static bool table_type_matches(struct iwl_scale_tbl_info *a,
864 struct iwl_scale_tbl_info *b)
865{
866 return (a->lq_type == b->lq_type) && (a->ant_type == b->ant_type) &&
867 (a->is_SGI == b->is_SGI);
868}
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700869
Johannes Berg7e6a5882010-08-23 10:46:46 +0200870static void rs_bt_update_lq(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
871 struct iwl_lq_sta *lq_sta)
Wey-Yi Guybee008b2010-08-23 07:57:04 -0700872{
873 struct iwl_scale_tbl_info *tbl;
Wey-Yi Guy66e863a52010-11-08 14:54:37 -0800874 bool full_concurrent = priv->bt_full_concurrent;
Wey-Yi Guybee008b2010-08-23 07:57:04 -0700875
Wey-Yi Guy66e863a52010-11-08 14:54:37 -0800876 if (priv->bt_ant_couple_ok) {
877 /*
878 * Is there a need to switch between
879 * full concurrency and 3-wire?
880 */
Wey-Yi Guy66e863a52010-11-08 14:54:37 -0800881 if (priv->bt_ci_compliance && priv->bt_ant_couple_ok)
882 full_concurrent = true;
883 else
884 full_concurrent = false;
Wey-Yi Guy66e863a52010-11-08 14:54:37 -0800885 }
886 if ((priv->bt_traffic_load != priv->last_bt_traffic_load) ||
887 (priv->bt_full_concurrent != full_concurrent)) {
Wey-Yi Guybee008b2010-08-23 07:57:04 -0700888 priv->bt_full_concurrent = full_concurrent;
Meenakshi Venkataraman882dde82012-05-16 22:35:57 +0200889 priv->last_bt_traffic_load = priv->bt_traffic_load;
Wey-Yi Guybee008b2010-08-23 07:57:04 -0700890
891 /* Update uCode's rate table. */
892 tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
893 rs_fill_link_cmd(priv, lq_sta, tbl->current_rate);
Johannes Berg7e6a5882010-08-23 10:46:46 +0200894 iwl_send_lq_cmd(priv, ctx, &lq_sta->lq, CMD_ASYNC, false);
Wey-Yi Guybee008b2010-08-23 07:57:04 -0700895
Johannes Berg1ee158d2012-02-17 10:07:44 -0800896 queue_work(priv->workqueue, &priv->bt_full_concurrency);
Wey-Yi Guybee008b2010-08-23 07:57:04 -0700897 }
898}
899
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700900/*
Ben Cahill77626352007-11-29 11:09:44 +0800901 * mac80211 sends us Tx status
902 */
Johannes Berg4b7679a2008-09-18 18:14:18 +0200903static void rs_tx_status(void *priv_r, struct ieee80211_supported_band *sband,
904 struct ieee80211_sta *sta, void *priv_sta,
Johannes Berge039fa42008-05-15 12:55:29 +0200905 struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -0700906{
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700907 int legacy_success;
908 int retries;
909 int rs_index, mac_index, i;
Tomas Winkler417f1142008-11-12 13:14:06 -0800910 struct iwl_lq_sta *lq_sta = priv_sta;
Tomas Winkler66c73db2008-04-15 16:01:40 -0700911 struct iwl_link_quality_cmd *table;
Zhu Yib481de92007-09-25 17:54:57 -0700912 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Emmanuel Grumbachd0f76d62012-02-09 16:08:15 +0200913 struct iwl_op_mode *op_mode = (struct iwl_op_mode *)priv_r;
914 struct iwl_priv *priv = IWL_OP_MODE_GET_DVM(op_mode);
Johannes Berge039fa42008-05-15 12:55:29 +0200915 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Daniel C Halperin50273092009-08-28 09:44:45 -0700916 enum mac80211_rate_control_flags mac_flags;
Guy Cohen39e88502008-04-23 17:14:57 -0700917 u32 tx_rate;
Tomas Winklere227cea2008-07-18 13:53:05 +0800918 struct iwl_scale_tbl_info tbl_type;
Shanyu Zhao04f2dec2010-03-19 13:34:45 -0700919 struct iwl_scale_tbl_info *curr_tbl, *other_tbl, *tmp_tbl;
Johannes Berg7e6a5882010-08-23 10:46:46 +0200920 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
Johannes Bergc6892902011-09-20 15:37:21 -0700921 struct iwl_rxon_context *ctx = sta_priv->ctx;
Zhu Yib481de92007-09-25 17:54:57 -0700922
Tomas Winklere1623442009-01-27 14:27:56 -0800923 IWL_DEBUG_RATE_LIMIT(priv, "get frame ack response, update rate scale window\n");
Zhu Yib481de92007-09-25 17:54:57 -0700924
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800925 /* Treat uninitialized rate scaling data same as non-existing. */
926 if (!lq_sta) {
927 IWL_DEBUG_RATE(priv, "Station rate scaling not created yet.\n");
928 return;
929 } else if (!lq_sta->drv) {
930 IWL_DEBUG_RATE(priv, "Rate scaling not initialized yet.\n");
931 return;
932 }
933
Tomas Winkler417f1142008-11-12 13:14:06 -0800934 if (!ieee80211_is_data(hdr->frame_control) ||
Gábor Stefanik514d65c2009-04-23 19:36:08 +0200935 info->flags & IEEE80211_TX_CTL_NO_ACK)
Zhu Yib481de92007-09-25 17:54:57 -0700936 return;
937
Daniel C Halperine3949d62009-09-17 10:43:50 -0700938 /* This packet was aggregated but doesn't carry status info */
Johannes Berge039fa42008-05-15 12:55:29 +0200939 if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
940 !(info->flags & IEEE80211_TX_STAT_AMPDU))
Ron Rindjunsky99556432008-01-28 14:07:25 +0200941 return;
942
Ben Cahill77626352007-11-29 11:09:44 +0800943 /*
944 * Ignore this Tx frame response if its initial rate doesn't match
945 * that of latest Link Quality command. There may be stragglers
946 * from a previous Link Quality command, but we're no longer interested
947 * in those; they're either from the "active" mode while we're trying
948 * to check "search" mode, or a prior "search" mode after we've moved
949 * to a new "search" mode (which might become the new "active" mode).
950 */
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700951 table = &lq_sta->lq;
Guy Cohen39e88502008-04-23 17:14:57 -0700952 tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
953 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800954 if (priv->band == IEEE80211_BAND_5GHZ)
955 rs_index -= IWL_FIRST_OFDM_RATE;
Daniel C Halperin50273092009-08-28 09:44:45 -0700956 mac_flags = info->status.rates[0].flags;
957 mac_index = info->status.rates[0].idx;
Daniel C Halperin31513be2009-08-28 09:44:47 -0700958 /* For HT packets, map MCS to PLCP */
959 if (mac_flags & IEEE80211_TX_RC_MCS) {
960 mac_index &= RATE_MCS_CODE_MSK; /* Remove # of streams */
961 if (mac_index >= (IWL_RATE_9M_INDEX - IWL_FIRST_OFDM_RATE))
962 mac_index++;
Daniel C Halperin392a0ba2009-09-11 10:38:08 -0700963 /*
964 * mac80211 HT index is always zero-indexed; we need to move
965 * HT OFDM rates after CCK rates in 2.4 GHz band
966 */
967 if (priv->band == IEEE80211_BAND_2GHZ)
968 mac_index += IWL_FIRST_OFDM_RATE;
Daniel C Halperin31513be2009-08-28 09:44:47 -0700969 }
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700970 /* Here we actually compare this rate to the latest LQ command */
Daniel C Halperin50273092009-08-28 09:44:45 -0700971 if ((mac_index < 0) ||
972 (tbl_type.is_SGI != !!(mac_flags & IEEE80211_TX_RC_SHORT_GI)) ||
973 (tbl_type.is_ht40 != !!(mac_flags & IEEE80211_TX_RC_40_MHZ_WIDTH)) ||
974 (tbl_type.is_dup != !!(mac_flags & IEEE80211_TX_RC_DUP_DATA)) ||
Johannes Bergd748b462012-03-28 11:04:23 +0200975 (tbl_type.ant_type != info->status.antenna) ||
Daniel C Halperin50273092009-08-28 09:44:45 -0700976 (!!(tx_rate & RATE_MCS_HT_MSK) != !!(mac_flags & IEEE80211_TX_RC_MCS)) ||
977 (!!(tx_rate & RATE_MCS_GF_MSK) != !!(mac_flags & IEEE80211_TX_RC_GREEN_FIELD)) ||
Daniel C Halperin31513be2009-08-28 09:44:47 -0700978 (rs_index != mac_index)) {
979 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 -0700980 /*
981 * Since rates mis-match, the last LQ command may have failed.
982 * After IWL_MISSED_RATE_MAX mis-matches, resync the uCode with
983 * ... driver.
Mohamed Abbasd5e49032008-12-12 08:22:15 -0800984 */
Abbas, Mohamed7d049e52009-01-20 21:33:53 -0800985 lq_sta->missed_rate_counter++;
986 if (lq_sta->missed_rate_counter > IWL_MISSED_RATE_MAX) {
987 lq_sta->missed_rate_counter = 0;
Johannes Berg7e6a5882010-08-23 10:46:46 +0200988 iwl_send_lq_cmd(priv, ctx, &lq_sta->lq, CMD_ASYNC, false);
Abbas, Mohamed7d049e52009-01-20 21:33:53 -0800989 }
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700990 /* Regardless, ignore this status info for outdated rate */
991 return;
992 } else
993 /* Rate did match, so reset the missed_rate_counter */
994 lq_sta->missed_rate_counter = 0;
995
996 /* Figure out if rate scale algorithm is in active or search table */
997 if (table_type_matches(&tbl_type,
998 &(lq_sta->lq_info[lq_sta->active_tbl]))) {
999 curr_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1000 other_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
1001 } else if (table_type_matches(&tbl_type,
1002 &lq_sta->lq_info[1 - lq_sta->active_tbl])) {
1003 curr_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
1004 other_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1005 } else {
1006 IWL_DEBUG_RATE(priv, "Neither active nor search matches tx rate\n");
Wey-Yi Guy80e91582010-08-03 08:23:32 -07001007 tmp_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1008 IWL_DEBUG_RATE(priv, "active- lq:%x, ant:%x, SGI:%d\n",
1009 tmp_tbl->lq_type, tmp_tbl->ant_type, tmp_tbl->is_SGI);
1010 tmp_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
1011 IWL_DEBUG_RATE(priv, "search- lq:%x, ant:%x, SGI:%d\n",
1012 tmp_tbl->lq_type, tmp_tbl->ant_type, tmp_tbl->is_SGI);
1013 IWL_DEBUG_RATE(priv, "actual- lq:%x, ant:%x, SGI:%d\n",
1014 tbl_type.lq_type, tbl_type.ant_type, tbl_type.is_SGI);
1015 /*
1016 * no matching table found, let's by-pass the data collection
1017 * and continue to perform rate scale to find the rate table
1018 */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001019 rs_stay_in_table(lq_sta, true);
Wey-Yi Guy80e91582010-08-03 08:23:32 -07001020 goto done;
Zhu Yib481de92007-09-25 17:54:57 -07001021 }
Zhu Yib481de92007-09-25 17:54:57 -07001022
Ben Cahill77626352007-11-29 11:09:44 +08001023 /*
Daniel C Halperin95407aa2009-09-17 10:43:48 -07001024 * Updating the frame history depends on whether packets were
1025 * aggregated.
1026 *
1027 * For aggregation, all packets were transmitted at the same rate, the
1028 * first index into rate scale table.
Ben Cahill77626352007-11-29 11:09:44 +08001029 */
Daniel C Halperin95407aa2009-09-17 10:43:48 -07001030 if (info->flags & IEEE80211_TX_STAT_AMPDU) {
1031 tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
1032 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type,
1033 &rs_index);
Shanyu Zhao04f2dec2010-03-19 13:34:45 -07001034 rs_collect_tx_data(curr_tbl, rs_index,
Daniel Halperine3a3cd82010-04-18 09:27:58 -07001035 info->status.ampdu_len,
1036 info->status.ampdu_ack_len);
Zhu Yib481de92007-09-25 17:54:57 -07001037
Daniel C Halperin95407aa2009-09-17 10:43:48 -07001038 /* Update success/fail counts if not searching for new mode */
1039 if (lq_sta->stay_in_tbl) {
Daniel Halperine3a3cd82010-04-18 09:27:58 -07001040 lq_sta->total_success += info->status.ampdu_ack_len;
1041 lq_sta->total_failed += (info->status.ampdu_len -
1042 info->status.ampdu_ack_len);
Daniel C Halperin95407aa2009-09-17 10:43:48 -07001043 }
1044 } else {
1045 /*
1046 * For legacy, update frame history with for each Tx retry.
1047 */
1048 retries = info->status.rates[0].count - 1;
1049 /* HW doesn't send more than 15 retries */
1050 retries = min(retries, 15);
1051
1052 /* The last transmission may have been successful */
1053 legacy_success = !!(info->flags & IEEE80211_TX_STAT_ACK);
1054 /* Collect data for each rate used during failed TX attempts */
1055 for (i = 0; i <= retries; ++i) {
1056 tx_rate = le32_to_cpu(table->rs_table[i].rate_n_flags);
1057 rs_get_tbl_info_from_mcs(tx_rate, priv->band,
1058 &tbl_type, &rs_index);
1059 /*
1060 * Only collect stats if retried rate is in the same RS
1061 * table as active/search.
1062 */
1063 if (table_type_matches(&tbl_type, curr_tbl))
Shanyu Zhao04f2dec2010-03-19 13:34:45 -07001064 tmp_tbl = curr_tbl;
Daniel C Halperin95407aa2009-09-17 10:43:48 -07001065 else if (table_type_matches(&tbl_type, other_tbl))
Shanyu Zhao04f2dec2010-03-19 13:34:45 -07001066 tmp_tbl = other_tbl;
Ron Rindjunsky99556432008-01-28 14:07:25 +02001067 else
Daniel C Halperin95407aa2009-09-17 10:43:48 -07001068 continue;
Shanyu Zhao04f2dec2010-03-19 13:34:45 -07001069 rs_collect_tx_data(tmp_tbl, rs_index, 1,
1070 i < retries ? 0 : legacy_success);
Daniel C Halperin95407aa2009-09-17 10:43:48 -07001071 }
1072
1073 /* Update success/fail counts if not searching for new mode */
1074 if (lq_sta->stay_in_tbl) {
1075 lq_sta->total_success += legacy_success;
1076 lq_sta->total_failed += retries + (1 - legacy_success);
Ron Rindjunsky99556432008-01-28 14:07:25 +02001077 }
Zhu Yib481de92007-09-25 17:54:57 -07001078 }
Daniel C Halperin95407aa2009-09-17 10:43:48 -07001079 /* The last TX rate is cached in lq_sta; it's set in if/else above */
1080 lq_sta->last_rate_n_flags = tx_rate;
Wey-Yi Guy80e91582010-08-03 08:23:32 -07001081done:
Ben Cahill77626352007-11-29 11:09:44 +08001082 /* See if there's a better rate or modulation mode to try. */
Abbas, Mohamedc338ba32009-01-21 10:58:02 -08001083 if (sta && sta->supp_rates[sband->band])
Gábor Stefanik514d65c2009-04-23 19:36:08 +02001084 rs_rate_scale_perform(priv, skb, sta, lq_sta);
Wey-Yi Guyc10e2c12011-07-13 11:13:46 -07001085
Wey-Yi Guy5ef15cc2011-11-30 13:24:06 -08001086#if defined(CONFIG_MAC80211_DEBUGFS) && defined(CONFIG_IWLWIFI_DEVICE_TESTMODE)
Wey-Yi Guy4e308112011-07-08 08:46:28 -07001087 if ((priv->tm_fixed_rate) &&
1088 (priv->tm_fixed_rate != lq_sta->dbg_fixed_rate))
Wey-Yi Guy64898542011-05-03 18:05:13 -07001089 rs_program_fix_rate(priv, lq_sta);
1090#endif
Emmanuel Grumbach21522682012-03-22 17:51:44 +02001091 if (priv->cfg->bt_params && priv->cfg->bt_params->advanced_bt_coexist)
Johannes Berg7e6a5882010-08-23 10:46:46 +02001092 rs_bt_update_lq(priv, ctx, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07001093}
1094
Ben Cahill77626352007-11-29 11:09:44 +08001095/*
1096 * Begin a period of staying with a selected modulation mode.
1097 * Set "stay_in_tbl" flag to prevent any mode switches.
1098 * Set frame tx success limits according to legacy vs. high-throughput,
1099 * and reset overall (spanning all rates) tx success history statistics.
1100 * These control how long we stay using same modulation mode before
1101 * searching for a new mode.
1102 */
Ester Kummerbf403db2008-05-05 10:22:40 +08001103static void rs_set_stay_in_table(struct iwl_priv *priv, u8 is_legacy,
Tomas Winklere227cea2008-07-18 13:53:05 +08001104 struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07001105{
Tomas Winklere1623442009-01-27 14:27:56 -08001106 IWL_DEBUG_RATE(priv, "we are staying in the same table\n");
Tomas Winklerc33104f2008-01-14 17:46:21 -08001107 lq_sta->stay_in_tbl = 1; /* only place this gets set */
Zhu Yib481de92007-09-25 17:54:57 -07001108 if (is_legacy) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001109 lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT;
1110 lq_sta->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
1111 lq_sta->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
Zhu Yib481de92007-09-25 17:54:57 -07001112 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001113 lq_sta->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
1114 lq_sta->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
1115 lq_sta->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
Zhu Yib481de92007-09-25 17:54:57 -07001116 }
Tomas Winklerc33104f2008-01-14 17:46:21 -08001117 lq_sta->table_count = 0;
1118 lq_sta->total_failed = 0;
1119 lq_sta->total_success = 0;
Mohamed Abbas447fee72009-04-20 14:37:02 -07001120 lq_sta->flush_timer = jiffies;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001121 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001122}
1123
Ben Cahill77626352007-11-29 11:09:44 +08001124/*
1125 * Find correct throughput table for given mode of modulation
1126 */
Tomas Winklere227cea2008-07-18 13:53:05 +08001127static void rs_set_expected_tpt_table(struct iwl_lq_sta *lq_sta,
1128 struct iwl_scale_tbl_info *tbl)
Zhu Yib481de92007-09-25 17:54:57 -07001129{
Daniel C Halperine3949d62009-09-17 10:43:50 -07001130 /* Used to choose among HT tables */
1131 s32 (*ht_tbl_pointer)[IWL_RATE_COUNT];
1132
1133 /* Check for invalid LQ type */
1134 if (WARN_ON_ONCE(!is_legacy(tbl->lq_type) && !is_Ht(tbl->lq_type))) {
1135 tbl->expected_tpt = expected_tpt_legacy;
1136 return;
1137 }
1138
1139 /* Legacy rates have only one table */
Zhu Yib481de92007-09-25 17:54:57 -07001140 if (is_legacy(tbl->lq_type)) {
Daniel C Halperine3949d62009-09-17 10:43:50 -07001141 tbl->expected_tpt = expected_tpt_legacy;
1142 return;
1143 }
1144
1145 /* Choose among many HT tables depending on number of streams
1146 * (SISO/MIMO2/MIMO3), channel width (20/40), SGI, and aggregation
1147 * status */
1148 if (is_siso(tbl->lq_type) && (!tbl->is_ht40 || lq_sta->is_dup))
1149 ht_tbl_pointer = expected_tpt_siso20MHz;
1150 else if (is_siso(tbl->lq_type))
1151 ht_tbl_pointer = expected_tpt_siso40MHz;
1152 else if (is_mimo2(tbl->lq_type) && (!tbl->is_ht40 || lq_sta->is_dup))
1153 ht_tbl_pointer = expected_tpt_mimo2_20MHz;
1154 else if (is_mimo2(tbl->lq_type))
1155 ht_tbl_pointer = expected_tpt_mimo2_40MHz;
1156 else if (is_mimo3(tbl->lq_type) && (!tbl->is_ht40 || lq_sta->is_dup))
1157 ht_tbl_pointer = expected_tpt_mimo3_20MHz;
1158 else /* if (is_mimo3(tbl->lq_type)) <-- must be true */
1159 ht_tbl_pointer = expected_tpt_mimo3_40MHz;
1160
1161 if (!tbl->is_SGI && !lq_sta->is_agg) /* Normal */
1162 tbl->expected_tpt = ht_tbl_pointer[0];
1163 else if (tbl->is_SGI && !lq_sta->is_agg) /* SGI */
1164 tbl->expected_tpt = ht_tbl_pointer[1];
1165 else if (!tbl->is_SGI && lq_sta->is_agg) /* AGG */
1166 tbl->expected_tpt = ht_tbl_pointer[2];
1167 else /* AGG+SGI */
1168 tbl->expected_tpt = ht_tbl_pointer[3];
Zhu Yib481de92007-09-25 17:54:57 -07001169}
1170
Ben Cahill77626352007-11-29 11:09:44 +08001171/*
1172 * Find starting rate for new "search" high-throughput mode of modulation.
1173 * Goal is to find lowest expected rate (under perfect conditions) that is
1174 * above the current measured throughput of "active" mode, to give new mode
1175 * a fair chance to prove itself without too many challenges.
1176 *
1177 * This gets called when transitioning to more aggressive modulation
1178 * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
1179 * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
1180 * to decrease to match "active" throughput. When moving from MIMO to SISO,
1181 * bit rate will typically need to increase, but not if performance was bad.
1182 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001183static s32 rs_get_best_rate(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001184 struct iwl_lq_sta *lq_sta,
1185 struct iwl_scale_tbl_info *tbl, /* "search" */
Guy Cohenf935a6d2008-04-23 17:15:02 -07001186 u16 rate_mask, s8 index)
Zhu Yib481de92007-09-25 17:54:57 -07001187{
Ben Cahill77626352007-11-29 11:09:44 +08001188 /* "active" values */
Tomas Winklere227cea2008-07-18 13:53:05 +08001189 struct iwl_scale_tbl_info *active_tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001190 &(lq_sta->lq_info[lq_sta->active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07001191 s32 active_sr = active_tbl->win[index].success_ratio;
Zhu Yib481de92007-09-25 17:54:57 -07001192 s32 active_tpt = active_tbl->expected_tpt[index];
Ben Cahill77626352007-11-29 11:09:44 +08001193
1194 /* expected "search" throughput */
1195 s32 *tpt_tbl = tbl->expected_tpt;
1196
1197 s32 new_rate, high, low, start_hi;
Zhu Yib481de92007-09-25 17:54:57 -07001198 u16 high_low;
Guy Cohenf935a6d2008-04-23 17:15:02 -07001199 s8 rate = index;
Zhu Yib481de92007-09-25 17:54:57 -07001200
1201 new_rate = high = low = start_hi = IWL_RATE_INVALID;
1202
1203 for (; ;) {
Ester Kummerbf403db2008-05-05 10:22:40 +08001204 high_low = rs_get_adjacent_rate(priv, rate, rate_mask,
1205 tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -07001206
1207 low = high_low & 0xff;
1208 high = (high_low >> 8) & 0xff;
1209
Ben Cahill77626352007-11-29 11:09:44 +08001210 /*
1211 * Lower the "search" bit rate, to give new "search" mode
1212 * approximately the same throughput as "active" if:
1213 *
1214 * 1) "Active" mode has been working modestly well (but not
1215 * great), and expected "search" throughput (under perfect
1216 * conditions) at candidate rate is above the actual
1217 * measured "active" throughput (but less than expected
1218 * "active" throughput under perfect conditions).
1219 * OR
1220 * 2) "Active" mode has been working perfectly or very well
1221 * and expected "search" throughput (under perfect
1222 * conditions) at candidate rate is above expected
1223 * "active" throughput (under perfect conditions).
1224 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001225 if ((((100 * tpt_tbl[rate]) > lq_sta->last_tpt) &&
Zhu Yib481de92007-09-25 17:54:57 -07001226 ((active_sr > IWL_RATE_DECREASE_TH) &&
1227 (active_sr <= IWL_RATE_HIGH_TH) &&
1228 (tpt_tbl[rate] <= active_tpt))) ||
1229 ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
1230 (tpt_tbl[rate] > active_tpt))) {
1231
Ben Cahill77626352007-11-29 11:09:44 +08001232 /* (2nd or later pass)
1233 * If we've already tried to raise the rate, and are
1234 * now trying to lower it, use the higher rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001235 if (start_hi != IWL_RATE_INVALID) {
1236 new_rate = start_hi;
1237 break;
1238 }
Ben Cahill77626352007-11-29 11:09:44 +08001239
Zhu Yib481de92007-09-25 17:54:57 -07001240 new_rate = rate;
Ben Cahill77626352007-11-29 11:09:44 +08001241
1242 /* Loop again with lower rate */
Zhu Yib481de92007-09-25 17:54:57 -07001243 if (low != IWL_RATE_INVALID)
1244 rate = low;
Ben Cahill77626352007-11-29 11:09:44 +08001245
1246 /* Lower rate not available, use the original */
Zhu Yib481de92007-09-25 17:54:57 -07001247 else
1248 break;
Ben Cahill77626352007-11-29 11:09:44 +08001249
1250 /* Else try to raise the "search" rate to match "active" */
Zhu Yib481de92007-09-25 17:54:57 -07001251 } else {
Ben Cahill77626352007-11-29 11:09:44 +08001252 /* (2nd or later pass)
1253 * If we've already tried to lower the rate, and are
1254 * now trying to raise it, use the lower rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001255 if (new_rate != IWL_RATE_INVALID)
1256 break;
Ben Cahill77626352007-11-29 11:09:44 +08001257
1258 /* Loop again with higher rate */
Zhu Yib481de92007-09-25 17:54:57 -07001259 else if (high != IWL_RATE_INVALID) {
1260 start_hi = high;
1261 rate = high;
Ben Cahill77626352007-11-29 11:09:44 +08001262
1263 /* Higher rate not available, use the original */
Zhu Yib481de92007-09-25 17:54:57 -07001264 } else {
Guy Cohen90d77952008-09-09 10:54:53 +08001265 new_rate = rate;
Zhu Yib481de92007-09-25 17:54:57 -07001266 break;
1267 }
1268 }
1269 }
1270
1271 return new_rate;
1272}
Zhu Yib481de92007-09-25 17:54:57 -07001273
Ben Cahill77626352007-11-29 11:09:44 +08001274/*
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001275 * Set up search table for MIMO2
Ben Cahill77626352007-11-29 11:09:44 +08001276 */
Guy Cohenfde0db32008-04-21 15:42:01 -07001277static int rs_switch_to_mimo2(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001278 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001279 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001280 struct ieee80211_sta *sta,
Tomas Winklere227cea2008-07-18 13:53:05 +08001281 struct iwl_scale_tbl_info *tbl, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001282{
Zhu Yib481de92007-09-25 17:54:57 -07001283 u16 rate_mask;
1284 s32 rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001285 s8 is_green = lq_sta->is_green;
Johannes Berg7e6a5882010-08-23 10:46:46 +02001286 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
Johannes Bergc6892902011-09-20 15:37:21 -07001287 struct iwl_rxon_context *ctx = sta_priv->ctx;
Zhu Yib481de92007-09-25 17:54:57 -07001288
Luis R. Rodriguezde27e642008-12-23 15:58:44 -08001289 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
Zhu Yib481de92007-09-25 17:54:57 -07001290 return -1;
1291
Johannes Bergd9fe60d2008-10-09 12:13:49 +02001292 if (((sta->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 2)
Tomas Winkler00c5ae22008-09-03 11:26:42 +08001293 == WLAN_HT_CAP_SM_PS_STATIC)
Zhu Yib481de92007-09-25 17:54:57 -07001294 return -1;
1295
Ben Cahill77626352007-11-29 11:09:44 +08001296 /* Need both Tx chains/antennas to support MIMO */
Johannes Berg9e295112012-04-09 17:46:55 -07001297 if (priv->hw_params.tx_chains_num < 2)
Zhu Yib481de92007-09-25 17:54:57 -07001298 return -1;
1299
Tomas Winklere1623442009-01-27 14:27:56 -08001300 IWL_DEBUG_RATE(priv, "LQ: try to switch to MIMO2\n");
Guy Cohen39e88502008-04-23 17:14:57 -07001301
1302 tbl->lq_type = LQ_MIMO2;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001303 tbl->is_dup = lq_sta->is_dup;
Zhu Yib481de92007-09-25 17:54:57 -07001304 tbl->action = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001305 tbl->max_search = IWL_MAX_SEARCH;
Guy Cohen39e88502008-04-23 17:14:57 -07001306 rate_mask = lq_sta->active_mimo2_rate;
1307
Johannes Berg7e6a5882010-08-23 10:46:46 +02001308 if (iwl_is_ht40_tx_allowed(priv, ctx, &sta->ht_cap))
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001309 tbl->is_ht40 = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001310 else
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001311 tbl->is_ht40 = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001312
Guy Coheneecd6e52008-04-23 17:14:58 -07001313 rs_set_expected_tpt_table(lq_sta, tbl);
Zhu Yib481de92007-09-25 17:54:57 -07001314
Guy Cohenf935a6d2008-04-23 17:15:02 -07001315 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
Zhu Yib481de92007-09-25 17:54:57 -07001316
Tomas Winklere1623442009-01-27 14:27:56 -08001317 IWL_DEBUG_RATE(priv, "LQ: MIMO2 best rate %d mask %X\n", rate, rate_mask);
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001318 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1319 IWL_DEBUG_RATE(priv, "Can't switch with index %d rate mask %x\n",
1320 rate, rate_mask);
1321 return -1;
1322 }
1323 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
Guy Cohen39e88502008-04-23 17:14:57 -07001324
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001325 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
1326 tbl->current_rate, is_green);
1327 return 0;
1328}
1329
1330/*
1331 * Set up search table for MIMO3
1332 */
1333static int rs_switch_to_mimo3(struct iwl_priv *priv,
1334 struct iwl_lq_sta *lq_sta,
1335 struct ieee80211_conf *conf,
1336 struct ieee80211_sta *sta,
1337 struct iwl_scale_tbl_info *tbl, int index)
1338{
1339 u16 rate_mask;
1340 s32 rate;
1341 s8 is_green = lq_sta->is_green;
Johannes Berg7e6a5882010-08-23 10:46:46 +02001342 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
Johannes Bergc6892902011-09-20 15:37:21 -07001343 struct iwl_rxon_context *ctx = sta_priv->ctx;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001344
1345 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
1346 return -1;
1347
1348 if (((sta->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 2)
1349 == WLAN_HT_CAP_SM_PS_STATIC)
1350 return -1;
1351
1352 /* Need both Tx chains/antennas to support MIMO */
Johannes Berg9e295112012-04-09 17:46:55 -07001353 if (priv->hw_params.tx_chains_num < 3)
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001354 return -1;
1355
1356 IWL_DEBUG_RATE(priv, "LQ: try to switch to MIMO3\n");
1357
1358 tbl->lq_type = LQ_MIMO3;
1359 tbl->is_dup = lq_sta->is_dup;
1360 tbl->action = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001361 tbl->max_search = IWL_MAX_11N_MIMO3_SEARCH;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001362 rate_mask = lq_sta->active_mimo3_rate;
1363
Johannes Berg7e6a5882010-08-23 10:46:46 +02001364 if (iwl_is_ht40_tx_allowed(priv, ctx, &sta->ht_cap))
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001365 tbl->is_ht40 = 1;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001366 else
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001367 tbl->is_ht40 = 0;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001368
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001369 rs_set_expected_tpt_table(lq_sta, tbl);
1370
1371 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
1372
1373 IWL_DEBUG_RATE(priv, "LQ: MIMO3 best rate %d mask %X\n",
1374 rate, rate_mask);
Guy Cohen39e88502008-04-23 17:14:57 -07001375 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
Tomas Winklere1623442009-01-27 14:27:56 -08001376 IWL_DEBUG_RATE(priv, "Can't switch with index %d rate mask %x\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001377 rate, rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07001378 return -1;
Guy Cohen39e88502008-04-23 17:14:57 -07001379 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001380 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
Zhu Yib481de92007-09-25 17:54:57 -07001381
Tomas Winklere1623442009-01-27 14:27:56 -08001382 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001383 tbl->current_rate, is_green);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001384 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07001385}
1386
Ben Cahill77626352007-11-29 11:09:44 +08001387/*
1388 * Set up search table for SISO
1389 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001390static int rs_switch_to_siso(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001391 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001392 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001393 struct ieee80211_sta *sta,
Tomas Winklere227cea2008-07-18 13:53:05 +08001394 struct iwl_scale_tbl_info *tbl, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001395{
Zhu Yib481de92007-09-25 17:54:57 -07001396 u16 rate_mask;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001397 u8 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07001398 s32 rate;
Johannes Berg7e6a5882010-08-23 10:46:46 +02001399 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
Johannes Bergc6892902011-09-20 15:37:21 -07001400 struct iwl_rxon_context *ctx = sta_priv->ctx;
Zhu Yib481de92007-09-25 17:54:57 -07001401
Luis R. Rodriguezde27e642008-12-23 15:58:44 -08001402 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
Zhu Yib481de92007-09-25 17:54:57 -07001403 return -1;
1404
Tomas Winklere1623442009-01-27 14:27:56 -08001405 IWL_DEBUG_RATE(priv, "LQ: try to switch to SISO\n");
Guy Cohen39e88502008-04-23 17:14:57 -07001406
Tomas Winklerc33104f2008-01-14 17:46:21 -08001407 tbl->is_dup = lq_sta->is_dup;
Zhu Yib481de92007-09-25 17:54:57 -07001408 tbl->lq_type = LQ_SISO;
1409 tbl->action = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001410 tbl->max_search = IWL_MAX_SEARCH;
Guy Cohen39e88502008-04-23 17:14:57 -07001411 rate_mask = lq_sta->active_siso_rate;
Zhu Yib481de92007-09-25 17:54:57 -07001412
Johannes Berg7e6a5882010-08-23 10:46:46 +02001413 if (iwl_is_ht40_tx_allowed(priv, ctx, &sta->ht_cap))
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001414 tbl->is_ht40 = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001415 else
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001416 tbl->is_ht40 = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001417
Zhu Yib481de92007-09-25 17:54:57 -07001418 if (is_green)
Guy Cohen39e88502008-04-23 17:14:57 -07001419 tbl->is_SGI = 0; /*11n spec: no SGI in SISO+Greenfield*/
Zhu Yib481de92007-09-25 17:54:57 -07001420
Guy Coheneecd6e52008-04-23 17:14:58 -07001421 rs_set_expected_tpt_table(lq_sta, tbl);
Guy Cohenf935a6d2008-04-23 17:15:02 -07001422 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
Zhu Yib481de92007-09-25 17:54:57 -07001423
Tomas Winklere1623442009-01-27 14:27:56 -08001424 IWL_DEBUG_RATE(priv, "LQ: get best rate %d mask %X\n", rate, rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07001425 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
Tomas Winklere1623442009-01-27 14:27:56 -08001426 IWL_DEBUG_RATE(priv, "can not switch with index %d rate mask %x\n",
Zhu Yib481de92007-09-25 17:54:57 -07001427 rate, rate_mask);
1428 return -1;
1429 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001430 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
Tomas Winklere1623442009-01-27 14:27:56 -08001431 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001432 tbl->current_rate, is_green);
Mohamed Abbas403ab562007-11-06 22:06:25 -08001433 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07001434}
1435
Ben Cahill77626352007-11-29 11:09:44 +08001436/*
1437 * Try to switch to new modulation mode from legacy
1438 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001439static int rs_move_legacy_other(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001440 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001441 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001442 struct ieee80211_sta *sta,
Zhu Yib481de92007-09-25 17:54:57 -07001443 int index)
1444{
Tomas Winklere227cea2008-07-18 13:53:05 +08001445 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1446 struct iwl_scale_tbl_info *search_tbl =
1447 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1448 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1449 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1450 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Wey-Yi Guyb1a78852010-07-31 08:52:03 -07001451 u8 start_action;
Eytan Lifshitzb7998c82012-12-01 20:59:49 +02001452 u8 valid_tx_ant = priv->nvm_data->valid_tx_ant;
Johannes Berg9e295112012-04-09 17:46:55 -07001453 u8 tx_chains_num = priv->hw_params.tx_chains_num;
Guy Cohenfde0db32008-04-21 15:42:01 -07001454 int ret = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001455 u8 update_search_tbl_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001456
Johannes Berg290f5992010-08-23 07:56:58 -07001457 switch (priv->bt_traffic_load) {
1458 case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
1459 /* nothing */
1460 break;
1461 case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
1462 /* avoid antenna B unless MIMO */
1463 if (tbl->action == IWL_LEGACY_SWITCH_ANTENNA2)
Venkataraman, Meenakshi383b0872011-11-10 06:55:25 -08001464 tbl->action = IWL_LEGACY_SWITCH_SISO;
Johannes Berg290f5992010-08-23 07:56:58 -07001465 break;
1466 case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
1467 case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
1468 /* avoid antenna B and MIMO */
Emmanuel Grumbachd6189122011-08-25 23:10:39 -07001469 valid_tx_ant =
Eytan Lifshitzb7998c82012-12-01 20:59:49 +02001470 first_antenna(priv->nvm_data->valid_tx_ant);
Johannes Berg290f5992010-08-23 07:56:58 -07001471 if (tbl->action >= IWL_LEGACY_SWITCH_ANTENNA2 &&
1472 tbl->action != IWL_LEGACY_SWITCH_SISO)
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001473 tbl->action = IWL_LEGACY_SWITCH_SISO;
Johannes Berg290f5992010-08-23 07:56:58 -07001474 break;
1475 default:
1476 IWL_ERR(priv, "Invalid BT load %d", priv->bt_traffic_load);
1477 break;
1478 }
1479
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001480 if (!iwl_ht_enabled(priv))
1481 /* stay in Legacy */
1482 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
Johannes Berg3ad3b922009-08-07 15:41:48 -07001483 else if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001484 tbl->action > IWL_LEGACY_SWITCH_SISO)
1485 tbl->action = IWL_LEGACY_SWITCH_SISO;
Wey-Yi Guybee008b2010-08-23 07:57:04 -07001486
1487 /* configure as 1x1 if bt full concurrency */
1488 if (priv->bt_full_concurrent) {
1489 if (!iwl_ht_enabled(priv))
1490 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
1491 else if (tbl->action >= IWL_LEGACY_SWITCH_ANTENNA2)
1492 tbl->action = IWL_LEGACY_SWITCH_SISO;
Emmanuel Grumbachd6189122011-08-25 23:10:39 -07001493 valid_tx_ant =
Eytan Lifshitzb7998c82012-12-01 20:59:49 +02001494 first_antenna(priv->nvm_data->valid_tx_ant);
Wey-Yi Guybee008b2010-08-23 07:57:04 -07001495 }
1496
Wey-Yi Guyb1a78852010-07-31 08:52:03 -07001497 start_action = tbl->action;
Zhu Yib481de92007-09-25 17:54:57 -07001498 for (; ;) {
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001499 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001500 switch (tbl->action) {
Guy Cohen3110bef2008-09-09 10:54:54 +08001501 case IWL_LEGACY_SWITCH_ANTENNA1:
1502 case IWL_LEGACY_SWITCH_ANTENNA2:
Tomas Winklere1623442009-01-27 14:27:56 -08001503 IWL_DEBUG_RATE(priv, "LQ: Legacy toggle Antenna\n");
Zhu Yib481de92007-09-25 17:54:57 -07001504
Guy Cohen3110bef2008-09-09 10:54:54 +08001505 if ((tbl->action == IWL_LEGACY_SWITCH_ANTENNA1 &&
1506 tx_chains_num <= 1) ||
1507 (tbl->action == IWL_LEGACY_SWITCH_ANTENNA2 &&
1508 tx_chains_num <= 2))
1509 break;
1510
Ben Cahill77626352007-11-29 11:09:44 +08001511 /* Don't change antenna if success has been great */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001512 if (window->success_ratio >= IWL_RS_GOOD_RATIO &&
1513 !priv->bt_full_concurrent &&
1514 priv->bt_traffic_load ==
1515 IWL_BT_COEX_TRAFFIC_LOAD_NONE)
Zhu Yib481de92007-09-25 17:54:57 -07001516 break;
Ben Cahill77626352007-11-29 11:09:44 +08001517
Ben Cahill77626352007-11-29 11:09:44 +08001518 /* Set up search table to try other antenna */
Zhu Yib481de92007-09-25 17:54:57 -07001519 memcpy(search_tbl, tbl, sz);
1520
Guy Cohenaade00c2008-04-23 17:14:59 -07001521 if (rs_toggle_antenna(valid_tx_ant,
1522 &search_tbl->current_rate, search_tbl)) {
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001523 update_search_tbl_counter = 1;
Guy Cohen3110bef2008-09-09 10:54:54 +08001524 rs_set_expected_tpt_table(lq_sta, search_tbl);
Guy Cohenaade00c2008-04-23 17:14:59 -07001525 goto out;
1526 }
Guy Cohena5e8b502008-05-29 16:35:11 +08001527 break;
Zhu Yib481de92007-09-25 17:54:57 -07001528 case IWL_LEGACY_SWITCH_SISO:
Tomas Winklere1623442009-01-27 14:27:56 -08001529 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to SISO\n");
Ben Cahill77626352007-11-29 11:09:44 +08001530
1531 /* Set up search table to try SISO */
Zhu Yib481de92007-09-25 17:54:57 -07001532 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001533 search_tbl->is_SGI = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001534 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001535 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001536 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001537 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001538 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001539 }
Zhu Yib481de92007-09-25 17:54:57 -07001540
1541 break;
Guy Cohen3110bef2008-09-09 10:54:54 +08001542 case IWL_LEGACY_SWITCH_MIMO2_AB:
1543 case IWL_LEGACY_SWITCH_MIMO2_AC:
1544 case IWL_LEGACY_SWITCH_MIMO2_BC:
Tomas Winklere1623442009-01-27 14:27:56 -08001545 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to MIMO2\n");
Ben Cahill77626352007-11-29 11:09:44 +08001546
1547 /* Set up search table to try MIMO */
Zhu Yib481de92007-09-25 17:54:57 -07001548 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001549 search_tbl->is_SGI = 0;
Guy Cohen3110bef2008-09-09 10:54:54 +08001550
1551 if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AB)
1552 search_tbl->ant_type = ANT_AB;
1553 else if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AC)
1554 search_tbl->ant_type = ANT_AC;
1555 else
1556 search_tbl->ant_type = ANT_BC;
1557
1558 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1559 break;
1560
Guy Cohenfde0db32008-04-21 15:42:01 -07001561 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001562 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001563 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001564 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001565 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001566 }
Zhu Yib481de92007-09-25 17:54:57 -07001567 break;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001568
1569 case IWL_LEGACY_SWITCH_MIMO3_ABC:
1570 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to MIMO3\n");
1571
1572 /* Set up search table to try MIMO3 */
1573 memcpy(search_tbl, tbl, sz);
1574 search_tbl->is_SGI = 0;
1575
1576 search_tbl->ant_type = ANT_ABC;
1577
1578 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1579 break;
1580
1581 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1582 search_tbl, index);
1583 if (!ret) {
1584 lq_sta->action_counter = 0;
1585 goto out;
1586 }
1587 break;
Zhu Yib481de92007-09-25 17:54:57 -07001588 }
1589 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001590 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001591 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
Zhu Yib481de92007-09-25 17:54:57 -07001592
1593 if (tbl->action == start_action)
1594 break;
1595
1596 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001597 search_tbl->lq_type = LQ_NONE;
Zhu Yib481de92007-09-25 17:54:57 -07001598 return 0;
1599
Guy Cohen3110bef2008-09-09 10:54:54 +08001600out:
1601 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001602 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001603 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001604 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001605 if (update_search_tbl_counter)
1606 search_tbl->action = tbl->action;
Zhu Yib481de92007-09-25 17:54:57 -07001607 return 0;
1608
1609}
1610
Ben Cahill77626352007-11-29 11:09:44 +08001611/*
1612 * Try to switch to new modulation mode from SISO
1613 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001614static int rs_move_siso_to_other(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001615 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001616 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001617 struct ieee80211_sta *sta, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001618{
Tomas Winklerc33104f2008-01-14 17:46:21 -08001619 u8 is_green = lq_sta->is_green;
Tomas Winklere227cea2008-07-18 13:53:05 +08001620 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1621 struct iwl_scale_tbl_info *search_tbl =
1622 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1623 struct iwl_rate_scale_data *window = &(tbl->win[index]);
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001624 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Tomas Winklere227cea2008-07-18 13:53:05 +08001625 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1626 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Wey-Yi Guyb1a78852010-07-31 08:52:03 -07001627 u8 start_action;
Eytan Lifshitzb7998c82012-12-01 20:59:49 +02001628 u8 valid_tx_ant = priv->nvm_data->valid_tx_ant;
Johannes Berg9e295112012-04-09 17:46:55 -07001629 u8 tx_chains_num = priv->hw_params.tx_chains_num;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001630 u8 update_search_tbl_counter = 0;
Guy Cohenfde0db32008-04-21 15:42:01 -07001631 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001632
Johannes Berg290f5992010-08-23 07:56:58 -07001633 switch (priv->bt_traffic_load) {
1634 case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
1635 /* nothing */
1636 break;
1637 case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
1638 /* avoid antenna B unless MIMO */
1639 if (tbl->action == IWL_SISO_SWITCH_ANTENNA2)
Venkataraman, Meenakshi383b0872011-11-10 06:55:25 -08001640 tbl->action = IWL_SISO_SWITCH_MIMO2_AB;
Johannes Berg290f5992010-08-23 07:56:58 -07001641 break;
1642 case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
1643 case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
1644 /* avoid antenna B and MIMO */
Emmanuel Grumbachd6189122011-08-25 23:10:39 -07001645 valid_tx_ant =
Eytan Lifshitzb7998c82012-12-01 20:59:49 +02001646 first_antenna(priv->nvm_data->valid_tx_ant);
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001647 if (tbl->action != IWL_SISO_SWITCH_ANTENNA1)
Johannes Berg290f5992010-08-23 07:56:58 -07001648 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1649 break;
1650 default:
1651 IWL_ERR(priv, "Invalid BT load %d", priv->bt_traffic_load);
1652 break;
1653 }
1654
Johannes Berg3ad3b922009-08-07 15:41:48 -07001655 if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001656 tbl->action > IWL_SISO_SWITCH_ANTENNA2) {
1657 /* stay in SISO */
1658 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1659 }
Wey-Yi Guybee008b2010-08-23 07:57:04 -07001660
1661 /* configure as 1x1 if bt full concurrency */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001662 if (priv->bt_full_concurrent) {
Emmanuel Grumbachd6189122011-08-25 23:10:39 -07001663 valid_tx_ant =
Eytan Lifshitzb7998c82012-12-01 20:59:49 +02001664 first_antenna(priv->nvm_data->valid_tx_ant);
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001665 if (tbl->action >= IWL_LEGACY_SWITCH_ANTENNA2)
1666 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1667 }
Wey-Yi Guybee008b2010-08-23 07:57:04 -07001668
Wey-Yi Guyb1a78852010-07-31 08:52:03 -07001669 start_action = tbl->action;
Zhu Yib481de92007-09-25 17:54:57 -07001670 for (;;) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001671 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001672 switch (tbl->action) {
Guy Cohen3110bef2008-09-09 10:54:54 +08001673 case IWL_SISO_SWITCH_ANTENNA1:
1674 case IWL_SISO_SWITCH_ANTENNA2:
Tomas Winklere1623442009-01-27 14:27:56 -08001675 IWL_DEBUG_RATE(priv, "LQ: SISO toggle Antenna\n");
Guy Cohen3110bef2008-09-09 10:54:54 +08001676 if ((tbl->action == IWL_SISO_SWITCH_ANTENNA1 &&
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001677 tx_chains_num <= 1) ||
Guy Cohen3110bef2008-09-09 10:54:54 +08001678 (tbl->action == IWL_SISO_SWITCH_ANTENNA2 &&
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001679 tx_chains_num <= 2))
Guy Cohen3110bef2008-09-09 10:54:54 +08001680 break;
1681
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001682 if (window->success_ratio >= IWL_RS_GOOD_RATIO &&
1683 !priv->bt_full_concurrent &&
1684 priv->bt_traffic_load ==
1685 IWL_BT_COEX_TRAFFIC_LOAD_NONE)
Zhu Yib481de92007-09-25 17:54:57 -07001686 break;
Zhu Yib481de92007-09-25 17:54:57 -07001687
1688 memcpy(search_tbl, tbl, sz);
Guy Cohenaade00c2008-04-23 17:14:59 -07001689 if (rs_toggle_antenna(valid_tx_ant,
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001690 &search_tbl->current_rate, search_tbl)) {
1691 update_search_tbl_counter = 1;
Guy Cohenaade00c2008-04-23 17:14:59 -07001692 goto out;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001693 }
Guy Cohena5e8b502008-05-29 16:35:11 +08001694 break;
Guy Cohen3110bef2008-09-09 10:54:54 +08001695 case IWL_SISO_SWITCH_MIMO2_AB:
1696 case IWL_SISO_SWITCH_MIMO2_AC:
1697 case IWL_SISO_SWITCH_MIMO2_BC:
Tomas Winklere1623442009-01-27 14:27:56 -08001698 IWL_DEBUG_RATE(priv, "LQ: SISO switch to MIMO2\n");
Zhu Yib481de92007-09-25 17:54:57 -07001699 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001700 search_tbl->is_SGI = 0;
Guy Cohen3110bef2008-09-09 10:54:54 +08001701
1702 if (tbl->action == IWL_SISO_SWITCH_MIMO2_AB)
1703 search_tbl->ant_type = ANT_AB;
1704 else if (tbl->action == IWL_SISO_SWITCH_MIMO2_AC)
1705 search_tbl->ant_type = ANT_AC;
1706 else
1707 search_tbl->ant_type = ANT_BC;
1708
1709 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1710 break;
1711
Guy Cohenfde0db32008-04-21 15:42:01 -07001712 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001713 search_tbl, index);
Guy Cohen3110bef2008-09-09 10:54:54 +08001714 if (!ret)
Zhu Yib481de92007-09-25 17:54:57 -07001715 goto out;
1716 break;
1717 case IWL_SISO_SWITCH_GI:
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001718 if (!tbl->is_ht40 && !(ht_cap->cap &
1719 IEEE80211_HT_CAP_SGI_20))
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001720 break;
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001721 if (tbl->is_ht40 && !(ht_cap->cap &
1722 IEEE80211_HT_CAP_SGI_40))
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001723 break;
1724
Tomas Winklere1623442009-01-27 14:27:56 -08001725 IWL_DEBUG_RATE(priv, "LQ: SISO toggle SGI/NGI\n");
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001726
Zhu Yib481de92007-09-25 17:54:57 -07001727 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001728 if (is_green) {
1729 if (!tbl->is_SGI)
1730 break;
1731 else
Winkler, Tomas15b16872008-12-19 10:37:33 +08001732 IWL_ERR(priv,
1733 "SGI was set in GF+SISO\n");
Zhu Yib481de92007-09-25 17:54:57 -07001734 }
Guy Cohen39e88502008-04-23 17:14:57 -07001735 search_tbl->is_SGI = !tbl->is_SGI;
Guy Coheneecd6e52008-04-23 17:14:58 -07001736 rs_set_expected_tpt_table(lq_sta, search_tbl);
Guy Cohen39e88502008-04-23 17:14:57 -07001737 if (tbl->is_SGI) {
1738 s32 tpt = lq_sta->last_tpt / 100;
1739 if (tpt >= search_tbl->expected_tpt[index])
1740 break;
1741 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001742 search_tbl->current_rate =
1743 rate_n_flags_from_tbl(priv, search_tbl,
1744 index, is_green);
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001745 update_search_tbl_counter = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001746 goto out;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001747 case IWL_SISO_SWITCH_MIMO3_ABC:
1748 IWL_DEBUG_RATE(priv, "LQ: SISO switch to MIMO3\n");
1749 memcpy(search_tbl, tbl, sz);
1750 search_tbl->is_SGI = 0;
1751 search_tbl->ant_type = ANT_ABC;
1752
1753 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1754 break;
1755
1756 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1757 search_tbl, index);
1758 if (!ret)
1759 goto out;
1760 break;
Zhu Yib481de92007-09-25 17:54:57 -07001761 }
1762 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001763 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001764 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
Zhu Yib481de92007-09-25 17:54:57 -07001765
1766 if (tbl->action == start_action)
1767 break;
1768 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001769 search_tbl->lq_type = LQ_NONE;
Zhu Yib481de92007-09-25 17:54:57 -07001770 return 0;
1771
1772 out:
Guy Cohen3110bef2008-09-09 10:54:54 +08001773 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001774 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001775 if (tbl->action > IWL_SISO_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001776 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001777 if (update_search_tbl_counter)
1778 search_tbl->action = tbl->action;
1779
Zhu Yib481de92007-09-25 17:54:57 -07001780 return 0;
1781}
1782
Ben Cahill77626352007-11-29 11:09:44 +08001783/*
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001784 * Try to switch to new modulation mode from MIMO2
Ben Cahill77626352007-11-29 11:09:44 +08001785 */
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001786static int rs_move_mimo2_to_other(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001787 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001788 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001789 struct ieee80211_sta *sta, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001790{
Tomas Winklerc33104f2008-01-14 17:46:21 -08001791 s8 is_green = lq_sta->is_green;
Tomas Winklere227cea2008-07-18 13:53:05 +08001792 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1793 struct iwl_scale_tbl_info *search_tbl =
1794 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Guy Cohen3110bef2008-09-09 10:54:54 +08001795 struct iwl_rate_scale_data *window = &(tbl->win[index]);
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001796 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Tomas Winklere227cea2008-07-18 13:53:05 +08001797 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1798 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Wey-Yi Guyb1a78852010-07-31 08:52:03 -07001799 u8 start_action;
Eytan Lifshitzb7998c82012-12-01 20:59:49 +02001800 u8 valid_tx_ant = priv->nvm_data->valid_tx_ant;
Johannes Berg9e295112012-04-09 17:46:55 -07001801 u8 tx_chains_num = priv->hw_params.tx_chains_num;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001802 u8 update_search_tbl_counter = 0;
Guy Cohenaade00c2008-04-23 17:14:59 -07001803 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001804
Johannes Berg290f5992010-08-23 07:56:58 -07001805 switch (priv->bt_traffic_load) {
1806 case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
1807 /* nothing */
1808 break;
1809 case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
1810 case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
1811 /* avoid antenna B and MIMO */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001812 if (tbl->action != IWL_MIMO2_SWITCH_SISO_A)
1813 tbl->action = IWL_MIMO2_SWITCH_SISO_A;
1814 break;
Johannes Berg290f5992010-08-23 07:56:58 -07001815 case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
1816 /* avoid antenna B unless MIMO */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001817 if (tbl->action == IWL_MIMO2_SWITCH_SISO_B ||
1818 tbl->action == IWL_MIMO2_SWITCH_SISO_C)
Johannes Berg290f5992010-08-23 07:56:58 -07001819 tbl->action = IWL_MIMO2_SWITCH_SISO_A;
1820 break;
1821 default:
1822 IWL_ERR(priv, "Invalid BT load %d", priv->bt_traffic_load);
1823 break;
1824 }
1825
Johannes Berg3ad3b922009-08-07 15:41:48 -07001826 if ((iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE) &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001827 (tbl->action < IWL_MIMO2_SWITCH_SISO_A ||
1828 tbl->action > IWL_MIMO2_SWITCH_SISO_C)) {
1829 /* switch in SISO */
1830 tbl->action = IWL_MIMO2_SWITCH_SISO_A;
1831 }
Wey-Yi Guybee008b2010-08-23 07:57:04 -07001832
1833 /* configure as 1x1 if bt full concurrency */
1834 if (priv->bt_full_concurrent &&
1835 (tbl->action < IWL_MIMO2_SWITCH_SISO_A ||
1836 tbl->action > IWL_MIMO2_SWITCH_SISO_C))
1837 tbl->action = IWL_MIMO2_SWITCH_SISO_A;
1838
Wey-Yi Guyb1a78852010-07-31 08:52:03 -07001839 start_action = tbl->action;
Zhu Yib481de92007-09-25 17:54:57 -07001840 for (;;) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001841 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001842 switch (tbl->action) {
Guy Cohen3110bef2008-09-09 10:54:54 +08001843 case IWL_MIMO2_SWITCH_ANTENNA1:
1844 case IWL_MIMO2_SWITCH_ANTENNA2:
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001845 IWL_DEBUG_RATE(priv, "LQ: MIMO2 toggle Antennas\n");
Guy Cohen3110bef2008-09-09 10:54:54 +08001846
1847 if (tx_chains_num <= 2)
1848 break;
1849
1850 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1851 break;
1852
1853 memcpy(search_tbl, tbl, sz);
1854 if (rs_toggle_antenna(valid_tx_ant,
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001855 &search_tbl->current_rate, search_tbl)) {
1856 update_search_tbl_counter = 1;
Guy Cohen3110bef2008-09-09 10:54:54 +08001857 goto out;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001858 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001859 break;
1860 case IWL_MIMO2_SWITCH_SISO_A:
1861 case IWL_MIMO2_SWITCH_SISO_B:
1862 case IWL_MIMO2_SWITCH_SISO_C:
Tomas Winklere1623442009-01-27 14:27:56 -08001863 IWL_DEBUG_RATE(priv, "LQ: MIMO2 switch to SISO\n");
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001864
Ben Cahill77626352007-11-29 11:09:44 +08001865 /* Set up new search table for SISO */
Zhu Yib481de92007-09-25 17:54:57 -07001866 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001867
Guy Cohen3110bef2008-09-09 10:54:54 +08001868 if (tbl->action == IWL_MIMO2_SWITCH_SISO_A)
Guy Cohenfde0db32008-04-21 15:42:01 -07001869 search_tbl->ant_type = ANT_A;
Guy Cohen3110bef2008-09-09 10:54:54 +08001870 else if (tbl->action == IWL_MIMO2_SWITCH_SISO_B)
Guy Cohenfde0db32008-04-21 15:42:01 -07001871 search_tbl->ant_type = ANT_B;
Guy Cohen3110bef2008-09-09 10:54:54 +08001872 else
1873 search_tbl->ant_type = ANT_C;
1874
1875 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1876 break;
Zhu Yib481de92007-09-25 17:54:57 -07001877
Tomas Winklerc33104f2008-01-14 17:46:21 -08001878 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001879 search_tbl, index);
Guy Cohen3110bef2008-09-09 10:54:54 +08001880 if (!ret)
Zhu Yib481de92007-09-25 17:54:57 -07001881 goto out;
Guy Cohen3110bef2008-09-09 10:54:54 +08001882
Zhu Yib481de92007-09-25 17:54:57 -07001883 break;
1884
Guy Cohen3110bef2008-09-09 10:54:54 +08001885 case IWL_MIMO2_SWITCH_GI:
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001886 if (!tbl->is_ht40 && !(ht_cap->cap &
1887 IEEE80211_HT_CAP_SGI_20))
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001888 break;
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001889 if (tbl->is_ht40 && !(ht_cap->cap &
1890 IEEE80211_HT_CAP_SGI_40))
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001891 break;
1892
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001893 IWL_DEBUG_RATE(priv, "LQ: MIMO2 toggle SGI/NGI\n");
1894
1895 /* Set up new search table for MIMO2 */
1896 memcpy(search_tbl, tbl, sz);
1897 search_tbl->is_SGI = !tbl->is_SGI;
1898 rs_set_expected_tpt_table(lq_sta, search_tbl);
1899 /*
1900 * If active table already uses the fastest possible
1901 * modulation (dual stream with short guard interval),
1902 * and it's working well, there's no need to look
1903 * for a better type of modulation!
1904 */
1905 if (tbl->is_SGI) {
1906 s32 tpt = lq_sta->last_tpt / 100;
1907 if (tpt >= search_tbl->expected_tpt[index])
1908 break;
1909 }
1910 search_tbl->current_rate =
1911 rate_n_flags_from_tbl(priv, search_tbl,
1912 index, is_green);
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001913 update_search_tbl_counter = 1;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001914 goto out;
1915
1916 case IWL_MIMO2_SWITCH_MIMO3_ABC:
1917 IWL_DEBUG_RATE(priv, "LQ: MIMO2 switch to MIMO3\n");
1918 memcpy(search_tbl, tbl, sz);
1919 search_tbl->is_SGI = 0;
1920 search_tbl->ant_type = ANT_ABC;
1921
1922 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1923 break;
1924
1925 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1926 search_tbl, index);
1927 if (!ret)
1928 goto out;
1929
1930 break;
1931 }
1932 tbl->action++;
1933 if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
1934 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
1935
1936 if (tbl->action == start_action)
1937 break;
1938 }
1939 search_tbl->lq_type = LQ_NONE;
1940 return 0;
1941 out:
1942 lq_sta->search_better_tbl = 1;
1943 tbl->action++;
1944 if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
1945 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001946 if (update_search_tbl_counter)
1947 search_tbl->action = tbl->action;
1948
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001949 return 0;
1950
1951}
1952
1953/*
1954 * Try to switch to new modulation mode from MIMO3
1955 */
1956static int rs_move_mimo3_to_other(struct iwl_priv *priv,
1957 struct iwl_lq_sta *lq_sta,
1958 struct ieee80211_conf *conf,
1959 struct ieee80211_sta *sta, int index)
1960{
1961 s8 is_green = lq_sta->is_green;
1962 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1963 struct iwl_scale_tbl_info *search_tbl =
1964 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1965 struct iwl_rate_scale_data *window = &(tbl->win[index]);
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001966 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001967 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1968 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Wey-Yi Guyb1a78852010-07-31 08:52:03 -07001969 u8 start_action;
Eytan Lifshitzb7998c82012-12-01 20:59:49 +02001970 u8 valid_tx_ant = priv->nvm_data->valid_tx_ant;
Johannes Berg9e295112012-04-09 17:46:55 -07001971 u8 tx_chains_num = priv->hw_params.tx_chains_num;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001972 int ret;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001973 u8 update_search_tbl_counter = 0;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001974
Johannes Berg290f5992010-08-23 07:56:58 -07001975 switch (priv->bt_traffic_load) {
1976 case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
1977 /* nothing */
1978 break;
1979 case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
1980 case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
1981 /* avoid antenna B and MIMO */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001982 if (tbl->action != IWL_MIMO3_SWITCH_SISO_A)
Johannes Berg290f5992010-08-23 07:56:58 -07001983 tbl->action = IWL_MIMO3_SWITCH_SISO_A;
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001984 break;
Johannes Berg290f5992010-08-23 07:56:58 -07001985 case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
1986 /* avoid antenna B unless MIMO */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001987 if (tbl->action == IWL_MIMO3_SWITCH_SISO_B ||
1988 tbl->action == IWL_MIMO3_SWITCH_SISO_C)
Johannes Berg290f5992010-08-23 07:56:58 -07001989 tbl->action = IWL_MIMO3_SWITCH_SISO_A;
Johannes Berg290f5992010-08-23 07:56:58 -07001990 break;
1991 default:
1992 IWL_ERR(priv, "Invalid BT load %d", priv->bt_traffic_load);
1993 break;
1994 }
1995
Johannes Berg3ad3b922009-08-07 15:41:48 -07001996 if ((iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE) &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001997 (tbl->action < IWL_MIMO3_SWITCH_SISO_A ||
1998 tbl->action > IWL_MIMO3_SWITCH_SISO_C)) {
1999 /* switch in SISO */
2000 tbl->action = IWL_MIMO3_SWITCH_SISO_A;
2001 }
Wey-Yi Guybee008b2010-08-23 07:57:04 -07002002
2003 /* configure as 1x1 if bt full concurrency */
2004 if (priv->bt_full_concurrent &&
2005 (tbl->action < IWL_MIMO3_SWITCH_SISO_A ||
2006 tbl->action > IWL_MIMO3_SWITCH_SISO_C))
2007 tbl->action = IWL_MIMO3_SWITCH_SISO_A;
2008
Wey-Yi Guyb1a78852010-07-31 08:52:03 -07002009 start_action = tbl->action;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07002010 for (;;) {
2011 lq_sta->action_counter++;
2012 switch (tbl->action) {
2013 case IWL_MIMO3_SWITCH_ANTENNA1:
2014 case IWL_MIMO3_SWITCH_ANTENNA2:
2015 IWL_DEBUG_RATE(priv, "LQ: MIMO3 toggle Antennas\n");
2016
2017 if (tx_chains_num <= 3)
2018 break;
2019
2020 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
2021 break;
2022
2023 memcpy(search_tbl, tbl, sz);
2024 if (rs_toggle_antenna(valid_tx_ant,
2025 &search_tbl->current_rate, search_tbl))
2026 goto out;
2027 break;
2028 case IWL_MIMO3_SWITCH_SISO_A:
2029 case IWL_MIMO3_SWITCH_SISO_B:
2030 case IWL_MIMO3_SWITCH_SISO_C:
2031 IWL_DEBUG_RATE(priv, "LQ: MIMO3 switch to SISO\n");
2032
2033 /* Set up new search table for SISO */
2034 memcpy(search_tbl, tbl, sz);
2035
2036 if (tbl->action == IWL_MIMO3_SWITCH_SISO_A)
2037 search_tbl->ant_type = ANT_A;
2038 else if (tbl->action == IWL_MIMO3_SWITCH_SISO_B)
2039 search_tbl->ant_type = ANT_B;
2040 else
2041 search_tbl->ant_type = ANT_C;
2042
2043 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
2044 break;
2045
2046 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
2047 search_tbl, index);
2048 if (!ret)
2049 goto out;
2050
2051 break;
2052
2053 case IWL_MIMO3_SWITCH_MIMO2_AB:
2054 case IWL_MIMO3_SWITCH_MIMO2_AC:
2055 case IWL_MIMO3_SWITCH_MIMO2_BC:
2056 IWL_DEBUG_RATE(priv, "LQ: MIMO3 switch to MIMO2\n");
2057
2058 memcpy(search_tbl, tbl, sz);
2059 search_tbl->is_SGI = 0;
2060 if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AB)
2061 search_tbl->ant_type = ANT_AB;
2062 else if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AC)
2063 search_tbl->ant_type = ANT_AC;
2064 else
2065 search_tbl->ant_type = ANT_BC;
2066
2067 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
2068 break;
2069
2070 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
2071 search_tbl, index);
2072 if (!ret)
2073 goto out;
2074
2075 break;
2076
2077 case IWL_MIMO3_SWITCH_GI:
Daniel C Halperin28e6f482009-08-13 13:30:58 -07002078 if (!tbl->is_ht40 && !(ht_cap->cap &
2079 IEEE80211_HT_CAP_SGI_20))
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07002080 break;
Daniel C Halperin28e6f482009-08-13 13:30:58 -07002081 if (tbl->is_ht40 && !(ht_cap->cap &
2082 IEEE80211_HT_CAP_SGI_40))
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07002083 break;
2084
2085 IWL_DEBUG_RATE(priv, "LQ: MIMO3 toggle SGI/NGI\n");
Ben Cahill77626352007-11-29 11:09:44 +08002086
2087 /* Set up new search table for MIMO */
Zhu Yib481de92007-09-25 17:54:57 -07002088 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07002089 search_tbl->is_SGI = !tbl->is_SGI;
Guy Coheneecd6e52008-04-23 17:14:58 -07002090 rs_set_expected_tpt_table(lq_sta, search_tbl);
Ben Cahill77626352007-11-29 11:09:44 +08002091 /*
2092 * If active table already uses the fastest possible
2093 * modulation (dual stream with short guard interval),
2094 * and it's working well, there's no need to look
2095 * for a better type of modulation!
2096 */
Guy Cohen39e88502008-04-23 17:14:57 -07002097 if (tbl->is_SGI) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002098 s32 tpt = lq_sta->last_tpt / 100;
Guy Cohen39e88502008-04-23 17:14:57 -07002099 if (tpt >= search_tbl->expected_tpt[index])
2100 break;
Zhu Yib481de92007-09-25 17:54:57 -07002101 }
Tomas Winkler978785a2008-12-19 10:37:31 +08002102 search_tbl->current_rate =
2103 rate_n_flags_from_tbl(priv, search_tbl,
2104 index, is_green);
Mohamed Abbasd6e93392009-05-08 13:44:39 -07002105 update_search_tbl_counter = 1;
Zhu Yib481de92007-09-25 17:54:57 -07002106 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07002107 }
2108 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07002109 if (tbl->action > IWL_MIMO3_SWITCH_GI)
2110 tbl->action = IWL_MIMO3_SWITCH_ANTENNA1;
Zhu Yib481de92007-09-25 17:54:57 -07002111
2112 if (tbl->action == start_action)
2113 break;
2114 }
Guy Cohen3110bef2008-09-09 10:54:54 +08002115 search_tbl->lq_type = LQ_NONE;
Zhu Yib481de92007-09-25 17:54:57 -07002116 return 0;
2117 out:
Guy Cohen3110bef2008-09-09 10:54:54 +08002118 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07002119 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07002120 if (tbl->action > IWL_MIMO3_SWITCH_GI)
2121 tbl->action = IWL_MIMO3_SWITCH_ANTENNA1;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07002122 if (update_search_tbl_counter)
2123 search_tbl->action = tbl->action;
2124
Zhu Yib481de92007-09-25 17:54:57 -07002125 return 0;
2126
2127}
2128
Ben Cahill77626352007-11-29 11:09:44 +08002129/*
2130 * Check whether we should continue using same modulation mode, or
2131 * begin search for a new mode, based on:
2132 * 1) # tx successes or failures while using this mode
2133 * 2) # times calling this function
2134 * 3) elapsed time in this mode (not used, for now)
2135 */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07002136static void rs_stay_in_table(struct iwl_lq_sta *lq_sta, bool force_search)
Zhu Yib481de92007-09-25 17:54:57 -07002137{
Tomas Winklere227cea2008-07-18 13:53:05 +08002138 struct iwl_scale_tbl_info *tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002139 int i;
2140 int active_tbl;
2141 int flush_interval_passed = 0;
Ester Kummerbf403db2008-05-05 10:22:40 +08002142 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07002143
Ester Kummerbf403db2008-05-05 10:22:40 +08002144 priv = lq_sta->drv;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002145 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002146
Tomas Winklerc33104f2008-01-14 17:46:21 -08002147 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07002148
Ben Cahill77626352007-11-29 11:09:44 +08002149 /* If we've been disallowing search, see if we should now allow it */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002150 if (lq_sta->stay_in_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07002151
Ben Cahill77626352007-11-29 11:09:44 +08002152 /* Elapsed time using current modulation mode */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002153 if (lq_sta->flush_timer)
Zhu Yib481de92007-09-25 17:54:57 -07002154 flush_interval_passed =
Mohamed Abbas447fee72009-04-20 14:37:02 -07002155 time_after(jiffies,
2156 (unsigned long)(lq_sta->flush_timer +
Zhu Yib481de92007-09-25 17:54:57 -07002157 IWL_RATE_SCALE_FLUSH_INTVL));
2158
Ben Cahill77626352007-11-29 11:09:44 +08002159 /*
2160 * Check if we should allow search for new modulation mode.
2161 * If many frames have failed or succeeded, or we've used
2162 * this same modulation for a long time, allow search, and
2163 * reset history stats that keep track of whether we should
2164 * allow a new search. Also (below) reset all bitmaps and
2165 * stats in active history.
2166 */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07002167 if (force_search ||
2168 (lq_sta->total_failed > lq_sta->max_failure_limit) ||
Tomas Winklerc33104f2008-01-14 17:46:21 -08002169 (lq_sta->total_success > lq_sta->max_success_limit) ||
2170 ((!lq_sta->search_better_tbl) && (lq_sta->flush_timer)
Zhu Yib481de92007-09-25 17:54:57 -07002171 && (flush_interval_passed))) {
Johannes Berg0ca24da2012-03-15 13:26:46 -07002172 IWL_DEBUG_RATE(priv, "LQ: stay is expired %d %d %d\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002173 lq_sta->total_failed,
2174 lq_sta->total_success,
Zhu Yib481de92007-09-25 17:54:57 -07002175 flush_interval_passed);
Ben Cahill77626352007-11-29 11:09:44 +08002176
2177 /* Allow search for new mode */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002178 lq_sta->stay_in_tbl = 0; /* only place reset */
2179 lq_sta->total_failed = 0;
2180 lq_sta->total_success = 0;
2181 lq_sta->flush_timer = 0;
Ben Cahill77626352007-11-29 11:09:44 +08002182
2183 /*
2184 * Else if we've used this modulation mode enough repetitions
2185 * (regardless of elapsed time or success/failure), reset
2186 * history bitmaps and rate-specific stats for all rates in
2187 * active table.
2188 */
Mohamed Abbas403ab562007-11-06 22:06:25 -08002189 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002190 lq_sta->table_count++;
2191 if (lq_sta->table_count >=
2192 lq_sta->table_count_limit) {
2193 lq_sta->table_count = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002194
Tomas Winklere1623442009-01-27 14:27:56 -08002195 IWL_DEBUG_RATE(priv, "LQ: stay in table clear win\n");
Zhu Yib481de92007-09-25 17:54:57 -07002196 for (i = 0; i < IWL_RATE_COUNT; i++)
2197 rs_rate_scale_clear_window(
2198 &(tbl->win[i]));
2199 }
2200 }
2201
Ben Cahill77626352007-11-29 11:09:44 +08002202 /* If transitioning to allow "search", reset all history
2203 * bitmaps and stats in active table (this will become the new
2204 * "search" table). */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002205 if (!lq_sta->stay_in_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07002206 for (i = 0; i < IWL_RATE_COUNT; i++)
2207 rs_rate_scale_clear_window(&(tbl->win[i]));
2208 }
2209 }
2210}
2211
Ben Cahill77626352007-11-29 11:09:44 +08002212/*
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07002213 * setup rate table in uCode
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07002214 */
Johannes Berg70817b52011-05-11 03:29:25 -07002215static void rs_update_rate_tbl(struct iwl_priv *priv,
2216 struct iwl_rxon_context *ctx,
2217 struct iwl_lq_sta *lq_sta,
2218 struct iwl_scale_tbl_info *tbl,
2219 int index, u8 is_green)
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07002220{
2221 u32 rate;
2222
2223 /* Update uCode's rate table. */
2224 rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
2225 rs_fill_link_cmd(priv, lq_sta, rate);
Johannes Berg7e6a5882010-08-23 10:46:46 +02002226 iwl_send_lq_cmd(priv, ctx, &lq_sta->lq, CMD_ASYNC, false);
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07002227}
2228
2229/*
Ben Cahill77626352007-11-29 11:09:44 +08002230 * Do rate scaling and search for new modulation mode.
2231 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002232static void rs_rate_scale_perform(struct iwl_priv *priv,
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002233 struct sk_buff *skb,
Johannes Berg4b7679a2008-09-18 18:14:18 +02002234 struct ieee80211_sta *sta,
2235 struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002236{
Johannes Berg4b7679a2008-09-18 18:14:18 +02002237 struct ieee80211_hw *hw = priv->hw;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002238 struct ieee80211_conf *conf = &hw->conf;
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002239 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2240 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002241 int low = IWL_RATE_INVALID;
2242 int high = IWL_RATE_INVALID;
2243 int index;
2244 int i;
Tomas Winklere227cea2008-07-18 13:53:05 +08002245 struct iwl_rate_scale_data *window = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002246 int current_tpt = IWL_INVALID_VALUE;
2247 int low_tpt = IWL_INVALID_VALUE;
2248 int high_tpt = IWL_INVALID_VALUE;
2249 u32 fail_count;
2250 s8 scale_action = 0;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002251 u16 rate_mask;
Zhu Yib481de92007-09-25 17:54:57 -07002252 u8 update_lq = 0;
Tomas Winklere227cea2008-07-18 13:53:05 +08002253 struct iwl_scale_tbl_info *tbl, *tbl1;
Zhu Yib481de92007-09-25 17:54:57 -07002254 u16 rate_scale_index_msk = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002255 u8 is_green = 0;
2256 u8 active_tbl = 0;
2257 u8 done_search = 0;
2258 u16 high_low;
Guy Cohena5e8b502008-05-29 16:35:11 +08002259 s32 sr;
Emmanuel Grumbach5f85a782011-08-25 23:11:18 -07002260 u8 tid = IWL_MAX_TID_COUNT;
Wey-Yi Guy86b47662009-03-10 14:35:09 -07002261 struct iwl_tid_data *tid_data;
Johannes Berg7e6a5882010-08-23 10:46:46 +02002262 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
Johannes Bergc6892902011-09-20 15:37:21 -07002263 struct iwl_rxon_context *ctx = sta_priv->ctx;
Zhu Yib481de92007-09-25 17:54:57 -07002264
Tomas Winklere1623442009-01-27 14:27:56 -08002265 IWL_DEBUG_RATE(priv, "rate scale calculate new rate for skb\n");
Zhu Yib481de92007-09-25 17:54:57 -07002266
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002267 /* Send management frames and NO_ACK data using lowest rate. */
Tomas Winkler417f1142008-11-12 13:14:06 -08002268 /* TODO: this could probably be improved.. */
2269 if (!ieee80211_is_data(hdr->frame_control) ||
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002270 info->flags & IEEE80211_TX_CTL_NO_ACK)
Zhu Yib481de92007-09-25 17:54:57 -07002271 return;
Zhu Yib481de92007-09-25 17:54:57 -07002272
Johannes Berg4b7679a2008-09-18 18:14:18 +02002273 lq_sta->supp_rates = sta->supp_rates[lq_sta->band];
Zhu Yib481de92007-09-25 17:54:57 -07002274
Ron Rindjunskyfaa29712008-06-12 09:46:58 +08002275 tid = rs_tl_add_packet(lq_sta, hdr);
Emmanuel Grumbach5f85a782011-08-25 23:11:18 -07002276 if ((tid != IWL_MAX_TID_COUNT) &&
2277 (lq_sta->tx_agg_tid_en & (1 << tid))) {
Emmanuel Grumbach04cf6822011-11-23 11:06:12 +02002278 tid_data = &priv->tid_data[lq_sta->lq.sta_id][tid];
Daniel C Halperine3949d62009-09-17 10:43:50 -07002279 if (tid_data->agg.state == IWL_AGG_OFF)
2280 lq_sta->is_agg = 0;
2281 else
2282 lq_sta->is_agg = 1;
2283 } else
2284 lq_sta->is_agg = 0;
Ron Rindjunskyfaa29712008-06-12 09:46:58 +08002285
Ben Cahill77626352007-11-29 11:09:44 +08002286 /*
2287 * Select rate-scale / modulation-mode table to work with in
2288 * the rest of this function: "search" if searching for better
2289 * modulation mode, or "active" if doing rate scaling within a mode.
2290 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002291 if (!lq_sta->search_better_tbl)
2292 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002293 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002294 active_tbl = 1 - lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002295
Tomas Winklerc33104f2008-01-14 17:46:21 -08002296 tbl = &(lq_sta->lq_info[active_tbl]);
Wey-Yi Guy2681b202009-05-21 13:44:22 -07002297 if (is_legacy(tbl->lq_type))
2298 lq_sta->is_green = 0;
2299 else
Johannes Berg7e6a5882010-08-23 10:46:46 +02002300 lq_sta->is_green = rs_use_green(sta);
Tomas Winklerc33104f2008-01-14 17:46:21 -08002301 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07002302
Ben Cahill77626352007-11-29 11:09:44 +08002303 /* current tx rate */
Johannes Bergb7e35002008-09-11 02:22:58 +02002304 index = lq_sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002305
Tomas Winklere1623442009-01-27 14:27:56 -08002306 IWL_DEBUG_RATE(priv, "Rate scale index %d for type %d\n", index,
Zhu Yib481de92007-09-25 17:54:57 -07002307 tbl->lq_type);
2308
Ben Cahill77626352007-11-29 11:09:44 +08002309 /* rates available for this association, and for modulation mode */
Guy Coheneecd6e52008-04-23 17:14:58 -07002310 rate_mask = rs_get_supported_rates(lq_sta, hdr, tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -07002311
Frans Pop91dd6c22010-03-24 14:19:58 -07002312 IWL_DEBUG_RATE(priv, "mask 0x%04X\n", rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07002313
2314 /* mask with station rate restriction */
2315 if (is_legacy(tbl->lq_type)) {
Johannes Berg8318d782008-01-24 19:38:38 +01002316 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Ben Cahill77626352007-11-29 11:09:44 +08002317 /* supp_rates has no CCK bits in A mode */
Zhu Yib481de92007-09-25 17:54:57 -07002318 rate_scale_index_msk = (u16) (rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -08002319 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
Zhu Yib481de92007-09-25 17:54:57 -07002320 else
2321 rate_scale_index_msk = (u16) (rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -08002322 lq_sta->supp_rates);
Zhu Yib481de92007-09-25 17:54:57 -07002323
2324 } else
2325 rate_scale_index_msk = rate_mask;
2326
2327 if (!rate_scale_index_msk)
2328 rate_scale_index_msk = rate_mask;
2329
Guy Cohen07bc28e2008-04-23 17:15:03 -07002330 if (!((1 << index) & rate_scale_index_msk)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002331 IWL_ERR(priv, "Current Rate is not valid\n");
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07002332 if (lq_sta->search_better_tbl) {
2333 /* revert to active table if search table is not valid*/
2334 tbl->lq_type = LQ_NONE;
2335 lq_sta->search_better_tbl = 0;
2336 tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
2337 /* get "active" rate info */
2338 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
Johannes Berg70817b52011-05-11 03:29:25 -07002339 rs_update_rate_tbl(priv, ctx, lq_sta, tbl,
2340 index, is_green);
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07002341 }
Guy Cohen07bc28e2008-04-23 17:15:03 -07002342 return;
Zhu Yib481de92007-09-25 17:54:57 -07002343 }
2344
Ben Cahill77626352007-11-29 11:09:44 +08002345 /* Get expected throughput table and history window for current rate */
Guy Cohen07bc28e2008-04-23 17:15:03 -07002346 if (!tbl->expected_tpt) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002347 IWL_ERR(priv, "tbl->expected_tpt is NULL\n");
Guy Cohen07bc28e2008-04-23 17:15:03 -07002348 return;
2349 }
Zhu Yib481de92007-09-25 17:54:57 -07002350
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002351 /* force user max rate if set by user */
2352 if ((lq_sta->max_rate_idx != -1) &&
2353 (lq_sta->max_rate_idx < index)) {
2354 index = lq_sta->max_rate_idx;
2355 update_lq = 1;
2356 window = &(tbl->win[index]);
2357 goto lq_update;
2358 }
2359
Zhu Yib481de92007-09-25 17:54:57 -07002360 window = &(tbl->win[index]);
2361
Ben Cahill77626352007-11-29 11:09:44 +08002362 /*
2363 * If there is not enough history to calculate actual average
2364 * throughput, keep analyzing results of more tx frames, without
2365 * changing rate or mode (bypass most of the rest of this function).
2366 * Set up new rate table in uCode only if old rate is not supported
2367 * in current association (use new rate found above).
2368 */
Zhu Yib481de92007-09-25 17:54:57 -07002369 fail_count = window->counter - window->success_counter;
Guy Cohen07bc28e2008-04-23 17:15:03 -07002370 if ((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
2371 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002372 IWL_DEBUG_RATE(priv, "LQ: still below TH. succ=%d total=%d "
Zhu Yib481de92007-09-25 17:54:57 -07002373 "for index %d\n",
2374 window->success_counter, window->counter, index);
Ben Cahill77626352007-11-29 11:09:44 +08002375
2376 /* Can't calculate this yet; not enough history */
Zhu Yib481de92007-09-25 17:54:57 -07002377 window->average_tpt = IWL_INVALID_VALUE;
Ben Cahill77626352007-11-29 11:09:44 +08002378
2379 /* Should we stay with this modulation mode,
2380 * or search for a new one? */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07002381 rs_stay_in_table(lq_sta, false);
Ben Cahill77626352007-11-29 11:09:44 +08002382
Zhu Yib481de92007-09-25 17:54:57 -07002383 goto out;
Guy Cohen3110bef2008-09-09 10:54:54 +08002384 }
Ben Cahill77626352007-11-29 11:09:44 +08002385 /* Else we have enough samples; calculate estimate of
2386 * actual average throughput */
Reinette Chatre3d79b2a2010-05-03 10:55:07 -07002387 if (window->average_tpt != ((window->success_ratio *
2388 tbl->expected_tpt[index] + 64) / 128)) {
2389 IWL_ERR(priv, "expected_tpt should have been calculated by now\n");
2390 window->average_tpt = ((window->success_ratio *
2391 tbl->expected_tpt[index] + 64) / 128);
2392 }
Zhu Yib481de92007-09-25 17:54:57 -07002393
Ben Cahill77626352007-11-29 11:09:44 +08002394 /* If we are searching for better modulation mode, check success. */
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002395 if (lq_sta->search_better_tbl &&
Johannes Berg3ad3b922009-08-07 15:41:48 -07002396 (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_MULTI)) {
Ben Cahill77626352007-11-29 11:09:44 +08002397 /* If good success, continue using the "search" mode;
2398 * no need to send new link quality command, since we're
2399 * continuing to use the setup that we've been trying. */
Guy Cohen07bc28e2008-04-23 17:15:03 -07002400 if (window->average_tpt > lq_sta->last_tpt) {
2401
Tomas Winklere1623442009-01-27 14:27:56 -08002402 IWL_DEBUG_RATE(priv, "LQ: SWITCHING TO NEW TABLE "
Guy Cohen07bc28e2008-04-23 17:15:03 -07002403 "suc=%d cur-tpt=%d old-tpt=%d\n",
2404 window->success_ratio,
2405 window->average_tpt,
2406 lq_sta->last_tpt);
2407
2408 if (!is_legacy(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08002409 lq_sta->enable_counter = 1;
Guy Cohen07bc28e2008-04-23 17:15:03 -07002410
Ben Cahill77626352007-11-29 11:09:44 +08002411 /* Swap tables; "search" becomes "active" */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002412 lq_sta->active_tbl = active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002413 current_tpt = window->average_tpt;
Ben Cahill77626352007-11-29 11:09:44 +08002414
2415 /* Else poor success; go back to mode in "active" table */
Zhu Yib481de92007-09-25 17:54:57 -07002416 } else {
Guy Cohen07bc28e2008-04-23 17:15:03 -07002417
Tomas Winklere1623442009-01-27 14:27:56 -08002418 IWL_DEBUG_RATE(priv, "LQ: GOING BACK TO THE OLD TABLE "
Guy Cohen07bc28e2008-04-23 17:15:03 -07002419 "suc=%d cur-tpt=%d old-tpt=%d\n",
2420 window->success_ratio,
2421 window->average_tpt,
2422 lq_sta->last_tpt);
2423
Ben Cahill77626352007-11-29 11:09:44 +08002424 /* Nullify "search" table */
Zhu Yib481de92007-09-25 17:54:57 -07002425 tbl->lq_type = LQ_NONE;
Ben Cahill77626352007-11-29 11:09:44 +08002426
2427 /* Revert to "active" table */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002428 active_tbl = lq_sta->active_tbl;
2429 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07002430
Ben Cahill77626352007-11-29 11:09:44 +08002431 /* Revert to "active" rate and throughput info */
Tomas Winklere7d326ac2008-06-12 09:47:11 +08002432 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
Tomas Winklerc33104f2008-01-14 17:46:21 -08002433 current_tpt = lq_sta->last_tpt;
Ben Cahill77626352007-11-29 11:09:44 +08002434
2435 /* Need to set up a new rate table in uCode */
2436 update_lq = 1;
Zhu Yib481de92007-09-25 17:54:57 -07002437 }
Ben Cahill77626352007-11-29 11:09:44 +08002438
2439 /* Either way, we've made a decision; modulation mode
2440 * search is done, allow rate adjustment next time. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002441 lq_sta->search_better_tbl = 0;
Ben Cahill77626352007-11-29 11:09:44 +08002442 done_search = 1; /* Don't switch modes below! */
Zhu Yib481de92007-09-25 17:54:57 -07002443 goto lq_update;
2444 }
2445
Ben Cahill77626352007-11-29 11:09:44 +08002446 /* (Else) not in search of better modulation mode, try for better
2447 * starting rate, while staying in this mode. */
Ester Kummerbf403db2008-05-05 10:22:40 +08002448 high_low = rs_get_adjacent_rate(priv, index, rate_scale_index_msk,
Zhu Yib481de92007-09-25 17:54:57 -07002449 tbl->lq_type);
2450 low = high_low & 0xff;
2451 high = (high_low >> 8) & 0xff;
2452
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002453 /* If user set max rate, dont allow higher than user constrain */
2454 if ((lq_sta->max_rate_idx != -1) &&
2455 (lq_sta->max_rate_idx < high))
2456 high = IWL_RATE_INVALID;
2457
Guy Cohena5e8b502008-05-29 16:35:11 +08002458 sr = window->success_ratio;
2459
Ben Cahill77626352007-11-29 11:09:44 +08002460 /* Collect measured throughputs for current and adjacent rates */
Zhu Yib481de92007-09-25 17:54:57 -07002461 current_tpt = window->average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07002462 if (low != IWL_RATE_INVALID)
2463 low_tpt = tbl->win[low].average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07002464 if (high != IWL_RATE_INVALID)
2465 high_tpt = tbl->win[high].average_tpt;
2466
Guy Cohena5e8b502008-05-29 16:35:11 +08002467 scale_action = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002468
Ben Cahill77626352007-11-29 11:09:44 +08002469 /* Too many failures, decrease rate */
Guy Cohena5e8b502008-05-29 16:35:11 +08002470 if ((sr <= IWL_RATE_DECREASE_TH) || (current_tpt == 0)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002471 IWL_DEBUG_RATE(priv, "decrease rate because of low success_ratio\n");
Zhu Yib481de92007-09-25 17:54:57 -07002472 scale_action = -1;
Ben Cahill77626352007-11-29 11:09:44 +08002473
2474 /* No throughput measured yet for adjacent rates; try increase. */
Zhu Yib481de92007-09-25 17:54:57 -07002475 } else if ((low_tpt == IWL_INVALID_VALUE) &&
Guy Cohena5e8b502008-05-29 16:35:11 +08002476 (high_tpt == IWL_INVALID_VALUE)) {
2477
2478 if (high != IWL_RATE_INVALID && sr >= IWL_RATE_INCREASE_TH)
2479 scale_action = 1;
2480 else if (low != IWL_RATE_INVALID)
Wey-Yi Guy8fe72312009-03-10 14:35:10 -07002481 scale_action = 0;
Guy Cohena5e8b502008-05-29 16:35:11 +08002482 }
Ben Cahill77626352007-11-29 11:09:44 +08002483
2484 /* Both adjacent throughputs are measured, but neither one has better
2485 * throughput; we're using the best rate, don't change it! */
Zhu Yib481de92007-09-25 17:54:57 -07002486 else if ((low_tpt != IWL_INVALID_VALUE) &&
2487 (high_tpt != IWL_INVALID_VALUE) &&
2488 (low_tpt < current_tpt) &&
2489 (high_tpt < current_tpt))
2490 scale_action = 0;
Ben Cahill77626352007-11-29 11:09:44 +08002491
2492 /* At least one adjacent rate's throughput is measured,
2493 * and may have better performance. */
Zhu Yib481de92007-09-25 17:54:57 -07002494 else {
Ben Cahill77626352007-11-29 11:09:44 +08002495 /* Higher adjacent rate's throughput is measured */
Zhu Yib481de92007-09-25 17:54:57 -07002496 if (high_tpt != IWL_INVALID_VALUE) {
Ben Cahill77626352007-11-29 11:09:44 +08002497 /* Higher rate has better throughput */
Guy Cohena5e8b502008-05-29 16:35:11 +08002498 if (high_tpt > current_tpt &&
2499 sr >= IWL_RATE_INCREASE_TH) {
Zhu Yib481de92007-09-25 17:54:57 -07002500 scale_action = 1;
Guy Cohena5e8b502008-05-29 16:35:11 +08002501 } else {
Wey-Yi Guy8fe72312009-03-10 14:35:10 -07002502 scale_action = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002503 }
Ben Cahill77626352007-11-29 11:09:44 +08002504
2505 /* Lower adjacent rate's throughput is measured */
Zhu Yib481de92007-09-25 17:54:57 -07002506 } else if (low_tpt != IWL_INVALID_VALUE) {
Ben Cahill77626352007-11-29 11:09:44 +08002507 /* Lower rate has better throughput */
Zhu Yib481de92007-09-25 17:54:57 -07002508 if (low_tpt > current_tpt) {
Tomas Winklere1623442009-01-27 14:27:56 -08002509 IWL_DEBUG_RATE(priv,
2510 "decrease rate because of low tpt\n");
Zhu Yib481de92007-09-25 17:54:57 -07002511 scale_action = -1;
Guy Cohena5e8b502008-05-29 16:35:11 +08002512 } else if (sr >= IWL_RATE_INCREASE_TH) {
Zhu Yib481de92007-09-25 17:54:57 -07002513 scale_action = 1;
Guy Cohena5e8b502008-05-29 16:35:11 +08002514 }
Zhu Yib481de92007-09-25 17:54:57 -07002515 }
2516 }
2517
Ben Cahill77626352007-11-29 11:09:44 +08002518 /* Sanity check; asked for decrease, but success rate or throughput
2519 * has been good at old rate. Don't change it. */
Guy Cohena5e8b502008-05-29 16:35:11 +08002520 if ((scale_action == -1) && (low != IWL_RATE_INVALID) &&
2521 ((sr > IWL_RATE_HIGH_TH) ||
Zhu Yib481de92007-09-25 17:54:57 -07002522 (current_tpt > (100 * tbl->expected_tpt[low]))))
Zhu Yib481de92007-09-25 17:54:57 -07002523 scale_action = 0;
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002524 if (!iwl_ht_enabled(priv) && !is_legacy(tbl->lq_type))
2525 scale_action = -1;
Johannes Berg3ad3b922009-08-07 15:41:48 -07002526 if (iwl_tx_ant_restriction(priv) != IWL_ANT_OK_MULTI &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002527 (is_mimo2(tbl->lq_type) || is_mimo3(tbl->lq_type)))
2528 scale_action = -1;
Johannes Berg290f5992010-08-23 07:56:58 -07002529
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07002530 if ((priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH) &&
2531 (is_mimo2(tbl->lq_type) || is_mimo3(tbl->lq_type))) {
2532 if (lq_sta->last_bt_traffic > priv->bt_traffic_load) {
2533 /*
2534 * don't set scale_action, don't want to scale up if
2535 * the rate scale doesn't otherwise think that is a
2536 * good idea.
2537 */
2538 } else if (lq_sta->last_bt_traffic <= priv->bt_traffic_load) {
2539 scale_action = -1;
2540 }
2541 }
2542 lq_sta->last_bt_traffic = priv->bt_traffic_load;
2543
2544 if ((priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH) &&
2545 (is_mimo2(tbl->lq_type) || is_mimo3(tbl->lq_type))) {
2546 /* search for a new modulation */
2547 rs_stay_in_table(lq_sta, true);
2548 goto lq_update;
Johannes Berg290f5992010-08-23 07:56:58 -07002549 }
2550
Zhu Yib481de92007-09-25 17:54:57 -07002551 switch (scale_action) {
2552 case -1:
Ben Cahill77626352007-11-29 11:09:44 +08002553 /* Decrease starting rate, update uCode's rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002554 if (low != IWL_RATE_INVALID) {
2555 update_lq = 1;
2556 index = low;
2557 }
Mohamed Abbas447fee72009-04-20 14:37:02 -07002558
Zhu Yib481de92007-09-25 17:54:57 -07002559 break;
2560 case 1:
Ben Cahill77626352007-11-29 11:09:44 +08002561 /* Increase starting rate, update uCode's rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002562 if (high != IWL_RATE_INVALID) {
2563 update_lq = 1;
2564 index = high;
2565 }
2566
2567 break;
2568 case 0:
Ben Cahill77626352007-11-29 11:09:44 +08002569 /* No change */
Zhu Yib481de92007-09-25 17:54:57 -07002570 default:
2571 break;
2572 }
2573
Tomas Winklere1623442009-01-27 14:27:56 -08002574 IWL_DEBUG_RATE(priv, "choose rate scale index %d action %d low %d "
Zhu Yib481de92007-09-25 17:54:57 -07002575 "high %d type %d\n",
2576 index, scale_action, low, high, tbl->lq_type);
2577
Guy Cohena5e8b502008-05-29 16:35:11 +08002578lq_update:
Ben Cahill77626352007-11-29 11:09:44 +08002579 /* Replace uCode's rate table for the destination station. */
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07002580 if (update_lq)
Johannes Berg70817b52011-05-11 03:29:25 -07002581 rs_update_rate_tbl(priv, ctx, lq_sta, tbl, index, is_green);
Ben Cahill77626352007-11-29 11:09:44 +08002582
Johannes Berg3ad3b922009-08-07 15:41:48 -07002583 if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_MULTI) {
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002584 /* Should we stay with this modulation mode,
2585 * or search for a new one? */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07002586 rs_stay_in_table(lq_sta, false);
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002587 }
Ben Cahill77626352007-11-29 11:09:44 +08002588 /*
2589 * Search for new modulation mode if we're:
2590 * 1) Not changing rates right now
2591 * 2) Not just finishing up a search
2592 * 3) Allowing a new search
2593 */
Guy Cohen47cfd462008-05-27 11:29:34 +08002594 if (!update_lq && !done_search && !lq_sta->stay_in_tbl && window->counter) {
Ben Cahill77626352007-11-29 11:09:44 +08002595 /* Save current throughput to compare with "search" throughput*/
Tomas Winklerc33104f2008-01-14 17:46:21 -08002596 lq_sta->last_tpt = current_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07002597
Ben Cahill77626352007-11-29 11:09:44 +08002598 /* Select a new "search" modulation mode to try.
2599 * If one is found, set up the new "search" table. */
Zhu Yib481de92007-09-25 17:54:57 -07002600 if (is_legacy(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08002601 rs_move_legacy_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07002602 else if (is_siso(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08002603 rs_move_siso_to_other(priv, lq_sta, conf, sta, index);
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07002604 else if (is_mimo2(tbl->lq_type))
2605 rs_move_mimo2_to_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07002606 else
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07002607 rs_move_mimo3_to_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07002608
Ben Cahill77626352007-11-29 11:09:44 +08002609 /* If new "search" mode was selected, set up in uCode table */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002610 if (lq_sta->search_better_tbl) {
Ben Cahill77626352007-11-29 11:09:44 +08002611 /* Access the "search" table, clear its history. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002612 tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Zhu Yib481de92007-09-25 17:54:57 -07002613 for (i = 0; i < IWL_RATE_COUNT; i++)
2614 rs_rate_scale_clear_window(&(tbl->win[i]));
2615
Ben Cahill77626352007-11-29 11:09:44 +08002616 /* Use new "search" start rate */
Tomas Winklere7d326ac2008-06-12 09:47:11 +08002617 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002618
Tomas Winklere1623442009-01-27 14:27:56 -08002619 IWL_DEBUG_RATE(priv, "Switch current mcs: %X index: %d\n",
Guy Cohen39e88502008-04-23 17:14:57 -07002620 tbl->current_rate, index);
Guy Cohen07bc28e2008-04-23 17:15:03 -07002621 rs_fill_link_cmd(priv, lq_sta, tbl->current_rate);
Johannes Berg7e6a5882010-08-23 10:46:46 +02002622 iwl_send_lq_cmd(priv, ctx, &lq_sta->lq, CMD_ASYNC, false);
Mohamed Abbas447fee72009-04-20 14:37:02 -07002623 } else
2624 done_search = 1;
2625 }
Zhu Yib481de92007-09-25 17:54:57 -07002626
Mohamed Abbas447fee72009-04-20 14:37:02 -07002627 if (done_search && !lq_sta->stay_in_tbl) {
Ben Cahill77626352007-11-29 11:09:44 +08002628 /* If the "active" (non-search) mode was legacy,
2629 * and we've tried switching antennas,
2630 * but we haven't been able to try HT modes (not available),
2631 * stay with best antenna legacy modulation for a while
2632 * before next round of mode comparisons. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002633 tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
Luis R. Rodriguezde27e642008-12-23 15:58:44 -08002634 if (is_legacy(tbl1->lq_type) && !conf_is_ht(conf) &&
Mohamed Abbasd6e93392009-05-08 13:44:39 -07002635 lq_sta->action_counter > tbl1->max_search) {
Tomas Winklere1623442009-01-27 14:27:56 -08002636 IWL_DEBUG_RATE(priv, "LQ: STAY in legacy table\n");
Ester Kummerbf403db2008-05-05 10:22:40 +08002637 rs_set_stay_in_table(priv, 1, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002638 }
2639
Ben Cahill77626352007-11-29 11:09:44 +08002640 /* If we're in an HT mode, and all 3 mode switch actions
2641 * have been tried and compared, stay in this best modulation
2642 * mode for a while before next round of mode comparisons. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002643 if (lq_sta->enable_counter &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002644 (lq_sta->action_counter >= tbl1->max_search) &&
2645 iwl_ht_enabled(priv)) {
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002646 if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
2647 (lq_sta->tx_agg_tid_en & (1 << tid)) &&
Emmanuel Grumbach5f85a782011-08-25 23:11:18 -07002648 (tid != IWL_MAX_TID_COUNT)) {
2649 u8 sta_id = lq_sta->lq.sta_id;
Emmanuel Grumbach04cf6822011-11-23 11:06:12 +02002650 tid_data = &priv->tid_data[sta_id][tid];
Wey-Yi Guy86b47662009-03-10 14:35:09 -07002651 if (tid_data->agg.state == IWL_AGG_OFF) {
2652 IWL_DEBUG_RATE(priv,
2653 "try to aggregate tid %d\n",
2654 tid);
2655 rs_tl_turn_on_agg(priv, tid,
2656 lq_sta, sta);
2657 }
Zhu Yib481de92007-09-25 17:54:57 -07002658 }
Ester Kummerbf403db2008-05-05 10:22:40 +08002659 rs_set_stay_in_table(priv, 0, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002660 }
Zhu Yib481de92007-09-25 17:54:57 -07002661 }
2662
2663out:
Tomas Winkler978785a2008-12-19 10:37:31 +08002664 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
Wey-Yi Guyc3322b32011-10-10 07:26:58 -07002665 lq_sta->last_txrate_idx = index;
Zhu Yib481de92007-09-25 17:54:57 -07002666}
2667
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08002668/**
2669 * rs_initialize_lq - Initialize a station's hardware rate table
2670 *
2671 * The uCode's station table contains a table of fallback rates
2672 * for automatic fallback during transmission.
2673 *
2674 * NOTE: This sets up a default set of values. These will be replaced later
2675 * if the driver's iwl-agn-rs rate scaling algorithm is used, instead of
2676 * rc80211_simple.
2677 *
2678 * NOTE: Run REPLY_ADD_STA command to set up station table entry, before
2679 * calling this function (which runs REPLY_TX_LINK_QUALITY_CMD,
2680 * which requires station table entry to exist).
2681 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002682static void rs_initialize_lq(struct iwl_priv *priv,
Johannes Berg4b7679a2008-09-18 18:14:18 +02002683 struct ieee80211_sta *sta,
2684 struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002685{
Tomas Winklere227cea2008-07-18 13:53:05 +08002686 struct iwl_scale_tbl_info *tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002687 int rate_idx;
Guy Cohenaade00c2008-04-23 17:14:59 -07002688 int i;
Guy Cohen39e88502008-04-23 17:14:57 -07002689 u32 rate;
Johannes Berg7e6a5882010-08-23 10:46:46 +02002690 u8 use_green = rs_use_green(sta);
Guy Cohenaade00c2008-04-23 17:14:59 -07002691 u8 active_tbl = 0;
2692 u8 valid_tx_ant;
Johannes Berg7e6a5882010-08-23 10:46:46 +02002693 struct iwl_station_priv *sta_priv;
2694 struct iwl_rxon_context *ctx;
Zhu Yib481de92007-09-25 17:54:57 -07002695
Johannes Berg4b7679a2008-09-18 18:14:18 +02002696 if (!sta || !lq_sta)
Johannes Berg7e6a5882010-08-23 10:46:46 +02002697 return;
2698
2699 sta_priv = (void *)sta->drv_priv;
Johannes Bergc6892902011-09-20 15:37:21 -07002700 ctx = sta_priv->ctx;
Zhu Yib481de92007-09-25 17:54:57 -07002701
Johannes Bergb7e35002008-09-11 02:22:58 +02002702 i = lq_sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002703
Eytan Lifshitzb7998c82012-12-01 20:59:49 +02002704 valid_tx_ant = priv->nvm_data->valid_tx_ant;
Guy Cohenaade00c2008-04-23 17:14:59 -07002705
Tomas Winklerc33104f2008-01-14 17:46:21 -08002706 if (!lq_sta->search_better_tbl)
2707 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002708 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002709 active_tbl = 1 - lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002710
Tomas Winklerc33104f2008-01-14 17:46:21 -08002711 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07002712
2713 if ((i < 0) || (i >= IWL_RATE_COUNT))
2714 i = 0;
2715
Tomas Winkler1826dcc2008-05-15 13:54:02 +08002716 rate = iwl_rates[i].plcp;
Winkler, Tomaseb48dca2008-10-29 14:05:48 -07002717 tbl->ant_type = first_antenna(valid_tx_ant);
2718 rate |= tbl->ant_type << RATE_MCS_ANT_POS;
Zhu Yib481de92007-09-25 17:54:57 -07002719
2720 if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
Guy Cohen39e88502008-04-23 17:14:57 -07002721 rate |= RATE_MCS_CCK_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002722
Guy Cohen39e88502008-04-23 17:14:57 -07002723 rs_get_tbl_info_from_mcs(rate, priv->band, tbl, &rate_idx);
Guy Cohenaade00c2008-04-23 17:14:59 -07002724 if (!rs_is_valid_ant(valid_tx_ant, tbl->ant_type))
2725 rs_toggle_antenna(valid_tx_ant, &rate, tbl);
Zhu Yib481de92007-09-25 17:54:57 -07002726
Tomas Winkler978785a2008-12-19 10:37:31 +08002727 rate = rate_n_flags_from_tbl(priv, tbl, rate_idx, use_green);
Guy Cohen39e88502008-04-23 17:14:57 -07002728 tbl->current_rate = rate;
Guy Coheneecd6e52008-04-23 17:14:58 -07002729 rs_set_expected_tpt_table(lq_sta, tbl);
Guy Cohen07bc28e2008-04-23 17:15:03 -07002730 rs_fill_link_cmd(NULL, lq_sta, rate);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08002731 priv->stations[lq_sta->lq.sta_id].lq = &lq_sta->lq;
Johannes Berg7e6a5882010-08-23 10:46:46 +02002732 iwl_send_lq_cmd(priv, ctx, &lq_sta->lq, CMD_SYNC, true);
Zhu Yib481de92007-09-25 17:54:57 -07002733}
2734
Johannes Berge6a98542008-10-21 12:40:02 +02002735static void rs_get_rate(void *priv_r, struct ieee80211_sta *sta, void *priv_sta,
2736 struct ieee80211_tx_rate_control *txrc)
Zhu Yib481de92007-09-25 17:54:57 -07002737{
2738
Johannes Berge6a98542008-10-21 12:40:02 +02002739 struct sk_buff *skb = txrc->skb;
2740 struct ieee80211_supported_band *sband = txrc->sband;
Emmanuel Grumbachd0f76d62012-02-09 16:08:15 +02002741 struct iwl_op_mode *op_mode __maybe_unused =
2742 (struct iwl_op_mode *)priv_r;
2743 struct iwl_priv *priv __maybe_unused = IWL_OP_MODE_GET_DVM(op_mode);
Johannes Berge6a98542008-10-21 12:40:02 +02002744 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Tomas Winkler417f1142008-11-12 13:14:06 -08002745 struct iwl_lq_sta *lq_sta = priv_sta;
2746 int rate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002747
Tomas Winklere1623442009-01-27 14:27:56 -08002748 IWL_DEBUG_RATE_LIMIT(priv, "rate scale calculate new rate for skb\n");
Zhu Yib481de92007-09-25 17:54:57 -07002749
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002750 /* Get max rate if user set max rate */
2751 if (lq_sta) {
2752 lq_sta->max_rate_idx = txrc->max_rate_idx;
2753 if ((sband->band == IEEE80211_BAND_5GHZ) &&
2754 (lq_sta->max_rate_idx != -1))
2755 lq_sta->max_rate_idx += IWL_FIRST_OFDM_RATE;
2756 if ((lq_sta->max_rate_idx < 0) ||
2757 (lq_sta->max_rate_idx >= IWL_RATE_COUNT))
2758 lq_sta->max_rate_idx = -1;
2759 }
2760
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08002761 /* Treat uninitialized rate scaling data same as non-existing. */
2762 if (lq_sta && !lq_sta->drv) {
2763 IWL_DEBUG_RATE(priv, "Rate scaling not initialized yet.\n");
2764 priv_sta = NULL;
2765 }
2766
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002767 /* Send management frames and NO_ACK data using lowest rate. */
Luis R. Rodriguez4c6d4f52009-07-16 10:05:41 -07002768 if (rate_control_send_low(sta, priv_sta, txrc))
Johannes Berg4b7679a2008-09-18 18:14:18 +02002769 return;
Zhu Yib481de92007-09-25 17:54:57 -07002770
Tomas Winkler417f1142008-11-12 13:14:06 -08002771 rate_idx = lq_sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002772
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002773 if (lq_sta->last_rate_n_flags & RATE_MCS_HT_MSK) {
Tomas Winkler417f1142008-11-12 13:14:06 -08002774 rate_idx -= IWL_FIRST_OFDM_RATE;
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002775 /* 6M and 9M shared same MCS index */
2776 rate_idx = (rate_idx > 0) ? (rate_idx - 1) : 0;
2777 if (rs_extract_rate(lq_sta->last_rate_n_flags) >=
2778 IWL_RATE_MIMO3_6M_PLCP)
2779 rate_idx = rate_idx + (2 * MCS_INDEX_PER_STREAM);
2780 else if (rs_extract_rate(lq_sta->last_rate_n_flags) >=
2781 IWL_RATE_MIMO2_6M_PLCP)
2782 rate_idx = rate_idx + MCS_INDEX_PER_STREAM;
2783 info->control.rates[0].flags = IEEE80211_TX_RC_MCS;
2784 if (lq_sta->last_rate_n_flags & RATE_MCS_SGI_MSK)
2785 info->control.rates[0].flags |= IEEE80211_TX_RC_SHORT_GI;
2786 if (lq_sta->last_rate_n_flags & RATE_MCS_DUP_MSK)
2787 info->control.rates[0].flags |= IEEE80211_TX_RC_DUP_DATA;
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07002788 if (lq_sta->last_rate_n_flags & RATE_MCS_HT40_MSK)
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002789 info->control.rates[0].flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
2790 if (lq_sta->last_rate_n_flags & RATE_MCS_GF_MSK)
2791 info->control.rates[0].flags |= IEEE80211_TX_RC_GREEN_FIELD;
2792 } else {
Daniel C Halperin50273092009-08-28 09:44:45 -07002793 /* Check for invalid rates */
2794 if ((rate_idx < 0) || (rate_idx >= IWL_RATE_COUNT_LEGACY) ||
2795 ((sband->band == IEEE80211_BAND_5GHZ) &&
2796 (rate_idx < IWL_FIRST_OFDM_RATE)))
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002797 rate_idx = rate_lowest_index(sband, sta);
Daniel C Halperin50273092009-08-28 09:44:45 -07002798 /* On valid 5 GHz rate, adjust index */
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002799 else if (sband->band == IEEE80211_BAND_5GHZ)
2800 rate_idx -= IWL_FIRST_OFDM_RATE;
Daniel C Halperin7ebaeff2009-08-21 13:34:20 -07002801 info->control.rates[0].flags = 0;
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002802 }
Tomas Winkler417f1142008-11-12 13:14:06 -08002803 info->control.rates[0].idx = rate_idx;
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002804
Zhu Yib481de92007-09-25 17:54:57 -07002805}
2806
Johannes Berg4b7679a2008-09-18 18:14:18 +02002807static void *rs_alloc_sta(void *priv_rate, struct ieee80211_sta *sta,
2808 gfp_t gfp)
Zhu Yib481de92007-09-25 17:54:57 -07002809{
Reinette Chatre8d9698b2009-10-16 14:25:55 -07002810 struct iwl_station_priv *sta_priv = (struct iwl_station_priv *) sta->drv_priv;
Emmanuel Grumbachd0f76d62012-02-09 16:08:15 +02002811 struct iwl_op_mode *op_mode __maybe_unused =
2812 (struct iwl_op_mode *)priv_rate;
2813 struct iwl_priv *priv __maybe_unused = IWL_OP_MODE_GET_DVM(op_mode);
Zhu Yib481de92007-09-25 17:54:57 -07002814
Tomas Winklere1623442009-01-27 14:27:56 -08002815 IWL_DEBUG_RATE(priv, "create station rate scale window\n");
Zhu Yib481de92007-09-25 17:54:57 -07002816
Johannes Berg08960de2011-04-05 09:41:53 -07002817 return &sta_priv->lq_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002818}
2819
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08002820/*
2821 * Called after adding a new station to initialize rate scaling
2822 */
2823void iwl_rs_rate_init(struct iwl_priv *priv, struct ieee80211_sta *sta, u8 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07002824{
2825 int i, j;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08002826 struct ieee80211_hw *hw = priv->hw;
Johannes Berg4b7679a2008-09-18 18:14:18 +02002827 struct ieee80211_conf *conf = &priv->hw->conf;
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002828 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08002829 struct iwl_station_priv *sta_priv;
2830 struct iwl_lq_sta *lq_sta;
2831 struct ieee80211_supported_band *sband;
Johannes Berg3ac40ed2012-03-08 09:18:04 +01002832 unsigned long supp; /* must be unsigned long for for_each_set_bit */
Zhu Yib481de92007-09-25 17:54:57 -07002833
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08002834 sta_priv = (struct iwl_station_priv *) sta->drv_priv;
2835 lq_sta = &sta_priv->lq_sta;
2836 sband = hw->wiphy->bands[conf->channel->band];
2837
2838
2839 lq_sta->lq.sta_id = sta_id;
Reinette Chatre5ad13f82009-10-30 14:36:14 -07002840
2841 for (j = 0; j < LQ_SIZE; j++)
2842 for (i = 0; i < IWL_RATE_COUNT; i++)
2843 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
2844
Tomas Winklerc33104f2008-01-14 17:46:21 -08002845 lq_sta->flush_timer = 0;
Johannes Berg4b7679a2008-09-18 18:14:18 +02002846 lq_sta->supp_rates = sta->supp_rates[sband->band];
Zhu Yib481de92007-09-25 17:54:57 -07002847 for (j = 0; j < LQ_SIZE; j++)
2848 for (i = 0; i < IWL_RATE_COUNT; i++)
Tomas Winkler3ac7f142008-07-21 02:40:14 +03002849 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
Zhu Yib481de92007-09-25 17:54:57 -07002850
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08002851 IWL_DEBUG_RATE(priv, "LQ: *** rate scale station global init for station %d ***\n",
2852 sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07002853 /* TODO: what is a good starting rate for STA? About middle? Maybe not
2854 * the lowest or the highest rate.. Could consider using RSSI from
2855 * previous packets? Need to have IEEE 802.1X auth succeed immediately
2856 * after assoc.. */
2857
Tomas Winklerc33104f2008-01-14 17:46:21 -08002858 lq_sta->is_dup = 0;
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002859 lq_sta->max_rate_idx = -1;
Abbas, Mohamed7d049e52009-01-20 21:33:53 -08002860 lq_sta->missed_rate_counter = IWL_MISSED_RATE_MAX;
Johannes Berg7e6a5882010-08-23 10:46:46 +02002861 lq_sta->is_green = rs_use_green(sta);
Johannes Berg3ac40ed2012-03-08 09:18:04 +01002862 lq_sta->band = sband->band;
2863 /*
2864 * active legacy rates as per supported rates bitmap
2865 */
2866 supp = sta->supp_rates[sband->band];
2867 lq_sta->active_legacy_rate = 0;
2868 for_each_set_bit(i, &supp, BITS_PER_LONG)
2869 lq_sta->active_legacy_rate |= BIT(sband->bitrates[i].hw_value);
2870
Ben Cahill77626352007-11-29 11:09:44 +08002871 /*
2872 * active_siso_rate mask includes 9 MBits (bit 5), and CCK (bits 0-3),
2873 * supp_rates[] does not; shift to convert format, force 9 MBits off.
2874 */
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002875 lq_sta->active_siso_rate = ht_cap->mcs.rx_mask[0] << 1;
2876 lq_sta->active_siso_rate |= ht_cap->mcs.rx_mask[0] & 0x1;
Guy Cohenfde0db32008-04-21 15:42:01 -07002877 lq_sta->active_siso_rate &= ~((u16)0x2);
2878 lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -07002879
Ben Cahill77626352007-11-29 11:09:44 +08002880 /* Same here */
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002881 lq_sta->active_mimo2_rate = ht_cap->mcs.rx_mask[1] << 1;
2882 lq_sta->active_mimo2_rate |= ht_cap->mcs.rx_mask[1] & 0x1;
Guy Cohenfde0db32008-04-21 15:42:01 -07002883 lq_sta->active_mimo2_rate &= ~((u16)0x2);
2884 lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE;
2885
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002886 lq_sta->active_mimo3_rate = ht_cap->mcs.rx_mask[2] << 1;
2887 lq_sta->active_mimo3_rate |= ht_cap->mcs.rx_mask[2] & 0x1;
Guy Cohenfde0db32008-04-21 15:42:01 -07002888 lq_sta->active_mimo3_rate &= ~((u16)0x2);
2889 lq_sta->active_mimo3_rate <<= IWL_FIRST_OFDM_RATE;
2890
Tomas Winklere1623442009-01-27 14:27:56 -08002891 IWL_DEBUG_RATE(priv, "SISO-RATE=%X MIMO2-RATE=%X MIMO3-RATE=%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002892 lq_sta->active_siso_rate,
Guy Cohenfde0db32008-04-21 15:42:01 -07002893 lq_sta->active_mimo2_rate,
2894 lq_sta->active_mimo3_rate);
2895
Tomas Winklera96a27f2008-10-23 23:48:56 -07002896 /* These values will be overridden later */
Wey-Yi Guy3a23d692010-04-03 16:44:39 -07002897 lq_sta->lq.general_params.single_stream_ant_msk =
Eytan Lifshitzb7998c82012-12-01 20:59:49 +02002898 first_antenna(priv->nvm_data->valid_tx_ant);
Wey-Yi Guy3a23d692010-04-03 16:44:39 -07002899 lq_sta->lq.general_params.dual_stream_ant_msk =
Eytan Lifshitzb7998c82012-12-01 20:59:49 +02002900 priv->nvm_data->valid_tx_ant &
2901 ~first_antenna(priv->nvm_data->valid_tx_ant);
Wey-Yi Guy3a23d692010-04-03 16:44:39 -07002902 if (!lq_sta->lq.general_params.dual_stream_ant_msk) {
2903 lq_sta->lq.general_params.dual_stream_ant_msk = ANT_AB;
Eytan Lifshitzb7998c82012-12-01 20:59:49 +02002904 } else if (num_of_ant(priv->nvm_data->valid_tx_ant) == 2) {
Wey-Yi Guy3a23d692010-04-03 16:44:39 -07002905 lq_sta->lq.general_params.dual_stream_ant_msk =
Eytan Lifshitzb7998c82012-12-01 20:59:49 +02002906 priv->nvm_data->valid_tx_ant;
Wey-Yi Guy3a23d692010-04-03 16:44:39 -07002907 }
Guy Cohen07bc28e2008-04-23 17:15:03 -07002908
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002909 /* as default allow aggregation for all tids */
2910 lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002911 lq_sta->drv = priv;
Zhu Yib481de92007-09-25 17:54:57 -07002912
Daniel C Halperin50273092009-08-28 09:44:45 -07002913 /* Set last_txrate_idx to lowest rate */
2914 lq_sta->last_txrate_idx = rate_lowest_index(sband, sta);
2915 if (sband->band == IEEE80211_BAND_5GHZ)
2916 lq_sta->last_txrate_idx += IWL_FIRST_OFDM_RATE;
Daniel C Halperine3949d62009-09-17 10:43:50 -07002917 lq_sta->is_agg = 0;
Wey-Yi Guy5ef15cc2011-11-30 13:24:06 -08002918#ifdef CONFIG_IWLWIFI_DEVICE_TESTMODE
Wey-Yi Guy4e308112011-07-08 08:46:28 -07002919 priv->tm_fixed_rate = 0;
Wey-Yi Guyc10e2c12011-07-13 11:13:46 -07002920#endif
Sedat Dilek7d5f01a2010-12-16 12:46:23 +01002921#ifdef CONFIG_MAC80211_DEBUGFS
Shanyu Zhaoa1da0772010-12-02 11:02:54 -08002922 lq_sta->dbg_fixed_rate = 0;
Sedat Dilek7d5f01a2010-12-16 12:46:23 +01002923#endif
Shanyu Zhaoa1da0772010-12-02 11:02:54 -08002924
Johannes Berg9b6ca442012-03-07 09:52:11 -08002925 rs_initialize_lq(priv, sta, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002926}
2927
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002928static void rs_fill_link_cmd(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08002929 struct iwl_lq_sta *lq_sta, u32 new_rate)
Zhu Yib481de92007-09-25 17:54:57 -07002930{
Tomas Winklere227cea2008-07-18 13:53:05 +08002931 struct iwl_scale_tbl_info tbl_type;
Zhu Yib481de92007-09-25 17:54:57 -07002932 int index = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002933 int rate_idx;
Zhu Yi1b696de2007-09-27 11:27:41 +08002934 int repeat_rate = 0;
Guy Cohenaade00c2008-04-23 17:14:59 -07002935 u8 ant_toggle_cnt = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002936 u8 use_ht_possible = 1;
Guy Cohenaade00c2008-04-23 17:14:59 -07002937 u8 valid_tx_ant = 0;
Johannes Berg7b090682011-01-19 02:53:54 -08002938 struct iwl_station_priv *sta_priv =
2939 container_of(lq_sta, struct iwl_station_priv, lq_sta);
Guy Cohen07bc28e2008-04-23 17:15:03 -07002940 struct iwl_link_quality_cmd *lq_cmd = &lq_sta->lq;
Zhu Yib481de92007-09-25 17:54:57 -07002941
Ben Cahill77626352007-11-29 11:09:44 +08002942 /* Override starting rate (index 0) if needed for debug purposes */
Guy Cohen39e88502008-04-23 17:14:57 -07002943 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Zhu Yi98d7e092007-09-27 11:27:42 +08002944
Guy Cohen39e88502008-04-23 17:14:57 -07002945 /* Interpret new_rate (rate_n_flags) */
2946 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band,
Zhu Yib481de92007-09-25 17:54:57 -07002947 &tbl_type, &rate_idx);
2948
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07002949 if (priv && priv->bt_full_concurrent) {
2950 /* 1x1 only */
2951 tbl_type.ant_type =
Eytan Lifshitzb7998c82012-12-01 20:59:49 +02002952 first_antenna(priv->nvm_data->valid_tx_ant);
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07002953 }
2954
Ben Cahill77626352007-11-29 11:09:44 +08002955 /* How many times should we repeat the initial rate? */
Zhu Yib481de92007-09-25 17:54:57 -07002956 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07002957 ant_toggle_cnt = 1;
Zhu Yi1b696de2007-09-27 11:27:41 +08002958 repeat_rate = IWL_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002959 } else {
Daniel Halperin374920c2011-03-16 19:16:36 -07002960 repeat_rate = min(IWL_HT_NUMBER_TRY,
2961 LINK_QUAL_AGG_DISABLE_START_DEF - 1);
Guy Cohenaade00c2008-04-23 17:14:59 -07002962 }
Zhu Yib481de92007-09-25 17:54:57 -07002963
2964 lq_cmd->general_params.mimo_delimiter =
2965 is_mimo(tbl_type.lq_type) ? 1 : 0;
Ben Cahill77626352007-11-29 11:09:44 +08002966
2967 /* Fill 1st table entry (index 0) */
Guy Cohen39e88502008-04-23 17:14:57 -07002968 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002969
Guy Cohen07bc28e2008-04-23 17:15:03 -07002970 if (num_of_ant(tbl_type.ant_type) == 1) {
2971 lq_cmd->general_params.single_stream_ant_msk =
2972 tbl_type.ant_type;
2973 } else if (num_of_ant(tbl_type.ant_type) == 2) {
2974 lq_cmd->general_params.dual_stream_ant_msk =
2975 tbl_type.ant_type;
2976 } /* otherwise we don't modify the existing value */
Zhu Yib481de92007-09-25 17:54:57 -07002977
2978 index++;
Zhu Yi1b696de2007-09-27 11:27:41 +08002979 repeat_rate--;
Wey-Yi Guybee008b2010-08-23 07:57:04 -07002980 if (priv) {
2981 if (priv->bt_full_concurrent)
2982 valid_tx_ant = ANT_A;
2983 else
Eytan Lifshitzb7998c82012-12-01 20:59:49 +02002984 valid_tx_ant = priv->nvm_data->valid_tx_ant;
Wey-Yi Guybee008b2010-08-23 07:57:04 -07002985 }
Guy Cohenaade00c2008-04-23 17:14:59 -07002986
Ben Cahill77626352007-11-29 11:09:44 +08002987 /* Fill rest of rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002988 while (index < LINK_QUAL_MAX_RETRY_NUM) {
Ben Cahill77626352007-11-29 11:09:44 +08002989 /* Repeat initial/next rate.
2990 * For legacy IWL_NUMBER_TRY == 1, this loop will not execute.
2991 * For HT IWL_HT_NUMBER_TRY == 3, this executes twice. */
Zhu Yi1b696de2007-09-27 11:27:41 +08002992 while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
Zhu Yib481de92007-09-25 17:54:57 -07002993 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07002994 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2995 ant_toggle_cnt++;
2996 else if (priv &&
2997 rs_toggle_antenna(valid_tx_ant,
2998 &new_rate, &tbl_type))
2999 ant_toggle_cnt = 1;
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07003000 }
Zhu Yi98d7e092007-09-27 11:27:42 +08003001
Ben Cahill77626352007-11-29 11:09:44 +08003002 /* Override next rate if needed for debug purposes */
Tomas Winklerc33104f2008-01-14 17:46:21 -08003003 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Ben Cahill77626352007-11-29 11:09:44 +08003004
3005 /* Fill next table entry */
Zhu Yib481de92007-09-25 17:54:57 -07003006 lq_cmd->rs_table[index].rate_n_flags =
Guy Cohen39e88502008-04-23 17:14:57 -07003007 cpu_to_le32(new_rate);
Zhu Yi1b696de2007-09-27 11:27:41 +08003008 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07003009 index++;
3010 }
3011
Guy Cohen39e88502008-04-23 17:14:57 -07003012 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band, &tbl_type,
Zhu Yib481de92007-09-25 17:54:57 -07003013 &rate_idx);
3014
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07003015 if (priv && priv->bt_full_concurrent) {
3016 /* 1x1 only */
3017 tbl_type.ant_type =
Eytan Lifshitzb7998c82012-12-01 20:59:49 +02003018 first_antenna(priv->nvm_data->valid_tx_ant);
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07003019 }
3020
Ben Cahill77626352007-11-29 11:09:44 +08003021 /* Indicate to uCode which entries might be MIMO.
3022 * If initial rate was MIMO, this will finally end up
3023 * as (IWL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
Zhu Yib481de92007-09-25 17:54:57 -07003024 if (is_mimo(tbl_type.lq_type))
3025 lq_cmd->general_params.mimo_delimiter = index;
3026
Ben Cahill77626352007-11-29 11:09:44 +08003027 /* Get next rate */
Guy Cohen39e88502008-04-23 17:14:57 -07003028 new_rate = rs_get_lower_rate(lq_sta, &tbl_type, rate_idx,
3029 use_ht_possible);
Zhu Yib481de92007-09-25 17:54:57 -07003030
Ben Cahill77626352007-11-29 11:09:44 +08003031 /* How many times should we repeat the next rate? */
Zhu Yib481de92007-09-25 17:54:57 -07003032 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07003033 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
3034 ant_toggle_cnt++;
3035 else if (priv &&
3036 rs_toggle_antenna(valid_tx_ant,
3037 &new_rate, &tbl_type))
3038 ant_toggle_cnt = 1;
3039
Zhu Yi1b696de2007-09-27 11:27:41 +08003040 repeat_rate = IWL_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07003041 } else {
Zhu Yi1b696de2007-09-27 11:27:41 +08003042 repeat_rate = IWL_HT_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07003043 }
Zhu Yib481de92007-09-25 17:54:57 -07003044
Ben Cahill77626352007-11-29 11:09:44 +08003045 /* Don't allow HT rates after next pass.
3046 * rs_get_lower_rate() will change type to LQ_A or LQ_G. */
Zhu Yib481de92007-09-25 17:54:57 -07003047 use_ht_possible = 0;
3048
Ben Cahill77626352007-11-29 11:09:44 +08003049 /* Override next rate if needed for debug purposes */
Tomas Winklerc33104f2008-01-14 17:46:21 -08003050 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Ben Cahill77626352007-11-29 11:09:44 +08003051
3052 /* Fill next table entry */
Guy Cohen39e88502008-04-23 17:14:57 -07003053 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
Zhu Yib481de92007-09-25 17:54:57 -07003054
3055 index++;
Zhu Yi1b696de2007-09-27 11:27:41 +08003056 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07003057 }
3058
Johannes Berg7b090682011-01-19 02:53:54 -08003059 lq_cmd->agg_params.agg_frame_cnt_limit =
3060 sta_priv->max_agg_bufsize ?: LINK_QUAL_AGG_FRAME_LIMIT_DEF;
Wey-Yi Guy13c33a02009-06-03 11:44:11 -07003061 lq_cmd->agg_params.agg_dis_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
Wey-Yi Guy74697012010-07-28 09:18:54 -07003062
Wey-Yi Guy13c33a02009-06-03 11:44:11 -07003063 lq_cmd->agg_params.agg_time_limit =
3064 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
Wey-Yi Guy74697012010-07-28 09:18:54 -07003065 /*
3066 * overwrite if needed, pass aggregation time limit
3067 * to uCode in uSec
3068 */
Emmanuel Grumbach21522682012-03-22 17:51:44 +02003069 if (priv && priv->cfg->bt_params &&
3070 priv->cfg->bt_params->agg_time_limit &&
Wey-Yi Guy66e863a52010-11-08 14:54:37 -08003071 priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH)
Wey-Yi Guy74697012010-07-28 09:18:54 -07003072 lq_cmd->agg_params.agg_time_limit =
Emmanuel Grumbach21522682012-03-22 17:51:44 +02003073 cpu_to_le16(priv->cfg->bt_params->agg_time_limit);
Zhu Yib481de92007-09-25 17:54:57 -07003074}
3075
Johannes Berg4b7679a2008-09-18 18:14:18 +02003076static void *rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
Zhu Yib481de92007-09-25 17:54:57 -07003077{
Johannes Berg4b7679a2008-09-18 18:14:18 +02003078 return hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003079}
3080/* rate scale requires free function to be implemented */
3081static void rs_free(void *priv_rate)
3082{
3083 return;
3084}
3085
Johannes Berg4b7679a2008-09-18 18:14:18 +02003086static void rs_free_sta(void *priv_r, struct ieee80211_sta *sta,
3087 void *priv_sta)
Zhu Yib481de92007-09-25 17:54:57 -07003088{
Emmanuel Grumbachd0f76d62012-02-09 16:08:15 +02003089 struct iwl_op_mode *op_mode __maybe_unused = priv_r;
3090 struct iwl_priv *priv __maybe_unused = IWL_OP_MODE_GET_DVM(op_mode);
Zhu Yib481de92007-09-25 17:54:57 -07003091
Tomas Winklere1623442009-01-27 14:27:56 -08003092 IWL_DEBUG_RATE(priv, "enter\n");
Tomas Winklere1623442009-01-27 14:27:56 -08003093 IWL_DEBUG_RATE(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003094}
3095
Zhu Yi93dc6462007-09-27 11:27:37 +08003096#ifdef CONFIG_MAC80211_DEBUGFS
Tomas Winklere227cea2008-07-18 13:53:05 +08003097static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
3098 u32 *rate_n_flags, int index)
Zhu Yi98d7e092007-09-27 11:27:42 +08003099{
Ester Kummerbf403db2008-05-05 10:22:40 +08003100 struct iwl_priv *priv;
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07003101 u8 valid_tx_ant;
3102 u8 ant_sel_tx;
Ester Kummerbf403db2008-05-05 10:22:40 +08003103
3104 priv = lq_sta->drv;
Eytan Lifshitzb7998c82012-12-01 20:59:49 +02003105 valid_tx_ant = priv->nvm_data->valid_tx_ant;
Guy Cohen39e88502008-04-23 17:14:57 -07003106 if (lq_sta->dbg_fixed_rate) {
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07003107 ant_sel_tx =
3108 ((lq_sta->dbg_fixed_rate & RATE_MCS_ANT_ABC_MSK)
3109 >> RATE_MCS_ANT_POS);
3110 if ((valid_tx_ant & ant_sel_tx) == ant_sel_tx) {
Guy Cohen39e88502008-04-23 17:14:57 -07003111 *rate_n_flags = lq_sta->dbg_fixed_rate;
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07003112 IWL_DEBUG_RATE(priv, "Fixed rate ON\n");
Guy Cohen39e88502008-04-23 17:14:57 -07003113 } else {
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07003114 lq_sta->dbg_fixed_rate = 0;
3115 IWL_ERR(priv,
3116 "Invalid antenna selection 0x%X, Valid is 0x%X\n",
3117 ant_sel_tx, valid_tx_ant);
3118 IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
Guy Cohen39e88502008-04-23 17:14:57 -07003119 }
Guy Cohen39e88502008-04-23 17:14:57 -07003120 } else {
Tomas Winklere1623442009-01-27 14:27:56 -08003121 IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
Zhu Yi98d7e092007-09-27 11:27:42 +08003122 }
Zhu Yi98d7e092007-09-27 11:27:42 +08003123}
3124
3125static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
3126 const char __user *user_buf, size_t count, loff_t *ppos)
3127{
Tomas Winklere227cea2008-07-18 13:53:05 +08003128 struct iwl_lq_sta *lq_sta = file->private_data;
Ester Kummerbf403db2008-05-05 10:22:40 +08003129 struct iwl_priv *priv;
Zhu Yi98d7e092007-09-27 11:27:42 +08003130 char buf[64];
Stephen Boyd805d7d22011-05-12 16:50:05 -07003131 size_t buf_size;
Zhu Yi98d7e092007-09-27 11:27:42 +08003132 u32 parsed_rate;
Wey-Yi Guy64898542011-05-03 18:05:13 -07003133
Zhu Yi98d7e092007-09-27 11:27:42 +08003134
Ester Kummerbf403db2008-05-05 10:22:40 +08003135 priv = lq_sta->drv;
Zhu Yi98d7e092007-09-27 11:27:42 +08003136 memset(buf, 0, sizeof(buf));
3137 buf_size = min(count, sizeof(buf) - 1);
3138 if (copy_from_user(buf, user_buf, buf_size))
3139 return -EFAULT;
3140
3141 if (sscanf(buf, "%x", &parsed_rate) == 1)
Wey-Yi Guy4e308112011-07-08 08:46:28 -07003142 lq_sta->dbg_fixed_rate = parsed_rate;
Zhu Yi98d7e092007-09-27 11:27:42 +08003143 else
Wey-Yi Guy4e308112011-07-08 08:46:28 -07003144 lq_sta->dbg_fixed_rate = 0;
Zhu Yi98d7e092007-09-27 11:27:42 +08003145
Wey-Yi Guy64898542011-05-03 18:05:13 -07003146 rs_program_fix_rate(priv, lq_sta);
Zhu Yi98d7e092007-09-27 11:27:42 +08003147
3148 return count;
3149}
Zhu Yi0209dc12007-09-27 11:27:43 +08003150
Zhu Yi5ae212c2007-09-27 11:27:38 +08003151static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
3152 char __user *user_buf, size_t count, loff_t *ppos)
3153{
Frank Seidela412c802009-03-01 20:25:38 +01003154 char *buff;
Zhu Yi5ae212c2007-09-27 11:27:38 +08003155 int desc = 0;
3156 int i = 0;
Wey-Yi Guy12b96812009-04-08 11:39:27 -07003157 int index = 0;
Frank Seidela412c802009-03-01 20:25:38 +01003158 ssize_t ret;
Zhu Yi5ae212c2007-09-27 11:27:38 +08003159
Tomas Winklere227cea2008-07-18 13:53:05 +08003160 struct iwl_lq_sta *lq_sta = file->private_data;
Wey-Yi Guyd8ae4f52009-03-10 14:35:07 -07003161 struct iwl_priv *priv;
Wey-Yi Guyadb7b5e2009-03-10 14:35:08 -07003162 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
Zhu Yi5ae212c2007-09-27 11:27:38 +08003163
Wey-Yi Guyd8ae4f52009-03-10 14:35:07 -07003164 priv = lq_sta->drv;
Frank Seidela412c802009-03-01 20:25:38 +01003165 buff = kmalloc(1024, GFP_KERNEL);
3166 if (!buff)
3167 return -ENOMEM;
3168
Tomas Winklerc33104f2008-01-14 17:46:21 -08003169 desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
Zhu Yi98d7e092007-09-27 11:27:42 +08003170 desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08003171 lq_sta->total_failed, lq_sta->total_success,
Guy Cohen39e88502008-04-23 17:14:57 -07003172 lq_sta->active_legacy_rate);
Zhu Yi98d7e092007-09-27 11:27:42 +08003173 desc += sprintf(buff+desc, "fixed rate 0x%X\n",
Wey-Yi Guy4e308112011-07-08 08:46:28 -07003174 lq_sta->dbg_fixed_rate);
Wey-Yi Guyd8ae4f52009-03-10 14:35:07 -07003175 desc += sprintf(buff+desc, "valid_tx_ant %s%s%s\n",
Eytan Lifshitzb7998c82012-12-01 20:59:49 +02003176 (priv->nvm_data->valid_tx_ant & ANT_A) ? "ANT_A," : "",
3177 (priv->nvm_data->valid_tx_ant & ANT_B) ? "ANT_B," : "",
3178 (priv->nvm_data->valid_tx_ant & ANT_C) ? "ANT_C" : "");
Wey-Yi Guyadb7b5e2009-03-10 14:35:08 -07003179 desc += sprintf(buff+desc, "lq type %s\n",
3180 (is_legacy(tbl->lq_type)) ? "legacy" : "HT");
3181 if (is_Ht(tbl->lq_type)) {
3182 desc += sprintf(buff+desc, " %s",
3183 (is_siso(tbl->lq_type)) ? "SISO" :
3184 ((is_mimo2(tbl->lq_type)) ? "MIMO2" : "MIMO3"));
3185 desc += sprintf(buff+desc, " %s",
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07003186 (tbl->is_ht40) ? "40MHz" : "20MHz");
Daniel C Halperine3949d62009-09-17 10:43:50 -07003187 desc += sprintf(buff+desc, " %s %s %s\n", (tbl->is_SGI) ? "SGI" : "",
3188 (lq_sta->is_green) ? "GF enabled" : "",
3189 (lq_sta->is_agg) ? "AGG on" : "");
Wey-Yi Guyadb7b5e2009-03-10 14:35:08 -07003190 }
Wey-Yi Guy12b96812009-04-08 11:39:27 -07003191 desc += sprintf(buff+desc, "last tx rate=0x%X\n",
3192 lq_sta->last_rate_n_flags);
Zhu Yi5ae212c2007-09-27 11:27:38 +08003193 desc += sprintf(buff+desc, "general:"
3194 "flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08003195 lq_sta->lq.general_params.flags,
3196 lq_sta->lq.general_params.mimo_delimiter,
3197 lq_sta->lq.general_params.single_stream_ant_msk,
3198 lq_sta->lq.general_params.dual_stream_ant_msk);
Zhu Yi5ae212c2007-09-27 11:27:38 +08003199
3200 desc += sprintf(buff+desc, "agg:"
3201 "time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08003202 le16_to_cpu(lq_sta->lq.agg_params.agg_time_limit),
3203 lq_sta->lq.agg_params.agg_dis_start_th,
3204 lq_sta->lq.agg_params.agg_frame_cnt_limit);
Zhu Yi5ae212c2007-09-27 11:27:38 +08003205
3206 desc += sprintf(buff+desc,
3207 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08003208 lq_sta->lq.general_params.start_rate_index[0],
3209 lq_sta->lq.general_params.start_rate_index[1],
3210 lq_sta->lq.general_params.start_rate_index[2],
3211 lq_sta->lq.general_params.start_rate_index[3]);
Zhu Yi5ae212c2007-09-27 11:27:38 +08003212
Wey-Yi Guy12b96812009-04-08 11:39:27 -07003213 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
3214 index = iwl_hwrate_to_plcp_idx(
3215 le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags));
3216 if (is_legacy(tbl->lq_type)) {
3217 desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps\n",
3218 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags),
3219 iwl_rate_mcs[index].mbps);
3220 } else {
3221 desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps (%s)\n",
3222 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags),
3223 iwl_rate_mcs[index].mbps, iwl_rate_mcs[index].mcs);
3224 }
3225 }
Zhu Yi5ae212c2007-09-27 11:27:38 +08003226
Frank Seidela412c802009-03-01 20:25:38 +01003227 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
3228 kfree(buff);
3229 return ret;
Zhu Yi5ae212c2007-09-27 11:27:38 +08003230}
3231
3232static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
Zhu Yi98d7e092007-09-27 11:27:42 +08003233 .write = rs_sta_dbgfs_scale_table_write,
Zhu Yi5ae212c2007-09-27 11:27:38 +08003234 .read = rs_sta_dbgfs_scale_table_read,
Stephen Boyd234e3402012-04-05 14:25:11 -07003235 .open = simple_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003236 .llseek = default_llseek,
Zhu Yi5ae212c2007-09-27 11:27:38 +08003237};
Zhu Yi0209dc12007-09-27 11:27:43 +08003238static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
3239 char __user *user_buf, size_t count, loff_t *ppos)
3240{
Frank Seidela412c802009-03-01 20:25:38 +01003241 char *buff;
Zhu Yi0209dc12007-09-27 11:27:43 +08003242 int desc = 0;
3243 int i, j;
Frank Seidela412c802009-03-01 20:25:38 +01003244 ssize_t ret;
Zhu Yi0209dc12007-09-27 11:27:43 +08003245
Tomas Winklere227cea2008-07-18 13:53:05 +08003246 struct iwl_lq_sta *lq_sta = file->private_data;
Frank Seidela412c802009-03-01 20:25:38 +01003247
3248 buff = kmalloc(1024, GFP_KERNEL);
3249 if (!buff)
3250 return -ENOMEM;
3251
Zhu Yi0209dc12007-09-27 11:27:43 +08003252 for (i = 0; i < LQ_SIZE; i++) {
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07003253 desc += sprintf(buff+desc,
3254 "%s type=%d SGI=%d HT40=%d DUP=%d GF=%d\n"
Zhu Yi0209dc12007-09-27 11:27:43 +08003255 "rate=0x%X\n",
Abhijeet Kolekarc3056062008-11-12 13:14:08 -08003256 lq_sta->active_tbl == i ? "*" : "x",
Tomas Winklerc33104f2008-01-14 17:46:21 -08003257 lq_sta->lq_info[i].lq_type,
3258 lq_sta->lq_info[i].is_SGI,
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07003259 lq_sta->lq_info[i].is_ht40,
Tomas Winklerc33104f2008-01-14 17:46:21 -08003260 lq_sta->lq_info[i].is_dup,
Wey-Yi Guy2681b202009-05-21 13:44:22 -07003261 lq_sta->is_green,
Guy Cohen39e88502008-04-23 17:14:57 -07003262 lq_sta->lq_info[i].current_rate);
Zhu Yi0209dc12007-09-27 11:27:43 +08003263 for (j = 0; j < IWL_RATE_COUNT; j++) {
3264 desc += sprintf(buff+desc,
Tomas Winklerc33104f2008-01-14 17:46:21 -08003265 "counter=%d success=%d %%=%d\n",
3266 lq_sta->lq_info[i].win[j].counter,
3267 lq_sta->lq_info[i].win[j].success_counter,
3268 lq_sta->lq_info[i].win[j].success_ratio);
Zhu Yi0209dc12007-09-27 11:27:43 +08003269 }
3270 }
Frank Seidela412c802009-03-01 20:25:38 +01003271 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
3272 kfree(buff);
3273 return ret;
Zhu Yi0209dc12007-09-27 11:27:43 +08003274}
3275
3276static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
3277 .read = rs_sta_dbgfs_stats_table_read,
Stephen Boyd234e3402012-04-05 14:25:11 -07003278 .open = simple_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003279 .llseek = default_llseek,
Zhu Yi0209dc12007-09-27 11:27:43 +08003280};
Zhu Yi5ae212c2007-09-27 11:27:38 +08003281
Wey-Yi Guy38167452009-05-08 13:44:43 -07003282static ssize_t rs_sta_dbgfs_rate_scale_data_read(struct file *file,
3283 char __user *user_buf, size_t count, loff_t *ppos)
3284{
Johannes Berg70817b52011-05-11 03:29:25 -07003285 struct iwl_lq_sta *lq_sta = file->private_data;
3286 struct iwl_scale_tbl_info *tbl = &lq_sta->lq_info[lq_sta->active_tbl];
Wey-Yi Guy38167452009-05-08 13:44:43 -07003287 char buff[120];
3288 int desc = 0;
Wey-Yi Guy38167452009-05-08 13:44:43 -07003289
Wey-Yi Guy38167452009-05-08 13:44:43 -07003290 if (is_Ht(tbl->lq_type))
3291 desc += sprintf(buff+desc,
3292 "Bit Rate= %d Mb/s\n",
3293 tbl->expected_tpt[lq_sta->last_txrate_idx]);
3294 else
3295 desc += sprintf(buff+desc,
3296 "Bit Rate= %d Mb/s\n",
3297 iwl_rates[lq_sta->last_txrate_idx].ieee >> 1);
Wey-Yi Guy38167452009-05-08 13:44:43 -07003298
Johannes Berg08960de2011-04-05 09:41:53 -07003299 return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
Wey-Yi Guy38167452009-05-08 13:44:43 -07003300}
3301
3302static const struct file_operations rs_sta_dbgfs_rate_scale_data_ops = {
3303 .read = rs_sta_dbgfs_rate_scale_data_read,
Stephen Boyd234e3402012-04-05 14:25:11 -07003304 .open = simple_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003305 .llseek = default_llseek,
Wey-Yi Guy38167452009-05-08 13:44:43 -07003306};
3307
Zhu Yi93dc6462007-09-27 11:27:37 +08003308static void rs_add_debugfs(void *priv, void *priv_sta,
3309 struct dentry *dir)
3310{
Tomas Winklere227cea2008-07-18 13:53:05 +08003311 struct iwl_lq_sta *lq_sta = priv_sta;
Tomas Winklerc33104f2008-01-14 17:46:21 -08003312 lq_sta->rs_sta_dbgfs_scale_table_file =
Wey-Yi Guy43e85112009-11-20 12:04:58 -08003313 debugfs_create_file("rate_scale_table", S_IRUSR | S_IWUSR, dir,
Tomas Winklerc33104f2008-01-14 17:46:21 -08003314 lq_sta, &rs_sta_dbgfs_scale_table_ops);
3315 lq_sta->rs_sta_dbgfs_stats_table_file =
Wey-Yi Guy43e85112009-11-20 12:04:58 -08003316 debugfs_create_file("rate_stats_table", S_IRUSR, dir,
Tomas Winklerc33104f2008-01-14 17:46:21 -08003317 lq_sta, &rs_sta_dbgfs_stats_table_ops);
Wey-Yi Guy38167452009-05-08 13:44:43 -07003318 lq_sta->rs_sta_dbgfs_rate_scale_data_file =
Wey-Yi Guy43e85112009-11-20 12:04:58 -08003319 debugfs_create_file("rate_scale_data", S_IRUSR, dir,
Wey-Yi Guy38167452009-05-08 13:44:43 -07003320 lq_sta, &rs_sta_dbgfs_rate_scale_data_ops);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02003321 lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file =
Wey-Yi Guy43e85112009-11-20 12:04:58 -08003322 debugfs_create_u8("tx_agg_tid_enable", S_IRUSR | S_IWUSR, dir,
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02003323 &lq_sta->tx_agg_tid_en);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02003324
Zhu Yi93dc6462007-09-27 11:27:37 +08003325}
3326
3327static void rs_remove_debugfs(void *priv, void *priv_sta)
3328{
Tomas Winklere227cea2008-07-18 13:53:05 +08003329 struct iwl_lq_sta *lq_sta = priv_sta;
Tomas Winklerc33104f2008-01-14 17:46:21 -08003330 debugfs_remove(lq_sta->rs_sta_dbgfs_scale_table_file);
3331 debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
Wey-Yi Guy38167452009-05-08 13:44:43 -07003332 debugfs_remove(lq_sta->rs_sta_dbgfs_rate_scale_data_file);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02003333 debugfs_remove(lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file);
Zhu Yi93dc6462007-09-27 11:27:37 +08003334}
3335#endif
3336
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003337/*
3338 * Initialization of rate scaling information is done by driver after
3339 * the station is added. Since mac80211 calls this function before a
3340 * station is added we ignore it.
3341 */
3342static void rs_rate_init_stub(void *priv_r, struct ieee80211_supported_band *sband,
3343 struct ieee80211_sta *sta, void *priv_sta)
3344{
3345}
Zhu Yib481de92007-09-25 17:54:57 -07003346static struct rate_control_ops rs_ops = {
3347 .module = NULL,
3348 .name = RS_NAME,
3349 .tx_status = rs_tx_status,
3350 .get_rate = rs_get_rate,
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003351 .rate_init = rs_rate_init_stub,
Zhu Yib481de92007-09-25 17:54:57 -07003352 .alloc = rs_alloc,
3353 .free = rs_free,
3354 .alloc_sta = rs_alloc_sta,
3355 .free_sta = rs_free_sta,
Zhu Yi93dc6462007-09-27 11:27:37 +08003356#ifdef CONFIG_MAC80211_DEBUGFS
3357 .add_sta_debugfs = rs_add_debugfs,
3358 .remove_sta_debugfs = rs_remove_debugfs,
3359#endif
Zhu Yib481de92007-09-25 17:54:57 -07003360};
3361
Tomas Winklere227cea2008-07-18 13:53:05 +08003362int iwlagn_rate_control_register(void)
Zhu Yib481de92007-09-25 17:54:57 -07003363{
Reinette Chatre897e1cf2008-03-28 16:21:09 -07003364 return ieee80211_rate_control_register(&rs_ops);
Zhu Yib481de92007-09-25 17:54:57 -07003365}
3366
Tomas Winklere227cea2008-07-18 13:53:05 +08003367void iwlagn_rate_control_unregister(void)
Zhu Yib481de92007-09-25 17:54:57 -07003368{
3369 ieee80211_rate_control_unregister(&rs_ops);
3370}
3371