blob: 8bf7c20b9d3928b4aba502062800ee006092aae9 [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>
29#include <linux/wireless.h>
30#include <net/mac80211.h>
Zhu Yib481de92007-09-25 17:54:57 -070031
32#include <linux/netdevice.h>
33#include <linux/etherdevice.h>
34#include <linux/delay.h>
35
36#include <linux/workqueue.h>
37
Tomas Winkler3e0d4cb2008-04-24 11:55:38 -070038#include "iwl-dev.h"
Emmanuel Grumbachbe1f3ab62008-06-12 09:47:18 +080039#include "iwl-sta.h"
Tomas Winkler857485c2008-03-21 13:53:44 -070040#include "iwl-core.h"
Zhu Yib481de92007-09-25 17:54:57 -070041
Tomas Winklere227cea2008-07-18 13:53:05 +080042#define RS_NAME "iwl-agn-rs"
Zhu Yib481de92007-09-25 17:54:57 -070043
Guy Cohenaade00c2008-04-23 17:14:59 -070044#define NUM_TRY_BEFORE_ANT_TOGGLE 1
Zhu Yib481de92007-09-25 17:54:57 -070045#define IWL_NUMBER_TRY 1
46#define IWL_HT_NUMBER_TRY 3
47
Ben Cahill77626352007-11-29 11:09:44 +080048#define IWL_RATE_MAX_WINDOW 62 /* # tx in history window */
49#define IWL_RATE_MIN_FAILURE_TH 6 /* min failures to calc tpt */
50#define IWL_RATE_MIN_SUCCESS_TH 8 /* min successes to calc tpt */
51
Abbas, Mohamed7d049e52009-01-20 21:33:53 -080052/* max allowed rate miss before sync LQ cmd */
53#define IWL_MISSED_RATE_MAX 15
Ben Cahill77626352007-11-29 11:09:44 +080054/* max time to accum history 2 seconds */
Mohamed Abbas447fee72009-04-20 14:37:02 -070055#define IWL_RATE_SCALE_FLUSH_INTVL (3*HZ)
Zhu Yib481de92007-09-25 17:54:57 -070056
57static u8 rs_ht_to_legacy[] = {
58 IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
59 IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
60 IWL_RATE_6M_INDEX,
61 IWL_RATE_6M_INDEX, IWL_RATE_9M_INDEX,
62 IWL_RATE_12M_INDEX, IWL_RATE_18M_INDEX,
63 IWL_RATE_24M_INDEX, IWL_RATE_36M_INDEX,
64 IWL_RATE_48M_INDEX, IWL_RATE_54M_INDEX
65};
66
Guy Cohenaade00c2008-04-23 17:14:59 -070067static const u8 ant_toggle_lookup[] = {
68 /*ANT_NONE -> */ ANT_NONE,
69 /*ANT_A -> */ ANT_B,
70 /*ANT_B -> */ ANT_C,
71 /*ANT_AB -> */ ANT_BC,
72 /*ANT_C -> */ ANT_A,
73 /*ANT_AC -> */ ANT_AB,
74 /*ANT_BC -> */ ANT_AC,
75 /*ANT_ABC -> */ ANT_ABC,
76};
77
Tomas Winklerc79dd5b2008-03-12 16:58:50 -070078static void rs_rate_scale_perform(struct iwl_priv *priv,
Gábor Stefanik514d65c2009-04-23 19:36:08 +020079 struct sk_buff *skb,
Johannes Berg4b7679a2008-09-18 18:14:18 +020080 struct ieee80211_sta *sta,
81 struct iwl_lq_sta *lq_sta);
Wey-Yi Guy46f93812009-07-24 11:13:03 -070082static void rs_fill_link_cmd(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +080083 struct iwl_lq_sta *lq_sta, u32 rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -070084
85
Zhu Yi98d7e092007-09-27 11:27:42 +080086#ifdef CONFIG_MAC80211_DEBUGFS
Tomas Winklere227cea2008-07-18 13:53:05 +080087static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
88 u32 *rate_n_flags, int index);
Zhu Yi98d7e092007-09-27 11:27:42 +080089#else
Tomas Winklere227cea2008-07-18 13:53:05 +080090static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
91 u32 *rate_n_flags, int index)
Zhu Yi98d7e092007-09-27 11:27:42 +080092{}
93#endif
Ben Cahill77626352007-11-29 11:09:44 +080094
Daniel C Halperine3949d62009-09-17 10:43:50 -070095/**
96 * The following tables contain the expected throughput metrics for all rates
97 *
98 * 1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
99 *
100 * where invalid entries are zeros.
101 *
102 * CCK rates are only valid in legacy table and will only be used in G
103 * (2.4 GHz) band.
Ben Cahill77626352007-11-29 11:09:44 +0800104 */
Wey-Yi Guy584a0f02009-04-01 14:33:24 -0700105
Daniel C Halperine3949d62009-09-17 10:43:50 -0700106static s32 expected_tpt_legacy[IWL_RATE_COUNT] = {
107 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 0
Zhu Yib481de92007-09-25 17:54:57 -0700108};
109
Daniel C Halperine3949d62009-09-17 10:43:50 -0700110static s32 expected_tpt_siso20MHz[4][IWL_RATE_COUNT] = {
111 {0, 0, 0, 0, 42, 0, 76, 102, 124, 158, 183, 193, 202}, /* Norm */
112 {0, 0, 0, 0, 46, 0, 82, 110, 132, 167, 192, 202, 210}, /* SGI */
113 {0, 0, 0, 0, 48, 0, 93, 135, 176, 251, 319, 351, 381}, /* AGG */
114 {0, 0, 0, 0, 53, 0, 102, 149, 193, 275, 348, 381, 413}, /* AGG+SGI */
Zhu Yib481de92007-09-25 17:54:57 -0700115};
116
Daniel C Halperine3949d62009-09-17 10:43:50 -0700117static s32 expected_tpt_siso40MHz[4][IWL_RATE_COUNT] = {
118 {0, 0, 0, 0, 77, 0, 127, 160, 184, 220, 242, 250, 257}, /* Norm */
119 {0, 0, 0, 0, 83, 0, 135, 169, 193, 229, 250, 257, 264}, /* SGI */
120 {0, 0, 0, 0, 96, 0, 182, 259, 328, 451, 553, 598, 640}, /* AGG */
121 {0, 0, 0, 0, 106, 0, 199, 282, 357, 487, 593, 640, 683}, /* AGG+SGI */
Zhu Yib481de92007-09-25 17:54:57 -0700122};
123
Daniel C Halperine3949d62009-09-17 10:43:50 -0700124static s32 expected_tpt_mimo2_20MHz[4][IWL_RATE_COUNT] = {
125 {0, 0, 0, 0, 74, 0, 123, 155, 179, 213, 235, 243, 250}, /* Norm */
126 {0, 0, 0, 0, 81, 0, 131, 164, 187, 221, 242, 250, 256}, /* SGI */
127 {0, 0, 0, 0, 92, 0, 175, 250, 317, 436, 534, 578, 619}, /* AGG */
128 {0, 0, 0, 0, 102, 0, 192, 273, 344, 470, 573, 619, 660}, /* AGG+SGI*/
Zhu Yib481de92007-09-25 17:54:57 -0700129};
130
Daniel C Halperine3949d62009-09-17 10:43:50 -0700131static s32 expected_tpt_mimo2_40MHz[4][IWL_RATE_COUNT] = {
132 {0, 0, 0, 0, 123, 0, 182, 214, 235, 264, 279, 285, 289}, /* Norm */
133 {0, 0, 0, 0, 131, 0, 191, 222, 242, 270, 284, 289, 293}, /* SGI */
134 {0, 0, 0, 0, 180, 0, 327, 446, 545, 708, 828, 878, 922}, /* AGG */
135 {0, 0, 0, 0, 197, 0, 355, 481, 584, 752, 872, 922, 966}, /* AGG+SGI */
Zhu Yib481de92007-09-25 17:54:57 -0700136};
137
Daniel C Halperine3949d62009-09-17 10:43:50 -0700138static s32 expected_tpt_mimo3_20MHz[4][IWL_RATE_COUNT] = {
139 {0, 0, 0, 0, 99, 0, 153, 186, 208, 239, 256, 263, 268}, /* Norm */
140 {0, 0, 0, 0, 106, 0, 162, 194, 215, 246, 262, 268, 273}, /* SGI */
141 {0, 0, 0, 0, 134, 0, 249, 346, 431, 574, 685, 732, 775}, /* AGG */
142 {0, 0, 0, 0, 148, 0, 272, 376, 465, 614, 727, 775, 818}, /* AGG+SGI */
Zhu Yib481de92007-09-25 17:54:57 -0700143};
144
Daniel C Halperine3949d62009-09-17 10:43:50 -0700145static s32 expected_tpt_mimo3_40MHz[4][IWL_RATE_COUNT] = {
146 {0, 0, 0, 0, 152, 0, 211, 239, 255, 279, 290, 294, 297}, /* Norm */
147 {0, 0, 0, 0, 160, 0, 219, 245, 261, 284, 294, 297, 300}, /* SGI */
148 {0, 0, 0, 0, 254, 0, 443, 584, 695, 868, 984, 1030, 1070}, /* AGG */
149 {0, 0, 0, 0, 277, 0, 478, 624, 737, 911, 1026, 1070, 1109}, /* AGG+SGI */
Wey-Yi Guy584a0f02009-04-01 14:33:24 -0700150};
151
Wey-Yi Guy12b96812009-04-08 11:39:27 -0700152/* mbps, mcs */
Tobias Klauser79496732009-12-23 14:18:11 +0100153static const struct iwl_rate_mcs_info iwl_rate_mcs[IWL_RATE_COUNT] = {
Daniel C Halperine3949d62009-09-17 10:43:50 -0700154 { "1", "BPSK DSSS"},
155 { "2", "QPSK DSSS"},
156 {"5.5", "BPSK CCK"},
157 { "11", "QPSK CCK"},
158 { "6", "BPSK 1/2"},
159 { "9", "BPSK 1/2"},
160 { "12", "QPSK 1/2"},
161 { "18", "QPSK 3/4"},
162 { "24", "16QAM 1/2"},
163 { "36", "16QAM 3/4"},
164 { "48", "64QAM 2/3"},
165 { "54", "64QAM 3/4"},
166 { "60", "64QAM 5/6"},
Wey-Yi Guy12b96812009-04-08 11:39:27 -0700167};
168
Wey-Yi Guya9c146b2009-05-22 11:01:48 -0700169#define MCS_INDEX_PER_STREAM (8)
170
Guy Cohen39e88502008-04-23 17:14:57 -0700171static inline u8 rs_extract_rate(u32 rate_n_flags)
Zhu Yib481de92007-09-25 17:54:57 -0700172{
173 return (u8)(rate_n_flags & 0xFF);
174}
175
Tomas Winklere227cea2008-07-18 13:53:05 +0800176static void rs_rate_scale_clear_window(struct iwl_rate_scale_data *window)
Zhu Yib481de92007-09-25 17:54:57 -0700177{
178 window->data = 0;
179 window->success_counter = 0;
180 window->success_ratio = IWL_INVALID_VALUE;
181 window->counter = 0;
182 window->average_tpt = IWL_INVALID_VALUE;
183 window->stamp = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700184}
185
Guy Cohenaade00c2008-04-23 17:14:59 -0700186static inline u8 rs_is_valid_ant(u8 valid_antenna, u8 ant_type)
187{
Tomas Winkler3ac7f142008-07-21 02:40:14 +0300188 return (ant_type & valid_antenna) == ant_type;
Guy Cohenaade00c2008-04-23 17:14:59 -0700189}
190
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200191/*
192 * removes the old data from the statistics. All data that is older than
193 * TID_MAX_TIME_DIFF, will be deleted.
194 */
Tomas Winklere227cea2008-07-18 13:53:05 +0800195static void rs_tl_rm_old_stats(struct iwl_traffic_load *tl, u32 curr_time)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200196{
197 /* The oldest age we want to keep */
198 u32 oldest_time = curr_time - TID_MAX_TIME_DIFF;
199
200 while (tl->queue_count &&
201 (tl->time_stamp < oldest_time)) {
202 tl->total -= tl->packet_count[tl->head];
203 tl->packet_count[tl->head] = 0;
204 tl->time_stamp += TID_QUEUE_CELL_SPACING;
205 tl->queue_count--;
206 tl->head++;
207 if (tl->head >= TID_QUEUE_MAX_SIZE)
208 tl->head = 0;
209 }
210}
211
212/*
213 * increment traffic load value for tid and also remove
214 * any old values if passed the certain time period
215 */
Tomas Winklere227cea2008-07-18 13:53:05 +0800216static u8 rs_tl_add_packet(struct iwl_lq_sta *lq_data,
Ron Rindjunskyfaa29712008-06-12 09:46:58 +0800217 struct ieee80211_hdr *hdr)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200218{
219 u32 curr_time = jiffies_to_msecs(jiffies);
220 u32 time_diff;
221 s32 index;
Tomas Winklere227cea2008-07-18 13:53:05 +0800222 struct iwl_traffic_load *tl = NULL;
Tomas Winkler54dbb522008-05-15 13:54:06 +0800223 u8 tid;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200224
Tomas Winkler417f1142008-11-12 13:14:06 -0800225 if (ieee80211_is_data_qos(hdr->frame_control)) {
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700226 u8 *qc = ieee80211_get_qos_ctl(hdr);
Tomas Winkler54dbb522008-05-15 13:54:06 +0800227 tid = qc[0] & 0xf;
228 } else
Ron Rindjunskyfaa29712008-06-12 09:46:58 +0800229 return MAX_TID_COUNT;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200230
Reinette Chatree6a6cf42009-08-13 13:30:50 -0700231 if (unlikely(tid >= TID_MAX_LOAD_COUNT))
232 return MAX_TID_COUNT;
233
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200234 tl = &lq_data->load[tid];
235
236 curr_time -= curr_time % TID_ROUND_VALUE;
237
238 /* Happens only for the first packet. Initialize the data */
239 if (!(tl->queue_count)) {
240 tl->total = 1;
241 tl->time_stamp = curr_time;
242 tl->queue_count = 1;
243 tl->head = 0;
244 tl->packet_count[0] = 1;
Ron Rindjunskyfaa29712008-06-12 09:46:58 +0800245 return MAX_TID_COUNT;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200246 }
247
248 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
249 index = time_diff / TID_QUEUE_CELL_SPACING;
250
251 /* The history is too long: remove data that is older than */
252 /* TID_MAX_TIME_DIFF */
253 if (index >= TID_QUEUE_MAX_SIZE)
254 rs_tl_rm_old_stats(tl, curr_time);
255
256 index = (tl->head + index) % TID_QUEUE_MAX_SIZE;
257 tl->packet_count[index] = tl->packet_count[index] + 1;
258 tl->total = tl->total + 1;
259
260 if ((index + 1) > tl->queue_count)
261 tl->queue_count = index + 1;
Ron Rindjunskyfaa29712008-06-12 09:46:58 +0800262
263 return tid;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200264}
265
266/*
267 get the traffic load value for tid
268*/
Tomas Winklere227cea2008-07-18 13:53:05 +0800269static u32 rs_tl_get_load(struct iwl_lq_sta *lq_data, u8 tid)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200270{
271 u32 curr_time = jiffies_to_msecs(jiffies);
272 u32 time_diff;
273 s32 index;
Tomas Winklere227cea2008-07-18 13:53:05 +0800274 struct iwl_traffic_load *tl = NULL;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200275
276 if (tid >= TID_MAX_LOAD_COUNT)
277 return 0;
278
279 tl = &(lq_data->load[tid]);
280
281 curr_time -= curr_time % TID_ROUND_VALUE;
282
283 if (!(tl->queue_count))
284 return 0;
285
286 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
287 index = time_diff / TID_QUEUE_CELL_SPACING;
288
289 /* The history is too long: remove data that is older than */
290 /* TID_MAX_TIME_DIFF */
291 if (index >= TID_QUEUE_MAX_SIZE)
292 rs_tl_rm_old_stats(tl, curr_time);
293
294 return tl->total;
295}
296
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700297static void rs_tl_turn_on_agg_for_tid(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +0800298 struct iwl_lq_sta *lq_data, u8 tid,
Johannes Berg4b7679a2008-09-18 18:14:18 +0200299 struct ieee80211_sta *sta)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200300{
Wey-Yi Guy45d42702010-02-03 12:24:44 -0800301 int ret;
302
Johannes Bergff550cb2008-09-11 03:17:05 +0200303 if (rs_tl_get_load(lq_data, tid) > IWL_AGG_LOAD_THRESHOLD) {
Tomas Winklere1623442009-01-27 14:27:56 -0800304 IWL_DEBUG_HT(priv, "Starting Tx agg: STA: %pM tid: %d\n",
Johannes Berge1749612008-10-27 15:59:26 -0700305 sta->addr, tid);
Wey-Yi Guy45d42702010-02-03 12:24:44 -0800306 ret = ieee80211_start_tx_ba_session(sta, tid);
307 if (ret == -EAGAIN) {
308 /*
309 * driver and mac80211 is out of sync
310 * this might be cause by reloading firmware
311 * stop the tx ba session here
312 */
313 IWL_DEBUG_HT(priv, "Fail start Tx agg on tid: %d\n",
314 tid);
315 ret = ieee80211_stop_tx_ba_session(sta, tid,
316 WLAN_BACK_INITIATOR);
317 }
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200318 }
319}
320
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700321static void rs_tl_turn_on_agg(struct iwl_priv *priv, u8 tid,
Tomas Winklere227cea2008-07-18 13:53:05 +0800322 struct iwl_lq_sta *lq_data,
Johannes Berg4b7679a2008-09-18 18:14:18 +0200323 struct ieee80211_sta *sta)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200324{
325 if ((tid < TID_MAX_LOAD_COUNT))
326 rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
327 else if (tid == IWL_AGG_ALL_TID)
328 for (tid = 0; tid < TID_MAX_LOAD_COUNT; tid++)
329 rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
Wey-Yi Guy47eef9b2009-09-17 10:43:44 -0700330 if (priv->cfg->use_rts_for_ht) {
331 /*
332 * switch to RTS/CTS if it is the prefer protection method
333 * for HT traffic
334 */
335 IWL_DEBUG_HT(priv, "use RTS/CTS protection for HT\n");
336 priv->staging_rxon.flags &= ~RXON_FLG_SELF_CTS_EN;
337 iwlcore_commit_rxon(priv);
338 }
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200339}
340
Guy Cohen39e88502008-04-23 17:14:57 -0700341static inline int get_num_of_ant_from_rate(u32 rate_n_flags)
Guy Cohenfde0db32008-04-21 15:42:01 -0700342{
Tomas Winkler3ac7f142008-07-21 02:40:14 +0300343 return !!(rate_n_flags & RATE_MCS_ANT_A_MSK) +
344 !!(rate_n_flags & RATE_MCS_ANT_B_MSK) +
345 !!(rate_n_flags & RATE_MCS_ANT_C_MSK);
Guy Cohenfde0db32008-04-21 15:42:01 -0700346}
347
Ben Cahill77626352007-11-29 11:09:44 +0800348/**
349 * rs_collect_tx_data - Update the success/failure sliding window
350 *
351 * We keep a sliding window of the last 62 packets transmitted
352 * at this rate. window->data contains the bitmask of successful
353 * packets.
354 */
Tomas Winklere227cea2008-07-18 13:53:05 +0800355static int rs_collect_tx_data(struct iwl_rate_scale_data *windows,
Daniel C Halperine3949d62009-09-17 10:43:50 -0700356 int scale_index, s32 tpt, int attempts,
Ron Rindjunsky99556432008-01-28 14:07:25 +0200357 int successes)
Zhu Yib481de92007-09-25 17:54:57 -0700358{
Tomas Winklere227cea2008-07-18 13:53:05 +0800359 struct iwl_rate_scale_data *window = NULL;
Guy Cohen39e88502008-04-23 17:14:57 -0700360 static const u64 mask = (((u64)1) << (IWL_RATE_MAX_WINDOW - 1));
Zhu Yib481de92007-09-25 17:54:57 -0700361 s32 fail_count;
362
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800363 if (scale_index < 0 || scale_index >= IWL_RATE_COUNT)
364 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -0700365
Daniel C Halperine3949d62009-09-17 10:43:50 -0700366 /* Select window for current tx bit rate */
Zhu Yib481de92007-09-25 17:54:57 -0700367 window = &(windows[scale_index]);
368
Ben Cahill77626352007-11-29 11:09:44 +0800369 /*
370 * Keep track of only the latest 62 tx frame attempts in this rate's
371 * history window; anything older isn't really relevant any more.
372 * If we have filled up the sliding window, drop the oldest attempt;
373 * if the oldest attempt (highest bit in bitmap) shows "success",
374 * subtract "1" from the success counter (this is the main reason
375 * we keep these bitmaps!).
376 */
Daniel C Halperine3949d62009-09-17 10:43:50 -0700377 while (attempts > 0) {
Guy Cohen39e88502008-04-23 17:14:57 -0700378 if (window->counter >= IWL_RATE_MAX_WINDOW) {
379
380 /* remove earliest */
381 window->counter = IWL_RATE_MAX_WINDOW - 1;
382
Ron Rindjunsky99556432008-01-28 14:07:25 +0200383 if (window->data & mask) {
384 window->data &= ~mask;
Guy Cohen39e88502008-04-23 17:14:57 -0700385 window->success_counter--;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200386 }
Zhu Yib481de92007-09-25 17:54:57 -0700387 }
Zhu Yib481de92007-09-25 17:54:57 -0700388
Ron Rindjunsky99556432008-01-28 14:07:25 +0200389 /* Increment frames-attempted counter */
390 window->counter++;
Ben Cahill77626352007-11-29 11:09:44 +0800391
Daniel C Halperine3949d62009-09-17 10:43:50 -0700392 /* Shift bitmap by one frame to throw away oldest history */
Guy Cohen90d77952008-09-09 10:54:53 +0800393 window->data <<= 1;
Daniel C Halperine3949d62009-09-17 10:43:50 -0700394
395 /* Mark the most recent #successes attempts as successful */
Ron Rindjunsky99556432008-01-28 14:07:25 +0200396 if (successes > 0) {
Guy Cohen39e88502008-04-23 17:14:57 -0700397 window->success_counter++;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200398 window->data |= 0x1;
399 successes--;
400 }
401
Daniel C Halperine3949d62009-09-17 10:43:50 -0700402 attempts--;
Zhu Yib481de92007-09-25 17:54:57 -0700403 }
404
Ben Cahill77626352007-11-29 11:09:44 +0800405 /* Calculate current success ratio, avoid divide-by-0! */
Zhu Yib481de92007-09-25 17:54:57 -0700406 if (window->counter > 0)
407 window->success_ratio = 128 * (100 * window->success_counter)
408 / window->counter;
409 else
410 window->success_ratio = IWL_INVALID_VALUE;
411
412 fail_count = window->counter - window->success_counter;
413
Ben Cahill77626352007-11-29 11:09:44 +0800414 /* Calculate average throughput, if we have enough history. */
Zhu Yib481de92007-09-25 17:54:57 -0700415 if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
416 (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
417 window->average_tpt = (window->success_ratio * tpt + 64) / 128;
418 else
419 window->average_tpt = IWL_INVALID_VALUE;
420
Ben Cahill77626352007-11-29 11:09:44 +0800421 /* Tag this window as having been updated */
Zhu Yib481de92007-09-25 17:54:57 -0700422 window->stamp = jiffies;
423
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800424 return 0;
Zhu Yib481de92007-09-25 17:54:57 -0700425}
426
Ben Cahill77626352007-11-29 11:09:44 +0800427/*
428 * Fill uCode API rate_n_flags field, based on "search" or "active" table.
429 */
Guy Cohen39e88502008-04-23 17:14:57 -0700430/* FIXME:RS:remove this function and put the flags statically in the table */
Tomas Winkler978785a2008-12-19 10:37:31 +0800431static u32 rate_n_flags_from_tbl(struct iwl_priv *priv,
432 struct iwl_scale_tbl_info *tbl,
433 int index, u8 use_green)
Zhu Yib481de92007-09-25 17:54:57 -0700434{
Guy Cohen39e88502008-04-23 17:14:57 -0700435 u32 rate_n_flags = 0;
436
Zhu Yib481de92007-09-25 17:54:57 -0700437 if (is_legacy(tbl->lq_type)) {
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800438 rate_n_flags = iwl_rates[index].plcp;
Zhu Yib481de92007-09-25 17:54:57 -0700439 if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
Guy Cohen39e88502008-04-23 17:14:57 -0700440 rate_n_flags |= RATE_MCS_CCK_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700441
Guy Cohenfde0db32008-04-21 15:42:01 -0700442 } else if (is_Ht(tbl->lq_type)) {
443 if (index > IWL_LAST_OFDM_RATE) {
Tomas Winkler978785a2008-12-19 10:37:31 +0800444 IWL_ERR(priv, "Invalid HT rate index %d\n", index);
Zhu Yib481de92007-09-25 17:54:57 -0700445 index = IWL_LAST_OFDM_RATE;
Guy Cohenfde0db32008-04-21 15:42:01 -0700446 }
Guy Cohen39e88502008-04-23 17:14:57 -0700447 rate_n_flags = RATE_MCS_HT_MSK;
Guy Cohenfde0db32008-04-21 15:42:01 -0700448
449 if (is_siso(tbl->lq_type))
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800450 rate_n_flags |= iwl_rates[index].plcp_siso;
Guy Cohenfde0db32008-04-21 15:42:01 -0700451 else if (is_mimo2(tbl->lq_type))
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800452 rate_n_flags |= iwl_rates[index].plcp_mimo2;
Guy Cohenfde0db32008-04-21 15:42:01 -0700453 else
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800454 rate_n_flags |= iwl_rates[index].plcp_mimo3;
Zhu Yib481de92007-09-25 17:54:57 -0700455 } else {
Tomas Winkler978785a2008-12-19 10:37:31 +0800456 IWL_ERR(priv, "Invalid tbl->lq_type %d\n", tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -0700457 }
458
Guy Cohen39e88502008-04-23 17:14:57 -0700459 rate_n_flags |= ((tbl->ant_type << RATE_MCS_ANT_POS) &
Guy Cohenfde0db32008-04-21 15:42:01 -0700460 RATE_MCS_ANT_ABC_MSK);
Zhu Yib481de92007-09-25 17:54:57 -0700461
Guy Cohen39e88502008-04-23 17:14:57 -0700462 if (is_Ht(tbl->lq_type)) {
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700463 if (tbl->is_ht40) {
Guy Cohen39e88502008-04-23 17:14:57 -0700464 if (tbl->is_dup)
465 rate_n_flags |= RATE_MCS_DUP_MSK;
466 else
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700467 rate_n_flags |= RATE_MCS_HT40_MSK;
Guy Cohen39e88502008-04-23 17:14:57 -0700468 }
469 if (tbl->is_SGI)
470 rate_n_flags |= RATE_MCS_SGI_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700471
Guy Cohen39e88502008-04-23 17:14:57 -0700472 if (use_green) {
473 rate_n_flags |= RATE_MCS_GF_MSK;
474 if (is_siso(tbl->lq_type) && tbl->is_SGI) {
475 rate_n_flags &= ~RATE_MCS_SGI_MSK;
Tomas Winkler978785a2008-12-19 10:37:31 +0800476 IWL_ERR(priv, "GF was set with SGI:SISO\n");
Guy Cohen39e88502008-04-23 17:14:57 -0700477 }
478 }
Zhu Yib481de92007-09-25 17:54:57 -0700479 }
Guy Cohen39e88502008-04-23 17:14:57 -0700480 return rate_n_flags;
Zhu Yib481de92007-09-25 17:54:57 -0700481}
482
Ben Cahill77626352007-11-29 11:09:44 +0800483/*
484 * Interpret uCode API's rate_n_flags format,
485 * fill "search" or "active" tx mode table.
486 */
Guy Cohen39e88502008-04-23 17:14:57 -0700487static int rs_get_tbl_info_from_mcs(const u32 rate_n_flags,
Johannes Berg8318d782008-01-24 19:38:38 +0100488 enum ieee80211_band band,
Tomas Winklere227cea2008-07-18 13:53:05 +0800489 struct iwl_scale_tbl_info *tbl,
Zhu Yib481de92007-09-25 17:54:57 -0700490 int *rate_idx)
491{
Guy Cohen39e88502008-04-23 17:14:57 -0700492 u32 ant_msk = (rate_n_flags & RATE_MCS_ANT_ABC_MSK);
493 u8 num_of_ant = get_num_of_ant_from_rate(rate_n_flags);
494 u8 mcs;
Zhu Yib481de92007-09-25 17:54:57 -0700495
Tomas Winklere7d326a2008-06-12 09:47:11 +0800496 *rate_idx = iwl_hwrate_to_plcp_idx(rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -0700497
Guy Cohenfde0db32008-04-21 15:42:01 -0700498 if (*rate_idx == IWL_RATE_INVALID) {
Zhu Yib481de92007-09-25 17:54:57 -0700499 *rate_idx = -1;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800500 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -0700501 }
Ben Cahill77626352007-11-29 11:09:44 +0800502 tbl->is_SGI = 0; /* default legacy setup */
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700503 tbl->is_ht40 = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700504 tbl->is_dup = 0;
Guy Cohenfde0db32008-04-21 15:42:01 -0700505 tbl->ant_type = (ant_msk >> RATE_MCS_ANT_POS);
506 tbl->lq_type = LQ_NONE;
Mohamed Abbasd6e93392009-05-08 13:44:39 -0700507 tbl->max_search = IWL_MAX_SEARCH;
Zhu Yib481de92007-09-25 17:54:57 -0700508
Ben Cahill77626352007-11-29 11:09:44 +0800509 /* legacy rate format */
Guy Cohen39e88502008-04-23 17:14:57 -0700510 if (!(rate_n_flags & RATE_MCS_HT_MSK)) {
Guy Cohenfde0db32008-04-21 15:42:01 -0700511 if (num_of_ant == 1) {
Johannes Berg8318d782008-01-24 19:38:38 +0100512 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700513 tbl->lq_type = LQ_A;
514 else
515 tbl->lq_type = LQ_G;
Zhu Yib481de92007-09-25 17:54:57 -0700516 }
Guy Cohenfde0db32008-04-21 15:42:01 -0700517 /* HT rate format */
Zhu Yib481de92007-09-25 17:54:57 -0700518 } else {
Guy Cohen39e88502008-04-23 17:14:57 -0700519 if (rate_n_flags & RATE_MCS_SGI_MSK)
Zhu Yib481de92007-09-25 17:54:57 -0700520 tbl->is_SGI = 1;
521
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700522 if ((rate_n_flags & RATE_MCS_HT40_MSK) ||
Guy Cohen39e88502008-04-23 17:14:57 -0700523 (rate_n_flags & RATE_MCS_DUP_MSK))
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700524 tbl->is_ht40 = 1;
Zhu Yib481de92007-09-25 17:54:57 -0700525
Guy Cohen39e88502008-04-23 17:14:57 -0700526 if (rate_n_flags & RATE_MCS_DUP_MSK)
Zhu Yib481de92007-09-25 17:54:57 -0700527 tbl->is_dup = 1;
Guy Cohenfde0db32008-04-21 15:42:01 -0700528
Guy Cohen39e88502008-04-23 17:14:57 -0700529 mcs = rs_extract_rate(rate_n_flags);
Guy Cohenfde0db32008-04-21 15:42:01 -0700530
Guy Cohen39e88502008-04-23 17:14:57 -0700531 /* SISO */
532 if (mcs <= IWL_RATE_SISO_60M_PLCP) {
Guy Cohenfde0db32008-04-21 15:42:01 -0700533 if (num_of_ant == 1)
534 tbl->lq_type = LQ_SISO; /*else NONE*/
535 /* MIMO2 */
Guy Cohen39e88502008-04-23 17:14:57 -0700536 } else if (mcs <= IWL_RATE_MIMO2_60M_PLCP) {
Guy Cohenfde0db32008-04-21 15:42:01 -0700537 if (num_of_ant == 2)
538 tbl->lq_type = LQ_MIMO2;
539 /* MIMO3 */
540 } else {
Mohamed Abbasd6e93392009-05-08 13:44:39 -0700541 if (num_of_ant == 3) {
542 tbl->max_search = IWL_MAX_11N_MIMO3_SEARCH;
Guy Cohenfde0db32008-04-21 15:42:01 -0700543 tbl->lq_type = LQ_MIMO3;
Mohamed Abbasd6e93392009-05-08 13:44:39 -0700544 }
Guy Cohenfde0db32008-04-21 15:42:01 -0700545 }
Zhu Yib481de92007-09-25 17:54:57 -0700546 }
547 return 0;
548}
Guy Cohenaade00c2008-04-23 17:14:59 -0700549
550/* switch to another antenna/antennas and return 1 */
551/* if no other valid antenna found, return 0 */
552static int rs_toggle_antenna(u32 valid_ant, u32 *rate_n_flags,
Tomas Winklere227cea2008-07-18 13:53:05 +0800553 struct iwl_scale_tbl_info *tbl)
Zhu Yib481de92007-09-25 17:54:57 -0700554{
Guy Cohenaade00c2008-04-23 17:14:59 -0700555 u8 new_ant_type;
556
557 if (!tbl->ant_type || tbl->ant_type > ANT_ABC)
558 return 0;
559
560 if (!rs_is_valid_ant(valid_ant, tbl->ant_type))
561 return 0;
562
563 new_ant_type = ant_toggle_lookup[tbl->ant_type];
564
565 while ((new_ant_type != tbl->ant_type) &&
566 !rs_is_valid_ant(valid_ant, new_ant_type))
567 new_ant_type = ant_toggle_lookup[new_ant_type];
568
569 if (new_ant_type == tbl->ant_type)
570 return 0;
571
572 tbl->ant_type = new_ant_type;
573 *rate_n_flags &= ~RATE_MCS_ANT_ABC_MSK;
574 *rate_n_flags |= new_ant_type << RATE_MCS_ANT_POS;
575 return 1;
Zhu Yib481de92007-09-25 17:54:57 -0700576}
577
Daniel C Halperinb2617932009-08-13 13:30:59 -0700578/**
579 * Green-field mode is valid if the station supports it and
580 * there are no non-GF stations present in the BSS.
581 */
582static inline u8 rs_use_green(struct ieee80211_sta *sta,
Johannes Bergfad95bf2009-09-11 10:38:15 -0700583 struct iwl_ht_config *ht_conf)
Zhu Yib481de92007-09-25 17:54:57 -0700584{
Daniel C Halperinb2617932009-08-13 13:30:59 -0700585 return (sta->ht_cap.cap & IEEE80211_HT_CAP_GRN_FLD) &&
586 !(ht_conf->non_GF_STA_present);
Guy Cohenf935a6d2008-04-23 17:15:02 -0700587}
Zhu Yib481de92007-09-25 17:54:57 -0700588
589/**
590 * rs_get_supported_rates - get the available rates
591 *
592 * if management frame or broadcast frame only return
593 * basic available rates.
594 *
595 */
Tomas Winklere227cea2008-07-18 13:53:05 +0800596static u16 rs_get_supported_rates(struct iwl_lq_sta *lq_sta,
597 struct ieee80211_hdr *hdr,
598 enum iwl_table_type rate_type)
Zhu Yib481de92007-09-25 17:54:57 -0700599{
Zhu Yib481de92007-09-25 17:54:57 -0700600 if (hdr && is_multicast_ether_addr(hdr->addr1) &&
Guy Coheneecd6e52008-04-23 17:14:58 -0700601 lq_sta->active_rate_basic)
602 return lq_sta->active_rate_basic;
603
604 if (is_legacy(rate_type)) {
605 return lq_sta->active_legacy_rate;
606 } else {
607 if (is_siso(rate_type))
608 return lq_sta->active_siso_rate;
609 else if (is_mimo2(rate_type))
610 return lq_sta->active_mimo2_rate;
611 else
612 return lq_sta->active_mimo3_rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -0800613 }
Zhu Yib481de92007-09-25 17:54:57 -0700614}
615
Ester Kummerbf403db2008-05-05 10:22:40 +0800616static u16 rs_get_adjacent_rate(struct iwl_priv *priv, u8 index, u16 rate_mask,
617 int rate_type)
Zhu Yib481de92007-09-25 17:54:57 -0700618{
619 u8 high = IWL_RATE_INVALID;
620 u8 low = IWL_RATE_INVALID;
621
Ian Schram01ebd062007-10-25 17:15:22 +0800622 /* 802.11A or ht walks to the next literal adjacent rate in
Zhu Yib481de92007-09-25 17:54:57 -0700623 * the rate table */
624 if (is_a_band(rate_type) || !is_legacy(rate_type)) {
625 int i;
626 u32 mask;
627
628 /* Find the previous rate that is in the rate mask */
629 i = index - 1;
630 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
631 if (rate_mask & mask) {
632 low = i;
633 break;
634 }
635 }
636
637 /* Find the next rate that is in the rate mask */
638 i = index + 1;
639 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
640 if (rate_mask & mask) {
641 high = i;
642 break;
643 }
644 }
645
646 return (high << 8) | low;
647 }
648
649 low = index;
650 while (low != IWL_RATE_INVALID) {
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800651 low = iwl_rates[low].prev_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700652 if (low == IWL_RATE_INVALID)
653 break;
654 if (rate_mask & (1 << low))
655 break;
Tomas Winklere1623442009-01-27 14:27:56 -0800656 IWL_DEBUG_RATE(priv, "Skipping masked lower rate: %d\n", low);
Zhu Yib481de92007-09-25 17:54:57 -0700657 }
658
659 high = index;
660 while (high != IWL_RATE_INVALID) {
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800661 high = iwl_rates[high].next_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700662 if (high == IWL_RATE_INVALID)
663 break;
664 if (rate_mask & (1 << high))
665 break;
Tomas Winklere1623442009-01-27 14:27:56 -0800666 IWL_DEBUG_RATE(priv, "Skipping masked higher rate: %d\n", high);
Zhu Yib481de92007-09-25 17:54:57 -0700667 }
668
669 return (high << 8) | low;
670}
671
Tomas Winklere227cea2008-07-18 13:53:05 +0800672static u32 rs_get_lower_rate(struct iwl_lq_sta *lq_sta,
673 struct iwl_scale_tbl_info *tbl,
674 u8 scale_index, u8 ht_possible)
Zhu Yib481de92007-09-25 17:54:57 -0700675{
Zhu Yib481de92007-09-25 17:54:57 -0700676 s32 low;
677 u16 rate_mask;
678 u16 high_low;
679 u8 switch_to_legacy = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -0800680 u8 is_green = lq_sta->is_green;
Wey-Yi Guy2ff65782009-09-11 10:38:18 -0700681 struct iwl_priv *priv = lq_sta->drv;
Zhu Yib481de92007-09-25 17:54:57 -0700682
683 /* check if we need to switch from HT to legacy rates.
684 * assumption is that mandatory rates (1Mbps or 6Mbps)
685 * are always supported (spec demand) */
686 if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_index)) {
687 switch_to_legacy = 1;
688 scale_index = rs_ht_to_legacy[scale_index];
Johannes Berg8318d782008-01-24 19:38:38 +0100689 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700690 tbl->lq_type = LQ_A;
691 else
692 tbl->lq_type = LQ_G;
693
Guy Cohen69fdb302008-04-23 17:15:01 -0700694 if (num_of_ant(tbl->ant_type) > 1)
Wey-Yi Guy2ff65782009-09-11 10:38:18 -0700695 tbl->ant_type =
696 first_antenna(priv->hw_params.valid_tx_ant);
Zhu Yib481de92007-09-25 17:54:57 -0700697
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700698 tbl->is_ht40 = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700699 tbl->is_SGI = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -0700700 tbl->max_search = IWL_MAX_SEARCH;
Zhu Yib481de92007-09-25 17:54:57 -0700701 }
702
Guy Coheneecd6e52008-04-23 17:14:58 -0700703 rate_mask = rs_get_supported_rates(lq_sta, NULL, tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -0700704
Ben Cahill77626352007-11-29 11:09:44 +0800705 /* Mask with station rate restriction */
Zhu Yib481de92007-09-25 17:54:57 -0700706 if (is_legacy(tbl->lq_type)) {
Ben Cahill77626352007-11-29 11:09:44 +0800707 /* supp_rates has no CCK bits in A mode */
Johannes Berg8318d782008-01-24 19:38:38 +0100708 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700709 rate_mask = (u16)(rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -0800710 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
Zhu Yib481de92007-09-25 17:54:57 -0700711 else
Tomas Winklerc33104f2008-01-14 17:46:21 -0800712 rate_mask = (u16)(rate_mask & lq_sta->supp_rates);
Zhu Yib481de92007-09-25 17:54:57 -0700713 }
714
Ben Cahill77626352007-11-29 11:09:44 +0800715 /* If we switched from HT to legacy, check current rate */
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800716 if (switch_to_legacy && (rate_mask & (1 << scale_index))) {
Guy Cohen39e88502008-04-23 17:14:57 -0700717 low = scale_index;
718 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700719 }
720
Ester Kummerbf403db2008-05-05 10:22:40 +0800721 high_low = rs_get_adjacent_rate(lq_sta->drv, scale_index, rate_mask,
722 tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -0700723 low = high_low & 0xff;
724
Guy Cohen39e88502008-04-23 17:14:57 -0700725 if (low == IWL_RATE_INVALID)
726 low = scale_index;
727
728out:
Tomas Winkler978785a2008-12-19 10:37:31 +0800729 return rate_n_flags_from_tbl(lq_sta->drv, tbl, low, is_green);
Zhu Yib481de92007-09-25 17:54:57 -0700730}
731
Ben Cahill77626352007-11-29 11:09:44 +0800732/*
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700733 * Simple function to compare two rate scale table types
734 */
735static bool table_type_matches(struct iwl_scale_tbl_info *a,
736 struct iwl_scale_tbl_info *b)
737{
738 return (a->lq_type == b->lq_type) && (a->ant_type == b->ant_type) &&
739 (a->is_SGI == b->is_SGI);
740}
741/*
742 * Static function to get the expected throughput from an iwl_scale_tbl_info
743 * that wraps a NULL pointer check
744 */
745static s32 get_expected_tpt(struct iwl_scale_tbl_info *tbl, int rs_index)
746{
747 if (tbl->expected_tpt)
748 return tbl->expected_tpt[rs_index];
749 return 0;
750}
751
752/*
Ben Cahill77626352007-11-29 11:09:44 +0800753 * mac80211 sends us Tx status
754 */
Johannes Berg4b7679a2008-09-18 18:14:18 +0200755static void rs_tx_status(void *priv_r, struct ieee80211_supported_band *sband,
756 struct ieee80211_sta *sta, void *priv_sta,
Johannes Berge039fa42008-05-15 12:55:29 +0200757 struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -0700758{
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700759 int legacy_success;
760 int retries;
761 int rs_index, mac_index, i;
Tomas Winkler417f1142008-11-12 13:14:06 -0800762 struct iwl_lq_sta *lq_sta = priv_sta;
Tomas Winkler66c73db2008-04-15 16:01:40 -0700763 struct iwl_link_quality_cmd *table;
Zhu Yib481de92007-09-25 17:54:57 -0700764 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berg4b7679a2008-09-18 18:14:18 +0200765 struct iwl_priv *priv = (struct iwl_priv *)priv_r;
Johannes Berge039fa42008-05-15 12:55:29 +0200766 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Tomas Winklere227cea2008-07-18 13:53:05 +0800767 struct iwl_rate_scale_data *window = NULL;
Daniel C Halperin50273092009-08-28 09:44:45 -0700768 enum mac80211_rate_control_flags mac_flags;
Guy Cohen39e88502008-04-23 17:14:57 -0700769 u32 tx_rate;
Tomas Winklere227cea2008-07-18 13:53:05 +0800770 struct iwl_scale_tbl_info tbl_type;
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700771 struct iwl_scale_tbl_info *curr_tbl, *other_tbl;
Zhu Yib481de92007-09-25 17:54:57 -0700772 s32 tpt = 0;
773
Tomas Winklere1623442009-01-27 14:27:56 -0800774 IWL_DEBUG_RATE_LIMIT(priv, "get frame ack response, update rate scale window\n");
Zhu Yib481de92007-09-25 17:54:57 -0700775
Tomas Winkler417f1142008-11-12 13:14:06 -0800776 if (!ieee80211_is_data(hdr->frame_control) ||
Gábor Stefanik514d65c2009-04-23 19:36:08 +0200777 info->flags & IEEE80211_TX_CTL_NO_ACK)
Zhu Yib481de92007-09-25 17:54:57 -0700778 return;
779
Daniel C Halperine3949d62009-09-17 10:43:50 -0700780 /* This packet was aggregated but doesn't carry status info */
Johannes Berge039fa42008-05-15 12:55:29 +0200781 if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
782 !(info->flags & IEEE80211_TX_STAT_AMPDU))
Ron Rindjunsky99556432008-01-28 14:07:25 +0200783 return;
784
Johannes Berg05c914f2008-09-11 00:01:58 +0200785 if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) &&
Tomas Winklerc33104f2008-01-14 17:46:21 -0800786 !lq_sta->ibss_sta_added)
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700787 return;
Zhu Yib481de92007-09-25 17:54:57 -0700788
Ben Cahill77626352007-11-29 11:09:44 +0800789 /*
790 * Ignore this Tx frame response if its initial rate doesn't match
791 * that of latest Link Quality command. There may be stragglers
792 * from a previous Link Quality command, but we're no longer interested
793 * in those; they're either from the "active" mode while we're trying
794 * to check "search" mode, or a prior "search" mode after we've moved
795 * to a new "search" mode (which might become the new "active" mode).
796 */
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700797 table = &lq_sta->lq;
Guy Cohen39e88502008-04-23 17:14:57 -0700798 tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
799 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800800 if (priv->band == IEEE80211_BAND_5GHZ)
801 rs_index -= IWL_FIRST_OFDM_RATE;
Daniel C Halperin50273092009-08-28 09:44:45 -0700802 mac_flags = info->status.rates[0].flags;
803 mac_index = info->status.rates[0].idx;
Daniel C Halperin31513be2009-08-28 09:44:47 -0700804 /* For HT packets, map MCS to PLCP */
805 if (mac_flags & IEEE80211_TX_RC_MCS) {
806 mac_index &= RATE_MCS_CODE_MSK; /* Remove # of streams */
807 if (mac_index >= (IWL_RATE_9M_INDEX - IWL_FIRST_OFDM_RATE))
808 mac_index++;
Daniel C Halperin392a0ba2009-09-11 10:38:08 -0700809 /*
810 * mac80211 HT index is always zero-indexed; we need to move
811 * HT OFDM rates after CCK rates in 2.4 GHz band
812 */
813 if (priv->band == IEEE80211_BAND_2GHZ)
814 mac_index += IWL_FIRST_OFDM_RATE;
Daniel C Halperin31513be2009-08-28 09:44:47 -0700815 }
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700816 /* Here we actually compare this rate to the latest LQ command */
Daniel C Halperin50273092009-08-28 09:44:45 -0700817 if ((mac_index < 0) ||
818 (tbl_type.is_SGI != !!(mac_flags & IEEE80211_TX_RC_SHORT_GI)) ||
819 (tbl_type.is_ht40 != !!(mac_flags & IEEE80211_TX_RC_40_MHZ_WIDTH)) ||
820 (tbl_type.is_dup != !!(mac_flags & IEEE80211_TX_RC_DUP_DATA)) ||
Johannes Berge6a98542008-10-21 12:40:02 +0200821 (tbl_type.ant_type != info->antenna_sel_tx) ||
Daniel C Halperin50273092009-08-28 09:44:45 -0700822 (!!(tx_rate & RATE_MCS_HT_MSK) != !!(mac_flags & IEEE80211_TX_RC_MCS)) ||
823 (!!(tx_rate & RATE_MCS_GF_MSK) != !!(mac_flags & IEEE80211_TX_RC_GREEN_FIELD)) ||
Daniel C Halperin31513be2009-08-28 09:44:47 -0700824 (rs_index != mac_index)) {
825 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 -0700826 /*
827 * Since rates mis-match, the last LQ command may have failed.
828 * After IWL_MISSED_RATE_MAX mis-matches, resync the uCode with
829 * ... driver.
Mohamed Abbasd5e49032008-12-12 08:22:15 -0800830 */
Abbas, Mohamed7d049e52009-01-20 21:33:53 -0800831 lq_sta->missed_rate_counter++;
832 if (lq_sta->missed_rate_counter > IWL_MISSED_RATE_MAX) {
833 lq_sta->missed_rate_counter = 0;
834 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
835 }
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700836 /* Regardless, ignore this status info for outdated rate */
837 return;
838 } else
839 /* Rate did match, so reset the missed_rate_counter */
840 lq_sta->missed_rate_counter = 0;
841
842 /* Figure out if rate scale algorithm is in active or search table */
843 if (table_type_matches(&tbl_type,
844 &(lq_sta->lq_info[lq_sta->active_tbl]))) {
845 curr_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
846 other_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
847 } else if (table_type_matches(&tbl_type,
848 &lq_sta->lq_info[1 - lq_sta->active_tbl])) {
849 curr_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
850 other_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
851 } else {
852 IWL_DEBUG_RATE(priv, "Neither active nor search matches tx rate\n");
853 return;
Zhu Yib481de92007-09-25 17:54:57 -0700854 }
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700855 window = (struct iwl_rate_scale_data *)&(curr_tbl->win[0]);
Zhu Yib481de92007-09-25 17:54:57 -0700856
Ben Cahill77626352007-11-29 11:09:44 +0800857 /*
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700858 * Updating the frame history depends on whether packets were
859 * aggregated.
860 *
861 * For aggregation, all packets were transmitted at the same rate, the
862 * first index into rate scale table.
Ben Cahill77626352007-11-29 11:09:44 +0800863 */
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700864 if (info->flags & IEEE80211_TX_STAT_AMPDU) {
865 tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
866 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type,
867 &rs_index);
868 tpt = get_expected_tpt(curr_tbl, rs_index);
869 rs_collect_tx_data(window, rs_index, tpt,
870 info->status.ampdu_ack_len,
871 info->status.ampdu_ack_map);
Zhu Yib481de92007-09-25 17:54:57 -0700872
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700873 /* Update success/fail counts if not searching for new mode */
874 if (lq_sta->stay_in_tbl) {
Johannes Berge039fa42008-05-15 12:55:29 +0200875 lq_sta->total_success += info->status.ampdu_ack_map;
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700876 lq_sta->total_failed += (info->status.ampdu_ack_len -
877 info->status.ampdu_ack_map);
878 }
879 } else {
880 /*
881 * For legacy, update frame history with for each Tx retry.
882 */
883 retries = info->status.rates[0].count - 1;
884 /* HW doesn't send more than 15 retries */
885 retries = min(retries, 15);
886
887 /* The last transmission may have been successful */
888 legacy_success = !!(info->flags & IEEE80211_TX_STAT_ACK);
889 /* Collect data for each rate used during failed TX attempts */
890 for (i = 0; i <= retries; ++i) {
891 tx_rate = le32_to_cpu(table->rs_table[i].rate_n_flags);
892 rs_get_tbl_info_from_mcs(tx_rate, priv->band,
893 &tbl_type, &rs_index);
894 /*
895 * Only collect stats if retried rate is in the same RS
896 * table as active/search.
897 */
898 if (table_type_matches(&tbl_type, curr_tbl))
899 tpt = get_expected_tpt(curr_tbl, rs_index);
900 else if (table_type_matches(&tbl_type, other_tbl))
901 tpt = get_expected_tpt(other_tbl, rs_index);
Ron Rindjunsky99556432008-01-28 14:07:25 +0200902 else
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700903 continue;
904
905 /* Constants mean 1 transmission, 0 successes */
906 if (i < retries)
907 rs_collect_tx_data(window, rs_index, tpt, 1,
908 0);
909 else
910 rs_collect_tx_data(window, rs_index, tpt, 1,
911 legacy_success);
912 }
913
914 /* Update success/fail counts if not searching for new mode */
915 if (lq_sta->stay_in_tbl) {
916 lq_sta->total_success += legacy_success;
917 lq_sta->total_failed += retries + (1 - legacy_success);
Ron Rindjunsky99556432008-01-28 14:07:25 +0200918 }
Zhu Yib481de92007-09-25 17:54:57 -0700919 }
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700920 /* The last TX rate is cached in lq_sta; it's set in if/else above */
921 lq_sta->last_rate_n_flags = tx_rate;
Zhu Yib481de92007-09-25 17:54:57 -0700922
Ben Cahill77626352007-11-29 11:09:44 +0800923 /* See if there's a better rate or modulation mode to try. */
Abbas, Mohamedc338ba32009-01-21 10:58:02 -0800924 if (sta && sta->supp_rates[sband->band])
Gábor Stefanik514d65c2009-04-23 19:36:08 +0200925 rs_rate_scale_perform(priv, skb, sta, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -0700926}
927
Ben Cahill77626352007-11-29 11:09:44 +0800928/*
929 * Begin a period of staying with a selected modulation mode.
930 * Set "stay_in_tbl" flag to prevent any mode switches.
931 * Set frame tx success limits according to legacy vs. high-throughput,
932 * and reset overall (spanning all rates) tx success history statistics.
933 * These control how long we stay using same modulation mode before
934 * searching for a new mode.
935 */
Ester Kummerbf403db2008-05-05 10:22:40 +0800936static void rs_set_stay_in_table(struct iwl_priv *priv, u8 is_legacy,
Tomas Winklere227cea2008-07-18 13:53:05 +0800937 struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -0700938{
Tomas Winklere1623442009-01-27 14:27:56 -0800939 IWL_DEBUG_RATE(priv, "we are staying in the same table\n");
Tomas Winklerc33104f2008-01-14 17:46:21 -0800940 lq_sta->stay_in_tbl = 1; /* only place this gets set */
Zhu Yib481de92007-09-25 17:54:57 -0700941 if (is_legacy) {
Tomas Winklerc33104f2008-01-14 17:46:21 -0800942 lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT;
943 lq_sta->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
944 lq_sta->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
Zhu Yib481de92007-09-25 17:54:57 -0700945 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -0800946 lq_sta->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
947 lq_sta->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
948 lq_sta->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
Zhu Yib481de92007-09-25 17:54:57 -0700949 }
Tomas Winklerc33104f2008-01-14 17:46:21 -0800950 lq_sta->table_count = 0;
951 lq_sta->total_failed = 0;
952 lq_sta->total_success = 0;
Mohamed Abbas447fee72009-04-20 14:37:02 -0700953 lq_sta->flush_timer = jiffies;
Mohamed Abbasd6e93392009-05-08 13:44:39 -0700954 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700955}
956
Ben Cahill77626352007-11-29 11:09:44 +0800957/*
958 * Find correct throughput table for given mode of modulation
959 */
Tomas Winklere227cea2008-07-18 13:53:05 +0800960static void rs_set_expected_tpt_table(struct iwl_lq_sta *lq_sta,
961 struct iwl_scale_tbl_info *tbl)
Zhu Yib481de92007-09-25 17:54:57 -0700962{
Daniel C Halperine3949d62009-09-17 10:43:50 -0700963 /* Used to choose among HT tables */
964 s32 (*ht_tbl_pointer)[IWL_RATE_COUNT];
965
966 /* Check for invalid LQ type */
967 if (WARN_ON_ONCE(!is_legacy(tbl->lq_type) && !is_Ht(tbl->lq_type))) {
968 tbl->expected_tpt = expected_tpt_legacy;
969 return;
970 }
971
972 /* Legacy rates have only one table */
Zhu Yib481de92007-09-25 17:54:57 -0700973 if (is_legacy(tbl->lq_type)) {
Daniel C Halperine3949d62009-09-17 10:43:50 -0700974 tbl->expected_tpt = expected_tpt_legacy;
975 return;
976 }
977
978 /* Choose among many HT tables depending on number of streams
979 * (SISO/MIMO2/MIMO3), channel width (20/40), SGI, and aggregation
980 * status */
981 if (is_siso(tbl->lq_type) && (!tbl->is_ht40 || lq_sta->is_dup))
982 ht_tbl_pointer = expected_tpt_siso20MHz;
983 else if (is_siso(tbl->lq_type))
984 ht_tbl_pointer = expected_tpt_siso40MHz;
985 else if (is_mimo2(tbl->lq_type) && (!tbl->is_ht40 || lq_sta->is_dup))
986 ht_tbl_pointer = expected_tpt_mimo2_20MHz;
987 else if (is_mimo2(tbl->lq_type))
988 ht_tbl_pointer = expected_tpt_mimo2_40MHz;
989 else if (is_mimo3(tbl->lq_type) && (!tbl->is_ht40 || lq_sta->is_dup))
990 ht_tbl_pointer = expected_tpt_mimo3_20MHz;
991 else /* if (is_mimo3(tbl->lq_type)) <-- must be true */
992 ht_tbl_pointer = expected_tpt_mimo3_40MHz;
993
994 if (!tbl->is_SGI && !lq_sta->is_agg) /* Normal */
995 tbl->expected_tpt = ht_tbl_pointer[0];
996 else if (tbl->is_SGI && !lq_sta->is_agg) /* SGI */
997 tbl->expected_tpt = ht_tbl_pointer[1];
998 else if (!tbl->is_SGI && lq_sta->is_agg) /* AGG */
999 tbl->expected_tpt = ht_tbl_pointer[2];
1000 else /* AGG+SGI */
1001 tbl->expected_tpt = ht_tbl_pointer[3];
Zhu Yib481de92007-09-25 17:54:57 -07001002}
1003
Ben Cahill77626352007-11-29 11:09:44 +08001004/*
1005 * Find starting rate for new "search" high-throughput mode of modulation.
1006 * Goal is to find lowest expected rate (under perfect conditions) that is
1007 * above the current measured throughput of "active" mode, to give new mode
1008 * a fair chance to prove itself without too many challenges.
1009 *
1010 * This gets called when transitioning to more aggressive modulation
1011 * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
1012 * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
1013 * to decrease to match "active" throughput. When moving from MIMO to SISO,
1014 * bit rate will typically need to increase, but not if performance was bad.
1015 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001016static s32 rs_get_best_rate(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001017 struct iwl_lq_sta *lq_sta,
1018 struct iwl_scale_tbl_info *tbl, /* "search" */
Guy Cohenf935a6d2008-04-23 17:15:02 -07001019 u16 rate_mask, s8 index)
Zhu Yib481de92007-09-25 17:54:57 -07001020{
Ben Cahill77626352007-11-29 11:09:44 +08001021 /* "active" values */
Tomas Winklere227cea2008-07-18 13:53:05 +08001022 struct iwl_scale_tbl_info *active_tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001023 &(lq_sta->lq_info[lq_sta->active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07001024 s32 active_sr = active_tbl->win[index].success_ratio;
Zhu Yib481de92007-09-25 17:54:57 -07001025 s32 active_tpt = active_tbl->expected_tpt[index];
Ben Cahill77626352007-11-29 11:09:44 +08001026
1027 /* expected "search" throughput */
1028 s32 *tpt_tbl = tbl->expected_tpt;
1029
1030 s32 new_rate, high, low, start_hi;
Zhu Yib481de92007-09-25 17:54:57 -07001031 u16 high_low;
Guy Cohenf935a6d2008-04-23 17:15:02 -07001032 s8 rate = index;
Zhu Yib481de92007-09-25 17:54:57 -07001033
1034 new_rate = high = low = start_hi = IWL_RATE_INVALID;
1035
1036 for (; ;) {
Ester Kummerbf403db2008-05-05 10:22:40 +08001037 high_low = rs_get_adjacent_rate(priv, rate, rate_mask,
1038 tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -07001039
1040 low = high_low & 0xff;
1041 high = (high_low >> 8) & 0xff;
1042
Ben Cahill77626352007-11-29 11:09:44 +08001043 /*
1044 * Lower the "search" bit rate, to give new "search" mode
1045 * approximately the same throughput as "active" if:
1046 *
1047 * 1) "Active" mode has been working modestly well (but not
1048 * great), and expected "search" throughput (under perfect
1049 * conditions) at candidate rate is above the actual
1050 * measured "active" throughput (but less than expected
1051 * "active" throughput under perfect conditions).
1052 * OR
1053 * 2) "Active" mode has been working perfectly or very well
1054 * and expected "search" throughput (under perfect
1055 * conditions) at candidate rate is above expected
1056 * "active" throughput (under perfect conditions).
1057 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001058 if ((((100 * tpt_tbl[rate]) > lq_sta->last_tpt) &&
Zhu Yib481de92007-09-25 17:54:57 -07001059 ((active_sr > IWL_RATE_DECREASE_TH) &&
1060 (active_sr <= IWL_RATE_HIGH_TH) &&
1061 (tpt_tbl[rate] <= active_tpt))) ||
1062 ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
1063 (tpt_tbl[rate] > active_tpt))) {
1064
Ben Cahill77626352007-11-29 11:09:44 +08001065 /* (2nd or later pass)
1066 * If we've already tried to raise the rate, and are
1067 * now trying to lower it, use the higher rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001068 if (start_hi != IWL_RATE_INVALID) {
1069 new_rate = start_hi;
1070 break;
1071 }
Ben Cahill77626352007-11-29 11:09:44 +08001072
Zhu Yib481de92007-09-25 17:54:57 -07001073 new_rate = rate;
Ben Cahill77626352007-11-29 11:09:44 +08001074
1075 /* Loop again with lower rate */
Zhu Yib481de92007-09-25 17:54:57 -07001076 if (low != IWL_RATE_INVALID)
1077 rate = low;
Ben Cahill77626352007-11-29 11:09:44 +08001078
1079 /* Lower rate not available, use the original */
Zhu Yib481de92007-09-25 17:54:57 -07001080 else
1081 break;
Ben Cahill77626352007-11-29 11:09:44 +08001082
1083 /* Else try to raise the "search" rate to match "active" */
Zhu Yib481de92007-09-25 17:54:57 -07001084 } else {
Ben Cahill77626352007-11-29 11:09:44 +08001085 /* (2nd or later pass)
1086 * If we've already tried to lower the rate, and are
1087 * now trying to raise it, use the lower rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001088 if (new_rate != IWL_RATE_INVALID)
1089 break;
Ben Cahill77626352007-11-29 11:09:44 +08001090
1091 /* Loop again with higher rate */
Zhu Yib481de92007-09-25 17:54:57 -07001092 else if (high != IWL_RATE_INVALID) {
1093 start_hi = high;
1094 rate = high;
Ben Cahill77626352007-11-29 11:09:44 +08001095
1096 /* Higher rate not available, use the original */
Zhu Yib481de92007-09-25 17:54:57 -07001097 } else {
Guy Cohen90d77952008-09-09 10:54:53 +08001098 new_rate = rate;
Zhu Yib481de92007-09-25 17:54:57 -07001099 break;
1100 }
1101 }
1102 }
1103
1104 return new_rate;
1105}
Zhu Yib481de92007-09-25 17:54:57 -07001106
Ben Cahill77626352007-11-29 11:09:44 +08001107/*
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001108 * Set up search table for MIMO2
Ben Cahill77626352007-11-29 11:09:44 +08001109 */
Guy Cohenfde0db32008-04-21 15:42:01 -07001110static int rs_switch_to_mimo2(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001111 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001112 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001113 struct ieee80211_sta *sta,
Tomas Winklere227cea2008-07-18 13:53:05 +08001114 struct iwl_scale_tbl_info *tbl, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001115{
Zhu Yib481de92007-09-25 17:54:57 -07001116 u16 rate_mask;
1117 s32 rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001118 s8 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07001119
Luis R. Rodriguezde27e642008-12-23 15:58:44 -08001120 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
Zhu Yib481de92007-09-25 17:54:57 -07001121 return -1;
1122
Johannes Bergd9fe60d2008-10-09 12:13:49 +02001123 if (((sta->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 2)
Tomas Winkler00c5ae22008-09-03 11:26:42 +08001124 == WLAN_HT_CAP_SM_PS_STATIC)
Zhu Yib481de92007-09-25 17:54:57 -07001125 return -1;
1126
Ben Cahill77626352007-11-29 11:09:44 +08001127 /* Need both Tx chains/antennas to support MIMO */
Guy Cohenaade00c2008-04-23 17:14:59 -07001128 if (priv->hw_params.tx_chains_num < 2)
Zhu Yib481de92007-09-25 17:54:57 -07001129 return -1;
1130
Tomas Winklere1623442009-01-27 14:27:56 -08001131 IWL_DEBUG_RATE(priv, "LQ: try to switch to MIMO2\n");
Guy Cohen39e88502008-04-23 17:14:57 -07001132
1133 tbl->lq_type = LQ_MIMO2;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001134 tbl->is_dup = lq_sta->is_dup;
Zhu Yib481de92007-09-25 17:54:57 -07001135 tbl->action = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001136 tbl->max_search = IWL_MAX_SEARCH;
Guy Cohen39e88502008-04-23 17:14:57 -07001137 rate_mask = lq_sta->active_mimo2_rate;
1138
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001139 if (iwl_is_ht40_tx_allowed(priv, &sta->ht_cap))
1140 tbl->is_ht40 = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001141 else
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001142 tbl->is_ht40 = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001143
Guy Coheneecd6e52008-04-23 17:14:58 -07001144 rs_set_expected_tpt_table(lq_sta, tbl);
Zhu Yib481de92007-09-25 17:54:57 -07001145
Guy Cohenf935a6d2008-04-23 17:15:02 -07001146 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
Zhu Yib481de92007-09-25 17:54:57 -07001147
Tomas Winklere1623442009-01-27 14:27:56 -08001148 IWL_DEBUG_RATE(priv, "LQ: MIMO2 best rate %d mask %X\n", rate, rate_mask);
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001149 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1150 IWL_DEBUG_RATE(priv, "Can't switch with index %d rate mask %x\n",
1151 rate, rate_mask);
1152 return -1;
1153 }
1154 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
Guy Cohen39e88502008-04-23 17:14:57 -07001155
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001156 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
1157 tbl->current_rate, is_green);
1158 return 0;
1159}
1160
1161/*
1162 * Set up search table for MIMO3
1163 */
1164static int rs_switch_to_mimo3(struct iwl_priv *priv,
1165 struct iwl_lq_sta *lq_sta,
1166 struct ieee80211_conf *conf,
1167 struct ieee80211_sta *sta,
1168 struct iwl_scale_tbl_info *tbl, int index)
1169{
1170 u16 rate_mask;
1171 s32 rate;
1172 s8 is_green = lq_sta->is_green;
1173
1174 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
1175 return -1;
1176
1177 if (((sta->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 2)
1178 == WLAN_HT_CAP_SM_PS_STATIC)
1179 return -1;
1180
1181 /* Need both Tx chains/antennas to support MIMO */
1182 if (priv->hw_params.tx_chains_num < 3)
1183 return -1;
1184
1185 IWL_DEBUG_RATE(priv, "LQ: try to switch to MIMO3\n");
1186
1187 tbl->lq_type = LQ_MIMO3;
1188 tbl->is_dup = lq_sta->is_dup;
1189 tbl->action = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001190 tbl->max_search = IWL_MAX_11N_MIMO3_SEARCH;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001191 rate_mask = lq_sta->active_mimo3_rate;
1192
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001193 if (iwl_is_ht40_tx_allowed(priv, &sta->ht_cap))
1194 tbl->is_ht40 = 1;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001195 else
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001196 tbl->is_ht40 = 0;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001197
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001198 rs_set_expected_tpt_table(lq_sta, tbl);
1199
1200 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
1201
1202 IWL_DEBUG_RATE(priv, "LQ: MIMO3 best rate %d mask %X\n",
1203 rate, rate_mask);
Guy Cohen39e88502008-04-23 17:14:57 -07001204 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
Tomas Winklere1623442009-01-27 14:27:56 -08001205 IWL_DEBUG_RATE(priv, "Can't switch with index %d rate mask %x\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001206 rate, rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07001207 return -1;
Guy Cohen39e88502008-04-23 17:14:57 -07001208 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001209 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
Zhu Yib481de92007-09-25 17:54:57 -07001210
Tomas Winklere1623442009-01-27 14:27:56 -08001211 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001212 tbl->current_rate, is_green);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001213 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07001214}
1215
Ben Cahill77626352007-11-29 11:09:44 +08001216/*
1217 * Set up search table for SISO
1218 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001219static int rs_switch_to_siso(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001220 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001221 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001222 struct ieee80211_sta *sta,
Tomas Winklere227cea2008-07-18 13:53:05 +08001223 struct iwl_scale_tbl_info *tbl, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001224{
Zhu Yib481de92007-09-25 17:54:57 -07001225 u16 rate_mask;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001226 u8 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07001227 s32 rate;
1228
Luis R. Rodriguezde27e642008-12-23 15:58:44 -08001229 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
Zhu Yib481de92007-09-25 17:54:57 -07001230 return -1;
1231
Tomas Winklere1623442009-01-27 14:27:56 -08001232 IWL_DEBUG_RATE(priv, "LQ: try to switch to SISO\n");
Guy Cohen39e88502008-04-23 17:14:57 -07001233
Tomas Winklerc33104f2008-01-14 17:46:21 -08001234 tbl->is_dup = lq_sta->is_dup;
Zhu Yib481de92007-09-25 17:54:57 -07001235 tbl->lq_type = LQ_SISO;
1236 tbl->action = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001237 tbl->max_search = IWL_MAX_SEARCH;
Guy Cohen39e88502008-04-23 17:14:57 -07001238 rate_mask = lq_sta->active_siso_rate;
Zhu Yib481de92007-09-25 17:54:57 -07001239
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001240 if (iwl_is_ht40_tx_allowed(priv, &sta->ht_cap))
1241 tbl->is_ht40 = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001242 else
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001243 tbl->is_ht40 = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001244
Zhu Yib481de92007-09-25 17:54:57 -07001245 if (is_green)
Guy Cohen39e88502008-04-23 17:14:57 -07001246 tbl->is_SGI = 0; /*11n spec: no SGI in SISO+Greenfield*/
Zhu Yib481de92007-09-25 17:54:57 -07001247
Guy Coheneecd6e52008-04-23 17:14:58 -07001248 rs_set_expected_tpt_table(lq_sta, tbl);
Guy Cohenf935a6d2008-04-23 17:15:02 -07001249 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
Zhu Yib481de92007-09-25 17:54:57 -07001250
Tomas Winklere1623442009-01-27 14:27:56 -08001251 IWL_DEBUG_RATE(priv, "LQ: get best rate %d mask %X\n", rate, rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07001252 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
Tomas Winklere1623442009-01-27 14:27:56 -08001253 IWL_DEBUG_RATE(priv, "can not switch with index %d rate mask %x\n",
Zhu Yib481de92007-09-25 17:54:57 -07001254 rate, rate_mask);
1255 return -1;
1256 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001257 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
Tomas Winklere1623442009-01-27 14:27:56 -08001258 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001259 tbl->current_rate, is_green);
Mohamed Abbas403ab562007-11-06 22:06:25 -08001260 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07001261}
1262
Ben Cahill77626352007-11-29 11:09:44 +08001263/*
1264 * Try to switch to new modulation mode from legacy
1265 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001266static int rs_move_legacy_other(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001267 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001268 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001269 struct ieee80211_sta *sta,
Zhu Yib481de92007-09-25 17:54:57 -07001270 int index)
1271{
Tomas Winklere227cea2008-07-18 13:53:05 +08001272 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1273 struct iwl_scale_tbl_info *search_tbl =
1274 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1275 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1276 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1277 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001278 u8 start_action = tbl->action;
Guy Cohenfde0db32008-04-21 15:42:01 -07001279 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
Guy Cohen3110bef2008-09-09 10:54:54 +08001280 u8 tx_chains_num = priv->hw_params.tx_chains_num;
Guy Cohenfde0db32008-04-21 15:42:01 -07001281 int ret = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001282 u8 update_search_tbl_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001283
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001284 if (!iwl_ht_enabled(priv))
1285 /* stay in Legacy */
1286 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
Johannes Berg3ad3b922009-08-07 15:41:48 -07001287 else if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001288 tbl->action > IWL_LEGACY_SWITCH_SISO)
1289 tbl->action = IWL_LEGACY_SWITCH_SISO;
Zhu Yib481de92007-09-25 17:54:57 -07001290 for (; ;) {
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001291 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001292 switch (tbl->action) {
Guy Cohen3110bef2008-09-09 10:54:54 +08001293 case IWL_LEGACY_SWITCH_ANTENNA1:
1294 case IWL_LEGACY_SWITCH_ANTENNA2:
Tomas Winklere1623442009-01-27 14:27:56 -08001295 IWL_DEBUG_RATE(priv, "LQ: Legacy toggle Antenna\n");
Zhu Yib481de92007-09-25 17:54:57 -07001296
Guy Cohen3110bef2008-09-09 10:54:54 +08001297 if ((tbl->action == IWL_LEGACY_SWITCH_ANTENNA1 &&
1298 tx_chains_num <= 1) ||
1299 (tbl->action == IWL_LEGACY_SWITCH_ANTENNA2 &&
1300 tx_chains_num <= 2))
1301 break;
1302
Ben Cahill77626352007-11-29 11:09:44 +08001303 /* Don't change antenna if success has been great */
Zhu Yib481de92007-09-25 17:54:57 -07001304 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1305 break;
Ben Cahill77626352007-11-29 11:09:44 +08001306
Ben Cahill77626352007-11-29 11:09:44 +08001307 /* Set up search table to try other antenna */
Zhu Yib481de92007-09-25 17:54:57 -07001308 memcpy(search_tbl, tbl, sz);
1309
Guy Cohenaade00c2008-04-23 17:14:59 -07001310 if (rs_toggle_antenna(valid_tx_ant,
1311 &search_tbl->current_rate, search_tbl)) {
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001312 update_search_tbl_counter = 1;
Guy Cohen3110bef2008-09-09 10:54:54 +08001313 rs_set_expected_tpt_table(lq_sta, search_tbl);
Guy Cohenaade00c2008-04-23 17:14:59 -07001314 goto out;
1315 }
Guy Cohena5e8b502008-05-29 16:35:11 +08001316 break;
Zhu Yib481de92007-09-25 17:54:57 -07001317 case IWL_LEGACY_SWITCH_SISO:
Tomas Winklere1623442009-01-27 14:27:56 -08001318 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to SISO\n");
Ben Cahill77626352007-11-29 11:09:44 +08001319
1320 /* Set up search table to try SISO */
Zhu Yib481de92007-09-25 17:54:57 -07001321 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001322 search_tbl->is_SGI = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001323 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001324 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001325 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001326 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001327 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001328 }
Zhu Yib481de92007-09-25 17:54:57 -07001329
1330 break;
Guy Cohen3110bef2008-09-09 10:54:54 +08001331 case IWL_LEGACY_SWITCH_MIMO2_AB:
1332 case IWL_LEGACY_SWITCH_MIMO2_AC:
1333 case IWL_LEGACY_SWITCH_MIMO2_BC:
Tomas Winklere1623442009-01-27 14:27:56 -08001334 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to MIMO2\n");
Ben Cahill77626352007-11-29 11:09:44 +08001335
1336 /* Set up search table to try MIMO */
Zhu Yib481de92007-09-25 17:54:57 -07001337 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001338 search_tbl->is_SGI = 0;
Guy Cohen3110bef2008-09-09 10:54:54 +08001339
1340 if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AB)
1341 search_tbl->ant_type = ANT_AB;
1342 else if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AC)
1343 search_tbl->ant_type = ANT_AC;
1344 else
1345 search_tbl->ant_type = ANT_BC;
1346
1347 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1348 break;
1349
Guy Cohenfde0db32008-04-21 15:42:01 -07001350 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001351 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001352 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001353 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001354 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001355 }
Zhu Yib481de92007-09-25 17:54:57 -07001356 break;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001357
1358 case IWL_LEGACY_SWITCH_MIMO3_ABC:
1359 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to MIMO3\n");
1360
1361 /* Set up search table to try MIMO3 */
1362 memcpy(search_tbl, tbl, sz);
1363 search_tbl->is_SGI = 0;
1364
1365 search_tbl->ant_type = ANT_ABC;
1366
1367 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1368 break;
1369
1370 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1371 search_tbl, index);
1372 if (!ret) {
1373 lq_sta->action_counter = 0;
1374 goto out;
1375 }
1376 break;
Zhu Yib481de92007-09-25 17:54:57 -07001377 }
1378 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001379 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001380 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
Zhu Yib481de92007-09-25 17:54:57 -07001381
1382 if (tbl->action == start_action)
1383 break;
1384
1385 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001386 search_tbl->lq_type = LQ_NONE;
Zhu Yib481de92007-09-25 17:54:57 -07001387 return 0;
1388
Guy Cohen3110bef2008-09-09 10:54:54 +08001389out:
1390 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001391 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001392 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001393 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001394 if (update_search_tbl_counter)
1395 search_tbl->action = tbl->action;
Zhu Yib481de92007-09-25 17:54:57 -07001396 return 0;
1397
1398}
1399
Ben Cahill77626352007-11-29 11:09:44 +08001400/*
1401 * Try to switch to new modulation mode from SISO
1402 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001403static int rs_move_siso_to_other(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001404 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001405 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001406 struct ieee80211_sta *sta, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001407{
Tomas Winklerc33104f2008-01-14 17:46:21 -08001408 u8 is_green = lq_sta->is_green;
Tomas Winklere227cea2008-07-18 13:53:05 +08001409 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1410 struct iwl_scale_tbl_info *search_tbl =
1411 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1412 struct iwl_rate_scale_data *window = &(tbl->win[index]);
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001413 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Tomas Winklere227cea2008-07-18 13:53:05 +08001414 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1415 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001416 u8 start_action = tbl->action;
Guy Cohenfde0db32008-04-21 15:42:01 -07001417 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
Guy Cohen3110bef2008-09-09 10:54:54 +08001418 u8 tx_chains_num = priv->hw_params.tx_chains_num;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001419 u8 update_search_tbl_counter = 0;
Guy Cohenfde0db32008-04-21 15:42:01 -07001420 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001421
Johannes Berg3ad3b922009-08-07 15:41:48 -07001422 if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001423 tbl->action > IWL_SISO_SWITCH_ANTENNA2) {
1424 /* stay in SISO */
1425 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1426 }
Zhu Yib481de92007-09-25 17:54:57 -07001427 for (;;) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001428 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001429 switch (tbl->action) {
Guy Cohen3110bef2008-09-09 10:54:54 +08001430 case IWL_SISO_SWITCH_ANTENNA1:
1431 case IWL_SISO_SWITCH_ANTENNA2:
Tomas Winklere1623442009-01-27 14:27:56 -08001432 IWL_DEBUG_RATE(priv, "LQ: SISO toggle Antenna\n");
Guy Cohen3110bef2008-09-09 10:54:54 +08001433
1434 if ((tbl->action == IWL_SISO_SWITCH_ANTENNA1 &&
1435 tx_chains_num <= 1) ||
1436 (tbl->action == IWL_SISO_SWITCH_ANTENNA2 &&
1437 tx_chains_num <= 2))
1438 break;
1439
Zhu Yib481de92007-09-25 17:54:57 -07001440 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1441 break;
Zhu Yib481de92007-09-25 17:54:57 -07001442
1443 memcpy(search_tbl, tbl, sz);
Guy Cohenaade00c2008-04-23 17:14:59 -07001444 if (rs_toggle_antenna(valid_tx_ant,
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001445 &search_tbl->current_rate, search_tbl)) {
1446 update_search_tbl_counter = 1;
Guy Cohenaade00c2008-04-23 17:14:59 -07001447 goto out;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001448 }
Guy Cohena5e8b502008-05-29 16:35:11 +08001449 break;
Guy Cohen3110bef2008-09-09 10:54:54 +08001450 case IWL_SISO_SWITCH_MIMO2_AB:
1451 case IWL_SISO_SWITCH_MIMO2_AC:
1452 case IWL_SISO_SWITCH_MIMO2_BC:
Tomas Winklere1623442009-01-27 14:27:56 -08001453 IWL_DEBUG_RATE(priv, "LQ: SISO switch to MIMO2\n");
Zhu Yib481de92007-09-25 17:54:57 -07001454 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001455 search_tbl->is_SGI = 0;
Guy Cohen3110bef2008-09-09 10:54:54 +08001456
1457 if (tbl->action == IWL_SISO_SWITCH_MIMO2_AB)
1458 search_tbl->ant_type = ANT_AB;
1459 else if (tbl->action == IWL_SISO_SWITCH_MIMO2_AC)
1460 search_tbl->ant_type = ANT_AC;
1461 else
1462 search_tbl->ant_type = ANT_BC;
1463
1464 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1465 break;
1466
Guy Cohenfde0db32008-04-21 15:42:01 -07001467 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001468 search_tbl, index);
Guy Cohen3110bef2008-09-09 10:54:54 +08001469 if (!ret)
Zhu Yib481de92007-09-25 17:54:57 -07001470 goto out;
1471 break;
1472 case IWL_SISO_SWITCH_GI:
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001473 if (!tbl->is_ht40 && !(ht_cap->cap &
1474 IEEE80211_HT_CAP_SGI_20))
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001475 break;
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001476 if (tbl->is_ht40 && !(ht_cap->cap &
1477 IEEE80211_HT_CAP_SGI_40))
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001478 break;
1479
Tomas Winklere1623442009-01-27 14:27:56 -08001480 IWL_DEBUG_RATE(priv, "LQ: SISO toggle SGI/NGI\n");
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001481
Zhu Yib481de92007-09-25 17:54:57 -07001482 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001483 if (is_green) {
1484 if (!tbl->is_SGI)
1485 break;
1486 else
Winkler, Tomas15b16872008-12-19 10:37:33 +08001487 IWL_ERR(priv,
1488 "SGI was set in GF+SISO\n");
Zhu Yib481de92007-09-25 17:54:57 -07001489 }
Guy Cohen39e88502008-04-23 17:14:57 -07001490 search_tbl->is_SGI = !tbl->is_SGI;
Guy Coheneecd6e52008-04-23 17:14:58 -07001491 rs_set_expected_tpt_table(lq_sta, search_tbl);
Guy Cohen39e88502008-04-23 17:14:57 -07001492 if (tbl->is_SGI) {
1493 s32 tpt = lq_sta->last_tpt / 100;
1494 if (tpt >= search_tbl->expected_tpt[index])
1495 break;
1496 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001497 search_tbl->current_rate =
1498 rate_n_flags_from_tbl(priv, search_tbl,
1499 index, is_green);
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001500 update_search_tbl_counter = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001501 goto out;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001502 case IWL_SISO_SWITCH_MIMO3_ABC:
1503 IWL_DEBUG_RATE(priv, "LQ: SISO switch to MIMO3\n");
1504 memcpy(search_tbl, tbl, sz);
1505 search_tbl->is_SGI = 0;
1506 search_tbl->ant_type = ANT_ABC;
1507
1508 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1509 break;
1510
1511 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1512 search_tbl, index);
1513 if (!ret)
1514 goto out;
1515 break;
Zhu Yib481de92007-09-25 17:54:57 -07001516 }
1517 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001518 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001519 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
Zhu Yib481de92007-09-25 17:54:57 -07001520
1521 if (tbl->action == start_action)
1522 break;
1523 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001524 search_tbl->lq_type = LQ_NONE;
Zhu Yib481de92007-09-25 17:54:57 -07001525 return 0;
1526
1527 out:
Guy Cohen3110bef2008-09-09 10:54:54 +08001528 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001529 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001530 if (tbl->action > IWL_SISO_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001531 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001532 if (update_search_tbl_counter)
1533 search_tbl->action = tbl->action;
1534
Zhu Yib481de92007-09-25 17:54:57 -07001535 return 0;
1536}
1537
Ben Cahill77626352007-11-29 11:09:44 +08001538/*
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001539 * Try to switch to new modulation mode from MIMO2
Ben Cahill77626352007-11-29 11:09:44 +08001540 */
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001541static int rs_move_mimo2_to_other(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001542 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001543 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001544 struct ieee80211_sta *sta, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001545{
Tomas Winklerc33104f2008-01-14 17:46:21 -08001546 s8 is_green = lq_sta->is_green;
Tomas Winklere227cea2008-07-18 13:53:05 +08001547 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1548 struct iwl_scale_tbl_info *search_tbl =
1549 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Guy Cohen3110bef2008-09-09 10:54:54 +08001550 struct iwl_rate_scale_data *window = &(tbl->win[index]);
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001551 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Tomas Winklere227cea2008-07-18 13:53:05 +08001552 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1553 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001554 u8 start_action = tbl->action;
Guy Cohen3110bef2008-09-09 10:54:54 +08001555 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1556 u8 tx_chains_num = priv->hw_params.tx_chains_num;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001557 u8 update_search_tbl_counter = 0;
Guy Cohenaade00c2008-04-23 17:14:59 -07001558 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001559
Johannes Berg3ad3b922009-08-07 15:41:48 -07001560 if ((iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE) &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001561 (tbl->action < IWL_MIMO2_SWITCH_SISO_A ||
1562 tbl->action > IWL_MIMO2_SWITCH_SISO_C)) {
1563 /* switch in SISO */
1564 tbl->action = IWL_MIMO2_SWITCH_SISO_A;
1565 }
Zhu Yib481de92007-09-25 17:54:57 -07001566 for (;;) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001567 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001568 switch (tbl->action) {
Guy Cohen3110bef2008-09-09 10:54:54 +08001569 case IWL_MIMO2_SWITCH_ANTENNA1:
1570 case IWL_MIMO2_SWITCH_ANTENNA2:
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001571 IWL_DEBUG_RATE(priv, "LQ: MIMO2 toggle Antennas\n");
Guy Cohen3110bef2008-09-09 10:54:54 +08001572
1573 if (tx_chains_num <= 2)
1574 break;
1575
1576 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1577 break;
1578
1579 memcpy(search_tbl, tbl, sz);
1580 if (rs_toggle_antenna(valid_tx_ant,
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001581 &search_tbl->current_rate, search_tbl)) {
1582 update_search_tbl_counter = 1;
Guy Cohen3110bef2008-09-09 10:54:54 +08001583 goto out;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001584 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001585 break;
1586 case IWL_MIMO2_SWITCH_SISO_A:
1587 case IWL_MIMO2_SWITCH_SISO_B:
1588 case IWL_MIMO2_SWITCH_SISO_C:
Tomas Winklere1623442009-01-27 14:27:56 -08001589 IWL_DEBUG_RATE(priv, "LQ: MIMO2 switch to SISO\n");
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001590
Ben Cahill77626352007-11-29 11:09:44 +08001591 /* Set up new search table for SISO */
Zhu Yib481de92007-09-25 17:54:57 -07001592 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001593
Guy Cohen3110bef2008-09-09 10:54:54 +08001594 if (tbl->action == IWL_MIMO2_SWITCH_SISO_A)
Guy Cohenfde0db32008-04-21 15:42:01 -07001595 search_tbl->ant_type = ANT_A;
Guy Cohen3110bef2008-09-09 10:54:54 +08001596 else if (tbl->action == IWL_MIMO2_SWITCH_SISO_B)
Guy Cohenfde0db32008-04-21 15:42:01 -07001597 search_tbl->ant_type = ANT_B;
Guy Cohen3110bef2008-09-09 10:54:54 +08001598 else
1599 search_tbl->ant_type = ANT_C;
1600
1601 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1602 break;
Zhu Yib481de92007-09-25 17:54:57 -07001603
Tomas Winklerc33104f2008-01-14 17:46:21 -08001604 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001605 search_tbl, index);
Guy Cohen3110bef2008-09-09 10:54:54 +08001606 if (!ret)
Zhu Yib481de92007-09-25 17:54:57 -07001607 goto out;
Guy Cohen3110bef2008-09-09 10:54:54 +08001608
Zhu Yib481de92007-09-25 17:54:57 -07001609 break;
1610
Guy Cohen3110bef2008-09-09 10:54:54 +08001611 case IWL_MIMO2_SWITCH_GI:
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001612 if (!tbl->is_ht40 && !(ht_cap->cap &
1613 IEEE80211_HT_CAP_SGI_20))
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001614 break;
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001615 if (tbl->is_ht40 && !(ht_cap->cap &
1616 IEEE80211_HT_CAP_SGI_40))
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001617 break;
1618
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001619 IWL_DEBUG_RATE(priv, "LQ: MIMO2 toggle SGI/NGI\n");
1620
1621 /* Set up new search table for MIMO2 */
1622 memcpy(search_tbl, tbl, sz);
1623 search_tbl->is_SGI = !tbl->is_SGI;
1624 rs_set_expected_tpt_table(lq_sta, search_tbl);
1625 /*
1626 * If active table already uses the fastest possible
1627 * modulation (dual stream with short guard interval),
1628 * and it's working well, there's no need to look
1629 * for a better type of modulation!
1630 */
1631 if (tbl->is_SGI) {
1632 s32 tpt = lq_sta->last_tpt / 100;
1633 if (tpt >= search_tbl->expected_tpt[index])
1634 break;
1635 }
1636 search_tbl->current_rate =
1637 rate_n_flags_from_tbl(priv, search_tbl,
1638 index, is_green);
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001639 update_search_tbl_counter = 1;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001640 goto out;
1641
1642 case IWL_MIMO2_SWITCH_MIMO3_ABC:
1643 IWL_DEBUG_RATE(priv, "LQ: MIMO2 switch to MIMO3\n");
1644 memcpy(search_tbl, tbl, sz);
1645 search_tbl->is_SGI = 0;
1646 search_tbl->ant_type = ANT_ABC;
1647
1648 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1649 break;
1650
1651 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1652 search_tbl, index);
1653 if (!ret)
1654 goto out;
1655
1656 break;
1657 }
1658 tbl->action++;
1659 if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
1660 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
1661
1662 if (tbl->action == start_action)
1663 break;
1664 }
1665 search_tbl->lq_type = LQ_NONE;
1666 return 0;
1667 out:
1668 lq_sta->search_better_tbl = 1;
1669 tbl->action++;
1670 if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
1671 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001672 if (update_search_tbl_counter)
1673 search_tbl->action = tbl->action;
1674
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001675 return 0;
1676
1677}
1678
1679/*
1680 * Try to switch to new modulation mode from MIMO3
1681 */
1682static int rs_move_mimo3_to_other(struct iwl_priv *priv,
1683 struct iwl_lq_sta *lq_sta,
1684 struct ieee80211_conf *conf,
1685 struct ieee80211_sta *sta, int index)
1686{
1687 s8 is_green = lq_sta->is_green;
1688 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1689 struct iwl_scale_tbl_info *search_tbl =
1690 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1691 struct iwl_rate_scale_data *window = &(tbl->win[index]);
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001692 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001693 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1694 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1695 u8 start_action = tbl->action;
1696 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1697 u8 tx_chains_num = priv->hw_params.tx_chains_num;
1698 int ret;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001699 u8 update_search_tbl_counter = 0;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001700
Johannes Berg3ad3b922009-08-07 15:41:48 -07001701 if ((iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE) &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001702 (tbl->action < IWL_MIMO3_SWITCH_SISO_A ||
1703 tbl->action > IWL_MIMO3_SWITCH_SISO_C)) {
1704 /* switch in SISO */
1705 tbl->action = IWL_MIMO3_SWITCH_SISO_A;
1706 }
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001707 for (;;) {
1708 lq_sta->action_counter++;
1709 switch (tbl->action) {
1710 case IWL_MIMO3_SWITCH_ANTENNA1:
1711 case IWL_MIMO3_SWITCH_ANTENNA2:
1712 IWL_DEBUG_RATE(priv, "LQ: MIMO3 toggle Antennas\n");
1713
1714 if (tx_chains_num <= 3)
1715 break;
1716
1717 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1718 break;
1719
1720 memcpy(search_tbl, tbl, sz);
1721 if (rs_toggle_antenna(valid_tx_ant,
1722 &search_tbl->current_rate, search_tbl))
1723 goto out;
1724 break;
1725 case IWL_MIMO3_SWITCH_SISO_A:
1726 case IWL_MIMO3_SWITCH_SISO_B:
1727 case IWL_MIMO3_SWITCH_SISO_C:
1728 IWL_DEBUG_RATE(priv, "LQ: MIMO3 switch to SISO\n");
1729
1730 /* Set up new search table for SISO */
1731 memcpy(search_tbl, tbl, sz);
1732
1733 if (tbl->action == IWL_MIMO3_SWITCH_SISO_A)
1734 search_tbl->ant_type = ANT_A;
1735 else if (tbl->action == IWL_MIMO3_SWITCH_SISO_B)
1736 search_tbl->ant_type = ANT_B;
1737 else
1738 search_tbl->ant_type = ANT_C;
1739
1740 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1741 break;
1742
1743 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
1744 search_tbl, index);
1745 if (!ret)
1746 goto out;
1747
1748 break;
1749
1750 case IWL_MIMO3_SWITCH_MIMO2_AB:
1751 case IWL_MIMO3_SWITCH_MIMO2_AC:
1752 case IWL_MIMO3_SWITCH_MIMO2_BC:
1753 IWL_DEBUG_RATE(priv, "LQ: MIMO3 switch to MIMO2\n");
1754
1755 memcpy(search_tbl, tbl, sz);
1756 search_tbl->is_SGI = 0;
1757 if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AB)
1758 search_tbl->ant_type = ANT_AB;
1759 else if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AC)
1760 search_tbl->ant_type = ANT_AC;
1761 else
1762 search_tbl->ant_type = ANT_BC;
1763
1764 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1765 break;
1766
1767 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
1768 search_tbl, index);
1769 if (!ret)
1770 goto out;
1771
1772 break;
1773
1774 case IWL_MIMO3_SWITCH_GI:
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001775 if (!tbl->is_ht40 && !(ht_cap->cap &
1776 IEEE80211_HT_CAP_SGI_20))
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001777 break;
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001778 if (tbl->is_ht40 && !(ht_cap->cap &
1779 IEEE80211_HT_CAP_SGI_40))
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001780 break;
1781
1782 IWL_DEBUG_RATE(priv, "LQ: MIMO3 toggle SGI/NGI\n");
Ben Cahill77626352007-11-29 11:09:44 +08001783
1784 /* Set up new search table for MIMO */
Zhu Yib481de92007-09-25 17:54:57 -07001785 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001786 search_tbl->is_SGI = !tbl->is_SGI;
Guy Coheneecd6e52008-04-23 17:14:58 -07001787 rs_set_expected_tpt_table(lq_sta, search_tbl);
Ben Cahill77626352007-11-29 11:09:44 +08001788 /*
1789 * If active table already uses the fastest possible
1790 * modulation (dual stream with short guard interval),
1791 * and it's working well, there's no need to look
1792 * for a better type of modulation!
1793 */
Guy Cohen39e88502008-04-23 17:14:57 -07001794 if (tbl->is_SGI) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001795 s32 tpt = lq_sta->last_tpt / 100;
Guy Cohen39e88502008-04-23 17:14:57 -07001796 if (tpt >= search_tbl->expected_tpt[index])
1797 break;
Zhu Yib481de92007-09-25 17:54:57 -07001798 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001799 search_tbl->current_rate =
1800 rate_n_flags_from_tbl(priv, search_tbl,
1801 index, is_green);
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001802 update_search_tbl_counter = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001803 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07001804 }
1805 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001806 if (tbl->action > IWL_MIMO3_SWITCH_GI)
1807 tbl->action = IWL_MIMO3_SWITCH_ANTENNA1;
Zhu Yib481de92007-09-25 17:54:57 -07001808
1809 if (tbl->action == start_action)
1810 break;
1811 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001812 search_tbl->lq_type = LQ_NONE;
Zhu Yib481de92007-09-25 17:54:57 -07001813 return 0;
1814 out:
Guy Cohen3110bef2008-09-09 10:54:54 +08001815 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001816 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001817 if (tbl->action > IWL_MIMO3_SWITCH_GI)
1818 tbl->action = IWL_MIMO3_SWITCH_ANTENNA1;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001819 if (update_search_tbl_counter)
1820 search_tbl->action = tbl->action;
1821
Zhu Yib481de92007-09-25 17:54:57 -07001822 return 0;
1823
1824}
1825
Ben Cahill77626352007-11-29 11:09:44 +08001826/*
1827 * Check whether we should continue using same modulation mode, or
1828 * begin search for a new mode, based on:
1829 * 1) # tx successes or failures while using this mode
1830 * 2) # times calling this function
1831 * 3) elapsed time in this mode (not used, for now)
1832 */
Tomas Winklere227cea2008-07-18 13:53:05 +08001833static void rs_stay_in_table(struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07001834{
Tomas Winklere227cea2008-07-18 13:53:05 +08001835 struct iwl_scale_tbl_info *tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001836 int i;
1837 int active_tbl;
1838 int flush_interval_passed = 0;
Ester Kummerbf403db2008-05-05 10:22:40 +08001839 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07001840
Ester Kummerbf403db2008-05-05 10:22:40 +08001841 priv = lq_sta->drv;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001842 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001843
Tomas Winklerc33104f2008-01-14 17:46:21 -08001844 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07001845
Ben Cahill77626352007-11-29 11:09:44 +08001846 /* If we've been disallowing search, see if we should now allow it */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001847 if (lq_sta->stay_in_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07001848
Ben Cahill77626352007-11-29 11:09:44 +08001849 /* Elapsed time using current modulation mode */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001850 if (lq_sta->flush_timer)
Zhu Yib481de92007-09-25 17:54:57 -07001851 flush_interval_passed =
Mohamed Abbas447fee72009-04-20 14:37:02 -07001852 time_after(jiffies,
1853 (unsigned long)(lq_sta->flush_timer +
Zhu Yib481de92007-09-25 17:54:57 -07001854 IWL_RATE_SCALE_FLUSH_INTVL));
1855
Ben Cahill77626352007-11-29 11:09:44 +08001856 /*
1857 * Check if we should allow search for new modulation mode.
1858 * If many frames have failed or succeeded, or we've used
1859 * this same modulation for a long time, allow search, and
1860 * reset history stats that keep track of whether we should
1861 * allow a new search. Also (below) reset all bitmaps and
1862 * stats in active history.
1863 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001864 if ((lq_sta->total_failed > lq_sta->max_failure_limit) ||
1865 (lq_sta->total_success > lq_sta->max_success_limit) ||
1866 ((!lq_sta->search_better_tbl) && (lq_sta->flush_timer)
Zhu Yib481de92007-09-25 17:54:57 -07001867 && (flush_interval_passed))) {
Tomas Winklere1623442009-01-27 14:27:56 -08001868 IWL_DEBUG_RATE(priv, "LQ: stay is expired %d %d %d\n:",
Tomas Winklerc33104f2008-01-14 17:46:21 -08001869 lq_sta->total_failed,
1870 lq_sta->total_success,
Zhu Yib481de92007-09-25 17:54:57 -07001871 flush_interval_passed);
Ben Cahill77626352007-11-29 11:09:44 +08001872
1873 /* Allow search for new mode */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001874 lq_sta->stay_in_tbl = 0; /* only place reset */
1875 lq_sta->total_failed = 0;
1876 lq_sta->total_success = 0;
1877 lq_sta->flush_timer = 0;
Ben Cahill77626352007-11-29 11:09:44 +08001878
1879 /*
1880 * Else if we've used this modulation mode enough repetitions
1881 * (regardless of elapsed time or success/failure), reset
1882 * history bitmaps and rate-specific stats for all rates in
1883 * active table.
1884 */
Mohamed Abbas403ab562007-11-06 22:06:25 -08001885 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001886 lq_sta->table_count++;
1887 if (lq_sta->table_count >=
1888 lq_sta->table_count_limit) {
1889 lq_sta->table_count = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001890
Tomas Winklere1623442009-01-27 14:27:56 -08001891 IWL_DEBUG_RATE(priv, "LQ: stay in table clear win\n");
Zhu Yib481de92007-09-25 17:54:57 -07001892 for (i = 0; i < IWL_RATE_COUNT; i++)
1893 rs_rate_scale_clear_window(
1894 &(tbl->win[i]));
1895 }
1896 }
1897
Ben Cahill77626352007-11-29 11:09:44 +08001898 /* If transitioning to allow "search", reset all history
1899 * bitmaps and stats in active table (this will become the new
1900 * "search" table). */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001901 if (!lq_sta->stay_in_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07001902 for (i = 0; i < IWL_RATE_COUNT; i++)
1903 rs_rate_scale_clear_window(&(tbl->win[i]));
1904 }
1905 }
1906}
1907
Ben Cahill77626352007-11-29 11:09:44 +08001908/*
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07001909 * setup rate table in uCode
1910 * return rate_n_flags as used in the table
1911 */
1912static u32 rs_update_rate_tbl(struct iwl_priv *priv,
1913 struct iwl_lq_sta *lq_sta,
1914 struct iwl_scale_tbl_info *tbl,
1915 int index, u8 is_green)
1916{
1917 u32 rate;
1918
1919 /* Update uCode's rate table. */
1920 rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
1921 rs_fill_link_cmd(priv, lq_sta, rate);
1922 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
1923
1924 return rate;
1925}
1926
1927/*
Ben Cahill77626352007-11-29 11:09:44 +08001928 * Do rate scaling and search for new modulation mode.
1929 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001930static void rs_rate_scale_perform(struct iwl_priv *priv,
Gábor Stefanik514d65c2009-04-23 19:36:08 +02001931 struct sk_buff *skb,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001932 struct ieee80211_sta *sta,
1933 struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07001934{
Johannes Berg4b7679a2008-09-18 18:14:18 +02001935 struct ieee80211_hw *hw = priv->hw;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001936 struct ieee80211_conf *conf = &hw->conf;
Gábor Stefanik514d65c2009-04-23 19:36:08 +02001937 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1938 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001939 int low = IWL_RATE_INVALID;
1940 int high = IWL_RATE_INVALID;
1941 int index;
1942 int i;
Tomas Winklere227cea2008-07-18 13:53:05 +08001943 struct iwl_rate_scale_data *window = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001944 int current_tpt = IWL_INVALID_VALUE;
1945 int low_tpt = IWL_INVALID_VALUE;
1946 int high_tpt = IWL_INVALID_VALUE;
1947 u32 fail_count;
1948 s8 scale_action = 0;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07001949 u16 rate_mask;
Zhu Yib481de92007-09-25 17:54:57 -07001950 u8 update_lq = 0;
Tomas Winklere227cea2008-07-18 13:53:05 +08001951 struct iwl_scale_tbl_info *tbl, *tbl1;
Zhu Yib481de92007-09-25 17:54:57 -07001952 u16 rate_scale_index_msk = 0;
Guy Cohen39e88502008-04-23 17:14:57 -07001953 u32 rate;
Zhu Yib481de92007-09-25 17:54:57 -07001954 u8 is_green = 0;
1955 u8 active_tbl = 0;
1956 u8 done_search = 0;
1957 u16 high_low;
Guy Cohena5e8b502008-05-29 16:35:11 +08001958 s32 sr;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001959 u8 tid = MAX_TID_COUNT;
Wey-Yi Guy86b47662009-03-10 14:35:09 -07001960 struct iwl_tid_data *tid_data;
Zhu Yib481de92007-09-25 17:54:57 -07001961
Tomas Winklere1623442009-01-27 14:27:56 -08001962 IWL_DEBUG_RATE(priv, "rate scale calculate new rate for skb\n");
Zhu Yib481de92007-09-25 17:54:57 -07001963
Gábor Stefanik514d65c2009-04-23 19:36:08 +02001964 /* Send management frames and NO_ACK data using lowest rate. */
Tomas Winkler417f1142008-11-12 13:14:06 -08001965 /* TODO: this could probably be improved.. */
1966 if (!ieee80211_is_data(hdr->frame_control) ||
Gábor Stefanik514d65c2009-04-23 19:36:08 +02001967 info->flags & IEEE80211_TX_CTL_NO_ACK)
Zhu Yib481de92007-09-25 17:54:57 -07001968 return;
Zhu Yib481de92007-09-25 17:54:57 -07001969
Johannes Berg4b7679a2008-09-18 18:14:18 +02001970 if (!sta || !lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07001971 return;
1972
Johannes Berg4b7679a2008-09-18 18:14:18 +02001973 lq_sta->supp_rates = sta->supp_rates[lq_sta->band];
Zhu Yib481de92007-09-25 17:54:57 -07001974
Ron Rindjunskyfaa29712008-06-12 09:46:58 +08001975 tid = rs_tl_add_packet(lq_sta, hdr);
Daniel C Halperine3949d62009-09-17 10:43:50 -07001976 if ((tid != MAX_TID_COUNT) && (lq_sta->tx_agg_tid_en & (1 << tid))) {
1977 tid_data = &priv->stations[lq_sta->lq.sta_id].tid[tid];
1978 if (tid_data->agg.state == IWL_AGG_OFF)
1979 lq_sta->is_agg = 0;
1980 else
1981 lq_sta->is_agg = 1;
1982 } else
1983 lq_sta->is_agg = 0;
Ron Rindjunskyfaa29712008-06-12 09:46:58 +08001984
Ben Cahill77626352007-11-29 11:09:44 +08001985 /*
1986 * Select rate-scale / modulation-mode table to work with in
1987 * the rest of this function: "search" if searching for better
1988 * modulation mode, or "active" if doing rate scaling within a mode.
1989 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001990 if (!lq_sta->search_better_tbl)
1991 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001992 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08001993 active_tbl = 1 - lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001994
Tomas Winklerc33104f2008-01-14 17:46:21 -08001995 tbl = &(lq_sta->lq_info[active_tbl]);
Wey-Yi Guy2681b202009-05-21 13:44:22 -07001996 if (is_legacy(tbl->lq_type))
1997 lq_sta->is_green = 0;
1998 else
Daniel C Halperinb2617932009-08-13 13:30:59 -07001999 lq_sta->is_green = rs_use_green(sta, &priv->current_ht_config);
Tomas Winklerc33104f2008-01-14 17:46:21 -08002000 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07002001
Ben Cahill77626352007-11-29 11:09:44 +08002002 /* current tx rate */
Johannes Bergb7e35002008-09-11 02:22:58 +02002003 index = lq_sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002004
Tomas Winklere1623442009-01-27 14:27:56 -08002005 IWL_DEBUG_RATE(priv, "Rate scale index %d for type %d\n", index,
Zhu Yib481de92007-09-25 17:54:57 -07002006 tbl->lq_type);
2007
Ben Cahill77626352007-11-29 11:09:44 +08002008 /* rates available for this association, and for modulation mode */
Guy Coheneecd6e52008-04-23 17:14:58 -07002009 rate_mask = rs_get_supported_rates(lq_sta, hdr, tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -07002010
Tomas Winklere1623442009-01-27 14:27:56 -08002011 IWL_DEBUG_RATE(priv, "mask 0x%04X \n", rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07002012
2013 /* mask with station rate restriction */
2014 if (is_legacy(tbl->lq_type)) {
Johannes Berg8318d782008-01-24 19:38:38 +01002015 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Ben Cahill77626352007-11-29 11:09:44 +08002016 /* supp_rates has no CCK bits in A mode */
Zhu Yib481de92007-09-25 17:54:57 -07002017 rate_scale_index_msk = (u16) (rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -08002018 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
Zhu Yib481de92007-09-25 17:54:57 -07002019 else
2020 rate_scale_index_msk = (u16) (rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -08002021 lq_sta->supp_rates);
Zhu Yib481de92007-09-25 17:54:57 -07002022
2023 } else
2024 rate_scale_index_msk = rate_mask;
2025
2026 if (!rate_scale_index_msk)
2027 rate_scale_index_msk = rate_mask;
2028
Guy Cohen07bc28e2008-04-23 17:15:03 -07002029 if (!((1 << index) & rate_scale_index_msk)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002030 IWL_ERR(priv, "Current Rate is not valid\n");
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07002031 if (lq_sta->search_better_tbl) {
2032 /* revert to active table if search table is not valid*/
2033 tbl->lq_type = LQ_NONE;
2034 lq_sta->search_better_tbl = 0;
2035 tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
2036 /* get "active" rate info */
2037 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
2038 rate = rs_update_rate_tbl(priv, lq_sta,
2039 tbl, index, is_green);
2040 }
Guy Cohen07bc28e2008-04-23 17:15:03 -07002041 return;
Zhu Yib481de92007-09-25 17:54:57 -07002042 }
2043
Ben Cahill77626352007-11-29 11:09:44 +08002044 /* Get expected throughput table and history window for current rate */
Guy Cohen07bc28e2008-04-23 17:15:03 -07002045 if (!tbl->expected_tpt) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002046 IWL_ERR(priv, "tbl->expected_tpt is NULL\n");
Guy Cohen07bc28e2008-04-23 17:15:03 -07002047 return;
2048 }
Zhu Yib481de92007-09-25 17:54:57 -07002049
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002050 /* force user max rate if set by user */
2051 if ((lq_sta->max_rate_idx != -1) &&
2052 (lq_sta->max_rate_idx < index)) {
2053 index = lq_sta->max_rate_idx;
2054 update_lq = 1;
2055 window = &(tbl->win[index]);
2056 goto lq_update;
2057 }
2058
Zhu Yib481de92007-09-25 17:54:57 -07002059 window = &(tbl->win[index]);
2060
Ben Cahill77626352007-11-29 11:09:44 +08002061 /*
2062 * If there is not enough history to calculate actual average
2063 * throughput, keep analyzing results of more tx frames, without
2064 * changing rate or mode (bypass most of the rest of this function).
2065 * Set up new rate table in uCode only if old rate is not supported
2066 * in current association (use new rate found above).
2067 */
Zhu Yib481de92007-09-25 17:54:57 -07002068 fail_count = window->counter - window->success_counter;
Guy Cohen07bc28e2008-04-23 17:15:03 -07002069 if ((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
2070 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002071 IWL_DEBUG_RATE(priv, "LQ: still below TH. succ=%d total=%d "
Zhu Yib481de92007-09-25 17:54:57 -07002072 "for index %d\n",
2073 window->success_counter, window->counter, index);
Ben Cahill77626352007-11-29 11:09:44 +08002074
2075 /* Can't calculate this yet; not enough history */
Zhu Yib481de92007-09-25 17:54:57 -07002076 window->average_tpt = IWL_INVALID_VALUE;
Ben Cahill77626352007-11-29 11:09:44 +08002077
2078 /* Should we stay with this modulation mode,
2079 * or search for a new one? */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002080 rs_stay_in_table(lq_sta);
Ben Cahill77626352007-11-29 11:09:44 +08002081
Zhu Yib481de92007-09-25 17:54:57 -07002082 goto out;
Guy Cohen3110bef2008-09-09 10:54:54 +08002083 }
Ben Cahill77626352007-11-29 11:09:44 +08002084 /* Else we have enough samples; calculate estimate of
2085 * actual average throughput */
Guy Cohen3110bef2008-09-09 10:54:54 +08002086
Daniel C Halperine3949d62009-09-17 10:43:50 -07002087 /* Sanity-check TPT calculations */
Guy Cohen3110bef2008-09-09 10:54:54 +08002088 BUG_ON(window->average_tpt != ((window->success_ratio *
2089 tbl->expected_tpt[index] + 64) / 128));
Zhu Yib481de92007-09-25 17:54:57 -07002090
Ben Cahill77626352007-11-29 11:09:44 +08002091 /* If we are searching for better modulation mode, check success. */
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002092 if (lq_sta->search_better_tbl &&
Johannes Berg3ad3b922009-08-07 15:41:48 -07002093 (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_MULTI)) {
Ben Cahill77626352007-11-29 11:09:44 +08002094 /* If good success, continue using the "search" mode;
2095 * no need to send new link quality command, since we're
2096 * continuing to use the setup that we've been trying. */
Guy Cohen07bc28e2008-04-23 17:15:03 -07002097 if (window->average_tpt > lq_sta->last_tpt) {
2098
Tomas Winklere1623442009-01-27 14:27:56 -08002099 IWL_DEBUG_RATE(priv, "LQ: SWITCHING TO NEW TABLE "
Guy Cohen07bc28e2008-04-23 17:15:03 -07002100 "suc=%d cur-tpt=%d old-tpt=%d\n",
2101 window->success_ratio,
2102 window->average_tpt,
2103 lq_sta->last_tpt);
2104
2105 if (!is_legacy(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08002106 lq_sta->enable_counter = 1;
Guy Cohen07bc28e2008-04-23 17:15:03 -07002107
Ben Cahill77626352007-11-29 11:09:44 +08002108 /* Swap tables; "search" becomes "active" */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002109 lq_sta->active_tbl = active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002110 current_tpt = window->average_tpt;
Ben Cahill77626352007-11-29 11:09:44 +08002111
2112 /* Else poor success; go back to mode in "active" table */
Zhu Yib481de92007-09-25 17:54:57 -07002113 } else {
Guy Cohen07bc28e2008-04-23 17:15:03 -07002114
Tomas Winklere1623442009-01-27 14:27:56 -08002115 IWL_DEBUG_RATE(priv, "LQ: GOING BACK TO THE OLD TABLE "
Guy Cohen07bc28e2008-04-23 17:15:03 -07002116 "suc=%d cur-tpt=%d old-tpt=%d\n",
2117 window->success_ratio,
2118 window->average_tpt,
2119 lq_sta->last_tpt);
2120
Ben Cahill77626352007-11-29 11:09:44 +08002121 /* Nullify "search" table */
Zhu Yib481de92007-09-25 17:54:57 -07002122 tbl->lq_type = LQ_NONE;
Ben Cahill77626352007-11-29 11:09:44 +08002123
2124 /* Revert to "active" table */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002125 active_tbl = lq_sta->active_tbl;
2126 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07002127
Ben Cahill77626352007-11-29 11:09:44 +08002128 /* Revert to "active" rate and throughput info */
Tomas Winklere7d326a2008-06-12 09:47:11 +08002129 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
Tomas Winklerc33104f2008-01-14 17:46:21 -08002130 current_tpt = lq_sta->last_tpt;
Ben Cahill77626352007-11-29 11:09:44 +08002131
2132 /* Need to set up a new rate table in uCode */
2133 update_lq = 1;
Zhu Yib481de92007-09-25 17:54:57 -07002134 }
Ben Cahill77626352007-11-29 11:09:44 +08002135
2136 /* Either way, we've made a decision; modulation mode
2137 * search is done, allow rate adjustment next time. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002138 lq_sta->search_better_tbl = 0;
Ben Cahill77626352007-11-29 11:09:44 +08002139 done_search = 1; /* Don't switch modes below! */
Zhu Yib481de92007-09-25 17:54:57 -07002140 goto lq_update;
2141 }
2142
Ben Cahill77626352007-11-29 11:09:44 +08002143 /* (Else) not in search of better modulation mode, try for better
2144 * starting rate, while staying in this mode. */
Ester Kummerbf403db2008-05-05 10:22:40 +08002145 high_low = rs_get_adjacent_rate(priv, index, rate_scale_index_msk,
Zhu Yib481de92007-09-25 17:54:57 -07002146 tbl->lq_type);
2147 low = high_low & 0xff;
2148 high = (high_low >> 8) & 0xff;
2149
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002150 /* If user set max rate, dont allow higher than user constrain */
2151 if ((lq_sta->max_rate_idx != -1) &&
2152 (lq_sta->max_rate_idx < high))
2153 high = IWL_RATE_INVALID;
2154
Guy Cohena5e8b502008-05-29 16:35:11 +08002155 sr = window->success_ratio;
2156
Ben Cahill77626352007-11-29 11:09:44 +08002157 /* Collect measured throughputs for current and adjacent rates */
Zhu Yib481de92007-09-25 17:54:57 -07002158 current_tpt = window->average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07002159 if (low != IWL_RATE_INVALID)
2160 low_tpt = tbl->win[low].average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07002161 if (high != IWL_RATE_INVALID)
2162 high_tpt = tbl->win[high].average_tpt;
2163
Guy Cohena5e8b502008-05-29 16:35:11 +08002164 scale_action = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002165
Ben Cahill77626352007-11-29 11:09:44 +08002166 /* Too many failures, decrease rate */
Guy Cohena5e8b502008-05-29 16:35:11 +08002167 if ((sr <= IWL_RATE_DECREASE_TH) || (current_tpt == 0)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002168 IWL_DEBUG_RATE(priv, "decrease rate because of low success_ratio\n");
Zhu Yib481de92007-09-25 17:54:57 -07002169 scale_action = -1;
Ben Cahill77626352007-11-29 11:09:44 +08002170
2171 /* No throughput measured yet for adjacent rates; try increase. */
Zhu Yib481de92007-09-25 17:54:57 -07002172 } else if ((low_tpt == IWL_INVALID_VALUE) &&
Guy Cohena5e8b502008-05-29 16:35:11 +08002173 (high_tpt == IWL_INVALID_VALUE)) {
2174
2175 if (high != IWL_RATE_INVALID && sr >= IWL_RATE_INCREASE_TH)
2176 scale_action = 1;
2177 else if (low != IWL_RATE_INVALID)
Wey-Yi Guy8fe72312009-03-10 14:35:10 -07002178 scale_action = 0;
Guy Cohena5e8b502008-05-29 16:35:11 +08002179 }
Ben Cahill77626352007-11-29 11:09:44 +08002180
2181 /* Both adjacent throughputs are measured, but neither one has better
2182 * throughput; we're using the best rate, don't change it! */
Zhu Yib481de92007-09-25 17:54:57 -07002183 else if ((low_tpt != IWL_INVALID_VALUE) &&
2184 (high_tpt != IWL_INVALID_VALUE) &&
2185 (low_tpt < current_tpt) &&
2186 (high_tpt < current_tpt))
2187 scale_action = 0;
Ben Cahill77626352007-11-29 11:09:44 +08002188
2189 /* At least one adjacent rate's throughput is measured,
2190 * and may have better performance. */
Zhu Yib481de92007-09-25 17:54:57 -07002191 else {
Ben Cahill77626352007-11-29 11:09:44 +08002192 /* Higher adjacent rate's throughput is measured */
Zhu Yib481de92007-09-25 17:54:57 -07002193 if (high_tpt != IWL_INVALID_VALUE) {
Ben Cahill77626352007-11-29 11:09:44 +08002194 /* Higher rate has better throughput */
Guy Cohena5e8b502008-05-29 16:35:11 +08002195 if (high_tpt > current_tpt &&
2196 sr >= IWL_RATE_INCREASE_TH) {
Zhu Yib481de92007-09-25 17:54:57 -07002197 scale_action = 1;
Guy Cohena5e8b502008-05-29 16:35:11 +08002198 } else {
Wey-Yi Guy8fe72312009-03-10 14:35:10 -07002199 scale_action = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002200 }
Ben Cahill77626352007-11-29 11:09:44 +08002201
2202 /* Lower adjacent rate's throughput is measured */
Zhu Yib481de92007-09-25 17:54:57 -07002203 } else if (low_tpt != IWL_INVALID_VALUE) {
Ben Cahill77626352007-11-29 11:09:44 +08002204 /* Lower rate has better throughput */
Zhu Yib481de92007-09-25 17:54:57 -07002205 if (low_tpt > current_tpt) {
Tomas Winklere1623442009-01-27 14:27:56 -08002206 IWL_DEBUG_RATE(priv,
2207 "decrease rate because of low tpt\n");
Zhu Yib481de92007-09-25 17:54:57 -07002208 scale_action = -1;
Guy Cohena5e8b502008-05-29 16:35:11 +08002209 } else if (sr >= IWL_RATE_INCREASE_TH) {
Zhu Yib481de92007-09-25 17:54:57 -07002210 scale_action = 1;
Guy Cohena5e8b502008-05-29 16:35:11 +08002211 }
Zhu Yib481de92007-09-25 17:54:57 -07002212 }
2213 }
2214
Ben Cahill77626352007-11-29 11:09:44 +08002215 /* Sanity check; asked for decrease, but success rate or throughput
2216 * has been good at old rate. Don't change it. */
Guy Cohena5e8b502008-05-29 16:35:11 +08002217 if ((scale_action == -1) && (low != IWL_RATE_INVALID) &&
2218 ((sr > IWL_RATE_HIGH_TH) ||
Zhu Yib481de92007-09-25 17:54:57 -07002219 (current_tpt > (100 * tbl->expected_tpt[low]))))
Zhu Yib481de92007-09-25 17:54:57 -07002220 scale_action = 0;
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002221 if (!iwl_ht_enabled(priv) && !is_legacy(tbl->lq_type))
2222 scale_action = -1;
Johannes Berg3ad3b922009-08-07 15:41:48 -07002223 if (iwl_tx_ant_restriction(priv) != IWL_ANT_OK_MULTI &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002224 (is_mimo2(tbl->lq_type) || is_mimo3(tbl->lq_type)))
2225 scale_action = -1;
Zhu Yib481de92007-09-25 17:54:57 -07002226 switch (scale_action) {
2227 case -1:
Ben Cahill77626352007-11-29 11:09:44 +08002228 /* Decrease starting rate, update uCode's rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002229 if (low != IWL_RATE_INVALID) {
2230 update_lq = 1;
2231 index = low;
2232 }
Mohamed Abbas447fee72009-04-20 14:37:02 -07002233
Zhu Yib481de92007-09-25 17:54:57 -07002234 break;
2235 case 1:
Ben Cahill77626352007-11-29 11:09:44 +08002236 /* Increase starting rate, update uCode's rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002237 if (high != IWL_RATE_INVALID) {
2238 update_lq = 1;
2239 index = high;
2240 }
2241
2242 break;
2243 case 0:
Ben Cahill77626352007-11-29 11:09:44 +08002244 /* No change */
Zhu Yib481de92007-09-25 17:54:57 -07002245 default:
2246 break;
2247 }
2248
Tomas Winklere1623442009-01-27 14:27:56 -08002249 IWL_DEBUG_RATE(priv, "choose rate scale index %d action %d low %d "
Zhu Yib481de92007-09-25 17:54:57 -07002250 "high %d type %d\n",
2251 index, scale_action, low, high, tbl->lq_type);
2252
Guy Cohena5e8b502008-05-29 16:35:11 +08002253lq_update:
Ben Cahill77626352007-11-29 11:09:44 +08002254 /* Replace uCode's rate table for the destination station. */
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07002255 if (update_lq)
2256 rate = rs_update_rate_tbl(priv, lq_sta,
2257 tbl, index, is_green);
Ben Cahill77626352007-11-29 11:09:44 +08002258
Johannes Berg3ad3b922009-08-07 15:41:48 -07002259 if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_MULTI) {
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002260 /* Should we stay with this modulation mode,
2261 * or search for a new one? */
2262 rs_stay_in_table(lq_sta);
2263 }
Ben Cahill77626352007-11-29 11:09:44 +08002264 /*
2265 * Search for new modulation mode if we're:
2266 * 1) Not changing rates right now
2267 * 2) Not just finishing up a search
2268 * 3) Allowing a new search
2269 */
Guy Cohen47cfd462008-05-27 11:29:34 +08002270 if (!update_lq && !done_search && !lq_sta->stay_in_tbl && window->counter) {
Ben Cahill77626352007-11-29 11:09:44 +08002271 /* Save current throughput to compare with "search" throughput*/
Tomas Winklerc33104f2008-01-14 17:46:21 -08002272 lq_sta->last_tpt = current_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07002273
Ben Cahill77626352007-11-29 11:09:44 +08002274 /* Select a new "search" modulation mode to try.
2275 * If one is found, set up the new "search" table. */
Zhu Yib481de92007-09-25 17:54:57 -07002276 if (is_legacy(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08002277 rs_move_legacy_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07002278 else if (is_siso(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08002279 rs_move_siso_to_other(priv, lq_sta, conf, sta, index);
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07002280 else if (is_mimo2(tbl->lq_type))
2281 rs_move_mimo2_to_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07002282 else
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07002283 rs_move_mimo3_to_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07002284
Ben Cahill77626352007-11-29 11:09:44 +08002285 /* If new "search" mode was selected, set up in uCode table */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002286 if (lq_sta->search_better_tbl) {
Ben Cahill77626352007-11-29 11:09:44 +08002287 /* Access the "search" table, clear its history. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002288 tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Zhu Yib481de92007-09-25 17:54:57 -07002289 for (i = 0; i < IWL_RATE_COUNT; i++)
2290 rs_rate_scale_clear_window(&(tbl->win[i]));
2291
Ben Cahill77626352007-11-29 11:09:44 +08002292 /* Use new "search" start rate */
Tomas Winklere7d326a2008-06-12 09:47:11 +08002293 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002294
Tomas Winklere1623442009-01-27 14:27:56 -08002295 IWL_DEBUG_RATE(priv, "Switch current mcs: %X index: %d\n",
Guy Cohen39e88502008-04-23 17:14:57 -07002296 tbl->current_rate, index);
Guy Cohen07bc28e2008-04-23 17:15:03 -07002297 rs_fill_link_cmd(priv, lq_sta, tbl->current_rate);
Tomas Winkler66c73db2008-04-15 16:01:40 -07002298 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
Mohamed Abbas447fee72009-04-20 14:37:02 -07002299 } else
2300 done_search = 1;
2301 }
Zhu Yib481de92007-09-25 17:54:57 -07002302
Mohamed Abbas447fee72009-04-20 14:37:02 -07002303 if (done_search && !lq_sta->stay_in_tbl) {
Ben Cahill77626352007-11-29 11:09:44 +08002304 /* If the "active" (non-search) mode was legacy,
2305 * and we've tried switching antennas,
2306 * but we haven't been able to try HT modes (not available),
2307 * stay with best antenna legacy modulation for a while
2308 * before next round of mode comparisons. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002309 tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
Luis R. Rodriguezde27e642008-12-23 15:58:44 -08002310 if (is_legacy(tbl1->lq_type) && !conf_is_ht(conf) &&
Mohamed Abbasd6e93392009-05-08 13:44:39 -07002311 lq_sta->action_counter > tbl1->max_search) {
Tomas Winklere1623442009-01-27 14:27:56 -08002312 IWL_DEBUG_RATE(priv, "LQ: STAY in legacy table\n");
Ester Kummerbf403db2008-05-05 10:22:40 +08002313 rs_set_stay_in_table(priv, 1, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002314 }
2315
Ben Cahill77626352007-11-29 11:09:44 +08002316 /* If we're in an HT mode, and all 3 mode switch actions
2317 * have been tried and compared, stay in this best modulation
2318 * mode for a while before next round of mode comparisons. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002319 if (lq_sta->enable_counter &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002320 (lq_sta->action_counter >= tbl1->max_search) &&
2321 iwl_ht_enabled(priv)) {
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002322 if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
2323 (lq_sta->tx_agg_tid_en & (1 << tid)) &&
2324 (tid != MAX_TID_COUNT)) {
Wey-Yi Guy86b47662009-03-10 14:35:09 -07002325 tid_data =
2326 &priv->stations[lq_sta->lq.sta_id].tid[tid];
2327 if (tid_data->agg.state == IWL_AGG_OFF) {
2328 IWL_DEBUG_RATE(priv,
2329 "try to aggregate tid %d\n",
2330 tid);
2331 rs_tl_turn_on_agg(priv, tid,
2332 lq_sta, sta);
2333 }
Zhu Yib481de92007-09-25 17:54:57 -07002334 }
Ester Kummerbf403db2008-05-05 10:22:40 +08002335 rs_set_stay_in_table(priv, 0, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002336 }
Zhu Yib481de92007-09-25 17:54:57 -07002337 }
2338
2339out:
Tomas Winkler978785a2008-12-19 10:37:31 +08002340 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
Zhu Yib481de92007-09-25 17:54:57 -07002341 i = index;
Johannes Bergb7e35002008-09-11 02:22:58 +02002342 lq_sta->last_txrate_idx = i;
Zhu Yib481de92007-09-25 17:54:57 -07002343
Zhu Yib481de92007-09-25 17:54:57 -07002344 return;
2345}
2346
2347
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002348static void rs_initialize_lq(struct iwl_priv *priv,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002349 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02002350 struct ieee80211_sta *sta,
2351 struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002352{
Tomas Winklere227cea2008-07-18 13:53:05 +08002353 struct iwl_scale_tbl_info *tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002354 int rate_idx;
Guy Cohenaade00c2008-04-23 17:14:59 -07002355 int i;
Guy Cohen39e88502008-04-23 17:14:57 -07002356 u32 rate;
Daniel C Halperinb2617932009-08-13 13:30:59 -07002357 u8 use_green = rs_use_green(sta, &priv->current_ht_config);
Guy Cohenaade00c2008-04-23 17:14:59 -07002358 u8 active_tbl = 0;
2359 u8 valid_tx_ant;
Zhu Yib481de92007-09-25 17:54:57 -07002360
Johannes Berg4b7679a2008-09-18 18:14:18 +02002361 if (!sta || !lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002362 goto out;
2363
Johannes Bergb7e35002008-09-11 02:22:58 +02002364 i = lq_sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002365
Tomas Winklerc33104f2008-01-14 17:46:21 -08002366 if ((lq_sta->lq.sta_id == 0xff) &&
Johannes Berg05c914f2008-09-11 00:01:58 +02002367 (priv->iw_mode == NL80211_IFTYPE_ADHOC))
Zhu Yib481de92007-09-25 17:54:57 -07002368 goto out;
2369
Guy Cohenaade00c2008-04-23 17:14:59 -07002370 valid_tx_ant = priv->hw_params.valid_tx_ant;
2371
Tomas Winklerc33104f2008-01-14 17:46:21 -08002372 if (!lq_sta->search_better_tbl)
2373 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002374 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002375 active_tbl = 1 - lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002376
Tomas Winklerc33104f2008-01-14 17:46:21 -08002377 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07002378
2379 if ((i < 0) || (i >= IWL_RATE_COUNT))
2380 i = 0;
2381
Tomas Winkler1826dcc2008-05-15 13:54:02 +08002382 rate = iwl_rates[i].plcp;
Winkler, Tomaseb48dca2008-10-29 14:05:48 -07002383 tbl->ant_type = first_antenna(valid_tx_ant);
2384 rate |= tbl->ant_type << RATE_MCS_ANT_POS;
Zhu Yib481de92007-09-25 17:54:57 -07002385
2386 if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
Guy Cohen39e88502008-04-23 17:14:57 -07002387 rate |= RATE_MCS_CCK_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002388
Guy Cohen39e88502008-04-23 17:14:57 -07002389 rs_get_tbl_info_from_mcs(rate, priv->band, tbl, &rate_idx);
Guy Cohenaade00c2008-04-23 17:14:59 -07002390 if (!rs_is_valid_ant(valid_tx_ant, tbl->ant_type))
2391 rs_toggle_antenna(valid_tx_ant, &rate, tbl);
Zhu Yib481de92007-09-25 17:54:57 -07002392
Tomas Winkler978785a2008-12-19 10:37:31 +08002393 rate = rate_n_flags_from_tbl(priv, tbl, rate_idx, use_green);
Guy Cohen39e88502008-04-23 17:14:57 -07002394 tbl->current_rate = rate;
Guy Coheneecd6e52008-04-23 17:14:58 -07002395 rs_set_expected_tpt_table(lq_sta, tbl);
Guy Cohen07bc28e2008-04-23 17:15:03 -07002396 rs_fill_link_cmd(NULL, lq_sta, rate);
Tomas Winkler66c73db2008-04-15 16:01:40 -07002397 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07002398 out:
2399 return;
2400}
2401
Johannes Berge6a98542008-10-21 12:40:02 +02002402static void rs_get_rate(void *priv_r, struct ieee80211_sta *sta, void *priv_sta,
2403 struct ieee80211_tx_rate_control *txrc)
Zhu Yib481de92007-09-25 17:54:57 -07002404{
2405
Johannes Berge6a98542008-10-21 12:40:02 +02002406 struct sk_buff *skb = txrc->skb;
2407 struct ieee80211_supported_band *sband = txrc->sband;
Johannes Berg4b7679a2008-09-18 18:14:18 +02002408 struct iwl_priv *priv = (struct iwl_priv *)priv_r;
2409 struct ieee80211_conf *conf = &priv->hw->conf;
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002410 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Zhu Yib481de92007-09-25 17:54:57 -07002411 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berge6a98542008-10-21 12:40:02 +02002412 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Tomas Winkler417f1142008-11-12 13:14:06 -08002413 struct iwl_lq_sta *lq_sta = priv_sta;
2414 int rate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002415
Tomas Winklere1623442009-01-27 14:27:56 -08002416 IWL_DEBUG_RATE_LIMIT(priv, "rate scale calculate new rate for skb\n");
Zhu Yib481de92007-09-25 17:54:57 -07002417
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002418 /* Get max rate if user set max rate */
2419 if (lq_sta) {
2420 lq_sta->max_rate_idx = txrc->max_rate_idx;
2421 if ((sband->band == IEEE80211_BAND_5GHZ) &&
2422 (lq_sta->max_rate_idx != -1))
2423 lq_sta->max_rate_idx += IWL_FIRST_OFDM_RATE;
2424 if ((lq_sta->max_rate_idx < 0) ||
2425 (lq_sta->max_rate_idx >= IWL_RATE_COUNT))
2426 lq_sta->max_rate_idx = -1;
2427 }
2428
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002429 /* Send management frames and NO_ACK data using lowest rate. */
Luis R. Rodriguez4c6d4f52009-07-16 10:05:41 -07002430 if (rate_control_send_low(sta, priv_sta, txrc))
Johannes Berg4b7679a2008-09-18 18:14:18 +02002431 return;
Zhu Yib481de92007-09-25 17:54:57 -07002432
Tomas Winkler417f1142008-11-12 13:14:06 -08002433 rate_idx = lq_sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002434
Johannes Berg05c914f2008-09-11 00:01:58 +02002435 if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) &&
Tomas Winklerc33104f2008-01-14 17:46:21 -08002436 !lq_sta->ibss_sta_added) {
Tomas Winklerc587de02009-06-03 11:44:07 -07002437 u8 sta_id = iwl_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002438
2439 if (sta_id == IWL_INVALID_STATION) {
Tomas Winklere1623442009-01-27 14:27:56 -08002440 IWL_DEBUG_RATE(priv, "LQ: ADD station %pM\n",
Johannes Berge1749612008-10-27 15:59:26 -07002441 hdr->addr1);
Tomas Winklerc587de02009-06-03 11:44:07 -07002442 sta_id = iwl_add_station(priv, hdr->addr1,
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002443 false, CMD_ASYNC, ht_cap);
Zhu Yib481de92007-09-25 17:54:57 -07002444 }
2445 if ((sta_id != IWL_INVALID_STATION)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002446 lq_sta->lq.sta_id = sta_id;
2447 lq_sta->lq.rs_table[0].rate_n_flags = 0;
2448 lq_sta->ibss_sta_added = 1;
Johannes Berg4b7679a2008-09-18 18:14:18 +02002449 rs_initialize_lq(priv, conf, sta, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002450 }
Zhu Yib481de92007-09-25 17:54:57 -07002451 }
2452
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002453 if (lq_sta->last_rate_n_flags & RATE_MCS_HT_MSK) {
Tomas Winkler417f1142008-11-12 13:14:06 -08002454 rate_idx -= IWL_FIRST_OFDM_RATE;
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002455 /* 6M and 9M shared same MCS index */
2456 rate_idx = (rate_idx > 0) ? (rate_idx - 1) : 0;
2457 if (rs_extract_rate(lq_sta->last_rate_n_flags) >=
2458 IWL_RATE_MIMO3_6M_PLCP)
2459 rate_idx = rate_idx + (2 * MCS_INDEX_PER_STREAM);
2460 else if (rs_extract_rate(lq_sta->last_rate_n_flags) >=
2461 IWL_RATE_MIMO2_6M_PLCP)
2462 rate_idx = rate_idx + MCS_INDEX_PER_STREAM;
2463 info->control.rates[0].flags = IEEE80211_TX_RC_MCS;
2464 if (lq_sta->last_rate_n_flags & RATE_MCS_SGI_MSK)
2465 info->control.rates[0].flags |= IEEE80211_TX_RC_SHORT_GI;
2466 if (lq_sta->last_rate_n_flags & RATE_MCS_DUP_MSK)
2467 info->control.rates[0].flags |= IEEE80211_TX_RC_DUP_DATA;
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07002468 if (lq_sta->last_rate_n_flags & RATE_MCS_HT40_MSK)
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002469 info->control.rates[0].flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
2470 if (lq_sta->last_rate_n_flags & RATE_MCS_GF_MSK)
2471 info->control.rates[0].flags |= IEEE80211_TX_RC_GREEN_FIELD;
2472 } else {
Daniel C Halperin50273092009-08-28 09:44:45 -07002473 /* Check for invalid rates */
2474 if ((rate_idx < 0) || (rate_idx >= IWL_RATE_COUNT_LEGACY) ||
2475 ((sband->band == IEEE80211_BAND_5GHZ) &&
2476 (rate_idx < IWL_FIRST_OFDM_RATE)))
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002477 rate_idx = rate_lowest_index(sband, sta);
Daniel C Halperin50273092009-08-28 09:44:45 -07002478 /* On valid 5 GHz rate, adjust index */
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002479 else if (sband->band == IEEE80211_BAND_5GHZ)
2480 rate_idx -= IWL_FIRST_OFDM_RATE;
Daniel C Halperin7ebaeff2009-08-21 13:34:20 -07002481 info->control.rates[0].flags = 0;
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002482 }
Tomas Winkler417f1142008-11-12 13:14:06 -08002483 info->control.rates[0].idx = rate_idx;
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002484
Zhu Yib481de92007-09-25 17:54:57 -07002485}
2486
Johannes Berg4b7679a2008-09-18 18:14:18 +02002487static void *rs_alloc_sta(void *priv_rate, struct ieee80211_sta *sta,
2488 gfp_t gfp)
Zhu Yib481de92007-09-25 17:54:57 -07002489{
Tomas Winklere227cea2008-07-18 13:53:05 +08002490 struct iwl_lq_sta *lq_sta;
Reinette Chatre8d9698b2009-10-16 14:25:55 -07002491 struct iwl_station_priv *sta_priv = (struct iwl_station_priv *) sta->drv_priv;
Ester Kummerbf403db2008-05-05 10:22:40 +08002492 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07002493
Ester Kummerbf403db2008-05-05 10:22:40 +08002494 priv = (struct iwl_priv *)priv_rate;
Tomas Winklere1623442009-01-27 14:27:56 -08002495 IWL_DEBUG_RATE(priv, "create station rate scale window\n");
Zhu Yib481de92007-09-25 17:54:57 -07002496
Reinette Chatre8d9698b2009-10-16 14:25:55 -07002497 lq_sta = &sta_priv->lq_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002498
Tomas Winklerc33104f2008-01-14 17:46:21 -08002499 return lq_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002500}
2501
Johannes Berg4b7679a2008-09-18 18:14:18 +02002502static void rs_rate_init(void *priv_r, struct ieee80211_supported_band *sband,
2503 struct ieee80211_sta *sta, void *priv_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002504{
2505 int i, j;
Johannes Berg4b7679a2008-09-18 18:14:18 +02002506 struct iwl_priv *priv = (struct iwl_priv *)priv_r;
2507 struct ieee80211_conf *conf = &priv->hw->conf;
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002508 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Tomas Winklere227cea2008-07-18 13:53:05 +08002509 struct iwl_lq_sta *lq_sta = priv_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002510
Reinette Chatre5ad13f82009-10-30 14:36:14 -07002511 lq_sta->lq.sta_id = 0xff;
2512
2513 for (j = 0; j < LQ_SIZE; j++)
2514 for (i = 0; i < IWL_RATE_COUNT; i++)
2515 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
2516
Tomas Winklerc33104f2008-01-14 17:46:21 -08002517 lq_sta->flush_timer = 0;
Johannes Berg4b7679a2008-09-18 18:14:18 +02002518 lq_sta->supp_rates = sta->supp_rates[sband->band];
Zhu Yib481de92007-09-25 17:54:57 -07002519 for (j = 0; j < LQ_SIZE; j++)
2520 for (i = 0; i < IWL_RATE_COUNT; i++)
Tomas Winkler3ac7f142008-07-21 02:40:14 +03002521 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
Zhu Yib481de92007-09-25 17:54:57 -07002522
Tomas Winklere1623442009-01-27 14:27:56 -08002523 IWL_DEBUG_RATE(priv, "LQ: *** rate scale station global init ***\n");
Zhu Yib481de92007-09-25 17:54:57 -07002524 /* TODO: what is a good starting rate for STA? About middle? Maybe not
2525 * the lowest or the highest rate.. Could consider using RSSI from
2526 * previous packets? Need to have IEEE 802.1X auth succeed immediately
2527 * after assoc.. */
2528
Tomas Winklerc33104f2008-01-14 17:46:21 -08002529 lq_sta->ibss_sta_added = 0;
Johannes Berg05c914f2008-09-11 00:01:58 +02002530 if (priv->iw_mode == NL80211_IFTYPE_AP) {
Tomas Winklerc587de02009-06-03 11:44:07 -07002531 u8 sta_id = iwl_find_station(priv,
Abhijeet Kolekare11bc022009-04-08 11:26:41 -07002532 sta->addr);
Joe Perches0795af52007-10-03 17:59:30 -07002533
Zhu Yib481de92007-09-25 17:54:57 -07002534 /* for IBSS the call are from tasklet */
Tomas Winklere1623442009-01-27 14:27:56 -08002535 IWL_DEBUG_RATE(priv, "LQ: ADD station %pM\n", sta->addr);
Zhu Yib481de92007-09-25 17:54:57 -07002536
2537 if (sta_id == IWL_INVALID_STATION) {
Tomas Winklere1623442009-01-27 14:27:56 -08002538 IWL_DEBUG_RATE(priv, "LQ: ADD station %pM\n", sta->addr);
Tomas Winklerc587de02009-06-03 11:44:07 -07002539 sta_id = iwl_add_station(priv, sta->addr, false,
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002540 CMD_ASYNC, ht_cap);
Zhu Yib481de92007-09-25 17:54:57 -07002541 }
2542 if ((sta_id != IWL_INVALID_STATION)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002543 lq_sta->lq.sta_id = sta_id;
2544 lq_sta->lq.rs_table[0].rate_n_flags = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002545 }
2546 /* FIXME: this is w/a remove it later */
2547 priv->assoc_station_added = 1;
2548 }
2549
Tomas Winklerc33104f2008-01-14 17:46:21 -08002550 lq_sta->is_dup = 0;
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002551 lq_sta->max_rate_idx = -1;
Abbas, Mohamed7d049e52009-01-20 21:33:53 -08002552 lq_sta->missed_rate_counter = IWL_MISSED_RATE_MAX;
Daniel C Halperinb2617932009-08-13 13:30:59 -07002553 lq_sta->is_green = rs_use_green(sta, &priv->current_ht_config);
Guy Cohen39e88502008-04-23 17:14:57 -07002554 lq_sta->active_legacy_rate = priv->active_rate & ~(0x1000);
Tomas Winklerc33104f2008-01-14 17:46:21 -08002555 lq_sta->active_rate_basic = priv->active_rate_basic;
Johannes Berg8318d782008-01-24 19:38:38 +01002556 lq_sta->band = priv->band;
Ben Cahill77626352007-11-29 11:09:44 +08002557 /*
2558 * active_siso_rate mask includes 9 MBits (bit 5), and CCK (bits 0-3),
2559 * supp_rates[] does not; shift to convert format, force 9 MBits off.
2560 */
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002561 lq_sta->active_siso_rate = ht_cap->mcs.rx_mask[0] << 1;
2562 lq_sta->active_siso_rate |= ht_cap->mcs.rx_mask[0] & 0x1;
Guy Cohenfde0db32008-04-21 15:42:01 -07002563 lq_sta->active_siso_rate &= ~((u16)0x2);
2564 lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -07002565
Ben Cahill77626352007-11-29 11:09:44 +08002566 /* Same here */
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002567 lq_sta->active_mimo2_rate = ht_cap->mcs.rx_mask[1] << 1;
2568 lq_sta->active_mimo2_rate |= ht_cap->mcs.rx_mask[1] & 0x1;
Guy Cohenfde0db32008-04-21 15:42:01 -07002569 lq_sta->active_mimo2_rate &= ~((u16)0x2);
2570 lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE;
2571
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002572 lq_sta->active_mimo3_rate = ht_cap->mcs.rx_mask[2] << 1;
2573 lq_sta->active_mimo3_rate |= ht_cap->mcs.rx_mask[2] & 0x1;
Guy Cohenfde0db32008-04-21 15:42:01 -07002574 lq_sta->active_mimo3_rate &= ~((u16)0x2);
2575 lq_sta->active_mimo3_rate <<= IWL_FIRST_OFDM_RATE;
2576
Tomas Winklere1623442009-01-27 14:27:56 -08002577 IWL_DEBUG_RATE(priv, "SISO-RATE=%X MIMO2-RATE=%X MIMO3-RATE=%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002578 lq_sta->active_siso_rate,
Guy Cohenfde0db32008-04-21 15:42:01 -07002579 lq_sta->active_mimo2_rate,
2580 lq_sta->active_mimo3_rate);
2581
Tomas Winklera96a27f2008-10-23 23:48:56 -07002582 /* These values will be overridden later */
Guy Cohen07bc28e2008-04-23 17:15:03 -07002583 lq_sta->lq.general_params.single_stream_ant_msk = ANT_A;
2584 lq_sta->lq.general_params.dual_stream_ant_msk = ANT_AB;
2585
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002586 /* as default allow aggregation for all tids */
2587 lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002588 lq_sta->drv = priv;
Zhu Yib481de92007-09-25 17:54:57 -07002589
Daniel C Halperin50273092009-08-28 09:44:45 -07002590 /* Set last_txrate_idx to lowest rate */
2591 lq_sta->last_txrate_idx = rate_lowest_index(sband, sta);
2592 if (sband->band == IEEE80211_BAND_5GHZ)
2593 lq_sta->last_txrate_idx += IWL_FIRST_OFDM_RATE;
Daniel C Halperine3949d62009-09-17 10:43:50 -07002594 lq_sta->is_agg = 0;
Mohamed Abbasd5e49032008-12-12 08:22:15 -08002595
Johannes Berg4b7679a2008-09-18 18:14:18 +02002596 rs_initialize_lq(priv, conf, sta, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002597}
2598
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002599static void rs_fill_link_cmd(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08002600 struct iwl_lq_sta *lq_sta, u32 new_rate)
Zhu Yib481de92007-09-25 17:54:57 -07002601{
Tomas Winklere227cea2008-07-18 13:53:05 +08002602 struct iwl_scale_tbl_info tbl_type;
Zhu Yib481de92007-09-25 17:54:57 -07002603 int index = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002604 int rate_idx;
Zhu Yi1b696de2007-09-27 11:27:41 +08002605 int repeat_rate = 0;
Guy Cohenaade00c2008-04-23 17:14:59 -07002606 u8 ant_toggle_cnt = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002607 u8 use_ht_possible = 1;
Guy Cohenaade00c2008-04-23 17:14:59 -07002608 u8 valid_tx_ant = 0;
Guy Cohen07bc28e2008-04-23 17:15:03 -07002609 struct iwl_link_quality_cmd *lq_cmd = &lq_sta->lq;
Zhu Yib481de92007-09-25 17:54:57 -07002610
Ben Cahill77626352007-11-29 11:09:44 +08002611 /* Override starting rate (index 0) if needed for debug purposes */
Guy Cohen39e88502008-04-23 17:14:57 -07002612 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Zhu Yi98d7e092007-09-27 11:27:42 +08002613
Guy Cohen39e88502008-04-23 17:14:57 -07002614 /* Interpret new_rate (rate_n_flags) */
Guy Cohenaade00c2008-04-23 17:14:59 -07002615 memset(&tbl_type, 0, sizeof(tbl_type));
Guy Cohen39e88502008-04-23 17:14:57 -07002616 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band,
Zhu Yib481de92007-09-25 17:54:57 -07002617 &tbl_type, &rate_idx);
2618
Ben Cahill77626352007-11-29 11:09:44 +08002619 /* How many times should we repeat the initial rate? */
Zhu Yib481de92007-09-25 17:54:57 -07002620 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07002621 ant_toggle_cnt = 1;
Zhu Yi1b696de2007-09-27 11:27:41 +08002622 repeat_rate = IWL_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002623 } else {
Zhu Yi1b696de2007-09-27 11:27:41 +08002624 repeat_rate = IWL_HT_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002625 }
Zhu Yib481de92007-09-25 17:54:57 -07002626
2627 lq_cmd->general_params.mimo_delimiter =
2628 is_mimo(tbl_type.lq_type) ? 1 : 0;
Ben Cahill77626352007-11-29 11:09:44 +08002629
2630 /* Fill 1st table entry (index 0) */
Guy Cohen39e88502008-04-23 17:14:57 -07002631 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002632
Guy Cohen07bc28e2008-04-23 17:15:03 -07002633 if (num_of_ant(tbl_type.ant_type) == 1) {
2634 lq_cmd->general_params.single_stream_ant_msk =
2635 tbl_type.ant_type;
2636 } else if (num_of_ant(tbl_type.ant_type) == 2) {
2637 lq_cmd->general_params.dual_stream_ant_msk =
2638 tbl_type.ant_type;
2639 } /* otherwise we don't modify the existing value */
Zhu Yib481de92007-09-25 17:54:57 -07002640
2641 index++;
Zhu Yi1b696de2007-09-27 11:27:41 +08002642 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002643
Guy Cohenaade00c2008-04-23 17:14:59 -07002644 if (priv)
2645 valid_tx_ant = priv->hw_params.valid_tx_ant;
2646
Ben Cahill77626352007-11-29 11:09:44 +08002647 /* Fill rest of rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002648 while (index < LINK_QUAL_MAX_RETRY_NUM) {
Ben Cahill77626352007-11-29 11:09:44 +08002649 /* Repeat initial/next rate.
2650 * For legacy IWL_NUMBER_TRY == 1, this loop will not execute.
2651 * For HT IWL_HT_NUMBER_TRY == 3, this executes twice. */
Zhu Yi1b696de2007-09-27 11:27:41 +08002652 while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
Zhu Yib481de92007-09-25 17:54:57 -07002653 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07002654 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2655 ant_toggle_cnt++;
2656 else if (priv &&
2657 rs_toggle_antenna(valid_tx_ant,
2658 &new_rate, &tbl_type))
2659 ant_toggle_cnt = 1;
2660}
Zhu Yi98d7e092007-09-27 11:27:42 +08002661
Ben Cahill77626352007-11-29 11:09:44 +08002662 /* Override next rate if needed for debug purposes */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002663 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Ben Cahill77626352007-11-29 11:09:44 +08002664
2665 /* Fill next table entry */
Zhu Yib481de92007-09-25 17:54:57 -07002666 lq_cmd->rs_table[index].rate_n_flags =
Guy Cohen39e88502008-04-23 17:14:57 -07002667 cpu_to_le32(new_rate);
Zhu Yi1b696de2007-09-27 11:27:41 +08002668 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002669 index++;
2670 }
2671
Guy Cohen39e88502008-04-23 17:14:57 -07002672 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band, &tbl_type,
Zhu Yib481de92007-09-25 17:54:57 -07002673 &rate_idx);
2674
Ben Cahill77626352007-11-29 11:09:44 +08002675 /* Indicate to uCode which entries might be MIMO.
2676 * If initial rate was MIMO, this will finally end up
2677 * as (IWL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
Zhu Yib481de92007-09-25 17:54:57 -07002678 if (is_mimo(tbl_type.lq_type))
2679 lq_cmd->general_params.mimo_delimiter = index;
2680
Ben Cahill77626352007-11-29 11:09:44 +08002681 /* Get next rate */
Guy Cohen39e88502008-04-23 17:14:57 -07002682 new_rate = rs_get_lower_rate(lq_sta, &tbl_type, rate_idx,
2683 use_ht_possible);
Zhu Yib481de92007-09-25 17:54:57 -07002684
Ben Cahill77626352007-11-29 11:09:44 +08002685 /* How many times should we repeat the next rate? */
Zhu Yib481de92007-09-25 17:54:57 -07002686 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07002687 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2688 ant_toggle_cnt++;
2689 else if (priv &&
2690 rs_toggle_antenna(valid_tx_ant,
2691 &new_rate, &tbl_type))
2692 ant_toggle_cnt = 1;
2693
Zhu Yi1b696de2007-09-27 11:27:41 +08002694 repeat_rate = IWL_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002695 } else {
Zhu Yi1b696de2007-09-27 11:27:41 +08002696 repeat_rate = IWL_HT_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002697 }
Zhu Yib481de92007-09-25 17:54:57 -07002698
Ben Cahill77626352007-11-29 11:09:44 +08002699 /* Don't allow HT rates after next pass.
2700 * rs_get_lower_rate() will change type to LQ_A or LQ_G. */
Zhu Yib481de92007-09-25 17:54:57 -07002701 use_ht_possible = 0;
2702
Ben Cahill77626352007-11-29 11:09:44 +08002703 /* Override next rate if needed for debug purposes */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002704 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Ben Cahill77626352007-11-29 11:09:44 +08002705
2706 /* Fill next table entry */
Guy Cohen39e88502008-04-23 17:14:57 -07002707 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002708
2709 index++;
Zhu Yi1b696de2007-09-27 11:27:41 +08002710 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002711 }
2712
Wey-Yi Guy4d80d722009-10-02 13:44:01 -07002713 lq_cmd->agg_params.agg_frame_cnt_limit = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
Wey-Yi Guy13c33a02009-06-03 11:44:11 -07002714 lq_cmd->agg_params.agg_dis_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
2715 lq_cmd->agg_params.agg_time_limit =
2716 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
Zhu Yib481de92007-09-25 17:54:57 -07002717}
2718
Johannes Berg4b7679a2008-09-18 18:14:18 +02002719static void *rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
Zhu Yib481de92007-09-25 17:54:57 -07002720{
Johannes Berg4b7679a2008-09-18 18:14:18 +02002721 return hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07002722}
2723/* rate scale requires free function to be implemented */
2724static void rs_free(void *priv_rate)
2725{
2726 return;
2727}
2728
Johannes Berg4b7679a2008-09-18 18:14:18 +02002729static void rs_free_sta(void *priv_r, struct ieee80211_sta *sta,
2730 void *priv_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002731{
Rami Rosen45527c22008-10-07 09:50:01 +02002732 struct iwl_priv *priv __maybe_unused = priv_r;
Zhu Yib481de92007-09-25 17:54:57 -07002733
Tomas Winklere1623442009-01-27 14:27:56 -08002734 IWL_DEBUG_RATE(priv, "enter\n");
Tomas Winklere1623442009-01-27 14:27:56 -08002735 IWL_DEBUG_RATE(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07002736}
2737
2738
Zhu Yi93dc6462007-09-27 11:27:37 +08002739#ifdef CONFIG_MAC80211_DEBUGFS
Zhu Yi5ae212c2007-09-27 11:27:38 +08002740static int open_file_generic(struct inode *inode, struct file *file)
2741{
2742 file->private_data = inode->i_private;
2743 return 0;
2744}
Tomas Winklere227cea2008-07-18 13:53:05 +08002745static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
2746 u32 *rate_n_flags, int index)
Zhu Yi98d7e092007-09-27 11:27:42 +08002747{
Ester Kummerbf403db2008-05-05 10:22:40 +08002748 struct iwl_priv *priv;
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07002749 u8 valid_tx_ant;
2750 u8 ant_sel_tx;
Ester Kummerbf403db2008-05-05 10:22:40 +08002751
2752 priv = lq_sta->drv;
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07002753 valid_tx_ant = priv->hw_params.valid_tx_ant;
Guy Cohen39e88502008-04-23 17:14:57 -07002754 if (lq_sta->dbg_fixed_rate) {
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07002755 ant_sel_tx =
2756 ((lq_sta->dbg_fixed_rate & RATE_MCS_ANT_ABC_MSK)
2757 >> RATE_MCS_ANT_POS);
2758 if ((valid_tx_ant & ant_sel_tx) == ant_sel_tx) {
Guy Cohen39e88502008-04-23 17:14:57 -07002759 *rate_n_flags = lq_sta->dbg_fixed_rate;
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07002760 IWL_DEBUG_RATE(priv, "Fixed rate ON\n");
Guy Cohen39e88502008-04-23 17:14:57 -07002761 } else {
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07002762 lq_sta->dbg_fixed_rate = 0;
2763 IWL_ERR(priv,
2764 "Invalid antenna selection 0x%X, Valid is 0x%X\n",
2765 ant_sel_tx, valid_tx_ant);
2766 IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
Guy Cohen39e88502008-04-23 17:14:57 -07002767 }
Guy Cohen39e88502008-04-23 17:14:57 -07002768 } else {
Tomas Winklere1623442009-01-27 14:27:56 -08002769 IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
Zhu Yi98d7e092007-09-27 11:27:42 +08002770 }
Zhu Yi98d7e092007-09-27 11:27:42 +08002771}
2772
2773static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
2774 const char __user *user_buf, size_t count, loff_t *ppos)
2775{
Tomas Winklere227cea2008-07-18 13:53:05 +08002776 struct iwl_lq_sta *lq_sta = file->private_data;
Ester Kummerbf403db2008-05-05 10:22:40 +08002777 struct iwl_priv *priv;
Zhu Yi98d7e092007-09-27 11:27:42 +08002778 char buf[64];
2779 int buf_size;
2780 u32 parsed_rate;
2781
Ester Kummerbf403db2008-05-05 10:22:40 +08002782 priv = lq_sta->drv;
Zhu Yi98d7e092007-09-27 11:27:42 +08002783 memset(buf, 0, sizeof(buf));
2784 buf_size = min(count, sizeof(buf) - 1);
2785 if (copy_from_user(buf, user_buf, buf_size))
2786 return -EFAULT;
2787
2788 if (sscanf(buf, "%x", &parsed_rate) == 1)
Guy Cohen39e88502008-04-23 17:14:57 -07002789 lq_sta->dbg_fixed_rate = parsed_rate;
Zhu Yi98d7e092007-09-27 11:27:42 +08002790 else
Guy Cohen39e88502008-04-23 17:14:57 -07002791 lq_sta->dbg_fixed_rate = 0;
Zhu Yi98d7e092007-09-27 11:27:42 +08002792
Guy Cohen39e88502008-04-23 17:14:57 -07002793 lq_sta->active_legacy_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
2794 lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2795 lq_sta->active_mimo2_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2796 lq_sta->active_mimo3_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
Zhu Yi98d7e092007-09-27 11:27:42 +08002797
Tomas Winklere1623442009-01-27 14:27:56 -08002798 IWL_DEBUG_RATE(priv, "sta_id %d rate 0x%X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07002799 lq_sta->lq.sta_id, lq_sta->dbg_fixed_rate);
Zhu Yi98d7e092007-09-27 11:27:42 +08002800
Guy Cohen39e88502008-04-23 17:14:57 -07002801 if (lq_sta->dbg_fixed_rate) {
Guy Cohen07bc28e2008-04-23 17:15:03 -07002802 rs_fill_link_cmd(NULL, lq_sta, lq_sta->dbg_fixed_rate);
Tomas Winkler66c73db2008-04-15 16:01:40 -07002803 iwl_send_lq_cmd(lq_sta->drv, &lq_sta->lq, CMD_ASYNC);
Zhu Yi98d7e092007-09-27 11:27:42 +08002804 }
2805
2806 return count;
2807}
Zhu Yi0209dc12007-09-27 11:27:43 +08002808
Zhu Yi5ae212c2007-09-27 11:27:38 +08002809static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
2810 char __user *user_buf, size_t count, loff_t *ppos)
2811{
Frank Seidela412c802009-03-01 20:25:38 +01002812 char *buff;
Zhu Yi5ae212c2007-09-27 11:27:38 +08002813 int desc = 0;
2814 int i = 0;
Wey-Yi Guy12b96812009-04-08 11:39:27 -07002815 int index = 0;
Frank Seidela412c802009-03-01 20:25:38 +01002816 ssize_t ret;
Zhu Yi5ae212c2007-09-27 11:27:38 +08002817
Tomas Winklere227cea2008-07-18 13:53:05 +08002818 struct iwl_lq_sta *lq_sta = file->private_data;
Wey-Yi Guyd8ae4f52009-03-10 14:35:07 -07002819 struct iwl_priv *priv;
Wey-Yi Guyadb7b5e2009-03-10 14:35:08 -07002820 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002821
Wey-Yi Guyd8ae4f52009-03-10 14:35:07 -07002822 priv = lq_sta->drv;
Frank Seidela412c802009-03-01 20:25:38 +01002823 buff = kmalloc(1024, GFP_KERNEL);
2824 if (!buff)
2825 return -ENOMEM;
2826
Tomas Winklerc33104f2008-01-14 17:46:21 -08002827 desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
Zhu Yi98d7e092007-09-27 11:27:42 +08002828 desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002829 lq_sta->total_failed, lq_sta->total_success,
Guy Cohen39e88502008-04-23 17:14:57 -07002830 lq_sta->active_legacy_rate);
Zhu Yi98d7e092007-09-27 11:27:42 +08002831 desc += sprintf(buff+desc, "fixed rate 0x%X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07002832 lq_sta->dbg_fixed_rate);
Wey-Yi Guyd8ae4f52009-03-10 14:35:07 -07002833 desc += sprintf(buff+desc, "valid_tx_ant %s%s%s\n",
2834 (priv->hw_params.valid_tx_ant & ANT_A) ? "ANT_A," : "",
2835 (priv->hw_params.valid_tx_ant & ANT_B) ? "ANT_B," : "",
2836 (priv->hw_params.valid_tx_ant & ANT_C) ? "ANT_C" : "");
Wey-Yi Guyadb7b5e2009-03-10 14:35:08 -07002837 desc += sprintf(buff+desc, "lq type %s\n",
2838 (is_legacy(tbl->lq_type)) ? "legacy" : "HT");
2839 if (is_Ht(tbl->lq_type)) {
2840 desc += sprintf(buff+desc, " %s",
2841 (is_siso(tbl->lq_type)) ? "SISO" :
2842 ((is_mimo2(tbl->lq_type)) ? "MIMO2" : "MIMO3"));
2843 desc += sprintf(buff+desc, " %s",
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07002844 (tbl->is_ht40) ? "40MHz" : "20MHz");
Daniel C Halperine3949d62009-09-17 10:43:50 -07002845 desc += sprintf(buff+desc, " %s %s %s\n", (tbl->is_SGI) ? "SGI" : "",
2846 (lq_sta->is_green) ? "GF enabled" : "",
2847 (lq_sta->is_agg) ? "AGG on" : "");
Wey-Yi Guyadb7b5e2009-03-10 14:35:08 -07002848 }
Wey-Yi Guy12b96812009-04-08 11:39:27 -07002849 desc += sprintf(buff+desc, "last tx rate=0x%X\n",
2850 lq_sta->last_rate_n_flags);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002851 desc += sprintf(buff+desc, "general:"
2852 "flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002853 lq_sta->lq.general_params.flags,
2854 lq_sta->lq.general_params.mimo_delimiter,
2855 lq_sta->lq.general_params.single_stream_ant_msk,
2856 lq_sta->lq.general_params.dual_stream_ant_msk);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002857
2858 desc += sprintf(buff+desc, "agg:"
2859 "time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002860 le16_to_cpu(lq_sta->lq.agg_params.agg_time_limit),
2861 lq_sta->lq.agg_params.agg_dis_start_th,
2862 lq_sta->lq.agg_params.agg_frame_cnt_limit);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002863
2864 desc += sprintf(buff+desc,
2865 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002866 lq_sta->lq.general_params.start_rate_index[0],
2867 lq_sta->lq.general_params.start_rate_index[1],
2868 lq_sta->lq.general_params.start_rate_index[2],
2869 lq_sta->lq.general_params.start_rate_index[3]);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002870
Wey-Yi Guy12b96812009-04-08 11:39:27 -07002871 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
2872 index = iwl_hwrate_to_plcp_idx(
2873 le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags));
2874 if (is_legacy(tbl->lq_type)) {
2875 desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps\n",
2876 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags),
2877 iwl_rate_mcs[index].mbps);
2878 } else {
2879 desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps (%s)\n",
2880 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags),
2881 iwl_rate_mcs[index].mbps, iwl_rate_mcs[index].mcs);
2882 }
2883 }
Zhu Yi5ae212c2007-09-27 11:27:38 +08002884
Frank Seidela412c802009-03-01 20:25:38 +01002885 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2886 kfree(buff);
2887 return ret;
Zhu Yi5ae212c2007-09-27 11:27:38 +08002888}
2889
2890static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
Zhu Yi98d7e092007-09-27 11:27:42 +08002891 .write = rs_sta_dbgfs_scale_table_write,
Zhu Yi5ae212c2007-09-27 11:27:38 +08002892 .read = rs_sta_dbgfs_scale_table_read,
2893 .open = open_file_generic,
2894};
Zhu Yi0209dc12007-09-27 11:27:43 +08002895static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
2896 char __user *user_buf, size_t count, loff_t *ppos)
2897{
Frank Seidela412c802009-03-01 20:25:38 +01002898 char *buff;
Zhu Yi0209dc12007-09-27 11:27:43 +08002899 int desc = 0;
2900 int i, j;
Frank Seidela412c802009-03-01 20:25:38 +01002901 ssize_t ret;
Zhu Yi0209dc12007-09-27 11:27:43 +08002902
Tomas Winklere227cea2008-07-18 13:53:05 +08002903 struct iwl_lq_sta *lq_sta = file->private_data;
Frank Seidela412c802009-03-01 20:25:38 +01002904
2905 buff = kmalloc(1024, GFP_KERNEL);
2906 if (!buff)
2907 return -ENOMEM;
2908
Zhu Yi0209dc12007-09-27 11:27:43 +08002909 for (i = 0; i < LQ_SIZE; i++) {
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07002910 desc += sprintf(buff+desc,
2911 "%s type=%d SGI=%d HT40=%d DUP=%d GF=%d\n"
Zhu Yi0209dc12007-09-27 11:27:43 +08002912 "rate=0x%X\n",
Abhijeet Kolekarc3056062008-11-12 13:14:08 -08002913 lq_sta->active_tbl == i ? "*" : "x",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002914 lq_sta->lq_info[i].lq_type,
2915 lq_sta->lq_info[i].is_SGI,
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07002916 lq_sta->lq_info[i].is_ht40,
Tomas Winklerc33104f2008-01-14 17:46:21 -08002917 lq_sta->lq_info[i].is_dup,
Wey-Yi Guy2681b202009-05-21 13:44:22 -07002918 lq_sta->is_green,
Guy Cohen39e88502008-04-23 17:14:57 -07002919 lq_sta->lq_info[i].current_rate);
Zhu Yi0209dc12007-09-27 11:27:43 +08002920 for (j = 0; j < IWL_RATE_COUNT; j++) {
2921 desc += sprintf(buff+desc,
Tomas Winklerc33104f2008-01-14 17:46:21 -08002922 "counter=%d success=%d %%=%d\n",
2923 lq_sta->lq_info[i].win[j].counter,
2924 lq_sta->lq_info[i].win[j].success_counter,
2925 lq_sta->lq_info[i].win[j].success_ratio);
Zhu Yi0209dc12007-09-27 11:27:43 +08002926 }
2927 }
Frank Seidela412c802009-03-01 20:25:38 +01002928 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2929 kfree(buff);
2930 return ret;
Zhu Yi0209dc12007-09-27 11:27:43 +08002931}
2932
2933static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
2934 .read = rs_sta_dbgfs_stats_table_read,
2935 .open = open_file_generic,
2936};
Zhu Yi5ae212c2007-09-27 11:27:38 +08002937
Wey-Yi Guy38167452009-05-08 13:44:43 -07002938static ssize_t rs_sta_dbgfs_rate_scale_data_read(struct file *file,
2939 char __user *user_buf, size_t count, loff_t *ppos)
2940{
2941 char buff[120];
2942 int desc = 0;
2943 ssize_t ret;
2944
2945 struct iwl_lq_sta *lq_sta = file->private_data;
2946 struct iwl_priv *priv;
2947 struct iwl_scale_tbl_info *tbl = &lq_sta->lq_info[lq_sta->active_tbl];
2948
2949 priv = lq_sta->drv;
2950
2951 if (is_Ht(tbl->lq_type))
2952 desc += sprintf(buff+desc,
2953 "Bit Rate= %d Mb/s\n",
2954 tbl->expected_tpt[lq_sta->last_txrate_idx]);
2955 else
2956 desc += sprintf(buff+desc,
2957 "Bit Rate= %d Mb/s\n",
2958 iwl_rates[lq_sta->last_txrate_idx].ieee >> 1);
2959 desc += sprintf(buff+desc,
2960 "Signal Level= %d dBm\tNoise Level= %d dBm\n",
2961 priv->last_rx_rssi, priv->last_rx_noise);
2962 desc += sprintf(buff+desc,
2963 "Tsf= 0x%llx\tBeacon time= 0x%08X\n",
2964 priv->last_tsf, priv->last_beacon_time);
2965
2966 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2967 return ret;
2968}
2969
2970static const struct file_operations rs_sta_dbgfs_rate_scale_data_ops = {
2971 .read = rs_sta_dbgfs_rate_scale_data_read,
2972 .open = open_file_generic,
2973};
2974
Zhu Yi93dc6462007-09-27 11:27:37 +08002975static void rs_add_debugfs(void *priv, void *priv_sta,
2976 struct dentry *dir)
2977{
Tomas Winklere227cea2008-07-18 13:53:05 +08002978 struct iwl_lq_sta *lq_sta = priv_sta;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002979 lq_sta->rs_sta_dbgfs_scale_table_file =
Wey-Yi Guy43e85112009-11-20 12:04:58 -08002980 debugfs_create_file("rate_scale_table", S_IRUSR | S_IWUSR, dir,
Tomas Winklerc33104f2008-01-14 17:46:21 -08002981 lq_sta, &rs_sta_dbgfs_scale_table_ops);
2982 lq_sta->rs_sta_dbgfs_stats_table_file =
Wey-Yi Guy43e85112009-11-20 12:04:58 -08002983 debugfs_create_file("rate_stats_table", S_IRUSR, dir,
Tomas Winklerc33104f2008-01-14 17:46:21 -08002984 lq_sta, &rs_sta_dbgfs_stats_table_ops);
Wey-Yi Guy38167452009-05-08 13:44:43 -07002985 lq_sta->rs_sta_dbgfs_rate_scale_data_file =
Wey-Yi Guy43e85112009-11-20 12:04:58 -08002986 debugfs_create_file("rate_scale_data", S_IRUSR, dir,
Wey-Yi Guy38167452009-05-08 13:44:43 -07002987 lq_sta, &rs_sta_dbgfs_rate_scale_data_ops);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002988 lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file =
Wey-Yi Guy43e85112009-11-20 12:04:58 -08002989 debugfs_create_u8("tx_agg_tid_enable", S_IRUSR | S_IWUSR, dir,
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002990 &lq_sta->tx_agg_tid_en);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002991
Zhu Yi93dc6462007-09-27 11:27:37 +08002992}
2993
2994static void rs_remove_debugfs(void *priv, void *priv_sta)
2995{
Tomas Winklere227cea2008-07-18 13:53:05 +08002996 struct iwl_lq_sta *lq_sta = priv_sta;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002997 debugfs_remove(lq_sta->rs_sta_dbgfs_scale_table_file);
2998 debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
Wey-Yi Guy38167452009-05-08 13:44:43 -07002999 debugfs_remove(lq_sta->rs_sta_dbgfs_rate_scale_data_file);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02003000 debugfs_remove(lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file);
Zhu Yi93dc6462007-09-27 11:27:37 +08003001}
3002#endif
3003
Zhu Yib481de92007-09-25 17:54:57 -07003004static struct rate_control_ops rs_ops = {
3005 .module = NULL,
3006 .name = RS_NAME,
3007 .tx_status = rs_tx_status,
3008 .get_rate = rs_get_rate,
3009 .rate_init = rs_rate_init,
Zhu Yib481de92007-09-25 17:54:57 -07003010 .alloc = rs_alloc,
3011 .free = rs_free,
3012 .alloc_sta = rs_alloc_sta,
3013 .free_sta = rs_free_sta,
Zhu Yi93dc6462007-09-27 11:27:37 +08003014#ifdef CONFIG_MAC80211_DEBUGFS
3015 .add_sta_debugfs = rs_add_debugfs,
3016 .remove_sta_debugfs = rs_remove_debugfs,
3017#endif
Zhu Yib481de92007-09-25 17:54:57 -07003018};
3019
Tomas Winklere227cea2008-07-18 13:53:05 +08003020int iwlagn_rate_control_register(void)
Zhu Yib481de92007-09-25 17:54:57 -07003021{
Reinette Chatre897e1cf2008-03-28 16:21:09 -07003022 return ieee80211_rate_control_register(&rs_ops);
Zhu Yib481de92007-09-25 17:54:57 -07003023}
3024
Tomas Winklere227cea2008-07-18 13:53:05 +08003025void iwlagn_rate_control_unregister(void)
Zhu Yib481de92007-09-25 17:54:57 -07003026{
3027 ieee80211_rate_control_unregister(&rs_ops);
3028}
3029