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