blob: 4d5738dfc7eb61d4be004f846caf642a7aed0572 [file] [log] [blame]
Zhu Yib481de92007-09-25 17:54:57 -07001/******************************************************************************
2 *
Reinette Chatre1f447802010-01-15 13:43:41 -08003 * Copyright(c) 2005 - 2010 Intel Corporation. All rights reserved.
Zhu Yib481de92007-09-25 17:54:57 -07004 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
17 *
18 * The full GNU General Public License is included in this distribution in the
19 * file called LICENSE.
20 *
21 * Contact Information:
Winkler, Tomas759ef892008-12-09 11:28:58 -080022 * Intel Linux Wireless <ilw@linux.intel.com>
Zhu Yib481de92007-09-25 17:54:57 -070023 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24 *
25 *****************************************************************************/
26#include <linux/kernel.h>
27#include <linux/init.h>
28#include <linux/skbuff.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Zhu Yib481de92007-09-25 17:54:57 -070030#include <linux/wireless.h>
31#include <net/mac80211.h>
Zhu Yib481de92007-09-25 17:54:57 -070032
33#include <linux/netdevice.h>
34#include <linux/etherdevice.h>
35#include <linux/delay.h>
36
37#include <linux/workqueue.h>
38
Tomas Winkler3e0d4cb2008-04-24 11:55:38 -070039#include "iwl-dev.h"
Emmanuel Grumbachbe1f3ab62008-06-12 09:47:18 +080040#include "iwl-sta.h"
Tomas Winkler857485c2008-03-21 13:53:44 -070041#include "iwl-core.h"
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
Tomas Winklerc79dd5b2008-03-12 16:58:50 -070079static void rs_rate_scale_perform(struct iwl_priv *priv,
Gábor Stefanik514d65c2009-04-23 19:36:08 +020080 struct sk_buff *skb,
Johannes Berg4b7679a2008-09-18 18:14:18 +020081 struct ieee80211_sta *sta,
82 struct iwl_lq_sta *lq_sta);
Wey-Yi Guy46f93812009-07-24 11:13:03 -070083static void rs_fill_link_cmd(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +080084 struct iwl_lq_sta *lq_sta, u32 rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -070085
86
Zhu Yi98d7e092007-09-27 11:27:42 +080087#ifdef CONFIG_MAC80211_DEBUGFS
Tomas Winklere227cea2008-07-18 13:53:05 +080088static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
89 u32 *rate_n_flags, int index);
Zhu Yi98d7e092007-09-27 11:27:42 +080090#else
Tomas Winklere227cea2008-07-18 13:53:05 +080091static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
92 u32 *rate_n_flags, int index)
Zhu Yi98d7e092007-09-27 11:27:42 +080093{}
94#endif
Ben Cahill77626352007-11-29 11:09:44 +080095
Daniel C Halperine3949d62009-09-17 10:43:50 -070096/**
97 * The following tables contain the expected throughput metrics for all rates
98 *
99 * 1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
100 *
101 * where invalid entries are zeros.
102 *
103 * CCK rates are only valid in legacy table and will only be used in G
104 * (2.4 GHz) band.
Ben Cahill77626352007-11-29 11:09:44 +0800105 */
Wey-Yi Guy584a0f02009-04-01 14:33:24 -0700106
Daniel C Halperine3949d62009-09-17 10:43:50 -0700107static s32 expected_tpt_legacy[IWL_RATE_COUNT] = {
108 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 0
Zhu Yib481de92007-09-25 17:54:57 -0700109};
110
Daniel C Halperine3949d62009-09-17 10:43:50 -0700111static s32 expected_tpt_siso20MHz[4][IWL_RATE_COUNT] = {
112 {0, 0, 0, 0, 42, 0, 76, 102, 124, 158, 183, 193, 202}, /* Norm */
113 {0, 0, 0, 0, 46, 0, 82, 110, 132, 167, 192, 202, 210}, /* SGI */
114 {0, 0, 0, 0, 48, 0, 93, 135, 176, 251, 319, 351, 381}, /* AGG */
115 {0, 0, 0, 0, 53, 0, 102, 149, 193, 275, 348, 381, 413}, /* AGG+SGI */
Zhu Yib481de92007-09-25 17:54:57 -0700116};
117
Daniel C Halperine3949d62009-09-17 10:43:50 -0700118static s32 expected_tpt_siso40MHz[4][IWL_RATE_COUNT] = {
119 {0, 0, 0, 0, 77, 0, 127, 160, 184, 220, 242, 250, 257}, /* Norm */
120 {0, 0, 0, 0, 83, 0, 135, 169, 193, 229, 250, 257, 264}, /* SGI */
121 {0, 0, 0, 0, 96, 0, 182, 259, 328, 451, 553, 598, 640}, /* AGG */
122 {0, 0, 0, 0, 106, 0, 199, 282, 357, 487, 593, 640, 683}, /* AGG+SGI */
Zhu Yib481de92007-09-25 17:54:57 -0700123};
124
Daniel C Halperine3949d62009-09-17 10:43:50 -0700125static s32 expected_tpt_mimo2_20MHz[4][IWL_RATE_COUNT] = {
126 {0, 0, 0, 0, 74, 0, 123, 155, 179, 213, 235, 243, 250}, /* Norm */
127 {0, 0, 0, 0, 81, 0, 131, 164, 187, 221, 242, 250, 256}, /* SGI */
128 {0, 0, 0, 0, 92, 0, 175, 250, 317, 436, 534, 578, 619}, /* AGG */
129 {0, 0, 0, 0, 102, 0, 192, 273, 344, 470, 573, 619, 660}, /* AGG+SGI*/
Zhu Yib481de92007-09-25 17:54:57 -0700130};
131
Daniel C Halperine3949d62009-09-17 10:43:50 -0700132static s32 expected_tpt_mimo2_40MHz[4][IWL_RATE_COUNT] = {
133 {0, 0, 0, 0, 123, 0, 182, 214, 235, 264, 279, 285, 289}, /* Norm */
134 {0, 0, 0, 0, 131, 0, 191, 222, 242, 270, 284, 289, 293}, /* SGI */
135 {0, 0, 0, 0, 180, 0, 327, 446, 545, 708, 828, 878, 922}, /* AGG */
136 {0, 0, 0, 0, 197, 0, 355, 481, 584, 752, 872, 922, 966}, /* AGG+SGI */
Zhu Yib481de92007-09-25 17:54:57 -0700137};
138
Daniel C Halperine3949d62009-09-17 10:43:50 -0700139static s32 expected_tpt_mimo3_20MHz[4][IWL_RATE_COUNT] = {
140 {0, 0, 0, 0, 99, 0, 153, 186, 208, 239, 256, 263, 268}, /* Norm */
141 {0, 0, 0, 0, 106, 0, 162, 194, 215, 246, 262, 268, 273}, /* SGI */
142 {0, 0, 0, 0, 134, 0, 249, 346, 431, 574, 685, 732, 775}, /* AGG */
143 {0, 0, 0, 0, 148, 0, 272, 376, 465, 614, 727, 775, 818}, /* AGG+SGI */
Zhu Yib481de92007-09-25 17:54:57 -0700144};
145
Daniel C Halperine3949d62009-09-17 10:43:50 -0700146static s32 expected_tpt_mimo3_40MHz[4][IWL_RATE_COUNT] = {
147 {0, 0, 0, 0, 152, 0, 211, 239, 255, 279, 290, 294, 297}, /* Norm */
148 {0, 0, 0, 0, 160, 0, 219, 245, 261, 284, 294, 297, 300}, /* SGI */
149 {0, 0, 0, 0, 254, 0, 443, 584, 695, 868, 984, 1030, 1070}, /* AGG */
150 {0, 0, 0, 0, 277, 0, 478, 624, 737, 911, 1026, 1070, 1109}, /* AGG+SGI */
Wey-Yi Guy584a0f02009-04-01 14:33:24 -0700151};
152
Wey-Yi Guy12b96812009-04-08 11:39:27 -0700153/* mbps, mcs */
Tobias Klauser79496732009-12-23 14:18:11 +0100154static const struct iwl_rate_mcs_info iwl_rate_mcs[IWL_RATE_COUNT] = {
Daniel C Halperine3949d62009-09-17 10:43:50 -0700155 { "1", "BPSK DSSS"},
156 { "2", "QPSK DSSS"},
157 {"5.5", "BPSK CCK"},
158 { "11", "QPSK CCK"},
159 { "6", "BPSK 1/2"},
160 { "9", "BPSK 1/2"},
161 { "12", "QPSK 1/2"},
162 { "18", "QPSK 3/4"},
163 { "24", "16QAM 1/2"},
164 { "36", "16QAM 3/4"},
165 { "48", "64QAM 2/3"},
166 { "54", "64QAM 3/4"},
167 { "60", "64QAM 5/6"},
Wey-Yi Guy12b96812009-04-08 11:39:27 -0700168};
169
Wey-Yi Guya9c146b2009-05-22 11:01:48 -0700170#define MCS_INDEX_PER_STREAM (8)
171
Guy Cohen39e88502008-04-23 17:14:57 -0700172static inline u8 rs_extract_rate(u32 rate_n_flags)
Zhu Yib481de92007-09-25 17:54:57 -0700173{
174 return (u8)(rate_n_flags & 0xFF);
175}
176
Tomas Winklere227cea2008-07-18 13:53:05 +0800177static void rs_rate_scale_clear_window(struct iwl_rate_scale_data *window)
Zhu Yib481de92007-09-25 17:54:57 -0700178{
179 window->data = 0;
180 window->success_counter = 0;
181 window->success_ratio = IWL_INVALID_VALUE;
182 window->counter = 0;
183 window->average_tpt = IWL_INVALID_VALUE;
184 window->stamp = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700185}
186
Guy Cohenaade00c2008-04-23 17:14:59 -0700187static inline u8 rs_is_valid_ant(u8 valid_antenna, u8 ant_type)
188{
Tomas Winkler3ac7f142008-07-21 02:40:14 +0300189 return (ant_type & valid_antenna) == ant_type;
Guy Cohenaade00c2008-04-23 17:14:59 -0700190}
191
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200192/*
193 * removes the old data from the statistics. All data that is older than
194 * TID_MAX_TIME_DIFF, will be deleted.
195 */
Tomas Winklere227cea2008-07-18 13:53:05 +0800196static void rs_tl_rm_old_stats(struct iwl_traffic_load *tl, u32 curr_time)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200197{
198 /* The oldest age we want to keep */
199 u32 oldest_time = curr_time - TID_MAX_TIME_DIFF;
200
201 while (tl->queue_count &&
202 (tl->time_stamp < oldest_time)) {
203 tl->total -= tl->packet_count[tl->head];
204 tl->packet_count[tl->head] = 0;
205 tl->time_stamp += TID_QUEUE_CELL_SPACING;
206 tl->queue_count--;
207 tl->head++;
208 if (tl->head >= TID_QUEUE_MAX_SIZE)
209 tl->head = 0;
210 }
211}
212
213/*
214 * increment traffic load value for tid and also remove
215 * any old values if passed the certain time period
216 */
Tomas Winklere227cea2008-07-18 13:53:05 +0800217static u8 rs_tl_add_packet(struct iwl_lq_sta *lq_data,
Ron Rindjunskyfaa29712008-06-12 09:46:58 +0800218 struct ieee80211_hdr *hdr)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200219{
220 u32 curr_time = jiffies_to_msecs(jiffies);
221 u32 time_diff;
222 s32 index;
Tomas Winklere227cea2008-07-18 13:53:05 +0800223 struct iwl_traffic_load *tl = NULL;
Tomas Winkler54dbb522008-05-15 13:54:06 +0800224 u8 tid;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200225
Tomas Winkler417f1142008-11-12 13:14:06 -0800226 if (ieee80211_is_data_qos(hdr->frame_control)) {
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700227 u8 *qc = ieee80211_get_qos_ctl(hdr);
Tomas Winkler54dbb522008-05-15 13:54:06 +0800228 tid = qc[0] & 0xf;
229 } else
Ron Rindjunskyfaa29712008-06-12 09:46:58 +0800230 return MAX_TID_COUNT;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200231
Reinette Chatree6a6cf42009-08-13 13:30:50 -0700232 if (unlikely(tid >= TID_MAX_LOAD_COUNT))
233 return MAX_TID_COUNT;
234
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200235 tl = &lq_data->load[tid];
236
237 curr_time -= curr_time % TID_ROUND_VALUE;
238
239 /* Happens only for the first packet. Initialize the data */
240 if (!(tl->queue_count)) {
241 tl->total = 1;
242 tl->time_stamp = curr_time;
243 tl->queue_count = 1;
244 tl->head = 0;
245 tl->packet_count[0] = 1;
Ron Rindjunskyfaa29712008-06-12 09:46:58 +0800246 return MAX_TID_COUNT;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200247 }
248
249 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
250 index = time_diff / TID_QUEUE_CELL_SPACING;
251
252 /* The history is too long: remove data that is older than */
253 /* TID_MAX_TIME_DIFF */
254 if (index >= TID_QUEUE_MAX_SIZE)
255 rs_tl_rm_old_stats(tl, curr_time);
256
257 index = (tl->head + index) % TID_QUEUE_MAX_SIZE;
258 tl->packet_count[index] = tl->packet_count[index] + 1;
259 tl->total = tl->total + 1;
260
261 if ((index + 1) > tl->queue_count)
262 tl->queue_count = index + 1;
Ron Rindjunskyfaa29712008-06-12 09:46:58 +0800263
264 return tid;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200265}
266
267/*
268 get the traffic load value for tid
269*/
Tomas Winklere227cea2008-07-18 13:53:05 +0800270static u32 rs_tl_get_load(struct iwl_lq_sta *lq_data, u8 tid)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200271{
272 u32 curr_time = jiffies_to_msecs(jiffies);
273 u32 time_diff;
274 s32 index;
Tomas Winklere227cea2008-07-18 13:53:05 +0800275 struct iwl_traffic_load *tl = NULL;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200276
277 if (tid >= TID_MAX_LOAD_COUNT)
278 return 0;
279
280 tl = &(lq_data->load[tid]);
281
282 curr_time -= curr_time % TID_ROUND_VALUE;
283
284 if (!(tl->queue_count))
285 return 0;
286
287 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
288 index = time_diff / TID_QUEUE_CELL_SPACING;
289
290 /* The history is too long: remove data that is older than */
291 /* TID_MAX_TIME_DIFF */
292 if (index >= TID_QUEUE_MAX_SIZE)
293 rs_tl_rm_old_stats(tl, curr_time);
294
295 return tl->total;
296}
297
Wey-Yi Guy82ca9342010-04-12 14:02:36 -0700298static int rs_tl_turn_on_agg_for_tid(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +0800299 struct iwl_lq_sta *lq_data, u8 tid,
Johannes Berg4b7679a2008-09-18 18:14:18 +0200300 struct ieee80211_sta *sta)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200301{
Wey-Yi Guy82ca9342010-04-12 14:02:36 -0700302 int ret = -EAGAIN;
Andy Lutomirski24110542010-07-25 13:14:29 -0400303 u32 load = rs_tl_get_load(lq_data, tid);
Wey-Yi Guy45d42702010-02-03 12:24:44 -0800304
Andy Lutomirski24110542010-07-25 13:14:29 -0400305 if (load > IWL_AGG_LOAD_THRESHOLD) {
Tomas Winklere1623442009-01-27 14:27:56 -0800306 IWL_DEBUG_HT(priv, "Starting Tx agg: STA: %pM tid: %d\n",
Johannes Berge1749612008-10-27 15:59:26 -0700307 sta->addr, tid);
Wey-Yi Guy45d42702010-02-03 12:24:44 -0800308 ret = ieee80211_start_tx_ba_session(sta, tid);
309 if (ret == -EAGAIN) {
310 /*
311 * driver and mac80211 is out of sync
312 * this might be cause by reloading firmware
313 * stop the tx ba session here
314 */
Andy Lutomirski24110542010-07-25 13:14:29 -0400315 IWL_ERR(priv, "Fail start Tx agg on tid: %d\n",
Wey-Yi Guy45d42702010-02-03 12:24:44 -0800316 tid);
Johannes Berg6a8579d2010-05-27 14:41:07 +0200317 ieee80211_stop_tx_ba_session(sta, tid);
Wey-Yi Guy45d42702010-02-03 12:24:44 -0800318 }
Andy Lutomirski24110542010-07-25 13:14:29 -0400319 } else {
320 IWL_ERR(priv, "Aggregation not enabled for tid %d "
321 "because load = %u\n", tid, load);
322 }
Wey-Yi Guy82ca9342010-04-12 14:02:36 -0700323 return ret;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200324}
325
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700326static void rs_tl_turn_on_agg(struct iwl_priv *priv, u8 tid,
Tomas Winklere227cea2008-07-18 13:53:05 +0800327 struct iwl_lq_sta *lq_data,
Johannes Berg4b7679a2008-09-18 18:14:18 +0200328 struct ieee80211_sta *sta)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200329{
Wey-Yi Guycfecc6b2010-06-18 11:33:15 -0700330 if (tid < TID_MAX_LOAD_COUNT)
331 rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
332 else
333 IWL_ERR(priv, "tid exceeds max load count: %d/%d\n",
334 tid, TID_MAX_LOAD_COUNT);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200335}
336
Guy Cohen39e88502008-04-23 17:14:57 -0700337static inline int get_num_of_ant_from_rate(u32 rate_n_flags)
Guy Cohenfde0db32008-04-21 15:42:01 -0700338{
Tomas Winkler3ac7f142008-07-21 02:40:14 +0300339 return !!(rate_n_flags & RATE_MCS_ANT_A_MSK) +
340 !!(rate_n_flags & RATE_MCS_ANT_B_MSK) +
341 !!(rate_n_flags & RATE_MCS_ANT_C_MSK);
Guy Cohenfde0db32008-04-21 15:42:01 -0700342}
343
Shanyu Zhao04f2dec2010-03-19 13:34:45 -0700344/*
345 * Static function to get the expected throughput from an iwl_scale_tbl_info
346 * that wraps a NULL pointer check
347 */
348static s32 get_expected_tpt(struct iwl_scale_tbl_info *tbl, int rs_index)
349{
350 if (tbl->expected_tpt)
351 return tbl->expected_tpt[rs_index];
352 return 0;
353}
354
Ben Cahill77626352007-11-29 11:09:44 +0800355/**
356 * rs_collect_tx_data - Update the success/failure sliding window
357 *
358 * We keep a sliding window of the last 62 packets transmitted
359 * at this rate. window->data contains the bitmask of successful
360 * packets.
361 */
Shanyu Zhao04f2dec2010-03-19 13:34:45 -0700362static int rs_collect_tx_data(struct iwl_scale_tbl_info *tbl,
363 int scale_index, int attempts, int successes)
Zhu Yib481de92007-09-25 17:54:57 -0700364{
Tomas Winklere227cea2008-07-18 13:53:05 +0800365 struct iwl_rate_scale_data *window = NULL;
Guy Cohen39e88502008-04-23 17:14:57 -0700366 static const u64 mask = (((u64)1) << (IWL_RATE_MAX_WINDOW - 1));
Shanyu Zhao04f2dec2010-03-19 13:34:45 -0700367 s32 fail_count, tpt;
Zhu Yib481de92007-09-25 17:54:57 -0700368
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800369 if (scale_index < 0 || scale_index >= IWL_RATE_COUNT)
370 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -0700371
Daniel C Halperine3949d62009-09-17 10:43:50 -0700372 /* Select window for current tx bit rate */
Shanyu Zhao04f2dec2010-03-19 13:34:45 -0700373 window = &(tbl->win[scale_index]);
374
375 /* Get expected throughput */
376 tpt = get_expected_tpt(tbl, scale_index);
Zhu Yib481de92007-09-25 17:54:57 -0700377
Ben Cahill77626352007-11-29 11:09:44 +0800378 /*
379 * Keep track of only the latest 62 tx frame attempts in this rate's
380 * history window; anything older isn't really relevant any more.
381 * If we have filled up the sliding window, drop the oldest attempt;
382 * if the oldest attempt (highest bit in bitmap) shows "success",
383 * subtract "1" from the success counter (this is the main reason
384 * we keep these bitmaps!).
385 */
Daniel C Halperine3949d62009-09-17 10:43:50 -0700386 while (attempts > 0) {
Guy Cohen39e88502008-04-23 17:14:57 -0700387 if (window->counter >= IWL_RATE_MAX_WINDOW) {
388
389 /* remove earliest */
390 window->counter = IWL_RATE_MAX_WINDOW - 1;
391
Ron Rindjunsky99556432008-01-28 14:07:25 +0200392 if (window->data & mask) {
393 window->data &= ~mask;
Guy Cohen39e88502008-04-23 17:14:57 -0700394 window->success_counter--;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200395 }
Zhu Yib481de92007-09-25 17:54:57 -0700396 }
Zhu Yib481de92007-09-25 17:54:57 -0700397
Ron Rindjunsky99556432008-01-28 14:07:25 +0200398 /* Increment frames-attempted counter */
399 window->counter++;
Ben Cahill77626352007-11-29 11:09:44 +0800400
Daniel C Halperine3949d62009-09-17 10:43:50 -0700401 /* Shift bitmap by one frame to throw away oldest history */
Guy Cohen90d77952008-09-09 10:54:53 +0800402 window->data <<= 1;
Daniel C Halperine3949d62009-09-17 10:43:50 -0700403
404 /* Mark the most recent #successes attempts as successful */
Ron Rindjunsky99556432008-01-28 14:07:25 +0200405 if (successes > 0) {
Guy Cohen39e88502008-04-23 17:14:57 -0700406 window->success_counter++;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200407 window->data |= 0x1;
408 successes--;
409 }
410
Daniel C Halperine3949d62009-09-17 10:43:50 -0700411 attempts--;
Zhu Yib481de92007-09-25 17:54:57 -0700412 }
413
Ben Cahill77626352007-11-29 11:09:44 +0800414 /* Calculate current success ratio, avoid divide-by-0! */
Zhu Yib481de92007-09-25 17:54:57 -0700415 if (window->counter > 0)
416 window->success_ratio = 128 * (100 * window->success_counter)
417 / window->counter;
418 else
419 window->success_ratio = IWL_INVALID_VALUE;
420
421 fail_count = window->counter - window->success_counter;
422
Ben Cahill77626352007-11-29 11:09:44 +0800423 /* Calculate average throughput, if we have enough history. */
Zhu Yib481de92007-09-25 17:54:57 -0700424 if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
425 (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
426 window->average_tpt = (window->success_ratio * tpt + 64) / 128;
427 else
428 window->average_tpt = IWL_INVALID_VALUE;
429
Ben Cahill77626352007-11-29 11:09:44 +0800430 /* Tag this window as having been updated */
Zhu Yib481de92007-09-25 17:54:57 -0700431 window->stamp = jiffies;
432
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800433 return 0;
Zhu Yib481de92007-09-25 17:54:57 -0700434}
435
Ben Cahill77626352007-11-29 11:09:44 +0800436/*
437 * Fill uCode API rate_n_flags field, based on "search" or "active" table.
438 */
Guy Cohen39e88502008-04-23 17:14:57 -0700439/* FIXME:RS:remove this function and put the flags statically in the table */
Tomas Winkler978785a2008-12-19 10:37:31 +0800440static u32 rate_n_flags_from_tbl(struct iwl_priv *priv,
441 struct iwl_scale_tbl_info *tbl,
442 int index, u8 use_green)
Zhu Yib481de92007-09-25 17:54:57 -0700443{
Guy Cohen39e88502008-04-23 17:14:57 -0700444 u32 rate_n_flags = 0;
445
Zhu Yib481de92007-09-25 17:54:57 -0700446 if (is_legacy(tbl->lq_type)) {
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800447 rate_n_flags = iwl_rates[index].plcp;
Zhu Yib481de92007-09-25 17:54:57 -0700448 if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
Guy Cohen39e88502008-04-23 17:14:57 -0700449 rate_n_flags |= RATE_MCS_CCK_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700450
Guy Cohenfde0db32008-04-21 15:42:01 -0700451 } else if (is_Ht(tbl->lq_type)) {
452 if (index > IWL_LAST_OFDM_RATE) {
Tomas Winkler978785a2008-12-19 10:37:31 +0800453 IWL_ERR(priv, "Invalid HT rate index %d\n", index);
Zhu Yib481de92007-09-25 17:54:57 -0700454 index = IWL_LAST_OFDM_RATE;
Guy Cohenfde0db32008-04-21 15:42:01 -0700455 }
Guy Cohen39e88502008-04-23 17:14:57 -0700456 rate_n_flags = RATE_MCS_HT_MSK;
Guy Cohenfde0db32008-04-21 15:42:01 -0700457
458 if (is_siso(tbl->lq_type))
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800459 rate_n_flags |= iwl_rates[index].plcp_siso;
Guy Cohenfde0db32008-04-21 15:42:01 -0700460 else if (is_mimo2(tbl->lq_type))
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800461 rate_n_flags |= iwl_rates[index].plcp_mimo2;
Guy Cohenfde0db32008-04-21 15:42:01 -0700462 else
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800463 rate_n_flags |= iwl_rates[index].plcp_mimo3;
Zhu Yib481de92007-09-25 17:54:57 -0700464 } else {
Tomas Winkler978785a2008-12-19 10:37:31 +0800465 IWL_ERR(priv, "Invalid tbl->lq_type %d\n", tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -0700466 }
467
Guy Cohen39e88502008-04-23 17:14:57 -0700468 rate_n_flags |= ((tbl->ant_type << RATE_MCS_ANT_POS) &
Guy Cohenfde0db32008-04-21 15:42:01 -0700469 RATE_MCS_ANT_ABC_MSK);
Zhu Yib481de92007-09-25 17:54:57 -0700470
Guy Cohen39e88502008-04-23 17:14:57 -0700471 if (is_Ht(tbl->lq_type)) {
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700472 if (tbl->is_ht40) {
Guy Cohen39e88502008-04-23 17:14:57 -0700473 if (tbl->is_dup)
474 rate_n_flags |= RATE_MCS_DUP_MSK;
475 else
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700476 rate_n_flags |= RATE_MCS_HT40_MSK;
Guy Cohen39e88502008-04-23 17:14:57 -0700477 }
478 if (tbl->is_SGI)
479 rate_n_flags |= RATE_MCS_SGI_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700480
Guy Cohen39e88502008-04-23 17:14:57 -0700481 if (use_green) {
482 rate_n_flags |= RATE_MCS_GF_MSK;
483 if (is_siso(tbl->lq_type) && tbl->is_SGI) {
484 rate_n_flags &= ~RATE_MCS_SGI_MSK;
Tomas Winkler978785a2008-12-19 10:37:31 +0800485 IWL_ERR(priv, "GF was set with SGI:SISO\n");
Guy Cohen39e88502008-04-23 17:14:57 -0700486 }
487 }
Zhu Yib481de92007-09-25 17:54:57 -0700488 }
Guy Cohen39e88502008-04-23 17:14:57 -0700489 return rate_n_flags;
Zhu Yib481de92007-09-25 17:54:57 -0700490}
491
Ben Cahill77626352007-11-29 11:09:44 +0800492/*
493 * Interpret uCode API's rate_n_flags format,
494 * fill "search" or "active" tx mode table.
495 */
Guy Cohen39e88502008-04-23 17:14:57 -0700496static int rs_get_tbl_info_from_mcs(const u32 rate_n_flags,
Johannes Berg8318d782008-01-24 19:38:38 +0100497 enum ieee80211_band band,
Tomas Winklere227cea2008-07-18 13:53:05 +0800498 struct iwl_scale_tbl_info *tbl,
Zhu Yib481de92007-09-25 17:54:57 -0700499 int *rate_idx)
500{
Guy Cohen39e88502008-04-23 17:14:57 -0700501 u32 ant_msk = (rate_n_flags & RATE_MCS_ANT_ABC_MSK);
502 u8 num_of_ant = get_num_of_ant_from_rate(rate_n_flags);
503 u8 mcs;
Zhu Yib481de92007-09-25 17:54:57 -0700504
Tomas Winklere7d326ac2008-06-12 09:47:11 +0800505 *rate_idx = iwl_hwrate_to_plcp_idx(rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -0700506
Guy Cohenfde0db32008-04-21 15:42:01 -0700507 if (*rate_idx == IWL_RATE_INVALID) {
Zhu Yib481de92007-09-25 17:54:57 -0700508 *rate_idx = -1;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800509 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -0700510 }
Ben Cahill77626352007-11-29 11:09:44 +0800511 tbl->is_SGI = 0; /* default legacy setup */
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700512 tbl->is_ht40 = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700513 tbl->is_dup = 0;
Guy Cohenfde0db32008-04-21 15:42:01 -0700514 tbl->ant_type = (ant_msk >> RATE_MCS_ANT_POS);
515 tbl->lq_type = LQ_NONE;
Mohamed Abbasd6e93392009-05-08 13:44:39 -0700516 tbl->max_search = IWL_MAX_SEARCH;
Zhu Yib481de92007-09-25 17:54:57 -0700517
Ben Cahill77626352007-11-29 11:09:44 +0800518 /* legacy rate format */
Guy Cohen39e88502008-04-23 17:14:57 -0700519 if (!(rate_n_flags & RATE_MCS_HT_MSK)) {
Guy Cohenfde0db32008-04-21 15:42:01 -0700520 if (num_of_ant == 1) {
Johannes Berg8318d782008-01-24 19:38:38 +0100521 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700522 tbl->lq_type = LQ_A;
523 else
524 tbl->lq_type = LQ_G;
Zhu Yib481de92007-09-25 17:54:57 -0700525 }
Guy Cohenfde0db32008-04-21 15:42:01 -0700526 /* HT rate format */
Zhu Yib481de92007-09-25 17:54:57 -0700527 } else {
Guy Cohen39e88502008-04-23 17:14:57 -0700528 if (rate_n_flags & RATE_MCS_SGI_MSK)
Zhu Yib481de92007-09-25 17:54:57 -0700529 tbl->is_SGI = 1;
530
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700531 if ((rate_n_flags & RATE_MCS_HT40_MSK) ||
Guy Cohen39e88502008-04-23 17:14:57 -0700532 (rate_n_flags & RATE_MCS_DUP_MSK))
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700533 tbl->is_ht40 = 1;
Zhu Yib481de92007-09-25 17:54:57 -0700534
Guy Cohen39e88502008-04-23 17:14:57 -0700535 if (rate_n_flags & RATE_MCS_DUP_MSK)
Zhu Yib481de92007-09-25 17:54:57 -0700536 tbl->is_dup = 1;
Guy Cohenfde0db32008-04-21 15:42:01 -0700537
Guy Cohen39e88502008-04-23 17:14:57 -0700538 mcs = rs_extract_rate(rate_n_flags);
Guy Cohenfde0db32008-04-21 15:42:01 -0700539
Guy Cohen39e88502008-04-23 17:14:57 -0700540 /* SISO */
541 if (mcs <= IWL_RATE_SISO_60M_PLCP) {
Guy Cohenfde0db32008-04-21 15:42:01 -0700542 if (num_of_ant == 1)
543 tbl->lq_type = LQ_SISO; /*else NONE*/
544 /* MIMO2 */
Guy Cohen39e88502008-04-23 17:14:57 -0700545 } else if (mcs <= IWL_RATE_MIMO2_60M_PLCP) {
Guy Cohenfde0db32008-04-21 15:42:01 -0700546 if (num_of_ant == 2)
547 tbl->lq_type = LQ_MIMO2;
548 /* MIMO3 */
549 } else {
Mohamed Abbasd6e93392009-05-08 13:44:39 -0700550 if (num_of_ant == 3) {
551 tbl->max_search = IWL_MAX_11N_MIMO3_SEARCH;
Guy Cohenfde0db32008-04-21 15:42:01 -0700552 tbl->lq_type = LQ_MIMO3;
Mohamed Abbasd6e93392009-05-08 13:44:39 -0700553 }
Guy Cohenfde0db32008-04-21 15:42:01 -0700554 }
Zhu Yib481de92007-09-25 17:54:57 -0700555 }
556 return 0;
557}
Guy Cohenaade00c2008-04-23 17:14:59 -0700558
559/* switch to another antenna/antennas and return 1 */
560/* if no other valid antenna found, return 0 */
561static int rs_toggle_antenna(u32 valid_ant, u32 *rate_n_flags,
Tomas Winklere227cea2008-07-18 13:53:05 +0800562 struct iwl_scale_tbl_info *tbl)
Zhu Yib481de92007-09-25 17:54:57 -0700563{
Guy Cohenaade00c2008-04-23 17:14:59 -0700564 u8 new_ant_type;
565
566 if (!tbl->ant_type || tbl->ant_type > ANT_ABC)
567 return 0;
568
569 if (!rs_is_valid_ant(valid_ant, tbl->ant_type))
570 return 0;
571
572 new_ant_type = ant_toggle_lookup[tbl->ant_type];
573
574 while ((new_ant_type != tbl->ant_type) &&
575 !rs_is_valid_ant(valid_ant, new_ant_type))
576 new_ant_type = ant_toggle_lookup[new_ant_type];
577
578 if (new_ant_type == tbl->ant_type)
579 return 0;
580
581 tbl->ant_type = new_ant_type;
582 *rate_n_flags &= ~RATE_MCS_ANT_ABC_MSK;
583 *rate_n_flags |= new_ant_type << RATE_MCS_ANT_POS;
584 return 1;
Zhu Yib481de92007-09-25 17:54:57 -0700585}
586
Daniel C Halperinb2617932009-08-13 13:30:59 -0700587/**
588 * Green-field mode is valid if the station supports it and
589 * there are no non-GF stations present in the BSS.
590 */
591static inline u8 rs_use_green(struct ieee80211_sta *sta,
Johannes Bergfad95bf2009-09-11 10:38:15 -0700592 struct iwl_ht_config *ht_conf)
Zhu Yib481de92007-09-25 17:54:57 -0700593{
Daniel C Halperinb2617932009-08-13 13:30:59 -0700594 return (sta->ht_cap.cap & IEEE80211_HT_CAP_GRN_FLD) &&
595 !(ht_conf->non_GF_STA_present);
Guy Cohenf935a6d2008-04-23 17:15:02 -0700596}
Zhu Yib481de92007-09-25 17:54:57 -0700597
598/**
599 * rs_get_supported_rates - get the available rates
600 *
601 * if management frame or broadcast frame only return
602 * basic available rates.
603 *
604 */
Tomas Winklere227cea2008-07-18 13:53:05 +0800605static u16 rs_get_supported_rates(struct iwl_lq_sta *lq_sta,
606 struct ieee80211_hdr *hdr,
607 enum iwl_table_type rate_type)
Zhu Yib481de92007-09-25 17:54:57 -0700608{
Guy Coheneecd6e52008-04-23 17:14:58 -0700609 if (is_legacy(rate_type)) {
610 return lq_sta->active_legacy_rate;
611 } else {
612 if (is_siso(rate_type))
613 return lq_sta->active_siso_rate;
614 else if (is_mimo2(rate_type))
615 return lq_sta->active_mimo2_rate;
616 else
617 return lq_sta->active_mimo3_rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -0800618 }
Zhu Yib481de92007-09-25 17:54:57 -0700619}
620
Ester Kummerbf403db2008-05-05 10:22:40 +0800621static u16 rs_get_adjacent_rate(struct iwl_priv *priv, u8 index, u16 rate_mask,
622 int rate_type)
Zhu Yib481de92007-09-25 17:54:57 -0700623{
624 u8 high = IWL_RATE_INVALID;
625 u8 low = IWL_RATE_INVALID;
626
Ian Schram01ebd062007-10-25 17:15:22 +0800627 /* 802.11A or ht walks to the next literal adjacent rate in
Zhu Yib481de92007-09-25 17:54:57 -0700628 * the rate table */
629 if (is_a_band(rate_type) || !is_legacy(rate_type)) {
630 int i;
631 u32 mask;
632
633 /* Find the previous rate that is in the rate mask */
634 i = index - 1;
635 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
636 if (rate_mask & mask) {
637 low = i;
638 break;
639 }
640 }
641
642 /* Find the next rate that is in the rate mask */
643 i = index + 1;
644 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
645 if (rate_mask & mask) {
646 high = i;
647 break;
648 }
649 }
650
651 return (high << 8) | low;
652 }
653
654 low = index;
655 while (low != IWL_RATE_INVALID) {
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800656 low = iwl_rates[low].prev_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700657 if (low == IWL_RATE_INVALID)
658 break;
659 if (rate_mask & (1 << low))
660 break;
Tomas Winklere1623442009-01-27 14:27:56 -0800661 IWL_DEBUG_RATE(priv, "Skipping masked lower rate: %d\n", low);
Zhu Yib481de92007-09-25 17:54:57 -0700662 }
663
664 high = index;
665 while (high != IWL_RATE_INVALID) {
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800666 high = iwl_rates[high].next_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700667 if (high == IWL_RATE_INVALID)
668 break;
669 if (rate_mask & (1 << high))
670 break;
Tomas Winklere1623442009-01-27 14:27:56 -0800671 IWL_DEBUG_RATE(priv, "Skipping masked higher rate: %d\n", high);
Zhu Yib481de92007-09-25 17:54:57 -0700672 }
673
674 return (high << 8) | low;
675}
676
Tomas Winklere227cea2008-07-18 13:53:05 +0800677static u32 rs_get_lower_rate(struct iwl_lq_sta *lq_sta,
678 struct iwl_scale_tbl_info *tbl,
679 u8 scale_index, u8 ht_possible)
Zhu Yib481de92007-09-25 17:54:57 -0700680{
Zhu Yib481de92007-09-25 17:54:57 -0700681 s32 low;
682 u16 rate_mask;
683 u16 high_low;
684 u8 switch_to_legacy = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -0800685 u8 is_green = lq_sta->is_green;
Wey-Yi Guy2ff65782009-09-11 10:38:18 -0700686 struct iwl_priv *priv = lq_sta->drv;
Zhu Yib481de92007-09-25 17:54:57 -0700687
688 /* check if we need to switch from HT to legacy rates.
689 * assumption is that mandatory rates (1Mbps or 6Mbps)
690 * are always supported (spec demand) */
691 if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_index)) {
692 switch_to_legacy = 1;
693 scale_index = rs_ht_to_legacy[scale_index];
Johannes Berg8318d782008-01-24 19:38:38 +0100694 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700695 tbl->lq_type = LQ_A;
696 else
697 tbl->lq_type = LQ_G;
698
Guy Cohen69fdb302008-04-23 17:15:01 -0700699 if (num_of_ant(tbl->ant_type) > 1)
Wey-Yi Guy2ff65782009-09-11 10:38:18 -0700700 tbl->ant_type =
701 first_antenna(priv->hw_params.valid_tx_ant);
Zhu Yib481de92007-09-25 17:54:57 -0700702
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700703 tbl->is_ht40 = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700704 tbl->is_SGI = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -0700705 tbl->max_search = IWL_MAX_SEARCH;
Zhu Yib481de92007-09-25 17:54:57 -0700706 }
707
Guy Coheneecd6e52008-04-23 17:14:58 -0700708 rate_mask = rs_get_supported_rates(lq_sta, NULL, tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -0700709
Ben Cahill77626352007-11-29 11:09:44 +0800710 /* Mask with station rate restriction */
Zhu Yib481de92007-09-25 17:54:57 -0700711 if (is_legacy(tbl->lq_type)) {
Ben Cahill77626352007-11-29 11:09:44 +0800712 /* supp_rates has no CCK bits in A mode */
Johannes Berg8318d782008-01-24 19:38:38 +0100713 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700714 rate_mask = (u16)(rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -0800715 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
Zhu Yib481de92007-09-25 17:54:57 -0700716 else
Tomas Winklerc33104f2008-01-14 17:46:21 -0800717 rate_mask = (u16)(rate_mask & lq_sta->supp_rates);
Zhu Yib481de92007-09-25 17:54:57 -0700718 }
719
Ben Cahill77626352007-11-29 11:09:44 +0800720 /* If we switched from HT to legacy, check current rate */
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800721 if (switch_to_legacy && (rate_mask & (1 << scale_index))) {
Guy Cohen39e88502008-04-23 17:14:57 -0700722 low = scale_index;
723 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700724 }
725
Ester Kummerbf403db2008-05-05 10:22:40 +0800726 high_low = rs_get_adjacent_rate(lq_sta->drv, scale_index, rate_mask,
727 tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -0700728 low = high_low & 0xff;
729
Guy Cohen39e88502008-04-23 17:14:57 -0700730 if (low == IWL_RATE_INVALID)
731 low = scale_index;
732
733out:
Tomas Winkler978785a2008-12-19 10:37:31 +0800734 return rate_n_flags_from_tbl(lq_sta->drv, tbl, low, is_green);
Zhu Yib481de92007-09-25 17:54:57 -0700735}
736
Ben Cahill77626352007-11-29 11:09:44 +0800737/*
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700738 * Simple function to compare two rate scale table types
739 */
740static bool table_type_matches(struct iwl_scale_tbl_info *a,
741 struct iwl_scale_tbl_info *b)
742{
743 return (a->lq_type == b->lq_type) && (a->ant_type == b->ant_type) &&
744 (a->is_SGI == b->is_SGI);
745}
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700746
747/*
Ben Cahill77626352007-11-29 11:09:44 +0800748 * mac80211 sends us Tx status
749 */
Johannes Berg4b7679a2008-09-18 18:14:18 +0200750static void rs_tx_status(void *priv_r, struct ieee80211_supported_band *sband,
751 struct ieee80211_sta *sta, void *priv_sta,
Johannes Berge039fa42008-05-15 12:55:29 +0200752 struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -0700753{
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700754 int legacy_success;
755 int retries;
756 int rs_index, mac_index, i;
Tomas Winkler417f1142008-11-12 13:14:06 -0800757 struct iwl_lq_sta *lq_sta = priv_sta;
Tomas Winkler66c73db2008-04-15 16:01:40 -0700758 struct iwl_link_quality_cmd *table;
Zhu Yib481de92007-09-25 17:54:57 -0700759 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berg4b7679a2008-09-18 18:14:18 +0200760 struct iwl_priv *priv = (struct iwl_priv *)priv_r;
Johannes Berge039fa42008-05-15 12:55:29 +0200761 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Daniel C Halperin50273092009-08-28 09:44:45 -0700762 enum mac80211_rate_control_flags mac_flags;
Guy Cohen39e88502008-04-23 17:14:57 -0700763 u32 tx_rate;
Tomas Winklere227cea2008-07-18 13:53:05 +0800764 struct iwl_scale_tbl_info tbl_type;
Shanyu Zhao04f2dec2010-03-19 13:34:45 -0700765 struct iwl_scale_tbl_info *curr_tbl, *other_tbl, *tmp_tbl;
Zhu Yib481de92007-09-25 17:54:57 -0700766
Tomas Winklere1623442009-01-27 14:27:56 -0800767 IWL_DEBUG_RATE_LIMIT(priv, "get frame ack response, update rate scale window\n");
Zhu Yib481de92007-09-25 17:54:57 -0700768
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800769 /* Treat uninitialized rate scaling data same as non-existing. */
770 if (!lq_sta) {
771 IWL_DEBUG_RATE(priv, "Station rate scaling not created yet.\n");
772 return;
773 } else if (!lq_sta->drv) {
774 IWL_DEBUG_RATE(priv, "Rate scaling not initialized yet.\n");
775 return;
776 }
777
Tomas Winkler417f1142008-11-12 13:14:06 -0800778 if (!ieee80211_is_data(hdr->frame_control) ||
Gábor Stefanik514d65c2009-04-23 19:36:08 +0200779 info->flags & IEEE80211_TX_CTL_NO_ACK)
Zhu Yib481de92007-09-25 17:54:57 -0700780 return;
781
Daniel C Halperine3949d62009-09-17 10:43:50 -0700782 /* This packet was aggregated but doesn't carry status info */
Johannes Berge039fa42008-05-15 12:55:29 +0200783 if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
784 !(info->flags & IEEE80211_TX_STAT_AMPDU))
Ron Rindjunsky99556432008-01-28 14:07:25 +0200785 return;
786
Ben Cahill77626352007-11-29 11:09:44 +0800787 /*
788 * Ignore this Tx frame response if its initial rate doesn't match
789 * that of latest Link Quality command. There may be stragglers
790 * from a previous Link Quality command, but we're no longer interested
791 * in those; they're either from the "active" mode while we're trying
792 * to check "search" mode, or a prior "search" mode after we've moved
793 * to a new "search" mode (which might become the new "active" mode).
794 */
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700795 table = &lq_sta->lq;
Guy Cohen39e88502008-04-23 17:14:57 -0700796 tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
797 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800798 if (priv->band == IEEE80211_BAND_5GHZ)
799 rs_index -= IWL_FIRST_OFDM_RATE;
Daniel C Halperin50273092009-08-28 09:44:45 -0700800 mac_flags = info->status.rates[0].flags;
801 mac_index = info->status.rates[0].idx;
Daniel C Halperin31513be2009-08-28 09:44:47 -0700802 /* For HT packets, map MCS to PLCP */
803 if (mac_flags & IEEE80211_TX_RC_MCS) {
804 mac_index &= RATE_MCS_CODE_MSK; /* Remove # of streams */
805 if (mac_index >= (IWL_RATE_9M_INDEX - IWL_FIRST_OFDM_RATE))
806 mac_index++;
Daniel C Halperin392a0ba2009-09-11 10:38:08 -0700807 /*
808 * mac80211 HT index is always zero-indexed; we need to move
809 * HT OFDM rates after CCK rates in 2.4 GHz band
810 */
811 if (priv->band == IEEE80211_BAND_2GHZ)
812 mac_index += IWL_FIRST_OFDM_RATE;
Daniel C Halperin31513be2009-08-28 09:44:47 -0700813 }
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700814 /* Here we actually compare this rate to the latest LQ command */
Daniel C Halperin50273092009-08-28 09:44:45 -0700815 if ((mac_index < 0) ||
816 (tbl_type.is_SGI != !!(mac_flags & IEEE80211_TX_RC_SHORT_GI)) ||
817 (tbl_type.is_ht40 != !!(mac_flags & IEEE80211_TX_RC_40_MHZ_WIDTH)) ||
818 (tbl_type.is_dup != !!(mac_flags & IEEE80211_TX_RC_DUP_DATA)) ||
Johannes Berge6a98542008-10-21 12:40:02 +0200819 (tbl_type.ant_type != info->antenna_sel_tx) ||
Daniel C Halperin50273092009-08-28 09:44:45 -0700820 (!!(tx_rate & RATE_MCS_HT_MSK) != !!(mac_flags & IEEE80211_TX_RC_MCS)) ||
821 (!!(tx_rate & RATE_MCS_GF_MSK) != !!(mac_flags & IEEE80211_TX_RC_GREEN_FIELD)) ||
Daniel C Halperin31513be2009-08-28 09:44:47 -0700822 (rs_index != mac_index)) {
823 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 -0700824 /*
825 * Since rates mis-match, the last LQ command may have failed.
826 * After IWL_MISSED_RATE_MAX mis-matches, resync the uCode with
827 * ... driver.
Mohamed Abbasd5e49032008-12-12 08:22:15 -0800828 */
Abbas, Mohamed7d049e52009-01-20 21:33:53 -0800829 lq_sta->missed_rate_counter++;
830 if (lq_sta->missed_rate_counter > IWL_MISSED_RATE_MAX) {
831 lq_sta->missed_rate_counter = 0;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800832 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC, false);
Abbas, Mohamed7d049e52009-01-20 21:33:53 -0800833 }
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700834 /* Regardless, ignore this status info for outdated rate */
835 return;
836 } else
837 /* Rate did match, so reset the missed_rate_counter */
838 lq_sta->missed_rate_counter = 0;
839
840 /* Figure out if rate scale algorithm is in active or search table */
841 if (table_type_matches(&tbl_type,
842 &(lq_sta->lq_info[lq_sta->active_tbl]))) {
843 curr_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
844 other_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
845 } else if (table_type_matches(&tbl_type,
846 &lq_sta->lq_info[1 - lq_sta->active_tbl])) {
847 curr_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
848 other_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
849 } else {
850 IWL_DEBUG_RATE(priv, "Neither active nor search matches tx rate\n");
851 return;
Zhu Yib481de92007-09-25 17:54:57 -0700852 }
Zhu Yib481de92007-09-25 17:54:57 -0700853
Ben Cahill77626352007-11-29 11:09:44 +0800854 /*
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700855 * Updating the frame history depends on whether packets were
856 * aggregated.
857 *
858 * For aggregation, all packets were transmitted at the same rate, the
859 * first index into rate scale table.
Ben Cahill77626352007-11-29 11:09:44 +0800860 */
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700861 if (info->flags & IEEE80211_TX_STAT_AMPDU) {
862 tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
863 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type,
864 &rs_index);
Shanyu Zhao04f2dec2010-03-19 13:34:45 -0700865 rs_collect_tx_data(curr_tbl, rs_index,
Daniel Halperine3a3cd82010-04-18 09:27:58 -0700866 info->status.ampdu_len,
867 info->status.ampdu_ack_len);
Zhu Yib481de92007-09-25 17:54:57 -0700868
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700869 /* Update success/fail counts if not searching for new mode */
870 if (lq_sta->stay_in_tbl) {
Daniel Halperine3a3cd82010-04-18 09:27:58 -0700871 lq_sta->total_success += info->status.ampdu_ack_len;
872 lq_sta->total_failed += (info->status.ampdu_len -
873 info->status.ampdu_ack_len);
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700874 }
875 } else {
876 /*
877 * For legacy, update frame history with for each Tx retry.
878 */
879 retries = info->status.rates[0].count - 1;
880 /* HW doesn't send more than 15 retries */
881 retries = min(retries, 15);
882
883 /* The last transmission may have been successful */
884 legacy_success = !!(info->flags & IEEE80211_TX_STAT_ACK);
885 /* Collect data for each rate used during failed TX attempts */
886 for (i = 0; i <= retries; ++i) {
887 tx_rate = le32_to_cpu(table->rs_table[i].rate_n_flags);
888 rs_get_tbl_info_from_mcs(tx_rate, priv->band,
889 &tbl_type, &rs_index);
890 /*
891 * Only collect stats if retried rate is in the same RS
892 * table as active/search.
893 */
894 if (table_type_matches(&tbl_type, curr_tbl))
Shanyu Zhao04f2dec2010-03-19 13:34:45 -0700895 tmp_tbl = curr_tbl;
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700896 else if (table_type_matches(&tbl_type, other_tbl))
Shanyu Zhao04f2dec2010-03-19 13:34:45 -0700897 tmp_tbl = other_tbl;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200898 else
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700899 continue;
Shanyu Zhao04f2dec2010-03-19 13:34:45 -0700900 rs_collect_tx_data(tmp_tbl, rs_index, 1,
901 i < retries ? 0 : legacy_success);
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700902 }
903
904 /* Update success/fail counts if not searching for new mode */
905 if (lq_sta->stay_in_tbl) {
906 lq_sta->total_success += legacy_success;
907 lq_sta->total_failed += retries + (1 - legacy_success);
Ron Rindjunsky99556432008-01-28 14:07:25 +0200908 }
Zhu Yib481de92007-09-25 17:54:57 -0700909 }
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700910 /* The last TX rate is cached in lq_sta; it's set in if/else above */
911 lq_sta->last_rate_n_flags = tx_rate;
Zhu Yib481de92007-09-25 17:54:57 -0700912
Ben Cahill77626352007-11-29 11:09:44 +0800913 /* See if there's a better rate or modulation mode to try. */
Abbas, Mohamedc338ba32009-01-21 10:58:02 -0800914 if (sta && sta->supp_rates[sband->band])
Gábor Stefanik514d65c2009-04-23 19:36:08 +0200915 rs_rate_scale_perform(priv, skb, sta, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -0700916}
917
Ben Cahill77626352007-11-29 11:09:44 +0800918/*
919 * Begin a period of staying with a selected modulation mode.
920 * Set "stay_in_tbl" flag to prevent any mode switches.
921 * Set frame tx success limits according to legacy vs. high-throughput,
922 * and reset overall (spanning all rates) tx success history statistics.
923 * These control how long we stay using same modulation mode before
924 * searching for a new mode.
925 */
Ester Kummerbf403db2008-05-05 10:22:40 +0800926static void rs_set_stay_in_table(struct iwl_priv *priv, u8 is_legacy,
Tomas Winklere227cea2008-07-18 13:53:05 +0800927 struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -0700928{
Tomas Winklere1623442009-01-27 14:27:56 -0800929 IWL_DEBUG_RATE(priv, "we are staying in the same table\n");
Tomas Winklerc33104f2008-01-14 17:46:21 -0800930 lq_sta->stay_in_tbl = 1; /* only place this gets set */
Zhu Yib481de92007-09-25 17:54:57 -0700931 if (is_legacy) {
Tomas Winklerc33104f2008-01-14 17:46:21 -0800932 lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT;
933 lq_sta->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
934 lq_sta->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
Zhu Yib481de92007-09-25 17:54:57 -0700935 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -0800936 lq_sta->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
937 lq_sta->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
938 lq_sta->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
Zhu Yib481de92007-09-25 17:54:57 -0700939 }
Tomas Winklerc33104f2008-01-14 17:46:21 -0800940 lq_sta->table_count = 0;
941 lq_sta->total_failed = 0;
942 lq_sta->total_success = 0;
Mohamed Abbas447fee72009-04-20 14:37:02 -0700943 lq_sta->flush_timer = jiffies;
Mohamed Abbasd6e93392009-05-08 13:44:39 -0700944 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700945}
946
Ben Cahill77626352007-11-29 11:09:44 +0800947/*
948 * Find correct throughput table for given mode of modulation
949 */
Tomas Winklere227cea2008-07-18 13:53:05 +0800950static void rs_set_expected_tpt_table(struct iwl_lq_sta *lq_sta,
951 struct iwl_scale_tbl_info *tbl)
Zhu Yib481de92007-09-25 17:54:57 -0700952{
Daniel C Halperine3949d62009-09-17 10:43:50 -0700953 /* Used to choose among HT tables */
954 s32 (*ht_tbl_pointer)[IWL_RATE_COUNT];
955
956 /* Check for invalid LQ type */
957 if (WARN_ON_ONCE(!is_legacy(tbl->lq_type) && !is_Ht(tbl->lq_type))) {
958 tbl->expected_tpt = expected_tpt_legacy;
959 return;
960 }
961
962 /* Legacy rates have only one table */
Zhu Yib481de92007-09-25 17:54:57 -0700963 if (is_legacy(tbl->lq_type)) {
Daniel C Halperine3949d62009-09-17 10:43:50 -0700964 tbl->expected_tpt = expected_tpt_legacy;
965 return;
966 }
967
968 /* Choose among many HT tables depending on number of streams
969 * (SISO/MIMO2/MIMO3), channel width (20/40), SGI, and aggregation
970 * status */
971 if (is_siso(tbl->lq_type) && (!tbl->is_ht40 || lq_sta->is_dup))
972 ht_tbl_pointer = expected_tpt_siso20MHz;
973 else if (is_siso(tbl->lq_type))
974 ht_tbl_pointer = expected_tpt_siso40MHz;
975 else if (is_mimo2(tbl->lq_type) && (!tbl->is_ht40 || lq_sta->is_dup))
976 ht_tbl_pointer = expected_tpt_mimo2_20MHz;
977 else if (is_mimo2(tbl->lq_type))
978 ht_tbl_pointer = expected_tpt_mimo2_40MHz;
979 else if (is_mimo3(tbl->lq_type) && (!tbl->is_ht40 || lq_sta->is_dup))
980 ht_tbl_pointer = expected_tpt_mimo3_20MHz;
981 else /* if (is_mimo3(tbl->lq_type)) <-- must be true */
982 ht_tbl_pointer = expected_tpt_mimo3_40MHz;
983
984 if (!tbl->is_SGI && !lq_sta->is_agg) /* Normal */
985 tbl->expected_tpt = ht_tbl_pointer[0];
986 else if (tbl->is_SGI && !lq_sta->is_agg) /* SGI */
987 tbl->expected_tpt = ht_tbl_pointer[1];
988 else if (!tbl->is_SGI && lq_sta->is_agg) /* AGG */
989 tbl->expected_tpt = ht_tbl_pointer[2];
990 else /* AGG+SGI */
991 tbl->expected_tpt = ht_tbl_pointer[3];
Zhu Yib481de92007-09-25 17:54:57 -0700992}
993
Ben Cahill77626352007-11-29 11:09:44 +0800994/*
995 * Find starting rate for new "search" high-throughput mode of modulation.
996 * Goal is to find lowest expected rate (under perfect conditions) that is
997 * above the current measured throughput of "active" mode, to give new mode
998 * a fair chance to prove itself without too many challenges.
999 *
1000 * This gets called when transitioning to more aggressive modulation
1001 * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
1002 * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
1003 * to decrease to match "active" throughput. When moving from MIMO to SISO,
1004 * bit rate will typically need to increase, but not if performance was bad.
1005 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001006static s32 rs_get_best_rate(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001007 struct iwl_lq_sta *lq_sta,
1008 struct iwl_scale_tbl_info *tbl, /* "search" */
Guy Cohenf935a6d2008-04-23 17:15:02 -07001009 u16 rate_mask, s8 index)
Zhu Yib481de92007-09-25 17:54:57 -07001010{
Ben Cahill77626352007-11-29 11:09:44 +08001011 /* "active" values */
Tomas Winklere227cea2008-07-18 13:53:05 +08001012 struct iwl_scale_tbl_info *active_tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001013 &(lq_sta->lq_info[lq_sta->active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07001014 s32 active_sr = active_tbl->win[index].success_ratio;
Zhu Yib481de92007-09-25 17:54:57 -07001015 s32 active_tpt = active_tbl->expected_tpt[index];
Ben Cahill77626352007-11-29 11:09:44 +08001016
1017 /* expected "search" throughput */
1018 s32 *tpt_tbl = tbl->expected_tpt;
1019
1020 s32 new_rate, high, low, start_hi;
Zhu Yib481de92007-09-25 17:54:57 -07001021 u16 high_low;
Guy Cohenf935a6d2008-04-23 17:15:02 -07001022 s8 rate = index;
Zhu Yib481de92007-09-25 17:54:57 -07001023
1024 new_rate = high = low = start_hi = IWL_RATE_INVALID;
1025
1026 for (; ;) {
Ester Kummerbf403db2008-05-05 10:22:40 +08001027 high_low = rs_get_adjacent_rate(priv, rate, rate_mask,
1028 tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -07001029
1030 low = high_low & 0xff;
1031 high = (high_low >> 8) & 0xff;
1032
Ben Cahill77626352007-11-29 11:09:44 +08001033 /*
1034 * Lower the "search" bit rate, to give new "search" mode
1035 * approximately the same throughput as "active" if:
1036 *
1037 * 1) "Active" mode has been working modestly well (but not
1038 * great), and expected "search" throughput (under perfect
1039 * conditions) at candidate rate is above the actual
1040 * measured "active" throughput (but less than expected
1041 * "active" throughput under perfect conditions).
1042 * OR
1043 * 2) "Active" mode has been working perfectly or very well
1044 * and expected "search" throughput (under perfect
1045 * conditions) at candidate rate is above expected
1046 * "active" throughput (under perfect conditions).
1047 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001048 if ((((100 * tpt_tbl[rate]) > lq_sta->last_tpt) &&
Zhu Yib481de92007-09-25 17:54:57 -07001049 ((active_sr > IWL_RATE_DECREASE_TH) &&
1050 (active_sr <= IWL_RATE_HIGH_TH) &&
1051 (tpt_tbl[rate] <= active_tpt))) ||
1052 ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
1053 (tpt_tbl[rate] > active_tpt))) {
1054
Ben Cahill77626352007-11-29 11:09:44 +08001055 /* (2nd or later pass)
1056 * If we've already tried to raise the rate, and are
1057 * now trying to lower it, use the higher rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001058 if (start_hi != IWL_RATE_INVALID) {
1059 new_rate = start_hi;
1060 break;
1061 }
Ben Cahill77626352007-11-29 11:09:44 +08001062
Zhu Yib481de92007-09-25 17:54:57 -07001063 new_rate = rate;
Ben Cahill77626352007-11-29 11:09:44 +08001064
1065 /* Loop again with lower rate */
Zhu Yib481de92007-09-25 17:54:57 -07001066 if (low != IWL_RATE_INVALID)
1067 rate = low;
Ben Cahill77626352007-11-29 11:09:44 +08001068
1069 /* Lower rate not available, use the original */
Zhu Yib481de92007-09-25 17:54:57 -07001070 else
1071 break;
Ben Cahill77626352007-11-29 11:09:44 +08001072
1073 /* Else try to raise the "search" rate to match "active" */
Zhu Yib481de92007-09-25 17:54:57 -07001074 } else {
Ben Cahill77626352007-11-29 11:09:44 +08001075 /* (2nd or later pass)
1076 * If we've already tried to lower the rate, and are
1077 * now trying to raise it, use the lower rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001078 if (new_rate != IWL_RATE_INVALID)
1079 break;
Ben Cahill77626352007-11-29 11:09:44 +08001080
1081 /* Loop again with higher rate */
Zhu Yib481de92007-09-25 17:54:57 -07001082 else if (high != IWL_RATE_INVALID) {
1083 start_hi = high;
1084 rate = high;
Ben Cahill77626352007-11-29 11:09:44 +08001085
1086 /* Higher rate not available, use the original */
Zhu Yib481de92007-09-25 17:54:57 -07001087 } else {
Guy Cohen90d77952008-09-09 10:54:53 +08001088 new_rate = rate;
Zhu Yib481de92007-09-25 17:54:57 -07001089 break;
1090 }
1091 }
1092 }
1093
1094 return new_rate;
1095}
Zhu Yib481de92007-09-25 17:54:57 -07001096
Ben Cahill77626352007-11-29 11:09:44 +08001097/*
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001098 * Set up search table for MIMO2
Ben Cahill77626352007-11-29 11:09:44 +08001099 */
Guy Cohenfde0db32008-04-21 15:42:01 -07001100static int rs_switch_to_mimo2(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001101 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001102 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001103 struct ieee80211_sta *sta,
Tomas Winklere227cea2008-07-18 13:53:05 +08001104 struct iwl_scale_tbl_info *tbl, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001105{
Zhu Yib481de92007-09-25 17:54:57 -07001106 u16 rate_mask;
1107 s32 rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001108 s8 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07001109
Luis R. Rodriguezde27e642008-12-23 15:58:44 -08001110 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
Zhu Yib481de92007-09-25 17:54:57 -07001111 return -1;
1112
Johannes Bergd9fe60d2008-10-09 12:13:49 +02001113 if (((sta->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 2)
Tomas Winkler00c5ae22008-09-03 11:26:42 +08001114 == WLAN_HT_CAP_SM_PS_STATIC)
Zhu Yib481de92007-09-25 17:54:57 -07001115 return -1;
1116
Ben Cahill77626352007-11-29 11:09:44 +08001117 /* Need both Tx chains/antennas to support MIMO */
Guy Cohenaade00c2008-04-23 17:14:59 -07001118 if (priv->hw_params.tx_chains_num < 2)
Zhu Yib481de92007-09-25 17:54:57 -07001119 return -1;
1120
Tomas Winklere1623442009-01-27 14:27:56 -08001121 IWL_DEBUG_RATE(priv, "LQ: try to switch to MIMO2\n");
Guy Cohen39e88502008-04-23 17:14:57 -07001122
1123 tbl->lq_type = LQ_MIMO2;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001124 tbl->is_dup = lq_sta->is_dup;
Zhu Yib481de92007-09-25 17:54:57 -07001125 tbl->action = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001126 tbl->max_search = IWL_MAX_SEARCH;
Guy Cohen39e88502008-04-23 17:14:57 -07001127 rate_mask = lq_sta->active_mimo2_rate;
1128
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001129 if (iwl_is_ht40_tx_allowed(priv, &sta->ht_cap))
1130 tbl->is_ht40 = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001131 else
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001132 tbl->is_ht40 = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001133
Guy Coheneecd6e52008-04-23 17:14:58 -07001134 rs_set_expected_tpt_table(lq_sta, tbl);
Zhu Yib481de92007-09-25 17:54:57 -07001135
Guy Cohenf935a6d2008-04-23 17:15:02 -07001136 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
Zhu Yib481de92007-09-25 17:54:57 -07001137
Tomas Winklere1623442009-01-27 14:27:56 -08001138 IWL_DEBUG_RATE(priv, "LQ: MIMO2 best rate %d mask %X\n", rate, rate_mask);
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001139 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1140 IWL_DEBUG_RATE(priv, "Can't switch with index %d rate mask %x\n",
1141 rate, rate_mask);
1142 return -1;
1143 }
1144 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
Guy Cohen39e88502008-04-23 17:14:57 -07001145
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001146 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
1147 tbl->current_rate, is_green);
1148 return 0;
1149}
1150
1151/*
1152 * Set up search table for MIMO3
1153 */
1154static int rs_switch_to_mimo3(struct iwl_priv *priv,
1155 struct iwl_lq_sta *lq_sta,
1156 struct ieee80211_conf *conf,
1157 struct ieee80211_sta *sta,
1158 struct iwl_scale_tbl_info *tbl, int index)
1159{
1160 u16 rate_mask;
1161 s32 rate;
1162 s8 is_green = lq_sta->is_green;
1163
1164 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
1165 return -1;
1166
1167 if (((sta->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 2)
1168 == WLAN_HT_CAP_SM_PS_STATIC)
1169 return -1;
1170
1171 /* Need both Tx chains/antennas to support MIMO */
1172 if (priv->hw_params.tx_chains_num < 3)
1173 return -1;
1174
1175 IWL_DEBUG_RATE(priv, "LQ: try to switch to MIMO3\n");
1176
1177 tbl->lq_type = LQ_MIMO3;
1178 tbl->is_dup = lq_sta->is_dup;
1179 tbl->action = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001180 tbl->max_search = IWL_MAX_11N_MIMO3_SEARCH;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001181 rate_mask = lq_sta->active_mimo3_rate;
1182
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001183 if (iwl_is_ht40_tx_allowed(priv, &sta->ht_cap))
1184 tbl->is_ht40 = 1;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001185 else
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001186 tbl->is_ht40 = 0;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001187
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001188 rs_set_expected_tpt_table(lq_sta, tbl);
1189
1190 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
1191
1192 IWL_DEBUG_RATE(priv, "LQ: MIMO3 best rate %d mask %X\n",
1193 rate, rate_mask);
Guy Cohen39e88502008-04-23 17:14:57 -07001194 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
Tomas Winklere1623442009-01-27 14:27:56 -08001195 IWL_DEBUG_RATE(priv, "Can't switch with index %d rate mask %x\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001196 rate, rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07001197 return -1;
Guy Cohen39e88502008-04-23 17:14:57 -07001198 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001199 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
Zhu Yib481de92007-09-25 17:54:57 -07001200
Tomas Winklere1623442009-01-27 14:27:56 -08001201 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001202 tbl->current_rate, is_green);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001203 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07001204}
1205
Ben Cahill77626352007-11-29 11:09:44 +08001206/*
1207 * Set up search table for SISO
1208 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001209static int rs_switch_to_siso(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001210 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001211 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001212 struct ieee80211_sta *sta,
Tomas Winklere227cea2008-07-18 13:53:05 +08001213 struct iwl_scale_tbl_info *tbl, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001214{
Zhu Yib481de92007-09-25 17:54:57 -07001215 u16 rate_mask;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001216 u8 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07001217 s32 rate;
1218
Luis R. Rodriguezde27e642008-12-23 15:58:44 -08001219 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
Zhu Yib481de92007-09-25 17:54:57 -07001220 return -1;
1221
Tomas Winklere1623442009-01-27 14:27:56 -08001222 IWL_DEBUG_RATE(priv, "LQ: try to switch to SISO\n");
Guy Cohen39e88502008-04-23 17:14:57 -07001223
Tomas Winklerc33104f2008-01-14 17:46:21 -08001224 tbl->is_dup = lq_sta->is_dup;
Zhu Yib481de92007-09-25 17:54:57 -07001225 tbl->lq_type = LQ_SISO;
1226 tbl->action = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001227 tbl->max_search = IWL_MAX_SEARCH;
Guy Cohen39e88502008-04-23 17:14:57 -07001228 rate_mask = lq_sta->active_siso_rate;
Zhu Yib481de92007-09-25 17:54:57 -07001229
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001230 if (iwl_is_ht40_tx_allowed(priv, &sta->ht_cap))
1231 tbl->is_ht40 = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001232 else
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001233 tbl->is_ht40 = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001234
Zhu Yib481de92007-09-25 17:54:57 -07001235 if (is_green)
Guy Cohen39e88502008-04-23 17:14:57 -07001236 tbl->is_SGI = 0; /*11n spec: no SGI in SISO+Greenfield*/
Zhu Yib481de92007-09-25 17:54:57 -07001237
Guy Coheneecd6e52008-04-23 17:14:58 -07001238 rs_set_expected_tpt_table(lq_sta, tbl);
Guy Cohenf935a6d2008-04-23 17:15:02 -07001239 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
Zhu Yib481de92007-09-25 17:54:57 -07001240
Tomas Winklere1623442009-01-27 14:27:56 -08001241 IWL_DEBUG_RATE(priv, "LQ: get best rate %d mask %X\n", rate, rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07001242 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
Tomas Winklere1623442009-01-27 14:27:56 -08001243 IWL_DEBUG_RATE(priv, "can not switch with index %d rate mask %x\n",
Zhu Yib481de92007-09-25 17:54:57 -07001244 rate, rate_mask);
1245 return -1;
1246 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001247 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
Tomas Winklere1623442009-01-27 14:27:56 -08001248 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001249 tbl->current_rate, is_green);
Mohamed Abbas403ab562007-11-06 22:06:25 -08001250 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07001251}
1252
Ben Cahill77626352007-11-29 11:09:44 +08001253/*
1254 * Try to switch to new modulation mode from legacy
1255 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001256static int rs_move_legacy_other(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001257 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001258 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001259 struct ieee80211_sta *sta,
Zhu Yib481de92007-09-25 17:54:57 -07001260 int index)
1261{
Tomas Winklere227cea2008-07-18 13:53:05 +08001262 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1263 struct iwl_scale_tbl_info *search_tbl =
1264 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1265 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1266 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1267 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Wey-Yi Guyb1a78852010-07-31 08:52:03 -07001268 u8 start_action;
Guy Cohenfde0db32008-04-21 15:42:01 -07001269 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
Guy Cohen3110bef2008-09-09 10:54:54 +08001270 u8 tx_chains_num = priv->hw_params.tx_chains_num;
Guy Cohenfde0db32008-04-21 15:42:01 -07001271 int ret = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001272 u8 update_search_tbl_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001273
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001274 if (!iwl_ht_enabled(priv))
1275 /* stay in Legacy */
1276 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
Johannes Berg3ad3b922009-08-07 15:41:48 -07001277 else if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001278 tbl->action > IWL_LEGACY_SWITCH_SISO)
1279 tbl->action = IWL_LEGACY_SWITCH_SISO;
Wey-Yi Guyb1a78852010-07-31 08:52:03 -07001280 start_action = tbl->action;
Zhu Yib481de92007-09-25 17:54:57 -07001281 for (; ;) {
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001282 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001283 switch (tbl->action) {
Guy Cohen3110bef2008-09-09 10:54:54 +08001284 case IWL_LEGACY_SWITCH_ANTENNA1:
1285 case IWL_LEGACY_SWITCH_ANTENNA2:
Tomas Winklere1623442009-01-27 14:27:56 -08001286 IWL_DEBUG_RATE(priv, "LQ: Legacy toggle Antenna\n");
Zhu Yib481de92007-09-25 17:54:57 -07001287
Guy Cohen3110bef2008-09-09 10:54:54 +08001288 if ((tbl->action == IWL_LEGACY_SWITCH_ANTENNA1 &&
1289 tx_chains_num <= 1) ||
1290 (tbl->action == IWL_LEGACY_SWITCH_ANTENNA2 &&
1291 tx_chains_num <= 2))
1292 break;
1293
Ben Cahill77626352007-11-29 11:09:44 +08001294 /* Don't change antenna if success has been great */
Zhu Yib481de92007-09-25 17:54:57 -07001295 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1296 break;
Ben Cahill77626352007-11-29 11:09:44 +08001297
Ben Cahill77626352007-11-29 11:09:44 +08001298 /* Set up search table to try other antenna */
Zhu Yib481de92007-09-25 17:54:57 -07001299 memcpy(search_tbl, tbl, sz);
1300
Guy Cohenaade00c2008-04-23 17:14:59 -07001301 if (rs_toggle_antenna(valid_tx_ant,
1302 &search_tbl->current_rate, search_tbl)) {
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001303 update_search_tbl_counter = 1;
Guy Cohen3110bef2008-09-09 10:54:54 +08001304 rs_set_expected_tpt_table(lq_sta, search_tbl);
Guy Cohenaade00c2008-04-23 17:14:59 -07001305 goto out;
1306 }
Guy Cohena5e8b502008-05-29 16:35:11 +08001307 break;
Zhu Yib481de92007-09-25 17:54:57 -07001308 case IWL_LEGACY_SWITCH_SISO:
Tomas Winklere1623442009-01-27 14:27:56 -08001309 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to SISO\n");
Ben Cahill77626352007-11-29 11:09:44 +08001310
1311 /* Set up search table to try SISO */
Zhu Yib481de92007-09-25 17:54:57 -07001312 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001313 search_tbl->is_SGI = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001314 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001315 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001316 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001317 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001318 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001319 }
Zhu Yib481de92007-09-25 17:54:57 -07001320
1321 break;
Guy Cohen3110bef2008-09-09 10:54:54 +08001322 case IWL_LEGACY_SWITCH_MIMO2_AB:
1323 case IWL_LEGACY_SWITCH_MIMO2_AC:
1324 case IWL_LEGACY_SWITCH_MIMO2_BC:
Tomas Winklere1623442009-01-27 14:27:56 -08001325 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to MIMO2\n");
Ben Cahill77626352007-11-29 11:09:44 +08001326
1327 /* Set up search table to try MIMO */
Zhu Yib481de92007-09-25 17:54:57 -07001328 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001329 search_tbl->is_SGI = 0;
Guy Cohen3110bef2008-09-09 10:54:54 +08001330
1331 if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AB)
1332 search_tbl->ant_type = ANT_AB;
1333 else if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AC)
1334 search_tbl->ant_type = ANT_AC;
1335 else
1336 search_tbl->ant_type = ANT_BC;
1337
1338 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1339 break;
1340
Guy Cohenfde0db32008-04-21 15:42:01 -07001341 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001342 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001343 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001344 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001345 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001346 }
Zhu Yib481de92007-09-25 17:54:57 -07001347 break;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001348
1349 case IWL_LEGACY_SWITCH_MIMO3_ABC:
1350 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to MIMO3\n");
1351
1352 /* Set up search table to try MIMO3 */
1353 memcpy(search_tbl, tbl, sz);
1354 search_tbl->is_SGI = 0;
1355
1356 search_tbl->ant_type = ANT_ABC;
1357
1358 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1359 break;
1360
1361 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1362 search_tbl, index);
1363 if (!ret) {
1364 lq_sta->action_counter = 0;
1365 goto out;
1366 }
1367 break;
Zhu Yib481de92007-09-25 17:54:57 -07001368 }
1369 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001370 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001371 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
Zhu Yib481de92007-09-25 17:54:57 -07001372
1373 if (tbl->action == start_action)
1374 break;
1375
1376 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001377 search_tbl->lq_type = LQ_NONE;
Zhu Yib481de92007-09-25 17:54:57 -07001378 return 0;
1379
Guy Cohen3110bef2008-09-09 10:54:54 +08001380out:
1381 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001382 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001383 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001384 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001385 if (update_search_tbl_counter)
1386 search_tbl->action = tbl->action;
Zhu Yib481de92007-09-25 17:54:57 -07001387 return 0;
1388
1389}
1390
Ben Cahill77626352007-11-29 11:09:44 +08001391/*
1392 * Try to switch to new modulation mode from SISO
1393 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001394static int rs_move_siso_to_other(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001395 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001396 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001397 struct ieee80211_sta *sta, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001398{
Tomas Winklerc33104f2008-01-14 17:46:21 -08001399 u8 is_green = lq_sta->is_green;
Tomas Winklere227cea2008-07-18 13:53:05 +08001400 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1401 struct iwl_scale_tbl_info *search_tbl =
1402 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1403 struct iwl_rate_scale_data *window = &(tbl->win[index]);
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001404 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Tomas Winklere227cea2008-07-18 13:53:05 +08001405 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1406 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Wey-Yi Guyb1a78852010-07-31 08:52:03 -07001407 u8 start_action;
Guy Cohenfde0db32008-04-21 15:42:01 -07001408 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
Guy Cohen3110bef2008-09-09 10:54:54 +08001409 u8 tx_chains_num = priv->hw_params.tx_chains_num;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001410 u8 update_search_tbl_counter = 0;
Guy Cohenfde0db32008-04-21 15:42:01 -07001411 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001412
Johannes Berg3ad3b922009-08-07 15:41:48 -07001413 if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001414 tbl->action > IWL_SISO_SWITCH_ANTENNA2) {
1415 /* stay in SISO */
1416 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1417 }
Wey-Yi Guyb1a78852010-07-31 08:52:03 -07001418 start_action = tbl->action;
Zhu Yib481de92007-09-25 17:54:57 -07001419 for (;;) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001420 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001421 switch (tbl->action) {
Guy Cohen3110bef2008-09-09 10:54:54 +08001422 case IWL_SISO_SWITCH_ANTENNA1:
1423 case IWL_SISO_SWITCH_ANTENNA2:
Tomas Winklere1623442009-01-27 14:27:56 -08001424 IWL_DEBUG_RATE(priv, "LQ: SISO toggle Antenna\n");
Guy Cohen3110bef2008-09-09 10:54:54 +08001425
1426 if ((tbl->action == IWL_SISO_SWITCH_ANTENNA1 &&
1427 tx_chains_num <= 1) ||
1428 (tbl->action == IWL_SISO_SWITCH_ANTENNA2 &&
1429 tx_chains_num <= 2))
1430 break;
1431
Zhu Yib481de92007-09-25 17:54:57 -07001432 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1433 break;
Zhu Yib481de92007-09-25 17:54:57 -07001434
1435 memcpy(search_tbl, tbl, sz);
Guy Cohenaade00c2008-04-23 17:14:59 -07001436 if (rs_toggle_antenna(valid_tx_ant,
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001437 &search_tbl->current_rate, search_tbl)) {
1438 update_search_tbl_counter = 1;
Guy Cohenaade00c2008-04-23 17:14:59 -07001439 goto out;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001440 }
Guy Cohena5e8b502008-05-29 16:35:11 +08001441 break;
Guy Cohen3110bef2008-09-09 10:54:54 +08001442 case IWL_SISO_SWITCH_MIMO2_AB:
1443 case IWL_SISO_SWITCH_MIMO2_AC:
1444 case IWL_SISO_SWITCH_MIMO2_BC:
Tomas Winklere1623442009-01-27 14:27:56 -08001445 IWL_DEBUG_RATE(priv, "LQ: SISO switch to MIMO2\n");
Zhu Yib481de92007-09-25 17:54:57 -07001446 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001447 search_tbl->is_SGI = 0;
Guy Cohen3110bef2008-09-09 10:54:54 +08001448
1449 if (tbl->action == IWL_SISO_SWITCH_MIMO2_AB)
1450 search_tbl->ant_type = ANT_AB;
1451 else if (tbl->action == IWL_SISO_SWITCH_MIMO2_AC)
1452 search_tbl->ant_type = ANT_AC;
1453 else
1454 search_tbl->ant_type = ANT_BC;
1455
1456 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1457 break;
1458
Guy Cohenfde0db32008-04-21 15:42:01 -07001459 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001460 search_tbl, index);
Guy Cohen3110bef2008-09-09 10:54:54 +08001461 if (!ret)
Zhu Yib481de92007-09-25 17:54:57 -07001462 goto out;
1463 break;
1464 case IWL_SISO_SWITCH_GI:
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001465 if (!tbl->is_ht40 && !(ht_cap->cap &
1466 IEEE80211_HT_CAP_SGI_20))
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001467 break;
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001468 if (tbl->is_ht40 && !(ht_cap->cap &
1469 IEEE80211_HT_CAP_SGI_40))
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001470 break;
1471
Tomas Winklere1623442009-01-27 14:27:56 -08001472 IWL_DEBUG_RATE(priv, "LQ: SISO toggle SGI/NGI\n");
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001473
Zhu Yib481de92007-09-25 17:54:57 -07001474 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001475 if (is_green) {
1476 if (!tbl->is_SGI)
1477 break;
1478 else
Winkler, Tomas15b16872008-12-19 10:37:33 +08001479 IWL_ERR(priv,
1480 "SGI was set in GF+SISO\n");
Zhu Yib481de92007-09-25 17:54:57 -07001481 }
Guy Cohen39e88502008-04-23 17:14:57 -07001482 search_tbl->is_SGI = !tbl->is_SGI;
Guy Coheneecd6e52008-04-23 17:14:58 -07001483 rs_set_expected_tpt_table(lq_sta, search_tbl);
Guy Cohen39e88502008-04-23 17:14:57 -07001484 if (tbl->is_SGI) {
1485 s32 tpt = lq_sta->last_tpt / 100;
1486 if (tpt >= search_tbl->expected_tpt[index])
1487 break;
1488 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001489 search_tbl->current_rate =
1490 rate_n_flags_from_tbl(priv, search_tbl,
1491 index, is_green);
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001492 update_search_tbl_counter = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001493 goto out;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001494 case IWL_SISO_SWITCH_MIMO3_ABC:
1495 IWL_DEBUG_RATE(priv, "LQ: SISO switch to MIMO3\n");
1496 memcpy(search_tbl, tbl, sz);
1497 search_tbl->is_SGI = 0;
1498 search_tbl->ant_type = ANT_ABC;
1499
1500 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1501 break;
1502
1503 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1504 search_tbl, index);
1505 if (!ret)
1506 goto out;
1507 break;
Zhu Yib481de92007-09-25 17:54:57 -07001508 }
1509 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001510 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001511 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
Zhu Yib481de92007-09-25 17:54:57 -07001512
1513 if (tbl->action == start_action)
1514 break;
1515 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001516 search_tbl->lq_type = LQ_NONE;
Zhu Yib481de92007-09-25 17:54:57 -07001517 return 0;
1518
1519 out:
Guy Cohen3110bef2008-09-09 10:54:54 +08001520 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001521 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001522 if (tbl->action > IWL_SISO_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001523 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001524 if (update_search_tbl_counter)
1525 search_tbl->action = tbl->action;
1526
Zhu Yib481de92007-09-25 17:54:57 -07001527 return 0;
1528}
1529
Ben Cahill77626352007-11-29 11:09:44 +08001530/*
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001531 * Try to switch to new modulation mode from MIMO2
Ben Cahill77626352007-11-29 11:09:44 +08001532 */
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001533static int rs_move_mimo2_to_other(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001534 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001535 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001536 struct ieee80211_sta *sta, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001537{
Tomas Winklerc33104f2008-01-14 17:46:21 -08001538 s8 is_green = lq_sta->is_green;
Tomas Winklere227cea2008-07-18 13:53:05 +08001539 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1540 struct iwl_scale_tbl_info *search_tbl =
1541 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Guy Cohen3110bef2008-09-09 10:54:54 +08001542 struct iwl_rate_scale_data *window = &(tbl->win[index]);
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001543 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Tomas Winklere227cea2008-07-18 13:53:05 +08001544 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1545 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Wey-Yi Guyb1a78852010-07-31 08:52:03 -07001546 u8 start_action;
Guy Cohen3110bef2008-09-09 10:54:54 +08001547 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1548 u8 tx_chains_num = priv->hw_params.tx_chains_num;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001549 u8 update_search_tbl_counter = 0;
Guy Cohenaade00c2008-04-23 17:14:59 -07001550 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001551
Johannes Berg3ad3b922009-08-07 15:41:48 -07001552 if ((iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE) &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001553 (tbl->action < IWL_MIMO2_SWITCH_SISO_A ||
1554 tbl->action > IWL_MIMO2_SWITCH_SISO_C)) {
1555 /* switch in SISO */
1556 tbl->action = IWL_MIMO2_SWITCH_SISO_A;
1557 }
Wey-Yi Guyb1a78852010-07-31 08:52:03 -07001558 start_action = tbl->action;
Zhu Yib481de92007-09-25 17:54:57 -07001559 for (;;) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001560 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001561 switch (tbl->action) {
Guy Cohen3110bef2008-09-09 10:54:54 +08001562 case IWL_MIMO2_SWITCH_ANTENNA1:
1563 case IWL_MIMO2_SWITCH_ANTENNA2:
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001564 IWL_DEBUG_RATE(priv, "LQ: MIMO2 toggle Antennas\n");
Guy Cohen3110bef2008-09-09 10:54:54 +08001565
1566 if (tx_chains_num <= 2)
1567 break;
1568
1569 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1570 break;
1571
1572 memcpy(search_tbl, tbl, sz);
1573 if (rs_toggle_antenna(valid_tx_ant,
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001574 &search_tbl->current_rate, search_tbl)) {
1575 update_search_tbl_counter = 1;
Guy Cohen3110bef2008-09-09 10:54:54 +08001576 goto out;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001577 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001578 break;
1579 case IWL_MIMO2_SWITCH_SISO_A:
1580 case IWL_MIMO2_SWITCH_SISO_B:
1581 case IWL_MIMO2_SWITCH_SISO_C:
Tomas Winklere1623442009-01-27 14:27:56 -08001582 IWL_DEBUG_RATE(priv, "LQ: MIMO2 switch to SISO\n");
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001583
Ben Cahill77626352007-11-29 11:09:44 +08001584 /* Set up new search table for SISO */
Zhu Yib481de92007-09-25 17:54:57 -07001585 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001586
Guy Cohen3110bef2008-09-09 10:54:54 +08001587 if (tbl->action == IWL_MIMO2_SWITCH_SISO_A)
Guy Cohenfde0db32008-04-21 15:42:01 -07001588 search_tbl->ant_type = ANT_A;
Guy Cohen3110bef2008-09-09 10:54:54 +08001589 else if (tbl->action == IWL_MIMO2_SWITCH_SISO_B)
Guy Cohenfde0db32008-04-21 15:42:01 -07001590 search_tbl->ant_type = ANT_B;
Guy Cohen3110bef2008-09-09 10:54:54 +08001591 else
1592 search_tbl->ant_type = ANT_C;
1593
1594 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1595 break;
Zhu Yib481de92007-09-25 17:54:57 -07001596
Tomas Winklerc33104f2008-01-14 17:46:21 -08001597 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001598 search_tbl, index);
Guy Cohen3110bef2008-09-09 10:54:54 +08001599 if (!ret)
Zhu Yib481de92007-09-25 17:54:57 -07001600 goto out;
Guy Cohen3110bef2008-09-09 10:54:54 +08001601
Zhu Yib481de92007-09-25 17:54:57 -07001602 break;
1603
Guy Cohen3110bef2008-09-09 10:54:54 +08001604 case IWL_MIMO2_SWITCH_GI:
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001605 if (!tbl->is_ht40 && !(ht_cap->cap &
1606 IEEE80211_HT_CAP_SGI_20))
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001607 break;
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001608 if (tbl->is_ht40 && !(ht_cap->cap &
1609 IEEE80211_HT_CAP_SGI_40))
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001610 break;
1611
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001612 IWL_DEBUG_RATE(priv, "LQ: MIMO2 toggle SGI/NGI\n");
1613
1614 /* Set up new search table for MIMO2 */
1615 memcpy(search_tbl, tbl, sz);
1616 search_tbl->is_SGI = !tbl->is_SGI;
1617 rs_set_expected_tpt_table(lq_sta, search_tbl);
1618 /*
1619 * If active table already uses the fastest possible
1620 * modulation (dual stream with short guard interval),
1621 * and it's working well, there's no need to look
1622 * for a better type of modulation!
1623 */
1624 if (tbl->is_SGI) {
1625 s32 tpt = lq_sta->last_tpt / 100;
1626 if (tpt >= search_tbl->expected_tpt[index])
1627 break;
1628 }
1629 search_tbl->current_rate =
1630 rate_n_flags_from_tbl(priv, search_tbl,
1631 index, is_green);
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001632 update_search_tbl_counter = 1;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001633 goto out;
1634
1635 case IWL_MIMO2_SWITCH_MIMO3_ABC:
1636 IWL_DEBUG_RATE(priv, "LQ: MIMO2 switch to MIMO3\n");
1637 memcpy(search_tbl, tbl, sz);
1638 search_tbl->is_SGI = 0;
1639 search_tbl->ant_type = ANT_ABC;
1640
1641 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1642 break;
1643
1644 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1645 search_tbl, index);
1646 if (!ret)
1647 goto out;
1648
1649 break;
1650 }
1651 tbl->action++;
1652 if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
1653 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
1654
1655 if (tbl->action == start_action)
1656 break;
1657 }
1658 search_tbl->lq_type = LQ_NONE;
1659 return 0;
1660 out:
1661 lq_sta->search_better_tbl = 1;
1662 tbl->action++;
1663 if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
1664 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001665 if (update_search_tbl_counter)
1666 search_tbl->action = tbl->action;
1667
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001668 return 0;
1669
1670}
1671
1672/*
1673 * Try to switch to new modulation mode from MIMO3
1674 */
1675static int rs_move_mimo3_to_other(struct iwl_priv *priv,
1676 struct iwl_lq_sta *lq_sta,
1677 struct ieee80211_conf *conf,
1678 struct ieee80211_sta *sta, int index)
1679{
1680 s8 is_green = lq_sta->is_green;
1681 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1682 struct iwl_scale_tbl_info *search_tbl =
1683 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1684 struct iwl_rate_scale_data *window = &(tbl->win[index]);
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001685 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001686 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1687 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Wey-Yi Guyb1a78852010-07-31 08:52:03 -07001688 u8 start_action;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001689 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1690 u8 tx_chains_num = priv->hw_params.tx_chains_num;
1691 int ret;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001692 u8 update_search_tbl_counter = 0;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001693
Johannes Berg3ad3b922009-08-07 15:41:48 -07001694 if ((iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE) &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001695 (tbl->action < IWL_MIMO3_SWITCH_SISO_A ||
1696 tbl->action > IWL_MIMO3_SWITCH_SISO_C)) {
1697 /* switch in SISO */
1698 tbl->action = IWL_MIMO3_SWITCH_SISO_A;
1699 }
Wey-Yi Guyb1a78852010-07-31 08:52:03 -07001700 start_action = tbl->action;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001701 for (;;) {
1702 lq_sta->action_counter++;
1703 switch (tbl->action) {
1704 case IWL_MIMO3_SWITCH_ANTENNA1:
1705 case IWL_MIMO3_SWITCH_ANTENNA2:
1706 IWL_DEBUG_RATE(priv, "LQ: MIMO3 toggle Antennas\n");
1707
1708 if (tx_chains_num <= 3)
1709 break;
1710
1711 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1712 break;
1713
1714 memcpy(search_tbl, tbl, sz);
1715 if (rs_toggle_antenna(valid_tx_ant,
1716 &search_tbl->current_rate, search_tbl))
1717 goto out;
1718 break;
1719 case IWL_MIMO3_SWITCH_SISO_A:
1720 case IWL_MIMO3_SWITCH_SISO_B:
1721 case IWL_MIMO3_SWITCH_SISO_C:
1722 IWL_DEBUG_RATE(priv, "LQ: MIMO3 switch to SISO\n");
1723
1724 /* Set up new search table for SISO */
1725 memcpy(search_tbl, tbl, sz);
1726
1727 if (tbl->action == IWL_MIMO3_SWITCH_SISO_A)
1728 search_tbl->ant_type = ANT_A;
1729 else if (tbl->action == IWL_MIMO3_SWITCH_SISO_B)
1730 search_tbl->ant_type = ANT_B;
1731 else
1732 search_tbl->ant_type = ANT_C;
1733
1734 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1735 break;
1736
1737 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
1738 search_tbl, index);
1739 if (!ret)
1740 goto out;
1741
1742 break;
1743
1744 case IWL_MIMO3_SWITCH_MIMO2_AB:
1745 case IWL_MIMO3_SWITCH_MIMO2_AC:
1746 case IWL_MIMO3_SWITCH_MIMO2_BC:
1747 IWL_DEBUG_RATE(priv, "LQ: MIMO3 switch to MIMO2\n");
1748
1749 memcpy(search_tbl, tbl, sz);
1750 search_tbl->is_SGI = 0;
1751 if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AB)
1752 search_tbl->ant_type = ANT_AB;
1753 else if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AC)
1754 search_tbl->ant_type = ANT_AC;
1755 else
1756 search_tbl->ant_type = ANT_BC;
1757
1758 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1759 break;
1760
1761 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
1762 search_tbl, index);
1763 if (!ret)
1764 goto out;
1765
1766 break;
1767
1768 case IWL_MIMO3_SWITCH_GI:
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001769 if (!tbl->is_ht40 && !(ht_cap->cap &
1770 IEEE80211_HT_CAP_SGI_20))
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001771 break;
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001772 if (tbl->is_ht40 && !(ht_cap->cap &
1773 IEEE80211_HT_CAP_SGI_40))
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001774 break;
1775
1776 IWL_DEBUG_RATE(priv, "LQ: MIMO3 toggle SGI/NGI\n");
Ben Cahill77626352007-11-29 11:09:44 +08001777
1778 /* Set up new search table for MIMO */
Zhu Yib481de92007-09-25 17:54:57 -07001779 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001780 search_tbl->is_SGI = !tbl->is_SGI;
Guy Coheneecd6e52008-04-23 17:14:58 -07001781 rs_set_expected_tpt_table(lq_sta, search_tbl);
Ben Cahill77626352007-11-29 11:09:44 +08001782 /*
1783 * If active table already uses the fastest possible
1784 * modulation (dual stream with short guard interval),
1785 * and it's working well, there's no need to look
1786 * for a better type of modulation!
1787 */
Guy Cohen39e88502008-04-23 17:14:57 -07001788 if (tbl->is_SGI) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001789 s32 tpt = lq_sta->last_tpt / 100;
Guy Cohen39e88502008-04-23 17:14:57 -07001790 if (tpt >= search_tbl->expected_tpt[index])
1791 break;
Zhu Yib481de92007-09-25 17:54:57 -07001792 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001793 search_tbl->current_rate =
1794 rate_n_flags_from_tbl(priv, search_tbl,
1795 index, is_green);
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001796 update_search_tbl_counter = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001797 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07001798 }
1799 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001800 if (tbl->action > IWL_MIMO3_SWITCH_GI)
1801 tbl->action = IWL_MIMO3_SWITCH_ANTENNA1;
Zhu Yib481de92007-09-25 17:54:57 -07001802
1803 if (tbl->action == start_action)
1804 break;
1805 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001806 search_tbl->lq_type = LQ_NONE;
Zhu Yib481de92007-09-25 17:54:57 -07001807 return 0;
1808 out:
Guy Cohen3110bef2008-09-09 10:54:54 +08001809 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001810 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001811 if (tbl->action > IWL_MIMO3_SWITCH_GI)
1812 tbl->action = IWL_MIMO3_SWITCH_ANTENNA1;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001813 if (update_search_tbl_counter)
1814 search_tbl->action = tbl->action;
1815
Zhu Yib481de92007-09-25 17:54:57 -07001816 return 0;
1817
1818}
1819
Ben Cahill77626352007-11-29 11:09:44 +08001820/*
1821 * Check whether we should continue using same modulation mode, or
1822 * begin search for a new mode, based on:
1823 * 1) # tx successes or failures while using this mode
1824 * 2) # times calling this function
1825 * 3) elapsed time in this mode (not used, for now)
1826 */
Tomas Winklere227cea2008-07-18 13:53:05 +08001827static void rs_stay_in_table(struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07001828{
Tomas Winklere227cea2008-07-18 13:53:05 +08001829 struct iwl_scale_tbl_info *tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001830 int i;
1831 int active_tbl;
1832 int flush_interval_passed = 0;
Ester Kummerbf403db2008-05-05 10:22:40 +08001833 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07001834
Ester Kummerbf403db2008-05-05 10:22:40 +08001835 priv = lq_sta->drv;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001836 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001837
Tomas Winklerc33104f2008-01-14 17:46:21 -08001838 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07001839
Ben Cahill77626352007-11-29 11:09:44 +08001840 /* If we've been disallowing search, see if we should now allow it */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001841 if (lq_sta->stay_in_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07001842
Ben Cahill77626352007-11-29 11:09:44 +08001843 /* Elapsed time using current modulation mode */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001844 if (lq_sta->flush_timer)
Zhu Yib481de92007-09-25 17:54:57 -07001845 flush_interval_passed =
Mohamed Abbas447fee72009-04-20 14:37:02 -07001846 time_after(jiffies,
1847 (unsigned long)(lq_sta->flush_timer +
Zhu Yib481de92007-09-25 17:54:57 -07001848 IWL_RATE_SCALE_FLUSH_INTVL));
1849
Ben Cahill77626352007-11-29 11:09:44 +08001850 /*
1851 * Check if we should allow search for new modulation mode.
1852 * If many frames have failed or succeeded, or we've used
1853 * this same modulation for a long time, allow search, and
1854 * reset history stats that keep track of whether we should
1855 * allow a new search. Also (below) reset all bitmaps and
1856 * stats in active history.
1857 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001858 if ((lq_sta->total_failed > lq_sta->max_failure_limit) ||
1859 (lq_sta->total_success > lq_sta->max_success_limit) ||
1860 ((!lq_sta->search_better_tbl) && (lq_sta->flush_timer)
Zhu Yib481de92007-09-25 17:54:57 -07001861 && (flush_interval_passed))) {
Tomas Winklere1623442009-01-27 14:27:56 -08001862 IWL_DEBUG_RATE(priv, "LQ: stay is expired %d %d %d\n:",
Tomas Winklerc33104f2008-01-14 17:46:21 -08001863 lq_sta->total_failed,
1864 lq_sta->total_success,
Zhu Yib481de92007-09-25 17:54:57 -07001865 flush_interval_passed);
Ben Cahill77626352007-11-29 11:09:44 +08001866
1867 /* Allow search for new mode */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001868 lq_sta->stay_in_tbl = 0; /* only place reset */
1869 lq_sta->total_failed = 0;
1870 lq_sta->total_success = 0;
1871 lq_sta->flush_timer = 0;
Ben Cahill77626352007-11-29 11:09:44 +08001872
1873 /*
1874 * Else if we've used this modulation mode enough repetitions
1875 * (regardless of elapsed time or success/failure), reset
1876 * history bitmaps and rate-specific stats for all rates in
1877 * active table.
1878 */
Mohamed Abbas403ab562007-11-06 22:06:25 -08001879 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001880 lq_sta->table_count++;
1881 if (lq_sta->table_count >=
1882 lq_sta->table_count_limit) {
1883 lq_sta->table_count = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001884
Tomas Winklere1623442009-01-27 14:27:56 -08001885 IWL_DEBUG_RATE(priv, "LQ: stay in table clear win\n");
Zhu Yib481de92007-09-25 17:54:57 -07001886 for (i = 0; i < IWL_RATE_COUNT; i++)
1887 rs_rate_scale_clear_window(
1888 &(tbl->win[i]));
1889 }
1890 }
1891
Ben Cahill77626352007-11-29 11:09:44 +08001892 /* If transitioning to allow "search", reset all history
1893 * bitmaps and stats in active table (this will become the new
1894 * "search" table). */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001895 if (!lq_sta->stay_in_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07001896 for (i = 0; i < IWL_RATE_COUNT; i++)
1897 rs_rate_scale_clear_window(&(tbl->win[i]));
1898 }
1899 }
1900}
1901
Ben Cahill77626352007-11-29 11:09:44 +08001902/*
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07001903 * setup rate table in uCode
1904 * return rate_n_flags as used in the table
1905 */
1906static u32 rs_update_rate_tbl(struct iwl_priv *priv,
1907 struct iwl_lq_sta *lq_sta,
1908 struct iwl_scale_tbl_info *tbl,
1909 int index, u8 is_green)
1910{
1911 u32 rate;
1912
1913 /* Update uCode's rate table. */
1914 rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
1915 rs_fill_link_cmd(priv, lq_sta, rate);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001916 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC, false);
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07001917
1918 return rate;
1919}
1920
1921/*
Ben Cahill77626352007-11-29 11:09:44 +08001922 * Do rate scaling and search for new modulation mode.
1923 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001924static void rs_rate_scale_perform(struct iwl_priv *priv,
Gábor Stefanik514d65c2009-04-23 19:36:08 +02001925 struct sk_buff *skb,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001926 struct ieee80211_sta *sta,
1927 struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07001928{
Johannes Berg4b7679a2008-09-18 18:14:18 +02001929 struct ieee80211_hw *hw = priv->hw;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001930 struct ieee80211_conf *conf = &hw->conf;
Gábor Stefanik514d65c2009-04-23 19:36:08 +02001931 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1932 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001933 int low = IWL_RATE_INVALID;
1934 int high = IWL_RATE_INVALID;
1935 int index;
1936 int i;
Tomas Winklere227cea2008-07-18 13:53:05 +08001937 struct iwl_rate_scale_data *window = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001938 int current_tpt = IWL_INVALID_VALUE;
1939 int low_tpt = IWL_INVALID_VALUE;
1940 int high_tpt = IWL_INVALID_VALUE;
1941 u32 fail_count;
1942 s8 scale_action = 0;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07001943 u16 rate_mask;
Zhu Yib481de92007-09-25 17:54:57 -07001944 u8 update_lq = 0;
Tomas Winklere227cea2008-07-18 13:53:05 +08001945 struct iwl_scale_tbl_info *tbl, *tbl1;
Zhu Yib481de92007-09-25 17:54:57 -07001946 u16 rate_scale_index_msk = 0;
Guy Cohen39e88502008-04-23 17:14:57 -07001947 u32 rate;
Zhu Yib481de92007-09-25 17:54:57 -07001948 u8 is_green = 0;
1949 u8 active_tbl = 0;
1950 u8 done_search = 0;
1951 u16 high_low;
Guy Cohena5e8b502008-05-29 16:35:11 +08001952 s32 sr;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001953 u8 tid = MAX_TID_COUNT;
Wey-Yi Guy86b47662009-03-10 14:35:09 -07001954 struct iwl_tid_data *tid_data;
Zhu Yib481de92007-09-25 17:54:57 -07001955
Tomas Winklere1623442009-01-27 14:27:56 -08001956 IWL_DEBUG_RATE(priv, "rate scale calculate new rate for skb\n");
Zhu Yib481de92007-09-25 17:54:57 -07001957
Gábor Stefanik514d65c2009-04-23 19:36:08 +02001958 /* Send management frames and NO_ACK data using lowest rate. */
Tomas Winkler417f1142008-11-12 13:14:06 -08001959 /* TODO: this could probably be improved.. */
1960 if (!ieee80211_is_data(hdr->frame_control) ||
Gábor Stefanik514d65c2009-04-23 19:36:08 +02001961 info->flags & IEEE80211_TX_CTL_NO_ACK)
Zhu Yib481de92007-09-25 17:54:57 -07001962 return;
Zhu Yib481de92007-09-25 17:54:57 -07001963
Johannes Berg4b7679a2008-09-18 18:14:18 +02001964 if (!sta || !lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07001965 return;
1966
Johannes Berg4b7679a2008-09-18 18:14:18 +02001967 lq_sta->supp_rates = sta->supp_rates[lq_sta->band];
Zhu Yib481de92007-09-25 17:54:57 -07001968
Ron Rindjunskyfaa29712008-06-12 09:46:58 +08001969 tid = rs_tl_add_packet(lq_sta, hdr);
Daniel C Halperine3949d62009-09-17 10:43:50 -07001970 if ((tid != MAX_TID_COUNT) && (lq_sta->tx_agg_tid_en & (1 << tid))) {
1971 tid_data = &priv->stations[lq_sta->lq.sta_id].tid[tid];
1972 if (tid_data->agg.state == IWL_AGG_OFF)
1973 lq_sta->is_agg = 0;
1974 else
1975 lq_sta->is_agg = 1;
1976 } else
1977 lq_sta->is_agg = 0;
Ron Rindjunskyfaa29712008-06-12 09:46:58 +08001978
Ben Cahill77626352007-11-29 11:09:44 +08001979 /*
1980 * Select rate-scale / modulation-mode table to work with in
1981 * the rest of this function: "search" if searching for better
1982 * modulation mode, or "active" if doing rate scaling within a mode.
1983 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001984 if (!lq_sta->search_better_tbl)
1985 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001986 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08001987 active_tbl = 1 - lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001988
Tomas Winklerc33104f2008-01-14 17:46:21 -08001989 tbl = &(lq_sta->lq_info[active_tbl]);
Wey-Yi Guy2681b202009-05-21 13:44:22 -07001990 if (is_legacy(tbl->lq_type))
1991 lq_sta->is_green = 0;
1992 else
Daniel C Halperinb2617932009-08-13 13:30:59 -07001993 lq_sta->is_green = rs_use_green(sta, &priv->current_ht_config);
Tomas Winklerc33104f2008-01-14 17:46:21 -08001994 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07001995
Ben Cahill77626352007-11-29 11:09:44 +08001996 /* current tx rate */
Johannes Bergb7e35002008-09-11 02:22:58 +02001997 index = lq_sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07001998
Tomas Winklere1623442009-01-27 14:27:56 -08001999 IWL_DEBUG_RATE(priv, "Rate scale index %d for type %d\n", index,
Zhu Yib481de92007-09-25 17:54:57 -07002000 tbl->lq_type);
2001
Ben Cahill77626352007-11-29 11:09:44 +08002002 /* rates available for this association, and for modulation mode */
Guy Coheneecd6e52008-04-23 17:14:58 -07002003 rate_mask = rs_get_supported_rates(lq_sta, hdr, tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -07002004
Frans Pop91dd6c22010-03-24 14:19:58 -07002005 IWL_DEBUG_RATE(priv, "mask 0x%04X\n", rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07002006
2007 /* mask with station rate restriction */
2008 if (is_legacy(tbl->lq_type)) {
Johannes Berg8318d782008-01-24 19:38:38 +01002009 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Ben Cahill77626352007-11-29 11:09:44 +08002010 /* supp_rates has no CCK bits in A mode */
Zhu Yib481de92007-09-25 17:54:57 -07002011 rate_scale_index_msk = (u16) (rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -08002012 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
Zhu Yib481de92007-09-25 17:54:57 -07002013 else
2014 rate_scale_index_msk = (u16) (rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -08002015 lq_sta->supp_rates);
Zhu Yib481de92007-09-25 17:54:57 -07002016
2017 } else
2018 rate_scale_index_msk = rate_mask;
2019
2020 if (!rate_scale_index_msk)
2021 rate_scale_index_msk = rate_mask;
2022
Guy Cohen07bc28e2008-04-23 17:15:03 -07002023 if (!((1 << index) & rate_scale_index_msk)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002024 IWL_ERR(priv, "Current Rate is not valid\n");
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07002025 if (lq_sta->search_better_tbl) {
2026 /* revert to active table if search table is not valid*/
2027 tbl->lq_type = LQ_NONE;
2028 lq_sta->search_better_tbl = 0;
2029 tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
2030 /* get "active" rate info */
2031 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
2032 rate = rs_update_rate_tbl(priv, lq_sta,
2033 tbl, index, is_green);
2034 }
Guy Cohen07bc28e2008-04-23 17:15:03 -07002035 return;
Zhu Yib481de92007-09-25 17:54:57 -07002036 }
2037
Ben Cahill77626352007-11-29 11:09:44 +08002038 /* Get expected throughput table and history window for current rate */
Guy Cohen07bc28e2008-04-23 17:15:03 -07002039 if (!tbl->expected_tpt) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002040 IWL_ERR(priv, "tbl->expected_tpt is NULL\n");
Guy Cohen07bc28e2008-04-23 17:15:03 -07002041 return;
2042 }
Zhu Yib481de92007-09-25 17:54:57 -07002043
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002044 /* force user max rate if set by user */
2045 if ((lq_sta->max_rate_idx != -1) &&
2046 (lq_sta->max_rate_idx < index)) {
2047 index = lq_sta->max_rate_idx;
2048 update_lq = 1;
2049 window = &(tbl->win[index]);
2050 goto lq_update;
2051 }
2052
Zhu Yib481de92007-09-25 17:54:57 -07002053 window = &(tbl->win[index]);
2054
Ben Cahill77626352007-11-29 11:09:44 +08002055 /*
2056 * If there is not enough history to calculate actual average
2057 * throughput, keep analyzing results of more tx frames, without
2058 * changing rate or mode (bypass most of the rest of this function).
2059 * Set up new rate table in uCode only if old rate is not supported
2060 * in current association (use new rate found above).
2061 */
Zhu Yib481de92007-09-25 17:54:57 -07002062 fail_count = window->counter - window->success_counter;
Guy Cohen07bc28e2008-04-23 17:15:03 -07002063 if ((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
2064 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002065 IWL_DEBUG_RATE(priv, "LQ: still below TH. succ=%d total=%d "
Zhu Yib481de92007-09-25 17:54:57 -07002066 "for index %d\n",
2067 window->success_counter, window->counter, index);
Ben Cahill77626352007-11-29 11:09:44 +08002068
2069 /* Can't calculate this yet; not enough history */
Zhu Yib481de92007-09-25 17:54:57 -07002070 window->average_tpt = IWL_INVALID_VALUE;
Ben Cahill77626352007-11-29 11:09:44 +08002071
2072 /* Should we stay with this modulation mode,
2073 * or search for a new one? */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002074 rs_stay_in_table(lq_sta);
Ben Cahill77626352007-11-29 11:09:44 +08002075
Zhu Yib481de92007-09-25 17:54:57 -07002076 goto out;
Guy Cohen3110bef2008-09-09 10:54:54 +08002077 }
Ben Cahill77626352007-11-29 11:09:44 +08002078 /* Else we have enough samples; calculate estimate of
2079 * actual average throughput */
Reinette Chatre3d79b2a2010-05-03 10:55:07 -07002080 if (window->average_tpt != ((window->success_ratio *
2081 tbl->expected_tpt[index] + 64) / 128)) {
2082 IWL_ERR(priv, "expected_tpt should have been calculated by now\n");
2083 window->average_tpt = ((window->success_ratio *
2084 tbl->expected_tpt[index] + 64) / 128);
2085 }
Zhu Yib481de92007-09-25 17:54:57 -07002086
Ben Cahill77626352007-11-29 11:09:44 +08002087 /* If we are searching for better modulation mode, check success. */
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002088 if (lq_sta->search_better_tbl &&
Johannes Berg3ad3b922009-08-07 15:41:48 -07002089 (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_MULTI)) {
Ben Cahill77626352007-11-29 11:09:44 +08002090 /* If good success, continue using the "search" mode;
2091 * no need to send new link quality command, since we're
2092 * continuing to use the setup that we've been trying. */
Guy Cohen07bc28e2008-04-23 17:15:03 -07002093 if (window->average_tpt > lq_sta->last_tpt) {
2094
Tomas Winklere1623442009-01-27 14:27:56 -08002095 IWL_DEBUG_RATE(priv, "LQ: SWITCHING TO NEW TABLE "
Guy Cohen07bc28e2008-04-23 17:15:03 -07002096 "suc=%d cur-tpt=%d old-tpt=%d\n",
2097 window->success_ratio,
2098 window->average_tpt,
2099 lq_sta->last_tpt);
2100
2101 if (!is_legacy(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08002102 lq_sta->enable_counter = 1;
Guy Cohen07bc28e2008-04-23 17:15:03 -07002103
Ben Cahill77626352007-11-29 11:09:44 +08002104 /* Swap tables; "search" becomes "active" */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002105 lq_sta->active_tbl = active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002106 current_tpt = window->average_tpt;
Ben Cahill77626352007-11-29 11:09:44 +08002107
2108 /* Else poor success; go back to mode in "active" table */
Zhu Yib481de92007-09-25 17:54:57 -07002109 } else {
Guy Cohen07bc28e2008-04-23 17:15:03 -07002110
Tomas Winklere1623442009-01-27 14:27:56 -08002111 IWL_DEBUG_RATE(priv, "LQ: GOING BACK TO THE OLD TABLE "
Guy Cohen07bc28e2008-04-23 17:15:03 -07002112 "suc=%d cur-tpt=%d old-tpt=%d\n",
2113 window->success_ratio,
2114 window->average_tpt,
2115 lq_sta->last_tpt);
2116
Ben Cahill77626352007-11-29 11:09:44 +08002117 /* Nullify "search" table */
Zhu Yib481de92007-09-25 17:54:57 -07002118 tbl->lq_type = LQ_NONE;
Ben Cahill77626352007-11-29 11:09:44 +08002119
2120 /* Revert to "active" table */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002121 active_tbl = lq_sta->active_tbl;
2122 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07002123
Ben Cahill77626352007-11-29 11:09:44 +08002124 /* Revert to "active" rate and throughput info */
Tomas Winklere7d326ac2008-06-12 09:47:11 +08002125 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
Tomas Winklerc33104f2008-01-14 17:46:21 -08002126 current_tpt = lq_sta->last_tpt;
Ben Cahill77626352007-11-29 11:09:44 +08002127
2128 /* Need to set up a new rate table in uCode */
2129 update_lq = 1;
Zhu Yib481de92007-09-25 17:54:57 -07002130 }
Ben Cahill77626352007-11-29 11:09:44 +08002131
2132 /* Either way, we've made a decision; modulation mode
2133 * search is done, allow rate adjustment next time. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002134 lq_sta->search_better_tbl = 0;
Ben Cahill77626352007-11-29 11:09:44 +08002135 done_search = 1; /* Don't switch modes below! */
Zhu Yib481de92007-09-25 17:54:57 -07002136 goto lq_update;
2137 }
2138
Ben Cahill77626352007-11-29 11:09:44 +08002139 /* (Else) not in search of better modulation mode, try for better
2140 * starting rate, while staying in this mode. */
Ester Kummerbf403db2008-05-05 10:22:40 +08002141 high_low = rs_get_adjacent_rate(priv, index, rate_scale_index_msk,
Zhu Yib481de92007-09-25 17:54:57 -07002142 tbl->lq_type);
2143 low = high_low & 0xff;
2144 high = (high_low >> 8) & 0xff;
2145
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002146 /* If user set max rate, dont allow higher than user constrain */
2147 if ((lq_sta->max_rate_idx != -1) &&
2148 (lq_sta->max_rate_idx < high))
2149 high = IWL_RATE_INVALID;
2150
Guy Cohena5e8b502008-05-29 16:35:11 +08002151 sr = window->success_ratio;
2152
Ben Cahill77626352007-11-29 11:09:44 +08002153 /* Collect measured throughputs for current and adjacent rates */
Zhu Yib481de92007-09-25 17:54:57 -07002154 current_tpt = window->average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07002155 if (low != IWL_RATE_INVALID)
2156 low_tpt = tbl->win[low].average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07002157 if (high != IWL_RATE_INVALID)
2158 high_tpt = tbl->win[high].average_tpt;
2159
Guy Cohena5e8b502008-05-29 16:35:11 +08002160 scale_action = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002161
Ben Cahill77626352007-11-29 11:09:44 +08002162 /* Too many failures, decrease rate */
Guy Cohena5e8b502008-05-29 16:35:11 +08002163 if ((sr <= IWL_RATE_DECREASE_TH) || (current_tpt == 0)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002164 IWL_DEBUG_RATE(priv, "decrease rate because of low success_ratio\n");
Zhu Yib481de92007-09-25 17:54:57 -07002165 scale_action = -1;
Ben Cahill77626352007-11-29 11:09:44 +08002166
2167 /* No throughput measured yet for adjacent rates; try increase. */
Zhu Yib481de92007-09-25 17:54:57 -07002168 } else if ((low_tpt == IWL_INVALID_VALUE) &&
Guy Cohena5e8b502008-05-29 16:35:11 +08002169 (high_tpt == IWL_INVALID_VALUE)) {
2170
2171 if (high != IWL_RATE_INVALID && sr >= IWL_RATE_INCREASE_TH)
2172 scale_action = 1;
2173 else if (low != IWL_RATE_INVALID)
Wey-Yi Guy8fe72312009-03-10 14:35:10 -07002174 scale_action = 0;
Guy Cohena5e8b502008-05-29 16:35:11 +08002175 }
Ben Cahill77626352007-11-29 11:09:44 +08002176
2177 /* Both adjacent throughputs are measured, but neither one has better
2178 * throughput; we're using the best rate, don't change it! */
Zhu Yib481de92007-09-25 17:54:57 -07002179 else if ((low_tpt != IWL_INVALID_VALUE) &&
2180 (high_tpt != IWL_INVALID_VALUE) &&
2181 (low_tpt < current_tpt) &&
2182 (high_tpt < current_tpt))
2183 scale_action = 0;
Ben Cahill77626352007-11-29 11:09:44 +08002184
2185 /* At least one adjacent rate's throughput is measured,
2186 * and may have better performance. */
Zhu Yib481de92007-09-25 17:54:57 -07002187 else {
Ben Cahill77626352007-11-29 11:09:44 +08002188 /* Higher adjacent rate's throughput is measured */
Zhu Yib481de92007-09-25 17:54:57 -07002189 if (high_tpt != IWL_INVALID_VALUE) {
Ben Cahill77626352007-11-29 11:09:44 +08002190 /* Higher rate has better throughput */
Guy Cohena5e8b502008-05-29 16:35:11 +08002191 if (high_tpt > current_tpt &&
2192 sr >= IWL_RATE_INCREASE_TH) {
Zhu Yib481de92007-09-25 17:54:57 -07002193 scale_action = 1;
Guy Cohena5e8b502008-05-29 16:35:11 +08002194 } else {
Wey-Yi Guy8fe72312009-03-10 14:35:10 -07002195 scale_action = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002196 }
Ben Cahill77626352007-11-29 11:09:44 +08002197
2198 /* Lower adjacent rate's throughput is measured */
Zhu Yib481de92007-09-25 17:54:57 -07002199 } else if (low_tpt != IWL_INVALID_VALUE) {
Ben Cahill77626352007-11-29 11:09:44 +08002200 /* Lower rate has better throughput */
Zhu Yib481de92007-09-25 17:54:57 -07002201 if (low_tpt > current_tpt) {
Tomas Winklere1623442009-01-27 14:27:56 -08002202 IWL_DEBUG_RATE(priv,
2203 "decrease rate because of low tpt\n");
Zhu Yib481de92007-09-25 17:54:57 -07002204 scale_action = -1;
Guy Cohena5e8b502008-05-29 16:35:11 +08002205 } else if (sr >= IWL_RATE_INCREASE_TH) {
Zhu Yib481de92007-09-25 17:54:57 -07002206 scale_action = 1;
Guy Cohena5e8b502008-05-29 16:35:11 +08002207 }
Zhu Yib481de92007-09-25 17:54:57 -07002208 }
2209 }
2210
Ben Cahill77626352007-11-29 11:09:44 +08002211 /* Sanity check; asked for decrease, but success rate or throughput
2212 * has been good at old rate. Don't change it. */
Guy Cohena5e8b502008-05-29 16:35:11 +08002213 if ((scale_action == -1) && (low != IWL_RATE_INVALID) &&
2214 ((sr > IWL_RATE_HIGH_TH) ||
Zhu Yib481de92007-09-25 17:54:57 -07002215 (current_tpt > (100 * tbl->expected_tpt[low]))))
Zhu Yib481de92007-09-25 17:54:57 -07002216 scale_action = 0;
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002217 if (!iwl_ht_enabled(priv) && !is_legacy(tbl->lq_type))
2218 scale_action = -1;
Johannes Berg3ad3b922009-08-07 15:41:48 -07002219 if (iwl_tx_ant_restriction(priv) != IWL_ANT_OK_MULTI &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002220 (is_mimo2(tbl->lq_type) || is_mimo3(tbl->lq_type)))
2221 scale_action = -1;
Zhu Yib481de92007-09-25 17:54:57 -07002222 switch (scale_action) {
2223 case -1:
Ben Cahill77626352007-11-29 11:09:44 +08002224 /* Decrease starting rate, update uCode's rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002225 if (low != IWL_RATE_INVALID) {
2226 update_lq = 1;
2227 index = low;
2228 }
Mohamed Abbas447fee72009-04-20 14:37:02 -07002229
Zhu Yib481de92007-09-25 17:54:57 -07002230 break;
2231 case 1:
Ben Cahill77626352007-11-29 11:09:44 +08002232 /* Increase starting rate, update uCode's rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002233 if (high != IWL_RATE_INVALID) {
2234 update_lq = 1;
2235 index = high;
2236 }
2237
2238 break;
2239 case 0:
Ben Cahill77626352007-11-29 11:09:44 +08002240 /* No change */
Zhu Yib481de92007-09-25 17:54:57 -07002241 default:
2242 break;
2243 }
2244
Tomas Winklere1623442009-01-27 14:27:56 -08002245 IWL_DEBUG_RATE(priv, "choose rate scale index %d action %d low %d "
Zhu Yib481de92007-09-25 17:54:57 -07002246 "high %d type %d\n",
2247 index, scale_action, low, high, tbl->lq_type);
2248
Guy Cohena5e8b502008-05-29 16:35:11 +08002249lq_update:
Ben Cahill77626352007-11-29 11:09:44 +08002250 /* Replace uCode's rate table for the destination station. */
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07002251 if (update_lq)
2252 rate = rs_update_rate_tbl(priv, lq_sta,
2253 tbl, index, is_green);
Ben Cahill77626352007-11-29 11:09:44 +08002254
Johannes Berg3ad3b922009-08-07 15:41:48 -07002255 if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_MULTI) {
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002256 /* Should we stay with this modulation mode,
2257 * or search for a new one? */
2258 rs_stay_in_table(lq_sta);
2259 }
Ben Cahill77626352007-11-29 11:09:44 +08002260 /*
2261 * Search for new modulation mode if we're:
2262 * 1) Not changing rates right now
2263 * 2) Not just finishing up a search
2264 * 3) Allowing a new search
2265 */
Guy Cohen47cfd462008-05-27 11:29:34 +08002266 if (!update_lq && !done_search && !lq_sta->stay_in_tbl && window->counter) {
Ben Cahill77626352007-11-29 11:09:44 +08002267 /* Save current throughput to compare with "search" throughput*/
Tomas Winklerc33104f2008-01-14 17:46:21 -08002268 lq_sta->last_tpt = current_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07002269
Ben Cahill77626352007-11-29 11:09:44 +08002270 /* Select a new "search" modulation mode to try.
2271 * If one is found, set up the new "search" table. */
Zhu Yib481de92007-09-25 17:54:57 -07002272 if (is_legacy(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08002273 rs_move_legacy_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07002274 else if (is_siso(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08002275 rs_move_siso_to_other(priv, lq_sta, conf, sta, index);
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07002276 else if (is_mimo2(tbl->lq_type))
2277 rs_move_mimo2_to_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07002278 else
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07002279 rs_move_mimo3_to_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07002280
Ben Cahill77626352007-11-29 11:09:44 +08002281 /* If new "search" mode was selected, set up in uCode table */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002282 if (lq_sta->search_better_tbl) {
Ben Cahill77626352007-11-29 11:09:44 +08002283 /* Access the "search" table, clear its history. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002284 tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Zhu Yib481de92007-09-25 17:54:57 -07002285 for (i = 0; i < IWL_RATE_COUNT; i++)
2286 rs_rate_scale_clear_window(&(tbl->win[i]));
2287
Ben Cahill77626352007-11-29 11:09:44 +08002288 /* Use new "search" start rate */
Tomas Winklere7d326ac2008-06-12 09:47:11 +08002289 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002290
Tomas Winklere1623442009-01-27 14:27:56 -08002291 IWL_DEBUG_RATE(priv, "Switch current mcs: %X index: %d\n",
Guy Cohen39e88502008-04-23 17:14:57 -07002292 tbl->current_rate, index);
Guy Cohen07bc28e2008-04-23 17:15:03 -07002293 rs_fill_link_cmd(priv, lq_sta, tbl->current_rate);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08002294 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC, false);
Mohamed Abbas447fee72009-04-20 14:37:02 -07002295 } else
2296 done_search = 1;
2297 }
Zhu Yib481de92007-09-25 17:54:57 -07002298
Mohamed Abbas447fee72009-04-20 14:37:02 -07002299 if (done_search && !lq_sta->stay_in_tbl) {
Ben Cahill77626352007-11-29 11:09:44 +08002300 /* If the "active" (non-search) mode was legacy,
2301 * and we've tried switching antennas,
2302 * but we haven't been able to try HT modes (not available),
2303 * stay with best antenna legacy modulation for a while
2304 * before next round of mode comparisons. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002305 tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
Luis R. Rodriguezde27e642008-12-23 15:58:44 -08002306 if (is_legacy(tbl1->lq_type) && !conf_is_ht(conf) &&
Mohamed Abbasd6e93392009-05-08 13:44:39 -07002307 lq_sta->action_counter > tbl1->max_search) {
Tomas Winklere1623442009-01-27 14:27:56 -08002308 IWL_DEBUG_RATE(priv, "LQ: STAY in legacy table\n");
Ester Kummerbf403db2008-05-05 10:22:40 +08002309 rs_set_stay_in_table(priv, 1, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002310 }
2311
Ben Cahill77626352007-11-29 11:09:44 +08002312 /* If we're in an HT mode, and all 3 mode switch actions
2313 * have been tried and compared, stay in this best modulation
2314 * mode for a while before next round of mode comparisons. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002315 if (lq_sta->enable_counter &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002316 (lq_sta->action_counter >= tbl1->max_search) &&
2317 iwl_ht_enabled(priv)) {
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002318 if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
2319 (lq_sta->tx_agg_tid_en & (1 << tid)) &&
2320 (tid != MAX_TID_COUNT)) {
Wey-Yi Guy86b47662009-03-10 14:35:09 -07002321 tid_data =
2322 &priv->stations[lq_sta->lq.sta_id].tid[tid];
2323 if (tid_data->agg.state == IWL_AGG_OFF) {
2324 IWL_DEBUG_RATE(priv,
2325 "try to aggregate tid %d\n",
2326 tid);
2327 rs_tl_turn_on_agg(priv, tid,
2328 lq_sta, sta);
2329 }
Zhu Yib481de92007-09-25 17:54:57 -07002330 }
Ester Kummerbf403db2008-05-05 10:22:40 +08002331 rs_set_stay_in_table(priv, 0, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002332 }
Zhu Yib481de92007-09-25 17:54:57 -07002333 }
2334
2335out:
Tomas Winkler978785a2008-12-19 10:37:31 +08002336 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
Zhu Yib481de92007-09-25 17:54:57 -07002337 i = index;
Johannes Bergb7e35002008-09-11 02:22:58 +02002338 lq_sta->last_txrate_idx = i;
Zhu Yib481de92007-09-25 17:54:57 -07002339}
2340
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08002341/**
2342 * rs_initialize_lq - Initialize a station's hardware rate table
2343 *
2344 * The uCode's station table contains a table of fallback rates
2345 * for automatic fallback during transmission.
2346 *
2347 * NOTE: This sets up a default set of values. These will be replaced later
2348 * if the driver's iwl-agn-rs rate scaling algorithm is used, instead of
2349 * rc80211_simple.
2350 *
2351 * NOTE: Run REPLY_ADD_STA command to set up station table entry, before
2352 * calling this function (which runs REPLY_TX_LINK_QUALITY_CMD,
2353 * which requires station table entry to exist).
2354 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002355static void rs_initialize_lq(struct iwl_priv *priv,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002356 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02002357 struct ieee80211_sta *sta,
2358 struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002359{
Tomas Winklere227cea2008-07-18 13:53:05 +08002360 struct iwl_scale_tbl_info *tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002361 int rate_idx;
Guy Cohenaade00c2008-04-23 17:14:59 -07002362 int i;
Guy Cohen39e88502008-04-23 17:14:57 -07002363 u32 rate;
Daniel C Halperinb2617932009-08-13 13:30:59 -07002364 u8 use_green = rs_use_green(sta, &priv->current_ht_config);
Guy Cohenaade00c2008-04-23 17:14:59 -07002365 u8 active_tbl = 0;
2366 u8 valid_tx_ant;
Zhu Yib481de92007-09-25 17:54:57 -07002367
Johannes Berg4b7679a2008-09-18 18:14:18 +02002368 if (!sta || !lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002369 goto out;
2370
Johannes Bergb7e35002008-09-11 02:22:58 +02002371 i = lq_sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002372
Guy Cohenaade00c2008-04-23 17:14:59 -07002373 valid_tx_ant = priv->hw_params.valid_tx_ant;
2374
Tomas Winklerc33104f2008-01-14 17:46:21 -08002375 if (!lq_sta->search_better_tbl)
2376 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002377 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002378 active_tbl = 1 - lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002379
Tomas Winklerc33104f2008-01-14 17:46:21 -08002380 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07002381
2382 if ((i < 0) || (i >= IWL_RATE_COUNT))
2383 i = 0;
2384
Tomas Winkler1826dcc2008-05-15 13:54:02 +08002385 rate = iwl_rates[i].plcp;
Winkler, Tomaseb48dca2008-10-29 14:05:48 -07002386 tbl->ant_type = first_antenna(valid_tx_ant);
2387 rate |= tbl->ant_type << RATE_MCS_ANT_POS;
Zhu Yib481de92007-09-25 17:54:57 -07002388
2389 if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
Guy Cohen39e88502008-04-23 17:14:57 -07002390 rate |= RATE_MCS_CCK_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002391
Guy Cohen39e88502008-04-23 17:14:57 -07002392 rs_get_tbl_info_from_mcs(rate, priv->band, tbl, &rate_idx);
Guy Cohenaade00c2008-04-23 17:14:59 -07002393 if (!rs_is_valid_ant(valid_tx_ant, tbl->ant_type))
2394 rs_toggle_antenna(valid_tx_ant, &rate, tbl);
Zhu Yib481de92007-09-25 17:54:57 -07002395
Tomas Winkler978785a2008-12-19 10:37:31 +08002396 rate = rate_n_flags_from_tbl(priv, tbl, rate_idx, use_green);
Guy Cohen39e88502008-04-23 17:14:57 -07002397 tbl->current_rate = rate;
Guy Coheneecd6e52008-04-23 17:14:58 -07002398 rs_set_expected_tpt_table(lq_sta, tbl);
Guy Cohen07bc28e2008-04-23 17:15:03 -07002399 rs_fill_link_cmd(NULL, lq_sta, rate);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08002400 priv->stations[lq_sta->lq.sta_id].lq = &lq_sta->lq;
2401 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_SYNC, true);
Zhu Yib481de92007-09-25 17:54:57 -07002402 out:
2403 return;
2404}
2405
Johannes Berge6a98542008-10-21 12:40:02 +02002406static void rs_get_rate(void *priv_r, struct ieee80211_sta *sta, void *priv_sta,
2407 struct ieee80211_tx_rate_control *txrc)
Zhu Yib481de92007-09-25 17:54:57 -07002408{
2409
Johannes Berge6a98542008-10-21 12:40:02 +02002410 struct sk_buff *skb = txrc->skb;
2411 struct ieee80211_supported_band *sband = txrc->sband;
Reinette Chatref875f512010-04-05 10:43:10 -07002412 struct iwl_priv *priv __maybe_unused = (struct iwl_priv *)priv_r;
Johannes Berge6a98542008-10-21 12:40:02 +02002413 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Tomas Winkler417f1142008-11-12 13:14:06 -08002414 struct iwl_lq_sta *lq_sta = priv_sta;
2415 int rate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002416
Tomas Winklere1623442009-01-27 14:27:56 -08002417 IWL_DEBUG_RATE_LIMIT(priv, "rate scale calculate new rate for skb\n");
Zhu Yib481de92007-09-25 17:54:57 -07002418
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002419 /* Get max rate if user set max rate */
2420 if (lq_sta) {
2421 lq_sta->max_rate_idx = txrc->max_rate_idx;
2422 if ((sband->band == IEEE80211_BAND_5GHZ) &&
2423 (lq_sta->max_rate_idx != -1))
2424 lq_sta->max_rate_idx += IWL_FIRST_OFDM_RATE;
2425 if ((lq_sta->max_rate_idx < 0) ||
2426 (lq_sta->max_rate_idx >= IWL_RATE_COUNT))
2427 lq_sta->max_rate_idx = -1;
2428 }
2429
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08002430 /* Treat uninitialized rate scaling data same as non-existing. */
2431 if (lq_sta && !lq_sta->drv) {
2432 IWL_DEBUG_RATE(priv, "Rate scaling not initialized yet.\n");
2433 priv_sta = NULL;
2434 }
2435
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002436 /* Send management frames and NO_ACK data using lowest rate. */
Luis R. Rodriguez4c6d4f52009-07-16 10:05:41 -07002437 if (rate_control_send_low(sta, priv_sta, txrc))
Johannes Berg4b7679a2008-09-18 18:14:18 +02002438 return;
Zhu Yib481de92007-09-25 17:54:57 -07002439
Tomas Winkler417f1142008-11-12 13:14:06 -08002440 rate_idx = lq_sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002441
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002442 if (lq_sta->last_rate_n_flags & RATE_MCS_HT_MSK) {
Tomas Winkler417f1142008-11-12 13:14:06 -08002443 rate_idx -= IWL_FIRST_OFDM_RATE;
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002444 /* 6M and 9M shared same MCS index */
2445 rate_idx = (rate_idx > 0) ? (rate_idx - 1) : 0;
2446 if (rs_extract_rate(lq_sta->last_rate_n_flags) >=
2447 IWL_RATE_MIMO3_6M_PLCP)
2448 rate_idx = rate_idx + (2 * MCS_INDEX_PER_STREAM);
2449 else if (rs_extract_rate(lq_sta->last_rate_n_flags) >=
2450 IWL_RATE_MIMO2_6M_PLCP)
2451 rate_idx = rate_idx + MCS_INDEX_PER_STREAM;
2452 info->control.rates[0].flags = IEEE80211_TX_RC_MCS;
2453 if (lq_sta->last_rate_n_flags & RATE_MCS_SGI_MSK)
2454 info->control.rates[0].flags |= IEEE80211_TX_RC_SHORT_GI;
2455 if (lq_sta->last_rate_n_flags & RATE_MCS_DUP_MSK)
2456 info->control.rates[0].flags |= IEEE80211_TX_RC_DUP_DATA;
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07002457 if (lq_sta->last_rate_n_flags & RATE_MCS_HT40_MSK)
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002458 info->control.rates[0].flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
2459 if (lq_sta->last_rate_n_flags & RATE_MCS_GF_MSK)
2460 info->control.rates[0].flags |= IEEE80211_TX_RC_GREEN_FIELD;
2461 } else {
Daniel C Halperin50273092009-08-28 09:44:45 -07002462 /* Check for invalid rates */
2463 if ((rate_idx < 0) || (rate_idx >= IWL_RATE_COUNT_LEGACY) ||
2464 ((sband->band == IEEE80211_BAND_5GHZ) &&
2465 (rate_idx < IWL_FIRST_OFDM_RATE)))
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002466 rate_idx = rate_lowest_index(sband, sta);
Daniel C Halperin50273092009-08-28 09:44:45 -07002467 /* On valid 5 GHz rate, adjust index */
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002468 else if (sband->band == IEEE80211_BAND_5GHZ)
2469 rate_idx -= IWL_FIRST_OFDM_RATE;
Daniel C Halperin7ebaeff2009-08-21 13:34:20 -07002470 info->control.rates[0].flags = 0;
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002471 }
Tomas Winkler417f1142008-11-12 13:14:06 -08002472 info->control.rates[0].idx = rate_idx;
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002473
Zhu Yib481de92007-09-25 17:54:57 -07002474}
2475
Johannes Berg4b7679a2008-09-18 18:14:18 +02002476static void *rs_alloc_sta(void *priv_rate, struct ieee80211_sta *sta,
2477 gfp_t gfp)
Zhu Yib481de92007-09-25 17:54:57 -07002478{
Tomas Winklere227cea2008-07-18 13:53:05 +08002479 struct iwl_lq_sta *lq_sta;
Reinette Chatre8d9698b2009-10-16 14:25:55 -07002480 struct iwl_station_priv *sta_priv = (struct iwl_station_priv *) sta->drv_priv;
Ester Kummerbf403db2008-05-05 10:22:40 +08002481 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07002482
Ester Kummerbf403db2008-05-05 10:22:40 +08002483 priv = (struct iwl_priv *)priv_rate;
Tomas Winklere1623442009-01-27 14:27:56 -08002484 IWL_DEBUG_RATE(priv, "create station rate scale window\n");
Zhu Yib481de92007-09-25 17:54:57 -07002485
Reinette Chatre8d9698b2009-10-16 14:25:55 -07002486 lq_sta = &sta_priv->lq_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002487
Tomas Winklerc33104f2008-01-14 17:46:21 -08002488 return lq_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002489}
2490
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08002491/*
2492 * Called after adding a new station to initialize rate scaling
2493 */
2494void iwl_rs_rate_init(struct iwl_priv *priv, struct ieee80211_sta *sta, u8 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07002495{
2496 int i, j;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08002497 struct ieee80211_hw *hw = priv->hw;
Johannes Berg4b7679a2008-09-18 18:14:18 +02002498 struct ieee80211_conf *conf = &priv->hw->conf;
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002499 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08002500 struct iwl_station_priv *sta_priv;
2501 struct iwl_lq_sta *lq_sta;
2502 struct ieee80211_supported_band *sband;
Zhu Yib481de92007-09-25 17:54:57 -07002503
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08002504 sta_priv = (struct iwl_station_priv *) sta->drv_priv;
2505 lq_sta = &sta_priv->lq_sta;
2506 sband = hw->wiphy->bands[conf->channel->band];
2507
2508
2509 lq_sta->lq.sta_id = sta_id;
Reinette Chatre5ad13f82009-10-30 14:36:14 -07002510
2511 for (j = 0; j < LQ_SIZE; j++)
2512 for (i = 0; i < IWL_RATE_COUNT; i++)
2513 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
2514
Tomas Winklerc33104f2008-01-14 17:46:21 -08002515 lq_sta->flush_timer = 0;
Johannes Berg4b7679a2008-09-18 18:14:18 +02002516 lq_sta->supp_rates = sta->supp_rates[sband->band];
Zhu Yib481de92007-09-25 17:54:57 -07002517 for (j = 0; j < LQ_SIZE; j++)
2518 for (i = 0; i < IWL_RATE_COUNT; i++)
Tomas Winkler3ac7f142008-07-21 02:40:14 +03002519 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
Zhu Yib481de92007-09-25 17:54:57 -07002520
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08002521 IWL_DEBUG_RATE(priv, "LQ: *** rate scale station global init for station %d ***\n",
2522 sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07002523 /* TODO: what is a good starting rate for STA? About middle? Maybe not
2524 * the lowest or the highest rate.. Could consider using RSSI from
2525 * previous packets? Need to have IEEE 802.1X auth succeed immediately
2526 * after assoc.. */
2527
Tomas Winklerc33104f2008-01-14 17:46:21 -08002528 lq_sta->is_dup = 0;
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002529 lq_sta->max_rate_idx = -1;
Abbas, Mohamed7d049e52009-01-20 21:33:53 -08002530 lq_sta->missed_rate_counter = IWL_MISSED_RATE_MAX;
Daniel C Halperinb2617932009-08-13 13:30:59 -07002531 lq_sta->is_green = rs_use_green(sta, &priv->current_ht_config);
Guy Cohen39e88502008-04-23 17:14:57 -07002532 lq_sta->active_legacy_rate = priv->active_rate & ~(0x1000);
Johannes Berg8318d782008-01-24 19:38:38 +01002533 lq_sta->band = priv->band;
Ben Cahill77626352007-11-29 11:09:44 +08002534 /*
2535 * active_siso_rate mask includes 9 MBits (bit 5), and CCK (bits 0-3),
2536 * supp_rates[] does not; shift to convert format, force 9 MBits off.
2537 */
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002538 lq_sta->active_siso_rate = ht_cap->mcs.rx_mask[0] << 1;
2539 lq_sta->active_siso_rate |= ht_cap->mcs.rx_mask[0] & 0x1;
Guy Cohenfde0db32008-04-21 15:42:01 -07002540 lq_sta->active_siso_rate &= ~((u16)0x2);
2541 lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -07002542
Ben Cahill77626352007-11-29 11:09:44 +08002543 /* Same here */
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002544 lq_sta->active_mimo2_rate = ht_cap->mcs.rx_mask[1] << 1;
2545 lq_sta->active_mimo2_rate |= ht_cap->mcs.rx_mask[1] & 0x1;
Guy Cohenfde0db32008-04-21 15:42:01 -07002546 lq_sta->active_mimo2_rate &= ~((u16)0x2);
2547 lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE;
2548
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002549 lq_sta->active_mimo3_rate = ht_cap->mcs.rx_mask[2] << 1;
2550 lq_sta->active_mimo3_rate |= ht_cap->mcs.rx_mask[2] & 0x1;
Guy Cohenfde0db32008-04-21 15:42:01 -07002551 lq_sta->active_mimo3_rate &= ~((u16)0x2);
2552 lq_sta->active_mimo3_rate <<= IWL_FIRST_OFDM_RATE;
2553
Tomas Winklere1623442009-01-27 14:27:56 -08002554 IWL_DEBUG_RATE(priv, "SISO-RATE=%X MIMO2-RATE=%X MIMO3-RATE=%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002555 lq_sta->active_siso_rate,
Guy Cohenfde0db32008-04-21 15:42:01 -07002556 lq_sta->active_mimo2_rate,
2557 lq_sta->active_mimo3_rate);
2558
Tomas Winklera96a27f2008-10-23 23:48:56 -07002559 /* These values will be overridden later */
Wey-Yi Guy3a23d692010-04-03 16:44:39 -07002560 lq_sta->lq.general_params.single_stream_ant_msk =
2561 first_antenna(priv->hw_params.valid_tx_ant);
2562 lq_sta->lq.general_params.dual_stream_ant_msk =
2563 priv->hw_params.valid_tx_ant &
2564 ~first_antenna(priv->hw_params.valid_tx_ant);
2565 if (!lq_sta->lq.general_params.dual_stream_ant_msk) {
2566 lq_sta->lq.general_params.dual_stream_ant_msk = ANT_AB;
2567 } else if (num_of_ant(priv->hw_params.valid_tx_ant) == 2) {
2568 lq_sta->lq.general_params.dual_stream_ant_msk =
2569 priv->hw_params.valid_tx_ant;
2570 }
Guy Cohen07bc28e2008-04-23 17:15:03 -07002571
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002572 /* as default allow aggregation for all tids */
2573 lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002574 lq_sta->drv = priv;
Zhu Yib481de92007-09-25 17:54:57 -07002575
Daniel C Halperin50273092009-08-28 09:44:45 -07002576 /* Set last_txrate_idx to lowest rate */
2577 lq_sta->last_txrate_idx = rate_lowest_index(sband, sta);
2578 if (sband->band == IEEE80211_BAND_5GHZ)
2579 lq_sta->last_txrate_idx += IWL_FIRST_OFDM_RATE;
Daniel C Halperine3949d62009-09-17 10:43:50 -07002580 lq_sta->is_agg = 0;
Mohamed Abbasd5e49032008-12-12 08:22:15 -08002581
Johannes Berg4b7679a2008-09-18 18:14:18 +02002582 rs_initialize_lq(priv, conf, sta, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002583}
2584
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002585static void rs_fill_link_cmd(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08002586 struct iwl_lq_sta *lq_sta, u32 new_rate)
Zhu Yib481de92007-09-25 17:54:57 -07002587{
Tomas Winklere227cea2008-07-18 13:53:05 +08002588 struct iwl_scale_tbl_info tbl_type;
Zhu Yib481de92007-09-25 17:54:57 -07002589 int index = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002590 int rate_idx;
Zhu Yi1b696de2007-09-27 11:27:41 +08002591 int repeat_rate = 0;
Guy Cohenaade00c2008-04-23 17:14:59 -07002592 u8 ant_toggle_cnt = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002593 u8 use_ht_possible = 1;
Guy Cohenaade00c2008-04-23 17:14:59 -07002594 u8 valid_tx_ant = 0;
Guy Cohen07bc28e2008-04-23 17:15:03 -07002595 struct iwl_link_quality_cmd *lq_cmd = &lq_sta->lq;
Zhu Yib481de92007-09-25 17:54:57 -07002596
Ben Cahill77626352007-11-29 11:09:44 +08002597 /* Override starting rate (index 0) if needed for debug purposes */
Guy Cohen39e88502008-04-23 17:14:57 -07002598 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Zhu Yi98d7e092007-09-27 11:27:42 +08002599
Guy Cohen39e88502008-04-23 17:14:57 -07002600 /* Interpret new_rate (rate_n_flags) */
Guy Cohenaade00c2008-04-23 17:14:59 -07002601 memset(&tbl_type, 0, sizeof(tbl_type));
Guy Cohen39e88502008-04-23 17:14:57 -07002602 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band,
Zhu Yib481de92007-09-25 17:54:57 -07002603 &tbl_type, &rate_idx);
2604
Ben Cahill77626352007-11-29 11:09:44 +08002605 /* How many times should we repeat the initial rate? */
Zhu Yib481de92007-09-25 17:54:57 -07002606 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07002607 ant_toggle_cnt = 1;
Zhu Yi1b696de2007-09-27 11:27:41 +08002608 repeat_rate = IWL_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002609 } else {
Zhu Yi1b696de2007-09-27 11:27:41 +08002610 repeat_rate = IWL_HT_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002611 }
Zhu Yib481de92007-09-25 17:54:57 -07002612
2613 lq_cmd->general_params.mimo_delimiter =
2614 is_mimo(tbl_type.lq_type) ? 1 : 0;
Ben Cahill77626352007-11-29 11:09:44 +08002615
2616 /* Fill 1st table entry (index 0) */
Guy Cohen39e88502008-04-23 17:14:57 -07002617 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002618
Guy Cohen07bc28e2008-04-23 17:15:03 -07002619 if (num_of_ant(tbl_type.ant_type) == 1) {
2620 lq_cmd->general_params.single_stream_ant_msk =
2621 tbl_type.ant_type;
2622 } else if (num_of_ant(tbl_type.ant_type) == 2) {
2623 lq_cmd->general_params.dual_stream_ant_msk =
2624 tbl_type.ant_type;
2625 } /* otherwise we don't modify the existing value */
Zhu Yib481de92007-09-25 17:54:57 -07002626
2627 index++;
Zhu Yi1b696de2007-09-27 11:27:41 +08002628 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002629
Guy Cohenaade00c2008-04-23 17:14:59 -07002630 if (priv)
2631 valid_tx_ant = priv->hw_params.valid_tx_ant;
2632
Ben Cahill77626352007-11-29 11:09:44 +08002633 /* Fill rest of rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002634 while (index < LINK_QUAL_MAX_RETRY_NUM) {
Ben Cahill77626352007-11-29 11:09:44 +08002635 /* Repeat initial/next rate.
2636 * For legacy IWL_NUMBER_TRY == 1, this loop will not execute.
2637 * For HT IWL_HT_NUMBER_TRY == 3, this executes twice. */
Zhu Yi1b696de2007-09-27 11:27:41 +08002638 while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
Zhu Yib481de92007-09-25 17:54:57 -07002639 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07002640 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2641 ant_toggle_cnt++;
2642 else if (priv &&
2643 rs_toggle_antenna(valid_tx_ant,
2644 &new_rate, &tbl_type))
2645 ant_toggle_cnt = 1;
2646}
Zhu Yi98d7e092007-09-27 11:27:42 +08002647
Ben Cahill77626352007-11-29 11:09:44 +08002648 /* Override next rate if needed for debug purposes */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002649 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Ben Cahill77626352007-11-29 11:09:44 +08002650
2651 /* Fill next table entry */
Zhu Yib481de92007-09-25 17:54:57 -07002652 lq_cmd->rs_table[index].rate_n_flags =
Guy Cohen39e88502008-04-23 17:14:57 -07002653 cpu_to_le32(new_rate);
Zhu Yi1b696de2007-09-27 11:27:41 +08002654 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002655 index++;
2656 }
2657
Guy Cohen39e88502008-04-23 17:14:57 -07002658 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band, &tbl_type,
Zhu Yib481de92007-09-25 17:54:57 -07002659 &rate_idx);
2660
Ben Cahill77626352007-11-29 11:09:44 +08002661 /* Indicate to uCode which entries might be MIMO.
2662 * If initial rate was MIMO, this will finally end up
2663 * as (IWL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
Zhu Yib481de92007-09-25 17:54:57 -07002664 if (is_mimo(tbl_type.lq_type))
2665 lq_cmd->general_params.mimo_delimiter = index;
2666
Ben Cahill77626352007-11-29 11:09:44 +08002667 /* Get next rate */
Guy Cohen39e88502008-04-23 17:14:57 -07002668 new_rate = rs_get_lower_rate(lq_sta, &tbl_type, rate_idx,
2669 use_ht_possible);
Zhu Yib481de92007-09-25 17:54:57 -07002670
Ben Cahill77626352007-11-29 11:09:44 +08002671 /* How many times should we repeat the next rate? */
Zhu Yib481de92007-09-25 17:54:57 -07002672 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07002673 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2674 ant_toggle_cnt++;
2675 else if (priv &&
2676 rs_toggle_antenna(valid_tx_ant,
2677 &new_rate, &tbl_type))
2678 ant_toggle_cnt = 1;
2679
Zhu Yi1b696de2007-09-27 11:27:41 +08002680 repeat_rate = IWL_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002681 } else {
Zhu Yi1b696de2007-09-27 11:27:41 +08002682 repeat_rate = IWL_HT_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002683 }
Zhu Yib481de92007-09-25 17:54:57 -07002684
Ben Cahill77626352007-11-29 11:09:44 +08002685 /* Don't allow HT rates after next pass.
2686 * rs_get_lower_rate() will change type to LQ_A or LQ_G. */
Zhu Yib481de92007-09-25 17:54:57 -07002687 use_ht_possible = 0;
2688
Ben Cahill77626352007-11-29 11:09:44 +08002689 /* Override next rate if needed for debug purposes */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002690 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Ben Cahill77626352007-11-29 11:09:44 +08002691
2692 /* Fill next table entry */
Guy Cohen39e88502008-04-23 17:14:57 -07002693 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002694
2695 index++;
Zhu Yi1b696de2007-09-27 11:27:41 +08002696 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002697 }
2698
Wey-Yi Guy4d80d722009-10-02 13:44:01 -07002699 lq_cmd->agg_params.agg_frame_cnt_limit = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
Wey-Yi Guy13c33a02009-06-03 11:44:11 -07002700 lq_cmd->agg_params.agg_dis_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
Wey-Yi Guy74697012010-07-28 09:18:54 -07002701
Wey-Yi Guy13c33a02009-06-03 11:44:11 -07002702 lq_cmd->agg_params.agg_time_limit =
2703 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
Wey-Yi Guy74697012010-07-28 09:18:54 -07002704 /*
2705 * overwrite if needed, pass aggregation time limit
2706 * to uCode in uSec
2707 */
2708 if (priv && priv->cfg->agg_time_limit &&
2709 priv->cfg->agg_time_limit >= LINK_QUAL_AGG_TIME_LIMIT_MIN &&
2710 priv->cfg->agg_time_limit <= LINK_QUAL_AGG_TIME_LIMIT_MAX)
2711 lq_cmd->agg_params.agg_time_limit =
2712 cpu_to_le16(priv->cfg->agg_time_limit);
Zhu Yib481de92007-09-25 17:54:57 -07002713}
2714
Johannes Berg4b7679a2008-09-18 18:14:18 +02002715static void *rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
Zhu Yib481de92007-09-25 17:54:57 -07002716{
Johannes Berg4b7679a2008-09-18 18:14:18 +02002717 return hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07002718}
2719/* rate scale requires free function to be implemented */
2720static void rs_free(void *priv_rate)
2721{
2722 return;
2723}
2724
Johannes Berg4b7679a2008-09-18 18:14:18 +02002725static void rs_free_sta(void *priv_r, struct ieee80211_sta *sta,
2726 void *priv_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002727{
Rami Rosen45527c22008-10-07 09:50:01 +02002728 struct iwl_priv *priv __maybe_unused = priv_r;
Zhu Yib481de92007-09-25 17:54:57 -07002729
Tomas Winklere1623442009-01-27 14:27:56 -08002730 IWL_DEBUG_RATE(priv, "enter\n");
Tomas Winklere1623442009-01-27 14:27:56 -08002731 IWL_DEBUG_RATE(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07002732}
2733
2734
Zhu Yi93dc6462007-09-27 11:27:37 +08002735#ifdef CONFIG_MAC80211_DEBUGFS
Zhu Yi5ae212c2007-09-27 11:27:38 +08002736static int open_file_generic(struct inode *inode, struct file *file)
2737{
2738 file->private_data = inode->i_private;
2739 return 0;
2740}
Tomas Winklere227cea2008-07-18 13:53:05 +08002741static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
2742 u32 *rate_n_flags, int index)
Zhu Yi98d7e092007-09-27 11:27:42 +08002743{
Ester Kummerbf403db2008-05-05 10:22:40 +08002744 struct iwl_priv *priv;
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07002745 u8 valid_tx_ant;
2746 u8 ant_sel_tx;
Ester Kummerbf403db2008-05-05 10:22:40 +08002747
2748 priv = lq_sta->drv;
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07002749 valid_tx_ant = priv->hw_params.valid_tx_ant;
Guy Cohen39e88502008-04-23 17:14:57 -07002750 if (lq_sta->dbg_fixed_rate) {
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07002751 ant_sel_tx =
2752 ((lq_sta->dbg_fixed_rate & RATE_MCS_ANT_ABC_MSK)
2753 >> RATE_MCS_ANT_POS);
2754 if ((valid_tx_ant & ant_sel_tx) == ant_sel_tx) {
Guy Cohen39e88502008-04-23 17:14:57 -07002755 *rate_n_flags = lq_sta->dbg_fixed_rate;
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07002756 IWL_DEBUG_RATE(priv, "Fixed rate ON\n");
Guy Cohen39e88502008-04-23 17:14:57 -07002757 } else {
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07002758 lq_sta->dbg_fixed_rate = 0;
2759 IWL_ERR(priv,
2760 "Invalid antenna selection 0x%X, Valid is 0x%X\n",
2761 ant_sel_tx, valid_tx_ant);
2762 IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
Guy Cohen39e88502008-04-23 17:14:57 -07002763 }
Guy Cohen39e88502008-04-23 17:14:57 -07002764 } else {
Tomas Winklere1623442009-01-27 14:27:56 -08002765 IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
Zhu Yi98d7e092007-09-27 11:27:42 +08002766 }
Zhu Yi98d7e092007-09-27 11:27:42 +08002767}
2768
2769static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
2770 const char __user *user_buf, size_t count, loff_t *ppos)
2771{
Tomas Winklere227cea2008-07-18 13:53:05 +08002772 struct iwl_lq_sta *lq_sta = file->private_data;
Ester Kummerbf403db2008-05-05 10:22:40 +08002773 struct iwl_priv *priv;
Zhu Yi98d7e092007-09-27 11:27:42 +08002774 char buf[64];
2775 int buf_size;
2776 u32 parsed_rate;
2777
Ester Kummerbf403db2008-05-05 10:22:40 +08002778 priv = lq_sta->drv;
Zhu Yi98d7e092007-09-27 11:27:42 +08002779 memset(buf, 0, sizeof(buf));
2780 buf_size = min(count, sizeof(buf) - 1);
2781 if (copy_from_user(buf, user_buf, buf_size))
2782 return -EFAULT;
2783
2784 if (sscanf(buf, "%x", &parsed_rate) == 1)
Guy Cohen39e88502008-04-23 17:14:57 -07002785 lq_sta->dbg_fixed_rate = parsed_rate;
Zhu Yi98d7e092007-09-27 11:27:42 +08002786 else
Guy Cohen39e88502008-04-23 17:14:57 -07002787 lq_sta->dbg_fixed_rate = 0;
Zhu Yi98d7e092007-09-27 11:27:42 +08002788
Guy Cohen39e88502008-04-23 17:14:57 -07002789 lq_sta->active_legacy_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
2790 lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2791 lq_sta->active_mimo2_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2792 lq_sta->active_mimo3_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
Zhu Yi98d7e092007-09-27 11:27:42 +08002793
Tomas Winklere1623442009-01-27 14:27:56 -08002794 IWL_DEBUG_RATE(priv, "sta_id %d rate 0x%X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07002795 lq_sta->lq.sta_id, lq_sta->dbg_fixed_rate);
Zhu Yi98d7e092007-09-27 11:27:42 +08002796
Guy Cohen39e88502008-04-23 17:14:57 -07002797 if (lq_sta->dbg_fixed_rate) {
Guy Cohen07bc28e2008-04-23 17:15:03 -07002798 rs_fill_link_cmd(NULL, lq_sta, lq_sta->dbg_fixed_rate);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08002799 iwl_send_lq_cmd(lq_sta->drv, &lq_sta->lq, CMD_ASYNC, false);
Zhu Yi98d7e092007-09-27 11:27:42 +08002800 }
2801
2802 return count;
2803}
Zhu Yi0209dc12007-09-27 11:27:43 +08002804
Zhu Yi5ae212c2007-09-27 11:27:38 +08002805static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
2806 char __user *user_buf, size_t count, loff_t *ppos)
2807{
Frank Seidela412c802009-03-01 20:25:38 +01002808 char *buff;
Zhu Yi5ae212c2007-09-27 11:27:38 +08002809 int desc = 0;
2810 int i = 0;
Wey-Yi Guy12b96812009-04-08 11:39:27 -07002811 int index = 0;
Frank Seidela412c802009-03-01 20:25:38 +01002812 ssize_t ret;
Zhu Yi5ae212c2007-09-27 11:27:38 +08002813
Tomas Winklere227cea2008-07-18 13:53:05 +08002814 struct iwl_lq_sta *lq_sta = file->private_data;
Wey-Yi Guyd8ae4f52009-03-10 14:35:07 -07002815 struct iwl_priv *priv;
Wey-Yi Guyadb7b5e2009-03-10 14:35:08 -07002816 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002817
Wey-Yi Guyd8ae4f52009-03-10 14:35:07 -07002818 priv = lq_sta->drv;
Frank Seidela412c802009-03-01 20:25:38 +01002819 buff = kmalloc(1024, GFP_KERNEL);
2820 if (!buff)
2821 return -ENOMEM;
2822
Tomas Winklerc33104f2008-01-14 17:46:21 -08002823 desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
Zhu Yi98d7e092007-09-27 11:27:42 +08002824 desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002825 lq_sta->total_failed, lq_sta->total_success,
Guy Cohen39e88502008-04-23 17:14:57 -07002826 lq_sta->active_legacy_rate);
Zhu Yi98d7e092007-09-27 11:27:42 +08002827 desc += sprintf(buff+desc, "fixed rate 0x%X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07002828 lq_sta->dbg_fixed_rate);
Wey-Yi Guyd8ae4f52009-03-10 14:35:07 -07002829 desc += sprintf(buff+desc, "valid_tx_ant %s%s%s\n",
2830 (priv->hw_params.valid_tx_ant & ANT_A) ? "ANT_A," : "",
2831 (priv->hw_params.valid_tx_ant & ANT_B) ? "ANT_B," : "",
2832 (priv->hw_params.valid_tx_ant & ANT_C) ? "ANT_C" : "");
Wey-Yi Guyadb7b5e2009-03-10 14:35:08 -07002833 desc += sprintf(buff+desc, "lq type %s\n",
2834 (is_legacy(tbl->lq_type)) ? "legacy" : "HT");
2835 if (is_Ht(tbl->lq_type)) {
2836 desc += sprintf(buff+desc, " %s",
2837 (is_siso(tbl->lq_type)) ? "SISO" :
2838 ((is_mimo2(tbl->lq_type)) ? "MIMO2" : "MIMO3"));
2839 desc += sprintf(buff+desc, " %s",
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07002840 (tbl->is_ht40) ? "40MHz" : "20MHz");
Daniel C Halperine3949d62009-09-17 10:43:50 -07002841 desc += sprintf(buff+desc, " %s %s %s\n", (tbl->is_SGI) ? "SGI" : "",
2842 (lq_sta->is_green) ? "GF enabled" : "",
2843 (lq_sta->is_agg) ? "AGG on" : "");
Wey-Yi Guyadb7b5e2009-03-10 14:35:08 -07002844 }
Wey-Yi Guy12b96812009-04-08 11:39:27 -07002845 desc += sprintf(buff+desc, "last tx rate=0x%X\n",
2846 lq_sta->last_rate_n_flags);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002847 desc += sprintf(buff+desc, "general:"
2848 "flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002849 lq_sta->lq.general_params.flags,
2850 lq_sta->lq.general_params.mimo_delimiter,
2851 lq_sta->lq.general_params.single_stream_ant_msk,
2852 lq_sta->lq.general_params.dual_stream_ant_msk);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002853
2854 desc += sprintf(buff+desc, "agg:"
2855 "time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002856 le16_to_cpu(lq_sta->lq.agg_params.agg_time_limit),
2857 lq_sta->lq.agg_params.agg_dis_start_th,
2858 lq_sta->lq.agg_params.agg_frame_cnt_limit);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002859
2860 desc += sprintf(buff+desc,
2861 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002862 lq_sta->lq.general_params.start_rate_index[0],
2863 lq_sta->lq.general_params.start_rate_index[1],
2864 lq_sta->lq.general_params.start_rate_index[2],
2865 lq_sta->lq.general_params.start_rate_index[3]);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002866
Wey-Yi Guy12b96812009-04-08 11:39:27 -07002867 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
2868 index = iwl_hwrate_to_plcp_idx(
2869 le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags));
2870 if (is_legacy(tbl->lq_type)) {
2871 desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps\n",
2872 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags),
2873 iwl_rate_mcs[index].mbps);
2874 } else {
2875 desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps (%s)\n",
2876 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags),
2877 iwl_rate_mcs[index].mbps, iwl_rate_mcs[index].mcs);
2878 }
2879 }
Zhu Yi5ae212c2007-09-27 11:27:38 +08002880
Frank Seidela412c802009-03-01 20:25:38 +01002881 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2882 kfree(buff);
2883 return ret;
Zhu Yi5ae212c2007-09-27 11:27:38 +08002884}
2885
2886static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
Zhu Yi98d7e092007-09-27 11:27:42 +08002887 .write = rs_sta_dbgfs_scale_table_write,
Zhu Yi5ae212c2007-09-27 11:27:38 +08002888 .read = rs_sta_dbgfs_scale_table_read,
2889 .open = open_file_generic,
2890};
Zhu Yi0209dc12007-09-27 11:27:43 +08002891static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
2892 char __user *user_buf, size_t count, loff_t *ppos)
2893{
Frank Seidela412c802009-03-01 20:25:38 +01002894 char *buff;
Zhu Yi0209dc12007-09-27 11:27:43 +08002895 int desc = 0;
2896 int i, j;
Frank Seidela412c802009-03-01 20:25:38 +01002897 ssize_t ret;
Zhu Yi0209dc12007-09-27 11:27:43 +08002898
Tomas Winklere227cea2008-07-18 13:53:05 +08002899 struct iwl_lq_sta *lq_sta = file->private_data;
Frank Seidela412c802009-03-01 20:25:38 +01002900
2901 buff = kmalloc(1024, GFP_KERNEL);
2902 if (!buff)
2903 return -ENOMEM;
2904
Zhu Yi0209dc12007-09-27 11:27:43 +08002905 for (i = 0; i < LQ_SIZE; i++) {
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07002906 desc += sprintf(buff+desc,
2907 "%s type=%d SGI=%d HT40=%d DUP=%d GF=%d\n"
Zhu Yi0209dc12007-09-27 11:27:43 +08002908 "rate=0x%X\n",
Abhijeet Kolekarc3056062008-11-12 13:14:08 -08002909 lq_sta->active_tbl == i ? "*" : "x",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002910 lq_sta->lq_info[i].lq_type,
2911 lq_sta->lq_info[i].is_SGI,
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07002912 lq_sta->lq_info[i].is_ht40,
Tomas Winklerc33104f2008-01-14 17:46:21 -08002913 lq_sta->lq_info[i].is_dup,
Wey-Yi Guy2681b202009-05-21 13:44:22 -07002914 lq_sta->is_green,
Guy Cohen39e88502008-04-23 17:14:57 -07002915 lq_sta->lq_info[i].current_rate);
Zhu Yi0209dc12007-09-27 11:27:43 +08002916 for (j = 0; j < IWL_RATE_COUNT; j++) {
2917 desc += sprintf(buff+desc,
Tomas Winklerc33104f2008-01-14 17:46:21 -08002918 "counter=%d success=%d %%=%d\n",
2919 lq_sta->lq_info[i].win[j].counter,
2920 lq_sta->lq_info[i].win[j].success_counter,
2921 lq_sta->lq_info[i].win[j].success_ratio);
Zhu Yi0209dc12007-09-27 11:27:43 +08002922 }
2923 }
Frank Seidela412c802009-03-01 20:25:38 +01002924 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2925 kfree(buff);
2926 return ret;
Zhu Yi0209dc12007-09-27 11:27:43 +08002927}
2928
2929static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
2930 .read = rs_sta_dbgfs_stats_table_read,
2931 .open = open_file_generic,
2932};
Zhu Yi5ae212c2007-09-27 11:27:38 +08002933
Wey-Yi Guy38167452009-05-08 13:44:43 -07002934static ssize_t rs_sta_dbgfs_rate_scale_data_read(struct file *file,
2935 char __user *user_buf, size_t count, loff_t *ppos)
2936{
2937 char buff[120];
2938 int desc = 0;
2939 ssize_t ret;
2940
2941 struct iwl_lq_sta *lq_sta = file->private_data;
2942 struct iwl_priv *priv;
2943 struct iwl_scale_tbl_info *tbl = &lq_sta->lq_info[lq_sta->active_tbl];
2944
2945 priv = lq_sta->drv;
2946
2947 if (is_Ht(tbl->lq_type))
2948 desc += sprintf(buff+desc,
2949 "Bit Rate= %d Mb/s\n",
2950 tbl->expected_tpt[lq_sta->last_txrate_idx]);
2951 else
2952 desc += sprintf(buff+desc,
2953 "Bit Rate= %d Mb/s\n",
2954 iwl_rates[lq_sta->last_txrate_idx].ieee >> 1);
Wey-Yi Guy38167452009-05-08 13:44:43 -07002955
2956 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2957 return ret;
2958}
2959
2960static const struct file_operations rs_sta_dbgfs_rate_scale_data_ops = {
2961 .read = rs_sta_dbgfs_rate_scale_data_read,
2962 .open = open_file_generic,
2963};
2964
Zhu Yi93dc6462007-09-27 11:27:37 +08002965static void rs_add_debugfs(void *priv, void *priv_sta,
2966 struct dentry *dir)
2967{
Tomas Winklere227cea2008-07-18 13:53:05 +08002968 struct iwl_lq_sta *lq_sta = priv_sta;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002969 lq_sta->rs_sta_dbgfs_scale_table_file =
Wey-Yi Guy43e85112009-11-20 12:04:58 -08002970 debugfs_create_file("rate_scale_table", S_IRUSR | S_IWUSR, dir,
Tomas Winklerc33104f2008-01-14 17:46:21 -08002971 lq_sta, &rs_sta_dbgfs_scale_table_ops);
2972 lq_sta->rs_sta_dbgfs_stats_table_file =
Wey-Yi Guy43e85112009-11-20 12:04:58 -08002973 debugfs_create_file("rate_stats_table", S_IRUSR, dir,
Tomas Winklerc33104f2008-01-14 17:46:21 -08002974 lq_sta, &rs_sta_dbgfs_stats_table_ops);
Wey-Yi Guy38167452009-05-08 13:44:43 -07002975 lq_sta->rs_sta_dbgfs_rate_scale_data_file =
Wey-Yi Guy43e85112009-11-20 12:04:58 -08002976 debugfs_create_file("rate_scale_data", S_IRUSR, dir,
Wey-Yi Guy38167452009-05-08 13:44:43 -07002977 lq_sta, &rs_sta_dbgfs_rate_scale_data_ops);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002978 lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file =
Wey-Yi Guy43e85112009-11-20 12:04:58 -08002979 debugfs_create_u8("tx_agg_tid_enable", S_IRUSR | S_IWUSR, dir,
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002980 &lq_sta->tx_agg_tid_en);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002981
Zhu Yi93dc6462007-09-27 11:27:37 +08002982}
2983
2984static void rs_remove_debugfs(void *priv, void *priv_sta)
2985{
Tomas Winklere227cea2008-07-18 13:53:05 +08002986 struct iwl_lq_sta *lq_sta = priv_sta;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002987 debugfs_remove(lq_sta->rs_sta_dbgfs_scale_table_file);
2988 debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
Wey-Yi Guy38167452009-05-08 13:44:43 -07002989 debugfs_remove(lq_sta->rs_sta_dbgfs_rate_scale_data_file);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002990 debugfs_remove(lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file);
Zhu Yi93dc6462007-09-27 11:27:37 +08002991}
2992#endif
2993
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08002994/*
2995 * Initialization of rate scaling information is done by driver after
2996 * the station is added. Since mac80211 calls this function before a
2997 * station is added we ignore it.
2998 */
2999static void rs_rate_init_stub(void *priv_r, struct ieee80211_supported_band *sband,
3000 struct ieee80211_sta *sta, void *priv_sta)
3001{
3002}
Zhu Yib481de92007-09-25 17:54:57 -07003003static struct rate_control_ops rs_ops = {
3004 .module = NULL,
3005 .name = RS_NAME,
3006 .tx_status = rs_tx_status,
3007 .get_rate = rs_get_rate,
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003008 .rate_init = rs_rate_init_stub,
Zhu Yib481de92007-09-25 17:54:57 -07003009 .alloc = rs_alloc,
3010 .free = rs_free,
3011 .alloc_sta = rs_alloc_sta,
3012 .free_sta = rs_free_sta,
Zhu Yi93dc6462007-09-27 11:27:37 +08003013#ifdef CONFIG_MAC80211_DEBUGFS
3014 .add_sta_debugfs = rs_add_debugfs,
3015 .remove_sta_debugfs = rs_remove_debugfs,
3016#endif
Zhu Yib481de92007-09-25 17:54:57 -07003017};
3018
Tomas Winklere227cea2008-07-18 13:53:05 +08003019int iwlagn_rate_control_register(void)
Zhu Yib481de92007-09-25 17:54:57 -07003020{
Reinette Chatre897e1cf2008-03-28 16:21:09 -07003021 return ieee80211_rate_control_register(&rs_ops);
Zhu Yib481de92007-09-25 17:54:57 -07003022}
3023
Tomas Winklere227cea2008-07-18 13:53:05 +08003024void iwlagn_rate_control_unregister(void)
Zhu Yib481de92007-09-25 17:54:57 -07003025{
3026 ieee80211_rate_control_unregister(&rs_ops);
3027}
3028