cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

rs.c (99065B)


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