File Index
Files and directories
Sign in
FILE

4965-rs.c

File preview with syntax highlighting.

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