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