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