]> git.kernelconcepts.de Git - karo-tx-linux.git/blobdiff - drivers/net/wireless/iwlwifi/iwl-4965-rs.c
Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-2.6
[karo-tx-linux.git] / drivers / net / wireless / iwlwifi / iwl-4965-rs.c
index 7c55aa9a2ba90c0cfa32045ce4f6d43b19a12bca..24dee00b0e85a55674162a7267a6bd00787668b9 100644 (file)
 
 #include "../net/mac80211/rate.h"
 
-#include "iwl-4965.h"
+#include "iwl-dev.h"
 #include "iwl-core.h"
 #include "iwl-helpers.h"
 
 #define RS_NAME "iwl-4965-rs"
 
-#define NUM_TRY_BEFORE_ANTENNA_TOGGLE 1
+#define NUM_TRY_BEFORE_ANT_TOGGLE 1
 #define IWL_NUMBER_TRY      1
 #define IWL_HT_NUMBER_TRY   3
 
@@ -64,6 +64,17 @@ static u8 rs_ht_to_legacy[] = {
        IWL_RATE_48M_INDEX, IWL_RATE_54M_INDEX
 };
 
+static const u8 ant_toggle_lookup[] = {
+       /*ANT_NONE -> */ ANT_NONE,
+       /*ANT_A    -> */ ANT_B,
+       /*ANT_B    -> */ ANT_C,
+       /*ANT_AB   -> */ ANT_BC,
+       /*ANT_C    -> */ ANT_A,
+       /*ANT_AC   -> */ ANT_AB,
+       /*ANT_BC   -> */ ANT_AC,
+       /*ANT_ABC  -> */ ANT_ABC,
+};
+
 /**
  * struct iwl4965_rate_scale_data -- tx success history for one rate
  */
@@ -131,8 +142,6 @@ struct iwl4965_lq_sta {
        u32 flush_timer;        /* time staying in mode before new search */
 
        u8 action_counter;      /* # mode-switch actions tried */
-       u8 antenna;
-       u8 valid_antenna;
        u8 is_green;
        u8 is_dup;
        enum ieee80211_band band;
@@ -159,8 +168,8 @@ struct iwl4965_lq_sta {
        struct dentry *rs_sta_dbgfs_tx_agg_tid_en_file;
 #endif
        u32 dbg_fixed_rate;
-       struct iwl_priv *drv;
 #endif
+       struct iwl_priv *drv;
 };
 
 static void rs_rate_scale_perform(struct iwl_priv *priv,
@@ -169,8 +178,7 @@ static void rs_rate_scale_perform(struct iwl_priv *priv,
                                   struct sta_info *sta);
 static void rs_fill_link_cmd(const struct iwl_priv *priv,
                             struct iwl4965_lq_sta *lq_sta,
-                            u32 rate_n_flags,
-                            struct iwl_link_quality_cmd *tbl);
+                            u32 rate_n_flags);
 
 
 #ifdef CONFIG_MAC80211_DEBUGFS
@@ -243,6 +251,11 @@ static void rs_rate_scale_clear_window(struct iwl4965_rate_scale_data *window)
        window->stamp = 0;
 }
 
+static inline u8 rs_is_valid_ant(u8 valid_antenna, u8 ant_type)
+{
+       return ((ant_type & valid_antenna) == ant_type);
+}
+
 #ifdef CONFIG_IWL4965_HT
 /*
  *     removes the old data from the statistics. All data that is older than
@@ -576,26 +589,51 @@ static int rs_get_tbl_info_from_mcs(const u32 rate_n_flags,
        }
        return 0;
 }
-/* FIXME:RS: need to toggle also ANT_C, and also AB,AC,BC */
-static inline void rs_toggle_antenna(u32 *rate_n_flags,
-                                    struct iwl4965_scale_tbl_info *tbl)
+
+/* switch to another antenna/antennas and return 1 */
+/* if no other valid antenna found, return 0 */
+static int rs_toggle_antenna(u32 valid_ant, u32 *rate_n_flags,
+                             struct iwl4965_scale_tbl_info *tbl)
 {
-       tbl->ant_type ^= ANT_AB;
-       *rate_n_flags ^= (RATE_MCS_ANT_A_MSK|RATE_MCS_ANT_B_MSK);
+       u8 new_ant_type;
+
+       if (!tbl->ant_type || tbl->ant_type > ANT_ABC)
+               return 0;
+
+       if (!rs_is_valid_ant(valid_ant, tbl->ant_type))
+               return 0;
+
+       new_ant_type = ant_toggle_lookup[tbl->ant_type];
+
+       while ((new_ant_type != tbl->ant_type) &&
+              !rs_is_valid_ant(valid_ant, new_ant_type))
+               new_ant_type = ant_toggle_lookup[new_ant_type];
+
+       if (new_ant_type == tbl->ant_type)
+               return 0;
+
+       tbl->ant_type = new_ant_type;
+       *rate_n_flags &= ~RATE_MCS_ANT_ABC_MSK;
+       *rate_n_flags |= new_ant_type << RATE_MCS_ANT_POS;
+       return 1;
 }
 
 /* FIXME:RS: in 4965 we don't use greenfield at all */
