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