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