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