mac80211: ath10k: search all IEs for variant before falling back
[openwrt/openwrt.git] / package / kernel / mac80211 / patches / 160-ath10k-search-all-IEs-for-variant-before-falling-back.patch
1 From: Thomas Hebb <tommyhebb@gmail.com>
2 Subject: [PATCH] ath10k: search all IEs for variant before falling back
3 Date: Wed, 21 Feb 2018 11:43:39 -0500
4
5 commit f2593cb1b291 ("ath10k: Search SMBIOS for OEM board file
6 extension") added a feature to ath10k that allows Board Data File
7 (BDF) conflicts between multiple devices that use the same device IDs
8 but have different calibration requirements to be resolved by allowing
9 a "variant" string to be stored in SMBIOS [and later device tree, added
10 by commit d06f26c5c8a4 ("ath10k: search DT for qcom,ath10k-calibration-
11 variant")] that gets appended to the ID stored in board-2.bin.
12
13 This original patch had a regression, however. Namely that devices with
14 a variant present in SMBIOS that didn't need custom BDFs could no longer
15 find the default BDF, which has no variant appended. The patch was
16 reverted and re-applied with a fix for this issue in commit 1657b8f84ed9
17 ("search SMBIOS for OEM board file extension").
18
19 But the fix to fall back to a default BDF introduced another issue: the
20 driver currently parses IEs in board-2.bin one by one, and for each one
21 it first checks to see if it matches the ID with the variant appended.
22 If it doesn't, it checks to see if it matches the "fallback" ID with no
23 variant. If a matching BDF is found at any point during this search, the
24 search is terminated and that BDF is used. The issue is that it's very
25 possible (and is currently the case for board-2.bin files present in the
26 ath10k-firmware repository) for the default BDF to occur in an earlier
27 IE than the variant-specific BDF. In this case, the current code will
28 happily choose the default BDF even though a better-matching BDF is
29 present later in the file.
30
31 This patch fixes the issue by first searching the entire file for the ID
32 with variant, and searching for the fallback ID only if that search
33 fails. It also includes some code cleanup in the area, as
34 ath10k_core_fetch_board_data_api_n() no longer does its own string
35 mangling to remove the variant from an ID, instead leaving that job to a
36 new flag passed to ath10k_core_create_board_name().
37
38 I've tested this patch on a QCA4019 and verified that the driver behaves
39 correctly for 1) both fallback and variant BDFs present, 2) only fallback
40 BDF present, and 3) no matching BDFs present.
41
42 Fixes: 1657b8f84ed9 ("ath10k: search SMBIOS for OEM board file extension")
43 Signed-off-by: Thomas Hebb <tommyhebb@gmail.com>
44 ---
45 drivers/net/wireless/ath/ath10k/core.c | 134 ++++++++++++++++++---------------
46 1 file changed, 72 insertions(+), 62 deletions(-)
47
48 --- a/drivers/net/wireless/ath/ath10k/core.c
49 +++ b/drivers/net/wireless/ath/ath10k/core.c
50 @@ -1129,14 +1129,61 @@ out:
51 return ret;
52 }
53
54 +static int ath10k_core_search_bd(struct ath10k *ar,
55 + const char *boardname,
56 + const u8 *data,
57 + size_t len)
58 +{
59 + size_t ie_len;
60 + struct ath10k_fw_ie *hdr;
61 + int ret = -ENOENT, ie_id;
62 +
63 + while (len > sizeof(struct ath10k_fw_ie)) {
64 + hdr = (struct ath10k_fw_ie *)data;
65 + ie_id = le32_to_cpu(hdr->id);
66 + ie_len = le32_to_cpu(hdr->len);
67 +
68 + len -= sizeof(*hdr);
69 + data = hdr->data;
70 +
71 + if (len < ALIGN(ie_len, 4)) {
72 + ath10k_err(ar, "invalid length for board ie_id %d ie_len %zu len %zu\n",
73 + ie_id, ie_len, len);
74 + return -EINVAL;
75 + }
76 +
77 + switch (ie_id) {
78 + case ATH10K_BD_IE_BOARD:
79 + ret = ath10k_core_parse_bd_ie_board(ar, data, ie_len,
80 + boardname);
81 + if (ret == -ENOENT)
82 + /* no match found, continue */
83 + break;
84 +
85 + /* either found or error, so stop searching */
86 + goto out;
87 + }
88 +
89 + /* jump over the padding */
90 + ie_len = ALIGN(ie_len, 4);
91 +
92 + len -= ie_len;
93 + data += ie_len;
94 + }
95 +
96 +out:
97 + /* return result of parse_bd_ie_board() or -ENOENT */
98 + return ret;
99 +}
100 +
101 static int ath10k_core_fetch_board_data_api_n(struct ath10k *ar,
102 const char *boardname,
103 + const char *fallback_boardname,
104 const char *filename)
105 {
106 - size_t len, magic_len, ie_len;
107 - struct ath10k_fw_ie *hdr;
108 + size_t len, magic_len;
109 const u8 *data;
110 - int ret, ie_id;
111 + int ret;
112
113 ar->normal_mode_fw.board = ath10k_fetch_fw_file(ar,
114 ar->hw_params.fw.dir,
115 @@ -1174,69 +1221,23 @@ static int ath10k_core_fetch_board_data_
116 data += magic_len;
117 len -= magic_len;
118
119 - while (len > sizeof(struct ath10k_fw_ie)) {
120 - hdr = (struct ath10k_fw_ie *)data;
121 - ie_id = le32_to_cpu(hdr->id);
122 - ie_len = le32_to_cpu(hdr->len);
123 -
124 - len -= sizeof(*hdr);
125 - data = hdr->data;
126 -
127 - if (len < ALIGN(ie_len, 4)) {
128 - ath10k_err(ar, "invalid length for board ie_id %d ie_len %zu len %zu\n",
129 - ie_id, ie_len, len);
130 - ret = -EINVAL;
131 - goto err;
132 - }
133 + /* attempt to find boardname in the IE list */
134 + ret = ath10k_core_search_bd(ar, boardname, data, len);
135
136 - switch (ie_id) {
137 - case ATH10K_BD_IE_BOARD:
138 - ret = ath10k_core_parse_bd_ie_board(ar, data, ie_len,
139 - boardname);
140 - if (ret == -ENOENT && ar->id.bdf_ext[0] != '\0') {
141 - /* try default bdf if variant was not found */
142 - char *s, *v = ",variant=";
143 - char boardname2[100];
144 -
145 - strlcpy(boardname2, boardname,
146 - sizeof(boardname2));
147 -
148 - s = strstr(boardname2, v);
149 - if (s)
150 - *s = '\0'; /* strip ",variant=%s" */
151 -
152 - ret = ath10k_core_parse_bd_ie_board(ar, data,
153 - ie_len,
154 - boardname2);
155 - }
156 + /* if we didn't find it and have a fallback name, try that */
157 + if (ret == -ENOENT && fallback_boardname)
158 + ret = ath10k_core_search_bd(ar, fallback_boardname, data, len);
159
160 - if (ret == -ENOENT)
161 - /* no match found, continue */
162 - break;
163 - else if (ret)
164 - /* there was an error, bail out */
165 - goto err;
166 -
167 - /* board data found */
168 - goto out;
169 - }
170 -
171 - /* jump over the padding */
172 - ie_len = ALIGN(ie_len, 4);
173 -
174 - len -= ie_len;
175 - data += ie_len;
176 - }
177 -
178 -out:
179 - if (!ar->normal_mode_fw.board_data || !ar->normal_mode_fw.board_len) {
180 + if (ret == -ENOENT) {
181 ath10k_err(ar,
182 "failed to fetch board data for %s from %s/%s\n",
183 boardname, ar->hw_params.fw.dir, filename);
184 ret = -ENODATA;
185 - goto err;
186 }
187
188 + if (ret)
189 + goto err;
190 +
191 return 0;
192
193 err:
194 @@ -1245,12 +1246,12 @@ err:
195 }
196
197 static int ath10k_core_create_board_name(struct ath10k *ar, char *name,
198 - size_t name_len)
199 + size_t name_len, bool with_variant)
200 {
201 /* strlen(',variant=') + strlen(ar->id.bdf_ext) */
202 char variant[9 + ATH10K_SMBIOS_BDF_EXT_STR_LENGTH] = { 0 };
203
204 - if (ar->id.bdf_ext[0] != '\0')
205 + if (with_variant && ar->id.bdf_ext[0] != '\0')
206 scnprintf(variant, sizeof(variant), ",variant=%s",
207 ar->id.bdf_ext);
208
209 @@ -1276,17 +1277,26 @@ out:
210
211 static int ath10k_core_fetch_board_file(struct ath10k *ar)
212 {
213 - char boardname[100];
214 + char boardname[100], fallback_boardname[100];
215 int ret;
216
217 - ret = ath10k_core_create_board_name(ar, boardname, sizeof(boardname));
218 + ret = ath10k_core_create_board_name(ar, boardname,
219 + sizeof(boardname), true);
220 if (ret) {
221 ath10k_err(ar, "failed to create board name: %d", ret);
222 return ret;
223 }
224
225 + ret = ath10k_core_create_board_name(ar, fallback_boardname,
226 + sizeof(boardname), false);
227 + if (ret) {
228 + ath10k_err(ar, "failed to create fallback board name: %d", ret);
229 + return ret;
230 + }
231 +
232 ar->bd_api = 2;
233 ret = ath10k_core_fetch_board_data_api_n(ar, boardname,
234 + fallback_boardname,
235 ATH10K_BOARD_API2_FILE);
236 if (!ret)
237 goto success;