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