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