blob: fe511cbf012e009daaecb22e1be14d4be2b0d466 [file] [log] [blame]
Zhu Yib481de92007-09-25 17:54:57 -07001/******************************************************************************
2 *
Reinette Chatre01f81622009-01-08 10:20:02 -08003 * Copyright(c) 2005 - 2009 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>
29#include <linux/wireless.h>
30#include <net/mac80211.h>
Zhu Yib481de92007-09-25 17:54:57 -070031
32#include <linux/netdevice.h>
33#include <linux/etherdevice.h>
34#include <linux/delay.h>
35
36#include <linux/workqueue.h>
37
Tomas Winkler3e0d4cb2008-04-24 11:55:38 -070038#include "iwl-dev.h"
Emmanuel Grumbachbe1f3ab62008-06-12 09:47:18 +080039#include "iwl-sta.h"
Tomas Winkler857485c2008-03-21 13:53:44 -070040#include "iwl-core.h"
Zhu Yib481de92007-09-25 17:54:57 -070041
Tomas Winklere227cea2008-07-18 13:53:05 +080042#define RS_NAME "iwl-agn-rs"
Zhu Yib481de92007-09-25 17:54:57 -070043
Guy Cohenaade00c2008-04-23 17:14:59 -070044#define NUM_TRY_BEFORE_ANT_TOGGLE 1
Zhu Yib481de92007-09-25 17:54:57 -070045#define IWL_NUMBER_TRY 1
46#define IWL_HT_NUMBER_TRY 3
47
Ben Cahill77626352007-11-29 11:09:44 +080048#define IWL_RATE_MAX_WINDOW 62 /* # tx in history window */
49#define IWL_RATE_MIN_FAILURE_TH 6 /* min failures to calc tpt */
50#define IWL_RATE_MIN_SUCCESS_TH 8 /* min successes to calc tpt */
51
Abbas, Mohamed7d049e52009-01-20 21:33:53 -080052/* max allowed rate miss before sync LQ cmd */
53#define IWL_MISSED_RATE_MAX 15
Ben Cahill77626352007-11-29 11:09:44 +080054/* max time to accum history 2 seconds */
Mohamed Abbas447fee72009-04-20 14:37:02 -070055#define IWL_RATE_SCALE_FLUSH_INTVL (3*HZ)
Zhu Yib481de92007-09-25 17:54:57 -070056
57static u8 rs_ht_to_legacy[] = {
58 IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
59 IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
60 IWL_RATE_6M_INDEX,
61 IWL_RATE_6M_INDEX, IWL_RATE_9M_INDEX,
62 IWL_RATE_12M_INDEX, IWL_RATE_18M_INDEX,
63 IWL_RATE_24M_INDEX, IWL_RATE_36M_INDEX,
64 IWL_RATE_48M_INDEX, IWL_RATE_54M_INDEX
65};
66
Guy Cohenaade00c2008-04-23 17:14:59 -070067static const u8 ant_toggle_lookup[] = {
68 /*ANT_NONE -> */ ANT_NONE,
69 /*ANT_A -> */ ANT_B,
70 /*ANT_B -> */ ANT_C,
71 /*ANT_AB -> */ ANT_BC,
72 /*ANT_C -> */ ANT_A,
73 /*ANT_AC -> */ ANT_AB,
74 /*ANT_BC -> */ ANT_AC,
75 /*ANT_ABC -> */ ANT_ABC,
76};
77
Tomas Winklerc79dd5b2008-03-12 16:58:50 -070078static void rs_rate_scale_perform(struct iwl_priv *priv,
Gábor Stefanik514d65c2009-04-23 19:36:08 +020079 struct sk_buff *skb,
Johannes Berg4b7679a2008-09-18 18:14:18 +020080 struct ieee80211_sta *sta,
81 struct iwl_lq_sta *lq_sta);
Wey-Yi Guy46f93812009-07-24 11:13:03 -070082static void rs_fill_link_cmd(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +080083 struct iwl_lq_sta *lq_sta, u32 rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -070084
85
Zhu Yi98d7e092007-09-27 11:27:42 +080086#ifdef CONFIG_MAC80211_DEBUGFS
Tomas Winklere227cea2008-07-18 13:53:05 +080087static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
88 u32 *rate_n_flags, int index);
Zhu Yi98d7e092007-09-27 11:27:42 +080089#else
Tomas Winklere227cea2008-07-18 13:53:05 +080090static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
91 u32 *rate_n_flags, int index)
Zhu Yi98d7e092007-09-27 11:27:42 +080092{}
93#endif
Ben Cahill77626352007-11-29 11:09:44 +080094
Daniel C Halperine3949d62009-09-17 10:43:50 -070095/**
96 * The following tables contain the expected throughput metrics for all rates
97 *
98 * 1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
99 *
100 * where invalid entries are zeros.
101 *
102 * CCK rates are only valid in legacy table and will only be used in G
103 * (2.4 GHz) band.
Ben Cahill77626352007-11-29 11:09:44 +0800104 */
Wey-Yi Guy584a0f02009-04-01 14:33:24 -0700105
Daniel C Halperine3949d62009-09-17 10:43:50 -0700106static s32 expected_tpt_legacy[IWL_RATE_COUNT] = {
107 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 0
Zhu Yib481de92007-09-25 17:54:57 -0700108};
109
Daniel C Halperine3949d62009-09-17 10:43:50 -0700110static s32 expected_tpt_siso20MHz[4][IWL_RATE_COUNT] = {
111 {0, 0, 0, 0, 42, 0, 76, 102, 124, 158, 183, 193, 202}, /* Norm */
112 {0, 0, 0, 0, 46, 0, 82, 110, 132, 167, 192, 202, 210}, /* SGI */
113 {0, 0, 0, 0, 48, 0, 93, 135, 176, 251, 319, 351, 381}, /* AGG */
114 {0, 0, 0, 0, 53, 0, 102, 149, 193, 275, 348, 381, 413}, /* AGG+SGI */
Zhu Yib481de92007-09-25 17:54:57 -0700115};
116
Daniel C Halperine3949d62009-09-17 10:43:50 -0700117static s32 expected_tpt_siso40MHz[4][IWL_RATE_COUNT] = {
118 {0, 0, 0, 0, 77, 0, 127, 160, 184, 220, 242, 250, 257}, /* Norm */
119 {0, 0, 0, 0, 83, 0, 135, 169, 193, 229, 250, 257, 264}, /* SGI */
120 {0, 0, 0, 0, 96, 0, 182, 259, 328, 451, 553, 598, 640}, /* AGG */
121 {0, 0, 0, 0, 106, 0, 199, 282, 357, 487, 593, 640, 683}, /* AGG+SGI */
Zhu Yib481de92007-09-25 17:54:57 -0700122};
123
Daniel C Halperine3949d62009-09-17 10:43:50 -0700124static s32 expected_tpt_mimo2_20MHz[4][IWL_RATE_COUNT] = {
125 {0, 0, 0, 0, 74, 0, 123, 155, 179, 213, 235, 243, 250}, /* Norm */
126 {0, 0, 0, 0, 81, 0, 131, 164, 187, 221, 242, 250, 256}, /* SGI */
127 {0, 0, 0, 0, 92, 0, 175, 250, 317, 436, 534, 578, 619}, /* AGG */
128 {0, 0, 0, 0, 102, 0, 192, 273, 344, 470, 573, 619, 660}, /* AGG+SGI*/
Zhu Yib481de92007-09-25 17:54:57 -0700129};
130
Daniel C Halperine3949d62009-09-17 10:43:50 -0700131static s32 expected_tpt_mimo2_40MHz[4][IWL_RATE_COUNT] = {
132 {0, 0, 0, 0, 123, 0, 182, 214, 235, 264, 279, 285, 289}, /* Norm */
133 {0, 0, 0, 0, 131, 0, 191, 222, 242, 270, 284, 289, 293}, /* SGI */
134 {0, 0, 0, 0, 180, 0, 327, 446, 545, 708, 828, 878, 922}, /* AGG */
135 {0, 0, 0, 0, 197, 0, 355, 481, 584, 752, 872, 922, 966}, /* AGG+SGI */
Zhu Yib481de92007-09-25 17:54:57 -0700136};
137
Daniel C Halperine3949d62009-09-17 10:43:50 -0700138static s32 expected_tpt_mimo3_20MHz[4][IWL_RATE_COUNT] = {
139 {0, 0, 0, 0, 99, 0, 153, 186, 208, 239, 256, 263, 268}, /* Norm */
140 {0, 0, 0, 0, 106, 0, 162, 194, 215, 246, 262, 268, 273}, /* SGI */
141 {0, 0, 0, 0, 134, 0, 249, 346, 431, 574, 685, 732, 775}, /* AGG */
142 {0, 0, 0, 0, 148, 0, 272, 376, 465, 614, 727, 775, 818}, /* AGG+SGI */
Zhu Yib481de92007-09-25 17:54:57 -0700143};
144
Daniel C Halperine3949d62009-09-17 10:43:50 -0700145static s32 expected_tpt_mimo3_40MHz[4][IWL_RATE_COUNT] = {
146 {0, 0, 0, 0, 152, 0, 211, 239, 255, 279, 290, 294, 297}, /* Norm */
147 {0, 0, 0, 0, 160, 0, 219, 245, 261, 284, 294, 297, 300}, /* SGI */
148 {0, 0, 0, 0, 254, 0, 443, 584, 695, 868, 984, 1030, 1070}, /* AGG */
149 {0, 0, 0, 0, 277, 0, 478, 624, 737, 911, 1026, 1070, 1109}, /* AGG+SGI */
Wey-Yi Guy584a0f02009-04-01 14:33:24 -0700150};
151
Wey-Yi Guy12b96812009-04-08 11:39:27 -0700152/* mbps, mcs */
153const static struct iwl_rate_mcs_info iwl_rate_mcs[IWL_RATE_COUNT] = {
Daniel C Halperine3949d62009-09-17 10:43:50 -0700154 { "1", "BPSK DSSS"},
155 { "2", "QPSK DSSS"},
156 {"5.5", "BPSK CCK"},
157 { "11", "QPSK CCK"},
158 { "6", "BPSK 1/2"},
159 { "9", "BPSK 1/2"},
160 { "12", "QPSK 1/2"},
161 { "18", "QPSK 3/4"},
162 { "24", "16QAM 1/2"},
163 { "36", "16QAM 3/4"},
164 { "48", "64QAM 2/3"},
165 { "54", "64QAM 3/4"},
166 { "60", "64QAM 5/6"},
Wey-Yi Guy12b96812009-04-08 11:39:27 -0700167};
168
Wey-Yi Guya9c146b2009-05-22 11:01:48 -0700169#define MCS_INDEX_PER_STREAM (8)
170
Guy Cohen39e88502008-04-23 17:14:57 -0700171static inline u8 rs_extract_rate(u32 rate_n_flags)
Zhu Yib481de92007-09-25 17:54:57 -0700172{
173 return (u8)(rate_n_flags & 0xFF);
174}
175
Tomas Winklere227cea2008-07-18 13:53:05 +0800176static void rs_rate_scale_clear_window(struct iwl_rate_scale_data *window)
Zhu Yib481de92007-09-25 17:54:57 -0700177{
178 window->data = 0;
179 window->success_counter = 0;
180 window->success_ratio = IWL_INVALID_VALUE;
181 window->counter = 0;
182 window->average_tpt = IWL_INVALID_VALUE;
183 window->stamp = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700184}
185
Guy Cohenaade00c2008-04-23 17:14:59 -0700186static inline u8 rs_is_valid_ant(u8 valid_antenna, u8 ant_type)
187{
Tomas Winkler3ac7f142008-07-21 02:40:14 +0300188 return (ant_type & valid_antenna) == ant_type;
Guy Cohenaade00c2008-04-23 17:14:59 -0700189}
190
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200191/*
192 * removes the old data from the statistics. All data that is older than
193 * TID_MAX_TIME_DIFF, will be deleted.
194 */
Tomas Winklere227cea2008-07-18 13:53:05 +0800195static void rs_tl_rm_old_stats(struct iwl_traffic_load *tl, u32 curr_time)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200196{
197 /* The oldest age we want to keep */
198 u32 oldest_time = curr_time - TID_MAX_TIME_DIFF;
199
200 while (tl->queue_count &&
201 (tl->time_stamp < oldest_time)) {
202 tl->total -= tl->packet_count[tl->head];
203 tl->packet_count[tl->head] = 0;
204 tl->time_stamp += TID_QUEUE_CELL_SPACING;
205 tl->queue_count--;
206 tl->head++;
207 if (tl->head >= TID_QUEUE_MAX_SIZE)
208 tl->head = 0;
209 }
210}
211
212/*
213 * increment traffic load value for tid and also remove
214 * any old values if passed the certain time period
215 */
Tomas Winklere227cea2008-07-18 13:53:05 +0800216static u8 rs_tl_add_packet(struct iwl_lq_sta *lq_data,
Ron Rindjunskyfaa29712008-06-12 09:46:58 +0800217 struct ieee80211_hdr *hdr)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200218{
219 u32 curr_time = jiffies_to_msecs(jiffies);
220 u32 time_diff;
221 s32 index;
Tomas Winklere227cea2008-07-18 13:53:05 +0800222 struct iwl_traffic_load *tl = NULL;
Tomas Winkler54dbb522008-05-15 13:54:06 +0800223 u8 tid;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200224
Tomas Winkler417f1142008-11-12 13:14:06 -0800225 if (ieee80211_is_data_qos(hdr->frame_control)) {
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700226 u8 *qc = ieee80211_get_qos_ctl(hdr);
Tomas Winkler54dbb522008-05-15 13:54:06 +0800227 tid = qc[0] & 0xf;
228 } else
Ron Rindjunskyfaa29712008-06-12 09:46:58 +0800229 return MAX_TID_COUNT;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200230
Reinette Chatree6a6cf42009-08-13 13:30:50 -0700231 if (unlikely(tid >= TID_MAX_LOAD_COUNT))
232 return MAX_TID_COUNT;
233
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200234 tl = &lq_data->load[tid];
235
236 curr_time -= curr_time % TID_ROUND_VALUE;
237
238 /* Happens only for the first packet. Initialize the data */
239 if (!(tl->queue_count)) {
240 tl->total = 1;
241 tl->time_stamp = curr_time;
242 tl->queue_count = 1;
243 tl->head = 0;
244 tl->packet_count[0] = 1;
Ron Rindjunskyfaa29712008-06-12 09:46:58 +0800245 return MAX_TID_COUNT;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200246 }
247
248 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
249 index = time_diff / TID_QUEUE_CELL_SPACING;
250
251 /* The history is too long: remove data that is older than */
252 /* TID_MAX_TIME_DIFF */
253 if (index >= TID_QUEUE_MAX_SIZE)
254 rs_tl_rm_old_stats(tl, curr_time);
255
256 index = (tl->head + index) % TID_QUEUE_MAX_SIZE;
257 tl->packet_count[index] = tl->packet_count[index] + 1;
258 tl->total = tl->total + 1;
259
260 if ((index + 1) > tl->queue_count)
261 tl->queue_count = index + 1;
Ron Rindjunskyfaa29712008-06-12 09:46:58 +0800262
263 return tid;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200264}
265
266/*
267 get the traffic load value for tid
268*/
Tomas Winklere227cea2008-07-18 13:53:05 +0800269static u32 rs_tl_get_load(struct iwl_lq_sta *lq_data, u8 tid)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200270{
271 u32 curr_time = jiffies_to_msecs(jiffies);
272 u32 time_diff;
273 s32 index;
Tomas Winklere227cea2008-07-18 13:53:05 +0800274 struct iwl_traffic_load *tl = NULL;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200275
276 if (tid >= TID_MAX_LOAD_COUNT)
277 return 0;
278
279 tl = &(lq_data->load[tid]);
280
281 curr_time -= curr_time % TID_ROUND_VALUE;
282
283 if (!(tl->queue_count))
284 return 0;
285
286 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
287 index = time_diff / TID_QUEUE_CELL_SPACING;
288
289 /* The history is too long: remove data that is older than */
290 /* TID_MAX_TIME_DIFF */
291 if (index >= TID_QUEUE_MAX_SIZE)
292 rs_tl_rm_old_stats(tl, curr_time);
293
294 return tl->total;
295}
296
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700297static void rs_tl_turn_on_agg_for_tid(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +0800298 struct iwl_lq_sta *lq_data, u8 tid,
Johannes Berg4b7679a2008-09-18 18:14:18 +0200299 struct ieee80211_sta *sta)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200300{
Johannes Bergff550cb2008-09-11 03:17:05 +0200301 if (rs_tl_get_load(lq_data, tid) > IWL_AGG_LOAD_THRESHOLD) {
Tomas Winklere1623442009-01-27 14:27:56 -0800302 IWL_DEBUG_HT(priv, "Starting Tx agg: STA: %pM tid: %d\n",
Johannes Berge1749612008-10-27 15:59:26 -0700303 sta->addr, tid);
Johannes Bergc951ad32009-11-16 12:00:38 +0100304 ieee80211_start_tx_ba_session(sta, tid);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200305 }
306}
307
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700308static void rs_tl_turn_on_agg(struct iwl_priv *priv, u8 tid,
Tomas Winklere227cea2008-07-18 13:53:05 +0800309 struct iwl_lq_sta *lq_data,
Johannes Berg4b7679a2008-09-18 18:14:18 +0200310 struct ieee80211_sta *sta)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200311{
312 if ((tid < TID_MAX_LOAD_COUNT))
313 rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
314 else if (tid == IWL_AGG_ALL_TID)
315 for (tid = 0; tid < TID_MAX_LOAD_COUNT; tid++)
316 rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
Wey-Yi Guy47eef9b2009-09-17 10:43:44 -0700317 if (priv->cfg->use_rts_for_ht) {
318 /*
319 * switch to RTS/CTS if it is the prefer protection method
320 * for HT traffic
321 */
322 IWL_DEBUG_HT(priv, "use RTS/CTS protection for HT\n");
323 priv->staging_rxon.flags &= ~RXON_FLG_SELF_CTS_EN;
324 iwlcore_commit_rxon(priv);
325 }
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200326}
327
Guy Cohen39e88502008-04-23 17:14:57 -0700328static inline int get_num_of_ant_from_rate(u32 rate_n_flags)
Guy Cohenfde0db32008-04-21 15:42:01 -0700329{
Tomas Winkler3ac7f142008-07-21 02:40:14 +0300330 return !!(rate_n_flags & RATE_MCS_ANT_A_MSK) +
331 !!(rate_n_flags & RATE_MCS_ANT_B_MSK) +
332 !!(rate_n_flags & RATE_MCS_ANT_C_MSK);
Guy Cohenfde0db32008-04-21 15:42:01 -0700333}
334
Ben Cahill77626352007-11-29 11:09:44 +0800335/**
336 * rs_collect_tx_data - Update the success/failure sliding window
337 *
338 * We keep a sliding window of the last 62 packets transmitted
339 * at this rate. window->data contains the bitmask of successful
340 * packets.
341 */
Tomas Winklere227cea2008-07-18 13:53:05 +0800342static int rs_collect_tx_data(struct iwl_rate_scale_data *windows,
Daniel C Halperine3949d62009-09-17 10:43:50 -0700343 int scale_index, s32 tpt, int attempts,
Ron Rindjunsky99556432008-01-28 14:07:25 +0200344 int successes)
Zhu Yib481de92007-09-25 17:54:57 -0700345{
Tomas Winklere227cea2008-07-18 13:53:05 +0800346 struct iwl_rate_scale_data *window = NULL;
Guy Cohen39e88502008-04-23 17:14:57 -0700347 static const u64 mask = (((u64)1) << (IWL_RATE_MAX_WINDOW - 1));
Zhu Yib481de92007-09-25 17:54:57 -0700348 s32 fail_count;
349
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800350 if (scale_index < 0 || scale_index >= IWL_RATE_COUNT)
351 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -0700352
Daniel C Halperine3949d62009-09-17 10:43:50 -0700353 /* Select window for current tx bit rate */
Zhu Yib481de92007-09-25 17:54:57 -0700354 window = &(windows[scale_index]);
355
Ben Cahill77626352007-11-29 11:09:44 +0800356 /*
357 * Keep track of only the latest 62 tx frame attempts in this rate's
358 * history window; anything older isn't really relevant any more.
359 * If we have filled up the sliding window, drop the oldest attempt;
360 * if the oldest attempt (highest bit in bitmap) shows "success",
361 * subtract "1" from the success counter (this is the main reason
362 * we keep these bitmaps!).
363 */
Daniel C Halperine3949d62009-09-17 10:43:50 -0700364 while (attempts > 0) {
Guy Cohen39e88502008-04-23 17:14:57 -0700365 if (window->counter >= IWL_RATE_MAX_WINDOW) {
366
367 /* remove earliest */
368 window->counter = IWL_RATE_MAX_WINDOW - 1;
369
Ron Rindjunsky99556432008-01-28 14:07:25 +0200370 if (window->data & mask) {
371 window->data &= ~mask;
Guy Cohen39e88502008-04-23 17:14:57 -0700372 window->success_counter--;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200373 }
Zhu Yib481de92007-09-25 17:54:57 -0700374 }
Zhu Yib481de92007-09-25 17:54:57 -0700375
Ron Rindjunsky99556432008-01-28 14:07:25 +0200376 /* Increment frames-attempted counter */
377 window->counter++;
Ben Cahill77626352007-11-29 11:09:44 +0800378
Daniel C Halperine3949d62009-09-17 10:43:50 -0700379 /* Shift bitmap by one frame to throw away oldest history */
Guy Cohen90d77952008-09-09 10:54:53 +0800380 window->data <<= 1;
Daniel C Halperine3949d62009-09-17 10:43:50 -0700381
382 /* Mark the most recent #successes attempts as successful */
Ron Rindjunsky99556432008-01-28 14:07:25 +0200383 if (successes > 0) {
Guy Cohen39e88502008-04-23 17:14:57 -0700384 window->success_counter++;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200385 window->data |= 0x1;
386 successes--;
387 }
388
Daniel C Halperine3949d62009-09-17 10:43:50 -0700389 attempts--;
Zhu Yib481de92007-09-25 17:54:57 -0700390 }
391
Ben Cahill77626352007-11-29 11:09:44 +0800392 /* Calculate current success ratio, avoid divide-by-0! */
Zhu Yib481de92007-09-25 17:54:57 -0700393 if (window->counter > 0)
394 window->success_ratio = 128 * (100 * window->success_counter)
395 / window->counter;
396 else
397 window->success_ratio = IWL_INVALID_VALUE;
398
399 fail_count = window->counter - window->success_counter;
400
Ben Cahill77626352007-11-29 11:09:44 +0800401 /* Calculate average throughput, if we have enough history. */
Zhu Yib481de92007-09-25 17:54:57 -0700402 if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
403 (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
404 window->average_tpt = (window->success_ratio * tpt + 64) / 128;
405 else
406 window->average_tpt = IWL_INVALID_VALUE;
407
Ben Cahill77626352007-11-29 11:09:44 +0800408 /* Tag this window as having been updated */
Zhu Yib481de92007-09-25 17:54:57 -0700409 window->stamp = jiffies;
410
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800411 return 0;
Zhu Yib481de92007-09-25 17:54:57 -0700412}
413
Ben Cahill77626352007-11-29 11:09:44 +0800414/*
415 * Fill uCode API rate_n_flags field, based on "search" or "active" table.
416 */
Guy Cohen39e88502008-04-23 17:14:57 -0700417/* FIXME:RS:remove this function and put the flags statically in the table */
Tomas Winkler978785a2008-12-19 10:37:31 +0800418static u32 rate_n_flags_from_tbl(struct iwl_priv *priv,
419 struct iwl_scale_tbl_info *tbl,
420 int index, u8 use_green)
Zhu Yib481de92007-09-25 17:54:57 -0700421{
Guy Cohen39e88502008-04-23 17:14:57 -0700422 u32 rate_n_flags = 0;
423
Zhu Yib481de92007-09-25 17:54:57 -0700424 if (is_legacy(tbl->lq_type)) {
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800425 rate_n_flags = iwl_rates[index].plcp;
Zhu Yib481de92007-09-25 17:54:57 -0700426 if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
Guy Cohen39e88502008-04-23 17:14:57 -0700427 rate_n_flags |= RATE_MCS_CCK_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700428
Guy Cohenfde0db32008-04-21 15:42:01 -0700429 } else if (is_Ht(tbl->lq_type)) {
430 if (index > IWL_LAST_OFDM_RATE) {
Tomas Winkler978785a2008-12-19 10:37:31 +0800431 IWL_ERR(priv, "Invalid HT rate index %d\n", index);
Zhu Yib481de92007-09-25 17:54:57 -0700432 index = IWL_LAST_OFDM_RATE;
Guy Cohenfde0db32008-04-21 15:42:01 -0700433 }
Guy Cohen39e88502008-04-23 17:14:57 -0700434 rate_n_flags = RATE_MCS_HT_MSK;
Guy Cohenfde0db32008-04-21 15:42:01 -0700435
436 if (is_siso(tbl->lq_type))
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800437 rate_n_flags |= iwl_rates[index].plcp_siso;
Guy Cohenfde0db32008-04-21 15:42:01 -0700438 else if (is_mimo2(tbl->lq_type))
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800439 rate_n_flags |= iwl_rates[index].plcp_mimo2;
Guy Cohenfde0db32008-04-21 15:42:01 -0700440 else
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800441 rate_n_flags |= iwl_rates[index].plcp_mimo3;
Zhu Yib481de92007-09-25 17:54:57 -0700442 } else {
Tomas Winkler978785a2008-12-19 10:37:31 +0800443 IWL_ERR(priv, "Invalid tbl->lq_type %d\n", tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -0700444 }
445
Guy Cohen39e88502008-04-23 17:14:57 -0700446 rate_n_flags |= ((tbl->ant_type << RATE_MCS_ANT_POS) &
Guy Cohenfde0db32008-04-21 15:42:01 -0700447 RATE_MCS_ANT_ABC_MSK);
Zhu Yib481de92007-09-25 17:54:57 -0700448
Guy Cohen39e88502008-04-23 17:14:57 -0700449 if (is_Ht(tbl->lq_type)) {
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700450 if (tbl->is_ht40) {
Guy Cohen39e88502008-04-23 17:14:57 -0700451 if (tbl->is_dup)
452 rate_n_flags |= RATE_MCS_DUP_MSK;
453 else
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700454 rate_n_flags |= RATE_MCS_HT40_MSK;
Guy Cohen39e88502008-04-23 17:14:57 -0700455 }
456 if (tbl->is_SGI)
457 rate_n_flags |= RATE_MCS_SGI_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700458
Guy Cohen39e88502008-04-23 17:14:57 -0700459 if (use_green) {
460 rate_n_flags |= RATE_MCS_GF_MSK;
461 if (is_siso(tbl->lq_type) && tbl->is_SGI) {
462 rate_n_flags &= ~RATE_MCS_SGI_MSK;
Tomas Winkler978785a2008-12-19 10:37:31 +0800463 IWL_ERR(priv, "GF was set with SGI:SISO\n");
Guy Cohen39e88502008-04-23 17:14:57 -0700464 }
465 }
Zhu Yib481de92007-09-25 17:54:57 -0700466 }
Guy Cohen39e88502008-04-23 17:14:57 -0700467 return rate_n_flags;
Zhu Yib481de92007-09-25 17:54:57 -0700468}
469
Ben Cahill77626352007-11-29 11:09:44 +0800470/*
471 * Interpret uCode API's rate_n_flags format,
472 * fill "search" or "active" tx mode table.
473 */
Guy Cohen39e88502008-04-23 17:14:57 -0700474static int rs_get_tbl_info_from_mcs(const u32 rate_n_flags,
Johannes Berg8318d782008-01-24 19:38:38 +0100475 enum ieee80211_band band,
Tomas Winklere227cea2008-07-18 13:53:05 +0800476 struct iwl_scale_tbl_info *tbl,
Zhu Yib481de92007-09-25 17:54:57 -0700477 int *rate_idx)
478{
Guy Cohen39e88502008-04-23 17:14:57 -0700479 u32 ant_msk = (rate_n_flags & RATE_MCS_ANT_ABC_MSK);
480 u8 num_of_ant = get_num_of_ant_from_rate(rate_n_flags);
481 u8 mcs;
Zhu Yib481de92007-09-25 17:54:57 -0700482
Tomas Winklere7d326a2008-06-12 09:47:11 +0800483 *rate_idx = iwl_hwrate_to_plcp_idx(rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -0700484
Guy Cohenfde0db32008-04-21 15:42:01 -0700485 if (*rate_idx == IWL_RATE_INVALID) {
Zhu Yib481de92007-09-25 17:54:57 -0700486 *rate_idx = -1;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800487 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -0700488 }
Ben Cahill77626352007-11-29 11:09:44 +0800489 tbl->is_SGI = 0; /* default legacy setup */
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700490 tbl->is_ht40 = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700491 tbl->is_dup = 0;
Guy Cohenfde0db32008-04-21 15:42:01 -0700492 tbl->ant_type = (ant_msk >> RATE_MCS_ANT_POS);
493 tbl->lq_type = LQ_NONE;
Mohamed Abbasd6e93392009-05-08 13:44:39 -0700494 tbl->max_search = IWL_MAX_SEARCH;
Zhu Yib481de92007-09-25 17:54:57 -0700495
Ben Cahill77626352007-11-29 11:09:44 +0800496 /* legacy rate format */
Guy Cohen39e88502008-04-23 17:14:57 -0700497 if (!(rate_n_flags & RATE_MCS_HT_MSK)) {
Guy Cohenfde0db32008-04-21 15:42:01 -0700498 if (num_of_ant == 1) {
Johannes Berg8318d782008-01-24 19:38:38 +0100499 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700500 tbl->lq_type = LQ_A;
501 else
502 tbl->lq_type = LQ_G;
Zhu Yib481de92007-09-25 17:54:57 -0700503 }
Guy Cohenfde0db32008-04-21 15:42:01 -0700504 /* HT rate format */
Zhu Yib481de92007-09-25 17:54:57 -0700505 } else {
Guy Cohen39e88502008-04-23 17:14:57 -0700506 if (rate_n_flags & RATE_MCS_SGI_MSK)
Zhu Yib481de92007-09-25 17:54:57 -0700507 tbl->is_SGI = 1;
508
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700509 if ((rate_n_flags & RATE_MCS_HT40_MSK) ||
Guy Cohen39e88502008-04-23 17:14:57 -0700510 (rate_n_flags & RATE_MCS_DUP_MSK))
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700511 tbl->is_ht40 = 1;
Zhu Yib481de92007-09-25 17:54:57 -0700512
Guy Cohen39e88502008-04-23 17:14:57 -0700513 if (rate_n_flags & RATE_MCS_DUP_MSK)
Zhu Yib481de92007-09-25 17:54:57 -0700514 tbl->is_dup = 1;
Guy Cohenfde0db32008-04-21 15:42:01 -0700515
Guy Cohen39e88502008-04-23 17:14:57 -0700516 mcs = rs_extract_rate(rate_n_flags);
Guy Cohenfde0db32008-04-21 15:42:01 -0700517
Guy Cohen39e88502008-04-23 17:14:57 -0700518 /* SISO */
519 if (mcs <= IWL_RATE_SISO_60M_PLCP) {
Guy Cohenfde0db32008-04-21 15:42:01 -0700520 if (num_of_ant == 1)
521 tbl->lq_type = LQ_SISO; /*else NONE*/
522 /* MIMO2 */
Guy Cohen39e88502008-04-23 17:14:57 -0700523 } else if (mcs <= IWL_RATE_MIMO2_60M_PLCP) {
Guy Cohenfde0db32008-04-21 15:42:01 -0700524 if (num_of_ant == 2)
525 tbl->lq_type = LQ_MIMO2;
526 /* MIMO3 */
527 } else {
Mohamed Abbasd6e93392009-05-08 13:44:39 -0700528 if (num_of_ant == 3) {
529 tbl->max_search = IWL_MAX_11N_MIMO3_SEARCH;
Guy Cohenfde0db32008-04-21 15:42:01 -0700530 tbl->lq_type = LQ_MIMO3;
Mohamed Abbasd6e93392009-05-08 13:44:39 -0700531 }
Guy Cohenfde0db32008-04-21 15:42:01 -0700532 }
Zhu Yib481de92007-09-25 17:54:57 -0700533 }
534 return 0;
535}
Guy Cohenaade00c2008-04-23 17:14:59 -0700536
537/* switch to another antenna/antennas and return 1 */
538/* if no other valid antenna found, return 0 */
539static int rs_toggle_antenna(u32 valid_ant, u32 *rate_n_flags,
Tomas Winklere227cea2008-07-18 13:53:05 +0800540 struct iwl_scale_tbl_info *tbl)
Zhu Yib481de92007-09-25 17:54:57 -0700541{
Guy Cohenaade00c2008-04-23 17:14:59 -0700542 u8 new_ant_type;
543
544 if (!tbl->ant_type || tbl->ant_type > ANT_ABC)
545 return 0;
546
547 if (!rs_is_valid_ant(valid_ant, tbl->ant_type))
548 return 0;
549
550 new_ant_type = ant_toggle_lookup[tbl->ant_type];
551
552 while ((new_ant_type != tbl->ant_type) &&
553 !rs_is_valid_ant(valid_ant, new_ant_type))
554 new_ant_type = ant_toggle_lookup[new_ant_type];
555
556 if (new_ant_type == tbl->ant_type)
557 return 0;
558
559 tbl->ant_type = new_ant_type;
560 *rate_n_flags &= ~RATE_MCS_ANT_ABC_MSK;
561 *rate_n_flags |= new_ant_type << RATE_MCS_ANT_POS;
562 return 1;
Zhu Yib481de92007-09-25 17:54:57 -0700563}
564
Daniel C Halperinb2617932009-08-13 13:30:59 -0700565/**
566 * Green-field mode is valid if the station supports it and
567 * there are no non-GF stations present in the BSS.
568 */
569static inline u8 rs_use_green(struct ieee80211_sta *sta,
Johannes Bergfad95bf2009-09-11 10:38:15 -0700570 struct iwl_ht_config *ht_conf)
Zhu Yib481de92007-09-25 17:54:57 -0700571{
Daniel C Halperinb2617932009-08-13 13:30:59 -0700572 return (sta->ht_cap.cap & IEEE80211_HT_CAP_GRN_FLD) &&
573 !(ht_conf->non_GF_STA_present);
Guy Cohenf935a6d2008-04-23 17:15:02 -0700574}
Zhu Yib481de92007-09-25 17:54:57 -0700575
576/**
577 * rs_get_supported_rates - get the available rates
578 *
579 * if management frame or broadcast frame only return
580 * basic available rates.
581 *
582 */
Tomas Winklere227cea2008-07-18 13:53:05 +0800583static u16 rs_get_supported_rates(struct iwl_lq_sta *lq_sta,
584 struct ieee80211_hdr *hdr,
585 enum iwl_table_type rate_type)
Zhu Yib481de92007-09-25 17:54:57 -0700586{
Zhu Yib481de92007-09-25 17:54:57 -0700587 if (hdr && is_multicast_ether_addr(hdr->addr1) &&
Guy Coheneecd6e52008-04-23 17:14:58 -0700588 lq_sta->active_rate_basic)
589 return lq_sta->active_rate_basic;
590
591 if (is_legacy(rate_type)) {
592 return lq_sta->active_legacy_rate;
593 } else {
594 if (is_siso(rate_type))
595 return lq_sta->active_siso_rate;
596 else if (is_mimo2(rate_type))
597 return lq_sta->active_mimo2_rate;
598 else
599 return lq_sta->active_mimo3_rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -0800600 }
Zhu Yib481de92007-09-25 17:54:57 -0700601}
602
Ester Kummerbf403db2008-05-05 10:22:40 +0800603static u16 rs_get_adjacent_rate(struct iwl_priv *priv, u8 index, u16 rate_mask,
604 int rate_type)
Zhu Yib481de92007-09-25 17:54:57 -0700605{
606 u8 high = IWL_RATE_INVALID;
607 u8 low = IWL_RATE_INVALID;
608
Ian Schram01ebd062007-10-25 17:15:22 +0800609 /* 802.11A or ht walks to the next literal adjacent rate in
Zhu Yib481de92007-09-25 17:54:57 -0700610 * the rate table */
611 if (is_a_band(rate_type) || !is_legacy(rate_type)) {
612 int i;
613 u32 mask;
614
615 /* Find the previous rate that is in the rate mask */
616 i = index - 1;
617 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
618 if (rate_mask & mask) {
619 low = i;
620 break;
621 }
622 }
623
624 /* Find the next rate that is in the rate mask */
625 i = index + 1;
626 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
627 if (rate_mask & mask) {
628 high = i;
629 break;
630 }
631 }
632
633 return (high << 8) | low;
634 }
635
636 low = index;
637 while (low != IWL_RATE_INVALID) {
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800638 low = iwl_rates[low].prev_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700639 if (low == IWL_RATE_INVALID)
640 break;
641 if (rate_mask & (1 << low))
642 break;
Tomas Winklere1623442009-01-27 14:27:56 -0800643 IWL_DEBUG_RATE(priv, "Skipping masked lower rate: %d\n", low);
Zhu Yib481de92007-09-25 17:54:57 -0700644 }
645
646 high = index;
647 while (high != IWL_RATE_INVALID) {
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800648 high = iwl_rates[high].next_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700649 if (high == IWL_RATE_INVALID)
650 break;
651 if (rate_mask & (1 << high))
652 break;
Tomas Winklere1623442009-01-27 14:27:56 -0800653 IWL_DEBUG_RATE(priv, "Skipping masked higher rate: %d\n", high);
Zhu Yib481de92007-09-25 17:54:57 -0700654 }
655
656 return (high << 8) | low;
657}
658
Tomas Winklere227cea2008-07-18 13:53:05 +0800659static u32 rs_get_lower_rate(struct iwl_lq_sta *lq_sta,
660 struct iwl_scale_tbl_info *tbl,
661 u8 scale_index, u8 ht_possible)
Zhu Yib481de92007-09-25 17:54:57 -0700662{
Zhu Yib481de92007-09-25 17:54:57 -0700663 s32 low;
664 u16 rate_mask;
665 u16 high_low;
666 u8 switch_to_legacy = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -0800667 u8 is_green = lq_sta->is_green;
Wey-Yi Guy2ff65782009-09-11 10:38:18 -0700668 struct iwl_priv *priv = lq_sta->drv;
Zhu Yib481de92007-09-25 17:54:57 -0700669
670 /* check if we need to switch from HT to legacy rates.
671 * assumption is that mandatory rates (1Mbps or 6Mbps)
672 * are always supported (spec demand) */
673 if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_index)) {
674 switch_to_legacy = 1;
675 scale_index = rs_ht_to_legacy[scale_index];
Johannes Berg8318d782008-01-24 19:38:38 +0100676 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700677 tbl->lq_type = LQ_A;
678 else
679 tbl->lq_type = LQ_G;
680
Guy Cohen69fdb302008-04-23 17:15:01 -0700681 if (num_of_ant(tbl->ant_type) > 1)
Wey-Yi Guy2ff65782009-09-11 10:38:18 -0700682 tbl->ant_type =
683 first_antenna(priv->hw_params.valid_tx_ant);
Zhu Yib481de92007-09-25 17:54:57 -0700684
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700685 tbl->is_ht40 = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700686 tbl->is_SGI = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -0700687 tbl->max_search = IWL_MAX_SEARCH;
Zhu Yib481de92007-09-25 17:54:57 -0700688 }
689
Guy Coheneecd6e52008-04-23 17:14:58 -0700690 rate_mask = rs_get_supported_rates(lq_sta, NULL, tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -0700691
Ben Cahill77626352007-11-29 11:09:44 +0800692 /* Mask with station rate restriction */
Zhu Yib481de92007-09-25 17:54:57 -0700693 if (is_legacy(tbl->lq_type)) {
Ben Cahill77626352007-11-29 11:09:44 +0800694 /* supp_rates has no CCK bits in A mode */
Johannes Berg8318d782008-01-24 19:38:38 +0100695 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700696 rate_mask = (u16)(rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -0800697 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
Zhu Yib481de92007-09-25 17:54:57 -0700698 else
Tomas Winklerc33104f2008-01-14 17:46:21 -0800699 rate_mask = (u16)(rate_mask & lq_sta->supp_rates);
Zhu Yib481de92007-09-25 17:54:57 -0700700 }
701
Ben Cahill77626352007-11-29 11:09:44 +0800702 /* If we switched from HT to legacy, check current rate */
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800703 if (switch_to_legacy && (rate_mask & (1 << scale_index))) {
Guy Cohen39e88502008-04-23 17:14:57 -0700704 low = scale_index;
705 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700706 }
707
Ester Kummerbf403db2008-05-05 10:22:40 +0800708 high_low = rs_get_adjacent_rate(lq_sta->drv, scale_index, rate_mask,
709 tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -0700710 low = high_low & 0xff;
711
Guy Cohen39e88502008-04-23 17:14:57 -0700712 if (low == IWL_RATE_INVALID)
713 low = scale_index;
714
715out:
Tomas Winkler978785a2008-12-19 10:37:31 +0800716 return rate_n_flags_from_tbl(lq_sta->drv, tbl, low, is_green);
Zhu Yib481de92007-09-25 17:54:57 -0700717}
718
Ben Cahill77626352007-11-29 11:09:44 +0800719/*
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700720 * Simple function to compare two rate scale table types
721 */
722static bool table_type_matches(struct iwl_scale_tbl_info *a,
723 struct iwl_scale_tbl_info *b)
724{
725 return (a->lq_type == b->lq_type) && (a->ant_type == b->ant_type) &&
726 (a->is_SGI == b->is_SGI);
727}
728/*
729 * Static function to get the expected throughput from an iwl_scale_tbl_info
730 * that wraps a NULL pointer check
731 */
732static s32 get_expected_tpt(struct iwl_scale_tbl_info *tbl, int rs_index)
733{
734 if (tbl->expected_tpt)
735 return tbl->expected_tpt[rs_index];
736 return 0;
737}
738
739/*
Ben Cahill77626352007-11-29 11:09:44 +0800740 * mac80211 sends us Tx status
741 */
Johannes Berg4b7679a2008-09-18 18:14:18 +0200742static void rs_tx_status(void *priv_r, struct ieee80211_supported_band *sband,
743 struct ieee80211_sta *sta, void *priv_sta,
Johannes Berge039fa42008-05-15 12:55:29 +0200744 struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -0700745{
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700746 int legacy_success;
747 int retries;
748 int rs_index, mac_index, i;
Tomas Winkler417f1142008-11-12 13:14:06 -0800749 struct iwl_lq_sta *lq_sta = priv_sta;
Tomas Winkler66c73db2008-04-15 16:01:40 -0700750 struct iwl_link_quality_cmd *table;
Zhu Yib481de92007-09-25 17:54:57 -0700751 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berg4b7679a2008-09-18 18:14:18 +0200752 struct iwl_priv *priv = (struct iwl_priv *)priv_r;
Johannes Berge039fa42008-05-15 12:55:29 +0200753 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Tomas Winklere227cea2008-07-18 13:53:05 +0800754 struct iwl_rate_scale_data *window = NULL;
Daniel C Halperin50273092009-08-28 09:44:45 -0700755 enum mac80211_rate_control_flags mac_flags;
Guy Cohen39e88502008-04-23 17:14:57 -0700756 u32 tx_rate;
Tomas Winklere227cea2008-07-18 13:53:05 +0800757 struct iwl_scale_tbl_info tbl_type;
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700758 struct iwl_scale_tbl_info *curr_tbl, *other_tbl;
Zhu Yib481de92007-09-25 17:54:57 -0700759 s32 tpt = 0;
760
Tomas Winklere1623442009-01-27 14:27:56 -0800761 IWL_DEBUG_RATE_LIMIT(priv, "get frame ack response, update rate scale window\n");
Zhu Yib481de92007-09-25 17:54:57 -0700762
Tomas Winkler417f1142008-11-12 13:14:06 -0800763 if (!ieee80211_is_data(hdr->frame_control) ||
Gábor Stefanik514d65c2009-04-23 19:36:08 +0200764 info->flags & IEEE80211_TX_CTL_NO_ACK)
Zhu Yib481de92007-09-25 17:54:57 -0700765 return;
766
Daniel C Halperine3949d62009-09-17 10:43:50 -0700767 /* This packet was aggregated but doesn't carry status info */
Johannes Berge039fa42008-05-15 12:55:29 +0200768 if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
769 !(info->flags & IEEE80211_TX_STAT_AMPDU))
Ron Rindjunsky99556432008-01-28 14:07:25 +0200770 return;
771
Johannes Berg05c914f2008-09-11 00:01:58 +0200772 if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) &&
Tomas Winklerc33104f2008-01-14 17:46:21 -0800773 !lq_sta->ibss_sta_added)
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700774 return;
Zhu Yib481de92007-09-25 17:54:57 -0700775
Ben Cahill77626352007-11-29 11:09:44 +0800776 /*
777 * Ignore this Tx frame response if its initial rate doesn't match
778 * that of latest Link Quality command. There may be stragglers
779 * from a previous Link Quality command, but we're no longer interested
780 * in those; they're either from the "active" mode while we're trying
781 * to check "search" mode, or a prior "search" mode after we've moved
782 * to a new "search" mode (which might become the new "active" mode).
783 */
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700784 table = &lq_sta->lq;
Guy Cohen39e88502008-04-23 17:14:57 -0700785 tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
786 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800787 if (priv->band == IEEE80211_BAND_5GHZ)
788 rs_index -= IWL_FIRST_OFDM_RATE;
Daniel C Halperin50273092009-08-28 09:44:45 -0700789 mac_flags = info->status.rates[0].flags;
790 mac_index = info->status.rates[0].idx;
Daniel C Halperin31513be2009-08-28 09:44:47 -0700791 /* For HT packets, map MCS to PLCP */
792 if (mac_flags & IEEE80211_TX_RC_MCS) {
793 mac_index &= RATE_MCS_CODE_MSK; /* Remove # of streams */
794 if (mac_index >= (IWL_RATE_9M_INDEX - IWL_FIRST_OFDM_RATE))
795 mac_index++;
Daniel C Halperin392a0ba2009-09-11 10:38:08 -0700796 /*
797 * mac80211 HT index is always zero-indexed; we need to move
798 * HT OFDM rates after CCK rates in 2.4 GHz band
799 */
800 if (priv->band == IEEE80211_BAND_2GHZ)
801 mac_index += IWL_FIRST_OFDM_RATE;
Daniel C Halperin31513be2009-08-28 09:44:47 -0700802 }
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700803 /* Here we actually compare this rate to the latest LQ command */
Daniel C Halperin50273092009-08-28 09:44:45 -0700804 if ((mac_index < 0) ||
805 (tbl_type.is_SGI != !!(mac_flags & IEEE80211_TX_RC_SHORT_GI)) ||
806 (tbl_type.is_ht40 != !!(mac_flags & IEEE80211_TX_RC_40_MHZ_WIDTH)) ||
807 (tbl_type.is_dup != !!(mac_flags & IEEE80211_TX_RC_DUP_DATA)) ||
Johannes Berge6a98542008-10-21 12:40:02 +0200808 (tbl_type.ant_type != info->antenna_sel_tx) ||
Daniel C Halperin50273092009-08-28 09:44:45 -0700809 (!!(tx_rate & RATE_MCS_HT_MSK) != !!(mac_flags & IEEE80211_TX_RC_MCS)) ||
810 (!!(tx_rate & RATE_MCS_GF_MSK) != !!(mac_flags & IEEE80211_TX_RC_GREEN_FIELD)) ||
Daniel C Halperin31513be2009-08-28 09:44:47 -0700811 (rs_index != mac_index)) {
812 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 -0700813 /*
814 * Since rates mis-match, the last LQ command may have failed.
815 * After IWL_MISSED_RATE_MAX mis-matches, resync the uCode with
816 * ... driver.
Mohamed Abbasd5e49032008-12-12 08:22:15 -0800817 */
Abbas, Mohamed7d049e52009-01-20 21:33:53 -0800818 lq_sta->missed_rate_counter++;
819 if (lq_sta->missed_rate_counter > IWL_MISSED_RATE_MAX) {
820 lq_sta->missed_rate_counter = 0;
821 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
822 }
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700823 /* Regardless, ignore this status info for outdated rate */
824 return;
825 } else
826 /* Rate did match, so reset the missed_rate_counter */
827 lq_sta->missed_rate_counter = 0;
828
829 /* Figure out if rate scale algorithm is in active or search table */
830 if (table_type_matches(&tbl_type,
831 &(lq_sta->lq_info[lq_sta->active_tbl]))) {
832 curr_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
833 other_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
834 } else if (table_type_matches(&tbl_type,
835 &lq_sta->lq_info[1 - lq_sta->active_tbl])) {
836 curr_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
837 other_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
838 } else {
839 IWL_DEBUG_RATE(priv, "Neither active nor search matches tx rate\n");
840 return;
Zhu Yib481de92007-09-25 17:54:57 -0700841 }
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700842 window = (struct iwl_rate_scale_data *)&(curr_tbl->win[0]);
Zhu Yib481de92007-09-25 17:54:57 -0700843
Ben Cahill77626352007-11-29 11:09:44 +0800844 /*
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700845 * Updating the frame history depends on whether packets were
846 * aggregated.
847 *
848 * For aggregation, all packets were transmitted at the same rate, the
849 * first index into rate scale table.
Ben Cahill77626352007-11-29 11:09:44 +0800850 */
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700851 if (info->flags & IEEE80211_TX_STAT_AMPDU) {
852 tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
853 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type,
854 &rs_index);
855 tpt = get_expected_tpt(curr_tbl, rs_index);
856 rs_collect_tx_data(window, rs_index, tpt,
857 info->status.ampdu_ack_len,
858 info->status.ampdu_ack_map);
Zhu Yib481de92007-09-25 17:54:57 -0700859
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700860 /* Update success/fail counts if not searching for new mode */
861 if (lq_sta->stay_in_tbl) {
Johannes Berge039fa42008-05-15 12:55:29 +0200862 lq_sta->total_success += info->status.ampdu_ack_map;
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700863 lq_sta->total_failed += (info->status.ampdu_ack_len -
864 info->status.ampdu_ack_map);
865 }
866 } else {
867 /*
868 * For legacy, update frame history with for each Tx retry.
869 */
870 retries = info->status.rates[0].count - 1;
871 /* HW doesn't send more than 15 retries */
872 retries = min(retries, 15);
873
874 /* The last transmission may have been successful */
875 legacy_success = !!(info->flags & IEEE80211_TX_STAT_ACK);
876 /* Collect data for each rate used during failed TX attempts */
877 for (i = 0; i <= retries; ++i) {
878 tx_rate = le32_to_cpu(table->rs_table[i].rate_n_flags);
879 rs_get_tbl_info_from_mcs(tx_rate, priv->band,
880 &tbl_type, &rs_index);
881 /*
882 * Only collect stats if retried rate is in the same RS
883 * table as active/search.
884 */
885 if (table_type_matches(&tbl_type, curr_tbl))
886 tpt = get_expected_tpt(curr_tbl, rs_index);
887 else if (table_type_matches(&tbl_type, other_tbl))
888 tpt = get_expected_tpt(other_tbl, rs_index);
Ron Rindjunsky99556432008-01-28 14:07:25 +0200889 else
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700890 continue;
891
892 /* Constants mean 1 transmission, 0 successes */
893 if (i < retries)
894 rs_collect_tx_data(window, rs_index, tpt, 1,
895 0);
896 else
897 rs_collect_tx_data(window, rs_index, tpt, 1,
898 legacy_success);
899 }
900
901 /* Update success/fail counts if not searching for new mode */
902 if (lq_sta->stay_in_tbl) {
903 lq_sta->total_success += legacy_success;
904 lq_sta->total_failed += retries + (1 - legacy_success);
Ron Rindjunsky99556432008-01-28 14:07:25 +0200905 }
Zhu Yib481de92007-09-25 17:54:57 -0700906 }
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700907 /* The last TX rate is cached in lq_sta; it's set in if/else above */
908 lq_sta->last_rate_n_flags = tx_rate;
Zhu Yib481de92007-09-25 17:54:57 -0700909
Ben Cahill77626352007-11-29 11:09:44 +0800910 /* See if there's a better rate or modulation mode to try. */
Abbas, Mohamedc338ba32009-01-21 10:58:02 -0800911 if (sta && sta->supp_rates[sband->band])
Gábor Stefanik514d65c2009-04-23 19:36:08 +0200912 rs_rate_scale_perform(priv, skb, sta, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -0700913}
914
Ben Cahill77626352007-11-29 11:09:44 +0800915/*
916 * Begin a period of staying with a selected modulation mode.
917 * Set "stay_in_tbl" flag to prevent any mode switches.
918 * Set frame tx success limits according to legacy vs. high-throughput,
919 * and reset overall (spanning all rates) tx success history statistics.
920 * These control how long we stay using same modulation mode before
921 * searching for a new mode.
922 */
Ester Kummerbf403db2008-05-05 10:22:40 +0800923static void rs_set_stay_in_table(struct iwl_priv *priv, u8 is_legacy,
Tomas Winklere227cea2008-07-18 13:53:05 +0800924 struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -0700925{
Tomas Winklere1623442009-01-27 14:27:56 -0800926 IWL_DEBUG_RATE(priv, "we are staying in the same table\n");
Tomas Winklerc33104f2008-01-14 17:46:21 -0800927 lq_sta->stay_in_tbl = 1; /* only place this gets set */
Zhu Yib481de92007-09-25 17:54:57 -0700928 if (is_legacy) {
Tomas Winklerc33104f2008-01-14 17:46:21 -0800929 lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT;
930 lq_sta->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
931 lq_sta->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
Zhu Yib481de92007-09-25 17:54:57 -0700932 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -0800933 lq_sta->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
934 lq_sta->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
935 lq_sta->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
Zhu Yib481de92007-09-25 17:54:57 -0700936 }
Tomas Winklerc33104f2008-01-14 17:46:21 -0800937 lq_sta->table_count = 0;
938 lq_sta->total_failed = 0;
939 lq_sta->total_success = 0;
Mohamed Abbas447fee72009-04-20 14:37:02 -0700940 lq_sta->flush_timer = jiffies;
Mohamed Abbasd6e93392009-05-08 13:44:39 -0700941 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700942}
943
Ben Cahill77626352007-11-29 11:09:44 +0800944/*
945 * Find correct throughput table for given mode of modulation
946 */
Tomas Winklere227cea2008-07-18 13:53:05 +0800947static void rs_set_expected_tpt_table(struct iwl_lq_sta *lq_sta,
948 struct iwl_scale_tbl_info *tbl)
Zhu Yib481de92007-09-25 17:54:57 -0700949{
Daniel C Halperine3949d62009-09-17 10:43:50 -0700950 /* Used to choose among HT tables */
951 s32 (*ht_tbl_pointer)[IWL_RATE_COUNT];
952
953 /* Check for invalid LQ type */
954 if (WARN_ON_ONCE(!is_legacy(tbl->lq_type) && !is_Ht(tbl->lq_type))) {
955 tbl->expected_tpt = expected_tpt_legacy;
956 return;
957 }
958
959 /* Legacy rates have only one table */
Zhu Yib481de92007-09-25 17:54:57 -0700960 if (is_legacy(tbl->lq_type)) {
Daniel C Halperine3949d62009-09-17 10:43:50 -0700961 tbl->expected_tpt = expected_tpt_legacy;
962 return;
963 }
964
965 /* Choose among many HT tables depending on number of streams
966 * (SISO/MIMO2/MIMO3), channel width (20/40), SGI, and aggregation
967 * status */
968 if (is_siso(tbl->lq_type) && (!tbl->is_ht40 || lq_sta->is_dup))
969 ht_tbl_pointer = expected_tpt_siso20MHz;
970 else if (is_siso(tbl->lq_type))
971 ht_tbl_pointer = expected_tpt_siso40MHz;
972 else if (is_mimo2(tbl->lq_type) && (!tbl->is_ht40 || lq_sta->is_dup))
973 ht_tbl_pointer = expected_tpt_mimo2_20MHz;
974 else if (is_mimo2(tbl->lq_type))
975 ht_tbl_pointer = expected_tpt_mimo2_40MHz;
976 else if (is_mimo3(tbl->lq_type) && (!tbl->is_ht40 || lq_sta->is_dup))
977 ht_tbl_pointer = expected_tpt_mimo3_20MHz;
978 else /* if (is_mimo3(tbl->lq_type)) <-- must be true */
979 ht_tbl_pointer = expected_tpt_mimo3_40MHz;
980
981 if (!tbl->is_SGI && !lq_sta->is_agg) /* Normal */
982 tbl->expected_tpt = ht_tbl_pointer[0];
983 else if (tbl->is_SGI && !lq_sta->is_agg) /* SGI */
984 tbl->expected_tpt = ht_tbl_pointer[1];
985 else if (!tbl->is_SGI && lq_sta->is_agg) /* AGG */
986 tbl->expected_tpt = ht_tbl_pointer[2];
987 else /* AGG+SGI */
988 tbl->expected_tpt = ht_tbl_pointer[3];
Zhu Yib481de92007-09-25 17:54:57 -0700989}
990
Ben Cahill77626352007-11-29 11:09:44 +0800991/*
992 * Find starting rate for new "search" high-throughput mode of modulation.
993 * Goal is to find lowest expected rate (under perfect conditions) that is
994 * above the current measured throughput of "active" mode, to give new mode
995 * a fair chance to prove itself without too many challenges.
996 *
997 * This gets called when transitioning to more aggressive modulation
998 * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
999 * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
1000 * to decrease to match "active" throughput. When moving from MIMO to SISO,
1001 * bit rate will typically need to increase, but not if performance was bad.
1002 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001003static s32 rs_get_best_rate(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001004 struct iwl_lq_sta *lq_sta,
1005 struct iwl_scale_tbl_info *tbl, /* "search" */
Guy Cohenf935a6d2008-04-23 17:15:02 -07001006 u16 rate_mask, s8 index)
Zhu Yib481de92007-09-25 17:54:57 -07001007{
Ben Cahill77626352007-11-29 11:09:44 +08001008 /* "active" values */
Tomas Winklere227cea2008-07-18 13:53:05 +08001009 struct iwl_scale_tbl_info *active_tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001010 &(lq_sta->lq_info[lq_sta->active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07001011 s32 active_sr = active_tbl->win[index].success_ratio;
Zhu Yib481de92007-09-25 17:54:57 -07001012 s32 active_tpt = active_tbl->expected_tpt[index];
Ben Cahill77626352007-11-29 11:09:44 +08001013
1014 /* expected "search" throughput */
1015 s32 *tpt_tbl = tbl->expected_tpt;
1016
1017 s32 new_rate, high, low, start_hi;
Zhu Yib481de92007-09-25 17:54:57 -07001018 u16 high_low;
Guy Cohenf935a6d2008-04-23 17:15:02 -07001019 s8 rate = index;
Zhu Yib481de92007-09-25 17:54:57 -07001020
1021 new_rate = high = low = start_hi = IWL_RATE_INVALID;
1022
1023 for (; ;) {
Ester Kummerbf403db2008-05-05 10:22:40 +08001024 high_low = rs_get_adjacent_rate(priv, rate, rate_mask,
1025 tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -07001026
1027 low = high_low & 0xff;
1028 high = (high_low >> 8) & 0xff;
1029
Ben Cahill77626352007-11-29 11:09:44 +08001030 /*
1031 * Lower the "search" bit rate, to give new "search" mode
1032 * approximately the same throughput as "active" if:
1033 *
1034 * 1) "Active" mode has been working modestly well (but not
1035 * great), and expected "search" throughput (under perfect
1036 * conditions) at candidate rate is above the actual
1037 * measured "active" throughput (but less than expected
1038 * "active" throughput under perfect conditions).
1039 * OR
1040 * 2) "Active" mode has been working perfectly or very well
1041 * and expected "search" throughput (under perfect
1042 * conditions) at candidate rate is above expected
1043 * "active" throughput (under perfect conditions).
1044 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001045 if ((((100 * tpt_tbl[rate]) > lq_sta->last_tpt) &&
Zhu Yib481de92007-09-25 17:54:57 -07001046 ((active_sr > IWL_RATE_DECREASE_TH) &&
1047 (active_sr <= IWL_RATE_HIGH_TH) &&
1048 (tpt_tbl[rate] <= active_tpt))) ||
1049 ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
1050 (tpt_tbl[rate] > active_tpt))) {
1051
Ben Cahill77626352007-11-29 11:09:44 +08001052 /* (2nd or later pass)
1053 * If we've already tried to raise the rate, and are
1054 * now trying to lower it, use the higher rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001055 if (start_hi != IWL_RATE_INVALID) {
1056 new_rate = start_hi;
1057 break;
1058 }
Ben Cahill77626352007-11-29 11:09:44 +08001059
Zhu Yib481de92007-09-25 17:54:57 -07001060 new_rate = rate;
Ben Cahill77626352007-11-29 11:09:44 +08001061
1062 /* Loop again with lower rate */
Zhu Yib481de92007-09-25 17:54:57 -07001063 if (low != IWL_RATE_INVALID)
1064 rate = low;
Ben Cahill77626352007-11-29 11:09:44 +08001065
1066 /* Lower rate not available, use the original */
Zhu Yib481de92007-09-25 17:54:57 -07001067 else
1068 break;
Ben Cahill77626352007-11-29 11:09:44 +08001069
1070 /* Else try to raise the "search" rate to match "active" */
Zhu Yib481de92007-09-25 17:54:57 -07001071 } else {
Ben Cahill77626352007-11-29 11:09:44 +08001072 /* (2nd or later pass)
1073 * If we've already tried to lower the rate, and are
1074 * now trying to raise it, use the lower rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001075 if (new_rate != IWL_RATE_INVALID)
1076 break;
Ben Cahill77626352007-11-29 11:09:44 +08001077
1078 /* Loop again with higher rate */
Zhu Yib481de92007-09-25 17:54:57 -07001079 else if (high != IWL_RATE_INVALID) {
1080 start_hi = high;
1081 rate = high;
Ben Cahill77626352007-11-29 11:09:44 +08001082
1083 /* Higher rate not available, use the original */
Zhu Yib481de92007-09-25 17:54:57 -07001084 } else {
Guy Cohen90d77952008-09-09 10:54:53 +08001085 new_rate = rate;
Zhu Yib481de92007-09-25 17:54:57 -07001086 break;
1087 }
1088 }
1089 }
1090
1091 return new_rate;
1092}
Zhu Yib481de92007-09-25 17:54:57 -07001093
Ben Cahill77626352007-11-29 11:09:44 +08001094/*
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001095 * Set up search table for MIMO2
Ben Cahill77626352007-11-29 11:09:44 +08001096 */
Guy Cohenfde0db32008-04-21 15:42:01 -07001097static int rs_switch_to_mimo2(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001098 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001099 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001100 struct ieee80211_sta *sta,
Tomas Winklere227cea2008-07-18 13:53:05 +08001101 struct iwl_scale_tbl_info *tbl, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001102{
Zhu Yib481de92007-09-25 17:54:57 -07001103 u16 rate_mask;
1104 s32 rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001105 s8 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07001106
Luis R. Rodriguezde27e642008-12-23 15:58:44 -08001107 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
Zhu Yib481de92007-09-25 17:54:57 -07001108 return -1;
1109
Johannes Bergd9fe60d2008-10-09 12:13:49 +02001110 if (((sta->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 2)
Tomas Winkler00c5ae22008-09-03 11:26:42 +08001111 == WLAN_HT_CAP_SM_PS_STATIC)
Zhu Yib481de92007-09-25 17:54:57 -07001112 return -1;
1113
Ben Cahill77626352007-11-29 11:09:44 +08001114 /* Need both Tx chains/antennas to support MIMO */
Guy Cohenaade00c2008-04-23 17:14:59 -07001115 if (priv->hw_params.tx_chains_num < 2)
Zhu Yib481de92007-09-25 17:54:57 -07001116 return -1;
1117
Tomas Winklere1623442009-01-27 14:27:56 -08001118 IWL_DEBUG_RATE(priv, "LQ: try to switch to MIMO2\n");
Guy Cohen39e88502008-04-23 17:14:57 -07001119
1120 tbl->lq_type = LQ_MIMO2;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001121 tbl->is_dup = lq_sta->is_dup;
Zhu Yib481de92007-09-25 17:54:57 -07001122 tbl->action = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001123 tbl->max_search = IWL_MAX_SEARCH;
Guy Cohen39e88502008-04-23 17:14:57 -07001124 rate_mask = lq_sta->active_mimo2_rate;
1125
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001126 if (iwl_is_ht40_tx_allowed(priv, &sta->ht_cap))
1127 tbl->is_ht40 = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001128 else
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001129 tbl->is_ht40 = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001130
Guy Coheneecd6e52008-04-23 17:14:58 -07001131 rs_set_expected_tpt_table(lq_sta, tbl);
Zhu Yib481de92007-09-25 17:54:57 -07001132
Guy Cohenf935a6d2008-04-23 17:15:02 -07001133 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
Zhu Yib481de92007-09-25 17:54:57 -07001134
Tomas Winklere1623442009-01-27 14:27:56 -08001135 IWL_DEBUG_RATE(priv, "LQ: MIMO2 best rate %d mask %X\n", rate, rate_mask);
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001136 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1137 IWL_DEBUG_RATE(priv, "Can't switch with index %d rate mask %x\n",
1138 rate, rate_mask);
1139 return -1;
1140 }
1141 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
Guy Cohen39e88502008-04-23 17:14:57 -07001142
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001143 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
1144 tbl->current_rate, is_green);
1145 return 0;
1146}
1147
1148/*
1149 * Set up search table for MIMO3
1150 */
1151static int rs_switch_to_mimo3(struct iwl_priv *priv,
1152 struct iwl_lq_sta *lq_sta,
1153 struct ieee80211_conf *conf,
1154 struct ieee80211_sta *sta,
1155 struct iwl_scale_tbl_info *tbl, int index)
1156{
1157 u16 rate_mask;
1158 s32 rate;
1159 s8 is_green = lq_sta->is_green;
1160
1161 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
1162 return -1;
1163
1164 if (((sta->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 2)
1165 == WLAN_HT_CAP_SM_PS_STATIC)
1166 return -1;
1167
1168 /* Need both Tx chains/antennas to support MIMO */
1169 if (priv->hw_params.tx_chains_num < 3)
1170 return -1;
1171
1172 IWL_DEBUG_RATE(priv, "LQ: try to switch to MIMO3\n");
1173
1174 tbl->lq_type = LQ_MIMO3;
1175 tbl->is_dup = lq_sta->is_dup;
1176 tbl->action = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001177 tbl->max_search = IWL_MAX_11N_MIMO3_SEARCH;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001178 rate_mask = lq_sta->active_mimo3_rate;
1179
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001180 if (iwl_is_ht40_tx_allowed(priv, &sta->ht_cap))
1181 tbl->is_ht40 = 1;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001182 else
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001183 tbl->is_ht40 = 0;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001184
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001185 rs_set_expected_tpt_table(lq_sta, tbl);
1186
1187 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
1188
1189 IWL_DEBUG_RATE(priv, "LQ: MIMO3 best rate %d mask %X\n",
1190 rate, rate_mask);
Guy Cohen39e88502008-04-23 17:14:57 -07001191 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
Tomas Winklere1623442009-01-27 14:27:56 -08001192 IWL_DEBUG_RATE(priv, "Can't switch with index %d rate mask %x\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001193 rate, rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07001194 return -1;
Guy Cohen39e88502008-04-23 17:14:57 -07001195 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001196 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
Zhu Yib481de92007-09-25 17:54:57 -07001197
Tomas Winklere1623442009-01-27 14:27:56 -08001198 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001199 tbl->current_rate, is_green);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001200 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07001201}
1202
Ben Cahill77626352007-11-29 11:09:44 +08001203/*
1204 * Set up search table for SISO
1205 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001206static int rs_switch_to_siso(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001207 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001208 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001209 struct ieee80211_sta *sta,
Tomas Winklere227cea2008-07-18 13:53:05 +08001210 struct iwl_scale_tbl_info *tbl, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001211{
Zhu Yib481de92007-09-25 17:54:57 -07001212 u16 rate_mask;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001213 u8 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07001214 s32 rate;
1215
Luis R. Rodriguezde27e642008-12-23 15:58:44 -08001216 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
Zhu Yib481de92007-09-25 17:54:57 -07001217 return -1;
1218
Tomas Winklere1623442009-01-27 14:27:56 -08001219 IWL_DEBUG_RATE(priv, "LQ: try to switch to SISO\n");
Guy Cohen39e88502008-04-23 17:14:57 -07001220
Tomas Winklerc33104f2008-01-14 17:46:21 -08001221 tbl->is_dup = lq_sta->is_dup;
Zhu Yib481de92007-09-25 17:54:57 -07001222 tbl->lq_type = LQ_SISO;
1223 tbl->action = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001224 tbl->max_search = IWL_MAX_SEARCH;
Guy Cohen39e88502008-04-23 17:14:57 -07001225 rate_mask = lq_sta->active_siso_rate;
Zhu Yib481de92007-09-25 17:54:57 -07001226
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001227 if (iwl_is_ht40_tx_allowed(priv, &sta->ht_cap))
1228 tbl->is_ht40 = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001229 else
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001230 tbl->is_ht40 = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001231
Zhu Yib481de92007-09-25 17:54:57 -07001232 if (is_green)
Guy Cohen39e88502008-04-23 17:14:57 -07001233 tbl->is_SGI = 0; /*11n spec: no SGI in SISO+Greenfield*/
Zhu Yib481de92007-09-25 17:54:57 -07001234
Guy Coheneecd6e52008-04-23 17:14:58 -07001235 rs_set_expected_tpt_table(lq_sta, tbl);
Guy Cohenf935a6d2008-04-23 17:15:02 -07001236 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
Zhu Yib481de92007-09-25 17:54:57 -07001237
Tomas Winklere1623442009-01-27 14:27:56 -08001238 IWL_DEBUG_RATE(priv, "LQ: get best rate %d mask %X\n", rate, rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07001239 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
Tomas Winklere1623442009-01-27 14:27:56 -08001240 IWL_DEBUG_RATE(priv, "can not switch with index %d rate mask %x\n",
Zhu Yib481de92007-09-25 17:54:57 -07001241 rate, rate_mask);
1242 return -1;
1243 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001244 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
Tomas Winklere1623442009-01-27 14:27:56 -08001245 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001246 tbl->current_rate, is_green);
Mohamed Abbas403ab562007-11-06 22:06:25 -08001247 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07001248}
1249
Ben Cahill77626352007-11-29 11:09:44 +08001250/*
1251 * Try to switch to new modulation mode from legacy
1252 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001253static int rs_move_legacy_other(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001254 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001255 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001256 struct ieee80211_sta *sta,
Zhu Yib481de92007-09-25 17:54:57 -07001257 int index)
1258{
Tomas Winklere227cea2008-07-18 13:53:05 +08001259 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1260 struct iwl_scale_tbl_info *search_tbl =
1261 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1262 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1263 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1264 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001265 u8 start_action = tbl->action;
Guy Cohenfde0db32008-04-21 15:42:01 -07001266 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
Guy Cohen3110bef2008-09-09 10:54:54 +08001267 u8 tx_chains_num = priv->hw_params.tx_chains_num;
Guy Cohenfde0db32008-04-21 15:42:01 -07001268 int ret = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001269 u8 update_search_tbl_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001270
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001271 if (!iwl_ht_enabled(priv))
1272 /* stay in Legacy */
1273 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
Johannes Berg3ad3b922009-08-07 15:41:48 -07001274 else if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001275 tbl->action > IWL_LEGACY_SWITCH_SISO)
1276 tbl->action = IWL_LEGACY_SWITCH_SISO;
Zhu Yib481de92007-09-25 17:54:57 -07001277 for (; ;) {
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001278 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001279 switch (tbl->action) {
Guy Cohen3110bef2008-09-09 10:54:54 +08001280 case IWL_LEGACY_SWITCH_ANTENNA1:
1281 case IWL_LEGACY_SWITCH_ANTENNA2:
Tomas Winklere1623442009-01-27 14:27:56 -08001282 IWL_DEBUG_RATE(priv, "LQ: Legacy toggle Antenna\n");
Zhu Yib481de92007-09-25 17:54:57 -07001283
Guy Cohen3110bef2008-09-09 10:54:54 +08001284 if ((tbl->action == IWL_LEGACY_SWITCH_ANTENNA1 &&
1285 tx_chains_num <= 1) ||
1286 (tbl->action == IWL_LEGACY_SWITCH_ANTENNA2 &&
1287 tx_chains_num <= 2))
1288 break;
1289
Ben Cahill77626352007-11-29 11:09:44 +08001290 /* Don't change antenna if success has been great */
Zhu Yib481de92007-09-25 17:54:57 -07001291 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1292 break;
Ben Cahill77626352007-11-29 11:09:44 +08001293
Ben Cahill77626352007-11-29 11:09:44 +08001294 /* Set up search table to try other antenna */
Zhu Yib481de92007-09-25 17:54:57 -07001295 memcpy(search_tbl, tbl, sz);
1296
Guy Cohenaade00c2008-04-23 17:14:59 -07001297 if (rs_toggle_antenna(valid_tx_ant,
1298 &search_tbl->current_rate, search_tbl)) {
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001299 update_search_tbl_counter = 1;
Guy Cohen3110bef2008-09-09 10:54:54 +08001300 rs_set_expected_tpt_table(lq_sta, search_tbl);
Guy Cohenaade00c2008-04-23 17:14:59 -07001301 goto out;
1302 }
Guy Cohena5e8b502008-05-29 16:35:11 +08001303 break;
Zhu Yib481de92007-09-25 17:54:57 -07001304 case IWL_LEGACY_SWITCH_SISO:
Tomas Winklere1623442009-01-27 14:27:56 -08001305 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to SISO\n");
Ben Cahill77626352007-11-29 11:09:44 +08001306
1307 /* Set up search table to try SISO */
Zhu Yib481de92007-09-25 17:54:57 -07001308 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001309 search_tbl->is_SGI = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001310 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001311 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001312 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001313 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001314 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001315 }
Zhu Yib481de92007-09-25 17:54:57 -07001316
1317 break;
Guy Cohen3110bef2008-09-09 10:54:54 +08001318 case IWL_LEGACY_SWITCH_MIMO2_AB:
1319 case IWL_LEGACY_SWITCH_MIMO2_AC:
1320 case IWL_LEGACY_SWITCH_MIMO2_BC:
Tomas Winklere1623442009-01-27 14:27:56 -08001321 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to MIMO2\n");
Ben Cahill77626352007-11-29 11:09:44 +08001322
1323 /* Set up search table to try MIMO */
Zhu Yib481de92007-09-25 17:54:57 -07001324 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001325 search_tbl->is_SGI = 0;
Guy Cohen3110bef2008-09-09 10:54:54 +08001326
1327 if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AB)
1328 search_tbl->ant_type = ANT_AB;
1329 else if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AC)
1330 search_tbl->ant_type = ANT_AC;
1331 else
1332 search_tbl->ant_type = ANT_BC;
1333
1334 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1335 break;
1336
Guy Cohenfde0db32008-04-21 15:42:01 -07001337 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001338 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001339 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001340 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001341 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001342 }
Zhu Yib481de92007-09-25 17:54:57 -07001343 break;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001344
1345 case IWL_LEGACY_SWITCH_MIMO3_ABC:
1346 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to MIMO3\n");
1347
1348 /* Set up search table to try MIMO3 */
1349 memcpy(search_tbl, tbl, sz);
1350 search_tbl->is_SGI = 0;
1351
1352 search_tbl->ant_type = ANT_ABC;
1353
1354 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1355 break;
1356
1357 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1358 search_tbl, index);
1359 if (!ret) {
1360 lq_sta->action_counter = 0;
1361 goto out;
1362 }
1363 break;
Zhu Yib481de92007-09-25 17:54:57 -07001364 }
1365 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001366 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001367 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
Zhu Yib481de92007-09-25 17:54:57 -07001368
1369 if (tbl->action == start_action)
1370 break;
1371
1372 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001373 search_tbl->lq_type = LQ_NONE;
Zhu Yib481de92007-09-25 17:54:57 -07001374 return 0;
1375
Guy Cohen3110bef2008-09-09 10:54:54 +08001376out:
1377 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001378 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001379 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001380 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001381 if (update_search_tbl_counter)
1382 search_tbl->action = tbl->action;
Zhu Yib481de92007-09-25 17:54:57 -07001383 return 0;
1384
1385}
1386
Ben Cahill77626352007-11-29 11:09:44 +08001387/*
1388 * Try to switch to new modulation mode from SISO
1389 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001390static int rs_move_siso_to_other(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001391 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001392 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001393 struct ieee80211_sta *sta, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001394{
Tomas Winklerc33104f2008-01-14 17:46:21 -08001395 u8 is_green = lq_sta->is_green;
Tomas Winklere227cea2008-07-18 13:53:05 +08001396 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1397 struct iwl_scale_tbl_info *search_tbl =
1398 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1399 struct iwl_rate_scale_data *window = &(tbl->win[index]);
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001400 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Tomas Winklere227cea2008-07-18 13:53:05 +08001401 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1402 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001403 u8 start_action = tbl->action;
Guy Cohenfde0db32008-04-21 15:42:01 -07001404 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
Guy Cohen3110bef2008-09-09 10:54:54 +08001405 u8 tx_chains_num = priv->hw_params.tx_chains_num;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001406 u8 update_search_tbl_counter = 0;
Guy Cohenfde0db32008-04-21 15:42:01 -07001407 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001408
Johannes Berg3ad3b922009-08-07 15:41:48 -07001409 if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001410 tbl->action > IWL_SISO_SWITCH_ANTENNA2) {
1411 /* stay in SISO */
1412 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1413 }
Zhu Yib481de92007-09-25 17:54:57 -07001414 for (;;) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001415 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001416 switch (tbl->action) {
Guy Cohen3110bef2008-09-09 10:54:54 +08001417 case IWL_SISO_SWITCH_ANTENNA1:
1418 case IWL_SISO_SWITCH_ANTENNA2:
Tomas Winklere1623442009-01-27 14:27:56 -08001419 IWL_DEBUG_RATE(priv, "LQ: SISO toggle Antenna\n");
Guy Cohen3110bef2008-09-09 10:54:54 +08001420
1421 if ((tbl->action == IWL_SISO_SWITCH_ANTENNA1 &&
1422 tx_chains_num <= 1) ||
1423 (tbl->action == IWL_SISO_SWITCH_ANTENNA2 &&
1424 tx_chains_num <= 2))
1425 break;
1426
Zhu Yib481de92007-09-25 17:54:57 -07001427 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1428 break;
Zhu Yib481de92007-09-25 17:54:57 -07001429
1430 memcpy(search_tbl, tbl, sz);
Guy Cohenaade00c2008-04-23 17:14:59 -07001431 if (rs_toggle_antenna(valid_tx_ant,
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001432 &search_tbl->current_rate, search_tbl)) {
1433 update_search_tbl_counter = 1;
Guy Cohenaade00c2008-04-23 17:14:59 -07001434 goto out;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001435 }
Guy Cohena5e8b502008-05-29 16:35:11 +08001436 break;
Guy Cohen3110bef2008-09-09 10:54:54 +08001437 case IWL_SISO_SWITCH_MIMO2_AB:
1438 case IWL_SISO_SWITCH_MIMO2_AC:
1439 case IWL_SISO_SWITCH_MIMO2_BC:
Tomas Winklere1623442009-01-27 14:27:56 -08001440 IWL_DEBUG_RATE(priv, "LQ: SISO switch to MIMO2\n");
Zhu Yib481de92007-09-25 17:54:57 -07001441 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001442 search_tbl->is_SGI = 0;
Guy Cohen3110bef2008-09-09 10:54:54 +08001443
1444 if (tbl->action == IWL_SISO_SWITCH_MIMO2_AB)
1445 search_tbl->ant_type = ANT_AB;
1446 else if (tbl->action == IWL_SISO_SWITCH_MIMO2_AC)
1447 search_tbl->ant_type = ANT_AC;
1448 else
1449 search_tbl->ant_type = ANT_BC;
1450
1451 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1452 break;
1453
Guy Cohenfde0db32008-04-21 15:42:01 -07001454 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001455 search_tbl, index);
Guy Cohen3110bef2008-09-09 10:54:54 +08001456 if (!ret)
Zhu Yib481de92007-09-25 17:54:57 -07001457 goto out;
1458 break;
1459 case IWL_SISO_SWITCH_GI:
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001460 if (!tbl->is_ht40 && !(ht_cap->cap &
1461 IEEE80211_HT_CAP_SGI_20))
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001462 break;
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001463 if (tbl->is_ht40 && !(ht_cap->cap &
1464 IEEE80211_HT_CAP_SGI_40))
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001465 break;
1466
Tomas Winklere1623442009-01-27 14:27:56 -08001467 IWL_DEBUG_RATE(priv, "LQ: SISO toggle SGI/NGI\n");
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001468
Zhu Yib481de92007-09-25 17:54:57 -07001469 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001470 if (is_green) {
1471 if (!tbl->is_SGI)
1472 break;
1473 else
Winkler, Tomas15b16872008-12-19 10:37:33 +08001474 IWL_ERR(priv,
1475 "SGI was set in GF+SISO\n");
Zhu Yib481de92007-09-25 17:54:57 -07001476 }
Guy Cohen39e88502008-04-23 17:14:57 -07001477 search_tbl->is_SGI = !tbl->is_SGI;
Guy Coheneecd6e52008-04-23 17:14:58 -07001478 rs_set_expected_tpt_table(lq_sta, search_tbl);
Guy Cohen39e88502008-04-23 17:14:57 -07001479 if (tbl->is_SGI) {
1480 s32 tpt = lq_sta->last_tpt / 100;
1481 if (tpt >= search_tbl->expected_tpt[index])
1482 break;
1483 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001484 search_tbl->current_rate =
1485 rate_n_flags_from_tbl(priv, search_tbl,
1486 index, is_green);
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001487 update_search_tbl_counter = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001488 goto out;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001489 case IWL_SISO_SWITCH_MIMO3_ABC:
1490 IWL_DEBUG_RATE(priv, "LQ: SISO switch to MIMO3\n");
1491 memcpy(search_tbl, tbl, sz);
1492 search_tbl->is_SGI = 0;
1493 search_tbl->ant_type = ANT_ABC;
1494
1495 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1496 break;
1497
1498 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1499 search_tbl, index);
1500 if (!ret)
1501 goto out;
1502 break;
Zhu Yib481de92007-09-25 17:54:57 -07001503 }
1504 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001505 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001506 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
Zhu Yib481de92007-09-25 17:54:57 -07001507
1508 if (tbl->action == start_action)
1509 break;
1510 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001511 search_tbl->lq_type = LQ_NONE;
Zhu Yib481de92007-09-25 17:54:57 -07001512 return 0;
1513
1514 out:
Guy Cohen3110bef2008-09-09 10:54:54 +08001515 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001516 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001517 if (tbl->action > IWL_SISO_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001518 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001519 if (update_search_tbl_counter)
1520 search_tbl->action = tbl->action;
1521
Zhu Yib481de92007-09-25 17:54:57 -07001522 return 0;
1523}
1524
Ben Cahill77626352007-11-29 11:09:44 +08001525/*
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001526 * Try to switch to new modulation mode from MIMO2
Ben Cahill77626352007-11-29 11:09:44 +08001527 */
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001528static int rs_move_mimo2_to_other(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001529 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001530 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001531 struct ieee80211_sta *sta, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001532{
Tomas Winklerc33104f2008-01-14 17:46:21 -08001533 s8 is_green = lq_sta->is_green;
Tomas Winklere227cea2008-07-18 13:53:05 +08001534 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1535 struct iwl_scale_tbl_info *search_tbl =
1536 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Guy Cohen3110bef2008-09-09 10:54:54 +08001537 struct iwl_rate_scale_data *window = &(tbl->win[index]);
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001538 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Tomas Winklere227cea2008-07-18 13:53:05 +08001539 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1540 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001541 u8 start_action = tbl->action;
Guy Cohen3110bef2008-09-09 10:54:54 +08001542 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1543 u8 tx_chains_num = priv->hw_params.tx_chains_num;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001544 u8 update_search_tbl_counter = 0;
Guy Cohenaade00c2008-04-23 17:14:59 -07001545 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001546
Johannes Berg3ad3b922009-08-07 15:41:48 -07001547 if ((iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE) &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001548 (tbl->action < IWL_MIMO2_SWITCH_SISO_A ||
1549 tbl->action > IWL_MIMO2_SWITCH_SISO_C)) {
1550 /* switch in SISO */
1551 tbl->action = IWL_MIMO2_SWITCH_SISO_A;
1552 }
Zhu Yib481de92007-09-25 17:54:57 -07001553 for (;;) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001554 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001555 switch (tbl->action) {
Guy Cohen3110bef2008-09-09 10:54:54 +08001556 case IWL_MIMO2_SWITCH_ANTENNA1:
1557 case IWL_MIMO2_SWITCH_ANTENNA2:
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001558 IWL_DEBUG_RATE(priv, "LQ: MIMO2 toggle Antennas\n");
Guy Cohen3110bef2008-09-09 10:54:54 +08001559
1560 if (tx_chains_num <= 2)
1561 break;
1562
1563 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1564 break;
1565
1566 memcpy(search_tbl, tbl, sz);
1567 if (rs_toggle_antenna(valid_tx_ant,
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001568 &search_tbl->current_rate, search_tbl)) {
1569 update_search_tbl_counter = 1;
Guy Cohen3110bef2008-09-09 10:54:54 +08001570 goto out;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001571 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001572 break;
1573 case IWL_MIMO2_SWITCH_SISO_A:
1574 case IWL_MIMO2_SWITCH_SISO_B:
1575 case IWL_MIMO2_SWITCH_SISO_C:
Tomas Winklere1623442009-01-27 14:27:56 -08001576 IWL_DEBUG_RATE(priv, "LQ: MIMO2 switch to SISO\n");
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001577
Ben Cahill77626352007-11-29 11:09:44 +08001578 /* Set up new search table for SISO */
Zhu Yib481de92007-09-25 17:54:57 -07001579 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001580
Guy Cohen3110bef2008-09-09 10:54:54 +08001581 if (tbl->action == IWL_MIMO2_SWITCH_SISO_A)
Guy Cohenfde0db32008-04-21 15:42:01 -07001582 search_tbl->ant_type = ANT_A;
Guy Cohen3110bef2008-09-09 10:54:54 +08001583 else if (tbl->action == IWL_MIMO2_SWITCH_SISO_B)
Guy Cohenfde0db32008-04-21 15:42:01 -07001584 search_tbl->ant_type = ANT_B;
Guy Cohen3110bef2008-09-09 10:54:54 +08001585 else
1586 search_tbl->ant_type = ANT_C;
1587
1588 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1589 break;
Zhu Yib481de92007-09-25 17:54:57 -07001590
Tomas Winklerc33104f2008-01-14 17:46:21 -08001591 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001592 search_tbl, index);
Guy Cohen3110bef2008-09-09 10:54:54 +08001593 if (!ret)
Zhu Yib481de92007-09-25 17:54:57 -07001594 goto out;
Guy Cohen3110bef2008-09-09 10:54:54 +08001595
Zhu Yib481de92007-09-25 17:54:57 -07001596 break;
1597
Guy Cohen3110bef2008-09-09 10:54:54 +08001598 case IWL_MIMO2_SWITCH_GI:
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001599 if (!tbl->is_ht40 && !(ht_cap->cap &
1600 IEEE80211_HT_CAP_SGI_20))
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001601 break;
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001602 if (tbl->is_ht40 && !(ht_cap->cap &
1603 IEEE80211_HT_CAP_SGI_40))
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001604 break;
1605
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001606 IWL_DEBUG_RATE(priv, "LQ: MIMO2 toggle SGI/NGI\n");
1607
1608 /* Set up new search table for MIMO2 */
1609 memcpy(search_tbl, tbl, sz);
1610 search_tbl->is_SGI = !tbl->is_SGI;
1611 rs_set_expected_tpt_table(lq_sta, search_tbl);
1612 /*
1613 * If active table already uses the fastest possible
1614 * modulation (dual stream with short guard interval),
1615 * and it's working well, there's no need to look
1616 * for a better type of modulation!
1617 */
1618 if (tbl->is_SGI) {
1619 s32 tpt = lq_sta->last_tpt / 100;
1620 if (tpt >= search_tbl->expected_tpt[index])
1621 break;
1622 }
1623 search_tbl->current_rate =
1624 rate_n_flags_from_tbl(priv, search_tbl,
1625 index, is_green);
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001626 update_search_tbl_counter = 1;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001627 goto out;
1628
1629 case IWL_MIMO2_SWITCH_MIMO3_ABC:
1630 IWL_DEBUG_RATE(priv, "LQ: MIMO2 switch to MIMO3\n");
1631 memcpy(search_tbl, tbl, sz);
1632 search_tbl->is_SGI = 0;
1633 search_tbl->ant_type = ANT_ABC;
1634
1635 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1636 break;
1637
1638 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1639 search_tbl, index);
1640 if (!ret)
1641 goto out;
1642
1643 break;
1644 }
1645 tbl->action++;
1646 if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
1647 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
1648
1649 if (tbl->action == start_action)
1650 break;
1651 }
1652 search_tbl->lq_type = LQ_NONE;
1653 return 0;
1654 out:
1655 lq_sta->search_better_tbl = 1;
1656 tbl->action++;
1657 if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
1658 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001659 if (update_search_tbl_counter)
1660 search_tbl->action = tbl->action;
1661
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001662 return 0;
1663
1664}
1665
1666/*
1667 * Try to switch to new modulation mode from MIMO3
1668 */
1669static int rs_move_mimo3_to_other(struct iwl_priv *priv,
1670 struct iwl_lq_sta *lq_sta,
1671 struct ieee80211_conf *conf,
1672 struct ieee80211_sta *sta, int index)
1673{
1674 s8 is_green = lq_sta->is_green;
1675 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1676 struct iwl_scale_tbl_info *search_tbl =
1677 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1678 struct iwl_rate_scale_data *window = &(tbl->win[index]);
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001679 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001680 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1681 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1682 u8 start_action = tbl->action;
1683 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1684 u8 tx_chains_num = priv->hw_params.tx_chains_num;
1685 int ret;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001686 u8 update_search_tbl_counter = 0;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001687
Johannes Berg3ad3b922009-08-07 15:41:48 -07001688 if ((iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE) &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001689 (tbl->action < IWL_MIMO3_SWITCH_SISO_A ||
1690 tbl->action > IWL_MIMO3_SWITCH_SISO_C)) {
1691 /* switch in SISO */
1692 tbl->action = IWL_MIMO3_SWITCH_SISO_A;
1693 }
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001694 for (;;) {
1695 lq_sta->action_counter++;
1696 switch (tbl->action) {
1697 case IWL_MIMO3_SWITCH_ANTENNA1:
1698 case IWL_MIMO3_SWITCH_ANTENNA2:
1699 IWL_DEBUG_RATE(priv, "LQ: MIMO3 toggle Antennas\n");
1700
1701 if (tx_chains_num <= 3)
1702 break;
1703
1704 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1705 break;
1706
1707 memcpy(search_tbl, tbl, sz);
1708 if (rs_toggle_antenna(valid_tx_ant,
1709 &search_tbl->current_rate, search_tbl))
1710 goto out;
1711 break;
1712 case IWL_MIMO3_SWITCH_SISO_A:
1713 case IWL_MIMO3_SWITCH_SISO_B:
1714 case IWL_MIMO3_SWITCH_SISO_C:
1715 IWL_DEBUG_RATE(priv, "LQ: MIMO3 switch to SISO\n");
1716
1717 /* Set up new search table for SISO */
1718 memcpy(search_tbl, tbl, sz);
1719
1720 if (tbl->action == IWL_MIMO3_SWITCH_SISO_A)
1721 search_tbl->ant_type = ANT_A;
1722 else if (tbl->action == IWL_MIMO3_SWITCH_SISO_B)
1723 search_tbl->ant_type = ANT_B;
1724 else
1725 search_tbl->ant_type = ANT_C;
1726
1727 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1728 break;
1729
1730 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
1731 search_tbl, index);
1732 if (!ret)
1733 goto out;
1734
1735 break;
1736
1737 case IWL_MIMO3_SWITCH_MIMO2_AB:
1738 case IWL_MIMO3_SWITCH_MIMO2_AC:
1739 case IWL_MIMO3_SWITCH_MIMO2_BC:
1740 IWL_DEBUG_RATE(priv, "LQ: MIMO3 switch to MIMO2\n");
1741
1742 memcpy(search_tbl, tbl, sz);
1743 search_tbl->is_SGI = 0;
1744 if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AB)
1745 search_tbl->ant_type = ANT_AB;
1746 else if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AC)
1747 search_tbl->ant_type = ANT_AC;
1748 else
1749 search_tbl->ant_type = ANT_BC;
1750
1751 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1752 break;
1753
1754 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
1755 search_tbl, index);
1756 if (!ret)
1757 goto out;
1758
1759 break;
1760
1761 case IWL_MIMO3_SWITCH_GI:
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001762 if (!tbl->is_ht40 && !(ht_cap->cap &
1763 IEEE80211_HT_CAP_SGI_20))
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001764 break;
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001765 if (tbl->is_ht40 && !(ht_cap->cap &
1766 IEEE80211_HT_CAP_SGI_40))
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001767 break;
1768
1769 IWL_DEBUG_RATE(priv, "LQ: MIMO3 toggle SGI/NGI\n");
Ben Cahill77626352007-11-29 11:09:44 +08001770
1771 /* Set up new search table for MIMO */
Zhu Yib481de92007-09-25 17:54:57 -07001772 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001773 search_tbl->is_SGI = !tbl->is_SGI;
Guy Coheneecd6e52008-04-23 17:14:58 -07001774 rs_set_expected_tpt_table(lq_sta, search_tbl);
Ben Cahill77626352007-11-29 11:09:44 +08001775 /*
1776 * If active table already uses the fastest possible
1777 * modulation (dual stream with short guard interval),
1778 * and it's working well, there's no need to look
1779 * for a better type of modulation!
1780 */
Guy Cohen39e88502008-04-23 17:14:57 -07001781 if (tbl->is_SGI) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001782 s32 tpt = lq_sta->last_tpt / 100;
Guy Cohen39e88502008-04-23 17:14:57 -07001783 if (tpt >= search_tbl->expected_tpt[index])
1784 break;
Zhu Yib481de92007-09-25 17:54:57 -07001785 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001786 search_tbl->current_rate =
1787 rate_n_flags_from_tbl(priv, search_tbl,
1788 index, is_green);
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001789 update_search_tbl_counter = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001790 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07001791 }
1792 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001793 if (tbl->action > IWL_MIMO3_SWITCH_GI)
1794 tbl->action = IWL_MIMO3_SWITCH_ANTENNA1;
Zhu Yib481de92007-09-25 17:54:57 -07001795
1796 if (tbl->action == start_action)
1797 break;
1798 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001799 search_tbl->lq_type = LQ_NONE;
Zhu Yib481de92007-09-25 17:54:57 -07001800 return 0;
1801 out:
Guy Cohen3110bef2008-09-09 10:54:54 +08001802 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001803 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001804 if (tbl->action > IWL_MIMO3_SWITCH_GI)
1805 tbl->action = IWL_MIMO3_SWITCH_ANTENNA1;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001806 if (update_search_tbl_counter)
1807 search_tbl->action = tbl->action;
1808
Zhu Yib481de92007-09-25 17:54:57 -07001809 return 0;
1810
1811}
1812
Ben Cahill77626352007-11-29 11:09:44 +08001813/*
1814 * Check whether we should continue using same modulation mode, or
1815 * begin search for a new mode, based on:
1816 * 1) # tx successes or failures while using this mode
1817 * 2) # times calling this function
1818 * 3) elapsed time in this mode (not used, for now)
1819 */
Tomas Winklere227cea2008-07-18 13:53:05 +08001820static void rs_stay_in_table(struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07001821{
Tomas Winklere227cea2008-07-18 13:53:05 +08001822 struct iwl_scale_tbl_info *tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001823 int i;
1824 int active_tbl;
1825 int flush_interval_passed = 0;
Ester Kummerbf403db2008-05-05 10:22:40 +08001826 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07001827
Ester Kummerbf403db2008-05-05 10:22:40 +08001828 priv = lq_sta->drv;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001829 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001830
Tomas Winklerc33104f2008-01-14 17:46:21 -08001831 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07001832
Ben Cahill77626352007-11-29 11:09:44 +08001833 /* If we've been disallowing search, see if we should now allow it */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001834 if (lq_sta->stay_in_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07001835
Ben Cahill77626352007-11-29 11:09:44 +08001836 /* Elapsed time using current modulation mode */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001837 if (lq_sta->flush_timer)
Zhu Yib481de92007-09-25 17:54:57 -07001838 flush_interval_passed =
Mohamed Abbas447fee72009-04-20 14:37:02 -07001839 time_after(jiffies,
1840 (unsigned long)(lq_sta->flush_timer +
Zhu Yib481de92007-09-25 17:54:57 -07001841 IWL_RATE_SCALE_FLUSH_INTVL));
1842
Ben Cahill77626352007-11-29 11:09:44 +08001843 /*
1844 * Check if we should allow search for new modulation mode.
1845 * If many frames have failed or succeeded, or we've used
1846 * this same modulation for a long time, allow search, and
1847 * reset history stats that keep track of whether we should
1848 * allow a new search. Also (below) reset all bitmaps and
1849 * stats in active history.
1850 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001851 if ((lq_sta->total_failed > lq_sta->max_failure_limit) ||
1852 (lq_sta->total_success > lq_sta->max_success_limit) ||
1853 ((!lq_sta->search_better_tbl) && (lq_sta->flush_timer)
Zhu Yib481de92007-09-25 17:54:57 -07001854 && (flush_interval_passed))) {
Tomas Winklere1623442009-01-27 14:27:56 -08001855 IWL_DEBUG_RATE(priv, "LQ: stay is expired %d %d %d\n:",
Tomas Winklerc33104f2008-01-14 17:46:21 -08001856 lq_sta->total_failed,
1857 lq_sta->total_success,
Zhu Yib481de92007-09-25 17:54:57 -07001858 flush_interval_passed);
Ben Cahill77626352007-11-29 11:09:44 +08001859
1860 /* Allow search for new mode */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001861 lq_sta->stay_in_tbl = 0; /* only place reset */
1862 lq_sta->total_failed = 0;
1863 lq_sta->total_success = 0;
1864 lq_sta->flush_timer = 0;
Ben Cahill77626352007-11-29 11:09:44 +08001865
1866 /*
1867 * Else if we've used this modulation mode enough repetitions
1868 * (regardless of elapsed time or success/failure), reset
1869 * history bitmaps and rate-specific stats for all rates in
1870 * active table.
1871 */
Mohamed Abbas403ab562007-11-06 22:06:25 -08001872 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001873 lq_sta->table_count++;
1874 if (lq_sta->table_count >=
1875 lq_sta->table_count_limit) {
1876 lq_sta->table_count = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001877
Tomas Winklere1623442009-01-27 14:27:56 -08001878 IWL_DEBUG_RATE(priv, "LQ: stay in table clear win\n");
Zhu Yib481de92007-09-25 17:54:57 -07001879 for (i = 0; i < IWL_RATE_COUNT; i++)
1880 rs_rate_scale_clear_window(
1881 &(tbl->win[i]));
1882 }
1883 }
1884
Ben Cahill77626352007-11-29 11:09:44 +08001885 /* If transitioning to allow "search", reset all history
1886 * bitmaps and stats in active table (this will become the new
1887 * "search" table). */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001888 if (!lq_sta->stay_in_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07001889 for (i = 0; i < IWL_RATE_COUNT; i++)
1890 rs_rate_scale_clear_window(&(tbl->win[i]));
1891 }
1892 }
1893}
1894
Ben Cahill77626352007-11-29 11:09:44 +08001895/*
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07001896 * setup rate table in uCode
1897 * return rate_n_flags as used in the table
1898 */
1899static u32 rs_update_rate_tbl(struct iwl_priv *priv,
1900 struct iwl_lq_sta *lq_sta,
1901 struct iwl_scale_tbl_info *tbl,
1902 int index, u8 is_green)
1903{
1904 u32 rate;
1905
1906 /* Update uCode's rate table. */
1907 rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
1908 rs_fill_link_cmd(priv, lq_sta, rate);
1909 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
1910
1911 return rate;
1912}
1913
1914/*
Ben Cahill77626352007-11-29 11:09:44 +08001915 * Do rate scaling and search for new modulation mode.
1916 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001917static void rs_rate_scale_perform(struct iwl_priv *priv,
Gábor Stefanik514d65c2009-04-23 19:36:08 +02001918 struct sk_buff *skb,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001919 struct ieee80211_sta *sta,
1920 struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07001921{
Johannes Berg4b7679a2008-09-18 18:14:18 +02001922 struct ieee80211_hw *hw = priv->hw;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001923 struct ieee80211_conf *conf = &hw->conf;
Gábor Stefanik514d65c2009-04-23 19:36:08 +02001924 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1925 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001926 int low = IWL_RATE_INVALID;
1927 int high = IWL_RATE_INVALID;
1928 int index;
1929 int i;
Tomas Winklere227cea2008-07-18 13:53:05 +08001930 struct iwl_rate_scale_data *window = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001931 int current_tpt = IWL_INVALID_VALUE;
1932 int low_tpt = IWL_INVALID_VALUE;
1933 int high_tpt = IWL_INVALID_VALUE;
1934 u32 fail_count;
1935 s8 scale_action = 0;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07001936 u16 rate_mask;
Zhu Yib481de92007-09-25 17:54:57 -07001937 u8 update_lq = 0;
Tomas Winklere227cea2008-07-18 13:53:05 +08001938 struct iwl_scale_tbl_info *tbl, *tbl1;
Zhu Yib481de92007-09-25 17:54:57 -07001939 u16 rate_scale_index_msk = 0;
Guy Cohen39e88502008-04-23 17:14:57 -07001940 u32 rate;
Zhu Yib481de92007-09-25 17:54:57 -07001941 u8 is_green = 0;
1942 u8 active_tbl = 0;
1943 u8 done_search = 0;
1944 u16 high_low;
Guy Cohena5e8b502008-05-29 16:35:11 +08001945 s32 sr;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001946 u8 tid = MAX_TID_COUNT;
Wey-Yi Guy86b47662009-03-10 14:35:09 -07001947 struct iwl_tid_data *tid_data;
Zhu Yib481de92007-09-25 17:54:57 -07001948
Tomas Winklere1623442009-01-27 14:27:56 -08001949 IWL_DEBUG_RATE(priv, "rate scale calculate new rate for skb\n");
Zhu Yib481de92007-09-25 17:54:57 -07001950
Gábor Stefanik514d65c2009-04-23 19:36:08 +02001951 /* Send management frames and NO_ACK data using lowest rate. */
Tomas Winkler417f1142008-11-12 13:14:06 -08001952 /* TODO: this could probably be improved.. */
1953 if (!ieee80211_is_data(hdr->frame_control) ||
Gábor Stefanik514d65c2009-04-23 19:36:08 +02001954 info->flags & IEEE80211_TX_CTL_NO_ACK)
Zhu Yib481de92007-09-25 17:54:57 -07001955 return;
Zhu Yib481de92007-09-25 17:54:57 -07001956
Johannes Berg4b7679a2008-09-18 18:14:18 +02001957 if (!sta || !lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07001958 return;
1959
Johannes Berg4b7679a2008-09-18 18:14:18 +02001960 lq_sta->supp_rates = sta->supp_rates[lq_sta->band];
Zhu Yib481de92007-09-25 17:54:57 -07001961
Ron Rindjunskyfaa29712008-06-12 09:46:58 +08001962 tid = rs_tl_add_packet(lq_sta, hdr);
Daniel C Halperine3949d62009-09-17 10:43:50 -07001963 if ((tid != MAX_TID_COUNT) && (lq_sta->tx_agg_tid_en & (1 << tid))) {
1964 tid_data = &priv->stations[lq_sta->lq.sta_id].tid[tid];
1965 if (tid_data->agg.state == IWL_AGG_OFF)
1966 lq_sta->is_agg = 0;
1967 else
1968 lq_sta->is_agg = 1;
1969 } else
1970 lq_sta->is_agg = 0;
Ron Rindjunskyfaa29712008-06-12 09:46:58 +08001971
Ben Cahill77626352007-11-29 11:09:44 +08001972 /*
1973 * Select rate-scale / modulation-mode table to work with in
1974 * the rest of this function: "search" if searching for better
1975 * modulation mode, or "active" if doing rate scaling within a mode.
1976 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001977 if (!lq_sta->search_better_tbl)
1978 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001979 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08001980 active_tbl = 1 - lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001981
Tomas Winklerc33104f2008-01-14 17:46:21 -08001982 tbl = &(lq_sta->lq_info[active_tbl]);
Wey-Yi Guy2681b202009-05-21 13:44:22 -07001983 if (is_legacy(tbl->lq_type))
1984 lq_sta->is_green = 0;
1985 else
Daniel C Halperinb2617932009-08-13 13:30:59 -07001986 lq_sta->is_green = rs_use_green(sta, &priv->current_ht_config);
Tomas Winklerc33104f2008-01-14 17:46:21 -08001987 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07001988
Ben Cahill77626352007-11-29 11:09:44 +08001989 /* current tx rate */
Johannes Bergb7e35002008-09-11 02:22:58 +02001990 index = lq_sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07001991
Tomas Winklere1623442009-01-27 14:27:56 -08001992 IWL_DEBUG_RATE(priv, "Rate scale index %d for type %d\n", index,
Zhu Yib481de92007-09-25 17:54:57 -07001993 tbl->lq_type);
1994
Ben Cahill77626352007-11-29 11:09:44 +08001995 /* rates available for this association, and for modulation mode */
Guy Coheneecd6e52008-04-23 17:14:58 -07001996 rate_mask = rs_get_supported_rates(lq_sta, hdr, tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -07001997
Tomas Winklere1623442009-01-27 14:27:56 -08001998 IWL_DEBUG_RATE(priv, "mask 0x%04X \n", rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07001999
2000 /* mask with station rate restriction */
2001 if (is_legacy(tbl->lq_type)) {
Johannes Berg8318d782008-01-24 19:38:38 +01002002 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Ben Cahill77626352007-11-29 11:09:44 +08002003 /* supp_rates has no CCK bits in A mode */
Zhu Yib481de92007-09-25 17:54:57 -07002004 rate_scale_index_msk = (u16) (rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -08002005 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
Zhu Yib481de92007-09-25 17:54:57 -07002006 else
2007 rate_scale_index_msk = (u16) (rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -08002008 lq_sta->supp_rates);
Zhu Yib481de92007-09-25 17:54:57 -07002009
2010 } else
2011 rate_scale_index_msk = rate_mask;
2012
2013 if (!rate_scale_index_msk)
2014 rate_scale_index_msk = rate_mask;
2015
Guy Cohen07bc28e2008-04-23 17:15:03 -07002016 if (!((1 << index) & rate_scale_index_msk)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002017 IWL_ERR(priv, "Current Rate is not valid\n");
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07002018 if (lq_sta->search_better_tbl) {
2019 /* revert to active table if search table is not valid*/
2020 tbl->lq_type = LQ_NONE;
2021 lq_sta->search_better_tbl = 0;
2022 tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
2023 /* get "active" rate info */
2024 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
2025 rate = rs_update_rate_tbl(priv, lq_sta,
2026 tbl, index, is_green);
2027 }
Guy Cohen07bc28e2008-04-23 17:15:03 -07002028 return;
Zhu Yib481de92007-09-25 17:54:57 -07002029 }
2030
Ben Cahill77626352007-11-29 11:09:44 +08002031 /* Get expected throughput table and history window for current rate */
Guy Cohen07bc28e2008-04-23 17:15:03 -07002032 if (!tbl->expected_tpt) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002033 IWL_ERR(priv, "tbl->expected_tpt is NULL\n");
Guy Cohen07bc28e2008-04-23 17:15:03 -07002034 return;
2035 }
Zhu Yib481de92007-09-25 17:54:57 -07002036
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002037 /* force user max rate if set by user */
2038 if ((lq_sta->max_rate_idx != -1) &&
2039 (lq_sta->max_rate_idx < index)) {
2040 index = lq_sta->max_rate_idx;
2041 update_lq = 1;
2042 window = &(tbl->win[index]);
2043 goto lq_update;
2044 }
2045
Zhu Yib481de92007-09-25 17:54:57 -07002046 window = &(tbl->win[index]);
2047
Ben Cahill77626352007-11-29 11:09:44 +08002048 /*
2049 * If there is not enough history to calculate actual average
2050 * throughput, keep analyzing results of more tx frames, without
2051 * changing rate or mode (bypass most of the rest of this function).
2052 * Set up new rate table in uCode only if old rate is not supported
2053 * in current association (use new rate found above).
2054 */
Zhu Yib481de92007-09-25 17:54:57 -07002055 fail_count = window->counter - window->success_counter;
Guy Cohen07bc28e2008-04-23 17:15:03 -07002056 if ((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
2057 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002058 IWL_DEBUG_RATE(priv, "LQ: still below TH. succ=%d total=%d "
Zhu Yib481de92007-09-25 17:54:57 -07002059 "for index %d\n",
2060 window->success_counter, window->counter, index);
Ben Cahill77626352007-11-29 11:09:44 +08002061
2062 /* Can't calculate this yet; not enough history */
Zhu Yib481de92007-09-25 17:54:57 -07002063 window->average_tpt = IWL_INVALID_VALUE;
Ben Cahill77626352007-11-29 11:09:44 +08002064
2065 /* Should we stay with this modulation mode,
2066 * or search for a new one? */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002067 rs_stay_in_table(lq_sta);
Ben Cahill77626352007-11-29 11:09:44 +08002068
Zhu Yib481de92007-09-25 17:54:57 -07002069 goto out;
Guy Cohen3110bef2008-09-09 10:54:54 +08002070 }
Ben Cahill77626352007-11-29 11:09:44 +08002071 /* Else we have enough samples; calculate estimate of
2072 * actual average throughput */
Guy Cohen3110bef2008-09-09 10:54:54 +08002073
Daniel C Halperine3949d62009-09-17 10:43:50 -07002074 /* Sanity-check TPT calculations */
Guy Cohen3110bef2008-09-09 10:54:54 +08002075 BUG_ON(window->average_tpt != ((window->success_ratio *
2076 tbl->expected_tpt[index] + 64) / 128));
Zhu Yib481de92007-09-25 17:54:57 -07002077
Ben Cahill77626352007-11-29 11:09:44 +08002078 /* If we are searching for better modulation mode, check success. */
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002079 if (lq_sta->search_better_tbl &&
Johannes Berg3ad3b922009-08-07 15:41:48 -07002080 (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_MULTI)) {
Ben Cahill77626352007-11-29 11:09:44 +08002081 /* If good success, continue using the "search" mode;
2082 * no need to send new link quality command, since we're
2083 * continuing to use the setup that we've been trying. */
Guy Cohen07bc28e2008-04-23 17:15:03 -07002084 if (window->average_tpt > lq_sta->last_tpt) {
2085
Tomas Winklere1623442009-01-27 14:27:56 -08002086 IWL_DEBUG_RATE(priv, "LQ: SWITCHING TO NEW TABLE "
Guy Cohen07bc28e2008-04-23 17:15:03 -07002087 "suc=%d cur-tpt=%d old-tpt=%d\n",
2088 window->success_ratio,
2089 window->average_tpt,
2090 lq_sta->last_tpt);
2091
2092 if (!is_legacy(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08002093 lq_sta->enable_counter = 1;
Guy Cohen07bc28e2008-04-23 17:15:03 -07002094
Ben Cahill77626352007-11-29 11:09:44 +08002095 /* Swap tables; "search" becomes "active" */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002096 lq_sta->active_tbl = active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002097 current_tpt = window->average_tpt;
Ben Cahill77626352007-11-29 11:09:44 +08002098
2099 /* Else poor success; go back to mode in "active" table */
Zhu Yib481de92007-09-25 17:54:57 -07002100 } else {
Guy Cohen07bc28e2008-04-23 17:15:03 -07002101
Tomas Winklere1623442009-01-27 14:27:56 -08002102 IWL_DEBUG_RATE(priv, "LQ: GOING BACK TO THE OLD TABLE "
Guy Cohen07bc28e2008-04-23 17:15:03 -07002103 "suc=%d cur-tpt=%d old-tpt=%d\n",
2104 window->success_ratio,
2105 window->average_tpt,
2106 lq_sta->last_tpt);
2107
Ben Cahill77626352007-11-29 11:09:44 +08002108 /* Nullify "search" table */
Zhu Yib481de92007-09-25 17:54:57 -07002109 tbl->lq_type = LQ_NONE;
Ben Cahill77626352007-11-29 11:09:44 +08002110
2111 /* Revert to "active" table */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002112 active_tbl = lq_sta->active_tbl;
2113 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07002114
Ben Cahill77626352007-11-29 11:09:44 +08002115 /* Revert to "active" rate and throughput info */
Tomas Winklere7d326a2008-06-12 09:47:11 +08002116 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
Tomas Winklerc33104f2008-01-14 17:46:21 -08002117 current_tpt = lq_sta->last_tpt;
Ben Cahill77626352007-11-29 11:09:44 +08002118
2119 /* Need to set up a new rate table in uCode */
2120 update_lq = 1;
Zhu Yib481de92007-09-25 17:54:57 -07002121 }
Ben Cahill77626352007-11-29 11:09:44 +08002122
2123 /* Either way, we've made a decision; modulation mode
2124 * search is done, allow rate adjustment next time. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002125 lq_sta->search_better_tbl = 0;
Ben Cahill77626352007-11-29 11:09:44 +08002126 done_search = 1; /* Don't switch modes below! */
Zhu Yib481de92007-09-25 17:54:57 -07002127 goto lq_update;
2128 }
2129
Ben Cahill77626352007-11-29 11:09:44 +08002130 /* (Else) not in search of better modulation mode, try for better
2131 * starting rate, while staying in this mode. */
Ester Kummerbf403db2008-05-05 10:22:40 +08002132 high_low = rs_get_adjacent_rate(priv, index, rate_scale_index_msk,
Zhu Yib481de92007-09-25 17:54:57 -07002133 tbl->lq_type);
2134 low = high_low & 0xff;
2135 high = (high_low >> 8) & 0xff;
2136
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002137 /* If user set max rate, dont allow higher than user constrain */
2138 if ((lq_sta->max_rate_idx != -1) &&
2139 (lq_sta->max_rate_idx < high))
2140 high = IWL_RATE_INVALID;
2141
Guy Cohena5e8b502008-05-29 16:35:11 +08002142 sr = window->success_ratio;
2143
Ben Cahill77626352007-11-29 11:09:44 +08002144 /* Collect measured throughputs for current and adjacent rates */
Zhu Yib481de92007-09-25 17:54:57 -07002145 current_tpt = window->average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07002146 if (low != IWL_RATE_INVALID)
2147 low_tpt = tbl->win[low].average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07002148 if (high != IWL_RATE_INVALID)
2149 high_tpt = tbl->win[high].average_tpt;
2150
Guy Cohena5e8b502008-05-29 16:35:11 +08002151 scale_action = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002152
Ben Cahill77626352007-11-29 11:09:44 +08002153 /* Too many failures, decrease rate */
Guy Cohena5e8b502008-05-29 16:35:11 +08002154 if ((sr <= IWL_RATE_DECREASE_TH) || (current_tpt == 0)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002155 IWL_DEBUG_RATE(priv, "decrease rate because of low success_ratio\n");
Zhu Yib481de92007-09-25 17:54:57 -07002156 scale_action = -1;
Ben Cahill77626352007-11-29 11:09:44 +08002157
2158 /* No throughput measured yet for adjacent rates; try increase. */
Zhu Yib481de92007-09-25 17:54:57 -07002159 } else if ((low_tpt == IWL_INVALID_VALUE) &&
Guy Cohena5e8b502008-05-29 16:35:11 +08002160 (high_tpt == IWL_INVALID_VALUE)) {
2161
2162 if (high != IWL_RATE_INVALID && sr >= IWL_RATE_INCREASE_TH)
2163 scale_action = 1;
2164 else if (low != IWL_RATE_INVALID)
Wey-Yi Guy8fe72312009-03-10 14:35:10 -07002165 scale_action = 0;
Guy Cohena5e8b502008-05-29 16:35:11 +08002166 }
Ben Cahill77626352007-11-29 11:09:44 +08002167
2168 /* Both adjacent throughputs are measured, but neither one has better
2169 * throughput; we're using the best rate, don't change it! */
Zhu Yib481de92007-09-25 17:54:57 -07002170 else if ((low_tpt != IWL_INVALID_VALUE) &&
2171 (high_tpt != IWL_INVALID_VALUE) &&
2172 (low_tpt < current_tpt) &&
2173 (high_tpt < current_tpt))
2174 scale_action = 0;
Ben Cahill77626352007-11-29 11:09:44 +08002175
2176 /* At least one adjacent rate's throughput is measured,
2177 * and may have better performance. */
Zhu Yib481de92007-09-25 17:54:57 -07002178 else {
Ben Cahill77626352007-11-29 11:09:44 +08002179 /* Higher adjacent rate's throughput is measured */
Zhu Yib481de92007-09-25 17:54:57 -07002180 if (high_tpt != IWL_INVALID_VALUE) {
Ben Cahill77626352007-11-29 11:09:44 +08002181 /* Higher rate has better throughput */
Guy Cohena5e8b502008-05-29 16:35:11 +08002182 if (high_tpt > current_tpt &&
2183 sr >= IWL_RATE_INCREASE_TH) {
Zhu Yib481de92007-09-25 17:54:57 -07002184 scale_action = 1;
Guy Cohena5e8b502008-05-29 16:35:11 +08002185 } else {
Wey-Yi Guy8fe72312009-03-10 14:35:10 -07002186 scale_action = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002187 }
Ben Cahill77626352007-11-29 11:09:44 +08002188
2189 /* Lower adjacent rate's throughput is measured */
Zhu Yib481de92007-09-25 17:54:57 -07002190 } else if (low_tpt != IWL_INVALID_VALUE) {
Ben Cahill77626352007-11-29 11:09:44 +08002191 /* Lower rate has better throughput */
Zhu Yib481de92007-09-25 17:54:57 -07002192 if (low_tpt > current_tpt) {
Tomas Winklere1623442009-01-27 14:27:56 -08002193 IWL_DEBUG_RATE(priv,
2194 "decrease rate because of low tpt\n");
Zhu Yib481de92007-09-25 17:54:57 -07002195 scale_action = -1;
Guy Cohena5e8b502008-05-29 16:35:11 +08002196 } else if (sr >= IWL_RATE_INCREASE_TH) {
Zhu Yib481de92007-09-25 17:54:57 -07002197 scale_action = 1;
Guy Cohena5e8b502008-05-29 16:35:11 +08002198 }
Zhu Yib481de92007-09-25 17:54:57 -07002199 }
2200 }
2201
Ben Cahill77626352007-11-29 11:09:44 +08002202 /* Sanity check; asked for decrease, but success rate or throughput
2203 * has been good at old rate. Don't change it. */
Guy Cohena5e8b502008-05-29 16:35:11 +08002204 if ((scale_action == -1) && (low != IWL_RATE_INVALID) &&
2205 ((sr > IWL_RATE_HIGH_TH) ||
Zhu Yib481de92007-09-25 17:54:57 -07002206 (current_tpt > (100 * tbl->expected_tpt[low]))))
Zhu Yib481de92007-09-25 17:54:57 -07002207 scale_action = 0;
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002208 if (!iwl_ht_enabled(priv) && !is_legacy(tbl->lq_type))
2209 scale_action = -1;
Johannes Berg3ad3b922009-08-07 15:41:48 -07002210 if (iwl_tx_ant_restriction(priv) != IWL_ANT_OK_MULTI &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002211 (is_mimo2(tbl->lq_type) || is_mimo3(tbl->lq_type)))
2212 scale_action = -1;
Zhu Yib481de92007-09-25 17:54:57 -07002213 switch (scale_action) {
2214 case -1:
Ben Cahill77626352007-11-29 11:09:44 +08002215 /* Decrease starting rate, update uCode's rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002216 if (low != IWL_RATE_INVALID) {
2217 update_lq = 1;
2218 index = low;
2219 }
Mohamed Abbas447fee72009-04-20 14:37:02 -07002220
Zhu Yib481de92007-09-25 17:54:57 -07002221 break;
2222 case 1:
Ben Cahill77626352007-11-29 11:09:44 +08002223 /* Increase starting rate, update uCode's rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002224 if (high != IWL_RATE_INVALID) {
2225 update_lq = 1;
2226 index = high;
2227 }
2228
2229 break;
2230 case 0:
Ben Cahill77626352007-11-29 11:09:44 +08002231 /* No change */
Zhu Yib481de92007-09-25 17:54:57 -07002232 default:
2233 break;
2234 }
2235
Tomas Winklere1623442009-01-27 14:27:56 -08002236 IWL_DEBUG_RATE(priv, "choose rate scale index %d action %d low %d "
Zhu Yib481de92007-09-25 17:54:57 -07002237 "high %d type %d\n",
2238 index, scale_action, low, high, tbl->lq_type);
2239
Guy Cohena5e8b502008-05-29 16:35:11 +08002240lq_update:
Ben Cahill77626352007-11-29 11:09:44 +08002241 /* Replace uCode's rate table for the destination station. */
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07002242 if (update_lq)
2243 rate = rs_update_rate_tbl(priv, lq_sta,
2244 tbl, index, is_green);
Ben Cahill77626352007-11-29 11:09:44 +08002245
Johannes Berg3ad3b922009-08-07 15:41:48 -07002246 if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_MULTI) {
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002247 /* Should we stay with this modulation mode,
2248 * or search for a new one? */
2249 rs_stay_in_table(lq_sta);
2250 }
Ben Cahill77626352007-11-29 11:09:44 +08002251 /*
2252 * Search for new modulation mode if we're:
2253 * 1) Not changing rates right now
2254 * 2) Not just finishing up a search
2255 * 3) Allowing a new search
2256 */
Guy Cohen47cfd462008-05-27 11:29:34 +08002257 if (!update_lq && !done_search && !lq_sta->stay_in_tbl && window->counter) {
Ben Cahill77626352007-11-29 11:09:44 +08002258 /* Save current throughput to compare with "search" throughput*/
Tomas Winklerc33104f2008-01-14 17:46:21 -08002259 lq_sta->last_tpt = current_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07002260
Ben Cahill77626352007-11-29 11:09:44 +08002261 /* Select a new "search" modulation mode to try.
2262 * If one is found, set up the new "search" table. */
Zhu Yib481de92007-09-25 17:54:57 -07002263 if (is_legacy(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08002264 rs_move_legacy_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07002265 else if (is_siso(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08002266 rs_move_siso_to_other(priv, lq_sta, conf, sta, index);
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07002267 else if (is_mimo2(tbl->lq_type))
2268 rs_move_mimo2_to_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07002269 else
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07002270 rs_move_mimo3_to_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07002271
Ben Cahill77626352007-11-29 11:09:44 +08002272 /* If new "search" mode was selected, set up in uCode table */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002273 if (lq_sta->search_better_tbl) {
Ben Cahill77626352007-11-29 11:09:44 +08002274 /* Access the "search" table, clear its history. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002275 tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Zhu Yib481de92007-09-25 17:54:57 -07002276 for (i = 0; i < IWL_RATE_COUNT; i++)
2277 rs_rate_scale_clear_window(&(tbl->win[i]));
2278
Ben Cahill77626352007-11-29 11:09:44 +08002279 /* Use new "search" start rate */
Tomas Winklere7d326a2008-06-12 09:47:11 +08002280 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002281
Tomas Winklere1623442009-01-27 14:27:56 -08002282 IWL_DEBUG_RATE(priv, "Switch current mcs: %X index: %d\n",
Guy Cohen39e88502008-04-23 17:14:57 -07002283 tbl->current_rate, index);
Guy Cohen07bc28e2008-04-23 17:15:03 -07002284 rs_fill_link_cmd(priv, lq_sta, tbl->current_rate);
Tomas Winkler66c73db2008-04-15 16:01:40 -07002285 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
Mohamed Abbas447fee72009-04-20 14:37:02 -07002286 } else
2287 done_search = 1;
2288 }
Zhu Yib481de92007-09-25 17:54:57 -07002289
Mohamed Abbas447fee72009-04-20 14:37:02 -07002290 if (done_search && !lq_sta->stay_in_tbl) {
Ben Cahill77626352007-11-29 11:09:44 +08002291 /* If the "active" (non-search) mode was legacy,
2292 * and we've tried switching antennas,
2293 * but we haven't been able to try HT modes (not available),
2294 * stay with best antenna legacy modulation for a while
2295 * before next round of mode comparisons. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002296 tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
Luis R. Rodriguezde27e642008-12-23 15:58:44 -08002297 if (is_legacy(tbl1->lq_type) && !conf_is_ht(conf) &&
Mohamed Abbasd6e93392009-05-08 13:44:39 -07002298 lq_sta->action_counter > tbl1->max_search) {
Tomas Winklere1623442009-01-27 14:27:56 -08002299 IWL_DEBUG_RATE(priv, "LQ: STAY in legacy table\n");
Ester Kummerbf403db2008-05-05 10:22:40 +08002300 rs_set_stay_in_table(priv, 1, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002301 }
2302
Ben Cahill77626352007-11-29 11:09:44 +08002303 /* If we're in an HT mode, and all 3 mode switch actions
2304 * have been tried and compared, stay in this best modulation
2305 * mode for a while before next round of mode comparisons. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002306 if (lq_sta->enable_counter &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002307 (lq_sta->action_counter >= tbl1->max_search) &&
2308 iwl_ht_enabled(priv)) {
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002309 if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
2310 (lq_sta->tx_agg_tid_en & (1 << tid)) &&
2311 (tid != MAX_TID_COUNT)) {
Wey-Yi Guy86b47662009-03-10 14:35:09 -07002312 tid_data =
2313 &priv->stations[lq_sta->lq.sta_id].tid[tid];
2314 if (tid_data->agg.state == IWL_AGG_OFF) {
2315 IWL_DEBUG_RATE(priv,
2316 "try to aggregate tid %d\n",
2317 tid);
2318 rs_tl_turn_on_agg(priv, tid,
2319 lq_sta, sta);
2320 }
Zhu Yib481de92007-09-25 17:54:57 -07002321 }
Ester Kummerbf403db2008-05-05 10:22:40 +08002322 rs_set_stay_in_table(priv, 0, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002323 }
Zhu Yib481de92007-09-25 17:54:57 -07002324 }
2325
2326out:
Tomas Winkler978785a2008-12-19 10:37:31 +08002327 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
Zhu Yib481de92007-09-25 17:54:57 -07002328 i = index;
Johannes Bergb7e35002008-09-11 02:22:58 +02002329 lq_sta->last_txrate_idx = i;
Zhu Yib481de92007-09-25 17:54:57 -07002330
Zhu Yib481de92007-09-25 17:54:57 -07002331 return;
2332}
2333
2334
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002335static void rs_initialize_lq(struct iwl_priv *priv,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002336 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02002337 struct ieee80211_sta *sta,
2338 struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002339{
Tomas Winklere227cea2008-07-18 13:53:05 +08002340 struct iwl_scale_tbl_info *tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002341 int rate_idx;
Guy Cohenaade00c2008-04-23 17:14:59 -07002342 int i;
Guy Cohen39e88502008-04-23 17:14:57 -07002343 u32 rate;
Daniel C Halperinb2617932009-08-13 13:30:59 -07002344 u8 use_green = rs_use_green(sta, &priv->current_ht_config);
Guy Cohenaade00c2008-04-23 17:14:59 -07002345 u8 active_tbl = 0;
2346 u8 valid_tx_ant;
Zhu Yib481de92007-09-25 17:54:57 -07002347
Johannes Berg4b7679a2008-09-18 18:14:18 +02002348 if (!sta || !lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002349 goto out;
2350
Johannes Bergb7e35002008-09-11 02:22:58 +02002351 i = lq_sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002352
Tomas Winklerc33104f2008-01-14 17:46:21 -08002353 if ((lq_sta->lq.sta_id == 0xff) &&
Johannes Berg05c914f2008-09-11 00:01:58 +02002354 (priv->iw_mode == NL80211_IFTYPE_ADHOC))
Zhu Yib481de92007-09-25 17:54:57 -07002355 goto out;
2356
Guy Cohenaade00c2008-04-23 17:14:59 -07002357 valid_tx_ant = priv->hw_params.valid_tx_ant;
2358
Tomas Winklerc33104f2008-01-14 17:46:21 -08002359 if (!lq_sta->search_better_tbl)
2360 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002361 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002362 active_tbl = 1 - lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002363
Tomas Winklerc33104f2008-01-14 17:46:21 -08002364 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07002365
2366 if ((i < 0) || (i >= IWL_RATE_COUNT))
2367 i = 0;
2368
Tomas Winkler1826dcc2008-05-15 13:54:02 +08002369 rate = iwl_rates[i].plcp;
Winkler, Tomaseb48dca2008-10-29 14:05:48 -07002370 tbl->ant_type = first_antenna(valid_tx_ant);
2371 rate |= tbl->ant_type << RATE_MCS_ANT_POS;
Zhu Yib481de92007-09-25 17:54:57 -07002372
2373 if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
Guy Cohen39e88502008-04-23 17:14:57 -07002374 rate |= RATE_MCS_CCK_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002375
Guy Cohen39e88502008-04-23 17:14:57 -07002376 rs_get_tbl_info_from_mcs(rate, priv->band, tbl, &rate_idx);
Guy Cohenaade00c2008-04-23 17:14:59 -07002377 if (!rs_is_valid_ant(valid_tx_ant, tbl->ant_type))
2378 rs_toggle_antenna(valid_tx_ant, &rate, tbl);
Zhu Yib481de92007-09-25 17:54:57 -07002379
Tomas Winkler978785a2008-12-19 10:37:31 +08002380 rate = rate_n_flags_from_tbl(priv, tbl, rate_idx, use_green);
Guy Cohen39e88502008-04-23 17:14:57 -07002381 tbl->current_rate = rate;
Guy Coheneecd6e52008-04-23 17:14:58 -07002382 rs_set_expected_tpt_table(lq_sta, tbl);
Guy Cohen07bc28e2008-04-23 17:15:03 -07002383 rs_fill_link_cmd(NULL, lq_sta, rate);
Tomas Winkler66c73db2008-04-15 16:01:40 -07002384 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07002385 out:
2386 return;
2387}
2388
Johannes Berge6a98542008-10-21 12:40:02 +02002389static void rs_get_rate(void *priv_r, struct ieee80211_sta *sta, void *priv_sta,
2390 struct ieee80211_tx_rate_control *txrc)
Zhu Yib481de92007-09-25 17:54:57 -07002391{
2392
Johannes Berge6a98542008-10-21 12:40:02 +02002393 struct sk_buff *skb = txrc->skb;
2394 struct ieee80211_supported_band *sband = txrc->sband;
Johannes Berg4b7679a2008-09-18 18:14:18 +02002395 struct iwl_priv *priv = (struct iwl_priv *)priv_r;
2396 struct ieee80211_conf *conf = &priv->hw->conf;
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002397 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Zhu Yib481de92007-09-25 17:54:57 -07002398 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berge6a98542008-10-21 12:40:02 +02002399 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Tomas Winkler417f1142008-11-12 13:14:06 -08002400 struct iwl_lq_sta *lq_sta = priv_sta;
2401 int rate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002402
Tomas Winklere1623442009-01-27 14:27:56 -08002403 IWL_DEBUG_RATE_LIMIT(priv, "rate scale calculate new rate for skb\n");
Zhu Yib481de92007-09-25 17:54:57 -07002404
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002405 /* Get max rate if user set max rate */
2406 if (lq_sta) {
2407 lq_sta->max_rate_idx = txrc->max_rate_idx;
2408 if ((sband->band == IEEE80211_BAND_5GHZ) &&
2409 (lq_sta->max_rate_idx != -1))
2410 lq_sta->max_rate_idx += IWL_FIRST_OFDM_RATE;
2411 if ((lq_sta->max_rate_idx < 0) ||
2412 (lq_sta->max_rate_idx >= IWL_RATE_COUNT))
2413 lq_sta->max_rate_idx = -1;
2414 }
2415
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002416 /* Send management frames and NO_ACK data using lowest rate. */
Luis R. Rodriguez4c6d4f52009-07-16 10:05:41 -07002417 if (rate_control_send_low(sta, priv_sta, txrc))
Johannes Berg4b7679a2008-09-18 18:14:18 +02002418 return;
Zhu Yib481de92007-09-25 17:54:57 -07002419
Tomas Winkler417f1142008-11-12 13:14:06 -08002420 rate_idx = lq_sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002421
Johannes Berg05c914f2008-09-11 00:01:58 +02002422 if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) &&
Tomas Winklerc33104f2008-01-14 17:46:21 -08002423 !lq_sta->ibss_sta_added) {
Tomas Winklerc587de02009-06-03 11:44:07 -07002424 u8 sta_id = iwl_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002425
2426 if (sta_id == IWL_INVALID_STATION) {
Tomas Winklere1623442009-01-27 14:27:56 -08002427 IWL_DEBUG_RATE(priv, "LQ: ADD station %pM\n",
Johannes Berge1749612008-10-27 15:59:26 -07002428 hdr->addr1);
Tomas Winklerc587de02009-06-03 11:44:07 -07002429 sta_id = iwl_add_station(priv, hdr->addr1,
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002430 false, CMD_ASYNC, ht_cap);
Zhu Yib481de92007-09-25 17:54:57 -07002431 }
2432 if ((sta_id != IWL_INVALID_STATION)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002433 lq_sta->lq.sta_id = sta_id;
2434 lq_sta->lq.rs_table[0].rate_n_flags = 0;
2435 lq_sta->ibss_sta_added = 1;
Johannes Berg4b7679a2008-09-18 18:14:18 +02002436 rs_initialize_lq(priv, conf, sta, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002437 }
Zhu Yib481de92007-09-25 17:54:57 -07002438 }
2439
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002440 if (lq_sta->last_rate_n_flags & RATE_MCS_HT_MSK) {
Tomas Winkler417f1142008-11-12 13:14:06 -08002441 rate_idx -= IWL_FIRST_OFDM_RATE;
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002442 /* 6M and 9M shared same MCS index */
2443 rate_idx = (rate_idx > 0) ? (rate_idx - 1) : 0;
2444 if (rs_extract_rate(lq_sta->last_rate_n_flags) >=
2445 IWL_RATE_MIMO3_6M_PLCP)
2446 rate_idx = rate_idx + (2 * MCS_INDEX_PER_STREAM);
2447 else if (rs_extract_rate(lq_sta->last_rate_n_flags) >=
2448 IWL_RATE_MIMO2_6M_PLCP)
2449 rate_idx = rate_idx + MCS_INDEX_PER_STREAM;
2450 info->control.rates[0].flags = IEEE80211_TX_RC_MCS;
2451 if (lq_sta->last_rate_n_flags & RATE_MCS_SGI_MSK)
2452 info->control.rates[0].flags |= IEEE80211_TX_RC_SHORT_GI;
2453 if (lq_sta->last_rate_n_flags & RATE_MCS_DUP_MSK)
2454 info->control.rates[0].flags |= IEEE80211_TX_RC_DUP_DATA;
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07002455 if (lq_sta->last_rate_n_flags & RATE_MCS_HT40_MSK)
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002456 info->control.rates[0].flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
2457 if (lq_sta->last_rate_n_flags & RATE_MCS_GF_MSK)
2458 info->control.rates[0].flags |= IEEE80211_TX_RC_GREEN_FIELD;
2459 } else {
Daniel C Halperin50273092009-08-28 09:44:45 -07002460 /* Check for invalid rates */
2461 if ((rate_idx < 0) || (rate_idx >= IWL_RATE_COUNT_LEGACY) ||
2462 ((sband->band == IEEE80211_BAND_5GHZ) &&
2463 (rate_idx < IWL_FIRST_OFDM_RATE)))
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002464 rate_idx = rate_lowest_index(sband, sta);
Daniel C Halperin50273092009-08-28 09:44:45 -07002465 /* On valid 5 GHz rate, adjust index */
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002466 else if (sband->band == IEEE80211_BAND_5GHZ)
2467 rate_idx -= IWL_FIRST_OFDM_RATE;
Daniel C Halperin7ebaeff2009-08-21 13:34:20 -07002468 info->control.rates[0].flags = 0;
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002469 }
Tomas Winkler417f1142008-11-12 13:14:06 -08002470 info->control.rates[0].idx = rate_idx;
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002471
Zhu Yib481de92007-09-25 17:54:57 -07002472}
2473
Johannes Berg4b7679a2008-09-18 18:14:18 +02002474static void *rs_alloc_sta(void *priv_rate, struct ieee80211_sta *sta,
2475 gfp_t gfp)
Zhu Yib481de92007-09-25 17:54:57 -07002476{
Tomas Winklere227cea2008-07-18 13:53:05 +08002477 struct iwl_lq_sta *lq_sta;
Reinette Chatre8d9698b2009-10-16 14:25:55 -07002478 struct iwl_station_priv *sta_priv = (struct iwl_station_priv *) sta->drv_priv;
Ester Kummerbf403db2008-05-05 10:22:40 +08002479 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07002480
Ester Kummerbf403db2008-05-05 10:22:40 +08002481 priv = (struct iwl_priv *)priv_rate;
Tomas Winklere1623442009-01-27 14:27:56 -08002482 IWL_DEBUG_RATE(priv, "create station rate scale window\n");
Zhu Yib481de92007-09-25 17:54:57 -07002483
Reinette Chatre8d9698b2009-10-16 14:25:55 -07002484 lq_sta = &sta_priv->lq_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002485
Tomas Winklerc33104f2008-01-14 17:46:21 -08002486 return lq_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002487}
2488
Johannes Berg4b7679a2008-09-18 18:14:18 +02002489static void rs_rate_init(void *priv_r, struct ieee80211_supported_band *sband,
2490 struct ieee80211_sta *sta, void *priv_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002491{
2492 int i, j;
Johannes Berg4b7679a2008-09-18 18:14:18 +02002493 struct iwl_priv *priv = (struct iwl_priv *)priv_r;
2494 struct ieee80211_conf *conf = &priv->hw->conf;
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002495 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Tomas Winklere227cea2008-07-18 13:53:05 +08002496 struct iwl_lq_sta *lq_sta = priv_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002497
Reinette Chatre5ad13f82009-10-30 14:36:14 -07002498 lq_sta->lq.sta_id = 0xff;
2499
2500 for (j = 0; j < LQ_SIZE; j++)
2501 for (i = 0; i < IWL_RATE_COUNT; i++)
2502 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
2503
Tomas Winklerc33104f2008-01-14 17:46:21 -08002504 lq_sta->flush_timer = 0;
Johannes Berg4b7679a2008-09-18 18:14:18 +02002505 lq_sta->supp_rates = sta->supp_rates[sband->band];
Zhu Yib481de92007-09-25 17:54:57 -07002506 for (j = 0; j < LQ_SIZE; j++)
2507 for (i = 0; i < IWL_RATE_COUNT; i++)
Tomas Winkler3ac7f142008-07-21 02:40:14 +03002508 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
Zhu Yib481de92007-09-25 17:54:57 -07002509
Tomas Winklere1623442009-01-27 14:27:56 -08002510 IWL_DEBUG_RATE(priv, "LQ: *** rate scale station global init ***\n");
Zhu Yib481de92007-09-25 17:54:57 -07002511 /* TODO: what is a good starting rate for STA? About middle? Maybe not
2512 * the lowest or the highest rate.. Could consider using RSSI from
2513 * previous packets? Need to have IEEE 802.1X auth succeed immediately
2514 * after assoc.. */
2515
Tomas Winklerc33104f2008-01-14 17:46:21 -08002516 lq_sta->ibss_sta_added = 0;
Johannes Berg05c914f2008-09-11 00:01:58 +02002517 if (priv->iw_mode == NL80211_IFTYPE_AP) {
Tomas Winklerc587de02009-06-03 11:44:07 -07002518 u8 sta_id = iwl_find_station(priv,
Abhijeet Kolekare11bc022009-04-08 11:26:41 -07002519 sta->addr);
Joe Perches0795af52007-10-03 17:59:30 -07002520
Zhu Yib481de92007-09-25 17:54:57 -07002521 /* for IBSS the call are from tasklet */
Tomas Winklere1623442009-01-27 14:27:56 -08002522 IWL_DEBUG_RATE(priv, "LQ: ADD station %pM\n", sta->addr);
Zhu Yib481de92007-09-25 17:54:57 -07002523
2524 if (sta_id == IWL_INVALID_STATION) {
Tomas Winklere1623442009-01-27 14:27:56 -08002525 IWL_DEBUG_RATE(priv, "LQ: ADD station %pM\n", sta->addr);
Tomas Winklerc587de02009-06-03 11:44:07 -07002526 sta_id = iwl_add_station(priv, sta->addr, false,
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002527 CMD_ASYNC, ht_cap);
Zhu Yib481de92007-09-25 17:54:57 -07002528 }
2529 if ((sta_id != IWL_INVALID_STATION)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002530 lq_sta->lq.sta_id = sta_id;
2531 lq_sta->lq.rs_table[0].rate_n_flags = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002532 }
2533 /* FIXME: this is w/a remove it later */
2534 priv->assoc_station_added = 1;
2535 }
2536
Tomas Winklerc33104f2008-01-14 17:46:21 -08002537 lq_sta->is_dup = 0;
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002538 lq_sta->max_rate_idx = -1;
Abbas, Mohamed7d049e52009-01-20 21:33:53 -08002539 lq_sta->missed_rate_counter = IWL_MISSED_RATE_MAX;
Daniel C Halperinb2617932009-08-13 13:30:59 -07002540 lq_sta->is_green = rs_use_green(sta, &priv->current_ht_config);
Guy Cohen39e88502008-04-23 17:14:57 -07002541 lq_sta->active_legacy_rate = priv->active_rate & ~(0x1000);
Tomas Winklerc33104f2008-01-14 17:46:21 -08002542 lq_sta->active_rate_basic = priv->active_rate_basic;
Johannes Berg8318d782008-01-24 19:38:38 +01002543 lq_sta->band = priv->band;
Ben Cahill77626352007-11-29 11:09:44 +08002544 /*
2545 * active_siso_rate mask includes 9 MBits (bit 5), and CCK (bits 0-3),
2546 * supp_rates[] does not; shift to convert format, force 9 MBits off.
2547 */
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002548 lq_sta->active_siso_rate = ht_cap->mcs.rx_mask[0] << 1;
2549 lq_sta->active_siso_rate |= ht_cap->mcs.rx_mask[0] & 0x1;
Guy Cohenfde0db32008-04-21 15:42:01 -07002550 lq_sta->active_siso_rate &= ~((u16)0x2);
2551 lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -07002552
Ben Cahill77626352007-11-29 11:09:44 +08002553 /* Same here */
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002554 lq_sta->active_mimo2_rate = ht_cap->mcs.rx_mask[1] << 1;
2555 lq_sta->active_mimo2_rate |= ht_cap->mcs.rx_mask[1] & 0x1;
Guy Cohenfde0db32008-04-21 15:42:01 -07002556 lq_sta->active_mimo2_rate &= ~((u16)0x2);
2557 lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE;
2558
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002559 lq_sta->active_mimo3_rate = ht_cap->mcs.rx_mask[2] << 1;
2560 lq_sta->active_mimo3_rate |= ht_cap->mcs.rx_mask[2] & 0x1;
Guy Cohenfde0db32008-04-21 15:42:01 -07002561 lq_sta->active_mimo3_rate &= ~((u16)0x2);
2562 lq_sta->active_mimo3_rate <<= IWL_FIRST_OFDM_RATE;
2563
Tomas Winklere1623442009-01-27 14:27:56 -08002564 IWL_DEBUG_RATE(priv, "SISO-RATE=%X MIMO2-RATE=%X MIMO3-RATE=%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002565 lq_sta->active_siso_rate,
Guy Cohenfde0db32008-04-21 15:42:01 -07002566 lq_sta->active_mimo2_rate,
2567 lq_sta->active_mimo3_rate);
2568
Tomas Winklera96a27f2008-10-23 23:48:56 -07002569 /* These values will be overridden later */
Guy Cohen07bc28e2008-04-23 17:15:03 -07002570 lq_sta->lq.general_params.single_stream_ant_msk = ANT_A;
2571 lq_sta->lq.general_params.dual_stream_ant_msk = ANT_AB;
2572
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002573 /* as default allow aggregation for all tids */
2574 lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002575 lq_sta->drv = priv;
Zhu Yib481de92007-09-25 17:54:57 -07002576
Daniel C Halperin50273092009-08-28 09:44:45 -07002577 /* Set last_txrate_idx to lowest rate */
2578 lq_sta->last_txrate_idx = rate_lowest_index(sband, sta);
2579 if (sband->band == IEEE80211_BAND_5GHZ)
2580 lq_sta->last_txrate_idx += IWL_FIRST_OFDM_RATE;
Daniel C Halperine3949d62009-09-17 10:43:50 -07002581 lq_sta->is_agg = 0;
Mohamed Abbasd5e49032008-12-12 08:22:15 -08002582
Johannes Berg4b7679a2008-09-18 18:14:18 +02002583 rs_initialize_lq(priv, conf, sta, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002584}
2585
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002586static void rs_fill_link_cmd(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08002587 struct iwl_lq_sta *lq_sta, u32 new_rate)
Zhu Yib481de92007-09-25 17:54:57 -07002588{
Tomas Winklere227cea2008-07-18 13:53:05 +08002589 struct iwl_scale_tbl_info tbl_type;
Zhu Yib481de92007-09-25 17:54:57 -07002590 int index = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002591 int rate_idx;
Zhu Yi1b696de2007-09-27 11:27:41 +08002592 int repeat_rate = 0;
Guy Cohenaade00c2008-04-23 17:14:59 -07002593 u8 ant_toggle_cnt = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002594 u8 use_ht_possible = 1;
Guy Cohenaade00c2008-04-23 17:14:59 -07002595 u8 valid_tx_ant = 0;
Guy Cohen07bc28e2008-04-23 17:15:03 -07002596 struct iwl_link_quality_cmd *lq_cmd = &lq_sta->lq;
Zhu Yib481de92007-09-25 17:54:57 -07002597
Ben Cahill77626352007-11-29 11:09:44 +08002598 /* Override starting rate (index 0) if needed for debug purposes */
Guy Cohen39e88502008-04-23 17:14:57 -07002599 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Zhu Yi98d7e092007-09-27 11:27:42 +08002600
Guy Cohen39e88502008-04-23 17:14:57 -07002601 /* Interpret new_rate (rate_n_flags) */
Guy Cohenaade00c2008-04-23 17:14:59 -07002602 memset(&tbl_type, 0, sizeof(tbl_type));
Guy Cohen39e88502008-04-23 17:14:57 -07002603 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band,
Zhu Yib481de92007-09-25 17:54:57 -07002604 &tbl_type, &rate_idx);
2605
Ben Cahill77626352007-11-29 11:09:44 +08002606 /* How many times should we repeat the initial rate? */
Zhu Yib481de92007-09-25 17:54:57 -07002607 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07002608 ant_toggle_cnt = 1;
Zhu Yi1b696de2007-09-27 11:27:41 +08002609 repeat_rate = IWL_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002610 } else {
Zhu Yi1b696de2007-09-27 11:27:41 +08002611 repeat_rate = IWL_HT_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002612 }
Zhu Yib481de92007-09-25 17:54:57 -07002613
2614 lq_cmd->general_params.mimo_delimiter =
2615 is_mimo(tbl_type.lq_type) ? 1 : 0;
Ben Cahill77626352007-11-29 11:09:44 +08002616
2617 /* Fill 1st table entry (index 0) */
Guy Cohen39e88502008-04-23 17:14:57 -07002618 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002619
Guy Cohen07bc28e2008-04-23 17:15:03 -07002620 if (num_of_ant(tbl_type.ant_type) == 1) {
2621 lq_cmd->general_params.single_stream_ant_msk =
2622 tbl_type.ant_type;
2623 } else if (num_of_ant(tbl_type.ant_type) == 2) {
2624 lq_cmd->general_params.dual_stream_ant_msk =
2625 tbl_type.ant_type;
2626 } /* otherwise we don't modify the existing value */
Zhu Yib481de92007-09-25 17:54:57 -07002627
2628 index++;
Zhu Yi1b696de2007-09-27 11:27:41 +08002629 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002630
Guy Cohenaade00c2008-04-23 17:14:59 -07002631 if (priv)
2632 valid_tx_ant = priv->hw_params.valid_tx_ant;
2633
Ben Cahill77626352007-11-29 11:09:44 +08002634 /* Fill rest of rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002635 while (index < LINK_QUAL_MAX_RETRY_NUM) {
Ben Cahill77626352007-11-29 11:09:44 +08002636 /* Repeat initial/next rate.
2637 * For legacy IWL_NUMBER_TRY == 1, this loop will not execute.
2638 * For HT IWL_HT_NUMBER_TRY == 3, this executes twice. */
Zhu Yi1b696de2007-09-27 11:27:41 +08002639 while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
Zhu Yib481de92007-09-25 17:54:57 -07002640 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07002641 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2642 ant_toggle_cnt++;
2643 else if (priv &&
2644 rs_toggle_antenna(valid_tx_ant,
2645 &new_rate, &tbl_type))
2646 ant_toggle_cnt = 1;
2647}
Zhu Yi98d7e092007-09-27 11:27:42 +08002648
Ben Cahill77626352007-11-29 11:09:44 +08002649 /* Override next rate if needed for debug purposes */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002650 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Ben Cahill77626352007-11-29 11:09:44 +08002651
2652 /* Fill next table entry */
Zhu Yib481de92007-09-25 17:54:57 -07002653 lq_cmd->rs_table[index].rate_n_flags =
Guy Cohen39e88502008-04-23 17:14:57 -07002654 cpu_to_le32(new_rate);
Zhu Yi1b696de2007-09-27 11:27:41 +08002655 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002656 index++;
2657 }
2658
Guy Cohen39e88502008-04-23 17:14:57 -07002659 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band, &tbl_type,
Zhu Yib481de92007-09-25 17:54:57 -07002660 &rate_idx);
2661
Ben Cahill77626352007-11-29 11:09:44 +08002662 /* Indicate to uCode which entries might be MIMO.
2663 * If initial rate was MIMO, this will finally end up
2664 * as (IWL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
Zhu Yib481de92007-09-25 17:54:57 -07002665 if (is_mimo(tbl_type.lq_type))
2666 lq_cmd->general_params.mimo_delimiter = index;
2667
Ben Cahill77626352007-11-29 11:09:44 +08002668 /* Get next rate */
Guy Cohen39e88502008-04-23 17:14:57 -07002669 new_rate = rs_get_lower_rate(lq_sta, &tbl_type, rate_idx,
2670 use_ht_possible);
Zhu Yib481de92007-09-25 17:54:57 -07002671
Ben Cahill77626352007-11-29 11:09:44 +08002672 /* How many times should we repeat the next rate? */
Zhu Yib481de92007-09-25 17:54:57 -07002673 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07002674 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2675 ant_toggle_cnt++;
2676 else if (priv &&
2677 rs_toggle_antenna(valid_tx_ant,
2678 &new_rate, &tbl_type))
2679 ant_toggle_cnt = 1;
2680
Zhu Yi1b696de2007-09-27 11:27:41 +08002681 repeat_rate = IWL_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002682 } else {
Zhu Yi1b696de2007-09-27 11:27:41 +08002683 repeat_rate = IWL_HT_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002684 }
Zhu Yib481de92007-09-25 17:54:57 -07002685
Ben Cahill77626352007-11-29 11:09:44 +08002686 /* Don't allow HT rates after next pass.
2687 * rs_get_lower_rate() will change type to LQ_A or LQ_G. */
Zhu Yib481de92007-09-25 17:54:57 -07002688 use_ht_possible = 0;
2689
Ben Cahill77626352007-11-29 11:09:44 +08002690 /* Override next rate if needed for debug purposes */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002691 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Ben Cahill77626352007-11-29 11:09:44 +08002692
2693 /* Fill next table entry */
Guy Cohen39e88502008-04-23 17:14:57 -07002694 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002695
2696 index++;
Zhu Yi1b696de2007-09-27 11:27:41 +08002697 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002698 }
2699
Wey-Yi Guy4d80d722009-10-02 13:44:01 -07002700 lq_cmd->agg_params.agg_frame_cnt_limit = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
Wey-Yi Guy13c33a02009-06-03 11:44:11 -07002701 lq_cmd->agg_params.agg_dis_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
2702 lq_cmd->agg_params.agg_time_limit =
2703 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
Zhu Yib481de92007-09-25 17:54:57 -07002704}
2705
Johannes Berg4b7679a2008-09-18 18:14:18 +02002706static void *rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
Zhu Yib481de92007-09-25 17:54:57 -07002707{
Johannes Berg4b7679a2008-09-18 18:14:18 +02002708 return hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07002709}
2710/* rate scale requires free function to be implemented */
2711static void rs_free(void *priv_rate)
2712{
2713 return;
2714}
2715
Johannes Berg4b7679a2008-09-18 18:14:18 +02002716static void rs_free_sta(void *priv_r, struct ieee80211_sta *sta,
2717 void *priv_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002718{
Rami Rosen45527c22008-10-07 09:50:01 +02002719 struct iwl_priv *priv __maybe_unused = priv_r;
Zhu Yib481de92007-09-25 17:54:57 -07002720
Tomas Winklere1623442009-01-27 14:27:56 -08002721 IWL_DEBUG_RATE(priv, "enter\n");
Tomas Winklere1623442009-01-27 14:27:56 -08002722 IWL_DEBUG_RATE(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07002723}
2724
2725
Zhu Yi93dc6462007-09-27 11:27:37 +08002726#ifdef CONFIG_MAC80211_DEBUGFS
Zhu Yi5ae212c2007-09-27 11:27:38 +08002727static int open_file_generic(struct inode *inode, struct file *file)
2728{
2729 file->private_data = inode->i_private;
2730 return 0;
2731}
Tomas Winklere227cea2008-07-18 13:53:05 +08002732static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
2733 u32 *rate_n_flags, int index)
Zhu Yi98d7e092007-09-27 11:27:42 +08002734{
Ester Kummerbf403db2008-05-05 10:22:40 +08002735 struct iwl_priv *priv;
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07002736 u8 valid_tx_ant;
2737 u8 ant_sel_tx;
Ester Kummerbf403db2008-05-05 10:22:40 +08002738
2739 priv = lq_sta->drv;
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07002740 valid_tx_ant = priv->hw_params.valid_tx_ant;
Guy Cohen39e88502008-04-23 17:14:57 -07002741 if (lq_sta->dbg_fixed_rate) {
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07002742 ant_sel_tx =
2743 ((lq_sta->dbg_fixed_rate & RATE_MCS_ANT_ABC_MSK)
2744 >> RATE_MCS_ANT_POS);
2745 if ((valid_tx_ant & ant_sel_tx) == ant_sel_tx) {
Guy Cohen39e88502008-04-23 17:14:57 -07002746 *rate_n_flags = lq_sta->dbg_fixed_rate;
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07002747 IWL_DEBUG_RATE(priv, "Fixed rate ON\n");
Guy Cohen39e88502008-04-23 17:14:57 -07002748 } else {
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07002749 lq_sta->dbg_fixed_rate = 0;
2750 IWL_ERR(priv,
2751 "Invalid antenna selection 0x%X, Valid is 0x%X\n",
2752 ant_sel_tx, valid_tx_ant);
2753 IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
Guy Cohen39e88502008-04-23 17:14:57 -07002754 }
Guy Cohen39e88502008-04-23 17:14:57 -07002755 } else {
Tomas Winklere1623442009-01-27 14:27:56 -08002756 IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
Zhu Yi98d7e092007-09-27 11:27:42 +08002757 }
Zhu Yi98d7e092007-09-27 11:27:42 +08002758}
2759
2760static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
2761 const char __user *user_buf, size_t count, loff_t *ppos)
2762{
Tomas Winklere227cea2008-07-18 13:53:05 +08002763 struct iwl_lq_sta *lq_sta = file->private_data;
Ester Kummerbf403db2008-05-05 10:22:40 +08002764 struct iwl_priv *priv;
Zhu Yi98d7e092007-09-27 11:27:42 +08002765 char buf[64];
2766 int buf_size;
2767 u32 parsed_rate;
2768
Ester Kummerbf403db2008-05-05 10:22:40 +08002769 priv = lq_sta->drv;
Zhu Yi98d7e092007-09-27 11:27:42 +08002770 memset(buf, 0, sizeof(buf));
2771 buf_size = min(count, sizeof(buf) - 1);
2772 if (copy_from_user(buf, user_buf, buf_size))
2773 return -EFAULT;
2774
2775 if (sscanf(buf, "%x", &parsed_rate) == 1)
Guy Cohen39e88502008-04-23 17:14:57 -07002776 lq_sta->dbg_fixed_rate = parsed_rate;
Zhu Yi98d7e092007-09-27 11:27:42 +08002777 else
Guy Cohen39e88502008-04-23 17:14:57 -07002778 lq_sta->dbg_fixed_rate = 0;
Zhu Yi98d7e092007-09-27 11:27:42 +08002779
Guy Cohen39e88502008-04-23 17:14:57 -07002780 lq_sta->active_legacy_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
2781 lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2782 lq_sta->active_mimo2_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2783 lq_sta->active_mimo3_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
Zhu Yi98d7e092007-09-27 11:27:42 +08002784
Tomas Winklere1623442009-01-27 14:27:56 -08002785 IWL_DEBUG_RATE(priv, "sta_id %d rate 0x%X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07002786 lq_sta->lq.sta_id, lq_sta->dbg_fixed_rate);
Zhu Yi98d7e092007-09-27 11:27:42 +08002787
Guy Cohen39e88502008-04-23 17:14:57 -07002788 if (lq_sta->dbg_fixed_rate) {
Guy Cohen07bc28e2008-04-23 17:15:03 -07002789 rs_fill_link_cmd(NULL, lq_sta, lq_sta->dbg_fixed_rate);
Tomas Winkler66c73db2008-04-15 16:01:40 -07002790 iwl_send_lq_cmd(lq_sta->drv, &lq_sta->lq, CMD_ASYNC);
Zhu Yi98d7e092007-09-27 11:27:42 +08002791 }
2792
2793 return count;
2794}
Zhu Yi0209dc12007-09-27 11:27:43 +08002795
Zhu Yi5ae212c2007-09-27 11:27:38 +08002796static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
2797 char __user *user_buf, size_t count, loff_t *ppos)
2798{
Frank Seidela412c802009-03-01 20:25:38 +01002799 char *buff;
Zhu Yi5ae212c2007-09-27 11:27:38 +08002800 int desc = 0;
2801 int i = 0;
Wey-Yi Guy12b96812009-04-08 11:39:27 -07002802 int index = 0;
Frank Seidela412c802009-03-01 20:25:38 +01002803 ssize_t ret;
Zhu Yi5ae212c2007-09-27 11:27:38 +08002804
Tomas Winklere227cea2008-07-18 13:53:05 +08002805 struct iwl_lq_sta *lq_sta = file->private_data;
Wey-Yi Guyd8ae4f52009-03-10 14:35:07 -07002806 struct iwl_priv *priv;
Wey-Yi Guyadb7b5e2009-03-10 14:35:08 -07002807 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002808
Wey-Yi Guyd8ae4f52009-03-10 14:35:07 -07002809 priv = lq_sta->drv;
Frank Seidela412c802009-03-01 20:25:38 +01002810 buff = kmalloc(1024, GFP_KERNEL);
2811 if (!buff)
2812 return -ENOMEM;
2813
Tomas Winklerc33104f2008-01-14 17:46:21 -08002814 desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
Zhu Yi98d7e092007-09-27 11:27:42 +08002815 desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002816 lq_sta->total_failed, lq_sta->total_success,
Guy Cohen39e88502008-04-23 17:14:57 -07002817 lq_sta->active_legacy_rate);
Zhu Yi98d7e092007-09-27 11:27:42 +08002818 desc += sprintf(buff+desc, "fixed rate 0x%X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07002819 lq_sta->dbg_fixed_rate);
Wey-Yi Guyd8ae4f52009-03-10 14:35:07 -07002820 desc += sprintf(buff+desc, "valid_tx_ant %s%s%s\n",
2821 (priv->hw_params.valid_tx_ant & ANT_A) ? "ANT_A," : "",
2822 (priv->hw_params.valid_tx_ant & ANT_B) ? "ANT_B," : "",
2823 (priv->hw_params.valid_tx_ant & ANT_C) ? "ANT_C" : "");
Wey-Yi Guyadb7b5e2009-03-10 14:35:08 -07002824 desc += sprintf(buff+desc, "lq type %s\n",
2825 (is_legacy(tbl->lq_type)) ? "legacy" : "HT");
2826 if (is_Ht(tbl->lq_type)) {
2827 desc += sprintf(buff+desc, " %s",
2828 (is_siso(tbl->lq_type)) ? "SISO" :
2829 ((is_mimo2(tbl->lq_type)) ? "MIMO2" : "MIMO3"));
2830 desc += sprintf(buff+desc, " %s",
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07002831 (tbl->is_ht40) ? "40MHz" : "20MHz");
Daniel C Halperine3949d62009-09-17 10:43:50 -07002832 desc += sprintf(buff+desc, " %s %s %s\n", (tbl->is_SGI) ? "SGI" : "",
2833 (lq_sta->is_green) ? "GF enabled" : "",
2834 (lq_sta->is_agg) ? "AGG on" : "");
Wey-Yi Guyadb7b5e2009-03-10 14:35:08 -07002835 }
Wey-Yi Guy12b96812009-04-08 11:39:27 -07002836 desc += sprintf(buff+desc, "last tx rate=0x%X\n",
2837 lq_sta->last_rate_n_flags);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002838 desc += sprintf(buff+desc, "general:"
2839 "flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002840 lq_sta->lq.general_params.flags,
2841 lq_sta->lq.general_params.mimo_delimiter,
2842 lq_sta->lq.general_params.single_stream_ant_msk,
2843 lq_sta->lq.general_params.dual_stream_ant_msk);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002844
2845 desc += sprintf(buff+desc, "agg:"
2846 "time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002847 le16_to_cpu(lq_sta->lq.agg_params.agg_time_limit),
2848 lq_sta->lq.agg_params.agg_dis_start_th,
2849 lq_sta->lq.agg_params.agg_frame_cnt_limit);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002850
2851 desc += sprintf(buff+desc,
2852 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002853 lq_sta->lq.general_params.start_rate_index[0],
2854 lq_sta->lq.general_params.start_rate_index[1],
2855 lq_sta->lq.general_params.start_rate_index[2],
2856 lq_sta->lq.general_params.start_rate_index[3]);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002857
Wey-Yi Guy12b96812009-04-08 11:39:27 -07002858 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
2859 index = iwl_hwrate_to_plcp_idx(
2860 le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags));
2861 if (is_legacy(tbl->lq_type)) {
2862 desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps\n",
2863 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags),
2864 iwl_rate_mcs[index].mbps);
2865 } else {
2866 desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps (%s)\n",
2867 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags),
2868 iwl_rate_mcs[index].mbps, iwl_rate_mcs[index].mcs);
2869 }
2870 }
Zhu Yi5ae212c2007-09-27 11:27:38 +08002871
Frank Seidela412c802009-03-01 20:25:38 +01002872 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2873 kfree(buff);
2874 return ret;
Zhu Yi5ae212c2007-09-27 11:27:38 +08002875}
2876
2877static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
Zhu Yi98d7e092007-09-27 11:27:42 +08002878 .write = rs_sta_dbgfs_scale_table_write,
Zhu Yi5ae212c2007-09-27 11:27:38 +08002879 .read = rs_sta_dbgfs_scale_table_read,
2880 .open = open_file_generic,
2881};
Zhu Yi0209dc12007-09-27 11:27:43 +08002882static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
2883 char __user *user_buf, size_t count, loff_t *ppos)
2884{
Frank Seidela412c802009-03-01 20:25:38 +01002885 char *buff;
Zhu Yi0209dc12007-09-27 11:27:43 +08002886 int desc = 0;
2887 int i, j;
Frank Seidela412c802009-03-01 20:25:38 +01002888 ssize_t ret;
Zhu Yi0209dc12007-09-27 11:27:43 +08002889
Tomas Winklere227cea2008-07-18 13:53:05 +08002890 struct iwl_lq_sta *lq_sta = file->private_data;
Frank Seidela412c802009-03-01 20:25:38 +01002891
2892 buff = kmalloc(1024, GFP_KERNEL);
2893 if (!buff)
2894 return -ENOMEM;
2895
Zhu Yi0209dc12007-09-27 11:27:43 +08002896 for (i = 0; i < LQ_SIZE; i++) {
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07002897 desc += sprintf(buff+desc,
2898 "%s type=%d SGI=%d HT40=%d DUP=%d GF=%d\n"
Zhu Yi0209dc12007-09-27 11:27:43 +08002899 "rate=0x%X\n",
Abhijeet Kolekarc3056062008-11-12 13:14:08 -08002900 lq_sta->active_tbl == i ? "*" : "x",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002901 lq_sta->lq_info[i].lq_type,
2902 lq_sta->lq_info[i].is_SGI,
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07002903 lq_sta->lq_info[i].is_ht40,
Tomas Winklerc33104f2008-01-14 17:46:21 -08002904 lq_sta->lq_info[i].is_dup,
Wey-Yi Guy2681b202009-05-21 13:44:22 -07002905 lq_sta->is_green,
Guy Cohen39e88502008-04-23 17:14:57 -07002906 lq_sta->lq_info[i].current_rate);
Zhu Yi0209dc12007-09-27 11:27:43 +08002907 for (j = 0; j < IWL_RATE_COUNT; j++) {
2908 desc += sprintf(buff+desc,
Tomas Winklerc33104f2008-01-14 17:46:21 -08002909 "counter=%d success=%d %%=%d\n",
2910 lq_sta->lq_info[i].win[j].counter,
2911 lq_sta->lq_info[i].win[j].success_counter,
2912 lq_sta->lq_info[i].win[j].success_ratio);
Zhu Yi0209dc12007-09-27 11:27:43 +08002913 }
2914 }
Frank Seidela412c802009-03-01 20:25:38 +01002915 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2916 kfree(buff);
2917 return ret;
Zhu Yi0209dc12007-09-27 11:27:43 +08002918}
2919
2920static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
2921 .read = rs_sta_dbgfs_stats_table_read,
2922 .open = open_file_generic,
2923};
Zhu Yi5ae212c2007-09-27 11:27:38 +08002924
Wey-Yi Guy38167452009-05-08 13:44:43 -07002925static ssize_t rs_sta_dbgfs_rate_scale_data_read(struct file *file,
2926 char __user *user_buf, size_t count, loff_t *ppos)
2927{
2928 char buff[120];
2929 int desc = 0;
2930 ssize_t ret;
2931
2932 struct iwl_lq_sta *lq_sta = file->private_data;
2933 struct iwl_priv *priv;
2934 struct iwl_scale_tbl_info *tbl = &lq_sta->lq_info[lq_sta->active_tbl];
2935
2936 priv = lq_sta->drv;
2937
2938 if (is_Ht(tbl->lq_type))
2939 desc += sprintf(buff+desc,
2940 "Bit Rate= %d Mb/s\n",
2941 tbl->expected_tpt[lq_sta->last_txrate_idx]);
2942 else
2943 desc += sprintf(buff+desc,
2944 "Bit Rate= %d Mb/s\n",
2945 iwl_rates[lq_sta->last_txrate_idx].ieee >> 1);
2946 desc += sprintf(buff+desc,
2947 "Signal Level= %d dBm\tNoise Level= %d dBm\n",
2948 priv->last_rx_rssi, priv->last_rx_noise);
2949 desc += sprintf(buff+desc,
2950 "Tsf= 0x%llx\tBeacon time= 0x%08X\n",
2951 priv->last_tsf, priv->last_beacon_time);
2952
2953 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2954 return ret;
2955}
2956
2957static const struct file_operations rs_sta_dbgfs_rate_scale_data_ops = {
2958 .read = rs_sta_dbgfs_rate_scale_data_read,
2959 .open = open_file_generic,
2960};
2961
Zhu Yi93dc6462007-09-27 11:27:37 +08002962static void rs_add_debugfs(void *priv, void *priv_sta,
2963 struct dentry *dir)
2964{
Tomas Winklere227cea2008-07-18 13:53:05 +08002965 struct iwl_lq_sta *lq_sta = priv_sta;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002966 lq_sta->rs_sta_dbgfs_scale_table_file =
Wey-Yi Guy43e85112009-11-20 12:04:58 -08002967 debugfs_create_file("rate_scale_table", S_IRUSR | S_IWUSR, dir,
Tomas Winklerc33104f2008-01-14 17:46:21 -08002968 lq_sta, &rs_sta_dbgfs_scale_table_ops);
2969 lq_sta->rs_sta_dbgfs_stats_table_file =
Wey-Yi Guy43e85112009-11-20 12:04:58 -08002970 debugfs_create_file("rate_stats_table", S_IRUSR, dir,
Tomas Winklerc33104f2008-01-14 17:46:21 -08002971 lq_sta, &rs_sta_dbgfs_stats_table_ops);
Wey-Yi Guy38167452009-05-08 13:44:43 -07002972 lq_sta->rs_sta_dbgfs_rate_scale_data_file =
Wey-Yi Guy43e85112009-11-20 12:04:58 -08002973 debugfs_create_file("rate_scale_data", S_IRUSR, dir,
Wey-Yi Guy38167452009-05-08 13:44:43 -07002974 lq_sta, &rs_sta_dbgfs_rate_scale_data_ops);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002975 lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file =
Wey-Yi Guy43e85112009-11-20 12:04:58 -08002976 debugfs_create_u8("tx_agg_tid_enable", S_IRUSR | S_IWUSR, dir,
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002977 &lq_sta->tx_agg_tid_en);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002978
Zhu Yi93dc6462007-09-27 11:27:37 +08002979}
2980
2981static void rs_remove_debugfs(void *priv, void *priv_sta)
2982{
Tomas Winklere227cea2008-07-18 13:53:05 +08002983 struct iwl_lq_sta *lq_sta = priv_sta;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002984 debugfs_remove(lq_sta->rs_sta_dbgfs_scale_table_file);
2985 debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
Wey-Yi Guy38167452009-05-08 13:44:43 -07002986 debugfs_remove(lq_sta->rs_sta_dbgfs_rate_scale_data_file);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002987 debugfs_remove(lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file);
Zhu Yi93dc6462007-09-27 11:27:37 +08002988}
2989#endif
2990
Zhu Yib481de92007-09-25 17:54:57 -07002991static struct rate_control_ops rs_ops = {
2992 .module = NULL,
2993 .name = RS_NAME,
2994 .tx_status = rs_tx_status,
2995 .get_rate = rs_get_rate,
2996 .rate_init = rs_rate_init,
Zhu Yib481de92007-09-25 17:54:57 -07002997 .alloc = rs_alloc,
2998 .free = rs_free,
2999 .alloc_sta = rs_alloc_sta,
3000 .free_sta = rs_free_sta,
Zhu Yi93dc6462007-09-27 11:27:37 +08003001#ifdef CONFIG_MAC80211_DEBUGFS
3002 .add_sta_debugfs = rs_add_debugfs,
3003 .remove_sta_debugfs = rs_remove_debugfs,
3004#endif
Zhu Yib481de92007-09-25 17:54:57 -07003005};
3006
Tomas Winklere227cea2008-07-18 13:53:05 +08003007int iwlagn_rate_control_register(void)
Zhu Yib481de92007-09-25 17:54:57 -07003008{
Reinette Chatre897e1cf2008-03-28 16:21:09 -07003009 return ieee80211_rate_control_register(&rs_ops);
Zhu Yib481de92007-09-25 17:54:57 -07003010}
3011
Tomas Winklere227cea2008-07-18 13:53:05 +08003012void iwlagn_rate_control_unregister(void)
Zhu Yib481de92007-09-25 17:54:57 -07003013{
3014 ieee80211_rate_control_unregister(&rs_ops);
3015}
3016