From 5cc942a80eff3b41df164d5e875befa4fc12b183 Mon Sep 17 00:00:00 2001
From: Koen Vandeputte <koen.vandeputte@ncentric.com>
Date: Wed, 28 Aug 2019 12:12:41 +0200
Subject: [PATCH] ath9k: backport dynack improvements

Close cooperation with Lorenzo Bianconi resulted
in these patches which fix all remaining seen issues
when using dynack.

Fix link losses when:
- Late Ack's are not seen or not present
- switching from too low static coverage class to dynack on a live link

These are fixed by setting the Ack Timeout/Slottime to
the max possible value for the currently used channel width when
a new station has been discovered.

When traffic flows, dynack is able to adjust to optimal values
within a few packets received (typically < 1 second)

These changes have been thoroughly tested on ~60 offshore devices
all interconnected using mesh over IBSS and dynack enabled on all.

Distances between devices varied from <100m up to ~35km

Signed-off-by: Koen Vandeputte <koen.vandeputte@ncentric.com>
Signed-off-by: Lorenzo Bianconi <lorenzo@kernel.org>
---
 ...roduce-ath_dynack_set_timeout-routin.patch | 94 ++++++++++++++++++
 ...perly-set-last-timeout-timestamp-in-.patch | 32 +++++++
 ...-max-timeout-according-to-channel-wi.patch | 96 +++++++++++++++++++
 ...-ackto-to-max-timeout-in-ath_dynack_.patch | 78 +++++++++++++++
 4 files changed, 300 insertions(+)
 create mode 100644 package/kernel/mac80211/patches/ath/558-ath9k-dynack-introduce-ath_dynack_set_timeout-routin.patch
 create mode 100644 package/kernel/mac80211/patches/ath/559-ath9k-dynack-properly-set-last-timeout-timestamp-in-.patch
 create mode 100644 package/kernel/mac80211/patches/ath/560-ath9k-dynack-set-max-timeout-according-to-channel-wi.patch
 create mode 100644 package/kernel/mac80211/patches/ath/561-ath9k-dynack-set-ackto-to-max-timeout-in-ath_dynack_.patch

