Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]
Version: [ 2.6.11.8 ] [ 2.6.25 ] [ 2.6.25.8 ] [ 2.6.31.13 ] Architecture: [ i386 ]
  1 /******************************************************************************
  2  *
  3  * Copyright(c) 2005 - 2009 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  *  Intel Linux Wireless <ilw@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 
 32 #include <linux/netdevice.h>
 33 #include <linux/etherdevice.h>
 34 #include <linux/delay.h>
 35 
 36 #include <linux/workqueue.h>
 37 
 38 #include "iwl-dev.h"
 39 #include "iwl-sta.h"
 40 #include "iwl-core.h"
 41 
 42 #define RS_NAME "iwl-agn-rs"
 43 
 44 #define NUM_TRY_BEFORE_ANT_TOGGLE 1
 45 #define IWL_NUMBER_TRY      1
 46 #define IWL_HT_NUMBER_TRY   3
 47 
 48 #define IWL_RATE_MAX_WINDOW             62      /* # tx in history window */
 49 #define IWL_RATE_MIN_FAILURE_TH         6       /* min failures to calc tpt */
 50 #define IWL_RATE_MIN_SUCCESS_TH         8       /* min successes to calc tpt */
 51 
 52 /* max allowed rate miss before sync LQ cmd */
 53 #define IWL_MISSED_RATE_MAX             15
 54 /* max time to accum history 2 seconds */
 55 #define IWL_RATE_SCALE_FLUSH_INTVL   (3*HZ)
 56 
 57 static u8 rs_ht_to_legacy[] = {
 58         IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
 59         IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
 60         IWL_RATE_6M_INDEX,
 61         IWL_RATE_6M_INDEX, IWL_RATE_9M_INDEX,
 62         IWL_RATE_12M_INDEX, IWL_RATE_18M_INDEX,
 63         IWL_RATE_24M_INDEX, IWL_RATE_36M_INDEX,
 64         IWL_RATE_48M_INDEX, IWL_RATE_54M_INDEX
 65 };
 66 
 67 static const u8 ant_toggle_lookup[] = {
 68         /*ANT_NONE -> */ ANT_NONE,
 69         /*ANT_A    -> */ ANT_B,
 70         /*ANT_B    -> */ ANT_C,
 71         /*ANT_AB   -> */ ANT_BC,
 72         /*ANT_C    -> */ ANT_A,
 73         /*ANT_AC   -> */ ANT_AB,
 74         /*ANT_BC   -> */ ANT_AC,
 75         /*ANT_ABC  -> */ ANT_ABC,
 76 };
 77 
 78 /**
 79  * struct iwl_rate_scale_data -- tx success history for one rate
 80  */
 81 struct iwl_rate_scale_data {
 82         u64 data;               /* bitmap of successful frames */
 83         s32 success_counter;    /* number of frames successful */
 84         s32 success_ratio;      /* per-cent * 128  */
 85         s32 counter;            /* number of frames attempted */
 86         s32 average_tpt;        /* success ratio * expected throughput */
 87         unsigned long stamp;
 88 };
 89 
 90 /**
 91  * struct iwl_scale_tbl_info -- tx params and success history for all rates
 92  *
 93  * There are two of these in struct iwl_lq_sta,
 94  * one for "active", and one for "search".
 95  */
 96 struct iwl_scale_tbl_info {
 97         enum iwl_table_type lq_type;
 98         u8 ant_type;
 99         u8 is_SGI;      /* 1 = short guard interval */
100         u8 is_fat;      /* 1 = 40 MHz channel width */
101         u8 is_dup;      /* 1 = duplicated data streams */
102         u8 action;      /* change modulation; IWL_[LEGACY/SISO/MIMO]_SWITCH_* */
103         u8 max_search;  /* maximun number of tables we can search */
104         s32 *expected_tpt;      /* throughput metrics; expected_tpt_G, etc. */
105         u32 current_rate;  /* rate_n_flags, uCode API format */
106         struct iwl_rate_scale_data win[IWL_RATE_COUNT]; /* rate histories */
107 };
108 
109 struct iwl_traffic_load {
110         unsigned long time_stamp;       /* age of the oldest statistics */
111         u32 packet_count[TID_QUEUE_MAX_SIZE];   /* packet count in this time
112                                                  * slice */
113         u32 total;                      /* total num of packets during the
114                                          * last TID_MAX_TIME_DIFF */
115         u8 queue_count;                 /* number of queues that has
116                                          * been used since the last cleanup */
117         u8 head;                        /* start of the circular buffer */
118 };
119 
120 /**
121  * struct iwl_lq_sta -- driver's rate scaling private structure
122  *
123  * Pointer to this gets passed back and forth between driver and mac80211.
124  */
125 struct iwl_lq_sta {
126         u8 active_tbl;          /* index of active table, range 0-1 */
127         u8 enable_counter;      /* indicates HT mode */
128         u8 stay_in_tbl;         /* 1: disallow, 0: allow search for new mode */
129         u8 search_better_tbl;   /* 1: currently trying alternate mode */
130         s32 last_tpt;
131 
132         /* The following determine when to search for a new mode */
133         u32 table_count_limit;
134         u32 max_failure_limit;  /* # failed frames before new search */
135         u32 max_success_limit;  /* # successful frames before new search */
136         u32 table_count;
137         u32 total_failed;       /* total failed frames, any/all rates */
138         u32 total_success;      /* total successful frames, any/all rates */
139         u64 flush_timer;        /* time staying in mode before new search */
140 
141         u8 action_counter;      /* # mode-switch actions tried */
142         u8 is_green;
143         u8 is_dup;
144         enum ieee80211_band band;
145         u8 ibss_sta_added;
146 
147         /* The following are bitmaps of rates; IWL_RATE_6M_MASK, etc. */
148         u32 supp_rates;
149         u16 active_legacy_rate;
150         u16 active_siso_rate;
151         u16 active_mimo2_rate;
152         u16 active_mimo3_rate;
153         u16 active_rate_basic;
154         s8 max_rate_idx;     /* Max rate set by user */
155         u8 missed_rate_counter;
156 
157         struct iwl_link_quality_cmd lq;
158         struct iwl_scale_tbl_info lq_info[LQ_SIZE]; /* "active", "search" */
159         struct iwl_traffic_load load[TID_MAX_LOAD_COUNT];
160         u8 tx_agg_tid_en;
161 #ifdef CONFIG_MAC80211_DEBUGFS
162         struct dentry *rs_sta_dbgfs_scale_table_file;
163         struct dentry *rs_sta_dbgfs_stats_table_file;
164         struct dentry *rs_sta_dbgfs_rate_scale_data_file;
165         struct dentry *rs_sta_dbgfs_tx_agg_tid_en_file;
166         u32 dbg_fixed_rate;
167 #endif
168         struct iwl_priv *drv;
169 
170         /* used to be in sta_info */
171         int last_txrate_idx;
172         /* last tx rate_n_flags */
173         u32 last_rate_n_flags;
174 };
175 
176 static void rs_rate_scale_perform(struct iwl_priv *priv,
177                                    struct sk_buff *skb,
178                                    struct ieee80211_sta *sta,
179                                    struct iwl_lq_sta *lq_sta);
180 static void rs_fill_link_cmd(const struct iwl_priv *priv,
181                              struct iwl_lq_sta *lq_sta, u32 rate_n_flags);
182 
183 
184 #ifdef CONFIG_MAC80211_DEBUGFS
185 static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
186                              u32 *rate_n_flags, int index);
187 #else
188 static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
189                              u32 *rate_n_flags, int index)
190 {}
191 #endif
192 
193 /*
194  * Expected throughput metrics for following rates:
195  * 1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
196  * "G" is the only table that supports CCK (the first 4 rates).
197  */
198 
199 static s32 expected_tpt_A[IWL_RATE_COUNT] = {
200         0, 0, 0, 0, 40, 57, 72, 98, 121, 154, 177, 186, 186
201 };
202 
203 static s32 expected_tpt_G[IWL_RATE_COUNT] = {
204         7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 186
205 };
206 
207 static s32 expected_tpt_siso20MHz[IWL_RATE_COUNT] = {
208         0, 0, 0, 0, 42, 42, 76, 102, 124, 159, 183, 193, 202
209 };
210 
211 static s32 expected_tpt_siso20MHzSGI[IWL_RATE_COUNT] = {
212         0, 0, 0, 0, 46, 46, 82, 110, 132, 168, 192, 202, 211
213 };
214 
215 static s32 expected_tpt_mimo2_20MHz[IWL_RATE_COUNT] = {
216         0, 0, 0, 0, 74, 74, 123, 155, 179, 214, 236, 244, 251
217 };
218 
219 static s32 expected_tpt_mimo2_20MHzSGI[IWL_RATE_COUNT] = {
220         0, 0, 0, 0, 81, 81, 131, 164, 188, 222, 243, 251, 257
221 };
222 
223 static s32 expected_tpt_siso40MHz[IWL_RATE_COUNT] = {
224         0, 0, 0, 0, 77, 77, 127, 160, 184, 220, 242, 250, 257
225 };
226 
227 static s32 expected_tpt_siso40MHzSGI[IWL_RATE_COUNT] = {
228         0, 0, 0, 0, 83, 83, 135, 169, 193, 229, 250, 257, 264
229 };
230 
231 static s32 expected_tpt_mimo2_40MHz[IWL_RATE_COUNT] = {
232         0, 0, 0, 0, 123, 123, 182, 214, 235, 264, 279, 285, 289
233 };
234 
235 static s32 expected_tpt_mimo2_40MHzSGI[IWL_RATE_COUNT] = {
236         0, 0, 0, 0, 131, 131, 191, 222, 242, 270, 284, 289, 293
237 };
238 
239 /* Expected throughput metric MIMO3 */
240 static s32 expected_tpt_mimo3_20MHz[IWL_RATE_COUNT] = {
241         0, 0, 0, 0, 99, 99, 153, 186, 208, 239, 256, 263, 268
242 };
243 
244 static s32 expected_tpt_mimo3_20MHzSGI[IWL_RATE_COUNT] = {
245         0, 0, 0, 0, 106, 106, 162, 194, 215, 246, 262, 268, 273
246 };
247 
248 static s32 expected_tpt_mimo3_40MHz[IWL_RATE_COUNT] = {
249         0, 0, 0, 0, 152, 152, 211, 239, 255, 279, 290, 294, 297
250 };
251 
252 static s32 expected_tpt_mimo3_40MHzSGI[IWL_RATE_COUNT] = {
253         0, 0, 0, 0, 160, 160, 219, 245, 261, 284, 294, 297, 300
254 };
255 
256 /* mbps, mcs */
257 const static struct iwl_rate_mcs_info iwl_rate_mcs[IWL_RATE_COUNT] = {
258   {"1", ""},
259   {"2", ""},
260   {"5.5", ""},
261   {"11", ""},
262   {"6", "BPSK 1/2"},
263   {"9", "BPSK 1/2"},
264   {"12", "QPSK 1/2"},
265   {"18", "QPSK 3/4"},
266   {"24", "16QAM 1/2"},
267   {"36", "16QAM 3/4"},
268   {"48", "64QAM 2/3"},
269   {"54", "64QAM 3/4"},
270   {"60", "64QAM 5/6"}
271 };
272 
273 #define MCS_INDEX_PER_STREAM    (8)
274 
275 static inline u8 rs_extract_rate(u32 rate_n_flags)
276 {
277         return (u8)(rate_n_flags & 0xFF);
278 }
279 
280 static void rs_rate_scale_clear_window(struct iwl_rate_scale_data *window)
281 {
282         window->data = 0;
283         window->success_counter = 0;
284         window->success_ratio = IWL_INVALID_VALUE;
285         window->counter = 0;
286         window->average_tpt = IWL_INVALID_VALUE;
287         window->stamp = 0;
288 }
289 
290 static inline u8 rs_is_valid_ant(u8 valid_antenna, u8 ant_type)
291 {
292         return (ant_type & valid_antenna) == ant_type;
293 }
294 
295 /*
296  *      removes the old data from the statistics. All data that is older than
297  *      TID_MAX_TIME_DIFF, will be deleted.
298  */
299 static void rs_tl_rm_old_stats(struct iwl_traffic_load *tl, u32 curr_time)
300 {
301         /* The oldest age we want to keep */
302         u32 oldest_time = curr_time - TID_MAX_TIME_DIFF;
303 
304         while (tl->queue_count &&
305                (tl->time_stamp < oldest_time)) {
306                 tl->total -= tl->packet_count[tl->head];
307                 tl->packet_count[tl->head] = 0;
308                 tl->time_stamp += TID_QUEUE_CELL_SPACING;
309                 tl->queue_count--;
310                 tl->head++;
311                 if (tl->head >= TID_QUEUE_MAX_SIZE)
312                         tl->head = 0;
313         }
314 }
315 
316 /*
317  *      increment traffic load value for tid and also remove
318  *      any old values if passed the certain time period
319  */
320 static u8 rs_tl_add_packet(struct iwl_lq_sta *lq_data,
321                            struct ieee80211_hdr *hdr)
322 {
323         u32 curr_time = jiffies_to_msecs(jiffies);
324         u32 time_diff;
325         s32 index;
326         struct iwl_traffic_load *tl = NULL;
327         u8 tid;
328 
329         if (ieee80211_is_data_qos(hdr->frame_control)) {
330                 u8 *qc = ieee80211_get_qos_ctl(hdr);
331                 tid = qc[0] & 0xf;
332         } else
333                 return MAX_TID_COUNT;
334 
335         tl = &lq_data->load[tid];
336 
337         curr_time -= curr_time % TID_ROUND_VALUE;
338 
339         /* Happens only for the first packet. Initialize the data */
340         if (!(tl->queue_count)) {
341                 tl->total = 1;
342                 tl->time_stamp = curr_time;
343                 tl->queue_count = 1;
344                 tl->head = 0;
345                 tl->packet_count[0] = 1;
346                 return MAX_TID_COUNT;
347         }
348 
349         time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
350         index = time_diff / TID_QUEUE_CELL_SPACING;
351 
352         /* The history is too long: remove data that is older than */
353         /* TID_MAX_TIME_DIFF */
354         if (index >= TID_QUEUE_MAX_SIZE)
355                 rs_tl_rm_old_stats(tl, curr_time);
356 
357         index = (tl->head + index) % TID_QUEUE_MAX_SIZE;
358         tl->packet_count[index] = tl->packet_count[index] + 1;
359         tl->total = tl->total + 1;
360 
361         if ((index + 1) > tl->queue_count)
362                 tl->queue_count = index + 1;
363 
364         return tid;
365 }
366 
367 /*
368         get the traffic load value for tid
369 */
370 static u32 rs_tl_get_load(struct iwl_lq_sta *lq_data, u8 tid)
371 {
372         u32 curr_time = jiffies_to_msecs(jiffies);
373         u32 time_diff;
374         s32 index;
375         struct iwl_traffic_load *tl = NULL;
376 
377         if (tid >= TID_MAX_LOAD_COUNT)
378                 return 0;
379 
380         tl = &(lq_data->load[tid]);
381 
382         curr_time -= curr_time % TID_ROUND_VALUE;
383 
384         if (!(tl->queue_count))
385                 return 0;
386 
387         time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
388         index = time_diff / TID_QUEUE_CELL_SPACING;
389 
390         /* The history is too long: remove data that is older than */
391         /* TID_MAX_TIME_DIFF */
392         if (index >= TID_QUEUE_MAX_SIZE)
393                 rs_tl_rm_old_stats(tl, curr_time);
394 
395         return tl->total;
396 }
397 
398 static void rs_tl_turn_on_agg_for_tid(struct iwl_priv *priv,
399                                       struct iwl_lq_sta *lq_data, u8 tid,
400                                       struct ieee80211_sta *sta)
401 {
402         if (rs_tl_get_load(lq_data, tid) > IWL_AGG_LOAD_THRESHOLD) {
403                 IWL_DEBUG_HT(priv, "Starting Tx agg: STA: %pM tid: %d\n",
404                                 sta->addr, tid);
405                 ieee80211_start_tx_ba_session(priv->hw, sta->addr, tid);
406         }
407 }
408 
409 static void rs_tl_turn_on_agg(struct iwl_priv *priv, u8 tid,
410                               struct iwl_lq_sta *lq_data,
411                               struct ieee80211_sta *sta)
412 {
413         if ((tid < TID_MAX_LOAD_COUNT))
414                 rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
415         else if (tid == IWL_AGG_ALL_TID)
416                 for (tid = 0; tid < TID_MAX_LOAD_COUNT; tid++)
417                         rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
418         if (priv->cfg->use_rts_for_ht) {
419                 /*
420                  * switch to RTS/CTS if it is the prefer protection method
421                  * for HT traffic
422                  */
423                 IWL_DEBUG_HT(priv, "use RTS/CTS protection for HT\n");
424                 priv->staging_rxon.flags &= ~RXON_FLG_SELF_CTS_EN;
425                 iwlcore_commit_rxon(priv);
426         }
427 }
428 
429 static inline int get_num_of_ant_from_rate(u32 rate_n_flags)
430 {
431         return !!(rate_n_flags & RATE_MCS_ANT_A_MSK) +
432                !!(rate_n_flags & RATE_MCS_ANT_B_MSK) +
433                !!(rate_n_flags & RATE_MCS_ANT_C_MSK);
434 }
435 
436 /**
437  * rs_collect_tx_data - Update the success/failure sliding window
438  *
439  * We keep a sliding window of the last 62 packets transmitted
440  * at this rate.  window->data contains the bitmask of successful
441  * packets.
442  */
443 static int rs_collect_tx_data(struct iwl_rate_scale_data *windows,
444                               int scale_index, s32 tpt, int retries,
445                               int successes)
446 {
447         struct iwl_rate_scale_data *window = NULL;
448         static const u64 mask = (((u64)1) << (IWL_RATE_MAX_WINDOW - 1));
449         s32 fail_count;
450 
451         if (scale_index < 0 || scale_index >= IWL_RATE_COUNT)
452                 return -EINVAL;
453 
454         /* Select data for current tx bit rate */
455         window = &(windows[scale_index]);
456 
457         /*
458          * Keep track of only the latest 62 tx frame attempts in this rate's
459          * history window; anything older isn't really relevant any more.
460          * If we have filled up the sliding window, drop the oldest attempt;
461          * if the oldest attempt (highest bit in bitmap) shows "success",
462          * subtract "1" from the success counter (this is the main reason
463          * we keep these bitmaps!).
464          */
465         while (retries > 0) {
466                 if (window->counter >= IWL_RATE_MAX_WINDOW) {
467 
468                         /* remove earliest */
469                         window->counter = IWL_RATE_MAX_WINDOW - 1;
470 
471                         if (window->data & mask) {
472                                 window->data &= ~mask;
473                                 window->success_counter--;
474                         }
475                 }
476 
477                 /* Increment frames-attempted counter */
478                 window->counter++;
479 
480                 /* Shift bitmap by one frame (throw away oldest history),
481                  * OR in "1", and increment "success" if this
482                  * frame was successful. */
483                 window->data <<= 1;
484                 if (successes > 0) {
485                         window->success_counter++;
486                         window->data |= 0x1;
487                         successes--;
488                 }
489 
490                 retries--;
491         }
492 
493         /* Calculate current success ratio, avoid divide-by-0! */
494         if (window->counter > 0)
495                 window->success_ratio = 128 * (100 * window->success_counter)
496                                         / window->counter;
497         else
498                 window->success_ratio = IWL_INVALID_VALUE;
499 
500         fail_count = window->counter - window->success_counter;
501 
502         /* Calculate average throughput, if we have enough history. */
503         if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
504             (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
505                 window->average_tpt = (window->success_ratio * tpt + 64) / 128;
506         else
507                 window->average_tpt = IWL_INVALID_VALUE;
508 
509         /* Tag this window as having been updated */
510         window->stamp = jiffies;
511 
512         return 0;
513 }
514 
515 /*
516  * Fill uCode API rate_n_flags field, based on "search" or "active" table.
517  */
518 /* FIXME:RS:remove this function and put the flags statically in the table */
519 static u32 rate_n_flags_from_tbl(struct iwl_priv *priv,
520                                  struct iwl_scale_tbl_info *tbl,
521                                  int index, u8 use_green)
522 {
523         u32 rate_n_flags = 0;
524 
525         if (is_legacy(tbl->lq_type)) {
526                 rate_n_flags = iwl_rates[index].plcp;
527                 if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
528                         rate_n_flags |= RATE_MCS_CCK_MSK;
529 
530         } else if (is_Ht(tbl->lq_type)) {
531                 if (index > IWL_LAST_OFDM_RATE) {
532                         IWL_ERR(priv, "Invalid HT rate index %d\n", index);
533                         index = IWL_LAST_OFDM_RATE;
534                 }
535                 rate_n_flags = RATE_MCS_HT_MSK;
536 
537                 if (is_siso(tbl->lq_type))
538                         rate_n_flags |= iwl_rates[index].plcp_siso;
539                 else if (is_mimo2(tbl->lq_type))
540                         rate_n_flags |= iwl_rates[index].plcp_mimo2;
541                 else
542                         rate_n_flags |= iwl_rates[index].plcp_mimo3;
543         } else {
544                 IWL_ERR(priv, "Invalid tbl->lq_type %d\n", tbl->lq_type);
545         }
546 
547         rate_n_flags |= ((tbl->ant_type << RATE_MCS_ANT_POS) &
548                                                      RATE_MCS_ANT_ABC_MSK);
549 
550         if (is_Ht(tbl->lq_type)) {
551                 if (tbl->is_fat) {
552                         if (tbl->is_dup)
553                                 rate_n_flags |= RATE_MCS_DUP_MSK;
554                         else
555                                 rate_n_flags |= RATE_MCS_FAT_MSK;
556                 }
557                 if (tbl->is_SGI)
558                         rate_n_flags |= RATE_MCS_SGI_MSK;
559 
560                 if (use_green) {
561                         rate_n_flags |= RATE_MCS_GF_MSK;
562                         if (is_siso(tbl->lq_type) && tbl->is_SGI) {
563                                 rate_n_flags &= ~RATE_MCS_SGI_MSK;
564                                 IWL_ERR(priv, "GF was set with SGI:SISO\n");
565                         }
566                 }
567         }
568         return rate_n_flags;
569 }
570 
571 /*
572  * Interpret uCode API's rate_n_flags format,
573  * fill "search" or "active" tx mode table.
574  */
575 static int rs_get_tbl_info_from_mcs(const u32 rate_n_flags,
576                                     enum ieee80211_band band,
577                                     struct iwl_scale_tbl_info *tbl,
578                                     int *rate_idx)
579 {
580         u32 ant_msk = (rate_n_flags & RATE_MCS_ANT_ABC_MSK);
581         u8 num_of_ant = get_num_of_ant_from_rate(rate_n_flags);
582         u8 mcs;
583 
584         *rate_idx = iwl_hwrate_to_plcp_idx(rate_n_flags);
585 
586         if (*rate_idx  == IWL_RATE_INVALID) {
587                 *rate_idx = -1;
588                 return -EINVAL;
589         }
590         tbl->is_SGI = 0;        /* default legacy setup */
591         tbl->is_fat = 0;
592         tbl->is_dup = 0;
593         tbl->ant_type = (ant_msk >> RATE_MCS_ANT_POS);
594         tbl->lq_type = LQ_NONE;
595         tbl->max_search = IWL_MAX_SEARCH;
596 
597         /* legacy rate format */
598         if (!(rate_n_flags & RATE_MCS_HT_MSK)) {
599                 if (num_of_ant == 1) {
600                         if (band == IEEE80211_BAND_5GHZ)
601                                 tbl->lq_type = LQ_A;
602                         else
603                                 tbl->lq_type = LQ_G;
604                 }
605         /* HT rate format */
606         } else {
607                 if (rate_n_flags & RATE_MCS_SGI_MSK)
608                         tbl->is_SGI = 1;
609 
610                 if ((rate_n_flags & RATE_MCS_FAT_MSK) ||
611                     (rate_n_flags & RATE_MCS_DUP_MSK))
612                         tbl->is_fat = 1;
613 
614                 if (rate_n_flags & RATE_MCS_DUP_MSK)
615                         tbl->is_dup = 1;
616 
617                 mcs = rs_extract_rate(rate_n_flags);
618 
619                 /* SISO */
620                 if (mcs <= IWL_RATE_SISO_60M_PLCP) {
621                         if (num_of_ant == 1)
622                                 tbl->lq_type = LQ_SISO; /*else NONE*/
623                 /* MIMO2 */
624                 } else if (mcs <= IWL_RATE_MIMO2_60M_PLCP) {
625                         if (num_of_ant == 2)
626                                 tbl->lq_type = LQ_MIMO2;
627                 /* MIMO3 */
628                 } else {
629                         if (num_of_ant == 3) {
630                                 tbl->max_search = IWL_MAX_11N_MIMO3_SEARCH;
631                                 tbl->lq_type = LQ_MIMO3;
632                         }
633                 }
634         }
635         return 0;
636 }
637 
638 /* switch to another antenna/antennas and return 1 */
639 /* if no other valid antenna found, return 0 */
640 static int rs_toggle_antenna(u32 valid_ant, u32 *rate_n_flags,
641                              struct iwl_scale_tbl_info *tbl)
642 {
643         u8 new_ant_type;
644 
645         if (!tbl->ant_type || tbl->ant_type > ANT_ABC)
646                 return 0;
647 
648         if (!rs_is_valid_ant(valid_ant, tbl->ant_type))
649                 return 0;
650 
651         new_ant_type = ant_toggle_lookup[tbl->ant_type];
652 
653         while ((new_ant_type != tbl->ant_type) &&
654                !rs_is_valid_ant(valid_ant, new_ant_type))
655                 new_ant_type = ant_toggle_lookup[new_ant_type];
656 
657         if (new_ant_type == tbl->ant_type)
658                 return 0;
659 
660         tbl->ant_type = new_ant_type;
661         *rate_n_flags &= ~RATE_MCS_ANT_ABC_MSK;
662         *rate_n_flags |= new_ant_type << RATE_MCS_ANT_POS;
663         return 1;
664 }
665 
666 /* in 4965 we don't use greenfield at all */
667 static inline u8 rs_use_green(struct iwl_priv *priv,
668                               struct ieee80211_conf *conf)
669 {
670         u8 is_green;
671 
672         if ((priv->hw_rev & CSR_HW_REV_TYPE_MSK) == CSR_HW_REV_TYPE_4965)
673                 is_green = 0;
674         else
675                 is_green = (conf_is_ht(conf) &&
676                            priv->current_ht_config.is_green_field &&
677                            !priv->current_ht_config.non_GF_STA_present);
678         return is_green;
679 }
680 
681 /**
682  * rs_get_supported_rates - get the available rates
683  *
684  * if management frame or broadcast frame only return
685  * basic available rates.
686  *
687  */
688 static u16 rs_get_supported_rates(struct iwl_lq_sta *lq_sta,
689                                   struct ieee80211_hdr *hdr,
690                                   enum iwl_table_type rate_type)
691 {
692         if (hdr && is_multicast_ether_addr(hdr->addr1) &&
693             lq_sta->active_rate_basic)
694                 return lq_sta->active_rate_basic;
695 
696         if (is_legacy(rate_type)) {
697                 return lq_sta->active_legacy_rate;
698         } else {
699                 if (is_siso(rate_type))
700                         return lq_sta->active_siso_rate;
701                 else if (is_mimo2(rate_type))
702                         return lq_sta->active_mimo2_rate;
703                 else
704                         return lq_sta->active_mimo3_rate;
705         }
706 }
707 
708 static u16 rs_get_adjacent_rate(struct iwl_priv *priv, u8 index, u16 rate_mask,
709                                 int rate_type)
710 {
711         u8 high = IWL_RATE_INVALID;
712         u8 low = IWL_RATE_INVALID;
713 
714         /* 802.11A or ht walks to the next literal adjacent rate in
715          * the rate table */
716         if (is_a_band(rate_type) || !is_legacy(rate_type)) {
717                 int i;
718                 u32 mask;
719 
720                 /* Find the previous rate that is in the rate mask */
721                 i = index - 1;
722                 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
723                         if (rate_mask & mask) {
724                                 low = i;
725                                 break;
726                         }
727                 }
728 
729                 /* Find the next rate that is in the rate mask */
730                 i = index + 1;
731                 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
732                         if (rate_mask & mask) {
733                                 high = i;
734                                 break;
735                         }
736                 }
737 
738                 return (high << 8) | low;
739         }
740 
741         low = index;
742         while (low != IWL_RATE_INVALID) {
743                 low = iwl_rates[low].prev_rs;
744                 if (low == IWL_RATE_INVALID)
745                         break;
746                 if (rate_mask & (1 << low))
747                         break;
748                 IWL_DEBUG_RATE(priv, "Skipping masked lower rate: %d\n", low);
749         }
750 
751         high = index;
752         while (high != IWL_RATE_INVALID) {
753                 high = iwl_rates[high].next_rs;
754                 if (high == IWL_RATE_INVALID)
755                         break;
756                 if (rate_mask & (1 << high))
757                         break;
758                 IWL_DEBUG_RATE(priv, "Skipping masked higher rate: %d\n", high);
759         }
760 
761         return (high << 8) | low;
762 }
763 
764 static u32 rs_get_lower_rate(struct iwl_lq_sta *lq_sta,
765                              struct iwl_scale_tbl_info *tbl,
766                              u8 scale_index, u8 ht_possible)
767 {
768         s32 low;
769         u16 rate_mask;
770         u16 high_low;
771         u8 switch_to_legacy = 0;
772         u8 is_green = lq_sta->is_green;
773 
774         /* check if we need to switch from HT to legacy rates.
775          * assumption is that mandatory rates (1Mbps or 6Mbps)
776          * are always supported (spec demand) */
777         if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_index)) {
778                 switch_to_legacy = 1;
779                 scale_index = rs_ht_to_legacy[scale_index];
780                 if (lq_sta->band == IEEE80211_BAND_5GHZ)
781                         tbl->lq_type = LQ_A;
782                 else
783                         tbl->lq_type = LQ_G;
784 
785                 if (num_of_ant(tbl->ant_type) > 1)
786                         tbl->ant_type = ANT_A;/*FIXME:RS*/
787 
788                 tbl->is_fat = 0;
789                 tbl->is_SGI = 0;
790                 tbl->max_search = IWL_MAX_SEARCH;
791         }
792 
793         rate_mask = rs_get_supported_rates(lq_sta, NULL, tbl->lq_type);
794 
795         /* Mask with station rate restriction */
796         if (is_legacy(tbl->lq_type)) {
797                 /* supp_rates has no CCK bits in A mode */
798                 if (lq_sta->band == IEEE80211_BAND_5GHZ)
799                         rate_mask  = (u16)(rate_mask &
800                            (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
801                 else
802                         rate_mask = (u16)(rate_mask & lq_sta->supp_rates);
803         }
804 
805         /* If we switched from HT to legacy, check current rate */
806         if (switch_to_legacy && (rate_mask & (1 << scale_index))) {
807                 low = scale_index;
808                 goto out;
809         }
810 
811         high_low = rs_get_adjacent_rate(lq_sta->drv, scale_index, rate_mask,
812                                         tbl->lq_type);
813         low = high_low & 0xff;
814 
815         if (low == IWL_RATE_INVALID)
816                 low = scale_index;
817 
818 out:
819         return rate_n_flags_from_tbl(lq_sta->drv, tbl, low, is_green);
820 }
821 
822 /*
823  * mac80211 sends us Tx status
824  */
825 static void rs_tx_status(void *priv_r, struct ieee80211_supported_band *sband,
826                          struct ieee80211_sta *sta, void *priv_sta,
827                          struct sk_buff *skb)
828 {
829         int status;
830         u8 retries;
831         int rs_index, index = 0;
832         struct iwl_lq_sta *lq_sta = priv_sta;
833         struct iwl_link_quality_cmd *table;
834         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
835         struct iwl_priv *priv = (struct iwl_priv *)priv_r;
836         struct ieee80211_hw *hw = priv->hw;
837         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
838         struct iwl_rate_scale_data *window = NULL;
839         struct iwl_rate_scale_data *search_win = NULL;
840         u32 tx_rate;
841         struct iwl_scale_tbl_info tbl_type;
842         struct iwl_scale_tbl_info *curr_tbl, *search_tbl;
843         u8 active_index = 0;
844         s32 tpt = 0;
845 
846         IWL_DEBUG_RATE_LIMIT(priv, "get frame ack response, update rate scale window\n");
847 
848         if (!ieee80211_is_data(hdr->frame_control) ||
849             info->flags & IEEE80211_TX_CTL_NO_ACK)
850                 return;
851 
852         /* This packet was aggregated but doesn't carry rate scale info */
853         if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
854             !(info->flags & IEEE80211_TX_STAT_AMPDU))
855                 return;
856 
857         if (info->flags & IEEE80211_TX_STAT_AMPDU)
858                 retries = 0;
859         else
860                 retries = info->status.rates[0].count - 1;
861 
862         if (retries > 15)
863                 retries = 15;
864 
865         if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) &&
866             !lq_sta->ibss_sta_added)
867                 goto out;
868 
869         table = &lq_sta->lq;
870         active_index = lq_sta->active_tbl;
871 
872         curr_tbl = &(lq_sta->lq_info[active_index]);
873         search_tbl = &(lq_sta->lq_info[(1 - active_index)]);
874         window = (struct iwl_rate_scale_data *)&(curr_tbl->win[0]);
875         search_win = (struct iwl_rate_scale_data *)&(search_tbl->win[0]);
876 
877         /*
878          * Ignore this Tx frame response if its initial rate doesn't match
879          * that of latest Link Quality command.  There may be stragglers
880          * from a previous Link Quality command, but we're no longer interested
881          * in those; they're either from the "active" mode while we're trying
882          * to check "search" mode, or a prior "search" mode after we've moved
883          * to a new "search" mode (which might become the new "active" mode).
884          */
885         tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
886         rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
887         if (priv->band == IEEE80211_BAND_5GHZ)
888                 rs_index -= IWL_FIRST_OFDM_RATE;
889 
890         if ((info->status.rates[0].idx < 0) ||
891             (tbl_type.is_SGI != !!(info->status.rates[0].flags & IEEE80211_TX_RC_SHORT_GI)) ||
892             (tbl_type.is_fat != !!(info->status.rates[0].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)) ||
893             (tbl_type.is_dup != !!(info->status.rates[0].flags & IEEE80211_TX_RC_DUP_DATA)) ||
894             (tbl_type.ant_type != info->antenna_sel_tx) ||
895             (!!(tx_rate & RATE_MCS_HT_MSK) != !!(info->status.rates[0].flags & IEEE80211_TX_RC_MCS)) ||
896             (!!(tx_rate & RATE_MCS_GF_MSK) != !!(info->status.rates[0].flags & IEEE80211_TX_RC_GREEN_FIELD)) ||
897             (hw->wiphy->bands[priv->band]->bitrates[rs_index].bitrate !=
898              hw->wiphy->bands[info->band]->bitrates[info->status.rates[0].idx].bitrate)) {
899                 IWL_DEBUG_RATE(priv, "initial rate does not match 0x%x\n", tx_rate);
900                 /* the last LQ command could failed so the LQ in ucode not
901                  * the same in driver sync up
902                  */
903                 lq_sta->missed_rate_counter++;
904                 if (lq_sta->missed_rate_counter > IWL_MISSED_RATE_MAX) {
905                         lq_sta->missed_rate_counter = 0;
906                         iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
907                 }
908                 goto out;
909         }
910 
911         lq_sta->missed_rate_counter = 0;
912         /* Update frame history window with "failure" for each Tx retry. */
913         while (retries) {
914                 /* Look up the rate and other info used for each tx attempt.
915                  * Each tx attempt steps one entry deeper in the rate table. */
916                 tx_rate = le32_to_cpu(table->rs_table[index].rate_n_flags);
917                 rs_get_tbl_info_from_mcs(tx_rate, priv->band,
918                                           &tbl_type, &rs_index);
919 
920                 /* If type matches "search" table,
921                  * add failure to "search" history */
922                 if ((tbl_type.lq_type == search_tbl->lq_type) &&
923                     (tbl_type.ant_type == search_tbl->ant_type) &&
924                     (tbl_type.is_SGI == search_tbl->is_SGI)) {
925                         if (search_tbl->expected_tpt)
926                                 tpt = search_tbl->expected_tpt[rs_index];
927                         else
928                                 tpt = 0;
929                         rs_collect_tx_data(search_win, rs_index, tpt, 1, 0);
930 
931                 /* Else if type matches "current/active" table,
932                  * add failure to "current/active" history */
933                 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
934                            (tbl_type.ant_type == curr_tbl->ant_type) &&
935                            (tbl_type.is_SGI == curr_tbl->is_SGI)) {
936                         if (curr_tbl->expected_tpt)
937                                 tpt = curr_tbl->expected_tpt[rs_index];
938                         else
939                                 tpt = 0;
940                         rs_collect_tx_data(window, rs_index, tpt, 1, 0);
941                 }
942 
943                 /* If not searching for a new mode, increment failed counter
944                  * ... this helps determine when to start searching again */
945                 if (lq_sta->stay_in_tbl)
946                         lq_sta->total_failed++;
947                 --retries;
948                 index++;
949 
950         }
951 
952         /*
953          * Find (by rate) the history window to update with final Tx attempt;
954          * if Tx was successful first try, use original rate,
955          * else look up the rate that was, finally, successful.
956          */
957         tx_rate = le32_to_cpu(table->rs_table[index].rate_n_flags);
958         lq_sta->last_rate_n_flags = tx_rate;
959         rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
960 
961         /* Update frame history window with "success" if Tx got ACKed ... */
962         status = !!(info->flags & IEEE80211_TX_STAT_ACK);
963 
964         /* If type matches "search" table,
965          * add final tx status to "search" history */
966         if ((tbl_type.lq_type == search_tbl->lq_type) &&
967             (tbl_type.ant_type == search_tbl->ant_type) &&
968             (tbl_type.is_SGI == search_tbl->is_SGI)) {
969                 if (search_tbl->expected_tpt)
970                         tpt = search_tbl->expected_tpt[rs_index];
971                 else
972                         tpt = 0;
973                 if (info->flags & IEEE80211_TX_STAT_AMPDU)
974                         rs_collect_tx_data(search_win, rs_index, tpt,
975                                            info->status.ampdu_ack_len,
976                                            info->status.ampdu_ack_map);
977                 else
978                         rs_collect_tx_data(search_win, rs_index, tpt,
979                                            1, status);
980         /* Else if type matches "current/active" table,
981          * add final tx status to "current/active" history */
982         } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
983                    (tbl_type.ant_type == curr_tbl->ant_type) &&
984                    (tbl_type.is_SGI == curr_tbl->is_SGI)) {
985                 if (curr_tbl->expected_tpt)
986                         tpt = curr_tbl->expected_tpt[rs_index];
987                 else
988                         tpt = 0;
989                 if (info->flags & IEEE80211_TX_STAT_AMPDU)
990                         rs_collect_tx_data(window, rs_index, tpt,
991                                            info->status.ampdu_ack_len,
992                                            info->status.ampdu_ack_map);
993                 else
994                         rs_collect_tx_data(window, rs_index, tpt,
995                                            1, status);
996         }
997 
998         /* If not searching for new mode, increment success/failed counter
999          * ... these help determine when to start searching again */
1000         if (lq_sta->stay_in_tbl) {
1001                 if (info->flags & IEEE80211_TX_STAT_AMPDU) {
1002                         lq_sta->total_success += info->status.ampdu_ack_map;
1003                         lq_sta->total_failed +=
1004                              (info->status.ampdu_ack_len - info->status.ampdu_ack_map);
1005                 } else {
1006                         if (status)
1007                                 lq_sta->total_success++;
1008                         else
1009                                 lq_sta->total_failed++;
1010                 }
1011         }
1012 
1013         /* See if there's a better rate or modulation mode to try. */
1014         if (sta && sta->supp_rates[sband->band])
1015                 rs_rate_scale_perform(priv, skb, sta, lq_sta);
1016 out:
1017         return;
1018 }
1019 
1020 /*
1021  * Begin a period of staying with a selected modulation mode.
1022  * Set "stay_in_tbl" flag to prevent any mode switches.
1023  * Set frame tx success limits according to legacy vs. high-throughput,
1024  * and reset overall (spanning all rates) tx success history statistics.
1025  * These control how long we stay using same modulation mode before
1026  * searching for a new mode.
1027  */
1028 static void rs_set_stay_in_table(struct iwl_priv *priv, u8 is_legacy,
1029                                  struct iwl_lq_sta *lq_sta)
1030 {
1031         IWL_DEBUG_RATE(priv, "we are staying in the same table\n");
1032         lq_sta->stay_in_tbl = 1;        /* only place this gets set */
1033         if (is_legacy) {
1034                 lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT;
1035                 lq_sta->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
1036                 lq_sta->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
1037         } else {
1038                 lq_sta->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
1039                 lq_sta->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
1040                 lq_sta->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
1041         }
1042         lq_sta->table_count = 0;
1043         lq_sta->total_failed = 0;
1044         lq_sta->total_success = 0;
1045         lq_sta->flush_timer = jiffies;
1046         lq_sta->action_counter = 0;
1047 }
1048 
1049 /*
1050  * Find correct throughput table for given mode of modulation
1051  */
1052 static void rs_set_expected_tpt_table(struct iwl_lq_sta *lq_sta,
1053                                       struct iwl_scale_tbl_info *tbl)
1054 {
1055         if (is_legacy(tbl->lq_type)) {
1056                 if (!is_a_band(tbl->lq_type))
1057                         tbl->expected_tpt = expected_tpt_G;
1058                 else
1059                         tbl->expected_tpt = expected_tpt_A;
1060         } else if (is_siso(tbl->lq_type)) {
1061                 if (tbl->is_fat && !lq_sta->is_dup)
1062                         if (tbl->is_SGI)
1063                                 tbl->expected_tpt = expected_tpt_siso40MHzSGI;
1064                         else
1065                                 tbl->expected_tpt = expected_tpt_siso40MHz;
1066                 else if (tbl->is_SGI)
1067                         tbl->expected_tpt = expected_tpt_siso20MHzSGI;
1068                 else
1069                         tbl->expected_tpt = expected_tpt_siso20MHz;
1070         } else if (is_mimo2(tbl->lq_type)) {
1071                 if (tbl->is_fat && !lq_sta->is_dup)
1072                         if (tbl->is_SGI)
1073                                 tbl->expected_tpt = expected_tpt_mimo2_40MHzSGI;
1074                         else
1075                                 tbl->expected_tpt = expected_tpt_mimo2_40MHz;
1076                 else if (tbl->is_SGI)
1077                         tbl->expected_tpt = expected_tpt_mimo2_20MHzSGI;
1078                 else
1079                         tbl->expected_tpt = expected_tpt_mimo2_20MHz;
1080         } else if (is_mimo3(tbl->lq_type)) {
1081                 if (tbl->is_fat && !lq_sta->is_dup)
1082                         if (tbl->is_SGI)
1083                                 tbl->expected_tpt = expected_tpt_mimo3_40MHzSGI;
1084                         else
1085                                 tbl->expected_tpt = expected_tpt_mimo3_40MHz;
1086                 else if (tbl->is_SGI)
1087                         tbl->expected_tpt = expected_tpt_mimo3_20MHzSGI;
1088                 else
1089                         tbl->expected_tpt = expected_tpt_mimo3_20MHz;
1090         } else
1091                 tbl->expected_tpt = expected_tpt_G;
1092 }
1093 
1094 /*
1095  * Find starting rate for new "search" high-throughput mode of modulation.
1096  * Goal is to find lowest expected rate (under perfect conditions) that is
1097  * above the current measured throughput of "active" mode, to give new mode
1098  * a fair chance to prove itself without too many challenges.
1099  *
1100  * This gets called when transitioning to more aggressive modulation
1101  * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
1102  * (i.e. MIMO to SISO).  When moving to MIMO, bit rate will typically need
1103  * to decrease to match "active" throughput.  When moving from MIMO to SISO,
1104  * bit rate will typically need to increase, but not if performance was bad.
1105  */
1106 static s32 rs_get_best_rate(struct iwl_priv *priv,
1107                             struct iwl_lq_sta *lq_sta,
1108                             struct iwl_scale_tbl_info *tbl,     /* "search" */
1109                             u16 rate_mask, s8 index)
1110 {
1111         /* "active" values */
1112         struct iwl_scale_tbl_info *active_tbl =
1113             &(lq_sta->lq_info[lq_sta->active_tbl]);
1114         s32 active_sr = active_tbl->win[index].success_ratio;
1115         s32 active_tpt = active_tbl->expected_tpt[index];
1116 
1117         /* expected "search" throughput */
1118         s32 *tpt_tbl = tbl->expected_tpt;
1119 
1120         s32 new_rate, high, low, start_hi;
1121         u16 high_low;
1122         s8 rate = index;
1123 
1124         new_rate = high = low = start_hi = IWL_RATE_INVALID;
1125 
1126         for (; ;) {
1127                 high_low = rs_get_adjacent_rate(priv, rate, rate_mask,
1128                                                 tbl->lq_type);
1129 
1130                 low = high_low & 0xff;
1131                 high = (high_low >> 8) & 0xff;
1132 
1133                 /*
1134                  * Lower the "search" bit rate, to give new "search" mode
1135                  * approximately the same throughput as "active" if:
1136                  *
1137                  * 1) "Active" mode has been working modestly well (but not
1138                  *    great), and expected "search" throughput (under perfect
1139                  *    conditions) at candidate rate is above the actual
1140                  *    measured "active" throughput (but less than expected
1141                  *    "active" throughput under perfect conditions).
1142                  * OR
1143                  * 2) "Active" mode has been working perfectly or very well
1144                  *    and expected "search" throughput (under perfect
1145                  *    conditions) at candidate rate is above expected
1146                  *    "active" throughput (under perfect conditions).
1147                  */
1148                 if ((((100 * tpt_tbl[rate]) > lq_sta->last_tpt) &&
1149                      ((active_sr > IWL_RATE_DECREASE_TH) &&
1150                       (active_sr <= IWL_RATE_HIGH_TH) &&
1151                       (tpt_tbl[rate] <= active_tpt))) ||
1152                     ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
1153                      (tpt_tbl[rate] > active_tpt))) {
1154 
1155                         /* (2nd or later pass)
1156                          * If we've already tried to raise the rate, and are
1157                          * now trying to lower it, use the higher rate. */
1158                         if (start_hi != IWL_RATE_INVALID) {
1159                                 new_rate = start_hi;
1160                                 break;
1161                         }
1162 
1163                         new_rate = rate;
1164 
1165                         /* Loop again with lower rate */
1166                         if (low != IWL_RATE_INVALID)
1167                                 rate = low;
1168 
1169                         /* Lower rate not available, use the original */
1170                         else
1171                                 break;
1172 
1173                 /* Else try to raise the "search" rate to match "active" */
1174                 } else {
1175                         /* (2nd or later pass)
1176                          * If we've already tried to lower the rate, and are
1177                          * now trying to raise it, use the lower rate. */
1178                         if (new_rate != IWL_RATE_INVALID)
1179                                 break;
1180 
1181                         /* Loop again with higher rate */
1182                         else if (high != IWL_RATE_INVALID) {
1183                                 start_hi = high;
1184                                 rate = high;
1185 
1186                         /* Higher rate not available, use the original */
1187                         } else {
1188                                 new_rate = rate;
1189                                 break;
1190                         }
1191                 }
1192         }
1193 
1194         return new_rate;
1195 }
1196 
1197 /*
1198  * Set up search table for MIMO2
1199  */
1200 static int rs_switch_to_mimo2(struct iwl_priv *priv,
1201                              struct iwl_lq_sta *lq_sta,
1202                              struct ieee80211_conf *conf,
1203                              struct ieee80211_sta *sta,
1204                              struct iwl_scale_tbl_info *tbl, int index)
1205 {
1206         u16 rate_mask;
1207         s32 rate;
1208         s8 is_green = lq_sta->is_green;
1209 
1210         if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
1211                 return -1;
1212 
1213         if (((sta->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 2)
1214                                                 == WLAN_HT_CAP_SM_PS_STATIC)
1215                 return -1;
1216 
1217         /* Need both Tx chains/antennas to support MIMO */
1218         if (priv->hw_params.tx_chains_num < 2)
1219                 return -1;
1220 
1221         IWL_DEBUG_RATE(priv, "LQ: try to switch to MIMO2\n");
1222 
1223         tbl->lq_type = LQ_MIMO2;
1224         tbl->is_dup = lq_sta->is_dup;
1225         tbl->action = 0;
1226         tbl->max_search = IWL_MAX_SEARCH;
1227         rate_mask = lq_sta->active_mimo2_rate;
1228 
1229         if (iwl_is_fat_tx_allowed(priv, &sta->ht_cap))
1230                 tbl->is_fat = 1;
1231         else
1232                 tbl->is_fat = 0;
1233 
1234         /* FIXME: - don't toggle SGI here
1235         if (tbl->is_fat) {
1236                 if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
1237                         tbl->is_SGI = 1;
1238                 else
1239                         tbl->is_SGI = 0;
1240         } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
1241                 tbl->is_SGI = 1;
1242         else
1243                 tbl->is_SGI = 0;
1244         */
1245 
1246         rs_set_expected_tpt_table(lq_sta, tbl);
1247 
1248         rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
1249 
1250         IWL_DEBUG_RATE(priv, "LQ: MIMO2 best rate %d mask %X\n", rate, rate_mask);
1251         if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1252                 IWL_DEBUG_RATE(priv, "Can't switch with index %d rate mask %x\n",
1253                                                 rate, rate_mask);
1254                 return -1;
1255         }
1256         tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
1257 
1258         IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
1259                      tbl->current_rate, is_green);
1260         return 0;
1261 }
1262 
1263 /*
1264  * Set up search table for MIMO3
1265  */
1266 static int rs_switch_to_mimo3(struct iwl_priv *priv,
1267                              struct iwl_lq_sta *lq_sta,
1268                              struct ieee80211_conf *conf,
1269                              struct ieee80211_sta *sta,
1270                              struct iwl_scale_tbl_info *tbl, int index)
1271 {
1272         u16 rate_mask;
1273         s32 rate;
1274         s8 is_green = lq_sta->is_green;
1275 
1276         if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
1277                 return -1;
1278 
1279         if (((sta->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 2)
1280                                                 == WLAN_HT_CAP_SM_PS_STATIC)
1281                 return -1;
1282 
1283         /* Need both Tx chains/antennas to support MIMO */
1284         if (priv->hw_params.tx_chains_num < 3)
1285                 return -1;
1286 
1287         IWL_DEBUG_RATE(priv, "LQ: try to switch to MIMO3\n");
1288 
1289         tbl->lq_type = LQ_MIMO3;
1290         tbl->is_dup = lq_sta->is_dup;
1291         tbl->action = 0;
1292         tbl->max_search = IWL_MAX_11N_MIMO3_SEARCH;
1293         rate_mask = lq_sta->active_mimo3_rate;
1294 
1295         if (iwl_is_fat_tx_allowed(priv, &sta->ht_cap))
1296                 tbl->is_fat = 1;
1297         else
1298                 tbl->is_fat = 0;
1299 
1300         /* FIXME: - don't toggle SGI here
1301         if (tbl->is_fat) {
1302                 if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
1303                         tbl->is_SGI = 1;
1304                 else
1305                         tbl->is_SGI = 0;
1306         } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
1307                 tbl->is_SGI = 1;
1308         else
1309                 tbl->is_SGI = 0;
1310         */
1311 
1312         rs_set_expected_tpt_table(lq_sta, tbl);
1313 
1314         rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
1315 
1316         IWL_DEBUG_RATE(priv, "LQ: MIMO3 best rate %d mask %X\n",
1317                 rate, rate_mask);
1318         if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1319                 IWL_DEBUG_RATE(priv, "Can't switch with index %d rate mask %x\n",
1320                                                 rate, rate_mask);
1321                 return -1;
1322         }
1323         tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
1324 
1325         IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
1326                      tbl->current_rate, is_green);
1327         return 0;
1328 }
1329 
1330 /*
1331  * Set up search table for SISO
1332  */
1333 static int rs_switch_to_siso(struct iwl_priv *priv,
1334                              struct iwl_lq_sta *lq_sta,
1335                              struct ieee80211_conf *conf,
1336                              struct ieee80211_sta *sta,
1337                              struct iwl_scale_tbl_info *tbl, int index)
1338 {
1339         u16 rate_mask;
1340         u8 is_green = lq_sta->is_green;
1341         s32 rate;
1342 
1343         if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
1344                 return -1;
1345 
1346         IWL_DEBUG_RATE(priv, "LQ: try to switch to SISO\n");
1347 
1348         tbl->is_dup = lq_sta->is_dup;
1349         tbl->lq_type = LQ_SISO;
1350         tbl->action = 0;
1351         tbl->max_search = IWL_MAX_SEARCH;
1352         rate_mask = lq_sta->active_siso_rate;
1353 
1354         if (iwl_is_fat_tx_allowed(priv, &sta->ht_cap))
1355                 tbl->is_fat = 1;
1356         else
1357                 tbl->is_fat = 0;
1358 
1359         /* FIXME: - don't toggle SGI here
1360         if (tbl->is_fat) {
1361                 if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
1362                         tbl->is_SGI = 1;
1363                 else
1364                         tbl->is_SGI = 0;
1365         } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
1366                 tbl->is_SGI = 1;
1367         else
1368                 tbl->is_SGI = 0;
1369         */
1370 
1371         if (is_green)
1372                 tbl->is_SGI = 0; /*11n spec: no SGI in SISO+Greenfield*/
1373 
1374         rs_set_expected_tpt_table(lq_sta, tbl);
1375         rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
1376 
1377         IWL_DEBUG_RATE(priv, "LQ: get best rate %d mask %X\n", rate, rate_mask);
1378         if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1379                 IWL_DEBUG_RATE(priv, "can not switch with index %d rate mask %x\n",
1380                              rate, rate_mask);
1381                 return -1;
1382         }
1383         tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
1384         IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
1385                      tbl->current_rate, is_green);
1386         return 0;
1387 }
1388 
1389 /*
1390  * Try to switch to new modulation mode from legacy
1391  */
1392 static int rs_move_legacy_other(struct iwl_priv *priv,
1393                                 struct iwl_lq_sta *lq_sta,
1394                                 struct ieee80211_conf *conf,
1395                                 struct ieee80211_sta *sta,
1396                                 int index)
1397 {
1398         struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1399         struct iwl_scale_tbl_info *search_tbl =
1400                                 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1401         struct iwl_rate_scale_data *window = &(tbl->win[index]);
1402         u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1403                   (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1404         u8 start_action = tbl->action;
1405         u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1406         u8 tx_chains_num = priv->hw_params.tx_chains_num;
1407         int ret = 0;
1408         u8 update_search_tbl_counter = 0;
1409 
1410         for (; ;) {
1411                 lq_sta->action_counter++;
1412                 switch (tbl->action) {
1413                 case IWL_LEGACY_SWITCH_ANTENNA1:
1414                 case IWL_LEGACY_SWITCH_ANTENNA2:
1415                         IWL_DEBUG_RATE(priv, "LQ: Legacy toggle Antenna\n");
1416 
1417                         if ((tbl->action == IWL_LEGACY_SWITCH_ANTENNA1 &&
1418                                                         tx_chains_num <= 1) ||
1419                             (tbl->action == IWL_LEGACY_SWITCH_ANTENNA2 &&
1420                                                         tx_chains_num <= 2))
1421                                 break;
1422 
1423                         /* Don't change antenna if success has been great */
1424                         if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1425                                 break;
1426 
1427                         /* Set up search table to try other antenna */
1428                         memcpy(search_tbl, tbl, sz);
1429 
1430                         if (rs_toggle_antenna(valid_tx_ant,
1431                                 &search_tbl->current_rate, search_tbl)) {
1432                                 update_search_tbl_counter = 1;
1433                                 rs_set_expected_tpt_table(lq_sta, search_tbl);
1434                                 goto out;
1435                         }
1436                         break;
1437                 case IWL_LEGACY_SWITCH_SISO:
1438                         IWL_DEBUG_RATE(priv, "LQ: Legacy switch to SISO\n");
1439 
1440                         /* Set up search table to try SISO */
1441                         memcpy(search_tbl, tbl, sz);
1442                         search_tbl->is_SGI = 0;
1443                         ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
1444                                                  search_tbl, index);
1445                         if (!ret) {
1446                                 lq_sta->action_counter = 0;
1447                                 goto out;
1448                         }
1449 
1450                         break;
1451                 case IWL_LEGACY_SWITCH_MIMO2_AB:
1452                 case IWL_LEGACY_SWITCH_MIMO2_AC:
1453                 case IWL_LEGACY_SWITCH_MIMO2_BC:
1454                         IWL_DEBUG_RATE(priv, "LQ: Legacy switch to MIMO2\n");
1455 
1456                         /* Set up search table to try MIMO */
1457                         memcpy(search_tbl, tbl, sz);
1458                         search_tbl->is_SGI = 0;
1459 
1460                         if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AB)
1461                                 search_tbl->ant_type = ANT_AB;
1462                         else if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AC)
1463                                 search_tbl->ant_type = ANT_AC;
1464                         else
1465                                 search_tbl->ant_type = ANT_BC;
1466 
1467                         if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1468                                 break;
1469 
1470                         ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
1471                                                  search_tbl, index);
1472                         if (!ret) {
1473                                 lq_sta->action_counter = 0;
1474                                 goto out;
1475                         }
1476                         break;
1477 
1478                 case IWL_LEGACY_SWITCH_MIMO3_ABC:
1479                         IWL_DEBUG_RATE(priv, "LQ: Legacy switch to MIMO3\n");
1480 
1481                         /* Set up search table to try MIMO3 */
1482                         memcpy(search_tbl, tbl, sz);
1483                         search_tbl->is_SGI = 0;
1484 
1485                         search_tbl->ant_type = ANT_ABC;
1486 
1487                         if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1488                                 break;
1489 
1490                         ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1491                                                  search_tbl, index);
1492                         if (!ret) {
1493                                 lq_sta->action_counter = 0;
1494                                 goto out;
1495                         }
1496                         break;
1497                 }
1498                 tbl->action++;
1499                 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
1500                         tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
1501 
1502                 if (tbl->action == start_action)
1503                         break;
1504 
1505         }
1506         search_tbl->lq_type = LQ_NONE;
1507         return 0;
1508 
1509 out:
1510         lq_sta->search_better_tbl = 1;
1511         tbl->action++;
1512         if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
1513                 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
1514         if (update_search_tbl_counter)
1515                 search_tbl->action = tbl->action;
1516         return 0;
1517 
1518 }
1519 
1520 /*
1521  * Try to switch to new modulation mode from SISO
1522  */
1523 static int rs_move_siso_to_other(struct iwl_priv *priv,
1524                                  struct iwl_lq_sta *lq_sta,
1525                                  struct ieee80211_conf *conf,
1526                                  struct ieee80211_sta *sta, int index)
1527 {
1528         u8 is_green = lq_sta->is_green;
1529         struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1530         struct iwl_scale_tbl_info *search_tbl =
1531                                 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1532         struct iwl_rate_scale_data *window = &(tbl->win[index]);
1533         u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1534                   (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1535         u8 start_action = tbl->action;
1536         u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1537         u8 tx_chains_num = priv->hw_params.tx_chains_num;
1538         u8 update_search_tbl_counter = 0;
1539         int ret;
1540 
1541         for (;;) {
1542                 lq_sta->action_counter++;
1543                 switch (tbl->action) {
1544                 case IWL_SISO_SWITCH_ANTENNA1:
1545                 case IWL_SISO_SWITCH_ANTENNA2:
1546                         IWL_DEBUG_RATE(priv, "LQ: SISO toggle Antenna\n");
1547 
1548                         if ((tbl->action == IWL_SISO_SWITCH_ANTENNA1 &&
1549                                                         tx_chains_num <= 1) ||
1550                             (tbl->action == IWL_SISO_SWITCH_ANTENNA2 &&
1551                                                         tx_chains_num <= 2))
1552                                 break;
1553 
1554                         if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1555                                 break;
1556 
1557                         memcpy(search_tbl, tbl, sz);
1558                         if (rs_toggle_antenna(valid_tx_ant,
1559                                        &search_tbl->current_rate, search_tbl)) {
1560                                 update_search_tbl_counter = 1;
1561                                 goto out;
1562                         }
1563                         break;
1564                 case IWL_SISO_SWITCH_MIMO2_AB:
1565                 case IWL_SISO_SWITCH_MIMO2_AC:
1566                 case IWL_SISO_SWITCH_MIMO2_BC:
1567                         IWL_DEBUG_RATE(priv, "LQ: SISO switch to MIMO2\n");
1568                         memcpy(search_tbl, tbl, sz);
1569                         search_tbl->is_SGI = 0;
1570 
1571                         if (tbl->action == IWL_SISO_SWITCH_MIMO2_AB)
1572                                 search_tbl->ant_type = ANT_AB;
1573                         else if (tbl->action == IWL_SISO_SWITCH_MIMO2_AC)
1574                                 search_tbl->ant_type = ANT_AC;
1575                         else
1576                                 search_tbl->ant_type = ANT_BC;
1577 
1578                         if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1579                                 break;
1580 
1581                         ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
1582                                                  search_tbl, index);
1583                         if (!ret)
1584                                 goto out;
1585                         break;
1586                 case IWL_SISO_SWITCH_GI:
1587                         if (!tbl->is_fat &&
1588                                 !(priv->current_ht_config.sgf &
1589                                                 HT_SHORT_GI_20MHZ))
1590                                 break;
1591                         if (tbl->is_fat &&
1592                                 !(priv->current_ht_config.sgf &
1593                                                 HT_SHORT_GI_40MHZ))
1594                                 break;
1595 
1596                         IWL_DEBUG_RATE(priv, "LQ: SISO toggle SGI/NGI\n");
1597 
1598                         memcpy(search_tbl, tbl, sz);
1599                         if (is_green) {
1600                                 if (!tbl->is_SGI)
1601                                         break;
1602                                 else
1603                                         IWL_ERR(priv,
1604                                                 "SGI was set in GF+SISO\n");
1605                         }
1606                         search_tbl->is_SGI = !tbl->is_SGI;
1607                         rs_set_expected_tpt_table(lq_sta, search_tbl);
1608                         if (tbl->is_SGI) {
1609                                 s32 tpt = lq_sta->last_tpt / 100;
1610                                 if (tpt >= search_tbl->expected_tpt[index])
1611                                         break;
1612                         }
1613                         search_tbl->current_rate =
1614                                 rate_n_flags_from_tbl(priv, search_tbl,
1615                                                       index, is_green);
1616                         update_search_tbl_counter = 1;
1617                         goto out;
1618                 case IWL_SISO_SWITCH_MIMO3_ABC:
1619                         IWL_DEBUG_RATE(priv, "LQ: SISO switch to MIMO3\n");
1620                         memcpy(search_tbl, tbl, sz);
1621                         search_tbl->is_SGI = 0;
1622                         search_tbl->ant_type = ANT_ABC;
1623 
1624                         if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1625                                 break;
1626 
1627                         ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1628                                                  search_tbl, index);
1629                         if (!ret)
1630                                 goto out;
1631                         break;
1632                 }
1633                 tbl->action++;
1634                 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
1635                         tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1636 
1637                 if (tbl->action == start_action)
1638                         break;
1639         }
1640         search_tbl->lq_type = LQ_NONE;
1641         return 0;
1642 
1643  out:
1644         lq_sta->search_better_tbl = 1;
1645         tbl->action++;
1646         if (tbl->action > IWL_SISO_SWITCH_MIMO3_ABC)
1647                 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1648         if (update_search_tbl_counter)
1649                 search_tbl->action = tbl->action;
1650 
1651         return 0;
1652 }
1653 
1654 /*
1655  * Try to switch to new modulation mode from MIMO2
1656  */
1657 static int rs_move_mimo2_to_other(struct iwl_priv *priv,
1658                                  struct iwl_lq_sta *lq_sta,
1659                                  struct ieee80211_conf *conf,
1660                                  struct ieee80211_sta *sta, int index)
1661 {
1662         s8 is_green = lq_sta->is_green;
1663         struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1664         struct iwl_scale_tbl_info *search_tbl =
1665                                 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1666         struct iwl_rate_scale_data *window = &(tbl->win[index]);
1667         u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1668                   (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1669         u8 start_action = tbl->action;
1670         u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1671         u8 tx_chains_num = priv->hw_params.tx_chains_num;
1672         u8 update_search_tbl_counter = 0;
1673         int ret;
1674 
1675         for (;;) {
1676                 lq_sta->action_counter++;
1677                 switch (tbl->action) {
1678                 case IWL_MIMO2_SWITCH_ANTENNA1:
1679                 case IWL_MIMO2_SWITCH_ANTENNA2:
1680                         IWL_DEBUG_RATE(priv, "LQ: MIMO2 toggle Antennas\n");
1681 
1682                         if (tx_chains_num <= 2)
1683                                 break;
1684 
1685                         if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1686                                 break;
1687 
1688                         memcpy(search_tbl, tbl, sz);
1689                         if (rs_toggle_antenna(valid_tx_ant,
1690                                        &search_tbl->current_rate, search_tbl)) {
1691                                 update_search_tbl_counter = 1;
1692                                 goto out;
1693                         }
1694                         break;
1695                 case IWL_MIMO2_SWITCH_SISO_A:
1696                 case IWL_MIMO2_SWITCH_SISO_B:
1697                 case IWL_MIMO2_SWITCH_SISO_C:
1698                         IWL_DEBUG_RATE(priv, "LQ: MIMO2 switch to SISO\n");
1699 
1700                         /* Set up new search table for SISO */
1701                         memcpy(search_tbl, tbl, sz);
1702 
1703                         if (tbl->action == IWL_MIMO2_SWITCH_SISO_A)
1704                                 search_tbl->ant_type = ANT_A;
1705                         else if (tbl->action == IWL_MIMO2_SWITCH_SISO_B)
1706                                 search_tbl->ant_type = ANT_B;
1707                         else
1708                                 search_tbl->ant_type = ANT_C;
1709 
1710                         if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1711                                 break;
1712 
1713                         ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
1714                                                  search_tbl, index);
1715                         if (!ret)
1716                                 goto out;
1717 
1718                         break;
1719 
1720                 case IWL_MIMO2_SWITCH_GI:
1721                         if (!tbl->is_fat &&
1722                                 !(priv->current_ht_config.sgf &
1723                                                 HT_SHORT_GI_20MHZ))
1724                                 break;
1725                         if (tbl->is_fat &&
1726                                 !(priv->current_ht_config.sgf &
1727                                                 HT_SHORT_GI_40MHZ))
1728                                 break;
1729 
1730                         IWL_DEBUG_RATE(priv, "LQ: MIMO2 toggle SGI/NGI\n");
1731 
1732                         /* Set up new search table for MIMO2 */
1733                         memcpy(search_tbl, tbl, sz);
1734                         search_tbl->is_SGI = !tbl->is_SGI;
1735                         rs_set_expected_tpt_table(lq_sta, search_tbl);
1736                         /*
1737                          * If active table already uses the fastest possible
1738                          * modulation (dual stream with short guard interval),
1739                          * and it's working well, there's no need to look
1740                          * for a better type of modulation!
1741                          */
1742                         if (tbl->is_SGI) {
1743                                 s32 tpt = lq_sta->last_tpt / 100;
1744                                 if (tpt >= search_tbl->expected_tpt[index])
1745                                         break;
1746                         }
1747                         search_tbl->current_rate =
1748                                 rate_n_flags_from_tbl(priv, search_tbl,
1749                                                       index, is_green);
1750                         update_search_tbl_counter = 1;
1751                         goto out;
1752 
1753                 case IWL_MIMO2_SWITCH_MIMO3_ABC:
1754                         IWL_DEBUG_RATE(priv, "LQ: MIMO2 switch to MIMO3\n");
1755                         memcpy(search_tbl, tbl, sz);
1756                         search_tbl->is_SGI = 0;
1757                         search_tbl->ant_type = ANT_ABC;
1758 
1759                         if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1760                                 break;
1761 
1762                         ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1763                                                  search_tbl, index);
1764                         if (!ret)
1765                                 goto out;
1766 
1767                         break;
1768                 }
1769                 tbl->action++;
1770                 if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
1771                         tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
1772 
1773                 if (tbl->action == start_action)
1774                         break;
1775         }
1776         search_tbl->lq_type = LQ_NONE;
1777         return 0;
1778  out:
1779         lq_sta->search_better_tbl = 1;
1780         tbl->action++;
1781         if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
1782                 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
1783         if (update_search_tbl_counter)
1784                 search_tbl->action = tbl->action;
1785 
1786         return 0;
1787 
1788 }
1789 
1790 /*
1791  * Try to switch to new modulation mode from MIMO3
1792  */
1793 static int rs_move_mimo3_to_other(struct iwl_priv *priv,
1794                                  struct iwl_lq_sta *lq_sta,
1795                                  struct ieee80211_conf *conf,
1796                                  struct ieee80211_sta *sta, int index)
1797 {
1798         s8 is_green = lq_sta->is_green;
1799         struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1800         struct iwl_scale_tbl_info *search_tbl =
1801                                 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1802         struct iwl_rate_scale_data *window = &(tbl->win[index]);
1803         u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1804                   (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1805         u8 start_action = tbl->action;
1806         u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1807         u8 tx_chains_num = priv->hw_params.tx_chains_num;
1808         int ret;
1809         u8 update_search_tbl_counter = 0;
1810 
1811         for (;;) {
1812                 lq_sta->action_counter++;
1813                 switch (tbl->action) {
1814                 case IWL_MIMO3_SWITCH_ANTENNA1:
1815                 case IWL_MIMO3_SWITCH_ANTENNA2:
1816                         IWL_DEBUG_RATE(priv, "LQ: MIMO3 toggle Antennas\n");
1817 
1818                         if (tx_chains_num <= 3)
1819                                 break;
1820 
1821                         if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1822                                 break;
1823 
1824                         memcpy(search_tbl, tbl, sz);
1825                         if (rs_toggle_antenna(valid_tx_ant,
1826                                        &search_tbl->current_rate, search_tbl))
1827                                 goto out;
1828                         break;
1829                 case IWL_MIMO3_SWITCH_SISO_A:
1830                 case IWL_MIMO3_SWITCH_SISO_B:
1831                 case IWL_MIMO3_SWITCH_SISO_C:
1832                         IWL_DEBUG_RATE(priv, "LQ: MIMO3 switch to SISO\n");
1833 
1834                         /* Set up new search table for SISO */
1835                         memcpy(search_tbl, tbl, sz);
1836 
1837                         if (tbl->action == IWL_MIMO3_SWITCH_SISO_A)
1838                                 search_tbl->ant_type = ANT_A;
1839                         else if (tbl->action == IWL_MIMO3_SWITCH_SISO_B)
1840                                 search_tbl->ant_type = ANT_B;
1841                         else
1842                                 search_tbl->ant_type = ANT_C;
1843 
1844                         if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1845                                 break;
1846 
1847                         ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
1848                                                  search_tbl, index);
1849                         if (!ret)
1850                                 goto out;
1851 
1852                         break;
1853 
1854                 case IWL_MIMO3_SWITCH_MIMO2_AB:
1855                 case IWL_MIMO3_SWITCH_MIMO2_AC:
1856                 case IWL_MIMO3_SWITCH_MIMO2_BC:
1857                         IWL_DEBUG_RATE(priv, "LQ: MIMO3 switch to MIMO2\n");
1858 
1859                         memcpy(search_tbl, tbl, sz);
1860                         search_tbl->is_SGI = 0;
1861                         if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AB)
1862                                 search_tbl->ant_type = ANT_AB;
1863                         else if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AC)
1864                                 search_tbl->ant_type = ANT_AC;
1865                         else
1866                                 search_tbl->ant_type = ANT_BC;
1867 
1868                         if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1869                                 break;
1870 
1871                         ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
1872                                                  search_tbl, index);
1873                         if (!ret)
1874                                 goto out;
1875 
1876                         break;
1877 
1878                 case IWL_MIMO3_SWITCH_GI:
1879                         if (!tbl->is_fat &&
1880                                 !(priv->current_ht_config.sgf &
1881                                                 HT_SHORT_GI_20MHZ))
1882                                 break;
1883                         if (tbl->is_fat &&
1884                                 !(priv->current_ht_config.sgf &
1885                                                 HT_SHORT_GI_40MHZ))
1886                                 break;
1887 
1888                         IWL_DEBUG_RATE(priv, "LQ: MIMO3 toggle SGI/NGI\n");
1889 
1890                         /* Set up new search table for MIMO */
1891                         memcpy(search_tbl, tbl, sz);
1892                         search_tbl->is_SGI = !tbl->is_SGI;
1893                         rs_set_expected_tpt_table(lq_sta, search_tbl);
1894                         /*
1895                          * If active table already uses the fastest possible
1896                          * modulation (dual stream with short guard interval),
1897                          * and it's working well, there's no need to look
1898                          * for a better type of modulation!
1899                          */
1900                         if (tbl->is_SGI) {
1901                                 s32 tpt = lq_sta->last_tpt / 100;
1902                                 if (tpt >= search_tbl->expected_tpt[index])
1903                                         break;
1904                         }
1905                         search_tbl->current_rate =
1906                                 rate_n_flags_from_tbl(priv, search_tbl,
1907                                                       index, is_green);
1908                         update_search_tbl_counter = 1;
1909                         goto out;
1910                 }
1911                 tbl->action++;
1912                 if (tbl->action > IWL_MIMO3_SWITCH_GI)
1913                         tbl->action = IWL_MIMO3_SWITCH_ANTENNA1;
1914 
1915                 if (tbl->action == start_action)
1916                         break;
1917         }
1918         search_tbl->lq_type = LQ_NONE;
1919         return 0;
1920  out:
1921         lq_sta->search_better_tbl = 1;
1922         tbl->action++;
1923         if (tbl->action > IWL_MIMO3_SWITCH_GI)
1924                 tbl->action = IWL_MIMO3_SWITCH_ANTENNA1;
1925         if (update_search_tbl_counter)
1926                 search_tbl->action = tbl->action;
1927 
1928         return 0;
1929 
1930 }
1931 
1932 /*
1933  * Check whether we should continue using same modulation mode, or
1934  * begin search for a new mode, based on:
1935  * 1) # tx successes or failures while using this mode
1936  * 2) # times calling this function
1937  * 3) elapsed time in this mode (not used, for now)
1938  */
1939 static void rs_stay_in_table(struct iwl_lq_sta *lq_sta)
1940 {
1941         struct iwl_scale_tbl_info *tbl;
1942         int i;
1943         int active_tbl;
1944         int flush_interval_passed = 0;
1945         struct iwl_priv *priv;
1946 
1947         priv = lq_sta->drv;
1948         active_tbl = lq_sta->active_tbl;
1949 
1950         tbl = &(lq_sta->lq_info[active_tbl]);
1951 
1952         /* If we've been disallowing search, see if we should now allow it */
1953         if (lq_sta->stay_in_tbl) {
1954 
1955                 /* Elapsed time using current modulation mode */
1956                 if (lq_sta->flush_timer)
1957                         flush_interval_passed =
1958                         time_after(jiffies,
1959                                         (unsigned long)(lq_sta->flush_timer +
1960                                         IWL_RATE_SCALE_FLUSH_INTVL));
1961 
1962                 /*
1963                  * Check if we should allow search for new modulation mode.
1964                  * If many frames have failed or succeeded, or we've used
1965                  * this same modulation for a long time, allow search, and
1966                  * reset history stats that keep track of whether we should
1967                  * allow a new search.  Also (below) reset all bitmaps and
1968                  * stats in active history.
1969                  */
1970                 if ((lq_sta->total_failed > lq_sta->max_failure_limit) ||
1971                     (lq_sta->total_success > lq_sta->max_success_limit) ||
1972                     ((!lq_sta->search_better_tbl) && (lq_sta->flush_timer)
1973                      && (flush_interval_passed))) {
1974                         IWL_DEBUG_RATE(priv, "LQ: stay is expired %d %d %d\n:",
1975                                      lq_sta->total_failed,
1976                                      lq_sta->total_success,
1977                                      flush_interval_passed);
1978 
1979                         /* Allow search for new mode */
1980                         lq_sta->stay_in_tbl = 0;        /* only place reset */
1981                         lq_sta->total_failed = 0;
1982                         lq_sta->total_success = 0;
1983                         lq_sta->flush_timer = 0;
1984 
1985                 /*
1986                  * Else if we've used this modulation mode enough repetitions
1987                  * (regardless of elapsed time or success/failure), reset
1988                  * history bitmaps and rate-specific stats for all rates in
1989                  * active table.
1990                  */
1991                 } else {
1992                         lq_sta->table_count++;
1993                         if (lq_sta->table_count >=
1994                             lq_sta->table_count_limit) {
1995                                 lq_sta->table_count = 0;
1996 
1997                                 IWL_DEBUG_RATE(priv, "LQ: stay in table clear win\n");
1998                                 for (i = 0; i < IWL_RATE_COUNT; i++)
1999                                         rs_rate_scale_clear_window(
2000                                                 &(tbl->win[i]));
2001                         }
2002                 }
2003 
2004                 /* If transitioning to allow "search", reset all history
2005                  * bitmaps and stats in active table (this will become the new
2006                  * "search" table). */
2007                 if (!lq_sta->stay_in_tbl) {
2008                         for (i = 0; i < IWL_RATE_COUNT; i++)
2009                                 rs_rate_scale_clear_window(&(tbl->win[i]));
2010                 }
2011         }
2012 }
2013 
2014 /*
2015  * Do rate scaling and search for new modulation mode.
2016  */
2017 static void rs_rate_scale_perform(struct iwl_priv *priv,
2018                                   struct sk_buff *skb,
2019                                   struct ieee80211_sta *sta,
2020                                   struct iwl_lq_sta *lq_sta)
2021 {
2022         struct ieee80211_hw *hw = priv->hw;
2023         struct ieee80211_conf *conf = &hw->conf;
2024         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2025         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2026         int low = IWL_RATE_INVALID;
2027         int high = IWL_RATE_INVALID;
2028         int index;
2029         int i;
2030         struct iwl_rate_scale_data *window = NULL;
2031         int current_tpt = IWL_INVALID_VALUE;
2032         int low_tpt = IWL_INVALID_VALUE;
2033         int high_tpt = IWL_INVALID_VALUE;
2034         u32 fail_count;
2035         s8 scale_action = 0;
2036         u16 rate_mask;
2037         u8 update_lq = 0;
2038         struct iwl_scale_tbl_info *tbl, *tbl1;
2039         u16 rate_scale_index_msk = 0;
2040         u32 rate;
2041         u8 is_green = 0;
2042         u8 active_tbl = 0;
2043         u8 done_search = 0;
2044         u16 high_low;
2045         s32 sr;
2046         u8 tid = MAX_TID_COUNT;
2047         struct iwl_tid_data *tid_data;
2048 
2049         IWL_DEBUG_RATE(priv, "rate scale calculate new rate for skb\n");
2050 
2051         /* Send management frames and NO_ACK data using lowest rate. */
2052         /* TODO: this could probably be improved.. */
2053         if (!ieee80211_is_data(hdr->frame_control) ||
2054             info->flags & IEEE80211_TX_CTL_NO_ACK)
2055                 return;
2056 
2057         if (!sta || !lq_sta)
2058                 return;
2059 
2060         lq_sta->supp_rates = sta->supp_rates[lq_sta->band];
2061 
2062         tid = rs_tl_add_packet(lq_sta, hdr);
2063 
2064         /*
2065          * Select rate-scale / modulation-mode table to work with in
2066          * the rest of this function:  "search" if searching for better
2067          * modulation mode, or "active" if doing rate scaling within a mode.
2068          */
2069         if (!lq_sta->search_better_tbl)
2070                 active_tbl = lq_sta->active_tbl;
2071         else
2072                 active_tbl = 1 - lq_sta->active_tbl;
2073 
2074         tbl = &(lq_sta->lq_info[active_tbl]);
2075         if (is_legacy(tbl->lq_type))
2076                 lq_sta->is_green = 0;
2077         else
2078                 lq_sta->is_green = rs_use_green(priv, conf);
2079         is_green = lq_sta->is_green;
2080 
2081         /* current tx rate */
2082         index = lq_sta->last_txrate_idx;
2083 
2084         IWL_DEBUG_RATE(priv, "Rate scale index %d for type %d\n", index,
2085                        tbl->lq_type);
2086 
2087         /* rates available for this association, and for modulation mode */
2088         rate_mask = rs_get_supported_rates(lq_sta, hdr, tbl->lq_type);
2089 
2090         IWL_DEBUG_RATE(priv, "mask 0x%04X \n", rate_mask);
2091 
2092         /* mask with station rate restriction */
2093         if (is_legacy(tbl->lq_type)) {
2094                 if (lq_sta->band == IEEE80211_BAND_5GHZ)
2095                         /* supp_rates has no CCK bits in A mode */
2096                         rate_scale_index_msk = (u16) (rate_mask &
2097                                 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
2098                 else
2099                         rate_scale_index_msk = (u16) (rate_mask &
2100                                                       lq_sta->supp_rates);
2101 
2102         } else
2103                 rate_scale_index_msk = rate_mask;
2104 
2105         if (!rate_scale_index_msk)
2106                 rate_scale_index_msk = rate_mask;
2107 
2108         if (!((1 << index) & rate_scale_index_msk)) {
2109                 IWL_ERR(priv, "Current Rate is not valid\n");
2110                 return;
2111         }
2112 
2113         /* Get expected throughput table and history window for current rate */
2114         if (!tbl->expected_tpt) {
2115                 IWL_ERR(priv, "tbl->expected_tpt is NULL\n");
2116                 return;
2117         }
2118 
2119         /* force user max rate if set by user */
2120         if ((lq_sta->max_rate_idx != -1) &&
2121             (lq_sta->max_rate_idx < index)) {
2122                 index = lq_sta->max_rate_idx;
2123                 update_lq = 1;
2124                 window = &(tbl->win[index]);
2125                 goto lq_update;
2126         }
2127 
2128         window = &(tbl->win[index]);
2129 
2130         /*
2131          * If there is not enough history to calculate actual average
2132          * throughput, keep analyzing results of more tx frames, without
2133          * changing rate or mode (bypass most of the rest of this function).
2134          * Set up new rate table in uCode only if old rate is not supported
2135          * in current association (use new rate found above).
2136          */
2137         fail_count = window->counter - window->success_counter;
2138         if ((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
2139                         (window->success_counter < IWL_RATE_MIN_SUCCESS_TH)) {
2140                 IWL_DEBUG_RATE(priv, "LQ: still below TH. succ=%d total=%d "
2141                                "for index %d\n",
2142                                window->success_counter, window->counter, index);
2143 
2144                 /* Can't calculate this yet; not enough history */
2145                 window->average_tpt = IWL_INVALID_VALUE;
2146 
2147                 /* Should we stay with this modulation mode,
2148                  * or search for a new one? */
2149                 rs_stay_in_table(lq_sta);
2150 
2151                 goto out;
2152         }
2153 
2154         /* Else we have enough samples; calculate estimate of
2155          * actual average throughput */
2156 
2157         BUG_ON(window->average_tpt != ((window->success_ratio *
2158                         tbl->expected_tpt[index] + 64) / 128));
2159 
2160         /* If we are searching for better modulation mode, check success. */
2161         if (lq_sta->search_better_tbl) {
2162 
2163                 /* If good success, continue using the "search" mode;
2164                  * no need to send new link quality command, since we're
2165                  * continuing to use the setup that we've been trying. */
2166                 if (window->average_tpt > lq_sta->last_tpt) {
2167 
2168                         IWL_DEBUG_RATE(priv, "LQ: SWITCHING TO NEW TABLE "
2169                                         "suc=%d cur-tpt=%d old-tpt=%d\n",
2170                                         window->success_ratio,
2171                                         window->average_tpt,
2172                                         lq_sta->last_tpt);
2173 
2174                         if (!is_legacy(tbl->lq_type))
2175                                 lq_sta->enable_counter = 1;
2176 
2177                         /* Swap tables; "search" becomes "active" */
2178                         lq_sta->active_tbl = active_tbl;
2179                         current_tpt = window->average_tpt;
2180 
2181                 /* Else poor success; go back to mode in "active" table */
2182                 } else {
2183 
2184                         IWL_DEBUG_RATE(priv, "LQ: GOING BACK TO THE OLD TABLE "
2185                                         "suc=%d cur-tpt=%d old-tpt=%d\n",
2186                                         window->success_ratio,
2187                                         window->average_tpt,
2188                                         lq_sta->last_tpt);
2189 
2190                         /* Nullify "search" table */
2191                         tbl->lq_type = LQ_NONE;
2192 
2193                         /* Revert to "active" table */
2194                         active_tbl = lq_sta->active_tbl;
2195                         tbl = &(lq_sta->lq_info[active_tbl]);
2196 
2197                         /* Revert to "active" rate and throughput info */
2198                         index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
2199                         current_tpt = lq_sta->last_tpt;
2200 
2201                         /* Need to set up a new rate table in uCode */
2202                         update_lq = 1;
2203                 }
2204 
2205                 /* Either way, we've made a decision; modulation mode
2206                  * search is done, allow rate adjustment next time. */
2207                 lq_sta->search_better_tbl = 0;
2208                 done_search = 1;        /* Don't switch modes below! */
2209                 goto lq_update;
2210         }
2211 
2212         /* (Else) not in search of better modulation mode, try for better
2213          * starting rate, while staying in this mode. */
2214         high_low = rs_get_adjacent_rate(priv, index, rate_scale_index_msk,
2215                                         tbl->lq_type);
2216         low = high_low & 0xff;
2217         high = (high_low >> 8) & 0xff;
2218 
2219         /* If user set max rate, dont allow higher than user constrain */
2220         if ((lq_sta->max_rate_idx != -1) &&
2221             (lq_sta->max_rate_idx < high))
2222                 high = IWL_RATE_INVALID;
2223 
2224         sr = window->success_ratio;
2225 
2226         /* Collect measured throughputs for current and adjacent rates */
2227         current_tpt = window->average_tpt;
2228         if (low != IWL_RATE_INVALID)
2229                 low_tpt = tbl->win[low].average_tpt;
2230         if (high != IWL_RATE_INVALID)
2231                 high_tpt = tbl->win[high].average_tpt;
2232 
2233         scale_action = 0;
2234 
2235         /* Too many failures, decrease rate */
2236         if ((sr <= IWL_RATE_DECREASE_TH) || (current_tpt == 0)) {
2237                 IWL_DEBUG_RATE(priv, "decrease rate because of low success_ratio\n");
2238                 scale_action = -1;
2239 
2240         /* No throughput measured yet for adjacent rates; try increase. */
2241         } else if ((low_tpt == IWL_INVALID_VALUE) &&
2242                    (high_tpt == IWL_INVALID_VALUE)) {
2243 
2244                 if (high != IWL_RATE_INVALID && sr >= IWL_RATE_INCREASE_TH)
2245                         scale_action = 1;
2246                 else if (low != IWL_RATE_INVALID)
2247                         scale_action = 0;
2248         }
2249 
2250         /* Both adjacent throughputs are measured, but neither one has better
2251          * throughput; we're using the best rate, don't change it! */
2252         else if ((low_tpt != IWL_INVALID_VALUE) &&
2253                  (high_tpt != IWL_INVALID_VALUE) &&
2254                  (low_tpt < current_tpt) &&
2255                  (high_tpt < current_tpt))
2256                 scale_action = 0;
2257 
2258         /* At least one adjacent rate's throughput is measured,
2259          * and may have better performance. */
2260         else {
2261                 /* Higher adjacent rate's throughput is measured */
2262                 if (high_tpt != IWL_INVALID_VALUE) {
2263                         /* Higher rate has better throughput */
2264                         if (high_tpt > current_tpt &&
2265                                         sr >= IWL_RATE_INCREASE_TH) {
2266                                 scale_action = 1;
2267                         } else {
2268                                 scale_action = 0;
2269                         }
2270 
2271                 /* Lower adjacent rate's throughput is measured */
2272                 } else if (low_tpt != IWL_INVALID_VALUE) {
2273                         /* Lower rate has better throughput */
2274                         if (low_tpt > current_tpt) {
2275                                 IWL_DEBUG_RATE(priv,
2276                                     "decrease rate because of low tpt\n");
2277                                 scale_action = -1;
2278                         } else if (sr >= IWL_RATE_INCREASE_TH) {
2279                                 scale_action = 1;
2280                         }
2281                 }
2282         }
2283 
2284         /* Sanity check; asked for decrease, but success rate or throughput
2285          * has been good at old rate.  Don't change it. */
2286         if ((scale_action == -1) && (low != IWL_RATE_INVALID) &&
2287                     ((sr > IWL_RATE_HIGH_TH) ||
2288                      (current_tpt > (100 * tbl->expected_tpt[low]))))
2289                 scale_action = 0;
2290 
2291         switch (scale_action) {
2292         case -1:
2293                 /* Decrease starting rate, update uCode's rate table */
2294                 if (low != IWL_RATE_INVALID) {
2295                         update_lq = 1;
2296                         index = low;
2297                 }
2298 
2299                 break;
2300         case 1:
2301                 /* Increase starting rate, update uCode's rate table */
2302                 if (high != IWL_RATE_INVALID) {
2303                         update_lq = 1;
2304                         index = high;
2305                 }
2306 
2307                 break;
2308         case 0:
2309                 /* No change */
2310         default:
2311                 break;
2312         }
2313 
2314         IWL_DEBUG_RATE(priv, "choose rate scale index %d action %d low %d "
2315                     "high %d type %d\n",
2316                      index, scale_action, low, high, tbl->lq_type);
2317 
2318 lq_update:
2319         /* Replace uCode's rate table for the destination station. */
2320         if (update_lq) {
2321                 rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
2322                 rs_fill_link_cmd(priv, lq_sta, rate);
2323                 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
2324         }
2325 
2326         /* Should we stay with this modulation mode, or search for a new one? */
2327         rs_stay_in_table(lq_sta);
2328 
2329         /*
2330          * Search for new modulation mode if we're:
2331          * 1)  Not changing rates right now
2332          * 2)  Not just finishing up a search
2333          * 3)  Allowing a new search
2334          */
2335         if (!update_lq && !done_search && !lq_sta->stay_in_tbl && window->counter) {
2336                 /* Save current throughput to compare with "search" throughput*/
2337                 lq_sta->last_tpt = current_tpt;
2338 
2339                 /* Select a new "search" modulation mode to try.
2340                  * If one is found, set up the new "search" table. */
2341                 if (is_legacy(tbl->lq_type))
2342                         rs_move_legacy_other(priv, lq_sta, conf, sta, index);
2343                 else if (is_siso(tbl->lq_type))
2344                         rs_move_siso_to_other(priv, lq_sta, conf, sta, index);
2345                 else if (is_mimo2(tbl->lq_type))
2346                         rs_move_mimo2_to_other(priv, lq_sta, conf, sta, index);
2347                 else
2348                         rs_move_mimo3_to_other(priv, lq_sta, conf, sta, index);
2349 
2350                 /* If new "search" mode was selected, set up in uCode table */
2351                 if (lq_sta->search_better_tbl) {
2352                         /* Access the "search" table, clear its history. */
2353                         tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
2354                         for (i = 0; i < IWL_RATE_COUNT; i++)
2355                                 rs_rate_scale_clear_window(&(tbl->win[i]));
2356 
2357                         /* Use new "search" start rate */
2358                         index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
2359 
2360                         IWL_DEBUG_RATE(priv, "Switch current  mcs: %X index: %d\n",
2361                                      tbl->current_rate, index);
2362                         rs_fill_link_cmd(priv, lq_sta, tbl->current_rate);
2363                         iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
2364                 } else
2365                         done_search = 1;
2366         }
2367 
2368         if (done_search && !lq_sta->stay_in_tbl) {
2369                 /* If the "active" (non-search) mode was legacy,
2370                  * and we've tried switching antennas,
2371                  * but we haven't been able to try HT modes (not available),
2372                  * stay with best antenna legacy modulation for a while
2373                  * before next round of mode comparisons. */
2374                 tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
2375                 if (is_legacy(tbl1->lq_type) && !conf_is_ht(conf) &&
2376                     lq_sta->action_counter > tbl1->max_search) {
2377                         IWL_DEBUG_RATE(priv, "LQ: STAY in legacy table\n");
2378                         rs_set_stay_in_table(priv, 1, lq_sta);
2379                 }
2380 
2381                 /* If we're in an HT mode, and all 3 mode switch actions
2382                  * have been tried and compared, stay in this best modulation
2383                  * mode for a while before next round of mode comparisons. */
2384                 if (lq_sta->enable_counter &&
2385                     (lq_sta->action_counter >= tbl1->max_search)) {
2386                         if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
2387                             (lq_sta->tx_agg_tid_en & (1 << tid)) &&
2388                             (tid != MAX_TID_COUNT)) {
2389                                 tid_data =
2390                                    &priv->stations[lq_sta->lq.sta_id].tid[tid];
2391                                 if (tid_data->agg.state == IWL_AGG_OFF) {
2392                                         IWL_DEBUG_RATE(priv,
2393                                                        "try to aggregate tid %d\n",
2394                                                        tid);
2395                                         rs_tl_turn_on_agg(priv, tid,
2396                                                           lq_sta, sta);
2397                                 }
2398                         }
2399                         rs_set_stay_in_table(priv, 0, lq_sta);
2400                 }
2401         }
2402 
2403 out:
2404         tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
2405         i = index;
2406         lq_sta->last_txrate_idx = i;
2407 
2408         return;
2409 }
2410 
2411 
2412 static void rs_initialize_lq(struct iwl_priv *priv,
2413                              struct ieee80211_conf *conf,
2414                              struct ieee80211_sta *sta,
2415                              struct iwl_lq_sta *lq_sta)
2416 {
2417         struct iwl_scale_tbl_info *tbl;
2418         int rate_idx;
2419         int i;
2420         u32 rate;
2421         u8 use_green = rs_use_green(priv, conf);
2422         u8 active_tbl = 0;
2423         u8 valid_tx_ant;
2424 
2425         if (!sta || !lq_sta)
2426                 goto out;
2427 
2428         i = lq_sta->last_txrate_idx;
2429 
2430         if ((lq_sta->lq.sta_id == 0xff) &&
2431             (priv->iw_mode == NL80211_IFTYPE_ADHOC))
2432                 goto out;
2433 
2434         valid_tx_ant = priv->hw_params.valid_tx_ant;
2435 
2436         if (!lq_sta->search_better_tbl)
2437                 active_tbl = lq_sta->active_tbl;
2438         else
2439                 active_tbl = 1 - lq_sta->active_tbl;
2440 
2441         tbl = &(lq_sta->lq_info[active_tbl]);
2442 
2443         if ((i < 0) || (i >= IWL_RATE_COUNT))
2444                 i = 0;
2445 
2446         rate = iwl_rates[i].plcp;
2447         tbl->ant_type = first_antenna(valid_tx_ant);
2448         rate |= tbl->ant_type << RATE_MCS_ANT_POS;
2449 
2450         if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
2451                 rate |= RATE_MCS_CCK_MSK;
2452 
2453         rs_get_tbl_info_from_mcs(rate, priv->band, tbl, &rate_idx);
2454         if (!rs_is_valid_ant(valid_tx_ant, tbl->ant_type))
2455             rs_toggle_antenna(valid_tx_ant, &rate, tbl);
2456 
2457         rate = rate_n_flags_from_tbl(priv, tbl, rate_idx, use_green);
2458         tbl->current_rate = rate;
2459         rs_set_expected_tpt_table(lq_sta, tbl);
2460         rs_fill_link_cmd(NULL, lq_sta, rate);
2461         iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
2462  out:
2463         return;
2464 }
2465 
2466 static void rs_get_rate(void *priv_r, struct ieee80211_sta *sta, void *priv_sta,
2467                         struct ieee80211_tx_rate_control *txrc)
2468 {
2469 
2470         struct sk_buff *skb = txrc->skb;
2471         struct ieee80211_supported_band *sband = txrc->sband;
2472         struct iwl_priv *priv = (struct iwl_priv *)priv_r;
2473         struct ieee80211_conf *conf = &priv->hw->conf;
2474         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2475         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2476         struct iwl_lq_sta *lq_sta = priv_sta;
2477         int rate_idx;
2478         u64 mask_bit = 0;
2479 
2480         IWL_DEBUG_RATE_LIMIT(priv, "rate scale calculate new rate for skb\n");
2481 
2482         /* Get max rate if user set max rate */
2483         if (lq_sta) {
2484                 lq_sta->max_rate_idx = txrc->max_rate_idx;
2485                 if ((sband->band == IEEE80211_BAND_5GHZ) &&
2486                     (lq_sta->max_rate_idx != -1))
2487                         lq_sta->max_rate_idx += IWL_FIRST_OFDM_RATE;
2488                 if ((lq_sta->max_rate_idx < 0) ||
2489                     (lq_sta->max_rate_idx >= IWL_RATE_COUNT))
2490                         lq_sta->max_rate_idx = -1;
2491         }
2492 
2493         if (sta)
2494                 mask_bit = sta->supp_rates[sband->band];
2495 
2496         /* Send management frames and NO_ACK data using lowest rate. */
2497         if (!ieee80211_is_data(hdr->frame_control) ||
2498             info->flags & IEEE80211_TX_CTL_NO_ACK || !sta || !lq_sta) {
2499                 if (!mask_bit)
2500                         info->control.rates[0].idx =
2501                                         rate_lowest_index(sband, NULL);
2502                 else
2503                         info->control.rates[0].idx =
2504                                         rate_lowest_index(sband, sta);
2505                 if (info->flags & IEEE80211_TX_CTL_NO_ACK)
2506                         info->control.rates[0].count = 1;
2507                 return;
2508         }
2509 
2510         rate_idx  = lq_sta->last_txrate_idx;
2511 
2512         if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) &&
2513             !lq_sta->ibss_sta_added) {
2514                 u8 sta_id = iwl_find_station(priv, hdr->addr1);
2515 
2516                 if (sta_id == IWL_INVALID_STATION) {
2517                         IWL_DEBUG_RATE(priv, "LQ: ADD station %pM\n",
2518                                        hdr->addr1);
2519                         sta_id = iwl_add_station(priv, hdr->addr1,
2520                                                 false, CMD_ASYNC, NULL);
2521                 }
2522                 if ((sta_id != IWL_INVALID_STATION)) {
2523                         lq_sta->lq.sta_id = sta_id;
2524                         lq_sta->lq.rs_table[0].rate_n_flags = 0;
2525                         lq_sta->ibss_sta_added = 1;
2526                         rs_initialize_lq(priv, conf, sta, lq_sta);
2527                 }
2528         }
2529 
2530         if (lq_sta->last_rate_n_flags & RATE_MCS_HT_MSK) {
2531                 rate_idx -= IWL_FIRST_OFDM_RATE;
2532                 /* 6M and 9M shared same MCS index */
2533                 rate_idx = (rate_idx > 0) ? (rate_idx - 1) : 0;
2534                 if (rs_extract_rate(lq_sta->last_rate_n_flags) >=
2535                     IWL_RATE_MIMO3_6M_PLCP)
2536                         rate_idx = rate_idx + (2 * MCS_INDEX_PER_STREAM);
2537                 else if (rs_extract_rate(lq_sta->last_rate_n_flags) >=
2538                          IWL_RATE_MIMO2_6M_PLCP)
2539                         rate_idx = rate_idx + MCS_INDEX_PER_STREAM;
2540                 info->control.rates[0].flags = IEEE80211_TX_RC_MCS;
2541                 if (lq_sta->last_rate_n_flags & RATE_MCS_SGI_MSK)
2542                         info->control.rates[0].flags |= IEEE80211_TX_RC_SHORT_GI;
2543                 if (lq_sta->last_rate_n_flags & RATE_MCS_DUP_MSK)
2544                         info->control.rates[0].flags |= IEEE80211_TX_RC_DUP_DATA;
2545                 if (lq_sta->last_rate_n_flags & RATE_MCS_FAT_MSK)
2546                         info->control.rates[0].flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
2547                 if (lq_sta->last_rate_n_flags & RATE_MCS_GF_MSK)
2548                         info->control.rates[0].flags |= IEEE80211_TX_RC_GREEN_FIELD;
2549         } else {
2550                 if (rate_idx < 0 || rate_idx > IWL_RATE_COUNT)
2551                         rate_idx = rate_lowest_index(sband, sta);
2552                 else if (sband->band == IEEE80211_BAND_5GHZ)
2553                         rate_idx -= IWL_FIRST_OFDM_RATE;
2554         }
2555         info->control.rates[0].idx = rate_idx;
2556 
2557 }
2558 
2559 static void *rs_alloc_sta(void *priv_rate, struct ieee80211_sta *sta,
2560                           gfp_t gfp)
2561 {
2562         struct iwl_lq_sta *lq_sta;
2563         struct iwl_priv *priv;
2564         int i, j;
2565 
2566         priv = (struct iwl_priv *)priv_rate;
2567         IWL_DEBUG_RATE(priv, "create station rate scale window\n");
2568 
2569         lq_sta = kzalloc(sizeof(struct iwl_lq_sta), gfp);
2570 
2571         if (lq_sta == NULL)
2572                 return NULL;
2573         lq_sta->lq.sta_id = 0xff;
2574 
2575 
2576         for (j = 0; j < LQ_SIZE; j++)
2577                 for (i = 0; i < IWL_RATE_COUNT; i++)
2578                         rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
2579 
2580         return lq_sta;
2581 }
2582 
2583 static void rs_rate_init(void *priv_r, struct ieee80211_supported_band *sband,
2584                          struct ieee80211_sta *sta, void *priv_sta)
2585 {
2586         int i, j;
2587         struct iwl_priv *priv = (struct iwl_priv *)priv_r;
2588         struct ieee80211_conf *conf = &priv->hw->conf;
2589         struct iwl_lq_sta *lq_sta = priv_sta;
2590         u16 mask_bit = 0;
2591         int count;
2592         int start_rate = 0;
2593 
2594         lq_sta->flush_timer = 0;
2595         lq_sta->supp_rates = sta->supp_rates[sband->band];
2596         for (j = 0; j < LQ_SIZE; j++)
2597                 for (i = 0; i < IWL_RATE_COUNT; i++)
2598                         rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
2599 
2600         IWL_DEBUG_RATE(priv, "LQ: *** rate scale station global init ***\n");
2601         /* TODO: what is a good starting rate for STA? About middle? Maybe not
2602          * the lowest or the highest rate.. Could consider using RSSI from
2603          * previous packets? Need to have IEEE 802.1X auth succeed immediately
2604          * after assoc.. */
2605 
2606         lq_sta->ibss_sta_added = 0;
2607         if (priv->iw_mode == NL80211_IFTYPE_AP) {
2608                 u8 sta_id = iwl_find_station(priv,
2609                                                                 sta->addr);
2610 
2611                 /* for IBSS the call are from tasklet */
2612                 IWL_DEBUG_RATE(priv, "LQ: ADD station %pM\n", sta->addr);
2613 
2614                 if (sta_id == IWL_INVALID_STATION) {
2615                         IWL_DEBUG_RATE(priv, "LQ: ADD station %pM\n", sta->addr);
2616                         sta_id = iwl_add_station(priv, sta->addr, false,
2617                                                 CMD_ASYNC, NULL);
2618                 }
2619                 if ((sta_id != IWL_INVALID_STATION)) {
2620                         lq_sta->lq.sta_id = sta_id;
2621                         lq_sta->lq.rs_table[0].rate_n_flags = 0;
2622                 }
2623                 /* FIXME: this is w/a remove it later */
2624                 priv->assoc_station_added = 1;
2625         }
2626 
2627         lq_sta->is_dup = 0;
2628         lq_sta->max_rate_idx = -1;
2629         lq_sta->missed_rate_counter = IWL_MISSED_RATE_MAX;
2630         lq_sta->is_green = rs_use_green(priv, conf);
2631         lq_sta->active_legacy_rate = priv->active_rate & ~(0x1000);
2632         lq_sta->active_rate_basic = priv->active_rate_basic;
2633         lq_sta->band = priv->band;
2634         /*
2635          * active_siso_rate mask includes 9 MBits (bit 5), and CCK (bits 0-3),
2636          * supp_rates[] does not; shift to convert format, force 9 MBits off.
2637          */
2638         lq_sta->active_siso_rate = sta->ht_cap.mcs.rx_mask[0] << 1;
2639         lq_sta->active_siso_rate |= sta->ht_cap.mcs.rx_mask[0] & 0x1;
2640         lq_sta->active_siso_rate &= ~((u16)0x2);
2641         lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE;
2642 
2643         /* Same here */
2644         lq_sta->active_mimo2_rate = sta->ht_cap.mcs.rx_mask[1] << 1;
2645         lq_sta->active_mimo2_rate |= sta->ht_cap.mcs.rx_mask[1] & 0x1;
2646         lq_sta->active_mimo2_rate &= ~((u16)0x2);
2647         lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE;
2648 
2649         lq_sta->active_mimo3_rate = sta->ht_cap.mcs.rx_mask[2] << 1;
2650         lq_sta->active_mimo3_rate |= sta->ht_cap.mcs.rx_mask[2] & 0x1;
2651         lq_sta->active_mimo3_rate &= ~((u16)0x2);
2652         lq_sta->active_mimo3_rate <<= IWL_FIRST_OFDM_RATE;
2653 
2654         IWL_DEBUG_RATE(priv, "SISO-RATE=%X MIMO2-RATE=%X MIMO3-RATE=%X\n",
2655                      lq_sta->active_siso_rate,
2656                      lq_sta->active_mimo2_rate,
2657                      lq_sta->active_mimo3_rate);
2658 
2659         /* These values will be overridden later */
2660         lq_sta->lq.general_params.single_stream_ant_msk = ANT_A;
2661         lq_sta->lq.general_params.dual_stream_ant_msk = ANT_AB;
2662 
2663         /* as default allow aggregation for all tids */
2664         lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
2665         lq_sta->drv = priv;
2666 
2667         /* Find highest tx rate supported by hardware and destination station */
2668         mask_bit = sta->supp_rates[sband->band];
2669         count = sband->n_bitrates;
2670         if (sband->band == IEEE80211_BAND_5GHZ) {
2671                 count += IWL_FIRST_OFDM_RATE;
2672                 start_rate = IWL_FIRST_OFDM_RATE;
2673                 mask_bit <<= IWL_FIRST_OFDM_RATE;
2674         }
2675 
2676         mask_bit = mask_bit & lq_sta->active_legacy_rate;
2677         lq_sta->last_txrate_idx = 4;
2678         for (i = start_rate; i < count; i++)
2679                 if (mask_bit & BIT(i))
2680                         lq_sta->last_txrate_idx = i;
2681 
2682         rs_initialize_lq(priv, conf, sta, lq_sta);
2683 }
2684 
2685 static void rs_fill_link_cmd(const struct iwl_priv *priv,
2686                              struct iwl_lq_sta *lq_sta, u32 new_rate)
2687 {
2688         struct iwl_scale_tbl_info tbl_type;
2689         int index = 0;
2690         int rate_idx;
2691         int repeat_rate = 0;
2692         u8 ant_toggle_cnt = 0;
2693         u8 use_ht_possible = 1;
2694         u8 valid_tx_ant = 0;
2695         struct iwl_link_quality_cmd *lq_cmd = &lq_sta->lq;
2696 
2697         /* Override starting rate (index 0) if needed for debug purposes */
2698         rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
2699 
2700         /* Interpret new_rate (rate_n_flags) */
2701         memset(&tbl_type, 0, sizeof(tbl_type));
2702         rs_get_tbl_info_from_mcs(new_rate, lq_sta->band,
2703                                   &tbl_type, &rate_idx);
2704 
2705         /* How many times should we repeat the initial rate? */
2706         if (is_legacy(tbl_type.lq_type)) {
2707                 ant_toggle_cnt = 1;
2708                 repeat_rate = IWL_NUMBER_TRY;
2709         } else {
2710                 repeat_rate = IWL_HT_NUMBER_TRY;
2711         }
2712 
2713         lq_cmd->general_params.mimo_delimiter =
2714                         is_mimo(tbl_type.lq_type) ? 1 : 0;
2715 
2716         /* Fill 1st table entry (index 0) */
2717         lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
2718 
2719         if (num_of_ant(tbl_type.ant_type) == 1) {
2720                 lq_cmd->general_params.single_stream_ant_msk =
2721                                                 tbl_type.ant_type;
2722         } else if (num_of_ant(tbl_type.ant_type) == 2) {
2723                 lq_cmd->general_params.dual_stream_ant_msk =
2724                                                 tbl_type.ant_type;
2725         } /* otherwise we don't modify the existing value */
2726 
2727         index++;
2728         repeat_rate--;
2729 
2730         if (priv)
2731                 valid_tx_ant = priv->hw_params.valid_tx_ant;
2732 
2733         /* Fill rest of rate table */
2734         while (index < LINK_QUAL_MAX_RETRY_NUM) {
2735                 /* Repeat initial/next rate.
2736                  * For legacy IWL_NUMBER_TRY == 1, this loop will not execute.
2737                  * For HT IWL_HT_NUMBER_TRY == 3, this executes twice. */
2738                 while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
2739                         if (is_legacy(tbl_type.lq_type)) {
2740                                 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2741                                         ant_toggle_cnt++;
2742                                 else if (priv &&
2743                                          rs_toggle_antenna(valid_tx_ant,
2744                                                         &new_rate, &tbl_type))
2745                                         ant_toggle_cnt = 1;
2746 }
2747 
2748                         /* Override next rate if needed for debug purposes */
2749                         rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
2750 
2751                         /* Fill next table entry */
2752                         lq_cmd->rs_table[index].rate_n_flags =
2753                                         cpu_to_le32(new_rate);
2754                         repeat_rate--;
2755                         index++;
2756                 }
2757 
2758                 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band, &tbl_type,
2759                                                 &rate_idx);
2760 
2761                 /* Indicate to uCode which entries might be MIMO.
2762                  * If initial rate was MIMO, this will finally end up
2763                  * as (IWL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
2764                 if (is_mimo(tbl_type.lq_type))
2765                         lq_cmd->general_params.mimo_delimiter = index;
2766 
2767                 /* Get next rate */
2768                 new_rate = rs_get_lower_rate(lq_sta, &tbl_type, rate_idx,
2769                                              use_ht_possible);
2770 
2771                 /* How many times should we repeat the next rate? */
2772                 if (is_legacy(tbl_type.lq_type)) {
2773                         if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2774                                 ant_toggle_cnt++;
2775                         else if (priv &&
2776                                  rs_toggle_antenna(valid_tx_ant,
2777                                                    &new_rate, &tbl_type))
2778                                 ant_toggle_cnt = 1;
2779 
2780                         repeat_rate = IWL_NUMBER_TRY;
2781                 } else {
2782                         repeat_rate = IWL_HT_NUMBER_TRY;
2783                 }
2784 
2785                 /* Don't allow HT rates after next pass.
2786                  * rs_get_lower_rate() will change type to LQ_A or LQ_G. */
2787                 use_ht_possible = 0;
2788 
2789                 /* Override next rate if needed for debug purposes */
2790                 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
2791 
2792                 /* Fill next table entry */
2793                 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
2794 
2795                 index++;
2796                 repeat_rate--;
2797         }
2798 
2799         lq_cmd->agg_params.agg_frame_cnt_limit = LINK_QUAL_AGG_FRAME_LIMIT_MAX;
2800         lq_cmd->agg_params.agg_dis_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
2801         lq_cmd->agg_params.agg_time_limit =
2802                 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
2803 }
2804 
2805 static void *rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
2806 {
2807         return hw->priv;
2808 }
2809 /* rate scale requires free function to be implemented */
2810 static void rs_free(void *priv_rate)
2811 {
2812         return;
2813 }
2814 
2815 static void rs_free_sta(void *priv_r, struct ieee80211_sta *sta,
2816                         void *priv_sta)
2817 {
2818         struct iwl_lq_sta *lq_sta = priv_sta;
2819         struct iwl_priv *priv __maybe_unused = priv_r;
2820 
2821         IWL_DEBUG_RATE(priv, "enter\n");
2822         kfree(lq_sta);
2823         IWL_DEBUG_RATE(priv, "leave\n");
2824 }
2825 
2826 
2827 #ifdef CONFIG_MAC80211_DEBUGFS
2828 static int open_file_generic(struct inode *inode, struct file *file)
2829 {
2830         file->private_data = inode->i_private;
2831         return 0;
2832 }
2833 static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
2834                              u32 *rate_n_flags, int index)
2835 {
2836         struct iwl_priv *priv;
2837         u8 valid_tx_ant;
2838         u8 ant_sel_tx;
2839 
2840         priv = lq_sta->drv;
2841         valid_tx_ant = priv->hw_params.valid_tx_ant;
2842         if (lq_sta->dbg_fixed_rate) {
2843                 ant_sel_tx =
2844                   ((lq_sta->dbg_fixed_rate & RATE_MCS_ANT_ABC_MSK)
2845                   >> RATE_MCS_ANT_POS);
2846                 if ((valid_tx_ant & ant_sel_tx) == ant_sel_tx) {
2847                         *rate_n_flags = lq_sta->dbg_fixed_rate;
2848                         IWL_DEBUG_RATE(priv, "Fixed rate ON\n");
2849                 } else {
2850                         lq_sta->dbg_fixed_rate = 0;
2851                         IWL_ERR(priv,
2852                             "Invalid antenna selection 0x%X, Valid is 0x%X\n",
2853                             ant_sel_tx, valid_tx_ant);
2854                         IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
2855                 }
2856         } else {
2857                 IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
2858         }
2859 }
2860 
2861 static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
2862                         const char __user *user_buf, size_t count, loff_t *ppos)
2863 {
2864         struct iwl_lq_sta *lq_sta = file->private_data;
2865         struct iwl_priv *priv;
2866         char buf[64];
2867         int buf_size;
2868         u32 parsed_rate;
2869 
2870         priv = lq_sta->drv;
2871         memset(buf, 0, sizeof(buf));
2872         buf_size = min(count, sizeof(buf) -  1);
2873         if (copy_from_user(buf, user_buf, buf_size))
2874                 return -EFAULT;
2875 
2876         if (sscanf(buf, "%x", &parsed_rate) == 1)
2877                 lq_sta->dbg_fixed_rate = parsed_rate;
2878         else
2879                 lq_sta->dbg_fixed_rate = 0;
2880 
2881         lq_sta->active_legacy_rate = 0x0FFF;    /* 1 - 54 MBits, includes CCK */
2882         lq_sta->active_siso_rate   = 0x1FD0;    /* 6 - 60 MBits, no 9, no CCK */
2883         lq_sta->active_mimo2_rate  = 0x1FD0;    /* 6 - 60 MBits, no 9, no CCK */
2884         lq_sta->active_mimo3_rate  = 0x1FD0;    /* 6 - 60 MBits, no 9, no CCK */
2885 
2886         IWL_DEBUG_RATE(priv, "sta_id %d rate 0x%X\n",
2887                 lq_sta->lq.sta_id, lq_sta->dbg_fixed_rate);
2888 
2889         if (lq_sta->dbg_fixed_rate) {
2890                 rs_fill_link_cmd(NULL, lq_sta, lq_sta->dbg_fixed_rate);
2891                 iwl_send_lq_cmd(lq_sta->drv, &lq_sta->lq, CMD_ASYNC);
2892         }
2893 
2894         return count;
2895 }
2896 
2897 static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
2898                         char __user *user_buf, size_t count, loff_t *ppos)
2899 {
2900         char *buff;
2901         int desc = 0;
2902         int i = 0;
2903         int index = 0;
2904         ssize_t ret;
2905 
2906         struct iwl_lq_sta *lq_sta = file->private_data;
2907         struct iwl_priv *priv;
2908         struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
2909 
2910         priv = lq_sta->drv;
2911         buff = kmalloc(1024, GFP_KERNEL);
2912         if (!buff)
2913                 return -ENOMEM;
2914 
2915         desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
2916         desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
2917                         lq_sta->total_failed, lq_sta->total_success,
2918                         lq_sta->active_legacy_rate);
2919         desc += sprintf(buff+desc, "fixed rate 0x%X\n",
2920                         lq_sta->dbg_fixed_rate);
2921         desc += sprintf(buff+desc, "valid_tx_ant %s%s%s\n",
2922             (priv->hw_params.valid_tx_ant & ANT_A) ? "ANT_A," : "",
2923             (priv->hw_params.valid_tx_ant & ANT_B) ? "ANT_B," : "",
2924             (priv->hw_params.valid_tx_ant & ANT_C) ? "ANT_C" : "");
2925         desc += sprintf(buff+desc, "lq type %s\n",
2926            (is_legacy(tbl->lq_type)) ? "legacy" : "HT");
2927         if (is_Ht(tbl->lq_type)) {
2928                 desc += sprintf(buff+desc, " %s",
2929                    (is_siso(tbl->lq_type)) ? "SISO" :
2930                    ((is_mimo2(tbl->lq_type)) ? "MIMO2" : "MIMO3"));
2931                    desc += sprintf(buff+desc, " %s",
2932                    (tbl->is_fat) ? "40MHz" : "20MHz");
2933                    desc += sprintf(buff+desc, " %s %s\n", (tbl->is_SGI) ? "SGI" : "",
2934                    (lq_sta->is_green) ? "GF enabled" : "");
2935         }
2936         desc += sprintf(buff+desc, "last tx rate=0x%X\n",
2937                 lq_sta->last_rate_n_flags);
2938         desc += sprintf(buff+desc, "general:"
2939                 "flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
2940                 lq_sta->lq.general_params.flags,
2941                 lq_sta->lq.general_params.mimo_delimiter,
2942                 lq_sta->lq.general_params.single_stream_ant_msk,
2943                 lq_sta->lq.general_params.dual_stream_ant_msk);
2944 
2945         desc += sprintf(buff+desc, "agg:"
2946                         "time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
2947                         le16_to_cpu(lq_sta->lq.agg_params.agg_time_limit),
2948                         lq_sta->lq.agg_params.agg_dis_start_th,
2949                         lq_sta->lq.agg_params.agg_frame_cnt_limit);
2950 
2951         desc += sprintf(buff+desc,
2952                         "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
2953                         lq_sta->lq.general_params.start_rate_index[0],
2954                         lq_sta->lq.general_params.start_rate_index[1],
2955                         lq_sta->lq.general_params.start_rate_index[2],
2956                         lq_sta->lq.general_params.start_rate_index[3]);
2957 
2958         for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
2959                 index = iwl_hwrate_to_plcp_idx(
2960                         le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags));
2961                 if (is_legacy(tbl->lq_type)) {
2962                         desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps\n",
2963                                 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags),
2964                                 iwl_rate_mcs[index].mbps);
2965                 } else {
2966                         desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps (%s)\n",
2967                                 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags),
2968                                 iwl_rate_mcs[index].mbps, iwl_rate_mcs[index].mcs);
2969                 }
2970         }
2971 
2972         ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2973         kfree(buff);
2974         return ret;
2975 }
2976 
2977 static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
2978         .write = rs_sta_dbgfs_scale_table_write,
2979         .read = rs_sta_dbgfs_scale_table_read,
2980         .open = open_file_generic,
2981 };
2982 static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
2983                         char __user *user_buf, size_t count, loff_t *ppos)
2984 {
2985         char *buff;
2986         int desc = 0;
2987         int i, j;
2988         ssize_t ret;
2989 
2990         struct iwl_lq_sta *lq_sta = file->private_data;
2991 
2992         buff = kmalloc(1024, GFP_KERNEL);
2993         if (!buff)
2994                 return -ENOMEM;
2995 
2996         for (i = 0; i < LQ_SIZE; i++) {
2997                 desc += sprintf(buff+desc, "%s type=%d SGI=%d FAT=%d DUP=%d GF=%d\n"
2998                                 "rate=0x%X\n",
2999                                 lq_sta->active_tbl == i ? "*" : "x",
3000                                 lq_sta->lq_info[i].lq_type,
3001                                 lq_sta->lq_info[i].is_SGI,
3002                                 lq_sta->lq_info[i].is_fat,
3003                                 lq_sta->lq_info[i].is_dup,
3004                                 lq_sta->is_green,
3005                                 lq_sta->lq_info[i].current_rate);
3006                 for (j = 0; j < IWL_RATE_COUNT; j++) {
3007                         desc += sprintf(buff+desc,
3008                                 "counter=%d success=%d %%=%d\n",
3009                                 lq_sta->lq_info[i].win[j].counter,
3010                                 lq_sta->lq_info[i].win[j].success_counter,
3011                                 lq_sta->lq_info[i].win[j].success_ratio);
3012                 }
3013         }
3014         ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
3015         kfree(buff);
3016         return ret;
3017 }
3018 
3019 static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
3020         .read = rs_sta_dbgfs_stats_table_read,
3021         .open = open_file_generic,
3022 };
3023 
3024 static ssize_t rs_sta_dbgfs_rate_scale_data_read(struct file *file,
3025                         char __user *user_buf, size_t count, loff_t *ppos)
3026 {
3027         char buff[120];
3028         int desc = 0;
3029         ssize_t ret;
3030 
3031         struct iwl_lq_sta *lq_sta = file->private_data;
3032         struct iwl_priv *priv;
3033         struct iwl_scale_tbl_info *tbl = &lq_sta->lq_info[lq_sta->active_tbl];
3034 
3035         priv = lq_sta->drv;
3036 
3037         if (is_Ht(tbl->lq_type))
3038                 desc += sprintf(buff+desc,
3039                                 "Bit Rate= %d Mb/s\n",
3040                                 tbl->expected_tpt[lq_sta->last_txrate_idx]);
3041         else
3042                 desc += sprintf(buff+desc,
3043                                 "Bit Rate= %d Mb/s\n",
3044                                 iwl_rates[lq_sta->last_txrate_idx].ieee >> 1);
3045         desc += sprintf(buff+desc,
3046                         "Signal Level= %d dBm\tNoise Level= %d dBm\n",
3047                         priv->last_rx_rssi, priv->last_rx_noise);
3048         desc += sprintf(buff+desc,
3049                         "Tsf= 0x%llx\tBeacon time= 0x%08X\n",
3050                         priv->last_tsf, priv->last_beacon_time);
3051 
3052         ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
3053         return ret;
3054 }
3055 
3056 static const struct file_operations rs_sta_dbgfs_rate_scale_data_ops = {
3057         .read = rs_sta_dbgfs_rate_scale_data_read,
3058         .open = open_file_generic,
3059 };
3060 
3061 static void rs_add_debugfs(void *priv, void *priv_sta,
3062                                         struct dentry *dir)
3063 {
3064         struct iwl_lq_sta *lq_sta = priv_sta;
3065         lq_sta->rs_sta_dbgfs_scale_table_file =
3066                 debugfs_create_file("rate_scale_table", 0600, dir,
3067                                 lq_sta, &rs_sta_dbgfs_scale_table_ops);
3068         lq_sta->rs_sta_dbgfs_stats_table_file =
3069                 debugfs_create_file("rate_stats_table", 0600, dir,
3070                         lq_sta, &rs_sta_dbgfs_stats_table_ops);
3071         lq_sta->rs_sta_dbgfs_rate_scale_data_file =
3072                 debugfs_create_file("rate_scale_data", 0600, dir,
3073                         lq_sta, &rs_sta_dbgfs_rate_scale_data_ops);
3074         lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file =
3075                 debugfs_create_u8("tx_agg_tid_enable", 0600, dir,
3076                 &lq_sta->tx_agg_tid_en);
3077 
3078 }
3079 
3080 static void rs_remove_debugfs(void *priv, void *priv_sta)
3081 {
3082         struct iwl_lq_sta *lq_sta = priv_sta;
3083         debugfs_remove(lq_sta->rs_sta_dbgfs_scale_table_file);
3084         debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
3085         debugfs_remove(lq_sta->rs_sta_dbgfs_rate_scale_data_file);
3086         debugfs_remove(lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file);
3087 }
3088 #endif
3089 
3090 static struct rate_control_ops rs_ops = {
3091         .module = NULL,
3092         .name = RS_NAME,
3093         .tx_status = rs_tx_status,
3094         .get_rate = rs_get_rate,
3095         .rate_init = rs_rate_init,
3096         .alloc = rs_alloc,
3097         .free = rs_free,
3098         .alloc_sta = rs_alloc_sta,
3099         .free_sta = rs_free_sta,
3100 #ifdef CONFIG_MAC80211_DEBUGFS
3101         .add_sta_debugfs = rs_add_debugfs,
3102         .remove_sta_debugfs = rs_remove_debugfs,
3103 #endif
3104 };
3105 
3106 int iwlagn_rate_control_register(void)
3107 {
3108         return ieee80211_rate_control_register(&rs_ops);
3109 }
3110 
3111 void iwlagn_rate_control_unregister(void)
3112 {
3113         ieee80211_rate_control_unregister(&rs_ops);
3114 }
3115 
3116 
  This page was automatically generated by the LXR engine.