From c662299bf98b6214628168379edd642f1b942b98 Mon Sep 17 00:00:00 2001
From: Hauke Mehrtens <hauke@hauke-m.de>
Date: Sun, 23 Sep 2018 20:15:52 +0200
Subject: [PATCH] ath10k-ct: update to version ath10k-4.16

Signed-off-by: Hauke Mehrtens <hauke@hauke-m.de>
---
 package/kernel/ath10k-ct/Makefile             |   2 +-
 .../081-ath10k-calibration-variant.patch      |   6 +-
 .../ath10k-ct/patches/100-kernel_compat.patch |  13 +-
 ...-IEs-for-variant-before-falling-back.patch | 207 +++++++++++++++++-
 4 files changed, 219 insertions(+), 9 deletions(-)

diff --git a/package/kernel/ath10k-ct/Makefile b/package/kernel/ath10k-ct/Makefile
index 1a94582a66..366515189a 100644
--- a/package/kernel/ath10k-ct/Makefile
+++ b/package/kernel/ath10k-ct/Makefile
@@ -15,7 +15,7 @@ PKG_MIRROR_HASH:=aa77f388727ff2201de615c3211ab5c0b2dc5e3c041bd1cde4d6eebde0ca437
 # Build the 4.13 ath10k-ct driver version.  Other options are "-4.9", or
 # leave un-defined for 4.7 kernel.  Probably this should match as closely as
 # possible to whatever mac80211 backports version is being used.
-CT_KVER="-4.13"
+CT_KVER="-4.16"
 
 PKG_MAINTAINER:=Ben Greear <greearb@candelatech.com>
 PKG_BUILD_PARALLEL:=1
diff --git a/package/kernel/ath10k-ct/patches/081-ath10k-calibration-variant.patch b/package/kernel/ath10k-ct/patches/081-ath10k-calibration-variant.patch
index d51451b596..1752bf8c95 100644
--- a/package/kernel/ath10k-ct/patches/081-ath10k-calibration-variant.patch
+++ b/package/kernel/ath10k-ct/patches/081-ath10k-calibration-variant.patch
@@ -43,7 +43,7 @@ Origin: upstream, https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux
 
 --- a/ath10k-4.13/core.c
 +++ b/ath10k-4.13/core.c
