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