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