From e9fd938246ca0276c6424c0b01ff7ead972be675 Mon Sep 17 00:00:00 2001
From: nbd <nbd@3c298f89-4303-0410-b956-a3cf2f4a3e73>
Date: Tue, 7 Oct 2008 01:39:38 +0000
Subject: [PATCH] madwifi: allow ad-hoc mode with software based TSF merging
 (hardware merging can screw up the internal timers and cause transmit hangs)
 - based on a patch by sven-ola. activated by wlanconfig's nosbeacon flag

git-svn-id: svn://svn.openwrt.org/openwrt/trunk@12883 3c298f89-4303-0410-b956-a3cf2f4a3e73
---
 .../patches/355-eap_auth_disassoc.patch       |   2 +-
 package/madwifi/patches/363-fix_turbo.patch   |   2 +-
 .../madwifi/patches/366-bstuck_thresh.patch   |   4 +-
 package/madwifi/patches/370-wdsvap.patch      |   4 +-
 package/madwifi/patches/374-nbtt_fix.patch    |   2 +-
 .../madwifi/patches/375-atim_tsf_update.patch |  14 +--
 package/madwifi/patches/383-ibss_hostap.patch | 105 ++++++++++++++++++
 .../madwifi/patches/406-monitor_r3711.patch   |   4 +-
 .../madwifi/patches/408-changeset_r3337.patch |   2 +-
 package/madwifi/patches/450-new_hal.patch     |   2 +-
 10 files changed, 123 insertions(+), 18 deletions(-)
 create mode 100644 package/madwifi/patches/383-ibss_hostap.patch

diff --git a/package/madwifi/patches/355-eap_auth_disassoc.patch b/package/madwifi/patches/355-eap_auth_disassoc.patch
index e121dae21..a3d8de4f5 100644
--- a/package/madwifi/patches/355-eap_auth_disassoc.patch
+++ b/package/madwifi/patches/355-eap_auth_disassoc.patch
@@ -5,7 +5,7 @@ Signed-off-by: Felix Fietkau <nbd@openwrt.org>
 
 --- a/ath/if_ath.c
 +++ b/ath/if_ath.c
-@@ -8322,6 +8322,14 @@
+@@ -8332,6 +8332,14 @@
  #endif
  				if (ts->ts_status & HAL_TXERR_XRETRY) {
  					sc->sc_stats.ast_tx_xretries++;
diff --git a/package/madwifi/patches/363-fix_turbo.patch b/package/madwifi/patches/363-fix_turbo.patch
index b3c732465..54fcd76a6 100644
--- a/package/madwifi/patches/363-fix_turbo.patch
+++ b/package/madwifi/patches/363-fix_turbo.patch
@@ -1,6 +1,6 @@
 --- a/ath/if_ath.c
 +++ b/ath/if_ath.c
-@@ -4915,7 +4915,7 @@
+@@ -4925,7 +4925,7 @@
  	 * capability info and arrange for a mode change
  	 * if needed.
  	 */
diff --git a/package/madwifi/patches/366-bstuck_thresh.patch b/package/madwifi/patches/366-bstuck_thresh.patch
index ecf5dc98f..6aa70d39b 100644
--- a/package/madwifi/patches/366-bstuck_thresh.patch
+++ b/package/madwifi/patches/366-bstuck_thresh.patch
@@ -32,7 +32,7 @@
  MODULE_PARM_DESC(autocreate, "Create ath device in "
  		"[sta|ap|wds|adhoc|ahdemo|monitor] mode. defaults to sta, use "
  		"'none' to disable");
-@@ -5062,7 +5066,7 @@
+@@ -5072,7 +5076,7 @@
  		DPRINTF(sc, ATH_DEBUG_BEACON_PROC,
  			"Missed %u consecutive beacons (n_beacon=%u)\n",
  			sc->sc_bmisscount, n_beacon);
@@ -41,7 +41,7 @@
  			ATH_SCHEDULE_TQUEUE(&sc->sc_bstucktq, needmark);
  		return;
  	}
-@@ -5218,7 +5222,7 @@
+@@ -5228,7 +5232,7 @@
  	 *     check will be true, in which case return
  	 *     without resetting the driver.
  	 */
diff --git a/package/madwifi/patches/370-wdsvap.patch b/package/madwifi/patches/370-wdsvap.patch
index d5307bb4b..0e4b3a85d 100644
--- a/package/madwifi/patches/370-wdsvap.patch
+++ b/package/madwifi/patches/370-wdsvap.patch
@@ -67,7 +67,7 @@
  	/* override with driver methods */
  	vap = &avp->av_vap;
  	avp->av_newstate = vap->iv_newstate;
