Linux Audio

Check our new training course

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