Fix build break in tsi108.c
[linux-2.6.git] / drivers / net / wireless / iwlwifi / iwl-4965-rs.c
1 /******************************************************************************
2  *
3  * Copyright(c) 2005 - 2007 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  * James P. Ketrenos <ipw2100-admin@linux.intel.com>
23  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24  *
25  *****************************************************************************/
26 #include <linux/kernel.h>
27 #include <linux/init.h>
28 #include <linux/skbuff.h>
29 #include <linux/wireless.h>
30 #include <net/mac80211.h>
31 #include <net/ieee80211.h>
32
33 #include <linux/netdevice.h>
34 #include <linux/etherdevice.h>
35 #include <linux/delay.h>
36
37 #include <linux/workqueue.h>
38
39 #include <net/mac80211.h>
40 #include <linux/wireless.h>
41
42 #define IWL 4965
43
44 #include "../net/mac80211/ieee80211_rate.h"
45
46 #include "iwlwifi.h"
47 #include "iwl-helpers.h"
48
49 #define RS_NAME "iwl-4965-rs"
50
51 #define NUM_TRY_BEFORE_ANTENNA_TOGGLE 1
52 #define IWL_NUMBER_TRY      1
53 #define IWL_HT_NUMBER_TRY   3
54
55 #define IWL_RATE_MAX_WINDOW             62
56 #define IWL_RATE_HIGH_TH                10880
57 #define IWL_RATE_MIN_FAILURE_TH         6
58 #define IWL_RATE_MIN_SUCCESS_TH         8
59 #define IWL_RATE_DECREASE_TH            1920
60 #define IWL_RATE_INCREASE_TH            8960
61 #define IWL_RATE_SCALE_FLUSH_INTVL   (2*HZ)        /*2 seconds */
62
63 static u8 rs_ht_to_legacy[] = {
64         IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
65         IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
66         IWL_RATE_6M_INDEX,
67         IWL_RATE_6M_INDEX, IWL_RATE_9M_INDEX,
68         IWL_RATE_12M_INDEX, IWL_RATE_18M_INDEX,
69         IWL_RATE_24M_INDEX, IWL_RATE_36M_INDEX,
70         IWL_RATE_48M_INDEX, IWL_RATE_54M_INDEX
71 };
72
73 struct iwl_rate {
74         u32 rate_n_flags;
75 } __attribute__ ((packed));
76
77 struct iwl_rate_scale_data {
78         u64 data;
79         s32 success_counter;
80         s32 success_ratio;
81         s32 counter;
82         s32 average_tpt;
83         unsigned long stamp;
84 };
85
86 struct iwl_scale_tbl_info {
87         enum iwl_table_type lq_type;
88         enum iwl_antenna_type antenna_type;
89         u8 is_SGI;
90         u8 is_fat;
91         u8 is_dup;
92         u8 action;
93         s32 *expected_tpt;
94         struct iwl_rate current_rate;
95         struct iwl_rate_scale_data win[IWL_RATE_COUNT];
96 };
97
98 struct iwl_rate_scale_priv {
99         u8 active_tbl;
100         u8 enable_counter;
101         u8 stay_in_tbl;
102         u8 search_better_tbl;
103         s32 last_tpt;
104         u32 table_count_limit;
105         u32 max_failure_limit;
106         u32 max_success_limit;
107         u32 table_count;
108         u32 total_failed;
109         u32 total_success;
110         u32 flush_timer;
111         u8 action_counter;
112         u8 antenna;
113         u8 valid_antenna;
114         u8 is_green;
115         u8 is_dup;
116         u8 phymode;
117         u8 ibss_sta_added;
118         u32 supp_rates;
119         u16 active_rate;
120         u16 active_siso_rate;
121         u16 active_mimo_rate;
122         u16 active_rate_basic;
123         struct iwl_link_quality_cmd lq;
124         struct iwl_scale_tbl_info lq_info[LQ_SIZE];
125 #ifdef CONFIG_MAC80211_DEBUGFS
126         struct dentry *rs_sta_dbgfs_scale_table_file;
127         struct dentry *rs_sta_dbgfs_stats_table_file;
128         struct iwl_rate dbg_fixed;
129         struct iwl_priv *drv;
130 #endif
131 };
132
133 static void rs_rate_scale_perform(struct iwl_priv *priv,
134                                    struct net_device *dev,
135                                    struct ieee80211_hdr *hdr,
136                                    struct sta_info *sta);
137 static void rs_fill_link_cmd(struct iwl_rate_scale_priv *lq_data,
138                              struct iwl_rate *tx_mcs,
139                              struct iwl_link_quality_cmd *tbl);
140
141
142 #ifdef CONFIG_MAC80211_DEBUGFS
143 static void rs_dbgfs_set_mcs(struct iwl_rate_scale_priv *rs_priv,
144                                 struct iwl_rate *mcs, int index);
145 #else
146 static void rs_dbgfs_set_mcs(struct iwl_rate_scale_priv *rs_priv,
147                                 struct iwl_rate *mcs, int index)
148 {}
149 #endif
150 static s32 expected_tpt_A[IWL_RATE_COUNT] = {
151         0, 0, 0, 0, 40, 57, 72, 98, 121, 154, 177, 186, 186
152 };
153
154 static s32 expected_tpt_G[IWL_RATE_COUNT] = {
155         7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 186
156 };
157
158 static s32 expected_tpt_siso20MHz[IWL_RATE_COUNT] = {
159         0, 0, 0, 0, 42, 42, 76, 102, 124, 159, 183, 193, 202
160 };
161
162 static s32 expected_tpt_siso20MHzSGI[IWL_RATE_COUNT] = {
163         0, 0, 0, 0, 46, 46, 82, 110, 132, 168, 192, 202, 211
164 };
165
166 static s32 expected_tpt_mimo20MHz[IWL_RATE_COUNT] = {
167         0, 0, 0, 0, 74, 74, 123, 155, 179, 214, 236, 244, 251
168 };
169
170 static s32 expected_tpt_mimo20MHzSGI[IWL_RATE_COUNT] = {
171         0, 0, 0, 0, 81, 81, 131, 164, 188, 222, 243, 251, 257
172 };
173
174 static s32 expected_tpt_siso40MHz[IWL_RATE_COUNT] = {
175         0, 0, 0, 0, 77, 77, 127, 160, 184, 220, 242, 250, 257
176 };
177
178 static s32 expected_tpt_siso40MHzSGI[IWL_RATE_COUNT] = {
179         0, 0, 0, 0, 83, 83, 135, 169, 193, 229, 250, 257, 264
180 };
181
182 static s32 expected_tpt_mimo40MHz[IWL_RATE_COUNT] = {
183         0, 0, 0, 0, 123, 123, 182, 214, 235, 264, 279, 285, 289
184 };
185
186 static s32 expected_tpt_mimo40MHzSGI[IWL_RATE_COUNT] = {
187         0, 0, 0, 0, 131, 131, 191, 222, 242, 270, 284, 289, 293
188 };
189
190 static int iwl_lq_sync_callback(struct iwl_priv *priv,
191                                 struct iwl_cmd *cmd, struct sk_buff *skb)
192 {
193         /*We didn't cache the SKB; let the caller free it */
194         return 1;
195 }
196
197 static inline u8 iwl_rate_get_rate(u32 rate_n_flags)
198 {
199         return (u8)(rate_n_flags & 0xFF);
200 }
201
202 static int rs_send_lq_cmd(struct iwl_priv *priv,
203                           struct iwl_link_quality_cmd *lq, u8 flags)
204 {
205 #ifdef CONFIG_IWLWIFI_DEBUG
206         int i;
207 #endif
208         int rc = -1;
209
210         struct iwl_host_cmd cmd = {
211                 .id = REPLY_TX_LINK_QUALITY_CMD,
212                 .len = sizeof(struct iwl_link_quality_cmd),
213                 .meta.flags = flags,
214                 .data = lq,
215         };
216
217         if ((lq->sta_id == 0xFF) &&
218             (priv->iw_mode == IEEE80211_IF_TYPE_IBSS))
219                 return rc;
220
221         if (lq->sta_id == 0xFF)
222                 lq->sta_id = IWL_AP_ID;
223
224         IWL_DEBUG_RATE("lq station id 0x%x\n", lq->sta_id);
225         IWL_DEBUG_RATE("lq dta 0x%X 0x%X\n",
226                        lq->general_params.single_stream_ant_msk,
227                        lq->general_params.dual_stream_ant_msk);
228 #ifdef CONFIG_IWLWIFI_DEBUG
229         for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
230                 IWL_DEBUG_RATE("lq index %d 0x%X\n",
231                                 i, lq->rs_table[i].rate_n_flags);
232 #endif
233
234         if (flags & CMD_ASYNC)
235                 cmd.meta.u.callback = iwl_lq_sync_callback;
236
237         if (iwl_is_associated(priv) && priv->assoc_station_added &&
238             priv->lq_mngr.lq_ready)
239                 rc = iwl_send_cmd(priv, &cmd);
240
241         return rc;
242 }
243
244 static int rs_rate_scale_clear_window(struct iwl_rate_scale_data *window)
245 {
246         window->data = 0;
247         window->success_counter = 0;
248         window->success_ratio = IWL_INVALID_VALUE;
249         window->counter = 0;
250         window->average_tpt = IWL_INVALID_VALUE;
251         window->stamp = 0;
252
253         return 0;
254 }
255
256 static int rs_collect_tx_data(struct iwl_rate_scale_data *windows,
257                               int scale_index, s32 tpt, u32 status)
258 {
259         int rc = 0;
260         struct iwl_rate_scale_data *window = NULL;
261         u64 mask;
262         u8 win_size = IWL_RATE_MAX_WINDOW;
263         s32 fail_count;
264
265         if (scale_index < 0)
266                 return -1;
267
268         if (scale_index >= IWL_RATE_COUNT)
269                 return -1;
270
271         window = &(windows[scale_index]);
272
273         if (window->counter >= win_size) {
274
275                 window->counter = win_size - 1;
276                 mask = 1;
277                 mask = (mask << (win_size - 1));
278                 if ((window->data & mask)) {
279                         window->data &= ~mask;
280                         window->success_counter = window->success_counter - 1;
281                 }
282         }
283
284         window->counter = window->counter + 1;
285         mask = window->data;
286         window->data = (mask << 1);
287         if (status != 0) {
288                 window->success_counter = window->success_counter + 1;
289                 window->data |= 0x1;
290         }
291
292         if (window->counter > 0)
293                 window->success_ratio = 128 * (100 * window->success_counter)
294                                         / window->counter;
295         else
296                 window->success_ratio = IWL_INVALID_VALUE;
297
298         fail_count = window->counter - window->success_counter;
299
300         if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
301             (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
302                 window->average_tpt = (window->success_ratio * tpt + 64) / 128;
303         else
304                 window->average_tpt = IWL_INVALID_VALUE;
305
306         window->stamp = jiffies;
307
308         return rc;
309 }
310
311 int static rs_mcs_from_tbl(struct iwl_rate *mcs_rate,
312                            struct iwl_scale_tbl_info *tbl,
313                            int index, u8 use_green)
314 {
315         int rc = 0;
316
317         if (is_legacy(tbl->lq_type)) {
318                 mcs_rate->rate_n_flags = iwl_rates[index].plcp;
319                 if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
320                         mcs_rate->rate_n_flags |= RATE_MCS_CCK_MSK;
321
322         } else if (is_siso(tbl->lq_type)) {
323                 if (index > IWL_LAST_OFDM_RATE)
324                         index = IWL_LAST_OFDM_RATE;
325                  mcs_rate->rate_n_flags = iwl_rates[index].plcp_siso |
326                                           RATE_MCS_HT_MSK;
327         } else {
328                 if (index > IWL_LAST_OFDM_RATE)
329                         index = IWL_LAST_OFDM_RATE;
330                 mcs_rate->rate_n_flags = iwl_rates[index].plcp_mimo |
331                                          RATE_MCS_HT_MSK;
332         }
333
334         switch (tbl->antenna_type) {
335         case ANT_BOTH:
336                 mcs_rate->rate_n_flags |= RATE_MCS_ANT_AB_MSK;
337                 break;
338         case ANT_MAIN:
339                 mcs_rate->rate_n_flags |= RATE_MCS_ANT_A_MSK;
340                 break;
341         case ANT_AUX:
342                 mcs_rate->rate_n_flags |= RATE_MCS_ANT_B_MSK;
343                 break;
344         case ANT_NONE:
345                 break;
346         }
347
348         if (is_legacy(tbl->lq_type))
349                 return rc;
350
351         if (tbl->is_fat) {
352                 if (tbl->is_dup)
353                         mcs_rate->rate_n_flags |= RATE_MCS_DUP_MSK;
354                 else
355                         mcs_rate->rate_n_flags |= RATE_MCS_FAT_MSK;
356         }
357         if (tbl->is_SGI)
358                 mcs_rate->rate_n_flags |= RATE_MCS_SGI_MSK;
359
360         if (use_green) {
361                 mcs_rate->rate_n_flags |= RATE_MCS_GF_MSK;
362                 if (is_siso(tbl->lq_type))
363                         mcs_rate->rate_n_flags &= ~RATE_MCS_SGI_MSK;
364         }
365         return rc;
366 }
367
368 static int rs_get_tbl_info_from_mcs(const struct iwl_rate *mcs_rate,
369                                     int phymode, struct iwl_scale_tbl_info *tbl,
370                                     int *rate_idx)
371 {
372         int index;
373         u32 ant_msk;
374
375         index = iwl_rate_index_from_plcp(mcs_rate->rate_n_flags);
376
377         if (index  == IWL_RATE_INVALID) {
378                 *rate_idx = -1;
379                 return -1;
380         }
381         tbl->is_SGI = 0;
382         tbl->is_fat = 0;
383         tbl->is_dup = 0;
384         tbl->antenna_type = ANT_BOTH;
385
386         if (!(mcs_rate->rate_n_flags & RATE_MCS_HT_MSK)) {
387                 ant_msk = (mcs_rate->rate_n_flags & RATE_MCS_ANT_AB_MSK);
388
389                 if (ant_msk == RATE_MCS_ANT_AB_MSK)
390                         tbl->lq_type = LQ_NONE;
391                 else {
392
393                         if (phymode == MODE_IEEE80211A)
394                                 tbl->lq_type = LQ_A;
395                         else
396                                 tbl->lq_type = LQ_G;
397
398                         if (mcs_rate->rate_n_flags & RATE_MCS_ANT_A_MSK)
399                                 tbl->antenna_type = ANT_MAIN;
400                         else
401                                 tbl->antenna_type = ANT_AUX;
402                 }
403                 *rate_idx = index;
404
405         } else if (iwl_rate_get_rate(mcs_rate->rate_n_flags)
406                                         <= IWL_RATE_SISO_60M_PLCP) {
407                 tbl->lq_type = LQ_SISO;
408
409                 ant_msk = (mcs_rate->rate_n_flags & RATE_MCS_ANT_AB_MSK);
410                 if (ant_msk == RATE_MCS_ANT_AB_MSK)
411                         tbl->lq_type = LQ_NONE;
412                 else {
413                         if (mcs_rate->rate_n_flags & RATE_MCS_ANT_A_MSK)
414                                 tbl->antenna_type = ANT_MAIN;
415                         else
416                                 tbl->antenna_type = ANT_AUX;
417                 }
418                 if (mcs_rate->rate_n_flags & RATE_MCS_SGI_MSK)
419                         tbl->is_SGI = 1;
420
421                 if ((mcs_rate->rate_n_flags & RATE_MCS_FAT_MSK) ||
422                     (mcs_rate->rate_n_flags & RATE_MCS_DUP_MSK))
423                         tbl->is_fat = 1;
424
425                 if (mcs_rate->rate_n_flags & RATE_MCS_DUP_MSK)
426                         tbl->is_dup = 1;
427
428                 *rate_idx = index;
429         } else {
430                 tbl->lq_type = LQ_MIMO;
431                 if (mcs_rate->rate_n_flags & RATE_MCS_SGI_MSK)
432                         tbl->is_SGI = 1;
433
434                 if ((mcs_rate->rate_n_flags & RATE_MCS_FAT_MSK) ||
435                     (mcs_rate->rate_n_flags & RATE_MCS_DUP_MSK))
436                         tbl->is_fat = 1;
437
438                 if (mcs_rate->rate_n_flags & RATE_MCS_DUP_MSK)
439                         tbl->is_dup = 1;
440                 *rate_idx = index;
441         }
442         return 0;
443 }
444
445 static inline void rs_toggle_antenna(struct iwl_rate *new_rate,
446                                      struct iwl_scale_tbl_info *tbl)
447 {
448         if (tbl->antenna_type == ANT_AUX) {
449                 tbl->antenna_type = ANT_MAIN;
450                 new_rate->rate_n_flags &= ~RATE_MCS_ANT_B_MSK;
451                 new_rate->rate_n_flags |= RATE_MCS_ANT_A_MSK;
452         } else {
453                 tbl->antenna_type = ANT_AUX;
454                 new_rate->rate_n_flags &= ~RATE_MCS_ANT_A_MSK;
455                 new_rate->rate_n_flags |= RATE_MCS_ANT_B_MSK;
456         }
457 }
458
459 static inline s8 rs_use_green(struct iwl_priv *priv)
460 {
461         s8 rc = 0;
462 #ifdef CONFIG_IWLWIFI_HT
463         if (!priv->is_ht_enabled || !priv->current_assoc_ht.is_ht)
464                 return 0;
465
466         if ((priv->current_assoc_ht.is_green_field) &&
467             !(priv->current_assoc_ht.operating_mode & 0x4))
468                 rc = 1;
469 #endif  /*CONFIG_IWLWIFI_HT */
470         return rc;
471 }
472
473 /**
474  * rs_get_supported_rates - get the available rates
475  *
476  * if management frame or broadcast frame only return
477  * basic available rates.
478  *
479  */
480 static void rs_get_supported_rates(struct iwl_rate_scale_priv *lq_data,
481                                    struct ieee80211_hdr *hdr,
482                                    enum iwl_table_type rate_type,
483                                    u16 *data_rate)
484 {
485         if (is_legacy(rate_type))
486                 *data_rate = lq_data->active_rate;
487         else {
488                 if (is_siso(rate_type))
489                         *data_rate = lq_data->active_siso_rate;
490                 else
491                         *data_rate = lq_data->active_mimo_rate;
492         }
493
494         if (hdr && is_multicast_ether_addr(hdr->addr1) &&
495             lq_data->active_rate_basic)
496                 *data_rate = lq_data->active_rate_basic;
497 }
498
499 static u16 rs_get_adjacent_rate(u8 index, u16 rate_mask, int rate_type)
500 {
501         u8 high = IWL_RATE_INVALID;
502         u8 low = IWL_RATE_INVALID;
503
504         /* 802.11A or ht walks to the next literal adjascent rate in
505          * the rate table */
506         if (is_a_band(rate_type) || !is_legacy(rate_type)) {
507                 int i;
508                 u32 mask;
509
510                 /* Find the previous rate that is in the rate mask */
511                 i = index - 1;
512                 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
513                         if (rate_mask & mask) {
514                                 low = i;
515                                 break;
516                         }
517                 }
518
519                 /* Find the next rate that is in the rate mask */
520                 i = index + 1;
521                 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
522                         if (rate_mask & mask) {
523                                 high = i;
524                                 break;
525                         }
526                 }
527
528                 return (high << 8) | low;
529         }
530
531         low = index;
532         while (low != IWL_RATE_INVALID) {
533                 low = iwl_rates[low].prev_rs;
534                 if (low == IWL_RATE_INVALID)
535                         break;
536                 if (rate_mask & (1 << low))
537                         break;
538                 IWL_DEBUG_RATE("Skipping masked lower rate: %d\n", low);
539         }
540
541         high = index;
542         while (high != IWL_RATE_INVALID) {
543                 high = iwl_rates[high].next_rs;
544                 if (high == IWL_RATE_INVALID)
545                         break;
546                 if (rate_mask & (1 << high))
547                         break;
548                 IWL_DEBUG_RATE("Skipping masked higher rate: %d\n", high);
549         }
550
551         return (high << 8) | low;
552 }
553
554 static int rs_get_lower_rate(struct iwl_rate_scale_priv *lq_data,
555                              struct iwl_scale_tbl_info *tbl, u8 scale_index,
556                              u8 ht_possible, struct iwl_rate *mcs_rate)
557 {
558         s32 low;
559         u16 rate_mask;
560         u16 high_low;
561         u8 switch_to_legacy = 0;
562         u8 is_green = lq_data->is_green;
563
564         /* check if we need to switch from HT to legacy rates.
565          * assumption is that mandatory rates (1Mbps or 6Mbps)
566          * are always supported (spec demand) */
567         if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_index)) {
568                 switch_to_legacy = 1;
569                 scale_index = rs_ht_to_legacy[scale_index];
570                 if (lq_data->phymode == MODE_IEEE80211A)
571                         tbl->lq_type = LQ_A;
572                 else
573                         tbl->lq_type = LQ_G;
574
575                 if ((tbl->antenna_type == ANT_BOTH) ||
576                     (tbl->antenna_type == ANT_NONE))
577                         tbl->antenna_type = ANT_MAIN;
578
579                 tbl->is_fat = 0;
580                 tbl->is_SGI = 0;
581         }
582
583         rs_get_supported_rates(lq_data, NULL, tbl->lq_type, &rate_mask);
584
585         /* mask with station rate restriction */
586         if (is_legacy(tbl->lq_type)) {
587                 if (lq_data->phymode == (u8) MODE_IEEE80211A)
588                         rate_mask  = (u16)(rate_mask &
589                            (lq_data->supp_rates << IWL_FIRST_OFDM_RATE));
590                 else
591                         rate_mask = (u16)(rate_mask & lq_data->supp_rates);
592         }
593
594         /* if we did switched from HT to legacy check current rate */
595         if ((switch_to_legacy) &&
596             (rate_mask & (1 << scale_index))) {
597                 rs_mcs_from_tbl(mcs_rate, tbl, scale_index, is_green);
598                 return 0;
599         }
600
601         high_low = rs_get_adjacent_rate(scale_index, rate_mask, tbl->lq_type);
602         low = high_low & 0xff;
603
604         if (low != IWL_RATE_INVALID)
605                 rs_mcs_from_tbl(mcs_rate, tbl, low, is_green);
606         else
607                 rs_mcs_from_tbl(mcs_rate, tbl, scale_index, is_green);
608
609         return 0;
610 }
611
612 static void rs_tx_status(void *priv_rate,
613                          struct net_device *dev,
614                          struct sk_buff *skb,
615                          struct ieee80211_tx_status *tx_resp)
616 {
617         int status;
618         u8 retries;
619         int rs_index, index = 0;
620         struct iwl_rate_scale_priv *lq;
621         struct iwl_link_quality_cmd *table;
622         struct sta_info *sta;
623         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
624         struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
625         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
626         struct iwl_rate_scale_data *window = NULL;
627         struct iwl_rate_scale_data *search_win = NULL;
628         struct iwl_rate tx_mcs;
629         struct iwl_scale_tbl_info tbl_type;
630         struct iwl_scale_tbl_info *curr_tbl, *search_tbl;
631         u8 active_index = 0;
632         u16 fc = le16_to_cpu(hdr->frame_control);
633         s32 tpt = 0;
634
635         IWL_DEBUG_RATE_LIMIT("get frame ack response, update rate scale window\n");
636
637         if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1))
638                 return;
639
640         retries = tx_resp->retry_count;
641
642         if (retries > 15)
643                 retries = 15;
644
645
646         sta = sta_info_get(local, hdr->addr1);
647
648         if (!sta || !sta->rate_ctrl_priv) {
649                 if (sta)
650                         sta_info_put(sta);
651                 return;
652         }
653
654         lq = (struct iwl_rate_scale_priv *)sta->rate_ctrl_priv;
655
656         if (!priv->lq_mngr.lq_ready)
657                 return;
658
659         if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) && !lq->ibss_sta_added)
660                 return;
661
662         table = &lq->lq;
663         active_index = lq->active_tbl;
664
665         lq->antenna = (lq->valid_antenna & local->hw.conf.antenna_sel_tx);
666         if (!lq->antenna)
667                 lq->antenna = lq->valid_antenna;
668
669         lq->antenna = lq->valid_antenna;
670         curr_tbl = &(lq->lq_info[active_index]);
671         search_tbl = &(lq->lq_info[(1 - active_index)]);
672         window = (struct iwl_rate_scale_data *)
673             &(curr_tbl->win[0]);
674         search_win = (struct iwl_rate_scale_data *)
675             &(search_tbl->win[0]);
676
677         tx_mcs.rate_n_flags = tx_resp->control.tx_rate;
678
679         rs_get_tbl_info_from_mcs(&tx_mcs, priv->phymode,
680                                   &tbl_type, &rs_index);
681         if ((rs_index < 0) || (rs_index >= IWL_RATE_COUNT)) {
682                 IWL_DEBUG_RATE("bad rate index at: %d rate 0x%X\n",
683                              rs_index, tx_mcs.rate_n_flags);
684                 sta_info_put(sta);
685                 return;
686         }
687
688         if (retries &&
689             (tx_mcs.rate_n_flags !=
690                                 le32_to_cpu(table->rs_table[0].rate_n_flags))) {
691                 IWL_DEBUG_RATE("initial rate does not match 0x%x 0x%x\n",
692                                 tx_mcs.rate_n_flags,
693                                 le32_to_cpu(table->rs_table[0].rate_n_flags));
694                 sta_info_put(sta);
695                 return;
696         }
697
698         while (retries) {
699                 tx_mcs.rate_n_flags =
700                     le32_to_cpu(table->rs_table[index].rate_n_flags);
701                 rs_get_tbl_info_from_mcs(&tx_mcs, priv->phymode,
702                                           &tbl_type, &rs_index);
703
704                 if ((tbl_type.lq_type == search_tbl->lq_type) &&
705                     (tbl_type.antenna_type == search_tbl->antenna_type) &&
706                     (tbl_type.is_SGI == search_tbl->is_SGI)) {
707                         if (search_tbl->expected_tpt)
708                                 tpt = search_tbl->expected_tpt[rs_index];
709                         else
710                                 tpt = 0;
711                         rs_collect_tx_data(search_win,
712                                             rs_index, tpt, 0);
713                 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
714                            (tbl_type.antenna_type == curr_tbl->antenna_type) &&
715                            (tbl_type.is_SGI == curr_tbl->is_SGI)) {
716                         if (curr_tbl->expected_tpt)
717                                 tpt = curr_tbl->expected_tpt[rs_index];
718                         else
719                                 tpt = 0;
720                         rs_collect_tx_data(window, rs_index, tpt, 0);
721                 }
722                 if (lq->stay_in_tbl)
723                         lq->total_failed++;
724                 --retries;
725                 index++;
726
727         }
728
729         if (!tx_resp->retry_count)
730                 tx_mcs.rate_n_flags = tx_resp->control.tx_rate;
731         else
732                 tx_mcs.rate_n_flags =
733                         le32_to_cpu(table->rs_table[index].rate_n_flags);
734
735         rs_get_tbl_info_from_mcs(&tx_mcs, priv->phymode,
736                                   &tbl_type, &rs_index);
737
738         if (tx_resp->flags & IEEE80211_TX_STATUS_ACK)
739                 status = 1;
740         else
741                 status = 0;
742
743         if ((tbl_type.lq_type == search_tbl->lq_type) &&
744             (tbl_type.antenna_type == search_tbl->antenna_type) &&
745             (tbl_type.is_SGI == search_tbl->is_SGI)) {
746                 if (search_tbl->expected_tpt)
747                         tpt = search_tbl->expected_tpt[rs_index];
748                 else
749                         tpt = 0;
750                 rs_collect_tx_data(search_win,
751                                     rs_index, tpt, status);
752         } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
753                    (tbl_type.antenna_type == curr_tbl->antenna_type) &&
754                    (tbl_type.is_SGI == curr_tbl->is_SGI)) {
755                 if (curr_tbl->expected_tpt)
756                         tpt = curr_tbl->expected_tpt[rs_index];
757                 else
758                         tpt = 0;
759                 rs_collect_tx_data(window, rs_index, tpt, status);
760         }
761
762         if (lq->stay_in_tbl) {
763                 if (status)
764                         lq->total_success++;
765                 else
766                         lq->total_failed++;
767         }
768
769         rs_rate_scale_perform(priv, dev, hdr, sta);
770         sta_info_put(sta);
771         return;
772 }
773
774 static u8 rs_is_ant_connected(u8 valid_antenna,
775                               enum iwl_antenna_type antenna_type)
776 {
777         if (antenna_type == ANT_AUX)
778                 return ((valid_antenna & 0x2) ? 1:0);
779         else if (antenna_type == ANT_MAIN)
780                 return ((valid_antenna & 0x1) ? 1:0);
781         else if (antenna_type == ANT_BOTH) {
782                 if ((valid_antenna & 0x3) == 0x3)
783                         return 1;
784                 else
785                         return 0;
786         }
787
788         return 1;
789 }
790
791 static u8 rs_is_other_ant_connected(u8 valid_antenna,
792                                     enum iwl_antenna_type antenna_type)
793 {
794         if (antenna_type == ANT_AUX)
795                 return (rs_is_ant_connected(valid_antenna, ANT_MAIN));
796         else
797                 return (rs_is_ant_connected(valid_antenna, ANT_AUX));
798
799         return 0;
800 }
801
802 static void rs_set_stay_in_table(u8 is_legacy,
803                                  struct iwl_rate_scale_priv *lq_data)
804 {
805         IWL_DEBUG_HT("we are staying in the same table\n");
806         lq_data->stay_in_tbl = 1;
807         if (is_legacy) {
808                 lq_data->table_count_limit = IWL_LEGACY_TABLE_COUNT;
809                 lq_data->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
810                 lq_data->max_success_limit = IWL_LEGACY_TABLE_COUNT;
811         } else {
812                 lq_data->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
813                 lq_data->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
814                 lq_data->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
815         }
816         lq_data->table_count = 0;
817         lq_data->total_failed = 0;
818         lq_data->total_success = 0;
819 }
820
821 static void rs_get_expected_tpt_table(struct iwl_rate_scale_priv *lq_data,
822                                       struct iwl_scale_tbl_info *tbl)
823 {
824         if (is_legacy(tbl->lq_type)) {
825                 if (!is_a_band(tbl->lq_type))
826                         tbl->expected_tpt = expected_tpt_G;
827                 else
828                         tbl->expected_tpt = expected_tpt_A;
829         } else if (is_siso(tbl->lq_type)) {
830                 if (tbl->is_fat && !lq_data->is_dup)
831                         if (tbl->is_SGI)
832                                 tbl->expected_tpt = expected_tpt_siso40MHzSGI;
833                         else
834                                 tbl->expected_tpt = expected_tpt_siso40MHz;
835                 else if (tbl->is_SGI)
836                         tbl->expected_tpt = expected_tpt_siso20MHzSGI;
837                 else
838                         tbl->expected_tpt = expected_tpt_siso20MHz;
839
840         } else if (is_mimo(tbl->lq_type)) {
841                 if (tbl->is_fat && !lq_data->is_dup)
842                         if (tbl->is_SGI)
843                                 tbl->expected_tpt = expected_tpt_mimo40MHzSGI;
844                         else
845                                 tbl->expected_tpt = expected_tpt_mimo40MHz;
846                 else if (tbl->is_SGI)
847                         tbl->expected_tpt = expected_tpt_mimo20MHzSGI;
848                 else
849                         tbl->expected_tpt = expected_tpt_mimo20MHz;
850         } else
851                 tbl->expected_tpt = expected_tpt_G;
852 }
853
854 #ifdef CONFIG_IWLWIFI_HT
855 static s32 rs_get_best_rate(struct iwl_priv *priv,
856                             struct iwl_rate_scale_priv *lq_data,
857                             struct iwl_scale_tbl_info *tbl,
858                             u16 rate_mask, s8 index, s8 rate)
859 {
860         struct iwl_scale_tbl_info *active_tbl =
861             &(lq_data->lq_info[lq_data->active_tbl]);
862         s32 new_rate, high, low, start_hi;
863         s32 active_sr = active_tbl->win[index].success_ratio;
864         s32 *tpt_tbl = tbl->expected_tpt;
865         s32 active_tpt = active_tbl->expected_tpt[index];
866         u16 high_low;
867
868         new_rate = high = low = start_hi = IWL_RATE_INVALID;
869
870         for (; ;) {
871                 high_low = rs_get_adjacent_rate(rate, rate_mask, tbl->lq_type);
872
873                 low = high_low & 0xff;
874                 high = (high_low >> 8) & 0xff;
875
876                 if ((((100 * tpt_tbl[rate]) > lq_data->last_tpt) &&
877                      ((active_sr > IWL_RATE_DECREASE_TH) &&
878                       (active_sr <= IWL_RATE_HIGH_TH) &&
879                       (tpt_tbl[rate] <= active_tpt))) ||
880                     ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
881                      (tpt_tbl[rate] > active_tpt))) {
882
883                         if (start_hi != IWL_RATE_INVALID) {
884                                 new_rate = start_hi;
885                                 break;
886                         }
887                         new_rate = rate;
888                         if (low != IWL_RATE_INVALID)
889                                 rate = low;
890                         else
891                                 break;
892                 } else {
893                         if (new_rate != IWL_RATE_INVALID)
894                                 break;
895                         else if (high != IWL_RATE_INVALID) {
896                                 start_hi = high;
897                                 rate = high;
898                         } else {
899                                 new_rate = rate;
900                                 break;
901                         }
902                 }
903         }
904
905         return new_rate;
906 }
907 #endif                          /* CONFIG_IWLWIFI_HT */
908
909 static inline u8 rs_is_both_ant_supp(u8 valid_antenna)
910 {
911         return (rs_is_ant_connected(valid_antenna, ANT_BOTH));
912 }
913
914 static int rs_switch_to_mimo(struct iwl_priv *priv,
915                              struct iwl_rate_scale_priv *lq_data,
916                              struct iwl_scale_tbl_info *tbl, int index)
917 {
918         int rc = -1;
919 #ifdef CONFIG_IWLWIFI_HT
920         u16 rate_mask;
921         s32 rate;
922         s8 is_green = lq_data->is_green;
923
924         if (!priv->is_ht_enabled || !priv->current_assoc_ht.is_ht)
925                 return -1;
926
927         IWL_DEBUG_HT("LQ: try to switch to MIMO\n");
928         tbl->lq_type = LQ_MIMO;
929         rs_get_supported_rates(lq_data, NULL, tbl->lq_type,
930                                 &rate_mask);
931
932         if (priv->current_assoc_ht.tx_mimo_ps_mode == IWL_MIMO_PS_STATIC)
933                 return -1;
934
935         if (!rs_is_both_ant_supp(lq_data->antenna))
936                 return -1;
937
938         rc = 0;
939         tbl->is_dup = lq_data->is_dup;
940         tbl->action = 0;
941         if (priv->current_channel_width == IWL_CHANNEL_WIDTH_40MHZ)
942                 tbl->is_fat = 1;
943         else
944                 tbl->is_fat = 0;
945
946         if (tbl->is_fat) {
947                 if (priv->current_assoc_ht.sgf & HT_SHORT_GI_40MHZ_ONLY)
948                         tbl->is_SGI = 1;
949                 else
950                         tbl->is_SGI = 0;
951         } else if (priv->current_assoc_ht.sgf & HT_SHORT_GI_20MHZ_ONLY)
952                 tbl->is_SGI = 1;
953         else
954                 tbl->is_SGI = 0;
955
956         rs_get_expected_tpt_table(lq_data, tbl);
957
958         rate = rs_get_best_rate(priv, lq_data, tbl, rate_mask, index, index);
959
960         IWL_DEBUG_HT("LQ: MIMO best rate %d mask %X\n", rate, rate_mask);
961         if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask))
962                 return -1;
963         rs_mcs_from_tbl(&tbl->current_rate, tbl, rate, is_green);
964
965         IWL_DEBUG_HT("LQ: Switch to new mcs %X index is green %X\n",
966                      tbl->current_rate.rate_n_flags, is_green);
967
968 #endif                          /*CONFIG_IWLWIFI_HT */
969         return rc;
970 }
971
972 static int rs_switch_to_siso(struct iwl_priv *priv,
973                              struct iwl_rate_scale_priv *lq_data,
974                              struct iwl_scale_tbl_info *tbl, int index)
975 {
976         int rc = -1;
977 #ifdef CONFIG_IWLWIFI_HT
978         u16 rate_mask;
979         u8 is_green = lq_data->is_green;
980         s32 rate;
981
982         IWL_DEBUG_HT("LQ: try to switch to SISO\n");
983         if (!priv->is_ht_enabled || !priv->current_assoc_ht.is_ht)
984                 return -1;
985
986         rc = 0;
987         tbl->is_dup = lq_data->is_dup;
988         tbl->lq_type = LQ_SISO;
989         tbl->action = 0;
990         rs_get_supported_rates(lq_data, NULL, tbl->lq_type,
991                                 &rate_mask);
992
993         if (priv->current_channel_width == IWL_CHANNEL_WIDTH_40MHZ)
994                 tbl->is_fat = 1;
995         else
996                 tbl->is_fat = 0;
997
998         if (tbl->is_fat) {
999                 if (priv->current_assoc_ht.sgf & HT_SHORT_GI_40MHZ_ONLY)
1000                         tbl->is_SGI = 1;
1001                 else
1002                         tbl->is_SGI = 0;
1003         } else if (priv->current_assoc_ht.sgf & HT_SHORT_GI_20MHZ_ONLY)
1004                 tbl->is_SGI = 1;
1005         else
1006                 tbl->is_SGI = 0;
1007
1008         if (is_green)
1009                 tbl->is_SGI = 0;
1010
1011         rs_get_expected_tpt_table(lq_data, tbl);
1012         rate = rs_get_best_rate(priv, lq_data, tbl, rate_mask, index, index);
1013
1014         IWL_DEBUG_HT("LQ: get best rate %d mask %X\n", rate, rate_mask);
1015         if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1016                 IWL_DEBUG_HT("can not switch with index %d rate mask %x\n",
1017                              rate, rate_mask);
1018                 return -1;
1019         }
1020         rs_mcs_from_tbl(&tbl->current_rate, tbl, rate, is_green);
1021         IWL_DEBUG_HT("LQ: Switch to new mcs %X index is green %X\n",
1022                      tbl->current_rate.rate_n_flags, is_green);
1023
1024 #endif                          /*CONFIG_IWLWIFI_HT */
1025         return rc;
1026 }
1027
1028 static int rs_move_legacy_other(struct iwl_priv *priv,
1029                                 struct iwl_rate_scale_priv *lq_data,
1030                                 int index)
1031 {
1032         int rc = 0;
1033         struct iwl_scale_tbl_info *tbl =
1034             &(lq_data->lq_info[lq_data->active_tbl]);
1035         struct iwl_scale_tbl_info *search_tbl =
1036             &(lq_data->lq_info[(1 - lq_data->active_tbl)]);
1037         struct iwl_rate_scale_data *window = &(tbl->win[index]);
1038         u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1039                   (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1040         u8 start_action = tbl->action;
1041
1042         for (; ;) {
1043                 switch (tbl->action) {
1044                 case IWL_LEGACY_SWITCH_ANTENNA:
1045                         IWL_DEBUG_HT("LQ Legacy switch Antenna\n");
1046
1047                         search_tbl->lq_type = LQ_NONE;
1048                         lq_data->action_counter++;
1049                         if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1050                                 break;
1051                         if (!rs_is_other_ant_connected(lq_data->antenna,
1052                                                         tbl->antenna_type))
1053                                 break;
1054
1055                         memcpy(search_tbl, tbl, sz);
1056
1057                         rs_toggle_antenna(&(search_tbl->current_rate),
1058                                            search_tbl);
1059                         rs_get_expected_tpt_table(lq_data, search_tbl);
1060                         lq_data->search_better_tbl = 1;
1061                         goto out;
1062
1063                 case IWL_LEGACY_SWITCH_SISO:
1064                         IWL_DEBUG_HT("LQ: Legacy switch to SISO\n");
1065                         memcpy(search_tbl, tbl, sz);
1066                         search_tbl->lq_type = LQ_SISO;
1067                         search_tbl->is_SGI = 0;
1068                         search_tbl->is_fat = 0;
1069                         rc = rs_switch_to_siso(priv, lq_data, search_tbl,
1070                                                index);
1071                         if (!rc) {
1072                                 lq_data->search_better_tbl = 1;
1073                                 lq_data->action_counter = 0;
1074                         }
1075                         if (!rc)
1076                                 goto out;
1077
1078                         break;
1079                 case IWL_LEGACY_SWITCH_MIMO:
1080                         IWL_DEBUG_HT("LQ: Legacy switch MIMO\n");
1081                         memcpy(search_tbl, tbl, sz);
1082                         search_tbl->lq_type = LQ_MIMO;
1083                         search_tbl->is_SGI = 0;
1084                         search_tbl->is_fat = 0;
1085                         search_tbl->antenna_type = ANT_BOTH;
1086                         rc = rs_switch_to_mimo(priv, lq_data, search_tbl,
1087                                                index);
1088                         if (!rc) {
1089                                 lq_data->search_better_tbl = 1;
1090                                 lq_data->action_counter = 0;
1091                         }
1092                         if (!rc)
1093                                 goto out;
1094                         break;
1095                 }
1096                 tbl->action++;
1097                 if (tbl->action > IWL_LEGACY_SWITCH_MIMO)
1098                         tbl->action = IWL_LEGACY_SWITCH_ANTENNA;
1099
1100                 if (tbl->action == start_action)
1101                         break;
1102
1103         }
1104         return 0;
1105
1106  out:
1107         tbl->action++;
1108         if (tbl->action > IWL_LEGACY_SWITCH_MIMO)
1109                 tbl->action = IWL_LEGACY_SWITCH_ANTENNA;
1110         return 0;
1111
1112 }
1113
1114 static int rs_move_siso_to_other(struct iwl_priv *priv,
1115                                  struct iwl_rate_scale_priv *lq_data,
1116                                  int index)
1117 {
1118         int rc = -1;
1119         u8 is_green = lq_data->is_green;
1120         struct iwl_scale_tbl_info *tbl =
1121             &(lq_data->lq_info[lq_data->active_tbl]);
1122         struct iwl_scale_tbl_info *search_tbl =
1123             &(lq_data->lq_info[(1 - lq_data->active_tbl)]);
1124         struct iwl_rate_scale_data *window = &(tbl->win[index]);
1125         u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1126                   (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1127         u8 start_action = tbl->action;
1128
1129         for (;;) {
1130                 lq_data->action_counter++;
1131                 switch (tbl->action) {
1132                 case IWL_SISO_SWITCH_ANTENNA:
1133                         IWL_DEBUG_HT("LQ: SISO SWITCH ANTENNA SISO\n");
1134                         search_tbl->lq_type = LQ_NONE;
1135                         if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1136                                 break;
1137                         if (!rs_is_other_ant_connected(lq_data->antenna,
1138                                                        tbl->antenna_type))
1139                                 break;
1140
1141                         memcpy(search_tbl, tbl, sz);
1142                         search_tbl->action = IWL_SISO_SWITCH_MIMO;
1143                         rs_toggle_antenna(&(search_tbl->current_rate),
1144                                            search_tbl);
1145                         lq_data->search_better_tbl = 1;
1146
1147                         goto out;
1148
1149                 case IWL_SISO_SWITCH_MIMO:
1150                         IWL_DEBUG_HT("LQ: SISO SWITCH TO MIMO FROM SISO\n");
1151                         memcpy(search_tbl, tbl, sz);
1152                         search_tbl->lq_type = LQ_MIMO;
1153                         search_tbl->is_SGI = 0;
1154                         search_tbl->is_fat = 0;
1155                         search_tbl->antenna_type = ANT_BOTH;
1156                         rc = rs_switch_to_mimo(priv, lq_data, search_tbl,
1157                                                index);
1158                         if (!rc)
1159                                 lq_data->search_better_tbl = 1;
1160
1161                         if (!rc)
1162                                 goto out;
1163                         break;
1164                 case IWL_SISO_SWITCH_GI:
1165                         IWL_DEBUG_HT("LQ: SISO SWITCH TO GI\n");
1166                         memcpy(search_tbl, tbl, sz);
1167                         search_tbl->action = 0;
1168                         if (search_tbl->is_SGI)
1169                                 search_tbl->is_SGI = 0;
1170                         else if (!is_green)
1171                                 search_tbl->is_SGI = 1;
1172                         else
1173                                 break;
1174                         lq_data->search_better_tbl = 1;
1175                         if ((tbl->lq_type == LQ_SISO) &&
1176                             (tbl->is_SGI)) {
1177                                 s32 tpt = lq_data->last_tpt / 100;
1178                                 if (((!tbl->is_fat) &&
1179                                      (tpt >= expected_tpt_siso20MHz[index])) ||
1180                                     ((tbl->is_fat) &&
1181                                      (tpt >= expected_tpt_siso40MHz[index])))
1182                                         lq_data->search_better_tbl = 0;
1183                         }
1184                         rs_get_expected_tpt_table(lq_data, search_tbl);
1185                         rs_mcs_from_tbl(&search_tbl->current_rate,
1186                                              search_tbl, index, is_green);
1187                         goto out;
1188                 }
1189                 tbl->action++;
1190                 if (tbl->action > IWL_SISO_SWITCH_GI)
1191                         tbl->action = IWL_SISO_SWITCH_ANTENNA;
1192
1193                 if (tbl->action == start_action)
1194                         break;
1195         }
1196         return 0;
1197
1198  out:
1199         tbl->action++;
1200         if (tbl->action > IWL_SISO_SWITCH_GI)
1201                 tbl->action = IWL_SISO_SWITCH_ANTENNA;
1202         return 0;
1203 }
1204
1205 static int rs_move_mimo_to_other(struct iwl_priv *priv,
1206                                  struct iwl_rate_scale_priv *lq_data,
1207                                  int index)
1208 {
1209         int rc = -1;
1210         s8 is_green = lq_data->is_green;
1211         struct iwl_scale_tbl_info *tbl =
1212             &(lq_data->lq_info[lq_data->active_tbl]);
1213         struct iwl_scale_tbl_info *search_tbl =
1214             &(lq_data->lq_info[(1 - lq_data->active_tbl)]);
1215         u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1216                   (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1217         u8 start_action = tbl->action;
1218
1219         for (;;) {
1220                 lq_data->action_counter++;
1221                 switch (tbl->action) {
1222                 case IWL_MIMO_SWITCH_ANTENNA_A:
1223                 case IWL_MIMO_SWITCH_ANTENNA_B:
1224                         IWL_DEBUG_HT("LQ: MIMO SWITCH TO SISO\n");
1225                         memcpy(search_tbl, tbl, sz);
1226                         search_tbl->lq_type = LQ_SISO;
1227                         search_tbl->is_SGI = 0;
1228                         search_tbl->is_fat = 0;
1229                         if (tbl->action == IWL_MIMO_SWITCH_ANTENNA_A)
1230                                 search_tbl->antenna_type = ANT_MAIN;
1231                         else
1232                                 search_tbl->antenna_type = ANT_AUX;
1233
1234                         rc = rs_switch_to_siso(priv, lq_data, search_tbl,
1235                                                index);
1236                         if (!rc) {
1237                                 lq_data->search_better_tbl = 1;
1238                                 goto out;
1239                         }
1240                         break;
1241
1242                 case IWL_MIMO_SWITCH_GI:
1243                         IWL_DEBUG_HT("LQ: MIMO SWITCH TO GI\n");
1244                         memcpy(search_tbl, tbl, sz);
1245                         search_tbl->lq_type = LQ_MIMO;
1246                         search_tbl->antenna_type = ANT_BOTH;
1247                         search_tbl->action = 0;
1248                         if (search_tbl->is_SGI)
1249                                 search_tbl->is_SGI = 0;
1250                         else
1251                                 search_tbl->is_SGI = 1;
1252                         lq_data->search_better_tbl = 1;
1253                         if ((tbl->lq_type == LQ_MIMO) &&
1254                             (tbl->is_SGI)) {
1255                                 s32 tpt = lq_data->last_tpt / 100;
1256                                 if (((!tbl->is_fat) &&
1257                                      (tpt >= expected_tpt_mimo20MHz[index])) ||
1258                                     ((tbl->is_fat) &&
1259                                      (tpt >= expected_tpt_mimo40MHz[index])))
1260                                         lq_data->search_better_tbl = 0;
1261                         }
1262                         rs_get_expected_tpt_table(lq_data, search_tbl);
1263                         rs_mcs_from_tbl(&search_tbl->current_rate,
1264                                              search_tbl, index, is_green);
1265                         goto out;
1266
1267                 }
1268                 tbl->action++;
1269                 if (tbl->action > IWL_MIMO_SWITCH_GI)
1270                         tbl->action = IWL_MIMO_SWITCH_ANTENNA_A;
1271
1272                 if (tbl->action == start_action)
1273                         break;
1274         }
1275
1276         return 0;
1277  out:
1278         tbl->action++;
1279         if (tbl->action > IWL_MIMO_SWITCH_GI)
1280                 tbl->action = IWL_MIMO_SWITCH_ANTENNA_A;
1281         return 0;
1282
1283 }
1284
1285 static void rs_stay_in_table(struct iwl_rate_scale_priv *lq_data)
1286 {
1287         struct iwl_scale_tbl_info *tbl;
1288         int i;
1289         int active_tbl;
1290         int flush_interval_passed = 0;
1291
1292         active_tbl = lq_data->active_tbl;
1293
1294         tbl = &(lq_data->lq_info[active_tbl]);
1295
1296         if (lq_data->stay_in_tbl) {
1297
1298                 if (lq_data->flush_timer)
1299                         flush_interval_passed =
1300                             time_after(jiffies,
1301                                        (unsigned long)(lq_data->flush_timer +
1302                                         IWL_RATE_SCALE_FLUSH_INTVL));
1303
1304                 flush_interval_passed = 0;
1305                 if ((lq_data->total_failed > lq_data->max_failure_limit) ||
1306                     (lq_data->total_success > lq_data->max_success_limit) ||
1307                     ((!lq_data->search_better_tbl) && (lq_data->flush_timer)
1308                      && (flush_interval_passed))) {
1309                         IWL_DEBUG_HT("LQ: stay is expired %d %d %d\n:",
1310                                      lq_data->total_failed,
1311                                      lq_data->total_success,
1312                                      flush_interval_passed);
1313                         lq_data->stay_in_tbl = 0;
1314                         lq_data->total_failed = 0;
1315                         lq_data->total_success = 0;
1316                         lq_data->flush_timer = 0;
1317                 } else if (lq_data->table_count > 0) {
1318                         lq_data->table_count++;
1319                         if (lq_data->table_count >=
1320                             lq_data->table_count_limit) {
1321                                 lq_data->table_count = 0;
1322
1323                                 IWL_DEBUG_HT("LQ: stay in table clear win\n");
1324                                 for (i = 0; i < IWL_RATE_COUNT; i++)
1325                                         rs_rate_scale_clear_window(
1326                                                 &(tbl->win[i]));
1327                         }
1328                 }
1329
1330                 if (!lq_data->stay_in_tbl) {
1331                         for (i = 0; i < IWL_RATE_COUNT; i++)
1332                                 rs_rate_scale_clear_window(&(tbl->win[i]));
1333                 }
1334         }
1335 }
1336
1337 static void rs_rate_scale_perform(struct iwl_priv *priv,
1338                                   struct net_device *dev,
1339                                   struct ieee80211_hdr *hdr,
1340                                   struct sta_info *sta)
1341 {
1342         int low = IWL_RATE_INVALID;
1343         int high = IWL_RATE_INVALID;
1344         int index;
1345         int i;
1346         struct iwl_rate_scale_data *window = NULL;
1347         int current_tpt = IWL_INVALID_VALUE;
1348         int low_tpt = IWL_INVALID_VALUE;
1349         int high_tpt = IWL_INVALID_VALUE;
1350         u32 fail_count;
1351         s8 scale_action = 0;
1352         u16 fc, rate_mask;
1353         u8 update_lq = 0;
1354         struct iwl_rate_scale_priv *lq_data;
1355         struct iwl_scale_tbl_info *tbl, *tbl1;
1356         u16 rate_scale_index_msk = 0;
1357         struct iwl_rate mcs_rate;
1358         u8 is_green = 0;
1359         u8 active_tbl = 0;
1360         u8 done_search = 0;
1361         u16 high_low;
1362
1363         IWL_DEBUG_RATE("rate scale calculate new rate for skb\n");
1364
1365         fc = le16_to_cpu(hdr->frame_control);
1366         if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1)) {
1367                 /* Send management frames and broadcast/multicast data using
1368                  * lowest rate. */
1369                 /* TODO: this could probably be improved.. */
1370                 return;
1371         }
1372
1373         if (!sta || !sta->rate_ctrl_priv)
1374                 return;
1375
1376         if (!priv->lq_mngr.lq_ready) {
1377                 IWL_DEBUG_RATE("still rate scaling not ready\n");
1378                 return;
1379         }
1380         lq_data = (struct iwl_rate_scale_priv *)sta->rate_ctrl_priv;
1381
1382         if (!lq_data->search_better_tbl)
1383                 active_tbl = lq_data->active_tbl;
1384         else
1385                 active_tbl = 1 - lq_data->active_tbl;
1386
1387         tbl = &(lq_data->lq_info[active_tbl]);
1388         is_green = lq_data->is_green;
1389
1390         index = sta->last_txrate;
1391
1392         IWL_DEBUG_RATE("Rate scale index %d for type %d\n", index,
1393                        tbl->lq_type);
1394
1395         rs_get_supported_rates(lq_data, hdr, tbl->lq_type,
1396                                 &rate_mask);
1397
1398         IWL_DEBUG_RATE("mask 0x%04X \n", rate_mask);
1399
1400         /* mask with station rate restriction */
1401         if (is_legacy(tbl->lq_type)) {
1402                 if (lq_data->phymode == (u8) MODE_IEEE80211A)
1403                         rate_scale_index_msk = (u16) (rate_mask &
1404                                 (lq_data->supp_rates << IWL_FIRST_OFDM_RATE));
1405                 else
1406                         rate_scale_index_msk = (u16) (rate_mask &
1407                                                       lq_data->supp_rates);
1408
1409         } else
1410                 rate_scale_index_msk = rate_mask;
1411
1412         if (!rate_scale_index_msk)
1413                 rate_scale_index_msk = rate_mask;
1414
1415         if (index < 0 || !((1 << index) & rate_scale_index_msk)) {
1416                 index = IWL_INVALID_VALUE;
1417                 update_lq = 1;
1418
1419                 /* get the lowest availabe rate */
1420                 for (i = 0; i <= IWL_RATE_COUNT; i++) {
1421                         if ((1 << i) & rate_scale_index_msk)
1422                                 index = i;
1423                 }
1424
1425                 if (index == IWL_INVALID_VALUE) {
1426                         IWL_WARNING("Can not find a suitable rate\n");
1427                         return;
1428                 }
1429         }
1430
1431         if (!tbl->expected_tpt)
1432                 rs_get_expected_tpt_table(lq_data, tbl);
1433
1434         window = &(tbl->win[index]);
1435
1436         fail_count = window->counter - window->success_counter;
1437         if (((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
1438              (window->success_counter < IWL_RATE_MIN_SUCCESS_TH))
1439             || (tbl->expected_tpt == NULL)) {
1440                 IWL_DEBUG_RATE("LQ: still below TH succ %d total %d "
1441                                "for index %d\n",
1442                                window->success_counter, window->counter, index);
1443                 window->average_tpt = IWL_INVALID_VALUE;
1444                 rs_stay_in_table(lq_data);
1445                 if (update_lq) {
1446                         rs_mcs_from_tbl(&mcs_rate, tbl, index, is_green);
1447                         rs_fill_link_cmd(lq_data, &mcs_rate, &lq_data->lq);
1448                         rs_send_lq_cmd(priv, &lq_data->lq, CMD_ASYNC);
1449                 }
1450                 goto out;
1451
1452         } else
1453                 window->average_tpt = ((window->success_ratio *
1454                                         tbl->expected_tpt[index] + 64) / 128);
1455
1456         if (lq_data->search_better_tbl) {
1457                 int success_limit = IWL_RATE_SCALE_SWITCH;
1458
1459                 if ((window->success_ratio > success_limit) ||
1460                     (window->average_tpt > lq_data->last_tpt)) {
1461                         if (!is_legacy(tbl->lq_type)) {
1462                                 IWL_DEBUG_HT("LQ: we are switching to HT"
1463                                              " rate suc %d current tpt %d"
1464                                              " old tpt %d\n",
1465                                              window->success_ratio,
1466                                              window->average_tpt,
1467                                              lq_data->last_tpt);
1468                                 lq_data->enable_counter = 1;
1469                         }
1470                         lq_data->active_tbl = active_tbl;
1471                         current_tpt = window->average_tpt;
1472                 } else {
1473                         tbl->lq_type = LQ_NONE;
1474                         active_tbl = lq_data->active_tbl;
1475                         tbl = &(lq_data->lq_info[active_tbl]);
1476
1477                         index = iwl_rate_index_from_plcp(
1478                                 tbl->current_rate.rate_n_flags);
1479
1480                         update_lq = 1;
1481                         current_tpt = lq_data->last_tpt;
1482                         IWL_DEBUG_HT("XXY GO BACK TO OLD TABLE\n");
1483                 }
1484                 lq_data->search_better_tbl = 0;
1485                 done_search = 1;
1486                 goto lq_update;
1487         }
1488
1489         high_low = rs_get_adjacent_rate(index, rate_scale_index_msk,
1490                                         tbl->lq_type);
1491         low = high_low & 0xff;
1492         high = (high_low >> 8) & 0xff;
1493
1494         current_tpt = window->average_tpt;
1495
1496         if (low != IWL_RATE_INVALID)
1497                 low_tpt = tbl->win[low].average_tpt;
1498
1499         if (high != IWL_RATE_INVALID)
1500                 high_tpt = tbl->win[high].average_tpt;
1501
1502
1503         scale_action = 1;
1504
1505         if ((window->success_ratio <= IWL_RATE_DECREASE_TH) ||
1506             (current_tpt == 0)) {
1507                 IWL_DEBUG_RATE("decrease rate because of low success_ratio\n");
1508                 scale_action = -1;
1509         } else if ((low_tpt == IWL_INVALID_VALUE) &&
1510                    (high_tpt == IWL_INVALID_VALUE))
1511                 scale_action = 1;
1512         else if ((low_tpt != IWL_INVALID_VALUE) &&
1513                  (high_tpt != IWL_INVALID_VALUE) &&
1514                  (low_tpt < current_tpt) &&
1515                  (high_tpt < current_tpt))
1516                 scale_action = 0;
1517         else {
1518                 if (high_tpt != IWL_INVALID_VALUE) {
1519                         if (high_tpt > current_tpt)
1520                                 scale_action = 1;
1521                         else {
1522                                 IWL_DEBUG_RATE
1523                                     ("decrease rate because of high tpt\n");
1524                                 scale_action = -1;
1525                         }
1526                 } else if (low_tpt != IWL_INVALID_VALUE) {
1527                         if (low_tpt > current_tpt) {
1528                                 IWL_DEBUG_RATE
1529                                     ("decrease rate because of low tpt\n");
1530                                 scale_action = -1;
1531                         } else
1532                                 scale_action = 1;
1533                 }
1534         }
1535
1536         if (scale_action == -1) {
1537                 if ((low != IWL_RATE_INVALID) &&
1538                     ((window->success_ratio > IWL_RATE_HIGH_TH) ||
1539                      (current_tpt > (100 * tbl->expected_tpt[low]))))
1540                         scale_action = 0;
1541         } else if ((scale_action == 1) &&
1542                    (window->success_ratio < IWL_RATE_INCREASE_TH))
1543                 scale_action = 0;
1544
1545         switch (scale_action) {
1546         case -1:
1547                 if (low != IWL_RATE_INVALID) {
1548                         update_lq = 1;
1549                         index = low;
1550                 }
1551                 break;
1552         case 1:
1553                 if (high != IWL_RATE_INVALID) {
1554                         update_lq = 1;
1555                         index = high;
1556                 }
1557
1558                 break;
1559         case 0:
1560         default:
1561                 break;
1562         }
1563
1564         IWL_DEBUG_HT("choose rate scale index %d action %d low %d "
1565                     "high %d type %d\n",
1566                      index, scale_action, low, high, tbl->lq_type);
1567
1568  lq_update:
1569         if (update_lq) {
1570                 rs_mcs_from_tbl(&mcs_rate, tbl, index, is_green);
1571                 rs_fill_link_cmd(lq_data, &mcs_rate, &lq_data->lq);
1572                 rs_send_lq_cmd(priv, &lq_data->lq, CMD_ASYNC);
1573         }
1574         rs_stay_in_table(lq_data);
1575
1576         if (!update_lq && !done_search && !lq_data->stay_in_tbl) {
1577                 lq_data->last_tpt = current_tpt;
1578
1579                 if (is_legacy(tbl->lq_type))
1580                         rs_move_legacy_other(priv, lq_data, index);
1581                 else if (is_siso(tbl->lq_type))
1582                         rs_move_siso_to_other(priv, lq_data, index);
1583                 else
1584                         rs_move_mimo_to_other(priv, lq_data, index);
1585
1586                 if (lq_data->search_better_tbl) {
1587                         tbl = &(lq_data->lq_info[(1 - lq_data->active_tbl)]);
1588                         for (i = 0; i < IWL_RATE_COUNT; i++)
1589                                 rs_rate_scale_clear_window(&(tbl->win[i]));
1590
1591                         index = iwl_rate_index_from_plcp(
1592                                         tbl->current_rate.rate_n_flags);
1593
1594                         IWL_DEBUG_HT("Switch current  mcs: %X index: %d\n",
1595                                      tbl->current_rate.rate_n_flags, index);
1596                         rs_fill_link_cmd(lq_data, &tbl->current_rate,
1597                                          &lq_data->lq);
1598                         rs_send_lq_cmd(priv, &lq_data->lq, CMD_ASYNC);
1599                 }
1600                 tbl1 = &(lq_data->lq_info[lq_data->active_tbl]);
1601
1602                 if (is_legacy(tbl1->lq_type) &&
1603 #ifdef CONFIG_IWLWIFI_HT
1604                     !priv->current_assoc_ht.is_ht &&
1605 #endif
1606                     (lq_data->action_counter >= 1)) {
1607                         lq_data->action_counter = 0;
1608                         IWL_DEBUG_HT("LQ: STAY in legacy table\n");
1609                         rs_set_stay_in_table(1, lq_data);
1610                 }
1611
1612                 if (lq_data->enable_counter &&
1613                     (lq_data->action_counter >= IWL_ACTION_LIMIT)) {
1614 #ifdef CONFIG_IWLWIFI_HT_AGG
1615                         if ((lq_data->last_tpt > TID_AGG_TPT_THREHOLD) &&
1616                             (priv->lq_mngr.agg_ctrl.auto_agg)) {
1617                                 priv->lq_mngr.agg_ctrl.tid_retry =
1618                                     TID_ALL_SPECIFIED;
1619                                 schedule_work(&priv->agg_work);
1620                         }
1621 #endif /*CONFIG_IWLWIFI_HT_AGG */
1622                         lq_data->action_counter = 0;
1623                         rs_set_stay_in_table(0, lq_data);
1624                 }
1625         } else {
1626                 if ((!update_lq) && (!done_search) && (!lq_data->flush_timer))
1627                         lq_data->flush_timer = jiffies;
1628         }
1629
1630 out:
1631         rs_mcs_from_tbl(&tbl->current_rate, tbl, index, is_green);
1632         i = index;
1633         sta->last_txrate = i;
1634
1635         /* sta->txrate is an index to A mode rates which start
1636          * at IWL_FIRST_OFDM_RATE
1637          */
1638         if (lq_data->phymode == (u8) MODE_IEEE80211A)
1639                 sta->txrate = i - IWL_FIRST_OFDM_RATE;
1640         else
1641                 sta->txrate = i;
1642
1643         return;
1644 }
1645
1646
1647 static void rs_initialize_lq(struct iwl_priv *priv,
1648                              struct sta_info *sta)
1649 {
1650         int i;
1651         struct iwl_rate_scale_priv *lq;
1652         struct iwl_scale_tbl_info *tbl;
1653         u8 active_tbl = 0;
1654         int rate_idx;
1655         u8 use_green = rs_use_green(priv);
1656         struct iwl_rate mcs_rate;
1657
1658         if (!sta || !sta->rate_ctrl_priv)
1659                 goto out;
1660
1661         lq = (struct iwl_rate_scale_priv *)sta->rate_ctrl_priv;
1662         i = sta->last_txrate;
1663
1664         if ((lq->lq.sta_id == 0xff) &&
1665             (priv->iw_mode == IEEE80211_IF_TYPE_IBSS))
1666                 goto out;
1667
1668         if (!lq->search_better_tbl)
1669                 active_tbl = lq->active_tbl;
1670         else
1671                 active_tbl = 1 - lq->active_tbl;
1672
1673         tbl = &(lq->lq_info[active_tbl]);
1674
1675         if ((i < 0) || (i >= IWL_RATE_COUNT))
1676                 i = 0;
1677
1678         mcs_rate.rate_n_flags = iwl_rates[i].plcp ;
1679         mcs_rate.rate_n_flags |= RATE_MCS_ANT_B_MSK;
1680         mcs_rate.rate_n_flags &= ~RATE_MCS_ANT_A_MSK;
1681
1682         if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
1683                 mcs_rate.rate_n_flags |= RATE_MCS_CCK_MSK;
1684
1685         tbl->antenna_type = ANT_AUX;
1686         rs_get_tbl_info_from_mcs(&mcs_rate, priv->phymode, tbl, &rate_idx);
1687         if (!rs_is_ant_connected(priv->valid_antenna, tbl->antenna_type))
1688             rs_toggle_antenna(&mcs_rate, tbl);
1689
1690         rs_mcs_from_tbl(&mcs_rate, tbl, rate_idx, use_green);
1691         tbl->current_rate.rate_n_flags = mcs_rate.rate_n_flags;
1692         rs_get_expected_tpt_table(lq, tbl);
1693         rs_fill_link_cmd(lq, &mcs_rate, &lq->lq);
1694         rs_send_lq_cmd(priv, &lq->lq, CMD_ASYNC);
1695  out:
1696         return;
1697 }
1698
1699 static struct ieee80211_rate *rs_get_lowest_rate(struct ieee80211_local
1700                                                  *local)
1701 {
1702         struct ieee80211_hw_mode *mode = local->oper_hw_mode;
1703         int i;
1704
1705         for (i = 0; i < mode->num_rates; i++) {
1706                 struct ieee80211_rate *rate = &mode->rates[i];
1707
1708                 if (rate->flags & IEEE80211_RATE_SUPPORTED)
1709                         return rate;
1710         }
1711
1712         return &mode->rates[0];
1713 }
1714
1715 static struct ieee80211_rate *rs_get_rate(void *priv_rate,
1716                                                struct net_device *dev,
1717                                                struct sk_buff *skb,
1718                                                struct rate_control_extra
1719                                                *extra)
1720 {
1721
1722         int i;
1723         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1724         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1725         struct sta_info *sta;
1726         u16 fc;
1727         struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
1728         struct iwl_rate_scale_priv *lq;
1729
1730         IWL_DEBUG_RATE_LIMIT("rate scale calculate new rate for skb\n");
1731
1732         memset(extra, 0, sizeof(*extra));
1733
1734         fc = le16_to_cpu(hdr->frame_control);
1735         if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1)) {
1736                 /* Send management frames and broadcast/multicast data using
1737                  * lowest rate. */
1738                 /* TODO: this could probably be improved.. */
1739                 return rs_get_lowest_rate(local);
1740         }
1741
1742         sta = sta_info_get(local, hdr->addr1);
1743
1744         if (!sta || !sta->rate_ctrl_priv) {
1745                 if (sta)
1746                         sta_info_put(sta);
1747                 return rs_get_lowest_rate(local);
1748         }
1749
1750         lq = (struct iwl_rate_scale_priv *)sta->rate_ctrl_priv;
1751         i = sta->last_txrate;
1752
1753         if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) && !lq->ibss_sta_added) {
1754                 u8 sta_id = iwl_hw_find_station(priv, hdr->addr1);
1755                 DECLARE_MAC_BUF(mac);
1756
1757                 if (sta_id == IWL_INVALID_STATION) {
1758                         IWL_DEBUG_RATE("LQ: ADD station %s\n",
1759                                        print_mac(mac, hdr->addr1));
1760                         sta_id = iwl_add_station(priv,
1761                                                  hdr->addr1, 0, CMD_ASYNC);
1762                 }
1763                 if ((sta_id != IWL_INVALID_STATION)) {
1764                         lq->lq.sta_id = sta_id;
1765                         lq->lq.rs_table[0].rate_n_flags = 0;
1766                         lq->ibss_sta_added = 1;
1767                         rs_initialize_lq(priv, sta);
1768                 }
1769                 if (!lq->ibss_sta_added)
1770                         goto done;
1771         }
1772
1773  done:
1774         sta_info_put(sta);
1775         if ((i < 0) || (i > IWL_RATE_COUNT))
1776                 return rs_get_lowest_rate(local);
1777
1778         return &priv->ieee_rates[i];
1779 }
1780
1781 static void *rs_alloc_sta(void *priv, gfp_t gfp)
1782 {
1783         struct iwl_rate_scale_priv *crl;
1784         int i, j;
1785
1786         IWL_DEBUG_RATE("create station rate scale window\n");
1787
1788         crl = kzalloc(sizeof(struct iwl_rate_scale_priv), gfp);
1789
1790         if (crl == NULL)
1791                 return NULL;
1792         crl->lq.sta_id = 0xff;
1793
1794
1795         for (j = 0; j < LQ_SIZE; j++)
1796                 for (i = 0; i < IWL_RATE_COUNT; i++)
1797                         rs_rate_scale_clear_window(&(crl->lq_info[j].win[i]));
1798
1799         return crl;
1800 }
1801
1802 static void rs_rate_init(void *priv_rate, void *priv_sta,
1803                          struct ieee80211_local *local,
1804                          struct sta_info *sta)
1805 {
1806         int i, j;
1807         struct ieee80211_hw_mode *mode = local->oper_hw_mode;
1808         struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
1809         struct iwl_rate_scale_priv *crl = priv_sta;
1810
1811         crl->flush_timer = 0;
1812         crl->supp_rates = sta->supp_rates;
1813         sta->txrate = 3;
1814         for (j = 0; j < LQ_SIZE; j++)
1815                 for (i = 0; i < IWL_RATE_COUNT; i++)
1816                         rs_rate_scale_clear_window(&(crl->lq_info[j].win[i]));
1817
1818         IWL_DEBUG_RATE("rate scale global init\n");
1819         /* TODO: what is a good starting rate for STA? About middle? Maybe not
1820          * the lowest or the highest rate.. Could consider using RSSI from
1821          * previous packets? Need to have IEEE 802.1X auth succeed immediately
1822          * after assoc.. */
1823
1824         crl->ibss_sta_added = 0;
1825         if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
1826                 u8 sta_id = iwl_hw_find_station(priv, sta->addr);
1827                 DECLARE_MAC_BUF(mac);
1828
1829                 /* for IBSS the call are from tasklet */
1830                 IWL_DEBUG_HT("LQ: ADD station %s\n",
1831                              print_mac(mac, sta->addr));
1832
1833                 if (sta_id == IWL_INVALID_STATION) {
1834                         IWL_DEBUG_RATE("LQ: ADD station %s\n",
1835                                        print_mac(mac, sta->addr));
1836                         sta_id = iwl_add_station(priv,
1837                                                  sta->addr, 0, CMD_ASYNC);
1838                 }
1839                 if ((sta_id != IWL_INVALID_STATION)) {
1840                         crl->lq.sta_id = sta_id;
1841                         crl->lq.rs_table[0].rate_n_flags = 0;
1842                 }
1843                 /* FIXME: this is w/a remove it later */
1844                 priv->assoc_station_added = 1;
1845         }
1846
1847         for (i = 0; i < mode->num_rates; i++) {
1848                 if ((sta->supp_rates & BIT(i)) &&
1849                     (mode->rates[i].flags & IEEE80211_RATE_SUPPORTED))
1850                         sta->txrate = i;
1851         }
1852         sta->last_txrate = sta->txrate;
1853         /* For MODE_IEEE80211A mode cck rate are at end
1854          * rate table
1855          */
1856         if (local->hw.conf.phymode == MODE_IEEE80211A)
1857                 sta->last_txrate += IWL_FIRST_OFDM_RATE;
1858
1859         crl->is_dup = priv->is_dup;
1860         crl->valid_antenna = priv->valid_antenna;
1861         crl->antenna = priv->antenna;
1862         crl->is_green = rs_use_green(priv);
1863         crl->active_rate = priv->active_rate;
1864         crl->active_rate &= ~(0x1000);
1865         crl->active_rate_basic = priv->active_rate_basic;
1866         crl->phymode = priv->phymode;
1867 #ifdef CONFIG_IWLWIFI_HT
1868         crl->active_siso_rate = (priv->current_assoc_ht.supp_rates[0] << 1);
1869         crl->active_siso_rate |= (priv->current_assoc_ht.supp_rates[0] & 0x1);
1870         crl->active_siso_rate &= ~((u16)0x2);
1871         crl->active_siso_rate = crl->active_siso_rate << IWL_FIRST_OFDM_RATE;
1872
1873         crl->active_mimo_rate = (priv->current_assoc_ht.supp_rates[1] << 1);
1874         crl->active_mimo_rate |= (priv->current_assoc_ht.supp_rates[1] & 0x1);
1875         crl->active_mimo_rate &= ~((u16)0x2);
1876         crl->active_mimo_rate = crl->active_mimo_rate << IWL_FIRST_OFDM_RATE;
1877         IWL_DEBUG_HT("MIMO RATE 0x%X SISO MASK 0x%X\n", crl->active_siso_rate,
1878                      crl->active_mimo_rate);
1879 #endif /*CONFIG_IWLWIFI_HT*/
1880 #ifdef CONFIG_MAC80211_DEBUGFS
1881         crl->drv = priv;
1882 #endif
1883
1884         if (priv->assoc_station_added)
1885                 priv->lq_mngr.lq_ready = 1;
1886
1887         rs_initialize_lq(priv, sta);
1888 }
1889
1890 static void rs_fill_link_cmd(struct iwl_rate_scale_priv *lq_data,
1891                             struct iwl_rate *tx_mcs,
1892                             struct iwl_link_quality_cmd *lq_cmd)
1893 {
1894         int index = 0;
1895         int rate_idx;
1896         int repeat_rate = 0;
1897         u8 ant_toggle_count = 0;
1898         u8 use_ht_possible = 1;
1899         struct iwl_rate new_rate;
1900         struct iwl_scale_tbl_info tbl_type = { 0 };
1901
1902         rs_dbgfs_set_mcs(lq_data, tx_mcs, index);
1903
1904         rs_get_tbl_info_from_mcs(tx_mcs, lq_data->phymode,
1905                                   &tbl_type, &rate_idx);
1906
1907         if (is_legacy(tbl_type.lq_type)) {
1908                 ant_toggle_count = 1;
1909                 repeat_rate = IWL_NUMBER_TRY;
1910         } else
1911                 repeat_rate = IWL_HT_NUMBER_TRY;
1912
1913         lq_cmd->general_params.mimo_delimiter =
1914                         is_mimo(tbl_type.lq_type) ? 1 : 0;
1915         lq_cmd->rs_table[index].rate_n_flags =
1916                         cpu_to_le32(tx_mcs->rate_n_flags);
1917         new_rate.rate_n_flags = tx_mcs->rate_n_flags;
1918
1919         if (is_mimo(tbl_type.lq_type) || (tbl_type.antenna_type == ANT_MAIN))
1920                 lq_cmd->general_params.single_stream_ant_msk = 1;
1921         else
1922                 lq_cmd->general_params.single_stream_ant_msk = 2;
1923
1924         index++;
1925         repeat_rate--;
1926
1927         while (index < LINK_QUAL_MAX_RETRY_NUM) {
1928                 while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
1929                         if (is_legacy(tbl_type.lq_type)) {
1930                                 if (ant_toggle_count <
1931                                     NUM_TRY_BEFORE_ANTENNA_TOGGLE)
1932                                         ant_toggle_count++;
1933                                 else {
1934                                         rs_toggle_antenna(&new_rate, &tbl_type);
1935                                         ant_toggle_count = 1;
1936                                 }
1937                         }
1938
1939                         rs_dbgfs_set_mcs(lq_data, &new_rate, index);
1940                         lq_cmd->rs_table[index].rate_n_flags =
1941                                         cpu_to_le32(new_rate.rate_n_flags);
1942                         repeat_rate--;
1943                         index++;
1944                 }
1945
1946                 rs_get_tbl_info_from_mcs(&new_rate, lq_data->phymode, &tbl_type,
1947                                                 &rate_idx);
1948
1949                 if (is_mimo(tbl_type.lq_type))
1950                         lq_cmd->general_params.mimo_delimiter = index;
1951
1952                 rs_get_lower_rate(lq_data, &tbl_type, rate_idx,
1953                                   use_ht_possible, &new_rate);
1954
1955                 if (is_legacy(tbl_type.lq_type)) {
1956                         if (ant_toggle_count < NUM_TRY_BEFORE_ANTENNA_TOGGLE)
1957                                 ant_toggle_count++;
1958                         else {
1959                                 rs_toggle_antenna(&new_rate, &tbl_type);
1960                                 ant_toggle_count = 1;
1961                         }
1962                         repeat_rate = IWL_NUMBER_TRY;
1963                 } else
1964                         repeat_rate = IWL_HT_NUMBER_TRY;
1965
1966                 use_ht_possible = 0;
1967
1968                 rs_dbgfs_set_mcs(lq_data, &new_rate, index);
1969                 lq_cmd->rs_table[index].rate_n_flags =
1970                                 cpu_to_le32(new_rate.rate_n_flags);
1971
1972                 index++;
1973                 repeat_rate--;
1974         }
1975
1976         lq_cmd->general_params.dual_stream_ant_msk = 3;
1977         lq_cmd->agg_params.agg_dis_start_th = 3;
1978         lq_cmd->agg_params.agg_time_limit = cpu_to_le16(4000);
1979 }
1980
1981 static void *rs_alloc(struct ieee80211_local *local)
1982 {
1983         return local->hw.priv;
1984 }
1985 /* rate scale requires free function to be implemented */
1986 static void rs_free(void *priv_rate)
1987 {
1988         return;
1989 }
1990
1991 static void rs_clear(void *priv_rate)
1992 {
1993         struct iwl_priv *priv = (struct iwl_priv *) priv_rate;
1994
1995         IWL_DEBUG_RATE("enter\n");
1996
1997         priv->lq_mngr.lq_ready = 0;
1998 #ifdef CONFIG_IWLWIFI_HT
1999 #ifdef CONFIG_IWLWIFI_HT_AGG
2000         if (priv->lq_mngr.agg_ctrl.granted_ba)
2001                 iwl4965_turn_off_agg(priv, TID_ALL_SPECIFIED);
2002 #endif /*CONFIG_IWLWIFI_HT_AGG */
2003 #endif /* CONFIG_IWLWIFI_HT */
2004
2005         IWL_DEBUG_RATE("leave\n");
2006 }
2007
2008 static void rs_free_sta(void *priv, void *priv_sta)
2009 {
2010         struct iwl_rate_scale_priv *rs_priv = priv_sta;
2011
2012         IWL_DEBUG_RATE("enter\n");
2013         kfree(rs_priv);
2014         IWL_DEBUG_RATE("leave\n");
2015 }
2016
2017
2018 #ifdef CONFIG_MAC80211_DEBUGFS
2019 static int open_file_generic(struct inode *inode, struct file *file)
2020 {
2021         file->private_data = inode->i_private;
2022         return 0;
2023 }
2024 static void rs_dbgfs_set_mcs(struct iwl_rate_scale_priv *rs_priv,
2025                                 struct iwl_rate *mcs, int index)
2026 {
2027         const u32 cck_rate = 0x820A;
2028         if (rs_priv->dbg_fixed.rate_n_flags) {
2029                 if (index < 12)
2030                         mcs->rate_n_flags = rs_priv->dbg_fixed.rate_n_flags;
2031                 else
2032                         mcs->rate_n_flags = cck_rate;
2033                 IWL_DEBUG_RATE("Fixed rate ON\n");
2034                 return;
2035         }
2036
2037         IWL_DEBUG_RATE("Fixed rate OFF\n");
2038 }
2039
2040 static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
2041                         const char __user *user_buf, size_t count, loff_t *ppos)
2042 {
2043         struct iwl_rate_scale_priv *rs_priv = file->private_data;
2044         char buf[64];
2045         int buf_size;
2046         u32 parsed_rate;
2047
2048         memset(buf, 0, sizeof(buf));
2049         buf_size = min(count, sizeof(buf) -  1);
2050         if (copy_from_user(buf, user_buf, buf_size))
2051                 return -EFAULT;
2052
2053         if (sscanf(buf, "%x", &parsed_rate) == 1)
2054                 rs_priv->dbg_fixed.rate_n_flags = parsed_rate;
2055         else
2056                 rs_priv->dbg_fixed.rate_n_flags = 0;
2057
2058         rs_priv->active_rate = 0x0FFF;
2059         rs_priv->active_siso_rate = 0x1FD0;
2060         rs_priv->active_mimo_rate = 0x1FD0;
2061
2062         IWL_DEBUG_RATE("sta_id %d rate 0x%X\n",
2063                 rs_priv->lq.sta_id, rs_priv->dbg_fixed.rate_n_flags);
2064
2065         if (rs_priv->dbg_fixed.rate_n_flags) {
2066                 rs_fill_link_cmd(rs_priv, &rs_priv->dbg_fixed, &rs_priv->lq);
2067                 rs_send_lq_cmd(rs_priv->drv, &rs_priv->lq, CMD_ASYNC);
2068         }
2069
2070         return count;
2071 }
2072
2073 static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
2074                         char __user *user_buf, size_t count, loff_t *ppos)
2075 {
2076         char buff[1024];
2077         int desc = 0;
2078         int i = 0;
2079
2080         struct iwl_rate_scale_priv *rs_priv = file->private_data;
2081
2082         desc += sprintf(buff+desc, "sta_id %d\n", rs_priv->lq.sta_id);
2083         desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
2084                         rs_priv->total_failed, rs_priv->total_success,
2085                         rs_priv->active_rate);
2086         desc += sprintf(buff+desc, "fixed rate 0x%X\n",
2087                         rs_priv->dbg_fixed.rate_n_flags);
2088         desc += sprintf(buff+desc, "general:"
2089                 "flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
2090                 rs_priv->lq.general_params.flags,
2091                 rs_priv->lq.general_params.mimo_delimiter,
2092                 rs_priv->lq.general_params.single_stream_ant_msk,
2093                 rs_priv->lq.general_params.dual_stream_ant_msk);
2094
2095         desc += sprintf(buff+desc, "agg:"
2096                         "time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
2097                         le16_to_cpu(rs_priv->lq.agg_params.agg_time_limit),
2098                         rs_priv->lq.agg_params.agg_dis_start_th,
2099                         rs_priv->lq.agg_params.agg_frame_cnt_limit);
2100
2101         desc += sprintf(buff+desc,
2102                         "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
2103                         rs_priv->lq.general_params.start_rate_index[0],
2104                         rs_priv->lq.general_params.start_rate_index[1],
2105                         rs_priv->lq.general_params.start_rate_index[2],
2106                         rs_priv->lq.general_params.start_rate_index[3]);
2107
2108
2109         for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
2110                 desc += sprintf(buff+desc, " rate[%d] 0x%X\n",
2111                         i, le32_to_cpu(rs_priv->lq.rs_table[i].rate_n_flags));
2112
2113         return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2114 }
2115
2116 static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
2117         .write = rs_sta_dbgfs_scale_table_write,
2118         .read = rs_sta_dbgfs_scale_table_read,
2119         .open = open_file_generic,
2120 };
2121 static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
2122                         char __user *user_buf, size_t count, loff_t *ppos)
2123 {
2124         char buff[1024];
2125         int desc = 0;
2126         int i, j;
2127
2128         struct iwl_rate_scale_priv *rs_priv = file->private_data;
2129         for (i = 0; i < LQ_SIZE; i++) {
2130                 desc += sprintf(buff+desc, "%s type=%d SGI=%d FAT=%d DUP=%d\n"
2131                                 "rate=0x%X\n",
2132                                 rs_priv->active_tbl == i?"*":"x",
2133                                 rs_priv->lq_info[i].lq_type,
2134                                 rs_priv->lq_info[i].is_SGI,
2135                                 rs_priv->lq_info[i].is_fat,
2136                                 rs_priv->lq_info[i].is_dup,
2137                                 rs_priv->lq_info[i].current_rate.rate_n_flags);
2138                 for (j = 0; j < IWL_RATE_COUNT; j++) {
2139                         desc += sprintf(buff+desc,
2140                                         "counter=%d success=%d %%=%d\n",
2141                                         rs_priv->lq_info[i].win[j].counter,
2142                                         rs_priv->lq_info[i].win[j].success_counter,
2143                                         rs_priv->lq_info[i].win[j].success_ratio);
2144                 }
2145         }
2146         return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2147 }
2148
2149 static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
2150         .read = rs_sta_dbgfs_stats_table_read,
2151         .open = open_file_generic,
2152 };
2153
2154 static void rs_add_debugfs(void *priv, void *priv_sta,
2155                                         struct dentry *dir)
2156 {
2157         struct iwl_rate_scale_priv *rs_priv = priv_sta;
2158         rs_priv->rs_sta_dbgfs_scale_table_file =
2159                 debugfs_create_file("rate_scale_table", 0600, dir,
2160                                 rs_priv, &rs_sta_dbgfs_scale_table_ops);
2161         rs_priv->rs_sta_dbgfs_stats_table_file =
2162                 debugfs_create_file("rate_stats_table", 0600, dir,
2163                         rs_priv, &rs_sta_dbgfs_stats_table_ops);
2164 }
2165
2166 static void rs_remove_debugfs(void *priv, void *priv_sta)
2167 {
2168         struct iwl_rate_scale_priv *rs_priv = priv_sta;
2169         debugfs_remove(rs_priv->rs_sta_dbgfs_scale_table_file);
2170         debugfs_remove(rs_priv->rs_sta_dbgfs_stats_table_file);
2171 }
2172 #endif
2173
2174 static struct rate_control_ops rs_ops = {
2175         .module = NULL,
2176         .name = RS_NAME,
2177         .tx_status = rs_tx_status,
2178         .get_rate = rs_get_rate,
2179         .rate_init = rs_rate_init,
2180         .clear = rs_clear,
2181         .alloc = rs_alloc,
2182         .free = rs_free,
2183         .alloc_sta = rs_alloc_sta,
2184         .free_sta = rs_free_sta,
2185 #ifdef CONFIG_MAC80211_DEBUGFS
2186         .add_sta_debugfs = rs_add_debugfs,
2187         .remove_sta_debugfs = rs_remove_debugfs,
2188 #endif
2189 };
2190
2191 int iwl_fill_rs_info(struct ieee80211_hw *hw, char *buf, u8 sta_id)
2192 {
2193         struct ieee80211_local *local = hw_to_local(hw);
2194         struct iwl_priv *priv = hw->priv;
2195         struct iwl_rate_scale_priv *rs_priv;
2196         struct sta_info *sta;
2197         int count = 0, i;
2198         u32 samples = 0, success = 0, good = 0;
2199         unsigned long now = jiffies;
2200         u32 max_time = 0;
2201         u8 lq_type, antenna;
2202
2203         sta = sta_info_get(local, priv->stations[sta_id].sta.sta.addr);
2204         if (!sta || !sta->rate_ctrl_priv) {
2205                 if (sta) {
2206                         sta_info_put(sta);
2207                         IWL_DEBUG_RATE("leave - no private rate data!\n");
2208                 } else
2209                         IWL_DEBUG_RATE("leave - no station!\n");
2210                 return sprintf(buf, "station %d not found\n", sta_id);
2211         }
2212
2213         rs_priv = (void *)sta->rate_ctrl_priv;
2214
2215         lq_type = rs_priv->lq_info[rs_priv->active_tbl].lq_type;
2216         antenna = rs_priv->lq_info[rs_priv->active_tbl].antenna_type;
2217
2218         if (is_legacy(lq_type))
2219                 i = IWL_RATE_54M_INDEX;
2220         else
2221                 i = IWL_RATE_60M_INDEX;
2222         while (1) {
2223                 u64 mask;
2224                 int j;
2225                 int active = rs_priv->active_tbl;
2226
2227                 count +=
2228                     sprintf(&buf[count], " %2dMbs: ", iwl_rates[i].ieee / 2);
2229
2230                 mask = (1ULL << (IWL_RATE_MAX_WINDOW - 1));
2231                 for (j = 0; j < IWL_RATE_MAX_WINDOW; j++, mask >>= 1)
2232                         buf[count++] =
2233                                 (rs_priv->lq_info[active].win[i].data & mask)
2234                                 ? '1' : '0';
2235
2236                 samples += rs_priv->lq_info[active].win[i].counter;
2237                 good += rs_priv->lq_info[active].win[i].success_counter;
2238                 success += rs_priv->lq_info[active].win[i].success_counter *
2239                            iwl_rates[i].ieee;
2240
2241                 if (rs_priv->lq_info[active].win[i].stamp) {
2242                         int delta =
2243                                    jiffies_to_msecs(now -
2244                                    rs_priv->lq_info[active].win[i].stamp);
2245
2246                         if (delta > max_time)
2247                                 max_time = delta;
2248
2249                         count += sprintf(&buf[count], "%5dms\n", delta);
2250                 } else
2251                         buf[count++] = '\n';
2252
2253                 j = iwl_get_prev_ieee_rate(i);
2254                 if (j == i)
2255                         break;
2256                 i = j;
2257         }
2258
2259         /* Display the average rate of all samples taken.
2260          *
2261          * NOTE:  We multiple # of samples by 2 since the IEEE measurement
2262          * added from iwl_rates is actually 2X the rate */
2263         if (samples)
2264                 count += sprintf(&buf[count],
2265                          "\nAverage rate is %3d.%02dMbs over last %4dms\n"
2266                          "%3d%% success (%d good packets over %d tries)\n",
2267                          success / (2 * samples), (success * 5 / samples) % 10,
2268                          max_time, good * 100 / samples, good, samples);
2269         else
2270                 count += sprintf(&buf[count], "\nAverage rate: 0Mbs\n");
2271         count += sprintf(&buf[count], "\nrate scale type %d anntena %d "
2272                          "active_search %d rate index %d\n", lq_type, antenna,
2273                          rs_priv->search_better_tbl, sta->last_txrate);
2274
2275         sta_info_put(sta);
2276         return count;
2277 }
2278
2279 void iwl_rate_scale_init(struct ieee80211_hw *hw, s32 sta_id)
2280 {
2281         struct iwl_priv *priv = hw->priv;
2282
2283         priv->lq_mngr.lq_ready = 1;
2284 }
2285
2286 void iwl_rate_control_register(struct ieee80211_hw *hw)
2287 {
2288         ieee80211_rate_control_register(&rs_ops);
2289 }
2290
2291 void iwl_rate_control_unregister(struct ieee80211_hw *hw)
2292 {
2293         ieee80211_rate_control_unregister(&rs_ops);
2294 }
2295