-@@ -4199,8 +4201,7 @@
+@@ -4209,8 +4211,7 @@
  	if (ic->ic_opmode == IEEE80211_M_STA ||
  	    sc->sc_opmode == HAL_M_IBSS ||	/* NB: AHDEMO too */
  	    (sc->sc_nostabeacons) || sc->sc_scanning ||
@@ -77,7 +77,7 @@
  		rfilt |= HAL_RX_FILTER_BEACON;
  	if (sc->sc_nmonvaps > 0)
  		rfilt |= (HAL_RX_FILTER_CONTROL | HAL_RX_FILTER_BEACON |
-@@ -9020,8 +9021,6 @@
+@@ -9030,8 +9031,6 @@
  		 * set sc->beacons if we might need to restart
                   * them after ath_reset. */
  		if (!sc->sc_beacons &&
diff --git a/package/madwifi/patches/374-nbtt_fix.patch b/package/madwifi/patches/374-nbtt_fix.patch
index 99c13ee5d..d8959435d 100644
--- a/package/madwifi/patches/374-nbtt_fix.patch
+++ b/package/madwifi/patches/374-nbtt_fix.patch
@@ -1,6 +1,6 @@
 --- a/ath/if_ath.c
 +++ b/ath/if_ath.c
-@@ -5474,6 +5474,9 @@
+@@ -5484,6 +5484,9 @@
  		ath_beacon_dturbo_config(vap, intval &
  				~(HAL_BEACON_RESET_TSF | HAL_BEACON_ENA));
  #endif
diff --git a/package/madwifi/patches/375-atim_tsf_update.patch b/package/madwifi/patches/375-atim_tsf_update.patch
index a8a05e03a..534986bbb 100644
--- a/package/madwifi/patches/375-atim_tsf_update.patch
+++ b/package/madwifi/patches/375-atim_tsf_update.patch
@@ -8,7 +8,7 @@
  static int ath_desc_alloc(struct ath_softc *);
  static void ath_desc_free(struct ath_softc *);
  static void ath_desc_swap(struct ath_desc *);
-@@ -2783,6 +2784,72 @@
+@@ -2793,6 +2794,72 @@
  	return 1;
  }
  
@@ -81,7 +81,7 @@
  /*
   * Reset the hardware w/o losing operational state.  This is
   * basically a more efficient way of doing ath_stop, ath_init,
-@@ -5282,6 +5349,7 @@
+@@ -5292,6 +5359,7 @@
  	u_int64_t tsf, hw_tsf;
  	u_int32_t tsftu, hw_tsftu;
  	u_int32_t intval, nexttbtt = 0;
@@ -89,7 +89,7 @@
  	int reset_tsf = 0;
  
  	if (vap == NULL)
-@@ -5289,6 +5357,9 @@
+@@ -5299,6 +5367,9 @@
  
  	ni = vap->iv_bss;
  
@@ -99,7 +99,7 @@
  	hw_tsf = ath_hal_gettsf64(ah);
  	tsf = le64_to_cpu(ni->ni_tstamp.tsf);
  	hw_tsftu = hw_tsf >> 10;
-@@ -5478,15 +5549,27 @@
+@@ -5488,15 +5559,27 @@
  				<= ath_hal_sw_beacon_response_time)
  			nexttbtt += intval;
  		sc->sc_nexttbtt = nexttbtt;
@@ -115,7 +115,7 @@
  		sc->sc_bmisscount = 0;
  		ath_hal_intrset(ah, sc->sc_imask);
 +
-+		if (ath_hw_check_atim(sc, 1, intval & HAL_BEACON_PERIOD)) {
++		if ((sc->sc_opmode == HAL_M_IBSS) && ath_hw_check_atim(sc, 1, intval & HAL_BEACON_PERIOD)) {
 +			DPRINTF(sc, ATH_DEBUG_BEACON,
 +				"fixed atim window after beacon init\n");
 +		}
@@ -127,11 +127,11 @@
  	/* We print all debug messages here, in order to preserve the
  	 * time critical aspect of this function */
  	DPRINTF(sc, ATH_DEBUG_BEACON,
-@@ -6389,6 +6472,11 @@
+@@ -6399,6 +6482,11 @@
  			DPRINTF(sc, ATH_DEBUG_BEACON, 
  				"Updated beacon timers\n");
  		}
-+		if ((sc->sc_opmode == IEEE80211_M_IBSS) &&
++		if ((sc->sc_opmode == HAL_M_IBSS) &&
 +				IEEE80211_ADDR_EQ(ni->ni_bssid, vap->iv_bss->ni_bssid) &&
 +				ath_hw_check_atim(sc, 1, vap->iv_bss->ni_intval)) {
 +			DPRINTF(sc, ATH_DEBUG_ANY, "Fixed ATIM window after beacon recv\n");
diff --git a/package/madwifi/patches/383-ibss_hostap.patch b/package/madwifi/patches/383-ibss_hostap.patch
new file mode 100644
index 000000000..b65c4afe8
--- /dev/null
+++ b/package/madwifi/patches/383-ibss_hostap.patch
@@ -0,0 +1,105 @@
+--- a/ath/if_ath.c
++++ b/ath/if_ath.c
+@@ -1452,6 +1452,23 @@
+ 		sc->sc_nstavaps++;
+ 	else if (opmode == IEEE80211_M_MONITOR)
+ 		sc->sc_nmonvaps++;
++
++
++	/* Driving the HAL in IBSS sometimes adapts the TSF and other timing registers
++	 * from received beacons/probes. If that happens, expected TX interrupts may
++	 * not occur until next reset. Which triggers the "lost beacon" tasklet.
++	 * Resulting effectively in not sending packets for minutes. Because that only
++	 * happens in large mesh networks, this mode needs to be activated by a kernel
++	 * module parameter: hostap_for_ibss=1. Note that using this mode has side
++	 * effects. Such as not supressing beacons/probe answers randomly when
++	 * receiving other node beacons. It's recommended to lower the beacon interval
++	 * then. When using an IBSS-VAP together with an HOSTAP-VAP, you may also need
++	 * to re-trigger IBSS beacon generation after creating the HOSTAP-VAP by
++	 * issueing "iwpriv athX bintval 1000".
++	 */
++	if ((flags & IEEE80211_NO_STABEACONS) && (ic->ic_opmode == IEEE80211_M_IBSS))
++		sc->sc_opmode = HAL_M_HOSTAP;
++	else
+ 	/*
+ 	 * Adhoc demo mode is a pseudo mode; to the HAL it's
+ 	 * just IBSS mode and the driver doesn't use management
+@@ -4279,7 +4296,8 @@
+ 	if (ic->ic_opmode != IEEE80211_M_HOSTAP && (dev->flags & IFF_PROMISC))
+ 		rfilt |= HAL_RX_FILTER_PROM;
+ 	if (ic->ic_opmode == IEEE80211_M_STA ||
+-	    sc->sc_opmode == HAL_M_IBSS ||	/* NB: AHDEMO too */
++	    ic->ic_opmode == IEEE80211_M_IBSS ||
++	    ic->ic_opmode == IEEE80211_M_AHDEMO ||
+ 	    (sc->sc_nostabeacons) || sc->sc_scanning ||
+ 		(ic->ic_opmode == IEEE80211_M_HOSTAP))
+ 		rfilt |= HAL_RX_FILTER_BEACON;
+@@ -6433,6 +6451,33 @@
+ }
+ 
+ /*
++ * Advances (forwards/adds) a microsecond value to current chip's TSF registers
++ */
++
++/* from ath_info.c */
++#define AR5K_TSF_L32_5210		0x806c	/* TSF (lower 32 bits) */
++#define AR5K_TSF_L32_5211		0x804c
++#define AR5K_TSF_L32			(ar_device(ah->ah_sc->devid) == 5210 ? \
++					AR5K_TSF_L32_5210 : AR5K_TSF_L32_5211)
++
++#define AR5K_TSF_U32_5210		0x8070
++#define AR5K_TSF_U32_5211		0x8050
++#define AR5K_TSF_U32			(ar_device(ah->ah_sc->devid) == 5210 ? \
++					AR5K_TSF_U32_5210 : AR5K_TSF_U32_5211)
++
++static inline void ath_hal_settsf64(struct ath_hal *ah, u_int64_t tsf_adv)
++{
++	ATH_HAL_LOCK_IRQ(ah->ah_sc);
++	ath_hal_set_function(__func__);
++	tsf_adv += ah->ah_getTsf64(ah);
++	OS_REG_WRITE(ah, AR5K_TSF_L32, 0ll);
++	OS_REG_WRITE(ah, AR5K_TSF_U32, (tsf_adv >> 32) & 0xffffffffll);
++	OS_REG_WRITE(ah, AR5K_TSF_L32, (tsf_adv >> 00) & 0xffffffffll);
++	ath_hal_set_function(NULL);
++	ATH_HAL_UNLOCK_IRQ(ah->ah_sc);
++}
++
++/*
+  * Intercept management frames to collect beacon RSSI data and to do
+  * ibss merges. This function is called for all management frames,
+  * including those belonging to other BSS.
+@@ -6485,10 +6530,19 @@
+ 			DPRINTF(sc, ATH_DEBUG_BEACON, 
+ 				"Updated beacon timers\n");
+ 		}
+-		if ((sc->sc_opmode == HAL_M_IBSS) &&
+-				IEEE80211_ADDR_EQ(ni->ni_bssid, vap->iv_bss->ni_bssid) &&
+-				ath_hw_check_atim(sc, 1, vap->iv_bss->ni_intval)) {
+-			DPRINTF(sc, ATH_DEBUG_ANY, "Fixed ATIM window after beacon recv\n");
++		if ((vap->iv_opmode == IEEE80211_M_IBSS) &&
++				(sc->sc_opmode == HAL_M_HOSTAP) &&
++				IEEE80211_ADDR_EQ(ni->ni_bssid, vap->iv_bss->ni_bssid)) {
++			/* In this mode, we drive the HAL in HOSTAP mode. Hence
++			 * we do the IBSS merging in software. Also do not merge
++			 * if the difference it too small. Otherwise we are playing
++			 * tsf-pingpong with other vendors drivers */
++			beacon_tsf = le64_to_cpu(ni->ni_tstamp.tsf);
++			if (beacon_tsf > rtsf + 0xffff) {
++				ath_hal_settsf64(sc->sc_ah, beacon_tsf - rtsf);
++				ieee80211_ibss_merge(ni);
++			}
++			break;
+ 		}
+ 		/* NB: Fall Through */
+ 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
+@@ -6561,6 +6615,10 @@
+ #endif
+ 			if (do_merge)
+ 				ieee80211_ibss_merge(ni);
++
++			if ((sc->sc_opmode == HAL_M_IBSS) &&
++					ath_hw_check_atim(sc, 1, vap->iv_bss->ni_intval))
++				DPRINTF(sc, ATH_DEBUG_ANY, "Fixed ATIM window after beacon recv\n");
+ 		}
+ 		break;
+ 	}
diff --git a/package/madwifi/patches/406-monitor_r3711.patch b/package/madwifi/patches/406-monitor_r3711.patch
index 74004ad2e..1304b97e4 100644
--- a/package/madwifi/patches/406-monitor_r3711.patch
+++ b/package/madwifi/patches/406-monitor_r3711.patch
@@ -1,6 +1,6 @@
 --- a/ath/if_ath.c
 +++ b/ath/if_ath.c
