};
 
 #define IWL_RATE_MAX_WINDOW          62
-#define IWL_RATE_FLUSH        (3*HZ/10)
+#define IWL_RATE_FLUSH          (3*HZ)
 #define IWL_RATE_WIN_FLUSH       (HZ/2)
 #define IWL_RATE_HIGH_TH          11520
+#define IWL_SUCCESS_UP_TH         8960
+#define IWL_SUCCESS_DOWN_TH      10880
 #define IWL_RATE_MIN_FAILURE_TH       8
 #define IWL_RATE_MIN_SUCCESS_TH       8
 #define IWL_RATE_DECREASE_TH       1920
 
 #define IWL_RATE_FLUSH_MAX              5000   /* msec */
 #define IWL_RATE_FLUSH_MIN              50     /* msec */
+#define IWL_AVERAGE_PACKETS             1500
 
 static void iwl3945_bg_rate_scale_flush(unsigned long data)
 {
 
        spin_lock_irqsave(&rs_sta->lock, flags);
 
-       rs_sta->flush_pending = 0;
-
        /* Number of packets Rx'd since last time this timer ran */
        packet_count = (rs_sta->tx_packets - rs_sta->last_tx_packets) + 1;
 
        if (unflushed) {
                duration =
                    jiffies_to_msecs(jiffies - rs_sta->last_partial_flush);
-/*              duration = jiffies_to_msecs(rs_sta->flush_time); */
 
                IWL_DEBUG_RATE("Tx'd %d packets in %dms\n",
                               packet_count, duration);
                        pps = 0;
 
                if (pps) {
-                       duration = IWL_RATE_FLUSH_MAX / pps;
+                       duration = (IWL_AVERAGE_PACKETS * 1000) / pps;
                        if (duration < IWL_RATE_FLUSH_MIN)
                                duration = IWL_RATE_FLUSH_MIN;
+                       else if (duration > IWL_RATE_FLUSH_MAX)
+                               duration = IWL_RATE_FLUSH_MAX;
                } else
                        duration = IWL_RATE_FLUSH_MAX;
 
                          rs_sta->flush_time);
 
                rs_sta->last_partial_flush = jiffies;
+       } else {
+               rs_sta->flush_time = IWL_RATE_FLUSH;
+               rs_sta->flush_pending = 0;
        }
