blob: 97abc10f4f221a69e9000c9f5df02f572eac1f0c [file] [log] [blame]
Zhu Yib481de92007-09-25 17:54:57 -07001/******************************************************************************
2 *
Reinette Chatre1f447802010-01-15 13:43:41 -08003 * Copyright(c) 2005 - 2010 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 <linux/wireless.h>
31#include <net/mac80211.h>
Zhu Yib481de92007-09-25 17:54:57 -070032
33#include <linux/netdevice.h>
34#include <linux/etherdevice.h>
35#include <linux/delay.h>
36
37#include <linux/workqueue.h>
38
Tomas Winkler3e0d4cb2008-04-24 11:55:38 -070039#include "iwl-dev.h"
Emmanuel Grumbachbe1f3ab62008-06-12 09:47:18 +080040#include "iwl-sta.h"
Tomas Winkler857485c2008-03-21 13:53:44 -070041#include "iwl-core.h"
Johannes Berga30e3112010-09-22 18:02:01 +020042#include "iwl-agn.h"
Zhu Yib481de92007-09-25 17:54:57 -070043
Tomas Winklere227cea2008-07-18 13:53:05 +080044#define RS_NAME "iwl-agn-rs"
Zhu Yib481de92007-09-25 17:54:57 -070045
Guy Cohenaade00c2008-04-23 17:14:59 -070046#define NUM_TRY_BEFORE_ANT_TOGGLE 1
Zhu Yib481de92007-09-25 17:54:57 -070047#define IWL_NUMBER_TRY 1
48#define IWL_HT_NUMBER_TRY 3
49
Ben Cahill77626352007-11-29 11:09:44 +080050#define IWL_RATE_MAX_WINDOW 62 /* # tx in history window */
51#define IWL_RATE_MIN_FAILURE_TH 6 /* min failures to calc tpt */
52#define IWL_RATE_MIN_SUCCESS_TH 8 /* min successes to calc tpt */
53
Abbas, Mohamed7d049e52009-01-20 21:33:53 -080054/* max allowed rate miss before sync LQ cmd */
55#define IWL_MISSED_RATE_MAX 15
Ben Cahill77626352007-11-29 11:09:44 +080056/* max time to accum history 2 seconds */
Mohamed Abbas447fee72009-04-20 14:37:02 -070057#define IWL_RATE_SCALE_FLUSH_INTVL (3*HZ)
Zhu Yib481de92007-09-25 17:54:57 -070058
59static u8 rs_ht_to_legacy[] = {
60 IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
61 IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
62 IWL_RATE_6M_INDEX,
63 IWL_RATE_6M_INDEX, IWL_RATE_9M_INDEX,
64 IWL_RATE_12M_INDEX, IWL_RATE_18M_INDEX,
65 IWL_RATE_24M_INDEX, IWL_RATE_36M_INDEX,
66 IWL_RATE_48M_INDEX, IWL_RATE_54M_INDEX
67};
68
Guy Cohenaade00c2008-04-23 17:14:59 -070069static const u8 ant_toggle_lookup[] = {
70 /*ANT_NONE -> */ ANT_NONE,
71 /*ANT_A -> */ ANT_B,
72 /*ANT_B -> */ ANT_C,
73 /*ANT_AB -> */ ANT_BC,
74 /*ANT_C -> */ ANT_A,
75 /*ANT_AC -> */ ANT_AB,
76 /*ANT_BC -> */ ANT_AC,
77 /*ANT_ABC -> */ ANT_ABC,
78};
79
Johannes Berg635b85b2010-09-22 18:02:04 +020080#define IWL_DECLARE_RATE_INFO(r, s, ip, in, rp, rn, pp, np) \
81 [IWL_RATE_##r##M_INDEX] = { IWL_RATE_##r##M_PLCP, \
82 IWL_RATE_SISO_##s##M_PLCP, \
83 IWL_RATE_MIMO2_##s##M_PLCP,\
84 IWL_RATE_MIMO3_##s##M_PLCP,\
85 IWL_RATE_##r##M_IEEE, \
86 IWL_RATE_##ip##M_INDEX, \
87 IWL_RATE_##in##M_INDEX, \
88 IWL_RATE_##rp##M_INDEX, \
89 IWL_RATE_##rn##M_INDEX, \
90 IWL_RATE_##pp##M_INDEX, \
91 IWL_RATE_##np##M_INDEX }
92
93/*
94 * Parameter order:
95 * rate, ht rate, prev rate, next rate, prev tgg rate, next tgg rate
96 *
97 * If there isn't a valid next or previous rate then INV is used which
98 * maps to IWL_RATE_INVALID
99 *
100 */
101const struct iwl_rate_info iwl_rates[IWL_RATE_COUNT] = {
102 IWL_DECLARE_RATE_INFO(1, INV, INV, 2, INV, 2, INV, 2), /* 1mbps */
103 IWL_DECLARE_RATE_INFO(2, INV, 1, 5, 1, 5, 1, 5), /* 2mbps */
104 IWL_DECLARE_RATE_INFO(5, INV, 2, 6, 2, 11, 2, 11), /*5.5mbps */
105 IWL_DECLARE_RATE_INFO(11, INV, 9, 12, 9, 12, 5, 18), /* 11mbps */
106 IWL_DECLARE_RATE_INFO(6, 6, 5, 9, 5, 11, 5, 11), /* 6mbps */
107 IWL_DECLARE_RATE_INFO(9, 6, 6, 11, 6, 11, 5, 11), /* 9mbps */
108 IWL_DECLARE_RATE_INFO(12, 12, 11, 18, 11, 18, 11, 18), /* 12mbps */
109 IWL_DECLARE_RATE_INFO(18, 18, 12, 24, 12, 24, 11, 24), /* 18mbps */
110 IWL_DECLARE_RATE_INFO(24, 24, 18, 36, 18, 36, 18, 36), /* 24mbps */
111 IWL_DECLARE_RATE_INFO(36, 36, 24, 48, 24, 48, 24, 48), /* 36mbps */
112 IWL_DECLARE_RATE_INFO(48, 48, 36, 54, 36, 54, 36, 54), /* 48mbps */
113 IWL_DECLARE_RATE_INFO(54, 54, 48, INV, 48, INV, 48, INV),/* 54mbps */
114 IWL_DECLARE_RATE_INFO(60, 60, 48, INV, 48, INV, 48, INV),/* 60mbps */
115 /* FIXME:RS: ^^ should be INV (legacy) */
116};
117
Daniel Halperin25205462011-03-18 18:48:55 -0700118static inline u8 rs_extract_rate(u32 rate_n_flags)
119{
120 return (u8)(rate_n_flags & RATE_MCS_RATE_MSK);
121}
122
Johannes Berg635b85b2010-09-22 18:02:04 +0200123static int iwl_hwrate_to_plcp_idx(u32 rate_n_flags)
124{
125 int idx = 0;
126
127 /* HT rate format */
128 if (rate_n_flags & RATE_MCS_HT_MSK) {
Daniel Halperin25205462011-03-18 18:48:55 -0700129 idx = rs_extract_rate(rate_n_flags);
Johannes Berg635b85b2010-09-22 18:02:04 +0200130
131 if (idx >= IWL_RATE_MIMO3_6M_PLCP)
132 idx = idx - IWL_RATE_MIMO3_6M_PLCP;
133 else if (idx >= IWL_RATE_MIMO2_6M_PLCP)
134 idx = idx - IWL_RATE_MIMO2_6M_PLCP;
135
136 idx += IWL_FIRST_OFDM_RATE;
137 /* skip 9M not supported in ht*/
138 if (idx >= IWL_RATE_9M_INDEX)
139 idx += 1;
140 if ((idx >= IWL_FIRST_OFDM_RATE) && (idx <= IWL_LAST_OFDM_RATE))
141 return idx;
142
143 /* legacy rate format, search for match in table */
144 } else {
145 for (idx = 0; idx < ARRAY_SIZE(iwl_rates); idx++)
Daniel Halperin25205462011-03-18 18:48:55 -0700146 if (iwl_rates[idx].plcp ==
147 rs_extract_rate(rate_n_flags))
Johannes Berg635b85b2010-09-22 18:02:04 +0200148 return idx;
149 }
150
151 return -1;
152}
153
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700154static void rs_rate_scale_perform(struct iwl_priv *priv,
Gábor Stefanik514d65c2009-04-23 19:36:08 +0200155 struct sk_buff *skb,
Johannes Berg4b7679a2008-09-18 18:14:18 +0200156 struct ieee80211_sta *sta,
157 struct iwl_lq_sta *lq_sta);
Wey-Yi Guy46f93812009-07-24 11:13:03 -0700158static void rs_fill_link_cmd(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +0800159 struct iwl_lq_sta *lq_sta, u32 rate_n_flags);
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -0700160static void rs_stay_in_table(struct iwl_lq_sta *lq_sta, bool force_search);
Zhu Yib481de92007-09-25 17:54:57 -0700161
162
Zhu Yi98d7e092007-09-27 11:27:42 +0800163#ifdef CONFIG_MAC80211_DEBUGFS
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#else
Tomas Winklere227cea2008-07-18 13:53:05 +0800167static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
168 u32 *rate_n_flags, int index)
Zhu Yi98d7e092007-09-27 11:27:42 +0800169{}
170#endif
Ben Cahill77626352007-11-29 11:09:44 +0800171
Daniel C Halperine3949d62009-09-17 10:43:50 -0700172/**
173 * The following tables contain the expected throughput metrics for all rates
174 *
175 * 1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
176 *
177 * where invalid entries are zeros.
178 *
179 * CCK rates are only valid in legacy table and will only be used in G
180 * (2.4 GHz) band.
Ben Cahill77626352007-11-29 11:09:44 +0800181 */
Wey-Yi Guy584a0f02009-04-01 14:33:24 -0700182
Daniel C Halperine3949d62009-09-17 10:43:50 -0700183static s32 expected_tpt_legacy[IWL_RATE_COUNT] = {
184 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 0
Zhu Yib481de92007-09-25 17:54:57 -0700185};
186
Daniel C Halperine3949d62009-09-17 10:43:50 -0700187static s32 expected_tpt_siso20MHz[4][IWL_RATE_COUNT] = {
Wey-Yi Guy7fc11e92011-01-15 09:16:59 -0800188 {0, 0, 0, 0, 42, 0, 76, 102, 124, 159, 183, 193, 202}, /* Norm */
189 {0, 0, 0, 0, 46, 0, 82, 110, 132, 168, 192, 202, 210}, /* SGI */
190 {0, 0, 0, 0, 47, 0, 91, 133, 171, 242, 305, 334, 362}, /* AGG */
191 {0, 0, 0, 0, 52, 0, 101, 145, 187, 264, 330, 361, 390}, /* AGG+SGI */
Zhu Yib481de92007-09-25 17:54:57 -0700192};
193
Daniel C Halperine3949d62009-09-17 10:43:50 -0700194static s32 expected_tpt_siso40MHz[4][IWL_RATE_COUNT] = {
195 {0, 0, 0, 0, 77, 0, 127, 160, 184, 220, 242, 250, 257}, /* Norm */
196 {0, 0, 0, 0, 83, 0, 135, 169, 193, 229, 250, 257, 264}, /* SGI */
Wey-Yi Guy7fc11e92011-01-15 09:16:59 -0800197 {0, 0, 0, 0, 94, 0, 177, 249, 313, 423, 512, 550, 586}, /* AGG */
198 {0, 0, 0, 0, 104, 0, 193, 270, 338, 454, 545, 584, 620}, /* AGG+SGI */
Zhu Yib481de92007-09-25 17:54:57 -0700199};
200
Daniel C Halperine3949d62009-09-17 10:43:50 -0700201static s32 expected_tpt_mimo2_20MHz[4][IWL_RATE_COUNT] = {
Wey-Yi Guy7fc11e92011-01-15 09:16:59 -0800202 {0, 0, 0, 0, 74, 0, 123, 155, 179, 214, 236, 244, 251}, /* Norm */
203 {0, 0, 0, 0, 81, 0, 131, 164, 188, 223, 243, 251, 257}, /* SGI */
204 {0, 0, 0, 0, 89, 0, 167, 235, 296, 402, 488, 526, 560}, /* AGG */
205 {0, 0, 0, 0, 97, 0, 182, 255, 320, 431, 520, 558, 593}, /* AGG+SGI*/
Zhu Yib481de92007-09-25 17:54:57 -0700206};
207
Daniel C Halperine3949d62009-09-17 10:43:50 -0700208static s32 expected_tpt_mimo2_40MHz[4][IWL_RATE_COUNT] = {
209 {0, 0, 0, 0, 123, 0, 182, 214, 235, 264, 279, 285, 289}, /* Norm */
210 {0, 0, 0, 0, 131, 0, 191, 222, 242, 270, 284, 289, 293}, /* SGI */
Wey-Yi Guy7fc11e92011-01-15 09:16:59 -0800211 {0, 0, 0, 0, 171, 0, 305, 410, 496, 634, 731, 771, 805}, /* AGG */
212 {0, 0, 0, 0, 186, 0, 329, 439, 527, 667, 764, 803, 838}, /* AGG+SGI */
Zhu Yib481de92007-09-25 17:54:57 -0700213};
214
Daniel C Halperine3949d62009-09-17 10:43:50 -0700215static s32 expected_tpt_mimo3_20MHz[4][IWL_RATE_COUNT] = {
216 {0, 0, 0, 0, 99, 0, 153, 186, 208, 239, 256, 263, 268}, /* Norm */
217 {0, 0, 0, 0, 106, 0, 162, 194, 215, 246, 262, 268, 273}, /* SGI */
218 {0, 0, 0, 0, 134, 0, 249, 346, 431, 574, 685, 732, 775}, /* AGG */
219 {0, 0, 0, 0, 148, 0, 272, 376, 465, 614, 727, 775, 818}, /* AGG+SGI */
Zhu Yib481de92007-09-25 17:54:57 -0700220};
221
Daniel C Halperine3949d62009-09-17 10:43:50 -0700222static s32 expected_tpt_mimo3_40MHz[4][IWL_RATE_COUNT] = {
223 {0, 0, 0, 0, 152, 0, 211, 239, 255, 279, 290, 294, 297}, /* Norm */
224 {0, 0, 0, 0, 160, 0, 219, 245, 261, 284, 294, 297, 300}, /* SGI */
225 {0, 0, 0, 0, 254, 0, 443, 584, 695, 868, 984, 1030, 1070}, /* AGG */
226 {0, 0, 0, 0, 277, 0, 478, 624, 737, 911, 1026, 1070, 1109}, /* AGG+SGI */
Wey-Yi Guy584a0f02009-04-01 14:33:24 -0700227};
228
Wey-Yi Guy12b96812009-04-08 11:39:27 -0700229/* mbps, mcs */
Tobias Klauser79496732009-12-23 14:18:11 +0100230static const struct iwl_rate_mcs_info iwl_rate_mcs[IWL_RATE_COUNT] = {
Daniel C Halperine3949d62009-09-17 10:43:50 -0700231 { "1", "BPSK DSSS"},
232 { "2", "QPSK DSSS"},
233 {"5.5", "BPSK CCK"},
234 { "11", "QPSK CCK"},
235 { "6", "BPSK 1/2"},
236 { "9", "BPSK 1/2"},
237 { "12", "QPSK 1/2"},
238 { "18", "QPSK 3/4"},
239 { "24", "16QAM 1/2"},
240 { "36", "16QAM 3/4"},
241 { "48", "64QAM 2/3"},
242 { "54", "64QAM 3/4"},
243 { "60", "64QAM 5/6"},
Wey-Yi Guy12b96812009-04-08 11:39:27 -0700244};
245
Wey-Yi Guya9c146b2009-05-22 11:01:48 -0700246#define MCS_INDEX_PER_STREAM (8)
247
Tomas Winklere227cea2008-07-18 13:53:05 +0800248static void rs_rate_scale_clear_window(struct iwl_rate_scale_data *window)
Zhu Yib481de92007-09-25 17:54:57 -0700249{
250 window->data = 0;
251 window->success_counter = 0;
252 window->success_ratio = IWL_INVALID_VALUE;
253 window->counter = 0;
254 window->average_tpt = IWL_INVALID_VALUE;
255 window->stamp = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700256}
257
Guy Cohenaade00c2008-04-23 17:14:59 -0700258static inline u8 rs_is_valid_ant(u8 valid_antenna, u8 ant_type)
259{
Tomas Winkler3ac7f142008-07-21 02:40:14 +0300260 return (ant_type & valid_antenna) == ant_type;
Guy Cohenaade00c2008-04-23 17:14:59 -0700261}
262
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200263/*
264 * removes the old data from the statistics. All data that is older than
265 * TID_MAX_TIME_DIFF, will be deleted.
266 */
Tomas Winklere227cea2008-07-18 13:53:05 +0800267static void rs_tl_rm_old_stats(struct iwl_traffic_load *tl, u32 curr_time)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200268{
269 /* The oldest age we want to keep */
270 u32 oldest_time = curr_time - TID_MAX_TIME_DIFF;
271
272 while (tl->queue_count &&
273 (tl->time_stamp < oldest_time)) {
274 tl->total -= tl->packet_count[tl->head];
275 tl->packet_count[tl->head] = 0;
276 tl->time_stamp += TID_QUEUE_CELL_SPACING;
277 tl->queue_count--;
278 tl->head++;
279 if (tl->head >= TID_QUEUE_MAX_SIZE)
280 tl->head = 0;
281 }
282}
283
284/*
285 * increment traffic load value for tid and also remove
286 * any old values if passed the certain time period
287 */
Tomas Winklere227cea2008-07-18 13:53:05 +0800288static u8 rs_tl_add_packet(struct iwl_lq_sta *lq_data,
Ron Rindjunskyfaa29712008-06-12 09:46:58 +0800289 struct ieee80211_hdr *hdr)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200290{
291 u32 curr_time = jiffies_to_msecs(jiffies);
292 u32 time_diff;
293 s32 index;
Tomas Winklere227cea2008-07-18 13:53:05 +0800294 struct iwl_traffic_load *tl = NULL;
Tomas Winkler54dbb522008-05-15 13:54:06 +0800295 u8 tid;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200296
Tomas Winkler417f1142008-11-12 13:14:06 -0800297 if (ieee80211_is_data_qos(hdr->frame_control)) {
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700298 u8 *qc = ieee80211_get_qos_ctl(hdr);
Tomas Winkler54dbb522008-05-15 13:54:06 +0800299 tid = qc[0] & 0xf;
300 } else
Ron Rindjunskyfaa29712008-06-12 09:46:58 +0800301 return MAX_TID_COUNT;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200302
Reinette Chatree6a6cf42009-08-13 13:30:50 -0700303 if (unlikely(tid >= TID_MAX_LOAD_COUNT))
304 return MAX_TID_COUNT;
305
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200306 tl = &lq_data->load[tid];
307
308 curr_time -= curr_time % TID_ROUND_VALUE;
309
310 /* Happens only for the first packet. Initialize the data */
311 if (!(tl->queue_count)) {
312 tl->total = 1;
313 tl->time_stamp = curr_time;
314 tl->queue_count = 1;
315 tl->head = 0;
316 tl->packet_count[0] = 1;
Ron Rindjunskyfaa29712008-06-12 09:46:58 +0800317 return MAX_TID_COUNT;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200318 }
319
320 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
321 index = time_diff / TID_QUEUE_CELL_SPACING;
322
323 /* The history is too long: remove data that is older than */
324 /* TID_MAX_TIME_DIFF */
325 if (index >= TID_QUEUE_MAX_SIZE)
326 rs_tl_rm_old_stats(tl, curr_time);
327
328 index = (tl->head + index) % TID_QUEUE_MAX_SIZE;
329 tl->packet_count[index] = tl->packet_count[index] + 1;
330 tl->total = tl->total + 1;
331
332 if ((index + 1) > tl->queue_count)
333 tl->queue_count = index + 1;
Ron Rindjunskyfaa29712008-06-12 09:46:58 +0800334
335 return tid;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200336}
337
338/*
339 get the traffic load value for tid
340*/
Tomas Winklere227cea2008-07-18 13:53:05 +0800341static u32 rs_tl_get_load(struct iwl_lq_sta *lq_data, u8 tid)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200342{
343 u32 curr_time = jiffies_to_msecs(jiffies);
344 u32 time_diff;
345 s32 index;
Tomas Winklere227cea2008-07-18 13:53:05 +0800346 struct iwl_traffic_load *tl = NULL;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200347
348 if (tid >= TID_MAX_LOAD_COUNT)
349 return 0;
350
351 tl = &(lq_data->load[tid]);
352
353 curr_time -= curr_time % TID_ROUND_VALUE;
354
355 if (!(tl->queue_count))
356 return 0;
357
358 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
359 index = time_diff / TID_QUEUE_CELL_SPACING;
360
361 /* The history is too long: remove data that is older than */
362 /* TID_MAX_TIME_DIFF */
363 if (index >= TID_QUEUE_MAX_SIZE)
364 rs_tl_rm_old_stats(tl, curr_time);
365
366 return tl->total;
367}
368
Wey-Yi Guy82ca9342010-04-12 14:02:36 -0700369static int rs_tl_turn_on_agg_for_tid(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +0800370 struct iwl_lq_sta *lq_data, u8 tid,
Johannes Berg4b7679a2008-09-18 18:14:18 +0200371 struct ieee80211_sta *sta)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200372{
Wey-Yi Guy82ca9342010-04-12 14:02:36 -0700373 int ret = -EAGAIN;
Johannes Berg290f5992010-08-23 07:56:58 -0700374 u32 load;
375
376 /*
377 * Don't create TX aggregation sessions when in high
378 * BT traffic, as they would just be disrupted by BT.
379 */
380 if (priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH) {
381 IWL_ERR(priv, "BT traffic (%d), no aggregation allowed\n",
382 priv->bt_traffic_load);
383 return ret;
384 }
385
386 load = rs_tl_get_load(lq_data, tid);
Wey-Yi Guy45d42702010-02-03 12:24:44 -0800387
Andy Lutomirski24110542010-07-25 13:14:29 -0400388 if (load > IWL_AGG_LOAD_THRESHOLD) {
Tomas Winklere1623442009-01-27 14:27:56 -0800389 IWL_DEBUG_HT(priv, "Starting Tx agg: STA: %pM tid: %d\n",
Johannes Berge1749612008-10-27 15:59:26 -0700390 sta->addr, tid);
Sujith Manoharanbd2ce6e2010-12-15 07:47:10 +0530391 ret = ieee80211_start_tx_ba_session(sta, tid, 5000);
Wey-Yi Guy45d42702010-02-03 12:24:44 -0800392 if (ret == -EAGAIN) {
393 /*
394 * driver and mac80211 is out of sync
395 * this might be cause by reloading firmware
396 * stop the tx ba session here
397 */
Andy Lutomirski24110542010-07-25 13:14:29 -0400398 IWL_ERR(priv, "Fail start Tx agg on tid: %d\n",
Wey-Yi Guy45d42702010-02-03 12:24:44 -0800399 tid);
Johannes Berg6a8579d2010-05-27 14:41:07 +0200400 ieee80211_stop_tx_ba_session(sta, tid);
Wey-Yi Guy45d42702010-02-03 12:24:44 -0800401 }
Andy Lutomirski24110542010-07-25 13:14:29 -0400402 } else {
403 IWL_ERR(priv, "Aggregation not enabled for tid %d "
404 "because load = %u\n", tid, load);
405 }
Wey-Yi Guy82ca9342010-04-12 14:02:36 -0700406 return ret;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200407}
408
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700409static void rs_tl_turn_on_agg(struct iwl_priv *priv, u8 tid,
Tomas Winklere227cea2008-07-18 13:53:05 +0800410 struct iwl_lq_sta *lq_data,
Johannes Berg4b7679a2008-09-18 18:14:18 +0200411 struct ieee80211_sta *sta)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200412{
Wey-Yi Guycfecc6b2010-06-18 11:33:15 -0700413 if (tid < TID_MAX_LOAD_COUNT)
414 rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
415 else
416 IWL_ERR(priv, "tid exceeds max load count: %d/%d\n",
417 tid, TID_MAX_LOAD_COUNT);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200418}
419
Guy Cohen39e88502008-04-23 17:14:57 -0700420static inline int get_num_of_ant_from_rate(u32 rate_n_flags)
Guy Cohenfde0db32008-04-21 15:42:01 -0700421{
Tomas Winkler3ac7f142008-07-21 02:40:14 +0300422 return !!(rate_n_flags & RATE_MCS_ANT_A_MSK) +
423 !!(rate_n_flags & RATE_MCS_ANT_B_MSK) +
424 !!(rate_n_flags & RATE_MCS_ANT_C_MSK);
Guy Cohenfde0db32008-04-21 15:42:01 -0700425}
426
Shanyu Zhao04f2dec2010-03-19 13:34:45 -0700427/*
428 * Static function to get the expected throughput from an iwl_scale_tbl_info
429 * that wraps a NULL pointer check
430 */
431static s32 get_expected_tpt(struct iwl_scale_tbl_info *tbl, int rs_index)
432{
433 if (tbl->expected_tpt)
434 return tbl->expected_tpt[rs_index];
435 return 0;
436}
437
Ben Cahill77626352007-11-29 11:09:44 +0800438/**
439 * rs_collect_tx_data - Update the success/failure sliding window
440 *
441 * We keep a sliding window of the last 62 packets transmitted
442 * at this rate. window->data contains the bitmask of successful
443 * packets.
444 */
Shanyu Zhao04f2dec2010-03-19 13:34:45 -0700445static int rs_collect_tx_data(struct iwl_scale_tbl_info *tbl,
446 int scale_index, int attempts, int successes)
Zhu Yib481de92007-09-25 17:54:57 -0700447{
Tomas Winklere227cea2008-07-18 13:53:05 +0800448 struct iwl_rate_scale_data *window = NULL;
Guy Cohen39e88502008-04-23 17:14:57 -0700449 static const u64 mask = (((u64)1) << (IWL_RATE_MAX_WINDOW - 1));
Shanyu Zhao04f2dec2010-03-19 13:34:45 -0700450 s32 fail_count, tpt;
Zhu Yib481de92007-09-25 17:54:57 -0700451
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800452 if (scale_index < 0 || scale_index >= IWL_RATE_COUNT)
453 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -0700454
Daniel C Halperine3949d62009-09-17 10:43:50 -0700455 /* Select window for current tx bit rate */
Shanyu Zhao04f2dec2010-03-19 13:34:45 -0700456 window = &(tbl->win[scale_index]);
457
458 /* Get expected throughput */
459 tpt = get_expected_tpt(tbl, scale_index);
Zhu Yib481de92007-09-25 17:54:57 -0700460
Ben Cahill77626352007-11-29 11:09:44 +0800461 /*
462 * Keep track of only the latest 62 tx frame attempts in this rate's
463 * history window; anything older isn't really relevant any more.
464 * If we have filled up the sliding window, drop the oldest attempt;
465 * if the oldest attempt (highest bit in bitmap) shows "success",
466 * subtract "1" from the success counter (this is the main reason
467 * we keep these bitmaps!).
468 */
Daniel C Halperine3949d62009-09-17 10:43:50 -0700469 while (attempts > 0) {
Guy Cohen39e88502008-04-23 17:14:57 -0700470 if (window->counter >= IWL_RATE_MAX_WINDOW) {
471
472 /* remove earliest */
473 window->counter = IWL_RATE_MAX_WINDOW - 1;
474
Ron Rindjunsky99556432008-01-28 14:07:25 +0200475 if (window->data & mask) {
476 window->data &= ~mask;
Guy Cohen39e88502008-04-23 17:14:57 -0700477 window->success_counter--;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200478 }
Zhu Yib481de92007-09-25 17:54:57 -0700479 }
Zhu Yib481de92007-09-25 17:54:57 -0700480
Ron Rindjunsky99556432008-01-28 14:07:25 +0200481 /* Increment frames-attempted counter */
482 window->counter++;
Ben Cahill77626352007-11-29 11:09:44 +0800483
Daniel C Halperine3949d62009-09-17 10:43:50 -0700484 /* Shift bitmap by one frame to throw away oldest history */
Guy Cohen90d77952008-09-09 10:54:53 +0800485 window->data <<= 1;
Daniel C Halperine3949d62009-09-17 10:43:50 -0700486
487 /* Mark the most recent #successes attempts as successful */
Ron Rindjunsky99556432008-01-28 14:07:25 +0200488 if (successes > 0) {
Guy Cohen39e88502008-04-23 17:14:57 -0700489 window->success_counter++;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200490 window->data |= 0x1;
491 successes--;
492 }
493
Daniel C Halperine3949d62009-09-17 10:43:50 -0700494 attempts--;
Zhu Yib481de92007-09-25 17:54:57 -0700495 }
496
Ben Cahill77626352007-11-29 11:09:44 +0800497 /* Calculate current success ratio, avoid divide-by-0! */
Zhu Yib481de92007-09-25 17:54:57 -0700498 if (window->counter > 0)
499 window->success_ratio = 128 * (100 * window->success_counter)
500 / window->counter;
501 else
502 window->success_ratio = IWL_INVALID_VALUE;
503
504 fail_count = window->counter - window->success_counter;
505
Ben Cahill77626352007-11-29 11:09:44 +0800506 /* Calculate average throughput, if we have enough history. */
Zhu Yib481de92007-09-25 17:54:57 -0700507 if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
508 (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
509 window->average_tpt = (window->success_ratio * tpt + 64) / 128;
510 else
511 window->average_tpt = IWL_INVALID_VALUE;
512
Ben Cahill77626352007-11-29 11:09:44 +0800513 /* Tag this window as having been updated */
Zhu Yib481de92007-09-25 17:54:57 -0700514 window->stamp = jiffies;
515
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800516 return 0;
Zhu Yib481de92007-09-25 17:54:57 -0700517}
518
Ben Cahill77626352007-11-29 11:09:44 +0800519/*
520 * Fill uCode API rate_n_flags field, based on "search" or "active" table.
521 */
Guy Cohen39e88502008-04-23 17:14:57 -0700522/* FIXME:RS:remove this function and put the flags statically in the table */
Tomas Winkler978785a2008-12-19 10:37:31 +0800523static u32 rate_n_flags_from_tbl(struct iwl_priv *priv,
524 struct iwl_scale_tbl_info *tbl,
525 int index, u8 use_green)
Zhu Yib481de92007-09-25 17:54:57 -0700526{
Guy Cohen39e88502008-04-23 17:14:57 -0700527 u32 rate_n_flags = 0;
528
Zhu Yib481de92007-09-25 17:54:57 -0700529 if (is_legacy(tbl->lq_type)) {
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800530 rate_n_flags = iwl_rates[index].plcp;
Zhu Yib481de92007-09-25 17:54:57 -0700531 if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
Guy Cohen39e88502008-04-23 17:14:57 -0700532 rate_n_flags |= RATE_MCS_CCK_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700533
Guy Cohenfde0db32008-04-21 15:42:01 -0700534 } else if (is_Ht(tbl->lq_type)) {
535 if (index > IWL_LAST_OFDM_RATE) {
Tomas Winkler978785a2008-12-19 10:37:31 +0800536 IWL_ERR(priv, "Invalid HT rate index %d\n", index);
Zhu Yib481de92007-09-25 17:54:57 -0700537 index = IWL_LAST_OFDM_RATE;
Guy Cohenfde0db32008-04-21 15:42:01 -0700538 }
Guy Cohen39e88502008-04-23 17:14:57 -0700539 rate_n_flags = RATE_MCS_HT_MSK;
Guy Cohenfde0db32008-04-21 15:42:01 -0700540
541 if (is_siso(tbl->lq_type))
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800542 rate_n_flags |= iwl_rates[index].plcp_siso;
Guy Cohenfde0db32008-04-21 15:42:01 -0700543 else if (is_mimo2(tbl->lq_type))
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800544 rate_n_flags |= iwl_rates[index].plcp_mimo2;
Guy Cohenfde0db32008-04-21 15:42:01 -0700545 else
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800546 rate_n_flags |= iwl_rates[index].plcp_mimo3;
Zhu Yib481de92007-09-25 17:54:57 -0700547 } else {
Tomas Winkler978785a2008-12-19 10:37:31 +0800548 IWL_ERR(priv, "Invalid tbl->lq_type %d\n", tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -0700549 }
550
Guy Cohen39e88502008-04-23 17:14:57 -0700551 rate_n_flags |= ((tbl->ant_type << RATE_MCS_ANT_POS) &
Guy Cohenfde0db32008-04-21 15:42:01 -0700552 RATE_MCS_ANT_ABC_MSK);
Zhu Yib481de92007-09-25 17:54:57 -0700553
Guy Cohen39e88502008-04-23 17:14:57 -0700554 if (is_Ht(tbl->lq_type)) {
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700555 if (tbl->is_ht40) {
Guy Cohen39e88502008-04-23 17:14:57 -0700556 if (tbl->is_dup)
557 rate_n_flags |= RATE_MCS_DUP_MSK;
558 else
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700559 rate_n_flags |= RATE_MCS_HT40_MSK;
Guy Cohen39e88502008-04-23 17:14:57 -0700560 }
561 if (tbl->is_SGI)
562 rate_n_flags |= RATE_MCS_SGI_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700563
Guy Cohen39e88502008-04-23 17:14:57 -0700564 if (use_green) {
565 rate_n_flags |= RATE_MCS_GF_MSK;
566 if (is_siso(tbl->lq_type) && tbl->is_SGI) {
567 rate_n_flags &= ~RATE_MCS_SGI_MSK;
Tomas Winkler978785a2008-12-19 10:37:31 +0800568 IWL_ERR(priv, "GF was set with SGI:SISO\n");
Guy Cohen39e88502008-04-23 17:14:57 -0700569 }
570 }
Zhu Yib481de92007-09-25 17:54:57 -0700571 }
Guy Cohen39e88502008-04-23 17:14:57 -0700572 return rate_n_flags;
Zhu Yib481de92007-09-25 17:54:57 -0700573}
574
Ben Cahill77626352007-11-29 11:09:44 +0800575/*
576 * Interpret uCode API's rate_n_flags format,
577 * fill "search" or "active" tx mode table.
578 */
Guy Cohen39e88502008-04-23 17:14:57 -0700579static int rs_get_tbl_info_from_mcs(const u32 rate_n_flags,
Johannes Berg8318d782008-01-24 19:38:38 +0100580 enum ieee80211_band band,
Tomas Winklere227cea2008-07-18 13:53:05 +0800581 struct iwl_scale_tbl_info *tbl,
Zhu Yib481de92007-09-25 17:54:57 -0700582 int *rate_idx)
583{
Guy Cohen39e88502008-04-23 17:14:57 -0700584 u32 ant_msk = (rate_n_flags & RATE_MCS_ANT_ABC_MSK);
585 u8 num_of_ant = get_num_of_ant_from_rate(rate_n_flags);
586 u8 mcs;
Zhu Yib481de92007-09-25 17:54:57 -0700587
Wey-Yi Guy80e91582010-08-03 08:23:32 -0700588 memset(tbl, 0, sizeof(struct iwl_scale_tbl_info));
Tomas Winklere7d326ac2008-06-12 09:47:11 +0800589 *rate_idx = iwl_hwrate_to_plcp_idx(rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -0700590
Guy Cohenfde0db32008-04-21 15:42:01 -0700591 if (*rate_idx == IWL_RATE_INVALID) {
Zhu Yib481de92007-09-25 17:54:57 -0700592 *rate_idx = -1;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800593 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -0700594 }
Ben Cahill77626352007-11-29 11:09:44 +0800595 tbl->is_SGI = 0; /* default legacy setup */
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700596 tbl->is_ht40 = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700597 tbl->is_dup = 0;
Guy Cohenfde0db32008-04-21 15:42:01 -0700598 tbl->ant_type = (ant_msk >> RATE_MCS_ANT_POS);
599 tbl->lq_type = LQ_NONE;
Mohamed Abbasd6e93392009-05-08 13:44:39 -0700600 tbl->max_search = IWL_MAX_SEARCH;
Zhu Yib481de92007-09-25 17:54:57 -0700601
Ben Cahill77626352007-11-29 11:09:44 +0800602 /* legacy rate format */
Guy Cohen39e88502008-04-23 17:14:57 -0700603 if (!(rate_n_flags & RATE_MCS_HT_MSK)) {
Guy Cohenfde0db32008-04-21 15:42:01 -0700604 if (num_of_ant == 1) {
Johannes Berg8318d782008-01-24 19:38:38 +0100605 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700606 tbl->lq_type = LQ_A;
607 else
608 tbl->lq_type = LQ_G;
Zhu Yib481de92007-09-25 17:54:57 -0700609 }
Guy Cohenfde0db32008-04-21 15:42:01 -0700610 /* HT rate format */
Zhu Yib481de92007-09-25 17:54:57 -0700611 } else {
Guy Cohen39e88502008-04-23 17:14:57 -0700612 if (rate_n_flags & RATE_MCS_SGI_MSK)
Zhu Yib481de92007-09-25 17:54:57 -0700613 tbl->is_SGI = 1;
614
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700615 if ((rate_n_flags & RATE_MCS_HT40_MSK) ||
Guy Cohen39e88502008-04-23 17:14:57 -0700616 (rate_n_flags & RATE_MCS_DUP_MSK))
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700617 tbl->is_ht40 = 1;
Zhu Yib481de92007-09-25 17:54:57 -0700618
Guy Cohen39e88502008-04-23 17:14:57 -0700619 if (rate_n_flags & RATE_MCS_DUP_MSK)
Zhu Yib481de92007-09-25 17:54:57 -0700620 tbl->is_dup = 1;
Guy Cohenfde0db32008-04-21 15:42:01 -0700621
Guy Cohen39e88502008-04-23 17:14:57 -0700622 mcs = rs_extract_rate(rate_n_flags);
Guy Cohenfde0db32008-04-21 15:42:01 -0700623
Guy Cohen39e88502008-04-23 17:14:57 -0700624 /* SISO */
625 if (mcs <= IWL_RATE_SISO_60M_PLCP) {
Guy Cohenfde0db32008-04-21 15:42:01 -0700626 if (num_of_ant == 1)
627 tbl->lq_type = LQ_SISO; /*else NONE*/
628 /* MIMO2 */
Guy Cohen39e88502008-04-23 17:14:57 -0700629 } else if (mcs <= IWL_RATE_MIMO2_60M_PLCP) {
Guy Cohenfde0db32008-04-21 15:42:01 -0700630 if (num_of_ant == 2)
631 tbl->lq_type = LQ_MIMO2;
632 /* MIMO3 */
633 } else {
Mohamed Abbasd6e93392009-05-08 13:44:39 -0700634 if (num_of_ant == 3) {
635 tbl->max_search = IWL_MAX_11N_MIMO3_SEARCH;
Guy Cohenfde0db32008-04-21 15:42:01 -0700636 tbl->lq_type = LQ_MIMO3;
Mohamed Abbasd6e93392009-05-08 13:44:39 -0700637 }
Guy Cohenfde0db32008-04-21 15:42:01 -0700638 }
Zhu Yib481de92007-09-25 17:54:57 -0700639 }
640 return 0;
641}
Guy Cohenaade00c2008-04-23 17:14:59 -0700642
643/* switch to another antenna/antennas and return 1 */
644/* if no other valid antenna found, return 0 */
645static int rs_toggle_antenna(u32 valid_ant, u32 *rate_n_flags,
Tomas Winklere227cea2008-07-18 13:53:05 +0800646 struct iwl_scale_tbl_info *tbl)
Zhu Yib481de92007-09-25 17:54:57 -0700647{
Guy Cohenaade00c2008-04-23 17:14:59 -0700648 u8 new_ant_type;
649
650 if (!tbl->ant_type || tbl->ant_type > ANT_ABC)
651 return 0;
652
653 if (!rs_is_valid_ant(valid_ant, tbl->ant_type))
654 return 0;
655
656 new_ant_type = ant_toggle_lookup[tbl->ant_type];
657
658 while ((new_ant_type != tbl->ant_type) &&
659 !rs_is_valid_ant(valid_ant, new_ant_type))
660 new_ant_type = ant_toggle_lookup[new_ant_type];
661
662 if (new_ant_type == tbl->ant_type)
663 return 0;
664
665 tbl->ant_type = new_ant_type;
666 *rate_n_flags &= ~RATE_MCS_ANT_ABC_MSK;
667 *rate_n_flags |= new_ant_type << RATE_MCS_ANT_POS;
668 return 1;
Zhu Yib481de92007-09-25 17:54:57 -0700669}
670
Daniel C Halperinb2617932009-08-13 13:30:59 -0700671/**
672 * Green-field mode is valid if the station supports it and
673 * there are no non-GF stations present in the BSS.
674 */
Johannes Berg7e6a5882010-08-23 10:46:46 +0200675static bool rs_use_green(struct ieee80211_sta *sta)
Zhu Yib481de92007-09-25 17:54:57 -0700676{
Johannes Berg7e6a5882010-08-23 10:46:46 +0200677 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
678 struct iwl_rxon_context *ctx = sta_priv->common.ctx;
679
Daniel C Halperinb2617932009-08-13 13:30:59 -0700680 return (sta->ht_cap.cap & IEEE80211_HT_CAP_GRN_FLD) &&
Johannes Berg7e6a5882010-08-23 10:46:46 +0200681 !(ctx->ht.non_gf_sta_present);
Guy Cohenf935a6d2008-04-23 17:15:02 -0700682}
Zhu Yib481de92007-09-25 17:54:57 -0700683
684/**
685 * rs_get_supported_rates - get the available rates
686 *
687 * if management frame or broadcast frame only return
688 * basic available rates.
689 *
690 */
Tomas Winklere227cea2008-07-18 13:53:05 +0800691static u16 rs_get_supported_rates(struct iwl_lq_sta *lq_sta,
692 struct ieee80211_hdr *hdr,
693 enum iwl_table_type rate_type)
Zhu Yib481de92007-09-25 17:54:57 -0700694{
Guy Coheneecd6e52008-04-23 17:14:58 -0700695 if (is_legacy(rate_type)) {
696 return lq_sta->active_legacy_rate;
697 } else {
698 if (is_siso(rate_type))
699 return lq_sta->active_siso_rate;
700 else if (is_mimo2(rate_type))
701 return lq_sta->active_mimo2_rate;
702 else
703 return lq_sta->active_mimo3_rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -0800704 }
Zhu Yib481de92007-09-25 17:54:57 -0700705}
706
Ester Kummerbf403db2008-05-05 10:22:40 +0800707static u16 rs_get_adjacent_rate(struct iwl_priv *priv, u8 index, u16 rate_mask,
708 int rate_type)
Zhu Yib481de92007-09-25 17:54:57 -0700709{
710 u8 high = IWL_RATE_INVALID;
711 u8 low = IWL_RATE_INVALID;
712
Ian Schram01ebd062007-10-25 17:15:22 +0800713 /* 802.11A or ht walks to the next literal adjacent rate in
Zhu Yib481de92007-09-25 17:54:57 -0700714 * the rate table */
715 if (is_a_band(rate_type) || !is_legacy(rate_type)) {
716 int i;
717 u32 mask;
718
719 /* Find the previous rate that is in the rate mask */
720 i = index - 1;
721 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
722 if (rate_mask & mask) {
723 low = i;
724 break;
725 }
726 }
727
728 /* Find the next rate that is in the rate mask */
729 i = index + 1;
730 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
731 if (rate_mask & mask) {
732 high = i;
733 break;
734 }
735 }
736
737 return (high << 8) | low;
738 }
739
740 low = index;
741 while (low != IWL_RATE_INVALID) {
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800742 low = iwl_rates[low].prev_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700743 if (low == IWL_RATE_INVALID)
744 break;
745 if (rate_mask & (1 << low))
746 break;
Tomas Winklere1623442009-01-27 14:27:56 -0800747 IWL_DEBUG_RATE(priv, "Skipping masked lower rate: %d\n", low);
Zhu Yib481de92007-09-25 17:54:57 -0700748 }
749
750 high = index;
751 while (high != IWL_RATE_INVALID) {
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800752 high = iwl_rates[high].next_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700753 if (high == IWL_RATE_INVALID)
754 break;
755 if (rate_mask & (1 << high))
756 break;
Tomas Winklere1623442009-01-27 14:27:56 -0800757 IWL_DEBUG_RATE(priv, "Skipping masked higher rate: %d\n", high);
Zhu Yib481de92007-09-25 17:54:57 -0700758 }
759
760 return (high << 8) | low;
761}
762
Tomas Winklere227cea2008-07-18 13:53:05 +0800763static u32 rs_get_lower_rate(struct iwl_lq_sta *lq_sta,
764 struct iwl_scale_tbl_info *tbl,
765 u8 scale_index, u8 ht_possible)
Zhu Yib481de92007-09-25 17:54:57 -0700766{
Zhu Yib481de92007-09-25 17:54:57 -0700767 s32 low;
768 u16 rate_mask;
769 u16 high_low;
770 u8 switch_to_legacy = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -0800771 u8 is_green = lq_sta->is_green;
Wey-Yi Guy2ff65782009-09-11 10:38:18 -0700772 struct iwl_priv *priv = lq_sta->drv;
Zhu Yib481de92007-09-25 17:54:57 -0700773
774 /* check if we need to switch from HT to legacy rates.
775 * assumption is that mandatory rates (1Mbps or 6Mbps)
776 * are always supported (spec demand) */
777 if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_index)) {
778 switch_to_legacy = 1;
779 scale_index = rs_ht_to_legacy[scale_index];
Johannes Berg8318d782008-01-24 19:38:38 +0100780 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700781 tbl->lq_type = LQ_A;
782 else
783 tbl->lq_type = LQ_G;
784
Guy Cohen69fdb302008-04-23 17:15:01 -0700785 if (num_of_ant(tbl->ant_type) > 1)
Wey-Yi Guy2ff65782009-09-11 10:38:18 -0700786 tbl->ant_type =
787 first_antenna(priv->hw_params.valid_tx_ant);
Zhu Yib481de92007-09-25 17:54:57 -0700788
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700789 tbl->is_ht40 = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700790 tbl->is_SGI = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -0700791 tbl->max_search = IWL_MAX_SEARCH;
Zhu Yib481de92007-09-25 17:54:57 -0700792 }
793
Guy Coheneecd6e52008-04-23 17:14:58 -0700794 rate_mask = rs_get_supported_rates(lq_sta, NULL, tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -0700795
Ben Cahill77626352007-11-29 11:09:44 +0800796 /* Mask with station rate restriction */
Zhu Yib481de92007-09-25 17:54:57 -0700797 if (is_legacy(tbl->lq_type)) {
Ben Cahill77626352007-11-29 11:09:44 +0800798 /* supp_rates has no CCK bits in A mode */
Johannes Berg8318d782008-01-24 19:38:38 +0100799 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700800 rate_mask = (u16)(rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -0800801 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
Zhu Yib481de92007-09-25 17:54:57 -0700802 else
Tomas Winklerc33104f2008-01-14 17:46:21 -0800803 rate_mask = (u16)(rate_mask & lq_sta->supp_rates);
Zhu Yib481de92007-09-25 17:54:57 -0700804 }
805
Ben Cahill77626352007-11-29 11:09:44 +0800806 /* If we switched from HT to legacy, check current rate */
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800807 if (switch_to_legacy && (rate_mask & (1 << scale_index))) {
Guy Cohen39e88502008-04-23 17:14:57 -0700808 low = scale_index;
809 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700810 }
811
Ester Kummerbf403db2008-05-05 10:22:40 +0800812 high_low = rs_get_adjacent_rate(lq_sta->drv, scale_index, rate_mask,
813 tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -0700814 low = high_low & 0xff;
815
Guy Cohen39e88502008-04-23 17:14:57 -0700816 if (low == IWL_RATE_INVALID)
817 low = scale_index;
818
819out:
Tomas Winkler978785a2008-12-19 10:37:31 +0800820 return rate_n_flags_from_tbl(lq_sta->drv, tbl, low, is_green);
Zhu Yib481de92007-09-25 17:54:57 -0700821}
822
Ben Cahill77626352007-11-29 11:09:44 +0800823/*
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700824 * Simple function to compare two rate scale table types
825 */
826static bool table_type_matches(struct iwl_scale_tbl_info *a,
827 struct iwl_scale_tbl_info *b)
828{
829 return (a->lq_type == b->lq_type) && (a->ant_type == b->ant_type) &&
830 (a->is_SGI == b->is_SGI);
831}
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700832
Johannes Berg7e6a5882010-08-23 10:46:46 +0200833static void rs_bt_update_lq(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
834 struct iwl_lq_sta *lq_sta)
Wey-Yi Guybee008b2010-08-23 07:57:04 -0700835{
836 struct iwl_scale_tbl_info *tbl;
Wey-Yi Guy66e863a52010-11-08 14:54:37 -0800837 bool full_concurrent = priv->bt_full_concurrent;
Wey-Yi Guybee008b2010-08-23 07:57:04 -0700838 unsigned long flags;
839
Wey-Yi Guy66e863a52010-11-08 14:54:37 -0800840 if (priv->bt_ant_couple_ok) {
841 /*
842 * Is there a need to switch between
843 * full concurrency and 3-wire?
844 */
845 spin_lock_irqsave(&priv->lock, flags);
846 if (priv->bt_ci_compliance && priv->bt_ant_couple_ok)
847 full_concurrent = true;
848 else
849 full_concurrent = false;
850 spin_unlock_irqrestore(&priv->lock, flags);
851 }
852 if ((priv->bt_traffic_load != priv->last_bt_traffic_load) ||
853 (priv->bt_full_concurrent != full_concurrent)) {
Wey-Yi Guybee008b2010-08-23 07:57:04 -0700854 priv->bt_full_concurrent = full_concurrent;
855
856 /* Update uCode's rate table. */
857 tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
858 rs_fill_link_cmd(priv, lq_sta, tbl->current_rate);
Johannes Berg7e6a5882010-08-23 10:46:46 +0200859 iwl_send_lq_cmd(priv, ctx, &lq_sta->lq, CMD_ASYNC, false);
Wey-Yi Guybee008b2010-08-23 07:57:04 -0700860
861 queue_work(priv->workqueue, &priv->bt_full_concurrency);
862 }
863}
864
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700865/*
Ben Cahill77626352007-11-29 11:09:44 +0800866 * mac80211 sends us Tx status
867 */
Johannes Berg4b7679a2008-09-18 18:14:18 +0200868static void rs_tx_status(void *priv_r, struct ieee80211_supported_band *sband,
869 struct ieee80211_sta *sta, void *priv_sta,
Johannes Berge039fa42008-05-15 12:55:29 +0200870 struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -0700871{
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700872 int legacy_success;
873 int retries;
874 int rs_index, mac_index, i;
Tomas Winkler417f1142008-11-12 13:14:06 -0800875 struct iwl_lq_sta *lq_sta = priv_sta;
Tomas Winkler66c73db2008-04-15 16:01:40 -0700876 struct iwl_link_quality_cmd *table;
Zhu Yib481de92007-09-25 17:54:57 -0700877 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berg4b7679a2008-09-18 18:14:18 +0200878 struct iwl_priv *priv = (struct iwl_priv *)priv_r;
Johannes Berge039fa42008-05-15 12:55:29 +0200879 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Daniel C Halperin50273092009-08-28 09:44:45 -0700880 enum mac80211_rate_control_flags mac_flags;
Guy Cohen39e88502008-04-23 17:14:57 -0700881 u32 tx_rate;
Tomas Winklere227cea2008-07-18 13:53:05 +0800882 struct iwl_scale_tbl_info tbl_type;
Shanyu Zhao04f2dec2010-03-19 13:34:45 -0700883 struct iwl_scale_tbl_info *curr_tbl, *other_tbl, *tmp_tbl;
Johannes Berg7e6a5882010-08-23 10:46:46 +0200884 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
885 struct iwl_rxon_context *ctx = sta_priv->common.ctx;
Zhu Yib481de92007-09-25 17:54:57 -0700886
Tomas Winklere1623442009-01-27 14:27:56 -0800887 IWL_DEBUG_RATE_LIMIT(priv, "get frame ack response, update rate scale window\n");
Zhu Yib481de92007-09-25 17:54:57 -0700888
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800889 /* Treat uninitialized rate scaling data same as non-existing. */
890 if (!lq_sta) {
891 IWL_DEBUG_RATE(priv, "Station rate scaling not created yet.\n");
892 return;
893 } else if (!lq_sta->drv) {
894 IWL_DEBUG_RATE(priv, "Rate scaling not initialized yet.\n");
895 return;
896 }
897
Tomas Winkler417f1142008-11-12 13:14:06 -0800898 if (!ieee80211_is_data(hdr->frame_control) ||
Gábor Stefanik514d65c2009-04-23 19:36:08 +0200899 info->flags & IEEE80211_TX_CTL_NO_ACK)
Zhu Yib481de92007-09-25 17:54:57 -0700900 return;
901
Daniel C Halperine3949d62009-09-17 10:43:50 -0700902 /* This packet was aggregated but doesn't carry status info */
Johannes Berge039fa42008-05-15 12:55:29 +0200903 if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
904 !(info->flags & IEEE80211_TX_STAT_AMPDU))
Ron Rindjunsky99556432008-01-28 14:07:25 +0200905 return;
906
Ben Cahill77626352007-11-29 11:09:44 +0800907 /*
908 * Ignore this Tx frame response if its initial rate doesn't match
909 * that of latest Link Quality command. There may be stragglers
910 * from a previous Link Quality command, but we're no longer interested
911 * in those; they're either from the "active" mode while we're trying
912 * to check "search" mode, or a prior "search" mode after we've moved
913 * to a new "search" mode (which might become the new "active" mode).
914 */
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700915 table = &lq_sta->lq;
Guy Cohen39e88502008-04-23 17:14:57 -0700916 tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
917 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800918 if (priv->band == IEEE80211_BAND_5GHZ)
919 rs_index -= IWL_FIRST_OFDM_RATE;
Daniel C Halperin50273092009-08-28 09:44:45 -0700920 mac_flags = info->status.rates[0].flags;
921 mac_index = info->status.rates[0].idx;
Daniel C Halperin31513be2009-08-28 09:44:47 -0700922 /* For HT packets, map MCS to PLCP */
923 if (mac_flags & IEEE80211_TX_RC_MCS) {
924 mac_index &= RATE_MCS_CODE_MSK; /* Remove # of streams */
925 if (mac_index >= (IWL_RATE_9M_INDEX - IWL_FIRST_OFDM_RATE))
926 mac_index++;
Daniel C Halperin392a0ba2009-09-11 10:38:08 -0700927 /*
928 * mac80211 HT index is always zero-indexed; we need to move
929 * HT OFDM rates after CCK rates in 2.4 GHz band
930 */
931 if (priv->band == IEEE80211_BAND_2GHZ)
932 mac_index += IWL_FIRST_OFDM_RATE;
Daniel C Halperin31513be2009-08-28 09:44:47 -0700933 }
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700934 /* Here we actually compare this rate to the latest LQ command */
Daniel C Halperin50273092009-08-28 09:44:45 -0700935 if ((mac_index < 0) ||
936 (tbl_type.is_SGI != !!(mac_flags & IEEE80211_TX_RC_SHORT_GI)) ||
937 (tbl_type.is_ht40 != !!(mac_flags & IEEE80211_TX_RC_40_MHZ_WIDTH)) ||
938 (tbl_type.is_dup != !!(mac_flags & IEEE80211_TX_RC_DUP_DATA)) ||
Johannes Berge6a98542008-10-21 12:40:02 +0200939 (tbl_type.ant_type != info->antenna_sel_tx) ||
Daniel C Halperin50273092009-08-28 09:44:45 -0700940 (!!(tx_rate & RATE_MCS_HT_MSK) != !!(mac_flags & IEEE80211_TX_RC_MCS)) ||
941 (!!(tx_rate & RATE_MCS_GF_MSK) != !!(mac_flags & IEEE80211_TX_RC_GREEN_FIELD)) ||
Daniel C Halperin31513be2009-08-28 09:44:47 -0700942 (rs_index != mac_index)) {
943 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 -0700944 /*
945 * Since rates mis-match, the last LQ command may have failed.
946 * After IWL_MISSED_RATE_MAX mis-matches, resync the uCode with
947 * ... driver.
Mohamed Abbasd5e49032008-12-12 08:22:15 -0800948 */
Abbas, Mohamed7d049e52009-01-20 21:33:53 -0800949 lq_sta->missed_rate_counter++;
950 if (lq_sta->missed_rate_counter > IWL_MISSED_RATE_MAX) {
951 lq_sta->missed_rate_counter = 0;
Johannes Berg7e6a5882010-08-23 10:46:46 +0200952 iwl_send_lq_cmd(priv, ctx, &lq_sta->lq, CMD_ASYNC, false);
Abbas, Mohamed7d049e52009-01-20 21:33:53 -0800953 }
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700954 /* Regardless, ignore this status info for outdated rate */
955 return;
956 } else
957 /* Rate did match, so reset the missed_rate_counter */
958 lq_sta->missed_rate_counter = 0;
959
960 /* Figure out if rate scale algorithm is in active or search table */
961 if (table_type_matches(&tbl_type,
962 &(lq_sta->lq_info[lq_sta->active_tbl]))) {
963 curr_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
964 other_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
965 } else if (table_type_matches(&tbl_type,
966 &lq_sta->lq_info[1 - lq_sta->active_tbl])) {
967 curr_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
968 other_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
969 } else {
970 IWL_DEBUG_RATE(priv, "Neither active nor search matches tx rate\n");
Wey-Yi Guy80e91582010-08-03 08:23:32 -0700971 tmp_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
972 IWL_DEBUG_RATE(priv, "active- lq:%x, ant:%x, SGI:%d\n",
973 tmp_tbl->lq_type, tmp_tbl->ant_type, tmp_tbl->is_SGI);
974 tmp_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
975 IWL_DEBUG_RATE(priv, "search- lq:%x, ant:%x, SGI:%d\n",
976 tmp_tbl->lq_type, tmp_tbl->ant_type, tmp_tbl->is_SGI);
977 IWL_DEBUG_RATE(priv, "actual- lq:%x, ant:%x, SGI:%d\n",
978 tbl_type.lq_type, tbl_type.ant_type, tbl_type.is_SGI);
979 /*
980 * no matching table found, let's by-pass the data collection
981 * and continue to perform rate scale to find the rate table
982 */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -0700983 rs_stay_in_table(lq_sta, true);
Wey-Yi Guy80e91582010-08-03 08:23:32 -0700984 goto done;
Zhu Yib481de92007-09-25 17:54:57 -0700985 }
Zhu Yib481de92007-09-25 17:54:57 -0700986
Ben Cahill77626352007-11-29 11:09:44 +0800987 /*
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700988 * Updating the frame history depends on whether packets were
989 * aggregated.
990 *
991 * For aggregation, all packets were transmitted at the same rate, the
992 * first index into rate scale table.
Ben Cahill77626352007-11-29 11:09:44 +0800993 */
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700994 if (info->flags & IEEE80211_TX_STAT_AMPDU) {
995 tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
996 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type,
997 &rs_index);
Shanyu Zhao04f2dec2010-03-19 13:34:45 -0700998 rs_collect_tx_data(curr_tbl, rs_index,
Daniel Halperine3a3cd82010-04-18 09:27:58 -0700999 info->status.ampdu_len,
1000 info->status.ampdu_ack_len);
Zhu Yib481de92007-09-25 17:54:57 -07001001
Daniel C Halperin95407aa2009-09-17 10:43:48 -07001002 /* Update success/fail counts if not searching for new mode */
1003 if (lq_sta->stay_in_tbl) {
Daniel Halperine3a3cd82010-04-18 09:27:58 -07001004 lq_sta->total_success += info->status.ampdu_ack_len;
1005 lq_sta->total_failed += (info->status.ampdu_len -
1006 info->status.ampdu_ack_len);
Daniel C Halperin95407aa2009-09-17 10:43:48 -07001007 }
1008 } else {
1009 /*
1010 * For legacy, update frame history with for each Tx retry.
1011 */
1012 retries = info->status.rates[0].count - 1;
1013 /* HW doesn't send more than 15 retries */
1014 retries = min(retries, 15);
1015
1016 /* The last transmission may have been successful */
1017 legacy_success = !!(info->flags & IEEE80211_TX_STAT_ACK);
1018 /* Collect data for each rate used during failed TX attempts */
1019 for (i = 0; i <= retries; ++i) {
1020 tx_rate = le32_to_cpu(table->rs_table[i].rate_n_flags);
1021 rs_get_tbl_info_from_mcs(tx_rate, priv->band,
1022 &tbl_type, &rs_index);
1023 /*
1024 * Only collect stats if retried rate is in the same RS
1025 * table as active/search.
1026 */
1027 if (table_type_matches(&tbl_type, curr_tbl))
Shanyu Zhao04f2dec2010-03-19 13:34:45 -07001028 tmp_tbl = curr_tbl;
Daniel C Halperin95407aa2009-09-17 10:43:48 -07001029 else if (table_type_matches(&tbl_type, other_tbl))
Shanyu Zhao04f2dec2010-03-19 13:34:45 -07001030 tmp_tbl = other_tbl;
Ron Rindjunsky99556432008-01-28 14:07:25 +02001031 else
Daniel C Halperin95407aa2009-09-17 10:43:48 -07001032 continue;
Shanyu Zhao04f2dec2010-03-19 13:34:45 -07001033 rs_collect_tx_data(tmp_tbl, rs_index, 1,
1034 i < retries ? 0 : legacy_success);
Daniel C Halperin95407aa2009-09-17 10:43:48 -07001035 }
1036
1037 /* Update success/fail counts if not searching for new mode */
1038 if (lq_sta->stay_in_tbl) {
1039 lq_sta->total_success += legacy_success;
1040 lq_sta->total_failed += retries + (1 - legacy_success);
Ron Rindjunsky99556432008-01-28 14:07:25 +02001041 }
Zhu Yib481de92007-09-25 17:54:57 -07001042 }
Daniel C Halperin95407aa2009-09-17 10:43:48 -07001043 /* The last TX rate is cached in lq_sta; it's set in if/else above */
1044 lq_sta->last_rate_n_flags = tx_rate;
Wey-Yi Guy80e91582010-08-03 08:23:32 -07001045done:
Ben Cahill77626352007-11-29 11:09:44 +08001046 /* See if there's a better rate or modulation mode to try. */
Abbas, Mohamedc338ba32009-01-21 10:58:02 -08001047 if (sta && sta->supp_rates[sband->band])
Gábor Stefanik514d65c2009-04-23 19:36:08 +02001048 rs_rate_scale_perform(priv, skb, sta, lq_sta);
Wey-Yi Guybee008b2010-08-23 07:57:04 -07001049
Wey-Yi Guy66e863a52010-11-08 14:54:37 -08001050 if (priv->cfg->bt_params && priv->cfg->bt_params->advanced_bt_coexist)
Johannes Berg7e6a5882010-08-23 10:46:46 +02001051 rs_bt_update_lq(priv, ctx, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07001052}
1053
Ben Cahill77626352007-11-29 11:09:44 +08001054/*
1055 * Begin a period of staying with a selected modulation mode.
1056 * Set "stay_in_tbl" flag to prevent any mode switches.
1057 * Set frame tx success limits according to legacy vs. high-throughput,
1058 * and reset overall (spanning all rates) tx success history statistics.
1059 * These control how long we stay using same modulation mode before
1060 * searching for a new mode.
1061 */
Ester Kummerbf403db2008-05-05 10:22:40 +08001062static void rs_set_stay_in_table(struct iwl_priv *priv, u8 is_legacy,
Tomas Winklere227cea2008-07-18 13:53:05 +08001063 struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07001064{
Tomas Winklere1623442009-01-27 14:27:56 -08001065 IWL_DEBUG_RATE(priv, "we are staying in the same table\n");
Tomas Winklerc33104f2008-01-14 17:46:21 -08001066 lq_sta->stay_in_tbl = 1; /* only place this gets set */
Zhu Yib481de92007-09-25 17:54:57 -07001067 if (is_legacy) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001068 lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT;
1069 lq_sta->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
1070 lq_sta->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
Zhu Yib481de92007-09-25 17:54:57 -07001071 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001072 lq_sta->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
1073 lq_sta->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
1074 lq_sta->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
Zhu Yib481de92007-09-25 17:54:57 -07001075 }
Tomas Winklerc33104f2008-01-14 17:46:21 -08001076 lq_sta->table_count = 0;
1077 lq_sta->total_failed = 0;
1078 lq_sta->total_success = 0;
Mohamed Abbas447fee72009-04-20 14:37:02 -07001079 lq_sta->flush_timer = jiffies;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001080 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001081}
1082
Ben Cahill77626352007-11-29 11:09:44 +08001083/*
1084 * Find correct throughput table for given mode of modulation
1085 */
Tomas Winklere227cea2008-07-18 13:53:05 +08001086static void rs_set_expected_tpt_table(struct iwl_lq_sta *lq_sta,
1087 struct iwl_scale_tbl_info *tbl)
Zhu Yib481de92007-09-25 17:54:57 -07001088{
Daniel C Halperine3949d62009-09-17 10:43:50 -07001089 /* Used to choose among HT tables */
1090 s32 (*ht_tbl_pointer)[IWL_RATE_COUNT];
1091
1092 /* Check for invalid LQ type */
1093 if (WARN_ON_ONCE(!is_legacy(tbl->lq_type) && !is_Ht(tbl->lq_type))) {
1094 tbl->expected_tpt = expected_tpt_legacy;
1095 return;
1096 }
1097
1098 /* Legacy rates have only one table */
Zhu Yib481de92007-09-25 17:54:57 -07001099 if (is_legacy(tbl->lq_type)) {
Daniel C Halperine3949d62009-09-17 10:43:50 -07001100 tbl->expected_tpt = expected_tpt_legacy;
1101 return;
1102 }
1103
1104 /* Choose among many HT tables depending on number of streams
1105 * (SISO/MIMO2/MIMO3), channel width (20/40), SGI, and aggregation
1106 * status */
1107 if (is_siso(tbl->lq_type) && (!tbl->is_ht40 || lq_sta->is_dup))
1108 ht_tbl_pointer = expected_tpt_siso20MHz;
1109 else if (is_siso(tbl->lq_type))
1110 ht_tbl_pointer = expected_tpt_siso40MHz;
1111 else if (is_mimo2(tbl->lq_type) && (!tbl->is_ht40 || lq_sta->is_dup))
1112 ht_tbl_pointer = expected_tpt_mimo2_20MHz;
1113 else if (is_mimo2(tbl->lq_type))
1114 ht_tbl_pointer = expected_tpt_mimo2_40MHz;
1115 else if (is_mimo3(tbl->lq_type) && (!tbl->is_ht40 || lq_sta->is_dup))
1116 ht_tbl_pointer = expected_tpt_mimo3_20MHz;
1117 else /* if (is_mimo3(tbl->lq_type)) <-- must be true */
1118 ht_tbl_pointer = expected_tpt_mimo3_40MHz;
1119
1120 if (!tbl->is_SGI && !lq_sta->is_agg) /* Normal */
1121 tbl->expected_tpt = ht_tbl_pointer[0];
1122 else if (tbl->is_SGI && !lq_sta->is_agg) /* SGI */
1123 tbl->expected_tpt = ht_tbl_pointer[1];
1124 else if (!tbl->is_SGI && lq_sta->is_agg) /* AGG */
1125 tbl->expected_tpt = ht_tbl_pointer[2];
1126 else /* AGG+SGI */
1127 tbl->expected_tpt = ht_tbl_pointer[3];
Zhu Yib481de92007-09-25 17:54:57 -07001128}
1129
Ben Cahill77626352007-11-29 11:09:44 +08001130/*
1131 * Find starting rate for new "search" high-throughput mode of modulation.
1132 * Goal is to find lowest expected rate (under perfect conditions) that is
1133 * above the current measured throughput of "active" mode, to give new mode
1134 * a fair chance to prove itself without too many challenges.
1135 *
1136 * This gets called when transitioning to more aggressive modulation
1137 * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
1138 * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
1139 * to decrease to match "active" throughput. When moving from MIMO to SISO,
1140 * bit rate will typically need to increase, but not if performance was bad.
1141 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001142static s32 rs_get_best_rate(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001143 struct iwl_lq_sta *lq_sta,
1144 struct iwl_scale_tbl_info *tbl, /* "search" */
Guy Cohenf935a6d2008-04-23 17:15:02 -07001145 u16 rate_mask, s8 index)
Zhu Yib481de92007-09-25 17:54:57 -07001146{
Ben Cahill77626352007-11-29 11:09:44 +08001147 /* "active" values */
Tomas Winklere227cea2008-07-18 13:53:05 +08001148 struct iwl_scale_tbl_info *active_tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001149 &(lq_sta->lq_info[lq_sta->active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07001150 s32 active_sr = active_tbl->win[index].success_ratio;
Zhu Yib481de92007-09-25 17:54:57 -07001151 s32 active_tpt = active_tbl->expected_tpt[index];
Ben Cahill77626352007-11-29 11:09:44 +08001152
1153 /* expected "search" throughput */
1154 s32 *tpt_tbl = tbl->expected_tpt;
1155
1156 s32 new_rate, high, low, start_hi;
Zhu Yib481de92007-09-25 17:54:57 -07001157 u16 high_low;
Guy Cohenf935a6d2008-04-23 17:15:02 -07001158 s8 rate = index;
Zhu Yib481de92007-09-25 17:54:57 -07001159
1160 new_rate = high = low = start_hi = IWL_RATE_INVALID;
1161
1162 for (; ;) {
Ester Kummerbf403db2008-05-05 10:22:40 +08001163 high_low = rs_get_adjacent_rate(priv, rate, rate_mask,
1164 tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -07001165
1166 low = high_low & 0xff;
1167 high = (high_low >> 8) & 0xff;
1168
Ben Cahill77626352007-11-29 11:09:44 +08001169 /*
1170 * Lower the "search" bit rate, to give new "search" mode
1171 * approximately the same throughput as "active" if:
1172 *
1173 * 1) "Active" mode has been working modestly well (but not
1174 * great), and expected "search" throughput (under perfect
1175 * conditions) at candidate rate is above the actual
1176 * measured "active" throughput (but less than expected
1177 * "active" throughput under perfect conditions).
1178 * OR
1179 * 2) "Active" mode has been working perfectly or very well
1180 * and expected "search" throughput (under perfect
1181 * conditions) at candidate rate is above expected
1182 * "active" throughput (under perfect conditions).
1183 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001184 if ((((100 * tpt_tbl[rate]) > lq_sta->last_tpt) &&
Zhu Yib481de92007-09-25 17:54:57 -07001185 ((active_sr > IWL_RATE_DECREASE_TH) &&
1186 (active_sr <= IWL_RATE_HIGH_TH) &&
1187 (tpt_tbl[rate] <= active_tpt))) ||
1188 ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
1189 (tpt_tbl[rate] > active_tpt))) {
1190
Ben Cahill77626352007-11-29 11:09:44 +08001191 /* (2nd or later pass)
1192 * If we've already tried to raise the rate, and are
1193 * now trying to lower it, use the higher rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001194 if (start_hi != IWL_RATE_INVALID) {
1195 new_rate = start_hi;
1196 break;
1197 }
Ben Cahill77626352007-11-29 11:09:44 +08001198
Zhu Yib481de92007-09-25 17:54:57 -07001199 new_rate = rate;
Ben Cahill77626352007-11-29 11:09:44 +08001200
1201 /* Loop again with lower rate */
Zhu Yib481de92007-09-25 17:54:57 -07001202 if (low != IWL_RATE_INVALID)
1203 rate = low;
Ben Cahill77626352007-11-29 11:09:44 +08001204
1205 /* Lower rate not available, use the original */
Zhu Yib481de92007-09-25 17:54:57 -07001206 else
1207 break;
Ben Cahill77626352007-11-29 11:09:44 +08001208
1209 /* Else try to raise the "search" rate to match "active" */
Zhu Yib481de92007-09-25 17:54:57 -07001210 } else {
Ben Cahill77626352007-11-29 11:09:44 +08001211 /* (2nd or later pass)
1212 * If we've already tried to lower the rate, and are
1213 * now trying to raise it, use the lower rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001214 if (new_rate != IWL_RATE_INVALID)
1215 break;
Ben Cahill77626352007-11-29 11:09:44 +08001216
1217 /* Loop again with higher rate */
Zhu Yib481de92007-09-25 17:54:57 -07001218 else if (high != IWL_RATE_INVALID) {
1219 start_hi = high;
1220 rate = high;
Ben Cahill77626352007-11-29 11:09:44 +08001221
1222 /* Higher rate not available, use the original */
Zhu Yib481de92007-09-25 17:54:57 -07001223 } else {
Guy Cohen90d77952008-09-09 10:54:53 +08001224 new_rate = rate;
Zhu Yib481de92007-09-25 17:54:57 -07001225 break;
1226 }
1227 }
1228 }
1229
1230 return new_rate;
1231}
Zhu Yib481de92007-09-25 17:54:57 -07001232
Ben Cahill77626352007-11-29 11:09:44 +08001233/*
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001234 * Set up search table for MIMO2
Ben Cahill77626352007-11-29 11:09:44 +08001235 */
Guy Cohenfde0db32008-04-21 15:42:01 -07001236static int rs_switch_to_mimo2(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001237 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001238 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001239 struct ieee80211_sta *sta,
Tomas Winklere227cea2008-07-18 13:53:05 +08001240 struct iwl_scale_tbl_info *tbl, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001241{
Zhu Yib481de92007-09-25 17:54:57 -07001242 u16 rate_mask;
1243 s32 rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001244 s8 is_green = lq_sta->is_green;
Johannes Berg7e6a5882010-08-23 10:46:46 +02001245 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
1246 struct iwl_rxon_context *ctx = sta_priv->common.ctx;
Zhu Yib481de92007-09-25 17:54:57 -07001247
Luis R. Rodriguezde27e642008-12-23 15:58:44 -08001248 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
Zhu Yib481de92007-09-25 17:54:57 -07001249 return -1;
1250
Johannes Bergd9fe60d2008-10-09 12:13:49 +02001251 if (((sta->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 2)
Tomas Winkler00c5ae22008-09-03 11:26:42 +08001252 == WLAN_HT_CAP_SM_PS_STATIC)
Zhu Yib481de92007-09-25 17:54:57 -07001253 return -1;
1254
Ben Cahill77626352007-11-29 11:09:44 +08001255 /* Need both Tx chains/antennas to support MIMO */
Guy Cohenaade00c2008-04-23 17:14:59 -07001256 if (priv->hw_params.tx_chains_num < 2)
Zhu Yib481de92007-09-25 17:54:57 -07001257 return -1;
1258
Tomas Winklere1623442009-01-27 14:27:56 -08001259 IWL_DEBUG_RATE(priv, "LQ: try to switch to MIMO2\n");
Guy Cohen39e88502008-04-23 17:14:57 -07001260
1261 tbl->lq_type = LQ_MIMO2;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001262 tbl->is_dup = lq_sta->is_dup;
Zhu Yib481de92007-09-25 17:54:57 -07001263 tbl->action = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001264 tbl->max_search = IWL_MAX_SEARCH;
Guy Cohen39e88502008-04-23 17:14:57 -07001265 rate_mask = lq_sta->active_mimo2_rate;
1266
Johannes Berg7e6a5882010-08-23 10:46:46 +02001267 if (iwl_is_ht40_tx_allowed(priv, ctx, &sta->ht_cap))
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001268 tbl->is_ht40 = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001269 else
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001270 tbl->is_ht40 = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001271
Guy Coheneecd6e52008-04-23 17:14:58 -07001272 rs_set_expected_tpt_table(lq_sta, tbl);
Zhu Yib481de92007-09-25 17:54:57 -07001273
Guy Cohenf935a6d2008-04-23 17:15:02 -07001274 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
Zhu Yib481de92007-09-25 17:54:57 -07001275
Tomas Winklere1623442009-01-27 14:27:56 -08001276 IWL_DEBUG_RATE(priv, "LQ: MIMO2 best rate %d mask %X\n", rate, rate_mask);
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001277 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1278 IWL_DEBUG_RATE(priv, "Can't switch with index %d rate mask %x\n",
1279 rate, rate_mask);
1280 return -1;
1281 }
1282 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
Guy Cohen39e88502008-04-23 17:14:57 -07001283
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001284 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
1285 tbl->current_rate, is_green);
1286 return 0;
1287}
1288
1289/*
1290 * Set up search table for MIMO3
1291 */
1292static int rs_switch_to_mimo3(struct iwl_priv *priv,
1293 struct iwl_lq_sta *lq_sta,
1294 struct ieee80211_conf *conf,
1295 struct ieee80211_sta *sta,
1296 struct iwl_scale_tbl_info *tbl, int index)
1297{
1298 u16 rate_mask;
1299 s32 rate;
1300 s8 is_green = lq_sta->is_green;
Johannes Berg7e6a5882010-08-23 10:46:46 +02001301 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
1302 struct iwl_rxon_context *ctx = sta_priv->common.ctx;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001303
1304 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
1305 return -1;
1306
1307 if (((sta->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 2)
1308 == WLAN_HT_CAP_SM_PS_STATIC)
1309 return -1;
1310
1311 /* Need both Tx chains/antennas to support MIMO */
1312 if (priv->hw_params.tx_chains_num < 3)
1313 return -1;
1314
1315 IWL_DEBUG_RATE(priv, "LQ: try to switch to MIMO3\n");
1316
1317 tbl->lq_type = LQ_MIMO3;
1318 tbl->is_dup = lq_sta->is_dup;
1319 tbl->action = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001320 tbl->max_search = IWL_MAX_11N_MIMO3_SEARCH;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001321 rate_mask = lq_sta->active_mimo3_rate;
1322
Johannes Berg7e6a5882010-08-23 10:46:46 +02001323 if (iwl_is_ht40_tx_allowed(priv, ctx, &sta->ht_cap))
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001324 tbl->is_ht40 = 1;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001325 else
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001326 tbl->is_ht40 = 0;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001327
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001328 rs_set_expected_tpt_table(lq_sta, tbl);
1329
1330 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
1331
1332 IWL_DEBUG_RATE(priv, "LQ: MIMO3 best rate %d mask %X\n",
1333 rate, rate_mask);
Guy Cohen39e88502008-04-23 17:14:57 -07001334 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
Tomas Winklere1623442009-01-27 14:27:56 -08001335 IWL_DEBUG_RATE(priv, "Can't switch with index %d rate mask %x\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001336 rate, rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07001337 return -1;
Guy Cohen39e88502008-04-23 17:14:57 -07001338 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001339 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
Zhu Yib481de92007-09-25 17:54:57 -07001340
Tomas Winklere1623442009-01-27 14:27:56 -08001341 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001342 tbl->current_rate, is_green);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001343 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07001344}
1345
Ben Cahill77626352007-11-29 11:09:44 +08001346/*
1347 * Set up search table for SISO
1348 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001349static int rs_switch_to_siso(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001350 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001351 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001352 struct ieee80211_sta *sta,
Tomas Winklere227cea2008-07-18 13:53:05 +08001353 struct iwl_scale_tbl_info *tbl, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001354{
Zhu Yib481de92007-09-25 17:54:57 -07001355 u16 rate_mask;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001356 u8 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07001357 s32 rate;
Johannes Berg7e6a5882010-08-23 10:46:46 +02001358 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
1359 struct iwl_rxon_context *ctx = sta_priv->common.ctx;
Zhu Yib481de92007-09-25 17:54:57 -07001360
Luis R. Rodriguezde27e642008-12-23 15:58:44 -08001361 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
Zhu Yib481de92007-09-25 17:54:57 -07001362 return -1;
1363
Tomas Winklere1623442009-01-27 14:27:56 -08001364 IWL_DEBUG_RATE(priv, "LQ: try to switch to SISO\n");
Guy Cohen39e88502008-04-23 17:14:57 -07001365
Tomas Winklerc33104f2008-01-14 17:46:21 -08001366 tbl->is_dup = lq_sta->is_dup;
Zhu Yib481de92007-09-25 17:54:57 -07001367 tbl->lq_type = LQ_SISO;
1368 tbl->action = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001369 tbl->max_search = IWL_MAX_SEARCH;
Guy Cohen39e88502008-04-23 17:14:57 -07001370 rate_mask = lq_sta->active_siso_rate;
Zhu Yib481de92007-09-25 17:54:57 -07001371
Johannes Berg7e6a5882010-08-23 10:46:46 +02001372 if (iwl_is_ht40_tx_allowed(priv, ctx, &sta->ht_cap))
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001373 tbl->is_ht40 = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001374 else
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001375 tbl->is_ht40 = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001376
Zhu Yib481de92007-09-25 17:54:57 -07001377 if (is_green)
Guy Cohen39e88502008-04-23 17:14:57 -07001378 tbl->is_SGI = 0; /*11n spec: no SGI in SISO+Greenfield*/
Zhu Yib481de92007-09-25 17:54:57 -07001379
Guy Coheneecd6e52008-04-23 17:14:58 -07001380 rs_set_expected_tpt_table(lq_sta, tbl);
Guy Cohenf935a6d2008-04-23 17:15:02 -07001381 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
Zhu Yib481de92007-09-25 17:54:57 -07001382
Tomas Winklere1623442009-01-27 14:27:56 -08001383 IWL_DEBUG_RATE(priv, "LQ: get best rate %d mask %X\n", rate, rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07001384 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
Tomas Winklere1623442009-01-27 14:27:56 -08001385 IWL_DEBUG_RATE(priv, "can not switch with index %d rate mask %x\n",
Zhu Yib481de92007-09-25 17:54:57 -07001386 rate, rate_mask);
1387 return -1;
1388 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001389 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
Tomas Winklere1623442009-01-27 14:27:56 -08001390 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001391 tbl->current_rate, is_green);
Mohamed Abbas403ab562007-11-06 22:06:25 -08001392 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07001393}
1394
Ben Cahill77626352007-11-29 11:09:44 +08001395/*
1396 * Try to switch to new modulation mode from legacy
1397 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001398static int rs_move_legacy_other(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001399 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001400 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001401 struct ieee80211_sta *sta,
Zhu Yib481de92007-09-25 17:54:57 -07001402 int index)
1403{
Tomas Winklere227cea2008-07-18 13:53:05 +08001404 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1405 struct iwl_scale_tbl_info *search_tbl =
1406 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1407 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1408 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1409 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Wey-Yi Guyb1a78852010-07-31 08:52:03 -07001410 u8 start_action;
Guy Cohenfde0db32008-04-21 15:42:01 -07001411 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
Guy Cohen3110bef2008-09-09 10:54:54 +08001412 u8 tx_chains_num = priv->hw_params.tx_chains_num;
Guy Cohenfde0db32008-04-21 15:42:01 -07001413 int ret = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001414 u8 update_search_tbl_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001415
Johannes Berg290f5992010-08-23 07:56:58 -07001416 switch (priv->bt_traffic_load) {
1417 case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
1418 /* nothing */
1419 break;
1420 case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
1421 /* avoid antenna B unless MIMO */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001422 valid_tx_ant = first_antenna(priv->hw_params.valid_tx_ant);
Johannes Berg290f5992010-08-23 07:56:58 -07001423 if (tbl->action == IWL_LEGACY_SWITCH_ANTENNA2)
1424 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
1425 break;
1426 case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
1427 case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
1428 /* avoid antenna B and MIMO */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001429 valid_tx_ant = first_antenna(priv->hw_params.valid_tx_ant);
Johannes Berg290f5992010-08-23 07:56:58 -07001430 if (tbl->action >= IWL_LEGACY_SWITCH_ANTENNA2 &&
1431 tbl->action != IWL_LEGACY_SWITCH_SISO)
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001432 tbl->action = IWL_LEGACY_SWITCH_SISO;
Johannes Berg290f5992010-08-23 07:56:58 -07001433 break;
1434 default:
1435 IWL_ERR(priv, "Invalid BT load %d", priv->bt_traffic_load);
1436 break;
1437 }
1438
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001439 if (!iwl_ht_enabled(priv))
1440 /* stay in Legacy */
1441 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
Johannes Berg3ad3b922009-08-07 15:41:48 -07001442 else if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001443 tbl->action > IWL_LEGACY_SWITCH_SISO)
1444 tbl->action = IWL_LEGACY_SWITCH_SISO;
Wey-Yi Guybee008b2010-08-23 07:57:04 -07001445
1446 /* configure as 1x1 if bt full concurrency */
1447 if (priv->bt_full_concurrent) {
1448 if (!iwl_ht_enabled(priv))
1449 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
1450 else if (tbl->action >= IWL_LEGACY_SWITCH_ANTENNA2)
1451 tbl->action = IWL_LEGACY_SWITCH_SISO;
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001452 valid_tx_ant = first_antenna(priv->hw_params.valid_tx_ant);
Wey-Yi Guybee008b2010-08-23 07:57:04 -07001453 }
1454
Wey-Yi Guyb1a78852010-07-31 08:52:03 -07001455 start_action = tbl->action;
Zhu Yib481de92007-09-25 17:54:57 -07001456 for (; ;) {
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001457 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001458 switch (tbl->action) {
Guy Cohen3110bef2008-09-09 10:54:54 +08001459 case IWL_LEGACY_SWITCH_ANTENNA1:
1460 case IWL_LEGACY_SWITCH_ANTENNA2:
Tomas Winklere1623442009-01-27 14:27:56 -08001461 IWL_DEBUG_RATE(priv, "LQ: Legacy toggle Antenna\n");
Zhu Yib481de92007-09-25 17:54:57 -07001462
Guy Cohen3110bef2008-09-09 10:54:54 +08001463 if ((tbl->action == IWL_LEGACY_SWITCH_ANTENNA1 &&
1464 tx_chains_num <= 1) ||
1465 (tbl->action == IWL_LEGACY_SWITCH_ANTENNA2 &&
1466 tx_chains_num <= 2))
1467 break;
1468
Ben Cahill77626352007-11-29 11:09:44 +08001469 /* Don't change antenna if success has been great */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001470 if (window->success_ratio >= IWL_RS_GOOD_RATIO &&
1471 !priv->bt_full_concurrent &&
1472 priv->bt_traffic_load ==
1473 IWL_BT_COEX_TRAFFIC_LOAD_NONE)
Zhu Yib481de92007-09-25 17:54:57 -07001474 break;
Ben Cahill77626352007-11-29 11:09:44 +08001475
Ben Cahill77626352007-11-29 11:09:44 +08001476 /* Set up search table to try other antenna */
Zhu Yib481de92007-09-25 17:54:57 -07001477 memcpy(search_tbl, tbl, sz);
1478
Guy Cohenaade00c2008-04-23 17:14:59 -07001479 if (rs_toggle_antenna(valid_tx_ant,
1480 &search_tbl->current_rate, search_tbl)) {
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001481 update_search_tbl_counter = 1;
Guy Cohen3110bef2008-09-09 10:54:54 +08001482 rs_set_expected_tpt_table(lq_sta, search_tbl);
Guy Cohenaade00c2008-04-23 17:14:59 -07001483 goto out;
1484 }
Guy Cohena5e8b502008-05-29 16:35:11 +08001485 break;
Zhu Yib481de92007-09-25 17:54:57 -07001486 case IWL_LEGACY_SWITCH_SISO:
Tomas Winklere1623442009-01-27 14:27:56 -08001487 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to SISO\n");
Ben Cahill77626352007-11-29 11:09:44 +08001488
1489 /* Set up search table to try SISO */
Zhu Yib481de92007-09-25 17:54:57 -07001490 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001491 search_tbl->is_SGI = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001492 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001493 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001494 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001495 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001496 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001497 }
Zhu Yib481de92007-09-25 17:54:57 -07001498
1499 break;
Guy Cohen3110bef2008-09-09 10:54:54 +08001500 case IWL_LEGACY_SWITCH_MIMO2_AB:
1501 case IWL_LEGACY_SWITCH_MIMO2_AC:
1502 case IWL_LEGACY_SWITCH_MIMO2_BC:
Tomas Winklere1623442009-01-27 14:27:56 -08001503 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to MIMO2\n");
Ben Cahill77626352007-11-29 11:09:44 +08001504
1505 /* Set up search table to try MIMO */
Zhu Yib481de92007-09-25 17:54:57 -07001506 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001507 search_tbl->is_SGI = 0;
Guy Cohen3110bef2008-09-09 10:54:54 +08001508
1509 if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AB)
1510 search_tbl->ant_type = ANT_AB;
1511 else if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AC)
1512 search_tbl->ant_type = ANT_AC;
1513 else
1514 search_tbl->ant_type = ANT_BC;
1515
1516 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1517 break;
1518
Guy Cohenfde0db32008-04-21 15:42:01 -07001519 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001520 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001521 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001522 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001523 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001524 }
Zhu Yib481de92007-09-25 17:54:57 -07001525 break;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001526
1527 case IWL_LEGACY_SWITCH_MIMO3_ABC:
1528 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to MIMO3\n");
1529
1530 /* Set up search table to try MIMO3 */
1531 memcpy(search_tbl, tbl, sz);
1532 search_tbl->is_SGI = 0;
1533
1534 search_tbl->ant_type = ANT_ABC;
1535
1536 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1537 break;
1538
1539 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1540 search_tbl, index);
1541 if (!ret) {
1542 lq_sta->action_counter = 0;
1543 goto out;
1544 }
1545 break;
Zhu Yib481de92007-09-25 17:54:57 -07001546 }
1547 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001548 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001549 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
Zhu Yib481de92007-09-25 17:54:57 -07001550
1551 if (tbl->action == start_action)
1552 break;
1553
1554 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001555 search_tbl->lq_type = LQ_NONE;
Zhu Yib481de92007-09-25 17:54:57 -07001556 return 0;
1557
Guy Cohen3110bef2008-09-09 10:54:54 +08001558out:
1559 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001560 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001561 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001562 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001563 if (update_search_tbl_counter)
1564 search_tbl->action = tbl->action;
Zhu Yib481de92007-09-25 17:54:57 -07001565 return 0;
1566
1567}
1568
Ben Cahill77626352007-11-29 11:09:44 +08001569/*
1570 * Try to switch to new modulation mode from SISO
1571 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001572static int rs_move_siso_to_other(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001573 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001574 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001575 struct ieee80211_sta *sta, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001576{
Tomas Winklerc33104f2008-01-14 17:46:21 -08001577 u8 is_green = lq_sta->is_green;
Tomas Winklere227cea2008-07-18 13:53:05 +08001578 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1579 struct iwl_scale_tbl_info *search_tbl =
1580 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1581 struct iwl_rate_scale_data *window = &(tbl->win[index]);
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001582 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Tomas Winklere227cea2008-07-18 13:53:05 +08001583 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1584 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Wey-Yi Guyb1a78852010-07-31 08:52:03 -07001585 u8 start_action;
Guy Cohenfde0db32008-04-21 15:42:01 -07001586 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
Guy Cohen3110bef2008-09-09 10:54:54 +08001587 u8 tx_chains_num = priv->hw_params.tx_chains_num;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001588 u8 update_search_tbl_counter = 0;
Guy Cohenfde0db32008-04-21 15:42:01 -07001589 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001590
Johannes Berg290f5992010-08-23 07:56:58 -07001591 switch (priv->bt_traffic_load) {
1592 case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
1593 /* nothing */
1594 break;
1595 case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
1596 /* avoid antenna B unless MIMO */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001597 valid_tx_ant = first_antenna(priv->hw_params.valid_tx_ant);
Johannes Berg290f5992010-08-23 07:56:58 -07001598 if (tbl->action == IWL_SISO_SWITCH_ANTENNA2)
1599 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1600 break;
1601 case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
1602 case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
1603 /* avoid antenna B and MIMO */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001604 valid_tx_ant = first_antenna(priv->hw_params.valid_tx_ant);
1605 if (tbl->action != IWL_SISO_SWITCH_ANTENNA1)
Johannes Berg290f5992010-08-23 07:56:58 -07001606 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1607 break;
1608 default:
1609 IWL_ERR(priv, "Invalid BT load %d", priv->bt_traffic_load);
1610 break;
1611 }
1612
Johannes Berg3ad3b922009-08-07 15:41:48 -07001613 if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001614 tbl->action > IWL_SISO_SWITCH_ANTENNA2) {
1615 /* stay in SISO */
1616 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1617 }
Wey-Yi Guybee008b2010-08-23 07:57:04 -07001618
1619 /* configure as 1x1 if bt full concurrency */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001620 if (priv->bt_full_concurrent) {
1621 valid_tx_ant = first_antenna(priv->hw_params.valid_tx_ant);
1622 if (tbl->action >= IWL_LEGACY_SWITCH_ANTENNA2)
1623 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1624 }
Wey-Yi Guybee008b2010-08-23 07:57:04 -07001625
Wey-Yi Guyb1a78852010-07-31 08:52:03 -07001626 start_action = tbl->action;
Zhu Yib481de92007-09-25 17:54:57 -07001627 for (;;) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001628 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001629 switch (tbl->action) {
Guy Cohen3110bef2008-09-09 10:54:54 +08001630 case IWL_SISO_SWITCH_ANTENNA1:
1631 case IWL_SISO_SWITCH_ANTENNA2:
Tomas Winklere1623442009-01-27 14:27:56 -08001632 IWL_DEBUG_RATE(priv, "LQ: SISO toggle Antenna\n");
Guy Cohen3110bef2008-09-09 10:54:54 +08001633 if ((tbl->action == IWL_SISO_SWITCH_ANTENNA1 &&
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001634 tx_chains_num <= 1) ||
Guy Cohen3110bef2008-09-09 10:54:54 +08001635 (tbl->action == IWL_SISO_SWITCH_ANTENNA2 &&
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001636 tx_chains_num <= 2))
Guy Cohen3110bef2008-09-09 10:54:54 +08001637 break;
1638
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001639 if (window->success_ratio >= IWL_RS_GOOD_RATIO &&
1640 !priv->bt_full_concurrent &&
1641 priv->bt_traffic_load ==
1642 IWL_BT_COEX_TRAFFIC_LOAD_NONE)
Zhu Yib481de92007-09-25 17:54:57 -07001643 break;
Zhu Yib481de92007-09-25 17:54:57 -07001644
1645 memcpy(search_tbl, tbl, sz);
Guy Cohenaade00c2008-04-23 17:14:59 -07001646 if (rs_toggle_antenna(valid_tx_ant,
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001647 &search_tbl->current_rate, search_tbl)) {
1648 update_search_tbl_counter = 1;
Guy Cohenaade00c2008-04-23 17:14:59 -07001649 goto out;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001650 }
Guy Cohena5e8b502008-05-29 16:35:11 +08001651 break;
Guy Cohen3110bef2008-09-09 10:54:54 +08001652 case IWL_SISO_SWITCH_MIMO2_AB:
1653 case IWL_SISO_SWITCH_MIMO2_AC:
1654 case IWL_SISO_SWITCH_MIMO2_BC:
Tomas Winklere1623442009-01-27 14:27:56 -08001655 IWL_DEBUG_RATE(priv, "LQ: SISO switch to MIMO2\n");
Zhu Yib481de92007-09-25 17:54:57 -07001656 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001657 search_tbl->is_SGI = 0;
Guy Cohen3110bef2008-09-09 10:54:54 +08001658
1659 if (tbl->action == IWL_SISO_SWITCH_MIMO2_AB)
1660 search_tbl->ant_type = ANT_AB;
1661 else if (tbl->action == IWL_SISO_SWITCH_MIMO2_AC)
1662 search_tbl->ant_type = ANT_AC;
1663 else
1664 search_tbl->ant_type = ANT_BC;
1665
1666 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1667 break;
1668
Guy Cohenfde0db32008-04-21 15:42:01 -07001669 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001670 search_tbl, index);
Guy Cohen3110bef2008-09-09 10:54:54 +08001671 if (!ret)
Zhu Yib481de92007-09-25 17:54:57 -07001672 goto out;
1673 break;
1674 case IWL_SISO_SWITCH_GI:
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001675 if (!tbl->is_ht40 && !(ht_cap->cap &
1676 IEEE80211_HT_CAP_SGI_20))
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001677 break;
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001678 if (tbl->is_ht40 && !(ht_cap->cap &
1679 IEEE80211_HT_CAP_SGI_40))
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001680 break;
1681
Tomas Winklere1623442009-01-27 14:27:56 -08001682 IWL_DEBUG_RATE(priv, "LQ: SISO toggle SGI/NGI\n");
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001683
Zhu Yib481de92007-09-25 17:54:57 -07001684 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001685 if (is_green) {
1686 if (!tbl->is_SGI)
1687 break;
1688 else
Winkler, Tomas15b16872008-12-19 10:37:33 +08001689 IWL_ERR(priv,
1690 "SGI was set in GF+SISO\n");
Zhu Yib481de92007-09-25 17:54:57 -07001691 }
Guy Cohen39e88502008-04-23 17:14:57 -07001692 search_tbl->is_SGI = !tbl->is_SGI;
Guy Coheneecd6e52008-04-23 17:14:58 -07001693 rs_set_expected_tpt_table(lq_sta, search_tbl);
Guy Cohen39e88502008-04-23 17:14:57 -07001694 if (tbl->is_SGI) {
1695 s32 tpt = lq_sta->last_tpt / 100;
1696 if (tpt >= search_tbl->expected_tpt[index])
1697 break;
1698 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001699 search_tbl->current_rate =
1700 rate_n_flags_from_tbl(priv, search_tbl,
1701 index, is_green);
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001702 update_search_tbl_counter = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001703 goto out;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001704 case IWL_SISO_SWITCH_MIMO3_ABC:
1705 IWL_DEBUG_RATE(priv, "LQ: SISO switch to MIMO3\n");
1706 memcpy(search_tbl, tbl, sz);
1707 search_tbl->is_SGI = 0;
1708 search_tbl->ant_type = ANT_ABC;
1709
1710 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1711 break;
1712
1713 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1714 search_tbl, index);
1715 if (!ret)
1716 goto out;
1717 break;
Zhu Yib481de92007-09-25 17:54:57 -07001718 }
1719 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001720 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001721 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
Zhu Yib481de92007-09-25 17:54:57 -07001722
1723 if (tbl->action == start_action)
1724 break;
1725 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001726 search_tbl->lq_type = LQ_NONE;
Zhu Yib481de92007-09-25 17:54:57 -07001727 return 0;
1728
1729 out:
Guy Cohen3110bef2008-09-09 10:54:54 +08001730 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001731 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001732 if (tbl->action > IWL_SISO_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001733 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001734 if (update_search_tbl_counter)
1735 search_tbl->action = tbl->action;
1736
Zhu Yib481de92007-09-25 17:54:57 -07001737 return 0;
1738}
1739
Ben Cahill77626352007-11-29 11:09:44 +08001740/*
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001741 * Try to switch to new modulation mode from MIMO2
Ben Cahill77626352007-11-29 11:09:44 +08001742 */
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001743static int rs_move_mimo2_to_other(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001744 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001745 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001746 struct ieee80211_sta *sta, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001747{
Tomas Winklerc33104f2008-01-14 17:46:21 -08001748 s8 is_green = lq_sta->is_green;
Tomas Winklere227cea2008-07-18 13:53:05 +08001749 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1750 struct iwl_scale_tbl_info *search_tbl =
1751 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Guy Cohen3110bef2008-09-09 10:54:54 +08001752 struct iwl_rate_scale_data *window = &(tbl->win[index]);
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001753 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Tomas Winklere227cea2008-07-18 13:53:05 +08001754 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1755 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Wey-Yi Guyb1a78852010-07-31 08:52:03 -07001756 u8 start_action;
Guy Cohen3110bef2008-09-09 10:54:54 +08001757 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1758 u8 tx_chains_num = priv->hw_params.tx_chains_num;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001759 u8 update_search_tbl_counter = 0;
Guy Cohenaade00c2008-04-23 17:14:59 -07001760 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001761
Johannes Berg290f5992010-08-23 07:56:58 -07001762 switch (priv->bt_traffic_load) {
1763 case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
1764 /* nothing */
1765 break;
1766 case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
1767 case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
1768 /* avoid antenna B and MIMO */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001769 if (tbl->action != IWL_MIMO2_SWITCH_SISO_A)
1770 tbl->action = IWL_MIMO2_SWITCH_SISO_A;
1771 break;
Johannes Berg290f5992010-08-23 07:56:58 -07001772 case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
1773 /* avoid antenna B unless MIMO */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001774 if (tbl->action == IWL_MIMO2_SWITCH_SISO_B ||
1775 tbl->action == IWL_MIMO2_SWITCH_SISO_C)
Johannes Berg290f5992010-08-23 07:56:58 -07001776 tbl->action = IWL_MIMO2_SWITCH_SISO_A;
1777 break;
1778 default:
1779 IWL_ERR(priv, "Invalid BT load %d", priv->bt_traffic_load);
1780 break;
1781 }
1782
Johannes Berg3ad3b922009-08-07 15:41:48 -07001783 if ((iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE) &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001784 (tbl->action < IWL_MIMO2_SWITCH_SISO_A ||
1785 tbl->action > IWL_MIMO2_SWITCH_SISO_C)) {
1786 /* switch in SISO */
1787 tbl->action = IWL_MIMO2_SWITCH_SISO_A;
1788 }
Wey-Yi Guybee008b2010-08-23 07:57:04 -07001789
1790 /* configure as 1x1 if bt full concurrency */
1791 if (priv->bt_full_concurrent &&
1792 (tbl->action < IWL_MIMO2_SWITCH_SISO_A ||
1793 tbl->action > IWL_MIMO2_SWITCH_SISO_C))
1794 tbl->action = IWL_MIMO2_SWITCH_SISO_A;
1795
Wey-Yi Guyb1a78852010-07-31 08:52:03 -07001796 start_action = tbl->action;
Zhu Yib481de92007-09-25 17:54:57 -07001797 for (;;) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001798 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001799 switch (tbl->action) {
Guy Cohen3110bef2008-09-09 10:54:54 +08001800 case IWL_MIMO2_SWITCH_ANTENNA1:
1801 case IWL_MIMO2_SWITCH_ANTENNA2:
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001802 IWL_DEBUG_RATE(priv, "LQ: MIMO2 toggle Antennas\n");
Guy Cohen3110bef2008-09-09 10:54:54 +08001803
1804 if (tx_chains_num <= 2)
1805 break;
1806
1807 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1808 break;
1809
1810 memcpy(search_tbl, tbl, sz);
1811 if (rs_toggle_antenna(valid_tx_ant,
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001812 &search_tbl->current_rate, search_tbl)) {
1813 update_search_tbl_counter = 1;
Guy Cohen3110bef2008-09-09 10:54:54 +08001814 goto out;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001815 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001816 break;
1817 case IWL_MIMO2_SWITCH_SISO_A:
1818 case IWL_MIMO2_SWITCH_SISO_B:
1819 case IWL_MIMO2_SWITCH_SISO_C:
Tomas Winklere1623442009-01-27 14:27:56 -08001820 IWL_DEBUG_RATE(priv, "LQ: MIMO2 switch to SISO\n");
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001821
Ben Cahill77626352007-11-29 11:09:44 +08001822 /* Set up new search table for SISO */
Zhu Yib481de92007-09-25 17:54:57 -07001823 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001824
Guy Cohen3110bef2008-09-09 10:54:54 +08001825 if (tbl->action == IWL_MIMO2_SWITCH_SISO_A)
Guy Cohenfde0db32008-04-21 15:42:01 -07001826 search_tbl->ant_type = ANT_A;
Guy Cohen3110bef2008-09-09 10:54:54 +08001827 else if (tbl->action == IWL_MIMO2_SWITCH_SISO_B)
Guy Cohenfde0db32008-04-21 15:42:01 -07001828 search_tbl->ant_type = ANT_B;
Guy Cohen3110bef2008-09-09 10:54:54 +08001829 else
1830 search_tbl->ant_type = ANT_C;
1831
1832 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1833 break;
Zhu Yib481de92007-09-25 17:54:57 -07001834
Tomas Winklerc33104f2008-01-14 17:46:21 -08001835 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001836 search_tbl, index);
Guy Cohen3110bef2008-09-09 10:54:54 +08001837 if (!ret)
Zhu Yib481de92007-09-25 17:54:57 -07001838 goto out;
Guy Cohen3110bef2008-09-09 10:54:54 +08001839
Zhu Yib481de92007-09-25 17:54:57 -07001840 break;
1841
Guy Cohen3110bef2008-09-09 10:54:54 +08001842 case IWL_MIMO2_SWITCH_GI:
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001843 if (!tbl->is_ht40 && !(ht_cap->cap &
1844 IEEE80211_HT_CAP_SGI_20))
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001845 break;
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001846 if (tbl->is_ht40 && !(ht_cap->cap &
1847 IEEE80211_HT_CAP_SGI_40))
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001848 break;
1849
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001850 IWL_DEBUG_RATE(priv, "LQ: MIMO2 toggle SGI/NGI\n");
1851
1852 /* Set up new search table for MIMO2 */
1853 memcpy(search_tbl, tbl, sz);
1854 search_tbl->is_SGI = !tbl->is_SGI;
1855 rs_set_expected_tpt_table(lq_sta, search_tbl);
1856 /*
1857 * If active table already uses the fastest possible
1858 * modulation (dual stream with short guard interval),
1859 * and it's working well, there's no need to look
1860 * for a better type of modulation!
1861 */
1862 if (tbl->is_SGI) {
1863 s32 tpt = lq_sta->last_tpt / 100;
1864 if (tpt >= search_tbl->expected_tpt[index])
1865 break;
1866 }
1867 search_tbl->current_rate =
1868 rate_n_flags_from_tbl(priv, search_tbl,
1869 index, is_green);
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001870 update_search_tbl_counter = 1;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001871 goto out;
1872
1873 case IWL_MIMO2_SWITCH_MIMO3_ABC:
1874 IWL_DEBUG_RATE(priv, "LQ: MIMO2 switch to MIMO3\n");
1875 memcpy(search_tbl, tbl, sz);
1876 search_tbl->is_SGI = 0;
1877 search_tbl->ant_type = ANT_ABC;
1878
1879 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1880 break;
1881
1882 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1883 search_tbl, index);
1884 if (!ret)
1885 goto out;
1886
1887 break;
1888 }
1889 tbl->action++;
1890 if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
1891 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
1892
1893 if (tbl->action == start_action)
1894 break;
1895 }
1896 search_tbl->lq_type = LQ_NONE;
1897 return 0;
1898 out:
1899 lq_sta->search_better_tbl = 1;
1900 tbl->action++;
1901 if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
1902 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001903 if (update_search_tbl_counter)
1904 search_tbl->action = tbl->action;
1905
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001906 return 0;
1907
1908}
1909
1910/*
1911 * Try to switch to new modulation mode from MIMO3
1912 */
1913static int rs_move_mimo3_to_other(struct iwl_priv *priv,
1914 struct iwl_lq_sta *lq_sta,
1915 struct ieee80211_conf *conf,
1916 struct ieee80211_sta *sta, int index)
1917{
1918 s8 is_green = lq_sta->is_green;
1919 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1920 struct iwl_scale_tbl_info *search_tbl =
1921 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1922 struct iwl_rate_scale_data *window = &(tbl->win[index]);
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001923 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001924 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1925 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Wey-Yi Guyb1a78852010-07-31 08:52:03 -07001926 u8 start_action;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001927 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1928 u8 tx_chains_num = priv->hw_params.tx_chains_num;
1929 int ret;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001930 u8 update_search_tbl_counter = 0;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001931
Johannes Berg290f5992010-08-23 07:56:58 -07001932 switch (priv->bt_traffic_load) {
1933 case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
1934 /* nothing */
1935 break;
1936 case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
1937 case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
1938 /* avoid antenna B and MIMO */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001939 if (tbl->action != IWL_MIMO3_SWITCH_SISO_A)
Johannes Berg290f5992010-08-23 07:56:58 -07001940 tbl->action = IWL_MIMO3_SWITCH_SISO_A;
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001941 break;
Johannes Berg290f5992010-08-23 07:56:58 -07001942 case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
1943 /* avoid antenna B unless MIMO */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001944 if (tbl->action == IWL_MIMO3_SWITCH_SISO_B ||
1945 tbl->action == IWL_MIMO3_SWITCH_SISO_C)
Johannes Berg290f5992010-08-23 07:56:58 -07001946 tbl->action = IWL_MIMO3_SWITCH_SISO_A;
Johannes Berg290f5992010-08-23 07:56:58 -07001947 break;
1948 default:
1949 IWL_ERR(priv, "Invalid BT load %d", priv->bt_traffic_load);
1950 break;
1951 }
1952
Johannes Berg3ad3b922009-08-07 15:41:48 -07001953 if ((iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE) &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001954 (tbl->action < IWL_MIMO3_SWITCH_SISO_A ||
1955 tbl->action > IWL_MIMO3_SWITCH_SISO_C)) {
1956 /* switch in SISO */
1957 tbl->action = IWL_MIMO3_SWITCH_SISO_A;
1958 }
Wey-Yi Guybee008b2010-08-23 07:57:04 -07001959
1960 /* configure as 1x1 if bt full concurrency */
1961 if (priv->bt_full_concurrent &&
1962 (tbl->action < IWL_MIMO3_SWITCH_SISO_A ||
1963 tbl->action > IWL_MIMO3_SWITCH_SISO_C))
1964 tbl->action = IWL_MIMO3_SWITCH_SISO_A;
1965
Wey-Yi Guyb1a78852010-07-31 08:52:03 -07001966 start_action = tbl->action;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001967 for (;;) {
1968 lq_sta->action_counter++;
1969 switch (tbl->action) {
1970 case IWL_MIMO3_SWITCH_ANTENNA1:
1971 case IWL_MIMO3_SWITCH_ANTENNA2:
1972 IWL_DEBUG_RATE(priv, "LQ: MIMO3 toggle Antennas\n");
1973
1974 if (tx_chains_num <= 3)
1975 break;
1976
1977 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1978 break;
1979
1980 memcpy(search_tbl, tbl, sz);
1981 if (rs_toggle_antenna(valid_tx_ant,
1982 &search_tbl->current_rate, search_tbl))
1983 goto out;
1984 break;
1985 case IWL_MIMO3_SWITCH_SISO_A:
1986 case IWL_MIMO3_SWITCH_SISO_B:
1987 case IWL_MIMO3_SWITCH_SISO_C:
1988 IWL_DEBUG_RATE(priv, "LQ: MIMO3 switch to SISO\n");
1989
1990 /* Set up new search table for SISO */
1991 memcpy(search_tbl, tbl, sz);
1992
1993 if (tbl->action == IWL_MIMO3_SWITCH_SISO_A)
1994 search_tbl->ant_type = ANT_A;
1995 else if (tbl->action == IWL_MIMO3_SWITCH_SISO_B)
1996 search_tbl->ant_type = ANT_B;
1997 else
1998 search_tbl->ant_type = ANT_C;
1999
2000 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
2001 break;
2002
2003 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
2004 search_tbl, index);
2005 if (!ret)
2006 goto out;
2007
2008 break;
2009
2010 case IWL_MIMO3_SWITCH_MIMO2_AB:
2011 case IWL_MIMO3_SWITCH_MIMO2_AC:
2012 case IWL_MIMO3_SWITCH_MIMO2_BC:
2013 IWL_DEBUG_RATE(priv, "LQ: MIMO3 switch to MIMO2\n");
2014
2015 memcpy(search_tbl, tbl, sz);
2016 search_tbl->is_SGI = 0;
2017 if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AB)
2018 search_tbl->ant_type = ANT_AB;
2019 else if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AC)
2020 search_tbl->ant_type = ANT_AC;
2021 else
2022 search_tbl->ant_type = ANT_BC;
2023
2024 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
2025 break;
2026
2027 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
2028 search_tbl, index);
2029 if (!ret)
2030 goto out;
2031
2032 break;
2033
2034 case IWL_MIMO3_SWITCH_GI:
Daniel C Halperin28e6f482009-08-13 13:30:58 -07002035 if (!tbl->is_ht40 && !(ht_cap->cap &
2036 IEEE80211_HT_CAP_SGI_20))
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07002037 break;
Daniel C Halperin28e6f482009-08-13 13:30:58 -07002038 if (tbl->is_ht40 && !(ht_cap->cap &
2039 IEEE80211_HT_CAP_SGI_40))
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07002040 break;
2041
2042 IWL_DEBUG_RATE(priv, "LQ: MIMO3 toggle SGI/NGI\n");
Ben Cahill77626352007-11-29 11:09:44 +08002043
2044 /* Set up new search table for MIMO */
Zhu Yib481de92007-09-25 17:54:57 -07002045 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07002046 search_tbl->is_SGI = !tbl->is_SGI;
Guy Coheneecd6e52008-04-23 17:14:58 -07002047 rs_set_expected_tpt_table(lq_sta, search_tbl);
Ben Cahill77626352007-11-29 11:09:44 +08002048 /*
2049 * If active table already uses the fastest possible
2050 * modulation (dual stream with short guard interval),
2051 * and it's working well, there's no need to look
2052 * for a better type of modulation!
2053 */
Guy Cohen39e88502008-04-23 17:14:57 -07002054 if (tbl->is_SGI) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002055 s32 tpt = lq_sta->last_tpt / 100;
Guy Cohen39e88502008-04-23 17:14:57 -07002056 if (tpt >= search_tbl->expected_tpt[index])
2057 break;
Zhu Yib481de92007-09-25 17:54:57 -07002058 }
Tomas Winkler978785a2008-12-19 10:37:31 +08002059 search_tbl->current_rate =
2060 rate_n_flags_from_tbl(priv, search_tbl,
2061 index, is_green);
Mohamed Abbasd6e93392009-05-08 13:44:39 -07002062 update_search_tbl_counter = 1;
Zhu Yib481de92007-09-25 17:54:57 -07002063 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07002064 }
2065 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07002066 if (tbl->action > IWL_MIMO3_SWITCH_GI)
2067 tbl->action = IWL_MIMO3_SWITCH_ANTENNA1;
Zhu Yib481de92007-09-25 17:54:57 -07002068
2069 if (tbl->action == start_action)
2070 break;
2071 }
Guy Cohen3110bef2008-09-09 10:54:54 +08002072 search_tbl->lq_type = LQ_NONE;
Zhu Yib481de92007-09-25 17:54:57 -07002073 return 0;
2074 out:
Guy Cohen3110bef2008-09-09 10:54:54 +08002075 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07002076 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07002077 if (tbl->action > IWL_MIMO3_SWITCH_GI)
2078 tbl->action = IWL_MIMO3_SWITCH_ANTENNA1;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07002079 if (update_search_tbl_counter)
2080 search_tbl->action = tbl->action;
2081
Zhu Yib481de92007-09-25 17:54:57 -07002082 return 0;
2083
2084}
2085
Ben Cahill77626352007-11-29 11:09:44 +08002086/*
2087 * Check whether we should continue using same modulation mode, or
2088 * begin search for a new mode, based on:
2089 * 1) # tx successes or failures while using this mode
2090 * 2) # times calling this function
2091 * 3) elapsed time in this mode (not used, for now)
2092 */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07002093static void rs_stay_in_table(struct iwl_lq_sta *lq_sta, bool force_search)
Zhu Yib481de92007-09-25 17:54:57 -07002094{
Tomas Winklere227cea2008-07-18 13:53:05 +08002095 struct iwl_scale_tbl_info *tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002096 int i;
2097 int active_tbl;
2098 int flush_interval_passed = 0;
Ester Kummerbf403db2008-05-05 10:22:40 +08002099 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07002100
Ester Kummerbf403db2008-05-05 10:22:40 +08002101 priv = lq_sta->drv;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002102 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002103
Tomas Winklerc33104f2008-01-14 17:46:21 -08002104 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07002105
Ben Cahill77626352007-11-29 11:09:44 +08002106 /* If we've been disallowing search, see if we should now allow it */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002107 if (lq_sta->stay_in_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07002108
Ben Cahill77626352007-11-29 11:09:44 +08002109 /* Elapsed time using current modulation mode */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002110 if (lq_sta->flush_timer)
Zhu Yib481de92007-09-25 17:54:57 -07002111 flush_interval_passed =
Mohamed Abbas447fee72009-04-20 14:37:02 -07002112 time_after(jiffies,
2113 (unsigned long)(lq_sta->flush_timer +
Zhu Yib481de92007-09-25 17:54:57 -07002114 IWL_RATE_SCALE_FLUSH_INTVL));
2115
Ben Cahill77626352007-11-29 11:09:44 +08002116 /*
2117 * Check if we should allow search for new modulation mode.
2118 * If many frames have failed or succeeded, or we've used
2119 * this same modulation for a long time, allow search, and
2120 * reset history stats that keep track of whether we should
2121 * allow a new search. Also (below) reset all bitmaps and
2122 * stats in active history.
2123 */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07002124 if (force_search ||
2125 (lq_sta->total_failed > lq_sta->max_failure_limit) ||
Tomas Winklerc33104f2008-01-14 17:46:21 -08002126 (lq_sta->total_success > lq_sta->max_success_limit) ||
2127 ((!lq_sta->search_better_tbl) && (lq_sta->flush_timer)
Zhu Yib481de92007-09-25 17:54:57 -07002128 && (flush_interval_passed))) {
Tomas Winklere1623442009-01-27 14:27:56 -08002129 IWL_DEBUG_RATE(priv, "LQ: stay is expired %d %d %d\n:",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002130 lq_sta->total_failed,
2131 lq_sta->total_success,
Zhu Yib481de92007-09-25 17:54:57 -07002132 flush_interval_passed);
Ben Cahill77626352007-11-29 11:09:44 +08002133
2134 /* Allow search for new mode */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002135 lq_sta->stay_in_tbl = 0; /* only place reset */
2136 lq_sta->total_failed = 0;
2137 lq_sta->total_success = 0;
2138 lq_sta->flush_timer = 0;
Ben Cahill77626352007-11-29 11:09:44 +08002139
2140 /*
2141 * Else if we've used this modulation mode enough repetitions
2142 * (regardless of elapsed time or success/failure), reset
2143 * history bitmaps and rate-specific stats for all rates in
2144 * active table.
2145 */
Mohamed Abbas403ab562007-11-06 22:06:25 -08002146 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002147 lq_sta->table_count++;
2148 if (lq_sta->table_count >=
2149 lq_sta->table_count_limit) {
2150 lq_sta->table_count = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002151
Tomas Winklere1623442009-01-27 14:27:56 -08002152 IWL_DEBUG_RATE(priv, "LQ: stay in table clear win\n");
Zhu Yib481de92007-09-25 17:54:57 -07002153 for (i = 0; i < IWL_RATE_COUNT; i++)
2154 rs_rate_scale_clear_window(
2155 &(tbl->win[i]));
2156 }
2157 }
2158
Ben Cahill77626352007-11-29 11:09:44 +08002159 /* If transitioning to allow "search", reset all history
2160 * bitmaps and stats in active table (this will become the new
2161 * "search" table). */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002162 if (!lq_sta->stay_in_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07002163 for (i = 0; i < IWL_RATE_COUNT; i++)
2164 rs_rate_scale_clear_window(&(tbl->win[i]));
2165 }
2166 }
2167}
2168
Ben Cahill77626352007-11-29 11:09:44 +08002169/*
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07002170 * setup rate table in uCode
2171 * return rate_n_flags as used in the table
2172 */
2173static u32 rs_update_rate_tbl(struct iwl_priv *priv,
Johannes Berg7e6a5882010-08-23 10:46:46 +02002174 struct iwl_rxon_context *ctx,
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07002175 struct iwl_lq_sta *lq_sta,
2176 struct iwl_scale_tbl_info *tbl,
2177 int index, u8 is_green)
2178{
2179 u32 rate;
2180
2181 /* Update uCode's rate table. */
2182 rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
2183 rs_fill_link_cmd(priv, lq_sta, rate);
Johannes Berg7e6a5882010-08-23 10:46:46 +02002184 iwl_send_lq_cmd(priv, ctx, &lq_sta->lq, CMD_ASYNC, false);
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07002185
2186 return rate;
2187}
2188
2189/*
Ben Cahill77626352007-11-29 11:09:44 +08002190 * Do rate scaling and search for new modulation mode.
2191 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002192static void rs_rate_scale_perform(struct iwl_priv *priv,
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002193 struct sk_buff *skb,
Johannes Berg4b7679a2008-09-18 18:14:18 +02002194 struct ieee80211_sta *sta,
2195 struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002196{
Johannes Berg4b7679a2008-09-18 18:14:18 +02002197 struct ieee80211_hw *hw = priv->hw;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002198 struct ieee80211_conf *conf = &hw->conf;
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002199 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2200 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002201 int low = IWL_RATE_INVALID;
2202 int high = IWL_RATE_INVALID;
2203 int index;
2204 int i;
Tomas Winklere227cea2008-07-18 13:53:05 +08002205 struct iwl_rate_scale_data *window = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002206 int current_tpt = IWL_INVALID_VALUE;
2207 int low_tpt = IWL_INVALID_VALUE;
2208 int high_tpt = IWL_INVALID_VALUE;
2209 u32 fail_count;
2210 s8 scale_action = 0;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002211 u16 rate_mask;
Zhu Yib481de92007-09-25 17:54:57 -07002212 u8 update_lq = 0;
Tomas Winklere227cea2008-07-18 13:53:05 +08002213 struct iwl_scale_tbl_info *tbl, *tbl1;
Zhu Yib481de92007-09-25 17:54:57 -07002214 u16 rate_scale_index_msk = 0;
Guy Cohen39e88502008-04-23 17:14:57 -07002215 u32 rate;
Zhu Yib481de92007-09-25 17:54:57 -07002216 u8 is_green = 0;
2217 u8 active_tbl = 0;
2218 u8 done_search = 0;
2219 u16 high_low;
Guy Cohena5e8b502008-05-29 16:35:11 +08002220 s32 sr;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002221 u8 tid = MAX_TID_COUNT;
Wey-Yi Guy86b47662009-03-10 14:35:09 -07002222 struct iwl_tid_data *tid_data;
Johannes Berg7e6a5882010-08-23 10:46:46 +02002223 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
2224 struct iwl_rxon_context *ctx = sta_priv->common.ctx;
Zhu Yib481de92007-09-25 17:54:57 -07002225
Tomas Winklere1623442009-01-27 14:27:56 -08002226 IWL_DEBUG_RATE(priv, "rate scale calculate new rate for skb\n");
Zhu Yib481de92007-09-25 17:54:57 -07002227
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002228 /* Send management frames and NO_ACK data using lowest rate. */
Tomas Winkler417f1142008-11-12 13:14:06 -08002229 /* TODO: this could probably be improved.. */
2230 if (!ieee80211_is_data(hdr->frame_control) ||
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002231 info->flags & IEEE80211_TX_CTL_NO_ACK)
Zhu Yib481de92007-09-25 17:54:57 -07002232 return;
Zhu Yib481de92007-09-25 17:54:57 -07002233
Johannes Berg4b7679a2008-09-18 18:14:18 +02002234 if (!sta || !lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002235 return;
2236
Johannes Berg4b7679a2008-09-18 18:14:18 +02002237 lq_sta->supp_rates = sta->supp_rates[lq_sta->band];
Zhu Yib481de92007-09-25 17:54:57 -07002238
Ron Rindjunskyfaa29712008-06-12 09:46:58 +08002239 tid = rs_tl_add_packet(lq_sta, hdr);
Daniel C Halperine3949d62009-09-17 10:43:50 -07002240 if ((tid != MAX_TID_COUNT) && (lq_sta->tx_agg_tid_en & (1 << tid))) {
2241 tid_data = &priv->stations[lq_sta->lq.sta_id].tid[tid];
2242 if (tid_data->agg.state == IWL_AGG_OFF)
2243 lq_sta->is_agg = 0;
2244 else
2245 lq_sta->is_agg = 1;
2246 } else
2247 lq_sta->is_agg = 0;
Ron Rindjunskyfaa29712008-06-12 09:46:58 +08002248
Ben Cahill77626352007-11-29 11:09:44 +08002249 /*
2250 * Select rate-scale / modulation-mode table to work with in
2251 * the rest of this function: "search" if searching for better
2252 * modulation mode, or "active" if doing rate scaling within a mode.
2253 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002254 if (!lq_sta->search_better_tbl)
2255 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002256 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002257 active_tbl = 1 - lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002258
Tomas Winklerc33104f2008-01-14 17:46:21 -08002259 tbl = &(lq_sta->lq_info[active_tbl]);
Wey-Yi Guy2681b202009-05-21 13:44:22 -07002260 if (is_legacy(tbl->lq_type))
2261 lq_sta->is_green = 0;
2262 else
Johannes Berg7e6a5882010-08-23 10:46:46 +02002263 lq_sta->is_green = rs_use_green(sta);
Tomas Winklerc33104f2008-01-14 17:46:21 -08002264 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07002265
Ben Cahill77626352007-11-29 11:09:44 +08002266 /* current tx rate */
Johannes Bergb7e35002008-09-11 02:22:58 +02002267 index = lq_sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002268
Tomas Winklere1623442009-01-27 14:27:56 -08002269 IWL_DEBUG_RATE(priv, "Rate scale index %d for type %d\n", index,
Zhu Yib481de92007-09-25 17:54:57 -07002270 tbl->lq_type);
2271
Ben Cahill77626352007-11-29 11:09:44 +08002272 /* rates available for this association, and for modulation mode */
Guy Coheneecd6e52008-04-23 17:14:58 -07002273 rate_mask = rs_get_supported_rates(lq_sta, hdr, tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -07002274
Frans Pop91dd6c22010-03-24 14:19:58 -07002275 IWL_DEBUG_RATE(priv, "mask 0x%04X\n", rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07002276
2277 /* mask with station rate restriction */
2278 if (is_legacy(tbl->lq_type)) {
Johannes Berg8318d782008-01-24 19:38:38 +01002279 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Ben Cahill77626352007-11-29 11:09:44 +08002280 /* supp_rates has no CCK bits in A mode */
Zhu Yib481de92007-09-25 17:54:57 -07002281 rate_scale_index_msk = (u16) (rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -08002282 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
Zhu Yib481de92007-09-25 17:54:57 -07002283 else
2284 rate_scale_index_msk = (u16) (rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -08002285 lq_sta->supp_rates);
Zhu Yib481de92007-09-25 17:54:57 -07002286
2287 } else
2288 rate_scale_index_msk = rate_mask;
2289
2290 if (!rate_scale_index_msk)
2291 rate_scale_index_msk = rate_mask;
2292
Guy Cohen07bc28e2008-04-23 17:15:03 -07002293 if (!((1 << index) & rate_scale_index_msk)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002294 IWL_ERR(priv, "Current Rate is not valid\n");
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07002295 if (lq_sta->search_better_tbl) {
2296 /* revert to active table if search table is not valid*/
2297 tbl->lq_type = LQ_NONE;
2298 lq_sta->search_better_tbl = 0;
2299 tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
2300 /* get "active" rate info */
2301 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
Johannes Berg7e6a5882010-08-23 10:46:46 +02002302 rate = rs_update_rate_tbl(priv, ctx, lq_sta,
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07002303 tbl, index, is_green);
2304 }
Guy Cohen07bc28e2008-04-23 17:15:03 -07002305 return;
Zhu Yib481de92007-09-25 17:54:57 -07002306 }
2307
Ben Cahill77626352007-11-29 11:09:44 +08002308 /* Get expected throughput table and history window for current rate */
Guy Cohen07bc28e2008-04-23 17:15:03 -07002309 if (!tbl->expected_tpt) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002310 IWL_ERR(priv, "tbl->expected_tpt is NULL\n");
Guy Cohen07bc28e2008-04-23 17:15:03 -07002311 return;
2312 }
Zhu Yib481de92007-09-25 17:54:57 -07002313
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002314 /* force user max rate if set by user */
2315 if ((lq_sta->max_rate_idx != -1) &&
2316 (lq_sta->max_rate_idx < index)) {
2317 index = lq_sta->max_rate_idx;
2318 update_lq = 1;
2319 window = &(tbl->win[index]);
2320 goto lq_update;
2321 }
2322
Zhu Yib481de92007-09-25 17:54:57 -07002323 window = &(tbl->win[index]);
2324
Ben Cahill77626352007-11-29 11:09:44 +08002325 /*
2326 * If there is not enough history to calculate actual average
2327 * throughput, keep analyzing results of more tx frames, without
2328 * changing rate or mode (bypass most of the rest of this function).
2329 * Set up new rate table in uCode only if old rate is not supported
2330 * in current association (use new rate found above).
2331 */
Zhu Yib481de92007-09-25 17:54:57 -07002332 fail_count = window->counter - window->success_counter;
Guy Cohen07bc28e2008-04-23 17:15:03 -07002333 if ((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
2334 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002335 IWL_DEBUG_RATE(priv, "LQ: still below TH. succ=%d total=%d "
Zhu Yib481de92007-09-25 17:54:57 -07002336 "for index %d\n",
2337 window->success_counter, window->counter, index);
Ben Cahill77626352007-11-29 11:09:44 +08002338
2339 /* Can't calculate this yet; not enough history */
Zhu Yib481de92007-09-25 17:54:57 -07002340 window->average_tpt = IWL_INVALID_VALUE;
Ben Cahill77626352007-11-29 11:09:44 +08002341
2342 /* Should we stay with this modulation mode,
2343 * or search for a new one? */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07002344 rs_stay_in_table(lq_sta, false);
Ben Cahill77626352007-11-29 11:09:44 +08002345
Zhu Yib481de92007-09-25 17:54:57 -07002346 goto out;
Guy Cohen3110bef2008-09-09 10:54:54 +08002347 }
Ben Cahill77626352007-11-29 11:09:44 +08002348 /* Else we have enough samples; calculate estimate of
2349 * actual average throughput */
Reinette Chatre3d79b2a2010-05-03 10:55:07 -07002350 if (window->average_tpt != ((window->success_ratio *
2351 tbl->expected_tpt[index] + 64) / 128)) {
2352 IWL_ERR(priv, "expected_tpt should have been calculated by now\n");
2353 window->average_tpt = ((window->success_ratio *
2354 tbl->expected_tpt[index] + 64) / 128);
2355 }
Zhu Yib481de92007-09-25 17:54:57 -07002356
Ben Cahill77626352007-11-29 11:09:44 +08002357 /* If we are searching for better modulation mode, check success. */
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002358 if (lq_sta->search_better_tbl &&
Johannes Berg3ad3b922009-08-07 15:41:48 -07002359 (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_MULTI)) {
Ben Cahill77626352007-11-29 11:09:44 +08002360 /* If good success, continue using the "search" mode;
2361 * no need to send new link quality command, since we're
2362 * continuing to use the setup that we've been trying. */
Guy Cohen07bc28e2008-04-23 17:15:03 -07002363 if (window->average_tpt > lq_sta->last_tpt) {
2364
Tomas Winklere1623442009-01-27 14:27:56 -08002365 IWL_DEBUG_RATE(priv, "LQ: SWITCHING TO NEW TABLE "
Guy Cohen07bc28e2008-04-23 17:15:03 -07002366 "suc=%d cur-tpt=%d old-tpt=%d\n",
2367 window->success_ratio,
2368 window->average_tpt,
2369 lq_sta->last_tpt);
2370
2371 if (!is_legacy(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08002372 lq_sta->enable_counter = 1;
Guy Cohen07bc28e2008-04-23 17:15:03 -07002373
Ben Cahill77626352007-11-29 11:09:44 +08002374 /* Swap tables; "search" becomes "active" */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002375 lq_sta->active_tbl = active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002376 current_tpt = window->average_tpt;
Ben Cahill77626352007-11-29 11:09:44 +08002377
2378 /* Else poor success; go back to mode in "active" table */
Zhu Yib481de92007-09-25 17:54:57 -07002379 } else {
Guy Cohen07bc28e2008-04-23 17:15:03 -07002380
Tomas Winklere1623442009-01-27 14:27:56 -08002381 IWL_DEBUG_RATE(priv, "LQ: GOING BACK TO THE OLD TABLE "
Guy Cohen07bc28e2008-04-23 17:15:03 -07002382 "suc=%d cur-tpt=%d old-tpt=%d\n",
2383 window->success_ratio,
2384 window->average_tpt,
2385 lq_sta->last_tpt);
2386
Ben Cahill77626352007-11-29 11:09:44 +08002387 /* Nullify "search" table */
Zhu Yib481de92007-09-25 17:54:57 -07002388 tbl->lq_type = LQ_NONE;
Ben Cahill77626352007-11-29 11:09:44 +08002389
2390 /* Revert to "active" table */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002391 active_tbl = lq_sta->active_tbl;
2392 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07002393
Ben Cahill77626352007-11-29 11:09:44 +08002394 /* Revert to "active" rate and throughput info */
Tomas Winklere7d326ac2008-06-12 09:47:11 +08002395 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
Tomas Winklerc33104f2008-01-14 17:46:21 -08002396 current_tpt = lq_sta->last_tpt;
Ben Cahill77626352007-11-29 11:09:44 +08002397
2398 /* Need to set up a new rate table in uCode */
2399 update_lq = 1;
Zhu Yib481de92007-09-25 17:54:57 -07002400 }
Ben Cahill77626352007-11-29 11:09:44 +08002401
2402 /* Either way, we've made a decision; modulation mode
2403 * search is done, allow rate adjustment next time. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002404 lq_sta->search_better_tbl = 0;
Ben Cahill77626352007-11-29 11:09:44 +08002405 done_search = 1; /* Don't switch modes below! */
Zhu Yib481de92007-09-25 17:54:57 -07002406 goto lq_update;
2407 }
2408
Ben Cahill77626352007-11-29 11:09:44 +08002409 /* (Else) not in search of better modulation mode, try for better
2410 * starting rate, while staying in this mode. */
Ester Kummerbf403db2008-05-05 10:22:40 +08002411 high_low = rs_get_adjacent_rate(priv, index, rate_scale_index_msk,
Zhu Yib481de92007-09-25 17:54:57 -07002412 tbl->lq_type);
2413 low = high_low & 0xff;
2414 high = (high_low >> 8) & 0xff;
2415
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002416 /* If user set max rate, dont allow higher than user constrain */
2417 if ((lq_sta->max_rate_idx != -1) &&
2418 (lq_sta->max_rate_idx < high))
2419 high = IWL_RATE_INVALID;
2420
Guy Cohena5e8b502008-05-29 16:35:11 +08002421 sr = window->success_ratio;
2422
Ben Cahill77626352007-11-29 11:09:44 +08002423 /* Collect measured throughputs for current and adjacent rates */
Zhu Yib481de92007-09-25 17:54:57 -07002424 current_tpt = window->average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07002425 if (low != IWL_RATE_INVALID)
2426 low_tpt = tbl->win[low].average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07002427 if (high != IWL_RATE_INVALID)
2428 high_tpt = tbl->win[high].average_tpt;
2429
Guy Cohena5e8b502008-05-29 16:35:11 +08002430 scale_action = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002431
Ben Cahill77626352007-11-29 11:09:44 +08002432 /* Too many failures, decrease rate */
Guy Cohena5e8b502008-05-29 16:35:11 +08002433 if ((sr <= IWL_RATE_DECREASE_TH) || (current_tpt == 0)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002434 IWL_DEBUG_RATE(priv, "decrease rate because of low success_ratio\n");
Zhu Yib481de92007-09-25 17:54:57 -07002435 scale_action = -1;
Ben Cahill77626352007-11-29 11:09:44 +08002436
2437 /* No throughput measured yet for adjacent rates; try increase. */
Zhu Yib481de92007-09-25 17:54:57 -07002438 } else if ((low_tpt == IWL_INVALID_VALUE) &&
Guy Cohena5e8b502008-05-29 16:35:11 +08002439 (high_tpt == IWL_INVALID_VALUE)) {
2440
2441 if (high != IWL_RATE_INVALID && sr >= IWL_RATE_INCREASE_TH)
2442 scale_action = 1;
2443 else if (low != IWL_RATE_INVALID)
Wey-Yi Guy8fe72312009-03-10 14:35:10 -07002444 scale_action = 0;
Guy Cohena5e8b502008-05-29 16:35:11 +08002445 }
Ben Cahill77626352007-11-29 11:09:44 +08002446
2447 /* Both adjacent throughputs are measured, but neither one has better
2448 * throughput; we're using the best rate, don't change it! */
Zhu Yib481de92007-09-25 17:54:57 -07002449 else if ((low_tpt != IWL_INVALID_VALUE) &&
2450 (high_tpt != IWL_INVALID_VALUE) &&
2451 (low_tpt < current_tpt) &&
2452 (high_tpt < current_tpt))
2453 scale_action = 0;
Ben Cahill77626352007-11-29 11:09:44 +08002454
2455 /* At least one adjacent rate's throughput is measured,
2456 * and may have better performance. */
Zhu Yib481de92007-09-25 17:54:57 -07002457 else {
Ben Cahill77626352007-11-29 11:09:44 +08002458 /* Higher adjacent rate's throughput is measured */
Zhu Yib481de92007-09-25 17:54:57 -07002459 if (high_tpt != IWL_INVALID_VALUE) {
Ben Cahill77626352007-11-29 11:09:44 +08002460 /* Higher rate has better throughput */
Guy Cohena5e8b502008-05-29 16:35:11 +08002461 if (high_tpt > current_tpt &&
2462 sr >= IWL_RATE_INCREASE_TH) {
Zhu Yib481de92007-09-25 17:54:57 -07002463 scale_action = 1;
Guy Cohena5e8b502008-05-29 16:35:11 +08002464 } else {
Wey-Yi Guy8fe72312009-03-10 14:35:10 -07002465 scale_action = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002466 }
Ben Cahill77626352007-11-29 11:09:44 +08002467
2468 /* Lower adjacent rate's throughput is measured */
Zhu Yib481de92007-09-25 17:54:57 -07002469 } else if (low_tpt != IWL_INVALID_VALUE) {
Ben Cahill77626352007-11-29 11:09:44 +08002470 /* Lower rate has better throughput */
Zhu Yib481de92007-09-25 17:54:57 -07002471 if (low_tpt > current_tpt) {
Tomas Winklere1623442009-01-27 14:27:56 -08002472 IWL_DEBUG_RATE(priv,
2473 "decrease rate because of low tpt\n");
Zhu Yib481de92007-09-25 17:54:57 -07002474 scale_action = -1;
Guy Cohena5e8b502008-05-29 16:35:11 +08002475 } else if (sr >= IWL_RATE_INCREASE_TH) {
Zhu Yib481de92007-09-25 17:54:57 -07002476 scale_action = 1;
Guy Cohena5e8b502008-05-29 16:35:11 +08002477 }
Zhu Yib481de92007-09-25 17:54:57 -07002478 }
2479 }
2480
Ben Cahill77626352007-11-29 11:09:44 +08002481 /* Sanity check; asked for decrease, but success rate or throughput
2482 * has been good at old rate. Don't change it. */
Guy Cohena5e8b502008-05-29 16:35:11 +08002483 if ((scale_action == -1) && (low != IWL_RATE_INVALID) &&
2484 ((sr > IWL_RATE_HIGH_TH) ||
Zhu Yib481de92007-09-25 17:54:57 -07002485 (current_tpt > (100 * tbl->expected_tpt[low]))))
Zhu Yib481de92007-09-25 17:54:57 -07002486 scale_action = 0;
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002487 if (!iwl_ht_enabled(priv) && !is_legacy(tbl->lq_type))
2488 scale_action = -1;
Johannes Berg3ad3b922009-08-07 15:41:48 -07002489 if (iwl_tx_ant_restriction(priv) != IWL_ANT_OK_MULTI &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002490 (is_mimo2(tbl->lq_type) || is_mimo3(tbl->lq_type)))
2491 scale_action = -1;
Johannes Berg290f5992010-08-23 07:56:58 -07002492
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07002493 if ((priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH) &&
2494 (is_mimo2(tbl->lq_type) || is_mimo3(tbl->lq_type))) {
2495 if (lq_sta->last_bt_traffic > priv->bt_traffic_load) {
2496 /*
2497 * don't set scale_action, don't want to scale up if
2498 * the rate scale doesn't otherwise think that is a
2499 * good idea.
2500 */
2501 } else if (lq_sta->last_bt_traffic <= priv->bt_traffic_load) {
2502 scale_action = -1;
2503 }
2504 }
2505 lq_sta->last_bt_traffic = priv->bt_traffic_load;
2506
2507 if ((priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH) &&
2508 (is_mimo2(tbl->lq_type) || is_mimo3(tbl->lq_type))) {
2509 /* search for a new modulation */
2510 rs_stay_in_table(lq_sta, true);
2511 goto lq_update;
Johannes Berg290f5992010-08-23 07:56:58 -07002512 }
2513
Zhu Yib481de92007-09-25 17:54:57 -07002514 switch (scale_action) {
2515 case -1:
Ben Cahill77626352007-11-29 11:09:44 +08002516 /* Decrease starting rate, update uCode's rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002517 if (low != IWL_RATE_INVALID) {
2518 update_lq = 1;
2519 index = low;
2520 }
Mohamed Abbas447fee72009-04-20 14:37:02 -07002521
Zhu Yib481de92007-09-25 17:54:57 -07002522 break;
2523 case 1:
Ben Cahill77626352007-11-29 11:09:44 +08002524 /* Increase starting rate, update uCode's rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002525 if (high != IWL_RATE_INVALID) {
2526 update_lq = 1;
2527 index = high;
2528 }
2529
2530 break;
2531 case 0:
Ben Cahill77626352007-11-29 11:09:44 +08002532 /* No change */
Zhu Yib481de92007-09-25 17:54:57 -07002533 default:
2534 break;
2535 }
2536
Tomas Winklere1623442009-01-27 14:27:56 -08002537 IWL_DEBUG_RATE(priv, "choose rate scale index %d action %d low %d "
Zhu Yib481de92007-09-25 17:54:57 -07002538 "high %d type %d\n",
2539 index, scale_action, low, high, tbl->lq_type);
2540
Guy Cohena5e8b502008-05-29 16:35:11 +08002541lq_update:
Ben Cahill77626352007-11-29 11:09:44 +08002542 /* Replace uCode's rate table for the destination station. */
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07002543 if (update_lq)
Johannes Berg7e6a5882010-08-23 10:46:46 +02002544 rate = rs_update_rate_tbl(priv, ctx, lq_sta,
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07002545 tbl, index, is_green);
Ben Cahill77626352007-11-29 11:09:44 +08002546
Johannes Berg3ad3b922009-08-07 15:41:48 -07002547 if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_MULTI) {
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002548 /* Should we stay with this modulation mode,
2549 * or search for a new one? */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07002550 rs_stay_in_table(lq_sta, false);
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002551 }
Ben Cahill77626352007-11-29 11:09:44 +08002552 /*
2553 * Search for new modulation mode if we're:
2554 * 1) Not changing rates right now
2555 * 2) Not just finishing up a search
2556 * 3) Allowing a new search
2557 */
Guy Cohen47cfd462008-05-27 11:29:34 +08002558 if (!update_lq && !done_search && !lq_sta->stay_in_tbl && window->counter) {
Ben Cahill77626352007-11-29 11:09:44 +08002559 /* Save current throughput to compare with "search" throughput*/
Tomas Winklerc33104f2008-01-14 17:46:21 -08002560 lq_sta->last_tpt = current_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07002561
Ben Cahill77626352007-11-29 11:09:44 +08002562 /* Select a new "search" modulation mode to try.
2563 * If one is found, set up the new "search" table. */
Zhu Yib481de92007-09-25 17:54:57 -07002564 if (is_legacy(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08002565 rs_move_legacy_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07002566 else if (is_siso(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08002567 rs_move_siso_to_other(priv, lq_sta, conf, sta, index);
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07002568 else if (is_mimo2(tbl->lq_type))
2569 rs_move_mimo2_to_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07002570 else
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07002571 rs_move_mimo3_to_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07002572
Ben Cahill77626352007-11-29 11:09:44 +08002573 /* If new "search" mode was selected, set up in uCode table */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002574 if (lq_sta->search_better_tbl) {
Ben Cahill77626352007-11-29 11:09:44 +08002575 /* Access the "search" table, clear its history. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002576 tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Zhu Yib481de92007-09-25 17:54:57 -07002577 for (i = 0; i < IWL_RATE_COUNT; i++)
2578 rs_rate_scale_clear_window(&(tbl->win[i]));
2579
Ben Cahill77626352007-11-29 11:09:44 +08002580 /* Use new "search" start rate */
Tomas Winklere7d326ac2008-06-12 09:47:11 +08002581 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002582
Tomas Winklere1623442009-01-27 14:27:56 -08002583 IWL_DEBUG_RATE(priv, "Switch current mcs: %X index: %d\n",
Guy Cohen39e88502008-04-23 17:14:57 -07002584 tbl->current_rate, index);
Guy Cohen07bc28e2008-04-23 17:15:03 -07002585 rs_fill_link_cmd(priv, lq_sta, tbl->current_rate);
Johannes Berg7e6a5882010-08-23 10:46:46 +02002586 iwl_send_lq_cmd(priv, ctx, &lq_sta->lq, CMD_ASYNC, false);
Mohamed Abbas447fee72009-04-20 14:37:02 -07002587 } else
2588 done_search = 1;
2589 }
Zhu Yib481de92007-09-25 17:54:57 -07002590
Mohamed Abbas447fee72009-04-20 14:37:02 -07002591 if (done_search && !lq_sta->stay_in_tbl) {
Ben Cahill77626352007-11-29 11:09:44 +08002592 /* If the "active" (non-search) mode was legacy,
2593 * and we've tried switching antennas,
2594 * but we haven't been able to try HT modes (not available),
2595 * stay with best antenna legacy modulation for a while
2596 * before next round of mode comparisons. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002597 tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
Luis R. Rodriguezde27e642008-12-23 15:58:44 -08002598 if (is_legacy(tbl1->lq_type) && !conf_is_ht(conf) &&
Mohamed Abbasd6e93392009-05-08 13:44:39 -07002599 lq_sta->action_counter > tbl1->max_search) {
Tomas Winklere1623442009-01-27 14:27:56 -08002600 IWL_DEBUG_RATE(priv, "LQ: STAY in legacy table\n");
Ester Kummerbf403db2008-05-05 10:22:40 +08002601 rs_set_stay_in_table(priv, 1, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002602 }
2603
Ben Cahill77626352007-11-29 11:09:44 +08002604 /* If we're in an HT mode, and all 3 mode switch actions
2605 * have been tried and compared, stay in this best modulation
2606 * mode for a while before next round of mode comparisons. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002607 if (lq_sta->enable_counter &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002608 (lq_sta->action_counter >= tbl1->max_search) &&
2609 iwl_ht_enabled(priv)) {
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002610 if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
2611 (lq_sta->tx_agg_tid_en & (1 << tid)) &&
2612 (tid != MAX_TID_COUNT)) {
Wey-Yi Guy86b47662009-03-10 14:35:09 -07002613 tid_data =
2614 &priv->stations[lq_sta->lq.sta_id].tid[tid];
2615 if (tid_data->agg.state == IWL_AGG_OFF) {
2616 IWL_DEBUG_RATE(priv,
2617 "try to aggregate tid %d\n",
2618 tid);
2619 rs_tl_turn_on_agg(priv, tid,
2620 lq_sta, sta);
2621 }
Zhu Yib481de92007-09-25 17:54:57 -07002622 }
Ester Kummerbf403db2008-05-05 10:22:40 +08002623 rs_set_stay_in_table(priv, 0, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002624 }
Zhu Yib481de92007-09-25 17:54:57 -07002625 }
2626
2627out:
Tomas Winkler978785a2008-12-19 10:37:31 +08002628 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
Zhu Yib481de92007-09-25 17:54:57 -07002629 i = index;
Johannes Bergb7e35002008-09-11 02:22:58 +02002630 lq_sta->last_txrate_idx = i;
Zhu Yib481de92007-09-25 17:54:57 -07002631}
2632
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08002633/**
2634 * rs_initialize_lq - Initialize a station's hardware rate table
2635 *
2636 * The uCode's station table contains a table of fallback rates
2637 * for automatic fallback during transmission.
2638 *
2639 * NOTE: This sets up a default set of values. These will be replaced later
2640 * if the driver's iwl-agn-rs rate scaling algorithm is used, instead of
2641 * rc80211_simple.
2642 *
2643 * NOTE: Run REPLY_ADD_STA command to set up station table entry, before
2644 * calling this function (which runs REPLY_TX_LINK_QUALITY_CMD,
2645 * which requires station table entry to exist).
2646 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002647static void rs_initialize_lq(struct iwl_priv *priv,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002648 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02002649 struct ieee80211_sta *sta,
2650 struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002651{
Tomas Winklere227cea2008-07-18 13:53:05 +08002652 struct iwl_scale_tbl_info *tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002653 int rate_idx;
Guy Cohenaade00c2008-04-23 17:14:59 -07002654 int i;
Guy Cohen39e88502008-04-23 17:14:57 -07002655 u32 rate;
Johannes Berg7e6a5882010-08-23 10:46:46 +02002656 u8 use_green = rs_use_green(sta);
Guy Cohenaade00c2008-04-23 17:14:59 -07002657 u8 active_tbl = 0;
2658 u8 valid_tx_ant;
Johannes Berg7e6a5882010-08-23 10:46:46 +02002659 struct iwl_station_priv *sta_priv;
2660 struct iwl_rxon_context *ctx;
Zhu Yib481de92007-09-25 17:54:57 -07002661
Johannes Berg4b7679a2008-09-18 18:14:18 +02002662 if (!sta || !lq_sta)
Johannes Berg7e6a5882010-08-23 10:46:46 +02002663 return;
2664
2665 sta_priv = (void *)sta->drv_priv;
2666 ctx = sta_priv->common.ctx;
Zhu Yib481de92007-09-25 17:54:57 -07002667
Johannes Bergb7e35002008-09-11 02:22:58 +02002668 i = lq_sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002669
Guy Cohenaade00c2008-04-23 17:14:59 -07002670 valid_tx_ant = priv->hw_params.valid_tx_ant;
2671
Tomas Winklerc33104f2008-01-14 17:46:21 -08002672 if (!lq_sta->search_better_tbl)
2673 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002674 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002675 active_tbl = 1 - lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002676
Tomas Winklerc33104f2008-01-14 17:46:21 -08002677 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07002678
2679 if ((i < 0) || (i >= IWL_RATE_COUNT))
2680 i = 0;
2681
Tomas Winkler1826dcc2008-05-15 13:54:02 +08002682 rate = iwl_rates[i].plcp;
Winkler, Tomaseb48dca2008-10-29 14:05:48 -07002683 tbl->ant_type = first_antenna(valid_tx_ant);
2684 rate |= tbl->ant_type << RATE_MCS_ANT_POS;
Zhu Yib481de92007-09-25 17:54:57 -07002685
2686 if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
Guy Cohen39e88502008-04-23 17:14:57 -07002687 rate |= RATE_MCS_CCK_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002688
Guy Cohen39e88502008-04-23 17:14:57 -07002689 rs_get_tbl_info_from_mcs(rate, priv->band, tbl, &rate_idx);
Guy Cohenaade00c2008-04-23 17:14:59 -07002690 if (!rs_is_valid_ant(valid_tx_ant, tbl->ant_type))
2691 rs_toggle_antenna(valid_tx_ant, &rate, tbl);
Zhu Yib481de92007-09-25 17:54:57 -07002692
Tomas Winkler978785a2008-12-19 10:37:31 +08002693 rate = rate_n_flags_from_tbl(priv, tbl, rate_idx, use_green);
Guy Cohen39e88502008-04-23 17:14:57 -07002694 tbl->current_rate = rate;
Guy Coheneecd6e52008-04-23 17:14:58 -07002695 rs_set_expected_tpt_table(lq_sta, tbl);
Guy Cohen07bc28e2008-04-23 17:15:03 -07002696 rs_fill_link_cmd(NULL, lq_sta, rate);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08002697 priv->stations[lq_sta->lq.sta_id].lq = &lq_sta->lq;
Johannes Berg7e6a5882010-08-23 10:46:46 +02002698 iwl_send_lq_cmd(priv, ctx, &lq_sta->lq, CMD_SYNC, true);
Zhu Yib481de92007-09-25 17:54:57 -07002699}
2700
Johannes Berge6a98542008-10-21 12:40:02 +02002701static void rs_get_rate(void *priv_r, struct ieee80211_sta *sta, void *priv_sta,
2702 struct ieee80211_tx_rate_control *txrc)
Zhu Yib481de92007-09-25 17:54:57 -07002703{
2704
Johannes Berge6a98542008-10-21 12:40:02 +02002705 struct sk_buff *skb = txrc->skb;
2706 struct ieee80211_supported_band *sband = txrc->sband;
Reinette Chatref875f512010-04-05 10:43:10 -07002707 struct iwl_priv *priv __maybe_unused = (struct iwl_priv *)priv_r;
Johannes Berge6a98542008-10-21 12:40:02 +02002708 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Tomas Winkler417f1142008-11-12 13:14:06 -08002709 struct iwl_lq_sta *lq_sta = priv_sta;
2710 int rate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002711
Tomas Winklere1623442009-01-27 14:27:56 -08002712 IWL_DEBUG_RATE_LIMIT(priv, "rate scale calculate new rate for skb\n");
Zhu Yib481de92007-09-25 17:54:57 -07002713
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002714 /* Get max rate if user set max rate */
2715 if (lq_sta) {
2716 lq_sta->max_rate_idx = txrc->max_rate_idx;
2717 if ((sband->band == IEEE80211_BAND_5GHZ) &&
2718 (lq_sta->max_rate_idx != -1))
2719 lq_sta->max_rate_idx += IWL_FIRST_OFDM_RATE;
2720 if ((lq_sta->max_rate_idx < 0) ||
2721 (lq_sta->max_rate_idx >= IWL_RATE_COUNT))
2722 lq_sta->max_rate_idx = -1;
2723 }
2724
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08002725 /* Treat uninitialized rate scaling data same as non-existing. */
2726 if (lq_sta && !lq_sta->drv) {
2727 IWL_DEBUG_RATE(priv, "Rate scaling not initialized yet.\n");
2728 priv_sta = NULL;
2729 }
2730
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002731 /* Send management frames and NO_ACK data using lowest rate. */
Luis R. Rodriguez4c6d4f52009-07-16 10:05:41 -07002732 if (rate_control_send_low(sta, priv_sta, txrc))
Johannes Berg4b7679a2008-09-18 18:14:18 +02002733 return;
Zhu Yib481de92007-09-25 17:54:57 -07002734
Tomas Winkler417f1142008-11-12 13:14:06 -08002735 rate_idx = lq_sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002736
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002737 if (lq_sta->last_rate_n_flags & RATE_MCS_HT_MSK) {
Tomas Winkler417f1142008-11-12 13:14:06 -08002738 rate_idx -= IWL_FIRST_OFDM_RATE;
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002739 /* 6M and 9M shared same MCS index */
2740 rate_idx = (rate_idx > 0) ? (rate_idx - 1) : 0;
2741 if (rs_extract_rate(lq_sta->last_rate_n_flags) >=
2742 IWL_RATE_MIMO3_6M_PLCP)
2743 rate_idx = rate_idx + (2 * MCS_INDEX_PER_STREAM);
2744 else if (rs_extract_rate(lq_sta->last_rate_n_flags) >=
2745 IWL_RATE_MIMO2_6M_PLCP)
2746 rate_idx = rate_idx + MCS_INDEX_PER_STREAM;
2747 info->control.rates[0].flags = IEEE80211_TX_RC_MCS;
2748 if (lq_sta->last_rate_n_flags & RATE_MCS_SGI_MSK)
2749 info->control.rates[0].flags |= IEEE80211_TX_RC_SHORT_GI;
2750 if (lq_sta->last_rate_n_flags & RATE_MCS_DUP_MSK)
2751 info->control.rates[0].flags |= IEEE80211_TX_RC_DUP_DATA;
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07002752 if (lq_sta->last_rate_n_flags & RATE_MCS_HT40_MSK)
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002753 info->control.rates[0].flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
2754 if (lq_sta->last_rate_n_flags & RATE_MCS_GF_MSK)
2755 info->control.rates[0].flags |= IEEE80211_TX_RC_GREEN_FIELD;
2756 } else {
Daniel C Halperin50273092009-08-28 09:44:45 -07002757 /* Check for invalid rates */
2758 if ((rate_idx < 0) || (rate_idx >= IWL_RATE_COUNT_LEGACY) ||
2759 ((sband->band == IEEE80211_BAND_5GHZ) &&
2760 (rate_idx < IWL_FIRST_OFDM_RATE)))
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002761 rate_idx = rate_lowest_index(sband, sta);
Daniel C Halperin50273092009-08-28 09:44:45 -07002762 /* On valid 5 GHz rate, adjust index */
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002763 else if (sband->band == IEEE80211_BAND_5GHZ)
2764 rate_idx -= IWL_FIRST_OFDM_RATE;
Daniel C Halperin7ebaeff2009-08-21 13:34:20 -07002765 info->control.rates[0].flags = 0;
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002766 }
Tomas Winkler417f1142008-11-12 13:14:06 -08002767 info->control.rates[0].idx = rate_idx;
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002768
Zhu Yib481de92007-09-25 17:54:57 -07002769}
2770
Johannes Berg4b7679a2008-09-18 18:14:18 +02002771static void *rs_alloc_sta(void *priv_rate, struct ieee80211_sta *sta,
2772 gfp_t gfp)
Zhu Yib481de92007-09-25 17:54:57 -07002773{
Reinette Chatre8d9698b2009-10-16 14:25:55 -07002774 struct iwl_station_priv *sta_priv = (struct iwl_station_priv *) sta->drv_priv;
Ester Kummerbf403db2008-05-05 10:22:40 +08002775 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07002776
Ester Kummerbf403db2008-05-05 10:22:40 +08002777 priv = (struct iwl_priv *)priv_rate;
Tomas Winklere1623442009-01-27 14:27:56 -08002778 IWL_DEBUG_RATE(priv, "create station rate scale window\n");
Zhu Yib481de92007-09-25 17:54:57 -07002779
Johannes Berg08960de2011-04-05 09:41:53 -07002780 return &sta_priv->lq_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002781}
2782
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08002783/*
2784 * Called after adding a new station to initialize rate scaling
2785 */
2786void iwl_rs_rate_init(struct iwl_priv *priv, struct ieee80211_sta *sta, u8 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07002787{
2788 int i, j;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08002789 struct ieee80211_hw *hw = priv->hw;
Johannes Berg4b7679a2008-09-18 18:14:18 +02002790 struct ieee80211_conf *conf = &priv->hw->conf;
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002791 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08002792 struct iwl_station_priv *sta_priv;
2793 struct iwl_lq_sta *lq_sta;
2794 struct ieee80211_supported_band *sband;
Zhu Yib481de92007-09-25 17:54:57 -07002795
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08002796 sta_priv = (struct iwl_station_priv *) sta->drv_priv;
2797 lq_sta = &sta_priv->lq_sta;
2798 sband = hw->wiphy->bands[conf->channel->band];
2799
2800
2801 lq_sta->lq.sta_id = sta_id;
Reinette Chatre5ad13f82009-10-30 14:36:14 -07002802
2803 for (j = 0; j < LQ_SIZE; j++)
2804 for (i = 0; i < IWL_RATE_COUNT; i++)
2805 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
2806
Tomas Winklerc33104f2008-01-14 17:46:21 -08002807 lq_sta->flush_timer = 0;
Johannes Berg4b7679a2008-09-18 18:14:18 +02002808 lq_sta->supp_rates = sta->supp_rates[sband->band];
Zhu Yib481de92007-09-25 17:54:57 -07002809 for (j = 0; j < LQ_SIZE; j++)
2810 for (i = 0; i < IWL_RATE_COUNT; i++)
Tomas Winkler3ac7f142008-07-21 02:40:14 +03002811 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
Zhu Yib481de92007-09-25 17:54:57 -07002812
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08002813 IWL_DEBUG_RATE(priv, "LQ: *** rate scale station global init for station %d ***\n",
2814 sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07002815 /* TODO: what is a good starting rate for STA? About middle? Maybe not
2816 * the lowest or the highest rate.. Could consider using RSSI from
2817 * previous packets? Need to have IEEE 802.1X auth succeed immediately
2818 * after assoc.. */
2819
Tomas Winklerc33104f2008-01-14 17:46:21 -08002820 lq_sta->is_dup = 0;
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002821 lq_sta->max_rate_idx = -1;
Abbas, Mohamed7d049e52009-01-20 21:33:53 -08002822 lq_sta->missed_rate_counter = IWL_MISSED_RATE_MAX;
Johannes Berg7e6a5882010-08-23 10:46:46 +02002823 lq_sta->is_green = rs_use_green(sta);
Guy Cohen39e88502008-04-23 17:14:57 -07002824 lq_sta->active_legacy_rate = priv->active_rate & ~(0x1000);
Johannes Berg8318d782008-01-24 19:38:38 +01002825 lq_sta->band = priv->band;
Ben Cahill77626352007-11-29 11:09:44 +08002826 /*
2827 * active_siso_rate mask includes 9 MBits (bit 5), and CCK (bits 0-3),
2828 * supp_rates[] does not; shift to convert format, force 9 MBits off.
2829 */
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002830 lq_sta->active_siso_rate = ht_cap->mcs.rx_mask[0] << 1;
2831 lq_sta->active_siso_rate |= ht_cap->mcs.rx_mask[0] & 0x1;
Guy Cohenfde0db32008-04-21 15:42:01 -07002832 lq_sta->active_siso_rate &= ~((u16)0x2);
2833 lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -07002834
Ben Cahill77626352007-11-29 11:09:44 +08002835 /* Same here */
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002836 lq_sta->active_mimo2_rate = ht_cap->mcs.rx_mask[1] << 1;
2837 lq_sta->active_mimo2_rate |= ht_cap->mcs.rx_mask[1] & 0x1;
Guy Cohenfde0db32008-04-21 15:42:01 -07002838 lq_sta->active_mimo2_rate &= ~((u16)0x2);
2839 lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE;
2840
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002841 lq_sta->active_mimo3_rate = ht_cap->mcs.rx_mask[2] << 1;
2842 lq_sta->active_mimo3_rate |= ht_cap->mcs.rx_mask[2] & 0x1;
Guy Cohenfde0db32008-04-21 15:42:01 -07002843 lq_sta->active_mimo3_rate &= ~((u16)0x2);
2844 lq_sta->active_mimo3_rate <<= IWL_FIRST_OFDM_RATE;
2845
Tomas Winklere1623442009-01-27 14:27:56 -08002846 IWL_DEBUG_RATE(priv, "SISO-RATE=%X MIMO2-RATE=%X MIMO3-RATE=%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002847 lq_sta->active_siso_rate,
Guy Cohenfde0db32008-04-21 15:42:01 -07002848 lq_sta->active_mimo2_rate,
2849 lq_sta->active_mimo3_rate);
2850
Tomas Winklera96a27f2008-10-23 23:48:56 -07002851 /* These values will be overridden later */
Wey-Yi Guy3a23d692010-04-03 16:44:39 -07002852 lq_sta->lq.general_params.single_stream_ant_msk =
2853 first_antenna(priv->hw_params.valid_tx_ant);
2854 lq_sta->lq.general_params.dual_stream_ant_msk =
2855 priv->hw_params.valid_tx_ant &
2856 ~first_antenna(priv->hw_params.valid_tx_ant);
2857 if (!lq_sta->lq.general_params.dual_stream_ant_msk) {
2858 lq_sta->lq.general_params.dual_stream_ant_msk = ANT_AB;
2859 } else if (num_of_ant(priv->hw_params.valid_tx_ant) == 2) {
2860 lq_sta->lq.general_params.dual_stream_ant_msk =
2861 priv->hw_params.valid_tx_ant;
2862 }
Guy Cohen07bc28e2008-04-23 17:15:03 -07002863
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002864 /* as default allow aggregation for all tids */
2865 lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002866 lq_sta->drv = priv;
Zhu Yib481de92007-09-25 17:54:57 -07002867
Daniel C Halperin50273092009-08-28 09:44:45 -07002868 /* Set last_txrate_idx to lowest rate */
2869 lq_sta->last_txrate_idx = rate_lowest_index(sband, sta);
2870 if (sband->band == IEEE80211_BAND_5GHZ)
2871 lq_sta->last_txrate_idx += IWL_FIRST_OFDM_RATE;
Daniel C Halperine3949d62009-09-17 10:43:50 -07002872 lq_sta->is_agg = 0;
Mohamed Abbasd5e49032008-12-12 08:22:15 -08002873
Sedat Dilek7d5f01a2010-12-16 12:46:23 +01002874#ifdef CONFIG_MAC80211_DEBUGFS
Shanyu Zhaoa1da0772010-12-02 11:02:54 -08002875 lq_sta->dbg_fixed_rate = 0;
Sedat Dilek7d5f01a2010-12-16 12:46:23 +01002876#endif
Shanyu Zhaoa1da0772010-12-02 11:02:54 -08002877
Johannes Berg4b7679a2008-09-18 18:14:18 +02002878 rs_initialize_lq(priv, conf, sta, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002879}
2880
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002881static void rs_fill_link_cmd(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08002882 struct iwl_lq_sta *lq_sta, u32 new_rate)
Zhu Yib481de92007-09-25 17:54:57 -07002883{
Tomas Winklere227cea2008-07-18 13:53:05 +08002884 struct iwl_scale_tbl_info tbl_type;
Zhu Yib481de92007-09-25 17:54:57 -07002885 int index = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002886 int rate_idx;
Zhu Yi1b696de2007-09-27 11:27:41 +08002887 int repeat_rate = 0;
Guy Cohenaade00c2008-04-23 17:14:59 -07002888 u8 ant_toggle_cnt = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002889 u8 use_ht_possible = 1;
Guy Cohenaade00c2008-04-23 17:14:59 -07002890 u8 valid_tx_ant = 0;
Johannes Berg7b090682011-01-19 02:53:54 -08002891 struct iwl_station_priv *sta_priv =
2892 container_of(lq_sta, struct iwl_station_priv, lq_sta);
Guy Cohen07bc28e2008-04-23 17:15:03 -07002893 struct iwl_link_quality_cmd *lq_cmd = &lq_sta->lq;
Zhu Yib481de92007-09-25 17:54:57 -07002894
Ben Cahill77626352007-11-29 11:09:44 +08002895 /* Override starting rate (index 0) if needed for debug purposes */
Guy Cohen39e88502008-04-23 17:14:57 -07002896 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Zhu Yi98d7e092007-09-27 11:27:42 +08002897
Guy Cohen39e88502008-04-23 17:14:57 -07002898 /* Interpret new_rate (rate_n_flags) */
2899 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band,
Zhu Yib481de92007-09-25 17:54:57 -07002900 &tbl_type, &rate_idx);
2901
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07002902 if (priv && priv->bt_full_concurrent) {
2903 /* 1x1 only */
2904 tbl_type.ant_type =
2905 first_antenna(priv->hw_params.valid_tx_ant);
2906 }
2907
Ben Cahill77626352007-11-29 11:09:44 +08002908 /* How many times should we repeat the initial rate? */
Zhu Yib481de92007-09-25 17:54:57 -07002909 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07002910 ant_toggle_cnt = 1;
Zhu Yi1b696de2007-09-27 11:27:41 +08002911 repeat_rate = IWL_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002912 } else {
Daniel Halperin374920c2011-03-16 19:16:36 -07002913 repeat_rate = min(IWL_HT_NUMBER_TRY,
2914 LINK_QUAL_AGG_DISABLE_START_DEF - 1);
Guy Cohenaade00c2008-04-23 17:14:59 -07002915 }
Zhu Yib481de92007-09-25 17:54:57 -07002916
2917 lq_cmd->general_params.mimo_delimiter =
2918 is_mimo(tbl_type.lq_type) ? 1 : 0;
Ben Cahill77626352007-11-29 11:09:44 +08002919
2920 /* Fill 1st table entry (index 0) */
Guy Cohen39e88502008-04-23 17:14:57 -07002921 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002922
Guy Cohen07bc28e2008-04-23 17:15:03 -07002923 if (num_of_ant(tbl_type.ant_type) == 1) {
2924 lq_cmd->general_params.single_stream_ant_msk =
2925 tbl_type.ant_type;
2926 } else if (num_of_ant(tbl_type.ant_type) == 2) {
2927 lq_cmd->general_params.dual_stream_ant_msk =
2928 tbl_type.ant_type;
2929 } /* otherwise we don't modify the existing value */
Zhu Yib481de92007-09-25 17:54:57 -07002930
2931 index++;
Zhu Yi1b696de2007-09-27 11:27:41 +08002932 repeat_rate--;
Wey-Yi Guybee008b2010-08-23 07:57:04 -07002933 if (priv) {
2934 if (priv->bt_full_concurrent)
2935 valid_tx_ant = ANT_A;
2936 else
2937 valid_tx_ant = priv->hw_params.valid_tx_ant;
2938 }
Guy Cohenaade00c2008-04-23 17:14:59 -07002939
Ben Cahill77626352007-11-29 11:09:44 +08002940 /* Fill rest of rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002941 while (index < LINK_QUAL_MAX_RETRY_NUM) {
Ben Cahill77626352007-11-29 11:09:44 +08002942 /* Repeat initial/next rate.
2943 * For legacy IWL_NUMBER_TRY == 1, this loop will not execute.
2944 * For HT IWL_HT_NUMBER_TRY == 3, this executes twice. */
Zhu Yi1b696de2007-09-27 11:27:41 +08002945 while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
Zhu Yib481de92007-09-25 17:54:57 -07002946 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07002947 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2948 ant_toggle_cnt++;
2949 else if (priv &&
2950 rs_toggle_antenna(valid_tx_ant,
2951 &new_rate, &tbl_type))
2952 ant_toggle_cnt = 1;
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07002953 }
Zhu Yi98d7e092007-09-27 11:27:42 +08002954
Ben Cahill77626352007-11-29 11:09:44 +08002955 /* Override next rate if needed for debug purposes */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002956 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Ben Cahill77626352007-11-29 11:09:44 +08002957
2958 /* Fill next table entry */
Zhu Yib481de92007-09-25 17:54:57 -07002959 lq_cmd->rs_table[index].rate_n_flags =
Guy Cohen39e88502008-04-23 17:14:57 -07002960 cpu_to_le32(new_rate);
Zhu Yi1b696de2007-09-27 11:27:41 +08002961 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002962 index++;
2963 }
2964
Guy Cohen39e88502008-04-23 17:14:57 -07002965 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band, &tbl_type,
Zhu Yib481de92007-09-25 17:54:57 -07002966 &rate_idx);
2967
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07002968 if (priv && priv->bt_full_concurrent) {
2969 /* 1x1 only */
2970 tbl_type.ant_type =
2971 first_antenna(priv->hw_params.valid_tx_ant);
2972 }
2973
Ben Cahill77626352007-11-29 11:09:44 +08002974 /* Indicate to uCode which entries might be MIMO.
2975 * If initial rate was MIMO, this will finally end up
2976 * as (IWL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
Zhu Yib481de92007-09-25 17:54:57 -07002977 if (is_mimo(tbl_type.lq_type))
2978 lq_cmd->general_params.mimo_delimiter = index;
2979
Ben Cahill77626352007-11-29 11:09:44 +08002980 /* Get next rate */
Guy Cohen39e88502008-04-23 17:14:57 -07002981 new_rate = rs_get_lower_rate(lq_sta, &tbl_type, rate_idx,
2982 use_ht_possible);
Zhu Yib481de92007-09-25 17:54:57 -07002983
Ben Cahill77626352007-11-29 11:09:44 +08002984 /* How many times should we repeat the next rate? */
Zhu Yib481de92007-09-25 17:54:57 -07002985 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07002986 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2987 ant_toggle_cnt++;
2988 else if (priv &&
2989 rs_toggle_antenna(valid_tx_ant,
2990 &new_rate, &tbl_type))
2991 ant_toggle_cnt = 1;
2992
Zhu Yi1b696de2007-09-27 11:27:41 +08002993 repeat_rate = IWL_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002994 } else {
Zhu Yi1b696de2007-09-27 11:27:41 +08002995 repeat_rate = IWL_HT_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002996 }
Zhu Yib481de92007-09-25 17:54:57 -07002997
Ben Cahill77626352007-11-29 11:09:44 +08002998 /* Don't allow HT rates after next pass.
2999 * rs_get_lower_rate() will change type to LQ_A or LQ_G. */
Zhu Yib481de92007-09-25 17:54:57 -07003000 use_ht_possible = 0;
3001
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 */
Guy Cohen39e88502008-04-23 17:14:57 -07003006 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
Zhu Yib481de92007-09-25 17:54:57 -07003007
3008 index++;
Zhu Yi1b696de2007-09-27 11:27:41 +08003009 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07003010 }
3011
Johannes Berg7b090682011-01-19 02:53:54 -08003012 lq_cmd->agg_params.agg_frame_cnt_limit =
3013 sta_priv->max_agg_bufsize ?: LINK_QUAL_AGG_FRAME_LIMIT_DEF;
Wey-Yi Guy13c33a02009-06-03 11:44:11 -07003014 lq_cmd->agg_params.agg_dis_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
Wey-Yi Guy74697012010-07-28 09:18:54 -07003015
Wey-Yi Guy13c33a02009-06-03 11:44:11 -07003016 lq_cmd->agg_params.agg_time_limit =
3017 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
Wey-Yi Guy74697012010-07-28 09:18:54 -07003018 /*
3019 * overwrite if needed, pass aggregation time limit
3020 * to uCode in uSec
3021 */
Wey-Yi Guy7cb1b082010-10-06 08:10:00 -07003022 if (priv && priv->cfg->bt_params &&
3023 priv->cfg->bt_params->agg_time_limit &&
Wey-Yi Guy66e863a52010-11-08 14:54:37 -08003024 priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH)
Wey-Yi Guy74697012010-07-28 09:18:54 -07003025 lq_cmd->agg_params.agg_time_limit =
Wey-Yi Guy7cb1b082010-10-06 08:10:00 -07003026 cpu_to_le16(priv->cfg->bt_params->agg_time_limit);
Zhu Yib481de92007-09-25 17:54:57 -07003027}
3028
Johannes Berg4b7679a2008-09-18 18:14:18 +02003029static void *rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
Zhu Yib481de92007-09-25 17:54:57 -07003030{
Johannes Berg4b7679a2008-09-18 18:14:18 +02003031 return hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003032}
3033/* rate scale requires free function to be implemented */
3034static void rs_free(void *priv_rate)
3035{
3036 return;
3037}
3038
Johannes Berg4b7679a2008-09-18 18:14:18 +02003039static void rs_free_sta(void *priv_r, struct ieee80211_sta *sta,
3040 void *priv_sta)
Zhu Yib481de92007-09-25 17:54:57 -07003041{
Rami Rosen45527c22008-10-07 09:50:01 +02003042 struct iwl_priv *priv __maybe_unused = priv_r;
Zhu Yib481de92007-09-25 17:54:57 -07003043
Tomas Winklere1623442009-01-27 14:27:56 -08003044 IWL_DEBUG_RATE(priv, "enter\n");
Tomas Winklere1623442009-01-27 14:27:56 -08003045 IWL_DEBUG_RATE(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003046}
3047
3048
Zhu Yi93dc6462007-09-27 11:27:37 +08003049#ifdef CONFIG_MAC80211_DEBUGFS
Zhu Yi5ae212c2007-09-27 11:27:38 +08003050static int open_file_generic(struct inode *inode, struct file *file)
3051{
3052 file->private_data = inode->i_private;
3053 return 0;
3054}
Tomas Winklere227cea2008-07-18 13:53:05 +08003055static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
3056 u32 *rate_n_flags, int index)
Zhu Yi98d7e092007-09-27 11:27:42 +08003057{
Ester Kummerbf403db2008-05-05 10:22:40 +08003058 struct iwl_priv *priv;
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07003059 u8 valid_tx_ant;
3060 u8 ant_sel_tx;
Ester Kummerbf403db2008-05-05 10:22:40 +08003061
3062 priv = lq_sta->drv;
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07003063 valid_tx_ant = priv->hw_params.valid_tx_ant;
Guy Cohen39e88502008-04-23 17:14:57 -07003064 if (lq_sta->dbg_fixed_rate) {
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07003065 ant_sel_tx =
3066 ((lq_sta->dbg_fixed_rate & RATE_MCS_ANT_ABC_MSK)
3067 >> RATE_MCS_ANT_POS);
3068 if ((valid_tx_ant & ant_sel_tx) == ant_sel_tx) {
Guy Cohen39e88502008-04-23 17:14:57 -07003069 *rate_n_flags = lq_sta->dbg_fixed_rate;
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07003070 IWL_DEBUG_RATE(priv, "Fixed rate ON\n");
Guy Cohen39e88502008-04-23 17:14:57 -07003071 } else {
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07003072 lq_sta->dbg_fixed_rate = 0;
3073 IWL_ERR(priv,
3074 "Invalid antenna selection 0x%X, Valid is 0x%X\n",
3075 ant_sel_tx, valid_tx_ant);
3076 IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
Guy Cohen39e88502008-04-23 17:14:57 -07003077 }
Guy Cohen39e88502008-04-23 17:14:57 -07003078 } else {
Tomas Winklere1623442009-01-27 14:27:56 -08003079 IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
Zhu Yi98d7e092007-09-27 11:27:42 +08003080 }
Zhu Yi98d7e092007-09-27 11:27:42 +08003081}
3082
3083static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
3084 const char __user *user_buf, size_t count, loff_t *ppos)
3085{
Tomas Winklere227cea2008-07-18 13:53:05 +08003086 struct iwl_lq_sta *lq_sta = file->private_data;
Ester Kummerbf403db2008-05-05 10:22:40 +08003087 struct iwl_priv *priv;
Zhu Yi98d7e092007-09-27 11:27:42 +08003088 char buf[64];
3089 int buf_size;
3090 u32 parsed_rate;
Johannes Berg7e6a5882010-08-23 10:46:46 +02003091 struct iwl_station_priv *sta_priv =
3092 container_of(lq_sta, struct iwl_station_priv, lq_sta);
3093 struct iwl_rxon_context *ctx = sta_priv->common.ctx;
Zhu Yi98d7e092007-09-27 11:27:42 +08003094
Ester Kummerbf403db2008-05-05 10:22:40 +08003095 priv = lq_sta->drv;
Zhu Yi98d7e092007-09-27 11:27:42 +08003096 memset(buf, 0, sizeof(buf));
3097 buf_size = min(count, sizeof(buf) - 1);
3098 if (copy_from_user(buf, user_buf, buf_size))
3099 return -EFAULT;
3100
3101 if (sscanf(buf, "%x", &parsed_rate) == 1)
Guy Cohen39e88502008-04-23 17:14:57 -07003102 lq_sta->dbg_fixed_rate = parsed_rate;
Zhu Yi98d7e092007-09-27 11:27:42 +08003103 else
Guy Cohen39e88502008-04-23 17:14:57 -07003104 lq_sta->dbg_fixed_rate = 0;
Zhu Yi98d7e092007-09-27 11:27:42 +08003105
Guy Cohen39e88502008-04-23 17:14:57 -07003106 lq_sta->active_legacy_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
3107 lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
3108 lq_sta->active_mimo2_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
3109 lq_sta->active_mimo3_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
Zhu Yi98d7e092007-09-27 11:27:42 +08003110
Tomas Winklere1623442009-01-27 14:27:56 -08003111 IWL_DEBUG_RATE(priv, "sta_id %d rate 0x%X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07003112 lq_sta->lq.sta_id, lq_sta->dbg_fixed_rate);
Zhu Yi98d7e092007-09-27 11:27:42 +08003113
Guy Cohen39e88502008-04-23 17:14:57 -07003114 if (lq_sta->dbg_fixed_rate) {
Guy Cohen07bc28e2008-04-23 17:15:03 -07003115 rs_fill_link_cmd(NULL, lq_sta, lq_sta->dbg_fixed_rate);
Johannes Berg7e6a5882010-08-23 10:46:46 +02003116 iwl_send_lq_cmd(lq_sta->drv, ctx, &lq_sta->lq, CMD_ASYNC,
3117 false);
Zhu Yi98d7e092007-09-27 11:27:42 +08003118 }
3119
3120 return count;
3121}
Zhu Yi0209dc12007-09-27 11:27:43 +08003122
Zhu Yi5ae212c2007-09-27 11:27:38 +08003123static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
3124 char __user *user_buf, size_t count, loff_t *ppos)
3125{
Frank Seidela412c802009-03-01 20:25:38 +01003126 char *buff;
Zhu Yi5ae212c2007-09-27 11:27:38 +08003127 int desc = 0;
3128 int i = 0;
Wey-Yi Guy12b96812009-04-08 11:39:27 -07003129 int index = 0;
Frank Seidela412c802009-03-01 20:25:38 +01003130 ssize_t ret;
Zhu Yi5ae212c2007-09-27 11:27:38 +08003131
Tomas Winklere227cea2008-07-18 13:53:05 +08003132 struct iwl_lq_sta *lq_sta = file->private_data;
Wey-Yi Guyd8ae4f52009-03-10 14:35:07 -07003133 struct iwl_priv *priv;
Wey-Yi Guyadb7b5e2009-03-10 14:35:08 -07003134 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
Zhu Yi5ae212c2007-09-27 11:27:38 +08003135
Wey-Yi Guyd8ae4f52009-03-10 14:35:07 -07003136 priv = lq_sta->drv;
Frank Seidela412c802009-03-01 20:25:38 +01003137 buff = kmalloc(1024, GFP_KERNEL);
3138 if (!buff)
3139 return -ENOMEM;
3140
Tomas Winklerc33104f2008-01-14 17:46:21 -08003141 desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
Zhu Yi98d7e092007-09-27 11:27:42 +08003142 desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08003143 lq_sta->total_failed, lq_sta->total_success,
Guy Cohen39e88502008-04-23 17:14:57 -07003144 lq_sta->active_legacy_rate);
Zhu Yi98d7e092007-09-27 11:27:42 +08003145 desc += sprintf(buff+desc, "fixed rate 0x%X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07003146 lq_sta->dbg_fixed_rate);
Wey-Yi Guyd8ae4f52009-03-10 14:35:07 -07003147 desc += sprintf(buff+desc, "valid_tx_ant %s%s%s\n",
3148 (priv->hw_params.valid_tx_ant & ANT_A) ? "ANT_A," : "",
3149 (priv->hw_params.valid_tx_ant & ANT_B) ? "ANT_B," : "",
3150 (priv->hw_params.valid_tx_ant & ANT_C) ? "ANT_C" : "");
Wey-Yi Guyadb7b5e2009-03-10 14:35:08 -07003151 desc += sprintf(buff+desc, "lq type %s\n",
3152 (is_legacy(tbl->lq_type)) ? "legacy" : "HT");
3153 if (is_Ht(tbl->lq_type)) {
3154 desc += sprintf(buff+desc, " %s",
3155 (is_siso(tbl->lq_type)) ? "SISO" :
3156 ((is_mimo2(tbl->lq_type)) ? "MIMO2" : "MIMO3"));
3157 desc += sprintf(buff+desc, " %s",
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07003158 (tbl->is_ht40) ? "40MHz" : "20MHz");
Daniel C Halperine3949d62009-09-17 10:43:50 -07003159 desc += sprintf(buff+desc, " %s %s %s\n", (tbl->is_SGI) ? "SGI" : "",
3160 (lq_sta->is_green) ? "GF enabled" : "",
3161 (lq_sta->is_agg) ? "AGG on" : "");
Wey-Yi Guyadb7b5e2009-03-10 14:35:08 -07003162 }
Wey-Yi Guy12b96812009-04-08 11:39:27 -07003163 desc += sprintf(buff+desc, "last tx rate=0x%X\n",
3164 lq_sta->last_rate_n_flags);
Zhu Yi5ae212c2007-09-27 11:27:38 +08003165 desc += sprintf(buff+desc, "general:"
3166 "flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08003167 lq_sta->lq.general_params.flags,
3168 lq_sta->lq.general_params.mimo_delimiter,
3169 lq_sta->lq.general_params.single_stream_ant_msk,
3170 lq_sta->lq.general_params.dual_stream_ant_msk);
Zhu Yi5ae212c2007-09-27 11:27:38 +08003171
3172 desc += sprintf(buff+desc, "agg:"
3173 "time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08003174 le16_to_cpu(lq_sta->lq.agg_params.agg_time_limit),
3175 lq_sta->lq.agg_params.agg_dis_start_th,
3176 lq_sta->lq.agg_params.agg_frame_cnt_limit);
Zhu Yi5ae212c2007-09-27 11:27:38 +08003177
3178 desc += sprintf(buff+desc,
3179 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08003180 lq_sta->lq.general_params.start_rate_index[0],
3181 lq_sta->lq.general_params.start_rate_index[1],
3182 lq_sta->lq.general_params.start_rate_index[2],
3183 lq_sta->lq.general_params.start_rate_index[3]);
Zhu Yi5ae212c2007-09-27 11:27:38 +08003184
Wey-Yi Guy12b96812009-04-08 11:39:27 -07003185 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
3186 index = iwl_hwrate_to_plcp_idx(
3187 le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags));
3188 if (is_legacy(tbl->lq_type)) {
3189 desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps\n",
3190 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags),
3191 iwl_rate_mcs[index].mbps);
3192 } else {
3193 desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps (%s)\n",
3194 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags),
3195 iwl_rate_mcs[index].mbps, iwl_rate_mcs[index].mcs);
3196 }
3197 }
Zhu Yi5ae212c2007-09-27 11:27:38 +08003198
Frank Seidela412c802009-03-01 20:25:38 +01003199 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
3200 kfree(buff);
3201 return ret;
Zhu Yi5ae212c2007-09-27 11:27:38 +08003202}
3203
3204static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
Zhu Yi98d7e092007-09-27 11:27:42 +08003205 .write = rs_sta_dbgfs_scale_table_write,
Zhu Yi5ae212c2007-09-27 11:27:38 +08003206 .read = rs_sta_dbgfs_scale_table_read,
3207 .open = open_file_generic,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003208 .llseek = default_llseek,
Zhu Yi5ae212c2007-09-27 11:27:38 +08003209};
Zhu Yi0209dc12007-09-27 11:27:43 +08003210static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
3211 char __user *user_buf, size_t count, loff_t *ppos)
3212{
Frank Seidela412c802009-03-01 20:25:38 +01003213 char *buff;
Zhu Yi0209dc12007-09-27 11:27:43 +08003214 int desc = 0;
3215 int i, j;
Frank Seidela412c802009-03-01 20:25:38 +01003216 ssize_t ret;
Zhu Yi0209dc12007-09-27 11:27:43 +08003217
Tomas Winklere227cea2008-07-18 13:53:05 +08003218 struct iwl_lq_sta *lq_sta = file->private_data;
Frank Seidela412c802009-03-01 20:25:38 +01003219
3220 buff = kmalloc(1024, GFP_KERNEL);
3221 if (!buff)
3222 return -ENOMEM;
3223
Zhu Yi0209dc12007-09-27 11:27:43 +08003224 for (i = 0; i < LQ_SIZE; i++) {
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07003225 desc += sprintf(buff+desc,
3226 "%s type=%d SGI=%d HT40=%d DUP=%d GF=%d\n"
Zhu Yi0209dc12007-09-27 11:27:43 +08003227 "rate=0x%X\n",
Abhijeet Kolekarc3056062008-11-12 13:14:08 -08003228 lq_sta->active_tbl == i ? "*" : "x",
Tomas Winklerc33104f2008-01-14 17:46:21 -08003229 lq_sta->lq_info[i].lq_type,
3230 lq_sta->lq_info[i].is_SGI,
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07003231 lq_sta->lq_info[i].is_ht40,
Tomas Winklerc33104f2008-01-14 17:46:21 -08003232 lq_sta->lq_info[i].is_dup,
Wey-Yi Guy2681b202009-05-21 13:44:22 -07003233 lq_sta->is_green,
Guy Cohen39e88502008-04-23 17:14:57 -07003234 lq_sta->lq_info[i].current_rate);
Zhu Yi0209dc12007-09-27 11:27:43 +08003235 for (j = 0; j < IWL_RATE_COUNT; j++) {
3236 desc += sprintf(buff+desc,
Tomas Winklerc33104f2008-01-14 17:46:21 -08003237 "counter=%d success=%d %%=%d\n",
3238 lq_sta->lq_info[i].win[j].counter,
3239 lq_sta->lq_info[i].win[j].success_counter,
3240 lq_sta->lq_info[i].win[j].success_ratio);
Zhu Yi0209dc12007-09-27 11:27:43 +08003241 }
3242 }
Frank Seidela412c802009-03-01 20:25:38 +01003243 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
3244 kfree(buff);
3245 return ret;
Zhu Yi0209dc12007-09-27 11:27:43 +08003246}
3247
3248static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
3249 .read = rs_sta_dbgfs_stats_table_read,
3250 .open = open_file_generic,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003251 .llseek = default_llseek,
Zhu Yi0209dc12007-09-27 11:27:43 +08003252};
Zhu Yi5ae212c2007-09-27 11:27:38 +08003253
Wey-Yi Guy38167452009-05-08 13:44:43 -07003254static ssize_t rs_sta_dbgfs_rate_scale_data_read(struct file *file,
3255 char __user *user_buf, size_t count, loff_t *ppos)
3256{
3257 char buff[120];
3258 int desc = 0;
Wey-Yi Guy38167452009-05-08 13:44:43 -07003259
3260 struct iwl_lq_sta *lq_sta = file->private_data;
3261 struct iwl_priv *priv;
3262 struct iwl_scale_tbl_info *tbl = &lq_sta->lq_info[lq_sta->active_tbl];
3263
3264 priv = lq_sta->drv;
3265
3266 if (is_Ht(tbl->lq_type))
3267 desc += sprintf(buff+desc,
3268 "Bit Rate= %d Mb/s\n",
3269 tbl->expected_tpt[lq_sta->last_txrate_idx]);
3270 else
3271 desc += sprintf(buff+desc,
3272 "Bit Rate= %d Mb/s\n",
3273 iwl_rates[lq_sta->last_txrate_idx].ieee >> 1);
Wey-Yi Guy38167452009-05-08 13:44:43 -07003274
Johannes Berg08960de2011-04-05 09:41:53 -07003275 return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
Wey-Yi Guy38167452009-05-08 13:44:43 -07003276}
3277
3278static const struct file_operations rs_sta_dbgfs_rate_scale_data_ops = {
3279 .read = rs_sta_dbgfs_rate_scale_data_read,
3280 .open = open_file_generic,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003281 .llseek = default_llseek,
Wey-Yi Guy38167452009-05-08 13:44:43 -07003282};
3283
Zhu Yi93dc6462007-09-27 11:27:37 +08003284static void rs_add_debugfs(void *priv, void *priv_sta,
3285 struct dentry *dir)
3286{
Tomas Winklere227cea2008-07-18 13:53:05 +08003287 struct iwl_lq_sta *lq_sta = priv_sta;
Tomas Winklerc33104f2008-01-14 17:46:21 -08003288 lq_sta->rs_sta_dbgfs_scale_table_file =
Wey-Yi Guy43e85112009-11-20 12:04:58 -08003289 debugfs_create_file("rate_scale_table", S_IRUSR | S_IWUSR, dir,
Tomas Winklerc33104f2008-01-14 17:46:21 -08003290 lq_sta, &rs_sta_dbgfs_scale_table_ops);
3291 lq_sta->rs_sta_dbgfs_stats_table_file =
Wey-Yi Guy43e85112009-11-20 12:04:58 -08003292 debugfs_create_file("rate_stats_table", S_IRUSR, dir,
Tomas Winklerc33104f2008-01-14 17:46:21 -08003293 lq_sta, &rs_sta_dbgfs_stats_table_ops);
Wey-Yi Guy38167452009-05-08 13:44:43 -07003294 lq_sta->rs_sta_dbgfs_rate_scale_data_file =
Wey-Yi Guy43e85112009-11-20 12:04:58 -08003295 debugfs_create_file("rate_scale_data", S_IRUSR, dir,
Wey-Yi Guy38167452009-05-08 13:44:43 -07003296 lq_sta, &rs_sta_dbgfs_rate_scale_data_ops);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02003297 lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file =
Wey-Yi Guy43e85112009-11-20 12:04:58 -08003298 debugfs_create_u8("tx_agg_tid_enable", S_IRUSR | S_IWUSR, dir,
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02003299 &lq_sta->tx_agg_tid_en);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02003300
Zhu Yi93dc6462007-09-27 11:27:37 +08003301}
3302
3303static void rs_remove_debugfs(void *priv, void *priv_sta)
3304{
Tomas Winklere227cea2008-07-18 13:53:05 +08003305 struct iwl_lq_sta *lq_sta = priv_sta;
Tomas Winklerc33104f2008-01-14 17:46:21 -08003306 debugfs_remove(lq_sta->rs_sta_dbgfs_scale_table_file);
3307 debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
Wey-Yi Guy38167452009-05-08 13:44:43 -07003308 debugfs_remove(lq_sta->rs_sta_dbgfs_rate_scale_data_file);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02003309 debugfs_remove(lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file);
Zhu Yi93dc6462007-09-27 11:27:37 +08003310}
3311#endif
3312
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003313/*
3314 * Initialization of rate scaling information is done by driver after
3315 * the station is added. Since mac80211 calls this function before a
3316 * station is added we ignore it.
3317 */
3318static void rs_rate_init_stub(void *priv_r, struct ieee80211_supported_band *sband,
3319 struct ieee80211_sta *sta, void *priv_sta)
3320{
3321}
Zhu Yib481de92007-09-25 17:54:57 -07003322static struct rate_control_ops rs_ops = {
3323 .module = NULL,
3324 .name = RS_NAME,
3325 .tx_status = rs_tx_status,
3326 .get_rate = rs_get_rate,
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003327 .rate_init = rs_rate_init_stub,
Zhu Yib481de92007-09-25 17:54:57 -07003328 .alloc = rs_alloc,
3329 .free = rs_free,
3330 .alloc_sta = rs_alloc_sta,
3331 .free_sta = rs_free_sta,
Zhu Yi93dc6462007-09-27 11:27:37 +08003332#ifdef CONFIG_MAC80211_DEBUGFS
3333 .add_sta_debugfs = rs_add_debugfs,
3334 .remove_sta_debugfs = rs_remove_debugfs,
3335#endif
Zhu Yib481de92007-09-25 17:54:57 -07003336};
3337
Tomas Winklere227cea2008-07-18 13:53:05 +08003338int iwlagn_rate_control_register(void)
Zhu Yib481de92007-09-25 17:54:57 -07003339{
Reinette Chatre897e1cf2008-03-28 16:21:09 -07003340 return ieee80211_rate_control_register(&rs_ops);
Zhu Yib481de92007-09-25 17:54:57 -07003341}
3342
Tomas Winklere227cea2008-07-18 13:53:05 +08003343void iwlagn_rate_control_unregister(void)
Zhu Yib481de92007-09-25 17:54:57 -07003344{
3345 ieee80211_rate_control_unregister(&rs_ops);
3346}
3347