diff --git a/package/kernel/mac80211/patches/ath/558-ath9k-dynack-introduce-ath_dynack_set_timeout-routin.patch b/package/kernel/mac80211/patches/ath/558-ath9k-dynack-introduce-ath_dynack_set_timeout-routin.patch
new file mode 100644
index 0000000000..e13a15026e
--- /dev/null
+++ b/package/kernel/mac80211/patches/ath/558-ath9k-dynack-introduce-ath_dynack_set_timeout-routin.patch
@@ -0,0 +1,94 @@
+From 4420866ef1b602682b009e0186fbb8aefd2125be Mon Sep 17 00:00:00 2001
+From: Lorenzo Bianconi <lorenzo@kernel.org>
+Date: Tue, 20 Aug 2019 18:20:19 +0200
+Subject: [PATCH 1/4] ath9k: dynack: introduce ath_dynack_set_timeout routine
+
+Introduce ath_dynack_set_timeout routine to configure slottime/ack/cts
+timeouts and remove duplicated code
+
+Tested-by: Koen Vandeputte <koen.vandeputte@ncentric.com>
+Signed-off-by: Lorenzo Bianconi <lorenzo@kernel.org>
+---
+ drivers/net/wireless/ath/ath9k/dynack.c | 37 ++++++++++++++-----------
+ 1 file changed, 21 insertions(+), 16 deletions(-)
+
+diff --git a/drivers/net/wireless/ath/ath9k/dynack.c b/drivers/net/wireless/ath/ath9k/dynack.c
+index f112fa5b2eac..38dbe25919f7 100644
+--- a/drivers/net/wireless/ath/ath9k/dynack.c
++++ b/drivers/net/wireless/ath/ath9k/dynack.c
+@@ -78,6 +78,24 @@ static inline bool ath_dynack_bssidmask(struct ath_hw *ah, const u8 *mac)
+ 	return true;
+ }
+ 
++/**
++ * ath_dynack_set_timeout - configure timeouts/slottime registers
++ * @ah: ath hw
++ * @to: timeout value
++ *
++ */
++static void ath_dynack_set_timeout(struct ath_hw *ah, int to)
++{
++	struct ath_common *common = ath9k_hw_common(ah);
++	int slottime = (to - 3) / 2;
++
++	ath_dbg(common, DYNACK, "ACK timeout %u slottime %u\n",
++		to, slottime);
++	ath9k_hw_setslottime(ah, slottime);
++	ath9k_hw_set_ack_timeout(ah, to);
++	ath9k_hw_set_cts_timeout(ah, to);
++}
++
+ /**
+  * ath_dynack_compute_ackto - compute ACK timeout as the maximum STA timeout
+  * @ah: ath hw
+@@ -86,7 +104,6 @@ static inline bool ath_dynack_bssidmask(struct ath_hw *ah, const u8 *mac)
+  */
+ static void ath_dynack_compute_ackto(struct ath_hw *ah)
+ {
+-	struct ath_common *common = ath9k_hw_common(ah);
+ 	struct ath_dynack *da = &ah->dynack;
+ 	struct ath_node *an;
+ 	int to = 0;
+@@ -96,15 +113,8 @@ static void ath_dynack_compute_ackto(struct ath_hw *ah)
+ 			to = an->ackto;
+ 
+ 	if (to && da->ackto != to) {
+-		u32 slottime;
+-
+-		slottime = (to - 3) / 2;
++		ath_dynack_set_timeout(ah, to);
+ 		da->ackto = to;
+-		ath_dbg(common, DYNACK, "ACK timeout %u slottime %u\n",
+-			da->ackto, slottime);
+-		ath9k_hw_setslottime(ah, slottime);
+-		ath9k_hw_set_ack_timeout(ah, da->ackto);
+-		ath9k_hw_set_cts_timeout(ah, da->ackto);
+ 	}
+ }
+ 
+@@ -198,10 +208,7 @@ void ath_dynack_sample_tx_ts(struct ath_hw *ah, struct sk_buff *skb,
+ 		    ieee80211_is_assoc_resp(hdr->frame_control) ||
+ 		    ieee80211_is_auth(hdr->frame_control)) {
+ 			ath_dbg(common, DYNACK, "late ack\n");
+-
+-			ath9k_hw_setslottime(ah, (LATEACK_TO - 3) / 2);
+-			ath9k_hw_set_ack_timeout(ah, LATEACK_TO);
+-			ath9k_hw_set_cts_timeout(ah, LATEACK_TO);
++			ath_dynack_set_timeout(ah, LATEACK_TO);
+ 			if (sta) {
+ 				struct ath_node *an;
+ 
+@@ -340,9 +347,7 @@ void ath_dynack_reset(struct ath_hw *ah)
+ 	da->ack_rbf.h_rb = 0;
+ 
+ 	/* init acktimeout */
+-	ath9k_hw_setslottime(ah, (ackto - 3) / 2);
+-	ath9k_hw_set_ack_timeout(ah, ackto);
+-	ath9k_hw_set_cts_timeout(ah, ackto);
++	ath_dynack_set_timeout(ah, ackto);
+ }
+ EXPORT_SYMBOL(ath_dynack_reset);
+ 
+-- 
+2.17.1
+
diff --git a/package/kernel/mac80211/patches/ath/559-ath9k-dynack-properly-set-last-timeout-timestamp-in-.patch b/package/kernel/mac80211/patches/ath/559-ath9k-dynack-properly-set-last-timeout-timestamp-in-.patch
new file mode 100644
index 0000000000..9504af1419
--- /dev/null
+++ b/package/kernel/mac80211/patches/ath/559-ath9k-dynack-properly-set-last-timeout-timestamp-in-.patch
@@ -0,0 +1,32 @@
+From e5b56ce50eab31d24df6a70cf025db3acc4aa3ac Mon Sep 17 00:00:00 2001
+From: Lorenzo Bianconi <lorenzo@kernel.org>
+Date: Tue, 20 Aug 2019 18:20:20 +0200
+Subject: [PATCH 2/4] ath9k: dynack: properly set last timeout timestamp in
+ ath_dynack_reset
+
+Add compute timeout to last computation timestamp in
+ath_dynack_reset in order to not run ath_dynack_compute_ackto
+immediately
+
+Tested-by: Koen Vandeputte <koen.vandeputte@ncentric.com>
+Signed-off-by: Lorenzo Bianconi <lorenzo@kernel.org>
+---
+ drivers/net/wireless/ath/ath9k/dynack.c | 2 +-
+ 1 file changed, 1 insertion(+), 1 deletion(-)
+
+diff --git a/drivers/net/wireless/ath/ath9k/dynack.c b/drivers/net/wireless/ath/ath9k/dynack.c
+index 38dbe25919f7..398ea872751f 100644
+--- a/drivers/net/wireless/ath/ath9k/dynack.c
++++ b/drivers/net/wireless/ath/ath9k/dynack.c
+@@ -338,7 +338,7 @@ void ath_dynack_reset(struct ath_hw *ah)
+ 	u32 ackto = 9 + 16 + 64;
+ 	struct ath_dynack *da = &ah->dynack;
+ 
+-	da->lto = jiffies;
++	da->lto = jiffies + COMPUTE_TO;
+ 	da->ackto = ackto;
+ 
+ 	da->st_rbf.t_rb = 0;
+-- 
+2.17.1
+
diff --git a/package/kernel/mac80211/patches/ath/560-ath9k-dynack-set-max-timeout-according-to-channel-wi.patch b/package/kernel/mac80211/patches/ath/560-ath9k-dynack-set-max-timeout-according-to-channel-wi.patch
new file mode 100644
index 0000000000..f40289cca4
--- /dev/null
+++ b/package/kernel/mac80211/patches/ath/560-ath9k-dynack-set-max-timeout-according-to-channel-wi.patch
@@ -0,0 +1,96 @@
+From 3f737abb7d53cc80d619a3b4a30b6fa63cdc8df7 Mon Sep 17 00:00:00 2001
+From: Lorenzo Bianconi <lorenzo@kernel.org>
+Date: Tue, 20 Aug 2019 18:20:21 +0200
+Subject: [PATCH 3/4] ath9k: dynack: set max timeout according to channel width
+
+Compute maximum configurable ackimeout/ctstimeout according to channel
+width (clockrate)
+
+Tested-by: Koen Vandeputte <koen.vandeputte@ncentric.com>
+Signed-off-by: Lorenzo Bianconi <lorenzo@kernel.org>
+---
+ drivers/net/wireless/ath/ath9k/dynack.c | 38 +++++++++++++++++++------
+ 1 file changed, 30 insertions(+), 8 deletions(-)
+
+diff --git a/drivers/net/wireless/ath/ath9k/dynack.c b/drivers/net/wireless/ath/ath9k/dynack.c
+index 398ea872751f..fe9181533de3 100644
+--- a/drivers/net/wireless/ath/ath9k/dynack.c
++++ b/drivers/net/wireless/ath/ath9k/dynack.c
+@@ -20,11 +20,30 @@
+ 
+ #define COMPUTE_TO		(5 * HZ)
+ #define LATEACK_DELAY		(10 * HZ)
+-#define LATEACK_TO		256
+-#define MAX_DELAY		300
+ #define EWMA_LEVEL		96
+ #define EWMA_DIV		128
+ 
++/**
++ * ath_dynack_get_max_to - set max timeout according to channel width
++ * @ah: ath hw
++ *
++ */
++static u32 ath_dynack_get_max_to(struct ath_hw *ah)
++{
++	const struct ath9k_channel *chan = ah->curchan;
++
++	if (!chan)
++		return 300;
++
++	if (IS_CHAN_HT40(chan))
++		return 300;
++	if (IS_CHAN_HALF_RATE(chan))
++		return 750;
++	if (IS_CHAN_QUARTER_RATE(chan))
++		return 1500;
++	return 600;
++}
++
+ /**
+  * ath_dynack_ewma - EWMA (Exponentially Weighted Moving Average) calculation
+  *
+@@ -126,15 +145,16 @@ static void ath_dynack_compute_ackto(struct ath_hw *ah)
+  */
+ static void ath_dynack_compute_to(struct ath_hw *ah)
+ {
+-	u32 ackto, ack_ts;
+-	u8 *dst, *src;
++	struct ath_dynack *da = &ah->dynack;
++	u32 ackto, ack_ts, max_to;
+ 	struct ieee80211_sta *sta;
+-	struct ath_node *an;
+ 	struct ts_info *st_ts;
+-	struct ath_dynack *da = &ah->dynack;
++	struct ath_node *an;
++	u8 *dst, *src;
+ 
+ 	rcu_read_lock();
+ 
++	max_to = ath_dynack_get_max_to(ah);
+ 	while (da->st_rbf.h_rb != da->st_rbf.t_rb &&
+ 	       da->ack_rbf.h_rb != da->ack_rbf.t_rb) {
+ 		ack_ts = da->ack_rbf.tstamp[da->ack_rbf.h_rb];
+@@ -150,7 +170,7 @@ static void ath_dynack_compute_to(struct ath_hw *ah)
+ 		if (ack_ts > st_ts->tstamp + st_ts->dur) {
+ 			ackto = ack_ts - st_ts->tstamp - st_ts->dur;
+ 
+-			if (ackto < MAX_DELAY) {
++			if (ackto < max_to) {
+ 				sta = ieee80211_find_sta_by_ifaddr(ah->hw, dst,
+ 								   src);
+ 				if (sta) {
+@@ -207,8 +227,10 @@ void ath_dynack_sample_tx_ts(struct ath_hw *ah, struct sk_buff *skb,
+ 		if (ieee80211_is_assoc_req(hdr->frame_control) ||
+ 		    ieee80211_is_assoc_resp(hdr->frame_control) ||
+ 		    ieee80211_is_auth(hdr->frame_control)) {
++			u32 max_to = ath_dynack_get_max_to(ah);
++
+ 			ath_dbg(common, DYNACK, "late ack\n");
+-			ath_dynack_set_timeout(ah, LATEACK_TO);
++			ath_dynack_set_timeout(ah, max_to);
+ 			if (sta) {
+ 				struct ath_node *an;
+ 
+-- 
+2.17.1
+
diff --git a/package/kernel/mac80211/patches/ath/561-ath9k-dynack-set-ackto-to-max-timeout-in-ath_dynack_.patch b/package/kernel/mac80211/patches/ath/561-ath9k-dynack-set-ackto-to-max-timeout-in-ath_dynack_.patch
new file mode 100644
index 0000000000..5b75096e19
--- /dev/null
+++ b/package/kernel/mac80211/patches/ath/561-ath9k-dynack-set-ackto-to-max-timeout-in-ath_dynack_.patch
@@ -0,0 +1,78 @@
+From cc783bfa67e87d2e6206f7626b7bbb74d5c5f269 Mon Sep 17 00:00:00 2001
+From: Lorenzo Bianconi <lorenzo@kernel.org>
+Date: Tue, 20 Aug 2019 18:20:22 +0200
+Subject: [PATCH 4/4] ath9k: dynack: set ackto to max timeout in
+ ath_dynack_reset
+
+Initialize acktimeout to the maximum configurable value in
+ath_dynack_reset in order to not disconnect long distance static links
+enabling dynack and even to take care of possible errors configuring
+a static timeout. Moreover initialize station timeout value to the current
+acktimeout value
+
+Tested-by: Koen Vandeputte <koen.vandeputte@ncentric.com>
+Signed-off-by: Lorenzo Bianconi <lorenzo@kernel.org>
+---
+ drivers/net/wireless/ath/ath9k/dynack.c | 20 +++++++++++++-------
+ 1 file changed, 13 insertions(+), 7 deletions(-)
+
+diff --git a/drivers/net/wireless/ath/ath9k/dynack.c b/drivers/net/wireless/ath/ath9k/dynack.c
+index fe9181533de3..f786be04d0ac 100644
+--- a/drivers/net/wireless/ath/ath9k/dynack.c
++++ b/drivers/net/wireless/ath/ath9k/dynack.c
+@@ -321,11 +321,9 @@ EXPORT_SYMBOL(ath_dynack_sample_ack_ts);
+  */
+ void ath_dynack_node_init(struct ath_hw *ah, struct ath_node *an)
+ {
+-	/* ackto = slottime + sifs + air delay */
+-	u32 ackto = 9 + 16 + 64;
+ 	struct ath_dynack *da = &ah->dynack;
+ 
+-	an->ackto = ackto;
++	an->ackto = da->ackto;
+ 
+ 	spin_lock(&da->qlock);
+ 	list_add_tail(&an->list, &da->nodes);
+@@ -356,20 +354,26 @@ EXPORT_SYMBOL(ath_dynack_node_deinit);
+  */
+ void ath_dynack_reset(struct ath_hw *ah)
+ {
+-	/* ackto = slottime + sifs + air delay */
+-	u32 ackto = 9 + 16 + 64;
+ 	struct ath_dynack *da = &ah->dynack;
++	struct ath_node *an;
++
++	spin_lock_bh(&da->qlock);
+ 
+ 	da->lto = jiffies + COMPUTE_TO;
+-	da->ackto = ackto;
+ 
+ 	da->st_rbf.t_rb = 0;
+ 	da->st_rbf.h_rb = 0;
+ 	da->ack_rbf.t_rb = 0;
+ 	da->ack_rbf.h_rb = 0;
+ 
++	da->ackto = ath_dynack_get_max_to(ah);
++	list_for_each_entry(an, &da->nodes, list)
++		an->ackto = da->ackto;
++
+ 	/* init acktimeout */
+-	ath_dynack_set_timeout(ah, ackto);
++	ath_dynack_set_timeout(ah, da->ackto);
++
++	spin_unlock_bh(&da->qlock);
+ }
+ EXPORT_SYMBOL(ath_dynack_reset);
+ 
+@@ -386,6 +390,8 @@ void ath_dynack_init(struct ath_hw *ah)
+ 
+ 	spin_lock_init(&da->qlock);
+ 	INIT_LIST_HEAD(&da->nodes);
++	/* ackto = slottime + sifs + air delay */
++	da->ackto = 9 + 16 + 64;
+ 
+ 	ah->hw->wiphy->features |= NL80211_FEATURE_ACKTO_ESTIMATION;
+ }
+-- 
+2.17.1
+
-- 
2.30.2