-
        /* If there weren't any unflushed entries, we don't schedule the timer
         * to run again */
 
  */
 static void iwl3945_collect_tx_data(struct iwl3945_rs_sta *rs_sta,
                                struct iwl3945_rate_scale_data *window,
-                               int success, int retries)
+                               int success, int retries, int index)
 {
        unsigned long flags;
+       s32 fail_count;
 
        if (!retries) {
                IWL_DEBUG_RATE("leave: retries == 0 -- should be at least 1\n");
                return;
        }
 
+       spin_lock_irqsave(&rs_sta->lock, flags);
        while (retries--) {
-               spin_lock_irqsave(&rs_sta->lock, flags);
 
                /* If we have filled up the window then subtract one from the
                 * success counter if the high-bit is counting toward
                /* Tag this window as having been updated */
                window->stamp = jiffies;
 
-               spin_unlock_irqrestore(&rs_sta->lock, flags);
        }
+
+       fail_count = window->counter - window->success_counter;
+       if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
+           (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
+               window->average_tpt = ((window->success_ratio *
+                               rs_sta->expected_tpt[index] + 64) / 128);
+       else
+               window->average_tpt = IWL_INV_TPT;
+
+       spin_unlock_irqrestore(&rs_sta->lock, flags);
+
 }
 
 static void rs_rate_init(void *priv, struct ieee80211_supported_band *sband,
                         struct ieee80211_sta *sta, void *priv_sta,
                         struct sk_buff *skb)
 {
-       u8 retries = 0, current_count;
+       s8 retries = 0, current_count;
        int scale_rate_index, first_index, last_index;
        unsigned long flags;
        struct iwl3945_priv *priv = (struct iwl3945_priv *)priv_rate;
        struct iwl3945_rs_sta *rs_sta = priv_sta;
        struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
-       int i;
 
        IWL_DEBUG_RATE("enter\n");
 
-       for (i = 0; i < IEEE80211_TX_MAX_RATES; i++)
-               retries += info->status.rates[i].count;
-       retries--;
+       retries = info->status.rates[0].count;
 
        first_index = sband->bitrates[info->status.rates[0].idx].hw_value;
        if ((first_index < 0) || (first_index >= IWL_RATE_COUNT)) {
         * at which the frame was finally transmitted (or failed if no
         * ACK)
         */
-       while (retries > 0) {
-               if (retries < priv->retry_rate) {
-                       current_count = retries;
+       while (retries > 1) {
+               if ((retries - 1) < priv->retry_rate) {
+                       current_count = (retries - 1);
                        last_index = scale_rate_index;
                } else {
                        current_count = priv->retry_rate;
                 * as was used for it (per current_count) */
                iwl3945_collect_tx_data(rs_sta,
                                    &rs_sta->win[scale_rate_index],
-                                   0, current_count);
+                                   0, current_count, scale_rate_index);
                IWL_DEBUG_RATE("Update rate %d for %d retries.\n",
                               scale_rate_index, current_count);
 
                retries -= current_count;
 
-               if (retries)
-                       scale_rate_index =
-                           iwl3945_rs_next_rate(priv, scale_rate_index);
+               scale_rate_index = last_index;
        }
 
 
                       "success" : "failure");
        iwl3945_collect_tx_data(rs_sta,
                            &rs_sta->win[last_index],
-                           info->flags & IEEE80211_TX_STAT_ACK, 1);
+                           info->flags & IEEE80211_TX_STAT_ACK, 1, last_index);
 
        /* We updated the rate scale window -- if its been more than
         * flush_time since the last run, schedule the flush
        spin_lock_irqsave(&rs_sta->lock, flags);
 
        if (!rs_sta->flush_pending &&
-           time_after(jiffies, rs_sta->last_partial_flush +
+           time_after(jiffies, rs_sta->last_flush +
                       rs_sta->flush_time)) {
 
+               rs_sta->last_partial_flush = jiffies;
                rs_sta->flush_pending = 1;
                mod_timer(&rs_sta->rate_scale_flush,
                          jiffies + rs_sta->flush_time);
 
        spin_lock_irqsave(&rs_sta->lock, flags);
 
+       /* for recent assoc, choose best rate regarding
+        * to rssi value
+        */
        if (rs_sta->start_rate != IWL_RATE_INVALID) {
-               index = rs_sta->start_rate;
+               if (rs_sta->start_rate < index &&
+                  (rate_mask & (1 << rs_sta->start_rate)))
+                       index = rs_sta->start_rate;
                rs_sta->start_rate = IWL_RATE_INVALID;
        }
 
 
        if (((fail_count <= IWL_RATE_MIN_FAILURE_TH) &&
             (window->success_counter < IWL_RATE_MIN_SUCCESS_TH))) {
-               window->average_tpt = IWL_INV_TPT;
                spin_unlock_irqrestore(&rs_sta->lock, flags);
 
                IWL_DEBUG_RATE("Invalid average_tpt on rate %d: "
 
        }
 
-       window->average_tpt = ((window->success_ratio *
-                               rs_sta->expected_tpt[index] + 64) / 128);
        current_tpt = window->average_tpt;
 
        high_low = iwl3945_get_adjacent_rate(rs_sta, index, rate_mask,
                }
        }
 
-       if ((window->success_ratio > IWL_RATE_HIGH_TH) ||
-           (current_tpt > window->average_tpt)) {
-               IWL_DEBUG_RATE("No action -- success_ratio [%d] > HIGH_TH or "
-                              "current_tpt [%d] > average_tpt [%d]\n",
-                              window->success_ratio,
-                              current_tpt, window->average_tpt);
-               scale_action = 0;
+       if (scale_action == -1) {
+               if (window->success_ratio > IWL_SUCCESS_DOWN_TH)
+                       scale_action = 0;
+       } else if (scale_action == 1) {
+               if (window->success_ratio < IWL_SUCCESS_UP_TH) {
+                       IWL_DEBUG_RATE("No action -- success_ratio [%d] < "
+                              "SUCCESS UP\n", window->success_ratio);
+                       scale_action = 0;
+               }
        }
 
        switch (scale_action) {