Index: compat-wireless-2014-05-22/net/mac80211/rc80211_minstrel.c
===================================================================
--- compat-wireless-2014-05-22.orig/net/mac80211/rc80211_minstrel.c
+++ compat-wireless-2014-05-22/net/mac80211/rc80211_minstrel.c
@@ -69,6 +69,23 @@ rix_to_ndx(struct minstrel_sta_info *mi,
 	return i;
 }
 
+/* Check whether rate sampling is required */
+static inline bool
+minstrel_requires_sampling(struct minstrel_sta_info *mi,
+	struct minstrel_priv *mp, unsigned int sampling_interval)
+{
+      if (mi->cur_intersample_spacing >= MIN_INTERSAMPLE_SPACING &&
+         (time_after(jiffies, mi->last_rate_update +
+                    (sampling_interval * HZ) / 1000))) {
+              mi->cur_intersample_spacing = 0;
+              mi->last_rate_update = jiffies;
+              return true;
+      } else {
+              mi->cur_intersample_spacing++;
+              return false;
+      }
+}
+
 /* find & sort topmost throughput rates */
 static inline void
 minstrel_sort_best_tp_rates(struct minstrel_sta_info *mi, int i, u8 *tp_list)
@@ -84,7 +101,8 @@ minstrel_sort_best_tp_rates(struct minst
 }
 
 static void