-@@ -889,6 +889,28 @@ static int ath10k_core_check_smbios(stru
+@@ -891,6 +891,28 @@ static int ath10k_core_check_smbios(stru
  	return 0;
  }
  
@@ -72,7 +72,7 @@ Origin: upstream, https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux
  static int ath10k_download_and_run_otp(struct ath10k *ar)
  {
  	u32 result, address = ar->hw_params.patch_load_addr;
-@@ -1522,19 +1544,19 @@ static int ath10k_core_create_board_name
+@@ -1530,19 +1552,19 @@ static int ath10k_core_create_board_name
  	/* strlen(',variant=') + strlen(ar->id.bdf_ext) */
  	char variant[9 + ATH10K_SMBIOS_BDF_EXT_STR_LENGTH] = { 0 };
  
@@ -98,7 +98,7 @@ Origin: upstream, https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux
  	scnprintf(name, name_len,
  		  "bus=%s,vendor=%04x,device=%04x,subsystem-vendor=%04x,subsystem-device=%04x%s",
  		  ath10k_bus_str(ar->hif.bus),
-@@ -2964,7 +2986,11 @@ static int ath10k_core_probe_fw(struct a
+@@ -2975,7 +2997,11 @@ static int ath10k_core_probe_fw(struct a
  
  	ret = ath10k_core_check_smbios(ar);
  	if (ret)
diff --git a/package/kernel/ath10k-ct/patches/100-kernel_compat.patch b/package/kernel/ath10k-ct/patches/100-kernel_compat.patch
index b2ec10bb61..df7a3b2a86 100644
--- a/package/kernel/ath10k-ct/patches/100-kernel_compat.patch
+++ b/package/kernel/ath10k-ct/patches/100-kernel_compat.patch
@@ -97,7 +97,18 @@
  	ATH10K_DFS_STAT_INC(ar, pulses_total);
 --- a/ath10k-4.13/wmi.c
 +++ b/ath10k-4.13/wmi.c
-@@ -3990,7 +3990,7 @@ static void ath10k_dfs_radar_report(stru
+@@ -4031,7 +4031,7 @@ static void ath10k_dfs_radar_report(stru
+ 
+ 	ATH10K_DFS_STAT_INC(ar, pulses_detected);
+ 
+-	if (!ar->dfs_detector->add_pulse(ar->dfs_detector, &pe)) {
++	if (!ar->dfs_detector->add_pulse(ar->dfs_detector, &pe, NULL)) {
+ 		ath10k_dbg(ar, ATH10K_DBG_REGULATORY,
+ 			   "dfs no pulse pattern detected, yet\n");
+ 		return;
+--- a/ath10k-4.16/wmi.c
++++ b/ath10k-4.16/wmi.c
+@@ -4061,7 +4061,7 @@ static void ath10k_dfs_radar_report(stru
  
  	ATH10K_DFS_STAT_INC(ar, pulses_detected);
  
diff --git a/package/kernel/ath10k-ct/patches/160-ath10k-search-all-IEs-for-variant-before-falling-back.patch b/package/kernel/ath10k-ct/patches/160-ath10k-search-all-IEs-for-variant-before-falling-back.patch
index a82bbda3cc..3788b81102 100644
--- a/package/kernel/ath10k-ct/patches/160-ath10k-search-all-IEs-for-variant-before-falling-back.patch
+++ b/package/kernel/ath10k-ct/patches/160-ath10k-search-all-IEs-for-variant-before-falling-back.patch
@@ -50,7 +50,7 @@ Origin: backport, https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux
 
 --- a/ath10k-4.13/core.c
 +++ b/ath10k-4.13/core.c
-@@ -1419,14 +1419,61 @@ out:
+@@ -1427,14 +1427,61 @@ out:
  	return ret;
  }
  
@@ -115,7 +115,7 @@ Origin: backport, https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux
  
  	ar->normal_mode_fw.board = ath10k_fetch_fw_file(ar,
  							ar->hw_params.fw.dir,
-@@ -1464,73 +1511,28 @@ static int ath10k_core_fetch_board_data_
+@@ -1472,73 +1519,28 @@ static int ath10k_core_fetch_board_data_
  	data += magic_len;
  	len -= magic_len;
  
@@ -198,7 +198,7 @@ Origin: backport, https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux
  	return 0;
  
  err:
-@@ -1539,12 +1541,12 @@ err:
+@@ -1547,12 +1549,12 @@ err:
  }
  
  static int ath10k_core_create_board_name(struct ath10k *ar, char *name,
@@ -213,7 +213,206 @@ Origin: backport, https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux
  		scnprintf(variant, sizeof(variant), ",variant=%s",
  			  ar->id.bdf_ext);
  
-@@ -1570,21 +1572,31 @@ out:
+@@ -1578,21 +1580,31 @@ out:
+ 
+ static int ath10k_core_fetch_board_file(struct ath10k *ar)
+ {
+-	char boardname[100];
++	char boardname[100], fallback_boardname[100];
+ 	int ret;
+ 
+-	ret = ath10k_core_create_board_name(ar, boardname, sizeof(boardname));
++	ret = ath10k_core_create_board_name(ar, boardname,
++					    sizeof(boardname), true);
+ 	if (ret) {
+ 		ath10k_err(ar, "failed to create board name: %d", ret);
+ 		return ret;
+ 	}
+ 
++	ret = ath10k_core_create_board_name(ar, fallback_boardname,
++					    sizeof(boardname), false);
++	if (ret) {
++		ath10k_err(ar, "failed to create fallback board name: %d", ret);
++		return ret;
++	}
++
+ 	ar->bd_api = 2;
+ 	if (ar->fwcfg.bname[0])
+ 		ret = ath10k_core_fetch_board_data_api_n(ar, boardname,
++							 fallback_boardname,
+ 							 ar->fwcfg.bname);
+ 	else
+ 		ret = ath10k_core_fetch_board_data_api_n(ar, boardname,
++							 fallback_boardname,
+ 							 ATH10K_BOARD_API2_FILE);
+ 	if (!ret)
+ 		goto success;
+--- a/ath10k-4.16/core.c
++++ b/ath10k-4.16/core.c
+@@ -1559,14 +1559,61 @@ out:
+ 	return ret;
+ }
+ 
++static int ath10k_core_search_bd(struct ath10k *ar,
++				 const char *boardname,
++				 const u8 *data,
++				 size_t len)
++{
++	size_t ie_len;
++	struct ath10k_fw_ie *hdr;
++	int ret = -ENOENT, ie_id;
++
++	while (len > sizeof(struct ath10k_fw_ie)) {
++		hdr = (struct ath10k_fw_ie *)data;
++		ie_id = le32_to_cpu(hdr->id);
++		ie_len = le32_to_cpu(hdr->len);
++
++		len -= sizeof(*hdr);
++		data = hdr->data;
++
++		if (len < ALIGN(ie_len, 4)) {
++			ath10k_err(ar, "invalid length for board ie_id %d ie_len %zu len %zu\n",
++				   ie_id, ie_len, len);
++			return -EINVAL;
++		}
++
++		switch (ie_id) {
++		case ATH10K_BD_IE_BOARD:
++			ret = ath10k_core_parse_bd_ie_board(ar, data, ie_len,
++							    boardname);
++			if (ret == -ENOENT)
++				/* no match found, continue */
++				break;
++
++			/* either found or error, so stop searching */
++			goto out;
++		}
++
++		/* jump over the padding */
++		ie_len = ALIGN(ie_len, 4);
++
++		len -= ie_len;
++		data += ie_len;
++	}
++
++out:
++	/* return result of parse_bd_ie_board() or -ENOENT */
++	return ret;
++}
++
+ static int ath10k_core_fetch_board_data_api_n(struct ath10k *ar,
+ 					      const char *boardname,
++					      const char *fallback_boardname,
+ 					      const char *filename)
+ {
+-	size_t len, magic_len, ie_len;
+-	struct ath10k_fw_ie *hdr;
++	size_t len, magic_len;
+ 	const u8 *data;
+-	int ret, ie_id;
++	int ret;
+ 
+ 	ar->normal_mode_fw.board = ath10k_fetch_fw_file(ar,
+ 							ar->hw_params.fw.dir,
+@@ -1604,73 +1651,28 @@ static int ath10k_core_fetch_board_data_
+ 	data += magic_len;
+ 	len -= magic_len;
+ 
+-	while (len > sizeof(struct ath10k_fw_ie)) {
+-		hdr = (struct ath10k_fw_ie *)data;
+-		ie_id = le32_to_cpu(hdr->id);
+-		ie_len = le32_to_cpu(hdr->len);
+-
+-		len -= sizeof(*hdr);
+-		data = hdr->data;
+-
+-		if (len < ALIGN(ie_len, 4)) {
+-			ath10k_err(ar, "invalid length for board ie_id %d ie_len %zu len %zu\n",
+-				   ie_id, ie_len, len);
+-			ret = -EINVAL;
+-			goto err;
+-		}
+-
+-		switch (ie_id) {
+-		case ATH10K_BD_IE_BOARD:
+-			ret = ath10k_core_parse_bd_ie_board(ar, data, ie_len,
+-							    boardname);
+-			if (ret == -ENOENT && ar->id.bdf_ext[0] != '\0') {
+-				/* try default bdf if variant was not found */
+-				char *s, *v = ",variant=";
+-				char boardname2[100];
+-
+-				strlcpy(boardname2, boardname,
+-					sizeof(boardname2));
+-
+-				s = strstr(boardname2, v);
+-				if (s)
+-					*s = '\0';  /* strip ",variant=%s" */
+-
+-				ret = ath10k_core_parse_bd_ie_board(ar, data,
+-								    ie_len,
+-								    boardname2);
+-			}
+-
+-			if (ret == -ENOENT)
+-				/* no match found, continue */
+-				break;
+-			else if (ret)
+-				/* there was an error, bail out */
+-				goto err;
+-
+-			/* board data found */
+-			goto out;
+-		}
++	/* attempt to find boardname in the IE list */
++	ret = ath10k_core_search_bd(ar, boardname, data, len);
+ 
+-		/* jump over the padding */
+-		ie_len = ALIGN(ie_len, 4);
+-
+-		len -= ie_len;
+-		data += ie_len;
+-	}
++	/* if we didn't find it and have a fallback name, try that */
++	if (ret == -ENOENT && fallback_boardname)
++		ret = ath10k_core_search_bd(ar, fallback_boardname, data, len);
+ 
+ out:
+-	if (!ar->normal_mode_fw.board_data || !ar->normal_mode_fw.board_len) {
++	if (ret == -ENOENT) {
+ 		ath10k_err(ar,
+ 			   "failed to fetch board data for %s from %s/%s\n",
+ 			   boardname, ar->hw_params.fw.dir, filename);
+ 		ret = -ENODATA;
+-		goto err;
+ 	}
+ 
+ 	/* Save firmware board name so we can display it later. */
+ 	strlcpy(ar->normal_mode_fw.fw_file.fw_board_name, filename,
+ 		sizeof(ar->normal_mode_fw.fw_file.fw_board_name));
+ 
++	if (ret)
++		goto err;
++
+ 	return 0;
+ 
+ err:
+@@ -1679,12 +1681,12 @@ err:
+ }
+ 
+ static int ath10k_core_create_board_name(struct ath10k *ar, char *name,
+-					 size_t name_len)
++					 size_t name_len, bool with_variant)
+ {
+ 	/* strlen(',variant=') + strlen(ar->id.bdf_ext) */
+ 	char variant[9 + ATH10K_SMBIOS_BDF_EXT_STR_LENGTH] = { 0 };
+ 
+-	if (ar->id.bdf_ext[0] != '\0')
++	if (with_variant && ar->id.bdf_ext[0] != '\0')
+ 		scnprintf(variant, sizeof(variant), ",variant=%s",
+ 			  ar->id.bdf_ext);
+ 
+@@ -1710,21 +1712,31 @@ out:
  
  static int ath10k_core_fetch_board_file(struct ath10k *ar)
  {
-- 
2.30.2