blob: f88e2e33e2e874a5c6617914d4d793d5791ae207 [file] [log] [blame]
Zhu Yib481de92007-09-25 17:54:57 -07001/******************************************************************************
2 *
Wey-Yi Guyfb4961d2012-01-06 13:16:33 -08003 * Copyright(c) 2005 - 2012 Intel Corporation. All rights reserved.
Zhu Yib481de92007-09-25 17:54:57 -07004 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
17 *
18 * The full GNU General Public License is included in this distribution in the
19 * file called LICENSE.
20 *
21 * Contact Information:
Winkler, Tomas759ef892008-12-09 11:28:58 -080022 * Intel Linux Wireless <ilw@linux.intel.com>
Zhu Yib481de92007-09-25 17:54:57 -070023 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24 *
25 *****************************************************************************/
26#include <linux/kernel.h>
27#include <linux/init.h>
28#include <linux/skbuff.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Zhu Yib481de92007-09-25 17:54:57 -070030#include <net/mac80211.h>
Zhu Yib481de92007-09-25 17:54:57 -070031
32#include <linux/netdevice.h>
33#include <linux/etherdevice.h>
34#include <linux/delay.h>
35
36#include <linux/workqueue.h>
37
Tomas Winkler3e0d4cb2008-04-24 11:55:38 -070038#include "iwl-dev.h"
Tomas Winkler857485c2008-03-21 13:53:44 -070039#include "iwl-core.h"
Johannes Berga30e3112010-09-22 18:02:01 +020040#include "iwl-agn.h"
Zhu Yib481de92007-09-25 17:54:57 -070041
Tomas Winklere227cea2008-07-18 13:53:05 +080042#define RS_NAME "iwl-agn-rs"
Zhu Yib481de92007-09-25 17:54:57 -070043
Guy Cohenaade00c2008-04-23 17:14:59 -070044#define NUM_TRY_BEFORE_ANT_TOGGLE 1
Zhu Yib481de92007-09-25 17:54:57 -070045#define IWL_NUMBER_TRY 1
46#define IWL_HT_NUMBER_TRY 3
47
Ben Cahill77626352007-11-29 11:09:44 +080048#define IWL_RATE_MAX_WINDOW 62 /* # tx in history window */
49#define IWL_RATE_MIN_FAILURE_TH 6 /* min failures to calc tpt */
50#define IWL_RATE_MIN_SUCCESS_TH 8 /* min successes to calc tpt */
51
Abbas, Mohamed7d049e52009-01-20 21:33:53 -080052/* max allowed rate miss before sync LQ cmd */
53#define IWL_MISSED_RATE_MAX 15
Ben Cahill77626352007-11-29 11:09:44 +080054/* max time to accum history 2 seconds */
Mohamed Abbas447fee72009-04-20 14:37:02 -070055#define IWL_RATE_SCALE_FLUSH_INTVL (3*HZ)
Zhu Yib481de92007-09-25 17:54:57 -070056
57static u8 rs_ht_to_legacy[] = {
58 IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
59 IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
60 IWL_RATE_6M_INDEX,
61 IWL_RATE_6M_INDEX, IWL_RATE_9M_INDEX,
62 IWL_RATE_12M_INDEX, IWL_RATE_18M_INDEX,
63 IWL_RATE_24M_INDEX, IWL_RATE_36M_INDEX,
64 IWL_RATE_48M_INDEX, IWL_RATE_54M_INDEX
65};
66
Guy Cohenaade00c2008-04-23 17:14:59 -070067static const u8 ant_toggle_lookup[] = {
68 /*ANT_NONE -> */ ANT_NONE,
69 /*ANT_A -> */ ANT_B,
70 /*ANT_B -> */ ANT_C,
71 /*ANT_AB -> */ ANT_BC,
72 /*ANT_C -> */ ANT_A,
73 /*ANT_AC -> */ ANT_AB,
74 /*ANT_BC -> */ ANT_AC,
75 /*ANT_ABC -> */ ANT_ABC,
76};
77
Johannes Berg635b85b2010-09-22 18:02:04 +020078#define IWL_DECLARE_RATE_INFO(r, s, ip, in, rp, rn, pp, np) \
79 [IWL_RATE_##r##M_INDEX] = { IWL_RATE_##r##M_PLCP, \
80 IWL_RATE_SISO_##s##M_PLCP, \
81 IWL_RATE_MIMO2_##s##M_PLCP,\
82 IWL_RATE_MIMO3_##s##M_PLCP,\
83 IWL_RATE_##r##M_IEEE, \
84 IWL_RATE_##ip##M_INDEX, \
85 IWL_RATE_##in##M_INDEX, \
86 IWL_RATE_##rp##M_INDEX, \
87 IWL_RATE_##rn##M_INDEX, \
88 IWL_RATE_##pp##M_INDEX, \
89 IWL_RATE_##np##M_INDEX }
90
91/*
92 * Parameter order:
93 * rate, ht rate, prev rate, next rate, prev tgg rate, next tgg rate
94 *
95 * If there isn't a valid next or previous rate then INV is used which
96 * maps to IWL_RATE_INVALID
97 *
98 */
99const struct iwl_rate_info iwl_rates[IWL_RATE_COUNT] = {
100 IWL_DECLARE_RATE_INFO(1, INV, INV, 2, INV, 2, INV, 2), /* 1mbps */
101 IWL_DECLARE_RATE_INFO(2, INV, 1, 5, 1, 5, 1, 5), /* 2mbps */
102 IWL_DECLARE_RATE_INFO(5, INV, 2, 6, 2, 11, 2, 11), /*5.5mbps */
103 IWL_DECLARE_RATE_INFO(11, INV, 9, 12, 9, 12, 5, 18), /* 11mbps */
104 IWL_DECLARE_RATE_INFO(6, 6, 5, 9, 5, 11, 5, 11), /* 6mbps */
105 IWL_DECLARE_RATE_INFO(9, 6, 6, 11, 6, 11, 5, 11), /* 9mbps */
106 IWL_DECLARE_RATE_INFO(12, 12, 11, 18, 11, 18, 11, 18), /* 12mbps */
107 IWL_DECLARE_RATE_INFO(18, 18, 12, 24, 12, 24, 11, 24), /* 18mbps */
108 IWL_DECLARE_RATE_INFO(24, 24, 18, 36, 18, 36, 18, 36), /* 24mbps */
109 IWL_DECLARE_RATE_INFO(36, 36, 24, 48, 24, 48, 24, 48), /* 36mbps */
110 IWL_DECLARE_RATE_INFO(48, 48, 36, 54, 36, 54, 36, 54), /* 48mbps */
111 IWL_DECLARE_RATE_INFO(54, 54, 48, INV, 48, INV, 48, INV),/* 54mbps */
112 IWL_DECLARE_RATE_INFO(60, 60, 48, INV, 48, INV, 48, INV),/* 60mbps */
113 /* FIXME:RS: ^^ should be INV (legacy) */
114};
115
Daniel Halperin25205462011-03-18 18:48:55 -0700116static inline u8 rs_extract_rate(u32 rate_n_flags)
117{
118 return (u8)(rate_n_flags & RATE_MCS_RATE_MSK);
119}
120
Johannes Berg635b85b2010-09-22 18:02:04 +0200121static int iwl_hwrate_to_plcp_idx(u32 rate_n_flags)
122{
123 int idx = 0;
124
125 /* HT rate format */
126 if (rate_n_flags & RATE_MCS_HT_MSK) {
Daniel Halperin25205462011-03-18 18:48:55 -0700127 idx = rs_extract_rate(rate_n_flags);
Johannes Berg635b85b2010-09-22 18:02:04 +0200128
129 if (idx >= IWL_RATE_MIMO3_6M_PLCP)
130 idx = idx - IWL_RATE_MIMO3_6M_PLCP;
131 else if (idx >= IWL_RATE_MIMO2_6M_PLCP)
132 idx = idx - IWL_RATE_MIMO2_6M_PLCP;
133
134 idx += IWL_FIRST_OFDM_RATE;
135 /* skip 9M not supported in ht*/
136 if (idx >= IWL_RATE_9M_INDEX)
137 idx += 1;
138 if ((idx >= IWL_FIRST_OFDM_RATE) && (idx <= IWL_LAST_OFDM_RATE))
139 return idx;
140
141 /* legacy rate format, search for match in table */
142 } else {
143 for (idx = 0; idx < ARRAY_SIZE(iwl_rates); idx++)
Daniel Halperin25205462011-03-18 18:48:55 -0700144 if (iwl_rates[idx].plcp ==
145 rs_extract_rate(rate_n_flags))
Johannes Berg635b85b2010-09-22 18:02:04 +0200146 return idx;
147 }
148
149 return -1;
150}
151
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700152static void rs_rate_scale_perform(struct iwl_priv *priv,
Gábor Stefanik514d65c2009-04-23 19:36:08 +0200153 struct sk_buff *skb,
Johannes Berg4b7679a2008-09-18 18:14:18 +0200154 struct ieee80211_sta *sta,
155 struct iwl_lq_sta *lq_sta);
Wey-Yi Guy46f93812009-07-24 11:13:03 -0700156static void rs_fill_link_cmd(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +0800157 struct iwl_lq_sta *lq_sta, u32 rate_n_flags);
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -0700158static void rs_stay_in_table(struct iwl_lq_sta *lq_sta, bool force_search);
Zhu Yib481de92007-09-25 17:54:57 -0700159
160
Zhu Yi98d7e092007-09-27 11:27:42 +0800161#ifdef CONFIG_MAC80211_DEBUGFS
Tomas Winklere227cea2008-07-18 13:53:05 +0800162static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
163 u32 *rate_n_flags, int index);
Zhu Yi98d7e092007-09-27 11:27:42 +0800164#else
Tomas Winklere227cea2008-07-18 13:53:05 +0800165static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
166 u32 *rate_n_flags, int index)
Zhu Yi98d7e092007-09-27 11:27:42 +0800167{}
168#endif
Ben Cahill77626352007-11-29 11:09:44 +0800169
Daniel C Halperine3949d62009-09-17 10:43:50 -0700170/**
171 * The following tables contain the expected throughput metrics for all rates
172 *
173 * 1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
174 *
175 * where invalid entries are zeros.
176 *
177 * CCK rates are only valid in legacy table and will only be used in G
178 * (2.4 GHz) band.
Ben Cahill77626352007-11-29 11:09:44 +0800179 */
Wey-Yi Guy584a0f02009-04-01 14:33:24 -0700180
Daniel C Halperine3949d62009-09-17 10:43:50 -0700181static s32 expected_tpt_legacy[IWL_RATE_COUNT] = {
182 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 0
Zhu Yib481de92007-09-25 17:54:57 -0700183};
184
Daniel C Halperine3949d62009-09-17 10:43:50 -0700185static s32 expected_tpt_siso20MHz[4][IWL_RATE_COUNT] = {
Wey-Yi Guy7fc11e92011-01-15 09:16:59 -0800186 {0, 0, 0, 0, 42, 0, 76, 102, 124, 159, 183, 193, 202}, /* Norm */
187 {0, 0, 0, 0, 46, 0, 82, 110, 132, 168, 192, 202, 210}, /* SGI */
188 {0, 0, 0, 0, 47, 0, 91, 133, 171, 242, 305, 334, 362}, /* AGG */
189 {0, 0, 0, 0, 52, 0, 101, 145, 187, 264, 330, 361, 390}, /* AGG+SGI */
Zhu Yib481de92007-09-25 17:54:57 -0700190};
191
Daniel C Halperine3949d62009-09-17 10:43:50 -0700192static s32 expected_tpt_siso40MHz[4][IWL_RATE_COUNT] = {
193 {0, 0, 0, 0, 77, 0, 127, 160, 184, 220, 242, 250, 257}, /* Norm */
194 {0, 0, 0, 0, 83, 0, 135, 169, 193, 229, 250, 257, 264}, /* SGI */
Wey-Yi Guy7fc11e92011-01-15 09:16:59 -0800195 {0, 0, 0, 0, 94, 0, 177, 249, 313, 423, 512, 550, 586}, /* AGG */
196 {0, 0, 0, 0, 104, 0, 193, 270, 338, 454, 545, 584, 620}, /* AGG+SGI */
Zhu Yib481de92007-09-25 17:54:57 -0700197};
198
Daniel C Halperine3949d62009-09-17 10:43:50 -0700199static s32 expected_tpt_mimo2_20MHz[4][IWL_RATE_COUNT] = {
Wey-Yi Guy7fc11e92011-01-15 09:16:59 -0800200 {0, 0, 0, 0, 74, 0, 123, 155, 179, 214, 236, 244, 251}, /* Norm */
201 {0, 0, 0, 0, 81, 0, 131, 164, 188, 223, 243, 251, 257}, /* SGI */
202 {0, 0, 0, 0, 89, 0, 167, 235, 296, 402, 488, 526, 560}, /* AGG */
203 {0, 0, 0, 0, 97, 0, 182, 255, 320, 431, 520, 558, 593}, /* AGG+SGI*/
Zhu Yib481de92007-09-25 17:54:57 -0700204};
205
Daniel C Halperine3949d62009-09-17 10:43:50 -0700206static s32 expected_tpt_mimo2_40MHz[4][IWL_RATE_COUNT] = {
207 {0, 0, 0, 0, 123, 0, 182, 214, 235, 264, 279, 285, 289}, /* Norm */
208 {0, 0, 0, 0, 131, 0, 191, 222, 242, 270, 284, 289, 293}, /* SGI */
Wey-Yi Guy7fc11e92011-01-15 09:16:59 -0800209 {0, 0, 0, 0, 171, 0, 305, 410, 496, 634, 731, 771, 805}, /* AGG */
210 {0, 0, 0, 0, 186, 0, 329, 439, 527, 667, 764, 803, 838}, /* AGG+SGI */
Zhu Yib481de92007-09-25 17:54:57 -0700211};
212
Daniel C Halperine3949d62009-09-17 10:43:50 -0700213static s32 expected_tpt_mimo3_20MHz[4][IWL_RATE_COUNT] = {
214 {0, 0, 0, 0, 99, 0, 153, 186, 208, 239, 256, 263, 268}, /* Norm */
215 {0, 0, 0, 0, 106, 0, 162, 194, 215, 246, 262, 268, 273}, /* SGI */
216 {0, 0, 0, 0, 134, 0, 249, 346, 431, 574, 685, 732, 775}, /* AGG */
217 {0, 0, 0, 0, 148, 0, 272, 376, 465, 614, 727, 775, 818}, /* AGG+SGI */
Zhu Yib481de92007-09-25 17:54:57 -0700218};
219
Daniel C Halperine3949d62009-09-17 10:43:50 -0700220static s32 expected_tpt_mimo3_40MHz[4][IWL_RATE_COUNT] = {
221 {0, 0, 0, 0, 152, 0, 211, 239, 255, 279, 290, 294, 297}, /* Norm */
222 {0, 0, 0, 0, 160, 0, 219, 245, 261, 284, 294, 297, 300}, /* SGI */
223 {0, 0, 0, 0, 254, 0, 443, 584, 695, 868, 984, 1030, 1070}, /* AGG */
224 {0, 0, 0, 0, 277, 0, 478, 624, 737, 911, 1026, 1070, 1109}, /* AGG+SGI */
Wey-Yi Guy584a0f02009-04-01 14:33:24 -0700225};
226
Wey-Yi Guy12b96812009-04-08 11:39:27 -0700227/* mbps, mcs */
Tobias Klauser79496732009-12-23 14:18:11 +0100228static const struct iwl_rate_mcs_info iwl_rate_mcs[IWL_RATE_COUNT] = {
Daniel C Halperine3949d62009-09-17 10:43:50 -0700229 { "1", "BPSK DSSS"},
230 { "2", "QPSK DSSS"},
231 {"5.5", "BPSK CCK"},
232 { "11", "QPSK CCK"},
233 { "6", "BPSK 1/2"},
234 { "9", "BPSK 1/2"},
235 { "12", "QPSK 1/2"},
236 { "18", "QPSK 3/4"},
237 { "24", "16QAM 1/2"},
238 { "36", "16QAM 3/4"},
239 { "48", "64QAM 2/3"},
240 { "54", "64QAM 3/4"},
241 { "60", "64QAM 5/6"},
Wey-Yi Guy12b96812009-04-08 11:39:27 -0700242};
243
Wey-Yi Guya9c146b2009-05-22 11:01:48 -0700244#define MCS_INDEX_PER_STREAM (8)
245
Tomas Winklere227cea2008-07-18 13:53:05 +0800246static void rs_rate_scale_clear_window(struct iwl_rate_scale_data *window)
Zhu Yib481de92007-09-25 17:54:57 -0700247{
248 window->data = 0;
249 window->success_counter = 0;
250 window->success_ratio = IWL_INVALID_VALUE;
251 window->counter = 0;
252 window->average_tpt = IWL_INVALID_VALUE;
253 window->stamp = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700254}
255
Guy Cohenaade00c2008-04-23 17:14:59 -0700256static inline u8 rs_is_valid_ant(u8 valid_antenna, u8 ant_type)
257{
Tomas Winkler3ac7f142008-07-21 02:40:14 +0300258 return (ant_type & valid_antenna) == ant_type;
Guy Cohenaade00c2008-04-23 17:14:59 -0700259}
260
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200261/*
262 * removes the old data from the statistics. All data that is older than
263 * TID_MAX_TIME_DIFF, will be deleted.
264 */
Tomas Winklere227cea2008-07-18 13:53:05 +0800265static void rs_tl_rm_old_stats(struct iwl_traffic_load *tl, u32 curr_time)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200266{
267 /* The oldest age we want to keep */
268 u32 oldest_time = curr_time - TID_MAX_TIME_DIFF;
269
270 while (tl->queue_count &&
271 (tl->time_stamp < oldest_time)) {
272 tl->total -= tl->packet_count[tl->head];
273 tl->packet_count[tl->head] = 0;
274 tl->time_stamp += TID_QUEUE_CELL_SPACING;
275 tl->queue_count--;
276 tl->head++;
277 if (tl->head >= TID_QUEUE_MAX_SIZE)
278 tl->head = 0;
279 }
280}
281
282/*
283 * increment traffic load value for tid and also remove
284 * any old values if passed the certain time period
285 */
Tomas Winklere227cea2008-07-18 13:53:05 +0800286static u8 rs_tl_add_packet(struct iwl_lq_sta *lq_data,
Ron Rindjunskyfaa29712008-06-12 09:46:58 +0800287 struct ieee80211_hdr *hdr)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200288{
289 u32 curr_time = jiffies_to_msecs(jiffies);
290 u32 time_diff;
291 s32 index;
Tomas Winklere227cea2008-07-18 13:53:05 +0800292 struct iwl_traffic_load *tl = NULL;
Tomas Winkler54dbb522008-05-15 13:54:06 +0800293 u8 tid;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200294
Tomas Winkler417f1142008-11-12 13:14:06 -0800295 if (ieee80211_is_data_qos(hdr->frame_control)) {
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700296 u8 *qc = ieee80211_get_qos_ctl(hdr);
Tomas Winkler54dbb522008-05-15 13:54:06 +0800297 tid = qc[0] & 0xf;
298 } else
Emmanuel Grumbach5f85a782011-08-25 23:11:18 -0700299 return IWL_MAX_TID_COUNT;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200300
Johannes Berga8448552011-12-02 12:25:12 -0800301 if (unlikely(tid >= IWL_MAX_TID_COUNT))
Emmanuel Grumbach5f85a782011-08-25 23:11:18 -0700302 return IWL_MAX_TID_COUNT;
Reinette Chatree6a6cf42009-08-13 13:30:50 -0700303
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200304 tl = &lq_data->load[tid];
305
306 curr_time -= curr_time % TID_ROUND_VALUE;
307
308 /* Happens only for the first packet. Initialize the data */
309 if (!(tl->queue_count)) {
310 tl->total = 1;
311 tl->time_stamp = curr_time;
312 tl->queue_count = 1;
313 tl->head = 0;
314 tl->packet_count[0] = 1;
Emmanuel Grumbach5f85a782011-08-25 23:11:18 -0700315 return IWL_MAX_TID_COUNT;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200316 }
317
318 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
319 index = time_diff / TID_QUEUE_CELL_SPACING;
320
321 /* The history is too long: remove data that is older than */
322 /* TID_MAX_TIME_DIFF */
323 if (index >= TID_QUEUE_MAX_SIZE)
324 rs_tl_rm_old_stats(tl, curr_time);
325
326 index = (tl->head + index) % TID_QUEUE_MAX_SIZE;
327 tl->packet_count[index] = tl->packet_count[index] + 1;
328 tl->total = tl->total + 1;
329
330 if ((index + 1) > tl->queue_count)
331 tl->queue_count = index + 1;
Ron Rindjunskyfaa29712008-06-12 09:46:58 +0800332
333 return tid;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200334}
335
Wey-Yi Guy54a430c2011-05-20 11:56:18 -0700336#ifdef CONFIG_MAC80211_DEBUGFS
Wey-Yi Guy4e308112011-07-08 08:46:28 -0700337/**
338 * Program the device to use fixed rate for frame transmit
339 * This is for debugging/testing only
340 * once the device start use fixed rate, we need to reload the module
341 * to being back the normal operation.
342 */
Wey-Yi Guy64898542011-05-03 18:05:13 -0700343static void rs_program_fix_rate(struct iwl_priv *priv,
344 struct iwl_lq_sta *lq_sta)
345{
346 struct iwl_station_priv *sta_priv =
347 container_of(lq_sta, struct iwl_station_priv, lq_sta);
Johannes Bergc6892902011-09-20 15:37:21 -0700348 struct iwl_rxon_context *ctx = sta_priv->ctx;
Wey-Yi Guy64898542011-05-03 18:05:13 -0700349
350 lq_sta->active_legacy_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
351 lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
352 lq_sta->active_mimo2_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
353 lq_sta->active_mimo3_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
354
Wey-Yi Guy5ef15cc2011-11-30 13:24:06 -0800355#ifdef CONFIG_IWLWIFI_DEVICE_TESTMODE
Wey-Yi Guy4e308112011-07-08 08:46:28 -0700356 /* testmode has higher priority to overwirte the fixed rate */
357 if (priv->tm_fixed_rate)
358 lq_sta->dbg_fixed_rate = priv->tm_fixed_rate;
Wey-Yi Guyc10e2c12011-07-13 11:13:46 -0700359#endif
Wey-Yi Guy64898542011-05-03 18:05:13 -0700360
361 IWL_DEBUG_RATE(priv, "sta_id %d rate 0x%X\n",
Wey-Yi Guy4e308112011-07-08 08:46:28 -0700362 lq_sta->lq.sta_id, lq_sta->dbg_fixed_rate);
Wey-Yi Guy64898542011-05-03 18:05:13 -0700363
Wey-Yi Guy4e308112011-07-08 08:46:28 -0700364 if (lq_sta->dbg_fixed_rate) {
365 rs_fill_link_cmd(NULL, lq_sta, lq_sta->dbg_fixed_rate);
Wey-Yi Guy64898542011-05-03 18:05:13 -0700366 iwl_send_lq_cmd(lq_sta->drv, ctx, &lq_sta->lq, CMD_ASYNC,
367 false);
368 }
369}
370#endif
371
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200372/*
373 get the traffic load value for tid
374*/
Tomas Winklere227cea2008-07-18 13:53:05 +0800375static u32 rs_tl_get_load(struct iwl_lq_sta *lq_data, u8 tid)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200376{
377 u32 curr_time = jiffies_to_msecs(jiffies);
378 u32 time_diff;
379 s32 index;
Tomas Winklere227cea2008-07-18 13:53:05 +0800380 struct iwl_traffic_load *tl = NULL;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200381
Johannes Berga8448552011-12-02 12:25:12 -0800382 if (tid >= IWL_MAX_TID_COUNT)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200383 return 0;
384
385 tl = &(lq_data->load[tid]);
386
387 curr_time -= curr_time % TID_ROUND_VALUE;
388
389 if (!(tl->queue_count))
390 return 0;
391
392 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
393 index = time_diff / TID_QUEUE_CELL_SPACING;
394
395 /* The history is too long: remove data that is older than */
396 /* TID_MAX_TIME_DIFF */
397 if (index >= TID_QUEUE_MAX_SIZE)
398 rs_tl_rm_old_stats(tl, curr_time);
399
400 return tl->total;
401}
402
Wey-Yi Guy82ca9342010-04-12 14:02:36 -0700403static int rs_tl_turn_on_agg_for_tid(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +0800404 struct iwl_lq_sta *lq_data, u8 tid,
Johannes Berg4b7679a2008-09-18 18:14:18 +0200405 struct ieee80211_sta *sta)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200406{
Wey-Yi Guy82ca9342010-04-12 14:02:36 -0700407 int ret = -EAGAIN;
Johannes Berg290f5992010-08-23 07:56:58 -0700408 u32 load;
409
410 /*
411 * Don't create TX aggregation sessions when in high
412 * BT traffic, as they would just be disrupted by BT.
413 */
414 if (priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH) {
415 IWL_ERR(priv, "BT traffic (%d), no aggregation allowed\n",
416 priv->bt_traffic_load);
417 return ret;
418 }
419
420 load = rs_tl_get_load(lq_data, tid);
Wey-Yi Guy45d42702010-02-03 12:24:44 -0800421
Wey-Yi Guydd5b6d02011-08-25 23:10:55 -0700422 if ((iwlagn_mod_params.auto_agg) || (load > IWL_AGG_LOAD_THRESHOLD)) {
Tomas Winklere1623442009-01-27 14:27:56 -0800423 IWL_DEBUG_HT(priv, "Starting Tx agg: STA: %pM tid: %d\n",
Johannes Berge1749612008-10-27 15:59:26 -0700424 sta->addr, tid);
Sujith Manoharanbd2ce6e2010-12-15 07:47:10 +0530425 ret = ieee80211_start_tx_ba_session(sta, tid, 5000);
Wey-Yi Guy45d42702010-02-03 12:24:44 -0800426 if (ret == -EAGAIN) {
427 /*
428 * driver and mac80211 is out of sync
429 * this might be cause by reloading firmware
430 * stop the tx ba session here
431 */
Andy Lutomirski24110542010-07-25 13:14:29 -0400432 IWL_ERR(priv, "Fail start Tx agg on tid: %d\n",
Wey-Yi Guy45d42702010-02-03 12:24:44 -0800433 tid);
Johannes Berg6a8579d2010-05-27 14:41:07 +0200434 ieee80211_stop_tx_ba_session(sta, tid);
Wey-Yi Guy45d42702010-02-03 12:24:44 -0800435 }
Andy Lutomirski24110542010-07-25 13:14:29 -0400436 } else {
Wey-Yi Guy5f88ac22011-05-27 08:40:31 -0700437 IWL_DEBUG_HT(priv, "Aggregation not enabled for tid %d "
Andy Lutomirski24110542010-07-25 13:14:29 -0400438 "because load = %u\n", tid, load);
439 }
Wey-Yi Guy82ca9342010-04-12 14:02:36 -0700440 return ret;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200441}
442
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700443static void rs_tl_turn_on_agg(struct iwl_priv *priv, u8 tid,
Tomas Winklere227cea2008-07-18 13:53:05 +0800444 struct iwl_lq_sta *lq_data,
Johannes Berg4b7679a2008-09-18 18:14:18 +0200445 struct ieee80211_sta *sta)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200446{
Johannes Berga8448552011-12-02 12:25:12 -0800447 if (tid < IWL_MAX_TID_COUNT)
Wey-Yi Guycfecc6b2010-06-18 11:33:15 -0700448 rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
449 else
Johannes Berga8448552011-12-02 12:25:12 -0800450 IWL_ERR(priv, "tid exceeds max TID count: %d/%d\n",
451 tid, IWL_MAX_TID_COUNT);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200452}
453
Guy Cohen39e88502008-04-23 17:14:57 -0700454static inline int get_num_of_ant_from_rate(u32 rate_n_flags)
Guy Cohenfde0db32008-04-21 15:42:01 -0700455{
Tomas Winkler3ac7f142008-07-21 02:40:14 +0300456 return !!(rate_n_flags & RATE_MCS_ANT_A_MSK) +
457 !!(rate_n_flags & RATE_MCS_ANT_B_MSK) +
458 !!(rate_n_flags & RATE_MCS_ANT_C_MSK);
Guy Cohenfde0db32008-04-21 15:42:01 -0700459}
460
Shanyu Zhao04f2dec2010-03-19 13:34:45 -0700461/*
462 * Static function to get the expected throughput from an iwl_scale_tbl_info
463 * that wraps a NULL pointer check
464 */
465static s32 get_expected_tpt(struct iwl_scale_tbl_info *tbl, int rs_index)
466{
467 if (tbl->expected_tpt)
468 return tbl->expected_tpt[rs_index];
469 return 0;
470}
471
Ben Cahill77626352007-11-29 11:09:44 +0800472/**
473 * rs_collect_tx_data - Update the success/failure sliding window
474 *
475 * We keep a sliding window of the last 62 packets transmitted
476 * at this rate. window->data contains the bitmask of successful
477 * packets.
478 */
Shanyu Zhao04f2dec2010-03-19 13:34:45 -0700479static int rs_collect_tx_data(struct iwl_scale_tbl_info *tbl,
480 int scale_index, int attempts, int successes)
Zhu Yib481de92007-09-25 17:54:57 -0700481{
Tomas Winklere227cea2008-07-18 13:53:05 +0800482 struct iwl_rate_scale_data *window = NULL;
Guy Cohen39e88502008-04-23 17:14:57 -0700483 static const u64 mask = (((u64)1) << (IWL_RATE_MAX_WINDOW - 1));
Shanyu Zhao04f2dec2010-03-19 13:34:45 -0700484 s32 fail_count, tpt;
Zhu Yib481de92007-09-25 17:54:57 -0700485
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800486 if (scale_index < 0 || scale_index >= IWL_RATE_COUNT)
487 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -0700488
Daniel C Halperine3949d62009-09-17 10:43:50 -0700489 /* Select window for current tx bit rate */
Shanyu Zhao04f2dec2010-03-19 13:34:45 -0700490 window = &(tbl->win[scale_index]);
491
492 /* Get expected throughput */
493 tpt = get_expected_tpt(tbl, scale_index);
Zhu Yib481de92007-09-25 17:54:57 -0700494
Ben Cahill77626352007-11-29 11:09:44 +0800495 /*
496 * Keep track of only the latest 62 tx frame attempts in this rate's
497 * history window; anything older isn't really relevant any more.
498 * If we have filled up the sliding window, drop the oldest attempt;
499 * if the oldest attempt (highest bit in bitmap) shows "success",
500 * subtract "1" from the success counter (this is the main reason
501 * we keep these bitmaps!).
502 */
Daniel C Halperine3949d62009-09-17 10:43:50 -0700503 while (attempts > 0) {
Guy Cohen39e88502008-04-23 17:14:57 -0700504 if (window->counter >= IWL_RATE_MAX_WINDOW) {
505
506 /* remove earliest */
507 window->counter = IWL_RATE_MAX_WINDOW - 1;
508
Ron Rindjunsky99556432008-01-28 14:07:25 +0200509 if (window->data & mask) {
510 window->data &= ~mask;
Guy Cohen39e88502008-04-23 17:14:57 -0700511 window->success_counter--;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200512 }
Zhu Yib481de92007-09-25 17:54:57 -0700513 }
Zhu Yib481de92007-09-25 17:54:57 -0700514
Ron Rindjunsky99556432008-01-28 14:07:25 +0200515 /* Increment frames-attempted counter */
516 window->counter++;
Ben Cahill77626352007-11-29 11:09:44 +0800517
Daniel C Halperine3949d62009-09-17 10:43:50 -0700518 /* Shift bitmap by one frame to throw away oldest history */
Guy Cohen90d77952008-09-09 10:54:53 +0800519 window->data <<= 1;
Daniel C Halperine3949d62009-09-17 10:43:50 -0700520
521 /* Mark the most recent #successes attempts as successful */
Ron Rindjunsky99556432008-01-28 14:07:25 +0200522 if (successes > 0) {
Guy Cohen39e88502008-04-23 17:14:57 -0700523 window->success_counter++;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200524 window->data |= 0x1;
525 successes--;
526 }
527
Daniel C Halperine3949d62009-09-17 10:43:50 -0700528 attempts--;
Zhu Yib481de92007-09-25 17:54:57 -0700529 }
530
Ben Cahill77626352007-11-29 11:09:44 +0800531 /* Calculate current success ratio, avoid divide-by-0! */
Zhu Yib481de92007-09-25 17:54:57 -0700532 if (window->counter > 0)
533 window->success_ratio = 128 * (100 * window->success_counter)
534 / window->counter;
535 else
536 window->success_ratio = IWL_INVALID_VALUE;
537
538 fail_count = window->counter - window->success_counter;
539
Ben Cahill77626352007-11-29 11:09:44 +0800540 /* Calculate average throughput, if we have enough history. */
Zhu Yib481de92007-09-25 17:54:57 -0700541 if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
542 (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
543 window->average_tpt = (window->success_ratio * tpt + 64) / 128;
544 else
545 window->average_tpt = IWL_INVALID_VALUE;
546
Ben Cahill77626352007-11-29 11:09:44 +0800547 /* Tag this window as having been updated */
Zhu Yib481de92007-09-25 17:54:57 -0700548 window->stamp = jiffies;
549
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800550 return 0;
Zhu Yib481de92007-09-25 17:54:57 -0700551}
552
Ben Cahill77626352007-11-29 11:09:44 +0800553/*
554 * Fill uCode API rate_n_flags field, based on "search" or "active" table.
555 */
Guy Cohen39e88502008-04-23 17:14:57 -0700556/* FIXME:RS:remove this function and put the flags statically in the table */
Tomas Winkler978785a2008-12-19 10:37:31 +0800557static u32 rate_n_flags_from_tbl(struct iwl_priv *priv,
558 struct iwl_scale_tbl_info *tbl,
559 int index, u8 use_green)
Zhu Yib481de92007-09-25 17:54:57 -0700560{
Guy Cohen39e88502008-04-23 17:14:57 -0700561 u32 rate_n_flags = 0;
562
Zhu Yib481de92007-09-25 17:54:57 -0700563 if (is_legacy(tbl->lq_type)) {
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800564 rate_n_flags = iwl_rates[index].plcp;
Zhu Yib481de92007-09-25 17:54:57 -0700565 if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
Guy Cohen39e88502008-04-23 17:14:57 -0700566 rate_n_flags |= RATE_MCS_CCK_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700567
Guy Cohenfde0db32008-04-21 15:42:01 -0700568 } else if (is_Ht(tbl->lq_type)) {
569 if (index > IWL_LAST_OFDM_RATE) {
Tomas Winkler978785a2008-12-19 10:37:31 +0800570 IWL_ERR(priv, "Invalid HT rate index %d\n", index);
Zhu Yib481de92007-09-25 17:54:57 -0700571 index = IWL_LAST_OFDM_RATE;
Guy Cohenfde0db32008-04-21 15:42:01 -0700572 }
Guy Cohen39e88502008-04-23 17:14:57 -0700573 rate_n_flags = RATE_MCS_HT_MSK;
Guy Cohenfde0db32008-04-21 15:42:01 -0700574
575 if (is_siso(tbl->lq_type))
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800576 rate_n_flags |= iwl_rates[index].plcp_siso;
Guy Cohenfde0db32008-04-21 15:42:01 -0700577 else if (is_mimo2(tbl->lq_type))
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800578 rate_n_flags |= iwl_rates[index].plcp_mimo2;
Guy Cohenfde0db32008-04-21 15:42:01 -0700579 else
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800580 rate_n_flags |= iwl_rates[index].plcp_mimo3;
Zhu Yib481de92007-09-25 17:54:57 -0700581 } else {
Tomas Winkler978785a2008-12-19 10:37:31 +0800582 IWL_ERR(priv, "Invalid tbl->lq_type %d\n", tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -0700583 }
584
Guy Cohen39e88502008-04-23 17:14:57 -0700585 rate_n_flags |= ((tbl->ant_type << RATE_MCS_ANT_POS) &
Guy Cohenfde0db32008-04-21 15:42:01 -0700586 RATE_MCS_ANT_ABC_MSK);
Zhu Yib481de92007-09-25 17:54:57 -0700587
Guy Cohen39e88502008-04-23 17:14:57 -0700588 if (is_Ht(tbl->lq_type)) {
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700589 if (tbl->is_ht40) {
Guy Cohen39e88502008-04-23 17:14:57 -0700590 if (tbl->is_dup)
591 rate_n_flags |= RATE_MCS_DUP_MSK;
592 else
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700593 rate_n_flags |= RATE_MCS_HT40_MSK;
Guy Cohen39e88502008-04-23 17:14:57 -0700594 }
595 if (tbl->is_SGI)
596 rate_n_flags |= RATE_MCS_SGI_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700597
Guy Cohen39e88502008-04-23 17:14:57 -0700598 if (use_green) {
599 rate_n_flags |= RATE_MCS_GF_MSK;
600 if (is_siso(tbl->lq_type) && tbl->is_SGI) {
601 rate_n_flags &= ~RATE_MCS_SGI_MSK;
Tomas Winkler978785a2008-12-19 10:37:31 +0800602 IWL_ERR(priv, "GF was set with SGI:SISO\n");
Guy Cohen39e88502008-04-23 17:14:57 -0700603 }
604 }
Zhu Yib481de92007-09-25 17:54:57 -0700605 }
Guy Cohen39e88502008-04-23 17:14:57 -0700606 return rate_n_flags;
Zhu Yib481de92007-09-25 17:54:57 -0700607}
608
Ben Cahill77626352007-11-29 11:09:44 +0800609/*
610 * Interpret uCode API's rate_n_flags format,
611 * fill "search" or "active" tx mode table.
612 */
Guy Cohen39e88502008-04-23 17:14:57 -0700613static int rs_get_tbl_info_from_mcs(const u32 rate_n_flags,
Johannes Berg8318d782008-01-24 19:38:38 +0100614 enum ieee80211_band band,
Tomas Winklere227cea2008-07-18 13:53:05 +0800615 struct iwl_scale_tbl_info *tbl,
Zhu Yib481de92007-09-25 17:54:57 -0700616 int *rate_idx)
617{
Guy Cohen39e88502008-04-23 17:14:57 -0700618 u32 ant_msk = (rate_n_flags & RATE_MCS_ANT_ABC_MSK);
619 u8 num_of_ant = get_num_of_ant_from_rate(rate_n_flags);
620 u8 mcs;
Zhu Yib481de92007-09-25 17:54:57 -0700621
Wey-Yi Guy80e91582010-08-03 08:23:32 -0700622 memset(tbl, 0, sizeof(struct iwl_scale_tbl_info));
Tomas Winklere7d326ac2008-06-12 09:47:11 +0800623 *rate_idx = iwl_hwrate_to_plcp_idx(rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -0700624
Guy Cohenfde0db32008-04-21 15:42:01 -0700625 if (*rate_idx == IWL_RATE_INVALID) {
Zhu Yib481de92007-09-25 17:54:57 -0700626 *rate_idx = -1;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800627 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -0700628 }
Ben Cahill77626352007-11-29 11:09:44 +0800629 tbl->is_SGI = 0; /* default legacy setup */
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700630 tbl->is_ht40 = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700631 tbl->is_dup = 0;
Guy Cohenfde0db32008-04-21 15:42:01 -0700632 tbl->ant_type = (ant_msk >> RATE_MCS_ANT_POS);
633 tbl->lq_type = LQ_NONE;
Mohamed Abbasd6e93392009-05-08 13:44:39 -0700634 tbl->max_search = IWL_MAX_SEARCH;
Zhu Yib481de92007-09-25 17:54:57 -0700635
Ben Cahill77626352007-11-29 11:09:44 +0800636 /* legacy rate format */
Guy Cohen39e88502008-04-23 17:14:57 -0700637 if (!(rate_n_flags & RATE_MCS_HT_MSK)) {
Guy Cohenfde0db32008-04-21 15:42:01 -0700638 if (num_of_ant == 1) {
Johannes Berg8318d782008-01-24 19:38:38 +0100639 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700640 tbl->lq_type = LQ_A;
641 else
642 tbl->lq_type = LQ_G;
Zhu Yib481de92007-09-25 17:54:57 -0700643 }
Guy Cohenfde0db32008-04-21 15:42:01 -0700644 /* HT rate format */
Zhu Yib481de92007-09-25 17:54:57 -0700645 } else {
Guy Cohen39e88502008-04-23 17:14:57 -0700646 if (rate_n_flags & RATE_MCS_SGI_MSK)
Zhu Yib481de92007-09-25 17:54:57 -0700647 tbl->is_SGI = 1;
648
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700649 if ((rate_n_flags & RATE_MCS_HT40_MSK) ||
Guy Cohen39e88502008-04-23 17:14:57 -0700650 (rate_n_flags & RATE_MCS_DUP_MSK))
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700651 tbl->is_ht40 = 1;
Zhu Yib481de92007-09-25 17:54:57 -0700652
Guy Cohen39e88502008-04-23 17:14:57 -0700653 if (rate_n_flags & RATE_MCS_DUP_MSK)
Zhu Yib481de92007-09-25 17:54:57 -0700654 tbl->is_dup = 1;
Guy Cohenfde0db32008-04-21 15:42:01 -0700655
Guy Cohen39e88502008-04-23 17:14:57 -0700656 mcs = rs_extract_rate(rate_n_flags);
Guy Cohenfde0db32008-04-21 15:42:01 -0700657
Guy Cohen39e88502008-04-23 17:14:57 -0700658 /* SISO */
659 if (mcs <= IWL_RATE_SISO_60M_PLCP) {
Guy Cohenfde0db32008-04-21 15:42:01 -0700660 if (num_of_ant == 1)
661 tbl->lq_type = LQ_SISO; /*else NONE*/
662 /* MIMO2 */
Guy Cohen39e88502008-04-23 17:14:57 -0700663 } else if (mcs <= IWL_RATE_MIMO2_60M_PLCP) {
Guy Cohenfde0db32008-04-21 15:42:01 -0700664 if (num_of_ant == 2)
665 tbl->lq_type = LQ_MIMO2;
666 /* MIMO3 */
667 } else {
Mohamed Abbasd6e93392009-05-08 13:44:39 -0700668 if (num_of_ant == 3) {
669 tbl->max_search = IWL_MAX_11N_MIMO3_SEARCH;
Guy Cohenfde0db32008-04-21 15:42:01 -0700670 tbl->lq_type = LQ_MIMO3;
Mohamed Abbasd6e93392009-05-08 13:44:39 -0700671 }
Guy Cohenfde0db32008-04-21 15:42:01 -0700672 }
Zhu Yib481de92007-09-25 17:54:57 -0700673 }
674 return 0;
675}
Guy Cohenaade00c2008-04-23 17:14:59 -0700676
677/* switch to another antenna/antennas and return 1 */
678/* if no other valid antenna found, return 0 */
679static int rs_toggle_antenna(u32 valid_ant, u32 *rate_n_flags,
Tomas Winklere227cea2008-07-18 13:53:05 +0800680 struct iwl_scale_tbl_info *tbl)
Zhu Yib481de92007-09-25 17:54:57 -0700681{
Guy Cohenaade00c2008-04-23 17:14:59 -0700682 u8 new_ant_type;
683
684 if (!tbl->ant_type || tbl->ant_type > ANT_ABC)
685 return 0;
686
687 if (!rs_is_valid_ant(valid_ant, tbl->ant_type))
688 return 0;
689
690 new_ant_type = ant_toggle_lookup[tbl->ant_type];
691
692 while ((new_ant_type != tbl->ant_type) &&
693 !rs_is_valid_ant(valid_ant, new_ant_type))
694 new_ant_type = ant_toggle_lookup[new_ant_type];
695
696 if (new_ant_type == tbl->ant_type)
697 return 0;
698
699 tbl->ant_type = new_ant_type;
700 *rate_n_flags &= ~RATE_MCS_ANT_ABC_MSK;
701 *rate_n_flags |= new_ant_type << RATE_MCS_ANT_POS;
702 return 1;
Zhu Yib481de92007-09-25 17:54:57 -0700703}
704
Daniel C Halperinb2617932009-08-13 13:30:59 -0700705/**
706 * Green-field mode is valid if the station supports it and
707 * there are no non-GF stations present in the BSS.
708 */
Johannes Berg7e6a5882010-08-23 10:46:46 +0200709static bool rs_use_green(struct ieee80211_sta *sta)
Zhu Yib481de92007-09-25 17:54:57 -0700710{
Johannes Berg7e6a5882010-08-23 10:46:46 +0200711 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
Johannes Bergc6892902011-09-20 15:37:21 -0700712 struct iwl_rxon_context *ctx = sta_priv->ctx;
Johannes Berg7e6a5882010-08-23 10:46:46 +0200713
Daniel C Halperinb2617932009-08-13 13:30:59 -0700714 return (sta->ht_cap.cap & IEEE80211_HT_CAP_GRN_FLD) &&
Johannes Berg7e6a5882010-08-23 10:46:46 +0200715 !(ctx->ht.non_gf_sta_present);
Guy Cohenf935a6d2008-04-23 17:15:02 -0700716}
Zhu Yib481de92007-09-25 17:54:57 -0700717
718/**
719 * rs_get_supported_rates - get the available rates
720 *
721 * if management frame or broadcast frame only return
722 * basic available rates.
723 *
724 */
Tomas Winklere227cea2008-07-18 13:53:05 +0800725static u16 rs_get_supported_rates(struct iwl_lq_sta *lq_sta,
726 struct ieee80211_hdr *hdr,
727 enum iwl_table_type rate_type)
Zhu Yib481de92007-09-25 17:54:57 -0700728{
Guy Coheneecd6e52008-04-23 17:14:58 -0700729 if (is_legacy(rate_type)) {
730 return lq_sta->active_legacy_rate;
731 } else {
732 if (is_siso(rate_type))
733 return lq_sta->active_siso_rate;
734 else if (is_mimo2(rate_type))
735 return lq_sta->active_mimo2_rate;
736 else
737 return lq_sta->active_mimo3_rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -0800738 }
Zhu Yib481de92007-09-25 17:54:57 -0700739}
740
Ester Kummerbf403db2008-05-05 10:22:40 +0800741static u16 rs_get_adjacent_rate(struct iwl_priv *priv, u8 index, u16 rate_mask,
742 int rate_type)
Zhu Yib481de92007-09-25 17:54:57 -0700743{
744 u8 high = IWL_RATE_INVALID;
745 u8 low = IWL_RATE_INVALID;
746
Ian Schram01ebd062007-10-25 17:15:22 +0800747 /* 802.11A or ht walks to the next literal adjacent rate in
Zhu Yib481de92007-09-25 17:54:57 -0700748 * the rate table */
749 if (is_a_band(rate_type) || !is_legacy(rate_type)) {
750 int i;
751 u32 mask;
752
753 /* Find the previous rate that is in the rate mask */
754 i = index - 1;
755 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
756 if (rate_mask & mask) {
757 low = i;
758 break;
759 }
760 }
761
762 /* Find the next rate that is in the rate mask */
763 i = index + 1;
764 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
765 if (rate_mask & mask) {
766 high = i;
767 break;
768 }
769 }
770
771 return (high << 8) | low;
772 }
773
774 low = index;
775 while (low != IWL_RATE_INVALID) {
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800776 low = iwl_rates[low].prev_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700777 if (low == IWL_RATE_INVALID)
778 break;
779 if (rate_mask & (1 << low))
780 break;
Tomas Winklere1623442009-01-27 14:27:56 -0800781 IWL_DEBUG_RATE(priv, "Skipping masked lower rate: %d\n", low);
Zhu Yib481de92007-09-25 17:54:57 -0700782 }
783
784 high = index;
785 while (high != IWL_RATE_INVALID) {
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800786 high = iwl_rates[high].next_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700787 if (high == IWL_RATE_INVALID)
788 break;
789 if (rate_mask & (1 << high))
790 break;
Tomas Winklere1623442009-01-27 14:27:56 -0800791 IWL_DEBUG_RATE(priv, "Skipping masked higher rate: %d\n", high);
Zhu Yib481de92007-09-25 17:54:57 -0700792 }
793
794 return (high << 8) | low;
795}
796
Tomas Winklere227cea2008-07-18 13:53:05 +0800797static u32 rs_get_lower_rate(struct iwl_lq_sta *lq_sta,
798 struct iwl_scale_tbl_info *tbl,
799 u8 scale_index, u8 ht_possible)
Zhu Yib481de92007-09-25 17:54:57 -0700800{
Zhu Yib481de92007-09-25 17:54:57 -0700801 s32 low;
802 u16 rate_mask;
803 u16 high_low;
804 u8 switch_to_legacy = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -0800805 u8 is_green = lq_sta->is_green;
Wey-Yi Guy2ff65782009-09-11 10:38:18 -0700806 struct iwl_priv *priv = lq_sta->drv;
Zhu Yib481de92007-09-25 17:54:57 -0700807
808 /* check if we need to switch from HT to legacy rates.
809 * assumption is that mandatory rates (1Mbps or 6Mbps)
810 * are always supported (spec demand) */
811 if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_index)) {
812 switch_to_legacy = 1;
813 scale_index = rs_ht_to_legacy[scale_index];
Johannes Berg8318d782008-01-24 19:38:38 +0100814 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700815 tbl->lq_type = LQ_A;
816 else
817 tbl->lq_type = LQ_G;
818
Guy Cohen69fdb302008-04-23 17:15:01 -0700819 if (num_of_ant(tbl->ant_type) > 1)
Wey-Yi Guy2ff65782009-09-11 10:38:18 -0700820 tbl->ant_type =
Emmanuel Grumbachd6189122011-08-25 23:10:39 -0700821 first_antenna(hw_params(priv).valid_tx_ant);
Zhu Yib481de92007-09-25 17:54:57 -0700822
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700823 tbl->is_ht40 = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700824 tbl->is_SGI = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -0700825 tbl->max_search = IWL_MAX_SEARCH;
Zhu Yib481de92007-09-25 17:54:57 -0700826 }
827
Guy Coheneecd6e52008-04-23 17:14:58 -0700828 rate_mask = rs_get_supported_rates(lq_sta, NULL, tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -0700829
Ben Cahill77626352007-11-29 11:09:44 +0800830 /* Mask with station rate restriction */
Zhu Yib481de92007-09-25 17:54:57 -0700831 if (is_legacy(tbl->lq_type)) {
Ben Cahill77626352007-11-29 11:09:44 +0800832 /* supp_rates has no CCK bits in A mode */
Johannes Berg8318d782008-01-24 19:38:38 +0100833 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700834 rate_mask = (u16)(rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -0800835 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
Zhu Yib481de92007-09-25 17:54:57 -0700836 else
Tomas Winklerc33104f2008-01-14 17:46:21 -0800837 rate_mask = (u16)(rate_mask & lq_sta->supp_rates);
Zhu Yib481de92007-09-25 17:54:57 -0700838 }
839
Ben Cahill77626352007-11-29 11:09:44 +0800840 /* If we switched from HT to legacy, check current rate */
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800841 if (switch_to_legacy && (rate_mask & (1 << scale_index))) {
Guy Cohen39e88502008-04-23 17:14:57 -0700842 low = scale_index;
843 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700844 }
845
Ester Kummerbf403db2008-05-05 10:22:40 +0800846 high_low = rs_get_adjacent_rate(lq_sta->drv, scale_index, rate_mask,
847 tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -0700848 low = high_low & 0xff;
849
Guy Cohen39e88502008-04-23 17:14:57 -0700850 if (low == IWL_RATE_INVALID)
851 low = scale_index;
852
853out:
Tomas Winkler978785a2008-12-19 10:37:31 +0800854 return rate_n_flags_from_tbl(lq_sta->drv, tbl, low, is_green);
Zhu Yib481de92007-09-25 17:54:57 -0700855}
856
Ben Cahill77626352007-11-29 11:09:44 +0800857/*
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700858 * Simple function to compare two rate scale table types
859 */
860static bool table_type_matches(struct iwl_scale_tbl_info *a,
861 struct iwl_scale_tbl_info *b)
862{
863 return (a->lq_type == b->lq_type) && (a->ant_type == b->ant_type) &&
864 (a->is_SGI == b->is_SGI);
865}
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700866
Johannes Berg7e6a5882010-08-23 10:46:46 +0200867static void rs_bt_update_lq(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
868 struct iwl_lq_sta *lq_sta)
Wey-Yi Guybee008b2010-08-23 07:57:04 -0700869{
870 struct iwl_scale_tbl_info *tbl;
Wey-Yi Guy66e863a52010-11-08 14:54:37 -0800871 bool full_concurrent = priv->bt_full_concurrent;
Wey-Yi Guybee008b2010-08-23 07:57:04 -0700872 unsigned long flags;
873
Wey-Yi Guy66e863a52010-11-08 14:54:37 -0800874 if (priv->bt_ant_couple_ok) {
875 /*
876 * Is there a need to switch between
877 * full concurrency and 3-wire?
878 */
Emmanuel Grumbach10b15e62011-08-25 23:10:43 -0700879 spin_lock_irqsave(&priv->shrd->lock, flags);
Wey-Yi Guy66e863a52010-11-08 14:54:37 -0800880 if (priv->bt_ci_compliance && priv->bt_ant_couple_ok)
881 full_concurrent = true;
882 else
883 full_concurrent = false;
Emmanuel Grumbach10b15e62011-08-25 23:10:43 -0700884 spin_unlock_irqrestore(&priv->shrd->lock, flags);
Wey-Yi Guy66e863a52010-11-08 14:54:37 -0800885 }
886 if ((priv->bt_traffic_load != priv->last_bt_traffic_load) ||
887 (priv->bt_full_concurrent != full_concurrent)) {
Wey-Yi Guybee008b2010-08-23 07:57:04 -0700888 priv->bt_full_concurrent = full_concurrent;
889
890 /* Update uCode's rate table. */
891 tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
892 rs_fill_link_cmd(priv, lq_sta, tbl->current_rate);
Johannes Berg7e6a5882010-08-23 10:46:46 +0200893 iwl_send_lq_cmd(priv, ctx, &lq_sta->lq, CMD_ASYNC, false);
Wey-Yi Guybee008b2010-08-23 07:57:04 -0700894
Johannes Berg1ee158d2012-02-17 10:07:44 -0800895 queue_work(priv->workqueue, &priv->bt_full_concurrency);
Wey-Yi Guybee008b2010-08-23 07:57:04 -0700896 }
897}
898
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700899/*
Ben Cahill77626352007-11-29 11:09:44 +0800900 * mac80211 sends us Tx status
901 */
Johannes Berg4b7679a2008-09-18 18:14:18 +0200902static void rs_tx_status(void *priv_r, struct ieee80211_supported_band *sband,
903 struct ieee80211_sta *sta, void *priv_sta,
Johannes Berge039fa42008-05-15 12:55:29 +0200904 struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -0700905{
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700906 int legacy_success;
907 int retries;
908 int rs_index, mac_index, i;
Tomas Winkler417f1142008-11-12 13:14:06 -0800909 struct iwl_lq_sta *lq_sta = priv_sta;
Tomas Winkler66c73db2008-04-15 16:01:40 -0700910 struct iwl_link_quality_cmd *table;
Zhu Yib481de92007-09-25 17:54:57 -0700911 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berg4b7679a2008-09-18 18:14:18 +0200912 struct iwl_priv *priv = (struct iwl_priv *)priv_r;
Johannes Berge039fa42008-05-15 12:55:29 +0200913 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Daniel C Halperin50273092009-08-28 09:44:45 -0700914 enum mac80211_rate_control_flags mac_flags;
Guy Cohen39e88502008-04-23 17:14:57 -0700915 u32 tx_rate;
Tomas Winklere227cea2008-07-18 13:53:05 +0800916 struct iwl_scale_tbl_info tbl_type;
Shanyu Zhao04f2dec2010-03-19 13:34:45 -0700917 struct iwl_scale_tbl_info *curr_tbl, *other_tbl, *tmp_tbl;
Johannes Berg7e6a5882010-08-23 10:46:46 +0200918 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
Johannes Bergc6892902011-09-20 15:37:21 -0700919 struct iwl_rxon_context *ctx = sta_priv->ctx;
Zhu Yib481de92007-09-25 17:54:57 -0700920
Tomas Winklere1623442009-01-27 14:27:56 -0800921 IWL_DEBUG_RATE_LIMIT(priv, "get frame ack response, update rate scale window\n");
Zhu Yib481de92007-09-25 17:54:57 -0700922
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800923 /* Treat uninitialized rate scaling data same as non-existing. */
924 if (!lq_sta) {
925 IWL_DEBUG_RATE(priv, "Station rate scaling not created yet.\n");
926 return;
927 } else if (!lq_sta->drv) {
928 IWL_DEBUG_RATE(priv, "Rate scaling not initialized yet.\n");
929 return;
930 }
931
Tomas Winkler417f1142008-11-12 13:14:06 -0800932 if (!ieee80211_is_data(hdr->frame_control) ||
Gábor Stefanik514d65c2009-04-23 19:36:08 +0200933 info->flags & IEEE80211_TX_CTL_NO_ACK)
Zhu Yib481de92007-09-25 17:54:57 -0700934 return;
935
Daniel C Halperine3949d62009-09-17 10:43:50 -0700936 /* This packet was aggregated but doesn't carry status info */
Johannes Berge039fa42008-05-15 12:55:29 +0200937 if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
938 !(info->flags & IEEE80211_TX_STAT_AMPDU))
Ron Rindjunsky99556432008-01-28 14:07:25 +0200939 return;
940
Ben Cahill77626352007-11-29 11:09:44 +0800941 /*
942 * Ignore this Tx frame response if its initial rate doesn't match
943 * that of latest Link Quality command. There may be stragglers
944 * from a previous Link Quality command, but we're no longer interested
945 * in those; they're either from the "active" mode while we're trying
946 * to check "search" mode, or a prior "search" mode after we've moved
947 * to a new "search" mode (which might become the new "active" mode).
948 */
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700949 table = &lq_sta->lq;
Guy Cohen39e88502008-04-23 17:14:57 -0700950 tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
951 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800952 if (priv->band == IEEE80211_BAND_5GHZ)
953 rs_index -= IWL_FIRST_OFDM_RATE;
Daniel C Halperin50273092009-08-28 09:44:45 -0700954 mac_flags = info->status.rates[0].flags;
955 mac_index = info->status.rates[0].idx;
Daniel C Halperin31513be2009-08-28 09:44:47 -0700956 /* For HT packets, map MCS to PLCP */
957 if (mac_flags & IEEE80211_TX_RC_MCS) {
958 mac_index &= RATE_MCS_CODE_MSK; /* Remove # of streams */
959 if (mac_index >= (IWL_RATE_9M_INDEX - IWL_FIRST_OFDM_RATE))
960 mac_index++;
Daniel C Halperin392a0ba2009-09-11 10:38:08 -0700961 /*
962 * mac80211 HT index is always zero-indexed; we need to move
963 * HT OFDM rates after CCK rates in 2.4 GHz band
964 */
965 if (priv->band == IEEE80211_BAND_2GHZ)
966 mac_index += IWL_FIRST_OFDM_RATE;
Daniel C Halperin31513be2009-08-28 09:44:47 -0700967 }
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700968 /* Here we actually compare this rate to the latest LQ command */
Daniel C Halperin50273092009-08-28 09:44:45 -0700969 if ((mac_index < 0) ||
970 (tbl_type.is_SGI != !!(mac_flags & IEEE80211_TX_RC_SHORT_GI)) ||
971 (tbl_type.is_ht40 != !!(mac_flags & IEEE80211_TX_RC_40_MHZ_WIDTH)) ||
972 (tbl_type.is_dup != !!(mac_flags & IEEE80211_TX_RC_DUP_DATA)) ||
Johannes Berge6a98542008-10-21 12:40:02 +0200973 (tbl_type.ant_type != info->antenna_sel_tx) ||
Daniel C Halperin50273092009-08-28 09:44:45 -0700974 (!!(tx_rate & RATE_MCS_HT_MSK) != !!(mac_flags & IEEE80211_TX_RC_MCS)) ||
975 (!!(tx_rate & RATE_MCS_GF_MSK) != !!(mac_flags & IEEE80211_TX_RC_GREEN_FIELD)) ||
Daniel C Halperin31513be2009-08-28 09:44:47 -0700976 (rs_index != mac_index)) {
977 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 -0700978 /*
979 * Since rates mis-match, the last LQ command may have failed.
980 * After IWL_MISSED_RATE_MAX mis-matches, resync the uCode with
981 * ... driver.
Mohamed Abbasd5e49032008-12-12 08:22:15 -0800982 */
Abbas, Mohamed7d049e52009-01-20 21:33:53 -0800983 lq_sta->missed_rate_counter++;
984 if (lq_sta->missed_rate_counter > IWL_MISSED_RATE_MAX) {
985 lq_sta->missed_rate_counter = 0;
Johannes Berg7e6a5882010-08-23 10:46:46 +0200986 iwl_send_lq_cmd(priv, ctx, &lq_sta->lq, CMD_ASYNC, false);
Abbas, Mohamed7d049e52009-01-20 21:33:53 -0800987 }
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700988 /* Regardless, ignore this status info for outdated rate */
989 return;
990 } else
991 /* Rate did match, so reset the missed_rate_counter */
992 lq_sta->missed_rate_counter = 0;
993
994 /* Figure out if rate scale algorithm is in active or search table */
995 if (table_type_matches(&tbl_type,
996 &(lq_sta->lq_info[lq_sta->active_tbl]))) {
997 curr_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
998 other_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
999 } else if (table_type_matches(&tbl_type,
1000 &lq_sta->lq_info[1 - lq_sta->active_tbl])) {
1001 curr_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
1002 other_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1003 } else {
1004 IWL_DEBUG_RATE(priv, "Neither active nor search matches tx rate\n");
Wey-Yi Guy80e91582010-08-03 08:23:32 -07001005 tmp_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1006 IWL_DEBUG_RATE(priv, "active- lq:%x, ant:%x, SGI:%d\n",
1007 tmp_tbl->lq_type, tmp_tbl->ant_type, tmp_tbl->is_SGI);
1008 tmp_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
1009 IWL_DEBUG_RATE(priv, "search- lq:%x, ant:%x, SGI:%d\n",
1010 tmp_tbl->lq_type, tmp_tbl->ant_type, tmp_tbl->is_SGI);
1011 IWL_DEBUG_RATE(priv, "actual- lq:%x, ant:%x, SGI:%d\n",
1012 tbl_type.lq_type, tbl_type.ant_type, tbl_type.is_SGI);
1013 /*
1014 * no matching table found, let's by-pass the data collection
1015 * and continue to perform rate scale to find the rate table
1016 */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001017 rs_stay_in_table(lq_sta, true);
Wey-Yi Guy80e91582010-08-03 08:23:32 -07001018 goto done;
Zhu Yib481de92007-09-25 17:54:57 -07001019 }
Zhu Yib481de92007-09-25 17:54:57 -07001020
Ben Cahill77626352007-11-29 11:09:44 +08001021 /*
Daniel C Halperin95407aa2009-09-17 10:43:48 -07001022 * Updating the frame history depends on whether packets were
1023 * aggregated.
1024 *
1025 * For aggregation, all packets were transmitted at the same rate, the
1026 * first index into rate scale table.
Ben Cahill77626352007-11-29 11:09:44 +08001027 */
Daniel C Halperin95407aa2009-09-17 10:43:48 -07001028 if (info->flags & IEEE80211_TX_STAT_AMPDU) {
1029 tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
1030 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type,
1031 &rs_index);
Shanyu Zhao04f2dec2010-03-19 13:34:45 -07001032 rs_collect_tx_data(curr_tbl, rs_index,
Daniel Halperine3a3cd82010-04-18 09:27:58 -07001033 info->status.ampdu_len,
1034 info->status.ampdu_ack_len);
Zhu Yib481de92007-09-25 17:54:57 -07001035
Daniel C Halperin95407aa2009-09-17 10:43:48 -07001036 /* Update success/fail counts if not searching for new mode */
1037 if (lq_sta->stay_in_tbl) {
Daniel Halperine3a3cd82010-04-18 09:27:58 -07001038 lq_sta->total_success += info->status.ampdu_ack_len;
1039 lq_sta->total_failed += (info->status.ampdu_len -
1040 info->status.ampdu_ack_len);
Daniel C Halperin95407aa2009-09-17 10:43:48 -07001041 }
1042 } else {
1043 /*
1044 * For legacy, update frame history with for each Tx retry.
1045 */
1046 retries = info->status.rates[0].count - 1;
1047 /* HW doesn't send more than 15 retries */
1048 retries = min(retries, 15);
1049
1050 /* The last transmission may have been successful */
1051 legacy_success = !!(info->flags & IEEE80211_TX_STAT_ACK);
1052 /* Collect data for each rate used during failed TX attempts */
1053 for (i = 0; i <= retries; ++i) {
1054 tx_rate = le32_to_cpu(table->rs_table[i].rate_n_flags);
1055 rs_get_tbl_info_from_mcs(tx_rate, priv->band,
1056 &tbl_type, &rs_index);
1057 /*
1058 * Only collect stats if retried rate is in the same RS
1059 * table as active/search.
1060 */
1061 if (table_type_matches(&tbl_type, curr_tbl))
Shanyu Zhao04f2dec2010-03-19 13:34:45 -07001062 tmp_tbl = curr_tbl;
Daniel C Halperin95407aa2009-09-17 10:43:48 -07001063 else if (table_type_matches(&tbl_type, other_tbl))
Shanyu Zhao04f2dec2010-03-19 13:34:45 -07001064 tmp_tbl = other_tbl;
Ron Rindjunsky99556432008-01-28 14:07:25 +02001065 else
Daniel C Halperin95407aa2009-09-17 10:43:48 -07001066 continue;
Shanyu Zhao04f2dec2010-03-19 13:34:45 -07001067 rs_collect_tx_data(tmp_tbl, rs_index, 1,
1068 i < retries ? 0 : legacy_success);
Daniel C Halperin95407aa2009-09-17 10:43:48 -07001069 }
1070
1071 /* Update success/fail counts if not searching for new mode */
1072 if (lq_sta->stay_in_tbl) {
1073 lq_sta->total_success += legacy_success;
1074 lq_sta->total_failed += retries + (1 - legacy_success);
Ron Rindjunsky99556432008-01-28 14:07:25 +02001075 }
Zhu Yib481de92007-09-25 17:54:57 -07001076 }
Daniel C Halperin95407aa2009-09-17 10:43:48 -07001077 /* The last TX rate is cached in lq_sta; it's set in if/else above */
1078 lq_sta->last_rate_n_flags = tx_rate;
Wey-Yi Guy80e91582010-08-03 08:23:32 -07001079done:
Ben Cahill77626352007-11-29 11:09:44 +08001080 /* See if there's a better rate or modulation mode to try. */
Abbas, Mohamedc338ba32009-01-21 10:58:02 -08001081 if (sta && sta->supp_rates[sband->band])
Gábor Stefanik514d65c2009-04-23 19:36:08 +02001082 rs_rate_scale_perform(priv, skb, sta, lq_sta);
Wey-Yi Guyc10e2c12011-07-13 11:13:46 -07001083
Wey-Yi Guy5ef15cc2011-11-30 13:24:06 -08001084#if defined(CONFIG_MAC80211_DEBUGFS) && defined(CONFIG_IWLWIFI_DEVICE_TESTMODE)
Wey-Yi Guy4e308112011-07-08 08:46:28 -07001085 if ((priv->tm_fixed_rate) &&
1086 (priv->tm_fixed_rate != lq_sta->dbg_fixed_rate))
Wey-Yi Guy64898542011-05-03 18:05:13 -07001087 rs_program_fix_rate(priv, lq_sta);
1088#endif
Don Fry38622412011-12-16 07:07:36 -08001089 if (cfg(priv)->bt_params && cfg(priv)->bt_params->advanced_bt_coexist)
Johannes Berg7e6a5882010-08-23 10:46:46 +02001090 rs_bt_update_lq(priv, ctx, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07001091}
1092
Ben Cahill77626352007-11-29 11:09:44 +08001093/*
1094 * Begin a period of staying with a selected modulation mode.
1095 * Set "stay_in_tbl" flag to prevent any mode switches.
1096 * Set frame tx success limits according to legacy vs. high-throughput,
1097 * and reset overall (spanning all rates) tx success history statistics.
1098 * These control how long we stay using same modulation mode before
1099 * searching for a new mode.
1100 */
Ester Kummerbf403db2008-05-05 10:22:40 +08001101static void rs_set_stay_in_table(struct iwl_priv *priv, u8 is_legacy,
Tomas Winklere227cea2008-07-18 13:53:05 +08001102 struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07001103{
Tomas Winklere1623442009-01-27 14:27:56 -08001104 IWL_DEBUG_RATE(priv, "we are staying in the same table\n");
Tomas Winklerc33104f2008-01-14 17:46:21 -08001105 lq_sta->stay_in_tbl = 1; /* only place this gets set */
Zhu Yib481de92007-09-25 17:54:57 -07001106 if (is_legacy) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001107 lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT;
1108 lq_sta->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
1109 lq_sta->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
Zhu Yib481de92007-09-25 17:54:57 -07001110 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001111 lq_sta->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
1112 lq_sta->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
1113 lq_sta->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
Zhu Yib481de92007-09-25 17:54:57 -07001114 }
Tomas Winklerc33104f2008-01-14 17:46:21 -08001115 lq_sta->table_count = 0;
1116 lq_sta->total_failed = 0;
1117 lq_sta->total_success = 0;
Mohamed Abbas447fee72009-04-20 14:37:02 -07001118 lq_sta->flush_timer = jiffies;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001119 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001120}
1121
Ben Cahill77626352007-11-29 11:09:44 +08001122/*
1123 * Find correct throughput table for given mode of modulation
1124 */
Tomas Winklere227cea2008-07-18 13:53:05 +08001125static void rs_set_expected_tpt_table(struct iwl_lq_sta *lq_sta,
1126 struct iwl_scale_tbl_info *tbl)
Zhu Yib481de92007-09-25 17:54:57 -07001127{
Daniel C Halperine3949d62009-09-17 10:43:50 -07001128 /* Used to choose among HT tables */
1129 s32 (*ht_tbl_pointer)[IWL_RATE_COUNT];
1130
1131 /* Check for invalid LQ type */
1132 if (WARN_ON_ONCE(!is_legacy(tbl->lq_type) && !is_Ht(tbl->lq_type))) {
1133 tbl->expected_tpt = expected_tpt_legacy;
1134 return;
1135 }
1136
1137 /* Legacy rates have only one table */
Zhu Yib481de92007-09-25 17:54:57 -07001138 if (is_legacy(tbl->lq_type)) {
Daniel C Halperine3949d62009-09-17 10:43:50 -07001139 tbl->expected_tpt = expected_tpt_legacy;
1140 return;
1141 }
1142
1143 /* Choose among many HT tables depending on number of streams
1144 * (SISO/MIMO2/MIMO3), channel width (20/40), SGI, and aggregation
1145 * status */
1146 if (is_siso(tbl->lq_type) && (!tbl->is_ht40 || lq_sta->is_dup))
1147 ht_tbl_pointer = expected_tpt_siso20MHz;
1148 else if (is_siso(tbl->lq_type))
1149 ht_tbl_pointer = expected_tpt_siso40MHz;
1150 else if (is_mimo2(tbl->lq_type) && (!tbl->is_ht40 || lq_sta->is_dup))
1151 ht_tbl_pointer = expected_tpt_mimo2_20MHz;
1152 else if (is_mimo2(tbl->lq_type))
1153 ht_tbl_pointer = expected_tpt_mimo2_40MHz;
1154 else if (is_mimo3(tbl->lq_type) && (!tbl->is_ht40 || lq_sta->is_dup))
1155 ht_tbl_pointer = expected_tpt_mimo3_20MHz;
1156 else /* if (is_mimo3(tbl->lq_type)) <-- must be true */
1157 ht_tbl_pointer = expected_tpt_mimo3_40MHz;
1158
1159 if (!tbl->is_SGI && !lq_sta->is_agg) /* Normal */
1160 tbl->expected_tpt = ht_tbl_pointer[0];
1161 else if (tbl->is_SGI && !lq_sta->is_agg) /* SGI */
1162 tbl->expected_tpt = ht_tbl_pointer[1];
1163 else if (!tbl->is_SGI && lq_sta->is_agg) /* AGG */
1164 tbl->expected_tpt = ht_tbl_pointer[2];
1165 else /* AGG+SGI */
1166 tbl->expected_tpt = ht_tbl_pointer[3];
Zhu Yib481de92007-09-25 17:54:57 -07001167}
1168
Ben Cahill77626352007-11-29 11:09:44 +08001169/*
1170 * Find starting rate for new "search" high-throughput mode of modulation.
1171 * Goal is to find lowest expected rate (under perfect conditions) that is
1172 * above the current measured throughput of "active" mode, to give new mode
1173 * a fair chance to prove itself without too many challenges.
1174 *
1175 * This gets called when transitioning to more aggressive modulation
1176 * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
1177 * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
1178 * to decrease to match "active" throughput. When moving from MIMO to SISO,
1179 * bit rate will typically need to increase, but not if performance was bad.
1180 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001181static s32 rs_get_best_rate(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001182 struct iwl_lq_sta *lq_sta,
1183 struct iwl_scale_tbl_info *tbl, /* "search" */
Guy Cohenf935a6d2008-04-23 17:15:02 -07001184 u16 rate_mask, s8 index)
Zhu Yib481de92007-09-25 17:54:57 -07001185{
Ben Cahill77626352007-11-29 11:09:44 +08001186 /* "active" values */
Tomas Winklere227cea2008-07-18 13:53:05 +08001187 struct iwl_scale_tbl_info *active_tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001188 &(lq_sta->lq_info[lq_sta->active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07001189 s32 active_sr = active_tbl->win[index].success_ratio;
Zhu Yib481de92007-09-25 17:54:57 -07001190 s32 active_tpt = active_tbl->expected_tpt[index];
Ben Cahill77626352007-11-29 11:09:44 +08001191
1192 /* expected "search" throughput */
1193 s32 *tpt_tbl = tbl->expected_tpt;
1194
1195 s32 new_rate, high, low, start_hi;
Zhu Yib481de92007-09-25 17:54:57 -07001196 u16 high_low;
Guy Cohenf935a6d2008-04-23 17:15:02 -07001197 s8 rate = index;
Zhu Yib481de92007-09-25 17:54:57 -07001198
1199 new_rate = high = low = start_hi = IWL_RATE_INVALID;
1200
1201 for (; ;) {
Ester Kummerbf403db2008-05-05 10:22:40 +08001202 high_low = rs_get_adjacent_rate(priv, rate, rate_mask,
1203 tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -07001204
1205 low = high_low & 0xff;
1206 high = (high_low >> 8) & 0xff;
1207
Ben Cahill77626352007-11-29 11:09:44 +08001208 /*
1209 * Lower the "search" bit rate, to give new "search" mode
1210 * approximately the same throughput as "active" if:
1211 *
1212 * 1) "Active" mode has been working modestly well (but not
1213 * great), and expected "search" throughput (under perfect
1214 * conditions) at candidate rate is above the actual
1215 * measured "active" throughput (but less than expected
1216 * "active" throughput under perfect conditions).
1217 * OR
1218 * 2) "Active" mode has been working perfectly or very well
1219 * and expected "search" throughput (under perfect
1220 * conditions) at candidate rate is above expected
1221 * "active" throughput (under perfect conditions).
1222 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001223 if ((((100 * tpt_tbl[rate]) > lq_sta->last_tpt) &&
Zhu Yib481de92007-09-25 17:54:57 -07001224 ((active_sr > IWL_RATE_DECREASE_TH) &&
1225 (active_sr <= IWL_RATE_HIGH_TH) &&
1226 (tpt_tbl[rate] <= active_tpt))) ||
1227 ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
1228 (tpt_tbl[rate] > active_tpt))) {
1229
Ben Cahill77626352007-11-29 11:09:44 +08001230 /* (2nd or later pass)
1231 * If we've already tried to raise the rate, and are
1232 * now trying to lower it, use the higher rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001233 if (start_hi != IWL_RATE_INVALID) {
1234 new_rate = start_hi;
1235 break;
1236 }
Ben Cahill77626352007-11-29 11:09:44 +08001237
Zhu Yib481de92007-09-25 17:54:57 -07001238 new_rate = rate;
Ben Cahill77626352007-11-29 11:09:44 +08001239
1240 /* Loop again with lower rate */
Zhu Yib481de92007-09-25 17:54:57 -07001241 if (low != IWL_RATE_INVALID)
1242 rate = low;
Ben Cahill77626352007-11-29 11:09:44 +08001243
1244 /* Lower rate not available, use the original */
Zhu Yib481de92007-09-25 17:54:57 -07001245 else
1246 break;
Ben Cahill77626352007-11-29 11:09:44 +08001247
1248 /* Else try to raise the "search" rate to match "active" */
Zhu Yib481de92007-09-25 17:54:57 -07001249 } else {
Ben Cahill77626352007-11-29 11:09:44 +08001250 /* (2nd or later pass)
1251 * If we've already tried to lower the rate, and are
1252 * now trying to raise it, use the lower rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001253 if (new_rate != IWL_RATE_INVALID)
1254 break;
Ben Cahill77626352007-11-29 11:09:44 +08001255
1256 /* Loop again with higher rate */
Zhu Yib481de92007-09-25 17:54:57 -07001257 else if (high != IWL_RATE_INVALID) {
1258 start_hi = high;
1259 rate = high;
Ben Cahill77626352007-11-29 11:09:44 +08001260
1261 /* Higher rate not available, use the original */
Zhu Yib481de92007-09-25 17:54:57 -07001262 } else {
Guy Cohen90d77952008-09-09 10:54:53 +08001263 new_rate = rate;
Zhu Yib481de92007-09-25 17:54:57 -07001264 break;
1265 }
1266 }
1267 }
1268
1269 return new_rate;
1270}
Zhu Yib481de92007-09-25 17:54:57 -07001271
Ben Cahill77626352007-11-29 11:09:44 +08001272/*
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001273 * Set up search table for MIMO2
Ben Cahill77626352007-11-29 11:09:44 +08001274 */
Guy Cohenfde0db32008-04-21 15:42:01 -07001275static int rs_switch_to_mimo2(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001276 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001277 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001278 struct ieee80211_sta *sta,
Tomas Winklere227cea2008-07-18 13:53:05 +08001279 struct iwl_scale_tbl_info *tbl, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001280{
Zhu Yib481de92007-09-25 17:54:57 -07001281 u16 rate_mask;
1282 s32 rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001283 s8 is_green = lq_sta->is_green;
Johannes Berg7e6a5882010-08-23 10:46:46 +02001284 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
Johannes Bergc6892902011-09-20 15:37:21 -07001285 struct iwl_rxon_context *ctx = sta_priv->ctx;
Zhu Yib481de92007-09-25 17:54:57 -07001286
Luis R. Rodriguezde27e642008-12-23 15:58:44 -08001287 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
Zhu Yib481de92007-09-25 17:54:57 -07001288 return -1;
1289
Johannes Bergd9fe60d2008-10-09 12:13:49 +02001290 if (((sta->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 2)
Tomas Winkler00c5ae22008-09-03 11:26:42 +08001291 == WLAN_HT_CAP_SM_PS_STATIC)
Zhu Yib481de92007-09-25 17:54:57 -07001292 return -1;
1293
Ben Cahill77626352007-11-29 11:09:44 +08001294 /* Need both Tx chains/antennas to support MIMO */
Emmanuel Grumbachd6189122011-08-25 23:10:39 -07001295 if (hw_params(priv).tx_chains_num < 2)
Zhu Yib481de92007-09-25 17:54:57 -07001296 return -1;
1297
Tomas Winklere1623442009-01-27 14:27:56 -08001298 IWL_DEBUG_RATE(priv, "LQ: try to switch to MIMO2\n");
Guy Cohen39e88502008-04-23 17:14:57 -07001299
1300 tbl->lq_type = LQ_MIMO2;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001301 tbl->is_dup = lq_sta->is_dup;
Zhu Yib481de92007-09-25 17:54:57 -07001302 tbl->action = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001303 tbl->max_search = IWL_MAX_SEARCH;
Guy Cohen39e88502008-04-23 17:14:57 -07001304 rate_mask = lq_sta->active_mimo2_rate;
1305
Johannes Berg7e6a5882010-08-23 10:46:46 +02001306 if (iwl_is_ht40_tx_allowed(priv, ctx, &sta->ht_cap))
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001307 tbl->is_ht40 = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001308 else
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001309 tbl->is_ht40 = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001310
Guy Coheneecd6e52008-04-23 17:14:58 -07001311 rs_set_expected_tpt_table(lq_sta, tbl);
Zhu Yib481de92007-09-25 17:54:57 -07001312
Guy Cohenf935a6d2008-04-23 17:15:02 -07001313 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
Zhu Yib481de92007-09-25 17:54:57 -07001314
Tomas Winklere1623442009-01-27 14:27:56 -08001315 IWL_DEBUG_RATE(priv, "LQ: MIMO2 best rate %d mask %X\n", rate, rate_mask);
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001316 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1317 IWL_DEBUG_RATE(priv, "Can't switch with index %d rate mask %x\n",
1318 rate, rate_mask);
1319 return -1;
1320 }
1321 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
Guy Cohen39e88502008-04-23 17:14:57 -07001322
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001323 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
1324 tbl->current_rate, is_green);
1325 return 0;
1326}
1327
1328/*
1329 * Set up search table for MIMO3
1330 */
1331static int rs_switch_to_mimo3(struct iwl_priv *priv,
1332 struct iwl_lq_sta *lq_sta,
1333 struct ieee80211_conf *conf,
1334 struct ieee80211_sta *sta,
1335 struct iwl_scale_tbl_info *tbl, int index)
1336{
1337 u16 rate_mask;
1338 s32 rate;
1339 s8 is_green = lq_sta->is_green;
Johannes Berg7e6a5882010-08-23 10:46:46 +02001340 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
Johannes Bergc6892902011-09-20 15:37:21 -07001341 struct iwl_rxon_context *ctx = sta_priv->ctx;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001342
1343 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
1344 return -1;
1345
1346 if (((sta->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 2)
1347 == WLAN_HT_CAP_SM_PS_STATIC)
1348 return -1;
1349
1350 /* Need both Tx chains/antennas to support MIMO */
Emmanuel Grumbachd6189122011-08-25 23:10:39 -07001351 if (hw_params(priv).tx_chains_num < 3)
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001352 return -1;
1353
1354 IWL_DEBUG_RATE(priv, "LQ: try to switch to MIMO3\n");
1355
1356 tbl->lq_type = LQ_MIMO3;
1357 tbl->is_dup = lq_sta->is_dup;
1358 tbl->action = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001359 tbl->max_search = IWL_MAX_11N_MIMO3_SEARCH;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001360 rate_mask = lq_sta->active_mimo3_rate;
1361
Johannes Berg7e6a5882010-08-23 10:46:46 +02001362 if (iwl_is_ht40_tx_allowed(priv, ctx, &sta->ht_cap))
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001363 tbl->is_ht40 = 1;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001364 else
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001365 tbl->is_ht40 = 0;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001366
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001367 rs_set_expected_tpt_table(lq_sta, tbl);
1368
1369 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
1370
1371 IWL_DEBUG_RATE(priv, "LQ: MIMO3 best rate %d mask %X\n",
1372 rate, rate_mask);
Guy Cohen39e88502008-04-23 17:14:57 -07001373 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
Tomas Winklere1623442009-01-27 14:27:56 -08001374 IWL_DEBUG_RATE(priv, "Can't switch with index %d rate mask %x\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001375 rate, rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07001376 return -1;
Guy Cohen39e88502008-04-23 17:14:57 -07001377 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001378 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
Zhu Yib481de92007-09-25 17:54:57 -07001379
Tomas Winklere1623442009-01-27 14:27:56 -08001380 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001381 tbl->current_rate, is_green);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001382 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07001383}
1384
Ben Cahill77626352007-11-29 11:09:44 +08001385/*
1386 * Set up search table for SISO
1387 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001388static int rs_switch_to_siso(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001389 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001390 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001391 struct ieee80211_sta *sta,
Tomas Winklere227cea2008-07-18 13:53:05 +08001392 struct iwl_scale_tbl_info *tbl, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001393{
Zhu Yib481de92007-09-25 17:54:57 -07001394 u16 rate_mask;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001395 u8 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07001396 s32 rate;
Johannes Berg7e6a5882010-08-23 10:46:46 +02001397 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
Johannes Bergc6892902011-09-20 15:37:21 -07001398 struct iwl_rxon_context *ctx = sta_priv->ctx;
Zhu Yib481de92007-09-25 17:54:57 -07001399
Luis R. Rodriguezde27e642008-12-23 15:58:44 -08001400 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
Zhu Yib481de92007-09-25 17:54:57 -07001401 return -1;
1402
Tomas Winklere1623442009-01-27 14:27:56 -08001403 IWL_DEBUG_RATE(priv, "LQ: try to switch to SISO\n");
Guy Cohen39e88502008-04-23 17:14:57 -07001404
Tomas Winklerc33104f2008-01-14 17:46:21 -08001405 tbl->is_dup = lq_sta->is_dup;
Zhu Yib481de92007-09-25 17:54:57 -07001406 tbl->lq_type = LQ_SISO;
1407 tbl->action = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001408 tbl->max_search = IWL_MAX_SEARCH;
Guy Cohen39e88502008-04-23 17:14:57 -07001409 rate_mask = lq_sta->active_siso_rate;
Zhu Yib481de92007-09-25 17:54:57 -07001410
Johannes Berg7e6a5882010-08-23 10:46:46 +02001411 if (iwl_is_ht40_tx_allowed(priv, ctx, &sta->ht_cap))
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001412 tbl->is_ht40 = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001413 else
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001414 tbl->is_ht40 = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001415
Zhu Yib481de92007-09-25 17:54:57 -07001416 if (is_green)
Guy Cohen39e88502008-04-23 17:14:57 -07001417 tbl->is_SGI = 0; /*11n spec: no SGI in SISO+Greenfield*/
Zhu Yib481de92007-09-25 17:54:57 -07001418
Guy Coheneecd6e52008-04-23 17:14:58 -07001419 rs_set_expected_tpt_table(lq_sta, tbl);
Guy Cohenf935a6d2008-04-23 17:15:02 -07001420 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
Zhu Yib481de92007-09-25 17:54:57 -07001421
Tomas Winklere1623442009-01-27 14:27:56 -08001422 IWL_DEBUG_RATE(priv, "LQ: get best rate %d mask %X\n", rate, rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07001423 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
Tomas Winklere1623442009-01-27 14:27:56 -08001424 IWL_DEBUG_RATE(priv, "can not switch with index %d rate mask %x\n",
Zhu Yib481de92007-09-25 17:54:57 -07001425 rate, rate_mask);
1426 return -1;
1427 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001428 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
Tomas Winklere1623442009-01-27 14:27:56 -08001429 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001430 tbl->current_rate, is_green);
Mohamed Abbas403ab562007-11-06 22:06:25 -08001431 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07001432}
1433
Ben Cahill77626352007-11-29 11:09:44 +08001434/*
1435 * Try to switch to new modulation mode from legacy
1436 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001437static int rs_move_legacy_other(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001438 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001439 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001440 struct ieee80211_sta *sta,
Zhu Yib481de92007-09-25 17:54:57 -07001441 int index)
1442{
Tomas Winklere227cea2008-07-18 13:53:05 +08001443 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1444 struct iwl_scale_tbl_info *search_tbl =
1445 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1446 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1447 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1448 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Wey-Yi Guyb1a78852010-07-31 08:52:03 -07001449 u8 start_action;
Emmanuel Grumbachd6189122011-08-25 23:10:39 -07001450 u8 valid_tx_ant = hw_params(priv).valid_tx_ant;
1451 u8 tx_chains_num = hw_params(priv).tx_chains_num;
Guy Cohenfde0db32008-04-21 15:42:01 -07001452 int ret = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001453 u8 update_search_tbl_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001454
Johannes Berg290f5992010-08-23 07:56:58 -07001455 switch (priv->bt_traffic_load) {
1456 case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
1457 /* nothing */
1458 break;
1459 case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
1460 /* avoid antenna B unless MIMO */
1461 if (tbl->action == IWL_LEGACY_SWITCH_ANTENNA2)
Venkataraman, Meenakshi383b0872011-11-10 06:55:25 -08001462 tbl->action = IWL_LEGACY_SWITCH_SISO;
Johannes Berg290f5992010-08-23 07:56:58 -07001463 break;
1464 case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
1465 case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
1466 /* avoid antenna B and MIMO */
Emmanuel Grumbachd6189122011-08-25 23:10:39 -07001467 valid_tx_ant =
1468 first_antenna(hw_params(priv).valid_tx_ant);
Johannes Berg290f5992010-08-23 07:56:58 -07001469 if (tbl->action >= IWL_LEGACY_SWITCH_ANTENNA2 &&
1470 tbl->action != IWL_LEGACY_SWITCH_SISO)
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001471 tbl->action = IWL_LEGACY_SWITCH_SISO;
Johannes Berg290f5992010-08-23 07:56:58 -07001472 break;
1473 default:
1474 IWL_ERR(priv, "Invalid BT load %d", priv->bt_traffic_load);
1475 break;
1476 }
1477
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001478 if (!iwl_ht_enabled(priv))
1479 /* stay in Legacy */
1480 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
Johannes Berg3ad3b922009-08-07 15:41:48 -07001481 else if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001482 tbl->action > IWL_LEGACY_SWITCH_SISO)
1483 tbl->action = IWL_LEGACY_SWITCH_SISO;
Wey-Yi Guybee008b2010-08-23 07:57:04 -07001484
1485 /* configure as 1x1 if bt full concurrency */
1486 if (priv->bt_full_concurrent) {
1487 if (!iwl_ht_enabled(priv))
1488 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
1489 else if (tbl->action >= IWL_LEGACY_SWITCH_ANTENNA2)
1490 tbl->action = IWL_LEGACY_SWITCH_SISO;
Emmanuel Grumbachd6189122011-08-25 23:10:39 -07001491 valid_tx_ant =
1492 first_antenna(hw_params(priv).valid_tx_ant);
Wey-Yi Guybee008b2010-08-23 07:57:04 -07001493 }
1494
Wey-Yi Guyb1a78852010-07-31 08:52:03 -07001495 start_action = tbl->action;
Zhu Yib481de92007-09-25 17:54:57 -07001496 for (; ;) {
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001497 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001498 switch (tbl->action) {
Guy Cohen3110bef2008-09-09 10:54:54 +08001499 case IWL_LEGACY_SWITCH_ANTENNA1:
1500 case IWL_LEGACY_SWITCH_ANTENNA2:
Tomas Winklere1623442009-01-27 14:27:56 -08001501 IWL_DEBUG_RATE(priv, "LQ: Legacy toggle Antenna\n");
Zhu Yib481de92007-09-25 17:54:57 -07001502
Guy Cohen3110bef2008-09-09 10:54:54 +08001503 if ((tbl->action == IWL_LEGACY_SWITCH_ANTENNA1 &&
1504 tx_chains_num <= 1) ||
1505 (tbl->action == IWL_LEGACY_SWITCH_ANTENNA2 &&
1506 tx_chains_num <= 2))
1507 break;
1508
Ben Cahill77626352007-11-29 11:09:44 +08001509 /* Don't change antenna if success has been great */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001510 if (window->success_ratio >= IWL_RS_GOOD_RATIO &&
1511 !priv->bt_full_concurrent &&
1512 priv->bt_traffic_load ==
1513 IWL_BT_COEX_TRAFFIC_LOAD_NONE)
Zhu Yib481de92007-09-25 17:54:57 -07001514 break;
Ben Cahill77626352007-11-29 11:09:44 +08001515
Ben Cahill77626352007-11-29 11:09:44 +08001516 /* Set up search table to try other antenna */
Zhu Yib481de92007-09-25 17:54:57 -07001517 memcpy(search_tbl, tbl, sz);
1518
Guy Cohenaade00c2008-04-23 17:14:59 -07001519 if (rs_toggle_antenna(valid_tx_ant,
1520 &search_tbl->current_rate, search_tbl)) {
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001521 update_search_tbl_counter = 1;
Guy Cohen3110bef2008-09-09 10:54:54 +08001522 rs_set_expected_tpt_table(lq_sta, search_tbl);
Guy Cohenaade00c2008-04-23 17:14:59 -07001523 goto out;
1524 }
Guy Cohena5e8b502008-05-29 16:35:11 +08001525 break;
Zhu Yib481de92007-09-25 17:54:57 -07001526 case IWL_LEGACY_SWITCH_SISO:
Tomas Winklere1623442009-01-27 14:27:56 -08001527 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to SISO\n");
Ben Cahill77626352007-11-29 11:09:44 +08001528
1529 /* Set up search table to try SISO */
Zhu Yib481de92007-09-25 17:54:57 -07001530 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001531 search_tbl->is_SGI = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001532 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001533 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001534 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001535 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001536 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001537 }
Zhu Yib481de92007-09-25 17:54:57 -07001538
1539 break;
Guy Cohen3110bef2008-09-09 10:54:54 +08001540 case IWL_LEGACY_SWITCH_MIMO2_AB:
1541 case IWL_LEGACY_SWITCH_MIMO2_AC:
1542 case IWL_LEGACY_SWITCH_MIMO2_BC:
Tomas Winklere1623442009-01-27 14:27:56 -08001543 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to MIMO2\n");
Ben Cahill77626352007-11-29 11:09:44 +08001544
1545 /* Set up search table to try MIMO */
Zhu Yib481de92007-09-25 17:54:57 -07001546 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001547 search_tbl->is_SGI = 0;
Guy Cohen3110bef2008-09-09 10:54:54 +08001548
1549 if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AB)
1550 search_tbl->ant_type = ANT_AB;
1551 else if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AC)
1552 search_tbl->ant_type = ANT_AC;
1553 else
1554 search_tbl->ant_type = ANT_BC;
1555
1556 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1557 break;
1558
Guy Cohenfde0db32008-04-21 15:42:01 -07001559 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001560 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001561 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001562 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001563 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001564 }
Zhu Yib481de92007-09-25 17:54:57 -07001565 break;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001566
1567 case IWL_LEGACY_SWITCH_MIMO3_ABC:
1568 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to MIMO3\n");
1569
1570 /* Set up search table to try MIMO3 */
1571 memcpy(search_tbl, tbl, sz);
1572 search_tbl->is_SGI = 0;
1573
1574 search_tbl->ant_type = ANT_ABC;
1575
1576 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1577 break;
1578
1579 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1580 search_tbl, index);
1581 if (!ret) {
1582 lq_sta->action_counter = 0;
1583 goto out;
1584 }
1585 break;
Zhu Yib481de92007-09-25 17:54:57 -07001586 }
1587 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001588 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001589 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
Zhu Yib481de92007-09-25 17:54:57 -07001590
1591 if (tbl->action == start_action)
1592 break;
1593
1594 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001595 search_tbl->lq_type = LQ_NONE;
Zhu Yib481de92007-09-25 17:54:57 -07001596 return 0;
1597
Guy Cohen3110bef2008-09-09 10:54:54 +08001598out:
1599 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001600 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001601 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001602 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001603 if (update_search_tbl_counter)
1604 search_tbl->action = tbl->action;
Zhu Yib481de92007-09-25 17:54:57 -07001605 return 0;
1606
1607}
1608
Ben Cahill77626352007-11-29 11:09:44 +08001609/*
1610 * Try to switch to new modulation mode from SISO
1611 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001612static int rs_move_siso_to_other(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001613 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001614 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001615 struct ieee80211_sta *sta, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001616{
Tomas Winklerc33104f2008-01-14 17:46:21 -08001617 u8 is_green = lq_sta->is_green;
Tomas Winklere227cea2008-07-18 13:53:05 +08001618 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1619 struct iwl_scale_tbl_info *search_tbl =
1620 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1621 struct iwl_rate_scale_data *window = &(tbl->win[index]);
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001622 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Tomas Winklere227cea2008-07-18 13:53:05 +08001623 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1624 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Wey-Yi Guyb1a78852010-07-31 08:52:03 -07001625 u8 start_action;
Emmanuel Grumbachd6189122011-08-25 23:10:39 -07001626 u8 valid_tx_ant = hw_params(priv).valid_tx_ant;
1627 u8 tx_chains_num = hw_params(priv).tx_chains_num;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001628 u8 update_search_tbl_counter = 0;
Guy Cohenfde0db32008-04-21 15:42:01 -07001629 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001630
Johannes Berg290f5992010-08-23 07:56:58 -07001631 switch (priv->bt_traffic_load) {
1632 case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
1633 /* nothing */
1634 break;
1635 case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
1636 /* avoid antenna B unless MIMO */
1637 if (tbl->action == IWL_SISO_SWITCH_ANTENNA2)
Venkataraman, Meenakshi383b0872011-11-10 06:55:25 -08001638 tbl->action = IWL_SISO_SWITCH_MIMO2_AB;
Johannes Berg290f5992010-08-23 07:56:58 -07001639 break;
1640 case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
1641 case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
1642 /* avoid antenna B and MIMO */
Emmanuel Grumbachd6189122011-08-25 23:10:39 -07001643 valid_tx_ant =
1644 first_antenna(hw_params(priv).valid_tx_ant);
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001645 if (tbl->action != IWL_SISO_SWITCH_ANTENNA1)
Johannes Berg290f5992010-08-23 07:56:58 -07001646 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1647 break;
1648 default:
1649 IWL_ERR(priv, "Invalid BT load %d", priv->bt_traffic_load);
1650 break;
1651 }
1652
Johannes Berg3ad3b922009-08-07 15:41:48 -07001653 if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001654 tbl->action > IWL_SISO_SWITCH_ANTENNA2) {
1655 /* stay in SISO */
1656 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1657 }
Wey-Yi Guybee008b2010-08-23 07:57:04 -07001658
1659 /* configure as 1x1 if bt full concurrency */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001660 if (priv->bt_full_concurrent) {
Emmanuel Grumbachd6189122011-08-25 23:10:39 -07001661 valid_tx_ant =
1662 first_antenna(hw_params(priv).valid_tx_ant);
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001663 if (tbl->action >= IWL_LEGACY_SWITCH_ANTENNA2)
1664 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1665 }
Wey-Yi Guybee008b2010-08-23 07:57:04 -07001666
Wey-Yi Guyb1a78852010-07-31 08:52:03 -07001667 start_action = tbl->action;
Zhu Yib481de92007-09-25 17:54:57 -07001668 for (;;) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001669 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001670 switch (tbl->action) {
Guy Cohen3110bef2008-09-09 10:54:54 +08001671 case IWL_SISO_SWITCH_ANTENNA1:
1672 case IWL_SISO_SWITCH_ANTENNA2:
Tomas Winklere1623442009-01-27 14:27:56 -08001673 IWL_DEBUG_RATE(priv, "LQ: SISO toggle Antenna\n");
Guy Cohen3110bef2008-09-09 10:54:54 +08001674 if ((tbl->action == IWL_SISO_SWITCH_ANTENNA1 &&
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001675 tx_chains_num <= 1) ||
Guy Cohen3110bef2008-09-09 10:54:54 +08001676 (tbl->action == IWL_SISO_SWITCH_ANTENNA2 &&
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001677 tx_chains_num <= 2))
Guy Cohen3110bef2008-09-09 10:54:54 +08001678 break;
1679
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001680 if (window->success_ratio >= IWL_RS_GOOD_RATIO &&
1681 !priv->bt_full_concurrent &&
1682 priv->bt_traffic_load ==
1683 IWL_BT_COEX_TRAFFIC_LOAD_NONE)
Zhu Yib481de92007-09-25 17:54:57 -07001684 break;
Zhu Yib481de92007-09-25 17:54:57 -07001685
1686 memcpy(search_tbl, tbl, sz);
Guy Cohenaade00c2008-04-23 17:14:59 -07001687 if (rs_toggle_antenna(valid_tx_ant,
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001688 &search_tbl->current_rate, search_tbl)) {
1689 update_search_tbl_counter = 1;
Guy Cohenaade00c2008-04-23 17:14:59 -07001690 goto out;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001691 }
Guy Cohena5e8b502008-05-29 16:35:11 +08001692 break;
Guy Cohen3110bef2008-09-09 10:54:54 +08001693 case IWL_SISO_SWITCH_MIMO2_AB:
1694 case IWL_SISO_SWITCH_MIMO2_AC:
1695 case IWL_SISO_SWITCH_MIMO2_BC:
Tomas Winklere1623442009-01-27 14:27:56 -08001696 IWL_DEBUG_RATE(priv, "LQ: SISO switch to MIMO2\n");
Zhu Yib481de92007-09-25 17:54:57 -07001697 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001698 search_tbl->is_SGI = 0;
Guy Cohen3110bef2008-09-09 10:54:54 +08001699
1700 if (tbl->action == IWL_SISO_SWITCH_MIMO2_AB)
1701 search_tbl->ant_type = ANT_AB;
1702 else if (tbl->action == IWL_SISO_SWITCH_MIMO2_AC)
1703 search_tbl->ant_type = ANT_AC;
1704 else
1705 search_tbl->ant_type = ANT_BC;
1706
1707 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1708 break;
1709
Guy Cohenfde0db32008-04-21 15:42:01 -07001710 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001711 search_tbl, index);
Guy Cohen3110bef2008-09-09 10:54:54 +08001712 if (!ret)
Zhu Yib481de92007-09-25 17:54:57 -07001713 goto out;
1714 break;
1715 case IWL_SISO_SWITCH_GI:
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001716 if (!tbl->is_ht40 && !(ht_cap->cap &
1717 IEEE80211_HT_CAP_SGI_20))
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001718 break;
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001719 if (tbl->is_ht40 && !(ht_cap->cap &
1720 IEEE80211_HT_CAP_SGI_40))
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001721 break;
1722
Tomas Winklere1623442009-01-27 14:27:56 -08001723 IWL_DEBUG_RATE(priv, "LQ: SISO toggle SGI/NGI\n");
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001724
Zhu Yib481de92007-09-25 17:54:57 -07001725 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001726 if (is_green) {
1727 if (!tbl->is_SGI)
1728 break;
1729 else
Winkler, Tomas15b16872008-12-19 10:37:33 +08001730 IWL_ERR(priv,
1731 "SGI was set in GF+SISO\n");
Zhu Yib481de92007-09-25 17:54:57 -07001732 }
Guy Cohen39e88502008-04-23 17:14:57 -07001733 search_tbl->is_SGI = !tbl->is_SGI;
Guy Coheneecd6e52008-04-23 17:14:58 -07001734 rs_set_expected_tpt_table(lq_sta, search_tbl);
Guy Cohen39e88502008-04-23 17:14:57 -07001735 if (tbl->is_SGI) {
1736 s32 tpt = lq_sta->last_tpt / 100;
1737 if (tpt >= search_tbl->expected_tpt[index])
1738 break;
1739 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001740 search_tbl->current_rate =
1741 rate_n_flags_from_tbl(priv, search_tbl,
1742 index, is_green);
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001743 update_search_tbl_counter = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001744 goto out;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001745 case IWL_SISO_SWITCH_MIMO3_ABC:
1746 IWL_DEBUG_RATE(priv, "LQ: SISO switch to MIMO3\n");
1747 memcpy(search_tbl, tbl, sz);
1748 search_tbl->is_SGI = 0;
1749 search_tbl->ant_type = ANT_ABC;
1750
1751 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1752 break;
1753
1754 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1755 search_tbl, index);
1756 if (!ret)
1757 goto out;
1758 break;
Zhu Yib481de92007-09-25 17:54:57 -07001759 }
1760 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001761 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001762 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
Zhu Yib481de92007-09-25 17:54:57 -07001763
1764 if (tbl->action == start_action)
1765 break;
1766 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001767 search_tbl->lq_type = LQ_NONE;
Zhu Yib481de92007-09-25 17:54:57 -07001768 return 0;
1769
1770 out:
Guy Cohen3110bef2008-09-09 10:54:54 +08001771 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001772 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001773 if (tbl->action > IWL_SISO_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001774 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001775 if (update_search_tbl_counter)
1776 search_tbl->action = tbl->action;
1777
Zhu Yib481de92007-09-25 17:54:57 -07001778 return 0;
1779}
1780
Ben Cahill77626352007-11-29 11:09:44 +08001781/*
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001782 * Try to switch to new modulation mode from MIMO2
Ben Cahill77626352007-11-29 11:09:44 +08001783 */
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001784static int rs_move_mimo2_to_other(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001785 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001786 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001787 struct ieee80211_sta *sta, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001788{
Tomas Winklerc33104f2008-01-14 17:46:21 -08001789 s8 is_green = lq_sta->is_green;
Tomas Winklere227cea2008-07-18 13:53:05 +08001790 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1791 struct iwl_scale_tbl_info *search_tbl =
1792 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Guy Cohen3110bef2008-09-09 10:54:54 +08001793 struct iwl_rate_scale_data *window = &(tbl->win[index]);
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001794 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Tomas Winklere227cea2008-07-18 13:53:05 +08001795 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1796 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Wey-Yi Guyb1a78852010-07-31 08:52:03 -07001797 u8 start_action;
Emmanuel Grumbachd6189122011-08-25 23:10:39 -07001798 u8 valid_tx_ant = hw_params(priv).valid_tx_ant;
1799 u8 tx_chains_num = hw_params(priv).tx_chains_num;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001800 u8 update_search_tbl_counter = 0;
Guy Cohenaade00c2008-04-23 17:14:59 -07001801 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001802
Johannes Berg290f5992010-08-23 07:56:58 -07001803 switch (priv->bt_traffic_load) {
1804 case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
1805 /* nothing */
1806 break;
1807 case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
1808 case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
1809 /* avoid antenna B and MIMO */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001810 if (tbl->action != IWL_MIMO2_SWITCH_SISO_A)
1811 tbl->action = IWL_MIMO2_SWITCH_SISO_A;
1812 break;
Johannes Berg290f5992010-08-23 07:56:58 -07001813 case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
1814 /* avoid antenna B unless MIMO */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001815 if (tbl->action == IWL_MIMO2_SWITCH_SISO_B ||
1816 tbl->action == IWL_MIMO2_SWITCH_SISO_C)
Johannes Berg290f5992010-08-23 07:56:58 -07001817 tbl->action = IWL_MIMO2_SWITCH_SISO_A;
1818 break;
1819 default:
1820 IWL_ERR(priv, "Invalid BT load %d", priv->bt_traffic_load);
1821 break;
1822 }
1823
Johannes Berg3ad3b922009-08-07 15:41:48 -07001824 if ((iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE) &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001825 (tbl->action < IWL_MIMO2_SWITCH_SISO_A ||
1826 tbl->action > IWL_MIMO2_SWITCH_SISO_C)) {
1827 /* switch in SISO */
1828 tbl->action = IWL_MIMO2_SWITCH_SISO_A;
1829 }
Wey-Yi Guybee008b2010-08-23 07:57:04 -07001830
1831 /* configure as 1x1 if bt full concurrency */
1832 if (priv->bt_full_concurrent &&
1833 (tbl->action < IWL_MIMO2_SWITCH_SISO_A ||
1834 tbl->action > IWL_MIMO2_SWITCH_SISO_C))
1835 tbl->action = IWL_MIMO2_SWITCH_SISO_A;
1836
Wey-Yi Guyb1a78852010-07-31 08:52:03 -07001837 start_action = tbl->action;
Zhu Yib481de92007-09-25 17:54:57 -07001838 for (;;) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001839 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001840 switch (tbl->action) {
Guy Cohen3110bef2008-09-09 10:54:54 +08001841 case IWL_MIMO2_SWITCH_ANTENNA1:
1842 case IWL_MIMO2_SWITCH_ANTENNA2:
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001843 IWL_DEBUG_RATE(priv, "LQ: MIMO2 toggle Antennas\n");
Guy Cohen3110bef2008-09-09 10:54:54 +08001844
1845 if (tx_chains_num <= 2)
1846 break;
1847
1848 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1849 break;
1850
1851 memcpy(search_tbl, tbl, sz);
1852 if (rs_toggle_antenna(valid_tx_ant,
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001853 &search_tbl->current_rate, search_tbl)) {
1854 update_search_tbl_counter = 1;
Guy Cohen3110bef2008-09-09 10:54:54 +08001855 goto out;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001856 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001857 break;
1858 case IWL_MIMO2_SWITCH_SISO_A:
1859 case IWL_MIMO2_SWITCH_SISO_B:
1860 case IWL_MIMO2_SWITCH_SISO_C:
Tomas Winklere1623442009-01-27 14:27:56 -08001861 IWL_DEBUG_RATE(priv, "LQ: MIMO2 switch to SISO\n");
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001862
Ben Cahill77626352007-11-29 11:09:44 +08001863 /* Set up new search table for SISO */
Zhu Yib481de92007-09-25 17:54:57 -07001864 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001865
Guy Cohen3110bef2008-09-09 10:54:54 +08001866 if (tbl->action == IWL_MIMO2_SWITCH_SISO_A)
Guy Cohenfde0db32008-04-21 15:42:01 -07001867 search_tbl->ant_type = ANT_A;
Guy Cohen3110bef2008-09-09 10:54:54 +08001868 else if (tbl->action == IWL_MIMO2_SWITCH_SISO_B)
Guy Cohenfde0db32008-04-21 15:42:01 -07001869 search_tbl->ant_type = ANT_B;
Guy Cohen3110bef2008-09-09 10:54:54 +08001870 else
1871 search_tbl->ant_type = ANT_C;
1872
1873 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1874 break;
Zhu Yib481de92007-09-25 17:54:57 -07001875
Tomas Winklerc33104f2008-01-14 17:46:21 -08001876 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001877 search_tbl, index);
Guy Cohen3110bef2008-09-09 10:54:54 +08001878 if (!ret)
Zhu Yib481de92007-09-25 17:54:57 -07001879 goto out;
Guy Cohen3110bef2008-09-09 10:54:54 +08001880
Zhu Yib481de92007-09-25 17:54:57 -07001881 break;
1882
Guy Cohen3110bef2008-09-09 10:54:54 +08001883 case IWL_MIMO2_SWITCH_GI:
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001884 if (!tbl->is_ht40 && !(ht_cap->cap &
1885 IEEE80211_HT_CAP_SGI_20))
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001886 break;
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001887 if (tbl->is_ht40 && !(ht_cap->cap &
1888 IEEE80211_HT_CAP_SGI_40))
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001889 break;
1890
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001891 IWL_DEBUG_RATE(priv, "LQ: MIMO2 toggle SGI/NGI\n");
1892
1893 /* Set up new search table for MIMO2 */
1894 memcpy(search_tbl, tbl, sz);
1895 search_tbl->is_SGI = !tbl->is_SGI;
1896 rs_set_expected_tpt_table(lq_sta, search_tbl);
1897 /*
1898 * If active table already uses the fastest possible
1899 * modulation (dual stream with short guard interval),
1900 * and it's working well, there's no need to look
1901 * for a better type of modulation!
1902 */
1903 if (tbl->is_SGI) {
1904 s32 tpt = lq_sta->last_tpt / 100;
1905 if (tpt >= search_tbl->expected_tpt[index])
1906 break;
1907 }
1908 search_tbl->current_rate =
1909 rate_n_flags_from_tbl(priv, search_tbl,
1910 index, is_green);
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001911 update_search_tbl_counter = 1;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001912 goto out;
1913
1914 case IWL_MIMO2_SWITCH_MIMO3_ABC:
1915 IWL_DEBUG_RATE(priv, "LQ: MIMO2 switch to MIMO3\n");
1916 memcpy(search_tbl, tbl, sz);
1917 search_tbl->is_SGI = 0;
1918 search_tbl->ant_type = ANT_ABC;
1919
1920 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1921 break;
1922
1923 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1924 search_tbl, index);
1925 if (!ret)
1926 goto out;
1927
1928 break;
1929 }
1930 tbl->action++;
1931 if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
1932 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
1933
1934 if (tbl->action == start_action)
1935 break;
1936 }
1937 search_tbl->lq_type = LQ_NONE;
1938 return 0;
1939 out:
1940 lq_sta->search_better_tbl = 1;
1941 tbl->action++;
1942 if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
1943 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001944 if (update_search_tbl_counter)
1945 search_tbl->action = tbl->action;
1946
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001947 return 0;
1948
1949}
1950
1951/*
1952 * Try to switch to new modulation mode from MIMO3
1953 */
1954static int rs_move_mimo3_to_other(struct iwl_priv *priv,
1955 struct iwl_lq_sta *lq_sta,
1956 struct ieee80211_conf *conf,
1957 struct ieee80211_sta *sta, int index)
1958{
1959 s8 is_green = lq_sta->is_green;
1960 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1961 struct iwl_scale_tbl_info *search_tbl =
1962 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1963 struct iwl_rate_scale_data *window = &(tbl->win[index]);
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001964 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001965 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1966 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Wey-Yi Guyb1a78852010-07-31 08:52:03 -07001967 u8 start_action;
Emmanuel Grumbachd6189122011-08-25 23:10:39 -07001968 u8 valid_tx_ant = hw_params(priv).valid_tx_ant;
1969 u8 tx_chains_num = hw_params(priv).tx_chains_num;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001970 int ret;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001971 u8 update_search_tbl_counter = 0;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001972
Johannes Berg290f5992010-08-23 07:56:58 -07001973 switch (priv->bt_traffic_load) {
1974 case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
1975 /* nothing */
1976 break;
1977 case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
1978 case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
1979 /* avoid antenna B and MIMO */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001980 if (tbl->action != IWL_MIMO3_SWITCH_SISO_A)
Johannes Berg290f5992010-08-23 07:56:58 -07001981 tbl->action = IWL_MIMO3_SWITCH_SISO_A;
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001982 break;
Johannes Berg290f5992010-08-23 07:56:58 -07001983 case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
1984 /* avoid antenna B unless MIMO */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001985 if (tbl->action == IWL_MIMO3_SWITCH_SISO_B ||
1986 tbl->action == IWL_MIMO3_SWITCH_SISO_C)
Johannes Berg290f5992010-08-23 07:56:58 -07001987 tbl->action = IWL_MIMO3_SWITCH_SISO_A;
Johannes Berg290f5992010-08-23 07:56:58 -07001988 break;
1989 default:
1990 IWL_ERR(priv, "Invalid BT load %d", priv->bt_traffic_load);
1991 break;
1992 }
1993
Johannes Berg3ad3b922009-08-07 15:41:48 -07001994 if ((iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE) &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001995 (tbl->action < IWL_MIMO3_SWITCH_SISO_A ||
1996 tbl->action > IWL_MIMO3_SWITCH_SISO_C)) {
1997 /* switch in SISO */
1998 tbl->action = IWL_MIMO3_SWITCH_SISO_A;
1999 }
Wey-Yi Guybee008b2010-08-23 07:57:04 -07002000
2001 /* configure as 1x1 if bt full concurrency */
2002 if (priv->bt_full_concurrent &&
2003 (tbl->action < IWL_MIMO3_SWITCH_SISO_A ||
2004 tbl->action > IWL_MIMO3_SWITCH_SISO_C))
2005 tbl->action = IWL_MIMO3_SWITCH_SISO_A;
2006
Wey-Yi Guyb1a78852010-07-31 08:52:03 -07002007 start_action = tbl->action;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07002008 for (;;) {
2009 lq_sta->action_counter++;
2010 switch (tbl->action) {
2011 case IWL_MIMO3_SWITCH_ANTENNA1:
2012 case IWL_MIMO3_SWITCH_ANTENNA2:
2013 IWL_DEBUG_RATE(priv, "LQ: MIMO3 toggle Antennas\n");
2014
2015 if (tx_chains_num <= 3)
2016 break;
2017
2018 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
2019 break;
2020
2021 memcpy(search_tbl, tbl, sz);
2022 if (rs_toggle_antenna(valid_tx_ant,
2023 &search_tbl->current_rate, search_tbl))
2024 goto out;
2025 break;
2026 case IWL_MIMO3_SWITCH_SISO_A:
2027 case IWL_MIMO3_SWITCH_SISO_B:
2028 case IWL_MIMO3_SWITCH_SISO_C:
2029 IWL_DEBUG_RATE(priv, "LQ: MIMO3 switch to SISO\n");
2030
2031 /* Set up new search table for SISO */
2032 memcpy(search_tbl, tbl, sz);
2033
2034 if (tbl->action == IWL_MIMO3_SWITCH_SISO_A)
2035 search_tbl->ant_type = ANT_A;
2036 else if (tbl->action == IWL_MIMO3_SWITCH_SISO_B)
2037 search_tbl->ant_type = ANT_B;
2038 else
2039 search_tbl->ant_type = ANT_C;
2040
2041 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
2042 break;
2043
2044 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
2045 search_tbl, index);
2046 if (!ret)
2047 goto out;
2048
2049 break;
2050
2051 case IWL_MIMO3_SWITCH_MIMO2_AB:
2052 case IWL_MIMO3_SWITCH_MIMO2_AC:
2053 case IWL_MIMO3_SWITCH_MIMO2_BC:
2054 IWL_DEBUG_RATE(priv, "LQ: MIMO3 switch to MIMO2\n");
2055
2056 memcpy(search_tbl, tbl, sz);
2057 search_tbl->is_SGI = 0;
2058 if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AB)
2059 search_tbl->ant_type = ANT_AB;
2060 else if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AC)
2061 search_tbl->ant_type = ANT_AC;
2062 else
2063 search_tbl->ant_type = ANT_BC;
2064
2065 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
2066 break;
2067
2068 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
2069 search_tbl, index);
2070 if (!ret)
2071 goto out;
2072
2073 break;
2074
2075 case IWL_MIMO3_SWITCH_GI:
Daniel C Halperin28e6f482009-08-13 13:30:58 -07002076 if (!tbl->is_ht40 && !(ht_cap->cap &
2077 IEEE80211_HT_CAP_SGI_20))
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07002078 break;
Daniel C Halperin28e6f482009-08-13 13:30:58 -07002079 if (tbl->is_ht40 && !(ht_cap->cap &
2080 IEEE80211_HT_CAP_SGI_40))
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07002081 break;
2082
2083 IWL_DEBUG_RATE(priv, "LQ: MIMO3 toggle SGI/NGI\n");
Ben Cahill77626352007-11-29 11:09:44 +08002084
2085 /* Set up new search table for MIMO */
Zhu Yib481de92007-09-25 17:54:57 -07002086 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07002087 search_tbl->is_SGI = !tbl->is_SGI;
Guy Coheneecd6e52008-04-23 17:14:58 -07002088 rs_set_expected_tpt_table(lq_sta, search_tbl);
Ben Cahill77626352007-11-29 11:09:44 +08002089 /*
2090 * If active table already uses the fastest possible
2091 * modulation (dual stream with short guard interval),
2092 * and it's working well, there's no need to look
2093 * for a better type of modulation!
2094 */
Guy Cohen39e88502008-04-23 17:14:57 -07002095 if (tbl->is_SGI) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002096 s32 tpt = lq_sta->last_tpt / 100;
Guy Cohen39e88502008-04-23 17:14:57 -07002097 if (tpt >= search_tbl->expected_tpt[index])
2098 break;
Zhu Yib481de92007-09-25 17:54:57 -07002099 }
Tomas Winkler978785a2008-12-19 10:37:31 +08002100 search_tbl->current_rate =
2101 rate_n_flags_from_tbl(priv, search_tbl,
2102 index, is_green);
Mohamed Abbasd6e93392009-05-08 13:44:39 -07002103 update_search_tbl_counter = 1;
Zhu Yib481de92007-09-25 17:54:57 -07002104 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07002105 }
2106 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07002107 if (tbl->action > IWL_MIMO3_SWITCH_GI)
2108 tbl->action = IWL_MIMO3_SWITCH_ANTENNA1;
Zhu Yib481de92007-09-25 17:54:57 -07002109
2110 if (tbl->action == start_action)
2111 break;
2112 }
Guy Cohen3110bef2008-09-09 10:54:54 +08002113 search_tbl->lq_type = LQ_NONE;
Zhu Yib481de92007-09-25 17:54:57 -07002114 return 0;
2115 out:
Guy Cohen3110bef2008-09-09 10:54:54 +08002116 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07002117 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07002118 if (tbl->action > IWL_MIMO3_SWITCH_GI)
2119 tbl->action = IWL_MIMO3_SWITCH_ANTENNA1;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07002120 if (update_search_tbl_counter)
2121 search_tbl->action = tbl->action;
2122
Zhu Yib481de92007-09-25 17:54:57 -07002123 return 0;
2124
2125}
2126
Ben Cahill77626352007-11-29 11:09:44 +08002127/*
2128 * Check whether we should continue using same modulation mode, or
2129 * begin search for a new mode, based on:
2130 * 1) # tx successes or failures while using this mode
2131 * 2) # times calling this function
2132 * 3) elapsed time in this mode (not used, for now)
2133 */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07002134static void rs_stay_in_table(struct iwl_lq_sta *lq_sta, bool force_search)
Zhu Yib481de92007-09-25 17:54:57 -07002135{
Tomas Winklere227cea2008-07-18 13:53:05 +08002136 struct iwl_scale_tbl_info *tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002137 int i;
2138 int active_tbl;
2139 int flush_interval_passed = 0;
Ester Kummerbf403db2008-05-05 10:22:40 +08002140 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07002141
Ester Kummerbf403db2008-05-05 10:22:40 +08002142 priv = lq_sta->drv;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002143 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002144
Tomas Winklerc33104f2008-01-14 17:46:21 -08002145 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07002146
Ben Cahill77626352007-11-29 11:09:44 +08002147 /* If we've been disallowing search, see if we should now allow it */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002148 if (lq_sta->stay_in_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07002149
Ben Cahill77626352007-11-29 11:09:44 +08002150 /* Elapsed time using current modulation mode */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002151 if (lq_sta->flush_timer)
Zhu Yib481de92007-09-25 17:54:57 -07002152 flush_interval_passed =
Mohamed Abbas447fee72009-04-20 14:37:02 -07002153 time_after(jiffies,
2154 (unsigned long)(lq_sta->flush_timer +
Zhu Yib481de92007-09-25 17:54:57 -07002155 IWL_RATE_SCALE_FLUSH_INTVL));
2156
Ben Cahill77626352007-11-29 11:09:44 +08002157 /*
2158 * Check if we should allow search for new modulation mode.
2159 * If many frames have failed or succeeded, or we've used
2160 * this same modulation for a long time, allow search, and
2161 * reset history stats that keep track of whether we should
2162 * allow a new search. Also (below) reset all bitmaps and
2163 * stats in active history.
2164 */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07002165 if (force_search ||
2166 (lq_sta->total_failed > lq_sta->max_failure_limit) ||
Tomas Winklerc33104f2008-01-14 17:46:21 -08002167 (lq_sta->total_success > lq_sta->max_success_limit) ||
2168 ((!lq_sta->search_better_tbl) && (lq_sta->flush_timer)
Zhu Yib481de92007-09-25 17:54:57 -07002169 && (flush_interval_passed))) {
Tomas Winklere1623442009-01-27 14:27:56 -08002170 IWL_DEBUG_RATE(priv, "LQ: stay is expired %d %d %d\n:",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002171 lq_sta->total_failed,
2172 lq_sta->total_success,
Zhu Yib481de92007-09-25 17:54:57 -07002173 flush_interval_passed);
Ben Cahill77626352007-11-29 11:09:44 +08002174
2175 /* Allow search for new mode */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002176 lq_sta->stay_in_tbl = 0; /* only place reset */
2177 lq_sta->total_failed = 0;
2178 lq_sta->total_success = 0;
2179 lq_sta->flush_timer = 0;
Ben Cahill77626352007-11-29 11:09:44 +08002180
2181 /*
2182 * Else if we've used this modulation mode enough repetitions
2183 * (regardless of elapsed time or success/failure), reset
2184 * history bitmaps and rate-specific stats for all rates in
2185 * active table.
2186 */
Mohamed Abbas403ab562007-11-06 22:06:25 -08002187 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002188 lq_sta->table_count++;
2189 if (lq_sta->table_count >=
2190 lq_sta->table_count_limit) {
2191 lq_sta->table_count = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002192
Tomas Winklere1623442009-01-27 14:27:56 -08002193 IWL_DEBUG_RATE(priv, "LQ: stay in table clear win\n");
Zhu Yib481de92007-09-25 17:54:57 -07002194 for (i = 0; i < IWL_RATE_COUNT; i++)
2195 rs_rate_scale_clear_window(
2196 &(tbl->win[i]));
2197 }
2198 }
2199
Ben Cahill77626352007-11-29 11:09:44 +08002200 /* If transitioning to allow "search", reset all history
2201 * bitmaps and stats in active table (this will become the new
2202 * "search" table). */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002203 if (!lq_sta->stay_in_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07002204 for (i = 0; i < IWL_RATE_COUNT; i++)
2205 rs_rate_scale_clear_window(&(tbl->win[i]));
2206 }
2207 }
2208}
2209
Ben Cahill77626352007-11-29 11:09:44 +08002210/*
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07002211 * setup rate table in uCode
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07002212 */
Johannes Berg70817b52011-05-11 03:29:25 -07002213static void rs_update_rate_tbl(struct iwl_priv *priv,
2214 struct iwl_rxon_context *ctx,
2215 struct iwl_lq_sta *lq_sta,
2216 struct iwl_scale_tbl_info *tbl,
2217 int index, u8 is_green)
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07002218{
2219 u32 rate;
2220
2221 /* Update uCode's rate table. */
2222 rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
2223 rs_fill_link_cmd(priv, lq_sta, rate);
Johannes Berg7e6a5882010-08-23 10:46:46 +02002224 iwl_send_lq_cmd(priv, ctx, &lq_sta->lq, CMD_ASYNC, false);
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07002225}
2226
2227/*
Ben Cahill77626352007-11-29 11:09:44 +08002228 * Do rate scaling and search for new modulation mode.
2229 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002230static void rs_rate_scale_perform(struct iwl_priv *priv,
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002231 struct sk_buff *skb,
Johannes Berg4b7679a2008-09-18 18:14:18 +02002232 struct ieee80211_sta *sta,
2233 struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002234{
Johannes Berg4b7679a2008-09-18 18:14:18 +02002235 struct ieee80211_hw *hw = priv->hw;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002236 struct ieee80211_conf *conf = &hw->conf;
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002237 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2238 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002239 int low = IWL_RATE_INVALID;
2240 int high = IWL_RATE_INVALID;
2241 int index;
2242 int i;
Tomas Winklere227cea2008-07-18 13:53:05 +08002243 struct iwl_rate_scale_data *window = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002244 int current_tpt = IWL_INVALID_VALUE;
2245 int low_tpt = IWL_INVALID_VALUE;
2246 int high_tpt = IWL_INVALID_VALUE;
2247 u32 fail_count;
2248 s8 scale_action = 0;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002249 u16 rate_mask;
Zhu Yib481de92007-09-25 17:54:57 -07002250 u8 update_lq = 0;
Tomas Winklere227cea2008-07-18 13:53:05 +08002251 struct iwl_scale_tbl_info *tbl, *tbl1;
Zhu Yib481de92007-09-25 17:54:57 -07002252 u16 rate_scale_index_msk = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002253 u8 is_green = 0;
2254 u8 active_tbl = 0;
2255 u8 done_search = 0;
2256 u16 high_low;
Guy Cohena5e8b502008-05-29 16:35:11 +08002257 s32 sr;
Emmanuel Grumbach5f85a782011-08-25 23:11:18 -07002258 u8 tid = IWL_MAX_TID_COUNT;
Wey-Yi Guy86b47662009-03-10 14:35:09 -07002259 struct iwl_tid_data *tid_data;
Johannes Berg7e6a5882010-08-23 10:46:46 +02002260 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
Johannes Bergc6892902011-09-20 15:37:21 -07002261 struct iwl_rxon_context *ctx = sta_priv->ctx;
Zhu Yib481de92007-09-25 17:54:57 -07002262
Tomas Winklere1623442009-01-27 14:27:56 -08002263 IWL_DEBUG_RATE(priv, "rate scale calculate new rate for skb\n");
Zhu Yib481de92007-09-25 17:54:57 -07002264
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002265 /* Send management frames and NO_ACK data using lowest rate. */
Tomas Winkler417f1142008-11-12 13:14:06 -08002266 /* TODO: this could probably be improved.. */
2267 if (!ieee80211_is_data(hdr->frame_control) ||
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002268 info->flags & IEEE80211_TX_CTL_NO_ACK)
Zhu Yib481de92007-09-25 17:54:57 -07002269 return;
Zhu Yib481de92007-09-25 17:54:57 -07002270
Johannes Berg4b7679a2008-09-18 18:14:18 +02002271 lq_sta->supp_rates = sta->supp_rates[lq_sta->band];
Zhu Yib481de92007-09-25 17:54:57 -07002272
Ron Rindjunskyfaa29712008-06-12 09:46:58 +08002273 tid = rs_tl_add_packet(lq_sta, hdr);
Emmanuel Grumbach5f85a782011-08-25 23:11:18 -07002274 if ((tid != IWL_MAX_TID_COUNT) &&
2275 (lq_sta->tx_agg_tid_en & (1 << tid))) {
Emmanuel Grumbach04cf6822011-11-23 11:06:12 +02002276 tid_data = &priv->tid_data[lq_sta->lq.sta_id][tid];
Daniel C Halperine3949d62009-09-17 10:43:50 -07002277 if (tid_data->agg.state == IWL_AGG_OFF)
2278 lq_sta->is_agg = 0;
2279 else
2280 lq_sta->is_agg = 1;
2281 } else
2282 lq_sta->is_agg = 0;
Ron Rindjunskyfaa29712008-06-12 09:46:58 +08002283
Ben Cahill77626352007-11-29 11:09:44 +08002284 /*
2285 * Select rate-scale / modulation-mode table to work with in
2286 * the rest of this function: "search" if searching for better
2287 * modulation mode, or "active" if doing rate scaling within a mode.
2288 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002289 if (!lq_sta->search_better_tbl)
2290 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002291 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002292 active_tbl = 1 - lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002293
Tomas Winklerc33104f2008-01-14 17:46:21 -08002294 tbl = &(lq_sta->lq_info[active_tbl]);
Wey-Yi Guy2681b202009-05-21 13:44:22 -07002295 if (is_legacy(tbl->lq_type))
2296 lq_sta->is_green = 0;
2297 else
Johannes Berg7e6a5882010-08-23 10:46:46 +02002298 lq_sta->is_green = rs_use_green(sta);
Tomas Winklerc33104f2008-01-14 17:46:21 -08002299 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07002300
Ben Cahill77626352007-11-29 11:09:44 +08002301 /* current tx rate */
Johannes Bergb7e35002008-09-11 02:22:58 +02002302 index = lq_sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002303
Tomas Winklere1623442009-01-27 14:27:56 -08002304 IWL_DEBUG_RATE(priv, "Rate scale index %d for type %d\n", index,
Zhu Yib481de92007-09-25 17:54:57 -07002305 tbl->lq_type);
2306
Ben Cahill77626352007-11-29 11:09:44 +08002307 /* rates available for this association, and for modulation mode */
Guy Coheneecd6e52008-04-23 17:14:58 -07002308 rate_mask = rs_get_supported_rates(lq_sta, hdr, tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -07002309
Frans Pop91dd6c22010-03-24 14:19:58 -07002310 IWL_DEBUG_RATE(priv, "mask 0x%04X\n", rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07002311
2312 /* mask with station rate restriction */
2313 if (is_legacy(tbl->lq_type)) {
Johannes Berg8318d782008-01-24 19:38:38 +01002314 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Ben Cahill77626352007-11-29 11:09:44 +08002315 /* supp_rates has no CCK bits in A mode */
Zhu Yib481de92007-09-25 17:54:57 -07002316 rate_scale_index_msk = (u16) (rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -08002317 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
Zhu Yib481de92007-09-25 17:54:57 -07002318 else
2319 rate_scale_index_msk = (u16) (rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -08002320 lq_sta->supp_rates);
Zhu Yib481de92007-09-25 17:54:57 -07002321
2322 } else
2323 rate_scale_index_msk = rate_mask;
2324
2325 if (!rate_scale_index_msk)
2326 rate_scale_index_msk = rate_mask;
2327
Guy Cohen07bc28e2008-04-23 17:15:03 -07002328 if (!((1 << index) & rate_scale_index_msk)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002329 IWL_ERR(priv, "Current Rate is not valid\n");
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07002330 if (lq_sta->search_better_tbl) {
2331 /* revert to active table if search table is not valid*/
2332 tbl->lq_type = LQ_NONE;
2333 lq_sta->search_better_tbl = 0;
2334 tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
2335 /* get "active" rate info */
2336 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
Johannes Berg70817b52011-05-11 03:29:25 -07002337 rs_update_rate_tbl(priv, ctx, lq_sta, tbl,
2338 index, is_green);
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07002339 }
Guy Cohen07bc28e2008-04-23 17:15:03 -07002340 return;
Zhu Yib481de92007-09-25 17:54:57 -07002341 }
2342
Ben Cahill77626352007-11-29 11:09:44 +08002343 /* Get expected throughput table and history window for current rate */
Guy Cohen07bc28e2008-04-23 17:15:03 -07002344 if (!tbl->expected_tpt) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002345 IWL_ERR(priv, "tbl->expected_tpt is NULL\n");
Guy Cohen07bc28e2008-04-23 17:15:03 -07002346 return;
2347 }
Zhu Yib481de92007-09-25 17:54:57 -07002348
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002349 /* force user max rate if set by user */
2350 if ((lq_sta->max_rate_idx != -1) &&
2351 (lq_sta->max_rate_idx < index)) {
2352 index = lq_sta->max_rate_idx;
2353 update_lq = 1;
2354 window = &(tbl->win[index]);
2355 goto lq_update;
2356 }
2357
Zhu Yib481de92007-09-25 17:54:57 -07002358 window = &(tbl->win[index]);
2359
Ben Cahill77626352007-11-29 11:09:44 +08002360 /*
2361 * If there is not enough history to calculate actual average
2362 * throughput, keep analyzing results of more tx frames, without
2363 * changing rate or mode (bypass most of the rest of this function).
2364 * Set up new rate table in uCode only if old rate is not supported
2365 * in current association (use new rate found above).
2366 */
Zhu Yib481de92007-09-25 17:54:57 -07002367 fail_count = window->counter - window->success_counter;
Guy Cohen07bc28e2008-04-23 17:15:03 -07002368 if ((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
2369 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002370 IWL_DEBUG_RATE(priv, "LQ: still below TH. succ=%d total=%d "
Zhu Yib481de92007-09-25 17:54:57 -07002371 "for index %d\n",
2372 window->success_counter, window->counter, index);
Ben Cahill77626352007-11-29 11:09:44 +08002373
2374 /* Can't calculate this yet; not enough history */
Zhu Yib481de92007-09-25 17:54:57 -07002375 window->average_tpt = IWL_INVALID_VALUE;
Ben Cahill77626352007-11-29 11:09:44 +08002376
2377 /* Should we stay with this modulation mode,
2378 * or search for a new one? */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07002379 rs_stay_in_table(lq_sta, false);
Ben Cahill77626352007-11-29 11:09:44 +08002380
Zhu Yib481de92007-09-25 17:54:57 -07002381 goto out;
Guy Cohen3110bef2008-09-09 10:54:54 +08002382 }
Ben Cahill77626352007-11-29 11:09:44 +08002383 /* Else we have enough samples; calculate estimate of
2384 * actual average throughput */
Reinette Chatre3d79b2a2010-05-03 10:55:07 -07002385 if (window->average_tpt != ((window->success_ratio *
2386 tbl->expected_tpt[index] + 64) / 128)) {
2387 IWL_ERR(priv, "expected_tpt should have been calculated by now\n");
2388 window->average_tpt = ((window->success_ratio *
2389 tbl->expected_tpt[index] + 64) / 128);
2390 }
Zhu Yib481de92007-09-25 17:54:57 -07002391
Ben Cahill77626352007-11-29 11:09:44 +08002392 /* If we are searching for better modulation mode, check success. */
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002393 if (lq_sta->search_better_tbl &&
Johannes Berg3ad3b922009-08-07 15:41:48 -07002394 (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_MULTI)) {
Ben Cahill77626352007-11-29 11:09:44 +08002395 /* If good success, continue using the "search" mode;
2396 * no need to send new link quality command, since we're
2397 * continuing to use the setup that we've been trying. */
Guy Cohen07bc28e2008-04-23 17:15:03 -07002398 if (window->average_tpt > lq_sta->last_tpt) {
2399
Tomas Winklere1623442009-01-27 14:27:56 -08002400 IWL_DEBUG_RATE(priv, "LQ: SWITCHING TO NEW TABLE "
Guy Cohen07bc28e2008-04-23 17:15:03 -07002401 "suc=%d cur-tpt=%d old-tpt=%d\n",
2402 window->success_ratio,
2403 window->average_tpt,
2404 lq_sta->last_tpt);
2405
2406 if (!is_legacy(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08002407 lq_sta->enable_counter = 1;
Guy Cohen07bc28e2008-04-23 17:15:03 -07002408
Ben Cahill77626352007-11-29 11:09:44 +08002409 /* Swap tables; "search" becomes "active" */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002410 lq_sta->active_tbl = active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002411 current_tpt = window->average_tpt;
Ben Cahill77626352007-11-29 11:09:44 +08002412
2413 /* Else poor success; go back to mode in "active" table */
Zhu Yib481de92007-09-25 17:54:57 -07002414 } else {
Guy Cohen07bc28e2008-04-23 17:15:03 -07002415
Tomas Winklere1623442009-01-27 14:27:56 -08002416 IWL_DEBUG_RATE(priv, "LQ: GOING BACK TO THE OLD TABLE "
Guy Cohen07bc28e2008-04-23 17:15:03 -07002417 "suc=%d cur-tpt=%d old-tpt=%d\n",
2418 window->success_ratio,
2419 window->average_tpt,
2420 lq_sta->last_tpt);
2421
Ben Cahill77626352007-11-29 11:09:44 +08002422 /* Nullify "search" table */
Zhu Yib481de92007-09-25 17:54:57 -07002423 tbl->lq_type = LQ_NONE;
Ben Cahill77626352007-11-29 11:09:44 +08002424
2425 /* Revert to "active" table */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002426 active_tbl = lq_sta->active_tbl;
2427 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07002428
Ben Cahill77626352007-11-29 11:09:44 +08002429 /* Revert to "active" rate and throughput info */
Tomas Winklere7d326ac2008-06-12 09:47:11 +08002430 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
Tomas Winklerc33104f2008-01-14 17:46:21 -08002431 current_tpt = lq_sta->last_tpt;
Ben Cahill77626352007-11-29 11:09:44 +08002432
2433 /* Need to set up a new rate table in uCode */
2434 update_lq = 1;
Zhu Yib481de92007-09-25 17:54:57 -07002435 }
Ben Cahill77626352007-11-29 11:09:44 +08002436
2437 /* Either way, we've made a decision; modulation mode
2438 * search is done, allow rate adjustment next time. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002439 lq_sta->search_better_tbl = 0;
Ben Cahill77626352007-11-29 11:09:44 +08002440 done_search = 1; /* Don't switch modes below! */
Zhu Yib481de92007-09-25 17:54:57 -07002441 goto lq_update;
2442 }
2443
Ben Cahill77626352007-11-29 11:09:44 +08002444 /* (Else) not in search of better modulation mode, try for better
2445 * starting rate, while staying in this mode. */
Ester Kummerbf403db2008-05-05 10:22:40 +08002446 high_low = rs_get_adjacent_rate(priv, index, rate_scale_index_msk,
Zhu Yib481de92007-09-25 17:54:57 -07002447 tbl->lq_type);
2448 low = high_low & 0xff;
2449 high = (high_low >> 8) & 0xff;
2450
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002451 /* If user set max rate, dont allow higher than user constrain */
2452 if ((lq_sta->max_rate_idx != -1) &&
2453 (lq_sta->max_rate_idx < high))
2454 high = IWL_RATE_INVALID;
2455
Guy Cohena5e8b502008-05-29 16:35:11 +08002456 sr = window->success_ratio;
2457
Ben Cahill77626352007-11-29 11:09:44 +08002458 /* Collect measured throughputs for current and adjacent rates */
Zhu Yib481de92007-09-25 17:54:57 -07002459 current_tpt = window->average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07002460 if (low != IWL_RATE_INVALID)
2461 low_tpt = tbl->win[low].average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07002462 if (high != IWL_RATE_INVALID)
2463 high_tpt = tbl->win[high].average_tpt;
2464
Guy Cohena5e8b502008-05-29 16:35:11 +08002465 scale_action = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002466
Ben Cahill77626352007-11-29 11:09:44 +08002467 /* Too many failures, decrease rate */
Guy Cohena5e8b502008-05-29 16:35:11 +08002468 if ((sr <= IWL_RATE_DECREASE_TH) || (current_tpt == 0)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002469 IWL_DEBUG_RATE(priv, "decrease rate because of low success_ratio\n");
Zhu Yib481de92007-09-25 17:54:57 -07002470 scale_action = -1;
Ben Cahill77626352007-11-29 11:09:44 +08002471
2472 /* No throughput measured yet for adjacent rates; try increase. */
Zhu Yib481de92007-09-25 17:54:57 -07002473 } else if ((low_tpt == IWL_INVALID_VALUE) &&
Guy Cohena5e8b502008-05-29 16:35:11 +08002474 (high_tpt == IWL_INVALID_VALUE)) {
2475
2476 if (high != IWL_RATE_INVALID && sr >= IWL_RATE_INCREASE_TH)
2477 scale_action = 1;
2478 else if (low != IWL_RATE_INVALID)
Wey-Yi Guy8fe72312009-03-10 14:35:10 -07002479 scale_action = 0;
Guy Cohena5e8b502008-05-29 16:35:11 +08002480 }
Ben Cahill77626352007-11-29 11:09:44 +08002481
2482 /* Both adjacent throughputs are measured, but neither one has better
2483 * throughput; we're using the best rate, don't change it! */
Zhu Yib481de92007-09-25 17:54:57 -07002484 else if ((low_tpt != IWL_INVALID_VALUE) &&
2485 (high_tpt != IWL_INVALID_VALUE) &&
2486 (low_tpt < current_tpt) &&
2487 (high_tpt < current_tpt))
2488 scale_action = 0;
Ben Cahill77626352007-11-29 11:09:44 +08002489
2490 /* At least one adjacent rate's throughput is measured,
2491 * and may have better performance. */
Zhu Yib481de92007-09-25 17:54:57 -07002492 else {
Ben Cahill77626352007-11-29 11:09:44 +08002493 /* Higher adjacent rate's throughput is measured */
Zhu Yib481de92007-09-25 17:54:57 -07002494 if (high_tpt != IWL_INVALID_VALUE) {
Ben Cahill77626352007-11-29 11:09:44 +08002495 /* Higher rate has better throughput */
Guy Cohena5e8b502008-05-29 16:35:11 +08002496 if (high_tpt > current_tpt &&
2497 sr >= IWL_RATE_INCREASE_TH) {
Zhu Yib481de92007-09-25 17:54:57 -07002498 scale_action = 1;
Guy Cohena5e8b502008-05-29 16:35:11 +08002499 } else {
Wey-Yi Guy8fe72312009-03-10 14:35:10 -07002500 scale_action = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002501 }
Ben Cahill77626352007-11-29 11:09:44 +08002502
2503 /* Lower adjacent rate's throughput is measured */
Zhu Yib481de92007-09-25 17:54:57 -07002504 } else if (low_tpt != IWL_INVALID_VALUE) {
Ben Cahill77626352007-11-29 11:09:44 +08002505 /* Lower rate has better throughput */
Zhu Yib481de92007-09-25 17:54:57 -07002506 if (low_tpt > current_tpt) {
Tomas Winklere1623442009-01-27 14:27:56 -08002507 IWL_DEBUG_RATE(priv,
2508 "decrease rate because of low tpt\n");
Zhu Yib481de92007-09-25 17:54:57 -07002509 scale_action = -1;
Guy Cohena5e8b502008-05-29 16:35:11 +08002510 } else if (sr >= IWL_RATE_INCREASE_TH) {
Zhu Yib481de92007-09-25 17:54:57 -07002511 scale_action = 1;
Guy Cohena5e8b502008-05-29 16:35:11 +08002512 }
Zhu Yib481de92007-09-25 17:54:57 -07002513 }
2514 }
2515
Ben Cahill77626352007-11-29 11:09:44 +08002516 /* Sanity check; asked for decrease, but success rate or throughput
2517 * has been good at old rate. Don't change it. */
Guy Cohena5e8b502008-05-29 16:35:11 +08002518 if ((scale_action == -1) && (low != IWL_RATE_INVALID) &&
2519 ((sr > IWL_RATE_HIGH_TH) ||
Zhu Yib481de92007-09-25 17:54:57 -07002520 (current_tpt > (100 * tbl->expected_tpt[low]))))
Zhu Yib481de92007-09-25 17:54:57 -07002521 scale_action = 0;
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002522 if (!iwl_ht_enabled(priv) && !is_legacy(tbl->lq_type))
2523 scale_action = -1;
Johannes Berg3ad3b922009-08-07 15:41:48 -07002524 if (iwl_tx_ant_restriction(priv) != IWL_ANT_OK_MULTI &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002525 (is_mimo2(tbl->lq_type) || is_mimo3(tbl->lq_type)))
2526 scale_action = -1;
Johannes Berg290f5992010-08-23 07:56:58 -07002527
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07002528 if ((priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH) &&
2529 (is_mimo2(tbl->lq_type) || is_mimo3(tbl->lq_type))) {
2530 if (lq_sta->last_bt_traffic > priv->bt_traffic_load) {
2531 /*
2532 * don't set scale_action, don't want to scale up if
2533 * the rate scale doesn't otherwise think that is a
2534 * good idea.
2535 */
2536 } else if (lq_sta->last_bt_traffic <= priv->bt_traffic_load) {
2537 scale_action = -1;
2538 }
2539 }
2540 lq_sta->last_bt_traffic = priv->bt_traffic_load;
2541
2542 if ((priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH) &&
2543 (is_mimo2(tbl->lq_type) || is_mimo3(tbl->lq_type))) {
2544 /* search for a new modulation */
2545 rs_stay_in_table(lq_sta, true);
2546 goto lq_update;
Johannes Berg290f5992010-08-23 07:56:58 -07002547 }
2548
Zhu Yib481de92007-09-25 17:54:57 -07002549 switch (scale_action) {
2550 case -1:
Ben Cahill77626352007-11-29 11:09:44 +08002551 /* Decrease starting rate, update uCode's rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002552 if (low != IWL_RATE_INVALID) {
2553 update_lq = 1;
2554 index = low;
2555 }
Mohamed Abbas447fee72009-04-20 14:37:02 -07002556
Zhu Yib481de92007-09-25 17:54:57 -07002557 break;
2558 case 1:
Ben Cahill77626352007-11-29 11:09:44 +08002559 /* Increase starting rate, update uCode's rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002560 if (high != IWL_RATE_INVALID) {
2561 update_lq = 1;
2562 index = high;
2563 }
2564
2565 break;
2566 case 0:
Ben Cahill77626352007-11-29 11:09:44 +08002567 /* No change */
Zhu Yib481de92007-09-25 17:54:57 -07002568 default:
2569 break;
2570 }
2571
Tomas Winklere1623442009-01-27 14:27:56 -08002572 IWL_DEBUG_RATE(priv, "choose rate scale index %d action %d low %d "
Zhu Yib481de92007-09-25 17:54:57 -07002573 "high %d type %d\n",
2574 index, scale_action, low, high, tbl->lq_type);
2575
Guy Cohena5e8b502008-05-29 16:35:11 +08002576lq_update:
Ben Cahill77626352007-11-29 11:09:44 +08002577 /* Replace uCode's rate table for the destination station. */
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07002578 if (update_lq)
Johannes Berg70817b52011-05-11 03:29:25 -07002579 rs_update_rate_tbl(priv, ctx, lq_sta, tbl, index, is_green);
Ben Cahill77626352007-11-29 11:09:44 +08002580
Johannes Berg3ad3b922009-08-07 15:41:48 -07002581 if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_MULTI) {
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002582 /* Should we stay with this modulation mode,
2583 * or search for a new one? */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07002584 rs_stay_in_table(lq_sta, false);
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002585 }
Ben Cahill77626352007-11-29 11:09:44 +08002586 /*
2587 * Search for new modulation mode if we're:
2588 * 1) Not changing rates right now
2589 * 2) Not just finishing up a search
2590 * 3) Allowing a new search
2591 */
Guy Cohen47cfd462008-05-27 11:29:34 +08002592 if (!update_lq && !done_search && !lq_sta->stay_in_tbl && window->counter) {
Ben Cahill77626352007-11-29 11:09:44 +08002593 /* Save current throughput to compare with "search" throughput*/
Tomas Winklerc33104f2008-01-14 17:46:21 -08002594 lq_sta->last_tpt = current_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07002595
Ben Cahill77626352007-11-29 11:09:44 +08002596 /* Select a new "search" modulation mode to try.
2597 * If one is found, set up the new "search" table. */
Zhu Yib481de92007-09-25 17:54:57 -07002598 if (is_legacy(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08002599 rs_move_legacy_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07002600 else if (is_siso(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08002601 rs_move_siso_to_other(priv, lq_sta, conf, sta, index);
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07002602 else if (is_mimo2(tbl->lq_type))
2603 rs_move_mimo2_to_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07002604 else
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07002605 rs_move_mimo3_to_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07002606
Ben Cahill77626352007-11-29 11:09:44 +08002607 /* If new "search" mode was selected, set up in uCode table */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002608 if (lq_sta->search_better_tbl) {
Ben Cahill77626352007-11-29 11:09:44 +08002609 /* Access the "search" table, clear its history. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002610 tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Zhu Yib481de92007-09-25 17:54:57 -07002611 for (i = 0; i < IWL_RATE_COUNT; i++)
2612 rs_rate_scale_clear_window(&(tbl->win[i]));
2613
Ben Cahill77626352007-11-29 11:09:44 +08002614 /* Use new "search" start rate */
Tomas Winklere7d326ac2008-06-12 09:47:11 +08002615 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002616
Tomas Winklere1623442009-01-27 14:27:56 -08002617 IWL_DEBUG_RATE(priv, "Switch current mcs: %X index: %d\n",
Guy Cohen39e88502008-04-23 17:14:57 -07002618 tbl->current_rate, index);
Guy Cohen07bc28e2008-04-23 17:15:03 -07002619 rs_fill_link_cmd(priv, lq_sta, tbl->current_rate);
Johannes Berg7e6a5882010-08-23 10:46:46 +02002620 iwl_send_lq_cmd(priv, ctx, &lq_sta->lq, CMD_ASYNC, false);
Mohamed Abbas447fee72009-04-20 14:37:02 -07002621 } else
2622 done_search = 1;
2623 }
Zhu Yib481de92007-09-25 17:54:57 -07002624
Mohamed Abbas447fee72009-04-20 14:37:02 -07002625 if (done_search && !lq_sta->stay_in_tbl) {
Ben Cahill77626352007-11-29 11:09:44 +08002626 /* If the "active" (non-search) mode was legacy,
2627 * and we've tried switching antennas,
2628 * but we haven't been able to try HT modes (not available),
2629 * stay with best antenna legacy modulation for a while
2630 * before next round of mode comparisons. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002631 tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
Luis R. Rodriguezde27e642008-12-23 15:58:44 -08002632 if (is_legacy(tbl1->lq_type) && !conf_is_ht(conf) &&
Mohamed Abbasd6e93392009-05-08 13:44:39 -07002633 lq_sta->action_counter > tbl1->max_search) {
Tomas Winklere1623442009-01-27 14:27:56 -08002634 IWL_DEBUG_RATE(priv, "LQ: STAY in legacy table\n");
Ester Kummerbf403db2008-05-05 10:22:40 +08002635 rs_set_stay_in_table(priv, 1, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002636 }
2637
Ben Cahill77626352007-11-29 11:09:44 +08002638 /* If we're in an HT mode, and all 3 mode switch actions
2639 * have been tried and compared, stay in this best modulation
2640 * mode for a while before next round of mode comparisons. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002641 if (lq_sta->enable_counter &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002642 (lq_sta->action_counter >= tbl1->max_search) &&
2643 iwl_ht_enabled(priv)) {
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002644 if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
2645 (lq_sta->tx_agg_tid_en & (1 << tid)) &&
Emmanuel Grumbach5f85a782011-08-25 23:11:18 -07002646 (tid != IWL_MAX_TID_COUNT)) {
2647 u8 sta_id = lq_sta->lq.sta_id;
Emmanuel Grumbach04cf6822011-11-23 11:06:12 +02002648 tid_data = &priv->tid_data[sta_id][tid];
Wey-Yi Guy86b47662009-03-10 14:35:09 -07002649 if (tid_data->agg.state == IWL_AGG_OFF) {
2650 IWL_DEBUG_RATE(priv,
2651 "try to aggregate tid %d\n",
2652 tid);
2653 rs_tl_turn_on_agg(priv, tid,
2654 lq_sta, sta);
2655 }
Zhu Yib481de92007-09-25 17:54:57 -07002656 }
Ester Kummerbf403db2008-05-05 10:22:40 +08002657 rs_set_stay_in_table(priv, 0, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002658 }
Zhu Yib481de92007-09-25 17:54:57 -07002659 }
2660
2661out:
Tomas Winkler978785a2008-12-19 10:37:31 +08002662 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
Wey-Yi Guyc3322b32011-10-10 07:26:58 -07002663 lq_sta->last_txrate_idx = index;
Zhu Yib481de92007-09-25 17:54:57 -07002664}
2665
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08002666/**
2667 * rs_initialize_lq - Initialize a station's hardware rate table
2668 *
2669 * The uCode's station table contains a table of fallback rates
2670 * for automatic fallback during transmission.
2671 *
2672 * NOTE: This sets up a default set of values. These will be replaced later
2673 * if the driver's iwl-agn-rs rate scaling algorithm is used, instead of
2674 * rc80211_simple.
2675 *
2676 * NOTE: Run REPLY_ADD_STA command to set up station table entry, before
2677 * calling this function (which runs REPLY_TX_LINK_QUALITY_CMD,
2678 * which requires station table entry to exist).
2679 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002680static void rs_initialize_lq(struct iwl_priv *priv,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002681 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02002682 struct ieee80211_sta *sta,
2683 struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002684{
Tomas Winklere227cea2008-07-18 13:53:05 +08002685 struct iwl_scale_tbl_info *tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002686 int rate_idx;
Guy Cohenaade00c2008-04-23 17:14:59 -07002687 int i;
Guy Cohen39e88502008-04-23 17:14:57 -07002688 u32 rate;
Johannes Berg7e6a5882010-08-23 10:46:46 +02002689 u8 use_green = rs_use_green(sta);
Guy Cohenaade00c2008-04-23 17:14:59 -07002690 u8 active_tbl = 0;
2691 u8 valid_tx_ant;
Johannes Berg7e6a5882010-08-23 10:46:46 +02002692 struct iwl_station_priv *sta_priv;
2693 struct iwl_rxon_context *ctx;
Zhu Yib481de92007-09-25 17:54:57 -07002694
Johannes Berg4b7679a2008-09-18 18:14:18 +02002695 if (!sta || !lq_sta)
Johannes Berg7e6a5882010-08-23 10:46:46 +02002696 return;
2697
2698 sta_priv = (void *)sta->drv_priv;
Johannes Bergc6892902011-09-20 15:37:21 -07002699 ctx = sta_priv->ctx;
Zhu Yib481de92007-09-25 17:54:57 -07002700
Johannes Bergb7e35002008-09-11 02:22:58 +02002701 i = lq_sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002702
Emmanuel Grumbachd6189122011-08-25 23:10:39 -07002703 valid_tx_ant = hw_params(priv).valid_tx_ant;
Guy Cohenaade00c2008-04-23 17:14:59 -07002704
Tomas Winklerc33104f2008-01-14 17:46:21 -08002705 if (!lq_sta->search_better_tbl)
2706 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002707 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002708 active_tbl = 1 - lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002709
Tomas Winklerc33104f2008-01-14 17:46:21 -08002710 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07002711
2712 if ((i < 0) || (i >= IWL_RATE_COUNT))
2713 i = 0;
2714
Tomas Winkler1826dcc2008-05-15 13:54:02 +08002715 rate = iwl_rates[i].plcp;
Winkler, Tomaseb48dca2008-10-29 14:05:48 -07002716 tbl->ant_type = first_antenna(valid_tx_ant);
2717 rate |= tbl->ant_type << RATE_MCS_ANT_POS;
Zhu Yib481de92007-09-25 17:54:57 -07002718
2719 if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
Guy Cohen39e88502008-04-23 17:14:57 -07002720 rate |= RATE_MCS_CCK_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002721
Guy Cohen39e88502008-04-23 17:14:57 -07002722 rs_get_tbl_info_from_mcs(rate, priv->band, tbl, &rate_idx);
Guy Cohenaade00c2008-04-23 17:14:59 -07002723 if (!rs_is_valid_ant(valid_tx_ant, tbl->ant_type))
2724 rs_toggle_antenna(valid_tx_ant, &rate, tbl);
Zhu Yib481de92007-09-25 17:54:57 -07002725
Tomas Winkler978785a2008-12-19 10:37:31 +08002726 rate = rate_n_flags_from_tbl(priv, tbl, rate_idx, use_green);
Guy Cohen39e88502008-04-23 17:14:57 -07002727 tbl->current_rate = rate;
Guy Coheneecd6e52008-04-23 17:14:58 -07002728 rs_set_expected_tpt_table(lq_sta, tbl);
Guy Cohen07bc28e2008-04-23 17:15:03 -07002729 rs_fill_link_cmd(NULL, lq_sta, rate);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08002730 priv->stations[lq_sta->lq.sta_id].lq = &lq_sta->lq;
Johannes Berg7e6a5882010-08-23 10:46:46 +02002731 iwl_send_lq_cmd(priv, ctx, &lq_sta->lq, CMD_SYNC, true);
Zhu Yib481de92007-09-25 17:54:57 -07002732}
2733
Johannes Berge6a98542008-10-21 12:40:02 +02002734static void rs_get_rate(void *priv_r, struct ieee80211_sta *sta, void *priv_sta,
2735 struct ieee80211_tx_rate_control *txrc)
Zhu Yib481de92007-09-25 17:54:57 -07002736{
2737
Johannes Berge6a98542008-10-21 12:40:02 +02002738 struct sk_buff *skb = txrc->skb;
2739 struct ieee80211_supported_band *sband = txrc->sband;
Reinette Chatref875f512010-04-05 10:43:10 -07002740 struct iwl_priv *priv __maybe_unused = (struct iwl_priv *)priv_r;
Johannes Berge6a98542008-10-21 12:40:02 +02002741 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Tomas Winkler417f1142008-11-12 13:14:06 -08002742 struct iwl_lq_sta *lq_sta = priv_sta;
2743 int rate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002744
Tomas Winklere1623442009-01-27 14:27:56 -08002745 IWL_DEBUG_RATE_LIMIT(priv, "rate scale calculate new rate for skb\n");
Zhu Yib481de92007-09-25 17:54:57 -07002746
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002747 /* Get max rate if user set max rate */
2748 if (lq_sta) {
2749 lq_sta->max_rate_idx = txrc->max_rate_idx;
2750 if ((sband->band == IEEE80211_BAND_5GHZ) &&
2751 (lq_sta->max_rate_idx != -1))
2752 lq_sta->max_rate_idx += IWL_FIRST_OFDM_RATE;
2753 if ((lq_sta->max_rate_idx < 0) ||
2754 (lq_sta->max_rate_idx >= IWL_RATE_COUNT))
2755 lq_sta->max_rate_idx = -1;
2756 }
2757
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08002758 /* Treat uninitialized rate scaling data same as non-existing. */
2759 if (lq_sta && !lq_sta->drv) {
2760 IWL_DEBUG_RATE(priv, "Rate scaling not initialized yet.\n");
2761 priv_sta = NULL;
2762 }
2763
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002764 /* Send management frames and NO_ACK data using lowest rate. */
Luis R. Rodriguez4c6d4f52009-07-16 10:05:41 -07002765 if (rate_control_send_low(sta, priv_sta, txrc))
Johannes Berg4b7679a2008-09-18 18:14:18 +02002766 return;
Zhu Yib481de92007-09-25 17:54:57 -07002767
Tomas Winkler417f1142008-11-12 13:14:06 -08002768 rate_idx = lq_sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002769
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002770 if (lq_sta->last_rate_n_flags & RATE_MCS_HT_MSK) {
Tomas Winkler417f1142008-11-12 13:14:06 -08002771 rate_idx -= IWL_FIRST_OFDM_RATE;
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002772 /* 6M and 9M shared same MCS index */
2773 rate_idx = (rate_idx > 0) ? (rate_idx - 1) : 0;
2774 if (rs_extract_rate(lq_sta->last_rate_n_flags) >=
2775 IWL_RATE_MIMO3_6M_PLCP)
2776 rate_idx = rate_idx + (2 * MCS_INDEX_PER_STREAM);
2777 else if (rs_extract_rate(lq_sta->last_rate_n_flags) >=
2778 IWL_RATE_MIMO2_6M_PLCP)
2779 rate_idx = rate_idx + MCS_INDEX_PER_STREAM;
2780 info->control.rates[0].flags = IEEE80211_TX_RC_MCS;
2781 if (lq_sta->last_rate_n_flags & RATE_MCS_SGI_MSK)
2782 info->control.rates[0].flags |= IEEE80211_TX_RC_SHORT_GI;
2783 if (lq_sta->last_rate_n_flags & RATE_MCS_DUP_MSK)
2784 info->control.rates[0].flags |= IEEE80211_TX_RC_DUP_DATA;
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07002785 if (lq_sta->last_rate_n_flags & RATE_MCS_HT40_MSK)
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002786 info->control.rates[0].flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
2787 if (lq_sta->last_rate_n_flags & RATE_MCS_GF_MSK)
2788 info->control.rates[0].flags |= IEEE80211_TX_RC_GREEN_FIELD;
2789 } else {
Daniel C Halperin50273092009-08-28 09:44:45 -07002790 /* Check for invalid rates */
2791 if ((rate_idx < 0) || (rate_idx >= IWL_RATE_COUNT_LEGACY) ||
2792 ((sband->band == IEEE80211_BAND_5GHZ) &&
2793 (rate_idx < IWL_FIRST_OFDM_RATE)))
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002794 rate_idx = rate_lowest_index(sband, sta);
Daniel C Halperin50273092009-08-28 09:44:45 -07002795 /* On valid 5 GHz rate, adjust index */
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002796 else if (sband->band == IEEE80211_BAND_5GHZ)
2797 rate_idx -= IWL_FIRST_OFDM_RATE;
Daniel C Halperin7ebaeff2009-08-21 13:34:20 -07002798 info->control.rates[0].flags = 0;
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002799 }
Tomas Winkler417f1142008-11-12 13:14:06 -08002800 info->control.rates[0].idx = rate_idx;
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002801
Zhu Yib481de92007-09-25 17:54:57 -07002802}
2803
Johannes Berg4b7679a2008-09-18 18:14:18 +02002804static void *rs_alloc_sta(void *priv_rate, struct ieee80211_sta *sta,
2805 gfp_t gfp)
Zhu Yib481de92007-09-25 17:54:57 -07002806{
Reinette Chatre8d9698b2009-10-16 14:25:55 -07002807 struct iwl_station_priv *sta_priv = (struct iwl_station_priv *) sta->drv_priv;
Ester Kummerbf403db2008-05-05 10:22:40 +08002808 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07002809
Ester Kummerbf403db2008-05-05 10:22:40 +08002810 priv = (struct iwl_priv *)priv_rate;
Tomas Winklere1623442009-01-27 14:27:56 -08002811 IWL_DEBUG_RATE(priv, "create station rate scale window\n");
Zhu Yib481de92007-09-25 17:54:57 -07002812
Johannes Berg08960de2011-04-05 09:41:53 -07002813 return &sta_priv->lq_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002814}
2815
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08002816/*
2817 * Called after adding a new station to initialize rate scaling
2818 */
2819void iwl_rs_rate_init(struct iwl_priv *priv, struct ieee80211_sta *sta, u8 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07002820{
2821 int i, j;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08002822 struct ieee80211_hw *hw = priv->hw;
Johannes Berg4b7679a2008-09-18 18:14:18 +02002823 struct ieee80211_conf *conf = &priv->hw->conf;
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002824 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08002825 struct iwl_station_priv *sta_priv;
2826 struct iwl_lq_sta *lq_sta;
2827 struct ieee80211_supported_band *sband;
Zhu Yib481de92007-09-25 17:54:57 -07002828
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08002829 sta_priv = (struct iwl_station_priv *) sta->drv_priv;
2830 lq_sta = &sta_priv->lq_sta;
2831 sband = hw->wiphy->bands[conf->channel->band];
2832
2833
2834 lq_sta->lq.sta_id = sta_id;
Reinette Chatre5ad13f82009-10-30 14:36:14 -07002835
2836 for (j = 0; j < LQ_SIZE; j++)
2837 for (i = 0; i < IWL_RATE_COUNT; i++)
2838 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
2839
Tomas Winklerc33104f2008-01-14 17:46:21 -08002840 lq_sta->flush_timer = 0;
Johannes Berg4b7679a2008-09-18 18:14:18 +02002841 lq_sta->supp_rates = sta->supp_rates[sband->band];
Zhu Yib481de92007-09-25 17:54:57 -07002842 for (j = 0; j < LQ_SIZE; j++)
2843 for (i = 0; i < IWL_RATE_COUNT; i++)
Tomas Winkler3ac7f142008-07-21 02:40:14 +03002844 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
Zhu Yib481de92007-09-25 17:54:57 -07002845
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08002846 IWL_DEBUG_RATE(priv, "LQ: *** rate scale station global init for station %d ***\n",
2847 sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07002848 /* TODO: what is a good starting rate for STA? About middle? Maybe not
2849 * the lowest or the highest rate.. Could consider using RSSI from
2850 * previous packets? Need to have IEEE 802.1X auth succeed immediately
2851 * after assoc.. */
2852
Tomas Winklerc33104f2008-01-14 17:46:21 -08002853 lq_sta->is_dup = 0;
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002854 lq_sta->max_rate_idx = -1;
Abbas, Mohamed7d049e52009-01-20 21:33:53 -08002855 lq_sta->missed_rate_counter = IWL_MISSED_RATE_MAX;
Johannes Berg7e6a5882010-08-23 10:46:46 +02002856 lq_sta->is_green = rs_use_green(sta);
Guy Cohen39e88502008-04-23 17:14:57 -07002857 lq_sta->active_legacy_rate = priv->active_rate & ~(0x1000);
Johannes Berg8318d782008-01-24 19:38:38 +01002858 lq_sta->band = priv->band;
Ben Cahill77626352007-11-29 11:09:44 +08002859 /*
2860 * active_siso_rate mask includes 9 MBits (bit 5), and CCK (bits 0-3),
2861 * supp_rates[] does not; shift to convert format, force 9 MBits off.
2862 */
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002863 lq_sta->active_siso_rate = ht_cap->mcs.rx_mask[0] << 1;
2864 lq_sta->active_siso_rate |= ht_cap->mcs.rx_mask[0] & 0x1;
Guy Cohenfde0db32008-04-21 15:42:01 -07002865 lq_sta->active_siso_rate &= ~((u16)0x2);
2866 lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -07002867
Ben Cahill77626352007-11-29 11:09:44 +08002868 /* Same here */
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002869 lq_sta->active_mimo2_rate = ht_cap->mcs.rx_mask[1] << 1;
2870 lq_sta->active_mimo2_rate |= ht_cap->mcs.rx_mask[1] & 0x1;
Guy Cohenfde0db32008-04-21 15:42:01 -07002871 lq_sta->active_mimo2_rate &= ~((u16)0x2);
2872 lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE;
2873
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002874 lq_sta->active_mimo3_rate = ht_cap->mcs.rx_mask[2] << 1;
2875 lq_sta->active_mimo3_rate |= ht_cap->mcs.rx_mask[2] & 0x1;
Guy Cohenfde0db32008-04-21 15:42:01 -07002876 lq_sta->active_mimo3_rate &= ~((u16)0x2);
2877 lq_sta->active_mimo3_rate <<= IWL_FIRST_OFDM_RATE;
2878
Tomas Winklere1623442009-01-27 14:27:56 -08002879 IWL_DEBUG_RATE(priv, "SISO-RATE=%X MIMO2-RATE=%X MIMO3-RATE=%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002880 lq_sta->active_siso_rate,
Guy Cohenfde0db32008-04-21 15:42:01 -07002881 lq_sta->active_mimo2_rate,
2882 lq_sta->active_mimo3_rate);
2883
Tomas Winklera96a27f2008-10-23 23:48:56 -07002884 /* These values will be overridden later */
Wey-Yi Guy3a23d692010-04-03 16:44:39 -07002885 lq_sta->lq.general_params.single_stream_ant_msk =
Emmanuel Grumbachd6189122011-08-25 23:10:39 -07002886 first_antenna(hw_params(priv).valid_tx_ant);
Wey-Yi Guy3a23d692010-04-03 16:44:39 -07002887 lq_sta->lq.general_params.dual_stream_ant_msk =
Emmanuel Grumbachd6189122011-08-25 23:10:39 -07002888 hw_params(priv).valid_tx_ant &
2889 ~first_antenna(hw_params(priv).valid_tx_ant);
Wey-Yi Guy3a23d692010-04-03 16:44:39 -07002890 if (!lq_sta->lq.general_params.dual_stream_ant_msk) {
2891 lq_sta->lq.general_params.dual_stream_ant_msk = ANT_AB;
Emmanuel Grumbachd6189122011-08-25 23:10:39 -07002892 } else if (num_of_ant(hw_params(priv).valid_tx_ant) == 2) {
Wey-Yi Guy3a23d692010-04-03 16:44:39 -07002893 lq_sta->lq.general_params.dual_stream_ant_msk =
Emmanuel Grumbachd6189122011-08-25 23:10:39 -07002894 hw_params(priv).valid_tx_ant;
Wey-Yi Guy3a23d692010-04-03 16:44:39 -07002895 }
Guy Cohen07bc28e2008-04-23 17:15:03 -07002896
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002897 /* as default allow aggregation for all tids */
2898 lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002899 lq_sta->drv = priv;
Zhu Yib481de92007-09-25 17:54:57 -07002900
Daniel C Halperin50273092009-08-28 09:44:45 -07002901 /* Set last_txrate_idx to lowest rate */
2902 lq_sta->last_txrate_idx = rate_lowest_index(sband, sta);
2903 if (sband->band == IEEE80211_BAND_5GHZ)
2904 lq_sta->last_txrate_idx += IWL_FIRST_OFDM_RATE;
Daniel C Halperine3949d62009-09-17 10:43:50 -07002905 lq_sta->is_agg = 0;
Wey-Yi Guy5ef15cc2011-11-30 13:24:06 -08002906#ifdef CONFIG_IWLWIFI_DEVICE_TESTMODE
Wey-Yi Guy4e308112011-07-08 08:46:28 -07002907 priv->tm_fixed_rate = 0;
Wey-Yi Guyc10e2c12011-07-13 11:13:46 -07002908#endif
Sedat Dilek7d5f01a2010-12-16 12:46:23 +01002909#ifdef CONFIG_MAC80211_DEBUGFS
Shanyu Zhaoa1da0772010-12-02 11:02:54 -08002910 lq_sta->dbg_fixed_rate = 0;
Sedat Dilek7d5f01a2010-12-16 12:46:23 +01002911#endif
Shanyu Zhaoa1da0772010-12-02 11:02:54 -08002912
Johannes Berg4b7679a2008-09-18 18:14:18 +02002913 rs_initialize_lq(priv, conf, sta, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002914}
2915
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002916static void rs_fill_link_cmd(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08002917 struct iwl_lq_sta *lq_sta, u32 new_rate)
Zhu Yib481de92007-09-25 17:54:57 -07002918{
Tomas Winklere227cea2008-07-18 13:53:05 +08002919 struct iwl_scale_tbl_info tbl_type;
Zhu Yib481de92007-09-25 17:54:57 -07002920 int index = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002921 int rate_idx;
Zhu Yi1b696de2007-09-27 11:27:41 +08002922 int repeat_rate = 0;
Guy Cohenaade00c2008-04-23 17:14:59 -07002923 u8 ant_toggle_cnt = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002924 u8 use_ht_possible = 1;
Guy Cohenaade00c2008-04-23 17:14:59 -07002925 u8 valid_tx_ant = 0;
Johannes Berg7b090682011-01-19 02:53:54 -08002926 struct iwl_station_priv *sta_priv =
2927 container_of(lq_sta, struct iwl_station_priv, lq_sta);
Guy Cohen07bc28e2008-04-23 17:15:03 -07002928 struct iwl_link_quality_cmd *lq_cmd = &lq_sta->lq;
Zhu Yib481de92007-09-25 17:54:57 -07002929
Ben Cahill77626352007-11-29 11:09:44 +08002930 /* Override starting rate (index 0) if needed for debug purposes */
Guy Cohen39e88502008-04-23 17:14:57 -07002931 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Zhu Yi98d7e092007-09-27 11:27:42 +08002932
Guy Cohen39e88502008-04-23 17:14:57 -07002933 /* Interpret new_rate (rate_n_flags) */
2934 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band,
Zhu Yib481de92007-09-25 17:54:57 -07002935 &tbl_type, &rate_idx);
2936
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07002937 if (priv && priv->bt_full_concurrent) {
2938 /* 1x1 only */
2939 tbl_type.ant_type =
Emmanuel Grumbachd6189122011-08-25 23:10:39 -07002940 first_antenna(hw_params(priv).valid_tx_ant);
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07002941 }
2942
Ben Cahill77626352007-11-29 11:09:44 +08002943 /* How many times should we repeat the initial rate? */
Zhu Yib481de92007-09-25 17:54:57 -07002944 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07002945 ant_toggle_cnt = 1;
Zhu Yi1b696de2007-09-27 11:27:41 +08002946 repeat_rate = IWL_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002947 } else {
Daniel Halperin374920c2011-03-16 19:16:36 -07002948 repeat_rate = min(IWL_HT_NUMBER_TRY,
2949 LINK_QUAL_AGG_DISABLE_START_DEF - 1);
Guy Cohenaade00c2008-04-23 17:14:59 -07002950 }
Zhu Yib481de92007-09-25 17:54:57 -07002951
2952 lq_cmd->general_params.mimo_delimiter =
2953 is_mimo(tbl_type.lq_type) ? 1 : 0;
Ben Cahill77626352007-11-29 11:09:44 +08002954
2955 /* Fill 1st table entry (index 0) */
Guy Cohen39e88502008-04-23 17:14:57 -07002956 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002957
Guy Cohen07bc28e2008-04-23 17:15:03 -07002958 if (num_of_ant(tbl_type.ant_type) == 1) {
2959 lq_cmd->general_params.single_stream_ant_msk =
2960 tbl_type.ant_type;
2961 } else if (num_of_ant(tbl_type.ant_type) == 2) {
2962 lq_cmd->general_params.dual_stream_ant_msk =
2963 tbl_type.ant_type;
2964 } /* otherwise we don't modify the existing value */
Zhu Yib481de92007-09-25 17:54:57 -07002965
2966 index++;
Zhu Yi1b696de2007-09-27 11:27:41 +08002967 repeat_rate--;
Wey-Yi Guybee008b2010-08-23 07:57:04 -07002968 if (priv) {
2969 if (priv->bt_full_concurrent)
2970 valid_tx_ant = ANT_A;
2971 else
Emmanuel Grumbachd6189122011-08-25 23:10:39 -07002972 valid_tx_ant = hw_params(priv).valid_tx_ant;
Wey-Yi Guybee008b2010-08-23 07:57:04 -07002973 }
Guy Cohenaade00c2008-04-23 17:14:59 -07002974
Ben Cahill77626352007-11-29 11:09:44 +08002975 /* Fill rest of rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002976 while (index < LINK_QUAL_MAX_RETRY_NUM) {
Ben Cahill77626352007-11-29 11:09:44 +08002977 /* Repeat initial/next rate.
2978 * For legacy IWL_NUMBER_TRY == 1, this loop will not execute.
2979 * For HT IWL_HT_NUMBER_TRY == 3, this executes twice. */
Zhu Yi1b696de2007-09-27 11:27:41 +08002980 while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
Zhu Yib481de92007-09-25 17:54:57 -07002981 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07002982 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2983 ant_toggle_cnt++;
2984 else if (priv &&
2985 rs_toggle_antenna(valid_tx_ant,
2986 &new_rate, &tbl_type))
2987 ant_toggle_cnt = 1;
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07002988 }
Zhu Yi98d7e092007-09-27 11:27:42 +08002989
Ben Cahill77626352007-11-29 11:09:44 +08002990 /* Override next rate if needed for debug purposes */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002991 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Ben Cahill77626352007-11-29 11:09:44 +08002992
2993 /* Fill next table entry */
Zhu Yib481de92007-09-25 17:54:57 -07002994 lq_cmd->rs_table[index].rate_n_flags =
Guy Cohen39e88502008-04-23 17:14:57 -07002995 cpu_to_le32(new_rate);
Zhu Yi1b696de2007-09-27 11:27:41 +08002996 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002997 index++;
2998 }
2999
Guy Cohen39e88502008-04-23 17:14:57 -07003000 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band, &tbl_type,
Zhu Yib481de92007-09-25 17:54:57 -07003001 &rate_idx);
3002
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07003003 if (priv && priv->bt_full_concurrent) {
3004 /* 1x1 only */
3005 tbl_type.ant_type =
Emmanuel Grumbachd6189122011-08-25 23:10:39 -07003006 first_antenna(hw_params(priv).valid_tx_ant);
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07003007 }
3008
Ben Cahill77626352007-11-29 11:09:44 +08003009 /* Indicate to uCode which entries might be MIMO.
3010 * If initial rate was MIMO, this will finally end up
3011 * as (IWL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
Zhu Yib481de92007-09-25 17:54:57 -07003012 if (is_mimo(tbl_type.lq_type))
3013 lq_cmd->general_params.mimo_delimiter = index;
3014
Ben Cahill77626352007-11-29 11:09:44 +08003015 /* Get next rate */
Guy Cohen39e88502008-04-23 17:14:57 -07003016 new_rate = rs_get_lower_rate(lq_sta, &tbl_type, rate_idx,
3017 use_ht_possible);
Zhu Yib481de92007-09-25 17:54:57 -07003018
Ben Cahill77626352007-11-29 11:09:44 +08003019 /* How many times should we repeat the next rate? */
Zhu Yib481de92007-09-25 17:54:57 -07003020 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07003021 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
3022 ant_toggle_cnt++;
3023 else if (priv &&
3024 rs_toggle_antenna(valid_tx_ant,
3025 &new_rate, &tbl_type))
3026 ant_toggle_cnt = 1;
3027
Zhu Yi1b696de2007-09-27 11:27:41 +08003028 repeat_rate = IWL_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07003029 } else {
Zhu Yi1b696de2007-09-27 11:27:41 +08003030 repeat_rate = IWL_HT_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07003031 }
Zhu Yib481de92007-09-25 17:54:57 -07003032
Ben Cahill77626352007-11-29 11:09:44 +08003033 /* Don't allow HT rates after next pass.
3034 * rs_get_lower_rate() will change type to LQ_A or LQ_G. */
Zhu Yib481de92007-09-25 17:54:57 -07003035 use_ht_possible = 0;
3036
Ben Cahill77626352007-11-29 11:09:44 +08003037 /* Override next rate if needed for debug purposes */
Tomas Winklerc33104f2008-01-14 17:46:21 -08003038 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Ben Cahill77626352007-11-29 11:09:44 +08003039
3040 /* Fill next table entry */
Guy Cohen39e88502008-04-23 17:14:57 -07003041 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
Zhu Yib481de92007-09-25 17:54:57 -07003042
3043 index++;
Zhu Yi1b696de2007-09-27 11:27:41 +08003044 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07003045 }
3046
Johannes Berg7b090682011-01-19 02:53:54 -08003047 lq_cmd->agg_params.agg_frame_cnt_limit =
3048 sta_priv->max_agg_bufsize ?: LINK_QUAL_AGG_FRAME_LIMIT_DEF;
Wey-Yi Guy13c33a02009-06-03 11:44:11 -07003049 lq_cmd->agg_params.agg_dis_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
Wey-Yi Guy74697012010-07-28 09:18:54 -07003050
Wey-Yi Guy13c33a02009-06-03 11:44:11 -07003051 lq_cmd->agg_params.agg_time_limit =
3052 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
Wey-Yi Guy74697012010-07-28 09:18:54 -07003053 /*
3054 * overwrite if needed, pass aggregation time limit
3055 * to uCode in uSec
3056 */
Don Fry38622412011-12-16 07:07:36 -08003057 if (priv && cfg(priv)->bt_params &&
3058 cfg(priv)->bt_params->agg_time_limit &&
Wey-Yi Guy66e863a52010-11-08 14:54:37 -08003059 priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH)
Wey-Yi Guy74697012010-07-28 09:18:54 -07003060 lq_cmd->agg_params.agg_time_limit =
Don Fry38622412011-12-16 07:07:36 -08003061 cpu_to_le16(cfg(priv)->bt_params->agg_time_limit);
Zhu Yib481de92007-09-25 17:54:57 -07003062}
3063
Johannes Berg4b7679a2008-09-18 18:14:18 +02003064static void *rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
Zhu Yib481de92007-09-25 17:54:57 -07003065{
Johannes Berg4b7679a2008-09-18 18:14:18 +02003066 return hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003067}
3068/* rate scale requires free function to be implemented */
3069static void rs_free(void *priv_rate)
3070{
3071 return;
3072}
3073
Johannes Berg4b7679a2008-09-18 18:14:18 +02003074static void rs_free_sta(void *priv_r, struct ieee80211_sta *sta,
3075 void *priv_sta)
Zhu Yib481de92007-09-25 17:54:57 -07003076{
Rami Rosen45527c22008-10-07 09:50:01 +02003077 struct iwl_priv *priv __maybe_unused = priv_r;
Zhu Yib481de92007-09-25 17:54:57 -07003078
Tomas Winklere1623442009-01-27 14:27:56 -08003079 IWL_DEBUG_RATE(priv, "enter\n");
Tomas Winklere1623442009-01-27 14:27:56 -08003080 IWL_DEBUG_RATE(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003081}
3082
Zhu Yi93dc6462007-09-27 11:27:37 +08003083#ifdef CONFIG_MAC80211_DEBUGFS
Zhu Yi5ae212c2007-09-27 11:27:38 +08003084static int open_file_generic(struct inode *inode, struct file *file)
3085{
3086 file->private_data = inode->i_private;
3087 return 0;
3088}
Tomas Winklere227cea2008-07-18 13:53:05 +08003089static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
3090 u32 *rate_n_flags, int index)
Zhu Yi98d7e092007-09-27 11:27:42 +08003091{
Ester Kummerbf403db2008-05-05 10:22:40 +08003092 struct iwl_priv *priv;
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07003093 u8 valid_tx_ant;
3094 u8 ant_sel_tx;
Ester Kummerbf403db2008-05-05 10:22:40 +08003095
3096 priv = lq_sta->drv;
Emmanuel Grumbachd6189122011-08-25 23:10:39 -07003097 valid_tx_ant = hw_params(priv).valid_tx_ant;
Guy Cohen39e88502008-04-23 17:14:57 -07003098 if (lq_sta->dbg_fixed_rate) {
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07003099 ant_sel_tx =
3100 ((lq_sta->dbg_fixed_rate & RATE_MCS_ANT_ABC_MSK)
3101 >> RATE_MCS_ANT_POS);
3102 if ((valid_tx_ant & ant_sel_tx) == ant_sel_tx) {
Guy Cohen39e88502008-04-23 17:14:57 -07003103 *rate_n_flags = lq_sta->dbg_fixed_rate;
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07003104 IWL_DEBUG_RATE(priv, "Fixed rate ON\n");
Guy Cohen39e88502008-04-23 17:14:57 -07003105 } else {
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07003106 lq_sta->dbg_fixed_rate = 0;
3107 IWL_ERR(priv,
3108 "Invalid antenna selection 0x%X, Valid is 0x%X\n",
3109 ant_sel_tx, valid_tx_ant);
3110 IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
Guy Cohen39e88502008-04-23 17:14:57 -07003111 }
Guy Cohen39e88502008-04-23 17:14:57 -07003112 } else {
Tomas Winklere1623442009-01-27 14:27:56 -08003113 IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
Zhu Yi98d7e092007-09-27 11:27:42 +08003114 }
Zhu Yi98d7e092007-09-27 11:27:42 +08003115}
3116
3117static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
3118 const char __user *user_buf, size_t count, loff_t *ppos)
3119{
Tomas Winklere227cea2008-07-18 13:53:05 +08003120 struct iwl_lq_sta *lq_sta = file->private_data;
Ester Kummerbf403db2008-05-05 10:22:40 +08003121 struct iwl_priv *priv;
Zhu Yi98d7e092007-09-27 11:27:42 +08003122 char buf[64];
Stephen Boyd805d7d22011-05-12 16:50:05 -07003123 size_t buf_size;
Zhu Yi98d7e092007-09-27 11:27:42 +08003124 u32 parsed_rate;
Wey-Yi Guy64898542011-05-03 18:05:13 -07003125
Zhu Yi98d7e092007-09-27 11:27:42 +08003126
Ester Kummerbf403db2008-05-05 10:22:40 +08003127 priv = lq_sta->drv;
Zhu Yi98d7e092007-09-27 11:27:42 +08003128 memset(buf, 0, sizeof(buf));
3129 buf_size = min(count, sizeof(buf) - 1);
3130 if (copy_from_user(buf, user_buf, buf_size))
3131 return -EFAULT;
3132
3133 if (sscanf(buf, "%x", &parsed_rate) == 1)
Wey-Yi Guy4e308112011-07-08 08:46:28 -07003134 lq_sta->dbg_fixed_rate = parsed_rate;
Zhu Yi98d7e092007-09-27 11:27:42 +08003135 else
Wey-Yi Guy4e308112011-07-08 08:46:28 -07003136 lq_sta->dbg_fixed_rate = 0;
Zhu Yi98d7e092007-09-27 11:27:42 +08003137
Wey-Yi Guy64898542011-05-03 18:05:13 -07003138 rs_program_fix_rate(priv, lq_sta);
Zhu Yi98d7e092007-09-27 11:27:42 +08003139
3140 return count;
3141}
Zhu Yi0209dc12007-09-27 11:27:43 +08003142
Zhu Yi5ae212c2007-09-27 11:27:38 +08003143static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
3144 char __user *user_buf, size_t count, loff_t *ppos)
3145{
Frank Seidela412c802009-03-01 20:25:38 +01003146 char *buff;
Zhu Yi5ae212c2007-09-27 11:27:38 +08003147 int desc = 0;
3148 int i = 0;
Wey-Yi Guy12b96812009-04-08 11:39:27 -07003149 int index = 0;
Frank Seidela412c802009-03-01 20:25:38 +01003150 ssize_t ret;
Zhu Yi5ae212c2007-09-27 11:27:38 +08003151
Tomas Winklere227cea2008-07-18 13:53:05 +08003152 struct iwl_lq_sta *lq_sta = file->private_data;
Wey-Yi Guyd8ae4f52009-03-10 14:35:07 -07003153 struct iwl_priv *priv;
Wey-Yi Guyadb7b5e2009-03-10 14:35:08 -07003154 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
Zhu Yi5ae212c2007-09-27 11:27:38 +08003155
Wey-Yi Guyd8ae4f52009-03-10 14:35:07 -07003156 priv = lq_sta->drv;
Frank Seidela412c802009-03-01 20:25:38 +01003157 buff = kmalloc(1024, GFP_KERNEL);
3158 if (!buff)
3159 return -ENOMEM;
3160
Tomas Winklerc33104f2008-01-14 17:46:21 -08003161 desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
Zhu Yi98d7e092007-09-27 11:27:42 +08003162 desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08003163 lq_sta->total_failed, lq_sta->total_success,
Guy Cohen39e88502008-04-23 17:14:57 -07003164 lq_sta->active_legacy_rate);
Zhu Yi98d7e092007-09-27 11:27:42 +08003165 desc += sprintf(buff+desc, "fixed rate 0x%X\n",
Wey-Yi Guy4e308112011-07-08 08:46:28 -07003166 lq_sta->dbg_fixed_rate);
Wey-Yi Guyd8ae4f52009-03-10 14:35:07 -07003167 desc += sprintf(buff+desc, "valid_tx_ant %s%s%s\n",
Emmanuel Grumbachd6189122011-08-25 23:10:39 -07003168 (hw_params(priv).valid_tx_ant & ANT_A) ? "ANT_A," : "",
3169 (hw_params(priv).valid_tx_ant & ANT_B) ? "ANT_B," : "",
3170 (hw_params(priv).valid_tx_ant & ANT_C) ? "ANT_C" : "");
Wey-Yi Guyadb7b5e2009-03-10 14:35:08 -07003171 desc += sprintf(buff+desc, "lq type %s\n",
3172 (is_legacy(tbl->lq_type)) ? "legacy" : "HT");
3173 if (is_Ht(tbl->lq_type)) {
3174 desc += sprintf(buff+desc, " %s",
3175 (is_siso(tbl->lq_type)) ? "SISO" :
3176 ((is_mimo2(tbl->lq_type)) ? "MIMO2" : "MIMO3"));
3177 desc += sprintf(buff+desc, " %s",
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07003178 (tbl->is_ht40) ? "40MHz" : "20MHz");
Daniel C Halperine3949d62009-09-17 10:43:50 -07003179 desc += sprintf(buff+desc, " %s %s %s\n", (tbl->is_SGI) ? "SGI" : "",
3180 (lq_sta->is_green) ? "GF enabled" : "",
3181 (lq_sta->is_agg) ? "AGG on" : "");
Wey-Yi Guyadb7b5e2009-03-10 14:35:08 -07003182 }
Wey-Yi Guy12b96812009-04-08 11:39:27 -07003183 desc += sprintf(buff+desc, "last tx rate=0x%X\n",
3184 lq_sta->last_rate_n_flags);
Zhu Yi5ae212c2007-09-27 11:27:38 +08003185 desc += sprintf(buff+desc, "general:"
3186 "flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08003187 lq_sta->lq.general_params.flags,
3188 lq_sta->lq.general_params.mimo_delimiter,
3189 lq_sta->lq.general_params.single_stream_ant_msk,
3190 lq_sta->lq.general_params.dual_stream_ant_msk);
Zhu Yi5ae212c2007-09-27 11:27:38 +08003191
3192 desc += sprintf(buff+desc, "agg:"
3193 "time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08003194 le16_to_cpu(lq_sta->lq.agg_params.agg_time_limit),
3195 lq_sta->lq.agg_params.agg_dis_start_th,
3196 lq_sta->lq.agg_params.agg_frame_cnt_limit);
Zhu Yi5ae212c2007-09-27 11:27:38 +08003197
3198 desc += sprintf(buff+desc,
3199 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08003200 lq_sta->lq.general_params.start_rate_index[0],
3201 lq_sta->lq.general_params.start_rate_index[1],
3202 lq_sta->lq.general_params.start_rate_index[2],
3203 lq_sta->lq.general_params.start_rate_index[3]);
Zhu Yi5ae212c2007-09-27 11:27:38 +08003204
Wey-Yi Guy12b96812009-04-08 11:39:27 -07003205 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
3206 index = iwl_hwrate_to_plcp_idx(
3207 le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags));
3208 if (is_legacy(tbl->lq_type)) {
3209 desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps\n",
3210 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags),
3211 iwl_rate_mcs[index].mbps);
3212 } else {
3213 desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps (%s)\n",
3214 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags),
3215 iwl_rate_mcs[index].mbps, iwl_rate_mcs[index].mcs);
3216 }
3217 }
Zhu Yi5ae212c2007-09-27 11:27:38 +08003218
Frank Seidela412c802009-03-01 20:25:38 +01003219 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
3220 kfree(buff);
3221 return ret;
Zhu Yi5ae212c2007-09-27 11:27:38 +08003222}
3223
3224static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
Zhu Yi98d7e092007-09-27 11:27:42 +08003225 .write = rs_sta_dbgfs_scale_table_write,
Zhu Yi5ae212c2007-09-27 11:27:38 +08003226 .read = rs_sta_dbgfs_scale_table_read,
3227 .open = open_file_generic,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003228 .llseek = default_llseek,
Zhu Yi5ae212c2007-09-27 11:27:38 +08003229};
Zhu Yi0209dc12007-09-27 11:27:43 +08003230static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
3231 char __user *user_buf, size_t count, loff_t *ppos)
3232{
Frank Seidela412c802009-03-01 20:25:38 +01003233 char *buff;
Zhu Yi0209dc12007-09-27 11:27:43 +08003234 int desc = 0;
3235 int i, j;
Frank Seidela412c802009-03-01 20:25:38 +01003236 ssize_t ret;
Zhu Yi0209dc12007-09-27 11:27:43 +08003237
Tomas Winklere227cea2008-07-18 13:53:05 +08003238 struct iwl_lq_sta *lq_sta = file->private_data;
Frank Seidela412c802009-03-01 20:25:38 +01003239
3240 buff = kmalloc(1024, GFP_KERNEL);
3241 if (!buff)
3242 return -ENOMEM;
3243
Zhu Yi0209dc12007-09-27 11:27:43 +08003244 for (i = 0; i < LQ_SIZE; i++) {
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07003245 desc += sprintf(buff+desc,
3246 "%s type=%d SGI=%d HT40=%d DUP=%d GF=%d\n"
Zhu Yi0209dc12007-09-27 11:27:43 +08003247 "rate=0x%X\n",
Abhijeet Kolekarc3056062008-11-12 13:14:08 -08003248 lq_sta->active_tbl == i ? "*" : "x",
Tomas Winklerc33104f2008-01-14 17:46:21 -08003249 lq_sta->lq_info[i].lq_type,
3250 lq_sta->lq_info[i].is_SGI,
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07003251 lq_sta->lq_info[i].is_ht40,
Tomas Winklerc33104f2008-01-14 17:46:21 -08003252 lq_sta->lq_info[i].is_dup,
Wey-Yi Guy2681b202009-05-21 13:44:22 -07003253 lq_sta->is_green,
Guy Cohen39e88502008-04-23 17:14:57 -07003254 lq_sta->lq_info[i].current_rate);
Zhu Yi0209dc12007-09-27 11:27:43 +08003255 for (j = 0; j < IWL_RATE_COUNT; j++) {
3256 desc += sprintf(buff+desc,
Tomas Winklerc33104f2008-01-14 17:46:21 -08003257 "counter=%d success=%d %%=%d\n",
3258 lq_sta->lq_info[i].win[j].counter,
3259 lq_sta->lq_info[i].win[j].success_counter,
3260 lq_sta->lq_info[i].win[j].success_ratio);
Zhu Yi0209dc12007-09-27 11:27:43 +08003261 }
3262 }
Frank Seidela412c802009-03-01 20:25:38 +01003263 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
3264 kfree(buff);
3265 return ret;
Zhu Yi0209dc12007-09-27 11:27:43 +08003266}
3267
3268static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
3269 .read = rs_sta_dbgfs_stats_table_read,
3270 .open = open_file_generic,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003271 .llseek = default_llseek,
Zhu Yi0209dc12007-09-27 11:27:43 +08003272};
Zhu Yi5ae212c2007-09-27 11:27:38 +08003273
Wey-Yi Guy38167452009-05-08 13:44:43 -07003274static ssize_t rs_sta_dbgfs_rate_scale_data_read(struct file *file,
3275 char __user *user_buf, size_t count, loff_t *ppos)
3276{
Johannes Berg70817b52011-05-11 03:29:25 -07003277 struct iwl_lq_sta *lq_sta = file->private_data;
3278 struct iwl_scale_tbl_info *tbl = &lq_sta->lq_info[lq_sta->active_tbl];
Wey-Yi Guy38167452009-05-08 13:44:43 -07003279 char buff[120];
3280 int desc = 0;
Wey-Yi Guy38167452009-05-08 13:44:43 -07003281
Wey-Yi Guy38167452009-05-08 13:44:43 -07003282 if (is_Ht(tbl->lq_type))
3283 desc += sprintf(buff+desc,
3284 "Bit Rate= %d Mb/s\n",
3285 tbl->expected_tpt[lq_sta->last_txrate_idx]);
3286 else
3287 desc += sprintf(buff+desc,
3288 "Bit Rate= %d Mb/s\n",
3289 iwl_rates[lq_sta->last_txrate_idx].ieee >> 1);
Wey-Yi Guy38167452009-05-08 13:44:43 -07003290
Johannes Berg08960de2011-04-05 09:41:53 -07003291 return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
Wey-Yi Guy38167452009-05-08 13:44:43 -07003292}
3293
3294static const struct file_operations rs_sta_dbgfs_rate_scale_data_ops = {
3295 .read = rs_sta_dbgfs_rate_scale_data_read,
3296 .open = open_file_generic,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003297 .llseek = default_llseek,
Wey-Yi Guy38167452009-05-08 13:44:43 -07003298};
3299
Zhu Yi93dc6462007-09-27 11:27:37 +08003300static void rs_add_debugfs(void *priv, void *priv_sta,
3301 struct dentry *dir)
3302{
Tomas Winklere227cea2008-07-18 13:53:05 +08003303 struct iwl_lq_sta *lq_sta = priv_sta;
Tomas Winklerc33104f2008-01-14 17:46:21 -08003304 lq_sta->rs_sta_dbgfs_scale_table_file =
Wey-Yi Guy43e85112009-11-20 12:04:58 -08003305 debugfs_create_file("rate_scale_table", S_IRUSR | S_IWUSR, dir,
Tomas Winklerc33104f2008-01-14 17:46:21 -08003306 lq_sta, &rs_sta_dbgfs_scale_table_ops);
3307 lq_sta->rs_sta_dbgfs_stats_table_file =
Wey-Yi Guy43e85112009-11-20 12:04:58 -08003308 debugfs_create_file("rate_stats_table", S_IRUSR, dir,
Tomas Winklerc33104f2008-01-14 17:46:21 -08003309 lq_sta, &rs_sta_dbgfs_stats_table_ops);
Wey-Yi Guy38167452009-05-08 13:44:43 -07003310 lq_sta->rs_sta_dbgfs_rate_scale_data_file =
Wey-Yi Guy43e85112009-11-20 12:04:58 -08003311 debugfs_create_file("rate_scale_data", S_IRUSR, dir,
Wey-Yi Guy38167452009-05-08 13:44:43 -07003312 lq_sta, &rs_sta_dbgfs_rate_scale_data_ops);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02003313 lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file =
Wey-Yi Guy43e85112009-11-20 12:04:58 -08003314 debugfs_create_u8("tx_agg_tid_enable", S_IRUSR | S_IWUSR, dir,
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02003315 &lq_sta->tx_agg_tid_en);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02003316
Zhu Yi93dc6462007-09-27 11:27:37 +08003317}
3318
3319static void rs_remove_debugfs(void *priv, void *priv_sta)
3320{
Tomas Winklere227cea2008-07-18 13:53:05 +08003321 struct iwl_lq_sta *lq_sta = priv_sta;
Tomas Winklerc33104f2008-01-14 17:46:21 -08003322 debugfs_remove(lq_sta->rs_sta_dbgfs_scale_table_file);
3323 debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
Wey-Yi Guy38167452009-05-08 13:44:43 -07003324 debugfs_remove(lq_sta->rs_sta_dbgfs_rate_scale_data_file);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02003325 debugfs_remove(lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file);
Zhu Yi93dc6462007-09-27 11:27:37 +08003326}
3327#endif
3328
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003329/*
3330 * Initialization of rate scaling information is done by driver after
3331 * the station is added. Since mac80211 calls this function before a
3332 * station is added we ignore it.
3333 */
3334static void rs_rate_init_stub(void *priv_r, struct ieee80211_supported_band *sband,
3335 struct ieee80211_sta *sta, void *priv_sta)
3336{
3337}
Zhu Yib481de92007-09-25 17:54:57 -07003338static struct rate_control_ops rs_ops = {
3339 .module = NULL,
3340 .name = RS_NAME,
3341 .tx_status = rs_tx_status,
3342 .get_rate = rs_get_rate,
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003343 .rate_init = rs_rate_init_stub,
Zhu Yib481de92007-09-25 17:54:57 -07003344 .alloc = rs_alloc,
3345 .free = rs_free,
3346 .alloc_sta = rs_alloc_sta,
3347 .free_sta = rs_free_sta,
Zhu Yi93dc6462007-09-27 11:27:37 +08003348#ifdef CONFIG_MAC80211_DEBUGFS
3349 .add_sta_debugfs = rs_add_debugfs,
3350 .remove_sta_debugfs = rs_remove_debugfs,
3351#endif
Zhu Yib481de92007-09-25 17:54:57 -07003352};
3353
Tomas Winklere227cea2008-07-18 13:53:05 +08003354int iwlagn_rate_control_register(void)
Zhu Yib481de92007-09-25 17:54:57 -07003355{
Reinette Chatre897e1cf2008-03-28 16:21:09 -07003356 return ieee80211_rate_control_register(&rs_ops);
Zhu Yib481de92007-09-25 17:54:57 -07003357}
3358
Tomas Winklere227cea2008-07-18 13:53:05 +08003359void iwlagn_rate_control_unregister(void)
Zhu Yib481de92007-09-25 17:54:57 -07003360{
3361 ieee80211_rate_control_unregister(&rs_ops);
3362}
3363