root/drivers/net/wireless/intel/iwlwifi/dvm/rs.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. rs_extract_rate
  2. iwl_hwrate_to_plcp_idx
  3. rs_dbgfs_set_mcs
  4. rs_rate_scale_clear_window
  5. rs_is_valid_ant
  6. rs_tl_rm_old_stats
  7. rs_tl_add_packet
  8. rs_program_fix_rate
  9. rs_tl_get_load
  10. rs_tl_turn_on_agg_for_tid
  11. rs_tl_turn_on_agg
  12. get_num_of_ant_from_rate
  13. get_expected_tpt
  14. rs_collect_tx_data
  15. rate_n_flags_from_tbl
  16. rs_get_tbl_info_from_mcs
  17. rs_toggle_antenna
  18. rs_use_green
  19. rs_get_supported_rates
  20. rs_get_adjacent_rate
  21. rs_get_lower_rate
  22. table_type_matches
  23. rs_bt_update_lq
  24. rs_tx_status
  25. rs_set_stay_in_table
  26. rs_set_expected_tpt_table
  27. rs_get_best_rate
  28. rs_switch_to_mimo2
  29. rs_switch_to_mimo3
  30. rs_switch_to_siso
  31. rs_move_legacy_other
  32. rs_move_siso_to_other
  33. rs_move_mimo2_to_other
  34. rs_move_mimo3_to_other
  35. rs_stay_in_table
  36. rs_update_rate_tbl
  37. rs_rate_scale_perform
  38. rs_initialize_lq
  39. rs_get_rate
  40. rs_alloc_sta
  41. iwl_rs_rate_init
  42. rs_fill_link_cmd
  43. rs_alloc
  44. rs_free
  45. rs_free_sta
  46. rs_dbgfs_set_mcs
  47. rs_sta_dbgfs_scale_table_write
  48. rs_sta_dbgfs_scale_table_read
  49. rs_sta_dbgfs_stats_table_read
  50. rs_sta_dbgfs_rate_scale_data_read
  51. rs_add_debugfs
  52. rs_rate_init_stub
  53. iwlagn_rate_control_register
  54. iwlagn_rate_control_unregister

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

/* [<][>][^][v][top][bottom][index][help] */