-@@ -6409,7 +6409,7 @@
+@@ -6440,7 +6440,7 @@
  
  	/* Never copy the SKB, as it is ours on the RX side, and this is the 
  	 * last process on the TX side and we only modify our own headers. */
@@ -9,7 +9,7 @@
  	if (tskb == NULL) {
  		DPRINTF(sc, ATH_DEBUG_ANY,
  			"Dropping; ath_skb_removepad failed!\n");
-@@ -6417,6 +6417,8 @@
+@@ -6448,6 +6448,8 @@
  	}
  	
  	ieee80211_input_monitor(ic, tskb, bf, tx, tsf, sc);
diff --git a/package/madwifi/patches/408-changeset_r3337.patch b/package/madwifi/patches/408-changeset_r3337.patch
index 5b225650a..8047e2e0f 100644
--- a/package/madwifi/patches/408-changeset_r3337.patch
+++ b/package/madwifi/patches/408-changeset_r3337.patch
@@ -10,7 +10,7 @@
  Please let us know if you think your name should be mentioned here!
 --- a/ath/if_ath.c
 +++ b/ath/if_ath.c
-@@ -3029,7 +3029,7 @@
+@@ -3059,7 +3059,7 @@
  	struct ath_softc *sc = dev->priv;
  	struct ath_hal *ah = sc->sc_ah;
  	struct ieee80211_phy_params *ph = (struct ieee80211_phy_params *)
diff --git a/package/madwifi/patches/450-new_hal.patch b/package/madwifi/patches/450-new_hal.patch
index b9e2454d8..60a7dc33e 100644
--- a/package/madwifi/patches/450-new_hal.patch
+++ b/package/madwifi/patches/450-new_hal.patch
@@ -108,7 +108,7 @@
  	/*
  	 * Check if the MAC has multi-rate retry support.
  	 * We do this by trying to setup a fake extended
-@@ -7382,7 +7390,7 @@
+@@ -7453,7 +7461,7 @@
  	if (qtype == HAL_TX_QUEUE_UAPSD)
  		qi.tqi_qflags = HAL_TXQ_TXDESCINT_ENABLE;
  	else
-- 
2.20.1