ath10k-ct: update to version ath10k-4.16
[openwrt/staging/wigyori.git] / package / kernel / ath10k-ct / patches / 160-ath10k-search-all-IEs-for-variant-before-falling-back.patch
1 From: Thomas Hebb <tommyhebb@gmail.com>
2 Date: Fri, 13 Apr 2018 17:40:26 +0300
3 Subject: [PATCH] ath10k: search all IEs for variant before falling back
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 Signed-off-by: Kalle Valo <kvalo@codeaurora.org>
45
46 Origin: backport, https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=c8489668065a283d3027e86e877b103a87f99d22
47 ---
48 ath10k-4.13/core.c | 134 ++++++++++++++++++---------------
49 1 file changed, 72 insertions(+), 62 deletions(-)
50
51 --- a/ath10k-4.13/core.c
52 +++ b/ath10k-4.13/core.c
53 @@ -1427,14 +1427,61 @@ out:
54 return ret;
55 }
56
57 +static int ath10k_core_search_bd(struct ath10k *ar,
58 + const char *boardname,
59 + const u8 *data,
60 + size_t len)
61 +{
62 + size_t ie_len;
63 + struct ath10k_fw_ie *hdr;
64 + int ret = -ENOENT, ie_id;
65 +
66 + while (len > sizeof(struct ath10k_fw_ie)) {
67 + hdr = (struct ath10k_fw_ie *)data;
68 + ie_id = le32_to_cpu(hdr->id);
69 + ie_len = le32_to_cpu(hdr->len);
70 +
71 + len -= sizeof(*hdr);
72 + data = hdr->data;
73 +
74 + if (len < ALIGN(ie_len, 4)) {
75 + ath10k_err(ar, "invalid length for board ie_id %d ie_len %zu len %zu\n",
76 + ie_id, ie_len, len);
77 + return -EINVAL;
78 + }
79 +
80 + switch (ie_id) {
81 + case ATH10K_BD_IE_BOARD:
82 + ret = ath10k_core_parse_bd_ie_board(ar, data, ie_len,
83 + boardname);
84 + if (ret == -ENOENT)
85 + /* no match found, continue */
86 + break;
87 +
88 + /* either found or error, so stop searching */
89 + goto out;
90 + }
91 +
92 + /* jump over the padding */
93 + ie_len = ALIGN(ie_len, 4);
94 +
95 + len -= ie_len;
96 + data += ie_len;
97 + }
98 +
99 +out:
100 + /* return result of parse_bd_ie_board() or -ENOENT */
101 + return ret;
102 +}
103 +
104 static int ath10k_core_fetch_board_data_api_n(struct ath10k *ar,
105 const char *boardname,
106 + const char *fallback_boardname,
107 const char *filename)
108 {
109 - size_t len, magic_len, ie_len;
110 - struct ath10k_fw_ie *hdr;
111 + size_t len, magic_len;
112 const u8 *data;
113 - int ret, ie_id;
114 + int ret;
115
116 ar->normal_mode_fw.board = ath10k_fetch_fw_file(ar,
117 ar->hw_params.fw.dir,
118 @@ -1472,73 +1519,28 @@ static int ath10k_core_fetch_board_data_
119 data += magic_len;
120 len -= magic_len;
121
122 - while (len > sizeof(struct ath10k_fw_ie)) {
123 - hdr = (struct ath10k_fw_ie *)data;
124 - ie_id = le32_to_cpu(hdr->id);
125 - ie_len = le32_to_cpu(hdr->len);
126 -
127 - len -= sizeof(*hdr);
128 - data = hdr->data;
129 -
130 - if (len < ALIGN(ie_len, 4)) {
131 - ath10k_err(ar, "invalid length for board ie_id %d ie_len %zu len %zu\n",
132 - ie_id, ie_len, len);
133 - ret = -EINVAL;
134 - goto err;
135 - }
136 -
137 - switch (ie_id) {
138 - case ATH10K_BD_IE_BOARD:
139 - ret = ath10k_core_parse_bd_ie_board(ar, data, ie_len,
140 - boardname);
141 - if (ret == -ENOENT && ar->id.bdf_ext[0] != '\0') {
142 - /* try default bdf if variant was not found */
143 - char *s, *v = ",variant=";
144 - char boardname2[100];
145 -
146 - strlcpy(boardname2, boardname,
147 - sizeof(boardname2));
148 -
149 - s = strstr(boardname2, v);
150 - if (s)
151 - *s = '\0'; /* strip ",variant=%s" */
152 -
153 - ret = ath10k_core_parse_bd_ie_board(ar, data,
154 - ie_len,
155 - boardname2);
156 - }
157 -
158 - if (ret == -ENOENT)
159 - /* no match found, continue */
160 - break;
161 - else if (ret)
162 - /* there was an error, bail out */
163 - goto err;
164 -
165 - /* board data found */
166 - goto out;
167 - }
168 + /* attempt to find boardname in the IE list */
169 + ret = ath10k_core_search_bd(ar, boardname, data, len);
170
171 - /* jump over the padding */
172 - ie_len = ALIGN(ie_len, 4);
173 -
174 - len -= ie_len;
175 - data += ie_len;
176 - }
177 + /* if we didn't find it and have a fallback name, try that */
178 + if (ret == -ENOENT && fallback_boardname)
179 + ret = ath10k_core_search_bd(ar, fallback_boardname, data, len);
180
181 out:
182 - if (!ar->normal_mode_fw.board_data || !ar->normal_mode_fw.board_len) {
183 + if (ret == -ENOENT) {
184 ath10k_err(ar,
185 "failed to fetch board data for %s from %s/%s\n",
186 boardname, ar->hw_params.fw.dir, filename);
187 ret = -ENODATA;
188 - goto err;
189 }
190
191 /* Save firmware board name so we can display it later. */
192 strlcpy(ar->normal_mode_fw.fw_file.fw_board_name, filename,
193 sizeof(ar->normal_mode_fw.fw_file.fw_board_name));
194
195 + if (ret)
196 + goto err;
197 +
198 return 0;
199
200 err:
201 @@ -1547,12 +1549,12 @@ err:
202 }
203
204 static int ath10k_core_create_board_name(struct ath10k *ar, char *name,
205 - size_t name_len)
206 + size_t name_len, bool with_variant)
207 {
208 /* strlen(',variant=') + strlen(ar->id.bdf_ext) */
209 char variant[9 + ATH10K_SMBIOS_BDF_EXT_STR_LENGTH] = { 0 };
210
211 - if (ar->id.bdf_ext[0] != '\0')
212 + if (with_variant && ar->id.bdf_ext[0] != '\0')
213 scnprintf(variant, sizeof(variant), ",variant=%s",
214 ar->id.bdf_ext);
215
216 @@ -1578,21 +1580,31 @@ out:
217
218 static int ath10k_core_fetch_board_file(struct ath10k *ar)
219 {
220 - char boardname[100];
221 + char boardname[100], fallback_boardname[100];
222 int ret;
223
224 - ret = ath10k_core_create_board_name(ar, boardname, sizeof(boardname));
225 + ret = ath10k_core_create_board_name(ar, boardname,
226 + sizeof(boardname), true);
227 if (ret) {
228 ath10k_err(ar, "failed to create board name: %d", ret);
229 return ret;
230 }
231
232 + ret = ath10k_core_create_board_name(ar, fallback_boardname,
233 + sizeof(boardname), false);
234 + if (ret) {
235 + ath10k_err(ar, "failed to create fallback board name: %d", ret);
236 + return ret;
237 + }
238 +
239 ar->bd_api = 2;
240 if (ar->fwcfg.bname[0])
241 ret = ath10k_core_fetch_board_data_api_n(ar, boardname,
242 + fallback_boardname,
243 ar->fwcfg.bname);
244 else
245 ret = ath10k_core_fetch_board_data_api_n(ar, boardname,
246 + fallback_boardname,
247 ATH10K_BOARD_API2_FILE);
248 if (!ret)
249 goto success;
250 --- a/ath10k-4.16/core.c
251 +++ b/ath10k-4.16/core.c
252 @@ -1559,14 +1559,61 @@ out:
253 return ret;
254 }
255
256 +static int ath10k_core_search_bd(struct ath10k *ar,
257 + const char *boardname,
258 + const u8 *data,
259 + size_t len)
260 +{
261 + size_t ie_len;
262 + struct ath10k_fw_ie *hdr;
263 + int ret = -ENOENT, ie_id;
264 +
265 + while (len > sizeof(struct ath10k_fw_ie)) {
266 + hdr = (struct ath10k_fw_ie *)data;
267 + ie_id = le32_to_cpu(hdr->id);
268 + ie_len = le32_to_cpu(hdr->len);
269 +
270 + len -= sizeof(*hdr);
271 + data = hdr->data;
272 +
273 + if (len < ALIGN(ie_len, 4)) {
274 + ath10k_err(ar, "invalid length for board ie_id %d ie_len %zu len %zu\n",
275 + ie_id, ie_len, len);
276 + return -EINVAL;
277 + }
278 +
279 + switch (ie_id) {
280 + case ATH10K_BD_IE_BOARD:
281 + ret = ath10k_core_parse_bd_ie_board(ar, data, ie_len,
282 + boardname);
283 + if (ret == -ENOENT)
284 + /* no match found, continue */
285 + break;
286 +
287 + /* either found or error, so stop searching */
288 + goto out;
289 + }
290 +
291 + /* jump over the padding */
292 + ie_len = ALIGN(ie_len, 4);
293 +
294 + len -= ie_len;
295 + data += ie_len;
296 + }
297 +
298 +out:
299 + /* return result of parse_bd_ie_board() or -ENOENT */
300 + return ret;
301 +}
302 +
303 static int ath10k_core_fetch_board_data_api_n(struct ath10k *ar,
304 const char *boardname,
305 + const char *fallback_boardname,
306 const char *filename)
307 {
308 - size_t len, magic_len, ie_len;
309 - struct ath10k_fw_ie *hdr;
310 + size_t len, magic_len;
311 const u8 *data;
312 - int ret, ie_id;
313 + int ret;
314
315 ar->normal_mode_fw.board = ath10k_fetch_fw_file(ar,
316 ar->hw_params.fw.dir,
317 @@ -1604,73 +1651,28 @@ static int ath10k_core_fetch_board_data_
318 data += magic_len;
319 len -= magic_len;
320
321 - while (len > sizeof(struct ath10k_fw_ie)) {
322 - hdr = (struct ath10k_fw_ie *)data;
323 - ie_id = le32_to_cpu(hdr->id);
324 - ie_len = le32_to_cpu(hdr->len);
325 -
326 - len -= sizeof(*hdr);
327 - data = hdr->data;
328 -
329 - if (len < ALIGN(ie_len, 4)) {
330 - ath10k_err(ar, "invalid length for board ie_id %d ie_len %zu len %zu\n",
331 - ie_id, ie_len, len);
332 - ret = -EINVAL;
333 - goto err;
334 - }
335 -
336 - switch (ie_id) {
337 - case ATH10K_BD_IE_BOARD:
338 - ret = ath10k_core_parse_bd_ie_board(ar, data, ie_len,
339 - boardname);
340 - if (ret == -ENOENT && ar->id.bdf_ext[0] != '\0') {
341 - /* try default bdf if variant was not found */
342 - char *s, *v = ",variant=";
343 - char boardname2[100];
344 -
345 - strlcpy(boardname2, boardname,
346 - sizeof(boardname2));
347 -
348 - s = strstr(boardname2, v);
349 - if (s)
350 - *s = '\0'; /* strip ",variant=%s" */
351 -
352 - ret = ath10k_core_parse_bd_ie_board(ar, data,
353 - ie_len,
354 - boardname2);
355 - }
356 -
357 - if (ret == -ENOENT)
358 - /* no match found, continue */
359 - break;
360 - else if (ret)
361 - /* there was an error, bail out */
362 - goto err;
363 -
364 - /* board data found */
365 - goto out;
366 - }
367 + /* attempt to find boardname in the IE list */
368 + ret = ath10k_core_search_bd(ar, boardname, data, len);
369
370 - /* jump over the padding */
371 - ie_len = ALIGN(ie_len, 4);
372 -
373 - len -= ie_len;
374 - data += ie_len;
375 - }
376 + /* if we didn't find it and have a fallback name, try that */
377 + if (ret == -ENOENT && fallback_boardname)
378 + ret = ath10k_core_search_bd(ar, fallback_boardname, data, len);
379
380 out:
381 - if (!ar->normal_mode_fw.board_data || !ar->normal_mode_fw.board_len) {
382 + if (ret == -ENOENT) {
383 ath10k_err(ar,
384 "failed to fetch board data for %s from %s/%s\n",
385 boardname, ar->hw_params.fw.dir, filename);
386 ret = -ENODATA;
387 - goto err;
388 }
389
390 /* Save firmware board name so we can display it later. */
391 strlcpy(ar->normal_mode_fw.fw_file.fw_board_name, filename,
392 sizeof(ar->normal_mode_fw.fw_file.fw_board_name));
393
394 + if (ret)
395 + goto err;
396 +
397 return 0;
398
399 err:
400 @@ -1679,12 +1681,12 @@ err:
401 }
402
403 static int ath10k_core_create_board_name(struct ath10k *ar, char *name,
404 - size_t name_len)
405 + size_t name_len, bool with_variant)
406 {
407 /* strlen(',variant=') + strlen(ar->id.bdf_ext) */
408 char variant[9 + ATH10K_SMBIOS_BDF_EXT_STR_LENGTH] = { 0 };
409
410 - if (ar->id.bdf_ext[0] != '\0')
411 + if (with_variant && ar->id.bdf_ext[0] != '\0')
412 scnprintf(variant, sizeof(variant), ",variant=%s",
413 ar->id.bdf_ext);
414
415 @@ -1710,21 +1712,31 @@ out:
416
417 static int ath10k_core_fetch_board_file(struct ath10k *ar)
418 {
419 - char boardname[100];
420 + char boardname[100], fallback_boardname[100];
421 int ret;
422
423 - ret = ath10k_core_create_board_name(ar, boardname, sizeof(boardname));
424 + ret = ath10k_core_create_board_name(ar, boardname,
425 + sizeof(boardname), true);
426 if (ret) {
427 ath10k_err(ar, "failed to create board name: %d", ret);
428 return ret;
429 }
430
431 + ret = ath10k_core_create_board_name(ar, fallback_boardname,
432 + sizeof(boardname), false);
433 + if (ret) {
434 + ath10k_err(ar, "failed to create fallback board name: %d", ret);
435 + return ret;
436 + }
437 +
438 ar->bd_api = 2;
439 if (ar->fwcfg.bname[0])
440 ret = ath10k_core_fetch_board_data_api_n(ar, boardname,
441 + fallback_boardname,
442 ar->fwcfg.bname);
443 else
444 ret = ath10k_core_fetch_board_data_api_n(ar, boardname,
445 + fallback_boardname,
446 ATH10K_BOARD_API2_FILE);
447 if (!ret)
448 goto success;