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