-minstrel_set_rate(struct minstrel_sta_info *mi, struct ieee80211_sta_rates *ratetbl,
+minstrel_set_rate(struct minstrel_sta_info *mi,
+		  struct ieee80211_sta_rates *ratetbl,
 		  int offset, int idx)
 {
 	struct minstrel_rate *r = &mi->r[idx];
@@ -127,6 +145,55 @@ minstrel_update_rates(struct minstrel_pr
 	rate_control_set_rates(mp->hw, mi->sta, ratetbl);
 }
 
+/*
+* Recalculate success probabilities and counters for a given rate using EWMA
+*/
+void
+minstrel_calc_rate_ewma(struct minstrel_priv *mp, struct minstrel_rate_stats *mr)
+{
+	int target_attempts, aging_factor;
+
+	target_attempts = 2;
+
+	/* Dynamic weigthting factor for EWMA calculation. The number of
+	* attempts to send a packet at a certain rate within an statistic
+	* update interval respresents the timeliness of success probability. */
+	if (mr->attempts > 10 * target_attempts)
+		aging_factor = 768; /* ~75% */
+	else if (mr->attempts > target_attempts)
+		aging_factor = 820; /* ~80% */
+	else if (mr->attempts > 0)
+		aging_factor = 920; /* ~90% */
+	else {
+		/* = ~99% to age prob. of non-sampled rates by 10% in 10sec */
+		aging_factor = 1023;
+	}
+
+	if (unlikely(mr->attempts > 0)) {
+		mr->sample_skipped = 0;
+		mr->cur_prob = MINSTREL_FRAC(mr->success, mr->attempts);
+		if (!mr->att_hist)
+			mr->probability = mr->cur_prob;
+		else
+			mr->probability = minstrel_ewma(mr->probability,
+						mr->cur_prob, aging_factor);
+		mr->att_hist += mr->attempts;
+		mr->succ_hist += mr->success;
+		mr->last_success = mr->success;
+		mr->last_attempts = mr->attempts;
+		mr->success = 0;
+		mr->attempts = 0;
+	} else {
+		mr->sample_skipped++;
+		if (mr->att_hist) {
+			mr->cur_prob = 0;
+			mr->probability = minstrel_ewma(mr->probability,
+						mr->cur_prob, aging_factor);
+		}
+	}
+
+}
+
 static void
 minstrel_update_stats(struct minstrel_priv *mp, struct minstrel_sta_info *mi)
 {
@@ -146,22 +213,8 @@ minstrel_update_stats(struct minstrel_pr
 		if (!usecs)
 			usecs = 1000000;
 
-		if (unlikely(mrs->attempts > 0)) {
-			mrs->sample_skipped = 0;
-			mrs->cur_prob = MINSTREL_FRAC(mrs->success,
-						      mrs->attempts);
-			mrs->succ_hist += mrs->success;
-			mrs->att_hist += mrs->attempts;
-			mrs->probability = minstrel_ewma(mrs->probability,
-							 mrs->cur_prob,
-							 EWMA_LEVEL);
-		} else
-			mrs->sample_skipped++;
-
-		mrs->last_success = mrs->success;
-		mrs->last_attempts = mrs->attempts;
-		mrs->success = 0;
-		mrs->attempts = 0;
+		/* Update success probabilities per rate */
+		minstrel_calc_rate_ewma(mp, mrs);
 
 		/* Update throughput per rate, reset thr. below 10% success */
 		if (mrs->probability < MINSTREL_FRAC(10, 100))
@@ -176,9 +229,7 @@ minstrel_update_stats(struct minstrel_pr
 			mr->adjusted_retry_count = mrs->retry_count >> 1;
 			if (mr->adjusted_retry_count > 2)
 				mr->adjusted_retry_count = 2;
-			mr->sample_limit = 4;
 		} else {
-			mr->sample_limit = -1;
 			mr->adjusted_retry_count = mrs->retry_count;
 		}
 		if (!mr->adjusted_retry_count)
@@ -215,7 +266,7 @@ minstrel_update_stats(struct minstrel_pr
 #endif
 
 	/* Reset update timer */
-	mi->stats_update = jiffies;
+	mi->last_stats_update = jiffies;
 
 	minstrel_update_rates(mp, mi);
 }
@@ -251,11 +302,8 @@ minstrel_tx_status(void *priv, struct ie
 	if ((info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE) && (i >= 0))
 		mi->sample_packets++;
 
-	if (mi->sample_deferred > 0)
-		mi->sample_deferred--;
-
-	if (time_after(jiffies, mi->stats_update +
-				(mp->update_interval * HZ) / 1000))
+	if (time_after(jiffies, mi->last_stats_update +
+				(WIFI_COHERENCE_TIME * 3 * HZ) / 1000))
 		minstrel_update_stats(mp, mi);
 }
 
@@ -273,7 +321,6 @@ minstrel_get_retry_count(struct minstrel
 	return retry;
 }
 
-
 static int
 minstrel_get_next_sample(struct minstrel_sta_info *mi)
 {
@@ -297,105 +344,85 @@ minstrel_get_rate(void *priv, struct iee
 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
 	struct minstrel_sta_info *mi = priv_sta;
 	struct minstrel_priv *mp = priv;
-	struct ieee80211_tx_rate *rate = &info->control.rates[0];
+	struct ieee80211_tx_rate *sampling_setup = NULL;
 	struct minstrel_rate *msr, *mr;
-	unsigned int ndx;
-	bool mrr_capable;
-	bool prev_sample;
-	int delta;
-	int sampling_ratio;
+	unsigned int sampling_ndx = 0;
+	unsigned int rate_sampling_int = 0;
+	bool indirect_rate_sampling = false;
+	bool mrr_capable = false;
 
 	/* management/no-ack frames do not use rate control */
 	if (rate_control_send_low(sta, priv_sta, txrc))
 		return;
 
-	/* check multi-rate-retry capabilities & adjust lookaround_rate */
-	mrr_capable = mp->has_mrr &&
-		      !txrc->rts &&
-		      !txrc->bss_conf->use_cts_prot;
-	if (mrr_capable)
-		sampling_ratio = mp->lookaround_rate_mrr;
-	else
-		sampling_ratio = mp->lookaround_rate;
-
 	/* increase sum packet counter */
 	mi->total_packets++;
 
+	/* avoid overflowing packet counters */
+	if (mi->total_packets == ~0) {
+	            mi->sample_packets = 0;
+		            mi->total_packets = 0;
+	}
+
 #ifdef CPTCFG_MAC80211_DEBUGFS
 	if (mp->fixed_rate_idx != -1)
 		return;
 #endif
 
-	delta = (mi->total_packets * sampling_ratio / 100) -
-			(mi->sample_packets + mi->sample_deferred / 2);
+	/* Don't use EAPOL frames for sampling on non-mrr hw */
+	if (mp->hw->max_rates == 1 &&
+	        (info->control.flags & IEEE80211_TX_CTRL_PORT_CTRL_PROTO))
+	            return;
 
-	/* delta < 0: no sampling required */
-	prev_sample = mi->prev_sample;
-	mi->prev_sample = false;
-	if (delta < 0 || (!mrr_capable && prev_sample))
-		return;
+	/* Check multi-rate-retry capabilities & adjust sampling interval */
+	mrr_capable = mp->has_mrr && !txrc->rts && !txrc->bss_conf->use_cts_prot;
+	if (mrr_capable)
+		rate_sampling_int = WIFI_COHERENCE_TIME / 2;
+	else
+		rate_sampling_int = WIFI_COHERENCE_TIME * 2;
 
-	if (mi->total_packets >= 10000) {
-		mi->sample_deferred = 0;
-		mi->sample_packets = 0;
-		mi->total_packets = 0;
-	} else if (delta > mi->n_rates * 2) {
-		/* With multi-rate retry, not every planned sample
-		 * attempt actually gets used, due to the way the retry
-		 * chain is set up - [max_tp,sample,prob,lowest] for
-		 * sample_rate < max_tp.
-		 *
-		 * If there's too much sampling backlog and the link
-		 * starts getting worse, minstrel would start bursting
-		 * out lots of sampling frames, which would result
-		 * in a large throughput loss. */
-		mi->sample_packets += (delta - mi->n_rates * 2);
-	}
-
-	/* get next random rate sample */
-	ndx = minstrel_get_next_sample(mi);
-	msr = &mi->r[ndx];
-	mr = &mi->r[mi->max_tp_rate[0]];
-
-	/* Decide if direct ( 1st mrr stage) or indirect (2nd mrr stage)
-	 * rate sampling method should be used.
-	 * Respect such rates that are not sampled for 20 interations.
-	 */
-	if (mrr_capable &&
-	    msr->perfect_tx_time > mr->perfect_tx_time &&
-	    msr->stats.sample_skipped < 20) {
-		/* Only use IEEE80211_TX_CTL_RATE_CTRL_PROBE to mark
-		 * packets that have the sampling rate deferred to the
-		 * second MRR stage. Increase the sample counter only
-		 * if the deferred sample rate was actually used.
-		 * Use the sample_deferred counter to make sure that
-		 * the sampling is not done in large bursts */
-		info->flags |= IEEE80211_TX_CTL_RATE_CTRL_PROBE;
-		rate++;
-		mi->sample_deferred++;
-	} else {
-		if (!msr->sample_limit != 0)
+	if (unlikely(minstrel_requires_sampling(mi, mp, rate_sampling_int))) {
+		/* get next random rate sample */
+		sampling_ndx = minstrel_get_next_sample(mi);
+		msr = &mi->r[sampling_ndx];
+
+		/* If we're not using MRR and the sampling rate already
+		 * has a probability of >95%, we shouldn't be attempting
+		 * to use it, as this only wastes precious airtime */
+		if (!mrr_capable && (mi->r[sampling_ndx].stats.probability >
+				     MINSTREL_FRAC(95, 100)))
 			return;
 
-		mi->sample_packets++;
-		if (msr->sample_limit > 0)
-			msr->sample_limit--;
-	}
+		/* current max throughput rate */
+		mr = &mi->r[mi->max_tp_rate[0]];
 
-	/* If we're not using MRR and the sampling rate already
-	 * has a probability of >95%, we shouldn't be attempting
-	 * to use it, as this only wastes precious airtime */
-	if (!mrr_capable &&
-	   (mi->r[ndx].stats.probability > MINSTREL_FRAC(95, 100)))
+		/* Check if direct (1st mrr stage) or indirect (2nd mrr stage)
+		 * rate sampling method should be used. Enforce rate sampling
+		 * for such rates not sampled within last 20 update cycles. */
+		if (mrr_capable &&
+		    msr->perfect_tx_time > mr->perfect_tx_time &&
+		    msr->stats.sample_skipped < 20)
+			indirect_rate_sampling = true;
+
+		/* setup mrr sampling: indirect -> mrr[1], direct -> mrr[0] */
+		if (indirect_rate_sampling) {
+			info->flags |= IEEE80211_TX_CTL_RATE_CTRL_PROBE;
+			sampling_setup = &info->control.rates[1];
+		} else {
+			sampling_setup = &info->control.rates[0];
+			mi->sample_packets++;
+		}
+	} else {
+		/* no sampling required */
 		return;
+	}
 
-	mi->prev_sample = true;
-
-	rate->idx = mi->r[ndx].rix;
-	rate->count = minstrel_get_retry_count(&mi->r[ndx], info);
+	/* assign sampling parameter*/
+	sampling_setup->idx = mi->r[sampling_ndx].rix;
+	sampling_setup->count = minstrel_get_retry_count(&mi->r[sampling_ndx],
+							 info);
 }
 
-
 static void
 calc_rate_durations(enum ieee80211_band band,
 		    struct minstrel_rate *d,
@@ -485,7 +512,6 @@ minstrel_rate_init(void *priv, struct ie
 
 		/* calculate maximum number of retransmissions before
 		 * fallback (based on maximum segment size) */
-		mr->sample_limit = -1;
 		mrs->retry_count = 1;
 		mr->retry_count_cts = 1;
 		mrs->retry_count_rtscts = 1;
@@ -520,7 +546,9 @@ minstrel_rate_init(void *priv, struct ie
 	}
 
 	mi->n_rates = n;
-	mi->stats_update = jiffies;
+	mi->last_stats_update = jiffies;
+
+	mi->last_rate_update = jiffies;
 
 	init_sample_table(mi);
 	minstrel_update_rates(mp, mi);
@@ -554,7 +582,9 @@ minstrel_alloc_sta(void *priv, struct ie
 	if (!mi->sample_table)
 		goto error1;
 
-	mi->stats_update = jiffies;
+	mi->last_stats_update = jiffies;
+	mi->last_rate_update = jiffies;
+
 	return mi;
 
 error1:
@@ -620,12 +650,6 @@ minstrel_alloc(struct ieee80211_hw *hw, 
 	mp->cw_min = 15;
 	mp->cw_max = 1023;
 
-	/* number of packets (in %) to use for sampling other rates
-	 * sample less often for non-mrr packets, because the overhead
-	 * is much higher than with mrr */
-	mp->lookaround_rate = 5;
-	mp->lookaround_rate_mrr = 10;
-
 	/* maximum time that the hw is allowed to stay in one MRR segment */
 	mp->segment_size = 6000;
 
@@ -639,7 +663,6 @@ minstrel_alloc(struct ieee80211_hw *hw, 
 		mp->has_mrr = true;
 
 	mp->hw = hw;
-	mp->update_interval = 100;
 
 #ifdef CPTCFG_MAC80211_DEBUGFS
 	mp->fixed_rate_idx = (u32) -1;
Index: compat-wireless-2014-05-22/net/mac80211/rc80211_minstrel.h
===================================================================
--- compat-wireless-2014-05-22.orig/net/mac80211/rc80211_minstrel.h
+++ compat-wireless-2014-05-22/net/mac80211/rc80211_minstrel.h
@@ -9,8 +9,8 @@
 #ifndef __RC_MINSTREL_H
 #define __RC_MINSTREL_H
 
-#define EWMA_LEVEL	96	/* ewma weighting factor [/EWMA_DIV] */
-#define EWMA_DIV	128
+#define EWMA_DIV	1024
+#define EWMA_LEVEL	 768	/* ewma weighting factor of 75% [/EWMA_DIV] */
 #define SAMPLE_COLUMNS	10	/* number of columns in sample table */
 
 
@@ -22,9 +22,13 @@
 /* number of highest throughput rates to consider*/
 #define MAX_THR_RATES 4
 
-/*
- * Perform EWMA (Exponentially Weighted Moving Average) calculation
-  */
+/* time in milliseconds the wireless channel is assumed to be stable */
+#define WIFI_COHERENCE_TIME 25
+
+/* minimum number of packets between two successive sample attempts */
+#define MIN_INTERSAMPLE_SPACING 1
+
+/* Perform EWMA (Exponentially Weighted Moving Average) calculation */
 static inline int
 minstrel_ewma(int old, int new, int weight)
 {
@@ -42,6 +46,8 @@ struct minstrel_rate_stats {
 	/* current throughput */
 	unsigned int cur_tp;
 
+	unsigned int max_potential_tp;
+
 	/* packet delivery probabilities */
 	unsigned int cur_prob, probability;
 
@@ -60,7 +66,6 @@ struct minstrel_rate {
 	unsigned int perfect_tx_time;
 	unsigned int ack_time;
 
-	int sample_limit;
 	unsigned int retry_count_cts;
 	unsigned int adjusted_retry_count;
 
@@ -70,7 +75,7 @@ struct minstrel_rate {
 struct minstrel_sta_info {
 	struct ieee80211_sta *sta;
 
-	unsigned long stats_update;
+	unsigned long last_stats_update;
 	unsigned int sp_ack_dur;
 	unsigned int rate_avg;
 
@@ -78,16 +83,16 @@ struct minstrel_sta_info {
 
 	u8 max_tp_rate[MAX_THR_RATES];
 	u8 max_prob_rate;
+	u8 cur_intersample_spacing;
+	unsigned long last_rate_update;
 	unsigned int total_packets;
 	unsigned int sample_packets;
-	int sample_deferred;
 
 	unsigned int sample_row;
 	unsigned int sample_column;
 
 	int n_rates;
 	struct minstrel_rate *r;
-	bool prev_sample;
 
 	/* sampling table */
 	u8 *sample_table;
@@ -97,16 +102,14 @@ struct minstrel_sta_info {
 #endif
 };
 
+
 struct minstrel_priv {
 	struct ieee80211_hw *hw;
-	bool has_mrr;
 	unsigned int cw_min;
 	unsigned int cw_max;
 	unsigned int max_retry;
 	unsigned int segment_size;
-	unsigned int update_interval;
-	unsigned int lookaround_rate;
-	unsigned int lookaround_rate_mrr;
+	bool has_mrr;
 
 	u8 cck_rates[4];
 
@@ -136,9 +139,18 @@ extern const struct rate_control_ops mac
 void minstrel_add_sta_debugfs(void *priv, void *priv_sta, struct dentry *dir);
 void minstrel_remove_sta_debugfs(void *priv, void *priv_sta);
 
+/* Get sampling interval as subject to mrr capabilities*/
+unsigned int
+minstrel_get_rate_sampling_interval(struct minstrel_priv *mp, bool mrr_capable);
+
+/* Recalculate success probabilities and counters for a given rate using EWMA */
+void
+minstrel_calc_rate_ewma(struct minstrel_priv *mp, struct minstrel_rate_stats *mr);
+
 /* debugfs */
 int minstrel_stats_open(struct inode *inode, struct file *file);
-ssize_t minstrel_stats_read(struct file *file, char __user *buf, size_t len, loff_t *ppos);
+ssize_t minstrel_stats_read(struct file *file, char __user *buf, size_t len,
+	loff_t *ppos);
 int minstrel_stats_release(struct inode *inode, struct file *file);
 
 #endif
