162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
262306a36Sopenharmony_ci/******************************************************************************
362306a36Sopenharmony_ci *
462306a36Sopenharmony_ci * Copyright(c) 2005 - 2014 Intel Corporation. All rights reserved.
562306a36Sopenharmony_ci * Copyright (C) 2019 - 2020, 2022 - 2023 Intel Corporation
662306a36Sopenharmony_ci *****************************************************************************/
762306a36Sopenharmony_ci#include <linux/kernel.h>
862306a36Sopenharmony_ci#include <linux/skbuff.h>
962306a36Sopenharmony_ci#include <linux/slab.h>
1062306a36Sopenharmony_ci#include <net/mac80211.h>
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ci#include <linux/netdevice.h>
1362306a36Sopenharmony_ci#include <linux/etherdevice.h>
1462306a36Sopenharmony_ci#include <linux/delay.h>
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ci#include <linux/workqueue.h>
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_ci#include "dev.h"
1962306a36Sopenharmony_ci#include "agn.h"
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_ci#define RS_NAME "iwl-agn-rs"
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_ci#define NUM_TRY_BEFORE_ANT_TOGGLE 1
2462306a36Sopenharmony_ci#define IWL_NUMBER_TRY      1
2562306a36Sopenharmony_ci#define IWL_HT_NUMBER_TRY   3
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_ci#define IWL_RATE_MAX_WINDOW		62	/* # tx in history window */
2862306a36Sopenharmony_ci#define IWL_RATE_MIN_FAILURE_TH		6	/* min failures to calc tpt */
2962306a36Sopenharmony_ci#define IWL_RATE_MIN_SUCCESS_TH		8	/* min successes to calc tpt */
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_ci/* max allowed rate miss before sync LQ cmd */
3262306a36Sopenharmony_ci#define IWL_MISSED_RATE_MAX		15
3362306a36Sopenharmony_ci/* max time to accum history 2 seconds */
3462306a36Sopenharmony_ci#define IWL_RATE_SCALE_FLUSH_INTVL   (3*HZ)
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_cistatic u8 rs_ht_to_legacy[] = {
3762306a36Sopenharmony_ci	IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
3862306a36Sopenharmony_ci	IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
3962306a36Sopenharmony_ci	IWL_RATE_6M_INDEX,
4062306a36Sopenharmony_ci	IWL_RATE_6M_INDEX, IWL_RATE_9M_INDEX,
4162306a36Sopenharmony_ci	IWL_RATE_12M_INDEX, IWL_RATE_18M_INDEX,
4262306a36Sopenharmony_ci	IWL_RATE_24M_INDEX, IWL_RATE_36M_INDEX,
4362306a36Sopenharmony_ci	IWL_RATE_48M_INDEX, IWL_RATE_54M_INDEX
4462306a36Sopenharmony_ci};
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_cistatic const u8 ant_toggle_lookup[] = {
4762306a36Sopenharmony_ci	/*ANT_NONE -> */ ANT_NONE,
4862306a36Sopenharmony_ci	/*ANT_A    -> */ ANT_B,
4962306a36Sopenharmony_ci	/*ANT_B    -> */ ANT_C,
5062306a36Sopenharmony_ci	/*ANT_AB   -> */ ANT_BC,
5162306a36Sopenharmony_ci	/*ANT_C    -> */ ANT_A,
5262306a36Sopenharmony_ci	/*ANT_AC   -> */ ANT_AB,
5362306a36Sopenharmony_ci	/*ANT_BC   -> */ ANT_AC,
5462306a36Sopenharmony_ci	/*ANT_ABC  -> */ ANT_ABC,
5562306a36Sopenharmony_ci};
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_ci#define IWL_DECLARE_RATE_INFO(r, s, ip, in, rp, rn, pp, np)    \
5862306a36Sopenharmony_ci	[IWL_RATE_##r##M_INDEX] = { IWL_RATE_##r##M_PLCP,      \
5962306a36Sopenharmony_ci				    IWL_RATE_SISO_##s##M_PLCP, \
6062306a36Sopenharmony_ci				    IWL_RATE_MIMO2_##s##M_PLCP,\
6162306a36Sopenharmony_ci				    IWL_RATE_MIMO3_##s##M_PLCP,\
6262306a36Sopenharmony_ci				    IWL_RATE_##r##M_IEEE,      \
6362306a36Sopenharmony_ci				    IWL_RATE_##ip##M_INDEX,    \
6462306a36Sopenharmony_ci				    IWL_RATE_##in##M_INDEX,    \
6562306a36Sopenharmony_ci				    IWL_RATE_##rp##M_INDEX,    \
6662306a36Sopenharmony_ci				    IWL_RATE_##rn##M_INDEX,    \
6762306a36Sopenharmony_ci				    IWL_RATE_##pp##M_INDEX,    \
6862306a36Sopenharmony_ci				    IWL_RATE_##np##M_INDEX }
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ci/*
7162306a36Sopenharmony_ci * Parameter order:
7262306a36Sopenharmony_ci *   rate, ht rate, prev rate, next rate, prev tgg rate, next tgg rate
7362306a36Sopenharmony_ci *
7462306a36Sopenharmony_ci * If there isn't a valid next or previous rate then INV is used which
7562306a36Sopenharmony_ci * maps to IWL_RATE_INVALID
7662306a36Sopenharmony_ci *
7762306a36Sopenharmony_ci */
7862306a36Sopenharmony_ciconst struct iwl_rate_info iwl_rates[IWL_RATE_COUNT] = {
7962306a36Sopenharmony_ci	IWL_DECLARE_RATE_INFO(1, INV, INV, 2, INV, 2, INV, 2),    /*  1mbps */
8062306a36Sopenharmony_ci	IWL_DECLARE_RATE_INFO(2, INV, 1, 5, 1, 5, 1, 5),          /*  2mbps */
8162306a36Sopenharmony_ci	IWL_DECLARE_RATE_INFO(5, INV, 2, 6, 2, 11, 2, 11),        /*5.5mbps */
8262306a36Sopenharmony_ci	IWL_DECLARE_RATE_INFO(11, INV, 9, 12, 9, 12, 5, 18),      /* 11mbps */
8362306a36Sopenharmony_ci	IWL_DECLARE_RATE_INFO(6, 6, 5, 9, 5, 11, 5, 11),        /*  6mbps */
8462306a36Sopenharmony_ci	IWL_DECLARE_RATE_INFO(9, 6, 6, 11, 6, 11, 5, 11),       /*  9mbps */
8562306a36Sopenharmony_ci	IWL_DECLARE_RATE_INFO(12, 12, 11, 18, 11, 18, 11, 18),   /* 12mbps */
8662306a36Sopenharmony_ci	IWL_DECLARE_RATE_INFO(18, 18, 12, 24, 12, 24, 11, 24),   /* 18mbps */
8762306a36Sopenharmony_ci	IWL_DECLARE_RATE_INFO(24, 24, 18, 36, 18, 36, 18, 36),   /* 24mbps */
8862306a36Sopenharmony_ci	IWL_DECLARE_RATE_INFO(36, 36, 24, 48, 24, 48, 24, 48),   /* 36mbps */
8962306a36Sopenharmony_ci	IWL_DECLARE_RATE_INFO(48, 48, 36, 54, 36, 54, 36, 54),   /* 48mbps */
9062306a36Sopenharmony_ci	IWL_DECLARE_RATE_INFO(54, 54, 48, INV, 48, INV, 48, INV),/* 54mbps */
9162306a36Sopenharmony_ci	IWL_DECLARE_RATE_INFO(60, 60, 48, INV, 48, INV, 48, INV),/* 60mbps */
9262306a36Sopenharmony_ci	/* FIXME:RS:          ^^    should be INV (legacy) */
9362306a36Sopenharmony_ci};
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_cistatic inline u8 rs_extract_rate(u32 rate_n_flags)
9662306a36Sopenharmony_ci{
9762306a36Sopenharmony_ci	return (u8)(rate_n_flags & RATE_MCS_RATE_MSK);
9862306a36Sopenharmony_ci}
9962306a36Sopenharmony_ci
10062306a36Sopenharmony_cistatic int iwl_hwrate_to_plcp_idx(u32 rate_n_flags)
10162306a36Sopenharmony_ci{
10262306a36Sopenharmony_ci	int idx = 0;
10362306a36Sopenharmony_ci
10462306a36Sopenharmony_ci	/* HT rate format */
10562306a36Sopenharmony_ci	if (rate_n_flags & RATE_MCS_HT_MSK) {
10662306a36Sopenharmony_ci		idx = rs_extract_rate(rate_n_flags);
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_ci		if (idx >= IWL_RATE_MIMO3_6M_PLCP)
10962306a36Sopenharmony_ci			idx = idx - IWL_RATE_MIMO3_6M_PLCP;
11062306a36Sopenharmony_ci		else if (idx >= IWL_RATE_MIMO2_6M_PLCP)
11162306a36Sopenharmony_ci			idx = idx - IWL_RATE_MIMO2_6M_PLCP;
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_ci		idx += IWL_FIRST_OFDM_RATE;
11462306a36Sopenharmony_ci		/* skip 9M not supported in ht*/
11562306a36Sopenharmony_ci		if (idx >= IWL_RATE_9M_INDEX)
11662306a36Sopenharmony_ci			idx += 1;
11762306a36Sopenharmony_ci		if ((idx >= IWL_FIRST_OFDM_RATE) && (idx <= IWL_LAST_OFDM_RATE))
11862306a36Sopenharmony_ci			return idx;
11962306a36Sopenharmony_ci
12062306a36Sopenharmony_ci	/* legacy rate format, search for match in table */
12162306a36Sopenharmony_ci	} else {
12262306a36Sopenharmony_ci		for (idx = 0; idx < ARRAY_SIZE(iwl_rates); idx++)
12362306a36Sopenharmony_ci			if (iwl_rates[idx].plcp ==
12462306a36Sopenharmony_ci					rs_extract_rate(rate_n_flags))
12562306a36Sopenharmony_ci				return idx;
12662306a36Sopenharmony_ci	}
12762306a36Sopenharmony_ci
12862306a36Sopenharmony_ci	return IWL_RATE_INVALID;
12962306a36Sopenharmony_ci}
13062306a36Sopenharmony_ci
13162306a36Sopenharmony_cistatic void rs_rate_scale_perform(struct iwl_priv *priv,
13262306a36Sopenharmony_ci				   struct sk_buff *skb,
13362306a36Sopenharmony_ci				   struct ieee80211_sta *sta,
13462306a36Sopenharmony_ci				   struct iwl_lq_sta *lq_sta);
13562306a36Sopenharmony_cistatic void rs_fill_link_cmd(struct iwl_priv *priv,
13662306a36Sopenharmony_ci			     struct iwl_lq_sta *lq_sta, u32 rate_n_flags);
13762306a36Sopenharmony_cistatic void rs_stay_in_table(struct iwl_lq_sta *lq_sta, bool force_search);
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ci
14062306a36Sopenharmony_ci#ifdef CONFIG_MAC80211_DEBUGFS
14162306a36Sopenharmony_cistatic void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
14262306a36Sopenharmony_ci			     u32 *rate_n_flags, int index);
14362306a36Sopenharmony_ci#else
14462306a36Sopenharmony_cistatic void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
14562306a36Sopenharmony_ci			     u32 *rate_n_flags, int index)
14662306a36Sopenharmony_ci{}
14762306a36Sopenharmony_ci#endif
14862306a36Sopenharmony_ci
14962306a36Sopenharmony_ci/*
15062306a36Sopenharmony_ci * The following tables contain the expected throughput metrics for all rates
15162306a36Sopenharmony_ci *
15262306a36Sopenharmony_ci *	1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
15362306a36Sopenharmony_ci *
15462306a36Sopenharmony_ci * where invalid entries are zeros.
15562306a36Sopenharmony_ci *
15662306a36Sopenharmony_ci * CCK rates are only valid in legacy table and will only be used in G
15762306a36Sopenharmony_ci * (2.4 GHz) band.
15862306a36Sopenharmony_ci */
15962306a36Sopenharmony_ci
16062306a36Sopenharmony_cistatic const u16 expected_tpt_legacy[IWL_RATE_COUNT] = {
16162306a36Sopenharmony_ci	7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 0
16262306a36Sopenharmony_ci};
16362306a36Sopenharmony_ci
16462306a36Sopenharmony_cistatic const u16 expected_tpt_siso20MHz[4][IWL_RATE_COUNT] = {
16562306a36Sopenharmony_ci	{0, 0, 0, 0, 42, 0,  76, 102, 124, 159, 183, 193, 202}, /* Norm */
16662306a36Sopenharmony_ci	{0, 0, 0, 0, 46, 0,  82, 110, 132, 168, 192, 202, 210}, /* SGI */
16762306a36Sopenharmony_ci	{0, 0, 0, 0, 47, 0,  91, 133, 171, 242, 305, 334, 362}, /* AGG */
16862306a36Sopenharmony_ci	{0, 0, 0, 0, 52, 0, 101, 145, 187, 264, 330, 361, 390}, /* AGG+SGI */
16962306a36Sopenharmony_ci};
17062306a36Sopenharmony_ci
17162306a36Sopenharmony_cistatic const u16 expected_tpt_siso40MHz[4][IWL_RATE_COUNT] = {
17262306a36Sopenharmony_ci	{0, 0, 0, 0,  77, 0, 127, 160, 184, 220, 242, 250, 257}, /* Norm */
17362306a36Sopenharmony_ci	{0, 0, 0, 0,  83, 0, 135, 169, 193, 229, 250, 257, 264}, /* SGI */
17462306a36Sopenharmony_ci	{0, 0, 0, 0,  94, 0, 177, 249, 313, 423, 512, 550, 586}, /* AGG */
17562306a36Sopenharmony_ci	{0, 0, 0, 0, 104, 0, 193, 270, 338, 454, 545, 584, 620}, /* AGG+SGI */
17662306a36Sopenharmony_ci};
17762306a36Sopenharmony_ci
17862306a36Sopenharmony_cistatic const u16 expected_tpt_mimo2_20MHz[4][IWL_RATE_COUNT] = {
17962306a36Sopenharmony_ci	{0, 0, 0, 0,  74, 0, 123, 155, 179, 214, 236, 244, 251}, /* Norm */
18062306a36Sopenharmony_ci	{0, 0, 0, 0,  81, 0, 131, 164, 188, 223, 243, 251, 257}, /* SGI */
18162306a36Sopenharmony_ci	{0, 0, 0, 0,  89, 0, 167, 235, 296, 402, 488, 526, 560}, /* AGG */
18262306a36Sopenharmony_ci	{0, 0, 0, 0,  97, 0, 182, 255, 320, 431, 520, 558, 593}, /* AGG+SGI*/
18362306a36Sopenharmony_ci};
18462306a36Sopenharmony_ci
18562306a36Sopenharmony_cistatic const u16 expected_tpt_mimo2_40MHz[4][IWL_RATE_COUNT] = {
18662306a36Sopenharmony_ci	{0, 0, 0, 0, 123, 0, 182, 214, 235, 264, 279, 285, 289}, /* Norm */
18762306a36Sopenharmony_ci	{0, 0, 0, 0, 131, 0, 191, 222, 242, 270, 284, 289, 293}, /* SGI */
18862306a36Sopenharmony_ci	{0, 0, 0, 0, 171, 0, 305, 410, 496, 634, 731, 771, 805}, /* AGG */
18962306a36Sopenharmony_ci	{0, 0, 0, 0, 186, 0, 329, 439, 527, 667, 764, 803, 838}, /* AGG+SGI */
19062306a36Sopenharmony_ci};
19162306a36Sopenharmony_ci
19262306a36Sopenharmony_cistatic const u16 expected_tpt_mimo3_20MHz[4][IWL_RATE_COUNT] = {
19362306a36Sopenharmony_ci	{0, 0, 0, 0,  99, 0, 153, 186, 208, 239, 256, 263, 268}, /* Norm */
19462306a36Sopenharmony_ci	{0, 0, 0, 0, 106, 0, 162, 194, 215, 246, 262, 268, 273}, /* SGI */
19562306a36Sopenharmony_ci	{0, 0, 0, 0, 134, 0, 249, 346, 431, 574, 685, 732, 775}, /* AGG */
19662306a36Sopenharmony_ci	{0, 0, 0, 0, 148, 0, 272, 376, 465, 614, 727, 775, 818}, /* AGG+SGI */
19762306a36Sopenharmony_ci};
19862306a36Sopenharmony_ci
19962306a36Sopenharmony_cistatic const u16 expected_tpt_mimo3_40MHz[4][IWL_RATE_COUNT] = {
20062306a36Sopenharmony_ci	{0, 0, 0, 0, 152, 0, 211, 239, 255, 279,  290,  294,  297}, /* Norm */
20162306a36Sopenharmony_ci	{0, 0, 0, 0, 160, 0, 219, 245, 261, 284,  294,  297,  300}, /* SGI */
20262306a36Sopenharmony_ci	{0, 0, 0, 0, 254, 0, 443, 584, 695, 868,  984, 1030, 1070}, /* AGG */
20362306a36Sopenharmony_ci	{0, 0, 0, 0, 277, 0, 478, 624, 737, 911, 1026, 1070, 1109}, /* AGG+SGI */
20462306a36Sopenharmony_ci};
20562306a36Sopenharmony_ci
20662306a36Sopenharmony_ci#define MCS_INDEX_PER_STREAM	(8)
20762306a36Sopenharmony_ci
20862306a36Sopenharmony_cistatic void rs_rate_scale_clear_window(struct iwl_rate_scale_data *window)
20962306a36Sopenharmony_ci{
21062306a36Sopenharmony_ci	window->data = 0;
21162306a36Sopenharmony_ci	window->success_counter = 0;
21262306a36Sopenharmony_ci	window->success_ratio = IWL_INVALID_VALUE;
21362306a36Sopenharmony_ci	window->counter = 0;
21462306a36Sopenharmony_ci	window->average_tpt = IWL_INVALID_VALUE;
21562306a36Sopenharmony_ci	window->stamp = 0;
21662306a36Sopenharmony_ci}
21762306a36Sopenharmony_ci
21862306a36Sopenharmony_cistatic inline u8 rs_is_valid_ant(u8 valid_antenna, u8 ant_type)
21962306a36Sopenharmony_ci{
22062306a36Sopenharmony_ci	return (ant_type & valid_antenna) == ant_type;
22162306a36Sopenharmony_ci}
22262306a36Sopenharmony_ci
22362306a36Sopenharmony_ci/*
22462306a36Sopenharmony_ci *	removes the old data from the statistics. All data that is older than
22562306a36Sopenharmony_ci *	TID_MAX_TIME_DIFF, will be deleted.
22662306a36Sopenharmony_ci */
22762306a36Sopenharmony_cistatic void rs_tl_rm_old_stats(struct iwl_traffic_load *tl, u32 curr_time)
22862306a36Sopenharmony_ci{
22962306a36Sopenharmony_ci	/* The oldest age we want to keep */
23062306a36Sopenharmony_ci	u32 oldest_time = curr_time - TID_MAX_TIME_DIFF;
23162306a36Sopenharmony_ci
23262306a36Sopenharmony_ci	while (tl->queue_count &&
23362306a36Sopenharmony_ci	       (tl->time_stamp < oldest_time)) {
23462306a36Sopenharmony_ci		tl->total -= tl->packet_count[tl->head];
23562306a36Sopenharmony_ci		tl->packet_count[tl->head] = 0;
23662306a36Sopenharmony_ci		tl->time_stamp += TID_QUEUE_CELL_SPACING;
23762306a36Sopenharmony_ci		tl->queue_count--;
23862306a36Sopenharmony_ci		tl->head++;
23962306a36Sopenharmony_ci		if (tl->head >= TID_QUEUE_MAX_SIZE)
24062306a36Sopenharmony_ci			tl->head = 0;
24162306a36Sopenharmony_ci	}
24262306a36Sopenharmony_ci}
24362306a36Sopenharmony_ci
24462306a36Sopenharmony_ci/*
24562306a36Sopenharmony_ci *	increment traffic load value for tid and also remove
24662306a36Sopenharmony_ci *	any old values if passed the certain time period
24762306a36Sopenharmony_ci */
24862306a36Sopenharmony_cistatic u8 rs_tl_add_packet(struct iwl_lq_sta *lq_data,
24962306a36Sopenharmony_ci			   struct ieee80211_hdr *hdr)
25062306a36Sopenharmony_ci{
25162306a36Sopenharmony_ci	u32 curr_time = jiffies_to_msecs(jiffies);
25262306a36Sopenharmony_ci	u32 time_diff;
25362306a36Sopenharmony_ci	s32 index;
25462306a36Sopenharmony_ci	struct iwl_traffic_load *tl = NULL;
25562306a36Sopenharmony_ci	u8 tid;
25662306a36Sopenharmony_ci
25762306a36Sopenharmony_ci	if (ieee80211_is_data_qos(hdr->frame_control)) {
25862306a36Sopenharmony_ci		u8 *qc = ieee80211_get_qos_ctl(hdr);
25962306a36Sopenharmony_ci		tid = qc[0] & 0xf;
26062306a36Sopenharmony_ci	} else
26162306a36Sopenharmony_ci		return IWL_MAX_TID_COUNT;
26262306a36Sopenharmony_ci
26362306a36Sopenharmony_ci	if (unlikely(tid >= IWL_MAX_TID_COUNT))
26462306a36Sopenharmony_ci		return IWL_MAX_TID_COUNT;
26562306a36Sopenharmony_ci
26662306a36Sopenharmony_ci	tl = &lq_data->load[tid];
26762306a36Sopenharmony_ci
26862306a36Sopenharmony_ci	curr_time -= curr_time % TID_ROUND_VALUE;
26962306a36Sopenharmony_ci
27062306a36Sopenharmony_ci	/* Happens only for the first packet. Initialize the data */
27162306a36Sopenharmony_ci	if (!(tl->queue_count)) {
27262306a36Sopenharmony_ci		tl->total = 1;
27362306a36Sopenharmony_ci		tl->time_stamp = curr_time;
27462306a36Sopenharmony_ci		tl->queue_count = 1;
27562306a36Sopenharmony_ci		tl->head = 0;
27662306a36Sopenharmony_ci		tl->packet_count[0] = 1;
27762306a36Sopenharmony_ci		return IWL_MAX_TID_COUNT;
27862306a36Sopenharmony_ci	}
27962306a36Sopenharmony_ci
28062306a36Sopenharmony_ci	time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
28162306a36Sopenharmony_ci	index = time_diff / TID_QUEUE_CELL_SPACING;
28262306a36Sopenharmony_ci
28362306a36Sopenharmony_ci	/* The history is too long: remove data that is older than */
28462306a36Sopenharmony_ci	/* TID_MAX_TIME_DIFF */
28562306a36Sopenharmony_ci	if (index >= TID_QUEUE_MAX_SIZE)
28662306a36Sopenharmony_ci		rs_tl_rm_old_stats(tl, curr_time);
28762306a36Sopenharmony_ci
28862306a36Sopenharmony_ci	index = (tl->head + index) % TID_QUEUE_MAX_SIZE;
28962306a36Sopenharmony_ci	tl->packet_count[index] = tl->packet_count[index] + 1;
29062306a36Sopenharmony_ci	tl->total = tl->total + 1;
29162306a36Sopenharmony_ci
29262306a36Sopenharmony_ci	if ((index + 1) > tl->queue_count)
29362306a36Sopenharmony_ci		tl->queue_count = index + 1;
29462306a36Sopenharmony_ci
29562306a36Sopenharmony_ci	return tid;
29662306a36Sopenharmony_ci}
29762306a36Sopenharmony_ci
29862306a36Sopenharmony_ci#ifdef CONFIG_MAC80211_DEBUGFS
29962306a36Sopenharmony_ci/*
30062306a36Sopenharmony_ci * Program the device to use fixed rate for frame transmit
30162306a36Sopenharmony_ci * This is for debugging/testing only
30262306a36Sopenharmony_ci * once the device start use fixed rate, we need to reload the module
30362306a36Sopenharmony_ci * to being back the normal operation.
30462306a36Sopenharmony_ci */
30562306a36Sopenharmony_cistatic void rs_program_fix_rate(struct iwl_priv *priv,
30662306a36Sopenharmony_ci				struct iwl_lq_sta *lq_sta)
30762306a36Sopenharmony_ci{
30862306a36Sopenharmony_ci	struct iwl_station_priv *sta_priv =
30962306a36Sopenharmony_ci		container_of(lq_sta, struct iwl_station_priv, lq_sta);
31062306a36Sopenharmony_ci	struct iwl_rxon_context *ctx = sta_priv->ctx;
31162306a36Sopenharmony_ci
31262306a36Sopenharmony_ci	lq_sta->active_legacy_rate = 0x0FFF;	/* 1 - 54 MBits, includes CCK */
31362306a36Sopenharmony_ci	lq_sta->active_siso_rate   = 0x1FD0;	/* 6 - 60 MBits, no 9, no CCK */
31462306a36Sopenharmony_ci	lq_sta->active_mimo2_rate  = 0x1FD0;	/* 6 - 60 MBits, no 9, no CCK */
31562306a36Sopenharmony_ci	lq_sta->active_mimo3_rate  = 0x1FD0;	/* 6 - 60 MBits, no 9, no CCK */
31662306a36Sopenharmony_ci
31762306a36Sopenharmony_ci	IWL_DEBUG_RATE(priv, "sta_id %d rate 0x%X\n",
31862306a36Sopenharmony_ci		lq_sta->lq.sta_id, lq_sta->dbg_fixed_rate);
31962306a36Sopenharmony_ci
32062306a36Sopenharmony_ci	if (lq_sta->dbg_fixed_rate) {
32162306a36Sopenharmony_ci		rs_fill_link_cmd(NULL, lq_sta, lq_sta->dbg_fixed_rate);
32262306a36Sopenharmony_ci		iwl_send_lq_cmd(lq_sta->drv, ctx, &lq_sta->lq, CMD_ASYNC,
32362306a36Sopenharmony_ci				false);
32462306a36Sopenharmony_ci	}
32562306a36Sopenharmony_ci}
32662306a36Sopenharmony_ci#endif
32762306a36Sopenharmony_ci
32862306a36Sopenharmony_ci/*
32962306a36Sopenharmony_ci	get the traffic load value for tid
33062306a36Sopenharmony_ci*/
33162306a36Sopenharmony_cistatic void rs_tl_get_load(struct iwl_lq_sta *lq_data, u8 tid)
33262306a36Sopenharmony_ci{
33362306a36Sopenharmony_ci	u32 curr_time = jiffies_to_msecs(jiffies);
33462306a36Sopenharmony_ci	u32 time_diff;
33562306a36Sopenharmony_ci	s32 index;
33662306a36Sopenharmony_ci	struct iwl_traffic_load *tl = NULL;
33762306a36Sopenharmony_ci
33862306a36Sopenharmony_ci	if (tid >= IWL_MAX_TID_COUNT)
33962306a36Sopenharmony_ci		return;
34062306a36Sopenharmony_ci
34162306a36Sopenharmony_ci	tl = &(lq_data->load[tid]);
34262306a36Sopenharmony_ci
34362306a36Sopenharmony_ci	curr_time -= curr_time % TID_ROUND_VALUE;
34462306a36Sopenharmony_ci
34562306a36Sopenharmony_ci	if (!(tl->queue_count))
34662306a36Sopenharmony_ci		return;
34762306a36Sopenharmony_ci
34862306a36Sopenharmony_ci	time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
34962306a36Sopenharmony_ci	index = time_diff / TID_QUEUE_CELL_SPACING;
35062306a36Sopenharmony_ci
35162306a36Sopenharmony_ci	/* The history is too long: remove data that is older than */
35262306a36Sopenharmony_ci	/* TID_MAX_TIME_DIFF */
35362306a36Sopenharmony_ci	if (index >= TID_QUEUE_MAX_SIZE)
35462306a36Sopenharmony_ci		rs_tl_rm_old_stats(tl, curr_time);
35562306a36Sopenharmony_ci}
35662306a36Sopenharmony_ci
35762306a36Sopenharmony_cistatic int rs_tl_turn_on_agg_for_tid(struct iwl_priv *priv,
35862306a36Sopenharmony_ci				      struct iwl_lq_sta *lq_data, u8 tid,
35962306a36Sopenharmony_ci				      struct ieee80211_sta *sta)
36062306a36Sopenharmony_ci{
36162306a36Sopenharmony_ci	int ret = -EAGAIN;
36262306a36Sopenharmony_ci
36362306a36Sopenharmony_ci	/*
36462306a36Sopenharmony_ci	 * Don't create TX aggregation sessions when in high
36562306a36Sopenharmony_ci	 * BT traffic, as they would just be disrupted by BT.
36662306a36Sopenharmony_ci	 */
36762306a36Sopenharmony_ci	if (priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH) {
36862306a36Sopenharmony_ci		IWL_DEBUG_COEX(priv,
36962306a36Sopenharmony_ci			       "BT traffic (%d), no aggregation allowed\n",
37062306a36Sopenharmony_ci			       priv->bt_traffic_load);
37162306a36Sopenharmony_ci		return ret;
37262306a36Sopenharmony_ci	}
37362306a36Sopenharmony_ci
37462306a36Sopenharmony_ci	rs_tl_get_load(lq_data, tid);
37562306a36Sopenharmony_ci
37662306a36Sopenharmony_ci	IWL_DEBUG_HT(priv, "Starting Tx agg: STA: %pM tid: %d\n",
37762306a36Sopenharmony_ci			sta->addr, tid);
37862306a36Sopenharmony_ci	ret = ieee80211_start_tx_ba_session(sta, tid, 5000);
37962306a36Sopenharmony_ci	if (ret == -EAGAIN) {
38062306a36Sopenharmony_ci		/*
38162306a36Sopenharmony_ci		 * driver and mac80211 is out of sync
38262306a36Sopenharmony_ci		 * this might be cause by reloading firmware
38362306a36Sopenharmony_ci		 * stop the tx ba session here
38462306a36Sopenharmony_ci		 */
38562306a36Sopenharmony_ci		IWL_ERR(priv, "Fail start Tx agg on tid: %d\n",
38662306a36Sopenharmony_ci			tid);
38762306a36Sopenharmony_ci		ieee80211_stop_tx_ba_session(sta, tid);
38862306a36Sopenharmony_ci	}
38962306a36Sopenharmony_ci	return ret;
39062306a36Sopenharmony_ci}
39162306a36Sopenharmony_ci
39262306a36Sopenharmony_cistatic void rs_tl_turn_on_agg(struct iwl_priv *priv, u8 tid,
39362306a36Sopenharmony_ci			      struct iwl_lq_sta *lq_data,
39462306a36Sopenharmony_ci			      struct ieee80211_sta *sta)
39562306a36Sopenharmony_ci{
39662306a36Sopenharmony_ci	if (tid < IWL_MAX_TID_COUNT)
39762306a36Sopenharmony_ci		rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
39862306a36Sopenharmony_ci	else
39962306a36Sopenharmony_ci		IWL_ERR(priv, "tid exceeds max TID count: %d/%d\n",
40062306a36Sopenharmony_ci			tid, IWL_MAX_TID_COUNT);
40162306a36Sopenharmony_ci}
40262306a36Sopenharmony_ci
40362306a36Sopenharmony_cistatic inline int get_num_of_ant_from_rate(u32 rate_n_flags)
40462306a36Sopenharmony_ci{
40562306a36Sopenharmony_ci	return !!(rate_n_flags & RATE_MCS_ANT_A_MSK) +
40662306a36Sopenharmony_ci	       !!(rate_n_flags & RATE_MCS_ANT_B_MSK) +
40762306a36Sopenharmony_ci	       !!(rate_n_flags & RATE_MCS_ANT_C_MSK);
40862306a36Sopenharmony_ci}
40962306a36Sopenharmony_ci
41062306a36Sopenharmony_ci/*
41162306a36Sopenharmony_ci * Static function to get the expected throughput from an iwl_scale_tbl_info
41262306a36Sopenharmony_ci * that wraps a NULL pointer check
41362306a36Sopenharmony_ci */
41462306a36Sopenharmony_cistatic s32 get_expected_tpt(struct iwl_scale_tbl_info *tbl, int rs_index)
41562306a36Sopenharmony_ci{
41662306a36Sopenharmony_ci	if (tbl->expected_tpt)
41762306a36Sopenharmony_ci		return tbl->expected_tpt[rs_index];
41862306a36Sopenharmony_ci	return 0;
41962306a36Sopenharmony_ci}
42062306a36Sopenharmony_ci
42162306a36Sopenharmony_ci/*
42262306a36Sopenharmony_ci * rs_collect_tx_data - Update the success/failure sliding window
42362306a36Sopenharmony_ci *
42462306a36Sopenharmony_ci * We keep a sliding window of the last 62 packets transmitted
42562306a36Sopenharmony_ci * at this rate.  window->data contains the bitmask of successful
42662306a36Sopenharmony_ci * packets.
42762306a36Sopenharmony_ci */
42862306a36Sopenharmony_cistatic int rs_collect_tx_data(struct iwl_scale_tbl_info *tbl,
42962306a36Sopenharmony_ci			      int scale_index, int attempts, int successes)
43062306a36Sopenharmony_ci{
43162306a36Sopenharmony_ci	struct iwl_rate_scale_data *window = NULL;
43262306a36Sopenharmony_ci	static const u64 mask = (((u64)1) << (IWL_RATE_MAX_WINDOW - 1));
43362306a36Sopenharmony_ci	s32 fail_count, tpt;
43462306a36Sopenharmony_ci
43562306a36Sopenharmony_ci	if (scale_index < 0 || scale_index >= IWL_RATE_COUNT)
43662306a36Sopenharmony_ci		return -EINVAL;
43762306a36Sopenharmony_ci
43862306a36Sopenharmony_ci	/* Select window for current tx bit rate */
43962306a36Sopenharmony_ci	window = &(tbl->win[scale_index]);
44062306a36Sopenharmony_ci
44162306a36Sopenharmony_ci	/* Get expected throughput */
44262306a36Sopenharmony_ci	tpt = get_expected_tpt(tbl, scale_index);
44362306a36Sopenharmony_ci
44462306a36Sopenharmony_ci	/*
44562306a36Sopenharmony_ci	 * Keep track of only the latest 62 tx frame attempts in this rate's
44662306a36Sopenharmony_ci	 * history window; anything older isn't really relevant any more.
44762306a36Sopenharmony_ci	 * If we have filled up the sliding window, drop the oldest attempt;
44862306a36Sopenharmony_ci	 * if the oldest attempt (highest bit in bitmap) shows "success",
44962306a36Sopenharmony_ci	 * subtract "1" from the success counter (this is the main reason
45062306a36Sopenharmony_ci	 * we keep these bitmaps!).
45162306a36Sopenharmony_ci	 */
45262306a36Sopenharmony_ci	while (attempts > 0) {
45362306a36Sopenharmony_ci		if (window->counter >= IWL_RATE_MAX_WINDOW) {
45462306a36Sopenharmony_ci
45562306a36Sopenharmony_ci			/* remove earliest */
45662306a36Sopenharmony_ci			window->counter = IWL_RATE_MAX_WINDOW - 1;
45762306a36Sopenharmony_ci
45862306a36Sopenharmony_ci			if (window->data & mask) {
45962306a36Sopenharmony_ci				window->data &= ~mask;
46062306a36Sopenharmony_ci				window->success_counter--;
46162306a36Sopenharmony_ci			}
46262306a36Sopenharmony_ci		}
46362306a36Sopenharmony_ci
46462306a36Sopenharmony_ci		/* Increment frames-attempted counter */
46562306a36Sopenharmony_ci		window->counter++;
46662306a36Sopenharmony_ci
46762306a36Sopenharmony_ci		/* Shift bitmap by one frame to throw away oldest history */
46862306a36Sopenharmony_ci		window->data <<= 1;
46962306a36Sopenharmony_ci
47062306a36Sopenharmony_ci		/* Mark the most recent #successes attempts as successful */
47162306a36Sopenharmony_ci		if (successes > 0) {
47262306a36Sopenharmony_ci			window->success_counter++;
47362306a36Sopenharmony_ci			window->data |= 0x1;
47462306a36Sopenharmony_ci			successes--;
47562306a36Sopenharmony_ci		}
47662306a36Sopenharmony_ci
47762306a36Sopenharmony_ci		attempts--;
47862306a36Sopenharmony_ci	}
47962306a36Sopenharmony_ci
48062306a36Sopenharmony_ci	/* Calculate current success ratio, avoid divide-by-0! */
48162306a36Sopenharmony_ci	if (window->counter > 0)
48262306a36Sopenharmony_ci		window->success_ratio = 128 * (100 * window->success_counter)
48362306a36Sopenharmony_ci					/ window->counter;
48462306a36Sopenharmony_ci	else
48562306a36Sopenharmony_ci		window->success_ratio = IWL_INVALID_VALUE;
48662306a36Sopenharmony_ci
48762306a36Sopenharmony_ci	fail_count = window->counter - window->success_counter;
48862306a36Sopenharmony_ci
48962306a36Sopenharmony_ci	/* Calculate average throughput, if we have enough history. */
49062306a36Sopenharmony_ci	if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
49162306a36Sopenharmony_ci	    (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
49262306a36Sopenharmony_ci		window->average_tpt = (window->success_ratio * tpt + 64) / 128;
49362306a36Sopenharmony_ci	else
49462306a36Sopenharmony_ci		window->average_tpt = IWL_INVALID_VALUE;
49562306a36Sopenharmony_ci
49662306a36Sopenharmony_ci	/* Tag this window as having been updated */
49762306a36Sopenharmony_ci	window->stamp = jiffies;
49862306a36Sopenharmony_ci
49962306a36Sopenharmony_ci	return 0;
50062306a36Sopenharmony_ci}
50162306a36Sopenharmony_ci
50262306a36Sopenharmony_ci/*
50362306a36Sopenharmony_ci * Fill uCode API rate_n_flags field, based on "search" or "active" table.
50462306a36Sopenharmony_ci */
50562306a36Sopenharmony_ci/* FIXME:RS:remove this function and put the flags statically in the table */
50662306a36Sopenharmony_cistatic u32 rate_n_flags_from_tbl(struct iwl_priv *priv,
50762306a36Sopenharmony_ci				 struct iwl_scale_tbl_info *tbl,
50862306a36Sopenharmony_ci				 int index, u8 use_green)
50962306a36Sopenharmony_ci{
51062306a36Sopenharmony_ci	u32 rate_n_flags = 0;
51162306a36Sopenharmony_ci
51262306a36Sopenharmony_ci	if (is_legacy(tbl->lq_type)) {
51362306a36Sopenharmony_ci		rate_n_flags = iwl_rates[index].plcp;
51462306a36Sopenharmony_ci		if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
51562306a36Sopenharmony_ci			rate_n_flags |= RATE_MCS_CCK_MSK;
51662306a36Sopenharmony_ci
51762306a36Sopenharmony_ci	} else if (is_Ht(tbl->lq_type)) {
51862306a36Sopenharmony_ci		if (index > IWL_LAST_OFDM_RATE) {
51962306a36Sopenharmony_ci			IWL_ERR(priv, "Invalid HT rate index %d\n", index);
52062306a36Sopenharmony_ci			index = IWL_LAST_OFDM_RATE;
52162306a36Sopenharmony_ci		}
52262306a36Sopenharmony_ci		rate_n_flags = RATE_MCS_HT_MSK;
52362306a36Sopenharmony_ci
52462306a36Sopenharmony_ci		if (is_siso(tbl->lq_type))
52562306a36Sopenharmony_ci			rate_n_flags |=	iwl_rates[index].plcp_siso;
52662306a36Sopenharmony_ci		else if (is_mimo2(tbl->lq_type))
52762306a36Sopenharmony_ci			rate_n_flags |=	iwl_rates[index].plcp_mimo2;
52862306a36Sopenharmony_ci		else
52962306a36Sopenharmony_ci			rate_n_flags |=	iwl_rates[index].plcp_mimo3;
53062306a36Sopenharmony_ci	} else {
53162306a36Sopenharmony_ci		IWL_ERR(priv, "Invalid tbl->lq_type %d\n", tbl->lq_type);
53262306a36Sopenharmony_ci	}
53362306a36Sopenharmony_ci
53462306a36Sopenharmony_ci	rate_n_flags |= ((tbl->ant_type << RATE_MCS_ANT_POS) &
53562306a36Sopenharmony_ci						     RATE_MCS_ANT_ABC_MSK);
53662306a36Sopenharmony_ci
53762306a36Sopenharmony_ci	if (is_Ht(tbl->lq_type)) {
53862306a36Sopenharmony_ci		if (tbl->is_ht40) {
53962306a36Sopenharmony_ci			if (tbl->is_dup)
54062306a36Sopenharmony_ci				rate_n_flags |= RATE_MCS_DUP_MSK;
54162306a36Sopenharmony_ci			else
54262306a36Sopenharmony_ci				rate_n_flags |= RATE_MCS_HT40_MSK;
54362306a36Sopenharmony_ci		}
54462306a36Sopenharmony_ci		if (tbl->is_SGI)
54562306a36Sopenharmony_ci			rate_n_flags |= RATE_MCS_SGI_MSK;
54662306a36Sopenharmony_ci
54762306a36Sopenharmony_ci		if (use_green) {
54862306a36Sopenharmony_ci			rate_n_flags |= RATE_MCS_GF_MSK;
54962306a36Sopenharmony_ci			if (is_siso(tbl->lq_type) && tbl->is_SGI) {
55062306a36Sopenharmony_ci				rate_n_flags &= ~RATE_MCS_SGI_MSK;
55162306a36Sopenharmony_ci				IWL_ERR(priv, "GF was set with SGI:SISO\n");
55262306a36Sopenharmony_ci			}
55362306a36Sopenharmony_ci		}
55462306a36Sopenharmony_ci	}
55562306a36Sopenharmony_ci	return rate_n_flags;
55662306a36Sopenharmony_ci}
55762306a36Sopenharmony_ci
55862306a36Sopenharmony_ci/*
55962306a36Sopenharmony_ci * Interpret uCode API's rate_n_flags format,
56062306a36Sopenharmony_ci * fill "search" or "active" tx mode table.
56162306a36Sopenharmony_ci */
56262306a36Sopenharmony_cistatic int rs_get_tbl_info_from_mcs(const u32 rate_n_flags,
56362306a36Sopenharmony_ci				    enum nl80211_band band,
56462306a36Sopenharmony_ci				    struct iwl_scale_tbl_info *tbl,
56562306a36Sopenharmony_ci				    int *rate_idx)
56662306a36Sopenharmony_ci{
56762306a36Sopenharmony_ci	u32 ant_msk = (rate_n_flags & RATE_MCS_ANT_ABC_MSK);
56862306a36Sopenharmony_ci	u8 num_of_ant = get_num_of_ant_from_rate(rate_n_flags);
56962306a36Sopenharmony_ci	u8 mcs;
57062306a36Sopenharmony_ci
57162306a36Sopenharmony_ci	memset(tbl, 0, sizeof(struct iwl_scale_tbl_info));
57262306a36Sopenharmony_ci	*rate_idx = iwl_hwrate_to_plcp_idx(rate_n_flags);
57362306a36Sopenharmony_ci
57462306a36Sopenharmony_ci	if (*rate_idx  == IWL_RATE_INVALID) {
57562306a36Sopenharmony_ci		*rate_idx = -1;
57662306a36Sopenharmony_ci		return -EINVAL;
57762306a36Sopenharmony_ci	}
57862306a36Sopenharmony_ci	tbl->is_SGI = 0;	/* default legacy setup */
57962306a36Sopenharmony_ci	tbl->is_ht40 = 0;
58062306a36Sopenharmony_ci	tbl->is_dup = 0;
58162306a36Sopenharmony_ci	tbl->ant_type = (ant_msk >> RATE_MCS_ANT_POS);
58262306a36Sopenharmony_ci	tbl->lq_type = LQ_NONE;
58362306a36Sopenharmony_ci	tbl->max_search = IWL_MAX_SEARCH;
58462306a36Sopenharmony_ci
58562306a36Sopenharmony_ci	/* legacy rate format */
58662306a36Sopenharmony_ci	if (!(rate_n_flags & RATE_MCS_HT_MSK)) {
58762306a36Sopenharmony_ci		if (num_of_ant == 1) {
58862306a36Sopenharmony_ci			if (band == NL80211_BAND_5GHZ)
58962306a36Sopenharmony_ci				tbl->lq_type = LQ_A;
59062306a36Sopenharmony_ci			else
59162306a36Sopenharmony_ci				tbl->lq_type = LQ_G;
59262306a36Sopenharmony_ci		}
59362306a36Sopenharmony_ci	/* HT rate format */
59462306a36Sopenharmony_ci	} else {
59562306a36Sopenharmony_ci		if (rate_n_flags & RATE_MCS_SGI_MSK)
59662306a36Sopenharmony_ci			tbl->is_SGI = 1;
59762306a36Sopenharmony_ci
59862306a36Sopenharmony_ci		if ((rate_n_flags & RATE_MCS_HT40_MSK) ||
59962306a36Sopenharmony_ci		    (rate_n_flags & RATE_MCS_DUP_MSK))
60062306a36Sopenharmony_ci			tbl->is_ht40 = 1;
60162306a36Sopenharmony_ci
60262306a36Sopenharmony_ci		if (rate_n_flags & RATE_MCS_DUP_MSK)
60362306a36Sopenharmony_ci			tbl->is_dup = 1;
60462306a36Sopenharmony_ci
60562306a36Sopenharmony_ci		mcs = rs_extract_rate(rate_n_flags);
60662306a36Sopenharmony_ci
60762306a36Sopenharmony_ci		/* SISO */
60862306a36Sopenharmony_ci		if (mcs <= IWL_RATE_SISO_60M_PLCP) {
60962306a36Sopenharmony_ci			if (num_of_ant == 1)
61062306a36Sopenharmony_ci				tbl->lq_type = LQ_SISO; /*else NONE*/
61162306a36Sopenharmony_ci		/* MIMO2 */
61262306a36Sopenharmony_ci		} else if (mcs <= IWL_RATE_MIMO2_60M_PLCP) {
61362306a36Sopenharmony_ci			if (num_of_ant == 2)
61462306a36Sopenharmony_ci				tbl->lq_type = LQ_MIMO2;
61562306a36Sopenharmony_ci		/* MIMO3 */
61662306a36Sopenharmony_ci		} else {
61762306a36Sopenharmony_ci			if (num_of_ant == 3) {
61862306a36Sopenharmony_ci				tbl->max_search = IWL_MAX_11N_MIMO3_SEARCH;
61962306a36Sopenharmony_ci				tbl->lq_type = LQ_MIMO3;
62062306a36Sopenharmony_ci			}
62162306a36Sopenharmony_ci		}
62262306a36Sopenharmony_ci	}
62362306a36Sopenharmony_ci	return 0;
62462306a36Sopenharmony_ci}
62562306a36Sopenharmony_ci
62662306a36Sopenharmony_ci/* switch to another antenna/antennas and return 1 */
62762306a36Sopenharmony_ci/* if no other valid antenna found, return 0 */
62862306a36Sopenharmony_cistatic int rs_toggle_antenna(u32 valid_ant, u32 *rate_n_flags,
62962306a36Sopenharmony_ci			     struct iwl_scale_tbl_info *tbl)
63062306a36Sopenharmony_ci{
63162306a36Sopenharmony_ci	u8 new_ant_type;
63262306a36Sopenharmony_ci
63362306a36Sopenharmony_ci	if (!tbl->ant_type || tbl->ant_type > ANT_ABC)
63462306a36Sopenharmony_ci		return 0;
63562306a36Sopenharmony_ci
63662306a36Sopenharmony_ci	if (!rs_is_valid_ant(valid_ant, tbl->ant_type))
63762306a36Sopenharmony_ci		return 0;
63862306a36Sopenharmony_ci
63962306a36Sopenharmony_ci	new_ant_type = ant_toggle_lookup[tbl->ant_type];
64062306a36Sopenharmony_ci
64162306a36Sopenharmony_ci	while ((new_ant_type != tbl->ant_type) &&
64262306a36Sopenharmony_ci	       !rs_is_valid_ant(valid_ant, new_ant_type))
64362306a36Sopenharmony_ci		new_ant_type = ant_toggle_lookup[new_ant_type];
64462306a36Sopenharmony_ci
64562306a36Sopenharmony_ci	if (new_ant_type == tbl->ant_type)
64662306a36Sopenharmony_ci		return 0;
64762306a36Sopenharmony_ci
64862306a36Sopenharmony_ci	tbl->ant_type = new_ant_type;
64962306a36Sopenharmony_ci	*rate_n_flags &= ~RATE_MCS_ANT_ABC_MSK;
65062306a36Sopenharmony_ci	*rate_n_flags |= new_ant_type << RATE_MCS_ANT_POS;
65162306a36Sopenharmony_ci	return 1;
65262306a36Sopenharmony_ci}
65362306a36Sopenharmony_ci
65462306a36Sopenharmony_ci/*
65562306a36Sopenharmony_ci * Green-field mode is valid if the station supports it and
65662306a36Sopenharmony_ci * there are no non-GF stations present in the BSS.
65762306a36Sopenharmony_ci */
65862306a36Sopenharmony_cistatic bool rs_use_green(struct ieee80211_sta *sta)
65962306a36Sopenharmony_ci{
66062306a36Sopenharmony_ci	/*
66162306a36Sopenharmony_ci	 * There's a bug somewhere in this code that causes the
66262306a36Sopenharmony_ci	 * scaling to get stuck because GF+SGI can't be combined
66362306a36Sopenharmony_ci	 * in SISO rates. Until we find that bug, disable GF, it
66462306a36Sopenharmony_ci	 * has only limited benefit and we still interoperate with
66562306a36Sopenharmony_ci	 * GF APs since we can always receive GF transmissions.
66662306a36Sopenharmony_ci	 */
66762306a36Sopenharmony_ci	return false;
66862306a36Sopenharmony_ci}
66962306a36Sopenharmony_ci
67062306a36Sopenharmony_ci/*
67162306a36Sopenharmony_ci * rs_get_supported_rates - get the available rates
67262306a36Sopenharmony_ci *
67362306a36Sopenharmony_ci * if management frame or broadcast frame only return
67462306a36Sopenharmony_ci * basic available rates.
67562306a36Sopenharmony_ci *
67662306a36Sopenharmony_ci */
67762306a36Sopenharmony_cistatic u16 rs_get_supported_rates(struct iwl_lq_sta *lq_sta,
67862306a36Sopenharmony_ci				  struct ieee80211_hdr *hdr,
67962306a36Sopenharmony_ci				  enum iwl_table_type rate_type)
68062306a36Sopenharmony_ci{
68162306a36Sopenharmony_ci	if (is_legacy(rate_type)) {
68262306a36Sopenharmony_ci		return lq_sta->active_legacy_rate;
68362306a36Sopenharmony_ci	} else {
68462306a36Sopenharmony_ci		if (is_siso(rate_type))
68562306a36Sopenharmony_ci			return lq_sta->active_siso_rate;
68662306a36Sopenharmony_ci		else if (is_mimo2(rate_type))
68762306a36Sopenharmony_ci			return lq_sta->active_mimo2_rate;
68862306a36Sopenharmony_ci		else
68962306a36Sopenharmony_ci			return lq_sta->active_mimo3_rate;
69062306a36Sopenharmony_ci	}
69162306a36Sopenharmony_ci}
69262306a36Sopenharmony_ci
69362306a36Sopenharmony_cistatic u16 rs_get_adjacent_rate(struct iwl_priv *priv, u8 index, u16 rate_mask,
69462306a36Sopenharmony_ci				int rate_type)
69562306a36Sopenharmony_ci{
69662306a36Sopenharmony_ci	u8 high = IWL_RATE_INVALID;
69762306a36Sopenharmony_ci	u8 low = IWL_RATE_INVALID;
69862306a36Sopenharmony_ci
69962306a36Sopenharmony_ci	/* 802.11A or ht walks to the next literal adjacent rate in
70062306a36Sopenharmony_ci	 * the rate table */
70162306a36Sopenharmony_ci	if (is_a_band(rate_type) || !is_legacy(rate_type)) {
70262306a36Sopenharmony_ci		int i;
70362306a36Sopenharmony_ci		u32 mask;
70462306a36Sopenharmony_ci
70562306a36Sopenharmony_ci		/* Find the previous rate that is in the rate mask */
70662306a36Sopenharmony_ci		i = index - 1;
70762306a36Sopenharmony_ci		if (i >= 0)
70862306a36Sopenharmony_ci			mask = BIT(i);
70962306a36Sopenharmony_ci
71062306a36Sopenharmony_ci		for (; i >= 0; i--, mask >>= 1) {
71162306a36Sopenharmony_ci			if (rate_mask & mask) {
71262306a36Sopenharmony_ci				low = i;
71362306a36Sopenharmony_ci				break;
71462306a36Sopenharmony_ci			}
71562306a36Sopenharmony_ci		}
71662306a36Sopenharmony_ci
71762306a36Sopenharmony_ci		/* Find the next rate that is in the rate mask */
71862306a36Sopenharmony_ci		i = index + 1;
71962306a36Sopenharmony_ci		for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
72062306a36Sopenharmony_ci			if (rate_mask & mask) {
72162306a36Sopenharmony_ci				high = i;
72262306a36Sopenharmony_ci				break;
72362306a36Sopenharmony_ci			}
72462306a36Sopenharmony_ci		}
72562306a36Sopenharmony_ci
72662306a36Sopenharmony_ci		return (high << 8) | low;
72762306a36Sopenharmony_ci	}
72862306a36Sopenharmony_ci
72962306a36Sopenharmony_ci	low = index;
73062306a36Sopenharmony_ci	while (low != IWL_RATE_INVALID) {
73162306a36Sopenharmony_ci		low = iwl_rates[low].prev_rs;
73262306a36Sopenharmony_ci		if (low == IWL_RATE_INVALID)
73362306a36Sopenharmony_ci			break;
73462306a36Sopenharmony_ci		if (rate_mask & (1 << low))
73562306a36Sopenharmony_ci			break;
73662306a36Sopenharmony_ci		IWL_DEBUG_RATE(priv, "Skipping masked lower rate: %d\n", low);
73762306a36Sopenharmony_ci	}
73862306a36Sopenharmony_ci
73962306a36Sopenharmony_ci	high = index;
74062306a36Sopenharmony_ci	while (high != IWL_RATE_INVALID) {
74162306a36Sopenharmony_ci		high = iwl_rates[high].next_rs;
74262306a36Sopenharmony_ci		if (high == IWL_RATE_INVALID)
74362306a36Sopenharmony_ci			break;
74462306a36Sopenharmony_ci		if (rate_mask & (1 << high))
74562306a36Sopenharmony_ci			break;
74662306a36Sopenharmony_ci		IWL_DEBUG_RATE(priv, "Skipping masked higher rate: %d\n", high);
74762306a36Sopenharmony_ci	}
74862306a36Sopenharmony_ci
74962306a36Sopenharmony_ci	return (high << 8) | low;
75062306a36Sopenharmony_ci}
75162306a36Sopenharmony_ci
75262306a36Sopenharmony_cistatic u32 rs_get_lower_rate(struct iwl_lq_sta *lq_sta,
75362306a36Sopenharmony_ci			     struct iwl_scale_tbl_info *tbl,
75462306a36Sopenharmony_ci			     u8 scale_index, u8 ht_possible)
75562306a36Sopenharmony_ci{
75662306a36Sopenharmony_ci	s32 low;
75762306a36Sopenharmony_ci	u16 rate_mask;
75862306a36Sopenharmony_ci	u16 high_low;
75962306a36Sopenharmony_ci	u8 switch_to_legacy = 0;
76062306a36Sopenharmony_ci	u8 is_green = lq_sta->is_green;
76162306a36Sopenharmony_ci	struct iwl_priv *priv = lq_sta->drv;
76262306a36Sopenharmony_ci
76362306a36Sopenharmony_ci	/* check if we need to switch from HT to legacy rates.
76462306a36Sopenharmony_ci	 * assumption is that mandatory rates (1Mbps or 6Mbps)
76562306a36Sopenharmony_ci	 * are always supported (spec demand) */
76662306a36Sopenharmony_ci	if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_index)) {
76762306a36Sopenharmony_ci		switch_to_legacy = 1;
76862306a36Sopenharmony_ci		scale_index = rs_ht_to_legacy[scale_index];
76962306a36Sopenharmony_ci		if (lq_sta->band == NL80211_BAND_5GHZ)
77062306a36Sopenharmony_ci			tbl->lq_type = LQ_A;
77162306a36Sopenharmony_ci		else
77262306a36Sopenharmony_ci			tbl->lq_type = LQ_G;
77362306a36Sopenharmony_ci
77462306a36Sopenharmony_ci		if (num_of_ant(tbl->ant_type) > 1)
77562306a36Sopenharmony_ci			tbl->ant_type =
77662306a36Sopenharmony_ci			    first_antenna(priv->nvm_data->valid_tx_ant);
77762306a36Sopenharmony_ci
77862306a36Sopenharmony_ci		tbl->is_ht40 = 0;
77962306a36Sopenharmony_ci		tbl->is_SGI = 0;
78062306a36Sopenharmony_ci		tbl->max_search = IWL_MAX_SEARCH;
78162306a36Sopenharmony_ci	}
78262306a36Sopenharmony_ci
78362306a36Sopenharmony_ci	rate_mask = rs_get_supported_rates(lq_sta, NULL, tbl->lq_type);
78462306a36Sopenharmony_ci
78562306a36Sopenharmony_ci	/* Mask with station rate restriction */
78662306a36Sopenharmony_ci	if (is_legacy(tbl->lq_type)) {
78762306a36Sopenharmony_ci		/* supp_rates has no CCK bits in A mode */
78862306a36Sopenharmony_ci		if (lq_sta->band == NL80211_BAND_5GHZ)
78962306a36Sopenharmony_ci			rate_mask  = (u16)(rate_mask &
79062306a36Sopenharmony_ci			   (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
79162306a36Sopenharmony_ci		else
79262306a36Sopenharmony_ci			rate_mask = (u16)(rate_mask & lq_sta->supp_rates);
79362306a36Sopenharmony_ci	}
79462306a36Sopenharmony_ci
79562306a36Sopenharmony_ci	/* If we switched from HT to legacy, check current rate */
79662306a36Sopenharmony_ci	if (switch_to_legacy && (rate_mask & (1 << scale_index))) {
79762306a36Sopenharmony_ci		low = scale_index;
79862306a36Sopenharmony_ci		goto out;
79962306a36Sopenharmony_ci	}
80062306a36Sopenharmony_ci
80162306a36Sopenharmony_ci	high_low = rs_get_adjacent_rate(lq_sta->drv, scale_index, rate_mask,
80262306a36Sopenharmony_ci					tbl->lq_type);
80362306a36Sopenharmony_ci	low = high_low & 0xff;
80462306a36Sopenharmony_ci
80562306a36Sopenharmony_ci	if (low == IWL_RATE_INVALID)
80662306a36Sopenharmony_ci		low = scale_index;
80762306a36Sopenharmony_ci
80862306a36Sopenharmony_ciout:
80962306a36Sopenharmony_ci	return rate_n_flags_from_tbl(lq_sta->drv, tbl, low, is_green);
81062306a36Sopenharmony_ci}
81162306a36Sopenharmony_ci
81262306a36Sopenharmony_ci/*
81362306a36Sopenharmony_ci * Simple function to compare two rate scale table types
81462306a36Sopenharmony_ci */
81562306a36Sopenharmony_cistatic bool table_type_matches(struct iwl_scale_tbl_info *a,
81662306a36Sopenharmony_ci			       struct iwl_scale_tbl_info *b)
81762306a36Sopenharmony_ci{
81862306a36Sopenharmony_ci	return (a->lq_type == b->lq_type) && (a->ant_type == b->ant_type) &&
81962306a36Sopenharmony_ci		(a->is_SGI == b->is_SGI);
82062306a36Sopenharmony_ci}
82162306a36Sopenharmony_ci
82262306a36Sopenharmony_cistatic void rs_bt_update_lq(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
82362306a36Sopenharmony_ci			    struct iwl_lq_sta *lq_sta)
82462306a36Sopenharmony_ci{
82562306a36Sopenharmony_ci	struct iwl_scale_tbl_info *tbl;
82662306a36Sopenharmony_ci	bool full_concurrent = priv->bt_full_concurrent;
82762306a36Sopenharmony_ci
82862306a36Sopenharmony_ci	if ((priv->bt_traffic_load != priv->last_bt_traffic_load) ||
82962306a36Sopenharmony_ci	    (priv->bt_full_concurrent != full_concurrent)) {
83062306a36Sopenharmony_ci		priv->bt_full_concurrent = full_concurrent;
83162306a36Sopenharmony_ci		priv->last_bt_traffic_load = priv->bt_traffic_load;
83262306a36Sopenharmony_ci
83362306a36Sopenharmony_ci		/* Update uCode's rate table. */
83462306a36Sopenharmony_ci		tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
83562306a36Sopenharmony_ci		rs_fill_link_cmd(priv, lq_sta, tbl->current_rate);
83662306a36Sopenharmony_ci		iwl_send_lq_cmd(priv, ctx, &lq_sta->lq, CMD_ASYNC, false);
83762306a36Sopenharmony_ci
83862306a36Sopenharmony_ci		queue_work(priv->workqueue, &priv->bt_full_concurrency);
83962306a36Sopenharmony_ci	}
84062306a36Sopenharmony_ci}
84162306a36Sopenharmony_ci
84262306a36Sopenharmony_ci/*
84362306a36Sopenharmony_ci * mac80211 sends us Tx status
84462306a36Sopenharmony_ci */
84562306a36Sopenharmony_cistatic void rs_tx_status(void *priv_r, struct ieee80211_supported_band *sband,
84662306a36Sopenharmony_ci			 struct ieee80211_sta *sta, void *priv_sta,
84762306a36Sopenharmony_ci			 struct sk_buff *skb)
84862306a36Sopenharmony_ci{
84962306a36Sopenharmony_ci	int legacy_success;
85062306a36Sopenharmony_ci	int retries;
85162306a36Sopenharmony_ci	int rs_index, mac_index, i;
85262306a36Sopenharmony_ci	struct iwl_lq_sta *lq_sta = priv_sta;
85362306a36Sopenharmony_ci	struct iwl_link_quality_cmd *table;
85462306a36Sopenharmony_ci	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
85562306a36Sopenharmony_ci	struct iwl_op_mode *op_mode = (struct iwl_op_mode *)priv_r;
85662306a36Sopenharmony_ci	struct iwl_priv *priv = IWL_OP_MODE_GET_DVM(op_mode);
85762306a36Sopenharmony_ci	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
85862306a36Sopenharmony_ci	enum mac80211_rate_control_flags mac_flags;
85962306a36Sopenharmony_ci	u32 tx_rate;
86062306a36Sopenharmony_ci	struct iwl_scale_tbl_info tbl_type;
86162306a36Sopenharmony_ci	struct iwl_scale_tbl_info *curr_tbl, *other_tbl, *tmp_tbl;
86262306a36Sopenharmony_ci	struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
86362306a36Sopenharmony_ci	struct iwl_rxon_context *ctx = sta_priv->ctx;
86462306a36Sopenharmony_ci
86562306a36Sopenharmony_ci	IWL_DEBUG_RATE_LIMIT(priv, "get frame ack response, update rate scale window\n");
86662306a36Sopenharmony_ci
86762306a36Sopenharmony_ci	/* Treat uninitialized rate scaling data same as non-existing. */
86862306a36Sopenharmony_ci	if (!lq_sta) {
86962306a36Sopenharmony_ci		IWL_DEBUG_RATE(priv, "Station rate scaling not created yet.\n");
87062306a36Sopenharmony_ci		return;
87162306a36Sopenharmony_ci	} else if (!lq_sta->drv) {
87262306a36Sopenharmony_ci		IWL_DEBUG_RATE(priv, "Rate scaling not initialized yet.\n");
87362306a36Sopenharmony_ci		return;
87462306a36Sopenharmony_ci	}
87562306a36Sopenharmony_ci
87662306a36Sopenharmony_ci	if (!ieee80211_is_data(hdr->frame_control) ||
87762306a36Sopenharmony_ci	    info->flags & IEEE80211_TX_CTL_NO_ACK)
87862306a36Sopenharmony_ci		return;
87962306a36Sopenharmony_ci
88062306a36Sopenharmony_ci	/* This packet was aggregated but doesn't carry status info */
88162306a36Sopenharmony_ci	if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
88262306a36Sopenharmony_ci	    !(info->flags & IEEE80211_TX_STAT_AMPDU))
88362306a36Sopenharmony_ci		return;
88462306a36Sopenharmony_ci
88562306a36Sopenharmony_ci	/*
88662306a36Sopenharmony_ci	 * Ignore this Tx frame response if its initial rate doesn't match
88762306a36Sopenharmony_ci	 * that of latest Link Quality command.  There may be stragglers
88862306a36Sopenharmony_ci	 * from a previous Link Quality command, but we're no longer interested
88962306a36Sopenharmony_ci	 * in those; they're either from the "active" mode while we're trying
89062306a36Sopenharmony_ci	 * to check "search" mode, or a prior "search" mode after we've moved
89162306a36Sopenharmony_ci	 * to a new "search" mode (which might become the new "active" mode).
89262306a36Sopenharmony_ci	 */
89362306a36Sopenharmony_ci	table = &lq_sta->lq;
89462306a36Sopenharmony_ci	tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
89562306a36Sopenharmony_ci	rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
89662306a36Sopenharmony_ci	if (priv->band == NL80211_BAND_5GHZ)
89762306a36Sopenharmony_ci		rs_index -= IWL_FIRST_OFDM_RATE;
89862306a36Sopenharmony_ci	mac_flags = info->status.rates[0].flags;
89962306a36Sopenharmony_ci	mac_index = info->status.rates[0].idx;
90062306a36Sopenharmony_ci	/* For HT packets, map MCS to PLCP */
90162306a36Sopenharmony_ci	if (mac_flags & IEEE80211_TX_RC_MCS) {
90262306a36Sopenharmony_ci		mac_index &= RATE_MCS_CODE_MSK;	/* Remove # of streams */
90362306a36Sopenharmony_ci		if (mac_index >= (IWL_RATE_9M_INDEX - IWL_FIRST_OFDM_RATE))
90462306a36Sopenharmony_ci			mac_index++;
90562306a36Sopenharmony_ci		/*
90662306a36Sopenharmony_ci		 * mac80211 HT index is always zero-indexed; we need to move
90762306a36Sopenharmony_ci		 * HT OFDM rates after CCK rates in 2.4 GHz band
90862306a36Sopenharmony_ci		 */
90962306a36Sopenharmony_ci		if (priv->band == NL80211_BAND_2GHZ)
91062306a36Sopenharmony_ci			mac_index += IWL_FIRST_OFDM_RATE;
91162306a36Sopenharmony_ci	}
91262306a36Sopenharmony_ci	/* Here we actually compare this rate to the latest LQ command */
91362306a36Sopenharmony_ci	if ((mac_index < 0) ||
91462306a36Sopenharmony_ci	    (tbl_type.is_SGI != !!(mac_flags & IEEE80211_TX_RC_SHORT_GI)) ||
91562306a36Sopenharmony_ci	    (tbl_type.is_ht40 != !!(mac_flags & IEEE80211_TX_RC_40_MHZ_WIDTH)) ||
91662306a36Sopenharmony_ci	    (tbl_type.is_dup != !!(mac_flags & IEEE80211_TX_RC_DUP_DATA)) ||
91762306a36Sopenharmony_ci	    (tbl_type.ant_type != info->status.antenna) ||
91862306a36Sopenharmony_ci	    (!!(tx_rate & RATE_MCS_HT_MSK) != !!(mac_flags & IEEE80211_TX_RC_MCS)) ||
91962306a36Sopenharmony_ci	    (!!(tx_rate & RATE_MCS_GF_MSK) != !!(mac_flags & IEEE80211_TX_RC_GREEN_FIELD)) ||
92062306a36Sopenharmony_ci	    (rs_index != mac_index)) {
92162306a36Sopenharmony_ci		IWL_DEBUG_RATE(priv, "initial rate %d does not match %d (0x%x)\n", mac_index, rs_index, tx_rate);
92262306a36Sopenharmony_ci		/*
92362306a36Sopenharmony_ci		 * Since rates mis-match, the last LQ command may have failed.
92462306a36Sopenharmony_ci		 * After IWL_MISSED_RATE_MAX mis-matches, resync the uCode with
92562306a36Sopenharmony_ci		 * ... driver.
92662306a36Sopenharmony_ci		 */
92762306a36Sopenharmony_ci		lq_sta->missed_rate_counter++;
92862306a36Sopenharmony_ci		if (lq_sta->missed_rate_counter > IWL_MISSED_RATE_MAX) {
92962306a36Sopenharmony_ci			lq_sta->missed_rate_counter = 0;
93062306a36Sopenharmony_ci			iwl_send_lq_cmd(priv, ctx, &lq_sta->lq, CMD_ASYNC, false);
93162306a36Sopenharmony_ci		}
93262306a36Sopenharmony_ci		/* Regardless, ignore this status info for outdated rate */
93362306a36Sopenharmony_ci		return;
93462306a36Sopenharmony_ci	} else
93562306a36Sopenharmony_ci		/* Rate did match, so reset the missed_rate_counter */
93662306a36Sopenharmony_ci		lq_sta->missed_rate_counter = 0;
93762306a36Sopenharmony_ci
93862306a36Sopenharmony_ci	/* Figure out if rate scale algorithm is in active or search table */
93962306a36Sopenharmony_ci	if (table_type_matches(&tbl_type,
94062306a36Sopenharmony_ci				&(lq_sta->lq_info[lq_sta->active_tbl]))) {
94162306a36Sopenharmony_ci		curr_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
94262306a36Sopenharmony_ci		other_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
94362306a36Sopenharmony_ci	} else if (table_type_matches(&tbl_type,
94462306a36Sopenharmony_ci				&lq_sta->lq_info[1 - lq_sta->active_tbl])) {
94562306a36Sopenharmony_ci		curr_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
94662306a36Sopenharmony_ci		other_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
94762306a36Sopenharmony_ci	} else {
94862306a36Sopenharmony_ci		IWL_DEBUG_RATE(priv, "Neither active nor search matches tx rate\n");
94962306a36Sopenharmony_ci		tmp_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
95062306a36Sopenharmony_ci		IWL_DEBUG_RATE(priv, "active- lq:%x, ant:%x, SGI:%d\n",
95162306a36Sopenharmony_ci			tmp_tbl->lq_type, tmp_tbl->ant_type, tmp_tbl->is_SGI);
95262306a36Sopenharmony_ci		tmp_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
95362306a36Sopenharmony_ci		IWL_DEBUG_RATE(priv, "search- lq:%x, ant:%x, SGI:%d\n",
95462306a36Sopenharmony_ci			tmp_tbl->lq_type, tmp_tbl->ant_type, tmp_tbl->is_SGI);
95562306a36Sopenharmony_ci		IWL_DEBUG_RATE(priv, "actual- lq:%x, ant:%x, SGI:%d\n",
95662306a36Sopenharmony_ci			tbl_type.lq_type, tbl_type.ant_type, tbl_type.is_SGI);
95762306a36Sopenharmony_ci		/*
95862306a36Sopenharmony_ci		 * no matching table found, let's by-pass the data collection
95962306a36Sopenharmony_ci		 * and continue to perform rate scale to find the rate table
96062306a36Sopenharmony_ci		 */
96162306a36Sopenharmony_ci		rs_stay_in_table(lq_sta, true);
96262306a36Sopenharmony_ci		goto done;
96362306a36Sopenharmony_ci	}
96462306a36Sopenharmony_ci
96562306a36Sopenharmony_ci	/*
96662306a36Sopenharmony_ci	 * Updating the frame history depends on whether packets were
96762306a36Sopenharmony_ci	 * aggregated.
96862306a36Sopenharmony_ci	 *
96962306a36Sopenharmony_ci	 * For aggregation, all packets were transmitted at the same rate, the
97062306a36Sopenharmony_ci	 * first index into rate scale table.
97162306a36Sopenharmony_ci	 */
97262306a36Sopenharmony_ci	if (info->flags & IEEE80211_TX_STAT_AMPDU) {
97362306a36Sopenharmony_ci		tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
97462306a36Sopenharmony_ci		rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type,
97562306a36Sopenharmony_ci				&rs_index);
97662306a36Sopenharmony_ci		rs_collect_tx_data(curr_tbl, rs_index,
97762306a36Sopenharmony_ci				   info->status.ampdu_len,
97862306a36Sopenharmony_ci				   info->status.ampdu_ack_len);
97962306a36Sopenharmony_ci
98062306a36Sopenharmony_ci		/* Update success/fail counts if not searching for new mode */
98162306a36Sopenharmony_ci		if (lq_sta->stay_in_tbl) {
98262306a36Sopenharmony_ci			lq_sta->total_success += info->status.ampdu_ack_len;
98362306a36Sopenharmony_ci			lq_sta->total_failed += (info->status.ampdu_len -
98462306a36Sopenharmony_ci					info->status.ampdu_ack_len);
98562306a36Sopenharmony_ci		}
98662306a36Sopenharmony_ci	} else {
98762306a36Sopenharmony_ci	/*
98862306a36Sopenharmony_ci	 * For legacy, update frame history with for each Tx retry.
98962306a36Sopenharmony_ci	 */
99062306a36Sopenharmony_ci		retries = info->status.rates[0].count - 1;
99162306a36Sopenharmony_ci		/* HW doesn't send more than 15 retries */
99262306a36Sopenharmony_ci		retries = min(retries, 15);
99362306a36Sopenharmony_ci
99462306a36Sopenharmony_ci		/* The last transmission may have been successful */
99562306a36Sopenharmony_ci		legacy_success = !!(info->flags & IEEE80211_TX_STAT_ACK);
99662306a36Sopenharmony_ci		/* Collect data for each rate used during failed TX attempts */
99762306a36Sopenharmony_ci		for (i = 0; i <= retries; ++i) {
99862306a36Sopenharmony_ci			tx_rate = le32_to_cpu(table->rs_table[i].rate_n_flags);
99962306a36Sopenharmony_ci			rs_get_tbl_info_from_mcs(tx_rate, priv->band,
100062306a36Sopenharmony_ci					&tbl_type, &rs_index);
100162306a36Sopenharmony_ci			/*
100262306a36Sopenharmony_ci			 * Only collect stats if retried rate is in the same RS
100362306a36Sopenharmony_ci			 * table as active/search.
100462306a36Sopenharmony_ci			 */
100562306a36Sopenharmony_ci			if (table_type_matches(&tbl_type, curr_tbl))
100662306a36Sopenharmony_ci				tmp_tbl = curr_tbl;
100762306a36Sopenharmony_ci			else if (table_type_matches(&tbl_type, other_tbl))
100862306a36Sopenharmony_ci				tmp_tbl = other_tbl;
100962306a36Sopenharmony_ci			else
101062306a36Sopenharmony_ci				continue;
101162306a36Sopenharmony_ci			rs_collect_tx_data(tmp_tbl, rs_index, 1,
101262306a36Sopenharmony_ci					   i < retries ? 0 : legacy_success);
101362306a36Sopenharmony_ci		}
101462306a36Sopenharmony_ci
101562306a36Sopenharmony_ci		/* Update success/fail counts if not searching for new mode */
101662306a36Sopenharmony_ci		if (lq_sta->stay_in_tbl) {
101762306a36Sopenharmony_ci			lq_sta->total_success += legacy_success;
101862306a36Sopenharmony_ci			lq_sta->total_failed += retries + (1 - legacy_success);
101962306a36Sopenharmony_ci		}
102062306a36Sopenharmony_ci	}
102162306a36Sopenharmony_ci	/* The last TX rate is cached in lq_sta; it's set in if/else above */
102262306a36Sopenharmony_ci	lq_sta->last_rate_n_flags = tx_rate;
102362306a36Sopenharmony_cidone:
102462306a36Sopenharmony_ci	/* See if there's a better rate or modulation mode to try. */
102562306a36Sopenharmony_ci	if (sta && sta->deflink.supp_rates[sband->band])
102662306a36Sopenharmony_ci		rs_rate_scale_perform(priv, skb, sta, lq_sta);
102762306a36Sopenharmony_ci
102862306a36Sopenharmony_ci	if (priv->lib->bt_params && priv->lib->bt_params->advanced_bt_coexist)
102962306a36Sopenharmony_ci		rs_bt_update_lq(priv, ctx, lq_sta);
103062306a36Sopenharmony_ci}
103162306a36Sopenharmony_ci
103262306a36Sopenharmony_ci/*
103362306a36Sopenharmony_ci * Begin a period of staying with a selected modulation mode.
103462306a36Sopenharmony_ci * Set "stay_in_tbl" flag to prevent any mode switches.
103562306a36Sopenharmony_ci * Set frame tx success limits according to legacy vs. high-throughput,
103662306a36Sopenharmony_ci * and reset overall (spanning all rates) tx success history statistics.
103762306a36Sopenharmony_ci * These control how long we stay using same modulation mode before
103862306a36Sopenharmony_ci * searching for a new mode.
103962306a36Sopenharmony_ci */
104062306a36Sopenharmony_cistatic void rs_set_stay_in_table(struct iwl_priv *priv, u8 is_legacy,
104162306a36Sopenharmony_ci				 struct iwl_lq_sta *lq_sta)
104262306a36Sopenharmony_ci{
104362306a36Sopenharmony_ci	IWL_DEBUG_RATE(priv, "we are staying in the same table\n");
104462306a36Sopenharmony_ci	lq_sta->stay_in_tbl = 1;	/* only place this gets set */
104562306a36Sopenharmony_ci	if (is_legacy) {
104662306a36Sopenharmony_ci		lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT;
104762306a36Sopenharmony_ci		lq_sta->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
104862306a36Sopenharmony_ci		lq_sta->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
104962306a36Sopenharmony_ci	} else {
105062306a36Sopenharmony_ci		lq_sta->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
105162306a36Sopenharmony_ci		lq_sta->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
105262306a36Sopenharmony_ci		lq_sta->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
105362306a36Sopenharmony_ci	}
105462306a36Sopenharmony_ci	lq_sta->table_count = 0;
105562306a36Sopenharmony_ci	lq_sta->total_failed = 0;
105662306a36Sopenharmony_ci	lq_sta->total_success = 0;
105762306a36Sopenharmony_ci	lq_sta->flush_timer = jiffies;
105862306a36Sopenharmony_ci	lq_sta->action_counter = 0;
105962306a36Sopenharmony_ci}
106062306a36Sopenharmony_ci
106162306a36Sopenharmony_ci/*
106262306a36Sopenharmony_ci * Find correct throughput table for given mode of modulation
106362306a36Sopenharmony_ci */
106462306a36Sopenharmony_cistatic void rs_set_expected_tpt_table(struct iwl_lq_sta *lq_sta,
106562306a36Sopenharmony_ci				      struct iwl_scale_tbl_info *tbl)
106662306a36Sopenharmony_ci{
106762306a36Sopenharmony_ci	/* Used to choose among HT tables */
106862306a36Sopenharmony_ci	const u16 (*ht_tbl_pointer)[IWL_RATE_COUNT];
106962306a36Sopenharmony_ci
107062306a36Sopenharmony_ci	/* Check for invalid LQ type */
107162306a36Sopenharmony_ci	if (WARN_ON_ONCE(!is_legacy(tbl->lq_type) && !is_Ht(tbl->lq_type))) {
107262306a36Sopenharmony_ci		tbl->expected_tpt = expected_tpt_legacy;
107362306a36Sopenharmony_ci		return;
107462306a36Sopenharmony_ci	}
107562306a36Sopenharmony_ci
107662306a36Sopenharmony_ci	/* Legacy rates have only one table */
107762306a36Sopenharmony_ci	if (is_legacy(tbl->lq_type)) {
107862306a36Sopenharmony_ci		tbl->expected_tpt = expected_tpt_legacy;
107962306a36Sopenharmony_ci		return;
108062306a36Sopenharmony_ci	}
108162306a36Sopenharmony_ci
108262306a36Sopenharmony_ci	/* Choose among many HT tables depending on number of streams
108362306a36Sopenharmony_ci	 * (SISO/MIMO2/MIMO3), channel width (20/40), SGI, and aggregation
108462306a36Sopenharmony_ci	 * status */
108562306a36Sopenharmony_ci	if (is_siso(tbl->lq_type) && (!tbl->is_ht40 || lq_sta->is_dup))
108662306a36Sopenharmony_ci		ht_tbl_pointer = expected_tpt_siso20MHz;
108762306a36Sopenharmony_ci	else if (is_siso(tbl->lq_type))
108862306a36Sopenharmony_ci		ht_tbl_pointer = expected_tpt_siso40MHz;
108962306a36Sopenharmony_ci	else if (is_mimo2(tbl->lq_type) && (!tbl->is_ht40 || lq_sta->is_dup))
109062306a36Sopenharmony_ci		ht_tbl_pointer = expected_tpt_mimo2_20MHz;
109162306a36Sopenharmony_ci	else if (is_mimo2(tbl->lq_type))
109262306a36Sopenharmony_ci		ht_tbl_pointer = expected_tpt_mimo2_40MHz;
109362306a36Sopenharmony_ci	else if (is_mimo3(tbl->lq_type) && (!tbl->is_ht40 || lq_sta->is_dup))
109462306a36Sopenharmony_ci		ht_tbl_pointer = expected_tpt_mimo3_20MHz;
109562306a36Sopenharmony_ci	else /* if (is_mimo3(tbl->lq_type)) <-- must be true */
109662306a36Sopenharmony_ci		ht_tbl_pointer = expected_tpt_mimo3_40MHz;
109762306a36Sopenharmony_ci
109862306a36Sopenharmony_ci	if (!tbl->is_SGI && !lq_sta->is_agg)		/* Normal */
109962306a36Sopenharmony_ci		tbl->expected_tpt = ht_tbl_pointer[0];
110062306a36Sopenharmony_ci	else if (tbl->is_SGI && !lq_sta->is_agg)	/* SGI */
110162306a36Sopenharmony_ci		tbl->expected_tpt = ht_tbl_pointer[1];
110262306a36Sopenharmony_ci	else if (!tbl->is_SGI && lq_sta->is_agg)	/* AGG */
110362306a36Sopenharmony_ci		tbl->expected_tpt = ht_tbl_pointer[2];
110462306a36Sopenharmony_ci	else						/* AGG+SGI */
110562306a36Sopenharmony_ci		tbl->expected_tpt = ht_tbl_pointer[3];
110662306a36Sopenharmony_ci}
110762306a36Sopenharmony_ci
110862306a36Sopenharmony_ci/*
110962306a36Sopenharmony_ci * Find starting rate for new "search" high-throughput mode of modulation.
111062306a36Sopenharmony_ci * Goal is to find lowest expected rate (under perfect conditions) that is
111162306a36Sopenharmony_ci * above the current measured throughput of "active" mode, to give new mode
111262306a36Sopenharmony_ci * a fair chance to prove itself without too many challenges.
111362306a36Sopenharmony_ci *
111462306a36Sopenharmony_ci * This gets called when transitioning to more aggressive modulation
111562306a36Sopenharmony_ci * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
111662306a36Sopenharmony_ci * (i.e. MIMO to SISO).  When moving to MIMO, bit rate will typically need
111762306a36Sopenharmony_ci * to decrease to match "active" throughput.  When moving from MIMO to SISO,
111862306a36Sopenharmony_ci * bit rate will typically need to increase, but not if performance was bad.
111962306a36Sopenharmony_ci */
112062306a36Sopenharmony_cistatic s32 rs_get_best_rate(struct iwl_priv *priv,
112162306a36Sopenharmony_ci			    struct iwl_lq_sta *lq_sta,
112262306a36Sopenharmony_ci			    struct iwl_scale_tbl_info *tbl,	/* "search" */
112362306a36Sopenharmony_ci			    u16 rate_mask, s8 index)
112462306a36Sopenharmony_ci{
112562306a36Sopenharmony_ci	/* "active" values */
112662306a36Sopenharmony_ci	struct iwl_scale_tbl_info *active_tbl =
112762306a36Sopenharmony_ci	    &(lq_sta->lq_info[lq_sta->active_tbl]);
112862306a36Sopenharmony_ci	s32 active_sr = active_tbl->win[index].success_ratio;
112962306a36Sopenharmony_ci	s32 active_tpt = active_tbl->expected_tpt[index];
113062306a36Sopenharmony_ci	/* expected "search" throughput */
113162306a36Sopenharmony_ci	const u16 *tpt_tbl = tbl->expected_tpt;
113262306a36Sopenharmony_ci
113362306a36Sopenharmony_ci	s32 new_rate, high, low, start_hi;
113462306a36Sopenharmony_ci	u16 high_low;
113562306a36Sopenharmony_ci	s8 rate = index;
113662306a36Sopenharmony_ci
113762306a36Sopenharmony_ci	new_rate = high = low = start_hi = IWL_RATE_INVALID;
113862306a36Sopenharmony_ci
113962306a36Sopenharmony_ci	for (; ;) {
114062306a36Sopenharmony_ci		high_low = rs_get_adjacent_rate(priv, rate, rate_mask,
114162306a36Sopenharmony_ci						tbl->lq_type);
114262306a36Sopenharmony_ci
114362306a36Sopenharmony_ci		low = high_low & 0xff;
114462306a36Sopenharmony_ci		high = (high_low >> 8) & 0xff;
114562306a36Sopenharmony_ci
114662306a36Sopenharmony_ci		/*
114762306a36Sopenharmony_ci		 * Lower the "search" bit rate, to give new "search" mode
114862306a36Sopenharmony_ci		 * approximately the same throughput as "active" if:
114962306a36Sopenharmony_ci		 *
115062306a36Sopenharmony_ci		 * 1) "Active" mode has been working modestly well (but not
115162306a36Sopenharmony_ci		 *    great), and expected "search" throughput (under perfect
115262306a36Sopenharmony_ci		 *    conditions) at candidate rate is above the actual
115362306a36Sopenharmony_ci		 *    measured "active" throughput (but less than expected
115462306a36Sopenharmony_ci		 *    "active" throughput under perfect conditions).
115562306a36Sopenharmony_ci		 * OR
115662306a36Sopenharmony_ci		 * 2) "Active" mode has been working perfectly or very well
115762306a36Sopenharmony_ci		 *    and expected "search" throughput (under perfect
115862306a36Sopenharmony_ci		 *    conditions) at candidate rate is above expected
115962306a36Sopenharmony_ci		 *    "active" throughput (under perfect conditions).
116062306a36Sopenharmony_ci		 */
116162306a36Sopenharmony_ci		if ((((100 * tpt_tbl[rate]) > lq_sta->last_tpt) &&
116262306a36Sopenharmony_ci		     ((active_sr > IWL_RATE_DECREASE_TH) &&
116362306a36Sopenharmony_ci		      (active_sr <= IWL_RATE_HIGH_TH) &&
116462306a36Sopenharmony_ci		      (tpt_tbl[rate] <= active_tpt))) ||
116562306a36Sopenharmony_ci		    ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
116662306a36Sopenharmony_ci		     (tpt_tbl[rate] > active_tpt))) {
116762306a36Sopenharmony_ci
116862306a36Sopenharmony_ci			/* (2nd or later pass)
116962306a36Sopenharmony_ci			 * If we've already tried to raise the rate, and are
117062306a36Sopenharmony_ci			 * now trying to lower it, use the higher rate. */
117162306a36Sopenharmony_ci			if (start_hi != IWL_RATE_INVALID) {
117262306a36Sopenharmony_ci				new_rate = start_hi;
117362306a36Sopenharmony_ci				break;
117462306a36Sopenharmony_ci			}
117562306a36Sopenharmony_ci
117662306a36Sopenharmony_ci			new_rate = rate;
117762306a36Sopenharmony_ci
117862306a36Sopenharmony_ci			/* Loop again with lower rate */
117962306a36Sopenharmony_ci			if (low != IWL_RATE_INVALID)
118062306a36Sopenharmony_ci				rate = low;
118162306a36Sopenharmony_ci
118262306a36Sopenharmony_ci			/* Lower rate not available, use the original */
118362306a36Sopenharmony_ci			else
118462306a36Sopenharmony_ci				break;
118562306a36Sopenharmony_ci
118662306a36Sopenharmony_ci		/* Else try to raise the "search" rate to match "active" */
118762306a36Sopenharmony_ci		} else {
118862306a36Sopenharmony_ci			/* (2nd or later pass)
118962306a36Sopenharmony_ci			 * If we've already tried to lower the rate, and are
119062306a36Sopenharmony_ci			 * now trying to raise it, use the lower rate. */
119162306a36Sopenharmony_ci			if (new_rate != IWL_RATE_INVALID)
119262306a36Sopenharmony_ci				break;
119362306a36Sopenharmony_ci
119462306a36Sopenharmony_ci			/* Loop again with higher rate */
119562306a36Sopenharmony_ci			else if (high != IWL_RATE_INVALID) {
119662306a36Sopenharmony_ci				start_hi = high;
119762306a36Sopenharmony_ci				rate = high;
119862306a36Sopenharmony_ci
119962306a36Sopenharmony_ci			/* Higher rate not available, use the original */
120062306a36Sopenharmony_ci			} else {
120162306a36Sopenharmony_ci				new_rate = rate;
120262306a36Sopenharmony_ci				break;
120362306a36Sopenharmony_ci			}
120462306a36Sopenharmony_ci		}
120562306a36Sopenharmony_ci	}
120662306a36Sopenharmony_ci
120762306a36Sopenharmony_ci	return new_rate;
120862306a36Sopenharmony_ci}
120962306a36Sopenharmony_ci
121062306a36Sopenharmony_ci/*
121162306a36Sopenharmony_ci * Set up search table for MIMO2
121262306a36Sopenharmony_ci */
121362306a36Sopenharmony_cistatic int rs_switch_to_mimo2(struct iwl_priv *priv,
121462306a36Sopenharmony_ci			     struct iwl_lq_sta *lq_sta,
121562306a36Sopenharmony_ci			     struct ieee80211_conf *conf,
121662306a36Sopenharmony_ci			     struct ieee80211_sta *sta,
121762306a36Sopenharmony_ci			     struct iwl_scale_tbl_info *tbl, int index)
121862306a36Sopenharmony_ci{
121962306a36Sopenharmony_ci	u16 rate_mask;
122062306a36Sopenharmony_ci	s32 rate;
122162306a36Sopenharmony_ci	s8 is_green = lq_sta->is_green;
122262306a36Sopenharmony_ci	struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
122362306a36Sopenharmony_ci	struct iwl_rxon_context *ctx = sta_priv->ctx;
122462306a36Sopenharmony_ci
122562306a36Sopenharmony_ci	if (!conf_is_ht(conf) || !sta->deflink.ht_cap.ht_supported)
122662306a36Sopenharmony_ci		return -1;
122762306a36Sopenharmony_ci
122862306a36Sopenharmony_ci	if (sta->deflink.smps_mode == IEEE80211_SMPS_STATIC)
122962306a36Sopenharmony_ci		return -1;
123062306a36Sopenharmony_ci
123162306a36Sopenharmony_ci	/* Need both Tx chains/antennas to support MIMO */
123262306a36Sopenharmony_ci	if (priv->hw_params.tx_chains_num < 2)
123362306a36Sopenharmony_ci		return -1;
123462306a36Sopenharmony_ci
123562306a36Sopenharmony_ci	IWL_DEBUG_RATE(priv, "LQ: try to switch to MIMO2\n");
123662306a36Sopenharmony_ci
123762306a36Sopenharmony_ci	tbl->lq_type = LQ_MIMO2;
123862306a36Sopenharmony_ci	tbl->is_dup = lq_sta->is_dup;
123962306a36Sopenharmony_ci	tbl->action = 0;
124062306a36Sopenharmony_ci	tbl->max_search = IWL_MAX_SEARCH;
124162306a36Sopenharmony_ci	rate_mask = lq_sta->active_mimo2_rate;
124262306a36Sopenharmony_ci
124362306a36Sopenharmony_ci	if (iwl_is_ht40_tx_allowed(priv, ctx, sta))
124462306a36Sopenharmony_ci		tbl->is_ht40 = 1;
124562306a36Sopenharmony_ci	else
124662306a36Sopenharmony_ci		tbl->is_ht40 = 0;
124762306a36Sopenharmony_ci
124862306a36Sopenharmony_ci	rs_set_expected_tpt_table(lq_sta, tbl);
124962306a36Sopenharmony_ci
125062306a36Sopenharmony_ci	rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
125162306a36Sopenharmony_ci
125262306a36Sopenharmony_ci	IWL_DEBUG_RATE(priv, "LQ: MIMO2 best rate %d mask %X\n", rate, rate_mask);
125362306a36Sopenharmony_ci	if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
125462306a36Sopenharmony_ci		IWL_DEBUG_RATE(priv, "Can't switch with index %d rate mask %x\n",
125562306a36Sopenharmony_ci						rate, rate_mask);
125662306a36Sopenharmony_ci		return -1;
125762306a36Sopenharmony_ci	}
125862306a36Sopenharmony_ci	tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
125962306a36Sopenharmony_ci
126062306a36Sopenharmony_ci	IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
126162306a36Sopenharmony_ci		     tbl->current_rate, is_green);
126262306a36Sopenharmony_ci	return 0;
126362306a36Sopenharmony_ci}
126462306a36Sopenharmony_ci
126562306a36Sopenharmony_ci/*
126662306a36Sopenharmony_ci * Set up search table for MIMO3
126762306a36Sopenharmony_ci */
126862306a36Sopenharmony_cistatic int rs_switch_to_mimo3(struct iwl_priv *priv,
126962306a36Sopenharmony_ci			     struct iwl_lq_sta *lq_sta,
127062306a36Sopenharmony_ci			     struct ieee80211_conf *conf,
127162306a36Sopenharmony_ci			     struct ieee80211_sta *sta,
127262306a36Sopenharmony_ci			     struct iwl_scale_tbl_info *tbl, int index)
127362306a36Sopenharmony_ci{
127462306a36Sopenharmony_ci	u16 rate_mask;
127562306a36Sopenharmony_ci	s32 rate;
127662306a36Sopenharmony_ci	s8 is_green = lq_sta->is_green;
127762306a36Sopenharmony_ci	struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
127862306a36Sopenharmony_ci	struct iwl_rxon_context *ctx = sta_priv->ctx;
127962306a36Sopenharmony_ci
128062306a36Sopenharmony_ci	if (!conf_is_ht(conf) || !sta->deflink.ht_cap.ht_supported)
128162306a36Sopenharmony_ci		return -1;
128262306a36Sopenharmony_ci
128362306a36Sopenharmony_ci	if (sta->deflink.smps_mode == IEEE80211_SMPS_STATIC)
128462306a36Sopenharmony_ci		return -1;
128562306a36Sopenharmony_ci
128662306a36Sopenharmony_ci	/* Need both Tx chains/antennas to support MIMO */
128762306a36Sopenharmony_ci	if (priv->hw_params.tx_chains_num < 3)
128862306a36Sopenharmony_ci		return -1;
128962306a36Sopenharmony_ci
129062306a36Sopenharmony_ci	IWL_DEBUG_RATE(priv, "LQ: try to switch to MIMO3\n");
129162306a36Sopenharmony_ci
129262306a36Sopenharmony_ci	tbl->lq_type = LQ_MIMO3;
129362306a36Sopenharmony_ci	tbl->is_dup = lq_sta->is_dup;
129462306a36Sopenharmony_ci	tbl->action = 0;
129562306a36Sopenharmony_ci	tbl->max_search = IWL_MAX_11N_MIMO3_SEARCH;
129662306a36Sopenharmony_ci	rate_mask = lq_sta->active_mimo3_rate;
129762306a36Sopenharmony_ci
129862306a36Sopenharmony_ci	if (iwl_is_ht40_tx_allowed(priv, ctx, sta))
129962306a36Sopenharmony_ci		tbl->is_ht40 = 1;
130062306a36Sopenharmony_ci	else
130162306a36Sopenharmony_ci		tbl->is_ht40 = 0;
130262306a36Sopenharmony_ci
130362306a36Sopenharmony_ci	rs_set_expected_tpt_table(lq_sta, tbl);
130462306a36Sopenharmony_ci
130562306a36Sopenharmony_ci	rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
130662306a36Sopenharmony_ci
130762306a36Sopenharmony_ci	IWL_DEBUG_RATE(priv, "LQ: MIMO3 best rate %d mask %X\n",
130862306a36Sopenharmony_ci		rate, rate_mask);
130962306a36Sopenharmony_ci	if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
131062306a36Sopenharmony_ci		IWL_DEBUG_RATE(priv, "Can't switch with index %d rate mask %x\n",
131162306a36Sopenharmony_ci						rate, rate_mask);
131262306a36Sopenharmony_ci		return -1;
131362306a36Sopenharmony_ci	}
131462306a36Sopenharmony_ci	tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
131562306a36Sopenharmony_ci
131662306a36Sopenharmony_ci	IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
131762306a36Sopenharmony_ci		     tbl->current_rate, is_green);
131862306a36Sopenharmony_ci	return 0;
131962306a36Sopenharmony_ci}
132062306a36Sopenharmony_ci
132162306a36Sopenharmony_ci/*
132262306a36Sopenharmony_ci * Set up search table for SISO
132362306a36Sopenharmony_ci */
132462306a36Sopenharmony_cistatic int rs_switch_to_siso(struct iwl_priv *priv,
132562306a36Sopenharmony_ci			     struct iwl_lq_sta *lq_sta,
132662306a36Sopenharmony_ci			     struct ieee80211_conf *conf,
132762306a36Sopenharmony_ci			     struct ieee80211_sta *sta,
132862306a36Sopenharmony_ci			     struct iwl_scale_tbl_info *tbl, int index)
132962306a36Sopenharmony_ci{
133062306a36Sopenharmony_ci	u16 rate_mask;
133162306a36Sopenharmony_ci	u8 is_green = lq_sta->is_green;
133262306a36Sopenharmony_ci	s32 rate;
133362306a36Sopenharmony_ci	struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
133462306a36Sopenharmony_ci	struct iwl_rxon_context *ctx = sta_priv->ctx;
133562306a36Sopenharmony_ci
133662306a36Sopenharmony_ci	if (!conf_is_ht(conf) || !sta->deflink.ht_cap.ht_supported)
133762306a36Sopenharmony_ci		return -1;
133862306a36Sopenharmony_ci
133962306a36Sopenharmony_ci	IWL_DEBUG_RATE(priv, "LQ: try to switch to SISO\n");
134062306a36Sopenharmony_ci
134162306a36Sopenharmony_ci	tbl->is_dup = lq_sta->is_dup;
134262306a36Sopenharmony_ci	tbl->lq_type = LQ_SISO;
134362306a36Sopenharmony_ci	tbl->action = 0;
134462306a36Sopenharmony_ci	tbl->max_search = IWL_MAX_SEARCH;
134562306a36Sopenharmony_ci	rate_mask = lq_sta->active_siso_rate;
134662306a36Sopenharmony_ci
134762306a36Sopenharmony_ci	if (iwl_is_ht40_tx_allowed(priv, ctx, sta))
134862306a36Sopenharmony_ci		tbl->is_ht40 = 1;
134962306a36Sopenharmony_ci	else
135062306a36Sopenharmony_ci		tbl->is_ht40 = 0;
135162306a36Sopenharmony_ci
135262306a36Sopenharmony_ci	if (is_green)
135362306a36Sopenharmony_ci		tbl->is_SGI = 0; /*11n spec: no SGI in SISO+Greenfield*/
135462306a36Sopenharmony_ci
135562306a36Sopenharmony_ci	rs_set_expected_tpt_table(lq_sta, tbl);
135662306a36Sopenharmony_ci	rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
135762306a36Sopenharmony_ci
135862306a36Sopenharmony_ci	IWL_DEBUG_RATE(priv, "LQ: get best rate %d mask %X\n", rate, rate_mask);
135962306a36Sopenharmony_ci	if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
136062306a36Sopenharmony_ci		IWL_DEBUG_RATE(priv, "can not switch with index %d rate mask %x\n",
136162306a36Sopenharmony_ci			     rate, rate_mask);
136262306a36Sopenharmony_ci		return -1;
136362306a36Sopenharmony_ci	}
136462306a36Sopenharmony_ci	tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
136562306a36Sopenharmony_ci	IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
136662306a36Sopenharmony_ci		     tbl->current_rate, is_green);
136762306a36Sopenharmony_ci	return 0;
136862306a36Sopenharmony_ci}
136962306a36Sopenharmony_ci
137062306a36Sopenharmony_ci/*
137162306a36Sopenharmony_ci * Try to switch to new modulation mode from legacy
137262306a36Sopenharmony_ci */
137362306a36Sopenharmony_cistatic void rs_move_legacy_other(struct iwl_priv *priv,
137462306a36Sopenharmony_ci				 struct iwl_lq_sta *lq_sta,
137562306a36Sopenharmony_ci				 struct ieee80211_conf *conf,
137662306a36Sopenharmony_ci				 struct ieee80211_sta *sta,
137762306a36Sopenharmony_ci				 int index)
137862306a36Sopenharmony_ci{
137962306a36Sopenharmony_ci	struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
138062306a36Sopenharmony_ci	struct iwl_scale_tbl_info *search_tbl =
138162306a36Sopenharmony_ci				&(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
138262306a36Sopenharmony_ci	struct iwl_rate_scale_data *window = &(tbl->win[index]);
138362306a36Sopenharmony_ci	u32 sz = (sizeof(struct iwl_scale_tbl_info) -
138462306a36Sopenharmony_ci		  (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
138562306a36Sopenharmony_ci	u8 start_action;
138662306a36Sopenharmony_ci	u8 valid_tx_ant = priv->nvm_data->valid_tx_ant;
138762306a36Sopenharmony_ci	u8 tx_chains_num = priv->hw_params.tx_chains_num;
138862306a36Sopenharmony_ci	int ret = 0;
138962306a36Sopenharmony_ci	u8 update_search_tbl_counter = 0;
139062306a36Sopenharmony_ci
139162306a36Sopenharmony_ci	switch (priv->bt_traffic_load) {
139262306a36Sopenharmony_ci	case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
139362306a36Sopenharmony_ci		/* nothing */
139462306a36Sopenharmony_ci		break;
139562306a36Sopenharmony_ci	case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
139662306a36Sopenharmony_ci		/* avoid antenna B unless MIMO */
139762306a36Sopenharmony_ci		if (tbl->action == IWL_LEGACY_SWITCH_ANTENNA2)
139862306a36Sopenharmony_ci			tbl->action = IWL_LEGACY_SWITCH_SISO;
139962306a36Sopenharmony_ci		break;
140062306a36Sopenharmony_ci	case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
140162306a36Sopenharmony_ci	case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
140262306a36Sopenharmony_ci		/* avoid antenna B and MIMO */
140362306a36Sopenharmony_ci		valid_tx_ant =
140462306a36Sopenharmony_ci			first_antenna(priv->nvm_data->valid_tx_ant);
140562306a36Sopenharmony_ci		if (tbl->action >= IWL_LEGACY_SWITCH_ANTENNA2 &&
140662306a36Sopenharmony_ci		    tbl->action != IWL_LEGACY_SWITCH_SISO)
140762306a36Sopenharmony_ci			tbl->action = IWL_LEGACY_SWITCH_SISO;
140862306a36Sopenharmony_ci		break;
140962306a36Sopenharmony_ci	default:
141062306a36Sopenharmony_ci		IWL_ERR(priv, "Invalid BT load %d\n", priv->bt_traffic_load);
141162306a36Sopenharmony_ci		break;
141262306a36Sopenharmony_ci	}
141362306a36Sopenharmony_ci
141462306a36Sopenharmony_ci	if (!iwl_ht_enabled(priv))
141562306a36Sopenharmony_ci		/* stay in Legacy */
141662306a36Sopenharmony_ci		tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
141762306a36Sopenharmony_ci	else if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE &&
141862306a36Sopenharmony_ci		   tbl->action > IWL_LEGACY_SWITCH_SISO)
141962306a36Sopenharmony_ci		tbl->action = IWL_LEGACY_SWITCH_SISO;
142062306a36Sopenharmony_ci
142162306a36Sopenharmony_ci	/* configure as 1x1 if bt full concurrency */
142262306a36Sopenharmony_ci	if (priv->bt_full_concurrent) {
142362306a36Sopenharmony_ci		if (!iwl_ht_enabled(priv))
142462306a36Sopenharmony_ci			tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
142562306a36Sopenharmony_ci		else if (tbl->action >= IWL_LEGACY_SWITCH_ANTENNA2)
142662306a36Sopenharmony_ci			tbl->action = IWL_LEGACY_SWITCH_SISO;
142762306a36Sopenharmony_ci		valid_tx_ant =
142862306a36Sopenharmony_ci			first_antenna(priv->nvm_data->valid_tx_ant);
142962306a36Sopenharmony_ci	}
143062306a36Sopenharmony_ci
143162306a36Sopenharmony_ci	start_action = tbl->action;
143262306a36Sopenharmony_ci	for (; ;) {
143362306a36Sopenharmony_ci		lq_sta->action_counter++;
143462306a36Sopenharmony_ci		switch (tbl->action) {
143562306a36Sopenharmony_ci		case IWL_LEGACY_SWITCH_ANTENNA1:
143662306a36Sopenharmony_ci		case IWL_LEGACY_SWITCH_ANTENNA2:
143762306a36Sopenharmony_ci			IWL_DEBUG_RATE(priv, "LQ: Legacy toggle Antenna\n");
143862306a36Sopenharmony_ci
143962306a36Sopenharmony_ci			if ((tbl->action == IWL_LEGACY_SWITCH_ANTENNA1 &&
144062306a36Sopenharmony_ci							tx_chains_num <= 1) ||
144162306a36Sopenharmony_ci			    (tbl->action == IWL_LEGACY_SWITCH_ANTENNA2 &&
144262306a36Sopenharmony_ci							tx_chains_num <= 2))
144362306a36Sopenharmony_ci				break;
144462306a36Sopenharmony_ci
144562306a36Sopenharmony_ci			/* Don't change antenna if success has been great */
144662306a36Sopenharmony_ci			if (window->success_ratio >= IWL_RS_GOOD_RATIO &&
144762306a36Sopenharmony_ci			    !priv->bt_full_concurrent &&
144862306a36Sopenharmony_ci			    priv->bt_traffic_load ==
144962306a36Sopenharmony_ci					IWL_BT_COEX_TRAFFIC_LOAD_NONE)
145062306a36Sopenharmony_ci				break;
145162306a36Sopenharmony_ci
145262306a36Sopenharmony_ci			/* Set up search table to try other antenna */
145362306a36Sopenharmony_ci			memcpy(search_tbl, tbl, sz);
145462306a36Sopenharmony_ci
145562306a36Sopenharmony_ci			if (rs_toggle_antenna(valid_tx_ant,
145662306a36Sopenharmony_ci				&search_tbl->current_rate, search_tbl)) {
145762306a36Sopenharmony_ci				update_search_tbl_counter = 1;
145862306a36Sopenharmony_ci				rs_set_expected_tpt_table(lq_sta, search_tbl);
145962306a36Sopenharmony_ci				goto out;
146062306a36Sopenharmony_ci			}
146162306a36Sopenharmony_ci			break;
146262306a36Sopenharmony_ci		case IWL_LEGACY_SWITCH_SISO:
146362306a36Sopenharmony_ci			IWL_DEBUG_RATE(priv, "LQ: Legacy switch to SISO\n");
146462306a36Sopenharmony_ci
146562306a36Sopenharmony_ci			/* Set up search table to try SISO */
146662306a36Sopenharmony_ci			memcpy(search_tbl, tbl, sz);
146762306a36Sopenharmony_ci			search_tbl->is_SGI = 0;
146862306a36Sopenharmony_ci			ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
146962306a36Sopenharmony_ci						 search_tbl, index);
147062306a36Sopenharmony_ci			if (!ret) {
147162306a36Sopenharmony_ci				lq_sta->action_counter = 0;
147262306a36Sopenharmony_ci				goto out;
147362306a36Sopenharmony_ci			}
147462306a36Sopenharmony_ci
147562306a36Sopenharmony_ci			break;
147662306a36Sopenharmony_ci		case IWL_LEGACY_SWITCH_MIMO2_AB:
147762306a36Sopenharmony_ci		case IWL_LEGACY_SWITCH_MIMO2_AC:
147862306a36Sopenharmony_ci		case IWL_LEGACY_SWITCH_MIMO2_BC:
147962306a36Sopenharmony_ci			IWL_DEBUG_RATE(priv, "LQ: Legacy switch to MIMO2\n");
148062306a36Sopenharmony_ci
148162306a36Sopenharmony_ci			/* Set up search table to try MIMO */
148262306a36Sopenharmony_ci			memcpy(search_tbl, tbl, sz);
148362306a36Sopenharmony_ci			search_tbl->is_SGI = 0;
148462306a36Sopenharmony_ci
148562306a36Sopenharmony_ci			if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AB)
148662306a36Sopenharmony_ci				search_tbl->ant_type = ANT_AB;
148762306a36Sopenharmony_ci			else if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AC)
148862306a36Sopenharmony_ci				search_tbl->ant_type = ANT_AC;
148962306a36Sopenharmony_ci			else
149062306a36Sopenharmony_ci				search_tbl->ant_type = ANT_BC;
149162306a36Sopenharmony_ci
149262306a36Sopenharmony_ci			if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
149362306a36Sopenharmony_ci				break;
149462306a36Sopenharmony_ci
149562306a36Sopenharmony_ci			ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
149662306a36Sopenharmony_ci						 search_tbl, index);
149762306a36Sopenharmony_ci			if (!ret) {
149862306a36Sopenharmony_ci				lq_sta->action_counter = 0;
149962306a36Sopenharmony_ci				goto out;
150062306a36Sopenharmony_ci			}
150162306a36Sopenharmony_ci			break;
150262306a36Sopenharmony_ci
150362306a36Sopenharmony_ci		case IWL_LEGACY_SWITCH_MIMO3_ABC:
150462306a36Sopenharmony_ci			IWL_DEBUG_RATE(priv, "LQ: Legacy switch to MIMO3\n");
150562306a36Sopenharmony_ci
150662306a36Sopenharmony_ci			/* Set up search table to try MIMO3 */
150762306a36Sopenharmony_ci			memcpy(search_tbl, tbl, sz);
150862306a36Sopenharmony_ci			search_tbl->is_SGI = 0;
150962306a36Sopenharmony_ci
151062306a36Sopenharmony_ci			search_tbl->ant_type = ANT_ABC;
151162306a36Sopenharmony_ci
151262306a36Sopenharmony_ci			if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
151362306a36Sopenharmony_ci				break;
151462306a36Sopenharmony_ci
151562306a36Sopenharmony_ci			ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
151662306a36Sopenharmony_ci						 search_tbl, index);
151762306a36Sopenharmony_ci			if (!ret) {
151862306a36Sopenharmony_ci				lq_sta->action_counter = 0;
151962306a36Sopenharmony_ci				goto out;
152062306a36Sopenharmony_ci			}
152162306a36Sopenharmony_ci			break;
152262306a36Sopenharmony_ci		}
152362306a36Sopenharmony_ci		tbl->action++;
152462306a36Sopenharmony_ci		if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
152562306a36Sopenharmony_ci			tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
152662306a36Sopenharmony_ci
152762306a36Sopenharmony_ci		if (tbl->action == start_action)
152862306a36Sopenharmony_ci			break;
152962306a36Sopenharmony_ci
153062306a36Sopenharmony_ci	}
153162306a36Sopenharmony_ci	search_tbl->lq_type = LQ_NONE;
153262306a36Sopenharmony_ci	return;
153362306a36Sopenharmony_ci
153462306a36Sopenharmony_ciout:
153562306a36Sopenharmony_ci	lq_sta->search_better_tbl = 1;
153662306a36Sopenharmony_ci	tbl->action++;
153762306a36Sopenharmony_ci	if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
153862306a36Sopenharmony_ci		tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
153962306a36Sopenharmony_ci	if (update_search_tbl_counter)
154062306a36Sopenharmony_ci		search_tbl->action = tbl->action;
154162306a36Sopenharmony_ci}
154262306a36Sopenharmony_ci
154362306a36Sopenharmony_ci/*
154462306a36Sopenharmony_ci * Try to switch to new modulation mode from SISO
154562306a36Sopenharmony_ci */
154662306a36Sopenharmony_cistatic void rs_move_siso_to_other(struct iwl_priv *priv,
154762306a36Sopenharmony_ci				  struct iwl_lq_sta *lq_sta,
154862306a36Sopenharmony_ci				  struct ieee80211_conf *conf,
154962306a36Sopenharmony_ci				  struct ieee80211_sta *sta, int index)
155062306a36Sopenharmony_ci{
155162306a36Sopenharmony_ci	u8 is_green = lq_sta->is_green;
155262306a36Sopenharmony_ci	struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
155362306a36Sopenharmony_ci	struct iwl_scale_tbl_info *search_tbl =
155462306a36Sopenharmony_ci				&(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
155562306a36Sopenharmony_ci	struct iwl_rate_scale_data *window = &(tbl->win[index]);
155662306a36Sopenharmony_ci	struct ieee80211_sta_ht_cap *ht_cap = &sta->deflink.ht_cap;
155762306a36Sopenharmony_ci	u32 sz = (sizeof(struct iwl_scale_tbl_info) -
155862306a36Sopenharmony_ci		  (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
155962306a36Sopenharmony_ci	u8 start_action;
156062306a36Sopenharmony_ci	u8 valid_tx_ant = priv->nvm_data->valid_tx_ant;
156162306a36Sopenharmony_ci	u8 tx_chains_num = priv->hw_params.tx_chains_num;
156262306a36Sopenharmony_ci	u8 update_search_tbl_counter = 0;
156362306a36Sopenharmony_ci	int ret;
156462306a36Sopenharmony_ci
156562306a36Sopenharmony_ci	switch (priv->bt_traffic_load) {
156662306a36Sopenharmony_ci	case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
156762306a36Sopenharmony_ci		/* nothing */
156862306a36Sopenharmony_ci		break;
156962306a36Sopenharmony_ci	case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
157062306a36Sopenharmony_ci		/* avoid antenna B unless MIMO */
157162306a36Sopenharmony_ci		if (tbl->action == IWL_SISO_SWITCH_ANTENNA2)
157262306a36Sopenharmony_ci			tbl->action = IWL_SISO_SWITCH_MIMO2_AB;
157362306a36Sopenharmony_ci		break;
157462306a36Sopenharmony_ci	case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
157562306a36Sopenharmony_ci	case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
157662306a36Sopenharmony_ci		/* avoid antenna B and MIMO */
157762306a36Sopenharmony_ci		valid_tx_ant =
157862306a36Sopenharmony_ci			first_antenna(priv->nvm_data->valid_tx_ant);
157962306a36Sopenharmony_ci		if (tbl->action != IWL_SISO_SWITCH_ANTENNA1)
158062306a36Sopenharmony_ci			tbl->action = IWL_SISO_SWITCH_ANTENNA1;
158162306a36Sopenharmony_ci		break;
158262306a36Sopenharmony_ci	default:
158362306a36Sopenharmony_ci		IWL_ERR(priv, "Invalid BT load %d\n", priv->bt_traffic_load);
158462306a36Sopenharmony_ci		break;
158562306a36Sopenharmony_ci	}
158662306a36Sopenharmony_ci
158762306a36Sopenharmony_ci	if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE &&
158862306a36Sopenharmony_ci	    tbl->action > IWL_SISO_SWITCH_ANTENNA2) {
158962306a36Sopenharmony_ci		/* stay in SISO */
159062306a36Sopenharmony_ci		tbl->action = IWL_SISO_SWITCH_ANTENNA1;
159162306a36Sopenharmony_ci	}
159262306a36Sopenharmony_ci
159362306a36Sopenharmony_ci	/* configure as 1x1 if bt full concurrency */
159462306a36Sopenharmony_ci	if (priv->bt_full_concurrent) {
159562306a36Sopenharmony_ci		valid_tx_ant =
159662306a36Sopenharmony_ci			first_antenna(priv->nvm_data->valid_tx_ant);
159762306a36Sopenharmony_ci		if (tbl->action >= IWL_LEGACY_SWITCH_ANTENNA2)
159862306a36Sopenharmony_ci			tbl->action = IWL_SISO_SWITCH_ANTENNA1;
159962306a36Sopenharmony_ci	}
160062306a36Sopenharmony_ci
160162306a36Sopenharmony_ci	start_action = tbl->action;
160262306a36Sopenharmony_ci	for (;;) {
160362306a36Sopenharmony_ci		lq_sta->action_counter++;
160462306a36Sopenharmony_ci		switch (tbl->action) {
160562306a36Sopenharmony_ci		case IWL_SISO_SWITCH_ANTENNA1:
160662306a36Sopenharmony_ci		case IWL_SISO_SWITCH_ANTENNA2:
160762306a36Sopenharmony_ci			IWL_DEBUG_RATE(priv, "LQ: SISO toggle Antenna\n");
160862306a36Sopenharmony_ci			if ((tbl->action == IWL_SISO_SWITCH_ANTENNA1 &&
160962306a36Sopenharmony_ci						tx_chains_num <= 1) ||
161062306a36Sopenharmony_ci			    (tbl->action == IWL_SISO_SWITCH_ANTENNA2 &&
161162306a36Sopenharmony_ci						tx_chains_num <= 2))
161262306a36Sopenharmony_ci				break;
161362306a36Sopenharmony_ci
161462306a36Sopenharmony_ci			if (window->success_ratio >= IWL_RS_GOOD_RATIO &&
161562306a36Sopenharmony_ci			    !priv->bt_full_concurrent &&
161662306a36Sopenharmony_ci			    priv->bt_traffic_load ==
161762306a36Sopenharmony_ci					IWL_BT_COEX_TRAFFIC_LOAD_NONE)
161862306a36Sopenharmony_ci				break;
161962306a36Sopenharmony_ci
162062306a36Sopenharmony_ci			memcpy(search_tbl, tbl, sz);
162162306a36Sopenharmony_ci			if (rs_toggle_antenna(valid_tx_ant,
162262306a36Sopenharmony_ci				       &search_tbl->current_rate, search_tbl)) {
162362306a36Sopenharmony_ci				update_search_tbl_counter = 1;
162462306a36Sopenharmony_ci				goto out;
162562306a36Sopenharmony_ci			}
162662306a36Sopenharmony_ci			break;
162762306a36Sopenharmony_ci		case IWL_SISO_SWITCH_MIMO2_AB:
162862306a36Sopenharmony_ci		case IWL_SISO_SWITCH_MIMO2_AC:
162962306a36Sopenharmony_ci		case IWL_SISO_SWITCH_MIMO2_BC:
163062306a36Sopenharmony_ci			IWL_DEBUG_RATE(priv, "LQ: SISO switch to MIMO2\n");
163162306a36Sopenharmony_ci			memcpy(search_tbl, tbl, sz);
163262306a36Sopenharmony_ci			search_tbl->is_SGI = 0;
163362306a36Sopenharmony_ci
163462306a36Sopenharmony_ci			if (tbl->action == IWL_SISO_SWITCH_MIMO2_AB)
163562306a36Sopenharmony_ci				search_tbl->ant_type = ANT_AB;
163662306a36Sopenharmony_ci			else if (tbl->action == IWL_SISO_SWITCH_MIMO2_AC)
163762306a36Sopenharmony_ci				search_tbl->ant_type = ANT_AC;
163862306a36Sopenharmony_ci			else
163962306a36Sopenharmony_ci				search_tbl->ant_type = ANT_BC;
164062306a36Sopenharmony_ci
164162306a36Sopenharmony_ci			if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
164262306a36Sopenharmony_ci				break;
164362306a36Sopenharmony_ci
164462306a36Sopenharmony_ci			ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
164562306a36Sopenharmony_ci						 search_tbl, index);
164662306a36Sopenharmony_ci			if (!ret)
164762306a36Sopenharmony_ci				goto out;
164862306a36Sopenharmony_ci			break;
164962306a36Sopenharmony_ci		case IWL_SISO_SWITCH_GI:
165062306a36Sopenharmony_ci			if (!tbl->is_ht40 && !(ht_cap->cap &
165162306a36Sopenharmony_ci						IEEE80211_HT_CAP_SGI_20))
165262306a36Sopenharmony_ci				break;
165362306a36Sopenharmony_ci			if (tbl->is_ht40 && !(ht_cap->cap &
165462306a36Sopenharmony_ci						IEEE80211_HT_CAP_SGI_40))
165562306a36Sopenharmony_ci				break;
165662306a36Sopenharmony_ci
165762306a36Sopenharmony_ci			IWL_DEBUG_RATE(priv, "LQ: SISO toggle SGI/NGI\n");
165862306a36Sopenharmony_ci
165962306a36Sopenharmony_ci			memcpy(search_tbl, tbl, sz);
166062306a36Sopenharmony_ci			if (is_green) {
166162306a36Sopenharmony_ci				if (!tbl->is_SGI)
166262306a36Sopenharmony_ci					break;
166362306a36Sopenharmony_ci				else
166462306a36Sopenharmony_ci					IWL_ERR(priv,
166562306a36Sopenharmony_ci						"SGI was set in GF+SISO\n");
166662306a36Sopenharmony_ci			}
166762306a36Sopenharmony_ci			search_tbl->is_SGI = !tbl->is_SGI;
166862306a36Sopenharmony_ci			rs_set_expected_tpt_table(lq_sta, search_tbl);
166962306a36Sopenharmony_ci			if (tbl->is_SGI) {
167062306a36Sopenharmony_ci				s32 tpt = lq_sta->last_tpt / 100;
167162306a36Sopenharmony_ci				if (tpt >= search_tbl->expected_tpt[index])
167262306a36Sopenharmony_ci					break;
167362306a36Sopenharmony_ci			}
167462306a36Sopenharmony_ci			search_tbl->current_rate =
167562306a36Sopenharmony_ci				rate_n_flags_from_tbl(priv, search_tbl,
167662306a36Sopenharmony_ci						      index, is_green);
167762306a36Sopenharmony_ci			update_search_tbl_counter = 1;
167862306a36Sopenharmony_ci			goto out;
167962306a36Sopenharmony_ci		case IWL_SISO_SWITCH_MIMO3_ABC:
168062306a36Sopenharmony_ci			IWL_DEBUG_RATE(priv, "LQ: SISO switch to MIMO3\n");
168162306a36Sopenharmony_ci			memcpy(search_tbl, tbl, sz);
168262306a36Sopenharmony_ci			search_tbl->is_SGI = 0;
168362306a36Sopenharmony_ci			search_tbl->ant_type = ANT_ABC;
168462306a36Sopenharmony_ci
168562306a36Sopenharmony_ci			if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
168662306a36Sopenharmony_ci				break;
168762306a36Sopenharmony_ci
168862306a36Sopenharmony_ci			ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
168962306a36Sopenharmony_ci						 search_tbl, index);
169062306a36Sopenharmony_ci			if (!ret)
169162306a36Sopenharmony_ci				goto out;
169262306a36Sopenharmony_ci			break;
169362306a36Sopenharmony_ci		}
169462306a36Sopenharmony_ci		tbl->action++;
169562306a36Sopenharmony_ci		if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
169662306a36Sopenharmony_ci			tbl->action = IWL_SISO_SWITCH_ANTENNA1;
169762306a36Sopenharmony_ci
169862306a36Sopenharmony_ci		if (tbl->action == start_action)
169962306a36Sopenharmony_ci			break;
170062306a36Sopenharmony_ci	}
170162306a36Sopenharmony_ci	search_tbl->lq_type = LQ_NONE;
170262306a36Sopenharmony_ci	return;
170362306a36Sopenharmony_ci
170462306a36Sopenharmony_ci out:
170562306a36Sopenharmony_ci	lq_sta->search_better_tbl = 1;
170662306a36Sopenharmony_ci	tbl->action++;
170762306a36Sopenharmony_ci	if (tbl->action > IWL_SISO_SWITCH_MIMO3_ABC)
170862306a36Sopenharmony_ci		tbl->action = IWL_SISO_SWITCH_ANTENNA1;
170962306a36Sopenharmony_ci	if (update_search_tbl_counter)
171062306a36Sopenharmony_ci		search_tbl->action = tbl->action;
171162306a36Sopenharmony_ci}
171262306a36Sopenharmony_ci
171362306a36Sopenharmony_ci/*
171462306a36Sopenharmony_ci * Try to switch to new modulation mode from MIMO2
171562306a36Sopenharmony_ci */
171662306a36Sopenharmony_cistatic void rs_move_mimo2_to_other(struct iwl_priv *priv,
171762306a36Sopenharmony_ci				   struct iwl_lq_sta *lq_sta,
171862306a36Sopenharmony_ci				   struct ieee80211_conf *conf,
171962306a36Sopenharmony_ci				   struct ieee80211_sta *sta, int index)
172062306a36Sopenharmony_ci{
172162306a36Sopenharmony_ci	s8 is_green = lq_sta->is_green;
172262306a36Sopenharmony_ci	struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
172362306a36Sopenharmony_ci	struct iwl_scale_tbl_info *search_tbl =
172462306a36Sopenharmony_ci				&(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
172562306a36Sopenharmony_ci	struct iwl_rate_scale_data *window = &(tbl->win[index]);
172662306a36Sopenharmony_ci	struct ieee80211_sta_ht_cap *ht_cap = &sta->deflink.ht_cap;
172762306a36Sopenharmony_ci	u32 sz = (sizeof(struct iwl_scale_tbl_info) -
172862306a36Sopenharmony_ci		  (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
172962306a36Sopenharmony_ci	u8 start_action;
173062306a36Sopenharmony_ci	u8 valid_tx_ant = priv->nvm_data->valid_tx_ant;
173162306a36Sopenharmony_ci	u8 tx_chains_num = priv->hw_params.tx_chains_num;
173262306a36Sopenharmony_ci	u8 update_search_tbl_counter = 0;
173362306a36Sopenharmony_ci	int ret;
173462306a36Sopenharmony_ci
173562306a36Sopenharmony_ci	switch (priv->bt_traffic_load) {
173662306a36Sopenharmony_ci	case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
173762306a36Sopenharmony_ci		/* nothing */
173862306a36Sopenharmony_ci		break;
173962306a36Sopenharmony_ci	case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
174062306a36Sopenharmony_ci	case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
174162306a36Sopenharmony_ci		/* avoid antenna B and MIMO */
174262306a36Sopenharmony_ci		if (tbl->action != IWL_MIMO2_SWITCH_SISO_A)
174362306a36Sopenharmony_ci			tbl->action = IWL_MIMO2_SWITCH_SISO_A;
174462306a36Sopenharmony_ci		break;
174562306a36Sopenharmony_ci	case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
174662306a36Sopenharmony_ci		/* avoid antenna B unless MIMO */
174762306a36Sopenharmony_ci		if (tbl->action == IWL_MIMO2_SWITCH_SISO_B ||
174862306a36Sopenharmony_ci		    tbl->action == IWL_MIMO2_SWITCH_SISO_C)
174962306a36Sopenharmony_ci			tbl->action = IWL_MIMO2_SWITCH_SISO_A;
175062306a36Sopenharmony_ci		break;
175162306a36Sopenharmony_ci	default:
175262306a36Sopenharmony_ci		IWL_ERR(priv, "Invalid BT load %d\n", priv->bt_traffic_load);
175362306a36Sopenharmony_ci		break;
175462306a36Sopenharmony_ci	}
175562306a36Sopenharmony_ci
175662306a36Sopenharmony_ci	if ((iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE) &&
175762306a36Sopenharmony_ci	    (tbl->action < IWL_MIMO2_SWITCH_SISO_A ||
175862306a36Sopenharmony_ci	     tbl->action > IWL_MIMO2_SWITCH_SISO_C)) {
175962306a36Sopenharmony_ci		/* switch in SISO */
176062306a36Sopenharmony_ci		tbl->action = IWL_MIMO2_SWITCH_SISO_A;
176162306a36Sopenharmony_ci	}
176262306a36Sopenharmony_ci
176362306a36Sopenharmony_ci	/* configure as 1x1 if bt full concurrency */
176462306a36Sopenharmony_ci	if (priv->bt_full_concurrent &&
176562306a36Sopenharmony_ci	    (tbl->action < IWL_MIMO2_SWITCH_SISO_A ||
176662306a36Sopenharmony_ci	     tbl->action > IWL_MIMO2_SWITCH_SISO_C))
176762306a36Sopenharmony_ci		tbl->action = IWL_MIMO2_SWITCH_SISO_A;
176862306a36Sopenharmony_ci
176962306a36Sopenharmony_ci	start_action = tbl->action;
177062306a36Sopenharmony_ci	for (;;) {
177162306a36Sopenharmony_ci		lq_sta->action_counter++;
177262306a36Sopenharmony_ci		switch (tbl->action) {
177362306a36Sopenharmony_ci		case IWL_MIMO2_SWITCH_ANTENNA1:
177462306a36Sopenharmony_ci		case IWL_MIMO2_SWITCH_ANTENNA2:
177562306a36Sopenharmony_ci			IWL_DEBUG_RATE(priv, "LQ: MIMO2 toggle Antennas\n");
177662306a36Sopenharmony_ci
177762306a36Sopenharmony_ci			if (tx_chains_num <= 2)
177862306a36Sopenharmony_ci				break;
177962306a36Sopenharmony_ci
178062306a36Sopenharmony_ci			if (window->success_ratio >= IWL_RS_GOOD_RATIO)
178162306a36Sopenharmony_ci				break;
178262306a36Sopenharmony_ci
178362306a36Sopenharmony_ci			memcpy(search_tbl, tbl, sz);
178462306a36Sopenharmony_ci			if (rs_toggle_antenna(valid_tx_ant,
178562306a36Sopenharmony_ci				       &search_tbl->current_rate, search_tbl)) {
178662306a36Sopenharmony_ci				update_search_tbl_counter = 1;
178762306a36Sopenharmony_ci				goto out;
178862306a36Sopenharmony_ci			}
178962306a36Sopenharmony_ci			break;
179062306a36Sopenharmony_ci		case IWL_MIMO2_SWITCH_SISO_A:
179162306a36Sopenharmony_ci		case IWL_MIMO2_SWITCH_SISO_B:
179262306a36Sopenharmony_ci		case IWL_MIMO2_SWITCH_SISO_C:
179362306a36Sopenharmony_ci			IWL_DEBUG_RATE(priv, "LQ: MIMO2 switch to SISO\n");
179462306a36Sopenharmony_ci
179562306a36Sopenharmony_ci			/* Set up new search table for SISO */
179662306a36Sopenharmony_ci			memcpy(search_tbl, tbl, sz);
179762306a36Sopenharmony_ci
179862306a36Sopenharmony_ci			if (tbl->action == IWL_MIMO2_SWITCH_SISO_A)
179962306a36Sopenharmony_ci				search_tbl->ant_type = ANT_A;
180062306a36Sopenharmony_ci			else if (tbl->action == IWL_MIMO2_SWITCH_SISO_B)
180162306a36Sopenharmony_ci				search_tbl->ant_type = ANT_B;
180262306a36Sopenharmony_ci			else
180362306a36Sopenharmony_ci				search_tbl->ant_type = ANT_C;
180462306a36Sopenharmony_ci
180562306a36Sopenharmony_ci			if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
180662306a36Sopenharmony_ci				break;
180762306a36Sopenharmony_ci
180862306a36Sopenharmony_ci			ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
180962306a36Sopenharmony_ci						 search_tbl, index);
181062306a36Sopenharmony_ci			if (!ret)
181162306a36Sopenharmony_ci				goto out;
181262306a36Sopenharmony_ci
181362306a36Sopenharmony_ci			break;
181462306a36Sopenharmony_ci
181562306a36Sopenharmony_ci		case IWL_MIMO2_SWITCH_GI:
181662306a36Sopenharmony_ci			if (!tbl->is_ht40 && !(ht_cap->cap &
181762306a36Sopenharmony_ci						IEEE80211_HT_CAP_SGI_20))
181862306a36Sopenharmony_ci				break;
181962306a36Sopenharmony_ci			if (tbl->is_ht40 && !(ht_cap->cap &
182062306a36Sopenharmony_ci						IEEE80211_HT_CAP_SGI_40))
182162306a36Sopenharmony_ci				break;
182262306a36Sopenharmony_ci
182362306a36Sopenharmony_ci			IWL_DEBUG_RATE(priv, "LQ: MIMO2 toggle SGI/NGI\n");
182462306a36Sopenharmony_ci
182562306a36Sopenharmony_ci			/* Set up new search table for MIMO2 */
182662306a36Sopenharmony_ci			memcpy(search_tbl, tbl, sz);
182762306a36Sopenharmony_ci			search_tbl->is_SGI = !tbl->is_SGI;
182862306a36Sopenharmony_ci			rs_set_expected_tpt_table(lq_sta, search_tbl);
182962306a36Sopenharmony_ci			/*
183062306a36Sopenharmony_ci			 * If active table already uses the fastest possible
183162306a36Sopenharmony_ci			 * modulation (dual stream with short guard interval),
183262306a36Sopenharmony_ci			 * and it's working well, there's no need to look
183362306a36Sopenharmony_ci			 * for a better type of modulation!
183462306a36Sopenharmony_ci			 */
183562306a36Sopenharmony_ci			if (tbl->is_SGI) {
183662306a36Sopenharmony_ci				s32 tpt = lq_sta->last_tpt / 100;
183762306a36Sopenharmony_ci				if (tpt >= search_tbl->expected_tpt[index])
183862306a36Sopenharmony_ci					break;
183962306a36Sopenharmony_ci			}
184062306a36Sopenharmony_ci			search_tbl->current_rate =
184162306a36Sopenharmony_ci				rate_n_flags_from_tbl(priv, search_tbl,
184262306a36Sopenharmony_ci						      index, is_green);
184362306a36Sopenharmony_ci			update_search_tbl_counter = 1;
184462306a36Sopenharmony_ci			goto out;
184562306a36Sopenharmony_ci
184662306a36Sopenharmony_ci		case IWL_MIMO2_SWITCH_MIMO3_ABC:
184762306a36Sopenharmony_ci			IWL_DEBUG_RATE(priv, "LQ: MIMO2 switch to MIMO3\n");
184862306a36Sopenharmony_ci			memcpy(search_tbl, tbl, sz);
184962306a36Sopenharmony_ci			search_tbl->is_SGI = 0;
185062306a36Sopenharmony_ci			search_tbl->ant_type = ANT_ABC;
185162306a36Sopenharmony_ci
185262306a36Sopenharmony_ci			if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
185362306a36Sopenharmony_ci				break;
185462306a36Sopenharmony_ci
185562306a36Sopenharmony_ci			ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
185662306a36Sopenharmony_ci						 search_tbl, index);
185762306a36Sopenharmony_ci			if (!ret)
185862306a36Sopenharmony_ci				goto out;
185962306a36Sopenharmony_ci
186062306a36Sopenharmony_ci			break;
186162306a36Sopenharmony_ci		}
186262306a36Sopenharmony_ci		tbl->action++;
186362306a36Sopenharmony_ci		if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
186462306a36Sopenharmony_ci			tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
186562306a36Sopenharmony_ci
186662306a36Sopenharmony_ci		if (tbl->action == start_action)
186762306a36Sopenharmony_ci			break;
186862306a36Sopenharmony_ci	}
186962306a36Sopenharmony_ci	search_tbl->lq_type = LQ_NONE;
187062306a36Sopenharmony_ci	return;
187162306a36Sopenharmony_ci out:
187262306a36Sopenharmony_ci	lq_sta->search_better_tbl = 1;
187362306a36Sopenharmony_ci	tbl->action++;
187462306a36Sopenharmony_ci	if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
187562306a36Sopenharmony_ci		tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
187662306a36Sopenharmony_ci	if (update_search_tbl_counter)
187762306a36Sopenharmony_ci		search_tbl->action = tbl->action;
187862306a36Sopenharmony_ci
187962306a36Sopenharmony_ci}
188062306a36Sopenharmony_ci
188162306a36Sopenharmony_ci/*
188262306a36Sopenharmony_ci * Try to switch to new modulation mode from MIMO3
188362306a36Sopenharmony_ci */
188462306a36Sopenharmony_cistatic void rs_move_mimo3_to_other(struct iwl_priv *priv,
188562306a36Sopenharmony_ci				   struct iwl_lq_sta *lq_sta,
188662306a36Sopenharmony_ci				   struct ieee80211_conf *conf,
188762306a36Sopenharmony_ci				   struct ieee80211_sta *sta, int index)
188862306a36Sopenharmony_ci{
188962306a36Sopenharmony_ci	s8 is_green = lq_sta->is_green;
189062306a36Sopenharmony_ci	struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
189162306a36Sopenharmony_ci	struct iwl_scale_tbl_info *search_tbl =
189262306a36Sopenharmony_ci				&(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
189362306a36Sopenharmony_ci	struct iwl_rate_scale_data *window = &(tbl->win[index]);
189462306a36Sopenharmony_ci	struct ieee80211_sta_ht_cap *ht_cap = &sta->deflink.ht_cap;
189562306a36Sopenharmony_ci	u32 sz = (sizeof(struct iwl_scale_tbl_info) -
189662306a36Sopenharmony_ci		  (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
189762306a36Sopenharmony_ci	u8 start_action;
189862306a36Sopenharmony_ci	u8 valid_tx_ant = priv->nvm_data->valid_tx_ant;
189962306a36Sopenharmony_ci	u8 tx_chains_num = priv->hw_params.tx_chains_num;
190062306a36Sopenharmony_ci	int ret;
190162306a36Sopenharmony_ci	u8 update_search_tbl_counter = 0;
190262306a36Sopenharmony_ci
190362306a36Sopenharmony_ci	switch (priv->bt_traffic_load) {
190462306a36Sopenharmony_ci	case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
190562306a36Sopenharmony_ci		/* nothing */
190662306a36Sopenharmony_ci		break;
190762306a36Sopenharmony_ci	case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
190862306a36Sopenharmony_ci	case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
190962306a36Sopenharmony_ci		/* avoid antenna B and MIMO */
191062306a36Sopenharmony_ci		if (tbl->action != IWL_MIMO3_SWITCH_SISO_A)
191162306a36Sopenharmony_ci			tbl->action = IWL_MIMO3_SWITCH_SISO_A;
191262306a36Sopenharmony_ci		break;
191362306a36Sopenharmony_ci	case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
191462306a36Sopenharmony_ci		/* avoid antenna B unless MIMO */
191562306a36Sopenharmony_ci		if (tbl->action == IWL_MIMO3_SWITCH_SISO_B ||
191662306a36Sopenharmony_ci		    tbl->action == IWL_MIMO3_SWITCH_SISO_C)
191762306a36Sopenharmony_ci			tbl->action = IWL_MIMO3_SWITCH_SISO_A;
191862306a36Sopenharmony_ci		break;
191962306a36Sopenharmony_ci	default:
192062306a36Sopenharmony_ci		IWL_ERR(priv, "Invalid BT load %d\n", priv->bt_traffic_load);
192162306a36Sopenharmony_ci		break;
192262306a36Sopenharmony_ci	}
192362306a36Sopenharmony_ci
192462306a36Sopenharmony_ci	if ((iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE) &&
192562306a36Sopenharmony_ci	    (tbl->action < IWL_MIMO3_SWITCH_SISO_A ||
192662306a36Sopenharmony_ci	     tbl->action > IWL_MIMO3_SWITCH_SISO_C)) {
192762306a36Sopenharmony_ci		/* switch in SISO */
192862306a36Sopenharmony_ci		tbl->action = IWL_MIMO3_SWITCH_SISO_A;
192962306a36Sopenharmony_ci	}
193062306a36Sopenharmony_ci
193162306a36Sopenharmony_ci	/* configure as 1x1 if bt full concurrency */
193262306a36Sopenharmony_ci	if (priv->bt_full_concurrent &&
193362306a36Sopenharmony_ci	    (tbl->action < IWL_MIMO3_SWITCH_SISO_A ||
193462306a36Sopenharmony_ci	     tbl->action > IWL_MIMO3_SWITCH_SISO_C))
193562306a36Sopenharmony_ci		tbl->action = IWL_MIMO3_SWITCH_SISO_A;
193662306a36Sopenharmony_ci
193762306a36Sopenharmony_ci	start_action = tbl->action;
193862306a36Sopenharmony_ci	for (;;) {
193962306a36Sopenharmony_ci		lq_sta->action_counter++;
194062306a36Sopenharmony_ci		switch (tbl->action) {
194162306a36Sopenharmony_ci		case IWL_MIMO3_SWITCH_ANTENNA1:
194262306a36Sopenharmony_ci		case IWL_MIMO3_SWITCH_ANTENNA2:
194362306a36Sopenharmony_ci			IWL_DEBUG_RATE(priv, "LQ: MIMO3 toggle Antennas\n");
194462306a36Sopenharmony_ci
194562306a36Sopenharmony_ci			if (tx_chains_num <= 3)
194662306a36Sopenharmony_ci				break;
194762306a36Sopenharmony_ci
194862306a36Sopenharmony_ci			if (window->success_ratio >= IWL_RS_GOOD_RATIO)
194962306a36Sopenharmony_ci				break;
195062306a36Sopenharmony_ci
195162306a36Sopenharmony_ci			memcpy(search_tbl, tbl, sz);
195262306a36Sopenharmony_ci			if (rs_toggle_antenna(valid_tx_ant,
195362306a36Sopenharmony_ci				       &search_tbl->current_rate, search_tbl))
195462306a36Sopenharmony_ci				goto out;
195562306a36Sopenharmony_ci			break;
195662306a36Sopenharmony_ci		case IWL_MIMO3_SWITCH_SISO_A:
195762306a36Sopenharmony_ci		case IWL_MIMO3_SWITCH_SISO_B:
195862306a36Sopenharmony_ci		case IWL_MIMO3_SWITCH_SISO_C:
195962306a36Sopenharmony_ci			IWL_DEBUG_RATE(priv, "LQ: MIMO3 switch to SISO\n");
196062306a36Sopenharmony_ci
196162306a36Sopenharmony_ci			/* Set up new search table for SISO */
196262306a36Sopenharmony_ci			memcpy(search_tbl, tbl, sz);
196362306a36Sopenharmony_ci
196462306a36Sopenharmony_ci			if (tbl->action == IWL_MIMO3_SWITCH_SISO_A)
196562306a36Sopenharmony_ci				search_tbl->ant_type = ANT_A;
196662306a36Sopenharmony_ci			else if (tbl->action == IWL_MIMO3_SWITCH_SISO_B)
196762306a36Sopenharmony_ci				search_tbl->ant_type = ANT_B;
196862306a36Sopenharmony_ci			else
196962306a36Sopenharmony_ci				search_tbl->ant_type = ANT_C;
197062306a36Sopenharmony_ci
197162306a36Sopenharmony_ci			if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
197262306a36Sopenharmony_ci				break;
197362306a36Sopenharmony_ci
197462306a36Sopenharmony_ci			ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
197562306a36Sopenharmony_ci						 search_tbl, index);
197662306a36Sopenharmony_ci			if (!ret)
197762306a36Sopenharmony_ci				goto out;
197862306a36Sopenharmony_ci
197962306a36Sopenharmony_ci			break;
198062306a36Sopenharmony_ci
198162306a36Sopenharmony_ci		case IWL_MIMO3_SWITCH_MIMO2_AB:
198262306a36Sopenharmony_ci		case IWL_MIMO3_SWITCH_MIMO2_AC:
198362306a36Sopenharmony_ci		case IWL_MIMO3_SWITCH_MIMO2_BC:
198462306a36Sopenharmony_ci			IWL_DEBUG_RATE(priv, "LQ: MIMO3 switch to MIMO2\n");
198562306a36Sopenharmony_ci
198662306a36Sopenharmony_ci			memcpy(search_tbl, tbl, sz);
198762306a36Sopenharmony_ci			search_tbl->is_SGI = 0;
198862306a36Sopenharmony_ci			if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AB)
198962306a36Sopenharmony_ci				search_tbl->ant_type = ANT_AB;
199062306a36Sopenharmony_ci			else if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AC)
199162306a36Sopenharmony_ci				search_tbl->ant_type = ANT_AC;
199262306a36Sopenharmony_ci			else
199362306a36Sopenharmony_ci				search_tbl->ant_type = ANT_BC;
199462306a36Sopenharmony_ci
199562306a36Sopenharmony_ci			if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
199662306a36Sopenharmony_ci				break;
199762306a36Sopenharmony_ci
199862306a36Sopenharmony_ci			ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
199962306a36Sopenharmony_ci						 search_tbl, index);
200062306a36Sopenharmony_ci			if (!ret)
200162306a36Sopenharmony_ci				goto out;
200262306a36Sopenharmony_ci
200362306a36Sopenharmony_ci			break;
200462306a36Sopenharmony_ci
200562306a36Sopenharmony_ci		case IWL_MIMO3_SWITCH_GI:
200662306a36Sopenharmony_ci			if (!tbl->is_ht40 && !(ht_cap->cap &
200762306a36Sopenharmony_ci						IEEE80211_HT_CAP_SGI_20))
200862306a36Sopenharmony_ci				break;
200962306a36Sopenharmony_ci			if (tbl->is_ht40 && !(ht_cap->cap &
201062306a36Sopenharmony_ci						IEEE80211_HT_CAP_SGI_40))
201162306a36Sopenharmony_ci				break;
201262306a36Sopenharmony_ci
201362306a36Sopenharmony_ci			IWL_DEBUG_RATE(priv, "LQ: MIMO3 toggle SGI/NGI\n");
201462306a36Sopenharmony_ci
201562306a36Sopenharmony_ci			/* Set up new search table for MIMO */
201662306a36Sopenharmony_ci			memcpy(search_tbl, tbl, sz);
201762306a36Sopenharmony_ci			search_tbl->is_SGI = !tbl->is_SGI;
201862306a36Sopenharmony_ci			rs_set_expected_tpt_table(lq_sta, search_tbl);
201962306a36Sopenharmony_ci			/*
202062306a36Sopenharmony_ci			 * If active table already uses the fastest possible
202162306a36Sopenharmony_ci			 * modulation (dual stream with short guard interval),
202262306a36Sopenharmony_ci			 * and it's working well, there's no need to look
202362306a36Sopenharmony_ci			 * for a better type of modulation!
202462306a36Sopenharmony_ci			 */
202562306a36Sopenharmony_ci			if (tbl->is_SGI) {
202662306a36Sopenharmony_ci				s32 tpt = lq_sta->last_tpt / 100;
202762306a36Sopenharmony_ci				if (tpt >= search_tbl->expected_tpt[index])
202862306a36Sopenharmony_ci					break;
202962306a36Sopenharmony_ci			}
203062306a36Sopenharmony_ci			search_tbl->current_rate =
203162306a36Sopenharmony_ci				rate_n_flags_from_tbl(priv, search_tbl,
203262306a36Sopenharmony_ci						      index, is_green);
203362306a36Sopenharmony_ci			update_search_tbl_counter = 1;
203462306a36Sopenharmony_ci			goto out;
203562306a36Sopenharmony_ci		}
203662306a36Sopenharmony_ci		tbl->action++;
203762306a36Sopenharmony_ci		if (tbl->action > IWL_MIMO3_SWITCH_GI)
203862306a36Sopenharmony_ci			tbl->action = IWL_MIMO3_SWITCH_ANTENNA1;
203962306a36Sopenharmony_ci
204062306a36Sopenharmony_ci		if (tbl->action == start_action)
204162306a36Sopenharmony_ci			break;
204262306a36Sopenharmony_ci	}
204362306a36Sopenharmony_ci	search_tbl->lq_type = LQ_NONE;
204462306a36Sopenharmony_ci	return;
204562306a36Sopenharmony_ci out:
204662306a36Sopenharmony_ci	lq_sta->search_better_tbl = 1;
204762306a36Sopenharmony_ci	tbl->action++;
204862306a36Sopenharmony_ci	if (tbl->action > IWL_MIMO3_SWITCH_GI)
204962306a36Sopenharmony_ci		tbl->action = IWL_MIMO3_SWITCH_ANTENNA1;
205062306a36Sopenharmony_ci	if (update_search_tbl_counter)
205162306a36Sopenharmony_ci		search_tbl->action = tbl->action;
205262306a36Sopenharmony_ci}
205362306a36Sopenharmony_ci
205462306a36Sopenharmony_ci/*
205562306a36Sopenharmony_ci * Check whether we should continue using same modulation mode, or
205662306a36Sopenharmony_ci * begin search for a new mode, based on:
205762306a36Sopenharmony_ci * 1) # tx successes or failures while using this mode
205862306a36Sopenharmony_ci * 2) # times calling this function
205962306a36Sopenharmony_ci * 3) elapsed time in this mode (not used, for now)
206062306a36Sopenharmony_ci */
206162306a36Sopenharmony_cistatic void rs_stay_in_table(struct iwl_lq_sta *lq_sta, bool force_search)
206262306a36Sopenharmony_ci{
206362306a36Sopenharmony_ci	struct iwl_scale_tbl_info *tbl;
206462306a36Sopenharmony_ci	int i;
206562306a36Sopenharmony_ci	int active_tbl;
206662306a36Sopenharmony_ci	int flush_interval_passed = 0;
206762306a36Sopenharmony_ci	struct iwl_priv *priv;
206862306a36Sopenharmony_ci
206962306a36Sopenharmony_ci	priv = lq_sta->drv;
207062306a36Sopenharmony_ci	active_tbl = lq_sta->active_tbl;
207162306a36Sopenharmony_ci
207262306a36Sopenharmony_ci	tbl = &(lq_sta->lq_info[active_tbl]);
207362306a36Sopenharmony_ci
207462306a36Sopenharmony_ci	/* If we've been disallowing search, see if we should now allow it */
207562306a36Sopenharmony_ci	if (lq_sta->stay_in_tbl) {
207662306a36Sopenharmony_ci
207762306a36Sopenharmony_ci		/* Elapsed time using current modulation mode */
207862306a36Sopenharmony_ci		if (lq_sta->flush_timer)
207962306a36Sopenharmony_ci			flush_interval_passed =
208062306a36Sopenharmony_ci			time_after(jiffies,
208162306a36Sopenharmony_ci					(unsigned long)(lq_sta->flush_timer +
208262306a36Sopenharmony_ci					IWL_RATE_SCALE_FLUSH_INTVL));
208362306a36Sopenharmony_ci
208462306a36Sopenharmony_ci		/*
208562306a36Sopenharmony_ci		 * Check if we should allow search for new modulation mode.
208662306a36Sopenharmony_ci		 * If many frames have failed or succeeded, or we've used
208762306a36Sopenharmony_ci		 * this same modulation for a long time, allow search, and
208862306a36Sopenharmony_ci		 * reset history stats that keep track of whether we should
208962306a36Sopenharmony_ci		 * allow a new search.  Also (below) reset all bitmaps and
209062306a36Sopenharmony_ci		 * stats in active history.
209162306a36Sopenharmony_ci		 */
209262306a36Sopenharmony_ci		if (force_search ||
209362306a36Sopenharmony_ci		    (lq_sta->total_failed > lq_sta->max_failure_limit) ||
209462306a36Sopenharmony_ci		    (lq_sta->total_success > lq_sta->max_success_limit) ||
209562306a36Sopenharmony_ci		    ((!lq_sta->search_better_tbl) && (lq_sta->flush_timer)
209662306a36Sopenharmony_ci		     && (flush_interval_passed))) {
209762306a36Sopenharmony_ci			IWL_DEBUG_RATE(priv, "LQ: stay is expired %d %d %d\n",
209862306a36Sopenharmony_ci				     lq_sta->total_failed,
209962306a36Sopenharmony_ci				     lq_sta->total_success,
210062306a36Sopenharmony_ci				     flush_interval_passed);
210162306a36Sopenharmony_ci
210262306a36Sopenharmony_ci			/* Allow search for new mode */
210362306a36Sopenharmony_ci			lq_sta->stay_in_tbl = 0;	/* only place reset */
210462306a36Sopenharmony_ci			lq_sta->total_failed = 0;
210562306a36Sopenharmony_ci			lq_sta->total_success = 0;
210662306a36Sopenharmony_ci			lq_sta->flush_timer = 0;
210762306a36Sopenharmony_ci
210862306a36Sopenharmony_ci		/*
210962306a36Sopenharmony_ci		 * Else if we've used this modulation mode enough repetitions
211062306a36Sopenharmony_ci		 * (regardless of elapsed time or success/failure), reset
211162306a36Sopenharmony_ci		 * history bitmaps and rate-specific stats for all rates in
211262306a36Sopenharmony_ci		 * active table.
211362306a36Sopenharmony_ci		 */
211462306a36Sopenharmony_ci		} else {
211562306a36Sopenharmony_ci			lq_sta->table_count++;
211662306a36Sopenharmony_ci			if (lq_sta->table_count >=
211762306a36Sopenharmony_ci			    lq_sta->table_count_limit) {
211862306a36Sopenharmony_ci				lq_sta->table_count = 0;
211962306a36Sopenharmony_ci
212062306a36Sopenharmony_ci				IWL_DEBUG_RATE(priv, "LQ: stay in table clear win\n");
212162306a36Sopenharmony_ci				for (i = 0; i < IWL_RATE_COUNT; i++)
212262306a36Sopenharmony_ci					rs_rate_scale_clear_window(
212362306a36Sopenharmony_ci						&(tbl->win[i]));
212462306a36Sopenharmony_ci			}
212562306a36Sopenharmony_ci		}
212662306a36Sopenharmony_ci
212762306a36Sopenharmony_ci		/* If transitioning to allow "search", reset all history
212862306a36Sopenharmony_ci		 * bitmaps and stats in active table (this will become the new
212962306a36Sopenharmony_ci		 * "search" table). */
213062306a36Sopenharmony_ci		if (!lq_sta->stay_in_tbl) {
213162306a36Sopenharmony_ci			for (i = 0; i < IWL_RATE_COUNT; i++)
213262306a36Sopenharmony_ci				rs_rate_scale_clear_window(&(tbl->win[i]));
213362306a36Sopenharmony_ci		}
213462306a36Sopenharmony_ci	}
213562306a36Sopenharmony_ci}
213662306a36Sopenharmony_ci
213762306a36Sopenharmony_ci/*
213862306a36Sopenharmony_ci * setup rate table in uCode
213962306a36Sopenharmony_ci */
214062306a36Sopenharmony_cistatic void rs_update_rate_tbl(struct iwl_priv *priv,
214162306a36Sopenharmony_ci			       struct iwl_rxon_context *ctx,
214262306a36Sopenharmony_ci			       struct iwl_lq_sta *lq_sta,
214362306a36Sopenharmony_ci			       struct iwl_scale_tbl_info *tbl,
214462306a36Sopenharmony_ci			       int index, u8 is_green)
214562306a36Sopenharmony_ci{
214662306a36Sopenharmony_ci	u32 rate;
214762306a36Sopenharmony_ci
214862306a36Sopenharmony_ci	/* Update uCode's rate table. */
214962306a36Sopenharmony_ci	rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
215062306a36Sopenharmony_ci	rs_fill_link_cmd(priv, lq_sta, rate);
215162306a36Sopenharmony_ci	iwl_send_lq_cmd(priv, ctx, &lq_sta->lq, CMD_ASYNC, false);
215262306a36Sopenharmony_ci}
215362306a36Sopenharmony_ci
215462306a36Sopenharmony_ci/*
215562306a36Sopenharmony_ci * Do rate scaling and search for new modulation mode.
215662306a36Sopenharmony_ci */
215762306a36Sopenharmony_cistatic void rs_rate_scale_perform(struct iwl_priv *priv,
215862306a36Sopenharmony_ci				  struct sk_buff *skb,
215962306a36Sopenharmony_ci				  struct ieee80211_sta *sta,
216062306a36Sopenharmony_ci				  struct iwl_lq_sta *lq_sta)
216162306a36Sopenharmony_ci{
216262306a36Sopenharmony_ci	struct ieee80211_hw *hw = priv->hw;
216362306a36Sopenharmony_ci	struct ieee80211_conf *conf = &hw->conf;
216462306a36Sopenharmony_ci	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
216562306a36Sopenharmony_ci	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
216662306a36Sopenharmony_ci	int low = IWL_RATE_INVALID;
216762306a36Sopenharmony_ci	int high = IWL_RATE_INVALID;
216862306a36Sopenharmony_ci	int index;
216962306a36Sopenharmony_ci	int i;
217062306a36Sopenharmony_ci	struct iwl_rate_scale_data *window = NULL;
217162306a36Sopenharmony_ci	int current_tpt = IWL_INVALID_VALUE;
217262306a36Sopenharmony_ci	int low_tpt = IWL_INVALID_VALUE;
217362306a36Sopenharmony_ci	int high_tpt = IWL_INVALID_VALUE;
217462306a36Sopenharmony_ci	u32 fail_count;
217562306a36Sopenharmony_ci	s8 scale_action = 0;
217662306a36Sopenharmony_ci	u16 rate_mask;
217762306a36Sopenharmony_ci	u8 update_lq = 0;
217862306a36Sopenharmony_ci	struct iwl_scale_tbl_info *tbl, *tbl1;
217962306a36Sopenharmony_ci	u16 rate_scale_index_msk = 0;
218062306a36Sopenharmony_ci	u8 is_green = 0;
218162306a36Sopenharmony_ci	u8 active_tbl = 0;
218262306a36Sopenharmony_ci	u8 done_search = 0;
218362306a36Sopenharmony_ci	u16 high_low;
218462306a36Sopenharmony_ci	s32 sr;
218562306a36Sopenharmony_ci	u8 tid = IWL_MAX_TID_COUNT;
218662306a36Sopenharmony_ci	struct iwl_tid_data *tid_data;
218762306a36Sopenharmony_ci	struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
218862306a36Sopenharmony_ci	struct iwl_rxon_context *ctx = sta_priv->ctx;
218962306a36Sopenharmony_ci
219062306a36Sopenharmony_ci	IWL_DEBUG_RATE(priv, "rate scale calculate new rate for skb\n");
219162306a36Sopenharmony_ci
219262306a36Sopenharmony_ci	/* Send management frames and NO_ACK data using lowest rate. */
219362306a36Sopenharmony_ci	/* TODO: this could probably be improved.. */
219462306a36Sopenharmony_ci	if (!ieee80211_is_data(hdr->frame_control) ||
219562306a36Sopenharmony_ci	    info->flags & IEEE80211_TX_CTL_NO_ACK)
219662306a36Sopenharmony_ci		return;
219762306a36Sopenharmony_ci
219862306a36Sopenharmony_ci	lq_sta->supp_rates = sta->deflink.supp_rates[lq_sta->band];
219962306a36Sopenharmony_ci
220062306a36Sopenharmony_ci	tid = rs_tl_add_packet(lq_sta, hdr);
220162306a36Sopenharmony_ci	if ((tid != IWL_MAX_TID_COUNT) &&
220262306a36Sopenharmony_ci	    (lq_sta->tx_agg_tid_en & (1 << tid))) {
220362306a36Sopenharmony_ci		tid_data = &priv->tid_data[lq_sta->lq.sta_id][tid];
220462306a36Sopenharmony_ci		if (tid_data->agg.state == IWL_AGG_OFF)
220562306a36Sopenharmony_ci			lq_sta->is_agg = 0;
220662306a36Sopenharmony_ci		else
220762306a36Sopenharmony_ci			lq_sta->is_agg = 1;
220862306a36Sopenharmony_ci	} else
220962306a36Sopenharmony_ci		lq_sta->is_agg = 0;
221062306a36Sopenharmony_ci
221162306a36Sopenharmony_ci	/*
221262306a36Sopenharmony_ci	 * Select rate-scale / modulation-mode table to work with in
221362306a36Sopenharmony_ci	 * the rest of this function:  "search" if searching for better
221462306a36Sopenharmony_ci	 * modulation mode, or "active" if doing rate scaling within a mode.
221562306a36Sopenharmony_ci	 */
221662306a36Sopenharmony_ci	if (!lq_sta->search_better_tbl)
221762306a36Sopenharmony_ci		active_tbl = lq_sta->active_tbl;
221862306a36Sopenharmony_ci	else
221962306a36Sopenharmony_ci		active_tbl = 1 - lq_sta->active_tbl;
222062306a36Sopenharmony_ci
222162306a36Sopenharmony_ci	tbl = &(lq_sta->lq_info[active_tbl]);
222262306a36Sopenharmony_ci	if (is_legacy(tbl->lq_type))
222362306a36Sopenharmony_ci		lq_sta->is_green = 0;
222462306a36Sopenharmony_ci	else
222562306a36Sopenharmony_ci		lq_sta->is_green = rs_use_green(sta);
222662306a36Sopenharmony_ci	is_green = lq_sta->is_green;
222762306a36Sopenharmony_ci
222862306a36Sopenharmony_ci	/* current tx rate */
222962306a36Sopenharmony_ci	index = lq_sta->last_txrate_idx;
223062306a36Sopenharmony_ci
223162306a36Sopenharmony_ci	IWL_DEBUG_RATE(priv, "Rate scale index %d for type %d\n", index,
223262306a36Sopenharmony_ci		       tbl->lq_type);
223362306a36Sopenharmony_ci
223462306a36Sopenharmony_ci	/* rates available for this association, and for modulation mode */
223562306a36Sopenharmony_ci	rate_mask = rs_get_supported_rates(lq_sta, hdr, tbl->lq_type);
223662306a36Sopenharmony_ci
223762306a36Sopenharmony_ci	IWL_DEBUG_RATE(priv, "mask 0x%04X\n", rate_mask);
223862306a36Sopenharmony_ci
223962306a36Sopenharmony_ci	/* mask with station rate restriction */
224062306a36Sopenharmony_ci	if (is_legacy(tbl->lq_type)) {
224162306a36Sopenharmony_ci		if (lq_sta->band == NL80211_BAND_5GHZ)
224262306a36Sopenharmony_ci			/* supp_rates has no CCK bits in A mode */
224362306a36Sopenharmony_ci			rate_scale_index_msk = (u16) (rate_mask &
224462306a36Sopenharmony_ci				(lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
224562306a36Sopenharmony_ci		else
224662306a36Sopenharmony_ci			rate_scale_index_msk = (u16) (rate_mask &
224762306a36Sopenharmony_ci						      lq_sta->supp_rates);
224862306a36Sopenharmony_ci
224962306a36Sopenharmony_ci	} else
225062306a36Sopenharmony_ci		rate_scale_index_msk = rate_mask;
225162306a36Sopenharmony_ci
225262306a36Sopenharmony_ci	if (!rate_scale_index_msk)
225362306a36Sopenharmony_ci		rate_scale_index_msk = rate_mask;
225462306a36Sopenharmony_ci
225562306a36Sopenharmony_ci	if (!((1 << index) & rate_scale_index_msk)) {
225662306a36Sopenharmony_ci		IWL_ERR(priv, "Current Rate is not valid\n");
225762306a36Sopenharmony_ci		if (lq_sta->search_better_tbl) {
225862306a36Sopenharmony_ci			/* revert to active table if search table is not valid*/
225962306a36Sopenharmony_ci			tbl->lq_type = LQ_NONE;
226062306a36Sopenharmony_ci			lq_sta->search_better_tbl = 0;
226162306a36Sopenharmony_ci			tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
226262306a36Sopenharmony_ci			/* get "active" rate info */
226362306a36Sopenharmony_ci			index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
226462306a36Sopenharmony_ci			rs_update_rate_tbl(priv, ctx, lq_sta, tbl,
226562306a36Sopenharmony_ci					   index, is_green);
226662306a36Sopenharmony_ci		}
226762306a36Sopenharmony_ci		return;
226862306a36Sopenharmony_ci	}
226962306a36Sopenharmony_ci
227062306a36Sopenharmony_ci	/* Get expected throughput table and history window for current rate */
227162306a36Sopenharmony_ci	if (!tbl->expected_tpt) {
227262306a36Sopenharmony_ci		IWL_ERR(priv, "tbl->expected_tpt is NULL\n");
227362306a36Sopenharmony_ci		return;
227462306a36Sopenharmony_ci	}
227562306a36Sopenharmony_ci
227662306a36Sopenharmony_ci	/* force user max rate if set by user */
227762306a36Sopenharmony_ci	if ((lq_sta->max_rate_idx != -1) &&
227862306a36Sopenharmony_ci	    (lq_sta->max_rate_idx < index)) {
227962306a36Sopenharmony_ci		index = lq_sta->max_rate_idx;
228062306a36Sopenharmony_ci		update_lq = 1;
228162306a36Sopenharmony_ci		window = &(tbl->win[index]);
228262306a36Sopenharmony_ci		goto lq_update;
228362306a36Sopenharmony_ci	}
228462306a36Sopenharmony_ci
228562306a36Sopenharmony_ci	window = &(tbl->win[index]);
228662306a36Sopenharmony_ci
228762306a36Sopenharmony_ci	/*
228862306a36Sopenharmony_ci	 * If there is not enough history to calculate actual average
228962306a36Sopenharmony_ci	 * throughput, keep analyzing results of more tx frames, without
229062306a36Sopenharmony_ci	 * changing rate or mode (bypass most of the rest of this function).
229162306a36Sopenharmony_ci	 * Set up new rate table in uCode only if old rate is not supported
229262306a36Sopenharmony_ci	 * in current association (use new rate found above).
229362306a36Sopenharmony_ci	 */
229462306a36Sopenharmony_ci	fail_count = window->counter - window->success_counter;
229562306a36Sopenharmony_ci	if ((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
229662306a36Sopenharmony_ci			(window->success_counter < IWL_RATE_MIN_SUCCESS_TH)) {
229762306a36Sopenharmony_ci		IWL_DEBUG_RATE(priv, "LQ: still below TH. succ=%d total=%d "
229862306a36Sopenharmony_ci			       "for index %d\n",
229962306a36Sopenharmony_ci			       window->success_counter, window->counter, index);
230062306a36Sopenharmony_ci
230162306a36Sopenharmony_ci		/* Can't calculate this yet; not enough history */
230262306a36Sopenharmony_ci		window->average_tpt = IWL_INVALID_VALUE;
230362306a36Sopenharmony_ci
230462306a36Sopenharmony_ci		/* Should we stay with this modulation mode,
230562306a36Sopenharmony_ci		 * or search for a new one? */
230662306a36Sopenharmony_ci		rs_stay_in_table(lq_sta, false);
230762306a36Sopenharmony_ci
230862306a36Sopenharmony_ci		goto out;
230962306a36Sopenharmony_ci	}
231062306a36Sopenharmony_ci	/* Else we have enough samples; calculate estimate of
231162306a36Sopenharmony_ci	 * actual average throughput */
231262306a36Sopenharmony_ci	if (window->average_tpt != ((window->success_ratio *
231362306a36Sopenharmony_ci			tbl->expected_tpt[index] + 64) / 128)) {
231462306a36Sopenharmony_ci		IWL_ERR(priv, "expected_tpt should have been calculated by now\n");
231562306a36Sopenharmony_ci		window->average_tpt = ((window->success_ratio *
231662306a36Sopenharmony_ci					tbl->expected_tpt[index] + 64) / 128);
231762306a36Sopenharmony_ci	}
231862306a36Sopenharmony_ci
231962306a36Sopenharmony_ci	/* If we are searching for better modulation mode, check success. */
232062306a36Sopenharmony_ci	if (lq_sta->search_better_tbl &&
232162306a36Sopenharmony_ci	    (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_MULTI)) {
232262306a36Sopenharmony_ci		/* If good success, continue using the "search" mode;
232362306a36Sopenharmony_ci		 * no need to send new link quality command, since we're
232462306a36Sopenharmony_ci		 * continuing to use the setup that we've been trying. */
232562306a36Sopenharmony_ci		if (window->average_tpt > lq_sta->last_tpt) {
232662306a36Sopenharmony_ci
232762306a36Sopenharmony_ci			IWL_DEBUG_RATE(priv, "LQ: SWITCHING TO NEW TABLE "
232862306a36Sopenharmony_ci					"suc=%d cur-tpt=%d old-tpt=%d\n",
232962306a36Sopenharmony_ci					window->success_ratio,
233062306a36Sopenharmony_ci					window->average_tpt,
233162306a36Sopenharmony_ci					lq_sta->last_tpt);
233262306a36Sopenharmony_ci
233362306a36Sopenharmony_ci			if (!is_legacy(tbl->lq_type))
233462306a36Sopenharmony_ci				lq_sta->enable_counter = 1;
233562306a36Sopenharmony_ci
233662306a36Sopenharmony_ci			/* Swap tables; "search" becomes "active" */
233762306a36Sopenharmony_ci			lq_sta->active_tbl = active_tbl;
233862306a36Sopenharmony_ci			current_tpt = window->average_tpt;
233962306a36Sopenharmony_ci
234062306a36Sopenharmony_ci		/* Else poor success; go back to mode in "active" table */
234162306a36Sopenharmony_ci		} else {
234262306a36Sopenharmony_ci
234362306a36Sopenharmony_ci			IWL_DEBUG_RATE(priv, "LQ: GOING BACK TO THE OLD TABLE "
234462306a36Sopenharmony_ci					"suc=%d cur-tpt=%d old-tpt=%d\n",
234562306a36Sopenharmony_ci					window->success_ratio,
234662306a36Sopenharmony_ci					window->average_tpt,
234762306a36Sopenharmony_ci					lq_sta->last_tpt);
234862306a36Sopenharmony_ci
234962306a36Sopenharmony_ci			/* Nullify "search" table */
235062306a36Sopenharmony_ci			tbl->lq_type = LQ_NONE;
235162306a36Sopenharmony_ci
235262306a36Sopenharmony_ci			/* Revert to "active" table */
235362306a36Sopenharmony_ci			active_tbl = lq_sta->active_tbl;
235462306a36Sopenharmony_ci			tbl = &(lq_sta->lq_info[active_tbl]);
235562306a36Sopenharmony_ci
235662306a36Sopenharmony_ci			/* Revert to "active" rate and throughput info */
235762306a36Sopenharmony_ci			index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
235862306a36Sopenharmony_ci			current_tpt = lq_sta->last_tpt;
235962306a36Sopenharmony_ci
236062306a36Sopenharmony_ci			/* Need to set up a new rate table in uCode */
236162306a36Sopenharmony_ci			update_lq = 1;
236262306a36Sopenharmony_ci		}
236362306a36Sopenharmony_ci
236462306a36Sopenharmony_ci		/* Either way, we've made a decision; modulation mode
236562306a36Sopenharmony_ci		 * search is done, allow rate adjustment next time. */
236662306a36Sopenharmony_ci		lq_sta->search_better_tbl = 0;
236762306a36Sopenharmony_ci		done_search = 1;	/* Don't switch modes below! */
236862306a36Sopenharmony_ci		goto lq_update;
236962306a36Sopenharmony_ci	}
237062306a36Sopenharmony_ci
237162306a36Sopenharmony_ci	/* (Else) not in search of better modulation mode, try for better
237262306a36Sopenharmony_ci	 * starting rate, while staying in this mode. */
237362306a36Sopenharmony_ci	high_low = rs_get_adjacent_rate(priv, index, rate_scale_index_msk,
237462306a36Sopenharmony_ci					tbl->lq_type);
237562306a36Sopenharmony_ci	low = high_low & 0xff;
237662306a36Sopenharmony_ci	high = (high_low >> 8) & 0xff;
237762306a36Sopenharmony_ci
237862306a36Sopenharmony_ci	/* If user set max rate, dont allow higher than user constrain */
237962306a36Sopenharmony_ci	if ((lq_sta->max_rate_idx != -1) &&
238062306a36Sopenharmony_ci	    (lq_sta->max_rate_idx < high))
238162306a36Sopenharmony_ci		high = IWL_RATE_INVALID;
238262306a36Sopenharmony_ci
238362306a36Sopenharmony_ci	sr = window->success_ratio;
238462306a36Sopenharmony_ci
238562306a36Sopenharmony_ci	/* Collect measured throughputs for current and adjacent rates */
238662306a36Sopenharmony_ci	current_tpt = window->average_tpt;
238762306a36Sopenharmony_ci	if (low != IWL_RATE_INVALID)
238862306a36Sopenharmony_ci		low_tpt = tbl->win[low].average_tpt;
238962306a36Sopenharmony_ci	if (high != IWL_RATE_INVALID)
239062306a36Sopenharmony_ci		high_tpt = tbl->win[high].average_tpt;
239162306a36Sopenharmony_ci
239262306a36Sopenharmony_ci	scale_action = 0;
239362306a36Sopenharmony_ci
239462306a36Sopenharmony_ci	/* Too many failures, decrease rate */
239562306a36Sopenharmony_ci	if ((sr <= IWL_RATE_DECREASE_TH) || (current_tpt == 0)) {
239662306a36Sopenharmony_ci		IWL_DEBUG_RATE(priv, "decrease rate because of low success_ratio\n");
239762306a36Sopenharmony_ci		scale_action = -1;
239862306a36Sopenharmony_ci
239962306a36Sopenharmony_ci	/* No throughput measured yet for adjacent rates; try increase. */
240062306a36Sopenharmony_ci	} else if ((low_tpt == IWL_INVALID_VALUE) &&
240162306a36Sopenharmony_ci		   (high_tpt == IWL_INVALID_VALUE)) {
240262306a36Sopenharmony_ci
240362306a36Sopenharmony_ci		if (high != IWL_RATE_INVALID && sr >= IWL_RATE_INCREASE_TH)
240462306a36Sopenharmony_ci			scale_action = 1;
240562306a36Sopenharmony_ci		else if (low != IWL_RATE_INVALID)
240662306a36Sopenharmony_ci			scale_action = 0;
240762306a36Sopenharmony_ci	}
240862306a36Sopenharmony_ci
240962306a36Sopenharmony_ci	/* Both adjacent throughputs are measured, but neither one has better
241062306a36Sopenharmony_ci	 * throughput; we're using the best rate, don't change it! */
241162306a36Sopenharmony_ci	else if ((low_tpt != IWL_INVALID_VALUE) &&
241262306a36Sopenharmony_ci		 (high_tpt != IWL_INVALID_VALUE) &&
241362306a36Sopenharmony_ci		 (low_tpt < current_tpt) &&
241462306a36Sopenharmony_ci		 (high_tpt < current_tpt))
241562306a36Sopenharmony_ci		scale_action = 0;
241662306a36Sopenharmony_ci
241762306a36Sopenharmony_ci	/* At least one adjacent rate's throughput is measured,
241862306a36Sopenharmony_ci	 * and may have better performance. */
241962306a36Sopenharmony_ci	else {
242062306a36Sopenharmony_ci		/* Higher adjacent rate's throughput is measured */
242162306a36Sopenharmony_ci		if (high_tpt != IWL_INVALID_VALUE) {
242262306a36Sopenharmony_ci			/* Higher rate has better throughput */
242362306a36Sopenharmony_ci			if (high_tpt > current_tpt &&
242462306a36Sopenharmony_ci					sr >= IWL_RATE_INCREASE_TH) {
242562306a36Sopenharmony_ci				scale_action = 1;
242662306a36Sopenharmony_ci			} else {
242762306a36Sopenharmony_ci				scale_action = 0;
242862306a36Sopenharmony_ci			}
242962306a36Sopenharmony_ci
243062306a36Sopenharmony_ci		/* Lower adjacent rate's throughput is measured */
243162306a36Sopenharmony_ci		} else if (low_tpt != IWL_INVALID_VALUE) {
243262306a36Sopenharmony_ci			/* Lower rate has better throughput */
243362306a36Sopenharmony_ci			if (low_tpt > current_tpt) {
243462306a36Sopenharmony_ci				IWL_DEBUG_RATE(priv,
243562306a36Sopenharmony_ci				    "decrease rate because of low tpt\n");
243662306a36Sopenharmony_ci				scale_action = -1;
243762306a36Sopenharmony_ci			} else if (sr >= IWL_RATE_INCREASE_TH) {
243862306a36Sopenharmony_ci				scale_action = 1;
243962306a36Sopenharmony_ci			}
244062306a36Sopenharmony_ci		}
244162306a36Sopenharmony_ci	}
244262306a36Sopenharmony_ci
244362306a36Sopenharmony_ci	/* Sanity check; asked for decrease, but success rate or throughput
244462306a36Sopenharmony_ci	 * has been good at old rate.  Don't change it. */
244562306a36Sopenharmony_ci	if ((scale_action == -1) && (low != IWL_RATE_INVALID) &&
244662306a36Sopenharmony_ci		    ((sr > IWL_RATE_HIGH_TH) ||
244762306a36Sopenharmony_ci		     (current_tpt > (100 * tbl->expected_tpt[low]))))
244862306a36Sopenharmony_ci		scale_action = 0;
244962306a36Sopenharmony_ci	if (!iwl_ht_enabled(priv) && !is_legacy(tbl->lq_type))
245062306a36Sopenharmony_ci		scale_action = -1;
245162306a36Sopenharmony_ci	if (iwl_tx_ant_restriction(priv) != IWL_ANT_OK_MULTI &&
245262306a36Sopenharmony_ci		(is_mimo2(tbl->lq_type) || is_mimo3(tbl->lq_type)))
245362306a36Sopenharmony_ci		scale_action = -1;
245462306a36Sopenharmony_ci
245562306a36Sopenharmony_ci	if ((priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH) &&
245662306a36Sopenharmony_ci	     (is_mimo2(tbl->lq_type) || is_mimo3(tbl->lq_type))) {
245762306a36Sopenharmony_ci		if (lq_sta->last_bt_traffic > priv->bt_traffic_load) {
245862306a36Sopenharmony_ci			/*
245962306a36Sopenharmony_ci			 * don't set scale_action, don't want to scale up if
246062306a36Sopenharmony_ci			 * the rate scale doesn't otherwise think that is a
246162306a36Sopenharmony_ci			 * good idea.
246262306a36Sopenharmony_ci			 */
246362306a36Sopenharmony_ci		} else if (lq_sta->last_bt_traffic <= priv->bt_traffic_load) {
246462306a36Sopenharmony_ci			scale_action = -1;
246562306a36Sopenharmony_ci		}
246662306a36Sopenharmony_ci	}
246762306a36Sopenharmony_ci	lq_sta->last_bt_traffic = priv->bt_traffic_load;
246862306a36Sopenharmony_ci
246962306a36Sopenharmony_ci	if ((priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH) &&
247062306a36Sopenharmony_ci	     (is_mimo2(tbl->lq_type) || is_mimo3(tbl->lq_type))) {
247162306a36Sopenharmony_ci		/* search for a new modulation */
247262306a36Sopenharmony_ci		rs_stay_in_table(lq_sta, true);
247362306a36Sopenharmony_ci		goto lq_update;
247462306a36Sopenharmony_ci	}
247562306a36Sopenharmony_ci
247662306a36Sopenharmony_ci	switch (scale_action) {
247762306a36Sopenharmony_ci	case -1:
247862306a36Sopenharmony_ci		/* Decrease starting rate, update uCode's rate table */
247962306a36Sopenharmony_ci		if (low != IWL_RATE_INVALID) {
248062306a36Sopenharmony_ci			update_lq = 1;
248162306a36Sopenharmony_ci			index = low;
248262306a36Sopenharmony_ci		}
248362306a36Sopenharmony_ci
248462306a36Sopenharmony_ci		break;
248562306a36Sopenharmony_ci	case 1:
248662306a36Sopenharmony_ci		/* Increase starting rate, update uCode's rate table */
248762306a36Sopenharmony_ci		if (high != IWL_RATE_INVALID) {
248862306a36Sopenharmony_ci			update_lq = 1;
248962306a36Sopenharmony_ci			index = high;
249062306a36Sopenharmony_ci		}
249162306a36Sopenharmony_ci
249262306a36Sopenharmony_ci		break;
249362306a36Sopenharmony_ci	case 0:
249462306a36Sopenharmony_ci		/* No change */
249562306a36Sopenharmony_ci	default:
249662306a36Sopenharmony_ci		break;
249762306a36Sopenharmony_ci	}
249862306a36Sopenharmony_ci
249962306a36Sopenharmony_ci	IWL_DEBUG_RATE(priv, "choose rate scale index %d action %d low %d "
250062306a36Sopenharmony_ci		    "high %d type %d\n",
250162306a36Sopenharmony_ci		     index, scale_action, low, high, tbl->lq_type);
250262306a36Sopenharmony_ci
250362306a36Sopenharmony_cilq_update:
250462306a36Sopenharmony_ci	/* Replace uCode's rate table for the destination station. */
250562306a36Sopenharmony_ci	if (update_lq)
250662306a36Sopenharmony_ci		rs_update_rate_tbl(priv, ctx, lq_sta, tbl, index, is_green);
250762306a36Sopenharmony_ci
250862306a36Sopenharmony_ci	if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_MULTI) {
250962306a36Sopenharmony_ci		/* Should we stay with this modulation mode,
251062306a36Sopenharmony_ci		 * or search for a new one? */
251162306a36Sopenharmony_ci	  rs_stay_in_table(lq_sta, false);
251262306a36Sopenharmony_ci	}
251362306a36Sopenharmony_ci	/*
251462306a36Sopenharmony_ci	 * Search for new modulation mode if we're:
251562306a36Sopenharmony_ci	 * 1)  Not changing rates right now
251662306a36Sopenharmony_ci	 * 2)  Not just finishing up a search
251762306a36Sopenharmony_ci	 * 3)  Allowing a new search
251862306a36Sopenharmony_ci	 */
251962306a36Sopenharmony_ci	if (!update_lq && !done_search && !lq_sta->stay_in_tbl && window->counter) {
252062306a36Sopenharmony_ci		/* Save current throughput to compare with "search" throughput*/
252162306a36Sopenharmony_ci		lq_sta->last_tpt = current_tpt;
252262306a36Sopenharmony_ci
252362306a36Sopenharmony_ci		/* Select a new "search" modulation mode to try.
252462306a36Sopenharmony_ci		 * If one is found, set up the new "search" table. */
252562306a36Sopenharmony_ci		if (is_legacy(tbl->lq_type))
252662306a36Sopenharmony_ci			rs_move_legacy_other(priv, lq_sta, conf, sta, index);
252762306a36Sopenharmony_ci		else if (is_siso(tbl->lq_type))
252862306a36Sopenharmony_ci			rs_move_siso_to_other(priv, lq_sta, conf, sta, index);
252962306a36Sopenharmony_ci		else if (is_mimo2(tbl->lq_type))
253062306a36Sopenharmony_ci			rs_move_mimo2_to_other(priv, lq_sta, conf, sta, index);
253162306a36Sopenharmony_ci		else
253262306a36Sopenharmony_ci			rs_move_mimo3_to_other(priv, lq_sta, conf, sta, index);
253362306a36Sopenharmony_ci
253462306a36Sopenharmony_ci		/* If new "search" mode was selected, set up in uCode table */
253562306a36Sopenharmony_ci		if (lq_sta->search_better_tbl) {
253662306a36Sopenharmony_ci			/* Access the "search" table, clear its history. */
253762306a36Sopenharmony_ci			tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
253862306a36Sopenharmony_ci			for (i = 0; i < IWL_RATE_COUNT; i++)
253962306a36Sopenharmony_ci				rs_rate_scale_clear_window(&(tbl->win[i]));
254062306a36Sopenharmony_ci
254162306a36Sopenharmony_ci			/* Use new "search" start rate */
254262306a36Sopenharmony_ci			index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
254362306a36Sopenharmony_ci
254462306a36Sopenharmony_ci			IWL_DEBUG_RATE(priv, "Switch current  mcs: %X index: %d\n",
254562306a36Sopenharmony_ci				     tbl->current_rate, index);
254662306a36Sopenharmony_ci			rs_fill_link_cmd(priv, lq_sta, tbl->current_rate);
254762306a36Sopenharmony_ci			iwl_send_lq_cmd(priv, ctx, &lq_sta->lq, CMD_ASYNC, false);
254862306a36Sopenharmony_ci		} else
254962306a36Sopenharmony_ci			done_search = 1;
255062306a36Sopenharmony_ci	}
255162306a36Sopenharmony_ci
255262306a36Sopenharmony_ci	if (done_search && !lq_sta->stay_in_tbl) {
255362306a36Sopenharmony_ci		/* If the "active" (non-search) mode was legacy,
255462306a36Sopenharmony_ci		 * and we've tried switching antennas,
255562306a36Sopenharmony_ci		 * but we haven't been able to try HT modes (not available),
255662306a36Sopenharmony_ci		 * stay with best antenna legacy modulation for a while
255762306a36Sopenharmony_ci		 * before next round of mode comparisons. */
255862306a36Sopenharmony_ci		tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
255962306a36Sopenharmony_ci		if (is_legacy(tbl1->lq_type) && !conf_is_ht(conf) &&
256062306a36Sopenharmony_ci		    lq_sta->action_counter > tbl1->max_search) {
256162306a36Sopenharmony_ci			IWL_DEBUG_RATE(priv, "LQ: STAY in legacy table\n");
256262306a36Sopenharmony_ci			rs_set_stay_in_table(priv, 1, lq_sta);
256362306a36Sopenharmony_ci		}
256462306a36Sopenharmony_ci
256562306a36Sopenharmony_ci		/* If we're in an HT mode, and all 3 mode switch actions
256662306a36Sopenharmony_ci		 * have been tried and compared, stay in this best modulation
256762306a36Sopenharmony_ci		 * mode for a while before next round of mode comparisons. */
256862306a36Sopenharmony_ci		if (lq_sta->enable_counter &&
256962306a36Sopenharmony_ci		    (lq_sta->action_counter >= tbl1->max_search) &&
257062306a36Sopenharmony_ci		    iwl_ht_enabled(priv)) {
257162306a36Sopenharmony_ci			if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
257262306a36Sopenharmony_ci			    (lq_sta->tx_agg_tid_en & (1 << tid)) &&
257362306a36Sopenharmony_ci			    (tid != IWL_MAX_TID_COUNT)) {
257462306a36Sopenharmony_ci				u8 sta_id = lq_sta->lq.sta_id;
257562306a36Sopenharmony_ci				tid_data = &priv->tid_data[sta_id][tid];
257662306a36Sopenharmony_ci				if (tid_data->agg.state == IWL_AGG_OFF) {
257762306a36Sopenharmony_ci					IWL_DEBUG_RATE(priv,
257862306a36Sopenharmony_ci						       "try to aggregate tid %d\n",
257962306a36Sopenharmony_ci						       tid);
258062306a36Sopenharmony_ci					rs_tl_turn_on_agg(priv, tid,
258162306a36Sopenharmony_ci							  lq_sta, sta);
258262306a36Sopenharmony_ci				}
258362306a36Sopenharmony_ci			}
258462306a36Sopenharmony_ci			rs_set_stay_in_table(priv, 0, lq_sta);
258562306a36Sopenharmony_ci		}
258662306a36Sopenharmony_ci	}
258762306a36Sopenharmony_ci
258862306a36Sopenharmony_ciout:
258962306a36Sopenharmony_ci	tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
259062306a36Sopenharmony_ci	lq_sta->last_txrate_idx = index;
259162306a36Sopenharmony_ci}
259262306a36Sopenharmony_ci
259362306a36Sopenharmony_ci/*
259462306a36Sopenharmony_ci * rs_initialize_lq - Initialize a station's hardware rate table
259562306a36Sopenharmony_ci *
259662306a36Sopenharmony_ci * The uCode's station table contains a table of fallback rates
259762306a36Sopenharmony_ci * for automatic fallback during transmission.
259862306a36Sopenharmony_ci *
259962306a36Sopenharmony_ci * NOTE: This sets up a default set of values.  These will be replaced later
260062306a36Sopenharmony_ci *       if the driver's iwl-agn-rs rate scaling algorithm is used, instead of
260162306a36Sopenharmony_ci *       rc80211_simple.
260262306a36Sopenharmony_ci *
260362306a36Sopenharmony_ci * NOTE: Run REPLY_ADD_STA command to set up station table entry, before
260462306a36Sopenharmony_ci *       calling this function (which runs REPLY_TX_LINK_QUALITY_CMD,
260562306a36Sopenharmony_ci *       which requires station table entry to exist).
260662306a36Sopenharmony_ci */
260762306a36Sopenharmony_cistatic void rs_initialize_lq(struct iwl_priv *priv,
260862306a36Sopenharmony_ci			     struct ieee80211_sta *sta,
260962306a36Sopenharmony_ci			     struct iwl_lq_sta *lq_sta)
261062306a36Sopenharmony_ci{
261162306a36Sopenharmony_ci	struct iwl_scale_tbl_info *tbl;
261262306a36Sopenharmony_ci	int rate_idx;
261362306a36Sopenharmony_ci	int i;
261462306a36Sopenharmony_ci	u32 rate;
261562306a36Sopenharmony_ci	u8 use_green = rs_use_green(sta);
261662306a36Sopenharmony_ci	u8 active_tbl = 0;
261762306a36Sopenharmony_ci	u8 valid_tx_ant;
261862306a36Sopenharmony_ci	struct iwl_station_priv *sta_priv;
261962306a36Sopenharmony_ci	struct iwl_rxon_context *ctx;
262062306a36Sopenharmony_ci
262162306a36Sopenharmony_ci	if (!sta || !lq_sta)
262262306a36Sopenharmony_ci		return;
262362306a36Sopenharmony_ci
262462306a36Sopenharmony_ci	sta_priv = (void *)sta->drv_priv;
262562306a36Sopenharmony_ci	ctx = sta_priv->ctx;
262662306a36Sopenharmony_ci
262762306a36Sopenharmony_ci	i = lq_sta->last_txrate_idx;
262862306a36Sopenharmony_ci
262962306a36Sopenharmony_ci	valid_tx_ant = priv->nvm_data->valid_tx_ant;
263062306a36Sopenharmony_ci
263162306a36Sopenharmony_ci	if (!lq_sta->search_better_tbl)
263262306a36Sopenharmony_ci		active_tbl = lq_sta->active_tbl;
263362306a36Sopenharmony_ci	else
263462306a36Sopenharmony_ci		active_tbl = 1 - lq_sta->active_tbl;
263562306a36Sopenharmony_ci
263662306a36Sopenharmony_ci	tbl = &(lq_sta->lq_info[active_tbl]);
263762306a36Sopenharmony_ci
263862306a36Sopenharmony_ci	if ((i < 0) || (i >= IWL_RATE_COUNT))
263962306a36Sopenharmony_ci		i = 0;
264062306a36Sopenharmony_ci
264162306a36Sopenharmony_ci	rate = iwl_rates[i].plcp;
264262306a36Sopenharmony_ci	tbl->ant_type = first_antenna(valid_tx_ant);
264362306a36Sopenharmony_ci	rate |= tbl->ant_type << RATE_MCS_ANT_POS;
264462306a36Sopenharmony_ci
264562306a36Sopenharmony_ci	if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
264662306a36Sopenharmony_ci		rate |= RATE_MCS_CCK_MSK;
264762306a36Sopenharmony_ci
264862306a36Sopenharmony_ci	rs_get_tbl_info_from_mcs(rate, priv->band, tbl, &rate_idx);
264962306a36Sopenharmony_ci	if (!rs_is_valid_ant(valid_tx_ant, tbl->ant_type))
265062306a36Sopenharmony_ci	    rs_toggle_antenna(valid_tx_ant, &rate, tbl);
265162306a36Sopenharmony_ci
265262306a36Sopenharmony_ci	rate = rate_n_flags_from_tbl(priv, tbl, rate_idx, use_green);
265362306a36Sopenharmony_ci	tbl->current_rate = rate;
265462306a36Sopenharmony_ci	rs_set_expected_tpt_table(lq_sta, tbl);
265562306a36Sopenharmony_ci	rs_fill_link_cmd(NULL, lq_sta, rate);
265662306a36Sopenharmony_ci	priv->stations[lq_sta->lq.sta_id].lq = &lq_sta->lq;
265762306a36Sopenharmony_ci	iwl_send_lq_cmd(priv, ctx, &lq_sta->lq, 0, true);
265862306a36Sopenharmony_ci}
265962306a36Sopenharmony_ci
266062306a36Sopenharmony_cistatic void rs_get_rate(void *priv_r, struct ieee80211_sta *sta, void *priv_sta,
266162306a36Sopenharmony_ci			struct ieee80211_tx_rate_control *txrc)
266262306a36Sopenharmony_ci{
266362306a36Sopenharmony_ci
266462306a36Sopenharmony_ci	struct sk_buff *skb = txrc->skb;
266562306a36Sopenharmony_ci	struct ieee80211_supported_band *sband = txrc->sband;
266662306a36Sopenharmony_ci	struct iwl_op_mode *op_mode __maybe_unused =
266762306a36Sopenharmony_ci			(struct iwl_op_mode *)priv_r;
266862306a36Sopenharmony_ci	struct iwl_priv *priv __maybe_unused = IWL_OP_MODE_GET_DVM(op_mode);
266962306a36Sopenharmony_ci	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
267062306a36Sopenharmony_ci	struct iwl_lq_sta *lq_sta = priv_sta;
267162306a36Sopenharmony_ci	int rate_idx;
267262306a36Sopenharmony_ci
267362306a36Sopenharmony_ci	IWL_DEBUG_RATE_LIMIT(priv, "rate scale calculate new rate for skb\n");
267462306a36Sopenharmony_ci
267562306a36Sopenharmony_ci	/* Get max rate if user set max rate */
267662306a36Sopenharmony_ci	if (lq_sta) {
267762306a36Sopenharmony_ci		lq_sta->max_rate_idx = fls(txrc->rate_idx_mask) - 1;
267862306a36Sopenharmony_ci		if ((sband->band == NL80211_BAND_5GHZ) &&
267962306a36Sopenharmony_ci		    (lq_sta->max_rate_idx != -1))
268062306a36Sopenharmony_ci			lq_sta->max_rate_idx += IWL_FIRST_OFDM_RATE;
268162306a36Sopenharmony_ci		if ((lq_sta->max_rate_idx < 0) ||
268262306a36Sopenharmony_ci		    (lq_sta->max_rate_idx >= IWL_RATE_COUNT))
268362306a36Sopenharmony_ci			lq_sta->max_rate_idx = -1;
268462306a36Sopenharmony_ci	}
268562306a36Sopenharmony_ci
268662306a36Sopenharmony_ci	/* Treat uninitialized rate scaling data same as non-existing. */
268762306a36Sopenharmony_ci	if (lq_sta && !lq_sta->drv) {
268862306a36Sopenharmony_ci		IWL_DEBUG_RATE(priv, "Rate scaling not initialized yet.\n");
268962306a36Sopenharmony_ci		priv_sta = NULL;
269062306a36Sopenharmony_ci	}
269162306a36Sopenharmony_ci
269262306a36Sopenharmony_ci	rate_idx  = lq_sta->last_txrate_idx;
269362306a36Sopenharmony_ci
269462306a36Sopenharmony_ci	if (lq_sta->last_rate_n_flags & RATE_MCS_HT_MSK) {
269562306a36Sopenharmony_ci		rate_idx -= IWL_FIRST_OFDM_RATE;
269662306a36Sopenharmony_ci		/* 6M and 9M shared same MCS index */
269762306a36Sopenharmony_ci		rate_idx = (rate_idx > 0) ? (rate_idx - 1) : 0;
269862306a36Sopenharmony_ci		if (rs_extract_rate(lq_sta->last_rate_n_flags) >=
269962306a36Sopenharmony_ci		    IWL_RATE_MIMO3_6M_PLCP)
270062306a36Sopenharmony_ci			rate_idx = rate_idx + (2 * MCS_INDEX_PER_STREAM);
270162306a36Sopenharmony_ci		else if (rs_extract_rate(lq_sta->last_rate_n_flags) >=
270262306a36Sopenharmony_ci			 IWL_RATE_MIMO2_6M_PLCP)
270362306a36Sopenharmony_ci			rate_idx = rate_idx + MCS_INDEX_PER_STREAM;
270462306a36Sopenharmony_ci		info->control.rates[0].flags = IEEE80211_TX_RC_MCS;
270562306a36Sopenharmony_ci		if (lq_sta->last_rate_n_flags & RATE_MCS_SGI_MSK)
270662306a36Sopenharmony_ci			info->control.rates[0].flags |= IEEE80211_TX_RC_SHORT_GI;
270762306a36Sopenharmony_ci		if (lq_sta->last_rate_n_flags & RATE_MCS_DUP_MSK)
270862306a36Sopenharmony_ci			info->control.rates[0].flags |= IEEE80211_TX_RC_DUP_DATA;
270962306a36Sopenharmony_ci		if (lq_sta->last_rate_n_flags & RATE_MCS_HT40_MSK)
271062306a36Sopenharmony_ci			info->control.rates[0].flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
271162306a36Sopenharmony_ci		if (lq_sta->last_rate_n_flags & RATE_MCS_GF_MSK)
271262306a36Sopenharmony_ci			info->control.rates[0].flags |= IEEE80211_TX_RC_GREEN_FIELD;
271362306a36Sopenharmony_ci	} else {
271462306a36Sopenharmony_ci		/* Check for invalid rates */
271562306a36Sopenharmony_ci		if ((rate_idx < 0) || (rate_idx >= IWL_RATE_COUNT_LEGACY) ||
271662306a36Sopenharmony_ci				((sband->band == NL80211_BAND_5GHZ) &&
271762306a36Sopenharmony_ci				 (rate_idx < IWL_FIRST_OFDM_RATE)))
271862306a36Sopenharmony_ci			rate_idx = rate_lowest_index(sband, sta);
271962306a36Sopenharmony_ci		/* On valid 5 GHz rate, adjust index */
272062306a36Sopenharmony_ci		else if (sband->band == NL80211_BAND_5GHZ)
272162306a36Sopenharmony_ci			rate_idx -= IWL_FIRST_OFDM_RATE;
272262306a36Sopenharmony_ci		info->control.rates[0].flags = 0;
272362306a36Sopenharmony_ci	}
272462306a36Sopenharmony_ci	info->control.rates[0].idx = rate_idx;
272562306a36Sopenharmony_ci	info->control.rates[0].count = 1;
272662306a36Sopenharmony_ci}
272762306a36Sopenharmony_ci
272862306a36Sopenharmony_cistatic void *rs_alloc_sta(void *priv_rate, struct ieee80211_sta *sta,
272962306a36Sopenharmony_ci			  gfp_t gfp)
273062306a36Sopenharmony_ci{
273162306a36Sopenharmony_ci	struct iwl_station_priv *sta_priv = (struct iwl_station_priv *) sta->drv_priv;
273262306a36Sopenharmony_ci	struct iwl_op_mode *op_mode __maybe_unused =
273362306a36Sopenharmony_ci			(struct iwl_op_mode *)priv_rate;
273462306a36Sopenharmony_ci	struct iwl_priv *priv __maybe_unused = IWL_OP_MODE_GET_DVM(op_mode);
273562306a36Sopenharmony_ci
273662306a36Sopenharmony_ci	IWL_DEBUG_RATE(priv, "create station rate scale window\n");
273762306a36Sopenharmony_ci
273862306a36Sopenharmony_ci	return &sta_priv->lq_sta;
273962306a36Sopenharmony_ci}
274062306a36Sopenharmony_ci
274162306a36Sopenharmony_ci/*
274262306a36Sopenharmony_ci * Called after adding a new station to initialize rate scaling
274362306a36Sopenharmony_ci */
274462306a36Sopenharmony_civoid iwl_rs_rate_init(struct iwl_priv *priv, struct ieee80211_sta *sta, u8 sta_id)
274562306a36Sopenharmony_ci{
274662306a36Sopenharmony_ci	int i, j;
274762306a36Sopenharmony_ci	struct ieee80211_hw *hw = priv->hw;
274862306a36Sopenharmony_ci	struct ieee80211_conf *conf = &priv->hw->conf;
274962306a36Sopenharmony_ci	struct ieee80211_sta_ht_cap *ht_cap = &sta->deflink.ht_cap;
275062306a36Sopenharmony_ci	struct iwl_station_priv *sta_priv;
275162306a36Sopenharmony_ci	struct iwl_lq_sta *lq_sta;
275262306a36Sopenharmony_ci	struct ieee80211_supported_band *sband;
275362306a36Sopenharmony_ci	unsigned long supp; /* must be unsigned long for for_each_set_bit */
275462306a36Sopenharmony_ci
275562306a36Sopenharmony_ci	sta_priv = (struct iwl_station_priv *) sta->drv_priv;
275662306a36Sopenharmony_ci	lq_sta = &sta_priv->lq_sta;
275762306a36Sopenharmony_ci	sband = hw->wiphy->bands[conf->chandef.chan->band];
275862306a36Sopenharmony_ci
275962306a36Sopenharmony_ci
276062306a36Sopenharmony_ci	lq_sta->lq.sta_id = sta_id;
276162306a36Sopenharmony_ci
276262306a36Sopenharmony_ci	for (j = 0; j < LQ_SIZE; j++)
276362306a36Sopenharmony_ci		for (i = 0; i < IWL_RATE_COUNT; i++)
276462306a36Sopenharmony_ci			rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
276562306a36Sopenharmony_ci
276662306a36Sopenharmony_ci	lq_sta->flush_timer = 0;
276762306a36Sopenharmony_ci	lq_sta->supp_rates = sta->deflink.supp_rates[sband->band];
276862306a36Sopenharmony_ci
276962306a36Sopenharmony_ci	IWL_DEBUG_RATE(priv, "LQ: *** rate scale station global init for station %d ***\n",
277062306a36Sopenharmony_ci		       sta_id);
277162306a36Sopenharmony_ci	/* TODO: what is a good starting rate for STA? About middle? Maybe not
277262306a36Sopenharmony_ci	 * the lowest or the highest rate.. Could consider using RSSI from
277362306a36Sopenharmony_ci	 * previous packets? Need to have IEEE 802.1X auth succeed immediately
277462306a36Sopenharmony_ci	 * after assoc.. */
277562306a36Sopenharmony_ci
277662306a36Sopenharmony_ci	lq_sta->is_dup = 0;
277762306a36Sopenharmony_ci	lq_sta->max_rate_idx = -1;
277862306a36Sopenharmony_ci	lq_sta->missed_rate_counter = IWL_MISSED_RATE_MAX;
277962306a36Sopenharmony_ci	lq_sta->is_green = rs_use_green(sta);
278062306a36Sopenharmony_ci	lq_sta->band = sband->band;
278162306a36Sopenharmony_ci	/*
278262306a36Sopenharmony_ci	 * active legacy rates as per supported rates bitmap
278362306a36Sopenharmony_ci	 */
278462306a36Sopenharmony_ci	supp = sta->deflink.supp_rates[sband->band];
278562306a36Sopenharmony_ci	lq_sta->active_legacy_rate = 0;
278662306a36Sopenharmony_ci	for_each_set_bit(i, &supp, BITS_PER_LONG)
278762306a36Sopenharmony_ci		lq_sta->active_legacy_rate |= BIT(sband->bitrates[i].hw_value);
278862306a36Sopenharmony_ci
278962306a36Sopenharmony_ci	/*
279062306a36Sopenharmony_ci	 * active_siso_rate mask includes 9 MBits (bit 5), and CCK (bits 0-3),
279162306a36Sopenharmony_ci	 * supp_rates[] does not; shift to convert format, force 9 MBits off.
279262306a36Sopenharmony_ci	 */
279362306a36Sopenharmony_ci	lq_sta->active_siso_rate = ht_cap->mcs.rx_mask[0] << 1;
279462306a36Sopenharmony_ci	lq_sta->active_siso_rate |= ht_cap->mcs.rx_mask[0] & 0x1;
279562306a36Sopenharmony_ci	lq_sta->active_siso_rate &= ~((u16)0x2);
279662306a36Sopenharmony_ci	lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE;
279762306a36Sopenharmony_ci
279862306a36Sopenharmony_ci	/* Same here */
279962306a36Sopenharmony_ci	lq_sta->active_mimo2_rate = ht_cap->mcs.rx_mask[1] << 1;
280062306a36Sopenharmony_ci	lq_sta->active_mimo2_rate |= ht_cap->mcs.rx_mask[1] & 0x1;
280162306a36Sopenharmony_ci	lq_sta->active_mimo2_rate &= ~((u16)0x2);
280262306a36Sopenharmony_ci	lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE;
280362306a36Sopenharmony_ci
280462306a36Sopenharmony_ci	lq_sta->active_mimo3_rate = ht_cap->mcs.rx_mask[2] << 1;
280562306a36Sopenharmony_ci	lq_sta->active_mimo3_rate |= ht_cap->mcs.rx_mask[2] & 0x1;
280662306a36Sopenharmony_ci	lq_sta->active_mimo3_rate &= ~((u16)0x2);
280762306a36Sopenharmony_ci	lq_sta->active_mimo3_rate <<= IWL_FIRST_OFDM_RATE;
280862306a36Sopenharmony_ci
280962306a36Sopenharmony_ci	IWL_DEBUG_RATE(priv, "SISO-RATE=%X MIMO2-RATE=%X MIMO3-RATE=%X\n",
281062306a36Sopenharmony_ci		     lq_sta->active_siso_rate,
281162306a36Sopenharmony_ci		     lq_sta->active_mimo2_rate,
281262306a36Sopenharmony_ci		     lq_sta->active_mimo3_rate);
281362306a36Sopenharmony_ci
281462306a36Sopenharmony_ci	/* These values will be overridden later */
281562306a36Sopenharmony_ci	lq_sta->lq.general_params.single_stream_ant_msk =
281662306a36Sopenharmony_ci		first_antenna(priv->nvm_data->valid_tx_ant);
281762306a36Sopenharmony_ci	lq_sta->lq.general_params.dual_stream_ant_msk =
281862306a36Sopenharmony_ci		priv->nvm_data->valid_tx_ant &
281962306a36Sopenharmony_ci		~first_antenna(priv->nvm_data->valid_tx_ant);
282062306a36Sopenharmony_ci	if (!lq_sta->lq.general_params.dual_stream_ant_msk) {
282162306a36Sopenharmony_ci		lq_sta->lq.general_params.dual_stream_ant_msk = ANT_AB;
282262306a36Sopenharmony_ci	} else if (num_of_ant(priv->nvm_data->valid_tx_ant) == 2) {
282362306a36Sopenharmony_ci		lq_sta->lq.general_params.dual_stream_ant_msk =
282462306a36Sopenharmony_ci			priv->nvm_data->valid_tx_ant;
282562306a36Sopenharmony_ci	}
282662306a36Sopenharmony_ci
282762306a36Sopenharmony_ci	/* as default allow aggregation for all tids */
282862306a36Sopenharmony_ci	lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
282962306a36Sopenharmony_ci	lq_sta->drv = priv;
283062306a36Sopenharmony_ci
283162306a36Sopenharmony_ci	/* Set last_txrate_idx to lowest rate */
283262306a36Sopenharmony_ci	lq_sta->last_txrate_idx = rate_lowest_index(sband, sta);
283362306a36Sopenharmony_ci	if (sband->band == NL80211_BAND_5GHZ)
283462306a36Sopenharmony_ci		lq_sta->last_txrate_idx += IWL_FIRST_OFDM_RATE;
283562306a36Sopenharmony_ci	lq_sta->is_agg = 0;
283662306a36Sopenharmony_ci#ifdef CONFIG_MAC80211_DEBUGFS
283762306a36Sopenharmony_ci	lq_sta->dbg_fixed_rate = 0;
283862306a36Sopenharmony_ci#endif
283962306a36Sopenharmony_ci
284062306a36Sopenharmony_ci	rs_initialize_lq(priv, sta, lq_sta);
284162306a36Sopenharmony_ci}
284262306a36Sopenharmony_ci
284362306a36Sopenharmony_cistatic void rs_fill_link_cmd(struct iwl_priv *priv,
284462306a36Sopenharmony_ci			     struct iwl_lq_sta *lq_sta, u32 new_rate)
284562306a36Sopenharmony_ci{
284662306a36Sopenharmony_ci	struct iwl_scale_tbl_info tbl_type;
284762306a36Sopenharmony_ci	int index = 0;
284862306a36Sopenharmony_ci	int rate_idx;
284962306a36Sopenharmony_ci	int repeat_rate = 0;
285062306a36Sopenharmony_ci	u8 ant_toggle_cnt = 0;
285162306a36Sopenharmony_ci	u8 use_ht_possible = 1;
285262306a36Sopenharmony_ci	u8 valid_tx_ant = 0;
285362306a36Sopenharmony_ci	struct iwl_station_priv *sta_priv =
285462306a36Sopenharmony_ci		container_of(lq_sta, struct iwl_station_priv, lq_sta);
285562306a36Sopenharmony_ci	struct iwl_link_quality_cmd *lq_cmd = &lq_sta->lq;
285662306a36Sopenharmony_ci
285762306a36Sopenharmony_ci	/* Override starting rate (index 0) if needed for debug purposes */
285862306a36Sopenharmony_ci	rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
285962306a36Sopenharmony_ci
286062306a36Sopenharmony_ci	/* Interpret new_rate (rate_n_flags) */
286162306a36Sopenharmony_ci	rs_get_tbl_info_from_mcs(new_rate, lq_sta->band,
286262306a36Sopenharmony_ci				  &tbl_type, &rate_idx);
286362306a36Sopenharmony_ci
286462306a36Sopenharmony_ci	if (priv && priv->bt_full_concurrent) {
286562306a36Sopenharmony_ci		/* 1x1 only */
286662306a36Sopenharmony_ci		tbl_type.ant_type =
286762306a36Sopenharmony_ci			first_antenna(priv->nvm_data->valid_tx_ant);
286862306a36Sopenharmony_ci	}
286962306a36Sopenharmony_ci
287062306a36Sopenharmony_ci	/* How many times should we repeat the initial rate? */
287162306a36Sopenharmony_ci	if (is_legacy(tbl_type.lq_type)) {
287262306a36Sopenharmony_ci		ant_toggle_cnt = 1;
287362306a36Sopenharmony_ci		repeat_rate = IWL_NUMBER_TRY;
287462306a36Sopenharmony_ci	} else {
287562306a36Sopenharmony_ci		repeat_rate = min(IWL_HT_NUMBER_TRY,
287662306a36Sopenharmony_ci				  LINK_QUAL_AGG_DISABLE_START_DEF - 1);
287762306a36Sopenharmony_ci	}
287862306a36Sopenharmony_ci
287962306a36Sopenharmony_ci	lq_cmd->general_params.mimo_delimiter =
288062306a36Sopenharmony_ci			is_mimo(tbl_type.lq_type) ? 1 : 0;
288162306a36Sopenharmony_ci
288262306a36Sopenharmony_ci	/* Fill 1st table entry (index 0) */
288362306a36Sopenharmony_ci	lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
288462306a36Sopenharmony_ci
288562306a36Sopenharmony_ci	if (num_of_ant(tbl_type.ant_type) == 1) {
288662306a36Sopenharmony_ci		lq_cmd->general_params.single_stream_ant_msk =
288762306a36Sopenharmony_ci						tbl_type.ant_type;
288862306a36Sopenharmony_ci	} else if (num_of_ant(tbl_type.ant_type) == 2) {
288962306a36Sopenharmony_ci		lq_cmd->general_params.dual_stream_ant_msk =
289062306a36Sopenharmony_ci						tbl_type.ant_type;
289162306a36Sopenharmony_ci	} /* otherwise we don't modify the existing value */
289262306a36Sopenharmony_ci
289362306a36Sopenharmony_ci	index++;
289462306a36Sopenharmony_ci	repeat_rate--;
289562306a36Sopenharmony_ci	if (priv) {
289662306a36Sopenharmony_ci		if (priv->bt_full_concurrent)
289762306a36Sopenharmony_ci			valid_tx_ant = ANT_A;
289862306a36Sopenharmony_ci		else
289962306a36Sopenharmony_ci			valid_tx_ant = priv->nvm_data->valid_tx_ant;
290062306a36Sopenharmony_ci	}
290162306a36Sopenharmony_ci
290262306a36Sopenharmony_ci	/* Fill rest of rate table */
290362306a36Sopenharmony_ci	while (index < LINK_QUAL_MAX_RETRY_NUM) {
290462306a36Sopenharmony_ci		/* Repeat initial/next rate.
290562306a36Sopenharmony_ci		 * For legacy IWL_NUMBER_TRY == 1, this loop will not execute.
290662306a36Sopenharmony_ci		 * For HT IWL_HT_NUMBER_TRY == 3, this executes twice. */
290762306a36Sopenharmony_ci		while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
290862306a36Sopenharmony_ci			if (is_legacy(tbl_type.lq_type)) {
290962306a36Sopenharmony_ci				if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
291062306a36Sopenharmony_ci					ant_toggle_cnt++;
291162306a36Sopenharmony_ci				else if (priv &&
291262306a36Sopenharmony_ci					 rs_toggle_antenna(valid_tx_ant,
291362306a36Sopenharmony_ci							&new_rate, &tbl_type))
291462306a36Sopenharmony_ci					ant_toggle_cnt = 1;
291562306a36Sopenharmony_ci			}
291662306a36Sopenharmony_ci
291762306a36Sopenharmony_ci			/* Override next rate if needed for debug purposes */
291862306a36Sopenharmony_ci			rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
291962306a36Sopenharmony_ci
292062306a36Sopenharmony_ci			/* Fill next table entry */
292162306a36Sopenharmony_ci			lq_cmd->rs_table[index].rate_n_flags =
292262306a36Sopenharmony_ci					cpu_to_le32(new_rate);
292362306a36Sopenharmony_ci			repeat_rate--;
292462306a36Sopenharmony_ci			index++;
292562306a36Sopenharmony_ci		}
292662306a36Sopenharmony_ci
292762306a36Sopenharmony_ci		rs_get_tbl_info_from_mcs(new_rate, lq_sta->band, &tbl_type,
292862306a36Sopenharmony_ci						&rate_idx);
292962306a36Sopenharmony_ci
293062306a36Sopenharmony_ci		if (priv && priv->bt_full_concurrent) {
293162306a36Sopenharmony_ci			/* 1x1 only */
293262306a36Sopenharmony_ci			tbl_type.ant_type =
293362306a36Sopenharmony_ci			    first_antenna(priv->nvm_data->valid_tx_ant);
293462306a36Sopenharmony_ci		}
293562306a36Sopenharmony_ci
293662306a36Sopenharmony_ci		/* Indicate to uCode which entries might be MIMO.
293762306a36Sopenharmony_ci		 * If initial rate was MIMO, this will finally end up
293862306a36Sopenharmony_ci		 * as (IWL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
293962306a36Sopenharmony_ci		if (is_mimo(tbl_type.lq_type))
294062306a36Sopenharmony_ci			lq_cmd->general_params.mimo_delimiter = index;
294162306a36Sopenharmony_ci
294262306a36Sopenharmony_ci		/* Get next rate */
294362306a36Sopenharmony_ci		new_rate = rs_get_lower_rate(lq_sta, &tbl_type, rate_idx,
294462306a36Sopenharmony_ci					     use_ht_possible);
294562306a36Sopenharmony_ci
294662306a36Sopenharmony_ci		/* How many times should we repeat the next rate? */
294762306a36Sopenharmony_ci		if (is_legacy(tbl_type.lq_type)) {
294862306a36Sopenharmony_ci			if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
294962306a36Sopenharmony_ci				ant_toggle_cnt++;
295062306a36Sopenharmony_ci			else if (priv &&
295162306a36Sopenharmony_ci				 rs_toggle_antenna(valid_tx_ant,
295262306a36Sopenharmony_ci						   &new_rate, &tbl_type))
295362306a36Sopenharmony_ci				ant_toggle_cnt = 1;
295462306a36Sopenharmony_ci
295562306a36Sopenharmony_ci			repeat_rate = IWL_NUMBER_TRY;
295662306a36Sopenharmony_ci		} else {
295762306a36Sopenharmony_ci			repeat_rate = IWL_HT_NUMBER_TRY;
295862306a36Sopenharmony_ci		}
295962306a36Sopenharmony_ci
296062306a36Sopenharmony_ci		/* Don't allow HT rates after next pass.
296162306a36Sopenharmony_ci		 * rs_get_lower_rate() will change type to LQ_A or LQ_G. */
296262306a36Sopenharmony_ci		use_ht_possible = 0;
296362306a36Sopenharmony_ci
296462306a36Sopenharmony_ci		/* Override next rate if needed for debug purposes */
296562306a36Sopenharmony_ci		rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
296662306a36Sopenharmony_ci
296762306a36Sopenharmony_ci		/* Fill next table entry */
296862306a36Sopenharmony_ci		lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
296962306a36Sopenharmony_ci
297062306a36Sopenharmony_ci		index++;
297162306a36Sopenharmony_ci		repeat_rate--;
297262306a36Sopenharmony_ci	}
297362306a36Sopenharmony_ci
297462306a36Sopenharmony_ci	lq_cmd->agg_params.agg_frame_cnt_limit =
297562306a36Sopenharmony_ci		sta_priv->max_agg_bufsize ?: LINK_QUAL_AGG_FRAME_LIMIT_DEF;
297662306a36Sopenharmony_ci	lq_cmd->agg_params.agg_dis_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
297762306a36Sopenharmony_ci
297862306a36Sopenharmony_ci	lq_cmd->agg_params.agg_time_limit =
297962306a36Sopenharmony_ci		cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
298062306a36Sopenharmony_ci	/*
298162306a36Sopenharmony_ci	 * overwrite if needed, pass aggregation time limit
298262306a36Sopenharmony_ci	 * to uCode in uSec
298362306a36Sopenharmony_ci	 */
298462306a36Sopenharmony_ci	if (priv && priv->lib->bt_params &&
298562306a36Sopenharmony_ci	    priv->lib->bt_params->agg_time_limit &&
298662306a36Sopenharmony_ci	    priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH)
298762306a36Sopenharmony_ci		lq_cmd->agg_params.agg_time_limit =
298862306a36Sopenharmony_ci			cpu_to_le16(priv->lib->bt_params->agg_time_limit);
298962306a36Sopenharmony_ci}
299062306a36Sopenharmony_ci
299162306a36Sopenharmony_cistatic void *rs_alloc(struct ieee80211_hw *hw)
299262306a36Sopenharmony_ci{
299362306a36Sopenharmony_ci	return hw->priv;
299462306a36Sopenharmony_ci}
299562306a36Sopenharmony_ci/* rate scale requires free function to be implemented */
299662306a36Sopenharmony_cistatic void rs_free(void *priv_rate)
299762306a36Sopenharmony_ci{
299862306a36Sopenharmony_ci	return;
299962306a36Sopenharmony_ci}
300062306a36Sopenharmony_ci
300162306a36Sopenharmony_cistatic void rs_free_sta(void *priv_r, struct ieee80211_sta *sta,
300262306a36Sopenharmony_ci			void *priv_sta)
300362306a36Sopenharmony_ci{
300462306a36Sopenharmony_ci	struct iwl_op_mode *op_mode __maybe_unused = priv_r;
300562306a36Sopenharmony_ci	struct iwl_priv *priv __maybe_unused = IWL_OP_MODE_GET_DVM(op_mode);
300662306a36Sopenharmony_ci
300762306a36Sopenharmony_ci	IWL_DEBUG_RATE(priv, "enter\n");
300862306a36Sopenharmony_ci	IWL_DEBUG_RATE(priv, "leave\n");
300962306a36Sopenharmony_ci}
301062306a36Sopenharmony_ci
301162306a36Sopenharmony_ci#ifdef CONFIG_MAC80211_DEBUGFS
301262306a36Sopenharmony_cistatic void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
301362306a36Sopenharmony_ci			     u32 *rate_n_flags, int index)
301462306a36Sopenharmony_ci{
301562306a36Sopenharmony_ci	struct iwl_priv *priv;
301662306a36Sopenharmony_ci	u8 valid_tx_ant;
301762306a36Sopenharmony_ci	u8 ant_sel_tx;
301862306a36Sopenharmony_ci
301962306a36Sopenharmony_ci	priv = lq_sta->drv;
302062306a36Sopenharmony_ci	valid_tx_ant = priv->nvm_data->valid_tx_ant;
302162306a36Sopenharmony_ci	if (lq_sta->dbg_fixed_rate) {
302262306a36Sopenharmony_ci		ant_sel_tx =
302362306a36Sopenharmony_ci		  ((lq_sta->dbg_fixed_rate & RATE_MCS_ANT_ABC_MSK)
302462306a36Sopenharmony_ci		  >> RATE_MCS_ANT_POS);
302562306a36Sopenharmony_ci		if ((valid_tx_ant & ant_sel_tx) == ant_sel_tx) {
302662306a36Sopenharmony_ci			*rate_n_flags = lq_sta->dbg_fixed_rate;
302762306a36Sopenharmony_ci			IWL_DEBUG_RATE(priv, "Fixed rate ON\n");
302862306a36Sopenharmony_ci		} else {
302962306a36Sopenharmony_ci			lq_sta->dbg_fixed_rate = 0;
303062306a36Sopenharmony_ci			IWL_ERR(priv,
303162306a36Sopenharmony_ci			    "Invalid antenna selection 0x%X, Valid is 0x%X\n",
303262306a36Sopenharmony_ci			    ant_sel_tx, valid_tx_ant);
303362306a36Sopenharmony_ci			IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
303462306a36Sopenharmony_ci		}
303562306a36Sopenharmony_ci	} else {
303662306a36Sopenharmony_ci		IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
303762306a36Sopenharmony_ci	}
303862306a36Sopenharmony_ci}
303962306a36Sopenharmony_ci
304062306a36Sopenharmony_cistatic ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
304162306a36Sopenharmony_ci			const char __user *user_buf, size_t count, loff_t *ppos)
304262306a36Sopenharmony_ci{
304362306a36Sopenharmony_ci	struct iwl_lq_sta *lq_sta = file->private_data;
304462306a36Sopenharmony_ci	struct iwl_priv *priv;
304562306a36Sopenharmony_ci	char buf[64];
304662306a36Sopenharmony_ci	size_t buf_size;
304762306a36Sopenharmony_ci	u32 parsed_rate;
304862306a36Sopenharmony_ci
304962306a36Sopenharmony_ci
305062306a36Sopenharmony_ci	priv = lq_sta->drv;
305162306a36Sopenharmony_ci	memset(buf, 0, sizeof(buf));
305262306a36Sopenharmony_ci	buf_size = min(count, sizeof(buf) -  1);
305362306a36Sopenharmony_ci	if (copy_from_user(buf, user_buf, buf_size))
305462306a36Sopenharmony_ci		return -EFAULT;
305562306a36Sopenharmony_ci
305662306a36Sopenharmony_ci	if (sscanf(buf, "%x", &parsed_rate) == 1)
305762306a36Sopenharmony_ci		lq_sta->dbg_fixed_rate = parsed_rate;
305862306a36Sopenharmony_ci	else
305962306a36Sopenharmony_ci		lq_sta->dbg_fixed_rate = 0;
306062306a36Sopenharmony_ci
306162306a36Sopenharmony_ci	rs_program_fix_rate(priv, lq_sta);
306262306a36Sopenharmony_ci
306362306a36Sopenharmony_ci	return count;
306462306a36Sopenharmony_ci}
306562306a36Sopenharmony_ci
306662306a36Sopenharmony_cistatic ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
306762306a36Sopenharmony_ci			char __user *user_buf, size_t count, loff_t *ppos)
306862306a36Sopenharmony_ci{
306962306a36Sopenharmony_ci	char *buff;
307062306a36Sopenharmony_ci	int desc = 0;
307162306a36Sopenharmony_ci	int i = 0;
307262306a36Sopenharmony_ci	int index = 0;
307362306a36Sopenharmony_ci	ssize_t ret;
307462306a36Sopenharmony_ci
307562306a36Sopenharmony_ci	/* mbps, mcs */
307662306a36Sopenharmony_ci	static const struct iwl_rate_mcs_info iwl_rate_mcs[IWL_RATE_COUNT] = {
307762306a36Sopenharmony_ci		{  "1", "BPSK DSSS"},
307862306a36Sopenharmony_ci		{  "2", "QPSK DSSS"},
307962306a36Sopenharmony_ci		{"5.5", "BPSK CCK"},
308062306a36Sopenharmony_ci		{ "11", "QPSK CCK"},
308162306a36Sopenharmony_ci		{  "6", "BPSK 1/2"},
308262306a36Sopenharmony_ci		{  "9", "BPSK 1/2"},
308362306a36Sopenharmony_ci		{ "12", "QPSK 1/2"},
308462306a36Sopenharmony_ci		{ "18", "QPSK 3/4"},
308562306a36Sopenharmony_ci		{ "24", "16QAM 1/2"},
308662306a36Sopenharmony_ci		{ "36", "16QAM 3/4"},
308762306a36Sopenharmony_ci		{ "48", "64QAM 2/3"},
308862306a36Sopenharmony_ci		{ "54", "64QAM 3/4"},
308962306a36Sopenharmony_ci		{ "60", "64QAM 5/6"},
309062306a36Sopenharmony_ci	};
309162306a36Sopenharmony_ci
309262306a36Sopenharmony_ci	struct iwl_lq_sta *lq_sta = file->private_data;
309362306a36Sopenharmony_ci	struct iwl_priv *priv;
309462306a36Sopenharmony_ci	struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
309562306a36Sopenharmony_ci
309662306a36Sopenharmony_ci	priv = lq_sta->drv;
309762306a36Sopenharmony_ci	buff = kmalloc(1024, GFP_KERNEL);
309862306a36Sopenharmony_ci	if (!buff)
309962306a36Sopenharmony_ci		return -ENOMEM;
310062306a36Sopenharmony_ci
310162306a36Sopenharmony_ci	desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
310262306a36Sopenharmony_ci	desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
310362306a36Sopenharmony_ci			lq_sta->total_failed, lq_sta->total_success,
310462306a36Sopenharmony_ci			lq_sta->active_legacy_rate);
310562306a36Sopenharmony_ci	desc += sprintf(buff+desc, "fixed rate 0x%X\n",
310662306a36Sopenharmony_ci			lq_sta->dbg_fixed_rate);
310762306a36Sopenharmony_ci	desc += sprintf(buff+desc, "valid_tx_ant %s%s%s\n",
310862306a36Sopenharmony_ci	    (priv->nvm_data->valid_tx_ant & ANT_A) ? "ANT_A," : "",
310962306a36Sopenharmony_ci	    (priv->nvm_data->valid_tx_ant & ANT_B) ? "ANT_B," : "",
311062306a36Sopenharmony_ci	    (priv->nvm_data->valid_tx_ant & ANT_C) ? "ANT_C" : "");
311162306a36Sopenharmony_ci	desc += sprintf(buff+desc, "lq type %s\n",
311262306a36Sopenharmony_ci	   (is_legacy(tbl->lq_type)) ? "legacy" : "HT");
311362306a36Sopenharmony_ci	if (is_Ht(tbl->lq_type)) {
311462306a36Sopenharmony_ci		desc += sprintf(buff + desc, " %s",
311562306a36Sopenharmony_ci		   (is_siso(tbl->lq_type)) ? "SISO" :
311662306a36Sopenharmony_ci		   ((is_mimo2(tbl->lq_type)) ? "MIMO2" : "MIMO3"));
311762306a36Sopenharmony_ci		desc += sprintf(buff + desc, " %s",
311862306a36Sopenharmony_ci		   (tbl->is_ht40) ? "40MHz" : "20MHz");
311962306a36Sopenharmony_ci		desc += sprintf(buff + desc, " %s %s %s\n",
312062306a36Sopenharmony_ci		   (tbl->is_SGI) ? "SGI" : "",
312162306a36Sopenharmony_ci		   (lq_sta->is_green) ? "GF enabled" : "",
312262306a36Sopenharmony_ci		   (lq_sta->is_agg) ? "AGG on" : "");
312362306a36Sopenharmony_ci	}
312462306a36Sopenharmony_ci	desc += sprintf(buff+desc, "last tx rate=0x%X\n",
312562306a36Sopenharmony_ci		lq_sta->last_rate_n_flags);
312662306a36Sopenharmony_ci	desc += sprintf(buff+desc, "general:"
312762306a36Sopenharmony_ci		"flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
312862306a36Sopenharmony_ci		lq_sta->lq.general_params.flags,
312962306a36Sopenharmony_ci		lq_sta->lq.general_params.mimo_delimiter,
313062306a36Sopenharmony_ci		lq_sta->lq.general_params.single_stream_ant_msk,
313162306a36Sopenharmony_ci		lq_sta->lq.general_params.dual_stream_ant_msk);
313262306a36Sopenharmony_ci
313362306a36Sopenharmony_ci	desc += sprintf(buff+desc, "agg:"
313462306a36Sopenharmony_ci			"time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
313562306a36Sopenharmony_ci			le16_to_cpu(lq_sta->lq.agg_params.agg_time_limit),
313662306a36Sopenharmony_ci			lq_sta->lq.agg_params.agg_dis_start_th,
313762306a36Sopenharmony_ci			lq_sta->lq.agg_params.agg_frame_cnt_limit);
313862306a36Sopenharmony_ci
313962306a36Sopenharmony_ci	desc += sprintf(buff+desc,
314062306a36Sopenharmony_ci			"Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
314162306a36Sopenharmony_ci			lq_sta->lq.general_params.start_rate_index[0],
314262306a36Sopenharmony_ci			lq_sta->lq.general_params.start_rate_index[1],
314362306a36Sopenharmony_ci			lq_sta->lq.general_params.start_rate_index[2],
314462306a36Sopenharmony_ci			lq_sta->lq.general_params.start_rate_index[3]);
314562306a36Sopenharmony_ci
314662306a36Sopenharmony_ci	for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
314762306a36Sopenharmony_ci		index = iwl_hwrate_to_plcp_idx(
314862306a36Sopenharmony_ci			le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags));
314962306a36Sopenharmony_ci		if (index == IWL_RATE_INVALID) {
315062306a36Sopenharmony_ci			desc += sprintf(buff + desc, " rate[%d] 0x%X invalid rate\n",
315162306a36Sopenharmony_ci				i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags));
315262306a36Sopenharmony_ci		} else if (is_legacy(tbl->lq_type)) {
315362306a36Sopenharmony_ci			desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps\n",
315462306a36Sopenharmony_ci				i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags),
315562306a36Sopenharmony_ci				iwl_rate_mcs[index].mbps);
315662306a36Sopenharmony_ci		} else {
315762306a36Sopenharmony_ci			desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps (%s)\n",
315862306a36Sopenharmony_ci				i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags),
315962306a36Sopenharmony_ci				iwl_rate_mcs[index].mbps, iwl_rate_mcs[index].mcs);
316062306a36Sopenharmony_ci		}
316162306a36Sopenharmony_ci	}
316262306a36Sopenharmony_ci
316362306a36Sopenharmony_ci	ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
316462306a36Sopenharmony_ci	kfree(buff);
316562306a36Sopenharmony_ci	return ret;
316662306a36Sopenharmony_ci}
316762306a36Sopenharmony_ci
316862306a36Sopenharmony_cistatic const struct file_operations rs_sta_dbgfs_scale_table_ops = {
316962306a36Sopenharmony_ci	.write = rs_sta_dbgfs_scale_table_write,
317062306a36Sopenharmony_ci	.read = rs_sta_dbgfs_scale_table_read,
317162306a36Sopenharmony_ci	.open = simple_open,
317262306a36Sopenharmony_ci	.llseek = default_llseek,
317362306a36Sopenharmony_ci};
317462306a36Sopenharmony_cistatic ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
317562306a36Sopenharmony_ci			char __user *user_buf, size_t count, loff_t *ppos)
317662306a36Sopenharmony_ci{
317762306a36Sopenharmony_ci	char *buff;
317862306a36Sopenharmony_ci	int desc = 0;
317962306a36Sopenharmony_ci	int i, j;
318062306a36Sopenharmony_ci	ssize_t ret;
318162306a36Sopenharmony_ci
318262306a36Sopenharmony_ci	struct iwl_lq_sta *lq_sta = file->private_data;
318362306a36Sopenharmony_ci
318462306a36Sopenharmony_ci	buff = kmalloc(1024, GFP_KERNEL);
318562306a36Sopenharmony_ci	if (!buff)
318662306a36Sopenharmony_ci		return -ENOMEM;
318762306a36Sopenharmony_ci
318862306a36Sopenharmony_ci	for (i = 0; i < LQ_SIZE; i++) {
318962306a36Sopenharmony_ci		desc += sprintf(buff+desc,
319062306a36Sopenharmony_ci				"%s type=%d SGI=%d HT40=%d DUP=%d GF=%d\n"
319162306a36Sopenharmony_ci				"rate=0x%X\n",
319262306a36Sopenharmony_ci				lq_sta->active_tbl == i ? "*" : "x",
319362306a36Sopenharmony_ci				lq_sta->lq_info[i].lq_type,
319462306a36Sopenharmony_ci				lq_sta->lq_info[i].is_SGI,
319562306a36Sopenharmony_ci				lq_sta->lq_info[i].is_ht40,
319662306a36Sopenharmony_ci				lq_sta->lq_info[i].is_dup,
319762306a36Sopenharmony_ci				lq_sta->is_green,
319862306a36Sopenharmony_ci				lq_sta->lq_info[i].current_rate);
319962306a36Sopenharmony_ci		for (j = 0; j < IWL_RATE_COUNT; j++) {
320062306a36Sopenharmony_ci			desc += sprintf(buff+desc,
320162306a36Sopenharmony_ci				"counter=%d success=%d %%=%d\n",
320262306a36Sopenharmony_ci				lq_sta->lq_info[i].win[j].counter,
320362306a36Sopenharmony_ci				lq_sta->lq_info[i].win[j].success_counter,
320462306a36Sopenharmony_ci				lq_sta->lq_info[i].win[j].success_ratio);
320562306a36Sopenharmony_ci		}
320662306a36Sopenharmony_ci	}
320762306a36Sopenharmony_ci	ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
320862306a36Sopenharmony_ci	kfree(buff);
320962306a36Sopenharmony_ci	return ret;
321062306a36Sopenharmony_ci}
321162306a36Sopenharmony_ci
321262306a36Sopenharmony_cistatic const struct file_operations rs_sta_dbgfs_stats_table_ops = {
321362306a36Sopenharmony_ci	.read = rs_sta_dbgfs_stats_table_read,
321462306a36Sopenharmony_ci	.open = simple_open,
321562306a36Sopenharmony_ci	.llseek = default_llseek,
321662306a36Sopenharmony_ci};
321762306a36Sopenharmony_ci
321862306a36Sopenharmony_cistatic ssize_t rs_sta_dbgfs_rate_scale_data_read(struct file *file,
321962306a36Sopenharmony_ci			char __user *user_buf, size_t count, loff_t *ppos)
322062306a36Sopenharmony_ci{
322162306a36Sopenharmony_ci	struct iwl_lq_sta *lq_sta = file->private_data;
322262306a36Sopenharmony_ci	struct iwl_scale_tbl_info *tbl = &lq_sta->lq_info[lq_sta->active_tbl];
322362306a36Sopenharmony_ci	char buff[120];
322462306a36Sopenharmony_ci	int desc = 0;
322562306a36Sopenharmony_ci
322662306a36Sopenharmony_ci	if (is_Ht(tbl->lq_type))
322762306a36Sopenharmony_ci		desc += sprintf(buff+desc,
322862306a36Sopenharmony_ci				"Bit Rate= %d Mb/s\n",
322962306a36Sopenharmony_ci				tbl->expected_tpt[lq_sta->last_txrate_idx]);
323062306a36Sopenharmony_ci	else
323162306a36Sopenharmony_ci		desc += sprintf(buff+desc,
323262306a36Sopenharmony_ci				"Bit Rate= %d Mb/s\n",
323362306a36Sopenharmony_ci				iwl_rates[lq_sta->last_txrate_idx].ieee >> 1);
323462306a36Sopenharmony_ci
323562306a36Sopenharmony_ci	return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
323662306a36Sopenharmony_ci}
323762306a36Sopenharmony_ci
323862306a36Sopenharmony_cistatic const struct file_operations rs_sta_dbgfs_rate_scale_data_ops = {
323962306a36Sopenharmony_ci	.read = rs_sta_dbgfs_rate_scale_data_read,
324062306a36Sopenharmony_ci	.open = simple_open,
324162306a36Sopenharmony_ci	.llseek = default_llseek,
324262306a36Sopenharmony_ci};
324362306a36Sopenharmony_ci
324462306a36Sopenharmony_cistatic void rs_add_debugfs(void *priv, void *priv_sta,
324562306a36Sopenharmony_ci					struct dentry *dir)
324662306a36Sopenharmony_ci{
324762306a36Sopenharmony_ci	struct iwl_lq_sta *lq_sta = priv_sta;
324862306a36Sopenharmony_ci
324962306a36Sopenharmony_ci	debugfs_create_file("rate_scale_table", 0600, dir, lq_sta,
325062306a36Sopenharmony_ci			    &rs_sta_dbgfs_scale_table_ops);
325162306a36Sopenharmony_ci	debugfs_create_file("rate_stats_table", 0400, dir, lq_sta,
325262306a36Sopenharmony_ci			    &rs_sta_dbgfs_stats_table_ops);
325362306a36Sopenharmony_ci	debugfs_create_file("rate_scale_data", 0400, dir, lq_sta,
325462306a36Sopenharmony_ci			    &rs_sta_dbgfs_rate_scale_data_ops);
325562306a36Sopenharmony_ci	debugfs_create_u8("tx_agg_tid_enable", 0600, dir,
325662306a36Sopenharmony_ci			  &lq_sta->tx_agg_tid_en);
325762306a36Sopenharmony_ci
325862306a36Sopenharmony_ci}
325962306a36Sopenharmony_ci#endif
326062306a36Sopenharmony_ci
326162306a36Sopenharmony_ci/*
326262306a36Sopenharmony_ci * Initialization of rate scaling information is done by driver after
326362306a36Sopenharmony_ci * the station is added. Since mac80211 calls this function before a
326462306a36Sopenharmony_ci * station is added we ignore it.
326562306a36Sopenharmony_ci */
326662306a36Sopenharmony_cistatic void rs_rate_init_stub(void *priv_r, struct ieee80211_supported_band *sband,
326762306a36Sopenharmony_ci			      struct cfg80211_chan_def *chandef,
326862306a36Sopenharmony_ci			      struct ieee80211_sta *sta, void *priv_sta)
326962306a36Sopenharmony_ci{
327062306a36Sopenharmony_ci}
327162306a36Sopenharmony_ci
327262306a36Sopenharmony_cistatic const struct rate_control_ops rs_ops = {
327362306a36Sopenharmony_ci	.name = RS_NAME,
327462306a36Sopenharmony_ci	.tx_status = rs_tx_status,
327562306a36Sopenharmony_ci	.get_rate = rs_get_rate,
327662306a36Sopenharmony_ci	.rate_init = rs_rate_init_stub,
327762306a36Sopenharmony_ci	.alloc = rs_alloc,
327862306a36Sopenharmony_ci	.free = rs_free,
327962306a36Sopenharmony_ci	.alloc_sta = rs_alloc_sta,
328062306a36Sopenharmony_ci	.free_sta = rs_free_sta,
328162306a36Sopenharmony_ci#ifdef CONFIG_MAC80211_DEBUGFS
328262306a36Sopenharmony_ci	.add_sta_debugfs = rs_add_debugfs,
328362306a36Sopenharmony_ci#endif
328462306a36Sopenharmony_ci};
328562306a36Sopenharmony_ci
328662306a36Sopenharmony_ciint iwlagn_rate_control_register(void)
328762306a36Sopenharmony_ci{
328862306a36Sopenharmony_ci	return ieee80211_rate_control_register(&rs_ops);
328962306a36Sopenharmony_ci}
329062306a36Sopenharmony_ci
329162306a36Sopenharmony_civoid iwlagn_rate_control_unregister(void)
329262306a36Sopenharmony_ci{
329362306a36Sopenharmony_ci	ieee80211_rate_control_unregister(&rs_ops);
329462306a36Sopenharmony_ci}
329562306a36Sopenharmony_ci
3296