iwlegacy: remove DEBUG_LIMIT
[deliverable/linux.git] / drivers / net / wireless / iwlegacy / iwl-4965-rs.c
CommitLineData
be663ab6
WYG
1/******************************************************************************
2 *
3 * Copyright(c) 2005 - 2011 Intel Corporation. All rights reserved.
4 *
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:
22 * Intel Linux Wireless <ilw@linux.intel.com>
23 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24 *
25 *****************************************************************************/
26#include <linux/kernel.h>
27#include <linux/init.h>
28#include <linux/skbuff.h>
29#include <linux/slab.h>
be663ab6
WYG
30#include <net/mac80211.h>
31
32#include <linux/netdevice.h>
33#include <linux/etherdevice.h>
34#include <linux/delay.h>
35
36#include <linux/workqueue.h>
37
38#include "iwl-dev.h"
39#include "iwl-sta.h"
40#include "iwl-core.h"
41#include "iwl-4965.h"
42
43#define IWL4965_RS_NAME "iwl-4965-rs"
44
45#define NUM_TRY_BEFORE_ANT_TOGGLE 1
e2ebc833
SG
46#define IL_NUMBER_TRY 1
47#define IL_HT_NUMBER_TRY 3
be663ab6 48
e2ebc833
SG
49#define IL_RATE_MAX_WINDOW 62 /* # tx in history window */
50#define IL_RATE_MIN_FAILURE_TH 6 /* min failures to calc tpt */
51#define IL_RATE_MIN_SUCCESS_TH 8 /* min successes to calc tpt */
be663ab6
WYG
52
53/* max allowed rate miss before sync LQ cmd */
e2ebc833 54#define IL_MISSED_RATE_MAX 15
be663ab6 55/* max time to accum history 2 seconds */
e2ebc833 56#define IL_RATE_SCALE_FLUSH_INTVL (3*HZ)
be663ab6
WYG
57
58static u8 rs_ht_to_legacy[] = {
e2ebc833
SG
59 IL_RATE_6M_INDEX, IL_RATE_6M_INDEX,
60 IL_RATE_6M_INDEX, IL_RATE_6M_INDEX,
61 IL_RATE_6M_INDEX,
62 IL_RATE_6M_INDEX, IL_RATE_9M_INDEX,
63 IL_RATE_12M_INDEX, IL_RATE_18M_INDEX,
64 IL_RATE_24M_INDEX, IL_RATE_36M_INDEX,
65 IL_RATE_48M_INDEX, IL_RATE_54M_INDEX
be663ab6
WYG
66};
67
68static const u8 ant_toggle_lookup[] = {
69 /*ANT_NONE -> */ ANT_NONE,
70 /*ANT_A -> */ ANT_B,
71 /*ANT_B -> */ ANT_C,
72 /*ANT_AB -> */ ANT_BC,
73 /*ANT_C -> */ ANT_A,
74 /*ANT_AC -> */ ANT_AB,
75 /*ANT_BC -> */ ANT_AC,
76 /*ANT_ABC -> */ ANT_ABC,
77};
78
e2ebc833
SG
79#define IL_DECLARE_RATE_INFO(r, s, ip, in, rp, rn, pp, np) \
80 [IL_RATE_##r##M_INDEX] = { IL_RATE_##r##M_PLCP, \
81 IL_RATE_SISO_##s##M_PLCP, \
82 IL_RATE_MIMO2_##s##M_PLCP,\
83 IL_RATE_##r##M_IEEE, \
84 IL_RATE_##ip##M_INDEX, \
85 IL_RATE_##in##M_INDEX, \
86 IL_RATE_##rp##M_INDEX, \
87 IL_RATE_##rn##M_INDEX, \
88 IL_RATE_##pp##M_INDEX, \
89 IL_RATE_##np##M_INDEX }
be663ab6
WYG
90
91/*
92 * Parameter order:
93 * rate, ht rate, prev rate, next rate, prev tgg rate, next tgg rate
94 *
95 * If there isn't a valid next or previous rate then INV is used which
e2ebc833 96 * maps to IL_RATE_INVALID
be663ab6
WYG
97 *
98 */
d2ddf621 99const struct il_rate_info il_rates[IL_RATE_COUNT] = {
e2ebc833
SG
100 IL_DECLARE_RATE_INFO(1, INV, INV, 2, INV, 2, INV, 2), /* 1mbps */
101 IL_DECLARE_RATE_INFO(2, INV, 1, 5, 1, 5, 1, 5), /* 2mbps */
102 IL_DECLARE_RATE_INFO(5, INV, 2, 6, 2, 11, 2, 11), /*5.5mbps */
103 IL_DECLARE_RATE_INFO(11, INV, 9, 12, 9, 12, 5, 18), /* 11mbps */
104 IL_DECLARE_RATE_INFO(6, 6, 5, 9, 5, 11, 5, 11), /* 6mbps */
105 IL_DECLARE_RATE_INFO(9, 6, 6, 11, 6, 11, 5, 11), /* 9mbps */
106 IL_DECLARE_RATE_INFO(12, 12, 11, 18, 11, 18, 11, 18), /* 12mbps */
107 IL_DECLARE_RATE_INFO(18, 18, 12, 24, 12, 24, 11, 24), /* 18mbps */
108 IL_DECLARE_RATE_INFO(24, 24, 18, 36, 18, 36, 18, 36), /* 24mbps */
109 IL_DECLARE_RATE_INFO(36, 36, 24, 48, 24, 48, 24, 48), /* 36mbps */
110 IL_DECLARE_RATE_INFO(48, 48, 36, 54, 36, 54, 36, 54), /* 48mbps */
111 IL_DECLARE_RATE_INFO(54, 54, 48, INV, 48, INV, 48, INV),/* 54mbps */
112 IL_DECLARE_RATE_INFO(60, 60, 48, INV, 48, INV, 48, INV),/* 60mbps */
be663ab6
WYG
113};
114
e2ebc833 115static int il4965_hwrate_to_plcp_idx(u32 rate_n_flags)
be663ab6
WYG
116{
117 int idx = 0;
118
119 /* HT rate format */
120 if (rate_n_flags & RATE_MCS_HT_MSK) {
121 idx = (rate_n_flags & 0xff);
122
e2ebc833
SG
123 if (idx >= IL_RATE_MIMO2_6M_PLCP)
124 idx = idx - IL_RATE_MIMO2_6M_PLCP;
be663ab6 125
e2ebc833 126 idx += IL_FIRST_OFDM_RATE;
be663ab6 127 /* skip 9M not supported in ht*/
e2ebc833 128 if (idx >= IL_RATE_9M_INDEX)
be663ab6 129 idx += 1;
e2ebc833 130 if ((idx >= IL_FIRST_OFDM_RATE) && (idx <= IL_LAST_OFDM_RATE))
be663ab6
WYG
131 return idx;
132
133 /* legacy rate format, search for match in table */
134 } else {
d2ddf621
SG
135 for (idx = 0; idx < ARRAY_SIZE(il_rates); idx++)
136 if (il_rates[idx].plcp == (rate_n_flags & 0xFF))
be663ab6
WYG
137 return idx;
138 }
139
140 return -1;
141}
142
46bc8d4b 143static void il4965_rs_rate_scale_perform(struct il_priv *il,
be663ab6
WYG
144 struct sk_buff *skb,
145 struct ieee80211_sta *sta,
e2ebc833 146 struct il_lq_sta *lq_sta);
46bc8d4b 147static void il4965_rs_fill_link_cmd(struct il_priv *il,
e2ebc833
SG
148 struct il_lq_sta *lq_sta, u32 rate_n_flags);
149static void il4965_rs_stay_in_table(struct il_lq_sta *lq_sta,
be663ab6
WYG
150 bool force_search);
151
152#ifdef CONFIG_MAC80211_DEBUGFS
e2ebc833 153static void il4965_rs_dbgfs_set_mcs(struct il_lq_sta *lq_sta,
be663ab6
WYG
154 u32 *rate_n_flags, int index);
155#else
e2ebc833 156static void il4965_rs_dbgfs_set_mcs(struct il_lq_sta *lq_sta,
be663ab6
WYG
157 u32 *rate_n_flags, int index)
158{}
159#endif
160
161/**
162 * The following tables contain the expected throughput metrics for all rates
163 *
164 * 1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
165 *
166 * where invalid entries are zeros.
167 *
168 * CCK rates are only valid in legacy table and will only be used in G
169 * (2.4 GHz) band.
170 */
171
e2ebc833 172static s32 expected_tpt_legacy[IL_RATE_COUNT] = {
be663ab6
WYG
173 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 0
174};
175
e2ebc833 176static s32 expected_tpt_siso20MHz[4][IL_RATE_COUNT] = {
be663ab6
WYG
177 {0, 0, 0, 0, 42, 0, 76, 102, 124, 158, 183, 193, 202}, /* Norm */
178 {0, 0, 0, 0, 46, 0, 82, 110, 132, 167, 192, 202, 210}, /* SGI */
179 {0, 0, 0, 0, 48, 0, 93, 135, 176, 251, 319, 351, 381}, /* AGG */
180 {0, 0, 0, 0, 53, 0, 102, 149, 193, 275, 348, 381, 413}, /* AGG+SGI */
181};
182
e2ebc833 183static s32 expected_tpt_siso40MHz[4][IL_RATE_COUNT] = {
be663ab6
WYG
184 {0, 0, 0, 0, 77, 0, 127, 160, 184, 220, 242, 250, 257}, /* Norm */
185 {0, 0, 0, 0, 83, 0, 135, 169, 193, 229, 250, 257, 264}, /* SGI */
186 {0, 0, 0, 0, 96, 0, 182, 259, 328, 451, 553, 598, 640}, /* AGG */
187 {0, 0, 0, 0, 106, 0, 199, 282, 357, 487, 593, 640, 683}, /* AGG+SGI */
188};
189
e2ebc833 190static s32 expected_tpt_mimo2_20MHz[4][IL_RATE_COUNT] = {
be663ab6
WYG
191 {0, 0, 0, 0, 74, 0, 123, 155, 179, 213, 235, 243, 250}, /* Norm */
192 {0, 0, 0, 0, 81, 0, 131, 164, 187, 221, 242, 250, 256}, /* SGI */
193 {0, 0, 0, 0, 92, 0, 175, 250, 317, 436, 534, 578, 619}, /* AGG */
194 {0, 0, 0, 0, 102, 0, 192, 273, 344, 470, 573, 619, 660}, /* AGG+SGI*/
195};
196
e2ebc833 197static s32 expected_tpt_mimo2_40MHz[4][IL_RATE_COUNT] = {
be663ab6
WYG
198 {0, 0, 0, 0, 123, 0, 182, 214, 235, 264, 279, 285, 289}, /* Norm */
199 {0, 0, 0, 0, 131, 0, 191, 222, 242, 270, 284, 289, 293}, /* SGI */
200 {0, 0, 0, 0, 180, 0, 327, 446, 545, 708, 828, 878, 922}, /* AGG */
201 {0, 0, 0, 0, 197, 0, 355, 481, 584, 752, 872, 922, 966}, /* AGG+SGI */
202};
203
204/* mbps, mcs */
e2ebc833 205static const struct il_rate_mcs_info il_rate_mcs[IL_RATE_COUNT] = {
be663ab6
WYG
206 { "1", "BPSK DSSS"},
207 { "2", "QPSK DSSS"},
208 {"5.5", "BPSK CCK"},
209 { "11", "QPSK CCK"},
210 { "6", "BPSK 1/2"},
211 { "9", "BPSK 1/2"},
212 { "12", "QPSK 1/2"},
213 { "18", "QPSK 3/4"},
214 { "24", "16QAM 1/2"},
215 { "36", "16QAM 3/4"},
216 { "48", "64QAM 2/3"},
217 { "54", "64QAM 3/4"},
218 { "60", "64QAM 5/6"},
219};
220
221#define MCS_INDEX_PER_STREAM (8)
222
e2ebc833 223static inline u8 il4965_rs_extract_rate(u32 rate_n_flags)
be663ab6
WYG
224{
225 return (u8)(rate_n_flags & 0xFF);
226}
227
228static void
e2ebc833 229il4965_rs_rate_scale_clear_window(struct il_rate_scale_data *window)
be663ab6
WYG
230{
231 window->data = 0;
232 window->success_counter = 0;
e2ebc833 233 window->success_ratio = IL_INVALID_VALUE;
be663ab6 234 window->counter = 0;
e2ebc833 235 window->average_tpt = IL_INVALID_VALUE;
be663ab6
WYG
236 window->stamp = 0;
237}
238
e2ebc833 239static inline u8 il4965_rs_is_valid_ant(u8 valid_antenna, u8 ant_type)
be663ab6
WYG
240{
241 return (ant_type & valid_antenna) == ant_type;
242}
243
244/*
245 * removes the old data from the statistics. All data that is older than
246 * TID_MAX_TIME_DIFF, will be deleted.
247 */
248static void
e2ebc833 249il4965_rs_tl_rm_old_stats(struct il_traffic_load *tl, u32 curr_time)
be663ab6
WYG
250{
251 /* The oldest age we want to keep */
252 u32 oldest_time = curr_time - TID_MAX_TIME_DIFF;
253
254 while (tl->queue_count &&
255 (tl->time_stamp < oldest_time)) {
256 tl->total -= tl->packet_count[tl->head];
257 tl->packet_count[tl->head] = 0;
258 tl->time_stamp += TID_QUEUE_CELL_SPACING;
259 tl->queue_count--;
260 tl->head++;
261 if (tl->head >= TID_QUEUE_MAX_SIZE)
262 tl->head = 0;
263 }
264}
265
266/*
267 * increment traffic load value for tid and also remove
268 * any old values if passed the certain time period
269 */
e2ebc833 270static u8 il4965_rs_tl_add_packet(struct il_lq_sta *lq_data,
be663ab6
WYG
271 struct ieee80211_hdr *hdr)
272{
273 u32 curr_time = jiffies_to_msecs(jiffies);
274 u32 time_diff;
275 s32 index;
e2ebc833 276 struct il_traffic_load *tl = NULL;
be663ab6
WYG
277 u8 tid;
278
279 if (ieee80211_is_data_qos(hdr->frame_control)) {
280 u8 *qc = ieee80211_get_qos_ctl(hdr);
281 tid = qc[0] & 0xf;
282 } else
283 return MAX_TID_COUNT;
284
285 if (unlikely(tid >= TID_MAX_LOAD_COUNT))
286 return MAX_TID_COUNT;
287
288 tl = &lq_data->load[tid];
289
290 curr_time -= curr_time % TID_ROUND_VALUE;
291
292 /* Happens only for the first packet. Initialize the data */
293 if (!(tl->queue_count)) {
294 tl->total = 1;
295 tl->time_stamp = curr_time;
296 tl->queue_count = 1;
297 tl->head = 0;
298 tl->packet_count[0] = 1;
299 return MAX_TID_COUNT;
300 }
301
302 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
303 index = time_diff / TID_QUEUE_CELL_SPACING;
304
305 /* The history is too long: remove data that is older than */
306 /* TID_MAX_TIME_DIFF */
307 if (index >= TID_QUEUE_MAX_SIZE)
e2ebc833 308 il4965_rs_tl_rm_old_stats(tl, curr_time);
be663ab6
WYG
309
310 index = (tl->head + index) % TID_QUEUE_MAX_SIZE;
311 tl->packet_count[index] = tl->packet_count[index] + 1;
312 tl->total = tl->total + 1;
313
314 if ((index + 1) > tl->queue_count)
315 tl->queue_count = index + 1;
316
317 return tid;
318}
319
320/*
321 get the traffic load value for tid
322*/
e2ebc833 323static u32 il4965_rs_tl_get_load(struct il_lq_sta *lq_data, u8 tid)
be663ab6
WYG
324{
325 u32 curr_time = jiffies_to_msecs(jiffies);
326 u32 time_diff;
327 s32 index;
e2ebc833 328 struct il_traffic_load *tl = NULL;
be663ab6
WYG
329
330 if (tid >= TID_MAX_LOAD_COUNT)
331 return 0;
332
333 tl = &(lq_data->load[tid]);
334
335 curr_time -= curr_time % TID_ROUND_VALUE;
336
337 if (!(tl->queue_count))
338 return 0;
339
340 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
341 index = time_diff / TID_QUEUE_CELL_SPACING;
342
343 /* The history is too long: remove data that is older than */
344 /* TID_MAX_TIME_DIFF */
345 if (index >= TID_QUEUE_MAX_SIZE)
e2ebc833 346 il4965_rs_tl_rm_old_stats(tl, curr_time);
be663ab6
WYG
347
348 return tl->total;
349}
350
46bc8d4b 351static int il4965_rs_tl_turn_on_agg_for_tid(struct il_priv *il,
e2ebc833 352 struct il_lq_sta *lq_data, u8 tid,
be663ab6
WYG
353 struct ieee80211_sta *sta)
354{
355 int ret = -EAGAIN;
356 u32 load;
357
e2ebc833 358 load = il4965_rs_tl_get_load(lq_data, tid);
be663ab6 359
e2ebc833 360 if (load > IL_AGG_LOAD_THRESHOLD) {
46bc8d4b 361 IL_DEBUG_HT(il, "Starting Tx agg: STA: %pM tid: %d\n",
be663ab6
WYG
362 sta->addr, tid);
363 ret = ieee80211_start_tx_ba_session(sta, tid, 5000);
364 if (ret == -EAGAIN) {
365 /*
366 * driver and mac80211 is out of sync
367 * this might be cause by reloading firmware
368 * stop the tx ba session here
369 */
46bc8d4b 370 IL_ERR(il, "Fail start Tx agg on tid: %d\n",
be663ab6
WYG
371 tid);
372 ieee80211_stop_tx_ba_session(sta, tid);
373 }
374 } else {
46bc8d4b 375 IL_ERR(il, "Aggregation not enabled for tid %d "
be663ab6
WYG
376 "because load = %u\n", tid, load);
377 }
378 return ret;
379}
380
46bc8d4b 381static void il4965_rs_tl_turn_on_agg(struct il_priv *il, u8 tid,
e2ebc833 382 struct il_lq_sta *lq_data,
be663ab6
WYG
383 struct ieee80211_sta *sta)
384{
385 if (tid < TID_MAX_LOAD_COUNT)
46bc8d4b 386 il4965_rs_tl_turn_on_agg_for_tid(il, lq_data, tid, sta);
be663ab6 387 else
46bc8d4b 388 IL_ERR(il, "tid exceeds max load count: %d/%d\n",
be663ab6
WYG
389 tid, TID_MAX_LOAD_COUNT);
390}
391
e2ebc833 392static inline int il4965_get_il4965_num_of_ant_from_rate(u32 rate_n_flags)
be663ab6
WYG
393{
394 return !!(rate_n_flags & RATE_MCS_ANT_A_MSK) +
395 !!(rate_n_flags & RATE_MCS_ANT_B_MSK) +
396 !!(rate_n_flags & RATE_MCS_ANT_C_MSK);
397}
398
399/*
e2ebc833 400 * Static function to get the expected throughput from an il_scale_tbl_info
be663ab6
WYG
401 * that wraps a NULL pointer check
402 */
403static s32
e2ebc833 404il4965_get_expected_tpt(struct il_scale_tbl_info *tbl, int rs_index)
be663ab6
WYG
405{
406 if (tbl->expected_tpt)
407 return tbl->expected_tpt[rs_index];
408 return 0;
409}
410
411/**
e2ebc833 412 * il4965_rs_collect_tx_data - Update the success/failure sliding window
be663ab6
WYG
413 *
414 * We keep a sliding window of the last 62 packets transmitted
415 * at this rate. window->data contains the bitmask of successful
416 * packets.
417 */
e2ebc833 418static int il4965_rs_collect_tx_data(struct il_scale_tbl_info *tbl,
be663ab6
WYG
419 int scale_index, int attempts, int successes)
420{
e2ebc833
SG
421 struct il_rate_scale_data *window = NULL;
422 static const u64 mask = (((u64)1) << (IL_RATE_MAX_WINDOW - 1));
be663ab6
WYG
423 s32 fail_count, tpt;
424
e2ebc833 425 if (scale_index < 0 || scale_index >= IL_RATE_COUNT)
be663ab6
WYG
426 return -EINVAL;
427
428 /* Select window for current tx bit rate */
429 window = &(tbl->win[scale_index]);
430
431 /* Get expected throughput */
e2ebc833 432 tpt = il4965_get_expected_tpt(tbl, scale_index);
be663ab6
WYG
433
434 /*
435 * Keep track of only the latest 62 tx frame attempts in this rate's
436 * history window; anything older isn't really relevant any more.
437 * If we have filled up the sliding window, drop the oldest attempt;
438 * if the oldest attempt (highest bit in bitmap) shows "success",
439 * subtract "1" from the success counter (this is the main reason
440 * we keep these bitmaps!).
441 */
442 while (attempts > 0) {
e2ebc833 443 if (window->counter >= IL_RATE_MAX_WINDOW) {
be663ab6
WYG
444
445 /* remove earliest */
e2ebc833 446 window->counter = IL_RATE_MAX_WINDOW - 1;
be663ab6
WYG
447
448 if (window->data & mask) {
449 window->data &= ~mask;
450 window->success_counter--;
451 }
452 }
453
454 /* Increment frames-attempted counter */
455 window->counter++;
456
457 /* Shift bitmap by one frame to throw away oldest history */
458 window->data <<= 1;
459
460 /* Mark the most recent #successes attempts as successful */
461 if (successes > 0) {
462 window->success_counter++;
463 window->data |= 0x1;
464 successes--;
465 }
466
467 attempts--;
468 }
469
470 /* Calculate current success ratio, avoid divide-by-0! */
471 if (window->counter > 0)
472 window->success_ratio = 128 * (100 * window->success_counter)
473 / window->counter;
474 else
e2ebc833 475 window->success_ratio = IL_INVALID_VALUE;
be663ab6
WYG
476
477 fail_count = window->counter - window->success_counter;
478
479 /* Calculate average throughput, if we have enough history. */
e2ebc833
SG
480 if ((fail_count >= IL_RATE_MIN_FAILURE_TH) ||
481 (window->success_counter >= IL_RATE_MIN_SUCCESS_TH))
be663ab6
WYG
482 window->average_tpt = (window->success_ratio * tpt + 64) / 128;
483 else
e2ebc833 484 window->average_tpt = IL_INVALID_VALUE;
be663ab6
WYG
485
486 /* Tag this window as having been updated */
487 window->stamp = jiffies;
488
489 return 0;
490}
491
492/*
493 * Fill uCode API rate_n_flags field, based on "search" or "active" table.
494 */
46bc8d4b 495static u32 il4965_rate_n_flags_from_tbl(struct il_priv *il,
e2ebc833 496 struct il_scale_tbl_info *tbl,
be663ab6
WYG
497 int index, u8 use_green)
498{
499 u32 rate_n_flags = 0;
500
501 if (is_legacy(tbl->lq_type)) {
d2ddf621 502 rate_n_flags = il_rates[index].plcp;
e2ebc833 503 if (index >= IL_FIRST_CCK_RATE && index <= IL_LAST_CCK_RATE)
be663ab6
WYG
504 rate_n_flags |= RATE_MCS_CCK_MSK;
505
506 } else if (is_Ht(tbl->lq_type)) {
e2ebc833 507 if (index > IL_LAST_OFDM_RATE) {
46bc8d4b 508 IL_ERR(il, "Invalid HT rate index %d\n", index);
e2ebc833 509 index = IL_LAST_OFDM_RATE;
be663ab6
WYG
510 }
511 rate_n_flags = RATE_MCS_HT_MSK;
512
513 if (is_siso(tbl->lq_type))
d2ddf621 514 rate_n_flags |= il_rates[index].plcp_siso;
be663ab6 515 else
d2ddf621 516 rate_n_flags |= il_rates[index].plcp_mimo2;
be663ab6 517 } else {
46bc8d4b 518 IL_ERR(il, "Invalid tbl->lq_type %d\n", tbl->lq_type);
be663ab6
WYG
519 }
520
521 rate_n_flags |= ((tbl->ant_type << RATE_MCS_ANT_POS) &
522 RATE_MCS_ANT_ABC_MSK);
523
524 if (is_Ht(tbl->lq_type)) {
525 if (tbl->is_ht40) {
526 if (tbl->is_dup)
527 rate_n_flags |= RATE_MCS_DUP_MSK;
528 else
529 rate_n_flags |= RATE_MCS_HT40_MSK;
530 }
531 if (tbl->is_SGI)
532 rate_n_flags |= RATE_MCS_SGI_MSK;
533
534 if (use_green) {
535 rate_n_flags |= RATE_MCS_GF_MSK;
536 if (is_siso(tbl->lq_type) && tbl->is_SGI) {
537 rate_n_flags &= ~RATE_MCS_SGI_MSK;
46bc8d4b 538 IL_ERR(il, "GF was set with SGI:SISO\n");
be663ab6
WYG
539 }
540 }
541 }
542 return rate_n_flags;
543}
544
545/*
546 * Interpret uCode API's rate_n_flags format,
547 * fill "search" or "active" tx mode table.
548 */
e2ebc833 549static int il4965_rs_get_tbl_info_from_mcs(const u32 rate_n_flags,
be663ab6 550 enum ieee80211_band band,
e2ebc833 551 struct il_scale_tbl_info *tbl,
be663ab6
WYG
552 int *rate_idx)
553{
554 u32 ant_msk = (rate_n_flags & RATE_MCS_ANT_ABC_MSK);
e2ebc833 555 u8 il4965_num_of_ant = il4965_get_il4965_num_of_ant_from_rate(rate_n_flags);
be663ab6
WYG
556 u8 mcs;
557
e2ebc833
SG
558 memset(tbl, 0, sizeof(struct il_scale_tbl_info));
559 *rate_idx = il4965_hwrate_to_plcp_idx(rate_n_flags);
be663ab6 560
e2ebc833 561 if (*rate_idx == IL_RATE_INVALID) {
be663ab6
WYG
562 *rate_idx = -1;
563 return -EINVAL;
564 }
565 tbl->is_SGI = 0; /* default legacy setup */
566 tbl->is_ht40 = 0;
567 tbl->is_dup = 0;
568 tbl->ant_type = (ant_msk >> RATE_MCS_ANT_POS);
569 tbl->lq_type = LQ_NONE;
e2ebc833 570 tbl->max_search = IL_MAX_SEARCH;
be663ab6
WYG
571
572 /* legacy rate format */
573 if (!(rate_n_flags & RATE_MCS_HT_MSK)) {
e2ebc833 574 if (il4965_num_of_ant == 1) {
be663ab6
WYG
575 if (band == IEEE80211_BAND_5GHZ)
576 tbl->lq_type = LQ_A;
577 else
578 tbl->lq_type = LQ_G;
579 }
580 /* HT rate format */
581 } else {
582 if (rate_n_flags & RATE_MCS_SGI_MSK)
583 tbl->is_SGI = 1;
584
585 if ((rate_n_flags & RATE_MCS_HT40_MSK) ||
586 (rate_n_flags & RATE_MCS_DUP_MSK))
587 tbl->is_ht40 = 1;
588
589 if (rate_n_flags & RATE_MCS_DUP_MSK)
590 tbl->is_dup = 1;
591
e2ebc833 592 mcs = il4965_rs_extract_rate(rate_n_flags);
be663ab6
WYG
593
594 /* SISO */
e2ebc833
SG
595 if (mcs <= IL_RATE_SISO_60M_PLCP) {
596 if (il4965_num_of_ant == 1)
be663ab6
WYG
597 tbl->lq_type = LQ_SISO; /*else NONE*/
598 /* MIMO2 */
599 } else {
e2ebc833 600 if (il4965_num_of_ant == 2)
be663ab6
WYG
601 tbl->lq_type = LQ_MIMO2;
602 }
603 }
604 return 0;
605}
606
607/* switch to another antenna/antennas and return 1 */
608/* if no other valid antenna found, return 0 */
e2ebc833
SG
609static int il4965_rs_toggle_antenna(u32 valid_ant, u32 *rate_n_flags,
610 struct il_scale_tbl_info *tbl)
be663ab6
WYG
611{
612 u8 new_ant_type;
613
614 if (!tbl->ant_type || tbl->ant_type > ANT_ABC)
615 return 0;
616
e2ebc833 617 if (!il4965_rs_is_valid_ant(valid_ant, tbl->ant_type))
be663ab6
WYG
618 return 0;
619
620 new_ant_type = ant_toggle_lookup[tbl->ant_type];
621
622 while ((new_ant_type != tbl->ant_type) &&
e2ebc833 623 !il4965_rs_is_valid_ant(valid_ant, new_ant_type))
be663ab6
WYG
624 new_ant_type = ant_toggle_lookup[new_ant_type];
625
626 if (new_ant_type == tbl->ant_type)
627 return 0;
628
629 tbl->ant_type = new_ant_type;
630 *rate_n_flags &= ~RATE_MCS_ANT_ABC_MSK;
631 *rate_n_flags |= new_ant_type << RATE_MCS_ANT_POS;
632 return 1;
633}
634
635/**
636 * Green-field mode is valid if the station supports it and
637 * there are no non-GF stations present in the BSS.
638 */
e2ebc833 639static bool il4965_rs_use_green(struct ieee80211_sta *sta)
be663ab6 640{
e2ebc833
SG
641 struct il_station_priv *sta_priv = (void *)sta->drv_priv;
642 struct il_rxon_context *ctx = sta_priv->common.ctx;
be663ab6
WYG
643
644 return (sta->ht_cap.cap & IEEE80211_HT_CAP_GRN_FLD) &&
645 !(ctx->ht.non_gf_sta_present);
646}
647
648/**
e2ebc833 649 * il4965_rs_get_supported_rates - get the available rates
be663ab6
WYG
650 *
651 * if management frame or broadcast frame only return
652 * basic available rates.
653 *
654 */
e2ebc833 655static u16 il4965_rs_get_supported_rates(struct il_lq_sta *lq_sta,
be663ab6 656 struct ieee80211_hdr *hdr,
e2ebc833 657 enum il_table_type rate_type)
be663ab6
WYG
658{
659 if (is_legacy(rate_type)) {
660 return lq_sta->active_legacy_rate;
661 } else {
662 if (is_siso(rate_type))
663 return lq_sta->active_siso_rate;
664 else
665 return lq_sta->active_mimo2_rate;
666 }
667}
668
669static u16
46bc8d4b 670il4965_rs_get_adjacent_rate(struct il_priv *il, u8 index, u16 rate_mask,
be663ab6
WYG
671 int rate_type)
672{
e2ebc833
SG
673 u8 high = IL_RATE_INVALID;
674 u8 low = IL_RATE_INVALID;
be663ab6
WYG
675
676 /* 802.11A or ht walks to the next literal adjacent rate in
677 * the rate table */
678 if (is_a_band(rate_type) || !is_legacy(rate_type)) {
679 int i;
680 u32 mask;
681
682 /* Find the previous rate that is in the rate mask */
683 i = index - 1;
684 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
685 if (rate_mask & mask) {
686 low = i;
687 break;
688 }
689 }
690
691 /* Find the next rate that is in the rate mask */
692 i = index + 1;
e2ebc833 693 for (mask = (1 << i); i < IL_RATE_COUNT; i++, mask <<= 1) {
be663ab6
WYG
694 if (rate_mask & mask) {
695 high = i;
696 break;
697 }
698 }
699
700 return (high << 8) | low;
701 }
702
703 low = index;
e2ebc833 704 while (low != IL_RATE_INVALID) {
d2ddf621 705 low = il_rates[low].prev_rs;
e2ebc833 706 if (low == IL_RATE_INVALID)
be663ab6
WYG
707 break;
708 if (rate_mask & (1 << low))
709 break;
46bc8d4b 710 IL_DEBUG_RATE(il, "Skipping masked lower rate: %d\n", low);
be663ab6
WYG
711 }
712
713 high = index;
e2ebc833 714 while (high != IL_RATE_INVALID) {
d2ddf621 715 high = il_rates[high].next_rs;
e2ebc833 716 if (high == IL_RATE_INVALID)
be663ab6
WYG
717 break;
718 if (rate_mask & (1 << high))
719 break;
46bc8d4b 720 IL_DEBUG_RATE(il, "Skipping masked higher rate: %d\n", high);
be663ab6
WYG
721 }
722
723 return (high << 8) | low;
724}
725
e2ebc833
SG
726static u32 il4965_rs_get_lower_rate(struct il_lq_sta *lq_sta,
727 struct il_scale_tbl_info *tbl,
be663ab6
WYG
728 u8 scale_index, u8 ht_possible)
729{
730 s32 low;
731 u16 rate_mask;
732 u16 high_low;
733 u8 switch_to_legacy = 0;
734 u8 is_green = lq_sta->is_green;
46bc8d4b 735 struct il_priv *il = lq_sta->drv;
be663ab6
WYG
736
737 /* check if we need to switch from HT to legacy rates.
738 * assumption is that mandatory rates (1Mbps or 6Mbps)
739 * are always supported (spec demand) */
740 if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_index)) {
741 switch_to_legacy = 1;
742 scale_index = rs_ht_to_legacy[scale_index];
743 if (lq_sta->band == IEEE80211_BAND_5GHZ)
744 tbl->lq_type = LQ_A;
745 else
746 tbl->lq_type = LQ_G;
747
e2ebc833 748 if (il4965_num_of_ant(tbl->ant_type) > 1)
be663ab6 749 tbl->ant_type =
46bc8d4b 750 il4965_first_antenna(il->hw_params.valid_tx_ant);
be663ab6
WYG
751
752 tbl->is_ht40 = 0;
753 tbl->is_SGI = 0;
e2ebc833 754 tbl->max_search = IL_MAX_SEARCH;
be663ab6
WYG
755 }
756
e2ebc833 757 rate_mask = il4965_rs_get_supported_rates(lq_sta, NULL, tbl->lq_type);
be663ab6
WYG
758
759 /* Mask with station rate restriction */
760 if (is_legacy(tbl->lq_type)) {
761 /* supp_rates has no CCK bits in A mode */
762 if (lq_sta->band == IEEE80211_BAND_5GHZ)
763 rate_mask = (u16)(rate_mask &
e2ebc833 764 (lq_sta->supp_rates << IL_FIRST_OFDM_RATE));
be663ab6
WYG
765 else
766 rate_mask = (u16)(rate_mask & lq_sta->supp_rates);
767 }
768
769 /* If we switched from HT to legacy, check current rate */
770 if (switch_to_legacy && (rate_mask & (1 << scale_index))) {
771 low = scale_index;
772 goto out;
773 }
774
e2ebc833 775 high_low = il4965_rs_get_adjacent_rate(lq_sta->drv,
be663ab6
WYG
776 scale_index, rate_mask,
777 tbl->lq_type);
778 low = high_low & 0xff;
779
e2ebc833 780 if (low == IL_RATE_INVALID)
be663ab6
WYG
781 low = scale_index;
782
783out:
e2ebc833 784 return il4965_rate_n_flags_from_tbl(lq_sta->drv, tbl, low, is_green);
be663ab6
WYG
785}
786
787/*
788 * Simple function to compare two rate scale table types
789 */
e2ebc833
SG
790static bool il4965_table_type_matches(struct il_scale_tbl_info *a,
791 struct il_scale_tbl_info *b)
be663ab6
WYG
792{
793 return (a->lq_type == b->lq_type) && (a->ant_type == b->ant_type) &&
794 (a->is_SGI == b->is_SGI);
795}
796
797/*
798 * mac80211 sends us Tx status
799 */
800static void
46bc8d4b
SG
801il4965_rs_tx_status(void *il_r, struct ieee80211_supported_band *sband,
802 struct ieee80211_sta *sta, void *il_sta,
be663ab6
WYG
803 struct sk_buff *skb)
804{
805 int legacy_success;
806 int retries;
807 int rs_index, mac_index, i;
46bc8d4b 808 struct il_lq_sta *lq_sta = il_sta;
e2ebc833 809 struct il_link_quality_cmd *table;
be663ab6 810 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
46bc8d4b 811 struct il_priv *il = (struct il_priv *)il_r;
be663ab6
WYG
812 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
813 enum mac80211_rate_control_flags mac_flags;
814 u32 tx_rate;
e2ebc833
SG
815 struct il_scale_tbl_info tbl_type;
816 struct il_scale_tbl_info *curr_tbl, *other_tbl, *tmp_tbl;
817 struct il_station_priv *sta_priv = (void *)sta->drv_priv;
818 struct il_rxon_context *ctx = sta_priv->common.ctx;
be663ab6 819
2d27c5db 820 IL_DEBUG_RATE(il,
be663ab6
WYG
821 "get frame ack response, update rate scale window\n");
822
823 /* Treat uninitialized rate scaling data same as non-existing. */
824 if (!lq_sta) {
46bc8d4b 825 IL_DEBUG_RATE(il, "Station rate scaling not created yet.\n");
be663ab6
WYG
826 return;
827 } else if (!lq_sta->drv) {
46bc8d4b 828 IL_DEBUG_RATE(il, "Rate scaling not initialized yet.\n");
be663ab6
WYG
829 return;
830 }
831
832 if (!ieee80211_is_data(hdr->frame_control) ||
833 info->flags & IEEE80211_TX_CTL_NO_ACK)
834 return;
835
836 /* This packet was aggregated but doesn't carry status info */
837 if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
838 !(info->flags & IEEE80211_TX_STAT_AMPDU))
839 return;
840
841 /*
842 * Ignore this Tx frame response if its initial rate doesn't match
843 * that of latest Link Quality command. There may be stragglers
844 * from a previous Link Quality command, but we're no longer interested
845 * in those; they're either from the "active" mode while we're trying
846 * to check "search" mode, or a prior "search" mode after we've moved
847 * to a new "search" mode (which might become the new "active" mode).
848 */
849 table = &lq_sta->lq;
850 tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
e2ebc833 851 il4965_rs_get_tbl_info_from_mcs(tx_rate,
46bc8d4b
SG
852 il->band, &tbl_type, &rs_index);
853 if (il->band == IEEE80211_BAND_5GHZ)
e2ebc833 854 rs_index -= IL_FIRST_OFDM_RATE;
be663ab6
WYG
855 mac_flags = info->status.rates[0].flags;
856 mac_index = info->status.rates[0].idx;
857 /* For HT packets, map MCS to PLCP */
858 if (mac_flags & IEEE80211_TX_RC_MCS) {
859 mac_index &= RATE_MCS_CODE_MSK; /* Remove # of streams */
e2ebc833 860 if (mac_index >= (IL_RATE_9M_INDEX - IL_FIRST_OFDM_RATE))
be663ab6
WYG
861 mac_index++;
862 /*
863 * mac80211 HT index is always zero-indexed; we need to move
864 * HT OFDM rates after CCK rates in 2.4 GHz band
865 */
46bc8d4b 866 if (il->band == IEEE80211_BAND_2GHZ)
e2ebc833 867 mac_index += IL_FIRST_OFDM_RATE;
be663ab6
WYG
868 }
869 /* Here we actually compare this rate to the latest LQ command */
870 if ((mac_index < 0) ||
871 (tbl_type.is_SGI !=
872 !!(mac_flags & IEEE80211_TX_RC_SHORT_GI)) ||
873 (tbl_type.is_ht40 !=
874 !!(mac_flags & IEEE80211_TX_RC_40_MHZ_WIDTH)) ||
875 (tbl_type.is_dup !=
876 !!(mac_flags & IEEE80211_TX_RC_DUP_DATA)) ||
877 (tbl_type.ant_type != info->antenna_sel_tx) ||
878 (!!(tx_rate & RATE_MCS_HT_MSK) !=
879 !!(mac_flags & IEEE80211_TX_RC_MCS)) ||
880 (!!(tx_rate & RATE_MCS_GF_MSK) !=
881 !!(mac_flags & IEEE80211_TX_RC_GREEN_FIELD)) ||
882 (rs_index != mac_index)) {
46bc8d4b 883 IL_DEBUG_RATE(il,
be663ab6
WYG
884 "initial rate %d does not match %d (0x%x)\n",
885 mac_index, rs_index, tx_rate);
886 /*
887 * Since rates mis-match, the last LQ command may have failed.
e2ebc833 888 * After IL_MISSED_RATE_MAX mis-matches, resync the uCode with
be663ab6
WYG
889 * ... driver.
890 */
891 lq_sta->missed_rate_counter++;
e2ebc833 892 if (lq_sta->missed_rate_counter > IL_MISSED_RATE_MAX) {
be663ab6 893 lq_sta->missed_rate_counter = 0;
46bc8d4b 894 il_send_lq_cmd(il, ctx, &lq_sta->lq,
be663ab6
WYG
895 CMD_ASYNC, false);
896 }
897 /* Regardless, ignore this status info for outdated rate */
898 return;
899 } else
900 /* Rate did match, so reset the missed_rate_counter */
901 lq_sta->missed_rate_counter = 0;
902
903 /* Figure out if rate scale algorithm is in active or search table */
e2ebc833 904 if (il4965_table_type_matches(&tbl_type,
be663ab6
WYG
905 &(lq_sta->lq_info[lq_sta->active_tbl]))) {
906 curr_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
907 other_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
e2ebc833 908 } else if (il4965_table_type_matches(&tbl_type,
be663ab6
WYG
909 &lq_sta->lq_info[1 - lq_sta->active_tbl])) {
910 curr_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
911 other_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
912 } else {
46bc8d4b 913 IL_DEBUG_RATE(il,
be663ab6
WYG
914 "Neither active nor search matches tx rate\n");
915 tmp_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
46bc8d4b 916 IL_DEBUG_RATE(il, "active- lq:%x, ant:%x, SGI:%d\n",
be663ab6
WYG
917 tmp_tbl->lq_type, tmp_tbl->ant_type, tmp_tbl->is_SGI);
918 tmp_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
46bc8d4b 919 IL_DEBUG_RATE(il, "search- lq:%x, ant:%x, SGI:%d\n",
be663ab6 920 tmp_tbl->lq_type, tmp_tbl->ant_type, tmp_tbl->is_SGI);
46bc8d4b 921 IL_DEBUG_RATE(il, "actual- lq:%x, ant:%x, SGI:%d\n",
be663ab6
WYG
922 tbl_type.lq_type, tbl_type.ant_type, tbl_type.is_SGI);
923 /*
924 * no matching table found, let's by-pass the data collection
925 * and continue to perform rate scale to find the rate table
926 */
e2ebc833 927 il4965_rs_stay_in_table(lq_sta, true);
be663ab6
WYG
928 goto done;
929 }
930
931 /*
932 * Updating the frame history depends on whether packets were
933 * aggregated.
934 *
935 * For aggregation, all packets were transmitted at the same rate, the
936 * first index into rate scale table.
937 */
938 if (info->flags & IEEE80211_TX_STAT_AMPDU) {
939 tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
46bc8d4b 940 il4965_rs_get_tbl_info_from_mcs(tx_rate, il->band, &tbl_type,
be663ab6 941 &rs_index);
e2ebc833 942 il4965_rs_collect_tx_data(curr_tbl, rs_index,
be663ab6
WYG
943 info->status.ampdu_len,
944 info->status.ampdu_ack_len);
945
946 /* Update success/fail counts if not searching for new mode */
947 if (lq_sta->stay_in_tbl) {
948 lq_sta->total_success += info->status.ampdu_ack_len;
949 lq_sta->total_failed += (info->status.ampdu_len -
950 info->status.ampdu_ack_len);
951 }
952 } else {
953 /*
954 * For legacy, update frame history with for each Tx retry.
955 */
956 retries = info->status.rates[0].count - 1;
957 /* HW doesn't send more than 15 retries */
958 retries = min(retries, 15);
959
960 /* The last transmission may have been successful */
961 legacy_success = !!(info->flags & IEEE80211_TX_STAT_ACK);
962 /* Collect data for each rate used during failed TX attempts */
963 for (i = 0; i <= retries; ++i) {
964 tx_rate = le32_to_cpu(table->rs_table[i].rate_n_flags);
46bc8d4b 965 il4965_rs_get_tbl_info_from_mcs(tx_rate, il->band,
be663ab6
WYG
966 &tbl_type, &rs_index);
967 /*
968 * Only collect stats if retried rate is in the same RS
969 * table as active/search.
970 */
e2ebc833 971 if (il4965_table_type_matches(&tbl_type, curr_tbl))
be663ab6 972 tmp_tbl = curr_tbl;
e2ebc833 973 else if (il4965_table_type_matches(&tbl_type,
be663ab6
WYG
974 other_tbl))
975 tmp_tbl = other_tbl;
976 else
977 continue;
e2ebc833 978 il4965_rs_collect_tx_data(tmp_tbl, rs_index, 1,
be663ab6
WYG
979 i < retries ? 0 : legacy_success);
980 }
981
982 /* Update success/fail counts if not searching for new mode */
983 if (lq_sta->stay_in_tbl) {
984 lq_sta->total_success += legacy_success;
985 lq_sta->total_failed += retries + (1 - legacy_success);
986 }
987 }
988 /* The last TX rate is cached in lq_sta; it's set in if/else above */
989 lq_sta->last_rate_n_flags = tx_rate;
990done:
991 /* See if there's a better rate or modulation mode to try. */
992 if (sta && sta->supp_rates[sband->band])
46bc8d4b 993 il4965_rs_rate_scale_perform(il, skb, sta, lq_sta);
be663ab6
WYG
994}
995
996/*
997 * Begin a period of staying with a selected modulation mode.
998 * Set "stay_in_tbl" flag to prevent any mode switches.
999 * Set frame tx success limits according to legacy vs. high-throughput,
1000 * and reset overall (spanning all rates) tx success history statistics.
1001 * These control how long we stay using same modulation mode before
1002 * searching for a new mode.
1003 */
46bc8d4b 1004static void il4965_rs_set_stay_in_table(struct il_priv *il, u8 is_legacy,
e2ebc833 1005 struct il_lq_sta *lq_sta)
be663ab6 1006{
46bc8d4b 1007 IL_DEBUG_RATE(il, "we are staying in the same table\n");
be663ab6
WYG
1008 lq_sta->stay_in_tbl = 1; /* only place this gets set */
1009 if (is_legacy) {
e2ebc833
SG
1010 lq_sta->table_count_limit = IL_LEGACY_TABLE_COUNT;
1011 lq_sta->max_failure_limit = IL_LEGACY_FAILURE_LIMIT;
1012 lq_sta->max_success_limit = IL_LEGACY_SUCCESS_LIMIT;
be663ab6 1013 } else {
e2ebc833
SG
1014 lq_sta->table_count_limit = IL_NONE_LEGACY_TABLE_COUNT;
1015 lq_sta->max_failure_limit = IL_NONE_LEGACY_FAILURE_LIMIT;
1016 lq_sta->max_success_limit = IL_NONE_LEGACY_SUCCESS_LIMIT;
be663ab6
WYG
1017 }
1018 lq_sta->table_count = 0;
1019 lq_sta->total_failed = 0;
1020 lq_sta->total_success = 0;
1021 lq_sta->flush_timer = jiffies;
1022 lq_sta->action_counter = 0;
1023}
1024
1025/*
1026 * Find correct throughput table for given mode of modulation
1027 */
e2ebc833
SG
1028static void il4965_rs_set_expected_tpt_table(struct il_lq_sta *lq_sta,
1029 struct il_scale_tbl_info *tbl)
be663ab6
WYG
1030{
1031 /* Used to choose among HT tables */
e2ebc833 1032 s32 (*ht_tbl_pointer)[IL_RATE_COUNT];
be663ab6
WYG
1033
1034 /* Check for invalid LQ type */
1035 if (WARN_ON_ONCE(!is_legacy(tbl->lq_type) && !is_Ht(tbl->lq_type))) {
1036 tbl->expected_tpt = expected_tpt_legacy;
1037 return;
1038 }
1039
1040 /* Legacy rates have only one table */
1041 if (is_legacy(tbl->lq_type)) {
1042 tbl->expected_tpt = expected_tpt_legacy;
1043 return;
1044 }
1045
1046 /* Choose among many HT tables depending on number of streams
1047 * (SISO/MIMO2), channel width (20/40), SGI, and aggregation
1048 * status */
1049 if (is_siso(tbl->lq_type) && (!tbl->is_ht40 || lq_sta->is_dup))
1050 ht_tbl_pointer = expected_tpt_siso20MHz;
1051 else if (is_siso(tbl->lq_type))
1052 ht_tbl_pointer = expected_tpt_siso40MHz;
1053 else if (is_mimo2(tbl->lq_type) && (!tbl->is_ht40 || lq_sta->is_dup))
1054 ht_tbl_pointer = expected_tpt_mimo2_20MHz;
1055 else /* if (is_mimo2(tbl->lq_type)) <-- must be true */
1056 ht_tbl_pointer = expected_tpt_mimo2_40MHz;
1057
1058 if (!tbl->is_SGI && !lq_sta->is_agg) /* Normal */
1059 tbl->expected_tpt = ht_tbl_pointer[0];
1060 else if (tbl->is_SGI && !lq_sta->is_agg) /* SGI */
1061 tbl->expected_tpt = ht_tbl_pointer[1];
1062 else if (!tbl->is_SGI && lq_sta->is_agg) /* AGG */
1063 tbl->expected_tpt = ht_tbl_pointer[2];
1064 else /* AGG+SGI */
1065 tbl->expected_tpt = ht_tbl_pointer[3];
1066}
1067
1068/*
1069 * Find starting rate for new "search" high-throughput mode of modulation.
1070 * Goal is to find lowest expected rate (under perfect conditions) that is
1071 * above the current measured throughput of "active" mode, to give new mode
1072 * a fair chance to prove itself without too many challenges.
1073 *
1074 * This gets called when transitioning to more aggressive modulation
1075 * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
1076 * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
1077 * to decrease to match "active" throughput. When moving from MIMO to SISO,
1078 * bit rate will typically need to increase, but not if performance was bad.
1079 */
46bc8d4b 1080static s32 il4965_rs_get_best_rate(struct il_priv *il,
e2ebc833
SG
1081 struct il_lq_sta *lq_sta,
1082 struct il_scale_tbl_info *tbl, /* "search" */
be663ab6
WYG
1083 u16 rate_mask, s8 index)
1084{
1085 /* "active" values */
e2ebc833 1086 struct il_scale_tbl_info *active_tbl =
be663ab6
WYG
1087 &(lq_sta->lq_info[lq_sta->active_tbl]);
1088 s32 active_sr = active_tbl->win[index].success_ratio;
1089 s32 active_tpt = active_tbl->expected_tpt[index];
1090
1091 /* expected "search" throughput */
1092 s32 *tpt_tbl = tbl->expected_tpt;
1093
1094 s32 new_rate, high, low, start_hi;
1095 u16 high_low;
1096 s8 rate = index;
1097
e2ebc833 1098 new_rate = high = low = start_hi = IL_RATE_INVALID;
be663ab6
WYG
1099
1100 for (; ;) {
46bc8d4b 1101 high_low = il4965_rs_get_adjacent_rate(il, rate, rate_mask,
be663ab6
WYG
1102 tbl->lq_type);
1103
1104 low = high_low & 0xff;
1105 high = (high_low >> 8) & 0xff;
1106
1107 /*
1108 * Lower the "search" bit rate, to give new "search" mode
1109 * approximately the same throughput as "active" if:
1110 *
1111 * 1) "Active" mode has been working modestly well (but not
1112 * great), and expected "search" throughput (under perfect
1113 * conditions) at candidate rate is above the actual
1114 * measured "active" throughput (but less than expected
1115 * "active" throughput under perfect conditions).
1116 * OR
1117 * 2) "Active" mode has been working perfectly or very well
1118 * and expected "search" throughput (under perfect
1119 * conditions) at candidate rate is above expected
1120 * "active" throughput (under perfect conditions).
1121 */
1122 if ((((100 * tpt_tbl[rate]) > lq_sta->last_tpt) &&
e2ebc833
SG
1123 ((active_sr > IL_RATE_DECREASE_TH) &&
1124 (active_sr <= IL_RATE_HIGH_TH) &&
be663ab6 1125 (tpt_tbl[rate] <= active_tpt))) ||
e2ebc833 1126 ((active_sr >= IL_RATE_SCALE_SWITCH) &&
be663ab6
WYG
1127 (tpt_tbl[rate] > active_tpt))) {
1128
1129 /* (2nd or later pass)
1130 * If we've already tried to raise the rate, and are
1131 * now trying to lower it, use the higher rate. */
e2ebc833 1132 if (start_hi != IL_RATE_INVALID) {
be663ab6
WYG
1133 new_rate = start_hi;
1134 break;
1135 }
1136
1137 new_rate = rate;
1138
1139 /* Loop again with lower rate */
e2ebc833 1140 if (low != IL_RATE_INVALID)
be663ab6
WYG
1141 rate = low;
1142
1143 /* Lower rate not available, use the original */
1144 else
1145 break;
1146
1147 /* Else try to raise the "search" rate to match "active" */
1148 } else {
1149 /* (2nd or later pass)
1150 * If we've already tried to lower the rate, and are
1151 * now trying to raise it, use the lower rate. */
e2ebc833 1152 if (new_rate != IL_RATE_INVALID)
be663ab6
WYG
1153 break;
1154
1155 /* Loop again with higher rate */
e2ebc833 1156 else if (high != IL_RATE_INVALID) {
be663ab6
WYG
1157 start_hi = high;
1158 rate = high;
1159
1160 /* Higher rate not available, use the original */
1161 } else {
1162 new_rate = rate;
1163 break;
1164 }
1165 }
1166 }
1167
1168 return new_rate;
1169}
1170
1171/*
1172 * Set up search table for MIMO2
1173 */
46bc8d4b 1174static int il4965_rs_switch_to_mimo2(struct il_priv *il,
e2ebc833 1175 struct il_lq_sta *lq_sta,
be663ab6
WYG
1176 struct ieee80211_conf *conf,
1177 struct ieee80211_sta *sta,
e2ebc833 1178 struct il_scale_tbl_info *tbl, int index)
be663ab6
WYG
1179{
1180 u16 rate_mask;
1181 s32 rate;
1182 s8 is_green = lq_sta->is_green;
e2ebc833
SG
1183 struct il_station_priv *sta_priv = (void *)sta->drv_priv;
1184 struct il_rxon_context *ctx = sta_priv->common.ctx;
be663ab6
WYG
1185
1186 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
1187 return -1;
1188
1189 if (((sta->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 2)
1190 == WLAN_HT_CAP_SM_PS_STATIC)
1191 return -1;
1192
1193 /* Need both Tx chains/antennas to support MIMO */
46bc8d4b 1194 if (il->hw_params.tx_chains_num < 2)
be663ab6
WYG
1195 return -1;
1196
46bc8d4b 1197 IL_DEBUG_RATE(il, "LQ: try to switch to MIMO2\n");
be663ab6
WYG
1198
1199 tbl->lq_type = LQ_MIMO2;
1200 tbl->is_dup = lq_sta->is_dup;
1201 tbl->action = 0;
e2ebc833 1202 tbl->max_search = IL_MAX_SEARCH;
be663ab6
WYG
1203 rate_mask = lq_sta->active_mimo2_rate;
1204
46bc8d4b 1205 if (il_is_ht40_tx_allowed(il, ctx, &sta->ht_cap))
be663ab6
WYG
1206 tbl->is_ht40 = 1;
1207 else
1208 tbl->is_ht40 = 0;
1209
e2ebc833 1210 il4965_rs_set_expected_tpt_table(lq_sta, tbl);
be663ab6 1211
46bc8d4b 1212 rate = il4965_rs_get_best_rate(il, lq_sta, tbl, rate_mask, index);
be663ab6 1213
46bc8d4b 1214 IL_DEBUG_RATE(il, "LQ: MIMO2 best rate %d mask %X\n",
be663ab6 1215 rate, rate_mask);
e2ebc833 1216 if ((rate == IL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
46bc8d4b 1217 IL_DEBUG_RATE(il,
be663ab6
WYG
1218 "Can't switch with index %d rate mask %x\n",
1219 rate, rate_mask);
1220 return -1;
1221 }
46bc8d4b 1222 tbl->current_rate = il4965_rate_n_flags_from_tbl(il,
be663ab6
WYG
1223 tbl, rate, is_green);
1224
46bc8d4b 1225 IL_DEBUG_RATE(il, "LQ: Switch to new mcs %X index is green %X\n",
be663ab6
WYG
1226 tbl->current_rate, is_green);
1227 return 0;
1228}
1229
1230/*
1231 * Set up search table for SISO
1232 */
46bc8d4b 1233static int il4965_rs_switch_to_siso(struct il_priv *il,
e2ebc833 1234 struct il_lq_sta *lq_sta,
be663ab6
WYG
1235 struct ieee80211_conf *conf,
1236 struct ieee80211_sta *sta,
e2ebc833 1237 struct il_scale_tbl_info *tbl, int index)
be663ab6
WYG
1238{
1239 u16 rate_mask;
1240 u8 is_green = lq_sta->is_green;
1241 s32 rate;
e2ebc833
SG
1242 struct il_station_priv *sta_priv = (void *)sta->drv_priv;
1243 struct il_rxon_context *ctx = sta_priv->common.ctx;
be663ab6
WYG
1244
1245 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
1246 return -1;
1247
46bc8d4b 1248 IL_DEBUG_RATE(il, "LQ: try to switch to SISO\n");
be663ab6
WYG
1249
1250 tbl->is_dup = lq_sta->is_dup;
1251 tbl->lq_type = LQ_SISO;
1252 tbl->action = 0;
e2ebc833 1253 tbl->max_search = IL_MAX_SEARCH;
be663ab6
WYG
1254 rate_mask = lq_sta->active_siso_rate;
1255
46bc8d4b 1256 if (il_is_ht40_tx_allowed(il, ctx, &sta->ht_cap))
be663ab6
WYG
1257 tbl->is_ht40 = 1;
1258 else
1259 tbl->is_ht40 = 0;
1260
1261 if (is_green)
1262 tbl->is_SGI = 0; /*11n spec: no SGI in SISO+Greenfield*/
1263
e2ebc833 1264 il4965_rs_set_expected_tpt_table(lq_sta, tbl);
46bc8d4b 1265 rate = il4965_rs_get_best_rate(il, lq_sta, tbl, rate_mask, index);
be663ab6 1266
46bc8d4b 1267 IL_DEBUG_RATE(il, "LQ: get best rate %d mask %X\n", rate, rate_mask);
e2ebc833 1268 if ((rate == IL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
46bc8d4b 1269 IL_DEBUG_RATE(il,
be663ab6
WYG
1270 "can not switch with index %d rate mask %x\n",
1271 rate, rate_mask);
1272 return -1;
1273 }
46bc8d4b 1274 tbl->current_rate = il4965_rate_n_flags_from_tbl(il,
be663ab6 1275 tbl, rate, is_green);
46bc8d4b 1276 IL_DEBUG_RATE(il, "LQ: Switch to new mcs %X index is green %X\n",
be663ab6
WYG
1277 tbl->current_rate, is_green);
1278 return 0;
1279}
1280
1281/*
1282 * Try to switch to new modulation mode from legacy
1283 */
46bc8d4b 1284static int il4965_rs_move_legacy_other(struct il_priv *il,
e2ebc833 1285 struct il_lq_sta *lq_sta,
be663ab6
WYG
1286 struct ieee80211_conf *conf,
1287 struct ieee80211_sta *sta,
1288 int index)
1289{
e2ebc833
SG
1290 struct il_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1291 struct il_scale_tbl_info *search_tbl =
be663ab6 1292 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
e2ebc833
SG
1293 struct il_rate_scale_data *window = &(tbl->win[index]);
1294 u32 sz = (sizeof(struct il_scale_tbl_info) -
1295 (sizeof(struct il_rate_scale_data) * IL_RATE_COUNT));
be663ab6 1296 u8 start_action;
46bc8d4b
SG
1297 u8 valid_tx_ant = il->hw_params.valid_tx_ant;
1298 u8 tx_chains_num = il->hw_params.tx_chains_num;
be663ab6
WYG
1299 int ret = 0;
1300 u8 update_search_tbl_counter = 0;
1301
e2ebc833 1302 tbl->action = IL_LEGACY_SWITCH_SISO;
be663ab6
WYG
1303
1304 start_action = tbl->action;
1305 for (; ;) {
1306 lq_sta->action_counter++;
1307 switch (tbl->action) {
e2ebc833
SG
1308 case IL_LEGACY_SWITCH_ANTENNA1:
1309 case IL_LEGACY_SWITCH_ANTENNA2:
46bc8d4b 1310 IL_DEBUG_RATE(il, "LQ: Legacy toggle Antenna\n");
be663ab6 1311
e2ebc833 1312 if ((tbl->action == IL_LEGACY_SWITCH_ANTENNA1 &&
be663ab6 1313 tx_chains_num <= 1) ||
e2ebc833 1314 (tbl->action == IL_LEGACY_SWITCH_ANTENNA2 &&
be663ab6
WYG
1315 tx_chains_num <= 2))
1316 break;
1317
1318 /* Don't change antenna if success has been great */
e2ebc833 1319 if (window->success_ratio >= IL_RS_GOOD_RATIO)
be663ab6
WYG
1320 break;
1321
1322 /* Set up search table to try other antenna */
1323 memcpy(search_tbl, tbl, sz);
1324
e2ebc833 1325 if (il4965_rs_toggle_antenna(valid_tx_ant,
be663ab6
WYG
1326 &search_tbl->current_rate, search_tbl)) {
1327 update_search_tbl_counter = 1;
e2ebc833 1328 il4965_rs_set_expected_tpt_table(lq_sta,
be663ab6
WYG
1329 search_tbl);
1330 goto out;
1331 }
1332 break;
e2ebc833 1333 case IL_LEGACY_SWITCH_SISO:
46bc8d4b 1334 IL_DEBUG_RATE(il, "LQ: Legacy switch to SISO\n");
be663ab6
WYG
1335
1336 /* Set up search table to try SISO */
1337 memcpy(search_tbl, tbl, sz);
1338 search_tbl->is_SGI = 0;
46bc8d4b 1339 ret = il4965_rs_switch_to_siso(il, lq_sta, conf, sta,
be663ab6
WYG
1340 search_tbl, index);
1341 if (!ret) {
1342 lq_sta->action_counter = 0;
1343 goto out;
1344 }
1345
1346 break;
e2ebc833
SG
1347 case IL_LEGACY_SWITCH_MIMO2_AB:
1348 case IL_LEGACY_SWITCH_MIMO2_AC:
1349 case IL_LEGACY_SWITCH_MIMO2_BC:
46bc8d4b 1350 IL_DEBUG_RATE(il, "LQ: Legacy switch to MIMO2\n");
be663ab6
WYG
1351
1352 /* Set up search table to try MIMO */
1353 memcpy(search_tbl, tbl, sz);
1354 search_tbl->is_SGI = 0;
1355
e2ebc833 1356 if (tbl->action == IL_LEGACY_SWITCH_MIMO2_AB)
be663ab6 1357 search_tbl->ant_type = ANT_AB;
e2ebc833 1358 else if (tbl->action == IL_LEGACY_SWITCH_MIMO2_AC)
be663ab6
WYG
1359 search_tbl->ant_type = ANT_AC;
1360 else
1361 search_tbl->ant_type = ANT_BC;
1362
e2ebc833 1363 if (!il4965_rs_is_valid_ant(valid_tx_ant,
be663ab6
WYG
1364 search_tbl->ant_type))
1365 break;
1366
46bc8d4b 1367 ret = il4965_rs_switch_to_mimo2(il, lq_sta,
be663ab6
WYG
1368 conf, sta,
1369 search_tbl, index);
1370 if (!ret) {
1371 lq_sta->action_counter = 0;
1372 goto out;
1373 }
1374 break;
1375 }
1376 tbl->action++;
e2ebc833
SG
1377 if (tbl->action > IL_LEGACY_SWITCH_MIMO2_BC)
1378 tbl->action = IL_LEGACY_SWITCH_ANTENNA1;
be663ab6
WYG
1379
1380 if (tbl->action == start_action)
1381 break;
1382
1383 }
1384 search_tbl->lq_type = LQ_NONE;
1385 return 0;
1386
1387out:
1388 lq_sta->search_better_tbl = 1;
1389 tbl->action++;
e2ebc833
SG
1390 if (tbl->action > IL_LEGACY_SWITCH_MIMO2_BC)
1391 tbl->action = IL_LEGACY_SWITCH_ANTENNA1;
be663ab6
WYG
1392 if (update_search_tbl_counter)
1393 search_tbl->action = tbl->action;
1394 return 0;
1395
1396}
1397
1398/*
1399 * Try to switch to new modulation mode from SISO
1400 */
46bc8d4b 1401static int il4965_rs_move_siso_to_other(struct il_priv *il,
e2ebc833 1402 struct il_lq_sta *lq_sta,
be663ab6
WYG
1403 struct ieee80211_conf *conf,
1404 struct ieee80211_sta *sta, int index)
1405{
1406 u8 is_green = lq_sta->is_green;
e2ebc833
SG
1407 struct il_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1408 struct il_scale_tbl_info *search_tbl =
be663ab6 1409 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
e2ebc833 1410 struct il_rate_scale_data *window = &(tbl->win[index]);
be663ab6 1411 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
e2ebc833
SG
1412 u32 sz = (sizeof(struct il_scale_tbl_info) -
1413 (sizeof(struct il_rate_scale_data) * IL_RATE_COUNT));
be663ab6 1414 u8 start_action;
46bc8d4b
SG
1415 u8 valid_tx_ant = il->hw_params.valid_tx_ant;
1416 u8 tx_chains_num = il->hw_params.tx_chains_num;
be663ab6
WYG
1417 u8 update_search_tbl_counter = 0;
1418 int ret;
1419
1420 start_action = tbl->action;
1421
1422 for (;;) {
1423 lq_sta->action_counter++;
1424 switch (tbl->action) {
e2ebc833
SG
1425 case IL_SISO_SWITCH_ANTENNA1:
1426 case IL_SISO_SWITCH_ANTENNA2:
46bc8d4b 1427 IL_DEBUG_RATE(il, "LQ: SISO toggle Antenna\n");
e2ebc833 1428 if ((tbl->action == IL_SISO_SWITCH_ANTENNA1 &&
be663ab6 1429 tx_chains_num <= 1) ||
e2ebc833 1430 (tbl->action == IL_SISO_SWITCH_ANTENNA2 &&
be663ab6
WYG
1431 tx_chains_num <= 2))
1432 break;
1433
e2ebc833 1434 if (window->success_ratio >= IL_RS_GOOD_RATIO)
be663ab6
WYG
1435 break;
1436
1437 memcpy(search_tbl, tbl, sz);
e2ebc833 1438 if (il4965_rs_toggle_antenna(valid_tx_ant,
be663ab6
WYG
1439 &search_tbl->current_rate, search_tbl)) {
1440 update_search_tbl_counter = 1;
1441 goto out;
1442 }
1443 break;
e2ebc833
SG
1444 case IL_SISO_SWITCH_MIMO2_AB:
1445 case IL_SISO_SWITCH_MIMO2_AC:
1446 case IL_SISO_SWITCH_MIMO2_BC:
46bc8d4b 1447 IL_DEBUG_RATE(il, "LQ: SISO switch to MIMO2\n");
be663ab6
WYG
1448 memcpy(search_tbl, tbl, sz);
1449 search_tbl->is_SGI = 0;
1450
e2ebc833 1451 if (tbl->action == IL_SISO_SWITCH_MIMO2_AB)
be663ab6 1452 search_tbl->ant_type = ANT_AB;
e2ebc833 1453 else if (tbl->action == IL_SISO_SWITCH_MIMO2_AC)
be663ab6
WYG
1454 search_tbl->ant_type = ANT_AC;
1455 else
1456 search_tbl->ant_type = ANT_BC;
1457
e2ebc833 1458 if (!il4965_rs_is_valid_ant(valid_tx_ant,
be663ab6
WYG
1459 search_tbl->ant_type))
1460 break;
1461
46bc8d4b 1462 ret = il4965_rs_switch_to_mimo2(il, lq_sta,
be663ab6
WYG
1463 conf, sta,
1464 search_tbl, index);
1465 if (!ret)
1466 goto out;
1467 break;
e2ebc833 1468 case IL_SISO_SWITCH_GI:
be663ab6
WYG
1469 if (!tbl->is_ht40 && !(ht_cap->cap &
1470 IEEE80211_HT_CAP_SGI_20))
1471 break;
1472 if (tbl->is_ht40 && !(ht_cap->cap &
1473 IEEE80211_HT_CAP_SGI_40))
1474 break;
1475
46bc8d4b 1476 IL_DEBUG_RATE(il, "LQ: SISO toggle SGI/NGI\n");
be663ab6
WYG
1477
1478 memcpy(search_tbl, tbl, sz);
1479 if (is_green) {
1480 if (!tbl->is_SGI)
1481 break;
1482 else
46bc8d4b 1483 IL_ERR(il,
be663ab6
WYG
1484 "SGI was set in GF+SISO\n");
1485 }
1486 search_tbl->is_SGI = !tbl->is_SGI;
e2ebc833 1487 il4965_rs_set_expected_tpt_table(lq_sta, search_tbl);
be663ab6
WYG
1488 if (tbl->is_SGI) {
1489 s32 tpt = lq_sta->last_tpt / 100;
1490 if (tpt >= search_tbl->expected_tpt[index])
1491 break;
1492 }
1493 search_tbl->current_rate =
46bc8d4b 1494 il4965_rate_n_flags_from_tbl(il, search_tbl,
be663ab6
WYG
1495 index, is_green);
1496 update_search_tbl_counter = 1;
1497 goto out;
1498 }
1499 tbl->action++;
e2ebc833
SG
1500 if (tbl->action > IL_SISO_SWITCH_GI)
1501 tbl->action = IL_SISO_SWITCH_ANTENNA1;
be663ab6
WYG
1502
1503 if (tbl->action == start_action)
1504 break;
1505 }
1506 search_tbl->lq_type = LQ_NONE;
1507 return 0;
1508
1509 out:
1510 lq_sta->search_better_tbl = 1;
1511 tbl->action++;
e2ebc833
SG
1512 if (tbl->action > IL_SISO_SWITCH_GI)
1513 tbl->action = IL_SISO_SWITCH_ANTENNA1;
be663ab6
WYG
1514 if (update_search_tbl_counter)
1515 search_tbl->action = tbl->action;
1516
1517 return 0;
1518}
1519
1520/*
1521 * Try to switch to new modulation mode from MIMO2
1522 */
46bc8d4b 1523static int il4965_rs_move_mimo2_to_other(struct il_priv *il,
e2ebc833 1524 struct il_lq_sta *lq_sta,
be663ab6
WYG
1525 struct ieee80211_conf *conf,
1526 struct ieee80211_sta *sta, int index)
1527{
1528 s8 is_green = lq_sta->is_green;
e2ebc833
SG
1529 struct il_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1530 struct il_scale_tbl_info *search_tbl =
be663ab6 1531 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
e2ebc833 1532 struct il_rate_scale_data *window = &(tbl->win[index]);
be663ab6 1533 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
e2ebc833
SG
1534 u32 sz = (sizeof(struct il_scale_tbl_info) -
1535 (sizeof(struct il_rate_scale_data) * IL_RATE_COUNT));
be663ab6 1536 u8 start_action;
46bc8d4b
SG
1537 u8 valid_tx_ant = il->hw_params.valid_tx_ant;
1538 u8 tx_chains_num = il->hw_params.tx_chains_num;
be663ab6
WYG
1539 u8 update_search_tbl_counter = 0;
1540 int ret;
1541
1542 start_action = tbl->action;
1543 for (;;) {
1544 lq_sta->action_counter++;
1545 switch (tbl->action) {
e2ebc833
SG
1546 case IL_MIMO2_SWITCH_ANTENNA1:
1547 case IL_MIMO2_SWITCH_ANTENNA2:
46bc8d4b 1548 IL_DEBUG_RATE(il, "LQ: MIMO2 toggle Antennas\n");
be663ab6
WYG
1549
1550 if (tx_chains_num <= 2)
1551 break;
1552
e2ebc833 1553 if (window->success_ratio >= IL_RS_GOOD_RATIO)
be663ab6
WYG
1554 break;
1555
1556 memcpy(search_tbl, tbl, sz);
e2ebc833 1557 if (il4965_rs_toggle_antenna(valid_tx_ant,
be663ab6
WYG
1558 &search_tbl->current_rate, search_tbl)) {
1559 update_search_tbl_counter = 1;
1560 goto out;
1561 }
1562 break;
e2ebc833
SG
1563 case IL_MIMO2_SWITCH_SISO_A:
1564 case IL_MIMO2_SWITCH_SISO_B:
1565 case IL_MIMO2_SWITCH_SISO_C:
46bc8d4b 1566 IL_DEBUG_RATE(il, "LQ: MIMO2 switch to SISO\n");
be663ab6
WYG
1567
1568 /* Set up new search table for SISO */
1569 memcpy(search_tbl, tbl, sz);
1570
e2ebc833 1571 if (tbl->action == IL_MIMO2_SWITCH_SISO_A)
be663ab6 1572 search_tbl->ant_type = ANT_A;
e2ebc833 1573 else if (tbl->action == IL_MIMO2_SWITCH_SISO_B)
be663ab6
WYG
1574 search_tbl->ant_type = ANT_B;
1575 else
1576 search_tbl->ant_type = ANT_C;
1577
e2ebc833 1578 if (!il4965_rs_is_valid_ant(valid_tx_ant,
be663ab6
WYG
1579 search_tbl->ant_type))
1580 break;
1581
46bc8d4b 1582 ret = il4965_rs_switch_to_siso(il, lq_sta,
be663ab6
WYG
1583 conf, sta,
1584 search_tbl, index);
1585 if (!ret)
1586 goto out;
1587
1588 break;
1589
e2ebc833 1590 case IL_MIMO2_SWITCH_GI:
be663ab6
WYG
1591 if (!tbl->is_ht40 && !(ht_cap->cap &
1592 IEEE80211_HT_CAP_SGI_20))
1593 break;
1594 if (tbl->is_ht40 && !(ht_cap->cap &
1595 IEEE80211_HT_CAP_SGI_40))
1596 break;
1597
46bc8d4b 1598 IL_DEBUG_RATE(il, "LQ: MIMO2 toggle SGI/NGI\n");
be663ab6
WYG
1599
1600 /* Set up new search table for MIMO2 */
1601 memcpy(search_tbl, tbl, sz);
1602 search_tbl->is_SGI = !tbl->is_SGI;
e2ebc833 1603 il4965_rs_set_expected_tpt_table(lq_sta, search_tbl);
be663ab6
WYG
1604 /*
1605 * If active table already uses the fastest possible
1606 * modulation (dual stream with short guard interval),
1607 * and it's working well, there's no need to look
1608 * for a better type of modulation!
1609 */
1610 if (tbl->is_SGI) {
1611 s32 tpt = lq_sta->last_tpt / 100;
1612 if (tpt >= search_tbl->expected_tpt[index])
1613 break;
1614 }
1615 search_tbl->current_rate =
46bc8d4b 1616 il4965_rate_n_flags_from_tbl(il, search_tbl,
be663ab6
WYG
1617 index, is_green);
1618 update_search_tbl_counter = 1;
1619 goto out;
1620
1621 }
1622 tbl->action++;
e2ebc833
SG
1623 if (tbl->action > IL_MIMO2_SWITCH_GI)
1624 tbl->action = IL_MIMO2_SWITCH_ANTENNA1;
be663ab6
WYG
1625
1626 if (tbl->action == start_action)
1627 break;
1628 }
1629 search_tbl->lq_type = LQ_NONE;
1630 return 0;
1631 out:
1632 lq_sta->search_better_tbl = 1;
1633 tbl->action++;
e2ebc833
SG
1634 if (tbl->action > IL_MIMO2_SWITCH_GI)
1635 tbl->action = IL_MIMO2_SWITCH_ANTENNA1;
be663ab6
WYG
1636 if (update_search_tbl_counter)
1637 search_tbl->action = tbl->action;
1638
1639 return 0;
1640
1641}
1642
1643/*
1644 * Check whether we should continue using same modulation mode, or
1645 * begin search for a new mode, based on:
1646 * 1) # tx successes or failures while using this mode
1647 * 2) # times calling this function
1648 * 3) elapsed time in this mode (not used, for now)
1649 */
1650static void
e2ebc833 1651il4965_rs_stay_in_table(struct il_lq_sta *lq_sta, bool force_search)
be663ab6 1652{
e2ebc833 1653 struct il_scale_tbl_info *tbl;
be663ab6
WYG
1654 int i;
1655 int active_tbl;
1656 int flush_interval_passed = 0;
46bc8d4b 1657 struct il_priv *il;
be663ab6 1658
46bc8d4b 1659 il = lq_sta->drv;
be663ab6
WYG
1660 active_tbl = lq_sta->active_tbl;
1661
1662 tbl = &(lq_sta->lq_info[active_tbl]);
1663
1664 /* If we've been disallowing search, see if we should now allow it */
1665 if (lq_sta->stay_in_tbl) {
1666
1667 /* Elapsed time using current modulation mode */
1668 if (lq_sta->flush_timer)
1669 flush_interval_passed =
1670 time_after(jiffies,
1671 (unsigned long)(lq_sta->flush_timer +
e2ebc833 1672 IL_RATE_SCALE_FLUSH_INTVL));
be663ab6
WYG
1673
1674 /*
1675 * Check if we should allow search for new modulation mode.
1676 * If many frames have failed or succeeded, or we've used
1677 * this same modulation for a long time, allow search, and
1678 * reset history stats that keep track of whether we should
1679 * allow a new search. Also (below) reset all bitmaps and
1680 * stats in active history.
1681 */
1682 if (force_search ||
1683 (lq_sta->total_failed > lq_sta->max_failure_limit) ||
1684 (lq_sta->total_success > lq_sta->max_success_limit) ||
1685 ((!lq_sta->search_better_tbl) && (lq_sta->flush_timer)
1686 && (flush_interval_passed))) {
46bc8d4b 1687 IL_DEBUG_RATE(il, "LQ: stay is expired %d %d %d\n:",
be663ab6
WYG
1688 lq_sta->total_failed,
1689 lq_sta->total_success,
1690 flush_interval_passed);
1691
1692 /* Allow search for new mode */
1693 lq_sta->stay_in_tbl = 0; /* only place reset */
1694 lq_sta->total_failed = 0;
1695 lq_sta->total_success = 0;
1696 lq_sta->flush_timer = 0;
1697
1698 /*
1699 * Else if we've used this modulation mode enough repetitions
1700 * (regardless of elapsed time or success/failure), reset
1701 * history bitmaps and rate-specific stats for all rates in
1702 * active table.
1703 */
1704 } else {
1705 lq_sta->table_count++;
1706 if (lq_sta->table_count >=
1707 lq_sta->table_count_limit) {
1708 lq_sta->table_count = 0;
1709
46bc8d4b 1710 IL_DEBUG_RATE(il,
be663ab6 1711 "LQ: stay in table clear win\n");
e2ebc833
SG
1712 for (i = 0; i < IL_RATE_COUNT; i++)
1713 il4965_rs_rate_scale_clear_window(
be663ab6
WYG
1714 &(tbl->win[i]));
1715 }
1716 }
1717
1718 /* If transitioning to allow "search", reset all history
1719 * bitmaps and stats in active table (this will become the new
1720 * "search" table). */
1721 if (!lq_sta->stay_in_tbl) {
e2ebc833
SG
1722 for (i = 0; i < IL_RATE_COUNT; i++)
1723 il4965_rs_rate_scale_clear_window(
be663ab6
WYG
1724 &(tbl->win[i]));
1725 }
1726 }
1727}
1728
1729/*
1730 * setup rate table in uCode
1731 * return rate_n_flags as used in the table
1732 */
46bc8d4b 1733static u32 il4965_rs_update_rate_tbl(struct il_priv *il,
e2ebc833
SG
1734 struct il_rxon_context *ctx,
1735 struct il_lq_sta *lq_sta,
1736 struct il_scale_tbl_info *tbl,
be663ab6
WYG
1737 int index, u8 is_green)
1738{
1739 u32 rate;
1740
1741 /* Update uCode's rate table. */
46bc8d4b
SG
1742 rate = il4965_rate_n_flags_from_tbl(il, tbl, index, is_green);
1743 il4965_rs_fill_link_cmd(il, lq_sta, rate);
1744 il_send_lq_cmd(il, ctx, &lq_sta->lq, CMD_ASYNC, false);
be663ab6
WYG
1745
1746 return rate;
1747}
1748
1749/*
1750 * Do rate scaling and search for new modulation mode.
1751 */
46bc8d4b 1752static void il4965_rs_rate_scale_perform(struct il_priv *il,
be663ab6
WYG
1753 struct sk_buff *skb,
1754 struct ieee80211_sta *sta,
e2ebc833 1755 struct il_lq_sta *lq_sta)
be663ab6 1756{
46bc8d4b 1757 struct ieee80211_hw *hw = il->hw;
be663ab6
WYG
1758 struct ieee80211_conf *conf = &hw->conf;
1759 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1760 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
e2ebc833
SG
1761 int low = IL_RATE_INVALID;
1762 int high = IL_RATE_INVALID;
be663ab6
WYG
1763 int index;
1764 int i;
e2ebc833
SG
1765 struct il_rate_scale_data *window = NULL;
1766 int current_tpt = IL_INVALID_VALUE;
1767 int low_tpt = IL_INVALID_VALUE;
1768 int high_tpt = IL_INVALID_VALUE;
be663ab6
WYG
1769 u32 fail_count;
1770 s8 scale_action = 0;
1771 u16 rate_mask;
1772 u8 update_lq = 0;
e2ebc833 1773 struct il_scale_tbl_info *tbl, *tbl1;
be663ab6
WYG
1774 u16 rate_scale_index_msk = 0;
1775 u32 rate;
1776 u8 is_green = 0;
1777 u8 active_tbl = 0;
1778 u8 done_search = 0;
1779 u16 high_low;
1780 s32 sr;
1781 u8 tid = MAX_TID_COUNT;
e2ebc833
SG
1782 struct il_tid_data *tid_data;
1783 struct il_station_priv *sta_priv = (void *)sta->drv_priv;
1784 struct il_rxon_context *ctx = sta_priv->common.ctx;
be663ab6 1785
46bc8d4b 1786 IL_DEBUG_RATE(il, "rate scale calculate new rate for skb\n");
be663ab6
WYG
1787
1788 /* Send management frames and NO_ACK data using lowest rate. */
1789 /* TODO: this could probably be improved.. */
1790 if (!ieee80211_is_data(hdr->frame_control) ||
1791 info->flags & IEEE80211_TX_CTL_NO_ACK)
1792 return;
1793
1794 if (!sta || !lq_sta)
1795 return;
1796
1797 lq_sta->supp_rates = sta->supp_rates[lq_sta->band];
1798
e2ebc833 1799 tid = il4965_rs_tl_add_packet(lq_sta, hdr);
be663ab6 1800 if ((tid != MAX_TID_COUNT) && (lq_sta->tx_agg_tid_en & (1 << tid))) {
46bc8d4b 1801 tid_data = &il->stations[lq_sta->lq.sta_id].tid[tid];
e2ebc833 1802 if (tid_data->agg.state == IL_AGG_OFF)
be663ab6
WYG
1803 lq_sta->is_agg = 0;
1804 else
1805 lq_sta->is_agg = 1;
1806 } else
1807 lq_sta->is_agg = 0;
1808
1809 /*
1810 * Select rate-scale / modulation-mode table to work with in
1811 * the rest of this function: "search" if searching for better
1812 * modulation mode, or "active" if doing rate scaling within a mode.
1813 */
1814 if (!lq_sta->search_better_tbl)
1815 active_tbl = lq_sta->active_tbl;
1816 else
1817 active_tbl = 1 - lq_sta->active_tbl;
1818
1819 tbl = &(lq_sta->lq_info[active_tbl]);
1820 if (is_legacy(tbl->lq_type))
1821 lq_sta->is_green = 0;
1822 else
e2ebc833 1823 lq_sta->is_green = il4965_rs_use_green(sta);
be663ab6
WYG
1824 is_green = lq_sta->is_green;
1825
1826 /* current tx rate */
1827 index = lq_sta->last_txrate_idx;
1828
46bc8d4b 1829 IL_DEBUG_RATE(il, "Rate scale index %d for type %d\n", index,
be663ab6
WYG
1830 tbl->lq_type);
1831
1832 /* rates available for this association, and for modulation mode */
e2ebc833 1833 rate_mask = il4965_rs_get_supported_rates(lq_sta, hdr, tbl->lq_type);
be663ab6 1834
46bc8d4b 1835 IL_DEBUG_RATE(il, "mask 0x%04X\n", rate_mask);
be663ab6
WYG
1836
1837 /* mask with station rate restriction */
1838 if (is_legacy(tbl->lq_type)) {
1839 if (lq_sta->band == IEEE80211_BAND_5GHZ)
1840 /* supp_rates has no CCK bits in A mode */
1841 rate_scale_index_msk = (u16) (rate_mask &
e2ebc833 1842 (lq_sta->supp_rates << IL_FIRST_OFDM_RATE));
be663ab6
WYG
1843 else
1844 rate_scale_index_msk = (u16) (rate_mask &
1845 lq_sta->supp_rates);
1846
1847 } else
1848 rate_scale_index_msk = rate_mask;
1849
1850 if (!rate_scale_index_msk)
1851 rate_scale_index_msk = rate_mask;
1852
1853 if (!((1 << index) & rate_scale_index_msk)) {
46bc8d4b 1854 IL_ERR(il, "Current Rate is not valid\n");
be663ab6
WYG
1855 if (lq_sta->search_better_tbl) {
1856 /* revert to active table if search table is not valid*/
1857 tbl->lq_type = LQ_NONE;
1858 lq_sta->search_better_tbl = 0;
1859 tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1860 /* get "active" rate info */
e2ebc833 1861 index = il4965_hwrate_to_plcp_idx(tbl->current_rate);
46bc8d4b 1862 rate = il4965_rs_update_rate_tbl(il, ctx, lq_sta,
be663ab6
WYG
1863 tbl, index, is_green);
1864 }
1865 return;
1866 }
1867
1868 /* Get expected throughput table and history window for current rate */
1869 if (!tbl->expected_tpt) {
46bc8d4b 1870 IL_ERR(il, "tbl->expected_tpt is NULL\n");
be663ab6
WYG
1871 return;
1872 }
1873
1874 /* force user max rate if set by user */
1875 if ((lq_sta->max_rate_idx != -1) &&
1876 (lq_sta->max_rate_idx < index)) {
1877 index = lq_sta->max_rate_idx;
1878 update_lq = 1;
1879 window = &(tbl->win[index]);
1880 goto lq_update;
1881 }
1882
1883 window = &(tbl->win[index]);
1884
1885 /*
1886 * If there is not enough history to calculate actual average
1887 * throughput, keep analyzing results of more tx frames, without
1888 * changing rate or mode (bypass most of the rest of this function).
1889 * Set up new rate table in uCode only if old rate is not supported
1890 * in current association (use new rate found above).
1891 */
1892 fail_count = window->counter - window->success_counter;
e2ebc833
SG
1893 if ((fail_count < IL_RATE_MIN_FAILURE_TH) &&
1894 (window->success_counter < IL_RATE_MIN_SUCCESS_TH)) {
46bc8d4b 1895 IL_DEBUG_RATE(il, "LQ: still below TH. succ=%d total=%d "
be663ab6
WYG
1896 "for index %d\n",
1897 window->success_counter, window->counter, index);
1898
1899 /* Can't calculate this yet; not enough history */
e2ebc833 1900 window->average_tpt = IL_INVALID_VALUE;
be663ab6
WYG
1901
1902 /* Should we stay with this modulation mode,
1903 * or search for a new one? */
e2ebc833 1904 il4965_rs_stay_in_table(lq_sta, false);
be663ab6
WYG
1905
1906 goto out;
1907 }
1908 /* Else we have enough samples; calculate estimate of
1909 * actual average throughput */
1910 if (window->average_tpt != ((window->success_ratio *
1911 tbl->expected_tpt[index] + 64) / 128)) {
46bc8d4b 1912 IL_ERR(il,
be663ab6
WYG
1913 "expected_tpt should have been calculated by now\n");
1914 window->average_tpt = ((window->success_ratio *
1915 tbl->expected_tpt[index] + 64) / 128);
1916 }
1917
1918 /* If we are searching for better modulation mode, check success. */
1919 if (lq_sta->search_better_tbl) {
1920 /* If good success, continue using the "search" mode;
1921 * no need to send new link quality command, since we're
1922 * continuing to use the setup that we've been trying. */
1923 if (window->average_tpt > lq_sta->last_tpt) {
1924
46bc8d4b 1925 IL_DEBUG_RATE(il, "LQ: SWITCHING TO NEW TABLE "
be663ab6
WYG
1926 "suc=%d cur-tpt=%d old-tpt=%d\n",
1927 window->success_ratio,
1928 window->average_tpt,
1929 lq_sta->last_tpt);
1930
1931 if (!is_legacy(tbl->lq_type))
1932 lq_sta->enable_counter = 1;
1933
1934 /* Swap tables; "search" becomes "active" */
1935 lq_sta->active_tbl = active_tbl;
1936 current_tpt = window->average_tpt;
1937
1938 /* Else poor success; go back to mode in "active" table */
1939 } else {
1940
46bc8d4b 1941 IL_DEBUG_RATE(il, "LQ: GOING BACK TO THE OLD TABLE "
be663ab6
WYG
1942 "suc=%d cur-tpt=%d old-tpt=%d\n",
1943 window->success_ratio,
1944 window->average_tpt,
1945 lq_sta->last_tpt);
1946
1947 /* Nullify "search" table */
1948 tbl->lq_type = LQ_NONE;
1949
1950 /* Revert to "active" table */
1951 active_tbl = lq_sta->active_tbl;
1952 tbl = &(lq_sta->lq_info[active_tbl]);
1953
1954 /* Revert to "active" rate and throughput info */
e2ebc833 1955 index = il4965_hwrate_to_plcp_idx(tbl->current_rate);
be663ab6
WYG
1956 current_tpt = lq_sta->last_tpt;
1957
1958 /* Need to set up a new rate table in uCode */
1959 update_lq = 1;
1960 }
1961
1962 /* Either way, we've made a decision; modulation mode
1963 * search is done, allow rate adjustment next time. */
1964 lq_sta->search_better_tbl = 0;
1965 done_search = 1; /* Don't switch modes below! */
1966 goto lq_update;
1967 }
1968
1969 /* (Else) not in search of better modulation mode, try for better
1970 * starting rate, while staying in this mode. */
46bc8d4b 1971 high_low = il4965_rs_get_adjacent_rate(il, index,
be663ab6
WYG
1972 rate_scale_index_msk,
1973 tbl->lq_type);
1974 low = high_low & 0xff;
1975 high = (high_low >> 8) & 0xff;
1976
1977 /* If user set max rate, dont allow higher than user constrain */
1978 if ((lq_sta->max_rate_idx != -1) &&
1979 (lq_sta->max_rate_idx < high))
e2ebc833 1980 high = IL_RATE_INVALID;
be663ab6
WYG
1981
1982 sr = window->success_ratio;
1983
1984 /* Collect measured throughputs for current and adjacent rates */
1985 current_tpt = window->average_tpt;
e2ebc833 1986 if (low != IL_RATE_INVALID)
be663ab6 1987 low_tpt = tbl->win[low].average_tpt;
e2ebc833 1988 if (high != IL_RATE_INVALID)
be663ab6
WYG
1989 high_tpt = tbl->win[high].average_tpt;
1990
1991 scale_action = 0;
1992
1993 /* Too many failures, decrease rate */
e2ebc833 1994 if ((sr <= IL_RATE_DECREASE_TH) || (current_tpt == 0)) {
46bc8d4b 1995 IL_DEBUG_RATE(il,
be663ab6
WYG
1996 "decrease rate because of low success_ratio\n");
1997 scale_action = -1;
1998
1999 /* No throughput measured yet for adjacent rates; try increase. */
e2ebc833
SG
2000 } else if ((low_tpt == IL_INVALID_VALUE) &&
2001 (high_tpt == IL_INVALID_VALUE)) {
be663ab6 2002
e2ebc833 2003 if (high != IL_RATE_INVALID && sr >= IL_RATE_INCREASE_TH)
be663ab6 2004 scale_action = 1;
e2ebc833 2005 else if (low != IL_RATE_INVALID)
be663ab6
WYG
2006 scale_action = 0;
2007 }
2008
2009 /* Both adjacent throughputs are measured, but neither one has better
2010 * throughput; we're using the best rate, don't change it! */
e2ebc833
SG
2011 else if ((low_tpt != IL_INVALID_VALUE) &&
2012 (high_tpt != IL_INVALID_VALUE) &&
be663ab6
WYG
2013 (low_tpt < current_tpt) &&
2014 (high_tpt < current_tpt))
2015 scale_action = 0;
2016
2017 /* At least one adjacent rate's throughput is measured,
2018 * and may have better performance. */
2019 else {
2020 /* Higher adjacent rate's throughput is measured */
e2ebc833 2021 if (high_tpt != IL_INVALID_VALUE) {
be663ab6
WYG
2022 /* Higher rate has better throughput */
2023 if (high_tpt > current_tpt &&
e2ebc833 2024 sr >= IL_RATE_INCREASE_TH) {
be663ab6
WYG
2025 scale_action = 1;
2026 } else {
2027 scale_action = 0;
2028 }
2029
2030 /* Lower adjacent rate's throughput is measured */
e2ebc833 2031 } else if (low_tpt != IL_INVALID_VALUE) {
be663ab6
WYG
2032 /* Lower rate has better throughput */
2033 if (low_tpt > current_tpt) {
46bc8d4b 2034 IL_DEBUG_RATE(il,
be663ab6
WYG
2035 "decrease rate because of low tpt\n");
2036 scale_action = -1;
e2ebc833 2037 } else if (sr >= IL_RATE_INCREASE_TH) {
be663ab6
WYG
2038 scale_action = 1;
2039 }
2040 }
2041 }
2042
2043 /* Sanity check; asked for decrease, but success rate or throughput
2044 * has been good at old rate. Don't change it. */
e2ebc833
SG
2045 if ((scale_action == -1) && (low != IL_RATE_INVALID) &&
2046 ((sr > IL_RATE_HIGH_TH) ||
be663ab6
WYG
2047 (current_tpt > (100 * tbl->expected_tpt[low]))))
2048 scale_action = 0;
2049
2050 switch (scale_action) {
2051 case -1:
2052 /* Decrease starting rate, update uCode's rate table */
e2ebc833 2053 if (low != IL_RATE_INVALID) {
be663ab6
WYG
2054 update_lq = 1;
2055 index = low;
2056 }
2057
2058 break;
2059 case 1:
2060 /* Increase starting rate, update uCode's rate table */
e2ebc833 2061 if (high != IL_RATE_INVALID) {
be663ab6
WYG
2062 update_lq = 1;
2063 index = high;
2064 }
2065
2066 break;
2067 case 0:
2068 /* No change */
2069 default:
2070 break;
2071 }
2072
46bc8d4b 2073 IL_DEBUG_RATE(il, "choose rate scale index %d action %d low %d "
be663ab6
WYG
2074 "high %d type %d\n",
2075 index, scale_action, low, high, tbl->lq_type);
2076
2077lq_update:
2078 /* Replace uCode's rate table for the destination station. */
2079 if (update_lq)
46bc8d4b 2080 rate = il4965_rs_update_rate_tbl(il, ctx, lq_sta,
be663ab6
WYG
2081 tbl, index, is_green);
2082
2083 /* Should we stay with this modulation mode,
2084 * or search for a new one? */
e2ebc833 2085 il4965_rs_stay_in_table(lq_sta, false);
be663ab6
WYG
2086
2087 /*
2088 * Search for new modulation mode if we're:
2089 * 1) Not changing rates right now
2090 * 2) Not just finishing up a search
2091 * 3) Allowing a new search
2092 */
2093 if (!update_lq && !done_search &&
2094 !lq_sta->stay_in_tbl && window->counter) {
2095 /* Save current throughput to compare with "search" throughput*/
2096 lq_sta->last_tpt = current_tpt;
2097
2098 /* Select a new "search" modulation mode to try.
2099 * If one is found, set up the new "search" table. */
2100 if (is_legacy(tbl->lq_type))
46bc8d4b 2101 il4965_rs_move_legacy_other(il, lq_sta,
be663ab6
WYG
2102 conf, sta, index);
2103 else if (is_siso(tbl->lq_type))
46bc8d4b 2104 il4965_rs_move_siso_to_other(il, lq_sta,
be663ab6
WYG
2105 conf, sta, index);
2106 else /* (is_mimo2(tbl->lq_type)) */
46bc8d4b 2107 il4965_rs_move_mimo2_to_other(il, lq_sta,
be663ab6
WYG
2108 conf, sta, index);
2109
2110 /* If new "search" mode was selected, set up in uCode table */
2111 if (lq_sta->search_better_tbl) {
2112 /* Access the "search" table, clear its history. */
2113 tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
e2ebc833
SG
2114 for (i = 0; i < IL_RATE_COUNT; i++)
2115 il4965_rs_rate_scale_clear_window(
be663ab6
WYG
2116 &(tbl->win[i]));
2117
2118 /* Use new "search" start rate */
e2ebc833 2119 index = il4965_hwrate_to_plcp_idx(tbl->current_rate);
be663ab6 2120
46bc8d4b 2121 IL_DEBUG_RATE(il,
be663ab6
WYG
2122 "Switch current mcs: %X index: %d\n",
2123 tbl->current_rate, index);
46bc8d4b 2124 il4965_rs_fill_link_cmd(il, lq_sta,
be663ab6 2125 tbl->current_rate);
46bc8d4b 2126 il_send_lq_cmd(il, ctx,
be663ab6
WYG
2127 &lq_sta->lq, CMD_ASYNC, false);
2128 } else
2129 done_search = 1;
2130 }
2131
2132 if (done_search && !lq_sta->stay_in_tbl) {
2133 /* If the "active" (non-search) mode was legacy,
2134 * and we've tried switching antennas,
2135 * but we haven't been able to try HT modes (not available),
2136 * stay with best antenna legacy modulation for a while
2137 * before next round of mode comparisons. */
2138 tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
2139 if (is_legacy(tbl1->lq_type) && !conf_is_ht(conf) &&
2140 lq_sta->action_counter > tbl1->max_search) {
46bc8d4b
SG
2141 IL_DEBUG_RATE(il, "LQ: STAY in legacy table\n");
2142 il4965_rs_set_stay_in_table(il, 1, lq_sta);
be663ab6
WYG
2143 }
2144
2145 /* If we're in an HT mode, and all 3 mode switch actions
2146 * have been tried and compared, stay in this best modulation
2147 * mode for a while before next round of mode comparisons. */
2148 if (lq_sta->enable_counter &&
2149 (lq_sta->action_counter >= tbl1->max_search)) {
e2ebc833 2150 if ((lq_sta->last_tpt > IL_AGG_TPT_THREHOLD) &&
be663ab6
WYG
2151 (lq_sta->tx_agg_tid_en & (1 << tid)) &&
2152 (tid != MAX_TID_COUNT)) {
2153 tid_data =
46bc8d4b 2154 &il->stations[lq_sta->lq.sta_id].tid[tid];
e2ebc833 2155 if (tid_data->agg.state == IL_AGG_OFF) {
46bc8d4b 2156 IL_DEBUG_RATE(il,
be663ab6
WYG
2157 "try to aggregate tid %d\n",
2158 tid);
46bc8d4b 2159 il4965_rs_tl_turn_on_agg(il, tid,
be663ab6
WYG
2160 lq_sta, sta);
2161 }
2162 }
46bc8d4b 2163 il4965_rs_set_stay_in_table(il, 0, lq_sta);
be663ab6
WYG
2164 }
2165 }
2166
2167out:
46bc8d4b 2168 tbl->current_rate = il4965_rate_n_flags_from_tbl(il, tbl,
be663ab6
WYG
2169 index, is_green);
2170 i = index;
2171 lq_sta->last_txrate_idx = i;
2172}
2173
2174/**
e2ebc833 2175 * il4965_rs_initialize_lq - Initialize a station's hardware rate table
be663ab6
WYG
2176 *
2177 * The uCode's station table contains a table of fallback rates
2178 * for automatic fallback during transmission.
2179 *
2180 * NOTE: This sets up a default set of values. These will be replaced later
2181 * if the driver's iwl-4965-rs rate scaling algorithm is used, instead of
2182 * rc80211_simple.
2183 *
2184 * NOTE: Run REPLY_ADD_STA command to set up station table entry, before
2185 * calling this function (which runs REPLY_TX_LINK_QUALITY_CMD,
2186 * which requires station table entry to exist).
2187 */
46bc8d4b 2188static void il4965_rs_initialize_lq(struct il_priv *il,
be663ab6
WYG
2189 struct ieee80211_conf *conf,
2190 struct ieee80211_sta *sta,
e2ebc833 2191 struct il_lq_sta *lq_sta)
be663ab6 2192{
e2ebc833 2193 struct il_scale_tbl_info *tbl;
be663ab6
WYG
2194 int rate_idx;
2195 int i;
2196 u32 rate;
e2ebc833 2197 u8 use_green = il4965_rs_use_green(sta);
be663ab6
WYG
2198 u8 active_tbl = 0;
2199 u8 valid_tx_ant;
e2ebc833
SG
2200 struct il_station_priv *sta_priv;
2201 struct il_rxon_context *ctx;
be663ab6
WYG
2202
2203 if (!sta || !lq_sta)
2204 return;
2205
2206 sta_priv = (void *)sta->drv_priv;
2207 ctx = sta_priv->common.ctx;
2208
2209 i = lq_sta->last_txrate_idx;
2210
46bc8d4b 2211 valid_tx_ant = il->hw_params.valid_tx_ant;
be663ab6
WYG
2212
2213 if (!lq_sta->search_better_tbl)
2214 active_tbl = lq_sta->active_tbl;
2215 else
2216 active_tbl = 1 - lq_sta->active_tbl;
2217
2218 tbl = &(lq_sta->lq_info[active_tbl]);
2219
e2ebc833 2220 if ((i < 0) || (i >= IL_RATE_COUNT))
be663ab6
WYG
2221 i = 0;
2222
d2ddf621 2223 rate = il_rates[i].plcp;
e2ebc833 2224 tbl->ant_type = il4965_first_antenna(valid_tx_ant);
be663ab6
WYG
2225 rate |= tbl->ant_type << RATE_MCS_ANT_POS;
2226
e2ebc833 2227 if (i >= IL_FIRST_CCK_RATE && i <= IL_LAST_CCK_RATE)
be663ab6
WYG
2228 rate |= RATE_MCS_CCK_MSK;
2229
46bc8d4b 2230 il4965_rs_get_tbl_info_from_mcs(rate, il->band, tbl, &rate_idx);
e2ebc833
SG
2231 if (!il4965_rs_is_valid_ant(valid_tx_ant, tbl->ant_type))
2232 il4965_rs_toggle_antenna(valid_tx_ant, &rate, tbl);
be663ab6 2233
46bc8d4b 2234 rate = il4965_rate_n_flags_from_tbl(il, tbl, rate_idx, use_green);
be663ab6 2235 tbl->current_rate = rate;
e2ebc833
SG
2236 il4965_rs_set_expected_tpt_table(lq_sta, tbl);
2237 il4965_rs_fill_link_cmd(NULL, lq_sta, rate);
46bc8d4b
SG
2238 il->stations[lq_sta->lq.sta_id].lq = &lq_sta->lq;
2239 il_send_lq_cmd(il, ctx, &lq_sta->lq, CMD_SYNC, true);
be663ab6
WYG
2240}
2241
2242static void
46bc8d4b 2243il4965_rs_get_rate(void *il_r, struct ieee80211_sta *sta, void *il_sta,
be663ab6
WYG
2244 struct ieee80211_tx_rate_control *txrc)
2245{
2246
2247 struct sk_buff *skb = txrc->skb;
2248 struct ieee80211_supported_band *sband = txrc->sband;
46bc8d4b 2249 struct il_priv *il __maybe_unused = (struct il_priv *)il_r;
be663ab6 2250 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
46bc8d4b 2251 struct il_lq_sta *lq_sta = il_sta;
be663ab6
WYG
2252 int rate_idx;
2253
2d27c5db 2254 IL_DEBUG_RATE(il, "rate scale calculate new rate for skb\n");
be663ab6
WYG
2255
2256 /* Get max rate if user set max rate */
2257 if (lq_sta) {
2258 lq_sta->max_rate_idx = txrc->max_rate_idx;
2259 if ((sband->band == IEEE80211_BAND_5GHZ) &&
2260 (lq_sta->max_rate_idx != -1))
e2ebc833 2261 lq_sta->max_rate_idx += IL_FIRST_OFDM_RATE;
be663ab6 2262 if ((lq_sta->max_rate_idx < 0) ||
e2ebc833 2263 (lq_sta->max_rate_idx >= IL_RATE_COUNT))
be663ab6
WYG
2264 lq_sta->max_rate_idx = -1;
2265 }
2266
2267 /* Treat uninitialized rate scaling data same as non-existing. */
2268 if (lq_sta && !lq_sta->drv) {
46bc8d4b
SG
2269 IL_DEBUG_RATE(il, "Rate scaling not initialized yet.\n");
2270 il_sta = NULL;
be663ab6
WYG
2271 }
2272
2273 /* Send management frames and NO_ACK data using lowest rate. */
46bc8d4b 2274 if (rate_control_send_low(sta, il_sta, txrc))
be663ab6
WYG
2275 return;
2276
0c34861e
GD
2277 if (!lq_sta)
2278 return;
2279
be663ab6
WYG
2280 rate_idx = lq_sta->last_txrate_idx;
2281
2282 if (lq_sta->last_rate_n_flags & RATE_MCS_HT_MSK) {
e2ebc833 2283 rate_idx -= IL_FIRST_OFDM_RATE;
be663ab6
WYG
2284 /* 6M and 9M shared same MCS index */
2285 rate_idx = (rate_idx > 0) ? (rate_idx - 1) : 0;
e2ebc833
SG
2286 if (il4965_rs_extract_rate(lq_sta->last_rate_n_flags) >=
2287 IL_RATE_MIMO2_6M_PLCP)
be663ab6
WYG
2288 rate_idx = rate_idx + MCS_INDEX_PER_STREAM;
2289 info->control.rates[0].flags = IEEE80211_TX_RC_MCS;
2290 if (lq_sta->last_rate_n_flags & RATE_MCS_SGI_MSK)
2291 info->control.rates[0].flags |=
2292 IEEE80211_TX_RC_SHORT_GI;
2293 if (lq_sta->last_rate_n_flags & RATE_MCS_DUP_MSK)
2294 info->control.rates[0].flags |=
2295 IEEE80211_TX_RC_DUP_DATA;
2296 if (lq_sta->last_rate_n_flags & RATE_MCS_HT40_MSK)
2297 info->control.rates[0].flags |=
2298 IEEE80211_TX_RC_40_MHZ_WIDTH;
2299 if (lq_sta->last_rate_n_flags & RATE_MCS_GF_MSK)
2300 info->control.rates[0].flags |=
2301 IEEE80211_TX_RC_GREEN_FIELD;
2302 } else {
2303 /* Check for invalid rates */
e2ebc833 2304 if ((rate_idx < 0) || (rate_idx >= IL_RATE_COUNT_LEGACY) ||
be663ab6 2305 ((sband->band == IEEE80211_BAND_5GHZ) &&
e2ebc833 2306 (rate_idx < IL_FIRST_OFDM_RATE)))
be663ab6
WYG
2307 rate_idx = rate_lowest_index(sband, sta);
2308 /* On valid 5 GHz rate, adjust index */
2309 else if (sband->band == IEEE80211_BAND_5GHZ)
e2ebc833 2310 rate_idx -= IL_FIRST_OFDM_RATE;
be663ab6
WYG
2311 info->control.rates[0].flags = 0;
2312 }
2313 info->control.rates[0].idx = rate_idx;
2314
2315}
2316
46bc8d4b 2317static void *il4965_rs_alloc_sta(void *il_rate, struct ieee80211_sta *sta,
be663ab6
WYG
2318 gfp_t gfp)
2319{
e2ebc833
SG
2320 struct il_lq_sta *lq_sta;
2321 struct il_station_priv *sta_priv =
2322 (struct il_station_priv *) sta->drv_priv;
46bc8d4b 2323 struct il_priv *il;
be663ab6 2324
46bc8d4b
SG
2325 il = (struct il_priv *)il_rate;
2326 IL_DEBUG_RATE(il, "create station rate scale window\n");
be663ab6
WYG
2327
2328 lq_sta = &sta_priv->lq_sta;
2329
2330 return lq_sta;
2331}
2332
2333/*
2334 * Called after adding a new station to initialize rate scaling
2335 */
2336void
46bc8d4b 2337il4965_rs_rate_init(struct il_priv *il,
be663ab6
WYG
2338 struct ieee80211_sta *sta,
2339 u8 sta_id)
2340{
2341 int i, j;
46bc8d4b
SG
2342 struct ieee80211_hw *hw = il->hw;
2343 struct ieee80211_conf *conf = &il->hw->conf;
be663ab6 2344 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
e2ebc833
SG
2345 struct il_station_priv *sta_priv;
2346 struct il_lq_sta *lq_sta;
be663ab6
WYG
2347 struct ieee80211_supported_band *sband;
2348
e2ebc833 2349 sta_priv = (struct il_station_priv *) sta->drv_priv;
be663ab6
WYG
2350 lq_sta = &sta_priv->lq_sta;
2351 sband = hw->wiphy->bands[conf->channel->band];
2352
2353
2354 lq_sta->lq.sta_id = sta_id;
2355
2356 for (j = 0; j < LQ_SIZE; j++)
e2ebc833
SG
2357 for (i = 0; i < IL_RATE_COUNT; i++)
2358 il4965_rs_rate_scale_clear_window(
be663ab6
WYG
2359 &lq_sta->lq_info[j].win[i]);
2360
2361 lq_sta->flush_timer = 0;
2362 lq_sta->supp_rates = sta->supp_rates[sband->band];
2363 for (j = 0; j < LQ_SIZE; j++)
e2ebc833
SG
2364 for (i = 0; i < IL_RATE_COUNT; i++)
2365 il4965_rs_rate_scale_clear_window(
be663ab6
WYG
2366 &lq_sta->lq_info[j].win[i]);
2367
46bc8d4b 2368 IL_DEBUG_RATE(il, "LQ:"
be663ab6
WYG
2369 "*** rate scale station global init for station %d ***\n",
2370 sta_id);
2371 /* TODO: what is a good starting rate for STA? About middle? Maybe not
2372 * the lowest or the highest rate.. Could consider using RSSI from
2373 * previous packets? Need to have IEEE 802.1X auth succeed immediately
2374 * after assoc.. */
2375
2376 lq_sta->is_dup = 0;
2377 lq_sta->max_rate_idx = -1;
e2ebc833
SG
2378 lq_sta->missed_rate_counter = IL_MISSED_RATE_MAX;
2379 lq_sta->is_green = il4965_rs_use_green(sta);
46bc8d4b
SG
2380 lq_sta->active_legacy_rate = il->active_rate & ~(0x1000);
2381 lq_sta->band = il->band;
be663ab6
WYG
2382 /*
2383 * active_siso_rate mask includes 9 MBits (bit 5), and CCK (bits 0-3),
2384 * supp_rates[] does not; shift to convert format, force 9 MBits off.
2385 */
2386 lq_sta->active_siso_rate = ht_cap->mcs.rx_mask[0] << 1;
2387 lq_sta->active_siso_rate |= ht_cap->mcs.rx_mask[0] & 0x1;
2388 lq_sta->active_siso_rate &= ~((u16)0x2);
e2ebc833 2389 lq_sta->active_siso_rate <<= IL_FIRST_OFDM_RATE;
be663ab6
WYG
2390
2391 /* Same here */
2392 lq_sta->active_mimo2_rate = ht_cap->mcs.rx_mask[1] << 1;
2393 lq_sta->active_mimo2_rate |= ht_cap->mcs.rx_mask[1] & 0x1;
2394 lq_sta->active_mimo2_rate &= ~((u16)0x2);
e2ebc833 2395 lq_sta->active_mimo2_rate <<= IL_FIRST_OFDM_RATE;
be663ab6
WYG
2396
2397 /* These values will be overridden later */
2398 lq_sta->lq.general_params.single_stream_ant_msk =
46bc8d4b 2399 il4965_first_antenna(il->hw_params.valid_tx_ant);
be663ab6 2400 lq_sta->lq.general_params.dual_stream_ant_msk =
46bc8d4b
SG
2401 il->hw_params.valid_tx_ant &
2402 ~il4965_first_antenna(il->hw_params.valid_tx_ant);
be663ab6
WYG
2403 if (!lq_sta->lq.general_params.dual_stream_ant_msk) {
2404 lq_sta->lq.general_params.dual_stream_ant_msk = ANT_AB;
46bc8d4b 2405 } else if (il4965_num_of_ant(il->hw_params.valid_tx_ant) == 2) {
be663ab6 2406 lq_sta->lq.general_params.dual_stream_ant_msk =
46bc8d4b 2407 il->hw_params.valid_tx_ant;
be663ab6
WYG
2408 }
2409
2410 /* as default allow aggregation for all tids */
e2ebc833 2411 lq_sta->tx_agg_tid_en = IL_AGG_ALL_TID;
46bc8d4b 2412 lq_sta->drv = il;
be663ab6
WYG
2413
2414 /* Set last_txrate_idx to lowest rate */
2415 lq_sta->last_txrate_idx = rate_lowest_index(sband, sta);
2416 if (sband->band == IEEE80211_BAND_5GHZ)
e2ebc833 2417 lq_sta->last_txrate_idx += IL_FIRST_OFDM_RATE;
be663ab6
WYG
2418 lq_sta->is_agg = 0;
2419
2420#ifdef CONFIG_MAC80211_DEBUGFS
2421 lq_sta->dbg_fixed_rate = 0;
2422#endif
2423
46bc8d4b 2424 il4965_rs_initialize_lq(il, conf, sta, lq_sta);
be663ab6
WYG
2425}
2426
46bc8d4b 2427static void il4965_rs_fill_link_cmd(struct il_priv *il,
e2ebc833 2428 struct il_lq_sta *lq_sta, u32 new_rate)
be663ab6 2429{
e2ebc833 2430 struct il_scale_tbl_info tbl_type;
be663ab6
WYG
2431 int index = 0;
2432 int rate_idx;
2433 int repeat_rate = 0;
2434 u8 ant_toggle_cnt = 0;
2435 u8 use_ht_possible = 1;
2436 u8 valid_tx_ant = 0;
e2ebc833 2437 struct il_link_quality_cmd *lq_cmd = &lq_sta->lq;
be663ab6
WYG
2438
2439 /* Override starting rate (index 0) if needed for debug purposes */
e2ebc833 2440 il4965_rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
be663ab6
WYG
2441
2442 /* Interpret new_rate (rate_n_flags) */
e2ebc833 2443 il4965_rs_get_tbl_info_from_mcs(new_rate, lq_sta->band,
be663ab6
WYG
2444 &tbl_type, &rate_idx);
2445
2446 /* How many times should we repeat the initial rate? */
2447 if (is_legacy(tbl_type.lq_type)) {
2448 ant_toggle_cnt = 1;
e2ebc833 2449 repeat_rate = IL_NUMBER_TRY;
be663ab6 2450 } else {
e2ebc833 2451 repeat_rate = IL_HT_NUMBER_TRY;
be663ab6
WYG
2452 }
2453
2454 lq_cmd->general_params.mimo_delimiter =
2455 is_mimo(tbl_type.lq_type) ? 1 : 0;
2456
2457 /* Fill 1st table entry (index 0) */
2458 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
2459
e2ebc833 2460 if (il4965_num_of_ant(tbl_type.ant_type) == 1) {
be663ab6
WYG
2461 lq_cmd->general_params.single_stream_ant_msk =
2462 tbl_type.ant_type;
e2ebc833 2463 } else if (il4965_num_of_ant(tbl_type.ant_type) == 2) {
be663ab6
WYG
2464 lq_cmd->general_params.dual_stream_ant_msk =
2465 tbl_type.ant_type;
2466 } /* otherwise we don't modify the existing value */
2467
2468 index++;
2469 repeat_rate--;
46bc8d4b
SG
2470 if (il)
2471 valid_tx_ant = il->hw_params.valid_tx_ant;
be663ab6
WYG
2472
2473 /* Fill rest of rate table */
2474 while (index < LINK_QUAL_MAX_RETRY_NUM) {
2475 /* Repeat initial/next rate.
e2ebc833
SG
2476 * For legacy IL_NUMBER_TRY == 1, this loop will not execute.
2477 * For HT IL_HT_NUMBER_TRY == 3, this executes twice. */
be663ab6
WYG
2478 while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
2479 if (is_legacy(tbl_type.lq_type)) {
2480 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2481 ant_toggle_cnt++;
46bc8d4b 2482 else if (il &&
e2ebc833 2483 il4965_rs_toggle_antenna(valid_tx_ant,
be663ab6
WYG
2484 &new_rate, &tbl_type))
2485 ant_toggle_cnt = 1;
2486 }
2487
2488 /* Override next rate if needed for debug purposes */
e2ebc833 2489 il4965_rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
be663ab6
WYG
2490
2491 /* Fill next table entry */
2492 lq_cmd->rs_table[index].rate_n_flags =
2493 cpu_to_le32(new_rate);
2494 repeat_rate--;
2495 index++;
2496 }
2497
e2ebc833 2498 il4965_rs_get_tbl_info_from_mcs(new_rate,
be663ab6
WYG
2499 lq_sta->band, &tbl_type,
2500 &rate_idx);
2501
2502 /* Indicate to uCode which entries might be MIMO.
2503 * If initial rate was MIMO, this will finally end up
e2ebc833 2504 * as (IL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
be663ab6
WYG
2505 if (is_mimo(tbl_type.lq_type))
2506 lq_cmd->general_params.mimo_delimiter = index;
2507
2508 /* Get next rate */
e2ebc833 2509 new_rate = il4965_rs_get_lower_rate(lq_sta,
be663ab6
WYG
2510 &tbl_type, rate_idx,
2511 use_ht_possible);
2512
2513 /* How many times should we repeat the next rate? */
2514 if (is_legacy(tbl_type.lq_type)) {
2515 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2516 ant_toggle_cnt++;
46bc8d4b 2517 else if (il &&
e2ebc833 2518 il4965_rs_toggle_antenna(valid_tx_ant,
be663ab6
WYG
2519 &new_rate, &tbl_type))
2520 ant_toggle_cnt = 1;
2521
e2ebc833 2522 repeat_rate = IL_NUMBER_TRY;
be663ab6 2523 } else {
e2ebc833 2524 repeat_rate = IL_HT_NUMBER_TRY;
be663ab6
WYG
2525 }
2526
2527 /* Don't allow HT rates after next pass.
e2ebc833 2528 * il4965_rs_get_lower_rate() will change type to LQ_A or LQ_G. */
be663ab6
WYG
2529 use_ht_possible = 0;
2530
2531 /* Override next rate if needed for debug purposes */
e2ebc833 2532 il4965_rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
be663ab6
WYG
2533
2534 /* Fill next table entry */
2535 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
2536
2537 index++;
2538 repeat_rate--;
2539 }
2540
2541 lq_cmd->agg_params.agg_frame_cnt_limit = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
2542 lq_cmd->agg_params.agg_dis_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
2543
2544 lq_cmd->agg_params.agg_time_limit =
2545 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
2546}
2547
2548static void
e2ebc833 2549*il4965_rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
be663ab6
WYG
2550{
2551 return hw->priv;
2552}
2553/* rate scale requires free function to be implemented */
46bc8d4b 2554static void il4965_rs_free(void *il_rate)
be663ab6
WYG
2555{
2556 return;
2557}
2558
46bc8d4b
SG
2559static void il4965_rs_free_sta(void *il_r, struct ieee80211_sta *sta,
2560 void *il_sta)
be663ab6 2561{
46bc8d4b 2562 struct il_priv *il __maybe_unused = il_r;
be663ab6 2563
46bc8d4b
SG
2564 IL_DEBUG_RATE(il, "enter\n");
2565 IL_DEBUG_RATE(il, "leave\n");
be663ab6
WYG
2566}
2567
2568
2569#ifdef CONFIG_MAC80211_DEBUGFS
e2ebc833 2570static int il4965_open_file_generic(struct inode *inode, struct file *file)
be663ab6
WYG
2571{
2572 file->private_data = inode->i_private;
2573 return 0;
2574}
e2ebc833 2575static void il4965_rs_dbgfs_set_mcs(struct il_lq_sta *lq_sta,
be663ab6
WYG
2576 u32 *rate_n_flags, int index)
2577{
46bc8d4b 2578 struct il_priv *il;
be663ab6
WYG
2579 u8 valid_tx_ant;
2580 u8 ant_sel_tx;
2581
46bc8d4b
SG
2582 il = lq_sta->drv;
2583 valid_tx_ant = il->hw_params.valid_tx_ant;
be663ab6
WYG
2584 if (lq_sta->dbg_fixed_rate) {
2585 ant_sel_tx =
2586 ((lq_sta->dbg_fixed_rate & RATE_MCS_ANT_ABC_MSK)
2587 >> RATE_MCS_ANT_POS);
2588 if ((valid_tx_ant & ant_sel_tx) == ant_sel_tx) {
2589 *rate_n_flags = lq_sta->dbg_fixed_rate;
46bc8d4b 2590 IL_DEBUG_RATE(il, "Fixed rate ON\n");
be663ab6
WYG
2591 } else {
2592 lq_sta->dbg_fixed_rate = 0;
46bc8d4b 2593 IL_ERR(il,
be663ab6
WYG
2594 "Invalid antenna selection 0x%X, Valid is 0x%X\n",
2595 ant_sel_tx, valid_tx_ant);
46bc8d4b 2596 IL_DEBUG_RATE(il, "Fixed rate OFF\n");
be663ab6
WYG
2597 }
2598 } else {
46bc8d4b 2599 IL_DEBUG_RATE(il, "Fixed rate OFF\n");
be663ab6
WYG
2600 }
2601}
2602
e2ebc833 2603static ssize_t il4965_rs_sta_dbgfs_scale_table_write(struct file *file,
be663ab6
WYG
2604 const char __user *user_buf, size_t count, loff_t *ppos)
2605{
e2ebc833 2606 struct il_lq_sta *lq_sta = file->private_data;
46bc8d4b 2607 struct il_priv *il;
be663ab6 2608 char buf[64];
8220ba3e 2609 size_t buf_size;
be663ab6 2610 u32 parsed_rate;
e2ebc833
SG
2611 struct il_station_priv *sta_priv =
2612 container_of(lq_sta, struct il_station_priv, lq_sta);
2613 struct il_rxon_context *ctx = sta_priv->common.ctx;
be663ab6 2614
46bc8d4b 2615 il = lq_sta->drv;
be663ab6
WYG
2616 memset(buf, 0, sizeof(buf));
2617 buf_size = min(count, sizeof(buf) - 1);
2618 if (copy_from_user(buf, user_buf, buf_size))
2619 return -EFAULT;
2620
2621 if (sscanf(buf, "%x", &parsed_rate) == 1)
2622 lq_sta->dbg_fixed_rate = parsed_rate;
2623 else
2624 lq_sta->dbg_fixed_rate = 0;
2625
2626 lq_sta->active_legacy_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
2627 lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2628 lq_sta->active_mimo2_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2629
46bc8d4b 2630 IL_DEBUG_RATE(il, "sta_id %d rate 0x%X\n",
be663ab6
WYG
2631 lq_sta->lq.sta_id, lq_sta->dbg_fixed_rate);
2632
2633 if (lq_sta->dbg_fixed_rate) {
e2ebc833
SG
2634 il4965_rs_fill_link_cmd(NULL, lq_sta, lq_sta->dbg_fixed_rate);
2635 il_send_lq_cmd(lq_sta->drv, ctx, &lq_sta->lq, CMD_ASYNC,
be663ab6
WYG
2636 false);
2637 }
2638
2639 return count;
2640}
2641
e2ebc833 2642static ssize_t il4965_rs_sta_dbgfs_scale_table_read(struct file *file,
be663ab6
WYG
2643 char __user *user_buf, size_t count, loff_t *ppos)
2644{
2645 char *buff;
2646 int desc = 0;
2647 int i = 0;
2648 int index = 0;
2649 ssize_t ret;
2650
e2ebc833 2651 struct il_lq_sta *lq_sta = file->private_data;
46bc8d4b 2652 struct il_priv *il;
e2ebc833 2653 struct il_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
be663ab6 2654
46bc8d4b 2655 il = lq_sta->drv;
be663ab6
WYG
2656 buff = kmalloc(1024, GFP_KERNEL);
2657 if (!buff)
2658 return -ENOMEM;
2659
2660 desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
2661 desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
2662 lq_sta->total_failed, lq_sta->total_success,
2663 lq_sta->active_legacy_rate);
2664 desc += sprintf(buff+desc, "fixed rate 0x%X\n",
2665 lq_sta->dbg_fixed_rate);
2666 desc += sprintf(buff+desc, "valid_tx_ant %s%s%s\n",
46bc8d4b
SG
2667 (il->hw_params.valid_tx_ant & ANT_A) ? "ANT_A," : "",
2668 (il->hw_params.valid_tx_ant & ANT_B) ? "ANT_B," : "",
2669 (il->hw_params.valid_tx_ant & ANT_C) ? "ANT_C" : "");
be663ab6
WYG
2670 desc += sprintf(buff+desc, "lq type %s\n",
2671 (is_legacy(tbl->lq_type)) ? "legacy" : "HT");
2672 if (is_Ht(tbl->lq_type)) {
2673 desc += sprintf(buff+desc, " %s",
2674 (is_siso(tbl->lq_type)) ? "SISO" : "MIMO2");
2675 desc += sprintf(buff+desc, " %s",
2676 (tbl->is_ht40) ? "40MHz" : "20MHz");
2677 desc += sprintf(buff+desc, " %s %s %s\n",
2678 (tbl->is_SGI) ? "SGI" : "",
2679 (lq_sta->is_green) ? "GF enabled" : "",
2680 (lq_sta->is_agg) ? "AGG on" : "");
2681 }
2682 desc += sprintf(buff+desc, "last tx rate=0x%X\n",
2683 lq_sta->last_rate_n_flags);
2684 desc += sprintf(buff+desc, "general:"
2685 "flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
2686 lq_sta->lq.general_params.flags,
2687 lq_sta->lq.general_params.mimo_delimiter,
2688 lq_sta->lq.general_params.single_stream_ant_msk,
2689 lq_sta->lq.general_params.dual_stream_ant_msk);
2690
2691 desc += sprintf(buff+desc, "agg:"
2692 "time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
2693 le16_to_cpu(lq_sta->lq.agg_params.agg_time_limit),
2694 lq_sta->lq.agg_params.agg_dis_start_th,
2695 lq_sta->lq.agg_params.agg_frame_cnt_limit);
2696
2697 desc += sprintf(buff+desc,
2698 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
2699 lq_sta->lq.general_params.start_rate_index[0],
2700 lq_sta->lq.general_params.start_rate_index[1],
2701 lq_sta->lq.general_params.start_rate_index[2],
2702 lq_sta->lq.general_params.start_rate_index[3]);
2703
2704 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
e2ebc833 2705 index = il4965_hwrate_to_plcp_idx(
be663ab6
WYG
2706 le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags));
2707 if (is_legacy(tbl->lq_type)) {
2708 desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps\n",
2709 i,
2710 le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags),
e2ebc833 2711 il_rate_mcs[index].mbps);
be663ab6
WYG
2712 } else {
2713 desc += sprintf(buff+desc,
2714 " rate[%d] 0x%X %smbps (%s)\n",
2715 i,
2716 le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags),
e2ebc833 2717 il_rate_mcs[index].mbps, il_rate_mcs[index].mcs);
be663ab6
WYG
2718 }
2719 }
2720
2721 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2722 kfree(buff);
2723 return ret;
2724}
2725
2726static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
e2ebc833
SG
2727 .write = il4965_rs_sta_dbgfs_scale_table_write,
2728 .read = il4965_rs_sta_dbgfs_scale_table_read,
2729 .open = il4965_open_file_generic,
be663ab6
WYG
2730 .llseek = default_llseek,
2731};
e2ebc833 2732static ssize_t il4965_rs_sta_dbgfs_stats_table_read(struct file *file,
be663ab6
WYG
2733 char __user *user_buf, size_t count, loff_t *ppos)
2734{
2735 char *buff;
2736 int desc = 0;
2737 int i, j;
2738 ssize_t ret;
2739
e2ebc833 2740 struct il_lq_sta *lq_sta = file->private_data;
be663ab6
WYG
2741
2742 buff = kmalloc(1024, GFP_KERNEL);
2743 if (!buff)
2744 return -ENOMEM;
2745
2746 for (i = 0; i < LQ_SIZE; i++) {
2747 desc += sprintf(buff+desc,
2748 "%s type=%d SGI=%d HT40=%d DUP=%d GF=%d\n"
2749 "rate=0x%X\n",
2750 lq_sta->active_tbl == i ? "*" : "x",
2751 lq_sta->lq_info[i].lq_type,
2752 lq_sta->lq_info[i].is_SGI,
2753 lq_sta->lq_info[i].is_ht40,
2754 lq_sta->lq_info[i].is_dup,
2755 lq_sta->is_green,
2756 lq_sta->lq_info[i].current_rate);
e2ebc833 2757 for (j = 0; j < IL_RATE_COUNT; j++) {
be663ab6
WYG
2758 desc += sprintf(buff+desc,
2759 "counter=%d success=%d %%=%d\n",
2760 lq_sta->lq_info[i].win[j].counter,
2761 lq_sta->lq_info[i].win[j].success_counter,
2762 lq_sta->lq_info[i].win[j].success_ratio);
2763 }
2764 }
2765 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2766 kfree(buff);
2767 return ret;
2768}
2769
2770static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
e2ebc833
SG
2771 .read = il4965_rs_sta_dbgfs_stats_table_read,
2772 .open = il4965_open_file_generic,
be663ab6
WYG
2773 .llseek = default_llseek,
2774};
2775
e2ebc833 2776static ssize_t il4965_rs_sta_dbgfs_rate_scale_data_read(struct file *file,
be663ab6
WYG
2777 char __user *user_buf, size_t count, loff_t *ppos)
2778{
2779 char buff[120];
2780 int desc = 0;
2781 ssize_t ret;
2782
e2ebc833 2783 struct il_lq_sta *lq_sta = file->private_data;
46bc8d4b 2784 struct il_priv *il;
e2ebc833 2785 struct il_scale_tbl_info *tbl = &lq_sta->lq_info[lq_sta->active_tbl];
be663ab6 2786
46bc8d4b 2787 il = lq_sta->drv;
be663ab6
WYG
2788
2789 if (is_Ht(tbl->lq_type))
2790 desc += sprintf(buff+desc,
2791 "Bit Rate= %d Mb/s\n",
2792 tbl->expected_tpt[lq_sta->last_txrate_idx]);
2793 else
2794 desc += sprintf(buff+desc,
2795 "Bit Rate= %d Mb/s\n",
d2ddf621 2796 il_rates[lq_sta->last_txrate_idx].ieee >> 1);
be663ab6
WYG
2797
2798 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2799 return ret;
2800}
2801
2802static const struct file_operations rs_sta_dbgfs_rate_scale_data_ops = {
e2ebc833
SG
2803 .read = il4965_rs_sta_dbgfs_rate_scale_data_read,
2804 .open = il4965_open_file_generic,
be663ab6
WYG
2805 .llseek = default_llseek,
2806};
2807
46bc8d4b 2808static void il4965_rs_add_debugfs(void *il, void *il_sta,
be663ab6
WYG
2809 struct dentry *dir)
2810{
46bc8d4b 2811 struct il_lq_sta *lq_sta = il_sta;
be663ab6
WYG
2812 lq_sta->rs_sta_dbgfs_scale_table_file =
2813 debugfs_create_file("rate_scale_table", S_IRUSR | S_IWUSR, dir,
2814 lq_sta, &rs_sta_dbgfs_scale_table_ops);
2815 lq_sta->rs_sta_dbgfs_stats_table_file =
2816 debugfs_create_file("rate_stats_table", S_IRUSR, dir,
2817 lq_sta, &rs_sta_dbgfs_stats_table_ops);
2818 lq_sta->rs_sta_dbgfs_rate_scale_data_file =
2819 debugfs_create_file("rate_scale_data", S_IRUSR, dir,
2820 lq_sta, &rs_sta_dbgfs_rate_scale_data_ops);
2821 lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file =
2822 debugfs_create_u8("tx_agg_tid_enable", S_IRUSR | S_IWUSR, dir,
2823 &lq_sta->tx_agg_tid_en);
2824
2825}
2826
46bc8d4b 2827static void il4965_rs_remove_debugfs(void *il, void *il_sta)
be663ab6 2828{
46bc8d4b 2829 struct il_lq_sta *lq_sta = il_sta;
be663ab6
WYG
2830 debugfs_remove(lq_sta->rs_sta_dbgfs_scale_table_file);
2831 debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
2832 debugfs_remove(lq_sta->rs_sta_dbgfs_rate_scale_data_file);
2833 debugfs_remove(lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file);
2834}
2835#endif
2836
2837/*
2838 * Initialization of rate scaling information is done by driver after
2839 * the station is added. Since mac80211 calls this function before a
2840 * station is added we ignore it.
2841 */
2842static void
46bc8d4b
SG
2843il4965_rs_rate_init_stub(void *il_r, struct ieee80211_supported_band *sband,
2844 struct ieee80211_sta *sta, void *il_sta)
be663ab6
WYG
2845{
2846}
2847static struct rate_control_ops rs_4965_ops = {
2848 .module = NULL,
2849 .name = IWL4965_RS_NAME,
e2ebc833
SG
2850 .tx_status = il4965_rs_tx_status,
2851 .get_rate = il4965_rs_get_rate,
2852 .rate_init = il4965_rs_rate_init_stub,
2853 .alloc = il4965_rs_alloc,
2854 .free = il4965_rs_free,
2855 .alloc_sta = il4965_rs_alloc_sta,
2856 .free_sta = il4965_rs_free_sta,
be663ab6 2857#ifdef CONFIG_MAC80211_DEBUGFS
e2ebc833
SG
2858 .add_sta_debugfs = il4965_rs_add_debugfs,
2859 .remove_sta_debugfs = il4965_rs_remove_debugfs,
be663ab6
WYG
2860#endif
2861};
2862
e2ebc833 2863int il4965_rate_control_register(void)
be663ab6 2864{
be663ab6
WYG
2865 return ieee80211_rate_control_register(&rs_4965_ops);
2866}
2867
e2ebc833 2868void il4965_rate_control_unregister(void)
be663ab6
WYG
2869{
2870 ieee80211_rate_control_unregister(&rs_4965_ops);
2871}
This page took 0.265591 seconds and 5 git commands to generate.