162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
262306a36Sopenharmony_ci/******************************************************************************
362306a36Sopenharmony_ci *
462306a36Sopenharmony_ci * Copyright(c) 2005 - 2011 Intel Corporation. All rights reserved.
562306a36Sopenharmony_ci *
662306a36Sopenharmony_ci * Contact Information:
762306a36Sopenharmony_ci *  Intel Linux Wireless <ilw@linux.intel.com>
862306a36Sopenharmony_ci * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
962306a36Sopenharmony_ci *
1062306a36Sopenharmony_ci *****************************************************************************/
1162306a36Sopenharmony_ci#include <linux/kernel.h>
1262306a36Sopenharmony_ci#include <linux/skbuff.h>
1362306a36Sopenharmony_ci#include <linux/slab.h>
1462306a36Sopenharmony_ci#include <net/mac80211.h>
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ci#include <linux/netdevice.h>
1762306a36Sopenharmony_ci#include <linux/etherdevice.h>
1862306a36Sopenharmony_ci#include <linux/delay.h>
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci#include <linux/workqueue.h>
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_ci#include "common.h"
2362306a36Sopenharmony_ci#include "4965.h"
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci#define IL4965_RS_NAME "iwl-4965-rs"
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_ci#define NUM_TRY_BEFORE_ANT_TOGGLE 1
2862306a36Sopenharmony_ci#define IL_NUMBER_TRY      1
2962306a36Sopenharmony_ci#define IL_HT_NUMBER_TRY   3
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_ci#define RATE_MAX_WINDOW		62	/* # tx in history win */
3262306a36Sopenharmony_ci#define RATE_MIN_FAILURE_TH		6	/* min failures to calc tpt */
3362306a36Sopenharmony_ci#define RATE_MIN_SUCCESS_TH		8	/* min successes to calc tpt */
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ci/* max allowed rate miss before sync LQ cmd */
3662306a36Sopenharmony_ci#define IL_MISSED_RATE_MAX		15
3762306a36Sopenharmony_ci/* max time to accum history 2 seconds */
3862306a36Sopenharmony_ci#define RATE_SCALE_FLUSH_INTVL   (3*HZ)
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_cistatic u8 rs_ht_to_legacy[] = {
4162306a36Sopenharmony_ci	RATE_6M_IDX, RATE_6M_IDX,
4262306a36Sopenharmony_ci	RATE_6M_IDX, RATE_6M_IDX,
4362306a36Sopenharmony_ci	RATE_6M_IDX,
4462306a36Sopenharmony_ci	RATE_6M_IDX, RATE_9M_IDX,
4562306a36Sopenharmony_ci	RATE_12M_IDX, RATE_18M_IDX,
4662306a36Sopenharmony_ci	RATE_24M_IDX, RATE_36M_IDX,
4762306a36Sopenharmony_ci	RATE_48M_IDX, RATE_54M_IDX
4862306a36Sopenharmony_ci};
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_cistatic const u8 ant_toggle_lookup[] = {
5162306a36Sopenharmony_ci	/*ANT_NONE -> */ ANT_NONE,
5262306a36Sopenharmony_ci	/*ANT_A    -> */ ANT_B,
5362306a36Sopenharmony_ci	/*ANT_B    -> */ ANT_C,
5462306a36Sopenharmony_ci	/*ANT_AB   -> */ ANT_BC,
5562306a36Sopenharmony_ci	/*ANT_C    -> */ ANT_A,
5662306a36Sopenharmony_ci	/*ANT_AC   -> */ ANT_AB,
5762306a36Sopenharmony_ci	/*ANT_BC   -> */ ANT_AC,
5862306a36Sopenharmony_ci	/*ANT_ABC  -> */ ANT_ABC,
5962306a36Sopenharmony_ci};
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_ci#define IL_DECLARE_RATE_INFO(r, s, ip, in, rp, rn, pp, np)    \
6262306a36Sopenharmony_ci	[RATE_##r##M_IDX] = { RATE_##r##M_PLCP,      \
6362306a36Sopenharmony_ci				    RATE_SISO_##s##M_PLCP, \
6462306a36Sopenharmony_ci				    RATE_MIMO2_##s##M_PLCP,\
6562306a36Sopenharmony_ci				    RATE_##r##M_IEEE,      \
6662306a36Sopenharmony_ci				    RATE_##ip##M_IDX,    \
6762306a36Sopenharmony_ci				    RATE_##in##M_IDX,    \
6862306a36Sopenharmony_ci				    RATE_##rp##M_IDX,    \
6962306a36Sopenharmony_ci				    RATE_##rn##M_IDX,    \
7062306a36Sopenharmony_ci				    RATE_##pp##M_IDX,    \
7162306a36Sopenharmony_ci				    RATE_##np##M_IDX }
7262306a36Sopenharmony_ci
7362306a36Sopenharmony_ci/*
7462306a36Sopenharmony_ci * Parameter order:
7562306a36Sopenharmony_ci *   rate, ht rate, prev rate, next rate, prev tgg rate, next tgg rate
7662306a36Sopenharmony_ci *
7762306a36Sopenharmony_ci * If there isn't a valid next or previous rate then INV is used which
7862306a36Sopenharmony_ci * maps to RATE_INVALID
7962306a36Sopenharmony_ci *
8062306a36Sopenharmony_ci */
8162306a36Sopenharmony_ciconst struct il_rate_info il_rates[RATE_COUNT] = {
8262306a36Sopenharmony_ci	IL_DECLARE_RATE_INFO(1, INV, INV, 2, INV, 2, INV, 2),	/*  1mbps */
8362306a36Sopenharmony_ci	IL_DECLARE_RATE_INFO(2, INV, 1, 5, 1, 5, 1, 5),		/*  2mbps */
8462306a36Sopenharmony_ci	IL_DECLARE_RATE_INFO(5, INV, 2, 6, 2, 11, 2, 11),	/*5.5mbps */
8562306a36Sopenharmony_ci	IL_DECLARE_RATE_INFO(11, INV, 9, 12, 9, 12, 5, 18),	/* 11mbps */
8662306a36Sopenharmony_ci	IL_DECLARE_RATE_INFO(6, 6, 5, 9, 5, 11, 5, 11),		/*  6mbps */
8762306a36Sopenharmony_ci	IL_DECLARE_RATE_INFO(9, 6, 6, 11, 6, 11, 5, 11),	/*  9mbps */
8862306a36Sopenharmony_ci	IL_DECLARE_RATE_INFO(12, 12, 11, 18, 11, 18, 11, 18),	/* 12mbps */
8962306a36Sopenharmony_ci	IL_DECLARE_RATE_INFO(18, 18, 12, 24, 12, 24, 11, 24),	/* 18mbps */
9062306a36Sopenharmony_ci	IL_DECLARE_RATE_INFO(24, 24, 18, 36, 18, 36, 18, 36),	/* 24mbps */
9162306a36Sopenharmony_ci	IL_DECLARE_RATE_INFO(36, 36, 24, 48, 24, 48, 24, 48),	/* 36mbps */
9262306a36Sopenharmony_ci	IL_DECLARE_RATE_INFO(48, 48, 36, 54, 36, 54, 36, 54),	/* 48mbps */
9362306a36Sopenharmony_ci	IL_DECLARE_RATE_INFO(54, 54, 48, INV, 48, INV, 48, INV),/* 54mbps */
9462306a36Sopenharmony_ci	IL_DECLARE_RATE_INFO(60, 60, 48, INV, 48, INV, 48, INV),/* 60mbps */
9562306a36Sopenharmony_ci};
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_cistatic int
9862306a36Sopenharmony_ciil4965_hwrate_to_plcp_idx(u32 rate_n_flags)
9962306a36Sopenharmony_ci{
10062306a36Sopenharmony_ci	int idx = 0;
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_ci	/* HT rate format */
10362306a36Sopenharmony_ci	if (rate_n_flags & RATE_MCS_HT_MSK) {
10462306a36Sopenharmony_ci		idx = (rate_n_flags & 0xff);
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_ci		if (idx >= RATE_MIMO2_6M_PLCP)
10762306a36Sopenharmony_ci			idx = idx - RATE_MIMO2_6M_PLCP;
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_ci		idx += IL_FIRST_OFDM_RATE;
11062306a36Sopenharmony_ci		/* skip 9M not supported in ht */
11162306a36Sopenharmony_ci		if (idx >= RATE_9M_IDX)
11262306a36Sopenharmony_ci			idx += 1;
11362306a36Sopenharmony_ci		if (idx >= IL_FIRST_OFDM_RATE && idx <= IL_LAST_OFDM_RATE)
11462306a36Sopenharmony_ci			return idx;
11562306a36Sopenharmony_ci
11662306a36Sopenharmony_ci		/* legacy rate format, search for match in table */
11762306a36Sopenharmony_ci	} else {
11862306a36Sopenharmony_ci		for (idx = 0; idx < ARRAY_SIZE(il_rates); idx++)
11962306a36Sopenharmony_ci			if (il_rates[idx].plcp == (rate_n_flags & 0xFF))
12062306a36Sopenharmony_ci				return idx;
12162306a36Sopenharmony_ci	}
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_ci	return -1;
12462306a36Sopenharmony_ci}
12562306a36Sopenharmony_ci
12662306a36Sopenharmony_cistatic void il4965_rs_rate_scale_perform(struct il_priv *il,
12762306a36Sopenharmony_ci					 struct sk_buff *skb,
12862306a36Sopenharmony_ci					 struct ieee80211_sta *sta,
12962306a36Sopenharmony_ci					 struct il_lq_sta *lq_sta);
13062306a36Sopenharmony_cistatic void il4965_rs_fill_link_cmd(struct il_priv *il,
13162306a36Sopenharmony_ci				    struct il_lq_sta *lq_sta, u32 rate_n_flags);
13262306a36Sopenharmony_cistatic void il4965_rs_stay_in_table(struct il_lq_sta *lq_sta,
13362306a36Sopenharmony_ci				    bool force_search);
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_ci#ifdef CONFIG_MAC80211_DEBUGFS
13662306a36Sopenharmony_cistatic void il4965_rs_dbgfs_set_mcs(struct il_lq_sta *lq_sta,
13762306a36Sopenharmony_ci				    u32 *rate_n_flags, int idx);
13862306a36Sopenharmony_ci#else
13962306a36Sopenharmony_cistatic void
14062306a36Sopenharmony_ciil4965_rs_dbgfs_set_mcs(struct il_lq_sta *lq_sta, u32 * rate_n_flags, int idx)
14162306a36Sopenharmony_ci{
14262306a36Sopenharmony_ci}
14362306a36Sopenharmony_ci#endif
14462306a36Sopenharmony_ci
14562306a36Sopenharmony_ci/*
14662306a36Sopenharmony_ci * The following tables contain the expected throughput metrics for all rates
14762306a36Sopenharmony_ci *
14862306a36Sopenharmony_ci *	1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
14962306a36Sopenharmony_ci *
15062306a36Sopenharmony_ci * where invalid entries are zeros.
15162306a36Sopenharmony_ci *
15262306a36Sopenharmony_ci * CCK rates are only valid in legacy table and will only be used in G
15362306a36Sopenharmony_ci * (2.4 GHz) band.
15462306a36Sopenharmony_ci */
15562306a36Sopenharmony_ci
15662306a36Sopenharmony_cistatic s32 expected_tpt_legacy[RATE_COUNT] = {
15762306a36Sopenharmony_ci	7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 0
15862306a36Sopenharmony_ci};
15962306a36Sopenharmony_ci
16062306a36Sopenharmony_cistatic s32 expected_tpt_siso20MHz[4][RATE_COUNT] = {
16162306a36Sopenharmony_ci	{0, 0, 0, 0, 42, 0, 76, 102, 124, 158, 183, 193, 202},	/* Norm */
16262306a36Sopenharmony_ci	{0, 0, 0, 0, 46, 0, 82, 110, 132, 167, 192, 202, 210},	/* SGI */
16362306a36Sopenharmony_ci	{0, 0, 0, 0, 48, 0, 93, 135, 176, 251, 319, 351, 381},	/* AGG */
16462306a36Sopenharmony_ci	{0, 0, 0, 0, 53, 0, 102, 149, 193, 275, 348, 381, 413},	/* AGG+SGI */
16562306a36Sopenharmony_ci};
16662306a36Sopenharmony_ci
16762306a36Sopenharmony_cistatic s32 expected_tpt_siso40MHz[4][RATE_COUNT] = {
16862306a36Sopenharmony_ci	{0, 0, 0, 0, 77, 0, 127, 160, 184, 220, 242, 250, 257},	/* Norm */
16962306a36Sopenharmony_ci	{0, 0, 0, 0, 83, 0, 135, 169, 193, 229, 250, 257, 264},	/* SGI */
17062306a36Sopenharmony_ci	{0, 0, 0, 0, 96, 0, 182, 259, 328, 451, 553, 598, 640},	/* AGG */
17162306a36Sopenharmony_ci	{0, 0, 0, 0, 106, 0, 199, 282, 357, 487, 593, 640, 683},	/* AGG+SGI */
17262306a36Sopenharmony_ci};
17362306a36Sopenharmony_ci
17462306a36Sopenharmony_cistatic s32 expected_tpt_mimo2_20MHz[4][RATE_COUNT] = {
17562306a36Sopenharmony_ci	{0, 0, 0, 0, 74, 0, 123, 155, 179, 213, 235, 243, 250},	/* Norm */
17662306a36Sopenharmony_ci	{0, 0, 0, 0, 81, 0, 131, 164, 187, 221, 242, 250, 256},	/* SGI */
17762306a36Sopenharmony_ci	{0, 0, 0, 0, 92, 0, 175, 250, 317, 436, 534, 578, 619},	/* AGG */
17862306a36Sopenharmony_ci	{0, 0, 0, 0, 102, 0, 192, 273, 344, 470, 573, 619, 660},	/* AGG+SGI */
17962306a36Sopenharmony_ci};
18062306a36Sopenharmony_ci
18162306a36Sopenharmony_cistatic s32 expected_tpt_mimo2_40MHz[4][RATE_COUNT] = {
18262306a36Sopenharmony_ci	{0, 0, 0, 0, 123, 0, 182, 214, 235, 264, 279, 285, 289},	/* Norm */
18362306a36Sopenharmony_ci	{0, 0, 0, 0, 131, 0, 191, 222, 242, 270, 284, 289, 293},	/* SGI */
18462306a36Sopenharmony_ci	{0, 0, 0, 0, 180, 0, 327, 446, 545, 708, 828, 878, 922},	/* AGG */
18562306a36Sopenharmony_ci	{0, 0, 0, 0, 197, 0, 355, 481, 584, 752, 872, 922, 966},	/* AGG+SGI */
18662306a36Sopenharmony_ci};
18762306a36Sopenharmony_ci
18862306a36Sopenharmony_ci/* mbps, mcs */
18962306a36Sopenharmony_cistatic const struct il_rate_mcs_info il_rate_mcs[RATE_COUNT] = {
19062306a36Sopenharmony_ci	{"1", "BPSK DSSS"},
19162306a36Sopenharmony_ci	{"2", "QPSK DSSS"},
19262306a36Sopenharmony_ci	{"5.5", "BPSK CCK"},
19362306a36Sopenharmony_ci	{"11", "QPSK CCK"},
19462306a36Sopenharmony_ci	{"6", "BPSK 1/2"},
19562306a36Sopenharmony_ci	{"9", "BPSK 1/2"},
19662306a36Sopenharmony_ci	{"12", "QPSK 1/2"},
19762306a36Sopenharmony_ci	{"18", "QPSK 3/4"},
19862306a36Sopenharmony_ci	{"24", "16QAM 1/2"},
19962306a36Sopenharmony_ci	{"36", "16QAM 3/4"},
20062306a36Sopenharmony_ci	{"48", "64QAM 2/3"},
20162306a36Sopenharmony_ci	{"54", "64QAM 3/4"},
20262306a36Sopenharmony_ci	{"60", "64QAM 5/6"},
20362306a36Sopenharmony_ci};
20462306a36Sopenharmony_ci
20562306a36Sopenharmony_ci#define MCS_IDX_PER_STREAM	(8)
20662306a36Sopenharmony_ci
20762306a36Sopenharmony_cistatic inline u8
20862306a36Sopenharmony_ciil4965_rs_extract_rate(u32 rate_n_flags)
20962306a36Sopenharmony_ci{
21062306a36Sopenharmony_ci	return (u8) (rate_n_flags & 0xFF);
21162306a36Sopenharmony_ci}
21262306a36Sopenharmony_ci
21362306a36Sopenharmony_cistatic void
21462306a36Sopenharmony_ciil4965_rs_rate_scale_clear_win(struct il_rate_scale_data *win)
21562306a36Sopenharmony_ci{
21662306a36Sopenharmony_ci	win->data = 0;
21762306a36Sopenharmony_ci	win->success_counter = 0;
21862306a36Sopenharmony_ci	win->success_ratio = IL_INVALID_VALUE;
21962306a36Sopenharmony_ci	win->counter = 0;
22062306a36Sopenharmony_ci	win->average_tpt = IL_INVALID_VALUE;
22162306a36Sopenharmony_ci	win->stamp = 0;
22262306a36Sopenharmony_ci}
22362306a36Sopenharmony_ci
22462306a36Sopenharmony_cistatic inline u8
22562306a36Sopenharmony_ciil4965_rs_is_valid_ant(u8 valid_antenna, u8 ant_type)
22662306a36Sopenharmony_ci{
22762306a36Sopenharmony_ci	return (ant_type & valid_antenna) == ant_type;
22862306a36Sopenharmony_ci}
22962306a36Sopenharmony_ci
23062306a36Sopenharmony_ci/*
23162306a36Sopenharmony_ci *	removes the old data from the stats. All data that is older than
23262306a36Sopenharmony_ci *	TID_MAX_TIME_DIFF, will be deleted.
23362306a36Sopenharmony_ci */
23462306a36Sopenharmony_cistatic void
23562306a36Sopenharmony_ciil4965_rs_tl_rm_old_stats(struct il_traffic_load *tl, u32 curr_time)
23662306a36Sopenharmony_ci{
23762306a36Sopenharmony_ci	/* The oldest age we want to keep */
23862306a36Sopenharmony_ci	u32 oldest_time = curr_time - TID_MAX_TIME_DIFF;
23962306a36Sopenharmony_ci
24062306a36Sopenharmony_ci	while (tl->queue_count && tl->time_stamp < oldest_time) {
24162306a36Sopenharmony_ci		tl->total -= tl->packet_count[tl->head];
24262306a36Sopenharmony_ci		tl->packet_count[tl->head] = 0;
24362306a36Sopenharmony_ci		tl->time_stamp += TID_QUEUE_CELL_SPACING;
24462306a36Sopenharmony_ci		tl->queue_count--;
24562306a36Sopenharmony_ci		tl->head++;
24662306a36Sopenharmony_ci		if (tl->head >= TID_QUEUE_MAX_SIZE)
24762306a36Sopenharmony_ci			tl->head = 0;
24862306a36Sopenharmony_ci	}
24962306a36Sopenharmony_ci}
25062306a36Sopenharmony_ci
25162306a36Sopenharmony_ci/*
25262306a36Sopenharmony_ci *	increment traffic load value for tid and also remove
25362306a36Sopenharmony_ci *	any old values if passed the certain time period
25462306a36Sopenharmony_ci */
25562306a36Sopenharmony_cistatic u8
25662306a36Sopenharmony_ciil4965_rs_tl_add_packet(struct il_lq_sta *lq_data, struct ieee80211_hdr *hdr)
25762306a36Sopenharmony_ci{
25862306a36Sopenharmony_ci	u32 curr_time = jiffies_to_msecs(jiffies);
25962306a36Sopenharmony_ci	u32 time_diff;
26062306a36Sopenharmony_ci	s32 idx;
26162306a36Sopenharmony_ci	struct il_traffic_load *tl = NULL;
26262306a36Sopenharmony_ci	u8 tid;
26362306a36Sopenharmony_ci
26462306a36Sopenharmony_ci	if (ieee80211_is_data_qos(hdr->frame_control)) {
26562306a36Sopenharmony_ci		u8 *qc = ieee80211_get_qos_ctl(hdr);
26662306a36Sopenharmony_ci		tid = qc[0] & 0xf;
26762306a36Sopenharmony_ci	} else
26862306a36Sopenharmony_ci		return MAX_TID_COUNT;
26962306a36Sopenharmony_ci
27062306a36Sopenharmony_ci	if (unlikely(tid >= TID_MAX_LOAD_COUNT))
27162306a36Sopenharmony_ci		return MAX_TID_COUNT;
27262306a36Sopenharmony_ci
27362306a36Sopenharmony_ci	tl = &lq_data->load[tid];
27462306a36Sopenharmony_ci
27562306a36Sopenharmony_ci	curr_time -= curr_time % TID_ROUND_VALUE;
27662306a36Sopenharmony_ci
27762306a36Sopenharmony_ci	/* Happens only for the first packet. Initialize the data */
27862306a36Sopenharmony_ci	if (!(tl->queue_count)) {
27962306a36Sopenharmony_ci		tl->total = 1;
28062306a36Sopenharmony_ci		tl->time_stamp = curr_time;
28162306a36Sopenharmony_ci		tl->queue_count = 1;
28262306a36Sopenharmony_ci		tl->head = 0;
28362306a36Sopenharmony_ci		tl->packet_count[0] = 1;
28462306a36Sopenharmony_ci		return MAX_TID_COUNT;
28562306a36Sopenharmony_ci	}
28662306a36Sopenharmony_ci
28762306a36Sopenharmony_ci	time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
28862306a36Sopenharmony_ci	idx = time_diff / TID_QUEUE_CELL_SPACING;
28962306a36Sopenharmony_ci
29062306a36Sopenharmony_ci	/* The history is too long: remove data that is older than */
29162306a36Sopenharmony_ci	/* TID_MAX_TIME_DIFF */
29262306a36Sopenharmony_ci	if (idx >= TID_QUEUE_MAX_SIZE)
29362306a36Sopenharmony_ci		il4965_rs_tl_rm_old_stats(tl, curr_time);
29462306a36Sopenharmony_ci
29562306a36Sopenharmony_ci	idx = (tl->head + idx) % TID_QUEUE_MAX_SIZE;
29662306a36Sopenharmony_ci	tl->packet_count[idx] = tl->packet_count[idx] + 1;
29762306a36Sopenharmony_ci	tl->total = tl->total + 1;
29862306a36Sopenharmony_ci
29962306a36Sopenharmony_ci	if ((idx + 1) > tl->queue_count)
30062306a36Sopenharmony_ci		tl->queue_count = idx + 1;
30162306a36Sopenharmony_ci
30262306a36Sopenharmony_ci	return tid;
30362306a36Sopenharmony_ci}
30462306a36Sopenharmony_ci
30562306a36Sopenharmony_ci/*
30662306a36Sopenharmony_ci	get the traffic load value for tid
30762306a36Sopenharmony_ci*/
30862306a36Sopenharmony_cistatic u32
30962306a36Sopenharmony_ciil4965_rs_tl_get_load(struct il_lq_sta *lq_data, u8 tid)
31062306a36Sopenharmony_ci{
31162306a36Sopenharmony_ci	u32 curr_time = jiffies_to_msecs(jiffies);
31262306a36Sopenharmony_ci	u32 time_diff;
31362306a36Sopenharmony_ci	s32 idx;
31462306a36Sopenharmony_ci	struct il_traffic_load *tl = NULL;
31562306a36Sopenharmony_ci
31662306a36Sopenharmony_ci	if (tid >= TID_MAX_LOAD_COUNT)
31762306a36Sopenharmony_ci		return 0;
31862306a36Sopenharmony_ci
31962306a36Sopenharmony_ci	tl = &(lq_data->load[tid]);
32062306a36Sopenharmony_ci
32162306a36Sopenharmony_ci	curr_time -= curr_time % TID_ROUND_VALUE;
32262306a36Sopenharmony_ci
32362306a36Sopenharmony_ci	if (!(tl->queue_count))
32462306a36Sopenharmony_ci		return 0;
32562306a36Sopenharmony_ci
32662306a36Sopenharmony_ci	time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
32762306a36Sopenharmony_ci	idx = time_diff / TID_QUEUE_CELL_SPACING;
32862306a36Sopenharmony_ci
32962306a36Sopenharmony_ci	/* The history is too long: remove data that is older than */
33062306a36Sopenharmony_ci	/* TID_MAX_TIME_DIFF */
33162306a36Sopenharmony_ci	if (idx >= TID_QUEUE_MAX_SIZE)
33262306a36Sopenharmony_ci		il4965_rs_tl_rm_old_stats(tl, curr_time);
33362306a36Sopenharmony_ci
33462306a36Sopenharmony_ci	return tl->total;
33562306a36Sopenharmony_ci}
33662306a36Sopenharmony_ci
33762306a36Sopenharmony_cistatic int
33862306a36Sopenharmony_ciil4965_rs_tl_turn_on_agg_for_tid(struct il_priv *il, struct il_lq_sta *lq_data,
33962306a36Sopenharmony_ci				 u8 tid, struct ieee80211_sta *sta)
34062306a36Sopenharmony_ci{
34162306a36Sopenharmony_ci	int ret = -EAGAIN;
34262306a36Sopenharmony_ci	u32 load;
34362306a36Sopenharmony_ci
34462306a36Sopenharmony_ci	load = il4965_rs_tl_get_load(lq_data, tid);
34562306a36Sopenharmony_ci
34662306a36Sopenharmony_ci	if (load > IL_AGG_LOAD_THRESHOLD) {
34762306a36Sopenharmony_ci		D_HT("Starting Tx agg: STA: %pM tid: %d\n", sta->addr, tid);
34862306a36Sopenharmony_ci		ret = ieee80211_start_tx_ba_session(sta, tid, 5000);
34962306a36Sopenharmony_ci		if (ret == -EAGAIN) {
35062306a36Sopenharmony_ci			/*
35162306a36Sopenharmony_ci			 * driver and mac80211 is out of sync
35262306a36Sopenharmony_ci			 * this might be cause by reloading firmware
35362306a36Sopenharmony_ci			 * stop the tx ba session here
35462306a36Sopenharmony_ci			 */
35562306a36Sopenharmony_ci			IL_ERR("Fail start Tx agg on tid: %d\n", tid);
35662306a36Sopenharmony_ci			ieee80211_stop_tx_ba_session(sta, tid);
35762306a36Sopenharmony_ci		}
35862306a36Sopenharmony_ci	} else
35962306a36Sopenharmony_ci		D_HT("Aggregation not enabled for tid %d because load = %u\n",
36062306a36Sopenharmony_ci		     tid, load);
36162306a36Sopenharmony_ci
36262306a36Sopenharmony_ci	return ret;
36362306a36Sopenharmony_ci}
36462306a36Sopenharmony_ci
36562306a36Sopenharmony_cistatic void
36662306a36Sopenharmony_ciil4965_rs_tl_turn_on_agg(struct il_priv *il, u8 tid, struct il_lq_sta *lq_data,
36762306a36Sopenharmony_ci			 struct ieee80211_sta *sta)
36862306a36Sopenharmony_ci{
36962306a36Sopenharmony_ci	if (tid < TID_MAX_LOAD_COUNT)
37062306a36Sopenharmony_ci		il4965_rs_tl_turn_on_agg_for_tid(il, lq_data, tid, sta);
37162306a36Sopenharmony_ci	else
37262306a36Sopenharmony_ci		IL_ERR("tid exceeds max load count: %d/%d\n", tid,
37362306a36Sopenharmony_ci		       TID_MAX_LOAD_COUNT);
37462306a36Sopenharmony_ci}
37562306a36Sopenharmony_ci
37662306a36Sopenharmony_cistatic inline int
37762306a36Sopenharmony_ciil4965_get_il4965_num_of_ant_from_rate(u32 rate_n_flags)
37862306a36Sopenharmony_ci{
37962306a36Sopenharmony_ci	return !!(rate_n_flags & RATE_MCS_ANT_A_MSK) +
38062306a36Sopenharmony_ci	    !!(rate_n_flags & RATE_MCS_ANT_B_MSK) +
38162306a36Sopenharmony_ci	    !!(rate_n_flags & RATE_MCS_ANT_C_MSK);
38262306a36Sopenharmony_ci}
38362306a36Sopenharmony_ci
38462306a36Sopenharmony_ci/*
38562306a36Sopenharmony_ci * Static function to get the expected throughput from an il_scale_tbl_info
38662306a36Sopenharmony_ci * that wraps a NULL pointer check
38762306a36Sopenharmony_ci */
38862306a36Sopenharmony_cistatic s32
38962306a36Sopenharmony_ciil4965_get_expected_tpt(struct il_scale_tbl_info *tbl, int rs_idx)
39062306a36Sopenharmony_ci{
39162306a36Sopenharmony_ci	if (tbl->expected_tpt)
39262306a36Sopenharmony_ci		return tbl->expected_tpt[rs_idx];
39362306a36Sopenharmony_ci	return 0;
39462306a36Sopenharmony_ci}
39562306a36Sopenharmony_ci
39662306a36Sopenharmony_ci/*
39762306a36Sopenharmony_ci * il4965_rs_collect_tx_data - Update the success/failure sliding win
39862306a36Sopenharmony_ci *
39962306a36Sopenharmony_ci * We keep a sliding win of the last 62 packets transmitted
40062306a36Sopenharmony_ci * at this rate.  win->data contains the bitmask of successful
40162306a36Sopenharmony_ci * packets.
40262306a36Sopenharmony_ci */
40362306a36Sopenharmony_cistatic int
40462306a36Sopenharmony_ciil4965_rs_collect_tx_data(struct il_scale_tbl_info *tbl, int scale_idx,
40562306a36Sopenharmony_ci			  int attempts, int successes)
40662306a36Sopenharmony_ci{
40762306a36Sopenharmony_ci	struct il_rate_scale_data *win = NULL;
40862306a36Sopenharmony_ci	static const u64 mask = (((u64) 1) << (RATE_MAX_WINDOW - 1));
40962306a36Sopenharmony_ci	s32 fail_count, tpt;
41062306a36Sopenharmony_ci
41162306a36Sopenharmony_ci	if (scale_idx < 0 || scale_idx >= RATE_COUNT)
41262306a36Sopenharmony_ci		return -EINVAL;
41362306a36Sopenharmony_ci
41462306a36Sopenharmony_ci	/* Select win for current tx bit rate */
41562306a36Sopenharmony_ci	win = &(tbl->win[scale_idx]);
41662306a36Sopenharmony_ci
41762306a36Sopenharmony_ci	/* Get expected throughput */
41862306a36Sopenharmony_ci	tpt = il4965_get_expected_tpt(tbl, scale_idx);
41962306a36Sopenharmony_ci
42062306a36Sopenharmony_ci	/*
42162306a36Sopenharmony_ci	 * Keep track of only the latest 62 tx frame attempts in this rate's
42262306a36Sopenharmony_ci	 * history win; anything older isn't really relevant any more.
42362306a36Sopenharmony_ci	 * If we have filled up the sliding win, drop the oldest attempt;
42462306a36Sopenharmony_ci	 * if the oldest attempt (highest bit in bitmap) shows "success",
42562306a36Sopenharmony_ci	 * subtract "1" from the success counter (this is the main reason
42662306a36Sopenharmony_ci	 * we keep these bitmaps!).
42762306a36Sopenharmony_ci	 */
42862306a36Sopenharmony_ci	while (attempts > 0) {
42962306a36Sopenharmony_ci		if (win->counter >= RATE_MAX_WINDOW) {
43062306a36Sopenharmony_ci
43162306a36Sopenharmony_ci			/* remove earliest */
43262306a36Sopenharmony_ci			win->counter = RATE_MAX_WINDOW - 1;
43362306a36Sopenharmony_ci
43462306a36Sopenharmony_ci			if (win->data & mask) {
43562306a36Sopenharmony_ci				win->data &= ~mask;
43662306a36Sopenharmony_ci				win->success_counter--;
43762306a36Sopenharmony_ci			}
43862306a36Sopenharmony_ci		}
43962306a36Sopenharmony_ci
44062306a36Sopenharmony_ci		/* Increment frames-attempted counter */
44162306a36Sopenharmony_ci		win->counter++;
44262306a36Sopenharmony_ci
44362306a36Sopenharmony_ci		/* Shift bitmap by one frame to throw away oldest history */
44462306a36Sopenharmony_ci		win->data <<= 1;
44562306a36Sopenharmony_ci
44662306a36Sopenharmony_ci		/* Mark the most recent #successes attempts as successful */
44762306a36Sopenharmony_ci		if (successes > 0) {
44862306a36Sopenharmony_ci			win->success_counter++;
44962306a36Sopenharmony_ci			win->data |= 0x1;
45062306a36Sopenharmony_ci			successes--;
45162306a36Sopenharmony_ci		}
45262306a36Sopenharmony_ci
45362306a36Sopenharmony_ci		attempts--;
45462306a36Sopenharmony_ci	}
45562306a36Sopenharmony_ci
45662306a36Sopenharmony_ci	/* Calculate current success ratio, avoid divide-by-0! */
45762306a36Sopenharmony_ci	if (win->counter > 0)
45862306a36Sopenharmony_ci		win->success_ratio =
45962306a36Sopenharmony_ci		    128 * (100 * win->success_counter) / win->counter;
46062306a36Sopenharmony_ci	else
46162306a36Sopenharmony_ci		win->success_ratio = IL_INVALID_VALUE;
46262306a36Sopenharmony_ci
46362306a36Sopenharmony_ci	fail_count = win->counter - win->success_counter;
46462306a36Sopenharmony_ci
46562306a36Sopenharmony_ci	/* Calculate average throughput, if we have enough history. */
46662306a36Sopenharmony_ci	if (fail_count >= RATE_MIN_FAILURE_TH ||
46762306a36Sopenharmony_ci	    win->success_counter >= RATE_MIN_SUCCESS_TH)
46862306a36Sopenharmony_ci		win->average_tpt = (win->success_ratio * tpt + 64) / 128;
46962306a36Sopenharmony_ci	else
47062306a36Sopenharmony_ci		win->average_tpt = IL_INVALID_VALUE;
47162306a36Sopenharmony_ci
47262306a36Sopenharmony_ci	/* Tag this win as having been updated */
47362306a36Sopenharmony_ci	win->stamp = jiffies;
47462306a36Sopenharmony_ci
47562306a36Sopenharmony_ci	return 0;
47662306a36Sopenharmony_ci}
47762306a36Sopenharmony_ci
47862306a36Sopenharmony_ci/*
47962306a36Sopenharmony_ci * Fill uCode API rate_n_flags field, based on "search" or "active" table.
48062306a36Sopenharmony_ci */
48162306a36Sopenharmony_cistatic u32
48262306a36Sopenharmony_ciil4965_rate_n_flags_from_tbl(struct il_priv *il, struct il_scale_tbl_info *tbl,
48362306a36Sopenharmony_ci			     int idx, u8 use_green)
48462306a36Sopenharmony_ci{
48562306a36Sopenharmony_ci	u32 rate_n_flags = 0;
48662306a36Sopenharmony_ci
48762306a36Sopenharmony_ci	if (is_legacy(tbl->lq_type)) {
48862306a36Sopenharmony_ci		rate_n_flags = il_rates[idx].plcp;
48962306a36Sopenharmony_ci		if (idx >= IL_FIRST_CCK_RATE && idx <= IL_LAST_CCK_RATE)
49062306a36Sopenharmony_ci			rate_n_flags |= RATE_MCS_CCK_MSK;
49162306a36Sopenharmony_ci
49262306a36Sopenharmony_ci	} else if (is_Ht(tbl->lq_type)) {
49362306a36Sopenharmony_ci		if (idx > IL_LAST_OFDM_RATE) {
49462306a36Sopenharmony_ci			IL_ERR("Invalid HT rate idx %d\n", idx);
49562306a36Sopenharmony_ci			idx = IL_LAST_OFDM_RATE;
49662306a36Sopenharmony_ci		}
49762306a36Sopenharmony_ci		rate_n_flags = RATE_MCS_HT_MSK;
49862306a36Sopenharmony_ci
49962306a36Sopenharmony_ci		if (is_siso(tbl->lq_type))
50062306a36Sopenharmony_ci			rate_n_flags |= il_rates[idx].plcp_siso;
50162306a36Sopenharmony_ci		else
50262306a36Sopenharmony_ci			rate_n_flags |= il_rates[idx].plcp_mimo2;
50362306a36Sopenharmony_ci	} else {
50462306a36Sopenharmony_ci		IL_ERR("Invalid tbl->lq_type %d\n", tbl->lq_type);
50562306a36Sopenharmony_ci	}
50662306a36Sopenharmony_ci
50762306a36Sopenharmony_ci	rate_n_flags |=
50862306a36Sopenharmony_ci	    ((tbl->ant_type << RATE_MCS_ANT_POS) & RATE_MCS_ANT_ABC_MSK);
50962306a36Sopenharmony_ci
51062306a36Sopenharmony_ci	if (is_Ht(tbl->lq_type)) {
51162306a36Sopenharmony_ci		if (tbl->is_ht40) {
51262306a36Sopenharmony_ci			if (tbl->is_dup)
51362306a36Sopenharmony_ci				rate_n_flags |= RATE_MCS_DUP_MSK;
51462306a36Sopenharmony_ci			else
51562306a36Sopenharmony_ci				rate_n_flags |= RATE_MCS_HT40_MSK;
51662306a36Sopenharmony_ci		}
51762306a36Sopenharmony_ci		if (tbl->is_SGI)
51862306a36Sopenharmony_ci			rate_n_flags |= RATE_MCS_SGI_MSK;
51962306a36Sopenharmony_ci
52062306a36Sopenharmony_ci		if (use_green) {
52162306a36Sopenharmony_ci			rate_n_flags |= RATE_MCS_GF_MSK;
52262306a36Sopenharmony_ci			if (is_siso(tbl->lq_type) && tbl->is_SGI) {
52362306a36Sopenharmony_ci				rate_n_flags &= ~RATE_MCS_SGI_MSK;
52462306a36Sopenharmony_ci				IL_ERR("GF was set with SGI:SISO\n");
52562306a36Sopenharmony_ci			}
52662306a36Sopenharmony_ci		}
52762306a36Sopenharmony_ci	}
52862306a36Sopenharmony_ci	return rate_n_flags;
52962306a36Sopenharmony_ci}
53062306a36Sopenharmony_ci
53162306a36Sopenharmony_ci/*
53262306a36Sopenharmony_ci * Interpret uCode API's rate_n_flags format,
53362306a36Sopenharmony_ci * fill "search" or "active" tx mode table.
53462306a36Sopenharmony_ci */
53562306a36Sopenharmony_cistatic int
53662306a36Sopenharmony_ciil4965_rs_get_tbl_info_from_mcs(const u32 rate_n_flags,
53762306a36Sopenharmony_ci				enum nl80211_band band,
53862306a36Sopenharmony_ci				struct il_scale_tbl_info *tbl, int *rate_idx)
53962306a36Sopenharmony_ci{
54062306a36Sopenharmony_ci	u32 ant_msk = (rate_n_flags & RATE_MCS_ANT_ABC_MSK);
54162306a36Sopenharmony_ci	u8 il4965_num_of_ant =
54262306a36Sopenharmony_ci	    il4965_get_il4965_num_of_ant_from_rate(rate_n_flags);
54362306a36Sopenharmony_ci	u8 mcs;
54462306a36Sopenharmony_ci
54562306a36Sopenharmony_ci	memset(tbl, 0, sizeof(struct il_scale_tbl_info));
54662306a36Sopenharmony_ci	*rate_idx = il4965_hwrate_to_plcp_idx(rate_n_flags);
54762306a36Sopenharmony_ci
54862306a36Sopenharmony_ci	if (*rate_idx == RATE_INVALID) {
54962306a36Sopenharmony_ci		*rate_idx = -1;
55062306a36Sopenharmony_ci		return -EINVAL;
55162306a36Sopenharmony_ci	}
55262306a36Sopenharmony_ci	tbl->is_SGI = 0;	/* default legacy setup */
55362306a36Sopenharmony_ci	tbl->is_ht40 = 0;
55462306a36Sopenharmony_ci	tbl->is_dup = 0;
55562306a36Sopenharmony_ci	tbl->ant_type = (ant_msk >> RATE_MCS_ANT_POS);
55662306a36Sopenharmony_ci	tbl->lq_type = LQ_NONE;
55762306a36Sopenharmony_ci	tbl->max_search = IL_MAX_SEARCH;
55862306a36Sopenharmony_ci
55962306a36Sopenharmony_ci	/* legacy rate format */
56062306a36Sopenharmony_ci	if (!(rate_n_flags & RATE_MCS_HT_MSK)) {
56162306a36Sopenharmony_ci		if (il4965_num_of_ant == 1) {
56262306a36Sopenharmony_ci			if (band == NL80211_BAND_5GHZ)
56362306a36Sopenharmony_ci				tbl->lq_type = LQ_A;
56462306a36Sopenharmony_ci			else
56562306a36Sopenharmony_ci				tbl->lq_type = LQ_G;
56662306a36Sopenharmony_ci		}
56762306a36Sopenharmony_ci		/* HT rate format */
56862306a36Sopenharmony_ci	} else {
56962306a36Sopenharmony_ci		if (rate_n_flags & RATE_MCS_SGI_MSK)
57062306a36Sopenharmony_ci			tbl->is_SGI = 1;
57162306a36Sopenharmony_ci
57262306a36Sopenharmony_ci		if ((rate_n_flags & RATE_MCS_HT40_MSK) ||
57362306a36Sopenharmony_ci		    (rate_n_flags & RATE_MCS_DUP_MSK))
57462306a36Sopenharmony_ci			tbl->is_ht40 = 1;
57562306a36Sopenharmony_ci
57662306a36Sopenharmony_ci		if (rate_n_flags & RATE_MCS_DUP_MSK)
57762306a36Sopenharmony_ci			tbl->is_dup = 1;
57862306a36Sopenharmony_ci
57962306a36Sopenharmony_ci		mcs = il4965_rs_extract_rate(rate_n_flags);
58062306a36Sopenharmony_ci
58162306a36Sopenharmony_ci		/* SISO */
58262306a36Sopenharmony_ci		if (mcs <= RATE_SISO_60M_PLCP) {
58362306a36Sopenharmony_ci			if (il4965_num_of_ant == 1)
58462306a36Sopenharmony_ci				tbl->lq_type = LQ_SISO;	/*else NONE */
58562306a36Sopenharmony_ci			/* MIMO2 */
58662306a36Sopenharmony_ci		} else {
58762306a36Sopenharmony_ci			if (il4965_num_of_ant == 2)
58862306a36Sopenharmony_ci				tbl->lq_type = LQ_MIMO2;
58962306a36Sopenharmony_ci		}
59062306a36Sopenharmony_ci	}
59162306a36Sopenharmony_ci	return 0;
59262306a36Sopenharmony_ci}
59362306a36Sopenharmony_ci
59462306a36Sopenharmony_ci/* switch to another antenna/antennas and return 1 */
59562306a36Sopenharmony_ci/* if no other valid antenna found, return 0 */
59662306a36Sopenharmony_cistatic int
59762306a36Sopenharmony_ciil4965_rs_toggle_antenna(u32 valid_ant, u32 *rate_n_flags,
59862306a36Sopenharmony_ci			 struct il_scale_tbl_info *tbl)
59962306a36Sopenharmony_ci{
60062306a36Sopenharmony_ci	u8 new_ant_type;
60162306a36Sopenharmony_ci
60262306a36Sopenharmony_ci	if (!tbl->ant_type || tbl->ant_type > ANT_ABC)
60362306a36Sopenharmony_ci		return 0;
60462306a36Sopenharmony_ci
60562306a36Sopenharmony_ci	if (!il4965_rs_is_valid_ant(valid_ant, tbl->ant_type))
60662306a36Sopenharmony_ci		return 0;
60762306a36Sopenharmony_ci
60862306a36Sopenharmony_ci	new_ant_type = ant_toggle_lookup[tbl->ant_type];
60962306a36Sopenharmony_ci
61062306a36Sopenharmony_ci	while (new_ant_type != tbl->ant_type &&
61162306a36Sopenharmony_ci	       !il4965_rs_is_valid_ant(valid_ant, new_ant_type))
61262306a36Sopenharmony_ci		new_ant_type = ant_toggle_lookup[new_ant_type];
61362306a36Sopenharmony_ci
61462306a36Sopenharmony_ci	if (new_ant_type == tbl->ant_type)
61562306a36Sopenharmony_ci		return 0;
61662306a36Sopenharmony_ci
61762306a36Sopenharmony_ci	tbl->ant_type = new_ant_type;
61862306a36Sopenharmony_ci	*rate_n_flags &= ~RATE_MCS_ANT_ABC_MSK;
61962306a36Sopenharmony_ci	*rate_n_flags |= new_ant_type << RATE_MCS_ANT_POS;
62062306a36Sopenharmony_ci	return 1;
62162306a36Sopenharmony_ci}
62262306a36Sopenharmony_ci
62362306a36Sopenharmony_ci/*
62462306a36Sopenharmony_ci * Green-field mode is valid if the station supports it and
62562306a36Sopenharmony_ci * there are no non-GF stations present in the BSS.
62662306a36Sopenharmony_ci */
62762306a36Sopenharmony_cistatic bool
62862306a36Sopenharmony_ciil4965_rs_use_green(struct il_priv *il, struct ieee80211_sta *sta)
62962306a36Sopenharmony_ci{
63062306a36Sopenharmony_ci	return (sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_GRN_FLD) &&
63162306a36Sopenharmony_ci	       !il->ht.non_gf_sta_present;
63262306a36Sopenharmony_ci}
63362306a36Sopenharmony_ci
63462306a36Sopenharmony_ci/*
63562306a36Sopenharmony_ci * il4965_rs_get_supported_rates - get the available rates
63662306a36Sopenharmony_ci *
63762306a36Sopenharmony_ci * if management frame or broadcast frame only return
63862306a36Sopenharmony_ci * basic available rates.
63962306a36Sopenharmony_ci *
64062306a36Sopenharmony_ci */
64162306a36Sopenharmony_cistatic u16
64262306a36Sopenharmony_ciil4965_rs_get_supported_rates(struct il_lq_sta *lq_sta,
64362306a36Sopenharmony_ci			      struct ieee80211_hdr *hdr,
64462306a36Sopenharmony_ci			      enum il_table_type rate_type)
64562306a36Sopenharmony_ci{
64662306a36Sopenharmony_ci	if (is_legacy(rate_type)) {
64762306a36Sopenharmony_ci		return lq_sta->active_legacy_rate;
64862306a36Sopenharmony_ci	} else {
64962306a36Sopenharmony_ci		if (is_siso(rate_type))
65062306a36Sopenharmony_ci			return lq_sta->active_siso_rate;
65162306a36Sopenharmony_ci		else
65262306a36Sopenharmony_ci			return lq_sta->active_mimo2_rate;
65362306a36Sopenharmony_ci	}
65462306a36Sopenharmony_ci}
65562306a36Sopenharmony_ci
65662306a36Sopenharmony_cistatic u16
65762306a36Sopenharmony_ciil4965_rs_get_adjacent_rate(struct il_priv *il, u8 idx, u16 rate_mask,
65862306a36Sopenharmony_ci			    int rate_type)
65962306a36Sopenharmony_ci{
66062306a36Sopenharmony_ci	u8 high = RATE_INVALID;
66162306a36Sopenharmony_ci	u8 low = RATE_INVALID;
66262306a36Sopenharmony_ci
66362306a36Sopenharmony_ci	/* 802.11A or ht walks to the next literal adjacent rate in
66462306a36Sopenharmony_ci	 * the rate table */
66562306a36Sopenharmony_ci	if (is_a_band(rate_type) || !is_legacy(rate_type)) {
66662306a36Sopenharmony_ci		int i;
66762306a36Sopenharmony_ci		u32 mask;
66862306a36Sopenharmony_ci
66962306a36Sopenharmony_ci		/* Find the previous rate that is in the rate mask */
67062306a36Sopenharmony_ci		i = idx - 1;
67162306a36Sopenharmony_ci		for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
67262306a36Sopenharmony_ci			if (rate_mask & mask) {
67362306a36Sopenharmony_ci				low = i;
67462306a36Sopenharmony_ci				break;
67562306a36Sopenharmony_ci			}
67662306a36Sopenharmony_ci		}
67762306a36Sopenharmony_ci
67862306a36Sopenharmony_ci		/* Find the next rate that is in the rate mask */
67962306a36Sopenharmony_ci		i = idx + 1;
68062306a36Sopenharmony_ci		for (mask = (1 << i); i < RATE_COUNT; i++, mask <<= 1) {
68162306a36Sopenharmony_ci			if (rate_mask & mask) {
68262306a36Sopenharmony_ci				high = i;
68362306a36Sopenharmony_ci				break;
68462306a36Sopenharmony_ci			}
68562306a36Sopenharmony_ci		}
68662306a36Sopenharmony_ci
68762306a36Sopenharmony_ci		return (high << 8) | low;
68862306a36Sopenharmony_ci	}
68962306a36Sopenharmony_ci
69062306a36Sopenharmony_ci	low = idx;
69162306a36Sopenharmony_ci	while (low != RATE_INVALID) {
69262306a36Sopenharmony_ci		low = il_rates[low].prev_rs;
69362306a36Sopenharmony_ci		if (low == RATE_INVALID)
69462306a36Sopenharmony_ci			break;
69562306a36Sopenharmony_ci		if (rate_mask & (1 << low))
69662306a36Sopenharmony_ci			break;
69762306a36Sopenharmony_ci		D_RATE("Skipping masked lower rate: %d\n", low);
69862306a36Sopenharmony_ci	}
69962306a36Sopenharmony_ci
70062306a36Sopenharmony_ci	high = idx;
70162306a36Sopenharmony_ci	while (high != RATE_INVALID) {
70262306a36Sopenharmony_ci		high = il_rates[high].next_rs;
70362306a36Sopenharmony_ci		if (high == RATE_INVALID)
70462306a36Sopenharmony_ci			break;
70562306a36Sopenharmony_ci		if (rate_mask & (1 << high))
70662306a36Sopenharmony_ci			break;
70762306a36Sopenharmony_ci		D_RATE("Skipping masked higher rate: %d\n", high);
70862306a36Sopenharmony_ci	}
70962306a36Sopenharmony_ci
71062306a36Sopenharmony_ci	return (high << 8) | low;
71162306a36Sopenharmony_ci}
71262306a36Sopenharmony_ci
71362306a36Sopenharmony_cistatic u32
71462306a36Sopenharmony_ciil4965_rs_get_lower_rate(struct il_lq_sta *lq_sta,
71562306a36Sopenharmony_ci			 struct il_scale_tbl_info *tbl, u8 scale_idx,
71662306a36Sopenharmony_ci			 u8 ht_possible)
71762306a36Sopenharmony_ci{
71862306a36Sopenharmony_ci	s32 low;
71962306a36Sopenharmony_ci	u16 rate_mask;
72062306a36Sopenharmony_ci	u16 high_low;
72162306a36Sopenharmony_ci	u8 switch_to_legacy = 0;
72262306a36Sopenharmony_ci	u8 is_green = lq_sta->is_green;
72362306a36Sopenharmony_ci	struct il_priv *il = lq_sta->drv;
72462306a36Sopenharmony_ci
72562306a36Sopenharmony_ci	/* check if we need to switch from HT to legacy rates.
72662306a36Sopenharmony_ci	 * assumption is that mandatory rates (1Mbps or 6Mbps)
72762306a36Sopenharmony_ci	 * are always supported (spec demand) */
72862306a36Sopenharmony_ci	if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_idx)) {
72962306a36Sopenharmony_ci		switch_to_legacy = 1;
73062306a36Sopenharmony_ci		scale_idx = rs_ht_to_legacy[scale_idx];
73162306a36Sopenharmony_ci		if (lq_sta->band == NL80211_BAND_5GHZ)
73262306a36Sopenharmony_ci			tbl->lq_type = LQ_A;
73362306a36Sopenharmony_ci		else
73462306a36Sopenharmony_ci			tbl->lq_type = LQ_G;
73562306a36Sopenharmony_ci
73662306a36Sopenharmony_ci		if (il4965_num_of_ant(tbl->ant_type) > 1)
73762306a36Sopenharmony_ci			tbl->ant_type =
73862306a36Sopenharmony_ci			    il4965_first_antenna(il->hw_params.valid_tx_ant);
73962306a36Sopenharmony_ci
74062306a36Sopenharmony_ci		tbl->is_ht40 = 0;
74162306a36Sopenharmony_ci		tbl->is_SGI = 0;
74262306a36Sopenharmony_ci		tbl->max_search = IL_MAX_SEARCH;
74362306a36Sopenharmony_ci	}
74462306a36Sopenharmony_ci
74562306a36Sopenharmony_ci	rate_mask = il4965_rs_get_supported_rates(lq_sta, NULL, tbl->lq_type);
74662306a36Sopenharmony_ci
74762306a36Sopenharmony_ci	/* Mask with station rate restriction */
74862306a36Sopenharmony_ci	if (is_legacy(tbl->lq_type)) {
74962306a36Sopenharmony_ci		/* supp_rates has no CCK bits in A mode */
75062306a36Sopenharmony_ci		if (lq_sta->band == NL80211_BAND_5GHZ)
75162306a36Sopenharmony_ci			rate_mask =
75262306a36Sopenharmony_ci			    (u16) (rate_mask &
75362306a36Sopenharmony_ci				   (lq_sta->supp_rates << IL_FIRST_OFDM_RATE));
75462306a36Sopenharmony_ci		else
75562306a36Sopenharmony_ci			rate_mask = (u16) (rate_mask & lq_sta->supp_rates);
75662306a36Sopenharmony_ci	}
75762306a36Sopenharmony_ci
75862306a36Sopenharmony_ci	/* If we switched from HT to legacy, check current rate */
75962306a36Sopenharmony_ci	if (switch_to_legacy && (rate_mask & (1 << scale_idx))) {
76062306a36Sopenharmony_ci		low = scale_idx;
76162306a36Sopenharmony_ci		goto out;
76262306a36Sopenharmony_ci	}
76362306a36Sopenharmony_ci
76462306a36Sopenharmony_ci	high_low =
76562306a36Sopenharmony_ci	    il4965_rs_get_adjacent_rate(lq_sta->drv, scale_idx, rate_mask,
76662306a36Sopenharmony_ci					tbl->lq_type);
76762306a36Sopenharmony_ci	low = high_low & 0xff;
76862306a36Sopenharmony_ci
76962306a36Sopenharmony_ci	if (low == RATE_INVALID)
77062306a36Sopenharmony_ci		low = scale_idx;
77162306a36Sopenharmony_ci
77262306a36Sopenharmony_ciout:
77362306a36Sopenharmony_ci	return il4965_rate_n_flags_from_tbl(lq_sta->drv, tbl, low, is_green);
77462306a36Sopenharmony_ci}
77562306a36Sopenharmony_ci
77662306a36Sopenharmony_ci/*
77762306a36Sopenharmony_ci * Simple function to compare two rate scale table types
77862306a36Sopenharmony_ci */
77962306a36Sopenharmony_cistatic bool
78062306a36Sopenharmony_ciil4965_table_type_matches(struct il_scale_tbl_info *a,
78162306a36Sopenharmony_ci			  struct il_scale_tbl_info *b)
78262306a36Sopenharmony_ci{
78362306a36Sopenharmony_ci	return (a->lq_type == b->lq_type && a->ant_type == b->ant_type &&
78462306a36Sopenharmony_ci		a->is_SGI == b->is_SGI);
78562306a36Sopenharmony_ci}
78662306a36Sopenharmony_ci
78762306a36Sopenharmony_ci/*
78862306a36Sopenharmony_ci * mac80211 sends us Tx status
78962306a36Sopenharmony_ci */
79062306a36Sopenharmony_cistatic void
79162306a36Sopenharmony_ciil4965_rs_tx_status(void *il_r, struct ieee80211_supported_band *sband,
79262306a36Sopenharmony_ci		    struct ieee80211_sta *sta, void *il_sta,
79362306a36Sopenharmony_ci		    struct sk_buff *skb)
79462306a36Sopenharmony_ci{
79562306a36Sopenharmony_ci	int legacy_success;
79662306a36Sopenharmony_ci	int retries;
79762306a36Sopenharmony_ci	int rs_idx, mac_idx, i;
79862306a36Sopenharmony_ci	struct il_lq_sta *lq_sta = il_sta;
79962306a36Sopenharmony_ci	struct il_link_quality_cmd *table;
80062306a36Sopenharmony_ci	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
80162306a36Sopenharmony_ci	struct il_priv *il = (struct il_priv *)il_r;
80262306a36Sopenharmony_ci	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
80362306a36Sopenharmony_ci	enum mac80211_rate_control_flags mac_flags;
80462306a36Sopenharmony_ci	u32 tx_rate;
80562306a36Sopenharmony_ci	struct il_scale_tbl_info tbl_type;
80662306a36Sopenharmony_ci	struct il_scale_tbl_info *curr_tbl, *other_tbl, *tmp_tbl;
80762306a36Sopenharmony_ci
80862306a36Sopenharmony_ci	D_RATE("get frame ack response, update rate scale win\n");
80962306a36Sopenharmony_ci
81062306a36Sopenharmony_ci	/* Treat uninitialized rate scaling data same as non-existing. */
81162306a36Sopenharmony_ci	if (!lq_sta) {
81262306a36Sopenharmony_ci		D_RATE("Station rate scaling not created yet.\n");
81362306a36Sopenharmony_ci		return;
81462306a36Sopenharmony_ci	} else if (!lq_sta->drv) {
81562306a36Sopenharmony_ci		D_RATE("Rate scaling not initialized yet.\n");
81662306a36Sopenharmony_ci		return;
81762306a36Sopenharmony_ci	}
81862306a36Sopenharmony_ci
81962306a36Sopenharmony_ci	if (!ieee80211_is_data(hdr->frame_control) ||
82062306a36Sopenharmony_ci	    (info->flags & IEEE80211_TX_CTL_NO_ACK))
82162306a36Sopenharmony_ci		return;
82262306a36Sopenharmony_ci
82362306a36Sopenharmony_ci	/* This packet was aggregated but doesn't carry status info */
82462306a36Sopenharmony_ci	if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
82562306a36Sopenharmony_ci	    !(info->flags & IEEE80211_TX_STAT_AMPDU))
82662306a36Sopenharmony_ci		return;
82762306a36Sopenharmony_ci
82862306a36Sopenharmony_ci	/*
82962306a36Sopenharmony_ci	 * Ignore this Tx frame response if its initial rate doesn't match
83062306a36Sopenharmony_ci	 * that of latest Link Quality command.  There may be stragglers
83162306a36Sopenharmony_ci	 * from a previous Link Quality command, but we're no longer interested
83262306a36Sopenharmony_ci	 * in those; they're either from the "active" mode while we're trying
83362306a36Sopenharmony_ci	 * to check "search" mode, or a prior "search" mode after we've moved
83462306a36Sopenharmony_ci	 * to a new "search" mode (which might become the new "active" mode).
83562306a36Sopenharmony_ci	 */
83662306a36Sopenharmony_ci	table = &lq_sta->lq;
83762306a36Sopenharmony_ci	tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
83862306a36Sopenharmony_ci	il4965_rs_get_tbl_info_from_mcs(tx_rate, il->band, &tbl_type, &rs_idx);
83962306a36Sopenharmony_ci	if (il->band == NL80211_BAND_5GHZ)
84062306a36Sopenharmony_ci		rs_idx -= IL_FIRST_OFDM_RATE;
84162306a36Sopenharmony_ci	mac_flags = info->status.rates[0].flags;
84262306a36Sopenharmony_ci	mac_idx = info->status.rates[0].idx;
84362306a36Sopenharmony_ci	/* For HT packets, map MCS to PLCP */
84462306a36Sopenharmony_ci	if (mac_flags & IEEE80211_TX_RC_MCS) {
84562306a36Sopenharmony_ci		mac_idx &= RATE_MCS_CODE_MSK;	/* Remove # of streams */
84662306a36Sopenharmony_ci		if (mac_idx >= (RATE_9M_IDX - IL_FIRST_OFDM_RATE))
84762306a36Sopenharmony_ci			mac_idx++;
84862306a36Sopenharmony_ci		/*
84962306a36Sopenharmony_ci		 * mac80211 HT idx is always zero-idxed; we need to move
85062306a36Sopenharmony_ci		 * HT OFDM rates after CCK rates in 2.4 GHz band
85162306a36Sopenharmony_ci		 */
85262306a36Sopenharmony_ci		if (il->band == NL80211_BAND_2GHZ)
85362306a36Sopenharmony_ci			mac_idx += IL_FIRST_OFDM_RATE;
85462306a36Sopenharmony_ci	}
85562306a36Sopenharmony_ci	/* Here we actually compare this rate to the latest LQ command */
85662306a36Sopenharmony_ci	if (mac_idx < 0 ||
85762306a36Sopenharmony_ci	    tbl_type.is_SGI != !!(mac_flags & IEEE80211_TX_RC_SHORT_GI) ||
85862306a36Sopenharmony_ci	    tbl_type.is_ht40 != !!(mac_flags & IEEE80211_TX_RC_40_MHZ_WIDTH) ||
85962306a36Sopenharmony_ci	    tbl_type.is_dup != !!(mac_flags & IEEE80211_TX_RC_DUP_DATA) ||
86062306a36Sopenharmony_ci	    tbl_type.ant_type != info->status.antenna ||
86162306a36Sopenharmony_ci	    !!(tx_rate & RATE_MCS_HT_MSK) != !!(mac_flags & IEEE80211_TX_RC_MCS)
86262306a36Sopenharmony_ci	    || !!(tx_rate & RATE_MCS_GF_MSK) !=
86362306a36Sopenharmony_ci	    !!(mac_flags & IEEE80211_TX_RC_GREEN_FIELD) || rs_idx != mac_idx) {
86462306a36Sopenharmony_ci		D_RATE("initial rate %d does not match %d (0x%x)\n", mac_idx,
86562306a36Sopenharmony_ci		       rs_idx, tx_rate);
86662306a36Sopenharmony_ci		/*
86762306a36Sopenharmony_ci		 * Since rates mis-match, the last LQ command may have failed.
86862306a36Sopenharmony_ci		 * After IL_MISSED_RATE_MAX mis-matches, resync the uCode with
86962306a36Sopenharmony_ci		 * ... driver.
87062306a36Sopenharmony_ci		 */
87162306a36Sopenharmony_ci		lq_sta->missed_rate_counter++;
87262306a36Sopenharmony_ci		if (lq_sta->missed_rate_counter > IL_MISSED_RATE_MAX) {
87362306a36Sopenharmony_ci			lq_sta->missed_rate_counter = 0;
87462306a36Sopenharmony_ci			il_send_lq_cmd(il, &lq_sta->lq, CMD_ASYNC, false);
87562306a36Sopenharmony_ci		}
87662306a36Sopenharmony_ci		/* Regardless, ignore this status info for outdated rate */
87762306a36Sopenharmony_ci		return;
87862306a36Sopenharmony_ci	} else
87962306a36Sopenharmony_ci		/* Rate did match, so reset the missed_rate_counter */
88062306a36Sopenharmony_ci		lq_sta->missed_rate_counter = 0;
88162306a36Sopenharmony_ci
88262306a36Sopenharmony_ci	/* Figure out if rate scale algorithm is in active or search table */
88362306a36Sopenharmony_ci	if (il4965_table_type_matches
88462306a36Sopenharmony_ci	    (&tbl_type, &(lq_sta->lq_info[lq_sta->active_tbl]))) {
88562306a36Sopenharmony_ci		curr_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
88662306a36Sopenharmony_ci		other_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
88762306a36Sopenharmony_ci	} else
88862306a36Sopenharmony_ci	    if (il4965_table_type_matches
88962306a36Sopenharmony_ci		(&tbl_type, &lq_sta->lq_info[1 - lq_sta->active_tbl])) {
89062306a36Sopenharmony_ci		curr_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
89162306a36Sopenharmony_ci		other_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
89262306a36Sopenharmony_ci	} else {
89362306a36Sopenharmony_ci		D_RATE("Neither active nor search matches tx rate\n");
89462306a36Sopenharmony_ci		tmp_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
89562306a36Sopenharmony_ci		D_RATE("active- lq:%x, ant:%x, SGI:%d\n", tmp_tbl->lq_type,
89662306a36Sopenharmony_ci		       tmp_tbl->ant_type, tmp_tbl->is_SGI);
89762306a36Sopenharmony_ci		tmp_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
89862306a36Sopenharmony_ci		D_RATE("search- lq:%x, ant:%x, SGI:%d\n", tmp_tbl->lq_type,
89962306a36Sopenharmony_ci		       tmp_tbl->ant_type, tmp_tbl->is_SGI);
90062306a36Sopenharmony_ci		D_RATE("actual- lq:%x, ant:%x, SGI:%d\n", tbl_type.lq_type,
90162306a36Sopenharmony_ci		       tbl_type.ant_type, tbl_type.is_SGI);
90262306a36Sopenharmony_ci		/*
90362306a36Sopenharmony_ci		 * no matching table found, let's by-pass the data collection
90462306a36Sopenharmony_ci		 * and continue to perform rate scale to find the rate table
90562306a36Sopenharmony_ci		 */
90662306a36Sopenharmony_ci		il4965_rs_stay_in_table(lq_sta, true);
90762306a36Sopenharmony_ci		goto done;
90862306a36Sopenharmony_ci	}
90962306a36Sopenharmony_ci
91062306a36Sopenharmony_ci	/*
91162306a36Sopenharmony_ci	 * Updating the frame history depends on whether packets were
91262306a36Sopenharmony_ci	 * aggregated.
91362306a36Sopenharmony_ci	 *
91462306a36Sopenharmony_ci	 * For aggregation, all packets were transmitted at the same rate, the
91562306a36Sopenharmony_ci	 * first idx into rate scale table.
91662306a36Sopenharmony_ci	 */
91762306a36Sopenharmony_ci	if (info->flags & IEEE80211_TX_STAT_AMPDU) {
91862306a36Sopenharmony_ci		tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
91962306a36Sopenharmony_ci		il4965_rs_get_tbl_info_from_mcs(tx_rate, il->band, &tbl_type,
92062306a36Sopenharmony_ci						&rs_idx);
92162306a36Sopenharmony_ci		il4965_rs_collect_tx_data(curr_tbl, rs_idx,
92262306a36Sopenharmony_ci					  info->status.ampdu_len,
92362306a36Sopenharmony_ci					  info->status.ampdu_ack_len);
92462306a36Sopenharmony_ci
92562306a36Sopenharmony_ci		/* Update success/fail counts if not searching for new mode */
92662306a36Sopenharmony_ci		if (lq_sta->stay_in_tbl) {
92762306a36Sopenharmony_ci			lq_sta->total_success += info->status.ampdu_ack_len;
92862306a36Sopenharmony_ci			lq_sta->total_failed +=
92962306a36Sopenharmony_ci			    (info->status.ampdu_len -
93062306a36Sopenharmony_ci			     info->status.ampdu_ack_len);
93162306a36Sopenharmony_ci		}
93262306a36Sopenharmony_ci	} else {
93362306a36Sopenharmony_ci		/*
93462306a36Sopenharmony_ci		 * For legacy, update frame history with for each Tx retry.
93562306a36Sopenharmony_ci		 */
93662306a36Sopenharmony_ci		retries = info->status.rates[0].count - 1;
93762306a36Sopenharmony_ci		/* HW doesn't send more than 15 retries */
93862306a36Sopenharmony_ci		retries = min(retries, 15);
93962306a36Sopenharmony_ci
94062306a36Sopenharmony_ci		/* The last transmission may have been successful */
94162306a36Sopenharmony_ci		legacy_success = !!(info->flags & IEEE80211_TX_STAT_ACK);
94262306a36Sopenharmony_ci		/* Collect data for each rate used during failed TX attempts */
94362306a36Sopenharmony_ci		for (i = 0; i <= retries; ++i) {
94462306a36Sopenharmony_ci			tx_rate = le32_to_cpu(table->rs_table[i].rate_n_flags);
94562306a36Sopenharmony_ci			il4965_rs_get_tbl_info_from_mcs(tx_rate, il->band,
94662306a36Sopenharmony_ci							&tbl_type, &rs_idx);
94762306a36Sopenharmony_ci			/*
94862306a36Sopenharmony_ci			 * Only collect stats if retried rate is in the same RS
94962306a36Sopenharmony_ci			 * table as active/search.
95062306a36Sopenharmony_ci			 */
95162306a36Sopenharmony_ci			if (il4965_table_type_matches(&tbl_type, curr_tbl))
95262306a36Sopenharmony_ci				tmp_tbl = curr_tbl;
95362306a36Sopenharmony_ci			else if (il4965_table_type_matches
95462306a36Sopenharmony_ci				 (&tbl_type, other_tbl))
95562306a36Sopenharmony_ci				tmp_tbl = other_tbl;
95662306a36Sopenharmony_ci			else
95762306a36Sopenharmony_ci				continue;
95862306a36Sopenharmony_ci			il4965_rs_collect_tx_data(tmp_tbl, rs_idx, 1,
95962306a36Sopenharmony_ci						  i <
96062306a36Sopenharmony_ci						  retries ? 0 : legacy_success);
96162306a36Sopenharmony_ci		}
96262306a36Sopenharmony_ci
96362306a36Sopenharmony_ci		/* Update success/fail counts if not searching for new mode */
96462306a36Sopenharmony_ci		if (lq_sta->stay_in_tbl) {
96562306a36Sopenharmony_ci			lq_sta->total_success += legacy_success;
96662306a36Sopenharmony_ci			lq_sta->total_failed += retries + (1 - legacy_success);
96762306a36Sopenharmony_ci		}
96862306a36Sopenharmony_ci	}
96962306a36Sopenharmony_ci	/* The last TX rate is cached in lq_sta; it's set in if/else above */
97062306a36Sopenharmony_ci	lq_sta->last_rate_n_flags = tx_rate;
97162306a36Sopenharmony_cidone:
97262306a36Sopenharmony_ci	/* See if there's a better rate or modulation mode to try. */
97362306a36Sopenharmony_ci	if (sta->deflink.supp_rates[sband->band])
97462306a36Sopenharmony_ci		il4965_rs_rate_scale_perform(il, skb, sta, lq_sta);
97562306a36Sopenharmony_ci}
97662306a36Sopenharmony_ci
97762306a36Sopenharmony_ci/*
97862306a36Sopenharmony_ci * Begin a period of staying with a selected modulation mode.
97962306a36Sopenharmony_ci * Set "stay_in_tbl" flag to prevent any mode switches.
98062306a36Sopenharmony_ci * Set frame tx success limits according to legacy vs. high-throughput,
98162306a36Sopenharmony_ci * and reset overall (spanning all rates) tx success history stats.
98262306a36Sopenharmony_ci * These control how long we stay using same modulation mode before
98362306a36Sopenharmony_ci * searching for a new mode.
98462306a36Sopenharmony_ci */
98562306a36Sopenharmony_cistatic void
98662306a36Sopenharmony_ciil4965_rs_set_stay_in_table(struct il_priv *il, u8 is_legacy,
98762306a36Sopenharmony_ci			    struct il_lq_sta *lq_sta)
98862306a36Sopenharmony_ci{
98962306a36Sopenharmony_ci	D_RATE("we are staying in the same table\n");
99062306a36Sopenharmony_ci	lq_sta->stay_in_tbl = 1;	/* only place this gets set */
99162306a36Sopenharmony_ci	if (is_legacy) {
99262306a36Sopenharmony_ci		lq_sta->table_count_limit = IL_LEGACY_TBL_COUNT;
99362306a36Sopenharmony_ci		lq_sta->max_failure_limit = IL_LEGACY_FAILURE_LIMIT;
99462306a36Sopenharmony_ci		lq_sta->max_success_limit = IL_LEGACY_SUCCESS_LIMIT;
99562306a36Sopenharmony_ci	} else {
99662306a36Sopenharmony_ci		lq_sta->table_count_limit = IL_NONE_LEGACY_TBL_COUNT;
99762306a36Sopenharmony_ci		lq_sta->max_failure_limit = IL_NONE_LEGACY_FAILURE_LIMIT;
99862306a36Sopenharmony_ci		lq_sta->max_success_limit = IL_NONE_LEGACY_SUCCESS_LIMIT;
99962306a36Sopenharmony_ci	}
100062306a36Sopenharmony_ci	lq_sta->table_count = 0;
100162306a36Sopenharmony_ci	lq_sta->total_failed = 0;
100262306a36Sopenharmony_ci	lq_sta->total_success = 0;
100362306a36Sopenharmony_ci	lq_sta->flush_timer = jiffies;
100462306a36Sopenharmony_ci	lq_sta->action_counter = 0;
100562306a36Sopenharmony_ci}
100662306a36Sopenharmony_ci
100762306a36Sopenharmony_ci/*
100862306a36Sopenharmony_ci * Find correct throughput table for given mode of modulation
100962306a36Sopenharmony_ci */
101062306a36Sopenharmony_cistatic void
101162306a36Sopenharmony_ciil4965_rs_set_expected_tpt_table(struct il_lq_sta *lq_sta,
101262306a36Sopenharmony_ci				 struct il_scale_tbl_info *tbl)
101362306a36Sopenharmony_ci{
101462306a36Sopenharmony_ci	/* Used to choose among HT tables */
101562306a36Sopenharmony_ci	s32(*ht_tbl_pointer)[RATE_COUNT];
101662306a36Sopenharmony_ci
101762306a36Sopenharmony_ci	/* Check for invalid LQ type */
101862306a36Sopenharmony_ci	if (WARN_ON_ONCE(!is_legacy(tbl->lq_type) && !is_Ht(tbl->lq_type))) {
101962306a36Sopenharmony_ci		tbl->expected_tpt = expected_tpt_legacy;
102062306a36Sopenharmony_ci		return;
102162306a36Sopenharmony_ci	}
102262306a36Sopenharmony_ci
102362306a36Sopenharmony_ci	/* Legacy rates have only one table */
102462306a36Sopenharmony_ci	if (is_legacy(tbl->lq_type)) {
102562306a36Sopenharmony_ci		tbl->expected_tpt = expected_tpt_legacy;
102662306a36Sopenharmony_ci		return;
102762306a36Sopenharmony_ci	}
102862306a36Sopenharmony_ci
102962306a36Sopenharmony_ci	/* Choose among many HT tables depending on number of streams
103062306a36Sopenharmony_ci	 * (SISO/MIMO2), channel width (20/40), SGI, and aggregation
103162306a36Sopenharmony_ci	 * status */
103262306a36Sopenharmony_ci	if (is_siso(tbl->lq_type) && (!tbl->is_ht40 || lq_sta->is_dup))
103362306a36Sopenharmony_ci		ht_tbl_pointer = expected_tpt_siso20MHz;
103462306a36Sopenharmony_ci	else if (is_siso(tbl->lq_type))
103562306a36Sopenharmony_ci		ht_tbl_pointer = expected_tpt_siso40MHz;
103662306a36Sopenharmony_ci	else if (is_mimo2(tbl->lq_type) && (!tbl->is_ht40 || lq_sta->is_dup))
103762306a36Sopenharmony_ci		ht_tbl_pointer = expected_tpt_mimo2_20MHz;
103862306a36Sopenharmony_ci	else			/* if (is_mimo2(tbl->lq_type)) <-- must be true */
103962306a36Sopenharmony_ci		ht_tbl_pointer = expected_tpt_mimo2_40MHz;
104062306a36Sopenharmony_ci
104162306a36Sopenharmony_ci	if (!tbl->is_SGI && !lq_sta->is_agg)	/* Normal */
104262306a36Sopenharmony_ci		tbl->expected_tpt = ht_tbl_pointer[0];
104362306a36Sopenharmony_ci	else if (tbl->is_SGI && !lq_sta->is_agg)	/* SGI */
104462306a36Sopenharmony_ci		tbl->expected_tpt = ht_tbl_pointer[1];
104562306a36Sopenharmony_ci	else if (!tbl->is_SGI && lq_sta->is_agg)	/* AGG */
104662306a36Sopenharmony_ci		tbl->expected_tpt = ht_tbl_pointer[2];
104762306a36Sopenharmony_ci	else			/* AGG+SGI */
104862306a36Sopenharmony_ci		tbl->expected_tpt = ht_tbl_pointer[3];
104962306a36Sopenharmony_ci}
105062306a36Sopenharmony_ci
105162306a36Sopenharmony_ci/*
105262306a36Sopenharmony_ci * Find starting rate for new "search" high-throughput mode of modulation.
105362306a36Sopenharmony_ci * Goal is to find lowest expected rate (under perfect conditions) that is
105462306a36Sopenharmony_ci * above the current measured throughput of "active" mode, to give new mode
105562306a36Sopenharmony_ci * a fair chance to prove itself without too many challenges.
105662306a36Sopenharmony_ci *
105762306a36Sopenharmony_ci * This gets called when transitioning to more aggressive modulation
105862306a36Sopenharmony_ci * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
105962306a36Sopenharmony_ci * (i.e. MIMO to SISO).  When moving to MIMO, bit rate will typically need
106062306a36Sopenharmony_ci * to decrease to match "active" throughput.  When moving from MIMO to SISO,
106162306a36Sopenharmony_ci * bit rate will typically need to increase, but not if performance was bad.
106262306a36Sopenharmony_ci */
106362306a36Sopenharmony_cistatic s32
106462306a36Sopenharmony_ciil4965_rs_get_best_rate(struct il_priv *il, struct il_lq_sta *lq_sta,
106562306a36Sopenharmony_ci			struct il_scale_tbl_info *tbl,	/* "search" */
106662306a36Sopenharmony_ci			u16 rate_mask, s8 idx)
106762306a36Sopenharmony_ci{
106862306a36Sopenharmony_ci	/* "active" values */
106962306a36Sopenharmony_ci	struct il_scale_tbl_info *active_tbl =
107062306a36Sopenharmony_ci	    &(lq_sta->lq_info[lq_sta->active_tbl]);
107162306a36Sopenharmony_ci	s32 active_sr = active_tbl->win[idx].success_ratio;
107262306a36Sopenharmony_ci	s32 active_tpt = active_tbl->expected_tpt[idx];
107362306a36Sopenharmony_ci
107462306a36Sopenharmony_ci	/* expected "search" throughput */
107562306a36Sopenharmony_ci	s32 *tpt_tbl = tbl->expected_tpt;
107662306a36Sopenharmony_ci
107762306a36Sopenharmony_ci	s32 new_rate, high, low, start_hi;
107862306a36Sopenharmony_ci	u16 high_low;
107962306a36Sopenharmony_ci	s8 rate = idx;
108062306a36Sopenharmony_ci
108162306a36Sopenharmony_ci	new_rate = high = low = start_hi = RATE_INVALID;
108262306a36Sopenharmony_ci
108362306a36Sopenharmony_ci	for (;;) {
108462306a36Sopenharmony_ci		high_low =
108562306a36Sopenharmony_ci		    il4965_rs_get_adjacent_rate(il, rate, rate_mask,
108662306a36Sopenharmony_ci						tbl->lq_type);
108762306a36Sopenharmony_ci
108862306a36Sopenharmony_ci		low = high_low & 0xff;
108962306a36Sopenharmony_ci		high = (high_low >> 8) & 0xff;
109062306a36Sopenharmony_ci
109162306a36Sopenharmony_ci		/*
109262306a36Sopenharmony_ci		 * Lower the "search" bit rate, to give new "search" mode
109362306a36Sopenharmony_ci		 * approximately the same throughput as "active" if:
109462306a36Sopenharmony_ci		 *
109562306a36Sopenharmony_ci		 * 1) "Active" mode has been working modestly well (but not
109662306a36Sopenharmony_ci		 *    great), and expected "search" throughput (under perfect
109762306a36Sopenharmony_ci		 *    conditions) at candidate rate is above the actual
109862306a36Sopenharmony_ci		 *    measured "active" throughput (but less than expected
109962306a36Sopenharmony_ci		 *    "active" throughput under perfect conditions).
110062306a36Sopenharmony_ci		 * OR
110162306a36Sopenharmony_ci		 * 2) "Active" mode has been working perfectly or very well
110262306a36Sopenharmony_ci		 *    and expected "search" throughput (under perfect
110362306a36Sopenharmony_ci		 *    conditions) at candidate rate is above expected
110462306a36Sopenharmony_ci		 *    "active" throughput (under perfect conditions).
110562306a36Sopenharmony_ci		 */
110662306a36Sopenharmony_ci		if ((100 * tpt_tbl[rate] > lq_sta->last_tpt &&
110762306a36Sopenharmony_ci		     (active_sr > RATE_DECREASE_TH && active_sr <= RATE_HIGH_TH
110862306a36Sopenharmony_ci		      && tpt_tbl[rate] <= active_tpt)) ||
110962306a36Sopenharmony_ci		    (active_sr >= RATE_SCALE_SWITCH &&
111062306a36Sopenharmony_ci		     tpt_tbl[rate] > active_tpt)) {
111162306a36Sopenharmony_ci
111262306a36Sopenharmony_ci			/* (2nd or later pass)
111362306a36Sopenharmony_ci			 * If we've already tried to raise the rate, and are
111462306a36Sopenharmony_ci			 * now trying to lower it, use the higher rate. */
111562306a36Sopenharmony_ci			if (start_hi != RATE_INVALID) {
111662306a36Sopenharmony_ci				new_rate = start_hi;
111762306a36Sopenharmony_ci				break;
111862306a36Sopenharmony_ci			}
111962306a36Sopenharmony_ci
112062306a36Sopenharmony_ci			new_rate = rate;
112162306a36Sopenharmony_ci
112262306a36Sopenharmony_ci			/* Loop again with lower rate */
112362306a36Sopenharmony_ci			if (low != RATE_INVALID)
112462306a36Sopenharmony_ci				rate = low;
112562306a36Sopenharmony_ci
112662306a36Sopenharmony_ci			/* Lower rate not available, use the original */
112762306a36Sopenharmony_ci			else
112862306a36Sopenharmony_ci				break;
112962306a36Sopenharmony_ci
113062306a36Sopenharmony_ci			/* Else try to raise the "search" rate to match "active" */
113162306a36Sopenharmony_ci		} else {
113262306a36Sopenharmony_ci			/* (2nd or later pass)
113362306a36Sopenharmony_ci			 * If we've already tried to lower the rate, and are
113462306a36Sopenharmony_ci			 * now trying to raise it, use the lower rate. */
113562306a36Sopenharmony_ci			if (new_rate != RATE_INVALID)
113662306a36Sopenharmony_ci				break;
113762306a36Sopenharmony_ci
113862306a36Sopenharmony_ci			/* Loop again with higher rate */
113962306a36Sopenharmony_ci			else if (high != RATE_INVALID) {
114062306a36Sopenharmony_ci				start_hi = high;
114162306a36Sopenharmony_ci				rate = high;
114262306a36Sopenharmony_ci
114362306a36Sopenharmony_ci				/* Higher rate not available, use the original */
114462306a36Sopenharmony_ci			} else {
114562306a36Sopenharmony_ci				new_rate = rate;
114662306a36Sopenharmony_ci				break;
114762306a36Sopenharmony_ci			}
114862306a36Sopenharmony_ci		}
114962306a36Sopenharmony_ci	}
115062306a36Sopenharmony_ci
115162306a36Sopenharmony_ci	return new_rate;
115262306a36Sopenharmony_ci}
115362306a36Sopenharmony_ci
115462306a36Sopenharmony_ci/*
115562306a36Sopenharmony_ci * Set up search table for MIMO2
115662306a36Sopenharmony_ci */
115762306a36Sopenharmony_cistatic int
115862306a36Sopenharmony_ciil4965_rs_switch_to_mimo2(struct il_priv *il, struct il_lq_sta *lq_sta,
115962306a36Sopenharmony_ci			  struct ieee80211_conf *conf,
116062306a36Sopenharmony_ci			  struct ieee80211_sta *sta,
116162306a36Sopenharmony_ci			  struct il_scale_tbl_info *tbl, int idx)
116262306a36Sopenharmony_ci{
116362306a36Sopenharmony_ci	u16 rate_mask;
116462306a36Sopenharmony_ci	s32 rate;
116562306a36Sopenharmony_ci	s8 is_green = lq_sta->is_green;
116662306a36Sopenharmony_ci
116762306a36Sopenharmony_ci	if (!conf_is_ht(conf) || !sta->deflink.ht_cap.ht_supported)
116862306a36Sopenharmony_ci		return -1;
116962306a36Sopenharmony_ci
117062306a36Sopenharmony_ci	if (sta->deflink.smps_mode == IEEE80211_SMPS_STATIC)
117162306a36Sopenharmony_ci		return -1;
117262306a36Sopenharmony_ci
117362306a36Sopenharmony_ci	/* Need both Tx chains/antennas to support MIMO */
117462306a36Sopenharmony_ci	if (il->hw_params.tx_chains_num < 2)
117562306a36Sopenharmony_ci		return -1;
117662306a36Sopenharmony_ci
117762306a36Sopenharmony_ci	D_RATE("LQ: try to switch to MIMO2\n");
117862306a36Sopenharmony_ci
117962306a36Sopenharmony_ci	tbl->lq_type = LQ_MIMO2;
118062306a36Sopenharmony_ci	tbl->is_dup = lq_sta->is_dup;
118162306a36Sopenharmony_ci	tbl->action = 0;
118262306a36Sopenharmony_ci	tbl->max_search = IL_MAX_SEARCH;
118362306a36Sopenharmony_ci	rate_mask = lq_sta->active_mimo2_rate;
118462306a36Sopenharmony_ci
118562306a36Sopenharmony_ci	if (il_is_ht40_tx_allowed(il, &sta->deflink.ht_cap))
118662306a36Sopenharmony_ci		tbl->is_ht40 = 1;
118762306a36Sopenharmony_ci	else
118862306a36Sopenharmony_ci		tbl->is_ht40 = 0;
118962306a36Sopenharmony_ci
119062306a36Sopenharmony_ci	il4965_rs_set_expected_tpt_table(lq_sta, tbl);
119162306a36Sopenharmony_ci
119262306a36Sopenharmony_ci	rate = il4965_rs_get_best_rate(il, lq_sta, tbl, rate_mask, idx);
119362306a36Sopenharmony_ci
119462306a36Sopenharmony_ci	D_RATE("LQ: MIMO2 best rate %d mask %X\n", rate, rate_mask);
119562306a36Sopenharmony_ci	if (rate == RATE_INVALID || !((1 << rate) & rate_mask)) {
119662306a36Sopenharmony_ci		D_RATE("Can't switch with idx %d rate mask %x\n", rate,
119762306a36Sopenharmony_ci		       rate_mask);
119862306a36Sopenharmony_ci		return -1;
119962306a36Sopenharmony_ci	}
120062306a36Sopenharmony_ci	tbl->current_rate =
120162306a36Sopenharmony_ci	    il4965_rate_n_flags_from_tbl(il, tbl, rate, is_green);
120262306a36Sopenharmony_ci
120362306a36Sopenharmony_ci	D_RATE("LQ: Switch to new mcs %X idx is green %X\n", tbl->current_rate,
120462306a36Sopenharmony_ci	       is_green);
120562306a36Sopenharmony_ci	return 0;
120662306a36Sopenharmony_ci}
120762306a36Sopenharmony_ci
120862306a36Sopenharmony_ci/*
120962306a36Sopenharmony_ci * Set up search table for SISO
121062306a36Sopenharmony_ci */
121162306a36Sopenharmony_cistatic int
121262306a36Sopenharmony_ciil4965_rs_switch_to_siso(struct il_priv *il, struct il_lq_sta *lq_sta,
121362306a36Sopenharmony_ci			 struct ieee80211_conf *conf, struct ieee80211_sta *sta,
121462306a36Sopenharmony_ci			 struct il_scale_tbl_info *tbl, int idx)
121562306a36Sopenharmony_ci{
121662306a36Sopenharmony_ci	u16 rate_mask;
121762306a36Sopenharmony_ci	u8 is_green = lq_sta->is_green;
121862306a36Sopenharmony_ci	s32 rate;
121962306a36Sopenharmony_ci
122062306a36Sopenharmony_ci	if (!conf_is_ht(conf) || !sta->deflink.ht_cap.ht_supported)
122162306a36Sopenharmony_ci		return -1;
122262306a36Sopenharmony_ci
122362306a36Sopenharmony_ci	D_RATE("LQ: try to switch to SISO\n");
122462306a36Sopenharmony_ci
122562306a36Sopenharmony_ci	tbl->is_dup = lq_sta->is_dup;
122662306a36Sopenharmony_ci	tbl->lq_type = LQ_SISO;
122762306a36Sopenharmony_ci	tbl->action = 0;
122862306a36Sopenharmony_ci	tbl->max_search = IL_MAX_SEARCH;
122962306a36Sopenharmony_ci	rate_mask = lq_sta->active_siso_rate;
123062306a36Sopenharmony_ci
123162306a36Sopenharmony_ci	if (il_is_ht40_tx_allowed(il, &sta->deflink.ht_cap))
123262306a36Sopenharmony_ci		tbl->is_ht40 = 1;
123362306a36Sopenharmony_ci	else
123462306a36Sopenharmony_ci		tbl->is_ht40 = 0;
123562306a36Sopenharmony_ci
123662306a36Sopenharmony_ci	if (is_green)
123762306a36Sopenharmony_ci		tbl->is_SGI = 0;	/*11n spec: no SGI in SISO+Greenfield */
123862306a36Sopenharmony_ci
123962306a36Sopenharmony_ci	il4965_rs_set_expected_tpt_table(lq_sta, tbl);
124062306a36Sopenharmony_ci	rate = il4965_rs_get_best_rate(il, lq_sta, tbl, rate_mask, idx);
124162306a36Sopenharmony_ci
124262306a36Sopenharmony_ci	D_RATE("LQ: get best rate %d mask %X\n", rate, rate_mask);
124362306a36Sopenharmony_ci	if (rate == RATE_INVALID || !((1 << rate) & rate_mask)) {
124462306a36Sopenharmony_ci		D_RATE("can not switch with idx %d rate mask %x\n", rate,
124562306a36Sopenharmony_ci		       rate_mask);
124662306a36Sopenharmony_ci		return -1;
124762306a36Sopenharmony_ci	}
124862306a36Sopenharmony_ci	tbl->current_rate =
124962306a36Sopenharmony_ci	    il4965_rate_n_flags_from_tbl(il, tbl, rate, is_green);
125062306a36Sopenharmony_ci	D_RATE("LQ: Switch to new mcs %X idx is green %X\n", tbl->current_rate,
125162306a36Sopenharmony_ci	       is_green);
125262306a36Sopenharmony_ci	return 0;
125362306a36Sopenharmony_ci}
125462306a36Sopenharmony_ci
125562306a36Sopenharmony_ci/*
125662306a36Sopenharmony_ci * Try to switch to new modulation mode from legacy
125762306a36Sopenharmony_ci */
125862306a36Sopenharmony_cistatic int
125962306a36Sopenharmony_ciil4965_rs_move_legacy_other(struct il_priv *il, struct il_lq_sta *lq_sta,
126062306a36Sopenharmony_ci			    struct ieee80211_conf *conf,
126162306a36Sopenharmony_ci			    struct ieee80211_sta *sta, int idx)
126262306a36Sopenharmony_ci{
126362306a36Sopenharmony_ci	struct il_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
126462306a36Sopenharmony_ci	struct il_scale_tbl_info *search_tbl =
126562306a36Sopenharmony_ci	    &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
126662306a36Sopenharmony_ci	struct il_rate_scale_data *win = &(tbl->win[idx]);
126762306a36Sopenharmony_ci	u32 sz =
126862306a36Sopenharmony_ci	    (sizeof(struct il_scale_tbl_info) -
126962306a36Sopenharmony_ci	     (sizeof(struct il_rate_scale_data) * RATE_COUNT));
127062306a36Sopenharmony_ci	u8 start_action;
127162306a36Sopenharmony_ci	u8 valid_tx_ant = il->hw_params.valid_tx_ant;
127262306a36Sopenharmony_ci	u8 tx_chains_num = il->hw_params.tx_chains_num;
127362306a36Sopenharmony_ci	int ret = 0;
127462306a36Sopenharmony_ci	u8 update_search_tbl_counter = 0;
127562306a36Sopenharmony_ci
127662306a36Sopenharmony_ci	tbl->action = IL_LEGACY_SWITCH_SISO;
127762306a36Sopenharmony_ci
127862306a36Sopenharmony_ci	start_action = tbl->action;
127962306a36Sopenharmony_ci	for (;;) {
128062306a36Sopenharmony_ci		lq_sta->action_counter++;
128162306a36Sopenharmony_ci		switch (tbl->action) {
128262306a36Sopenharmony_ci		case IL_LEGACY_SWITCH_ANTENNA1:
128362306a36Sopenharmony_ci		case IL_LEGACY_SWITCH_ANTENNA2:
128462306a36Sopenharmony_ci			D_RATE("LQ: Legacy toggle Antenna\n");
128562306a36Sopenharmony_ci
128662306a36Sopenharmony_ci			if ((tbl->action == IL_LEGACY_SWITCH_ANTENNA1 &&
128762306a36Sopenharmony_ci			     tx_chains_num <= 1) ||
128862306a36Sopenharmony_ci			    (tbl->action == IL_LEGACY_SWITCH_ANTENNA2 &&
128962306a36Sopenharmony_ci			     tx_chains_num <= 2))
129062306a36Sopenharmony_ci				break;
129162306a36Sopenharmony_ci
129262306a36Sopenharmony_ci			/* Don't change antenna if success has been great */
129362306a36Sopenharmony_ci			if (win->success_ratio >= IL_RS_GOOD_RATIO)
129462306a36Sopenharmony_ci				break;
129562306a36Sopenharmony_ci
129662306a36Sopenharmony_ci			/* Set up search table to try other antenna */
129762306a36Sopenharmony_ci			memcpy(search_tbl, tbl, sz);
129862306a36Sopenharmony_ci
129962306a36Sopenharmony_ci			if (il4965_rs_toggle_antenna
130062306a36Sopenharmony_ci			    (valid_tx_ant, &search_tbl->current_rate,
130162306a36Sopenharmony_ci			     search_tbl)) {
130262306a36Sopenharmony_ci				update_search_tbl_counter = 1;
130362306a36Sopenharmony_ci				il4965_rs_set_expected_tpt_table(lq_sta,
130462306a36Sopenharmony_ci								 search_tbl);
130562306a36Sopenharmony_ci				goto out;
130662306a36Sopenharmony_ci			}
130762306a36Sopenharmony_ci			break;
130862306a36Sopenharmony_ci		case IL_LEGACY_SWITCH_SISO:
130962306a36Sopenharmony_ci			D_RATE("LQ: Legacy switch to SISO\n");
131062306a36Sopenharmony_ci
131162306a36Sopenharmony_ci			/* Set up search table to try SISO */
131262306a36Sopenharmony_ci			memcpy(search_tbl, tbl, sz);
131362306a36Sopenharmony_ci			search_tbl->is_SGI = 0;
131462306a36Sopenharmony_ci			ret =
131562306a36Sopenharmony_ci			    il4965_rs_switch_to_siso(il, lq_sta, conf, sta,
131662306a36Sopenharmony_ci						     search_tbl, idx);
131762306a36Sopenharmony_ci			if (!ret) {
131862306a36Sopenharmony_ci				lq_sta->action_counter = 0;
131962306a36Sopenharmony_ci				goto out;
132062306a36Sopenharmony_ci			}
132162306a36Sopenharmony_ci
132262306a36Sopenharmony_ci			break;
132362306a36Sopenharmony_ci		case IL_LEGACY_SWITCH_MIMO2_AB:
132462306a36Sopenharmony_ci		case IL_LEGACY_SWITCH_MIMO2_AC:
132562306a36Sopenharmony_ci		case IL_LEGACY_SWITCH_MIMO2_BC:
132662306a36Sopenharmony_ci			D_RATE("LQ: Legacy switch to MIMO2\n");
132762306a36Sopenharmony_ci
132862306a36Sopenharmony_ci			/* Set up search table to try MIMO */
132962306a36Sopenharmony_ci			memcpy(search_tbl, tbl, sz);
133062306a36Sopenharmony_ci			search_tbl->is_SGI = 0;
133162306a36Sopenharmony_ci
133262306a36Sopenharmony_ci			if (tbl->action == IL_LEGACY_SWITCH_MIMO2_AB)
133362306a36Sopenharmony_ci				search_tbl->ant_type = ANT_AB;
133462306a36Sopenharmony_ci			else if (tbl->action == IL_LEGACY_SWITCH_MIMO2_AC)
133562306a36Sopenharmony_ci				search_tbl->ant_type = ANT_AC;
133662306a36Sopenharmony_ci			else
133762306a36Sopenharmony_ci				search_tbl->ant_type = ANT_BC;
133862306a36Sopenharmony_ci
133962306a36Sopenharmony_ci			if (!il4965_rs_is_valid_ant
134062306a36Sopenharmony_ci			    (valid_tx_ant, search_tbl->ant_type))
134162306a36Sopenharmony_ci				break;
134262306a36Sopenharmony_ci
134362306a36Sopenharmony_ci			ret =
134462306a36Sopenharmony_ci			    il4965_rs_switch_to_mimo2(il, lq_sta, conf, sta,
134562306a36Sopenharmony_ci						      search_tbl, idx);
134662306a36Sopenharmony_ci			if (!ret) {
134762306a36Sopenharmony_ci				lq_sta->action_counter = 0;
134862306a36Sopenharmony_ci				goto out;
134962306a36Sopenharmony_ci			}
135062306a36Sopenharmony_ci			break;
135162306a36Sopenharmony_ci		}
135262306a36Sopenharmony_ci		tbl->action++;
135362306a36Sopenharmony_ci		if (tbl->action > IL_LEGACY_SWITCH_MIMO2_BC)
135462306a36Sopenharmony_ci			tbl->action = IL_LEGACY_SWITCH_ANTENNA1;
135562306a36Sopenharmony_ci
135662306a36Sopenharmony_ci		if (tbl->action == start_action)
135762306a36Sopenharmony_ci			break;
135862306a36Sopenharmony_ci
135962306a36Sopenharmony_ci	}
136062306a36Sopenharmony_ci	search_tbl->lq_type = LQ_NONE;
136162306a36Sopenharmony_ci	return 0;
136262306a36Sopenharmony_ci
136362306a36Sopenharmony_ciout:
136462306a36Sopenharmony_ci	lq_sta->search_better_tbl = 1;
136562306a36Sopenharmony_ci	tbl->action++;
136662306a36Sopenharmony_ci	if (tbl->action > IL_LEGACY_SWITCH_MIMO2_BC)
136762306a36Sopenharmony_ci		tbl->action = IL_LEGACY_SWITCH_ANTENNA1;
136862306a36Sopenharmony_ci	if (update_search_tbl_counter)
136962306a36Sopenharmony_ci		search_tbl->action = tbl->action;
137062306a36Sopenharmony_ci	return 0;
137162306a36Sopenharmony_ci
137262306a36Sopenharmony_ci}
137362306a36Sopenharmony_ci
137462306a36Sopenharmony_ci/*
137562306a36Sopenharmony_ci * Try to switch to new modulation mode from SISO
137662306a36Sopenharmony_ci */
137762306a36Sopenharmony_cistatic int
137862306a36Sopenharmony_ciil4965_rs_move_siso_to_other(struct il_priv *il, struct il_lq_sta *lq_sta,
137962306a36Sopenharmony_ci			     struct ieee80211_conf *conf,
138062306a36Sopenharmony_ci			     struct ieee80211_sta *sta, int idx)
138162306a36Sopenharmony_ci{
138262306a36Sopenharmony_ci	u8 is_green = lq_sta->is_green;
138362306a36Sopenharmony_ci	struct il_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
138462306a36Sopenharmony_ci	struct il_scale_tbl_info *search_tbl =
138562306a36Sopenharmony_ci	    &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
138662306a36Sopenharmony_ci	struct il_rate_scale_data *win = &(tbl->win[idx]);
138762306a36Sopenharmony_ci	struct ieee80211_sta_ht_cap *ht_cap = &sta->deflink.ht_cap;
138862306a36Sopenharmony_ci	u32 sz =
138962306a36Sopenharmony_ci	    (sizeof(struct il_scale_tbl_info) -
139062306a36Sopenharmony_ci	     (sizeof(struct il_rate_scale_data) * RATE_COUNT));
139162306a36Sopenharmony_ci	u8 start_action;
139262306a36Sopenharmony_ci	u8 valid_tx_ant = il->hw_params.valid_tx_ant;
139362306a36Sopenharmony_ci	u8 tx_chains_num = il->hw_params.tx_chains_num;
139462306a36Sopenharmony_ci	u8 update_search_tbl_counter = 0;
139562306a36Sopenharmony_ci	int ret;
139662306a36Sopenharmony_ci
139762306a36Sopenharmony_ci	start_action = tbl->action;
139862306a36Sopenharmony_ci
139962306a36Sopenharmony_ci	for (;;) {
140062306a36Sopenharmony_ci		lq_sta->action_counter++;
140162306a36Sopenharmony_ci		switch (tbl->action) {
140262306a36Sopenharmony_ci		case IL_SISO_SWITCH_ANTENNA1:
140362306a36Sopenharmony_ci		case IL_SISO_SWITCH_ANTENNA2:
140462306a36Sopenharmony_ci			D_RATE("LQ: SISO toggle Antenna\n");
140562306a36Sopenharmony_ci			if ((tbl->action == IL_SISO_SWITCH_ANTENNA1 &&
140662306a36Sopenharmony_ci			     tx_chains_num <= 1) ||
140762306a36Sopenharmony_ci			    (tbl->action == IL_SISO_SWITCH_ANTENNA2 &&
140862306a36Sopenharmony_ci			     tx_chains_num <= 2))
140962306a36Sopenharmony_ci				break;
141062306a36Sopenharmony_ci
141162306a36Sopenharmony_ci			if (win->success_ratio >= IL_RS_GOOD_RATIO)
141262306a36Sopenharmony_ci				break;
141362306a36Sopenharmony_ci
141462306a36Sopenharmony_ci			memcpy(search_tbl, tbl, sz);
141562306a36Sopenharmony_ci			if (il4965_rs_toggle_antenna
141662306a36Sopenharmony_ci			    (valid_tx_ant, &search_tbl->current_rate,
141762306a36Sopenharmony_ci			     search_tbl)) {
141862306a36Sopenharmony_ci				update_search_tbl_counter = 1;
141962306a36Sopenharmony_ci				goto out;
142062306a36Sopenharmony_ci			}
142162306a36Sopenharmony_ci			break;
142262306a36Sopenharmony_ci		case IL_SISO_SWITCH_MIMO2_AB:
142362306a36Sopenharmony_ci		case IL_SISO_SWITCH_MIMO2_AC:
142462306a36Sopenharmony_ci		case IL_SISO_SWITCH_MIMO2_BC:
142562306a36Sopenharmony_ci			D_RATE("LQ: SISO switch to MIMO2\n");
142662306a36Sopenharmony_ci			memcpy(search_tbl, tbl, sz);
142762306a36Sopenharmony_ci			search_tbl->is_SGI = 0;
142862306a36Sopenharmony_ci
142962306a36Sopenharmony_ci			if (tbl->action == IL_SISO_SWITCH_MIMO2_AB)
143062306a36Sopenharmony_ci				search_tbl->ant_type = ANT_AB;
143162306a36Sopenharmony_ci			else if (tbl->action == IL_SISO_SWITCH_MIMO2_AC)
143262306a36Sopenharmony_ci				search_tbl->ant_type = ANT_AC;
143362306a36Sopenharmony_ci			else
143462306a36Sopenharmony_ci				search_tbl->ant_type = ANT_BC;
143562306a36Sopenharmony_ci
143662306a36Sopenharmony_ci			if (!il4965_rs_is_valid_ant
143762306a36Sopenharmony_ci			    (valid_tx_ant, search_tbl->ant_type))
143862306a36Sopenharmony_ci				break;
143962306a36Sopenharmony_ci
144062306a36Sopenharmony_ci			ret =
144162306a36Sopenharmony_ci			    il4965_rs_switch_to_mimo2(il, lq_sta, conf, sta,
144262306a36Sopenharmony_ci						      search_tbl, idx);
144362306a36Sopenharmony_ci			if (!ret)
144462306a36Sopenharmony_ci				goto out;
144562306a36Sopenharmony_ci			break;
144662306a36Sopenharmony_ci		case IL_SISO_SWITCH_GI:
144762306a36Sopenharmony_ci			if (!tbl->is_ht40 &&
144862306a36Sopenharmony_ci			    !(ht_cap->cap & IEEE80211_HT_CAP_SGI_20))
144962306a36Sopenharmony_ci				break;
145062306a36Sopenharmony_ci			if (tbl->is_ht40 &&
145162306a36Sopenharmony_ci			    !(ht_cap->cap & IEEE80211_HT_CAP_SGI_40))
145262306a36Sopenharmony_ci				break;
145362306a36Sopenharmony_ci
145462306a36Sopenharmony_ci			D_RATE("LQ: SISO toggle SGI/NGI\n");
145562306a36Sopenharmony_ci
145662306a36Sopenharmony_ci			memcpy(search_tbl, tbl, sz);
145762306a36Sopenharmony_ci			if (is_green) {
145862306a36Sopenharmony_ci				if (!tbl->is_SGI)
145962306a36Sopenharmony_ci					break;
146062306a36Sopenharmony_ci				else
146162306a36Sopenharmony_ci					IL_ERR("SGI was set in GF+SISO\n");
146262306a36Sopenharmony_ci			}
146362306a36Sopenharmony_ci			search_tbl->is_SGI = !tbl->is_SGI;
146462306a36Sopenharmony_ci			il4965_rs_set_expected_tpt_table(lq_sta, search_tbl);
146562306a36Sopenharmony_ci			if (tbl->is_SGI) {
146662306a36Sopenharmony_ci				s32 tpt = lq_sta->last_tpt / 100;
146762306a36Sopenharmony_ci				if (tpt >= search_tbl->expected_tpt[idx])
146862306a36Sopenharmony_ci					break;
146962306a36Sopenharmony_ci			}
147062306a36Sopenharmony_ci			search_tbl->current_rate =
147162306a36Sopenharmony_ci			    il4965_rate_n_flags_from_tbl(il, search_tbl, idx,
147262306a36Sopenharmony_ci							 is_green);
147362306a36Sopenharmony_ci			update_search_tbl_counter = 1;
147462306a36Sopenharmony_ci			goto out;
147562306a36Sopenharmony_ci		}
147662306a36Sopenharmony_ci		tbl->action++;
147762306a36Sopenharmony_ci		if (tbl->action > IL_SISO_SWITCH_GI)
147862306a36Sopenharmony_ci			tbl->action = IL_SISO_SWITCH_ANTENNA1;
147962306a36Sopenharmony_ci
148062306a36Sopenharmony_ci		if (tbl->action == start_action)
148162306a36Sopenharmony_ci			break;
148262306a36Sopenharmony_ci	}
148362306a36Sopenharmony_ci	search_tbl->lq_type = LQ_NONE;
148462306a36Sopenharmony_ci	return 0;
148562306a36Sopenharmony_ci
148662306a36Sopenharmony_ciout:
148762306a36Sopenharmony_ci	lq_sta->search_better_tbl = 1;
148862306a36Sopenharmony_ci	tbl->action++;
148962306a36Sopenharmony_ci	if (tbl->action > IL_SISO_SWITCH_GI)
149062306a36Sopenharmony_ci		tbl->action = IL_SISO_SWITCH_ANTENNA1;
149162306a36Sopenharmony_ci	if (update_search_tbl_counter)
149262306a36Sopenharmony_ci		search_tbl->action = tbl->action;
149362306a36Sopenharmony_ci
149462306a36Sopenharmony_ci	return 0;
149562306a36Sopenharmony_ci}
149662306a36Sopenharmony_ci
149762306a36Sopenharmony_ci/*
149862306a36Sopenharmony_ci * Try to switch to new modulation mode from MIMO2
149962306a36Sopenharmony_ci */
150062306a36Sopenharmony_cistatic int
150162306a36Sopenharmony_ciil4965_rs_move_mimo2_to_other(struct il_priv *il, struct il_lq_sta *lq_sta,
150262306a36Sopenharmony_ci			      struct ieee80211_conf *conf,
150362306a36Sopenharmony_ci			      struct ieee80211_sta *sta, int idx)
150462306a36Sopenharmony_ci{
150562306a36Sopenharmony_ci	s8 is_green = lq_sta->is_green;
150662306a36Sopenharmony_ci	struct il_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
150762306a36Sopenharmony_ci	struct il_scale_tbl_info *search_tbl =
150862306a36Sopenharmony_ci	    &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
150962306a36Sopenharmony_ci	struct il_rate_scale_data *win = &(tbl->win[idx]);
151062306a36Sopenharmony_ci	struct ieee80211_sta_ht_cap *ht_cap = &sta->deflink.ht_cap;
151162306a36Sopenharmony_ci	u32 sz =
151262306a36Sopenharmony_ci	    (sizeof(struct il_scale_tbl_info) -
151362306a36Sopenharmony_ci	     (sizeof(struct il_rate_scale_data) * RATE_COUNT));
151462306a36Sopenharmony_ci	u8 start_action;
151562306a36Sopenharmony_ci	u8 valid_tx_ant = il->hw_params.valid_tx_ant;
151662306a36Sopenharmony_ci	u8 tx_chains_num = il->hw_params.tx_chains_num;
151762306a36Sopenharmony_ci	u8 update_search_tbl_counter = 0;
151862306a36Sopenharmony_ci	int ret;
151962306a36Sopenharmony_ci
152062306a36Sopenharmony_ci	start_action = tbl->action;
152162306a36Sopenharmony_ci	for (;;) {
152262306a36Sopenharmony_ci		lq_sta->action_counter++;
152362306a36Sopenharmony_ci		switch (tbl->action) {
152462306a36Sopenharmony_ci		case IL_MIMO2_SWITCH_ANTENNA1:
152562306a36Sopenharmony_ci		case IL_MIMO2_SWITCH_ANTENNA2:
152662306a36Sopenharmony_ci			D_RATE("LQ: MIMO2 toggle Antennas\n");
152762306a36Sopenharmony_ci
152862306a36Sopenharmony_ci			if (tx_chains_num <= 2)
152962306a36Sopenharmony_ci				break;
153062306a36Sopenharmony_ci
153162306a36Sopenharmony_ci			if (win->success_ratio >= IL_RS_GOOD_RATIO)
153262306a36Sopenharmony_ci				break;
153362306a36Sopenharmony_ci
153462306a36Sopenharmony_ci			memcpy(search_tbl, tbl, sz);
153562306a36Sopenharmony_ci			if (il4965_rs_toggle_antenna
153662306a36Sopenharmony_ci			    (valid_tx_ant, &search_tbl->current_rate,
153762306a36Sopenharmony_ci			     search_tbl)) {
153862306a36Sopenharmony_ci				update_search_tbl_counter = 1;
153962306a36Sopenharmony_ci				goto out;
154062306a36Sopenharmony_ci			}
154162306a36Sopenharmony_ci			break;
154262306a36Sopenharmony_ci		case IL_MIMO2_SWITCH_SISO_A:
154362306a36Sopenharmony_ci		case IL_MIMO2_SWITCH_SISO_B:
154462306a36Sopenharmony_ci		case IL_MIMO2_SWITCH_SISO_C:
154562306a36Sopenharmony_ci			D_RATE("LQ: MIMO2 switch to SISO\n");
154662306a36Sopenharmony_ci
154762306a36Sopenharmony_ci			/* Set up new search table for SISO */
154862306a36Sopenharmony_ci			memcpy(search_tbl, tbl, sz);
154962306a36Sopenharmony_ci
155062306a36Sopenharmony_ci			if (tbl->action == IL_MIMO2_SWITCH_SISO_A)
155162306a36Sopenharmony_ci				search_tbl->ant_type = ANT_A;
155262306a36Sopenharmony_ci			else if (tbl->action == IL_MIMO2_SWITCH_SISO_B)
155362306a36Sopenharmony_ci				search_tbl->ant_type = ANT_B;
155462306a36Sopenharmony_ci			else
155562306a36Sopenharmony_ci				search_tbl->ant_type = ANT_C;
155662306a36Sopenharmony_ci
155762306a36Sopenharmony_ci			if (!il4965_rs_is_valid_ant
155862306a36Sopenharmony_ci			    (valid_tx_ant, search_tbl->ant_type))
155962306a36Sopenharmony_ci				break;
156062306a36Sopenharmony_ci
156162306a36Sopenharmony_ci			ret =
156262306a36Sopenharmony_ci			    il4965_rs_switch_to_siso(il, lq_sta, conf, sta,
156362306a36Sopenharmony_ci						     search_tbl, idx);
156462306a36Sopenharmony_ci			if (!ret)
156562306a36Sopenharmony_ci				goto out;
156662306a36Sopenharmony_ci
156762306a36Sopenharmony_ci			break;
156862306a36Sopenharmony_ci
156962306a36Sopenharmony_ci		case IL_MIMO2_SWITCH_GI:
157062306a36Sopenharmony_ci			if (!tbl->is_ht40 &&
157162306a36Sopenharmony_ci			    !(ht_cap->cap & IEEE80211_HT_CAP_SGI_20))
157262306a36Sopenharmony_ci				break;
157362306a36Sopenharmony_ci			if (tbl->is_ht40 &&
157462306a36Sopenharmony_ci			    !(ht_cap->cap & IEEE80211_HT_CAP_SGI_40))
157562306a36Sopenharmony_ci				break;
157662306a36Sopenharmony_ci
157762306a36Sopenharmony_ci			D_RATE("LQ: MIMO2 toggle SGI/NGI\n");
157862306a36Sopenharmony_ci
157962306a36Sopenharmony_ci			/* Set up new search table for MIMO2 */
158062306a36Sopenharmony_ci			memcpy(search_tbl, tbl, sz);
158162306a36Sopenharmony_ci			search_tbl->is_SGI = !tbl->is_SGI;
158262306a36Sopenharmony_ci			il4965_rs_set_expected_tpt_table(lq_sta, search_tbl);
158362306a36Sopenharmony_ci			/*
158462306a36Sopenharmony_ci			 * If active table already uses the fastest possible
158562306a36Sopenharmony_ci			 * modulation (dual stream with short guard interval),
158662306a36Sopenharmony_ci			 * and it's working well, there's no need to look
158762306a36Sopenharmony_ci			 * for a better type of modulation!
158862306a36Sopenharmony_ci			 */
158962306a36Sopenharmony_ci			if (tbl->is_SGI) {
159062306a36Sopenharmony_ci				s32 tpt = lq_sta->last_tpt / 100;
159162306a36Sopenharmony_ci				if (tpt >= search_tbl->expected_tpt[idx])
159262306a36Sopenharmony_ci					break;
159362306a36Sopenharmony_ci			}
159462306a36Sopenharmony_ci			search_tbl->current_rate =
159562306a36Sopenharmony_ci			    il4965_rate_n_flags_from_tbl(il, search_tbl, idx,
159662306a36Sopenharmony_ci							 is_green);
159762306a36Sopenharmony_ci			update_search_tbl_counter = 1;
159862306a36Sopenharmony_ci			goto out;
159962306a36Sopenharmony_ci
160062306a36Sopenharmony_ci		}
160162306a36Sopenharmony_ci		tbl->action++;
160262306a36Sopenharmony_ci		if (tbl->action > IL_MIMO2_SWITCH_GI)
160362306a36Sopenharmony_ci			tbl->action = IL_MIMO2_SWITCH_ANTENNA1;
160462306a36Sopenharmony_ci
160562306a36Sopenharmony_ci		if (tbl->action == start_action)
160662306a36Sopenharmony_ci			break;
160762306a36Sopenharmony_ci	}
160862306a36Sopenharmony_ci	search_tbl->lq_type = LQ_NONE;
160962306a36Sopenharmony_ci	return 0;
161062306a36Sopenharmony_ciout:
161162306a36Sopenharmony_ci	lq_sta->search_better_tbl = 1;
161262306a36Sopenharmony_ci	tbl->action++;
161362306a36Sopenharmony_ci	if (tbl->action > IL_MIMO2_SWITCH_GI)
161462306a36Sopenharmony_ci		tbl->action = IL_MIMO2_SWITCH_ANTENNA1;
161562306a36Sopenharmony_ci	if (update_search_tbl_counter)
161662306a36Sopenharmony_ci		search_tbl->action = tbl->action;
161762306a36Sopenharmony_ci
161862306a36Sopenharmony_ci	return 0;
161962306a36Sopenharmony_ci
162062306a36Sopenharmony_ci}
162162306a36Sopenharmony_ci
162262306a36Sopenharmony_ci/*
162362306a36Sopenharmony_ci * Check whether we should continue using same modulation mode, or
162462306a36Sopenharmony_ci * begin search for a new mode, based on:
162562306a36Sopenharmony_ci * 1) # tx successes or failures while using this mode
162662306a36Sopenharmony_ci * 2) # times calling this function
162762306a36Sopenharmony_ci * 3) elapsed time in this mode (not used, for now)
162862306a36Sopenharmony_ci */
162962306a36Sopenharmony_cistatic void
163062306a36Sopenharmony_ciil4965_rs_stay_in_table(struct il_lq_sta *lq_sta, bool force_search)
163162306a36Sopenharmony_ci{
163262306a36Sopenharmony_ci	struct il_scale_tbl_info *tbl;
163362306a36Sopenharmony_ci	int i;
163462306a36Sopenharmony_ci	int active_tbl;
163562306a36Sopenharmony_ci	int flush_interval_passed = 0;
163662306a36Sopenharmony_ci	struct il_priv *il;
163762306a36Sopenharmony_ci
163862306a36Sopenharmony_ci	il = lq_sta->drv;
163962306a36Sopenharmony_ci	active_tbl = lq_sta->active_tbl;
164062306a36Sopenharmony_ci
164162306a36Sopenharmony_ci	tbl = &(lq_sta->lq_info[active_tbl]);
164262306a36Sopenharmony_ci
164362306a36Sopenharmony_ci	/* If we've been disallowing search, see if we should now allow it */
164462306a36Sopenharmony_ci	if (lq_sta->stay_in_tbl) {
164562306a36Sopenharmony_ci
164662306a36Sopenharmony_ci		/* Elapsed time using current modulation mode */
164762306a36Sopenharmony_ci		if (lq_sta->flush_timer)
164862306a36Sopenharmony_ci			flush_interval_passed =
164962306a36Sopenharmony_ci			    time_after(jiffies,
165062306a36Sopenharmony_ci				       (unsigned long)(lq_sta->flush_timer +
165162306a36Sopenharmony_ci						       RATE_SCALE_FLUSH_INTVL));
165262306a36Sopenharmony_ci
165362306a36Sopenharmony_ci		/*
165462306a36Sopenharmony_ci		 * Check if we should allow search for new modulation mode.
165562306a36Sopenharmony_ci		 * If many frames have failed or succeeded, or we've used
165662306a36Sopenharmony_ci		 * this same modulation for a long time, allow search, and
165762306a36Sopenharmony_ci		 * reset history stats that keep track of whether we should
165862306a36Sopenharmony_ci		 * allow a new search.  Also (below) reset all bitmaps and
165962306a36Sopenharmony_ci		 * stats in active history.
166062306a36Sopenharmony_ci		 */
166162306a36Sopenharmony_ci		if (force_search ||
166262306a36Sopenharmony_ci		    lq_sta->total_failed > lq_sta->max_failure_limit ||
166362306a36Sopenharmony_ci		    lq_sta->total_success > lq_sta->max_success_limit ||
166462306a36Sopenharmony_ci		    (!lq_sta->search_better_tbl && lq_sta->flush_timer &&
166562306a36Sopenharmony_ci		     flush_interval_passed)) {
166662306a36Sopenharmony_ci			D_RATE("LQ: stay is expired %d %d %d\n",
166762306a36Sopenharmony_ci			       lq_sta->total_failed, lq_sta->total_success,
166862306a36Sopenharmony_ci			       flush_interval_passed);
166962306a36Sopenharmony_ci
167062306a36Sopenharmony_ci			/* Allow search for new mode */
167162306a36Sopenharmony_ci			lq_sta->stay_in_tbl = 0;	/* only place reset */
167262306a36Sopenharmony_ci			lq_sta->total_failed = 0;
167362306a36Sopenharmony_ci			lq_sta->total_success = 0;
167462306a36Sopenharmony_ci			lq_sta->flush_timer = 0;
167562306a36Sopenharmony_ci
167662306a36Sopenharmony_ci			/*
167762306a36Sopenharmony_ci			 * Else if we've used this modulation mode enough repetitions
167862306a36Sopenharmony_ci			 * (regardless of elapsed time or success/failure), reset
167962306a36Sopenharmony_ci			 * history bitmaps and rate-specific stats for all rates in
168062306a36Sopenharmony_ci			 * active table.
168162306a36Sopenharmony_ci			 */
168262306a36Sopenharmony_ci		} else {
168362306a36Sopenharmony_ci			lq_sta->table_count++;
168462306a36Sopenharmony_ci			if (lq_sta->table_count >= lq_sta->table_count_limit) {
168562306a36Sopenharmony_ci				lq_sta->table_count = 0;
168662306a36Sopenharmony_ci
168762306a36Sopenharmony_ci				D_RATE("LQ: stay in table clear win\n");
168862306a36Sopenharmony_ci				for (i = 0; i < RATE_COUNT; i++)
168962306a36Sopenharmony_ci					il4965_rs_rate_scale_clear_win(&
169062306a36Sopenharmony_ci								       (tbl->
169162306a36Sopenharmony_ci									win
169262306a36Sopenharmony_ci									[i]));
169362306a36Sopenharmony_ci			}
169462306a36Sopenharmony_ci		}
169562306a36Sopenharmony_ci
169662306a36Sopenharmony_ci		/* If transitioning to allow "search", reset all history
169762306a36Sopenharmony_ci		 * bitmaps and stats in active table (this will become the new
169862306a36Sopenharmony_ci		 * "search" table). */
169962306a36Sopenharmony_ci		if (!lq_sta->stay_in_tbl) {
170062306a36Sopenharmony_ci			for (i = 0; i < RATE_COUNT; i++)
170162306a36Sopenharmony_ci				il4965_rs_rate_scale_clear_win(&(tbl->win[i]));
170262306a36Sopenharmony_ci		}
170362306a36Sopenharmony_ci	}
170462306a36Sopenharmony_ci}
170562306a36Sopenharmony_ci
170662306a36Sopenharmony_ci/*
170762306a36Sopenharmony_ci * setup rate table in uCode
170862306a36Sopenharmony_ci */
170962306a36Sopenharmony_cistatic void
171062306a36Sopenharmony_ciil4965_rs_update_rate_tbl(struct il_priv *il, struct il_lq_sta *lq_sta,
171162306a36Sopenharmony_ci			  struct il_scale_tbl_info *tbl, int idx, u8 is_green)
171262306a36Sopenharmony_ci{
171362306a36Sopenharmony_ci	u32 rate;
171462306a36Sopenharmony_ci
171562306a36Sopenharmony_ci	/* Update uCode's rate table. */
171662306a36Sopenharmony_ci	rate = il4965_rate_n_flags_from_tbl(il, tbl, idx, is_green);
171762306a36Sopenharmony_ci	il4965_rs_fill_link_cmd(il, lq_sta, rate);
171862306a36Sopenharmony_ci	il_send_lq_cmd(il, &lq_sta->lq, CMD_ASYNC, false);
171962306a36Sopenharmony_ci}
172062306a36Sopenharmony_ci
172162306a36Sopenharmony_ci/*
172262306a36Sopenharmony_ci * Do rate scaling and search for new modulation mode.
172362306a36Sopenharmony_ci */
172462306a36Sopenharmony_cistatic void
172562306a36Sopenharmony_ciil4965_rs_rate_scale_perform(struct il_priv *il, struct sk_buff *skb,
172662306a36Sopenharmony_ci			     struct ieee80211_sta *sta,
172762306a36Sopenharmony_ci			     struct il_lq_sta *lq_sta)
172862306a36Sopenharmony_ci{
172962306a36Sopenharmony_ci	struct ieee80211_hw *hw = il->hw;
173062306a36Sopenharmony_ci	struct ieee80211_conf *conf = &hw->conf;
173162306a36Sopenharmony_ci	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
173262306a36Sopenharmony_ci	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
173362306a36Sopenharmony_ci	int low = RATE_INVALID;
173462306a36Sopenharmony_ci	int high = RATE_INVALID;
173562306a36Sopenharmony_ci	int idx;
173662306a36Sopenharmony_ci	int i;
173762306a36Sopenharmony_ci	struct il_rate_scale_data *win = NULL;
173862306a36Sopenharmony_ci	int current_tpt = IL_INVALID_VALUE;
173962306a36Sopenharmony_ci	int low_tpt = IL_INVALID_VALUE;
174062306a36Sopenharmony_ci	int high_tpt = IL_INVALID_VALUE;
174162306a36Sopenharmony_ci	u32 fail_count;
174262306a36Sopenharmony_ci	s8 scale_action = 0;
174362306a36Sopenharmony_ci	u16 rate_mask;
174462306a36Sopenharmony_ci	u8 update_lq = 0;
174562306a36Sopenharmony_ci	struct il_scale_tbl_info *tbl, *tbl1;
174662306a36Sopenharmony_ci	u16 rate_scale_idx_msk = 0;
174762306a36Sopenharmony_ci	u8 is_green = 0;
174862306a36Sopenharmony_ci	u8 active_tbl = 0;
174962306a36Sopenharmony_ci	u8 done_search = 0;
175062306a36Sopenharmony_ci	u16 high_low;
175162306a36Sopenharmony_ci	s32 sr;
175262306a36Sopenharmony_ci	u8 tid;
175362306a36Sopenharmony_ci	struct il_tid_data *tid_data;
175462306a36Sopenharmony_ci
175562306a36Sopenharmony_ci	D_RATE("rate scale calculate new rate for skb\n");
175662306a36Sopenharmony_ci
175762306a36Sopenharmony_ci	/* Send management frames and NO_ACK data using lowest rate. */
175862306a36Sopenharmony_ci	/* TODO: this could probably be improved.. */
175962306a36Sopenharmony_ci	if (!ieee80211_is_data(hdr->frame_control) ||
176062306a36Sopenharmony_ci	    (info->flags & IEEE80211_TX_CTL_NO_ACK))
176162306a36Sopenharmony_ci		return;
176262306a36Sopenharmony_ci
176362306a36Sopenharmony_ci	lq_sta->supp_rates = sta->deflink.supp_rates[lq_sta->band];
176462306a36Sopenharmony_ci
176562306a36Sopenharmony_ci	tid = il4965_rs_tl_add_packet(lq_sta, hdr);
176662306a36Sopenharmony_ci	if (tid != MAX_TID_COUNT && (lq_sta->tx_agg_tid_en & (1 << tid))) {
176762306a36Sopenharmony_ci		tid_data = &il->stations[lq_sta->lq.sta_id].tid[tid];
176862306a36Sopenharmony_ci		if (tid_data->agg.state == IL_AGG_OFF)
176962306a36Sopenharmony_ci			lq_sta->is_agg = 0;
177062306a36Sopenharmony_ci		else
177162306a36Sopenharmony_ci			lq_sta->is_agg = 1;
177262306a36Sopenharmony_ci	} else
177362306a36Sopenharmony_ci		lq_sta->is_agg = 0;
177462306a36Sopenharmony_ci
177562306a36Sopenharmony_ci	/*
177662306a36Sopenharmony_ci	 * Select rate-scale / modulation-mode table to work with in
177762306a36Sopenharmony_ci	 * the rest of this function:  "search" if searching for better
177862306a36Sopenharmony_ci	 * modulation mode, or "active" if doing rate scaling within a mode.
177962306a36Sopenharmony_ci	 */
178062306a36Sopenharmony_ci	if (!lq_sta->search_better_tbl)
178162306a36Sopenharmony_ci		active_tbl = lq_sta->active_tbl;
178262306a36Sopenharmony_ci	else
178362306a36Sopenharmony_ci		active_tbl = 1 - lq_sta->active_tbl;
178462306a36Sopenharmony_ci
178562306a36Sopenharmony_ci	tbl = &(lq_sta->lq_info[active_tbl]);
178662306a36Sopenharmony_ci	if (is_legacy(tbl->lq_type))
178762306a36Sopenharmony_ci		lq_sta->is_green = 0;
178862306a36Sopenharmony_ci	else
178962306a36Sopenharmony_ci		lq_sta->is_green = il4965_rs_use_green(il, sta);
179062306a36Sopenharmony_ci	is_green = lq_sta->is_green;
179162306a36Sopenharmony_ci
179262306a36Sopenharmony_ci	/* current tx rate */
179362306a36Sopenharmony_ci	idx = lq_sta->last_txrate_idx;
179462306a36Sopenharmony_ci
179562306a36Sopenharmony_ci	D_RATE("Rate scale idx %d for type %d\n", idx, tbl->lq_type);
179662306a36Sopenharmony_ci
179762306a36Sopenharmony_ci	/* rates available for this association, and for modulation mode */
179862306a36Sopenharmony_ci	rate_mask = il4965_rs_get_supported_rates(lq_sta, hdr, tbl->lq_type);
179962306a36Sopenharmony_ci
180062306a36Sopenharmony_ci	D_RATE("mask 0x%04X\n", rate_mask);
180162306a36Sopenharmony_ci
180262306a36Sopenharmony_ci	/* mask with station rate restriction */
180362306a36Sopenharmony_ci	if (is_legacy(tbl->lq_type)) {
180462306a36Sopenharmony_ci		if (lq_sta->band == NL80211_BAND_5GHZ)
180562306a36Sopenharmony_ci			/* supp_rates has no CCK bits in A mode */
180662306a36Sopenharmony_ci			rate_scale_idx_msk =
180762306a36Sopenharmony_ci			    (u16) (rate_mask &
180862306a36Sopenharmony_ci				   (lq_sta->supp_rates << IL_FIRST_OFDM_RATE));
180962306a36Sopenharmony_ci		else
181062306a36Sopenharmony_ci			rate_scale_idx_msk =
181162306a36Sopenharmony_ci			    (u16) (rate_mask & lq_sta->supp_rates);
181262306a36Sopenharmony_ci
181362306a36Sopenharmony_ci	} else
181462306a36Sopenharmony_ci		rate_scale_idx_msk = rate_mask;
181562306a36Sopenharmony_ci
181662306a36Sopenharmony_ci	if (!rate_scale_idx_msk)
181762306a36Sopenharmony_ci		rate_scale_idx_msk = rate_mask;
181862306a36Sopenharmony_ci
181962306a36Sopenharmony_ci	if (!((1 << idx) & rate_scale_idx_msk)) {
182062306a36Sopenharmony_ci		IL_ERR("Current Rate is not valid\n");
182162306a36Sopenharmony_ci		if (lq_sta->search_better_tbl) {
182262306a36Sopenharmony_ci			/* revert to active table if search table is not valid */
182362306a36Sopenharmony_ci			tbl->lq_type = LQ_NONE;
182462306a36Sopenharmony_ci			lq_sta->search_better_tbl = 0;
182562306a36Sopenharmony_ci			tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
182662306a36Sopenharmony_ci			/* get "active" rate info */
182762306a36Sopenharmony_ci			idx = il4965_hwrate_to_plcp_idx(tbl->current_rate);
182862306a36Sopenharmony_ci			il4965_rs_update_rate_tbl(il, lq_sta, tbl, idx,
182962306a36Sopenharmony_ci						      is_green);
183062306a36Sopenharmony_ci		}
183162306a36Sopenharmony_ci		return;
183262306a36Sopenharmony_ci	}
183362306a36Sopenharmony_ci
183462306a36Sopenharmony_ci	/* Get expected throughput table and history win for current rate */
183562306a36Sopenharmony_ci	if (!tbl->expected_tpt) {
183662306a36Sopenharmony_ci		IL_ERR("tbl->expected_tpt is NULL\n");
183762306a36Sopenharmony_ci		return;
183862306a36Sopenharmony_ci	}
183962306a36Sopenharmony_ci
184062306a36Sopenharmony_ci	/* force user max rate if set by user */
184162306a36Sopenharmony_ci	if (lq_sta->max_rate_idx != -1 && lq_sta->max_rate_idx < idx) {
184262306a36Sopenharmony_ci		idx = lq_sta->max_rate_idx;
184362306a36Sopenharmony_ci		update_lq = 1;
184462306a36Sopenharmony_ci		win = &(tbl->win[idx]);
184562306a36Sopenharmony_ci		goto lq_update;
184662306a36Sopenharmony_ci	}
184762306a36Sopenharmony_ci
184862306a36Sopenharmony_ci	win = &(tbl->win[idx]);
184962306a36Sopenharmony_ci
185062306a36Sopenharmony_ci	/*
185162306a36Sopenharmony_ci	 * If there is not enough history to calculate actual average
185262306a36Sopenharmony_ci	 * throughput, keep analyzing results of more tx frames, without
185362306a36Sopenharmony_ci	 * changing rate or mode (bypass most of the rest of this function).
185462306a36Sopenharmony_ci	 * Set up new rate table in uCode only if old rate is not supported
185562306a36Sopenharmony_ci	 * in current association (use new rate found above).
185662306a36Sopenharmony_ci	 */
185762306a36Sopenharmony_ci	fail_count = win->counter - win->success_counter;
185862306a36Sopenharmony_ci	if (fail_count < RATE_MIN_FAILURE_TH &&
185962306a36Sopenharmony_ci	    win->success_counter < RATE_MIN_SUCCESS_TH) {
186062306a36Sopenharmony_ci		D_RATE("LQ: still below TH. succ=%d total=%d " "for idx %d\n",
186162306a36Sopenharmony_ci		       win->success_counter, win->counter, idx);
186262306a36Sopenharmony_ci
186362306a36Sopenharmony_ci		/* Can't calculate this yet; not enough history */
186462306a36Sopenharmony_ci		win->average_tpt = IL_INVALID_VALUE;
186562306a36Sopenharmony_ci
186662306a36Sopenharmony_ci		/* Should we stay with this modulation mode,
186762306a36Sopenharmony_ci		 * or search for a new one? */
186862306a36Sopenharmony_ci		il4965_rs_stay_in_table(lq_sta, false);
186962306a36Sopenharmony_ci
187062306a36Sopenharmony_ci		goto out;
187162306a36Sopenharmony_ci	}
187262306a36Sopenharmony_ci	/* Else we have enough samples; calculate estimate of
187362306a36Sopenharmony_ci	 * actual average throughput */
187462306a36Sopenharmony_ci	if (win->average_tpt !=
187562306a36Sopenharmony_ci	    ((win->success_ratio * tbl->expected_tpt[idx] + 64) / 128)) {
187662306a36Sopenharmony_ci		IL_ERR("expected_tpt should have been calculated by now\n");
187762306a36Sopenharmony_ci		win->average_tpt =
187862306a36Sopenharmony_ci		    ((win->success_ratio * tbl->expected_tpt[idx] + 64) / 128);
187962306a36Sopenharmony_ci	}
188062306a36Sopenharmony_ci
188162306a36Sopenharmony_ci	/* If we are searching for better modulation mode, check success. */
188262306a36Sopenharmony_ci	if (lq_sta->search_better_tbl) {
188362306a36Sopenharmony_ci		/* If good success, continue using the "search" mode;
188462306a36Sopenharmony_ci		 * no need to send new link quality command, since we're
188562306a36Sopenharmony_ci		 * continuing to use the setup that we've been trying. */
188662306a36Sopenharmony_ci		if (win->average_tpt > lq_sta->last_tpt) {
188762306a36Sopenharmony_ci
188862306a36Sopenharmony_ci			D_RATE("LQ: SWITCHING TO NEW TBL "
188962306a36Sopenharmony_ci			       "suc=%d cur-tpt=%d old-tpt=%d\n",
189062306a36Sopenharmony_ci			       win->success_ratio, win->average_tpt,
189162306a36Sopenharmony_ci			       lq_sta->last_tpt);
189262306a36Sopenharmony_ci
189362306a36Sopenharmony_ci			if (!is_legacy(tbl->lq_type))
189462306a36Sopenharmony_ci				lq_sta->enable_counter = 1;
189562306a36Sopenharmony_ci
189662306a36Sopenharmony_ci			/* Swap tables; "search" becomes "active" */
189762306a36Sopenharmony_ci			lq_sta->active_tbl = active_tbl;
189862306a36Sopenharmony_ci			current_tpt = win->average_tpt;
189962306a36Sopenharmony_ci
190062306a36Sopenharmony_ci			/* Else poor success; go back to mode in "active" table */
190162306a36Sopenharmony_ci		} else {
190262306a36Sopenharmony_ci
190362306a36Sopenharmony_ci			D_RATE("LQ: GOING BACK TO THE OLD TBL "
190462306a36Sopenharmony_ci			       "suc=%d cur-tpt=%d old-tpt=%d\n",
190562306a36Sopenharmony_ci			       win->success_ratio, win->average_tpt,
190662306a36Sopenharmony_ci			       lq_sta->last_tpt);
190762306a36Sopenharmony_ci
190862306a36Sopenharmony_ci			/* Nullify "search" table */
190962306a36Sopenharmony_ci			tbl->lq_type = LQ_NONE;
191062306a36Sopenharmony_ci
191162306a36Sopenharmony_ci			/* Revert to "active" table */
191262306a36Sopenharmony_ci			active_tbl = lq_sta->active_tbl;
191362306a36Sopenharmony_ci			tbl = &(lq_sta->lq_info[active_tbl]);
191462306a36Sopenharmony_ci
191562306a36Sopenharmony_ci			/* Revert to "active" rate and throughput info */
191662306a36Sopenharmony_ci			idx = il4965_hwrate_to_plcp_idx(tbl->current_rate);
191762306a36Sopenharmony_ci			current_tpt = lq_sta->last_tpt;
191862306a36Sopenharmony_ci
191962306a36Sopenharmony_ci			/* Need to set up a new rate table in uCode */
192062306a36Sopenharmony_ci			update_lq = 1;
192162306a36Sopenharmony_ci		}
192262306a36Sopenharmony_ci
192362306a36Sopenharmony_ci		/* Either way, we've made a decision; modulation mode
192462306a36Sopenharmony_ci		 * search is done, allow rate adjustment next time. */
192562306a36Sopenharmony_ci		lq_sta->search_better_tbl = 0;
192662306a36Sopenharmony_ci		done_search = 1;	/* Don't switch modes below! */
192762306a36Sopenharmony_ci		goto lq_update;
192862306a36Sopenharmony_ci	}
192962306a36Sopenharmony_ci
193062306a36Sopenharmony_ci	/* (Else) not in search of better modulation mode, try for better
193162306a36Sopenharmony_ci	 * starting rate, while staying in this mode. */
193262306a36Sopenharmony_ci	high_low =
193362306a36Sopenharmony_ci	    il4965_rs_get_adjacent_rate(il, idx, rate_scale_idx_msk,
193462306a36Sopenharmony_ci					tbl->lq_type);
193562306a36Sopenharmony_ci	low = high_low & 0xff;
193662306a36Sopenharmony_ci	high = (high_low >> 8) & 0xff;
193762306a36Sopenharmony_ci
193862306a36Sopenharmony_ci	/* If user set max rate, dont allow higher than user constrain */
193962306a36Sopenharmony_ci	if (lq_sta->max_rate_idx != -1 && lq_sta->max_rate_idx < high)
194062306a36Sopenharmony_ci		high = RATE_INVALID;
194162306a36Sopenharmony_ci
194262306a36Sopenharmony_ci	sr = win->success_ratio;
194362306a36Sopenharmony_ci
194462306a36Sopenharmony_ci	/* Collect measured throughputs for current and adjacent rates */
194562306a36Sopenharmony_ci	current_tpt = win->average_tpt;
194662306a36Sopenharmony_ci	if (low != RATE_INVALID)
194762306a36Sopenharmony_ci		low_tpt = tbl->win[low].average_tpt;
194862306a36Sopenharmony_ci	if (high != RATE_INVALID)
194962306a36Sopenharmony_ci		high_tpt = tbl->win[high].average_tpt;
195062306a36Sopenharmony_ci
195162306a36Sopenharmony_ci	scale_action = 0;
195262306a36Sopenharmony_ci
195362306a36Sopenharmony_ci	/* Too many failures, decrease rate */
195462306a36Sopenharmony_ci	if (sr <= RATE_DECREASE_TH || current_tpt == 0) {
195562306a36Sopenharmony_ci		D_RATE("decrease rate because of low success_ratio\n");
195662306a36Sopenharmony_ci		scale_action = -1;
195762306a36Sopenharmony_ci
195862306a36Sopenharmony_ci		/* No throughput measured yet for adjacent rates; try increase. */
195962306a36Sopenharmony_ci	} else if (low_tpt == IL_INVALID_VALUE && high_tpt == IL_INVALID_VALUE) {
196062306a36Sopenharmony_ci
196162306a36Sopenharmony_ci		if (high != RATE_INVALID && sr >= RATE_INCREASE_TH)
196262306a36Sopenharmony_ci			scale_action = 1;
196362306a36Sopenharmony_ci		else if (low != RATE_INVALID)
196462306a36Sopenharmony_ci			scale_action = 0;
196562306a36Sopenharmony_ci	}
196662306a36Sopenharmony_ci
196762306a36Sopenharmony_ci	/* Both adjacent throughputs are measured, but neither one has better
196862306a36Sopenharmony_ci	 * throughput; we're using the best rate, don't change it! */
196962306a36Sopenharmony_ci	else if (low_tpt != IL_INVALID_VALUE && high_tpt != IL_INVALID_VALUE &&
197062306a36Sopenharmony_ci		 low_tpt < current_tpt && high_tpt < current_tpt)
197162306a36Sopenharmony_ci		scale_action = 0;
197262306a36Sopenharmony_ci
197362306a36Sopenharmony_ci	/* At least one adjacent rate's throughput is measured,
197462306a36Sopenharmony_ci	 * and may have better performance. */
197562306a36Sopenharmony_ci	else {
197662306a36Sopenharmony_ci		/* Higher adjacent rate's throughput is measured */
197762306a36Sopenharmony_ci		if (high_tpt != IL_INVALID_VALUE) {
197862306a36Sopenharmony_ci			/* Higher rate has better throughput */
197962306a36Sopenharmony_ci			if (high_tpt > current_tpt && sr >= RATE_INCREASE_TH)
198062306a36Sopenharmony_ci				scale_action = 1;
198162306a36Sopenharmony_ci			else
198262306a36Sopenharmony_ci				scale_action = 0;
198362306a36Sopenharmony_ci
198462306a36Sopenharmony_ci			/* Lower adjacent rate's throughput is measured */
198562306a36Sopenharmony_ci		} else if (low_tpt != IL_INVALID_VALUE) {
198662306a36Sopenharmony_ci			/* Lower rate has better throughput */
198762306a36Sopenharmony_ci			if (low_tpt > current_tpt) {
198862306a36Sopenharmony_ci				D_RATE("decrease rate because of low tpt\n");
198962306a36Sopenharmony_ci				scale_action = -1;
199062306a36Sopenharmony_ci			} else if (sr >= RATE_INCREASE_TH) {
199162306a36Sopenharmony_ci				scale_action = 1;
199262306a36Sopenharmony_ci			}
199362306a36Sopenharmony_ci		}
199462306a36Sopenharmony_ci	}
199562306a36Sopenharmony_ci
199662306a36Sopenharmony_ci	/* Sanity check; asked for decrease, but success rate or throughput
199762306a36Sopenharmony_ci	 * has been good at old rate.  Don't change it. */
199862306a36Sopenharmony_ci	if (scale_action == -1 && low != RATE_INVALID &&
199962306a36Sopenharmony_ci	    (sr > RATE_HIGH_TH || current_tpt > 100 * tbl->expected_tpt[low]))
200062306a36Sopenharmony_ci		scale_action = 0;
200162306a36Sopenharmony_ci
200262306a36Sopenharmony_ci	switch (scale_action) {
200362306a36Sopenharmony_ci	case -1:
200462306a36Sopenharmony_ci		/* Decrease starting rate, update uCode's rate table */
200562306a36Sopenharmony_ci		if (low != RATE_INVALID) {
200662306a36Sopenharmony_ci			update_lq = 1;
200762306a36Sopenharmony_ci			idx = low;
200862306a36Sopenharmony_ci		}
200962306a36Sopenharmony_ci
201062306a36Sopenharmony_ci		break;
201162306a36Sopenharmony_ci	case 1:
201262306a36Sopenharmony_ci		/* Increase starting rate, update uCode's rate table */
201362306a36Sopenharmony_ci		if (high != RATE_INVALID) {
201462306a36Sopenharmony_ci			update_lq = 1;
201562306a36Sopenharmony_ci			idx = high;
201662306a36Sopenharmony_ci		}
201762306a36Sopenharmony_ci
201862306a36Sopenharmony_ci		break;
201962306a36Sopenharmony_ci	case 0:
202062306a36Sopenharmony_ci		/* No change */
202162306a36Sopenharmony_ci	default:
202262306a36Sopenharmony_ci		break;
202362306a36Sopenharmony_ci	}
202462306a36Sopenharmony_ci
202562306a36Sopenharmony_ci	D_RATE("choose rate scale idx %d action %d low %d " "high %d type %d\n",
202662306a36Sopenharmony_ci	       idx, scale_action, low, high, tbl->lq_type);
202762306a36Sopenharmony_ci
202862306a36Sopenharmony_cilq_update:
202962306a36Sopenharmony_ci	/* Replace uCode's rate table for the destination station. */
203062306a36Sopenharmony_ci	if (update_lq)
203162306a36Sopenharmony_ci		il4965_rs_update_rate_tbl(il, lq_sta, tbl, idx, is_green);
203262306a36Sopenharmony_ci
203362306a36Sopenharmony_ci	/* Should we stay with this modulation mode,
203462306a36Sopenharmony_ci	 * or search for a new one? */
203562306a36Sopenharmony_ci	il4965_rs_stay_in_table(lq_sta, false);
203662306a36Sopenharmony_ci
203762306a36Sopenharmony_ci	/*
203862306a36Sopenharmony_ci	 * Search for new modulation mode if we're:
203962306a36Sopenharmony_ci	 * 1)  Not changing rates right now
204062306a36Sopenharmony_ci	 * 2)  Not just finishing up a search
204162306a36Sopenharmony_ci	 * 3)  Allowing a new search
204262306a36Sopenharmony_ci	 */
204362306a36Sopenharmony_ci	if (!update_lq && !done_search && !lq_sta->stay_in_tbl && win->counter) {
204462306a36Sopenharmony_ci		/* Save current throughput to compare with "search" throughput */
204562306a36Sopenharmony_ci		lq_sta->last_tpt = current_tpt;
204662306a36Sopenharmony_ci
204762306a36Sopenharmony_ci		/* Select a new "search" modulation mode to try.
204862306a36Sopenharmony_ci		 * If one is found, set up the new "search" table. */
204962306a36Sopenharmony_ci		if (is_legacy(tbl->lq_type))
205062306a36Sopenharmony_ci			il4965_rs_move_legacy_other(il, lq_sta, conf, sta, idx);
205162306a36Sopenharmony_ci		else if (is_siso(tbl->lq_type))
205262306a36Sopenharmony_ci			il4965_rs_move_siso_to_other(il, lq_sta, conf, sta,
205362306a36Sopenharmony_ci						     idx);
205462306a36Sopenharmony_ci		else		/* (is_mimo2(tbl->lq_type)) */
205562306a36Sopenharmony_ci			il4965_rs_move_mimo2_to_other(il, lq_sta, conf, sta,
205662306a36Sopenharmony_ci						      idx);
205762306a36Sopenharmony_ci
205862306a36Sopenharmony_ci		/* If new "search" mode was selected, set up in uCode table */
205962306a36Sopenharmony_ci		if (lq_sta->search_better_tbl) {
206062306a36Sopenharmony_ci			/* Access the "search" table, clear its history. */
206162306a36Sopenharmony_ci			tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
206262306a36Sopenharmony_ci			for (i = 0; i < RATE_COUNT; i++)
206362306a36Sopenharmony_ci				il4965_rs_rate_scale_clear_win(&(tbl->win[i]));
206462306a36Sopenharmony_ci
206562306a36Sopenharmony_ci			/* Use new "search" start rate */
206662306a36Sopenharmony_ci			idx = il4965_hwrate_to_plcp_idx(tbl->current_rate);
206762306a36Sopenharmony_ci
206862306a36Sopenharmony_ci			D_RATE("Switch current  mcs: %X idx: %d\n",
206962306a36Sopenharmony_ci			       tbl->current_rate, idx);
207062306a36Sopenharmony_ci			il4965_rs_fill_link_cmd(il, lq_sta, tbl->current_rate);
207162306a36Sopenharmony_ci			il_send_lq_cmd(il, &lq_sta->lq, CMD_ASYNC, false);
207262306a36Sopenharmony_ci		} else
207362306a36Sopenharmony_ci			done_search = 1;
207462306a36Sopenharmony_ci	}
207562306a36Sopenharmony_ci
207662306a36Sopenharmony_ci	if (done_search && !lq_sta->stay_in_tbl) {
207762306a36Sopenharmony_ci		/* If the "active" (non-search) mode was legacy,
207862306a36Sopenharmony_ci		 * and we've tried switching antennas,
207962306a36Sopenharmony_ci		 * but we haven't been able to try HT modes (not available),
208062306a36Sopenharmony_ci		 * stay with best antenna legacy modulation for a while
208162306a36Sopenharmony_ci		 * before next round of mode comparisons. */
208262306a36Sopenharmony_ci		tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
208362306a36Sopenharmony_ci		if (is_legacy(tbl1->lq_type) && !conf_is_ht(conf) &&
208462306a36Sopenharmony_ci		    lq_sta->action_counter > tbl1->max_search) {
208562306a36Sopenharmony_ci			D_RATE("LQ: STAY in legacy table\n");
208662306a36Sopenharmony_ci			il4965_rs_set_stay_in_table(il, 1, lq_sta);
208762306a36Sopenharmony_ci		}
208862306a36Sopenharmony_ci
208962306a36Sopenharmony_ci		/* If we're in an HT mode, and all 3 mode switch actions
209062306a36Sopenharmony_ci		 * have been tried and compared, stay in this best modulation
209162306a36Sopenharmony_ci		 * mode for a while before next round of mode comparisons. */
209262306a36Sopenharmony_ci		if (lq_sta->enable_counter &&
209362306a36Sopenharmony_ci		    lq_sta->action_counter >= tbl1->max_search) {
209462306a36Sopenharmony_ci			if (lq_sta->last_tpt > IL_AGG_TPT_THREHOLD &&
209562306a36Sopenharmony_ci			    (lq_sta->tx_agg_tid_en & (1 << tid)) &&
209662306a36Sopenharmony_ci			    tid != MAX_TID_COUNT) {
209762306a36Sopenharmony_ci				tid_data =
209862306a36Sopenharmony_ci				    &il->stations[lq_sta->lq.sta_id].tid[tid];
209962306a36Sopenharmony_ci				if (tid_data->agg.state == IL_AGG_OFF) {
210062306a36Sopenharmony_ci					D_RATE("try to aggregate tid %d\n",
210162306a36Sopenharmony_ci					       tid);
210262306a36Sopenharmony_ci					il4965_rs_tl_turn_on_agg(il, tid,
210362306a36Sopenharmony_ci								 lq_sta, sta);
210462306a36Sopenharmony_ci				}
210562306a36Sopenharmony_ci			}
210662306a36Sopenharmony_ci			il4965_rs_set_stay_in_table(il, 0, lq_sta);
210762306a36Sopenharmony_ci		}
210862306a36Sopenharmony_ci	}
210962306a36Sopenharmony_ci
211062306a36Sopenharmony_ciout:
211162306a36Sopenharmony_ci	tbl->current_rate =
211262306a36Sopenharmony_ci	    il4965_rate_n_flags_from_tbl(il, tbl, idx, is_green);
211362306a36Sopenharmony_ci	i = idx;
211462306a36Sopenharmony_ci	lq_sta->last_txrate_idx = i;
211562306a36Sopenharmony_ci}
211662306a36Sopenharmony_ci
211762306a36Sopenharmony_ci/*
211862306a36Sopenharmony_ci * il4965_rs_initialize_lq - Initialize a station's hardware rate table
211962306a36Sopenharmony_ci *
212062306a36Sopenharmony_ci * The uCode's station table contains a table of fallback rates
212162306a36Sopenharmony_ci * for automatic fallback during transmission.
212262306a36Sopenharmony_ci *
212362306a36Sopenharmony_ci * NOTE: This sets up a default set of values.  These will be replaced later
212462306a36Sopenharmony_ci *       if the driver's iwl-4965-rs rate scaling algorithm is used, instead of
212562306a36Sopenharmony_ci *       rc80211_simple.
212662306a36Sopenharmony_ci *
212762306a36Sopenharmony_ci * NOTE: Run C_ADD_STA command to set up station table entry, before
212862306a36Sopenharmony_ci *       calling this function (which runs C_TX_LINK_QUALITY_CMD,
212962306a36Sopenharmony_ci *       which requires station table entry to exist).
213062306a36Sopenharmony_ci */
213162306a36Sopenharmony_cistatic void
213262306a36Sopenharmony_ciil4965_rs_initialize_lq(struct il_priv *il, struct ieee80211_conf *conf,
213362306a36Sopenharmony_ci			struct ieee80211_sta *sta, struct il_lq_sta *lq_sta)
213462306a36Sopenharmony_ci{
213562306a36Sopenharmony_ci	struct il_scale_tbl_info *tbl;
213662306a36Sopenharmony_ci	int rate_idx;
213762306a36Sopenharmony_ci	int i;
213862306a36Sopenharmony_ci	u32 rate;
213962306a36Sopenharmony_ci	u8 use_green;
214062306a36Sopenharmony_ci	u8 active_tbl = 0;
214162306a36Sopenharmony_ci	u8 valid_tx_ant;
214262306a36Sopenharmony_ci
214362306a36Sopenharmony_ci	if (!sta || !lq_sta)
214462306a36Sopenharmony_ci		return;
214562306a36Sopenharmony_ci
214662306a36Sopenharmony_ci	use_green = il4965_rs_use_green(il, sta);
214762306a36Sopenharmony_ci
214862306a36Sopenharmony_ci	i = lq_sta->last_txrate_idx;
214962306a36Sopenharmony_ci
215062306a36Sopenharmony_ci	valid_tx_ant = il->hw_params.valid_tx_ant;
215162306a36Sopenharmony_ci
215262306a36Sopenharmony_ci	if (!lq_sta->search_better_tbl)
215362306a36Sopenharmony_ci		active_tbl = lq_sta->active_tbl;
215462306a36Sopenharmony_ci	else
215562306a36Sopenharmony_ci		active_tbl = 1 - lq_sta->active_tbl;
215662306a36Sopenharmony_ci
215762306a36Sopenharmony_ci	tbl = &(lq_sta->lq_info[active_tbl]);
215862306a36Sopenharmony_ci
215962306a36Sopenharmony_ci	if (i < 0 || i >= RATE_COUNT)
216062306a36Sopenharmony_ci		i = 0;
216162306a36Sopenharmony_ci
216262306a36Sopenharmony_ci	rate = il_rates[i].plcp;
216362306a36Sopenharmony_ci	tbl->ant_type = il4965_first_antenna(valid_tx_ant);
216462306a36Sopenharmony_ci	rate |= tbl->ant_type << RATE_MCS_ANT_POS;
216562306a36Sopenharmony_ci
216662306a36Sopenharmony_ci	if (i >= IL_FIRST_CCK_RATE && i <= IL_LAST_CCK_RATE)
216762306a36Sopenharmony_ci		rate |= RATE_MCS_CCK_MSK;
216862306a36Sopenharmony_ci
216962306a36Sopenharmony_ci	il4965_rs_get_tbl_info_from_mcs(rate, il->band, tbl, &rate_idx);
217062306a36Sopenharmony_ci	if (!il4965_rs_is_valid_ant(valid_tx_ant, tbl->ant_type))
217162306a36Sopenharmony_ci		il4965_rs_toggle_antenna(valid_tx_ant, &rate, tbl);
217262306a36Sopenharmony_ci
217362306a36Sopenharmony_ci	rate = il4965_rate_n_flags_from_tbl(il, tbl, rate_idx, use_green);
217462306a36Sopenharmony_ci	tbl->current_rate = rate;
217562306a36Sopenharmony_ci	il4965_rs_set_expected_tpt_table(lq_sta, tbl);
217662306a36Sopenharmony_ci	il4965_rs_fill_link_cmd(NULL, lq_sta, rate);
217762306a36Sopenharmony_ci	il->stations[lq_sta->lq.sta_id].lq = &lq_sta->lq;
217862306a36Sopenharmony_ci	il_send_lq_cmd(il, &lq_sta->lq, CMD_SYNC, true);
217962306a36Sopenharmony_ci}
218062306a36Sopenharmony_ci
218162306a36Sopenharmony_cistatic void
218262306a36Sopenharmony_ciil4965_rs_get_rate(void *il_r, struct ieee80211_sta *sta, void *il_sta,
218362306a36Sopenharmony_ci		   struct ieee80211_tx_rate_control *txrc)
218462306a36Sopenharmony_ci{
218562306a36Sopenharmony_ci
218662306a36Sopenharmony_ci	struct sk_buff *skb = txrc->skb;
218762306a36Sopenharmony_ci	struct ieee80211_supported_band *sband = txrc->sband;
218862306a36Sopenharmony_ci	struct il_priv *il __maybe_unused = (struct il_priv *)il_r;
218962306a36Sopenharmony_ci	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
219062306a36Sopenharmony_ci	struct il_lq_sta *lq_sta = il_sta;
219162306a36Sopenharmony_ci	int rate_idx;
219262306a36Sopenharmony_ci
219362306a36Sopenharmony_ci	D_RATE("rate scale calculate new rate for skb\n");
219462306a36Sopenharmony_ci
219562306a36Sopenharmony_ci	/* Get max rate if user set max rate */
219662306a36Sopenharmony_ci	if (lq_sta) {
219762306a36Sopenharmony_ci		lq_sta->max_rate_idx = fls(txrc->rate_idx_mask) - 1;
219862306a36Sopenharmony_ci		if (sband->band == NL80211_BAND_5GHZ &&
219962306a36Sopenharmony_ci		    lq_sta->max_rate_idx != -1)
220062306a36Sopenharmony_ci			lq_sta->max_rate_idx += IL_FIRST_OFDM_RATE;
220162306a36Sopenharmony_ci		if (lq_sta->max_rate_idx < 0 ||
220262306a36Sopenharmony_ci		    lq_sta->max_rate_idx >= RATE_COUNT)
220362306a36Sopenharmony_ci			lq_sta->max_rate_idx = -1;
220462306a36Sopenharmony_ci	}
220562306a36Sopenharmony_ci
220662306a36Sopenharmony_ci	/* Treat uninitialized rate scaling data same as non-existing. */
220762306a36Sopenharmony_ci	if (lq_sta && !lq_sta->drv) {
220862306a36Sopenharmony_ci		D_RATE("Rate scaling not initialized yet.\n");
220962306a36Sopenharmony_ci		il_sta = NULL;
221062306a36Sopenharmony_ci	}
221162306a36Sopenharmony_ci
221262306a36Sopenharmony_ci	if (!lq_sta)
221362306a36Sopenharmony_ci		return;
221462306a36Sopenharmony_ci
221562306a36Sopenharmony_ci	rate_idx = lq_sta->last_txrate_idx;
221662306a36Sopenharmony_ci
221762306a36Sopenharmony_ci	if (lq_sta->last_rate_n_flags & RATE_MCS_HT_MSK) {
221862306a36Sopenharmony_ci		rate_idx -= IL_FIRST_OFDM_RATE;
221962306a36Sopenharmony_ci		/* 6M and 9M shared same MCS idx */
222062306a36Sopenharmony_ci		rate_idx = (rate_idx > 0) ? (rate_idx - 1) : 0;
222162306a36Sopenharmony_ci		if (il4965_rs_extract_rate(lq_sta->last_rate_n_flags) >=
222262306a36Sopenharmony_ci		    RATE_MIMO2_6M_PLCP)
222362306a36Sopenharmony_ci			rate_idx = rate_idx + MCS_IDX_PER_STREAM;
222462306a36Sopenharmony_ci		info->control.rates[0].flags = IEEE80211_TX_RC_MCS;
222562306a36Sopenharmony_ci		if (lq_sta->last_rate_n_flags & RATE_MCS_SGI_MSK)
222662306a36Sopenharmony_ci			info->control.rates[0].flags |=
222762306a36Sopenharmony_ci			    IEEE80211_TX_RC_SHORT_GI;
222862306a36Sopenharmony_ci		if (lq_sta->last_rate_n_flags & RATE_MCS_DUP_MSK)
222962306a36Sopenharmony_ci			info->control.rates[0].flags |=
223062306a36Sopenharmony_ci			    IEEE80211_TX_RC_DUP_DATA;
223162306a36Sopenharmony_ci		if (lq_sta->last_rate_n_flags & RATE_MCS_HT40_MSK)
223262306a36Sopenharmony_ci			info->control.rates[0].flags |=
223362306a36Sopenharmony_ci			    IEEE80211_TX_RC_40_MHZ_WIDTH;
223462306a36Sopenharmony_ci		if (lq_sta->last_rate_n_flags & RATE_MCS_GF_MSK)
223562306a36Sopenharmony_ci			info->control.rates[0].flags |=
223662306a36Sopenharmony_ci			    IEEE80211_TX_RC_GREEN_FIELD;
223762306a36Sopenharmony_ci	} else {
223862306a36Sopenharmony_ci		/* Check for invalid rates */
223962306a36Sopenharmony_ci		if (rate_idx < 0 || rate_idx >= RATE_COUNT_LEGACY ||
224062306a36Sopenharmony_ci		    (sband->band == NL80211_BAND_5GHZ &&
224162306a36Sopenharmony_ci		     rate_idx < IL_FIRST_OFDM_RATE))
224262306a36Sopenharmony_ci			rate_idx = rate_lowest_index(sband, sta);
224362306a36Sopenharmony_ci		/* On valid 5 GHz rate, adjust idx */
224462306a36Sopenharmony_ci		else if (sband->band == NL80211_BAND_5GHZ)
224562306a36Sopenharmony_ci			rate_idx -= IL_FIRST_OFDM_RATE;
224662306a36Sopenharmony_ci		info->control.rates[0].flags = 0;
224762306a36Sopenharmony_ci	}
224862306a36Sopenharmony_ci	info->control.rates[0].idx = rate_idx;
224962306a36Sopenharmony_ci	info->control.rates[0].count = 1;
225062306a36Sopenharmony_ci}
225162306a36Sopenharmony_ci
225262306a36Sopenharmony_cistatic void *
225362306a36Sopenharmony_ciil4965_rs_alloc_sta(void *il_rate, struct ieee80211_sta *sta, gfp_t gfp)
225462306a36Sopenharmony_ci{
225562306a36Sopenharmony_ci	struct il_station_priv *sta_priv =
225662306a36Sopenharmony_ci	    (struct il_station_priv *)sta->drv_priv;
225762306a36Sopenharmony_ci	struct il_priv *il;
225862306a36Sopenharmony_ci
225962306a36Sopenharmony_ci	il = (struct il_priv *)il_rate;
226062306a36Sopenharmony_ci	D_RATE("create station rate scale win\n");
226162306a36Sopenharmony_ci
226262306a36Sopenharmony_ci	return &sta_priv->lq_sta;
226362306a36Sopenharmony_ci}
226462306a36Sopenharmony_ci
226562306a36Sopenharmony_ci/*
226662306a36Sopenharmony_ci * Called after adding a new station to initialize rate scaling
226762306a36Sopenharmony_ci */
226862306a36Sopenharmony_civoid
226962306a36Sopenharmony_ciil4965_rs_rate_init(struct il_priv *il, struct ieee80211_sta *sta, u8 sta_id)
227062306a36Sopenharmony_ci{
227162306a36Sopenharmony_ci	int i, j;
227262306a36Sopenharmony_ci	struct ieee80211_hw *hw = il->hw;
227362306a36Sopenharmony_ci	struct ieee80211_conf *conf = &il->hw->conf;
227462306a36Sopenharmony_ci	struct ieee80211_sta_ht_cap *ht_cap = &sta->deflink.ht_cap;
227562306a36Sopenharmony_ci	struct il_station_priv *sta_priv;
227662306a36Sopenharmony_ci	struct il_lq_sta *lq_sta;
227762306a36Sopenharmony_ci	struct ieee80211_supported_band *sband;
227862306a36Sopenharmony_ci
227962306a36Sopenharmony_ci	sta_priv = (struct il_station_priv *)sta->drv_priv;
228062306a36Sopenharmony_ci	lq_sta = &sta_priv->lq_sta;
228162306a36Sopenharmony_ci	sband = hw->wiphy->bands[conf->chandef.chan->band];
228262306a36Sopenharmony_ci
228362306a36Sopenharmony_ci	lq_sta->lq.sta_id = sta_id;
228462306a36Sopenharmony_ci
228562306a36Sopenharmony_ci	for (j = 0; j < LQ_SIZE; j++)
228662306a36Sopenharmony_ci		for (i = 0; i < RATE_COUNT; i++)
228762306a36Sopenharmony_ci			il4965_rs_rate_scale_clear_win(&lq_sta->lq_info[j].
228862306a36Sopenharmony_ci						       win[i]);
228962306a36Sopenharmony_ci
229062306a36Sopenharmony_ci	lq_sta->flush_timer = 0;
229162306a36Sopenharmony_ci	lq_sta->supp_rates = sta->deflink.supp_rates[sband->band];
229262306a36Sopenharmony_ci	for (j = 0; j < LQ_SIZE; j++)
229362306a36Sopenharmony_ci		for (i = 0; i < RATE_COUNT; i++)
229462306a36Sopenharmony_ci			il4965_rs_rate_scale_clear_win(&lq_sta->lq_info[j].
229562306a36Sopenharmony_ci						       win[i]);
229662306a36Sopenharmony_ci
229762306a36Sopenharmony_ci	D_RATE("LQ:" "*** rate scale station global init for station %d ***\n",
229862306a36Sopenharmony_ci	       sta_id);
229962306a36Sopenharmony_ci	/* TODO: what is a good starting rate for STA? About middle? Maybe not
230062306a36Sopenharmony_ci	 * the lowest or the highest rate.. Could consider using RSSI from
230162306a36Sopenharmony_ci	 * previous packets? Need to have IEEE 802.1X auth succeed immediately
230262306a36Sopenharmony_ci	 * after assoc.. */
230362306a36Sopenharmony_ci
230462306a36Sopenharmony_ci	lq_sta->is_dup = 0;
230562306a36Sopenharmony_ci	lq_sta->max_rate_idx = -1;
230662306a36Sopenharmony_ci	lq_sta->missed_rate_counter = IL_MISSED_RATE_MAX;
230762306a36Sopenharmony_ci	lq_sta->is_green = il4965_rs_use_green(il, sta);
230862306a36Sopenharmony_ci	lq_sta->active_legacy_rate = il->active_rate & ~(0x1000);
230962306a36Sopenharmony_ci	lq_sta->band = il->band;
231062306a36Sopenharmony_ci	/*
231162306a36Sopenharmony_ci	 * active_siso_rate mask includes 9 MBits (bit 5), and CCK (bits 0-3),
231262306a36Sopenharmony_ci	 * supp_rates[] does not; shift to convert format, force 9 MBits off.
231362306a36Sopenharmony_ci	 */
231462306a36Sopenharmony_ci	lq_sta->active_siso_rate = ht_cap->mcs.rx_mask[0] << 1;
231562306a36Sopenharmony_ci	lq_sta->active_siso_rate |= ht_cap->mcs.rx_mask[0] & 0x1;
231662306a36Sopenharmony_ci	lq_sta->active_siso_rate &= ~((u16) 0x2);
231762306a36Sopenharmony_ci	lq_sta->active_siso_rate <<= IL_FIRST_OFDM_RATE;
231862306a36Sopenharmony_ci
231962306a36Sopenharmony_ci	/* Same here */
232062306a36Sopenharmony_ci	lq_sta->active_mimo2_rate = ht_cap->mcs.rx_mask[1] << 1;
232162306a36Sopenharmony_ci	lq_sta->active_mimo2_rate |= ht_cap->mcs.rx_mask[1] & 0x1;
232262306a36Sopenharmony_ci	lq_sta->active_mimo2_rate &= ~((u16) 0x2);
232362306a36Sopenharmony_ci	lq_sta->active_mimo2_rate <<= IL_FIRST_OFDM_RATE;
232462306a36Sopenharmony_ci
232562306a36Sopenharmony_ci	/* These values will be overridden later */
232662306a36Sopenharmony_ci	lq_sta->lq.general_params.single_stream_ant_msk =
232762306a36Sopenharmony_ci	    il4965_first_antenna(il->hw_params.valid_tx_ant);
232862306a36Sopenharmony_ci	lq_sta->lq.general_params.dual_stream_ant_msk =
232962306a36Sopenharmony_ci	    il->hw_params.valid_tx_ant & ~il4965_first_antenna(il->hw_params.
233062306a36Sopenharmony_ci							       valid_tx_ant);
233162306a36Sopenharmony_ci	if (!lq_sta->lq.general_params.dual_stream_ant_msk) {
233262306a36Sopenharmony_ci		lq_sta->lq.general_params.dual_stream_ant_msk = ANT_AB;
233362306a36Sopenharmony_ci	} else if (il4965_num_of_ant(il->hw_params.valid_tx_ant) == 2) {
233462306a36Sopenharmony_ci		lq_sta->lq.general_params.dual_stream_ant_msk =
233562306a36Sopenharmony_ci		    il->hw_params.valid_tx_ant;
233662306a36Sopenharmony_ci	}
233762306a36Sopenharmony_ci
233862306a36Sopenharmony_ci	/* as default allow aggregation for all tids */
233962306a36Sopenharmony_ci	lq_sta->tx_agg_tid_en = IL_AGG_ALL_TID;
234062306a36Sopenharmony_ci	lq_sta->drv = il;
234162306a36Sopenharmony_ci
234262306a36Sopenharmony_ci	/* Set last_txrate_idx to lowest rate */
234362306a36Sopenharmony_ci	lq_sta->last_txrate_idx = rate_lowest_index(sband, sta);
234462306a36Sopenharmony_ci	if (sband->band == NL80211_BAND_5GHZ)
234562306a36Sopenharmony_ci		lq_sta->last_txrate_idx += IL_FIRST_OFDM_RATE;
234662306a36Sopenharmony_ci	lq_sta->is_agg = 0;
234762306a36Sopenharmony_ci
234862306a36Sopenharmony_ci#ifdef CONFIG_MAC80211_DEBUGFS
234962306a36Sopenharmony_ci	lq_sta->dbg_fixed_rate = 0;
235062306a36Sopenharmony_ci#endif
235162306a36Sopenharmony_ci
235262306a36Sopenharmony_ci	il4965_rs_initialize_lq(il, conf, sta, lq_sta);
235362306a36Sopenharmony_ci}
235462306a36Sopenharmony_ci
235562306a36Sopenharmony_cistatic void
235662306a36Sopenharmony_ciil4965_rs_fill_link_cmd(struct il_priv *il, struct il_lq_sta *lq_sta,
235762306a36Sopenharmony_ci			u32 new_rate)
235862306a36Sopenharmony_ci{
235962306a36Sopenharmony_ci	struct il_scale_tbl_info tbl_type;
236062306a36Sopenharmony_ci	int idx = 0;
236162306a36Sopenharmony_ci	int rate_idx;
236262306a36Sopenharmony_ci	int repeat_rate = 0;
236362306a36Sopenharmony_ci	u8 ant_toggle_cnt = 0;
236462306a36Sopenharmony_ci	u8 use_ht_possible = 1;
236562306a36Sopenharmony_ci	u8 valid_tx_ant = 0;
236662306a36Sopenharmony_ci	struct il_link_quality_cmd *lq_cmd = &lq_sta->lq;
236762306a36Sopenharmony_ci
236862306a36Sopenharmony_ci	/* Override starting rate (idx 0) if needed for debug purposes */
236962306a36Sopenharmony_ci	il4965_rs_dbgfs_set_mcs(lq_sta, &new_rate, idx);
237062306a36Sopenharmony_ci
237162306a36Sopenharmony_ci	/* Interpret new_rate (rate_n_flags) */
237262306a36Sopenharmony_ci	il4965_rs_get_tbl_info_from_mcs(new_rate, lq_sta->band, &tbl_type,
237362306a36Sopenharmony_ci					&rate_idx);
237462306a36Sopenharmony_ci
237562306a36Sopenharmony_ci	/* How many times should we repeat the initial rate? */
237662306a36Sopenharmony_ci	if (is_legacy(tbl_type.lq_type)) {
237762306a36Sopenharmony_ci		ant_toggle_cnt = 1;
237862306a36Sopenharmony_ci		repeat_rate = IL_NUMBER_TRY;
237962306a36Sopenharmony_ci	} else {
238062306a36Sopenharmony_ci		repeat_rate = IL_HT_NUMBER_TRY;
238162306a36Sopenharmony_ci	}
238262306a36Sopenharmony_ci
238362306a36Sopenharmony_ci	lq_cmd->general_params.mimo_delimiter =
238462306a36Sopenharmony_ci	    is_mimo(tbl_type.lq_type) ? 1 : 0;
238562306a36Sopenharmony_ci
238662306a36Sopenharmony_ci	/* Fill 1st table entry (idx 0) */
238762306a36Sopenharmony_ci	lq_cmd->rs_table[idx].rate_n_flags = cpu_to_le32(new_rate);
238862306a36Sopenharmony_ci
238962306a36Sopenharmony_ci	if (il4965_num_of_ant(tbl_type.ant_type) == 1) {
239062306a36Sopenharmony_ci		lq_cmd->general_params.single_stream_ant_msk =
239162306a36Sopenharmony_ci		    tbl_type.ant_type;
239262306a36Sopenharmony_ci	} else if (il4965_num_of_ant(tbl_type.ant_type) == 2) {
239362306a36Sopenharmony_ci		lq_cmd->general_params.dual_stream_ant_msk = tbl_type.ant_type;
239462306a36Sopenharmony_ci	}
239562306a36Sopenharmony_ci	/* otherwise we don't modify the existing value */
239662306a36Sopenharmony_ci	idx++;
239762306a36Sopenharmony_ci	repeat_rate--;
239862306a36Sopenharmony_ci	if (il)
239962306a36Sopenharmony_ci		valid_tx_ant = il->hw_params.valid_tx_ant;
240062306a36Sopenharmony_ci
240162306a36Sopenharmony_ci	/* Fill rest of rate table */
240262306a36Sopenharmony_ci	while (idx < LINK_QUAL_MAX_RETRY_NUM) {
240362306a36Sopenharmony_ci		/* Repeat initial/next rate.
240462306a36Sopenharmony_ci		 * For legacy IL_NUMBER_TRY == 1, this loop will not execute.
240562306a36Sopenharmony_ci		 * For HT IL_HT_NUMBER_TRY == 3, this executes twice. */
240662306a36Sopenharmony_ci		while (repeat_rate > 0 && idx < (LINK_QUAL_MAX_RETRY_NUM - 1)) {
240762306a36Sopenharmony_ci			if (is_legacy(tbl_type.lq_type)) {
240862306a36Sopenharmony_ci				if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
240962306a36Sopenharmony_ci					ant_toggle_cnt++;
241062306a36Sopenharmony_ci				else if (il &&
241162306a36Sopenharmony_ci					 il4965_rs_toggle_antenna(valid_tx_ant,
241262306a36Sopenharmony_ci								  &new_rate,
241362306a36Sopenharmony_ci								  &tbl_type))
241462306a36Sopenharmony_ci					ant_toggle_cnt = 1;
241562306a36Sopenharmony_ci			}
241662306a36Sopenharmony_ci
241762306a36Sopenharmony_ci			/* Override next rate if needed for debug purposes */
241862306a36Sopenharmony_ci			il4965_rs_dbgfs_set_mcs(lq_sta, &new_rate, idx);
241962306a36Sopenharmony_ci
242062306a36Sopenharmony_ci			/* Fill next table entry */
242162306a36Sopenharmony_ci			lq_cmd->rs_table[idx].rate_n_flags =
242262306a36Sopenharmony_ci			    cpu_to_le32(new_rate);
242362306a36Sopenharmony_ci			repeat_rate--;
242462306a36Sopenharmony_ci			idx++;
242562306a36Sopenharmony_ci		}
242662306a36Sopenharmony_ci
242762306a36Sopenharmony_ci		il4965_rs_get_tbl_info_from_mcs(new_rate, lq_sta->band,
242862306a36Sopenharmony_ci						&tbl_type, &rate_idx);
242962306a36Sopenharmony_ci
243062306a36Sopenharmony_ci		/* Indicate to uCode which entries might be MIMO.
243162306a36Sopenharmony_ci		 * If initial rate was MIMO, this will finally end up
243262306a36Sopenharmony_ci		 * as (IL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
243362306a36Sopenharmony_ci		if (is_mimo(tbl_type.lq_type))
243462306a36Sopenharmony_ci			lq_cmd->general_params.mimo_delimiter = idx;
243562306a36Sopenharmony_ci
243662306a36Sopenharmony_ci		/* Get next rate */
243762306a36Sopenharmony_ci		new_rate =
243862306a36Sopenharmony_ci		    il4965_rs_get_lower_rate(lq_sta, &tbl_type, rate_idx,
243962306a36Sopenharmony_ci					     use_ht_possible);
244062306a36Sopenharmony_ci
244162306a36Sopenharmony_ci		/* How many times should we repeat the next rate? */
244262306a36Sopenharmony_ci		if (is_legacy(tbl_type.lq_type)) {
244362306a36Sopenharmony_ci			if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
244462306a36Sopenharmony_ci				ant_toggle_cnt++;
244562306a36Sopenharmony_ci			else if (il &&
244662306a36Sopenharmony_ci				 il4965_rs_toggle_antenna(valid_tx_ant,
244762306a36Sopenharmony_ci							  &new_rate, &tbl_type))
244862306a36Sopenharmony_ci				ant_toggle_cnt = 1;
244962306a36Sopenharmony_ci
245062306a36Sopenharmony_ci			repeat_rate = IL_NUMBER_TRY;
245162306a36Sopenharmony_ci		} else {
245262306a36Sopenharmony_ci			repeat_rate = IL_HT_NUMBER_TRY;
245362306a36Sopenharmony_ci		}
245462306a36Sopenharmony_ci
245562306a36Sopenharmony_ci		/* Don't allow HT rates after next pass.
245662306a36Sopenharmony_ci		 * il4965_rs_get_lower_rate() will change type to LQ_A or LQ_G. */
245762306a36Sopenharmony_ci		use_ht_possible = 0;
245862306a36Sopenharmony_ci
245962306a36Sopenharmony_ci		/* Override next rate if needed for debug purposes */
246062306a36Sopenharmony_ci		il4965_rs_dbgfs_set_mcs(lq_sta, &new_rate, idx);
246162306a36Sopenharmony_ci
246262306a36Sopenharmony_ci		/* Fill next table entry */
246362306a36Sopenharmony_ci		lq_cmd->rs_table[idx].rate_n_flags = cpu_to_le32(new_rate);
246462306a36Sopenharmony_ci
246562306a36Sopenharmony_ci		idx++;
246662306a36Sopenharmony_ci		repeat_rate--;
246762306a36Sopenharmony_ci	}
246862306a36Sopenharmony_ci
246962306a36Sopenharmony_ci	lq_cmd->agg_params.agg_frame_cnt_limit = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
247062306a36Sopenharmony_ci	lq_cmd->agg_params.agg_dis_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
247162306a36Sopenharmony_ci
247262306a36Sopenharmony_ci	lq_cmd->agg_params.agg_time_limit =
247362306a36Sopenharmony_ci	    cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
247462306a36Sopenharmony_ci}
247562306a36Sopenharmony_ci
247662306a36Sopenharmony_cistatic void *
247762306a36Sopenharmony_ciil4965_rs_alloc(struct ieee80211_hw *hw)
247862306a36Sopenharmony_ci{
247962306a36Sopenharmony_ci	return hw->priv;
248062306a36Sopenharmony_ci}
248162306a36Sopenharmony_ci
248262306a36Sopenharmony_ci/* rate scale requires free function to be implemented */
248362306a36Sopenharmony_cistatic void
248462306a36Sopenharmony_ciil4965_rs_free(void *il_rate)
248562306a36Sopenharmony_ci{
248662306a36Sopenharmony_ci	return;
248762306a36Sopenharmony_ci}
248862306a36Sopenharmony_ci
248962306a36Sopenharmony_cistatic void
249062306a36Sopenharmony_ciil4965_rs_free_sta(void *il_r, struct ieee80211_sta *sta, void *il_sta)
249162306a36Sopenharmony_ci{
249262306a36Sopenharmony_ci	struct il_priv *il __maybe_unused = il_r;
249362306a36Sopenharmony_ci
249462306a36Sopenharmony_ci	D_RATE("enter\n");
249562306a36Sopenharmony_ci	D_RATE("leave\n");
249662306a36Sopenharmony_ci}
249762306a36Sopenharmony_ci
249862306a36Sopenharmony_ci#ifdef CONFIG_MAC80211_DEBUGFS
249962306a36Sopenharmony_ci
250062306a36Sopenharmony_cistatic void
250162306a36Sopenharmony_ciil4965_rs_dbgfs_set_mcs(struct il_lq_sta *lq_sta, u32 * rate_n_flags, int idx)
250262306a36Sopenharmony_ci{
250362306a36Sopenharmony_ci	struct il_priv *il;
250462306a36Sopenharmony_ci	u8 valid_tx_ant;
250562306a36Sopenharmony_ci	u8 ant_sel_tx;
250662306a36Sopenharmony_ci
250762306a36Sopenharmony_ci	il = lq_sta->drv;
250862306a36Sopenharmony_ci	valid_tx_ant = il->hw_params.valid_tx_ant;
250962306a36Sopenharmony_ci	if (lq_sta->dbg_fixed_rate) {
251062306a36Sopenharmony_ci		ant_sel_tx =
251162306a36Sopenharmony_ci		    ((lq_sta->
251262306a36Sopenharmony_ci		      dbg_fixed_rate & RATE_MCS_ANT_ABC_MSK) >>
251362306a36Sopenharmony_ci		     RATE_MCS_ANT_POS);
251462306a36Sopenharmony_ci		if ((valid_tx_ant & ant_sel_tx) == ant_sel_tx) {
251562306a36Sopenharmony_ci			*rate_n_flags = lq_sta->dbg_fixed_rate;
251662306a36Sopenharmony_ci			D_RATE("Fixed rate ON\n");
251762306a36Sopenharmony_ci		} else {
251862306a36Sopenharmony_ci			lq_sta->dbg_fixed_rate = 0;
251962306a36Sopenharmony_ci			IL_ERR
252062306a36Sopenharmony_ci			    ("Invalid antenna selection 0x%X, Valid is 0x%X\n",
252162306a36Sopenharmony_ci			     ant_sel_tx, valid_tx_ant);
252262306a36Sopenharmony_ci			D_RATE("Fixed rate OFF\n");
252362306a36Sopenharmony_ci		}
252462306a36Sopenharmony_ci	} else {
252562306a36Sopenharmony_ci		D_RATE("Fixed rate OFF\n");
252662306a36Sopenharmony_ci	}
252762306a36Sopenharmony_ci}
252862306a36Sopenharmony_ci
252962306a36Sopenharmony_cistatic ssize_t
253062306a36Sopenharmony_ciil4965_rs_sta_dbgfs_scale_table_write(struct file *file,
253162306a36Sopenharmony_ci				      const char __user *user_buf,
253262306a36Sopenharmony_ci				      size_t count, loff_t *ppos)
253362306a36Sopenharmony_ci{
253462306a36Sopenharmony_ci	struct il_lq_sta *lq_sta = file->private_data;
253562306a36Sopenharmony_ci	struct il_priv *il;
253662306a36Sopenharmony_ci	char buf[64];
253762306a36Sopenharmony_ci	size_t buf_size;
253862306a36Sopenharmony_ci	u32 parsed_rate;
253962306a36Sopenharmony_ci
254062306a36Sopenharmony_ci	il = lq_sta->drv;
254162306a36Sopenharmony_ci	memset(buf, 0, sizeof(buf));
254262306a36Sopenharmony_ci	buf_size = min(count, sizeof(buf) - 1);
254362306a36Sopenharmony_ci	if (copy_from_user(buf, user_buf, buf_size))
254462306a36Sopenharmony_ci		return -EFAULT;
254562306a36Sopenharmony_ci
254662306a36Sopenharmony_ci	if (sscanf(buf, "%x", &parsed_rate) == 1)
254762306a36Sopenharmony_ci		lq_sta->dbg_fixed_rate = parsed_rate;
254862306a36Sopenharmony_ci	else
254962306a36Sopenharmony_ci		lq_sta->dbg_fixed_rate = 0;
255062306a36Sopenharmony_ci
255162306a36Sopenharmony_ci	lq_sta->active_legacy_rate = 0x0FFF;	/* 1 - 54 MBits, includes CCK */
255262306a36Sopenharmony_ci	lq_sta->active_siso_rate = 0x1FD0;	/* 6 - 60 MBits, no 9, no CCK */
255362306a36Sopenharmony_ci	lq_sta->active_mimo2_rate = 0x1FD0;	/* 6 - 60 MBits, no 9, no CCK */
255462306a36Sopenharmony_ci
255562306a36Sopenharmony_ci	D_RATE("sta_id %d rate 0x%X\n", lq_sta->lq.sta_id,
255662306a36Sopenharmony_ci	       lq_sta->dbg_fixed_rate);
255762306a36Sopenharmony_ci
255862306a36Sopenharmony_ci	if (lq_sta->dbg_fixed_rate) {
255962306a36Sopenharmony_ci		il4965_rs_fill_link_cmd(NULL, lq_sta, lq_sta->dbg_fixed_rate);
256062306a36Sopenharmony_ci		il_send_lq_cmd(lq_sta->drv, &lq_sta->lq, CMD_ASYNC, false);
256162306a36Sopenharmony_ci	}
256262306a36Sopenharmony_ci
256362306a36Sopenharmony_ci	return count;
256462306a36Sopenharmony_ci}
256562306a36Sopenharmony_ci
256662306a36Sopenharmony_cistatic ssize_t
256762306a36Sopenharmony_ciil4965_rs_sta_dbgfs_scale_table_read(struct file *file, char __user *user_buf,
256862306a36Sopenharmony_ci				     size_t count, loff_t *ppos)
256962306a36Sopenharmony_ci{
257062306a36Sopenharmony_ci	char *buff;
257162306a36Sopenharmony_ci	int desc = 0;
257262306a36Sopenharmony_ci	int i = 0;
257362306a36Sopenharmony_ci	int idx = 0;
257462306a36Sopenharmony_ci	ssize_t ret;
257562306a36Sopenharmony_ci
257662306a36Sopenharmony_ci	struct il_lq_sta *lq_sta = file->private_data;
257762306a36Sopenharmony_ci	struct il_priv *il;
257862306a36Sopenharmony_ci	struct il_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
257962306a36Sopenharmony_ci
258062306a36Sopenharmony_ci	il = lq_sta->drv;
258162306a36Sopenharmony_ci	buff = kmalloc(1024, GFP_KERNEL);
258262306a36Sopenharmony_ci	if (!buff)
258362306a36Sopenharmony_ci		return -ENOMEM;
258462306a36Sopenharmony_ci
258562306a36Sopenharmony_ci	desc += sprintf(buff + desc, "sta_id %d\n", lq_sta->lq.sta_id);
258662306a36Sopenharmony_ci	desc +=
258762306a36Sopenharmony_ci	    sprintf(buff + desc, "failed=%d success=%d rate=0%X\n",
258862306a36Sopenharmony_ci		    lq_sta->total_failed, lq_sta->total_success,
258962306a36Sopenharmony_ci		    lq_sta->active_legacy_rate);
259062306a36Sopenharmony_ci	desc +=
259162306a36Sopenharmony_ci	    sprintf(buff + desc, "fixed rate 0x%X\n", lq_sta->dbg_fixed_rate);
259262306a36Sopenharmony_ci	desc +=
259362306a36Sopenharmony_ci	    sprintf(buff + desc, "valid_tx_ant %s%s%s\n",
259462306a36Sopenharmony_ci		    (il->hw_params.valid_tx_ant & ANT_A) ? "ANT_A," : "",
259562306a36Sopenharmony_ci		    (il->hw_params.valid_tx_ant & ANT_B) ? "ANT_B," : "",
259662306a36Sopenharmony_ci		    (il->hw_params.valid_tx_ant & ANT_C) ? "ANT_C" : "");
259762306a36Sopenharmony_ci	desc +=
259862306a36Sopenharmony_ci	    sprintf(buff + desc, "lq type %s\n",
259962306a36Sopenharmony_ci		    (is_legacy(tbl->lq_type)) ? "legacy" : "HT");
260062306a36Sopenharmony_ci	if (is_Ht(tbl->lq_type)) {
260162306a36Sopenharmony_ci		desc +=
260262306a36Sopenharmony_ci		    sprintf(buff + desc, " %s",
260362306a36Sopenharmony_ci			    (is_siso(tbl->lq_type)) ? "SISO" : "MIMO2");
260462306a36Sopenharmony_ci		desc +=
260562306a36Sopenharmony_ci		    sprintf(buff + desc, " %s",
260662306a36Sopenharmony_ci			    (tbl->is_ht40) ? "40MHz" : "20MHz");
260762306a36Sopenharmony_ci		desc +=
260862306a36Sopenharmony_ci		    sprintf(buff + desc, " %s %s %s\n",
260962306a36Sopenharmony_ci			    (tbl->is_SGI) ? "SGI" : "",
261062306a36Sopenharmony_ci			    (lq_sta->is_green) ? "GF enabled" : "",
261162306a36Sopenharmony_ci			    (lq_sta->is_agg) ? "AGG on" : "");
261262306a36Sopenharmony_ci	}
261362306a36Sopenharmony_ci	desc +=
261462306a36Sopenharmony_ci	    sprintf(buff + desc, "last tx rate=0x%X\n",
261562306a36Sopenharmony_ci		    lq_sta->last_rate_n_flags);
261662306a36Sopenharmony_ci	desc +=
261762306a36Sopenharmony_ci	    sprintf(buff + desc,
261862306a36Sopenharmony_ci		    "general:" "flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
261962306a36Sopenharmony_ci		    lq_sta->lq.general_params.flags,
262062306a36Sopenharmony_ci		    lq_sta->lq.general_params.mimo_delimiter,
262162306a36Sopenharmony_ci		    lq_sta->lq.general_params.single_stream_ant_msk,
262262306a36Sopenharmony_ci		    lq_sta->lq.general_params.dual_stream_ant_msk);
262362306a36Sopenharmony_ci
262462306a36Sopenharmony_ci	desc +=
262562306a36Sopenharmony_ci	    sprintf(buff + desc,
262662306a36Sopenharmony_ci		    "agg:"
262762306a36Sopenharmony_ci		    "time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
262862306a36Sopenharmony_ci		    le16_to_cpu(lq_sta->lq.agg_params.agg_time_limit),
262962306a36Sopenharmony_ci		    lq_sta->lq.agg_params.agg_dis_start_th,
263062306a36Sopenharmony_ci		    lq_sta->lq.agg_params.agg_frame_cnt_limit);
263162306a36Sopenharmony_ci
263262306a36Sopenharmony_ci	desc +=
263362306a36Sopenharmony_ci	    sprintf(buff + desc,
263462306a36Sopenharmony_ci		    "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
263562306a36Sopenharmony_ci		    lq_sta->lq.general_params.start_rate_idx[0],
263662306a36Sopenharmony_ci		    lq_sta->lq.general_params.start_rate_idx[1],
263762306a36Sopenharmony_ci		    lq_sta->lq.general_params.start_rate_idx[2],
263862306a36Sopenharmony_ci		    lq_sta->lq.general_params.start_rate_idx[3]);
263962306a36Sopenharmony_ci
264062306a36Sopenharmony_ci	for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
264162306a36Sopenharmony_ci		idx =
264262306a36Sopenharmony_ci		    il4965_hwrate_to_plcp_idx(le32_to_cpu
264362306a36Sopenharmony_ci					      (lq_sta->lq.rs_table[i].
264462306a36Sopenharmony_ci					       rate_n_flags));
264562306a36Sopenharmony_ci		if (is_legacy(tbl->lq_type)) {
264662306a36Sopenharmony_ci			desc +=
264762306a36Sopenharmony_ci			    sprintf(buff + desc, " rate[%d] 0x%X %smbps\n", i,
264862306a36Sopenharmony_ci				    le32_to_cpu(lq_sta->lq.rs_table[i].
264962306a36Sopenharmony_ci						rate_n_flags),
265062306a36Sopenharmony_ci				    il_rate_mcs[idx].mbps);
265162306a36Sopenharmony_ci		} else {
265262306a36Sopenharmony_ci			desc +=
265362306a36Sopenharmony_ci			    sprintf(buff + desc, " rate[%d] 0x%X %smbps (%s)\n",
265462306a36Sopenharmony_ci				    i,
265562306a36Sopenharmony_ci				    le32_to_cpu(lq_sta->lq.rs_table[i].
265662306a36Sopenharmony_ci						rate_n_flags),
265762306a36Sopenharmony_ci				    il_rate_mcs[idx].mbps,
265862306a36Sopenharmony_ci				    il_rate_mcs[idx].mcs);
265962306a36Sopenharmony_ci		}
266062306a36Sopenharmony_ci	}
266162306a36Sopenharmony_ci
266262306a36Sopenharmony_ci	ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
266362306a36Sopenharmony_ci	kfree(buff);
266462306a36Sopenharmony_ci	return ret;
266562306a36Sopenharmony_ci}
266662306a36Sopenharmony_ci
266762306a36Sopenharmony_cistatic const struct file_operations rs_sta_dbgfs_scale_table_ops = {
266862306a36Sopenharmony_ci	.write = il4965_rs_sta_dbgfs_scale_table_write,
266962306a36Sopenharmony_ci	.read = il4965_rs_sta_dbgfs_scale_table_read,
267062306a36Sopenharmony_ci	.open = simple_open,
267162306a36Sopenharmony_ci	.llseek = default_llseek,
267262306a36Sopenharmony_ci};
267362306a36Sopenharmony_ci
267462306a36Sopenharmony_cistatic ssize_t
267562306a36Sopenharmony_ciil4965_rs_sta_dbgfs_stats_table_read(struct file *file, char __user *user_buf,
267662306a36Sopenharmony_ci				     size_t count, loff_t *ppos)
267762306a36Sopenharmony_ci{
267862306a36Sopenharmony_ci	char *buff;
267962306a36Sopenharmony_ci	int desc = 0;
268062306a36Sopenharmony_ci	int i, j;
268162306a36Sopenharmony_ci	ssize_t ret;
268262306a36Sopenharmony_ci
268362306a36Sopenharmony_ci	struct il_lq_sta *lq_sta = file->private_data;
268462306a36Sopenharmony_ci
268562306a36Sopenharmony_ci	buff = kmalloc(1024, GFP_KERNEL);
268662306a36Sopenharmony_ci	if (!buff)
268762306a36Sopenharmony_ci		return -ENOMEM;
268862306a36Sopenharmony_ci
268962306a36Sopenharmony_ci	for (i = 0; i < LQ_SIZE; i++) {
269062306a36Sopenharmony_ci		desc +=
269162306a36Sopenharmony_ci		    sprintf(buff + desc,
269262306a36Sopenharmony_ci			    "%s type=%d SGI=%d HT40=%d DUP=%d GF=%d\n"
269362306a36Sopenharmony_ci			    "rate=0x%X\n", lq_sta->active_tbl == i ? "*" : "x",
269462306a36Sopenharmony_ci			    lq_sta->lq_info[i].lq_type,
269562306a36Sopenharmony_ci			    lq_sta->lq_info[i].is_SGI,
269662306a36Sopenharmony_ci			    lq_sta->lq_info[i].is_ht40,
269762306a36Sopenharmony_ci			    lq_sta->lq_info[i].is_dup, lq_sta->is_green,
269862306a36Sopenharmony_ci			    lq_sta->lq_info[i].current_rate);
269962306a36Sopenharmony_ci		for (j = 0; j < RATE_COUNT; j++) {
270062306a36Sopenharmony_ci			desc +=
270162306a36Sopenharmony_ci			    sprintf(buff + desc,
270262306a36Sopenharmony_ci				    "counter=%d success=%d %%=%d\n",
270362306a36Sopenharmony_ci				    lq_sta->lq_info[i].win[j].counter,
270462306a36Sopenharmony_ci				    lq_sta->lq_info[i].win[j].success_counter,
270562306a36Sopenharmony_ci				    lq_sta->lq_info[i].win[j].success_ratio);
270662306a36Sopenharmony_ci		}
270762306a36Sopenharmony_ci	}
270862306a36Sopenharmony_ci	ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
270962306a36Sopenharmony_ci	kfree(buff);
271062306a36Sopenharmony_ci	return ret;
271162306a36Sopenharmony_ci}
271262306a36Sopenharmony_ci
271362306a36Sopenharmony_cistatic const struct file_operations rs_sta_dbgfs_stats_table_ops = {
271462306a36Sopenharmony_ci	.read = il4965_rs_sta_dbgfs_stats_table_read,
271562306a36Sopenharmony_ci	.open = simple_open,
271662306a36Sopenharmony_ci	.llseek = default_llseek,
271762306a36Sopenharmony_ci};
271862306a36Sopenharmony_ci
271962306a36Sopenharmony_cistatic ssize_t
272062306a36Sopenharmony_ciil4965_rs_sta_dbgfs_rate_scale_data_read(struct file *file,
272162306a36Sopenharmony_ci					 char __user *user_buf, size_t count,
272262306a36Sopenharmony_ci					 loff_t *ppos)
272362306a36Sopenharmony_ci{
272462306a36Sopenharmony_ci	char buff[120];
272562306a36Sopenharmony_ci	int desc = 0;
272662306a36Sopenharmony_ci	struct il_lq_sta *lq_sta = file->private_data;
272762306a36Sopenharmony_ci	struct il_scale_tbl_info *tbl = &lq_sta->lq_info[lq_sta->active_tbl];
272862306a36Sopenharmony_ci
272962306a36Sopenharmony_ci	if (is_Ht(tbl->lq_type))
273062306a36Sopenharmony_ci		desc +=
273162306a36Sopenharmony_ci		    sprintf(buff + desc, "Bit Rate= %d Mb/s\n",
273262306a36Sopenharmony_ci			    tbl->expected_tpt[lq_sta->last_txrate_idx]);
273362306a36Sopenharmony_ci	else
273462306a36Sopenharmony_ci		desc +=
273562306a36Sopenharmony_ci		    sprintf(buff + desc, "Bit Rate= %d Mb/s\n",
273662306a36Sopenharmony_ci			    il_rates[lq_sta->last_txrate_idx].ieee >> 1);
273762306a36Sopenharmony_ci
273862306a36Sopenharmony_ci	return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
273962306a36Sopenharmony_ci}
274062306a36Sopenharmony_ci
274162306a36Sopenharmony_cistatic const struct file_operations rs_sta_dbgfs_rate_scale_data_ops = {
274262306a36Sopenharmony_ci	.read = il4965_rs_sta_dbgfs_rate_scale_data_read,
274362306a36Sopenharmony_ci	.open = simple_open,
274462306a36Sopenharmony_ci	.llseek = default_llseek,
274562306a36Sopenharmony_ci};
274662306a36Sopenharmony_ci
274762306a36Sopenharmony_cistatic void
274862306a36Sopenharmony_ciil4965_rs_add_debugfs(void *il, void *il_sta, struct dentry *dir)
274962306a36Sopenharmony_ci{
275062306a36Sopenharmony_ci	struct il_lq_sta *lq_sta = il_sta;
275162306a36Sopenharmony_ci
275262306a36Sopenharmony_ci	debugfs_create_file("rate_scale_table", 0600, dir, lq_sta,
275362306a36Sopenharmony_ci			    &rs_sta_dbgfs_scale_table_ops);
275462306a36Sopenharmony_ci	debugfs_create_file("rate_stats_table", 0400, dir, lq_sta,
275562306a36Sopenharmony_ci			    &rs_sta_dbgfs_stats_table_ops);
275662306a36Sopenharmony_ci	debugfs_create_file("rate_scale_data", 0400, dir, lq_sta,
275762306a36Sopenharmony_ci			    &rs_sta_dbgfs_rate_scale_data_ops);
275862306a36Sopenharmony_ci	debugfs_create_u8("tx_agg_tid_enable", 0600, dir,
275962306a36Sopenharmony_ci			  &lq_sta->tx_agg_tid_en);
276062306a36Sopenharmony_ci}
276162306a36Sopenharmony_ci#endif
276262306a36Sopenharmony_ci
276362306a36Sopenharmony_ci/*
276462306a36Sopenharmony_ci * Initialization of rate scaling information is done by driver after
276562306a36Sopenharmony_ci * the station is added. Since mac80211 calls this function before a
276662306a36Sopenharmony_ci * station is added we ignore it.
276762306a36Sopenharmony_ci */
276862306a36Sopenharmony_cistatic void
276962306a36Sopenharmony_ciil4965_rs_rate_init_stub(void *il_r, struct ieee80211_supported_band *sband,
277062306a36Sopenharmony_ci			 struct cfg80211_chan_def *chandef,
277162306a36Sopenharmony_ci			 struct ieee80211_sta *sta, void *il_sta)
277262306a36Sopenharmony_ci{
277362306a36Sopenharmony_ci}
277462306a36Sopenharmony_ci
277562306a36Sopenharmony_cistatic const struct rate_control_ops rs_4965_ops = {
277662306a36Sopenharmony_ci	.name = IL4965_RS_NAME,
277762306a36Sopenharmony_ci	.tx_status = il4965_rs_tx_status,
277862306a36Sopenharmony_ci	.get_rate = il4965_rs_get_rate,
277962306a36Sopenharmony_ci	.rate_init = il4965_rs_rate_init_stub,
278062306a36Sopenharmony_ci	.alloc = il4965_rs_alloc,
278162306a36Sopenharmony_ci	.free = il4965_rs_free,
278262306a36Sopenharmony_ci	.alloc_sta = il4965_rs_alloc_sta,
278362306a36Sopenharmony_ci	.free_sta = il4965_rs_free_sta,
278462306a36Sopenharmony_ci#ifdef CONFIG_MAC80211_DEBUGFS
278562306a36Sopenharmony_ci	.add_sta_debugfs = il4965_rs_add_debugfs,
278662306a36Sopenharmony_ci#endif
278762306a36Sopenharmony_ci};
278862306a36Sopenharmony_ci
278962306a36Sopenharmony_ciint
279062306a36Sopenharmony_ciil4965_rate_control_register(void)
279162306a36Sopenharmony_ci{
279262306a36Sopenharmony_ci	return ieee80211_rate_control_register(&rs_4965_ops);
279362306a36Sopenharmony_ci}
279462306a36Sopenharmony_ci
279562306a36Sopenharmony_civoid
279662306a36Sopenharmony_ciil4965_rate_control_unregister(void)
279762306a36Sopenharmony_ci{
279862306a36Sopenharmony_ci	ieee80211_rate_control_unregister(&rs_4965_ops);
279962306a36Sopenharmony_ci}
2800