-static inline u8 rs_use_green(struct iwl_priv *priv,
-                             struct ieee80211_conf *conf)
+/* FIXME:RS: don't use greenfield for now in TX */
+/* #ifdef CONFIG_IWL4965_HT */
+#if 0
+static inline u8 rs_use_green(struct iwl_priv *priv, struct ieee80211_conf *conf)
 {
-#ifdef CONFIG_IWL4965_HT
        return ((conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) &&
                priv->current_ht_config.is_green_field &&
                !priv->current_ht_config.non_GF_STA_present);
+}
 #else
+static inline u8 rs_use_green(struct iwl_priv *priv, struct ieee80211_conf *conf)
+{
        return 0;
-#endif /* CONFIG_IWL4965_HT */
 }
+#endif /* CONFIG_IWL4965_HT */
 
 /**
  * rs_get_supported_rates - get the available rates
@@ -604,25 +642,23 @@ static inline u8 rs_use_green(struct iwl_priv *priv,
  * basic available rates.
  *
  */
-static void rs_get_supported_rates(struct iwl4965_lq_sta *lq_sta,
+static u16 rs_get_supported_rates(struct iwl4965_lq_sta *lq_sta,
                                   struct ieee80211_hdr *hdr,
-                                  enum iwl_table_type rate_type,
-                                  u16 *data_rate)
+                                  enum iwl_table_type rate_type)
 {
-       if (is_legacy(rate_type))
-               *data_rate = lq_sta->active_legacy_rate;
-       else {
+       if (hdr && is_multicast_ether_addr(hdr->addr1) &&
+           lq_sta->active_rate_basic)
+               return lq_sta->active_rate_basic;
+
+       if (is_legacy(rate_type)) {
+               return lq_sta->active_legacy_rate;
+       } else {
                if (is_siso(rate_type))
-                       *data_rate = lq_sta->active_siso_rate;
+                       return lq_sta->active_siso_rate;
                else if (is_mimo2(rate_type))
-                       *data_rate = lq_sta->active_mimo2_rate;
+                       return lq_sta->active_mimo2_rate;
                else
-                       *data_rate = lq_sta->active_mimo3_rate;
-       }
-
-       if (hdr && is_multicast_ether_addr(hdr->addr1) &&
-           lq_sta->active_rate_basic) {
-               *data_rate = lq_sta->active_rate_basic;
+                       return lq_sta->active_mimo3_rate;
        }
 }
 
@@ -702,14 +738,14 @@ static u32 rs_get_lower_rate(struct iwl4965_lq_sta *lq_sta,
                else
                        tbl->lq_type = LQ_G;
 
-               if (num_of_ant(tbl->ant_type > 1))
+               if (num_of_ant(tbl->ant_type) > 1)
                        tbl->ant_type = ANT_A;/*FIXME:RS*/
 
                tbl->is_fat = 0;
                tbl->is_SGI = 0;
        }
 
-       rs_get_supported_rates(lq_sta, NULL, tbl->lq_type, &rate_mask);
+       rate_mask = rs_get_supported_rates(lq_sta, NULL, tbl->lq_type);
 
        /* Mask with station rate restriction */
        if (is_legacy(tbl->lq_type)) {
@@ -798,8 +834,6 @@ static void rs_tx_status(void *priv_rate, struct net_device *dev,
        table = &lq_sta->lq;
        active_index = lq_sta->active_tbl;
 
-       lq_sta->antenna = lq_sta->valid_antenna;
-
        curr_tbl = &(lq_sta->lq_info[active_index]);
        search_tbl = &(lq_sta->lq_info[(1 - active_index)]);
        window = (struct iwl4965_rate_scale_data *)
@@ -948,22 +982,6 @@ out:
        return;
 }
 
-static inline u8 rs_is_ant_connected(u8 valid_antenna, u8 ant_type)
-{
-       return ((ant_type & valid_antenna) == ant_type);
-}
-
-/*FIXME:RS: this function should be replaced*/
-static u8 rs_is_other_ant_connected(u8 valid_antenna, u8 ant_type)
-{
-       if (ant_type == ANT_B)
-               return rs_is_ant_connected(valid_antenna, ANT_A);
-       else
-               return rs_is_ant_connected(valid_antenna, ANT_B);
-
-       return 0;
-}
-
 /*
  * Begin a period of staying with a selected modulation mode.
  * Set "stay_in_tbl" flag to prevent any mode switches.
@@ -975,7 +993,7 @@ static u8 rs_is_other_ant_connected(u8 valid_antenna, u8 ant_type)
 static void rs_set_stay_in_table(u8 is_legacy,
                                 struct iwl4965_lq_sta *lq_sta)
 {
-       IWL_DEBUG_HT("we are staying in the same table\n");
+       IWL_DEBUG_RATE("we are staying in the same table\n");
        lq_sta->stay_in_tbl = 1;        /* only place this gets set */
        if (is_legacy) {
                lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT;
@@ -994,7 +1012,7 @@ static void rs_set_stay_in_table(u8 is_legacy,
 /*
  * Find correct throughput table for given mode of modulation
  */
-static void rs_get_expected_tpt_table(struct iwl4965_lq_sta *lq_sta,
+static void rs_set_expected_tpt_table(struct iwl4965_lq_sta *lq_sta,
                                      struct iwl4965_scale_tbl_info *tbl)
 {
        if (is_legacy(tbl->lq_type)) {
@@ -1043,7 +1061,7 @@ static void rs_get_expected_tpt_table(struct iwl4965_lq_sta *lq_sta,
 static s32 rs_get_best_rate(struct iwl_priv *priv,
                            struct iwl4965_lq_sta *lq_sta,
                            struct iwl4965_scale_tbl_info *tbl, /* "search" */
-                           u16 rate_mask, s8 index, s8 rate)
+                           u16 rate_mask, s8 index)
 {
        /* "active" values */
        struct iwl4965_scale_tbl_info *active_tbl =
@@ -1056,6 +1074,7 @@ static s32 rs_get_best_rate(struct iwl_priv *priv,
 
        s32 new_rate, high, low, start_hi;
        u16 high_low;
+       s8 rate = index;
 
        new_rate = high = low = start_hi = IWL_RATE_INVALID;
 
@@ -1130,12 +1149,6 @@ static s32 rs_get_best_rate(struct iwl_priv *priv,
 }
 #endif                         /* CONFIG_IWL4965_HT */
 
-/*FIXME:RS:this function should be replaced*/
-static inline u8 rs_is_both_ant_supp(u8 valid_antenna)
-{
-       return (rs_is_ant_connected(valid_antenna, ANT_AB));
-}
-
 /*
  * Set up search table for MIMO
  */
@@ -1158,10 +1171,10 @@ static int rs_switch_to_mimo2(struct iwl_priv *priv,
                return -1;
 
        /* Need both Tx chains/antennas to support MIMO */
-       if (!rs_is_both_ant_supp(priv->hw_params.valid_tx_ant))
+       if (priv->hw_params.tx_chains_num < 2)
                return -1;
 
-       IWL_DEBUG_HT("LQ: try to switch to MIMO2\n");
+       IWL_DEBUG_RATE("LQ: try to switch to MIMO2\n");
 
        tbl->lq_type = LQ_MIMO2;
        tbl->is_dup = lq_sta->is_dup;
@@ -1186,20 +1199,20 @@ static int rs_switch_to_mimo2(struct iwl_priv *priv,
                tbl->is_SGI = 0;
        */
 
-       rs_get_expected_tpt_table(lq_sta, tbl);
+       rs_set_expected_tpt_table(lq_sta, tbl);
 
-       rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index, index);
+       rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
 
-       IWL_DEBUG_HT("LQ: MIMO2 best rate %d mask %X\n", rate, rate_mask);
+       IWL_DEBUG_RATE("LQ: MIMO2 best rate %d mask %X\n", rate, rate_mask);
 
        if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
-               IWL_DEBUG_HT("Can't switch with index %d rate mask %x\n",
+               IWL_DEBUG_RATE("Can't switch with index %d rate mask %x\n",
                                                rate, rate_mask);
                return -1;
        }
        tbl->current_rate = rate_n_flags_from_tbl(tbl, rate, is_green);
 
-       IWL_DEBUG_HT("LQ: Switch to new mcs %X index is green %X\n",
+       IWL_DEBUG_RATE("LQ: Switch to new mcs %X index is green %X\n",
                     tbl->current_rate, is_green);
        return 0;
 }
@@ -1232,7 +1245,7 @@ static int rs_switch_to_siso(struct iwl_priv *priv,
            !sta->ht_info.ht_supported)
                return -1;
 
-       IWL_DEBUG_HT("LQ: try to switch to SISO\n");
+       IWL_DEBUG_RATE("LQ: try to switch to SISO\n");
 
        tbl->is_dup = lq_sta->is_dup;
        tbl->lq_type = LQ_SISO;
@@ -1260,22 +1273,21 @@ static int rs_switch_to_siso(struct iwl_priv *priv,
        if (is_green)
                tbl->is_SGI = 0; /*11n spec: no SGI in SISO+Greenfield*/
 
-       rs_get_expected_tpt_table(lq_sta, tbl);
-       rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index, index);
+       rs_set_expected_tpt_table(lq_sta, tbl);
+       rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
 
-       IWL_DEBUG_HT("LQ: get best rate %d mask %X\n", rate, rate_mask);
+       IWL_DEBUG_RATE("LQ: get best rate %d mask %X\n", rate, rate_mask);
        if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
-               IWL_DEBUG_HT("can not switch with index %d rate mask %x\n",
+               IWL_DEBUG_RATE("can not switch with index %d rate mask %x\n",
                             rate, rate_mask);
                return -1;
        }
        tbl->current_rate = rate_n_flags_from_tbl(tbl, rate, is_green);
-       IWL_DEBUG_HT("LQ: Switch to new mcs %X index is green %X\n",
+       IWL_DEBUG_RATE("LQ: Switch to new mcs %X index is green %X\n",
                     tbl->current_rate, is_green);
        return 0;
 #else
        return -1;
-
 #endif /*CONFIG_IWL4965_HT */
 }
 
@@ -1302,32 +1314,25 @@ static int rs_move_legacy_other(struct iwl_priv *priv,
        for (; ;) {
                switch (tbl->action) {
                case IWL_LEGACY_SWITCH_ANTENNA:
-                       IWL_DEBUG_HT("LQ Legacy toggle Antenna\n");
+                       IWL_DEBUG_RATE("LQ: Legacy toggle Antenna\n");
 
-                       search_tbl->lq_type = LQ_NONE;
                        lq_sta->action_counter++;
 
                        /* Don't change antenna if success has been great */
-                       /*FIXME:RS:not sure this is really needed*/
                        if (window->success_ratio >= IWL_RS_GOOD_RATIO)
                                break;
 
-                       /* Don't change antenna if other one is not connected */
-                       if (!rs_is_other_ant_connected(valid_tx_ant,
-                                                       tbl->ant_type))
-                               break;
-
                        /* Set up search table to try other antenna */
                        memcpy(search_tbl, tbl, sz);
 
-                       rs_toggle_antenna(&search_tbl->current_rate,
-                                          search_tbl);
-                       rs_get_expected_tpt_table(lq_sta, search_tbl);
-                       lq_sta->search_better_tbl = 1;
-                       goto out;
+                       if (rs_toggle_antenna(valid_tx_ant,
+                               &search_tbl->current_rate, search_tbl)) {
+                               lq_sta->search_better_tbl = 1;
+                               goto out;
+                       }
 
                case IWL_LEGACY_SWITCH_SISO:
-                       IWL_DEBUG_HT("LQ: Legacy switch to SISO\n");
+                       IWL_DEBUG_RATE("LQ: Legacy switch to SISO\n");
 
                        /* Set up search table to try SISO */
                        memcpy(search_tbl, tbl, sz);
@@ -1342,7 +1347,7 @@ static int rs_move_legacy_other(struct iwl_priv *priv,
 
                        break;
                case IWL_LEGACY_SWITCH_MIMO2:
-                       IWL_DEBUG_HT("LQ: Legacy switch to MIMO2\n");
+                       IWL_DEBUG_RATE("LQ: Legacy switch to MIMO2\n");
 
                        /* Set up search table to try MIMO */
                        memcpy(search_tbl, tbl, sz);
@@ -1401,23 +1406,19 @@ static int rs_move_siso_to_other(struct iwl_priv *priv,
                lq_sta->action_counter++;
                switch (tbl->action) {
                case IWL_SISO_SWITCH_ANTENNA:
-                       IWL_DEBUG_HT("LQ: SISO toggle Antenna\n");
-                       /*FIXME:RS: is this really needed for SISO?*/
+                       IWL_DEBUG_RATE("LQ: SISO toggle Antenna\n");
                        if (window->success_ratio >= IWL_RS_GOOD_RATIO)
                                break;
-                       if (!rs_is_other_ant_connected(valid_tx_ant,
-                                                      tbl->ant_type))
-                               break;
 
                        memcpy(search_tbl, tbl, sz);
-                       rs_toggle_antenna(&search_tbl->current_rate,
-                                          search_tbl);
-                       lq_sta->search_better_tbl = 1;
-
-                       goto out;
+                       if (rs_toggle_antenna(valid_tx_ant,
+                                      &search_tbl->current_rate, search_tbl)) {
+                               lq_sta->search_better_tbl = 1;
+                               goto out;
+                       }
 
                case IWL_SISO_SWITCH_MIMO2:
-                       IWL_DEBUG_HT("LQ: SISO switch to MIMO\n");
+                       IWL_DEBUG_RATE("LQ: SISO switch to MIMO\n");
                        memcpy(search_tbl, tbl, sz);
                        search_tbl->is_SGI = 0;
                        search_tbl->ant_type = ANT_AB; /*FIXME:RS*/
@@ -1429,7 +1430,7 @@ static int rs_move_siso_to_other(struct iwl_priv *priv,
                        }
                        break;
                case IWL_SISO_SWITCH_GI:
-                       IWL_DEBUG_HT("LQ: SISO toggle SGI/NGI\n");
+                       IWL_DEBUG_RATE("LQ: SISO toggle SGI/NGI\n");
 
                        memcpy(search_tbl, tbl, sz);
                        if (is_green) {
@@ -1439,7 +1440,7 @@ static int rs_move_siso_to_other(struct iwl_priv *priv,
                                        IWL_ERROR("SGI was set in GF+SISO\n");
                        }
                        search_tbl->is_SGI = !tbl->is_SGI;
-                       rs_get_expected_tpt_table(lq_sta, search_tbl);
+                       rs_set_expected_tpt_table(lq_sta, search_tbl);
                        if (tbl->is_SGI) {
                                s32 tpt = lq_sta->last_tpt / 100;
                                if (tpt >= search_tbl->expected_tpt[index])
@@ -1475,7 +1476,6 @@ static int rs_move_mimo_to_other(struct iwl_priv *priv,
                                 struct sta_info *sta,
                                 int index)
 {
-       int ret;
        s8 is_green = lq_sta->is_green;
        struct iwl4965_scale_tbl_info *tbl =
            &(lq_sta->lq_info[lq_sta->active_tbl]);
@@ -1484,13 +1484,15 @@ static int rs_move_mimo_to_other(struct iwl_priv *priv,
        u32 sz = (sizeof(struct iwl4965_scale_tbl_info) -
                  (sizeof(struct iwl4965_rate_scale_data) * IWL_RATE_COUNT));
        u8 start_action = tbl->action;
+       /*u8 valid_tx_ant = priv->hw_params.valid_tx_ant;*/
+       int ret;
 
        for (;;) {
                lq_sta->action_counter++;
                switch (tbl->action) {
                case IWL_MIMO_SWITCH_ANTENNA_A:
                case IWL_MIMO_SWITCH_ANTENNA_B:
-                       IWL_DEBUG_HT("LQ: MIMO2 switch to SISO\n");
+                       IWL_DEBUG_RATE("LQ: MIMO2 switch to SISO\n");
 
                        /* Set up new search table for SISO */
                        memcpy(search_tbl, tbl, sz);
@@ -1510,12 +1512,12 @@ static int rs_move_mimo_to_other(struct iwl_priv *priv,
                        break;
 
                case IWL_MIMO_SWITCH_GI:
-                       IWL_DEBUG_HT("LQ: MIMO toggle SGI/NGI\n");
+                       IWL_DEBUG_RATE("LQ: MIMO toggle SGI/NGI\n");
 
                        /* Set up new search table for MIMO */
                        memcpy(search_tbl, tbl, sz);
                        search_tbl->is_SGI = !tbl->is_SGI;
-                       rs_get_expected_tpt_table(lq_sta, search_tbl);
+                       rs_set_expected_tpt_table(lq_sta, search_tbl);
                        /*
                         * If active table already uses the fastest possible
                         * modulation (dual stream with short guard interval),
@@ -1578,9 +1580,6 @@ static void rs_stay_in_table(struct iwl4965_lq_sta *lq_sta)
                                       (unsigned long)(lq_sta->flush_timer +
                                        IWL_RATE_SCALE_FLUSH_INTVL));
 
-               /* For now, disable the elapsed time criterion */
-               flush_interval_passed = 0;
-
                /*
                 * Check if we should allow search for new modulation mode.
                 * If many frames have failed or succeeded, or we've used
@@ -1593,7 +1592,7 @@ static void rs_stay_in_table(struct iwl4965_lq_sta *lq_sta)
                    (lq_sta->total_success > lq_sta->max_success_limit) ||
                    ((!lq_sta->search_better_tbl) && (lq_sta->flush_timer)
                     && (flush_interval_passed))) {
-                       IWL_DEBUG_HT("LQ: stay is expired %d %d %d\n:",
+                       IWL_DEBUG_RATE("LQ: stay is expired %d %d %d\n:",
                                     lq_sta->total_failed,
                                     lq_sta->total_success,
                                     flush_interval_passed);
@@ -1616,7 +1615,7 @@ static void rs_stay_in_table(struct iwl4965_lq_sta *lq_sta)
                            lq_sta->table_count_limit) {
                                lq_sta->table_count = 0;
 
-                               IWL_DEBUG_HT("LQ: stay in table clear win\n");
+                               IWL_DEBUG_RATE("LQ: stay in table clear win\n");
                                for (i = 0; i < IWL_RATE_COUNT; i++)
                                        rs_rate_scale_clear_window(
                                                &(tbl->win[i]));
@@ -1715,8 +1714,7 @@ static void rs_rate_scale_perform(struct iwl_priv *priv,
                       tbl->lq_type);
 
        /* rates available for this association, and for modulation mode */
-       rs_get_supported_rates(lq_sta, hdr, tbl->lq_type,
-                               &rate_mask);
+       rate_mask = rs_get_supported_rates(lq_sta, hdr, tbl->lq_type);
 
        IWL_DEBUG_RATE("mask 0x%04X \n", rate_mask);
 
@@ -1736,27 +1734,16 @@ static void rs_rate_scale_perform(struct iwl_priv *priv,
        if (!rate_scale_index_msk)
                rate_scale_index_msk = rate_mask;
 
-       /* If current rate is no longer supported on current association,
-        * or user changed preferences for rates, find a new supported rate. */
-       if (index < 0 || !((1 << index) & rate_scale_index_msk)) {
-               index = IWL_INVALID_VALUE;
-               update_lq = 1;
-
-               /* get the highest available rate */
-               for (i = 0; i <= IWL_RATE_COUNT; i++) {
-                       if ((1 << i) & rate_scale_index_msk)
-                               index = i;
-               }
-
-               if (index == IWL_INVALID_VALUE) {
-                       IWL_WARNING("Can not find a suitable rate\n");
-                       return;
-               }
+       if (!((1 << index) & rate_scale_index_msk)) {
+               IWL_ERROR("Current Rate is not valid\n");
+               return;
        }
 
        /* Get expected throughput table and history window for current rate */
-       if (!tbl->expected_tpt)
-               rs_get_expected_tpt_table(lq_sta, tbl);
+       if (!tbl->expected_tpt) {
+               IWL_ERROR("tbl->expected_tpt is NULL\n");
+               return;
+       }
 
        window = &(tbl->win[index]);
 
@@ -1768,10 +1755,9 @@ static void rs_rate_scale_perform(struct iwl_priv *priv,
         * in current association (use new rate found above).
         */
        fail_count = window->counter - window->success_counter;
-       if (((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
-            (window->success_counter < IWL_RATE_MIN_SUCCESS_TH))
-           || (tbl->expected_tpt == NULL)) {
-               IWL_DEBUG_RATE("LQ: still below TH succ %d total %d "
+       if ((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
+                       (window->success_counter < IWL_RATE_MIN_SUCCESS_TH)) {
+               IWL_DEBUG_RATE("LQ: still below TH. succ=%d total=%d "
                               "for index %d\n",
                               window->success_counter, window->counter, index);
 
@@ -1782,44 +1768,51 @@ static void rs_rate_scale_perform(struct iwl_priv *priv,
                 * or search for a new one? */
                rs_stay_in_table(lq_sta);
 
-               /* Set up new rate table in uCode, if needed */
-               if (update_lq) {
-                       rate = rate_n_flags_from_tbl(tbl, index, is_green);
-                       rs_fill_link_cmd(priv, lq_sta, rate, &lq_sta->lq);
-                       iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
-               }
                goto out;
 
        /* Else we have enough samples; calculate estimate of
         * actual average throughput */
-       } else
-               window->average_tpt = ((window->success_ratio *
+       } else {
+               /*FIXME:RS remove this else if we don't get this error*/
+               if (window->average_tpt != ((window->success_ratio *
+                               tbl->expected_tpt[index] + 64) / 128)) {
+                       IWL_ERROR("expected_tpt should have been calculated"
+                                                               " by now\n");
+                       window->average_tpt = ((window->success_ratio *
                                        tbl->expected_tpt[index] + 64) / 128);
+               }
+       }
 
        /* If we are searching for better modulation mode, check success. */
        if (lq_sta->search_better_tbl) {
-               int success_limit = IWL_RATE_SCALE_SWITCH;
 
                /* If good success, continue using the "search" mode;
                 * no need to send new link quality command, since we're
                 * continuing to use the setup that we've been trying. */
-               if ((window->success_ratio > success_limit) ||
-                   (window->average_tpt > lq_sta->last_tpt)) {
-                       if (!is_legacy(tbl->lq_type)) {
-                               IWL_DEBUG_HT("LQ: we are switching to HT"
-                                            " rate suc %d current tpt %d"
-                                            " old tpt %d\n",
-                                            window->success_ratio,
-                                            window->average_tpt,
-                                            lq_sta->last_tpt);
+               if (window->average_tpt > lq_sta->last_tpt) {
+
+                       IWL_DEBUG_RATE("LQ: SWITCHING TO CURRENT TABLE "
+                                       "suc=%d cur-tpt=%d old-tpt=%d\n",
+                                       window->success_ratio,
+                                       window->average_tpt,
+                                       lq_sta->last_tpt);
+
+                       if (!is_legacy(tbl->lq_type))
                                lq_sta->enable_counter = 1;
-                       }
+
                        /* Swap tables; "search" becomes "active" */
                        lq_sta->active_tbl = active_tbl;
                        current_tpt = window->average_tpt;
 
                /* Else poor success; go back to mode in "active" table */
                } else {
+
+                       IWL_DEBUG_RATE("LQ: GOING BACK TO THE OLD TABLE "
+                                       "suc=%d cur-tpt=%d old-tpt=%d\n",
+                                       window->success_ratio,
+                                       window->average_tpt,
+                                       lq_sta->last_tpt);
+
                        /* Nullify "search" table */
                        tbl->lq_type = LQ_NONE;
 
@@ -1834,7 +1827,6 @@ static void rs_rate_scale_perform(struct iwl_priv *priv,
 
                        /* Need to set up a new rate table in uCode */
                        update_lq = 1;
-                       IWL_DEBUG_HT("XXY GO BACK TO OLD TABLE\n");
                }
 
                /* Either way, we've made a decision; modulation mode
@@ -1942,7 +1934,7 @@ static void rs_rate_scale_perform(struct iwl_priv *priv,
                break;
        }
 
-       IWL_DEBUG_HT("choose rate scale index %d action %d low %d "
+       IWL_DEBUG_RATE("choose rate scale index %d action %d low %d "
                    "high %d type %d\n",
                     index, scale_action, low, high, tbl->lq_type);
 
@@ -1950,7 +1942,7 @@ static void rs_rate_scale_perform(struct iwl_priv *priv,
        /* Replace uCode's rate table for the destination station. */
        if (update_lq) {
                rate = rate_n_flags_from_tbl(tbl, index, is_green);
-               rs_fill_link_cmd(priv, lq_sta, rate, &lq_sta->lq);
+               rs_fill_link_cmd(priv, lq_sta, rate);
                iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
        }
 
@@ -1987,10 +1979,9 @@ static void rs_rate_scale_perform(struct iwl_priv *priv,
                        index = iwl4965_hwrate_to_plcp_idx(
                                                        tbl->current_rate);
 
-                       IWL_DEBUG_HT("Switch current  mcs: %X index: %d\n",
+                       IWL_DEBUG_RATE("Switch current  mcs: %X index: %d\n",
                                     tbl->current_rate, index);
-                       rs_fill_link_cmd(priv, lq_sta, tbl->current_rate,
-                                        &lq_sta->lq);
+                       rs_fill_link_cmd(priv, lq_sta, tbl->current_rate);
                        iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
                }
 
@@ -2006,7 +1997,7 @@ static void rs_rate_scale_perform(struct iwl_priv *priv,
 #endif
                    (lq_sta->action_counter >= 1)) {
                        lq_sta->action_counter = 0;
-                       IWL_DEBUG_HT("LQ: STAY in legacy table\n");
+                       IWL_DEBUG_RATE("LQ: STAY in legacy table\n");
                        rs_set_stay_in_table(1, lq_sta);
                }
 
@@ -2019,7 +2010,7 @@ static void rs_rate_scale_perform(struct iwl_priv *priv,
                        if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
                            (lq_sta->tx_agg_tid_en & (1 << tid)) &&
                            (tid != MAX_TID_COUNT)) {
-                               IWL_DEBUG_HT("try to aggregate tid %d\n", tid);
+                               IWL_DEBUG_RATE("try to aggregate tid %d\n", tid);
                                rs_tl_turn_on_agg(priv, tid, lq_sta, sta);
                        }
 #endif /*CONFIG_IWL4965_HT */
@@ -2060,13 +2051,14 @@ static void rs_initialize_lq(struct iwl_priv *priv,
                             struct ieee80211_conf *conf,
                             struct sta_info *sta)
 {
-       int i;
        struct iwl4965_lq_sta *lq_sta;
        struct iwl4965_scale_tbl_info *tbl;
-       u8 active_tbl = 0;
        int rate_idx;
-       u8 use_green = rs_use_green(priv, conf);
+       int i;
        u32 rate;
+       u8 use_green = rs_use_green(priv, conf);
+       u8 active_tbl = 0;
+       u8 valid_tx_ant;
 
        if (!sta || !sta->rate_ctrl_priv)
                goto out;
@@ -2078,6 +2070,8 @@ static void rs_initialize_lq(struct iwl_priv *priv,
            (priv->iw_mode == IEEE80211_IF_TYPE_IBSS))
                goto out;
 
+       valid_tx_ant = priv->hw_params.valid_tx_ant;
+
        if (!lq_sta->search_better_tbl)
                active_tbl = lq_sta->active_tbl;
        else
@@ -2098,13 +2092,13 @@ static void rs_initialize_lq(struct iwl_priv *priv,
 
        tbl->ant_type = ANT_B;
        rs_get_tbl_info_from_mcs(rate, priv->band, tbl, &rate_idx);
-       if (!rs_is_ant_connected(priv->hw_params.valid_tx_ant, tbl->ant_type))
-           rs_toggle_antenna(&rate, tbl);
+       if (!rs_is_valid_ant(valid_tx_ant, tbl->ant_type))
+           rs_toggle_antenna(valid_tx_ant, &rate, tbl);
 
        rate = rate_n_flags_from_tbl(tbl, rate_idx, use_green);
        tbl->current_rate = rate;
-       rs_get_expected_tpt_table(lq_sta, tbl);
-       rs_fill_link_cmd(NULL, lq_sta, rate, &lq_sta->lq);
+       rs_set_expected_tpt_table(lq_sta, tbl);
+       rs_fill_link_cmd(NULL, lq_sta, rate);
        iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
  out:
        return;
@@ -2215,7 +2209,7 @@ static void rs_rate_init(void *priv_rate, void *priv_sta,
                for (i = 0; i < IWL_RATE_COUNT; i++)
                        rs_rate_scale_clear_window(&(lq_sta->lq_info[j].win[i]));
 
-       IWL_DEBUG_RATE("rate scale global init\n");
+       IWL_DEBUG_RATE("LQ: *** rate scale global init ***\n");
        /* TODO: what is a good starting rate for STA? About middle? Maybe not
         * the lowest or the highest rate.. Could consider using RSSI from
         * previous packets? Need to have IEEE 802.1X auth succeed immediately
@@ -2227,7 +2221,7 @@ static void rs_rate_init(void *priv_rate, void *priv_sta,
                DECLARE_MAC_BUF(mac);
 
                /* for IBSS the call are from tasklet */
-               IWL_DEBUG_HT("LQ: ADD station %s\n",
+               IWL_DEBUG_RATE("LQ: ADD station %s\n",
                             print_mac(mac, sta->addr));
 
                if (sta_id == IWL_INVALID_STATION) {
@@ -2287,11 +2281,15 @@ static void rs_rate_init(void *priv_rate, void *priv_sta,
        lq_sta->active_mimo3_rate &= ~((u16)0x2);
        lq_sta->active_mimo3_rate <<= IWL_FIRST_OFDM_RATE;
 
-       IWL_DEBUG_HT("SISO RATE %X MIMO2 RATE %X MIMO3 RATE %X\n",
+       IWL_DEBUG_RATE("SISO-RATE=%X MIMO2-RATE=%X MIMO3-RATE=%X\n",
                     lq_sta->active_siso_rate,
                     lq_sta->active_mimo2_rate,
                     lq_sta->active_mimo3_rate);
 
+       /* These values will be overriden later */
+       lq_sta->lq.general_params.single_stream_ant_msk = ANT_A;
+       lq_sta->lq.general_params.dual_stream_ant_msk = ANT_AB;
+
        /* as default allow aggregation for all tids */
        lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
 #endif /*CONFIG_IWL4965_HT*/
@@ -2307,29 +2305,32 @@ static void rs_rate_init(void *priv_rate, void *priv_sta,
 
 static void rs_fill_link_cmd(const struct iwl_priv *priv,
                             struct iwl4965_lq_sta *lq_sta,
-                            u32 new_rate,
-                            struct iwl_link_quality_cmd *lq_cmd)
+                            u32 new_rate)
 {
+       struct iwl4965_scale_tbl_info tbl_type;
        int index = 0;
        int rate_idx;
        int repeat_rate = 0;
-       u8 ant_toggle_count = 0;
+       u8 ant_toggle_cnt = 0;
        u8 use_ht_possible = 1;
-       struct iwl4965_scale_tbl_info tbl_type = { 0 };
+       u8 valid_tx_ant = 0;
+       struct iwl_link_quality_cmd *lq_cmd = &lq_sta->lq;
 
        /* Override starting rate (index 0) if needed for debug purposes */
        rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
 
        /* Interpret new_rate (rate_n_flags) */
+       memset(&tbl_type, 0, sizeof(tbl_type));
        rs_get_tbl_info_from_mcs(new_rate, lq_sta->band,
                                  &tbl_type, &rate_idx);
 
        /* How many times should we repeat the initial rate? */
        if (is_legacy(tbl_type.lq_type)) {
-               ant_toggle_count = 1;
+               ant_toggle_cnt = 1;
                repeat_rate = IWL_NUMBER_TRY;
-       } else
+       } else {
                repeat_rate = IWL_HT_NUMBER_TRY;
+       }
 
        lq_cmd->general_params.mimo_delimiter =
                        is_mimo(tbl_type.lq_type) ? 1 : 0;
@@ -2337,17 +2338,20 @@ static void rs_fill_link_cmd(const struct iwl_priv *priv,
        /* Fill 1st table entry (index 0) */
        lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
 
-       /*FIXME:RS*/
-       if (is_mimo(tbl_type.lq_type) || (tbl_type.ant_type == ANT_A))
-               lq_cmd->general_params.single_stream_ant_msk
-                       = LINK_QUAL_ANT_A_MSK;
-       else
-               lq_cmd->general_params.single_stream_ant_msk
-                       = LINK_QUAL_ANT_B_MSK;
+       if (num_of_ant(tbl_type.ant_type) == 1) {
+               lq_cmd->general_params.single_stream_ant_msk =
+                                               tbl_type.ant_type;
+       } else if (num_of_ant(tbl_type.ant_type) == 2) {
+               lq_cmd->general_params.dual_stream_ant_msk =
+                                               tbl_type.ant_type;
+       } /* otherwise we don't modify the existing value */
 
        index++;
        repeat_rate--;
 
+       if (priv)
+               valid_tx_ant = priv->hw_params.valid_tx_ant;
+
        /* Fill rest of rate table */
        while (index < LINK_QUAL_MAX_RETRY_NUM) {
                /* Repeat initial/next rate.
@@ -2355,16 +2359,13 @@ static void rs_fill_link_cmd(const struct iwl_priv *priv,
                 * For HT IWL_HT_NUMBER_TRY == 3, this executes twice. */
                while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
                        if (is_legacy(tbl_type.lq_type)) {
-                               if (ant_toggle_count <
-                                   NUM_TRY_BEFORE_ANTENNA_TOGGLE)
-                                       ant_toggle_count++;
-                               else if (priv && rs_is_other_ant_connected(
-                                               priv->hw_params.valid_tx_ant,
-                                               tbl_type.ant_type)) {
-                                       rs_toggle_antenna(&new_rate, &tbl_type);
-                                       ant_toggle_count = 1;
-                               }
-                       }
+                               if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
+                                       ant_toggle_cnt++;
+                               else if (priv &&
+                                        rs_toggle_antenna(valid_tx_ant,
+                                                       &new_rate, &tbl_type))
+                                       ant_toggle_cnt = 1;
+}
 
                        /* Override next rate if needed for debug purposes */
                        rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
@@ -2391,17 +2392,17 @@ static void rs_fill_link_cmd(const struct iwl_priv *priv,
 
                /* How many times should we repeat the next rate? */
                if (is_legacy(tbl_type.lq_type)) {
-                       if (ant_toggle_count < NUM_TRY_BEFORE_ANTENNA_TOGGLE)
-                               ant_toggle_count++;
-                       else if (priv && rs_is_other_ant_connected(
-                                               priv->hw_params.valid_tx_ant,
-                                               tbl_type.ant_type)) {
-                               rs_toggle_antenna(&new_rate, &tbl_type);
-                               ant_toggle_count = 1;
-                       }
+                       if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
+                               ant_toggle_cnt++;
+                       else if (priv &&
+                                rs_toggle_antenna(valid_tx_ant,
+                                                  &new_rate, &tbl_type))
+                               ant_toggle_cnt = 1;
+
                        repeat_rate = IWL_NUMBER_TRY;
-               } else
+               } else {
                        repeat_rate = IWL_HT_NUMBER_TRY;
+               }
 
                /* Don't allow HT rates after next pass.
                 * rs_get_lower_rate() will change type to LQ_A or LQ_G. */
@@ -2417,7 +2418,6 @@ static void rs_fill_link_cmd(const struct iwl_priv *priv,
                repeat_rate--;
        }
 
-       lq_cmd->general_params.dual_stream_ant_msk = 3;
        lq_cmd->agg_params.agg_dis_start_th = 3;
        lq_cmd->agg_params.agg_time_limit = cpu_to_le16(4000);
 }
@@ -2504,8 +2504,7 @@ static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
                lq_sta->lq.sta_id, lq_sta->dbg_fixed_rate);
 
        if (lq_sta->dbg_fixed_rate) {
-               rs_fill_link_cmd(NULL, lq_sta, lq_sta->dbg_fixed_rate,
-                                                               &lq_sta->lq);
+               rs_fill_link_cmd(NULL, lq_sta, lq_sta->dbg_fixed_rate);
                iwl_send_lq_cmd(lq_sta->drv, &lq_sta->lq, CMD_ASYNC);
        }