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