18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/******************************************************************************
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Copyright(c) 2005 - 2011 Intel Corporation. All rights reserved.
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Contact Information:
78c2ecf20Sopenharmony_ci *  Intel Linux Wireless <ilw@linux.intel.com>
88c2ecf20Sopenharmony_ci * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci *****************************************************************************/
118c2ecf20Sopenharmony_ci#include <linux/kernel.h>
128c2ecf20Sopenharmony_ci#include <linux/skbuff.h>
138c2ecf20Sopenharmony_ci#include <linux/slab.h>
148c2ecf20Sopenharmony_ci#include <net/mac80211.h>
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#include <linux/netdevice.h>
178c2ecf20Sopenharmony_ci#include <linux/etherdevice.h>
188c2ecf20Sopenharmony_ci#include <linux/delay.h>
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#include <linux/workqueue.h>
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#include "common.h"
238c2ecf20Sopenharmony_ci#include "4965.h"
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci#define IL4965_RS_NAME "iwl-4965-rs"
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci#define NUM_TRY_BEFORE_ANT_TOGGLE 1
288c2ecf20Sopenharmony_ci#define IL_NUMBER_TRY      1
298c2ecf20Sopenharmony_ci#define IL_HT_NUMBER_TRY   3
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#define RATE_MAX_WINDOW		62	/* # tx in history win */
328c2ecf20Sopenharmony_ci#define RATE_MIN_FAILURE_TH		6	/* min failures to calc tpt */
338c2ecf20Sopenharmony_ci#define RATE_MIN_SUCCESS_TH		8	/* min successes to calc tpt */
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci/* max allowed rate miss before sync LQ cmd */
368c2ecf20Sopenharmony_ci#define IL_MISSED_RATE_MAX		15
378c2ecf20Sopenharmony_ci/* max time to accum history 2 seconds */
388c2ecf20Sopenharmony_ci#define RATE_SCALE_FLUSH_INTVL   (3*HZ)
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_cistatic u8 rs_ht_to_legacy[] = {
418c2ecf20Sopenharmony_ci	RATE_6M_IDX, RATE_6M_IDX,
428c2ecf20Sopenharmony_ci	RATE_6M_IDX, RATE_6M_IDX,
438c2ecf20Sopenharmony_ci	RATE_6M_IDX,
448c2ecf20Sopenharmony_ci	RATE_6M_IDX, RATE_9M_IDX,
458c2ecf20Sopenharmony_ci	RATE_12M_IDX, RATE_18M_IDX,
468c2ecf20Sopenharmony_ci	RATE_24M_IDX, RATE_36M_IDX,
478c2ecf20Sopenharmony_ci	RATE_48M_IDX, RATE_54M_IDX
488c2ecf20Sopenharmony_ci};
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_cistatic const u8 ant_toggle_lookup[] = {
518c2ecf20Sopenharmony_ci	/*ANT_NONE -> */ ANT_NONE,
528c2ecf20Sopenharmony_ci	/*ANT_A    -> */ ANT_B,
538c2ecf20Sopenharmony_ci	/*ANT_B    -> */ ANT_C,
548c2ecf20Sopenharmony_ci	/*ANT_AB   -> */ ANT_BC,
558c2ecf20Sopenharmony_ci	/*ANT_C    -> */ ANT_A,
568c2ecf20Sopenharmony_ci	/*ANT_AC   -> */ ANT_AB,
578c2ecf20Sopenharmony_ci	/*ANT_BC   -> */ ANT_AC,
588c2ecf20Sopenharmony_ci	/*ANT_ABC  -> */ ANT_ABC,
598c2ecf20Sopenharmony_ci};
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci#define IL_DECLARE_RATE_INFO(r, s, ip, in, rp, rn, pp, np)    \
628c2ecf20Sopenharmony_ci	[RATE_##r##M_IDX] = { RATE_##r##M_PLCP,      \
638c2ecf20Sopenharmony_ci				    RATE_SISO_##s##M_PLCP, \
648c2ecf20Sopenharmony_ci				    RATE_MIMO2_##s##M_PLCP,\
658c2ecf20Sopenharmony_ci				    RATE_##r##M_IEEE,      \
668c2ecf20Sopenharmony_ci				    RATE_##ip##M_IDX,    \
678c2ecf20Sopenharmony_ci				    RATE_##in##M_IDX,    \
688c2ecf20Sopenharmony_ci				    RATE_##rp##M_IDX,    \
698c2ecf20Sopenharmony_ci				    RATE_##rn##M_IDX,    \
708c2ecf20Sopenharmony_ci				    RATE_##pp##M_IDX,    \
718c2ecf20Sopenharmony_ci				    RATE_##np##M_IDX }
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci/*
748c2ecf20Sopenharmony_ci * Parameter order:
758c2ecf20Sopenharmony_ci *   rate, ht rate, prev rate, next rate, prev tgg rate, next tgg rate
768c2ecf20Sopenharmony_ci *
778c2ecf20Sopenharmony_ci * If there isn't a valid next or previous rate then INV is used which
788c2ecf20Sopenharmony_ci * maps to RATE_INVALID
798c2ecf20Sopenharmony_ci *
808c2ecf20Sopenharmony_ci */
818c2ecf20Sopenharmony_ciconst struct il_rate_info il_rates[RATE_COUNT] = {
828c2ecf20Sopenharmony_ci	IL_DECLARE_RATE_INFO(1, INV, INV, 2, INV, 2, INV, 2),	/*  1mbps */
838c2ecf20Sopenharmony_ci	IL_DECLARE_RATE_INFO(2, INV, 1, 5, 1, 5, 1, 5),		/*  2mbps */
848c2ecf20Sopenharmony_ci	IL_DECLARE_RATE_INFO(5, INV, 2, 6, 2, 11, 2, 11),	/*5.5mbps */
858c2ecf20Sopenharmony_ci	IL_DECLARE_RATE_INFO(11, INV, 9, 12, 9, 12, 5, 18),	/* 11mbps */
868c2ecf20Sopenharmony_ci	IL_DECLARE_RATE_INFO(6, 6, 5, 9, 5, 11, 5, 11),		/*  6mbps */
878c2ecf20Sopenharmony_ci	IL_DECLARE_RATE_INFO(9, 6, 6, 11, 6, 11, 5, 11),	/*  9mbps */
888c2ecf20Sopenharmony_ci	IL_DECLARE_RATE_INFO(12, 12, 11, 18, 11, 18, 11, 18),	/* 12mbps */
898c2ecf20Sopenharmony_ci	IL_DECLARE_RATE_INFO(18, 18, 12, 24, 12, 24, 11, 24),	/* 18mbps */
908c2ecf20Sopenharmony_ci	IL_DECLARE_RATE_INFO(24, 24, 18, 36, 18, 36, 18, 36),	/* 24mbps */
918c2ecf20Sopenharmony_ci	IL_DECLARE_RATE_INFO(36, 36, 24, 48, 24, 48, 24, 48),	/* 36mbps */
928c2ecf20Sopenharmony_ci	IL_DECLARE_RATE_INFO(48, 48, 36, 54, 36, 54, 36, 54),	/* 48mbps */
938c2ecf20Sopenharmony_ci	IL_DECLARE_RATE_INFO(54, 54, 48, INV, 48, INV, 48, INV),/* 54mbps */
948c2ecf20Sopenharmony_ci	IL_DECLARE_RATE_INFO(60, 60, 48, INV, 48, INV, 48, INV),/* 60mbps */
958c2ecf20Sopenharmony_ci};
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_cistatic int
988c2ecf20Sopenharmony_ciil4965_hwrate_to_plcp_idx(u32 rate_n_flags)
998c2ecf20Sopenharmony_ci{
1008c2ecf20Sopenharmony_ci	int idx = 0;
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci	/* HT rate format */
1038c2ecf20Sopenharmony_ci	if (rate_n_flags & RATE_MCS_HT_MSK) {
1048c2ecf20Sopenharmony_ci		idx = (rate_n_flags & 0xff);
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci		if (idx >= RATE_MIMO2_6M_PLCP)
1078c2ecf20Sopenharmony_ci			idx = idx - RATE_MIMO2_6M_PLCP;
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci		idx += IL_FIRST_OFDM_RATE;
1108c2ecf20Sopenharmony_ci		/* skip 9M not supported in ht */
1118c2ecf20Sopenharmony_ci		if (idx >= RATE_9M_IDX)
1128c2ecf20Sopenharmony_ci			idx += 1;
1138c2ecf20Sopenharmony_ci		if (idx >= IL_FIRST_OFDM_RATE && idx <= IL_LAST_OFDM_RATE)
1148c2ecf20Sopenharmony_ci			return idx;
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci		/* legacy rate format, search for match in table */
1178c2ecf20Sopenharmony_ci	} else {
1188c2ecf20Sopenharmony_ci		for (idx = 0; idx < ARRAY_SIZE(il_rates); idx++)
1198c2ecf20Sopenharmony_ci			if (il_rates[idx].plcp == (rate_n_flags & 0xFF))
1208c2ecf20Sopenharmony_ci				return idx;
1218c2ecf20Sopenharmony_ci	}
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci	return -1;
1248c2ecf20Sopenharmony_ci}
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_cistatic void il4965_rs_rate_scale_perform(struct il_priv *il,
1278c2ecf20Sopenharmony_ci					 struct sk_buff *skb,
1288c2ecf20Sopenharmony_ci					 struct ieee80211_sta *sta,
1298c2ecf20Sopenharmony_ci					 struct il_lq_sta *lq_sta);
1308c2ecf20Sopenharmony_cistatic void il4965_rs_fill_link_cmd(struct il_priv *il,
1318c2ecf20Sopenharmony_ci				    struct il_lq_sta *lq_sta, u32 rate_n_flags);
1328c2ecf20Sopenharmony_cistatic void il4965_rs_stay_in_table(struct il_lq_sta *lq_sta,
1338c2ecf20Sopenharmony_ci				    bool force_search);
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci#ifdef CONFIG_MAC80211_DEBUGFS
1368c2ecf20Sopenharmony_cistatic void il4965_rs_dbgfs_set_mcs(struct il_lq_sta *lq_sta,
1378c2ecf20Sopenharmony_ci				    u32 *rate_n_flags, int idx);
1388c2ecf20Sopenharmony_ci#else
1398c2ecf20Sopenharmony_cistatic void
1408c2ecf20Sopenharmony_ciil4965_rs_dbgfs_set_mcs(struct il_lq_sta *lq_sta, u32 * rate_n_flags, int idx)
1418c2ecf20Sopenharmony_ci{
1428c2ecf20Sopenharmony_ci}
1438c2ecf20Sopenharmony_ci#endif
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci/*
1468c2ecf20Sopenharmony_ci * The following tables contain the expected throughput metrics for all rates
1478c2ecf20Sopenharmony_ci *
1488c2ecf20Sopenharmony_ci *	1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
1498c2ecf20Sopenharmony_ci *
1508c2ecf20Sopenharmony_ci * where invalid entries are zeros.
1518c2ecf20Sopenharmony_ci *
1528c2ecf20Sopenharmony_ci * CCK rates are only valid in legacy table and will only be used in G
1538c2ecf20Sopenharmony_ci * (2.4 GHz) band.
1548c2ecf20Sopenharmony_ci */
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_cistatic s32 expected_tpt_legacy[RATE_COUNT] = {
1578c2ecf20Sopenharmony_ci	7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 0
1588c2ecf20Sopenharmony_ci};
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_cistatic s32 expected_tpt_siso20MHz[4][RATE_COUNT] = {
1618c2ecf20Sopenharmony_ci	{0, 0, 0, 0, 42, 0, 76, 102, 124, 158, 183, 193, 202},	/* Norm */
1628c2ecf20Sopenharmony_ci	{0, 0, 0, 0, 46, 0, 82, 110, 132, 167, 192, 202, 210},	/* SGI */
1638c2ecf20Sopenharmony_ci	{0, 0, 0, 0, 48, 0, 93, 135, 176, 251, 319, 351, 381},	/* AGG */
1648c2ecf20Sopenharmony_ci	{0, 0, 0, 0, 53, 0, 102, 149, 193, 275, 348, 381, 413},	/* AGG+SGI */
1658c2ecf20Sopenharmony_ci};
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_cistatic s32 expected_tpt_siso40MHz[4][RATE_COUNT] = {
1688c2ecf20Sopenharmony_ci	{0, 0, 0, 0, 77, 0, 127, 160, 184, 220, 242, 250, 257},	/* Norm */
1698c2ecf20Sopenharmony_ci	{0, 0, 0, 0, 83, 0, 135, 169, 193, 229, 250, 257, 264},	/* SGI */
1708c2ecf20Sopenharmony_ci	{0, 0, 0, 0, 96, 0, 182, 259, 328, 451, 553, 598, 640},	/* AGG */
1718c2ecf20Sopenharmony_ci	{0, 0, 0, 0, 106, 0, 199, 282, 357, 487, 593, 640, 683},	/* AGG+SGI */
1728c2ecf20Sopenharmony_ci};
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_cistatic s32 expected_tpt_mimo2_20MHz[4][RATE_COUNT] = {
1758c2ecf20Sopenharmony_ci	{0, 0, 0, 0, 74, 0, 123, 155, 179, 213, 235, 243, 250},	/* Norm */
1768c2ecf20Sopenharmony_ci	{0, 0, 0, 0, 81, 0, 131, 164, 187, 221, 242, 250, 256},	/* SGI */
1778c2ecf20Sopenharmony_ci	{0, 0, 0, 0, 92, 0, 175, 250, 317, 436, 534, 578, 619},	/* AGG */
1788c2ecf20Sopenharmony_ci	{0, 0, 0, 0, 102, 0, 192, 273, 344, 470, 573, 619, 660},	/* AGG+SGI */
1798c2ecf20Sopenharmony_ci};
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_cistatic s32 expected_tpt_mimo2_40MHz[4][RATE_COUNT] = {
1828c2ecf20Sopenharmony_ci	{0, 0, 0, 0, 123, 0, 182, 214, 235, 264, 279, 285, 289},	/* Norm */
1838c2ecf20Sopenharmony_ci	{0, 0, 0, 0, 131, 0, 191, 222, 242, 270, 284, 289, 293},	/* SGI */
1848c2ecf20Sopenharmony_ci	{0, 0, 0, 0, 180, 0, 327, 446, 545, 708, 828, 878, 922},	/* AGG */
1858c2ecf20Sopenharmony_ci	{0, 0, 0, 0, 197, 0, 355, 481, 584, 752, 872, 922, 966},	/* AGG+SGI */
1868c2ecf20Sopenharmony_ci};
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci/* mbps, mcs */
1898c2ecf20Sopenharmony_cistatic const struct il_rate_mcs_info il_rate_mcs[RATE_COUNT] = {
1908c2ecf20Sopenharmony_ci	{"1", "BPSK DSSS"},
1918c2ecf20Sopenharmony_ci	{"2", "QPSK DSSS"},
1928c2ecf20Sopenharmony_ci	{"5.5", "BPSK CCK"},
1938c2ecf20Sopenharmony_ci	{"11", "QPSK CCK"},
1948c2ecf20Sopenharmony_ci	{"6", "BPSK 1/2"},
1958c2ecf20Sopenharmony_ci	{"9", "BPSK 1/2"},
1968c2ecf20Sopenharmony_ci	{"12", "QPSK 1/2"},
1978c2ecf20Sopenharmony_ci	{"18", "QPSK 3/4"},
1988c2ecf20Sopenharmony_ci	{"24", "16QAM 1/2"},
1998c2ecf20Sopenharmony_ci	{"36", "16QAM 3/4"},
2008c2ecf20Sopenharmony_ci	{"48", "64QAM 2/3"},
2018c2ecf20Sopenharmony_ci	{"54", "64QAM 3/4"},
2028c2ecf20Sopenharmony_ci	{"60", "64QAM 5/6"},
2038c2ecf20Sopenharmony_ci};
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci#define MCS_IDX_PER_STREAM	(8)
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_cistatic inline u8
2088c2ecf20Sopenharmony_ciil4965_rs_extract_rate(u32 rate_n_flags)
2098c2ecf20Sopenharmony_ci{
2108c2ecf20Sopenharmony_ci	return (u8) (rate_n_flags & 0xFF);
2118c2ecf20Sopenharmony_ci}
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_cistatic void
2148c2ecf20Sopenharmony_ciil4965_rs_rate_scale_clear_win(struct il_rate_scale_data *win)
2158c2ecf20Sopenharmony_ci{
2168c2ecf20Sopenharmony_ci	win->data = 0;
2178c2ecf20Sopenharmony_ci	win->success_counter = 0;
2188c2ecf20Sopenharmony_ci	win->success_ratio = IL_INVALID_VALUE;
2198c2ecf20Sopenharmony_ci	win->counter = 0;
2208c2ecf20Sopenharmony_ci	win->average_tpt = IL_INVALID_VALUE;
2218c2ecf20Sopenharmony_ci	win->stamp = 0;
2228c2ecf20Sopenharmony_ci}
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_cistatic inline u8
2258c2ecf20Sopenharmony_ciil4965_rs_is_valid_ant(u8 valid_antenna, u8 ant_type)
2268c2ecf20Sopenharmony_ci{
2278c2ecf20Sopenharmony_ci	return (ant_type & valid_antenna) == ant_type;
2288c2ecf20Sopenharmony_ci}
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci/*
2318c2ecf20Sopenharmony_ci *	removes the old data from the stats. All data that is older than
2328c2ecf20Sopenharmony_ci *	TID_MAX_TIME_DIFF, will be deleted.
2338c2ecf20Sopenharmony_ci */
2348c2ecf20Sopenharmony_cistatic void
2358c2ecf20Sopenharmony_ciil4965_rs_tl_rm_old_stats(struct il_traffic_load *tl, u32 curr_time)
2368c2ecf20Sopenharmony_ci{
2378c2ecf20Sopenharmony_ci	/* The oldest age we want to keep */
2388c2ecf20Sopenharmony_ci	u32 oldest_time = curr_time - TID_MAX_TIME_DIFF;
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci	while (tl->queue_count && tl->time_stamp < oldest_time) {
2418c2ecf20Sopenharmony_ci		tl->total -= tl->packet_count[tl->head];
2428c2ecf20Sopenharmony_ci		tl->packet_count[tl->head] = 0;
2438c2ecf20Sopenharmony_ci		tl->time_stamp += TID_QUEUE_CELL_SPACING;
2448c2ecf20Sopenharmony_ci		tl->queue_count--;
2458c2ecf20Sopenharmony_ci		tl->head++;
2468c2ecf20Sopenharmony_ci		if (tl->head >= TID_QUEUE_MAX_SIZE)
2478c2ecf20Sopenharmony_ci			tl->head = 0;
2488c2ecf20Sopenharmony_ci	}
2498c2ecf20Sopenharmony_ci}
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci/*
2528c2ecf20Sopenharmony_ci *	increment traffic load value for tid and also remove
2538c2ecf20Sopenharmony_ci *	any old values if passed the certain time period
2548c2ecf20Sopenharmony_ci */
2558c2ecf20Sopenharmony_cistatic u8
2568c2ecf20Sopenharmony_ciil4965_rs_tl_add_packet(struct il_lq_sta *lq_data, struct ieee80211_hdr *hdr)
2578c2ecf20Sopenharmony_ci{
2588c2ecf20Sopenharmony_ci	u32 curr_time = jiffies_to_msecs(jiffies);
2598c2ecf20Sopenharmony_ci	u32 time_diff;
2608c2ecf20Sopenharmony_ci	s32 idx;
2618c2ecf20Sopenharmony_ci	struct il_traffic_load *tl = NULL;
2628c2ecf20Sopenharmony_ci	u8 tid;
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ci	if (ieee80211_is_data_qos(hdr->frame_control)) {
2658c2ecf20Sopenharmony_ci		u8 *qc = ieee80211_get_qos_ctl(hdr);
2668c2ecf20Sopenharmony_ci		tid = qc[0] & 0xf;
2678c2ecf20Sopenharmony_ci	} else
2688c2ecf20Sopenharmony_ci		return MAX_TID_COUNT;
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci	if (unlikely(tid >= TID_MAX_LOAD_COUNT))
2718c2ecf20Sopenharmony_ci		return MAX_TID_COUNT;
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci	tl = &lq_data->load[tid];
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ci	curr_time -= curr_time % TID_ROUND_VALUE;
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ci	/* Happens only for the first packet. Initialize the data */
2788c2ecf20Sopenharmony_ci	if (!(tl->queue_count)) {
2798c2ecf20Sopenharmony_ci		tl->total = 1;
2808c2ecf20Sopenharmony_ci		tl->time_stamp = curr_time;
2818c2ecf20Sopenharmony_ci		tl->queue_count = 1;
2828c2ecf20Sopenharmony_ci		tl->head = 0;
2838c2ecf20Sopenharmony_ci		tl->packet_count[0] = 1;
2848c2ecf20Sopenharmony_ci		return MAX_TID_COUNT;
2858c2ecf20Sopenharmony_ci	}
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci	time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
2888c2ecf20Sopenharmony_ci	idx = time_diff / TID_QUEUE_CELL_SPACING;
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci	/* The history is too long: remove data that is older than */
2918c2ecf20Sopenharmony_ci	/* TID_MAX_TIME_DIFF */
2928c2ecf20Sopenharmony_ci	if (idx >= TID_QUEUE_MAX_SIZE)
2938c2ecf20Sopenharmony_ci		il4965_rs_tl_rm_old_stats(tl, curr_time);
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_ci	idx = (tl->head + idx) % TID_QUEUE_MAX_SIZE;
2968c2ecf20Sopenharmony_ci	tl->packet_count[idx] = tl->packet_count[idx] + 1;
2978c2ecf20Sopenharmony_ci	tl->total = tl->total + 1;
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_ci	if ((idx + 1) > tl->queue_count)
3008c2ecf20Sopenharmony_ci		tl->queue_count = idx + 1;
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_ci	return tid;
3038c2ecf20Sopenharmony_ci}
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci/*
3068c2ecf20Sopenharmony_ci	get the traffic load value for tid
3078c2ecf20Sopenharmony_ci*/
3088c2ecf20Sopenharmony_cistatic u32
3098c2ecf20Sopenharmony_ciil4965_rs_tl_get_load(struct il_lq_sta *lq_data, u8 tid)
3108c2ecf20Sopenharmony_ci{
3118c2ecf20Sopenharmony_ci	u32 curr_time = jiffies_to_msecs(jiffies);
3128c2ecf20Sopenharmony_ci	u32 time_diff;
3138c2ecf20Sopenharmony_ci	s32 idx;
3148c2ecf20Sopenharmony_ci	struct il_traffic_load *tl = NULL;
3158c2ecf20Sopenharmony_ci
3168c2ecf20Sopenharmony_ci	if (tid >= TID_MAX_LOAD_COUNT)
3178c2ecf20Sopenharmony_ci		return 0;
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ci	tl = &(lq_data->load[tid]);
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_ci	curr_time -= curr_time % TID_ROUND_VALUE;
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_ci	if (!(tl->queue_count))
3248c2ecf20Sopenharmony_ci		return 0;
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_ci	time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
3278c2ecf20Sopenharmony_ci	idx = time_diff / TID_QUEUE_CELL_SPACING;
3288c2ecf20Sopenharmony_ci
3298c2ecf20Sopenharmony_ci	/* The history is too long: remove data that is older than */
3308c2ecf20Sopenharmony_ci	/* TID_MAX_TIME_DIFF */
3318c2ecf20Sopenharmony_ci	if (idx >= TID_QUEUE_MAX_SIZE)
3328c2ecf20Sopenharmony_ci		il4965_rs_tl_rm_old_stats(tl, curr_time);
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_ci	return tl->total;
3358c2ecf20Sopenharmony_ci}
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_cistatic int
3388c2ecf20Sopenharmony_ciil4965_rs_tl_turn_on_agg_for_tid(struct il_priv *il, struct il_lq_sta *lq_data,
3398c2ecf20Sopenharmony_ci				 u8 tid, struct ieee80211_sta *sta)
3408c2ecf20Sopenharmony_ci{
3418c2ecf20Sopenharmony_ci	int ret = -EAGAIN;
3428c2ecf20Sopenharmony_ci	u32 load;
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci	load = il4965_rs_tl_get_load(lq_data, tid);
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_ci	if (load > IL_AGG_LOAD_THRESHOLD) {
3478c2ecf20Sopenharmony_ci		D_HT("Starting Tx agg: STA: %pM tid: %d\n", sta->addr, tid);
3488c2ecf20Sopenharmony_ci		ret = ieee80211_start_tx_ba_session(sta, tid, 5000);
3498c2ecf20Sopenharmony_ci		if (ret == -EAGAIN) {
3508c2ecf20Sopenharmony_ci			/*
3518c2ecf20Sopenharmony_ci			 * driver and mac80211 is out of sync
3528c2ecf20Sopenharmony_ci			 * this might be cause by reloading firmware
3538c2ecf20Sopenharmony_ci			 * stop the tx ba session here
3548c2ecf20Sopenharmony_ci			 */
3558c2ecf20Sopenharmony_ci			IL_ERR("Fail start Tx agg on tid: %d\n", tid);
3568c2ecf20Sopenharmony_ci			ieee80211_stop_tx_ba_session(sta, tid);
3578c2ecf20Sopenharmony_ci		}
3588c2ecf20Sopenharmony_ci	} else
3598c2ecf20Sopenharmony_ci		D_HT("Aggregation not enabled for tid %d because load = %u\n",
3608c2ecf20Sopenharmony_ci		     tid, load);
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_ci	return ret;
3638c2ecf20Sopenharmony_ci}
3648c2ecf20Sopenharmony_ci
3658c2ecf20Sopenharmony_cistatic void
3668c2ecf20Sopenharmony_ciil4965_rs_tl_turn_on_agg(struct il_priv *il, u8 tid, struct il_lq_sta *lq_data,
3678c2ecf20Sopenharmony_ci			 struct ieee80211_sta *sta)
3688c2ecf20Sopenharmony_ci{
3698c2ecf20Sopenharmony_ci	if (tid < TID_MAX_LOAD_COUNT)
3708c2ecf20Sopenharmony_ci		il4965_rs_tl_turn_on_agg_for_tid(il, lq_data, tid, sta);
3718c2ecf20Sopenharmony_ci	else
3728c2ecf20Sopenharmony_ci		IL_ERR("tid exceeds max load count: %d/%d\n", tid,
3738c2ecf20Sopenharmony_ci		       TID_MAX_LOAD_COUNT);
3748c2ecf20Sopenharmony_ci}
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_cistatic inline int
3778c2ecf20Sopenharmony_ciil4965_get_il4965_num_of_ant_from_rate(u32 rate_n_flags)
3788c2ecf20Sopenharmony_ci{
3798c2ecf20Sopenharmony_ci	return !!(rate_n_flags & RATE_MCS_ANT_A_MSK) +
3808c2ecf20Sopenharmony_ci	    !!(rate_n_flags & RATE_MCS_ANT_B_MSK) +
3818c2ecf20Sopenharmony_ci	    !!(rate_n_flags & RATE_MCS_ANT_C_MSK);
3828c2ecf20Sopenharmony_ci}
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_ci/*
3858c2ecf20Sopenharmony_ci * Static function to get the expected throughput from an il_scale_tbl_info
3868c2ecf20Sopenharmony_ci * that wraps a NULL pointer check
3878c2ecf20Sopenharmony_ci */
3888c2ecf20Sopenharmony_cistatic s32
3898c2ecf20Sopenharmony_ciil4965_get_expected_tpt(struct il_scale_tbl_info *tbl, int rs_idx)
3908c2ecf20Sopenharmony_ci{
3918c2ecf20Sopenharmony_ci	if (tbl->expected_tpt)
3928c2ecf20Sopenharmony_ci		return tbl->expected_tpt[rs_idx];
3938c2ecf20Sopenharmony_ci	return 0;
3948c2ecf20Sopenharmony_ci}
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_ci/*
3978c2ecf20Sopenharmony_ci * il4965_rs_collect_tx_data - Update the success/failure sliding win
3988c2ecf20Sopenharmony_ci *
3998c2ecf20Sopenharmony_ci * We keep a sliding win of the last 62 packets transmitted
4008c2ecf20Sopenharmony_ci * at this rate.  win->data contains the bitmask of successful
4018c2ecf20Sopenharmony_ci * packets.
4028c2ecf20Sopenharmony_ci */
4038c2ecf20Sopenharmony_cistatic int
4048c2ecf20Sopenharmony_ciil4965_rs_collect_tx_data(struct il_scale_tbl_info *tbl, int scale_idx,
4058c2ecf20Sopenharmony_ci			  int attempts, int successes)
4068c2ecf20Sopenharmony_ci{
4078c2ecf20Sopenharmony_ci	struct il_rate_scale_data *win = NULL;
4088c2ecf20Sopenharmony_ci	static const u64 mask = (((u64) 1) << (RATE_MAX_WINDOW - 1));
4098c2ecf20Sopenharmony_ci	s32 fail_count, tpt;
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_ci	if (scale_idx < 0 || scale_idx >= RATE_COUNT)
4128c2ecf20Sopenharmony_ci		return -EINVAL;
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_ci	/* Select win for current tx bit rate */
4158c2ecf20Sopenharmony_ci	win = &(tbl->win[scale_idx]);
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_ci	/* Get expected throughput */
4188c2ecf20Sopenharmony_ci	tpt = il4965_get_expected_tpt(tbl, scale_idx);
4198c2ecf20Sopenharmony_ci
4208c2ecf20Sopenharmony_ci	/*
4218c2ecf20Sopenharmony_ci	 * Keep track of only the latest 62 tx frame attempts in this rate's
4228c2ecf20Sopenharmony_ci	 * history win; anything older isn't really relevant any more.
4238c2ecf20Sopenharmony_ci	 * If we have filled up the sliding win, drop the oldest attempt;
4248c2ecf20Sopenharmony_ci	 * if the oldest attempt (highest bit in bitmap) shows "success",
4258c2ecf20Sopenharmony_ci	 * subtract "1" from the success counter (this is the main reason
4268c2ecf20Sopenharmony_ci	 * we keep these bitmaps!).
4278c2ecf20Sopenharmony_ci	 */
4288c2ecf20Sopenharmony_ci	while (attempts > 0) {
4298c2ecf20Sopenharmony_ci		if (win->counter >= RATE_MAX_WINDOW) {
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_ci			/* remove earliest */
4328c2ecf20Sopenharmony_ci			win->counter = RATE_MAX_WINDOW - 1;
4338c2ecf20Sopenharmony_ci
4348c2ecf20Sopenharmony_ci			if (win->data & mask) {
4358c2ecf20Sopenharmony_ci				win->data &= ~mask;
4368c2ecf20Sopenharmony_ci				win->success_counter--;
4378c2ecf20Sopenharmony_ci			}
4388c2ecf20Sopenharmony_ci		}
4398c2ecf20Sopenharmony_ci
4408c2ecf20Sopenharmony_ci		/* Increment frames-attempted counter */
4418c2ecf20Sopenharmony_ci		win->counter++;
4428c2ecf20Sopenharmony_ci
4438c2ecf20Sopenharmony_ci		/* Shift bitmap by one frame to throw away oldest history */
4448c2ecf20Sopenharmony_ci		win->data <<= 1;
4458c2ecf20Sopenharmony_ci
4468c2ecf20Sopenharmony_ci		/* Mark the most recent #successes attempts as successful */
4478c2ecf20Sopenharmony_ci		if (successes > 0) {
4488c2ecf20Sopenharmony_ci			win->success_counter++;
4498c2ecf20Sopenharmony_ci			win->data |= 0x1;
4508c2ecf20Sopenharmony_ci			successes--;
4518c2ecf20Sopenharmony_ci		}
4528c2ecf20Sopenharmony_ci
4538c2ecf20Sopenharmony_ci		attempts--;
4548c2ecf20Sopenharmony_ci	}
4558c2ecf20Sopenharmony_ci
4568c2ecf20Sopenharmony_ci	/* Calculate current success ratio, avoid divide-by-0! */
4578c2ecf20Sopenharmony_ci	if (win->counter > 0)
4588c2ecf20Sopenharmony_ci		win->success_ratio =
4598c2ecf20Sopenharmony_ci		    128 * (100 * win->success_counter) / win->counter;
4608c2ecf20Sopenharmony_ci	else
4618c2ecf20Sopenharmony_ci		win->success_ratio = IL_INVALID_VALUE;
4628c2ecf20Sopenharmony_ci
4638c2ecf20Sopenharmony_ci	fail_count = win->counter - win->success_counter;
4648c2ecf20Sopenharmony_ci
4658c2ecf20Sopenharmony_ci	/* Calculate average throughput, if we have enough history. */
4668c2ecf20Sopenharmony_ci	if (fail_count >= RATE_MIN_FAILURE_TH ||
4678c2ecf20Sopenharmony_ci	    win->success_counter >= RATE_MIN_SUCCESS_TH)
4688c2ecf20Sopenharmony_ci		win->average_tpt = (win->success_ratio * tpt + 64) / 128;
4698c2ecf20Sopenharmony_ci	else
4708c2ecf20Sopenharmony_ci		win->average_tpt = IL_INVALID_VALUE;
4718c2ecf20Sopenharmony_ci
4728c2ecf20Sopenharmony_ci	/* Tag this win as having been updated */
4738c2ecf20Sopenharmony_ci	win->stamp = jiffies;
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_ci	return 0;
4768c2ecf20Sopenharmony_ci}
4778c2ecf20Sopenharmony_ci
4788c2ecf20Sopenharmony_ci/*
4798c2ecf20Sopenharmony_ci * Fill uCode API rate_n_flags field, based on "search" or "active" table.
4808c2ecf20Sopenharmony_ci */
4818c2ecf20Sopenharmony_cistatic u32
4828c2ecf20Sopenharmony_ciil4965_rate_n_flags_from_tbl(struct il_priv *il, struct il_scale_tbl_info *tbl,
4838c2ecf20Sopenharmony_ci			     int idx, u8 use_green)
4848c2ecf20Sopenharmony_ci{
4858c2ecf20Sopenharmony_ci	u32 rate_n_flags = 0;
4868c2ecf20Sopenharmony_ci
4878c2ecf20Sopenharmony_ci	if (is_legacy(tbl->lq_type)) {
4888c2ecf20Sopenharmony_ci		rate_n_flags = il_rates[idx].plcp;
4898c2ecf20Sopenharmony_ci		if (idx >= IL_FIRST_CCK_RATE && idx <= IL_LAST_CCK_RATE)
4908c2ecf20Sopenharmony_ci			rate_n_flags |= RATE_MCS_CCK_MSK;
4918c2ecf20Sopenharmony_ci
4928c2ecf20Sopenharmony_ci	} else if (is_Ht(tbl->lq_type)) {
4938c2ecf20Sopenharmony_ci		if (idx > IL_LAST_OFDM_RATE) {
4948c2ecf20Sopenharmony_ci			IL_ERR("Invalid HT rate idx %d\n", idx);
4958c2ecf20Sopenharmony_ci			idx = IL_LAST_OFDM_RATE;
4968c2ecf20Sopenharmony_ci		}
4978c2ecf20Sopenharmony_ci		rate_n_flags = RATE_MCS_HT_MSK;
4988c2ecf20Sopenharmony_ci
4998c2ecf20Sopenharmony_ci		if (is_siso(tbl->lq_type))
5008c2ecf20Sopenharmony_ci			rate_n_flags |= il_rates[idx].plcp_siso;
5018c2ecf20Sopenharmony_ci		else
5028c2ecf20Sopenharmony_ci			rate_n_flags |= il_rates[idx].plcp_mimo2;
5038c2ecf20Sopenharmony_ci	} else {
5048c2ecf20Sopenharmony_ci		IL_ERR("Invalid tbl->lq_type %d\n", tbl->lq_type);
5058c2ecf20Sopenharmony_ci	}
5068c2ecf20Sopenharmony_ci
5078c2ecf20Sopenharmony_ci	rate_n_flags |=
5088c2ecf20Sopenharmony_ci	    ((tbl->ant_type << RATE_MCS_ANT_POS) & RATE_MCS_ANT_ABC_MSK);
5098c2ecf20Sopenharmony_ci
5108c2ecf20Sopenharmony_ci	if (is_Ht(tbl->lq_type)) {
5118c2ecf20Sopenharmony_ci		if (tbl->is_ht40) {
5128c2ecf20Sopenharmony_ci			if (tbl->is_dup)
5138c2ecf20Sopenharmony_ci				rate_n_flags |= RATE_MCS_DUP_MSK;
5148c2ecf20Sopenharmony_ci			else
5158c2ecf20Sopenharmony_ci				rate_n_flags |= RATE_MCS_HT40_MSK;
5168c2ecf20Sopenharmony_ci		}
5178c2ecf20Sopenharmony_ci		if (tbl->is_SGI)
5188c2ecf20Sopenharmony_ci			rate_n_flags |= RATE_MCS_SGI_MSK;
5198c2ecf20Sopenharmony_ci
5208c2ecf20Sopenharmony_ci		if (use_green) {
5218c2ecf20Sopenharmony_ci			rate_n_flags |= RATE_MCS_GF_MSK;
5228c2ecf20Sopenharmony_ci			if (is_siso(tbl->lq_type) && tbl->is_SGI) {
5238c2ecf20Sopenharmony_ci				rate_n_flags &= ~RATE_MCS_SGI_MSK;
5248c2ecf20Sopenharmony_ci				IL_ERR("GF was set with SGI:SISO\n");
5258c2ecf20Sopenharmony_ci			}
5268c2ecf20Sopenharmony_ci		}
5278c2ecf20Sopenharmony_ci	}
5288c2ecf20Sopenharmony_ci	return rate_n_flags;
5298c2ecf20Sopenharmony_ci}
5308c2ecf20Sopenharmony_ci
5318c2ecf20Sopenharmony_ci/*
5328c2ecf20Sopenharmony_ci * Interpret uCode API's rate_n_flags format,
5338c2ecf20Sopenharmony_ci * fill "search" or "active" tx mode table.
5348c2ecf20Sopenharmony_ci */
5358c2ecf20Sopenharmony_cistatic int
5368c2ecf20Sopenharmony_ciil4965_rs_get_tbl_info_from_mcs(const u32 rate_n_flags,
5378c2ecf20Sopenharmony_ci				enum nl80211_band band,
5388c2ecf20Sopenharmony_ci				struct il_scale_tbl_info *tbl, int *rate_idx)
5398c2ecf20Sopenharmony_ci{
5408c2ecf20Sopenharmony_ci	u32 ant_msk = (rate_n_flags & RATE_MCS_ANT_ABC_MSK);
5418c2ecf20Sopenharmony_ci	u8 il4965_num_of_ant =
5428c2ecf20Sopenharmony_ci	    il4965_get_il4965_num_of_ant_from_rate(rate_n_flags);
5438c2ecf20Sopenharmony_ci	u8 mcs;
5448c2ecf20Sopenharmony_ci
5458c2ecf20Sopenharmony_ci	memset(tbl, 0, sizeof(struct il_scale_tbl_info));
5468c2ecf20Sopenharmony_ci	*rate_idx = il4965_hwrate_to_plcp_idx(rate_n_flags);
5478c2ecf20Sopenharmony_ci
5488c2ecf20Sopenharmony_ci	if (*rate_idx == RATE_INVALID) {
5498c2ecf20Sopenharmony_ci		*rate_idx = -1;
5508c2ecf20Sopenharmony_ci		return -EINVAL;
5518c2ecf20Sopenharmony_ci	}
5528c2ecf20Sopenharmony_ci	tbl->is_SGI = 0;	/* default legacy setup */
5538c2ecf20Sopenharmony_ci	tbl->is_ht40 = 0;
5548c2ecf20Sopenharmony_ci	tbl->is_dup = 0;
5558c2ecf20Sopenharmony_ci	tbl->ant_type = (ant_msk >> RATE_MCS_ANT_POS);
5568c2ecf20Sopenharmony_ci	tbl->lq_type = LQ_NONE;
5578c2ecf20Sopenharmony_ci	tbl->max_search = IL_MAX_SEARCH;
5588c2ecf20Sopenharmony_ci
5598c2ecf20Sopenharmony_ci	/* legacy rate format */
5608c2ecf20Sopenharmony_ci	if (!(rate_n_flags & RATE_MCS_HT_MSK)) {
5618c2ecf20Sopenharmony_ci		if (il4965_num_of_ant == 1) {
5628c2ecf20Sopenharmony_ci			if (band == NL80211_BAND_5GHZ)
5638c2ecf20Sopenharmony_ci				tbl->lq_type = LQ_A;
5648c2ecf20Sopenharmony_ci			else
5658c2ecf20Sopenharmony_ci				tbl->lq_type = LQ_G;
5668c2ecf20Sopenharmony_ci		}
5678c2ecf20Sopenharmony_ci		/* HT rate format */
5688c2ecf20Sopenharmony_ci	} else {
5698c2ecf20Sopenharmony_ci		if (rate_n_flags & RATE_MCS_SGI_MSK)
5708c2ecf20Sopenharmony_ci			tbl->is_SGI = 1;
5718c2ecf20Sopenharmony_ci
5728c2ecf20Sopenharmony_ci		if ((rate_n_flags & RATE_MCS_HT40_MSK) ||
5738c2ecf20Sopenharmony_ci		    (rate_n_flags & RATE_MCS_DUP_MSK))
5748c2ecf20Sopenharmony_ci			tbl->is_ht40 = 1;
5758c2ecf20Sopenharmony_ci
5768c2ecf20Sopenharmony_ci		if (rate_n_flags & RATE_MCS_DUP_MSK)
5778c2ecf20Sopenharmony_ci			tbl->is_dup = 1;
5788c2ecf20Sopenharmony_ci
5798c2ecf20Sopenharmony_ci		mcs = il4965_rs_extract_rate(rate_n_flags);
5808c2ecf20Sopenharmony_ci
5818c2ecf20Sopenharmony_ci		/* SISO */
5828c2ecf20Sopenharmony_ci		if (mcs <= RATE_SISO_60M_PLCP) {
5838c2ecf20Sopenharmony_ci			if (il4965_num_of_ant == 1)
5848c2ecf20Sopenharmony_ci				tbl->lq_type = LQ_SISO;	/*else NONE */
5858c2ecf20Sopenharmony_ci			/* MIMO2 */
5868c2ecf20Sopenharmony_ci		} else {
5878c2ecf20Sopenharmony_ci			if (il4965_num_of_ant == 2)
5888c2ecf20Sopenharmony_ci				tbl->lq_type = LQ_MIMO2;
5898c2ecf20Sopenharmony_ci		}
5908c2ecf20Sopenharmony_ci	}
5918c2ecf20Sopenharmony_ci	return 0;
5928c2ecf20Sopenharmony_ci}
5938c2ecf20Sopenharmony_ci
5948c2ecf20Sopenharmony_ci/* switch to another antenna/antennas and return 1 */
5958c2ecf20Sopenharmony_ci/* if no other valid antenna found, return 0 */
5968c2ecf20Sopenharmony_cistatic int
5978c2ecf20Sopenharmony_ciil4965_rs_toggle_antenna(u32 valid_ant, u32 *rate_n_flags,
5988c2ecf20Sopenharmony_ci			 struct il_scale_tbl_info *tbl)
5998c2ecf20Sopenharmony_ci{
6008c2ecf20Sopenharmony_ci	u8 new_ant_type;
6018c2ecf20Sopenharmony_ci
6028c2ecf20Sopenharmony_ci	if (!tbl->ant_type || tbl->ant_type > ANT_ABC)
6038c2ecf20Sopenharmony_ci		return 0;
6048c2ecf20Sopenharmony_ci
6058c2ecf20Sopenharmony_ci	if (!il4965_rs_is_valid_ant(valid_ant, tbl->ant_type))
6068c2ecf20Sopenharmony_ci		return 0;
6078c2ecf20Sopenharmony_ci
6088c2ecf20Sopenharmony_ci	new_ant_type = ant_toggle_lookup[tbl->ant_type];
6098c2ecf20Sopenharmony_ci
6108c2ecf20Sopenharmony_ci	while (new_ant_type != tbl->ant_type &&
6118c2ecf20Sopenharmony_ci	       !il4965_rs_is_valid_ant(valid_ant, new_ant_type))
6128c2ecf20Sopenharmony_ci		new_ant_type = ant_toggle_lookup[new_ant_type];
6138c2ecf20Sopenharmony_ci
6148c2ecf20Sopenharmony_ci	if (new_ant_type == tbl->ant_type)
6158c2ecf20Sopenharmony_ci		return 0;
6168c2ecf20Sopenharmony_ci
6178c2ecf20Sopenharmony_ci	tbl->ant_type = new_ant_type;
6188c2ecf20Sopenharmony_ci	*rate_n_flags &= ~RATE_MCS_ANT_ABC_MSK;
6198c2ecf20Sopenharmony_ci	*rate_n_flags |= new_ant_type << RATE_MCS_ANT_POS;
6208c2ecf20Sopenharmony_ci	return 1;
6218c2ecf20Sopenharmony_ci}
6228c2ecf20Sopenharmony_ci
6238c2ecf20Sopenharmony_ci/*
6248c2ecf20Sopenharmony_ci * Green-field mode is valid if the station supports it and
6258c2ecf20Sopenharmony_ci * there are no non-GF stations present in the BSS.
6268c2ecf20Sopenharmony_ci */
6278c2ecf20Sopenharmony_cistatic bool
6288c2ecf20Sopenharmony_ciil4965_rs_use_green(struct il_priv *il, struct ieee80211_sta *sta)
6298c2ecf20Sopenharmony_ci{
6308c2ecf20Sopenharmony_ci	return (sta->ht_cap.cap & IEEE80211_HT_CAP_GRN_FLD) &&
6318c2ecf20Sopenharmony_ci	       !il->ht.non_gf_sta_present;
6328c2ecf20Sopenharmony_ci}
6338c2ecf20Sopenharmony_ci
6348c2ecf20Sopenharmony_ci/*
6358c2ecf20Sopenharmony_ci * il4965_rs_get_supported_rates - get the available rates
6368c2ecf20Sopenharmony_ci *
6378c2ecf20Sopenharmony_ci * if management frame or broadcast frame only return
6388c2ecf20Sopenharmony_ci * basic available rates.
6398c2ecf20Sopenharmony_ci *
6408c2ecf20Sopenharmony_ci */
6418c2ecf20Sopenharmony_cistatic u16
6428c2ecf20Sopenharmony_ciil4965_rs_get_supported_rates(struct il_lq_sta *lq_sta,
6438c2ecf20Sopenharmony_ci			      struct ieee80211_hdr *hdr,
6448c2ecf20Sopenharmony_ci			      enum il_table_type rate_type)
6458c2ecf20Sopenharmony_ci{
6468c2ecf20Sopenharmony_ci	if (is_legacy(rate_type)) {
6478c2ecf20Sopenharmony_ci		return lq_sta->active_legacy_rate;
6488c2ecf20Sopenharmony_ci	} else {
6498c2ecf20Sopenharmony_ci		if (is_siso(rate_type))
6508c2ecf20Sopenharmony_ci			return lq_sta->active_siso_rate;
6518c2ecf20Sopenharmony_ci		else
6528c2ecf20Sopenharmony_ci			return lq_sta->active_mimo2_rate;
6538c2ecf20Sopenharmony_ci	}
6548c2ecf20Sopenharmony_ci}
6558c2ecf20Sopenharmony_ci
6568c2ecf20Sopenharmony_cistatic u16
6578c2ecf20Sopenharmony_ciil4965_rs_get_adjacent_rate(struct il_priv *il, u8 idx, u16 rate_mask,
6588c2ecf20Sopenharmony_ci			    int rate_type)
6598c2ecf20Sopenharmony_ci{
6608c2ecf20Sopenharmony_ci	u8 high = RATE_INVALID;
6618c2ecf20Sopenharmony_ci	u8 low = RATE_INVALID;
6628c2ecf20Sopenharmony_ci
6638c2ecf20Sopenharmony_ci	/* 802.11A or ht walks to the next literal adjacent rate in
6648c2ecf20Sopenharmony_ci	 * the rate table */
6658c2ecf20Sopenharmony_ci	if (is_a_band(rate_type) || !is_legacy(rate_type)) {
6668c2ecf20Sopenharmony_ci		int i;
6678c2ecf20Sopenharmony_ci		u32 mask;
6688c2ecf20Sopenharmony_ci
6698c2ecf20Sopenharmony_ci		/* Find the previous rate that is in the rate mask */
6708c2ecf20Sopenharmony_ci		i = idx - 1;
6718c2ecf20Sopenharmony_ci		for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
6728c2ecf20Sopenharmony_ci			if (rate_mask & mask) {
6738c2ecf20Sopenharmony_ci				low = i;
6748c2ecf20Sopenharmony_ci				break;
6758c2ecf20Sopenharmony_ci			}
6768c2ecf20Sopenharmony_ci		}
6778c2ecf20Sopenharmony_ci
6788c2ecf20Sopenharmony_ci		/* Find the next rate that is in the rate mask */
6798c2ecf20Sopenharmony_ci		i = idx + 1;
6808c2ecf20Sopenharmony_ci		for (mask = (1 << i); i < RATE_COUNT; i++, mask <<= 1) {
6818c2ecf20Sopenharmony_ci			if (rate_mask & mask) {
6828c2ecf20Sopenharmony_ci				high = i;
6838c2ecf20Sopenharmony_ci				break;
6848c2ecf20Sopenharmony_ci			}
6858c2ecf20Sopenharmony_ci		}
6868c2ecf20Sopenharmony_ci
6878c2ecf20Sopenharmony_ci		return (high << 8) | low;
6888c2ecf20Sopenharmony_ci	}
6898c2ecf20Sopenharmony_ci
6908c2ecf20Sopenharmony_ci	low = idx;
6918c2ecf20Sopenharmony_ci	while (low != RATE_INVALID) {
6928c2ecf20Sopenharmony_ci		low = il_rates[low].prev_rs;
6938c2ecf20Sopenharmony_ci		if (low == RATE_INVALID)
6948c2ecf20Sopenharmony_ci			break;
6958c2ecf20Sopenharmony_ci		if (rate_mask & (1 << low))
6968c2ecf20Sopenharmony_ci			break;
6978c2ecf20Sopenharmony_ci		D_RATE("Skipping masked lower rate: %d\n", low);
6988c2ecf20Sopenharmony_ci	}
6998c2ecf20Sopenharmony_ci
7008c2ecf20Sopenharmony_ci	high = idx;
7018c2ecf20Sopenharmony_ci	while (high != RATE_INVALID) {
7028c2ecf20Sopenharmony_ci		high = il_rates[high].next_rs;
7038c2ecf20Sopenharmony_ci		if (high == RATE_INVALID)
7048c2ecf20Sopenharmony_ci			break;
7058c2ecf20Sopenharmony_ci		if (rate_mask & (1 << high))
7068c2ecf20Sopenharmony_ci			break;
7078c2ecf20Sopenharmony_ci		D_RATE("Skipping masked higher rate: %d\n", high);
7088c2ecf20Sopenharmony_ci	}
7098c2ecf20Sopenharmony_ci
7108c2ecf20Sopenharmony_ci	return (high << 8) | low;
7118c2ecf20Sopenharmony_ci}
7128c2ecf20Sopenharmony_ci
7138c2ecf20Sopenharmony_cistatic u32
7148c2ecf20Sopenharmony_ciil4965_rs_get_lower_rate(struct il_lq_sta *lq_sta,
7158c2ecf20Sopenharmony_ci			 struct il_scale_tbl_info *tbl, u8 scale_idx,
7168c2ecf20Sopenharmony_ci			 u8 ht_possible)
7178c2ecf20Sopenharmony_ci{
7188c2ecf20Sopenharmony_ci	s32 low;
7198c2ecf20Sopenharmony_ci	u16 rate_mask;
7208c2ecf20Sopenharmony_ci	u16 high_low;
7218c2ecf20Sopenharmony_ci	u8 switch_to_legacy = 0;
7228c2ecf20Sopenharmony_ci	u8 is_green = lq_sta->is_green;
7238c2ecf20Sopenharmony_ci	struct il_priv *il = lq_sta->drv;
7248c2ecf20Sopenharmony_ci
7258c2ecf20Sopenharmony_ci	/* check if we need to switch from HT to legacy rates.
7268c2ecf20Sopenharmony_ci	 * assumption is that mandatory rates (1Mbps or 6Mbps)
7278c2ecf20Sopenharmony_ci	 * are always supported (spec demand) */
7288c2ecf20Sopenharmony_ci	if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_idx)) {
7298c2ecf20Sopenharmony_ci		switch_to_legacy = 1;
7308c2ecf20Sopenharmony_ci		scale_idx = rs_ht_to_legacy[scale_idx];
7318c2ecf20Sopenharmony_ci		if (lq_sta->band == NL80211_BAND_5GHZ)
7328c2ecf20Sopenharmony_ci			tbl->lq_type = LQ_A;
7338c2ecf20Sopenharmony_ci		else
7348c2ecf20Sopenharmony_ci			tbl->lq_type = LQ_G;
7358c2ecf20Sopenharmony_ci
7368c2ecf20Sopenharmony_ci		if (il4965_num_of_ant(tbl->ant_type) > 1)
7378c2ecf20Sopenharmony_ci			tbl->ant_type =
7388c2ecf20Sopenharmony_ci			    il4965_first_antenna(il->hw_params.valid_tx_ant);
7398c2ecf20Sopenharmony_ci
7408c2ecf20Sopenharmony_ci		tbl->is_ht40 = 0;
7418c2ecf20Sopenharmony_ci		tbl->is_SGI = 0;
7428c2ecf20Sopenharmony_ci		tbl->max_search = IL_MAX_SEARCH;
7438c2ecf20Sopenharmony_ci	}
7448c2ecf20Sopenharmony_ci
7458c2ecf20Sopenharmony_ci	rate_mask = il4965_rs_get_supported_rates(lq_sta, NULL, tbl->lq_type);
7468c2ecf20Sopenharmony_ci
7478c2ecf20Sopenharmony_ci	/* Mask with station rate restriction */
7488c2ecf20Sopenharmony_ci	if (is_legacy(tbl->lq_type)) {
7498c2ecf20Sopenharmony_ci		/* supp_rates has no CCK bits in A mode */
7508c2ecf20Sopenharmony_ci		if (lq_sta->band == NL80211_BAND_5GHZ)
7518c2ecf20Sopenharmony_ci			rate_mask =
7528c2ecf20Sopenharmony_ci			    (u16) (rate_mask &
7538c2ecf20Sopenharmony_ci				   (lq_sta->supp_rates << IL_FIRST_OFDM_RATE));
7548c2ecf20Sopenharmony_ci		else
7558c2ecf20Sopenharmony_ci			rate_mask = (u16) (rate_mask & lq_sta->supp_rates);
7568c2ecf20Sopenharmony_ci	}
7578c2ecf20Sopenharmony_ci
7588c2ecf20Sopenharmony_ci	/* If we switched from HT to legacy, check current rate */
7598c2ecf20Sopenharmony_ci	if (switch_to_legacy && (rate_mask & (1 << scale_idx))) {
7608c2ecf20Sopenharmony_ci		low = scale_idx;
7618c2ecf20Sopenharmony_ci		goto out;
7628c2ecf20Sopenharmony_ci	}
7638c2ecf20Sopenharmony_ci
7648c2ecf20Sopenharmony_ci	high_low =
7658c2ecf20Sopenharmony_ci	    il4965_rs_get_adjacent_rate(lq_sta->drv, scale_idx, rate_mask,
7668c2ecf20Sopenharmony_ci					tbl->lq_type);
7678c2ecf20Sopenharmony_ci	low = high_low & 0xff;
7688c2ecf20Sopenharmony_ci
7698c2ecf20Sopenharmony_ci	if (low == RATE_INVALID)
7708c2ecf20Sopenharmony_ci		low = scale_idx;
7718c2ecf20Sopenharmony_ci
7728c2ecf20Sopenharmony_ciout:
7738c2ecf20Sopenharmony_ci	return il4965_rate_n_flags_from_tbl(lq_sta->drv, tbl, low, is_green);
7748c2ecf20Sopenharmony_ci}
7758c2ecf20Sopenharmony_ci
7768c2ecf20Sopenharmony_ci/*
7778c2ecf20Sopenharmony_ci * Simple function to compare two rate scale table types
7788c2ecf20Sopenharmony_ci */
7798c2ecf20Sopenharmony_cistatic bool
7808c2ecf20Sopenharmony_ciil4965_table_type_matches(struct il_scale_tbl_info *a,
7818c2ecf20Sopenharmony_ci			  struct il_scale_tbl_info *b)
7828c2ecf20Sopenharmony_ci{
7838c2ecf20Sopenharmony_ci	return (a->lq_type == b->lq_type && a->ant_type == b->ant_type &&
7848c2ecf20Sopenharmony_ci		a->is_SGI == b->is_SGI);
7858c2ecf20Sopenharmony_ci}
7868c2ecf20Sopenharmony_ci
7878c2ecf20Sopenharmony_ci/*
7888c2ecf20Sopenharmony_ci * mac80211 sends us Tx status
7898c2ecf20Sopenharmony_ci */
7908c2ecf20Sopenharmony_cistatic void
7918c2ecf20Sopenharmony_ciil4965_rs_tx_status(void *il_r, struct ieee80211_supported_band *sband,
7928c2ecf20Sopenharmony_ci		    struct ieee80211_sta *sta, void *il_sta,
7938c2ecf20Sopenharmony_ci		    struct sk_buff *skb)
7948c2ecf20Sopenharmony_ci{
7958c2ecf20Sopenharmony_ci	int legacy_success;
7968c2ecf20Sopenharmony_ci	int retries;
7978c2ecf20Sopenharmony_ci	int rs_idx, mac_idx, i;
7988c2ecf20Sopenharmony_ci	struct il_lq_sta *lq_sta = il_sta;
7998c2ecf20Sopenharmony_ci	struct il_link_quality_cmd *table;
8008c2ecf20Sopenharmony_ci	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
8018c2ecf20Sopenharmony_ci	struct il_priv *il = (struct il_priv *)il_r;
8028c2ecf20Sopenharmony_ci	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
8038c2ecf20Sopenharmony_ci	enum mac80211_rate_control_flags mac_flags;
8048c2ecf20Sopenharmony_ci	u32 tx_rate;
8058c2ecf20Sopenharmony_ci	struct il_scale_tbl_info tbl_type;
8068c2ecf20Sopenharmony_ci	struct il_scale_tbl_info *curr_tbl, *other_tbl, *tmp_tbl;
8078c2ecf20Sopenharmony_ci
8088c2ecf20Sopenharmony_ci	D_RATE("get frame ack response, update rate scale win\n");
8098c2ecf20Sopenharmony_ci
8108c2ecf20Sopenharmony_ci	/* Treat uninitialized rate scaling data same as non-existing. */
8118c2ecf20Sopenharmony_ci	if (!lq_sta) {
8128c2ecf20Sopenharmony_ci		D_RATE("Station rate scaling not created yet.\n");
8138c2ecf20Sopenharmony_ci		return;
8148c2ecf20Sopenharmony_ci	} else if (!lq_sta->drv) {
8158c2ecf20Sopenharmony_ci		D_RATE("Rate scaling not initialized yet.\n");
8168c2ecf20Sopenharmony_ci		return;
8178c2ecf20Sopenharmony_ci	}
8188c2ecf20Sopenharmony_ci
8198c2ecf20Sopenharmony_ci	if (!ieee80211_is_data(hdr->frame_control) ||
8208c2ecf20Sopenharmony_ci	    (info->flags & IEEE80211_TX_CTL_NO_ACK))
8218c2ecf20Sopenharmony_ci		return;
8228c2ecf20Sopenharmony_ci
8238c2ecf20Sopenharmony_ci	/* This packet was aggregated but doesn't carry status info */
8248c2ecf20Sopenharmony_ci	if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
8258c2ecf20Sopenharmony_ci	    !(info->flags & IEEE80211_TX_STAT_AMPDU))
8268c2ecf20Sopenharmony_ci		return;
8278c2ecf20Sopenharmony_ci
8288c2ecf20Sopenharmony_ci	/*
8298c2ecf20Sopenharmony_ci	 * Ignore this Tx frame response if its initial rate doesn't match
8308c2ecf20Sopenharmony_ci	 * that of latest Link Quality command.  There may be stragglers
8318c2ecf20Sopenharmony_ci	 * from a previous Link Quality command, but we're no longer interested
8328c2ecf20Sopenharmony_ci	 * in those; they're either from the "active" mode while we're trying
8338c2ecf20Sopenharmony_ci	 * to check "search" mode, or a prior "search" mode after we've moved
8348c2ecf20Sopenharmony_ci	 * to a new "search" mode (which might become the new "active" mode).
8358c2ecf20Sopenharmony_ci	 */
8368c2ecf20Sopenharmony_ci	table = &lq_sta->lq;
8378c2ecf20Sopenharmony_ci	tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
8388c2ecf20Sopenharmony_ci	il4965_rs_get_tbl_info_from_mcs(tx_rate, il->band, &tbl_type, &rs_idx);
8398c2ecf20Sopenharmony_ci	if (il->band == NL80211_BAND_5GHZ)
8408c2ecf20Sopenharmony_ci		rs_idx -= IL_FIRST_OFDM_RATE;
8418c2ecf20Sopenharmony_ci	mac_flags = info->status.rates[0].flags;
8428c2ecf20Sopenharmony_ci	mac_idx = info->status.rates[0].idx;
8438c2ecf20Sopenharmony_ci	/* For HT packets, map MCS to PLCP */
8448c2ecf20Sopenharmony_ci	if (mac_flags & IEEE80211_TX_RC_MCS) {
8458c2ecf20Sopenharmony_ci		mac_idx &= RATE_MCS_CODE_MSK;	/* Remove # of streams */
8468c2ecf20Sopenharmony_ci		if (mac_idx >= (RATE_9M_IDX - IL_FIRST_OFDM_RATE))
8478c2ecf20Sopenharmony_ci			mac_idx++;
8488c2ecf20Sopenharmony_ci		/*
8498c2ecf20Sopenharmony_ci		 * mac80211 HT idx is always zero-idxed; we need to move
8508c2ecf20Sopenharmony_ci		 * HT OFDM rates after CCK rates in 2.4 GHz band
8518c2ecf20Sopenharmony_ci		 */
8528c2ecf20Sopenharmony_ci		if (il->band == NL80211_BAND_2GHZ)
8538c2ecf20Sopenharmony_ci			mac_idx += IL_FIRST_OFDM_RATE;
8548c2ecf20Sopenharmony_ci	}
8558c2ecf20Sopenharmony_ci	/* Here we actually compare this rate to the latest LQ command */
8568c2ecf20Sopenharmony_ci	if (mac_idx < 0 ||
8578c2ecf20Sopenharmony_ci	    tbl_type.is_SGI != !!(mac_flags & IEEE80211_TX_RC_SHORT_GI) ||
8588c2ecf20Sopenharmony_ci	    tbl_type.is_ht40 != !!(mac_flags & IEEE80211_TX_RC_40_MHZ_WIDTH) ||
8598c2ecf20Sopenharmony_ci	    tbl_type.is_dup != !!(mac_flags & IEEE80211_TX_RC_DUP_DATA) ||
8608c2ecf20Sopenharmony_ci	    tbl_type.ant_type != info->status.antenna ||
8618c2ecf20Sopenharmony_ci	    !!(tx_rate & RATE_MCS_HT_MSK) != !!(mac_flags & IEEE80211_TX_RC_MCS)
8628c2ecf20Sopenharmony_ci	    || !!(tx_rate & RATE_MCS_GF_MSK) !=
8638c2ecf20Sopenharmony_ci	    !!(mac_flags & IEEE80211_TX_RC_GREEN_FIELD) || rs_idx != mac_idx) {
8648c2ecf20Sopenharmony_ci		D_RATE("initial rate %d does not match %d (0x%x)\n", mac_idx,
8658c2ecf20Sopenharmony_ci		       rs_idx, tx_rate);
8668c2ecf20Sopenharmony_ci		/*
8678c2ecf20Sopenharmony_ci		 * Since rates mis-match, the last LQ command may have failed.
8688c2ecf20Sopenharmony_ci		 * After IL_MISSED_RATE_MAX mis-matches, resync the uCode with
8698c2ecf20Sopenharmony_ci		 * ... driver.
8708c2ecf20Sopenharmony_ci		 */
8718c2ecf20Sopenharmony_ci		lq_sta->missed_rate_counter++;
8728c2ecf20Sopenharmony_ci		if (lq_sta->missed_rate_counter > IL_MISSED_RATE_MAX) {
8738c2ecf20Sopenharmony_ci			lq_sta->missed_rate_counter = 0;
8748c2ecf20Sopenharmony_ci			il_send_lq_cmd(il, &lq_sta->lq, CMD_ASYNC, false);
8758c2ecf20Sopenharmony_ci		}
8768c2ecf20Sopenharmony_ci		/* Regardless, ignore this status info for outdated rate */
8778c2ecf20Sopenharmony_ci		return;
8788c2ecf20Sopenharmony_ci	} else
8798c2ecf20Sopenharmony_ci		/* Rate did match, so reset the missed_rate_counter */
8808c2ecf20Sopenharmony_ci		lq_sta->missed_rate_counter = 0;
8818c2ecf20Sopenharmony_ci
8828c2ecf20Sopenharmony_ci	/* Figure out if rate scale algorithm is in active or search table */
8838c2ecf20Sopenharmony_ci	if (il4965_table_type_matches
8848c2ecf20Sopenharmony_ci	    (&tbl_type, &(lq_sta->lq_info[lq_sta->active_tbl]))) {
8858c2ecf20Sopenharmony_ci		curr_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
8868c2ecf20Sopenharmony_ci		other_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
8878c2ecf20Sopenharmony_ci	} else
8888c2ecf20Sopenharmony_ci	    if (il4965_table_type_matches
8898c2ecf20Sopenharmony_ci		(&tbl_type, &lq_sta->lq_info[1 - lq_sta->active_tbl])) {
8908c2ecf20Sopenharmony_ci		curr_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
8918c2ecf20Sopenharmony_ci		other_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
8928c2ecf20Sopenharmony_ci	} else {
8938c2ecf20Sopenharmony_ci		D_RATE("Neither active nor search matches tx rate\n");
8948c2ecf20Sopenharmony_ci		tmp_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
8958c2ecf20Sopenharmony_ci		D_RATE("active- lq:%x, ant:%x, SGI:%d\n", tmp_tbl->lq_type,
8968c2ecf20Sopenharmony_ci		       tmp_tbl->ant_type, tmp_tbl->is_SGI);
8978c2ecf20Sopenharmony_ci		tmp_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
8988c2ecf20Sopenharmony_ci		D_RATE("search- lq:%x, ant:%x, SGI:%d\n", tmp_tbl->lq_type,
8998c2ecf20Sopenharmony_ci		       tmp_tbl->ant_type, tmp_tbl->is_SGI);
9008c2ecf20Sopenharmony_ci		D_RATE("actual- lq:%x, ant:%x, SGI:%d\n", tbl_type.lq_type,
9018c2ecf20Sopenharmony_ci		       tbl_type.ant_type, tbl_type.is_SGI);
9028c2ecf20Sopenharmony_ci		/*
9038c2ecf20Sopenharmony_ci		 * no matching table found, let's by-pass the data collection
9048c2ecf20Sopenharmony_ci		 * and continue to perform rate scale to find the rate table
9058c2ecf20Sopenharmony_ci		 */
9068c2ecf20Sopenharmony_ci		il4965_rs_stay_in_table(lq_sta, true);
9078c2ecf20Sopenharmony_ci		goto done;
9088c2ecf20Sopenharmony_ci	}
9098c2ecf20Sopenharmony_ci
9108c2ecf20Sopenharmony_ci	/*
9118c2ecf20Sopenharmony_ci	 * Updating the frame history depends on whether packets were
9128c2ecf20Sopenharmony_ci	 * aggregated.
9138c2ecf20Sopenharmony_ci	 *
9148c2ecf20Sopenharmony_ci	 * For aggregation, all packets were transmitted at the same rate, the
9158c2ecf20Sopenharmony_ci	 * first idx into rate scale table.
9168c2ecf20Sopenharmony_ci	 */
9178c2ecf20Sopenharmony_ci	if (info->flags & IEEE80211_TX_STAT_AMPDU) {
9188c2ecf20Sopenharmony_ci		tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
9198c2ecf20Sopenharmony_ci		il4965_rs_get_tbl_info_from_mcs(tx_rate, il->band, &tbl_type,
9208c2ecf20Sopenharmony_ci						&rs_idx);
9218c2ecf20Sopenharmony_ci		il4965_rs_collect_tx_data(curr_tbl, rs_idx,
9228c2ecf20Sopenharmony_ci					  info->status.ampdu_len,
9238c2ecf20Sopenharmony_ci					  info->status.ampdu_ack_len);
9248c2ecf20Sopenharmony_ci
9258c2ecf20Sopenharmony_ci		/* Update success/fail counts if not searching for new mode */
9268c2ecf20Sopenharmony_ci		if (lq_sta->stay_in_tbl) {
9278c2ecf20Sopenharmony_ci			lq_sta->total_success += info->status.ampdu_ack_len;
9288c2ecf20Sopenharmony_ci			lq_sta->total_failed +=
9298c2ecf20Sopenharmony_ci			    (info->status.ampdu_len -
9308c2ecf20Sopenharmony_ci			     info->status.ampdu_ack_len);
9318c2ecf20Sopenharmony_ci		}
9328c2ecf20Sopenharmony_ci	} else {
9338c2ecf20Sopenharmony_ci		/*
9348c2ecf20Sopenharmony_ci		 * For legacy, update frame history with for each Tx retry.
9358c2ecf20Sopenharmony_ci		 */
9368c2ecf20Sopenharmony_ci		retries = info->status.rates[0].count - 1;
9378c2ecf20Sopenharmony_ci		/* HW doesn't send more than 15 retries */
9388c2ecf20Sopenharmony_ci		retries = min(retries, 15);
9398c2ecf20Sopenharmony_ci
9408c2ecf20Sopenharmony_ci		/* The last transmission may have been successful */
9418c2ecf20Sopenharmony_ci		legacy_success = !!(info->flags & IEEE80211_TX_STAT_ACK);
9428c2ecf20Sopenharmony_ci		/* Collect data for each rate used during failed TX attempts */
9438c2ecf20Sopenharmony_ci		for (i = 0; i <= retries; ++i) {
9448c2ecf20Sopenharmony_ci			tx_rate = le32_to_cpu(table->rs_table[i].rate_n_flags);
9458c2ecf20Sopenharmony_ci			il4965_rs_get_tbl_info_from_mcs(tx_rate, il->band,
9468c2ecf20Sopenharmony_ci							&tbl_type, &rs_idx);
9478c2ecf20Sopenharmony_ci			/*
9488c2ecf20Sopenharmony_ci			 * Only collect stats if retried rate is in the same RS
9498c2ecf20Sopenharmony_ci			 * table as active/search.
9508c2ecf20Sopenharmony_ci			 */
9518c2ecf20Sopenharmony_ci			if (il4965_table_type_matches(&tbl_type, curr_tbl))
9528c2ecf20Sopenharmony_ci				tmp_tbl = curr_tbl;
9538c2ecf20Sopenharmony_ci			else if (il4965_table_type_matches
9548c2ecf20Sopenharmony_ci				 (&tbl_type, other_tbl))
9558c2ecf20Sopenharmony_ci				tmp_tbl = other_tbl;
9568c2ecf20Sopenharmony_ci			else
9578c2ecf20Sopenharmony_ci				continue;
9588c2ecf20Sopenharmony_ci			il4965_rs_collect_tx_data(tmp_tbl, rs_idx, 1,
9598c2ecf20Sopenharmony_ci						  i <
9608c2ecf20Sopenharmony_ci						  retries ? 0 : legacy_success);
9618c2ecf20Sopenharmony_ci		}
9628c2ecf20Sopenharmony_ci
9638c2ecf20Sopenharmony_ci		/* Update success/fail counts if not searching for new mode */
9648c2ecf20Sopenharmony_ci		if (lq_sta->stay_in_tbl) {
9658c2ecf20Sopenharmony_ci			lq_sta->total_success += legacy_success;
9668c2ecf20Sopenharmony_ci			lq_sta->total_failed += retries + (1 - legacy_success);
9678c2ecf20Sopenharmony_ci		}
9688c2ecf20Sopenharmony_ci	}
9698c2ecf20Sopenharmony_ci	/* The last TX rate is cached in lq_sta; it's set in if/else above */
9708c2ecf20Sopenharmony_ci	lq_sta->last_rate_n_flags = tx_rate;
9718c2ecf20Sopenharmony_cidone:
9728c2ecf20Sopenharmony_ci	/* See if there's a better rate or modulation mode to try. */
9738c2ecf20Sopenharmony_ci	if (sta->supp_rates[sband->band])
9748c2ecf20Sopenharmony_ci		il4965_rs_rate_scale_perform(il, skb, sta, lq_sta);
9758c2ecf20Sopenharmony_ci}
9768c2ecf20Sopenharmony_ci
9778c2ecf20Sopenharmony_ci/*
9788c2ecf20Sopenharmony_ci * Begin a period of staying with a selected modulation mode.
9798c2ecf20Sopenharmony_ci * Set "stay_in_tbl" flag to prevent any mode switches.
9808c2ecf20Sopenharmony_ci * Set frame tx success limits according to legacy vs. high-throughput,
9818c2ecf20Sopenharmony_ci * and reset overall (spanning all rates) tx success history stats.
9828c2ecf20Sopenharmony_ci * These control how long we stay using same modulation mode before
9838c2ecf20Sopenharmony_ci * searching for a new mode.
9848c2ecf20Sopenharmony_ci */
9858c2ecf20Sopenharmony_cistatic void
9868c2ecf20Sopenharmony_ciil4965_rs_set_stay_in_table(struct il_priv *il, u8 is_legacy,
9878c2ecf20Sopenharmony_ci			    struct il_lq_sta *lq_sta)
9888c2ecf20Sopenharmony_ci{
9898c2ecf20Sopenharmony_ci	D_RATE("we are staying in the same table\n");
9908c2ecf20Sopenharmony_ci	lq_sta->stay_in_tbl = 1;	/* only place this gets set */
9918c2ecf20Sopenharmony_ci	if (is_legacy) {
9928c2ecf20Sopenharmony_ci		lq_sta->table_count_limit = IL_LEGACY_TBL_COUNT;
9938c2ecf20Sopenharmony_ci		lq_sta->max_failure_limit = IL_LEGACY_FAILURE_LIMIT;
9948c2ecf20Sopenharmony_ci		lq_sta->max_success_limit = IL_LEGACY_SUCCESS_LIMIT;
9958c2ecf20Sopenharmony_ci	} else {
9968c2ecf20Sopenharmony_ci		lq_sta->table_count_limit = IL_NONE_LEGACY_TBL_COUNT;
9978c2ecf20Sopenharmony_ci		lq_sta->max_failure_limit = IL_NONE_LEGACY_FAILURE_LIMIT;
9988c2ecf20Sopenharmony_ci		lq_sta->max_success_limit = IL_NONE_LEGACY_SUCCESS_LIMIT;
9998c2ecf20Sopenharmony_ci	}
10008c2ecf20Sopenharmony_ci	lq_sta->table_count = 0;
10018c2ecf20Sopenharmony_ci	lq_sta->total_failed = 0;
10028c2ecf20Sopenharmony_ci	lq_sta->total_success = 0;
10038c2ecf20Sopenharmony_ci	lq_sta->flush_timer = jiffies;
10048c2ecf20Sopenharmony_ci	lq_sta->action_counter = 0;
10058c2ecf20Sopenharmony_ci}
10068c2ecf20Sopenharmony_ci
10078c2ecf20Sopenharmony_ci/*
10088c2ecf20Sopenharmony_ci * Find correct throughput table for given mode of modulation
10098c2ecf20Sopenharmony_ci */
10108c2ecf20Sopenharmony_cistatic void
10118c2ecf20Sopenharmony_ciil4965_rs_set_expected_tpt_table(struct il_lq_sta *lq_sta,
10128c2ecf20Sopenharmony_ci				 struct il_scale_tbl_info *tbl)
10138c2ecf20Sopenharmony_ci{
10148c2ecf20Sopenharmony_ci	/* Used to choose among HT tables */
10158c2ecf20Sopenharmony_ci	s32(*ht_tbl_pointer)[RATE_COUNT];
10168c2ecf20Sopenharmony_ci
10178c2ecf20Sopenharmony_ci	/* Check for invalid LQ type */
10188c2ecf20Sopenharmony_ci	if (WARN_ON_ONCE(!is_legacy(tbl->lq_type) && !is_Ht(tbl->lq_type))) {
10198c2ecf20Sopenharmony_ci		tbl->expected_tpt = expected_tpt_legacy;
10208c2ecf20Sopenharmony_ci		return;
10218c2ecf20Sopenharmony_ci	}
10228c2ecf20Sopenharmony_ci
10238c2ecf20Sopenharmony_ci	/* Legacy rates have only one table */
10248c2ecf20Sopenharmony_ci	if (is_legacy(tbl->lq_type)) {
10258c2ecf20Sopenharmony_ci		tbl->expected_tpt = expected_tpt_legacy;
10268c2ecf20Sopenharmony_ci		return;
10278c2ecf20Sopenharmony_ci	}
10288c2ecf20Sopenharmony_ci
10298c2ecf20Sopenharmony_ci	/* Choose among many HT tables depending on number of streams
10308c2ecf20Sopenharmony_ci	 * (SISO/MIMO2), channel width (20/40), SGI, and aggregation
10318c2ecf20Sopenharmony_ci	 * status */
10328c2ecf20Sopenharmony_ci	if (is_siso(tbl->lq_type) && (!tbl->is_ht40 || lq_sta->is_dup))
10338c2ecf20Sopenharmony_ci		ht_tbl_pointer = expected_tpt_siso20MHz;
10348c2ecf20Sopenharmony_ci	else if (is_siso(tbl->lq_type))
10358c2ecf20Sopenharmony_ci		ht_tbl_pointer = expected_tpt_siso40MHz;
10368c2ecf20Sopenharmony_ci	else if (is_mimo2(tbl->lq_type) && (!tbl->is_ht40 || lq_sta->is_dup))
10378c2ecf20Sopenharmony_ci		ht_tbl_pointer = expected_tpt_mimo2_20MHz;
10388c2ecf20Sopenharmony_ci	else			/* if (is_mimo2(tbl->lq_type)) <-- must be true */
10398c2ecf20Sopenharmony_ci		ht_tbl_pointer = expected_tpt_mimo2_40MHz;
10408c2ecf20Sopenharmony_ci
10418c2ecf20Sopenharmony_ci	if (!tbl->is_SGI && !lq_sta->is_agg)	/* Normal */
10428c2ecf20Sopenharmony_ci		tbl->expected_tpt = ht_tbl_pointer[0];
10438c2ecf20Sopenharmony_ci	else if (tbl->is_SGI && !lq_sta->is_agg)	/* SGI */
10448c2ecf20Sopenharmony_ci		tbl->expected_tpt = ht_tbl_pointer[1];
10458c2ecf20Sopenharmony_ci	else if (!tbl->is_SGI && lq_sta->is_agg)	/* AGG */
10468c2ecf20Sopenharmony_ci		tbl->expected_tpt = ht_tbl_pointer[2];
10478c2ecf20Sopenharmony_ci	else			/* AGG+SGI */
10488c2ecf20Sopenharmony_ci		tbl->expected_tpt = ht_tbl_pointer[3];
10498c2ecf20Sopenharmony_ci}
10508c2ecf20Sopenharmony_ci
10518c2ecf20Sopenharmony_ci/*
10528c2ecf20Sopenharmony_ci * Find starting rate for new "search" high-throughput mode of modulation.
10538c2ecf20Sopenharmony_ci * Goal is to find lowest expected rate (under perfect conditions) that is
10548c2ecf20Sopenharmony_ci * above the current measured throughput of "active" mode, to give new mode
10558c2ecf20Sopenharmony_ci * a fair chance to prove itself without too many challenges.
10568c2ecf20Sopenharmony_ci *
10578c2ecf20Sopenharmony_ci * This gets called when transitioning to more aggressive modulation
10588c2ecf20Sopenharmony_ci * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
10598c2ecf20Sopenharmony_ci * (i.e. MIMO to SISO).  When moving to MIMO, bit rate will typically need
10608c2ecf20Sopenharmony_ci * to decrease to match "active" throughput.  When moving from MIMO to SISO,
10618c2ecf20Sopenharmony_ci * bit rate will typically need to increase, but not if performance was bad.
10628c2ecf20Sopenharmony_ci */
10638c2ecf20Sopenharmony_cistatic s32
10648c2ecf20Sopenharmony_ciil4965_rs_get_best_rate(struct il_priv *il, struct il_lq_sta *lq_sta,
10658c2ecf20Sopenharmony_ci			struct il_scale_tbl_info *tbl,	/* "search" */
10668c2ecf20Sopenharmony_ci			u16 rate_mask, s8 idx)
10678c2ecf20Sopenharmony_ci{
10688c2ecf20Sopenharmony_ci	/* "active" values */
10698c2ecf20Sopenharmony_ci	struct il_scale_tbl_info *active_tbl =
10708c2ecf20Sopenharmony_ci	    &(lq_sta->lq_info[lq_sta->active_tbl]);
10718c2ecf20Sopenharmony_ci	s32 active_sr = active_tbl->win[idx].success_ratio;
10728c2ecf20Sopenharmony_ci	s32 active_tpt = active_tbl->expected_tpt[idx];
10738c2ecf20Sopenharmony_ci
10748c2ecf20Sopenharmony_ci	/* expected "search" throughput */
10758c2ecf20Sopenharmony_ci	s32 *tpt_tbl = tbl->expected_tpt;
10768c2ecf20Sopenharmony_ci
10778c2ecf20Sopenharmony_ci	s32 new_rate, high, low, start_hi;
10788c2ecf20Sopenharmony_ci	u16 high_low;
10798c2ecf20Sopenharmony_ci	s8 rate = idx;
10808c2ecf20Sopenharmony_ci
10818c2ecf20Sopenharmony_ci	new_rate = high = low = start_hi = RATE_INVALID;
10828c2ecf20Sopenharmony_ci
10838c2ecf20Sopenharmony_ci	for (;;) {
10848c2ecf20Sopenharmony_ci		high_low =
10858c2ecf20Sopenharmony_ci		    il4965_rs_get_adjacent_rate(il, rate, rate_mask,
10868c2ecf20Sopenharmony_ci						tbl->lq_type);
10878c2ecf20Sopenharmony_ci
10888c2ecf20Sopenharmony_ci		low = high_low & 0xff;
10898c2ecf20Sopenharmony_ci		high = (high_low >> 8) & 0xff;
10908c2ecf20Sopenharmony_ci
10918c2ecf20Sopenharmony_ci		/*
10928c2ecf20Sopenharmony_ci		 * Lower the "search" bit rate, to give new "search" mode
10938c2ecf20Sopenharmony_ci		 * approximately the same throughput as "active" if:
10948c2ecf20Sopenharmony_ci		 *
10958c2ecf20Sopenharmony_ci		 * 1) "Active" mode has been working modestly well (but not
10968c2ecf20Sopenharmony_ci		 *    great), and expected "search" throughput (under perfect
10978c2ecf20Sopenharmony_ci		 *    conditions) at candidate rate is above the actual
10988c2ecf20Sopenharmony_ci		 *    measured "active" throughput (but less than expected
10998c2ecf20Sopenharmony_ci		 *    "active" throughput under perfect conditions).
11008c2ecf20Sopenharmony_ci		 * OR
11018c2ecf20Sopenharmony_ci		 * 2) "Active" mode has been working perfectly or very well
11028c2ecf20Sopenharmony_ci		 *    and expected "search" throughput (under perfect
11038c2ecf20Sopenharmony_ci		 *    conditions) at candidate rate is above expected
11048c2ecf20Sopenharmony_ci		 *    "active" throughput (under perfect conditions).
11058c2ecf20Sopenharmony_ci		 */
11068c2ecf20Sopenharmony_ci		if ((100 * tpt_tbl[rate] > lq_sta->last_tpt &&
11078c2ecf20Sopenharmony_ci		     (active_sr > RATE_DECREASE_TH && active_sr <= RATE_HIGH_TH
11088c2ecf20Sopenharmony_ci		      && tpt_tbl[rate] <= active_tpt)) ||
11098c2ecf20Sopenharmony_ci		    (active_sr >= RATE_SCALE_SWITCH &&
11108c2ecf20Sopenharmony_ci		     tpt_tbl[rate] > active_tpt)) {
11118c2ecf20Sopenharmony_ci
11128c2ecf20Sopenharmony_ci			/* (2nd or later pass)
11138c2ecf20Sopenharmony_ci			 * If we've already tried to raise the rate, and are
11148c2ecf20Sopenharmony_ci			 * now trying to lower it, use the higher rate. */
11158c2ecf20Sopenharmony_ci			if (start_hi != RATE_INVALID) {
11168c2ecf20Sopenharmony_ci				new_rate = start_hi;
11178c2ecf20Sopenharmony_ci				break;
11188c2ecf20Sopenharmony_ci			}
11198c2ecf20Sopenharmony_ci
11208c2ecf20Sopenharmony_ci			new_rate = rate;
11218c2ecf20Sopenharmony_ci
11228c2ecf20Sopenharmony_ci			/* Loop again with lower rate */
11238c2ecf20Sopenharmony_ci			if (low != RATE_INVALID)
11248c2ecf20Sopenharmony_ci				rate = low;
11258c2ecf20Sopenharmony_ci
11268c2ecf20Sopenharmony_ci			/* Lower rate not available, use the original */
11278c2ecf20Sopenharmony_ci			else
11288c2ecf20Sopenharmony_ci				break;
11298c2ecf20Sopenharmony_ci
11308c2ecf20Sopenharmony_ci			/* Else try to raise the "search" rate to match "active" */
11318c2ecf20Sopenharmony_ci		} else {
11328c2ecf20Sopenharmony_ci			/* (2nd or later pass)
11338c2ecf20Sopenharmony_ci			 * If we've already tried to lower the rate, and are
11348c2ecf20Sopenharmony_ci			 * now trying to raise it, use the lower rate. */
11358c2ecf20Sopenharmony_ci			if (new_rate != RATE_INVALID)
11368c2ecf20Sopenharmony_ci				break;
11378c2ecf20Sopenharmony_ci
11388c2ecf20Sopenharmony_ci			/* Loop again with higher rate */
11398c2ecf20Sopenharmony_ci			else if (high != RATE_INVALID) {
11408c2ecf20Sopenharmony_ci				start_hi = high;
11418c2ecf20Sopenharmony_ci				rate = high;
11428c2ecf20Sopenharmony_ci
11438c2ecf20Sopenharmony_ci				/* Higher rate not available, use the original */
11448c2ecf20Sopenharmony_ci			} else {
11458c2ecf20Sopenharmony_ci				new_rate = rate;
11468c2ecf20Sopenharmony_ci				break;
11478c2ecf20Sopenharmony_ci			}
11488c2ecf20Sopenharmony_ci		}
11498c2ecf20Sopenharmony_ci	}
11508c2ecf20Sopenharmony_ci
11518c2ecf20Sopenharmony_ci	return new_rate;
11528c2ecf20Sopenharmony_ci}
11538c2ecf20Sopenharmony_ci
11548c2ecf20Sopenharmony_ci/*
11558c2ecf20Sopenharmony_ci * Set up search table for MIMO2
11568c2ecf20Sopenharmony_ci */
11578c2ecf20Sopenharmony_cistatic int
11588c2ecf20Sopenharmony_ciil4965_rs_switch_to_mimo2(struct il_priv *il, struct il_lq_sta *lq_sta,
11598c2ecf20Sopenharmony_ci			  struct ieee80211_conf *conf,
11608c2ecf20Sopenharmony_ci			  struct ieee80211_sta *sta,
11618c2ecf20Sopenharmony_ci			  struct il_scale_tbl_info *tbl, int idx)
11628c2ecf20Sopenharmony_ci{
11638c2ecf20Sopenharmony_ci	u16 rate_mask;
11648c2ecf20Sopenharmony_ci	s32 rate;
11658c2ecf20Sopenharmony_ci	s8 is_green = lq_sta->is_green;
11668c2ecf20Sopenharmony_ci
11678c2ecf20Sopenharmony_ci	if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
11688c2ecf20Sopenharmony_ci		return -1;
11698c2ecf20Sopenharmony_ci
11708c2ecf20Sopenharmony_ci	if (sta->smps_mode == IEEE80211_SMPS_STATIC)
11718c2ecf20Sopenharmony_ci		return -1;
11728c2ecf20Sopenharmony_ci
11738c2ecf20Sopenharmony_ci	/* Need both Tx chains/antennas to support MIMO */
11748c2ecf20Sopenharmony_ci	if (il->hw_params.tx_chains_num < 2)
11758c2ecf20Sopenharmony_ci		return -1;
11768c2ecf20Sopenharmony_ci
11778c2ecf20Sopenharmony_ci	D_RATE("LQ: try to switch to MIMO2\n");
11788c2ecf20Sopenharmony_ci
11798c2ecf20Sopenharmony_ci	tbl->lq_type = LQ_MIMO2;
11808c2ecf20Sopenharmony_ci	tbl->is_dup = lq_sta->is_dup;
11818c2ecf20Sopenharmony_ci	tbl->action = 0;
11828c2ecf20Sopenharmony_ci	tbl->max_search = IL_MAX_SEARCH;
11838c2ecf20Sopenharmony_ci	rate_mask = lq_sta->active_mimo2_rate;
11848c2ecf20Sopenharmony_ci
11858c2ecf20Sopenharmony_ci	if (il_is_ht40_tx_allowed(il, &sta->ht_cap))
11868c2ecf20Sopenharmony_ci		tbl->is_ht40 = 1;
11878c2ecf20Sopenharmony_ci	else
11888c2ecf20Sopenharmony_ci		tbl->is_ht40 = 0;
11898c2ecf20Sopenharmony_ci
11908c2ecf20Sopenharmony_ci	il4965_rs_set_expected_tpt_table(lq_sta, tbl);
11918c2ecf20Sopenharmony_ci
11928c2ecf20Sopenharmony_ci	rate = il4965_rs_get_best_rate(il, lq_sta, tbl, rate_mask, idx);
11938c2ecf20Sopenharmony_ci
11948c2ecf20Sopenharmony_ci	D_RATE("LQ: MIMO2 best rate %d mask %X\n", rate, rate_mask);
11958c2ecf20Sopenharmony_ci	if (rate == RATE_INVALID || !((1 << rate) & rate_mask)) {
11968c2ecf20Sopenharmony_ci		D_RATE("Can't switch with idx %d rate mask %x\n", rate,
11978c2ecf20Sopenharmony_ci		       rate_mask);
11988c2ecf20Sopenharmony_ci		return -1;
11998c2ecf20Sopenharmony_ci	}
12008c2ecf20Sopenharmony_ci	tbl->current_rate =
12018c2ecf20Sopenharmony_ci	    il4965_rate_n_flags_from_tbl(il, tbl, rate, is_green);
12028c2ecf20Sopenharmony_ci
12038c2ecf20Sopenharmony_ci	D_RATE("LQ: Switch to new mcs %X idx is green %X\n", tbl->current_rate,
12048c2ecf20Sopenharmony_ci	       is_green);
12058c2ecf20Sopenharmony_ci	return 0;
12068c2ecf20Sopenharmony_ci}
12078c2ecf20Sopenharmony_ci
12088c2ecf20Sopenharmony_ci/*
12098c2ecf20Sopenharmony_ci * Set up search table for SISO
12108c2ecf20Sopenharmony_ci */
12118c2ecf20Sopenharmony_cistatic int
12128c2ecf20Sopenharmony_ciil4965_rs_switch_to_siso(struct il_priv *il, struct il_lq_sta *lq_sta,
12138c2ecf20Sopenharmony_ci			 struct ieee80211_conf *conf, struct ieee80211_sta *sta,
12148c2ecf20Sopenharmony_ci			 struct il_scale_tbl_info *tbl, int idx)
12158c2ecf20Sopenharmony_ci{
12168c2ecf20Sopenharmony_ci	u16 rate_mask;
12178c2ecf20Sopenharmony_ci	u8 is_green = lq_sta->is_green;
12188c2ecf20Sopenharmony_ci	s32 rate;
12198c2ecf20Sopenharmony_ci
12208c2ecf20Sopenharmony_ci	if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
12218c2ecf20Sopenharmony_ci		return -1;
12228c2ecf20Sopenharmony_ci
12238c2ecf20Sopenharmony_ci	D_RATE("LQ: try to switch to SISO\n");
12248c2ecf20Sopenharmony_ci
12258c2ecf20Sopenharmony_ci	tbl->is_dup = lq_sta->is_dup;
12268c2ecf20Sopenharmony_ci	tbl->lq_type = LQ_SISO;
12278c2ecf20Sopenharmony_ci	tbl->action = 0;
12288c2ecf20Sopenharmony_ci	tbl->max_search = IL_MAX_SEARCH;
12298c2ecf20Sopenharmony_ci	rate_mask = lq_sta->active_siso_rate;
12308c2ecf20Sopenharmony_ci
12318c2ecf20Sopenharmony_ci	if (il_is_ht40_tx_allowed(il, &sta->ht_cap))
12328c2ecf20Sopenharmony_ci		tbl->is_ht40 = 1;
12338c2ecf20Sopenharmony_ci	else
12348c2ecf20Sopenharmony_ci		tbl->is_ht40 = 0;
12358c2ecf20Sopenharmony_ci
12368c2ecf20Sopenharmony_ci	if (is_green)
12378c2ecf20Sopenharmony_ci		tbl->is_SGI = 0;	/*11n spec: no SGI in SISO+Greenfield */
12388c2ecf20Sopenharmony_ci
12398c2ecf20Sopenharmony_ci	il4965_rs_set_expected_tpt_table(lq_sta, tbl);
12408c2ecf20Sopenharmony_ci	rate = il4965_rs_get_best_rate(il, lq_sta, tbl, rate_mask, idx);
12418c2ecf20Sopenharmony_ci
12428c2ecf20Sopenharmony_ci	D_RATE("LQ: get best rate %d mask %X\n", rate, rate_mask);
12438c2ecf20Sopenharmony_ci	if (rate == RATE_INVALID || !((1 << rate) & rate_mask)) {
12448c2ecf20Sopenharmony_ci		D_RATE("can not switch with idx %d rate mask %x\n", rate,
12458c2ecf20Sopenharmony_ci		       rate_mask);
12468c2ecf20Sopenharmony_ci		return -1;
12478c2ecf20Sopenharmony_ci	}
12488c2ecf20Sopenharmony_ci	tbl->current_rate =
12498c2ecf20Sopenharmony_ci	    il4965_rate_n_flags_from_tbl(il, tbl, rate, is_green);
12508c2ecf20Sopenharmony_ci	D_RATE("LQ: Switch to new mcs %X idx is green %X\n", tbl->current_rate,
12518c2ecf20Sopenharmony_ci	       is_green);
12528c2ecf20Sopenharmony_ci	return 0;
12538c2ecf20Sopenharmony_ci}
12548c2ecf20Sopenharmony_ci
12558c2ecf20Sopenharmony_ci/*
12568c2ecf20Sopenharmony_ci * Try to switch to new modulation mode from legacy
12578c2ecf20Sopenharmony_ci */
12588c2ecf20Sopenharmony_cistatic int
12598c2ecf20Sopenharmony_ciil4965_rs_move_legacy_other(struct il_priv *il, struct il_lq_sta *lq_sta,
12608c2ecf20Sopenharmony_ci			    struct ieee80211_conf *conf,
12618c2ecf20Sopenharmony_ci			    struct ieee80211_sta *sta, int idx)
12628c2ecf20Sopenharmony_ci{
12638c2ecf20Sopenharmony_ci	struct il_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
12648c2ecf20Sopenharmony_ci	struct il_scale_tbl_info *search_tbl =
12658c2ecf20Sopenharmony_ci	    &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
12668c2ecf20Sopenharmony_ci	struct il_rate_scale_data *win = &(tbl->win[idx]);
12678c2ecf20Sopenharmony_ci	u32 sz =
12688c2ecf20Sopenharmony_ci	    (sizeof(struct il_scale_tbl_info) -
12698c2ecf20Sopenharmony_ci	     (sizeof(struct il_rate_scale_data) * RATE_COUNT));
12708c2ecf20Sopenharmony_ci	u8 start_action;
12718c2ecf20Sopenharmony_ci	u8 valid_tx_ant = il->hw_params.valid_tx_ant;
12728c2ecf20Sopenharmony_ci	u8 tx_chains_num = il->hw_params.tx_chains_num;
12738c2ecf20Sopenharmony_ci	int ret = 0;
12748c2ecf20Sopenharmony_ci	u8 update_search_tbl_counter = 0;
12758c2ecf20Sopenharmony_ci
12768c2ecf20Sopenharmony_ci	tbl->action = IL_LEGACY_SWITCH_SISO;
12778c2ecf20Sopenharmony_ci
12788c2ecf20Sopenharmony_ci	start_action = tbl->action;
12798c2ecf20Sopenharmony_ci	for (;;) {
12808c2ecf20Sopenharmony_ci		lq_sta->action_counter++;
12818c2ecf20Sopenharmony_ci		switch (tbl->action) {
12828c2ecf20Sopenharmony_ci		case IL_LEGACY_SWITCH_ANTENNA1:
12838c2ecf20Sopenharmony_ci		case IL_LEGACY_SWITCH_ANTENNA2:
12848c2ecf20Sopenharmony_ci			D_RATE("LQ: Legacy toggle Antenna\n");
12858c2ecf20Sopenharmony_ci
12868c2ecf20Sopenharmony_ci			if ((tbl->action == IL_LEGACY_SWITCH_ANTENNA1 &&
12878c2ecf20Sopenharmony_ci			     tx_chains_num <= 1) ||
12888c2ecf20Sopenharmony_ci			    (tbl->action == IL_LEGACY_SWITCH_ANTENNA2 &&
12898c2ecf20Sopenharmony_ci			     tx_chains_num <= 2))
12908c2ecf20Sopenharmony_ci				break;
12918c2ecf20Sopenharmony_ci
12928c2ecf20Sopenharmony_ci			/* Don't change antenna if success has been great */
12938c2ecf20Sopenharmony_ci			if (win->success_ratio >= IL_RS_GOOD_RATIO)
12948c2ecf20Sopenharmony_ci				break;
12958c2ecf20Sopenharmony_ci
12968c2ecf20Sopenharmony_ci			/* Set up search table to try other antenna */
12978c2ecf20Sopenharmony_ci			memcpy(search_tbl, tbl, sz);
12988c2ecf20Sopenharmony_ci
12998c2ecf20Sopenharmony_ci			if (il4965_rs_toggle_antenna
13008c2ecf20Sopenharmony_ci			    (valid_tx_ant, &search_tbl->current_rate,
13018c2ecf20Sopenharmony_ci			     search_tbl)) {
13028c2ecf20Sopenharmony_ci				update_search_tbl_counter = 1;
13038c2ecf20Sopenharmony_ci				il4965_rs_set_expected_tpt_table(lq_sta,
13048c2ecf20Sopenharmony_ci								 search_tbl);
13058c2ecf20Sopenharmony_ci				goto out;
13068c2ecf20Sopenharmony_ci			}
13078c2ecf20Sopenharmony_ci			break;
13088c2ecf20Sopenharmony_ci		case IL_LEGACY_SWITCH_SISO:
13098c2ecf20Sopenharmony_ci			D_RATE("LQ: Legacy switch to SISO\n");
13108c2ecf20Sopenharmony_ci
13118c2ecf20Sopenharmony_ci			/* Set up search table to try SISO */
13128c2ecf20Sopenharmony_ci			memcpy(search_tbl, tbl, sz);
13138c2ecf20Sopenharmony_ci			search_tbl->is_SGI = 0;
13148c2ecf20Sopenharmony_ci			ret =
13158c2ecf20Sopenharmony_ci			    il4965_rs_switch_to_siso(il, lq_sta, conf, sta,
13168c2ecf20Sopenharmony_ci						     search_tbl, idx);
13178c2ecf20Sopenharmony_ci			if (!ret) {
13188c2ecf20Sopenharmony_ci				lq_sta->action_counter = 0;
13198c2ecf20Sopenharmony_ci				goto out;
13208c2ecf20Sopenharmony_ci			}
13218c2ecf20Sopenharmony_ci
13228c2ecf20Sopenharmony_ci			break;
13238c2ecf20Sopenharmony_ci		case IL_LEGACY_SWITCH_MIMO2_AB:
13248c2ecf20Sopenharmony_ci		case IL_LEGACY_SWITCH_MIMO2_AC:
13258c2ecf20Sopenharmony_ci		case IL_LEGACY_SWITCH_MIMO2_BC:
13268c2ecf20Sopenharmony_ci			D_RATE("LQ: Legacy switch to MIMO2\n");
13278c2ecf20Sopenharmony_ci
13288c2ecf20Sopenharmony_ci			/* Set up search table to try MIMO */
13298c2ecf20Sopenharmony_ci			memcpy(search_tbl, tbl, sz);
13308c2ecf20Sopenharmony_ci			search_tbl->is_SGI = 0;
13318c2ecf20Sopenharmony_ci
13328c2ecf20Sopenharmony_ci			if (tbl->action == IL_LEGACY_SWITCH_MIMO2_AB)
13338c2ecf20Sopenharmony_ci				search_tbl->ant_type = ANT_AB;
13348c2ecf20Sopenharmony_ci			else if (tbl->action == IL_LEGACY_SWITCH_MIMO2_AC)
13358c2ecf20Sopenharmony_ci				search_tbl->ant_type = ANT_AC;
13368c2ecf20Sopenharmony_ci			else
13378c2ecf20Sopenharmony_ci				search_tbl->ant_type = ANT_BC;
13388c2ecf20Sopenharmony_ci
13398c2ecf20Sopenharmony_ci			if (!il4965_rs_is_valid_ant
13408c2ecf20Sopenharmony_ci			    (valid_tx_ant, search_tbl->ant_type))
13418c2ecf20Sopenharmony_ci				break;
13428c2ecf20Sopenharmony_ci
13438c2ecf20Sopenharmony_ci			ret =
13448c2ecf20Sopenharmony_ci			    il4965_rs_switch_to_mimo2(il, lq_sta, conf, sta,
13458c2ecf20Sopenharmony_ci						      search_tbl, idx);
13468c2ecf20Sopenharmony_ci			if (!ret) {
13478c2ecf20Sopenharmony_ci				lq_sta->action_counter = 0;
13488c2ecf20Sopenharmony_ci				goto out;
13498c2ecf20Sopenharmony_ci			}
13508c2ecf20Sopenharmony_ci			break;
13518c2ecf20Sopenharmony_ci		}
13528c2ecf20Sopenharmony_ci		tbl->action++;
13538c2ecf20Sopenharmony_ci		if (tbl->action > IL_LEGACY_SWITCH_MIMO2_BC)
13548c2ecf20Sopenharmony_ci			tbl->action = IL_LEGACY_SWITCH_ANTENNA1;
13558c2ecf20Sopenharmony_ci
13568c2ecf20Sopenharmony_ci		if (tbl->action == start_action)
13578c2ecf20Sopenharmony_ci			break;
13588c2ecf20Sopenharmony_ci
13598c2ecf20Sopenharmony_ci	}
13608c2ecf20Sopenharmony_ci	search_tbl->lq_type = LQ_NONE;
13618c2ecf20Sopenharmony_ci	return 0;
13628c2ecf20Sopenharmony_ci
13638c2ecf20Sopenharmony_ciout:
13648c2ecf20Sopenharmony_ci	lq_sta->search_better_tbl = 1;
13658c2ecf20Sopenharmony_ci	tbl->action++;
13668c2ecf20Sopenharmony_ci	if (tbl->action > IL_LEGACY_SWITCH_MIMO2_BC)
13678c2ecf20Sopenharmony_ci		tbl->action = IL_LEGACY_SWITCH_ANTENNA1;
13688c2ecf20Sopenharmony_ci	if (update_search_tbl_counter)
13698c2ecf20Sopenharmony_ci		search_tbl->action = tbl->action;
13708c2ecf20Sopenharmony_ci	return 0;
13718c2ecf20Sopenharmony_ci
13728c2ecf20Sopenharmony_ci}
13738c2ecf20Sopenharmony_ci
13748c2ecf20Sopenharmony_ci/*
13758c2ecf20Sopenharmony_ci * Try to switch to new modulation mode from SISO
13768c2ecf20Sopenharmony_ci */
13778c2ecf20Sopenharmony_cistatic int
13788c2ecf20Sopenharmony_ciil4965_rs_move_siso_to_other(struct il_priv *il, struct il_lq_sta *lq_sta,
13798c2ecf20Sopenharmony_ci			     struct ieee80211_conf *conf,
13808c2ecf20Sopenharmony_ci			     struct ieee80211_sta *sta, int idx)
13818c2ecf20Sopenharmony_ci{
13828c2ecf20Sopenharmony_ci	u8 is_green = lq_sta->is_green;
13838c2ecf20Sopenharmony_ci	struct il_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
13848c2ecf20Sopenharmony_ci	struct il_scale_tbl_info *search_tbl =
13858c2ecf20Sopenharmony_ci	    &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
13868c2ecf20Sopenharmony_ci	struct il_rate_scale_data *win = &(tbl->win[idx]);
13878c2ecf20Sopenharmony_ci	struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
13888c2ecf20Sopenharmony_ci	u32 sz =
13898c2ecf20Sopenharmony_ci	    (sizeof(struct il_scale_tbl_info) -
13908c2ecf20Sopenharmony_ci	     (sizeof(struct il_rate_scale_data) * RATE_COUNT));
13918c2ecf20Sopenharmony_ci	u8 start_action;
13928c2ecf20Sopenharmony_ci	u8 valid_tx_ant = il->hw_params.valid_tx_ant;
13938c2ecf20Sopenharmony_ci	u8 tx_chains_num = il->hw_params.tx_chains_num;
13948c2ecf20Sopenharmony_ci	u8 update_search_tbl_counter = 0;
13958c2ecf20Sopenharmony_ci	int ret;
13968c2ecf20Sopenharmony_ci
13978c2ecf20Sopenharmony_ci	start_action = tbl->action;
13988c2ecf20Sopenharmony_ci
13998c2ecf20Sopenharmony_ci	for (;;) {
14008c2ecf20Sopenharmony_ci		lq_sta->action_counter++;
14018c2ecf20Sopenharmony_ci		switch (tbl->action) {
14028c2ecf20Sopenharmony_ci		case IL_SISO_SWITCH_ANTENNA1:
14038c2ecf20Sopenharmony_ci		case IL_SISO_SWITCH_ANTENNA2:
14048c2ecf20Sopenharmony_ci			D_RATE("LQ: SISO toggle Antenna\n");
14058c2ecf20Sopenharmony_ci			if ((tbl->action == IL_SISO_SWITCH_ANTENNA1 &&
14068c2ecf20Sopenharmony_ci			     tx_chains_num <= 1) ||
14078c2ecf20Sopenharmony_ci			    (tbl->action == IL_SISO_SWITCH_ANTENNA2 &&
14088c2ecf20Sopenharmony_ci			     tx_chains_num <= 2))
14098c2ecf20Sopenharmony_ci				break;
14108c2ecf20Sopenharmony_ci
14118c2ecf20Sopenharmony_ci			if (win->success_ratio >= IL_RS_GOOD_RATIO)
14128c2ecf20Sopenharmony_ci				break;
14138c2ecf20Sopenharmony_ci
14148c2ecf20Sopenharmony_ci			memcpy(search_tbl, tbl, sz);
14158c2ecf20Sopenharmony_ci			if (il4965_rs_toggle_antenna
14168c2ecf20Sopenharmony_ci			    (valid_tx_ant, &search_tbl->current_rate,
14178c2ecf20Sopenharmony_ci			     search_tbl)) {
14188c2ecf20Sopenharmony_ci				update_search_tbl_counter = 1;
14198c2ecf20Sopenharmony_ci				goto out;
14208c2ecf20Sopenharmony_ci			}
14218c2ecf20Sopenharmony_ci			break;
14228c2ecf20Sopenharmony_ci		case IL_SISO_SWITCH_MIMO2_AB:
14238c2ecf20Sopenharmony_ci		case IL_SISO_SWITCH_MIMO2_AC:
14248c2ecf20Sopenharmony_ci		case IL_SISO_SWITCH_MIMO2_BC:
14258c2ecf20Sopenharmony_ci			D_RATE("LQ: SISO switch to MIMO2\n");
14268c2ecf20Sopenharmony_ci			memcpy(search_tbl, tbl, sz);
14278c2ecf20Sopenharmony_ci			search_tbl->is_SGI = 0;
14288c2ecf20Sopenharmony_ci
14298c2ecf20Sopenharmony_ci			if (tbl->action == IL_SISO_SWITCH_MIMO2_AB)
14308c2ecf20Sopenharmony_ci				search_tbl->ant_type = ANT_AB;
14318c2ecf20Sopenharmony_ci			else if (tbl->action == IL_SISO_SWITCH_MIMO2_AC)
14328c2ecf20Sopenharmony_ci				search_tbl->ant_type = ANT_AC;
14338c2ecf20Sopenharmony_ci			else
14348c2ecf20Sopenharmony_ci				search_tbl->ant_type = ANT_BC;
14358c2ecf20Sopenharmony_ci
14368c2ecf20Sopenharmony_ci			if (!il4965_rs_is_valid_ant
14378c2ecf20Sopenharmony_ci			    (valid_tx_ant, search_tbl->ant_type))
14388c2ecf20Sopenharmony_ci				break;
14398c2ecf20Sopenharmony_ci
14408c2ecf20Sopenharmony_ci			ret =
14418c2ecf20Sopenharmony_ci			    il4965_rs_switch_to_mimo2(il, lq_sta, conf, sta,
14428c2ecf20Sopenharmony_ci						      search_tbl, idx);
14438c2ecf20Sopenharmony_ci			if (!ret)
14448c2ecf20Sopenharmony_ci				goto out;
14458c2ecf20Sopenharmony_ci			break;
14468c2ecf20Sopenharmony_ci		case IL_SISO_SWITCH_GI:
14478c2ecf20Sopenharmony_ci			if (!tbl->is_ht40 &&
14488c2ecf20Sopenharmony_ci			    !(ht_cap->cap & IEEE80211_HT_CAP_SGI_20))
14498c2ecf20Sopenharmony_ci				break;
14508c2ecf20Sopenharmony_ci			if (tbl->is_ht40 &&
14518c2ecf20Sopenharmony_ci			    !(ht_cap->cap & IEEE80211_HT_CAP_SGI_40))
14528c2ecf20Sopenharmony_ci				break;
14538c2ecf20Sopenharmony_ci
14548c2ecf20Sopenharmony_ci			D_RATE("LQ: SISO toggle SGI/NGI\n");
14558c2ecf20Sopenharmony_ci
14568c2ecf20Sopenharmony_ci			memcpy(search_tbl, tbl, sz);
14578c2ecf20Sopenharmony_ci			if (is_green) {
14588c2ecf20Sopenharmony_ci				if (!tbl->is_SGI)
14598c2ecf20Sopenharmony_ci					break;
14608c2ecf20Sopenharmony_ci				else
14618c2ecf20Sopenharmony_ci					IL_ERR("SGI was set in GF+SISO\n");
14628c2ecf20Sopenharmony_ci			}
14638c2ecf20Sopenharmony_ci			search_tbl->is_SGI = !tbl->is_SGI;
14648c2ecf20Sopenharmony_ci			il4965_rs_set_expected_tpt_table(lq_sta, search_tbl);
14658c2ecf20Sopenharmony_ci			if (tbl->is_SGI) {
14668c2ecf20Sopenharmony_ci				s32 tpt = lq_sta->last_tpt / 100;
14678c2ecf20Sopenharmony_ci				if (tpt >= search_tbl->expected_tpt[idx])
14688c2ecf20Sopenharmony_ci					break;
14698c2ecf20Sopenharmony_ci			}
14708c2ecf20Sopenharmony_ci			search_tbl->current_rate =
14718c2ecf20Sopenharmony_ci			    il4965_rate_n_flags_from_tbl(il, search_tbl, idx,
14728c2ecf20Sopenharmony_ci							 is_green);
14738c2ecf20Sopenharmony_ci			update_search_tbl_counter = 1;
14748c2ecf20Sopenharmony_ci			goto out;
14758c2ecf20Sopenharmony_ci		}
14768c2ecf20Sopenharmony_ci		tbl->action++;
14778c2ecf20Sopenharmony_ci		if (tbl->action > IL_SISO_SWITCH_GI)
14788c2ecf20Sopenharmony_ci			tbl->action = IL_SISO_SWITCH_ANTENNA1;
14798c2ecf20Sopenharmony_ci
14808c2ecf20Sopenharmony_ci		if (tbl->action == start_action)
14818c2ecf20Sopenharmony_ci			break;
14828c2ecf20Sopenharmony_ci	}
14838c2ecf20Sopenharmony_ci	search_tbl->lq_type = LQ_NONE;
14848c2ecf20Sopenharmony_ci	return 0;
14858c2ecf20Sopenharmony_ci
14868c2ecf20Sopenharmony_ciout:
14878c2ecf20Sopenharmony_ci	lq_sta->search_better_tbl = 1;
14888c2ecf20Sopenharmony_ci	tbl->action++;
14898c2ecf20Sopenharmony_ci	if (tbl->action > IL_SISO_SWITCH_GI)
14908c2ecf20Sopenharmony_ci		tbl->action = IL_SISO_SWITCH_ANTENNA1;
14918c2ecf20Sopenharmony_ci	if (update_search_tbl_counter)
14928c2ecf20Sopenharmony_ci		search_tbl->action = tbl->action;
14938c2ecf20Sopenharmony_ci
14948c2ecf20Sopenharmony_ci	return 0;
14958c2ecf20Sopenharmony_ci}
14968c2ecf20Sopenharmony_ci
14978c2ecf20Sopenharmony_ci/*
14988c2ecf20Sopenharmony_ci * Try to switch to new modulation mode from MIMO2
14998c2ecf20Sopenharmony_ci */
15008c2ecf20Sopenharmony_cistatic int
15018c2ecf20Sopenharmony_ciil4965_rs_move_mimo2_to_other(struct il_priv *il, struct il_lq_sta *lq_sta,
15028c2ecf20Sopenharmony_ci			      struct ieee80211_conf *conf,
15038c2ecf20Sopenharmony_ci			      struct ieee80211_sta *sta, int idx)
15048c2ecf20Sopenharmony_ci{
15058c2ecf20Sopenharmony_ci	s8 is_green = lq_sta->is_green;
15068c2ecf20Sopenharmony_ci	struct il_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
15078c2ecf20Sopenharmony_ci	struct il_scale_tbl_info *search_tbl =
15088c2ecf20Sopenharmony_ci	    &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
15098c2ecf20Sopenharmony_ci	struct il_rate_scale_data *win = &(tbl->win[idx]);
15108c2ecf20Sopenharmony_ci	struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
15118c2ecf20Sopenharmony_ci	u32 sz =
15128c2ecf20Sopenharmony_ci	    (sizeof(struct il_scale_tbl_info) -
15138c2ecf20Sopenharmony_ci	     (sizeof(struct il_rate_scale_data) * RATE_COUNT));
15148c2ecf20Sopenharmony_ci	u8 start_action;
15158c2ecf20Sopenharmony_ci	u8 valid_tx_ant = il->hw_params.valid_tx_ant;
15168c2ecf20Sopenharmony_ci	u8 tx_chains_num = il->hw_params.tx_chains_num;
15178c2ecf20Sopenharmony_ci	u8 update_search_tbl_counter = 0;
15188c2ecf20Sopenharmony_ci	int ret;
15198c2ecf20Sopenharmony_ci
15208c2ecf20Sopenharmony_ci	start_action = tbl->action;
15218c2ecf20Sopenharmony_ci	for (;;) {
15228c2ecf20Sopenharmony_ci		lq_sta->action_counter++;
15238c2ecf20Sopenharmony_ci		switch (tbl->action) {
15248c2ecf20Sopenharmony_ci		case IL_MIMO2_SWITCH_ANTENNA1:
15258c2ecf20Sopenharmony_ci		case IL_MIMO2_SWITCH_ANTENNA2:
15268c2ecf20Sopenharmony_ci			D_RATE("LQ: MIMO2 toggle Antennas\n");
15278c2ecf20Sopenharmony_ci
15288c2ecf20Sopenharmony_ci			if (tx_chains_num <= 2)
15298c2ecf20Sopenharmony_ci				break;
15308c2ecf20Sopenharmony_ci
15318c2ecf20Sopenharmony_ci			if (win->success_ratio >= IL_RS_GOOD_RATIO)
15328c2ecf20Sopenharmony_ci				break;
15338c2ecf20Sopenharmony_ci
15348c2ecf20Sopenharmony_ci			memcpy(search_tbl, tbl, sz);
15358c2ecf20Sopenharmony_ci			if (il4965_rs_toggle_antenna
15368c2ecf20Sopenharmony_ci			    (valid_tx_ant, &search_tbl->current_rate,
15378c2ecf20Sopenharmony_ci			     search_tbl)) {
15388c2ecf20Sopenharmony_ci				update_search_tbl_counter = 1;
15398c2ecf20Sopenharmony_ci				goto out;
15408c2ecf20Sopenharmony_ci			}
15418c2ecf20Sopenharmony_ci			break;
15428c2ecf20Sopenharmony_ci		case IL_MIMO2_SWITCH_SISO_A:
15438c2ecf20Sopenharmony_ci		case IL_MIMO2_SWITCH_SISO_B:
15448c2ecf20Sopenharmony_ci		case IL_MIMO2_SWITCH_SISO_C:
15458c2ecf20Sopenharmony_ci			D_RATE("LQ: MIMO2 switch to SISO\n");
15468c2ecf20Sopenharmony_ci
15478c2ecf20Sopenharmony_ci			/* Set up new search table for SISO */
15488c2ecf20Sopenharmony_ci			memcpy(search_tbl, tbl, sz);
15498c2ecf20Sopenharmony_ci
15508c2ecf20Sopenharmony_ci			if (tbl->action == IL_MIMO2_SWITCH_SISO_A)
15518c2ecf20Sopenharmony_ci				search_tbl->ant_type = ANT_A;
15528c2ecf20Sopenharmony_ci			else if (tbl->action == IL_MIMO2_SWITCH_SISO_B)
15538c2ecf20Sopenharmony_ci				search_tbl->ant_type = ANT_B;
15548c2ecf20Sopenharmony_ci			else
15558c2ecf20Sopenharmony_ci				search_tbl->ant_type = ANT_C;
15568c2ecf20Sopenharmony_ci
15578c2ecf20Sopenharmony_ci			if (!il4965_rs_is_valid_ant
15588c2ecf20Sopenharmony_ci			    (valid_tx_ant, search_tbl->ant_type))
15598c2ecf20Sopenharmony_ci				break;
15608c2ecf20Sopenharmony_ci
15618c2ecf20Sopenharmony_ci			ret =
15628c2ecf20Sopenharmony_ci			    il4965_rs_switch_to_siso(il, lq_sta, conf, sta,
15638c2ecf20Sopenharmony_ci						     search_tbl, idx);
15648c2ecf20Sopenharmony_ci			if (!ret)
15658c2ecf20Sopenharmony_ci				goto out;
15668c2ecf20Sopenharmony_ci
15678c2ecf20Sopenharmony_ci			break;
15688c2ecf20Sopenharmony_ci
15698c2ecf20Sopenharmony_ci		case IL_MIMO2_SWITCH_GI:
15708c2ecf20Sopenharmony_ci			if (!tbl->is_ht40 &&
15718c2ecf20Sopenharmony_ci			    !(ht_cap->cap & IEEE80211_HT_CAP_SGI_20))
15728c2ecf20Sopenharmony_ci				break;
15738c2ecf20Sopenharmony_ci			if (tbl->is_ht40 &&
15748c2ecf20Sopenharmony_ci			    !(ht_cap->cap & IEEE80211_HT_CAP_SGI_40))
15758c2ecf20Sopenharmony_ci				break;
15768c2ecf20Sopenharmony_ci
15778c2ecf20Sopenharmony_ci			D_RATE("LQ: MIMO2 toggle SGI/NGI\n");
15788c2ecf20Sopenharmony_ci
15798c2ecf20Sopenharmony_ci			/* Set up new search table for MIMO2 */
15808c2ecf20Sopenharmony_ci			memcpy(search_tbl, tbl, sz);
15818c2ecf20Sopenharmony_ci			search_tbl->is_SGI = !tbl->is_SGI;
15828c2ecf20Sopenharmony_ci			il4965_rs_set_expected_tpt_table(lq_sta, search_tbl);
15838c2ecf20Sopenharmony_ci			/*
15848c2ecf20Sopenharmony_ci			 * If active table already uses the fastest possible
15858c2ecf20Sopenharmony_ci			 * modulation (dual stream with short guard interval),
15868c2ecf20Sopenharmony_ci			 * and it's working well, there's no need to look
15878c2ecf20Sopenharmony_ci			 * for a better type of modulation!
15888c2ecf20Sopenharmony_ci			 */
15898c2ecf20Sopenharmony_ci			if (tbl->is_SGI) {
15908c2ecf20Sopenharmony_ci				s32 tpt = lq_sta->last_tpt / 100;
15918c2ecf20Sopenharmony_ci				if (tpt >= search_tbl->expected_tpt[idx])
15928c2ecf20Sopenharmony_ci					break;
15938c2ecf20Sopenharmony_ci			}
15948c2ecf20Sopenharmony_ci			search_tbl->current_rate =
15958c2ecf20Sopenharmony_ci			    il4965_rate_n_flags_from_tbl(il, search_tbl, idx,
15968c2ecf20Sopenharmony_ci							 is_green);
15978c2ecf20Sopenharmony_ci			update_search_tbl_counter = 1;
15988c2ecf20Sopenharmony_ci			goto out;
15998c2ecf20Sopenharmony_ci
16008c2ecf20Sopenharmony_ci		}
16018c2ecf20Sopenharmony_ci		tbl->action++;
16028c2ecf20Sopenharmony_ci		if (tbl->action > IL_MIMO2_SWITCH_GI)
16038c2ecf20Sopenharmony_ci			tbl->action = IL_MIMO2_SWITCH_ANTENNA1;
16048c2ecf20Sopenharmony_ci
16058c2ecf20Sopenharmony_ci		if (tbl->action == start_action)
16068c2ecf20Sopenharmony_ci			break;
16078c2ecf20Sopenharmony_ci	}
16088c2ecf20Sopenharmony_ci	search_tbl->lq_type = LQ_NONE;
16098c2ecf20Sopenharmony_ci	return 0;
16108c2ecf20Sopenharmony_ciout:
16118c2ecf20Sopenharmony_ci	lq_sta->search_better_tbl = 1;
16128c2ecf20Sopenharmony_ci	tbl->action++;
16138c2ecf20Sopenharmony_ci	if (tbl->action > IL_MIMO2_SWITCH_GI)
16148c2ecf20Sopenharmony_ci		tbl->action = IL_MIMO2_SWITCH_ANTENNA1;
16158c2ecf20Sopenharmony_ci	if (update_search_tbl_counter)
16168c2ecf20Sopenharmony_ci		search_tbl->action = tbl->action;
16178c2ecf20Sopenharmony_ci
16188c2ecf20Sopenharmony_ci	return 0;
16198c2ecf20Sopenharmony_ci
16208c2ecf20Sopenharmony_ci}
16218c2ecf20Sopenharmony_ci
16228c2ecf20Sopenharmony_ci/*
16238c2ecf20Sopenharmony_ci * Check whether we should continue using same modulation mode, or
16248c2ecf20Sopenharmony_ci * begin search for a new mode, based on:
16258c2ecf20Sopenharmony_ci * 1) # tx successes or failures while using this mode
16268c2ecf20Sopenharmony_ci * 2) # times calling this function
16278c2ecf20Sopenharmony_ci * 3) elapsed time in this mode (not used, for now)
16288c2ecf20Sopenharmony_ci */
16298c2ecf20Sopenharmony_cistatic void
16308c2ecf20Sopenharmony_ciil4965_rs_stay_in_table(struct il_lq_sta *lq_sta, bool force_search)
16318c2ecf20Sopenharmony_ci{
16328c2ecf20Sopenharmony_ci	struct il_scale_tbl_info *tbl;
16338c2ecf20Sopenharmony_ci	int i;
16348c2ecf20Sopenharmony_ci	int active_tbl;
16358c2ecf20Sopenharmony_ci	int flush_interval_passed = 0;
16368c2ecf20Sopenharmony_ci	struct il_priv *il;
16378c2ecf20Sopenharmony_ci
16388c2ecf20Sopenharmony_ci	il = lq_sta->drv;
16398c2ecf20Sopenharmony_ci	active_tbl = lq_sta->active_tbl;
16408c2ecf20Sopenharmony_ci
16418c2ecf20Sopenharmony_ci	tbl = &(lq_sta->lq_info[active_tbl]);
16428c2ecf20Sopenharmony_ci
16438c2ecf20Sopenharmony_ci	/* If we've been disallowing search, see if we should now allow it */
16448c2ecf20Sopenharmony_ci	if (lq_sta->stay_in_tbl) {
16458c2ecf20Sopenharmony_ci
16468c2ecf20Sopenharmony_ci		/* Elapsed time using current modulation mode */
16478c2ecf20Sopenharmony_ci		if (lq_sta->flush_timer)
16488c2ecf20Sopenharmony_ci			flush_interval_passed =
16498c2ecf20Sopenharmony_ci			    time_after(jiffies,
16508c2ecf20Sopenharmony_ci				       (unsigned long)(lq_sta->flush_timer +
16518c2ecf20Sopenharmony_ci						       RATE_SCALE_FLUSH_INTVL));
16528c2ecf20Sopenharmony_ci
16538c2ecf20Sopenharmony_ci		/*
16548c2ecf20Sopenharmony_ci		 * Check if we should allow search for new modulation mode.
16558c2ecf20Sopenharmony_ci		 * If many frames have failed or succeeded, or we've used
16568c2ecf20Sopenharmony_ci		 * this same modulation for a long time, allow search, and
16578c2ecf20Sopenharmony_ci		 * reset history stats that keep track of whether we should
16588c2ecf20Sopenharmony_ci		 * allow a new search.  Also (below) reset all bitmaps and
16598c2ecf20Sopenharmony_ci		 * stats in active history.
16608c2ecf20Sopenharmony_ci		 */
16618c2ecf20Sopenharmony_ci		if (force_search ||
16628c2ecf20Sopenharmony_ci		    lq_sta->total_failed > lq_sta->max_failure_limit ||
16638c2ecf20Sopenharmony_ci		    lq_sta->total_success > lq_sta->max_success_limit ||
16648c2ecf20Sopenharmony_ci		    (!lq_sta->search_better_tbl && lq_sta->flush_timer &&
16658c2ecf20Sopenharmony_ci		     flush_interval_passed)) {
16668c2ecf20Sopenharmony_ci			D_RATE("LQ: stay is expired %d %d %d\n",
16678c2ecf20Sopenharmony_ci			       lq_sta->total_failed, lq_sta->total_success,
16688c2ecf20Sopenharmony_ci			       flush_interval_passed);
16698c2ecf20Sopenharmony_ci
16708c2ecf20Sopenharmony_ci			/* Allow search for new mode */
16718c2ecf20Sopenharmony_ci			lq_sta->stay_in_tbl = 0;	/* only place reset */
16728c2ecf20Sopenharmony_ci			lq_sta->total_failed = 0;
16738c2ecf20Sopenharmony_ci			lq_sta->total_success = 0;
16748c2ecf20Sopenharmony_ci			lq_sta->flush_timer = 0;
16758c2ecf20Sopenharmony_ci
16768c2ecf20Sopenharmony_ci			/*
16778c2ecf20Sopenharmony_ci			 * Else if we've used this modulation mode enough repetitions
16788c2ecf20Sopenharmony_ci			 * (regardless of elapsed time or success/failure), reset
16798c2ecf20Sopenharmony_ci			 * history bitmaps and rate-specific stats for all rates in
16808c2ecf20Sopenharmony_ci			 * active table.
16818c2ecf20Sopenharmony_ci			 */
16828c2ecf20Sopenharmony_ci		} else {
16838c2ecf20Sopenharmony_ci			lq_sta->table_count++;
16848c2ecf20Sopenharmony_ci			if (lq_sta->table_count >= lq_sta->table_count_limit) {
16858c2ecf20Sopenharmony_ci				lq_sta->table_count = 0;
16868c2ecf20Sopenharmony_ci
16878c2ecf20Sopenharmony_ci				D_RATE("LQ: stay in table clear win\n");
16888c2ecf20Sopenharmony_ci				for (i = 0; i < RATE_COUNT; i++)
16898c2ecf20Sopenharmony_ci					il4965_rs_rate_scale_clear_win(&
16908c2ecf20Sopenharmony_ci								       (tbl->
16918c2ecf20Sopenharmony_ci									win
16928c2ecf20Sopenharmony_ci									[i]));
16938c2ecf20Sopenharmony_ci			}
16948c2ecf20Sopenharmony_ci		}
16958c2ecf20Sopenharmony_ci
16968c2ecf20Sopenharmony_ci		/* If transitioning to allow "search", reset all history
16978c2ecf20Sopenharmony_ci		 * bitmaps and stats in active table (this will become the new
16988c2ecf20Sopenharmony_ci		 * "search" table). */
16998c2ecf20Sopenharmony_ci		if (!lq_sta->stay_in_tbl) {
17008c2ecf20Sopenharmony_ci			for (i = 0; i < RATE_COUNT; i++)
17018c2ecf20Sopenharmony_ci				il4965_rs_rate_scale_clear_win(&(tbl->win[i]));
17028c2ecf20Sopenharmony_ci		}
17038c2ecf20Sopenharmony_ci	}
17048c2ecf20Sopenharmony_ci}
17058c2ecf20Sopenharmony_ci
17068c2ecf20Sopenharmony_ci/*
17078c2ecf20Sopenharmony_ci * setup rate table in uCode
17088c2ecf20Sopenharmony_ci */
17098c2ecf20Sopenharmony_cistatic void
17108c2ecf20Sopenharmony_ciil4965_rs_update_rate_tbl(struct il_priv *il, struct il_lq_sta *lq_sta,
17118c2ecf20Sopenharmony_ci			  struct il_scale_tbl_info *tbl, int idx, u8 is_green)
17128c2ecf20Sopenharmony_ci{
17138c2ecf20Sopenharmony_ci	u32 rate;
17148c2ecf20Sopenharmony_ci
17158c2ecf20Sopenharmony_ci	/* Update uCode's rate table. */
17168c2ecf20Sopenharmony_ci	rate = il4965_rate_n_flags_from_tbl(il, tbl, idx, is_green);
17178c2ecf20Sopenharmony_ci	il4965_rs_fill_link_cmd(il, lq_sta, rate);
17188c2ecf20Sopenharmony_ci	il_send_lq_cmd(il, &lq_sta->lq, CMD_ASYNC, false);
17198c2ecf20Sopenharmony_ci}
17208c2ecf20Sopenharmony_ci
17218c2ecf20Sopenharmony_ci/*
17228c2ecf20Sopenharmony_ci * Do rate scaling and search for new modulation mode.
17238c2ecf20Sopenharmony_ci */
17248c2ecf20Sopenharmony_cistatic void
17258c2ecf20Sopenharmony_ciil4965_rs_rate_scale_perform(struct il_priv *il, struct sk_buff *skb,
17268c2ecf20Sopenharmony_ci			     struct ieee80211_sta *sta,
17278c2ecf20Sopenharmony_ci			     struct il_lq_sta *lq_sta)
17288c2ecf20Sopenharmony_ci{
17298c2ecf20Sopenharmony_ci	struct ieee80211_hw *hw = il->hw;
17308c2ecf20Sopenharmony_ci	struct ieee80211_conf *conf = &hw->conf;
17318c2ecf20Sopenharmony_ci	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
17328c2ecf20Sopenharmony_ci	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
17338c2ecf20Sopenharmony_ci	int low = RATE_INVALID;
17348c2ecf20Sopenharmony_ci	int high = RATE_INVALID;
17358c2ecf20Sopenharmony_ci	int idx;
17368c2ecf20Sopenharmony_ci	int i;
17378c2ecf20Sopenharmony_ci	struct il_rate_scale_data *win = NULL;
17388c2ecf20Sopenharmony_ci	int current_tpt = IL_INVALID_VALUE;
17398c2ecf20Sopenharmony_ci	int low_tpt = IL_INVALID_VALUE;
17408c2ecf20Sopenharmony_ci	int high_tpt = IL_INVALID_VALUE;
17418c2ecf20Sopenharmony_ci	u32 fail_count;
17428c2ecf20Sopenharmony_ci	s8 scale_action = 0;
17438c2ecf20Sopenharmony_ci	u16 rate_mask;
17448c2ecf20Sopenharmony_ci	u8 update_lq = 0;
17458c2ecf20Sopenharmony_ci	struct il_scale_tbl_info *tbl, *tbl1;
17468c2ecf20Sopenharmony_ci	u16 rate_scale_idx_msk = 0;
17478c2ecf20Sopenharmony_ci	u8 is_green = 0;
17488c2ecf20Sopenharmony_ci	u8 active_tbl = 0;
17498c2ecf20Sopenharmony_ci	u8 done_search = 0;
17508c2ecf20Sopenharmony_ci	u16 high_low;
17518c2ecf20Sopenharmony_ci	s32 sr;
17528c2ecf20Sopenharmony_ci	u8 tid;
17538c2ecf20Sopenharmony_ci	struct il_tid_data *tid_data;
17548c2ecf20Sopenharmony_ci
17558c2ecf20Sopenharmony_ci	D_RATE("rate scale calculate new rate for skb\n");
17568c2ecf20Sopenharmony_ci
17578c2ecf20Sopenharmony_ci	/* Send management frames and NO_ACK data using lowest rate. */
17588c2ecf20Sopenharmony_ci	/* TODO: this could probably be improved.. */
17598c2ecf20Sopenharmony_ci	if (!ieee80211_is_data(hdr->frame_control) ||
17608c2ecf20Sopenharmony_ci	    (info->flags & IEEE80211_TX_CTL_NO_ACK))
17618c2ecf20Sopenharmony_ci		return;
17628c2ecf20Sopenharmony_ci
17638c2ecf20Sopenharmony_ci	lq_sta->supp_rates = sta->supp_rates[lq_sta->band];
17648c2ecf20Sopenharmony_ci
17658c2ecf20Sopenharmony_ci	tid = il4965_rs_tl_add_packet(lq_sta, hdr);
17668c2ecf20Sopenharmony_ci	if (tid != MAX_TID_COUNT && (lq_sta->tx_agg_tid_en & (1 << tid))) {
17678c2ecf20Sopenharmony_ci		tid_data = &il->stations[lq_sta->lq.sta_id].tid[tid];
17688c2ecf20Sopenharmony_ci		if (tid_data->agg.state == IL_AGG_OFF)
17698c2ecf20Sopenharmony_ci			lq_sta->is_agg = 0;
17708c2ecf20Sopenharmony_ci		else
17718c2ecf20Sopenharmony_ci			lq_sta->is_agg = 1;
17728c2ecf20Sopenharmony_ci	} else
17738c2ecf20Sopenharmony_ci		lq_sta->is_agg = 0;
17748c2ecf20Sopenharmony_ci
17758c2ecf20Sopenharmony_ci	/*
17768c2ecf20Sopenharmony_ci	 * Select rate-scale / modulation-mode table to work with in
17778c2ecf20Sopenharmony_ci	 * the rest of this function:  "search" if searching for better
17788c2ecf20Sopenharmony_ci	 * modulation mode, or "active" if doing rate scaling within a mode.
17798c2ecf20Sopenharmony_ci	 */
17808c2ecf20Sopenharmony_ci	if (!lq_sta->search_better_tbl)
17818c2ecf20Sopenharmony_ci		active_tbl = lq_sta->active_tbl;
17828c2ecf20Sopenharmony_ci	else
17838c2ecf20Sopenharmony_ci		active_tbl = 1 - lq_sta->active_tbl;
17848c2ecf20Sopenharmony_ci
17858c2ecf20Sopenharmony_ci	tbl = &(lq_sta->lq_info[active_tbl]);
17868c2ecf20Sopenharmony_ci	if (is_legacy(tbl->lq_type))
17878c2ecf20Sopenharmony_ci		lq_sta->is_green = 0;
17888c2ecf20Sopenharmony_ci	else
17898c2ecf20Sopenharmony_ci		lq_sta->is_green = il4965_rs_use_green(il, sta);
17908c2ecf20Sopenharmony_ci	is_green = lq_sta->is_green;
17918c2ecf20Sopenharmony_ci
17928c2ecf20Sopenharmony_ci	/* current tx rate */
17938c2ecf20Sopenharmony_ci	idx = lq_sta->last_txrate_idx;
17948c2ecf20Sopenharmony_ci
17958c2ecf20Sopenharmony_ci	D_RATE("Rate scale idx %d for type %d\n", idx, tbl->lq_type);
17968c2ecf20Sopenharmony_ci
17978c2ecf20Sopenharmony_ci	/* rates available for this association, and for modulation mode */
17988c2ecf20Sopenharmony_ci	rate_mask = il4965_rs_get_supported_rates(lq_sta, hdr, tbl->lq_type);
17998c2ecf20Sopenharmony_ci
18008c2ecf20Sopenharmony_ci	D_RATE("mask 0x%04X\n", rate_mask);
18018c2ecf20Sopenharmony_ci
18028c2ecf20Sopenharmony_ci	/* mask with station rate restriction */
18038c2ecf20Sopenharmony_ci	if (is_legacy(tbl->lq_type)) {
18048c2ecf20Sopenharmony_ci		if (lq_sta->band == NL80211_BAND_5GHZ)
18058c2ecf20Sopenharmony_ci			/* supp_rates has no CCK bits in A mode */
18068c2ecf20Sopenharmony_ci			rate_scale_idx_msk =
18078c2ecf20Sopenharmony_ci			    (u16) (rate_mask &
18088c2ecf20Sopenharmony_ci				   (lq_sta->supp_rates << IL_FIRST_OFDM_RATE));
18098c2ecf20Sopenharmony_ci		else
18108c2ecf20Sopenharmony_ci			rate_scale_idx_msk =
18118c2ecf20Sopenharmony_ci			    (u16) (rate_mask & lq_sta->supp_rates);
18128c2ecf20Sopenharmony_ci
18138c2ecf20Sopenharmony_ci	} else
18148c2ecf20Sopenharmony_ci		rate_scale_idx_msk = rate_mask;
18158c2ecf20Sopenharmony_ci
18168c2ecf20Sopenharmony_ci	if (!rate_scale_idx_msk)
18178c2ecf20Sopenharmony_ci		rate_scale_idx_msk = rate_mask;
18188c2ecf20Sopenharmony_ci
18198c2ecf20Sopenharmony_ci	if (!((1 << idx) & rate_scale_idx_msk)) {
18208c2ecf20Sopenharmony_ci		IL_ERR("Current Rate is not valid\n");
18218c2ecf20Sopenharmony_ci		if (lq_sta->search_better_tbl) {
18228c2ecf20Sopenharmony_ci			/* revert to active table if search table is not valid */
18238c2ecf20Sopenharmony_ci			tbl->lq_type = LQ_NONE;
18248c2ecf20Sopenharmony_ci			lq_sta->search_better_tbl = 0;
18258c2ecf20Sopenharmony_ci			tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
18268c2ecf20Sopenharmony_ci			/* get "active" rate info */
18278c2ecf20Sopenharmony_ci			idx = il4965_hwrate_to_plcp_idx(tbl->current_rate);
18288c2ecf20Sopenharmony_ci			il4965_rs_update_rate_tbl(il, lq_sta, tbl, idx,
18298c2ecf20Sopenharmony_ci						      is_green);
18308c2ecf20Sopenharmony_ci		}
18318c2ecf20Sopenharmony_ci		return;
18328c2ecf20Sopenharmony_ci	}
18338c2ecf20Sopenharmony_ci
18348c2ecf20Sopenharmony_ci	/* Get expected throughput table and history win for current rate */
18358c2ecf20Sopenharmony_ci	if (!tbl->expected_tpt) {
18368c2ecf20Sopenharmony_ci		IL_ERR("tbl->expected_tpt is NULL\n");
18378c2ecf20Sopenharmony_ci		return;
18388c2ecf20Sopenharmony_ci	}
18398c2ecf20Sopenharmony_ci
18408c2ecf20Sopenharmony_ci	/* force user max rate if set by user */
18418c2ecf20Sopenharmony_ci	if (lq_sta->max_rate_idx != -1 && lq_sta->max_rate_idx < idx) {
18428c2ecf20Sopenharmony_ci		idx = lq_sta->max_rate_idx;
18438c2ecf20Sopenharmony_ci		update_lq = 1;
18448c2ecf20Sopenharmony_ci		win = &(tbl->win[idx]);
18458c2ecf20Sopenharmony_ci		goto lq_update;
18468c2ecf20Sopenharmony_ci	}
18478c2ecf20Sopenharmony_ci
18488c2ecf20Sopenharmony_ci	win = &(tbl->win[idx]);
18498c2ecf20Sopenharmony_ci
18508c2ecf20Sopenharmony_ci	/*
18518c2ecf20Sopenharmony_ci	 * If there is not enough history to calculate actual average
18528c2ecf20Sopenharmony_ci	 * throughput, keep analyzing results of more tx frames, without
18538c2ecf20Sopenharmony_ci	 * changing rate or mode (bypass most of the rest of this function).
18548c2ecf20Sopenharmony_ci	 * Set up new rate table in uCode only if old rate is not supported
18558c2ecf20Sopenharmony_ci	 * in current association (use new rate found above).
18568c2ecf20Sopenharmony_ci	 */
18578c2ecf20Sopenharmony_ci	fail_count = win->counter - win->success_counter;
18588c2ecf20Sopenharmony_ci	if (fail_count < RATE_MIN_FAILURE_TH &&
18598c2ecf20Sopenharmony_ci	    win->success_counter < RATE_MIN_SUCCESS_TH) {
18608c2ecf20Sopenharmony_ci		D_RATE("LQ: still below TH. succ=%d total=%d " "for idx %d\n",
18618c2ecf20Sopenharmony_ci		       win->success_counter, win->counter, idx);
18628c2ecf20Sopenharmony_ci
18638c2ecf20Sopenharmony_ci		/* Can't calculate this yet; not enough history */
18648c2ecf20Sopenharmony_ci		win->average_tpt = IL_INVALID_VALUE;
18658c2ecf20Sopenharmony_ci
18668c2ecf20Sopenharmony_ci		/* Should we stay with this modulation mode,
18678c2ecf20Sopenharmony_ci		 * or search for a new one? */
18688c2ecf20Sopenharmony_ci		il4965_rs_stay_in_table(lq_sta, false);
18698c2ecf20Sopenharmony_ci
18708c2ecf20Sopenharmony_ci		goto out;
18718c2ecf20Sopenharmony_ci	}
18728c2ecf20Sopenharmony_ci	/* Else we have enough samples; calculate estimate of
18738c2ecf20Sopenharmony_ci	 * actual average throughput */
18748c2ecf20Sopenharmony_ci	if (win->average_tpt !=
18758c2ecf20Sopenharmony_ci	    ((win->success_ratio * tbl->expected_tpt[idx] + 64) / 128)) {
18768c2ecf20Sopenharmony_ci		IL_ERR("expected_tpt should have been calculated by now\n");
18778c2ecf20Sopenharmony_ci		win->average_tpt =
18788c2ecf20Sopenharmony_ci		    ((win->success_ratio * tbl->expected_tpt[idx] + 64) / 128);
18798c2ecf20Sopenharmony_ci	}
18808c2ecf20Sopenharmony_ci
18818c2ecf20Sopenharmony_ci	/* If we are searching for better modulation mode, check success. */
18828c2ecf20Sopenharmony_ci	if (lq_sta->search_better_tbl) {
18838c2ecf20Sopenharmony_ci		/* If good success, continue using the "search" mode;
18848c2ecf20Sopenharmony_ci		 * no need to send new link quality command, since we're
18858c2ecf20Sopenharmony_ci		 * continuing to use the setup that we've been trying. */
18868c2ecf20Sopenharmony_ci		if (win->average_tpt > lq_sta->last_tpt) {
18878c2ecf20Sopenharmony_ci
18888c2ecf20Sopenharmony_ci			D_RATE("LQ: SWITCHING TO NEW TBL "
18898c2ecf20Sopenharmony_ci			       "suc=%d cur-tpt=%d old-tpt=%d\n",
18908c2ecf20Sopenharmony_ci			       win->success_ratio, win->average_tpt,
18918c2ecf20Sopenharmony_ci			       lq_sta->last_tpt);
18928c2ecf20Sopenharmony_ci
18938c2ecf20Sopenharmony_ci			if (!is_legacy(tbl->lq_type))
18948c2ecf20Sopenharmony_ci				lq_sta->enable_counter = 1;
18958c2ecf20Sopenharmony_ci
18968c2ecf20Sopenharmony_ci			/* Swap tables; "search" becomes "active" */
18978c2ecf20Sopenharmony_ci			lq_sta->active_tbl = active_tbl;
18988c2ecf20Sopenharmony_ci			current_tpt = win->average_tpt;
18998c2ecf20Sopenharmony_ci
19008c2ecf20Sopenharmony_ci			/* Else poor success; go back to mode in "active" table */
19018c2ecf20Sopenharmony_ci		} else {
19028c2ecf20Sopenharmony_ci
19038c2ecf20Sopenharmony_ci			D_RATE("LQ: GOING BACK TO THE OLD TBL "
19048c2ecf20Sopenharmony_ci			       "suc=%d cur-tpt=%d old-tpt=%d\n",
19058c2ecf20Sopenharmony_ci			       win->success_ratio, win->average_tpt,
19068c2ecf20Sopenharmony_ci			       lq_sta->last_tpt);
19078c2ecf20Sopenharmony_ci
19088c2ecf20Sopenharmony_ci			/* Nullify "search" table */
19098c2ecf20Sopenharmony_ci			tbl->lq_type = LQ_NONE;
19108c2ecf20Sopenharmony_ci
19118c2ecf20Sopenharmony_ci			/* Revert to "active" table */
19128c2ecf20Sopenharmony_ci			active_tbl = lq_sta->active_tbl;
19138c2ecf20Sopenharmony_ci			tbl = &(lq_sta->lq_info[active_tbl]);
19148c2ecf20Sopenharmony_ci
19158c2ecf20Sopenharmony_ci			/* Revert to "active" rate and throughput info */
19168c2ecf20Sopenharmony_ci			idx = il4965_hwrate_to_plcp_idx(tbl->current_rate);
19178c2ecf20Sopenharmony_ci			current_tpt = lq_sta->last_tpt;
19188c2ecf20Sopenharmony_ci
19198c2ecf20Sopenharmony_ci			/* Need to set up a new rate table in uCode */
19208c2ecf20Sopenharmony_ci			update_lq = 1;
19218c2ecf20Sopenharmony_ci		}
19228c2ecf20Sopenharmony_ci
19238c2ecf20Sopenharmony_ci		/* Either way, we've made a decision; modulation mode
19248c2ecf20Sopenharmony_ci		 * search is done, allow rate adjustment next time. */
19258c2ecf20Sopenharmony_ci		lq_sta->search_better_tbl = 0;
19268c2ecf20Sopenharmony_ci		done_search = 1;	/* Don't switch modes below! */
19278c2ecf20Sopenharmony_ci		goto lq_update;
19288c2ecf20Sopenharmony_ci	}
19298c2ecf20Sopenharmony_ci
19308c2ecf20Sopenharmony_ci	/* (Else) not in search of better modulation mode, try for better
19318c2ecf20Sopenharmony_ci	 * starting rate, while staying in this mode. */
19328c2ecf20Sopenharmony_ci	high_low =
19338c2ecf20Sopenharmony_ci	    il4965_rs_get_adjacent_rate(il, idx, rate_scale_idx_msk,
19348c2ecf20Sopenharmony_ci					tbl->lq_type);
19358c2ecf20Sopenharmony_ci	low = high_low & 0xff;
19368c2ecf20Sopenharmony_ci	high = (high_low >> 8) & 0xff;
19378c2ecf20Sopenharmony_ci
19388c2ecf20Sopenharmony_ci	/* If user set max rate, dont allow higher than user constrain */
19398c2ecf20Sopenharmony_ci	if (lq_sta->max_rate_idx != -1 && lq_sta->max_rate_idx < high)
19408c2ecf20Sopenharmony_ci		high = RATE_INVALID;
19418c2ecf20Sopenharmony_ci
19428c2ecf20Sopenharmony_ci	sr = win->success_ratio;
19438c2ecf20Sopenharmony_ci
19448c2ecf20Sopenharmony_ci	/* Collect measured throughputs for current and adjacent rates */
19458c2ecf20Sopenharmony_ci	current_tpt = win->average_tpt;
19468c2ecf20Sopenharmony_ci	if (low != RATE_INVALID)
19478c2ecf20Sopenharmony_ci		low_tpt = tbl->win[low].average_tpt;
19488c2ecf20Sopenharmony_ci	if (high != RATE_INVALID)
19498c2ecf20Sopenharmony_ci		high_tpt = tbl->win[high].average_tpt;
19508c2ecf20Sopenharmony_ci
19518c2ecf20Sopenharmony_ci	scale_action = 0;
19528c2ecf20Sopenharmony_ci
19538c2ecf20Sopenharmony_ci	/* Too many failures, decrease rate */
19548c2ecf20Sopenharmony_ci	if (sr <= RATE_DECREASE_TH || current_tpt == 0) {
19558c2ecf20Sopenharmony_ci		D_RATE("decrease rate because of low success_ratio\n");
19568c2ecf20Sopenharmony_ci		scale_action = -1;
19578c2ecf20Sopenharmony_ci
19588c2ecf20Sopenharmony_ci		/* No throughput measured yet for adjacent rates; try increase. */
19598c2ecf20Sopenharmony_ci	} else if (low_tpt == IL_INVALID_VALUE && high_tpt == IL_INVALID_VALUE) {
19608c2ecf20Sopenharmony_ci
19618c2ecf20Sopenharmony_ci		if (high != RATE_INVALID && sr >= RATE_INCREASE_TH)
19628c2ecf20Sopenharmony_ci			scale_action = 1;
19638c2ecf20Sopenharmony_ci		else if (low != RATE_INVALID)
19648c2ecf20Sopenharmony_ci			scale_action = 0;
19658c2ecf20Sopenharmony_ci	}
19668c2ecf20Sopenharmony_ci
19678c2ecf20Sopenharmony_ci	/* Both adjacent throughputs are measured, but neither one has better
19688c2ecf20Sopenharmony_ci	 * throughput; we're using the best rate, don't change it! */
19698c2ecf20Sopenharmony_ci	else if (low_tpt != IL_INVALID_VALUE && high_tpt != IL_INVALID_VALUE &&
19708c2ecf20Sopenharmony_ci		 low_tpt < current_tpt && high_tpt < current_tpt)
19718c2ecf20Sopenharmony_ci		scale_action = 0;
19728c2ecf20Sopenharmony_ci
19738c2ecf20Sopenharmony_ci	/* At least one adjacent rate's throughput is measured,
19748c2ecf20Sopenharmony_ci	 * and may have better performance. */
19758c2ecf20Sopenharmony_ci	else {
19768c2ecf20Sopenharmony_ci		/* Higher adjacent rate's throughput is measured */
19778c2ecf20Sopenharmony_ci		if (high_tpt != IL_INVALID_VALUE) {
19788c2ecf20Sopenharmony_ci			/* Higher rate has better throughput */
19798c2ecf20Sopenharmony_ci			if (high_tpt > current_tpt && sr >= RATE_INCREASE_TH)
19808c2ecf20Sopenharmony_ci				scale_action = 1;
19818c2ecf20Sopenharmony_ci			else
19828c2ecf20Sopenharmony_ci				scale_action = 0;
19838c2ecf20Sopenharmony_ci
19848c2ecf20Sopenharmony_ci			/* Lower adjacent rate's throughput is measured */
19858c2ecf20Sopenharmony_ci		} else if (low_tpt != IL_INVALID_VALUE) {
19868c2ecf20Sopenharmony_ci			/* Lower rate has better throughput */
19878c2ecf20Sopenharmony_ci			if (low_tpt > current_tpt) {
19888c2ecf20Sopenharmony_ci				D_RATE("decrease rate because of low tpt\n");
19898c2ecf20Sopenharmony_ci				scale_action = -1;
19908c2ecf20Sopenharmony_ci			} else if (sr >= RATE_INCREASE_TH) {
19918c2ecf20Sopenharmony_ci				scale_action = 1;
19928c2ecf20Sopenharmony_ci			}
19938c2ecf20Sopenharmony_ci		}
19948c2ecf20Sopenharmony_ci	}
19958c2ecf20Sopenharmony_ci
19968c2ecf20Sopenharmony_ci	/* Sanity check; asked for decrease, but success rate or throughput
19978c2ecf20Sopenharmony_ci	 * has been good at old rate.  Don't change it. */
19988c2ecf20Sopenharmony_ci	if (scale_action == -1 && low != RATE_INVALID &&
19998c2ecf20Sopenharmony_ci	    (sr > RATE_HIGH_TH || current_tpt > 100 * tbl->expected_tpt[low]))
20008c2ecf20Sopenharmony_ci		scale_action = 0;
20018c2ecf20Sopenharmony_ci
20028c2ecf20Sopenharmony_ci	switch (scale_action) {
20038c2ecf20Sopenharmony_ci	case -1:
20048c2ecf20Sopenharmony_ci		/* Decrease starting rate, update uCode's rate table */
20058c2ecf20Sopenharmony_ci		if (low != RATE_INVALID) {
20068c2ecf20Sopenharmony_ci			update_lq = 1;
20078c2ecf20Sopenharmony_ci			idx = low;
20088c2ecf20Sopenharmony_ci		}
20098c2ecf20Sopenharmony_ci
20108c2ecf20Sopenharmony_ci		break;
20118c2ecf20Sopenharmony_ci	case 1:
20128c2ecf20Sopenharmony_ci		/* Increase starting rate, update uCode's rate table */
20138c2ecf20Sopenharmony_ci		if (high != RATE_INVALID) {
20148c2ecf20Sopenharmony_ci			update_lq = 1;
20158c2ecf20Sopenharmony_ci			idx = high;
20168c2ecf20Sopenharmony_ci		}
20178c2ecf20Sopenharmony_ci
20188c2ecf20Sopenharmony_ci		break;
20198c2ecf20Sopenharmony_ci	case 0:
20208c2ecf20Sopenharmony_ci		/* No change */
20218c2ecf20Sopenharmony_ci	default:
20228c2ecf20Sopenharmony_ci		break;
20238c2ecf20Sopenharmony_ci	}
20248c2ecf20Sopenharmony_ci
20258c2ecf20Sopenharmony_ci	D_RATE("choose rate scale idx %d action %d low %d " "high %d type %d\n",
20268c2ecf20Sopenharmony_ci	       idx, scale_action, low, high, tbl->lq_type);
20278c2ecf20Sopenharmony_ci
20288c2ecf20Sopenharmony_cilq_update:
20298c2ecf20Sopenharmony_ci	/* Replace uCode's rate table for the destination station. */
20308c2ecf20Sopenharmony_ci	if (update_lq)
20318c2ecf20Sopenharmony_ci		il4965_rs_update_rate_tbl(il, lq_sta, tbl, idx, is_green);
20328c2ecf20Sopenharmony_ci
20338c2ecf20Sopenharmony_ci	/* Should we stay with this modulation mode,
20348c2ecf20Sopenharmony_ci	 * or search for a new one? */
20358c2ecf20Sopenharmony_ci	il4965_rs_stay_in_table(lq_sta, false);
20368c2ecf20Sopenharmony_ci
20378c2ecf20Sopenharmony_ci	/*
20388c2ecf20Sopenharmony_ci	 * Search for new modulation mode if we're:
20398c2ecf20Sopenharmony_ci	 * 1)  Not changing rates right now
20408c2ecf20Sopenharmony_ci	 * 2)  Not just finishing up a search
20418c2ecf20Sopenharmony_ci	 * 3)  Allowing a new search
20428c2ecf20Sopenharmony_ci	 */
20438c2ecf20Sopenharmony_ci	if (!update_lq && !done_search && !lq_sta->stay_in_tbl && win->counter) {
20448c2ecf20Sopenharmony_ci		/* Save current throughput to compare with "search" throughput */
20458c2ecf20Sopenharmony_ci		lq_sta->last_tpt = current_tpt;
20468c2ecf20Sopenharmony_ci
20478c2ecf20Sopenharmony_ci		/* Select a new "search" modulation mode to try.
20488c2ecf20Sopenharmony_ci		 * If one is found, set up the new "search" table. */
20498c2ecf20Sopenharmony_ci		if (is_legacy(tbl->lq_type))
20508c2ecf20Sopenharmony_ci			il4965_rs_move_legacy_other(il, lq_sta, conf, sta, idx);
20518c2ecf20Sopenharmony_ci		else if (is_siso(tbl->lq_type))
20528c2ecf20Sopenharmony_ci			il4965_rs_move_siso_to_other(il, lq_sta, conf, sta,
20538c2ecf20Sopenharmony_ci						     idx);
20548c2ecf20Sopenharmony_ci		else		/* (is_mimo2(tbl->lq_type)) */
20558c2ecf20Sopenharmony_ci			il4965_rs_move_mimo2_to_other(il, lq_sta, conf, sta,
20568c2ecf20Sopenharmony_ci						      idx);
20578c2ecf20Sopenharmony_ci
20588c2ecf20Sopenharmony_ci		/* If new "search" mode was selected, set up in uCode table */
20598c2ecf20Sopenharmony_ci		if (lq_sta->search_better_tbl) {
20608c2ecf20Sopenharmony_ci			/* Access the "search" table, clear its history. */
20618c2ecf20Sopenharmony_ci			tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
20628c2ecf20Sopenharmony_ci			for (i = 0; i < RATE_COUNT; i++)
20638c2ecf20Sopenharmony_ci				il4965_rs_rate_scale_clear_win(&(tbl->win[i]));
20648c2ecf20Sopenharmony_ci
20658c2ecf20Sopenharmony_ci			/* Use new "search" start rate */
20668c2ecf20Sopenharmony_ci			idx = il4965_hwrate_to_plcp_idx(tbl->current_rate);
20678c2ecf20Sopenharmony_ci
20688c2ecf20Sopenharmony_ci			D_RATE("Switch current  mcs: %X idx: %d\n",
20698c2ecf20Sopenharmony_ci			       tbl->current_rate, idx);
20708c2ecf20Sopenharmony_ci			il4965_rs_fill_link_cmd(il, lq_sta, tbl->current_rate);
20718c2ecf20Sopenharmony_ci			il_send_lq_cmd(il, &lq_sta->lq, CMD_ASYNC, false);
20728c2ecf20Sopenharmony_ci		} else
20738c2ecf20Sopenharmony_ci			done_search = 1;
20748c2ecf20Sopenharmony_ci	}
20758c2ecf20Sopenharmony_ci
20768c2ecf20Sopenharmony_ci	if (done_search && !lq_sta->stay_in_tbl) {
20778c2ecf20Sopenharmony_ci		/* If the "active" (non-search) mode was legacy,
20788c2ecf20Sopenharmony_ci		 * and we've tried switching antennas,
20798c2ecf20Sopenharmony_ci		 * but we haven't been able to try HT modes (not available),
20808c2ecf20Sopenharmony_ci		 * stay with best antenna legacy modulation for a while
20818c2ecf20Sopenharmony_ci		 * before next round of mode comparisons. */
20828c2ecf20Sopenharmony_ci		tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
20838c2ecf20Sopenharmony_ci		if (is_legacy(tbl1->lq_type) && !conf_is_ht(conf) &&
20848c2ecf20Sopenharmony_ci		    lq_sta->action_counter > tbl1->max_search) {
20858c2ecf20Sopenharmony_ci			D_RATE("LQ: STAY in legacy table\n");
20868c2ecf20Sopenharmony_ci			il4965_rs_set_stay_in_table(il, 1, lq_sta);
20878c2ecf20Sopenharmony_ci		}
20888c2ecf20Sopenharmony_ci
20898c2ecf20Sopenharmony_ci		/* If we're in an HT mode, and all 3 mode switch actions
20908c2ecf20Sopenharmony_ci		 * have been tried and compared, stay in this best modulation
20918c2ecf20Sopenharmony_ci		 * mode for a while before next round of mode comparisons. */
20928c2ecf20Sopenharmony_ci		if (lq_sta->enable_counter &&
20938c2ecf20Sopenharmony_ci		    lq_sta->action_counter >= tbl1->max_search) {
20948c2ecf20Sopenharmony_ci			if (lq_sta->last_tpt > IL_AGG_TPT_THREHOLD &&
20958c2ecf20Sopenharmony_ci			    (lq_sta->tx_agg_tid_en & (1 << tid)) &&
20968c2ecf20Sopenharmony_ci			    tid != MAX_TID_COUNT) {
20978c2ecf20Sopenharmony_ci				tid_data =
20988c2ecf20Sopenharmony_ci				    &il->stations[lq_sta->lq.sta_id].tid[tid];
20998c2ecf20Sopenharmony_ci				if (tid_data->agg.state == IL_AGG_OFF) {
21008c2ecf20Sopenharmony_ci					D_RATE("try to aggregate tid %d\n",
21018c2ecf20Sopenharmony_ci					       tid);
21028c2ecf20Sopenharmony_ci					il4965_rs_tl_turn_on_agg(il, tid,
21038c2ecf20Sopenharmony_ci								 lq_sta, sta);
21048c2ecf20Sopenharmony_ci				}
21058c2ecf20Sopenharmony_ci			}
21068c2ecf20Sopenharmony_ci			il4965_rs_set_stay_in_table(il, 0, lq_sta);
21078c2ecf20Sopenharmony_ci		}
21088c2ecf20Sopenharmony_ci	}
21098c2ecf20Sopenharmony_ci
21108c2ecf20Sopenharmony_ciout:
21118c2ecf20Sopenharmony_ci	tbl->current_rate =
21128c2ecf20Sopenharmony_ci	    il4965_rate_n_flags_from_tbl(il, tbl, idx, is_green);
21138c2ecf20Sopenharmony_ci	i = idx;
21148c2ecf20Sopenharmony_ci	lq_sta->last_txrate_idx = i;
21158c2ecf20Sopenharmony_ci}
21168c2ecf20Sopenharmony_ci
21178c2ecf20Sopenharmony_ci/*
21188c2ecf20Sopenharmony_ci * il4965_rs_initialize_lq - Initialize a station's hardware rate table
21198c2ecf20Sopenharmony_ci *
21208c2ecf20Sopenharmony_ci * The uCode's station table contains a table of fallback rates
21218c2ecf20Sopenharmony_ci * for automatic fallback during transmission.
21228c2ecf20Sopenharmony_ci *
21238c2ecf20Sopenharmony_ci * NOTE: This sets up a default set of values.  These will be replaced later
21248c2ecf20Sopenharmony_ci *       if the driver's iwl-4965-rs rate scaling algorithm is used, instead of
21258c2ecf20Sopenharmony_ci *       rc80211_simple.
21268c2ecf20Sopenharmony_ci *
21278c2ecf20Sopenharmony_ci * NOTE: Run C_ADD_STA command to set up station table entry, before
21288c2ecf20Sopenharmony_ci *       calling this function (which runs C_TX_LINK_QUALITY_CMD,
21298c2ecf20Sopenharmony_ci *       which requires station table entry to exist).
21308c2ecf20Sopenharmony_ci */
21318c2ecf20Sopenharmony_cistatic void
21328c2ecf20Sopenharmony_ciil4965_rs_initialize_lq(struct il_priv *il, struct ieee80211_conf *conf,
21338c2ecf20Sopenharmony_ci			struct ieee80211_sta *sta, struct il_lq_sta *lq_sta)
21348c2ecf20Sopenharmony_ci{
21358c2ecf20Sopenharmony_ci	struct il_scale_tbl_info *tbl;
21368c2ecf20Sopenharmony_ci	int rate_idx;
21378c2ecf20Sopenharmony_ci	int i;
21388c2ecf20Sopenharmony_ci	u32 rate;
21398c2ecf20Sopenharmony_ci	u8 use_green;
21408c2ecf20Sopenharmony_ci	u8 active_tbl = 0;
21418c2ecf20Sopenharmony_ci	u8 valid_tx_ant;
21428c2ecf20Sopenharmony_ci
21438c2ecf20Sopenharmony_ci	if (!sta || !lq_sta)
21448c2ecf20Sopenharmony_ci		return;
21458c2ecf20Sopenharmony_ci
21468c2ecf20Sopenharmony_ci	use_green = il4965_rs_use_green(il, sta);
21478c2ecf20Sopenharmony_ci
21488c2ecf20Sopenharmony_ci	i = lq_sta->last_txrate_idx;
21498c2ecf20Sopenharmony_ci
21508c2ecf20Sopenharmony_ci	valid_tx_ant = il->hw_params.valid_tx_ant;
21518c2ecf20Sopenharmony_ci
21528c2ecf20Sopenharmony_ci	if (!lq_sta->search_better_tbl)
21538c2ecf20Sopenharmony_ci		active_tbl = lq_sta->active_tbl;
21548c2ecf20Sopenharmony_ci	else
21558c2ecf20Sopenharmony_ci		active_tbl = 1 - lq_sta->active_tbl;
21568c2ecf20Sopenharmony_ci
21578c2ecf20Sopenharmony_ci	tbl = &(lq_sta->lq_info[active_tbl]);
21588c2ecf20Sopenharmony_ci
21598c2ecf20Sopenharmony_ci	if (i < 0 || i >= RATE_COUNT)
21608c2ecf20Sopenharmony_ci		i = 0;
21618c2ecf20Sopenharmony_ci
21628c2ecf20Sopenharmony_ci	rate = il_rates[i].plcp;
21638c2ecf20Sopenharmony_ci	tbl->ant_type = il4965_first_antenna(valid_tx_ant);
21648c2ecf20Sopenharmony_ci	rate |= tbl->ant_type << RATE_MCS_ANT_POS;
21658c2ecf20Sopenharmony_ci
21668c2ecf20Sopenharmony_ci	if (i >= IL_FIRST_CCK_RATE && i <= IL_LAST_CCK_RATE)
21678c2ecf20Sopenharmony_ci		rate |= RATE_MCS_CCK_MSK;
21688c2ecf20Sopenharmony_ci
21698c2ecf20Sopenharmony_ci	il4965_rs_get_tbl_info_from_mcs(rate, il->band, tbl, &rate_idx);
21708c2ecf20Sopenharmony_ci	if (!il4965_rs_is_valid_ant(valid_tx_ant, tbl->ant_type))
21718c2ecf20Sopenharmony_ci		il4965_rs_toggle_antenna(valid_tx_ant, &rate, tbl);
21728c2ecf20Sopenharmony_ci
21738c2ecf20Sopenharmony_ci	rate = il4965_rate_n_flags_from_tbl(il, tbl, rate_idx, use_green);
21748c2ecf20Sopenharmony_ci	tbl->current_rate = rate;
21758c2ecf20Sopenharmony_ci	il4965_rs_set_expected_tpt_table(lq_sta, tbl);
21768c2ecf20Sopenharmony_ci	il4965_rs_fill_link_cmd(NULL, lq_sta, rate);
21778c2ecf20Sopenharmony_ci	il->stations[lq_sta->lq.sta_id].lq = &lq_sta->lq;
21788c2ecf20Sopenharmony_ci	il_send_lq_cmd(il, &lq_sta->lq, CMD_SYNC, true);
21798c2ecf20Sopenharmony_ci}
21808c2ecf20Sopenharmony_ci
21818c2ecf20Sopenharmony_cistatic void
21828c2ecf20Sopenharmony_ciil4965_rs_get_rate(void *il_r, struct ieee80211_sta *sta, void *il_sta,
21838c2ecf20Sopenharmony_ci		   struct ieee80211_tx_rate_control *txrc)
21848c2ecf20Sopenharmony_ci{
21858c2ecf20Sopenharmony_ci
21868c2ecf20Sopenharmony_ci	struct sk_buff *skb = txrc->skb;
21878c2ecf20Sopenharmony_ci	struct ieee80211_supported_band *sband = txrc->sband;
21888c2ecf20Sopenharmony_ci	struct il_priv *il __maybe_unused = (struct il_priv *)il_r;
21898c2ecf20Sopenharmony_ci	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
21908c2ecf20Sopenharmony_ci	struct il_lq_sta *lq_sta = il_sta;
21918c2ecf20Sopenharmony_ci	int rate_idx;
21928c2ecf20Sopenharmony_ci
21938c2ecf20Sopenharmony_ci	D_RATE("rate scale calculate new rate for skb\n");
21948c2ecf20Sopenharmony_ci
21958c2ecf20Sopenharmony_ci	/* Get max rate if user set max rate */
21968c2ecf20Sopenharmony_ci	if (lq_sta) {
21978c2ecf20Sopenharmony_ci		lq_sta->max_rate_idx = fls(txrc->rate_idx_mask) - 1;
21988c2ecf20Sopenharmony_ci		if (sband->band == NL80211_BAND_5GHZ &&
21998c2ecf20Sopenharmony_ci		    lq_sta->max_rate_idx != -1)
22008c2ecf20Sopenharmony_ci			lq_sta->max_rate_idx += IL_FIRST_OFDM_RATE;
22018c2ecf20Sopenharmony_ci		if (lq_sta->max_rate_idx < 0 ||
22028c2ecf20Sopenharmony_ci		    lq_sta->max_rate_idx >= RATE_COUNT)
22038c2ecf20Sopenharmony_ci			lq_sta->max_rate_idx = -1;
22048c2ecf20Sopenharmony_ci	}
22058c2ecf20Sopenharmony_ci
22068c2ecf20Sopenharmony_ci	/* Treat uninitialized rate scaling data same as non-existing. */
22078c2ecf20Sopenharmony_ci	if (lq_sta && !lq_sta->drv) {
22088c2ecf20Sopenharmony_ci		D_RATE("Rate scaling not initialized yet.\n");
22098c2ecf20Sopenharmony_ci		il_sta = NULL;
22108c2ecf20Sopenharmony_ci	}
22118c2ecf20Sopenharmony_ci
22128c2ecf20Sopenharmony_ci	if (!lq_sta)
22138c2ecf20Sopenharmony_ci		return;
22148c2ecf20Sopenharmony_ci
22158c2ecf20Sopenharmony_ci	rate_idx = lq_sta->last_txrate_idx;
22168c2ecf20Sopenharmony_ci
22178c2ecf20Sopenharmony_ci	if (lq_sta->last_rate_n_flags & RATE_MCS_HT_MSK) {
22188c2ecf20Sopenharmony_ci		rate_idx -= IL_FIRST_OFDM_RATE;
22198c2ecf20Sopenharmony_ci		/* 6M and 9M shared same MCS idx */
22208c2ecf20Sopenharmony_ci		rate_idx = (rate_idx > 0) ? (rate_idx - 1) : 0;
22218c2ecf20Sopenharmony_ci		if (il4965_rs_extract_rate(lq_sta->last_rate_n_flags) >=
22228c2ecf20Sopenharmony_ci		    RATE_MIMO2_6M_PLCP)
22238c2ecf20Sopenharmony_ci			rate_idx = rate_idx + MCS_IDX_PER_STREAM;
22248c2ecf20Sopenharmony_ci		info->control.rates[0].flags = IEEE80211_TX_RC_MCS;
22258c2ecf20Sopenharmony_ci		if (lq_sta->last_rate_n_flags & RATE_MCS_SGI_MSK)
22268c2ecf20Sopenharmony_ci			info->control.rates[0].flags |=
22278c2ecf20Sopenharmony_ci			    IEEE80211_TX_RC_SHORT_GI;
22288c2ecf20Sopenharmony_ci		if (lq_sta->last_rate_n_flags & RATE_MCS_DUP_MSK)
22298c2ecf20Sopenharmony_ci			info->control.rates[0].flags |=
22308c2ecf20Sopenharmony_ci			    IEEE80211_TX_RC_DUP_DATA;
22318c2ecf20Sopenharmony_ci		if (lq_sta->last_rate_n_flags & RATE_MCS_HT40_MSK)
22328c2ecf20Sopenharmony_ci			info->control.rates[0].flags |=
22338c2ecf20Sopenharmony_ci			    IEEE80211_TX_RC_40_MHZ_WIDTH;
22348c2ecf20Sopenharmony_ci		if (lq_sta->last_rate_n_flags & RATE_MCS_GF_MSK)
22358c2ecf20Sopenharmony_ci			info->control.rates[0].flags |=
22368c2ecf20Sopenharmony_ci			    IEEE80211_TX_RC_GREEN_FIELD;
22378c2ecf20Sopenharmony_ci	} else {
22388c2ecf20Sopenharmony_ci		/* Check for invalid rates */
22398c2ecf20Sopenharmony_ci		if (rate_idx < 0 || rate_idx >= RATE_COUNT_LEGACY ||
22408c2ecf20Sopenharmony_ci		    (sband->band == NL80211_BAND_5GHZ &&
22418c2ecf20Sopenharmony_ci		     rate_idx < IL_FIRST_OFDM_RATE))
22428c2ecf20Sopenharmony_ci			rate_idx = rate_lowest_index(sband, sta);
22438c2ecf20Sopenharmony_ci		/* On valid 5 GHz rate, adjust idx */
22448c2ecf20Sopenharmony_ci		else if (sband->band == NL80211_BAND_5GHZ)
22458c2ecf20Sopenharmony_ci			rate_idx -= IL_FIRST_OFDM_RATE;
22468c2ecf20Sopenharmony_ci		info->control.rates[0].flags = 0;
22478c2ecf20Sopenharmony_ci	}
22488c2ecf20Sopenharmony_ci	info->control.rates[0].idx = rate_idx;
22498c2ecf20Sopenharmony_ci	info->control.rates[0].count = 1;
22508c2ecf20Sopenharmony_ci}
22518c2ecf20Sopenharmony_ci
22528c2ecf20Sopenharmony_cistatic void *
22538c2ecf20Sopenharmony_ciil4965_rs_alloc_sta(void *il_rate, struct ieee80211_sta *sta, gfp_t gfp)
22548c2ecf20Sopenharmony_ci{
22558c2ecf20Sopenharmony_ci	struct il_station_priv *sta_priv =
22568c2ecf20Sopenharmony_ci	    (struct il_station_priv *)sta->drv_priv;
22578c2ecf20Sopenharmony_ci	struct il_priv *il;
22588c2ecf20Sopenharmony_ci
22598c2ecf20Sopenharmony_ci	il = (struct il_priv *)il_rate;
22608c2ecf20Sopenharmony_ci	D_RATE("create station rate scale win\n");
22618c2ecf20Sopenharmony_ci
22628c2ecf20Sopenharmony_ci	return &sta_priv->lq_sta;
22638c2ecf20Sopenharmony_ci}
22648c2ecf20Sopenharmony_ci
22658c2ecf20Sopenharmony_ci/*
22668c2ecf20Sopenharmony_ci * Called after adding a new station to initialize rate scaling
22678c2ecf20Sopenharmony_ci */
22688c2ecf20Sopenharmony_civoid
22698c2ecf20Sopenharmony_ciil4965_rs_rate_init(struct il_priv *il, struct ieee80211_sta *sta, u8 sta_id)
22708c2ecf20Sopenharmony_ci{
22718c2ecf20Sopenharmony_ci	int i, j;
22728c2ecf20Sopenharmony_ci	struct ieee80211_hw *hw = il->hw;
22738c2ecf20Sopenharmony_ci	struct ieee80211_conf *conf = &il->hw->conf;
22748c2ecf20Sopenharmony_ci	struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
22758c2ecf20Sopenharmony_ci	struct il_station_priv *sta_priv;
22768c2ecf20Sopenharmony_ci	struct il_lq_sta *lq_sta;
22778c2ecf20Sopenharmony_ci	struct ieee80211_supported_band *sband;
22788c2ecf20Sopenharmony_ci
22798c2ecf20Sopenharmony_ci	sta_priv = (struct il_station_priv *)sta->drv_priv;
22808c2ecf20Sopenharmony_ci	lq_sta = &sta_priv->lq_sta;
22818c2ecf20Sopenharmony_ci	sband = hw->wiphy->bands[conf->chandef.chan->band];
22828c2ecf20Sopenharmony_ci
22838c2ecf20Sopenharmony_ci	lq_sta->lq.sta_id = sta_id;
22848c2ecf20Sopenharmony_ci
22858c2ecf20Sopenharmony_ci	for (j = 0; j < LQ_SIZE; j++)
22868c2ecf20Sopenharmony_ci		for (i = 0; i < RATE_COUNT; i++)
22878c2ecf20Sopenharmony_ci			il4965_rs_rate_scale_clear_win(&lq_sta->lq_info[j].
22888c2ecf20Sopenharmony_ci						       win[i]);
22898c2ecf20Sopenharmony_ci
22908c2ecf20Sopenharmony_ci	lq_sta->flush_timer = 0;
22918c2ecf20Sopenharmony_ci	lq_sta->supp_rates = sta->supp_rates[sband->band];
22928c2ecf20Sopenharmony_ci	for (j = 0; j < LQ_SIZE; j++)
22938c2ecf20Sopenharmony_ci		for (i = 0; i < RATE_COUNT; i++)
22948c2ecf20Sopenharmony_ci			il4965_rs_rate_scale_clear_win(&lq_sta->lq_info[j].
22958c2ecf20Sopenharmony_ci						       win[i]);
22968c2ecf20Sopenharmony_ci
22978c2ecf20Sopenharmony_ci	D_RATE("LQ:" "*** rate scale station global init for station %d ***\n",
22988c2ecf20Sopenharmony_ci	       sta_id);
22998c2ecf20Sopenharmony_ci	/* TODO: what is a good starting rate for STA? About middle? Maybe not
23008c2ecf20Sopenharmony_ci	 * the lowest or the highest rate.. Could consider using RSSI from
23018c2ecf20Sopenharmony_ci	 * previous packets? Need to have IEEE 802.1X auth succeed immediately
23028c2ecf20Sopenharmony_ci	 * after assoc.. */
23038c2ecf20Sopenharmony_ci
23048c2ecf20Sopenharmony_ci	lq_sta->is_dup = 0;
23058c2ecf20Sopenharmony_ci	lq_sta->max_rate_idx = -1;
23068c2ecf20Sopenharmony_ci	lq_sta->missed_rate_counter = IL_MISSED_RATE_MAX;
23078c2ecf20Sopenharmony_ci	lq_sta->is_green = il4965_rs_use_green(il, sta);
23088c2ecf20Sopenharmony_ci	lq_sta->active_legacy_rate = il->active_rate & ~(0x1000);
23098c2ecf20Sopenharmony_ci	lq_sta->band = il->band;
23108c2ecf20Sopenharmony_ci	/*
23118c2ecf20Sopenharmony_ci	 * active_siso_rate mask includes 9 MBits (bit 5), and CCK (bits 0-3),
23128c2ecf20Sopenharmony_ci	 * supp_rates[] does not; shift to convert format, force 9 MBits off.
23138c2ecf20Sopenharmony_ci	 */
23148c2ecf20Sopenharmony_ci	lq_sta->active_siso_rate = ht_cap->mcs.rx_mask[0] << 1;
23158c2ecf20Sopenharmony_ci	lq_sta->active_siso_rate |= ht_cap->mcs.rx_mask[0] & 0x1;
23168c2ecf20Sopenharmony_ci	lq_sta->active_siso_rate &= ~((u16) 0x2);
23178c2ecf20Sopenharmony_ci	lq_sta->active_siso_rate <<= IL_FIRST_OFDM_RATE;
23188c2ecf20Sopenharmony_ci
23198c2ecf20Sopenharmony_ci	/* Same here */
23208c2ecf20Sopenharmony_ci	lq_sta->active_mimo2_rate = ht_cap->mcs.rx_mask[1] << 1;
23218c2ecf20Sopenharmony_ci	lq_sta->active_mimo2_rate |= ht_cap->mcs.rx_mask[1] & 0x1;
23228c2ecf20Sopenharmony_ci	lq_sta->active_mimo2_rate &= ~((u16) 0x2);
23238c2ecf20Sopenharmony_ci	lq_sta->active_mimo2_rate <<= IL_FIRST_OFDM_RATE;
23248c2ecf20Sopenharmony_ci
23258c2ecf20Sopenharmony_ci	/* These values will be overridden later */
23268c2ecf20Sopenharmony_ci	lq_sta->lq.general_params.single_stream_ant_msk =
23278c2ecf20Sopenharmony_ci	    il4965_first_antenna(il->hw_params.valid_tx_ant);
23288c2ecf20Sopenharmony_ci	lq_sta->lq.general_params.dual_stream_ant_msk =
23298c2ecf20Sopenharmony_ci	    il->hw_params.valid_tx_ant & ~il4965_first_antenna(il->hw_params.
23308c2ecf20Sopenharmony_ci							       valid_tx_ant);
23318c2ecf20Sopenharmony_ci	if (!lq_sta->lq.general_params.dual_stream_ant_msk) {
23328c2ecf20Sopenharmony_ci		lq_sta->lq.general_params.dual_stream_ant_msk = ANT_AB;
23338c2ecf20Sopenharmony_ci	} else if (il4965_num_of_ant(il->hw_params.valid_tx_ant) == 2) {
23348c2ecf20Sopenharmony_ci		lq_sta->lq.general_params.dual_stream_ant_msk =
23358c2ecf20Sopenharmony_ci		    il->hw_params.valid_tx_ant;
23368c2ecf20Sopenharmony_ci	}
23378c2ecf20Sopenharmony_ci
23388c2ecf20Sopenharmony_ci	/* as default allow aggregation for all tids */
23398c2ecf20Sopenharmony_ci	lq_sta->tx_agg_tid_en = IL_AGG_ALL_TID;
23408c2ecf20Sopenharmony_ci	lq_sta->drv = il;
23418c2ecf20Sopenharmony_ci
23428c2ecf20Sopenharmony_ci	/* Set last_txrate_idx to lowest rate */
23438c2ecf20Sopenharmony_ci	lq_sta->last_txrate_idx = rate_lowest_index(sband, sta);
23448c2ecf20Sopenharmony_ci	if (sband->band == NL80211_BAND_5GHZ)
23458c2ecf20Sopenharmony_ci		lq_sta->last_txrate_idx += IL_FIRST_OFDM_RATE;
23468c2ecf20Sopenharmony_ci	lq_sta->is_agg = 0;
23478c2ecf20Sopenharmony_ci
23488c2ecf20Sopenharmony_ci#ifdef CONFIG_MAC80211_DEBUGFS
23498c2ecf20Sopenharmony_ci	lq_sta->dbg_fixed_rate = 0;
23508c2ecf20Sopenharmony_ci#endif
23518c2ecf20Sopenharmony_ci
23528c2ecf20Sopenharmony_ci	il4965_rs_initialize_lq(il, conf, sta, lq_sta);
23538c2ecf20Sopenharmony_ci}
23548c2ecf20Sopenharmony_ci
23558c2ecf20Sopenharmony_cistatic void
23568c2ecf20Sopenharmony_ciil4965_rs_fill_link_cmd(struct il_priv *il, struct il_lq_sta *lq_sta,
23578c2ecf20Sopenharmony_ci			u32 new_rate)
23588c2ecf20Sopenharmony_ci{
23598c2ecf20Sopenharmony_ci	struct il_scale_tbl_info tbl_type;
23608c2ecf20Sopenharmony_ci	int idx = 0;
23618c2ecf20Sopenharmony_ci	int rate_idx;
23628c2ecf20Sopenharmony_ci	int repeat_rate = 0;
23638c2ecf20Sopenharmony_ci	u8 ant_toggle_cnt = 0;
23648c2ecf20Sopenharmony_ci	u8 use_ht_possible = 1;
23658c2ecf20Sopenharmony_ci	u8 valid_tx_ant = 0;
23668c2ecf20Sopenharmony_ci	struct il_link_quality_cmd *lq_cmd = &lq_sta->lq;
23678c2ecf20Sopenharmony_ci
23688c2ecf20Sopenharmony_ci	/* Override starting rate (idx 0) if needed for debug purposes */
23698c2ecf20Sopenharmony_ci	il4965_rs_dbgfs_set_mcs(lq_sta, &new_rate, idx);
23708c2ecf20Sopenharmony_ci
23718c2ecf20Sopenharmony_ci	/* Interpret new_rate (rate_n_flags) */
23728c2ecf20Sopenharmony_ci	il4965_rs_get_tbl_info_from_mcs(new_rate, lq_sta->band, &tbl_type,
23738c2ecf20Sopenharmony_ci					&rate_idx);
23748c2ecf20Sopenharmony_ci
23758c2ecf20Sopenharmony_ci	/* How many times should we repeat the initial rate? */
23768c2ecf20Sopenharmony_ci	if (is_legacy(tbl_type.lq_type)) {
23778c2ecf20Sopenharmony_ci		ant_toggle_cnt = 1;
23788c2ecf20Sopenharmony_ci		repeat_rate = IL_NUMBER_TRY;
23798c2ecf20Sopenharmony_ci	} else {
23808c2ecf20Sopenharmony_ci		repeat_rate = IL_HT_NUMBER_TRY;
23818c2ecf20Sopenharmony_ci	}
23828c2ecf20Sopenharmony_ci
23838c2ecf20Sopenharmony_ci	lq_cmd->general_params.mimo_delimiter =
23848c2ecf20Sopenharmony_ci	    is_mimo(tbl_type.lq_type) ? 1 : 0;
23858c2ecf20Sopenharmony_ci
23868c2ecf20Sopenharmony_ci	/* Fill 1st table entry (idx 0) */
23878c2ecf20Sopenharmony_ci	lq_cmd->rs_table[idx].rate_n_flags = cpu_to_le32(new_rate);
23888c2ecf20Sopenharmony_ci
23898c2ecf20Sopenharmony_ci	if (il4965_num_of_ant(tbl_type.ant_type) == 1) {
23908c2ecf20Sopenharmony_ci		lq_cmd->general_params.single_stream_ant_msk =
23918c2ecf20Sopenharmony_ci		    tbl_type.ant_type;
23928c2ecf20Sopenharmony_ci	} else if (il4965_num_of_ant(tbl_type.ant_type) == 2) {
23938c2ecf20Sopenharmony_ci		lq_cmd->general_params.dual_stream_ant_msk = tbl_type.ant_type;
23948c2ecf20Sopenharmony_ci	}
23958c2ecf20Sopenharmony_ci	/* otherwise we don't modify the existing value */
23968c2ecf20Sopenharmony_ci	idx++;
23978c2ecf20Sopenharmony_ci	repeat_rate--;
23988c2ecf20Sopenharmony_ci	if (il)
23998c2ecf20Sopenharmony_ci		valid_tx_ant = il->hw_params.valid_tx_ant;
24008c2ecf20Sopenharmony_ci
24018c2ecf20Sopenharmony_ci	/* Fill rest of rate table */
24028c2ecf20Sopenharmony_ci	while (idx < LINK_QUAL_MAX_RETRY_NUM) {
24038c2ecf20Sopenharmony_ci		/* Repeat initial/next rate.
24048c2ecf20Sopenharmony_ci		 * For legacy IL_NUMBER_TRY == 1, this loop will not execute.
24058c2ecf20Sopenharmony_ci		 * For HT IL_HT_NUMBER_TRY == 3, this executes twice. */
24068c2ecf20Sopenharmony_ci		while (repeat_rate > 0 && idx < (LINK_QUAL_MAX_RETRY_NUM - 1)) {
24078c2ecf20Sopenharmony_ci			if (is_legacy(tbl_type.lq_type)) {
24088c2ecf20Sopenharmony_ci				if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
24098c2ecf20Sopenharmony_ci					ant_toggle_cnt++;
24108c2ecf20Sopenharmony_ci				else if (il &&
24118c2ecf20Sopenharmony_ci					 il4965_rs_toggle_antenna(valid_tx_ant,
24128c2ecf20Sopenharmony_ci								  &new_rate,
24138c2ecf20Sopenharmony_ci								  &tbl_type))
24148c2ecf20Sopenharmony_ci					ant_toggle_cnt = 1;
24158c2ecf20Sopenharmony_ci			}
24168c2ecf20Sopenharmony_ci
24178c2ecf20Sopenharmony_ci			/* Override next rate if needed for debug purposes */
24188c2ecf20Sopenharmony_ci			il4965_rs_dbgfs_set_mcs(lq_sta, &new_rate, idx);
24198c2ecf20Sopenharmony_ci
24208c2ecf20Sopenharmony_ci			/* Fill next table entry */
24218c2ecf20Sopenharmony_ci			lq_cmd->rs_table[idx].rate_n_flags =
24228c2ecf20Sopenharmony_ci			    cpu_to_le32(new_rate);
24238c2ecf20Sopenharmony_ci			repeat_rate--;
24248c2ecf20Sopenharmony_ci			idx++;
24258c2ecf20Sopenharmony_ci		}
24268c2ecf20Sopenharmony_ci
24278c2ecf20Sopenharmony_ci		il4965_rs_get_tbl_info_from_mcs(new_rate, lq_sta->band,
24288c2ecf20Sopenharmony_ci						&tbl_type, &rate_idx);
24298c2ecf20Sopenharmony_ci
24308c2ecf20Sopenharmony_ci		/* Indicate to uCode which entries might be MIMO.
24318c2ecf20Sopenharmony_ci		 * If initial rate was MIMO, this will finally end up
24328c2ecf20Sopenharmony_ci		 * as (IL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
24338c2ecf20Sopenharmony_ci		if (is_mimo(tbl_type.lq_type))
24348c2ecf20Sopenharmony_ci			lq_cmd->general_params.mimo_delimiter = idx;
24358c2ecf20Sopenharmony_ci
24368c2ecf20Sopenharmony_ci		/* Get next rate */
24378c2ecf20Sopenharmony_ci		new_rate =
24388c2ecf20Sopenharmony_ci		    il4965_rs_get_lower_rate(lq_sta, &tbl_type, rate_idx,
24398c2ecf20Sopenharmony_ci					     use_ht_possible);
24408c2ecf20Sopenharmony_ci
24418c2ecf20Sopenharmony_ci		/* How many times should we repeat the next rate? */
24428c2ecf20Sopenharmony_ci		if (is_legacy(tbl_type.lq_type)) {
24438c2ecf20Sopenharmony_ci			if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
24448c2ecf20Sopenharmony_ci				ant_toggle_cnt++;
24458c2ecf20Sopenharmony_ci			else if (il &&
24468c2ecf20Sopenharmony_ci				 il4965_rs_toggle_antenna(valid_tx_ant,
24478c2ecf20Sopenharmony_ci							  &new_rate, &tbl_type))
24488c2ecf20Sopenharmony_ci				ant_toggle_cnt = 1;
24498c2ecf20Sopenharmony_ci
24508c2ecf20Sopenharmony_ci			repeat_rate = IL_NUMBER_TRY;
24518c2ecf20Sopenharmony_ci		} else {
24528c2ecf20Sopenharmony_ci			repeat_rate = IL_HT_NUMBER_TRY;
24538c2ecf20Sopenharmony_ci		}
24548c2ecf20Sopenharmony_ci
24558c2ecf20Sopenharmony_ci		/* Don't allow HT rates after next pass.
24568c2ecf20Sopenharmony_ci		 * il4965_rs_get_lower_rate() will change type to LQ_A or LQ_G. */
24578c2ecf20Sopenharmony_ci		use_ht_possible = 0;
24588c2ecf20Sopenharmony_ci
24598c2ecf20Sopenharmony_ci		/* Override next rate if needed for debug purposes */
24608c2ecf20Sopenharmony_ci		il4965_rs_dbgfs_set_mcs(lq_sta, &new_rate, idx);
24618c2ecf20Sopenharmony_ci
24628c2ecf20Sopenharmony_ci		/* Fill next table entry */
24638c2ecf20Sopenharmony_ci		lq_cmd->rs_table[idx].rate_n_flags = cpu_to_le32(new_rate);
24648c2ecf20Sopenharmony_ci
24658c2ecf20Sopenharmony_ci		idx++;
24668c2ecf20Sopenharmony_ci		repeat_rate--;
24678c2ecf20Sopenharmony_ci	}
24688c2ecf20Sopenharmony_ci
24698c2ecf20Sopenharmony_ci	lq_cmd->agg_params.agg_frame_cnt_limit = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
24708c2ecf20Sopenharmony_ci	lq_cmd->agg_params.agg_dis_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
24718c2ecf20Sopenharmony_ci
24728c2ecf20Sopenharmony_ci	lq_cmd->agg_params.agg_time_limit =
24738c2ecf20Sopenharmony_ci	    cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
24748c2ecf20Sopenharmony_ci}
24758c2ecf20Sopenharmony_ci
24768c2ecf20Sopenharmony_cistatic void *
24778c2ecf20Sopenharmony_ciil4965_rs_alloc(struct ieee80211_hw *hw)
24788c2ecf20Sopenharmony_ci{
24798c2ecf20Sopenharmony_ci	return hw->priv;
24808c2ecf20Sopenharmony_ci}
24818c2ecf20Sopenharmony_ci
24828c2ecf20Sopenharmony_ci/* rate scale requires free function to be implemented */
24838c2ecf20Sopenharmony_cistatic void
24848c2ecf20Sopenharmony_ciil4965_rs_free(void *il_rate)
24858c2ecf20Sopenharmony_ci{
24868c2ecf20Sopenharmony_ci	return;
24878c2ecf20Sopenharmony_ci}
24888c2ecf20Sopenharmony_ci
24898c2ecf20Sopenharmony_cistatic void
24908c2ecf20Sopenharmony_ciil4965_rs_free_sta(void *il_r, struct ieee80211_sta *sta, void *il_sta)
24918c2ecf20Sopenharmony_ci{
24928c2ecf20Sopenharmony_ci	struct il_priv *il __maybe_unused = il_r;
24938c2ecf20Sopenharmony_ci
24948c2ecf20Sopenharmony_ci	D_RATE("enter\n");
24958c2ecf20Sopenharmony_ci	D_RATE("leave\n");
24968c2ecf20Sopenharmony_ci}
24978c2ecf20Sopenharmony_ci
24988c2ecf20Sopenharmony_ci#ifdef CONFIG_MAC80211_DEBUGFS
24998c2ecf20Sopenharmony_ci
25008c2ecf20Sopenharmony_cistatic void
25018c2ecf20Sopenharmony_ciil4965_rs_dbgfs_set_mcs(struct il_lq_sta *lq_sta, u32 * rate_n_flags, int idx)
25028c2ecf20Sopenharmony_ci{
25038c2ecf20Sopenharmony_ci	struct il_priv *il;
25048c2ecf20Sopenharmony_ci	u8 valid_tx_ant;
25058c2ecf20Sopenharmony_ci	u8 ant_sel_tx;
25068c2ecf20Sopenharmony_ci
25078c2ecf20Sopenharmony_ci	il = lq_sta->drv;
25088c2ecf20Sopenharmony_ci	valid_tx_ant = il->hw_params.valid_tx_ant;
25098c2ecf20Sopenharmony_ci	if (lq_sta->dbg_fixed_rate) {
25108c2ecf20Sopenharmony_ci		ant_sel_tx =
25118c2ecf20Sopenharmony_ci		    ((lq_sta->
25128c2ecf20Sopenharmony_ci		      dbg_fixed_rate & RATE_MCS_ANT_ABC_MSK) >>
25138c2ecf20Sopenharmony_ci		     RATE_MCS_ANT_POS);
25148c2ecf20Sopenharmony_ci		if ((valid_tx_ant & ant_sel_tx) == ant_sel_tx) {
25158c2ecf20Sopenharmony_ci			*rate_n_flags = lq_sta->dbg_fixed_rate;
25168c2ecf20Sopenharmony_ci			D_RATE("Fixed rate ON\n");
25178c2ecf20Sopenharmony_ci		} else {
25188c2ecf20Sopenharmony_ci			lq_sta->dbg_fixed_rate = 0;
25198c2ecf20Sopenharmony_ci			IL_ERR
25208c2ecf20Sopenharmony_ci			    ("Invalid antenna selection 0x%X, Valid is 0x%X\n",
25218c2ecf20Sopenharmony_ci			     ant_sel_tx, valid_tx_ant);
25228c2ecf20Sopenharmony_ci			D_RATE("Fixed rate OFF\n");
25238c2ecf20Sopenharmony_ci		}
25248c2ecf20Sopenharmony_ci	} else {
25258c2ecf20Sopenharmony_ci		D_RATE("Fixed rate OFF\n");
25268c2ecf20Sopenharmony_ci	}
25278c2ecf20Sopenharmony_ci}
25288c2ecf20Sopenharmony_ci
25298c2ecf20Sopenharmony_cistatic ssize_t
25308c2ecf20Sopenharmony_ciil4965_rs_sta_dbgfs_scale_table_write(struct file *file,
25318c2ecf20Sopenharmony_ci				      const char __user *user_buf,
25328c2ecf20Sopenharmony_ci				      size_t count, loff_t *ppos)
25338c2ecf20Sopenharmony_ci{
25348c2ecf20Sopenharmony_ci	struct il_lq_sta *lq_sta = file->private_data;
25358c2ecf20Sopenharmony_ci	struct il_priv *il;
25368c2ecf20Sopenharmony_ci	char buf[64];
25378c2ecf20Sopenharmony_ci	size_t buf_size;
25388c2ecf20Sopenharmony_ci	u32 parsed_rate;
25398c2ecf20Sopenharmony_ci
25408c2ecf20Sopenharmony_ci	il = lq_sta->drv;
25418c2ecf20Sopenharmony_ci	memset(buf, 0, sizeof(buf));
25428c2ecf20Sopenharmony_ci	buf_size = min(count, sizeof(buf) - 1);
25438c2ecf20Sopenharmony_ci	if (copy_from_user(buf, user_buf, buf_size))
25448c2ecf20Sopenharmony_ci		return -EFAULT;
25458c2ecf20Sopenharmony_ci
25468c2ecf20Sopenharmony_ci	if (sscanf(buf, "%x", &parsed_rate) == 1)
25478c2ecf20Sopenharmony_ci		lq_sta->dbg_fixed_rate = parsed_rate;
25488c2ecf20Sopenharmony_ci	else
25498c2ecf20Sopenharmony_ci		lq_sta->dbg_fixed_rate = 0;
25508c2ecf20Sopenharmony_ci
25518c2ecf20Sopenharmony_ci	lq_sta->active_legacy_rate = 0x0FFF;	/* 1 - 54 MBits, includes CCK */
25528c2ecf20Sopenharmony_ci	lq_sta->active_siso_rate = 0x1FD0;	/* 6 - 60 MBits, no 9, no CCK */
25538c2ecf20Sopenharmony_ci	lq_sta->active_mimo2_rate = 0x1FD0;	/* 6 - 60 MBits, no 9, no CCK */
25548c2ecf20Sopenharmony_ci
25558c2ecf20Sopenharmony_ci	D_RATE("sta_id %d rate 0x%X\n", lq_sta->lq.sta_id,
25568c2ecf20Sopenharmony_ci	       lq_sta->dbg_fixed_rate);
25578c2ecf20Sopenharmony_ci
25588c2ecf20Sopenharmony_ci	if (lq_sta->dbg_fixed_rate) {
25598c2ecf20Sopenharmony_ci		il4965_rs_fill_link_cmd(NULL, lq_sta, lq_sta->dbg_fixed_rate);
25608c2ecf20Sopenharmony_ci		il_send_lq_cmd(lq_sta->drv, &lq_sta->lq, CMD_ASYNC, false);
25618c2ecf20Sopenharmony_ci	}
25628c2ecf20Sopenharmony_ci
25638c2ecf20Sopenharmony_ci	return count;
25648c2ecf20Sopenharmony_ci}
25658c2ecf20Sopenharmony_ci
25668c2ecf20Sopenharmony_cistatic ssize_t
25678c2ecf20Sopenharmony_ciil4965_rs_sta_dbgfs_scale_table_read(struct file *file, char __user *user_buf,
25688c2ecf20Sopenharmony_ci				     size_t count, loff_t *ppos)
25698c2ecf20Sopenharmony_ci{
25708c2ecf20Sopenharmony_ci	char *buff;
25718c2ecf20Sopenharmony_ci	int desc = 0;
25728c2ecf20Sopenharmony_ci	int i = 0;
25738c2ecf20Sopenharmony_ci	int idx = 0;
25748c2ecf20Sopenharmony_ci	ssize_t ret;
25758c2ecf20Sopenharmony_ci
25768c2ecf20Sopenharmony_ci	struct il_lq_sta *lq_sta = file->private_data;
25778c2ecf20Sopenharmony_ci	struct il_priv *il;
25788c2ecf20Sopenharmony_ci	struct il_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
25798c2ecf20Sopenharmony_ci
25808c2ecf20Sopenharmony_ci	il = lq_sta->drv;
25818c2ecf20Sopenharmony_ci	buff = kmalloc(1024, GFP_KERNEL);
25828c2ecf20Sopenharmony_ci	if (!buff)
25838c2ecf20Sopenharmony_ci		return -ENOMEM;
25848c2ecf20Sopenharmony_ci
25858c2ecf20Sopenharmony_ci	desc += sprintf(buff + desc, "sta_id %d\n", lq_sta->lq.sta_id);
25868c2ecf20Sopenharmony_ci	desc +=
25878c2ecf20Sopenharmony_ci	    sprintf(buff + desc, "failed=%d success=%d rate=0%X\n",
25888c2ecf20Sopenharmony_ci		    lq_sta->total_failed, lq_sta->total_success,
25898c2ecf20Sopenharmony_ci		    lq_sta->active_legacy_rate);
25908c2ecf20Sopenharmony_ci	desc +=
25918c2ecf20Sopenharmony_ci	    sprintf(buff + desc, "fixed rate 0x%X\n", lq_sta->dbg_fixed_rate);
25928c2ecf20Sopenharmony_ci	desc +=
25938c2ecf20Sopenharmony_ci	    sprintf(buff + desc, "valid_tx_ant %s%s%s\n",
25948c2ecf20Sopenharmony_ci		    (il->hw_params.valid_tx_ant & ANT_A) ? "ANT_A," : "",
25958c2ecf20Sopenharmony_ci		    (il->hw_params.valid_tx_ant & ANT_B) ? "ANT_B," : "",
25968c2ecf20Sopenharmony_ci		    (il->hw_params.valid_tx_ant & ANT_C) ? "ANT_C" : "");
25978c2ecf20Sopenharmony_ci	desc +=
25988c2ecf20Sopenharmony_ci	    sprintf(buff + desc, "lq type %s\n",
25998c2ecf20Sopenharmony_ci		    (is_legacy(tbl->lq_type)) ? "legacy" : "HT");
26008c2ecf20Sopenharmony_ci	if (is_Ht(tbl->lq_type)) {
26018c2ecf20Sopenharmony_ci		desc +=
26028c2ecf20Sopenharmony_ci		    sprintf(buff + desc, " %s",
26038c2ecf20Sopenharmony_ci			    (is_siso(tbl->lq_type)) ? "SISO" : "MIMO2");
26048c2ecf20Sopenharmony_ci		desc +=
26058c2ecf20Sopenharmony_ci		    sprintf(buff + desc, " %s",
26068c2ecf20Sopenharmony_ci			    (tbl->is_ht40) ? "40MHz" : "20MHz");
26078c2ecf20Sopenharmony_ci		desc +=
26088c2ecf20Sopenharmony_ci		    sprintf(buff + desc, " %s %s %s\n",
26098c2ecf20Sopenharmony_ci			    (tbl->is_SGI) ? "SGI" : "",
26108c2ecf20Sopenharmony_ci			    (lq_sta->is_green) ? "GF enabled" : "",
26118c2ecf20Sopenharmony_ci			    (lq_sta->is_agg) ? "AGG on" : "");
26128c2ecf20Sopenharmony_ci	}
26138c2ecf20Sopenharmony_ci	desc +=
26148c2ecf20Sopenharmony_ci	    sprintf(buff + desc, "last tx rate=0x%X\n",
26158c2ecf20Sopenharmony_ci		    lq_sta->last_rate_n_flags);
26168c2ecf20Sopenharmony_ci	desc +=
26178c2ecf20Sopenharmony_ci	    sprintf(buff + desc,
26188c2ecf20Sopenharmony_ci		    "general:" "flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
26198c2ecf20Sopenharmony_ci		    lq_sta->lq.general_params.flags,
26208c2ecf20Sopenharmony_ci		    lq_sta->lq.general_params.mimo_delimiter,
26218c2ecf20Sopenharmony_ci		    lq_sta->lq.general_params.single_stream_ant_msk,
26228c2ecf20Sopenharmony_ci		    lq_sta->lq.general_params.dual_stream_ant_msk);
26238c2ecf20Sopenharmony_ci
26248c2ecf20Sopenharmony_ci	desc +=
26258c2ecf20Sopenharmony_ci	    sprintf(buff + desc,
26268c2ecf20Sopenharmony_ci		    "agg:"
26278c2ecf20Sopenharmony_ci		    "time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
26288c2ecf20Sopenharmony_ci		    le16_to_cpu(lq_sta->lq.agg_params.agg_time_limit),
26298c2ecf20Sopenharmony_ci		    lq_sta->lq.agg_params.agg_dis_start_th,
26308c2ecf20Sopenharmony_ci		    lq_sta->lq.agg_params.agg_frame_cnt_limit);
26318c2ecf20Sopenharmony_ci
26328c2ecf20Sopenharmony_ci	desc +=
26338c2ecf20Sopenharmony_ci	    sprintf(buff + desc,
26348c2ecf20Sopenharmony_ci		    "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
26358c2ecf20Sopenharmony_ci		    lq_sta->lq.general_params.start_rate_idx[0],
26368c2ecf20Sopenharmony_ci		    lq_sta->lq.general_params.start_rate_idx[1],
26378c2ecf20Sopenharmony_ci		    lq_sta->lq.general_params.start_rate_idx[2],
26388c2ecf20Sopenharmony_ci		    lq_sta->lq.general_params.start_rate_idx[3]);
26398c2ecf20Sopenharmony_ci
26408c2ecf20Sopenharmony_ci	for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
26418c2ecf20Sopenharmony_ci		idx =
26428c2ecf20Sopenharmony_ci		    il4965_hwrate_to_plcp_idx(le32_to_cpu
26438c2ecf20Sopenharmony_ci					      (lq_sta->lq.rs_table[i].
26448c2ecf20Sopenharmony_ci					       rate_n_flags));
26458c2ecf20Sopenharmony_ci		if (is_legacy(tbl->lq_type)) {
26468c2ecf20Sopenharmony_ci			desc +=
26478c2ecf20Sopenharmony_ci			    sprintf(buff + desc, " rate[%d] 0x%X %smbps\n", i,
26488c2ecf20Sopenharmony_ci				    le32_to_cpu(lq_sta->lq.rs_table[i].
26498c2ecf20Sopenharmony_ci						rate_n_flags),
26508c2ecf20Sopenharmony_ci				    il_rate_mcs[idx].mbps);
26518c2ecf20Sopenharmony_ci		} else {
26528c2ecf20Sopenharmony_ci			desc +=
26538c2ecf20Sopenharmony_ci			    sprintf(buff + desc, " rate[%d] 0x%X %smbps (%s)\n",
26548c2ecf20Sopenharmony_ci				    i,
26558c2ecf20Sopenharmony_ci				    le32_to_cpu(lq_sta->lq.rs_table[i].
26568c2ecf20Sopenharmony_ci						rate_n_flags),
26578c2ecf20Sopenharmony_ci				    il_rate_mcs[idx].mbps,
26588c2ecf20Sopenharmony_ci				    il_rate_mcs[idx].mcs);
26598c2ecf20Sopenharmony_ci		}
26608c2ecf20Sopenharmony_ci	}
26618c2ecf20Sopenharmony_ci
26628c2ecf20Sopenharmony_ci	ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
26638c2ecf20Sopenharmony_ci	kfree(buff);
26648c2ecf20Sopenharmony_ci	return ret;
26658c2ecf20Sopenharmony_ci}
26668c2ecf20Sopenharmony_ci
26678c2ecf20Sopenharmony_cistatic const struct file_operations rs_sta_dbgfs_scale_table_ops = {
26688c2ecf20Sopenharmony_ci	.write = il4965_rs_sta_dbgfs_scale_table_write,
26698c2ecf20Sopenharmony_ci	.read = il4965_rs_sta_dbgfs_scale_table_read,
26708c2ecf20Sopenharmony_ci	.open = simple_open,
26718c2ecf20Sopenharmony_ci	.llseek = default_llseek,
26728c2ecf20Sopenharmony_ci};
26738c2ecf20Sopenharmony_ci
26748c2ecf20Sopenharmony_cistatic ssize_t
26758c2ecf20Sopenharmony_ciil4965_rs_sta_dbgfs_stats_table_read(struct file *file, char __user *user_buf,
26768c2ecf20Sopenharmony_ci				     size_t count, loff_t *ppos)
26778c2ecf20Sopenharmony_ci{
26788c2ecf20Sopenharmony_ci	char *buff;
26798c2ecf20Sopenharmony_ci	int desc = 0;
26808c2ecf20Sopenharmony_ci	int i, j;
26818c2ecf20Sopenharmony_ci	ssize_t ret;
26828c2ecf20Sopenharmony_ci
26838c2ecf20Sopenharmony_ci	struct il_lq_sta *lq_sta = file->private_data;
26848c2ecf20Sopenharmony_ci
26858c2ecf20Sopenharmony_ci	buff = kmalloc(1024, GFP_KERNEL);
26868c2ecf20Sopenharmony_ci	if (!buff)
26878c2ecf20Sopenharmony_ci		return -ENOMEM;
26888c2ecf20Sopenharmony_ci
26898c2ecf20Sopenharmony_ci	for (i = 0; i < LQ_SIZE; i++) {
26908c2ecf20Sopenharmony_ci		desc +=
26918c2ecf20Sopenharmony_ci		    sprintf(buff + desc,
26928c2ecf20Sopenharmony_ci			    "%s type=%d SGI=%d HT40=%d DUP=%d GF=%d\n"
26938c2ecf20Sopenharmony_ci			    "rate=0x%X\n", lq_sta->active_tbl == i ? "*" : "x",
26948c2ecf20Sopenharmony_ci			    lq_sta->lq_info[i].lq_type,
26958c2ecf20Sopenharmony_ci			    lq_sta->lq_info[i].is_SGI,
26968c2ecf20Sopenharmony_ci			    lq_sta->lq_info[i].is_ht40,
26978c2ecf20Sopenharmony_ci			    lq_sta->lq_info[i].is_dup, lq_sta->is_green,
26988c2ecf20Sopenharmony_ci			    lq_sta->lq_info[i].current_rate);
26998c2ecf20Sopenharmony_ci		for (j = 0; j < RATE_COUNT; j++) {
27008c2ecf20Sopenharmony_ci			desc +=
27018c2ecf20Sopenharmony_ci			    sprintf(buff + desc,
27028c2ecf20Sopenharmony_ci				    "counter=%d success=%d %%=%d\n",
27038c2ecf20Sopenharmony_ci				    lq_sta->lq_info[i].win[j].counter,
27048c2ecf20Sopenharmony_ci				    lq_sta->lq_info[i].win[j].success_counter,
27058c2ecf20Sopenharmony_ci				    lq_sta->lq_info[i].win[j].success_ratio);
27068c2ecf20Sopenharmony_ci		}
27078c2ecf20Sopenharmony_ci	}
27088c2ecf20Sopenharmony_ci	ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
27098c2ecf20Sopenharmony_ci	kfree(buff);
27108c2ecf20Sopenharmony_ci	return ret;
27118c2ecf20Sopenharmony_ci}
27128c2ecf20Sopenharmony_ci
27138c2ecf20Sopenharmony_cistatic const struct file_operations rs_sta_dbgfs_stats_table_ops = {
27148c2ecf20Sopenharmony_ci	.read = il4965_rs_sta_dbgfs_stats_table_read,
27158c2ecf20Sopenharmony_ci	.open = simple_open,
27168c2ecf20Sopenharmony_ci	.llseek = default_llseek,
27178c2ecf20Sopenharmony_ci};
27188c2ecf20Sopenharmony_ci
27198c2ecf20Sopenharmony_cistatic ssize_t
27208c2ecf20Sopenharmony_ciil4965_rs_sta_dbgfs_rate_scale_data_read(struct file *file,
27218c2ecf20Sopenharmony_ci					 char __user *user_buf, size_t count,
27228c2ecf20Sopenharmony_ci					 loff_t *ppos)
27238c2ecf20Sopenharmony_ci{
27248c2ecf20Sopenharmony_ci	char buff[120];
27258c2ecf20Sopenharmony_ci	int desc = 0;
27268c2ecf20Sopenharmony_ci	struct il_lq_sta *lq_sta = file->private_data;
27278c2ecf20Sopenharmony_ci	struct il_scale_tbl_info *tbl = &lq_sta->lq_info[lq_sta->active_tbl];
27288c2ecf20Sopenharmony_ci
27298c2ecf20Sopenharmony_ci	if (is_Ht(tbl->lq_type))
27308c2ecf20Sopenharmony_ci		desc +=
27318c2ecf20Sopenharmony_ci		    sprintf(buff + desc, "Bit Rate= %d Mb/s\n",
27328c2ecf20Sopenharmony_ci			    tbl->expected_tpt[lq_sta->last_txrate_idx]);
27338c2ecf20Sopenharmony_ci	else
27348c2ecf20Sopenharmony_ci		desc +=
27358c2ecf20Sopenharmony_ci		    sprintf(buff + desc, "Bit Rate= %d Mb/s\n",
27368c2ecf20Sopenharmony_ci			    il_rates[lq_sta->last_txrate_idx].ieee >> 1);
27378c2ecf20Sopenharmony_ci
27388c2ecf20Sopenharmony_ci	return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
27398c2ecf20Sopenharmony_ci}
27408c2ecf20Sopenharmony_ci
27418c2ecf20Sopenharmony_cistatic const struct file_operations rs_sta_dbgfs_rate_scale_data_ops = {
27428c2ecf20Sopenharmony_ci	.read = il4965_rs_sta_dbgfs_rate_scale_data_read,
27438c2ecf20Sopenharmony_ci	.open = simple_open,
27448c2ecf20Sopenharmony_ci	.llseek = default_llseek,
27458c2ecf20Sopenharmony_ci};
27468c2ecf20Sopenharmony_ci
27478c2ecf20Sopenharmony_cistatic void
27488c2ecf20Sopenharmony_ciil4965_rs_add_debugfs(void *il, void *il_sta, struct dentry *dir)
27498c2ecf20Sopenharmony_ci{
27508c2ecf20Sopenharmony_ci	struct il_lq_sta *lq_sta = il_sta;
27518c2ecf20Sopenharmony_ci
27528c2ecf20Sopenharmony_ci	debugfs_create_file("rate_scale_table", 0600, dir, lq_sta,
27538c2ecf20Sopenharmony_ci			    &rs_sta_dbgfs_scale_table_ops);
27548c2ecf20Sopenharmony_ci	debugfs_create_file("rate_stats_table", 0400, dir, lq_sta,
27558c2ecf20Sopenharmony_ci			    &rs_sta_dbgfs_stats_table_ops);
27568c2ecf20Sopenharmony_ci	debugfs_create_file("rate_scale_data", 0400, dir, lq_sta,
27578c2ecf20Sopenharmony_ci			    &rs_sta_dbgfs_rate_scale_data_ops);
27588c2ecf20Sopenharmony_ci	debugfs_create_u8("tx_agg_tid_enable", 0600, dir,
27598c2ecf20Sopenharmony_ci			  &lq_sta->tx_agg_tid_en);
27608c2ecf20Sopenharmony_ci}
27618c2ecf20Sopenharmony_ci#endif
27628c2ecf20Sopenharmony_ci
27638c2ecf20Sopenharmony_ci/*
27648c2ecf20Sopenharmony_ci * Initialization of rate scaling information is done by driver after
27658c2ecf20Sopenharmony_ci * the station is added. Since mac80211 calls this function before a
27668c2ecf20Sopenharmony_ci * station is added we ignore it.
27678c2ecf20Sopenharmony_ci */
27688c2ecf20Sopenharmony_cistatic void
27698c2ecf20Sopenharmony_ciil4965_rs_rate_init_stub(void *il_r, struct ieee80211_supported_band *sband,
27708c2ecf20Sopenharmony_ci			 struct cfg80211_chan_def *chandef,
27718c2ecf20Sopenharmony_ci			 struct ieee80211_sta *sta, void *il_sta)
27728c2ecf20Sopenharmony_ci{
27738c2ecf20Sopenharmony_ci}
27748c2ecf20Sopenharmony_ci
27758c2ecf20Sopenharmony_cistatic const struct rate_control_ops rs_4965_ops = {
27768c2ecf20Sopenharmony_ci	.name = IL4965_RS_NAME,
27778c2ecf20Sopenharmony_ci	.tx_status = il4965_rs_tx_status,
27788c2ecf20Sopenharmony_ci	.get_rate = il4965_rs_get_rate,
27798c2ecf20Sopenharmony_ci	.rate_init = il4965_rs_rate_init_stub,
27808c2ecf20Sopenharmony_ci	.alloc = il4965_rs_alloc,
27818c2ecf20Sopenharmony_ci	.free = il4965_rs_free,
27828c2ecf20Sopenharmony_ci	.alloc_sta = il4965_rs_alloc_sta,
27838c2ecf20Sopenharmony_ci	.free_sta = il4965_rs_free_sta,
27848c2ecf20Sopenharmony_ci#ifdef CONFIG_MAC80211_DEBUGFS
27858c2ecf20Sopenharmony_ci	.add_sta_debugfs = il4965_rs_add_debugfs,
27868c2ecf20Sopenharmony_ci#endif
27878c2ecf20Sopenharmony_ci};
27888c2ecf20Sopenharmony_ci
27898c2ecf20Sopenharmony_ciint
27908c2ecf20Sopenharmony_ciil4965_rate_control_register(void)
27918c2ecf20Sopenharmony_ci{
27928c2ecf20Sopenharmony_ci	return ieee80211_rate_control_register(&rs_4965_ops);
27938c2ecf20Sopenharmony_ci}
27948c2ecf20Sopenharmony_ci
27958c2ecf20Sopenharmony_civoid
27968c2ecf20Sopenharmony_ciil4965_rate_control_unregister(void)
27978c2ecf20Sopenharmony_ci{
27988c2ecf20Sopenharmony_ci	ieee80211_rate_control_unregister(&rs_4965_ops);
27998c2ecf20Sopenharmony_ci}
2800