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