blob: 7e5cf1a2b21e17016d4023224a32a55e3879cfb4 [file] [log] [blame]
Zhu Yib481de92007-09-25 17:54:57 -07001/******************************************************************************
2 *
Reinette Chatre1f447802010-01-15 13:43:41 -08003 * Copyright(c) 2005 - 2010 Intel Corporation. All rights reserved.
Zhu Yib481de92007-09-25 17:54:57 -07004 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
17 *
18 * The full GNU General Public License is included in this distribution in the
19 * file called LICENSE.
20 *
21 * Contact Information:
Winkler, Tomas759ef892008-12-09 11:28:58 -080022 * Intel Linux Wireless <ilw@linux.intel.com>
Zhu Yib481de92007-09-25 17:54:57 -070023 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24 *
25 *****************************************************************************/
26#include <linux/kernel.h>
27#include <linux/init.h>
28#include <linux/skbuff.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Zhu Yib481de92007-09-25 17:54:57 -070030#include <linux/wireless.h>
31#include <net/mac80211.h>
Zhu Yib481de92007-09-25 17:54:57 -070032
33#include <linux/netdevice.h>
34#include <linux/etherdevice.h>
35#include <linux/delay.h>
36
37#include <linux/workqueue.h>
38
Tomas Winkler3e0d4cb2008-04-24 11:55:38 -070039#include "iwl-dev.h"
Emmanuel Grumbachbe1f3ab62008-06-12 09:47:18 +080040#include "iwl-sta.h"
Tomas Winkler857485c2008-03-21 13:53:44 -070041#include "iwl-core.h"
Johannes Berga30e3112010-09-22 18:02:01 +020042#include "iwl-agn.h"
Zhu Yib481de92007-09-25 17:54:57 -070043
Tomas Winklere227cea2008-07-18 13:53:05 +080044#define RS_NAME "iwl-agn-rs"
Zhu Yib481de92007-09-25 17:54:57 -070045
Guy Cohenaade00c2008-04-23 17:14:59 -070046#define NUM_TRY_BEFORE_ANT_TOGGLE 1
Zhu Yib481de92007-09-25 17:54:57 -070047#define IWL_NUMBER_TRY 1
48#define IWL_HT_NUMBER_TRY 3
49
Ben Cahill77626352007-11-29 11:09:44 +080050#define IWL_RATE_MAX_WINDOW 62 /* # tx in history window */
51#define IWL_RATE_MIN_FAILURE_TH 6 /* min failures to calc tpt */
52#define IWL_RATE_MIN_SUCCESS_TH 8 /* min successes to calc tpt */
53
Abbas, Mohamed7d049e52009-01-20 21:33:53 -080054/* max allowed rate miss before sync LQ cmd */
55#define IWL_MISSED_RATE_MAX 15
Ben Cahill77626352007-11-29 11:09:44 +080056/* max time to accum history 2 seconds */
Mohamed Abbas447fee72009-04-20 14:37:02 -070057#define IWL_RATE_SCALE_FLUSH_INTVL (3*HZ)
Zhu Yib481de92007-09-25 17:54:57 -070058
59static u8 rs_ht_to_legacy[] = {
60 IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
61 IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
62 IWL_RATE_6M_INDEX,
63 IWL_RATE_6M_INDEX, IWL_RATE_9M_INDEX,
64 IWL_RATE_12M_INDEX, IWL_RATE_18M_INDEX,
65 IWL_RATE_24M_INDEX, IWL_RATE_36M_INDEX,
66 IWL_RATE_48M_INDEX, IWL_RATE_54M_INDEX
67};
68
Guy Cohenaade00c2008-04-23 17:14:59 -070069static const u8 ant_toggle_lookup[] = {
70 /*ANT_NONE -> */ ANT_NONE,
71 /*ANT_A -> */ ANT_B,
72 /*ANT_B -> */ ANT_C,
73 /*ANT_AB -> */ ANT_BC,
74 /*ANT_C -> */ ANT_A,
75 /*ANT_AC -> */ ANT_AB,
76 /*ANT_BC -> */ ANT_AC,
77 /*ANT_ABC -> */ ANT_ABC,
78};
79
Tomas Winklerc79dd5b2008-03-12 16:58:50 -070080static void rs_rate_scale_perform(struct iwl_priv *priv,
Gábor Stefanik514d65c2009-04-23 19:36:08 +020081 struct sk_buff *skb,
Johannes Berg4b7679a2008-09-18 18:14:18 +020082 struct ieee80211_sta *sta,
83 struct iwl_lq_sta *lq_sta);
Wey-Yi Guy46f93812009-07-24 11:13:03 -070084static void rs_fill_link_cmd(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +080085 struct iwl_lq_sta *lq_sta, u32 rate_n_flags);
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -070086static void rs_stay_in_table(struct iwl_lq_sta *lq_sta, bool force_search);
Zhu Yib481de92007-09-25 17:54:57 -070087
88
Zhu Yi98d7e092007-09-27 11:27:42 +080089#ifdef CONFIG_MAC80211_DEBUGFS
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#else
Tomas Winklere227cea2008-07-18 13:53:05 +080093static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
94 u32 *rate_n_flags, int index)
Zhu Yi98d7e092007-09-27 11:27:42 +080095{}
96#endif
Ben Cahill77626352007-11-29 11:09:44 +080097
Daniel C Halperine3949d62009-09-17 10:43:50 -070098/**
99 * The following tables contain the expected throughput metrics for all rates
100 *
101 * 1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
102 *
103 * where invalid entries are zeros.
104 *
105 * CCK rates are only valid in legacy table and will only be used in G
106 * (2.4 GHz) band.
Ben Cahill77626352007-11-29 11:09:44 +0800107 */
Wey-Yi Guy584a0f02009-04-01 14:33:24 -0700108
Daniel C Halperine3949d62009-09-17 10:43:50 -0700109static s32 expected_tpt_legacy[IWL_RATE_COUNT] = {
110 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 0
Zhu Yib481de92007-09-25 17:54:57 -0700111};
112
Daniel C Halperine3949d62009-09-17 10:43:50 -0700113static s32 expected_tpt_siso20MHz[4][IWL_RATE_COUNT] = {
114 {0, 0, 0, 0, 42, 0, 76, 102, 124, 158, 183, 193, 202}, /* Norm */
115 {0, 0, 0, 0, 46, 0, 82, 110, 132, 167, 192, 202, 210}, /* SGI */
116 {0, 0, 0, 0, 48, 0, 93, 135, 176, 251, 319, 351, 381}, /* AGG */
117 {0, 0, 0, 0, 53, 0, 102, 149, 193, 275, 348, 381, 413}, /* AGG+SGI */
Zhu Yib481de92007-09-25 17:54:57 -0700118};
119
Daniel C Halperine3949d62009-09-17 10:43:50 -0700120static s32 expected_tpt_siso40MHz[4][IWL_RATE_COUNT] = {
121 {0, 0, 0, 0, 77, 0, 127, 160, 184, 220, 242, 250, 257}, /* Norm */
122 {0, 0, 0, 0, 83, 0, 135, 169, 193, 229, 250, 257, 264}, /* SGI */
123 {0, 0, 0, 0, 96, 0, 182, 259, 328, 451, 553, 598, 640}, /* AGG */
124 {0, 0, 0, 0, 106, 0, 199, 282, 357, 487, 593, 640, 683}, /* AGG+SGI */
Zhu Yib481de92007-09-25 17:54:57 -0700125};
126
Daniel C Halperine3949d62009-09-17 10:43:50 -0700127static s32 expected_tpt_mimo2_20MHz[4][IWL_RATE_COUNT] = {
128 {0, 0, 0, 0, 74, 0, 123, 155, 179, 213, 235, 243, 250}, /* Norm */
129 {0, 0, 0, 0, 81, 0, 131, 164, 187, 221, 242, 250, 256}, /* SGI */
130 {0, 0, 0, 0, 92, 0, 175, 250, 317, 436, 534, 578, 619}, /* AGG */
131 {0, 0, 0, 0, 102, 0, 192, 273, 344, 470, 573, 619, 660}, /* AGG+SGI*/
Zhu Yib481de92007-09-25 17:54:57 -0700132};
133
Daniel C Halperine3949d62009-09-17 10:43:50 -0700134static s32 expected_tpt_mimo2_40MHz[4][IWL_RATE_COUNT] = {
135 {0, 0, 0, 0, 123, 0, 182, 214, 235, 264, 279, 285, 289}, /* Norm */
136 {0, 0, 0, 0, 131, 0, 191, 222, 242, 270, 284, 289, 293}, /* SGI */
137 {0, 0, 0, 0, 180, 0, 327, 446, 545, 708, 828, 878, 922}, /* AGG */
138 {0, 0, 0, 0, 197, 0, 355, 481, 584, 752, 872, 922, 966}, /* AGG+SGI */
Zhu Yib481de92007-09-25 17:54:57 -0700139};
140
Daniel C Halperine3949d62009-09-17 10:43:50 -0700141static s32 expected_tpt_mimo3_20MHz[4][IWL_RATE_COUNT] = {
142 {0, 0, 0, 0, 99, 0, 153, 186, 208, 239, 256, 263, 268}, /* Norm */
143 {0, 0, 0, 0, 106, 0, 162, 194, 215, 246, 262, 268, 273}, /* SGI */
144 {0, 0, 0, 0, 134, 0, 249, 346, 431, 574, 685, 732, 775}, /* AGG */
145 {0, 0, 0, 0, 148, 0, 272, 376, 465, 614, 727, 775, 818}, /* AGG+SGI */
Zhu Yib481de92007-09-25 17:54:57 -0700146};
147
Daniel C Halperine3949d62009-09-17 10:43:50 -0700148static s32 expected_tpt_mimo3_40MHz[4][IWL_RATE_COUNT] = {
149 {0, 0, 0, 0, 152, 0, 211, 239, 255, 279, 290, 294, 297}, /* Norm */
150 {0, 0, 0, 0, 160, 0, 219, 245, 261, 284, 294, 297, 300}, /* SGI */
151 {0, 0, 0, 0, 254, 0, 443, 584, 695, 868, 984, 1030, 1070}, /* AGG */
152 {0, 0, 0, 0, 277, 0, 478, 624, 737, 911, 1026, 1070, 1109}, /* AGG+SGI */
Wey-Yi Guy584a0f02009-04-01 14:33:24 -0700153};
154
Wey-Yi Guy12b96812009-04-08 11:39:27 -0700155/* mbps, mcs */
Tobias Klauser79496732009-12-23 14:18:11 +0100156static const struct iwl_rate_mcs_info iwl_rate_mcs[IWL_RATE_COUNT] = {
Daniel C Halperine3949d62009-09-17 10:43:50 -0700157 { "1", "BPSK DSSS"},
158 { "2", "QPSK DSSS"},
159 {"5.5", "BPSK CCK"},
160 { "11", "QPSK CCK"},
161 { "6", "BPSK 1/2"},
162 { "9", "BPSK 1/2"},
163 { "12", "QPSK 1/2"},
164 { "18", "QPSK 3/4"},
165 { "24", "16QAM 1/2"},
166 { "36", "16QAM 3/4"},
167 { "48", "64QAM 2/3"},
168 { "54", "64QAM 3/4"},
169 { "60", "64QAM 5/6"},
Wey-Yi Guy12b96812009-04-08 11:39:27 -0700170};
171
Wey-Yi Guya9c146b2009-05-22 11:01:48 -0700172#define MCS_INDEX_PER_STREAM (8)
173
Guy Cohen39e88502008-04-23 17:14:57 -0700174static inline u8 rs_extract_rate(u32 rate_n_flags)
Zhu Yib481de92007-09-25 17:54:57 -0700175{
176 return (u8)(rate_n_flags & 0xFF);
177}
178
Tomas Winklere227cea2008-07-18 13:53:05 +0800179static void rs_rate_scale_clear_window(struct iwl_rate_scale_data *window)
Zhu Yib481de92007-09-25 17:54:57 -0700180{
181 window->data = 0;
182 window->success_counter = 0;
183 window->success_ratio = IWL_INVALID_VALUE;
184 window->counter = 0;
185 window->average_tpt = IWL_INVALID_VALUE;
186 window->stamp = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700187}
188
Guy Cohenaade00c2008-04-23 17:14:59 -0700189static inline u8 rs_is_valid_ant(u8 valid_antenna, u8 ant_type)
190{
Tomas Winkler3ac7f142008-07-21 02:40:14 +0300191 return (ant_type & valid_antenna) == ant_type;
Guy Cohenaade00c2008-04-23 17:14:59 -0700192}
193
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200194/*
195 * removes the old data from the statistics. All data that is older than
196 * TID_MAX_TIME_DIFF, will be deleted.
197 */
Tomas Winklere227cea2008-07-18 13:53:05 +0800198static void rs_tl_rm_old_stats(struct iwl_traffic_load *tl, u32 curr_time)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200199{
200 /* The oldest age we want to keep */
201 u32 oldest_time = curr_time - TID_MAX_TIME_DIFF;
202
203 while (tl->queue_count &&
204 (tl->time_stamp < oldest_time)) {
205 tl->total -= tl->packet_count[tl->head];
206 tl->packet_count[tl->head] = 0;
207 tl->time_stamp += TID_QUEUE_CELL_SPACING;
208 tl->queue_count--;
209 tl->head++;
210 if (tl->head >= TID_QUEUE_MAX_SIZE)
211 tl->head = 0;
212 }
213}
214
215/*
216 * increment traffic load value for tid and also remove
217 * any old values if passed the certain time period
218 */
Tomas Winklere227cea2008-07-18 13:53:05 +0800219static u8 rs_tl_add_packet(struct iwl_lq_sta *lq_data,
Ron Rindjunskyfaa29712008-06-12 09:46:58 +0800220 struct ieee80211_hdr *hdr)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200221{
222 u32 curr_time = jiffies_to_msecs(jiffies);
223 u32 time_diff;
224 s32 index;
Tomas Winklere227cea2008-07-18 13:53:05 +0800225 struct iwl_traffic_load *tl = NULL;
Tomas Winkler54dbb522008-05-15 13:54:06 +0800226 u8 tid;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200227
Tomas Winkler417f1142008-11-12 13:14:06 -0800228 if (ieee80211_is_data_qos(hdr->frame_control)) {
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700229 u8 *qc = ieee80211_get_qos_ctl(hdr);
Tomas Winkler54dbb522008-05-15 13:54:06 +0800230 tid = qc[0] & 0xf;
231 } else
Ron Rindjunskyfaa29712008-06-12 09:46:58 +0800232 return MAX_TID_COUNT;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200233
Reinette Chatree6a6cf42009-08-13 13:30:50 -0700234 if (unlikely(tid >= TID_MAX_LOAD_COUNT))
235 return MAX_TID_COUNT;
236
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200237 tl = &lq_data->load[tid];
238
239 curr_time -= curr_time % TID_ROUND_VALUE;
240
241 /* Happens only for the first packet. Initialize the data */
242 if (!(tl->queue_count)) {
243 tl->total = 1;
244 tl->time_stamp = curr_time;
245 tl->queue_count = 1;
246 tl->head = 0;
247 tl->packet_count[0] = 1;
Ron Rindjunskyfaa29712008-06-12 09:46:58 +0800248 return MAX_TID_COUNT;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200249 }
250
251 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
252 index = time_diff / TID_QUEUE_CELL_SPACING;
253
254 /* The history is too long: remove data that is older than */
255 /* TID_MAX_TIME_DIFF */
256 if (index >= TID_QUEUE_MAX_SIZE)
257 rs_tl_rm_old_stats(tl, curr_time);
258
259 index = (tl->head + index) % TID_QUEUE_MAX_SIZE;
260 tl->packet_count[index] = tl->packet_count[index] + 1;
261 tl->total = tl->total + 1;
262
263 if ((index + 1) > tl->queue_count)
264 tl->queue_count = index + 1;
Ron Rindjunskyfaa29712008-06-12 09:46:58 +0800265
266 return tid;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200267}
268
269/*
270 get the traffic load value for tid
271*/
Tomas Winklere227cea2008-07-18 13:53:05 +0800272static u32 rs_tl_get_load(struct iwl_lq_sta *lq_data, u8 tid)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200273{
274 u32 curr_time = jiffies_to_msecs(jiffies);
275 u32 time_diff;
276 s32 index;
Tomas Winklere227cea2008-07-18 13:53:05 +0800277 struct iwl_traffic_load *tl = NULL;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200278
279 if (tid >= TID_MAX_LOAD_COUNT)
280 return 0;
281
282 tl = &(lq_data->load[tid]);
283
284 curr_time -= curr_time % TID_ROUND_VALUE;
285
286 if (!(tl->queue_count))
287 return 0;
288
289 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
290 index = time_diff / TID_QUEUE_CELL_SPACING;
291
292 /* The history is too long: remove data that is older than */
293 /* TID_MAX_TIME_DIFF */
294 if (index >= TID_QUEUE_MAX_SIZE)
295 rs_tl_rm_old_stats(tl, curr_time);
296
297 return tl->total;
298}
299
Wey-Yi Guy82ca9342010-04-12 14:02:36 -0700300static int rs_tl_turn_on_agg_for_tid(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +0800301 struct iwl_lq_sta *lq_data, u8 tid,
Johannes Berg4b7679a2008-09-18 18:14:18 +0200302 struct ieee80211_sta *sta)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200303{
Wey-Yi Guy82ca9342010-04-12 14:02:36 -0700304 int ret = -EAGAIN;
Johannes Berg290f5992010-08-23 07:56:58 -0700305 u32 load;
306
307 /*
308 * Don't create TX aggregation sessions when in high
309 * BT traffic, as they would just be disrupted by BT.
310 */
311 if (priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH) {
312 IWL_ERR(priv, "BT traffic (%d), no aggregation allowed\n",
313 priv->bt_traffic_load);
314 return ret;
315 }
316
317 load = rs_tl_get_load(lq_data, tid);
Wey-Yi Guy45d42702010-02-03 12:24:44 -0800318
Andy Lutomirski24110542010-07-25 13:14:29 -0400319 if (load > IWL_AGG_LOAD_THRESHOLD) {
Tomas Winklere1623442009-01-27 14:27:56 -0800320 IWL_DEBUG_HT(priv, "Starting Tx agg: STA: %pM tid: %d\n",
Johannes Berge1749612008-10-27 15:59:26 -0700321 sta->addr, tid);
Wey-Yi Guy45d42702010-02-03 12:24:44 -0800322 ret = ieee80211_start_tx_ba_session(sta, tid);
323 if (ret == -EAGAIN) {
324 /*
325 * driver and mac80211 is out of sync
326 * this might be cause by reloading firmware
327 * stop the tx ba session here
328 */
Andy Lutomirski24110542010-07-25 13:14:29 -0400329 IWL_ERR(priv, "Fail start Tx agg on tid: %d\n",
Wey-Yi Guy45d42702010-02-03 12:24:44 -0800330 tid);
Johannes Berg6a8579d2010-05-27 14:41:07 +0200331 ieee80211_stop_tx_ba_session(sta, tid);
Wey-Yi Guy45d42702010-02-03 12:24:44 -0800332 }
Andy Lutomirski24110542010-07-25 13:14:29 -0400333 } else {
334 IWL_ERR(priv, "Aggregation not enabled for tid %d "
335 "because load = %u\n", tid, load);
336 }
Wey-Yi Guy82ca9342010-04-12 14:02:36 -0700337 return ret;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200338}
339
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700340static void rs_tl_turn_on_agg(struct iwl_priv *priv, u8 tid,
Tomas Winklere227cea2008-07-18 13:53:05 +0800341 struct iwl_lq_sta *lq_data,
Johannes Berg4b7679a2008-09-18 18:14:18 +0200342 struct ieee80211_sta *sta)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200343{
Wey-Yi Guycfecc6b2010-06-18 11:33:15 -0700344 if (tid < TID_MAX_LOAD_COUNT)
345 rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
346 else
347 IWL_ERR(priv, "tid exceeds max load count: %d/%d\n",
348 tid, TID_MAX_LOAD_COUNT);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200349}
350
Guy Cohen39e88502008-04-23 17:14:57 -0700351static inline int get_num_of_ant_from_rate(u32 rate_n_flags)
Guy Cohenfde0db32008-04-21 15:42:01 -0700352{
Tomas Winkler3ac7f142008-07-21 02:40:14 +0300353 return !!(rate_n_flags & RATE_MCS_ANT_A_MSK) +
354 !!(rate_n_flags & RATE_MCS_ANT_B_MSK) +
355 !!(rate_n_flags & RATE_MCS_ANT_C_MSK);
Guy Cohenfde0db32008-04-21 15:42:01 -0700356}
357
Shanyu Zhao04f2dec2010-03-19 13:34:45 -0700358/*
359 * Static function to get the expected throughput from an iwl_scale_tbl_info
360 * that wraps a NULL pointer check
361 */
362static s32 get_expected_tpt(struct iwl_scale_tbl_info *tbl, int rs_index)
363{
364 if (tbl->expected_tpt)
365 return tbl->expected_tpt[rs_index];
366 return 0;
367}
368
Ben Cahill77626352007-11-29 11:09:44 +0800369/**
370 * rs_collect_tx_data - Update the success/failure sliding window
371 *
372 * We keep a sliding window of the last 62 packets transmitted
373 * at this rate. window->data contains the bitmask of successful
374 * packets.
375 */
Shanyu Zhao04f2dec2010-03-19 13:34:45 -0700376static int rs_collect_tx_data(struct iwl_scale_tbl_info *tbl,
377 int scale_index, int attempts, int successes)
Zhu Yib481de92007-09-25 17:54:57 -0700378{
Tomas Winklere227cea2008-07-18 13:53:05 +0800379 struct iwl_rate_scale_data *window = NULL;
Guy Cohen39e88502008-04-23 17:14:57 -0700380 static const u64 mask = (((u64)1) << (IWL_RATE_MAX_WINDOW - 1));
Shanyu Zhao04f2dec2010-03-19 13:34:45 -0700381 s32 fail_count, tpt;
Zhu Yib481de92007-09-25 17:54:57 -0700382
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800383 if (scale_index < 0 || scale_index >= IWL_RATE_COUNT)
384 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -0700385
Daniel C Halperine3949d62009-09-17 10:43:50 -0700386 /* Select window for current tx bit rate */
Shanyu Zhao04f2dec2010-03-19 13:34:45 -0700387 window = &(tbl->win[scale_index]);
388
389 /* Get expected throughput */
390 tpt = get_expected_tpt(tbl, scale_index);
Zhu Yib481de92007-09-25 17:54:57 -0700391
Ben Cahill77626352007-11-29 11:09:44 +0800392 /*
393 * Keep track of only the latest 62 tx frame attempts in this rate's
394 * history window; anything older isn't really relevant any more.
395 * If we have filled up the sliding window, drop the oldest attempt;
396 * if the oldest attempt (highest bit in bitmap) shows "success",
397 * subtract "1" from the success counter (this is the main reason
398 * we keep these bitmaps!).
399 */
Daniel C Halperine3949d62009-09-17 10:43:50 -0700400 while (attempts > 0) {
Guy Cohen39e88502008-04-23 17:14:57 -0700401 if (window->counter >= IWL_RATE_MAX_WINDOW) {
402
403 /* remove earliest */
404 window->counter = IWL_RATE_MAX_WINDOW - 1;
405
Ron Rindjunsky99556432008-01-28 14:07:25 +0200406 if (window->data & mask) {
407 window->data &= ~mask;
Guy Cohen39e88502008-04-23 17:14:57 -0700408 window->success_counter--;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200409 }
Zhu Yib481de92007-09-25 17:54:57 -0700410 }
Zhu Yib481de92007-09-25 17:54:57 -0700411
Ron Rindjunsky99556432008-01-28 14:07:25 +0200412 /* Increment frames-attempted counter */
413 window->counter++;
Ben Cahill77626352007-11-29 11:09:44 +0800414
Daniel C Halperine3949d62009-09-17 10:43:50 -0700415 /* Shift bitmap by one frame to throw away oldest history */
Guy Cohen90d77952008-09-09 10:54:53 +0800416 window->data <<= 1;
Daniel C Halperine3949d62009-09-17 10:43:50 -0700417
418 /* Mark the most recent #successes attempts as successful */
Ron Rindjunsky99556432008-01-28 14:07:25 +0200419 if (successes > 0) {
Guy Cohen39e88502008-04-23 17:14:57 -0700420 window->success_counter++;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200421 window->data |= 0x1;
422 successes--;
423 }
424
Daniel C Halperine3949d62009-09-17 10:43:50 -0700425 attempts--;
Zhu Yib481de92007-09-25 17:54:57 -0700426 }
427
Ben Cahill77626352007-11-29 11:09:44 +0800428 /* Calculate current success ratio, avoid divide-by-0! */
Zhu Yib481de92007-09-25 17:54:57 -0700429 if (window->counter > 0)
430 window->success_ratio = 128 * (100 * window->success_counter)
431 / window->counter;
432 else
433 window->success_ratio = IWL_INVALID_VALUE;
434
435 fail_count = window->counter - window->success_counter;
436
Ben Cahill77626352007-11-29 11:09:44 +0800437 /* Calculate average throughput, if we have enough history. */
Zhu Yib481de92007-09-25 17:54:57 -0700438 if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
439 (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
440 window->average_tpt = (window->success_ratio * tpt + 64) / 128;
441 else
442 window->average_tpt = IWL_INVALID_VALUE;
443
Ben Cahill77626352007-11-29 11:09:44 +0800444 /* Tag this window as having been updated */
Zhu Yib481de92007-09-25 17:54:57 -0700445 window->stamp = jiffies;
446
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800447 return 0;
Zhu Yib481de92007-09-25 17:54:57 -0700448}
449
Ben Cahill77626352007-11-29 11:09:44 +0800450/*
451 * Fill uCode API rate_n_flags field, based on "search" or "active" table.
452 */
Guy Cohen39e88502008-04-23 17:14:57 -0700453/* FIXME:RS:remove this function and put the flags statically in the table */
Tomas Winkler978785a2008-12-19 10:37:31 +0800454static u32 rate_n_flags_from_tbl(struct iwl_priv *priv,
455 struct iwl_scale_tbl_info *tbl,
456 int index, u8 use_green)
Zhu Yib481de92007-09-25 17:54:57 -0700457{
Guy Cohen39e88502008-04-23 17:14:57 -0700458 u32 rate_n_flags = 0;
459
Zhu Yib481de92007-09-25 17:54:57 -0700460 if (is_legacy(tbl->lq_type)) {
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800461 rate_n_flags = iwl_rates[index].plcp;
Zhu Yib481de92007-09-25 17:54:57 -0700462 if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
Guy Cohen39e88502008-04-23 17:14:57 -0700463 rate_n_flags |= RATE_MCS_CCK_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700464
Guy Cohenfde0db32008-04-21 15:42:01 -0700465 } else if (is_Ht(tbl->lq_type)) {
466 if (index > IWL_LAST_OFDM_RATE) {
Tomas Winkler978785a2008-12-19 10:37:31 +0800467 IWL_ERR(priv, "Invalid HT rate index %d\n", index);
Zhu Yib481de92007-09-25 17:54:57 -0700468 index = IWL_LAST_OFDM_RATE;
Guy Cohenfde0db32008-04-21 15:42:01 -0700469 }
Guy Cohen39e88502008-04-23 17:14:57 -0700470 rate_n_flags = RATE_MCS_HT_MSK;
Guy Cohenfde0db32008-04-21 15:42:01 -0700471
472 if (is_siso(tbl->lq_type))
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800473 rate_n_flags |= iwl_rates[index].plcp_siso;
Guy Cohenfde0db32008-04-21 15:42:01 -0700474 else if (is_mimo2(tbl->lq_type))
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800475 rate_n_flags |= iwl_rates[index].plcp_mimo2;
Guy Cohenfde0db32008-04-21 15:42:01 -0700476 else
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800477 rate_n_flags |= iwl_rates[index].plcp_mimo3;
Zhu Yib481de92007-09-25 17:54:57 -0700478 } else {
Tomas Winkler978785a2008-12-19 10:37:31 +0800479 IWL_ERR(priv, "Invalid tbl->lq_type %d\n", tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -0700480 }
481
Guy Cohen39e88502008-04-23 17:14:57 -0700482 rate_n_flags |= ((tbl->ant_type << RATE_MCS_ANT_POS) &
Guy Cohenfde0db32008-04-21 15:42:01 -0700483 RATE_MCS_ANT_ABC_MSK);
Zhu Yib481de92007-09-25 17:54:57 -0700484
Guy Cohen39e88502008-04-23 17:14:57 -0700485 if (is_Ht(tbl->lq_type)) {
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700486 if (tbl->is_ht40) {
Guy Cohen39e88502008-04-23 17:14:57 -0700487 if (tbl->is_dup)
488 rate_n_flags |= RATE_MCS_DUP_MSK;
489 else
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700490 rate_n_flags |= RATE_MCS_HT40_MSK;
Guy Cohen39e88502008-04-23 17:14:57 -0700491 }
492 if (tbl->is_SGI)
493 rate_n_flags |= RATE_MCS_SGI_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700494
Guy Cohen39e88502008-04-23 17:14:57 -0700495 if (use_green) {
496 rate_n_flags |= RATE_MCS_GF_MSK;
497 if (is_siso(tbl->lq_type) && tbl->is_SGI) {
498 rate_n_flags &= ~RATE_MCS_SGI_MSK;
Tomas Winkler978785a2008-12-19 10:37:31 +0800499 IWL_ERR(priv, "GF was set with SGI:SISO\n");
Guy Cohen39e88502008-04-23 17:14:57 -0700500 }
501 }
Zhu Yib481de92007-09-25 17:54:57 -0700502 }
Guy Cohen39e88502008-04-23 17:14:57 -0700503 return rate_n_flags;
Zhu Yib481de92007-09-25 17:54:57 -0700504}
505
Ben Cahill77626352007-11-29 11:09:44 +0800506/*
507 * Interpret uCode API's rate_n_flags format,
508 * fill "search" or "active" tx mode table.
509 */
Guy Cohen39e88502008-04-23 17:14:57 -0700510static int rs_get_tbl_info_from_mcs(const u32 rate_n_flags,
Johannes Berg8318d782008-01-24 19:38:38 +0100511 enum ieee80211_band band,
Tomas Winklere227cea2008-07-18 13:53:05 +0800512 struct iwl_scale_tbl_info *tbl,
Zhu Yib481de92007-09-25 17:54:57 -0700513 int *rate_idx)
514{
Guy Cohen39e88502008-04-23 17:14:57 -0700515 u32 ant_msk = (rate_n_flags & RATE_MCS_ANT_ABC_MSK);
516 u8 num_of_ant = get_num_of_ant_from_rate(rate_n_flags);
517 u8 mcs;
Zhu Yib481de92007-09-25 17:54:57 -0700518
Wey-Yi Guy80e91582010-08-03 08:23:32 -0700519 memset(tbl, 0, sizeof(struct iwl_scale_tbl_info));
Tomas Winklere7d326ac2008-06-12 09:47:11 +0800520 *rate_idx = iwl_hwrate_to_plcp_idx(rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -0700521
Guy Cohenfde0db32008-04-21 15:42:01 -0700522 if (*rate_idx == IWL_RATE_INVALID) {
Zhu Yib481de92007-09-25 17:54:57 -0700523 *rate_idx = -1;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800524 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -0700525 }
Ben Cahill77626352007-11-29 11:09:44 +0800526 tbl->is_SGI = 0; /* default legacy setup */
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700527 tbl->is_ht40 = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700528 tbl->is_dup = 0;
Guy Cohenfde0db32008-04-21 15:42:01 -0700529 tbl->ant_type = (ant_msk >> RATE_MCS_ANT_POS);
530 tbl->lq_type = LQ_NONE;
Mohamed Abbasd6e93392009-05-08 13:44:39 -0700531 tbl->max_search = IWL_MAX_SEARCH;
Zhu Yib481de92007-09-25 17:54:57 -0700532
Ben Cahill77626352007-11-29 11:09:44 +0800533 /* legacy rate format */
Guy Cohen39e88502008-04-23 17:14:57 -0700534 if (!(rate_n_flags & RATE_MCS_HT_MSK)) {
Guy Cohenfde0db32008-04-21 15:42:01 -0700535 if (num_of_ant == 1) {
Johannes Berg8318d782008-01-24 19:38:38 +0100536 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700537 tbl->lq_type = LQ_A;
538 else
539 tbl->lq_type = LQ_G;
Zhu Yib481de92007-09-25 17:54:57 -0700540 }
Guy Cohenfde0db32008-04-21 15:42:01 -0700541 /* HT rate format */
Zhu Yib481de92007-09-25 17:54:57 -0700542 } else {
Guy Cohen39e88502008-04-23 17:14:57 -0700543 if (rate_n_flags & RATE_MCS_SGI_MSK)
Zhu Yib481de92007-09-25 17:54:57 -0700544 tbl->is_SGI = 1;
545
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700546 if ((rate_n_flags & RATE_MCS_HT40_MSK) ||
Guy Cohen39e88502008-04-23 17:14:57 -0700547 (rate_n_flags & RATE_MCS_DUP_MSK))
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700548 tbl->is_ht40 = 1;
Zhu Yib481de92007-09-25 17:54:57 -0700549
Guy Cohen39e88502008-04-23 17:14:57 -0700550 if (rate_n_flags & RATE_MCS_DUP_MSK)
Zhu Yib481de92007-09-25 17:54:57 -0700551 tbl->is_dup = 1;
Guy Cohenfde0db32008-04-21 15:42:01 -0700552
Guy Cohen39e88502008-04-23 17:14:57 -0700553 mcs = rs_extract_rate(rate_n_flags);
Guy Cohenfde0db32008-04-21 15:42:01 -0700554
Guy Cohen39e88502008-04-23 17:14:57 -0700555 /* SISO */
556 if (mcs <= IWL_RATE_SISO_60M_PLCP) {
Guy Cohenfde0db32008-04-21 15:42:01 -0700557 if (num_of_ant == 1)
558 tbl->lq_type = LQ_SISO; /*else NONE*/
559 /* MIMO2 */
Guy Cohen39e88502008-04-23 17:14:57 -0700560 } else if (mcs <= IWL_RATE_MIMO2_60M_PLCP) {
Guy Cohenfde0db32008-04-21 15:42:01 -0700561 if (num_of_ant == 2)
562 tbl->lq_type = LQ_MIMO2;
563 /* MIMO3 */
564 } else {
Mohamed Abbasd6e93392009-05-08 13:44:39 -0700565 if (num_of_ant == 3) {
566 tbl->max_search = IWL_MAX_11N_MIMO3_SEARCH;
Guy Cohenfde0db32008-04-21 15:42:01 -0700567 tbl->lq_type = LQ_MIMO3;
Mohamed Abbasd6e93392009-05-08 13:44:39 -0700568 }
Guy Cohenfde0db32008-04-21 15:42:01 -0700569 }
Zhu Yib481de92007-09-25 17:54:57 -0700570 }
571 return 0;
572}
Guy Cohenaade00c2008-04-23 17:14:59 -0700573
574/* switch to another antenna/antennas and return 1 */
575/* if no other valid antenna found, return 0 */
576static int rs_toggle_antenna(u32 valid_ant, u32 *rate_n_flags,
Tomas Winklere227cea2008-07-18 13:53:05 +0800577 struct iwl_scale_tbl_info *tbl)
Zhu Yib481de92007-09-25 17:54:57 -0700578{
Guy Cohenaade00c2008-04-23 17:14:59 -0700579 u8 new_ant_type;
580
581 if (!tbl->ant_type || tbl->ant_type > ANT_ABC)
582 return 0;
583
584 if (!rs_is_valid_ant(valid_ant, tbl->ant_type))
585 return 0;
586
587 new_ant_type = ant_toggle_lookup[tbl->ant_type];
588
589 while ((new_ant_type != tbl->ant_type) &&
590 !rs_is_valid_ant(valid_ant, new_ant_type))
591 new_ant_type = ant_toggle_lookup[new_ant_type];
592
593 if (new_ant_type == tbl->ant_type)
594 return 0;
595
596 tbl->ant_type = new_ant_type;
597 *rate_n_flags &= ~RATE_MCS_ANT_ABC_MSK;
598 *rate_n_flags |= new_ant_type << RATE_MCS_ANT_POS;
599 return 1;
Zhu Yib481de92007-09-25 17:54:57 -0700600}
601
Daniel C Halperinb2617932009-08-13 13:30:59 -0700602/**
603 * Green-field mode is valid if the station supports it and
604 * there are no non-GF stations present in the BSS.
605 */
Johannes Berg7e6a5882010-08-23 10:46:46 +0200606static bool rs_use_green(struct ieee80211_sta *sta)
Zhu Yib481de92007-09-25 17:54:57 -0700607{
Johannes Berg7e6a5882010-08-23 10:46:46 +0200608 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
609 struct iwl_rxon_context *ctx = sta_priv->common.ctx;
610
Daniel C Halperinb2617932009-08-13 13:30:59 -0700611 return (sta->ht_cap.cap & IEEE80211_HT_CAP_GRN_FLD) &&
Johannes Berg7e6a5882010-08-23 10:46:46 +0200612 !(ctx->ht.non_gf_sta_present);
Guy Cohenf935a6d2008-04-23 17:15:02 -0700613}
Zhu Yib481de92007-09-25 17:54:57 -0700614
615/**
616 * rs_get_supported_rates - get the available rates
617 *
618 * if management frame or broadcast frame only return
619 * basic available rates.
620 *
621 */
Tomas Winklere227cea2008-07-18 13:53:05 +0800622static u16 rs_get_supported_rates(struct iwl_lq_sta *lq_sta,
623 struct ieee80211_hdr *hdr,
624 enum iwl_table_type rate_type)
Zhu Yib481de92007-09-25 17:54:57 -0700625{
Guy Coheneecd6e52008-04-23 17:14:58 -0700626 if (is_legacy(rate_type)) {
627 return lq_sta->active_legacy_rate;
628 } else {
629 if (is_siso(rate_type))
630 return lq_sta->active_siso_rate;
631 else if (is_mimo2(rate_type))
632 return lq_sta->active_mimo2_rate;
633 else
634 return lq_sta->active_mimo3_rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -0800635 }
Zhu Yib481de92007-09-25 17:54:57 -0700636}
637
Ester Kummerbf403db2008-05-05 10:22:40 +0800638static u16 rs_get_adjacent_rate(struct iwl_priv *priv, u8 index, u16 rate_mask,
639 int rate_type)
Zhu Yib481de92007-09-25 17:54:57 -0700640{
641 u8 high = IWL_RATE_INVALID;
642 u8 low = IWL_RATE_INVALID;
643
Ian Schram01ebd062007-10-25 17:15:22 +0800644 /* 802.11A or ht walks to the next literal adjacent rate in
Zhu Yib481de92007-09-25 17:54:57 -0700645 * the rate table */
646 if (is_a_band(rate_type) || !is_legacy(rate_type)) {
647 int i;
648 u32 mask;
649
650 /* Find the previous rate that is in the rate mask */
651 i = index - 1;
652 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
653 if (rate_mask & mask) {
654 low = i;
655 break;
656 }
657 }
658
659 /* Find the next rate that is in the rate mask */
660 i = index + 1;
661 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
662 if (rate_mask & mask) {
663 high = i;
664 break;
665 }
666 }
667
668 return (high << 8) | low;
669 }
670
671 low = index;
672 while (low != IWL_RATE_INVALID) {
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800673 low = iwl_rates[low].prev_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700674 if (low == IWL_RATE_INVALID)
675 break;
676 if (rate_mask & (1 << low))
677 break;
Tomas Winklere1623442009-01-27 14:27:56 -0800678 IWL_DEBUG_RATE(priv, "Skipping masked lower rate: %d\n", low);
Zhu Yib481de92007-09-25 17:54:57 -0700679 }
680
681 high = index;
682 while (high != IWL_RATE_INVALID) {
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800683 high = iwl_rates[high].next_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700684 if (high == IWL_RATE_INVALID)
685 break;
686 if (rate_mask & (1 << high))
687 break;
Tomas Winklere1623442009-01-27 14:27:56 -0800688 IWL_DEBUG_RATE(priv, "Skipping masked higher rate: %d\n", high);
Zhu Yib481de92007-09-25 17:54:57 -0700689 }
690
691 return (high << 8) | low;
692}
693
Tomas Winklere227cea2008-07-18 13:53:05 +0800694static u32 rs_get_lower_rate(struct iwl_lq_sta *lq_sta,
695 struct iwl_scale_tbl_info *tbl,
696 u8 scale_index, u8 ht_possible)
Zhu Yib481de92007-09-25 17:54:57 -0700697{
Zhu Yib481de92007-09-25 17:54:57 -0700698 s32 low;
699 u16 rate_mask;
700 u16 high_low;
701 u8 switch_to_legacy = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -0800702 u8 is_green = lq_sta->is_green;
Wey-Yi Guy2ff65782009-09-11 10:38:18 -0700703 struct iwl_priv *priv = lq_sta->drv;
Zhu Yib481de92007-09-25 17:54:57 -0700704
705 /* check if we need to switch from HT to legacy rates.
706 * assumption is that mandatory rates (1Mbps or 6Mbps)
707 * are always supported (spec demand) */
708 if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_index)) {
709 switch_to_legacy = 1;
710 scale_index = rs_ht_to_legacy[scale_index];
Johannes Berg8318d782008-01-24 19:38:38 +0100711 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700712 tbl->lq_type = LQ_A;
713 else
714 tbl->lq_type = LQ_G;
715
Guy Cohen69fdb302008-04-23 17:15:01 -0700716 if (num_of_ant(tbl->ant_type) > 1)
Wey-Yi Guy2ff65782009-09-11 10:38:18 -0700717 tbl->ant_type =
718 first_antenna(priv->hw_params.valid_tx_ant);
Zhu Yib481de92007-09-25 17:54:57 -0700719
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700720 tbl->is_ht40 = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700721 tbl->is_SGI = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -0700722 tbl->max_search = IWL_MAX_SEARCH;
Zhu Yib481de92007-09-25 17:54:57 -0700723 }
724
Guy Coheneecd6e52008-04-23 17:14:58 -0700725 rate_mask = rs_get_supported_rates(lq_sta, NULL, tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -0700726
Ben Cahill77626352007-11-29 11:09:44 +0800727 /* Mask with station rate restriction */
Zhu Yib481de92007-09-25 17:54:57 -0700728 if (is_legacy(tbl->lq_type)) {
Ben Cahill77626352007-11-29 11:09:44 +0800729 /* supp_rates has no CCK bits in A mode */
Johannes Berg8318d782008-01-24 19:38:38 +0100730 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700731 rate_mask = (u16)(rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -0800732 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
Zhu Yib481de92007-09-25 17:54:57 -0700733 else
Tomas Winklerc33104f2008-01-14 17:46:21 -0800734 rate_mask = (u16)(rate_mask & lq_sta->supp_rates);
Zhu Yib481de92007-09-25 17:54:57 -0700735 }
736
Ben Cahill77626352007-11-29 11:09:44 +0800737 /* If we switched from HT to legacy, check current rate */
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800738 if (switch_to_legacy && (rate_mask & (1 << scale_index))) {
Guy Cohen39e88502008-04-23 17:14:57 -0700739 low = scale_index;
740 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700741 }
742
Ester Kummerbf403db2008-05-05 10:22:40 +0800743 high_low = rs_get_adjacent_rate(lq_sta->drv, scale_index, rate_mask,
744 tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -0700745 low = high_low & 0xff;
746
Guy Cohen39e88502008-04-23 17:14:57 -0700747 if (low == IWL_RATE_INVALID)
748 low = scale_index;
749
750out:
Tomas Winkler978785a2008-12-19 10:37:31 +0800751 return rate_n_flags_from_tbl(lq_sta->drv, tbl, low, is_green);
Zhu Yib481de92007-09-25 17:54:57 -0700752}
753
Ben Cahill77626352007-11-29 11:09:44 +0800754/*
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700755 * Simple function to compare two rate scale table types
756 */
757static bool table_type_matches(struct iwl_scale_tbl_info *a,
758 struct iwl_scale_tbl_info *b)
759{
760 return (a->lq_type == b->lq_type) && (a->ant_type == b->ant_type) &&
761 (a->is_SGI == b->is_SGI);
762}
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700763
Johannes Berg7e6a5882010-08-23 10:46:46 +0200764static void rs_bt_update_lq(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
765 struct iwl_lq_sta *lq_sta)
Wey-Yi Guybee008b2010-08-23 07:57:04 -0700766{
767 struct iwl_scale_tbl_info *tbl;
768 bool full_concurrent;
769 unsigned long flags;
770
771 spin_lock_irqsave(&priv->lock, flags);
772 if (priv->bt_ci_compliance && priv->bt_ant_couple_ok)
773 full_concurrent = true;
774 else
775 full_concurrent = false;
776 spin_unlock_irqrestore(&priv->lock, flags);
777
778 if (priv->bt_full_concurrent != full_concurrent) {
779 priv->bt_full_concurrent = full_concurrent;
780
781 /* Update uCode's rate table. */
782 tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
783 rs_fill_link_cmd(priv, lq_sta, tbl->current_rate);
Johannes Berg7e6a5882010-08-23 10:46:46 +0200784 iwl_send_lq_cmd(priv, ctx, &lq_sta->lq, CMD_ASYNC, false);
Wey-Yi Guybee008b2010-08-23 07:57:04 -0700785
786 queue_work(priv->workqueue, &priv->bt_full_concurrency);
787 }
788}
789
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700790/*
Ben Cahill77626352007-11-29 11:09:44 +0800791 * mac80211 sends us Tx status
792 */
Johannes Berg4b7679a2008-09-18 18:14:18 +0200793static void rs_tx_status(void *priv_r, struct ieee80211_supported_band *sband,
794 struct ieee80211_sta *sta, void *priv_sta,
Johannes Berge039fa42008-05-15 12:55:29 +0200795 struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -0700796{
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700797 int legacy_success;
798 int retries;
799 int rs_index, mac_index, i;
Tomas Winkler417f1142008-11-12 13:14:06 -0800800 struct iwl_lq_sta *lq_sta = priv_sta;
Tomas Winkler66c73db2008-04-15 16:01:40 -0700801 struct iwl_link_quality_cmd *table;
Zhu Yib481de92007-09-25 17:54:57 -0700802 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berg4b7679a2008-09-18 18:14:18 +0200803 struct iwl_priv *priv = (struct iwl_priv *)priv_r;
Johannes Berge039fa42008-05-15 12:55:29 +0200804 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Daniel C Halperin50273092009-08-28 09:44:45 -0700805 enum mac80211_rate_control_flags mac_flags;
Guy Cohen39e88502008-04-23 17:14:57 -0700806 u32 tx_rate;
Tomas Winklere227cea2008-07-18 13:53:05 +0800807 struct iwl_scale_tbl_info tbl_type;
Shanyu Zhao04f2dec2010-03-19 13:34:45 -0700808 struct iwl_scale_tbl_info *curr_tbl, *other_tbl, *tmp_tbl;
Johannes Berg7e6a5882010-08-23 10:46:46 +0200809 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
810 struct iwl_rxon_context *ctx = sta_priv->common.ctx;
Zhu Yib481de92007-09-25 17:54:57 -0700811
Tomas Winklere1623442009-01-27 14:27:56 -0800812 IWL_DEBUG_RATE_LIMIT(priv, "get frame ack response, update rate scale window\n");
Zhu Yib481de92007-09-25 17:54:57 -0700813
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800814 /* Treat uninitialized rate scaling data same as non-existing. */
815 if (!lq_sta) {
816 IWL_DEBUG_RATE(priv, "Station rate scaling not created yet.\n");
817 return;
818 } else if (!lq_sta->drv) {
819 IWL_DEBUG_RATE(priv, "Rate scaling not initialized yet.\n");
820 return;
821 }
822
Tomas Winkler417f1142008-11-12 13:14:06 -0800823 if (!ieee80211_is_data(hdr->frame_control) ||
Gábor Stefanik514d65c2009-04-23 19:36:08 +0200824 info->flags & IEEE80211_TX_CTL_NO_ACK)
Zhu Yib481de92007-09-25 17:54:57 -0700825 return;
826
Daniel C Halperine3949d62009-09-17 10:43:50 -0700827 /* This packet was aggregated but doesn't carry status info */
Johannes Berge039fa42008-05-15 12:55:29 +0200828 if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
829 !(info->flags & IEEE80211_TX_STAT_AMPDU))
Ron Rindjunsky99556432008-01-28 14:07:25 +0200830 return;
831
Ben Cahill77626352007-11-29 11:09:44 +0800832 /*
833 * Ignore this Tx frame response if its initial rate doesn't match
834 * that of latest Link Quality command. There may be stragglers
835 * from a previous Link Quality command, but we're no longer interested
836 * in those; they're either from the "active" mode while we're trying
837 * to check "search" mode, or a prior "search" mode after we've moved
838 * to a new "search" mode (which might become the new "active" mode).
839 */
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700840 table = &lq_sta->lq;
Guy Cohen39e88502008-04-23 17:14:57 -0700841 tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
842 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800843 if (priv->band == IEEE80211_BAND_5GHZ)
844 rs_index -= IWL_FIRST_OFDM_RATE;
Daniel C Halperin50273092009-08-28 09:44:45 -0700845 mac_flags = info->status.rates[0].flags;
846 mac_index = info->status.rates[0].idx;
Daniel C Halperin31513be2009-08-28 09:44:47 -0700847 /* For HT packets, map MCS to PLCP */
848 if (mac_flags & IEEE80211_TX_RC_MCS) {
849 mac_index &= RATE_MCS_CODE_MSK; /* Remove # of streams */
850 if (mac_index >= (IWL_RATE_9M_INDEX - IWL_FIRST_OFDM_RATE))
851 mac_index++;
Daniel C Halperin392a0ba2009-09-11 10:38:08 -0700852 /*
853 * mac80211 HT index is always zero-indexed; we need to move
854 * HT OFDM rates after CCK rates in 2.4 GHz band
855 */
856 if (priv->band == IEEE80211_BAND_2GHZ)
857 mac_index += IWL_FIRST_OFDM_RATE;
Daniel C Halperin31513be2009-08-28 09:44:47 -0700858 }
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700859 /* Here we actually compare this rate to the latest LQ command */
Daniel C Halperin50273092009-08-28 09:44:45 -0700860 if ((mac_index < 0) ||
861 (tbl_type.is_SGI != !!(mac_flags & IEEE80211_TX_RC_SHORT_GI)) ||
862 (tbl_type.is_ht40 != !!(mac_flags & IEEE80211_TX_RC_40_MHZ_WIDTH)) ||
863 (tbl_type.is_dup != !!(mac_flags & IEEE80211_TX_RC_DUP_DATA)) ||
Johannes Berge6a98542008-10-21 12:40:02 +0200864 (tbl_type.ant_type != info->antenna_sel_tx) ||
Daniel C Halperin50273092009-08-28 09:44:45 -0700865 (!!(tx_rate & RATE_MCS_HT_MSK) != !!(mac_flags & IEEE80211_TX_RC_MCS)) ||
866 (!!(tx_rate & RATE_MCS_GF_MSK) != !!(mac_flags & IEEE80211_TX_RC_GREEN_FIELD)) ||
Daniel C Halperin31513be2009-08-28 09:44:47 -0700867 (rs_index != mac_index)) {
868 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 -0700869 /*
870 * Since rates mis-match, the last LQ command may have failed.
871 * After IWL_MISSED_RATE_MAX mis-matches, resync the uCode with
872 * ... driver.
Mohamed Abbasd5e49032008-12-12 08:22:15 -0800873 */
Abbas, Mohamed7d049e52009-01-20 21:33:53 -0800874 lq_sta->missed_rate_counter++;
875 if (lq_sta->missed_rate_counter > IWL_MISSED_RATE_MAX) {
876 lq_sta->missed_rate_counter = 0;
Johannes Berg7e6a5882010-08-23 10:46:46 +0200877 iwl_send_lq_cmd(priv, ctx, &lq_sta->lq, CMD_ASYNC, false);
Abbas, Mohamed7d049e52009-01-20 21:33:53 -0800878 }
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700879 /* Regardless, ignore this status info for outdated rate */
880 return;
881 } else
882 /* Rate did match, so reset the missed_rate_counter */
883 lq_sta->missed_rate_counter = 0;
884
885 /* Figure out if rate scale algorithm is in active or search table */
886 if (table_type_matches(&tbl_type,
887 &(lq_sta->lq_info[lq_sta->active_tbl]))) {
888 curr_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
889 other_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
890 } else if (table_type_matches(&tbl_type,
891 &lq_sta->lq_info[1 - lq_sta->active_tbl])) {
892 curr_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
893 other_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
894 } else {
895 IWL_DEBUG_RATE(priv, "Neither active nor search matches tx rate\n");
Wey-Yi Guy80e91582010-08-03 08:23:32 -0700896 tmp_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
897 IWL_DEBUG_RATE(priv, "active- lq:%x, ant:%x, SGI:%d\n",
898 tmp_tbl->lq_type, tmp_tbl->ant_type, tmp_tbl->is_SGI);
899 tmp_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
900 IWL_DEBUG_RATE(priv, "search- lq:%x, ant:%x, SGI:%d\n",
901 tmp_tbl->lq_type, tmp_tbl->ant_type, tmp_tbl->is_SGI);
902 IWL_DEBUG_RATE(priv, "actual- lq:%x, ant:%x, SGI:%d\n",
903 tbl_type.lq_type, tbl_type.ant_type, tbl_type.is_SGI);
904 /*
905 * no matching table found, let's by-pass the data collection
906 * and continue to perform rate scale to find the rate table
907 */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -0700908 rs_stay_in_table(lq_sta, true);
Wey-Yi Guy80e91582010-08-03 08:23:32 -0700909 goto done;
Zhu Yib481de92007-09-25 17:54:57 -0700910 }
Zhu Yib481de92007-09-25 17:54:57 -0700911
Ben Cahill77626352007-11-29 11:09:44 +0800912 /*
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700913 * Updating the frame history depends on whether packets were
914 * aggregated.
915 *
916 * For aggregation, all packets were transmitted at the same rate, the
917 * first index into rate scale table.
Ben Cahill77626352007-11-29 11:09:44 +0800918 */
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700919 if (info->flags & IEEE80211_TX_STAT_AMPDU) {
920 tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
921 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type,
922 &rs_index);
Shanyu Zhao04f2dec2010-03-19 13:34:45 -0700923 rs_collect_tx_data(curr_tbl, rs_index,
Daniel Halperine3a3cd82010-04-18 09:27:58 -0700924 info->status.ampdu_len,
925 info->status.ampdu_ack_len);
Zhu Yib481de92007-09-25 17:54:57 -0700926
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700927 /* Update success/fail counts if not searching for new mode */
928 if (lq_sta->stay_in_tbl) {
Daniel Halperine3a3cd82010-04-18 09:27:58 -0700929 lq_sta->total_success += info->status.ampdu_ack_len;
930 lq_sta->total_failed += (info->status.ampdu_len -
931 info->status.ampdu_ack_len);
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700932 }
933 } else {
934 /*
935 * For legacy, update frame history with for each Tx retry.
936 */
937 retries = info->status.rates[0].count - 1;
938 /* HW doesn't send more than 15 retries */
939 retries = min(retries, 15);
940
941 /* The last transmission may have been successful */
942 legacy_success = !!(info->flags & IEEE80211_TX_STAT_ACK);
943 /* Collect data for each rate used during failed TX attempts */
944 for (i = 0; i <= retries; ++i) {
945 tx_rate = le32_to_cpu(table->rs_table[i].rate_n_flags);
946 rs_get_tbl_info_from_mcs(tx_rate, priv->band,
947 &tbl_type, &rs_index);
948 /*
949 * Only collect stats if retried rate is in the same RS
950 * table as active/search.
951 */
952 if (table_type_matches(&tbl_type, curr_tbl))
Shanyu Zhao04f2dec2010-03-19 13:34:45 -0700953 tmp_tbl = curr_tbl;
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700954 else if (table_type_matches(&tbl_type, other_tbl))
Shanyu Zhao04f2dec2010-03-19 13:34:45 -0700955 tmp_tbl = other_tbl;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200956 else
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700957 continue;
Shanyu Zhao04f2dec2010-03-19 13:34:45 -0700958 rs_collect_tx_data(tmp_tbl, rs_index, 1,
959 i < retries ? 0 : legacy_success);
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700960 }
961
962 /* Update success/fail counts if not searching for new mode */
963 if (lq_sta->stay_in_tbl) {
964 lq_sta->total_success += legacy_success;
965 lq_sta->total_failed += retries + (1 - legacy_success);
Ron Rindjunsky99556432008-01-28 14:07:25 +0200966 }
Zhu Yib481de92007-09-25 17:54:57 -0700967 }
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700968 /* The last TX rate is cached in lq_sta; it's set in if/else above */
969 lq_sta->last_rate_n_flags = tx_rate;
Wey-Yi Guy80e91582010-08-03 08:23:32 -0700970done:
Ben Cahill77626352007-11-29 11:09:44 +0800971 /* See if there's a better rate or modulation mode to try. */
Abbas, Mohamedc338ba32009-01-21 10:58:02 -0800972 if (sta && sta->supp_rates[sband->band])
Gábor Stefanik514d65c2009-04-23 19:36:08 +0200973 rs_rate_scale_perform(priv, skb, sta, lq_sta);
Wey-Yi Guybee008b2010-08-23 07:57:04 -0700974
975 /* Is there a need to switch between full concurrency and 3-wire? */
976 if (priv->bt_ant_couple_ok)
Johannes Berg7e6a5882010-08-23 10:46:46 +0200977 rs_bt_update_lq(priv, ctx, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -0700978}
979
Ben Cahill77626352007-11-29 11:09:44 +0800980/*
981 * Begin a period of staying with a selected modulation mode.
982 * Set "stay_in_tbl" flag to prevent any mode switches.
983 * Set frame tx success limits according to legacy vs. high-throughput,
984 * and reset overall (spanning all rates) tx success history statistics.
985 * These control how long we stay using same modulation mode before
986 * searching for a new mode.
987 */
Ester Kummerbf403db2008-05-05 10:22:40 +0800988static void rs_set_stay_in_table(struct iwl_priv *priv, u8 is_legacy,
Tomas Winklere227cea2008-07-18 13:53:05 +0800989 struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -0700990{
Tomas Winklere1623442009-01-27 14:27:56 -0800991 IWL_DEBUG_RATE(priv, "we are staying in the same table\n");
Tomas Winklerc33104f2008-01-14 17:46:21 -0800992 lq_sta->stay_in_tbl = 1; /* only place this gets set */
Zhu Yib481de92007-09-25 17:54:57 -0700993 if (is_legacy) {
Tomas Winklerc33104f2008-01-14 17:46:21 -0800994 lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT;
995 lq_sta->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
996 lq_sta->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
Zhu Yib481de92007-09-25 17:54:57 -0700997 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -0800998 lq_sta->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
999 lq_sta->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
1000 lq_sta->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
Zhu Yib481de92007-09-25 17:54:57 -07001001 }
Tomas Winklerc33104f2008-01-14 17:46:21 -08001002 lq_sta->table_count = 0;
1003 lq_sta->total_failed = 0;
1004 lq_sta->total_success = 0;
Mohamed Abbas447fee72009-04-20 14:37:02 -07001005 lq_sta->flush_timer = jiffies;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001006 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001007}
1008
Ben Cahill77626352007-11-29 11:09:44 +08001009/*
1010 * Find correct throughput table for given mode of modulation
1011 */
Tomas Winklere227cea2008-07-18 13:53:05 +08001012static void rs_set_expected_tpt_table(struct iwl_lq_sta *lq_sta,
1013 struct iwl_scale_tbl_info *tbl)
Zhu Yib481de92007-09-25 17:54:57 -07001014{
Daniel C Halperine3949d62009-09-17 10:43:50 -07001015 /* Used to choose among HT tables */
1016 s32 (*ht_tbl_pointer)[IWL_RATE_COUNT];
1017
1018 /* Check for invalid LQ type */
1019 if (WARN_ON_ONCE(!is_legacy(tbl->lq_type) && !is_Ht(tbl->lq_type))) {
1020 tbl->expected_tpt = expected_tpt_legacy;
1021 return;
1022 }
1023
1024 /* Legacy rates have only one table */
Zhu Yib481de92007-09-25 17:54:57 -07001025 if (is_legacy(tbl->lq_type)) {
Daniel C Halperine3949d62009-09-17 10:43:50 -07001026 tbl->expected_tpt = expected_tpt_legacy;
1027 return;
1028 }
1029
1030 /* Choose among many HT tables depending on number of streams
1031 * (SISO/MIMO2/MIMO3), channel width (20/40), SGI, and aggregation
1032 * status */
1033 if (is_siso(tbl->lq_type) && (!tbl->is_ht40 || lq_sta->is_dup))
1034 ht_tbl_pointer = expected_tpt_siso20MHz;
1035 else if (is_siso(tbl->lq_type))
1036 ht_tbl_pointer = expected_tpt_siso40MHz;
1037 else if (is_mimo2(tbl->lq_type) && (!tbl->is_ht40 || lq_sta->is_dup))
1038 ht_tbl_pointer = expected_tpt_mimo2_20MHz;
1039 else if (is_mimo2(tbl->lq_type))
1040 ht_tbl_pointer = expected_tpt_mimo2_40MHz;
1041 else if (is_mimo3(tbl->lq_type) && (!tbl->is_ht40 || lq_sta->is_dup))
1042 ht_tbl_pointer = expected_tpt_mimo3_20MHz;
1043 else /* if (is_mimo3(tbl->lq_type)) <-- must be true */
1044 ht_tbl_pointer = expected_tpt_mimo3_40MHz;
1045
1046 if (!tbl->is_SGI && !lq_sta->is_agg) /* Normal */
1047 tbl->expected_tpt = ht_tbl_pointer[0];
1048 else if (tbl->is_SGI && !lq_sta->is_agg) /* SGI */
1049 tbl->expected_tpt = ht_tbl_pointer[1];
1050 else if (!tbl->is_SGI && lq_sta->is_agg) /* AGG */
1051 tbl->expected_tpt = ht_tbl_pointer[2];
1052 else /* AGG+SGI */
1053 tbl->expected_tpt = ht_tbl_pointer[3];
Zhu Yib481de92007-09-25 17:54:57 -07001054}
1055
Ben Cahill77626352007-11-29 11:09:44 +08001056/*
1057 * Find starting rate for new "search" high-throughput mode of modulation.
1058 * Goal is to find lowest expected rate (under perfect conditions) that is
1059 * above the current measured throughput of "active" mode, to give new mode
1060 * a fair chance to prove itself without too many challenges.
1061 *
1062 * This gets called when transitioning to more aggressive modulation
1063 * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
1064 * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
1065 * to decrease to match "active" throughput. When moving from MIMO to SISO,
1066 * bit rate will typically need to increase, but not if performance was bad.
1067 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001068static s32 rs_get_best_rate(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001069 struct iwl_lq_sta *lq_sta,
1070 struct iwl_scale_tbl_info *tbl, /* "search" */
Guy Cohenf935a6d2008-04-23 17:15:02 -07001071 u16 rate_mask, s8 index)
Zhu Yib481de92007-09-25 17:54:57 -07001072{
Ben Cahill77626352007-11-29 11:09:44 +08001073 /* "active" values */
Tomas Winklere227cea2008-07-18 13:53:05 +08001074 struct iwl_scale_tbl_info *active_tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001075 &(lq_sta->lq_info[lq_sta->active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07001076 s32 active_sr = active_tbl->win[index].success_ratio;
Zhu Yib481de92007-09-25 17:54:57 -07001077 s32 active_tpt = active_tbl->expected_tpt[index];
Ben Cahill77626352007-11-29 11:09:44 +08001078
1079 /* expected "search" throughput */
1080 s32 *tpt_tbl = tbl->expected_tpt;
1081
1082 s32 new_rate, high, low, start_hi;
Zhu Yib481de92007-09-25 17:54:57 -07001083 u16 high_low;
Guy Cohenf935a6d2008-04-23 17:15:02 -07001084 s8 rate = index;
Zhu Yib481de92007-09-25 17:54:57 -07001085
1086 new_rate = high = low = start_hi = IWL_RATE_INVALID;
1087
1088 for (; ;) {
Ester Kummerbf403db2008-05-05 10:22:40 +08001089 high_low = rs_get_adjacent_rate(priv, rate, rate_mask,
1090 tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -07001091
1092 low = high_low & 0xff;
1093 high = (high_low >> 8) & 0xff;
1094
Ben Cahill77626352007-11-29 11:09:44 +08001095 /*
1096 * Lower the "search" bit rate, to give new "search" mode
1097 * approximately the same throughput as "active" if:
1098 *
1099 * 1) "Active" mode has been working modestly well (but not
1100 * great), and expected "search" throughput (under perfect
1101 * conditions) at candidate rate is above the actual
1102 * measured "active" throughput (but less than expected
1103 * "active" throughput under perfect conditions).
1104 * OR
1105 * 2) "Active" mode has been working perfectly or very well
1106 * and expected "search" throughput (under perfect
1107 * conditions) at candidate rate is above expected
1108 * "active" throughput (under perfect conditions).
1109 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001110 if ((((100 * tpt_tbl[rate]) > lq_sta->last_tpt) &&
Zhu Yib481de92007-09-25 17:54:57 -07001111 ((active_sr > IWL_RATE_DECREASE_TH) &&
1112 (active_sr <= IWL_RATE_HIGH_TH) &&
1113 (tpt_tbl[rate] <= active_tpt))) ||
1114 ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
1115 (tpt_tbl[rate] > active_tpt))) {
1116
Ben Cahill77626352007-11-29 11:09:44 +08001117 /* (2nd or later pass)
1118 * If we've already tried to raise the rate, and are
1119 * now trying to lower it, use the higher rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001120 if (start_hi != IWL_RATE_INVALID) {
1121 new_rate = start_hi;
1122 break;
1123 }
Ben Cahill77626352007-11-29 11:09:44 +08001124
Zhu Yib481de92007-09-25 17:54:57 -07001125 new_rate = rate;
Ben Cahill77626352007-11-29 11:09:44 +08001126
1127 /* Loop again with lower rate */
Zhu Yib481de92007-09-25 17:54:57 -07001128 if (low != IWL_RATE_INVALID)
1129 rate = low;
Ben Cahill77626352007-11-29 11:09:44 +08001130
1131 /* Lower rate not available, use the original */
Zhu Yib481de92007-09-25 17:54:57 -07001132 else
1133 break;
Ben Cahill77626352007-11-29 11:09:44 +08001134
1135 /* Else try to raise the "search" rate to match "active" */
Zhu Yib481de92007-09-25 17:54:57 -07001136 } else {
Ben Cahill77626352007-11-29 11:09:44 +08001137 /* (2nd or later pass)
1138 * If we've already tried to lower the rate, and are
1139 * now trying to raise it, use the lower rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001140 if (new_rate != IWL_RATE_INVALID)
1141 break;
Ben Cahill77626352007-11-29 11:09:44 +08001142
1143 /* Loop again with higher rate */
Zhu Yib481de92007-09-25 17:54:57 -07001144 else if (high != IWL_RATE_INVALID) {
1145 start_hi = high;
1146 rate = high;
Ben Cahill77626352007-11-29 11:09:44 +08001147
1148 /* Higher rate not available, use the original */
Zhu Yib481de92007-09-25 17:54:57 -07001149 } else {
Guy Cohen90d77952008-09-09 10:54:53 +08001150 new_rate = rate;
Zhu Yib481de92007-09-25 17:54:57 -07001151 break;
1152 }
1153 }
1154 }
1155
1156 return new_rate;
1157}
Zhu Yib481de92007-09-25 17:54:57 -07001158
Ben Cahill77626352007-11-29 11:09:44 +08001159/*
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001160 * Set up search table for MIMO2
Ben Cahill77626352007-11-29 11:09:44 +08001161 */
Guy Cohenfde0db32008-04-21 15:42:01 -07001162static int rs_switch_to_mimo2(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001163 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001164 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001165 struct ieee80211_sta *sta,
Tomas Winklere227cea2008-07-18 13:53:05 +08001166 struct iwl_scale_tbl_info *tbl, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001167{
Zhu Yib481de92007-09-25 17:54:57 -07001168 u16 rate_mask;
1169 s32 rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001170 s8 is_green = lq_sta->is_green;
Johannes Berg7e6a5882010-08-23 10:46:46 +02001171 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
1172 struct iwl_rxon_context *ctx = sta_priv->common.ctx;
Zhu Yib481de92007-09-25 17:54:57 -07001173
Luis R. Rodriguezde27e642008-12-23 15:58:44 -08001174 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
Zhu Yib481de92007-09-25 17:54:57 -07001175 return -1;
1176
Johannes Bergd9fe60d2008-10-09 12:13:49 +02001177 if (((sta->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 2)
Tomas Winkler00c5ae22008-09-03 11:26:42 +08001178 == WLAN_HT_CAP_SM_PS_STATIC)
Zhu Yib481de92007-09-25 17:54:57 -07001179 return -1;
1180
Ben Cahill77626352007-11-29 11:09:44 +08001181 /* Need both Tx chains/antennas to support MIMO */
Guy Cohenaade00c2008-04-23 17:14:59 -07001182 if (priv->hw_params.tx_chains_num < 2)
Zhu Yib481de92007-09-25 17:54:57 -07001183 return -1;
1184
Tomas Winklere1623442009-01-27 14:27:56 -08001185 IWL_DEBUG_RATE(priv, "LQ: try to switch to MIMO2\n");
Guy Cohen39e88502008-04-23 17:14:57 -07001186
1187 tbl->lq_type = LQ_MIMO2;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001188 tbl->is_dup = lq_sta->is_dup;
Zhu Yib481de92007-09-25 17:54:57 -07001189 tbl->action = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001190 tbl->max_search = IWL_MAX_SEARCH;
Guy Cohen39e88502008-04-23 17:14:57 -07001191 rate_mask = lq_sta->active_mimo2_rate;
1192
Johannes Berg7e6a5882010-08-23 10:46:46 +02001193 if (iwl_is_ht40_tx_allowed(priv, ctx, &sta->ht_cap))
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001194 tbl->is_ht40 = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001195 else
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001196 tbl->is_ht40 = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001197
Guy Coheneecd6e52008-04-23 17:14:58 -07001198 rs_set_expected_tpt_table(lq_sta, tbl);
Zhu Yib481de92007-09-25 17:54:57 -07001199
Guy Cohenf935a6d2008-04-23 17:15:02 -07001200 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
Zhu Yib481de92007-09-25 17:54:57 -07001201
Tomas Winklere1623442009-01-27 14:27:56 -08001202 IWL_DEBUG_RATE(priv, "LQ: MIMO2 best rate %d mask %X\n", rate, rate_mask);
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001203 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1204 IWL_DEBUG_RATE(priv, "Can't switch with index %d rate mask %x\n",
1205 rate, rate_mask);
1206 return -1;
1207 }
1208 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
Guy Cohen39e88502008-04-23 17:14:57 -07001209
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001210 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
1211 tbl->current_rate, is_green);
1212 return 0;
1213}
1214
1215/*
1216 * Set up search table for MIMO3
1217 */
1218static int rs_switch_to_mimo3(struct iwl_priv *priv,
1219 struct iwl_lq_sta *lq_sta,
1220 struct ieee80211_conf *conf,
1221 struct ieee80211_sta *sta,
1222 struct iwl_scale_tbl_info *tbl, int index)
1223{
1224 u16 rate_mask;
1225 s32 rate;
1226 s8 is_green = lq_sta->is_green;
Johannes Berg7e6a5882010-08-23 10:46:46 +02001227 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
1228 struct iwl_rxon_context *ctx = sta_priv->common.ctx;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001229
1230 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
1231 return -1;
1232
1233 if (((sta->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 2)
1234 == WLAN_HT_CAP_SM_PS_STATIC)
1235 return -1;
1236
1237 /* Need both Tx chains/antennas to support MIMO */
1238 if (priv->hw_params.tx_chains_num < 3)
1239 return -1;
1240
1241 IWL_DEBUG_RATE(priv, "LQ: try to switch to MIMO3\n");
1242
1243 tbl->lq_type = LQ_MIMO3;
1244 tbl->is_dup = lq_sta->is_dup;
1245 tbl->action = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001246 tbl->max_search = IWL_MAX_11N_MIMO3_SEARCH;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001247 rate_mask = lq_sta->active_mimo3_rate;
1248
Johannes Berg7e6a5882010-08-23 10:46:46 +02001249 if (iwl_is_ht40_tx_allowed(priv, ctx, &sta->ht_cap))
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001250 tbl->is_ht40 = 1;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001251 else
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001252 tbl->is_ht40 = 0;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001253
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001254 rs_set_expected_tpt_table(lq_sta, tbl);
1255
1256 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
1257
1258 IWL_DEBUG_RATE(priv, "LQ: MIMO3 best rate %d mask %X\n",
1259 rate, rate_mask);
Guy Cohen39e88502008-04-23 17:14:57 -07001260 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
Tomas Winklere1623442009-01-27 14:27:56 -08001261 IWL_DEBUG_RATE(priv, "Can't switch with index %d rate mask %x\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001262 rate, rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07001263 return -1;
Guy Cohen39e88502008-04-23 17:14:57 -07001264 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001265 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
Zhu Yib481de92007-09-25 17:54:57 -07001266
Tomas Winklere1623442009-01-27 14:27:56 -08001267 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001268 tbl->current_rate, is_green);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001269 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07001270}
1271
Ben Cahill77626352007-11-29 11:09:44 +08001272/*
1273 * Set up search table for SISO
1274 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001275static int rs_switch_to_siso(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001276 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001277 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001278 struct ieee80211_sta *sta,
Tomas Winklere227cea2008-07-18 13:53:05 +08001279 struct iwl_scale_tbl_info *tbl, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001280{
Zhu Yib481de92007-09-25 17:54:57 -07001281 u16 rate_mask;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001282 u8 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07001283 s32 rate;
Johannes Berg7e6a5882010-08-23 10:46:46 +02001284 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
1285 struct iwl_rxon_context *ctx = sta_priv->common.ctx;
Zhu Yib481de92007-09-25 17:54:57 -07001286
Luis R. Rodriguezde27e642008-12-23 15:58:44 -08001287 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
Zhu Yib481de92007-09-25 17:54:57 -07001288 return -1;
1289
Tomas Winklere1623442009-01-27 14:27:56 -08001290 IWL_DEBUG_RATE(priv, "LQ: try to switch to SISO\n");
Guy Cohen39e88502008-04-23 17:14:57 -07001291
Tomas Winklerc33104f2008-01-14 17:46:21 -08001292 tbl->is_dup = lq_sta->is_dup;
Zhu Yib481de92007-09-25 17:54:57 -07001293 tbl->lq_type = LQ_SISO;
1294 tbl->action = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001295 tbl->max_search = IWL_MAX_SEARCH;
Guy Cohen39e88502008-04-23 17:14:57 -07001296 rate_mask = lq_sta->active_siso_rate;
Zhu Yib481de92007-09-25 17:54:57 -07001297
Johannes Berg7e6a5882010-08-23 10:46:46 +02001298 if (iwl_is_ht40_tx_allowed(priv, ctx, &sta->ht_cap))
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001299 tbl->is_ht40 = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001300 else
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001301 tbl->is_ht40 = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001302
Zhu Yib481de92007-09-25 17:54:57 -07001303 if (is_green)
Guy Cohen39e88502008-04-23 17:14:57 -07001304 tbl->is_SGI = 0; /*11n spec: no SGI in SISO+Greenfield*/
Zhu Yib481de92007-09-25 17:54:57 -07001305
Guy Coheneecd6e52008-04-23 17:14:58 -07001306 rs_set_expected_tpt_table(lq_sta, tbl);
Guy Cohenf935a6d2008-04-23 17:15:02 -07001307 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
Zhu Yib481de92007-09-25 17:54:57 -07001308
Tomas Winklere1623442009-01-27 14:27:56 -08001309 IWL_DEBUG_RATE(priv, "LQ: get best rate %d mask %X\n", rate, rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07001310 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
Tomas Winklere1623442009-01-27 14:27:56 -08001311 IWL_DEBUG_RATE(priv, "can not switch with index %d rate mask %x\n",
Zhu Yib481de92007-09-25 17:54:57 -07001312 rate, rate_mask);
1313 return -1;
1314 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001315 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
Tomas Winklere1623442009-01-27 14:27:56 -08001316 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001317 tbl->current_rate, is_green);
Mohamed Abbas403ab562007-11-06 22:06:25 -08001318 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07001319}
1320
Ben Cahill77626352007-11-29 11:09:44 +08001321/*
1322 * Try to switch to new modulation mode from legacy
1323 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001324static int rs_move_legacy_other(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001325 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001326 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001327 struct ieee80211_sta *sta,
Zhu Yib481de92007-09-25 17:54:57 -07001328 int index)
1329{
Tomas Winklere227cea2008-07-18 13:53:05 +08001330 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1331 struct iwl_scale_tbl_info *search_tbl =
1332 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1333 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1334 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1335 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Wey-Yi Guyb1a78852010-07-31 08:52:03 -07001336 u8 start_action;
Guy Cohenfde0db32008-04-21 15:42:01 -07001337 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
Guy Cohen3110bef2008-09-09 10:54:54 +08001338 u8 tx_chains_num = priv->hw_params.tx_chains_num;
Guy Cohenfde0db32008-04-21 15:42:01 -07001339 int ret = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001340 u8 update_search_tbl_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001341
Johannes Berg290f5992010-08-23 07:56:58 -07001342 switch (priv->bt_traffic_load) {
1343 case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
1344 /* nothing */
1345 break;
1346 case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
1347 /* avoid antenna B unless MIMO */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001348 valid_tx_ant = first_antenna(priv->hw_params.valid_tx_ant);
Johannes Berg290f5992010-08-23 07:56:58 -07001349 if (tbl->action == IWL_LEGACY_SWITCH_ANTENNA2)
1350 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
1351 break;
1352 case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
1353 case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
1354 /* avoid antenna B and MIMO */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001355 valid_tx_ant = first_antenna(priv->hw_params.valid_tx_ant);
Johannes Berg290f5992010-08-23 07:56:58 -07001356 if (tbl->action >= IWL_LEGACY_SWITCH_ANTENNA2 &&
1357 tbl->action != IWL_LEGACY_SWITCH_SISO)
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001358 tbl->action = IWL_LEGACY_SWITCH_SISO;
Johannes Berg290f5992010-08-23 07:56:58 -07001359 break;
1360 default:
1361 IWL_ERR(priv, "Invalid BT load %d", priv->bt_traffic_load);
1362 break;
1363 }
1364
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001365 if (!iwl_ht_enabled(priv))
1366 /* stay in Legacy */
1367 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
Johannes Berg3ad3b922009-08-07 15:41:48 -07001368 else if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001369 tbl->action > IWL_LEGACY_SWITCH_SISO)
1370 tbl->action = IWL_LEGACY_SWITCH_SISO;
Wey-Yi Guybee008b2010-08-23 07:57:04 -07001371
1372 /* configure as 1x1 if bt full concurrency */
1373 if (priv->bt_full_concurrent) {
1374 if (!iwl_ht_enabled(priv))
1375 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
1376 else if (tbl->action >= IWL_LEGACY_SWITCH_ANTENNA2)
1377 tbl->action = IWL_LEGACY_SWITCH_SISO;
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001378 valid_tx_ant = first_antenna(priv->hw_params.valid_tx_ant);
Wey-Yi Guybee008b2010-08-23 07:57:04 -07001379 }
1380
Wey-Yi Guyb1a78852010-07-31 08:52:03 -07001381 start_action = tbl->action;
Zhu Yib481de92007-09-25 17:54:57 -07001382 for (; ;) {
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001383 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001384 switch (tbl->action) {
Guy Cohen3110bef2008-09-09 10:54:54 +08001385 case IWL_LEGACY_SWITCH_ANTENNA1:
1386 case IWL_LEGACY_SWITCH_ANTENNA2:
Tomas Winklere1623442009-01-27 14:27:56 -08001387 IWL_DEBUG_RATE(priv, "LQ: Legacy toggle Antenna\n");
Zhu Yib481de92007-09-25 17:54:57 -07001388
Guy Cohen3110bef2008-09-09 10:54:54 +08001389 if ((tbl->action == IWL_LEGACY_SWITCH_ANTENNA1 &&
1390 tx_chains_num <= 1) ||
1391 (tbl->action == IWL_LEGACY_SWITCH_ANTENNA2 &&
1392 tx_chains_num <= 2))
1393 break;
1394
Ben Cahill77626352007-11-29 11:09:44 +08001395 /* Don't change antenna if success has been great */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001396 if (window->success_ratio >= IWL_RS_GOOD_RATIO &&
1397 !priv->bt_full_concurrent &&
1398 priv->bt_traffic_load ==
1399 IWL_BT_COEX_TRAFFIC_LOAD_NONE)
Zhu Yib481de92007-09-25 17:54:57 -07001400 break;
Ben Cahill77626352007-11-29 11:09:44 +08001401
Ben Cahill77626352007-11-29 11:09:44 +08001402 /* Set up search table to try other antenna */
Zhu Yib481de92007-09-25 17:54:57 -07001403 memcpy(search_tbl, tbl, sz);
1404
Guy Cohenaade00c2008-04-23 17:14:59 -07001405 if (rs_toggle_antenna(valid_tx_ant,
1406 &search_tbl->current_rate, search_tbl)) {
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001407 update_search_tbl_counter = 1;
Guy Cohen3110bef2008-09-09 10:54:54 +08001408 rs_set_expected_tpt_table(lq_sta, search_tbl);
Guy Cohenaade00c2008-04-23 17:14:59 -07001409 goto out;
1410 }
Guy Cohena5e8b502008-05-29 16:35:11 +08001411 break;
Zhu Yib481de92007-09-25 17:54:57 -07001412 case IWL_LEGACY_SWITCH_SISO:
Tomas Winklere1623442009-01-27 14:27:56 -08001413 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to SISO\n");
Ben Cahill77626352007-11-29 11:09:44 +08001414
1415 /* Set up search table to try SISO */
Zhu Yib481de92007-09-25 17:54:57 -07001416 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001417 search_tbl->is_SGI = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001418 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001419 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001420 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001421 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001422 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001423 }
Zhu Yib481de92007-09-25 17:54:57 -07001424
1425 break;
Guy Cohen3110bef2008-09-09 10:54:54 +08001426 case IWL_LEGACY_SWITCH_MIMO2_AB:
1427 case IWL_LEGACY_SWITCH_MIMO2_AC:
1428 case IWL_LEGACY_SWITCH_MIMO2_BC:
Tomas Winklere1623442009-01-27 14:27:56 -08001429 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to MIMO2\n");
Ben Cahill77626352007-11-29 11:09:44 +08001430
1431 /* Set up search table to try MIMO */
Zhu Yib481de92007-09-25 17:54:57 -07001432 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001433 search_tbl->is_SGI = 0;
Guy Cohen3110bef2008-09-09 10:54:54 +08001434
1435 if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AB)
1436 search_tbl->ant_type = ANT_AB;
1437 else if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AC)
1438 search_tbl->ant_type = ANT_AC;
1439 else
1440 search_tbl->ant_type = ANT_BC;
1441
1442 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1443 break;
1444
Guy Cohenfde0db32008-04-21 15:42:01 -07001445 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001446 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001447 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001448 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001449 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001450 }
Zhu Yib481de92007-09-25 17:54:57 -07001451 break;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001452
1453 case IWL_LEGACY_SWITCH_MIMO3_ABC:
1454 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to MIMO3\n");
1455
1456 /* Set up search table to try MIMO3 */
1457 memcpy(search_tbl, tbl, sz);
1458 search_tbl->is_SGI = 0;
1459
1460 search_tbl->ant_type = ANT_ABC;
1461
1462 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1463 break;
1464
1465 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1466 search_tbl, index);
1467 if (!ret) {
1468 lq_sta->action_counter = 0;
1469 goto out;
1470 }
1471 break;
Zhu Yib481de92007-09-25 17:54:57 -07001472 }
1473 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001474 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001475 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
Zhu Yib481de92007-09-25 17:54:57 -07001476
1477 if (tbl->action == start_action)
1478 break;
1479
1480 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001481 search_tbl->lq_type = LQ_NONE;
Zhu Yib481de92007-09-25 17:54:57 -07001482 return 0;
1483
Guy Cohen3110bef2008-09-09 10:54:54 +08001484out:
1485 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001486 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001487 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001488 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001489 if (update_search_tbl_counter)
1490 search_tbl->action = tbl->action;
Zhu Yib481de92007-09-25 17:54:57 -07001491 return 0;
1492
1493}
1494
Ben Cahill77626352007-11-29 11:09:44 +08001495/*
1496 * Try to switch to new modulation mode from SISO
1497 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001498static int rs_move_siso_to_other(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001499 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001500 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001501 struct ieee80211_sta *sta, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001502{
Tomas Winklerc33104f2008-01-14 17:46:21 -08001503 u8 is_green = lq_sta->is_green;
Tomas Winklere227cea2008-07-18 13:53:05 +08001504 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1505 struct iwl_scale_tbl_info *search_tbl =
1506 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1507 struct iwl_rate_scale_data *window = &(tbl->win[index]);
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001508 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Tomas Winklere227cea2008-07-18 13:53:05 +08001509 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1510 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Wey-Yi Guyb1a78852010-07-31 08:52:03 -07001511 u8 start_action;
Guy Cohenfde0db32008-04-21 15:42:01 -07001512 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
Guy Cohen3110bef2008-09-09 10:54:54 +08001513 u8 tx_chains_num = priv->hw_params.tx_chains_num;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001514 u8 update_search_tbl_counter = 0;
Guy Cohenfde0db32008-04-21 15:42:01 -07001515 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001516
Johannes Berg290f5992010-08-23 07:56:58 -07001517 switch (priv->bt_traffic_load) {
1518 case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
1519 /* nothing */
1520 break;
1521 case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
1522 /* avoid antenna B unless MIMO */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001523 valid_tx_ant = first_antenna(priv->hw_params.valid_tx_ant);
Johannes Berg290f5992010-08-23 07:56:58 -07001524 if (tbl->action == IWL_SISO_SWITCH_ANTENNA2)
1525 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1526 break;
1527 case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
1528 case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
1529 /* avoid antenna B and MIMO */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001530 valid_tx_ant = first_antenna(priv->hw_params.valid_tx_ant);
1531 if (tbl->action != IWL_SISO_SWITCH_ANTENNA1)
Johannes Berg290f5992010-08-23 07:56:58 -07001532 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1533 break;
1534 default:
1535 IWL_ERR(priv, "Invalid BT load %d", priv->bt_traffic_load);
1536 break;
1537 }
1538
Johannes Berg3ad3b922009-08-07 15:41:48 -07001539 if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001540 tbl->action > IWL_SISO_SWITCH_ANTENNA2) {
1541 /* stay in SISO */
1542 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1543 }
Wey-Yi Guybee008b2010-08-23 07:57:04 -07001544
1545 /* configure as 1x1 if bt full concurrency */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001546 if (priv->bt_full_concurrent) {
1547 valid_tx_ant = first_antenna(priv->hw_params.valid_tx_ant);
1548 if (tbl->action >= IWL_LEGACY_SWITCH_ANTENNA2)
1549 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1550 }
Wey-Yi Guybee008b2010-08-23 07:57:04 -07001551
Wey-Yi Guyb1a78852010-07-31 08:52:03 -07001552 start_action = tbl->action;
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_SISO_SWITCH_ANTENNA1:
1557 case IWL_SISO_SWITCH_ANTENNA2:
Tomas Winklere1623442009-01-27 14:27:56 -08001558 IWL_DEBUG_RATE(priv, "LQ: SISO toggle Antenna\n");
Guy Cohen3110bef2008-09-09 10:54:54 +08001559 if ((tbl->action == IWL_SISO_SWITCH_ANTENNA1 &&
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001560 tx_chains_num <= 1) ||
Guy Cohen3110bef2008-09-09 10:54:54 +08001561 (tbl->action == IWL_SISO_SWITCH_ANTENNA2 &&
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001562 tx_chains_num <= 2))
Guy Cohen3110bef2008-09-09 10:54:54 +08001563 break;
1564
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001565 if (window->success_ratio >= IWL_RS_GOOD_RATIO &&
1566 !priv->bt_full_concurrent &&
1567 priv->bt_traffic_load ==
1568 IWL_BT_COEX_TRAFFIC_LOAD_NONE)
Zhu Yib481de92007-09-25 17:54:57 -07001569 break;
Zhu Yib481de92007-09-25 17:54:57 -07001570
1571 memcpy(search_tbl, tbl, sz);
Guy Cohenaade00c2008-04-23 17:14:59 -07001572 if (rs_toggle_antenna(valid_tx_ant,
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001573 &search_tbl->current_rate, search_tbl)) {
1574 update_search_tbl_counter = 1;
Guy Cohenaade00c2008-04-23 17:14:59 -07001575 goto out;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001576 }
Guy Cohena5e8b502008-05-29 16:35:11 +08001577 break;
Guy Cohen3110bef2008-09-09 10:54:54 +08001578 case IWL_SISO_SWITCH_MIMO2_AB:
1579 case IWL_SISO_SWITCH_MIMO2_AC:
1580 case IWL_SISO_SWITCH_MIMO2_BC:
Tomas Winklere1623442009-01-27 14:27:56 -08001581 IWL_DEBUG_RATE(priv, "LQ: SISO switch to MIMO2\n");
Zhu Yib481de92007-09-25 17:54:57 -07001582 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001583 search_tbl->is_SGI = 0;
Guy Cohen3110bef2008-09-09 10:54:54 +08001584
1585 if (tbl->action == IWL_SISO_SWITCH_MIMO2_AB)
1586 search_tbl->ant_type = ANT_AB;
1587 else if (tbl->action == IWL_SISO_SWITCH_MIMO2_AC)
1588 search_tbl->ant_type = ANT_AC;
1589 else
1590 search_tbl->ant_type = ANT_BC;
1591
1592 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1593 break;
1594
Guy Cohenfde0db32008-04-21 15:42:01 -07001595 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001596 search_tbl, index);
Guy Cohen3110bef2008-09-09 10:54:54 +08001597 if (!ret)
Zhu Yib481de92007-09-25 17:54:57 -07001598 goto out;
1599 break;
1600 case IWL_SISO_SWITCH_GI:
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001601 if (!tbl->is_ht40 && !(ht_cap->cap &
1602 IEEE80211_HT_CAP_SGI_20))
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001603 break;
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001604 if (tbl->is_ht40 && !(ht_cap->cap &
1605 IEEE80211_HT_CAP_SGI_40))
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001606 break;
1607
Tomas Winklere1623442009-01-27 14:27:56 -08001608 IWL_DEBUG_RATE(priv, "LQ: SISO toggle SGI/NGI\n");
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001609
Zhu Yib481de92007-09-25 17:54:57 -07001610 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001611 if (is_green) {
1612 if (!tbl->is_SGI)
1613 break;
1614 else
Winkler, Tomas15b16872008-12-19 10:37:33 +08001615 IWL_ERR(priv,
1616 "SGI was set in GF+SISO\n");
Zhu Yib481de92007-09-25 17:54:57 -07001617 }
Guy Cohen39e88502008-04-23 17:14:57 -07001618 search_tbl->is_SGI = !tbl->is_SGI;
Guy Coheneecd6e52008-04-23 17:14:58 -07001619 rs_set_expected_tpt_table(lq_sta, search_tbl);
Guy Cohen39e88502008-04-23 17:14:57 -07001620 if (tbl->is_SGI) {
1621 s32 tpt = lq_sta->last_tpt / 100;
1622 if (tpt >= search_tbl->expected_tpt[index])
1623 break;
1624 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001625 search_tbl->current_rate =
1626 rate_n_flags_from_tbl(priv, search_tbl,
1627 index, is_green);
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001628 update_search_tbl_counter = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001629 goto out;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001630 case IWL_SISO_SWITCH_MIMO3_ABC:
1631 IWL_DEBUG_RATE(priv, "LQ: SISO switch to MIMO3\n");
1632 memcpy(search_tbl, tbl, sz);
1633 search_tbl->is_SGI = 0;
1634 search_tbl->ant_type = ANT_ABC;
1635
1636 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1637 break;
1638
1639 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1640 search_tbl, index);
1641 if (!ret)
1642 goto out;
1643 break;
Zhu Yib481de92007-09-25 17:54:57 -07001644 }
1645 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001646 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001647 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
Zhu Yib481de92007-09-25 17:54:57 -07001648
1649 if (tbl->action == start_action)
1650 break;
1651 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001652 search_tbl->lq_type = LQ_NONE;
Zhu Yib481de92007-09-25 17:54:57 -07001653 return 0;
1654
1655 out:
Guy Cohen3110bef2008-09-09 10:54:54 +08001656 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001657 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001658 if (tbl->action > IWL_SISO_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001659 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001660 if (update_search_tbl_counter)
1661 search_tbl->action = tbl->action;
1662
Zhu Yib481de92007-09-25 17:54:57 -07001663 return 0;
1664}
1665
Ben Cahill77626352007-11-29 11:09:44 +08001666/*
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001667 * Try to switch to new modulation mode from MIMO2
Ben Cahill77626352007-11-29 11:09:44 +08001668 */
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001669static int rs_move_mimo2_to_other(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001670 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001671 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001672 struct ieee80211_sta *sta, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001673{
Tomas Winklerc33104f2008-01-14 17:46:21 -08001674 s8 is_green = lq_sta->is_green;
Tomas Winklere227cea2008-07-18 13:53:05 +08001675 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)]);
Guy Cohen3110bef2008-09-09 10:54:54 +08001678 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;
Tomas Winklere227cea2008-07-18 13:53:05 +08001680 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1681 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Wey-Yi Guyb1a78852010-07-31 08:52:03 -07001682 u8 start_action;
Guy Cohen3110bef2008-09-09 10:54:54 +08001683 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1684 u8 tx_chains_num = priv->hw_params.tx_chains_num;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001685 u8 update_search_tbl_counter = 0;
Guy Cohenaade00c2008-04-23 17:14:59 -07001686 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001687
Johannes Berg290f5992010-08-23 07:56:58 -07001688 switch (priv->bt_traffic_load) {
1689 case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
1690 /* nothing */
1691 break;
1692 case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
1693 case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
1694 /* avoid antenna B and MIMO */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001695 if (tbl->action != IWL_MIMO2_SWITCH_SISO_A)
1696 tbl->action = IWL_MIMO2_SWITCH_SISO_A;
1697 break;
Johannes Berg290f5992010-08-23 07:56:58 -07001698 case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
1699 /* avoid antenna B unless MIMO */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001700 if (tbl->action == IWL_MIMO2_SWITCH_SISO_B ||
1701 tbl->action == IWL_MIMO2_SWITCH_SISO_C)
Johannes Berg290f5992010-08-23 07:56:58 -07001702 tbl->action = IWL_MIMO2_SWITCH_SISO_A;
1703 break;
1704 default:
1705 IWL_ERR(priv, "Invalid BT load %d", priv->bt_traffic_load);
1706 break;
1707 }
1708
Johannes Berg3ad3b922009-08-07 15:41:48 -07001709 if ((iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE) &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001710 (tbl->action < IWL_MIMO2_SWITCH_SISO_A ||
1711 tbl->action > IWL_MIMO2_SWITCH_SISO_C)) {
1712 /* switch in SISO */
1713 tbl->action = IWL_MIMO2_SWITCH_SISO_A;
1714 }
Wey-Yi Guybee008b2010-08-23 07:57:04 -07001715
1716 /* configure as 1x1 if bt full concurrency */
1717 if (priv->bt_full_concurrent &&
1718 (tbl->action < IWL_MIMO2_SWITCH_SISO_A ||
1719 tbl->action > IWL_MIMO2_SWITCH_SISO_C))
1720 tbl->action = IWL_MIMO2_SWITCH_SISO_A;
1721
Wey-Yi Guyb1a78852010-07-31 08:52:03 -07001722 start_action = tbl->action;
Zhu Yib481de92007-09-25 17:54:57 -07001723 for (;;) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001724 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001725 switch (tbl->action) {
Guy Cohen3110bef2008-09-09 10:54:54 +08001726 case IWL_MIMO2_SWITCH_ANTENNA1:
1727 case IWL_MIMO2_SWITCH_ANTENNA2:
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001728 IWL_DEBUG_RATE(priv, "LQ: MIMO2 toggle Antennas\n");
Guy Cohen3110bef2008-09-09 10:54:54 +08001729
1730 if (tx_chains_num <= 2)
1731 break;
1732
1733 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1734 break;
1735
1736 memcpy(search_tbl, tbl, sz);
1737 if (rs_toggle_antenna(valid_tx_ant,
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001738 &search_tbl->current_rate, search_tbl)) {
1739 update_search_tbl_counter = 1;
Guy Cohen3110bef2008-09-09 10:54:54 +08001740 goto out;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001741 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001742 break;
1743 case IWL_MIMO2_SWITCH_SISO_A:
1744 case IWL_MIMO2_SWITCH_SISO_B:
1745 case IWL_MIMO2_SWITCH_SISO_C:
Tomas Winklere1623442009-01-27 14:27:56 -08001746 IWL_DEBUG_RATE(priv, "LQ: MIMO2 switch to SISO\n");
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001747
Ben Cahill77626352007-11-29 11:09:44 +08001748 /* Set up new search table for SISO */
Zhu Yib481de92007-09-25 17:54:57 -07001749 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001750
Guy Cohen3110bef2008-09-09 10:54:54 +08001751 if (tbl->action == IWL_MIMO2_SWITCH_SISO_A)
Guy Cohenfde0db32008-04-21 15:42:01 -07001752 search_tbl->ant_type = ANT_A;
Guy Cohen3110bef2008-09-09 10:54:54 +08001753 else if (tbl->action == IWL_MIMO2_SWITCH_SISO_B)
Guy Cohenfde0db32008-04-21 15:42:01 -07001754 search_tbl->ant_type = ANT_B;
Guy Cohen3110bef2008-09-09 10:54:54 +08001755 else
1756 search_tbl->ant_type = ANT_C;
1757
1758 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1759 break;
Zhu Yib481de92007-09-25 17:54:57 -07001760
Tomas Winklerc33104f2008-01-14 17:46:21 -08001761 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001762 search_tbl, index);
Guy Cohen3110bef2008-09-09 10:54:54 +08001763 if (!ret)
Zhu Yib481de92007-09-25 17:54:57 -07001764 goto out;
Guy Cohen3110bef2008-09-09 10:54:54 +08001765
Zhu Yib481de92007-09-25 17:54:57 -07001766 break;
1767
Guy Cohen3110bef2008-09-09 10:54:54 +08001768 case IWL_MIMO2_SWITCH_GI:
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001769 if (!tbl->is_ht40 && !(ht_cap->cap &
1770 IEEE80211_HT_CAP_SGI_20))
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001771 break;
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001772 if (tbl->is_ht40 && !(ht_cap->cap &
1773 IEEE80211_HT_CAP_SGI_40))
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001774 break;
1775
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001776 IWL_DEBUG_RATE(priv, "LQ: MIMO2 toggle SGI/NGI\n");
1777
1778 /* Set up new search table for MIMO2 */
1779 memcpy(search_tbl, tbl, sz);
1780 search_tbl->is_SGI = !tbl->is_SGI;
1781 rs_set_expected_tpt_table(lq_sta, search_tbl);
1782 /*
1783 * If active table already uses the fastest possible
1784 * modulation (dual stream with short guard interval),
1785 * and it's working well, there's no need to look
1786 * for a better type of modulation!
1787 */
1788 if (tbl->is_SGI) {
1789 s32 tpt = lq_sta->last_tpt / 100;
1790 if (tpt >= search_tbl->expected_tpt[index])
1791 break;
1792 }
1793 search_tbl->current_rate =
1794 rate_n_flags_from_tbl(priv, search_tbl,
1795 index, is_green);
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001796 update_search_tbl_counter = 1;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001797 goto out;
1798
1799 case IWL_MIMO2_SWITCH_MIMO3_ABC:
1800 IWL_DEBUG_RATE(priv, "LQ: MIMO2 switch to MIMO3\n");
1801 memcpy(search_tbl, tbl, sz);
1802 search_tbl->is_SGI = 0;
1803 search_tbl->ant_type = ANT_ABC;
1804
1805 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1806 break;
1807
1808 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1809 search_tbl, index);
1810 if (!ret)
1811 goto out;
1812
1813 break;
1814 }
1815 tbl->action++;
1816 if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
1817 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
1818
1819 if (tbl->action == start_action)
1820 break;
1821 }
1822 search_tbl->lq_type = LQ_NONE;
1823 return 0;
1824 out:
1825 lq_sta->search_better_tbl = 1;
1826 tbl->action++;
1827 if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
1828 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001829 if (update_search_tbl_counter)
1830 search_tbl->action = tbl->action;
1831
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001832 return 0;
1833
1834}
1835
1836/*
1837 * Try to switch to new modulation mode from MIMO3
1838 */
1839static int rs_move_mimo3_to_other(struct iwl_priv *priv,
1840 struct iwl_lq_sta *lq_sta,
1841 struct ieee80211_conf *conf,
1842 struct ieee80211_sta *sta, int index)
1843{
1844 s8 is_green = lq_sta->is_green;
1845 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1846 struct iwl_scale_tbl_info *search_tbl =
1847 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1848 struct iwl_rate_scale_data *window = &(tbl->win[index]);
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001849 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001850 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1851 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Wey-Yi Guyb1a78852010-07-31 08:52:03 -07001852 u8 start_action;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001853 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1854 u8 tx_chains_num = priv->hw_params.tx_chains_num;
1855 int ret;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001856 u8 update_search_tbl_counter = 0;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001857
Johannes Berg290f5992010-08-23 07:56:58 -07001858 switch (priv->bt_traffic_load) {
1859 case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
1860 /* nothing */
1861 break;
1862 case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
1863 case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
1864 /* avoid antenna B and MIMO */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001865 if (tbl->action != IWL_MIMO3_SWITCH_SISO_A)
Johannes Berg290f5992010-08-23 07:56:58 -07001866 tbl->action = IWL_MIMO3_SWITCH_SISO_A;
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001867 break;
Johannes Berg290f5992010-08-23 07:56:58 -07001868 case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
1869 /* avoid antenna B unless MIMO */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001870 if (tbl->action == IWL_MIMO3_SWITCH_SISO_B ||
1871 tbl->action == IWL_MIMO3_SWITCH_SISO_C)
Johannes Berg290f5992010-08-23 07:56:58 -07001872 tbl->action = IWL_MIMO3_SWITCH_SISO_A;
Johannes Berg290f5992010-08-23 07:56:58 -07001873 break;
1874 default:
1875 IWL_ERR(priv, "Invalid BT load %d", priv->bt_traffic_load);
1876 break;
1877 }
1878
Johannes Berg3ad3b922009-08-07 15:41:48 -07001879 if ((iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE) &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001880 (tbl->action < IWL_MIMO3_SWITCH_SISO_A ||
1881 tbl->action > IWL_MIMO3_SWITCH_SISO_C)) {
1882 /* switch in SISO */
1883 tbl->action = IWL_MIMO3_SWITCH_SISO_A;
1884 }
Wey-Yi Guybee008b2010-08-23 07:57:04 -07001885
1886 /* configure as 1x1 if bt full concurrency */
1887 if (priv->bt_full_concurrent &&
1888 (tbl->action < IWL_MIMO3_SWITCH_SISO_A ||
1889 tbl->action > IWL_MIMO3_SWITCH_SISO_C))
1890 tbl->action = IWL_MIMO3_SWITCH_SISO_A;
1891
Wey-Yi Guyb1a78852010-07-31 08:52:03 -07001892 start_action = tbl->action;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001893 for (;;) {
1894 lq_sta->action_counter++;
1895 switch (tbl->action) {
1896 case IWL_MIMO3_SWITCH_ANTENNA1:
1897 case IWL_MIMO3_SWITCH_ANTENNA2:
1898 IWL_DEBUG_RATE(priv, "LQ: MIMO3 toggle Antennas\n");
1899
1900 if (tx_chains_num <= 3)
1901 break;
1902
1903 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1904 break;
1905
1906 memcpy(search_tbl, tbl, sz);
1907 if (rs_toggle_antenna(valid_tx_ant,
1908 &search_tbl->current_rate, search_tbl))
1909 goto out;
1910 break;
1911 case IWL_MIMO3_SWITCH_SISO_A:
1912 case IWL_MIMO3_SWITCH_SISO_B:
1913 case IWL_MIMO3_SWITCH_SISO_C:
1914 IWL_DEBUG_RATE(priv, "LQ: MIMO3 switch to SISO\n");
1915
1916 /* Set up new search table for SISO */
1917 memcpy(search_tbl, tbl, sz);
1918
1919 if (tbl->action == IWL_MIMO3_SWITCH_SISO_A)
1920 search_tbl->ant_type = ANT_A;
1921 else if (tbl->action == IWL_MIMO3_SWITCH_SISO_B)
1922 search_tbl->ant_type = ANT_B;
1923 else
1924 search_tbl->ant_type = ANT_C;
1925
1926 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1927 break;
1928
1929 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
1930 search_tbl, index);
1931 if (!ret)
1932 goto out;
1933
1934 break;
1935
1936 case IWL_MIMO3_SWITCH_MIMO2_AB:
1937 case IWL_MIMO3_SWITCH_MIMO2_AC:
1938 case IWL_MIMO3_SWITCH_MIMO2_BC:
1939 IWL_DEBUG_RATE(priv, "LQ: MIMO3 switch to MIMO2\n");
1940
1941 memcpy(search_tbl, tbl, sz);
1942 search_tbl->is_SGI = 0;
1943 if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AB)
1944 search_tbl->ant_type = ANT_AB;
1945 else if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AC)
1946 search_tbl->ant_type = ANT_AC;
1947 else
1948 search_tbl->ant_type = ANT_BC;
1949
1950 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1951 break;
1952
1953 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
1954 search_tbl, index);
1955 if (!ret)
1956 goto out;
1957
1958 break;
1959
1960 case IWL_MIMO3_SWITCH_GI:
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001961 if (!tbl->is_ht40 && !(ht_cap->cap &
1962 IEEE80211_HT_CAP_SGI_20))
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001963 break;
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001964 if (tbl->is_ht40 && !(ht_cap->cap &
1965 IEEE80211_HT_CAP_SGI_40))
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001966 break;
1967
1968 IWL_DEBUG_RATE(priv, "LQ: MIMO3 toggle SGI/NGI\n");
Ben Cahill77626352007-11-29 11:09:44 +08001969
1970 /* Set up new search table for MIMO */
Zhu Yib481de92007-09-25 17:54:57 -07001971 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001972 search_tbl->is_SGI = !tbl->is_SGI;
Guy Coheneecd6e52008-04-23 17:14:58 -07001973 rs_set_expected_tpt_table(lq_sta, search_tbl);
Ben Cahill77626352007-11-29 11:09:44 +08001974 /*
1975 * If active table already uses the fastest possible
1976 * modulation (dual stream with short guard interval),
1977 * and it's working well, there's no need to look
1978 * for a better type of modulation!
1979 */
Guy Cohen39e88502008-04-23 17:14:57 -07001980 if (tbl->is_SGI) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001981 s32 tpt = lq_sta->last_tpt / 100;
Guy Cohen39e88502008-04-23 17:14:57 -07001982 if (tpt >= search_tbl->expected_tpt[index])
1983 break;
Zhu Yib481de92007-09-25 17:54:57 -07001984 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001985 search_tbl->current_rate =
1986 rate_n_flags_from_tbl(priv, search_tbl,
1987 index, is_green);
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001988 update_search_tbl_counter = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001989 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07001990 }
1991 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001992 if (tbl->action > IWL_MIMO3_SWITCH_GI)
1993 tbl->action = IWL_MIMO3_SWITCH_ANTENNA1;
Zhu Yib481de92007-09-25 17:54:57 -07001994
1995 if (tbl->action == start_action)
1996 break;
1997 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001998 search_tbl->lq_type = LQ_NONE;
Zhu Yib481de92007-09-25 17:54:57 -07001999 return 0;
2000 out:
Guy Cohen3110bef2008-09-09 10:54:54 +08002001 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07002002 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07002003 if (tbl->action > IWL_MIMO3_SWITCH_GI)
2004 tbl->action = IWL_MIMO3_SWITCH_ANTENNA1;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07002005 if (update_search_tbl_counter)
2006 search_tbl->action = tbl->action;
2007
Zhu Yib481de92007-09-25 17:54:57 -07002008 return 0;
2009
2010}
2011
Ben Cahill77626352007-11-29 11:09:44 +08002012/*
2013 * Check whether we should continue using same modulation mode, or
2014 * begin search for a new mode, based on:
2015 * 1) # tx successes or failures while using this mode
2016 * 2) # times calling this function
2017 * 3) elapsed time in this mode (not used, for now)
2018 */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07002019static void rs_stay_in_table(struct iwl_lq_sta *lq_sta, bool force_search)
Zhu Yib481de92007-09-25 17:54:57 -07002020{
Tomas Winklere227cea2008-07-18 13:53:05 +08002021 struct iwl_scale_tbl_info *tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002022 int i;
2023 int active_tbl;
2024 int flush_interval_passed = 0;
Ester Kummerbf403db2008-05-05 10:22:40 +08002025 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07002026
Ester Kummerbf403db2008-05-05 10:22:40 +08002027 priv = lq_sta->drv;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002028 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002029
Tomas Winklerc33104f2008-01-14 17:46:21 -08002030 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07002031
Ben Cahill77626352007-11-29 11:09:44 +08002032 /* If we've been disallowing search, see if we should now allow it */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002033 if (lq_sta->stay_in_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07002034
Ben Cahill77626352007-11-29 11:09:44 +08002035 /* Elapsed time using current modulation mode */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002036 if (lq_sta->flush_timer)
Zhu Yib481de92007-09-25 17:54:57 -07002037 flush_interval_passed =
Mohamed Abbas447fee72009-04-20 14:37:02 -07002038 time_after(jiffies,
2039 (unsigned long)(lq_sta->flush_timer +
Zhu Yib481de92007-09-25 17:54:57 -07002040 IWL_RATE_SCALE_FLUSH_INTVL));
2041
Ben Cahill77626352007-11-29 11:09:44 +08002042 /*
2043 * Check if we should allow search for new modulation mode.
2044 * If many frames have failed or succeeded, or we've used
2045 * this same modulation for a long time, allow search, and
2046 * reset history stats that keep track of whether we should
2047 * allow a new search. Also (below) reset all bitmaps and
2048 * stats in active history.
2049 */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07002050 if (force_search ||
2051 (lq_sta->total_failed > lq_sta->max_failure_limit) ||
Tomas Winklerc33104f2008-01-14 17:46:21 -08002052 (lq_sta->total_success > lq_sta->max_success_limit) ||
2053 ((!lq_sta->search_better_tbl) && (lq_sta->flush_timer)
Zhu Yib481de92007-09-25 17:54:57 -07002054 && (flush_interval_passed))) {
Tomas Winklere1623442009-01-27 14:27:56 -08002055 IWL_DEBUG_RATE(priv, "LQ: stay is expired %d %d %d\n:",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002056 lq_sta->total_failed,
2057 lq_sta->total_success,
Zhu Yib481de92007-09-25 17:54:57 -07002058 flush_interval_passed);
Ben Cahill77626352007-11-29 11:09:44 +08002059
2060 /* Allow search for new mode */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002061 lq_sta->stay_in_tbl = 0; /* only place reset */
2062 lq_sta->total_failed = 0;
2063 lq_sta->total_success = 0;
2064 lq_sta->flush_timer = 0;
Ben Cahill77626352007-11-29 11:09:44 +08002065
2066 /*
2067 * Else if we've used this modulation mode enough repetitions
2068 * (regardless of elapsed time or success/failure), reset
2069 * history bitmaps and rate-specific stats for all rates in
2070 * active table.
2071 */
Mohamed Abbas403ab562007-11-06 22:06:25 -08002072 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002073 lq_sta->table_count++;
2074 if (lq_sta->table_count >=
2075 lq_sta->table_count_limit) {
2076 lq_sta->table_count = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002077
Tomas Winklere1623442009-01-27 14:27:56 -08002078 IWL_DEBUG_RATE(priv, "LQ: stay in table clear win\n");
Zhu Yib481de92007-09-25 17:54:57 -07002079 for (i = 0; i < IWL_RATE_COUNT; i++)
2080 rs_rate_scale_clear_window(
2081 &(tbl->win[i]));
2082 }
2083 }
2084
Ben Cahill77626352007-11-29 11:09:44 +08002085 /* If transitioning to allow "search", reset all history
2086 * bitmaps and stats in active table (this will become the new
2087 * "search" table). */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002088 if (!lq_sta->stay_in_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07002089 for (i = 0; i < IWL_RATE_COUNT; i++)
2090 rs_rate_scale_clear_window(&(tbl->win[i]));
2091 }
2092 }
2093}
2094
Ben Cahill77626352007-11-29 11:09:44 +08002095/*
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07002096 * setup rate table in uCode
2097 * return rate_n_flags as used in the table
2098 */
2099static u32 rs_update_rate_tbl(struct iwl_priv *priv,
Johannes Berg7e6a5882010-08-23 10:46:46 +02002100 struct iwl_rxon_context *ctx,
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07002101 struct iwl_lq_sta *lq_sta,
2102 struct iwl_scale_tbl_info *tbl,
2103 int index, u8 is_green)
2104{
2105 u32 rate;
2106
2107 /* Update uCode's rate table. */
2108 rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
2109 rs_fill_link_cmd(priv, lq_sta, rate);
Johannes Berg7e6a5882010-08-23 10:46:46 +02002110 iwl_send_lq_cmd(priv, ctx, &lq_sta->lq, CMD_ASYNC, false);
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07002111
2112 return rate;
2113}
2114
2115/*
Ben Cahill77626352007-11-29 11:09:44 +08002116 * Do rate scaling and search for new modulation mode.
2117 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002118static void rs_rate_scale_perform(struct iwl_priv *priv,
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002119 struct sk_buff *skb,
Johannes Berg4b7679a2008-09-18 18:14:18 +02002120 struct ieee80211_sta *sta,
2121 struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002122{
Johannes Berg4b7679a2008-09-18 18:14:18 +02002123 struct ieee80211_hw *hw = priv->hw;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002124 struct ieee80211_conf *conf = &hw->conf;
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002125 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2126 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002127 int low = IWL_RATE_INVALID;
2128 int high = IWL_RATE_INVALID;
2129 int index;
2130 int i;
Tomas Winklere227cea2008-07-18 13:53:05 +08002131 struct iwl_rate_scale_data *window = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002132 int current_tpt = IWL_INVALID_VALUE;
2133 int low_tpt = IWL_INVALID_VALUE;
2134 int high_tpt = IWL_INVALID_VALUE;
2135 u32 fail_count;
2136 s8 scale_action = 0;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002137 u16 rate_mask;
Zhu Yib481de92007-09-25 17:54:57 -07002138 u8 update_lq = 0;
Tomas Winklere227cea2008-07-18 13:53:05 +08002139 struct iwl_scale_tbl_info *tbl, *tbl1;
Zhu Yib481de92007-09-25 17:54:57 -07002140 u16 rate_scale_index_msk = 0;
Guy Cohen39e88502008-04-23 17:14:57 -07002141 u32 rate;
Zhu Yib481de92007-09-25 17:54:57 -07002142 u8 is_green = 0;
2143 u8 active_tbl = 0;
2144 u8 done_search = 0;
2145 u16 high_low;
Guy Cohena5e8b502008-05-29 16:35:11 +08002146 s32 sr;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002147 u8 tid = MAX_TID_COUNT;
Wey-Yi Guy86b47662009-03-10 14:35:09 -07002148 struct iwl_tid_data *tid_data;
Johannes Berg7e6a5882010-08-23 10:46:46 +02002149 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
2150 struct iwl_rxon_context *ctx = sta_priv->common.ctx;
Zhu Yib481de92007-09-25 17:54:57 -07002151
Tomas Winklere1623442009-01-27 14:27:56 -08002152 IWL_DEBUG_RATE(priv, "rate scale calculate new rate for skb\n");
Zhu Yib481de92007-09-25 17:54:57 -07002153
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002154 /* Send management frames and NO_ACK data using lowest rate. */
Tomas Winkler417f1142008-11-12 13:14:06 -08002155 /* TODO: this could probably be improved.. */
2156 if (!ieee80211_is_data(hdr->frame_control) ||
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002157 info->flags & IEEE80211_TX_CTL_NO_ACK)
Zhu Yib481de92007-09-25 17:54:57 -07002158 return;
Zhu Yib481de92007-09-25 17:54:57 -07002159
Johannes Berg4b7679a2008-09-18 18:14:18 +02002160 if (!sta || !lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002161 return;
2162
Johannes Berg4b7679a2008-09-18 18:14:18 +02002163 lq_sta->supp_rates = sta->supp_rates[lq_sta->band];
Zhu Yib481de92007-09-25 17:54:57 -07002164
Ron Rindjunskyfaa29712008-06-12 09:46:58 +08002165 tid = rs_tl_add_packet(lq_sta, hdr);
Daniel C Halperine3949d62009-09-17 10:43:50 -07002166 if ((tid != MAX_TID_COUNT) && (lq_sta->tx_agg_tid_en & (1 << tid))) {
2167 tid_data = &priv->stations[lq_sta->lq.sta_id].tid[tid];
2168 if (tid_data->agg.state == IWL_AGG_OFF)
2169 lq_sta->is_agg = 0;
2170 else
2171 lq_sta->is_agg = 1;
2172 } else
2173 lq_sta->is_agg = 0;
Ron Rindjunskyfaa29712008-06-12 09:46:58 +08002174
Ben Cahill77626352007-11-29 11:09:44 +08002175 /*
2176 * Select rate-scale / modulation-mode table to work with in
2177 * the rest of this function: "search" if searching for better
2178 * modulation mode, or "active" if doing rate scaling within a mode.
2179 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002180 if (!lq_sta->search_better_tbl)
2181 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002182 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002183 active_tbl = 1 - lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002184
Tomas Winklerc33104f2008-01-14 17:46:21 -08002185 tbl = &(lq_sta->lq_info[active_tbl]);
Wey-Yi Guy2681b202009-05-21 13:44:22 -07002186 if (is_legacy(tbl->lq_type))
2187 lq_sta->is_green = 0;
2188 else
Johannes Berg7e6a5882010-08-23 10:46:46 +02002189 lq_sta->is_green = rs_use_green(sta);
Tomas Winklerc33104f2008-01-14 17:46:21 -08002190 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07002191
Ben Cahill77626352007-11-29 11:09:44 +08002192 /* current tx rate */
Johannes Bergb7e35002008-09-11 02:22:58 +02002193 index = lq_sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002194
Tomas Winklere1623442009-01-27 14:27:56 -08002195 IWL_DEBUG_RATE(priv, "Rate scale index %d for type %d\n", index,
Zhu Yib481de92007-09-25 17:54:57 -07002196 tbl->lq_type);
2197
Ben Cahill77626352007-11-29 11:09:44 +08002198 /* rates available for this association, and for modulation mode */
Guy Coheneecd6e52008-04-23 17:14:58 -07002199 rate_mask = rs_get_supported_rates(lq_sta, hdr, tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -07002200
Frans Pop91dd6c22010-03-24 14:19:58 -07002201 IWL_DEBUG_RATE(priv, "mask 0x%04X\n", rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07002202
2203 /* mask with station rate restriction */
2204 if (is_legacy(tbl->lq_type)) {
Johannes Berg8318d782008-01-24 19:38:38 +01002205 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Ben Cahill77626352007-11-29 11:09:44 +08002206 /* supp_rates has no CCK bits in A mode */
Zhu Yib481de92007-09-25 17:54:57 -07002207 rate_scale_index_msk = (u16) (rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -08002208 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
Zhu Yib481de92007-09-25 17:54:57 -07002209 else
2210 rate_scale_index_msk = (u16) (rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -08002211 lq_sta->supp_rates);
Zhu Yib481de92007-09-25 17:54:57 -07002212
2213 } else
2214 rate_scale_index_msk = rate_mask;
2215
2216 if (!rate_scale_index_msk)
2217 rate_scale_index_msk = rate_mask;
2218
Guy Cohen07bc28e2008-04-23 17:15:03 -07002219 if (!((1 << index) & rate_scale_index_msk)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002220 IWL_ERR(priv, "Current Rate is not valid\n");
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07002221 if (lq_sta->search_better_tbl) {
2222 /* revert to active table if search table is not valid*/
2223 tbl->lq_type = LQ_NONE;
2224 lq_sta->search_better_tbl = 0;
2225 tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
2226 /* get "active" rate info */
2227 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
Johannes Berg7e6a5882010-08-23 10:46:46 +02002228 rate = rs_update_rate_tbl(priv, ctx, lq_sta,
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07002229 tbl, index, is_green);
2230 }
Guy Cohen07bc28e2008-04-23 17:15:03 -07002231 return;
Zhu Yib481de92007-09-25 17:54:57 -07002232 }
2233
Ben Cahill77626352007-11-29 11:09:44 +08002234 /* Get expected throughput table and history window for current rate */
Guy Cohen07bc28e2008-04-23 17:15:03 -07002235 if (!tbl->expected_tpt) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002236 IWL_ERR(priv, "tbl->expected_tpt is NULL\n");
Guy Cohen07bc28e2008-04-23 17:15:03 -07002237 return;
2238 }
Zhu Yib481de92007-09-25 17:54:57 -07002239
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002240 /* force user max rate if set by user */
2241 if ((lq_sta->max_rate_idx != -1) &&
2242 (lq_sta->max_rate_idx < index)) {
2243 index = lq_sta->max_rate_idx;
2244 update_lq = 1;
2245 window = &(tbl->win[index]);
2246 goto lq_update;
2247 }
2248
Zhu Yib481de92007-09-25 17:54:57 -07002249 window = &(tbl->win[index]);
2250
Ben Cahill77626352007-11-29 11:09:44 +08002251 /*
2252 * If there is not enough history to calculate actual average
2253 * throughput, keep analyzing results of more tx frames, without
2254 * changing rate or mode (bypass most of the rest of this function).
2255 * Set up new rate table in uCode only if old rate is not supported
2256 * in current association (use new rate found above).
2257 */
Zhu Yib481de92007-09-25 17:54:57 -07002258 fail_count = window->counter - window->success_counter;
Guy Cohen07bc28e2008-04-23 17:15:03 -07002259 if ((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
2260 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002261 IWL_DEBUG_RATE(priv, "LQ: still below TH. succ=%d total=%d "
Zhu Yib481de92007-09-25 17:54:57 -07002262 "for index %d\n",
2263 window->success_counter, window->counter, index);
Ben Cahill77626352007-11-29 11:09:44 +08002264
2265 /* Can't calculate this yet; not enough history */
Zhu Yib481de92007-09-25 17:54:57 -07002266 window->average_tpt = IWL_INVALID_VALUE;
Ben Cahill77626352007-11-29 11:09:44 +08002267
2268 /* Should we stay with this modulation mode,
2269 * or search for a new one? */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07002270 rs_stay_in_table(lq_sta, false);
Ben Cahill77626352007-11-29 11:09:44 +08002271
Zhu Yib481de92007-09-25 17:54:57 -07002272 goto out;
Guy Cohen3110bef2008-09-09 10:54:54 +08002273 }
Ben Cahill77626352007-11-29 11:09:44 +08002274 /* Else we have enough samples; calculate estimate of
2275 * actual average throughput */
Reinette Chatre3d79b2a2010-05-03 10:55:07 -07002276 if (window->average_tpt != ((window->success_ratio *
2277 tbl->expected_tpt[index] + 64) / 128)) {
2278 IWL_ERR(priv, "expected_tpt should have been calculated by now\n");
2279 window->average_tpt = ((window->success_ratio *
2280 tbl->expected_tpt[index] + 64) / 128);
2281 }
Zhu Yib481de92007-09-25 17:54:57 -07002282
Ben Cahill77626352007-11-29 11:09:44 +08002283 /* If we are searching for better modulation mode, check success. */
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002284 if (lq_sta->search_better_tbl &&
Johannes Berg3ad3b922009-08-07 15:41:48 -07002285 (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_MULTI)) {
Ben Cahill77626352007-11-29 11:09:44 +08002286 /* If good success, continue using the "search" mode;
2287 * no need to send new link quality command, since we're
2288 * continuing to use the setup that we've been trying. */
Guy Cohen07bc28e2008-04-23 17:15:03 -07002289 if (window->average_tpt > lq_sta->last_tpt) {
2290
Tomas Winklere1623442009-01-27 14:27:56 -08002291 IWL_DEBUG_RATE(priv, "LQ: SWITCHING TO NEW TABLE "
Guy Cohen07bc28e2008-04-23 17:15:03 -07002292 "suc=%d cur-tpt=%d old-tpt=%d\n",
2293 window->success_ratio,
2294 window->average_tpt,
2295 lq_sta->last_tpt);
2296
2297 if (!is_legacy(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08002298 lq_sta->enable_counter = 1;
Guy Cohen07bc28e2008-04-23 17:15:03 -07002299
Ben Cahill77626352007-11-29 11:09:44 +08002300 /* Swap tables; "search" becomes "active" */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002301 lq_sta->active_tbl = active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002302 current_tpt = window->average_tpt;
Ben Cahill77626352007-11-29 11:09:44 +08002303
2304 /* Else poor success; go back to mode in "active" table */
Zhu Yib481de92007-09-25 17:54:57 -07002305 } else {
Guy Cohen07bc28e2008-04-23 17:15:03 -07002306
Tomas Winklere1623442009-01-27 14:27:56 -08002307 IWL_DEBUG_RATE(priv, "LQ: GOING BACK TO THE OLD TABLE "
Guy Cohen07bc28e2008-04-23 17:15:03 -07002308 "suc=%d cur-tpt=%d old-tpt=%d\n",
2309 window->success_ratio,
2310 window->average_tpt,
2311 lq_sta->last_tpt);
2312
Ben Cahill77626352007-11-29 11:09:44 +08002313 /* Nullify "search" table */
Zhu Yib481de92007-09-25 17:54:57 -07002314 tbl->lq_type = LQ_NONE;
Ben Cahill77626352007-11-29 11:09:44 +08002315
2316 /* Revert to "active" table */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002317 active_tbl = lq_sta->active_tbl;
2318 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07002319
Ben Cahill77626352007-11-29 11:09:44 +08002320 /* Revert to "active" rate and throughput info */
Tomas Winklere7d326ac2008-06-12 09:47:11 +08002321 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
Tomas Winklerc33104f2008-01-14 17:46:21 -08002322 current_tpt = lq_sta->last_tpt;
Ben Cahill77626352007-11-29 11:09:44 +08002323
2324 /* Need to set up a new rate table in uCode */
2325 update_lq = 1;
Zhu Yib481de92007-09-25 17:54:57 -07002326 }
Ben Cahill77626352007-11-29 11:09:44 +08002327
2328 /* Either way, we've made a decision; modulation mode
2329 * search is done, allow rate adjustment next time. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002330 lq_sta->search_better_tbl = 0;
Ben Cahill77626352007-11-29 11:09:44 +08002331 done_search = 1; /* Don't switch modes below! */
Zhu Yib481de92007-09-25 17:54:57 -07002332 goto lq_update;
2333 }
2334
Ben Cahill77626352007-11-29 11:09:44 +08002335 /* (Else) not in search of better modulation mode, try for better
2336 * starting rate, while staying in this mode. */
Ester Kummerbf403db2008-05-05 10:22:40 +08002337 high_low = rs_get_adjacent_rate(priv, index, rate_scale_index_msk,
Zhu Yib481de92007-09-25 17:54:57 -07002338 tbl->lq_type);
2339 low = high_low & 0xff;
2340 high = (high_low >> 8) & 0xff;
2341
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002342 /* If user set max rate, dont allow higher than user constrain */
2343 if ((lq_sta->max_rate_idx != -1) &&
2344 (lq_sta->max_rate_idx < high))
2345 high = IWL_RATE_INVALID;
2346
Guy Cohena5e8b502008-05-29 16:35:11 +08002347 sr = window->success_ratio;
2348
Ben Cahill77626352007-11-29 11:09:44 +08002349 /* Collect measured throughputs for current and adjacent rates */
Zhu Yib481de92007-09-25 17:54:57 -07002350 current_tpt = window->average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07002351 if (low != IWL_RATE_INVALID)
2352 low_tpt = tbl->win[low].average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07002353 if (high != IWL_RATE_INVALID)
2354 high_tpt = tbl->win[high].average_tpt;
2355
Guy Cohena5e8b502008-05-29 16:35:11 +08002356 scale_action = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002357
Ben Cahill77626352007-11-29 11:09:44 +08002358 /* Too many failures, decrease rate */
Guy Cohena5e8b502008-05-29 16:35:11 +08002359 if ((sr <= IWL_RATE_DECREASE_TH) || (current_tpt == 0)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002360 IWL_DEBUG_RATE(priv, "decrease rate because of low success_ratio\n");
Zhu Yib481de92007-09-25 17:54:57 -07002361 scale_action = -1;
Ben Cahill77626352007-11-29 11:09:44 +08002362
2363 /* No throughput measured yet for adjacent rates; try increase. */
Zhu Yib481de92007-09-25 17:54:57 -07002364 } else if ((low_tpt == IWL_INVALID_VALUE) &&
Guy Cohena5e8b502008-05-29 16:35:11 +08002365 (high_tpt == IWL_INVALID_VALUE)) {
2366
2367 if (high != IWL_RATE_INVALID && sr >= IWL_RATE_INCREASE_TH)
2368 scale_action = 1;
2369 else if (low != IWL_RATE_INVALID)
Wey-Yi Guy8fe72312009-03-10 14:35:10 -07002370 scale_action = 0;
Guy Cohena5e8b502008-05-29 16:35:11 +08002371 }
Ben Cahill77626352007-11-29 11:09:44 +08002372
2373 /* Both adjacent throughputs are measured, but neither one has better
2374 * throughput; we're using the best rate, don't change it! */
Zhu Yib481de92007-09-25 17:54:57 -07002375 else if ((low_tpt != IWL_INVALID_VALUE) &&
2376 (high_tpt != IWL_INVALID_VALUE) &&
2377 (low_tpt < current_tpt) &&
2378 (high_tpt < current_tpt))
2379 scale_action = 0;
Ben Cahill77626352007-11-29 11:09:44 +08002380
2381 /* At least one adjacent rate's throughput is measured,
2382 * and may have better performance. */
Zhu Yib481de92007-09-25 17:54:57 -07002383 else {
Ben Cahill77626352007-11-29 11:09:44 +08002384 /* Higher adjacent rate's throughput is measured */
Zhu Yib481de92007-09-25 17:54:57 -07002385 if (high_tpt != IWL_INVALID_VALUE) {
Ben Cahill77626352007-11-29 11:09:44 +08002386 /* Higher rate has better throughput */
Guy Cohena5e8b502008-05-29 16:35:11 +08002387 if (high_tpt > current_tpt &&
2388 sr >= IWL_RATE_INCREASE_TH) {
Zhu Yib481de92007-09-25 17:54:57 -07002389 scale_action = 1;
Guy Cohena5e8b502008-05-29 16:35:11 +08002390 } else {
Wey-Yi Guy8fe72312009-03-10 14:35:10 -07002391 scale_action = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002392 }
Ben Cahill77626352007-11-29 11:09:44 +08002393
2394 /* Lower adjacent rate's throughput is measured */
Zhu Yib481de92007-09-25 17:54:57 -07002395 } else if (low_tpt != IWL_INVALID_VALUE) {
Ben Cahill77626352007-11-29 11:09:44 +08002396 /* Lower rate has better throughput */
Zhu Yib481de92007-09-25 17:54:57 -07002397 if (low_tpt > current_tpt) {
Tomas Winklere1623442009-01-27 14:27:56 -08002398 IWL_DEBUG_RATE(priv,
2399 "decrease rate because of low tpt\n");
Zhu Yib481de92007-09-25 17:54:57 -07002400 scale_action = -1;
Guy Cohena5e8b502008-05-29 16:35:11 +08002401 } else if (sr >= IWL_RATE_INCREASE_TH) {
Zhu Yib481de92007-09-25 17:54:57 -07002402 scale_action = 1;
Guy Cohena5e8b502008-05-29 16:35:11 +08002403 }
Zhu Yib481de92007-09-25 17:54:57 -07002404 }
2405 }
2406
Ben Cahill77626352007-11-29 11:09:44 +08002407 /* Sanity check; asked for decrease, but success rate or throughput
2408 * has been good at old rate. Don't change it. */
Guy Cohena5e8b502008-05-29 16:35:11 +08002409 if ((scale_action == -1) && (low != IWL_RATE_INVALID) &&
2410 ((sr > IWL_RATE_HIGH_TH) ||
Zhu Yib481de92007-09-25 17:54:57 -07002411 (current_tpt > (100 * tbl->expected_tpt[low]))))
Zhu Yib481de92007-09-25 17:54:57 -07002412 scale_action = 0;
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002413 if (!iwl_ht_enabled(priv) && !is_legacy(tbl->lq_type))
2414 scale_action = -1;
Johannes Berg3ad3b922009-08-07 15:41:48 -07002415 if (iwl_tx_ant_restriction(priv) != IWL_ANT_OK_MULTI &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002416 (is_mimo2(tbl->lq_type) || is_mimo3(tbl->lq_type)))
2417 scale_action = -1;
Johannes Berg290f5992010-08-23 07:56:58 -07002418
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07002419 if ((priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH) &&
2420 (is_mimo2(tbl->lq_type) || is_mimo3(tbl->lq_type))) {
2421 if (lq_sta->last_bt_traffic > priv->bt_traffic_load) {
2422 /*
2423 * don't set scale_action, don't want to scale up if
2424 * the rate scale doesn't otherwise think that is a
2425 * good idea.
2426 */
2427 } else if (lq_sta->last_bt_traffic <= priv->bt_traffic_load) {
2428 scale_action = -1;
2429 }
2430 }
2431 lq_sta->last_bt_traffic = priv->bt_traffic_load;
2432
2433 if ((priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH) &&
2434 (is_mimo2(tbl->lq_type) || is_mimo3(tbl->lq_type))) {
2435 /* search for a new modulation */
2436 rs_stay_in_table(lq_sta, true);
2437 goto lq_update;
Johannes Berg290f5992010-08-23 07:56:58 -07002438 }
2439
Zhu Yib481de92007-09-25 17:54:57 -07002440 switch (scale_action) {
2441 case -1:
Ben Cahill77626352007-11-29 11:09:44 +08002442 /* Decrease starting rate, update uCode's rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002443 if (low != IWL_RATE_INVALID) {
2444 update_lq = 1;
2445 index = low;
2446 }
Mohamed Abbas447fee72009-04-20 14:37:02 -07002447
Zhu Yib481de92007-09-25 17:54:57 -07002448 break;
2449 case 1:
Ben Cahill77626352007-11-29 11:09:44 +08002450 /* Increase starting rate, update uCode's rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002451 if (high != IWL_RATE_INVALID) {
2452 update_lq = 1;
2453 index = high;
2454 }
2455
2456 break;
2457 case 0:
Ben Cahill77626352007-11-29 11:09:44 +08002458 /* No change */
Zhu Yib481de92007-09-25 17:54:57 -07002459 default:
2460 break;
2461 }
2462
Tomas Winklere1623442009-01-27 14:27:56 -08002463 IWL_DEBUG_RATE(priv, "choose rate scale index %d action %d low %d "
Zhu Yib481de92007-09-25 17:54:57 -07002464 "high %d type %d\n",
2465 index, scale_action, low, high, tbl->lq_type);
2466
Guy Cohena5e8b502008-05-29 16:35:11 +08002467lq_update:
Ben Cahill77626352007-11-29 11:09:44 +08002468 /* Replace uCode's rate table for the destination station. */
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07002469 if (update_lq)
Johannes Berg7e6a5882010-08-23 10:46:46 +02002470 rate = rs_update_rate_tbl(priv, ctx, lq_sta,
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07002471 tbl, index, is_green);
Ben Cahill77626352007-11-29 11:09:44 +08002472
Johannes Berg3ad3b922009-08-07 15:41:48 -07002473 if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_MULTI) {
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002474 /* Should we stay with this modulation mode,
2475 * or search for a new one? */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07002476 rs_stay_in_table(lq_sta, false);
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002477 }
Ben Cahill77626352007-11-29 11:09:44 +08002478 /*
2479 * Search for new modulation mode if we're:
2480 * 1) Not changing rates right now
2481 * 2) Not just finishing up a search
2482 * 3) Allowing a new search
2483 */
Guy Cohen47cfd462008-05-27 11:29:34 +08002484 if (!update_lq && !done_search && !lq_sta->stay_in_tbl && window->counter) {
Ben Cahill77626352007-11-29 11:09:44 +08002485 /* Save current throughput to compare with "search" throughput*/
Tomas Winklerc33104f2008-01-14 17:46:21 -08002486 lq_sta->last_tpt = current_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07002487
Ben Cahill77626352007-11-29 11:09:44 +08002488 /* Select a new "search" modulation mode to try.
2489 * If one is found, set up the new "search" table. */
Zhu Yib481de92007-09-25 17:54:57 -07002490 if (is_legacy(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08002491 rs_move_legacy_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07002492 else if (is_siso(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08002493 rs_move_siso_to_other(priv, lq_sta, conf, sta, index);
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07002494 else if (is_mimo2(tbl->lq_type))
2495 rs_move_mimo2_to_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07002496 else
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07002497 rs_move_mimo3_to_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07002498
Ben Cahill77626352007-11-29 11:09:44 +08002499 /* If new "search" mode was selected, set up in uCode table */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002500 if (lq_sta->search_better_tbl) {
Ben Cahill77626352007-11-29 11:09:44 +08002501 /* Access the "search" table, clear its history. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002502 tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Zhu Yib481de92007-09-25 17:54:57 -07002503 for (i = 0; i < IWL_RATE_COUNT; i++)
2504 rs_rate_scale_clear_window(&(tbl->win[i]));
2505
Ben Cahill77626352007-11-29 11:09:44 +08002506 /* Use new "search" start rate */
Tomas Winklere7d326ac2008-06-12 09:47:11 +08002507 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002508
Tomas Winklere1623442009-01-27 14:27:56 -08002509 IWL_DEBUG_RATE(priv, "Switch current mcs: %X index: %d\n",
Guy Cohen39e88502008-04-23 17:14:57 -07002510 tbl->current_rate, index);
Guy Cohen07bc28e2008-04-23 17:15:03 -07002511 rs_fill_link_cmd(priv, lq_sta, tbl->current_rate);
Johannes Berg7e6a5882010-08-23 10:46:46 +02002512 iwl_send_lq_cmd(priv, ctx, &lq_sta->lq, CMD_ASYNC, false);
Mohamed Abbas447fee72009-04-20 14:37:02 -07002513 } else
2514 done_search = 1;
2515 }
Zhu Yib481de92007-09-25 17:54:57 -07002516
Mohamed Abbas447fee72009-04-20 14:37:02 -07002517 if (done_search && !lq_sta->stay_in_tbl) {
Ben Cahill77626352007-11-29 11:09:44 +08002518 /* If the "active" (non-search) mode was legacy,
2519 * and we've tried switching antennas,
2520 * but we haven't been able to try HT modes (not available),
2521 * stay with best antenna legacy modulation for a while
2522 * before next round of mode comparisons. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002523 tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
Luis R. Rodriguezde27e642008-12-23 15:58:44 -08002524 if (is_legacy(tbl1->lq_type) && !conf_is_ht(conf) &&
Mohamed Abbasd6e93392009-05-08 13:44:39 -07002525 lq_sta->action_counter > tbl1->max_search) {
Tomas Winklere1623442009-01-27 14:27:56 -08002526 IWL_DEBUG_RATE(priv, "LQ: STAY in legacy table\n");
Ester Kummerbf403db2008-05-05 10:22:40 +08002527 rs_set_stay_in_table(priv, 1, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002528 }
2529
Ben Cahill77626352007-11-29 11:09:44 +08002530 /* If we're in an HT mode, and all 3 mode switch actions
2531 * have been tried and compared, stay in this best modulation
2532 * mode for a while before next round of mode comparisons. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002533 if (lq_sta->enable_counter &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002534 (lq_sta->action_counter >= tbl1->max_search) &&
2535 iwl_ht_enabled(priv)) {
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002536 if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
2537 (lq_sta->tx_agg_tid_en & (1 << tid)) &&
2538 (tid != MAX_TID_COUNT)) {
Wey-Yi Guy86b47662009-03-10 14:35:09 -07002539 tid_data =
2540 &priv->stations[lq_sta->lq.sta_id].tid[tid];
2541 if (tid_data->agg.state == IWL_AGG_OFF) {
2542 IWL_DEBUG_RATE(priv,
2543 "try to aggregate tid %d\n",
2544 tid);
2545 rs_tl_turn_on_agg(priv, tid,
2546 lq_sta, sta);
2547 }
Zhu Yib481de92007-09-25 17:54:57 -07002548 }
Ester Kummerbf403db2008-05-05 10:22:40 +08002549 rs_set_stay_in_table(priv, 0, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002550 }
Zhu Yib481de92007-09-25 17:54:57 -07002551 }
2552
2553out:
Tomas Winkler978785a2008-12-19 10:37:31 +08002554 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
Zhu Yib481de92007-09-25 17:54:57 -07002555 i = index;
Johannes Bergb7e35002008-09-11 02:22:58 +02002556 lq_sta->last_txrate_idx = i;
Zhu Yib481de92007-09-25 17:54:57 -07002557}
2558
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08002559/**
2560 * rs_initialize_lq - Initialize a station's hardware rate table
2561 *
2562 * The uCode's station table contains a table of fallback rates
2563 * for automatic fallback during transmission.
2564 *
2565 * NOTE: This sets up a default set of values. These will be replaced later
2566 * if the driver's iwl-agn-rs rate scaling algorithm is used, instead of
2567 * rc80211_simple.
2568 *
2569 * NOTE: Run REPLY_ADD_STA command to set up station table entry, before
2570 * calling this function (which runs REPLY_TX_LINK_QUALITY_CMD,
2571 * which requires station table entry to exist).
2572 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002573static void rs_initialize_lq(struct iwl_priv *priv,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002574 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02002575 struct ieee80211_sta *sta,
2576 struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002577{
Tomas Winklere227cea2008-07-18 13:53:05 +08002578 struct iwl_scale_tbl_info *tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002579 int rate_idx;
Guy Cohenaade00c2008-04-23 17:14:59 -07002580 int i;
Guy Cohen39e88502008-04-23 17:14:57 -07002581 u32 rate;
Johannes Berg7e6a5882010-08-23 10:46:46 +02002582 u8 use_green = rs_use_green(sta);
Guy Cohenaade00c2008-04-23 17:14:59 -07002583 u8 active_tbl = 0;
2584 u8 valid_tx_ant;
Johannes Berg7e6a5882010-08-23 10:46:46 +02002585 struct iwl_station_priv *sta_priv;
2586 struct iwl_rxon_context *ctx;
Zhu Yib481de92007-09-25 17:54:57 -07002587
Johannes Berg4b7679a2008-09-18 18:14:18 +02002588 if (!sta || !lq_sta)
Johannes Berg7e6a5882010-08-23 10:46:46 +02002589 return;
2590
2591 sta_priv = (void *)sta->drv_priv;
2592 ctx = sta_priv->common.ctx;
Zhu Yib481de92007-09-25 17:54:57 -07002593
Johannes Bergb7e35002008-09-11 02:22:58 +02002594 i = lq_sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002595
Guy Cohenaade00c2008-04-23 17:14:59 -07002596 valid_tx_ant = priv->hw_params.valid_tx_ant;
2597
Tomas Winklerc33104f2008-01-14 17:46:21 -08002598 if (!lq_sta->search_better_tbl)
2599 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002600 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002601 active_tbl = 1 - lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002602
Tomas Winklerc33104f2008-01-14 17:46:21 -08002603 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07002604
2605 if ((i < 0) || (i >= IWL_RATE_COUNT))
2606 i = 0;
2607
Tomas Winkler1826dcc2008-05-15 13:54:02 +08002608 rate = iwl_rates[i].plcp;
Winkler, Tomaseb48dca2008-10-29 14:05:48 -07002609 tbl->ant_type = first_antenna(valid_tx_ant);
2610 rate |= tbl->ant_type << RATE_MCS_ANT_POS;
Zhu Yib481de92007-09-25 17:54:57 -07002611
2612 if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
Guy Cohen39e88502008-04-23 17:14:57 -07002613 rate |= RATE_MCS_CCK_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002614
Guy Cohen39e88502008-04-23 17:14:57 -07002615 rs_get_tbl_info_from_mcs(rate, priv->band, tbl, &rate_idx);
Guy Cohenaade00c2008-04-23 17:14:59 -07002616 if (!rs_is_valid_ant(valid_tx_ant, tbl->ant_type))
2617 rs_toggle_antenna(valid_tx_ant, &rate, tbl);
Zhu Yib481de92007-09-25 17:54:57 -07002618
Tomas Winkler978785a2008-12-19 10:37:31 +08002619 rate = rate_n_flags_from_tbl(priv, tbl, rate_idx, use_green);
Guy Cohen39e88502008-04-23 17:14:57 -07002620 tbl->current_rate = rate;
Guy Coheneecd6e52008-04-23 17:14:58 -07002621 rs_set_expected_tpt_table(lq_sta, tbl);
Guy Cohen07bc28e2008-04-23 17:15:03 -07002622 rs_fill_link_cmd(NULL, lq_sta, rate);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08002623 priv->stations[lq_sta->lq.sta_id].lq = &lq_sta->lq;
Johannes Berg7e6a5882010-08-23 10:46:46 +02002624 iwl_send_lq_cmd(priv, ctx, &lq_sta->lq, CMD_SYNC, true);
Zhu Yib481de92007-09-25 17:54:57 -07002625}
2626
Johannes Berge6a98542008-10-21 12:40:02 +02002627static void rs_get_rate(void *priv_r, struct ieee80211_sta *sta, void *priv_sta,
2628 struct ieee80211_tx_rate_control *txrc)
Zhu Yib481de92007-09-25 17:54:57 -07002629{
2630
Johannes Berge6a98542008-10-21 12:40:02 +02002631 struct sk_buff *skb = txrc->skb;
2632 struct ieee80211_supported_band *sband = txrc->sband;
Reinette Chatref875f512010-04-05 10:43:10 -07002633 struct iwl_priv *priv __maybe_unused = (struct iwl_priv *)priv_r;
Johannes Berge6a98542008-10-21 12:40:02 +02002634 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Tomas Winkler417f1142008-11-12 13:14:06 -08002635 struct iwl_lq_sta *lq_sta = priv_sta;
2636 int rate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002637
Tomas Winklere1623442009-01-27 14:27:56 -08002638 IWL_DEBUG_RATE_LIMIT(priv, "rate scale calculate new rate for skb\n");
Zhu Yib481de92007-09-25 17:54:57 -07002639
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002640 /* Get max rate if user set max rate */
2641 if (lq_sta) {
2642 lq_sta->max_rate_idx = txrc->max_rate_idx;
2643 if ((sband->band == IEEE80211_BAND_5GHZ) &&
2644 (lq_sta->max_rate_idx != -1))
2645 lq_sta->max_rate_idx += IWL_FIRST_OFDM_RATE;
2646 if ((lq_sta->max_rate_idx < 0) ||
2647 (lq_sta->max_rate_idx >= IWL_RATE_COUNT))
2648 lq_sta->max_rate_idx = -1;
2649 }
2650
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08002651 /* Treat uninitialized rate scaling data same as non-existing. */
2652 if (lq_sta && !lq_sta->drv) {
2653 IWL_DEBUG_RATE(priv, "Rate scaling not initialized yet.\n");
2654 priv_sta = NULL;
2655 }
2656
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002657 /* Send management frames and NO_ACK data using lowest rate. */
Luis R. Rodriguez4c6d4f52009-07-16 10:05:41 -07002658 if (rate_control_send_low(sta, priv_sta, txrc))
Johannes Berg4b7679a2008-09-18 18:14:18 +02002659 return;
Zhu Yib481de92007-09-25 17:54:57 -07002660
Tomas Winkler417f1142008-11-12 13:14:06 -08002661 rate_idx = lq_sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002662
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002663 if (lq_sta->last_rate_n_flags & RATE_MCS_HT_MSK) {
Tomas Winkler417f1142008-11-12 13:14:06 -08002664 rate_idx -= IWL_FIRST_OFDM_RATE;
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002665 /* 6M and 9M shared same MCS index */
2666 rate_idx = (rate_idx > 0) ? (rate_idx - 1) : 0;
2667 if (rs_extract_rate(lq_sta->last_rate_n_flags) >=
2668 IWL_RATE_MIMO3_6M_PLCP)
2669 rate_idx = rate_idx + (2 * MCS_INDEX_PER_STREAM);
2670 else if (rs_extract_rate(lq_sta->last_rate_n_flags) >=
2671 IWL_RATE_MIMO2_6M_PLCP)
2672 rate_idx = rate_idx + MCS_INDEX_PER_STREAM;
2673 info->control.rates[0].flags = IEEE80211_TX_RC_MCS;
2674 if (lq_sta->last_rate_n_flags & RATE_MCS_SGI_MSK)
2675 info->control.rates[0].flags |= IEEE80211_TX_RC_SHORT_GI;
2676 if (lq_sta->last_rate_n_flags & RATE_MCS_DUP_MSK)
2677 info->control.rates[0].flags |= IEEE80211_TX_RC_DUP_DATA;
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07002678 if (lq_sta->last_rate_n_flags & RATE_MCS_HT40_MSK)
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002679 info->control.rates[0].flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
2680 if (lq_sta->last_rate_n_flags & RATE_MCS_GF_MSK)
2681 info->control.rates[0].flags |= IEEE80211_TX_RC_GREEN_FIELD;
2682 } else {
Daniel C Halperin50273092009-08-28 09:44:45 -07002683 /* Check for invalid rates */
2684 if ((rate_idx < 0) || (rate_idx >= IWL_RATE_COUNT_LEGACY) ||
2685 ((sband->band == IEEE80211_BAND_5GHZ) &&
2686 (rate_idx < IWL_FIRST_OFDM_RATE)))
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002687 rate_idx = rate_lowest_index(sband, sta);
Daniel C Halperin50273092009-08-28 09:44:45 -07002688 /* On valid 5 GHz rate, adjust index */
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002689 else if (sband->band == IEEE80211_BAND_5GHZ)
2690 rate_idx -= IWL_FIRST_OFDM_RATE;
Daniel C Halperin7ebaeff2009-08-21 13:34:20 -07002691 info->control.rates[0].flags = 0;
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002692 }
Tomas Winkler417f1142008-11-12 13:14:06 -08002693 info->control.rates[0].idx = rate_idx;
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002694
Zhu Yib481de92007-09-25 17:54:57 -07002695}
2696
Johannes Berg4b7679a2008-09-18 18:14:18 +02002697static void *rs_alloc_sta(void *priv_rate, struct ieee80211_sta *sta,
2698 gfp_t gfp)
Zhu Yib481de92007-09-25 17:54:57 -07002699{
Tomas Winklere227cea2008-07-18 13:53:05 +08002700 struct iwl_lq_sta *lq_sta;
Reinette Chatre8d9698b2009-10-16 14:25:55 -07002701 struct iwl_station_priv *sta_priv = (struct iwl_station_priv *) sta->drv_priv;
Ester Kummerbf403db2008-05-05 10:22:40 +08002702 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07002703
Ester Kummerbf403db2008-05-05 10:22:40 +08002704 priv = (struct iwl_priv *)priv_rate;
Tomas Winklere1623442009-01-27 14:27:56 -08002705 IWL_DEBUG_RATE(priv, "create station rate scale window\n");
Zhu Yib481de92007-09-25 17:54:57 -07002706
Reinette Chatre8d9698b2009-10-16 14:25:55 -07002707 lq_sta = &sta_priv->lq_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002708
Tomas Winklerc33104f2008-01-14 17:46:21 -08002709 return lq_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002710}
2711
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08002712/*
2713 * Called after adding a new station to initialize rate scaling
2714 */
2715void iwl_rs_rate_init(struct iwl_priv *priv, struct ieee80211_sta *sta, u8 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07002716{
2717 int i, j;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08002718 struct ieee80211_hw *hw = priv->hw;
Johannes Berg4b7679a2008-09-18 18:14:18 +02002719 struct ieee80211_conf *conf = &priv->hw->conf;
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002720 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08002721 struct iwl_station_priv *sta_priv;
2722 struct iwl_lq_sta *lq_sta;
2723 struct ieee80211_supported_band *sband;
Zhu Yib481de92007-09-25 17:54:57 -07002724
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08002725 sta_priv = (struct iwl_station_priv *) sta->drv_priv;
2726 lq_sta = &sta_priv->lq_sta;
2727 sband = hw->wiphy->bands[conf->channel->band];
2728
2729
2730 lq_sta->lq.sta_id = sta_id;
Reinette Chatre5ad13f82009-10-30 14:36:14 -07002731
2732 for (j = 0; j < LQ_SIZE; j++)
2733 for (i = 0; i < IWL_RATE_COUNT; i++)
2734 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
2735
Tomas Winklerc33104f2008-01-14 17:46:21 -08002736 lq_sta->flush_timer = 0;
Johannes Berg4b7679a2008-09-18 18:14:18 +02002737 lq_sta->supp_rates = sta->supp_rates[sband->band];
Zhu Yib481de92007-09-25 17:54:57 -07002738 for (j = 0; j < LQ_SIZE; j++)
2739 for (i = 0; i < IWL_RATE_COUNT; i++)
Tomas Winkler3ac7f142008-07-21 02:40:14 +03002740 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
Zhu Yib481de92007-09-25 17:54:57 -07002741
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08002742 IWL_DEBUG_RATE(priv, "LQ: *** rate scale station global init for station %d ***\n",
2743 sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07002744 /* TODO: what is a good starting rate for STA? About middle? Maybe not
2745 * the lowest or the highest rate.. Could consider using RSSI from
2746 * previous packets? Need to have IEEE 802.1X auth succeed immediately
2747 * after assoc.. */
2748
Tomas Winklerc33104f2008-01-14 17:46:21 -08002749 lq_sta->is_dup = 0;
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002750 lq_sta->max_rate_idx = -1;
Abbas, Mohamed7d049e52009-01-20 21:33:53 -08002751 lq_sta->missed_rate_counter = IWL_MISSED_RATE_MAX;
Johannes Berg7e6a5882010-08-23 10:46:46 +02002752 lq_sta->is_green = rs_use_green(sta);
Guy Cohen39e88502008-04-23 17:14:57 -07002753 lq_sta->active_legacy_rate = priv->active_rate & ~(0x1000);
Johannes Berg8318d782008-01-24 19:38:38 +01002754 lq_sta->band = priv->band;
Ben Cahill77626352007-11-29 11:09:44 +08002755 /*
2756 * active_siso_rate mask includes 9 MBits (bit 5), and CCK (bits 0-3),
2757 * supp_rates[] does not; shift to convert format, force 9 MBits off.
2758 */
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002759 lq_sta->active_siso_rate = ht_cap->mcs.rx_mask[0] << 1;
2760 lq_sta->active_siso_rate |= ht_cap->mcs.rx_mask[0] & 0x1;
Guy Cohenfde0db32008-04-21 15:42:01 -07002761 lq_sta->active_siso_rate &= ~((u16)0x2);
2762 lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -07002763
Ben Cahill77626352007-11-29 11:09:44 +08002764 /* Same here */
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002765 lq_sta->active_mimo2_rate = ht_cap->mcs.rx_mask[1] << 1;
2766 lq_sta->active_mimo2_rate |= ht_cap->mcs.rx_mask[1] & 0x1;
Guy Cohenfde0db32008-04-21 15:42:01 -07002767 lq_sta->active_mimo2_rate &= ~((u16)0x2);
2768 lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE;
2769
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002770 lq_sta->active_mimo3_rate = ht_cap->mcs.rx_mask[2] << 1;
2771 lq_sta->active_mimo3_rate |= ht_cap->mcs.rx_mask[2] & 0x1;
Guy Cohenfde0db32008-04-21 15:42:01 -07002772 lq_sta->active_mimo3_rate &= ~((u16)0x2);
2773 lq_sta->active_mimo3_rate <<= IWL_FIRST_OFDM_RATE;
2774
Tomas Winklere1623442009-01-27 14:27:56 -08002775 IWL_DEBUG_RATE(priv, "SISO-RATE=%X MIMO2-RATE=%X MIMO3-RATE=%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002776 lq_sta->active_siso_rate,
Guy Cohenfde0db32008-04-21 15:42:01 -07002777 lq_sta->active_mimo2_rate,
2778 lq_sta->active_mimo3_rate);
2779
Tomas Winklera96a27f2008-10-23 23:48:56 -07002780 /* These values will be overridden later */
Wey-Yi Guy3a23d692010-04-03 16:44:39 -07002781 lq_sta->lq.general_params.single_stream_ant_msk =
2782 first_antenna(priv->hw_params.valid_tx_ant);
2783 lq_sta->lq.general_params.dual_stream_ant_msk =
2784 priv->hw_params.valid_tx_ant &
2785 ~first_antenna(priv->hw_params.valid_tx_ant);
2786 if (!lq_sta->lq.general_params.dual_stream_ant_msk) {
2787 lq_sta->lq.general_params.dual_stream_ant_msk = ANT_AB;
2788 } else if (num_of_ant(priv->hw_params.valid_tx_ant) == 2) {
2789 lq_sta->lq.general_params.dual_stream_ant_msk =
2790 priv->hw_params.valid_tx_ant;
2791 }
Guy Cohen07bc28e2008-04-23 17:15:03 -07002792
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002793 /* as default allow aggregation for all tids */
2794 lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002795 lq_sta->drv = priv;
Zhu Yib481de92007-09-25 17:54:57 -07002796
Daniel C Halperin50273092009-08-28 09:44:45 -07002797 /* Set last_txrate_idx to lowest rate */
2798 lq_sta->last_txrate_idx = rate_lowest_index(sband, sta);
2799 if (sband->band == IEEE80211_BAND_5GHZ)
2800 lq_sta->last_txrate_idx += IWL_FIRST_OFDM_RATE;
Daniel C Halperine3949d62009-09-17 10:43:50 -07002801 lq_sta->is_agg = 0;
Mohamed Abbasd5e49032008-12-12 08:22:15 -08002802
Johannes Berg4b7679a2008-09-18 18:14:18 +02002803 rs_initialize_lq(priv, conf, sta, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002804}
2805
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002806static void rs_fill_link_cmd(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08002807 struct iwl_lq_sta *lq_sta, u32 new_rate)
Zhu Yib481de92007-09-25 17:54:57 -07002808{
Tomas Winklere227cea2008-07-18 13:53:05 +08002809 struct iwl_scale_tbl_info tbl_type;
Zhu Yib481de92007-09-25 17:54:57 -07002810 int index = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002811 int rate_idx;
Zhu Yi1b696de2007-09-27 11:27:41 +08002812 int repeat_rate = 0;
Guy Cohenaade00c2008-04-23 17:14:59 -07002813 u8 ant_toggle_cnt = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002814 u8 use_ht_possible = 1;
Guy Cohenaade00c2008-04-23 17:14:59 -07002815 u8 valid_tx_ant = 0;
Guy Cohen07bc28e2008-04-23 17:15:03 -07002816 struct iwl_link_quality_cmd *lq_cmd = &lq_sta->lq;
Zhu Yib481de92007-09-25 17:54:57 -07002817
Ben Cahill77626352007-11-29 11:09:44 +08002818 /* Override starting rate (index 0) if needed for debug purposes */
Guy Cohen39e88502008-04-23 17:14:57 -07002819 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Zhu Yi98d7e092007-09-27 11:27:42 +08002820
Guy Cohen39e88502008-04-23 17:14:57 -07002821 /* Interpret new_rate (rate_n_flags) */
2822 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band,
Zhu Yib481de92007-09-25 17:54:57 -07002823 &tbl_type, &rate_idx);
2824
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07002825 if (priv && priv->bt_full_concurrent) {
2826 /* 1x1 only */
2827 tbl_type.ant_type =
2828 first_antenna(priv->hw_params.valid_tx_ant);
2829 }
2830
Ben Cahill77626352007-11-29 11:09:44 +08002831 /* How many times should we repeat the initial rate? */
Zhu Yib481de92007-09-25 17:54:57 -07002832 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07002833 ant_toggle_cnt = 1;
Zhu Yi1b696de2007-09-27 11:27:41 +08002834 repeat_rate = IWL_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002835 } else {
Zhu Yi1b696de2007-09-27 11:27:41 +08002836 repeat_rate = IWL_HT_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002837 }
Zhu Yib481de92007-09-25 17:54:57 -07002838
2839 lq_cmd->general_params.mimo_delimiter =
2840 is_mimo(tbl_type.lq_type) ? 1 : 0;
Ben Cahill77626352007-11-29 11:09:44 +08002841
2842 /* Fill 1st table entry (index 0) */
Guy Cohen39e88502008-04-23 17:14:57 -07002843 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002844
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07002845 if (num_of_ant(tbl_type.ant_type) == 1) {
Guy Cohen07bc28e2008-04-23 17:15:03 -07002846 lq_cmd->general_params.single_stream_ant_msk =
2847 tbl_type.ant_type;
2848 } else if (num_of_ant(tbl_type.ant_type) == 2) {
2849 lq_cmd->general_params.dual_stream_ant_msk =
2850 tbl_type.ant_type;
2851 } /* otherwise we don't modify the existing value */
Zhu Yib481de92007-09-25 17:54:57 -07002852
2853 index++;
Zhu Yi1b696de2007-09-27 11:27:41 +08002854 repeat_rate--;
Wey-Yi Guybee008b2010-08-23 07:57:04 -07002855 if (priv) {
2856 if (priv->bt_full_concurrent)
2857 valid_tx_ant = ANT_A;
2858 else
2859 valid_tx_ant = priv->hw_params.valid_tx_ant;
2860 }
Guy Cohenaade00c2008-04-23 17:14:59 -07002861
Ben Cahill77626352007-11-29 11:09:44 +08002862 /* Fill rest of rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002863 while (index < LINK_QUAL_MAX_RETRY_NUM) {
Ben Cahill77626352007-11-29 11:09:44 +08002864 /* Repeat initial/next rate.
2865 * For legacy IWL_NUMBER_TRY == 1, this loop will not execute.
2866 * For HT IWL_HT_NUMBER_TRY == 3, this executes twice. */
Zhu Yi1b696de2007-09-27 11:27:41 +08002867 while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
Zhu Yib481de92007-09-25 17:54:57 -07002868 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07002869 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2870 ant_toggle_cnt++;
2871 else if (priv &&
2872 rs_toggle_antenna(valid_tx_ant,
2873 &new_rate, &tbl_type))
2874 ant_toggle_cnt = 1;
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07002875 }
Zhu Yi98d7e092007-09-27 11:27:42 +08002876
Ben Cahill77626352007-11-29 11:09:44 +08002877 /* Override next rate if needed for debug purposes */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002878 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Ben Cahill77626352007-11-29 11:09:44 +08002879
2880 /* Fill next table entry */
Zhu Yib481de92007-09-25 17:54:57 -07002881 lq_cmd->rs_table[index].rate_n_flags =
Guy Cohen39e88502008-04-23 17:14:57 -07002882 cpu_to_le32(new_rate);
Zhu Yi1b696de2007-09-27 11:27:41 +08002883 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002884 index++;
2885 }
2886
Guy Cohen39e88502008-04-23 17:14:57 -07002887 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band, &tbl_type,
Zhu Yib481de92007-09-25 17:54:57 -07002888 &rate_idx);
2889
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07002890 if (priv && priv->bt_full_concurrent) {
2891 /* 1x1 only */
2892 tbl_type.ant_type =
2893 first_antenna(priv->hw_params.valid_tx_ant);
2894 }
2895
Ben Cahill77626352007-11-29 11:09:44 +08002896 /* Indicate to uCode which entries might be MIMO.
2897 * If initial rate was MIMO, this will finally end up
2898 * as (IWL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
Zhu Yib481de92007-09-25 17:54:57 -07002899 if (is_mimo(tbl_type.lq_type))
2900 lq_cmd->general_params.mimo_delimiter = index;
2901
Ben Cahill77626352007-11-29 11:09:44 +08002902 /* Get next rate */
Guy Cohen39e88502008-04-23 17:14:57 -07002903 new_rate = rs_get_lower_rate(lq_sta, &tbl_type, rate_idx,
2904 use_ht_possible);
Zhu Yib481de92007-09-25 17:54:57 -07002905
Ben Cahill77626352007-11-29 11:09:44 +08002906 /* How many times should we repeat the next rate? */
Zhu Yib481de92007-09-25 17:54:57 -07002907 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07002908 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2909 ant_toggle_cnt++;
2910 else if (priv &&
2911 rs_toggle_antenna(valid_tx_ant,
2912 &new_rate, &tbl_type))
2913 ant_toggle_cnt = 1;
2914
Zhu Yi1b696de2007-09-27 11:27:41 +08002915 repeat_rate = IWL_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002916 } else {
Zhu Yi1b696de2007-09-27 11:27:41 +08002917 repeat_rate = IWL_HT_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002918 }
Zhu Yib481de92007-09-25 17:54:57 -07002919
Ben Cahill77626352007-11-29 11:09:44 +08002920 /* Don't allow HT rates after next pass.
2921 * rs_get_lower_rate() will change type to LQ_A or LQ_G. */
Zhu Yib481de92007-09-25 17:54:57 -07002922 use_ht_possible = 0;
2923
Ben Cahill77626352007-11-29 11:09:44 +08002924 /* Override next rate if needed for debug purposes */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002925 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Ben Cahill77626352007-11-29 11:09:44 +08002926
2927 /* Fill next table entry */
Guy Cohen39e88502008-04-23 17:14:57 -07002928 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002929
2930 index++;
Zhu Yi1b696de2007-09-27 11:27:41 +08002931 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002932 }
2933
Wey-Yi Guy4d80d722009-10-02 13:44:01 -07002934 lq_cmd->agg_params.agg_frame_cnt_limit = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
Wey-Yi Guy13c33a02009-06-03 11:44:11 -07002935 lq_cmd->agg_params.agg_dis_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
Wey-Yi Guy74697012010-07-28 09:18:54 -07002936
Wey-Yi Guy13c33a02009-06-03 11:44:11 -07002937 lq_cmd->agg_params.agg_time_limit =
2938 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
Wey-Yi Guy74697012010-07-28 09:18:54 -07002939 /*
2940 * overwrite if needed, pass aggregation time limit
2941 * to uCode in uSec
2942 */
Wey-Yi Guy7cb1b082010-10-06 08:10:00 -07002943 if (priv && priv->cfg->bt_params &&
2944 priv->cfg->bt_params->agg_time_limit &&
2945 priv->cfg->bt_params->agg_time_limit >=
2946 LINK_QUAL_AGG_TIME_LIMIT_MIN &&
2947 priv->cfg->bt_params->agg_time_limit <=
2948 LINK_QUAL_AGG_TIME_LIMIT_MAX)
Wey-Yi Guy74697012010-07-28 09:18:54 -07002949 lq_cmd->agg_params.agg_time_limit =
Wey-Yi Guy7cb1b082010-10-06 08:10:00 -07002950 cpu_to_le16(priv->cfg->bt_params->agg_time_limit);
Zhu Yib481de92007-09-25 17:54:57 -07002951}
2952
Johannes Berg4b7679a2008-09-18 18:14:18 +02002953static void *rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
Zhu Yib481de92007-09-25 17:54:57 -07002954{
Johannes Berg4b7679a2008-09-18 18:14:18 +02002955 return hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07002956}
2957/* rate scale requires free function to be implemented */
2958static void rs_free(void *priv_rate)
2959{
2960 return;
2961}
2962
Johannes Berg4b7679a2008-09-18 18:14:18 +02002963static void rs_free_sta(void *priv_r, struct ieee80211_sta *sta,
2964 void *priv_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002965{
Rami Rosen45527c22008-10-07 09:50:01 +02002966 struct iwl_priv *priv __maybe_unused = priv_r;
Zhu Yib481de92007-09-25 17:54:57 -07002967
Tomas Winklere1623442009-01-27 14:27:56 -08002968 IWL_DEBUG_RATE(priv, "enter\n");
Tomas Winklere1623442009-01-27 14:27:56 -08002969 IWL_DEBUG_RATE(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07002970}
2971
2972
Zhu Yi93dc6462007-09-27 11:27:37 +08002973#ifdef CONFIG_MAC80211_DEBUGFS
Zhu Yi5ae212c2007-09-27 11:27:38 +08002974static int open_file_generic(struct inode *inode, struct file *file)
2975{
2976 file->private_data = inode->i_private;
2977 return 0;
2978}
Tomas Winklere227cea2008-07-18 13:53:05 +08002979static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
2980 u32 *rate_n_flags, int index)
Zhu Yi98d7e092007-09-27 11:27:42 +08002981{
Ester Kummerbf403db2008-05-05 10:22:40 +08002982 struct iwl_priv *priv;
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07002983 u8 valid_tx_ant;
2984 u8 ant_sel_tx;
Ester Kummerbf403db2008-05-05 10:22:40 +08002985
2986 priv = lq_sta->drv;
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07002987 valid_tx_ant = priv->hw_params.valid_tx_ant;
Guy Cohen39e88502008-04-23 17:14:57 -07002988 if (lq_sta->dbg_fixed_rate) {
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07002989 ant_sel_tx =
2990 ((lq_sta->dbg_fixed_rate & RATE_MCS_ANT_ABC_MSK)
2991 >> RATE_MCS_ANT_POS);
2992 if ((valid_tx_ant & ant_sel_tx) == ant_sel_tx) {
Guy Cohen39e88502008-04-23 17:14:57 -07002993 *rate_n_flags = lq_sta->dbg_fixed_rate;
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07002994 IWL_DEBUG_RATE(priv, "Fixed rate ON\n");
Guy Cohen39e88502008-04-23 17:14:57 -07002995 } else {
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07002996 lq_sta->dbg_fixed_rate = 0;
2997 IWL_ERR(priv,
2998 "Invalid antenna selection 0x%X, Valid is 0x%X\n",
2999 ant_sel_tx, valid_tx_ant);
3000 IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
Guy Cohen39e88502008-04-23 17:14:57 -07003001 }
Guy Cohen39e88502008-04-23 17:14:57 -07003002 } else {
Tomas Winklere1623442009-01-27 14:27:56 -08003003 IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
Zhu Yi98d7e092007-09-27 11:27:42 +08003004 }
Zhu Yi98d7e092007-09-27 11:27:42 +08003005}
3006
3007static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
3008 const char __user *user_buf, size_t count, loff_t *ppos)
3009{
Tomas Winklere227cea2008-07-18 13:53:05 +08003010 struct iwl_lq_sta *lq_sta = file->private_data;
Ester Kummerbf403db2008-05-05 10:22:40 +08003011 struct iwl_priv *priv;
Zhu Yi98d7e092007-09-27 11:27:42 +08003012 char buf[64];
3013 int buf_size;
3014 u32 parsed_rate;
Johannes Berg7e6a5882010-08-23 10:46:46 +02003015 struct iwl_station_priv *sta_priv =
3016 container_of(lq_sta, struct iwl_station_priv, lq_sta);
3017 struct iwl_rxon_context *ctx = sta_priv->common.ctx;
Zhu Yi98d7e092007-09-27 11:27:42 +08003018
Ester Kummerbf403db2008-05-05 10:22:40 +08003019 priv = lq_sta->drv;
Zhu Yi98d7e092007-09-27 11:27:42 +08003020 memset(buf, 0, sizeof(buf));
3021 buf_size = min(count, sizeof(buf) - 1);
3022 if (copy_from_user(buf, user_buf, buf_size))
3023 return -EFAULT;
3024
3025 if (sscanf(buf, "%x", &parsed_rate) == 1)
Guy Cohen39e88502008-04-23 17:14:57 -07003026 lq_sta->dbg_fixed_rate = parsed_rate;
Zhu Yi98d7e092007-09-27 11:27:42 +08003027 else
Guy Cohen39e88502008-04-23 17:14:57 -07003028 lq_sta->dbg_fixed_rate = 0;
Zhu Yi98d7e092007-09-27 11:27:42 +08003029
Guy Cohen39e88502008-04-23 17:14:57 -07003030 lq_sta->active_legacy_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
3031 lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
3032 lq_sta->active_mimo2_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
3033 lq_sta->active_mimo3_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
Zhu Yi98d7e092007-09-27 11:27:42 +08003034
Tomas Winklere1623442009-01-27 14:27:56 -08003035 IWL_DEBUG_RATE(priv, "sta_id %d rate 0x%X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07003036 lq_sta->lq.sta_id, lq_sta->dbg_fixed_rate);
Zhu Yi98d7e092007-09-27 11:27:42 +08003037
Guy Cohen39e88502008-04-23 17:14:57 -07003038 if (lq_sta->dbg_fixed_rate) {
Guy Cohen07bc28e2008-04-23 17:15:03 -07003039 rs_fill_link_cmd(NULL, lq_sta, lq_sta->dbg_fixed_rate);
Johannes Berg7e6a5882010-08-23 10:46:46 +02003040 iwl_send_lq_cmd(lq_sta->drv, ctx, &lq_sta->lq, CMD_ASYNC,
3041 false);
Zhu Yi98d7e092007-09-27 11:27:42 +08003042 }
3043
3044 return count;
3045}
Zhu Yi0209dc12007-09-27 11:27:43 +08003046
Zhu Yi5ae212c2007-09-27 11:27:38 +08003047static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
3048 char __user *user_buf, size_t count, loff_t *ppos)
3049{
Frank Seidela412c802009-03-01 20:25:38 +01003050 char *buff;
Zhu Yi5ae212c2007-09-27 11:27:38 +08003051 int desc = 0;
3052 int i = 0;
Wey-Yi Guy12b96812009-04-08 11:39:27 -07003053 int index = 0;
Frank Seidela412c802009-03-01 20:25:38 +01003054 ssize_t ret;
Zhu Yi5ae212c2007-09-27 11:27:38 +08003055
Tomas Winklere227cea2008-07-18 13:53:05 +08003056 struct iwl_lq_sta *lq_sta = file->private_data;
Wey-Yi Guyd8ae4f52009-03-10 14:35:07 -07003057 struct iwl_priv *priv;
Wey-Yi Guyadb7b5e2009-03-10 14:35:08 -07003058 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
Zhu Yi5ae212c2007-09-27 11:27:38 +08003059
Wey-Yi Guyd8ae4f52009-03-10 14:35:07 -07003060 priv = lq_sta->drv;
Frank Seidela412c802009-03-01 20:25:38 +01003061 buff = kmalloc(1024, GFP_KERNEL);
3062 if (!buff)
3063 return -ENOMEM;
3064
Tomas Winklerc33104f2008-01-14 17:46:21 -08003065 desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
Zhu Yi98d7e092007-09-27 11:27:42 +08003066 desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08003067 lq_sta->total_failed, lq_sta->total_success,
Guy Cohen39e88502008-04-23 17:14:57 -07003068 lq_sta->active_legacy_rate);
Zhu Yi98d7e092007-09-27 11:27:42 +08003069 desc += sprintf(buff+desc, "fixed rate 0x%X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07003070 lq_sta->dbg_fixed_rate);
Wey-Yi Guyd8ae4f52009-03-10 14:35:07 -07003071 desc += sprintf(buff+desc, "valid_tx_ant %s%s%s\n",
3072 (priv->hw_params.valid_tx_ant & ANT_A) ? "ANT_A," : "",
3073 (priv->hw_params.valid_tx_ant & ANT_B) ? "ANT_B," : "",
3074 (priv->hw_params.valid_tx_ant & ANT_C) ? "ANT_C" : "");
Wey-Yi Guyadb7b5e2009-03-10 14:35:08 -07003075 desc += sprintf(buff+desc, "lq type %s\n",
3076 (is_legacy(tbl->lq_type)) ? "legacy" : "HT");
3077 if (is_Ht(tbl->lq_type)) {
3078 desc += sprintf(buff+desc, " %s",
3079 (is_siso(tbl->lq_type)) ? "SISO" :
3080 ((is_mimo2(tbl->lq_type)) ? "MIMO2" : "MIMO3"));
3081 desc += sprintf(buff+desc, " %s",
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07003082 (tbl->is_ht40) ? "40MHz" : "20MHz");
Daniel C Halperine3949d62009-09-17 10:43:50 -07003083 desc += sprintf(buff+desc, " %s %s %s\n", (tbl->is_SGI) ? "SGI" : "",
3084 (lq_sta->is_green) ? "GF enabled" : "",
3085 (lq_sta->is_agg) ? "AGG on" : "");
Wey-Yi Guyadb7b5e2009-03-10 14:35:08 -07003086 }
Wey-Yi Guy12b96812009-04-08 11:39:27 -07003087 desc += sprintf(buff+desc, "last tx rate=0x%X\n",
3088 lq_sta->last_rate_n_flags);
Zhu Yi5ae212c2007-09-27 11:27:38 +08003089 desc += sprintf(buff+desc, "general:"
3090 "flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08003091 lq_sta->lq.general_params.flags,
3092 lq_sta->lq.general_params.mimo_delimiter,
3093 lq_sta->lq.general_params.single_stream_ant_msk,
3094 lq_sta->lq.general_params.dual_stream_ant_msk);
Zhu Yi5ae212c2007-09-27 11:27:38 +08003095
3096 desc += sprintf(buff+desc, "agg:"
3097 "time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08003098 le16_to_cpu(lq_sta->lq.agg_params.agg_time_limit),
3099 lq_sta->lq.agg_params.agg_dis_start_th,
3100 lq_sta->lq.agg_params.agg_frame_cnt_limit);
Zhu Yi5ae212c2007-09-27 11:27:38 +08003101
3102 desc += sprintf(buff+desc,
3103 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08003104 lq_sta->lq.general_params.start_rate_index[0],
3105 lq_sta->lq.general_params.start_rate_index[1],
3106 lq_sta->lq.general_params.start_rate_index[2],
3107 lq_sta->lq.general_params.start_rate_index[3]);
Zhu Yi5ae212c2007-09-27 11:27:38 +08003108
Wey-Yi Guy12b96812009-04-08 11:39:27 -07003109 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
3110 index = iwl_hwrate_to_plcp_idx(
3111 le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags));
3112 if (is_legacy(tbl->lq_type)) {
3113 desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps\n",
3114 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags),
3115 iwl_rate_mcs[index].mbps);
3116 } else {
3117 desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps (%s)\n",
3118 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags),
3119 iwl_rate_mcs[index].mbps, iwl_rate_mcs[index].mcs);
3120 }
3121 }
Zhu Yi5ae212c2007-09-27 11:27:38 +08003122
Frank Seidela412c802009-03-01 20:25:38 +01003123 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
3124 kfree(buff);
3125 return ret;
Zhu Yi5ae212c2007-09-27 11:27:38 +08003126}
3127
3128static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
Zhu Yi98d7e092007-09-27 11:27:42 +08003129 .write = rs_sta_dbgfs_scale_table_write,
Zhu Yi5ae212c2007-09-27 11:27:38 +08003130 .read = rs_sta_dbgfs_scale_table_read,
3131 .open = open_file_generic,
3132};
Zhu Yi0209dc12007-09-27 11:27:43 +08003133static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
3134 char __user *user_buf, size_t count, loff_t *ppos)
3135{
Frank Seidela412c802009-03-01 20:25:38 +01003136 char *buff;
Zhu Yi0209dc12007-09-27 11:27:43 +08003137 int desc = 0;
3138 int i, j;
Frank Seidela412c802009-03-01 20:25:38 +01003139 ssize_t ret;
Zhu Yi0209dc12007-09-27 11:27:43 +08003140
Tomas Winklere227cea2008-07-18 13:53:05 +08003141 struct iwl_lq_sta *lq_sta = file->private_data;
Frank Seidela412c802009-03-01 20:25:38 +01003142
3143 buff = kmalloc(1024, GFP_KERNEL);
3144 if (!buff)
3145 return -ENOMEM;
3146
Zhu Yi0209dc12007-09-27 11:27:43 +08003147 for (i = 0; i < LQ_SIZE; i++) {
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07003148 desc += sprintf(buff+desc,
3149 "%s type=%d SGI=%d HT40=%d DUP=%d GF=%d\n"
Zhu Yi0209dc12007-09-27 11:27:43 +08003150 "rate=0x%X\n",
Abhijeet Kolekarc3056062008-11-12 13:14:08 -08003151 lq_sta->active_tbl == i ? "*" : "x",
Tomas Winklerc33104f2008-01-14 17:46:21 -08003152 lq_sta->lq_info[i].lq_type,
3153 lq_sta->lq_info[i].is_SGI,
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07003154 lq_sta->lq_info[i].is_ht40,
Tomas Winklerc33104f2008-01-14 17:46:21 -08003155 lq_sta->lq_info[i].is_dup,
Wey-Yi Guy2681b202009-05-21 13:44:22 -07003156 lq_sta->is_green,
Guy Cohen39e88502008-04-23 17:14:57 -07003157 lq_sta->lq_info[i].current_rate);
Zhu Yi0209dc12007-09-27 11:27:43 +08003158 for (j = 0; j < IWL_RATE_COUNT; j++) {
3159 desc += sprintf(buff+desc,
Tomas Winklerc33104f2008-01-14 17:46:21 -08003160 "counter=%d success=%d %%=%d\n",
3161 lq_sta->lq_info[i].win[j].counter,
3162 lq_sta->lq_info[i].win[j].success_counter,
3163 lq_sta->lq_info[i].win[j].success_ratio);
Zhu Yi0209dc12007-09-27 11:27:43 +08003164 }
3165 }
Frank Seidela412c802009-03-01 20:25:38 +01003166 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
3167 kfree(buff);
3168 return ret;
Zhu Yi0209dc12007-09-27 11:27:43 +08003169}
3170
3171static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
3172 .read = rs_sta_dbgfs_stats_table_read,
3173 .open = open_file_generic,
3174};
Zhu Yi5ae212c2007-09-27 11:27:38 +08003175
Wey-Yi Guy38167452009-05-08 13:44:43 -07003176static ssize_t rs_sta_dbgfs_rate_scale_data_read(struct file *file,
3177 char __user *user_buf, size_t count, loff_t *ppos)
3178{
3179 char buff[120];
3180 int desc = 0;
3181 ssize_t ret;
3182
3183 struct iwl_lq_sta *lq_sta = file->private_data;
3184 struct iwl_priv *priv;
3185 struct iwl_scale_tbl_info *tbl = &lq_sta->lq_info[lq_sta->active_tbl];
3186
3187 priv = lq_sta->drv;
3188
3189 if (is_Ht(tbl->lq_type))
3190 desc += sprintf(buff+desc,
3191 "Bit Rate= %d Mb/s\n",
3192 tbl->expected_tpt[lq_sta->last_txrate_idx]);
3193 else
3194 desc += sprintf(buff+desc,
3195 "Bit Rate= %d Mb/s\n",
3196 iwl_rates[lq_sta->last_txrate_idx].ieee >> 1);
Wey-Yi Guy38167452009-05-08 13:44:43 -07003197
3198 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
3199 return ret;
3200}
3201
3202static const struct file_operations rs_sta_dbgfs_rate_scale_data_ops = {
3203 .read = rs_sta_dbgfs_rate_scale_data_read,
3204 .open = open_file_generic,
3205};
3206
Zhu Yi93dc6462007-09-27 11:27:37 +08003207static void rs_add_debugfs(void *priv, void *priv_sta,
3208 struct dentry *dir)
3209{
Tomas Winklere227cea2008-07-18 13:53:05 +08003210 struct iwl_lq_sta *lq_sta = priv_sta;
Tomas Winklerc33104f2008-01-14 17:46:21 -08003211 lq_sta->rs_sta_dbgfs_scale_table_file =
Wey-Yi Guy43e85112009-11-20 12:04:58 -08003212 debugfs_create_file("rate_scale_table", S_IRUSR | S_IWUSR, dir,
Tomas Winklerc33104f2008-01-14 17:46:21 -08003213 lq_sta, &rs_sta_dbgfs_scale_table_ops);
3214 lq_sta->rs_sta_dbgfs_stats_table_file =
Wey-Yi Guy43e85112009-11-20 12:04:58 -08003215 debugfs_create_file("rate_stats_table", S_IRUSR, dir,
Tomas Winklerc33104f2008-01-14 17:46:21 -08003216 lq_sta, &rs_sta_dbgfs_stats_table_ops);
Wey-Yi Guy38167452009-05-08 13:44:43 -07003217 lq_sta->rs_sta_dbgfs_rate_scale_data_file =
Wey-Yi Guy43e85112009-11-20 12:04:58 -08003218 debugfs_create_file("rate_scale_data", S_IRUSR, dir,
Wey-Yi Guy38167452009-05-08 13:44:43 -07003219 lq_sta, &rs_sta_dbgfs_rate_scale_data_ops);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02003220 lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file =
Wey-Yi Guy43e85112009-11-20 12:04:58 -08003221 debugfs_create_u8("tx_agg_tid_enable", S_IRUSR | S_IWUSR, dir,
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02003222 &lq_sta->tx_agg_tid_en);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02003223
Zhu Yi93dc6462007-09-27 11:27:37 +08003224}
3225
3226static void rs_remove_debugfs(void *priv, void *priv_sta)
3227{
Tomas Winklere227cea2008-07-18 13:53:05 +08003228 struct iwl_lq_sta *lq_sta = priv_sta;
Tomas Winklerc33104f2008-01-14 17:46:21 -08003229 debugfs_remove(lq_sta->rs_sta_dbgfs_scale_table_file);
3230 debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
Wey-Yi Guy38167452009-05-08 13:44:43 -07003231 debugfs_remove(lq_sta->rs_sta_dbgfs_rate_scale_data_file);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02003232 debugfs_remove(lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file);
Zhu Yi93dc6462007-09-27 11:27:37 +08003233}
3234#endif
3235
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003236/*
3237 * Initialization of rate scaling information is done by driver after
3238 * the station is added. Since mac80211 calls this function before a
3239 * station is added we ignore it.
3240 */
3241static void rs_rate_init_stub(void *priv_r, struct ieee80211_supported_band *sband,
3242 struct ieee80211_sta *sta, void *priv_sta)
3243{
3244}
Zhu Yib481de92007-09-25 17:54:57 -07003245static struct rate_control_ops rs_ops = {
3246 .module = NULL,
3247 .name = RS_NAME,
3248 .tx_status = rs_tx_status,
3249 .get_rate = rs_get_rate,
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003250 .rate_init = rs_rate_init_stub,
Zhu Yib481de92007-09-25 17:54:57 -07003251 .alloc = rs_alloc,
3252 .free = rs_free,
3253 .alloc_sta = rs_alloc_sta,
3254 .free_sta = rs_free_sta,
Zhu Yi93dc6462007-09-27 11:27:37 +08003255#ifdef CONFIG_MAC80211_DEBUGFS
3256 .add_sta_debugfs = rs_add_debugfs,
3257 .remove_sta_debugfs = rs_remove_debugfs,
3258#endif
Zhu Yib481de92007-09-25 17:54:57 -07003259};
3260
Tomas Winklere227cea2008-07-18 13:53:05 +08003261int iwlagn_rate_control_register(void)
Zhu Yib481de92007-09-25 17:54:57 -07003262{
Reinette Chatre897e1cf2008-03-28 16:21:09 -07003263 return ieee80211_rate_control_register(&rs_ops);
Zhu Yib481de92007-09-25 17:54:57 -07003264}
3265
Tomas Winklere227cea2008-07-18 13:53:05 +08003266void iwlagn_rate_control_unregister(void)
Zhu Yib481de92007-09-25 17:54:57 -07003267{
3268 ieee80211_rate_control_unregister(&rs_ops);
3269}
3270