blob: 5abe2e9ff0d2e35f924639adb96adc197ce1589b [file] [log] [blame]
Zhu Yib481de92007-09-25 17:54:57 -07001/******************************************************************************
2 *
Reinette Chatre1f447802010-01-15 13:43:41 -08003 * Copyright(c) 2005 - 2010 Intel Corporation. All rights reserved.
Zhu Yib481de92007-09-25 17:54:57 -07004 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
17 *
18 * The full GNU General Public License is included in this distribution in the
19 * file called LICENSE.
20 *
21 * Contact Information:
Winkler, Tomas759ef892008-12-09 11:28:58 -080022 * Intel Linux Wireless <ilw@linux.intel.com>
Zhu Yib481de92007-09-25 17:54:57 -070023 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24 *
25 *****************************************************************************/
26#include <linux/kernel.h>
27#include <linux/init.h>
28#include <linux/skbuff.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Zhu Yib481de92007-09-25 17:54:57 -070030#include <linux/wireless.h>
31#include <net/mac80211.h>
Zhu Yib481de92007-09-25 17:54:57 -070032
33#include <linux/netdevice.h>
34#include <linux/etherdevice.h>
35#include <linux/delay.h>
36
37#include <linux/workqueue.h>
38
Tomas Winkler3e0d4cb2008-04-24 11:55:38 -070039#include "iwl-dev.h"
Emmanuel Grumbachbe1f3ab62008-06-12 09:47:18 +080040#include "iwl-sta.h"
Tomas Winkler857485c2008-03-21 13:53:44 -070041#include "iwl-core.h"
Johannes Berga30e3112010-09-22 18:02:01 +020042#include "iwl-agn.h"
Zhu Yib481de92007-09-25 17:54:57 -070043
Tomas Winklere227cea2008-07-18 13:53:05 +080044#define RS_NAME "iwl-agn-rs"
Zhu Yib481de92007-09-25 17:54:57 -070045
Guy Cohenaade00c2008-04-23 17:14:59 -070046#define NUM_TRY_BEFORE_ANT_TOGGLE 1
Zhu Yib481de92007-09-25 17:54:57 -070047#define IWL_NUMBER_TRY 1
48#define IWL_HT_NUMBER_TRY 3
49
Ben Cahill77626352007-11-29 11:09:44 +080050#define IWL_RATE_MAX_WINDOW 62 /* # tx in history window */
51#define IWL_RATE_MIN_FAILURE_TH 6 /* min failures to calc tpt */
52#define IWL_RATE_MIN_SUCCESS_TH 8 /* min successes to calc tpt */
53
Abbas, Mohamed7d049e52009-01-20 21:33:53 -080054/* max allowed rate miss before sync LQ cmd */
55#define IWL_MISSED_RATE_MAX 15
Ben Cahill77626352007-11-29 11:09:44 +080056/* max time to accum history 2 seconds */
Mohamed Abbas447fee72009-04-20 14:37:02 -070057#define IWL_RATE_SCALE_FLUSH_INTVL (3*HZ)
Zhu Yib481de92007-09-25 17:54:57 -070058
59static u8 rs_ht_to_legacy[] = {
60 IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
61 IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
62 IWL_RATE_6M_INDEX,
63 IWL_RATE_6M_INDEX, IWL_RATE_9M_INDEX,
64 IWL_RATE_12M_INDEX, IWL_RATE_18M_INDEX,
65 IWL_RATE_24M_INDEX, IWL_RATE_36M_INDEX,
66 IWL_RATE_48M_INDEX, IWL_RATE_54M_INDEX
67};
68
Guy Cohenaade00c2008-04-23 17:14:59 -070069static const u8 ant_toggle_lookup[] = {
70 /*ANT_NONE -> */ ANT_NONE,
71 /*ANT_A -> */ ANT_B,
72 /*ANT_B -> */ ANT_C,
73 /*ANT_AB -> */ ANT_BC,
74 /*ANT_C -> */ ANT_A,
75 /*ANT_AC -> */ ANT_AB,
76 /*ANT_BC -> */ ANT_AC,
77 /*ANT_ABC -> */ ANT_ABC,
78};
79
Johannes Berg635b85b2010-09-22 18:02:04 +020080#define IWL_DECLARE_RATE_INFO(r, s, ip, in, rp, rn, pp, np) \
81 [IWL_RATE_##r##M_INDEX] = { IWL_RATE_##r##M_PLCP, \
82 IWL_RATE_SISO_##s##M_PLCP, \
83 IWL_RATE_MIMO2_##s##M_PLCP,\
84 IWL_RATE_MIMO3_##s##M_PLCP,\
85 IWL_RATE_##r##M_IEEE, \
86 IWL_RATE_##ip##M_INDEX, \
87 IWL_RATE_##in##M_INDEX, \
88 IWL_RATE_##rp##M_INDEX, \
89 IWL_RATE_##rn##M_INDEX, \
90 IWL_RATE_##pp##M_INDEX, \
91 IWL_RATE_##np##M_INDEX }
92
93/*
94 * Parameter order:
95 * rate, ht rate, prev rate, next rate, prev tgg rate, next tgg rate
96 *
97 * If there isn't a valid next or previous rate then INV is used which
98 * maps to IWL_RATE_INVALID
99 *
100 */
101const struct iwl_rate_info iwl_rates[IWL_RATE_COUNT] = {
102 IWL_DECLARE_RATE_INFO(1, INV, INV, 2, INV, 2, INV, 2), /* 1mbps */
103 IWL_DECLARE_RATE_INFO(2, INV, 1, 5, 1, 5, 1, 5), /* 2mbps */
104 IWL_DECLARE_RATE_INFO(5, INV, 2, 6, 2, 11, 2, 11), /*5.5mbps */
105 IWL_DECLARE_RATE_INFO(11, INV, 9, 12, 9, 12, 5, 18), /* 11mbps */
106 IWL_DECLARE_RATE_INFO(6, 6, 5, 9, 5, 11, 5, 11), /* 6mbps */
107 IWL_DECLARE_RATE_INFO(9, 6, 6, 11, 6, 11, 5, 11), /* 9mbps */
108 IWL_DECLARE_RATE_INFO(12, 12, 11, 18, 11, 18, 11, 18), /* 12mbps */
109 IWL_DECLARE_RATE_INFO(18, 18, 12, 24, 12, 24, 11, 24), /* 18mbps */
110 IWL_DECLARE_RATE_INFO(24, 24, 18, 36, 18, 36, 18, 36), /* 24mbps */
111 IWL_DECLARE_RATE_INFO(36, 36, 24, 48, 24, 48, 24, 48), /* 36mbps */
112 IWL_DECLARE_RATE_INFO(48, 48, 36, 54, 36, 54, 36, 54), /* 48mbps */
113 IWL_DECLARE_RATE_INFO(54, 54, 48, INV, 48, INV, 48, INV),/* 54mbps */
114 IWL_DECLARE_RATE_INFO(60, 60, 48, INV, 48, INV, 48, INV),/* 60mbps */
115 /* FIXME:RS: ^^ should be INV (legacy) */
116};
117
118static int iwl_hwrate_to_plcp_idx(u32 rate_n_flags)
119{
120 int idx = 0;
121
122 /* HT rate format */
123 if (rate_n_flags & RATE_MCS_HT_MSK) {
124 idx = (rate_n_flags & 0xff);
125
126 if (idx >= IWL_RATE_MIMO3_6M_PLCP)
127 idx = idx - IWL_RATE_MIMO3_6M_PLCP;
128 else if (idx >= IWL_RATE_MIMO2_6M_PLCP)
129 idx = idx - IWL_RATE_MIMO2_6M_PLCP;
130
131 idx += IWL_FIRST_OFDM_RATE;
132 /* skip 9M not supported in ht*/
133 if (idx >= IWL_RATE_9M_INDEX)
134 idx += 1;
135 if ((idx >= IWL_FIRST_OFDM_RATE) && (idx <= IWL_LAST_OFDM_RATE))
136 return idx;
137
138 /* legacy rate format, search for match in table */
139 } else {
140 for (idx = 0; idx < ARRAY_SIZE(iwl_rates); idx++)
141 if (iwl_rates[idx].plcp == (rate_n_flags & 0xFF))
142 return idx;
143 }
144
145 return -1;
146}
147
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700148static void rs_rate_scale_perform(struct iwl_priv *priv,
Gábor Stefanik514d65c2009-04-23 19:36:08 +0200149 struct sk_buff *skb,
Johannes Berg4b7679a2008-09-18 18:14:18 +0200150 struct ieee80211_sta *sta,
151 struct iwl_lq_sta *lq_sta);
Wey-Yi Guy46f93812009-07-24 11:13:03 -0700152static void rs_fill_link_cmd(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +0800153 struct iwl_lq_sta *lq_sta, u32 rate_n_flags);
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -0700154static void rs_stay_in_table(struct iwl_lq_sta *lq_sta, bool force_search);
Zhu Yib481de92007-09-25 17:54:57 -0700155
156
Zhu Yi98d7e092007-09-27 11:27:42 +0800157#ifdef CONFIG_MAC80211_DEBUGFS
Tomas Winklere227cea2008-07-18 13:53:05 +0800158static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
159 u32 *rate_n_flags, int index);
Zhu Yi98d7e092007-09-27 11:27:42 +0800160#else
Tomas Winklere227cea2008-07-18 13:53:05 +0800161static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
162 u32 *rate_n_flags, int index)
Zhu Yi98d7e092007-09-27 11:27:42 +0800163{}
164#endif
Ben Cahill77626352007-11-29 11:09:44 +0800165
Daniel C Halperine3949d62009-09-17 10:43:50 -0700166/**
167 * The following tables contain the expected throughput metrics for all rates
168 *
169 * 1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
170 *
171 * where invalid entries are zeros.
172 *
173 * CCK rates are only valid in legacy table and will only be used in G
174 * (2.4 GHz) band.
Ben Cahill77626352007-11-29 11:09:44 +0800175 */
Wey-Yi Guy584a0f02009-04-01 14:33:24 -0700176
Daniel C Halperine3949d62009-09-17 10:43:50 -0700177static s32 expected_tpt_legacy[IWL_RATE_COUNT] = {
178 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 0
Zhu Yib481de92007-09-25 17:54:57 -0700179};
180
Daniel C Halperine3949d62009-09-17 10:43:50 -0700181static s32 expected_tpt_siso20MHz[4][IWL_RATE_COUNT] = {
182 {0, 0, 0, 0, 42, 0, 76, 102, 124, 158, 183, 193, 202}, /* Norm */
183 {0, 0, 0, 0, 46, 0, 82, 110, 132, 167, 192, 202, 210}, /* SGI */
184 {0, 0, 0, 0, 48, 0, 93, 135, 176, 251, 319, 351, 381}, /* AGG */
185 {0, 0, 0, 0, 53, 0, 102, 149, 193, 275, 348, 381, 413}, /* AGG+SGI */
Zhu Yib481de92007-09-25 17:54:57 -0700186};
187
Daniel C Halperine3949d62009-09-17 10:43:50 -0700188static s32 expected_tpt_siso40MHz[4][IWL_RATE_COUNT] = {
189 {0, 0, 0, 0, 77, 0, 127, 160, 184, 220, 242, 250, 257}, /* Norm */
190 {0, 0, 0, 0, 83, 0, 135, 169, 193, 229, 250, 257, 264}, /* SGI */
191 {0, 0, 0, 0, 96, 0, 182, 259, 328, 451, 553, 598, 640}, /* AGG */
192 {0, 0, 0, 0, 106, 0, 199, 282, 357, 487, 593, 640, 683}, /* AGG+SGI */
Zhu Yib481de92007-09-25 17:54:57 -0700193};
194
Daniel C Halperine3949d62009-09-17 10:43:50 -0700195static s32 expected_tpt_mimo2_20MHz[4][IWL_RATE_COUNT] = {
196 {0, 0, 0, 0, 74, 0, 123, 155, 179, 213, 235, 243, 250}, /* Norm */
197 {0, 0, 0, 0, 81, 0, 131, 164, 187, 221, 242, 250, 256}, /* SGI */
198 {0, 0, 0, 0, 92, 0, 175, 250, 317, 436, 534, 578, 619}, /* AGG */
199 {0, 0, 0, 0, 102, 0, 192, 273, 344, 470, 573, 619, 660}, /* AGG+SGI*/
Zhu Yib481de92007-09-25 17:54:57 -0700200};
201
Daniel C Halperine3949d62009-09-17 10:43:50 -0700202static s32 expected_tpt_mimo2_40MHz[4][IWL_RATE_COUNT] = {
203 {0, 0, 0, 0, 123, 0, 182, 214, 235, 264, 279, 285, 289}, /* Norm */
204 {0, 0, 0, 0, 131, 0, 191, 222, 242, 270, 284, 289, 293}, /* SGI */
205 {0, 0, 0, 0, 180, 0, 327, 446, 545, 708, 828, 878, 922}, /* AGG */
206 {0, 0, 0, 0, 197, 0, 355, 481, 584, 752, 872, 922, 966}, /* AGG+SGI */
Zhu Yib481de92007-09-25 17:54:57 -0700207};
208
Daniel C Halperine3949d62009-09-17 10:43:50 -0700209static s32 expected_tpt_mimo3_20MHz[4][IWL_RATE_COUNT] = {
210 {0, 0, 0, 0, 99, 0, 153, 186, 208, 239, 256, 263, 268}, /* Norm */
211 {0, 0, 0, 0, 106, 0, 162, 194, 215, 246, 262, 268, 273}, /* SGI */
212 {0, 0, 0, 0, 134, 0, 249, 346, 431, 574, 685, 732, 775}, /* AGG */
213 {0, 0, 0, 0, 148, 0, 272, 376, 465, 614, 727, 775, 818}, /* AGG+SGI */
Zhu Yib481de92007-09-25 17:54:57 -0700214};
215
Daniel C Halperine3949d62009-09-17 10:43:50 -0700216static s32 expected_tpt_mimo3_40MHz[4][IWL_RATE_COUNT] = {
217 {0, 0, 0, 0, 152, 0, 211, 239, 255, 279, 290, 294, 297}, /* Norm */
218 {0, 0, 0, 0, 160, 0, 219, 245, 261, 284, 294, 297, 300}, /* SGI */
219 {0, 0, 0, 0, 254, 0, 443, 584, 695, 868, 984, 1030, 1070}, /* AGG */
220 {0, 0, 0, 0, 277, 0, 478, 624, 737, 911, 1026, 1070, 1109}, /* AGG+SGI */
Wey-Yi Guy584a0f02009-04-01 14:33:24 -0700221};
222
Wey-Yi Guy12b96812009-04-08 11:39:27 -0700223/* mbps, mcs */
Tobias Klauser79496732009-12-23 14:18:11 +0100224static const struct iwl_rate_mcs_info iwl_rate_mcs[IWL_RATE_COUNT] = {
Daniel C Halperine3949d62009-09-17 10:43:50 -0700225 { "1", "BPSK DSSS"},
226 { "2", "QPSK DSSS"},
227 {"5.5", "BPSK CCK"},
228 { "11", "QPSK CCK"},
229 { "6", "BPSK 1/2"},
230 { "9", "BPSK 1/2"},
231 { "12", "QPSK 1/2"},
232 { "18", "QPSK 3/4"},
233 { "24", "16QAM 1/2"},
234 { "36", "16QAM 3/4"},
235 { "48", "64QAM 2/3"},
236 { "54", "64QAM 3/4"},
237 { "60", "64QAM 5/6"},
Wey-Yi Guy12b96812009-04-08 11:39:27 -0700238};
239
Wey-Yi Guya9c146b2009-05-22 11:01:48 -0700240#define MCS_INDEX_PER_STREAM (8)
241
Guy Cohen39e88502008-04-23 17:14:57 -0700242static inline u8 rs_extract_rate(u32 rate_n_flags)
Zhu Yib481de92007-09-25 17:54:57 -0700243{
244 return (u8)(rate_n_flags & 0xFF);
245}
246
Tomas Winklere227cea2008-07-18 13:53:05 +0800247static void rs_rate_scale_clear_window(struct iwl_rate_scale_data *window)
Zhu Yib481de92007-09-25 17:54:57 -0700248{
249 window->data = 0;
250 window->success_counter = 0;
251 window->success_ratio = IWL_INVALID_VALUE;
252 window->counter = 0;
253 window->average_tpt = IWL_INVALID_VALUE;
254 window->stamp = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700255}
256
Guy Cohenaade00c2008-04-23 17:14:59 -0700257static inline u8 rs_is_valid_ant(u8 valid_antenna, u8 ant_type)
258{
Tomas Winkler3ac7f142008-07-21 02:40:14 +0300259 return (ant_type & valid_antenna) == ant_type;
Guy Cohenaade00c2008-04-23 17:14:59 -0700260}
261
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200262/*
263 * removes the old data from the statistics. All data that is older than
264 * TID_MAX_TIME_DIFF, will be deleted.
265 */
Tomas Winklere227cea2008-07-18 13:53:05 +0800266static void rs_tl_rm_old_stats(struct iwl_traffic_load *tl, u32 curr_time)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200267{
268 /* The oldest age we want to keep */
269 u32 oldest_time = curr_time - TID_MAX_TIME_DIFF;
270
271 while (tl->queue_count &&
272 (tl->time_stamp < oldest_time)) {
273 tl->total -= tl->packet_count[tl->head];
274 tl->packet_count[tl->head] = 0;
275 tl->time_stamp += TID_QUEUE_CELL_SPACING;
276 tl->queue_count--;
277 tl->head++;
278 if (tl->head >= TID_QUEUE_MAX_SIZE)
279 tl->head = 0;
280 }
281}
282
283/*
284 * increment traffic load value for tid and also remove
285 * any old values if passed the certain time period
286 */
Tomas Winklere227cea2008-07-18 13:53:05 +0800287static u8 rs_tl_add_packet(struct iwl_lq_sta *lq_data,
Ron Rindjunskyfaa29712008-06-12 09:46:58 +0800288 struct ieee80211_hdr *hdr)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200289{
290 u32 curr_time = jiffies_to_msecs(jiffies);
291 u32 time_diff;
292 s32 index;
Tomas Winklere227cea2008-07-18 13:53:05 +0800293 struct iwl_traffic_load *tl = NULL;
Tomas Winkler54dbb522008-05-15 13:54:06 +0800294 u8 tid;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200295
Tomas Winkler417f1142008-11-12 13:14:06 -0800296 if (ieee80211_is_data_qos(hdr->frame_control)) {
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700297 u8 *qc = ieee80211_get_qos_ctl(hdr);
Tomas Winkler54dbb522008-05-15 13:54:06 +0800298 tid = qc[0] & 0xf;
299 } else
Ron Rindjunskyfaa29712008-06-12 09:46:58 +0800300 return MAX_TID_COUNT;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200301
Reinette Chatree6a6cf42009-08-13 13:30:50 -0700302 if (unlikely(tid >= TID_MAX_LOAD_COUNT))
303 return MAX_TID_COUNT;
304
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200305 tl = &lq_data->load[tid];
306
307 curr_time -= curr_time % TID_ROUND_VALUE;
308
309 /* Happens only for the first packet. Initialize the data */
310 if (!(tl->queue_count)) {
311 tl->total = 1;
312 tl->time_stamp = curr_time;
313 tl->queue_count = 1;
314 tl->head = 0;
315 tl->packet_count[0] = 1;
Ron Rindjunskyfaa29712008-06-12 09:46:58 +0800316 return MAX_TID_COUNT;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200317 }
318
319 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
320 index = time_diff / TID_QUEUE_CELL_SPACING;
321
322 /* The history is too long: remove data that is older than */
323 /* TID_MAX_TIME_DIFF */
324 if (index >= TID_QUEUE_MAX_SIZE)
325 rs_tl_rm_old_stats(tl, curr_time);
326
327 index = (tl->head + index) % TID_QUEUE_MAX_SIZE;
328 tl->packet_count[index] = tl->packet_count[index] + 1;
329 tl->total = tl->total + 1;
330
331 if ((index + 1) > tl->queue_count)
332 tl->queue_count = index + 1;
Ron Rindjunskyfaa29712008-06-12 09:46:58 +0800333
334 return tid;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200335}
336
337/*
338 get the traffic load value for tid
339*/
Tomas Winklere227cea2008-07-18 13:53:05 +0800340static u32 rs_tl_get_load(struct iwl_lq_sta *lq_data, u8 tid)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200341{
342 u32 curr_time = jiffies_to_msecs(jiffies);
343 u32 time_diff;
344 s32 index;
Tomas Winklere227cea2008-07-18 13:53:05 +0800345 struct iwl_traffic_load *tl = NULL;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200346
347 if (tid >= TID_MAX_LOAD_COUNT)
348 return 0;
349
350 tl = &(lq_data->load[tid]);
351
352 curr_time -= curr_time % TID_ROUND_VALUE;
353
354 if (!(tl->queue_count))
355 return 0;
356
357 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
358 index = time_diff / TID_QUEUE_CELL_SPACING;
359
360 /* The history is too long: remove data that is older than */
361 /* TID_MAX_TIME_DIFF */
362 if (index >= TID_QUEUE_MAX_SIZE)
363 rs_tl_rm_old_stats(tl, curr_time);
364
365 return tl->total;
366}
367
Wey-Yi Guy82ca9342010-04-12 14:02:36 -0700368static int rs_tl_turn_on_agg_for_tid(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +0800369 struct iwl_lq_sta *lq_data, u8 tid,
Johannes Berg4b7679a2008-09-18 18:14:18 +0200370 struct ieee80211_sta *sta)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200371{
Wey-Yi Guy82ca9342010-04-12 14:02:36 -0700372 int ret = -EAGAIN;
Johannes Berg290f5992010-08-23 07:56:58 -0700373 u32 load;
374
375 /*
376 * Don't create TX aggregation sessions when in high
377 * BT traffic, as they would just be disrupted by BT.
378 */
379 if (priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH) {
380 IWL_ERR(priv, "BT traffic (%d), no aggregation allowed\n",
381 priv->bt_traffic_load);
382 return ret;
383 }
384
385 load = rs_tl_get_load(lq_data, tid);
Wey-Yi Guy45d42702010-02-03 12:24:44 -0800386
Andy Lutomirski24110542010-07-25 13:14:29 -0400387 if (load > IWL_AGG_LOAD_THRESHOLD) {
Tomas Winklere1623442009-01-27 14:27:56 -0800388 IWL_DEBUG_HT(priv, "Starting Tx agg: STA: %pM tid: %d\n",
Johannes Berge1749612008-10-27 15:59:26 -0700389 sta->addr, tid);
Wey-Yi Guy45d42702010-02-03 12:24:44 -0800390 ret = ieee80211_start_tx_ba_session(sta, tid);
391 if (ret == -EAGAIN) {
392 /*
393 * driver and mac80211 is out of sync
394 * this might be cause by reloading firmware
395 * stop the tx ba session here
396 */
Andy Lutomirski24110542010-07-25 13:14:29 -0400397 IWL_ERR(priv, "Fail start Tx agg on tid: %d\n",
Wey-Yi Guy45d42702010-02-03 12:24:44 -0800398 tid);
Johannes Berg6a8579d2010-05-27 14:41:07 +0200399 ieee80211_stop_tx_ba_session(sta, tid);
Wey-Yi Guy45d42702010-02-03 12:24:44 -0800400 }
Andy Lutomirski24110542010-07-25 13:14:29 -0400401 } else {
402 IWL_ERR(priv, "Aggregation not enabled for tid %d "
403 "because load = %u\n", tid, load);
404 }
Wey-Yi Guy82ca9342010-04-12 14:02:36 -0700405 return ret;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200406}
407
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700408static void rs_tl_turn_on_agg(struct iwl_priv *priv, u8 tid,
Tomas Winklere227cea2008-07-18 13:53:05 +0800409 struct iwl_lq_sta *lq_data,
Johannes Berg4b7679a2008-09-18 18:14:18 +0200410 struct ieee80211_sta *sta)
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200411{
Wey-Yi Guycfecc6b2010-06-18 11:33:15 -0700412 if (tid < TID_MAX_LOAD_COUNT)
413 rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
414 else
415 IWL_ERR(priv, "tid exceeds max load count: %d/%d\n",
416 tid, TID_MAX_LOAD_COUNT);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200417}
418
Guy Cohen39e88502008-04-23 17:14:57 -0700419static inline int get_num_of_ant_from_rate(u32 rate_n_flags)
Guy Cohenfde0db32008-04-21 15:42:01 -0700420{
Tomas Winkler3ac7f142008-07-21 02:40:14 +0300421 return !!(rate_n_flags & RATE_MCS_ANT_A_MSK) +
422 !!(rate_n_flags & RATE_MCS_ANT_B_MSK) +
423 !!(rate_n_flags & RATE_MCS_ANT_C_MSK);
Guy Cohenfde0db32008-04-21 15:42:01 -0700424}
425
Shanyu Zhao04f2dec2010-03-19 13:34:45 -0700426/*
427 * Static function to get the expected throughput from an iwl_scale_tbl_info
428 * that wraps a NULL pointer check
429 */
430static s32 get_expected_tpt(struct iwl_scale_tbl_info *tbl, int rs_index)
431{
432 if (tbl->expected_tpt)
433 return tbl->expected_tpt[rs_index];
434 return 0;
435}
436
Ben Cahill77626352007-11-29 11:09:44 +0800437/**
438 * rs_collect_tx_data - Update the success/failure sliding window
439 *
440 * We keep a sliding window of the last 62 packets transmitted
441 * at this rate. window->data contains the bitmask of successful
442 * packets.
443 */
Shanyu Zhao04f2dec2010-03-19 13:34:45 -0700444static int rs_collect_tx_data(struct iwl_scale_tbl_info *tbl,
445 int scale_index, int attempts, int successes)
Zhu Yib481de92007-09-25 17:54:57 -0700446{
Tomas Winklere227cea2008-07-18 13:53:05 +0800447 struct iwl_rate_scale_data *window = NULL;
Guy Cohen39e88502008-04-23 17:14:57 -0700448 static const u64 mask = (((u64)1) << (IWL_RATE_MAX_WINDOW - 1));
Shanyu Zhao04f2dec2010-03-19 13:34:45 -0700449 s32 fail_count, tpt;
Zhu Yib481de92007-09-25 17:54:57 -0700450
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800451 if (scale_index < 0 || scale_index >= IWL_RATE_COUNT)
452 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -0700453
Daniel C Halperine3949d62009-09-17 10:43:50 -0700454 /* Select window for current tx bit rate */
Shanyu Zhao04f2dec2010-03-19 13:34:45 -0700455 window = &(tbl->win[scale_index]);
456
457 /* Get expected throughput */
458 tpt = get_expected_tpt(tbl, scale_index);
Zhu Yib481de92007-09-25 17:54:57 -0700459
Ben Cahill77626352007-11-29 11:09:44 +0800460 /*
461 * Keep track of only the latest 62 tx frame attempts in this rate's
462 * history window; anything older isn't really relevant any more.
463 * If we have filled up the sliding window, drop the oldest attempt;
464 * if the oldest attempt (highest bit in bitmap) shows "success",
465 * subtract "1" from the success counter (this is the main reason
466 * we keep these bitmaps!).
467 */
Daniel C Halperine3949d62009-09-17 10:43:50 -0700468 while (attempts > 0) {
Guy Cohen39e88502008-04-23 17:14:57 -0700469 if (window->counter >= IWL_RATE_MAX_WINDOW) {
470
471 /* remove earliest */
472 window->counter = IWL_RATE_MAX_WINDOW - 1;
473
Ron Rindjunsky99556432008-01-28 14:07:25 +0200474 if (window->data & mask) {
475 window->data &= ~mask;
Guy Cohen39e88502008-04-23 17:14:57 -0700476 window->success_counter--;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200477 }
Zhu Yib481de92007-09-25 17:54:57 -0700478 }
Zhu Yib481de92007-09-25 17:54:57 -0700479
Ron Rindjunsky99556432008-01-28 14:07:25 +0200480 /* Increment frames-attempted counter */
481 window->counter++;
Ben Cahill77626352007-11-29 11:09:44 +0800482
Daniel C Halperine3949d62009-09-17 10:43:50 -0700483 /* Shift bitmap by one frame to throw away oldest history */
Guy Cohen90d77952008-09-09 10:54:53 +0800484 window->data <<= 1;
Daniel C Halperine3949d62009-09-17 10:43:50 -0700485
486 /* Mark the most recent #successes attempts as successful */
Ron Rindjunsky99556432008-01-28 14:07:25 +0200487 if (successes > 0) {
Guy Cohen39e88502008-04-23 17:14:57 -0700488 window->success_counter++;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200489 window->data |= 0x1;
490 successes--;
491 }
492
Daniel C Halperine3949d62009-09-17 10:43:50 -0700493 attempts--;
Zhu Yib481de92007-09-25 17:54:57 -0700494 }
495
Ben Cahill77626352007-11-29 11:09:44 +0800496 /* Calculate current success ratio, avoid divide-by-0! */
Zhu Yib481de92007-09-25 17:54:57 -0700497 if (window->counter > 0)
498 window->success_ratio = 128 * (100 * window->success_counter)
499 / window->counter;
500 else
501 window->success_ratio = IWL_INVALID_VALUE;
502
503 fail_count = window->counter - window->success_counter;
504
Ben Cahill77626352007-11-29 11:09:44 +0800505 /* Calculate average throughput, if we have enough history. */
Zhu Yib481de92007-09-25 17:54:57 -0700506 if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
507 (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
508 window->average_tpt = (window->success_ratio * tpt + 64) / 128;
509 else
510 window->average_tpt = IWL_INVALID_VALUE;
511
Ben Cahill77626352007-11-29 11:09:44 +0800512 /* Tag this window as having been updated */
Zhu Yib481de92007-09-25 17:54:57 -0700513 window->stamp = jiffies;
514
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800515 return 0;
Zhu Yib481de92007-09-25 17:54:57 -0700516}
517
Ben Cahill77626352007-11-29 11:09:44 +0800518/*
519 * Fill uCode API rate_n_flags field, based on "search" or "active" table.
520 */
Guy Cohen39e88502008-04-23 17:14:57 -0700521/* FIXME:RS:remove this function and put the flags statically in the table */
Tomas Winkler978785a2008-12-19 10:37:31 +0800522static u32 rate_n_flags_from_tbl(struct iwl_priv *priv,
523 struct iwl_scale_tbl_info *tbl,
524 int index, u8 use_green)
Zhu Yib481de92007-09-25 17:54:57 -0700525{
Guy Cohen39e88502008-04-23 17:14:57 -0700526 u32 rate_n_flags = 0;
527
Zhu Yib481de92007-09-25 17:54:57 -0700528 if (is_legacy(tbl->lq_type)) {
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800529 rate_n_flags = iwl_rates[index].plcp;
Zhu Yib481de92007-09-25 17:54:57 -0700530 if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
Guy Cohen39e88502008-04-23 17:14:57 -0700531 rate_n_flags |= RATE_MCS_CCK_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700532
Guy Cohenfde0db32008-04-21 15:42:01 -0700533 } else if (is_Ht(tbl->lq_type)) {
534 if (index > IWL_LAST_OFDM_RATE) {
Tomas Winkler978785a2008-12-19 10:37:31 +0800535 IWL_ERR(priv, "Invalid HT rate index %d\n", index);
Zhu Yib481de92007-09-25 17:54:57 -0700536 index = IWL_LAST_OFDM_RATE;
Guy Cohenfde0db32008-04-21 15:42:01 -0700537 }
Guy Cohen39e88502008-04-23 17:14:57 -0700538 rate_n_flags = RATE_MCS_HT_MSK;
Guy Cohenfde0db32008-04-21 15:42:01 -0700539
540 if (is_siso(tbl->lq_type))
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800541 rate_n_flags |= iwl_rates[index].plcp_siso;
Guy Cohenfde0db32008-04-21 15:42:01 -0700542 else if (is_mimo2(tbl->lq_type))
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800543 rate_n_flags |= iwl_rates[index].plcp_mimo2;
Guy Cohenfde0db32008-04-21 15:42:01 -0700544 else
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800545 rate_n_flags |= iwl_rates[index].plcp_mimo3;
Zhu Yib481de92007-09-25 17:54:57 -0700546 } else {
Tomas Winkler978785a2008-12-19 10:37:31 +0800547 IWL_ERR(priv, "Invalid tbl->lq_type %d\n", tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -0700548 }
549
Guy Cohen39e88502008-04-23 17:14:57 -0700550 rate_n_flags |= ((tbl->ant_type << RATE_MCS_ANT_POS) &
Guy Cohenfde0db32008-04-21 15:42:01 -0700551 RATE_MCS_ANT_ABC_MSK);
Zhu Yib481de92007-09-25 17:54:57 -0700552
Guy Cohen39e88502008-04-23 17:14:57 -0700553 if (is_Ht(tbl->lq_type)) {
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700554 if (tbl->is_ht40) {
Guy Cohen39e88502008-04-23 17:14:57 -0700555 if (tbl->is_dup)
556 rate_n_flags |= RATE_MCS_DUP_MSK;
557 else
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700558 rate_n_flags |= RATE_MCS_HT40_MSK;
Guy Cohen39e88502008-04-23 17:14:57 -0700559 }
560 if (tbl->is_SGI)
561 rate_n_flags |= RATE_MCS_SGI_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700562
Guy Cohen39e88502008-04-23 17:14:57 -0700563 if (use_green) {
564 rate_n_flags |= RATE_MCS_GF_MSK;
565 if (is_siso(tbl->lq_type) && tbl->is_SGI) {
566 rate_n_flags &= ~RATE_MCS_SGI_MSK;
Tomas Winkler978785a2008-12-19 10:37:31 +0800567 IWL_ERR(priv, "GF was set with SGI:SISO\n");
Guy Cohen39e88502008-04-23 17:14:57 -0700568 }
569 }
Zhu Yib481de92007-09-25 17:54:57 -0700570 }
Guy Cohen39e88502008-04-23 17:14:57 -0700571 return rate_n_flags;
Zhu Yib481de92007-09-25 17:54:57 -0700572}
573
Ben Cahill77626352007-11-29 11:09:44 +0800574/*
575 * Interpret uCode API's rate_n_flags format,
576 * fill "search" or "active" tx mode table.
577 */
Guy Cohen39e88502008-04-23 17:14:57 -0700578static int rs_get_tbl_info_from_mcs(const u32 rate_n_flags,
Johannes Berg8318d782008-01-24 19:38:38 +0100579 enum ieee80211_band band,
Tomas Winklere227cea2008-07-18 13:53:05 +0800580 struct iwl_scale_tbl_info *tbl,
Zhu Yib481de92007-09-25 17:54:57 -0700581 int *rate_idx)
582{
Guy Cohen39e88502008-04-23 17:14:57 -0700583 u32 ant_msk = (rate_n_flags & RATE_MCS_ANT_ABC_MSK);
584 u8 num_of_ant = get_num_of_ant_from_rate(rate_n_flags);
585 u8 mcs;
Zhu Yib481de92007-09-25 17:54:57 -0700586
Wey-Yi Guy80e91582010-08-03 08:23:32 -0700587 memset(tbl, 0, sizeof(struct iwl_scale_tbl_info));
Tomas Winklere7d326a2008-06-12 09:47:11 +0800588 *rate_idx = iwl_hwrate_to_plcp_idx(rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -0700589
Guy Cohenfde0db32008-04-21 15:42:01 -0700590 if (*rate_idx == IWL_RATE_INVALID) {
Zhu Yib481de92007-09-25 17:54:57 -0700591 *rate_idx = -1;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800592 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -0700593 }
Ben Cahill77626352007-11-29 11:09:44 +0800594 tbl->is_SGI = 0; /* default legacy setup */
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700595 tbl->is_ht40 = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700596 tbl->is_dup = 0;
Guy Cohenfde0db32008-04-21 15:42:01 -0700597 tbl->ant_type = (ant_msk >> RATE_MCS_ANT_POS);
598 tbl->lq_type = LQ_NONE;
Mohamed Abbasd6e93392009-05-08 13:44:39 -0700599 tbl->max_search = IWL_MAX_SEARCH;
Zhu Yib481de92007-09-25 17:54:57 -0700600
Ben Cahill77626352007-11-29 11:09:44 +0800601 /* legacy rate format */
Guy Cohen39e88502008-04-23 17:14:57 -0700602 if (!(rate_n_flags & RATE_MCS_HT_MSK)) {
Guy Cohenfde0db32008-04-21 15:42:01 -0700603 if (num_of_ant == 1) {
Johannes Berg8318d782008-01-24 19:38:38 +0100604 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700605 tbl->lq_type = LQ_A;
606 else
607 tbl->lq_type = LQ_G;
Zhu Yib481de92007-09-25 17:54:57 -0700608 }
Guy Cohenfde0db32008-04-21 15:42:01 -0700609 /* HT rate format */
Zhu Yib481de92007-09-25 17:54:57 -0700610 } else {
Guy Cohen39e88502008-04-23 17:14:57 -0700611 if (rate_n_flags & RATE_MCS_SGI_MSK)
Zhu Yib481de92007-09-25 17:54:57 -0700612 tbl->is_SGI = 1;
613
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700614 if ((rate_n_flags & RATE_MCS_HT40_MSK) ||
Guy Cohen39e88502008-04-23 17:14:57 -0700615 (rate_n_flags & RATE_MCS_DUP_MSK))
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700616 tbl->is_ht40 = 1;
Zhu Yib481de92007-09-25 17:54:57 -0700617
Guy Cohen39e88502008-04-23 17:14:57 -0700618 if (rate_n_flags & RATE_MCS_DUP_MSK)
Zhu Yib481de92007-09-25 17:54:57 -0700619 tbl->is_dup = 1;
Guy Cohenfde0db32008-04-21 15:42:01 -0700620
Guy Cohen39e88502008-04-23 17:14:57 -0700621 mcs = rs_extract_rate(rate_n_flags);
Guy Cohenfde0db32008-04-21 15:42:01 -0700622
Guy Cohen39e88502008-04-23 17:14:57 -0700623 /* SISO */
624 if (mcs <= IWL_RATE_SISO_60M_PLCP) {
Guy Cohenfde0db32008-04-21 15:42:01 -0700625 if (num_of_ant == 1)
626 tbl->lq_type = LQ_SISO; /*else NONE*/
627 /* MIMO2 */
Guy Cohen39e88502008-04-23 17:14:57 -0700628 } else if (mcs <= IWL_RATE_MIMO2_60M_PLCP) {
Guy Cohenfde0db32008-04-21 15:42:01 -0700629 if (num_of_ant == 2)
630 tbl->lq_type = LQ_MIMO2;
631 /* MIMO3 */
632 } else {
Mohamed Abbasd6e93392009-05-08 13:44:39 -0700633 if (num_of_ant == 3) {
634 tbl->max_search = IWL_MAX_11N_MIMO3_SEARCH;
Guy Cohenfde0db32008-04-21 15:42:01 -0700635 tbl->lq_type = LQ_MIMO3;
Mohamed Abbasd6e93392009-05-08 13:44:39 -0700636 }
Guy Cohenfde0db32008-04-21 15:42:01 -0700637 }
Zhu Yib481de92007-09-25 17:54:57 -0700638 }
639 return 0;
640}
Guy Cohenaade00c2008-04-23 17:14:59 -0700641
642/* switch to another antenna/antennas and return 1 */
643/* if no other valid antenna found, return 0 */
644static int rs_toggle_antenna(u32 valid_ant, u32 *rate_n_flags,
Tomas Winklere227cea2008-07-18 13:53:05 +0800645 struct iwl_scale_tbl_info *tbl)
Zhu Yib481de92007-09-25 17:54:57 -0700646{
Guy Cohenaade00c2008-04-23 17:14:59 -0700647 u8 new_ant_type;
648
649 if (!tbl->ant_type || tbl->ant_type > ANT_ABC)
650 return 0;
651
652 if (!rs_is_valid_ant(valid_ant, tbl->ant_type))
653 return 0;
654
655 new_ant_type = ant_toggle_lookup[tbl->ant_type];
656
657 while ((new_ant_type != tbl->ant_type) &&
658 !rs_is_valid_ant(valid_ant, new_ant_type))
659 new_ant_type = ant_toggle_lookup[new_ant_type];
660
661 if (new_ant_type == tbl->ant_type)
662 return 0;
663
664 tbl->ant_type = new_ant_type;
665 *rate_n_flags &= ~RATE_MCS_ANT_ABC_MSK;
666 *rate_n_flags |= new_ant_type << RATE_MCS_ANT_POS;
667 return 1;
Zhu Yib481de92007-09-25 17:54:57 -0700668}
669
Daniel C Halperinb2617932009-08-13 13:30:59 -0700670/**
671 * Green-field mode is valid if the station supports it and
672 * there are no non-GF stations present in the BSS.
673 */
Johannes Berg7e6a5882010-08-23 10:46:46 +0200674static bool rs_use_green(struct ieee80211_sta *sta)
Zhu Yib481de92007-09-25 17:54:57 -0700675{
Johannes Berg7e6a5882010-08-23 10:46:46 +0200676 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
677 struct iwl_rxon_context *ctx = sta_priv->common.ctx;
678
Daniel C Halperinb2617932009-08-13 13:30:59 -0700679 return (sta->ht_cap.cap & IEEE80211_HT_CAP_GRN_FLD) &&
Johannes Berg7e6a5882010-08-23 10:46:46 +0200680 !(ctx->ht.non_gf_sta_present);
Guy Cohenf935a6d2008-04-23 17:15:02 -0700681}
Zhu Yib481de92007-09-25 17:54:57 -0700682
683/**
684 * rs_get_supported_rates - get the available rates
685 *
686 * if management frame or broadcast frame only return
687 * basic available rates.
688 *
689 */
Tomas Winklere227cea2008-07-18 13:53:05 +0800690static u16 rs_get_supported_rates(struct iwl_lq_sta *lq_sta,
691 struct ieee80211_hdr *hdr,
692 enum iwl_table_type rate_type)
Zhu Yib481de92007-09-25 17:54:57 -0700693{
Guy Coheneecd6e52008-04-23 17:14:58 -0700694 if (is_legacy(rate_type)) {
695 return lq_sta->active_legacy_rate;
696 } else {
697 if (is_siso(rate_type))
698 return lq_sta->active_siso_rate;
699 else if (is_mimo2(rate_type))
700 return lq_sta->active_mimo2_rate;
701 else
702 return lq_sta->active_mimo3_rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -0800703 }
Zhu Yib481de92007-09-25 17:54:57 -0700704}
705
Ester Kummerbf403db2008-05-05 10:22:40 +0800706static u16 rs_get_adjacent_rate(struct iwl_priv *priv, u8 index, u16 rate_mask,
707 int rate_type)
Zhu Yib481de92007-09-25 17:54:57 -0700708{
709 u8 high = IWL_RATE_INVALID;
710 u8 low = IWL_RATE_INVALID;
711
Ian Schram01ebd062007-10-25 17:15:22 +0800712 /* 802.11A or ht walks to the next literal adjacent rate in
Zhu Yib481de92007-09-25 17:54:57 -0700713 * the rate table */
714 if (is_a_band(rate_type) || !is_legacy(rate_type)) {
715 int i;
716 u32 mask;
717
718 /* Find the previous rate that is in the rate mask */
719 i = index - 1;
720 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
721 if (rate_mask & mask) {
722 low = i;
723 break;
724 }
725 }
726
727 /* Find the next rate that is in the rate mask */
728 i = index + 1;
729 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
730 if (rate_mask & mask) {
731 high = i;
732 break;
733 }
734 }
735
736 return (high << 8) | low;
737 }
738
739 low = index;
740 while (low != IWL_RATE_INVALID) {
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800741 low = iwl_rates[low].prev_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700742 if (low == IWL_RATE_INVALID)
743 break;
744 if (rate_mask & (1 << low))
745 break;
Tomas Winklere1623442009-01-27 14:27:56 -0800746 IWL_DEBUG_RATE(priv, "Skipping masked lower rate: %d\n", low);
Zhu Yib481de92007-09-25 17:54:57 -0700747 }
748
749 high = index;
750 while (high != IWL_RATE_INVALID) {
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800751 high = iwl_rates[high].next_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700752 if (high == IWL_RATE_INVALID)
753 break;
754 if (rate_mask & (1 << high))
755 break;
Tomas Winklere1623442009-01-27 14:27:56 -0800756 IWL_DEBUG_RATE(priv, "Skipping masked higher rate: %d\n", high);
Zhu Yib481de92007-09-25 17:54:57 -0700757 }
758
759 return (high << 8) | low;
760}
761
Tomas Winklere227cea2008-07-18 13:53:05 +0800762static u32 rs_get_lower_rate(struct iwl_lq_sta *lq_sta,
763 struct iwl_scale_tbl_info *tbl,
764 u8 scale_index, u8 ht_possible)
Zhu Yib481de92007-09-25 17:54:57 -0700765{
Zhu Yib481de92007-09-25 17:54:57 -0700766 s32 low;
767 u16 rate_mask;
768 u16 high_low;
769 u8 switch_to_legacy = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -0800770 u8 is_green = lq_sta->is_green;
Wey-Yi Guy2ff65782009-09-11 10:38:18 -0700771 struct iwl_priv *priv = lq_sta->drv;
Zhu Yib481de92007-09-25 17:54:57 -0700772
773 /* check if we need to switch from HT to legacy rates.
774 * assumption is that mandatory rates (1Mbps or 6Mbps)
775 * are always supported (spec demand) */
776 if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_index)) {
777 switch_to_legacy = 1;
778 scale_index = rs_ht_to_legacy[scale_index];
Johannes Berg8318d782008-01-24 19:38:38 +0100779 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700780 tbl->lq_type = LQ_A;
781 else
782 tbl->lq_type = LQ_G;
783
Guy Cohen69fdb302008-04-23 17:15:01 -0700784 if (num_of_ant(tbl->ant_type) > 1)
Wey-Yi Guy2ff65782009-09-11 10:38:18 -0700785 tbl->ant_type =
786 first_antenna(priv->hw_params.valid_tx_ant);
Zhu Yib481de92007-09-25 17:54:57 -0700787
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700788 tbl->is_ht40 = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700789 tbl->is_SGI = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -0700790 tbl->max_search = IWL_MAX_SEARCH;
Zhu Yib481de92007-09-25 17:54:57 -0700791 }
792
Guy Coheneecd6e52008-04-23 17:14:58 -0700793 rate_mask = rs_get_supported_rates(lq_sta, NULL, tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -0700794
Ben Cahill77626352007-11-29 11:09:44 +0800795 /* Mask with station rate restriction */
Zhu Yib481de92007-09-25 17:54:57 -0700796 if (is_legacy(tbl->lq_type)) {
Ben Cahill77626352007-11-29 11:09:44 +0800797 /* supp_rates has no CCK bits in A mode */
Johannes Berg8318d782008-01-24 19:38:38 +0100798 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700799 rate_mask = (u16)(rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -0800800 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
Zhu Yib481de92007-09-25 17:54:57 -0700801 else
Tomas Winklerc33104f2008-01-14 17:46:21 -0800802 rate_mask = (u16)(rate_mask & lq_sta->supp_rates);
Zhu Yib481de92007-09-25 17:54:57 -0700803 }
804
Ben Cahill77626352007-11-29 11:09:44 +0800805 /* If we switched from HT to legacy, check current rate */
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800806 if (switch_to_legacy && (rate_mask & (1 << scale_index))) {
Guy Cohen39e88502008-04-23 17:14:57 -0700807 low = scale_index;
808 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700809 }
810
Ester Kummerbf403db2008-05-05 10:22:40 +0800811 high_low = rs_get_adjacent_rate(lq_sta->drv, scale_index, rate_mask,
812 tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -0700813 low = high_low & 0xff;
814
Guy Cohen39e88502008-04-23 17:14:57 -0700815 if (low == IWL_RATE_INVALID)
816 low = scale_index;
817
818out:
Tomas Winkler978785a2008-12-19 10:37:31 +0800819 return rate_n_flags_from_tbl(lq_sta->drv, tbl, low, is_green);
Zhu Yib481de92007-09-25 17:54:57 -0700820}
821
Ben Cahill77626352007-11-29 11:09:44 +0800822/*
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700823 * Simple function to compare two rate scale table types
824 */
825static bool table_type_matches(struct iwl_scale_tbl_info *a,
826 struct iwl_scale_tbl_info *b)
827{
828 return (a->lq_type == b->lq_type) && (a->ant_type == b->ant_type) &&
829 (a->is_SGI == b->is_SGI);
830}
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700831
Johannes Berg7e6a5882010-08-23 10:46:46 +0200832static void rs_bt_update_lq(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
833 struct iwl_lq_sta *lq_sta)
Wey-Yi Guybee008b2010-08-23 07:57:04 -0700834{
835 struct iwl_scale_tbl_info *tbl;
836 bool full_concurrent;
837 unsigned long flags;
838
839 spin_lock_irqsave(&priv->lock, flags);
840 if (priv->bt_ci_compliance && priv->bt_ant_couple_ok)
841 full_concurrent = true;
842 else
843 full_concurrent = false;
844 spin_unlock_irqrestore(&priv->lock, flags);
845
846 if (priv->bt_full_concurrent != full_concurrent) {
847 priv->bt_full_concurrent = full_concurrent;
848
849 /* Update uCode's rate table. */
850 tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
851 rs_fill_link_cmd(priv, lq_sta, tbl->current_rate);
Johannes Berg7e6a5882010-08-23 10:46:46 +0200852 iwl_send_lq_cmd(priv, ctx, &lq_sta->lq, CMD_ASYNC, false);
Wey-Yi Guybee008b2010-08-23 07:57:04 -0700853
854 queue_work(priv->workqueue, &priv->bt_full_concurrency);
855 }
856}
857
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700858/*
Ben Cahill77626352007-11-29 11:09:44 +0800859 * mac80211 sends us Tx status
860 */
Johannes Berg4b7679a2008-09-18 18:14:18 +0200861static void rs_tx_status(void *priv_r, struct ieee80211_supported_band *sband,
862 struct ieee80211_sta *sta, void *priv_sta,
Johannes Berge039fa42008-05-15 12:55:29 +0200863 struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -0700864{
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700865 int legacy_success;
866 int retries;
867 int rs_index, mac_index, i;
Tomas Winkler417f1142008-11-12 13:14:06 -0800868 struct iwl_lq_sta *lq_sta = priv_sta;
Tomas Winkler66c73db2008-04-15 16:01:40 -0700869 struct iwl_link_quality_cmd *table;
Zhu Yib481de92007-09-25 17:54:57 -0700870 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berg4b7679a2008-09-18 18:14:18 +0200871 struct iwl_priv *priv = (struct iwl_priv *)priv_r;
Johannes Berge039fa42008-05-15 12:55:29 +0200872 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Daniel C Halperin50273092009-08-28 09:44:45 -0700873 enum mac80211_rate_control_flags mac_flags;
Guy Cohen39e88502008-04-23 17:14:57 -0700874 u32 tx_rate;
Tomas Winklere227cea2008-07-18 13:53:05 +0800875 struct iwl_scale_tbl_info tbl_type;
Shanyu Zhao04f2dec2010-03-19 13:34:45 -0700876 struct iwl_scale_tbl_info *curr_tbl, *other_tbl, *tmp_tbl;
Johannes Berg7e6a5882010-08-23 10:46:46 +0200877 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
878 struct iwl_rxon_context *ctx = sta_priv->common.ctx;
Zhu Yib481de92007-09-25 17:54:57 -0700879
Tomas Winklere1623442009-01-27 14:27:56 -0800880 IWL_DEBUG_RATE_LIMIT(priv, "get frame ack response, update rate scale window\n");
Zhu Yib481de92007-09-25 17:54:57 -0700881
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800882 /* Treat uninitialized rate scaling data same as non-existing. */
883 if (!lq_sta) {
884 IWL_DEBUG_RATE(priv, "Station rate scaling not created yet.\n");
885 return;
886 } else if (!lq_sta->drv) {
887 IWL_DEBUG_RATE(priv, "Rate scaling not initialized yet.\n");
888 return;
889 }
890
Tomas Winkler417f1142008-11-12 13:14:06 -0800891 if (!ieee80211_is_data(hdr->frame_control) ||
Gábor Stefanik514d65c2009-04-23 19:36:08 +0200892 info->flags & IEEE80211_TX_CTL_NO_ACK)
Zhu Yib481de92007-09-25 17:54:57 -0700893 return;
894
Daniel C Halperine3949d62009-09-17 10:43:50 -0700895 /* This packet was aggregated but doesn't carry status info */
Johannes Berge039fa42008-05-15 12:55:29 +0200896 if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
897 !(info->flags & IEEE80211_TX_STAT_AMPDU))
Ron Rindjunsky99556432008-01-28 14:07:25 +0200898 return;
899
Ben Cahill77626352007-11-29 11:09:44 +0800900 /*
901 * Ignore this Tx frame response if its initial rate doesn't match
902 * that of latest Link Quality command. There may be stragglers
903 * from a previous Link Quality command, but we're no longer interested
904 * in those; they're either from the "active" mode while we're trying
905 * to check "search" mode, or a prior "search" mode after we've moved
906 * to a new "search" mode (which might become the new "active" mode).
907 */
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700908 table = &lq_sta->lq;
Guy Cohen39e88502008-04-23 17:14:57 -0700909 tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
910 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800911 if (priv->band == IEEE80211_BAND_5GHZ)
912 rs_index -= IWL_FIRST_OFDM_RATE;
Daniel C Halperin50273092009-08-28 09:44:45 -0700913 mac_flags = info->status.rates[0].flags;
914 mac_index = info->status.rates[0].idx;
Daniel C Halperin31513be2009-08-28 09:44:47 -0700915 /* For HT packets, map MCS to PLCP */
916 if (mac_flags & IEEE80211_TX_RC_MCS) {
917 mac_index &= RATE_MCS_CODE_MSK; /* Remove # of streams */
918 if (mac_index >= (IWL_RATE_9M_INDEX - IWL_FIRST_OFDM_RATE))
919 mac_index++;
Daniel C Halperin392a0ba2009-09-11 10:38:08 -0700920 /*
921 * mac80211 HT index is always zero-indexed; we need to move
922 * HT OFDM rates after CCK rates in 2.4 GHz band
923 */
924 if (priv->band == IEEE80211_BAND_2GHZ)
925 mac_index += IWL_FIRST_OFDM_RATE;
Daniel C Halperin31513be2009-08-28 09:44:47 -0700926 }
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700927 /* Here we actually compare this rate to the latest LQ command */
Daniel C Halperin50273092009-08-28 09:44:45 -0700928 if ((mac_index < 0) ||
929 (tbl_type.is_SGI != !!(mac_flags & IEEE80211_TX_RC_SHORT_GI)) ||
930 (tbl_type.is_ht40 != !!(mac_flags & IEEE80211_TX_RC_40_MHZ_WIDTH)) ||
931 (tbl_type.is_dup != !!(mac_flags & IEEE80211_TX_RC_DUP_DATA)) ||
Johannes Berge6a98542008-10-21 12:40:02 +0200932 (tbl_type.ant_type != info->antenna_sel_tx) ||
Daniel C Halperin50273092009-08-28 09:44:45 -0700933 (!!(tx_rate & RATE_MCS_HT_MSK) != !!(mac_flags & IEEE80211_TX_RC_MCS)) ||
934 (!!(tx_rate & RATE_MCS_GF_MSK) != !!(mac_flags & IEEE80211_TX_RC_GREEN_FIELD)) ||
Daniel C Halperin31513be2009-08-28 09:44:47 -0700935 (rs_index != mac_index)) {
936 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 -0700937 /*
938 * Since rates mis-match, the last LQ command may have failed.
939 * After IWL_MISSED_RATE_MAX mis-matches, resync the uCode with
940 * ... driver.
Mohamed Abbasd5e49032008-12-12 08:22:15 -0800941 */
Abbas, Mohamed7d049e52009-01-20 21:33:53 -0800942 lq_sta->missed_rate_counter++;
943 if (lq_sta->missed_rate_counter > IWL_MISSED_RATE_MAX) {
944 lq_sta->missed_rate_counter = 0;
Johannes Berg7e6a5882010-08-23 10:46:46 +0200945 iwl_send_lq_cmd(priv, ctx, &lq_sta->lq, CMD_ASYNC, false);
Abbas, Mohamed7d049e52009-01-20 21:33:53 -0800946 }
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700947 /* Regardless, ignore this status info for outdated rate */
948 return;
949 } else
950 /* Rate did match, so reset the missed_rate_counter */
951 lq_sta->missed_rate_counter = 0;
952
953 /* Figure out if rate scale algorithm is in active or search table */
954 if (table_type_matches(&tbl_type,
955 &(lq_sta->lq_info[lq_sta->active_tbl]))) {
956 curr_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
957 other_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
958 } else if (table_type_matches(&tbl_type,
959 &lq_sta->lq_info[1 - lq_sta->active_tbl])) {
960 curr_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
961 other_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
962 } else {
963 IWL_DEBUG_RATE(priv, "Neither active nor search matches tx rate\n");
Wey-Yi Guy80e91582010-08-03 08:23:32 -0700964 tmp_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
965 IWL_DEBUG_RATE(priv, "active- lq:%x, ant:%x, SGI:%d\n",
966 tmp_tbl->lq_type, tmp_tbl->ant_type, tmp_tbl->is_SGI);
967 tmp_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
968 IWL_DEBUG_RATE(priv, "search- lq:%x, ant:%x, SGI:%d\n",
969 tmp_tbl->lq_type, tmp_tbl->ant_type, tmp_tbl->is_SGI);
970 IWL_DEBUG_RATE(priv, "actual- lq:%x, ant:%x, SGI:%d\n",
971 tbl_type.lq_type, tbl_type.ant_type, tbl_type.is_SGI);
972 /*
973 * no matching table found, let's by-pass the data collection
974 * and continue to perform rate scale to find the rate table
975 */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -0700976 rs_stay_in_table(lq_sta, true);
Wey-Yi Guy80e91582010-08-03 08:23:32 -0700977 goto done;
Zhu Yib481de92007-09-25 17:54:57 -0700978 }
Zhu Yib481de92007-09-25 17:54:57 -0700979
Ben Cahill77626352007-11-29 11:09:44 +0800980 /*
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700981 * Updating the frame history depends on whether packets were
982 * aggregated.
983 *
984 * For aggregation, all packets were transmitted at the same rate, the
985 * first index into rate scale table.
Ben Cahill77626352007-11-29 11:09:44 +0800986 */
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700987 if (info->flags & IEEE80211_TX_STAT_AMPDU) {
988 tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
989 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type,
990 &rs_index);
Shanyu Zhao04f2dec2010-03-19 13:34:45 -0700991 rs_collect_tx_data(curr_tbl, rs_index,
Daniel Halperine3a3cd82010-04-18 09:27:58 -0700992 info->status.ampdu_len,
993 info->status.ampdu_ack_len);
Zhu Yib481de92007-09-25 17:54:57 -0700994
Daniel C Halperin95407aa2009-09-17 10:43:48 -0700995 /* Update success/fail counts if not searching for new mode */
996 if (lq_sta->stay_in_tbl) {
Daniel Halperine3a3cd82010-04-18 09:27:58 -0700997 lq_sta->total_success += info->status.ampdu_ack_len;
998 lq_sta->total_failed += (info->status.ampdu_len -
999 info->status.ampdu_ack_len);
Daniel C Halperin95407aa2009-09-17 10:43:48 -07001000 }
1001 } else {
1002 /*
1003 * For legacy, update frame history with for each Tx retry.
1004 */
1005 retries = info->status.rates[0].count - 1;
1006 /* HW doesn't send more than 15 retries */
1007 retries = min(retries, 15);
1008
1009 /* The last transmission may have been successful */
1010 legacy_success = !!(info->flags & IEEE80211_TX_STAT_ACK);
1011 /* Collect data for each rate used during failed TX attempts */
1012 for (i = 0; i <= retries; ++i) {
1013 tx_rate = le32_to_cpu(table->rs_table[i].rate_n_flags);
1014 rs_get_tbl_info_from_mcs(tx_rate, priv->band,
1015 &tbl_type, &rs_index);
1016 /*
1017 * Only collect stats if retried rate is in the same RS
1018 * table as active/search.
1019 */
1020 if (table_type_matches(&tbl_type, curr_tbl))
Shanyu Zhao04f2dec2010-03-19 13:34:45 -07001021 tmp_tbl = curr_tbl;
Daniel C Halperin95407aa2009-09-17 10:43:48 -07001022 else if (table_type_matches(&tbl_type, other_tbl))
Shanyu Zhao04f2dec2010-03-19 13:34:45 -07001023 tmp_tbl = other_tbl;
Ron Rindjunsky99556432008-01-28 14:07:25 +02001024 else
Daniel C Halperin95407aa2009-09-17 10:43:48 -07001025 continue;
Shanyu Zhao04f2dec2010-03-19 13:34:45 -07001026 rs_collect_tx_data(tmp_tbl, rs_index, 1,
1027 i < retries ? 0 : legacy_success);
Daniel C Halperin95407aa2009-09-17 10:43:48 -07001028 }
1029
1030 /* Update success/fail counts if not searching for new mode */
1031 if (lq_sta->stay_in_tbl) {
1032 lq_sta->total_success += legacy_success;
1033 lq_sta->total_failed += retries + (1 - legacy_success);
Ron Rindjunsky99556432008-01-28 14:07:25 +02001034 }
Zhu Yib481de92007-09-25 17:54:57 -07001035 }
Daniel C Halperin95407aa2009-09-17 10:43:48 -07001036 /* The last TX rate is cached in lq_sta; it's set in if/else above */
1037 lq_sta->last_rate_n_flags = tx_rate;
Wey-Yi Guy80e91582010-08-03 08:23:32 -07001038done:
Ben Cahill77626352007-11-29 11:09:44 +08001039 /* See if there's a better rate or modulation mode to try. */
Abbas, Mohamedc338ba32009-01-21 10:58:02 -08001040 if (sta && sta->supp_rates[sband->band])
Gábor Stefanik514d65c2009-04-23 19:36:08 +02001041 rs_rate_scale_perform(priv, skb, sta, lq_sta);
Wey-Yi Guybee008b2010-08-23 07:57:04 -07001042
1043 /* Is there a need to switch between full concurrency and 3-wire? */
1044 if (priv->bt_ant_couple_ok)
Johannes Berg7e6a5882010-08-23 10:46:46 +02001045 rs_bt_update_lq(priv, ctx, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07001046}
1047
Ben Cahill77626352007-11-29 11:09:44 +08001048/*
1049 * Begin a period of staying with a selected modulation mode.
1050 * Set "stay_in_tbl" flag to prevent any mode switches.
1051 * Set frame tx success limits according to legacy vs. high-throughput,
1052 * and reset overall (spanning all rates) tx success history statistics.
1053 * These control how long we stay using same modulation mode before
1054 * searching for a new mode.
1055 */
Ester Kummerbf403db2008-05-05 10:22:40 +08001056static void rs_set_stay_in_table(struct iwl_priv *priv, u8 is_legacy,
Tomas Winklere227cea2008-07-18 13:53:05 +08001057 struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07001058{
Tomas Winklere1623442009-01-27 14:27:56 -08001059 IWL_DEBUG_RATE(priv, "we are staying in the same table\n");
Tomas Winklerc33104f2008-01-14 17:46:21 -08001060 lq_sta->stay_in_tbl = 1; /* only place this gets set */
Zhu Yib481de92007-09-25 17:54:57 -07001061 if (is_legacy) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001062 lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT;
1063 lq_sta->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
1064 lq_sta->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
Zhu Yib481de92007-09-25 17:54:57 -07001065 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001066 lq_sta->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
1067 lq_sta->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
1068 lq_sta->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
Zhu Yib481de92007-09-25 17:54:57 -07001069 }
Tomas Winklerc33104f2008-01-14 17:46:21 -08001070 lq_sta->table_count = 0;
1071 lq_sta->total_failed = 0;
1072 lq_sta->total_success = 0;
Mohamed Abbas447fee72009-04-20 14:37:02 -07001073 lq_sta->flush_timer = jiffies;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001074 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001075}
1076
Ben Cahill77626352007-11-29 11:09:44 +08001077/*
1078 * Find correct throughput table for given mode of modulation
1079 */
Tomas Winklere227cea2008-07-18 13:53:05 +08001080static void rs_set_expected_tpt_table(struct iwl_lq_sta *lq_sta,
1081 struct iwl_scale_tbl_info *tbl)
Zhu Yib481de92007-09-25 17:54:57 -07001082{
Daniel C Halperine3949d62009-09-17 10:43:50 -07001083 /* Used to choose among HT tables */
1084 s32 (*ht_tbl_pointer)[IWL_RATE_COUNT];
1085
1086 /* Check for invalid LQ type */
1087 if (WARN_ON_ONCE(!is_legacy(tbl->lq_type) && !is_Ht(tbl->lq_type))) {
1088 tbl->expected_tpt = expected_tpt_legacy;
1089 return;
1090 }
1091
1092 /* Legacy rates have only one table */
Zhu Yib481de92007-09-25 17:54:57 -07001093 if (is_legacy(tbl->lq_type)) {
Daniel C Halperine3949d62009-09-17 10:43:50 -07001094 tbl->expected_tpt = expected_tpt_legacy;
1095 return;
1096 }
1097
1098 /* Choose among many HT tables depending on number of streams
1099 * (SISO/MIMO2/MIMO3), channel width (20/40), SGI, and aggregation
1100 * status */
1101 if (is_siso(tbl->lq_type) && (!tbl->is_ht40 || lq_sta->is_dup))
1102 ht_tbl_pointer = expected_tpt_siso20MHz;
1103 else if (is_siso(tbl->lq_type))
1104 ht_tbl_pointer = expected_tpt_siso40MHz;
1105 else if (is_mimo2(tbl->lq_type) && (!tbl->is_ht40 || lq_sta->is_dup))
1106 ht_tbl_pointer = expected_tpt_mimo2_20MHz;
1107 else if (is_mimo2(tbl->lq_type))
1108 ht_tbl_pointer = expected_tpt_mimo2_40MHz;
1109 else if (is_mimo3(tbl->lq_type) && (!tbl->is_ht40 || lq_sta->is_dup))
1110 ht_tbl_pointer = expected_tpt_mimo3_20MHz;
1111 else /* if (is_mimo3(tbl->lq_type)) <-- must be true */
1112 ht_tbl_pointer = expected_tpt_mimo3_40MHz;
1113
1114 if (!tbl->is_SGI && !lq_sta->is_agg) /* Normal */
1115 tbl->expected_tpt = ht_tbl_pointer[0];
1116 else if (tbl->is_SGI && !lq_sta->is_agg) /* SGI */
1117 tbl->expected_tpt = ht_tbl_pointer[1];
1118 else if (!tbl->is_SGI && lq_sta->is_agg) /* AGG */
1119 tbl->expected_tpt = ht_tbl_pointer[2];
1120 else /* AGG+SGI */
1121 tbl->expected_tpt = ht_tbl_pointer[3];
Zhu Yib481de92007-09-25 17:54:57 -07001122}
1123
Ben Cahill77626352007-11-29 11:09:44 +08001124/*
1125 * Find starting rate for new "search" high-throughput mode of modulation.
1126 * Goal is to find lowest expected rate (under perfect conditions) that is
1127 * above the current measured throughput of "active" mode, to give new mode
1128 * a fair chance to prove itself without too many challenges.
1129 *
1130 * This gets called when transitioning to more aggressive modulation
1131 * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
1132 * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
1133 * to decrease to match "active" throughput. When moving from MIMO to SISO,
1134 * bit rate will typically need to increase, but not if performance was bad.
1135 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001136static s32 rs_get_best_rate(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001137 struct iwl_lq_sta *lq_sta,
1138 struct iwl_scale_tbl_info *tbl, /* "search" */
Guy Cohenf935a6d2008-04-23 17:15:02 -07001139 u16 rate_mask, s8 index)
Zhu Yib481de92007-09-25 17:54:57 -07001140{
Ben Cahill77626352007-11-29 11:09:44 +08001141 /* "active" values */
Tomas Winklere227cea2008-07-18 13:53:05 +08001142 struct iwl_scale_tbl_info *active_tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001143 &(lq_sta->lq_info[lq_sta->active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07001144 s32 active_sr = active_tbl->win[index].success_ratio;
Zhu Yib481de92007-09-25 17:54:57 -07001145 s32 active_tpt = active_tbl->expected_tpt[index];
Ben Cahill77626352007-11-29 11:09:44 +08001146
1147 /* expected "search" throughput */
1148 s32 *tpt_tbl = tbl->expected_tpt;
1149
1150 s32 new_rate, high, low, start_hi;
Zhu Yib481de92007-09-25 17:54:57 -07001151 u16 high_low;
Guy Cohenf935a6d2008-04-23 17:15:02 -07001152 s8 rate = index;
Zhu Yib481de92007-09-25 17:54:57 -07001153
1154 new_rate = high = low = start_hi = IWL_RATE_INVALID;
1155
1156 for (; ;) {
Ester Kummerbf403db2008-05-05 10:22:40 +08001157 high_low = rs_get_adjacent_rate(priv, rate, rate_mask,
1158 tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -07001159
1160 low = high_low & 0xff;
1161 high = (high_low >> 8) & 0xff;
1162
Ben Cahill77626352007-11-29 11:09:44 +08001163 /*
1164 * Lower the "search" bit rate, to give new "search" mode
1165 * approximately the same throughput as "active" if:
1166 *
1167 * 1) "Active" mode has been working modestly well (but not
1168 * great), and expected "search" throughput (under perfect
1169 * conditions) at candidate rate is above the actual
1170 * measured "active" throughput (but less than expected
1171 * "active" throughput under perfect conditions).
1172 * OR
1173 * 2) "Active" mode has been working perfectly or very well
1174 * and expected "search" throughput (under perfect
1175 * conditions) at candidate rate is above expected
1176 * "active" throughput (under perfect conditions).
1177 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001178 if ((((100 * tpt_tbl[rate]) > lq_sta->last_tpt) &&
Zhu Yib481de92007-09-25 17:54:57 -07001179 ((active_sr > IWL_RATE_DECREASE_TH) &&
1180 (active_sr <= IWL_RATE_HIGH_TH) &&
1181 (tpt_tbl[rate] <= active_tpt))) ||
1182 ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
1183 (tpt_tbl[rate] > active_tpt))) {
1184
Ben Cahill77626352007-11-29 11:09:44 +08001185 /* (2nd or later pass)
1186 * If we've already tried to raise the rate, and are
1187 * now trying to lower it, use the higher rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001188 if (start_hi != IWL_RATE_INVALID) {
1189 new_rate = start_hi;
1190 break;
1191 }
Ben Cahill77626352007-11-29 11:09:44 +08001192
Zhu Yib481de92007-09-25 17:54:57 -07001193 new_rate = rate;
Ben Cahill77626352007-11-29 11:09:44 +08001194
1195 /* Loop again with lower rate */
Zhu Yib481de92007-09-25 17:54:57 -07001196 if (low != IWL_RATE_INVALID)
1197 rate = low;
Ben Cahill77626352007-11-29 11:09:44 +08001198
1199 /* Lower rate not available, use the original */
Zhu Yib481de92007-09-25 17:54:57 -07001200 else
1201 break;
Ben Cahill77626352007-11-29 11:09:44 +08001202
1203 /* Else try to raise the "search" rate to match "active" */
Zhu Yib481de92007-09-25 17:54:57 -07001204 } else {
Ben Cahill77626352007-11-29 11:09:44 +08001205 /* (2nd or later pass)
1206 * If we've already tried to lower the rate, and are
1207 * now trying to raise it, use the lower rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001208 if (new_rate != IWL_RATE_INVALID)
1209 break;
Ben Cahill77626352007-11-29 11:09:44 +08001210
1211 /* Loop again with higher rate */
Zhu Yib481de92007-09-25 17:54:57 -07001212 else if (high != IWL_RATE_INVALID) {
1213 start_hi = high;
1214 rate = high;
Ben Cahill77626352007-11-29 11:09:44 +08001215
1216 /* Higher rate not available, use the original */
Zhu Yib481de92007-09-25 17:54:57 -07001217 } else {
Guy Cohen90d77952008-09-09 10:54:53 +08001218 new_rate = rate;
Zhu Yib481de92007-09-25 17:54:57 -07001219 break;
1220 }
1221 }
1222 }
1223
1224 return new_rate;
1225}
Zhu Yib481de92007-09-25 17:54:57 -07001226
Ben Cahill77626352007-11-29 11:09:44 +08001227/*
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001228 * Set up search table for MIMO2
Ben Cahill77626352007-11-29 11:09:44 +08001229 */
Guy Cohenfde0db32008-04-21 15:42:01 -07001230static int rs_switch_to_mimo2(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001231 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001232 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001233 struct ieee80211_sta *sta,
Tomas Winklere227cea2008-07-18 13:53:05 +08001234 struct iwl_scale_tbl_info *tbl, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001235{
Zhu Yib481de92007-09-25 17:54:57 -07001236 u16 rate_mask;
1237 s32 rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001238 s8 is_green = lq_sta->is_green;
Johannes Berg7e6a5882010-08-23 10:46:46 +02001239 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
1240 struct iwl_rxon_context *ctx = sta_priv->common.ctx;
Zhu Yib481de92007-09-25 17:54:57 -07001241
Luis R. Rodriguezde27e642008-12-23 15:58:44 -08001242 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
Zhu Yib481de92007-09-25 17:54:57 -07001243 return -1;
1244
Johannes Bergd9fe60d2008-10-09 12:13:49 +02001245 if (((sta->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 2)
Tomas Winkler00c5ae22008-09-03 11:26:42 +08001246 == WLAN_HT_CAP_SM_PS_STATIC)
Zhu Yib481de92007-09-25 17:54:57 -07001247 return -1;
1248
Ben Cahill77626352007-11-29 11:09:44 +08001249 /* Need both Tx chains/antennas to support MIMO */
Guy Cohenaade00c2008-04-23 17:14:59 -07001250 if (priv->hw_params.tx_chains_num < 2)
Zhu Yib481de92007-09-25 17:54:57 -07001251 return -1;
1252
Tomas Winklere1623442009-01-27 14:27:56 -08001253 IWL_DEBUG_RATE(priv, "LQ: try to switch to MIMO2\n");
Guy Cohen39e88502008-04-23 17:14:57 -07001254
1255 tbl->lq_type = LQ_MIMO2;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001256 tbl->is_dup = lq_sta->is_dup;
Zhu Yib481de92007-09-25 17:54:57 -07001257 tbl->action = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001258 tbl->max_search = IWL_MAX_SEARCH;
Guy Cohen39e88502008-04-23 17:14:57 -07001259 rate_mask = lq_sta->active_mimo2_rate;
1260
Johannes Berg7e6a5882010-08-23 10:46:46 +02001261 if (iwl_is_ht40_tx_allowed(priv, ctx, &sta->ht_cap))
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001262 tbl->is_ht40 = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001263 else
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001264 tbl->is_ht40 = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001265
Guy Coheneecd6e52008-04-23 17:14:58 -07001266 rs_set_expected_tpt_table(lq_sta, tbl);
Zhu Yib481de92007-09-25 17:54:57 -07001267
Guy Cohenf935a6d2008-04-23 17:15:02 -07001268 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
Zhu Yib481de92007-09-25 17:54:57 -07001269
Tomas Winklere1623442009-01-27 14:27:56 -08001270 IWL_DEBUG_RATE(priv, "LQ: MIMO2 best rate %d mask %X\n", rate, rate_mask);
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001271 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1272 IWL_DEBUG_RATE(priv, "Can't switch with index %d rate mask %x\n",
1273 rate, rate_mask);
1274 return -1;
1275 }
1276 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
Guy Cohen39e88502008-04-23 17:14:57 -07001277
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001278 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
1279 tbl->current_rate, is_green);
1280 return 0;
1281}
1282
1283/*
1284 * Set up search table for MIMO3
1285 */
1286static int rs_switch_to_mimo3(struct iwl_priv *priv,
1287 struct iwl_lq_sta *lq_sta,
1288 struct ieee80211_conf *conf,
1289 struct ieee80211_sta *sta,
1290 struct iwl_scale_tbl_info *tbl, int index)
1291{
1292 u16 rate_mask;
1293 s32 rate;
1294 s8 is_green = lq_sta->is_green;
Johannes Berg7e6a5882010-08-23 10:46:46 +02001295 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
1296 struct iwl_rxon_context *ctx = sta_priv->common.ctx;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001297
1298 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
1299 return -1;
1300
1301 if (((sta->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 2)
1302 == WLAN_HT_CAP_SM_PS_STATIC)
1303 return -1;
1304
1305 /* Need both Tx chains/antennas to support MIMO */
1306 if (priv->hw_params.tx_chains_num < 3)
1307 return -1;
1308
1309 IWL_DEBUG_RATE(priv, "LQ: try to switch to MIMO3\n");
1310
1311 tbl->lq_type = LQ_MIMO3;
1312 tbl->is_dup = lq_sta->is_dup;
1313 tbl->action = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001314 tbl->max_search = IWL_MAX_11N_MIMO3_SEARCH;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001315 rate_mask = lq_sta->active_mimo3_rate;
1316
Johannes Berg7e6a5882010-08-23 10:46:46 +02001317 if (iwl_is_ht40_tx_allowed(priv, ctx, &sta->ht_cap))
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001318 tbl->is_ht40 = 1;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001319 else
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001320 tbl->is_ht40 = 0;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001321
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001322 rs_set_expected_tpt_table(lq_sta, tbl);
1323
1324 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
1325
1326 IWL_DEBUG_RATE(priv, "LQ: MIMO3 best rate %d mask %X\n",
1327 rate, rate_mask);
Guy Cohen39e88502008-04-23 17:14:57 -07001328 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
Tomas Winklere1623442009-01-27 14:27:56 -08001329 IWL_DEBUG_RATE(priv, "Can't switch with index %d rate mask %x\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001330 rate, rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07001331 return -1;
Guy Cohen39e88502008-04-23 17:14:57 -07001332 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001333 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
Zhu Yib481de92007-09-25 17:54:57 -07001334
Tomas Winklere1623442009-01-27 14:27:56 -08001335 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001336 tbl->current_rate, is_green);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001337 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07001338}
1339
Ben Cahill77626352007-11-29 11:09:44 +08001340/*
1341 * Set up search table for SISO
1342 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001343static int rs_switch_to_siso(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001344 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001345 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001346 struct ieee80211_sta *sta,
Tomas Winklere227cea2008-07-18 13:53:05 +08001347 struct iwl_scale_tbl_info *tbl, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001348{
Zhu Yib481de92007-09-25 17:54:57 -07001349 u16 rate_mask;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001350 u8 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07001351 s32 rate;
Johannes Berg7e6a5882010-08-23 10:46:46 +02001352 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
1353 struct iwl_rxon_context *ctx = sta_priv->common.ctx;
Zhu Yib481de92007-09-25 17:54:57 -07001354
Luis R. Rodriguezde27e642008-12-23 15:58:44 -08001355 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
Zhu Yib481de92007-09-25 17:54:57 -07001356 return -1;
1357
Tomas Winklere1623442009-01-27 14:27:56 -08001358 IWL_DEBUG_RATE(priv, "LQ: try to switch to SISO\n");
Guy Cohen39e88502008-04-23 17:14:57 -07001359
Tomas Winklerc33104f2008-01-14 17:46:21 -08001360 tbl->is_dup = lq_sta->is_dup;
Zhu Yib481de92007-09-25 17:54:57 -07001361 tbl->lq_type = LQ_SISO;
1362 tbl->action = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001363 tbl->max_search = IWL_MAX_SEARCH;
Guy Cohen39e88502008-04-23 17:14:57 -07001364 rate_mask = lq_sta->active_siso_rate;
Zhu Yib481de92007-09-25 17:54:57 -07001365
Johannes Berg7e6a5882010-08-23 10:46:46 +02001366 if (iwl_is_ht40_tx_allowed(priv, ctx, &sta->ht_cap))
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001367 tbl->is_ht40 = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001368 else
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07001369 tbl->is_ht40 = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001370
Zhu Yib481de92007-09-25 17:54:57 -07001371 if (is_green)
Guy Cohen39e88502008-04-23 17:14:57 -07001372 tbl->is_SGI = 0; /*11n spec: no SGI in SISO+Greenfield*/
Zhu Yib481de92007-09-25 17:54:57 -07001373
Guy Coheneecd6e52008-04-23 17:14:58 -07001374 rs_set_expected_tpt_table(lq_sta, tbl);
Guy Cohenf935a6d2008-04-23 17:15:02 -07001375 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
Zhu Yib481de92007-09-25 17:54:57 -07001376
Tomas Winklere1623442009-01-27 14:27:56 -08001377 IWL_DEBUG_RATE(priv, "LQ: get best rate %d mask %X\n", rate, rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07001378 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
Tomas Winklere1623442009-01-27 14:27:56 -08001379 IWL_DEBUG_RATE(priv, "can not switch with index %d rate mask %x\n",
Zhu Yib481de92007-09-25 17:54:57 -07001380 rate, rate_mask);
1381 return -1;
1382 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001383 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
Tomas Winklere1623442009-01-27 14:27:56 -08001384 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001385 tbl->current_rate, is_green);
Mohamed Abbas403ab562007-11-06 22:06:25 -08001386 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07001387}
1388
Ben Cahill77626352007-11-29 11:09:44 +08001389/*
1390 * Try to switch to new modulation mode from legacy
1391 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001392static int rs_move_legacy_other(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001393 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001394 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001395 struct ieee80211_sta *sta,
Zhu Yib481de92007-09-25 17:54:57 -07001396 int index)
1397{
Tomas Winklere227cea2008-07-18 13:53:05 +08001398 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1399 struct iwl_scale_tbl_info *search_tbl =
1400 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1401 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1402 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1403 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Wey-Yi Guyb1a78852010-07-31 08:52:03 -07001404 u8 start_action;
Guy Cohenfde0db32008-04-21 15:42:01 -07001405 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
Guy Cohen3110bef2008-09-09 10:54:54 +08001406 u8 tx_chains_num = priv->hw_params.tx_chains_num;
Guy Cohenfde0db32008-04-21 15:42:01 -07001407 int ret = 0;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001408 u8 update_search_tbl_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001409
Johannes Berg290f5992010-08-23 07:56:58 -07001410 switch (priv->bt_traffic_load) {
1411 case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
1412 /* nothing */
1413 break;
1414 case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
1415 /* avoid antenna B unless MIMO */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001416 valid_tx_ant = first_antenna(priv->hw_params.valid_tx_ant);
Johannes Berg290f5992010-08-23 07:56:58 -07001417 if (tbl->action == IWL_LEGACY_SWITCH_ANTENNA2)
1418 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
1419 break;
1420 case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
1421 case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
1422 /* avoid antenna B and MIMO */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001423 valid_tx_ant = first_antenna(priv->hw_params.valid_tx_ant);
Johannes Berg290f5992010-08-23 07:56:58 -07001424 if (tbl->action >= IWL_LEGACY_SWITCH_ANTENNA2 &&
1425 tbl->action != IWL_LEGACY_SWITCH_SISO)
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001426 tbl->action = IWL_LEGACY_SWITCH_SISO;
Johannes Berg290f5992010-08-23 07:56:58 -07001427 break;
1428 default:
1429 IWL_ERR(priv, "Invalid BT load %d", priv->bt_traffic_load);
1430 break;
1431 }
1432
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001433 if (!iwl_ht_enabled(priv))
1434 /* stay in Legacy */
1435 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
Johannes Berg3ad3b922009-08-07 15:41:48 -07001436 else if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001437 tbl->action > IWL_LEGACY_SWITCH_SISO)
1438 tbl->action = IWL_LEGACY_SWITCH_SISO;
Wey-Yi Guybee008b2010-08-23 07:57:04 -07001439
1440 /* configure as 1x1 if bt full concurrency */
1441 if (priv->bt_full_concurrent) {
1442 if (!iwl_ht_enabled(priv))
1443 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
1444 else if (tbl->action >= IWL_LEGACY_SWITCH_ANTENNA2)
1445 tbl->action = IWL_LEGACY_SWITCH_SISO;
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001446 valid_tx_ant = first_antenna(priv->hw_params.valid_tx_ant);
Wey-Yi Guybee008b2010-08-23 07:57:04 -07001447 }
1448
Wey-Yi Guyb1a78852010-07-31 08:52:03 -07001449 start_action = tbl->action;
Zhu Yib481de92007-09-25 17:54:57 -07001450 for (; ;) {
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001451 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001452 switch (tbl->action) {
Guy Cohen3110bef2008-09-09 10:54:54 +08001453 case IWL_LEGACY_SWITCH_ANTENNA1:
1454 case IWL_LEGACY_SWITCH_ANTENNA2:
Tomas Winklere1623442009-01-27 14:27:56 -08001455 IWL_DEBUG_RATE(priv, "LQ: Legacy toggle Antenna\n");
Zhu Yib481de92007-09-25 17:54:57 -07001456
Guy Cohen3110bef2008-09-09 10:54:54 +08001457 if ((tbl->action == IWL_LEGACY_SWITCH_ANTENNA1 &&
1458 tx_chains_num <= 1) ||
1459 (tbl->action == IWL_LEGACY_SWITCH_ANTENNA2 &&
1460 tx_chains_num <= 2))
1461 break;
1462
Ben Cahill77626352007-11-29 11:09:44 +08001463 /* Don't change antenna if success has been great */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001464 if (window->success_ratio >= IWL_RS_GOOD_RATIO &&
1465 !priv->bt_full_concurrent &&
1466 priv->bt_traffic_load ==
1467 IWL_BT_COEX_TRAFFIC_LOAD_NONE)
Zhu Yib481de92007-09-25 17:54:57 -07001468 break;
Ben Cahill77626352007-11-29 11:09:44 +08001469
Ben Cahill77626352007-11-29 11:09:44 +08001470 /* Set up search table to try other antenna */
Zhu Yib481de92007-09-25 17:54:57 -07001471 memcpy(search_tbl, tbl, sz);
1472
Guy Cohenaade00c2008-04-23 17:14:59 -07001473 if (rs_toggle_antenna(valid_tx_ant,
1474 &search_tbl->current_rate, search_tbl)) {
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001475 update_search_tbl_counter = 1;
Guy Cohen3110bef2008-09-09 10:54:54 +08001476 rs_set_expected_tpt_table(lq_sta, search_tbl);
Guy Cohenaade00c2008-04-23 17:14:59 -07001477 goto out;
1478 }
Guy Cohena5e8b502008-05-29 16:35:11 +08001479 break;
Zhu Yib481de92007-09-25 17:54:57 -07001480 case IWL_LEGACY_SWITCH_SISO:
Tomas Winklere1623442009-01-27 14:27:56 -08001481 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to SISO\n");
Ben Cahill77626352007-11-29 11:09:44 +08001482
1483 /* Set up search table to try SISO */
Zhu Yib481de92007-09-25 17:54:57 -07001484 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001485 search_tbl->is_SGI = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001486 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001487 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001488 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001489 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001490 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001491 }
Zhu Yib481de92007-09-25 17:54:57 -07001492
1493 break;
Guy Cohen3110bef2008-09-09 10:54:54 +08001494 case IWL_LEGACY_SWITCH_MIMO2_AB:
1495 case IWL_LEGACY_SWITCH_MIMO2_AC:
1496 case IWL_LEGACY_SWITCH_MIMO2_BC:
Tomas Winklere1623442009-01-27 14:27:56 -08001497 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to MIMO2\n");
Ben Cahill77626352007-11-29 11:09:44 +08001498
1499 /* Set up search table to try MIMO */
Zhu Yib481de92007-09-25 17:54:57 -07001500 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001501 search_tbl->is_SGI = 0;
Guy Cohen3110bef2008-09-09 10:54:54 +08001502
1503 if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AB)
1504 search_tbl->ant_type = ANT_AB;
1505 else if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AC)
1506 search_tbl->ant_type = ANT_AC;
1507 else
1508 search_tbl->ant_type = ANT_BC;
1509
1510 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1511 break;
1512
Guy Cohenfde0db32008-04-21 15:42:01 -07001513 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001514 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001515 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001516 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001517 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001518 }
Zhu Yib481de92007-09-25 17:54:57 -07001519 break;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001520
1521 case IWL_LEGACY_SWITCH_MIMO3_ABC:
1522 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to MIMO3\n");
1523
1524 /* Set up search table to try MIMO3 */
1525 memcpy(search_tbl, tbl, sz);
1526 search_tbl->is_SGI = 0;
1527
1528 search_tbl->ant_type = ANT_ABC;
1529
1530 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1531 break;
1532
1533 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1534 search_tbl, index);
1535 if (!ret) {
1536 lq_sta->action_counter = 0;
1537 goto out;
1538 }
1539 break;
Zhu Yib481de92007-09-25 17:54:57 -07001540 }
1541 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001542 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001543 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
Zhu Yib481de92007-09-25 17:54:57 -07001544
1545 if (tbl->action == start_action)
1546 break;
1547
1548 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001549 search_tbl->lq_type = LQ_NONE;
Zhu Yib481de92007-09-25 17:54:57 -07001550 return 0;
1551
Guy Cohen3110bef2008-09-09 10:54:54 +08001552out:
1553 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001554 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001555 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001556 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001557 if (update_search_tbl_counter)
1558 search_tbl->action = tbl->action;
Zhu Yib481de92007-09-25 17:54:57 -07001559 return 0;
1560
1561}
1562
Ben Cahill77626352007-11-29 11:09:44 +08001563/*
1564 * Try to switch to new modulation mode from SISO
1565 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001566static int rs_move_siso_to_other(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001567 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001568 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001569 struct ieee80211_sta *sta, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001570{
Tomas Winklerc33104f2008-01-14 17:46:21 -08001571 u8 is_green = lq_sta->is_green;
Tomas Winklere227cea2008-07-18 13:53:05 +08001572 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1573 struct iwl_scale_tbl_info *search_tbl =
1574 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1575 struct iwl_rate_scale_data *window = &(tbl->win[index]);
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001576 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Tomas Winklere227cea2008-07-18 13:53:05 +08001577 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1578 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Wey-Yi Guyb1a78852010-07-31 08:52:03 -07001579 u8 start_action;
Guy Cohenfde0db32008-04-21 15:42:01 -07001580 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
Guy Cohen3110bef2008-09-09 10:54:54 +08001581 u8 tx_chains_num = priv->hw_params.tx_chains_num;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001582 u8 update_search_tbl_counter = 0;
Guy Cohenfde0db32008-04-21 15:42:01 -07001583 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001584
Johannes Berg290f5992010-08-23 07:56:58 -07001585 switch (priv->bt_traffic_load) {
1586 case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
1587 /* nothing */
1588 break;
1589 case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
1590 /* avoid antenna B unless MIMO */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001591 valid_tx_ant = first_antenna(priv->hw_params.valid_tx_ant);
Johannes Berg290f5992010-08-23 07:56:58 -07001592 if (tbl->action == IWL_SISO_SWITCH_ANTENNA2)
1593 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1594 break;
1595 case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
1596 case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
1597 /* avoid antenna B and MIMO */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001598 valid_tx_ant = first_antenna(priv->hw_params.valid_tx_ant);
1599 if (tbl->action != IWL_SISO_SWITCH_ANTENNA1)
Johannes Berg290f5992010-08-23 07:56:58 -07001600 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1601 break;
1602 default:
1603 IWL_ERR(priv, "Invalid BT load %d", priv->bt_traffic_load);
1604 break;
1605 }
1606
Johannes Berg3ad3b922009-08-07 15:41:48 -07001607 if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001608 tbl->action > IWL_SISO_SWITCH_ANTENNA2) {
1609 /* stay in SISO */
1610 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1611 }
Wey-Yi Guybee008b2010-08-23 07:57:04 -07001612
1613 /* configure as 1x1 if bt full concurrency */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001614 if (priv->bt_full_concurrent) {
1615 valid_tx_ant = first_antenna(priv->hw_params.valid_tx_ant);
1616 if (tbl->action >= IWL_LEGACY_SWITCH_ANTENNA2)
1617 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1618 }
Wey-Yi Guybee008b2010-08-23 07:57:04 -07001619
Wey-Yi Guyb1a78852010-07-31 08:52:03 -07001620 start_action = tbl->action;
Zhu Yib481de92007-09-25 17:54:57 -07001621 for (;;) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001622 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001623 switch (tbl->action) {
Guy Cohen3110bef2008-09-09 10:54:54 +08001624 case IWL_SISO_SWITCH_ANTENNA1:
1625 case IWL_SISO_SWITCH_ANTENNA2:
Tomas Winklere1623442009-01-27 14:27:56 -08001626 IWL_DEBUG_RATE(priv, "LQ: SISO toggle Antenna\n");
Guy Cohen3110bef2008-09-09 10:54:54 +08001627 if ((tbl->action == IWL_SISO_SWITCH_ANTENNA1 &&
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001628 tx_chains_num <= 1) ||
Guy Cohen3110bef2008-09-09 10:54:54 +08001629 (tbl->action == IWL_SISO_SWITCH_ANTENNA2 &&
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001630 tx_chains_num <= 2))
Guy Cohen3110bef2008-09-09 10:54:54 +08001631 break;
1632
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001633 if (window->success_ratio >= IWL_RS_GOOD_RATIO &&
1634 !priv->bt_full_concurrent &&
1635 priv->bt_traffic_load ==
1636 IWL_BT_COEX_TRAFFIC_LOAD_NONE)
Zhu Yib481de92007-09-25 17:54:57 -07001637 break;
Zhu Yib481de92007-09-25 17:54:57 -07001638
1639 memcpy(search_tbl, tbl, sz);
Guy Cohenaade00c2008-04-23 17:14:59 -07001640 if (rs_toggle_antenna(valid_tx_ant,
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001641 &search_tbl->current_rate, search_tbl)) {
1642 update_search_tbl_counter = 1;
Guy Cohenaade00c2008-04-23 17:14:59 -07001643 goto out;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001644 }
Guy Cohena5e8b502008-05-29 16:35:11 +08001645 break;
Guy Cohen3110bef2008-09-09 10:54:54 +08001646 case IWL_SISO_SWITCH_MIMO2_AB:
1647 case IWL_SISO_SWITCH_MIMO2_AC:
1648 case IWL_SISO_SWITCH_MIMO2_BC:
Tomas Winklere1623442009-01-27 14:27:56 -08001649 IWL_DEBUG_RATE(priv, "LQ: SISO switch to MIMO2\n");
Zhu Yib481de92007-09-25 17:54:57 -07001650 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001651 search_tbl->is_SGI = 0;
Guy Cohen3110bef2008-09-09 10:54:54 +08001652
1653 if (tbl->action == IWL_SISO_SWITCH_MIMO2_AB)
1654 search_tbl->ant_type = ANT_AB;
1655 else if (tbl->action == IWL_SISO_SWITCH_MIMO2_AC)
1656 search_tbl->ant_type = ANT_AC;
1657 else
1658 search_tbl->ant_type = ANT_BC;
1659
1660 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1661 break;
1662
Guy Cohenfde0db32008-04-21 15:42:01 -07001663 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001664 search_tbl, index);
Guy Cohen3110bef2008-09-09 10:54:54 +08001665 if (!ret)
Zhu Yib481de92007-09-25 17:54:57 -07001666 goto out;
1667 break;
1668 case IWL_SISO_SWITCH_GI:
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001669 if (!tbl->is_ht40 && !(ht_cap->cap &
1670 IEEE80211_HT_CAP_SGI_20))
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001671 break;
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001672 if (tbl->is_ht40 && !(ht_cap->cap &
1673 IEEE80211_HT_CAP_SGI_40))
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001674 break;
1675
Tomas Winklere1623442009-01-27 14:27:56 -08001676 IWL_DEBUG_RATE(priv, "LQ: SISO toggle SGI/NGI\n");
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001677
Zhu Yib481de92007-09-25 17:54:57 -07001678 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001679 if (is_green) {
1680 if (!tbl->is_SGI)
1681 break;
1682 else
Winkler, Tomas15b16872008-12-19 10:37:33 +08001683 IWL_ERR(priv,
1684 "SGI was set in GF+SISO\n");
Zhu Yib481de92007-09-25 17:54:57 -07001685 }
Guy Cohen39e88502008-04-23 17:14:57 -07001686 search_tbl->is_SGI = !tbl->is_SGI;
Guy Coheneecd6e52008-04-23 17:14:58 -07001687 rs_set_expected_tpt_table(lq_sta, search_tbl);
Guy Cohen39e88502008-04-23 17:14:57 -07001688 if (tbl->is_SGI) {
1689 s32 tpt = lq_sta->last_tpt / 100;
1690 if (tpt >= search_tbl->expected_tpt[index])
1691 break;
1692 }
Tomas Winkler978785a2008-12-19 10:37:31 +08001693 search_tbl->current_rate =
1694 rate_n_flags_from_tbl(priv, search_tbl,
1695 index, is_green);
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001696 update_search_tbl_counter = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001697 goto out;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001698 case IWL_SISO_SWITCH_MIMO3_ABC:
1699 IWL_DEBUG_RATE(priv, "LQ: SISO switch to MIMO3\n");
1700 memcpy(search_tbl, tbl, sz);
1701 search_tbl->is_SGI = 0;
1702 search_tbl->ant_type = ANT_ABC;
1703
1704 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1705 break;
1706
1707 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1708 search_tbl, index);
1709 if (!ret)
1710 goto out;
1711 break;
Zhu Yib481de92007-09-25 17:54:57 -07001712 }
1713 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001714 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001715 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
Zhu Yib481de92007-09-25 17:54:57 -07001716
1717 if (tbl->action == start_action)
1718 break;
1719 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001720 search_tbl->lq_type = LQ_NONE;
Zhu Yib481de92007-09-25 17:54:57 -07001721 return 0;
1722
1723 out:
Guy Cohen3110bef2008-09-09 10:54:54 +08001724 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001725 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001726 if (tbl->action > IWL_SISO_SWITCH_MIMO3_ABC)
Guy Cohen3110bef2008-09-09 10:54:54 +08001727 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001728 if (update_search_tbl_counter)
1729 search_tbl->action = tbl->action;
1730
Zhu Yib481de92007-09-25 17:54:57 -07001731 return 0;
1732}
1733
Ben Cahill77626352007-11-29 11:09:44 +08001734/*
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001735 * Try to switch to new modulation mode from MIMO2
Ben Cahill77626352007-11-29 11:09:44 +08001736 */
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001737static int rs_move_mimo2_to_other(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08001738 struct iwl_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001739 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001740 struct ieee80211_sta *sta, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001741{
Tomas Winklerc33104f2008-01-14 17:46:21 -08001742 s8 is_green = lq_sta->is_green;
Tomas Winklere227cea2008-07-18 13:53:05 +08001743 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1744 struct iwl_scale_tbl_info *search_tbl =
1745 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Guy Cohen3110bef2008-09-09 10:54:54 +08001746 struct iwl_rate_scale_data *window = &(tbl->win[index]);
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001747 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Tomas Winklere227cea2008-07-18 13:53:05 +08001748 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1749 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Wey-Yi Guyb1a78852010-07-31 08:52:03 -07001750 u8 start_action;
Guy Cohen3110bef2008-09-09 10:54:54 +08001751 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1752 u8 tx_chains_num = priv->hw_params.tx_chains_num;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001753 u8 update_search_tbl_counter = 0;
Guy Cohenaade00c2008-04-23 17:14:59 -07001754 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001755
Johannes Berg290f5992010-08-23 07:56:58 -07001756 switch (priv->bt_traffic_load) {
1757 case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
1758 /* nothing */
1759 break;
1760 case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
1761 case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
1762 /* avoid antenna B and MIMO */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001763 if (tbl->action != IWL_MIMO2_SWITCH_SISO_A)
1764 tbl->action = IWL_MIMO2_SWITCH_SISO_A;
1765 break;
Johannes Berg290f5992010-08-23 07:56:58 -07001766 case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
1767 /* avoid antenna B unless MIMO */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001768 if (tbl->action == IWL_MIMO2_SWITCH_SISO_B ||
1769 tbl->action == IWL_MIMO2_SWITCH_SISO_C)
Johannes Berg290f5992010-08-23 07:56:58 -07001770 tbl->action = IWL_MIMO2_SWITCH_SISO_A;
1771 break;
1772 default:
1773 IWL_ERR(priv, "Invalid BT load %d", priv->bt_traffic_load);
1774 break;
1775 }
1776
Johannes Berg3ad3b922009-08-07 15:41:48 -07001777 if ((iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE) &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001778 (tbl->action < IWL_MIMO2_SWITCH_SISO_A ||
1779 tbl->action > IWL_MIMO2_SWITCH_SISO_C)) {
1780 /* switch in SISO */
1781 tbl->action = IWL_MIMO2_SWITCH_SISO_A;
1782 }
Wey-Yi Guybee008b2010-08-23 07:57:04 -07001783
1784 /* configure as 1x1 if bt full concurrency */
1785 if (priv->bt_full_concurrent &&
1786 (tbl->action < IWL_MIMO2_SWITCH_SISO_A ||
1787 tbl->action > IWL_MIMO2_SWITCH_SISO_C))
1788 tbl->action = IWL_MIMO2_SWITCH_SISO_A;
1789
Wey-Yi Guyb1a78852010-07-31 08:52:03 -07001790 start_action = tbl->action;
Zhu Yib481de92007-09-25 17:54:57 -07001791 for (;;) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001792 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001793 switch (tbl->action) {
Guy Cohen3110bef2008-09-09 10:54:54 +08001794 case IWL_MIMO2_SWITCH_ANTENNA1:
1795 case IWL_MIMO2_SWITCH_ANTENNA2:
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001796 IWL_DEBUG_RATE(priv, "LQ: MIMO2 toggle Antennas\n");
Guy Cohen3110bef2008-09-09 10:54:54 +08001797
1798 if (tx_chains_num <= 2)
1799 break;
1800
1801 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1802 break;
1803
1804 memcpy(search_tbl, tbl, sz);
1805 if (rs_toggle_antenna(valid_tx_ant,
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001806 &search_tbl->current_rate, search_tbl)) {
1807 update_search_tbl_counter = 1;
Guy Cohen3110bef2008-09-09 10:54:54 +08001808 goto out;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001809 }
Guy Cohen3110bef2008-09-09 10:54:54 +08001810 break;
1811 case IWL_MIMO2_SWITCH_SISO_A:
1812 case IWL_MIMO2_SWITCH_SISO_B:
1813 case IWL_MIMO2_SWITCH_SISO_C:
Tomas Winklere1623442009-01-27 14:27:56 -08001814 IWL_DEBUG_RATE(priv, "LQ: MIMO2 switch to SISO\n");
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001815
Ben Cahill77626352007-11-29 11:09:44 +08001816 /* Set up new search table for SISO */
Zhu Yib481de92007-09-25 17:54:57 -07001817 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001818
Guy Cohen3110bef2008-09-09 10:54:54 +08001819 if (tbl->action == IWL_MIMO2_SWITCH_SISO_A)
Guy Cohenfde0db32008-04-21 15:42:01 -07001820 search_tbl->ant_type = ANT_A;
Guy Cohen3110bef2008-09-09 10:54:54 +08001821 else if (tbl->action == IWL_MIMO2_SWITCH_SISO_B)
Guy Cohenfde0db32008-04-21 15:42:01 -07001822 search_tbl->ant_type = ANT_B;
Guy Cohen3110bef2008-09-09 10:54:54 +08001823 else
1824 search_tbl->ant_type = ANT_C;
1825
1826 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1827 break;
Zhu Yib481de92007-09-25 17:54:57 -07001828
Tomas Winklerc33104f2008-01-14 17:46:21 -08001829 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001830 search_tbl, index);
Guy Cohen3110bef2008-09-09 10:54:54 +08001831 if (!ret)
Zhu Yib481de92007-09-25 17:54:57 -07001832 goto out;
Guy Cohen3110bef2008-09-09 10:54:54 +08001833
Zhu Yib481de92007-09-25 17:54:57 -07001834 break;
1835
Guy Cohen3110bef2008-09-09 10:54:54 +08001836 case IWL_MIMO2_SWITCH_GI:
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001837 if (!tbl->is_ht40 && !(ht_cap->cap &
1838 IEEE80211_HT_CAP_SGI_20))
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001839 break;
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001840 if (tbl->is_ht40 && !(ht_cap->cap &
1841 IEEE80211_HT_CAP_SGI_40))
Emmanuel Grumbacha9841012008-05-15 13:54:08 +08001842 break;
1843
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001844 IWL_DEBUG_RATE(priv, "LQ: MIMO2 toggle SGI/NGI\n");
1845
1846 /* Set up new search table for MIMO2 */
1847 memcpy(search_tbl, tbl, sz);
1848 search_tbl->is_SGI = !tbl->is_SGI;
1849 rs_set_expected_tpt_table(lq_sta, search_tbl);
1850 /*
1851 * If active table already uses the fastest possible
1852 * modulation (dual stream with short guard interval),
1853 * and it's working well, there's no need to look
1854 * for a better type of modulation!
1855 */
1856 if (tbl->is_SGI) {
1857 s32 tpt = lq_sta->last_tpt / 100;
1858 if (tpt >= search_tbl->expected_tpt[index])
1859 break;
1860 }
1861 search_tbl->current_rate =
1862 rate_n_flags_from_tbl(priv, search_tbl,
1863 index, is_green);
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001864 update_search_tbl_counter = 1;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001865 goto out;
1866
1867 case IWL_MIMO2_SWITCH_MIMO3_ABC:
1868 IWL_DEBUG_RATE(priv, "LQ: MIMO2 switch to MIMO3\n");
1869 memcpy(search_tbl, tbl, sz);
1870 search_tbl->is_SGI = 0;
1871 search_tbl->ant_type = ANT_ABC;
1872
1873 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1874 break;
1875
1876 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1877 search_tbl, index);
1878 if (!ret)
1879 goto out;
1880
1881 break;
1882 }
1883 tbl->action++;
1884 if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
1885 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
1886
1887 if (tbl->action == start_action)
1888 break;
1889 }
1890 search_tbl->lq_type = LQ_NONE;
1891 return 0;
1892 out:
1893 lq_sta->search_better_tbl = 1;
1894 tbl->action++;
1895 if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
1896 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001897 if (update_search_tbl_counter)
1898 search_tbl->action = tbl->action;
1899
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001900 return 0;
1901
1902}
1903
1904/*
1905 * Try to switch to new modulation mode from MIMO3
1906 */
1907static int rs_move_mimo3_to_other(struct iwl_priv *priv,
1908 struct iwl_lq_sta *lq_sta,
1909 struct ieee80211_conf *conf,
1910 struct ieee80211_sta *sta, int index)
1911{
1912 s8 is_green = lq_sta->is_green;
1913 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1914 struct iwl_scale_tbl_info *search_tbl =
1915 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1916 struct iwl_rate_scale_data *window = &(tbl->win[index]);
Daniel C Halperin28e6f482009-08-13 13:30:58 -07001917 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001918 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1919 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
Wey-Yi Guyb1a78852010-07-31 08:52:03 -07001920 u8 start_action;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001921 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1922 u8 tx_chains_num = priv->hw_params.tx_chains_num;
1923 int ret;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07001924 u8 update_search_tbl_counter = 0;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001925
Johannes Berg290f5992010-08-23 07:56:58 -07001926 switch (priv->bt_traffic_load) {
1927 case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
1928 /* nothing */
1929 break;
1930 case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
1931 case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
1932 /* avoid antenna B and MIMO */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001933 if (tbl->action != IWL_MIMO3_SWITCH_SISO_A)
Johannes Berg290f5992010-08-23 07:56:58 -07001934 tbl->action = IWL_MIMO3_SWITCH_SISO_A;
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001935 break;
Johannes Berg290f5992010-08-23 07:56:58 -07001936 case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
1937 /* avoid antenna B unless MIMO */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07001938 if (tbl->action == IWL_MIMO3_SWITCH_SISO_B ||
1939 tbl->action == IWL_MIMO3_SWITCH_SISO_C)
Johannes Berg290f5992010-08-23 07:56:58 -07001940 tbl->action = IWL_MIMO3_SWITCH_SISO_A;
Johannes Berg290f5992010-08-23 07:56:58 -07001941 break;
1942 default:
1943 IWL_ERR(priv, "Invalid BT load %d", priv->bt_traffic_load);
1944 break;
1945 }
1946
Johannes Berg3ad3b922009-08-07 15:41:48 -07001947 if ((iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE) &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07001948 (tbl->action < IWL_MIMO3_SWITCH_SISO_A ||
1949 tbl->action > IWL_MIMO3_SWITCH_SISO_C)) {
1950 /* switch in SISO */
1951 tbl->action = IWL_MIMO3_SWITCH_SISO_A;
1952 }
Wey-Yi Guybee008b2010-08-23 07:57:04 -07001953
1954 /* configure as 1x1 if bt full concurrency */
1955 if (priv->bt_full_concurrent &&
1956 (tbl->action < IWL_MIMO3_SWITCH_SISO_A ||
1957 tbl->action > IWL_MIMO3_SWITCH_SISO_C))
1958 tbl->action = IWL_MIMO3_SWITCH_SISO_A;
1959
Wey-Yi Guyb1a78852010-07-31 08:52:03 -07001960 start_action = tbl->action;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07001961 for (;;) {
1962 lq_sta->action_counter++;
1963 switch (tbl->action) {
1964 case IWL_MIMO3_SWITCH_ANTENNA1:
1965 case IWL_MIMO3_SWITCH_ANTENNA2:
1966 IWL_DEBUG_RATE(priv, "LQ: MIMO3 toggle Antennas\n");
1967
1968 if (tx_chains_num <= 3)
1969 break;
1970
1971 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1972 break;
1973
1974 memcpy(search_tbl, tbl, sz);
1975 if (rs_toggle_antenna(valid_tx_ant,
1976 &search_tbl->current_rate, search_tbl))
1977 goto out;
1978 break;
1979 case IWL_MIMO3_SWITCH_SISO_A:
1980 case IWL_MIMO3_SWITCH_SISO_B:
1981 case IWL_MIMO3_SWITCH_SISO_C:
1982 IWL_DEBUG_RATE(priv, "LQ: MIMO3 switch to SISO\n");
1983
1984 /* Set up new search table for SISO */
1985 memcpy(search_tbl, tbl, sz);
1986
1987 if (tbl->action == IWL_MIMO3_SWITCH_SISO_A)
1988 search_tbl->ant_type = ANT_A;
1989 else if (tbl->action == IWL_MIMO3_SWITCH_SISO_B)
1990 search_tbl->ant_type = ANT_B;
1991 else
1992 search_tbl->ant_type = ANT_C;
1993
1994 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1995 break;
1996
1997 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
1998 search_tbl, index);
1999 if (!ret)
2000 goto out;
2001
2002 break;
2003
2004 case IWL_MIMO3_SWITCH_MIMO2_AB:
2005 case IWL_MIMO3_SWITCH_MIMO2_AC:
2006 case IWL_MIMO3_SWITCH_MIMO2_BC:
2007 IWL_DEBUG_RATE(priv, "LQ: MIMO3 switch to MIMO2\n");
2008
2009 memcpy(search_tbl, tbl, sz);
2010 search_tbl->is_SGI = 0;
2011 if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AB)
2012 search_tbl->ant_type = ANT_AB;
2013 else if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AC)
2014 search_tbl->ant_type = ANT_AC;
2015 else
2016 search_tbl->ant_type = ANT_BC;
2017
2018 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
2019 break;
2020
2021 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
2022 search_tbl, index);
2023 if (!ret)
2024 goto out;
2025
2026 break;
2027
2028 case IWL_MIMO3_SWITCH_GI:
Daniel C Halperin28e6f482009-08-13 13:30:58 -07002029 if (!tbl->is_ht40 && !(ht_cap->cap &
2030 IEEE80211_HT_CAP_SGI_20))
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07002031 break;
Daniel C Halperin28e6f482009-08-13 13:30:58 -07002032 if (tbl->is_ht40 && !(ht_cap->cap &
2033 IEEE80211_HT_CAP_SGI_40))
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07002034 break;
2035
2036 IWL_DEBUG_RATE(priv, "LQ: MIMO3 toggle SGI/NGI\n");
Ben Cahill77626352007-11-29 11:09:44 +08002037
2038 /* Set up new search table for MIMO */
Zhu Yib481de92007-09-25 17:54:57 -07002039 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07002040 search_tbl->is_SGI = !tbl->is_SGI;
Guy Coheneecd6e52008-04-23 17:14:58 -07002041 rs_set_expected_tpt_table(lq_sta, search_tbl);
Ben Cahill77626352007-11-29 11:09:44 +08002042 /*
2043 * If active table already uses the fastest possible
2044 * modulation (dual stream with short guard interval),
2045 * and it's working well, there's no need to look
2046 * for a better type of modulation!
2047 */
Guy Cohen39e88502008-04-23 17:14:57 -07002048 if (tbl->is_SGI) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002049 s32 tpt = lq_sta->last_tpt / 100;
Guy Cohen39e88502008-04-23 17:14:57 -07002050 if (tpt >= search_tbl->expected_tpt[index])
2051 break;
Zhu Yib481de92007-09-25 17:54:57 -07002052 }
Tomas Winkler978785a2008-12-19 10:37:31 +08002053 search_tbl->current_rate =
2054 rate_n_flags_from_tbl(priv, search_tbl,
2055 index, is_green);
Mohamed Abbasd6e93392009-05-08 13:44:39 -07002056 update_search_tbl_counter = 1;
Zhu Yib481de92007-09-25 17:54:57 -07002057 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07002058 }
2059 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07002060 if (tbl->action > IWL_MIMO3_SWITCH_GI)
2061 tbl->action = IWL_MIMO3_SWITCH_ANTENNA1;
Zhu Yib481de92007-09-25 17:54:57 -07002062
2063 if (tbl->action == start_action)
2064 break;
2065 }
Guy Cohen3110bef2008-09-09 10:54:54 +08002066 search_tbl->lq_type = LQ_NONE;
Zhu Yib481de92007-09-25 17:54:57 -07002067 return 0;
2068 out:
Guy Cohen3110bef2008-09-09 10:54:54 +08002069 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07002070 tbl->action++;
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07002071 if (tbl->action > IWL_MIMO3_SWITCH_GI)
2072 tbl->action = IWL_MIMO3_SWITCH_ANTENNA1;
Mohamed Abbasd6e93392009-05-08 13:44:39 -07002073 if (update_search_tbl_counter)
2074 search_tbl->action = tbl->action;
2075
Zhu Yib481de92007-09-25 17:54:57 -07002076 return 0;
2077
2078}
2079
Ben Cahill77626352007-11-29 11:09:44 +08002080/*
2081 * Check whether we should continue using same modulation mode, or
2082 * begin search for a new mode, based on:
2083 * 1) # tx successes or failures while using this mode
2084 * 2) # times calling this function
2085 * 3) elapsed time in this mode (not used, for now)
2086 */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07002087static void rs_stay_in_table(struct iwl_lq_sta *lq_sta, bool force_search)
Zhu Yib481de92007-09-25 17:54:57 -07002088{
Tomas Winklere227cea2008-07-18 13:53:05 +08002089 struct iwl_scale_tbl_info *tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002090 int i;
2091 int active_tbl;
2092 int flush_interval_passed = 0;
Ester Kummerbf403db2008-05-05 10:22:40 +08002093 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07002094
Ester Kummerbf403db2008-05-05 10:22:40 +08002095 priv = lq_sta->drv;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002096 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002097
Tomas Winklerc33104f2008-01-14 17:46:21 -08002098 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07002099
Ben Cahill77626352007-11-29 11:09:44 +08002100 /* If we've been disallowing search, see if we should now allow it */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002101 if (lq_sta->stay_in_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07002102
Ben Cahill77626352007-11-29 11:09:44 +08002103 /* Elapsed time using current modulation mode */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002104 if (lq_sta->flush_timer)
Zhu Yib481de92007-09-25 17:54:57 -07002105 flush_interval_passed =
Mohamed Abbas447fee72009-04-20 14:37:02 -07002106 time_after(jiffies,
2107 (unsigned long)(lq_sta->flush_timer +
Zhu Yib481de92007-09-25 17:54:57 -07002108 IWL_RATE_SCALE_FLUSH_INTVL));
2109
Ben Cahill77626352007-11-29 11:09:44 +08002110 /*
2111 * Check if we should allow search for new modulation mode.
2112 * If many frames have failed or succeeded, or we've used
2113 * this same modulation for a long time, allow search, and
2114 * reset history stats that keep track of whether we should
2115 * allow a new search. Also (below) reset all bitmaps and
2116 * stats in active history.
2117 */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07002118 if (force_search ||
2119 (lq_sta->total_failed > lq_sta->max_failure_limit) ||
Tomas Winklerc33104f2008-01-14 17:46:21 -08002120 (lq_sta->total_success > lq_sta->max_success_limit) ||
2121 ((!lq_sta->search_better_tbl) && (lq_sta->flush_timer)
Zhu Yib481de92007-09-25 17:54:57 -07002122 && (flush_interval_passed))) {
Tomas Winklere1623442009-01-27 14:27:56 -08002123 IWL_DEBUG_RATE(priv, "LQ: stay is expired %d %d %d\n:",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002124 lq_sta->total_failed,
2125 lq_sta->total_success,
Zhu Yib481de92007-09-25 17:54:57 -07002126 flush_interval_passed);
Ben Cahill77626352007-11-29 11:09:44 +08002127
2128 /* Allow search for new mode */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002129 lq_sta->stay_in_tbl = 0; /* only place reset */
2130 lq_sta->total_failed = 0;
2131 lq_sta->total_success = 0;
2132 lq_sta->flush_timer = 0;
Ben Cahill77626352007-11-29 11:09:44 +08002133
2134 /*
2135 * Else if we've used this modulation mode enough repetitions
2136 * (regardless of elapsed time or success/failure), reset
2137 * history bitmaps and rate-specific stats for all rates in
2138 * active table.
2139 */
Mohamed Abbas403ab562007-11-06 22:06:25 -08002140 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002141 lq_sta->table_count++;
2142 if (lq_sta->table_count >=
2143 lq_sta->table_count_limit) {
2144 lq_sta->table_count = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002145
Tomas Winklere1623442009-01-27 14:27:56 -08002146 IWL_DEBUG_RATE(priv, "LQ: stay in table clear win\n");
Zhu Yib481de92007-09-25 17:54:57 -07002147 for (i = 0; i < IWL_RATE_COUNT; i++)
2148 rs_rate_scale_clear_window(
2149 &(tbl->win[i]));
2150 }
2151 }
2152
Ben Cahill77626352007-11-29 11:09:44 +08002153 /* If transitioning to allow "search", reset all history
2154 * bitmaps and stats in active table (this will become the new
2155 * "search" table). */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002156 if (!lq_sta->stay_in_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07002157 for (i = 0; i < IWL_RATE_COUNT; i++)
2158 rs_rate_scale_clear_window(&(tbl->win[i]));
2159 }
2160 }
2161}
2162
Ben Cahill77626352007-11-29 11:09:44 +08002163/*
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07002164 * setup rate table in uCode
2165 * return rate_n_flags as used in the table
2166 */
2167static u32 rs_update_rate_tbl(struct iwl_priv *priv,
Johannes Berg7e6a5882010-08-23 10:46:46 +02002168 struct iwl_rxon_context *ctx,
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07002169 struct iwl_lq_sta *lq_sta,
2170 struct iwl_scale_tbl_info *tbl,
2171 int index, u8 is_green)
2172{
2173 u32 rate;
2174
2175 /* Update uCode's rate table. */
2176 rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
2177 rs_fill_link_cmd(priv, lq_sta, rate);
Johannes Berg7e6a5882010-08-23 10:46:46 +02002178 iwl_send_lq_cmd(priv, ctx, &lq_sta->lq, CMD_ASYNC, false);
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07002179
2180 return rate;
2181}
2182
2183/*
Ben Cahill77626352007-11-29 11:09:44 +08002184 * Do rate scaling and search for new modulation mode.
2185 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002186static void rs_rate_scale_perform(struct iwl_priv *priv,
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002187 struct sk_buff *skb,
Johannes Berg4b7679a2008-09-18 18:14:18 +02002188 struct ieee80211_sta *sta,
2189 struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002190{
Johannes Berg4b7679a2008-09-18 18:14:18 +02002191 struct ieee80211_hw *hw = priv->hw;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002192 struct ieee80211_conf *conf = &hw->conf;
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002193 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2194 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002195 int low = IWL_RATE_INVALID;
2196 int high = IWL_RATE_INVALID;
2197 int index;
2198 int i;
Tomas Winklere227cea2008-07-18 13:53:05 +08002199 struct iwl_rate_scale_data *window = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002200 int current_tpt = IWL_INVALID_VALUE;
2201 int low_tpt = IWL_INVALID_VALUE;
2202 int high_tpt = IWL_INVALID_VALUE;
2203 u32 fail_count;
2204 s8 scale_action = 0;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002205 u16 rate_mask;
Zhu Yib481de92007-09-25 17:54:57 -07002206 u8 update_lq = 0;
Tomas Winklere227cea2008-07-18 13:53:05 +08002207 struct iwl_scale_tbl_info *tbl, *tbl1;
Zhu Yib481de92007-09-25 17:54:57 -07002208 u16 rate_scale_index_msk = 0;
Guy Cohen39e88502008-04-23 17:14:57 -07002209 u32 rate;
Zhu Yib481de92007-09-25 17:54:57 -07002210 u8 is_green = 0;
2211 u8 active_tbl = 0;
2212 u8 done_search = 0;
2213 u16 high_low;
Guy Cohena5e8b502008-05-29 16:35:11 +08002214 s32 sr;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002215 u8 tid = MAX_TID_COUNT;
Wey-Yi Guy86b47662009-03-10 14:35:09 -07002216 struct iwl_tid_data *tid_data;
Johannes Berg7e6a5882010-08-23 10:46:46 +02002217 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
2218 struct iwl_rxon_context *ctx = sta_priv->common.ctx;
Zhu Yib481de92007-09-25 17:54:57 -07002219
Tomas Winklere1623442009-01-27 14:27:56 -08002220 IWL_DEBUG_RATE(priv, "rate scale calculate new rate for skb\n");
Zhu Yib481de92007-09-25 17:54:57 -07002221
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002222 /* Send management frames and NO_ACK data using lowest rate. */
Tomas Winkler417f1142008-11-12 13:14:06 -08002223 /* TODO: this could probably be improved.. */
2224 if (!ieee80211_is_data(hdr->frame_control) ||
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002225 info->flags & IEEE80211_TX_CTL_NO_ACK)
Zhu Yib481de92007-09-25 17:54:57 -07002226 return;
Zhu Yib481de92007-09-25 17:54:57 -07002227
Johannes Berg4b7679a2008-09-18 18:14:18 +02002228 if (!sta || !lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002229 return;
2230
Johannes Berg4b7679a2008-09-18 18:14:18 +02002231 lq_sta->supp_rates = sta->supp_rates[lq_sta->band];
Zhu Yib481de92007-09-25 17:54:57 -07002232
Ron Rindjunskyfaa29712008-06-12 09:46:58 +08002233 tid = rs_tl_add_packet(lq_sta, hdr);
Daniel C Halperine3949d62009-09-17 10:43:50 -07002234 if ((tid != MAX_TID_COUNT) && (lq_sta->tx_agg_tid_en & (1 << tid))) {
2235 tid_data = &priv->stations[lq_sta->lq.sta_id].tid[tid];
2236 if (tid_data->agg.state == IWL_AGG_OFF)
2237 lq_sta->is_agg = 0;
2238 else
2239 lq_sta->is_agg = 1;
2240 } else
2241 lq_sta->is_agg = 0;
Ron Rindjunskyfaa29712008-06-12 09:46:58 +08002242
Ben Cahill77626352007-11-29 11:09:44 +08002243 /*
2244 * Select rate-scale / modulation-mode table to work with in
2245 * the rest of this function: "search" if searching for better
2246 * modulation mode, or "active" if doing rate scaling within a mode.
2247 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002248 if (!lq_sta->search_better_tbl)
2249 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002250 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002251 active_tbl = 1 - lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002252
Tomas Winklerc33104f2008-01-14 17:46:21 -08002253 tbl = &(lq_sta->lq_info[active_tbl]);
Wey-Yi Guy2681b202009-05-21 13:44:22 -07002254 if (is_legacy(tbl->lq_type))
2255 lq_sta->is_green = 0;
2256 else
Johannes Berg7e6a5882010-08-23 10:46:46 +02002257 lq_sta->is_green = rs_use_green(sta);
Tomas Winklerc33104f2008-01-14 17:46:21 -08002258 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07002259
Ben Cahill77626352007-11-29 11:09:44 +08002260 /* current tx rate */
Johannes Bergb7e35002008-09-11 02:22:58 +02002261 index = lq_sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002262
Tomas Winklere1623442009-01-27 14:27:56 -08002263 IWL_DEBUG_RATE(priv, "Rate scale index %d for type %d\n", index,
Zhu Yib481de92007-09-25 17:54:57 -07002264 tbl->lq_type);
2265
Ben Cahill77626352007-11-29 11:09:44 +08002266 /* rates available for this association, and for modulation mode */
Guy Coheneecd6e52008-04-23 17:14:58 -07002267 rate_mask = rs_get_supported_rates(lq_sta, hdr, tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -07002268
Frans Pop91dd6c22010-03-24 14:19:58 -07002269 IWL_DEBUG_RATE(priv, "mask 0x%04X\n", rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07002270
2271 /* mask with station rate restriction */
2272 if (is_legacy(tbl->lq_type)) {
Johannes Berg8318d782008-01-24 19:38:38 +01002273 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Ben Cahill77626352007-11-29 11:09:44 +08002274 /* supp_rates has no CCK bits in A mode */
Zhu Yib481de92007-09-25 17:54:57 -07002275 rate_scale_index_msk = (u16) (rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -08002276 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
Zhu Yib481de92007-09-25 17:54:57 -07002277 else
2278 rate_scale_index_msk = (u16) (rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -08002279 lq_sta->supp_rates);
Zhu Yib481de92007-09-25 17:54:57 -07002280
2281 } else
2282 rate_scale_index_msk = rate_mask;
2283
2284 if (!rate_scale_index_msk)
2285 rate_scale_index_msk = rate_mask;
2286
Guy Cohen07bc28e2008-04-23 17:15:03 -07002287 if (!((1 << index) & rate_scale_index_msk)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002288 IWL_ERR(priv, "Current Rate is not valid\n");
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07002289 if (lq_sta->search_better_tbl) {
2290 /* revert to active table if search table is not valid*/
2291 tbl->lq_type = LQ_NONE;
2292 lq_sta->search_better_tbl = 0;
2293 tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
2294 /* get "active" rate info */
2295 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
Johannes Berg7e6a5882010-08-23 10:46:46 +02002296 rate = rs_update_rate_tbl(priv, ctx, lq_sta,
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07002297 tbl, index, is_green);
2298 }
Guy Cohen07bc28e2008-04-23 17:15:03 -07002299 return;
Zhu Yib481de92007-09-25 17:54:57 -07002300 }
2301
Ben Cahill77626352007-11-29 11:09:44 +08002302 /* Get expected throughput table and history window for current rate */
Guy Cohen07bc28e2008-04-23 17:15:03 -07002303 if (!tbl->expected_tpt) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002304 IWL_ERR(priv, "tbl->expected_tpt is NULL\n");
Guy Cohen07bc28e2008-04-23 17:15:03 -07002305 return;
2306 }
Zhu Yib481de92007-09-25 17:54:57 -07002307
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002308 /* force user max rate if set by user */
2309 if ((lq_sta->max_rate_idx != -1) &&
2310 (lq_sta->max_rate_idx < index)) {
2311 index = lq_sta->max_rate_idx;
2312 update_lq = 1;
2313 window = &(tbl->win[index]);
2314 goto lq_update;
2315 }
2316
Zhu Yib481de92007-09-25 17:54:57 -07002317 window = &(tbl->win[index]);
2318
Ben Cahill77626352007-11-29 11:09:44 +08002319 /*
2320 * If there is not enough history to calculate actual average
2321 * throughput, keep analyzing results of more tx frames, without
2322 * changing rate or mode (bypass most of the rest of this function).
2323 * Set up new rate table in uCode only if old rate is not supported
2324 * in current association (use new rate found above).
2325 */
Zhu Yib481de92007-09-25 17:54:57 -07002326 fail_count = window->counter - window->success_counter;
Guy Cohen07bc28e2008-04-23 17:15:03 -07002327 if ((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
2328 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002329 IWL_DEBUG_RATE(priv, "LQ: still below TH. succ=%d total=%d "
Zhu Yib481de92007-09-25 17:54:57 -07002330 "for index %d\n",
2331 window->success_counter, window->counter, index);
Ben Cahill77626352007-11-29 11:09:44 +08002332
2333 /* Can't calculate this yet; not enough history */
Zhu Yib481de92007-09-25 17:54:57 -07002334 window->average_tpt = IWL_INVALID_VALUE;
Ben Cahill77626352007-11-29 11:09:44 +08002335
2336 /* Should we stay with this modulation mode,
2337 * or search for a new one? */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07002338 rs_stay_in_table(lq_sta, false);
Ben Cahill77626352007-11-29 11:09:44 +08002339
Zhu Yib481de92007-09-25 17:54:57 -07002340 goto out;
Guy Cohen3110bef2008-09-09 10:54:54 +08002341 }
Ben Cahill77626352007-11-29 11:09:44 +08002342 /* Else we have enough samples; calculate estimate of
2343 * actual average throughput */
Reinette Chatre3d79b2a2010-05-03 10:55:07 -07002344 if (window->average_tpt != ((window->success_ratio *
2345 tbl->expected_tpt[index] + 64) / 128)) {
2346 IWL_ERR(priv, "expected_tpt should have been calculated by now\n");
2347 window->average_tpt = ((window->success_ratio *
2348 tbl->expected_tpt[index] + 64) / 128);
2349 }
Zhu Yib481de92007-09-25 17:54:57 -07002350
Ben Cahill77626352007-11-29 11:09:44 +08002351 /* If we are searching for better modulation mode, check success. */
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002352 if (lq_sta->search_better_tbl &&
Johannes Berg3ad3b922009-08-07 15:41:48 -07002353 (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_MULTI)) {
Ben Cahill77626352007-11-29 11:09:44 +08002354 /* If good success, continue using the "search" mode;
2355 * no need to send new link quality command, since we're
2356 * continuing to use the setup that we've been trying. */
Guy Cohen07bc28e2008-04-23 17:15:03 -07002357 if (window->average_tpt > lq_sta->last_tpt) {
2358
Tomas Winklere1623442009-01-27 14:27:56 -08002359 IWL_DEBUG_RATE(priv, "LQ: SWITCHING TO NEW TABLE "
Guy Cohen07bc28e2008-04-23 17:15:03 -07002360 "suc=%d cur-tpt=%d old-tpt=%d\n",
2361 window->success_ratio,
2362 window->average_tpt,
2363 lq_sta->last_tpt);
2364
2365 if (!is_legacy(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08002366 lq_sta->enable_counter = 1;
Guy Cohen07bc28e2008-04-23 17:15:03 -07002367
Ben Cahill77626352007-11-29 11:09:44 +08002368 /* Swap tables; "search" becomes "active" */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002369 lq_sta->active_tbl = active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002370 current_tpt = window->average_tpt;
Ben Cahill77626352007-11-29 11:09:44 +08002371
2372 /* Else poor success; go back to mode in "active" table */
Zhu Yib481de92007-09-25 17:54:57 -07002373 } else {
Guy Cohen07bc28e2008-04-23 17:15:03 -07002374
Tomas Winklere1623442009-01-27 14:27:56 -08002375 IWL_DEBUG_RATE(priv, "LQ: GOING BACK TO THE OLD TABLE "
Guy Cohen07bc28e2008-04-23 17:15:03 -07002376 "suc=%d cur-tpt=%d old-tpt=%d\n",
2377 window->success_ratio,
2378 window->average_tpt,
2379 lq_sta->last_tpt);
2380
Ben Cahill77626352007-11-29 11:09:44 +08002381 /* Nullify "search" table */
Zhu Yib481de92007-09-25 17:54:57 -07002382 tbl->lq_type = LQ_NONE;
Ben Cahill77626352007-11-29 11:09:44 +08002383
2384 /* Revert to "active" table */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002385 active_tbl = lq_sta->active_tbl;
2386 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07002387
Ben Cahill77626352007-11-29 11:09:44 +08002388 /* Revert to "active" rate and throughput info */
Tomas Winklere7d326a2008-06-12 09:47:11 +08002389 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
Tomas Winklerc33104f2008-01-14 17:46:21 -08002390 current_tpt = lq_sta->last_tpt;
Ben Cahill77626352007-11-29 11:09:44 +08002391
2392 /* Need to set up a new rate table in uCode */
2393 update_lq = 1;
Zhu Yib481de92007-09-25 17:54:57 -07002394 }
Ben Cahill77626352007-11-29 11:09:44 +08002395
2396 /* Either way, we've made a decision; modulation mode
2397 * search is done, allow rate adjustment next time. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002398 lq_sta->search_better_tbl = 0;
Ben Cahill77626352007-11-29 11:09:44 +08002399 done_search = 1; /* Don't switch modes below! */
Zhu Yib481de92007-09-25 17:54:57 -07002400 goto lq_update;
2401 }
2402
Ben Cahill77626352007-11-29 11:09:44 +08002403 /* (Else) not in search of better modulation mode, try for better
2404 * starting rate, while staying in this mode. */
Ester Kummerbf403db2008-05-05 10:22:40 +08002405 high_low = rs_get_adjacent_rate(priv, index, rate_scale_index_msk,
Zhu Yib481de92007-09-25 17:54:57 -07002406 tbl->lq_type);
2407 low = high_low & 0xff;
2408 high = (high_low >> 8) & 0xff;
2409
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002410 /* If user set max rate, dont allow higher than user constrain */
2411 if ((lq_sta->max_rate_idx != -1) &&
2412 (lq_sta->max_rate_idx < high))
2413 high = IWL_RATE_INVALID;
2414
Guy Cohena5e8b502008-05-29 16:35:11 +08002415 sr = window->success_ratio;
2416
Ben Cahill77626352007-11-29 11:09:44 +08002417 /* Collect measured throughputs for current and adjacent rates */
Zhu Yib481de92007-09-25 17:54:57 -07002418 current_tpt = window->average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07002419 if (low != IWL_RATE_INVALID)
2420 low_tpt = tbl->win[low].average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07002421 if (high != IWL_RATE_INVALID)
2422 high_tpt = tbl->win[high].average_tpt;
2423
Guy Cohena5e8b502008-05-29 16:35:11 +08002424 scale_action = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002425
Ben Cahill77626352007-11-29 11:09:44 +08002426 /* Too many failures, decrease rate */
Guy Cohena5e8b502008-05-29 16:35:11 +08002427 if ((sr <= IWL_RATE_DECREASE_TH) || (current_tpt == 0)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002428 IWL_DEBUG_RATE(priv, "decrease rate because of low success_ratio\n");
Zhu Yib481de92007-09-25 17:54:57 -07002429 scale_action = -1;
Ben Cahill77626352007-11-29 11:09:44 +08002430
2431 /* No throughput measured yet for adjacent rates; try increase. */
Zhu Yib481de92007-09-25 17:54:57 -07002432 } else if ((low_tpt == IWL_INVALID_VALUE) &&
Guy Cohena5e8b502008-05-29 16:35:11 +08002433 (high_tpt == IWL_INVALID_VALUE)) {
2434
2435 if (high != IWL_RATE_INVALID && sr >= IWL_RATE_INCREASE_TH)
2436 scale_action = 1;
2437 else if (low != IWL_RATE_INVALID)
Wey-Yi Guy8fe72312009-03-10 14:35:10 -07002438 scale_action = 0;
Guy Cohena5e8b502008-05-29 16:35:11 +08002439 }
Ben Cahill77626352007-11-29 11:09:44 +08002440
2441 /* Both adjacent throughputs are measured, but neither one has better
2442 * throughput; we're using the best rate, don't change it! */
Zhu Yib481de92007-09-25 17:54:57 -07002443 else if ((low_tpt != IWL_INVALID_VALUE) &&
2444 (high_tpt != IWL_INVALID_VALUE) &&
2445 (low_tpt < current_tpt) &&
2446 (high_tpt < current_tpt))
2447 scale_action = 0;
Ben Cahill77626352007-11-29 11:09:44 +08002448
2449 /* At least one adjacent rate's throughput is measured,
2450 * and may have better performance. */
Zhu Yib481de92007-09-25 17:54:57 -07002451 else {
Ben Cahill77626352007-11-29 11:09:44 +08002452 /* Higher adjacent rate's throughput is measured */
Zhu Yib481de92007-09-25 17:54:57 -07002453 if (high_tpt != IWL_INVALID_VALUE) {
Ben Cahill77626352007-11-29 11:09:44 +08002454 /* Higher rate has better throughput */
Guy Cohena5e8b502008-05-29 16:35:11 +08002455 if (high_tpt > current_tpt &&
2456 sr >= IWL_RATE_INCREASE_TH) {
Zhu Yib481de92007-09-25 17:54:57 -07002457 scale_action = 1;
Guy Cohena5e8b502008-05-29 16:35:11 +08002458 } else {
Wey-Yi Guy8fe72312009-03-10 14:35:10 -07002459 scale_action = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002460 }
Ben Cahill77626352007-11-29 11:09:44 +08002461
2462 /* Lower adjacent rate's throughput is measured */
Zhu Yib481de92007-09-25 17:54:57 -07002463 } else if (low_tpt != IWL_INVALID_VALUE) {
Ben Cahill77626352007-11-29 11:09:44 +08002464 /* Lower rate has better throughput */
Zhu Yib481de92007-09-25 17:54:57 -07002465 if (low_tpt > current_tpt) {
Tomas Winklere1623442009-01-27 14:27:56 -08002466 IWL_DEBUG_RATE(priv,
2467 "decrease rate because of low tpt\n");
Zhu Yib481de92007-09-25 17:54:57 -07002468 scale_action = -1;
Guy Cohena5e8b502008-05-29 16:35:11 +08002469 } else if (sr >= IWL_RATE_INCREASE_TH) {
Zhu Yib481de92007-09-25 17:54:57 -07002470 scale_action = 1;
Guy Cohena5e8b502008-05-29 16:35:11 +08002471 }
Zhu Yib481de92007-09-25 17:54:57 -07002472 }
2473 }
2474
Ben Cahill77626352007-11-29 11:09:44 +08002475 /* Sanity check; asked for decrease, but success rate or throughput
2476 * has been good at old rate. Don't change it. */
Guy Cohena5e8b502008-05-29 16:35:11 +08002477 if ((scale_action == -1) && (low != IWL_RATE_INVALID) &&
2478 ((sr > IWL_RATE_HIGH_TH) ||
Zhu Yib481de92007-09-25 17:54:57 -07002479 (current_tpt > (100 * tbl->expected_tpt[low]))))
Zhu Yib481de92007-09-25 17:54:57 -07002480 scale_action = 0;
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002481 if (!iwl_ht_enabled(priv) && !is_legacy(tbl->lq_type))
2482 scale_action = -1;
Johannes Berg3ad3b922009-08-07 15:41:48 -07002483 if (iwl_tx_ant_restriction(priv) != IWL_ANT_OK_MULTI &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002484 (is_mimo2(tbl->lq_type) || is_mimo3(tbl->lq_type)))
2485 scale_action = -1;
Johannes Berg290f5992010-08-23 07:56:58 -07002486
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07002487 if ((priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH) &&
2488 (is_mimo2(tbl->lq_type) || is_mimo3(tbl->lq_type))) {
2489 if (lq_sta->last_bt_traffic > priv->bt_traffic_load) {
2490 /*
2491 * don't set scale_action, don't want to scale up if
2492 * the rate scale doesn't otherwise think that is a
2493 * good idea.
2494 */
2495 } else if (lq_sta->last_bt_traffic <= priv->bt_traffic_load) {
2496 scale_action = -1;
2497 }
2498 }
2499 lq_sta->last_bt_traffic = priv->bt_traffic_load;
2500
2501 if ((priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH) &&
2502 (is_mimo2(tbl->lq_type) || is_mimo3(tbl->lq_type))) {
2503 /* search for a new modulation */
2504 rs_stay_in_table(lq_sta, true);
2505 goto lq_update;
Johannes Berg290f5992010-08-23 07:56:58 -07002506 }
2507
Zhu Yib481de92007-09-25 17:54:57 -07002508 switch (scale_action) {
2509 case -1:
Ben Cahill77626352007-11-29 11:09:44 +08002510 /* Decrease starting rate, update uCode's rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002511 if (low != IWL_RATE_INVALID) {
2512 update_lq = 1;
2513 index = low;
2514 }
Mohamed Abbas447fee72009-04-20 14:37:02 -07002515
Zhu Yib481de92007-09-25 17:54:57 -07002516 break;
2517 case 1:
Ben Cahill77626352007-11-29 11:09:44 +08002518 /* Increase starting rate, update uCode's rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002519 if (high != IWL_RATE_INVALID) {
2520 update_lq = 1;
2521 index = high;
2522 }
2523
2524 break;
2525 case 0:
Ben Cahill77626352007-11-29 11:09:44 +08002526 /* No change */
Zhu Yib481de92007-09-25 17:54:57 -07002527 default:
2528 break;
2529 }
2530
Tomas Winklere1623442009-01-27 14:27:56 -08002531 IWL_DEBUG_RATE(priv, "choose rate scale index %d action %d low %d "
Zhu Yib481de92007-09-25 17:54:57 -07002532 "high %d type %d\n",
2533 index, scale_action, low, high, tbl->lq_type);
2534
Guy Cohena5e8b502008-05-29 16:35:11 +08002535lq_update:
Ben Cahill77626352007-11-29 11:09:44 +08002536 /* Replace uCode's rate table for the destination station. */
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07002537 if (update_lq)
Johannes Berg7e6a5882010-08-23 10:46:46 +02002538 rate = rs_update_rate_tbl(priv, ctx, lq_sta,
Wey-Yi Guye3139fe2009-07-24 11:13:00 -07002539 tbl, index, is_green);
Ben Cahill77626352007-11-29 11:09:44 +08002540
Johannes Berg3ad3b922009-08-07 15:41:48 -07002541 if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_MULTI) {
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002542 /* Should we stay with this modulation mode,
2543 * or search for a new one? */
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07002544 rs_stay_in_table(lq_sta, false);
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002545 }
Ben Cahill77626352007-11-29 11:09:44 +08002546 /*
2547 * Search for new modulation mode if we're:
2548 * 1) Not changing rates right now
2549 * 2) Not just finishing up a search
2550 * 3) Allowing a new search
2551 */
Guy Cohen47cfd462008-05-27 11:29:34 +08002552 if (!update_lq && !done_search && !lq_sta->stay_in_tbl && window->counter) {
Ben Cahill77626352007-11-29 11:09:44 +08002553 /* Save current throughput to compare with "search" throughput*/
Tomas Winklerc33104f2008-01-14 17:46:21 -08002554 lq_sta->last_tpt = current_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07002555
Ben Cahill77626352007-11-29 11:09:44 +08002556 /* Select a new "search" modulation mode to try.
2557 * If one is found, set up the new "search" table. */
Zhu Yib481de92007-09-25 17:54:57 -07002558 if (is_legacy(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08002559 rs_move_legacy_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07002560 else if (is_siso(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08002561 rs_move_siso_to_other(priv, lq_sta, conf, sta, index);
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07002562 else if (is_mimo2(tbl->lq_type))
2563 rs_move_mimo2_to_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07002564 else
Wey-Yi Guy584a0f02009-04-01 14:33:24 -07002565 rs_move_mimo3_to_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07002566
Ben Cahill77626352007-11-29 11:09:44 +08002567 /* If new "search" mode was selected, set up in uCode table */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002568 if (lq_sta->search_better_tbl) {
Ben Cahill77626352007-11-29 11:09:44 +08002569 /* Access the "search" table, clear its history. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002570 tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Zhu Yib481de92007-09-25 17:54:57 -07002571 for (i = 0; i < IWL_RATE_COUNT; i++)
2572 rs_rate_scale_clear_window(&(tbl->win[i]));
2573
Ben Cahill77626352007-11-29 11:09:44 +08002574 /* Use new "search" start rate */
Tomas Winklere7d326a2008-06-12 09:47:11 +08002575 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002576
Tomas Winklere1623442009-01-27 14:27:56 -08002577 IWL_DEBUG_RATE(priv, "Switch current mcs: %X index: %d\n",
Guy Cohen39e88502008-04-23 17:14:57 -07002578 tbl->current_rate, index);
Guy Cohen07bc28e2008-04-23 17:15:03 -07002579 rs_fill_link_cmd(priv, lq_sta, tbl->current_rate);
Johannes Berg7e6a5882010-08-23 10:46:46 +02002580 iwl_send_lq_cmd(priv, ctx, &lq_sta->lq, CMD_ASYNC, false);
Mohamed Abbas447fee72009-04-20 14:37:02 -07002581 } else
2582 done_search = 1;
2583 }
Zhu Yib481de92007-09-25 17:54:57 -07002584
Mohamed Abbas447fee72009-04-20 14:37:02 -07002585 if (done_search && !lq_sta->stay_in_tbl) {
Ben Cahill77626352007-11-29 11:09:44 +08002586 /* If the "active" (non-search) mode was legacy,
2587 * and we've tried switching antennas,
2588 * but we haven't been able to try HT modes (not available),
2589 * stay with best antenna legacy modulation for a while
2590 * before next round of mode comparisons. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002591 tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
Luis R. Rodriguezde27e642008-12-23 15:58:44 -08002592 if (is_legacy(tbl1->lq_type) && !conf_is_ht(conf) &&
Mohamed Abbasd6e93392009-05-08 13:44:39 -07002593 lq_sta->action_counter > tbl1->max_search) {
Tomas Winklere1623442009-01-27 14:27:56 -08002594 IWL_DEBUG_RATE(priv, "LQ: STAY in legacy table\n");
Ester Kummerbf403db2008-05-05 10:22:40 +08002595 rs_set_stay_in_table(priv, 1, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002596 }
2597
Ben Cahill77626352007-11-29 11:09:44 +08002598 /* If we're in an HT mode, and all 3 mode switch actions
2599 * have been tried and compared, stay in this best modulation
2600 * mode for a while before next round of mode comparisons. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002601 if (lq_sta->enable_counter &&
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002602 (lq_sta->action_counter >= tbl1->max_search) &&
2603 iwl_ht_enabled(priv)) {
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002604 if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
2605 (lq_sta->tx_agg_tid_en & (1 << tid)) &&
2606 (tid != MAX_TID_COUNT)) {
Wey-Yi Guy86b47662009-03-10 14:35:09 -07002607 tid_data =
2608 &priv->stations[lq_sta->lq.sta_id].tid[tid];
2609 if (tid_data->agg.state == IWL_AGG_OFF) {
2610 IWL_DEBUG_RATE(priv,
2611 "try to aggregate tid %d\n",
2612 tid);
2613 rs_tl_turn_on_agg(priv, tid,
2614 lq_sta, sta);
2615 }
Zhu Yib481de92007-09-25 17:54:57 -07002616 }
Ester Kummerbf403db2008-05-05 10:22:40 +08002617 rs_set_stay_in_table(priv, 0, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002618 }
Zhu Yib481de92007-09-25 17:54:57 -07002619 }
2620
2621out:
Tomas Winkler978785a2008-12-19 10:37:31 +08002622 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
Zhu Yib481de92007-09-25 17:54:57 -07002623 i = index;
Johannes Bergb7e35002008-09-11 02:22:58 +02002624 lq_sta->last_txrate_idx = i;
Zhu Yib481de92007-09-25 17:54:57 -07002625}
2626
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08002627/**
2628 * rs_initialize_lq - Initialize a station's hardware rate table
2629 *
2630 * The uCode's station table contains a table of fallback rates
2631 * for automatic fallback during transmission.
2632 *
2633 * NOTE: This sets up a default set of values. These will be replaced later
2634 * if the driver's iwl-agn-rs rate scaling algorithm is used, instead of
2635 * rc80211_simple.
2636 *
2637 * NOTE: Run REPLY_ADD_STA command to set up station table entry, before
2638 * calling this function (which runs REPLY_TX_LINK_QUALITY_CMD,
2639 * which requires station table entry to exist).
2640 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002641static void rs_initialize_lq(struct iwl_priv *priv,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002642 struct ieee80211_conf *conf,
Johannes Berg4b7679a2008-09-18 18:14:18 +02002643 struct ieee80211_sta *sta,
2644 struct iwl_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002645{
Tomas Winklere227cea2008-07-18 13:53:05 +08002646 struct iwl_scale_tbl_info *tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002647 int rate_idx;
Guy Cohenaade00c2008-04-23 17:14:59 -07002648 int i;
Guy Cohen39e88502008-04-23 17:14:57 -07002649 u32 rate;
Johannes Berg7e6a5882010-08-23 10:46:46 +02002650 u8 use_green = rs_use_green(sta);
Guy Cohenaade00c2008-04-23 17:14:59 -07002651 u8 active_tbl = 0;
2652 u8 valid_tx_ant;
Johannes Berg7e6a5882010-08-23 10:46:46 +02002653 struct iwl_station_priv *sta_priv;
2654 struct iwl_rxon_context *ctx;
Zhu Yib481de92007-09-25 17:54:57 -07002655
Johannes Berg4b7679a2008-09-18 18:14:18 +02002656 if (!sta || !lq_sta)
Johannes Berg7e6a5882010-08-23 10:46:46 +02002657 return;
2658
2659 sta_priv = (void *)sta->drv_priv;
2660 ctx = sta_priv->common.ctx;
Zhu Yib481de92007-09-25 17:54:57 -07002661
Johannes Bergb7e35002008-09-11 02:22:58 +02002662 i = lq_sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002663
Guy Cohenaade00c2008-04-23 17:14:59 -07002664 valid_tx_ant = priv->hw_params.valid_tx_ant;
2665
Tomas Winklerc33104f2008-01-14 17:46:21 -08002666 if (!lq_sta->search_better_tbl)
2667 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002668 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002669 active_tbl = 1 - lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002670
Tomas Winklerc33104f2008-01-14 17:46:21 -08002671 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07002672
2673 if ((i < 0) || (i >= IWL_RATE_COUNT))
2674 i = 0;
2675
Tomas Winkler1826dcc2008-05-15 13:54:02 +08002676 rate = iwl_rates[i].plcp;
Winkler, Tomaseb48dca2008-10-29 14:05:48 -07002677 tbl->ant_type = first_antenna(valid_tx_ant);
2678 rate |= tbl->ant_type << RATE_MCS_ANT_POS;
Zhu Yib481de92007-09-25 17:54:57 -07002679
2680 if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
Guy Cohen39e88502008-04-23 17:14:57 -07002681 rate |= RATE_MCS_CCK_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002682
Guy Cohen39e88502008-04-23 17:14:57 -07002683 rs_get_tbl_info_from_mcs(rate, priv->band, tbl, &rate_idx);
Guy Cohenaade00c2008-04-23 17:14:59 -07002684 if (!rs_is_valid_ant(valid_tx_ant, tbl->ant_type))
2685 rs_toggle_antenna(valid_tx_ant, &rate, tbl);
Zhu Yib481de92007-09-25 17:54:57 -07002686
Tomas Winkler978785a2008-12-19 10:37:31 +08002687 rate = rate_n_flags_from_tbl(priv, tbl, rate_idx, use_green);
Guy Cohen39e88502008-04-23 17:14:57 -07002688 tbl->current_rate = rate;
Guy Coheneecd6e52008-04-23 17:14:58 -07002689 rs_set_expected_tpt_table(lq_sta, tbl);
Guy Cohen07bc28e2008-04-23 17:15:03 -07002690 rs_fill_link_cmd(NULL, lq_sta, rate);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08002691 priv->stations[lq_sta->lq.sta_id].lq = &lq_sta->lq;
Johannes Berg7e6a5882010-08-23 10:46:46 +02002692 iwl_send_lq_cmd(priv, ctx, &lq_sta->lq, CMD_SYNC, true);
Zhu Yib481de92007-09-25 17:54:57 -07002693}
2694
Johannes Berge6a98542008-10-21 12:40:02 +02002695static void rs_get_rate(void *priv_r, struct ieee80211_sta *sta, void *priv_sta,
2696 struct ieee80211_tx_rate_control *txrc)
Zhu Yib481de92007-09-25 17:54:57 -07002697{
2698
Johannes Berge6a98542008-10-21 12:40:02 +02002699 struct sk_buff *skb = txrc->skb;
2700 struct ieee80211_supported_band *sband = txrc->sband;
Reinette Chatref875f512010-04-05 10:43:10 -07002701 struct iwl_priv *priv __maybe_unused = (struct iwl_priv *)priv_r;
Johannes Berge6a98542008-10-21 12:40:02 +02002702 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Tomas Winkler417f1142008-11-12 13:14:06 -08002703 struct iwl_lq_sta *lq_sta = priv_sta;
2704 int rate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002705
Tomas Winklere1623442009-01-27 14:27:56 -08002706 IWL_DEBUG_RATE_LIMIT(priv, "rate scale calculate new rate for skb\n");
Zhu Yib481de92007-09-25 17:54:57 -07002707
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002708 /* Get max rate if user set max rate */
2709 if (lq_sta) {
2710 lq_sta->max_rate_idx = txrc->max_rate_idx;
2711 if ((sband->band == IEEE80211_BAND_5GHZ) &&
2712 (lq_sta->max_rate_idx != -1))
2713 lq_sta->max_rate_idx += IWL_FIRST_OFDM_RATE;
2714 if ((lq_sta->max_rate_idx < 0) ||
2715 (lq_sta->max_rate_idx >= IWL_RATE_COUNT))
2716 lq_sta->max_rate_idx = -1;
2717 }
2718
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08002719 /* Treat uninitialized rate scaling data same as non-existing. */
2720 if (lq_sta && !lq_sta->drv) {
2721 IWL_DEBUG_RATE(priv, "Rate scaling not initialized yet.\n");
2722 priv_sta = NULL;
2723 }
2724
Gábor Stefanik514d65c2009-04-23 19:36:08 +02002725 /* Send management frames and NO_ACK data using lowest rate. */
Luis R. Rodriguez4c6d4f52009-07-16 10:05:41 -07002726 if (rate_control_send_low(sta, priv_sta, txrc))
Johannes Berg4b7679a2008-09-18 18:14:18 +02002727 return;
Zhu Yib481de92007-09-25 17:54:57 -07002728
Tomas Winkler417f1142008-11-12 13:14:06 -08002729 rate_idx = lq_sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002730
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002731 if (lq_sta->last_rate_n_flags & RATE_MCS_HT_MSK) {
Tomas Winkler417f1142008-11-12 13:14:06 -08002732 rate_idx -= IWL_FIRST_OFDM_RATE;
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002733 /* 6M and 9M shared same MCS index */
2734 rate_idx = (rate_idx > 0) ? (rate_idx - 1) : 0;
2735 if (rs_extract_rate(lq_sta->last_rate_n_flags) >=
2736 IWL_RATE_MIMO3_6M_PLCP)
2737 rate_idx = rate_idx + (2 * MCS_INDEX_PER_STREAM);
2738 else if (rs_extract_rate(lq_sta->last_rate_n_flags) >=
2739 IWL_RATE_MIMO2_6M_PLCP)
2740 rate_idx = rate_idx + MCS_INDEX_PER_STREAM;
2741 info->control.rates[0].flags = IEEE80211_TX_RC_MCS;
2742 if (lq_sta->last_rate_n_flags & RATE_MCS_SGI_MSK)
2743 info->control.rates[0].flags |= IEEE80211_TX_RC_SHORT_GI;
2744 if (lq_sta->last_rate_n_flags & RATE_MCS_DUP_MSK)
2745 info->control.rates[0].flags |= IEEE80211_TX_RC_DUP_DATA;
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07002746 if (lq_sta->last_rate_n_flags & RATE_MCS_HT40_MSK)
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002747 info->control.rates[0].flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
2748 if (lq_sta->last_rate_n_flags & RATE_MCS_GF_MSK)
2749 info->control.rates[0].flags |= IEEE80211_TX_RC_GREEN_FIELD;
2750 } else {
Daniel C Halperin50273092009-08-28 09:44:45 -07002751 /* Check for invalid rates */
2752 if ((rate_idx < 0) || (rate_idx >= IWL_RATE_COUNT_LEGACY) ||
2753 ((sband->band == IEEE80211_BAND_5GHZ) &&
2754 (rate_idx < IWL_FIRST_OFDM_RATE)))
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002755 rate_idx = rate_lowest_index(sband, sta);
Daniel C Halperin50273092009-08-28 09:44:45 -07002756 /* On valid 5 GHz rate, adjust index */
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002757 else if (sband->band == IEEE80211_BAND_5GHZ)
2758 rate_idx -= IWL_FIRST_OFDM_RATE;
Daniel C Halperin7ebaeff2009-08-21 13:34:20 -07002759 info->control.rates[0].flags = 0;
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002760 }
Tomas Winkler417f1142008-11-12 13:14:06 -08002761 info->control.rates[0].idx = rate_idx;
Wey-Yi Guya9c146b2009-05-22 11:01:48 -07002762
Zhu Yib481de92007-09-25 17:54:57 -07002763}
2764
Johannes Berg4b7679a2008-09-18 18:14:18 +02002765static void *rs_alloc_sta(void *priv_rate, struct ieee80211_sta *sta,
2766 gfp_t gfp)
Zhu Yib481de92007-09-25 17:54:57 -07002767{
Tomas Winklere227cea2008-07-18 13:53:05 +08002768 struct iwl_lq_sta *lq_sta;
Reinette Chatre8d9698b2009-10-16 14:25:55 -07002769 struct iwl_station_priv *sta_priv = (struct iwl_station_priv *) sta->drv_priv;
Ester Kummerbf403db2008-05-05 10:22:40 +08002770 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07002771
Ester Kummerbf403db2008-05-05 10:22:40 +08002772 priv = (struct iwl_priv *)priv_rate;
Tomas Winklere1623442009-01-27 14:27:56 -08002773 IWL_DEBUG_RATE(priv, "create station rate scale window\n");
Zhu Yib481de92007-09-25 17:54:57 -07002774
Reinette Chatre8d9698b2009-10-16 14:25:55 -07002775 lq_sta = &sta_priv->lq_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002776
Tomas Winklerc33104f2008-01-14 17:46:21 -08002777 return lq_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002778}
2779
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08002780/*
2781 * Called after adding a new station to initialize rate scaling
2782 */
2783void iwl_rs_rate_init(struct iwl_priv *priv, struct ieee80211_sta *sta, u8 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07002784{
2785 int i, j;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08002786 struct ieee80211_hw *hw = priv->hw;
Johannes Berg4b7679a2008-09-18 18:14:18 +02002787 struct ieee80211_conf *conf = &priv->hw->conf;
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002788 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08002789 struct iwl_station_priv *sta_priv;
2790 struct iwl_lq_sta *lq_sta;
2791 struct ieee80211_supported_band *sband;
Zhu Yib481de92007-09-25 17:54:57 -07002792
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08002793 sta_priv = (struct iwl_station_priv *) sta->drv_priv;
2794 lq_sta = &sta_priv->lq_sta;
2795 sband = hw->wiphy->bands[conf->channel->band];
2796
2797
2798 lq_sta->lq.sta_id = sta_id;
Reinette Chatre5ad13f82009-10-30 14:36:14 -07002799
2800 for (j = 0; j < LQ_SIZE; j++)
2801 for (i = 0; i < IWL_RATE_COUNT; i++)
2802 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
2803
Tomas Winklerc33104f2008-01-14 17:46:21 -08002804 lq_sta->flush_timer = 0;
Johannes Berg4b7679a2008-09-18 18:14:18 +02002805 lq_sta->supp_rates = sta->supp_rates[sband->band];
Zhu Yib481de92007-09-25 17:54:57 -07002806 for (j = 0; j < LQ_SIZE; j++)
2807 for (i = 0; i < IWL_RATE_COUNT; i++)
Tomas Winkler3ac7f142008-07-21 02:40:14 +03002808 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
Zhu Yib481de92007-09-25 17:54:57 -07002809
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08002810 IWL_DEBUG_RATE(priv, "LQ: *** rate scale station global init for station %d ***\n",
2811 sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07002812 /* TODO: what is a good starting rate for STA? About middle? Maybe not
2813 * the lowest or the highest rate.. Could consider using RSSI from
2814 * previous packets? Need to have IEEE 802.1X auth succeed immediately
2815 * after assoc.. */
2816
Tomas Winklerc33104f2008-01-14 17:46:21 -08002817 lq_sta->is_dup = 0;
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -08002818 lq_sta->max_rate_idx = -1;
Abbas, Mohamed7d049e52009-01-20 21:33:53 -08002819 lq_sta->missed_rate_counter = IWL_MISSED_RATE_MAX;
Johannes Berg7e6a5882010-08-23 10:46:46 +02002820 lq_sta->is_green = rs_use_green(sta);
Guy Cohen39e88502008-04-23 17:14:57 -07002821 lq_sta->active_legacy_rate = priv->active_rate & ~(0x1000);
Johannes Berg8318d782008-01-24 19:38:38 +01002822 lq_sta->band = priv->band;
Ben Cahill77626352007-11-29 11:09:44 +08002823 /*
2824 * active_siso_rate mask includes 9 MBits (bit 5), and CCK (bits 0-3),
2825 * supp_rates[] does not; shift to convert format, force 9 MBits off.
2826 */
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002827 lq_sta->active_siso_rate = ht_cap->mcs.rx_mask[0] << 1;
2828 lq_sta->active_siso_rate |= ht_cap->mcs.rx_mask[0] & 0x1;
Guy Cohenfde0db32008-04-21 15:42:01 -07002829 lq_sta->active_siso_rate &= ~((u16)0x2);
2830 lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -07002831
Ben Cahill77626352007-11-29 11:09:44 +08002832 /* Same here */
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002833 lq_sta->active_mimo2_rate = ht_cap->mcs.rx_mask[1] << 1;
2834 lq_sta->active_mimo2_rate |= ht_cap->mcs.rx_mask[1] & 0x1;
Guy Cohenfde0db32008-04-21 15:42:01 -07002835 lq_sta->active_mimo2_rate &= ~((u16)0x2);
2836 lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE;
2837
Daniel C Halperin7869b0e2009-08-13 13:30:52 -07002838 lq_sta->active_mimo3_rate = ht_cap->mcs.rx_mask[2] << 1;
2839 lq_sta->active_mimo3_rate |= ht_cap->mcs.rx_mask[2] & 0x1;
Guy Cohenfde0db32008-04-21 15:42:01 -07002840 lq_sta->active_mimo3_rate &= ~((u16)0x2);
2841 lq_sta->active_mimo3_rate <<= IWL_FIRST_OFDM_RATE;
2842
Tomas Winklere1623442009-01-27 14:27:56 -08002843 IWL_DEBUG_RATE(priv, "SISO-RATE=%X MIMO2-RATE=%X MIMO3-RATE=%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002844 lq_sta->active_siso_rate,
Guy Cohenfde0db32008-04-21 15:42:01 -07002845 lq_sta->active_mimo2_rate,
2846 lq_sta->active_mimo3_rate);
2847
Tomas Winklera96a27f2008-10-23 23:48:56 -07002848 /* These values will be overridden later */
Wey-Yi Guy3a23d692010-04-03 16:44:39 -07002849 lq_sta->lq.general_params.single_stream_ant_msk =
2850 first_antenna(priv->hw_params.valid_tx_ant);
2851 lq_sta->lq.general_params.dual_stream_ant_msk =
2852 priv->hw_params.valid_tx_ant &
2853 ~first_antenna(priv->hw_params.valid_tx_ant);
2854 if (!lq_sta->lq.general_params.dual_stream_ant_msk) {
2855 lq_sta->lq.general_params.dual_stream_ant_msk = ANT_AB;
2856 } else if (num_of_ant(priv->hw_params.valid_tx_ant) == 2) {
2857 lq_sta->lq.general_params.dual_stream_ant_msk =
2858 priv->hw_params.valid_tx_ant;
2859 }
Guy Cohen07bc28e2008-04-23 17:15:03 -07002860
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002861 /* as default allow aggregation for all tids */
2862 lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002863 lq_sta->drv = priv;
Zhu Yib481de92007-09-25 17:54:57 -07002864
Daniel C Halperin50273092009-08-28 09:44:45 -07002865 /* Set last_txrate_idx to lowest rate */
2866 lq_sta->last_txrate_idx = rate_lowest_index(sband, sta);
2867 if (sband->band == IEEE80211_BAND_5GHZ)
2868 lq_sta->last_txrate_idx += IWL_FIRST_OFDM_RATE;
Daniel C Halperine3949d62009-09-17 10:43:50 -07002869 lq_sta->is_agg = 0;
Mohamed Abbasd5e49032008-12-12 08:22:15 -08002870
Johannes Berg4b7679a2008-09-18 18:14:18 +02002871 rs_initialize_lq(priv, conf, sta, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002872}
2873
Wey-Yi Guy46f93812009-07-24 11:13:03 -07002874static void rs_fill_link_cmd(struct iwl_priv *priv,
Tomas Winklere227cea2008-07-18 13:53:05 +08002875 struct iwl_lq_sta *lq_sta, u32 new_rate)
Zhu Yib481de92007-09-25 17:54:57 -07002876{
Tomas Winklere227cea2008-07-18 13:53:05 +08002877 struct iwl_scale_tbl_info tbl_type;
Zhu Yib481de92007-09-25 17:54:57 -07002878 int index = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002879 int rate_idx;
Zhu Yi1b696de2007-09-27 11:27:41 +08002880 int repeat_rate = 0;
Guy Cohenaade00c2008-04-23 17:14:59 -07002881 u8 ant_toggle_cnt = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002882 u8 use_ht_possible = 1;
Guy Cohenaade00c2008-04-23 17:14:59 -07002883 u8 valid_tx_ant = 0;
Guy Cohen07bc28e2008-04-23 17:15:03 -07002884 struct iwl_link_quality_cmd *lq_cmd = &lq_sta->lq;
Zhu Yib481de92007-09-25 17:54:57 -07002885
Ben Cahill77626352007-11-29 11:09:44 +08002886 /* Override starting rate (index 0) if needed for debug purposes */
Guy Cohen39e88502008-04-23 17:14:57 -07002887 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Zhu Yi98d7e092007-09-27 11:27:42 +08002888
Guy Cohen39e88502008-04-23 17:14:57 -07002889 /* Interpret new_rate (rate_n_flags) */
2890 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band,
Zhu Yib481de92007-09-25 17:54:57 -07002891 &tbl_type, &rate_idx);
2892
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07002893 if (priv && priv->bt_full_concurrent) {
2894 /* 1x1 only */
2895 tbl_type.ant_type =
2896 first_antenna(priv->hw_params.valid_tx_ant);
2897 }
2898
Ben Cahill77626352007-11-29 11:09:44 +08002899 /* How many times should we repeat the initial rate? */
Zhu Yib481de92007-09-25 17:54:57 -07002900 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07002901 ant_toggle_cnt = 1;
Zhu Yi1b696de2007-09-27 11:27:41 +08002902 repeat_rate = IWL_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002903 } else {
Zhu Yi1b696de2007-09-27 11:27:41 +08002904 repeat_rate = IWL_HT_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002905 }
Zhu Yib481de92007-09-25 17:54:57 -07002906
2907 lq_cmd->general_params.mimo_delimiter =
2908 is_mimo(tbl_type.lq_type) ? 1 : 0;
Ben Cahill77626352007-11-29 11:09:44 +08002909
2910 /* Fill 1st table entry (index 0) */
Guy Cohen39e88502008-04-23 17:14:57 -07002911 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002912
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07002913 if (num_of_ant(tbl_type.ant_type) == 1) {
Guy Cohen07bc28e2008-04-23 17:15:03 -07002914 lq_cmd->general_params.single_stream_ant_msk =
2915 tbl_type.ant_type;
2916 } else if (num_of_ant(tbl_type.ant_type) == 2) {
2917 lq_cmd->general_params.dual_stream_ant_msk =
2918 tbl_type.ant_type;
2919 } /* otherwise we don't modify the existing value */
Zhu Yib481de92007-09-25 17:54:57 -07002920
2921 index++;
Zhu Yi1b696de2007-09-27 11:27:41 +08002922 repeat_rate--;
Wey-Yi Guybee008b2010-08-23 07:57:04 -07002923 if (priv) {
2924 if (priv->bt_full_concurrent)
2925 valid_tx_ant = ANT_A;
2926 else
2927 valid_tx_ant = priv->hw_params.valid_tx_ant;
2928 }
Guy Cohenaade00c2008-04-23 17:14:59 -07002929
Ben Cahill77626352007-11-29 11:09:44 +08002930 /* Fill rest of rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002931 while (index < LINK_QUAL_MAX_RETRY_NUM) {
Ben Cahill77626352007-11-29 11:09:44 +08002932 /* Repeat initial/next rate.
2933 * For legacy IWL_NUMBER_TRY == 1, this loop will not execute.
2934 * For HT IWL_HT_NUMBER_TRY == 3, this executes twice. */
Zhu Yi1b696de2007-09-27 11:27:41 +08002935 while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
Zhu Yib481de92007-09-25 17:54:57 -07002936 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07002937 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2938 ant_toggle_cnt++;
2939 else if (priv &&
2940 rs_toggle_antenna(valid_tx_ant,
2941 &new_rate, &tbl_type))
2942 ant_toggle_cnt = 1;
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07002943 }
Zhu Yi98d7e092007-09-27 11:27:42 +08002944
Ben Cahill77626352007-11-29 11:09:44 +08002945 /* Override next rate if needed for debug purposes */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002946 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Ben Cahill77626352007-11-29 11:09:44 +08002947
2948 /* Fill next table entry */
Zhu Yib481de92007-09-25 17:54:57 -07002949 lq_cmd->rs_table[index].rate_n_flags =
Guy Cohen39e88502008-04-23 17:14:57 -07002950 cpu_to_le32(new_rate);
Zhu Yi1b696de2007-09-27 11:27:41 +08002951 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002952 index++;
2953 }
2954
Guy Cohen39e88502008-04-23 17:14:57 -07002955 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band, &tbl_type,
Zhu Yib481de92007-09-25 17:54:57 -07002956 &rate_idx);
2957
Wey-Yi Guyda5dbb92010-08-23 07:57:13 -07002958 if (priv && priv->bt_full_concurrent) {
2959 /* 1x1 only */
2960 tbl_type.ant_type =
2961 first_antenna(priv->hw_params.valid_tx_ant);
2962 }
2963
Ben Cahill77626352007-11-29 11:09:44 +08002964 /* Indicate to uCode which entries might be MIMO.
2965 * If initial rate was MIMO, this will finally end up
2966 * as (IWL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
Zhu Yib481de92007-09-25 17:54:57 -07002967 if (is_mimo(tbl_type.lq_type))
2968 lq_cmd->general_params.mimo_delimiter = index;
2969
Ben Cahill77626352007-11-29 11:09:44 +08002970 /* Get next rate */
Guy Cohen39e88502008-04-23 17:14:57 -07002971 new_rate = rs_get_lower_rate(lq_sta, &tbl_type, rate_idx,
2972 use_ht_possible);
Zhu Yib481de92007-09-25 17:54:57 -07002973
Ben Cahill77626352007-11-29 11:09:44 +08002974 /* How many times should we repeat the next rate? */
Zhu Yib481de92007-09-25 17:54:57 -07002975 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07002976 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2977 ant_toggle_cnt++;
2978 else if (priv &&
2979 rs_toggle_antenna(valid_tx_ant,
2980 &new_rate, &tbl_type))
2981 ant_toggle_cnt = 1;
2982
Zhu Yi1b696de2007-09-27 11:27:41 +08002983 repeat_rate = IWL_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002984 } else {
Zhu Yi1b696de2007-09-27 11:27:41 +08002985 repeat_rate = IWL_HT_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002986 }
Zhu Yib481de92007-09-25 17:54:57 -07002987
Ben Cahill77626352007-11-29 11:09:44 +08002988 /* Don't allow HT rates after next pass.
2989 * rs_get_lower_rate() will change type to LQ_A or LQ_G. */
Zhu Yib481de92007-09-25 17:54:57 -07002990 use_ht_possible = 0;
2991
Ben Cahill77626352007-11-29 11:09:44 +08002992 /* Override next rate if needed for debug purposes */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002993 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Ben Cahill77626352007-11-29 11:09:44 +08002994
2995 /* Fill next table entry */
Guy Cohen39e88502008-04-23 17:14:57 -07002996 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002997
2998 index++;
Zhu Yi1b696de2007-09-27 11:27:41 +08002999 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07003000 }
3001
Wey-Yi Guy4d80d722009-10-02 13:44:01 -07003002 lq_cmd->agg_params.agg_frame_cnt_limit = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
Wey-Yi Guy13c33a02009-06-03 11:44:11 -07003003 lq_cmd->agg_params.agg_dis_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
Wey-Yi Guy74697012010-07-28 09:18:54 -07003004
Wey-Yi Guy13c33a02009-06-03 11:44:11 -07003005 lq_cmd->agg_params.agg_time_limit =
3006 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
Wey-Yi Guy74697012010-07-28 09:18:54 -07003007 /*
3008 * overwrite if needed, pass aggregation time limit
3009 * to uCode in uSec
3010 */
Wey-Yi Guy7cb1b082010-10-06 08:10:00 -07003011 if (priv && priv->cfg->bt_params &&
3012 priv->cfg->bt_params->agg_time_limit &&
3013 priv->cfg->bt_params->agg_time_limit >=
3014 LINK_QUAL_AGG_TIME_LIMIT_MIN &&
3015 priv->cfg->bt_params->agg_time_limit <=
3016 LINK_QUAL_AGG_TIME_LIMIT_MAX)
Wey-Yi Guy74697012010-07-28 09:18:54 -07003017 lq_cmd->agg_params.agg_time_limit =
Wey-Yi Guy7cb1b082010-10-06 08:10:00 -07003018 cpu_to_le16(priv->cfg->bt_params->agg_time_limit);
Zhu Yib481de92007-09-25 17:54:57 -07003019}
3020
Johannes Berg4b7679a2008-09-18 18:14:18 +02003021static void *rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
Zhu Yib481de92007-09-25 17:54:57 -07003022{
Johannes Berg4b7679a2008-09-18 18:14:18 +02003023 return hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003024}
3025/* rate scale requires free function to be implemented */
3026static void rs_free(void *priv_rate)
3027{
3028 return;
3029}
3030
Johannes Berg4b7679a2008-09-18 18:14:18 +02003031static void rs_free_sta(void *priv_r, struct ieee80211_sta *sta,
3032 void *priv_sta)
Zhu Yib481de92007-09-25 17:54:57 -07003033{
Rami Rosen45527c22008-10-07 09:50:01 +02003034 struct iwl_priv *priv __maybe_unused = priv_r;
Zhu Yib481de92007-09-25 17:54:57 -07003035
Tomas Winklere1623442009-01-27 14:27:56 -08003036 IWL_DEBUG_RATE(priv, "enter\n");
Tomas Winklere1623442009-01-27 14:27:56 -08003037 IWL_DEBUG_RATE(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003038}
3039
3040
Zhu Yi93dc6462007-09-27 11:27:37 +08003041#ifdef CONFIG_MAC80211_DEBUGFS
Zhu Yi5ae212c2007-09-27 11:27:38 +08003042static int open_file_generic(struct inode *inode, struct file *file)
3043{
3044 file->private_data = inode->i_private;
3045 return 0;
3046}
Tomas Winklere227cea2008-07-18 13:53:05 +08003047static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
3048 u32 *rate_n_flags, int index)
Zhu Yi98d7e092007-09-27 11:27:42 +08003049{
Ester Kummerbf403db2008-05-05 10:22:40 +08003050 struct iwl_priv *priv;
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07003051 u8 valid_tx_ant;
3052 u8 ant_sel_tx;
Ester Kummerbf403db2008-05-05 10:22:40 +08003053
3054 priv = lq_sta->drv;
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07003055 valid_tx_ant = priv->hw_params.valid_tx_ant;
Guy Cohen39e88502008-04-23 17:14:57 -07003056 if (lq_sta->dbg_fixed_rate) {
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07003057 ant_sel_tx =
3058 ((lq_sta->dbg_fixed_rate & RATE_MCS_ANT_ABC_MSK)
3059 >> RATE_MCS_ANT_POS);
3060 if ((valid_tx_ant & ant_sel_tx) == ant_sel_tx) {
Guy Cohen39e88502008-04-23 17:14:57 -07003061 *rate_n_flags = lq_sta->dbg_fixed_rate;
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07003062 IWL_DEBUG_RATE(priv, "Fixed rate ON\n");
Guy Cohen39e88502008-04-23 17:14:57 -07003063 } else {
Wey-Yi Guy3c4955f2009-03-10 14:35:12 -07003064 lq_sta->dbg_fixed_rate = 0;
3065 IWL_ERR(priv,
3066 "Invalid antenna selection 0x%X, Valid is 0x%X\n",
3067 ant_sel_tx, valid_tx_ant);
3068 IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
Guy Cohen39e88502008-04-23 17:14:57 -07003069 }
Guy Cohen39e88502008-04-23 17:14:57 -07003070 } else {
Tomas Winklere1623442009-01-27 14:27:56 -08003071 IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
Zhu Yi98d7e092007-09-27 11:27:42 +08003072 }
Zhu Yi98d7e092007-09-27 11:27:42 +08003073}
3074
3075static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
3076 const char __user *user_buf, size_t count, loff_t *ppos)
3077{
Tomas Winklere227cea2008-07-18 13:53:05 +08003078 struct iwl_lq_sta *lq_sta = file->private_data;
Ester Kummerbf403db2008-05-05 10:22:40 +08003079 struct iwl_priv *priv;
Zhu Yi98d7e092007-09-27 11:27:42 +08003080 char buf[64];
3081 int buf_size;
3082 u32 parsed_rate;
Johannes Berg7e6a5882010-08-23 10:46:46 +02003083 struct iwl_station_priv *sta_priv =
3084 container_of(lq_sta, struct iwl_station_priv, lq_sta);
3085 struct iwl_rxon_context *ctx = sta_priv->common.ctx;
Zhu Yi98d7e092007-09-27 11:27:42 +08003086
Ester Kummerbf403db2008-05-05 10:22:40 +08003087 priv = lq_sta->drv;
Zhu Yi98d7e092007-09-27 11:27:42 +08003088 memset(buf, 0, sizeof(buf));
3089 buf_size = min(count, sizeof(buf) - 1);
3090 if (copy_from_user(buf, user_buf, buf_size))
3091 return -EFAULT;
3092
3093 if (sscanf(buf, "%x", &parsed_rate) == 1)
Guy Cohen39e88502008-04-23 17:14:57 -07003094 lq_sta->dbg_fixed_rate = parsed_rate;
Zhu Yi98d7e092007-09-27 11:27:42 +08003095 else
Guy Cohen39e88502008-04-23 17:14:57 -07003096 lq_sta->dbg_fixed_rate = 0;
Zhu Yi98d7e092007-09-27 11:27:42 +08003097
Guy Cohen39e88502008-04-23 17:14:57 -07003098 lq_sta->active_legacy_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
3099 lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
3100 lq_sta->active_mimo2_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
3101 lq_sta->active_mimo3_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
Zhu Yi98d7e092007-09-27 11:27:42 +08003102
Tomas Winklere1623442009-01-27 14:27:56 -08003103 IWL_DEBUG_RATE(priv, "sta_id %d rate 0x%X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07003104 lq_sta->lq.sta_id, lq_sta->dbg_fixed_rate);
Zhu Yi98d7e092007-09-27 11:27:42 +08003105
Guy Cohen39e88502008-04-23 17:14:57 -07003106 if (lq_sta->dbg_fixed_rate) {
Guy Cohen07bc28e2008-04-23 17:15:03 -07003107 rs_fill_link_cmd(NULL, lq_sta, lq_sta->dbg_fixed_rate);
Johannes Berg7e6a5882010-08-23 10:46:46 +02003108 iwl_send_lq_cmd(lq_sta->drv, ctx, &lq_sta->lq, CMD_ASYNC,
3109 false);
Zhu Yi98d7e092007-09-27 11:27:42 +08003110 }
3111
3112 return count;
3113}
Zhu Yi0209dc12007-09-27 11:27:43 +08003114
Zhu Yi5ae212c2007-09-27 11:27:38 +08003115static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
3116 char __user *user_buf, size_t count, loff_t *ppos)
3117{
Frank Seidela412c802009-03-01 20:25:38 +01003118 char *buff;
Zhu Yi5ae212c2007-09-27 11:27:38 +08003119 int desc = 0;
3120 int i = 0;
Wey-Yi Guy12b96812009-04-08 11:39:27 -07003121 int index = 0;
Frank Seidela412c802009-03-01 20:25:38 +01003122 ssize_t ret;
Zhu Yi5ae212c2007-09-27 11:27:38 +08003123
Tomas Winklere227cea2008-07-18 13:53:05 +08003124 struct iwl_lq_sta *lq_sta = file->private_data;
Wey-Yi Guyd8ae4f52009-03-10 14:35:07 -07003125 struct iwl_priv *priv;
Wey-Yi Guyadb7b5e2009-03-10 14:35:08 -07003126 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
Zhu Yi5ae212c2007-09-27 11:27:38 +08003127
Wey-Yi Guyd8ae4f52009-03-10 14:35:07 -07003128 priv = lq_sta->drv;
Frank Seidela412c802009-03-01 20:25:38 +01003129 buff = kmalloc(1024, GFP_KERNEL);
3130 if (!buff)
3131 return -ENOMEM;
3132
Tomas Winklerc33104f2008-01-14 17:46:21 -08003133 desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
Zhu Yi98d7e092007-09-27 11:27:42 +08003134 desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08003135 lq_sta->total_failed, lq_sta->total_success,
Guy Cohen39e88502008-04-23 17:14:57 -07003136 lq_sta->active_legacy_rate);
Zhu Yi98d7e092007-09-27 11:27:42 +08003137 desc += sprintf(buff+desc, "fixed rate 0x%X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07003138 lq_sta->dbg_fixed_rate);
Wey-Yi Guyd8ae4f52009-03-10 14:35:07 -07003139 desc += sprintf(buff+desc, "valid_tx_ant %s%s%s\n",
3140 (priv->hw_params.valid_tx_ant & ANT_A) ? "ANT_A," : "",
3141 (priv->hw_params.valid_tx_ant & ANT_B) ? "ANT_B," : "",
3142 (priv->hw_params.valid_tx_ant & ANT_C) ? "ANT_C" : "");
Wey-Yi Guyadb7b5e2009-03-10 14:35:08 -07003143 desc += sprintf(buff+desc, "lq type %s\n",
3144 (is_legacy(tbl->lq_type)) ? "legacy" : "HT");
3145 if (is_Ht(tbl->lq_type)) {
3146 desc += sprintf(buff+desc, " %s",
3147 (is_siso(tbl->lq_type)) ? "SISO" :
3148 ((is_mimo2(tbl->lq_type)) ? "MIMO2" : "MIMO3"));
3149 desc += sprintf(buff+desc, " %s",
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07003150 (tbl->is_ht40) ? "40MHz" : "20MHz");
Daniel C Halperine3949d62009-09-17 10:43:50 -07003151 desc += sprintf(buff+desc, " %s %s %s\n", (tbl->is_SGI) ? "SGI" : "",
3152 (lq_sta->is_green) ? "GF enabled" : "",
3153 (lq_sta->is_agg) ? "AGG on" : "");
Wey-Yi Guyadb7b5e2009-03-10 14:35:08 -07003154 }
Wey-Yi Guy12b96812009-04-08 11:39:27 -07003155 desc += sprintf(buff+desc, "last tx rate=0x%X\n",
3156 lq_sta->last_rate_n_flags);
Zhu Yi5ae212c2007-09-27 11:27:38 +08003157 desc += sprintf(buff+desc, "general:"
3158 "flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08003159 lq_sta->lq.general_params.flags,
3160 lq_sta->lq.general_params.mimo_delimiter,
3161 lq_sta->lq.general_params.single_stream_ant_msk,
3162 lq_sta->lq.general_params.dual_stream_ant_msk);
Zhu Yi5ae212c2007-09-27 11:27:38 +08003163
3164 desc += sprintf(buff+desc, "agg:"
3165 "time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08003166 le16_to_cpu(lq_sta->lq.agg_params.agg_time_limit),
3167 lq_sta->lq.agg_params.agg_dis_start_th,
3168 lq_sta->lq.agg_params.agg_frame_cnt_limit);
Zhu Yi5ae212c2007-09-27 11:27:38 +08003169
3170 desc += sprintf(buff+desc,
3171 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08003172 lq_sta->lq.general_params.start_rate_index[0],
3173 lq_sta->lq.general_params.start_rate_index[1],
3174 lq_sta->lq.general_params.start_rate_index[2],
3175 lq_sta->lq.general_params.start_rate_index[3]);
Zhu Yi5ae212c2007-09-27 11:27:38 +08003176
Wey-Yi Guy12b96812009-04-08 11:39:27 -07003177 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
3178 index = iwl_hwrate_to_plcp_idx(
3179 le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags));
3180 if (is_legacy(tbl->lq_type)) {
3181 desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps\n",
3182 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags),
3183 iwl_rate_mcs[index].mbps);
3184 } else {
3185 desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps (%s)\n",
3186 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags),
3187 iwl_rate_mcs[index].mbps, iwl_rate_mcs[index].mcs);
3188 }
3189 }
Zhu Yi5ae212c2007-09-27 11:27:38 +08003190
Frank Seidela412c802009-03-01 20:25:38 +01003191 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
3192 kfree(buff);
3193 return ret;
Zhu Yi5ae212c2007-09-27 11:27:38 +08003194}
3195
3196static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
Zhu Yi98d7e092007-09-27 11:27:42 +08003197 .write = rs_sta_dbgfs_scale_table_write,
Zhu Yi5ae212c2007-09-27 11:27:38 +08003198 .read = rs_sta_dbgfs_scale_table_read,
3199 .open = open_file_generic,
3200};
Zhu Yi0209dc12007-09-27 11:27:43 +08003201static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
3202 char __user *user_buf, size_t count, loff_t *ppos)
3203{
Frank Seidela412c802009-03-01 20:25:38 +01003204 char *buff;
Zhu Yi0209dc12007-09-27 11:27:43 +08003205 int desc = 0;
3206 int i, j;
Frank Seidela412c802009-03-01 20:25:38 +01003207 ssize_t ret;
Zhu Yi0209dc12007-09-27 11:27:43 +08003208
Tomas Winklere227cea2008-07-18 13:53:05 +08003209 struct iwl_lq_sta *lq_sta = file->private_data;
Frank Seidela412c802009-03-01 20:25:38 +01003210
3211 buff = kmalloc(1024, GFP_KERNEL);
3212 if (!buff)
3213 return -ENOMEM;
3214
Zhu Yi0209dc12007-09-27 11:27:43 +08003215 for (i = 0; i < LQ_SIZE; i++) {
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07003216 desc += sprintf(buff+desc,
3217 "%s type=%d SGI=%d HT40=%d DUP=%d GF=%d\n"
Zhu Yi0209dc12007-09-27 11:27:43 +08003218 "rate=0x%X\n",
Abhijeet Kolekarc3056062008-11-12 13:14:08 -08003219 lq_sta->active_tbl == i ? "*" : "x",
Tomas Winklerc33104f2008-01-14 17:46:21 -08003220 lq_sta->lq_info[i].lq_type,
3221 lq_sta->lq_info[i].is_SGI,
Wey-Yi Guy7aafef12009-08-07 15:41:38 -07003222 lq_sta->lq_info[i].is_ht40,
Tomas Winklerc33104f2008-01-14 17:46:21 -08003223 lq_sta->lq_info[i].is_dup,
Wey-Yi Guy2681b202009-05-21 13:44:22 -07003224 lq_sta->is_green,
Guy Cohen39e88502008-04-23 17:14:57 -07003225 lq_sta->lq_info[i].current_rate);
Zhu Yi0209dc12007-09-27 11:27:43 +08003226 for (j = 0; j < IWL_RATE_COUNT; j++) {
3227 desc += sprintf(buff+desc,
Tomas Winklerc33104f2008-01-14 17:46:21 -08003228 "counter=%d success=%d %%=%d\n",
3229 lq_sta->lq_info[i].win[j].counter,
3230 lq_sta->lq_info[i].win[j].success_counter,
3231 lq_sta->lq_info[i].win[j].success_ratio);
Zhu Yi0209dc12007-09-27 11:27:43 +08003232 }
3233 }
Frank Seidela412c802009-03-01 20:25:38 +01003234 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
3235 kfree(buff);
3236 return ret;
Zhu Yi0209dc12007-09-27 11:27:43 +08003237}
3238
3239static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
3240 .read = rs_sta_dbgfs_stats_table_read,
3241 .open = open_file_generic,
3242};
Zhu Yi5ae212c2007-09-27 11:27:38 +08003243
Wey-Yi Guy38167452009-05-08 13:44:43 -07003244static ssize_t rs_sta_dbgfs_rate_scale_data_read(struct file *file,
3245 char __user *user_buf, size_t count, loff_t *ppos)
3246{
3247 char buff[120];
3248 int desc = 0;
3249 ssize_t ret;
3250
3251 struct iwl_lq_sta *lq_sta = file->private_data;
3252 struct iwl_priv *priv;
3253 struct iwl_scale_tbl_info *tbl = &lq_sta->lq_info[lq_sta->active_tbl];
3254
3255 priv = lq_sta->drv;
3256
3257 if (is_Ht(tbl->lq_type))
3258 desc += sprintf(buff+desc,
3259 "Bit Rate= %d Mb/s\n",
3260 tbl->expected_tpt[lq_sta->last_txrate_idx]);
3261 else
3262 desc += sprintf(buff+desc,
3263 "Bit Rate= %d Mb/s\n",
3264 iwl_rates[lq_sta->last_txrate_idx].ieee >> 1);
Wey-Yi Guy38167452009-05-08 13:44:43 -07003265
3266 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
3267 return ret;
3268}
3269
3270static const struct file_operations rs_sta_dbgfs_rate_scale_data_ops = {
3271 .read = rs_sta_dbgfs_rate_scale_data_read,
3272 .open = open_file_generic,
3273};
3274
Zhu Yi93dc6462007-09-27 11:27:37 +08003275static void rs_add_debugfs(void *priv, void *priv_sta,
3276 struct dentry *dir)
3277{
Tomas Winklere227cea2008-07-18 13:53:05 +08003278 struct iwl_lq_sta *lq_sta = priv_sta;
Tomas Winklerc33104f2008-01-14 17:46:21 -08003279 lq_sta->rs_sta_dbgfs_scale_table_file =
Wey-Yi Guy43e85112009-11-20 12:04:58 -08003280 debugfs_create_file("rate_scale_table", S_IRUSR | S_IWUSR, dir,
Tomas Winklerc33104f2008-01-14 17:46:21 -08003281 lq_sta, &rs_sta_dbgfs_scale_table_ops);
3282 lq_sta->rs_sta_dbgfs_stats_table_file =
Wey-Yi Guy43e85112009-11-20 12:04:58 -08003283 debugfs_create_file("rate_stats_table", S_IRUSR, dir,
Tomas Winklerc33104f2008-01-14 17:46:21 -08003284 lq_sta, &rs_sta_dbgfs_stats_table_ops);
Wey-Yi Guy38167452009-05-08 13:44:43 -07003285 lq_sta->rs_sta_dbgfs_rate_scale_data_file =
Wey-Yi Guy43e85112009-11-20 12:04:58 -08003286 debugfs_create_file("rate_scale_data", S_IRUSR, dir,
Wey-Yi Guy38167452009-05-08 13:44:43 -07003287 lq_sta, &rs_sta_dbgfs_rate_scale_data_ops);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02003288 lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file =
Wey-Yi Guy43e85112009-11-20 12:04:58 -08003289 debugfs_create_u8("tx_agg_tid_enable", S_IRUSR | S_IWUSR, dir,
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02003290 &lq_sta->tx_agg_tid_en);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02003291
Zhu Yi93dc6462007-09-27 11:27:37 +08003292}
3293
3294static void rs_remove_debugfs(void *priv, void *priv_sta)
3295{
Tomas Winklere227cea2008-07-18 13:53:05 +08003296 struct iwl_lq_sta *lq_sta = priv_sta;
Tomas Winklerc33104f2008-01-14 17:46:21 -08003297 debugfs_remove(lq_sta->rs_sta_dbgfs_scale_table_file);
3298 debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
Wey-Yi Guy38167452009-05-08 13:44:43 -07003299 debugfs_remove(lq_sta->rs_sta_dbgfs_rate_scale_data_file);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02003300 debugfs_remove(lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file);
Zhu Yi93dc6462007-09-27 11:27:37 +08003301}
3302#endif
3303
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003304/*
3305 * Initialization of rate scaling information is done by driver after
3306 * the station is added. Since mac80211 calls this function before a
3307 * station is added we ignore it.
3308 */
3309static void rs_rate_init_stub(void *priv_r, struct ieee80211_supported_band *sband,
3310 struct ieee80211_sta *sta, void *priv_sta)
3311{
3312}
Zhu Yib481de92007-09-25 17:54:57 -07003313static struct rate_control_ops rs_ops = {
3314 .module = NULL,
3315 .name = RS_NAME,
3316 .tx_status = rs_tx_status,
3317 .get_rate = rs_get_rate,
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003318 .rate_init = rs_rate_init_stub,
Zhu Yib481de92007-09-25 17:54:57 -07003319 .alloc = rs_alloc,
3320 .free = rs_free,
3321 .alloc_sta = rs_alloc_sta,
3322 .free_sta = rs_free_sta,
Zhu Yi93dc6462007-09-27 11:27:37 +08003323#ifdef CONFIG_MAC80211_DEBUGFS
3324 .add_sta_debugfs = rs_add_debugfs,
3325 .remove_sta_debugfs = rs_remove_debugfs,
3326#endif
Zhu Yib481de92007-09-25 17:54:57 -07003327};
3328
Tomas Winklere227cea2008-07-18 13:53:05 +08003329int iwlagn_rate_control_register(void)
Zhu Yib481de92007-09-25 17:54:57 -07003330{
Reinette Chatre897e1cf2008-03-28 16:21:09 -07003331 return ieee80211_rate_control_register(&rs_ops);
Zhu Yib481de92007-09-25 17:54:57 -07003332}
3333
Tomas Winklere227cea2008-07-18 13:53:05 +08003334void iwlagn_rate_control_unregister(void)
Zhu Yib481de92007-09-25 17:54:57 -07003335{
3336 ieee80211_rate_control_unregister(&rs_ops);
3337}
3338