blob: 7db5d45da23716a99ff50432503608cfa4f840e4 [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"
Emmanuel Grumbachd0f76d62012-02-09 16:08:15 +020041#include "iwl-op-mode.h"
Zhu Yib481de92007-09-25 17:54:57 -070042
Tomas Winklere227cea2008-07-18 13:53:05 +080043#define RS_NAME "iwl-agn-rs"
Zhu Yib481de92007-09-25 17:54:57 -070044
Guy Cohenaade00c2008-04-23 17:14:59 -070045#define NUM_TRY_BEFORE_ANT_TOGGLE 1
Zhu Yib481de92007-09-25 17:54:57 -070046#define IWL_NUMBER_TRY 1
47#define IWL_HT_NUMBER_TRY 3
48
Ben Cahill77626352007-11-29 11:09:44 +080049#define IWL_RATE_MAX_WINDOW 62 /* # tx in history window */
50#define IWL_RATE_MIN_FAILURE_TH 6 /* min failures to calc tpt */
51#define IWL_RATE_MIN_SUCCESS_TH 8 /* min successes to calc tpt */
52
Abbas, Mohamed7d049e52009-01-20 21:33:53 -080053/* max allowed rate miss before sync LQ cmd */
54#define IWL_MISSED_RATE_MAX 15
Ben Cahill77626352007-11-29 11:09:44 +080055/* max time to accum history 2 seconds */
Mohamed Abbas447fee72009-04-20 14:37:02 -070056#define IWL_RATE_SCALE_FLUSH_INTVL (3*HZ)
Zhu Yib481de92007-09-25 17:54:57 -070057
58static u8 rs_ht_to_legacy[] = {
59 IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
60 IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
61 IWL_RATE_6M_INDEX,
62 IWL_RATE_6M_INDEX, IWL_RATE_9M_INDEX,
63 IWL_RATE_12M_INDEX, IWL_RATE_18M_INDEX,
64 IWL_RATE_24M_INDEX, IWL_RATE_36M_INDEX,
65 IWL_RATE_48M_INDEX, IWL_RATE_54M_INDEX
66};
67
Guy Cohenaade00c2008-04-23 17:14:59 -070068static const u8 ant_toggle_lookup[] = {
69 /*ANT_NONE -> */ ANT_NONE,
70 /*ANT_A -> */ ANT_B,
71 /*ANT_B -> */ ANT_C,
72 /*ANT_AB -> */ ANT_BC,
73 /*ANT_C -> */ ANT_A,
74 /*ANT_AC -> */ ANT_AB,
75 /*ANT_BC -> */ ANT_AC,
76 /*ANT_ABC -> */ ANT_ABC,
77};
78
Johannes Berg635b85b2010-09-22 18:02:04 +020079#define IWL_DECLARE_RATE_INFO(r, s, ip, in, rp, rn, pp, np) \
80 [IWL_RATE_##r##M_INDEX] = { IWL_RATE_##r##M_PLCP, \
81 IWL_RATE_SISO_##s##M_PLCP, \
82 IWL_RATE_MIMO2_##s##M_PLCP,\
83 IWL_RATE_MIMO3_##s##M_PLCP,\
84 IWL_RATE_##r##M_IEEE, \
85 IWL_RATE_##ip##M_INDEX, \
86 IWL_RATE_##in##M_INDEX, \
87 IWL_RATE_##rp##M_INDEX, \
88 IWL_RATE_##rn##M_INDEX, \
89 IWL_RATE_##pp##M_INDEX, \
90 IWL_RATE_##np##M_INDEX }
91
92/*
93 * Parameter order:
94 * rate, ht rate, prev rate, next rate, prev tgg rate, next tgg rate
95 *
96 * If there isn't a valid next or previous rate then INV is used which
97 * maps to IWL_RATE_INVALID
98 *
99 */
100const struct iwl_rate_info iwl_rates[IWL_RATE_COUNT] = {
101 IWL_DECLARE_RATE_INFO(1, INV, INV, 2, INV, 2, INV, 2), /* 1mbps */
102 IWL_DECLARE_RATE_INFO(2, INV, 1, 5, 1, 5, 1, 5), /* 2mbps */
103 IWL_DECLARE_RATE_INFO(5, INV, 2, 6, 2, 11, 2, 11), /*5.5mbps */
104 IWL_DECLARE_RATE_INFO(11, INV, 9, 12, 9, 12, 5, 18), /* 11mbps */
105 IWL_DECLARE_RATE_INFO(6, 6, 5, 9, 5, 11, 5, 11), /* 6mbps */
106 IWL_DECLARE_RATE_INFO(9, 6, 6, 11, 6, 11, 5, 11), /* 9mbps */
107 IWL_DECLARE_RATE_INFO(12, 12, 11, 18, 11, 18, 11, 18), /* 12mbps */
108 IWL_DECLARE_RATE_INFO(18, 18, 12, 24, 12, 24, 11, 24), /* 18mbps */
109 IWL_DECLARE_RATE_INFO(24, 24, 18, 36, 18, 36, 18, 36), /* 24mbps */
110 IWL_DECLARE_RATE_INFO(36, 36, 24, 48, 24, 48, 24, 48), /* 36mbps */
111 IWL_DECLARE_RATE_INFO(48, 48, 36, 54, 36, 54, 36, 54), /* 48mbps */
112 IWL_DECLARE_RATE_INFO(54, 54, 48, INV, 48, INV, 48, INV),/* 54mbps */
113 IWL_DECLARE_RATE_INFO(60, 60, 48, INV, 48, INV, 48, INV),/* 60mbps */
114 /* FIXME:RS: ^^ should be INV (legacy) */
115};
116
Daniel Halperin25205462011-03-18 18:48:55 -0700117static inline u8 rs_extract_rate(u32 rate_n_flags)
118{
119 return (u8)(rate_n_flags & RATE_MCS_RATE_MSK);
120}
121
Johannes Berg635b85b2010-09-22 18:02:04 +0200122static int iwl_hwrate_to_plcp_idx(u32 rate_n_flags)
123{
124 int idx = 0;
125
126 /* HT rate format */
127 if (rate_n_flags & RATE_MCS_HT_MSK) {
Daniel Halperin25205462011-03-18 18:48:55 -0700128 idx = rs_extract_rate(rate_n_flags);
Johannes Berg635b85b2010-09-22 18:02:04 +0200129
130 if (idx >= IWL_RATE_MIMO3_6M_PLCP)
131 idx = idx - IWL_RATE_MIMO3_6M_PLCP;
132 else if (idx >= IWL_RATE_MIMO2_6M_PLCP)
133 idx = idx - IWL_RATE_MIMO2_6M_PLCP;
134
135 idx += IWL_FIRST_OFDM_RATE;
136 /* skip 9M not supported in ht*/
137 if (idx >= IWL_RATE_9M_INDEX)
138 idx += 1;
139 if ((idx >= IWL_FIRST_OFDM_RATE) && (idx <= IWL_LAST_OFDM_RATE))
140 return idx;
141
142 /* legacy rate format, search for match in table */
143 } else {
144 for (idx = 0; idx < ARRAY_SIZE(iwl_rates); idx++)
Daniel Halperin25205462011-03-18 18:48:55 -0700145 if (iwl_rates[idx].plcp ==
146 rs_extract_rate(rate_n_flags))
Johannes Berg635b85b2010-09-22 18:02:04 +0200147 return idx;
148 }
149
150 return -1;
151}
152
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700153static void rs_rate_scale_perform(struct iwl_priv *priv,
Gábor Stefanik514d65c2009-04-23 19:36:08 +0200154 struct sk_buff *skb,
Johannes Berg4b7679a2008-09-18 18:14:18 +0200155 struct ieee80211_sta *sta,
156 struct iwl_lq_sta *lq_sta);
Wey-Yi Guy46f93812009-07-24 11:13:03 -0700157static void rs_fill_link_cmd(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +0800158 struct iwl_lq_sta *lq_sta, u32 rate_n_flags);
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -0700159static void rs_stay_in_table(struct iwl_lq_sta *lq_sta, bool force_search);
Zhu Yib481de92007-09-25 17:54:57 -0700160
161
Zhu Yi98d7e092007-09-27 11:27:42 +0800162#ifdef CONFIG_MAC80211_DEBUGFS
Tomas Winklere227cea2008-07-18 13:53:05 +0800163static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
164 u32 *rate_n_flags, int index);
Zhu Yi98d7e092007-09-27 11:27:42 +0800165#else
Tomas Winklere227cea2008-07-18 13:53:05 +0800166static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
167 u32 *rate_n_flags, int index)
Zhu Yi98d7e092007-09-27 11:27:42 +0800168{}
169#endif
Ben Cahill77626352007-11-29 11:09:44 +0800170
Daniel C Halperine3949d62009-09-17 10:43:50 -0700171/**
172 * The following tables contain the expected throughput metrics for all rates
173 *
174 * 1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
175 *
176 * where invalid entries are zeros.
177 *
178 * CCK rates are only valid in legacy table and will only be used in G
179 * (2.4 GHz) band.
Ben Cahill77626352007-11-29 11:09:44 +0800180 */
Wey-Yi Guy584a0f02009-04-01 14:33:24 -0700181
Daniel C Halperine3949d62009-09-17 10:43:50 -0700182static s32 expected_tpt_legacy[IWL_RATE_COUNT] = {
183 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 0
Zhu Yib481de92007-09-25 17:54:57 -0700184};
185
Daniel C Halperine3949d62009-09-17 10:43:50 -0700186static s32 expected_tpt_siso20MHz[4][IWL_RATE_COUNT] = {
Wey-Yi Guy7fc11e92011-01-15 09:16:59 -0800187 {0, 0, 0, 0, 42, 0, 76, 102, 124, 159, 183, 193, 202}, /* Norm */
188 {0, 0, 0, 0, 46, 0, 82, 110, 132, 168, 192, 202, 210}, /* SGI */
189 {0, 0, 0, 0, 47, 0, 91, 133, 171, 242, 305, 334, 362}, /* AGG */
190 {0, 0, 0, 0, 52, 0, 101, 145, 187, 264, 330, 361, 390}, /* AGG+SGI */
Zhu Yib481de92007-09-25 17:54:57 -0700191};
192
Daniel C Halperine3949d62009-09-17 10:43:50 -0700193static s32 expected_tpt_siso40MHz[4][IWL_RATE_COUNT] = {
194 {0, 0, 0, 0, 77, 0, 127, 160, 184, 220, 242, 250, 257}, /* Norm */
195 {0, 0, 0, 0, 83, 0, 135, 169, 193, 229, 250, 257, 264}, /* SGI */
Wey-Yi Guy7fc11e92011-01-15 09:16:59 -0800196 {0, 0, 0, 0, 94, 0, 177, 249, 313, 423, 512, 550, 586}, /* AGG */
197 {0, 0, 0, 0, 104, 0, 193, 270, 338, 454, 545, 584, 620}, /* AGG+SGI */
Zhu Yib481de92007-09-25 17:54:57 -0700198};
199
Daniel C Halperine3949d62009-09-17 10:43:50 -0700200static s32 expected_tpt_mimo2_20MHz[4][IWL_RATE_COUNT] = {
Wey-Yi Guy7fc11e92011-01-15 09:16:59 -0800201 {0, 0, 0, 0, 74, 0, 123, 155, 179, 214, 236, 244, 251}, /* Norm */
202 {0, 0, 0, 0, 81, 0, 131, 164, 188, 223, 243, 251, 257}, /* SGI */
203 {0, 0, 0, 0, 89, 0, 167, 235, 296, 402, 488, 526, 560}, /* AGG */
204 {0, 0, 0, 0, 97, 0, 182, 255, 320, 431, 520, 558, 593}, /* AGG+SGI*/
Zhu Yib481de92007-09-25 17:54:57 -0700205};
206
Daniel C Halperine3949d62009-09-17 10:43:50 -0700207static s32 expected_tpt_mimo2_40MHz[4][IWL_RATE_COUNT] = {
208 {0, 0, 0, 0, 123, 0, 182, 214, 235, 264, 279, 285, 289}, /* Norm */
209 {0, 0, 0, 0, 131, 0, 191, 222, 242, 270, 284, 289, 293}, /* SGI */
Wey-Yi Guy7fc11e92011-01-15 09:16:59 -0800210 {0, 0, 0, 0, 171, 0, 305, 410, 496, 634, 731, 771, 805}, /* AGG */
211 {0, 0, 0, 0, 186, 0, 329, 439, 527, 667, 764, 803, 838}, /* AGG+SGI */
Zhu Yib481de92007-09-25 17:54:57 -0700212};
213
Daniel C Halperine3949d62009-09-17 10:43:50 -0700214static s32 expected_tpt_mimo3_20MHz[4][IWL_RATE_COUNT] = {
215 {0, 0, 0, 0, 99, 0, 153, 186, 208, 239, 256, 263, 268}, /* Norm */
216 {0, 0, 0, 0, 106, 0, 162, 194, 215, 246, 262, 268, 273}, /* SGI */
217 {0, 0, 0, 0, 134, 0, 249, 346, 431, 574, 685, 732, 775}, /* AGG */
218 {0, 0, 0, 0, 148, 0, 272, 376, 465, 614, 727, 775, 818}, /* AGG+SGI */
Zhu Yib481de92007-09-25 17:54:57 -0700219};
220
Daniel C Halperine3949d62009-09-17 10:43:50 -0700221static s32 expected_tpt_mimo3_40MHz[4][IWL_RATE_COUNT] = {
222 {0, 0, 0, 0, 152, 0, 211, 239, 255, 279, 290, 294, 297}, /* Norm */
223 {0, 0, 0, 0, 160, 0, 219, 245, 261, 284, 294, 297, 300}, /* SGI */
224 {0, 0, 0, 0, 254, 0, 443, 584, 695, 868, 984, 1030, 1070}, /* AGG */
225 {0, 0, 0, 0, 277, 0, 478, 624, 737, 911, 1026, 1070, 1109}, /* AGG+SGI */
Wey-Yi Guy584a0f02009-04-01 14:33:24 -0700226};
227
Wey-Yi Guy12b96812009-04-08 11:39:27 -0700228/* mbps, mcs */
Tobias Klauser79496732009-12-23 14:18:11 +0100229static const struct iwl_rate_mcs_info iwl_rate_mcs[IWL_RATE_COUNT] = {
Daniel C Halperine3949d62009-09-17 10:43:50 -0700230 { "1", "BPSK DSSS"},
231 { "2", "QPSK DSSS"},
232 {"5.5", "BPSK CCK"},
233 { "11", "QPSK CCK"},
234 { "6", "BPSK 1/2"},
235 { "9", "BPSK 1/2"},
236 { "12", "QPSK 1/2"},
237 { "18", "QPSK 3/4"},
238 { "24", "16QAM 1/2"},
239 { "36", "16QAM 3/4"},
240 { "48", "64QAM 2/3"},
241 { "54", "64QAM 3/4"},
242 { "60", "64QAM 5/6"},
Wey-Yi Guy12b96812009-04-08 11:39:27 -0700243};
244
Wey-Yi Guya9c146b2009-05-22 11:01:48 -0700245#define MCS_INDEX_PER_STREAM (8)
246
Tomas Winklere227cea2008-07-18 13:53:05 +0800247static void rs_rate_scale_clear_window(struct iwl_rate_scale_data *window)
Zhu Yib481de92007-09-25 17:54:57 -0700248{
249 window->data = 0;
250 window->success_counter = 0;
251 window->success_ratio = IWL_INVALID_VALUE;
252 window->counter = 0;
253 window->average_tpt = IWL_INVALID_VALUE;
254 window->stamp = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700255}
256
Guy Cohenaade00c2008-04-23 17:14:59 -0700257static inline u8 rs_is_valid_ant(u8 valid_antenna, u8 ant_type)
258{
Tomas Winkler3ac7f142008-07-21 02:40:14 +0300259 return (ant_type & valid_antenna) == ant_type;
Guy Cohenaade00c2008-04-23 17:14:59 -0700260}
261
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200262/*
263 * removes the old data from the statistics. All data that is older than
264 * TID_MAX_TIME_DIFF, will be deleted.
265 */
Tomas Winklere227cea2008-07-18 13:53:05 +0800266static void rs_tl_rm_old_stats(struct iwl_traffic_load *tl, u32 curr_time)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200267{
268 /* The oldest age we want to keep */
269 u32 oldest_time = curr_time - TID_MAX_TIME_DIFF;
270
271 while (tl->queue_count &&
272 (tl->time_stamp < oldest_time)) {
273 tl->total -= tl->packet_count[tl->head];
274 tl->packet_count[tl->head] = 0;
275 tl->time_stamp += TID_QUEUE_CELL_SPACING;
276 tl->queue_count--;
277 tl->head++;
278 if (tl->head >= TID_QUEUE_MAX_SIZE)
279 tl->head = 0;
280 }
281}
282
283/*
284 * increment traffic load value for tid and also remove
285 * any old values if passed the certain time period
286 */
Tomas Winklere227cea2008-07-18 13:53:05 +0800287static u8 rs_tl_add_packet(struct iwl_lq_sta *lq_data,
Ron Rindjunskyfaa29712008-06-12 09:46:58 +0800288 struct ieee80211_hdr *hdr)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200289{
290 u32 curr_time = jiffies_to_msecs(jiffies);
291 u32 time_diff;
292 s32 index;
Tomas Winklere227cea2008-07-18 13:53:05 +0800293 struct iwl_traffic_load *tl = NULL;
Tomas Winkler54dbb522008-05-15 13:54:06 +0800294 u8 tid;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200295
Tomas Winkler417f1142008-11-12 13:14:06 -0800296 if (ieee80211_is_data_qos(hdr->frame_control)) {
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700297 u8 *qc = ieee80211_get_qos_ctl(hdr);
Tomas Winkler54dbb522008-05-15 13:54:06 +0800298 tid = qc[0] & 0xf;
299 } else
Emmanuel Grumbach5f85a782011-08-25 23:11:18 -0700300 return IWL_MAX_TID_COUNT;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200301
Johannes Berga8448552011-12-02 12:25:12 -0800302 if (unlikely(tid >= IWL_MAX_TID_COUNT))
Emmanuel Grumbach5f85a782011-08-25 23:11:18 -0700303 return IWL_MAX_TID_COUNT;
Reinette Chatree6a6cf42009-08-13 13:30:50 -0700304
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200305 tl = &lq_data->load[tid];
306
307 curr_time -= curr_time % TID_ROUND_VALUE;
308
309 /* Happens only for the first packet. Initialize the data */
310 if (!(tl->queue_count)) {
311 tl->total = 1;
312 tl->time_stamp = curr_time;
313 tl->queue_count = 1;
314 tl->head = 0;
315 tl->packet_count[0] = 1;
Emmanuel Grumbach5f85a782011-08-25 23:11:18 -0700316 return IWL_MAX_TID_COUNT;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200317 }
318
319 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
320 index = time_diff / TID_QUEUE_CELL_SPACING;
321
322 /* The history is too long: remove data that is older than */
323 /* TID_MAX_TIME_DIFF */
324 if (index >= TID_QUEUE_MAX_SIZE)
325 rs_tl_rm_old_stats(tl, curr_time);
326
327 index = (tl->head + index) % TID_QUEUE_MAX_SIZE;
328 tl->packet_count[index] = tl->packet_count[index] + 1;
329 tl->total = tl->total + 1;
330
331 if ((index + 1) > tl->queue_count)
332 tl->queue_count = index + 1;
Ron Rindjunskyfaa29712008-06-12 09:46:58 +0800333
334 return tid;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200335}
336
Wey-Yi Guy54a430c2011-05-20 11:56:18 -0700337#ifdef CONFIG_MAC80211_DEBUGFS
Wey-Yi Guy4e308112011-07-08 08:46:28 -0700338/**
339 * Program the device to use fixed rate for frame transmit
340 * This is for debugging/testing only
341 * once the device start use fixed rate, we need to reload the module
342 * to being back the normal operation.
343 */
Wey-Yi Guy64898542011-05-03 18:05:13 -0700344static void rs_program_fix_rate(struct iwl_priv *priv,
345 struct iwl_lq_sta *lq_sta)
346{
347 struct iwl_station_priv *sta_priv =
348 container_of(lq_sta, struct iwl_station_priv, lq_sta);
Johannes Bergc6892902011-09-20 15:37:21 -0700349 struct iwl_rxon_context *ctx = sta_priv->ctx;
Wey-Yi Guy64898542011-05-03 18:05:13 -0700350
351 lq_sta->active_legacy_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
352 lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
353 lq_sta->active_mimo2_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
354 lq_sta->active_mimo3_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
355
Wey-Yi Guy5ef15cc2011-11-30 13:24:06 -0800356#ifdef CONFIG_IWLWIFI_DEVICE_TESTMODE
Wey-Yi Guy4e308112011-07-08 08:46:28 -0700357 /* testmode has higher priority to overwirte the fixed rate */
358 if (priv->tm_fixed_rate)
359 lq_sta->dbg_fixed_rate = priv->tm_fixed_rate;
Wey-Yi Guyc10e2c12011-07-13 11:13:46 -0700360#endif
Wey-Yi Guy64898542011-05-03 18:05:13 -0700361
362 IWL_DEBUG_RATE(priv, "sta_id %d rate 0x%X\n",
Wey-Yi Guy4e308112011-07-08 08:46:28 -0700363 lq_sta->lq.sta_id, lq_sta->dbg_fixed_rate);
Wey-Yi Guy64898542011-05-03 18:05:13 -0700364
Wey-Yi Guy4e308112011-07-08 08:46:28 -0700365 if (lq_sta->dbg_fixed_rate) {
366 rs_fill_link_cmd(NULL, lq_sta, lq_sta->dbg_fixed_rate);
Wey-Yi Guy64898542011-05-03 18:05:13 -0700367 iwl_send_lq_cmd(lq_sta->drv, ctx, &lq_sta->lq, CMD_ASYNC,
368 false);
369 }
370}
371#endif
372
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200373/*
374 get the traffic load value for tid
375*/
Tomas Winklere227cea2008-07-18 13:53:05 +0800376static u32 rs_tl_get_load(struct iwl_lq_sta *lq_data, u8 tid)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200377{
378 u32 curr_time = jiffies_to_msecs(jiffies);
379 u32 time_diff;
380 s32 index;
Tomas Winklere227cea2008-07-18 13:53:05 +0800381 struct iwl_traffic_load *tl = NULL;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200382
Johannes Berga8448552011-12-02 12:25:12 -0800383 if (tid >= IWL_MAX_TID_COUNT)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200384 return 0;
385
386 tl = &(lq_data->load[tid]);
387
388 curr_time -= curr_time % TID_ROUND_VALUE;
389
390 if (!(tl->queue_count))
391 return 0;
392
393 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
394 index = time_diff / TID_QUEUE_CELL_SPACING;
395
396 /* The history is too long: remove data that is older than */
397 /* TID_MAX_TIME_DIFF */
398 if (index >= TID_QUEUE_MAX_SIZE)
399 rs_tl_rm_old_stats(tl, curr_time);
400
401 return tl->total;
402}
403
Wey-Yi Guy82ca9342010-04-12 14:02:36 -0700404static int rs_tl_turn_on_agg_for_tid(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +0800405 struct iwl_lq_sta *lq_data, u8 tid,
Johannes Berg4b7679a2008-09-18 18:14:18 +0200406 struct ieee80211_sta *sta)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200407{
Wey-Yi Guy82ca9342010-04-12 14:02:36 -0700408 int ret = -EAGAIN;
Johannes Berg290f5992010-08-23 07:56:58 -0700409 u32 load;
410
411 /*
412 * Don't create TX aggregation sessions when in high
413 * BT traffic, as they would just be disrupted by BT.
414 */
415 if (priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH) {
416 IWL_ERR(priv, "BT traffic (%d), no aggregation allowed\n",
417 priv->bt_traffic_load);
418 return ret;
419 }
420
421 load = rs_tl_get_load(lq_data, tid);
Wey-Yi Guy45d42702010-02-03 12:24:44 -0800422
Wey-Yi Guydd5b6d02011-08-25 23:10:55 -0700423 if ((iwlagn_mod_params.auto_agg) || (load > IWL_AGG_LOAD_THRESHOLD)) {
Tomas Winklere1623442009-01-27 14:27:56 -0800424 IWL_DEBUG_HT(priv, "Starting Tx agg: STA: %pM tid: %d\n",
Johannes Berge1749612008-10-27 15:59:26 -0700425 sta->addr, tid);
Sujith Manoharanbd2ce6e2010-12-15 07:47:10 +0530426 ret = ieee80211_start_tx_ba_session(sta, tid, 5000);
Wey-Yi Guy45d42702010-02-03 12:24:44 -0800427 if (ret == -EAGAIN) {
428 /*
429 * driver and mac80211 is out of sync
430 * this might be cause by reloading firmware
431 * stop the tx ba session here
432 */
Andy Lutomirski24110542010-07-25 13:14:29 -0400433 IWL_ERR(priv, "Fail start Tx agg on tid: %d\n",
Wey-Yi Guy45d42702010-02-03 12:24:44 -0800434 tid);
Johannes Berg6a8579d2010-05-27 14:41:07 +0200435 ieee80211_stop_tx_ba_session(sta, tid);
Wey-Yi Guy45d42702010-02-03 12:24:44 -0800436 }
Andy Lutomirski24110542010-07-25 13:14:29 -0400437 } else {
Wey-Yi Guy5f88ac22011-05-27 08:40:31 -0700438 IWL_DEBUG_HT(priv, "Aggregation not enabled for tid %d "
Andy Lutomirski24110542010-07-25 13:14:29 -0400439 "because load = %u\n", tid, load);
440 }
Wey-Yi Guy82ca9342010-04-12 14:02:36 -0700441 return ret;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200442}
443
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700444static void rs_tl_turn_on_agg(struct iwl_priv *priv, u8 tid,
Tomas Winklere227cea2008-07-18 13:53:05 +0800445 struct iwl_lq_sta *lq_data,
Johannes Berg4b7679a2008-09-18 18:14:18 +0200446 struct ieee80211_sta *sta)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200447{
Johannes Berga8448552011-12-02 12:25:12 -0800448 if (tid < IWL_MAX_TID_COUNT)
Wey-Yi Guycfecc6b2010-06-18 11:33:15 -0700449 rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
450 else
Johannes Berga8448552011-12-02 12:25:12 -0800451 IWL_ERR(priv, "tid exceeds max TID count: %d/%d\n",
452 tid, IWL_MAX_TID_COUNT);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200453}
454
Guy Cohen39e88502008-04-23 17:14:57 -0700455static inline int get_num_of_ant_from_rate(u32 rate_n_flags)
Guy Cohenfde0db32008-04-21 15:42:01 -0700456{
Tomas Winkler3ac7f142008-07-21 02:40:14 +0300457 return !!(rate_n_flags & RATE_MCS_ANT_A_MSK) +
458 !!(rate_n_flags & RATE_MCS_ANT_B_MSK) +
459 !!(rate_n_flags & RATE_MCS_ANT_C_MSK);
Guy Cohenfde0db32008-04-21 15:42:01 -0700460}
461
Shanyu Zhao04f2dec2010-03-19 13:34:45 -0700462/*
463 * Static function to get the expected throughput from an iwl_scale_tbl_info
464 * that wraps a NULL pointer check
465 */
466static s32 get_expected_tpt(struct iwl_scale_tbl_info *tbl, int rs_index)
467{
468 if (tbl->expected_tpt)
469 return tbl->expected_tpt[rs_index];
470 return 0;
471}
472
Ben Cahill77626352007-11-29 11:09:44 +0800473/**
474 * rs_collect_tx_data - Update the success/failure sliding window
475 *
476 * We keep a sliding window of the last 62 packets transmitted
477 * at this rate. window->data contains the bitmask of successful
478 * packets.
479 */
Shanyu Zhao04f2dec2010-03-19 13:34:45 -0700480static int rs_collect_tx_data(struct iwl_scale_tbl_info *tbl,
481 int scale_index, int attempts, int successes)
Zhu Yib481de92007-09-25 17:54:57 -0700482{
Tomas Winklere227cea2008-07-18 13:53:05 +0800483 struct iwl_rate_scale_data *window = NULL;
Guy Cohen39e88502008-04-23 17:14:57 -0700484 static const u64 mask = (((u64)1) << (IWL_RATE_MAX_WINDOW - 1));
Shanyu Zhao04f2dec2010-03-19 13:34:45 -0700485 s32 fail_count, tpt;
Zhu Yib481de92007-09-25 17:54:57 -0700486
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800487 if (scale_index < 0 || scale_index >= IWL_RATE_COUNT)
488 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -0700489
Daniel C Halperine3949d62009-09-17 10:43:50 -0700490 /* Select window for current tx bit rate */
Shanyu Zhao04f2dec2010-03-19 13:34:45 -0700491 window = &(tbl->win[scale_index]);
492
493 /* Get expected throughput */
494 tpt = get_expected_tpt(tbl, scale_index);
Zhu Yib481de92007-09-25 17:54:57 -0700495
Ben Cahill77626352007-11-29 11:09:44 +0800496 /*
497 * Keep track of only the latest 62 tx frame attempts in this rate's
498 * history window; anything older isn't really relevant any more.
499 * If we have filled up the sliding window, drop the oldest attempt;
500 * if the oldest attempt (highest bit in bitmap) shows "success",
501 * subtract "1" from the success counter (this is the main reason
502 * we keep these bitmaps!).
503 */
Daniel C Halperine3949d62009-09-17 10:43:50 -0700504 while (attempts > 0) {
Guy Cohen39e88502008-04-23 17:14:57 -0700505 if (window->counter >= IWL_RATE_MAX_WINDOW) {
506
507 /* remove earliest */
508 window->counter = IWL_RATE_MAX_WINDOW - 1;
509
Ron Rindjunsky99556432008-01-28 14:07:25 +0200510 if (window->data & mask) {
511 window->data &= ~mask;
Guy Cohen39e88502008-04-23 17:14:57 -0700512 window->success_counter--;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200513 }
Zhu Yib481de92007-09-25 17:54:57 -0700514 }
Zhu Yib481de92007-09-25 17:54:57 -0700515
Ron Rindjunsky99556432008-01-28 14:07:25 +0200516 /* Increment frames-attempted counter */
517 window->counter++;
Ben Cahill77626352007-11-29 11:09:44 +0800518
Daniel C Halperine3949d62009-09-17 10:43:50 -0700519 /* Shift bitmap by one frame to throw away oldest history */
Guy Cohen90d77952008-09-09 10:54:53 +0800520 window->data <<= 1;
Daniel C Halperine3949d62009-09-17 10:43:50 -0700521
522 /* Mark the most recent #successes attempts as successful */
Ron Rindjunsky99556432008-01-28 14:07:25 +0200523 if (successes > 0) {
Guy Cohen39e88502008-04-23 17:14:57 -0700524 window->success_counter++;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200525 window->data |= 0x1;
526 successes--;
527 }
528
Daniel C Halperine3949d62009-09-17 10:43:50 -0700529 attempts--;
Zhu Yib481de92007-09-25 17:54:57 -0700530 }
531
Ben Cahill77626352007-11-29 11:09:44 +0800532 /* Calculate current success ratio, avoid divide-by-0! */
Zhu Yib481de92007-09-25 17:54:57 -0700533 if (window->counter > 0)
534 window->success_ratio = 128 * (100 * window->success_counter)
535 / window->counter;
536 else
537 window->success_ratio = IWL_INVALID_VALUE;
538
539 fail_count = window->counter - window->success_counter;
540
Ben Cahill77626352007-11-29 11:09:44 +0800541 /* Calculate average throughput, if we have enough history. */
Zhu Yib481de92007-09-25 17:54:57 -0700542 if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
543 (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
544 window->average_tpt = (window->success_ratio * tpt + 64) / 128;
545 else
546 window->average_tpt = IWL_INVALID_VALUE;
547
Ben Cahill77626352007-11-29 11:09:44 +0800548 /* Tag this window as having been updated */
Zhu Yib481de92007-09-25 17:54:57 -0700549 window->stamp = jiffies;
550
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800551 return 0;
Zhu Yib481de92007-09-25 17:54:57 -0700552}
553
Ben Cahill77626352007-11-29 11:09:44 +0800554/*
555 * Fill uCode API rate_n_flags field, based on "search" or "active" table.
556 */
Guy Cohen39e88502008-04-23 17:14:57 -0700557/* FIXME:RS:remove this function and put the flags statically in the table */
Tomas Winkler978785a2008-12-19 10:37:31 +0800558static u32 rate_n_flags_from_tbl(struct iwl_priv *priv,
559 struct iwl_scale_tbl_info *tbl,
560 int index, u8 use_green)
Zhu Yib481de92007-09-25 17:54:57 -0700561{
Guy Cohen39e88502008-04-23 17:14:57 -0700562 u32 rate_n_flags = 0;
563
Zhu Yib481de92007-09-25 17:54:57 -0700564 if (is_legacy(tbl->lq_type)) {
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800565 rate_n_flags = iwl_rates[index].plcp;
Zhu Yib481de92007-09-25 17:54:57 -0700566 if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
Guy Cohen39e88502008-04-23 17:14:57 -0700567 rate_n_flags |= RATE_MCS_CCK_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700568
Guy Cohenfde0db32008-04-21 15:42:01 -0700569 } else if (is_Ht(tbl->lq_type)) {
570 if (index > IWL_LAST_OFDM_RATE) {
Tomas Winkler978785a2008-12-19 10:37:31 +0800571 IWL_ERR(priv, "Invalid HT rate index %d\n", index);
Zhu Yib481de92007-09-25 17:54:57 -0700572 index = IWL_LAST_OFDM_RATE;
Guy Cohenfde0db32008-04-21 15:42:01 -0700573 }
Guy Cohen39e88502008-04-23 17:14:57 -0700574 rate_n_flags = RATE_MCS_HT_MSK;
Guy Cohenfde0db32008-04-21 15:42:01 -0700575
576 if (is_siso(tbl->lq_type))
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800577 rate_n_flags |= iwl_rates[index].plcp_siso;
Guy Cohenfde0db32008-04-21 15:42:01 -0700578 else if (is_mimo2(tbl->lq_type))
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800579 rate_n_flags |= iwl_rates[index].plcp_mimo2;
Guy Cohenfde0db32008-04-21 15:42:01 -0700580 else
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800581 rate_n_flags |= iwl_rates[index].plcp_mimo3;
Zhu Yib481de92007-09-25 17:54:57 -0700582 } else {
Tomas Winkler978785a2008-12-19 10:37:31 +0800583 IWL_ERR(priv, "Invalid tbl->lq_type %d\n", tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -0700584 }
585
Guy Cohen39e88502008-04-23 17:14:57 -0700586 rate_n_flags |= ((tbl->ant_type << RATE_MCS_ANT_POS) &
Guy Cohenfde0db32008-04-21 15:42:01 -0700587 RATE_MCS_ANT_ABC_MSK);
Zhu Yib481de92007-09-25 17:54:57 -0700588
Guy Cohen39e88502008-04-23 17:14:57 -0700589 if (is_Ht(tbl->lq_type)) {
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700590 if (tbl->is_ht40) {
Guy Cohen39e88502008-04-23 17:14:57 -0700591 if (tbl->is_dup)
592 rate_n_flags |= RATE_MCS_DUP_MSK;
593 else
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700594 rate_n_flags |= RATE_MCS_HT40_MSK;
Guy Cohen39e88502008-04-23 17:14:57 -0700595 }
596 if (tbl->is_SGI)
597 rate_n_flags |= RATE_MCS_SGI_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700598
Guy Cohen39e88502008-04-23 17:14:57 -0700599 if (use_green) {
600 rate_n_flags |= RATE_MCS_GF_MSK;
601 if (is_siso(tbl->lq_type) && tbl->is_SGI) {
602 rate_n_flags &= ~RATE_MCS_SGI_MSK;
Tomas Winkler978785a2008-12-19 10:37:31 +0800603 IWL_ERR(priv, "GF was set with SGI:SISO\n");
Guy Cohen39e88502008-04-23 17:14:57 -0700604 }
605 }
Zhu Yib481de92007-09-25 17:54:57 -0700606 }
Guy Cohen39e88502008-04-23 17:14:57 -0700607 return rate_n_flags;
Zhu Yib481de92007-09-25 17:54:57 -0700608}
609
Ben Cahill77626352007-11-29 11:09:44 +0800610/*
611 * Interpret uCode API's rate_n_flags format,
612 * fill "search" or "active" tx mode table.
613 */
Guy Cohen39e88502008-04-23 17:14:57 -0700614static int rs_get_tbl_info_from_mcs(const u32 rate_n_flags,
Johannes Berg8318d782008-01-24 19:38:38 +0100615 enum ieee80211_band band,
Tomas Winklere227cea2008-07-18 13:53:05 +0800616 struct iwl_scale_tbl_info *tbl,
Zhu Yib481de92007-09-25 17:54:57 -0700617 int *rate_idx)
618{
Guy Cohen39e88502008-04-23 17:14:57 -0700619 u32 ant_msk = (rate_n_flags & RATE_MCS_ANT_ABC_MSK);
620 u8 num_of_ant = get_num_of_ant_from_rate(rate_n_flags);
621 u8 mcs;
Zhu Yib481de92007-09-25 17:54:57 -0700622
Wey-Yi Guy80e91582010-08-03 08:23:32 -0700623 memset(tbl, 0, sizeof(struct iwl_scale_tbl_info));
Tomas Winklere7d326a2008-06-12 09:47:11 +0800624 *rate_idx = iwl_hwrate_to_plcp_idx(rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -0700625
Guy Cohenfde0db32008-04-21 15:42:01 -0700626 if (*rate_idx == IWL_RATE_INVALID) {
Zhu Yib481de92007-09-25 17:54:57 -0700627 *rate_idx = -1;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800628 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -0700629 }
Ben Cahill77626352007-11-29 11:09:44 +0800630 tbl->is_SGI = 0; /* default legacy setup */
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700631 tbl->is_ht40 = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700632 tbl->is_dup = 0;
Guy Cohenfde0db32008-04-21 15:42:01 -0700633 tbl->ant_type = (ant_msk >> RATE_MCS_ANT_POS);
634 tbl->lq_type = LQ_NONE;
Mohamed Abbasd6e93392009-05-08 13:44:39 -0700635 tbl->max_search = IWL_MAX_SEARCH;
Zhu Yib481de92007-09-25 17:54:57 -0700636
Ben Cahill77626352007-11-29 11:09:44 +0800637 /* legacy rate format */
Guy Cohen39e88502008-04-23 17:14:57 -0700638 if (!(rate_n_flags & RATE_MCS_HT_MSK)) {
Guy Cohenfde0db32008-04-21 15:42:01 -0700639 if (num_of_ant == 1) {
Johannes Berg8318d782008-01-24 19:38:38 +0100640 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700641 tbl->lq_type = LQ_A;
642 else
643 tbl->lq_type = LQ_G;
Zhu Yib481de92007-09-25 17:54:57 -0700644 }
Guy Cohenfde0db32008-04-21 15:42:01 -0700645 /* HT rate format */
Zhu Yib481de92007-09-25 17:54:57 -0700646 } else {
Guy Cohen39e88502008-04-23 17:14:57 -0700647 if (rate_n_flags & RATE_MCS_SGI_MSK)
Zhu Yib481de92007-09-25 17:54:57 -0700648 tbl->is_SGI = 1;
649
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700650 if ((rate_n_flags & RATE_MCS_HT40_MSK) ||
Guy Cohen39e88502008-04-23 17:14:57 -0700651 (rate_n_flags & RATE_MCS_DUP_MSK))
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700652 tbl->is_ht40 = 1;
Zhu Yib481de92007-09-25 17:54:57 -0700653
Guy Cohen39e88502008-04-23 17:14:57 -0700654 if (rate_n_flags & RATE_MCS_DUP_MSK)
Zhu Yib481de92007-09-25 17:54:57 -0700655 tbl->is_dup = 1;
Guy Cohenfde0db32008-04-21 15:42:01 -0700656
Guy Cohen39e88502008-04-23 17:14:57 -0700657 mcs = rs_extract_rate(rate_n_flags);
Guy Cohenfde0db32008-04-21 15:42:01 -0700658
Guy Cohen39e88502008-04-23 17:14:57 -0700659 /* SISO */
660 if (mcs <= IWL_RATE_SISO_60M_PLCP) {
Guy Cohenfde0db32008-04-21 15:42:01 -0700661 if (num_of_ant == 1)
662 tbl->lq_type = LQ_SISO; /*else NONE*/
663 /* MIMO2 */
Guy Cohen39e88502008-04-23 17:14:57 -0700664 } else if (mcs <= IWL_RATE_MIMO2_60M_PLCP) {
Guy Cohenfde0db32008-04-21 15:42:01 -0700665 if (num_of_ant == 2)
666 tbl->lq_type = LQ_MIMO2;
667 /* MIMO3 */
668 } else {
Mohamed Abbasd6e93392009-05-08 13:44:39 -0700669 if (num_of_ant == 3) {
670 tbl->max_search = IWL_MAX_11N_MIMO3_SEARCH;
Guy Cohenfde0db32008-04-21 15:42:01 -0700671 tbl->lq_type = LQ_MIMO3;
Mohamed Abbasd6e93392009-05-08 13:44:39 -0700672 }
Guy Cohenfde0db32008-04-21 15:42:01 -0700673 }
Zhu Yib481de92007-09-25 17:54:57 -0700674 }
675 return 0;
676}
Guy Cohenaade00c2008-04-23 17:14:59 -0700677
678/* switch to another antenna/antennas and return 1 */
679/* if no other valid antenna found, return 0 */
680static int rs_toggle_antenna(u32 valid_ant, u32 *rate_n_flags,
Tomas Winklere227cea2008-07-18 13:53:05 +0800681 struct iwl_scale_tbl_info *tbl)
Zhu Yib481de92007-09-25 17:54:57 -0700682{
Guy Cohenaade00c2008-04-23 17:14:59 -0700683 u8 new_ant_type;
684
685 if (!tbl->ant_type || tbl->ant_type > ANT_ABC)
686 return 0;
687
688 if (!rs_is_valid_ant(valid_ant, tbl->ant_type))
689 return 0;
690
691 new_ant_type = ant_toggle_lookup[tbl->ant_type];
692
693 while ((new_ant_type != tbl->ant_type) &&
694 !rs_is_valid_ant(valid_ant, new_ant_type))
695 new_ant_type = ant_toggle_lookup[new_ant_type];
696
697 if (new_ant_type == tbl->ant_type)
698 return 0;
699
700 tbl->ant_type = new_ant_type;
701 *rate_n_flags &= ~RATE_MCS_ANT_ABC_MSK;
702 *rate_n_flags |= new_ant_type << RATE_MCS_ANT_POS;
703 return 1;
Zhu Yib481de92007-09-25 17:54:57 -0700704}
705
Daniel C Halperinb2617932009-08-13 13:30:59 -0700706/**
707 * Green-field mode is valid if the station supports it and
708 * there are no non-GF stations present in the BSS.
709 */
Johannes Berg7e6a5882010-08-23 10:46:46 +0200710static bool rs_use_green(struct ieee80211_sta *sta)
Zhu Yib481de92007-09-25 17:54:57 -0700711{
Johannes Berg66f3d992012-08-05 18:31:46 +0200712 /*
713 * There's a bug somewhere in this code that causes the
714 * scaling to get stuck because GF+SGI can't be combined
715 * in SISO rates. Until we find that bug, disable GF, it
716 * has only limited benefit and we still interoperate with
717 * GF APs since we can always receive GF transmissions.
718 */
719 return false;
Guy Cohenf935a6d2008-04-23 17:15:02 -0700720}
Zhu Yib481de92007-09-25 17:54:57 -0700721
722/**
723 * rs_get_supported_rates - get the available rates
724 *
725 * if management frame or broadcast frame only return
726 * basic available rates.
727 *
728 */
Tomas Winklere227cea2008-07-18 13:53:05 +0800729static u16 rs_get_supported_rates(struct iwl_lq_sta *lq_sta,
730 struct ieee80211_hdr *hdr,
731 enum iwl_table_type rate_type)
Zhu Yib481de92007-09-25 17:54:57 -0700732{
Guy Coheneecd6e52008-04-23 17:14:58 -0700733 if (is_legacy(rate_type)) {
734 return lq_sta->active_legacy_rate;
735 } else {
736 if (is_siso(rate_type))
737 return lq_sta->active_siso_rate;
738 else if (is_mimo2(rate_type))
739 return lq_sta->active_mimo2_rate;
740 else
741 return lq_sta->active_mimo3_rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -0800742 }
Zhu Yib481de92007-09-25 17:54:57 -0700743}
744
Ester Kummerbf403db2008-05-05 10:22:40 +0800745static u16 rs_get_adjacent_rate(struct iwl_priv *priv, u8 index, u16 rate_mask,
746 int rate_type)
Zhu Yib481de92007-09-25 17:54:57 -0700747{
748 u8 high = IWL_RATE_INVALID;
749 u8 low = IWL_RATE_INVALID;
750
Ian Schram01ebd062007-10-25 17:15:22 +0800751 /* 802.11A or ht walks to the next literal adjacent rate in
Zhu Yib481de92007-09-25 17:54:57 -0700752 * the rate table */
753 if (is_a_band(rate_type) || !is_legacy(rate_type)) {
754 int i;
755 u32 mask;
756
757 /* Find the previous rate that is in the rate mask */
758 i = index - 1;
759 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
760 if (rate_mask & mask) {
761 low = i;
762 break;
763 }
764 }
765
766 /* Find the next rate that is in the rate mask */
767 i = index + 1;
768 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
769 if (rate_mask & mask) {
770 high = i;
771 break;
772 }
773 }
774
775 return (high << 8) | low;
776 }
777
778 low = index;
779 while (low != IWL_RATE_INVALID) {
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800780 low = iwl_rates[low].prev_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700781 if (low == IWL_RATE_INVALID)
782 break;
783 if (rate_mask & (1 << low))
784 break;
Tomas Winklere1623442009-01-27 14:27:56 -0800785 IWL_DEBUG_RATE(priv, "Skipping masked lower rate: %d\n", low);
Zhu Yib481de92007-09-25 17:54:57 -0700786 }
787
788 high = index;
789 while (high != IWL_RATE_INVALID) {
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800790 high = iwl_rates[high].next_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700791 if (high == IWL_RATE_INVALID)
792 break;
793 if (rate_mask & (1 << high))
794 break;
Tomas Winklere1623442009-01-27 14:27:56 -0800795 IWL_DEBUG_RATE(priv, "Skipping masked higher rate: %d\n", high);
Zhu Yib481de92007-09-25 17:54:57 -0700796 }
797
798 return (high << 8) | low;
799}
800
Tomas Winklere227cea2008-07-18 13:53:05 +0800801static u32 rs_get_lower_rate(struct iwl_lq_sta *lq_sta,
802 struct iwl_scale_tbl_info *tbl,
803 u8 scale_index, u8 ht_possible)
Zhu Yib481de92007-09-25 17:54:57 -0700804{
Zhu Yib481de92007-09-25 17:54:57 -0700805 s32 low;
806 u16 rate_mask;
807 u16 high_low;
808 u8 switch_to_legacy = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -0800809 u8 is_green = lq_sta->is_green;
Wey-Yi Guy2ff65782009-09-11 10:38:18 -0700810 struct iwl_priv *priv = lq_sta->drv;
Zhu Yib481de92007-09-25 17:54:57 -0700811
812 /* check if we need to switch from HT to legacy rates.
813 * assumption is that mandatory rates (1Mbps or 6Mbps)
814 * are always supported (spec demand) */
815 if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_index)) {
816 switch_to_legacy = 1;
817 scale_index = rs_ht_to_legacy[scale_index];
Johannes Berg8318d782008-01-24 19:38:38 +0100818 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700819 tbl->lq_type = LQ_A;
820 else
821 tbl->lq_type = LQ_G;
822
Guy Cohen69fdb302008-04-23 17:15:01 -0700823 if (num_of_ant(tbl->ant_type) > 1)
Wey-Yi Guy2ff65782009-09-11 10:38:18 -0700824 tbl->ant_type =
Emmanuel Grumbachd6189122011-08-25 23:10:39 -0700825 first_antenna(hw_params(priv).valid_tx_ant);
Zhu Yib481de92007-09-25 17:54:57 -0700826
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700827 tbl->is_ht40 = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700828 tbl->is_SGI = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -0700829 tbl->max_search = IWL_MAX_SEARCH;
Zhu Yib481de92007-09-25 17:54:57 -0700830 }
831
Guy Coheneecd6e52008-04-23 17:14:58 -0700832 rate_mask = rs_get_supported_rates(lq_sta, NULL, tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -0700833
Ben Cahill77626352007-11-29 11:09:44 +0800834 /* Mask with station rate restriction */
Zhu Yib481de92007-09-25 17:54:57 -0700835 if (is_legacy(tbl->lq_type)) {
Ben Cahill77626352007-11-29 11:09:44 +0800836 /* supp_rates has no CCK bits in A mode */
Johannes Berg8318d782008-01-24 19:38:38 +0100837 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700838 rate_mask = (u16)(rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -0800839 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
Zhu Yib481de92007-09-25 17:54:57 -0700840 else
Tomas Winklerc33104f2008-01-14 17:46:21 -0800841 rate_mask = (u16)(rate_mask & lq_sta->supp_rates);
Zhu Yib481de92007-09-25 17:54:57 -0700842 }
843
Ben Cahill77626352007-11-29 11:09:44 +0800844 /* If we switched from HT to legacy, check current rate */
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800845 if (switch_to_legacy && (rate_mask & (1 << scale_index))) {
Guy Cohen39e88502008-04-23 17:14:57 -0700846 low = scale_index;
847 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700848 }
849
Ester Kummerbf403db2008-05-05 10:22:40 +0800850 high_low = rs_get_adjacent_rate(lq_sta->drv, scale_index, rate_mask,
851 tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -0700852 low = high_low & 0xff;
853
Guy Cohen39e88502008-04-23 17:14:57 -0700854 if (low == IWL_RATE_INVALID)
855 low = scale_index;
856
857out:
Tomas Winkler978785a2008-12-19 10:37:31 +0800858 return rate_n_flags_from_tbl(lq_sta->drv, tbl, low, is_green);
Zhu Yib481de92007-09-25 17:54:57 -0700859}
860
Ben Cahill77626352007-11-29 11:09:44 +0800861/*
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700862 * Simple function to compare two rate scale table types
863 */
864static bool table_type_matches(struct iwl_scale_tbl_info *a,
865 struct iwl_scale_tbl_info *b)
866{
867 return (a->lq_type == b->lq_type) && (a->ant_type == b->ant_type) &&
868 (a->is_SGI == b->is_SGI);
869}
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700870
Johannes Berg7e6a5882010-08-23 10:46:46 +0200871static void rs_bt_update_lq(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
872 struct iwl_lq_sta *lq_sta)
Wey-Yi Guybee008b2010-08-23 07:57:04 -0700873{
874 struct iwl_scale_tbl_info *tbl;
Wey-Yi Guy66e863a52010-11-08 14:54:37 -0800875 bool full_concurrent = priv->bt_full_concurrent;
Wey-Yi Guybee008b2010-08-23 07:57:04 -0700876
Wey-Yi Guy66e863a52010-11-08 14:54:37 -0800877 if (priv->bt_ant_couple_ok) {
878 /*
879 * Is there a need to switch between
880 * full concurrency and 3-wire?
881 */
Wey-Yi Guy66e863a52010-11-08 14:54:37 -0800882 if (priv->bt_ci_compliance && priv->bt_ant_couple_ok)
883 full_concurrent = true;
884 else
885 full_concurrent = false;
Wey-Yi Guy66e863a52010-11-08 14:54:37 -0800886 }
887 if ((priv->bt_traffic_load != priv->last_bt_traffic_load) ||
888 (priv->bt_full_concurrent != full_concurrent)) {
Wey-Yi Guybee008b2010-08-23 07:57:04 -0700889 priv->bt_full_concurrent = full_concurrent;
Meenakshi Venkataraman75713d32012-05-16 22:35:57 +0200890 priv->last_bt_traffic_load = priv->bt_traffic_load;
Wey-Yi Guybee008b2010-08-23 07:57:04 -0700891
892 /* Update uCode's rate table. */
893 tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
894 rs_fill_link_cmd(priv, lq_sta, tbl->current_rate);
Johannes Berg7e6a5882010-08-23 10:46:46 +0200895 iwl_send_lq_cmd(priv, ctx, &lq_sta->lq, CMD_ASYNC, false);
Wey-Yi Guybee008b2010-08-23 07:57:04 -0700896
Johannes Berg1ee158d2012-02-17 10:07:44 -0800897 queue_work(priv->workqueue, &priv->bt_full_concurrency);
Wey-Yi Guybee008b2010-08-23 07:57:04 -0700898 }
899}
900
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700901/*
Ben Cahill77626352007-11-29 11:09:44 +0800902 * mac80211 sends us Tx status
903 */
Johannes Berg4b7679a2008-09-18 18:14:18 +0200904static void rs_tx_status(void *priv_r, struct ieee80211_supported_band *sband,
905 struct ieee80211_sta *sta, void *priv_sta,
Johannes Berge039fa42008-05-15 12:55:29 +0200906 struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -0700907{
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700908 int legacy_success;
909 int retries;
910 int rs_index, mac_index, i;
Tomas Winkler417f1142008-11-12 13:14:06 -0800911 struct iwl_lq_sta *lq_sta = priv_sta;
Tomas Winkler66c73db2008-04-15 16:01:40 -0700912 struct iwl_link_quality_cmd *table;
Zhu Yib481de92007-09-25 17:54:57 -0700913 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Emmanuel Grumbachd0f76d62012-02-09 16:08:15 +0200914 struct iwl_op_mode *op_mode = (struct iwl_op_mode *)priv_r;
915 struct iwl_priv *priv = IWL_OP_MODE_GET_DVM(op_mode);
Johannes Berge039fa42008-05-15 12:55:29 +0200916 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Daniel C Halperin50273092009-08-28 09:44:45 -0700917 enum mac80211_rate_control_flags mac_flags;
Guy Cohen39e88502008-04-23 17:14:57 -0700918 u32 tx_rate;
Tomas Winklere227cea2008-07-18 13:53:05 +0800919 struct iwl_scale_tbl_info tbl_type;
Shanyu Zhao04f2dec2010-03-19 13:34:45 -0700920 struct iwl_scale_tbl_info *curr_tbl, *other_tbl, *tmp_tbl;
Johannes Berg7e6a5882010-08-23 10:46:46 +0200921 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
Johannes Bergc6892902011-09-20 15:37:21 -0700922 struct iwl_rxon_context *ctx = sta_priv->ctx;
Zhu Yib481de92007-09-25 17:54:57 -0700923
Tomas Winklere1623442009-01-27 14:27:56 -0800924 IWL_DEBUG_RATE_LIMIT(priv, "get frame ack response, update rate scale window\n");
Zhu Yib481de92007-09-25 17:54:57 -0700925
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800926 /* Treat uninitialized rate scaling data same as non-existing. */
927 if (!lq_sta) {
928 IWL_DEBUG_RATE(priv, "Station rate scaling not created yet.\n");
929 return;
930 } else if (!lq_sta->drv) {
931 IWL_DEBUG_RATE(priv, "Rate scaling not initialized yet.\n");
932 return;
933 }
934
Tomas Winkler417f1142008-11-12 13:14:06 -0800935 if (!ieee80211_is_data(hdr->frame_control) ||
Gábor Stefanik514d65c2009-04-23 19:36:08 +0200936 info->flags & IEEE80211_TX_CTL_NO_ACK)
Zhu Yib481de92007-09-25 17:54:57 -0700937 return;
938
Daniel C Halperine3949d62009-09-17 10:43:50 -0700939 /* This packet was aggregated but doesn't carry status info */
Johannes Berge039fa42008-05-15 12:55:29 +0200940 if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
941 !(info->flags & IEEE80211_TX_STAT_AMPDU))
Ron Rindjunsky99556432008-01-28 14:07:25 +0200942 return;
943
Ben Cahill77626352007-11-29 11:09:44 +0800944 /*
945 * Ignore this Tx frame response if its initial rate doesn't match
946 * that of latest Link Quality command. There may be stragglers
947 * from a previous Link Quality command, but we're no longer interested
948 * in those; they're either from the "active" mode while we're trying
949 * to check "search" mode, or a prior "search" mode after we've moved
950 * to a new "search" mode (which might become the new "active" mode).
951 */
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700952 table = &lq_sta->lq;
Guy Cohen39e88502008-04-23 17:14:57 -0700953 tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
954 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800955 if (priv->band == IEEE80211_BAND_5GHZ)
956 rs_index -= IWL_FIRST_OFDM_RATE;
Daniel C Halperin50273092009-08-28 09:44:45 -0700957 mac_flags = info->status.rates[0].flags;
958 mac_index = info->status.rates[0].idx;
Daniel C Halperin31513be2009-08-28 09:44:47 -0700959 /* For HT packets, map MCS to PLCP */
960 if (mac_flags & IEEE80211_TX_RC_MCS) {
961 mac_index &= RATE_MCS_CODE_MSK; /* Remove # of streams */
962 if (mac_index >= (IWL_RATE_9M_INDEX - IWL_FIRST_OFDM_RATE))
963 mac_index++;
Daniel C Halperin392a0ba2009-09-11 10:38:08 -0700964 /*
965 * mac80211 HT index is always zero-indexed; we need to move
966 * HT OFDM rates after CCK rates in 2.4 GHz band
967 */
968 if (priv->band == IEEE80211_BAND_2GHZ)
969 mac_index += IWL_FIRST_OFDM_RATE;
Daniel C Halperin31513be2009-08-28 09:44:47 -0700970 }
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700971 /* Here we actually compare this rate to the latest LQ command */
Daniel C Halperin50273092009-08-28 09:44:45 -0700972 if ((mac_index < 0) ||
973 (tbl_type.is_SGI != !!(mac_flags & IEEE80211_TX_RC_SHORT_GI)) ||
974 (tbl_type.is_ht40 != !!(mac_flags & IEEE80211_TX_RC_40_MHZ_WIDTH)) ||
975 (tbl_type.is_dup != !!(mac_flags & IEEE80211_TX_RC_DUP_DATA)) ||
Johannes Berge6a98542008-10-21 12:40:02 +0200976 (tbl_type.ant_type != info->antenna_sel_tx) ||
Daniel C Halperin50273092009-08-28 09:44:45 -0700977 (!!(tx_rate & RATE_MCS_HT_MSK) != !!(mac_flags & IEEE80211_TX_RC_MCS)) ||
978 (!!(tx_rate & RATE_MCS_GF_MSK) != !!(mac_flags & IEEE80211_TX_RC_GREEN_FIELD)) ||
Daniel C Halperin31513be2009-08-28 09:44:47 -0700979 (rs_index != mac_index)) {
980 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 -0700981 /*
982 * Since rates mis-match, the last LQ command may have failed.
983 * After IWL_MISSED_RATE_MAX mis-matches, resync the uCode with
984 * ... driver.
Mohamed Abbasd5e49032008-12-12 08:22:15 -0800985 */
Abbas, Mohamed7d049e52009-01-20 21:33:53 -0800986 lq_sta->missed_rate_counter++;
987 if (lq_sta->missed_rate_counter > IWL_MISSED_RATE_MAX) {
988 lq_sta->missed_rate_counter = 0;
Johannes Berg7e6a5882010-08-23 10:46:46 +0200989 iwl_send_lq_cmd(priv, ctx, &lq_sta->lq, CMD_ASYNC, false);
Abbas, Mohamed7d049e52009-01-20 21:33:53 -0800990 }
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700991 /* Regardless, ignore this status info for outdated rate */
992 return;
993 } else
994 /* Rate did match, so reset the missed_rate_counter */
995 lq_sta->missed_rate_counter = 0;
996
997 /* Figure out if rate scale algorithm is in active or search table */
998 if (table_type_matches(&tbl_type,
999 &(lq_sta->lq_info[lq_sta->active_tbl]))) {
1000 curr_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1001 other_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
1002 } else if (table_type_matches(&tbl_type,
1003 &lq_sta->lq_info[1 - lq_sta->active_tbl])) {
1004 curr_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
1005 other_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1006 } else {
1007 IWL_DEBUG_RATE(priv, "Neither active nor search matches tx rate\n");
Wey-Yi Guy80e91582010-08-03 08:23:32 -07001008 tmp_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1009 IWL_DEBUG_RATE(priv, "active- lq:%x, ant:%x, SGI:%d\n",
1010 tmp_tbl->lq_type, tmp_tbl->ant_type, tmp_tbl->is_SGI);
1011 tmp_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
1012 IWL_DEBUG_RATE(priv, "search- lq:%x, ant:%x, SGI:%d\n",
1013 tmp_tbl->lq_type, tmp_tbl->ant_type, tmp_tbl->is_SGI);
1014 IWL_DEBUG_RATE(priv, "actual- lq:%x, ant:%x, SGI:%d\n",
1015 tbl_type.lq_type, tbl_type.ant_type, tbl_type.is_SGI);
1016 /*
1017 * no matching table found, let's by-pass the data collection
1018 * and continue to perform rate scale to find the rate table
1019 */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001020 rs_stay_in_table(lq_sta, true);
Wey-Yi Guy80e91582010-08-03 08:23:32 -07001021 goto done;
Zhu Yib481de92007-09-25 17:54:57 -07001022 }
Zhu Yib481de92007-09-25 17:54:57 -07001023
Ben Cahill77626352007-11-29 11:09:44 +08001024 /*
Daniel C Halperin95407aa2009-09-17 10:43:48 -07001025 * Updating the frame history depends on whether packets were
1026 * aggregated.
1027 *
1028 * For aggregation, all packets were transmitted at the same rate, the
1029 * first index into rate scale table.
Ben Cahill77626352007-11-29 11:09:44 +08001030 */
Daniel C Halperin95407aa2009-09-17 10:43:48 -07001031 if (info->flags & IEEE80211_TX_STAT_AMPDU) {
1032 tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
1033 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type,
1034 &rs_index);
Shanyu Zhao04f2dec2010-03-19 13:34:45 -07001035 rs_collect_tx_data(curr_tbl, rs_index,
Daniel Halperine3a3cd82010-04-18 09:27:58 -07001036 info->status.ampdu_len,
1037 info->status.ampdu_ack_len);
Zhu Yib481de92007-09-25 17:54:57 -07001038
Daniel C Halperin95407aa2009-09-17 10:43:48 -07001039 /* Update success/fail counts if not searching for new mode */
1040 if (lq_sta->stay_in_tbl) {
Daniel Halperine3a3cd82010-04-18 09:27:58 -07001041 lq_sta->total_success += info->status.ampdu_ack_len;
1042 lq_sta->total_failed += (info->status.ampdu_len -
1043 info->status.ampdu_ack_len);
Daniel C Halperin95407aa2009-09-17 10:43:48 -07001044 }
1045 } else {
1046 /*
1047 * For legacy, update frame history with for each Tx retry.
1048 */
1049 retries = info->status.rates[0].count - 1;
1050 /* HW doesn't send more than 15 retries */
1051 retries = min(retries, 15);
1052
1053 /* The last transmission may have been successful */
1054 legacy_success = !!(info->flags & IEEE80211_TX_STAT_ACK);
1055 /* Collect data for each rate used during failed TX attempts */
1056 for (i = 0; i <= retries; ++i) {
1057 tx_rate = le32_to_cpu(table->rs_table[i].rate_n_flags);
1058 rs_get_tbl_info_from_mcs(tx_rate, priv->band,
1059 &tbl_type, &rs_index);
1060 /*
1061 * Only collect stats if retried rate is in the same RS
1062 * table as active/search.
1063 */
1064 if (table_type_matches(&tbl_type, curr_tbl))
Shanyu Zhao04f2dec2010-03-19 13:34:45 -07001065 tmp_tbl = curr_tbl;
Daniel C Halperin95407aa2009-09-17 10:43:48 -07001066 else if (table_type_matches(&tbl_type, other_tbl))
Shanyu Zhao04f2dec2010-03-19 13:34:45 -07001067 tmp_tbl = other_tbl;
Ron Rindjunsky99556432008-01-28 14:07:25 +02001068 else
Daniel C Halperin95407aa2009-09-17 10:43:48 -07001069 continue;
Shanyu Zhao04f2dec2010-03-19 13:34:45 -07001070 rs_collect_tx_data(tmp_tbl, rs_index, 1,
1071 i < retries ? 0 : legacy_success);
Daniel C Halperin95407aa2009-09-17 10:43:48 -07001072 }
1073
1074 /* Update success/fail counts if not searching for new mode */
1075 if (lq_sta->stay_in_tbl) {
1076 lq_sta->total_success += legacy_success;
1077 lq_sta->total_failed += retries + (1 - legacy_success);
Ron Rindjunsky99556432008-01-28 14:07:25 +02001078 }
Zhu Yib481de92007-09-25 17:54:57 -07001079 }
Daniel C Halperin95407aa2009-09-17 10:43:48 -07001080 /* The last TX rate is cached in lq_sta; it's set in if/else above */
1081 lq_sta->last_rate_n_flags = tx_rate;
Wey-Yi Guy80e91582010-08-03 08:23:32 -07001082done:
Ben Cahill77626352007-11-29 11:09:44 +08001083 /* See if there's a better rate or modulation mode to try. */
Abbas, Mohamedc338ba32009-01-21 10:58:02 -08001084 if (sta && sta->supp_rates[sband->band])
Gábor Stefanik514d65c2009-04-23 19:36:08 +02001085 rs_rate_scale_perform(priv, skb, sta, lq_sta);
Wey-Yi Guyc10e2c12011-07-13 11:13:46 -07001086
Wey-Yi Guy5ef15cc2011-11-30 13:24:06 -08001087#if defined(CONFIG_MAC80211_DEBUGFS) && defined(CONFIG_IWLWIFI_DEVICE_TESTMODE)
Wey-Yi Guy4e308112011-07-08 08:46:28 -07001088 if ((priv->tm_fixed_rate) &&
1089 (priv->tm_fixed_rate != lq_sta->dbg_fixed_rate))
Wey-Yi Guy64898542011-05-03 18:05:13 -07001090 rs_program_fix_rate(priv, lq_sta);
1091#endif
Don Fry38622412011-12-16 07:07:36 -08001092 if (cfg(priv)->bt_params && cfg(priv)->bt_params->advanced_bt_coexist)
Johannes Berg7e6a5882010-08-23 10:46:46 +02001093 rs_bt_update_lq(priv, ctx, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07001094}
1095
Ben Cahill77626352007-11-29 11:09:44 +08001096/*
1097 * Begin a period of staying with a selected modulation mode.
1098 * Set "stay_in_tbl" flag to prevent any mode switches.
1099 * Set frame tx success limits according to legacy vs. high-throughput,
1100 * and reset overall (spanning all rates) tx success history statistics.
1101 * These control how long we stay using same modulation mode before
1102 * searching for a new mode.
1103 */
Ester Kummerbf403db2008-05-05 10:22:40 +08001104static void rs_set_stay_in_table(struct iwl_priv *priv, u8 is_legacy,
Tomas Winklere227cea2008-07-18 13:53:05 +08001105 struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07001106{
Tomas Winklere1623442009-01-27 14:27:56 -08001107 IWL_DEBUG_RATE(priv, "we are staying in the same table\n");
Tomas Winklerc33104f2008-01-14 17:46:21 -08001108 lq_sta->stay_in_tbl = 1; /* only place this gets set */
Zhu Yib481de92007-09-25 17:54:57 -07001109 if (is_legacy) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001110 lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT;
1111 lq_sta->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
1112 lq_sta->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
Zhu Yib481de92007-09-25 17:54:57 -07001113 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001114 lq_sta->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
1115 lq_sta->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
1116 lq_sta->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
Zhu Yib481de92007-09-25 17:54:57 -07001117 }
Tomas Winklerc33104f2008-01-14 17:46:21 -08001118 lq_sta->table_count = 0;
1119 lq_sta->total_failed = 0;
1120 lq_sta->total_success = 0;
Mohamed Abbas447fee72009-04-20 14:37:02 -07001121 lq_sta->flush_timer = jiffies;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001122 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001123}
1124
Ben Cahill77626352007-11-29 11:09:44 +08001125/*
1126 * Find correct throughput table for given mode of modulation
1127 */
Tomas Winklere227cea2008-07-18 13:53:05 +08001128static void rs_set_expected_tpt_table(struct iwl_lq_sta *lq_sta,
1129 struct iwl_scale_tbl_info *tbl)
Zhu Yib481de92007-09-25 17:54:57 -07001130{
Daniel C Halperine3949d62009-09-17 10:43:50 -07001131 /* Used to choose among HT tables */
1132 s32 (*ht_tbl_pointer)[IWL_RATE_COUNT];
1133
1134 /* Check for invalid LQ type */
1135 if (WARN_ON_ONCE(!is_legacy(tbl->lq_type) && !is_Ht(tbl->lq_type))) {
1136 tbl->expected_tpt = expected_tpt_legacy;
1137 return;
1138 }
1139
1140 /* Legacy rates have only one table */
Zhu Yib481de92007-09-25 17:54:57 -07001141 if (is_legacy(tbl->lq_type)) {
Daniel C Halperine3949d62009-09-17 10:43:50 -07001142 tbl->expected_tpt = expected_tpt_legacy;
1143 return;
1144 }
1145
1146 /* Choose among many HT tables depending on number of streams
1147 * (SISO/MIMO2/MIMO3), channel width (20/40), SGI, and aggregation
1148 * status */
1149 if (is_siso(tbl->lq_type) && (!tbl->is_ht40 || lq_sta->is_dup))
1150 ht_tbl_pointer = expected_tpt_siso20MHz;
1151 else if (is_siso(tbl->lq_type))
1152 ht_tbl_pointer = expected_tpt_siso40MHz;
1153 else if (is_mimo2(tbl->lq_type) && (!tbl->is_ht40 || lq_sta->is_dup))
1154 ht_tbl_pointer = expected_tpt_mimo2_20MHz;
1155 else if (is_mimo2(tbl->lq_type))
1156 ht_tbl_pointer = expected_tpt_mimo2_40MHz;
1157 else if (is_mimo3(tbl->lq_type) && (!tbl->is_ht40 || lq_sta->is_dup))
1158 ht_tbl_pointer = expected_tpt_mimo3_20MHz;
1159 else /* if (is_mimo3(tbl->lq_type)) <-- must be true */
1160 ht_tbl_pointer = expected_tpt_mimo3_40MHz;
1161
1162 if (!tbl->is_SGI && !lq_sta->is_agg) /* Normal */
1163 tbl->expected_tpt = ht_tbl_pointer[0];
1164 else if (tbl->is_SGI && !lq_sta->is_agg) /* SGI */
1165 tbl->expected_tpt = ht_tbl_pointer[1];
1166 else if (!tbl->is_SGI && lq_sta->is_agg) /* AGG */
1167 tbl->expected_tpt = ht_tbl_pointer[2];
1168 else /* AGG+SGI */
1169 tbl->expected_tpt = ht_tbl_pointer[3];
Zhu Yib481de92007-09-25 17:54:57 -07001170}
1171
Ben Cahill77626352007-11-29 11:09:44 +08001172/*
1173 * Find starting rate for new "search" high-throughput mode of modulation.
1174 * Goal is to find lowest expected rate (under perfect conditions) that is
1175 * above the current measured throughput of "active" mode, to give new mode
1176 * a fair chance to prove itself without too many challenges.
1177 *
1178 * This gets called when transitioning to more aggressive modulation
1179 * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
1180 * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
1181 * to decrease to match "active" throughput. When moving from MIMO to SISO,
1182 * bit rate will typically need to increase, but not if performance was bad.
1183 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001184static s32 rs_get_best_rate(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001185 struct iwl_lq_sta *lq_sta,
1186 struct iwl_scale_tbl_info *tbl, /* "search" */
Guy Cohenf935a6d2008-04-23 17:15:02 -07001187 u16 rate_mask, s8 index)
Zhu Yib481de92007-09-25 17:54:57 -07001188{
Ben Cahill77626352007-11-29 11:09:44 +08001189 /* "active" values */
Tomas Winklere227cea2008-07-18 13:53:05 +08001190 struct iwl_scale_tbl_info *active_tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001191 &(lq_sta->lq_info[lq_sta->active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07001192 s32 active_sr = active_tbl->win[index].success_ratio;
Zhu Yib481de92007-09-25 17:54:57 -07001193 s32 active_tpt = active_tbl->expected_tpt[index];
Ben Cahill77626352007-11-29 11:09:44 +08001194
1195 /* expected "search" throughput */
1196 s32 *tpt_tbl = tbl->expected_tpt;
1197
1198 s32 new_rate, high, low, start_hi;
Zhu Yib481de92007-09-25 17:54:57 -07001199 u16 high_low;
Guy Cohenf935a6d2008-04-23 17:15:02 -07001200 s8 rate = index;
Zhu Yib481de92007-09-25 17:54:57 -07001201
1202 new_rate = high = low = start_hi = IWL_RATE_INVALID;
1203
1204 for (; ;) {
Ester Kummerbf403db2008-05-05 10:22:40 +08001205 high_low = rs_get_adjacent_rate(priv, rate, rate_mask,
1206 tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -07001207
1208 low = high_low & 0xff;
1209 high = (high_low >> 8) & 0xff;
1210
Ben Cahill77626352007-11-29 11:09:44 +08001211 /*
1212 * Lower the "search" bit rate, to give new "search" mode
1213 * approximately the same throughput as "active" if:
1214 *
1215 * 1) "Active" mode has been working modestly well (but not
1216 * great), and expected "search" throughput (under perfect
1217 * conditions) at candidate rate is above the actual
1218 * measured "active" throughput (but less than expected
1219 * "active" throughput under perfect conditions).
1220 * OR
1221 * 2) "Active" mode has been working perfectly or very well
1222 * and expected "search" throughput (under perfect
1223 * conditions) at candidate rate is above expected
1224 * "active" throughput (under perfect conditions).
1225 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001226 if ((((100 * tpt_tbl[rate]) > lq_sta->last_tpt) &&
Zhu Yib481de92007-09-25 17:54:57 -07001227 ((active_sr > IWL_RATE_DECREASE_TH) &&
1228 (active_sr <= IWL_RATE_HIGH_TH) &&
1229 (tpt_tbl[rate] <= active_tpt))) ||
1230 ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
1231 (tpt_tbl[rate] > active_tpt))) {
1232
Ben Cahill77626352007-11-29 11:09:44 +08001233 /* (2nd or later pass)
1234 * If we've already tried to raise the rate, and are
1235 * now trying to lower it, use the higher rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001236 if (start_hi != IWL_RATE_INVALID) {
1237 new_rate = start_hi;
1238 break;
1239 }
Ben Cahill77626352007-11-29 11:09:44 +08001240
Zhu Yib481de92007-09-25 17:54:57 -07001241 new_rate = rate;
Ben Cahill77626352007-11-29 11:09:44 +08001242
1243 /* Loop again with lower rate */
Zhu Yib481de92007-09-25 17:54:57 -07001244 if (low != IWL_RATE_INVALID)
1245 rate = low;
Ben Cahill77626352007-11-29 11:09:44 +08001246
1247 /* Lower rate not available, use the original */
Zhu Yib481de92007-09-25 17:54:57 -07001248 else
1249 break;
Ben Cahill77626352007-11-29 11:09:44 +08001250
1251 /* Else try to raise the "search" rate to match "active" */
Zhu Yib481de92007-09-25 17:54:57 -07001252 } else {
Ben Cahill77626352007-11-29 11:09:44 +08001253 /* (2nd or later pass)
1254 * If we've already tried to lower the rate, and are
1255 * now trying to raise it, use the lower rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001256 if (new_rate != IWL_RATE_INVALID)
1257 break;
Ben Cahill77626352007-11-29 11:09:44 +08001258
1259 /* Loop again with higher rate */
Zhu Yib481de92007-09-25 17:54:57 -07001260 else if (high != IWL_RATE_INVALID) {
1261 start_hi = high;
1262 rate = high;
Ben Cahill77626352007-11-29 11:09:44 +08001263
1264 /* Higher rate not available, use the original */
Zhu Yib481de92007-09-25 17:54:57 -07001265 } else {
Guy Cohen90d77952008-09-09 10:54:53 +08001266 new_rate = rate;
Zhu Yib481de92007-09-25 17:54:57 -07001267 break;
1268 }
1269 }
1270 }
1271
1272 return new_rate;
1273}
Zhu Yib481de92007-09-25 17:54:57 -07001274
Ben Cahill77626352007-11-29 11:09:44 +08001275/*
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001276 * Set up search table for MIMO2
Ben Cahill77626352007-11-29 11:09:44 +08001277 */
Guy Cohenfde0db32008-04-21 15:42:01 -07001278static int rs_switch_to_mimo2(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001279 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001280 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001281 struct ieee80211_sta *sta,
Tomas Winklere227cea2008-07-18 13:53:05 +08001282 struct iwl_scale_tbl_info *tbl, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001283{
Zhu Yib481de92007-09-25 17:54:57 -07001284 u16 rate_mask;
1285 s32 rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001286 s8 is_green = lq_sta->is_green;
Johannes Berg7e6a5882010-08-23 10:46:46 +02001287 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
Johannes Bergc6892902011-09-20 15:37:21 -07001288 struct iwl_rxon_context *ctx = sta_priv->ctx;
Zhu Yib481de92007-09-25 17:54:57 -07001289
Luis R. Rodriguezde27e642008-12-23 15:58:44 -08001290 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
Zhu Yib481de92007-09-25 17:54:57 -07001291 return -1;
1292
Johannes Bergd9fe60d2008-10-09 12:13:49 +02001293 if (((sta->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 2)
Tomas Winkler00c5ae22008-09-03 11:26:42 +08001294 == WLAN_HT_CAP_SM_PS_STATIC)
Zhu Yib481de92007-09-25 17:54:57 -07001295 return -1;
1296
Ben Cahill77626352007-11-29 11:09:44 +08001297 /* Need both Tx chains/antennas to support MIMO */
Emmanuel Grumbachd6189122011-08-25 23:10:39 -07001298 if (hw_params(priv).tx_chains_num < 2)
Zhu Yib481de92007-09-25 17:54:57 -07001299 return -1;
1300
Tomas Winklere1623442009-01-27 14:27:56 -08001301 IWL_DEBUG_RATE(priv, "LQ: try to switch to MIMO2\n");
Guy Cohen39e88502008-04-23 17:14:57 -07001302
1303 tbl->lq_type = LQ_MIMO2;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001304 tbl->is_dup = lq_sta->is_dup;
Zhu Yib481de92007-09-25 17:54:57 -07001305 tbl->action = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001306 tbl->max_search = IWL_MAX_SEARCH;
Guy Cohen39e88502008-04-23 17:14:57 -07001307 rate_mask = lq_sta->active_mimo2_rate;
1308
Johannes Berg7e6a5882010-08-23 10:46:46 +02001309 if (iwl_is_ht40_tx_allowed(priv, ctx, &sta->ht_cap))
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001310 tbl->is_ht40 = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001311 else
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001312 tbl->is_ht40 = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001313
Guy Coheneecd6e52008-04-23 17:14:58 -07001314 rs_set_expected_tpt_table(lq_sta, tbl);
Zhu Yib481de92007-09-25 17:54:57 -07001315
Guy Cohenf935a6d2008-04-23 17:15:02 -07001316 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
Zhu Yib481de92007-09-25 17:54:57 -07001317
Tomas Winklere1623442009-01-27 14:27:56 -08001318 IWL_DEBUG_RATE(priv, "LQ: MIMO2 best rate %d mask %X\n", rate, rate_mask);
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001319 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1320 IWL_DEBUG_RATE(priv, "Can't switch with index %d rate mask %x\n",
1321 rate, rate_mask);
1322 return -1;
1323 }
1324 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
Guy Cohen39e88502008-04-23 17:14:57 -07001325
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001326 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
1327 tbl->current_rate, is_green);
1328 return 0;
1329}
1330
1331/*
1332 * Set up search table for MIMO3
1333 */
1334static int rs_switch_to_mimo3(struct iwl_priv *priv,
1335 struct iwl_lq_sta *lq_sta,
1336 struct ieee80211_conf *conf,
1337 struct ieee80211_sta *sta,
1338 struct iwl_scale_tbl_info *tbl, int index)
1339{
1340 u16 rate_mask;
1341 s32 rate;
1342 s8 is_green = lq_sta->is_green;
Johannes Berg7e6a5882010-08-23 10:46:46 +02001343 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
Johannes Bergc6892902011-09-20 15:37:21 -07001344 struct iwl_rxon_context *ctx = sta_priv->ctx;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001345
1346 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
1347 return -1;
1348
1349 if (((sta->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 2)
1350 == WLAN_HT_CAP_SM_PS_STATIC)
1351 return -1;
1352
1353 /* Need both Tx chains/antennas to support MIMO */
Emmanuel Grumbachd6189122011-08-25 23:10:39 -07001354 if (hw_params(priv).tx_chains_num < 3)
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001355 return -1;
1356
1357 IWL_DEBUG_RATE(priv, "LQ: try to switch to MIMO3\n");
1358
1359 tbl->lq_type = LQ_MIMO3;
1360 tbl->is_dup = lq_sta->is_dup;
1361 tbl->action = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001362 tbl->max_search = IWL_MAX_11N_MIMO3_SEARCH;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001363 rate_mask = lq_sta->active_mimo3_rate;
1364
Johannes Berg7e6a5882010-08-23 10:46:46 +02001365 if (iwl_is_ht40_tx_allowed(priv, ctx, &sta->ht_cap))
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001366 tbl->is_ht40 = 1;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001367 else
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001368 tbl->is_ht40 = 0;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001369
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001370 rs_set_expected_tpt_table(lq_sta, tbl);
1371
1372 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
1373
1374 IWL_DEBUG_RATE(priv, "LQ: MIMO3 best rate %d mask %X\n",
1375 rate, rate_mask);
Guy Cohen39e88502008-04-23 17:14:57 -07001376 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
Tomas Winklere1623442009-01-27 14:27:56 -08001377 IWL_DEBUG_RATE(priv, "Can't switch with index %d rate mask %x\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001378 rate, rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07001379 return -1;
Guy Cohen39e88502008-04-23 17:14:57 -07001380 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001381 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
Zhu Yib481de92007-09-25 17:54:57 -07001382
Tomas Winklere1623442009-01-27 14:27:56 -08001383 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001384 tbl->current_rate, is_green);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001385 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07001386}
1387
Ben Cahill77626352007-11-29 11:09:44 +08001388/*
1389 * Set up search table for SISO
1390 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001391static int rs_switch_to_siso(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001392 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001393 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001394 struct ieee80211_sta *sta,
Tomas Winklere227cea2008-07-18 13:53:05 +08001395 struct iwl_scale_tbl_info *tbl, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001396{
Zhu Yib481de92007-09-25 17:54:57 -07001397 u16 rate_mask;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001398 u8 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07001399 s32 rate;
Johannes Berg7e6a5882010-08-23 10:46:46 +02001400 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
Johannes Bergc6892902011-09-20 15:37:21 -07001401 struct iwl_rxon_context *ctx = sta_priv->ctx;
Zhu Yib481de92007-09-25 17:54:57 -07001402
Luis R. Rodriguezde27e642008-12-23 15:58:44 -08001403 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
Zhu Yib481de92007-09-25 17:54:57 -07001404 return -1;
1405
Tomas Winklere1623442009-01-27 14:27:56 -08001406 IWL_DEBUG_RATE(priv, "LQ: try to switch to SISO\n");
Guy Cohen39e88502008-04-23 17:14:57 -07001407
Tomas Winklerc33104f2008-01-14 17:46:21 -08001408 tbl->is_dup = lq_sta->is_dup;
Zhu Yib481de92007-09-25 17:54:57 -07001409 tbl->lq_type = LQ_SISO;
1410 tbl->action = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001411 tbl->max_search = IWL_MAX_SEARCH;
Guy Cohen39e88502008-04-23 17:14:57 -07001412 rate_mask = lq_sta->active_siso_rate;
Zhu Yib481de92007-09-25 17:54:57 -07001413
Johannes Berg7e6a5882010-08-23 10:46:46 +02001414 if (iwl_is_ht40_tx_allowed(priv, ctx, &sta->ht_cap))
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001415 tbl->is_ht40 = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001416 else
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001417 tbl->is_ht40 = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001418
Zhu Yib481de92007-09-25 17:54:57 -07001419 if (is_green)
Guy Cohen39e88502008-04-23 17:14:57 -07001420 tbl->is_SGI = 0; /*11n spec: no SGI in SISO+Greenfield*/
Zhu Yib481de92007-09-25 17:54:57 -07001421
Guy Coheneecd6e52008-04-23 17:14:58 -07001422 rs_set_expected_tpt_table(lq_sta, tbl);
Guy Cohenf935a6d2008-04-23 17:15:02 -07001423 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
Zhu Yib481de92007-09-25 17:54:57 -07001424
Tomas Winklere1623442009-01-27 14:27:56 -08001425 IWL_DEBUG_RATE(priv, "LQ: get best rate %d mask %X\n", rate, rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07001426 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
Tomas Winklere1623442009-01-27 14:27:56 -08001427 IWL_DEBUG_RATE(priv, "can not switch with index %d rate mask %x\n",
Zhu Yib481de92007-09-25 17:54:57 -07001428 rate, rate_mask);
1429 return -1;
1430 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001431 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
Tomas Winklere1623442009-01-27 14:27:56 -08001432 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001433 tbl->current_rate, is_green);
Mohamed Abbas403ab562007-11-06 22:06:25 -08001434 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07001435}
1436
Ben Cahill77626352007-11-29 11:09:44 +08001437/*
1438 * Try to switch to new modulation mode from legacy
1439 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001440static int rs_move_legacy_other(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001441 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001442 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001443 struct ieee80211_sta *sta,
Zhu Yib481de92007-09-25 17:54:57 -07001444 int index)
1445{
Tomas Winklere227cea2008-07-18 13:53:05 +08001446 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1447 struct iwl_scale_tbl_info *search_tbl =
1448 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1449 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1450 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1451 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Wey-Yi Guyb1a78852010-07-31 08:52:03 -07001452 u8 start_action;
Emmanuel Grumbachd6189122011-08-25 23:10:39 -07001453 u8 valid_tx_ant = hw_params(priv).valid_tx_ant;
1454 u8 tx_chains_num = hw_params(priv).tx_chains_num;
Guy Cohenfde0db32008-04-21 15:42:01 -07001455 int ret = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001456 u8 update_search_tbl_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001457
Johannes Berg290f5992010-08-23 07:56:58 -07001458 switch (priv->bt_traffic_load) {
1459 case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
1460 /* nothing */
1461 break;
1462 case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
1463 /* avoid antenna B unless MIMO */
1464 if (tbl->action == IWL_LEGACY_SWITCH_ANTENNA2)
Venkataraman, Meenakshi383b0872011-11-10 06:55:25 -08001465 tbl->action = IWL_LEGACY_SWITCH_SISO;
Johannes Berg290f5992010-08-23 07:56:58 -07001466 break;
1467 case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
1468 case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
1469 /* avoid antenna B and MIMO */
Emmanuel Grumbachd6189122011-08-25 23:10:39 -07001470 valid_tx_ant =
1471 first_antenna(hw_params(priv).valid_tx_ant);
Johannes Berg290f5992010-08-23 07:56:58 -07001472 if (tbl->action >= IWL_LEGACY_SWITCH_ANTENNA2 &&
1473 tbl->action != IWL_LEGACY_SWITCH_SISO)
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001474 tbl->action = IWL_LEGACY_SWITCH_SISO;
Johannes Berg290f5992010-08-23 07:56:58 -07001475 break;
1476 default:
1477 IWL_ERR(priv, "Invalid BT load %d", priv->bt_traffic_load);
1478 break;
1479 }
1480
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001481 if (!iwl_ht_enabled(priv))
1482 /* stay in Legacy */
1483 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
Johannes Berg3ad3b922009-08-07 15:41:48 -07001484 else if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001485 tbl->action > IWL_LEGACY_SWITCH_SISO)
1486 tbl->action = IWL_LEGACY_SWITCH_SISO;
Wey-Yi Guybee008b2010-08-23 07:57:04 -07001487
1488 /* configure as 1x1 if bt full concurrency */
1489 if (priv->bt_full_concurrent) {
1490 if (!iwl_ht_enabled(priv))
1491 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
1492 else if (tbl->action >= IWL_LEGACY_SWITCH_ANTENNA2)
1493 tbl->action = IWL_LEGACY_SWITCH_SISO;
Emmanuel Grumbachd6189122011-08-25 23:10:39 -07001494 valid_tx_ant =
1495 first_antenna(hw_params(priv).valid_tx_ant);
Wey-Yi Guybee008b2010-08-23 07:57:04 -07001496 }
1497
Wey-Yi Guyb1a78852010-07-31 08:52:03 -07001498 start_action = tbl->action;
Zhu Yib481de92007-09-25 17:54:57 -07001499 for (; ;) {
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001500 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001501 switch (tbl->action) {
Guy Cohen3110bef2008-09-09 10:54:54 +08001502 case IWL_LEGACY_SWITCH_ANTENNA1:
1503 case IWL_LEGACY_SWITCH_ANTENNA2:
Tomas Winklere1623442009-01-27 14:27:56 -08001504 IWL_DEBUG_RATE(priv, "LQ: Legacy toggle Antenna\n");
Zhu Yib481de92007-09-25 17:54:57 -07001505
Guy Cohen3110bef2008-09-09 10:54:54 +08001506 if ((tbl->action == IWL_LEGACY_SWITCH_ANTENNA1 &&
1507 tx_chains_num <= 1) ||
1508 (tbl->action == IWL_LEGACY_SWITCH_ANTENNA2 &&
1509 tx_chains_num <= 2))
1510 break;
1511
Ben Cahill77626352007-11-29 11:09:44 +08001512 /* Don't change antenna if success has been great */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001513 if (window->success_ratio >= IWL_RS_GOOD_RATIO &&
1514 !priv->bt_full_concurrent &&
1515 priv->bt_traffic_load ==
1516 IWL_BT_COEX_TRAFFIC_LOAD_NONE)
Zhu Yib481de92007-09-25 17:54:57 -07001517 break;
Ben Cahill77626352007-11-29 11:09:44 +08001518
Ben Cahill77626352007-11-29 11:09:44 +08001519 /* Set up search table to try other antenna */
Zhu Yib481de92007-09-25 17:54:57 -07001520 memcpy(search_tbl, tbl, sz);
1521
Guy Cohenaade00c2008-04-23 17:14:59 -07001522 if (rs_toggle_antenna(valid_tx_ant,
1523 &search_tbl->current_rate, search_tbl)) {
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001524 update_search_tbl_counter = 1;
Guy Cohen3110bef2008-09-09 10:54:54 +08001525 rs_set_expected_tpt_table(lq_sta, search_tbl);
Guy Cohenaade00c2008-04-23 17:14:59 -07001526 goto out;
1527 }
Guy Cohena5e8b502008-05-29 16:35:11 +08001528 break;
Zhu Yib481de92007-09-25 17:54:57 -07001529 case IWL_LEGACY_SWITCH_SISO:
Tomas Winklere1623442009-01-27 14:27:56 -08001530 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to SISO\n");
Ben Cahill77626352007-11-29 11:09:44 +08001531
1532 /* Set up search table to try SISO */
Zhu Yib481de92007-09-25 17:54:57 -07001533 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001534 search_tbl->is_SGI = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001535 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001536 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001537 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001538 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001539 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001540 }
Zhu Yib481de92007-09-25 17:54:57 -07001541
1542 break;
Guy Cohen3110bef2008-09-09 10:54:54 +08001543 case IWL_LEGACY_SWITCH_MIMO2_AB:
1544 case IWL_LEGACY_SWITCH_MIMO2_AC:
1545 case IWL_LEGACY_SWITCH_MIMO2_BC:
Tomas Winklere1623442009-01-27 14:27:56 -08001546 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to MIMO2\n");
Ben Cahill77626352007-11-29 11:09:44 +08001547
1548 /* Set up search table to try MIMO */
Zhu Yib481de92007-09-25 17:54:57 -07001549 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001550 search_tbl->is_SGI = 0;
Guy Cohen3110bef2008-09-09 10:54:54 +08001551
1552 if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AB)
1553 search_tbl->ant_type = ANT_AB;
1554 else if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AC)
1555 search_tbl->ant_type = ANT_AC;
1556 else
1557 search_tbl->ant_type = ANT_BC;
1558
1559 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1560 break;
1561
Guy Cohenfde0db32008-04-21 15:42:01 -07001562 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001563 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001564 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001565 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001566 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001567 }
Zhu Yib481de92007-09-25 17:54:57 -07001568 break;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001569
1570 case IWL_LEGACY_SWITCH_MIMO3_ABC:
1571 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to MIMO3\n");
1572
1573 /* Set up search table to try MIMO3 */
1574 memcpy(search_tbl, tbl, sz);
1575 search_tbl->is_SGI = 0;
1576
1577 search_tbl->ant_type = ANT_ABC;
1578
1579 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1580 break;
1581
1582 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1583 search_tbl, index);
1584 if (!ret) {
1585 lq_sta->action_counter = 0;
1586 goto out;
1587 }
1588 break;
Zhu Yib481de92007-09-25 17:54:57 -07001589 }
1590 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001591 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001592 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
Zhu Yib481de92007-09-25 17:54:57 -07001593
1594 if (tbl->action == start_action)
1595 break;
1596
1597 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001598 search_tbl->lq_type = LQ_NONE;
Zhu Yib481de92007-09-25 17:54:57 -07001599 return 0;
1600
Guy Cohen3110bef2008-09-09 10:54:54 +08001601out:
1602 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001603 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001604 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001605 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001606 if (update_search_tbl_counter)
1607 search_tbl->action = tbl->action;
Zhu Yib481de92007-09-25 17:54:57 -07001608 return 0;
1609
1610}
1611
Ben Cahill77626352007-11-29 11:09:44 +08001612/*
1613 * Try to switch to new modulation mode from SISO
1614 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001615static int rs_move_siso_to_other(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001616 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001617 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001618 struct ieee80211_sta *sta, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001619{
Tomas Winklerc33104f2008-01-14 17:46:21 -08001620 u8 is_green = lq_sta->is_green;
Tomas Winklere227cea2008-07-18 13:53:05 +08001621 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1622 struct iwl_scale_tbl_info *search_tbl =
1623 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1624 struct iwl_rate_scale_data *window = &(tbl->win[index]);
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001625 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Tomas Winklere227cea2008-07-18 13:53:05 +08001626 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1627 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Wey-Yi Guyb1a78852010-07-31 08:52:03 -07001628 u8 start_action;
Emmanuel Grumbachd6189122011-08-25 23:10:39 -07001629 u8 valid_tx_ant = hw_params(priv).valid_tx_ant;
1630 u8 tx_chains_num = hw_params(priv).tx_chains_num;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001631 u8 update_search_tbl_counter = 0;
Guy Cohenfde0db32008-04-21 15:42:01 -07001632 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001633
Johannes Berg290f5992010-08-23 07:56:58 -07001634 switch (priv->bt_traffic_load) {
1635 case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
1636 /* nothing */
1637 break;
1638 case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
1639 /* avoid antenna B unless MIMO */
1640 if (tbl->action == IWL_SISO_SWITCH_ANTENNA2)
Venkataraman, Meenakshi383b0872011-11-10 06:55:25 -08001641 tbl->action = IWL_SISO_SWITCH_MIMO2_AB;
Johannes Berg290f5992010-08-23 07:56:58 -07001642 break;
1643 case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
1644 case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
1645 /* avoid antenna B and MIMO */
Emmanuel Grumbachd6189122011-08-25 23:10:39 -07001646 valid_tx_ant =
1647 first_antenna(hw_params(priv).valid_tx_ant);
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001648 if (tbl->action != IWL_SISO_SWITCH_ANTENNA1)
Johannes Berg290f5992010-08-23 07:56:58 -07001649 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1650 break;
1651 default:
1652 IWL_ERR(priv, "Invalid BT load %d", priv->bt_traffic_load);
1653 break;
1654 }
1655
Johannes Berg3ad3b922009-08-07 15:41:48 -07001656 if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001657 tbl->action > IWL_SISO_SWITCH_ANTENNA2) {
1658 /* stay in SISO */
1659 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1660 }
Wey-Yi Guybee008b2010-08-23 07:57:04 -07001661
1662 /* configure as 1x1 if bt full concurrency */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001663 if (priv->bt_full_concurrent) {
Emmanuel Grumbachd6189122011-08-25 23:10:39 -07001664 valid_tx_ant =
1665 first_antenna(hw_params(priv).valid_tx_ant);
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001666 if (tbl->action >= IWL_LEGACY_SWITCH_ANTENNA2)
1667 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1668 }
Wey-Yi Guybee008b2010-08-23 07:57:04 -07001669
Wey-Yi Guyb1a78852010-07-31 08:52:03 -07001670 start_action = tbl->action;
Zhu Yib481de92007-09-25 17:54:57 -07001671 for (;;) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001672 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001673 switch (tbl->action) {
Guy Cohen3110bef2008-09-09 10:54:54 +08001674 case IWL_SISO_SWITCH_ANTENNA1:
1675 case IWL_SISO_SWITCH_ANTENNA2:
Tomas Winklere1623442009-01-27 14:27:56 -08001676 IWL_DEBUG_RATE(priv, "LQ: SISO toggle Antenna\n");
Guy Cohen3110bef2008-09-09 10:54:54 +08001677 if ((tbl->action == IWL_SISO_SWITCH_ANTENNA1 &&
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001678 tx_chains_num <= 1) ||
Guy Cohen3110bef2008-09-09 10:54:54 +08001679 (tbl->action == IWL_SISO_SWITCH_ANTENNA2 &&
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001680 tx_chains_num <= 2))
Guy Cohen3110bef2008-09-09 10:54:54 +08001681 break;
1682
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001683 if (window->success_ratio >= IWL_RS_GOOD_RATIO &&
1684 !priv->bt_full_concurrent &&
1685 priv->bt_traffic_load ==
1686 IWL_BT_COEX_TRAFFIC_LOAD_NONE)
Zhu Yib481de92007-09-25 17:54:57 -07001687 break;
Zhu Yib481de92007-09-25 17:54:57 -07001688
1689 memcpy(search_tbl, tbl, sz);
Guy Cohenaade00c2008-04-23 17:14:59 -07001690 if (rs_toggle_antenna(valid_tx_ant,
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001691 &search_tbl->current_rate, search_tbl)) {
1692 update_search_tbl_counter = 1;
Guy Cohenaade00c2008-04-23 17:14:59 -07001693 goto out;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001694 }
Guy Cohena5e8b502008-05-29 16:35:11 +08001695 break;
Guy Cohen3110bef2008-09-09 10:54:54 +08001696 case IWL_SISO_SWITCH_MIMO2_AB:
1697 case IWL_SISO_SWITCH_MIMO2_AC:
1698 case IWL_SISO_SWITCH_MIMO2_BC:
Tomas Winklere1623442009-01-27 14:27:56 -08001699 IWL_DEBUG_RATE(priv, "LQ: SISO switch to MIMO2\n");
Zhu Yib481de92007-09-25 17:54:57 -07001700 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001701 search_tbl->is_SGI = 0;
Guy Cohen3110bef2008-09-09 10:54:54 +08001702
1703 if (tbl->action == IWL_SISO_SWITCH_MIMO2_AB)
1704 search_tbl->ant_type = ANT_AB;
1705 else if (tbl->action == IWL_SISO_SWITCH_MIMO2_AC)
1706 search_tbl->ant_type = ANT_AC;
1707 else
1708 search_tbl->ant_type = ANT_BC;
1709
1710 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1711 break;
1712
Guy Cohenfde0db32008-04-21 15:42:01 -07001713 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001714 search_tbl, index);
Guy Cohen3110bef2008-09-09 10:54:54 +08001715 if (!ret)
Zhu Yib481de92007-09-25 17:54:57 -07001716 goto out;
1717 break;
1718 case IWL_SISO_SWITCH_GI:
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001719 if (!tbl->is_ht40 && !(ht_cap->cap &
1720 IEEE80211_HT_CAP_SGI_20))
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001721 break;
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001722 if (tbl->is_ht40 && !(ht_cap->cap &
1723 IEEE80211_HT_CAP_SGI_40))
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001724 break;
1725
Tomas Winklere1623442009-01-27 14:27:56 -08001726 IWL_DEBUG_RATE(priv, "LQ: SISO toggle SGI/NGI\n");
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001727
Zhu Yib481de92007-09-25 17:54:57 -07001728 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001729 if (is_green) {
1730 if (!tbl->is_SGI)
1731 break;
1732 else
Winkler, Tomas15b16872008-12-19 10:37:33 +08001733 IWL_ERR(priv,
1734 "SGI was set in GF+SISO\n");
Zhu Yib481de92007-09-25 17:54:57 -07001735 }
Guy Cohen39e88502008-04-23 17:14:57 -07001736 search_tbl->is_SGI = !tbl->is_SGI;
Guy Coheneecd6e52008-04-23 17:14:58 -07001737 rs_set_expected_tpt_table(lq_sta, search_tbl);
Guy Cohen39e88502008-04-23 17:14:57 -07001738 if (tbl->is_SGI) {
1739 s32 tpt = lq_sta->last_tpt / 100;
1740 if (tpt >= search_tbl->expected_tpt[index])
1741 break;
1742 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001743 search_tbl->current_rate =
1744 rate_n_flags_from_tbl(priv, search_tbl,
1745 index, is_green);
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001746 update_search_tbl_counter = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001747 goto out;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001748 case IWL_SISO_SWITCH_MIMO3_ABC:
1749 IWL_DEBUG_RATE(priv, "LQ: SISO switch to MIMO3\n");
1750 memcpy(search_tbl, tbl, sz);
1751 search_tbl->is_SGI = 0;
1752 search_tbl->ant_type = ANT_ABC;
1753
1754 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1755 break;
1756
1757 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1758 search_tbl, index);
1759 if (!ret)
1760 goto out;
1761 break;
Zhu Yib481de92007-09-25 17:54:57 -07001762 }
1763 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001764 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001765 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
Zhu Yib481de92007-09-25 17:54:57 -07001766
1767 if (tbl->action == start_action)
1768 break;
1769 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001770 search_tbl->lq_type = LQ_NONE;
Zhu Yib481de92007-09-25 17:54:57 -07001771 return 0;
1772
1773 out:
Guy Cohen3110bef2008-09-09 10:54:54 +08001774 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001775 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001776 if (tbl->action > IWL_SISO_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001777 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001778 if (update_search_tbl_counter)
1779 search_tbl->action = tbl->action;
1780
Zhu Yib481de92007-09-25 17:54:57 -07001781 return 0;
1782}
1783
Ben Cahill77626352007-11-29 11:09:44 +08001784/*
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001785 * Try to switch to new modulation mode from MIMO2
Ben Cahill77626352007-11-29 11:09:44 +08001786 */
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001787static int rs_move_mimo2_to_other(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001788 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001789 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001790 struct ieee80211_sta *sta, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001791{
Tomas Winklerc33104f2008-01-14 17:46:21 -08001792 s8 is_green = lq_sta->is_green;
Tomas Winklere227cea2008-07-18 13:53:05 +08001793 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1794 struct iwl_scale_tbl_info *search_tbl =
1795 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Guy Cohen3110bef2008-09-09 10:54:54 +08001796 struct iwl_rate_scale_data *window = &(tbl->win[index]);
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001797 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Tomas Winklere227cea2008-07-18 13:53:05 +08001798 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1799 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Wey-Yi Guyb1a78852010-07-31 08:52:03 -07001800 u8 start_action;
Emmanuel Grumbachd6189122011-08-25 23:10:39 -07001801 u8 valid_tx_ant = hw_params(priv).valid_tx_ant;
1802 u8 tx_chains_num = hw_params(priv).tx_chains_num;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001803 u8 update_search_tbl_counter = 0;
Guy Cohenaade00c2008-04-23 17:14:59 -07001804 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001805
Johannes Berg290f5992010-08-23 07:56:58 -07001806 switch (priv->bt_traffic_load) {
1807 case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
1808 /* nothing */
1809 break;
1810 case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
1811 case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
1812 /* avoid antenna B and MIMO */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001813 if (tbl->action != IWL_MIMO2_SWITCH_SISO_A)
1814 tbl->action = IWL_MIMO2_SWITCH_SISO_A;
1815 break;
Johannes Berg290f5992010-08-23 07:56:58 -07001816 case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
1817 /* avoid antenna B unless MIMO */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001818 if (tbl->action == IWL_MIMO2_SWITCH_SISO_B ||
1819 tbl->action == IWL_MIMO2_SWITCH_SISO_C)
Johannes Berg290f5992010-08-23 07:56:58 -07001820 tbl->action = IWL_MIMO2_SWITCH_SISO_A;
1821 break;
1822 default:
1823 IWL_ERR(priv, "Invalid BT load %d", priv->bt_traffic_load);
1824 break;
1825 }
1826
Johannes Berg3ad3b922009-08-07 15:41:48 -07001827 if ((iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE) &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001828 (tbl->action < IWL_MIMO2_SWITCH_SISO_A ||
1829 tbl->action > IWL_MIMO2_SWITCH_SISO_C)) {
1830 /* switch in SISO */
1831 tbl->action = IWL_MIMO2_SWITCH_SISO_A;
1832 }
Wey-Yi Guybee008b2010-08-23 07:57:04 -07001833
1834 /* configure as 1x1 if bt full concurrency */
1835 if (priv->bt_full_concurrent &&
1836 (tbl->action < IWL_MIMO2_SWITCH_SISO_A ||
1837 tbl->action > IWL_MIMO2_SWITCH_SISO_C))
1838 tbl->action = IWL_MIMO2_SWITCH_SISO_A;
1839
Wey-Yi Guyb1a78852010-07-31 08:52:03 -07001840 start_action = tbl->action;
Zhu Yib481de92007-09-25 17:54:57 -07001841 for (;;) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001842 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001843 switch (tbl->action) {
Guy Cohen3110bef2008-09-09 10:54:54 +08001844 case IWL_MIMO2_SWITCH_ANTENNA1:
1845 case IWL_MIMO2_SWITCH_ANTENNA2:
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001846 IWL_DEBUG_RATE(priv, "LQ: MIMO2 toggle Antennas\n");
Guy Cohen3110bef2008-09-09 10:54:54 +08001847
1848 if (tx_chains_num <= 2)
1849 break;
1850
1851 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1852 break;
1853
1854 memcpy(search_tbl, tbl, sz);
1855 if (rs_toggle_antenna(valid_tx_ant,
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001856 &search_tbl->current_rate, search_tbl)) {
1857 update_search_tbl_counter = 1;
Guy Cohen3110bef2008-09-09 10:54:54 +08001858 goto out;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001859 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001860 break;
1861 case IWL_MIMO2_SWITCH_SISO_A:
1862 case IWL_MIMO2_SWITCH_SISO_B:
1863 case IWL_MIMO2_SWITCH_SISO_C:
Tomas Winklere1623442009-01-27 14:27:56 -08001864 IWL_DEBUG_RATE(priv, "LQ: MIMO2 switch to SISO\n");
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001865
Ben Cahill77626352007-11-29 11:09:44 +08001866 /* Set up new search table for SISO */
Zhu Yib481de92007-09-25 17:54:57 -07001867 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001868
Guy Cohen3110bef2008-09-09 10:54:54 +08001869 if (tbl->action == IWL_MIMO2_SWITCH_SISO_A)
Guy Cohenfde0db32008-04-21 15:42:01 -07001870 search_tbl->ant_type = ANT_A;
Guy Cohen3110bef2008-09-09 10:54:54 +08001871 else if (tbl->action == IWL_MIMO2_SWITCH_SISO_B)
Guy Cohenfde0db32008-04-21 15:42:01 -07001872 search_tbl->ant_type = ANT_B;
Guy Cohen3110bef2008-09-09 10:54:54 +08001873 else
1874 search_tbl->ant_type = ANT_C;
1875
1876 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1877 break;
Zhu Yib481de92007-09-25 17:54:57 -07001878
Tomas Winklerc33104f2008-01-14 17:46:21 -08001879 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001880 search_tbl, index);
Guy Cohen3110bef2008-09-09 10:54:54 +08001881 if (!ret)
Zhu Yib481de92007-09-25 17:54:57 -07001882 goto out;
Guy Cohen3110bef2008-09-09 10:54:54 +08001883
Zhu Yib481de92007-09-25 17:54:57 -07001884 break;
1885
Guy Cohen3110bef2008-09-09 10:54:54 +08001886 case IWL_MIMO2_SWITCH_GI:
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001887 if (!tbl->is_ht40 && !(ht_cap->cap &
1888 IEEE80211_HT_CAP_SGI_20))
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001889 break;
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001890 if (tbl->is_ht40 && !(ht_cap->cap &
1891 IEEE80211_HT_CAP_SGI_40))
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001892 break;
1893
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001894 IWL_DEBUG_RATE(priv, "LQ: MIMO2 toggle SGI/NGI\n");
1895
1896 /* Set up new search table for MIMO2 */
1897 memcpy(search_tbl, tbl, sz);
1898 search_tbl->is_SGI = !tbl->is_SGI;
1899 rs_set_expected_tpt_table(lq_sta, search_tbl);
1900 /*
1901 * If active table already uses the fastest possible
1902 * modulation (dual stream with short guard interval),
1903 * and it's working well, there's no need to look
1904 * for a better type of modulation!
1905 */
1906 if (tbl->is_SGI) {
1907 s32 tpt = lq_sta->last_tpt / 100;
1908 if (tpt >= search_tbl->expected_tpt[index])
1909 break;
1910 }
1911 search_tbl->current_rate =
1912 rate_n_flags_from_tbl(priv, search_tbl,
1913 index, is_green);
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001914 update_search_tbl_counter = 1;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001915 goto out;
1916
1917 case IWL_MIMO2_SWITCH_MIMO3_ABC:
1918 IWL_DEBUG_RATE(priv, "LQ: MIMO2 switch to MIMO3\n");
1919 memcpy(search_tbl, tbl, sz);
1920 search_tbl->is_SGI = 0;
1921 search_tbl->ant_type = ANT_ABC;
1922
1923 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1924 break;
1925
1926 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1927 search_tbl, index);
1928 if (!ret)
1929 goto out;
1930
1931 break;
1932 }
1933 tbl->action++;
1934 if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
1935 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
1936
1937 if (tbl->action == start_action)
1938 break;
1939 }
1940 search_tbl->lq_type = LQ_NONE;
1941 return 0;
1942 out:
1943 lq_sta->search_better_tbl = 1;
1944 tbl->action++;
1945 if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
1946 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001947 if (update_search_tbl_counter)
1948 search_tbl->action = tbl->action;
1949
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001950 return 0;
1951
1952}
1953
1954/*
1955 * Try to switch to new modulation mode from MIMO3
1956 */
1957static int rs_move_mimo3_to_other(struct iwl_priv *priv,
1958 struct iwl_lq_sta *lq_sta,
1959 struct ieee80211_conf *conf,
1960 struct ieee80211_sta *sta, int index)
1961{
1962 s8 is_green = lq_sta->is_green;
1963 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1964 struct iwl_scale_tbl_info *search_tbl =
1965 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1966 struct iwl_rate_scale_data *window = &(tbl->win[index]);
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001967 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001968 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1969 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Wey-Yi Guyb1a78852010-07-31 08:52:03 -07001970 u8 start_action;
Emmanuel Grumbachd6189122011-08-25 23:10:39 -07001971 u8 valid_tx_ant = hw_params(priv).valid_tx_ant;
1972 u8 tx_chains_num = hw_params(priv).tx_chains_num;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001973 int ret;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001974 u8 update_search_tbl_counter = 0;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001975
Johannes Berg290f5992010-08-23 07:56:58 -07001976 switch (priv->bt_traffic_load) {
1977 case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
1978 /* nothing */
1979 break;
1980 case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
1981 case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
1982 /* avoid antenna B and MIMO */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001983 if (tbl->action != IWL_MIMO3_SWITCH_SISO_A)
Johannes Berg290f5992010-08-23 07:56:58 -07001984 tbl->action = IWL_MIMO3_SWITCH_SISO_A;
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001985 break;
Johannes Berg290f5992010-08-23 07:56:58 -07001986 case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
1987 /* avoid antenna B unless MIMO */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001988 if (tbl->action == IWL_MIMO3_SWITCH_SISO_B ||
1989 tbl->action == IWL_MIMO3_SWITCH_SISO_C)
Johannes Berg290f5992010-08-23 07:56:58 -07001990 tbl->action = IWL_MIMO3_SWITCH_SISO_A;
Johannes Berg290f5992010-08-23 07:56:58 -07001991 break;
1992 default:
1993 IWL_ERR(priv, "Invalid BT load %d", priv->bt_traffic_load);
1994 break;
1995 }
1996
Johannes Berg3ad3b922009-08-07 15:41:48 -07001997 if ((iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE) &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001998 (tbl->action < IWL_MIMO3_SWITCH_SISO_A ||
1999 tbl->action > IWL_MIMO3_SWITCH_SISO_C)) {
2000 /* switch in SISO */
2001 tbl->action = IWL_MIMO3_SWITCH_SISO_A;
2002 }
Wey-Yi Guybee008b2010-08-23 07:57:04 -07002003
2004 /* configure as 1x1 if bt full concurrency */
2005 if (priv->bt_full_concurrent &&
2006 (tbl->action < IWL_MIMO3_SWITCH_SISO_A ||
2007 tbl->action > IWL_MIMO3_SWITCH_SISO_C))
2008 tbl->action = IWL_MIMO3_SWITCH_SISO_A;
2009
Wey-Yi Guyb1a78852010-07-31 08:52:03 -07002010 start_action = tbl->action;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07002011 for (;;) {
2012 lq_sta->action_counter++;
2013 switch (tbl->action) {
2014 case IWL_MIMO3_SWITCH_ANTENNA1:
2015 case IWL_MIMO3_SWITCH_ANTENNA2:
2016 IWL_DEBUG_RATE(priv, "LQ: MIMO3 toggle Antennas\n");
2017
2018 if (tx_chains_num <= 3)
2019 break;
2020
2021 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
2022 break;
2023
2024 memcpy(search_tbl, tbl, sz);
2025 if (rs_toggle_antenna(valid_tx_ant,
2026 &search_tbl->current_rate, search_tbl))
2027 goto out;
2028 break;
2029 case IWL_MIMO3_SWITCH_SISO_A:
2030 case IWL_MIMO3_SWITCH_SISO_B:
2031 case IWL_MIMO3_SWITCH_SISO_C:
2032 IWL_DEBUG_RATE(priv, "LQ: MIMO3 switch to SISO\n");
2033
2034 /* Set up new search table for SISO */
2035 memcpy(search_tbl, tbl, sz);
2036
2037 if (tbl->action == IWL_MIMO3_SWITCH_SISO_A)
2038 search_tbl->ant_type = ANT_A;
2039 else if (tbl->action == IWL_MIMO3_SWITCH_SISO_B)
2040 search_tbl->ant_type = ANT_B;
2041 else
2042 search_tbl->ant_type = ANT_C;
2043
2044 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
2045 break;
2046
2047 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
2048 search_tbl, index);
2049 if (!ret)
2050 goto out;
2051
2052 break;
2053
2054 case IWL_MIMO3_SWITCH_MIMO2_AB:
2055 case IWL_MIMO3_SWITCH_MIMO2_AC:
2056 case IWL_MIMO3_SWITCH_MIMO2_BC:
2057 IWL_DEBUG_RATE(priv, "LQ: MIMO3 switch to MIMO2\n");
2058
2059 memcpy(search_tbl, tbl, sz);
2060 search_tbl->is_SGI = 0;
2061 if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AB)
2062 search_tbl->ant_type = ANT_AB;
2063 else if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AC)
2064 search_tbl->ant_type = ANT_AC;
2065 else
2066 search_tbl->ant_type = ANT_BC;
2067
2068 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
2069 break;
2070
2071 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
2072 search_tbl, index);
2073 if (!ret)
2074 goto out;
2075
2076 break;
2077
2078 case IWL_MIMO3_SWITCH_GI:
Daniel C Halperin28e6f482009-08-13 13:30:58 -07002079 if (!tbl->is_ht40 && !(ht_cap->cap &
2080 IEEE80211_HT_CAP_SGI_20))
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07002081 break;
Daniel C Halperin28e6f482009-08-13 13:30:58 -07002082 if (tbl->is_ht40 && !(ht_cap->cap &
2083 IEEE80211_HT_CAP_SGI_40))
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07002084 break;
2085
2086 IWL_DEBUG_RATE(priv, "LQ: MIMO3 toggle SGI/NGI\n");
Ben Cahill77626352007-11-29 11:09:44 +08002087
2088 /* Set up new search table for MIMO */
Zhu Yib481de92007-09-25 17:54:57 -07002089 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07002090 search_tbl->is_SGI = !tbl->is_SGI;
Guy Coheneecd6e52008-04-23 17:14:58 -07002091 rs_set_expected_tpt_table(lq_sta, search_tbl);
Ben Cahill77626352007-11-29 11:09:44 +08002092 /*
2093 * If active table already uses the fastest possible
2094 * modulation (dual stream with short guard interval),
2095 * and it's working well, there's no need to look
2096 * for a better type of modulation!
2097 */
Guy Cohen39e88502008-04-23 17:14:57 -07002098 if (tbl->is_SGI) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002099 s32 tpt = lq_sta->last_tpt / 100;
Guy Cohen39e88502008-04-23 17:14:57 -07002100 if (tpt >= search_tbl->expected_tpt[index])
2101 break;
Zhu Yib481de92007-09-25 17:54:57 -07002102 }
Tomas Winkler978785a2008-12-19 10:37:31 +08002103 search_tbl->current_rate =
2104 rate_n_flags_from_tbl(priv, search_tbl,
2105 index, is_green);
Mohamed Abbasd6e93392009-05-08 13:44:39 -07002106 update_search_tbl_counter = 1;
Zhu Yib481de92007-09-25 17:54:57 -07002107 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07002108 }
2109 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07002110 if (tbl->action > IWL_MIMO3_SWITCH_GI)
2111 tbl->action = IWL_MIMO3_SWITCH_ANTENNA1;
Zhu Yib481de92007-09-25 17:54:57 -07002112
2113 if (tbl->action == start_action)
2114 break;
2115 }
Guy Cohen3110bef2008-09-09 10:54:54 +08002116 search_tbl->lq_type = LQ_NONE;
Zhu Yib481de92007-09-25 17:54:57 -07002117 return 0;
2118 out:
Guy Cohen3110bef2008-09-09 10:54:54 +08002119 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07002120 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07002121 if (tbl->action > IWL_MIMO3_SWITCH_GI)
2122 tbl->action = IWL_MIMO3_SWITCH_ANTENNA1;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07002123 if (update_search_tbl_counter)
2124 search_tbl->action = tbl->action;
2125
Zhu Yib481de92007-09-25 17:54:57 -07002126 return 0;
2127
2128}
2129
Ben Cahill77626352007-11-29 11:09:44 +08002130/*
2131 * Check whether we should continue using same modulation mode, or
2132 * begin search for a new mode, based on:
2133 * 1) # tx successes or failures while using this mode
2134 * 2) # times calling this function
2135 * 3) elapsed time in this mode (not used, for now)
2136 */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07002137static void rs_stay_in_table(struct iwl_lq_sta *lq_sta, bool force_search)
Zhu Yib481de92007-09-25 17:54:57 -07002138{
Tomas Winklere227cea2008-07-18 13:53:05 +08002139 struct iwl_scale_tbl_info *tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002140 int i;
2141 int active_tbl;
2142 int flush_interval_passed = 0;
Ester Kummerbf403db2008-05-05 10:22:40 +08002143 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07002144
Ester Kummerbf403db2008-05-05 10:22:40 +08002145 priv = lq_sta->drv;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002146 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002147
Tomas Winklerc33104f2008-01-14 17:46:21 -08002148 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07002149
Ben Cahill77626352007-11-29 11:09:44 +08002150 /* If we've been disallowing search, see if we should now allow it */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002151 if (lq_sta->stay_in_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07002152
Ben Cahill77626352007-11-29 11:09:44 +08002153 /* Elapsed time using current modulation mode */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002154 if (lq_sta->flush_timer)
Zhu Yib481de92007-09-25 17:54:57 -07002155 flush_interval_passed =
Mohamed Abbas447fee72009-04-20 14:37:02 -07002156 time_after(jiffies,
2157 (unsigned long)(lq_sta->flush_timer +
Zhu Yib481de92007-09-25 17:54:57 -07002158 IWL_RATE_SCALE_FLUSH_INTVL));
2159
Ben Cahill77626352007-11-29 11:09:44 +08002160 /*
2161 * Check if we should allow search for new modulation mode.
2162 * If many frames have failed or succeeded, or we've used
2163 * this same modulation for a long time, allow search, and
2164 * reset history stats that keep track of whether we should
2165 * allow a new search. Also (below) reset all bitmaps and
2166 * stats in active history.
2167 */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07002168 if (force_search ||
2169 (lq_sta->total_failed > lq_sta->max_failure_limit) ||
Tomas Winklerc33104f2008-01-14 17:46:21 -08002170 (lq_sta->total_success > lq_sta->max_success_limit) ||
2171 ((!lq_sta->search_better_tbl) && (lq_sta->flush_timer)
Zhu Yib481de92007-09-25 17:54:57 -07002172 && (flush_interval_passed))) {
Tomas Winklere1623442009-01-27 14:27:56 -08002173 IWL_DEBUG_RATE(priv, "LQ: stay is expired %d %d %d\n:",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002174 lq_sta->total_failed,
2175 lq_sta->total_success,
Zhu Yib481de92007-09-25 17:54:57 -07002176 flush_interval_passed);
Ben Cahill77626352007-11-29 11:09:44 +08002177
2178 /* Allow search for new mode */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002179 lq_sta->stay_in_tbl = 0; /* only place reset */
2180 lq_sta->total_failed = 0;
2181 lq_sta->total_success = 0;
2182 lq_sta->flush_timer = 0;
Ben Cahill77626352007-11-29 11:09:44 +08002183
2184 /*
2185 * Else if we've used this modulation mode enough repetitions
2186 * (regardless of elapsed time or success/failure), reset
2187 * history bitmaps and rate-specific stats for all rates in
2188 * active table.
2189 */
Mohamed Abbas403ab562007-11-06 22:06:25 -08002190 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002191 lq_sta->table_count++;
2192 if (lq_sta->table_count >=
2193 lq_sta->table_count_limit) {
2194 lq_sta->table_count = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002195
Tomas Winklere1623442009-01-27 14:27:56 -08002196 IWL_DEBUG_RATE(priv, "LQ: stay in table clear win\n");
Zhu Yib481de92007-09-25 17:54:57 -07002197 for (i = 0; i < IWL_RATE_COUNT; i++)
2198 rs_rate_scale_clear_window(
2199 &(tbl->win[i]));
2200 }
2201 }
2202
Ben Cahill77626352007-11-29 11:09:44 +08002203 /* If transitioning to allow "search", reset all history
2204 * bitmaps and stats in active table (this will become the new
2205 * "search" table). */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002206 if (!lq_sta->stay_in_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07002207 for (i = 0; i < IWL_RATE_COUNT; i++)
2208 rs_rate_scale_clear_window(&(tbl->win[i]));
2209 }
2210 }
2211}
2212
Ben Cahill77626352007-11-29 11:09:44 +08002213/*
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07002214 * setup rate table in uCode
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07002215 */
Johannes Berg70817b52011-05-11 03:29:25 -07002216static void rs_update_rate_tbl(struct iwl_priv *priv,
2217 struct iwl_rxon_context *ctx,
2218 struct iwl_lq_sta *lq_sta,
2219 struct iwl_scale_tbl_info *tbl,
2220 int index, u8 is_green)
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07002221{
2222 u32 rate;
2223
2224 /* Update uCode's rate table. */
2225 rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
2226 rs_fill_link_cmd(priv, lq_sta, rate);
Johannes Berg7e6a5882010-08-23 10:46:46 +02002227 iwl_send_lq_cmd(priv, ctx, &lq_sta->lq, CMD_ASYNC, false);
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07002228}
2229
2230/*
Ben Cahill77626352007-11-29 11:09:44 +08002231 * Do rate scaling and search for new modulation mode.
2232 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002233static void rs_rate_scale_perform(struct iwl_priv *priv,
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002234 struct sk_buff *skb,
Johannes Berg4b7679a2008-09-18 18:14:18 +02002235 struct ieee80211_sta *sta,
2236 struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002237{
Johannes Berg4b7679a2008-09-18 18:14:18 +02002238 struct ieee80211_hw *hw = priv->hw;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002239 struct ieee80211_conf *conf = &hw->conf;
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002240 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2241 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002242 int low = IWL_RATE_INVALID;
2243 int high = IWL_RATE_INVALID;
2244 int index;
2245 int i;
Tomas Winklere227cea2008-07-18 13:53:05 +08002246 struct iwl_rate_scale_data *window = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002247 int current_tpt = IWL_INVALID_VALUE;
2248 int low_tpt = IWL_INVALID_VALUE;
2249 int high_tpt = IWL_INVALID_VALUE;
2250 u32 fail_count;
2251 s8 scale_action = 0;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002252 u16 rate_mask;
Zhu Yib481de92007-09-25 17:54:57 -07002253 u8 update_lq = 0;
Tomas Winklere227cea2008-07-18 13:53:05 +08002254 struct iwl_scale_tbl_info *tbl, *tbl1;
Zhu Yib481de92007-09-25 17:54:57 -07002255 u16 rate_scale_index_msk = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002256 u8 is_green = 0;
2257 u8 active_tbl = 0;
2258 u8 done_search = 0;
2259 u16 high_low;
Guy Cohena5e8b502008-05-29 16:35:11 +08002260 s32 sr;
Emmanuel Grumbach5f85a782011-08-25 23:11:18 -07002261 u8 tid = IWL_MAX_TID_COUNT;
Wey-Yi Guy86b47662009-03-10 14:35:09 -07002262 struct iwl_tid_data *tid_data;
Johannes Berg7e6a5882010-08-23 10:46:46 +02002263 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
Johannes Bergc6892902011-09-20 15:37:21 -07002264 struct iwl_rxon_context *ctx = sta_priv->ctx;
Zhu Yib481de92007-09-25 17:54:57 -07002265
Tomas Winklere1623442009-01-27 14:27:56 -08002266 IWL_DEBUG_RATE(priv, "rate scale calculate new rate for skb\n");
Zhu Yib481de92007-09-25 17:54:57 -07002267
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002268 /* Send management frames and NO_ACK data using lowest rate. */
Tomas Winkler417f1142008-11-12 13:14:06 -08002269 /* TODO: this could probably be improved.. */
2270 if (!ieee80211_is_data(hdr->frame_control) ||
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002271 info->flags & IEEE80211_TX_CTL_NO_ACK)
Zhu Yib481de92007-09-25 17:54:57 -07002272 return;
Zhu Yib481de92007-09-25 17:54:57 -07002273
Johannes Berg4b7679a2008-09-18 18:14:18 +02002274 lq_sta->supp_rates = sta->supp_rates[lq_sta->band];
Zhu Yib481de92007-09-25 17:54:57 -07002275
Ron Rindjunskyfaa29712008-06-12 09:46:58 +08002276 tid = rs_tl_add_packet(lq_sta, hdr);
Emmanuel Grumbach5f85a782011-08-25 23:11:18 -07002277 if ((tid != IWL_MAX_TID_COUNT) &&
2278 (lq_sta->tx_agg_tid_en & (1 << tid))) {
Emmanuel Grumbach04cf6822011-11-23 11:06:12 +02002279 tid_data = &priv->tid_data[lq_sta->lq.sta_id][tid];
Daniel C Halperine3949d62009-09-17 10:43:50 -07002280 if (tid_data->agg.state == IWL_AGG_OFF)
2281 lq_sta->is_agg = 0;
2282 else
2283 lq_sta->is_agg = 1;
2284 } else
2285 lq_sta->is_agg = 0;
Ron Rindjunskyfaa29712008-06-12 09:46:58 +08002286
Ben Cahill77626352007-11-29 11:09:44 +08002287 /*
2288 * Select rate-scale / modulation-mode table to work with in
2289 * the rest of this function: "search" if searching for better
2290 * modulation mode, or "active" if doing rate scaling within a mode.
2291 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002292 if (!lq_sta->search_better_tbl)
2293 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002294 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002295 active_tbl = 1 - lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002296
Tomas Winklerc33104f2008-01-14 17:46:21 -08002297 tbl = &(lq_sta->lq_info[active_tbl]);
Wey-Yi Guy2681b202009-05-21 13:44:22 -07002298 if (is_legacy(tbl->lq_type))
2299 lq_sta->is_green = 0;
2300 else
Johannes Berg7e6a5882010-08-23 10:46:46 +02002301 lq_sta->is_green = rs_use_green(sta);
Tomas Winklerc33104f2008-01-14 17:46:21 -08002302 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07002303
Ben Cahill77626352007-11-29 11:09:44 +08002304 /* current tx rate */
Johannes Bergb7e35002008-09-11 02:22:58 +02002305 index = lq_sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002306
Tomas Winklere1623442009-01-27 14:27:56 -08002307 IWL_DEBUG_RATE(priv, "Rate scale index %d for type %d\n", index,
Zhu Yib481de92007-09-25 17:54:57 -07002308 tbl->lq_type);
2309
Ben Cahill77626352007-11-29 11:09:44 +08002310 /* rates available for this association, and for modulation mode */
Guy Coheneecd6e52008-04-23 17:14:58 -07002311 rate_mask = rs_get_supported_rates(lq_sta, hdr, tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -07002312
Frans Pop91dd6c22010-03-24 14:19:58 -07002313 IWL_DEBUG_RATE(priv, "mask 0x%04X\n", rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07002314
2315 /* mask with station rate restriction */
2316 if (is_legacy(tbl->lq_type)) {
Johannes Berg8318d782008-01-24 19:38:38 +01002317 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Ben Cahill77626352007-11-29 11:09:44 +08002318 /* supp_rates has no CCK bits in A mode */
Zhu Yib481de92007-09-25 17:54:57 -07002319 rate_scale_index_msk = (u16) (rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -08002320 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
Zhu Yib481de92007-09-25 17:54:57 -07002321 else
2322 rate_scale_index_msk = (u16) (rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -08002323 lq_sta->supp_rates);
Zhu Yib481de92007-09-25 17:54:57 -07002324
2325 } else
2326 rate_scale_index_msk = rate_mask;
2327
2328 if (!rate_scale_index_msk)
2329 rate_scale_index_msk = rate_mask;
2330
Guy Cohen07bc28e2008-04-23 17:15:03 -07002331 if (!((1 << index) & rate_scale_index_msk)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002332 IWL_ERR(priv, "Current Rate is not valid\n");
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07002333 if (lq_sta->search_better_tbl) {
2334 /* revert to active table if search table is not valid*/
2335 tbl->lq_type = LQ_NONE;
2336 lq_sta->search_better_tbl = 0;
2337 tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
2338 /* get "active" rate info */
2339 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
Johannes Berg70817b52011-05-11 03:29:25 -07002340 rs_update_rate_tbl(priv, ctx, lq_sta, tbl,
2341 index, is_green);
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07002342 }
Guy Cohen07bc28e2008-04-23 17:15:03 -07002343 return;
Zhu Yib481de92007-09-25 17:54:57 -07002344 }
2345
Ben Cahill77626352007-11-29 11:09:44 +08002346 /* Get expected throughput table and history window for current rate */
Guy Cohen07bc28e2008-04-23 17:15:03 -07002347 if (!tbl->expected_tpt) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002348 IWL_ERR(priv, "tbl->expected_tpt is NULL\n");
Guy Cohen07bc28e2008-04-23 17:15:03 -07002349 return;
2350 }
Zhu Yib481de92007-09-25 17:54:57 -07002351
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002352 /* force user max rate if set by user */
2353 if ((lq_sta->max_rate_idx != -1) &&
2354 (lq_sta->max_rate_idx < index)) {
2355 index = lq_sta->max_rate_idx;
2356 update_lq = 1;
2357 window = &(tbl->win[index]);
2358 goto lq_update;
2359 }
2360
Zhu Yib481de92007-09-25 17:54:57 -07002361 window = &(tbl->win[index]);
2362
Ben Cahill77626352007-11-29 11:09:44 +08002363 /*
2364 * If there is not enough history to calculate actual average
2365 * throughput, keep analyzing results of more tx frames, without
2366 * changing rate or mode (bypass most of the rest of this function).
2367 * Set up new rate table in uCode only if old rate is not supported
2368 * in current association (use new rate found above).
2369 */
Zhu Yib481de92007-09-25 17:54:57 -07002370 fail_count = window->counter - window->success_counter;
Guy Cohen07bc28e2008-04-23 17:15:03 -07002371 if ((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
2372 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002373 IWL_DEBUG_RATE(priv, "LQ: still below TH. succ=%d total=%d "
Zhu Yib481de92007-09-25 17:54:57 -07002374 "for index %d\n",
2375 window->success_counter, window->counter, index);
Ben Cahill77626352007-11-29 11:09:44 +08002376
2377 /* Can't calculate this yet; not enough history */
Zhu Yib481de92007-09-25 17:54:57 -07002378 window->average_tpt = IWL_INVALID_VALUE;
Ben Cahill77626352007-11-29 11:09:44 +08002379
2380 /* Should we stay with this modulation mode,
2381 * or search for a new one? */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07002382 rs_stay_in_table(lq_sta, false);
Ben Cahill77626352007-11-29 11:09:44 +08002383
Zhu Yib481de92007-09-25 17:54:57 -07002384 goto out;
Guy Cohen3110bef2008-09-09 10:54:54 +08002385 }
Ben Cahill77626352007-11-29 11:09:44 +08002386 /* Else we have enough samples; calculate estimate of
2387 * actual average throughput */
Reinette Chatre3d79b2a2010-05-03 10:55:07 -07002388 if (window->average_tpt != ((window->success_ratio *
2389 tbl->expected_tpt[index] + 64) / 128)) {
2390 IWL_ERR(priv, "expected_tpt should have been calculated by now\n");
2391 window->average_tpt = ((window->success_ratio *
2392 tbl->expected_tpt[index] + 64) / 128);
2393 }
Zhu Yib481de92007-09-25 17:54:57 -07002394
Ben Cahill77626352007-11-29 11:09:44 +08002395 /* If we are searching for better modulation mode, check success. */
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002396 if (lq_sta->search_better_tbl &&
Johannes Berg3ad3b922009-08-07 15:41:48 -07002397 (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_MULTI)) {
Ben Cahill77626352007-11-29 11:09:44 +08002398 /* If good success, continue using the "search" mode;
2399 * no need to send new link quality command, since we're
2400 * continuing to use the setup that we've been trying. */
Guy Cohen07bc28e2008-04-23 17:15:03 -07002401 if (window->average_tpt > lq_sta->last_tpt) {
2402
Tomas Winklere1623442009-01-27 14:27:56 -08002403 IWL_DEBUG_RATE(priv, "LQ: SWITCHING TO NEW TABLE "
Guy Cohen07bc28e2008-04-23 17:15:03 -07002404 "suc=%d cur-tpt=%d old-tpt=%d\n",
2405 window->success_ratio,
2406 window->average_tpt,
2407 lq_sta->last_tpt);
2408
2409 if (!is_legacy(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08002410 lq_sta->enable_counter = 1;
Guy Cohen07bc28e2008-04-23 17:15:03 -07002411
Ben Cahill77626352007-11-29 11:09:44 +08002412 /* Swap tables; "search" becomes "active" */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002413 lq_sta->active_tbl = active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002414 current_tpt = window->average_tpt;
Ben Cahill77626352007-11-29 11:09:44 +08002415
2416 /* Else poor success; go back to mode in "active" table */
Zhu Yib481de92007-09-25 17:54:57 -07002417 } else {
Guy Cohen07bc28e2008-04-23 17:15:03 -07002418
Tomas Winklere1623442009-01-27 14:27:56 -08002419 IWL_DEBUG_RATE(priv, "LQ: GOING BACK TO THE OLD TABLE "
Guy Cohen07bc28e2008-04-23 17:15:03 -07002420 "suc=%d cur-tpt=%d old-tpt=%d\n",
2421 window->success_ratio,
2422 window->average_tpt,
2423 lq_sta->last_tpt);
2424
Ben Cahill77626352007-11-29 11:09:44 +08002425 /* Nullify "search" table */
Zhu Yib481de92007-09-25 17:54:57 -07002426 tbl->lq_type = LQ_NONE;
Ben Cahill77626352007-11-29 11:09:44 +08002427
2428 /* Revert to "active" table */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002429 active_tbl = lq_sta->active_tbl;
2430 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07002431
Ben Cahill77626352007-11-29 11:09:44 +08002432 /* Revert to "active" rate and throughput info */
Tomas Winklere7d326a2008-06-12 09:47:11 +08002433 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
Tomas Winklerc33104f2008-01-14 17:46:21 -08002434 current_tpt = lq_sta->last_tpt;
Ben Cahill77626352007-11-29 11:09:44 +08002435
2436 /* Need to set up a new rate table in uCode */
2437 update_lq = 1;
Zhu Yib481de92007-09-25 17:54:57 -07002438 }
Ben Cahill77626352007-11-29 11:09:44 +08002439
2440 /* Either way, we've made a decision; modulation mode
2441 * search is done, allow rate adjustment next time. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002442 lq_sta->search_better_tbl = 0;
Ben Cahill77626352007-11-29 11:09:44 +08002443 done_search = 1; /* Don't switch modes below! */
Zhu Yib481de92007-09-25 17:54:57 -07002444 goto lq_update;
2445 }
2446
Ben Cahill77626352007-11-29 11:09:44 +08002447 /* (Else) not in search of better modulation mode, try for better
2448 * starting rate, while staying in this mode. */
Ester Kummerbf403db2008-05-05 10:22:40 +08002449 high_low = rs_get_adjacent_rate(priv, index, rate_scale_index_msk,
Zhu Yib481de92007-09-25 17:54:57 -07002450 tbl->lq_type);
2451 low = high_low & 0xff;
2452 high = (high_low >> 8) & 0xff;
2453
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002454 /* If user set max rate, dont allow higher than user constrain */
2455 if ((lq_sta->max_rate_idx != -1) &&
2456 (lq_sta->max_rate_idx < high))
2457 high = IWL_RATE_INVALID;
2458
Guy Cohena5e8b502008-05-29 16:35:11 +08002459 sr = window->success_ratio;
2460
Ben Cahill77626352007-11-29 11:09:44 +08002461 /* Collect measured throughputs for current and adjacent rates */
Zhu Yib481de92007-09-25 17:54:57 -07002462 current_tpt = window->average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07002463 if (low != IWL_RATE_INVALID)
2464 low_tpt = tbl->win[low].average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07002465 if (high != IWL_RATE_INVALID)
2466 high_tpt = tbl->win[high].average_tpt;
2467
Guy Cohena5e8b502008-05-29 16:35:11 +08002468 scale_action = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002469
Ben Cahill77626352007-11-29 11:09:44 +08002470 /* Too many failures, decrease rate */
Guy Cohena5e8b502008-05-29 16:35:11 +08002471 if ((sr <= IWL_RATE_DECREASE_TH) || (current_tpt == 0)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002472 IWL_DEBUG_RATE(priv, "decrease rate because of low success_ratio\n");
Zhu Yib481de92007-09-25 17:54:57 -07002473 scale_action = -1;
Ben Cahill77626352007-11-29 11:09:44 +08002474
2475 /* No throughput measured yet for adjacent rates; try increase. */
Zhu Yib481de92007-09-25 17:54:57 -07002476 } else if ((low_tpt == IWL_INVALID_VALUE) &&
Guy Cohena5e8b502008-05-29 16:35:11 +08002477 (high_tpt == IWL_INVALID_VALUE)) {
2478
2479 if (high != IWL_RATE_INVALID && sr >= IWL_RATE_INCREASE_TH)
2480 scale_action = 1;
2481 else if (low != IWL_RATE_INVALID)
Wey-Yi Guy8fe72312009-03-10 14:35:10 -07002482 scale_action = 0;
Guy Cohena5e8b502008-05-29 16:35:11 +08002483 }
Ben Cahill77626352007-11-29 11:09:44 +08002484
2485 /* Both adjacent throughputs are measured, but neither one has better
2486 * throughput; we're using the best rate, don't change it! */
Zhu Yib481de92007-09-25 17:54:57 -07002487 else if ((low_tpt != IWL_INVALID_VALUE) &&
2488 (high_tpt != IWL_INVALID_VALUE) &&
2489 (low_tpt < current_tpt) &&
2490 (high_tpt < current_tpt))
2491 scale_action = 0;
Ben Cahill77626352007-11-29 11:09:44 +08002492
2493 /* At least one adjacent rate's throughput is measured,
2494 * and may have better performance. */
Zhu Yib481de92007-09-25 17:54:57 -07002495 else {
Ben Cahill77626352007-11-29 11:09:44 +08002496 /* Higher adjacent rate's throughput is measured */
Zhu Yib481de92007-09-25 17:54:57 -07002497 if (high_tpt != IWL_INVALID_VALUE) {
Ben Cahill77626352007-11-29 11:09:44 +08002498 /* Higher rate has better throughput */
Guy Cohena5e8b502008-05-29 16:35:11 +08002499 if (high_tpt > current_tpt &&
2500 sr >= IWL_RATE_INCREASE_TH) {
Zhu Yib481de92007-09-25 17:54:57 -07002501 scale_action = 1;
Guy Cohena5e8b502008-05-29 16:35:11 +08002502 } else {
Wey-Yi Guy8fe72312009-03-10 14:35:10 -07002503 scale_action = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002504 }
Ben Cahill77626352007-11-29 11:09:44 +08002505
2506 /* Lower adjacent rate's throughput is measured */
Zhu Yib481de92007-09-25 17:54:57 -07002507 } else if (low_tpt != IWL_INVALID_VALUE) {
Ben Cahill77626352007-11-29 11:09:44 +08002508 /* Lower rate has better throughput */
Zhu Yib481de92007-09-25 17:54:57 -07002509 if (low_tpt > current_tpt) {
Tomas Winklere1623442009-01-27 14:27:56 -08002510 IWL_DEBUG_RATE(priv,
2511 "decrease rate because of low tpt\n");
Zhu Yib481de92007-09-25 17:54:57 -07002512 scale_action = -1;
Guy Cohena5e8b502008-05-29 16:35:11 +08002513 } else if (sr >= IWL_RATE_INCREASE_TH) {
Zhu Yib481de92007-09-25 17:54:57 -07002514 scale_action = 1;
Guy Cohena5e8b502008-05-29 16:35:11 +08002515 }
Zhu Yib481de92007-09-25 17:54:57 -07002516 }
2517 }
2518
Ben Cahill77626352007-11-29 11:09:44 +08002519 /* Sanity check; asked for decrease, but success rate or throughput
2520 * has been good at old rate. Don't change it. */
Guy Cohena5e8b502008-05-29 16:35:11 +08002521 if ((scale_action == -1) && (low != IWL_RATE_INVALID) &&
2522 ((sr > IWL_RATE_HIGH_TH) ||
Zhu Yib481de92007-09-25 17:54:57 -07002523 (current_tpt > (100 * tbl->expected_tpt[low]))))
Zhu Yib481de92007-09-25 17:54:57 -07002524 scale_action = 0;
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002525 if (!iwl_ht_enabled(priv) && !is_legacy(tbl->lq_type))
2526 scale_action = -1;
Johannes Berg3ad3b922009-08-07 15:41:48 -07002527 if (iwl_tx_ant_restriction(priv) != IWL_ANT_OK_MULTI &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002528 (is_mimo2(tbl->lq_type) || is_mimo3(tbl->lq_type)))
2529 scale_action = -1;
Johannes Berg290f5992010-08-23 07:56:58 -07002530
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07002531 if ((priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH) &&
2532 (is_mimo2(tbl->lq_type) || is_mimo3(tbl->lq_type))) {
2533 if (lq_sta->last_bt_traffic > priv->bt_traffic_load) {
2534 /*
2535 * don't set scale_action, don't want to scale up if
2536 * the rate scale doesn't otherwise think that is a
2537 * good idea.
2538 */
2539 } else if (lq_sta->last_bt_traffic <= priv->bt_traffic_load) {
2540 scale_action = -1;
2541 }
2542 }
2543 lq_sta->last_bt_traffic = priv->bt_traffic_load;
2544
2545 if ((priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH) &&
2546 (is_mimo2(tbl->lq_type) || is_mimo3(tbl->lq_type))) {
2547 /* search for a new modulation */
2548 rs_stay_in_table(lq_sta, true);
2549 goto lq_update;
Johannes Berg290f5992010-08-23 07:56:58 -07002550 }
2551
Zhu Yib481de92007-09-25 17:54:57 -07002552 switch (scale_action) {
2553 case -1:
Ben Cahill77626352007-11-29 11:09:44 +08002554 /* Decrease starting rate, update uCode's rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002555 if (low != IWL_RATE_INVALID) {
2556 update_lq = 1;
2557 index = low;
2558 }
Mohamed Abbas447fee72009-04-20 14:37:02 -07002559
Zhu Yib481de92007-09-25 17:54:57 -07002560 break;
2561 case 1:
Ben Cahill77626352007-11-29 11:09:44 +08002562 /* Increase starting rate, update uCode's rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002563 if (high != IWL_RATE_INVALID) {
2564 update_lq = 1;
2565 index = high;
2566 }
2567
2568 break;
2569 case 0:
Ben Cahill77626352007-11-29 11:09:44 +08002570 /* No change */
Zhu Yib481de92007-09-25 17:54:57 -07002571 default:
2572 break;
2573 }
2574
Tomas Winklere1623442009-01-27 14:27:56 -08002575 IWL_DEBUG_RATE(priv, "choose rate scale index %d action %d low %d "
Zhu Yib481de92007-09-25 17:54:57 -07002576 "high %d type %d\n",
2577 index, scale_action, low, high, tbl->lq_type);
2578
Guy Cohena5e8b502008-05-29 16:35:11 +08002579lq_update:
Ben Cahill77626352007-11-29 11:09:44 +08002580 /* Replace uCode's rate table for the destination station. */
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07002581 if (update_lq)
Johannes Berg70817b52011-05-11 03:29:25 -07002582 rs_update_rate_tbl(priv, ctx, lq_sta, tbl, index, is_green);
Ben Cahill77626352007-11-29 11:09:44 +08002583
Johannes Berg3ad3b922009-08-07 15:41:48 -07002584 if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_MULTI) {
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002585 /* Should we stay with this modulation mode,
2586 * or search for a new one? */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07002587 rs_stay_in_table(lq_sta, false);
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002588 }
Ben Cahill77626352007-11-29 11:09:44 +08002589 /*
2590 * Search for new modulation mode if we're:
2591 * 1) Not changing rates right now
2592 * 2) Not just finishing up a search
2593 * 3) Allowing a new search
2594 */
Guy Cohen47cfd462008-05-27 11:29:34 +08002595 if (!update_lq && !done_search && !lq_sta->stay_in_tbl && window->counter) {
Ben Cahill77626352007-11-29 11:09:44 +08002596 /* Save current throughput to compare with "search" throughput*/
Tomas Winklerc33104f2008-01-14 17:46:21 -08002597 lq_sta->last_tpt = current_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07002598
Ben Cahill77626352007-11-29 11:09:44 +08002599 /* Select a new "search" modulation mode to try.
2600 * If one is found, set up the new "search" table. */
Zhu Yib481de92007-09-25 17:54:57 -07002601 if (is_legacy(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08002602 rs_move_legacy_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07002603 else if (is_siso(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08002604 rs_move_siso_to_other(priv, lq_sta, conf, sta, index);
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07002605 else if (is_mimo2(tbl->lq_type))
2606 rs_move_mimo2_to_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07002607 else
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07002608 rs_move_mimo3_to_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07002609
Ben Cahill77626352007-11-29 11:09:44 +08002610 /* If new "search" mode was selected, set up in uCode table */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002611 if (lq_sta->search_better_tbl) {
Ben Cahill77626352007-11-29 11:09:44 +08002612 /* Access the "search" table, clear its history. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002613 tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Zhu Yib481de92007-09-25 17:54:57 -07002614 for (i = 0; i < IWL_RATE_COUNT; i++)
2615 rs_rate_scale_clear_window(&(tbl->win[i]));
2616
Ben Cahill77626352007-11-29 11:09:44 +08002617 /* Use new "search" start rate */
Tomas Winklere7d326a2008-06-12 09:47:11 +08002618 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002619
Tomas Winklere1623442009-01-27 14:27:56 -08002620 IWL_DEBUG_RATE(priv, "Switch current mcs: %X index: %d\n",
Guy Cohen39e88502008-04-23 17:14:57 -07002621 tbl->current_rate, index);
Guy Cohen07bc28e2008-04-23 17:15:03 -07002622 rs_fill_link_cmd(priv, lq_sta, tbl->current_rate);
Johannes Berg7e6a5882010-08-23 10:46:46 +02002623 iwl_send_lq_cmd(priv, ctx, &lq_sta->lq, CMD_ASYNC, false);
Mohamed Abbas447fee72009-04-20 14:37:02 -07002624 } else
2625 done_search = 1;
2626 }
Zhu Yib481de92007-09-25 17:54:57 -07002627
Mohamed Abbas447fee72009-04-20 14:37:02 -07002628 if (done_search && !lq_sta->stay_in_tbl) {
Ben Cahill77626352007-11-29 11:09:44 +08002629 /* If the "active" (non-search) mode was legacy,
2630 * and we've tried switching antennas,
2631 * but we haven't been able to try HT modes (not available),
2632 * stay with best antenna legacy modulation for a while
2633 * before next round of mode comparisons. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002634 tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
Luis R. Rodriguezde27e642008-12-23 15:58:44 -08002635 if (is_legacy(tbl1->lq_type) && !conf_is_ht(conf) &&
Mohamed Abbasd6e93392009-05-08 13:44:39 -07002636 lq_sta->action_counter > tbl1->max_search) {
Tomas Winklere1623442009-01-27 14:27:56 -08002637 IWL_DEBUG_RATE(priv, "LQ: STAY in legacy table\n");
Ester Kummerbf403db2008-05-05 10:22:40 +08002638 rs_set_stay_in_table(priv, 1, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002639 }
2640
Ben Cahill77626352007-11-29 11:09:44 +08002641 /* If we're in an HT mode, and all 3 mode switch actions
2642 * have been tried and compared, stay in this best modulation
2643 * mode for a while before next round of mode comparisons. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002644 if (lq_sta->enable_counter &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002645 (lq_sta->action_counter >= tbl1->max_search) &&
2646 iwl_ht_enabled(priv)) {
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002647 if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
2648 (lq_sta->tx_agg_tid_en & (1 << tid)) &&
Emmanuel Grumbach5f85a782011-08-25 23:11:18 -07002649 (tid != IWL_MAX_TID_COUNT)) {
2650 u8 sta_id = lq_sta->lq.sta_id;
Emmanuel Grumbach04cf6822011-11-23 11:06:12 +02002651 tid_data = &priv->tid_data[sta_id][tid];
Wey-Yi Guy86b47662009-03-10 14:35:09 -07002652 if (tid_data->agg.state == IWL_AGG_OFF) {
2653 IWL_DEBUG_RATE(priv,
2654 "try to aggregate tid %d\n",
2655 tid);
2656 rs_tl_turn_on_agg(priv, tid,
2657 lq_sta, sta);
2658 }
Zhu Yib481de92007-09-25 17:54:57 -07002659 }
Ester Kummerbf403db2008-05-05 10:22:40 +08002660 rs_set_stay_in_table(priv, 0, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002661 }
Zhu Yib481de92007-09-25 17:54:57 -07002662 }
2663
2664out:
Tomas Winkler978785a2008-12-19 10:37:31 +08002665 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
Wey-Yi Guyc3322b32011-10-10 07:26:58 -07002666 lq_sta->last_txrate_idx = index;
Zhu Yib481de92007-09-25 17:54:57 -07002667}
2668
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08002669/**
2670 * rs_initialize_lq - Initialize a station's hardware rate table
2671 *
2672 * The uCode's station table contains a table of fallback rates
2673 * for automatic fallback during transmission.
2674 *
2675 * NOTE: This sets up a default set of values. These will be replaced later
2676 * if the driver's iwl-agn-rs rate scaling algorithm is used, instead of
2677 * rc80211_simple.
2678 *
2679 * NOTE: Run REPLY_ADD_STA command to set up station table entry, before
2680 * calling this function (which runs REPLY_TX_LINK_QUALITY_CMD,
2681 * which requires station table entry to exist).
2682 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002683static void rs_initialize_lq(struct iwl_priv *priv,
Johannes Berg4b7679a2008-09-18 18:14:18 +02002684 struct ieee80211_sta *sta,
2685 struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002686{
Tomas Winklere227cea2008-07-18 13:53:05 +08002687 struct iwl_scale_tbl_info *tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002688 int rate_idx;
Guy Cohenaade00c2008-04-23 17:14:59 -07002689 int i;
Guy Cohen39e88502008-04-23 17:14:57 -07002690 u32 rate;
Johannes Berg7e6a5882010-08-23 10:46:46 +02002691 u8 use_green = rs_use_green(sta);
Guy Cohenaade00c2008-04-23 17:14:59 -07002692 u8 active_tbl = 0;
2693 u8 valid_tx_ant;
Johannes Berg7e6a5882010-08-23 10:46:46 +02002694 struct iwl_station_priv *sta_priv;
2695 struct iwl_rxon_context *ctx;
Zhu Yib481de92007-09-25 17:54:57 -07002696
Johannes Berg4b7679a2008-09-18 18:14:18 +02002697 if (!sta || !lq_sta)
Johannes Berg7e6a5882010-08-23 10:46:46 +02002698 return;
2699
2700 sta_priv = (void *)sta->drv_priv;
Johannes Bergc6892902011-09-20 15:37:21 -07002701 ctx = sta_priv->ctx;
Zhu Yib481de92007-09-25 17:54:57 -07002702
Johannes Bergb7e35002008-09-11 02:22:58 +02002703 i = lq_sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002704
Emmanuel Grumbachd6189122011-08-25 23:10:39 -07002705 valid_tx_ant = hw_params(priv).valid_tx_ant;
Guy Cohenaade00c2008-04-23 17:14:59 -07002706
Tomas Winklerc33104f2008-01-14 17:46:21 -08002707 if (!lq_sta->search_better_tbl)
2708 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002709 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002710 active_tbl = 1 - lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002711
Tomas Winklerc33104f2008-01-14 17:46:21 -08002712 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07002713
2714 if ((i < 0) || (i >= IWL_RATE_COUNT))
2715 i = 0;
2716
Tomas Winkler1826dcc2008-05-15 13:54:02 +08002717 rate = iwl_rates[i].plcp;
Winkler, Tomaseb48dca2008-10-29 14:05:48 -07002718 tbl->ant_type = first_antenna(valid_tx_ant);
2719 rate |= tbl->ant_type << RATE_MCS_ANT_POS;
Zhu Yib481de92007-09-25 17:54:57 -07002720
2721 if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
Guy Cohen39e88502008-04-23 17:14:57 -07002722 rate |= RATE_MCS_CCK_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002723
Guy Cohen39e88502008-04-23 17:14:57 -07002724 rs_get_tbl_info_from_mcs(rate, priv->band, tbl, &rate_idx);
Guy Cohenaade00c2008-04-23 17:14:59 -07002725 if (!rs_is_valid_ant(valid_tx_ant, tbl->ant_type))
2726 rs_toggle_antenna(valid_tx_ant, &rate, tbl);
Zhu Yib481de92007-09-25 17:54:57 -07002727
Tomas Winkler978785a2008-12-19 10:37:31 +08002728 rate = rate_n_flags_from_tbl(priv, tbl, rate_idx, use_green);
Guy Cohen39e88502008-04-23 17:14:57 -07002729 tbl->current_rate = rate;
Guy Coheneecd6e52008-04-23 17:14:58 -07002730 rs_set_expected_tpt_table(lq_sta, tbl);
Guy Cohen07bc28e2008-04-23 17:15:03 -07002731 rs_fill_link_cmd(NULL, lq_sta, rate);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08002732 priv->stations[lq_sta->lq.sta_id].lq = &lq_sta->lq;
Johannes Berg7e6a5882010-08-23 10:46:46 +02002733 iwl_send_lq_cmd(priv, ctx, &lq_sta->lq, CMD_SYNC, true);
Zhu Yib481de92007-09-25 17:54:57 -07002734}
2735
Johannes Berge6a98542008-10-21 12:40:02 +02002736static void rs_get_rate(void *priv_r, struct ieee80211_sta *sta, void *priv_sta,
2737 struct ieee80211_tx_rate_control *txrc)
Zhu Yib481de92007-09-25 17:54:57 -07002738{
2739
Johannes Berge6a98542008-10-21 12:40:02 +02002740 struct sk_buff *skb = txrc->skb;
2741 struct ieee80211_supported_band *sband = txrc->sband;
Emmanuel Grumbachd0f76d62012-02-09 16:08:15 +02002742 struct iwl_op_mode *op_mode __maybe_unused =
2743 (struct iwl_op_mode *)priv_r;
2744 struct iwl_priv *priv __maybe_unused = IWL_OP_MODE_GET_DVM(op_mode);
Johannes Berge6a98542008-10-21 12:40:02 +02002745 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Tomas Winkler417f1142008-11-12 13:14:06 -08002746 struct iwl_lq_sta *lq_sta = priv_sta;
2747 int rate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002748
Tomas Winklere1623442009-01-27 14:27:56 -08002749 IWL_DEBUG_RATE_LIMIT(priv, "rate scale calculate new rate for skb\n");
Zhu Yib481de92007-09-25 17:54:57 -07002750
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002751 /* Get max rate if user set max rate */
2752 if (lq_sta) {
2753 lq_sta->max_rate_idx = txrc->max_rate_idx;
2754 if ((sband->band == IEEE80211_BAND_5GHZ) &&
2755 (lq_sta->max_rate_idx != -1))
2756 lq_sta->max_rate_idx += IWL_FIRST_OFDM_RATE;
2757 if ((lq_sta->max_rate_idx < 0) ||
2758 (lq_sta->max_rate_idx >= IWL_RATE_COUNT))
2759 lq_sta->max_rate_idx = -1;
2760 }
2761
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08002762 /* Treat uninitialized rate scaling data same as non-existing. */
2763 if (lq_sta && !lq_sta->drv) {
2764 IWL_DEBUG_RATE(priv, "Rate scaling not initialized yet.\n");
2765 priv_sta = NULL;
2766 }
2767
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002768 /* Send management frames and NO_ACK data using lowest rate. */
Luis R. Rodriguez4c6d4f52009-07-16 10:05:41 -07002769 if (rate_control_send_low(sta, priv_sta, txrc))
Johannes Berg4b7679a2008-09-18 18:14:18 +02002770 return;
Zhu Yib481de92007-09-25 17:54:57 -07002771
Tomas Winkler417f1142008-11-12 13:14:06 -08002772 rate_idx = lq_sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002773
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002774 if (lq_sta->last_rate_n_flags & RATE_MCS_HT_MSK) {
Tomas Winkler417f1142008-11-12 13:14:06 -08002775 rate_idx -= IWL_FIRST_OFDM_RATE;
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002776 /* 6M and 9M shared same MCS index */
2777 rate_idx = (rate_idx > 0) ? (rate_idx - 1) : 0;
2778 if (rs_extract_rate(lq_sta->last_rate_n_flags) >=
2779 IWL_RATE_MIMO3_6M_PLCP)
2780 rate_idx = rate_idx + (2 * MCS_INDEX_PER_STREAM);
2781 else if (rs_extract_rate(lq_sta->last_rate_n_flags) >=
2782 IWL_RATE_MIMO2_6M_PLCP)
2783 rate_idx = rate_idx + MCS_INDEX_PER_STREAM;
2784 info->control.rates[0].flags = IEEE80211_TX_RC_MCS;
2785 if (lq_sta->last_rate_n_flags & RATE_MCS_SGI_MSK)
2786 info->control.rates[0].flags |= IEEE80211_TX_RC_SHORT_GI;
2787 if (lq_sta->last_rate_n_flags & RATE_MCS_DUP_MSK)
2788 info->control.rates[0].flags |= IEEE80211_TX_RC_DUP_DATA;
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07002789 if (lq_sta->last_rate_n_flags & RATE_MCS_HT40_MSK)
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002790 info->control.rates[0].flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
2791 if (lq_sta->last_rate_n_flags & RATE_MCS_GF_MSK)
2792 info->control.rates[0].flags |= IEEE80211_TX_RC_GREEN_FIELD;
2793 } else {
Daniel C Halperin50273092009-08-28 09:44:45 -07002794 /* Check for invalid rates */
2795 if ((rate_idx < 0) || (rate_idx >= IWL_RATE_COUNT_LEGACY) ||
2796 ((sband->band == IEEE80211_BAND_5GHZ) &&
2797 (rate_idx < IWL_FIRST_OFDM_RATE)))
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002798 rate_idx = rate_lowest_index(sband, sta);
Daniel C Halperin50273092009-08-28 09:44:45 -07002799 /* On valid 5 GHz rate, adjust index */
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002800 else if (sband->band == IEEE80211_BAND_5GHZ)
2801 rate_idx -= IWL_FIRST_OFDM_RATE;
Daniel C Halperin7ebaeff2009-08-21 13:34:20 -07002802 info->control.rates[0].flags = 0;
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002803 }
Tomas Winkler417f1142008-11-12 13:14:06 -08002804 info->control.rates[0].idx = rate_idx;
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002805
Zhu Yib481de92007-09-25 17:54:57 -07002806}
2807
Johannes Berg4b7679a2008-09-18 18:14:18 +02002808static void *rs_alloc_sta(void *priv_rate, struct ieee80211_sta *sta,
2809 gfp_t gfp)
Zhu Yib481de92007-09-25 17:54:57 -07002810{
Reinette Chatre8d9698b2009-10-16 14:25:55 -07002811 struct iwl_station_priv *sta_priv = (struct iwl_station_priv *) sta->drv_priv;
Emmanuel Grumbachd0f76d62012-02-09 16:08:15 +02002812 struct iwl_op_mode *op_mode __maybe_unused =
2813 (struct iwl_op_mode *)priv_rate;
2814 struct iwl_priv *priv __maybe_unused = IWL_OP_MODE_GET_DVM(op_mode);
Zhu Yib481de92007-09-25 17:54:57 -07002815
Tomas Winklere1623442009-01-27 14:27:56 -08002816 IWL_DEBUG_RATE(priv, "create station rate scale window\n");
Zhu Yib481de92007-09-25 17:54:57 -07002817
Johannes Berg08960de2011-04-05 09:41:53 -07002818 return &sta_priv->lq_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002819}
2820
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08002821/*
2822 * Called after adding a new station to initialize rate scaling
2823 */
2824void iwl_rs_rate_init(struct iwl_priv *priv, struct ieee80211_sta *sta, u8 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07002825{
2826 int i, j;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08002827 struct ieee80211_hw *hw = priv->hw;
Johannes Berg4b7679a2008-09-18 18:14:18 +02002828 struct ieee80211_conf *conf = &priv->hw->conf;
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002829 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08002830 struct iwl_station_priv *sta_priv;
2831 struct iwl_lq_sta *lq_sta;
2832 struct ieee80211_supported_band *sband;
Zhu Yib481de92007-09-25 17:54:57 -07002833
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08002834 sta_priv = (struct iwl_station_priv *) sta->drv_priv;
2835 lq_sta = &sta_priv->lq_sta;
2836 sband = hw->wiphy->bands[conf->channel->band];
2837
2838
2839 lq_sta->lq.sta_id = sta_id;
Reinette Chatre5ad13f82009-10-30 14:36:14 -07002840
2841 for (j = 0; j < LQ_SIZE; j++)
2842 for (i = 0; i < IWL_RATE_COUNT; i++)
2843 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
2844
Tomas Winklerc33104f2008-01-14 17:46:21 -08002845 lq_sta->flush_timer = 0;
Johannes Berg4b7679a2008-09-18 18:14:18 +02002846 lq_sta->supp_rates = sta->supp_rates[sband->band];
Zhu Yib481de92007-09-25 17:54:57 -07002847 for (j = 0; j < LQ_SIZE; j++)
2848 for (i = 0; i < IWL_RATE_COUNT; i++)
Tomas Winkler3ac7f142008-07-21 02:40:14 +03002849 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
Zhu Yib481de92007-09-25 17:54:57 -07002850
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08002851 IWL_DEBUG_RATE(priv, "LQ: *** rate scale station global init for station %d ***\n",
2852 sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07002853 /* TODO: what is a good starting rate for STA? About middle? Maybe not
2854 * the lowest or the highest rate.. Could consider using RSSI from
2855 * previous packets? Need to have IEEE 802.1X auth succeed immediately
2856 * after assoc.. */
2857
Tomas Winklerc33104f2008-01-14 17:46:21 -08002858 lq_sta->is_dup = 0;
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002859 lq_sta->max_rate_idx = -1;
Abbas, Mohamed7d049e52009-01-20 21:33:53 -08002860 lq_sta->missed_rate_counter = IWL_MISSED_RATE_MAX;
Johannes Berg7e6a5882010-08-23 10:46:46 +02002861 lq_sta->is_green = rs_use_green(sta);
Guy Cohen39e88502008-04-23 17:14:57 -07002862 lq_sta->active_legacy_rate = priv->active_rate & ~(0x1000);
Johannes Berg8318d782008-01-24 19:38:38 +01002863 lq_sta->band = priv->band;
Ben Cahill77626352007-11-29 11:09:44 +08002864 /*
2865 * active_siso_rate mask includes 9 MBits (bit 5), and CCK (bits 0-3),
2866 * supp_rates[] does not; shift to convert format, force 9 MBits off.
2867 */
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002868 lq_sta->active_siso_rate = ht_cap->mcs.rx_mask[0] << 1;
2869 lq_sta->active_siso_rate |= ht_cap->mcs.rx_mask[0] & 0x1;
Guy Cohenfde0db32008-04-21 15:42:01 -07002870 lq_sta->active_siso_rate &= ~((u16)0x2);
2871 lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -07002872
Ben Cahill77626352007-11-29 11:09:44 +08002873 /* Same here */
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002874 lq_sta->active_mimo2_rate = ht_cap->mcs.rx_mask[1] << 1;
2875 lq_sta->active_mimo2_rate |= ht_cap->mcs.rx_mask[1] & 0x1;
Guy Cohenfde0db32008-04-21 15:42:01 -07002876 lq_sta->active_mimo2_rate &= ~((u16)0x2);
2877 lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE;
2878
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002879 lq_sta->active_mimo3_rate = ht_cap->mcs.rx_mask[2] << 1;
2880 lq_sta->active_mimo3_rate |= ht_cap->mcs.rx_mask[2] & 0x1;
Guy Cohenfde0db32008-04-21 15:42:01 -07002881 lq_sta->active_mimo3_rate &= ~((u16)0x2);
2882 lq_sta->active_mimo3_rate <<= IWL_FIRST_OFDM_RATE;
2883
Tomas Winklere1623442009-01-27 14:27:56 -08002884 IWL_DEBUG_RATE(priv, "SISO-RATE=%X MIMO2-RATE=%X MIMO3-RATE=%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002885 lq_sta->active_siso_rate,
Guy Cohenfde0db32008-04-21 15:42:01 -07002886 lq_sta->active_mimo2_rate,
2887 lq_sta->active_mimo3_rate);
2888
Tomas Winklera96a27f2008-10-23 23:48:56 -07002889 /* These values will be overridden later */
Wey-Yi Guy3a23d692010-04-03 16:44:39 -07002890 lq_sta->lq.general_params.single_stream_ant_msk =
Emmanuel Grumbachd6189122011-08-25 23:10:39 -07002891 first_antenna(hw_params(priv).valid_tx_ant);
Wey-Yi Guy3a23d692010-04-03 16:44:39 -07002892 lq_sta->lq.general_params.dual_stream_ant_msk =
Emmanuel Grumbachd6189122011-08-25 23:10:39 -07002893 hw_params(priv).valid_tx_ant &
2894 ~first_antenna(hw_params(priv).valid_tx_ant);
Wey-Yi Guy3a23d692010-04-03 16:44:39 -07002895 if (!lq_sta->lq.general_params.dual_stream_ant_msk) {
2896 lq_sta->lq.general_params.dual_stream_ant_msk = ANT_AB;
Emmanuel Grumbachd6189122011-08-25 23:10:39 -07002897 } else if (num_of_ant(hw_params(priv).valid_tx_ant) == 2) {
Wey-Yi Guy3a23d692010-04-03 16:44:39 -07002898 lq_sta->lq.general_params.dual_stream_ant_msk =
Emmanuel Grumbachd6189122011-08-25 23:10:39 -07002899 hw_params(priv).valid_tx_ant;
Wey-Yi Guy3a23d692010-04-03 16:44:39 -07002900 }
Guy Cohen07bc28e2008-04-23 17:15:03 -07002901
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002902 /* as default allow aggregation for all tids */
2903 lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002904 lq_sta->drv = priv;
Zhu Yib481de92007-09-25 17:54:57 -07002905
Daniel C Halperin50273092009-08-28 09:44:45 -07002906 /* Set last_txrate_idx to lowest rate */
2907 lq_sta->last_txrate_idx = rate_lowest_index(sband, sta);
2908 if (sband->band == IEEE80211_BAND_5GHZ)
2909 lq_sta->last_txrate_idx += IWL_FIRST_OFDM_RATE;
Daniel C Halperine3949d62009-09-17 10:43:50 -07002910 lq_sta->is_agg = 0;
Wey-Yi Guy5ef15cc2011-11-30 13:24:06 -08002911#ifdef CONFIG_IWLWIFI_DEVICE_TESTMODE
Wey-Yi Guy4e308112011-07-08 08:46:28 -07002912 priv->tm_fixed_rate = 0;
Wey-Yi Guyc10e2c12011-07-13 11:13:46 -07002913#endif
Sedat Dilek7d5f01a2010-12-16 12:46:23 +01002914#ifdef CONFIG_MAC80211_DEBUGFS
Shanyu Zhaoa1da0772010-12-02 11:02:54 -08002915 lq_sta->dbg_fixed_rate = 0;
Sedat Dilek7d5f01a2010-12-16 12:46:23 +01002916#endif
Shanyu Zhaoa1da0772010-12-02 11:02:54 -08002917
Johannes Berg9b6ca442012-03-07 09:52:11 -08002918 rs_initialize_lq(priv, sta, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002919}
2920
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002921static void rs_fill_link_cmd(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08002922 struct iwl_lq_sta *lq_sta, u32 new_rate)
Zhu Yib481de92007-09-25 17:54:57 -07002923{
Tomas Winklere227cea2008-07-18 13:53:05 +08002924 struct iwl_scale_tbl_info tbl_type;
Zhu Yib481de92007-09-25 17:54:57 -07002925 int index = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002926 int rate_idx;
Zhu Yi1b696de2007-09-27 11:27:41 +08002927 int repeat_rate = 0;
Guy Cohenaade00c2008-04-23 17:14:59 -07002928 u8 ant_toggle_cnt = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002929 u8 use_ht_possible = 1;
Guy Cohenaade00c2008-04-23 17:14:59 -07002930 u8 valid_tx_ant = 0;
Johannes Berg7b090682011-01-19 02:53:54 -08002931 struct iwl_station_priv *sta_priv =
2932 container_of(lq_sta, struct iwl_station_priv, lq_sta);
Guy Cohen07bc28e2008-04-23 17:15:03 -07002933 struct iwl_link_quality_cmd *lq_cmd = &lq_sta->lq;
Zhu Yib481de92007-09-25 17:54:57 -07002934
Ben Cahill77626352007-11-29 11:09:44 +08002935 /* Override starting rate (index 0) if needed for debug purposes */
Guy Cohen39e88502008-04-23 17:14:57 -07002936 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Zhu Yi98d7e092007-09-27 11:27:42 +08002937
Guy Cohen39e88502008-04-23 17:14:57 -07002938 /* Interpret new_rate (rate_n_flags) */
2939 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band,
Zhu Yib481de92007-09-25 17:54:57 -07002940 &tbl_type, &rate_idx);
2941
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07002942 if (priv && priv->bt_full_concurrent) {
2943 /* 1x1 only */
2944 tbl_type.ant_type =
Emmanuel Grumbachd6189122011-08-25 23:10:39 -07002945 first_antenna(hw_params(priv).valid_tx_ant);
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07002946 }
2947
Ben Cahill77626352007-11-29 11:09:44 +08002948 /* How many times should we repeat the initial rate? */
Zhu Yib481de92007-09-25 17:54:57 -07002949 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07002950 ant_toggle_cnt = 1;
Zhu Yi1b696de2007-09-27 11:27:41 +08002951 repeat_rate = IWL_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002952 } else {
Daniel Halperin374920c2011-03-16 19:16:36 -07002953 repeat_rate = min(IWL_HT_NUMBER_TRY,
2954 LINK_QUAL_AGG_DISABLE_START_DEF - 1);
Guy Cohenaade00c2008-04-23 17:14:59 -07002955 }
Zhu Yib481de92007-09-25 17:54:57 -07002956
2957 lq_cmd->general_params.mimo_delimiter =
2958 is_mimo(tbl_type.lq_type) ? 1 : 0;
Ben Cahill77626352007-11-29 11:09:44 +08002959
2960 /* Fill 1st table entry (index 0) */
Guy Cohen39e88502008-04-23 17:14:57 -07002961 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002962
Guy Cohen07bc28e2008-04-23 17:15:03 -07002963 if (num_of_ant(tbl_type.ant_type) == 1) {
2964 lq_cmd->general_params.single_stream_ant_msk =
2965 tbl_type.ant_type;
2966 } else if (num_of_ant(tbl_type.ant_type) == 2) {
2967 lq_cmd->general_params.dual_stream_ant_msk =
2968 tbl_type.ant_type;
2969 } /* otherwise we don't modify the existing value */
Zhu Yib481de92007-09-25 17:54:57 -07002970
2971 index++;
Zhu Yi1b696de2007-09-27 11:27:41 +08002972 repeat_rate--;
Wey-Yi Guybee008b2010-08-23 07:57:04 -07002973 if (priv) {
2974 if (priv->bt_full_concurrent)
2975 valid_tx_ant = ANT_A;
2976 else
Emmanuel Grumbachd6189122011-08-25 23:10:39 -07002977 valid_tx_ant = hw_params(priv).valid_tx_ant;
Wey-Yi Guybee008b2010-08-23 07:57:04 -07002978 }
Guy Cohenaade00c2008-04-23 17:14:59 -07002979
Ben Cahill77626352007-11-29 11:09:44 +08002980 /* Fill rest of rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002981 while (index < LINK_QUAL_MAX_RETRY_NUM) {
Ben Cahill77626352007-11-29 11:09:44 +08002982 /* Repeat initial/next rate.
2983 * For legacy IWL_NUMBER_TRY == 1, this loop will not execute.
2984 * For HT IWL_HT_NUMBER_TRY == 3, this executes twice. */
Zhu Yi1b696de2007-09-27 11:27:41 +08002985 while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
Zhu Yib481de92007-09-25 17:54:57 -07002986 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07002987 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2988 ant_toggle_cnt++;
2989 else if (priv &&
2990 rs_toggle_antenna(valid_tx_ant,
2991 &new_rate, &tbl_type))
2992 ant_toggle_cnt = 1;
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07002993 }
Zhu Yi98d7e092007-09-27 11:27:42 +08002994
Ben Cahill77626352007-11-29 11:09:44 +08002995 /* Override next rate if needed for debug purposes */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002996 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Ben Cahill77626352007-11-29 11:09:44 +08002997
2998 /* Fill next table entry */
Zhu Yib481de92007-09-25 17:54:57 -07002999 lq_cmd->rs_table[index].rate_n_flags =
Guy Cohen39e88502008-04-23 17:14:57 -07003000 cpu_to_le32(new_rate);
Zhu Yi1b696de2007-09-27 11:27:41 +08003001 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07003002 index++;
3003 }
3004
Guy Cohen39e88502008-04-23 17:14:57 -07003005 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band, &tbl_type,
Zhu Yib481de92007-09-25 17:54:57 -07003006 &rate_idx);
3007
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07003008 if (priv && priv->bt_full_concurrent) {
3009 /* 1x1 only */
3010 tbl_type.ant_type =
Emmanuel Grumbachd6189122011-08-25 23:10:39 -07003011 first_antenna(hw_params(priv).valid_tx_ant);
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07003012 }
3013
Ben Cahill77626352007-11-29 11:09:44 +08003014 /* Indicate to uCode which entries might be MIMO.
3015 * If initial rate was MIMO, this will finally end up
3016 * as (IWL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
Zhu Yib481de92007-09-25 17:54:57 -07003017 if (is_mimo(tbl_type.lq_type))
3018 lq_cmd->general_params.mimo_delimiter = index;
3019
Ben Cahill77626352007-11-29 11:09:44 +08003020 /* Get next rate */
Guy Cohen39e88502008-04-23 17:14:57 -07003021 new_rate = rs_get_lower_rate(lq_sta, &tbl_type, rate_idx,
3022 use_ht_possible);
Zhu Yib481de92007-09-25 17:54:57 -07003023
Ben Cahill77626352007-11-29 11:09:44 +08003024 /* How many times should we repeat the next rate? */
Zhu Yib481de92007-09-25 17:54:57 -07003025 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07003026 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
3027 ant_toggle_cnt++;
3028 else if (priv &&
3029 rs_toggle_antenna(valid_tx_ant,
3030 &new_rate, &tbl_type))
3031 ant_toggle_cnt = 1;
3032
Zhu Yi1b696de2007-09-27 11:27:41 +08003033 repeat_rate = IWL_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07003034 } else {
Zhu Yi1b696de2007-09-27 11:27:41 +08003035 repeat_rate = IWL_HT_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07003036 }
Zhu Yib481de92007-09-25 17:54:57 -07003037
Ben Cahill77626352007-11-29 11:09:44 +08003038 /* Don't allow HT rates after next pass.
3039 * rs_get_lower_rate() will change type to LQ_A or LQ_G. */
Zhu Yib481de92007-09-25 17:54:57 -07003040 use_ht_possible = 0;
3041
Ben Cahill77626352007-11-29 11:09:44 +08003042 /* Override next rate if needed for debug purposes */
Tomas Winklerc33104f2008-01-14 17:46:21 -08003043 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Ben Cahill77626352007-11-29 11:09:44 +08003044
3045 /* Fill next table entry */
Guy Cohen39e88502008-04-23 17:14:57 -07003046 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
Zhu Yib481de92007-09-25 17:54:57 -07003047
3048 index++;
Zhu Yi1b696de2007-09-27 11:27:41 +08003049 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07003050 }
3051
Johannes Berg7b090682011-01-19 02:53:54 -08003052 lq_cmd->agg_params.agg_frame_cnt_limit =
3053 sta_priv->max_agg_bufsize ?: LINK_QUAL_AGG_FRAME_LIMIT_DEF;
Wey-Yi Guy13c33a02009-06-03 11:44:11 -07003054 lq_cmd->agg_params.agg_dis_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
Wey-Yi Guy74697012010-07-28 09:18:54 -07003055
Wey-Yi Guy13c33a02009-06-03 11:44:11 -07003056 lq_cmd->agg_params.agg_time_limit =
3057 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
Wey-Yi Guy74697012010-07-28 09:18:54 -07003058 /*
3059 * overwrite if needed, pass aggregation time limit
3060 * to uCode in uSec
3061 */
Don Fry38622412011-12-16 07:07:36 -08003062 if (priv && cfg(priv)->bt_params &&
3063 cfg(priv)->bt_params->agg_time_limit &&
Wey-Yi Guy66e863a52010-11-08 14:54:37 -08003064 priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH)
Wey-Yi Guy74697012010-07-28 09:18:54 -07003065 lq_cmd->agg_params.agg_time_limit =
Don Fry38622412011-12-16 07:07:36 -08003066 cpu_to_le16(cfg(priv)->bt_params->agg_time_limit);
Zhu Yib481de92007-09-25 17:54:57 -07003067}
3068
Johannes Berg4b7679a2008-09-18 18:14:18 +02003069static void *rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
Zhu Yib481de92007-09-25 17:54:57 -07003070{
Johannes Berg4b7679a2008-09-18 18:14:18 +02003071 return hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003072}
3073/* rate scale requires free function to be implemented */
3074static void rs_free(void *priv_rate)
3075{
3076 return;
3077}
3078
Johannes Berg4b7679a2008-09-18 18:14:18 +02003079static void rs_free_sta(void *priv_r, struct ieee80211_sta *sta,
3080 void *priv_sta)
Zhu Yib481de92007-09-25 17:54:57 -07003081{
Emmanuel Grumbachd0f76d62012-02-09 16:08:15 +02003082 struct iwl_op_mode *op_mode __maybe_unused = priv_r;
3083 struct iwl_priv *priv __maybe_unused = IWL_OP_MODE_GET_DVM(op_mode);
Zhu Yib481de92007-09-25 17:54:57 -07003084
Tomas Winklere1623442009-01-27 14:27:56 -08003085 IWL_DEBUG_RATE(priv, "enter\n");
Tomas Winklere1623442009-01-27 14:27:56 -08003086 IWL_DEBUG_RATE(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003087}
3088
Zhu Yi93dc6462007-09-27 11:27:37 +08003089#ifdef CONFIG_MAC80211_DEBUGFS
Tomas Winklere227cea2008-07-18 13:53:05 +08003090static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
3091 u32 *rate_n_flags, int index)
Zhu Yi98d7e092007-09-27 11:27:42 +08003092{
Ester Kummerbf403db2008-05-05 10:22:40 +08003093 struct iwl_priv *priv;
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07003094 u8 valid_tx_ant;
3095 u8 ant_sel_tx;
Ester Kummerbf403db2008-05-05 10:22:40 +08003096
3097 priv = lq_sta->drv;
Emmanuel Grumbachd6189122011-08-25 23:10:39 -07003098 valid_tx_ant = hw_params(priv).valid_tx_ant;
Guy Cohen39e88502008-04-23 17:14:57 -07003099 if (lq_sta->dbg_fixed_rate) {
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07003100 ant_sel_tx =
3101 ((lq_sta->dbg_fixed_rate & RATE_MCS_ANT_ABC_MSK)
3102 >> RATE_MCS_ANT_POS);
3103 if ((valid_tx_ant & ant_sel_tx) == ant_sel_tx) {
Guy Cohen39e88502008-04-23 17:14:57 -07003104 *rate_n_flags = lq_sta->dbg_fixed_rate;
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07003105 IWL_DEBUG_RATE(priv, "Fixed rate ON\n");
Guy Cohen39e88502008-04-23 17:14:57 -07003106 } else {
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07003107 lq_sta->dbg_fixed_rate = 0;
3108 IWL_ERR(priv,
3109 "Invalid antenna selection 0x%X, Valid is 0x%X\n",
3110 ant_sel_tx, valid_tx_ant);
3111 IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
Guy Cohen39e88502008-04-23 17:14:57 -07003112 }
Guy Cohen39e88502008-04-23 17:14:57 -07003113 } else {
Tomas Winklere1623442009-01-27 14:27:56 -08003114 IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
Zhu Yi98d7e092007-09-27 11:27:42 +08003115 }
Zhu Yi98d7e092007-09-27 11:27:42 +08003116}
3117
3118static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
3119 const char __user *user_buf, size_t count, loff_t *ppos)
3120{
Tomas Winklere227cea2008-07-18 13:53:05 +08003121 struct iwl_lq_sta *lq_sta = file->private_data;
Ester Kummerbf403db2008-05-05 10:22:40 +08003122 struct iwl_priv *priv;
Zhu Yi98d7e092007-09-27 11:27:42 +08003123 char buf[64];
Stephen Boyd805d7d22011-05-12 16:50:05 -07003124 size_t buf_size;
Zhu Yi98d7e092007-09-27 11:27:42 +08003125 u32 parsed_rate;
Wey-Yi Guy64898542011-05-03 18:05:13 -07003126
Zhu Yi98d7e092007-09-27 11:27:42 +08003127
Ester Kummerbf403db2008-05-05 10:22:40 +08003128 priv = lq_sta->drv;
Zhu Yi98d7e092007-09-27 11:27:42 +08003129 memset(buf, 0, sizeof(buf));
3130 buf_size = min(count, sizeof(buf) - 1);
3131 if (copy_from_user(buf, user_buf, buf_size))
3132 return -EFAULT;
3133
3134 if (sscanf(buf, "%x", &parsed_rate) == 1)
Wey-Yi Guy4e308112011-07-08 08:46:28 -07003135 lq_sta->dbg_fixed_rate = parsed_rate;
Zhu Yi98d7e092007-09-27 11:27:42 +08003136 else
Wey-Yi Guy4e308112011-07-08 08:46:28 -07003137 lq_sta->dbg_fixed_rate = 0;
Zhu Yi98d7e092007-09-27 11:27:42 +08003138
Wey-Yi Guy64898542011-05-03 18:05:13 -07003139 rs_program_fix_rate(priv, lq_sta);
Zhu Yi98d7e092007-09-27 11:27:42 +08003140
3141 return count;
3142}
Zhu Yi0209dc12007-09-27 11:27:43 +08003143
Zhu Yi5ae212c2007-09-27 11:27:38 +08003144static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
3145 char __user *user_buf, size_t count, loff_t *ppos)
3146{
Frank Seidela412c802009-03-01 20:25:38 +01003147 char *buff;
Zhu Yi5ae212c2007-09-27 11:27:38 +08003148 int desc = 0;
3149 int i = 0;
Wey-Yi Guy12b96812009-04-08 11:39:27 -07003150 int index = 0;
Frank Seidela412c802009-03-01 20:25:38 +01003151 ssize_t ret;
Zhu Yi5ae212c2007-09-27 11:27:38 +08003152
Tomas Winklere227cea2008-07-18 13:53:05 +08003153 struct iwl_lq_sta *lq_sta = file->private_data;
Wey-Yi Guyd8ae4f52009-03-10 14:35:07 -07003154 struct iwl_priv *priv;
Wey-Yi Guyadb7b5e2009-03-10 14:35:08 -07003155 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
Zhu Yi5ae212c2007-09-27 11:27:38 +08003156
Wey-Yi Guyd8ae4f52009-03-10 14:35:07 -07003157 priv = lq_sta->drv;
Frank Seidela412c802009-03-01 20:25:38 +01003158 buff = kmalloc(1024, GFP_KERNEL);
3159 if (!buff)
3160 return -ENOMEM;
3161
Tomas Winklerc33104f2008-01-14 17:46:21 -08003162 desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
Zhu Yi98d7e092007-09-27 11:27:42 +08003163 desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08003164 lq_sta->total_failed, lq_sta->total_success,
Guy Cohen39e88502008-04-23 17:14:57 -07003165 lq_sta->active_legacy_rate);
Zhu Yi98d7e092007-09-27 11:27:42 +08003166 desc += sprintf(buff+desc, "fixed rate 0x%X\n",
Wey-Yi Guy4e308112011-07-08 08:46:28 -07003167 lq_sta->dbg_fixed_rate);
Wey-Yi Guyd8ae4f52009-03-10 14:35:07 -07003168 desc += sprintf(buff+desc, "valid_tx_ant %s%s%s\n",
Emmanuel Grumbachd6189122011-08-25 23:10:39 -07003169 (hw_params(priv).valid_tx_ant & ANT_A) ? "ANT_A," : "",
3170 (hw_params(priv).valid_tx_ant & ANT_B) ? "ANT_B," : "",
3171 (hw_params(priv).valid_tx_ant & ANT_C) ? "ANT_C" : "");
Wey-Yi Guyadb7b5e2009-03-10 14:35:08 -07003172 desc += sprintf(buff+desc, "lq type %s\n",
3173 (is_legacy(tbl->lq_type)) ? "legacy" : "HT");
3174 if (is_Ht(tbl->lq_type)) {
3175 desc += sprintf(buff+desc, " %s",
3176 (is_siso(tbl->lq_type)) ? "SISO" :
3177 ((is_mimo2(tbl->lq_type)) ? "MIMO2" : "MIMO3"));
3178 desc += sprintf(buff+desc, " %s",
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07003179 (tbl->is_ht40) ? "40MHz" : "20MHz");
Daniel C Halperine3949d62009-09-17 10:43:50 -07003180 desc += sprintf(buff+desc, " %s %s %s\n", (tbl->is_SGI) ? "SGI" : "",
3181 (lq_sta->is_green) ? "GF enabled" : "",
3182 (lq_sta->is_agg) ? "AGG on" : "");
Wey-Yi Guyadb7b5e2009-03-10 14:35:08 -07003183 }
Wey-Yi Guy12b96812009-04-08 11:39:27 -07003184 desc += sprintf(buff+desc, "last tx rate=0x%X\n",
3185 lq_sta->last_rate_n_flags);
Zhu Yi5ae212c2007-09-27 11:27:38 +08003186 desc += sprintf(buff+desc, "general:"
3187 "flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08003188 lq_sta->lq.general_params.flags,
3189 lq_sta->lq.general_params.mimo_delimiter,
3190 lq_sta->lq.general_params.single_stream_ant_msk,
3191 lq_sta->lq.general_params.dual_stream_ant_msk);
Zhu Yi5ae212c2007-09-27 11:27:38 +08003192
3193 desc += sprintf(buff+desc, "agg:"
3194 "time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08003195 le16_to_cpu(lq_sta->lq.agg_params.agg_time_limit),
3196 lq_sta->lq.agg_params.agg_dis_start_th,
3197 lq_sta->lq.agg_params.agg_frame_cnt_limit);
Zhu Yi5ae212c2007-09-27 11:27:38 +08003198
3199 desc += sprintf(buff+desc,
3200 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08003201 lq_sta->lq.general_params.start_rate_index[0],
3202 lq_sta->lq.general_params.start_rate_index[1],
3203 lq_sta->lq.general_params.start_rate_index[2],
3204 lq_sta->lq.general_params.start_rate_index[3]);
Zhu Yi5ae212c2007-09-27 11:27:38 +08003205
Wey-Yi Guy12b96812009-04-08 11:39:27 -07003206 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
3207 index = iwl_hwrate_to_plcp_idx(
3208 le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags));
3209 if (is_legacy(tbl->lq_type)) {
3210 desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps\n",
3211 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags),
3212 iwl_rate_mcs[index].mbps);
3213 } else {
3214 desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps (%s)\n",
3215 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags),
3216 iwl_rate_mcs[index].mbps, iwl_rate_mcs[index].mcs);
3217 }
3218 }
Zhu Yi5ae212c2007-09-27 11:27:38 +08003219
Frank Seidela412c802009-03-01 20:25:38 +01003220 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
3221 kfree(buff);
3222 return ret;
Zhu Yi5ae212c2007-09-27 11:27:38 +08003223}
3224
3225static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
Zhu Yi98d7e092007-09-27 11:27:42 +08003226 .write = rs_sta_dbgfs_scale_table_write,
Zhu Yi5ae212c2007-09-27 11:27:38 +08003227 .read = rs_sta_dbgfs_scale_table_read,
Stephen Boyd234e3402012-04-05 14:25:11 -07003228 .open = simple_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003229 .llseek = default_llseek,
Zhu Yi5ae212c2007-09-27 11:27:38 +08003230};
Zhu Yi0209dc12007-09-27 11:27:43 +08003231static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
3232 char __user *user_buf, size_t count, loff_t *ppos)
3233{
Frank Seidela412c802009-03-01 20:25:38 +01003234 char *buff;
Zhu Yi0209dc12007-09-27 11:27:43 +08003235 int desc = 0;
3236 int i, j;
Frank Seidela412c802009-03-01 20:25:38 +01003237 ssize_t ret;
Zhu Yi0209dc12007-09-27 11:27:43 +08003238
Tomas Winklere227cea2008-07-18 13:53:05 +08003239 struct iwl_lq_sta *lq_sta = file->private_data;
Frank Seidela412c802009-03-01 20:25:38 +01003240
3241 buff = kmalloc(1024, GFP_KERNEL);
3242 if (!buff)
3243 return -ENOMEM;
3244
Zhu Yi0209dc12007-09-27 11:27:43 +08003245 for (i = 0; i < LQ_SIZE; i++) {
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07003246 desc += sprintf(buff+desc,
3247 "%s type=%d SGI=%d HT40=%d DUP=%d GF=%d\n"
Zhu Yi0209dc12007-09-27 11:27:43 +08003248 "rate=0x%X\n",
Abhijeet Kolekarc3056062008-11-12 13:14:08 -08003249 lq_sta->active_tbl == i ? "*" : "x",
Tomas Winklerc33104f2008-01-14 17:46:21 -08003250 lq_sta->lq_info[i].lq_type,
3251 lq_sta->lq_info[i].is_SGI,
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07003252 lq_sta->lq_info[i].is_ht40,
Tomas Winklerc33104f2008-01-14 17:46:21 -08003253 lq_sta->lq_info[i].is_dup,
Wey-Yi Guy2681b202009-05-21 13:44:22 -07003254 lq_sta->is_green,
Guy Cohen39e88502008-04-23 17:14:57 -07003255 lq_sta->lq_info[i].current_rate);
Zhu Yi0209dc12007-09-27 11:27:43 +08003256 for (j = 0; j < IWL_RATE_COUNT; j++) {
3257 desc += sprintf(buff+desc,
Tomas Winklerc33104f2008-01-14 17:46:21 -08003258 "counter=%d success=%d %%=%d\n",
3259 lq_sta->lq_info[i].win[j].counter,
3260 lq_sta->lq_info[i].win[j].success_counter,
3261 lq_sta->lq_info[i].win[j].success_ratio);
Zhu Yi0209dc12007-09-27 11:27:43 +08003262 }
3263 }
Frank Seidela412c802009-03-01 20:25:38 +01003264 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
3265 kfree(buff);
3266 return ret;
Zhu Yi0209dc12007-09-27 11:27:43 +08003267}
3268
3269static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
3270 .read = rs_sta_dbgfs_stats_table_read,
Stephen Boyd234e3402012-04-05 14:25:11 -07003271 .open = simple_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003272 .llseek = default_llseek,
Zhu Yi0209dc12007-09-27 11:27:43 +08003273};
Zhu Yi5ae212c2007-09-27 11:27:38 +08003274
Wey-Yi Guy38167452009-05-08 13:44:43 -07003275static ssize_t rs_sta_dbgfs_rate_scale_data_read(struct file *file,
3276 char __user *user_buf, size_t count, loff_t *ppos)
3277{
Johannes Berg70817b52011-05-11 03:29:25 -07003278 struct iwl_lq_sta *lq_sta = file->private_data;
3279 struct iwl_scale_tbl_info *tbl = &lq_sta->lq_info[lq_sta->active_tbl];
Wey-Yi Guy38167452009-05-08 13:44:43 -07003280 char buff[120];
3281 int desc = 0;
Wey-Yi Guy38167452009-05-08 13:44:43 -07003282
Wey-Yi Guy38167452009-05-08 13:44:43 -07003283 if (is_Ht(tbl->lq_type))
3284 desc += sprintf(buff+desc,
3285 "Bit Rate= %d Mb/s\n",
3286 tbl->expected_tpt[lq_sta->last_txrate_idx]);
3287 else
3288 desc += sprintf(buff+desc,
3289 "Bit Rate= %d Mb/s\n",
3290 iwl_rates[lq_sta->last_txrate_idx].ieee >> 1);
Wey-Yi Guy38167452009-05-08 13:44:43 -07003291
Johannes Berg08960de2011-04-05 09:41:53 -07003292 return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
Wey-Yi Guy38167452009-05-08 13:44:43 -07003293}
3294
3295static const struct file_operations rs_sta_dbgfs_rate_scale_data_ops = {
3296 .read = rs_sta_dbgfs_rate_scale_data_read,
Stephen Boyd234e3402012-04-05 14:25:11 -07003297 .open = simple_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003298 .llseek = default_llseek,
Wey-Yi Guy38167452009-05-08 13:44:43 -07003299};
3300
Zhu Yi93dc6462007-09-27 11:27:37 +08003301static void rs_add_debugfs(void *priv, void *priv_sta,
3302 struct dentry *dir)
3303{
Tomas Winklere227cea2008-07-18 13:53:05 +08003304 struct iwl_lq_sta *lq_sta = priv_sta;
Tomas Winklerc33104f2008-01-14 17:46:21 -08003305 lq_sta->rs_sta_dbgfs_scale_table_file =
Wey-Yi Guy43e85112009-11-20 12:04:58 -08003306 debugfs_create_file("rate_scale_table", S_IRUSR | S_IWUSR, dir,
Tomas Winklerc33104f2008-01-14 17:46:21 -08003307 lq_sta, &rs_sta_dbgfs_scale_table_ops);
3308 lq_sta->rs_sta_dbgfs_stats_table_file =
Wey-Yi Guy43e85112009-11-20 12:04:58 -08003309 debugfs_create_file("rate_stats_table", S_IRUSR, dir,
Tomas Winklerc33104f2008-01-14 17:46:21 -08003310 lq_sta, &rs_sta_dbgfs_stats_table_ops);
Wey-Yi Guy38167452009-05-08 13:44:43 -07003311 lq_sta->rs_sta_dbgfs_rate_scale_data_file =
Wey-Yi Guy43e85112009-11-20 12:04:58 -08003312 debugfs_create_file("rate_scale_data", S_IRUSR, dir,
Wey-Yi Guy38167452009-05-08 13:44:43 -07003313 lq_sta, &rs_sta_dbgfs_rate_scale_data_ops);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02003314 lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file =
Wey-Yi Guy43e85112009-11-20 12:04:58 -08003315 debugfs_create_u8("tx_agg_tid_enable", S_IRUSR | S_IWUSR, dir,
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02003316 &lq_sta->tx_agg_tid_en);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02003317
Zhu Yi93dc6462007-09-27 11:27:37 +08003318}
3319
3320static void rs_remove_debugfs(void *priv, void *priv_sta)
3321{
Tomas Winklere227cea2008-07-18 13:53:05 +08003322 struct iwl_lq_sta *lq_sta = priv_sta;
Tomas Winklerc33104f2008-01-14 17:46:21 -08003323 debugfs_remove(lq_sta->rs_sta_dbgfs_scale_table_file);
3324 debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
Wey-Yi Guy38167452009-05-08 13:44:43 -07003325 debugfs_remove(lq_sta->rs_sta_dbgfs_rate_scale_data_file);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02003326 debugfs_remove(lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file);
Zhu Yi93dc6462007-09-27 11:27:37 +08003327}
3328#endif
3329
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003330/*
3331 * Initialization of rate scaling information is done by driver after
3332 * the station is added. Since mac80211 calls this function before a
3333 * station is added we ignore it.
3334 */
3335static void rs_rate_init_stub(void *priv_r, struct ieee80211_supported_band *sband,
3336 struct ieee80211_sta *sta, void *priv_sta)
3337{
3338}
Zhu Yib481de92007-09-25 17:54:57 -07003339static struct rate_control_ops rs_ops = {
3340 .module = NULL,
3341 .name = RS_NAME,
3342 .tx_status = rs_tx_status,
3343 .get_rate = rs_get_rate,
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003344 .rate_init = rs_rate_init_stub,
Zhu Yib481de92007-09-25 17:54:57 -07003345 .alloc = rs_alloc,
3346 .free = rs_free,
3347 .alloc_sta = rs_alloc_sta,
3348 .free_sta = rs_free_sta,
Zhu Yi93dc6462007-09-27 11:27:37 +08003349#ifdef CONFIG_MAC80211_DEBUGFS
3350 .add_sta_debugfs = rs_add_debugfs,
3351 .remove_sta_debugfs = rs_remove_debugfs,
3352#endif
Zhu Yib481de92007-09-25 17:54:57 -07003353};
3354
Tomas Winklere227cea2008-07-18 13:53:05 +08003355int iwlagn_rate_control_register(void)
Zhu Yib481de92007-09-25 17:54:57 -07003356{
Reinette Chatre897e1cf2008-03-28 16:21:09 -07003357 return ieee80211_rate_control_register(&rs_ops);
Zhu Yib481de92007-09-25 17:54:57 -07003358}
3359
Tomas Winklere227cea2008-07-18 13:53:05 +08003360void iwlagn_rate_control_unregister(void)
Zhu Yib481de92007-09-25 17:54:57 -07003361{
3362 ieee80211_rate_control_unregister(&rs_ops);
3363}
3364