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