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